codetrap 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +151 -52
  2. package/docs/installation.md +113 -29
  3. package/package.json +4 -3
  4. package/plugins/codetrap-agent/.codex-plugin/plugin.json +1 -2
  5. package/plugins/codetrap-agent/hooks/post-flight-capture.example.md +19 -17
  6. package/plugins/codetrap-agent/hooks.json +2 -2
  7. package/{skills → plugins/codetrap-agent/skills}/codetrap-add/SKILL.md +10 -4
  8. package/plugins/codetrap-agent/skills/codetrap-capture/SKILL.md +14 -3
  9. package/plugins/codetrap-agent/skills/codetrap-capture-external/SKILL.md +52 -9
  10. package/plugins/codetrap-agent/skills/codetrap-check/SKILL.md +74 -6
  11. package/{skills → plugins/codetrap-agent/skills}/codetrap-search/SKILL.md +6 -5
  12. package/plugins/codetrap-agent/templates/AGENTS.codetrap.md +31 -5
  13. package/scripts/search-policy-sweep.ts +131 -0
  14. package/src/commands/workflow.ts +144 -68
  15. package/src/db/embedding-queries.ts +230 -48
  16. package/src/db/queries.ts +0 -25
  17. package/src/db/repository.ts +32 -21
  18. package/src/db/schema.ts +80 -0
  19. package/src/index.ts +28 -3
  20. package/src/lib/command-requests.ts +112 -1
  21. package/src/lib/config.ts +57 -7
  22. package/src/lib/constants.ts +1 -1
  23. package/src/lib/doctor.ts +42 -12
  24. package/src/lib/embedder.ts +118 -3
  25. package/src/lib/embedding-health.ts +3 -1
  26. package/src/lib/embedding-job.ts +3 -0
  27. package/src/lib/embedding-management.ts +65 -0
  28. package/src/lib/embedding-runtime.ts +177 -0
  29. package/src/lib/output-json.ts +0 -2
  30. package/src/lib/scope-context.ts +12 -6
  31. package/src/lib/scope-migration.ts +2 -1
  32. package/src/lib/scope.ts +0 -2
  33. package/src/lib/search-eval.ts +38 -18
  34. package/src/lib/search-policy-sweep.ts +563 -0
  35. package/src/lib/search-policy.ts +0 -4
  36. package/src/lib/search-service.ts +14 -15
  37. package/src/lib/session-candidate-document.ts +175 -0
  38. package/src/lib/session-candidate-scope.ts +6 -0
  39. package/src/lib/session-capture.ts +298 -32
  40. package/src/lib/session-codec.ts +1 -8
  41. package/src/lib/session-operations.ts +83 -60
  42. package/src/lib/session-review.ts +327 -0
  43. package/src/lib/session-store.ts +87 -73
  44. package/src/lib/store.ts +74 -10
  45. package/src/lib/string-list.ts +3 -0
  46. package/src/lib/text-lines.ts +7 -0
  47. package/src/lib/trap-search-document.ts +2 -1
  48. package/src/lib/value-types.ts +3 -0
  49. package/src/web/client-review.ts +171 -0
  50. package/src/web/client-script.ts +426 -51
  51. package/src/web/client-shell.ts +414 -0
  52. package/src/web/client-text.ts +112 -0
  53. package/src/web/project-registry.ts +3 -5
  54. package/src/web/server.ts +117 -103
  55. package/src/web/static.ts +364 -19
  56. package/skills/codetrap-capture-external/SKILL.md +0 -62
  57. package/skills/codetrap-check/SKILL.md +0 -69
  58. package/src/lib/embedding-index.ts +0 -53
package/README.md CHANGED
@@ -53,18 +53,74 @@ codetrap list
53
53
  codetrap show 1
