Open-source SEO, GEO, and marketing skills for AI agents.
fix(notfair-cmo): refuse submit_task_status on terminal tasks
Yuting Zhong3237415986d7
diff --git a/notfair-cmo/src/server/mcp-server/jsonrpc.test.ts b/notfair-cmo/src/server/mcp-server/jsonrpc.test.tsindex 70200b18..0c878c0a 100644--- a/notfair-cmo/src/server/mcp-server/jsonrpc.test.ts+++ b/notfair-cmo/src/server/mcp-server/jsonrpc.test.ts@@ -285,7 +285,7 @@ describe("handleJsonRpc — tools/call submit_task_status", () => {expect(updateTaskMock).not.toHaveBeenCalled();});- it("is a no-op on terminal tasks (already succeeded/failed/cancelled)", async () => {+ it("refuses updates to terminal tasks with an instructive error", async () => {getTaskMock.mockReturnValue({id: "t1",display_id: "x-1",@@ -308,8 +308,17 @@ describe("handleJsonRpc — tools/call submit_task_status", () => {},});if (!r || "error" in r) throw new Error("expected ok envelope");- const result = r.result as { isError: boolean };- expect(result.isError).toBe(false);+ const result = r.result as {+ isError: boolean;+ content: Array<{ text: string }>;+ };+ // Used to be a silent ok:true no-op — the agent would "restart" a+ // cancelled task and narrate success the DB never recorded. Now the+ // tool result is an error that names the terminal status and points+ // the agent at create_task.+ expect(result.isError).toBe(true);+ expect(result.content[0]!.text).toContain("done");+ expect(result.content[0]!.text).toContain("create_task");// Terminal status is preserved — no DB write happens.expect(updateTaskMock).not.toHaveBeenCalled();});diff --git a/notfair-cmo/src/server/orchestration/handlers.test.ts b/notfair-cmo/src/server/orchestration/handlers.test.tsindex ae25e14d..af26eee1 100644--- a/notfair-cmo/src/server/orchestration/handlers.test.ts+++ b/notfair-cmo/src/server/orchestration/handlers.test.ts@@ -232,6 +232,35 @@ describe("handleTaskStatus — cannot-close-with-pending-approval invariant", ());expect(r.ok && r.data.status).toBe("done");});++ it("refuses updates to terminal tasks with an instructive error (no silent no-op)", async () => {+ const { handleTaskStatus } = await import("./handlers");+ for (const terminal of ["cancelled", "failed", "done"] as const) {+ const t = createTask({+ project_slug: "demo",+ agent_id: "demo-google-ads",+ brief: "x",+ });+ testDb.prepare("UPDATE tasks SET status=? WHERE id = ?").run(terminal, t.id);+ const r = handleTaskStatus(+ { task_id: t.id, status: "working" },+ { project_slug: "demo", agent_id: "demo-google-ads" },+ );+ // The old behavior returned ok:true here — the agent "restarted" a+ // cancelled task, saw success, and narrated progress the DB never+ // recorded. The error must name the terminal status and point the+ // agent at create_task instead.+ expect(r.ok).toBe(false);+ if (!r.ok) {+ expect(r.error).toContain(terminal);+ expect(r.error).toContain("create_task");+ }+ const after = testDb+ .prepare("SELECT status FROM tasks WHERE id = ?")+ .get(t.id) as { status: string };+ expect(after.status).toBe(terminal);+ }+ });});describe("handleListMyTasks", () => {diff --git a/notfair-cmo/src/server/orchestration/handlers.ts b/notfair-cmo/src/server/orchestration/handlers.tsindex 128b7feb..04220f2d 100644--- a/notfair-cmo/src/server/orchestration/handlers.ts+++ b/notfair-cmo/src/server/orchestration/handlers.ts@@ -182,7 +182,18 @@ export function handleTaskStatus(task.status === "failed" ||task.status === "done") {- return { ok: true, data: { task_id: task.id, status: task.status } };+ // Terminal statuses never change. This used to return ok:true (a+ // silent no-op so a late status flush after a mid-run user-cancel+ // wouldn't error the turn) — but that lied to the agent: it would+ // "restart" a cancelled task, get success back, and narrate progress+ // the DB never recorded. Refuse loudly and say what to do instead.+ return {+ ok: false,+ error:+ `Task ${task.display_id} is '${task.status}' — a terminal status that cannot change. ` ++ `Do NOT claim this task was restarted or updated. If the work should be redone, ` ++ `create a NEW task via create_task and tell the user that's what you did.`,+ };}// Closing a task that's gated on an unresolved approval is almost always