clawmem 0.14.0 → 0.15.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.
- package/AGENTS.md +165 -716
- package/CLAUDE.md +4 -786
- package/README.md +20 -191
- package/SKILL.md +157 -711
- package/docs/clawmem-architecture.excalidraw +2415 -0
- package/docs/clawmem-architecture.png +0 -0
- package/docs/clawmem_hero.jpg +0 -0
- package/docs/concepts/architecture.md +413 -0
- package/docs/concepts/composite-scoring.md +133 -0
- package/docs/concepts/hooks-vs-mcp.md +156 -0
- package/docs/concepts/multi-vault.md +71 -0
- package/docs/contributing.md +101 -0
- package/docs/guides/cloud-embedding.md +134 -0
- package/docs/guides/hermes-plugin.md +187 -0
- package/docs/guides/inference-services.md +144 -0
- package/docs/guides/multi-vault-config.md +84 -0
- package/docs/guides/openclaw-plugin.md +306 -0
- package/docs/guides/setup-hooks.md +146 -0
- package/docs/guides/setup-mcp.md +76 -0
- package/docs/guides/systemd-services.md +332 -0
- package/docs/guides/upgrading.md +566 -0
- package/docs/internals/entity-resolution.md +135 -0
- package/docs/internals/graph-traversal.md +85 -0
- package/docs/internals/intent-search-pipeline.md +103 -0
- package/docs/internals/query-pipeline.md +100 -0
- package/docs/introduction.md +104 -0
- package/docs/quickstart.md +158 -0
- package/docs/reference/cli.md +195 -0
- package/docs/reference/configuration.md +101 -0
- package/docs/reference/mcp-tools.md +336 -0
- package/docs/reference/rest-api.md +204 -0
- package/docs/troubleshooting.md +330 -0
- package/package.json +2 -1
- package/src/memory.ts +2 -0
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
# ClawMem architecture
|
|
2
|
+
|
|
3
|
+
ClawMem stores AI agent memory in a single SQLite vault that combines BM25 full-text search with vector embeddings and graph-based retrieval. This page explains how vaults, collections, documents, and search backends fit together.
|
|
4
|
+
|
|
5
|
+
## Vaults
|
|
6
|
+
|
|
7
|
+
A vault is a single SQLite database file containing all of ClawMem's data: documents, content, vectors, relations, sessions, and usage tracking. The default vault lives at `~/.cache/clawmem/index.sqlite`.
|
|
8
|
+
|
|
9
|
+
SQLite is used in WAL (Write-Ahead Logging) mode with `busy_timeout=5000ms`, allowing concurrent reads from multiple processes (e.g., Claude Code and OpenClaw sharing the same vault).
|
|
10
|
+
|
|
11
|
+
## Collections
|
|
12
|
+
|
|
13
|
+
Collections are named groups of documents sourced from a directory. Each collection has:
|
|
14
|
+
|
|
15
|
+
- **Name** — identifier (e.g., `notes`, `research`, `memory`)
|
|
16
|
+
- **Path** — absolute directory path to scan
|
|
17
|
+
- **Pattern** — glob pattern for files to index (default: `**/*.md`)
|
|
18
|
+
|
|
19
|
+
Collections are defined in `~/.config/clawmem/config.yaml` (symlinked as `index.yml` in the vault directory).
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
collections:
|
|
23
|
+
notes:
|
|
24
|
+
path: ~/notes
|
|
25
|
+
pattern: "**/*.md"
|
|
26
|
+
research:
|
|
27
|
+
path: ~/research
|
|
28
|
+
pattern: "**/*.md"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Only markdown files are indexed. Binary files, code, and credentials are never indexed.
|
|
32
|
+
|
|
33
|
+
### What to index
|
|
34
|
+
|
|
35
|
+
The retrieval pipeline surfaces better results from a richer corpus. Beyond the default memory and session log patterns, consider adding collections for research notes, architecture decisions, domain references, project specs, and any markdown you regularly consult during agent sessions. The broader the indexed field, the more likely context-surfacing will find something relevant to the current task.
|
|
36
|
+
|
|
37
|
+
Code files (`.ts`, `.py`, `.go`, etc.) are intentionally excluded. BM25 and embedding models trained on natural language don't perform well on code syntax — variable names, imports, and bracket-heavy constructs pollute the search index. Code retrieval is better served by tools built for that purpose (tree-sitter, LSP, semantic code search). Capture your technical decisions and architecture rationale in markdown instead; that's what agents need when making decisions during a coding session.
|
|
38
|
+
|
|
39
|
+
### Excluded directories
|
|
40
|
+
|
|
41
|
+
These directories are always skipped during indexing:
|
|
42
|
+
|
|
43
|
+
`_PRIVATE`, `.clawmem`, `.git`, `.obsidian`, `.logseq`, `.foam`, `.dendron`, `.trash`, `.stversions`, `node_modules`, `.cache`, `vendor`, `dist`, `build`, `gits`, `scraped`
|
|
44
|
+
|
|
45
|
+
## Documents
|
|
46
|
+
|
|
47
|
+
Each indexed file becomes a document with:
|
|
48
|
+
|
|
49
|
+
| Field | Description |
|
|
50
|
+
|-------|-------------|
|
|
51
|
+
| `collection` | Which collection it belongs to |
|
|
52
|
+
| `path` | Relative path within the collection |
|
|
53
|
+
| `title` | Extracted from first heading or filename |
|
|
54
|
+
| `hash` | SHA-256 of content (canonical identity) |
|
|
55
|
+
| `docid` | First 6 hex chars of hash (for human reference) |
|
|
56
|
+
| `content_type` | Auto-detected: decision, deductive, preference, note, handoff, conversation, progress, research, hub, antipattern, project, milestone, problem |
|
|
57
|
+
| `quality_score` | 0.0-1.0 based on length, structure, headings, lists, decision keywords, frontmatter |
|
|
58
|
+
| `confidence` | Starts at 0.5, adjusted by contradiction detection and feedback |
|
|
59
|
+
| `pinned` | Manual boost flag (+0.3 composite score) |
|
|
60
|
+
| `snoozed_until` | Temporarily hidden from context surfacing |
|
|
61
|
+
|
|
62
|
+
## Fragments
|
|
63
|
+
|
|
64
|
+
Each document is split into fragments for embedding:
|
|
65
|
+
|
|
66
|
+
- **full** — entire document (if under size limit)
|
|
67
|
+
- **section** — content under each heading
|
|
68
|
+
- **list** — list items grouped together
|
|
69
|
+
- **code** — code blocks
|
|
70
|
+
|
|
71
|
+
Fragments are embedded independently. The full-document fragment catches broad queries; section/list/code fragments provide precision.
|
|
72
|
+
|
|
73
|
+
## Search backends
|
|
74
|
+
|
|
75
|
+
| Backend | Signal | GPU cost | Use case |
|
|
76
|
+
|---------|--------|----------|----------|
|
|
77
|
+
| BM25 (FTS5) | Keyword exact match | 0 | Known terms, spot checks |
|
|
78
|
+
| Vector (vec0) | Semantic similarity | 1 call | Conceptual queries, fuzzy recall |
|
|
79
|
+
| Hybrid (RRF) | BM25 + Vector fused | 1+ calls | General recall (default) |
|
|
80
|
+
|
|
81
|
+
BM25 uses SQLite's FTS5 extension with prefix matching. Vector search uses the `vec0` extension with cosine similarity. Embedding dimensions depend on the model: 768 for the default EmbeddingGemma-300M, 2560 for the SOTA zembed-1, or provider-determined for cloud embedding.
|
|
82
|
+
|
|
83
|
+
## Graphs
|
|
84
|
+
|
|
85
|
+
ClawMem maintains a `memory_relations` table of typed edges between documents: semantic, supporting, contradicts, causal, and temporal. These edges let `intent_search` answer "why" and "what led to" questions by following chains across documents rather than relying on keyword or vector similarity alone.
|
|
86
|
+
|
|
87
|
+
A separate `entity_triples` table stores structured SPO (Subject-Predicate-Object) facts with temporal validity (`valid_from`/`valid_to`). Triples are emitted by the observer LLM alongside facts as `<triples>` blocks in each observation, parsed and validated against a tight canonical predicate vocabulary (adopted, migrated_to, deployed_to, runs_on, replaced, depends_on, integrates_with, uses, prefers, avoids, caused_by, resolved_by, owned_by), then persisted by `decision-extractor` using canonical `vault:type:slug` entity IDs via `ensureEntityCanonical`. Subject/object type inheritance is ambiguity-safe (exact-match only, defaults to `concept` on unknown or ambiguous names). Query via `kg_query(entity)` for structured entity relationships — accepts entity name or canonical ID.
|
|
88
|
+
|
|
89
|
+
Most edges are created automatically. When new documents are indexed, A-MEM finds vector neighbors and uses the LLM to classify relationships (semantic, supporting, contradicts). When `decision-extractor` runs after each response, it infers causal links between observations and extracts SPO triples from facts. The `build_graphs` MCP tool adds temporal backbone edges (creation-order) and bulk semantic edges — run it after large ingestion batches, not after routine indexing.
|
|
90
|
+
|
|
91
|
+
See [graph traversal](../internals/graph-traversal.md) for edge types, traversal mechanics, and beam search details.
|
|
92
|
+
|
|
93
|
+
## Consolidation safety (v0.7.1)
|
|
94
|
+
|
|
95
|
+
The background consolidation worker has three independent safety gates that prevent observation contamination, accidental cross-entity merges, and unchecked contradictions from landing in the vault.
|
|
96
|
+
|
|
97
|
+
### Phase 2 — name-aware merge safety
|
|
98
|
+
|
|
99
|
+
When the worker finds a candidate existing observation to merge a new pattern into, it runs a deterministic name-aware gate before updating. The gate extracts entity anchors from both the new and existing texts — first via `entity_mentions` (if the source docs are enriched), falling back to lexical proper-noun extraction — and compares them with normalized character 3-gram cosine similarity. When anchor sets differ materially (Jaccard ≤ 0.5), the merge is hard-rejected regardless of text similarity. Otherwise, a dual-threshold score applies: `CLAWMEM_MERGE_SCORE_NORMAL` (default `0.93`) for aligned anchors, `CLAWMEM_MERGE_SCORE_STRICT` (default `0.98`) as the strictest fallback. This prevents "Alice decided X" from merging into "Bob decided X" just because the predicate is identical. Set `CLAWMEM_MERGE_GUARD_DRY_RUN=true` to log rejections without enforcing them.
|
|
100
|
+
|
|
101
|
+
### Phase 2 — contradiction-aware merge gate
|
|
102
|
+
|
|
103
|
+
After the name-aware gate passes, the worker checks whether the new observation contradicts the existing one. A deterministic heuristic runs first (negation asymmetry, number/date mismatch), then an LLM check confirms. If the final confidence exceeds `CLAWMEM_CONTRADICTION_MIN_CONFIDENCE` (default `0.5`), the merge is blocked and one of two policies applies (controlled by `CLAWMEM_CONTRADICTION_POLICY`):
|
|
104
|
+
|
|
105
|
+
- `link` (default) — insert a new `consolidated_observations` row and create a `contradicts` edge in `memory_relations` between the two rows. Both remain active and queryable.
|
|
106
|
+
- `supersede` — insert the new row and mark the old row `status='inactive'` with `invalidated_at`/`superseded_by` set. The old row is filtered from retrieval but preserved for audit.
|
|
107
|
+
|
|
108
|
+
Phase 3 deductive synthesis applies the same `contradicts` link for any draft that matches a prior deductive observation with conflicting content.
|
|
109
|
+
|
|
110
|
+
### Phase 3 — anti-contamination deductive synthesis
|
|
111
|
+
|
|
112
|
+
Phase 3 synthesizes cross-session insights from recent observations into `content_type='deductive'` documents. The draft-generation LLM can produce drafts whose conclusion references entities that only appear in the candidate pool but not in the cited sources — a form of context bleed. Each draft runs through a three-layer validator:
|
|
113
|
+
|
|
114
|
+
1. **Deterministic pre-checks** — reject empty conclusions; reject drafts whose `source_indices` don't resolve to at least two unique source docs; reject drafts whose conclusion names an entity (entity-aware via `entity_mentions`, lexical fallback via proper-noun extraction) that exists in the candidate pool but not in any cited source.
|
|
115
|
+
2. **LLM validator** — a separate LLM call checks that the conclusion is genuinely supported by the cited source snippets. Fail-open: if the validator times out or returns malformed JSON, the draft is accepted and flagged via the `validatorFallbackAccepts` stat so operators can detect when the LLM path is effectively offline.
|
|
116
|
+
3. **Dedupe** — accepted drafts are compared against recent deductive observations to prevent duplicates.
|
|
117
|
+
|
|
118
|
+
Rejection reasons are tracked individually in `DeductiveSynthesisStats` (`contaminationRejects`, `invalidIndexRejects`, `unsupportedRejects`, `emptyRejects`, `dedupSkipped`, `validatorFallbackAccepts`) so Phase 3 yield can be diagnosed without enabling extra logging.
|
|
119
|
+
|
|
120
|
+
## Post-import conversation synthesis (v0.7.2)
|
|
121
|
+
|
|
122
|
+
`clawmem mine` imports raw chat exports (Claude Code, ChatGPT, Claude.ai, Slack, plain text) as `content_type='conversation'` full-text documents. Conversations preserve the narrative but rarely cluster well in retrieval — the same decision can appear across many turns and many conversations, and BM25 or vector search surfaces the prose rather than the structured claim underneath. The `--synthesize` opt-in flag adds a post-import LLM pass that walks the freshly indexed conversations and extracts first-class structured facts (decisions, preferences, milestones, problems) with cross-fact relations, writing them as searchable documents alongside the raw exchanges.
|
|
123
|
+
|
|
124
|
+
The synthesis module is `src/conversation-synthesis.ts`. It runs **after** `indexCollection` has committed the raw conversation docs, and a failure inside the synthesis pipeline never rolls back the mine import — the raw conversations remain indexed.
|
|
125
|
+
|
|
126
|
+
### Two-pass pipeline
|
|
127
|
+
|
|
128
|
+
**Pass 1 — fact extraction.** For each conversation doc in the target collection (capped by `--synthesis-max-docs`, default 20), the pipeline:
|
|
129
|
+
|
|
130
|
+
1. Sends the conversation body (truncated to 3000 chars) to the LLM with a strict extraction prompt. The prompt lists the four allowed `contentType` values (`decision`, `preference`, `milestone`, `problem`) and the six allowed `relationType` values (`semantic`, `supporting`, `contradicts`, `causal`, `temporal`, `entity`), and explicitly authorizes links that reference facts from **other** conversations in the same imported batch.
|
|
131
|
+
2. Parses the response via `extractJsonFromLLM` (the same helper the A-MEM pipeline uses, robust to truncated arrays and markdown fences).
|
|
132
|
+
3. Normalizes each fact: rejects empty titles, disallowed contentTypes, non-string facts/aliases entries, links with bad relation types, and clamps weights to `[0, 1]`.
|
|
133
|
+
4. Writes each valid fact via dedup-aware `saveMemory` with a stable synthesized path:
|
|
134
|
+
```
|
|
135
|
+
synthesized/<slug(title)>-src<sourceDocId>-<short sha256(normalized title)>.md
|
|
136
|
+
```
|
|
137
|
+
The path is a pure function of `(sourceDocId, slug, hash(normalizedTitle))`. No encounter-order dependence. Same-slug collisions (`Use OAuth.` and `Use OAuth!` both slugify to `use-oauth`) are disambiguated by the stable hash suffix — reruns in different LLM order still pin each title to the same path, so `saveMemory`'s `UNIQUE(collection, path)` update branch is hit instead of creating parallel rows.
|
|
138
|
+
5. Populates a local alias map: `Map<normalizedTitleOrAlias, Set<docId>>`. Each fact contributes its canonical title and every alias into the Set. If two different facts claim the same title or alias the Set accumulates multiple docIds and later becomes ambiguous.
|
|
139
|
+
|
|
140
|
+
`extractFactsFromConversation` returns `ExtractedFact[] | null`. A `null` return discriminates "LLM path failed" (null response, thrown generate, non-array JSON) from a valid empty extraction `[]`. The orchestrator uses this to increment either `llmFailures` or `docsWithNoFacts` — two distinct operator counters that were previously conflated as `nullCalls`.
|
|
141
|
+
|
|
142
|
+
**Pass 2 — link resolution.** Runs after Pass 1 finishes for every doc in the batch (skipped entirely in `--dry-run` mode). For each saved fact's `links[]`:
|
|
143
|
+
|
|
144
|
+
1. `resolveLinkTarget` first checks the local map. If the entry maps to exactly one docId, that's the resolved target. If the set contains two or more distinct docIds, the link is treated as **ambiguous** and counted as unresolved — the resolver fails closed rather than silently binding to an arbitrary candidate.
|
|
145
|
+
2. If the local map has no entry, the resolver falls back to a SQL lookup scoped to the same collection: `SELECT id FROM documents WHERE collection=? AND active=1 AND LOWER(TRIM(title))=? LIMIT 2`. If the result contains more than one row (two pre-existing docs with duplicate titles), the link is again treated as ambiguous.
|
|
146
|
+
3. Self-referencing links (target resolves to the source fact's own docId) are skipped.
|
|
147
|
+
4. Resolved links are inserted into `memory_relations` via a weight-monotonic upsert:
|
|
148
|
+
```sql
|
|
149
|
+
INSERT INTO memory_relations (source_id, target_id, relation_type, weight, metadata, created_at)
|
|
150
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
151
|
+
ON CONFLICT(source_id, target_id, relation_type)
|
|
152
|
+
DO UPDATE SET weight = MAX(weight, excluded.weight)
|
|
153
|
+
```
|
|
154
|
+
This policy is idempotent on equal-weight reruns (no inflation) but monotonically accepts stronger later evidence (a subsequent run that finds the same triple with higher weight updates the existing row; a subsequent run with lower weight leaves it untouched). The earlier `INSERT OR IGNORE` would have under-accumulated by discarding legitimate stronger evidence, and `store.insertRelation`'s `weight += excluded.weight` would have over-accumulated by inflating weights linearly with rerun count.
|
|
155
|
+
|
|
156
|
+
### Failure model
|
|
157
|
+
|
|
158
|
+
All LLM failures, JSON parse errors, saveMemory collisions, and relation insert errors are caught and counted, never re-thrown. The final `SynthesisResult` exposes seven counters:
|
|
159
|
+
|
|
160
|
+
| Counter | Meaning |
|
|
161
|
+
|---------|---------|
|
|
162
|
+
| `docsScanned` | Conversations selected for extraction |
|
|
163
|
+
| `factsExtracted` | Facts that passed validation (per fact across all docs) |
|
|
164
|
+
| `factsSaved` | Facts where `saveMemory` returned `inserted` or `updated` (deduplicated is counted as extracted but not saved-new) |
|
|
165
|
+
| `linksResolved` | Links that bound to a unique non-self target and landed in `memory_relations` |
|
|
166
|
+
| `linksUnresolved` | Links that couldn't resolve (unknown target, ambiguous local, ambiguous SQL, self-reference) |
|
|
167
|
+
| `llmFailures` | Docs where the LLM path failed — null, thrown, or non-array JSON |
|
|
168
|
+
| `docsWithNoFacts` | Docs where the LLM responded validly but returned zero facts (or all candidates were rejected by normalize) |
|
|
169
|
+
|
|
170
|
+
Synthesis runs only when the user explicitly passes `--synthesize` — it is off by default because each pass drives one extra LLM call per conversation doc. Reruns over the same collection are safe: paths are stable, relation weights are monotone, saveMemory dedup collapses true duplicates.
|
|
171
|
+
|
|
172
|
+
## Heavy maintenance lane (v0.8.0)
|
|
173
|
+
|
|
174
|
+
The consolidation worker that ticks every 5 minutes (the "light lane") is tuned for interactive sessions — it backfills A-MEM notes on the newest three documents per tick and runs Phase 2 consolidation every 30 minutes and Phase 3 deductive synthesis every 15 minutes. On large vaults this keeps context-surfacing happy but is too slow to catch up on a multi-thousand-document backlog or to apply anomaly-first reviews to long-tail content. v0.8.0 adds a **second worker** — the **heavy maintenance lane** — that runs on a longer interval, only during configured quiet windows, with DB-backed exclusivity and stale-first batching. It is **off by default** and requires `CLAWMEM_HEAVY_LANE=true` to start. The light lane is unchanged.
|
|
175
|
+
|
|
176
|
+
The heavy lane lives in `src/maintenance.ts`. Exclusivity is provided by `src/worker-lease.ts`. Schema additions are in `store.ts`: a `maintenance_runs` journal table and a `worker_leases` exclusivity table.
|
|
177
|
+
|
|
178
|
+
### Why a second lane
|
|
179
|
+
|
|
180
|
+
Running Phase 2/3 more aggressively in the light lane would starve interactive sessions. Running them only when the user is idle requires knowing when the user is idle — in v0.8.0 that signal is the existing `context_usage` table (the same v0.7.0 telemetry that `recall_events` feeds off). The heavy lane counts context injections in the last 10 minutes and skips when the rate exceeds its configured cap. No new `query_activity` table is needed.
|
|
181
|
+
|
|
182
|
+
### Quiet-window gating
|
|
183
|
+
|
|
184
|
+
Two knobs select when the heavy lane is allowed to fire:
|
|
185
|
+
|
|
186
|
+
- **Hour window** — `CLAWMEM_HEAVY_LANE_WINDOW_START` and `_WINDOW_END` accept integer hours `0-23`. The lane runs when the current local hour is inside `[start, end)`. Midnight wraparound is supported: `start=22, end=6` means "10 PM through 6 AM". When either bound is unset (the default), the window check is skipped entirely.
|
|
187
|
+
- **Query-rate cap** — `CLAWMEM_HEAVY_LANE_MAX_USAGES` caps the number of `context_usage` rows in the last 10 minutes. The default of 30 is conservative; raise it on vaults where many concurrent agents share a store. The query runs `SELECT COUNT(*) FROM context_usage WHERE timestamp > ?` with the cutoff computed in JS and bound as a parameter (the naive `datetime('now', '-10 minutes')` pattern returns a space-separated string that sorts incorrectly against the ISO 8601 `T`-separated timestamps that `context_usage.timestamp` is actually written with).
|
|
188
|
+
|
|
189
|
+
Gate failures write a `maintenance_runs` row with `phase='gate'`, `status='skipped'`, and `reason='outside_window'` or `reason='query_rate_high'` so operators can tell whether the lane is being gated by the schedule or by actual activity.
|
|
190
|
+
|
|
191
|
+
### Worker lease exclusivity
|
|
192
|
+
|
|
193
|
+
Even when the gate passes, two processes sharing a vault could start heavy ticks simultaneously. v0.8.0 adds a `worker_leases` table and an atomic acquire path in `src/worker-lease.ts`:
|
|
194
|
+
|
|
195
|
+
```sql
|
|
196
|
+
INSERT INTO worker_leases (worker_name, lease_token, acquired_at, expires_at)
|
|
197
|
+
VALUES (?, ?, ?, ?)
|
|
198
|
+
ON CONFLICT(worker_name) DO UPDATE SET
|
|
199
|
+
lease_token = excluded.lease_token,
|
|
200
|
+
acquired_at = excluded.acquired_at,
|
|
201
|
+
expires_at = excluded.expires_at
|
|
202
|
+
WHERE worker_leases.expires_at <= excluded.acquired_at
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
The `WHERE` clause on the upsert path only reclaims a row whose existing `expires_at` has passed, so a live lease blocks the conflict branch entirely and SQLite reports `changes=0`. The caller interprets `changes === 0` as "another worker holds it" and returns `{ acquired: false }`. A single statement means no SELECT-then-INSERT race window exists across processes — the old TurnArc-style `transaction(SELECT → if existing → UPDATE else INSERT)` pattern had a window where two callers could both observe "no row" and then one would hit a UNIQUE violation on its INSERT, which would throw instead of returning a cooperative "busy" result.
|
|
206
|
+
|
|
207
|
+
Acquired leases return a random 16-byte hex fencing token. `releaseWorkerLease` deletes the row only when `worker_name = ? AND lease_token = ?`, so a lease that has been reclaimed by another worker after TTL expiry cannot be torn down by the original holder on its way out. The entire acquire path is wrapped in a `try/catch` that translates `SQLITE_BUSY` (pathological contention under heavy WAL pressure) and any other DB error into `{ acquired: false }` so the advertised non-throw contract holds for `shouldRunHeavyMaintenance`-style gates layered on top.
|
|
208
|
+
|
|
209
|
+
A lease TTL of 10 minutes by default (`CLAWMEM_HEAVY_LANE_INTERVAL` / 3 or thereabouts) covers the worst-case duration of a Phase 2 + Phase 3 run; if a worker crashes mid-tick, the lease naturally expires and the next tick reclaims it.
|
|
210
|
+
|
|
211
|
+
Failure to acquire the lease writes a `maintenance_runs` row with `status='skipped'` and `reason='lease_unavailable'`.
|
|
212
|
+
|
|
213
|
+
### Stale-first selection
|
|
214
|
+
|
|
215
|
+
The default light-lane Phase 2 SELECT orders by `modified_at DESC` so the most-recently-changed observations are consolidated first. That works for an interactive agent but neglects long-tail content whose consolidated patterns never get refreshed. The heavy lane passes `staleOnly: true` into `consolidateObservations`, which switches the SQL to:
|
|
216
|
+
|
|
217
|
+
```sql
|
|
218
|
+
SELECT d.id, d.title, d.facts, d.amem_context AS context, d.modified_at, d.collection
|
|
219
|
+
FROM documents d
|
|
220
|
+
LEFT JOIN recall_stats rs ON rs.doc_id = d.id
|
|
221
|
+
WHERE d.active = 1
|
|
222
|
+
AND d.content_type = 'observation'
|
|
223
|
+
AND d.facts IS NOT NULL
|
|
224
|
+
AND d.id NOT IN (
|
|
225
|
+
SELECT value FROM (
|
|
226
|
+
SELECT json_each.value AS value
|
|
227
|
+
FROM consolidated_observations co, json_each(co.source_doc_ids)
|
|
228
|
+
WHERE co.status = 'active'
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
ORDER BY d.collection,
|
|
232
|
+
COALESCE(rs.last_recalled_at, d.last_accessed_at, d.modified_at) ASC,
|
|
233
|
+
d.modified_at ASC
|
|
234
|
+
LIMIT ?
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
The `COALESCE` fallback chain is important: a fresh vault has an empty `recall_stats` table, so the ordering has to fall through to `documents.last_accessed_at` (which is backfilled from `modified_at` by the initial migration) and then to `documents.modified_at` itself. An empty `recall_stats` is a first-class case, not an error, and is covered by unit tests that explicitly assert valid stale ordering with zero `recall_stats` rows. The same switching logic applies to Phase 3's recent-observation SELECT when the heavy lane calls `generateDeductiveObservations({ staleOnly: true })`.
|
|
238
|
+
|
|
239
|
+
### Surprisal selector (optional)
|
|
240
|
+
|
|
241
|
+
Stale-first is a good default, but it is driven purely by access timestamps. An operator can instead ask the heavy lane to feed Phase 2 with k-NN anomaly-ranked doc ids by setting `CLAWMEM_HEAVY_LANE_SURPRISAL=true`. The heavy lane then calls `selectSurprisingObservationBatch(store, staleObservationLimit)` — a thin wrapper over the existing `computeSurprisalScores` from `consolidation.ts` — and passes the returned ids into `consolidateObservations` via a new `candidateIds` option. The Phase 2 SELECT then filters `AND d.id IN (?, ?, ...)` against exactly those ids.
|
|
242
|
+
|
|
243
|
+
When the surprisal backend returns an empty array (no embeddings in the vault, `vectors_vec` missing, or the k-NN query yields fewer docs than `k+1`), the heavy lane **falls through to stale-first** rather than doing nothing. The `maintenance_runs.metrics_json` distinguishes the three cases via the `selector` field: `stale-first` (default), `surprisal` (selector returned a non-empty batch), or `surprisal-fallback-stale` (selector returned empty and the lane degraded gracefully). Empty `candidateIds` passed in explicitly is treated as "the selector found nothing" and short-circuits without hitting the LLM — distinct from `candidateIds: undefined` which means "select via the default ordering".
|
|
244
|
+
|
|
245
|
+
### Guarded merge-safety enforcement
|
|
246
|
+
|
|
247
|
+
The light lane respects `CLAWMEM_MERGE_GUARD_DRY_RUN=true` — when set, Phase 2 merge-safety gate rejections are logged but not enforced, giving operators a way to calibrate thresholds before switching the gate on. The heavy lane passes `guarded: true` into `consolidateObservations`, which is threaded down through `synthesizeCluster` into `findSimilarConsolidation(forceEnforce=true)`. With `forceEnforce=true`, the function ignores the dry-run env var and always enforces the name-aware dual-threshold gate. This means experimenting operators cannot weaken heavy-lane guarantees by toggling an env flag, while still keeping the light lane tunable for calibration runs.
|
|
248
|
+
|
|
249
|
+
### Journal rows
|
|
250
|
+
|
|
251
|
+
Every scheduled heavy-lane attempt writes rows to `maintenance_runs` via `insertMaintenanceRun` / `finalizeMaintenanceRun`:
|
|
252
|
+
|
|
253
|
+
| Column | Meaning |
|
|
254
|
+
|---|---|
|
|
255
|
+
| `lane` | Always `heavy` for v0.8.0. The light-lane tick does not journal (would require a larger refactor). |
|
|
256
|
+
| `phase` | `gate` (for skipped rows), `consolidate` (Phase 2), or `deductive` (Phase 3). |
|
|
257
|
+
| `status` | `started` when the row is first written, `completed` on success, `failed` on exception, `skipped` when gating blocks the tick. |
|
|
258
|
+
| `reason` | For skips: `outside_window`, `query_rate_high`, or `lease_unavailable`. For failures: `phase2_exception` or `phase3_exception`. |
|
|
259
|
+
| `selected_count` | For Phase 2: the limit passed to the SELECT (or the surprisal batch size). For Phase 3: `DeductiveSynthesisStats.considered`. |
|
|
260
|
+
| `processed_count` | Phase 3 only: `DeductiveSynthesisStats.drafted`. |
|
|
261
|
+
| `created_count` | Phase 3 only: `DeductiveSynthesisStats.created`. |
|
|
262
|
+
| `rejected_count` | Phase 3 only: `DeductiveSynthesisStats.rejected` (the sum of all reject reasons). |
|
|
263
|
+
| `null_call_count` | Phase 3 only: `DeductiveSynthesisStats.nullCalls`. |
|
|
264
|
+
| `metrics_json` | Phase 2: `{ selector, candidateCount? }`. Phase 3: the full `DeductiveSynthesisStats` breakdown (contamination rejects, invalid index rejects, unsupported rejects, empty rejects, dedupe skipped, validator fallback accepts). |
|
|
265
|
+
| `started_at` / `finished_at` | Both ISO 8601 UTC. `finished_at` is null for rows that were never finalized because the process crashed between `insertMaintenanceRun` and `finalizeMaintenanceRun`. |
|
|
266
|
+
|
|
267
|
+
Operators can use these rows to reconstruct any lane decision without reading worker logs:
|
|
268
|
+
|
|
269
|
+
```sql
|
|
270
|
+
-- Why did the lane skip the most recent tick?
|
|
271
|
+
SELECT status, reason, started_at FROM maintenance_runs
|
|
272
|
+
WHERE lane = 'heavy' AND phase = 'gate'
|
|
273
|
+
ORDER BY started_at DESC LIMIT 5;
|
|
274
|
+
|
|
275
|
+
-- What selector has the heavy lane been running?
|
|
276
|
+
SELECT json_extract(metrics_json, '$.selector') AS selector, COUNT(*)
|
|
277
|
+
FROM maintenance_runs
|
|
278
|
+
WHERE lane = 'heavy' AND phase = 'consolidate' AND status = 'completed'
|
|
279
|
+
GROUP BY selector;
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Vault scoping
|
|
283
|
+
|
|
284
|
+
The heavy lane operates on whatever `Store` it is handed. `createStore(path)` maps 1:1 to a single SQLite vault, so `context_usage` counts and `recall_stats` ordering are both inherently scoped to the current vault via `store.db` — no per-vault predicate is needed in the queries. Multi-vault mode (running the heavy lane across multiple stores from a single process) is explicitly out of scope for v0.8.0 and would require extending `HeavyMaintenanceConfig` with an explicit vault list plus a per-vault lease name.
|
|
285
|
+
|
|
286
|
+
### Dual-host worker architecture (v0.8.2)
|
|
287
|
+
|
|
288
|
+
v0.8.0 shipped the heavy lane wired into `cmdMcp` (the stdio MCP host) only. In a Claude Code deployment that means workers are spawned per-session and die when the user closes Claude Code — defeating the heavy lane's quiet-window premise, which expects a long-lived process running at the configured hours regardless of user interactivity. v0.8.2 adds `cmdWatch` as a second host so the existing `clawmem-watcher.service` systemd user unit can carry both lanes 24/7, alongside the per-session MCP fallback.
|
|
289
|
+
|
|
290
|
+
**Light lane gets its own DB lease.** The v0.8.0 heavy lane already had cross-process exclusivity via `worker_leases` (key `heavy-maintenance`). v0.8.2 extends the same primitive to the light lane (key `light-consolidation`, 10-min default TTL). `runConsolidationTick` now wraps the entire tick body in `withWorkerLease`, so two host processes against the same vault cannot:
|
|
291
|
+
|
|
292
|
+
- both decide `findSimilarConsolidation(...)` is null and both INSERT a duplicate row into `consolidated_observations`
|
|
293
|
+
- both merge into the same existing row and lose source_ids from the read-modify-write update in `mergeIntoExistingConsolidation`
|
|
294
|
+
- double-burn LLM calls on the v0.7.1 anti-contamination wrapper for Phase 3 deductive synthesis
|
|
295
|
+
|
|
296
|
+
The in-process `isRunning` reentrancy guard remains as the cheap first defense that catches overlapping `setInterval` fires before any SQLite round-trip; the lease is the cross-process authority.
|
|
297
|
+
|
|
298
|
+
**Both hosts wire the same env-var gates.** `cmdWatch` and `cmdMcp` both check `CLAWMEM_ENABLE_CONSOLIDATION` and `CLAWMEM_HEAVY_LANE` (parsed via the shared `parseHeavyLaneConfigFromEnv()` helper in `maintenance.ts`). Off by default in both hosts. Operators opt in by setting the env vars on whichever long-lived process they want to host the workers — typically `clawmem-watcher.service` for the canonical setup.
|
|
299
|
+
|
|
300
|
+
**`cmdMcp` warns when heavy lane is enabled.** Per-session stdio MCPs are short-lived and may never see the configured quiet window. When `CLAWMEM_HEAVY_LANE=true` is set on a stdio MCP host, `cmdMcp` emits a one-line warning to stderr advising operators to move heavy-lane hosting to `clawmem watch`. The fallback host still works, just with a visible reminder.
|
|
301
|
+
|
|
302
|
+
**Async drain on shutdown.** Both worker stop helpers (`stopConsolidationWorker` and the closure returned by `startHeavyMaintenanceWorker`) are now `async`. They clear their `setInterval` AND poll their in-flight running flag (`isRunning` / `heavyRunning`) until any mid-tick worker drains. This guarantees the worker's `withWorkerLease` finally block runs against a still-open store, so the lease is released cleanly via `releaseWorkerLease` instead of being abandoned to TTL expiry. The drain wait is bounded — `STOP_DRAIN_TIMEOUT_MS=15s` for the light lane, `HEAVY_STOP_DRAIN_TIMEOUT_MS=30s` for the heavy lane — so a pathologically stuck tick (e.g. unreachable LLM with no socket timeout) cannot wedge shutdown indefinitely. After the timeout, the host logs and exits anyway; the next process reclaims the stale lease via the v0.8.0 atomic upsert.
|
|
303
|
+
|
|
304
|
+
**Signal handlers registered before worker startup.** Both `cmdWatch` and `cmdMcp` now register their `SIGINT`/`SIGTERM` handlers BEFORE any worker initialization. The mutable `stopHeavyLane` is declared at the top, the closure captures it, the handlers are registered immediately, then the workers start and assign into the captured variable. Without this ordering, a `SIGTERM` arriving in the brief window between worker startup and handler registration would be handled by Node's default signal action (terminate with exit 143) and skip the async drain entirely.
|
|
305
|
+
|
|
306
|
+
**Multi-host contention is safe.** Running both `clawmem watch` AND a per-session `clawmem mcp` against the same vault with both env vars enabled is supported. The `worker_leases` table arbitrates: only one host wins each tick, the other journals a skip (heavy lane) or logs a "lease held" message (light lane) and waits for the next interval. Operators who want exactly one worker per lane should set the env vars on `clawmem-watcher.service` only and leave `cmdMcp` unset.
|
|
307
|
+
|
|
308
|
+
## Multi-turn prior-query lookback (v0.8.1)
|
|
309
|
+
|
|
310
|
+
A single-prompt retrieval query is wrong when the user's current turn is short. "Do the same thing for X", "Explain that in more depth", and "Now talk about refresh tokens in the same design" are all legitimate questions whose intent lives in the *previous* turn, not the current one. v0.8.1 teaches `context-surfacing` to build its retrieval query from the current prompt plus up to two recent same-session prior prompts, so short follow-up turns inherit the vocabulary of earlier turns. Everything else (composite scoring, snippet extraction, reranking, dedupe, routing hints, recall attribution) continues to use the raw current prompt unchanged — only the discovery path sees the multi-turn query.
|
|
311
|
+
|
|
312
|
+
The helper lives in `src/hooks/context-surfacing.ts` and is backed by a new nullable `query_text` column on `context_usage`.
|
|
313
|
+
|
|
314
|
+
### Additive schema migration
|
|
315
|
+
|
|
316
|
+
```sql
|
|
317
|
+
ALTER TABLE context_usage ADD COLUMN query_text TEXT;
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
Guarded with `PRAGMA table_info(context_usage)` the same way the existing `turn_index` migration is. Stores created before v0.8.1 pick up the column on first open. A WeakMap `contextUsageHasQueryTextCache` records the column presence per `Database` instance at migration time so `insertUsageFn` can pick the correct INSERT shape without running `PRAGMA table_info` on every write. Ad-hoc stores that construct a `Database` outside `createStore()` default the cache to `false` and fall back to the pre-v0.8.1 7-column INSERT shape — the new code never writes a column that doesn't exist.
|
|
321
|
+
|
|
322
|
+
### Privacy-conscious persistence split
|
|
323
|
+
|
|
324
|
+
Raw prompt text has privacy implications. Two classes of `logEmptyTurn` call site exist in `contextSurfacing`, and they get different treatment:
|
|
325
|
+
|
|
326
|
+
- **Pre-retrieval gates** — slash commands (`prompt.startsWith("/")`), too-short prompts (`< MIN_PROMPT_LENGTH`), `shouldSkipRetrieval` hits (greetings, shell commands, affirmations), and `wasPromptSeenRecently` / `isHeartbeatPrompt` dedupe. These are not meaningful user questions and carry a higher sensitivity profile (they often contain incidental tool output or noise). They write a `context_usage` row with `query_text = NULL` to keep `turn_index` aligned with the transcript but not persist the raw text.
|
|
327
|
+
- **Post-retrieval empty paths** — empty result set, all results in `FILTERED_PATHS`, all results snoozed, activation floor not met, adaptive threshold filter, and empty `buildContext`. These are legitimate user questions that simply didn't match anything. They write a row with `query_text = prompt` so a follow-up turn ("try again" / "what about Y") can still use the intent via multi-turn lookback.
|
|
328
|
+
|
|
329
|
+
The happy path (successful injection) also persists `query_text = prompt`.
|
|
330
|
+
|
|
331
|
+
### Retrieval query construction
|
|
332
|
+
|
|
333
|
+
`buildMultiTurnSurfacingQuery(store, sessionId, currentQuery, lookback=2, maxAgeMinutes=10, maxChars=2000)` fetches recent `query_text` rows via:
|
|
334
|
+
|
|
335
|
+
```sql
|
|
336
|
+
SELECT query_text FROM context_usage
|
|
337
|
+
WHERE session_id = ?
|
|
338
|
+
AND hook_name = 'context-surfacing'
|
|
339
|
+
AND timestamp > ?
|
|
340
|
+
AND query_text IS NOT NULL
|
|
341
|
+
AND query_text != ''
|
|
342
|
+
AND query_text != ? -- SQL-level self-match guard
|
|
343
|
+
ORDER BY id DESC
|
|
344
|
+
LIMIT ?
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
The ISO 8601 cutoff is computed in JS and bound as a parameter (same lesson as v0.8.0's `countRecentContextUsages` fix — `datetime('now', ...)` returns a space-separated format that sorts wrong against the `T`-separated ISO 8601 timestamps written by `new Date().toISOString()`).
|
|
348
|
+
|
|
349
|
+
The self-match filter lives in SQL because pushing it into application code under a `LIMIT lookback + 1` under-fills the window when multiple duplicate rows of the current prompt share the session. Example: `[current, current, prior1, prior2]` with app-level filtering would return only 3 rows, drop both duplicates, and leave just `prior1` in the result — half the lookback budget wasted. With the SQL inequality, every returned row is a valid non-self prior by construction and the `LIMIT = lookback` exactly matches the budget.
|
|
350
|
+
|
|
351
|
+
Fallback paths: missing `sessionId`, empty current prompt, missing `query_text` column on a pre-migration schema (SELECT throws → caught), and any other DB error all return the current prompt unchanged. The function never throws.
|
|
352
|
+
|
|
353
|
+
### Current-first truncation
|
|
354
|
+
|
|
355
|
+
The combined query format is:
|
|
356
|
+
|
|
357
|
+
```
|
|
358
|
+
<current prompt>
|
|
359
|
+
|
|
360
|
+
<newest prior>
|
|
361
|
+
|
|
362
|
+
<older prior>
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
(blank lines between segments). If the assembled string exceeds `maxChars` (default 2000), the algorithm drops older priors one at a time rather than truncating the head. The current prompt is **always** present verbatim in the result so the user's actual question anchors the retrieval. Only when the current prompt alone already exceeds `maxChars` (rare, because the handler enforces `MAX_QUERY_LENGTH` earlier) does the function return the truncated current prompt with priors omitted entirely.
|
|
366
|
+
|
|
367
|
+
### Which retrieval stages use the combined query
|
|
368
|
+
|
|
369
|
+
| Stage | Query used | Rationale |
|
|
370
|
+
|---|---|---|
|
|
371
|
+
| `searchVec` | combined | Discovery — inherit prior-turn vocabulary |
|
|
372
|
+
| `searchFTS` (fast path) | combined | Discovery |
|
|
373
|
+
| `searchFTS` (expanded variants) | combined | Discovery — expansion already reads from the combined query |
|
|
374
|
+
| `expandQuery` | combined | The LLM expansion benefits from context |
|
|
375
|
+
| File-path FTS supplements | raw current | File names live in the current prompt only; priors would pollute this channel |
|
|
376
|
+
| Cross-encoder rerank | **raw current** | Rerank asks "how well does this doc match the user's current question" — diluting with older turns blurs the final ordering |
|
|
377
|
+
| Composite scoring | raw current | Recency intent / content-type weighting is per-current-turn |
|
|
378
|
+
| Snippet extraction | raw current | Highlighting should point at the user's current question |
|
|
379
|
+
| Routing hint detection | raw current | "why did we decide X" is about the current turn's intent |
|
|
380
|
+
| `hashQuery` for recall attribution | raw current | Each event should attribute to the specific turn that surfaced it |
|
|
381
|
+
| `wasPromptSeenRecently` dedupe | raw current | Dedupe key is about the actual submitted text |
|
|
382
|
+
| `isHeartbeatPrompt` check | raw current | Heartbeat detection runs on the raw input |
|
|
383
|
+
|
|
384
|
+
This split keeps multi-turn lookback a discovery-only enhancement and ensures every downstream signal that depends on "what did the user actually type right now" continues to see exactly that.
|
|
385
|
+
|
|
386
|
+
## Retrieval tiers
|
|
387
|
+
|
|
388
|
+
| Tier | Mechanism | Agent effort | Coverage |
|
|
389
|
+
|------|-----------|-------------|----------|
|
|
390
|
+
| Tier 1 | Infrastructure (watcher + embed timer) | None | Keeps vault fresh |
|
|
391
|
+
| Tier 2 | Hooks (automatic) | None | ~90% of retrieval |
|
|
392
|
+
| Tier 3 | MCP tools (agent-initiated) | 1 tool call | ~10% — escalation only |
|
|
393
|
+
|
|
394
|
+
See [Hooks vs MCP](hooks-vs-mcp.md) for details.
|
|
395
|
+
|
|
396
|
+
## Recall tracking
|
|
397
|
+
|
|
398
|
+
ClawMem tracks which documents are surfaced by retrieval, which queries surfaced them, and whether the assistant actually cited them. This data feeds lifecycle decisions (pin/snooze candidates) and provides empirical signals beyond raw search relevance.
|
|
399
|
+
|
|
400
|
+
The `recall_events` table is an append-only log. Each time context-surfacing injects documents, it writes one event per injected doc with the query hash, search score, session ID, and turn index. The `feedback-loop` hook later marks which events were actually referenced in the assistant's response, using per-turn transcript segmentation to attribute references to specific turns rather than the session globally.
|
|
401
|
+
|
|
402
|
+
The `recall_stats` table is a derived summary recomputed by the consolidation worker. It tracks per-document:
|
|
403
|
+
|
|
404
|
+
| Signal | Description |
|
|
405
|
+
|--------|-------------|
|
|
406
|
+
| `recall_count` | Total times surfaced |
|
|
407
|
+
| `unique_queries` | Distinct query contexts (cross-domain generality) |
|
|
408
|
+
| `recall_days` | Distinct calendar days surfaced (spaced vs binge frequency) |
|
|
409
|
+
| `diversity_score` | `min(1, max(unique_queries, recall_days) / 5)` |
|
|
410
|
+
| `spacing_score` | Multi-day spread: log-scaled day count + calendar span |
|
|
411
|
+
| `negative_count` | Surfaced but not referenced (noise signal) |
|
|
412
|
+
|
|
413
|
+
`lifecycle_status` uses these signals to surface pin candidates (high diversity + spacing + recall count) and snooze candidates (high recall count with mostly negative signals).
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Composite Scoring
|
|
2
|
+
|
|
3
|
+
Every search result in ClawMem is scored using a composite formula that blends multiple signals beyond raw search relevance.
|
|
4
|
+
|
|
5
|
+
## Formula
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
compositeScore = (w_search * searchScore + w_recency * recencyScore + w_confidence * confidenceScore)
|
|
9
|
+
* qualityMultiplier * coActivationBoost
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### Weights
|
|
13
|
+
|
|
14
|
+
| Signal | Normal (default) | `query` tool | Recency intent |
|
|
15
|
+
|--------|------------------|--------------|----------------|
|
|
16
|
+
| searchScore | 0.50 | 0.70 | 0.10 |
|
|
17
|
+
| recencyScore | 0.25 | 0.15 | 0.70 |
|
|
18
|
+
| confidenceScore | 0.25 | 0.15 | 0.20 |
|
|
19
|
+
|
|
20
|
+
Recency intent is detected automatically when queries contain "latest", "recent", "last session", etc. — and **takes precedence over the `query`-tool weights**: a recency-phrased `query` call still uses the Recency-intent column.
|
|
21
|
+
|
|
22
|
+
The **`query` tool** uses retrieval-tuned weights (search 0.70) derived from a held-out judged-relevance eval (v0.13.0) — more weight on topical relevance measurably improves graded NDCG@10 without demoting the newest-correct version of evolving docs. All other tools (`search`, `vsearch`, `memory_retrieve`, and the context-surfacing hook) use the **Normal** column.
|
|
23
|
+
|
|
24
|
+
## Signal breakdown
|
|
25
|
+
|
|
26
|
+
### Search score (0.0 - 1.0)
|
|
27
|
+
|
|
28
|
+
Raw relevance from the search backend — BM25, vector cosine similarity, or RRF-fused hybrid score.
|
|
29
|
+
|
|
30
|
+
### Recency score (0.0 - 1.0)
|
|
31
|
+
|
|
32
|
+
Exponential decay based on document age and content type half-life:
|
|
33
|
+
|
|
34
|
+
| Content type | Half-life | Behavior |
|
|
35
|
+
|-------------|-----------|----------|
|
|
36
|
+
| decision, deductive, preference, hub | Infinite | Never decay |
|
|
37
|
+
| antipattern | Infinite | Never decay |
|
|
38
|
+
| project | 120 days | Slow decay |
|
|
39
|
+
| research | 90 days | Moderate decay |
|
|
40
|
+
| problem, milestone, note | 60 days | Default |
|
|
41
|
+
| conversation, progress | 45 days | Faster decay |
|
|
42
|
+
| handoff | 30 days | Fast decay — recent matters most |
|
|
43
|
+
|
|
44
|
+
Half-lives extend up to 3x for frequently-accessed memories (access reinforcement decays over 90 days).
|
|
45
|
+
|
|
46
|
+
### Confidence score (0.0 - 1.0)
|
|
47
|
+
|
|
48
|
+
Starts at 0.5 for new documents. Adjusted by:
|
|
49
|
+
|
|
50
|
+
- **Contradiction detection** — when `decision-extractor` finds a new decision contradicting an old one, the old decision's confidence is lowered. The consolidation worker applies an additional merge-time contradiction gate (v0.7.1): before merging a new pattern into an existing consolidated observation, it checks for contradictions via a deterministic heuristic plus an LLM confirmation. Contradictory merges are blocked and either linked via a `contradicts` edge (default) or supersede the old row with `status='inactive'` (see [consolidation safety](architecture.md#consolidation-safety-v071)).
|
|
51
|
+
- **Feedback loop** — referenced notes get confidence boosts
|
|
52
|
+
- **Attention decay** — non-durable types (handoff, progress, conversation, note, project) lose 5% confidence per week without access. Decision, deductive, preference, hub, research, and antipattern types are exempt.
|
|
53
|
+
|
|
54
|
+
### Quality multiplier (0.7 - 1.3)
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
qualityMultiplier = 0.7 + 0.6 * qualityScore
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Quality score (0.0 - 1.0) is computed during indexing based on:
|
|
61
|
+
- Document length
|
|
62
|
+
- Structural elements (headings, lists)
|
|
63
|
+
- Decision keywords
|
|
64
|
+
- Correction keywords
|
|
65
|
+
- Frontmatter richness
|
|
66
|
+
|
|
67
|
+
A document with headings, structured lists, decision-related terms ("decided", "chose", "tradeoff"), and YAML frontmatter will score near the top of the range. A single paragraph with no structure scores near the bottom and gets penalized to 0.7x in every search result.
|
|
68
|
+
|
|
69
|
+
### Writing documents that score well
|
|
70
|
+
|
|
71
|
+
Structure your notes the way you'd want to read them later. Use headings to separate topics — each heading creates a separate fragment that can be embedded and retrieved independently. Bullet lists score better than run-on paragraphs. If a document records a decision, say so explicitly ("We decided X because Y") rather than burying it in narrative.
|
|
72
|
+
|
|
73
|
+
Frontmatter adds 0.2 to the quality score. Even minimal frontmatter helps:
|
|
74
|
+
|
|
75
|
+
```yaml
|
|
76
|
+
---
|
|
77
|
+
title: Switch from REST to gRPC for internal services
|
|
78
|
+
type: decision
|
|
79
|
+
date: 2026-03-15
|
|
80
|
+
---
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This is not about gaming the scoring system. Documents that score well are also documents that agents (and humans) can actually use — they have structure, they state their purpose, and they're findable by the terms you'd naturally search for.
|
|
84
|
+
|
|
85
|
+
### Co-activation boost (1.0 - 1.15)
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
coActivationBoost = 1 + min(coCount / 10, 0.15)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Documents frequently surfaced together in the same session get up to 15% boost.
|
|
92
|
+
|
|
93
|
+
**Where co-activation is applied depends on the caller.** MCP tools (`query`, `search`, `vsearch`) pass a co-activation function into `applyCompositeScoring()`, so the boost is part of the composite score used for ranking and `minScore` filtering. The context-surfacing hook does NOT pass co-activation into composite scoring — instead it applies a separate spreading-activation step *after* adaptive threshold filtering. This means the hook's threshold decisions are based on scores without co-activation, and co-activation only boosts results that already passed the threshold. This is intentional: it prevents relationship boosts from rescuing otherwise-weak results into the surfaced set.
|
|
94
|
+
|
|
95
|
+
## Additional modifiers
|
|
96
|
+
|
|
97
|
+
### Pin boost
|
|
98
|
+
|
|
99
|
+
Pinned documents receive +0.3 additive boost (capped at 1.0 total).
|
|
100
|
+
|
|
101
|
+
### Length normalization
|
|
102
|
+
|
|
103
|
+
```
|
|
104
|
+
lengthPenalty = 1 / (1 + 0.5 * log2(max(bodyLength / 500, 1)))
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Penalizes verbose entries. Floor at 30% of original score.
|
|
108
|
+
|
|
109
|
+
### Frequency boost
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
freqSignal = (revisions - 1) * 2 + (duplicates - 1)
|
|
113
|
+
freqBoost = min(0.10, log1p(freqSignal) * 0.03)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Revision count weighted 2x vs duplicate count. Capped at 10%.
|
|
117
|
+
|
|
118
|
+
### MMR diversity filter
|
|
119
|
+
|
|
120
|
+
After scoring, results pass through Maximal Marginal Relevance filtering. Documents with Jaccard bigram similarity > 0.6 to a higher-ranked result are demoted (not removed).
|
|
121
|
+
|
|
122
|
+
## Score distribution and adaptive thresholds
|
|
123
|
+
|
|
124
|
+
Absolute composite scores vary across vaults due to several factors:
|
|
125
|
+
|
|
126
|
+
- **Vault size** — smaller vaults produce higher BM25 scores per hit because IDF (inverse document frequency) is less diluted. A 30-doc vault may score 0.8 on a good match while a 3000-doc vault scores 0.4 for equal relevance.
|
|
127
|
+
- **Quality multiplier** — ranges 0.7x to 1.3x based on document structure. A vault of well-structured docs with headings, lists, and frontmatter gets systematically higher composite scores than a vault of flat text notes.
|
|
128
|
+
- **Embedding model** — zembed-1 (2560d) and EmbeddingGemma (768d) produce different cosine similarity distributions. Higher-dimensional embeddings tend toward lower absolute cosine scores but better relative ordering.
|
|
129
|
+
- **Content age** — recency accounts for 25% of composite score. A vault of recent docs (< 30 days old) has minimal decay. A vault of 6-month-old research notes gets significant recency penalties even on strong search relevance.
|
|
130
|
+
|
|
131
|
+
Context-surfacing handles this with adaptive ratio-based thresholds instead of fixed absolute values. The hook computes the best composite score in the result set, then keeps results within a percentage of that best score (e.g., 55% for balanced). An activation floor prevents surfacing when even the best result is too weak. See [profiles](hooks-vs-mcp.md#adaptive-thresholds) for the specific values per profile.
|
|
132
|
+
|
|
133
|
+
MCP tools use fixed absolute `minScore` thresholds since agents control those limits directly.
|