@@ -13,7 +13,7 @@ import {
1313 resolveCliRuntimeArtifactFingerprint ,
1414 resolveCliRuntimeOwnerFingerprint ,
1515} from "../cli-auth-epoch.js" ;
16- import type { CliOutput } from "../cli-output-contracts.js" ;
16+ import type { CliOutput , CliTerminalInterruption } from "../cli-output-contracts.js" ;
1717import { claudeCliSessionTranscriptHasContent as claudeCliSessionTranscriptHasContentImpl } from "../command/attempt-execution.helpers.js" ;
1818import type { EmbeddedAgentRunResult } from "../embedded-agent-runner.js" ;
1919import { resolveExplicitFinalSourceReplyDeliveryEvidence } from "../embedded-agent-runner/delivery-evidence.js" ;
@@ -28,6 +28,11 @@ import type { PreparedCliRunContext, RunCliAgentParams } from "./types.js";
2828
2929const log = createSubsystemLogger ( "agents/cli-runner" ) ;
3030
31+ /** Operator-visible reason recorded on trace attempts and agent_end when a live turn kept partial output. */
32+ export function formatCliTerminalInterruption ( interruption : CliTerminalInterruption ) : string {
33+ return `CLI turn ${ interruption . reason } after partial output` ;
34+ }
35+
3136export const cliRunSettlementDeps = {
3237 claudeCliSessionTranscriptHasContent : claudeCliSessionTranscriptHasContentImpl ,
3338 delay : async ( delayMs : number ) => {
@@ -485,19 +490,16 @@ export function buildCliRunResult(params: {
485490 sourceReplyDeliveryMode : runParams . sourceReplyDeliveryMode ,
486491 } ) ;
487492 const unflushedCliSessionId =
488- ! output . terminalInterruption &&
489- ! sessionBindingDisabled &&
490- effectiveCliSessionId &&
491- bindingFlushOk === false
493+ ! sessionBindingDisabled && effectiveCliSessionId && bindingFlushOk === false
492494 ? effectiveCliSessionId
493495 : undefined ;
494- const persistedCliSessionId = output . terminalInterruption
495- ? undefined
496- : sessionBindingDisabled
497- ? undefined
498- : unflushedCliSessionId
499- ? undefined
500- : effectiveCliSessionId ;
496+ const terminalInterruption = output . terminalInterruption ;
497+ // An interrupted live turn closed its process, so its native continuity is dead.
498+ const cliSessionBindingCleared =
499+ terminalInterruption !== undefined ||
500+ sessionBindingDisabled ||
501+ unflushedCliSessionId !== undefined ;
502+ const persistedCliSessionId = cliSessionBindingCleared ? undefined : effectiveCliSessionId ;
501503 const createdReseedReceipt =
502504 persistedCliSessionId &&
503505 usedHistoryPrompt &&
@@ -519,15 +521,13 @@ export function buildCliRunResult(params: {
519521 ? runParams . cliSessionBinding . reseedReceipt
520522 : undefined ;
521523 const reseedReceipt = createdReseedReceipt ?? preservedReseedReceipt ;
522- const agentSessionId = output . terminalInterruption
523- ? ""
524- : sessionBindingDisabled
525- ? ( runParams . sessionId ?? "" )
526- : unflushedCliSessionId
527- ? ""
524+ const agentSessionId =
525+ terminalInterruption || unflushedCliSessionId
526+ ? ""
527+ : sessionBindingDisabled
528+ ? ( runParams . sessionId ?? "" )
528529 : ( effectiveCliSessionId ?? runParams . sessionId ?? "" ) ;
529530 const yielded = output . yielded === true ;
530- const terminalInterruption = output . terminalInterruption ;
531531 const stopReason = terminalInterruption ?. reason ?? ( yielded ? "end_turn" : "completed" ) ;
532532
533533 if ( ! terminalInterruption ) {
@@ -587,7 +587,7 @@ export function buildCliRunResult(params: {
587587 model : context . modelId ,
588588 result : terminalInterruption ?. reason ?? "success" ,
589589 ...( terminalInterruption
590- ? { reason : `CLI turn ${ terminalInterruption . reason } after partial output` }
590+ ? { reason : formatCliTerminalInterruption ( terminalInterruption ) }
591591 : { } ) ,
592592 } ,
593593 ] ,
@@ -644,9 +644,7 @@ export function buildCliRunResult(params: {
644644 } ,
645645 }
646646 : { } ) ,
647- ...( terminalInterruption || sessionBindingDisabled || unflushedCliSessionId
648- ? { clearCliSessionBinding : true }
649- : { } ) ,
647+ ...( cliSessionBindingCleared ? { clearCliSessionBinding : true } : { } ) ,
650648 } ,
651649 } ,
652650 ...( output . didSendViaMessagingTool ? { didSendViaMessagingTool : true } : { } ) ,
0 commit comments