@@ -104,6 +104,7 @@ import type { EffectiveSubagentRoster, SpawnAgentSurface } from "../../codex/cat
104104import { buildToolBridgeMaps , collabSurface , injectDeveloperMessage , multiAgentGuidanceText } from "./collaboration" ;
105105import { hasUnreadableEncryptedAgentTask , looksLikeBackendCiphertext , sanitizeEncryptedContentInPlace } from "./encrypted-payload" ;
106106import { fetchWithHeaderTimeout , providerFetch , safeHostLabel } from "./fetch-helpers" ;
107+ import { guardTerminalEventStream } from "./terminal-guard" ;
107108
108109/**
109110 * Adapters whose continuation state must survive Codex's store:false requests.
@@ -1620,8 +1621,124 @@ export async function handleResponses(
16201621 }
16211622 }
16221623
1624+ // Claude can return a clean end_turn after announcing an edit without emitting any tool call.
1625+ // Keep the normal request/recovery path above intact, and use this bounded callback only for the
1626+ // one internal continuation pass. A continuation failure becomes an in-stream adapter error so
1627+ // the client never sees a second hidden HTTP response or an unbounded retry loop.
1628+ const terminalGuardEnabled = activeAdapter . name === "anthropic" && ! options . comboAttempt && ! routedCompaction ;
1629+ const fetchTerminalGuardContinuation = async function * ( nextParsed : OcxParsedRequest ) : AsyncGenerator < AdapterEvent > {
1630+ let imageTierBias = 0 ;
1631+ let response : Response | undefined ;
1632+ while ( true ) {
1633+ try {
1634+ const continuationRequest = await activeAdapter . buildRequest ( nextParsed , {
1635+ headers : selectedForwardHeaders ,
1636+ ...( imageTierBias > 0 ? { imageTierBias } : { } ) ,
1637+ } ) ;
1638+ const continuationEstimate = typeof continuationRequest . usageLog ?. inputTokens === "number"
1639+ ? continuationRequest . usageLog . inputTokens
1640+ : undefined ;
1641+ if ( continuationEstimate !== undefined ) logCtx . usageLogInputTokens = continuationEstimate ;
1642+ if ( activeAdapter . fetchResponse ) {
1643+ noteAttemptSend ( logCtx . activeAttempt , continuationEstimate ) ;
1644+ response = await activeAdapter . fetchResponse ( continuationRequest , {
1645+ abortSignal : upstream . signal ,
1646+ timeoutMs : connectMs ,
1647+ stream : nextParsed . stream ,
1648+ } ) ;
1649+ } else {
1650+ response = await fetchWithResetRetry (
1651+ recovery => {
1652+ noteAttemptSend ( logCtx . activeAttempt , continuationEstimate , recovery ) ;
1653+ return fetchWithHeaderTimeout (
1654+ continuationRequest . url ,
1655+ applyUpstreamRecoveryInit ( {
1656+ method : continuationRequest . method ,
1657+ headers : continuationRequest . headers ,
1658+ body : continuationRequest . body ,
1659+ } , recovery ) ,
1660+ upstream . signal ,
1661+ connectMs ,
1662+ nextParsed . stream ,
1663+ providerFetch ( route . provider ) ,
1664+ ) ;
1665+ } ,
1666+ { abortSignal : upstream . signal , label : safeHostLabel ( continuationRequest . url ) } ,
1667+ ) ;
1668+ }
1669+ } catch ( error ) {
1670+ if ( options . abortSignal ?. aborted ) {
1671+ yield { type : "error" , message : "client closed request during terminal continuation" , status : 499 } ;
1672+ } else {
1673+ yield { type : "error" , message : `Provider continuation failed: ${ error instanceof Error ? error . message : String ( error ) } ` } ;
1674+ }
1675+ return ;
1676+ }
1677+
1678+ if ( response . status === 429 && hasKeyPoolFailover ( route . provider ) ) {
1679+ const rotated = rotateProviderTransportOn429 ( config , route . providerName , {
1680+ retryAfter : response . headers . get ( "retry-after" ) ,
1681+ now : Date . now ( ) ,
1682+ attemptedKey : route . provider . apiKey ,
1683+ promptCacheKey : nextParsed . options . promptCacheKey ,
1684+ } ) ;
1685+ if ( rotated ) {
1686+ try { void response . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
1687+ route . provider = rotated ;
1688+ activeAdapter = resolveAdapter (
1689+ resolveWireProtocolOverride ( route . providerName , route . modelId , route . provider ) ,
1690+ config . cacheRetention ,
1691+ ) ;
1692+ continue ;
1693+ }
1694+ }
1695+ if ( shouldAttemptImageTierRetry ( {
1696+ status : response . status ,
1697+ adapterName : activeAdapter . name ,
1698+ parsed : nextParsed ,
1699+ alreadyAttempted : imageTierBias > 0 ,
1700+ } ) ) {
1701+ imageTierBias = 1 ;
1702+ try { void response . body ?. cancel ( ) . catch ( ( ) => { } ) ; } catch { /* already closed */ }
1703+ continue ;
1704+ }
1705+ break ;
1706+ }
1707+
1708+ if ( ! response . ok ) {
1709+ const errorText = await response . text ( ) . catch ( ( ) => "unknown error" ) ;
1710+ yield {
1711+ type : "error" ,
1712+ status : response . status ,
1713+ message : `Provider continuation error ${ response . status } : ${ redactSecretString ( errorText . slice ( 0 , 500 ) ) } ` ,
1714+ } ;
1715+ return ;
1716+ }
1717+
1718+ try {
1719+ if ( nextParsed . stream ) {
1720+ yield * activeAdapter . parseStream ( response ) ;
1721+ } else if ( activeAdapter . parseResponse ) {
1722+ yield * await activeAdapter . parseResponse ( response ) ;
1723+ } else {
1724+ yield { type : "error" , message : "Provider continuation does not support response parsing" } ;
1725+ }
1726+ } catch ( error ) {
1727+ yield { type : "error" , message : `Provider continuation parse failed: ${ error instanceof Error ? error . message : String ( error ) } ` } ;
1728+ }
1729+ } ;
1730+
16231731 if ( parsed . stream ) {
1624- const eventStream = activeAdapter . parseStream ( upstreamResponse ) ;
1732+ const initialEventStream = activeAdapter . parseStream ( upstreamResponse ) ;
1733+ const eventStream = terminalGuardEnabled
1734+ ? guardTerminalEventStream ( {
1735+ parsed,
1736+ firstEvents : initialEventStream ,
1737+ adapterName : activeAdapter . name ,
1738+ maxAutoContinuations : 1 ,
1739+ continuation : fetchTerminalGuardContinuation ,
1740+ } )
1741+ : initialEventStream ;
16251742 const { toolNsMap, freeformToolNames, toolSearchToolNames } = buildToolBridgeMaps ( parsed ) ;
16261743 const sseStream = bridgeToResponsesSSE (
16271744 eventStream , parsed . modelId , toolNsMap , freeformToolNames , toolSearchToolNames ,
@@ -1656,7 +1773,19 @@ export async function handleResponses(
16561773 if ( activeAdapter . parseResponse ) {
16571774 let events : AdapterEvent [ ] ;
16581775 try {
1659- events = await activeAdapter . parseResponse ( upstreamResponse ) ;
1776+ const initialEvents = await activeAdapter . parseResponse ( upstreamResponse ) ;
1777+ if ( terminalGuardEnabled ) {
1778+ events = [ ] ;
1779+ for await ( const event of guardTerminalEventStream ( {
1780+ parsed,
1781+ firstEvents : ( async function * ( ) { yield * initialEvents ; } ) ( ) ,
1782+ adapterName : activeAdapter . name ,
1783+ maxAutoContinuations : 1 ,
1784+ continuation : fetchTerminalGuardContinuation ,
1785+ } ) ) events . push ( event ) ;
1786+ } else {
1787+ events = initialEvents ;
1788+ }
16601789 } finally {
16611790 cleanupUpstreamAbort ( ) ;
16621791 }
0 commit comments