@jmtrin/opencode-kevin 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +580 -360
  2. package/dist/migrations/006_v05_glassbox.sql +118 -0
  3. package/dist/migrations/007_v06_pull.sql +145 -0
  4. package/dist/plugin/Archiver.d.ts +42 -0
  5. package/dist/plugin/Archiver.js +99 -0
  6. package/dist/plugin/Archiver.js.map +1 -0
  7. package/dist/plugin/ArtifactWriter.d.ts +50 -0
  8. package/dist/plugin/ArtifactWriter.js +240 -0
  9. package/dist/plugin/ArtifactWriter.js.map +1 -0
  10. package/dist/plugin/ContextInjector.d.ts +87 -0
  11. package/dist/plugin/ContextInjector.js +212 -44
  12. package/dist/plugin/ContextInjector.js.map +1 -1
  13. package/dist/plugin/Curator.d.ts +96 -0
  14. package/dist/plugin/Curator.js +252 -0
  15. package/dist/plugin/Curator.js.map +1 -0
  16. package/dist/plugin/Feedback.d.ts +67 -0
  17. package/dist/plugin/Feedback.js +138 -0
  18. package/dist/plugin/Feedback.js.map +1 -0
  19. package/dist/plugin/InjectionLedger.d.ts +9 -2
  20. package/dist/plugin/InjectionLedger.js +63 -9
  21. package/dist/plugin/InjectionLedger.js.map +1 -1
  22. package/dist/plugin/Materializer.d.ts +59 -0
  23. package/dist/plugin/Materializer.js +237 -0
  24. package/dist/plugin/Materializer.js.map +1 -0
  25. package/dist/plugin/MemoryService.d.ts +38 -0
  26. package/dist/plugin/MemoryService.js +265 -30
  27. package/dist/plugin/MemoryService.js.map +1 -1
  28. package/dist/plugin/Migrate.js +41 -0
  29. package/dist/plugin/Migrate.js.map +1 -1
  30. package/dist/plugin/QualityGate.d.ts +53 -0
  31. package/dist/plugin/QualityGate.js +50 -8
  32. package/dist/plugin/QualityGate.js.map +1 -1
  33. package/dist/plugin/Retrospective.d.ts +1 -0
  34. package/dist/plugin/Retrospective.js +24 -1
  35. package/dist/plugin/Retrospective.js.map +1 -1
  36. package/dist/plugin/capabilities.d.ts +15 -0
  37. package/dist/plugin/capabilities.js +42 -0
  38. package/dist/plugin/capabilities.js.map +1 -0
  39. package/dist/plugin/confidence.d.ts +3 -1
  40. package/dist/plugin/confidence.js +14 -2
  41. package/dist/plugin/confidence.js.map +1 -1
  42. package/dist/plugin/diff.d.ts +8 -0
  43. package/dist/plugin/diff.js +183 -0
  44. package/dist/plugin/diff.js.map +1 -0
  45. package/dist/plugin/index.d.ts +3 -1
  46. package/dist/plugin/index.js +371 -0
  47. package/dist/plugin/index.js.map +1 -1
  48. package/dist/plugin/inferability.d.ts +32 -0
  49. package/dist/plugin/inferability.js +89 -0
  50. package/dist/plugin/inferability.js.map +1 -0
  51. package/dist/plugin/kevin_approve.d.ts +34 -0
  52. package/dist/plugin/kevin_approve.js +50 -0
  53. package/dist/plugin/kevin_approve.js.map +1 -0
  54. package/dist/plugin/kevin_audit.d.ts +95 -0
  55. package/dist/plugin/kevin_audit.js +233 -0
  56. package/dist/plugin/kevin_audit.js.map +1 -0
  57. package/dist/plugin/kevin_propose.d.ts +23 -0
  58. package/dist/plugin/kevin_propose.js +15 -0
  59. package/dist/plugin/kevin_propose.js.map +1 -0
  60. package/dist/plugin/kevin_publish.d.ts +38 -0
  61. package/dist/plugin/kevin_publish.js +19 -0
  62. package/dist/plugin/kevin_publish.js.map +1 -0
  63. package/dist/plugin/kevin_why.js +23 -2
  64. package/dist/plugin/kevin_why.js.map +1 -1
  65. package/dist/plugin/metrics.d.ts +32 -1
  66. package/dist/plugin/metrics.js +86 -2
  67. package/dist/plugin/metrics.js.map +1 -1
  68. package/dist/plugin/replay-types.d.ts +327 -0
  69. package/dist/plugin/replay-types.js +65 -0
  70. package/dist/plugin/replay-types.js.map +1 -0
  71. package/dist/plugin/replay.d.ts +36 -0
  72. package/dist/plugin/replay.js +203 -0
  73. package/dist/plugin/replay.js.map +1 -0
  74. package/migrations/006_v05_glassbox.sql +118 -0
  75. package/migrations/007_v06_pull.sql +145 -0
  76. package/package.json +3 -2
