claude-db 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +17 -2
  2. package/dist/capture/flush.d.ts +22 -0
  3. package/dist/capture/flush.js +45 -10
  4. package/dist/capture/identity.d.ts +6 -0
  5. package/dist/capture/identity.js +14 -0
  6. package/dist/capture/index.d.ts +5 -3
  7. package/dist/capture/index.js +4 -3
  8. package/dist/capture/manual.d.ts +21 -0
  9. package/dist/capture/manual.js +40 -0
  10. package/dist/capture/transcript.d.ts +12 -0
  11. package/dist/capture/transcript.js +69 -1
  12. package/dist/capture/turn-extractor.js +40 -8
  13. package/dist/cli/index.js +376 -30
  14. package/dist/cli/install.d.ts +15 -1
  15. package/dist/cli/install.js +127 -8
  16. package/dist/config/schema.d.ts +1 -0
  17. package/dist/config/schema.js +9 -1
  18. package/dist/context.js +24 -2
  19. package/dist/embed/builtin.js +3 -1
  20. package/dist/hooks/relevance.js +11 -4
  21. package/dist/hooks/session-end.js +2 -1
  22. package/dist/hooks/session-start.js +22 -3
  23. package/dist/mcp/server.js +56 -6
  24. package/dist/search/service.js +4 -1
  25. package/dist/store/adapter.d.ts +22 -6
  26. package/dist/store/adapter.js +10 -1
  27. package/dist/store/mongo/driver.d.ts +1 -0
  28. package/dist/store/mongo/index.d.ts +3 -2
  29. package/dist/store/mongo/index.js +57 -7
  30. package/dist/store/postgres/index.d.ts +22 -8
  31. package/dist/store/postgres/index.js +165 -21
  32. package/dist/store/sqlite/index.d.ts +3 -2
  33. package/dist/store/sqlite/index.js +112 -9
  34. package/dist/store/sqlite/schema.sql +4 -3
  35. package/dist/types.d.ts +28 -0
  36. package/dist/util/project.d.ts +9 -10
  37. package/dist/util/project.js +52 -13
  38. package/dist/util/vector.d.ts +11 -1
  39. package/dist/util/vector.js +14 -3
  40. package/package.json +2 -2
@@ -1,5 +1,6 @@
1
1
  PRAGMA journal_mode = WAL;
2
2
  PRAGMA synchronous = NORMAL;
3
+ PRAGMA recursive_triggers = ON;
3
4
 
