fix: persist Claude Agent SDK lifecycle across turns - #11
Open
mradwankhalil wants to merge 11 commits into
Open
Conversation
Author
|
Follow-up fix added after live validation. /compact was lowering OpenCode's local context correctly, but the next normal request resumed the old sticky Claude session, whose hidden pre-compaction context reported ~806k cached input tokens. b4d999 adds the regression; 858a34 clears the base conversation binding at a classified summary boundary so the next turn transfers compacted OpenCode history into a fresh Claude session. Focused regressions, typecheck, build, and diagnostics pass. The full suite still stops at the unchanged Windows smoke assertion in test/smoke.ts:236. |
This was referenced Sep 5, 2026
Introduce ClaudePromptInput, a long-lived AsyncIterable<SDKUserMessage> backed by an unbounded queue. Each push() resolves the next pending pull; close() releases any waiting consumer. This is the primitive the proxy uses to stream user input into a persistent Agent SDK Query.
Closing the prompt input tears the SDK stream down synchronously. Doing it inline meant every turn paid to close the previous turn's bridge before it could proceed, adding seconds to every request. Measured on Windows with Opus 5: ~11s for a trivial prompt, ~3s once deferred. The close still happens, so the stream leak this branch fixes stays fixed — it just runs on the next macrotask instead of while a request is waiting. handle.close() and removal from the pool remain synchronous. Adds test/bridge-teardown-regression.ts covering both teardown paths: deleteBridge, and putBridge superseding an earlier turn for the same conversation.
mradwankhalil
force-pushed
the
fix/compaction-usage-and-summary
branch
from
September 6, 2026 18:13
f16b23d to
d0cb807
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
0.14.0-fix.1Why
Creating a fresh Agent SDK lifecycle for every normal turn can discard the SDK/MCP prompt cache and resend the conversation unnecessarily, contributing to inflated usage. This keeps the normal conversation lifecycle persistent while retaining explicit reset boundaries for summaries, model/cwd changes, and compaction.
Verification
bun run testpassedbun run buildpassedgit diff --checkpassed/healthwith expected HTTP 401 and no stderrC:\Users\Zephyrus\Documents\ai\opencode-claudeNotes
Update: bridge teardown was on the response path
input.close()tears the SDK stream down synchronously, so calling it inline meantevery turn paid to close the previous turn's bridge before it could proceed.
Measured on Windows with Opus 5: ~11s for a trivial prompt, ~3s once deferred.
closeInputDeferred()moves that one call to the next macrotask at both teardownsites —
deleteBridge, andputBridgesuperseding an earlier turn for the sameconversation. Everything else stays synchronous:
handle.close(), pending-toolrejection, and removal from the pool. The stream still closes, so the leak this
branch fixes stays fixed; it just no longer happens while a request is waiting.
test/bridge-teardown-regression.tscovers both paths — assertsinput.close()has not run immediately after teardown, that
handle.close()has, that the bridgeleaves the pool at once, and that the close does still happen one tick later.
Wired into
npm test; all six suites pass.