Files
wekan/server/lib/syncRuleArchiveEffects.js
T
Lauri Ojansivu 76f6eba249 Link Sync History rows to the chain when they are written, not planned.
Sync and the stored rule archive runner fixed each History row's
previousHash and integrityHash when they PLANNED a batch, so the board's
History chain had to stay unchanged from planning until the last row was
written - which needed a multi-row History reservation that would block
or queue ordinary edits, and made a coordinated board refuse Sync.
Maintainer decision of 2026-09-30: plans are content only, and each row
is linked when it is appended, like an ordinary edit.

On a board still on the legacy path, the batch holds one writer token
(so a chain migration drains it) and each row is linked after the
chain's tip - the newest hashed row, then its successors, since the rows
of one batch share a planned createdAt and "newest" alone is a tie that
forks the chain. On a coordinated board each row goes through the chain
head. Appends are idempotent by row _id, and a replay accepts only the
planned content. The archive runner needs no reservation any more; it
captures the actor's redo candidates when it plans. Plans in the old
chained format are refused. An index on (boardId, previousHash) serves
the tip walk.

Unit and MongoDB tests: plans carry no link; rows chain correctly in
both modes and replay once; ordinary History written between planning
and writing is neither blocked nor forked; a coordinated board appends
through the head; old-format plans and adapters without admission or
linking are refused. The full-app archive case now runs without a
reservation and verifies the resulting chain. 561 server tests pass;
the one other failure in that run, the activity notification recovery
scan, is intermittent and passes on its own.

Thanks to xet7 !
2026-09-30 10:35:57 +03:00

116 lines
6.6 KiB
JavaScript