54
54
  ```
55
55
 
56
+ ## 5-Minute Agent Setup
57
+
58
+ For a first AI-agent user, the fastest path is CLI-first project guidance:
59
+
60
+ ```bash
61
+ # npm/source installs require Bun because the package entrypoint uses /usr/bin/env bun
62
+ bun --version # If this fails, install Bun first or use the binary install in docs/installation.md
63
+ npm install -g codetrap
64
+
65
+ # Initialize pitfall memory in the target project
66
+ cd /path/to/project
67
+ codetrap init
68
+ codetrap doctor
69
+ ```
70
+
71
+ Add the packaged agent guidance to `AGENTS.md` for Codex, or to `CLAUDE.md` for Claude Code:
72
+
73
+ ```bash
74
+ cat "$(npm root -g)/codetrap/plugins/codetrap-agent/templates/AGENTS.codetrap.md" >> AGENTS.md
75
+ # or:
76
+ cat "$(npm root -g)/codetrap/plugins/codetrap-agent/templates/AGENTS.codetrap.md" >> CLAUDE.md
77
+ ```
78
+
79
+ Then have the agent run this before non-trivial edits:
80
+
81
+ ```bash
82
+ codetrap search "<task keywords>" --mode hybrid --json
83
+ ```
84
+
85
+ Review the top 3 returned action cards, or all returned cards if fewer than 3, before deciding whether any trap applies. Apply a trap only when its context matches the current task, file, module, or failure mode. Severity alone is not enough to apply a trap. Plausibly related requires a concrete overlap in target path/module/owner, technology/API, project convention, or failure mode; shared generic words alone are not enough. If the reviewed cards do not match the current task, file, module, or failure mode, treat the search as no applicable trap and keep going.
86
+
87
+ After a user correction, repeated test failure, or review finding, have the agent draft a candidate instead of writing confirmed memory:
88
+
89
+ ```bash
90
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
91
+ Title: <durable pitfall>
92
+ Context: <when it triggers>
93
+ Mistake: <what the agent did wrong>
94
+ Fix: <what to do instead>
95
+ EOF
96
+ ```
97
+
98
+ Review pending candidates with `codetrap session status`, `codetrap session list`, `codetrap doctor`, or `codetrap web`; accepting a candidate remains an explicit human decision.
99
+
100
+ Use the returned `candidate_id` and `session_id` to inspect and resolve the candidate:
101
+
102
+ ```bash
103
+ codetrap session candidate <candidate-id> --session <session-id> --json
104
+
105
+ # Only after explicit human approval:
106
+ codetrap session accept <candidate-id> --session <session-id>
107
+
108
+ # Or reject instead:
109
+ codetrap session reject <candidate-id> --session <session-id> --reason "<reason>"
110
+ ```
111
+
56
112
  ## Features
57
113
 
58
114
  - **Structured trap recording** — title, category, context, mistake, fix, severity, tags, lifecycle, evidence, before/after code
59
115
  - **Session mode capture** — record implementation notes, promote explicit structured trap notes into candidates, and save only user-accepted lessons
60
116
  - **Dual scope** — project-scoped (`.codetrap/traps.db`) and global (`~/.codetrap/traps.db`)
61
117
  - **CLI-first agent API** — `search/show/list/stats/doctor --json` and stdin query support for shell-friendly automation
62
- - **Three search modes** — FTS (SQLite FTS5), semantic (Jina embeddings), hybrid (RRF fusion)
118
+ - **Three search modes** — FTS (SQLite FTS5), semantic (local Ollama or Jina embeddings), hybrid (RRF fusion)
63
119
  - **Chinese + mixed-language search** — CJK bigram tokenizer, synonym map for Chinese-English terms
64
120
  - **MCP server** — optional tools + resources for AI agent integration
65
- - **Embedding cache** with freshness tracking — embeddings are rebuildable, stale ones auto-invalidated
121
+ - **Embedding cache** with multi-profile freshness tracking — Jina/Ollama vectors can coexist and stale ones auto-invalidate
66
122
  - **Doctor diagnostics** — scope, database, and embedding health in text or JSON
67
- - **Schema migrations** — in-code migration system from v0 through current v5
123
+ - **Schema migrations** — in-code migration system from v0 through current v6
68
124
  - **Single-binary builds** — `bun build --compile` produces standalone binaries in `dist/`
69
125
 
70
126
  ## Directory Structure
@@ -74,7 +130,7 @@ codetrap/
74
130
  ├── src/
75
131
  │ ├── index.ts CLI entry point
76
132
  │ ├── mcp-server.ts MCP server entry point
77
- │ ├── commands/router.ts Thin CLI adapter + renderer
133
+ │ ├── commands/router.ts Optional thin CLI adapter + renderer
78
134
  │ ├── commands/workflow.ts CLI command behavior
79
135
  │ ├── commands/command-result.ts CLI command results + rendering
80
136
  │ ├── mcp/
@@ -87,9 +143,12 @@ codetrap/
87
143
  │ │ ├── store.ts Project/global scope orchestration
88
144
  │ │ ├── trap-operations.ts Shared CLI/MCP operation semantics
89
145
  │ │ ├── session-operations.ts Session command semantics + accept/reject flow
146
+ │ │ ├── session-review.ts Shared session review payloads + CLI conflict presenter
90
147
  │ │ ├── session-store.ts Session files, active state, index, recaps
91
148
  │ │ ├── session-codec.ts Session JSON/Markdown/candidate file conversion
92
- │ │ ├── session-capture.ts Candidate trap extraction from explicit structured notes
149
+ │ │ ├── session-capture.ts Candidate draft normalization, extraction, and merge policy
150
+ │ │ ├── session-candidate-document.ts Candidate document transition rules
151
+ │ │ ├── session-candidate-scope.ts Candidate accepted-scope fallback
93
152
  │ │ ├── session-conflicts.ts Candidate vs active-trap conflict checks
94
153
  │ │ ├── trap-quality.ts Deterministic candidate quality scoring
95
154
  │ │ ├── command-requests.ts CLI/MCP request normalization helpers
@@ -97,7 +156,9 @@ codetrap/
97
156
  │ │ ├── scope-context.ts cwd/project/global DB context + repo selection
98
157
  │ │ ├── scope-migration.ts Safe project trap scope repair/migration
99
158
  │ │ ├── doctor.ts Scope and embedding health diagnostics
159
+ │ │ ├── embedding-runtime.ts Embedding provider runtime/config/status
100
160
  │ │ ├── embedding-health.ts Fresh/stale/missing embedding summaries
161
+ │ │ ├── embedding-management.ts Embedding profile command output
101
162
  │ │ ├── search-service.ts FTS/semantic/hybrid candidate retrieval
102
163
  │ │ ├── search-policy.ts Applicability filtering, rerank, fusion signals
103
164
  │ │ ├── search-result-card.ts Compact agent-facing result cards
@@ -107,10 +168,13 @@ codetrap/
107
168
  │ │ ├── trap-json-fields.ts Tags/path/evidence JSON array codec
108
169
  │ │ ├── trap-codec.ts Storage/JSON/archive/import shape conversion
109
170
  │ │ ├── trap-mutation-result.ts Mutation result + scope fallback semantics
171
+ │ │ ├── string-list.ts Shared string list de-duping
172
+ │ │ ├── text-lines.ts Shared line trimming helpers
173
+ │ │ ├── value-types.ts Shared runtime value guards
110
174
  │ │ ├── trap-scope-match.ts Path/module/owner applicability matching
111
175
  │ │ ├── trap-archive.ts Import/export compatibility
112
176
  │ │ ├── trap-transfer.ts DB-to-DB transfer for scope migration
113
- │ │ ├── embedder.ts Jina Embeddings adapter
177
+ │ │ ├── embedder.ts Ollama and Jina Embeddings adapters
114
178
  │ │ ├── embedding-job.ts Batch embedding generation
115
179
  │ │ ├── format.ts CLI output formatting
116
180
  │ │ ├── scope.ts Project root detection
@@ -121,6 +185,13 @@ codetrap/
121
185
  │ │ ├── queries.ts CRUD, search, stats, import/export
122
186
  │ │ ├── embedding-queries.ts Embedding storage SQL
123
187
  │ │ └── repository.ts Single-DB facade
188
+ │ ├── web/
189
+ │ │ ├── server.ts Thin Web API adapter over shared operations
190
+ │ │ ├── static.ts HTML/CSS shell
191
+ │ │ ├── client-shell.ts Pane sizing/collapse behavior
192
+ │ │ ├── client-review.ts Review queue + candidate draft/request model
193
+ │ │ ├── client-script.ts DOM composition and event wiring
194
+ │ │ └── client-text.ts Localized UI strings
124
195
  │ └── tests/
125
196
  │ ├── search-*.test.ts
126
197
  │ ├── trap-*.test.ts
@@ -130,8 +201,7 @@ codetrap/
130
201
  │ ├── scope-migration-cli.test.ts
131
202
  │ ├── import-export-cli.test.ts
132
203
  │ └── fixtures/search-eval.json
133
- ├── skills/ Agent skill definitions
134
- ├── plugins/codetrap-agent/ Sample Codex plugin bundle
204
+ ├── plugins/codetrap-agent/ Codex plugin bundle with skills, MCP config, hooks, and templates
135
205
  ├── scripts/ Release asset and preflight scripts
136
206
  ├── docs/ Architecture + reference docs
137
207
  ├── package.json
@@ -144,7 +214,7 @@ codetrap/
144
214
  | Command | Description |
145
215
  |---|---|
146
216
  | `init` | Initialize `.codetrap/` in current project |
147
- | `add` | Record a new trap (`--json` structured input; interactive mode is not implemented) |
217
+ | `add` | Record a confirmed trap (`--json` structured input; interactive mode is not implemented) |
148
218
  | `search <query>` | Search traps (--mode fts\|semantic\|hybrid, --category, --scope, --status, --limit, --path, --module, --owner, --no-rerank, --ranking-signals, --json; query can come from stdin) |
149
219
  | `list` | List traps (--category, --scope, --status, --path, --module, --owner, --limit, --json) |
150
220
  | `show <id>` | Show full trap details (--json) |
@@ -159,9 +229,10 @@ codetrap/
159
229
  | `doctor` | Diagnose cwd, scope, database paths, trap counts, and embedding health (--json) |
160
230
  | `repair-scope` | Move legacy mis-scoped project traps into the current project (dry-run by default, `--apply` to mutate, `--json`) |
161
231
  | `migrate-project` | Move project traps between initialized projects (`--from-project-path`, `--to-project-path`, dry-run by default, `--apply`, `--json`) |
162
- | `embed` | Generate embeddings (requires JINA_API_KEY) |
163
- | `session` | Start a development session, append notes, promote explicit structured trap notes into candidates, accept/reject candidates, and clean up session files |
164
- | `web` | Start the local review and trap library console |
232
+ | `embed` | Generate embeddings (requires configured Ollama or Jina provider) |
233
+ | `embeddings` | Manage embedding profiles (`status`, `list`, `use ollama|jina`, `reindex`) |
234
+ | `session` | Start a development session, append notes, capture post-flight candidates, promote explicit structured trap notes into candidates, accept/reject candidates, and clean up session files |
235
+ | `web` | Start the local review, trap library, insights, and Embeddings console |
165
236
  | `serve` | Start MCP server |
166
237
 
167
238
  ### Session Mode
@@ -171,13 +242,26 @@ Session mode stores temporary working memory in `.codetrap/sessions/`. It does n
171
242
  ```bash
