From 1af6d64b8c0208834c76bd8f4685da21fd66b959 Mon Sep 17 00:00:00 2001 From: JulioMCruz Date: Thu, 8 Oct 2026 16:04:22 +0800 Subject: [PATCH] Let tokenless status updates commit while their claim is unchanged The transaction compared the stored claim token with the caller's, so board MCP calls (which send no token and pass the pre-check for back-compat) always failed with task claim changed during update. The guard now compares against the token checked before the transaction when the caller sends none. --- src/tools/claimGuard.ts | 19 +++++++++++++++++++ src/tools/updateTaskStatus.ts | 8 +++++++- tests/claimGuard.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/tools/claimGuard.ts create mode 100644 tests/claimGuard.test.ts 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); + }); +});