@jmtrin/opencode-kevin 1.0.0 → 1.2.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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -8
  3. package/dist/migrations/001_initial.sql +91 -91
  4. package/dist/migrations/002_indexes.sql +13 -13
  5. package/dist/migrations/003_v02_signal.sql +57 -57
  6. package/dist/migrations/004_v03_knowledge.sql +138 -138
  7. package/dist/migrations/005_v04_signal.sql +57 -57
  8. package/dist/migrations/006_v05_glassbox.sql +118 -118
  9. package/dist/migrations/007_v06_pull.sql +144 -144
  10. package/dist/migrations/012_v11_drift.sql +24 -0
  11. package/dist/plugin/Archiver.js +2 -17
  12. package/dist/plugin/CausalChain.js +32 -13
  13. package/dist/plugin/ChatBridge.d.ts +41 -0
  14. package/dist/plugin/ChatBridge.js +103 -0
  15. package/dist/plugin/ConflictDetector.js +7 -29
  16. package/dist/plugin/DashboardHtml.d.ts +5 -0
  17. package/dist/plugin/DashboardHtml.js +180 -0
  18. package/dist/plugin/Feedback.js +2 -19
  19. package/dist/plugin/HookLiveness.d.ts +1 -0
  20. package/dist/plugin/HookLiveness.js +11 -27
  21. package/dist/plugin/InjectionLedger.js +104 -57
  22. package/dist/plugin/Materializer.js +1 -88
  23. package/dist/plugin/MemoryService.d.ts +59 -1
  24. package/dist/plugin/MemoryService.js +13 -106
  25. package/dist/plugin/Migrate.js +5 -0
  26. package/dist/plugin/Retrospective.js +7 -0
  27. package/dist/plugin/ToolCallObserver.js +18 -5
  28. package/dist/plugin/TuiActions.d.ts +43 -0
  29. package/dist/plugin/TuiActions.js +181 -0
  30. package/dist/plugin/TuiSnapshots.d.ts +24 -0
  31. package/dist/plugin/TuiSnapshots.js +158 -0
  32. package/dist/plugin/capabilities.d.ts +2 -0
  33. package/dist/plugin/capabilities.js +3 -0
  34. package/dist/plugin/columns.d.ts +11 -0
  35. package/dist/plugin/columns.js +54 -0
  36. package/dist/plugin/contract.d.ts +8 -0
  37. package/dist/plugin/contract.js +23 -5
  38. package/dist/plugin/index.d.ts +2 -2
  39. package/dist/plugin/index.js +315 -10
  40. package/dist/plugin/kevin_audit.d.ts +17 -1
  41. package/dist/plugin/kevin_audit.js +69 -1
  42. package/dist/plugin/kevin_forget.d.ts +33 -0
  43. package/dist/plugin/kevin_forget.js +260 -0
  44. package/dist/plugin/kevin_why.js +1 -18
  45. package/dist/plugin/metrics.d.ts +1 -1
  46. package/dist/plugin/metrics.js +8 -0
  47. package/dist/plugin/query-tokenizer.js +56 -8
  48. package/dist/plugin/time-ms.d.ts +1 -0
  49. package/dist/plugin/time-ms.js +16 -0
  50. package/dist/plugin/tui-types.d.ts +59 -0
  51. package/dist/plugin/tui-types.js +4 -0
  52. package/dist/plugin/tui.d.ts +18 -0
  53. package/dist/plugin/tui.js +198 -0
  54. package/package.json +8 -2