172
243
  codetrap session start "implement agent harness" --spec docs/agent-harness-spec.md --module agent-runtime
173
244
  codetrap session note --kind decision --text "Defaulted tool calls to 30s because the spec does not define timeout behavior."
174
- codetrap session note --kind review --text $'Title: Do not parse nested tool calls with regex\nContext: When implementing parser logic for nested tool-call arguments.\nMistake: Using regex to split nested calls corrupts arguments.\nFix: Use a tokenizer/parser and add regression tests for nested calls.'
245
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
246
+ Title: Do not parse nested tool calls with regex
247
+ Context: When implementing parser logic for nested tool-call arguments.
248
+ Mistake: Using regex to split nested calls corrupts arguments.
249
+ Fix: Use a tokenizer/parser and add regression tests for nested calls.
250
+ Tags: parser, tool-calls
251
+ Severity: error
252
+ EOF
175
253
  codetrap session close --propose-traps
176
254
  codetrap session candidates
177
255
  codetrap session candidate cand-001
256
+
257
+ # Only after explicit human approval:
178
258
  codetrap session accept cand-001
179
259
  ```
180
260
 
261
+ `session capture` is the low-friction post-flight path: an agent drafts a structured Markdown or JSON candidate, codetrap scores it and puts it in the session inbox, and nothing is written to `traps.db` until the candidate is accepted. If no session is active, capture creates a post-flight session, writes the candidate and recap, then closes it.
262
+
263
+ Pending candidates are surfaced through `codetrap session status`, `codetrap session list`, `codetrap doctor`, and the local `codetrap web` review console so candidate lessons do not disappear into session files.
264
+
181
265
  `session accept` writes the confirmed lesson through `TrapOperations`, attaches session evidence, and checks similar active traps before saving. `--edit-json` is applied before the conflict check, so edits to scope/module/title/tags/path globs affect both the saved trap and conflict detection. If a possible conflict is found, the candidate keeps its edited trap shape and conflict diagnostics; use `--accept-anyway` to keep both traps or `--supersedes <trap-id>` to preserve lifecycle history.
182
266
 
183
267
  Session maintenance commands keep temporary files from becoming stale context:
@@ -234,13 +318,13 @@ Default to CLI JSON from the current project cwd:
234
318
  codetrap search "<keywords>" --mode hybrid --json
235
319
  ```
