Skip to content

refactor(opencode): optimize doom loop detection, summary debounce, parallel plugin events#20303

Open
AndresCdo wants to merge 3 commits intoanomalyco:devfrom
AndresCdo:perf/doom-loop-summary-plugin
Open

refactor(opencode): optimize doom loop detection, summary debounce, parallel plugin events#20303
AndresCdo wants to merge 3 commits intoanomalyco:devfrom
AndresCdo:perf/doom-loop-summary-plugin

Conversation

@AndresCdo
Copy link
Copy Markdown

Issue for this PR

Closes #20302

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Three targeted performance optimizations in the session processing loop:

1. In-memory doom loop detection (session/processor.ts)

Replaces MessageV2.parts(ctx.assistantMessage.id) database call with an in-memory toolCallHistory array tracked in ProcessorContext. The doom loop check already has all the data it needs from the streaming event handler — no need to re-query the database on every tool call.

2. Summary debounce (session/processor.ts)

Runs SessionSummary.summarize() only on the first step of a session. Subsequent steps produce summaries that overwrite the first one with no additional value, wasting CPU and I/O on redundant summary computation.

3. Parallel plugin event handlers (plugin/index.ts)

Replaces sequential for loop with Effect.forEach({ concurrency: "unbounded" }) so slow plugin event handlers don't block the main event stream. Each handler runs in a forked fiber with error isolation via Effect.tryPromise.

How did you verify your code works?

  • bun typecheck passes (zero errors in changed files)
  • Changes are surgical: +30/-15 lines across 2 files
  • Doom loop: same logic, different data source (in-memory vs DB)
  • Summary: first step behavior unchanged, subsequent steps skip redundant work
  • Plugin handlers: same input, parallel execution with error isolation
  • No API changes, no breaking changes

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…lel plugin events

Three targeted performance optimizations:

1. In-memory doom loop detection: Replace MessageV2.parts() DB call
   with toolCallHistory array in ProcessorContext. Eliminates one
   database query per tool call.

2. Summary debounce: Run SessionSummary.summarize() only on the first
   step. Subsequent steps produce redundant summaries that overwrite
   the first one with no additional value.

3. Parallel plugin event handlers: Replace sequential for loop with
   Effect.forEach({ concurrency: "unbounded" }) so slow handlers
   don't block the main event stream. Each handler runs in a forked
   fiber with error isolation.

Closes anomalyco#20302
@github-actions
Copy link
Copy Markdown
Contributor

Hey! Your PR title perf(opencode): optimize doom loop detection, summary debounce, parallel plugin events doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found

PR #20288 - refactor(opencode): optimize session loop with message cache, tool cache, doom loop, summary debounce
#20288

Why they're related:

Recommendation: Check PR #20288 to verify if it's addressing the same issue #20302 or if the changes overlap before merging.

@AndresCdo AndresCdo changed the title perf(opencode): optimize doom loop detection, summary debounce, parallel plugin events refactor(opencode): optimize doom loop detection, summary debounce, parallel plugin events Mar 31, 2026
@AndresCdo
Copy link
Copy Markdown
Author

Re: duplicate flag from bot

PR #20288 and this PR (#20303) are not duplicates — they target different code structures:

File overlap analysis:

The changes are complementary and non-conflicting. If both are merged, PR #20288 would need to be rebased onto current dev to apply the message/tool cache to the Effect-based code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Session loop performance — doom loop in-memory, summary debounce, parallel plugin events

1 participant