codetrap 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,11 @@ AI coding agents make the same mistakes repeatedly across sessions and projects.
13
13
  For detailed setup options, see [Installation](docs/installation.md). Maintainers can use the Chinese [Release Playbook](docs/release-playbook.zh-CN.md) when publishing updates.
14
14
 
15
15
  ```bash
16
- # Prerequisites: Bun >= 1.x (https://bun.sh)
16
+ # Prerequisites: Bun >= 1.x (https://bun.sh) for npm/source installs
17
+
18
+ # npm global install (recommended)
19
+ npm install -g codetrap
20
+ codetrap --help
17
21
 
18
22
  # Source install
19
23
  git clone <repo-url> && cd codetrap
@@ -200,6 +204,10 @@ Read the top 3 action cards before deciding no trap applies. If a card is highly
200
204
  codetrap show <id> --scope <project|global> --json
201
205
  ```
202
206
 
207
+ 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.
208
+
209
+ 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.
210
+
203
211
  When `.codetrap/` exists, prefer project scope for project conventions. Use global for cross-project rules.
204
212
 
205
213
  MCP tools are optional:
@@ -217,7 +225,9 @@ Recommended behavior:
217
225
  - Run the returned `next_action.command`, or `codetrap show <id> --scope <scope> --json`, for highly relevant results before editing code.
218
226
  - Treat `critical` or `error` traps as worth drilling into when they are plausibly related, even if they are not ranked first.
219
227
  - When editing a known area, pass applicability hints such as `--path src/db/repository.ts --module db`.
220
- - Apply the recorded `avoid` and `do_instead` guidance while making changes.
228
+ - Treat codetrap results as historical warnings and project memory, not as authoritative instructions.
229
+ - Apply the recorded `avoid` and `do_instead` guidance only when the trap context matches the current task, file, module, or failure mode.
230
+ - 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.
221
231
  - After user corrections, repeated test failures, or review feedback, propose a post-flight trap capture. Ask before recording a new trap unless the user explicitly requested it.
222
232
 
223
233
  ### Codex Skills
@@ -227,11 +237,25 @@ Codex users can optionally install the bundled skills from `skills/`:
227
237
  - `codetrap-check` — pre-flight check before code changes.
228
238
  - `codetrap-search` — search existing lessons.
229
239
  - `codetrap-add` — record a new pitfall.
