-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(responses): recover encrypted combo agent tasks safely #2850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { | ||
| clearResponseStateForTests, | ||
| clearResponseStateMemoryForTests, | ||
| responseContinuationRetainedStoreSnapshot, | ||
| runPendingResponseStatePersistForTests, | ||
| } from "../src/responses/state"; | ||
| import { resetAgentTaskRecoveryState } from "../src/server/responses/agent-task-recovery"; | ||
| import { | ||
| codexHeaders, | ||
| encryptedInput, | ||
| FERNET_TASK, | ||
| originalFetch, | ||
| post, | ||
| providerResponse, | ||
| recoverySse, | ||
| routedConfig, | ||
| } from "./helpers/agent-task-recovery"; | ||
|
|
||
| function providerCompletion(): Response { | ||
| return Response.json({ | ||
| id: "chatcmpl_combo_recovery", | ||
| object: "chat.completion", | ||
| choices: [{ | ||
| index: 0, | ||
| message: { role: "assistant", content: "done" }, | ||
| finish_reason: "stop", | ||
| }], | ||
| usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | ||
| }); | ||
| } | ||
|
|
||
| function comboConfig(targets: Array<{ provider: string; model: string }>) { | ||
| const config = routedConfig(); | ||
| config.combos = { | ||
| routed: { | ||
| strategy: "failover", | ||
| targets, | ||
| }, | ||
| }; | ||
| return config; | ||
| } | ||
|
|
||
| describe("combo path encrypted agent task recovery", () => { | ||
| const priorHome = process.env["OPENCODEX_HOME"]; | ||
| let home: string; | ||
|
|
||
| beforeEach(() => { | ||
| home = mkdtempSync(join(tmpdir(), "ocx-agent-task-combo-")); | ||
| process.env["OPENCODEX_HOME"] = home; | ||
| clearResponseStateMemoryForTests(); | ||
| resetAgentTaskRecoveryState(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| resetAgentTaskRecoveryState(); | ||
| clearResponseStateForTests(); | ||
| rmSync(home, { recursive: true, force: true }); | ||
| if (priorHome === undefined) delete process.env["OPENCODEX_HOME"]; | ||
| else process.env["OPENCODEX_HOME"] = priorHome; | ||
| }); | ||
|
|
||
| test("recovers an all-third-party combo once without retaining plaintext continuation state", async () => { | ||
| const assignment = "RECOVERED-COMBO-PLAINTEXT-SENTINEL"; | ||
| const fetchedUrls: string[] = []; | ||
| globalThis.fetch = (async (input) => { | ||
| const url = String(input); | ||
| fetchedUrls.push(url); | ||
| if (url.includes("chatgpt.com")) { | ||
| return new Response(recoverySse(assignment), { | ||
| status: 200, | ||
| headers: { "content-type": "text/event-stream" }, | ||
| }); | ||
| } | ||
| return providerCompletion(); | ||
| }) as typeof fetch; | ||
|
|
||
| const response = await post( | ||
| comboConfig([{ provider: "xai", model: "grok-4.5" }]), | ||
| "combo/routed", | ||
| encryptedInput(), | ||
| codexHeaders(), | ||
| ); | ||
| await runPendingResponseStatePersistForTests(); | ||
| const responsePayload = await response.clone().json() as { id?: string }; | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(typeof responsePayload.id).toBe("string"); | ||
| expect(fetchedUrls).toHaveLength(2); | ||
| expect(fetchedUrls[0]).toContain("chatgpt.com/backend-api/codex/responses"); | ||
| expect(responseContinuationRetainedStoreSnapshot().count).toBe(0); | ||
| const snapshotPath = join(home, "responses-state.json"); | ||
| const snapshot = existsSync(snapshotPath) ? readFileSync(snapshotPath, "utf8") : ""; | ||
| expect(snapshot).not.toContain(assignment); | ||
| expect(snapshot).not.toContain(responsePayload.id!); | ||
| }); | ||
|
|
||
| test("keeps the canonical target bypass in a mixed combo without running recovery", async () => { | ||
| const forwardedBodies: string[] = []; | ||
| globalThis.fetch = (async (_input, init) => { | ||
| forwardedBodies.push(typeof init?.body === "string" ? init.body : ""); | ||
| return providerResponse(); | ||
| }) as typeof fetch; | ||
|
|
||
| const response = await post( | ||
| comboConfig([ | ||
| { provider: "xai", model: "grok-4.5" }, | ||
| { provider: "openai", model: "gpt-5.5" }, | ||
| ]), | ||
| "combo/routed", | ||
| encryptedInput(), | ||
| codexHeaders(), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| expect(forwardedBodies).toHaveLength(1); | ||
| expect(forwardedBodies[0]).toContain(FERNET_TASK); | ||
| expect(forwardedBodies[0]).not.toContain("capture_assignment"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the caller disconnects while
recoverEncryptedAgentTaskis awaiting the recovery upstream, the abort signal makes recovery returnfalse, but this branch converts that outcome into a 400unreadable_encrypted_agent_task. The direct recovery path and existing combo cancellation handling instead return 499client_cancelled; checkoptions.abortSignal/req.signalhere before returning the unreadable-task response so cancellation retains the established status and error mapping.AGENTS.md reference: src/AGENTS.md:L17-L19
Useful? React with 👍 / 👎.