package/README.md CHANGED
@@ -1,360 +1,580 @@
1
- # Kevin
2
-
3
- > Observe and learn: the learning layer OpenCode was missing.
4
-
5
- Kevin is an [OpenCode](https://opencode.ai) plugin that **observes** every agent tool call, **learns** from failures by generating lessons, and **shares** what it learned proactively in future sessions. It does not plan, orchestrate, or compete with the plugin ecosystem. It only learns.
6
-
7
- - **Local-first**: SQLite + FTS5, no external services, no network calls.
8
- - **Global memory**: a single `~/.opencode-kevin/kevin.db` shared across all your projects (WAL mode → safe for concurrent sessions). No per-project folders.
9
- - **Knowledge + Causality (v0.3.0)**: causal failure→fix chains, `kevin_why` explanations, OKF export/import, a supersede model, and human-in-the-loop AGENTS.md suggestions.
10
- - **Signal over Noise (v0.4.0)**: a quality gate that stores weak lessons without injecting them, an injection ledger with honest `precision_rate`, two-sided confidence, and a fixed compacting hook.
11
- - **Audited**: the v0.4.0 bug catalog (`docs/Kevin_v0.4.0_Bugs.md`) is fully closed 16/16 bugs fixed and regression-tested (evidence in `kevin_query`/`kevin_get`, OKF round-trip fidelity, causal refresh guard, redaction precision, cross-session isolation).
12
- - **Standalone**: works without any other plugin. With the ecosystem, it learns more richly.
13
-
14
- ---
15
-
16
- ## Installation
17
-
18
- ### 1. Declare the plugin
19
-
20
- Add Kevin to your OpenCode config. For **all projects** (global):
21
-
22
- ```jsonc
23
- // ~/.config/opencode/opencode.jsonc
24
- {
25
- "$schema": "https://opencode.ai/config.json",
26
- "plugin": [
27
- "@jmtrin/opencode-kevin@latest"
28
- ]
29
- }
30
- ```
31
-
32
- For a **single project**, put the same `plugin` array in `./opencode.json` or `.opencode/opencode.json` at the project root.
33
-
34
- ### 2. Restart OpenCode
35
-
36
- Config is loaded once at startup and is **not hot-reloaded** — quit and reopen OpenCode after editing. On start, OpenCode resolves the npm spec, caches the plugin in `~/.cache/opencode/packages/@jmtrin/opencode-kevin/`, and exposes ten tools: `kevin_save`, `kevin_query`, `kevin_get`, `kevin_recall`, `kevin_status`, `kevin_retrospective`, `kevin_why`, `kevin_export`, `kevin_import`, `kevin_config`.
37
-
38
- ### 3. Where data lives
39
-
40
- Kevin stores everything in a single **global, shared** location under your home directory — no per-project `.kevin/` folders:
41
-
42
- | Path | Content |
43
- |---|---|
44
- | `~/.opencode-kevin/kevin.db` | SQLite database (memories, tool calls, retrospectives). WAL mode → safe for concurrent OpenCode sessions across projects. |
45
- | `~/.opencode-kevin/retrospectives/<session>.md` | Per-session retrospective markdown. |
46
-
47
- Migrations run automatically on startup.
48
-
49
- ### Requirements
50
-
51
- - **Node.js >= 22.5** (uses `node:sqlite`, the built-in SQLite module — no native binaries to compile).
52
- - OpenCode with plugin support (`@opencode-ai/plugin` >= 1.17).
53
-
54
- > **Runtimes**:
55
- > - **Bun**: uses `bun:sqlite` (built-in).
56
- > - **Node 24+**: uses `node:sqlite` directly, no flags needed (emits an experimental warning, harmless).
57
- > - **Node 22/23 without `--experimental-sqlite` flag** or **Node 20**: falls back to `better-sqlite3`, declared as `optionalDependencies`. If you need it, install it manually in your opencode config directory (`~/.config/opencode/`): `npm install better-sqlite3`.
58
-
59
- ### Verification
60
-
61
- ```bash
62
- npm run verify
63
- ```
64
-
65
- Checks Node version, SQLite, migration, MemoryService save/query, Reflector, ContextInjector, and TypeScript strict mode.
66
-
67
- ### Advanced (optional)
68
-
69
- Override defaults via the plugin tuple form:
70
-
71
- ```jsonc
72
- {
73
- "plugin": [
74
- ["@jmtrin/opencode-kevin", {
75
- "dbPath": "/custom/path/kevin.db",
76
- "retrospectivesDir": "/custom/path/retrospectives",
77
- "throttleMs": 120000
78
- }]
79
- ]
80
- }
81
- ```
82
-
83
- Use `:memory:` for `dbPath` in tests.
84
-
85
- ---
86
-
87
- ## Cycle: Observe → Learn → Share
88
-
89
- ```
90
- Tool call (success or failure)
91
-
92
-
93
- ┌─────────────────┐
94
- │ OBSERVE │ ToolCallObserver records every call
95
- │ ToolCallObserver│ (tool, args redacted, success, duration, error_type,
96
- │ │ id = callID) + stripPrivate + opt-in dedup (v0.2.0)
97
- └────────┬────────┘
98
- │ on failure │ on success
99
- ▼ ▼
100
- ┌─────────────────┐ ┌─────────────────────────┐
101
- │ LEARN │ │ CAUSAL CHAIN (v0.3.0) │
102
- │ Reflector │ │ CausalChain.onSuccess │
103
- │ heuristic lesson │ │ links the fix to the │
104
- │ per-error-code │ │ failure within 10 tool │
105
- │ rule table │ │ calls (error_fingerprint)
106
- │ (v0.2.0 v2), │ └───────────┬─────────────┘
107
- │ per-fingerprint │ │
108
- │ throttle BEFORE │ │ session.idle
109
- │ LLM enrich │ ▼
110
- (opt-in v0.3.0),│ ┌─────────────────────────┐
111
- │ stamps │ │ CausalChain.onSessionIdle│
112
- error_fingerprint│ │ promotes recurring errors│
113
- └────────┬────────┘ causal patterns (idempotent,
114
- │ │ cumulative evidence)
115
- ▼ └───────────┬───────────────┘
116
- ┌─────────────────┐
117
- SHARE │◄──────────────┘
118
- ContextInjector injects relevant lessons pre-prompt
119
- (1500 tokens) + on compacting (2000)
120
- + <protect> + id: line (v0.2.0)
121
- + origin-aware rank (v0.2.0)
122
- + <kevin-suggestion> after negative
123
- │ feedback half (HITL, v0.3.0)
124
- └────────┬────────┘
125
- │ session.idle
126
-
127
- ┌─────────────────┐
128
- RETROSPECTIVE generates ~/.opencode-kevin/retrospectives/<session>.md
129
- with origin labels, FP recap, metrics snapshot (v0.2.0)
130
- + boostPositiveReflectors (v0.2.0)
131
- │ │ + penalizeRecurringReflectors (v0.3.0)
132
- │ + PatternMiner.mine (opt-in, v0.2.0)
133
- └─────────────────┘
134
- ```
135
-
136
- ---
137
-
138
- ## Tools
139
-
140
- Kevin exposes 10 tools callable by the agent:
141
-
142
- ### `kevin_save`
143
-
144
- Saves an explicit memory.
145
-
146
- ```
147
- kevin_save({ type: "decision", content: "We use vitest for tests", scope: "project" })
148
- // → { "id": "0195a3b2-..." }
149
- ```
150
-
151
- `type`: `error` | `pattern` | `decision` | `context` | `rule` | `solution` (v0.3.0). `scope`: `project` (persists) | `session` (TTL 24h).
152
-
153
- Saving a `decision` or `rule` with the same `fingerprint` as an existing active row supersedes the old one (v0.3.0 — `status='superseded'`, hidden from default queries).
154
-
155
- ### `kevin_query`
156
-
157
- Searches memories by text (FTS5 + bm25). Returns a **slim** payload by default (v0.2.0). Pass `full: true` for the v0.1.x full content body, or `evidence: true` (v0.3.0) to include `confidence`, `evidence_count`, `last_verified_at` in the slim payload.
158
-
159
- ```
160
- kevin_query({ query: "typecheck", type: "error", limit: 5 })
161
- // → [{ "id": "...", "type": "error", "scope": "project", "score": -0.87, "snippet": "When bash fails with typecheck:..." }, ...]
162
-
163
- kevin_query({ query: "typecheck", type: "error", limit: 5, full: true })
164
- // → [{ "id": "...", "type": "error", "content": "...", "scope": "project" }, ...]
165
- ```
166
-
167
- ### `kevin_get`
168
-
169
- Fetches a **single full memory** by id (v0.2.0 — progressive disclosure). Use when `kevin_query` returns a slim snippet and you need the complete content.
170
-
171
- ```
172
- kevin_get({ id: "0195a3b2-..." })
173
- // → { "id": "...", "type": "error", "content": "...", "scope": "project",
174
- // "relevanceScore": 0.55, "origin": "reflector", "fingerprint": "cbf29ce484222325",
175
- // "projectId": null, "metadata": null,
176
- // "evidenceCount": 2, "recurrenceCount": 1, "lastVerifiedAt": "2026-08-01 10:00:00",
177
- // "status": "active", "confidence": 0.55, "fixArgs": "npm i -g rg" }
178
- ```
179
-
180
- ### `kevin_recall`
181
-
182
- Retrieves relevant memories (greedy fill by relevance). Without `query`, returns all memories in scope. Pass `includeSuperseded: true` to include superseded rows (v0.3.0).
183
-
184
- ```
185
- kevin_recall({ query: "auth", limit: 3 })
186
- // → [{ "id": "...", "type": "decision", ... }, ...]
187
- ```
188
-
189
- ### `kevin_status`
190
-
191
- Global counts and metrics. v0.2.0 adds `memories_reflector`, `memories_agent`, `memories_pattern` and a `metrics` object; v0.3.0 adds `memories_causal` and 3 more seeded counters (`patterns_causal`, `causal_links`, `memories_superseded`); v0.4.0 adds the precision block: `injections_total`, `injections_effective`, `injections_ineffective`, `precision_rate`, `patterns_promoted_new`, and per-origin `recurrence_by_origin`.
192
-
193
- ```
194
- kevin_status({})
195
- // → { "memories": 42, "memories_reflector": 12, "memories_agent": 30, "memories_pattern": 0,
196
- // "memories_causal": 1, "tool_calls": 318, "retrospectives": 7,
197
- // "metrics": { "tokens_injected_pre_prompt": 51, "tokens_injected_compacting": 0,
198
- // "reflections_throttled": 3, "duplicate_suppressions": 2,
199
- // "tool_calls_deduped": 0, "patterns_mined": 0,
200
- // "patterns_causal": 1, "causal_links": 2, "memories_superseded": 0 },
201
- // "injections_total": 14, "injections_effective": 11, "injections_ineffective": 3,
202
- // "precision_rate": 0.79, "patterns_promoted_new": 2,
203
- // "recurrence_by_origin": { "reflector": 3, "causal": 1 } }
204
- ```
205
-
206
- ### `kevin_retrospective`
207
-
208
- Generates a retrospective for a session (uses current session if `session_id` is omitted).
209
-
210
- ```
211
- kevin_retrospective({ session_id: "sess-abc" })
212
- // { "file_path": "~/.opencode-kevin/retrospectives/sess-abc.md" }
213
- // or → { "message": "No failures in session sess-abc." }
214
- ```
215
-
216
- ### `kevin_why` (v0.3.0)
217
-
218
- Explains *why* a failure keeps happening: looks up causal patterns for the query and builds a failure → fix trace from memories + tool_calls, including related TypeScript error-code rules.
219
-
220
- ```
221
- kevin_why({ query: "TS2304 cannot find name" })
222
- // → { "summary": "TS2304 recurs because ... Confirmed by 2 fixes.",
223
- // "confidence": 0.7, "evidence_count": 2, "last_verified": "2026-08-01 10:00:00",
224
- // "trace": [ { "type": "error", "summary": "..." }, { "type": "fix", "tool": "bash" } ],
225
- // "related_rules": [ { "code": "TS2304", "suggestion": "import or typo" } ] }
226
- ```
227
-
228
- ### `kevin_export` (v0.3.0)
229
-
230
- Exports knowledge for sharing: `decision`/`rule`/`pattern` memories (active only, no raw errors) as YAML-frontmatter blocks (`format: "okf"`) or markdown (`format: "markdown"`). Includes `id`, `type`, `confidence` (two-sided v0.4.0 formula), `evidence_count`, `recurrence_count`, `last_verified_at`, `fingerprint`. Timestamps are treated as UTC — a re-import reproduces the exact source values.
231
-
232
- ### `kevin_import` (v0.3.0)
233
-
234
- Ingests an exported bundle. Each entry becomes a `context` memory with `origin='imported'`; a fingerprint collision with an existing `decision`/`rule` supersedes the old row. Returns `{ imported, superseded }`.
235
-
236
- ### `kevin_config` (v0.4.0)
237
-
238
- Reads/writes `kevin_settings` without SQL. `action: "list"` returns every setting; `action: "set"` upserts a value (default `"1"` when omitted) and rejects unknown keys unless `strict: false`.
239
-
240
- ```
241
- kevin_config({ action: "list" })
242
- // { "quality_gate_enabled": "1", "lesson_snippet_injection": "1", "llm_reflection_enabled": "0", ... }
243
-
244
- kevin_config({ action: "set", key: "quality_gate_enabled", value: "0" })
245
- // → { "ok": true }
246
- ```
247
-
248
- Known keys: `quality_gate_enabled`, `lesson_snippet_injection`, `llm_reflection_enabled`, `cross_project_enabled`, `patternminer_enabled`, `tool_calls_dedup_enabled` (v0.4.0).
249
-
250
- ---
251
-
252
- ## Precision (v0.4.0)
253
-
254
- Weak lessons errors the reflector cannot dispatch to a deterministic rule — are **stored but never injected** while `quality_gate_enabled = '1'` (default). Injection now goes through a ledger: every pre-prompt/compacting injection is recorded and settled as effective or ineffective at session idle, so `kevin_status` reports the honest picture (`injections_total`, `injections_effective/ineffective`, `precision_rate`, `patterns_promoted_new`) instead of raw "lessons shared" counts. Recurrences demote lessons (`recurrence_count` → `stale`) and lower confidence. Debug mode: `kevin_config({ action: "set", key: "quality_gate_enabled", value: "0" })` re-injects weak lessons with a `(low confidence)` marker.
255
-
256
- ---
257
-
258
- ## Hooks
259
-
260
- Kevin subscribes to 6 OpenCode hooks:
261
-
262
- | Hook | What Kevin does |
263
- |---|---|
264
- | `tool.execute.before` | Records tool call start (callID + redacted args) |
265
- | `tool.execute.after` | Records result (id = callID); on failure → Reflector.invoke async (throttled); on success → CausalChain.onSuccess links the fix (v0.3.0) |
266
- | `experimental.chat.system.transform` | Injects relevant lessons in `<kevin-context>` (1500 tokens) + optional `<kevin-suggestion>` (v0.3.0) |
267
- | `experimental.session.compacting` | Re-injects lessons in `<kevin-memory>` after compacting (2000 tokens) + optional `<kevin-suggestion>` |
268
- | `event` (`session.created`) | Captures current `sessionID` |
269
- | `event` (`session.idle`) | Generates retrospective.md; boosts positive lessons (v0.2.0); penalizes recurring failures (v0.3.0); promotes causal patterns + mines patterns (opt-in); flushes metrics |
270
-
271
- **Redaction**: absolute paths (`C:\Users\...`, `/home/...`) → `<path>` and secrets (`API_KEY=`, `Bearer`, `token`) → `<redacted>` before persisting anything. v0.2.0 adds `<private>…</private>` block redaction: sweeps tool call args and output before persistence, replaces with `<private: redacted N chars>`.
272
-
273
- **Throttle**: Reflector generates at most 1 lesson per minute per unique fingerprint (v0.2.0: per-fingerprint, not global). Configurable via `throttleMs`.
274
-
275
- **Truncation**: content > 4KB keeps the lesson searchable; only the additional context is truncated (`metadata.truncated = true`).
276
-
277
- ---
278
-
279
- ## Configuration
280
-
281
- Kevin accepts options via the plugin's tuple form (see Installation Advanced). Programmatic defaults:
282
-
283
- ```ts
284
- import { KevinPlugin } from "@jmtrin/opencode-kevin";
285
-
286
- // defaults
287
- KevinPlugin(input, {
288
- dbPath: "~/.opencode-kevin/kevin.db", // or ":memory:" for tests
289
- migrationsDir: "<package>/dist/migrations", // resolved automatically
290
- retrospectivesDir: "~/.opencode-kevin/retrospectives",
291
- throttleMs: 60_000,
292
- });
293
- ```
294
-
295
- ---
296
-
297
- ## Development
298
-
299
- ```bash
300
- git clone https://github.com/jmtrin/opencode-kevin.git
301
- cd opencode-kevin
302
- npm install
303
- npm run typecheck # tsc --noEmit (strict)
304
- npm run lint # biome check .
305
- npm test # vitest run (unit + integration + e2e)
306
- npm run verify # post-install verification
307
- ```
308
-
309
- ### Publishing (maintainer)
310
-
311
- ```bash
312
- npm login # as the jmtrin account that owns the @jmtrin scope
313
- npm publish --access public
314
- ```
315
-
316
- `prepublishOnly` runs `npm run build` (tsc + copy migrations) automatically. The `files` field ships only `dist/plugin`, `dist/migrations`, and `migrations`. `dist/` is gitignored and rebuilt on publish.
317
-
318
- ### Structure
319
-
320
- ```
321
- plugin/
322
- index.ts # Entry point: KevinPlugin
323
- Store.ts # Wrapper SQLite (node:sqlite / bun:sqlite / better-sqlite3 fallback)
324
- Migrate.ts # Idempotent migrations + post-apply hooks
325
- MemoryService.ts # save/query/getRelevant (FTS5 + bm25 + origin-aware rank + supersede)
326
- ToolCallObserver.ts # onBefore/onAfter + redact + inferErrorType + dedup (opt-in)
327
- Reflector.ts # Heuristic lessons + per-fingerprint throttle + lesson v2 + LLM enrich
328
- CausalChain.ts # v0.3.0 — links fixes to failures + promotes causal patterns
329
- ContextInjector.ts # deriveQuery + pre-prompt/compacting injection + <kevin-suggestion>
330
- QualityGate.ts # v0.4.0 weak-lesson gate (stored, not injected by default)
331
- InjectionLedger.ts # v0.4.0 injection ledger + settle precision_rate
332
- Retrospective.ts # Generates retrospective.md + FP recap + metrics snapshot
333
- LessonFixer.ts # v0.4.0 deterministic fix_args capture + promotion enrichment
334
- confidence.ts # v0.4.0 two-sided computeConfidence (evidence + recurrence)
335
- fingerprint.ts # FNV-1a 64-bit (in-house, no node:crypto)
336
- metrics.ts # In-memory counters + debounced flush to kevin_metrics
337
- PatternMiner.ts # Opt-in deterministic 2-gram/3-gram miner
338
- kevin_why.ts # v0.3.0 — kevin_why tool: failure→fix traces + related rules
339
- okf-export.ts # v0.3.0kevin_export: OKF/markdown export
340
- okf-import.ts # v0.3.0 — kevin_import: bundle parser + import
341
- query-tokenizer.ts # v0.4.0 — FTS5 tokenizer for query sanitization
342
- memory-format.ts # escapeInjectedText, formatMemories, <protect> + id: line wrappers
343
- redact.ts # redactPaths + stripPrivate
344
- uuid.ts # UUIDv7
345
- migrations/
346
- 001_initial.sql # schema: memories, tool_calls, retrospectives
347
- 002_indexes.sql # FTS5 + indexes
348
- 003_v02_signal.sql # v0.2.0 Signal Quality: fingerprint, origin, metrics, dedup indexes
349
- 004_v03_knowledge.sql # v0.3.0 Knowledge + Causality: evidence/status/supersede, error_fingerprint
350
- 005_v04_signal.sql # v0.4.0 Signal over Noise: recurrence_count, fix_args, last_injected_at
351
- tests/{unit,integration,e2e}/
352
- scripts/
353
- copy-migrations.mjs # build step: copies *.sql to dist/migrations
354
- verify-install.ts # npm run verify
355
- ```
356
-
357
- ---
358
- ## License
359
-
360
- MIT
1
+ # Kevin
2
+
3
+ > Observe and learn: the learning layer OpenCode was missing.
4
+
5
+ Kevin is an [OpenCode](https://opencode.ai) plugin that **observes** every agent tool call, **learns** from failures by generating lessons, and **shares** what it learned proactively in future sessions. It does not plan, orchestrate, or compete with the plugin ecosystem. It only learns.
6
+
7
+ - **Local-first**: SQLite + FTS5, no external services, no network calls.
8
+ - **Global memory**: a single `~/.opencode-kevin/kevin.db` shared across all your projects (WAL mode → safe for concurrent sessions). No per-project folders.
9
+ - **Knowledge + Causality**: causal failure→fix chains, `kevin_why` explanations, OKF export/import, a supersede model, and human-in-the-loop AGENTS.md suggestions.
10
+ - **Signal over Noise**: a quality gate that stores weak lessons without injecting them, an injection ledger with honest `precision_rate`, and two-sided confidence.
11
+ - **Glass Box**: honest measurement replaces estimates — three-way injection settlement (`effective` / `ineffective` / `inconclusive`), human feedback that actually moves confidence, a strict dry-run `kevin_trace`, a read-only `kevin_audit`, and a hermetic replay harness.
12
+ - **Pull**: knowledge earns its way into files the model actually reads — `kevin_propose` generates a reviewable diff, a human approves, and **only then** does Kevin write, inside a frozen marker block, preserving your file's CRLF/BOM/formatting byte-for-byte outside it. Plus three distribution channels (AGENTS.md, skills, references) and a push budget gated by a confidence floor.
13
+ - **Audited**: the v0.4.0 bug catalog (`docs/Kevin_v0.4.0_Bugs.md`) is fully closed — 16/16 bugs fixed and regression-tested.
14
+ - **Standalone**: works without any other plugin. With the ecosystem, it learns more richly.
15
+
16
+ ---
17
+
18
+ ## Contents
19
+
20
+ - [Installation](#installation)
21
+ - [How Kevin works](#how-kevin-works)
22
+ - [Tools](#tools)
23
+ - [How Kevin measures itself](#how-kevin-measures-itself)
24
+ - [Curation & Pull](#curation--pull)
25
+ - [Replay harness](#replay-harness)
26
+ - [Hooks](#hooks)
27
+ - [Configuration](#configuration)
28
+ - [Development](#development)
29
+ - [License](#license)
30
+
31
+ ---
32
+
33
+ ## Installation
34
+
35
+ ### 1. Declare the plugin
36
+
37
+ Add Kevin to your OpenCode config. For **all projects** (global):
38
+
39
+ ```jsonc
40
+ // ~/.config/opencode/opencode.jsonc
41
+ {
42
+ "$schema": "https://opencode.ai/config.json",
43
+ "plugin": [
44
+ "@jmtrin/opencode-kevin@latest"
45
+ ]
46
+ }
47
+ ```
48
+
49
+ For a **single project**, put the same `plugin` array in `./opencode.json` or `.opencode/opencode.json` at the project root.
50
+
51
+ ### 2. Restart OpenCode
52
+
53
+ Config is loaded once at startup and is **not hot-reloaded** — quit and reopen OpenCode after editing. On start, OpenCode resolves the npm spec, caches the plugin in `~/.cache/opencode/packages/@jmtrin/opencode-kevin/`, and exposes sixteen tools: `kevin_save`, `kevin_query`, `kevin_get`, `kevin_recall`, `kevin_status`, `kevin_retrospective`, `kevin_why`, `kevin_export`, `kevin_import`, `kevin_config`, `kevin_feedback`, `kevin_trace`, `kevin_audit`, `kevin_propose`, `kevin_approve`, `kevin_publish`.
54
+
55
+ ### 3. Where data lives
56
+
57
+ Kevin stores everything in a single **global, shared** location under your home directory no per-project `.kevin/` folders:
58
+
59
+ | Path | Content |
60
+ |---|---|
61
+ | `~/.opencode-kevin/kevin.db` | SQLite database (memories, tool calls, retrospectives). WAL mode → safe for concurrent OpenCode sessions across projects. |
62
+ | `~/.opencode-kevin/retrospectives/<session>.md` | Per-session retrospective markdown. |
63
+
64
+ Migrations run automatically on startup.
65
+
66
+ ### Requirements
67
+
68
+ - **Node.js >= 22.5** (uses `node:sqlite`, the built-in SQLite module — no native binaries to compile).
69
+ - OpenCode with plugin support (`@opencode-ai/plugin` >= 1.17).
70
+
71
+ > **Runtimes**:
72
+ > - **Bun**: uses `bun:sqlite` (built-in).
73
+ > - **Node 24+**: uses `node:sqlite` directly, no flags needed (emits an experimental warning, harmless).
74
+ > - **Node 22/23 without `--experimental-sqlite` flag** or **Node 20**: falls back to `better-sqlite3`, declared as `optionalDependencies`. If you need it, install it manually in your opencode config directory (`~/.config/opencode/`): `npm install better-sqlite3`.
75
+
76
+ ### Verification
77
+
78
+ ```bash
79
+ npm run verify
80
+ ```
81
+
82
+ Checks Node version, SQLite, migration, MemoryService save/query, Reflector, ContextInjector, and TypeScript strict mode.
83
+
84
+ ### Advanced (optional)
85
+
86
+ Override defaults via the plugin tuple form:
87
+
88
+ ```jsonc
89
+ {
90
+ "plugin": [
91
+ ["@jmtrin/opencode-kevin", {
92
+ "dbPath": "/custom/path/kevin.db",
93
+ "retrospectivesDir": "/custom/path/retrospectives",
94
+ "throttleMs": 120000
95
+ }]
96
+ ]
97
+ }
98
+ ```
99
+
100
+ Use `:memory:` for `dbPath` in tests.
101
+
102
+ ---
103
+
104
+ ## How Kevin works
105
+
106
+ Every tool call is observed; every failure becomes a lesson; every lesson is either pushed into the next prompt, written into an artifact a human approved, or retired when it stops earning its place.
107
+
108
+ ```
109
+ Tool call (success or failure)
110
+
111
+
112
+ ┌─────────────────────────┐ OBSERVE
113
+ ToolCallObserver │ records every call (tool, redacted args,
114
+ └───────────┬─────────────┘ success, duration, error type, dedup)
115
+
116
+ failure success
117
+ ┌──────────▼───────────┐ ┌─────────────────────────┐
118
+ Reflector │ CausalChain │
119
+ heuristic lesson links the fix to the │
120
+ per error code failure within 10 calls│
121
+ (throttled per │ └────────────┬────────────┘
122
+ fingerprint)
123
+ └──────────┬───────────┘ session.idle
124
+ │ ▼
125
+ ▼ ┌─────────────────────────┐
126
+ ┌───────────────────────┐ │ promotes recurring │
127
+ │ ContextInjector │◄─┤ errors → causal │
128
+ SHARE: injects │ patterns (cumulative │
129
+ <kevin-context> │ evidence)
130
+ ≤400 tokens/prompt └─────────────────────────┘
131
+ └───────────┬───────────┘
132
+ session.idle
133
+
134
+ ┌─────────────────────────┐ RETROSPECTIVE: <session>.md with
135
+ │ Retrospective │ lessons, metrics snapshot, causal
136
+ └─────────────────────────┘ promotion, pattern mining (opt-in)
137
+ ```
138
+
139
+ At `session.idle` Kevin also settles injection outcomes, retires stale memories, and — when curation is enabled — drafts pull proposals for your review (see [Curation & Pull](#curation--pull)).
140
+
141
+ ---
142
+
143
+ ## Tools
144
+
145
+ Kevin exposes 16 tools callable by the agent.
146
+
147
+ ### `kevin_save`
148
+
149
+ Saves an explicit memory.
150
+
151
+ ```
152
+ kevin_save({ type: "decision", content: "We use vitest for tests", scope: "project" })
153
+ // { "id": "0195a3b2-..." }
154
+ ```
155
+
156
+ `type`: `error` | `pattern` | `decision` | `context` | `rule` | `solution`. `scope`: `project` (persists) | `session` (TTL 24h).
157
+
158
+ Saving a `decision` or `rule` with the same `fingerprint` as an existing active row supersedes the old one (`status='superseded'`, hidden from default queries).
159
+
160
+ ### `kevin_query`
161
+
162
+ Searches memories by text (FTS5 + bm25). Returns a **slim** payload by default; pass `full: true` for the complete content, or `evidence: true` to include `confidence`, `evidence_count` and `last_verified_at`.
163
+
164
+ ```
165
+ kevin_query({ query: "typecheck", type: "error", limit: 5 })
166
+ // → [{ "id": "...", "type": "error", "scope": "project", "score": -0.87,
167
+ // "snippet": "When bash fails with typecheck:..." }, ...]
168
+ ```
169
+
170
+ ### `kevin_get`
171
+
172
+ Fetches a **single full memory** by id (progressive disclosure) — use it when `kevin_query` returned a slim snippet and you need the complete content.
173
+
174
+ ```
175
+ kevin_get({ id: "0195a3b2-..." })
176
+ // → { "id": "...", "type": "error", "content": "...", "scope": "project",
177
+ // "relevanceScore": 0.55, "origin": "reflector", "fingerprint": "cbf29ce484222325",
178
+ // "projectId": null, "metadata": null,
179
+ // "evidenceCount": 2, "recurrenceCount": 1, "lastVerifiedAt": "2026-08-01 10:00:00",
180
+ // "status": "active", "confidence": 0.55, "fixArgs": "npm i -g rg" }
181
+ ```
182
+
183
+ ### `kevin_recall`
184
+
185
+ Retrieves relevant memories (greedy fill by relevance). Without `query`, returns all memories in scope. Pass `includeSuperseded: true` to include superseded rows.
186
+
187
+ ```
188
+ kevin_recall({ query: "auth", limit: 3 })
189
+ // → [{ "id": "...", "type": "decision", ... }, ...]
190
+ ```
191
+
192
+ ### `kevin_status`
193
+
194
+ Global counts and metrics: memory census, the precision block, the six blocked-gate counters, feedback totals, and the v0.6 block (`schema_version`, `curation_enabled`, emission states, `proposals_pending` — omitted on pre-007 databases).
195
+
196
+ ```
197
+ kevin_status({})
198
+ // → { "memories": 42, "memories_reflector": 12, "memories_agent": 30,
199
+ // "memories_pattern": 0, "memories_causal": 1, "tool_calls": 318,
200
+ // "retrospectives": 7, "tool_count": 16,
201
+ // "metrics": { "tokens_injected_pre_prompt": 51, "tokens_injected_compacting": 0,
202
+ // "reflections_throttled": 3, "duplicate_suppressions": 2,
203
+ // "tool_calls_deduped": 0, "patterns_mined": 0,
204
+ // "patterns_causal": 1, "causal_links": 2, "memories_superseded": 0,
205
+ // "injections_inconclusive": 9, ... },
206
+ // "injections_total": 14, "injections_effective": 2, "injections_ineffective": 3,
207
+ // "injections_inconclusive": 9, "precision_rate": 0.40, "coverage_rate": 0.36,
208
+ // "blocked": { "seen": 1, "weak": 0, "recurrence": 2, "stale": 0,
209
+ // "ignored": 1, "confidence": 2 },
210
+ // "memories_ignored": 1, "memories_archived": 4,
211
+ // "feedback": { "positive": 2, "negative": 1 },
212
+ // "patterns_promoted_new": 2, "recurrence_by_origin": { "reflector": 3, "causal": 1 },
213
+ // "v06": { "schema_version": "007", "curation_enabled": "1",
214
+ // "skill_emission": "off", "reference_emission": "off",
215
+ // "proposals_pending": 2 } }
216
+ ```
217
+
218
+ ### `kevin_retrospective`
219
+
220
+ Generates a retrospective for a session (uses the current session if `session_id` is omitted).
221
+
222
+ ```
223
+ kevin_retrospective({ session_id: "sess-abc" })
224
+ // { "file_path": "~/.opencode-kevin/retrospectives/sess-abc.md" }
225
+ // or { "message": "No failures in session sess-abc." }
226
+ ```
227
+
228
+ ### `kevin_why`
229
+
230
+ Explains *why* a failure keeps happening: looks up causal patterns for the query and builds a failure fix trace from memories + tool_calls, including related TypeScript error-code rules.
231
+
232
+ ```
233
+ kevin_why({ query: "TS2304 cannot find name" })
234
+ // { "summary": "TS2304 recurs because ... Confirmed by 2 fixes.",
235
+ // "confidence": 0.7, "evidence_count": 2, "last_verified": "2026-08-01 10:00:00",
236
+ // "trace": [ { "type": "error", "summary": "..." }, { "type": "fix", "tool": "bash" } ],
237
+ // "related_rules": [ { "code": "TS2304", "suggestion": "import or typo" } ] }
238
+ ```
239
+
240
+ ### `kevin_export`
241
+
242
+ Exports knowledge for sharing: `decision`/`rule`/`pattern` memories (active only, no raw errors) as YAML-frontmatter blocks (`format: "okf"`) or markdown (`format: "markdown"`). Includes `id`, `type`, `confidence`, `evidence_count`, `recurrence_count`, `last_verified_at`, `fingerprint`. Timestamps are treated as UTC — a re-import reproduces the exact source values.
243
+
244
+ ### `kevin_import`
245
+
246
+ Ingests an exported bundle. Each entry becomes a `context` memory with `origin='imported'`; a fingerprint collision with an existing `decision`/`rule` supersedes the old row. Returns `{ imported, superseded }`.
247
+
248
+ ### `kevin_config`
249
+
250
+ Reads/writes `kevin_settings` without SQL. `action: "list"` returns every setting; `action: "set"` upserts a value (default `"1"` when omitted) and rejects unknown keys unless `strict: false`.
251
+
252
+ ```
253
+ kevin_config({ action: "list" })
254
+ // { "quality_gate_enabled": "1", "lesson_snippet_injection": "1",
255
+ // "llm_reflection_enabled": "0", "pre_prompt_budget_tokens": "400",
256
+ // "injection_confidence_floor": "0.6", ... }
257
+
258
+ kevin_config({ action: "set", key: "quality_gate_enabled", value: "0" })
259
+ // → { "ok": true, "key": "quality_gate_enabled", "value": "0" }
260
+ ```
261
+
262
+ All settings and their defaults are listed in [Configuration](#configuration).
263
+
264
+ ### `kevin_feedback`
265
+
266
+ Rates an injected memory and makes the rating count. `verdict` is `useful` | `wrong` | `outdated` | `ignore`. The first three are stored in `memory_feedback` and move `kevin_why`'s confidence (`+0.05` / `-0.1` per count); **`ignore` is a hard action** — the memory is stamped `ignored = 1` and excluded from retrieval, queries and injection.
267
+
268
+ ```
269
+ kevin_feedback({ memory_id: "0195a3b2-...", verdict: "wrong", note: "the fix was wrong" })
270
+ // → { "ok": true, "verdict": "wrong" }
271
+ ```
272
+
273
+ ### `kevin_trace`
274
+
275
+ Strict dry-run: predicts exactly which memories `onSystemTransform` WOULD inject for a query (optionally `session_id`, `tag` and `cap`), with **zero side effects** no counters, no ledger rows, no seen-set writes, no relevance bumps. Rejected items carry their `GateReason` (`seen_this_session` | `weak` | `recurrence` | `stale` | `ignored` | `confidence`).
276
+
277
+ ```
278
+ kevin_trace({ query: "tsc error" })
279
+ // → { "query": "tsc error", "tag": "context", "cap": 400, "would_inject": true,
280
+ // "total_tokens": 82,
281
+ // "admitted": [ { "id": "...", "type": "error", "decision": "admitted", "tokens": 62 } ],
282
+ // "blocked": [ { "id": "...", "type": "error", "decision": "blocked",
283
+ // "reason": "confidence", "tokens": 20 } ] }
284
+ ```
285
+
286
+ ### `kevin_audit`
287
+
288
+ Read-only report of the whole system state: memories by `status`/`origin`/`type`, injection outcomes with `precision_rate`/`coverage_rate`, the six `blocked` counters, feedback by verdict, tokens injected, the push-vs-pull `channels` comparison and the `curation` scoreboard. `verbose: true` adds the settings block. No writes, no LLM; on pre-007 databases it omits the v0.6 blocks and reports `"partial": true`.
289
+
290
+ ```
291
+ kevin_audit({})
292
+ // → { "memories": { "total": 42, "by_status": { "active": 37, "stale": 1, "archived": 4 },
293
+ // "by_origin": { "reflector": 12, "agent": 30 }, "by_type": { "error": 20, ... },
294
+ // "ignored": 1, "with_feedback": 3 },
295
+ // "injections": { "total": 14, "effective": 2, "ineffective": 3, "inconclusive": 9,
296
+ // "unmeasured": 0, "precision_rate": 0.40, "coverage_rate": 0.36 },
297
+ // "blocked": { "seen": 1, "weak": 0, "recurrence": 2, "stale": 0,
298
+ // "ignored": 1, "confidence": 2 },
299
+ // "feedback": { "positive": 2, "negative": 1, "by_verdict": { "useful": 2, "wrong": 1 } },
300
+ // "tokens": { "pre_prompt": 51, "compacting": 0 }, "partial": false,
301
+ // "channels": { "push": { "tokens_pre_prompt": 51, "injections_total": 14,
302
+ // "precision_rate": 0.40, "coverage_rate": 0.36,
303
+ // "budget_tokens": 400 },
304
+ // "pull": { "proposals_created": 6, "proposals_approved": 1,
305
+ // "proposals_rejected": 2, "artifact_writes_total": 2,
306
+ // "artifact_writes_noop": 1, "references_registered": 0,
307
+ // "skills_registered": 0,
308
+ // "skill_emission": "off", "reference_emission": "off" } },
309
+ // "curation": { "eligible": 5, "curated": 1, "inferable": 3, "non_inferable": 2,
310
+ // "unknown": 1, "proposals_by_status": { "pending": 2, "applied": 1, ... } } }
311
+ ```
312
+
313
+ ### `kevin_propose`
314
+
315
+ Creates curation proposals as `pending` rows with unified diffs — **a strict dry run**. Reads the eligible memories (`inferable != 1`), renders what would go into the artifact, and returns the minimal diff. No disk write, no `curated` marks, no side effects. Only `kevin_approve` may write.
316
+
317
+ ```
318
+ kevin_propose({ kind: "agents_md" }) // kind: "agents_md" | "skill" | "reference"
319
+ // → { "proposals": [ { "id": "...", "kind": "agents_md", "targetPath": "AGENTS.md",
320
+ // "memoryIds": ["mem-1"], "status": "pending",
321
+ // "createdAt": "2026-08-14 10:00:00",
322
+ // "diff": "--- a/AGENTS.md\n+++ b/AGENTS.md\n@@ ..." } ] }
323
+ ```
324
+
325
+ ### `kevin_approve`
326
+
327
+ The **only** code path that writes a file. `approve` applies the proposal's diff atomically (temp file + rename, CRLF/BOM preserved), records an `artifact_writes` audit row, marks the proposal `applied` and its memories `curated`. `reject` records the human decision and touches nothing. Refusals and noops are audited, never silent.
328
+
329
+ ```
330
+ kevin_approve({ proposal_id: "...", decision: "approve" }) // or "reject"
331
+ // { "proposalId": "...", "status": "applied", "outcome": "written", "curated": 1 }
332
+ // ("outcome": "noop" when the artifact already matches, "refused" when the
333
+ // marker block is malformed; a rejected proposal returns
334
+ // { "proposalId": "...", "status": "rejected" })
335
+ ```
336
+
337
+ ### `kevin_publish`
338
+
339
+ Regenerates the pull-channel bundles under `~/.opencode-kevin/` — `skills/project-knowledge.md` and `refs/<topic>.md` reporting per-bundle outcome and the emission state (`on` / `off` / `unavailable`). Registration with the host happens at plugin startup; this tool only materializes and reports.
340
+
341
+ ---
342
+
343
+ ## How Kevin measures itself
344
+
345
+ ### Injection outcomes
346
+
347
+ Every injection is settled at `session.idle` into one of **four outcomes**:
348
+
349
+ | Outcome | Meaning | Counts toward precision? |
350
+ |---|---|---|
351
+ | `effective` | A linked fix was observed after the injection | yes (numerator) |
352
+ | `ineffective` | The same error recurred after the injection | yes (denominator) |
353
+ | `inconclusive` | Neither the error did not recur, but no fix was seen either | no |
354
+ | `unmeasured` | Session went idle before settlement could run | no |
355
+
356
+ - **`precision_rate`** = `effective / (effective + ineffective)`. Measuring *effect*, not absence of recurrence: a lesson that was injected and never contradicted counts as `inconclusive`, not success. **Your precision rate will look lower than before v0.5.0. That is the honest number.**
357
+ - **`coverage_rate`** = `(effective + ineffective) / total` — the share of injections that were actually measured. Reported alongside precision so a low measurable fraction stays visible instead of hiding behind a large total.
358
+ - **`blocked`** counts every gate rejection by reason — `seen_this_session`, `weak`, `recurrence`, `stale`, `ignored`, `confidence` — a rejection you did not count did not happen.
359
+
360
+ ### The quality gate
361
+
362
+ Weak lessons — errors the reflector cannot dispatch to a deterministic rule — are **stored but never injected** while `quality_gate_enabled = '1'` (default). Recurrences demote lessons (`recurrence_count` → `stale`) and lower confidence. Debug mode: `kevin_config({ action: "set", key: "quality_gate_enabled", value: "0" })` re-injects weak lessons with a `(low confidence)` marker.
363
+
364
+ ### Seeing the whole picture
365
+
366
+ `kevin_trace` shows you the plan *before* it happens (dry run, zero side effects); `kevin_audit` reads the whole state after; `kevin_feedback` lets a human correct it — and the correction moves the confidence number `kevin_why` reports.
367
+
368
+ ---
369
+
370
+ ## Curation & Pull
371
+
372
+ ### The marker contract
373
+
374
+ Kevin never edits your files directly. Every artifact write happens inside a frozen marker block, delimited verbatim by:
375
+
376
+ ```
377
+ <!-- kevin:begin — curated by opencode-kevin, safe to edit -->
378
+ <!-- kevin:end -->
379
+ ```
380
+
381
+ These exact strings are **frozen for the v0.x line** — README, tests and the v1.0.0 migration plan all depend on their byte sequences. What Kevin guarantees:
382
+
383
+ - **Only the block between the markers may change.** Bytes outside them are byte-identical after every write — including line endings (a CRLF file stays CRLF everywhere, even inside the generated block), a leading UTF-8 BOM, and the file's final newline.
384
+ - **Malformed markers are refused, never repaired.** If the file contains a `begin` without an `end` (or vice versa), Kevin refuses the write with an explicit reason and the file is untouched. Repairing would mean guessing at user intent; refusing means the state stays visible and auditable.
385
+ - **Idempotent**: applying an unchanged plan is a counted `noop` — no temp file, no write, no mtime churn.
386
+
387
+ ### The propose → review → approve flow
388
+
389
+ ```
390
+ eligible memories (inferable != 1)
391
+
392
+
393
+ kevin_propose({ kind }) ── creates pending rows + unified diffs.
394
+ │ NO disk write, NO curated marks.
395
+
396
+ HUMAN REVIEWS THE DIFF ── this is the entire safety model:
397
+ │ a memory earns its way into a file
398
+ ▼ only after a human said yes.
399
+ kevin_approve({ proposal_id, decision })
400
+
401
+ ├── "approve" ── ArtifactWriter.apply() (the ONLY write path)
402
+ │ atomic temp+rename, audit row in artifact_writes,
403
+ │ memory marked curated, proposal marked applied
404
+ └── "reject" ── recorded, nothing touches disk
405
+ ```
406
+
407
+ Rejection history is never deleted: it is the evidence base for the roadmap's kill criterion "proposals rejected more often than approved".
408
+
409
+ ### Three distribution channels
410
+
411
+ | Channel | Artifact | Cost when unused |
412
+ |---|---|---|
413
+ | **Push** | per-prompt `<kevin-context>` injection | charges on every prompt — now capped at 400 tokens by default |
414
+ | **Pull — AGENTS.md** | marker block in the project's `AGENTS.md` | zero |
415
+ | **Pull — skills** | `~/.opencode-kevin/skills/project-knowledge.md` (`skill_emission_enabled`) | zero |
416
+ | **Pull — references** | `~/.opencode-kevin/refs/<topic>.md` (`reference_emission_enabled`) | zero |
417
+
418
+ `kevin_audit`'s `channels` block compares push vs pull on the same axes, and reports each emission channel as `"on"`, `"off"` (setting `'0'` on a capable host) or `"unavailable"` (host without the v2 domain).
419
+
420
+ ### The confidence floor gate
421
+
422
+ `injection_confidence_floor` (default `'0.6'`) rejects memories whose computed confidence is below the floor, counted as `injections_blocked_confidence` — the sixth gate rejection reason, measured exactly like the first five. Single-observation memories (base confidence 0.5, no confirmed evidence) stop being pushed by default; `kevin_config({ action: "set", key: "injection_confidence_floor", value: "0" })` restores v0.5 behaviour exactly.
423
+
424
+ ---
425
+
426
+ ## Replay harness
427
+
428
+ `npm run replay` runs every transcript in `tests/replay/fixtures/` through the plugin against an in-memory database with a frozen clock and prints one table row per transcript (memories created, injection outcomes, `precision_rate`, `coverage_rate`, tokens). Record your own session as a JSON array of typed events (`session.created`, `chat.message`, `tool.before`, `tool.after`, `system.transform`, `compacting`, `session.idle`) with ISO-8601 `at` timestamps, drop it into `tests/replay/fixtures/`, and re-run. The `at` timestamps are the only source of time during replay.
429
+
430
+ ---
431
+
432
+ ## Hooks
433
+
434
+ Kevin subscribes to 6 OpenCode hooks:
435
+
436
+ | Hook | What Kevin does |
437
+ |---|---|
438
+ | `tool.execute.before` | Records tool call start (callID + redacted args) |
439
+ | `tool.execute.after` | Records result (id = callID); on failure → Reflector.invoke async (throttled); on success → CausalChain links the fix |
440
+ | `experimental.chat.system.transform` | Injects relevant lessons in `<kevin-context>` (400 tokens by default, configurable) + optional `<kevin-suggestion>` |
441
+ | `experimental.session.compacting` | Re-injects lessons in `<kevin-memory>` after compacting (2000 tokens) + optional `<kevin-suggestion>` |
442
+ | `event` (`session.created`) | Captures current `sessionID` (skill/reference emissions register at plugin startup, not per session) |
443
+ | `event` (`session.idle`) | Settles injection outcomes; generates the retrospective; boosts positive lessons; penalizes recurring failures; promotes causal patterns and mines patterns (opt-in); drafts curation proposals (`curation_enabled`); flushes metrics |
444
+
445
+ **Redaction**: absolute paths (`C:\Users\...`, `/home/...`) → `<path>` and secrets (`API_KEY=`, `Bearer`, `token`) → `<redacted>` before persisting anything. `<private>…</private>` blocks are swept from tool call args and output before persistence and replaced with `<private: redacted N chars>`.
446
+
447
+ **Throttle**: Reflector generates at most 1 lesson per minute per unique fingerprint (per-fingerprint, not global). Configurable via `throttleMs`.
448
+
449
+ **Truncation**: content > 4KB keeps the lesson searchable; only the additional context is truncated (`metadata.truncated = true`).
450
+
451
+ ---
452
+
453
+ ## Configuration
454
+
455
+ ### Plugin options
456
+
457
+ Kevin accepts options via the plugin's tuple form (see Installation → Advanced). Programmatic defaults:
458
+
459
+ ```ts
460
+ import { KevinPlugin } from "@jmtrin/opencode-kevin";
461
+
462
+ // defaults
463
+ KevinPlugin(input, {
464
+ dbPath: "~/.opencode-kevin/kevin.db", // or ":memory:" for tests
465
+ migrationsDir: "<package>/dist/migrations", // resolved automatically
466
+ retrospectivesDir: "~/.opencode-kevin/retrospectives",
467
+ throttleMs: 60_000,
468
+ });
469
+ ```
470
+
471
+ ### Settings
472
+
473
+ Read/write via `kevin_config({ action: "list" | "set", ... })`. All values are TEXT; booleans compare against `"1"`.
474
+
475
+ | Setting | Default | Effect |
476
+ |---|---|---|
477
+ | `quality_gate_enabled` | `"1"` | Weak lessons are stored but never injected while enabled |
478
+ | `lesson_snippet_injection` | `"1"` | Injects the rescued errorType snippet with each lesson |
479
+ | `llm_reflection_enabled` | `"0"` | Opt-in LLM enrichment of reflector lessons |
480
+ | `cross_project_enabled` | `"0"` | `kevin_query` includes imported cross-project memories |
481
+ | `patternminer_enabled` | `"0"` | Opt-in deterministic 2-gram/3-gram pattern miner at `session.idle` |
482
+ | `tool_calls_dedup_enabled` | `"0"` | Opt-in dedup of repeated tool calls |
483
+ | `deterministic_retrieval` | `"0"` | Freezes Kevin's internal clock (recency factor 1.0, no relevance bumps) — for hermetic tests and the replay harness |
484
+ | `pre_prompt_budget_tokens` | `"400"` | Pre-prompt injection cap, clamped to `[0, 4000]`; `0` turns push off |
485
+ | `archive_after_days` | `"30"` | Age at which stale non-pattern memories are retired to `archived` on `session.idle` |
486
+ | `curation_enabled` | `"1"` | Generates curation proposals at `session.idle` |
487
+ | `agents_md_path` | `"AGENTS.md"` | Where the AGENTS.md channel writes (project-relative) |
488
+ | `skill_emission_enabled` | `"0"` | Registers the curated skill with the host at startup (v2 hosts only) |
489
+ | `reference_emission_enabled` | `"0"` | Registers `@kevin/<topic>` references at startup (v2 hosts only) |
490
+ | `injection_confidence_floor` | `"0.6"` | Push gate: memories below this confidence are counted and rejected |
491
+
492
+ ---
493
+
494
+ ## Development
495
+
496
+ ```bash
497
+ git clone https://github.com/jmtrin/opencode-kevin.git
498
+ cd opencode-kevin
499
+ npm install
500
+ npm run typecheck # tsc --noEmit (strict)
501
+ npm run lint # biome check .
502
+ npm test # vitest run (unit + integration + e2e + replay)
503
+ npm run verify # post-install verification
504
+ npm run replay # replay report over tests/replay/fixtures
505
+ ```
506
+
507
+ ### Publishing (maintainer)
508
+
509
+ ```bash
510
+ npm login # as the jmtrin account that owns the @jmtrin scope
511
+ npm publish --access public
512
+ ```
513
+
514
+ `prepublishOnly` runs `npm run build` (tsc + copy migrations) automatically. The `files` field ships only `dist/plugin`, `dist/migrations`, and `migrations`. `dist/` is gitignored and rebuilt on publish.
515
+
516
+ ### Structure
517
+
518
+ ```
519
+ plugin/
520
+ index.ts # Entry point: KevinPlugin (wires hooks, tools, emissions)
521
+ Store.ts # SQLite wrapper (node:sqlite / bun:sqlite / better-sqlite3 fallback)
522
+ sqlite-adapter.ts # Runtime-agnostic SQLite adapter behind Store
523
+ Migrate.ts # Idempotent migrations + post-apply hooks
524
+ MemoryService.ts # save/query/getRelevant (FTS5 + bm25 + origin-aware rank + supersede)
525
+ ToolCallObserver.ts # onBefore/onAfter + redact + inferErrorType + dedup (opt-in)
526
+ Reflector.ts # Heuristic lessons + per-fingerprint throttle + LLM enrich (opt-in)
527
+ ContextInjector.ts # deriveQuery + pre-prompt/compacting injection + <kevin-suggestion>
528
+ Retrospective.ts # Generates retrospective.md + FP recap + metrics snapshot
529
+ Feedback.ts # kevin_feedback: verdicts, confidence terms, ignored stamp
530
+ Archiver.ts # Retires stale non-pattern memories past archive_after_days
531
+ CausalChain.ts # Links fixes to failures + promotes causal patterns
532
+ QualityGate.ts # Weak-lesson gate (stored, not injected by default)
533
+ InjectionLedger.ts # Injection ledger + settle → precision_rate
534
+ LessonFixer.ts # Deterministic fix_args capture + promotion enrichment
535
+ PatternMiner.ts # Opt-in deterministic 2-gram/3-gram miner
536
+ Curator.ts # Curation candidates + propose/approve lifecycle
537
+ ArtifactWriter.ts # The SINGLE write path (markers, atomic, noop, audit rows)
538
+ Materializer.ts # Pull-channel topic bundles (skills, refs)
539
+ inferability.ts # Deterministic inferable/non-inferable/unknown classifier
540
+ capabilities.ts # v2 domain probe (skills / references)
541
+ diff.ts # Minimal unified diff for proposal review
542
+ replay.ts # Hermetic replay driver over recorded transcripts
543
+ replay-types.ts # Transcript/result types for the replay harness
544
+ kevin_propose.ts # kevin_propose tool (strict dry run)
545
+ kevin_approve.ts # kevin_approve tool (only writer call site)
546
+ kevin_publish.ts # kevin_publish tool (bundle regeneration)
547
+ kevin_audit.ts # Read-only audit + channels/curation blocks
548
+ kevin_why.ts # kevin_why tool: failure→fix traces + related rules
549
+ okf-export.ts # kevin_export: OKF/markdown export
550
+ okf-import.ts # kevin_import: bundle parser + import
551
+ confidence.ts # Two-sided computeConfidence (evidence + recurrence + feedback)
552
+ query-tokenizer.ts # FTS5 tokenizer for query sanitization
553
+ memory-format.ts # escapeInjectedText, formatMemories, <protect> + id: line wrappers
554
+ redact.ts # redactPaths + stripPrivate
555
+ fingerprint.ts # FNV-1a 64-bit (in-house, no node:crypto)
556
+ metrics.ts # In-memory counters + debounced flush to kevin_metrics
557
+ uuid.ts # UUIDv7
558
+ migrations/
559
+ 001_initial.sql # schema: memories, tool_calls, retrospectives
560
+ 002_indexes.sql # FTS5 + indexes
561
+ 003_v02_signal.sql # fingerprint, origin, metrics, dedup indexes
562
+ 004_v03_knowledge.sql # evidence/status/supersede, error_fingerprint
563
+ 005_v04_signal.sql # recurrence_count, fix_args, last_injected_at
564
+ 006_v05_glassbox.sql # ignored/archived/superseded_by, feedback, metrics
565
+ 007_v06_pull.sql # curation_proposals, artifact_writes, curated/inferable
566
+ tests/
567
+ unit/ # component tests
568
+ integration/ # tool-level tests through real components
569
+ e2e/ # closed-loop tests through the host hooks
570
+ replay/ # transcript fixtures + replay harness tests
571
+ scripts/
572
+ copy-migrations.mjs # build step: copies *.sql to dist/migrations
573
+ verify-install.ts # npm run verify
574
+ ```
575
+
576
+ ---
577
+
578
+ ## License
579
+
580
+ MIT