Files
wekan/server/lib/syncEffects.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

101 lines
6.0 KiB
JavaScript

'use strict';
const { EJSON } = require('bson');
const { validateSyncEffectPolicy, assertSyncEffectPolicy } = require('./syncEffectPolicy');
const { syncOperationEffectId, applySyncOperationStep } = require('./syncOperationApply');
const { createSyncHistoryPlanner, validateSyncFieldHistory, persistSyncFieldHistory } = require('./syncHistoryBatch');
const { prepareSyncCreationActivity, validateSyncCreationActivity, persistSyncCreationActivity } = require('./syncCreationActivity');
const { prepareSyncUpdateActivities, validateSyncUpdateActivities, persistSyncUpdateActivities } = require('./syncUpdateActivities');
const copy = value => EJSON.parse(EJSON.stringify(value), { relaxed: true });
const fail = () => { throw new Error('sync-effects-invalid'); };
// Capture all display metadata before the journal begins applying cards.
// Context changes during replay must not rewrite an already persisted event.
function createSyncEffectPlanner({ userId, username, createdAt, list, swimlanes, redoRows = [], policy }) {
policy = validateSyncEffectPolicy(policy);
const captured = copy({ userId, username, createdAt, list, swimlanes });
if (!captured.list || !Array.isArray(captured.swimlanes) || captured.swimlanes.length > 10000) fail();
const lanes = new Map();
for (const lane of captured.swimlanes) {
if (!lane || typeof lane._id !== 'string' || !lane._id || lanes.has(lane._id) ||
lane.boardId !== captured.list.boardId || typeof lane.title !== 'string') fail();
lanes.set(lane._id, lane);
}
// History rows are linked when appended, not planned (syncHistoryBatch.js).
const historyPlanner = createSyncHistoryPlanner({ userId, createdAt, redoRows });
return (step, context) => {
const effectId = syncOperationEffectId(context.operationId, context.index);
// Validate activities first: a failed reference must not advance the
// History planner and make a corrected retry of this index impossible.
const activities = policy.activities ? (step.kind === 'create' ? prepareSyncCreationActivity : prepareSyncUpdateActivities)({
...captured, step, effectId, swimlane: lanes.get(step.after.swimlaneId) }) : null;
const plan = { version: 2, policy: { ...policy }, history: historyPlanner(step, context), activities };
validateSyncEffects(plan, step, effectId);
return plan;
};
}
function validateSyncEffects(plan, step, effectId) {
if (!plan || ![1, 2].includes(plan.version) || Object.keys(plan).sort().join(',') !==
(plan.version === 1 ? 'activities,history,version' : 'activities,history,policy,version')) fail();
const policy = planPolicy(plan);
validateSyncFieldHistory(plan.history, step, effectId);
if (policy.activities) {
(step.kind === 'create' ? validateSyncCreationActivity : validateSyncUpdateActivities)(plan.activities, step, effectId);
const actor = step.kind === 'create' ? plan.activities.activity : plan.activities.context;
if (plan.history.userId !== actor.userId || plan.history.rows.some(row => row.createdAt.getTime() !== actor.createdAt.getTime())) fail();
} else if (plan.activities !== null) fail();
if (Buffer.byteLength(EJSON.stringify(plan)) > 15 * 1024 * 1024) fail();
return true;
}
function planPolicy(plan) {
// Version-one plans predate policy capture and always include activities.
return plan.version === 1 ? { activities: true, notifications: true } : validateSyncEffectPolicy(plan.policy);
}
// Internal coordinator: callers still supply durable rules/notification
// delivery and coordinate ordinary hooks. Changed flags pause the saved plan;
// replay never silently replaces its captured policy with current defaults.
function createEffectGuard({ history, activities, plan, step, effectId, assertCurrent, completeDelivery, readPolicy }) {
validateSyncEffects(plan, step, effectId);
const policy = planPolicy(plan);
if (typeof assertCurrent !== 'function' || typeof readPolicy !== 'function' ||
!['findOneAsync','insertAsync','updateAsync'].every(key => typeof history?.[key] === 'function') ||
(policy.activities && (typeof completeDelivery !== 'function' ||
!['findOneAsync','insertAsync'].every(key => typeof activities?.[key] === 'function')))) fail();
return async () => {
await assertCurrent();
await assertSyncEffectPolicy(policy, readPolicy);
await assertCurrent();
};
}
async function persistSyncEffects(options) {
const { history, activities, step, effectId, completeDelivery } = options;
const plan = copy(options.plan);
const policy = planPolicy(plan);
const guard = createEffectGuard({ ...options, plan });
await guard();
await persistSyncFieldHistory({ history, plan: plan.history, assertCurrent: guard });
if (policy.activities) {
await (step.kind === 'create' ? persistSyncCreationActivity : persistSyncUpdateActivities)({
activities, plan: plan.activities, step, effectId, assertCurrent: guard,
completeDelivery: context => completeDelivery({ ...context, policy: { ...policy } }) });
}
await guard();
return effectId;
}
// Apply one saved card unit only after validating its entire effect plan,
// persisted actor, live feature policy and all required adapters. Ordinary
// collection hooks and durable delivery remain the production caller's job.
async function applySyncEffectsStep({ cards, history, activities, step, effects, operationId, index,
userId, assertCurrent, completeDelivery, readPolicy }) {
const effectId = syncOperationEffectId(operationId, index);
const plan = copy(effects), savedStep = copy(step);
validateSyncEffects(plan, savedStep, effectId);
if (typeof userId !== 'string' || !userId || plan.history.userId !== userId) fail();
const options = { history, activities, plan, step: savedStep, effectId, assertCurrent, completeDelivery, readPolicy };
const guard = createEffectGuard(options);
return applySyncOperationStep({ cards, step: savedStep, operationId, index, assertCurrent: guard,
completeEffects: () => persistSyncEffects(options) });
}
module.exports = { createSyncEffectPlanner, validateSyncEffects, persistSyncEffects, applySyncEffectsStep };