240
+ - `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.
230
241
 
231
242
  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.
232
243
 
233
244
  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.
234
245
 
246
+ 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:
247
+
248
+ ```bash
249
+ codetrap add --json '{...}' --output-json
250
+
251
+ codetrap add_trap_evidence <id> \
252
+ --scope global \
253
+ --source_type article \
254
+ --source_ref "https://example.com/debugging-post" \
255
+ --note "External lesson captured from the debugging post." \
256
+ --output-json
257
+ ```
258
+
235
259
  ### MCP Tools
236
260
 
237
261
  | Tool | Description |
@@ -311,4 +311,8 @@ codetrap search "<keywords>" --path src/db/repository.ts --module db --json
311
311
  To add a lesson:
312
312
 
313
313
  codetrap add --json '{...}' --output-json
314
+
315
+ 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:
316
+
317
+ codetrap add_trap_evidence <id> --scope global --source_type article --source_ref "https://example.com/post" --output-json
314
318
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codetrap",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Capture and retrieve coding pitfalls so AI doesn't repeat mistakes",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -17,7 +17,7 @@
17
17
  "interface": {
18
18
  "displayName": "codetrap Agent",
19
19
  "shortDescription": "Check local pitfall memory before code changes.",
20
- "longDescription": "Installs CLI-first guidance, optional MCP config, and example hooks so coding agents can search codetrap before risky edits and propose new trap captures after failures.",
20
+ "longDescription": "Installs CLI-first guidance, optional MCP config, and example hooks so coding agents can search codetrap before risky edits, propose new trap captures after failures, and save useful lessons from external references.",
21
21
  "developerName": "codetrap maintainers",
22
22
  "category": "Productivity",
23
23
  "capabilities": ["Tools", "Memory", "Code"],
@@ -27,7 +27,8 @@
27
27
  "defaultPrompt": [
28
28
  "Check codetrap before editing this code.",
29
29
  "Search prior pitfalls for this task.",
30
- "Propose a codetrap for this failure."
30
+ "Propose a codetrap for this failure.",
31
+ "Capture useful lessons from this article."
31
32
  ],
32
33
  "brandColor": "#2563EB"
33
34
  }
@@ -0,0 +1,19 @@
1
+ ---
2
+ name: codetrap-capture-external
3
+ description: Extract durable coding pitfalls from an external article, blog post, issue, paper, or reference, then save selected lessons to codetrap with source evidence after user confirmation.
4
+ ---
5
+
6
+ Use this when the user shares an external source and wants to save useful lessons for future AI coding work.
7
+
8
+ The agent should read the source. The codetrap CLI should not fetch URLs or crawl the web; it only stores confirmed lessons and evidence.
9
+
10
+ Workflow:
11
+
12
+ 1. Read the URL, article text, issue, paper, or reference.
13
+ 2. Extract every candidate trap that has a clear trigger, mistake, and fix. Do not force a fixed count.
14
+ 3. Filter out broad summaries, one-off facts, vague advice, and source details that will not change future coding behavior.
15
+ 4. Rank the recommended candidates and ask the user which ones to save.
16
+ 5. After confirmation, run `codetrap add --json '<trap-json>' --output-json`.
17
+ 6. Attach the source with `codetrap add_trap_evidence <id> --scope <project|global> --source_type article --source_ref "<url-or-source-id>" --note "External lesson captured from <short source title>." --output-json`.
18
+
19
+ Default to `global` for generally reusable engineering lessons. Use `project` only when the source lesson is specific to the current repository or stack.
@@ -11,4 +11,6 @@ codetrap search "<task keywords>" --mode hybrid --json
11
11
 
12
12
  Review the top 3 action cards. If a card is highly relevant, or has `critical` or `error` severity and is plausibly related, run its `next_action.command` before editing.
13
13
 
14
+ 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. 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.
15
+
14
16
  Use MCP only as an optional adapter. When calling MCP tools, pass `cwd` when the client supports it.
@@ -12,6 +12,10 @@ Review the top 3 action cards before deciding no trap applies. If a card is high
12
12
  codetrap show <id> --scope <project|global> --json
13
13
  ```
14
14
 
15
+ 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.
16
+
17
+ 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.
18
+
15
19
  When editing a specific area, pass applicability hints:
16
20
 
