diff --git a/packages/paperclip-runner/src/contracts/completion-result.test.ts b/packages/paperclip-runner/src/contracts/completion-result.test.ts index 191754955c6..807ba5f0ec2 100644 --- a/packages/paperclip-runner/src/contracts/completion-result.test.ts +++ b/packages/paperclip-runner/src/contracts/completion-result.test.ts @@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest"; import { PRP_BLOCKER_ALREADY_REPORTED_MESSAGE, PRP_ENVIRONMENT_BLOCKER_ALREADY_REPORTED_MESSAGE, - PRP_SEMANTIC_RESULT_CONFLICT_MESSAGE, + PRP_RESULT_ALREADY_ACCEPTED_MESSAGE, prpSemanticResultConflictMessage, PRP_BLOCK_RESULT_OUTPUT_SCHEMA, PRP_BLOCK_RESULT_PROVIDER_INPUT_SCHEMA, @@ -303,7 +303,23 @@ describe("semantic result conflict feedback", () => { expect(prpSemanticResultConflictMessage(blocked("system", "tool_unavailable"))).toBe(PRP_ENVIRONMENT_BLOCKER_ALREADY_REPORTED_MESSAGE); expect(prpSemanticResultConflictMessage(blocked("user", "credential_missing"))).toBe(PRP_BLOCKER_ALREADY_REPORTED_MESSAGE); expect(prpSemanticResultConflictMessage(blocked("system", "credential_missing"))).toBe(PRP_BLOCKER_ALREADY_REPORTED_MESSAGE); - expect(prpSemanticResultConflictMessage({ reportedWorkDisposition: "done" })).toBe(PRP_SEMANTIC_RESULT_CONFLICT_MESSAGE); expect(PRP_BLOCKER_ALREADY_REPORTED_MESSAGE).not.toContain("automatically"); }); + + it.each(["done", "needs_review", "yielded"])("tells a provider that a %s result is final and to end the turn", disposition => { + const message = prpSemanticResultConflictMessage({ reportedWorkDisposition: disposition }); + expect(message).toBe(PRP_RESULT_ALREADY_ACCEPTED_MESSAGE); + expect(message).toContain("already accepted"); + expect(message).toContain("Do not call paperclip_finish or paperclip_block again in this turn"); + expect(message).toContain("final response"); + expect(message).toContain("end the turn"); + expect(prpSemanticResultConflictMessage(null)).toBe(PRP_RESULT_ALREADY_ACCEPTED_MESSAGE); + }); + + it("tells a provider to stop both report tools after a committed blocker", () => { + for (const message of [PRP_BLOCKER_ALREADY_REPORTED_MESSAGE, PRP_ENVIRONMENT_BLOCKER_ALREADY_REPORTED_MESSAGE]) { + expect(message).toContain("Do not call paperclip_finish or paperclip_block again in this turn"); + expect(message).toContain("end the turn"); + } + }); }); diff --git a/packages/paperclip-runner/src/contracts/completion-result.ts b/packages/paperclip-runner/src/contracts/completion-result.ts index 42750c88507..026becebbe6 100644 --- a/packages/paperclip-runner/src/contracts/completion-result.ts +++ b/packages/paperclip-runner/src/contracts/completion-result.ts @@ -6,10 +6,14 @@ export const PRP_COMPLETION_TOOL_DESCRIPTION = "Report completed work (done), work requiring review (needs_review), or an explicit wait (yielded with response_wake for a response, monitor after set_task_monitor confirms a schedule on this task, or omit continuation to use this run's authorized published-review wait or saved repository-preparation continuation). Use the current completion contract and supporting evidence. If rejected, correct the report and retry. After acceptance, read the returned outcome; do not claim completion while gated. Explain any required approval with its supplied link and action, then write the final response and end the turn without further tool calls."; export const PRP_BLOCK_TOOL_DESCRIPTION = "Report work that cannot continue because of a concrete blocker. Routine repository preparation is not a blocker: use paperclip_finish with yielded and omit continuation to resume through the saved workspace continuation. Identify the blocker, its owner, and the action needed to unblock it; use the current completion contract and supporting evidence. If rejected, correct the report and retry. After acceptance, read the returned outcome, explain the blocker and any required action in the final response, and end the turn without further tool calls."; -export const PRP_SEMANTIC_RESULT_CONFLICT_MESSAGE = "A different semantic result was already committed"; +const PRP_STOP_REPORTING_INSTRUCTION = + "a turn's first accepted report is final, so this report was not recorded. Do not call paperclip_finish or paperclip_block again in this turn. "; +export const PRP_RESULT_ALREADY_ACCEPTED_MESSAGE = + "This turn's result was already accepted, and " + PRP_STOP_REPORTING_INSTRUCTION + + "Put any extra notes in your final response, then end the turn."; export const PRP_BLOCKER_ALREADY_REPORTED_MESSAGE = - "This turn already reported a blocker, and a turn's first accepted report is final, so this report was not recorded. Do not report again in this turn. " + - "In your final response, list the steps that are complete and say whether the blocker still applies, then end the turn."; + "This turn already reported a blocker, and " + PRP_STOP_REPORTING_INSTRUCTION + + "In your final response, list the steps that are complete, say whether the blocker still applies, and add any extra notes, then end the turn."; export const PRP_ENVIRONMENT_BLOCKER_ALREADY_REPORTED_MESSAGE = PRP_BLOCKER_ALREADY_REPORTED_MESSAGE + " Paperclip continues this environment or tool blocker automatically in a fresh session that checks the saved work."; @@ -20,7 +24,7 @@ export function prpSemanticResultConflictMessage(committed: { reportedWorkDisposition: string; blocker?: { owner?: { kind?: string } | null; reasonCode?: string } | null; } | null | undefined): string { - if (committed?.reportedWorkDisposition !== "blocked") return PRP_SEMANTIC_RESULT_CONFLICT_MESSAGE; + if (committed?.reportedWorkDisposition !== "blocked") return PRP_RESULT_ALREADY_ACCEPTED_MESSAGE; return committed.blocker?.owner?.kind === "system" && PRP_ENVIRONMENT_BLOCKER_REASON_CODES.has(committed.blocker.reasonCode ?? "") ? PRP_ENVIRONMENT_BLOCKER_ALREADY_REPORTED_MESSAGE : PRP_BLOCKER_ALREADY_REPORTED_MESSAGE; } diff --git a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts index f783077e503..84166b7c210 100644 --- a/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts +++ b/packages/paperclip-runner/src/drivers/opencode/opencode-server-driver.test.ts @@ -30,7 +30,7 @@ import { type NativeRuntimeContextSnapshot, } from "../../contracts/runtime-context.js"; import { createCodexTaskEnvelope } from "../../contracts/codex.js"; -import { PRP_BLOCKER_ALREADY_REPORTED_MESSAGE } from "../../contracts/completion-result.js"; +import { PRP_BLOCKER_ALREADY_REPORTED_MESSAGE, PRP_RESULT_ALREADY_ACCEPTED_MESSAGE } from "../../contracts/completion-result.js"; import { CONFIGURED_ENVIRONMENT_KEYS, configuredEnvironmentProjection } from "../../configured-environment.js"; import { localIntegrityBoundaryGolden } from "../../../test-support/local-integrity-boundary-golden.js"; import type { PrpEvent } from "../../protocol/replay-contract.js"; @@ -2889,6 +2889,33 @@ describe("OpenCodeServerDriver", () => { await session.close({ reason: "test" }); }); + it("tells a provider that a turn's accepted done result is final when it reports again", async () => { + await chmod(fixture, 0o755); + const root = await mkdtemp(join(tmpdir(), "paperclip-opencode-finish-finish-")); + const workspace = await mkdtemp(join(tmpdir(), "paperclip-opencode-workspace-")); + roots.push(root, workspace); + const driver = new OpenCodeServerDriver({ model: "openrouter/deepseek/deepseek-v4-flash-0731", runtimeDirectory: root, command: fixture, environment: { PATH: process.env.PATH, OPENROUTER_API_KEY: "fixture-key" } }); + const session = await driver.openSession({ runId: "finish-then-finish", normalizedSessionId: "finish-then-finish", workingDirectory: workspace }); + await session.startTurn({ message: { role: "user", text: "finish-after-finish" } }); + const events = await collectTurnEvents(session.events()); + const results = events.filter((event) => event.eventType === "run.result.proposed"); + expect(results).toHaveLength(1); + expect(results[0].payload).toMatchObject({ reportedWorkDisposition: "done", summary: "Fake provider completed the task." }); + expect(events.at(-1)?.eventType).toBe("turn.completed"); + const files = await readdir(root, { recursive: true }); + const evidence = files.find((name) => name.endsWith("fake-finish-after-finish.json")); + expect(evidence).toBeDefined(); + const calls = JSON.parse(await readFile(join(root, evidence!), "utf8")); + expect(calls.first.result.isError).not.toBe(true); + expect(calls.repeated.result.isError).not.toBe(true); + for (const rejected of [calls.rejected, calls.rejectedBlock]) { + expect(rejected).toMatchObject({ result: { isError: true, content: [{ text: expect.stringContaining(PRP_RESULT_ALREADY_ACCEPTED_MESSAGE) }] } }); + expect(rejected.result.content[0].text).toContain("Do not call paperclip_finish or paperclip_block again in this turn"); + expect(rejected.result.content[0].text).toContain("end the turn"); + } + await session.close({ reason: "test" }); + }); + it.each(["monitor", "response_wake"] as const)("resolves an omitted %s through the real OpenCode MCP boundary only from current server state", async kind => { for (const eligible of [true, false]) { await chmod(fixture, 0o755); diff --git a/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs b/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs index 97ec466b340..753e2ad23c7 100644 --- a/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs +++ b/packages/paperclip-runner/test/fixtures/fake-opencode-server.mjs @@ -323,6 +323,17 @@ async function callTerminalTool(promptBody, signal) { const rejected = await mcpRequest("tools/call", { name: "paperclip_finish", arguments: done }); await writeFile(join(process.env.XDG_DATA_HOME, "fake-finish-after-block.json"), JSON.stringify(rejected)); } + if (!blocked && String(prompt.message ?? "").includes("finish-after-finish")) { + const repeated = await mcpRequest("tools/call", { name: "paperclip_finish", arguments: result }); + const changed = structuredClone(result); + changed.summary = "Fake provider completed the task and adds a late note."; + const rejected = await mcpRequest("tools/call", { name: "paperclip_finish", arguments: changed }); + const blockedLate = structuredClone(result); + blockedLate.reportedWorkDisposition = "blocked"; + blockedLate.blocker = { reasonCode: "test_dependency", owner: { kind: "external", name: "fixture" }, unblockAction: "Release the fixture dependency.", scope: "current_track" }; + const rejectedBlock = await mcpRequest("tools/call", { name: "paperclip_block", arguments: blockedLate }); + await writeFile(join(process.env.XDG_DATA_HOME, "fake-finish-after-finish.json"), JSON.stringify({ first, repeated, rejected, rejectedBlock })); + } if (String(prompt.message ?? prompt.task?.prompt ?? "").includes("completion-feedback")) { const outcomes = [first]; if (first.result?.isError && String(prompt.message ?? prompt.task?.prompt ?? "").includes("current-wake-completion-feedback")) {