Files
wekan/server/lib/syncHistoryBatch.js
T
Lauri Ojansivu af1b1bee2d Make the checklist rule actions durable in Sync.
checkAll, uncheckAll, checkItem and uncheckItem join the durable rule
actions. The command is captured with performAction's own lookups - the
checklist by title on the card, the items in the order ReactiveCache returns
them, the bulk selection, and checkItem's item query as it is - so a replay
acts on the same items with the same outcome, including the ordinary
action's no-ops.

The ordinary action writes each item in turn, and the checklist item hooks
record on every write, changed or not: uncompleteChecklist before it when
the whole checklist was finished, checkedItem or uncheckedItem after it, and
completeChecklist when the whole checklist is finished - plus a History row
on the item when isFinished changed. The command simulates exactly that on
the captured items and saves one unit per item. The runner goes item by item:
the before-write activity, the conditional write with the item's three hooks
deferred, its History, its after-write activities - each delivered durably -
so a rule triggered by one item runs before the next, as ordinarily.

The History planner now accepts checklist-item rows (entity and card kept
apart), and the recording deferral can name one checklist item. Unit tests
cover the hook sequence for check-all, uncheck-all and hidden-all lists,
no-ops and tampering; a full-app test runs a "check every item" rule through
the stored rule stage - two checkedItem, one completeChecklist, two History
rows, and nothing more on replay. 566 server tests pass.

Thanks to xet7 !
2026-09-30 12:17:20 +03:00

227 lines
13 KiB
JavaScript