'use strict';
const { EJSON, calculateObjectSize } = require('bson');
const { canonical, sha256 } = require('../../models/lib/changeHistoryIntegrity');
const { validateRuleArchiveCommand } = require('./syncRuleArchiveCommand');
const { archiveUnits, applyRuleArchiveCommand } = require('./syncRuleArchiveApply');
const { prepareSyncFieldHistory } = require('./syncHistoryBatch');
const { prepareSyncUpdateActivities } = require('./syncUpdateActivities');
const { validateSyncEffects, persistSyncEffects } = require('./syncEffects');
const { validateSyncEffectPolicy, assertSyncEffectPolicy } = require('./syncEffectPolicy');
const copy = value => EJSON.parse(EJSON.stringify(value), { relaxed: true });
const fail = () => { throw new Error('sync-rule-archive-effects-invalid'); };
function stepFor(unit) {
// parentId is part of the mutation predicate, not a changed History field.
const clean = card => { const result = copy(card); delete result.parentId; return result; };
return { kind: 'archive', cardId: unit.cardId, before: clean(unit.before), after: clean(unit.after) };
}
function validateRuleArchiveEffects(saved, command, context) {
command = validateRuleArchiveCommand(command, context);
const units = archiveUnits(command, context);
// Version 2: History rows are content, linked to the chain when appended
// (maintainer decision of 2026-09-30), so the saved effects carry no chain
// link and need no History reservation between planning and writing.
if (!saved || Object.keys(saved).sort().join(',') !== '_id,checksum,commandHash,rows,version' ||
saved._id !== command._id || saved.version !== 2 || saved.commandHash !== command.checksum ||
!Array.isArray(saved.rows) || saved.rows.length !== units.length || calculateObjectSize(saved) > 14 * 1024 * 1024) fail();
let changed = false;
for (let i = 0; i < units.length; i++) {
const effects = saved.rows[i], unit = units[i];
validateSyncEffects(effects, stepFor(unit), unit.effectId);
if (effects.version !== 2 || effects.history.userId !== command.actorId ||
(i && canonical(effects.policy) !== canonical(saved.rows[0].policy))) fail();
const history = effects.history;
if (history.rows.length) {
// The redo stack is cleared once, by the first card that records History.
if (changed && history.redo.length) fail();
changed = true;
}
if (history.rows.some(row => row.createdAt.getTime() !== command.createdAt.getTime()) ||
(effects.activities && effects.activities.context.createdAt.getTime() !== command.createdAt.getTime())) fail();
}
const { checksum, ...payload } = saved;
if (checksum !== sha256(canonical(payload))) fail();
return copy(saved);
}
function prepareRuleArchiveEffects({ command, plan, activity, effectId, index, username, lists,
redoRows = [], policy }) {
const context = { plan, activity, effectId, index };
command = validateRuleArchiveCommand(command, context);
policy = validateSyncEffectPolicy(policy);
if (typeof username !== 'string' || !Array.isArray(lists) || lists.length > 1000) fail();
const byId = new Map();
for (const list of lists) {
if (!list || typeof list._id !== 'string' || !list._id || list.boardId !== command.boardId ||
typeof list.title !== 'string' || byId.has(list._id)) fail();
byId.set(list._id, list);
}
const saved = { _id: command._id, version: 2, commandHash: command.checksum, rows: [] };
let redo = redoRows, bytes = 0;
for (const unit of archiveUnits(command, context)) {
const step = stepFor(unit);
const options = { step, effectId: unit.effectId, userId: command.actorId, createdAt: command.createdAt };
const history = prepareSyncFieldHistory({ ...options, redoRows: redo });
const activities = policy.activities ? prepareSyncUpdateActivities({ ...options, username,
list: byId.get(unit.after.listId) }) : null;
const row = { version: 2, policy: { ...policy }, history, activities };
bytes += calculateObjectSize(row) + 32;
if (bytes > 14 * 1024 * 1024) fail();
saved.rows.push(row);
if (history.rows.length) redo = [];
}
saved.checksum = sha256(canonical(saved));
return validateRuleArchiveEffects(saved, command, context);
}
async function ensureRuleArchiveEffects({ effects, command, build, assertCurrent, ...context }) {
command = validateRuleArchiveCommand(command, context);
if (typeof build !== 'function' || typeof assertCurrent !== 'function' ||
!['findOne', 'insertOne'].every(key => typeof effects?.[key] === 'function')) fail();
const read = async () => {
await assertCurrent(); const row = await effects.findOne({ _id: command._id }); await assertCurrent();
return row ? validateRuleArchiveEffects(row, command, context) : null;
};
let saved = await read();
if (!saved) {
const candidate = validateRuleArchiveEffects(await build(copy(command)), command, context);
await assertCurrent(); let failure;
try { await effects.insertOne(copy(candidate)); } catch (error) { failure = error; }
saved = await read();
if (!saved) throw failure || new Error('sync-rule-archive-effects-unconfirmed');
}
return saved;
}
async function applyRuleArchiveEffects(options) {
const { command, history, activities, completeDelivery, readPolicy, assertCurrent } = options;
const saved = validateRuleArchiveEffects(options.effects, command, options);
if (typeof assertCurrent !== 'function' || typeof readPolicy !== 'function' ||
!['findOneAsync', 'insertAsync', 'updateAsync'].every(key => typeof history?.[key] === 'function')) fail();
const units = archiveUnits(command, options);
const guard = async () => {
await assertCurrent();
if (saved.rows.length) await assertSyncEffectPolicy(saved.rows[0].policy, readPolicy);
await assertCurrent();
};
const rows = new Map(units.map((unit, i) => [unit.effectId, { unit, effects: saved.rows[i] }]));
if (saved.rows.some(row => row.policy.activities) && (typeof completeDelivery !== 'function' ||
!['findOneAsync', 'insertAsync'].every(key => typeof activities?.[key] === 'function'))) fail();
return applyRuleArchiveCommand({ ...options, assertCurrent: guard,
preflightEffects: async () => { await guard(); },
completeEffects: async ({ unit, assertCurrent: checkCard }) => {
const row = rows.get(unit.effectId);
if (!row || canonical(unit) !== canonical(row.unit)) fail();
return persistSyncEffects({ history, activities, plan: row.effects, step: stepFor(unit),
effectId: unit.effectId, assertCurrent: checkCard, completeDelivery, readPolicy });
} });
}
module.exports = { archiveEffectStep: stepFor, prepareRuleArchiveEffects, validateRuleArchiveEffects, ensureRuleArchiveEffects, applyRuleArchiveEffects };