[Bug] opencode-plugin: auto-recall silently skipped on first message of every new/reconnected session
Bug Description
The OpenCode plugin's auto-recall hook (chat.message → injectRelevantMemories) silently fails on the first user message of every new session and every reconnect. As a result, session-starting prompts — the exact moment auto-recall is most valuable — never benefit from memory injection.
This affects @openviking/opencode-plugin v0.2.3 (the version installed by the official installer) and is also present on main as of 2026-07-22.
Root Cause (code-level)
In examples/opencode-plugin/index.mjs, the chat.message hook executes two steps sequentially on the same output.parts array:
// index.mjs:65-73
"chat.message": async (input, output) => {
try {
await sessionInject.injectSessionContext(input, output) // step 1: prepends <openviking-context> part
if (!config.autoRecall?.enabled) return
await recall.injectRelevantMemories(input, output) // step 2: reads parts, bails
} catch (error) { ... }
}
Step 1 (session-inject.mjs:38) inserts a synthetic text part at index 0:
output.parts.unshift({
text: '<openviking-context source="session-start">...</openviking-context>',
synthetic: true,
...
})
Step 2 calls extractCurrentUserText(output.parts) in memory-recall.mjs:38-47:
function extractCurrentUserText(parts) {
const texts = []
for (const part of parts) {
if (part.type !== "text" || typeof part.text !== "string") continue
if (part.text.includes("<openviking-context")) return null // ⚠️ LINE 42: hard bail
if (!part.synthetic && !part.ignored) texts.push(part.text)
}
...
}
The guard on line 42 returns null (give up on the whole query) as soon as it sees any part containing <openviking-context. On the first message of a session, the synthetic context part from Step 1 is at parts[0], so the loop bails before reaching the real user text at parts[1+].
Note that the very next line (if (!part.synthetic && !part.ignored) texts.push(...)) already filters synthetic parts correctly — so the return null on line 42 is a redundant over-defensive guard that fires before the synthetic filter has a chance to work.
Why it only affects the first message
session-inject.mjs:11 guards with injectedSessions.has(sessionID) — the synthetic context part is only prepended once per session. On the 2nd+ message, no <openviking-context> part is present, so extractCurrentUserText reads real user text normally and recall works as designed.
Reproduces on:
- New session (first message)
- Session reconnect (plugin instance recreated →
injectedSessionsSet is empty → first message after reconnect is treated as a "first message" again)
Impact
- Auto-recall fails at the exact moment it matters most: cold-start of a new task.
- Silent failure (no log line, no error) — very hard to notice without instrumenting the hook.
- Users who open a new session, ask a memory-sensitive question, and close it after one exchange get zero recall benefit, while appearing to have a working plugin.
- The second-message workaround exists but is non-obvious and undocumented.
Reproduction
Setup: OpenCode + @openviking/opencode-plugin v0.2.3, autoRecall.enabled=true, a populated memory store with at least one event clearly relevant to a known query string.
Steps:
- Open a fresh OpenCode session.
- Send a first message containing a phrase that should trigger recall (e.g. a unique token previously stored as an event).
- Observe the assistant's context — no recall block is injected (only the static session-start profile/available-memories block from
session-inject). - Send a second message on the same topic.
- Observe — recall now fires correctly and a recall block is prepended.
Cross-session capture/recall validation (2026-07-22)
A unique sentinel token OPENCODE_OPENVIKING_TEST_20260722T_CURRENT was registered in session A (no manual remember call; relying on auto-capture). Session A was allowed to end normally; its messages were committed to the OpenViking archive, confirmed by openviking_grep returning 5 literal matches under viking://user/coding/sessions/oc-ses_077659843.../history/archive_001/messages.jsonl.
A new session B was opened and, before any manual MCP call, the auto-injected session-start context was inspected:
| Check | Result |
|---|---|
| Auto-capture committed session A's messages | PASS (archive_001 contains the token, 5 grep matches) |
| Session-start injection runs in session B | PASS (profile + available-memories block present) |
| Auto-recall fires on session B's first message | FAIL — no recall block, token not surfaced |
Manual openviking_grep for the token |
PASS (5 matches, all in archived session A) |
Manual openviking_search (semantic) |
Partial — returns topically related items, does not directly surface the literal token |
Verdict: capture → commit → persist chain is healthy. The failure is isolated to the auto-recall hook on first messages.
Environment
- Plugin:
@openviking/opencode-pluginv0.2.3 (installed via official installer) - Server: OpenViking v0.4.9 on
127.0.0.1:1933 - Host: Windows 11, OpenCode Desktop
- Upstream
mainbranchexamples/opencode-plugin/lib/memory-recall.mjschecked at 2026-07-22 — byte-identical, bug present.
Suggested Fix (two options, either works)
Option A — minimal, preferred: change line 42 of memory-recall.mjs from a hard bail to a skip:
- if (part.text.includes("<openviking-context")) return null
+ if (part.text.includes("<openviking-context")) continue
This lets the existing !part.synthetic filter on the next line handle synthetic parts as designed, instead of aborting the whole query.
Option B — reorder hooks in index.mjs:65-73 so recall runs before session-inject:
"chat.message": async (input, output) => {
try {
- await sessionInject.injectSessionContext(input, output)
if (config.autoRecall?.enabled) {
await recall.injectRelevantMemories(input, output)
}
+ await sessionInject.injectSessionContext(input, output)
} catch (error) { ... }
}
Option A is lower-risk (one-character semantic change, no ordering concerns); Option B avoids the synthetic-filter path entirely but changes hook ordering.
Related upstream work
- #3298 / #3313 propose a separate archive-fallback path for Codex/Claude Code, but explicitly leave the OpenCode plugin unchanged ("other integrations remain behaviorally unchanged"). This issue is about the base auto-recall path in the OpenCode plugin itself, which #3313 does not touch.