fix(opencode): fix memory-inject race condition, empty-memory ban, add turn limit

Three critical fixes to the iai-mcp-memory-inject.js plugin:

- Race condition: inflight is now a Map<sessionID, Promise> so concurrent
  callers (warm-on-create event + first transform hook) await the same
  fetch instead of getting empty string. First turn now gets memory.
- Empty-memory ban: fetchMemory returns {ok, text} tuple. HTTP 200 with
  empty body is memoized as valid (new user, minimal mode). Only actual
  network errors count against the retry budget.
- Turn-limited injection: new INJECT_MAX_TURNS (default 3) stops injecting
  after N turns. Memory was already in system prompt; the model has seen
  it. Controls per-turn token cost that was 50x the old approach.

Also: README now documents memory-inject.js (not session-init), and
session-init.js is marked @deprecated.
This commit is contained in:
Apunkt 2026-06-03 16:47:55 +02:00
parent 4f6b91aef8
commit 7559bac57f
No known key found for this signature in database
3 changed files with 69 additions and 24 deletions

View file

@ -1,8 +1,17 @@
/**
* iai-mcp session-init plugin for OpenCode.
* @deprecated Use `iai-mcp-memory-inject.js` instead.
*
* Sends SDK prompt on session.updated to inject the system instruction.
* The model calls memory_session_context on every new session.
* This plugin forces a tool call via a phantom "INIT" user turn, which
* corrupts session title generation. The memory-inject plugin uses the
* system.transform hook to inject memory directly into the system prompt
* no phantom turn, clean titles, memory visible every turn.
*
* Retained temporarily for backward compatibility. Will be removed in a
* future release. Migrate to iai-mcp-memory-inject.js:
*
* cp deploy/opencode/iai-mcp-memory-inject.js ~/.config/opencode/plugins/
*
* Then replace this plugin reference in ~/.config/opencode/config.json.
*/
export const IaiMcpSessionInit = async ({