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
19 changes: 19 additions & 0 deletions src/tools/claimGuard.ts
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 7 additions & 1 deletion src/tools/updateTaskStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -207,7 +208,12 @@ export const updateTaskStatus: Tool<typeof InputSchema> = {
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) {
Expand Down
22 changes: 22 additions & 0 deletions tests/claimGuard.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading