@@ -23,6 +23,7 @@ import {
2323} from "./transcript-history-projection.js" ;
2424import {
2525 attachCodexMirrorAttestation ,
26+ attachCodexMirrorRunId ,
2627 fingerprintCodexMirrorSourceMessage ,
2728 readCodexMirrorSourceFingerprint ,
2829} from "./transcript-mirror-attestation.js" ;
@@ -134,6 +135,8 @@ async function mirrorBestEffort(params: {
134135 // identity (not via the scope). Dropping `turnId` from the scope here is
135136 // what lets a re-emitted prior-turn entry collide with its existing key.
136137 idempotencyScope : `codex-app-server:${ params . threadId } ` ,
138+ runId : params . params . runId ,
139+ runMirrorIdentityPrefix : `${ params . turnId } :` ,
137140 terminalAssistantOwner : {
138141 mirrorIdentity : `${ params . turnId } :assistant` ,
139142 runId : params . params . runId ,
@@ -166,11 +169,13 @@ async function mirrorBestEffort(params: {
166169 ) ;
167170 } ) ;
168171 const assistantMirrorIdentity = `${ params . turnId } :assistant` ;
169- const assistantTranscriptOwned =
170- mirrorResult . assistantMirrorIdentitiesOwned . includes ( assistantMirrorIdentity ) ;
171- const assistantTranscriptMessage = assistantTranscriptOwned
172- ? mirroredMessages . find ( ( message ) => readMirrorIdentity ( message ) === assistantMirrorIdentity )
173- : undefined ;
172+ const assistantTranscriptMessage = mirroredMessages . find (
173+ ( message ) => readMirrorIdentity ( message ) === assistantMirrorIdentity ,
174+ ) ;
175+ const assistantTranscriptOwned = Boolean (
176+ assistantTranscriptMessage &&
177+ mirrorResult . assistantMirrorIdentitiesOwned . includes ( assistantMirrorIdentity ) ,
178+ ) ;
174179 const assistantTranscriptIdempotencyKey = normalizeOptionalString (
175180 ( assistantTranscriptMessage as { idempotencyKey ?: unknown } | undefined ) ?. idempotencyKey ,
176181 ) ;
@@ -283,6 +288,8 @@ export async function mirrorPromptAtTurnStartBestEffort(params: {
283288 cwd : params . cwd ,
284289 messages : [ userPromptMessage ] ,
285290 idempotencyScope : `codex-app-server:${ params . threadId } ` ,
291+ runId : params . params . runId ,
292+ runMirrorIdentityPrefix : `${ params . turnId } :` ,
286293 config : params . params . config ,
287294 } ) ;
288295 for ( const receipt of mirrorResult . userMessageReceipts ) {
@@ -327,6 +334,8 @@ async function mirror(params: {
327334 storePath ?: string ;
328335 messages : AgentMessage [ ] ;
329336 idempotencyScope ?: string ;
337+ runId ?: string ;
338+ runMirrorIdentityPrefix ?: string ;
330339 terminalAssistantOwner ?: { mirrorIdentity : string ; runId : string } ;
331340 config ?: SessionTranscriptWriteLockParams [ "config" ] ;
332341 skipBeforeMessageWriteHooks ?: boolean ;
@@ -377,8 +386,22 @@ async function mirror(params: {
377386 idempotencyKeys : candidateIdempotencyKeys ,
378387 } ) ;
379388 for ( const { dedupeIdentity, idempotencyKey, message, sourceFingerprint } of candidates ) {
389+ const mirrorIdentity = readMirrorIdentity ( message ) ;
390+ const ownsRun = Boolean (
391+ params . runId &&
392+ ( ! params . runMirrorIdentityPrefix ||
393+ mirrorIdentity ?. startsWith ( params . runMirrorIdentityPrefix ) ) ,
394+ ) ;
395+ const terminalOwner = params . terminalAssistantOwner ;
396+ const ownsTerminal = Boolean (
397+ ownsRun && terminalOwner && mirrorIdentity === terminalOwner . mirrorIdentity ,
398+ ) ;
399+ const ownedMessage =
400+ ownsRun && params . runId
401+ ? attachCodexMirrorRunId ( message , params . runId , ownsTerminal )
402+ : message ;
380403 const transcriptMessage = {
381- ...attachCodexMirrorAttestation ( message , sourceFingerprint ) ,
404+ ...attachCodexMirrorAttestation ( ownedMessage , sourceFingerprint ) ,
382405 ...( idempotencyKey ? { idempotencyKey } : { } ) ,
383406 } as AgentMessage ;
384407 if ( idempotencyKey && mirrorFacts . existingIdempotencyKeys . has ( idempotencyKey ) ) {
@@ -428,12 +451,14 @@ async function mirror(params: {
428451 }
429452 : attachCodexMirrorAttestation ( nextMessage , sourceFingerprint )
430453 ) as AgentMessage ;
431- const mirrorIdentity = readMirrorIdentity ( message ) ;
432454 if ( mirrorIdentity ) {
433455 // Hooks may replace the whole message. Restore the provider-owned
434456 // identity so retries cannot turn a stale idempotency hit into evidence.
435457 messageToAppend = attachCodexMirrorIdentity ( messageToAppend , mirrorIdentity ) ;
436458 }
459+ if ( ownsRun && params . runId ) {
460+ messageToAppend = attachCodexMirrorRunId ( messageToAppend , params . runId , ownsTerminal ) ;
461+ }
437462 messageToAppend = projectAgentHarnessTranscriptMessageForDisplay ( {
438463 hidden : ( message as { display ?: boolean } ) . display === false ,
439464 message : messageToAppend ,
0 commit comments