Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions loopx/control_plane/coordination/coordination_projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): string[] {
return [...values].sort(authorityUnicodeCompare);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -361,7 +370,7 @@ export class CoordinationProjectionRead {
}

validateTodoReadModel(): JsonObject {
return validateIndexedTodoReadModel(this.head, this.todoIndex);
return validateIndexedTodoReadModel(this.head, this.todoIndex).readModel;
}
}

Expand All @@ -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<string>();
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 54 additions & 1 deletion tests/control_plane_ts/coordination_projection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -516,6 +522,53 @@ 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",
// 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);
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
Expand Down
Loading