Skip to content

fix(opencode-plugin): only commit sessions with new messages at cleanup - #5497

Open
oliver-mee wants to merge 2 commits into
volcengine:mainfrom
oliver-mee:fix/opencode-v2-session-end-commit
Open

oliver-mee wants to merge 2 commits into
volcengine:mainfrom
oliver-mee:fix/opencode-v2-session-end-commit

Conversation

@oliver-mee

@oliver-mee oliver-mee commented Sep 30, 2026 •

Copy link
Copy Markdown

Description

On OpenCode v2, plugin cleanup commits every session in the state file, including ones with nothing new since their last commit. A server that ran many short conversations sends a stack of empty commits each time cleanup runs (an hour of inactivity, a service stop, or a hot reload).

This PR makes cleanup commit only sessions that have new messages since their last commit, and adds the server's status and archived to the commit log line so an empty commit is visible in openviking-memory.log.

An earlier revision also added a plugin-side idle timer (commitIdleSeconds) to commit sessions once they go quiet. Following review, that part is dropped: the server's memory.session_auto_commit scheduler with a default auto_commit_policy already covers it for every harness, with no plugin code.

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

Refs #5494

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

  • lib/memory-session.mjs: flushSession and flushAll take onlyIfUncommitted, which skips the commit when a session has no messages, or no successful captures beyond its commit watermark. It defaults to false, so v1 cleanup and every other caller behave as before. The commit log line now includes status and archived.
  • lib/v2-plugin.mjs: v2 cleanup calls flushAll({ commit: true, onlyIfUncommitted: true }).
  • Docs: the OpenCode lifecycle paragraph in docs/en, docs/zh and the plugin README.
  • package.json: version 0.5.28 to 0.5.29 for the plugin-versions check.

Testing

Current refresh on macOS (arm64) with Node 26.13.1, based on af8ae4a4cb74 (head a6a026accbf0):

  • OpenCode plugin: npm --prefix examples/opencode-plugin test: 66 passed, 0 failed, 0 cancelled, 0 skipped, with an isolated test home.
  • Plugin syntax: npm --prefix examples/opencode-plugin run check: passed.
  • Version gate: bash .github/scripts/check-plugin-version-bumps.sh origin/main: passed with 0.5.28 -> 0.5.29.
  • Shared runtime generation: node examples/memory-plugin-shared/sync.mjs: completed and left the checkout clean.
  • CI selection: .github/scripts/ci_plan.py --base origin/main: plugins_changed=true, deps_changed=false, and the other optional component selectors false.
  • git diff --check: passed.
  • Shared library suite and full repository suite: not run for this focused plugin-only refresh.
  • No real model calls or deployed service changes were made. At publication, the three current-head official workflows were action_required awaiting maintainer approval; no executed upstream CI result is claimed.

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

Additional Notes

The branch is rebased on af8ae4a4cb74db4414ac4860fa9ce9bace7275b3; current head is a6a026accbf008c40e15020141ac850a680d0222. Upstream had moved the OpenCode package to 0.5.28, so this PR now carries 0.5.29. Conflicts in package.json and tests/v2-plugin.test.mjs were resolved while retaining current upstream changes and restoring the cleanup regression. The implementation preserves upstream stale-session pruning, missing-session mapping removal, subagent exclusion, flush serialization, and full-backlog commit behavior.

The cleanup watermark is captured when the commit request is sent. The blocked-response test queues its second flush before releasing the first response, so captures that arrive during a slow commit remain uncommitted without relying on a timeout. The server-side idle scheduler remains the preferred deployment-controlled idle mechanism; this PR retains only the cleanup watermark fix.

Model used: Claude Opus 5.5 (Claude Code) for the original code and tests. Later refreshes used GPT-6.1 Sol and Kimi under human direction.


🤖 Generated with Claude Opus 5.5; refreshed with GPT-6.1 Sol and Kimi
🧑‍💻 Ideated, directed and reviewed by a human, @oliver-mee

@ZaynJarvis ZaynJarvis added the agent-plugins Agent harness and plugin integrations label Oct 1, 2026
@ZaynJarvis

Copy link
Copy Markdown
Collaborator

there's a config in server side, where you can configure auto-commit on server, that applies to all agents.

I think thats the easier way, and no extra code in plugin side.

@ZaynJarvis

Copy link
Copy Markdown
Collaborator

To make my previous comment precise, current main can centralize this in the server's ov.conf:

{
  "memory": {
    "session_auto_commit": {
      "enabled": true,
      "check_interval_seconds": 60.0,
      "scan_rate_limit_files_per_second": 2.0
    }
  },
  "server": {
    "user_config_defaults": {
      "auto_commit_policy": {
        "pending_token_threshold": 0,
        "message_count_threshold": 0,
        "idle_timeout_seconds": 600,
        "keep_recent_count": 0,
        "min_commit_interval_seconds": 0
      }
    }
  }
}

