Skip to content

fix(opencode): inject recalled context via system.transform hook#207

Merged
pszymkowiak merged 1 commit into
developfrom
fix/opencode-context-injection
May 10, 2026
Merged

fix(opencode): inject recalled context via system.transform hook#207
pszymkowiak merged 1 commit into
developfrom
fix/opencode-context-injection

Conversation

@pszymkowiak
Copy link
Copy Markdown
Contributor

Summary

Closes #169.

The previous `session.created` handler only logged a debug line — it never actually delivered recalled memories into the session. OpenCode's `session.created` is observational; the API for context injection is `experimental.chat.system.transform`, which fires before each model call and lets a plugin push extra system-prompt strings.

Fix

New Layer 3 handler in the plugin that:

  • Runs once per session (de-dup by sessionID so the wake-up doesn't re-inject on every chat turn).
  • Captures `icm wake-up --project

    ` (critical/high facts + preferences) → pushes to `output.system`.

  • Captures `icm recall-project --limit 5` (top project memories) → pushes to `output.system`.

Both calls are best-effort — a missing binary or empty memory store returns "" and silently no-ops.

Why `experimental.chat.system.transform`

Verified against `@opencode-ai/plugin@1.14.46`:

```ts
"experimental.chat.system.transform"?: (input: { sessionID?, model }, output: { system: string[] }) => Promise
```

This is the only hook that lets a plugin add to the system prompt. `chat.params` only modifies model parameters (temperature etc.), `chat.message` mutates the user message itself.

Test plan

  • Smoke-tested both CLI commands the plugin shells out to (`icm wake-up --project icm`, `icm recall-project --limit 3`) — both produce non-empty stdout
  • `tsc --strict` against `@opencode-ai/plugin@1.14.46` — clean
  • `cargo test --workspace` — 423 passed (no Rust changes; verified suite still green)
  • `cargo clippy --all-targets -- -D warnings` — clean
  • Other layers preserved: tool.execute.after extraction, experimental.session.compacting extraction, session.created log

🤖 Generated with Claude Code

Closes #169.

The `session.created` handler only logged a debug line — it never
actually delivered recalled memories into the session. OpenCode's
`session.created` is observational; the API for context injection
is `experimental.chat.system.transform`, which fires before each
model call and lets a plugin push extra system-prompt strings.

Add a Layer 3 handler that:

- Runs once per session (de-dup by sessionID so the wake-up doesn't
  re-inject on every chat turn).
- Captures `icm wake-up --project <p>` (critical/high facts +
  preferences) and pushes it to `output.system`.
- Captures `icm recall-project --limit 5` (top project memories)
  and pushes it too.

Both calls are best-effort — a missing binary or empty memory store
returns "" and silently no-ops.

Type-checked with `tsc --strict` against @opencode-ai/plugin@1.14.46.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@pszymkowiak pszymkowiak merged commit f85ce05 into develop May 10, 2026
7 checks passed
@pszymkowiak pszymkowiak deleted the fix/opencode-context-injection branch May 10, 2026 08:59
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.

1 participant