@jeffober/thatch 0.1.18 → 0.1.20
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.
- package/README.md +50 -224
- package/artifacts/skills/pr-description.md +140 -62
- package/artifacts/skills/thatch-code-review.md +10 -2
- package/artifacts/skills/thatch-review-synthesizer.md +29 -0
- package/artifacts/skills/ticket-description.md +53 -23
- package/bin/thatch +73 -7
- package/docs/dev/skills.md +5 -5
- package/docs/in-progress/prediction-engine.md +404 -0
- package/docs/qa/README.md +33 -1
- package/docs/qa/use-cases/README.md +4 -0
- package/docs/qa/use-cases/UC-021-prediction-autofire.md +21 -0
- package/docs/qa/use-cases/UC-022-prediction-dedup.md +19 -0
- package/docs/qa/use-cases/UC-023-prediction-confidence-model.md +23 -0
- package/docs/qa/use-cases/UC-024-prediction-delete.md +20 -0
- package/docs/user/README.md +6 -5
- package/package.json +1 -1
- package/src/db.ts +480 -0
- package/src/index.ts +54 -18
- package/src/prompts.ts +119 -11
- package/src/sideband.ts +51 -13
- package/src/tool-defs.ts +203 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# Prediction engine (in progress)
|
|
2
|
+
|
|
3
|
+
> **Status: implementation complete across both host paths.** The
|
|
4
|
+
> schema, tools, auto-fire (opencode), sideband prediction fire (MCP),
|
|
5
|
+
> prompt instructions, and tests are shipped and passing the quality
|
|
6
|
+
> gate. The feature is ready for field testing. This doc is the live
|
|
7
|
+
> ledger; when the work closes, graduate durable parts into a permanent
|
|
8
|
+
> `docs/dev/prediction-engine.md` and retire this file.
|
|
9
|
+
|
|
10
|
+
## Purpose
|
|
11
|
+
|
|
12
|
+
A **user decision model** that learns the user's preferences for
|
|
13
|
+
judgment calls the coding agent faces every session: scope (how much to
|
|
14
|
+
do), appropriateness (is this the right thing), methodology (the right
|
|
15
|
+
way). The agent gets a "guess what the user would want in this
|
|
16
|
+
situation" system -- inspired by nak's samskara predictive model, but
|
|
17
|
+
different mechanism and domain.
|
|
18
|
+
|
|
19
|
+
The delta over flat thatch memory is three things flat memory cannot do:
|
|
20
|
+
|
|
21
|
+
1. **Graded confidence** -- memory is binary (stored or not).
|
|
22
|
+
Predictions have a Bayesian posterior that is reinforced or weakened
|
|
23
|
+
by user feedback. This enables ambiguity-surfacing: when confidence
|
|
24
|
+
is thin or predictions conflict, the agent asks the user instead of
|
|
25
|
+
guessing.
|
|
26
|
+
2. **Context-disambiguation** -- matchers separate "when" from "what."
|
|
27
|
+
The same topic (tech debt in a PR) can produce opposite predictions
|
|
28
|
+
in different situations (flaky test blocking CI = fix it; unrelated
|
|
29
|
+
to change = skip it). Flat memory encodes this in prose the LLM must
|
|
30
|
+
re-reason about every time; the prediction engine encodes it as
|
|
31
|
+
structure.
|
|
32
|
+
3. **Agent-driven feedback loop** -- the LLM itself decides to
|
|
33
|
+
strengthen or weaken a prediction based on user feedback, creating a
|
|
34
|
+
loop that flat memory lacks.
|
|
35
|
+
|
|
36
|
+
## Design decisions (settled)
|
|
37
|
+
|
|
38
|
+
These came out of design discussion with Jeff, July 2026. They are
|
|
39
|
+
constraints, not open questions.
|
|
40
|
+
|
|
41
|
+
1. **Auto-fire at chat.message.** The hook already embeds the user's
|
|
42
|
+
text for the recall nudge. The prediction fire shares that embedding
|
|
43
|
+
and does a second cosine search against the matchers table. When
|
|
44
|
+
matchers clear a relevance floor, scored predictions are injected as
|
|
45
|
+
a synthetic nudge part. No matchers fire = nothing injected (zero
|
|
46
|
+
tokens, zero latency). No "if appropriate" hedging -- the block's
|
|
47
|
+
presence is the appropriateness signal.
|
|
48
|
+
|
|
49
|
+
2. **Tool-based update, agent-driven.** No local LLM classifier. Prompt
|
|
50
|
+
instructions tell the agent: when the user corrects you, answers a
|
|
51
|
+
question, or provides a clear signal, use prediction_update to
|
|
52
|
+
create/reinforce/weaken a prediction. The LLM decides with
|
|
53
|
+
appropriately nuanced prompt instructions; the tool is the trusted
|
|
54
|
+
boundary that handles embedding, dedup, and confidence math.
|
|
55
|
+
|
|
56
|
+
3. **Statistical model query, not AI.** The query (embed + cosine +
|
|
57
|
+
weighted edges) is mechanical -- same as thatch_memory_recall but
|
|
58
|
+
against different tables. No model call. The agent can also call
|
|
59
|
+
prediction_query explicitly when it wants to check the model before a
|
|
60
|
+
judgment call.
|
|
61
|
+
|
|
62
|
+
4. **No local LLM.** Considered and declined. The host LLM is already
|
|
63
|
+
reading the message and classifying "was this a correction?" as part
|
|
64
|
+
of understanding it. Adding a second classifier pays twice for the
|
|
65
|
+
same answer. The cost of not having a classifier is wasted nudge text
|
|
66
|
+
on turns with no signal -- not wasted model calls.
|
|
67
|
+
|
|
68
|
+
5. **Descriptive context, not directive.** The injected block reads
|
|
69
|
+
"[0.72 conf, 4 tests] When X: you tend to Y" -- same shape as
|
|
70
|
+
samskara's compound summary. The system prompt instructions govern
|
|
71
|
+
how to act on it (follow strong silently, surface weak/competing,
|
|
72
|
+
update on user response). No "if appropriate" or "consider" hedging
|
|
73
|
+
in the injection itself.
|
|
74
|
+
|
|
75
|
+
6. **Confidence mirrors samskara health.** A Bayesian posterior with
|
|
76
|
+
population-prior shrinkage: `confidence = (confirm + k*p0) /
|
|
77
|
+
(confirm + disconfirm + k)`. No wall-clock decay -- relevance-gated,
|
|
78
|
+
like samskara. Being tested (user gives feedback) is what moves
|
|
79
|
+
confidence. Untested predictions stay at p0.
|
|
80
|
+
|
|
81
|
+
7. **LLM is the conflict detector.** The auto-fire returns all
|
|
82
|
+
predictions from matching matchers, not just the top one. The LLM
|
|
83
|
+
reads competing predictions and decides whether they conflict. No
|
|
84
|
+
mechanical conflict heuristic -- the LLM is better at semantic
|
|
85
|
+
conflict detection than any rule.
|
|
86
|
+
|
|
87
|
+
## The normative asymmetry (carried from samskara)
|
|
88
|
+
|
|
89
|
+
Samskara's intents doc identifies a normative asymmetry: the descriptive
|
|
90
|
+
layer (samskara predictions) is ethically free to surface, but the
|
|
91
|
+
normative layer (intents -- "what the model is working toward") carries
|
|
92
|
+
weight because it shapes behavior. The prediction engine is descriptive
|
|
93
|
+
("you tend to prefer X"), not normative ("I'm working toward X with
|
|
94
|
+
you"). It reads user behavior, it does not pursue an agenda. The
|
|
95
|
+
firewall from samskara's efficacy model carries over: the model that
|
|
96
|
+
uses predictions cannot grade its own predictions -- the user's
|
|
97
|
+
feedback is the only signal that moves confidence.
|
|
98
|
+
|
|
99
|
+
## Data model
|
|
100
|
+
|
|
101
|
+
Four new tables in the same SQLite DB, same conventions as the existing
|
|
102
|
+
`entries`/`dedup_pairs` tables (idempotent CREATE TABLE IF NOT EXISTS,
|
|
103
|
+
BLOB embeddings, store column, crypto.randomUUID PKs).
|
|
104
|
+
|
|
105
|
+
### `prediction_matchers`
|
|
106
|
+
|
|
107
|
+
Context patterns describing situations. The "when."
|
|
108
|
+
|
|
109
|
+
- `id TEXT PRIMARY KEY` (crypto.randomUUID).
|
|
110
|
+
- `store TEXT NOT NULL`.
|
|
111
|
+
- `description TEXT NOT NULL` -- "reviewing a PR with pre-existing tech
|
|
112
|
+
debt."
|
|
113
|
+
- `embedding BLOB` -- 384-dim Float32Array, passageEmbed (no query
|
|
114
|
+
prefix; matchers are passages, not queries).
|
|
115
|
+
- `model TEXT`.
|
|
116
|
+
- `created_at`, `updated_at` -- ISO timestamps.
|
|
117
|
+
|
|
118
|
+
### `predictions`
|
|
119
|
+
|
|
120
|
+
User preference statements. The "what."
|
|
121
|
+
|
|
122
|
+
- `id TEXT PRIMARY KEY`.
|
|
123
|
+
- `store TEXT NOT NULL`.
|
|
124
|
+
- `statement TEXT NOT NULL` -- "skip it unless it's blocking this
|
|
125
|
+
change."
|
|
126
|
+
- `rationale TEXT` -- why this prediction exists (inspector context).
|
|
127
|
+
- `embedding BLOB` -- for dedup at creation time.
|
|
128
|
+
- `model TEXT`.
|
|
129
|
+
- `confidence REAL NOT NULL DEFAULT 0.5` -- Bayesian posterior [0,1].
|
|
130
|
+
- `confirm_count REAL NOT NULL DEFAULT 0` -- fractional (soft signals
|
|
131
|
+
are sub-unit).
|
|
132
|
+
- `disconfirm_count REAL NOT NULL DEFAULT 0` -- same.
|
|
133
|
+
- `created_at`, `updated_at`.
|
|
134
|
+
|
|
135
|
+
### `prediction_edges`
|
|
136
|
+
|
|
137
|
+
Weighted matcher-to-prediction links. The "context-dependence."
|
|
138
|
+
|
|
139
|
+
- `matcher_id TEXT NOT NULL` (FK to prediction_matchers, CASCADE).
|
|
140
|
+
- `prediction_id TEXT NOT NULL` (FK to predictions, CASCADE).
|
|
141
|
+
- `weight REAL NOT NULL DEFAULT 1.0`.
|
|
142
|
+
- `created_at`.
|
|
143
|
+
- `PRIMARY KEY (matcher_id, prediction_id)`.
|
|
144
|
+
|
|
145
|
+
### `prediction_provenance`
|
|
146
|
+
|
|
147
|
+
Audit trail. Gives the inspector context and the agent "plenty of
|
|
148
|
+
context" for nuance.
|
|
149
|
+
|
|
150
|
+
- `id TEXT PRIMARY KEY`.
|
|
151
|
+
- `prediction_id TEXT NOT NULL` (FK to predictions, CASCADE).
|
|
152
|
+
- `signal TEXT NOT NULL` -- 'confirm' | 'disconfirm' | 'soft' |
|
|
153
|
+
'create'.
|
|
154
|
+
- `detail TEXT` -- what the user said or did.
|
|
155
|
+
- `created_at`.
|
|
156
|
+
|
|
157
|
+
## Confidence model
|
|
158
|
+
|
|
159
|
+
Mirrors samskara's health posterior with population-prior shrinkage.
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
confidence = (confirm_count + k * p0) / (confirm_count + disconfirm_count + k)
|
|
163
|
+
|
|
164
|
+
k = 5 (prior strength, pseudo-count)
|
|
165
|
+
p0 = 0.5 (population baseline; make population-derived later)
|
|
166
|
+
W_SOFT = 0.25 (soft miss weight, same as samskara)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
- `confirm` signal: confirm_count += 1.
|
|
170
|
+
- `disconfirm` signal: disconfirm_count += 1.
|
|
171
|
+
- `soft` signal: disconfirm_count += W_SOFT (0.25).
|
|
172
|
+
- `create` signal: no confidence adjustment, just seeds at p0.
|
|
173
|
+
- No wall-clock decay. Relevance-gated: being tested (user gives
|
|
174
|
+
feedback) is what moves confidence. Untested = stays at p0.
|
|
175
|
+
- confirm_count / disconfirm_count are REAL (fractional). An integer
|
|
176
|
+
column would truncate soft signals to 0 (samskara's bug).
|
|
177
|
+
- p0 starts at 0.5 for v1. Later: `populationP0(store)` = sum(confirm) /
|
|
178
|
+
sum(confirm + disconfirm), with a 0.5 fallback under 20 evidence.
|
|
179
|
+
|
|
180
|
+
## Scoring at query time
|
|
181
|
+
|
|
182
|
+
```
|
|
183
|
+
score = cosine(user_text, matcher.embedding) * edge.weight * prediction.confidence
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
The auto-fire returns all predictions from matching matchers (not just
|
|
187
|
+
the top scoring one), so the LLM can read conflicts. The score is for
|
|
188
|
+
ranking and display, not for a mechanical "follow if above X" gate --
|
|
189
|
+
the LLM decides whether to follow, surface, or ignore, informed by the
|
|
190
|
+
confidence and evidence count shown alongside.
|
|
191
|
+
|
|
192
|
+
## Ambiguity-surfacing (the killer feature)
|
|
193
|
+
|
|
194
|
+
- **Strong prediction** (high confidence, sufficient evidence) -> agent
|
|
195
|
+
follows silently, does not interrupt.
|
|
196
|
+
- **No prediction** -> agent asks the user (same as today).
|
|
197
|
+
- **Ambiguous or competing predictions** -> agent surfaces naturally:
|
|
198
|
+
"I think you usually prefer X here, but I'm not sure -- what do you
|
|
199
|
+
want?"
|
|
200
|
+
- **User responds to a surfaced prediction** -> agent calls
|
|
201
|
+
prediction_update to reinforce or weaken.
|
|
202
|
+
|
|
203
|
+
Value = fewer interruptions, not more. High-confidence predictions
|
|
204
|
+
suppress questions the agent would otherwise ask.
|
|
205
|
+
|
|
206
|
+
## Tool surface
|
|
207
|
+
|
|
208
|
+
Four tools, following the existing ToolDef pattern in tool-defs.ts.
|
|
209
|
+
|
|
210
|
+
### `prediction_query`
|
|
211
|
+
|
|
212
|
+
Mechanical search. Returns scored predictions with matcher
|
|
213
|
+
description, confidence, and evidence count.
|
|
214
|
+
|
|
215
|
+
Args: `context` (string), `store` (optional).
|
|
216
|
+
|
|
217
|
+
### `prediction_update`
|
|
218
|
+
|
|
219
|
+
The trusted boundary. The agent says "here's the situation, here's the
|
|
220
|
+
preference, here's what happened." The tool handles: embed matcher,
|
|
221
|
+
find/create by cosine dedup, embed prediction, find/create by cosine
|
|
222
|
+
dedup, adjust confidence, record provenance.
|
|
223
|
+
|
|
224
|
+
Args: `matcher` (string), `prediction` (string), `signal` (confirm |
|
|
225
|
+
disconfirm | soft | create), `rationale` (string), `store` (optional).
|
|
226
|
+
|
|
227
|
+
### `prediction_list`
|
|
228
|
+
|
|
229
|
+
Inspector. Lists all predictions with confidence, evidence, matchers,
|
|
230
|
+
and rationale.
|
|
231
|
+
|
|
232
|
+
Args: `store` (optional).
|
|
233
|
+
|
|
234
|
+
## Auto-fire in chat.message hook
|
|
235
|
+
|
|
236
|
+
Slots into the existing recall-nudge path in index.ts, sharing the
|
|
237
|
+
embedding call. After the recall nudge block, before the catch:
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
embedding = await model.queryEmbed(promptText) // already computed
|
|
241
|
+
|
|
242
|
+
// recall nudge (existing)
|
|
243
|
+
results = db.search([repo, "global"], embedding, { limit: 5 })
|
|
244
|
+
if matches >= RECALL_THRESHOLD -> inject recallNudge
|
|
245
|
+
|
|
246
|
+
// prediction fire (new)
|
|
247
|
+
predItems = db.scorePredictionNudge([repo, "global"], embedding, PREDICTION_THRESHOLD)
|
|
248
|
+
if predItems.length > 0:
|
|
249
|
+
output.parts.push(predictionNudge(predItems)) // synthetic: true
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
One more cosine scan against a separate table. Same brute-force
|
|
253
|
+
pattern, same cost profile. No extra embedding call.
|
|
254
|
+
|
|
255
|
+
## System prompt instructions
|
|
256
|
+
|
|
257
|
+
New section in all three prompt variants (systemPrompt,
|
|
258
|
+
claudeInstructions, cursorInstructions), after "Before Responding."
|
|
259
|
+
|
|
260
|
+
New tools registered in the tool list line:
|
|
261
|
+
`thatch_prediction_query`, `thatch_prediction_update`,
|
|
262
|
+
`thatch_prediction_list` (opencode); bare names for MCP.
|
|
263
|
+
|
|
264
|
+
The injected block format:
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
## User decision model
|
|
268
|
+
- [0.72 conf, 4 tests] When reviewing a PR with pre-existing tech debt: you tend to skip it unless it's blocking the change
|
|
269
|
+
- [0.61 conf, 2 tests] When the tech debt is a flaky test: you've preferred fixing it
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
Prompt instructions:
|
|
273
|
+
- Follow strong predictions silently.
|
|
274
|
+
- Surface weak or competing predictions to the user naturally.
|
|
275
|
+
- When the user responds to a surfaced prediction, use
|
|
276
|
+
prediction_update to reinforce or weaken.
|
|
277
|
+
- When the user corrects you, answers a question, or provides a clear
|
|
278
|
+
signal, use prediction_query then prediction_update.
|
|
279
|
+
|
|
280
|
+
## Constants
|
|
281
|
+
|
|
282
|
+
```
|
|
283
|
+
PREDICTION_THRESHOLD = 0.45 // lower than RECALL_THRESHOLD (0.55)
|
|
284
|
+
MATCHER_DEDUP_COSINE = 0.85 // near-duplicate check at creation
|
|
285
|
+
PREDICTION_DEDUP_COSINE = 0.85
|
|
286
|
+
P0 = 0.5 // prior (population-derived later)
|
|
287
|
+
K = 5 // prior strength
|
|
288
|
+
W_SOFT = 0.25 // soft miss weight
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## Hook integration
|
|
292
|
+
|
|
293
|
+
The `tool.execute.after` hook (index.ts) already skips
|
|
294
|
+
`thatch_*`-prefixed tools from the extraction buffer.
|
|
295
|
+
`thatch_prediction_*` is caught by that check -- no special handling
|
|
296
|
+
needed. MCP path same: `mcp__thatch__prediction_*` caught by the
|
|
297
|
+
`mcp__thatch__*` filter in extract-queue.ts.
|
|
298
|
+
|
|
299
|
+
Prediction calls do NOT drain the extraction buffer -- they are a
|
|
300
|
+
separate system. The agent may do both in the same turn (save a memory
|
|
301
|
+
AND update a prediction).
|
|
302
|
+
|
|
303
|
+
## Build status
|
|
304
|
+
|
|
305
|
+
Landed so far:
|
|
306
|
+
|
|
307
|
+
1. DB schema + methods (db.ts) -- four new tables (prediction_matchers,
|
|
308
|
+
predictions, prediction_edges, prediction_provenance) in #initSchema,
|
|
309
|
+
plus findMatchers, scorePredictions, findNearestMatcher,
|
|
310
|
+
findNearestPrediction, createMatcher, createPrediction, createEdge,
|
|
311
|
+
adjustConfidence, addProvenance, listPredictions, populationP0,
|
|
312
|
+
getPrediction, getProvenance, deletePrediction, scorePredictionNudge.
|
|
313
|
+
Confidence uses Bayesian posterior with k=5, p0=0.5, W_SOFT=0.25.
|
|
314
|
+
2. Tool definitions (tool-defs.ts) -- prediction_query,
|
|
315
|
+
prediction_update, prediction_list, prediction_delete. Added to
|
|
316
|
+
TOOL_DEFS array (9 -> 13).
|
|
317
|
+
3. Auto-fire (index.ts) -- prediction fire in chat.message hook,
|
|
318
|
+
sharing embedding with recall nudge. Prediction blocks injected
|
|
319
|
+
when matchers clear PREDICTION_THRESHOLD (0.45). predictionNudge
|
|
320
|
+
formatter in prompts.ts.
|
|
321
|
+
4. System prompt (prompts.ts) -- "User Decision Model" section in all
|
|
322
|
+
three variants (systemPrompt, claudeInstructions,
|
|
323
|
+
cursorInstructions). New tools in all three tool lists.
|
|
324
|
+
5. Tools wiring (tools.ts + mcp.ts) -- automatic via TOOL_DEFS
|
|
325
|
+
iteration. No manual changes needed.
|
|
326
|
+
6. Tests -- tests/prediction.test.ts (schema, confidence math, dedup,
|
|
327
|
+
scoring, provenance, listPredictions, populationP0). Updated
|
|
328
|
+
tool-defs.test.ts and plugin.test.ts tool count assertions (9 -> 13).
|
|
329
|
+
7. MCP path (sideband.ts + bin/thatch) -- the sideband server now
|
|
330
|
+
handles `method: "predictions"` alongside `method: "match"`, so
|
|
331
|
+
the warm MCP server can query matchers and score predictions for
|
|
332
|
+
one-shot hook processes. The `flush-tools` CLI subcommand (already
|
|
333
|
+
wired into UserPromptSubmit hooks for Claude Code and Cursor) now
|
|
334
|
+
fires predictions alongside the recall nudge in its tier 2 block,
|
|
335
|
+
sharing one sideband round-trip via `Promise.all`. A standalone
|
|
336
|
+
`flush-predictions` subcommand is also available for testing.
|
|
337
|
+
No new hook installation needed -- the existing `flush-tools` hook
|
|
338
|
+
covers it.
|
|
339
|
+
|
|
340
|
+
Future enhancements:
|
|
341
|
+
|
|
342
|
+
1. **p0 population derivation** -- v1 uses flat 0.5. Once enough
|
|
343
|
+
predictions accumulate, switch to population-derived p0 per store.
|
|
344
|
+
2. **Auto-fire block separation** -- separate synthetic part from the
|
|
345
|
+
recall nudge, or combined? Starting with separate (simpler).
|
|
346
|
+
3. **Inspector UI** -- `thatch prediction list` CLI subcommand for
|
|
347
|
+
terminal inspection. No Svelte UI (thatch has no UI).
|
|
348
|
+
4. **Score formula tuning** -- v1 is `cosine * weight * confidence`.
|
|
349
|
+
If confidence dominates (high confidence on weak cosine swamps low
|
|
350
|
+
confidence on strong cosine), add samskara's `cosine^1.3` power.
|
|
351
|
+
Start simple; let data drive.
|
|
352
|
+
5. **Edge weight tuning** -- v1 is flat 1.0 for all edges. The weight
|
|
353
|
+
encodes context-dependence but we have no signal to set it yet.
|
|
354
|
+
Could later set it from the matcher's cosine at creation time
|
|
355
|
+
(stronger match = higher weight) or from co-occurrence statistics.
|
|
356
|
+
|
|
357
|
+
## Interactions
|
|
358
|
+
|
|
359
|
+
- **Memory** (existing) -- distinct system. Memory is facts the
|
|
360
|
+
agent/user committed; predictions are learned preferences with graded
|
|
361
|
+
confidence. No data flows between them. The agent may write a memory
|
|
362
|
+
about a preference AND update a prediction about the same preference;
|
|
363
|
+
the memory is the "what" (durable fact), the prediction is the "how
|
|
364
|
+
confident" (graded, updated by feedback).
|
|
365
|
+
- **Extraction pipeline** (existing) -- prediction tools are excluded
|
|
366
|
+
from the extraction buffer by the thatch_* prefix filter. No
|
|
367
|
+
feedback loop.
|
|
368
|
+
- **Recall nudge** (existing) -- the prediction fire shares the
|
|
369
|
+
embedding call and runs in the same chat.message hook path (opencode)
|
|
370
|
+
or the same sideband round-trip (MCP). The two nudges are separate
|
|
371
|
+
synthetic parts (opencode) or combined in one output block (MCP);
|
|
372
|
+
either can be absent.
|
|
373
|
+
- **Sideband** (existing, MCP path) -- the sideband server now handles
|
|
374
|
+
a `predictions` method alongside `match`. The `flush-tools` CLI
|
|
375
|
+
subcommand queries both in parallel via `Promise.all`.
|
|
376
|
+
- **Compaction guard** (existing) -- the compacting set check at the
|
|
377
|
+
top of chat.message suppresses ALL nudges, including prediction fire.
|
|
378
|
+
No change needed.
|
|
379
|
+
|
|
380
|
+
## Gotchas
|
|
381
|
+
|
|
382
|
+
- **Matcher embeddings use passageEmbed, not queryEmbed.** Matchers
|
|
383
|
+
are passages (stored content matched against), not queries. The query
|
|
384
|
+
prefix applies to the user's text at fire time. Same pattern as
|
|
385
|
+
remember() which uses passageEmbed for content.
|
|
386
|
+
- **confirm_count / disconfirm_count MUST be REAL.** An integer column
|
|
387
|
+
truncates soft signals (W_SOFT = 0.25) to 0 and freezes confidence at
|
|
388
|
+
p0. This is the same bug samskara had.
|
|
389
|
+
- **Cold start is free.** Empty matchers table, findMatchers returns
|
|
390
|
+
[], nothing injected. The feature is inert until the first
|
|
391
|
+
prediction_update call. No migration, no config.
|
|
392
|
+
- **Prediction calls do not drain the extraction buffer.** They are a
|
|
393
|
+
separate system. The agent may do both in the same turn.
|
|
394
|
+
|
|
395
|
+
## Reference
|
|
396
|
+
|
|
397
|
+
- nak samskara: `docs/dev/samskara.md` in the nak repo (the predictive
|
|
398
|
+
model of the user this feature is inspired by).
|
|
399
|
+
- nak intents: `docs/dev/in-progress/intents.md` in the nak repo (the
|
|
400
|
+
normative layer atop samskara; the C efficacy model and firewall
|
|
401
|
+
between employment and efficacy informed the confidence model here).
|
|
402
|
+
- thatch memory: this doc's "Interactions" section.
|
|
403
|
+
- thatch extraction pipeline: `docs/dev/README.md` + `docs/dev/mcp-parity.md`.
|
|
404
|
+
- thatch hook surface: `docs/dev/setup-and-hooks.md`, `docs/dev/gotchas.md`.
|
package/docs/qa/README.md
CHANGED
|
@@ -91,7 +91,7 @@ correctness on `README.md` and `docs/` excluding `docs/plans/`; see
|
|
|
91
91
|
|
|
92
92
|
### `tests/plugin.test.ts`
|
|
93
93
|
|
|
94
|
-
- `server` export shape: all
|
|
94
|
+
- `server` export shape: all thirteen tools, every hook present, tools carry
|
|
95
95
|
description/args/execute
|
|
96
96
|
- System transform and compaction hooks append their text
|
|
97
97
|
- `tool.execute.after` → `chat.message` extraction round-trip: nudge carries
|
|
@@ -101,6 +101,38 @@ correctness on `README.md` and `docs/` excluding `docs/plans/`; see
|
|
|
101
101
|
- `event` handler: calls `client.session.prompt` on `session.created` with
|
|
102
102
|
correct session ID and nudge text; ignores non-`session.created` events
|
|
103
103
|
- `sessionStartReminder`: includes store name and recall instructions
|
|
104
|
+
- Recall nudge via `chat.message`: surfaces when prompt matches a stored
|
|
105
|
+
memory, no nudge for unrelated or short prompts, extraction priority
|
|
106
|
+
- Prediction auto-fire via `chat.message`: surfaces when prompt matches a
|
|
107
|
+
stored matcher, no nudge for unrelated prompts, prediction and recall
|
|
108
|
+
nudges fire independently as separate synthetic parts
|
|
109
|
+
|
|
110
|
+
### `tests/tool-defs.test.ts`
|
|
111
|
+
|
|
112
|
+
- Tool count (13) and name list
|
|
113
|
+
- Zod schema validation for each tool's args
|
|
114
|
+
- Execute functions: `memory_remember` save/duplicate/overwrite/archived,
|
|
115
|
+
`memory_recall` default/empty/archived, `memory_list`/`show`/`forget`,
|
|
116
|
+
`store_list`, `find_duplicates` clean store
|
|
117
|
+
- Prediction execute functions: `prediction_update` create/confirm/disconfirm/
|
|
118
|
+
create-on-existing (dedup links without disconfirming), `prediction_query`
|
|
119
|
+
matching/unrelated, `prediction_list` with provenance, `prediction_delete`
|
|
120
|
+
found/not-found
|
|
121
|
+
|
|
122
|
+
### `tests/prediction.test.ts`
|
|
123
|
+
|
|
124
|
+
- Schema: tables exist, `populationP0` fallback on empty store
|
|
125
|
+
- Matcher creation and lookup: `findMatchers` ranking, `findNearestMatcher`
|
|
126
|
+
threshold/dedup
|
|
127
|
+
- Prediction creation: `createPrediction` seeds at p0, `adjustConfidence`
|
|
128
|
+
confirm/disconfirm/soft/multiple, non-existent ID no-op
|
|
129
|
+
- Edges and scoring: `createEdge` links, `scorePredictions` empty/dedup/
|
|
130
|
+
weight-preservation, `scorePredictionNudge` threshold filtering + dedup
|
|
131
|
+
- `findNearestPrediction`: store-wide search, threshold, cross-matcher
|
|
132
|
+
- Provenance: `addProvenance` + `getProvenance` read-back, `deletePrediction`
|
|
133
|
+
cascade to edges and provenance
|
|
134
|
+
- `listPredictions`: sorted by confidence, matchers included
|
|
135
|
+
- `populationP0`: under-20 fallback, population hit rate with sufficient evidence
|
|
104
136
|
|
|
105
137
|
### `tests/hygiene.test.ts`
|
|
106
138
|
|
|
@@ -29,6 +29,10 @@ file/IPC/CLI contracts ready to graduate into `bun:test` integration tests.
|
|
|
29
29
|
| [UC-018](UC-018-mcp-startup-setup-detection.md) | MCP startup detects missing/broken setup instructions and warns the agent |
|
|
30
30
|
| [UC-019](UC-019-archived-memory-lifecycle.md) | Archive, search exclusion, update guard, unarchive |
|
|
31
31
|
| [UC-020](UC-020-extraction-escalation.md) | Nudge escalation (polite → insistent → ALL-CAPS), buffer persistence, `extraction_done` drain |
|
|
32
|
+
| [UC-021](UC-021-prediction-autofire.md) | Prediction auto-fire: matcher scores prompt, prediction nudge injected alongside recall |
|
|
33
|
+
| [UC-022](UC-022-prediction-dedup.md) | Prediction create-on-existing: dedup finds match, links edge without disconfirming |
|
|
34
|
+
| [UC-023](UC-023-prediction-confidence-model.md) | Bayesian confidence math: create, confirm, soft (weak disconfirm), disconfirm signals |
|
|
35
|
+
| [UC-024](UC-024-prediction-delete.md) | Prediction delete with cascade, provenance inspection, not-found handling |
|
|
32
36
|
|
|
33
37
|
## Template
|
|
34
38
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# UC-021: Prediction auto-fire
|
|
2
|
+
|
|
3
|
+
**Preconditions**
|
|
4
|
+
- An opencode session (or Claude Code / Cursor with the MCP server running and sideband socket live)
|
|
5
|
+
- A clean DB (no existing matchers or predictions)
|
|
6
|
+
|
|
7
|
+
**Steps**
|
|
8
|
+
|
|
9
|
+
1. Send a prompt expressing a preference, e.g. "I prefer to handle error cases before happy paths. Record this preference."
|
|
10
|
+
2. Observe the agent calls `prediction_update` with `signal: "create"`.
|
|
11
|
+
3. Start a new session (or send a fresh prompt in the same session).
|
|
12
|
+
4. Send a prompt matching the matcher context, e.g. "Write a function to parse a JSON config file."
|
|
13
|
+
5. Send an unrelated prompt, e.g. "What's the weather like?"
|
|
14
|
+
|
|
15
|
+
**Expected**
|
|
16
|
+
- Step 2: Agent calls `thatch_prediction_update` with a matcher describing the context ("writing functions", "error handling") and a prediction describing the preference ("handle errors before happy paths"). The tool returns `[created]` and seeds confidence at 0.50 with 0 evidence.
|
|
17
|
+
- Step 4: The `chat.message` hook embeds the prompt, finds matchers above the 0.45 threshold, scores linked predictions, and injects a `[thatch] User decision model` nudge. The 0-evidence prediction uses "you may prefer" (not "you tend to"). The agent may follow the prediction, surface it, or ignore it.
|
|
18
|
+
- Step 5: No prediction nudge (cosine below threshold). No extra synthetic parts.
|
|
19
|
+
- Claude Code / Cursor: The `flush-tools` hook fires predictions alongside the recall nudge via the sideband socket. The `flush-predictions` CLI subcommand provides standalone prediction-only output.
|
|
20
|
+
|
|
21
|
+
_Automatable: yes — the auto-fire path is tested in `plugin.test.ts` (prediction nudge via chat.message, independent firing, no-match suppression). The end-to-end "agent acts on prediction" flow is manual._
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# UC-022: Prediction create-on-existing (dedup + link)
|
|
2
|
+
|
|
3
|
+
**Preconditions**
|
|
4
|
+
- An opencode session with at least one existing prediction (from UC-021 or seeded directly)
|
|
5
|
+
- The existing prediction's matcher context is semantically similar to a new observation
|
|
6
|
+
|
|
7
|
+
**Steps**
|
|
8
|
+
|
|
9
|
+
1. Seed a prediction: `prediction_update(matcher="reviewing a PR", prediction="go through findings one at a time using a todo list", signal="create")`.
|
|
10
|
+
2. In the same or a new session, call `prediction_update(matcher="code review session", prediction="go through findings one at a time using a todo list", signal="create")`.
|
|
11
|
+
3. Call `prediction_update(matcher="code review session", prediction="go through findings one at a time using a todo list", signal="confirm")`.
|
|
12
|
+
4. Call `prediction_list` to inspect the model.
|
|
13
|
+
|
|
14
|
+
**Expected**
|
|
15
|
+
- Step 2: `findNearestPrediction` finds the existing prediction (cosine > 0.85). The tool returns `[linked]` (not `[created]`). The new matcher is linked via an edge. No duplicate prediction is created. Confidence is unchanged (create is confidence-neutral).
|
|
16
|
+
- Step 3: Same prediction is found. The matcher edge already exists (ON CONFLICT DO NOTHING). Confidence is adjusted upward by the confirm signal. Returns `[confirm]` with updated confidence.
|
|
17
|
+
- Step 4: `prediction_list` shows one prediction with two matchers ("reviewing a PR" and "code review session"), confidence reflecting one confirm, and provenance entries for both the create and confirm signals.
|
|
18
|
+
|
|
19
|
+
_Automatable: yes — the create-on-existing path is tested in `tool-defs.test.ts` (prediction_update create on existing prediction links without disconfirming). The dedup + edge + confidence behavior is verified with assertions on the return value and prediction_list output._
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# UC-023: Prediction confidence and signal model
|
|
2
|
+
|
|
3
|
+
**Preconditions**
|
|
4
|
+
- An opencode or Claude Code / Cursor session with the prediction model seeded
|
|
5
|
+
|
|
6
|
+
**Steps**
|
|
7
|
+
|
|
8
|
+
1. `prediction_update(matcher="X", prediction="Y", signal="create")` — seeds at p0 (0.50, 0 evidence).
|
|
9
|
+
2. `prediction_update(matcher="X", prediction="Y", signal="confirm")` — user confirmed.
|
|
10
|
+
3. `prediction_update(matcher="X", prediction="Y", signal="soft")` — user partially disagreed.
|
|
11
|
+
4. `prediction_update(matcher="X", prediction="Y", signal="disconfirm")` — user pushed back.
|
|
12
|
+
5. `prediction_list` to inspect confidence and evidence counts.
|
|
13
|
+
6. `prediction_query(context="X")` to query the model.
|
|
14
|
+
|
|
15
|
+
**Expected**
|
|
16
|
+
- Step 1: Returns `[created]`, confidence=0.50, counts (0/0).
|
|
17
|
+
- Step 2: Returns `[confirm]`, confidence > 0.50. Formula: (1 + 5*0.5) / (1 + 0 + 5) = 0.583. Counts (1/0).
|
|
18
|
+
- Step 3: Returns `[soft]`, confidence drops slightly. Soft adds 0.25 to disconfirm_count. Formula: (1 + 2.5) / (1 + 0.25 + 5) = 0.571. Counts (1/0.25).
|
|
19
|
+
- Step 4: Returns `[disconfirm]`, confidence drops further. Formula: (1 + 2.5) / (1 + 1.25 + 5) = 0.524. Counts (1/1.25).
|
|
20
|
+
- Step 5: `prediction_list` shows the prediction with accumulated provenance entries (create, confirm, soft, disconfirm) sorted newest-first.
|
|
21
|
+
- Step 6: `prediction_query` returns the prediction with the current confidence and evidence count. Uses "you may prefer" for 0-evidence and "you tend to" for predictions with evidence. Threshold (0.45) filters out matchers below the relevance floor.
|
|
22
|
+
|
|
23
|
+
_Automatable: yes — the confidence math and signal mapping are tested in `prediction.test.ts` (adjustConfidence confirm/disconfirm/soft/multiple) and `tool-defs.test.ts` (prediction_update confirm applies signal immediately)._
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# UC-024: Prediction delete and provenance inspection
|
|
2
|
+
|
|
3
|
+
**Preconditions**
|
|
4
|
+
- An opencode or Claude Code / Cursor session with predictions in the model
|
|
5
|
+
|
|
6
|
+
**Steps**
|
|
7
|
+
|
|
8
|
+
1. Seed two predictions in the same store.
|
|
9
|
+
2. `prediction_list` to see both predictions with matchers, confidence, and provenance.
|
|
10
|
+
3. `prediction_delete(statement="first prediction text")` to remove one.
|
|
11
|
+
4. `prediction_list` to verify only one prediction remains.
|
|
12
|
+
5. `prediction_delete(statement="nonexistent prediction")` to test not-found handling.
|
|
13
|
+
|
|
14
|
+
**Expected**
|
|
15
|
+
- Step 2: Both predictions listed with confidence, evidence count, matchers (with weights), and provenance entries (signal type, detail, date).
|
|
16
|
+
- Step 3: Returns `[deleted]` with the matched prediction's statement. Matching is semantic (cosine >= 0.85), not exact string match. Edges and provenance are cascade-deleted via FK ON DELETE CASCADE.
|
|
17
|
+
- Step 4: Only the remaining prediction is listed. No orphaned edges or provenance entries.
|
|
18
|
+
- Step 5: Returns "No prediction matching ... found" for a statement with no semantic match above threshold.
|
|
19
|
+
|
|
20
|
+
_Automatable: yes — prediction_delete is tested in `tool-defs.test.ts` (found and not-found cases), and deletePrediction cascade is tested in `prediction.test.ts` (cascade to edges and provenance, non-existent ID)._
|
package/docs/user/README.md
CHANGED
|
@@ -174,7 +174,7 @@ Six specialist review lenses, each a self-contained static-analysis pass:
|
|
|
174
174
|
| `thatch-review-no-slop` | AI writing anti-patterns: change narration, fourth wall breaks, em dashes, hedging, filler. |
|
|
175
175
|
| `thatch-review-breadcrumbs` | Comment narrative: do comments form a coherent outline of the code's behavior? |
|
|
176
176
|
| `thatch-review-mark-and-sweep` | Mechanical change completeness: whole-repo sweep for stragglers after renames, flag removals, API substitutions. |
|
|
177
|
-
| `thatch-review-synthesizer` | Verifies and synthesizes findings from multiple specialists into a
|
|
177
|
+
| `thatch-review-synthesizer` | Verifies and synthesizes findings from multiple specialists into a report that starts with workflow changes, then deduplicated severity-grouped findings. |
|
|
178
178
|
| `thatch-review-context` | Gathers project context (PR descriptions, git archaeology, ticket references, memory) before a review. Prevents false positives about intentionally deferred work. |
|
|
179
179
|
| `thatch-workflow-research` | Researches code workflows and features affected by a change or planned change. Reads code flows, comments, git history, and produces a guide for reviewers or planners. |
|
|
180
180
|
| `thatch-change-walkthrough` | Explains a change to the user as a teaching walkthrough: researches each affected workflow at the merge-base, teaches current behavior, then overlays the modifications with file:line citations. |
|
|
@@ -184,15 +184,15 @@ Six specialist review lenses, each a self-contained static-analysis pass:
|
|
|
184
184
|
|
|
185
185
|
| Skill | Purpose |
|
|
186
186
|
|-------|---------|
|
|
187
|
-
| `pr-description` | Drafts PR descriptions
|
|
188
|
-
| `ticket-description` | Drafts ticket or issue descriptions (Linear or Jira)
|
|
187
|
+
| `pr-description` | Drafts PR descriptions with SYNOPSIS / PURPOSE / DESCRIPTION / WALK-THROUGH / NOTES, project-context research, clarity checks, and bold/italic emphasis for scanning. |
|
|
188
|
+
| `ticket-description` | Drafts ticket or issue descriptions (Linear or Jira) with clear sections, project-context research, clarity checks, and bold/italic emphasis for scanning. |
|
|
189
189
|
| `split-overlarge-pr` | Splits already-completed work from an overlarge PR into human-reviewable, release-safe PRs targeting main. |
|
|
190
190
|
|
|
191
191
|
### opencode-only skills
|
|
192
192
|
|
|
193
193
|
| Skill | Purpose |
|
|
194
194
|
|-------|---------|
|
|
195
|
-
| `thatch-code-review` | Multi-agent review coordinator. Dispatches parallel sub-agents for triage, decomposition, specialist fan-out, and synthesis. Not available in Claude Code (requires sub-agent support). |
|
|
195
|
+
| `thatch-code-review` | Multi-agent review coordinator. Dispatches parallel sub-agents for triage, decomposition, specialist fan-out, and synthesis with a workflow-change preface. Not available in Claude Code (requires sub-agent support). |
|
|
196
196
|
|
|
197
197
|
### Host availability
|
|
198
198
|
|
|
@@ -222,7 +222,8 @@ Load thatch-code-review and review branch feature-x.
|
|
|
222
222
|
```
|
|
223
223
|
|
|
224
224
|
The coordinator will triage the change, dispatch parallel sub-agents (one per
|
|
225
|
-
specialist lens), and synthesize a final report.
|
|
225
|
+
specialist lens), and synthesize a final report. The report starts with the
|
|
226
|
+
workflow-level changes so the findings have context.
|
|
226
227
|
|
|
227
228
|
For a **full review on Claude Code** (or without the coordinator), run each
|
|
228
229
|
specialist in sequence, then synthesize:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeffober/thatch",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.20",
|
|
4
4
|
"description": "Persistent memory for AI coding agents — local embeddings, SQLite stores, zero config. Works with OpenCode (as a plugin), Claude Code (as an MCP server), and Cursor (as an MCP server).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|