17
21
  ```bash
@@ -16,6 +16,10 @@ Ask the user to describe what went wrong. Guide them to provide:
16
16
 
17
17
  If the user already provided enough detail, don't re-ask — just proceed to structuring.
18
18
 
19
+ ## Quality gate
20
+
21
+ Only record stable lessons that are likely to change future AI behavior. Do not save unverified guesses, one-off logs, overly broad advice, or traps without a clear trigger and actionable fix. If the candidate is too vague, ask the user to clarify or suggest keeping it as a note instead of writing it to codetrap.
22
+
19
23
  ## Step 2: Determine scope
20
24
 
21
25
  Ask the user (or infer from context):
@@ -0,0 +1,62 @@
1
+ ---
2
+ name: codetrap-capture-external
3
+ description: Extract durable coding pitfalls from an external article, blog post, issue, paper, or reference, then save selected lessons to codetrap with source evidence after user confirmation.
4
+ ---
5
+
6
+ Use this when the user shares an external source and wants to save useful lessons for future AI coding work.
7
+
8
+ The external source is read by the agent. Do not ask codetrap CLI to fetch URLs or crawl the web. codetrap stays a local memory store.
9
+
10
+ ## Step 1: Read The Source
11
+
12
+ Open or read the provided URL, article text, issue, paper, or reference. Identify lessons that could change future implementation behavior.
13
+
14
+ Do not summarize the whole source into codetrap. Extract only durable pitfalls with a clear trigger, mistake, and fix.
15
+
16
+ ## Step 2: Extract Candidate Traps
17
+
18
+ Create as many candidate traps as pass the quality bar. Do not force a fixed count.
19
+
20
+ Each candidate must include:
21
+
22
+ - `context`: when this lesson applies
23
+ - `mistake`: what an AI coding agent might do wrong
24
+ - `fix`: what it should do instead
25
+ - `severity`: `warning`, `error`, or `critical`
26
+ - `tags`: useful retrieval terms
27
+ - optional `path_globs`, `module`, and `owner` when the lesson is project-specific
28
+
29
+ Reject or omit candidates that are broad summaries, one-off facts, vague advice, marketing claims, or source details that would not change future coding behavior.
30
+
31
+ ## Step 3: Rank And Ask
32
+
33
+ Present the recommended candidates in priority order. Include a short reason for each recommendation.
34
+
35
+ Ask the user which candidates to save. Do not write any trap until the user confirms.
36
+
37
+ If a candidate is useful but needs a narrower scope, ask for or propose edits before saving.
38
+
39
+ ## Step 4: Save Confirmed Lessons
40
+
41
+ For each confirmed candidate, call:
42
+
43
+ ```bash
44
+ codetrap add --json '<trap-json>' --output-json
45
+ ```
46
+
47
+ Then attach the external source as evidence:
48
+
49
+ ```bash
50
+ codetrap add_trap_evidence <id> \
51
+ --scope <project|global> \
52
+ --source_type article \
53
+ --source_ref "<url-or-source-id>" \
54
+ --note "External lesson captured from <short source title>." \
55
+ --output-json
56
+ ```
57
+
58
+ Use `global` for generally reusable lessons across projects. Use `project` only when the lesson is specific to the current repository or technology stack.
59
+
60
+ ## Step 5: Confirm
61
+
62
+ Tell the user which trap IDs were saved, their scopes, and the source reference attached as evidence.
@@ -45,10 +45,12 @@ MCP `search_traps` is optional. Use it only when it is already available and pro
45
45
 
46
46
  Review the top 3 returned action cards before deciding that no trap applies. Do not stop after only the first result; relevant traps may rank second or third. If fewer than 3 cards are returned, review all returned cards.
47
47
 
48
+ 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. 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.
49
+
48
50
  ## Step 3: Apply the lessons
49
51
 
50
52
  For each relevant trap found in the reviewed top cards:
51
- 1. Read the action card's `avoid` and `do_instead`
53
+ 1. Confirm the trap context matches the current task, file, module, or failure mode
52
54
  2. If the card is highly relevant, or has `critical`/`error` severity and is plausibly related, and you are about to edit code, run `next_action.command` from CLI JSON; with MCP, call `get_trap` with `next_action.details_args.id` and `next_action.details_args.scope`
53
55
  3. Adjust your code generation to follow the correct approach
54
56
  4. If a trap matches exactly what you were about to do, explicitly tell the user: "I was about to [avoid], but the codetrap database says [do_instead]. I'll do it the right way."
@@ -48,11 +48,13 @@ search_traps(query="<keywords>", scope=<optional>, category=<optional>, path=<op
48
48
 
49
49
  Review the top 3 action cards before deciding that no trap applies. Do not rely only on the first result; a relevant trap can rank second or third. If fewer than 3 cards are returned, review all returned cards.
50
50
 
51
+ 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. 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.
52
+
51
53
  ## How to present results
52
54
 
53
55
  1. Show the most relevant reviewed traps first (project scope traps before global)
54
56
  2. Summarize each reviewed card's title, severity, `avoid`, and `do_instead`
55
- 3. If any reviewed card is highly relevant, or has `critical`/`error` severity and is plausibly related, and you are about to edit code, run the CLI `next_action.command`; with MCP, call `get_trap` with the card's `id` and `scope` before proceeding
57
+ 3. If any reviewed card is highly relevant, has matching context, or has `critical`/`error` severity and is plausibly related, and you are about to edit code, run the CLI `next_action.command`; with MCP, call `get_trap` with the card's `id` and `scope` before proceeding
56
58
  4. If no results, tell the user (this is a new area with no recorded pitfalls yet)
57
59
 
58
60
  ## Example
@@ -2,7 +2,6 @@ import { readFileSync } from "node:fs";
2
2
  import { TrapStore } from "../lib/store";
3
3
  import { formatTrapShort, formatTrapDetails, formatTrapActionCard } from "../lib/format";
4
4
  import type { Trap } from "../domain/trap";
5
- import { SEARCH_MODES, type SearchMode } from "../lib/constants";
6
5
  import {
7
6
  formatScopeMigrationText,
8
7
  runScopeMigration,
@@ -24,6 +23,13 @@ import {
24
23
  type CommandResult,
25
24
  } from "./command-result";
26
25
  import { mutationJsonPayload } from "../lib/trap-mutation-result";
26
+ import {
27
+ embedRequestFromArgs,
28
+ evidenceRequestFromArgs,
29
+ listRequestFromArgs,
30
+ searchRequestFromArgs,
31
+ statsRequestFromArgs,
32
+ } from "../lib/command-requests";
27
33
 
28
34
  type ParsedArgs = {
29
35
  opts: Record<string, string>;
@@ -141,21 +147,8 @@ async function cmdSearch(args: string[], operations: TrapOperations): Promise<Co
141
147
  }
142
148
 
143
149
  try {
144
- const mode = opts.mode ? parseSearchMode(opts.mode) : undefined;
145
150
  const defaults = searchDefaultsFromConfig();
146
- const cards = await operations.searchTrapCards({
147
- query,
148
- category: opts.category,
149
- scope: opts.scope ?? defaults.scope,
150
- limit: opts.limit ? parseOptionalInt(opts.limit) : defaults.limit,
151
- mode: mode ?? defaults.mode,
152
- status: opts.status,
153
- path: opts.path,
154
- module: opts.module,
155
- owner: opts.owner,
156
- rerank: opts["no-rerank"] !== undefined ? false : defaults.rerank,
157
- includeRankingSignals: opts["ranking-signals"] !== undefined,
158
- });
151
+ const cards = await operations.searchTrapCards(searchRequestFromArgs(query, opts, defaults));
159
152
  if (opts.json !== undefined) return jsonResult(toCliSearchJson(cards));
160
153
  return textResult(cards.length > 0 ? cards.map(formatTrapActionCard).join("\n\n") : "No traps found.");
161
154
  } catch (error) {
@@ -166,15 +159,7 @@ async function cmdSearch(args: string[], operations: TrapOperations): Promise<Co
166
159
  function cmdList(args: string[], operations: TrapOperations): CommandResult {
167
160
  const { opts } = parseArgs(args);
168
161
  try {
169
- const groups = operations.listTraps({
170
- category: opts.category,
171
- scope: opts.scope,
172
- status: opts.status,
173
- path: opts.path,
174
- module: opts.module,
175
- owner: opts.owner,
176
- limit: parseOptionalInt(opts.limit, 50),
177
- });
162
+ const groups = operations.listTraps(listRequestFromArgs(opts));
178
163
  if (opts.json !== undefined) return jsonResult(toListJson(groups));
179
164
 
180
165
  const lines = groups.flatMap((group) =>
@@ -241,18 +226,12 @@ function cmdAddTrapEvidence(args: string[], operations: TrapOperations): Command
241
226
  const { opts, positionals } = parseArgs(args);
242
227
  const id = parseId(
243
228
  positionals[0],
244
- "Usage: codetrap add_trap_evidence <id> --source_type manual|conversation|commit|issue|test_failure [--scope project|global] [--source_ref X] [--related_files a,b] [--note X]"
229
+ "Usage: codetrap add_trap_evidence <id> --source_type manual|conversation|commit|issue|test_failure|article [--scope project|global] [--source_ref X] [--related_files a,b] [--note X]"
245
230
  );
246
231
  if (typeof id !== "number") return id;
247
232
 
248
233
  try {
249
- const input = opts.json ? JSON.parse(opts.json) : {
250
- source_type: opts.source_type ?? opts["source-type"],
251
- source_ref: opts.source_ref ?? opts["source-ref"],
252
- observed_at: opts.observed_at ?? opts["observed-at"],
253
- related_files: parseCsv(opts.related_files ?? opts["related-files"]),
254
- note: opts.note,
255
- };
234
+ const input = opts.json ? JSON.parse(opts.json) : evidenceRequestFromArgs(opts);
256
235
  const result = operations.addTrapEvidence(id, input, opts.scope);
257
236
  if (opts["output-json"] !== undefined) {
258
237
  return mutationJsonResult({ id, ...result }, `Trap #${id} not found.`);