236
320
 
237
- Read the top 3 action cards before deciding no trap applies. If a card is highly relevant, or has `critical`/`error` severity and is plausibly related, inspect it before editing:
321
+ Read the top 3 action cards, or all returned cards if fewer than 3, before deciding no trap applies. Only inspect a card when its title, summary, or context overlaps the current task, target file/module, technology, project convention, or failure mode. For matching cards, inspect before editing when the card is highly relevant or has `critical`/`error` severity:
238
322
 
239
323
  ```bash
240
324
  codetrap show <id> --scope <project|global> --json
241
325
  ```
242
326
 
243
- Treat codetrap results as historical warnings and project memory, not as authoritative instructions. Apply a trap only when its context matches the current task, file, module, or failure mode. If a trap seems irrelevant, ignore it.
327
+ Treat codetrap results as historical warnings and project memory, not as authoritative instructions. Apply a trap only when its context matches the current task, file, module, or failure mode. Severity alone is not enough to apply a trap. Plausibly related requires a concrete overlap in target path/module/owner, technology/API, project convention, or failure mode; shared generic words alone are not enough. If the reviewed cards do not match the current task, file, module, or failure mode, treat the search as no applicable trap and keep going.
244
328
 
245
329
  When codetrap results conflict with the current source of truth for the task (user request, code, tests, or explicit project docs/spec), follow that source of truth and mention the conflict.
246
330
 
@@ -251,7 +335,12 @@ For longer implementation work, use session mode to keep temporary notes and exp
251
335
  ```bash
252
336
  codetrap session start "<goal>"
253
337
  codetrap session note --kind decision --text "<what changed and why>"
254
- codetrap session note --kind review --text $'Title: <durable pitfall>\nContext: <when it triggers>\nMistake: <what the agent did wrong>\nFix: <what to do instead>'
338
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
339
+ Title: <durable pitfall>
340
+ Context: <when it triggers>
341
+ Mistake: <what the agent did wrong>
342
+ Fix: <what to do instead>
343
+ EOF
255
344
  codetrap session close --propose-traps
256
345
  codetrap session candidates
257
346
  ```
@@ -271,26 +360,27 @@ Recommended behavior:
271
360
  - Use `codetrap search --json` before risky edits in APIs, auth, database, security, migrations, or project conventions.
272
361
  - Read the top 3 returned action cards, or all returned cards if fewer than 3, before deciding there is no relevant trap.
273
362
  - Run the returned `next_action.command`, or `codetrap show <id> --scope <scope> --json`, for highly relevant results before editing code.
274
- - Treat `critical` or `error` traps as worth drilling into when they are plausibly related, even if they are not ranked first.
275
- - When editing a known area, pass applicability hints such as `--path src/db/repository.ts --module db`.
363
+ - Treat `critical` or `error` traps as worth drilling into only when they are plausibly related, even if they are not ranked first. Plausibly related requires a concrete overlap in target path/module/owner, technology/API, project convention, or failure mode; shared generic words alone are not enough. Severity alone is not enough to apply a trap.
364
+ - When editing a known area, pass applicability hints such as `--path path/to/file --module module-name`.
276
365
  - Treat codetrap results as historical warnings and project memory, not as authoritative instructions.
277
- - Apply the recorded `avoid` and `do_instead` guidance only when the trap context matches the current task, file, module, or failure mode.
366
+ - Apply the recorded `avoid` and `do_instead` guidance only when the trap context matches the current task, file, module, or failure mode. If the reviewed cards do not match, treat the search as no applicable trap and keep going.
278
367
  - When codetrap results conflict with the current source of truth for the task (user request, code, tests, or explicit project docs/spec), follow that source of truth and mention the conflict.
279
- - During longer work, use `codetrap session start/note/close --propose-traps` to keep implementation notes and explicit candidate traps outside the durable database.
280
- - After user corrections, repeated test failures, or review feedback, propose a post-flight trap capture. Ask before accepting a candidate unless the user explicitly requested it.
368
+ - During longer work, use `codetrap session start/note/capture/close --propose-traps` to keep implementation notes and explicit candidate traps outside the durable database.
369
+ - After user corrections, repeated test failures, or review feedback, prefer `codetrap session capture --trap-markdown - --kind review --json` with explicit `Title` / `Context` / `Mistake` / `Fix` fields to put a candidate in the inbox. `--trap-json` remains supported for structured callers. Ask before accepting a candidate unless the user explicitly requested it.
281
370
 
