Skip to content

fix(opencode-plugin): serialize overlapping flushes of one session - #5674

Merged
ZaynJarvis merged 1 commit into
volcengine:mainfrom
t0saki:fix/opencode-v1-serialize-flush
Oct 7, 2026
Merged

ZaynJarvis merged 1 commit into
volcengine:mainfrom
t0saki:fix/opencode-v1-serialize-flush

Conversation

@t0saki

@t0saki t0saki commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Description

Fixes the OpenCode plugin (v1 hooks) sending the same messages/batch twice at session end. flushSession now runs on a per-session promise chain, so overlapping flushes of one session (the session.idle flush and dispose → flushAll({ commit: true })) run one after another. The second flush re-reads captured, finds nothing to send, and its commit lands after the first flush's batch.

Root cause

OpenCode calls the plugin event hook without awaiting it (void hook["event"]?.(...)) and runs dispose from a separate finalizer. When a turn ends, session.idle → handleSessionIdle → flushSession(commit: false) and dispose → flushAll({ commit: true }) → flushSession run for the same session at the same time. flushSession had no per-session mutual exclusion. flushPendingMessages snapshots the messages with captured === false, awaits GET /health and the batch POST, and sets captured = true only afterwards, so both flushes send the same batch. The batch carries no per-message id, so the server appends both copies and the session holds 2N messages. Because the two flushes were not ordered, the dispose commit could also land between the two batches (the duplicate stays in the live session) or before the idle batch (that batch is left out of the commit).

How it was introduced

Alternatives considered

  • Per-session promise chain in flushSession (chosen). Every flush trigger already goes through flushSession: idle, deleted/error, compaction, the experimental.session.compacting hook, and flushAll from v1 and v2 dispose. About 10 lines fix both the duplicate batch and the commit ordering. The chain is per session, so a slow commit on one session does not hold up another, and it follows the existing saveChain/enqueueSave pattern in the same file.
  • Claim messages synchronously before the first await (restore the old in-flight set). This fixes the duplicate batch, but dispose's flushAll would then find nothing to send and commit immediately, possibly before the idle batch lands. In a one-shot opencode run, those messages would stay uncommitted. It also needs rollback for partial failures.
  • Wrap the v1 event/compacting/dispose hooks in one global chain, as v2 does. This serializes all capture and replay across all sessions, so a slow commit would stall capture and the awaited compacting hook. That is a bigger behavior change than this bug needs.

Open PR #3691 contains an equivalent per-session chain, bundled with an unrelated periodic-flush and saveState rewrite.

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

Related Issue

Fixes #5666

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • examples/opencode-plugin/lib/memory-session.mjs: flushSession chains its body (flush pending messages, then a forced commit or the threshold check, then enqueueSave) onto state.flushChain. flushChain is never persisted, because serializeSessionState lists its fields explicitly.
  • examples/opencode-plugin/tests/memory-session.test.mjs: regression test that runs session.idle and flushAll({ commit: true }) concurrently, then asserts exactly one messages/batch and a commit after it.
  • @openviking/opencode-plugin 0.5.4 → 0.5.5.

Testing

  • I have added tests that prove my fix is effective or that my feature works

  • New and existing unit tests pass locally with my changes

  • I have tested this on the following platforms:

    • Linux
    • macOS
    • Windows
  • With the old memory-session.mjs and the new test, the test fails and the request sequence matches the issue: two /health, two identical messages/batch, then the commit. With the fix it passes.

  • cd examples/opencode-plugin && npm test: 51/51 pass. npm run check passes.

  • bash .github/scripts/check-plugin-version-bumps.sh origin/main passes.

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

N/A

Additional Notes

OpenCode fires the v1 event hook without awaiting it and runs dispose
from a separate finalizer, so session.idle's flush and dispose's
flushAll({ commit: true }) can run for the same session at once. Both
snapshot the same uncaptured messages before marking them captured, so
the batch is sent twice and the server stores every message twice; the
commit can also land between or before the batches.

Chain flushSession per session so the second flush re-reads captured
state after the first finishes and the commit follows the batch.
@ZaynJarvis ZaynJarvis added the agent-plugins Agent harness and plugin integrations label Oct 7, 2026
@t0saki
t0saki marked this pull request as ready for review October 7, 2026 07:12
Copilot AI balanced review requested due to automatic review settings October 7, 2026 07:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ZaynJarvis
ZaynJarvis merged commit 9b9ac10 into volcengine:main Oct 7, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-plugins Agent harness and plugin integrations

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug]: opencode-plugin (v1 hooks) sends the same messages/batch twice at session end, storing every message twice

3 participants