diff --git a/src/tools/claimGuard.ts b/src/tools/claimGuard.ts new file mode 100644 index 0000000..c5cfbda --- /dev/null +++ b/src/tools/claimGuard.ts @@ -0,0 +1,19 @@ +/** + * An update checked against a task's active claim must still match that claim + * when it commits. A worker that sends its claim token must hold the current + * claim; a call without a token (the board MCP sends none, and the pre-check + * lets it through for back-compat) only needs the claim it was checked against + * to be unchanged. + */ +export function claimStillHeld(input: { + claimActive: boolean; + /** Claim token read before the transaction, when the call was checked. */ + checkedToken?: string; + /** Claim token read inside the transaction. */ + latestToken?: string; + /** Claim token the worker sent, if any. */ + argsToken?: string; +}): boolean { + if (!input.claimActive) return true; + return input.latestToken === (input.argsToken ?? input.checkedToken); +} diff --git a/src/tools/updateTaskStatus.ts b/src/tools/updateTaskStatus.ts index df0bef5..9d4e701 100644 --- a/src/tools/updateTaskStatus.ts +++ b/src/tools/updateTaskStatus.ts @@ -34,6 +34,7 @@ import { logActivity } from "../activityEvents.js"; import { logDeliveredResult, logTaskPickedUp } from "../coordinationLog.js"; import type { Tool } from "./types.js"; import { redactClaimTokens } from "./outputSanitizer.js"; +import { claimStillHeld } from "./claimGuard.js"; import { dispatchStateForStatus, isPlaceholderResult, isTerminalTaskTransition, refreshesClaim } from "./taskStatusPolicy.js"; import { buildTaskStatusEvents, enqueueExecutionEvents } from "../executionEvents.js"; @@ -207,7 +208,12 @@ export const updateTaskStatus: Tool = { if (!latestTask.exists) throw new Error("task disappeared during status update"); const latest = latestTask.data() as { status?: string; claim?: { token?: string } | null }; if (latest.status !== current) throw new Error("task status changed during update"); - if (claimActive && latest.claim?.token !== args.claimToken) { + if (!claimStillHeld({ + claimActive, + checkedToken: claim?.token, + latestToken: latest.claim?.token, + argsToken: args.claimToken, + })) { throw new Error("task claim changed during update"); } if (canonicalEvents.length > 0) { diff --git a/tests/claimGuard.test.ts b/tests/claimGuard.test.ts new file mode 100644 index 0000000..309713a --- /dev/null +++ b/tests/claimGuard.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from "vitest"; + +import { claimStillHeld } from "../src/tools/claimGuard.js"; + +describe("claimStillHeld", () => { + it("lets a board MCP call without a token commit while its claim is unchanged", () => { + expect(claimStillHeld({ claimActive: true, checkedToken: "c1", latestToken: "c1" })).toBe(true); + }); + + it("stops a call without a token when the claim moved to another worker meanwhile", () => { + expect(claimStillHeld({ claimActive: true, checkedToken: "c1", latestToken: "c2" })).toBe(false); + }); + + it("requires a worker that sends its token to hold the current claim", () => { + expect(claimStillHeld({ claimActive: true, checkedToken: "c1", latestToken: "c1", argsToken: "c1" })).toBe(true); + expect(claimStillHeld({ claimActive: true, checkedToken: "c1", latestToken: "c2", argsToken: "c1" })).toBe(false); + }); + + it("does not check anything when the task had no active claim", () => { + expect(claimStillHeld({ claimActive: false, latestToken: "c9" })).toBe(true); + }); +});