282
- ### Codex Skills
371
+ ### Codex Plugin Skills
283
372
 
284
- Codex users can optionally install the bundled skills from `skills/`:
373
+ Codex skills are distributed through the bundled plugin at `plugins/codetrap-agent/skills/`:
285
374
 
286
375
  - `codetrap-check` — pre-flight check before code changes.
287
376
  - `codetrap-search` — search existing lessons.
288
- - `codetrap-add` — record a new pitfall.
377
+ - `codetrap-capture` — propose an agent-discovered post-flight lesson into the candidate inbox.
378
+ - `codetrap-add` — record a confirmed pitfall only after explicit user approval.
289
379
  - `codetrap-capture-external` — extract durable trap candidates from an external article, issue, paper, or reference; Codex reads the source and codetrap stores only confirmed lessons.
290
380
 
291
- Skills are a convenience layer for Codex users. They do not replace MCP or `AGENTS.md`; they make manual triggers like "run codetrap-check" easier.
381
+ The plugin skill directory is the single source of truth for Codex skill packaging. The repo does not keep a duplicate root `skills/` tree.
292
382
 
293
- The repo also includes a sample Codex plugin bundle at `plugins/codetrap-agent` with skills, optional MCP config, hook templates, and an `AGENTS.md` snippet.
383
+ Skills are a convenience layer for Codex users. They do not replace MCP or `AGENTS.md`; they make manual triggers like "run codetrap-check" easier.
294
384
 
295
385
  External lessons should keep codetrap local-first: let the agent read the URL or pasted source, ask which candidate traps to save, then attach the source as evidence instead of making the CLI crawl the web:
296
386
 
@@ -338,7 +428,11 @@ codetrap://project/recent?cwd=%2Fpath%2Fto%2Fproject
338
428
 
339
429
  | Env Variable | Required | Description |
340
430
  |---|---|---|
341
- | `JINA_API_KEY` | No | Jina AI API key for semantic/hybrid search. Without it, hybrid falls back to FTS. Get one at [jina.ai](https://jina.ai/api-dashboard/) |
431
+ | `CODETRAP_EMBEDDING_PROVIDER` | No | Embedding provider for semantic/hybrid search: `ollama` or `jina`. Recommended local value: `ollama` |
432
+ | `CODETRAP_OLLAMA_MODEL` | No | Ollama embedding model. Recommended: `qwen3-embedding:0.6b` |
433
+ | `CODETRAP_OLLAMA_ENDPOINT` | No | Ollama endpoint. Default: `http://127.0.0.1:11434` |
434
+ | `CODETRAP_OLLAMA_DIMENSIONS` | No | Ollama embedding dimensions. Default: `1024` for `qwen3-embedding:0.6b` |
435
+ | `JINA_API_KEY` | No | Jina AI API key for optional cloud semantic/hybrid search. Get one at [jina.ai](https://jina.ai/api-dashboard/) |
342
436
  | `CODETRAP_SEARCH_MODE` | No | Default search mode: `fts`, `semantic`, or `hybrid` |
343
437
  | `CODETRAP_SEARCH_LIMIT` | No | Default search result limit |
344
438
  | `CODETRAP_SEARCH_SCOPE` | No | Default search scope: `project` or `global` |
@@ -353,6 +447,12 @@ Behavior preferences can also live in `~/.codetrap/config.json`; CLI args overri
353
447
  "limit": 20,
354
448
  "scope": "project",
355
449
  "rerank": true
450
+ },
451
+ "embeddings": {
452
+ "provider": "ollama",
453
+ "endpoint": "http://127.0.0.1:11434",
454
+ "model": "qwen3-embedding:0.6b",
455
+ "dimensions": 1024
356
456
  }
357
457
  }
358
458
  ```
@@ -373,61 +473,60 @@ Trap JSON supports optional applicability fields:
373
473
 
374
474
  Empty applicability fields mean the trap applies everywhere. `search` and `list` can filter with `--path`, `--module`, and `--owner`; matching scoped traps receive a small rerank boost.
375
475
 
376
- ### Jina Embeddings Setup
377
-
378
- codetrap works without a Jina API key. In that mode, search uses SQLite FTS keyword matching. If you want semantic search or stronger hybrid search, configure `JINA_API_KEY`.
476
+ ### Local Ollama Embeddings Setup
379
477
 
380
- 1. Create a Jina API key from the [Jina AI dashboard](https://jina.ai/api-dashboard/).
478
+ codetrap works with no embedding provider. In that mode, search uses local SQLite FTS keyword matching, and hybrid search falls back to FTS.
381
479
 
382
- 2. Add it to your shell environment.
480
+ Recommended local semantic search uses Ollama with `qwen3-embedding:0.6b`. This keeps trap passages and query text on your machine.
383
481
 
384
- macOS or Linux with zsh:
482
+ 1. Install Ollama, then pull the 0.6B embedding model:
385
483
 
386
484
  ```bash
387
- echo 'export JINA_API_KEY="your-jina-api-key"' >> ~/.zshrc
388
- source ~/.zshrc
485
+ ollama pull qwen3-embedding:0.6b
389
486
  ```
390
487
 
391
- macOS or Linux with bash:
392
-
393
- ```bash
394
- echo 'export JINA_API_KEY="your-jina-api-key"' >> ~/.bashrc
395
- source ~/.bashrc
396
- ```
488
+ Do not omit `:0.6b`; `qwen3-embedding:latest` is much larger.
397
489
 
398
- Windows PowerShell:
490
+ 2. Configure codetrap to use Ollama:
399
491
 
400
- ```powershell
401
- setx JINA_API_KEY "your-jina-api-key"
492
+ ```bash
493
+ codetrap embeddings use ollama
494
+ codetrap embeddings status
402
495
  ```
