Skip to content

Commit bab58a7

Browse files
fix(session): retry once without encrypted reasoning when the provider refuses it
A Responses provider only accepts replayed encrypted reasoning that it issued to the same caller. When the caller behind a gateway changes (another key, account or org), every request of the session gets a non-retryable 400, "reasoning encrypted_content was not issued to this caller", and the session is dead for good. This stopped Muses for hours this week. On that exact error, the processor sends the step once more with the encrypted reasoning and the reasoning item ids removed from the replayed history. A second refusal is an ordinary error and ends the turn with it; the retry happens only when the history actually carries encrypted reasoning, so it cannot loop. Nothing is stripped up front: reasoning the provider accepts is replayed unchanged, and the stored history is not rewritten. - session/reasoning-replay.ts (pure): `rejected` (only a 400 whose message or body carries the refusal), `strip` (removes reasoningEncryptedContent and itemId from reasoning parts under any provider key, nothing else) and `carries`. - processor.ts: one catch around the stream attempt, ahead of the existing retry policy and halt. Red first: reasoning-replay.test.ts (7) before the module existed, with the recorded 400 body; accuracy-loop.test.ts gains three integration cases on an @ai-sdk/openai Responses provider against the test server. Two of them failed before the fix (1 request instead of 2); the case where the encrypted reasoning is accepted passed before and after. After the fix: the first request carries the encrypted reasoning, the retry carries neither it nor its item id, the turn completes; two refusals give 2 requests and an APIError. Also seen: "no runaway reminder when the guard is off" and "no todo continuation when the flag is off" time out at 20s intermittently on this branch and on dev (445456e) alike, interleaved runs; contention, not this change. Typecheck clean. Ref anomalyco#44, LDE-35 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHX6CpBR14wnFxJLDahwTr
1 parent 445456e commit bab58a7

4 files changed

Lines changed: 368 additions & 4 deletions