@@ -331,8 +310,9 @@ function cmdImport(args: string[], operations: TrapOperations): CommandResult {
331
310
 
332
311
  function cmdStats(args: string[], operations: TrapOperations): CommandResult {
333
312
  const { opts } = parseArgs(args);
334
- const stats = operations.getStats();
335
- const embeddingStats = operations.getEmbeddingStats();
313
+ const request = statsRequestFromArgs(opts);
314
+ const stats = operations.getStats(request.scope);
315
+ const embeddingStats = operations.getEmbeddingStats(request.scope);
336
316
  return opts.json !== undefined
337
317
  ? jsonResult(toStatsJson(stats, embeddingStats))
338
318
  : textResult(formatStatsText(stats));
@@ -378,13 +358,7 @@ function cmdScopeMigration(
378
358
  async function cmdEmbed(args: string[], store: TrapStore): Promise<CommandResult> {
379
359
  const { opts } = parseArgs(args);
380
360
  try {
381
- const result = await store.ensureEmbeddings({
382
- scope: opts.scope,
383
- category: opts.category,
384
- limit: opts.limit ? parseOptionalInt(opts.limit) : undefined,
385
- force: opts.force === "true",
386
- batchSize: opts["batch-size"] ? parseOptionalInt(opts["batch-size"]) : undefined,
387
- });
361
+ const result = await store.ensureEmbeddings(embedRequestFromArgs(opts));
388
362
  return textResult([
389
363
  ...result.scopes.map((scoped) =>
390
364
  `[${scoped.scope}] embeddings generated: ${scoped.generated}, skipped: ${scoped.skipped}, batches: ${scoped.batches}`
@@ -401,7 +375,9 @@ function formatStatsText(stats: ReturnType<TrapOperations["getStats"]>): string
401
375
  if (stats.project) {
402
376
  sections.push("── Project ──", formatStatsBlock(stats.project));
403
377
  }
404
- sections.push("── Global ──", formatStatsBlock(stats.global));
378
+ if (stats.global) {
379
+ sections.push("── Global ──", formatStatsBlock(stats.global));
380
+ }
405
381
  return sections.join("\n");
406
382
  }
407
383
 
@@ -415,41 +391,18 @@ function formatStatsBlock(stats: { total: number; byCategory: Record<string, num
415
391
  ].join("\n");
416
392
  }
417
393
 
418
- function parseSearchMode(mode: string): SearchMode {
419
- if ((SEARCH_MODES as readonly string[]).includes(mode)) return mode as SearchMode;
420
- throw new Error(`Invalid search mode: ${mode}. Expected one of: ${SEARCH_MODES.join(", ")}`);
421
- }
422
-
423
394
  function parseId(value: string | undefined, usage: string): number | CommandResult {
424
395
  if (value === undefined) return errorResult(usage);
425
396
  const id = Number.parseInt(value, 10);
426
397
  return Number.isNaN(id) ? errorResult("Error: id must be a number") : id;
427
398
  }
428
399
 
429
- function parseOptionalInt(value: string | undefined, fallback?: number): number {
430
- if (value === undefined) {
431
- if (fallback === undefined) throw new Error("Missing numeric value.");
432
- return fallback;
433
- }
434
- const parsed = Number.parseInt(value, 10);
435
- if (Number.isNaN(parsed)) throw new Error(`Invalid number: ${value}`);
436
- return parsed;
437
- }
438
-
439
400
  function readQuery(positionals: string[]): string {
440
401
  if (positionals.length > 0) return positionals.join(" ").trim();
441
402
  if (process.stdin.isTTY) return "";
442
403
  return readFileSync(0, "utf-8").trim();
443
404
  }
444
405
 
445
- function parseCsv(value?: string): string[] | undefined {
446
- if (!value) return undefined;
447
- return value
448
- .split(",")
449
- .map((item) => item.trim())
450
- .filter(Boolean);
451
- }
452
-
453
406
  function mutationJsonResult<T extends Record<string, unknown> & { success: boolean }>(
454
407
  value: T,
455
408
  error: string
package/src/db/queries.ts CHANGED
@@ -180,6 +180,10 @@ export function listTrapEvidence(db: Database, trapId: number): TrapEvidence[] {
180
180
  }
181
181
 
182
182
  export function archiveTrap(db: Database, id: number): boolean {
183
+ return markTrapArchived(db, id);
184
+ }
185
+
186
+ export function markTrapArchived(db: Database, id: number): boolean {
183
187
  const result = db
184
188
  .prepare(
185
189
  `
@@ -208,33 +212,43 @@ export function supersedeTrap(
208
212
 
209
213
  const key = stateKey ?? oldTrap.state_key ?? newTrap.state_key ?? `trap:${id}`;
210
214
  const tx = db.transaction(() => {
211
- db.prepare(
212
- `
213
- UPDATE traps
214
- SET status = 'superseded',
215
- state_key = ?,
216
- valid_until = COALESCE(valid_until, datetime('now')),
217
- updated_at = datetime('now')
218
- WHERE id = ?
219
- `
220
- ).run(key, id);
221
- db.prepare(
222
- `
223
- UPDATE traps
224
- SET status = 'active',
225
- state_key = ?,
226
- supersedes_id = ?,
227
- valid_from = COALESCE(valid_from, datetime('now')),
228
- valid_until = NULL,
229
- updated_at = datetime('now')
230
- WHERE id = ?
231
- `
232
- ).run(key, id, supersededById);
215
+ markTrapSuperseded(db, id, key);
216
+ markTrapSuperseding(db, supersededById, id, key);
233
217
  });
234
218
  tx();
235
219
  return true;
236
220
  }
237
221
 
222
+ export function markTrapSuperseded(db: Database, id: number, stateKey: string): boolean {
223
+ const result = db.prepare(supersedeTrapSql).run(stateKey, id);
224
+ return result.changes > 0;
225
+ }
226
+
227
+ export function markTrapSuperseding(db: Database, id: number, supersedesId: number, stateKey: string): boolean {
228
+ const result = db.prepare(supersedingTrapSql).run(stateKey, supersedesId, id);
229
+ return result.changes > 0;
230
+ }
231
+
232
+ const supersedeTrapSql = `
233
+ UPDATE traps
234
+ SET status = 'superseded',
235
+ state_key = ?,
236
+ valid_until = COALESCE(valid_until, datetime('now')),
237
+ updated_at = datetime('now')
238
+ WHERE id = ?
239
+ `;
240
+
241
+ const supersedingTrapSql = `
242
+ UPDATE traps
243
+ SET status = 'active',
244
+ state_key = ?,
245
+ supersedes_id = ?,
246
+ valid_from = COALESCE(valid_from, datetime('now')),
247
+ valid_until = NULL,
248
+ updated_at = datetime('now')
249
+ WHERE id = ?
250
+ `;
251
+
238
252
  export function incrementHitCount(db: Database, id: number): void {
239
253
  db.prepare("UPDATE traps SET hit_count = hit_count + 1, updated_at = datetime('now') WHERE id = ?").run(id);
240
254
  }
@@ -245,18 +259,22 @@ export function getTopTraps(db: Database, scope: string, limit = 20): Trap[] {
245
259
  .all(scope, limit) as Trap[];
246
260
  }
247
261
 
248
- export function getStats(db: Database): {
262
+ export function getStats(db: Database, opts: { scope?: string; status?: TrapStatusFilter } = {}): {
249
263
  total: number;
250
264
  byCategory: Record<string, number>;
251
265
  bySeverity: Record<string, number>;
252
266
  } {
253
- const total = (db.query("SELECT COUNT(*) as c FROM traps").get() as { c: number }).c;
267
+ const conditions: string[] = [];
268
+ const params: SQLQueryBindings[] = [];
269
+ addTrapFilters(conditions, params, opts);
270
+ const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
271
+ const total = (db.query(`SELECT COUNT(*) as c FROM traps ${where}`).get(...params) as { c: number }).c;
254
272
  const byCategory = db
255
- .query("SELECT category, COUNT(*) as c FROM traps GROUP BY category")
256
- .all() as { category: string; c: number }[];
273
+ .query(`SELECT category, COUNT(*) as c FROM traps ${where} GROUP BY category`)
274
+ .all(...params) as { category: string; c: number }[];
257
275
  const bySeverity = db
258
- .query("SELECT severity, COUNT(*) as c FROM traps GROUP BY severity")
259
- .all() as { severity: string; c: number }[];
276
+ .query(`SELECT severity, COUNT(*) as c FROM traps ${where} GROUP BY severity`)
277
+ .all(...params) as { severity: string; c: number }[];
260
278
 
261
279
  return {
262
280
  total,
@@ -8,7 +8,6 @@ import type {
8
8
  TrapSearchResult,
9
9
  TrapUpdate,
10
10
  } from "../domain/trap";
11
- import * as embeddingQueries from "./embedding-queries";
12
11
  import { SearchService, type SearchOptions } from "../lib/search-service";
13
12
  import {
14
13
  type EmbeddingConfig,
@@ -20,20 +19,24 @@ import { passageFieldsChanged } from "../lib/trap-search-document";
20
19
  import * as queries from "./queries";
21
20
  import type { TrapStatus } from "../lib/constants";
22
21
  import { TrapSearchPolicy } from "../lib/search-policy";
22
+ import { DatabaseEmbeddingIndex } from "../lib/embedding-index";
23
+ import { archiveTrapLifecycle, supersedeTrapLifecycle } from "../lib/trap-lifecycle";
23
24
 
24
25
  export type TrapStats = ReturnType<typeof queries.getStats>;
25
- export type EmbeddingStateCounts = ReturnType<typeof embeddingQueries.getEmbeddingStateCounts>;
26
+ export type EmbeddingStateCounts = ReturnType<DatabaseEmbeddingIndex["stateCounts"]>;
26
27
  export type TrapRecordInsert = queries.TrapRecordInsert;
27
28
 
28
29
  export class TrapRepository {
29
30
  private readonly searchService: SearchService;
30
31
  private readonly searchPolicy = new TrapSearchPolicy();
32
+ private readonly embeddingIndex: DatabaseEmbeddingIndex;
31
33
 
32
34
  constructor(
33
35
  private readonly db: Database,
34
36
  private readonly embedder?: EmbeddingProvider
35
37
  ) {
36
38
  this.searchService = new SearchService(db, embedder);
39
+ this.embeddingIndex = new DatabaseEmbeddingIndex(db);
37
40
  }
38
41
 
39
42
  add(input: TrapInput): number {
@@ -67,10 +70,16 @@ export class TrapRepository {
67
70
  .slice(0, limit);
68
71
  }
69
72
 
73
+ listMisScoped(expectedScope: string): Trap[] {
74
+ return queries
75
+ .listTraps(this.db, { status: "all", limit: 100000 })
76
+ .filter((trap) => trap.scope !== expectedScope);
77
+ }
78
+
70
79
  update(id: number, input: TrapUpdate): boolean {
71
80
  const success = queries.updateTrap(this.db, id, input);
72
81
  if (success && passageFieldsChanged(input)) {
73
- embeddingQueries.deleteEmbedding(this.db, id);
82
+ this.embeddingIndex.delete(id);
74
83
  }
75
84
  return success;
76
85
  }
@@ -85,11 +94,11 @@ export class TrapRepository {
85
94
  }
86
95
 
87
96
  archive(id: number): boolean {
88
- return queries.archiveTrap(this.db, id);
97
+ return archiveTrapLifecycle(this.lifecycleAdapter(), id);
89
98
  }
90
99
 
91
100
  supersede(id: number, supersededById: number, stateKey?: string): boolean {
92
- return queries.supersedeTrap(this.db, id, supersededById, stateKey);
101
+ return supersedeTrapLifecycle(this.lifecycleAdapter(), id, supersededById, stateKey);
93
102
  }
94
103
 
95
104
  hit(id: number): void {
@@ -100,12 +109,12 @@ export class TrapRepository {
100
109
  return queries.getTopTraps(this.db, scope, limit);
101
110
  }
102
111
 
103
- stats(): TrapStats {
104
- return queries.getStats(this.db);
112
+ stats(opts: { scope?: string; status?: TrapStatus | "all" } = {}): TrapStats {
113
+ return queries.getStats(this.db, opts);
105
114
  }
106
115
 
107
- embeddingStats(config: EmbeddingConfig | null): EmbeddingStateCounts {
108
- return embeddingQueries.getEmbeddingStateCounts(this.db, config);
116
+ embeddingStats(config: EmbeddingConfig | null, opts: { scope?: string; status?: TrapStatus | "all" } = {}): EmbeddingStateCounts {
117
+ return this.embeddingIndex.stateCounts(config, opts);
109
118
  }
110
119
 
111
120
  exportAll(): TrapExportRecord[] {
@@ -137,22 +146,22 @@ export class TrapRepository {
137
146
  }
138
147
 
139
148
  getEmbedding(trapId: number): StoredEmbedding | null {
140
- return embeddingQueries.getEmbedding(this.db, trapId);
149
+ return this.embeddingIndex.get(trapId);
141
150
  }
142
151
 
143
152
  upsertEmbedding(record: StoredEmbedding): void {
144
- embeddingQueries.upsertEmbedding(this.db, record);
153
+ this.embeddingIndex.save(record);
145
154
  }
146
155
 
147
156
  deleteEmbedding(trapId: number): void {
148
- embeddingQueries.deleteEmbedding(this.db, trapId);
157
+ this.embeddingIndex.delete(trapId);
149
158
  }
150
159
 
151
160
  getTrapsNeedingEmbeddings(
152
161
  config: EmbeddingConfig,
153
162
  opts: { scope?: string; category?: string; status?: TrapStatus | "all"; force?: boolean; limit?: number } = {}
154
163
  ): Trap[] {
155
- return embeddingQueries.getTrapsNeedingEmbeddings(this.db, config, opts);
164
+ return this.embeddingIndex.trapsNeedingEmbeddings(config, opts);
156
165
  }
157
166
 
158
167
  async ensureEmbeddings(opts: { scope?: string; category?: string; limit?: number; force?: boolean; batchSize?: number } = {}): Promise<{
@@ -166,13 +175,24 @@ export class TrapRepository {
166
175
 
167
176
  return runEmbeddingJob(
168
177
  {
169
- countEmbeddable: (countOpts) => embeddingQueries.countEmbeddableTraps(this.db, countOpts),
178
+ countEmbeddable: (countOpts) => this.embeddingIndex.countEmbeddable(countOpts),
170
179
  trapsNeedingEmbeddings: (config, jobOpts) =>
171
- embeddingQueries.getTrapsNeedingEmbeddings(this.db, config, jobOpts),
172
- saveEmbedding: (record) => embeddingQueries.upsertEmbedding(this.db, record),
180
+ this.embeddingIndex.trapsNeedingEmbeddings(config, jobOpts),
181
+ saveEmbedding: (record) => this.embeddingIndex.save(record),
173
182
  },
174
183
  this.embedder,
175
184
  opts
176
185
  );
177
186
  }
187
+
188
+ private lifecycleAdapter() {
189
+ return {
190
+ get: (id: number) => queries.getTrap(this.db, id),
191
+ transaction: <T>(callback: () => T) => this.transaction(callback),
192
+ markArchived: (id: number) => queries.markTrapArchived(this.db, id),
193
+ markSuperseded: (id: number, stateKey: string) => queries.markTrapSuperseded(this.db, id, stateKey),
194
+ markSuperseding: (id: number, supersedesId: number, stateKey: string) =>
195
+ queries.markTrapSuperseding(this.db, id, supersedesId, stateKey),
196
+ };
197
+ }
178
198
  }