Skip to content

Commit 5ea367d

Browse files
fix(session): remember a refused encrypted-reasoning replay for the session
After the first commit, a provider that refused a session's encrypted reasoning cost one extra fast 400 on every later step of that session, since each step replayed the stored reasoning again. Once a caller is refused for a session, it is refused on every later step, so the refusal is now remembered: the processor layer holds a process-local set keyed by session id + provider id, armed only by an observed refusal (with one log line when it arms), and later steps of that session send the stripped history from their first request. Other sessions, and the same session with another provider, keep their encrypted reasoning. A server restart clears the set; the next refusal arms it again. The stored history is still not rewritten. Red first, accuracy-loop.test.ts: - "after one refusal, the session's next steps go without encrypted reasoning from the first request": 3 requests over two turns, and the third carries no encrypted reasoning. Failed before (the third replayed it). - "a refusal in one session does not strip another session's encrypted reasoning": passed before and after, pins the key. All five replay cases pass; 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 bab58a7 commit 5ea367d

2 files changed

Lines changed: 85 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ const layer = Layer.effect(
105105
const image = yield* Image.Service
106106
const events = yield* EventV2Bridge.Service
107107
const database = yield* Database.Service
108+
// Sessions whose encrypted reasoning a provider refused (session id + provider
109+
// id). Process-local: a restart clears it and the next refusal arms it again.
110+
const refusedReplays = new Set<string>()
108111

109112
const create = Effect.fn("SessionProcessor.create")(function* (input: Input) {
110113
// Pre-capture snapshot before the LLM stream starts. The AI SDK
@@ -695,20 +698,33 @@ const layer = Layer.effect(
695698
)
696699
})
697700

701+
// Once this provider has refused this session's encrypted reasoning, it
702+
// refuses it on every later step too, so those steps go without it from
703+
// the first request instead of paying a refusal each.
704+
const refusalKey = `${input.sessionID}\u0000${input.model.providerID}`
705+
const request = refusedReplays.has(refusalKey)
706+
? { ...streamInput, messages: ReasoningReplay.strip(streamInput.messages) }
707+
: streamInput
708+
698709
return yield* Effect.gen(function* () {
699-
yield* attempt(streamInput).pipe(
710+
yield* attempt(request).pipe(
700711
// A provider that did not issue the replayed encrypted reasoning refuses
701712
// the request with a non-retryable 400. Send it once more without that
702713
// reasoning; a second refusal is an ordinary error.
703714
Effect.catchIf(
704-
(e) => ReasoningReplay.rejected(parse(e)) && ReasoningReplay.carries(streamInput.messages),
715+
(e) => ReasoningReplay.rejected(parse(e)) && ReasoningReplay.carries(request.messages),
705716
() =>
706717
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) })
718+
refusedReplays.add(refusalKey)
719+
yield* Effect.logWarning(
720+
"provider refused replayed encrypted reasoning; this session now replays without it",
721+
{
722+
"session.id": input.sessionID,
723+
"provider.id": input.model.providerID,
724+
messageID: input.assistantMessage.id,
725+
},
726+
)
727+
yield* attempt({ ...request, messages: ReasoningReplay.strip(request.messages) })
712728
}),
713729
),
714730
Effect.onInterrupt(() =>

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,65 @@ it.instance(
605605
}),
606606
30_000,
607607
)
608+
609+
it.instance(
610+
"after one refusal, the session's next steps go without encrypted reasoning from the first request",
611+
() =>
612+
Effect.gen(function* () {
613+
const { llm } = yield* useResponsesConfig()
614+
const prompt = yield* SessionPrompt.Service
615+
const chat = yield* session()
616+
const model = yield* seedEncryptedReasoning(chat.id)
617+
yield* llm.error(400, rejectedReplay)
618+
yield* llm.text("recovered")
619+
yield* prompt.prompt({ sessionID: chat.id, agent: "build", model, parts: [{ type: "text", text: "continue" }] })
620+
expect(yield* llm.calls).toBe(2)
621+
622+
yield* llm.text("next turn")
623+
const next = yield* prompt.prompt({
624+
sessionID: chat.id,
625+
agent: "build",
626+
model,
627+
parts: [{ type: "text", text: "and then" }],
628+
})
629+
const hits = yield* llm.hits
630+
// one request for the next turn, not a refusal plus a retry
631+
expect(hits).toHaveLength(3)
632+
expect(JSON.stringify(hits[2]!.body)).not.toContain("gAAAA-foreign-caller")
633+
expect(next.parts.some((part) => part.type === "text" && part.text === "next turn")).toBe(true)
634+
}),
635+
30_000,
636+
)
637+
638+
it.instance(
639+
"a refusal in one session does not strip another session's encrypted reasoning",
640+
() =>
641+
Effect.gen(function* () {
642+
const { llm } = yield* useResponsesConfig()
643+
const prompt = yield* SessionPrompt.Service
644+
const first = yield* session()
645+
const firstModel = yield* seedEncryptedReasoning(first.id)
646+
yield* llm.error(400, rejectedReplay)
647+
yield* llm.text("recovered")
648+
yield* prompt.prompt({
649+
sessionID: first.id,
650+
agent: "build",
651+
model: firstModel,
652+
parts: [{ type: "text", text: "continue" }],
653+
})
654+
655+
const second = yield* session()
656+
const secondModel = yield* seedEncryptedReasoning(second.id)
657+
yield* llm.text("fine")
658+
yield* prompt.prompt({
659+
sessionID: second.id,
660+
agent: "build",
661+
model: secondModel,
662+
parts: [{ type: "text", text: "continue" }],
663+
})
664+
const hits = yield* llm.hits
665+
expect(hits).toHaveLength(3)
666+
expect(JSON.stringify(hits[2]!.body)).toContain("gAAAA-foreign-caller")
667+
}),
668+
30_000,
669+
)

0 commit comments

Comments
 (0)