Skip to content

fix: capture OpenCode sessions that predate the session-tracker (#402) - #403

Merged
dhague merged 3 commits into
mainfrom
worktree-402-opencode-session-fallback
Sep 3, 2026
Merged

fix: capture OpenCode sessions that predate the session-tracker (#402)#403
dhague merged 3 commits into
mainfrom
worktree-402-opencode-session-fallback

Conversation

@dhague

@dhague dhague commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Problem

enchiridion save-session refused to capture an OpenCode session that was started before the session-tracker plugin was installed, exiting non-zero with:

No state recorded for session <id> under .opencode/wiki-knowledge/sessions, per the session-tracker plugin.

The transcript then had to be reconstructed by hand. Fixes #402.

Root cause

captureOpenCodeSession required a recorded <id>.json state entry (via findOpenCodeSessionID, which threw). But opencode export <id> returns the transcript whether or not the SessionStart hook ever fired — so tracker state was never a real prerequisite for the capture, only tie-break evidence.

Fix (issue option 2)

Split findOpenCodeSessionID into two intents:

  • openCodeSessionIDFromEnv — reads $OPENCODE_SESSION_ID, the only hard prerequisite for a capture. captureOpenCodeSession now uses this and always shells out to opencode export, tracked or not.
  • isOpenCodeSessionTracked — a never-throwing boolean, consulted only as tie-break evidence in captureSession when both host session-id variables are set. Behaviour there is unchanged: an untracked OpenCode id still yields to Claude Code (an untracked id can be a leaked variable from an unrelated project).

Tests

  • New: captureOpenCodeSession captures a session that predates the tracker (no .opencode/ state at all) via the injected export seam.
  • New: unit coverage for openCodeSessionIDFromEnv (throws unset / returns id) and isOpenCodeSessionTracked (all four false branches + the true branch).
  • Updated: the only-$OPENCODE_SESSION_ID-set dispatch test now asserts the export path succeeds without tracker state, rather than failing.

npm run typecheck, npm run lint clean. Full suite passes except pre-existing Windows-only failures (subprocess .bin/tsx spawn; tests assuming no .claude/ ancestor) — CI runs on Linux and is unaffected.

Bundle artifacts are intentionally not rebuilt here — the shipped bundle is refreshed at release time (scripts/release.sh); the CI freshness guard only runs on version-bump PRs.

save-session refused to capture an OpenCode session started before the
session-tracker plugin was installed, because captureOpenCodeSession
required a recorded <id>.json state entry (via findOpenCodeSessionID,
which threw "No state recorded for session ..."). But `opencode export`
returns the transcript whether or not the SessionStart hook ever fired,
so the tracker state was never a real prerequisite for the capture.

Split findOpenCodeSessionID into two intents:

- openCodeSessionIDFromEnv — reads $OPENCODE_SESSION_ID, the only hard
  prerequisite for a capture; captureOpenCodeSession now uses this and
  always shells out to export.
- isOpenCodeSessionTracked — a never-throwing boolean, consulted only as
  tie-break evidence in captureSession when both host session-id
  variables are set (unchanged behaviour: an untracked OpenCode id still
  yields to Claude Code).

Bundle artifacts are intentionally not rebuilt here — the shipped bundle
is refreshed at release time (scripts/release.sh), and the CI freshness
guard only runs on version-bump PRs.
An untracked OpenCode session no longer errors on tracker state; it
falls through to `opencode export`. Isolate the OpenCode dispatch by
unsetting CLAUDE_CODE_SESSION_ID and accept the missing-CLI error as
proof the env var was read.
@dhague
dhague merged commit 789f5e3 into main Sep 3, 2026
7 checks passed
@dhague
dhague deleted the worktree-402-opencode-session-fallback branch September 3, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session tracker should record sessions started before plugin is active

1 participant