OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

Bug: Long-running sessions lose messages due to delayed session mapping

Open sacrtap opened this issue 6 months ago • 2 comments

Bug Description

When an OpenCode session runs for an extended period (especially across multiple days), messages are lost because the OpenViking plugin buffers them indefinitely waiting for session mapping, but never replays them once the mapping is finally established.

Root Cause Analysis

After investigating the plugin logs (`~/.config/opencode/plugins/openviking-memory.log`), I found the following evidence:

Session: `ses_1d98635d2ffeS72ifOpUOrGCaj`

Log Message Count Meaning
`Message buffered (no session mapping yet)` 853 Messages buffered waiting for mapping
`Message content buffered (no session mapping yet)` 114 Message content buffered
`Session mapping established` 1 Mapping finally created
`OpenCode session created` 1 Session creation
`OpenCode session error` 2 Session errors at 08:22 and 08:25

Timeline

``` May 14: Session created ↓ May 14-16: 967 messages produced (853 + 114) ↓ All messages buffered: "no session mapping yet" ↓ May 16: Session mapping finally established (too late) ↓ 2x "OpenCode session error" events ↓ Buffered messages NEVER sent to OpenViking ↓ No records in OpenViking database ```

Expected Behavior

  1. When session mapping is established, the plugin should replay all buffered messages to OpenViking
  2. OR the plugin should timeout and report an error if mapping takes too long
  3. Cross-day sessions should not lose message history

Actual Behavior

  • Messages are buffered indefinitely with no timeout
  • When mapping is finally established, buffered messages are discarded
  • No error is reported to the user about lost messages
  • Session appears to work normally but data is silently lost

Environment

  • OpenViking version: 0.3.17
  • OpenCode plugin: `openviking-memory.ts` at `~/.config/opencode/plugins/`
  • Auto-commit config: `intervalMinutes: 3`
  • Session duration: Multi-day (started May 14, continued May 16)
  • Terminal: VS Code integrated terminal (separate from main terminal)

Reproduction Steps

  1. Start an OpenCode session in VS Code integrated terminal
  2. Keep the session running across multiple days
  3. Use OpenViking tools (memsearch, memread, etc.)
  4. Check OpenViking database - messages from this session are missing
  5. Check plugin logs - all messages show "no session mapping yet"

Suggested Fixes

Option A: Replay buffered messages on mapping establishment

When `Session mapping established` event fires, replay all buffered messages for that session before processing new messages.

Option B: Add timeout with user notification

If session mapping is not established within N minutes:

  • Log a warning
  • Notify the user that messages may be lost
  • Suggest restarting the session

Option C: Persistent message buffer

Store buffered messages to disk and replay them on next plugin initialization.

Impact

  • Data loss: All conversation history from affected sessions is lost
  • Silent failure: No user-facing indication that data is being lost
  • Multi-day sessions: Particularly impacts users who keep sessions open for extended periods

Log Evidence

Full log excerpt for the affected session (May 16 entries only): ```json {"timestamp":"2026-05-16T06:39:05.529Z","level":"DEBUG","tool":"message","message":"Message buffered (no session mapping yet)","data":{"session_id":"ses_1d98635d2ffeS72ifOpUOrGCaj",...}} ... (853 similar entries) {"timestamp":"2026-05-16T08:22:30.128Z","level":"ERROR","tool":"event","message":"OpenCode session error","data":{"session_id":"ses_1d98635d2ffeS72ifOpUOrGCaj"}} {"timestamp":"2026-05-16T08:25:37.632Z","level":"ERROR","tool":"event","message":"OpenCode session error","data":{"session_id":"ses_1d98635d2ffeS72ifOpUOrGCaj"}} ```

sacrtap avatar May 16 '26 08:05 sacrtap

Thanks for reporting this, and for including the detailed log analysis.

After checking the plugin code, this does not look like an OpenViking core storage/search bug. The Message buffered (no session mapping yet) log means the OpenCode plugin has not yet established the mapping from the OpenCode session to an OpenViking session. That buffer is only intended as a short-lived best-effort bridge while the session mapping is being created. It is not designed to be a reliable multi-day offline queue.

In the current plugin, the pre-mapping buffer is in memory and bounded (MAX_BUFFERED_MESSAGES_PER_SESSION = 100, BUFFERED_MESSAGE_TTL_MS = 15 minutes). So if a session keeps running for hours or across days while the mapping is not established, older buffered messages may expire or be evicted. In that situation, continuing to use the session will not reliably preserve all history.

The expected usage is:

  1. Start the OpenViking server before starting the OpenCode session.
  2. Confirm the plugin logs show the OpenViking health check passing.
  3. For a normal new session, Session mapping established should appear shortly after OpenCode session created.
  4. If the plugin keeps logging Message buffered (no session mapping yet), stop the current OpenCode session, verify the OpenViking server/configuration, and then start a new OpenCode session.
  5. For long-running sessions, periodically run memcommit or end/restart the session at natural boundaries.

There was also a related session.error cleanup issue where pending messages could be lost if a session error occurred before they were captured. That specific issue has been fixed by #2094.

Given the above, I would classify this issue as a plugin usage/recovery limitation rather than an OpenViking core bug. We can still improve the plugin by adding clearer warnings when mapping is delayed too long, and possibly by retrying mapping creation more explicitly. But the current pre-mapping buffer should not be relied on to preserve multi-day session history.

qin-ctx avatar May 18 '26 09:05 qin-ctx

Following #2421, I’m considering an OpenCode-specific recovery fix for this issue.

Unlike #2421, the main problem here is that session mapping is attempted only once. The proposed PR would retry failed mapping creation and durably preserve pre-mapping messages, then replay them after mapping succeeds.

Would this scoped change be acceptable? I would reuse the existing OpenCode session state where possible rather than copy the Claude Code pending queue implementation.

CuSO41108 avatar Jun 14 '26 18:06 CuSO41108