* 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>
139 lines
5.1 KiB
TypeScript
139 lines
5.1 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
import { deriveBankId, ensureBankMission } from './bank.js';
|
|
import { makeConfig } from './test-helpers.js';
|
|
|
|
describe('deriveBankId', () => {
|
|
const originalEnv = { ...process.env };
|
|
|
|
afterEach(() => {
|
|
process.env = { ...originalEnv };
|
|
});
|
|
|
|
it('returns default bank name in static mode', () => {
|
|
expect(deriveBankId(makeConfig(), '/home/user/project')).toBe('opencode');
|
|
});
|
|
|
|
it('returns configured bankId in static mode', () => {
|
|
const config = makeConfig({ bankId: 'my-bank' });
|
|
expect(deriveBankId(config, '/home/user/project')).toBe('my-bank');
|
|
});
|
|
|
|
it('adds prefix in static mode', () => {
|
|
const config = makeConfig({ bankIdPrefix: 'dev', bankId: 'my-bank' });
|
|
expect(deriveBankId(config, '/home/user/project')).toBe('dev-my-bank');
|
|
});
|
|
|
|
it('composes from granularity fields in dynamic mode', () => {
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
dynamicBankGranularity: ['agent', 'project'],
|
|
agentName: 'opencode',
|
|
});
|
|
expect(deriveBankId(config, '/home/user/my-project')).toBe('opencode::my-project');
|
|
});
|
|
|
|
it('uses default granularity when not specified', () => {
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
dynamicBankGranularity: [],
|
|
});
|
|
expect(deriveBankId(config, '/home/user/proj')).toBe('opencode::proj');
|
|
});
|
|
|
|
it('URL-encodes special characters', () => {
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
dynamicBankGranularity: ['project'],
|
|
});
|
|
expect(deriveBankId(config, '/home/user/my project')).toBe('my%20project');
|
|
});
|
|
|
|
it('uses channel/user from env vars', () => {
|
|
process.env.HINDSIGHT_CHANNEL_ID = 'slack-general';
|
|
process.env.HINDSIGHT_USER_ID = 'user123';
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
dynamicBankGranularity: ['agent', 'channel', 'user'],
|
|
});
|
|
expect(deriveBankId(config, '/home/user/proj')).toBe('opencode::slack-general::user123');
|
|
});
|
|
|
|
it('uses defaults for missing env vars', () => {
|
|
delete process.env.HINDSIGHT_CHANNEL_ID;
|
|
delete process.env.HINDSIGHT_USER_ID;
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
dynamicBankGranularity: ['channel', 'user'],
|
|
});
|
|
expect(deriveBankId(config, '/home/user/proj')).toBe('default::anonymous');
|
|
});
|
|
|
|
it('adds prefix in dynamic mode', () => {
|
|
const config = makeConfig({
|
|
dynamicBankId: true,
|
|
bankIdPrefix: 'dev',
|
|
dynamicBankGranularity: ['agent'],
|
|
});
|
|
expect(deriveBankId(config, '/home/user/proj')).toBe('dev-opencode');
|
|
});
|
|
});
|
|
|
|
describe('ensureBankMission', () => {
|
|
it('calls createBank on first use', async () => {
|
|
const client = { createBank: vi.fn().mockResolvedValue({}) } as any;
|
|
const missionsSet = new Set<string>();
|
|
const config = makeConfig({ bankMission: 'Test mission' });
|
|
|
|
await ensureBankMission(client, 'test-bank', config, missionsSet);
|
|
|
|
expect(client.createBank).toHaveBeenCalledWith('test-bank', {
|
|
reflectMission: 'Test mission',
|
|
retainMission: undefined,
|
|
});
|
|
expect(missionsSet.has('test-bank')).toBe(true);
|
|
});
|
|
|
|
it('skips if already set', async () => {
|
|
const client = { createBank: vi.fn() } as any;
|
|
const missionsSet = new Set(['test-bank']);
|
|
const config = makeConfig({ bankMission: 'Test mission' });
|
|
|
|
await ensureBankMission(client, 'test-bank', config, missionsSet);
|
|
|
|
expect(client.createBank).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('skips if no mission configured', async () => {
|
|
const client = { createBank: vi.fn() } as any;
|
|
const missionsSet = new Set<string>();
|
|
const config = makeConfig({ bankMission: '' });
|
|
|
|
await ensureBankMission(client, 'test-bank', config, missionsSet);
|
|
|
|
expect(client.createBank).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not throw on client error', async () => {
|
|
const client = { createBank: vi.fn().mockRejectedValue(new Error('Network error')) } as any;
|
|
const missionsSet = new Set<string>();
|
|
const config = makeConfig({ bankMission: 'Mission' });
|
|
|
|
await expect(
|
|
ensureBankMission(client, 'test-bank', config, missionsSet),
|
|
).resolves.not.toThrow();
|
|
expect(missionsSet.has('test-bank')).toBe(false);
|
|
});
|
|
|
|
it('passes retainMission when configured', async () => {
|
|
const client = { createBank: vi.fn().mockResolvedValue({}) } as any;
|
|
const missionsSet = new Set<string>();
|
|
const config = makeConfig({ bankMission: 'Reflect', retainMission: 'Extract carefully' });
|
|
|
|
await ensureBankMission(client, 'test-bank', config, missionsSet);
|
|
|
|
expect(client.createBank).toHaveBeenCalledWith('test-bank', {
|
|
reflectMission: 'Reflect',
|
|
retainMission: 'Extract carefully',
|
|
});
|
|
});
|
|
});
|