fleet-memory/hindsight-docs/docs-integrations/opencode.md
DK09876 e1c6220f0e
feat: add OpenCode persistent memory plugin (#853)
* feat: add OpenCode persistent memory plugin

Add hindsight-opencode integration with:
- Three custom tools: hindsight_retain, hindsight_recall, hindsight_reflect
- Auto-retain on session.idle with document_id deduplication
- Memory injection on session start via system transform hook
- Memory preservation during context window compaction
- Sliding window retain with retainOverlapTurns support
- 4-level config hierarchy (defaults, user file, plugin options, env vars)
- Dynamic bank ID derivation (agent, project, channel, user dimensions)
- CI job, release script entry, docs page

79 tests across 6 test files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address review findings for opencode integration

1. Pre-compaction retain now uses shared retainSession() helper,
   respecting retainMode, documentId, and session_id metadata
   consistently with idle-retain (was bypassing retention policy).

2. System transform recall is only consumed after successful injection.
   If Hindsight is briefly unavailable, the plugin retries on the next
   LLM call instead of permanently skipping recall for the session.

3. Config validation for retainMode and recallBudget — typos like
   "full_session" or "maximum" now log a warning and fall back to
   the default instead of silently changing retention semantics.

85 tests (6 new covering compaction documentId, recall retry, and
config validation).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: docs/tools findings from second review round

1. Remove "session" from supported dynamic bank fields in docs —
   the implementation can't vary bank ID per session since it's
   derived once at plugin startup.

2. Explicit tools (retain, reflect) now call ensureBankMission()
   before API calls, so bankMission/retainMission are applied even
   when the agent uses tools exclusively without triggering hooks.

3. Added tests for mission setup via tools path.

88 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: recall retry semantics and README bank scoping clarity

1. recallForContext now returns { context, ok } to distinguish
   "no results" (ok=true) from "API error" (ok=false). System
   transform consumes the session on ok=true even with 0 results,
   so empty banks don't cause repeated queries. Only transient API
   failures preserve retry.

2. README clarifies that channel/user bank dimensions are process-
   scoped (set via env vars before launch), not per-session dynamic
   within a running OpenCode process.

89 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: review fixes for opencode integration

- Rename CI job from build-opencode-integration to test-opencode-integration
  to match naming convention for integrations that run tests
- Fix tsconfig module resolution to Node16 (consistent with other integrations)
- Extract shared makeConfig test helper to avoid duplication across 3 test files

* fix: remove unused PluginState import from tools.ts

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Nicolò Boschi <boschi1997@gmail.com>
2026-04-07 10:11:57 +02:00

4.6 KiB

sidebar_position title description
20 OpenCode Persistent Memory with Hindsight | Integration Add long-term memory to OpenCode with Hindsight. Automatically captures conversations and recalls relevant context across coding sessions.

OpenCode

Persistent long-term memory plugin for OpenCode using Hindsight. Automatically captures conversations, recalls relevant context on session start, and provides retain/recall/reflect tools the agent can call directly.

Quick Start

# 1. Install the plugin
npm install @vectorize-io/opencode-hindsight

Add to your opencode.json:

{
  "plugin": ["@vectorize-io/opencode-hindsight"]
}
# 2. Configure your Hindsight server
export HINDSIGHT_API_URL="http://localhost:8888"

# Optional: API key for Hindsight Cloud
export HINDSIGHT_API_TOKEN="your-api-key"

# 3. Start OpenCode — the plugin activates automatically
opencode

Features

Custom Tools

The plugin registers three tools the agent can call explicitly:

Tool Description
hindsight_retain Store information in long-term memory
hindsight_recall Search long-term memory for relevant information
hindsight_reflect Generate a synthesized answer from long-term memory

Auto-Retain

When the session goes idle (session.idle event), the plugin automatically retains the conversation transcript to Hindsight. Configurable via retainEveryNTurns to control frequency.

Session Recall

When a new session starts, the plugin recalls relevant project context and injects it into the system prompt, giving the agent access to memories from prior sessions.

Compaction Hook

When OpenCode compacts the context window, the plugin:

  1. Retains the current conversation before compaction
  2. Recalls relevant memories and injects them into the compaction context

This ensures memories survive context window trimming.

Configuration

Plugin Options

{
  "plugin": [
    ["@vectorize-io/opencode-hindsight", {
      "hindsightApiUrl": "http://localhost:8888",
      "hindsightApiToken": "your-api-key",
      "bankId": "my-project",
      "autoRecall": true,
      "autoRetain": true,
      "recallBudget": "mid",
      "retainEveryNTurns": 10,
      "debug": false
    }]
  ]
}

Config File

Create ~/.hindsight/opencode.json for persistent configuration that applies across all projects:

{
  "hindsightApiUrl": "http://localhost:8888",
  "hindsightApiToken": "your-api-key",
  "recallBudget": "mid"
}

Environment Variables

Variable Description Default
HINDSIGHT_API_URL Hindsight API base URL (required)
HINDSIGHT_API_TOKEN API key for authentication
HINDSIGHT_BANK_ID Static memory bank ID opencode
HINDSIGHT_AGENT_NAME Agent name for dynamic bank IDs opencode
HINDSIGHT_AUTO_RECALL Auto-recall on session start true
HINDSIGHT_AUTO_RETAIN Auto-retain on session idle true
HINDSIGHT_RETAIN_MODE full-session or last-turn full-session
HINDSIGHT_RECALL_BUDGET Recall budget: low, mid, high mid
HINDSIGHT_RECALL_MAX_TOKENS Max tokens for recall results 1024
HINDSIGHT_DYNAMIC_BANK_ID Enable dynamic bank ID derivation false
HINDSIGHT_BANK_MISSION Bank mission/context for reflect
HINDSIGHT_DEBUG Enable debug logging to stderr false

Configuration priority (later wins): defaults < ~/.hindsight/opencode.json < plugin options < env vars.

Dynamic Bank IDs

For multi-project isolation, enable dynamic bank ID derivation:

export HINDSIGHT_DYNAMIC_BANK_ID=true

The bank ID is composed from granularity fields (default: agent::project). Supported fields: agent, project, channel, user.

For multi-user scenarios (e.g., shared agent serving multiple users):

export HINDSIGHT_CHANNEL_ID="slack-general"
export HINDSIGHT_USER_ID="user123"

How It Works

  1. Plugin loads when OpenCode starts — creates a HindsightClient, derives the bank ID, and registers tools + hooks
  2. Session startssession.created event triggers, plugin marks session for recall injection
  3. System transform — on the first LLM call, recalled memories are injected into the system prompt
  4. Agent works — can call hindsight_recall and hindsight_retain explicitly during the session
  5. Session idlessession.idle event triggers auto-retain of the conversation
  6. Compaction — if the context window fills up, memories are preserved through the compaction