memory.session_auto_commit.enabled starts the server-side idle scheduler. server.user_config_defaults.auto_commit_policy gives newly created sessions that do not provide an explicit policy a 600-second idle timeout; the two threshold values are 0 here so this policy uses idle commit only. Idle commits archive the full backlog and ignore keep_recent_count.

Two caveats:

  • This default applies to newly created sessions. Existing sessions that already have auto_commit_policy: null need to be recreated or updated through PATCH /api/v1/sessions/{session_id}/config.
  • The actual commit can happen up to roughly one scan interval after the 600-second timeout, hence the 60-second scan interval above.

So for deployments where we control server config, I still prefer this centralized path over adding another OpenCode-only timer/config. The plugin-side fallback is only needed if OpenCode must support servers where this is not enabled or cannot be configured.

@oliver-mee
oliver-mee force-pushed the fix/opencode-v2-session-end-commit branch from ed0519f to f210b8b Compare October 1, 2026 15:29
@oliver-mee oliver-mee changed the title fix(opencode-plugin): commit v2 sessions when they end fix(opencode-plugin): only commit sessions with new messages at cleanup Oct 1, 2026
@oliver-mee

Copy link
Copy Markdown
Author

Thanks @ZaynJarvis, that's the cleaner route. We've turned on session_auto_commit with a 600 second idle policy on our server and it does the job: a fresh OpenCode session committed about 11 minutes after its last message, with no plugin timer.

One note for anyone on the 0.4.22 release: there the switch is still memory.session_auto_commit.idle_enabled, not enabled.

I've cut this PR down to the cleanup fix. Cleanup now commits only sessions with new messages since their last commit, and the commit log line shows status and archived. I've also rebased on main and bumped the plugin version for the version check.

@ktz03 ktz03 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Follow-up: 2026-10-14, current head de41f08162d4a69b9e6e69a6d931a0b971f7ad82

Rechecked against main ad4d99de1d95145d42b81904be2546c4302c5ac7: the cleanup guard follows pending capture inside upstream's serialized flush chain, preserves keep_recent_count: 0 and the commit watermark, and the delayed-response regression releases the response before awaiting the queued flush.

Windows / Node 24 passes 26/26 in the two affected manager/v2 test files, including both watermark regressions, both changed modules pass syntax checks, and the official version gate passes with 0.5.14 -> 0.5.15; the actual merge-tree is conflict-free and its OpenCode/shared/gate inputs match the tested author head byte-for-byte.

Temporary-fixture ENOENT log-write warnings appear during teardown; this controlled-localhost execution covers the two test files, with no complete 56-test suite, Linux, full merged server, real host/backend/auth/model, performance or legacy-state upgrade validation.

I found no additional blocker in those scoped cleanup/watermark cases, while the code and regressions remain @oliver-mee's work; my repository permission is READ, so I cannot approve workflows and these local results do not establish official CI execution or approval.

Preserved earlier review

Verbatim previous body; its stated heads, execution scope and original GitHub review metadata are historical.

Follow-up: 2026-10-04, current head 5a4289fe94c581dcd31f1df1526aca8f7ba4aea1

AI-assisted follow-up with local execution and an additional AI-assisted source review. The author's capture/committed sequence watermark addresses both findings below. I reran the original same-millisecond and capture-during-commit-response scenarios: each now makes the second commit and leaves no pending fixture messages. All seven diagnostic probes passed, including normal cleanup, empty cleanup, retryable failure/retry, keep-window and existing reentrant behavior controls. The two original scenarios also pass on current main 9d9bc85e1f6a15afa7f23b0d7bf114a7c61cad14.

The complete current-head OpenCode suite passes on Ubuntu WSL with Node 24.21.0: 55 passed, 0 failed, 0 skipped, including both new manager regressions. On Windows, it is 50 passed / 5 failed; current main is 45 passed / the same 5 failed. Those five failures are the previously reported POSIX-path fixture assertions. I did not modify the author's tests to obtain a green result.

Current main is not an ancestor of this head. The actual merge-tree is conflict-free, but differs from the head tree. Both examples/opencode-plugin and examples/memory-plugin-shared are identical between the tested head and that merge result, so the OpenCode code/tests reviewed here also match the merged view. The OpenCode release version is 0.5.5 versus current main's 0.5.4; its 22 generated shared runtime files match current main's canonical sources byte-for-byte.

I found no remaining blocker for the two reported watermark scenarios within this review scope. The three current-head official workflows are still action_required with zero jobs; these local results are not executed official CI or maintainer approval. The probes use a controlled local HTTP fixture and clock. I did not test a deployed OpenViking service, real OpenCode host workflows, or an upgrade from legacy persisted state without sequence fields. The code change and added regressions are the author's work.

Historical review: previous head f210b8b8e8738f08e4e7db9da841b8cbda34d6ea

The findings and counts below describe the old head and are superseded for the two watermark cases by the follow-up above. GitHub's original review commit metadata remains attached to that old head.

