Skip to content

fix: prevent reviewer sessions from cluttering chat history - #51

Merged
dyoshikawa merged 10 commits into
dyoshikawa:mainfrom
thisfilipe:fix/reviewer-session-cleanup
Oct 23, 2026
Merged

dyoshikawa merged 10 commits into
dyoshikawa:mainfrom
thisfilipe:fix/reviewer-session-cleanup

Conversation

@thisfilipe

Copy link
Copy Markdown
Contributor

Summary

Fixes #50.

The V2 agent reviewer currently creates a new top-level Auto-approval review session for every operation, cluttering the chat history and leaving persistent reviewer sessions behind.

This PR fixes the reviewer session lifecycle:

  • Associates temporary reviewer sessions with their originating session using parentID.
  • Automatically removes temporary reviewer sessions after a verdict, including failed reviews and invalid responses.
  • Ensures cleanup errors do not override the original reviewer result or error.
  • Preserves V1 compatibility through an optional session cleanup method.
  • Keeps the reviewer's existing read-only permission rules unchanged.

Tests

Validated on Linux against version 0.7.0:

  • pnpm cicheck — passed.
  • 291 tests across 16 test files — passed.
  • Formatting, lint, TypeScript, spellcheck and secret scanning — passed.
  • pnpm build — passed (ESM and CommonJS).

Added regression tests covering parent session association, temporary session removal, cleanup after failures, and preservation of reviewer results.

Compatibility

No configuration changes are required. The fix applies to newly created reviewer sessions; existing sessions are unaffected.

@dyoshikawa dyoshikawa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this — linking the reviewer session to its parent and cleaning it up afterwards is a good idea, and I'd like to take it.

It can't be merged as-is though: main has moved 23 commits since this branch and src/agent-reviewer.ts now conflicts (the agent reviewer gained a runtime option, so create now passes (agent.runtime === "opencode" ? agent.model : undefined) ?? input.model). Could you rebase onto main? Please keep parentID/remove limited to the V2 OpenCode client so the Claude/Codex runtimes and V1 stay unchanged.

While you're there, two things I'd like changed (see the inline comment): remove should not be able to hold up the verdict, and since the reviewer transcript is currently the only record of why something was allowed, I'd rather not delete it unconditionally. A follow-up for V1 (its SDK also has parentID and session.delete) would be welcome but isn't required here.

Comment thread src/agent-reviewer.ts Outdated
try {
// V2 removes temporary sessions; V1 keeps its existing behavior.
// Cleanup must never replace the review verdict or error.
await this.#client.remove?.({ sessionID });

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This await isn't covered by withTimeout, so if session.remove hangs (or waits for the run that abort just interrupted without awaiting) the permission hook stays blocked past reviewer.timeoutMs. I'd make this best-effort and non-blocking, e.g. void this.#client.remove?.({ sessionID }).catch(() => undefined) after the verdict is settled (dropping the ID from #reviewerSessionIDs once it finishes), or wrap it in a short timeout. A test where remove never resolves and review() still returns in time would pin it down.

Also, with the audit log off by default and it only recording verdict/category, the reviewer session is the only place you can see what the reviewer read and why it allowed something. Could removal be opt-in, or at least skipped for allow verdicts and errors? Otherwise it gets hard to investigate a bad approval after the fact.

Comment thread src/agent-reviewer.ts Outdated
await this.#client.remove?.({ sessionID });
} catch {
// The parent relationship still keeps an orphan out of the top-level list.
} finally {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small thing: this comment only holds for V2 — V1 ignores parentID, so there an orphan still shows up at the top level.

@thisfilipe
thisfilipe force-pushed the fix/reviewer-session-cleanup branch from 737bfd2 to 306e4d9 Compare October 22, 2026 15:11
@thisfilipe

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

I've rebased the PR onto the current main (v0.10.0) and addressed your feedback:

  • Reviewer sessions are linked through parentID only in OpenCode V2.
  • Session deletion is now opt-in via reviewer.agent.cleanupSessions: true. Transcripts are preserved by default for auditing.
  • Cleanup is non-blocking and cannot delay the verdict, even if deletion hangs.
  • V1 and Claude/Codex runtimes retain their original behavior.
  • Added regression tests covering hanging cleanup, failures, and session isolation.

Validation passed in my fork's CI: 378 tests, lint, typecheck, formatting, and build.

https://github.com/thisfilipe/opencode-auto-approval-plugin/actions/runs/39804479474

I'd appreciate another review when you have time!

@dyoshikawa
dyoshikawa merged commit e634e71 into dyoshikawa:main Oct 23, 2026
1 check passed
@dyoshikawa

Copy link
Copy Markdown
Owner

@thisfilipe Thank you! I've collected the remaining small follow-ups in #64.

@dyoshikawa dyoshikawa mentioned this pull request Oct 23, 2026
dyoshikawa added a commit to thisfilipe/opencode-auto-approval-plugin that referenced this pull request Oct 23, 2026
Keep both config tests: the cleanupSessions one from dyoshikawa#51 and the
allToolsNativeAsks one from this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QUBBebviXSBQtiRAmDopZ7
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.

fix: reviewer sessions clutter the main session list in OpenCode v2

2 participants