claude-db 0.1.0 → 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 +95 -243
  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,6 +1,7 @@
1
- import { closeSync, fstatSync, openSync, readSync } from 'node:fs';
1
+ import { closeSync, fstatSync, openSync, readdirSync, readSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import { join } from 'node:path';
4
+ import { resolveProject } from '../util/project.js';
4
5
  /**
5
6
  * Claude Code already writes a complete record of every session to disk:
6
7
  * prompts, replies, and tool calls, appended as JSONL.
@@ -158,3 +159,70 @@ export function transcriptPathFor(project, sessionId) {
158
159
  const slug = project.replace(/[/.]/g, '-');
159
160
  return join(homedir(), '.claude', 'projects', slug, `${sessionId}.jsonl`);
160
161
  }
162
+ /**
163
+ * Every transcript belonging to a project, including sessions started from one
164
+ * of its subdirectories.
165
+ *
166
+ * Claude Code files transcripts under the directory the agent was launched in,
167
+ * while memory is keyed on the repository root, and those differ the moment
168
+ * anyone runs the agent from `repo/frontend`. Candidate directories are found
169
+ * by prefix and then confirmed by the `cwd` each transcript records: the slug
170
+ * maps both `/` and `.` to `-`, so it cannot tell `~/app` from `~/app-extra`,
171
+ * and without the second step a flush would ingest a neighbour's history.
172
+ */
173
+ export function transcriptsFor(project) {
174
+ const root = join(homedir(), '.claude', 'projects');
175
+ const slug = project.replace(/[/.]/g, '-');
176
+ let candidates;
177
+ try {
178
+ candidates = readdirSync(root).filter((name) => name === slug || name.startsWith(`${slug}-`));
179
+ }
180
+ catch {
181
+ return [];
182
+ }
183
+ const transcripts = [];
184
+ for (const dir of candidates) {
185
+ let files;
186
+ try {
187
+ files = readdirSync(join(root, dir)).filter((name) => name.endsWith('.jsonl'));
188
+ }
189
+ catch {
190
+ continue;
191
+ }
192
+ for (const file of files) {
193
+ const path = join(root, dir, file);
194
+ const cwd = transcriptCwd(path);
195
+ if (cwd === null || resolveProject(cwd) === project)
196
+ transcripts.push(path);
197
+ }
198
+ }
199
+ return transcripts.sort();
200
+ }
201
+ /** First cwd recorded in a transcript, without reading a 90MB file. */
202
+ function transcriptCwd(path) {
203
+ let fd;
204
+ try {
205
+ fd = openSync(path, 'r');
206
+ }
207
+ catch {
208
+ return null;
209
+ }
210
+ try {
211
+ const buffer = Buffer.allocUnsafe(65_536);
212
+ const read = readSync(fd, buffer, 0, buffer.length, 0);
213
+ for (const line of buffer.subarray(0, read).toString('utf8').split('\n')) {
214
+ try {
215
+ const cwd = JSON.parse(line).cwd;
216
+ if (typeof cwd === 'string' && cwd.length > 0)
217
+ return cwd;
218
+ }
219
+ catch {
220
+ // Entry without a cwd, or the torn last line of the window.
221
+ }
222
+ }
223
+ return null;
224
+ }
225
+ finally {
226
+ closeSync(fd);
227
+ }
228
+ }
@@ -1,5 +1,5 @@
1
1
  import { classifyCommand } from './command.js';
2
- import { observationId } from './identity.js';
2
+ import { currentAuthor, observationId } from './identity.js';
3
3
  /**
4
4
  * Builds observations from transcript turns.
5
5
  *
@@ -40,15 +40,18 @@ function buildObservation(turn, sessionId, project, config) {
40
40
  ]
41
41
  .filter((line) => line !== '')
42
42
  .join('\n'), config.capture.maxBodyChars));
43
+ const author = currentAuthor();
43
44
  return {
44
45
  id: observationId(sessionId, turn.timestamp, turn.prompt),
45
46
  sessionId,
46
47
  project,
47
- kind: classifyTurn(turn, commands.length > 0),
48
- title: buildTitle(turn, files),
48
+ ...(author ? { author } : {}),
49
+ kind: classifyTurn(turn),
50
+ // Redacted like the body: the title is injected above every future prompt.
51
+ title: redact(buildTitle(turn, files)),
49
52
  body,
50
53
  files,
51
- tags: [],
54
+ tags: topLevelDirs(files, project),
52
55
  createdAt: turn.timestamp,
53
56
  };
54
57
  }
@@ -88,8 +91,34 @@ function headline(sentence) {
88
91
  const word = clause.lastIndexOf(' ');
89
92
  return `${clause.slice(0, word > 30 ? word : TITLE_MAX)}...`;
90
93
  }
91
- function classifyTurn(turn, hasCommands) {
94
+ /**
95
+ * The first path segment under the project root, which is the repository name
96
+ * when the project directory holds several repos, and the top-level source
97
+ * folder when it is one repo. Tags carry real weight in every backend's index,
98
+ * so this is what makes a pooled memory filterable by where the work happened.
99
+ */
100
+ function topLevelDirs(files, project) {
101
+ const prefix = project.endsWith('/') ? project : `${project}/`;
102
+ const tags = new Set();
103
+ for (const file of files) {
104
+ if (!file.startsWith(prefix))
105
+ continue;
106
+ const parts = file.slice(prefix.length).split('/');
107
+ // Two or more parts means the first is a directory, not a root-level file.
108
+ if (parts.length > 1 && parts[0])
109
+ tags.add(parts[0]);
110
+ }
111
+ return [...tags].slice(0, 3);
112
+ }
113
+ function classifyTurn(turn) {
92
114
  const text = `${turn.prompt} ${turn.reasoning}`.toLowerCase();
115
+ // Read from the prompt alone, and only where the phrasing is a standing
116
+ // instruction. "the build always fails" is a bugfix, not a house rule, so
117
+ // always/never have to be followed by something imperative to count.
118
+ if (/\b(from now on|going forward|remember to|prefer)\b/.test(turn.prompt.toLowerCase()) ||
119
+ /\b(always|never)\s+(use|run|do|add|write|call|put|name|commit|push|import|install)\b/.test(turn.prompt.toLowerCase())) {
120
+ return 'preference';
121
+ }
93
122
  if (/\b(instead of|rather than|chose|decided|because|why we|trade-?off)\b/.test(text)) {
94
123
  return 'decision';
95
124
  }
@@ -98,9 +127,7 @@ function classifyTurn(turn, hasCommands) {
98
127
  }
99
128
  if (/\b(fix(ed)?|bug|broken|regression|error|crash)\b/.test(text))
100
129
  return 'bugfix';
101
- if (turn.files.length > 0)
102
- return 'pattern';
103
- return hasCommands ? 'context' : 'context';
130
+ return turn.files.length > 0 ? 'pattern' : 'context';
104
131
  }
105
132
  function firstSentence(text) {
106
133
  const trimmed = text.trim();
@@ -123,11 +150,16 @@ function isExcluded(file, patterns) {
123
150
  return patterns.some((pattern) => file.includes(pattern));
124
151
  }
125
152
  const PRIVATE_BLOCK = /<private>[\s\S]*?<\/private>/gi;
153
+ const PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g;
126
154
  function redact(text) {
127
155
  return text
128
156
  .replace(PRIVATE_BLOCK, '[redacted]')
157
+ .replace(PRIVATE_KEY, '[redacted-private-key]')
129
158
  .replace(/\b(sk-[A-Za-z0-9_-]{16,})\b/g, '[redacted-key]')
130
159
  .replace(/\b(gh[pousr]_[A-Za-z0-9]{16,})\b/g, '[redacted-token]')
160
+ .replace(/\bAKIA[0-9A-Z]{16}\b/g, '[redacted-key]')
161
+ .replace(/\bxox[baprs]-[A-Za-z0-9-]{10,}/g, '[redacted-token]')
162
+ .replace(/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g, '[redacted-jwt]')
131
163
  .replace(/(?<=(password|secret|token|api[_-]?key)"?\s*[:=]\s*)"[^"]+"/gi, '"[redacted]"');
132
164
  }
133
165
  function truncate(text, max) {