Repository navigation
fix(opencode-plugin): serialize overlapping flushes of one session - #5674
Merged
ZaynJarvis merged 1 commit intoOct 7, 2026
Merged
Conversation
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
approved these changes
Oct 7, 2026
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.
Description
Fixes the OpenCode plugin (v1 hooks) sending the same
messages/batchtwice at session end.flushSessionnow runs on a per-session promise chain, so overlapping flushes of one session (thesession.idleflush anddispose→flushAll({ commit: true })) run one after another. The second flush re-readscaptured, finds nothing to send, and its commit lands after the first flush's batch.Root cause
OpenCode calls the plugin
eventhook without awaiting it (void hook["event"]?.(...)) and runsdisposefrom a separate finalizer. When a turn ends,session.idle→handleSessionIdle→flushSession(commit: false)anddispose→flushAll({ commit: true })→flushSessionrun for the same session at the same time.flushSessionhad no per-session mutual exclusion.flushPendingMessagessnapshots the messages withcaptured === false, awaitsGET /healthand the batch POST, and setscaptured = trueonly 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
mapping.sendingMessagesin-flight set before awaiting, and had acommitInFlightguard.memory-session.mjson top of the shared lib. It dropped both guards and setcaptured = trueonly after an awaited send whose first step is an awaited/health. The same PR addeddispose → flushAll({ commit: true })and thesession.idle → flushSessionpath./health→ batch → mark captured, which widened the window to the whole pending list.serializeSessionchain inv2-plugin.mjs, but the v1 hooks never got an equivalent.Alternatives considered
flushSession(chosen). Every flush trigger already goes throughflushSession: idle, deleted/error, compaction, theexperimental.session.compactinghook, andflushAllfrom 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 existingsaveChain/enqueueSavepattern in the same file.flushAllwould then find nothing to send and commit immediately, possibly before the idle batch lands. In a one-shotopencode run, those messages would stay uncommitted. It also needs rollback for partial failures.event/compacting/disposehooks 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
saveStaterewrite.Human Involvement
Related Issue
Fixes #5666
Type of Change
Changes Made
examples/opencode-plugin/lib/memory-session.mjs:flushSessionchains its body (flush pending messages, then a forced commit or the threshold check, thenenqueueSave) ontostate.flushChain.flushChainis never persisted, becauseserializeSessionStatelists its fields explicitly.examples/opencode-plugin/tests/memory-session.test.mjs: regression test that runssession.idleandflushAll({ commit: true })concurrently, then asserts exactly onemessages/batchand a commit after it.@openviking/opencode-plugin0.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:
With the old
memory-session.mjsand the new test, the test fails and the request sequence matches the issue: two/health, two identicalmessages/batch, then the commit. With the fix it passes.cd examples/opencode-plugin && npm test: 51/51 pass.npm run checkpasses.bash .github/scripts/check-plugin-version-bumps.sh origin/mainpasses.Checklist
Screenshots (if applicable)
N/A
Additional Notes
commitSession(the explicit commit path) still callsflushPendingMessagesdirectly. Nothing in production calls it today, so it is left unchanged.