Skip to content

fix(opencode-plugin): decouple flush/threshold-commit from session.idle and make state save process-safe - #3691

Open
TE-N-ElvenWang wants to merge 17 commits into
volcengine:mainfrom
TE-N-ElvenWang:fix/opencode-plugin-periodic-flush
Open

fix(opencode-plugin): decouple flush/threshold-commit from session.idle and make state save process-safe#3691
TE-N-ElvenWang wants to merge 17 commits into
volcengine:mainfrom
TE-N-ElvenWang:fix/opencode-plugin-periodic-flush

Conversation

@TE-N-ElvenWang

Copy link
Copy Markdown

Problem

The opencode plugin's message flush and threshold-commit path
(flushPendingMessagesmaybeCommitByThreshold) is wired exclusively to the
session.idle event. OpenCode emits session.idle only when an assistant turn
fully completes (no queued work). During long multi-step agent turns
(back-to-back tool calls), session.idle is never emitted, so:

  1. Messages accumulate locally as captured: false but are never POSTed.
  2. pending_tokens on the server never grows.
  3. The configured commitTokenThreshold is never reached, so threshold-based
    commits never fire.
  4. Persistence only happens at plugin dispose (shutdown) via flushAll.

Additionally, saveState uses a single fixed temp file (${statePath}.tmp).
When two opencode processes run concurrently, both race on the same .tmp,
causing ENOENT rename failures and cross-process state clobbering.

Fix

Fix A: Process-safe temp file

Use a per-PID temp file: ${statePath}.${process.pid}.tmp — eliminates
cross-process ENOENT rename races.

Fix B: Periodic flush timer

Add a setInterval-based periodic flush independent of session.idle:

  • Default 60s interval (configurable via periodicFlushIntervalMs, floor 10s)
  • Scans sessions for uncaptured messages, calls flushSession(..., { reason: 'periodic' })
  • Starts in init(), cleared in flushAll(), .unref()'d

This decouples the send + threshold-check path from session.idle, so messages
flush and threshold commits fire even during long multi-step agent turns.

Verification

Tested in production: Periodic flush timer started appears at startup;
pending_tokens accumulates during long turns; threshold commit fires at
≥20000 without dispose; zero ENOENT races under dual-process concurrency.

@TE-N-ElvenWang

Copy link
Copy Markdown
Author

虽然是AI写的patch啊,但是问题是opencode里用openviking的话会导致上传信息不准确的问题。尤其是多个opencode进程一起开着的时候。

@TE-N-ElvenWang

Copy link
Copy Markdown
Author

all patches are refined by open-code-review tools with claude opus 4.8.

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant