From b54173de2b9992bebb727dfa6b6f133b9eb2cff9 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:45:37 +0800 Subject: [PATCH 1/2] fix(todos): retain compatible authority manifests on ordinary writes Preserve the verified declaration of an existing Todo snapshot while every record still uses fields it declares. Widen only when a mutation needs newer fields, so ordinary writes retain the earlier reader path. Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../coordination/coordination_projection.ts | 34 ++++++++---- .../coordination_projection.test.ts | 54 ++++++++++++++++++- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/loopx/control_plane/coordination/coordination_projection.ts b/loopx/control_plane/coordination/coordination_projection.ts index 07b2340b88..c38e9de725 100644 --- a/loopx/control_plane/coordination/coordination_projection.ts +++ b/loopx/control_plane/coordination/coordination_projection.ts @@ -122,6 +122,16 @@ function historicalTodoContracts( return contracts; } +function usesFieldsOutsideTodoContract( + records: readonly JsonObject[], + contract: HistoricalTodoContract, +): boolean { + for (const field of contract.absentFields) { + if (records.some((record) => field in record)) return true; + } + return false; +} + function sortedIds(values: Iterable): string[] { return [...values].sort(authorityUnicodeCompare); } @@ -181,13 +191,13 @@ export function validateCoordinationTodoReadModel( value: JsonObject, expectedGoalId: string, ): JsonObject { - return validateIndexedTodoReadModel(value, indexCoordinationProjectionTodos(value, expectedGoalId)); + return validateIndexedTodoReadModel(value, indexCoordinationProjectionTodos(value, expectedGoalId)).readModel; } function validateIndexedTodoReadModel( value: JsonObject, index: CoordinationTodoProjectionIndex, -): JsonObject { +): { readModel: JsonObject; historicalContract: HistoricalTodoContract | undefined } { const records = index.todo_ids.map((todoId) => index.todos.get(todoId)!); // Identity indexing already validates/copies every record and rejects duplicate // IDs. The insertion order of those same records proves order; serializing @@ -222,14 +232,13 @@ function validateIndexedTodoReadModel( if (!currentContract && historicalContract === undefined) { throw new AuthorityStoreProtocolError("coordination Todo read-model field contract mismatch"); } - if (historicalContract !== undefined && - records.some((record) => [...historicalContract.absentFields].some((field) => field in record))) { + if (historicalContract !== undefined && usesFieldsOutsideTodoContract(records, historicalContract)) { throw new AuthorityStoreProtocolError("coordination Todo record exceeds its historical field contract"); } for (const [recordIndex, record] of records.entries()) { canonicalTodoRecord(record, `coordination Todo read record ${recordIndex}`); } - return readModel; + return { readModel, historicalContract }; } function requireCompleteTodoReplacement( @@ -361,7 +370,7 @@ export class CoordinationProjectionRead { } validateTodoReadModel(): JsonObject { - return validateIndexedTodoReadModel(this.head, this.todoIndex); + return validateIndexedTodoReadModel(this.head, this.todoIndex).readModel; } } @@ -380,8 +389,8 @@ export function reduceCoordinationProjection( throw new AuthorityStoreProtocolError("coordination projection mutation batch is empty"); } const current = indexCoordinationProjection(value, expectedGoalId); - const readModel = value.todo_read_model === undefined - ? undefined : validateCoordinationTodoReadModel(value, expectedGoalId); + const previous = value.todo_read_model === undefined + ? undefined : validateIndexedTodoReadModel(value, current); const todos = new Map(current.todos); const leases = new Map(current.leases); const mutatedRecords = new Set(); @@ -415,13 +424,20 @@ export function reduceCoordinationProjection( } } const nextTodos = sortedIds(todos.keys()).map((todoId) => todos.get(todoId)!); + const readModel = previous && coordinationTodoReadModel(nextTodos, previous.readModel.schema_version); + const historicalContract = previous?.historicalContract; + // Ordinary writes should keep a released declaration readable by its + // original consumer. A write that uses newer fields needs the current one. + if (readModel && historicalContract && !usesFieldsOutsideTodoContract(nextTodos, historicalContract)) { + readModel.contract_fields = [...historicalContract.fields]; + } const reduced = canonicalAuthorityObject({ ...value, todos: nextTodos, leases: sortedIds(leases.keys()).map((todoId) => leases.get(todoId)!), ...(readModel === undefined ? {} - : { todo_read_model: coordinationTodoReadModel(nextTodos, readModel.schema_version) }), + : { todo_read_model: readModel }), }, "coordination projection"); if (value.todo_read_model !== undefined) { validateCoordinationTodoReadModel(reduced, expectedGoalId); diff --git a/tests/control_plane_ts/coordination_projection.test.ts b/tests/control_plane_ts/coordination_projection.test.ts index 22e533ffdf..a2cefe0f5d 100644 --- a/tests/control_plane_ts/coordination_projection.test.ts +++ b/tests/control_plane_ts/coordination_projection.test.ts @@ -417,7 +417,9 @@ for (const native of [false, true]) { const commit = prepareCoordinationProjectionCommit({goal_id: head.goal_id, operation_id: "next-mutation", expected_provider_revision: initial.provider_revision, projection: reopened.head, mutations: [{kind: "todo_upsert", todo: changed}]}); - assert.deepEqual(commit.next_projection.todo_read_model, coordinationTodoReadModel([changed], schema)); + assert.deepEqual(commit.next_projection.todo_read_model, { + ...head.todo_read_model, records_sha256: canonicalAuthoritySha256([changed]), + }, "an ordinary edit must keep the exact persisted field declaration"); assert.equal((await store.commitAuthority(commit)).status, "applied"); const updated = await listLocalCoordinationTodos(request, {createStore}); assert.equal(updated.status, "loaded"); @@ -489,6 +491,10 @@ for (const native of [false, true]) { // fixture and its registered additions, independently of today's field list. const preReceiptFields = [...previousReleaseFields]; preReceiptFields.splice(preReceiptFields.indexOf("completion_turn_key") + 1, 0, "completion_result"); +const releasedBeforeScopedMonitors = [...preReceiptFields]; +releasedBeforeScopedMonitors.splice( + releasedBeforeScopedMonitors.indexOf("completion_recovery") + 1, 0, "completion_receipt_id", +); for (const native of [false, true]) { test(`pre-receipt ${native ? "domain" : "canonical"} head remains readable and upgrades on commit`, () => { const fields = preReceiptFields.filter(field => @@ -516,6 +522,52 @@ for (const native of [false, true]) { }); } +for (const native of [false, true]) { + test(`released ${native ? "domain" : "canonical"} manifest changes only when the mutation uses a newer field`, () => { + const fields = releasedBeforeScopedMonitors.filter(field => + !native || !historicalFields.projection_metadata_fields.includes(field)); + const schema = native ? TODO_DOMAIN_READ_RECORD_SCHEMA : TODO_CANONICAL_READ_RECORD_SCHEMA; + const monitor: JsonObject = { + schema_version: native ? TODO_DOMAIN_ITEM_SCHEMA : "todo_item_v0", + todo_id: "todo_monitor", role: "agent", status: "open", done: false, + text: "Observe the assigned run", archive_state: "active", + task_class: "continuous_monitor", + ...(native ? {} : {source_section: "Agent Todo"}), + }; + const head = { + goal_id: "monitor-goal", todos: [monitor], leases: [], + todo_read_model: { + schema_version: schema, contract_fields: fields, todo_count: 1, + records_sha256: canonicalAuthoritySha256([monitor]), + }, + }; + const noteEdit = {...monitor, note: "Keep the existing watch"}; + const edited = reduceCoordinationProjection(head, head.goal_id, [{kind: "todo_upsert", todo: noteEdit}]); + assert.deepEqual(edited.todo_read_model, { + ...head.todo_read_model, records_sha256: canonicalAuthoritySha256([noteEdit]), + }); + + const observation = { + ...noteEdit, material_change_scope: "case-a/run-1", + material_change_scope_generations: {"case-a/run-1": 1}, + }; + const observed = reduceCoordinationProjection(edited, head.goal_id, [{kind: "todo_upsert", todo: observation}]); + const model = validateCoordinationTodoReadModel(observed, head.goal_id); + assert.deepEqual(observed.todos, [observation]); + assert.deepEqual(model.contract_fields, [ + ...fields.slice(0, fields.indexOf("material_change_generation") + 1), + "material_change_scope", "material_change_scope_generations", + ...fields.slice(fields.indexOf("material_change_generation") + 1), + ]); + const cleared = reduceCoordinationProjection(observed, head.goal_id, [{ + kind: "todo_upsert", todo: noteEdit, + clear_fields: ["material_change_scope", "material_change_scope_generations"], + }]); + assert.deepEqual(validateCoordinationTodoReadModel(cleared, head.goal_id).contract_fields, model.contract_fields, + "once committed, a newer field declaration does not downgrade implicitly"); + }); +} + for (const native of [false, true]) { test(`read-model order uses unique Unicode identities without weakening ${native ? "domain" : "canonical"} content validation`, () => { // U+E000 precedes U+10000 in persisted Unicode code-point order, but not From 58663a96c654a1391d23107a067c2df9182bbb11 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:17:13 +0800 Subject: [PATCH 2/2] Use the persisted monitor generation encoding in rollback coverage Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- tests/control_plane_ts/coordination_projection.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/control_plane_ts/coordination_projection.test.ts b/tests/control_plane_ts/coordination_projection.test.ts index a2cefe0f5d..8bd8fe9494 100644 --- a/tests/control_plane_ts/coordination_projection.test.ts +++ b/tests/control_plane_ts/coordination_projection.test.ts @@ -549,7 +549,8 @@ for (const native of [false, true]) { const observation = { ...noteEdit, material_change_scope: "case-a/run-1", - material_change_scope_generations: {"case-a/run-1": 1}, + // The Monitor persists a scope digest, generation, and result digest. + material_change_scope_generations: "95866d27011023fae340614c:1:cb27aee87af9e0125c0766d8", }; const observed = reduceCoordinationProjection(edited, head.goal_id, [{kind: "todo_upsert", todo: observation}]); const model = validateCoordinationTodoReadModel(observed, head.goal_id);