Skip to content

Commit ca9d90a

Browse files
anandgupta42claude
andcommitted
fix(analyst): recover the echo only from transient transport failures; never persist a transient fatal error
- Recovery is structural, not phrase-based: an echo failure is recoverable only when it is an `APIError` with HTTP 5xx, 429 or 408, or a statusless network/stream failure (connection reset, socket timeout, idle watchdog, SDK retryable request failure). Any 4xx, any structured policy, content, safety or moderation code in the body, `ProviderAuthError`, and local errors raised before the provider request stay fatal. Error normalisation preserves the structured code ahead of body truncation and keeps a policy decision with overflow-looking text fatal. - A context overflow on the echo step after a completed commit is recoverable because the committed text already exists: the processor records the cause as `metadata.answer_commit_echo_error` instead of setting `error` or publishing `session.error`, and the loop publishes the committed text directly. This fixes the headless `--max-turns 1` scenario where the overflow was published as a fatal event before the recovery guard ran and the run exited 1. - The `--max-turns` exemption is cleared at every distinct persisted real-request boundary, including a request submitted by another attached client, and the step-start path never awaits a session fetch. - `rows.precision` with duplicate column labels is rejected; duplicates without precision render unchanged. - Tests: 503 and socket reset recover without fatal events; 400 structured policy denial and 403 stay fatal; local pre-request failure stays fatal; overflow on the echo publishes the committed text without fatal events; policy code beyond the body limit survives normalisation; over-budget step aborts without any session fetch; new attached-session request clears the exemption; `--max-turns 1` publishes the committed output after an echo overflow; unique column labels required for precision. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent db33d1c commit ca9d90a

9 files changed

Lines changed: 567 additions & 70 deletions

File tree

