clawmem 0.8.0 → 0.8.1
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/AGENTS.md +1 -1
- package/CLAUDE.md +1 -1
- package/README.md +14 -0
- package/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/hooks/context-surfacing.ts +160 -16
- package/src/hooks.ts +9 -1
- package/src/store.ts +61 -6
package/AGENTS.md
CHANGED
|
@@ -259,7 +259,7 @@ ClawMem hooks handle ~90% of retrieval automatically. Agent-initiated MCP calls
|
|
|
259
259
|
|
|
260
260
|
| Hook | Trigger | Budget | Content |
|
|
261
261
|
|------|---------|--------|---------|
|
|
262
|
-
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate → profile-driven hybrid search (vector if `useVector`, timeout from profile) → FTS supplement → file-aware supplemental search (E13) → snooze filter → noise filter → spreading activation (E11: co-activated doc boost) → memory type diversification (E10) → tiered injection (HOT/WARM/COLD snippets) → `<vault-context><instruction>…</instruction><facts>…</facts><relationships>…</relationships></vault-context>` (v0.7.1: instruction always prepended when context is returned; relationships block lists memory-graph edges where BOTH endpoints are in the surfaced set, truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, and min score all driven by `CLAWMEM_PROFILE`. |
|
|
262
|
+
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate → **multi-turn query construction** (v0.8.1: current prompt + up to 2 recent same-session priors from `context_usage.query_text`, 10-min max age, capped at 2000 chars with current-first preservation — used only for discovery: vector/FTS/expansion, NOT for rerank/scoring/snippet extraction) → profile-driven hybrid search (vector if `useVector`, timeout from profile) → FTS supplement → file-aware supplemental search (E13, raw current prompt) → snooze filter → noise filter → spreading activation (E11: co-activated doc boost) → memory type diversification (E10) → tiered injection (HOT/WARM/COLD snippets) → `<vault-context><instruction>…</instruction><facts>…</facts><relationships>…</relationships></vault-context>` (v0.7.1: instruction always prepended when context is returned; relationships block lists memory-graph edges where BOTH endpoints are in the surfaced set, truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, and min score all driven by `CLAWMEM_PROFILE`. Raw prompt persisted to `context_usage.query_text` for future multi-turn lookback — except on gated skip paths (slash commands, heartbeats, too-short prompts) where the text is withheld for privacy. |
|
|
263
263
|
| `postcompact-inject` | SessionStart (compact) | 1200 tokens | re-injects authoritative context after compaction: precompact state (600) + recent decisions (400) + antipatterns (150) + vault context (200) → `<vault-postcompact>` |
|
|
264
264
|
| `curator-nudge` | SessionStart | 200 tokens | surfaces curator report actions, nudges when report is stale (>7 days) |
|
|
265
265
|
| `precompact-extract` | PreCompact | — | extracts decisions, file paths, open questions → writes `precompact-state.md` to auto-memory. Query-aware decision ranking. Reindexes auto-memory collection. |
|
package/CLAUDE.md
CHANGED
|
@@ -259,7 +259,7 @@ ClawMem hooks handle ~90% of retrieval automatically. Agent-initiated MCP calls
|
|
|
259
259
|
|
|
260
260
|
| Hook | Trigger | Budget | Content |
|
|
261
261
|
|------|---------|--------|---------|
|
|
262
|
-
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate → profile-driven hybrid search (vector if `useVector`, timeout from profile) → FTS supplement → file-aware supplemental search (E13) → snooze filter → noise filter → spreading activation (E11: co-activated doc boost) → memory type diversification (E10) → tiered injection (HOT/WARM/COLD snippets) → `<vault-context><instruction>…</instruction><facts>…</facts><relationships>…</relationships></vault-context>` (v0.7.1: instruction always prepended when context is returned; relationships block lists memory-graph edges where BOTH endpoints are in the surfaced set, truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, and min score all driven by `CLAWMEM_PROFILE`. |
|
|
262
|
+
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate → **multi-turn query construction** (v0.8.1: current prompt + up to 2 recent same-session priors from `context_usage.query_text`, 10-min max age, capped at 2000 chars with current-first preservation — used only for discovery: vector/FTS/expansion, NOT for rerank/scoring/snippet extraction) → profile-driven hybrid search (vector if `useVector`, timeout from profile) → FTS supplement → file-aware supplemental search (E13, raw current prompt) → snooze filter → noise filter → spreading activation (E11: co-activated doc boost) → memory type diversification (E10) → tiered injection (HOT/WARM/COLD snippets) → `<vault-context><instruction>…</instruction><facts>…</facts><relationships>…</relationships></vault-context>` (v0.7.1: instruction always prepended when context is returned; relationships block lists memory-graph edges where BOTH endpoints are in the surfaced set, truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, and min score all driven by `CLAWMEM_PROFILE`. Raw prompt persisted to `context_usage.query_text` for future multi-turn lookback — except on gated skip paths (slash commands, heartbeats, too-short prompts) where the text is withheld for privacy. |
|
|
263
263
|
| `postcompact-inject` | SessionStart (compact) | 1200 tokens | re-injects authoritative context after compaction: precompact state (600) + recent decisions (400) + antipatterns (150) + vault context (200) → `<vault-postcompact>` |
|
|
264
264
|
| `curator-nudge` | SessionStart | 200 tokens | surfaces curator report actions, nudges when report is stale (>7 days) |
|
|
265
265
|
| `precompact-extract` | PreCompact | — | extracts decisions, file paths, open questions → writes `precompact-state.md` to auto-memory. Query-aware decision ranking. Reindexes auto-memory collection. |
|
package/README.md
CHANGED
|
@@ -95,6 +95,19 @@ A second, longer-interval consolidation worker that keeps Phase 2 + Phase 3 runn
|
|
|
95
95
|
|
|
96
96
|
Adds +56 tests (13 worker-lease + 35 maintenance unit + 8 maintenance integration) on top of the v0.7.2 baseline.
|
|
97
97
|
|
|
98
|
+
### v0.8.1 Multi-Turn Prior-Query Lookback
|
|
99
|
+
|
|
100
|
+
`context-surfacing` now builds its retrieval query from the current prompt plus up to two recent same-session prior prompts, so a short follow-up turn ("do the same for X", "explain the rationale") can still inherit the vocabulary of earlier turns. The raw prompt is persisted in a new nullable `context_usage.query_text` column so future hook ticks can reconstitute the multi-turn query from the DB. See [multi-turn lookback](docs/concepts/architecture.md#multi-turn-prior-query-lookback-v081) for the full walkthrough.
|
|
101
|
+
|
|
102
|
+
- **Additive schema migration** — new nullable `query_text TEXT` column on `context_usage`, guarded by `PRAGMA table_info`. Pre-v0.8.1 stores get the column added on first open; ad-hoc stores that skip the migration path degrade transparently via a feature-detect WeakMap so `insertUsageFn` never writes a column that doesn't exist.
|
|
103
|
+
- **Discovery path only** — the multi-turn query feeds vector search, BM25, and query expansion. Cross-encoder reranking continues to use the RAW current prompt so relevance scoring is not diluted by older turns, and composite scoring / snippet extraction / dedupe / routing-hint detection all remain on the raw prompt as well.
|
|
104
|
+
- **Privacy-conscious persistence split** — gated skip paths (slash commands, `MIN_PROMPT_LENGTH`, `shouldSkipRetrieval`, heartbeat dedupe) do NOT persist their raw text because those turns are not meaningful user questions and carry a higher sensitivity profile. Post-retrieval empty paths (empty result set, threshold blocked, budget blocked) DO persist so a follow-up turn can still inherit the intent even when the current turn surfaced nothing.
|
|
105
|
+
- **Current-first truncation** — the combined query is clamped to 2000 chars with the current prompt preserved verbatim at the head. Older priors are dropped first when the budget runs out. If the current prompt alone already exceeds the cap, priors are omitted entirely and the current prompt is truncated.
|
|
106
|
+
- **SQL-level self-match guard** — duplicate submits of the same prompt are filtered out of the lookback SELECT via `AND query_text != ?` so a retry burst cannot eat into the 2-prior budget and leave the lookback window underfilled.
|
|
107
|
+
- **10-minute max age, session-scoped** — priors older than 10 minutes or from a different `session_id` are invisible to the lookback. All fallback paths (missing column, DB error, no matching rows) return the current prompt unchanged — the hook never throws on lookback failures.
|
|
108
|
+
|
|
109
|
+
Adds +27 tests (22 unit + 5 integration) on top of the v0.8.0 baseline.
|
|
110
|
+
|
|
98
111
|
## Architecture
|
|
99
112
|
|
|
100
113
|
<p align="center">
|
|
@@ -1187,6 +1200,7 @@ Built on the shoulders of:
|
|
|
1187
1200
|
- [QMD](https://github.com/tobi/qmd) — search backend (BM25 + vectors + RRF + reranking)
|
|
1188
1201
|
- [SAME](https://github.com/sgx-labs/statelessagent) — agent memory concepts (recency decay, confidence scoring, session tracking)
|
|
1189
1202
|
- [supermemory](https://github.com/supermemoryai/clawdbot-supermemory) — hook patterns and context surfacing ideas
|
|
1203
|
+
- [Thoth](https://github.com/siddsachar/Thoth) — anti-contamination deductive synthesis, contradiction-aware + name-aware merge gates, post-import conversation fact extraction, quiet-window heavy maintenance lane with worker leases, context instruction framing, relationship snippets, multi-turn prior-query lookback
|
|
1190
1204
|
|
|
1191
1205
|
## Roadmap
|
|
1192
1206
|
|
package/SKILL.md
CHANGED
|
@@ -190,7 +190,7 @@ Hooks handle ~90% of retrieval. Zero agent effort.
|
|
|
190
190
|
|
|
191
191
|
| Hook | Trigger | Budget | Content |
|
|
192
192
|
|------|---------|--------|---------|
|
|
193
|
-
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate -> profile-driven hybrid search (vector if `useVector`, timeout from profile) -> FTS supplement -> file-aware search (E13) -> snooze filter -> noise filter -> spreading activation (E11) -> memory type diversification (E10) -> tiered injection (HOT/WARM/COLD) -> `<vault-context><instruction>...</instruction><facts>...</facts><relationships>...</relationships></vault-context>` (v0.7.1: instruction always prepended; relationships list memory-graph edges where BOTH endpoints are in the surfaced set; relationships truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, min score all driven by `CLAWMEM_PROFILE`. |
|
|
193
|
+
| `context-surfacing` | UserPromptSubmit | profile-driven (default 800) | retrieval gate -> **multi-turn query** (v0.8.1: current + up to 2 recent same-session priors from `context_usage.query_text`, 10-min max age, 2000-char cap with current-first, used only for discovery — not rerank/scoring/snippet) -> profile-driven hybrid search (vector if `useVector`, timeout from profile) -> FTS supplement -> file-aware search (E13, raw current) -> snooze filter -> noise filter -> spreading activation (E11) -> memory type diversification (E10) -> tiered injection (HOT/WARM/COLD) -> `<vault-context><instruction>...</instruction><facts>...</facts><relationships>...</relationships></vault-context>` (v0.7.1: instruction always prepended; relationships list memory-graph edges where BOTH endpoints are in the surfaced set; relationships truncated first when over budget) + optional `<vault-routing>` hint. Budget, max results, vector timeout, min score all driven by `CLAWMEM_PROFILE`. Raw prompt persisted to `context_usage.query_text` for future lookback — gated skip paths (slash commands, heartbeats, too-short prompts) withhold the text for privacy. |
|
|
194
194
|
| `postcompact-inject` | SessionStart (compact) | 1200 tokens | re-injects authoritative context after compaction: precompact state (600) + decisions (400) + antipatterns (150) + vault context (200) -> `<vault-postcompact>` |
|
|
195
195
|
| `curator-nudge` | SessionStart | 200 tokens | surfaces curator report actions, nudges when report is stale (>7 days) |
|
|
196
196
|
| `precompact-extract` | PreCompact | — | extracts decisions, file paths, open questions -> writes `precompact-state.md`. Query-aware ranking. Reindexes auto-memory. |
|
package/package.json
CHANGED
|
@@ -69,6 +69,19 @@ const INSTRUCTION_TOKEN_COST = estimateTokens(INSTRUCTION_XML);
|
|
|
69
69
|
const RELATIONSHIPS_XML_OVERHEAD_TOKENS = estimateTokens("<relationships>\n\n</relationships>");
|
|
70
70
|
const MAX_RELATION_SNIPPETS = 10;
|
|
71
71
|
|
|
72
|
+
// Ext 6b: Multi-turn prior-query lookback
|
|
73
|
+
// The retrieval query is built from the current prompt plus up to
|
|
74
|
+
// MULTI_TURN_LOOKBACK recent same-session prior prompts within
|
|
75
|
+
// MULTI_TURN_MAX_AGE_MINUTES. The combined query is clamped to
|
|
76
|
+
// MULTI_TURN_MAX_CHARS with newest content preserved first — so the
|
|
77
|
+
// current prompt is always the first N chars even when older priors
|
|
78
|
+
// would otherwise push it out. All other hook signals (scoring,
|
|
79
|
+
// composite recency intent, recall attribution, routing hints)
|
|
80
|
+
// continue to use the raw current prompt.
|
|
81
|
+
const MULTI_TURN_LOOKBACK = 2;
|
|
82
|
+
const MULTI_TURN_MAX_AGE_MINUTES = 10;
|
|
83
|
+
const MULTI_TURN_MAX_CHARS = 2000;
|
|
84
|
+
|
|
72
85
|
// File path patterns to extract from prompts (E13 replacement: file-aware UserPromptSubmit)
|
|
73
86
|
const FILE_PATH_RE = /(?:^|\s)((?:\/[\w.@-]+)+(?:\.\w+)?|[\w.@-]+\.(?:ts|js|py|md|sh|yaml|yml|json|toml|rs|go|tsx|jsx|css|html))\b/g;
|
|
74
87
|
|
|
@@ -133,12 +146,25 @@ export async function contextSurfacing(
|
|
|
133
146
|
const isRecency = hasRecencyIntent(prompt);
|
|
134
147
|
const minScore = isRecency ? MIN_COMPOSITE_SCORE_RECENCY : profile.minScore;
|
|
135
148
|
|
|
149
|
+
// Ext 6b: Build the retrieval query from the current prompt plus up to
|
|
150
|
+
// MULTI_TURN_LOOKBACK recent same-session prior prompts. Used only for
|
|
151
|
+
// the discovery path (vector, FTS, query expansion, reranking) so that
|
|
152
|
+
// a short "do that" / "same for X" turn can inherit the vocabulary of
|
|
153
|
+
// earlier turns. All other prompt-dependent signals (recency intent,
|
|
154
|
+
// composite scoring, recall attribution, snippet highlighting, routing
|
|
155
|
+
// hints, dedupe, heartbeat check) continue to use the raw current
|
|
156
|
+
// prompt. If the session has no priors in the window, the helper
|
|
157
|
+
// returns the current prompt unchanged.
|
|
158
|
+
const retrievalQuery = input.sessionId
|
|
159
|
+
? buildMultiTurnSurfacingQuery(store, input.sessionId, prompt)
|
|
160
|
+
: prompt;
|
|
161
|
+
|
|
136
162
|
// Search: try vector first (if profile allows), fall back to BM25
|
|
137
163
|
// When vector succeeds, also supplement with FTS for keyword-exact recall
|
|
138
164
|
let results: SearchResult[] = [];
|
|
139
165
|
if (profile.useVector) {
|
|
140
166
|
try {
|
|
141
|
-
const vectorPromise = store.searchVec(
|
|
167
|
+
const vectorPromise = store.searchVec(retrievalQuery, DEFAULT_EMBED_MODEL, maxResults);
|
|
142
168
|
const timeoutPromise = new Promise<SearchResult[]>((_, reject) =>
|
|
143
169
|
setTimeout(() => reject(new Error("vector timeout")), profile.vectorTimeout)
|
|
144
170
|
);
|
|
@@ -149,11 +175,11 @@ export async function contextSurfacing(
|
|
|
149
175
|
}
|
|
150
176
|
|
|
151
177
|
if (results.length === 0) {
|
|
152
|
-
results = store.searchFTS(
|
|
178
|
+
results = store.searchFTS(retrievalQuery, maxResults);
|
|
153
179
|
} else {
|
|
154
180
|
// Supplement vector results with FTS for keyword-exact matches (<10ms)
|
|
155
181
|
const seen = new Set(results.map(r => r.filepath));
|
|
156
|
-
const ftsSupplemental = store.searchFTS(
|
|
182
|
+
const ftsSupplemental = store.searchFTS(retrievalQuery, 5);
|
|
157
183
|
for (const r of ftsSupplemental) {
|
|
158
184
|
if (!seen.has(r.filepath)) {
|
|
159
185
|
seen.add(r.filepath);
|
|
@@ -166,7 +192,7 @@ export async function contextSurfacing(
|
|
|
166
192
|
if (getVaultPath("skill")) {
|
|
167
193
|
try {
|
|
168
194
|
const skillStore = resolveStore("skill");
|
|
169
|
-
const skillResults = skillStore.searchFTS(
|
|
195
|
+
const skillResults = skillStore.searchFTS(retrievalQuery, 5);
|
|
170
196
|
// Tag skill vault results for identification in output
|
|
171
197
|
for (const r of skillResults) {
|
|
172
198
|
(r as any)._fromVault = "skill";
|
|
@@ -178,7 +204,9 @@ export async function contextSurfacing(
|
|
|
178
204
|
}
|
|
179
205
|
|
|
180
206
|
// File-aware supplemental search (E13 replacement): extract file paths/names from prompt
|
|
181
|
-
// and run targeted FTS queries to surface file-specific vault context
|
|
207
|
+
// and run targeted FTS queries to surface file-specific vault context.
|
|
208
|
+
// File-path extraction stays on the raw current prompt so priors cannot
|
|
209
|
+
// pollute the file-specific discovery channel with stale filenames.
|
|
182
210
|
const fileMatches = [...prompt.matchAll(FILE_PATH_RE)].map(m => m[1]!.trim()).filter(Boolean);
|
|
183
211
|
if (fileMatches.length > 0) {
|
|
184
212
|
const seen = new Set(results.map(r => r.filepath));
|
|
@@ -195,17 +223,23 @@ export async function contextSurfacing(
|
|
|
195
223
|
}
|
|
196
224
|
}
|
|
197
225
|
|
|
198
|
-
if (results.length === 0) { logEmptyTurn(store, input); return makeEmptyOutput("context-surfacing"); }
|
|
226
|
+
if (results.length === 0) { logEmptyTurn(store, input, prompt); return makeEmptyOutput("context-surfacing"); }
|
|
199
227
|
|
|
200
228
|
// Budget-aware deep escalation (deep profile only):
|
|
201
229
|
// If the fast path finished quickly and found results, spend remaining time budget
|
|
202
230
|
// on query expansion (discovers new candidates) and cross-encoder reranking (reorders).
|
|
231
|
+
// Ext 6b: expansion + FTS variants use the multi-turn retrieval query so
|
|
232
|
+
// short current prompts still inherit prior-turn vocabulary. Reranking
|
|
233
|
+
// continues to use the RAW current prompt so relevance scoring is not
|
|
234
|
+
// diluted by older turns — the cross-encoder is asked "how well does
|
|
235
|
+
// this doc match the user's current question", not "how well does it
|
|
236
|
+
// match the last 10 minutes of questions".
|
|
203
237
|
if (profile.deepEscalation && results.length >= 2) {
|
|
204
238
|
const elapsed = Date.now() - startTime;
|
|
205
239
|
if (elapsed < profile.escalationBudgetMs) {
|
|
206
240
|
try {
|
|
207
241
|
// Phase 1: Query expansion — discover candidates BM25+vector missed
|
|
208
|
-
const expanded = await store.expandQuery(
|
|
242
|
+
const expanded = await store.expandQuery(retrievalQuery, DEFAULT_QUERY_MODEL);
|
|
209
243
|
if (expanded.length > 0) {
|
|
210
244
|
const seen = new Set(results.map(r => r.filepath));
|
|
211
245
|
for (const eq of expanded.slice(0, 3)) {
|
|
@@ -253,7 +287,7 @@ export async function contextSurfacing(
|
|
|
253
287
|
!FILTERED_PATHS.some(p => r.displayPath.includes(p))
|
|
254
288
|
);
|
|
255
289
|
|
|
256
|
-
if (results.length === 0) { logEmptyTurn(store, input); return makeEmptyOutput("context-surfacing"); }
|
|
290
|
+
if (results.length === 0) { logEmptyTurn(store, input, prompt); return makeEmptyOutput("context-surfacing"); }
|
|
257
291
|
|
|
258
292
|
// Filter out snoozed documents
|
|
259
293
|
const now = new Date();
|
|
@@ -269,7 +303,7 @@ export async function contextSurfacing(
|
|
|
269
303
|
return true;
|
|
270
304
|
});
|
|
271
305
|
|
|
272
|
-
if (results.length === 0) { logEmptyTurn(store, input); return makeEmptyOutput("context-surfacing"); }
|
|
306
|
+
if (results.length === 0) { logEmptyTurn(store, input, prompt); return makeEmptyOutput("context-surfacing"); }
|
|
273
307
|
|
|
274
308
|
// Deduplicate by filepath (keep best score per path)
|
|
275
309
|
const deduped = new Map<string, SearchResult>();
|
|
@@ -311,7 +345,7 @@ export async function contextSurfacing(
|
|
|
311
345
|
: 0;
|
|
312
346
|
|
|
313
347
|
// Activation floor: if even the best result is too weak, bail entirely
|
|
314
|
-
if (bestScore < profile.activationFloor) { logEmptyTurn(store, input); return makeEmptyOutput("context-surfacing"); }
|
|
348
|
+
if (bestScore < profile.activationFloor) { logEmptyTurn(store, input, prompt); return makeEmptyOutput("context-surfacing"); }
|
|
315
349
|
|
|
316
350
|
const adaptiveMin = Math.max(bestScore * profile.minScoreRatio, profile.absoluteFloor);
|
|
317
351
|
scored = allScored.filter(r => r.compositeScore >= adaptiveMin);
|
|
@@ -320,7 +354,7 @@ export async function contextSurfacing(
|
|
|
320
354
|
scored = allScored.filter(r => r.compositeScore >= minScore);
|
|
321
355
|
}
|
|
322
356
|
|
|
323
|
-
if (scored.length === 0) { logEmptyTurn(store, input); return makeEmptyOutput("context-surfacing"); }
|
|
357
|
+
if (scored.length === 0) { logEmptyTurn(store, input, prompt); return makeEmptyOutput("context-surfacing"); }
|
|
324
358
|
|
|
325
359
|
// Spreading activation (E11): boost results co-activated with top HOT results
|
|
326
360
|
if (scored.length > 3) {
|
|
@@ -369,7 +403,7 @@ export async function contextSurfacing(
|
|
|
369
403
|
const { context, paths, tokens } = buildContext(scored, prompt, factsBudget);
|
|
370
404
|
|
|
371
405
|
if (!context) {
|
|
372
|
-
logEmptyTurn(store, input);
|
|
406
|
+
logEmptyTurn(store, input, prompt);
|
|
373
407
|
return makeEmptyOutput("context-surfacing");
|
|
374
408
|
}
|
|
375
409
|
|
|
@@ -377,8 +411,10 @@ export async function contextSurfacing(
|
|
|
377
411
|
if (input.sessionId) {
|
|
378
412
|
const turnIndex = (input as any)._turnIndex ?? 0;
|
|
379
413
|
|
|
380
|
-
// Log the injection — returns usage_id for recall event linkage
|
|
381
|
-
|
|
414
|
+
// Log the injection — returns usage_id for recall event linkage.
|
|
415
|
+
// Ext 6b: persist the raw prompt as query_text so future turns in
|
|
416
|
+
// the same session can reconstitute a multi-turn retrieval query.
|
|
417
|
+
const usageId = logInjection(store, input.sessionId, "context-surfacing", paths, tokens, turnIndex, prompt);
|
|
382
418
|
|
|
383
419
|
// Record recall events ONLY for docs that made it into the injected context
|
|
384
420
|
// (post-budget). Docs trimmed by token budget were never seen by the model.
|
|
@@ -469,12 +505,21 @@ export async function contextSurfacing(
|
|
|
469
505
|
* Log an empty context_usage row for a skipped turn.
|
|
470
506
|
* Keeps turn_index aligned with transcript turns so per-turn recall
|
|
471
507
|
* attribution doesn't drift when some prompts are gated.
|
|
508
|
+
*
|
|
509
|
+
* Ext 6b: `queryText` is optional. Callers that gated BEFORE the
|
|
510
|
+
* retrieval stage (slash commands, heartbeat dedupe, too-short prompts,
|
|
511
|
+
* `shouldSkipRetrieval`) pass nothing — those turns are not meaningful
|
|
512
|
+
* user questions and their raw text is not worth persisting for future
|
|
513
|
+
* multi-turn lookback. Callers that gated AFTER retrieval (empty result
|
|
514
|
+
* set, threshold filter, budget) pass the prompt so a follow-up turn
|
|
515
|
+
* can still reuse the intent even though the current turn surfaced
|
|
516
|
+
* nothing.
|
|
472
517
|
*/
|
|
473
|
-
function logEmptyTurn(store: Store, input: HookInput): void {
|
|
518
|
+
function logEmptyTurn(store: Store, input: HookInput, queryText?: string): void {
|
|
474
519
|
if (!input.sessionId) return;
|
|
475
520
|
try {
|
|
476
521
|
const turnIndex = (input as any)._turnIndex ?? 0;
|
|
477
|
-
logInjection(store, input.sessionId, "context-surfacing", [], 0, turnIndex);
|
|
522
|
+
logInjection(store, input.sessionId, "context-surfacing", [], 0, turnIndex, queryText);
|
|
478
523
|
} catch { /* non-fatal */ }
|
|
479
524
|
}
|
|
480
525
|
|
|
@@ -700,6 +745,105 @@ export function buildVaultContextInner(
|
|
|
700
745
|
return lines.join("\n");
|
|
701
746
|
}
|
|
702
747
|
|
|
748
|
+
// =============================================================================
|
|
749
|
+
// Ext 6b: Multi-turn prior-query lookback
|
|
750
|
+
// =============================================================================
|
|
751
|
+
|
|
752
|
+
/**
|
|
753
|
+
* Build the retrieval query from the current prompt plus up to `lookback`
|
|
754
|
+
* recent prior prompts from the same session within `maxAgeMinutes`.
|
|
755
|
+
*
|
|
756
|
+
* Returns the current prompt unchanged when:
|
|
757
|
+
* - no `sessionId` (nothing to scope by)
|
|
758
|
+
* - the `query_text` column is missing (pre-migration store)
|
|
759
|
+
* - no prior rows within the window / all NULL
|
|
760
|
+
* - any DB error (fail-open — never throws)
|
|
761
|
+
*
|
|
762
|
+
* The combined query format is
|
|
763
|
+
* `<current>\n\n<newest prior>\n\n<older prior>...`
|
|
764
|
+
* truncated to `MULTI_TURN_MAX_CHARS` with **current content preserved
|
|
765
|
+
* first** — so even when older priors would push the current prompt
|
|
766
|
+
* past the char limit, the truncation drops the tail (older priors),
|
|
767
|
+
* not the head. This guarantees the retrieval query always contains the
|
|
768
|
+
* user's current question verbatim.
|
|
769
|
+
*
|
|
770
|
+
* Exported for direct unit testing.
|
|
771
|
+
*/
|
|
772
|
+
export function buildMultiTurnSurfacingQuery(
|
|
773
|
+
store: Store,
|
|
774
|
+
sessionId: string,
|
|
775
|
+
currentQuery: string,
|
|
776
|
+
lookback: number = MULTI_TURN_LOOKBACK,
|
|
777
|
+
maxAgeMinutes: number = MULTI_TURN_MAX_AGE_MINUTES,
|
|
778
|
+
maxChars: number = MULTI_TURN_MAX_CHARS,
|
|
779
|
+
): string {
|
|
780
|
+
if (!sessionId || currentQuery.length === 0) return currentQuery;
|
|
781
|
+
|
|
782
|
+
let priors: string[] = [];
|
|
783
|
+
try {
|
|
784
|
+
// ISO 8601 cutoff computed in JS (same lesson as the v0.8.0
|
|
785
|
+
// countRecentContextUsages fix — datetime('now', ...) returns a
|
|
786
|
+
// space-separated string that sorts incorrectly against the
|
|
787
|
+
// T-separated ISO 8601 timestamps stored in context_usage).
|
|
788
|
+
const cutoff = new Date(Date.now() - maxAgeMinutes * 60 * 1000).toISOString();
|
|
789
|
+
// Self-match guard lives in SQL so a duplicate submit/retry cannot eat
|
|
790
|
+
// into the lookback budget. Turn 18 review found that filtering in
|
|
791
|
+
// application code with `LIMIT lookback + 1` under-fills when multiple
|
|
792
|
+
// prior rows carry the same text as the current prompt — the SELECT
|
|
793
|
+
// returned only `lookback + 1` rows and application-level skipping
|
|
794
|
+
// then dropped legitimate distinct priors along with the dupes.
|
|
795
|
+
// Pushing the inequality into WHERE means every returned row is a
|
|
796
|
+
// valid non-self prior and the LIMIT == lookback fits exactly.
|
|
797
|
+
const rows = store.db.prepare(
|
|
798
|
+
`SELECT query_text FROM context_usage
|
|
799
|
+
WHERE session_id = ?
|
|
800
|
+
AND hook_name = 'context-surfacing'
|
|
801
|
+
AND timestamp > ?
|
|
802
|
+
AND query_text IS NOT NULL
|
|
803
|
+
AND query_text != ''
|
|
804
|
+
AND query_text != ?
|
|
805
|
+
ORDER BY id DESC
|
|
806
|
+
LIMIT ?`,
|
|
807
|
+
).all(sessionId, cutoff, currentQuery, lookback) as { query_text: string }[];
|
|
808
|
+
|
|
809
|
+
for (const row of rows) {
|
|
810
|
+
if (!row.query_text) continue;
|
|
811
|
+
priors.push(row.query_text);
|
|
812
|
+
}
|
|
813
|
+
} catch {
|
|
814
|
+
// query_text column may be missing on a pre-migration store, or
|
|
815
|
+
// the DB might be in a corrupted state — fall back to current-only.
|
|
816
|
+
return currentQuery;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
if (priors.length === 0) return currentQuery;
|
|
820
|
+
|
|
821
|
+
// Assemble newest-first: current first, then newest prior, then older.
|
|
822
|
+
// The SQL already ordered rows DESC by id, so `priors[0]` is the newest.
|
|
823
|
+
const segments = [currentQuery, ...priors];
|
|
824
|
+
const combined = segments.join("\n\n");
|
|
825
|
+
|
|
826
|
+
if (combined.length <= maxChars) return combined;
|
|
827
|
+
|
|
828
|
+
// Over budget. Current query ALWAYS wins — include the full current
|
|
829
|
+
// prompt first, then add priors newest-first until the budget runs out.
|
|
830
|
+
// If the current prompt alone is already over budget, return it
|
|
831
|
+
// truncated (same as pre-v0.8.1 behavior — MAX_QUERY_LENGTH is
|
|
832
|
+
// enforced earlier in the handler so this branch is rare).
|
|
833
|
+
if (currentQuery.length >= maxChars) return currentQuery.slice(0, maxChars);
|
|
834
|
+
|
|
835
|
+
const parts: string[] = [currentQuery];
|
|
836
|
+
let used = currentQuery.length;
|
|
837
|
+
const separator = "\n\n";
|
|
838
|
+
for (const prior of priors) {
|
|
839
|
+
const cost = separator.length + prior.length;
|
|
840
|
+
if (used + cost > maxChars) break;
|
|
841
|
+
parts.push(prior);
|
|
842
|
+
used += cost;
|
|
843
|
+
}
|
|
844
|
+
return parts.join(separator);
|
|
845
|
+
}
|
|
846
|
+
|
|
703
847
|
/**
|
|
704
848
|
* Check if the agent should be nudged to use lifecycle tools.
|
|
705
849
|
* Returns true if N+ context-surfacing invocations have occurred since the
|
package/src/hooks.ts
CHANGED
|
@@ -379,6 +379,12 @@ export function smartTruncate(text: string, maxChars: number = 300): string {
|
|
|
379
379
|
|
|
380
380
|
/**
|
|
381
381
|
* Log a context injection to the usage tracking table.
|
|
382
|
+
*
|
|
383
|
+
* `queryText` (v0.8.1 Ext 6b) is the raw prompt for this turn. Persisted
|
|
384
|
+
* only when the caller passes it — logEmptyTurn-style skip paths omit it
|
|
385
|
+
* so gated turns (slash commands, heartbeats, noise) cannot leak raw
|
|
386
|
+
* prompt text into `context_usage.query_text`. Pre-migration stores
|
|
387
|
+
* transparently drop the column via `insertUsageFn`'s feature-detect.
|
|
382
388
|
*/
|
|
383
389
|
export function logInjection(
|
|
384
390
|
store: Store,
|
|
@@ -386,7 +392,8 @@ export function logInjection(
|
|
|
386
392
|
hookName: string,
|
|
387
393
|
injectedPaths: string[],
|
|
388
394
|
estimatedTokens: number,
|
|
389
|
-
turnIndex?: number
|
|
395
|
+
turnIndex?: number,
|
|
396
|
+
queryText?: string
|
|
390
397
|
): number {
|
|
391
398
|
try {
|
|
392
399
|
const usageId = store.insertUsage({
|
|
@@ -397,6 +404,7 @@ export function logInjection(
|
|
|
397
404
|
estimatedTokens,
|
|
398
405
|
wasReferenced: 0,
|
|
399
406
|
turnIndex,
|
|
407
|
+
queryText,
|
|
400
408
|
});
|
|
401
409
|
|
|
402
410
|
// Record co-activation for all injected paths (E3)
|
package/src/store.ts
CHANGED
|
@@ -496,17 +496,36 @@ function initializeDatabase(db: Database): void {
|
|
|
496
496
|
injected_paths TEXT NOT NULL DEFAULT '[]',
|
|
497
497
|
estimated_tokens INTEGER NOT NULL DEFAULT 0,
|
|
498
498
|
was_referenced INTEGER NOT NULL DEFAULT 0,
|
|
499
|
-
turn_index INTEGER NOT NULL DEFAULT 0
|
|
499
|
+
turn_index INTEGER NOT NULL DEFAULT 0,
|
|
500
|
+
query_text TEXT
|
|
500
501
|
)
|
|
501
502
|
`);
|
|
502
503
|
db.exec(`CREATE INDEX IF NOT EXISTS idx_context_usage_session ON context_usage(session_id)`);
|
|
503
504
|
|
|
504
505
|
// Migration: add turn_index to existing context_usage
|
|
505
|
-
|
|
506
|
+
let cuCols = db.prepare("PRAGMA table_info(context_usage)").all() as { name: string }[];
|
|
506
507
|
if (!cuCols.some(c => c.name === "turn_index")) {
|
|
507
508
|
try { db.exec(`ALTER TABLE context_usage ADD COLUMN turn_index INTEGER NOT NULL DEFAULT 0`); } catch { /* exists */ }
|
|
509
|
+
cuCols = db.prepare("PRAGMA table_info(context_usage)").all() as { name: string }[];
|
|
508
510
|
}
|
|
509
511
|
|
|
512
|
+
// v0.8.1 Ext 6b: add nullable query_text column to existing context_usage
|
|
513
|
+
// so multi-turn lookback can persist the raw prompt alongside turn_index.
|
|
514
|
+
// The column is nullable and defaults to NULL — pre-migration rows are
|
|
515
|
+
// treated as "no prior query" by buildMultiTurnSurfacingQuery, preserving
|
|
516
|
+
// the current-prompt-only fallback for any session that predates v0.8.1.
|
|
517
|
+
if (!cuCols.some(c => c.name === "query_text")) {
|
|
518
|
+
try { db.exec(`ALTER TABLE context_usage ADD COLUMN query_text TEXT`); } catch { /* exists */ }
|
|
519
|
+
}
|
|
520
|
+
// Cache the column presence for insertUsageFn so it can build the INSERT
|
|
521
|
+
// statement without running PRAGMA table_info on every write path.
|
|
522
|
+
contextUsageHasQueryTextCache.set(
|
|
523
|
+
db,
|
|
524
|
+
db.prepare("PRAGMA table_info(context_usage)")
|
|
525
|
+
.all()
|
|
526
|
+
.some((c) => (c as { name: string }).name === "query_text"),
|
|
527
|
+
);
|
|
528
|
+
|
|
510
529
|
// Hook prompt dedupe: suppress duplicate/heartbeat prompts to reduce GPU churn.
|
|
511
530
|
db.exec(`
|
|
512
531
|
CREATE TABLE IF NOT EXISTS hook_dedupe (
|
|
@@ -895,6 +914,12 @@ function initializeDatabase(db: Database): void {
|
|
|
895
914
|
// Per-database dimension cache (WeakMap keyed by db object — no collisions for in-memory DBs)
|
|
896
915
|
const vecTableDimsCache = new WeakMap<Database, number>();
|
|
897
916
|
|
|
917
|
+
// v0.8.1 Ext 6b: per-database cache for the query_text column presence on
|
|
918
|
+
// context_usage. Set once at migration time so insertUsageFn can pick the
|
|
919
|
+
// correct INSERT shape without running PRAGMA on every write. Falls back
|
|
920
|
+
// to `false` (safe — equivalent to pre-migration behavior) when absent.
|
|
921
|
+
const contextUsageHasQueryTextCache = new WeakMap<Database, boolean>();
|
|
922
|
+
|
|
898
923
|
function ensureVecTableInternal(db: Database, dimensions: number): void {
|
|
899
924
|
if (vecTableDimsCache.get(db) === dimensions) return;
|
|
900
925
|
|
|
@@ -1722,6 +1747,13 @@ export type UsageRecord = {
|
|
|
1722
1747
|
estimatedTokens: number;
|
|
1723
1748
|
wasReferenced: number;
|
|
1724
1749
|
turnIndex?: number;
|
|
1750
|
+
/**
|
|
1751
|
+
* v0.8.1 Ext 6b: raw user prompt for this turn. Written when the caller
|
|
1752
|
+
* wants the row to be usable for multi-turn lookback retrieval. Persisted
|
|
1753
|
+
* via `insertUsageFn` only when the `query_text` column is present on
|
|
1754
|
+
* `context_usage` (pre-migration stores degrade to "no prior query").
|
|
1755
|
+
*/
|
|
1756
|
+
queryText?: string;
|
|
1725
1757
|
};
|
|
1726
1758
|
|
|
1727
1759
|
export type UsageRow = {
|
|
@@ -3939,10 +3971,33 @@ function getRecentSessionsFn(db: Database, limit: number): SessionRecord[] {
|
|
|
3939
3971
|
// =============================================================================
|
|
3940
3972
|
|
|
3941
3973
|
function insertUsageFn(db: Database, usage: UsageRecord): number {
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3974
|
+
// v0.8.1 Ext 6b: write query_text when the column is present AND the
|
|
3975
|
+
// caller provided one. The column presence is cached at migration time
|
|
3976
|
+
// in contextUsageHasQueryTextCache — missing entries default to false
|
|
3977
|
+
// so ad-hoc DBs constructed outside createStore() degrade gracefully
|
|
3978
|
+
// to the pre-v0.8.1 INSERT shape.
|
|
3979
|
+
const hasQueryText = contextUsageHasQueryTextCache.get(db) ?? false;
|
|
3980
|
+
if (hasQueryText) {
|
|
3981
|
+
db.prepare(`
|
|
3982
|
+
INSERT INTO context_usage
|
|
3983
|
+
(session_id, timestamp, hook_name, injected_paths, estimated_tokens, was_referenced, turn_index, query_text)
|
|
3984
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
3985
|
+
`).run(
|
|
3986
|
+
usage.sessionId,
|
|
3987
|
+
usage.timestamp,
|
|
3988
|
+
usage.hookName,
|
|
3989
|
+
JSON.stringify(usage.injectedPaths),
|
|
3990
|
+
usage.estimatedTokens,
|
|
3991
|
+
usage.wasReferenced,
|
|
3992
|
+
usage.turnIndex ?? 0,
|
|
3993
|
+
usage.queryText ?? null,
|
|
3994
|
+
);
|
|
3995
|
+
} else {
|
|
3996
|
+
db.prepare(`
|
|
3997
|
+
INSERT INTO context_usage (session_id, timestamp, hook_name, injected_paths, estimated_tokens, was_referenced, turn_index)
|
|
3998
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
3999
|
+
`).run(usage.sessionId, usage.timestamp, usage.hookName, JSON.stringify(usage.injectedPaths), usage.estimatedTokens, usage.wasReferenced, usage.turnIndex ?? 0);
|
|
4000
|
+
}
|
|
3946
4001
|
// Return the rowid of the just-inserted row for recall event linkage
|
|
3947
4002
|
const row = db.prepare("SELECT last_insert_rowid() as id").get() as { id: number };
|
|
3948
4003
|
return row.id;
|