403
496
 
404
- After `setx`, open a new PowerShell window.
497
+ This writes `~/.codetrap/config.json`. Environment variables such as `CODETRAP_EMBEDDING_PROVIDER` and `CODETRAP_OLLAMA_MODEL` are still supported for temporary shell or CI overrides.
405
498
 
406
- 3. Verify that the key is visible without printing the secret:
499
+ 3. Verify Ollama embedding generation:
407
500
 
408
501
  ```bash
409
- bun -e 'console.log(process.env.JINA_API_KEY ? "has-key" : "no-key")'
502
+ curl http://127.0.0.1:11434/api/embed -d '{"model":"qwen3-embedding:0.6b","input":"HTTP request timeout"}'
410
503
  ```
411
504
 
412
505
  4. Generate embeddings for the traps you want semantic search to use:
413
506
 
414
507
  ```bash
415
508
  cd /path/to/your/project
416
- codetrap embed --scope project
417
- codetrap embed --scope global
509
+ codetrap embeddings reindex --scope project
510
+ codetrap embeddings reindex --scope global
418
511
  ```
419
512
 
513
+ `codetrap embed` remains as a short alias for reindexing. codetrap stores embeddings by profile, so switching between Jina and Ollama does not overwrite existing vectors; it creates or refreshes the selected profile.
514
+
515
+ You can also run `codetrap web` and open the `Embeddings` view to inspect the active provider/profile, see project and global fresh/stale/missing counts, switch between Ollama and Jina, and reindex project or global embeddings from the web console. The web console does not save Jina API keys; Jina still reads `JINA_API_KEY` from the environment.
516
+
420
517
  5. Search with hybrid mode:
421
518
 
422
519
  ```bash
423
520
  codetrap search "HTTP request timeout" --mode hybrid
424
521
  ```
425
522
 
426
- If `JINA_API_KEY` is not set:
523
+ Optional cloud provider: run `codetrap embeddings use jina` and set `JINA_API_KEY` to use Jina instead of Ollama. Privacy note: codetrap does not collect telemetry. FTS and Ollama search are local-only. When Jina is configured, reindexing sends trap passages to Jina, and semantic or hybrid search may send query text to Jina to compute embeddings.
524
+
525
+ If no embedding provider is configured:
427
526
 
428
527
  - `codetrap search "<query>" --mode fts` works normally.
429
528
  - `codetrap search "<query>" --mode hybrid` works, but falls back to FTS.
430
- - `codetrap search "<query>" --mode semantic` and `codetrap embed` require `JINA_API_KEY`.
529
+ - `codetrap search "<query>" --mode semantic` and `codetrap embed` require an embedding provider.
431
530
 
432
531
  ## Build
433
532
 
@@ -454,7 +553,7 @@ bun run eval:dogfood -- report --live # Dogfood eval with configured embedding
454
553
  |---|---|
455
554
  | Runtime | Bun + TypeScript |
456
555
  | Database | SQLite (bun:sqlite) + FTS5 |
457
- | Embeddings | Jina AI (`jina-embeddings-v5-text-small`) |
556
+ | Embeddings | Local Ollama (`qwen3-embedding:0.6b`) or Jina AI (`jina-embeddings-v5-text-small`) |
458
557
  | MCP | `@modelcontextprotocol/sdk` |
459
558
  | Search | FTS5 + cosine similarity + RRF fusion + generic rerank |
460
559
 
@@ -63,6 +63,8 @@ This method does not require Bun at runtime once release binaries are published.
63
63
 
64
64
  ### For maintainers
65
65
 
66
+ Maintainer-only: agents must not create tags, push, publish packages, or create releases unless the user explicitly requests a release operation.
67
+
66
68
  Release binaries are built by `.github/workflows/release.yml` when a version tag is pushed.
67
69
 
68
70
  1. Make sure `package.json` has the version you want to release.
@@ -72,11 +74,12 @@ Release binaries are built by `.github/workflows/release.yml` when a version tag
72
74
  3. Create and push a matching tag:
73
75
 
74
76
  ```bash
75
- git tag v0.1.6
76
- git push origin v0.1.6
77
+ # Maintainer-only: do not run unless explicitly releasing.
78
+ git tag v<version>
79
+ git push origin v<version>
77
80
  ```
78
81
 
79
- The release tag must match `package.json` exactly. For example, package version `0.1.6` must use tag `v0.1.6`.
82
+ The release tag must match `package.json` exactly. For example, package version `<version>` must use tag `v<version>`.
80
83
 
81
84
  The workflow runs:
82
85
 
@@ -162,6 +165,8 @@ Best for long-term use with the published `codetrap` package.
162
165
 
163
166
  ### For maintainers
164
167
 
168
+ Maintainer-only: agents must not create tags, push, publish packages, or create releases unless the user explicitly requests a release operation.
169
+
165
170
  Package publishing is handled by `.github/workflows/npm-publish.yml` when a GitHub Release is published.
166
171
 
167
172
  Before automated publishing, configure npm trusted publishing:
@@ -179,6 +184,7 @@ If npm does not let you configure trusted publishing before the first version ex
179
184
 