File tree

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { isNearOverflow, isOverflow } from "./overflow"
1616
import { PartID } from "./schema"
1717
import type { SessionID } from "./schema"
1818
import { SessionRetry } from "./retry"
19+
import { ReasoningReplay } from "./reasoning-replay"
1920
import { SessionStatus } from "./status"
2021
import { SessionSummary } from "./summary"
2122
import type { Provider } from "@/provider/provider"
@@ -680,19 +681,36 @@ const layer = Layer.effect(
680681
reminder = undefined
681682
ctx.shouldBreak = (yield* config.get()).experimental?.continue_loop_on_deny !== true
682683

683-
return yield* Effect.gen(function* () {
684-
yield* Effect.gen(function* () {
684+
const attempt = (request: LLM.StreamInput) =>
685+
Effect.gen(function* () {
685686
ctx.currentText = undefined
686687
ctx.reasoningMap = {}
687688
yield* status.set(ctx.sessionID, { type: "busy" })
688-
const stream = llm.stream(streamInput)
689+
const stream = llm.stream(request)
689690

690691
yield* stream.pipe(
691692
Stream.tap((event) => handleEvent(event)),
692693
Stream.takeUntil(() => ctx.needsCompaction),
693694
Stream.runDrain,
694695
)
695-
}).pipe(
696+
})
697+
698+
return yield* Effect.gen(function* () {
699+
yield* attempt(streamInput).pipe(
700+
// A provider that did not issue the replayed encrypted reasoning refuses
701+
// the request with a non-retryable 400. Send it once more without that
702+
// reasoning; a second refusal is an ordinary error.
703+
Effect.catchIf(
704+
(e) => ReasoningReplay.rejected(parse(e)) && ReasoningReplay.carries(streamInput.messages),
705+
() =>
706+
Effect.gen(function* () {
707+
yield* Effect.logWarning("provider refused replayed encrypted reasoning; retrying without it", {
708+
"session.id": input.sessionID,
709+
messageID: input.assistantMessage.id,
710+
})
711+
yield* attempt({ ...streamInput, messages: ReasoningReplay.strip(streamInput.messages) })
712+
}),
713+
),
696714
Effect.onInterrupt(() =>
697715
Effect.gen(function* () {
698716
aborted = true
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Recovery for a provider that refuses replayed encrypted reasoning.
2+
//
3+
// Responses providers return encrypted reasoning with each turn, and the harness
4+
// replays it on the next request so the model keeps its chain of thought. The
5+
// provider only accepts encrypted reasoning it issued to the same caller. When the
6+
// caller changes (another key, account or org behind a gateway), it answers every
7+
// request of the session with a non-retryable 400, "reasoning encrypted_content
8+
// was not issued to this caller", and the session is dead.
9+
//
10+
// On that exact error the request is sent once more without the encrypted
11+
// reasoning and the reasoning item ids tied to it. It is never stripped up front:
12+
// encrypted reasoning the provider accepts is kept.
13+
//
14+
// Pure, so the rule is unit-tested and ports as-is.
15+
16+
import type { ModelMessage } from "ai"
17+
import type { NamedError } from "@opencode-ai/core/util/error"
18+
import { SessionV1 } from "@opencode-ai/core/v1/session"
19+
20+
type Err = ReturnType<NamedError["toObject"]>
21+
22+
const REJECTED = /encrypted_content was not issued to this caller/i
23+
24+
/** True for the provider's refusal of replayed encrypted reasoning, and nothing else. */
25+
export function rejected(error: Err) {
26+
if (!SessionV1.APIError.isInstance(error)) return false
27+
if (error.data.statusCode !== 400) return false
28+
return REJECTED.test(error.data.message) || REJECTED.test(error.data.responseBody ?? "")
29+
}
30+
31+
/** The same messages with encrypted reasoning and reasoning item ids removed from reasoning parts. */
32+
export function strip(messages: ModelMessage[]): ModelMessage[] {
33+
return messages.map((message) => {
34+
if (message.role !== "assistant" || !Array.isArray(message.content)) return message
35+
return {
36+
...message,
37+
content: message.content.map((part) =>
38+
part.type === "reasoning" && part.providerOptions
39+
? { ...part, providerOptions: withoutEncrypted(part.providerOptions) }
40+
: part,
41+
),
42+
}
43+
})
44+
}
45+
46+
/** Whether any reasoning part still carries encrypted reasoning, so a retry would change the request. */
47+
export function carries(messages: ModelMessage[]) {
48+
return messages.some(
49+
(message) =>
50+
message.role === "assistant" &&
51+
Array.isArray(message.content) &&
52+
message.content.some(
53+
(part) =>
54+
part.type === "reasoning" &&
55+
Object.values(part.providerOptions ?? {}).some(
56+
(options) => options !== null && typeof options === "object" && "reasoningEncryptedContent" in options,
57+
),
58+
),
59+
)
60+
}
61+
62+
function withoutEncrypted(options: NonNullable<ModelMessage["providerOptions"]>) {
63+
return Object.fromEntries(
64+
Object.entries(options).map(([provider, value]) => {
65+
if (value === null || typeof value !== "object") return [provider, value]
66+
const { reasoningEncryptedContent: _content, itemId: _id, ...rest } = value as Record<string, unknown>
67+
return [provider, rest]
68+
}),
69+
) as NonNullable<ModelMessage["providerOptions"]>
70+
}
71+
72+
export * as ReasoningReplay from "./reasoning-replay"

‎packages/opencode/test/session/accuracy-loop.test.ts‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
22
import { Database } from "@opencode-ai/core/database/database"
33
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
44
import { SessionProjector } from "@opencode-ai/core/session/projector"
5+
import { MessageID, PartID } from "../../src/session/schema"
56
import { expect } from "bun:test"
67
import { Effect, Layer } from "effect"
78
import path from "path"
@@ -440,3 +441,167 @@ it.instance(
440441
}),
441442
20000,
442443
)
444+
445+
// Reliability: a provider that refuses replayed encrypted reasoning (#44)
446+
447+
/** A Responses-API provider on the test server, so encrypted reasoning is serialized into the request. */
448+
const useResponsesConfig = Effect.fn("test.useResponsesConfig")(function* () {
449+
const { directory } = yield* TestInstance
450+
const llm = yield* TestLLMServer
451+
const fs = yield* FSUtil.Service
452+
const config = {
453+
provider: {
454+
oa: {
455+
name: "Responses",
456+
id: "oa",
457+
env: [],
458+
npm: "@ai-sdk/openai",
459+
models: {
460+
"oa-model": {
461+
...provider.test.models["test-model"],
462+
id: "oa-model",
463+
name: "Responses Model",
464+
reasoning: true,
465+
},
466+
},
467+
options: { apiKey: "test-key", baseURL: llm.url },
468+
},
469+
},
470+
}
471+
yield* fs.writeWithDirs(
472+
path.join(directory, "opencode.json"),
473+
JSON.stringify({ $schema: "https://opencode.ai/config.json", ...config }),
474+
)
475+
return { llm }
476+
})
477+
478+
/** A finished earlier turn whose reasoning carries encrypted content another caller was issued. */
479+
const seedEncryptedReasoning = Effect.fn("test.seedEncryptedReasoning")(function* (sessionID: Session.Info["id"]) {
480+
const sessions = yield* Session.Service
481+
const { directory } = yield* TestInstance
482+
const model = { providerID: ProviderV2.ID.make("oa"), modelID: ModelV2.ID.make("oa-model") }
483+
const user = yield* sessions.updateMessage({
484+
id: MessageID.ascending(),
485+
role: "user",
486+
sessionID,
487+
agent: "build",
488+
model,
489+
time: { created: Date.now() },
490+
})
491+
yield* sessions.updatePart({
492+
id: PartID.ascending(),
493+
messageID: user.id,
494+
sessionID,
495+
type: "text",
496+
text: "earlier question",
497+
})
498+
const assistant = yield* sessions.updateMessage({
499+
id: MessageID.ascending(),
500+
role: "assistant",
501+
sessionID,
502+
parentID: user.id,
503+
mode: "build",
504+
agent: "build",
505+
path: { cwd: directory, root: directory },
506+
cost: 0,
507+
tokens: { input: 1, output: 1, reasoning: 0, cache: { read: 0, write: 0 } },
508+
modelID: model.modelID,
509+
providerID: model.providerID,
510+
finish: "stop",
511+
time: { created: Date.now(), completed: Date.now() },
512+
})
513+
yield* sessions.updatePart({
514+
id: PartID.ascending(),
515+
messageID: assistant.id,
516+
sessionID,
517+
type: "reasoning",
518+
text: "thinking about it",
519+
metadata: { openai: { itemId: "rs_foreign", reasoningEncryptedContent: "gAAAA-foreign-caller" } },
520+
time: { start: Date.now(), end: Date.now() },
521+
})
522+
yield* sessions.updatePart({
523+
id: PartID.ascending(),
524+
messageID: assistant.id,
525+
sessionID,
526+
type: "text",
527+
text: "earlier answer",
528+
})
529+
return model
530+
})
531+
532+
const rejectedReplay = {
533+
error: {
534+
message: "reasoning encrypted_content was not issued to this caller",
535+
type: "invalid_request_error",
536+
param: "input",
537+
code: null,
538+
},
539+
}
540+
541+
it.instance(
542+
"a refused encrypted-reasoning replay is retried once without it, and the turn completes",
543+
() =>
544+
Effect.gen(function* () {
545+
const { llm } = yield* useResponsesConfig()
546+
const prompt = yield* SessionPrompt.Service
547+
const chat = yield* session()
548+
const model = yield* seedEncryptedReasoning(chat.id)
549+
yield* llm.error(400, rejectedReplay)
550+
yield* llm.text("recovered")
551+
const result = yield* prompt.prompt({
552+
sessionID: chat.id,
553+
agent: "build",
554+
model,
555+
parts: [{ type: "text", text: "continue" }],
556+
})
557+
const hits = yield* llm.hits
558+
expect(hits).toHaveLength(2)
559+
// the first request replayed the encrypted reasoning as it was stored
560+
expect(JSON.stringify(hits[0]!.body)).toContain("gAAAA-foreign-caller")
561+
// the retry carried neither the encrypted reasoning nor its item id
562+
expect(JSON.stringify(hits[1]!.body)).not.toContain("gAAAA-foreign-caller")
563+
expect(JSON.stringify(hits[1]!.body)).not.toContain("rs_foreign")
564+
expect(result.info.role === "assistant" && result.info.error).toBeFalsy()
565+
expect(result.parts.some((part) => part.type === "text" && part.text === "recovered")).toBe(true)
566+
}),
567+
30_000,
568+
)
569+
570+
it.instance(
571+
"encrypted reasoning the provider accepts is replayed unchanged",
572+
() =>
573+
Effect.gen(function* () {
574+
const { llm } = yield* useResponsesConfig()
575+
const prompt = yield* SessionPrompt.Service
576+
const chat = yield* session()
577+
const model = yield* seedEncryptedReasoning(chat.id)
578+
yield* llm.text("fine")
579+
yield* prompt.prompt({ sessionID: chat.id, agent: "build", model, parts: [{ type: "text", text: "continue" }] })
580+
const hits = yield* llm.hits
581+
expect(hits).toHaveLength(1)
582+
expect(JSON.stringify(hits[0]!.body)).toContain("gAAAA-foreign-caller")
583+
}),
584+
30_000,
585+
)
586+
587+
it.instance(
588+
"a second refusal after the stripped retry ends the turn with the error, not a loop",
589+
() =>
590+
Effect.gen(function* () {
591+
const { llm } = yield* useResponsesConfig()
592+
const prompt = yield* SessionPrompt.Service
593+
const chat = yield* session()
594+
const model = yield* seedEncryptedReasoning(chat.id)
595+
yield* llm.error(400, rejectedReplay)
596+
yield* llm.error(400, rejectedReplay)
597+
const result = yield* prompt.prompt({
598+
sessionID: chat.id,
599+
agent: "build",
600+
model,
601+
parts: [{ type: "text", text: "continue" }],
602+
})
603+
expect(yield* llm.calls).toBe(2)
604+
expect(result.info.role === "assistant" && result.info.error?.name).toBe("APIError")
605+
}),
606+
30_000,
607+
)

0 commit comments

Comments
 (0)