diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index f93d1cf..47fa97b 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -191,7 +191,10 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and in `mcp.ts`: not read-only, inside a pin, the caller's own user or room lane, or a repo the room is linked to), because a session id is not bound to a credential and read visibility alone would let a reader confirm another lane's claim. Rows from before the mark never confirm, and a later unmarked `wrong` still withdraws. An unstamped survivor (an echo) never supersedes a stamped candidate until the group - promotes, so the claim the echo reply tells the agent to rate stays live. The plan + promotes, so the claim the echo reply tells the agent to rate stays live. An echo that changes the + content (whitespace aside) and is newer than the survivor is a pending update (`echoUpdate`): kept + through the survivor's promotion, matched by `created_at` because promotion bumps `updated_at`, and + promoted like any update once it recurs. A plain copy is still superseded. The plan carries the ratings it counted (`GroupPlan.confirmed`) and `apply` refuses it if any is no longer that session's newest verdict at the same time (`created_at`, then `rowid`), because a rating never touches the claim rows the version guard reads. This is the only way a session that was diff --git a/worker/src/memory.ts b/worker/src/memory.ts index f96d88f..1b74a76 100644 --- a/worker/src/memory.ts +++ b/worker/src/memory.ts @@ -148,11 +148,18 @@ export function groupKey(c: Claim): GroupKey { // - an accepted survivor keeps newer candidates of the same authority alive as pending UPDATES (an // older accepted claim must not silently eat its own correction). Once the updates recur, the // strongest update is promoted and the old answer superseded. +// An echo that changes what it echoes is a pending update to the survivor: it has no recurrence credit, but its +// change is kept, not superseded, until updates recur or a later pass retires it. +const sameText = (a: string, b: string) => a.split(WHITESPACE).filter(Boolean).join(" ") === b.split(WHITESPACE).filter(Boolean).join(" "); +const echoUpdate = (m: Claim, best: Claim) => m.status === "candidate" && !m.source_ids.length && m.authority === best.authority + && m.created_at > best.created_at && !sameText(m.content, best.content); + export function planGroup(members: Claim[], policy: Policy = DEFAULT_POLICY): GroupPlan { const best = survivor(members); const plan: GroupPlan = { key: groupKey(best), version: groupVersion(members), supersede: [], promote: [], support: [] }; + // Promotion bumps the survivor's updated_at, so an echo update kept through it is matched by when it was written. const updates = best.status === "accepted" - ? members.filter((m) => m.status === "candidate" && m.authority === best.authority && m.updated_at > best.updated_at) + ? members.filter((m) => m.status === "candidate" && m.authority === best.authority && (m.updated_at > best.updated_at || echoUpdate(m, best))) : []; if (updates.length && recurs(updates, policy.k, policy.minGapMs) && policy.autoAccept(best)) { const winner = survivor(updates); @@ -166,7 +173,7 @@ export function planGroup(members: Claim[], policy: Policy = DEFAULT_POLICY): Gr const promoting = best.status === "candidate" && recurs(members, policy.k, policy.minGapMs) && policy.autoAccept(best); // An unstamped survivor (an echo) never retires a stamped candidate: that claim is the subject's evidence, and the // one a later `used` rating names. - const keep = new Set(updates.map((u) => u.id)); + const keep = new Set([...updates, ...members.filter((m) => echoUpdate(m, best))].map((u) => u.id)); if (!promoting && best.status === "candidate" && !best.source_ids.length) { for (const m of members) if (m.status === "candidate" && m.source_ids.length) keep.add(m.id); } diff --git a/worker/test/confirm.test.ts b/worker/test/confirm.test.ts index 6bd2cd1..ca4179b 100644 --- a/worker/test/confirm.test.ts +++ b/worker/test/confirm.test.ts @@ -221,3 +221,28 @@ test("re-rating a counted used before apply moves its time, so the stale plan is await r("feedback", { ratings: [{ handle: id, ...USED }] }); // still used, but newer assert.deepEqual(await store.apply(plans), { applied: 0, stale: 1 }); }); + +test("an echo that changes the claim is kept as a pending update, and replaces it once the update is confirmed", async () => { + const db = await setup(), store = new D1Store(db); + const id = (await agent(db, "w")("claim", LESSON)).split("\n")[0]; + const r = agent(db, "r"); + await r("recall", { query: "wrangler port" }); + const copy = (await r("claim", { ...LESSON, content: "wrangler dev listens on 8787" })).split("\n")[0]; + await consolidate(store, NOW, null); + assert.equal(await status(db, copy), "superseded", "a copy of what it was shown is still dropped"); + + const update = (await r("claim", { ...LESSON, content: "wrangler dev listens on 8787; pass --port to change it" })).split("\n")[0]; + await consolidate(store, NOW, null); + assert.equal(await status(db, update), "candidate", "the change is kept"); + await r("feedback", { ratings: [{ handle: id, ...USED }] }); + await consolidate(store, NOW, null); + assert.deepEqual([await status(db, id), await status(db, update)], ["accepted", "candidate"], "the confirmed claim promotes; the change waits as its update"); + + for (const s of ["s1", "s2"]) { + const reader = agent(db, s); + await reader("recall", { query: "wrangler port" }); + await reader("feedback", { ratings: [{ handle: update, verdict: "used", note: "--port 9000 moved it" }] }); + } + await consolidate(store, NOW, null); + assert.deepEqual([await status(db, id), await status(db, update)], ["superseded", "accepted"], "two confirmations promote the update"); +});