From 60e9fb1eb1f5a6bce48b332b582a4ed53a6df86a Mon Sep 17 00:00:00 2001 From: vu1n <2105991+vu1n@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:22:45 +0000 Subject: [PATCH 1/4] worker: a claim rated wrong leaves the briefing until a later used clears it --- docs/features/hosted-worker.md | 8 +++ worker/src/mcp.ts | 40 +++++++++++---- worker/src/memory.ts | 2 + worker/src/store.ts | 27 ++++++++++ worker/test/dispute.test.ts | 91 ++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 worker/test/dispute.test.ts diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index 6b0f1ba..afa00d9 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -16,6 +16,7 @@ tests: - "worker/test/readonly.test.ts" - "worker/test/feedback.test.ts" - "worker/test/confirm.test.ts" + - "worker/test/dispute.test.ts" - "tests/test_usefulness.py" --- # Hosted Worker @@ -212,6 +213,13 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and claim rows the version guard reads. This is the only way a session that was shown a candidate can help accept it: its re-claim is an echo, and the claim reply says so. The Python arbiter doesn't count ratings, so local and hosted promote differently here. +- A `wrong` rating disputes its claim (`store.disputes`): while some session's newest writable verdict + is `wrong` and no newest writable `used` came after it, every briefing section drops the claim and + recall marks it `⚠ rated wrong`; `expand` returns the note as `rated_wrong`. It is read-side only and + keyed on the claim id, so a new version under the subject starts clean and no status changes. + Verdict order is the row order (`created_at`, then `rowid`), not the timestamp alone. Only writable + ratings count, for the same reason only they confirm. A new read path that renders claims into a + briefing must call `markDisputed` and filter. The local store has no feedback, so local briefings don't drop them. - `initialize` instructions are built from `STEPS` in `mcp.ts`, keeping only the steps whose tool the caller can see (`visibleTools`), so a read-only or `KYPP_CORRECT=off` caller is never told to call a tool it lacks. diff --git a/worker/src/mcp.ts b/worker/src/mcp.ts index 012a723..987bee0 100644 --- a/worker/src/mcp.ts +++ b/worker/src/mcp.ts @@ -5,7 +5,7 @@ import { type Claim, DEFAULT_POLICY, HUMAN_CORRECTION_CONFIDENCE, type Policy, T import { consolidate } from "./consolidate.ts"; import { fileClaim } from "./defrag.ts"; import { CONTROL_ABOVE, DROP_BELOW, type ProjectInfo, RERANK_POOL, type S1Client, judge, rankForQuery } from "./s1.ts"; -import { type Lane, type Renamed, type RoomScope, D1Store } from "./store.ts"; +import { type Dispute, type Lane, type Renamed, type RoomScope, D1Store } from "./store.ts"; import { roomIdentity } from "./rooms.ts"; export interface Caller { @@ -87,7 +87,7 @@ const TOOLS = [ }, { name: "feedback", - description: "Rate the claims this session was shown (by briefing, recall or expand), once, before you finish: `used` you acted on it and it held (say what you checked in `note`), `not_used` it was shown but didn't bear on the task, `wrong` it misled you (say how in `note`). Changes no claim; the ratings tune what memory shows. Rate again to change a verdict.", + description: "Rate the claims this session was shown (by briefing, recall or expand), once, before you finish: `used` you acted on it and it held (say what you checked in `note`), `not_used` it was shown but didn't bear on the task, `wrong` it misled you (say how in `note`). Changes no claim, but a `wrong` keeps it out of every briefing until another session rates it `used` or a new version replaces it. Rate again to change a verdict.", inputSchema: { type: "object", required: ["ratings"], properties: { @@ -167,9 +167,23 @@ const ROOM_TOOLS = [ type Args = Record; type Rpc = { jsonrpc: "2.0"; id?: string | number | null; method: string; params?: any }; -function claimDict(c: Claim) { +function claimDict(c: Claim, dispute?: Dispute) { const { id, type, subject, content, scope, project, status, authority, confidence, source_ids, code_refs, agent, user, created_at } = c; - return { id, type, subject, content, scope, project, status, authority, confidence, source_ids, code_refs, agent, user, created_at }; + return { id, type, subject, content, scope, project, status, authority, confidence, source_ids, code_refs, agent, user, created_at, + ...(dispute ? { rated_wrong: dispute } : {}) }; +} + +// A claim a session rated `wrong` stays out of the briefing, which every session loads, until a later +// `used` from a session clears it or a new version under its subject replaces it. Recall still shows it, marked. +async function markDisputed(store: D1Store, claims: Claim[]): Promise { + const d = await store.disputes(claims.map((c) => c.id)); + for (const c of claims) if (d.has(c.id)) c.disputed = true; +} +const undisputed = (cs: Claim[]) => cs.filter((c) => !c.disputed); + +async function expanded(store: D1Store, c: Claim): Promise { + const d = (await store.disputes([c.id])).get(c.id); + return JSON.stringify(claimDict(c, d), null, 2); } // Search pulls a wide pool and System One reranks it; ranking never changes what a caller may see. @@ -263,11 +277,13 @@ async function roomTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, key const limit = Math.min(a.limit ?? 12, 30); // Why a read of its own, as in a repo briefing: recall ranks the org last, so a busy room's // capped list would crowd its rules out. - const [settled, { orgs, org }, yours] = await Promise.all([ + const [all, { orgs, org: allOrg }, allYours] = await Promise.all([ store.recall("", null, null, { room, candidates: "none", limit: limit * 3 }), orgRules(store, groups), store.recentUserCandidates(identity, ctx.session, RECENT_USER, "room"), ]); + await markDisputed(store, [...all, ...allOrg, ...allYours]); + const [settled, org, yours] = [undisputed(all), undisputed(allOrg), undisputed(allYours)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); await store.recordUsage(ctx.session, [["org", org], ["settled", claims], ["yours", yours]], "briefing", null); @@ -278,7 +294,7 @@ async function roomTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, key const query = String(a.query ?? ""), limit = recallLimit(a.limit); const claims = await rankForQuery(ctx.s1, query, await store.recall(query, null, null, { room, candidates: a.include_candidates ? "all" : "origin", types: a.types, limit: poolSize(ctx.s1, query, limit), session: ctx.session }), limit); - await store.recordUsage(ctx.session, [["results", claims]], "recall", null, a.query || null); + await Promise.all([markDisputed(store, claims), store.recordUsage(ctx.session, [["results", claims]], "recall", null, a.query || null)]); return renderClaims(claims); } case "claim": { @@ -312,7 +328,7 @@ async function roomTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, key const c = await store.get(String(a.handle ?? ""), identity, ctx.session, room); if (!c) throw new Error(`unknown claim handle ${a.handle}`); await store.recordUsage(ctx.session, [["expand", [c]]], "expand", c.project); - return JSON.stringify(claimDict(c), null, 2); + return expanded(store, c); } } throw new Error(`unknown tool ${name}`); @@ -398,7 +414,7 @@ async function pinnedTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, p const visible = c && (c.scope === "project" ? c.project === null || names.includes(c.project) : c.scope === "group" ? groups.has(c.project ?? "") : true); if (!c || !visible) throw new Error(`unknown claim handle ${a.handle}`); await store.recordUsage(ctx.session, [["expand", [c]]], "expand", c.project); - return JSON.stringify(claimDict(c), null, 2); + return expanded(store, c); } // Why: an unsorted claim from a pinned token could be filed anywhere, so it must name its project. if (!project && name === "claim" && a.scope !== "user") throw new Error(`say which project this lesson is about: ${mayUse}`); @@ -416,7 +432,7 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, case "briefing": { const limit = Math.min(a.limit ?? 12, 30); // An org's rules apply to every contributor, so they come first under their own heading. - const [settled, { orgs, org }, recent, yours] = await Promise.all([ + const [all, { orgs, org: allOrg }, allRecent, allYours] = await Promise.all([ store.recall("", project, user, { candidates: "none", groups, limit: limit * 3 }), orgRules(store, groups), // Why: the briefing stays settled memory; others' unconfirmed claims get a short, labelled section. @@ -424,6 +440,8 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, // User scope is how this person works, read from every repo; unconfirmed lessons stay labelled and apart. user ? store.recentUserCandidates(user, ctx.session, RECENT_USER) : [], ]); + await markDisputed(store, [...all, ...allOrg, ...allRecent, ...allYours]); + const [settled, org, recent, yours] = [undisputed(all), undisputed(allOrg), undisputed(allRecent), undisputed(allYours)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); // In the order the reply renders them: yours comes before recent. @@ -436,7 +454,7 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, const query = String(a.query ?? ""), limit = recallLimit(a.limit); const claims = await rankForQuery(ctx.s1, query, await store.recall(query, project, user, { candidates: a.include_candidates ? "all" : "origin", groups, types: a.types, agent: a.agent, limit: poolSize(ctx.s1, query, limit), session: ctx.session }), limit); - await store.recordUsage(ctx.session, [["results", claims]], "recall", project, a.query || null); + await Promise.all([markDisputed(store, claims), store.recordUsage(ctx.session, [["results", claims]], "recall", project, a.query || null)]); return note + renderClaims(claims); } case "claim": { @@ -484,7 +502,7 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, const c = await store.get(String(a.handle ?? ""), user, ctx.session); if (!c) throw new Error(`unknown claim handle ${a.handle}`); await store.recordUsage(ctx.session, [["expand", [c]]], "expand", c.project); - return JSON.stringify(claimDict(c), null, 2); + return expanded(store, c); } case "correct": { if (!ctx.correct) throw new Error("correct is turned off on this server; use claim instead"); diff --git a/worker/src/memory.ts b/worker/src/memory.ts index 1b74a76..ad6bf20 100644 --- a/worker/src/memory.ts +++ b/worker/src/memory.ts @@ -62,6 +62,7 @@ export interface Claim { verify?: string | null; // the operator's freshness command; set only through POST /v1/operator pointer?: boolean; // set by recall when a path, anchor or doc:// ref in the query matched the claim, not its words confirmations?: Confirmation[]; // set by consolidate: other sessions whose newest feedback on this claim is `used` + disputed?: boolean; // set by reads: a session rated it `wrong` and no later `used` cleared it } // A session that was shown the claim, acted on it, and rated it `used` with a note on what it checked. @@ -213,6 +214,7 @@ export function compactLine(c: Claim): string { if (body.length > CLIP) line += `… (expand ${c.id.slice(0, 8)} for full)`; const path = c.code_refs.find((r) => r && typeof r === "object" && typeof r.path === "string")?.path; if (path) line += ` → ${path}`; + if (c.disputed) line += ` ⚠ rated wrong (expand ${c.id.slice(0, 8)} for why)`; return line; } diff --git a/worker/src/store.ts b/worker/src/store.ts index 55dde99..bbda685 100644 --- a/worker/src/store.ts +++ b/worker/src/store.ts @@ -115,6 +115,8 @@ const CONFIRMING = "CASE WHEN json_extract(u.metadata, '$.writable') = 1 THEN u. /** A claim's level, keyed the way its group is: a project, a person's own scope, or a room's lane. */ export type Lane = { project: string } | { user: string } | { room: string }; /** An earlier claim of this session's with the same content under another subject, and its provenance. */ +/** A standing `wrong` rating on a claim: the rater's note and when it was made. */ +export interface Dispute { note: string | null; at: string } export interface Renamed { id: string; subject: string; stamped: boolean; echoOf: string[] } const laneSql = (at: Lane): [string, string] => "project" in at ? ["c.scope = 'project' AND c.project = ?", at.project] : "room" in at ? ["c.scope = 'room' AND c.user = ?", at.room] : ["c.scope = 'user' AND c.user = ?", at.user]; @@ -565,6 +567,31 @@ export class D1Store { return out; } + // Per claim, the newest standing `wrong` and its note. A claim is disputed while some session's newest + // writable verdict on it is `wrong` and no session's newest writable verdict is a `used` made after it. + async disputes(ids: string[]): Promise> { + const out = new Map(); + if (!ids.length) return out; + const { results } = await this.db.prepare( + `SELECT u.claim_id, u.consumer, ${CONFIRMING} AS score, json_extract(u.metadata, '$.note') AS note, u.created_at` + + " FROM claim_usages u JOIN json_each(?) j ON j.value = u.claim_id WHERE u.surface = 'feedback' ORDER BY u.created_at, u.rowid", + ).bind(JSON.stringify([...new Set(ids)])).all(); + // Rows arrive in rating order (created_at, then rowid), so the index orders verdicts made in the same millisecond. + const latest = new Map, number]>(); + results.forEach((r, i) => latest.set(`${r.claim_id}\u0000${r.consumer}`, [r, i])); + const used = new Map(), wrong = new Map(); + for (const [r, i] of latest.values()) { + const id = r.claim_id as string; + if (r.score === 1 && i > (used.get(id) ?? -1)) used.set(id, i); + if (r.score === -1 && i > (wrong.get(id) ?? -1)) { + wrong.set(id, i); + out.set(id, { note: (r.note as string | null) ?? null, at: r.created_at as string }); + } + } + for (const id of out.keys()) if ((used.get(id) ?? -1) > wrong.get(id)!) out.delete(id); + return out; + } + // Every group or org `project` rolls up to, nearest first, through parents of parents up to three // levels (a cycle in the table just stops). The roll-up table is the only thing that decides what a // project can see. diff --git a/worker/test/dispute.test.ts b/worker/test/dispute.test.ts new file mode 100644 index 0000000..cfac0a8 --- /dev/null +++ b/worker/test/dispute.test.ts @@ -0,0 +1,91 @@ +// A claim a writable session rated `wrong` leaves the briefing until a later `used` clears it or a new +// version replaces it; recall still shows it, marked, and expand shows the note. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { type Caller, handleMcp } from "../src/mcp.ts"; +import { memoryD1 } from "./d1.ts"; + +const OWNER: Caller = { user: "owner", agent: "claude" }; + +async function setup() { + const db = memoryD1(); + await db.prepare("INSERT INTO projects(name, description, created_at) VALUES('kypp', '', '2026-11-06T00:00:00Z')").run(); + return db; +} + +function agent(db: D1Database, session: string, caller: Caller = OWNER) { + return async (name: string, args: Record = {}) => { + const req = new Request("https://k/mcp", { method: "POST", headers: { "X-Kypp-Project": "kypp", "Mcp-Session-Id": session }, + body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }) }); + const { result } = await (await handleMcp(req, db, caller, { s1: null })).json() as any; + return result.content[0].text as string; + }; +} + +// An accepted claim, as a human correction lands it. +async function accepted(db: D1Database, subject: string, content: string) { + return (await agent(db, "w")("correct", { subject, content, type: "pitfall" })).split("\n")[0].trim(); +} + +const WRONG = { verdict: "wrong", note: "the port moved to 8788" }; +const USED = { verdict: "used", note: "ran wrangler dev; it answered on 8787" }; + +test("a wrong rating takes the claim out of the briefing; recall marks it and expand says why", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const r = agent(db, "r"); + assert.match(await r("briefing"), /wrangler port/); + await r("feedback", { ratings: [{ handle: id, ...WRONG }] }); + + const next = agent(db, "n"); + assert.doesNotMatch(await next("briefing"), /wrangler port/); + assert.match(await next("recall", { query: "wrangler port" }), new RegExp(`wrangler port .*⚠ rated wrong \\(expand ${id.slice(0, 8)} for why\\)`)); + const full = JSON.parse(await next("expand", { handle: id })); + assert.equal(full.rated_wrong.note, "the port moved to 8788"); + assert.equal(full.status, "accepted", "a rating changes no claim"); +}); + +test("a later used from another session clears the dispute", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const a = agent(db, "a"), b = agent(db, "b"); + await a("briefing"); await b("briefing"); + await a("feedback", { ratings: [{ handle: id, ...WRONG }] }); + assert.doesNotMatch(await agent(db, "x")("briefing"), /wrangler port/); + await b("feedback", { ratings: [{ handle: id, ...USED }] }); + assert.match(await agent(db, "y")("briefing"), /wrangler port/); + assert.doesNotMatch(await agent(db, "y")("recall", { query: "wrangler port" }), /rated wrong/); +}); + +test("a used made before the wrong doesn't clear it, and the rater withdrawing does", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const a = agent(db, "a"), b = agent(db, "b"); + await a("briefing"); await b("briefing"); + await b("feedback", { ratings: [{ handle: id, ...USED }] }); + await a("feedback", { ratings: [{ handle: id, ...WRONG }] }); + assert.doesNotMatch(await agent(db, "x")("briefing"), /wrangler port/); + await a("feedback", { ratings: [{ handle: id, verdict: "not_used" }] }); + assert.match(await agent(db, "y")("briefing"), /wrangler port/); +}); + +test("a wrong from a caller that can't write at the claim's level hides nothing", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const ro = agent(db, "ro", { ...OWNER, readOnly: true }); + await ro("briefing"); + await ro("feedback", { ratings: [{ handle: id, ...WRONG }] }); + assert.match(await agent(db, "x")("briefing"), /wrangler port/); +}); + +test("a new version under the subject is not disputed by the old one's rating", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const a = agent(db, "a"); + await a("briefing"); + await a("feedback", { ratings: [{ handle: id, ...WRONG }] }); + await accepted(db, "wrangler port", "wrangler dev listens on 8788"); + const brief = await agent(db, "x")("briefing"); + assert.match(brief, /wrangler port — wrangler dev listens on 8788/); + assert.doesNotMatch(brief, /rated wrong/); +}); From 2fbcaf988f6a644b2fbdc37e5dd107ffcb32734f Mon Sep 17 00:00:00 2001 From: vu1n <2105991+vu1n@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:28:14 +0000 Subject: [PATCH 2/4] worker: dispute ignores unmarked ratings and never shows the rater's note --- docs/features/hosted-worker.md | 8 +++++--- worker/src/memory.ts | 2 +- worker/src/store.ts | 14 ++++++++------ worker/test/dispute.test.ts | 23 +++++++++++++++++------ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index afa00d9..be319b2 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -215,10 +215,12 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and Python arbiter doesn't count ratings, so local and hosted promote differently here. - A `wrong` rating disputes its claim (`store.disputes`): while some session's newest writable verdict is `wrong` and no newest writable `used` came after it, every briefing section drops the claim and - recall marks it `⚠ rated wrong`; `expand` returns the note as `rated_wrong`. It is read-side only and + recall marks it `⚠ a session rated this wrong`; `expand` returns `rated_wrong: {at}`. The note is never + shown to agents: it skips the write gate, so it would be an unscreened channel. It is read-side only and keyed on the claim id, so a new version under the subject starts clean and no status changes. - Verdict order is the row order (`created_at`, then `rowid`), not the timestamp alone. Only writable - ratings count, for the same reason only they confirm. A new read path that renders claims into a + Verdict order is the row order (`created_at`, then `rowid`), not the timestamp alone. Unmarked + (non-writable) rows are dropped before each session's newest is picked, so a session id reused with a + read-only token can't lift its own dispute. A new read path that renders claims into a briefing must call `markDisputed` and filter. The local store has no feedback, so local briefings don't drop them. - `initialize` instructions are built from `STEPS` in `mcp.ts`, keeping only the steps whose tool the caller can see (`visibleTools`), so a read-only or `KYPP_CORRECT=off` caller is never told to call diff --git a/worker/src/memory.ts b/worker/src/memory.ts index ad6bf20..ab36bb6 100644 --- a/worker/src/memory.ts +++ b/worker/src/memory.ts @@ -214,7 +214,7 @@ export function compactLine(c: Claim): string { if (body.length > CLIP) line += `… (expand ${c.id.slice(0, 8)} for full)`; const path = c.code_refs.find((r) => r && typeof r === "object" && typeof r.path === "string")?.path; if (path) line += ` → ${path}`; - if (c.disputed) line += ` ⚠ rated wrong (expand ${c.id.slice(0, 8)} for why)`; + if (c.disputed) line += " ⚠ a session rated this wrong; check it before relying on it"; return line; } diff --git a/worker/src/store.ts b/worker/src/store.ts index bbda685..7124e2a 100644 --- a/worker/src/store.ts +++ b/worker/src/store.ts @@ -115,8 +115,8 @@ const CONFIRMING = "CASE WHEN json_extract(u.metadata, '$.writable') = 1 THEN u. /** A claim's level, keyed the way its group is: a project, a person's own scope, or a room's lane. */ export type Lane = { project: string } | { user: string } | { room: string }; /** An earlier claim of this session's with the same content under another subject, and its provenance. */ -/** A standing `wrong` rating on a claim: the rater's note and when it was made. */ -export interface Dispute { note: string | null; at: string } +/** A standing `wrong` rating on a claim: when it was made. */ +export interface Dispute { at: string } export interface Renamed { id: string; subject: string; stamped: boolean; echoOf: string[] } const laneSql = (at: Lane): [string, string] => "project" in at ? ["c.scope = 'project' AND c.project = ?", at.project] : "room" in at ? ["c.scope = 'room' AND c.user = ?", at.room] : ["c.scope = 'user' AND c.user = ?", at.user]; @@ -567,14 +567,16 @@ export class D1Store { return out; } - // Per claim, the newest standing `wrong` and its note. A claim is disputed while some session's newest + // Per claim, when its standing `wrong` was made. A claim is disputed while some session's newest // writable verdict on it is `wrong` and no session's newest writable verdict is a `used` made after it. + // Unmarked rows are dropped before picking each session's newest, so a later read-only rating can't + // lift a dispute. The note is never returned: it skipped the write gate, so other agents don't read it. async disputes(ids: string[]): Promise> { const out = new Map(); if (!ids.length) return out; const { results } = await this.db.prepare( - `SELECT u.claim_id, u.consumer, ${CONFIRMING} AS score, json_extract(u.metadata, '$.note') AS note, u.created_at` - + " FROM claim_usages u JOIN json_each(?) j ON j.value = u.claim_id WHERE u.surface = 'feedback' ORDER BY u.created_at, u.rowid", + "SELECT u.claim_id, u.consumer, u.score, u.created_at FROM claim_usages u JOIN json_each(?) j ON j.value = u.claim_id" + + " WHERE u.surface = 'feedback' AND json_extract(u.metadata, '$.writable') = 1 ORDER BY u.created_at, u.rowid", ).bind(JSON.stringify([...new Set(ids)])).all(); // Rows arrive in rating order (created_at, then rowid), so the index orders verdicts made in the same millisecond. const latest = new Map, number]>(); @@ -585,7 +587,7 @@ export class D1Store { if (r.score === 1 && i > (used.get(id) ?? -1)) used.set(id, i); if (r.score === -1 && i > (wrong.get(id) ?? -1)) { wrong.set(id, i); - out.set(id, { note: (r.note as string | null) ?? null, at: r.created_at as string }); + out.set(id, { at: r.created_at as string }); } } for (const id of out.keys()) if ((used.get(id) ?? -1) > wrong.get(id)!) out.delete(id); diff --git a/worker/test/dispute.test.ts b/worker/test/dispute.test.ts index cfac0a8..f5469df 100644 --- a/worker/test/dispute.test.ts +++ b/worker/test/dispute.test.ts @@ -1,5 +1,5 @@ // A claim a writable session rated `wrong` leaves the briefing until a later `used` clears it or a new -// version replaces it; recall still shows it, marked, and expand shows the note. +// version replaces it; recall still shows it, marked. The rater's note is never shown to other agents. import assert from "node:assert/strict"; import { test } from "node:test"; import { type Caller, handleMcp } from "../src/mcp.ts"; @@ -30,7 +30,7 @@ async function accepted(db: D1Database, subject: string, content: string) { const WRONG = { verdict: "wrong", note: "the port moved to 8788" }; const USED = { verdict: "used", note: "ran wrangler dev; it answered on 8787" }; -test("a wrong rating takes the claim out of the briefing; recall marks it and expand says why", async () => { +test("a wrong rating takes the claim out of the briefing; recall and expand mark it without the note", async () => { const db = await setup(); const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); const r = agent(db, "r"); @@ -39,9 +39,10 @@ test("a wrong rating takes the claim out of the briefing; recall marks it and ex const next = agent(db, "n"); assert.doesNotMatch(await next("briefing"), /wrangler port/); - assert.match(await next("recall", { query: "wrangler port" }), new RegExp(`wrangler port .*⚠ rated wrong \\(expand ${id.slice(0, 8)} for why\\)`)); + assert.match(await next("recall", { query: "wrangler port" }), /wrangler port .*⚠ a session rated this wrong; check it before relying on it$/); const full = JSON.parse(await next("expand", { handle: id })); - assert.equal(full.rated_wrong.note, "the port moved to 8788"); + assert.ok(full.rated_wrong.at); + assert.doesNotMatch(JSON.stringify(full), /8788/, "the note skipped the write gate, so no agent reads it"); assert.equal(full.status, "accepted", "a rating changes no claim"); }); @@ -54,7 +55,7 @@ test("a later used from another session clears the dispute", async () => { assert.doesNotMatch(await agent(db, "x")("briefing"), /wrangler port/); await b("feedback", { ratings: [{ handle: id, ...USED }] }); assert.match(await agent(db, "y")("briefing"), /wrangler port/); - assert.doesNotMatch(await agent(db, "y")("recall", { query: "wrangler port" }), /rated wrong/); + assert.doesNotMatch(await agent(db, "y")("recall", { query: "wrangler port" }), /rated this wrong/); }); test("a used made before the wrong doesn't clear it, and the rater withdrawing does", async () => { @@ -87,5 +88,15 @@ test("a new version under the subject is not disputed by the old one's rating", await accepted(db, "wrangler port", "wrangler dev listens on 8788"); const brief = await agent(db, "x")("briefing"); assert.match(brief, /wrangler port — wrangler dev listens on 8788/); - assert.doesNotMatch(brief, /rated wrong/); + assert.doesNotMatch(brief, /rated this wrong/); +}); + +test("a later rating from the same session without write access doesn't lift its dispute", async () => { + const db = await setup(); + const id = await accepted(db, "wrangler port", "wrangler dev listens on 8787"); + const a = agent(db, "a"), aRo = agent(db, "a", { ...OWNER, readOnly: true }); + await a("briefing"); + await a("feedback", { ratings: [{ handle: id, ...WRONG }] }); + await aRo("feedback", { ratings: [{ handle: id, ...USED }] }); + assert.doesNotMatch(await agent(db, "x")("briefing"), /wrangler port/); }); From 4f8c5495cee50bd7fc36552866272a9d37203219 Mon Sep 17 00:00:00 2001 From: vu1n <2105991+vu1n@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:33:42 +0000 Subject: [PATCH 3/4] worker: briefing sections fill past disputed claims instead of losing their slots --- docs/features/hosted-worker.md | 3 ++- worker/src/mcp.ts | 32 ++++++++++++++++++++++---------- worker/test/dispute.test.ts | 13 +++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index be319b2..36c8b21 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -221,7 +221,8 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and Verdict order is the row order (`created_at`, then `rowid`), not the timestamp alone. Unmarked (non-writable) rows are dropped before each session's newest is picked, so a session id reused with a read-only token can't lift its own dispute. A new read path that renders claims into a - briefing must call `markDisputed` and filter. The local store has no feedback, so local briefings don't drop them. + briefing must fetch through `filled`, which marks disputes and widens the page while too few + undisputed claims came back, so disputed claims can't use up a section's limit. The local store has no feedback, so local briefings don't drop them. - `initialize` instructions are built from `STEPS` in `mcp.ts`, keeping only the steps whose tool the caller can see (`visibleTools`), so a read-only or `KYPP_CORRECT=off` caller is never told to call a tool it lacks. diff --git a/worker/src/mcp.ts b/worker/src/mcp.ts index 987bee0..cebbcf6 100644 --- a/worker/src/mcp.ts +++ b/worker/src/mcp.ts @@ -181,6 +181,19 @@ async function markDisputed(store: D1Store, claims: Claim[]): Promise { } const undisputed = (cs: Claim[]) => cs.filter((c) => !c.disputed); +// Fetches `want` claims, marks the disputed ones, and fetches a wider page while too few undisputed +// ones came back, so disputed claims can't use up a section's query limit. Returns every fetched claim. +const FILL_ROUNDS = 4; +async function filled(store: D1Store, fetch: (n: number) => Promise, want: number): Promise { + let n = want, got: Claim[] = []; + for (let round = 0; round < FILL_ROUNDS; round++, n *= 4) { + got = await fetch(n); + await markDisputed(store, got); + if (got.length < n || undisputed(got).length >= want) break; + } + return got; +} + async function expanded(store: D1Store, c: Claim): Promise { const d = (await store.disputes([c.id])).get(c.id); return JSON.stringify(claimDict(c, d), null, 2); @@ -235,7 +248,7 @@ const NEAR_SUBJECTS = 3; async function orgRules(store: D1Store, groups: string[]): Promise<{ orgs: Set; org: Claim[] }> { const orgs = await store.orgs(groups); if (!orgs.size) return { orgs, org: [] }; - const org = await store.recall("", null, null, { candidates: "none", groups: groups.filter((g) => orgs.has(g)), limit: ORG_RULES }); + const org = await filled(store, (limit) => store.recall("", null, null, { candidates: "none", groups: groups.filter((g) => orgs.has(g)), limit }), ORG_RULES); return { orgs, org: org.filter((c) => c.scope === "group" && orgs.has(c.project ?? "")) }; } @@ -278,12 +291,11 @@ async function roomTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, key // Why a read of its own, as in a repo briefing: recall ranks the org last, so a busy room's // capped list would crowd its rules out. const [all, { orgs, org: allOrg }, allYours] = await Promise.all([ - store.recall("", null, null, { room, candidates: "none", limit: limit * 3 }), + filled(store, (n) => store.recall("", null, null, { room, candidates: "none", limit: n }), limit * 3), orgRules(store, groups), - store.recentUserCandidates(identity, ctx.session, RECENT_USER, "room"), + filled(store, (n) => store.recentUserCandidates(identity, ctx.session, n, "room"), RECENT_USER), ]); - await markDisputed(store, [...all, ...allOrg, ...allYours]); - const [settled, org, yours] = [undisputed(all), undisputed(allOrg), undisputed(allYours)]; + const [settled, org, yours] = [undisputed(all), undisputed(allOrg).slice(0, ORG_RULES), undisputed(allYours).slice(0, RECENT_USER)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); await store.recordUsage(ctx.session, [["org", org], ["settled", claims], ["yours", yours]], "briefing", null); @@ -433,15 +445,15 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, const limit = Math.min(a.limit ?? 12, 30); // An org's rules apply to every contributor, so they come first under their own heading. const [all, { orgs, org: allOrg }, allRecent, allYours] = await Promise.all([ - store.recall("", project, user, { candidates: "none", groups, limit: limit * 3 }), + filled(store, (n) => store.recall("", project, user, { candidates: "none", groups, limit: n }), limit * 3), orgRules(store, groups), // Why: the briefing stays settled memory; others' unconfirmed claims get a short, labelled section. - project ? store.recentCandidates(project, ctx.session, RECENT) : [], + project ? filled(store, (n) => store.recentCandidates(project, ctx.session, n), RECENT) : [], // User scope is how this person works, read from every repo; unconfirmed lessons stay labelled and apart. - user ? store.recentUserCandidates(user, ctx.session, RECENT_USER) : [], + user ? filled(store, (n) => store.recentUserCandidates(user, ctx.session, n), RECENT_USER) : [], ]); - await markDisputed(store, [...all, ...allOrg, ...allRecent, ...allYours]); - const [settled, org, recent, yours] = [undisputed(all), undisputed(allOrg), undisputed(allRecent), undisputed(allYours)]; + const [settled, org, recent, yours] = [undisputed(all), undisputed(allOrg).slice(0, ORG_RULES), + undisputed(allRecent).slice(0, RECENT), undisputed(allYours).slice(0, RECENT_USER)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); // In the order the reply renders them: yours comes before recent. diff --git a/worker/test/dispute.test.ts b/worker/test/dispute.test.ts index f5469df..f3d66a5 100644 --- a/worker/test/dispute.test.ts +++ b/worker/test/dispute.test.ts @@ -100,3 +100,16 @@ test("a later rating from the same session without write access doesn't lift its await aRo("feedback", { ratings: [{ handle: id, ...USED }] }); assert.doesNotMatch(await agent(db, "x")("briefing"), /wrangler port/); }); + +test("disputed claims don't use up a section's limit: the briefing fills past them", async () => { + const db = await setup(); + const w = agent(db, "w"), a = agent(db, "a"); + await w("claim", { subject: "older lesson", content: "an older lesson about ports" }); + const newer: string[] = []; + for (const s of ["one", "two", "three"]) newer.push((await w("claim", { subject: `lesson ${s}`, content: `a lesson about ports ${s}` })).split("\n")[0]); + await a("recall", { query: "ports", include_candidates: true }); + await a("feedback", { ratings: newer.map((handle) => ({ handle, ...WRONG })) }); + const recent = (await agent(db, "x")("briefing")).split("# recent from other agents (unconfirmed)\n")[1] ?? ""; + assert.match(recent, /older lesson/, `the older undisputed candidate fills the section: ${recent}`); + assert.doesNotMatch(recent, /lesson (one|two|three)/); +}); From 0ef0a4999cd37c21a5aaa46cdb4adcba3e6901fb Mon Sep 17 00:00:00 2001 From: vu1n <2105991+vu1n@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:38:04 +0000 Subject: [PATCH 4/4] worker: the dispute rule lives in SQL; briefing queries exclude disputed claims before LIMIT --- docs/features/hosted-worker.md | 19 +++++++------ worker/src/mcp.ts | 37 +++++++------------------ worker/src/store.ts | 49 +++++++++++++++++----------------- worker/test/dispute.test.ts | 18 +++++++++++++ 4 files changed, 61 insertions(+), 62 deletions(-) diff --git a/docs/features/hosted-worker.md b/docs/features/hosted-worker.md index 36c8b21..5d4d64d 100644 --- a/docs/features/hosted-worker.md +++ b/docs/features/hosted-worker.md @@ -213,16 +213,15 @@ TypeSafe API's shape, for GitHub Actions OIDC tokens (`worker/src/oidc.ts`) and claim rows the version guard reads. This is the only way a session that was shown a candidate can help accept it: its re-claim is an echo, and the claim reply says so. The Python arbiter doesn't count ratings, so local and hosted promote differently here. -- A `wrong` rating disputes its claim (`store.disputes`): while some session's newest writable verdict - is `wrong` and no newest writable `used` came after it, every briefing section drops the claim and - recall marks it `⚠ a session rated this wrong`; `expand` returns `rated_wrong: {at}`. The note is never - shown to agents: it skips the write gate, so it would be an unscreened channel. It is read-side only and - keyed on the claim id, so a new version under the subject starts clean and no status changes. - Verdict order is the row order (`created_at`, then `rowid`), not the timestamp alone. Unmarked - (non-writable) rows are dropped before each session's newest is picked, so a session id reused with a - read-only token can't lift its own dispute. A new read path that renders claims into a - briefing must fetch through `filled`, which marks disputes and widens the page while too few - undisputed claims came back, so disputed claims can't use up a section's limit. The local store has no feedback, so local briefings don't drop them. +- A `wrong` rating disputes its claim. The rule is one SQL fragment, `DISPUTED` in `store.ts`: some + session's newest writable verdict is `wrong` and no newest writable `used` came after it, in + (`created_at`, `rowid`) order. Unmarked rows are ignored when picking a session's newest, so a session + id reused with a read-only token can't lift its own dispute. Briefing queries exclude disputed claims + in SQL (`recall(..., { undisputed: true })`, both `recent*Candidates`), so they never use up a LIMIT; + a new briefing read must do the same. Recall marks them `⚠ a session rated this wrong` and `expand` + returns `rated_wrong: {at}` (`store.disputes`). The note is never shown to agents: it skips the write + gate. Keyed on the claim id, so a new version under the subject starts clean; no status changes. The + local store has no feedback, so local briefings don't drop them. - `initialize` instructions are built from `STEPS` in `mcp.ts`, keeping only the steps whose tool the caller can see (`visibleTools`), so a read-only or `KYPP_CORRECT=off` caller is never told to call a tool it lacks. diff --git a/worker/src/mcp.ts b/worker/src/mcp.ts index cebbcf6..4f0ec14 100644 --- a/worker/src/mcp.ts +++ b/worker/src/mcp.ts @@ -173,26 +173,12 @@ function claimDict(c: Claim, dispute?: Dispute) { ...(dispute ? { rated_wrong: dispute } : {}) }; } -// A claim a session rated `wrong` stays out of the briefing, which every session loads, until a later -// `used` from a session clears it or a new version under its subject replaces it. Recall still shows it, marked. +// A claim a session rated `wrong` stays out of the briefing, which every session loads (its queries pass +// `undisputed`), until a later `used` clears it or a new version replaces it. Recall still shows it, marked. async function markDisputed(store: D1Store, claims: Claim[]): Promise { const d = await store.disputes(claims.map((c) => c.id)); for (const c of claims) if (d.has(c.id)) c.disputed = true; } -const undisputed = (cs: Claim[]) => cs.filter((c) => !c.disputed); - -// Fetches `want` claims, marks the disputed ones, and fetches a wider page while too few undisputed -// ones came back, so disputed claims can't use up a section's query limit. Returns every fetched claim. -const FILL_ROUNDS = 4; -async function filled(store: D1Store, fetch: (n: number) => Promise, want: number): Promise { - let n = want, got: Claim[] = []; - for (let round = 0; round < FILL_ROUNDS; round++, n *= 4) { - got = await fetch(n); - await markDisputed(store, got); - if (got.length < n || undisputed(got).length >= want) break; - } - return got; -} async function expanded(store: D1Store, c: Claim): Promise { const d = (await store.disputes([c.id])).get(c.id); @@ -248,7 +234,7 @@ const NEAR_SUBJECTS = 3; async function orgRules(store: D1Store, groups: string[]): Promise<{ orgs: Set; org: Claim[] }> { const orgs = await store.orgs(groups); if (!orgs.size) return { orgs, org: [] }; - const org = await filled(store, (limit) => store.recall("", null, null, { candidates: "none", groups: groups.filter((g) => orgs.has(g)), limit }), ORG_RULES); + const org = await store.recall("", null, null, { candidates: "none", groups: groups.filter((g) => orgs.has(g)), limit: ORG_RULES, undisputed: true }); return { orgs, org: org.filter((c) => c.scope === "group" && orgs.has(c.project ?? "")) }; } @@ -290,12 +276,11 @@ async function roomTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, key const limit = Math.min(a.limit ?? 12, 30); // Why a read of its own, as in a repo briefing: recall ranks the org last, so a busy room's // capped list would crowd its rules out. - const [all, { orgs, org: allOrg }, allYours] = await Promise.all([ - filled(store, (n) => store.recall("", null, null, { room, candidates: "none", limit: n }), limit * 3), + const [settled, { orgs, org }, yours] = await Promise.all([ + store.recall("", null, null, { room, candidates: "none", limit: limit * 3, undisputed: true }), orgRules(store, groups), - filled(store, (n) => store.recentUserCandidates(identity, ctx.session, n, "room"), RECENT_USER), + store.recentUserCandidates(identity, ctx.session, RECENT_USER, "room"), ]); - const [settled, org, yours] = [undisputed(all), undisputed(allOrg).slice(0, ORG_RULES), undisputed(allYours).slice(0, RECENT_USER)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); await store.recordUsage(ctx.session, [["org", org], ["settled", claims], ["yours", yours]], "briefing", null); @@ -444,16 +429,14 @@ async function repoTool(store: D1Store, name: string, a: Args, ctx: ToolCtx, case "briefing": { const limit = Math.min(a.limit ?? 12, 30); // An org's rules apply to every contributor, so they come first under their own heading. - const [all, { orgs, org: allOrg }, allRecent, allYours] = await Promise.all([ - filled(store, (n) => store.recall("", project, user, { candidates: "none", groups, limit: n }), limit * 3), + const [settled, { orgs, org }, recent, yours] = await Promise.all([ + store.recall("", project, user, { candidates: "none", groups, limit: limit * 3, undisputed: true }), orgRules(store, groups), // Why: the briefing stays settled memory; others' unconfirmed claims get a short, labelled section. - project ? filled(store, (n) => store.recentCandidates(project, ctx.session, n), RECENT) : [], + project ? store.recentCandidates(project, ctx.session, RECENT) : [], // User scope is how this person works, read from every repo; unconfirmed lessons stay labelled and apart. - user ? filled(store, (n) => store.recentUserCandidates(user, ctx.session, n), RECENT_USER) : [], + user ? store.recentUserCandidates(user, ctx.session, RECENT_USER) : [], ]); - const [settled, org, recent, yours] = [undisputed(all), undisputed(allOrg).slice(0, ORG_RULES), - undisputed(allRecent).slice(0, RECENT), undisputed(allYours).slice(0, RECENT_USER)]; const isOrg = (c: Claim) => c.scope === "group" && orgs.has(c.project ?? ""); const claims = briefingOrder(settled.filter((c) => !isOrg(c)), limit); // In the order the reply renders them: yours comes before recent. diff --git a/worker/src/store.ts b/worker/src/store.ts index 7124e2a..a4dafbb 100644 --- a/worker/src/store.ts +++ b/worker/src/store.ts @@ -112,6 +112,19 @@ const UNSORTED = "scope = 'project' AND project IS NULL"; // A session's verdict is its newest rating of the claim; it confirms only if that rating is `used` and marked writable. const CONFIRMING = "CASE WHEN json_extract(u.metadata, '$.writable') = 1 THEN u.score END"; +// A claim is disputed while some session's newest writable verdict on it is `wrong` and no session's +// newest writable verdict is a `used` made after that. Only writable ratings count, and unmarked rows +// are ignored when picking a session's newest, so a read-only rating can't lift a dispute. +// Verdict order is (created_at, rowid), so two ratings in one millisecond still have an order. +const RATED = (a: string) => `${a}.surface = 'feedback' AND json_extract(${a}.metadata, '$.writable') = 1`; +const LATER = (a: string, b: string) => `(${a}.created_at > ${b}.created_at OR (${a}.created_at = ${b}.created_at AND ${a}.rowid > ${b}.rowid))`; +const NEWEST = (a: string, n: string) => + `NOT EXISTS (SELECT 1 FROM claim_usages ${n} WHERE ${n}.claim_id = ${a}.claim_id AND ${n}.consumer = ${a}.consumer AND ${RATED(n)} AND ${LATER(n, a)})`; +const STANDING_WRONG = (t: string) => `SELECT w.created_at FROM claim_usages w WHERE w.claim_id = ${t}.id AND ${RATED("w")} AND w.score = -1` + + ` AND ${NEWEST("w", "wn")} AND NOT EXISTS (SELECT 1 FROM claim_usages u WHERE u.claim_id = w.claim_id AND ${RATED("u")}` + + ` AND u.score = 1 AND ${LATER("u", "w")} AND ${NEWEST("u", "un")})`; +const DISPUTED = (t: string) => `EXISTS (${STANDING_WRONG(t)})`; + /** A claim's level, keyed the way its group is: a project, a person's own scope, or a room's lane. */ export type Lane = { project: string } | { user: string } | { room: string }; /** An earlier claim of this session's with the same content under another subject, and its provenance. */ @@ -377,7 +390,8 @@ export class D1Store { // store.recall: accepted, nearer level (project, then its groups, then user, then global), confidence. // The id is last so equal rows come back in the same order on every engine. async recall(match: string, project: string | null, user: string | null, - opts: { candidates?: Candidates; groups?: string[]; types?: string[]; agent?: string; limit?: number; session?: string | null; room?: RoomScope } = {}): Promise { + opts: { candidates?: Candidates; groups?: string[]; types?: string[]; agent?: string; limit?: number; session?: string | null; room?: RoomScope; + undisputed?: boolean } = {}): Promise { match = match.trim(); const [where, params] = opts.room ? roomVisible("c", opts.room, opts.candidates ?? "origin") @@ -387,6 +401,8 @@ export class D1Store { let filter = screen + (types.length ? ` AND c.type IN (${types.map(() => "?").join(",")})` : ""); const fparams: unknown[] = [...sparams, ...types]; if (opts.agent) { filter += " AND c.agent = ?"; fparams.push(opts.agent); } + // Briefings leave disputed claims out in the query itself, so they can't use up its LIMIT. + if (opts.undisputed) filter += ` AND NOT ${DISPUTED("c")}`; // `groups` comes nearest first, so a parent's claim outranks a grandparent's. const groups = opts.room?.groups ?? opts.groups ?? []; const near = groups.length ? `1 + (CASE c.project ${groups.map((_, i) => `WHEN ? THEN ${i}`).join(" ")} END) * 0.01` : "1"; @@ -567,31 +583,14 @@ export class D1Store { return out; } - // Per claim, when its standing `wrong` was made. A claim is disputed while some session's newest - // writable verdict on it is `wrong` and no session's newest writable verdict is a `used` made after it. - // Unmarked rows are dropped before picking each session's newest, so a later read-only rating can't - // lift a dispute. The note is never returned: it skipped the write gate, so other agents don't read it. + // Per claim, when its newest standing `wrong` was made, for the claims that are disputed. async disputes(ids: string[]): Promise> { - const out = new Map(); - if (!ids.length) return out; + if (!ids.length) return new Map(); const { results } = await this.db.prepare( - "SELECT u.claim_id, u.consumer, u.score, u.created_at FROM claim_usages u JOIN json_each(?) j ON j.value = u.claim_id" - + " WHERE u.surface = 'feedback' AND json_extract(u.metadata, '$.writable') = 1 ORDER BY u.created_at, u.rowid", + `SELECT c.id, (SELECT MAX(created_at) FROM (${STANDING_WRONG("c")})) AS at` + + ` FROM memory_claims c JOIN json_each(?) j ON j.value = c.id WHERE ${DISPUTED("c")}`, ).bind(JSON.stringify([...new Set(ids)])).all(); - // Rows arrive in rating order (created_at, then rowid), so the index orders verdicts made in the same millisecond. - const latest = new Map, number]>(); - results.forEach((r, i) => latest.set(`${r.claim_id}\u0000${r.consumer}`, [r, i])); - const used = new Map(), wrong = new Map(); - for (const [r, i] of latest.values()) { - const id = r.claim_id as string; - if (r.score === 1 && i > (used.get(id) ?? -1)) used.set(id, i); - if (r.score === -1 && i > (wrong.get(id) ?? -1)) { - wrong.set(id, i); - out.set(id, { at: r.created_at as string }); - } - } - for (const id of out.keys()) if ((used.get(id) ?? -1) > wrong.get(id)!) out.delete(id); - return out; + return new Map(results.map((r) => [r.id as string, { at: r.at as string }])); } // Every group or org `project` rolls up to, nearest first, through parents of parents up to three @@ -623,7 +622,7 @@ export class D1Store { const [screen, sparams] = this.screen("memory_claims", session); const { results } = await this.db.prepare( `SELECT ${claimColumns()} FROM memory_claims WHERE status = 'candidate' AND scope = ? AND user = ? AND session IS NOT ?` - + screen + " ORDER BY created_at DESC LIMIT ?", + + screen + ` AND NOT ${DISPUTED("memory_claims")} ORDER BY created_at DESC LIMIT ?`, ).bind(scope, user, session, ...sparams, limit).all(); return results.map(hydrate); } @@ -633,7 +632,7 @@ export class D1Store { const [screen, sparams] = this.screen("memory_claims", session); const { results } = await this.db.prepare( `SELECT ${claimColumns()} FROM memory_claims WHERE status = 'candidate' AND scope = 'project' AND project = ? AND session IS NOT ?` - + screen + " ORDER BY created_at DESC LIMIT ?", + + screen + ` AND NOT ${DISPUTED("memory_claims")} ORDER BY created_at DESC LIMIT ?`, ).bind(project, session, ...sparams, limit).all(); return results.map(hydrate); } diff --git a/worker/test/dispute.test.ts b/worker/test/dispute.test.ts index f3d66a5..5db2e7d 100644 --- a/worker/test/dispute.test.ts +++ b/worker/test/dispute.test.ts @@ -113,3 +113,21 @@ test("disputed claims don't use up a section's limit: the briefing fills past th assert.match(recent, /older lesson/, `the older undisputed candidate fills the section: ${recent}`); assert.doesNotMatch(recent, /lesson (one|two|three)/); }); + +test("the briefing finds undisputed memory past more disputed claims than one query returns", async () => { + const db = await setup(); + const disputed: string[] = []; + for (let i = 0; i < 55; i++) disputed.push(await accepted(db, `trap ${i}`, `a trap about ports, number ${i}`)); + const keep = await accepted(db, "a lesson that held", "this one was never rated wrong"); + // The undisputed claim is the oldest, so it ranks after every disputed one. + await db.prepare("UPDATE memory_claims SET updated_at = '2000-01-01T00:00:00Z' WHERE id = ?").bind(keep).run(); + // Session "a" was shown every claim, so it may rate them. + await db.prepare("INSERT INTO claim_usages(id,consumer,claim_id,project,scope,surface,created_at)" + + " SELECT 'shown-' || id, 'a', id, 'kypp', 'project', 'recall', '2026-11-07T00:00:00Z' FROM memory_claims").run(); + const a = agent(db, "a"); + await a("feedback", { ratings: disputed.slice(0, 50).map((handle) => ({ handle, ...WRONG })) }); + await a("feedback", { ratings: disputed.slice(50).map((handle) => ({ handle, ...WRONG })) }); + const brief = await agent(db, "x")("briefing", { limit: 30 }); + assert.match(brief, /a lesson that held/); + assert.doesNotMatch(brief, /trap \d/); +});