Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/opencode/src/provider/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,50 @@ export class StreamInterruptedAfterToolCallError extends Error {
}
}

/** Fixed labels only: never serialize provider messages, stack traces or payloads. */
export function interruptedStreamCause(error: unknown): Record<string, string> {
try {
const kinds: string[] = []
const codes = new Set(["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "ENOTFOUND", "EAI_AGAIN"])
const seen = new Set<unknown>()
let code: string | undefined
let current = error
for (let depth = 0; depth < 4 && current !== undefined; depth++) {
if (seen.has(current)) {
kinds.push("cycle")
break
}
seen.add(current)
if (!(current instanceof Error)) {
kinds.push("unknown")
break
}
// Read data properties only. Accessors can throw or expose unrelated data.
const descriptor = Object.getOwnPropertyDescriptor(current, "code")
const value = descriptor && "value" in descriptor ? descriptor.value : undefined
const networkCode = typeof value === "string" && codes.has(value) ? value : undefined
kinds.push(
current instanceof HeaderTimeoutError
? "header_timeout"
: current instanceof ResponseStreamError
? "response_stream"
: networkCode
? "network"
: "unknown",
)
code ??= networkCode
const cause = Object.getOwnPropertyDescriptor(current, "cause")
current = cause && "value" in cause ? cause.value : undefined
if (depth === 3 && current !== undefined) kinds.push("truncated")
}
return { causeChain: kinds.join(">") || "unknown", ...(code ? { causeCode: code } : {}) }
} catch {
// Diagnostics must never replace the original interruption. Proxies can throw
// even from instanceof or descriptor inspection. Drop any partial enrichment.
return { causeChain: "unknown" }
}
}

function isOpenAiErrorRetryable(e: APICallError) {
const status = e.statusCode
if (!status) return e.isRetryable
Expand Down
1 change: 1 addition & 0 deletions packages/opencode/src/session/message-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ export function fromError(
isRetryable: false,
metadata: {
code: e.name,
...ProviderError.interruptedStreamCause(e.cause),
},
},
{ cause: e },
Expand Down
117 changes: 117 additions & 0 deletions packages/opencode/test/session/message-v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { APICallError } from "ai"
import * as ProviderError from "../../src/provider/error"
import { MessageV2 } from "../../src/session/message-v2"
import { ProviderTransform } from "@/provider/transform"
import type { Provider } from "@/provider/provider"
Expand Down Expand Up @@ -1426,6 +1427,122 @@ describe("session.message-v2.toModelMessage", () => {
})

describe("session.message-v2.fromError", () => {
test("persists only safe interruption cause labels through serialization", () => {
const secret = "https://private.invalid/?token=SECRET response=PRIVATE auth=BEARER"
const network = Object.assign(new Error(secret), { code: "ECONNRESET", response: secret })
const input = new ProviderError.StreamInterruptedAfterToolCallError("Interrupted", {
cause: new ProviderError.ResponseStreamError(secret, { cause: network }),
})
const result = JSON.parse(JSON.stringify(MessageV2.fromError(input, { providerID })))
expect(result.data.metadata).toEqual({
code: "ProviderStreamInterruptedAfterToolCallError",
causeChain: "response_stream>network",
causeCode: "ECONNRESET",
})
expect(result.data.isRetryable).toBe(false)
expect(JSON.stringify(result)).not.toContain("SECRET")
expect(JSON.stringify(result)).not.toContain("PRIVATE")
expect(JSON.stringify(result)).not.toContain("stack")
})

test("bounds unknown, cyclic and deep interruption causes without invoking accessors", () => {
const serialize = (cause: unknown) =>
JSON.parse(
JSON.stringify(
MessageV2.fromError(new ProviderError.StreamInterruptedAfterToolCallError("Interrupted", { cause }), {
providerID,
}),
),
).data.metadata
expect(serialize("SECRET").causeChain).toBe("unknown")
expect(serialize(undefined).causeChain).toBe("unknown")
const cycle = new Error("SECRET")
cycle.cause = cycle
expect(serialize(cycle).causeChain).toBe("unknown>cycle")
let deep: Error = new Error("SECRET")
for (let i = 0; i < 10; i++) deep = new Error("SECRET", { cause: deep })
expect(serialize(deep).causeChain).toBe("unknown>unknown>unknown>unknown>truncated")
const accessor = new Error("SECRET")
Object.defineProperties(accessor, {
code: {
get() {
throw new Error("must not read code")
},
},
cause: {
get() {
throw new Error("must not read cause")
},
},
})
expect(serialize(accessor).causeChain).toBe("unknown")
expect(serialize(Object.assign(new Error("SECRET"), { code: "PRIVATE" }))).not.toHaveProperty("causeCode")
expect(serialize(new ProviderError.HeaderTimeoutError(1200)).causeChain).toBe("header_timeout")
expect(serialize(Object.assign(new Error("SECRET"), { name: "ProviderResponseStreamError" })).causeChain).toBe(
"unknown",
)
for (const code of ["ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "ENOTFOUND", "EAI_AGAIN"]) {
expect(serialize(Object.assign(new Error("SECRET"), { code }))).toEqual({
code: "ProviderStreamInterruptedAfterToolCallError",
causeChain: "network",
causeCode: code,
})
}
})

test("contains failed introspection and denies inherited or arbitrary cause data", () => {
const serialize = (cause: unknown) =>
JSON.parse(
JSON.stringify(
MessageV2.fromError(new ProviderError.StreamInterruptedAfterToolCallError("Interrupted", { cause }), {
providerID,
}),
),
)
const expected = {
name: "APIError",
data: {
message: "Interrupted",
isRetryable: false,
metadata: { code: "ProviderStreamInterruptedAfterToolCallError", causeChain: "unknown" },
},
}
const descriptorProxy = new Proxy(new Error("SECRET"), {
getOwnPropertyDescriptor() {
throw new Error("SECRET")
},
})
const prototypeProxy = new Proxy(new Error("SECRET"), {
getPrototypeOf() {
throw new Error("SECRET")
},
})
const revoked = Proxy.revocable(new Error("SECRET"), {})
revoked.revoke()
const inherited = Object.create(
Object.assign(new Error("SECRET"), {
code: "ECONNRESET",
cause: new ProviderError.ResponseStreamError("SECRET"),
}),
)
for (const cause of [descriptorProxy, prototypeProxy, revoked.proxy, inherited]) {
expect(serialize(cause)).toEqual(expected)
}
for (const code of ["PRIVATE", "https://private.invalid", "Bearer SECRET", "ECONNRESET SECRET", "", "econnreset"]) {
const cause = Object.assign(new Error("SECRET"), {
code,
stack: "STACK_SECRET",
response: "RESPONSE_SECRET",
responseBody: "BODY_SECRET",
url: "https://private.invalid",
headers: { authorization: "Bearer SECRET" },
})
expect(serialize(cause)).toEqual(expected)
}
// A trap deeper in an otherwise classifiable chain must also be contained.
expect(serialize(new ProviderError.ResponseStreamError("SECRET", { cause: descriptorProxy }))).toEqual(expected)
})

test("serializes context_length_exceeded as ContextOverflowError", () => {
const input = {
type: "error",
Expand Down
Loading