diff --git a/AGENTS.md b/AGENTS.md index ccfcdf6070..5ce48bdaa5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -246,7 +246,7 @@ The four numbered files above are the spine. These carry the rest of the behavio - `proxy/messageSerializer.mjs` — builds the envelope. Fresh vs `continue` (delta) mode, folds the tool instructions into the last delegated instruction, tags assistant messages `commentary`/`final_answer` for phase-aware agents, and owns two recovery variants: `serializeIncompleteTaskContinuationRequest` for a turn that stopped early, and `serializeToolRecoveryRequest` for a turn that answered in prose when a tool call was required. - `proxy/toolCallReminder.mjs` — the injected instruction text: XML, JSON, and phased tool-call reminders plus the task-completion contract, and the compact tool-router prompt used by the recovery turn. - `proxy/imageInputs.mjs` + `imageDimensions.mjs` — image input validation: `data:` URLs only, PNG/JPEG/WebP, header magic bytes must match the declared type, at most 12 images, 10 MB each and 40 MB total, 8192x8192 and 32 MP. Rejects with `400 invalid_image_input`. -- `proxy/sessionThreads.mjs` — the session-to-WPP-thread mirror behind `continue` mode. Hashes instructions, tools, protocol, and every non-system message; a `continue` requires a live pinned tab, an unchanged context hash, the prior request as an exact prefix, and the WPP assistant echo at the expected boundary. Any mismatch degrades silently to a fresh replay, which is safe because the body still holds the full logical conversation. `acquireThreadTurn` serializes the whole decide → submit → commit lifecycle per session, because the worker pool locks the tab but deltas are computed before a worker is acquired. `resetThread` runs on any failure. Disable with `O1_CODE_THREAD_CONTINUITY=0`. +- `proxy/sessionThreads.mjs` — the session-to-WPP-thread mirror behind `continue` mode. Hashes instructions, tools, protocol, and every non-system message; a `continue` requires a live pinned tab, an unchanged context hash, the prior request as an exact prefix, and the WPP assistant echo at the expected boundary. The echo check ignores surrounding whitespace in assistant text, treats `null` and `""` content alike, and compares tool arguments as JSON values, because OpenCode replays the text it was streamed and re-serializes tool input. Any mismatch degrades silently to a fresh replay, which is safe because the body still holds the full logical conversation. `acquireThreadTurn` serializes the whole decide → submit → commit lifecycle per session, because the worker pool locks the tab but deltas are computed before a worker is acquired. `resetThread` runs on any failure. Disable with `O1_CODE_THREAD_CONTINUITY=0`. WPP ownership keys include client, runtime UUID, Session and agent. V1 and V2 emit runtime identity only for the `cookiemonster` provider. V1 strips configured/plugin case variants of identity headers before applying its own values; V2 request headers take precedence over route defaults. Original opencode-provider client headers remain. Clients missing runtime identity use unpinned fresh replay on every turn. Explicit CLI `--attach` uses the receiving backend's runtime and existing same-process execution coordinator. **Response out — parsing** diff --git a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.capture.test.mjs b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.capture.test.mjs index 0f0d4c7fc8..6a4c9dcb1e 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.capture.test.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.capture.test.mjs @@ -643,6 +643,36 @@ describe("required tool-call recovery", () => { expect(response.body).not.toContain(""); }); + test("writes a live-streamed preamble once before its tool call", async () => { + const response = fakeResponse(); + const preamble = "I’ll inspect the workspace now.\n"; + // Built at runtime so this file holds no literal tool-call markup. + const call = [ + "<", "function_calls><", 'invoke id="call-1" name="bash"><', + 'parameter name="command">git status --short<', "/parameter><", "/invoke><", "/function_calls>", + ].join(""); + const bridge = { + hasSession: () => false, + run: async (_prompt, options) => { + options.onProgress?.({ finalText: preamble + call }); + return bridgeRun(preamble + call); + }, + }; + const requestBody = toolBody(user("Inspect the workspace.")); + requestBody.stream = true; + + await withNoRunLogs(() => handleChatCompletions({ headers: {} }, response, requestBody, { bridge })); + + const content = response.body + .split("\n") + .filter((line) => line.startsWith("data: {") && !line.includes('"error"')) + .map((line) => JSON.parse(line.slice(6)).choices?.[0]?.delta?.content || "") + .join(""); + expect(content).toBe(preamble); + expect(response.body).toContain('"name":"bash"'); + expect(response.body).toContain('"finish_reason":"tool_calls"'); + }); + test("returns a typed error when the bounded replay also omits the required tool call", async () => { const response = fakeResponse(); const bridge = { diff --git a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs index 0a4142ab9b..f4ecba6cf9 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/openaiCompat.mjs @@ -786,12 +786,16 @@ function createStreamSession({ sentenceQuarantine = false } = {}) { }; } -// Close out a streamed completion using the authoritative normalized result. Three shapes: -// 1. tool_calls → the gate suppressed all live prose; emit structured tool-call chunks. +// Close out a streamed completion using the authoritative normalized result. Content goes first, +// then any structured tool-call chunks. Three content shapes: +// 1. recovery replay → the recovered response is a new cumulative text origin. Append it in full +// after any benign commentary already emitted from the discarded answer instead of diffing +// unrelated strings. // 2. prose already streamed live → emit only the trailing diff (normalized content past what we -// already sent), so the client isn't sent duplicate text. -// 3. nothing streamed yet (short/undecided prose, or a leading-brace turn the normalizer decided -// was content after all) → emit the whole content body. +// already sent). This covers a tool turn's preamble too: sentence quarantine releases it live up +// to the first tool-call marker, so writing it again would duplicate it in the stored message. +// 3. nothing streamed yet (short/undecided prose, a buffered tool turn, or a leading-brace turn the +// normalizer decided was content after all) → emit the whole content body. // Always terminates the stream via finishChatCompletion. The caller must unsubscribe from progress // BEFORE calling this so a late frame can't interleave a delta after finish_reason. function finalizeStreamedCompletion(response, streamBase, { @@ -805,23 +809,14 @@ function finalizeStreamedCompletion(response, streamBase, { const tool_calls = normalized.tool_calls; const content = normalized.content || ""; - if (Array.isArray(tool_calls) && tool_calls.length > 0) { - if (content) { - writeContentBody(response, streamBase, content); - } - writeToolCallChunks(response, streamBase, tool_calls); - } else if (recoveryReplay) { - // The recovery response is a new cumulative text origin. Append it in full after any benign - // commentary already emitted from the discarded answer instead of diffing unrelated strings. + if (recoveryReplay || !streamSession.hasStreamed()) { writeContentBody(response, streamBase, content); - } else if (streamSession.hasStreamed()) { - // Emit the authoritative content past what we've already streamed live. - const trailing = streamSession.trailingDiff(content); - if (trailing) { - writeContentDelta(response, streamBase, trailing); - } } else { - writeContentBody(response, streamBase, content); + writeContentDelta(response, streamBase, streamSession.trailingDiff(content)); + } + + if (Array.isArray(tool_calls) && tool_calls.length > 0) { + writeToolCallChunks(response, streamBase, tool_calls); } finishChatCompletion(response, streamBase, { diff --git a/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.mjs b/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.mjs index cfc5cef6e8..ea35321378 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.mjs @@ -51,14 +51,14 @@ function nonSystemMessages(body) { function hashMessage(message) { return hashValue({ role: message?.role || "user", - content: message?.content ?? null, + content: comparableContent(message), toolCallId: message?.tool_call_id || message?.toolCallId || "", toolCalls: Array.isArray(message?.tool_calls) ? message.tool_calls.map((call) => ({ id: call.id || "", type: call.type || "function", name: call.function?.name || call.name || "", - arguments: call.function?.arguments ?? call.arguments ?? "{}", + arguments: comparableArguments(call.function?.arguments ?? call.arguments ?? "{}"), })) : Array.isArray(message?.toolCalls) ? message.toolCalls @@ -66,6 +66,28 @@ function hashMessage(message) { }) } +// The assistant echo is compared across two producers: the response committed here and the client's +// replay of it. The client replays the text it was streamed, which keeps whitespace the normalizer +// trims (the blank line before a tool call or the completion marker), and it sends "" for a tool-only +// turn the normalizer returns as null. Neither changes the WPP thread, so ignore surrounding +// whitespace in assistant text. +function comparableContent(message) { + const content = message?.content ?? null + if (message?.role !== "assistant" || Array.isArray(content)) return content + return typeof content === "string" ? content.trim() : "" +} + +// The client re-serializes tool input it parsed from the stream, so compare arguments as JSON values; +// hashValue sorts their keys. Text that is not JSON is compared as written. +function comparableArguments(value) { + if (typeof value !== "string") return value + try { + return JSON.parse(value) + } catch { + return value + } +} + function hashesFor(body) { return nonSystemMessages(body).map(hashMessage) } diff --git a/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.test.mjs b/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.test.mjs index f90025f5c3..9fc67d0311 100644 --- a/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.test.mjs +++ b/packages/desktop/src/main/wpp-bridge/proxy/sessionThreads.test.mjs @@ -105,6 +105,38 @@ describe("decideThreadMode", () => { expect(decideThreadMode(KEY, next, true)).toEqual({ mode: "continue", sinceIndex: 2 }) }) + test("a tool turn continues when the client replays its streamed text and re-serialized arguments", () => { + commitThread(KEY, body(sys(), user("inspect")), { + content: "I'll check.", + tool_calls: [ + { + id: "t1", + type: "function", + function: { name: "bash", arguments: JSON.stringify({ command: "pwd", description: "Print dir" }) }, + }, + ], + }) + const echo = { + role: "assistant", + content: "I'll check.\n\n", + tool_calls: [ + { + id: "t1", + type: "function", + function: { name: "bash", arguments: JSON.stringify({ description: "Print dir", command: "pwd" }) }, + }, + ], + } + const next = body(sys(), user("inspect"), echo, toolResult("t1", "ok")) + expect(decideThreadMode(KEY, next, true)).toEqual({ mode: "continue", sinceIndex: 2 }) + }) + + test("a tool-only turn committed with null content continues from the client's empty string", () => { + commitThread(KEY, body(sys(), user("inspect")), { content: null, tool_calls: assistantToolCall("t1").tool_calls }) + const next = body(sys(), user("inspect"), assistantToolCall("t1"), toolResult("t1", "ok")) + expect(decideThreadMode(KEY, next, true)).toEqual({ mode: "continue", sinceIndex: 2 }) + }) + test("an unexpected assistant echo resyncs fresh", () => { const first = body(sys(), user("hello")) commitThread(KEY, first, assistant("answer")) diff --git a/packages/desktop/src/main/wpp-bridge/worker-pool.test.ts b/packages/desktop/src/main/wpp-bridge/worker-pool.test.ts index 6b9b550214..e5147da0bb 100644 --- a/packages/desktop/src/main/wpp-bridge/worker-pool.test.ts +++ b/packages/desktop/src/main/wpp-bridge/worker-pool.test.ts @@ -368,6 +368,44 @@ describe("WorkerPool cancellation", () => { expect(destroyed).toBe(true) pool.destroy() }) + + test("probes a fresh job once per worker and agent", async () => { + const pool = new WorkerPool({ chatUrl: "https://example.test/chat" }) + const jobs: string[] = [] + const entry = { + id: 1, + window: { isDestroyed: () => false, destroy: () => {} }, + controller: { + runJob: async (job: { id?: string }) => { + if (job.id?.startsWith("user-job")) { + jobs.push("user") + return { ok: true, responseSource: "network", finalText: "done" } + } + jobs.push("probe") + return { ok: true, finalText: "CM_CAPABILITY_V1_OK" } + }, + }, + netWitness: { summarizeWindow: () => ({ cdpRequestSeen: true, cdpStatus: 200, cdpFinished: true }) }, + agent: "CM_Opus5.5-High", + protocolAgent: "", + sessionKey: "", + subagent: false, + busy: true, + lastUsed: Date.now(), + } + Reflect.get(pool, "workers").set(entry.id, entry) + Reflect.set(pool, "acquire", async () => entry) + try { + for (const id of ["user-job-1", "user-job-2"]) { + await pool.run({ id, payload: { model: entry.agent } }) + } + expect(jobs).toEqual(["probe", "user", "user"]) + expect(entry.protocolAgent).toBe(entry.agent) + } finally { + pool.destroy() + } + }) + test("discards the leased worker when its client aborts", async () => { const pool = new WorkerPool({ chatUrl: "https://example.test/chat" }) let destroyed = false diff --git a/packages/desktop/src/main/wpp-bridge/worker-pool.ts b/packages/desktop/src/main/wpp-bridge/worker-pool.ts index f8ca87ca05..e9ba5acd94 100644 --- a/packages/desktop/src/main/wpp-bridge/worker-pool.ts +++ b/packages/desktop/src/main/wpp-bridge/worker-pool.ts @@ -368,10 +368,11 @@ export class WorkerPool { }, ) { const agent = (job.payload?.model || worker.agent).trim() - if (job.payload?.continueThread === true) { - if (worker.protocolAgent === agent) return - assertCapabilityResponse(null, agent) - } + // The handshake checks the WPP instruction installed on the agent, which a fresh chat in the same + // tab does not change. Probe once per worker and agent; claim() clears the result on an agent + // switch. A continued thread cannot be probed in place, so an unverified one fails for replay. + if (worker.protocolAgent === agent) return + if (job.payload?.continueThread === true) assertCapabilityResponse(null, agent) const probeJob = buildCapabilityProbeJob(job) const result = await worker.controller.runJob(probeJob, undefined, remainingJobTimeout(job))