180
185
  ```bash
181
186
  npm pack --dry-run
187
+ # Maintainer-only: do not run unless explicitly publishing.
182
188
  npm publish --access public
183
189
  ```
184
190
 
@@ -189,6 +195,7 @@ The workflow runs:
189
195
  ```bash
190
196
  bun test src/tests
191
197
  npm pack --dry-run
198
+ # Maintainer-only: this is run by the release workflow after explicit release approval.
192
199
  npm publish --access public
193
200
  ```
194
201
 
@@ -225,59 +232,113 @@ bun install
225
232
  bun run install:cli
226
233
  ```
227
234
 
228
- ## Optional: Jina Embeddings
235
+ ## 5-Minute Agent Setup
229
236
 
230
- `JINA_API_KEY` is optional. Without it, codetrap still works with SQLite FTS keyword search, and hybrid search falls back to FTS.
237
+ Use this path when the first user is a coding agent such as Codex or Claude Code.
231
238
 
232
- Create a key from the [Jina AI dashboard](https://jina.ai/api-dashboard/), then set it globally if you want semantic or hybrid search to work from every directory.
239
+ ```bash
240
+ bun --version # If this fails, install Bun first or use Method 2 binary install
241
+ npm install -g codetrap
242
+ cd /path/to/project
243
+ codetrap init
244
+ codetrap doctor
245
+ ```
233
246
 
234
- macOS or Linux with zsh:
247
+ Add the packaged agent guidance to `AGENTS.md` or `CLAUDE.md`:
235
248
 
236
249
  ```bash
237
- echo 'export JINA_API_KEY="your-jina-api-key"' >> ~/.zshrc
238
- source ~/.zshrc
250
+ cat "$(npm root -g)/codetrap/plugins/codetrap-agent/templates/AGENTS.codetrap.md" >> AGENTS.md
251
+ # or:
252
+ cat "$(npm root -g)/codetrap/plugins/codetrap-agent/templates/AGENTS.codetrap.md" >> CLAUDE.md
239
253
  ```
240
254
 
241
- macOS or Linux with bash:
255
+ Then have the agent run a pre-edit check from the project cwd:
242
256
 
243
257
  ```bash
244
- echo 'export JINA_API_KEY="your-jina-api-key"' >> ~/.bashrc
245
- source ~/.bashrc
258
+ codetrap search "<task keywords>" --mode hybrid --json
246
259
  ```
247
260
 
248
- Windows PowerShell:
261
+ Review the top 3 returned action cards, or all returned cards if fewer than 3, before deciding whether any trap applies. Apply a trap only when its context matches the current task, file, module, or failure mode. Severity alone is not enough to apply a trap. Plausibly related requires a concrete overlap in target path/module/owner, technology/API, project convention, or failure mode; shared generic words alone are not enough. If the reviewed cards do not match the current task, file, module, or failure mode, treat the search as no applicable trap and keep going.
249
262
 
250
- ```powershell
251
- setx JINA_API_KEY "your-jina-api-key"
263
+ After user corrections, repeated test failures, or review feedback, have the agent write a candidate into the review inbox:
264
+
265
+ ```bash
266
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
267
+ Title: <durable pitfall>
268
+ Context: <when it triggers>
269
+ Mistake: <what the agent did wrong>
270
+ Fix: <what to do instead>
271
+ EOF
272
+ ```
273
+
274
+ Use `codetrap session status`, `codetrap session list`, `codetrap doctor`, or `codetrap web` to review pending candidates. Do not accept candidates automatically.
275
+
276
+ Use the returned `candidate_id` and `session_id` to inspect and resolve the candidate:
277
+
278
+ ```bash
279
+ codetrap session candidate <candidate-id> --session <session-id> --json
280
+
281
+ # Only after explicit human approval:
282
+ codetrap session accept <candidate-id> --session <session-id>
283
+
284
+ # Or reject instead:
285
+ codetrap session reject <candidate-id> --session <session-id> --reason "<reason>"
286
+ ```
287
+
288
+ ## Optional: Local Ollama Embeddings
289
+
290
+ codetrap works with no embedding provider. In that mode, search uses SQLite FTS keyword matching, and hybrid search falls back to FTS.
291
+
292
+ Recommended local semantic search uses Ollama with `qwen3-embedding:0.6b`. This keeps trap passages and query text on your machine.
293
+
294
+ Install Ollama, then pull the 0.6B embedding model:
295
+
296
+ ```bash
297
+ ollama pull qwen3-embedding:0.6b
252
298
  ```
253
299
 
254
- After `setx`, open a new PowerShell window.
300
+ Do not omit `:0.6b`; `qwen3-embedding:latest` is much larger.
255
301
 
256
- Verify that the key is visible without printing the secret:
302
+ Configure codetrap to use Ollama:
257
303
 
258
304
  ```bash
259
- bun -e 'console.log(process.env.JINA_API_KEY ? "has-key" : "no-key")'
305
+ codetrap embeddings use ollama
306
+ codetrap embeddings status
307
+ ```
308
+
309
+ This writes `~/.codetrap/config.json`. Environment variables such as `CODETRAP_EMBEDDING_PROVIDER` and `CODETRAP_OLLAMA_MODEL` are still supported for temporary shell or CI overrides.
310
+
311
+ Verify Ollama embedding generation:
312
+
313
+ ```bash
314
+ curl http://127.0.0.1:11434/api/embed -d '{"model":"qwen3-embedding:0.6b","input":"HTTP request timeout"}'
260
315
  ```
261
316
 
