Skip to content

Commit 6131577

Browse files
Wibiaslidge-jun
authored andcommitted
fix(server): normalize plaintext agent messages before parsing
1 parent 50aa116 commit 6131577

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎src/server/responses.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,28 @@ function encryptedSlotParts(payload: string): Array<Record<string, string>> {
298298
return parts.length > 0 ? parts : [{ type: "input_text", text: payload }];
299299
}
300300

301+
function hasEncryptedContentPart(content: unknown): boolean {
302+
return Array.isArray(content) && content.some(part => (
303+
part && typeof part === "object"
304+
&& (part as { type?: unknown }).type === "encrypted_content"
305+
));
306+
}
307+
301308
/**
302309
* Rewrite non-ciphertext `{type:"encrypted_content"}` parts into `{type:"input_text"}`
303-
* throughout a native-bound request's input items (message content and
304-
* function_call_output content arrays share the part shape, codex-rs protocol/models.rs).
310+
* throughout a request's input items (message content and function_call_output content
311+
* arrays share the part shape, codex-rs protocol/models.rs). An `agent_message` whose
312+
* payload becomes entirely plaintext is normalized to a user message so routed parsers
313+
* that do not understand the internal item type still deliver the spawn task.
305314
* Genuine backend blobs are left byte-identical so replay/cache semantics survive, and
306315
* MIXED slots (plaintext preamble + embedded Fernet task body) are split so the backend
307316
* decrypts the blob while the prose passes as text. Returns the number of parts rewritten.
308317
*/
309318
export function sanitizeEncryptedContentInPlace(input: unknown): number {
310319
if (!Array.isArray(input)) return 0;
311320
let rewritten = 0;
312-
const visit = (node: unknown): void => {
321+
const visit = (node: unknown): number => {
322+
const before = rewritten;
313323
if (Array.isArray(node)) {
314324
for (let i = 0; i < node.length; i += 1) {
315325
const child = node[i] as unknown;
@@ -327,13 +337,26 @@ export function sanitizeEncryptedContentInPlace(input: unknown): number {
327337
continue;
328338
}
329339
}
330-
visit(child);
340+
const childRewrites = visit(child);
341+
if (
342+
childRewrites > 0
343+
&& child && typeof child === "object"
344+
&& (child as { type?: unknown }).type === "agent_message"
345+
&& !hasEncryptedContentPart((child as { content?: unknown }).content)
346+
) {
347+
const message = child as { type: string; role?: string; author?: unknown; recipient?: unknown };
348+
message.type = "message";
349+
message.role = "user";
350+
delete message.author;
351+
delete message.recipient;
352+
}
331353
}
332-
return;
354+
return rewritten - before;
333355
}
334356
if (node && typeof node === "object") {
335357
for (const value of Object.values(node)) visit(value);
336358
}
359+
return rewritten - before;
337360
};
338361
visit(input);
339362
return rewritten;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ describe("sanitizeEncryptedContentInPlace", () => {
417417

418418
describe("spawn-message delivery (agent_message + encrypted slot)", () => {
419419
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).
420+
// Mirrors handleResponses order: sanitize and normalize the RAW input, then parseRequest.
421+
// Regression for spawned sub-agents receiving empty task payloads when the routed parser
422+
// does not understand agent_message and its task rides in a plaintext encrypted slot.
423423
const body = {
424424
model: "anthropic/claude-fable-5",
425425
input: [
@@ -431,6 +431,7 @@ describe("spawn-message delivery (agent_message + encrypted slot)", () => {
431431
],
432432
};
433433
expect(sanitizeEncryptedContentInPlace(body.input)).toBe(1);
434+
expect(body.input[1]).toMatchObject({ type: "message", role: "user" });
434435
const parsed = parseRequest(body);
435436
const users = parsed.context.messages.filter(m => m.role === "user");
436437
expect(users).toHaveLength(2);

0 commit comments

Comments
 (0)