AI-assisted first-pass review of f210b8b8, with automated local validation on Windows / Node 24.21.0. No independent human code review is claimed.

One correctness blocker in the new hasUncommittedActivity() guard: wall-clock comparison does not reliably identify messages covered by a completed commit.

I reproduced both cases through the actual createMemorySessionManager() and startV2Plugin() cleanup entrypoint, using a local HTTP fixture:

  1. A new message in the same millisecond as the preceding commit. After the first commit, a second message is captured and POSTed while both lastActivityAt and lastCommitTime are 1000. Cleanup sees equality, skips the second commit, and leaves the second message pending.
  2. A message captured while an earlier commit response is delayed. The fixture snapshots the first message for the commit, then delays its response. The second message is captured and POSTed at 2100; the first response arrives at 3000, causing commitOvSession() to advance lastCommitTime past the second message's activity. Cleanup at 4000 skips, leaving that second message pending even though it was not covered by the earlier commit.

On this head, each scenario issues one commit and leaves the second message pending. With the same production-client probes against current-main df32bf6e runtime, both scenarios issue the second commit and leave no pending messages. This is a client-entrypoint A/B reproduction with a fake backend, not a live OpenViking deployment.

Please use a capture/commit watermark tied to the messages actually covered by that commit, so an older request's completion cannot mark newer captured messages clean. Add these two regression cases alongside the existing quiet-cleanup test; normal new-message cleanup, empty sessions and retryable commit failure/retry controls passed in this probe.

The existing suite passed 48 of 53 tests on Windows; its five failures are unchanged POSIX path assertions that also fail on main. I submitted the independent test-only correction in #5562, where the current-main suite passes 50/50. Those fixture failures are separate from the two new cleanup regressions above. The scoped manager suite passed 13/13 and the v2 cleanup tests passed 2/2. This head currently has no check runs, and its Actions await approval, so these local results do not imply green CI.

@oliver-mee

Copy link
Copy Markdown
Author

Thanks @ktz03, good catch on both. Cleanup now tracks a commit watermark taken when the commit is sent instead of comparing wall-clock times, so a slow response can't mark later messages clean. Added both cases as regression tests in 5a4289f.

@oliver-mee

Copy link
Copy Markdown
Author

Rebased onto b8ec9d8210ab on current main. The cleanup guard now sits inside upstream's serialized flush chain, and commits retain upstream's keep_recent_count: 0 behavior. The commit watermark and its regression tests are preserved.

Updated the blocked-commit test to release the response before awaiting the queued flush, so it no longer depends on a request timeout. All 56 plugin tests pass, syntax checks pass, and the plugin-version gate passes with 0.5.14 to 0.5.15.

The old failed check was a version-bump failure on an earlier head. Could a maintainer approve the workflows for the new head, de41f0816? Upstream CI is not yet verified on this update.

@ZaynJarvis

Copy link
Copy Markdown
Collaborator

Please rebase onto main: #5936 (60629f9) and #5935 (a0c3574) changed examples/opencode-plugin/lib/memory-session.mjs and its tests (pruning fully-captured sessions and dropping the local mapping when the server no longer has the session), so this PR now conflicts there and in package.json (main is at opencode 0.5.17). When resolving, keep the cleanup commit watermark working with the new pruning path, and bump opencode to 0.5.18.

@oliver-mee
oliver-mee force-pushed the fix/opencode-v2-session-end-commit branch from de41f08 to d5015c1 Compare October 31, 2026 20:46
@oliver-mee

Copy link
Copy Markdown
Author

Refreshed on current main 012e1f0c8340 as d5015c183. I rebuilt the final patch on the new base rather than replaying the old intermediate commits. The cleanup watermark now preserves upstream's subagent exclusion and stale-session pruning, and both timing regressions remain covered. On macOS / Node 24.22.2, all 61 OpenCode tests and the syntax/version gates pass. The shared suite is 620/621; the Desktop launcher output assertion also fails on pristine main. The PR body has the current validation scope. Upstream CI has not reported results yet.

Refresh assisted by GPT-6.1 Sol.

@oliver-mee

Copy link
Copy Markdown
Author

Rebased onto current main (through 60629f9 and a0c3574), so the memory-session.mjs and package.json conflicts you flagged are resolved and opencode is on 0.5.26. Head is 72447d13.

@ZaynJarvis on the server side point: agreed that session_auto_commit in ov.conf is the cheaper route where the server is configured for it. I would rather not fold that into this PR, since the plugin path still has to handle hosts that do not run the server, but say the word if you want the scope narrowed further. Thanks for the detail on which upstream changes moved the file.

@oliver-mee
oliver-mee force-pushed the fix/opencode-v2-session-end-commit branch from 72447d1 to a6a026a Compare November 16, 2026 10:27

This branch has not been deployed

No deployments
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: Backlog

Development

Successfully merging this pull request may close these issues.

3 participants