Skip to content

Commit 50aa116

Browse files
Wibiaslidge-jun
authored andcommitted
fix(server): sanitize plaintext encrypted_content before parse so routed sub-agents receive spawn payloads
sanitizeEncryptedContentInPlace now runs on the raw body BEFORE parseRequest, for every path. Previously it ran only for native-bound models (no provider prefix) and only after parsing, so routed models (anthropic/*, opencode-go/*) built their parsed messages from the unsanitized input: the agent_message branch dropped the encrypted_content part carrying the actual task text, and spawned sub-agents received an empty NEW_TASK envelope. _rawBody shares the same object reference, so the native passthrough keeps the rewritten parts too; genuine Fernet ciphertext stays byte-identical (looksLikeBackendCiphertext). Adds a regression test mirroring the exact handleResponses order (sanitize, then parse).
1 parent 8852e50 commit 50aa116

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎src/server/responses.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,21 @@ export async function handleResponses(
410410
body = expandPreviousResponseInput(body);
411411
const previousResponseInputExpanded = body !== originalBody;
412412

413+
// Spawn-message compatibility (both directions): agent_message task payloads ride in
414+
// encrypted_content slots as plaintext. Rewrite them to input_text on the RAW body BEFORE
415+
// parsing so every consumer sees the payload: parseRequest (routed/translated providers read
416+
// the parsed messages) and the native passthrough (_rawBody is this same object, serialized
417+
// verbatim). Genuine backend ciphertext is left byte-identical (looksLikeBackendCiphertext).
418+
{
419+
const rewritten = sanitizeEncryptedContentInPlace(
420+
(body as { input?: unknown } | undefined)?.input,
421+
);
422+
if (rewritten > 0)
423+
console.warn(
424+
`[opencodex] rewrote ${rewritten} plaintext encrypted_content part(s) to input_text (spawn-message compatibility)`,
425+
);
426+
}
427+
413428
let parsed;
414429
try {
415430
parsed = parseRequest(body);
@@ -452,15 +467,6 @@ export async function handleResponses(
452467
// Runs BEFORE the mock-max clamp below so the synthetic top tier (ultra arrives
453468
// as max on the codex wire) is still visible. Both request shapes are rewritten.
454469
{
455-
const requestedModelId = logCtx.requestedModel ?? route.modelId;
456-
// Cross-provider spawn poison fix: native-bound requests may carry plaintext parked in
457-
// encrypted_content slots (spawn messages minted under a routed parent). Rewrite them
458-
// to input_text before the passthrough serializes _rawBody verbatim.
459-
if (!requestedModelId.includes("/")) {
460-
const raw = parsed._rawBody as { input?: unknown } | undefined;
461-
const rewritten = sanitizeEncryptedContentInPlace(raw?.input);
462-
if (rewritten > 0) console.warn(`[opencodex] ${route.modelId}: rewrote ${rewritten} plaintext encrypted_content part(s) to input_text (routed-parent spawn compatibility)`);
463-
}
464470
const guidance = await multiAgentGuidanceText(parsed, config.injectionModel, config.injectionEffort, config.subagentModels, config.injectionPrompt);
465471
if (guidance) {
466472
injectDeveloperMessage(parsed, guidance);

‎tests/multi-agent-compat.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,31 @@ describe("sanitizeEncryptedContentInPlace", () => {
414414
expect(parts[0]).toEqual({ type: "encrypted_content", encrypted_content: fernet });
415415
});
416416
});
417+
418+
describe("spawn-message delivery (agent_message + encrypted slot)", () => {
419+
test("sanitize-then-parse delivers the spawn task payload as a user message on routed paths", () => {
420+
// Mirrors handleResponses order: sanitizeEncryptedContentInPlace on the RAW input,
421+
// then parseRequest. Regression for spawned sub-agents receiving empty task payloads
422+
// (agent_message payload rides in a plaintext encrypted_content slot).
423+
const body = {
424+
model: "anthropic/claude-fable-5",
425+
input: [
426+
{ type: "message", role: "user", content: [{ type: "input_text", text: "env context" }] },
427+
{ type: "agent_message", author: "/root", recipient: "/root/worker", content: [
428+
{ type: "input_text", text: "Message Type: NEW_TASK\nTask name: /root/worker\nSender: /root\nPayload:\n" },
429+
{ type: "encrypted_content", encrypted_content: "TASK: build the thing exactly as specified." },
430+
] },
431+
],
432+
};
433+
expect(sanitizeEncryptedContentInPlace(body.input)).toBe(1);
434+
const parsed = parseRequest(body);
435+
const users = parsed.context.messages.filter(m => m.role === "user");
436+
expect(users).toHaveLength(2);
437+
const content = users[1].content;
438+
const flat = typeof content === "string"
439+
? content
440+
: (content as Array<{ type: string; text?: string }>).map(p => p.text ?? "").join("");
441+
expect(flat).toContain("Message Type: NEW_TASK");
442+
expect(flat).toContain("TASK: build the thing exactly as specified.");
443+
});
444+
});

0 commit comments

Comments
 (0)