262
317
  Generate embeddings for the traps you want semantic search to use:
263
318
 
264
319
  ```bash
265
320
  cd /path/to/your/project
266
- codetrap embed --scope project
267
- codetrap embed --scope global
321
+ codetrap embeddings reindex --scope project
322
+ codetrap embeddings reindex --scope global
268
323
  ```
269
324
 
325
+ `codetrap embed` remains as a short alias for reindexing. codetrap stores embeddings by profile, so switching between Jina and Ollama does not overwrite existing vectors; it creates or refreshes the selected profile.
326
+
327
+ You can also run `codetrap web` and open the `Embeddings` view to inspect the active provider/profile, see project and global fresh/stale/missing counts, switch between Ollama and Jina, and reindex project or global embeddings from the web console. The web console does not save Jina API keys; Jina still reads `JINA_API_KEY` from the environment.
328
+
270
329
  Then search:
271
330
 
272
331
  ```bash
273
332
  codetrap search "HTTP request timeout" --mode hybrid
274
333
  ```
275
334
 
276
- If `JINA_API_KEY` is not set:
335
+ Optional cloud provider: run `codetrap embeddings use jina` and set `JINA_API_KEY` to use Jina instead of Ollama. Privacy note: codetrap does not collect telemetry. FTS and Ollama search are local-only. When Jina is configured, reindexing sends trap passages to Jina, and semantic or hybrid search may send query text to Jina to compute embeddings.
336
+
337
+ If no embedding provider is configured:
277
338
 
278
339
  - `codetrap search "<query>" --mode fts` works normally.
279
340
  - `codetrap search "<query>" --mode hybrid` works, but falls back to FTS.
280
- - `codetrap search "<query>" --mode semantic` and `codetrap embed` require `JINA_API_KEY`.
341
+ - `codetrap search "<query>" --mode semantic` and `codetrap embed` require an embedding provider.
281
342
 
282
343
  ## Optional: Codex MCP
283
344
 
@@ -295,36 +356,59 @@ codex mcp add codetrap -- "$(bun pm bin -g)/codetrap" serve
295
356
 
296
357
  Agents can also use the CLI directly from `AGENTS.md`:
297
358
 
298
- ```md
359
+ ````md
299
360
  Before non-trivial code edits, check codetrap from the current project cwd:
300
361
 
301
362
  codetrap search "<keywords>" --mode hybrid --json
302
363
 
303
- Review the top 3 action cards before deciding no trap applies. If a critical/error result is plausibly related, inspect it before editing:
364
+ Review the top 3 action cards, or all returned cards if fewer than 3, before deciding no trap applies. Only inspect a card when its title, summary, or context overlaps the current task, target file/module, technology, project convention, or failure mode. For matching critical/error results, inspect before editing:
304
365
 
305
366
  codetrap show <id> --scope <project|global> --json
306
367
 
368
+ Apply a trap only when its context matches the current task, file, module, or failure mode. Severity alone is not enough to apply a trap. Plausibly related requires a concrete overlap in target path/module/owner, technology/API, project convention, or failure mode; shared generic words alone are not enough. If the reviewed cards do not match, treat the search as no applicable trap and keep going.
369
+
307
370
  When editing a known area, pass applicability hints:
308
371
 
309
- codetrap search "<keywords>" --path src/db/repository.ts --module db --json
372
+ codetrap search "<keywords>" --path path/to/file --module module-name --json
373
+
374
+ To capture a post-flight lesson from agent work:
375
+
376
+ ```bash
377
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
378
+ Title: <durable pitfall>
379
+ Context: <when it triggers>
380
+ Mistake: <what the agent did wrong>
381
+ Fix: <what to do instead>
382
+ EOF
383
+ ```
310
384
 
311
- To add a lesson:
385
+ `--trap-json` remains available for callers that already have a structured object:
312
386
 
313
- codetrap add --json '{...}' --output-json
387
+ ```bash
388
+ codetrap session capture --trap-json '{...}' --kind review --json
389
+ ```
314
390
 
315
391
  For longer implementation work, keep temporary notes and explicit candidate traps in session files first:
316
392
 
317
393
  ```bash
318
394
  codetrap session start "<goal>"
319
395
  codetrap session note --kind decision --text "<what changed and why>"
320
- codetrap session note --kind review --text $'Title: <durable pitfall>\nContext: <when it triggers>\nMistake: <what the agent did wrong>\nFix: <what to do instead>'
396
+ cat <<'EOF' | codetrap session capture --trap-markdown - --kind review --json
397
+ Title: <durable pitfall>
398
+ Context: <when it triggers>
399
+ Mistake: <what the agent did wrong>
400
+ Fix: <what to do instead>
401
+ EOF
321
402
  codetrap session close --propose-traps
322
403
  codetrap session candidates
323
404
  ```
324
405
 
406
+ Pending candidates are visible from `codetrap session status`, `codetrap session list`, `codetrap doctor`, and `codetrap web`.
407
+
325
408
  Only accepted candidates are written to `traps.db`:
326
409
 
327
410
  ```bash
411
+ # Only after explicit human approval:
328
412
  codetrap session accept <candidate-id>
329
413
  ```
330
414
 
@@ -333,4 +417,4 @@ codetrap session accept <candidate-id>
333
417
  To save a lesson from an external article or reference, let the agent read the source and attach the URL as evidence after the user confirms the trap:
334
418
 
335
419
  codetrap add_trap_evidence <id> --scope global --source_type article --source_ref "https://example.com/post" --output-json
336
- ```
420
+ ````