@@ -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 */
309318export 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 ;
0 commit comments