‎packages/opencode/src/altimate/tools/answer-commit.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ const rows = z
6767
})
6868
.strict()
6969
.superRefine((rows, ctx) => {
70+
if (rows.precision !== undefined && new Set(rows.columns).size !== rows.columns.length) {
71+
ctx.addIssue({
72+
code: "custom",
73+
path: ["precision"],
74+
message: "rows.precision requires unique column labels",
75+
})
76+
}
7077
const unknown = Object.keys(rows.precision ?? {}).filter((key) => !rows.columns.includes(key))
7178
if (unknown.length) {
7279
ctx.addIssue({

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,15 @@ You are speaking to a non-technical business executive. Follow these rules stric
727727
const accounting = RunAccounting.create()
728728
let committedAnswer = false
729729
let activeRequestID: string | undefined
730+
const assistantRequests = new Map<string, string | undefined>()
731+
const userRequests = new Map<string, string | undefined>()
732+
const seenRequests = new Set<string>()
730733
const beginRequest = (messageID: string) => {
731-
// Retries retain the request; compaction rows never emit a request-start event.
732-
if (activeRequestID && messageID <= activeRequestID) return
734+
// The persisted request boundary includes requests from other attached clients.
735+
// Retries retain their ID; internal synthetic/compaction rows do not emit this event.
736+
if (seenRequests.has(messageID)) return
737+
seenRequests.add(messageID)
738+
userRequests.set(messageID, messageID)
733739
activeRequestID = messageID
734740
committedAnswer = false
735741
}
@@ -976,12 +982,27 @@ You are speaking to a non-technical business executive. Follow these rules stric
976982
// step-start parts (which carry only messageID/sessionID) can be attributed.
977983
// The assistant message row is persisted — and this event published — before
978984
// its first step-start part streams, so the lookup is populated in time.
985+
if (
986+
event.type === "message.updated" &&
987+
event.properties.info.role === "user" &&
988+
event.properties.info.sessionID === sessionID &&
989+
!userRequests.has(event.properties.info.id)
990+
) {
991+
// Compaction-created parent rows inherit the current request; a real
992+
// request's subsequent persisted boundary replaces this with its own ID.
993+
userRequests.set(event.properties.info.id, activeRequestID)
994+
}
979995
if (
980996
event.type === "message.updated" &&
981997
event.properties.info.role === "assistant" &&
982998
event.properties.info.sessionID === sessionID
983999
) {
9841000
accounting.onAssistantMessage(event.properties.info)
1001+
// Keep the request that owned the assistant's creation, even if an older
1002+
// tool finishes after another attached client persists a new request.
1003+
if (!assistantRequests.has(event.properties.info.id)) {
1004+
assistantRequests.set(event.properties.info.id, userRequests.get(event.properties.info.parentID))
1005+
}
9851006
assistantStarted = true
9861007
// Enrich only from this run's assistant, independently of presentation.
9871008
const info = event.properties.info
@@ -1016,9 +1037,9 @@ You are speaking to a non-technical business executive. Follow these rules stric
10161037
if (part.type === "tool" && !accounting.isCompactionStep(part.messageID)) lastToolStep = step
10171038
if (
10181039
part.type === "tool" && part.tool === "answer_commit" && part.state.status === "completed" &&
1019-
activeRequestID !== undefined && part.messageID > activeRequestID
1040+
activeRequestID !== undefined && assistantRequests.get(part.messageID) === activeRequestID
10201041
) {
1021-
// A tool from an interrupted older request may finish after its replacement was sent.
1042+
// Only this request's commit can exempt its echo from the work-turn limit.
10221043
committedAnswer = true
10231044
}
10241045
// altimate_change end

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,53 @@ export namespace MessageV2 {
519519
})
520520
export type Assistant = z.infer<typeof Assistant>
521521

522-
// altimate_change start — only provider/transport failures permit committed-answer echo recovery
522+
// altimate_change start — recover committed echoes only from transient transport failures
523+
const echoNetworkCodes = new Set([
524+
"ECONNRESET",
525+
"ECONNREFUSED",
526+
"ETIMEDOUT",
527+
"ESOCKETTIMEDOUT",
528+
"EPIPE",
529+
"ENOTFOUND",
530+
"EAI_AGAIN",
531+
"UND_ERR_SOCKET",
532+
"UND_ERR_CONNECT_TIMEOUT",
533+
"UND_ERR_HEADERS_TIMEOUT",
534+
"UND_ERR_BODY_TIMEOUT",
535+
])
536+
537+
function echoPolicyCode(responseBody: string | undefined): string | undefined {
538+
if (!responseBody) return undefined
539+
const find = (value: unknown): string | undefined => {
540+
if (!value || typeof value !== "object") return undefined
541+
for (const [key, child] of Object.entries(value)) {
542+
if (key === "code" && typeof child === "string" && /policy|content|safety|moderation/i.test(child)) return child
543+
const nested = find(child)
544+
if (nested) return nested
545+
}
546+
}
547+
try {
548+
return find(JSON.parse(responseBody))
549+
} catch {
550+
return undefined
551+
}
552+
}
553+
523554
export function isEchoTransportError(error: Assistant["error"]): boolean {
524-
if (error?.name !== "APIError" && error?.name !== "UnknownError" && error?.name !== "ProviderAuthError") return false
525-
// HTTP policy rejections can arrive as APIError rather than a content-filter finish.
526-
const detail = error.data.message + (error.name === "APIError" ? `\n${error.data.responseBody ?? ""}` : "")
527-
return !/content[-_\s]?(?:filter|policy)|safety|guardrail[_\s-]intervened|ResponsibleAIPolicyViolation/i.test(detail)
555+
if (error?.name !== "APIError") return false
556+
const { statusCode, responseBody, metadata, isRetryable } = error.data
557+
// Provider decisions can be wrapped by a gateway with a transient HTTP status.
558+
// Inspect structured codes, never prose in a message or response body.
559+
if (echoPolicyCode(responseBody) || /policy|content|safety|moderation/i.test(metadata?.code ?? "")) return false
560+
if (statusCode !== undefined)
561+
return (statusCode >= 500 && statusCode < 600) || statusCode === 429 || statusCode === 408
562+
const code = metadata?.code
563+
return (
564+
(code !== undefined &&
565+
(echoNetworkCodes.has(code) ||
566+
["ZlibError", "ProviderHeaderTimeoutError", "ProviderStreamIdleTimeoutError"].includes(code))) ||
567+
(isRetryable && metadata?.url !== undefined && !responseBody)
568+
)
528569
}
529570
// altimate_change end
530571
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
@@ -1154,10 +1195,14 @@ export namespace MessageV2 {
11541195
},
11551196
{ cause: e },
11561197
).toObject()
1157-
case (e as SystemError)?.code === "ECONNRESET":
1198+
// altimate_change start — retain structured network codes for echo recovery
1199+
case echoNetworkCodes.has((e as SystemError)?.code ?? ""):
1200+
// altimate_change end
11581201
return new MessageV2.APIError(
11591202
{
1160-
message: "Connection reset by server",
1203+
// altimate_change start — preserve the established reset message and other transport details
1204+
message: (e as SystemError).code === "ECONNRESET" ? "Connection reset by server" : errorMessage(e),
1205+
// altimate_change end
11611206
isRetryable: true,
11621207
metadata: {
11631208
code: (e as SystemError).code ?? "",
@@ -1213,6 +1258,22 @@ export namespace MessageV2 {
12131258
providerID: ctx.providerID,
12141259
error: e,
12151260
})
1261+
// altimate_change start — retain policy decisions before response truncation or overflow detection can hide them
1262+
const policyCode = echoPolicyCode(e.responseBody)
1263+
if (policyCode) {
1264+
return new MessageV2.APIError(
1265+
{
1266+
message: parsed.message,
1267+
statusCode: e.statusCode,
1268+
isRetryable: parsed.type === "api_error" ? parsed.isRetryable : e.isRetryable,
1269+
responseHeaders: parsed.type === "api_error" ? parsed.responseHeaders : e.responseHeaders,
1270+
responseBody: parsed.responseBody,
1271+
metadata: { ...(parsed.type === "api_error" ? parsed.metadata : undefined), code: policyCode },
1272+
},
1273+
{ cause: e },
1274+
).toObject()
1275+
}
1276+
// altimate_change end
12161277
if (parsed.type === "context_overflow") {
12171278
return new MessageV2.ContextOverflowError(
12181279
{
@@ -1230,7 +1291,11 @@ export namespace MessageV2 {
12301291
isRetryable: parsed.isRetryable,
12311292
responseHeaders: parsed.responseHeaders,
12321293
responseBody: parsed.responseBody,
1233-
metadata: parsed.metadata,
1294+
// altimate_change start — SDK-wrapped socket failures retain their structural transport code
1295+
metadata: echoNetworkCodes.has((e.cause as SystemError)?.code ?? "")
1296+
? { ...parsed.metadata, code: (e.cause as SystemError).code ?? "" }
1297+
: parsed.metadata,
1298+
// altimate_change end
12341299
},
12351300
{ cause: e },
12361301
).toObject()

‎packages/opencode/src/session/processor.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,19 @@ export namespace SessionProcessor {
12611261
// altimate_change end
12621262
})
12631263
const error = MessageV2.fromError(e, { providerID: input.model.providerID })
1264-
if (MessageV2.ContextOverflowError.isInstance(error)) {
1264+
// altimate_change start — a buffered echo already has its answer; persist the cause without a fatal event
1265+
if (
1266+
deferText &&
1267+
!input.abort.aborted &&
1268+
input.assistantMessage.finish !== "content-filter" &&
1269+
(MessageV2.isEchoTransportError(error) || MessageV2.ContextOverflowError.isInstance(error))
1270+
) {
1271+
input.assistantMessage.metadata = {
1272+
...input.assistantMessage.metadata,
1273+
answer_commit_echo_error: error,
1274+
}
1275+
} else if (MessageV2.ContextOverflowError.isInstance(error)) {
1276+
// altimate_change end
12651277
if ((await Config.get()).compaction?.auto === false && !input.assistantMessage.summary) {
12661278
// altimate_change start — upstream_fix: honor
12671279
// compaction.auto=false on reactive provider overflow.
@@ -1309,25 +1321,15 @@ export namespace SessionProcessor {
13091321
})
13101322
}
13111323
// altimate_change end
1312-
input.assistantMessage.error = error
13131324
// altimate_change start — order terminal error before idle publication
13141325
// Publish the error before idle. The run harness drains this
13151326
// generation through idle before opening a challenge stream; an
13161327
// unawaited publish can otherwise arrive on that fresh stream.
1317-
// The prompt loop recovers a buffered committed-answer echo and records
1318-
// its transport error as message metadata before publishing the stored text.
1319-
// Publishing a fatal event here would permanently fail headless run accounting.
1320-
if (
1321-
!deferText ||
1322-
input.abort.aborted ||
1323-
input.assistantMessage.finish === "content-filter" ||
1324-
!MessageV2.isEchoTransportError(error)
1325-
) {
1326-
await Bus.publish(Session.Event.Error, {
1327-
sessionID: input.assistantMessage.sessionID,
1328-
error: input.assistantMessage.error,
1329-
})
1330-
}
1328+
input.assistantMessage.error = error
1329+
await Bus.publish(Session.Event.Error, {
1330+
sessionID: input.assistantMessage.sessionID,
1331+
error: input.assistantMessage.error,
1332+
})
13311333
// altimate_change end
13321334
// altimate_change start — telemetry for unhandled streaming errors (non-retry, non-overflow)
13331335
// Covers: MessageAbortedError (Stop/dispose), UnknownError (SSE chunk timeout),

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ export namespace SessionPrompt {
903903
for (const old of storedText) {
904904
if (old.id !== part.id) await Session.removePart({ sessionID, messageID: message.id, partID: old.id })
905905
}
906-
const cause = error ?? (message.finish !== "stop" ? { finish: message.finish ?? "unknown" } : undefined)
906+
const cause =
907+
error ?? message.metadata?.answer_commit_echo_error ??
908+
(message.finish !== "stop" ? { finish: message.finish ?? "unknown" } : undefined)
907909
if (cause) message.metadata = { ...message.metadata, answer_commit_echo_error: cause }
908910
delete message.error
909911
// A failed summariser becomes the exiting answer, so clients must no longer hide it as compaction machinery.

0 commit comments

Comments
 (0)