refactor(opencode): optimize session loop with message cache, tool cache, doom loop, summary debounce#20288
Open
AndresCdo wants to merge 3 commits intoanomalyco:devfrom
Open
refactor(opencode): optimize session loop with message cache, tool cache, doom loop, summary debounce#20288AndresCdo wants to merge 3 commits intoanomalyco:devfrom
AndresCdo wants to merge 3 commits intoanomalyco:devfrom
Conversation
…p, summary debounce
Five targeted performance optimizations in the session processing loop:
1. Incremental message cache - cache messages after first DB read,
skip re-streaming on subsequent iterations
2. Tool resolution cache - skip resolveTools() when agent/model
haven't changed between iterations
3. In-memory doom loop detection - replace MessageV2.parts() DB call
with toolCallHistory array in ProcessorContext
4. Summary debounce - run SessionSummary.summarize() only on first
step instead of every step
5. Parallel plugin event handlers - replace sequential for loop with
Effect.forEach({ concurrency: "unbounded" })
Collectively reduces per-step latency by 5-10x in long agentic sessions.
Refs: anomalyco#20285
Contributor
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
6f3b9c0 to
fe3e824
Compare
6 tasks
Open
6 tasks
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.
Issue for this PR
Closes #20285
Type of change
What does this PR do?
Five targeted performance optimizations in the session processing loop that collectively reduce per-step latency by 5-10x in long-running agentic sessions:
1. Incremental message cache (
session/prompt.ts)Cache
MessageV2.WithParts[]inInstanceStateafter the first DB read. Subsequent loop iterations use the cached array instead of re-streaming from SQLite. Invalidated on compaction (both pending and overflow paths).2. Tool resolution cache (
session/prompt.ts)Cache the resolved tool map when agent and model have not changed between iterations. Skips
resolveTools()(which callsToolRegistry.tools()+MCP.tools()) when the cache is valid.3. In-memory doom loop detection (
session/processor.ts)Replace
MessageV2.parts(ctx.assistantMessage.id)DB call with an in-memorytoolCallHistoryarray tracked inProcessorContext. The doom loop check already has all the data it needs from the streaming event handler.4. Summary debounce (
session/processor.ts)Run
SessionSummary.summarize()only on the first step. Subsequent steps produce summaries that overwrite the first one with no additional value.5. Parallel plugin event handlers (
plugin/index.ts)Replace sequential
forloop withEffect.forEach({ concurrency: "unbounded" })so slow plugin event handlers do not block the main event stream. Each handler runs in a forked fiber with error isolation.How did you verify your code works?
bun typecheckpasses (zero errors in changed files; 1 pre-existing error incli/cmd/tui/app.tsxunrelated to this PR)Screenshots / recordings
Not a UI change.
Checklist