moflo 4.8.38 → 4.8.40
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/.claude/guidance/shipped/memory-strategy.md +276 -204
- package/.claude/guidance/shipped/memorydb-maintenance.md +111 -0
- package/.claude/scripts/session-start-launcher.mjs +28 -24
- package/bin/session-start-launcher.mjs +28 -24
- package/package.json +2 -2
- package/src/@claude-flow/cli/dist/src/version.js +1 -1
- package/src/@claude-flow/cli/package.json +1 -1
- package/.claude/guidance/shipped/guidance-memory-strategy.md +0 -262
|
@@ -1,204 +1,276 @@
|
|
|
1
|
-
# Memory & Semantic Search Strategy
|
|
2
|
-
|
|
3
|
-
**Purpose:** How memory, embeddings, and semantic search work in moflo. Reference when
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
---
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
**
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
|
138
|
-
|
|
139
|
-
| `
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
|
162
|
-
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
1
|
+
# Memory & Semantic Search Strategy
|
|
2
|
+
|
|
3
|
+
**Purpose:** How memory, embeddings, and semantic search work in moflo. Reference when writing guidance documents, debugging search quality, configuring memory for a consumer project, or extending the system.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Problem Statement
|
|
8
|
+
|
|
9
|
+
Claude Code agents need project-specific knowledge — coding rules, architecture patterns, entity templates, testing conventions — delivered at the right moment. Without a retrieval system, agents either miss critical rules or require massive CLAUDE.md files that waste context window tokens.
|
|
10
|
+
|
|
11
|
+
**Goals:**
|
|
12
|
+
- Agents find relevant guidance automatically via semantic search
|
|
13
|
+
- Subagents spawned by the coordinator inherit memory access
|
|
14
|
+
- Search quality is high enough that agents don't need to read whole files
|
|
15
|
+
- The system survives `npm install` (indexing runs on session start)
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Architecture Overview
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
Source Files (.claude/guidance/*.md, docs/*.md)
|
|
23
|
+
|
|
|
24
|
+
v
|
|
25
|
+
index-guidance.mjs --- Chunk on ## headers, build RAG links
|
|
26
|
+
| (prev/next, siblings, parent/child, context overlap)
|
|
27
|
+
v
|
|
28
|
+
.swarm/memory.db ----- SQLite (entries + metadata + embedding vectors)
|
|
29
|
+
|
|
|
30
|
+
v
|
|
31
|
+
build-embeddings.mjs - Generate 384-dim vectors per entry
|
|
32
|
+
| (Xenova/all-MiniLM-L6-v2 neural, or domain-aware hash fallback)
|
|
33
|
+
v
|
|
34
|
+
HNSW index ----------- Approximate nearest-neighbor search
|
|
35
|
+
v
|
|
36
|
+
Search layer ---------- Three access paths:
|
|
37
|
+
1. MCP tools (mcp__moflo__memory_search) -- preferred
|
|
38
|
+
2. CLI (npx flo memory search) -- fallback
|
|
39
|
+
3. Script (semantic-search.mjs) -- detailed output
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**Key files:**
|
|
43
|
+
|
|
44
|
+
| File | Purpose |
|
|
45
|
+
|------|---------|
|
|
46
|
+
| `.swarm/memory.db` | SQLite database with all entries, embeddings, metadata |
|
|
47
|
+
| `.swarm/code-map-hash.txt` | SHA-256 hash for incremental code map skip |
|
|
48
|
+
| `.claude-flow/neural/patterns.json` | ReasoningBank learned patterns |
|
|
49
|
+
| `bin/build-embeddings.mjs` | Generates 384-dim embeddings |
|
|
50
|
+
| `bin/index-guidance.mjs` | Indexes guidance files with RAG linking |
|
|
51
|
+
| `bin/generate-code-map.mjs` | Generates structural code map (projects, dirs, types, interfaces) |
|
|
52
|
+
| `bin/index-patterns.mjs` | Extracts per-file code patterns |
|
|
53
|
+
| `bin/index-tests.mjs` | Indexes test structure and patterns |
|
|
54
|
+
| `bin/index-all.mjs` | Runs the full indexing chain sequentially |
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Namespaces
|
|
59
|
+
|
|
60
|
+
| Namespace | Content | Indexed By |
|
|
61
|
+
|-----------|---------|------------|
|
|
62
|
+
| `guidance` | Indexed guidance and docs | `index-guidance.mjs` |
|
|
63
|
+
| `code-map` | Structural codebase index (projects, directories, types, interfaces) | `generate-code-map.mjs` |
|
|
64
|
+
| `patterns` | Per-file code patterns (services, routes, error handling, exports) | `index-patterns.mjs` |
|
|
65
|
+
| `tests` | Test structure and patterns | `index-tests.mjs` |
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Guidance Document Optimization Rules
|
|
70
|
+
|
|
71
|
+
These rules determine how well your guidance documents retrieve via semantic search:
|
|
72
|
+
|
|
73
|
+
### 1. Every file needs a Purpose line
|
|
74
|
+
|
|
75
|
+
Add `**Purpose:**` as the first meaningful line after the title. Claude checks this first for relevance scoring. Without it, the chunk has no summary signal.
|
|
76
|
+
|
|
77
|
+
### 2. H2 headings are the primary retrieval signal
|
|
78
|
+
|
|
79
|
+
The indexer splits on `##`. Each heading becomes the chunk title, prepended to searchable content. Domain-specific keywords in headings dramatically improve recall.
|
|
80
|
+
|
|
81
|
+
**Bad:** `## Overview`, `## Rules`, `## Pattern`
|
|
82
|
+
**Good:** `## Soft Delete Rules`, `## JWT Authentication Pattern`, `## Database Entity Migration`
|
|
83
|
+
|
|
84
|
+
### 3. Ideal chunk size: 1000-4000 characters
|
|
85
|
+
|
|
86
|
+
Below 50 chars the chunk is dropped. Above 6000 the indexer force-splits on paragraphs, which breaks mid-thought. The sweet spot produces focused embeddings.
|
|
87
|
+
|
|
88
|
+
### 4. Self-contained chunks
|
|
89
|
+
|
|
90
|
+
Each H2 section must answer a question without needing the rest of the document. Include: the rule, a code example, and a cross-reference.
|
|
91
|
+
|
|
92
|
+
### 5. Tables over prose
|
|
93
|
+
|
|
94
|
+
Claude parses structured data more accurately than paragraphs. DO/DON'T tables, field reference tables, and command tables all retrieve better.
|
|
95
|
+
|
|
96
|
+
### 6. Cross-references create a navigation graph
|
|
97
|
+
|
|
98
|
+
The RAG indexer stores `prevChunk`/`nextChunk`/`siblings` metadata. Cross-references between documents let Claude follow chains: `core.md -> coding-rules.md -> database.md`.
|
|
99
|
+
|
|
100
|
+
### 7. No decorative formatting
|
|
101
|
+
|
|
102
|
+
ASCII boxes, excessive emoji, rhetorical questions, and motivational text all waste tokens without improving retrieval or comprehension.
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Embedding Strategy
|
|
107
|
+
|
|
108
|
+
### Embedding Models
|
|
109
|
+
|
|
110
|
+
| Model | Quality | Speed | When Used |
|
|
111
|
+
|-------|---------|-------|-----------|
|
|
112
|
+
| `Xenova/all-MiniLM-L6-v2` | High (true semantic) | ~3s for 1000 entries | Primary — `build-embeddings.mjs` uses this |
|
|
113
|
+
| `domain-aware-hash-v1` | Good (domain clustering) | <1s for 1000 entries | Fallback when Transformers.js unavailable |
|
|
114
|
+
|
|
115
|
+
**Neural embeddings (Xenova/all-MiniLM-L6-v2):**
|
|
116
|
+
- Uses `@xenova/transformers` with ONNX WASM runtime
|
|
117
|
+
- 384-dimensional vectors, L2-normalized
|
|
118
|
+
- True semantic understanding — "soft delete" matches "mark as deleted" without keyword overlap
|
|
119
|
+
- Loaded lazily on first use, cached for subsequent queries
|
|
120
|
+
|
|
121
|
+
**Domain-aware hash embeddings (fallback):**
|
|
122
|
+
- Custom SimHash-style algorithm with 12 domain clusters
|
|
123
|
+
- Multi-position hashing with bigram/trigram features
|
|
124
|
+
- Good at keyword-level matching but misses semantic paraphrases
|
|
125
|
+
- No external dependencies — always available
|
|
126
|
+
|
|
127
|
+
### The Embedding Alignment Problem
|
|
128
|
+
|
|
129
|
+
**Critical rule:** Query embeddings MUST match stored embeddings. Computing cosine similarity between vectors from different models produces meaningless scores.
|
|
130
|
+
|
|
131
|
+
Both the search scripts and the MCP memory tools auto-detect the stored embedding model and generate matching query vectors. Search also filters out entries with mismatched `embedding_model`.
|
|
132
|
+
|
|
133
|
+
### Domain Cluster Tuning
|
|
134
|
+
|
|
135
|
+
The hash fallback's domain clusters can be extended with project-specific terms:
|
|
136
|
+
|
|
137
|
+
| Cluster | Example Terms |
|
|
138
|
+
|---------|--------------|
|
|
139
|
+
| `database` | your ORM, database engine, schema terms |
|
|
140
|
+
| `frontend` | UI framework, component library terms |
|
|
141
|
+
| `backend` | DI container, API framework terms |
|
|
142
|
+
| `testing` | test framework, assertion library terms |
|
|
143
|
+
| `security` | auth system, permission model terms |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## RAG Indexing Pipeline
|
|
148
|
+
|
|
149
|
+
### How `index-guidance.mjs` Works
|
|
150
|
+
|
|
151
|
+
1. **Scan** configured directories for `.md` files
|
|
152
|
+
2. **Hash check** — Skip files whose content hash hasn't changed (unless `--force`)
|
|
153
|
+
3. **Store full document** as `doc-{prefix}-{name}` (for complete retrieval)
|
|
154
|
+
4. **Chunk on `##` headers** — Each H2 section becomes a separate entry
|
|
155
|
+
5. **H3 subsections** become child chunks with parent H2 as context prefix
|
|
156
|
+
6. **Force-split** sections over 4000 chars on paragraph boundaries
|
|
157
|
+
7. **Build RAG metadata** for every chunk:
|
|
158
|
+
|
|
159
|
+
| Metadata Field | Purpose |
|
|
160
|
+
|---------------|---------|
|
|
161
|
+
| `parentDoc` | Link back to full document |
|
|
162
|
+
| `prevChunk` / `nextChunk` | Sequential navigation |
|
|
163
|
+
| `siblings` | All chunk keys from same document |
|
|
164
|
+
| `hierarchicalParent` / `hierarchicalChildren` | H2->H3 relationships |
|
|
165
|
+
| `contextBefore` / `contextAfter` | 20% overlapping text from adjacent chunks |
|
|
166
|
+
|
|
167
|
+
8. **Prepend context** — Each chunk's searchable content includes overlap from neighbors
|
|
168
|
+
9. **Stale cleanup** — After indexing, remove entries for files that no longer exist on disk
|
|
169
|
+
10. **Background embedding** — Spawn `build-embeddings.mjs` to generate vectors
|
|
170
|
+
|
|
171
|
+
### Configuring Indexed Directories
|
|
172
|
+
|
|
173
|
+
In `moflo.yaml`:
|
|
174
|
+
|
|
175
|
+
```yaml
|
|
176
|
+
guidance:
|
|
177
|
+
directories:
|
|
178
|
+
- .claude/guidance
|
|
179
|
+
- docs/guides
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Default directories (when no config): `.claude/guidance`, `docs/guides`
|
|
183
|
+
|
|
184
|
+
Moflo also automatically indexes its own bundled guidance from `node_modules/moflo/.claude/guidance/` when installed as a dependency.
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## Search Commands
|
|
189
|
+
|
|
190
|
+
All methods auto-detect the stored embedding model and generate matching query vectors:
|
|
191
|
+
|
|
192
|
+
**MCP (Preferred):** `mcp__moflo__memory_search` — `query: "your query", namespace: "guidance"`
|
|
193
|
+
|
|
194
|
+
**CLI (Fallback):**
|
|
195
|
+
```bash
|
|
196
|
+
npx flo memory search --query "your query" --namespace guidance
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Search options:**
|
|
200
|
+
|
|
201
|
+
| Flag | Default | Purpose |
|
|
202
|
+
|------|---------|---------|
|
|
203
|
+
| `--namespace` | all | Filter to specific namespace |
|
|
204
|
+
| `--limit` | 5 | Number of results |
|
|
205
|
+
| `--threshold` | 0.3 | Minimum similarity score |
|
|
206
|
+
| `--json` | false | Output as JSON |
|
|
207
|
+
|
|
208
|
+
### Code Map Search (for codebase navigation)
|
|
209
|
+
|
|
210
|
+
When you need to find where a type, service, entity, or component lives — search `code-map` BEFORE using Glob/Grep:
|
|
211
|
+
|
|
212
|
+
**MCP:** `mcp__moflo__memory_search` — `query: "payment service", namespace: "code-map"`
|
|
213
|
+
|
|
214
|
+
**What code-map contains:**
|
|
215
|
+
|
|
216
|
+
| Chunk prefix | What it answers |
|
|
217
|
+
|--------------|-----------------|
|
|
218
|
+
| `project:` | "What's in the api project?" |
|
|
219
|
+
| `dir:` | "What types are in the entities directory?" |
|
|
220
|
+
| `iface-map:` | "What implements IPaymentService?" |
|
|
221
|
+
| `type-index:` | "Where is Service defined?" |
|
|
222
|
+
|
|
223
|
+
---
|
|
224
|
+
|
|
225
|
+
## Session Start Indexing
|
|
226
|
+
|
|
227
|
+
On every session start, `hooks.mjs` spawns `index-all.mjs` which runs the full chain:
|
|
228
|
+
|
|
229
|
+
| Indexer | Namespace | What it does |
|
|
230
|
+
|---------|-----------|--------------|
|
|
231
|
+
| `index-guidance.mjs` | `guidance` | Chunks markdown, builds RAG links |
|
|
232
|
+
| `generate-code-map.mjs` | `code-map` | Scans source for types, interfaces, directories |
|
|
233
|
+
| `index-tests.mjs` | `tests` | Indexes test structure |
|
|
234
|
+
| `index-patterns.mjs` | `patterns` | Extracts per-file code patterns |
|
|
235
|
+
| `build-embeddings.mjs` | all | Generates vectors for any unembedded entries |
|
|
236
|
+
|
|
237
|
+
Indexing is incremental by default — files whose content hash hasn't changed are skipped. Use `--force` to reindex everything.
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## Replication Guide
|
|
242
|
+
|
|
243
|
+
To set up this system in a new project:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
npm install moflo
|
|
247
|
+
npx flo init
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Create `.claude/guidance/` with markdown files following the optimization rules above, then:
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
npx flo-index --force # Index documents
|
|
254
|
+
npx flo memory search --query "your domain query" --namespace guidance # Verify
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Troubleshooting
|
|
260
|
+
|
|
261
|
+
| Symptom | Cause | Fix |
|
|
262
|
+
|---------|-------|-----|
|
|
263
|
+
| Search returns irrelevant results | Query/stored embedding model mismatch | Auto-detected now; verify with `--verbose` flag |
|
|
264
|
+
| Low similarity scores | Query doesn't match domain terms | Include domain keywords in query |
|
|
265
|
+
| "Vector: No" in list | Entry lacks embedding | Run `node bin/build-embeddings.mjs` |
|
|
266
|
+
| Entries not found after adding file | Indexer hasn't run yet | Run `node bin/index-all.mjs` or restart session |
|
|
267
|
+
| Bundled moflo guidance not indexed | Not installed as dependency | Only indexes when `node_modules/moflo/.claude/guidance/` exists |
|
|
268
|
+
| Empty namespace | Indexer never ran or DB was purged | See `memorydb-maintenance.md` for reindex/purge procedures |
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## See Also
|
|
273
|
+
|
|
274
|
+
- `memorydb-maintenance.md` — Database location, schema, purge/reindex procedures
|
|
275
|
+
- `agent-bootstrap.md` — Subagent bootstrap guide
|
|
276
|
+
- `moflo.md` — Full CLI/MCP reference
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Memory Database Maintenance
|
|
2
|
+
|
|
3
|
+
## Database Location
|
|
4
|
+
|
|
5
|
+
- **Path:** `.swarm/memory.db`
|
|
6
|
+
- **Engine:** sql.js (WASM-based SQLite — no native binaries needed)
|
|
7
|
+
- **Single table:** `memory_entries` — stores content, embeddings, and metadata in one table
|
|
8
|
+
|
|
9
|
+
## Schema
|
|
10
|
+
|
|
11
|
+
```sql
|
|
12
|
+
CREATE TABLE memory_entries (
|
|
13
|
+
id TEXT PRIMARY KEY,
|
|
14
|
+
key TEXT NOT NULL,
|
|
15
|
+
namespace TEXT DEFAULT 'default',
|
|
16
|
+
content TEXT NOT NULL,
|
|
17
|
+
type TEXT DEFAULT 'semantic',
|
|
18
|
+
embedding TEXT, -- inline vector (no separate embeddings table)
|
|
19
|
+
embedding_model TEXT DEFAULT 'local',
|
|
20
|
+
embedding_dimensions INTEGER,
|
|
21
|
+
tags TEXT,
|
|
22
|
+
metadata TEXT,
|
|
23
|
+
owner_id TEXT,
|
|
24
|
+
created_at INTEGER,
|
|
25
|
+
updated_at INTEGER,
|
|
26
|
+
expires_at INTEGER,
|
|
27
|
+
last_accessed_at INTEGER,
|
|
28
|
+
access_count INTEGER DEFAULT 0,
|
|
29
|
+
status TEXT DEFAULT 'active',
|
|
30
|
+
UNIQUE(namespace, key)
|
|
31
|
+
);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Namespaces
|
|
35
|
+
|
|
36
|
+
| Namespace | Indexed By | Key Patterns | Purpose |
|
|
37
|
+
|-----------|-----------|--------------|---------|
|
|
38
|
+
| `code-map` | `generate-code-map.mjs` | `file:{path}`, `dir:{path}`, `type-index:{n}` | Codebase navigation |
|
|
39
|
+
| `guidance` | `index-guidance.mjs` | `chunk-guidance-*` | Governance docs, shipped guidance |
|
|
40
|
+
| `patterns` | `index-patterns.mjs` | `pattern:file:{path}`, `pattern:service:{name}`, `pattern:route:{path}`, `pattern:error:{path}` | Per-file code patterns |
|
|
41
|
+
| `tests` | `index-tests.mjs` | test-related keys | Test structure and patterns |
|
|
42
|
+
|
|
43
|
+
## MCP Tools
|
|
44
|
+
|
|
45
|
+
| Tool | Use |
|
|
46
|
+
|------|-----|
|
|
47
|
+
| `mcp__moflo__memory_search` | Semantic search (HNSW-accelerated) |
|
|
48
|
+
| `mcp__moflo__memory_store` | Store/upsert entries |
|
|
49
|
+
| `mcp__moflo__memory_delete` | Delete single entry by key |
|
|
50
|
+
| `mcp__moflo__memory_stats` | Namespace counts and HNSW index status |
|
|
51
|
+
| `mcp__moflo__memory_list` | List keys in a namespace |
|
|
52
|
+
|
|
53
|
+
## Reindex Commands
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
# Full reindex (guidance → code-map → tests → patterns → pretrain → HNSW rebuild)
|
|
57
|
+
node bin/index-all.mjs
|
|
58
|
+
|
|
59
|
+
# Individual indexers
|
|
60
|
+
node bin/index-guidance.mjs
|
|
61
|
+
node bin/generate-code-map.mjs
|
|
62
|
+
node bin/index-tests.mjs
|
|
63
|
+
node bin/index-patterns.mjs # --force for full reindex, --stats for counts
|
|
64
|
+
|
|
65
|
+
# Rebuild embeddings (vectorize all entries)
|
|
66
|
+
node bin/build-embeddings.mjs
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Purging a Namespace
|
|
70
|
+
|
|
71
|
+
Use sql.js directly (no sqlite3 binary on this machine):
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
node --input-type=module -e "
|
|
75
|
+
import initSqlJs from 'sql.js';
|
|
76
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
77
|
+
|
|
78
|
+
const SQL = await initSqlJs();
|
|
79
|
+
const buf = readFileSync('.swarm/memory.db');
|
|
80
|
+
const db = new SQL.Database(buf);
|
|
81
|
+
|
|
82
|
+
// Check counts before
|
|
83
|
+
const before = db.exec('SELECT namespace, COUNT(*) FROM memory_entries GROUP BY namespace');
|
|
84
|
+
console.log('BEFORE:', JSON.stringify(before[0]?.values));
|
|
85
|
+
|
|
86
|
+
// Purge a namespace (embeddings are inline — no separate table to clean)
|
|
87
|
+
db.run(\"DELETE FROM memory_entries WHERE namespace = 'code-map'\");
|
|
88
|
+
|
|
89
|
+
const after = db.exec('SELECT namespace, COUNT(*) FROM memory_entries GROUP BY namespace');
|
|
90
|
+
console.log('AFTER:', JSON.stringify(after[0]?.values));
|
|
91
|
+
|
|
92
|
+
writeFileSync('.swarm/memory.db', Buffer.from(db.export()));
|
|
93
|
+
db.close();
|
|
94
|
+
"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
After purging, reindex the namespace and rebuild embeddings:
|
|
98
|
+
```bash
|
|
99
|
+
node bin/generate-code-map.mjs # or whichever indexer owns the namespace
|
|
100
|
+
node bin/build-embeddings.mjs
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Auto-Reindex on Session Start
|
|
104
|
+
|
|
105
|
+
`hooks.mjs` spawns `index-all.mjs` on session start, which runs the full chain. This only works if `index-all.mjs` is in the `scriptFiles` array in `session-start-launcher.mjs` (see docs/BUILD.md § "Adding New Scripts").
|
|
106
|
+
|
|
107
|
+
## Troubleshooting
|
|
108
|
+
|
|
109
|
+
- **Empty search results:** Run `mcp__moflo__memory_stats` to check entry counts. If a namespace is empty, run its indexer.
|
|
110
|
+
- **Stale embeddings:** Run `node bin/build-embeddings.mjs` — it skips entries that already have embeddings unless content changed.
|
|
111
|
+
- **Full reset:** Delete `.swarm/memory.db` and run `node bin/index-all.mjs && node bin/build-embeddings.mjs`.
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { spawn } from 'child_process';
|
|
11
|
-
import { existsSync, readFileSync, copyFileSync, unlinkSync, readdirSync } from 'fs';
|
|
11
|
+
import { existsSync, readFileSync, writeFileSync, copyFileSync, unlinkSync, readdirSync, mkdirSync } from 'fs';
|
|
12
12
|
import { resolve, dirname } from 'path';
|
|
13
13
|
import { fileURLToPath } from 'url';
|
|
14
14
|
|
|
@@ -32,7 +32,6 @@ function fireAndForget(cmd, args, label) {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// ── 2. Reset workflow state for new session ──────────────────────────────────
|
|
35
|
-
import { writeFileSync, mkdirSync } from 'fs';
|
|
36
35
|
const stateDir = resolve(projectRoot, '.claude');
|
|
37
36
|
const stateFile = resolve(stateDir, 'workflow-state.json');
|
|
38
37
|
try {
|
|
@@ -112,7 +111,7 @@ try {
|
|
|
112
111
|
const scriptFiles = [
|
|
113
112
|
'hooks.mjs', 'session-start-launcher.mjs', 'index-guidance.mjs',
|
|
114
113
|
'build-embeddings.mjs', 'generate-code-map.mjs', 'semantic-search.mjs',
|
|
115
|
-
'index-tests.mjs',
|
|
114
|
+
'index-tests.mjs', 'index-all.mjs',
|
|
116
115
|
];
|
|
117
116
|
for (const file of scriptFiles) {
|
|
118
117
|
syncFile(resolve(binDir, file), resolve(scriptsDir, file), `.claude/scripts/${file}`);
|
|
@@ -164,19 +163,21 @@ try {
|
|
|
164
163
|
}
|
|
165
164
|
}
|
|
166
165
|
|
|
167
|
-
// Sync guidance
|
|
168
|
-
const shippedBootstrap = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped/agent-bootstrap.md');
|
|
169
|
-
const legacyBootstrap = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/agent-bootstrap.md');
|
|
170
|
-
const bootstrapSrc = existsSync(shippedBootstrap) ? shippedBootstrap : legacyBootstrap;
|
|
166
|
+
// Sync all shipped guidance files from node_modules/moflo/.claude/guidance/shipped/
|
|
171
167
|
const guidanceDir = resolve(projectRoot, '.claude/guidance');
|
|
172
|
-
const
|
|
173
|
-
if (existsSync(
|
|
168
|
+
const shippedDir = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped');
|
|
169
|
+
if (existsSync(shippedDir)) {
|
|
174
170
|
try {
|
|
175
171
|
if (!existsSync(guidanceDir)) mkdirSync(guidanceDir, { recursive: true });
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
172
|
+
const shippedFiles = readdirSync(shippedDir).filter(f => f.endsWith('.md'));
|
|
173
|
+
for (const file of shippedFiles) {
|
|
174
|
+
const src = resolve(shippedDir, file);
|
|
175
|
+
const dest = resolve(guidanceDir, `moflo-${file}`);
|
|
176
|
+
const header = `<!-- AUTO-GENERATED by moflo session-start. Do not edit — changes will be overwritten. -->\n<!-- Source: node_modules/moflo/.claude/guidance/shipped/${file} -->\n\n`;
|
|
177
|
+
const content = readFileSync(src, 'utf-8');
|
|
178
|
+
writeFileSync(dest, header + content);
|
|
179
|
+
currentManifest.push(`.claude/guidance/moflo-${file}`);
|
|
180
|
+
}
|
|
180
181
|
} catch { /* non-fatal */ }
|
|
181
182
|
}
|
|
182
183
|
|
|
@@ -206,19 +207,22 @@ try {
|
|
|
206
207
|
// Non-fatal — scripts will still work, just may be stale
|
|
207
208
|
}
|
|
208
209
|
|
|
209
|
-
// ── 3b. Ensure guidance
|
|
210
|
-
// Subagents need
|
|
210
|
+
// ── 3b. Ensure shipped guidance files exist (even without version change) ──
|
|
211
|
+
// Subagents need these files on disk for direct reads without memory search.
|
|
211
212
|
try {
|
|
212
|
-
const shippedBs = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped/agent-bootstrap.md');
|
|
213
|
-
const legacyBs = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/agent-bootstrap.md');
|
|
214
|
-
const bootstrapSrc = existsSync(shippedBs) ? shippedBs : legacyBs;
|
|
215
213
|
const guidanceDir = resolve(projectRoot, '.claude/guidance');
|
|
216
|
-
const
|
|
217
|
-
if (existsSync(
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
214
|
+
const shippedDir = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped');
|
|
215
|
+
if (existsSync(shippedDir)) {
|
|
216
|
+
const shippedFiles = readdirSync(shippedDir).filter(f => f.endsWith('.md'));
|
|
217
|
+
for (const file of shippedFiles) {
|
|
218
|
+
const dest = resolve(guidanceDir, `moflo-${file}`);
|
|
219
|
+
if (!existsSync(dest)) {
|
|
220
|
+
if (!existsSync(guidanceDir)) mkdirSync(guidanceDir, { recursive: true });
|
|
221
|
+
const header = `<!-- AUTO-GENERATED by moflo session-start. Do not edit — changes will be overwritten. -->\n<!-- Source: node_modules/moflo/.claude/guidance/shipped/${file} -->\n\n`;
|
|
222
|
+
const content = readFileSync(resolve(shippedDir, file), 'utf-8');
|
|
223
|
+
writeFileSync(dest, header + content);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
222
226
|
}
|
|
223
227
|
} catch { /* non-fatal */ }
|
|
224
228
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { spawn } from 'child_process';
|
|
11
|
-
import { existsSync, readFileSync, copyFileSync, unlinkSync, readdirSync } from 'fs';
|
|
11
|
+
import { existsSync, readFileSync, writeFileSync, copyFileSync, unlinkSync, readdirSync, mkdirSync } from 'fs';
|
|
12
12
|
import { resolve, dirname } from 'path';
|
|
13
13
|
import { fileURLToPath } from 'url';
|
|
14
14
|
|
|
@@ -32,7 +32,6 @@ function fireAndForget(cmd, args, label) {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// ── 2. Reset workflow state for new session ──────────────────────────────────
|
|
35
|
-
import { writeFileSync, mkdirSync } from 'fs';
|
|
36
35
|
const stateDir = resolve(projectRoot, '.claude');
|
|
37
36
|
const stateFile = resolve(stateDir, 'workflow-state.json');
|
|
38
37
|
try {
|
|
@@ -112,7 +111,7 @@ try {
|
|
|
112
111
|
const scriptFiles = [
|
|
113
112
|
'hooks.mjs', 'session-start-launcher.mjs', 'index-guidance.mjs',
|
|
114
113
|
'build-embeddings.mjs', 'generate-code-map.mjs', 'semantic-search.mjs',
|
|
115
|
-
'index-tests.mjs',
|
|
114
|
+
'index-tests.mjs', 'index-all.mjs',
|
|
116
115
|
];
|
|
117
116
|
for (const file of scriptFiles) {
|
|
118
117
|
syncFile(resolve(binDir, file), resolve(scriptsDir, file), `.claude/scripts/${file}`);
|
|
@@ -164,19 +163,21 @@ try {
|
|
|
164
163
|
}
|
|
165
164
|
}
|
|
166
165
|
|
|
167
|
-
// Sync guidance
|
|
168
|
-
const shippedBootstrap = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped/agent-bootstrap.md');
|
|
169
|
-
const legacyBootstrap = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/agent-bootstrap.md');
|
|
170
|
-
const bootstrapSrc = existsSync(shippedBootstrap) ? shippedBootstrap : legacyBootstrap;
|
|
166
|
+
// Sync all shipped guidance files from node_modules/moflo/.claude/guidance/shipped/
|
|
171
167
|
const guidanceDir = resolve(projectRoot, '.claude/guidance');
|
|
172
|
-
const
|
|
173
|
-
if (existsSync(
|
|
168
|
+
const shippedDir = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped');
|
|
169
|
+
if (existsSync(shippedDir)) {
|
|
174
170
|
try {
|
|
175
171
|
if (!existsSync(guidanceDir)) mkdirSync(guidanceDir, { recursive: true });
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
172
|
+
const shippedFiles = readdirSync(shippedDir).filter(f => f.endsWith('.md'));
|
|
173
|
+
for (const file of shippedFiles) {
|
|
174
|
+
const src = resolve(shippedDir, file);
|
|
175
|
+
const dest = resolve(guidanceDir, `moflo-${file}`);
|
|
176
|
+
const header = `<!-- AUTO-GENERATED by moflo session-start. Do not edit — changes will be overwritten. -->\n<!-- Source: node_modules/moflo/.claude/guidance/shipped/${file} -->\n\n`;
|
|
177
|
+
const content = readFileSync(src, 'utf-8');
|
|
178
|
+
writeFileSync(dest, header + content);
|
|
179
|
+
currentManifest.push(`.claude/guidance/moflo-${file}`);
|
|
180
|
+
}
|
|
180
181
|
} catch { /* non-fatal */ }
|
|
181
182
|
}
|
|
182
183
|
|
|
@@ -206,19 +207,22 @@ try {
|
|
|
206
207
|
// Non-fatal — scripts will still work, just may be stale
|
|
207
208
|
}
|
|
208
209
|
|
|
209
|
-
// ── 3b. Ensure guidance
|
|
210
|
-
// Subagents need
|
|
210
|
+
// ── 3b. Ensure shipped guidance files exist (even without version change) ──
|
|
211
|
+
// Subagents need these files on disk for direct reads without memory search.
|
|
211
212
|
try {
|
|
212
|
-
const shippedBs = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped/agent-bootstrap.md');
|
|
213
|
-
const legacyBs = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/agent-bootstrap.md');
|
|
214
|
-
const bootstrapSrc = existsSync(shippedBs) ? shippedBs : legacyBs;
|
|
215
213
|
const guidanceDir = resolve(projectRoot, '.claude/guidance');
|
|
216
|
-
const
|
|
217
|
-
if (existsSync(
|
|
218
|
-
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
214
|
+
const shippedDir = resolve(projectRoot, 'node_modules/moflo/.claude/guidance/shipped');
|
|
215
|
+
if (existsSync(shippedDir)) {
|
|
216
|
+
const shippedFiles = readdirSync(shippedDir).filter(f => f.endsWith('.md'));
|
|
217
|
+
for (const file of shippedFiles) {
|
|
218
|
+
const dest = resolve(guidanceDir, `moflo-${file}`);
|
|
219
|
+
if (!existsSync(dest)) {
|
|
220
|
+
if (!existsSync(guidanceDir)) mkdirSync(guidanceDir, { recursive: true });
|
|
221
|
+
const header = `<!-- AUTO-GENERATED by moflo session-start. Do not edit — changes will be overwritten. -->\n<!-- Source: node_modules/moflo/.claude/guidance/shipped/${file} -->\n\n`;
|
|
222
|
+
const content = readFileSync(resolve(shippedDir, file), 'utf-8');
|
|
223
|
+
writeFileSync(dest, header + content);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
222
226
|
}
|
|
223
227
|
} catch { /* non-fatal */ }
|
|
224
228
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moflo",
|
|
3
|
-
"version": "4.8.
|
|
3
|
+
"version": "4.8.40",
|
|
4
4
|
"description": "MoFlo — AI agent orchestration for Claude Code. Forked from ruflo/claude-flow with patches applied to source, plus feature-level orchestration.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"@types/bcrypt": "^5.0.2",
|
|
90
90
|
"@types/node": "^20.19.37",
|
|
91
91
|
"eslint": "^8.0.0",
|
|
92
|
-
"moflo": "^4.8.
|
|
92
|
+
"moflo": "^4.8.39",
|
|
93
93
|
"tsx": "^4.21.0",
|
|
94
94
|
"typescript": "^5.9.3",
|
|
95
95
|
"vitest": "^4.0.0"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moflo/cli",
|
|
3
|
-
"version": "4.8.
|
|
3
|
+
"version": "4.8.40",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MoFlo CLI — AI agent orchestration with specialized agents, swarm coordination, MCP server, self-learning hooks, and vector memory for Claude Code",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
# Guidance & Memory Tuning Strategy
|
|
2
|
-
|
|
3
|
-
**Purpose:** How to build and tune a RAG-based guidance system using moflo's semantic search, embedding pipeline, and indexing. Reference when creating guidance documents, troubleshooting search quality, or extending the system.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Problem Statement
|
|
8
|
-
|
|
9
|
-
Claude Code agents need project-specific knowledge — coding rules, architecture patterns, entity templates, testing conventions — delivered at the right moment. Without a retrieval system, agents either miss critical rules or require massive CLAUDE.md files that waste context window tokens.
|
|
10
|
-
|
|
11
|
-
**Goals:**
|
|
12
|
-
- Agents find relevant guidance automatically via semantic search
|
|
13
|
-
- Subagents spawned by the coordinator inherit memory access
|
|
14
|
-
- Search quality is high enough that agents don't need to read whole files
|
|
15
|
-
- The system survives `npm install` (indexing runs on session start)
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## Architecture
|
|
20
|
-
|
|
21
|
-
Three layers: embedding generation, vector storage, and search.
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
Source Files (.claude/guidance/*.md, docs/*.md)
|
|
25
|
-
|
|
|
26
|
-
v
|
|
27
|
-
index-guidance.mjs --- Chunk on ## headers, build RAG links
|
|
28
|
-
| (prev/next, siblings, parent/child, context overlap)
|
|
29
|
-
v
|
|
30
|
-
.swarm/memory.db ----- SQLite (entries + metadata + embedding vectors)
|
|
31
|
-
|
|
|
32
|
-
v
|
|
33
|
-
build-embeddings.mjs - Generate 384-dim vectors per entry
|
|
34
|
-
| (Xenova/all-MiniLM-L6-v2 neural, or domain-aware hash fallback)
|
|
35
|
-
v
|
|
36
|
-
RuVector (@ruvector/core) -- HNSW index infrastructure
|
|
37
|
-
v
|
|
38
|
-
Search layer ---------- Three access paths:
|
|
39
|
-
1. MCP tools (mcp__moflo__memory_search) -- preferred
|
|
40
|
-
2. CLI (npx flo memory search) -- fallback
|
|
41
|
-
3. Script (semantic-search.mjs) -- detailed output
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
**Key files:**
|
|
45
|
-
|
|
46
|
-
| File | Role |
|
|
47
|
-
|------|------|
|
|
48
|
-
| `.claude/guidance/*.md` | Guidance documents (source of truth) |
|
|
49
|
-
| `bin/index-guidance.mjs` | Chunks documents, stores in SQLite with RAG metadata |
|
|
50
|
-
| `bin/build-embeddings.mjs` | Generates vector embeddings (neural or hash) |
|
|
51
|
-
| `.swarm/memory.db` | SQLite database with entries, metadata, embeddings |
|
|
52
|
-
| `@ruvector/core` | HNSW vector index, WASM fallback, SIMD operations |
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
## Guidance Document Optimization Rules
|
|
57
|
-
|
|
58
|
-
These rules determine how well your guidance documents retrieve via semantic search:
|
|
59
|
-
|
|
60
|
-
### 1. Every file needs a Purpose line
|
|
61
|
-
|
|
62
|
-
Add `**Purpose:**` as the first meaningful line after the title. Claude checks this first for relevance scoring. Without it, the chunk has no summary signal.
|
|
63
|
-
|
|
64
|
-
### 2. H2 headings are the primary retrieval signal
|
|
65
|
-
|
|
66
|
-
The indexer splits on `##`. Each heading becomes the chunk title, prepended to searchable content. Domain-specific keywords in headings dramatically improve recall.
|
|
67
|
-
|
|
68
|
-
**Bad:** `## Overview`, `## Rules`, `## Pattern`
|
|
69
|
-
**Good:** `## Soft Delete Rules`, `## JWT Authentication Pattern`, `## Database Entity Migration`
|
|
70
|
-
|
|
71
|
-
### 3. Ideal chunk size: 1000-4000 characters
|
|
72
|
-
|
|
73
|
-
Below 50 chars the chunk is dropped. Above 6000 the indexer force-splits on paragraphs, which breaks mid-thought. The sweet spot produces focused embeddings.
|
|
74
|
-
|
|
75
|
-
### 4. Self-contained chunks
|
|
76
|
-
|
|
77
|
-
Each H2 section must answer a question without needing the rest of the document. Include: the rule, a code example, and a cross-reference.
|
|
78
|
-
|
|
79
|
-
### 5. Tables over prose
|
|
80
|
-
|
|
81
|
-
Claude parses structured data more accurately than paragraphs. DO/DON'T tables, field reference tables, and command tables all retrieve better.
|
|
82
|
-
|
|
83
|
-
### 6. Cross-references create a navigation graph
|
|
84
|
-
|
|
85
|
-
The RAG indexer stores `prevChunk`/`nextChunk`/`siblings` metadata. Cross-references between documents let Claude follow chains: `core.md -> coding-rules.md -> database.md`.
|
|
86
|
-
|
|
87
|
-
### 7. No decorative formatting
|
|
88
|
-
|
|
89
|
-
ASCII boxes, excessive emoji, rhetorical questions, and motivational text all waste tokens without improving retrieval or comprehension.
|
|
90
|
-
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
## Embedding Pipeline
|
|
94
|
-
|
|
95
|
-
### Embedding Models
|
|
96
|
-
|
|
97
|
-
| Model | Quality | Speed | When Used |
|
|
98
|
-
|-------|---------|-------|-----------|
|
|
99
|
-
| `Xenova/all-MiniLM-L6-v2` | High (true semantic) | ~3s for 1000 entries | Primary — `build-embeddings.mjs` uses this |
|
|
100
|
-
| `domain-aware-hash-v1` | Good (domain clustering) | <1s for 1000 entries | Fallback when Transformers.js unavailable |
|
|
101
|
-
|
|
102
|
-
**Neural embeddings (Xenova/all-MiniLM-L6-v2):**
|
|
103
|
-
- Uses `@xenova/transformers` with ONNX WASM runtime
|
|
104
|
-
- 384-dimensional vectors, L2-normalized
|
|
105
|
-
- True semantic understanding — "soft delete" matches "mark as deleted" without keyword overlap
|
|
106
|
-
- Loaded lazily on first use, cached for subsequent queries
|
|
107
|
-
- Ships with moflo; no additional install needed
|
|
108
|
-
|
|
109
|
-
**Domain-aware hash embeddings (fallback):**
|
|
110
|
-
- Custom SimHash-style algorithm with 12 domain clusters
|
|
111
|
-
- Domain clusters group related terms: `database` (orm, postgresql, entity, schema...), `frontend` (react, component, css...), `testing` (vitest, mock, expect...), etc.
|
|
112
|
-
- Multi-position hashing with bigram/trigram features
|
|
113
|
-
- Good at keyword-level matching but misses semantic paraphrases
|
|
114
|
-
- No external dependencies — always available
|
|
115
|
-
|
|
116
|
-
### The Embedding Alignment Problem
|
|
117
|
-
|
|
118
|
-
**Critical rule:** Query embeddings MUST match stored embeddings. Computing cosine similarity between vectors from different models produces meaningless scores.
|
|
119
|
-
|
|
120
|
-
Both the search scripts and the MCP memory tools auto-detect the stored embedding model:
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
// Check what model stored entries predominantly use
|
|
124
|
-
const modelCheck = db.prepare(
|
|
125
|
-
`SELECT embedding_model, COUNT(*) as cnt FROM memory_entries
|
|
126
|
-
WHERE status = 'active' AND embedding IS NOT NULL
|
|
127
|
-
GROUP BY embedding_model ORDER BY cnt DESC LIMIT 1`
|
|
128
|
-
).get();
|
|
129
|
-
|
|
130
|
-
// If stored embeddings are neural, use neural for query too
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
Search also **filters out entries with mismatched `embedding_model`** — if the query uses neural embeddings, hash-embedded entries are skipped (and vice versa).
|
|
134
|
-
|
|
135
|
-
### Domain Cluster Tuning
|
|
136
|
-
|
|
137
|
-
The hash fallback's domain clusters can be extended with project-specific terms. Add terms to the relevant cluster in the hash embedding function to improve keyword-level matching for your domain:
|
|
138
|
-
|
|
139
|
-
| Cluster | Example Terms |
|
|
140
|
-
|---------|--------------|
|
|
141
|
-
| `database` | your ORM, database engine, schema terms |
|
|
142
|
-
| `frontend` | UI framework, component library terms |
|
|
143
|
-
| `backend` | DI container, API framework terms |
|
|
144
|
-
| `testing` | test framework, assertion library terms |
|
|
145
|
-
| `security` | auth system, permission model terms |
|
|
146
|
-
|
|
147
|
-
---
|
|
148
|
-
|
|
149
|
-
## RAG Indexing Pipeline
|
|
150
|
-
|
|
151
|
-
### How `index-guidance.mjs` Works
|
|
152
|
-
|
|
153
|
-
1. **Scan** configured directories for `.md` files
|
|
154
|
-
2. **Hash check** — Skip files whose content hash hasn't changed (unless `--force`)
|
|
155
|
-
3. **Store full document** as `doc-{prefix}-{name}` (for complete retrieval)
|
|
156
|
-
4. **Chunk on `##` headers** — Each H2 section becomes a separate entry
|
|
157
|
-
5. **H3 subsections** become child chunks with parent H2 as context prefix
|
|
158
|
-
6. **Force-split** sections over 4000 chars on paragraph boundaries
|
|
159
|
-
7. **Build RAG metadata** for every chunk:
|
|
160
|
-
|
|
161
|
-
| Metadata Field | Purpose |
|
|
162
|
-
|---------------|---------|
|
|
163
|
-
| `parentDoc` | Link back to full document |
|
|
164
|
-
| `prevChunk` / `nextChunk` | Sequential navigation |
|
|
165
|
-
| `siblings` | All chunk keys from same document |
|
|
166
|
-
| `hierarchicalParent` / `hierarchicalChildren` | H2->H3 relationships |
|
|
167
|
-
| `contextBefore` / `contextAfter` | 20% overlapping text from adjacent chunks |
|
|
168
|
-
|
|
169
|
-
8. **Prepend context** — Each chunk's searchable content includes overlap from neighbors
|
|
170
|
-
9. **Stale cleanup** — After indexing, remove entries for files that no longer exist on disk
|
|
171
|
-
10. **Background embedding** — Spawn `build-embeddings.mjs` in background to generate vectors
|
|
172
|
-
|
|
173
|
-
### Configuring Indexed Directories
|
|
174
|
-
|
|
175
|
-
In `moflo.yaml`:
|
|
176
|
-
|
|
177
|
-
```yaml
|
|
178
|
-
guidance:
|
|
179
|
-
directories:
|
|
180
|
-
- .claude/guidance
|
|
181
|
-
- docs/guides
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
Default directories (when no config): `.claude/guidance`, `docs/guides`
|
|
185
|
-
|
|
186
|
-
Moflo also automatically indexes its own bundled guidance from `node_modules/moflo/.claude/guidance/` when installed as a library in a consumer project.
|
|
187
|
-
|
|
188
|
-
---
|
|
189
|
-
|
|
190
|
-
## Lessons Learned
|
|
191
|
-
|
|
192
|
-
### Document Optimization
|
|
193
|
-
|
|
194
|
-
1. **`**Purpose:**` lines are critical** — They're the single highest-impact addition for retrieval quality.
|
|
195
|
-
2. **Headings are embeddings** — In a chunk-per-section system, the heading IS the embedding's primary signal. Generic headings are nearly useless.
|
|
196
|
-
3. **Tables retrieve better than prose** — Claude parses structured data with higher accuracy.
|
|
197
|
-
4. **Cross-references are the RAG graph** — Isolated documents can't be navigated.
|
|
198
|
-
5. **Chunk size matters** — A 10,000-char section produces a diluted embedding. Splitting into focused sections triples the chance of matching specific queries.
|
|
199
|
-
|
|
200
|
-
### Embedding Pipeline
|
|
201
|
-
|
|
202
|
-
6. **Query embeddings MUST match stored embeddings** — This is the single most critical rule. Auto-detect and match.
|
|
203
|
-
7. **Domain clusters need project-specific terms** — Generic NLP clusters miss project-specific terminology. Adding terms to domain clusters dramatically improves keyword-level matching.
|
|
204
|
-
8. **Filter mismatched entries during search** — Mixed databases need explicit filtering by `embedding_model`.
|
|
205
|
-
|
|
206
|
-
---
|
|
207
|
-
|
|
208
|
-
## Replication Guide
|
|
209
|
-
|
|
210
|
-
To set up this system in a new project using moflo:
|
|
211
|
-
|
|
212
|
-
### 1. Install Moflo
|
|
213
|
-
|
|
214
|
-
```bash
|
|
215
|
-
npm install moflo
|
|
216
|
-
npx flo init
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
### 2. Create Guidance Documents
|
|
220
|
-
|
|
221
|
-
Create `.claude/guidance/` directory with markdown files following the optimization rules above:
|
|
222
|
-
- Every file has `**Purpose:**` line
|
|
223
|
-
- H2 sections with domain keywords in headings
|
|
224
|
-
- Tables for structured rules
|
|
225
|
-
- Cross-references between related docs
|
|
226
|
-
- 1000-4000 char sections
|
|
227
|
-
|
|
228
|
-
### 3. Configure Indexing
|
|
229
|
-
|
|
230
|
-
In `moflo.yaml`:
|
|
231
|
-
|
|
232
|
-
```yaml
|
|
233
|
-
guidance:
|
|
234
|
-
directories:
|
|
235
|
-
- .claude/guidance
|
|
236
|
-
- docs/guides
|
|
237
|
-
|
|
238
|
-
auto_index:
|
|
239
|
-
guidance: true
|
|
240
|
-
code_map: true
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
### 4. Index and Verify
|
|
244
|
-
|
|
245
|
-
```bash
|
|
246
|
-
# Index documents
|
|
247
|
-
npx flo-index --force
|
|
248
|
-
|
|
249
|
-
# Test search quality
|
|
250
|
-
npx flo memory search --query "your domain query" --namespace guidance
|
|
251
|
-
|
|
252
|
-
# Verify from Claude Code via MCP
|
|
253
|
-
# mcp__moflo__memory_search query="your domain query" namespace="guidance"
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
---
|
|
257
|
-
|
|
258
|
-
## See Also
|
|
259
|
-
|
|
260
|
-
- `.claude/guidance/memory-strategy.md` - Memory architecture and search commands
|
|
261
|
-
- `.claude/guidance/agent-bootstrap.md` - Subagent bootstrap guide
|
|
262
|
-
- `.claude/guidance/moflo.md` - Full CLI/MCP reference
|