'use strict';
const { diffFields, groupForField, valueFromContent } = require('../../models/lib/changeHistoryGroups');
const { canonical, sha256, rowHashIsValid, PROTECTED } = require('../../models/lib/changeHistoryIntegrity');
const { prepareSyncOperationMutation } = require('./syncOperationMutation');
const { EJSON } = require('bson');
const { syncOperationEffectId } = require('./syncOperationApply');
const { exactFieldSelector } = require('../../models/lib/exactFieldSelector');
const copy = value => EJSON.parse(EJSON.stringify(value), { relaxed: true });
const fail = () => { throw new Error('sync-history-plan-invalid'); };
// Old History rows predate integrity hashing. Keep their exact observed
// payload privately; never install a retroactive hash or accept a damaged one.
const unhashed = row => row && (!Object.hasOwn(row, 'integrityHash') ||
row.integrityHash === null || row.integrityHash === '');
function legacySnapshot(row) {
const snapshot = { ...row };
delete snapshot.superseded;
return snapshot;
}
function redoTarget(row) {
if (!row || row.undone !== true || row.superseded === true) fail();
const legacyRow = unhashed(row) ? legacySnapshot(row) : null;
if (!legacyRow && !rowHashIsValid(row)) fail();
if (legacyRow && canonical(copy(legacyRow)) !== canonical(legacyRow)) fail();
return { _id: row._id, boardId: row.boardId, userId: row.userId,
undoneAt: row.undoneAt, ...(legacyRow
? { legacyRow, snapshotHash: sha256(canonical(legacyRow)) }
: { integrityHash: row.integrityHash }) };
}
// Prepare BEFORE mutation and persist this exact plan in the owning journal.
// The caller supplies the redo candidates; retries never invalidate newly
// undone rows.
//
// The rows are CONTENT only (maintainer decision of 2026-09-30): no
// previousHash and no integrityHash. Each row is linked to the board's chain
// when it is appended (persistSyncFieldHistory), exactly like an ordinary
// edit, so a plan never holds the chain between planning and writing and
// ordinary History is never blocked or queued behind a Sync batch.
// The card fields a planned History row may record: Sync's own, and the ones
// durable rule card actions change (server/lib/syncRuleCardCommand.js).
const SYNC_FIELDS = ['title', 'description', 'spentTime', 'customFields', 'archived'];
const RULE_CARD_FIELDS = ['labelIds', 'color', 'dueComplete', 'startAt', 'endAt', 'dueAt', 'receivedAt', 'members'];
// ...and the checklist item field durable rule checklist actions change
// (server/lib/syncRuleChecklistCommand.js), recorded on the item.
const RULE_CHECKLIST_ITEM_FIELDS = ['isFinished'];
const entityOf = fields => (fields === RULE_CHECKLIST_ITEM_FIELDS ? 'checklistItem'
: [SYNC_FIELDS, RULE_CARD_FIELDS].includes(fields) ? 'card' : fail());
function prepareSyncFieldHistory({ step, effectId, userId, createdAt, redoRows = [], ...rest }) {
prepareSyncOperationMutation(step);
if (Object.keys(rest).length) fail();
// Ordinary creation records an activity, not field-by-field History. Its
// separate durable activity adapter must still acknowledge downstream work.
return prepareCardFieldHistory({ before: step.kind === 'create' ? null : step.before, after: step.after,
effectId, userId, createdAt, redoRows, fields: SYNC_FIELDS });
}
// The rows one planned card change records, one per changed field, with the
// redo rows it supersedes. `fields` bounds which fields may be recorded.
// For a checklist item, `entityId` is the item and `cardId` its card.
function prepareCardFieldHistory({ before, after, effectId, userId, createdAt, redoRows = [], fields: allowed,
entityId = after?._id, cardId = after?._id }) {
const entityType = entityOf(allowed);
if (!/^[a-f0-9]{64}$/.test(effectId) || typeof userId !== 'string' || !userId ||
!(createdAt instanceof Date) || !Number.isFinite(createdAt.getTime()) || !after ||
typeof entityId !== 'string' || !entityId || typeof cardId !== 'string' || !cardId) fail();
const fields = [...new Set([...Object.keys(before || {}), ...Object.keys(after)])].sort();
const changes = before === null ? [] : diffFields(entityType, before, after, fields);
const rows = changes.map(change => {
const row = { _id: `sync-history-${sha256(canonical([effectId, change.field]))}`,
boardId: after.boardId, swimlaneId: after.swimlaneId ?? null, listId: after.listId,
cardId, entityType, entityId, group: change.group,
changeType: change.changeType, previousContent: change.previousContent, newContent: change.newContent,
userId, batchId: `sync-${effectId}`, restoredFromId: null, restoredByUserId: null,
createdAt: new Date(createdAt), undone: false, undoneAt: null, superseded: false,
isCheckpoint: false };
return row;
});
if (!Array.isArray(redoRows) || redoRows.length > 10000) fail();
const redo = rows.length ? redoRows.map(redoTarget) : [];
const plan = { effectId, boardId: after.boardId, userId, rows, redo };
validatePlan(plan, allowed);
return copy(plan);
}
// One planner per preparation attempt. The journal invokes it in index order;
// index zero also resets a reused planner after interrupted preparation. Once
// persisted, replay reads these plans and never invokes the planner again.
function createSyncHistoryPlanner(options) {
const captured = copy(options);
let operationId, nextIndex, boardId, redoRows;
return (step, context) => {
const effectId = syncOperationEffectId(context.operationId, context.index);
const first = context.index === 0;
if (!first && (operationId !== context.operationId || nextIndex !== context.index ||
boardId !== step.after?.boardId)) fail();
const plan = prepareSyncFieldHistory({ ...captured, step, effectId,
redoRows: first ? captured.redoRows ?? [] : redoRows });
// Advance only after successful validation. Baseline-only changes do not
// consume the original redo candidates.
operationId = context.operationId;
boardId = step.after.boardId;
nextIndex = context.index + 1;
redoRows = plan.rows.length ? [] : (first ? captured.redoRows ?? [] : redoRows);
return plan;
};
}
function validatePlan(plan, allowed = SYNC_FIELDS) {
const entityType = entityOf(allowed);
if (!plan || Object.keys(plan).sort().join(',') !== 'boardId,effectId,redo,rows,userId' ||
typeof plan.effectId !== 'string' || !/^[a-f0-9]{64}$/.test(plan.effectId) ||
!['boardId', 'userId'].every(key => typeof plan[key] === 'string' && plan[key]) ||
!Array.isArray(plan.rows) || plan.rows.length > 16 || !Array.isArray(plan.redo) || plan.redo.length > 10000 ||
(!plan.rows.length && plan.redo.length)) fail();
const ids = new Set();
for (const row of plan.rows) {
const field = row.newContent?.field || row.previousContent?.field;
const keys = '_id,batchId,boardId,cardId,changeType,createdAt,entityId,entityType,group,isCheckpoint,listId,newContent,previousContent,restoredByUserId,restoredFromId,superseded,swimlaneId,undone,undoneAt,userId';
if (Object.keys(row).sort().join(',') !== keys || !allowed.includes(field) ||
!(row.createdAt instanceof Date) || !Number.isFinite(row.createdAt.getTime()) ||
!['entityId', 'listId'].every(key => typeof row[key] === 'string' && row[key]) ||
row._id !== `sync-history-${sha256(canonical([plan.effectId, field]))}` ||
ids.has(row._id) || row.boardId !== plan.boardId || row.userId !== plan.userId ||
row.batchId !== `sync-${plan.effectId}` || row.entityType !== entityType ||
(entityType === 'card' ? row.cardId !== row.entityId : typeof row.cardId !== 'string' || !row.cardId) ||
row.group !== groupForField(entityType, field) || !['added', 'removed', 'edited'].includes(row.changeType) ||
row.restoredFromId !== null || row.restoredByUserId !== null ||
row.isCheckpoint !== false || row.undone !== false || row.undoneAt !== null || row.superseded !== false) fail();
for (const content of [row.previousContent, row.newContent]) {
if (content !== null) {
if (content.field !== field) fail();
valueFromContent(content);
}
}
ids.add(row._id);
}
for (const row of plan.redo) {
if (Object.keys(row).sort().join(',') !== (Object.hasOwn(row, 'legacyRow')
? '_id,boardId,legacyRow,snapshotHash,undoneAt,userId' : '_id,boardId,integrityHash,undoneAt,userId') ||
typeof row._id !== 'string' || !row._id || ids.has(row._id) ||
row.boardId !== plan.boardId || row.userId !== plan.userId ||
!/^[a-f0-9]{64}$/.test(Object.hasOwn(row, 'legacyRow') ? row.snapshotHash : row.integrityHash) || !(row.undoneAt instanceof Date) ||
!Number.isFinite(row.undoneAt.getTime())) fail();
if (Object.hasOwn(row, 'legacyRow')) {
const legacy = row.legacyRow;
if (!legacy || Array.isArray(legacy) || typeof legacy !== 'object' ||
!unhashed(legacy) || Object.hasOwn(legacy, 'superseded') || legacy.undone !== true ||
['_id', 'boardId', 'userId', 'undoneAt'].some(key => canonical(legacy[key]) !== canonical(row[key])) ||
Object.keys(legacy).some(key => key.startsWith('$') || key.includes('.')) ||
canonical(copy(legacy)) !== canonical(legacy) || sha256(canonical(legacy)) !== row.snapshotHash) fail();
}
ids.add(row._id);
}
if (Buffer.byteLength(EJSON.stringify(plan)) > 15 * 1024 * 1024) fail();
}
// Sync writes only as a writer the board's History gate admits, and links each
// planned row to the chain as it appends it (maintainer decision of
// 2026-09-30). history.admitHistoryWriter decides the mode: on a board still
// on the legacy path it holds a writer token for the whole batch, so a chain
// migration waits for it; on a coordinated board each row is appended through
// the chain head like any ordinary edit. history.appendSyncHistoryRow does the
// linking and is idempotent by row _id. There is no default for either: an
// adapter without them is refused, never silently written around the gate.
async function persistSyncFieldHistory({ history, plan, assertCurrent, fields = SYNC_FIELDS }) {
entityOf(fields);
validatePlan(plan, fields);
if (typeof assertCurrent !== 'function' || typeof history?.admitHistoryWriter !== 'function' ||
typeof history?.appendSyncHistoryRow !== 'function') fail();
// Do not let a collection adapter mutate the journal's verification inputs.
plan = copy(plan);
return history.admitHistoryWriter({ boardId: plan.boardId, work: async ({ mode, assertCurrent: writerCurrent, fencedInsert }) => {
if (!['legacy', 'coordinated'].includes(mode) || typeof writerCurrent !== 'function') fail();
return writeSyncFieldHistory({ history, plan, mode, fencedInsert,
assertCurrent: async () => { await assertCurrent(); await writerCurrent(); } });
} });
}
// The saved row is this plan's row: its hash holds, its protected content is
// the planned content (the chain link is whatever it was appended after), and
// it is not a checkpoint. Mutable undo flags may have changed since.
function isPlannedRow(saved, row) {
return !!saved && saved._id === row._id && rowHashIsValid(saved) && saved.isCheckpoint === row.isCheckpoint &&
PROTECTED.filter(key => key !== 'previousHash').every(key => canonical(saved[key]) === canonical(row[key]));
}
async function writeSyncFieldHistory({ history, plan, mode, fencedInsert, assertCurrent }) {
for (const target of plan.redo) {
await assertCurrent();
let error;
try {
const selector = target.legacyRow
? exactFieldSelector(target.legacyRow, [...new Set([...Object.keys(target.legacyRow), 'integrityHash'])])
: { ...target, undone: true };
await history.updateAsync({ ...selector, superseded: { $ne: true } }, { $set: { superseded: true } });
} catch (failure) { error = failure; }
await assertCurrent();
let saved;
try { saved = await history.findOneAsync(target._id); } catch (failure) { throw error || failure; }
const matches = target.legacyRow
? saved && canonical(legacySnapshot(saved)) === canonical(target.legacyRow)
: rowHashIsValid(saved) && Object.keys(target).every(key => canonical(saved[key]) === canonical(target[key]));
if (!saved || saved.superseded !== true || !matches) {
throw error || new Error('sync-history-redo-unconfirmed');
}
}
for (const row of plan.rows) {
await assertCurrent();
let error;
try { await history.appendSyncHistoryRow({ row: copy(row), mode, assertCurrent, fencedInsert }); }
catch (failure) { error = failure; }
await assertCurrent();
let saved;
try { saved = await history.findOneAsync(row._id); } catch (failure) { throw error || failure; }
if (!isPlannedRow(saved, row)) throw error || new Error('sync-history-event-unconfirmed');
}
await assertCurrent();
return plan.effectId;
}
function validateSyncFieldHistory(plan, step, effectId) {
validatePlan(plan);
if (plan.effectId !== effectId || plan.boardId !== step.after?.boardId) fail();
const expected = prepareSyncFieldHistory({ step, effectId, userId: plan.userId,
createdAt: plan.rows[0]?.createdAt || new Date(0) });
if (canonical(expected.rows) !== canonical(plan.rows)) fail();
return true;
}
module.exports = { createSyncHistoryPlanner, prepareSyncFieldHistory, prepareCardFieldHistory, persistSyncFieldHistory,
validateSyncFieldHistory, isPlannedRow, SYNC_FIELDS, RULE_CARD_FIELDS, RULE_CHECKLIST_ITEM_FIELDS };