@@ -1,138 +1,138 @@
1
- -- ============================================================
2
- -- Kevin 0.3.0 — Migration 004: Knowledge + Causality (additive)
3
- -- ============================================================
4
- -- Backward-compatible, additive only. All new columns are
5
- -- nullable or carry a NOT NULL DEFAULT so legacy rows keep
6
- -- working without a destructive rebuild.
7
- -- ============================================================
8
-
9
- -- 1. memories: evidence + lifecycle columns.
10
- -- evidence_count — how many times this fingerprint was confirmed as fixed.
11
- -- last_verified_at — timestamp of the most recent causal confirmation.
12
- -- status — lifecycle state; default 'active'. Superseded rows are hidden.
13
- ALTER TABLE memories ADD COLUMN evidence_count INTEGER NOT NULL DEFAULT 0;
14
- ALTER TABLE memories ADD COLUMN last_verified_at TEXT;
15
- ALTER TABLE memories ADD COLUMN status TEXT NOT NULL DEFAULT 'active'
16
- CHECK (status IN ('active', 'superseded', 'stale', 'archived'));
17
-
18
- -- 2. tool_calls: causal link + feedback-loop link columns.
19
- -- fix_for_fingerprint — set when this successful call resolved a prior failure
20
- -- with the given fingerprint. NULL for tool calls that were not fixes.
21
- -- error_fingerprint — set by Reflector (via onLinkError callback) when a call
22
- -- FAILS, to the stderr-based fingerprint the matching error memory uses.
23
- -- This fixes the v0.2.0/v0.3.0 feedback-loop fingerprint mismatch bug:
24
- -- tool_calls.fingerprint is hashed from "tool|args|success" by ToolCallObserver,
25
- -- while memories.fingerprint is hashed from stderr text by Reflector — they
26
- -- never agreed, so boost/penalize queries silently mismatched. The new column
27
- -- stores the SAME identity dimension the error memory uses.
28
- ALTER TABLE tool_calls ADD COLUMN fix_for_fingerprint TEXT;
29
- ALTER TABLE tool_calls ADD COLUMN error_fingerprint TEXT;
30
-
31
- -- 3. Index: causal linkage by fingerprint. Used by CausalChain.onSuccess
32
- -- and kevin_why to materialize traces.
33
- CREATE INDEX IF NOT EXISTS idx_tool_calls_fix_fp
34
- ON tool_calls(fix_for_fingerprint)
35
- WHERE fix_for_fingerprint IS NOT NULL;
36
-
37
- -- 3b. Index: feedback-loop linkage by error_fingerprint. Used by
38
- -- boostPositiveReflectors / penalizeRecurringReflectors to count
39
- -- recurrences by the same identity dimension the error memory uses.
40
- CREATE INDEX IF NOT EXISTS idx_tool_calls_error_fp
41
- ON tool_calls(error_fingerprint)
42
- WHERE error_fingerprint IS NOT NULL;
43
-
44
- -- 4. Index: memories by fingerprint for promotion + supersede queries.
45
- CREATE INDEX IF NOT EXISTS idx_memories_fp
46
- ON memories(fingerprint)
47
- WHERE fingerprint IS NOT NULL;
48
-
49
- -- 5. kevin_metrics: seed new v0.3 counters.
50
- INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
51
- ('patterns_causal', 0),
52
- ('causal_links', 0),
53
- ('memories_superseded', 0);
54
-
55
- -- 6. kevin_settings: seed new opt-in flags.
56
- INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
57
- ('llm_reflection_enabled', '0'),
58
- ('cross_project_enabled', '0');
59
-
60
- -- 7. Seed version 004.
61
- INSERT OR IGNORE INTO schema_version (version) VALUES ('004');
62
-
63
- -- ============================================================
64
- -- 8. Rebuild memories table with expanded CHECK constraints.
65
- -- v0.3.0 introduces new types (rule, solution) and origins
66
- -- (causal, imported). SQLite cannot ALTER a CHECK constraint,
67
- -- so we rebuild via a temporary table. FTS5 external-content
68
- -- table references the content table by name, so we drop+recreate
69
- -- the FTS5 triggers after the rebuild.
70
- -- ============================================================
71
-
72
- -- Step 1: Create new table with correct constraints
73
- CREATE TABLE memories_v04 (
74
- id TEXT PRIMARY KEY,
75
- type TEXT NOT NULL CHECK(type IN ('error','pattern','decision','context','rule','solution')),
76
- content TEXT NOT NULL,
77
- scope TEXT NOT NULL DEFAULT 'project' CHECK(scope IN ('project','session')),
78
- relevance_score REAL DEFAULT 0.5,
79
- source_tool TEXT,
80
- source_session TEXT,
81
- metadata TEXT,
82
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
83
- updated_at TEXT NOT NULL DEFAULT (datetime('now')),
84
- expires_at TEXT,
85
- project_id TEXT,
86
- fingerprint TEXT,
87
- origin TEXT NOT NULL DEFAULT 'agent'
88
- CHECK(origin IN ('reflector','agent','pattern','retrospective','causal','imported')),
89
- evidence_count INTEGER NOT NULL DEFAULT 0,
90
- last_verified_at TEXT,
91
- status TEXT NOT NULL DEFAULT 'active'
92
- CHECK(status IN ('active','superseded','stale','archived'))
93
- );
94
-
95
- -- Step 2: Copy data preserving rowids (FTS5 content sync relies on rowid)
96
- INSERT INTO memories_v04
97
- (rowid, id, type, content, scope, relevance_score, source_tool,
98
- source_session, metadata, created_at, updated_at, expires_at,
99
- project_id, fingerprint, origin, evidence_count, last_verified_at, status)
100
- SELECT rowid, id, type, content, scope, relevance_score, source_tool,
101
- source_session, metadata, created_at, updated_at, expires_at,
102
- project_id, fingerprint, origin, evidence_count, last_verified_at, status
103
- FROM memories;
104
-
105
- -- Step 3: Drop old table and FTS triggers
106
- DROP TRIGGER IF EXISTS memories_ai;
107
- DROP TRIGGER IF EXISTS memories_ad;
108
- DROP TRIGGER IF EXISTS memories_au;
109
- DROP TABLE memories;
110
-
111
- -- Step 4: Rename new table
112
- ALTER TABLE memories_v04 RENAME TO memories;
113
-
114
- -- Step 5: Recreate FTS triggers (memories_fts is content=external, survives DROP of content table)
115
- CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
116
- INSERT INTO memories_fts(rowid, content) VALUES (new.rowid, new.content);
117
- END;
118
-
119
- CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
120
- INSERT INTO memories_fts(memories_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
121
- END;
122
-
123
- CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
124
- INSERT INTO memories_fts(memories_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
125
- INSERT INTO memories_fts(rowid, content) VALUES (new.rowid, new.content);
126
- END;
127
-
128
- -- Step 6: Recreate indexes
129
- CREATE INDEX IF NOT EXISTS idx_memories_type ON memories(type);
130
- CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope);
131
- CREATE INDEX IF NOT EXISTS idx_memories_relevance ON memories(relevance_score DESC);
132
- CREATE INDEX IF NOT EXISTS idx_memories_created ON memories(created_at);
133
- CREATE UNIQUE INDEX IF NOT EXISTS uq_memories_error_fp
134
- ON memories(project_id, fingerprint)
135
- WHERE type = 'error' AND fingerprint IS NOT NULL AND origin = 'reflector';
136
- CREATE INDEX IF NOT EXISTS idx_memories_fp
137
- ON memories(fingerprint)
138
- WHERE fingerprint IS NOT NULL;
1
+ -- ============================================================
2
+ -- Kevin 0.3.0 — Migration 004: Knowledge + Causality (additive)
3
+ -- ============================================================
4
+ -- Backward-compatible, additive only. All new columns are
5
+ -- nullable or carry a NOT NULL DEFAULT so legacy rows keep
6
+ -- working without a destructive rebuild.
7
+ -- ============================================================
8
+
9
+ -- 1. memories: evidence + lifecycle columns.
10
+ -- evidence_count — how many times this fingerprint was confirmed as fixed.
11
+ -- last_verified_at — timestamp of the most recent causal confirmation.
12
+ -- status — lifecycle state; default 'active'. Superseded rows are hidden.
13
+ ALTER TABLE memories ADD COLUMN evidence_count INTEGER NOT NULL DEFAULT 0;
14
+ ALTER TABLE memories ADD COLUMN last_verified_at TEXT;
15
+ ALTER TABLE memories ADD COLUMN status TEXT NOT NULL DEFAULT 'active'
16
+ CHECK (status IN ('active', 'superseded', 'stale', 'archived'));
17
+
18
+ -- 2. tool_calls: causal link + feedback-loop link columns.
19
+ -- fix_for_fingerprint — set when this successful call resolved a prior failure
20
+ -- with the given fingerprint. NULL for tool calls that were not fixes.
21
+ -- error_fingerprint — set by Reflector (via onLinkError callback) when a call
22
+ -- FAILS, to the stderr-based fingerprint the matching error memory uses.
23
+ -- This fixes the v0.2.0/v0.3.0 feedback-loop fingerprint mismatch bug:
24
+ -- tool_calls.fingerprint is hashed from "tool|args|success" by ToolCallObserver,
25
+ -- while memories.fingerprint is hashed from stderr text by Reflector — they
26
+ -- never agreed, so boost/penalize queries silently mismatched. The new column
27
+ -- stores the SAME identity dimension the error memory uses.
28
+ ALTER TABLE tool_calls ADD COLUMN fix_for_fingerprint TEXT;
29
+ ALTER TABLE tool_calls ADD COLUMN error_fingerprint TEXT;
30
+
31
+ -- 3. Index: causal linkage by fingerprint. Used by CausalChain.onSuccess
32
+ -- and kevin_why to materialize traces.
33
+ CREATE INDEX IF NOT EXISTS idx_tool_calls_fix_fp
34
+ ON tool_calls(fix_for_fingerprint)
35
+ WHERE fix_for_fingerprint IS NOT NULL;
36
+
37
+ -- 3b. Index: feedback-loop linkage by error_fingerprint. Used by
38
+ -- boostPositiveReflectors / penalizeRecurringReflectors to count
39
+ -- recurrences by the same identity dimension the error memory uses.
40
+ CREATE INDEX IF NOT EXISTS idx_tool_calls_error_fp
41
+ ON tool_calls(error_fingerprint)
42
+ WHERE error_fingerprint IS NOT NULL;
43
+
44
+ -- 4. Index: memories by fingerprint for promotion + supersede queries.
45
+ CREATE INDEX IF NOT EXISTS idx_memories_fp
46
+ ON memories(fingerprint)
47
+ WHERE fingerprint IS NOT NULL;
48
+
49
+ -- 5. kevin_metrics: seed new v0.3 counters.
50
+ INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
51
+ ('patterns_causal', 0),
52
+ ('causal_links', 0),
53
+ ('memories_superseded', 0);
54
+
55
+ -- 6. kevin_settings: seed new opt-in flags.
56
+ INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
57
+ ('llm_reflection_enabled', '0'),
58
+ ('cross_project_enabled', '0');
59
+
60
+ -- 7. Seed version 004.
61
+ INSERT OR IGNORE INTO schema_version (version) VALUES ('004');
62
+
63
+ -- ============================================================
64
+ -- 8. Rebuild memories table with expanded CHECK constraints.
65
+ -- v0.3.0 introduces new types (rule, solution) and origins
66
+ -- (causal, imported). SQLite cannot ALTER a CHECK constraint,
67
+ -- so we rebuild via a temporary table. FTS5 external-content
68
+ -- table references the content table by name, so we drop+recreate
69
+ -- the FTS5 triggers after the rebuild.
70
+ -- ============================================================
71
+
72
+ -- Step 1: Create new table with correct constraints
73
+ CREATE TABLE memories_v04 (
74
+ id TEXT PRIMARY KEY,
75
+ type TEXT NOT NULL CHECK(type IN ('error','pattern','decision','context','rule','solution')),
76
+ content TEXT NOT NULL,
77
+ scope TEXT NOT NULL DEFAULT 'project' CHECK(scope IN ('project','session')),
78
+ relevance_score REAL DEFAULT 0.5,
79
+ source_tool TEXT,
80
+ source_session TEXT,
81
+ metadata TEXT,
82
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
83
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
84
+ expires_at TEXT,
85
+ project_id TEXT,
86
+ fingerprint TEXT,
87
+ origin TEXT NOT NULL DEFAULT 'agent'
88
+ CHECK(origin IN ('reflector','agent','pattern','retrospective','causal','imported')),
89
+ evidence_count INTEGER NOT NULL DEFAULT 0,
90
+ last_verified_at TEXT,
91
+ status TEXT NOT NULL DEFAULT 'active'
92
+ CHECK(status IN ('active','superseded','stale','archived'))
93
+ );
94
+
95
+ -- Step 2: Copy data preserving rowids (FTS5 content sync relies on rowid)
96
+ INSERT INTO memories_v04
97
+ (rowid, id, type, content, scope, relevance_score, source_tool,
98
+ source_session, metadata, created_at, updated_at, expires_at,
99
+ project_id, fingerprint, origin, evidence_count, last_verified_at, status)
100
+ SELECT rowid, id, type, content, scope, relevance_score, source_tool,
101
+ source_session, metadata, created_at, updated_at, expires_at,
102
+ project_id, fingerprint, origin, evidence_count, last_verified_at, status
103
+ FROM memories;
104
+
105
+ -- Step 3: Drop old table and FTS triggers
106
+ DROP TRIGGER IF EXISTS memories_ai;
107
+ DROP TRIGGER IF EXISTS memories_ad;
108
+ DROP TRIGGER IF EXISTS memories_au;
109
+ DROP TABLE memories;
110
+
111
+ -- Step 4: Rename new table
112
+ ALTER TABLE memories_v04 RENAME TO memories;
113
+
114
+ -- Step 5: Recreate FTS triggers (memories_fts is content=external, survives DROP of content table)
115
+ CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
116
+ INSERT INTO memories_fts(rowid, content) VALUES (new.rowid, new.content);
117
+ END;
118
+
119
+ CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
120
+ INSERT INTO memories_fts(memories_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
121
+ END;
122
+
123
+ CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
124
+ INSERT INTO memories_fts(memories_fts, rowid, content) VALUES ('delete', old.rowid, old.content);
125
+ INSERT INTO memories_fts(rowid, content) VALUES (new.rowid, new.content);
126
+ END;
127
+
128
+ -- Step 6: Recreate indexes
129
+ CREATE INDEX IF NOT EXISTS idx_memories_type ON memories(type);
130
+ CREATE INDEX IF NOT EXISTS idx_memories_scope ON memories(scope);
131
+ CREATE INDEX IF NOT EXISTS idx_memories_relevance ON memories(relevance_score DESC);
132
+ CREATE INDEX IF NOT EXISTS idx_memories_created ON memories(created_at);
133
+ CREATE UNIQUE INDEX IF NOT EXISTS uq_memories_error_fp
134
+ ON memories(project_id, fingerprint)
135
+ WHERE type = 'error' AND fingerprint IS NOT NULL AND origin = 'reflector';
136
+ CREATE INDEX IF NOT EXISTS idx_memories_fp
137
+ ON memories(fingerprint)
138
+ WHERE fingerprint IS NOT NULL;
@@ -1,57 +1,57 @@
1
- -- ============================================================
2
- -- Kevin 0.4.0 — Migration 005: Signal over Noise (additive)
3
- -- ============================================================
4
- -- Backward-compatible, additive only. All new columns are
5
- -- nullable or carry a NOT NULL DEFAULT so legacy rows keep
6
- -- working without a destructive rebuild.
7
- -- ============================================================
8
-
9
- -- 1. memories: positive/negative evidence split (D4-03).
10
- -- recurrence_count — how many times this fingerprint recurred AFTER
11
- -- injection (negative evidence; lowers confidence).
12
- -- fix_args — deterministic capture of the linked success call's
13
- -- args_summary ("Fixed by:" raw material, D4-07).
14
- -- last_injected_at — timestamp of the most recent injection of this memory.
15
- ALTER TABLE memories ADD COLUMN recurrence_count INTEGER NOT NULL DEFAULT 0;
16
- ALTER TABLE memories ADD COLUMN fix_args TEXT;
17
- ALTER TABLE memories ADD COLUMN last_injected_at TEXT;
18
-
19
- -- 2. kevin_injections: the injection ledger (D4-04). One row per injected
20
- -- memory per prompt/compaction, settled at session.idle.
21
- CREATE TABLE IF NOT EXISTS kevin_injections (
22
- id TEXT PRIMARY KEY,
23
- memory_id TEXT NOT NULL,
24
- fingerprint TEXT NOT NULL,
25
- session_id TEXT NOT NULL,
26
- hook TEXT NOT NULL CHECK (hook IN ('pre_prompt', 'compacting')),
27
- tokens INTEGER NOT NULL,
28
- injected_at TEXT NOT NULL DEFAULT (datetime('now')),
29
- outcome TEXT CHECK (outcome IN ('unmeasured', 'effective', 'ineffective'))
30
- NOT NULL DEFAULT 'unmeasured'
31
- );
32
-
33
- -- 2b. Indexes: settlement by session, recurrence lookups by fingerprint,
34
- -- and outcome rollups for precision_rate.
35
- CREATE INDEX IF NOT EXISTS idx_injections_fp
36
- ON kevin_injections(fingerprint);
37
- CREATE INDEX IF NOT EXISTS idx_injections_session
38
- ON kevin_injections(session_id);
39
- CREATE INDEX IF NOT EXISTS idx_injections_outcome
40
- ON kevin_injections(outcome);
41
-
42
- -- 3. kevin_metrics: seed new v0.4 counters.
43
- -- patterns_promoted_new replaces patterns_causal (which was inflated by
44
- -- idempotent refreshes); the latter stays for compat but is frozen.
45
- INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
46
- ('injections_total', 0),
47
- ('injections_effective', 0),
48
- ('injections_ineffective', 0),
49
- ('patterns_promoted_new', 0);
50
-
51
- -- 4. kevin_settings: seed new v0.4 flags.
52
- INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
53
- ('quality_gate_enabled', '1'),
54
- ('lesson_snippet_injection','1');
55
-
56
- -- 5. Seed version 005.
57
- INSERT OR IGNORE INTO schema_version (version) VALUES ('005');
1
+ -- ============================================================
2
+ -- Kevin 0.4.0 — Migration 005: Signal over Noise (additive)
3
+ -- ============================================================
4
+ -- Backward-compatible, additive only. All new columns are
5
+ -- nullable or carry a NOT NULL DEFAULT so legacy rows keep
6
+ -- working without a destructive rebuild.
7
+ -- ============================================================
8
+
9
+ -- 1. memories: positive/negative evidence split (D4-03).
10
+ -- recurrence_count — how many times this fingerprint recurred AFTER
11
+ -- injection (negative evidence; lowers confidence).
12
+ -- fix_args — deterministic capture of the linked success call's
13
+ -- args_summary ("Fixed by:" raw material, D4-07).
14
+ -- last_injected_at — timestamp of the most recent injection of this memory.
15
+ ALTER TABLE memories ADD COLUMN recurrence_count INTEGER NOT NULL DEFAULT 0;
16
+ ALTER TABLE memories ADD COLUMN fix_args TEXT;
17
+ ALTER TABLE memories ADD COLUMN last_injected_at TEXT;
18
+
19
+ -- 2. kevin_injections: the injection ledger (D4-04). One row per injected
20
+ -- memory per prompt/compaction, settled at session.idle.
21
+ CREATE TABLE IF NOT EXISTS kevin_injections (
22
+ id TEXT PRIMARY KEY,
23
+ memory_id TEXT NOT NULL,
24
+ fingerprint TEXT NOT NULL,
25
+ session_id TEXT NOT NULL,
26
+ hook TEXT NOT NULL CHECK (hook IN ('pre_prompt', 'compacting')),
27
+ tokens INTEGER NOT NULL,
28
+ injected_at TEXT NOT NULL DEFAULT (datetime('now')),
29
+ outcome TEXT CHECK (outcome IN ('unmeasured', 'effective', 'ineffective'))
30
+ NOT NULL DEFAULT 'unmeasured'
31
+ );
32
+
33
+ -- 2b. Indexes: settlement by session, recurrence lookups by fingerprint,
34
+ -- and outcome rollups for precision_rate.
35
+ CREATE INDEX IF NOT EXISTS idx_injections_fp
36
+ ON kevin_injections(fingerprint);
37
+ CREATE INDEX IF NOT EXISTS idx_injections_session
38
+ ON kevin_injections(session_id);
39
+ CREATE INDEX IF NOT EXISTS idx_injections_outcome
40
+ ON kevin_injections(outcome);
41
+
42
+ -- 3. kevin_metrics: seed new v0.4 counters.
43
+ -- patterns_promoted_new replaces patterns_causal (which was inflated by
44
+ -- idempotent refreshes); the latter stays for compat but is frozen.
45
+ INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
46
+ ('injections_total', 0),
47
+ ('injections_effective', 0),
48
+ ('injections_ineffective', 0),
49
+ ('patterns_promoted_new', 0);
50
+
51
+ -- 4. kevin_settings: seed new v0.4 flags.
52
+ INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
53
+ ('quality_gate_enabled', '1'),
54
+ ('lesson_snippet_injection','1');
55
+
56
+ -- 5. Seed version 005.
57
+ INSERT OR IGNORE INTO schema_version (version) VALUES ('005');
@@ -1,118 +1,118 @@
1
- -- ============================================================================
2
- -- 006_v05_glassbox.sql — v0.5.0 "Glass Box"
3
- --
4
- -- Honest measurement, human feedback, lifecycle completion.
5
- --
6
- -- Section 1: rebuild kevin_injections to admit a fourth outcome.
7
- -- Section 2: human feedback storage.
8
- -- Section 3: memory lifecycle columns.
9
- -- Section 4: metric seeds.
10
- -- Section 5: setting seeds.
11
- -- Section 6: schema_version.
12
- -- ============================================================================
13
-
14
- -- ---------------------------------------------------------------------------
15
- -- 1. kevin_injections: add 'inconclusive'.
16
- --
17
- -- SQLite cannot ALTER a CHECK constraint, so the table must be rebuilt.
18
- -- Migration 004 set this precedent. kevin_injections has no FTS5 triggers,
19
- -- so unlike 004 this is a straight four-step rebuild.
20
- --
21
- -- Existing rows with outcome='effective' are remapped to 'inconclusive'.
22
- -- This is not data loss: v0.4's 'effective' meant "the error did not recur",
23
- -- which is the exact definition of the new 'inconclusive' bucket. Rows that
24
- -- genuinely earned the new 'effective' will be re-settled naturally, and the
25
- -- post-apply hook re-derives the counters from the table.
26
- -- ---------------------------------------------------------------------------
27
- CREATE TABLE IF NOT EXISTS kevin_injections_new (
28
- id TEXT PRIMARY KEY,
29
- memory_id TEXT NOT NULL,
30
- fingerprint TEXT NOT NULL,
31
- session_id TEXT NOT NULL,
32
- hook TEXT NOT NULL CHECK (hook IN ('pre_prompt','compacting')),
33
- tokens INTEGER NOT NULL,
34
- injected_at TEXT NOT NULL DEFAULT (datetime('now')),
35
- outcome TEXT NOT NULL DEFAULT 'unmeasured'
36
- CHECK (outcome IN ('unmeasured','effective','ineffective','inconclusive'))
37
- );
38
-
39
- INSERT INTO kevin_injections_new
40
- (id, memory_id, fingerprint, session_id, hook, tokens, injected_at, outcome)
41
- SELECT
42
- id, memory_id, fingerprint, session_id, hook, tokens, injected_at,
43
- CASE WHEN outcome = 'effective' THEN 'inconclusive' ELSE outcome END
44
- FROM kevin_injections;
45
-
46
- DROP TABLE kevin_injections;
47
- ALTER TABLE kevin_injections_new RENAME TO kevin_injections;
48
-
49
- CREATE INDEX IF NOT EXISTS idx_injections_fp ON kevin_injections(fingerprint);
50
- CREATE INDEX IF NOT EXISTS idx_injections_session ON kevin_injections(session_id);
51
- CREATE INDEX IF NOT EXISTS idx_injections_outcome ON kevin_injections(outcome);
52
-
53
- -- ---------------------------------------------------------------------------
54
- -- 2. Human feedback. Append-only audit trail; the hot path reads the
55
- -- denormalized counters on `memories` (section 3), never this table.
56
- -- ---------------------------------------------------------------------------
57
- CREATE TABLE IF NOT EXISTS memory_feedback (
58
- id TEXT PRIMARY KEY,
59
- memory_id TEXT NOT NULL,
60
- verdict TEXT NOT NULL CHECK (verdict IN ('useful','wrong','outdated','ignore')),
61
- session_id TEXT,
62
- note TEXT,
63
- created_at TEXT NOT NULL DEFAULT (datetime('now'))
64
- );
65
-
66
- CREATE INDEX IF NOT EXISTS idx_feedback_memory ON memory_feedback(memory_id);
67
- CREATE INDEX IF NOT EXISTS idx_feedback_created ON memory_feedback(created_at);
68
-
69
- -- ---------------------------------------------------------------------------
70
- -- 3. Memory lifecycle and feedback columns.
71
- --
72
- -- feedback_positive / feedback_negative are SEPARATE from evidence_count
73
- -- and recurrence_count by design: human judgement is evidence about the
74
- -- memory, causal counters are evidence about the world. Mixing them was
75
- -- the confidence-poisoning defect closed in v0.4.0.
76
- --
77
- -- superseded_by has no REFERENCES clause on purpose. Store enables
78
- -- PRAGMA foreign_keys=ON, and a hard FK would block deletion of a memory
79
- -- that superseded another.
80
- -- ---------------------------------------------------------------------------
81
- ALTER TABLE memories ADD COLUMN feedback_positive INTEGER NOT NULL DEFAULT 0;
82
- ALTER TABLE memories ADD COLUMN feedback_negative INTEGER NOT NULL DEFAULT 0;
83
- ALTER TABLE memories ADD COLUMN ignored INTEGER NOT NULL DEFAULT 0;
84
- ALTER TABLE memories ADD COLUMN superseded_by TEXT;
85
- ALTER TABLE memories ADD COLUMN archived_at TEXT;
86
-
87
- CREATE INDEX IF NOT EXISTS idx_memories_ignored ON memories(ignored);
88
- CREATE INDEX IF NOT EXISTS idx_memories_archived ON memories(archived_at);
89
-
90
- -- ---------------------------------------------------------------------------
91
- -- 4. Metric seeds. Order matches the additions to METRIC_KEYS in metrics.ts.
92
- -- ---------------------------------------------------------------------------
93
- INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
94
- ('injections_inconclusive', 0),
95
- ('injections_blocked_seen', 0),
96
- ('injections_blocked_weak', 0),
97
- ('injections_blocked_recurrence',0),
98
- ('injections_blocked_stale', 0),
99
- ('injections_blocked_ignored', 0),
100
- ('feedback_positive_total', 0),
101
- ('feedback_negative_total', 0),
102
- ('memories_archived', 0);
103
-
104
- -- ---------------------------------------------------------------------------
105
- -- 5. Setting seeds. Values are TEXT, always. Read them with an explicit
106
- -- string comparison or an explicit Number() parse — never `=== 1`.
107
- -- (That exact mistake kept cross_project_enabled unreachable for the
108
- -- whole of v0.3.0.)
109
- -- ---------------------------------------------------------------------------
110
- INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
111
- ('deterministic_retrieval', '0'),
112
- ('pre_prompt_budget_tokens', '900'),
113
- ('archive_after_days', '30');
114
-
115
- -- ---------------------------------------------------------------------------
116
- -- 6. Version marker.
117
- -- ---------------------------------------------------------------------------
118
- INSERT OR IGNORE INTO schema_version (version) VALUES ('006');
1
+ -- ============================================================================
2
+ -- 006_v05_glassbox.sql — v0.5.0 "Glass Box"
3
+ --
4
+ -- Honest measurement, human feedback, lifecycle completion.
5
+ --
6
+ -- Section 1: rebuild kevin_injections to admit a fourth outcome.
7
+ -- Section 2: human feedback storage.
8
+ -- Section 3: memory lifecycle columns.
9
+ -- Section 4: metric seeds.
10
+ -- Section 5: setting seeds.
11
+ -- Section 6: schema_version.
12
+ -- ============================================================================
13
+
14
+ -- ---------------------------------------------------------------------------
15
+ -- 1. kevin_injections: add 'inconclusive'.
16
+ --
17
+ -- SQLite cannot ALTER a CHECK constraint, so the table must be rebuilt.
18
+ -- Migration 004 set this precedent. kevin_injections has no FTS5 triggers,
19
+ -- so unlike 004 this is a straight four-step rebuild.
20
+ --
21
+ -- Existing rows with outcome='effective' are remapped to 'inconclusive'.
22
+ -- This is not data loss: v0.4's 'effective' meant "the error did not recur",
23
+ -- which is the exact definition of the new 'inconclusive' bucket. Rows that
24
+ -- genuinely earned the new 'effective' will be re-settled naturally, and the
25
+ -- post-apply hook re-derives the counters from the table.
26
+ -- ---------------------------------------------------------------------------
27
+ CREATE TABLE IF NOT EXISTS kevin_injections_new (
28
+ id TEXT PRIMARY KEY,
29
+ memory_id TEXT NOT NULL,
30
+ fingerprint TEXT NOT NULL,
31
+ session_id TEXT NOT NULL,
32
+ hook TEXT NOT NULL CHECK (hook IN ('pre_prompt','compacting')),
33
+ tokens INTEGER NOT NULL,
34
+ injected_at TEXT NOT NULL DEFAULT (datetime('now')),
35
+ outcome TEXT NOT NULL DEFAULT 'unmeasured'
36
+ CHECK (outcome IN ('unmeasured','effective','ineffective','inconclusive'))
37
+ );
38
+
39
+ INSERT INTO kevin_injections_new
40
+ (id, memory_id, fingerprint, session_id, hook, tokens, injected_at, outcome)
41
+ SELECT
42
+ id, memory_id, fingerprint, session_id, hook, tokens, injected_at,
43
+ CASE WHEN outcome = 'effective' THEN 'inconclusive' ELSE outcome END
44
+ FROM kevin_injections;
45
+
46
+ DROP TABLE kevin_injections;
47
+ ALTER TABLE kevin_injections_new RENAME TO kevin_injections;
48
+
49
+ CREATE INDEX IF NOT EXISTS idx_injections_fp ON kevin_injections(fingerprint);
50
+ CREATE INDEX IF NOT EXISTS idx_injections_session ON kevin_injections(session_id);
51
+ CREATE INDEX IF NOT EXISTS idx_injections_outcome ON kevin_injections(outcome);
52
+
53
+ -- ---------------------------------------------------------------------------
54
+ -- 2. Human feedback. Append-only audit trail; the hot path reads the
55
+ -- denormalized counters on `memories` (section 3), never this table.
56
+ -- ---------------------------------------------------------------------------
57
+ CREATE TABLE IF NOT EXISTS memory_feedback (
58
+ id TEXT PRIMARY KEY,
59
+ memory_id TEXT NOT NULL,
60
+ verdict TEXT NOT NULL CHECK (verdict IN ('useful','wrong','outdated','ignore')),
61
+ session_id TEXT,
62
+ note TEXT,
63
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
64
+ );
65
+
66
+ CREATE INDEX IF NOT EXISTS idx_feedback_memory ON memory_feedback(memory_id);
67
+ CREATE INDEX IF NOT EXISTS idx_feedback_created ON memory_feedback(created_at);
68
+
69
+ -- ---------------------------------------------------------------------------
70
+ -- 3. Memory lifecycle and feedback columns.
71
+ --
72
+ -- feedback_positive / feedback_negative are SEPARATE from evidence_count
73
+ -- and recurrence_count by design: human judgement is evidence about the
74
+ -- memory, causal counters are evidence about the world. Mixing them was
75
+ -- the confidence-poisoning defect closed in v0.4.0.
76
+ --
77
+ -- superseded_by has no REFERENCES clause on purpose. Store enables
78
+ -- PRAGMA foreign_keys=ON, and a hard FK would block deletion of a memory
79
+ -- that superseded another.
80
+ -- ---------------------------------------------------------------------------
81
+ ALTER TABLE memories ADD COLUMN feedback_positive INTEGER NOT NULL DEFAULT 0;
82
+ ALTER TABLE memories ADD COLUMN feedback_negative INTEGER NOT NULL DEFAULT 0;
83
+ ALTER TABLE memories ADD COLUMN ignored INTEGER NOT NULL DEFAULT 0;
84
+ ALTER TABLE memories ADD COLUMN superseded_by TEXT;
85
+ ALTER TABLE memories ADD COLUMN archived_at TEXT;
86
+
87
+ CREATE INDEX IF NOT EXISTS idx_memories_ignored ON memories(ignored);
88
+ CREATE INDEX IF NOT EXISTS idx_memories_archived ON memories(archived_at);
89
+
90
+ -- ---------------------------------------------------------------------------
91
+ -- 4. Metric seeds. Order matches the additions to METRIC_KEYS in metrics.ts.
92
+ -- ---------------------------------------------------------------------------
93
+ INSERT OR IGNORE INTO kevin_metrics (key, value) VALUES
94
+ ('injections_inconclusive', 0),
95
+ ('injections_blocked_seen', 0),
96
+ ('injections_blocked_weak', 0),
97
+ ('injections_blocked_recurrence',0),
98
+ ('injections_blocked_stale', 0),
99
+ ('injections_blocked_ignored', 0),
100
+ ('feedback_positive_total', 0),
101
+ ('feedback_negative_total', 0),
102
+ ('memories_archived', 0);
103
+
104
+ -- ---------------------------------------------------------------------------
105
+ -- 5. Setting seeds. Values are TEXT, always. Read them with an explicit
106
+ -- string comparison or an explicit Number() parse — never `=== 1`.
107
+ -- (That exact mistake kept cross_project_enabled unreachable for the
108
+ -- whole of v0.3.0.)
109
+ -- ---------------------------------------------------------------------------
110
+ INSERT OR IGNORE INTO kevin_settings (key, value) VALUES
111
+ ('deterministic_retrieval', '0'),
112
+ ('pre_prompt_budget_tokens', '900'),
113
+ ('archive_after_days', '30');
114
+
115
+ -- ---------------------------------------------------------------------------
116
+ -- 6. Version marker.
117
+ -- ---------------------------------------------------------------------------
118
+ INSERT OR IGNORE INTO schema_version (version) VALUES ('006');