4
5
  CREATE TABLE IF NOT EXISTS sessions (
5
6
  id TEXT PRIMARY KEY,
@@ -16,8 +17,6 @@ CREATE TABLE IF NOT EXISTS observations (
16
17
  id TEXT PRIMARY KEY,
17
18
  session_id TEXT NOT NULL,
18
19
  project TEXT NOT NULL,
19
- -- Single opaque token identifying the project, indexed inside FTS so that
20
- -- scoping happens during the match rather than as a post-filter join.
21
20
  scope TEXT NOT NULL DEFAULT '',
22
21
  kind TEXT NOT NULL,
23
22
  title TEXT NOT NULL,
@@ -25,7 +24,9 @@ CREATE TABLE IF NOT EXISTS observations (
25
24
  files TEXT NOT NULL DEFAULT '[]',
26
25
  tags TEXT NOT NULL DEFAULT '[]',
27
26
  created_at INTEGER NOT NULL,
28
- embedding BLOB
27
+ embedding BLOB,
28
+ embedder TEXT,
29
+ author TEXT
29
30
  );
30
31
 
31
32
  CREATE INDEX IF NOT EXISTS idx_obs_project_time
package/dist/types.d.ts CHANGED
@@ -27,6 +27,10 @@ export interface Observation {
27
27
  createdAt: number;
28
28
  /** Present only when an embedder is configured. */
29
29
  embedding?: number[];
30
+ /** Which embedder produced `embedding`. Absent on rows written before 0.2. */
31
+ embedder?: string;
32
+ /** Whose machine recorded this. Only interesting on a shared database. */
33
+ author?: string;
30
34
  }
31
35
  /** Compact projection returned by search. Deliberately excludes `body`. */
32
36
  export interface ObservationIndexEntry {
@@ -46,6 +50,30 @@ export interface SearchQuery {
46
50
  limit: number;
47
51
  /** Ceiling for brute-force vector scans. Ignored by indexed backends. */
48
52
  maxScanCandidates?: number;
53
+ /**
54
+ * Restricts vector search to rows embedded by this model, plus rows that
55
+ * predate the column. Vectors from different models are not comparable.
56
+ */
57
+ embedder?: string;
58
+ }
59
+ /**
60
+ * What to delete. Every field narrows, and an empty filter means everything,
61
+ * which is what `reset` asks for. An explicitly empty `ids` array deletes
62
+ * nothing: `forget` with no resolvable id must not wipe the database.
63
+ */
64
+ export interface RemoveFilter {
65
+ ids?: string[];
66
+ project?: string;
67
+ kind?: ObservationKind;
68
+ /** Deletes observations recorded strictly before this timestamp. */
69
+ before?: number;
70
+ }
71
+ /** Bulk read, ordered oldest first so `after` can page through a large table. */
72
+ export interface ListFilter {
73
+ project?: string;
74
+ kind?: ObservationKind;
75
+ after?: number;
76
+ limit?: number;
49
77
  }
50
78
  export interface TimelineQuery {
51
79
  /** Anchor observation. Results are the ones surrounding it in time. */
@@ -1,14 +1,13 @@
1
1
  /**
2
- * Canonicalises a project path so scoping survives symlinks.
2
+ * Canonicalises a project path so scoping survives symlinks, then widens it to
3
+ * the repository root so every directory inside a repo keys the same memory.
3
4
  *
4
- * Project scoping is an exact string comparison, so two spellings of the same
5
- * directory silently split a project's memory in half. macOS makes this the
6
- * default case rather than an edge case: `/tmp` is a symlink to `/private/tmp`
7
- * and `/var` to `/private/var`, so a hook receiving `cwd` from the agent and a
8
- * CLI reading `process.cwd()` disagree about the same folder.
9
- *
10
- * Resolving to the real path makes every entry point agree. Falls back to the
11
- * absolute path when the directory does not exist yet, which keeps callers
12
- * working against paths that have not been created.
5
+ * Scoping is an exact string comparison, so two spellings of the same place
6
+ * silently split a project's memory in half. macOS makes symlinks the default
7
+ * case rather than an edge case: `/tmp` links to `/private/tmp`, so a hook
8
+ * reading `cwd` and a CLI reading `process.cwd()` disagree about one folder.
9
+ * Subdirectories are the same failure by a different route: running the agent
10
+ * from `repo/frontend` used to start a second, empty memory, indistinguishable
11
+ * from never having worked in the repo at all.
13
12
  */
14
13
  export declare function resolveProject(path: string | undefined): string;
@@ -1,24 +1,63 @@
1
- import { realpathSync } from 'node:fs';
2
- import { resolve } from 'node:path';
1
+ import { existsSync, realpathSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { dirname, join, resolve, sep } from 'node:path';
3
4
  /**
4
- * Canonicalises a project path so scoping survives symlinks.
5
+ * Canonicalises a project path so scoping survives symlinks, then widens it to
6
+ * the repository root so every directory inside a repo keys the same memory.
5
7
  *
6
- * Project scoping is an exact string comparison, so two spellings of the same
7
- * directory silently split a project's memory in half. macOS makes this the
8
- * default case rather than an edge case: `/tmp` is a symlink to `/private/tmp`
9
- * and `/var` to `/private/var`, so a hook receiving `cwd` from the agent and a
10
- * CLI reading `process.cwd()` disagree about the same folder.
11
- *
12
- * Resolving to the real path makes every entry point agree. Falls back to the
13
- * absolute path when the directory does not exist yet, which keeps callers
14
- * working against paths that have not been created.
8
+ * Scoping is an exact string comparison, so two spellings of the same place
9
+ * silently split a project's memory in half. macOS makes symlinks the default
10
+ * case rather than an edge case: `/tmp` links to `/private/tmp`, so a hook
11
+ * reading `cwd` and a CLI reading `process.cwd()` disagree about one folder.
12
+ * Subdirectories are the same failure by a different route: running the agent
13
+ * from `repo/frontend` used to start a second, empty memory, indistinguishable
14
+ * from never having worked in the repo at all.
15
15
  */
16
16
  export function resolveProject(path) {
17
17
  const absolute = resolve(path ?? process.cwd());
18
+ let canonical;
18
19
  try {
19
- return realpathSync(absolute);
20
+ canonical = realpathSync(absolute);
20
21
  }
21
22
  catch {
22
23
  return absolute;
23
24
  }
25
+ return repoRoot(canonical) ?? canonical;
26
+ }
27
+ /**
28
+ * Nearest ancestor containing a `.git` entry, or null outside a work tree.
29
+ *
30
+ * Never returns the home directory or anything above it. People keep dotfiles
31
+ * repositories in `$HOME`, and without that guard every unrelated project on
32
+ * the machine would walk up, find `~/.git`, and collapse into a single shared
33
+ * memory. A directory that merely holds several repositories is not a repo
34
+ * itself, so it keeps its own key and its contents stay pooled.
35
+ *
36
+ * `.git` is a file rather than a directory in worktrees and submodules, so
37
+ * this tests for existence, not for a directory.
38
+ */
39
+ function repoRoot(start) {
40
+ const home = canonicalHome();
41
+ for (let dir = start;;) {
42
+ if (dir === sep || dir === home || home.startsWith(dir + sep))
43
+ return null;
44
+ if (existsSync(join(dir, '.git')))
45
+ return dir;
46
+ const parent = dirname(dir);
47
+ if (parent === dir)
48
+ return null;
49
+ dir = parent;
50
+ }
51
+ }
52
+ let cachedHome = null;
53
+ function canonicalHome() {
54
+ if (cachedHome === null) {
55
+ try {
56
+ cachedHome = realpathSync(homedir());
57
+ }
58
+ catch {
59
+ cachedHome = homedir();
60
+ }
61
+ }
62
+ return cachedHome;
24
63
  }
@@ -1,5 +1,15 @@
1
1
  /** Serialize an embedding for BLOB columns / BSON binary. */
2
2
  export declare function packVector(values: number[]): Buffer;
3
3
  export declare function unpackVector(buffer: Buffer): number[];
4
- /** Assumes both inputs are L2-normalized, which the embedder guarantees. */
4
+ /**
5
+ * Assumes both inputs are L2-normalized, which the embedder guarantees.
6
+ *
7
+ * Different widths are different embedding spaces, and a database accumulates
8
+ * both the moment `provider: auto` upgrades from the 256d builtin to 384d
9
+ * MiniLM. Scoring zero drops those below the relevance floor instead of
10
+ * comparing a prefix and returning a meaningless number.
11
+ *
12
+ * ponytail: width stands in for model identity; store an embedder id per row
13
+ * if two same-size models ever coexist.
14
+ */
5
15
  export declare function cosine(a: number[], b: number[]): number;
@@ -7,11 +7,22 @@ export function unpackVector(buffer) {
7
7
  const copy = Buffer.from(buffer);
8
8
  return Array.from(new Float32Array(copy.buffer, copy.byteOffset, copy.byteLength / 4));
9
9
  }
10
- /** Assumes both inputs are L2-normalized, which the embedder guarantees. */
10
+ /**
11
+ * Assumes both inputs are L2-normalized, which the embedder guarantees.
12
+ *
13
+ * Different widths are different embedding spaces, and a database accumulates
14
+ * both the moment `provider: auto` upgrades from the 256d builtin to 384d
15
+ * MiniLM. Scoring zero drops those below the relevance floor instead of
16
+ * comparing a prefix and returning a meaningless number.
17
+ *
18
+ * ponytail: width stands in for model identity; store an embedder id per row
19
+ * if two same-size models ever coexist.
20
+ */
11
21
  export function cosine(a, b) {
12
- const len = Math.min(a.length, b.length);
22
+ if (a.length !== b.length)
23
+ return 0;
13
24
  let dot = 0;
14
- for (let i = 0; i < len; i += 1)
25
+ for (let i = 0; i < a.length; i += 1)
15
26
  dot += (a[i] ?? 0) * (b[i] ?? 0);
16
27
  return dot;
17
28
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "claude-db",
3
- "version": "0.1.1",
4
- "description": "Persistent memory for Claude Code and other AI coding agents. Captures why decisions were made, not just which files changed. Bring your own database: SQLite, MongoDB or Postgres. No cloud, no subscription.",
3
+ "version": "0.2.0",
4
+ "description": "Persistent memory for Claude Code. Captures why decisions were made, not just which files changed. Bring your own database: SQLite, MongoDB or Postgres. No cloud, no subscription.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
7
7
  "bin": {