claude-db 0.7.0 → 0.8.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 (43) hide show
  1. package/CHANGELOG.md +97 -0
  2. package/README.md +11 -3
  3. package/dist/capture/flush.js +0 -4
  4. package/dist/capture/index.d.ts +1 -0
  5. package/dist/capture/index.js +1 -0
  6. package/dist/capture/summarize.d.ts +3 -0
  7. package/dist/capture/summarize.js +32 -0
  8. package/dist/capture/turn-extractor.js +12 -3
  9. package/dist/cli/commands/adoption.js +8 -2
  10. package/dist/cli/commands/doctor.js +39 -0
  11. package/dist/cli/commands/setup.js +10 -0
  12. package/dist/cli/commands/view-page.d.ts +2 -0
  13. package/dist/cli/commands/view-page.js +151 -0
  14. package/dist/cli/commands/view.d.ts +23 -0
  15. package/dist/cli/commands/view.js +99 -0
  16. package/dist/cli/index.js +4 -0
  17. package/dist/cli/install.js +9 -3
  18. package/dist/cli/usage.js +1 -0
  19. package/dist/config/schema.d.ts +10 -0
  20. package/dist/config/schema.js +3 -0
  21. package/dist/graph/languages/basic.d.ts +11 -0
  22. package/dist/graph/languages/basic.js +156 -0
  23. package/dist/graph/languages/index.d.ts +1 -0
  24. package/dist/graph/languages/index.js +7 -4
  25. package/dist/graph/languages/ruby.d.ts +2 -0
  26. package/dist/graph/languages/ruby.js +16 -0
  27. package/dist/graph/languages/rules.d.ts +1 -0
  28. package/dist/graph/query/suggest.d.ts +0 -1
  29. package/dist/graph/query/suggest.js +2 -9
  30. package/dist/graph/scan/extract.d.ts +1 -0
  31. package/dist/graph/scan/extract.js +43 -2
  32. package/dist/graph/scan/files.d.ts +1 -1
  33. package/dist/graph/scan/files.js +3 -2
  34. package/dist/graph/scan/resolve.js +2 -0
  35. package/dist/hooks/grep-symbols.d.ts +0 -1
  36. package/dist/hooks/grep-symbols.js +5 -5
  37. package/dist/hooks/relevance.d.ts +4 -0
  38. package/dist/hooks/relevance.js +10 -0
  39. package/dist/hooks/session-end.js +11 -2
  40. package/dist/hooks/session-start.js +9 -2
  41. package/dist/hooks/user-prompt.js +6 -3
  42. package/dist/mcp/tools/observations.js +1 -1
  43. package/package.json +5 -3
@@ -18,7 +18,7 @@ const HOOKS = [
18
18
  ['SessionStart', 'session-start.js'],
19
19
  ['UserPromptSubmit', 'user-prompt.js'],
20
20
  ['SessionEnd', 'session-end.js'],
21
- ['PreToolUse', 'prefer-usages.js', 'Bash|Grep'],
21
+ ['PreToolUse', 'prefer-usages.js', 'Bash|Grep', 10],
22
22
  ];
23
23
  export function assertStableLocation(distDir) {
24
24
  const ephemeral = ['_npx', '_cacache'];
@@ -35,7 +35,7 @@ export function install(distDir, scope, project) {
35
35
  const path = settingsPathFor(scope, project);
36
36
  const settings = readJson(path);
37
37
  const hooks = (settings['hooks'] ?? {});
38
- for (const [event, file, matcher] of HOOKS) {
38
+ for (const [event, file, matcher, timeout] of HOOKS) {
39
39
  const kept = (hooks[event] ?? [])
40
40
  .map((entry) => ({
41
41
  ...entry,
@@ -44,7 +44,13 @@ export function install(distDir, scope, project) {
44
44
  .filter((entry) => entry.hooks.length > 0);
45
45
  kept.push({
46
46
  ...(matcher ? { matcher } : {}),
47
- hooks: [{ type: 'command', command: hookCommand(distDir, file) }],
47
+ hooks: [
48
+ {
49
+ type: 'command',
50
+ command: hookCommand(distDir, file),
51
+ ...(timeout ? { timeout } : {}),
52
+ },
53
+ ],
48
54
  });
49
55
  hooks[event] = kept;
50
56
  }
package/dist/cli/usage.js CHANGED
@@ -16,6 +16,7 @@ export function usage() {
16
16
  merge [<old-path>] [--yes] Move memory from an old project path onto this one
17
17
  stats What this project's memory is made of
18
18
  adoption How often sessions grep vs use the memory tools
19
+ view [--export <file>] See this project's memory live in the browser
19
20
  flush Re-ingest every transcript for this project
20
21
  seed --from-git [--limit n] Fill a cold memory from this repo's history
21
22
  scan [--force] Map this repo's symbols and how they connect
@@ -21,12 +21,22 @@ export declare const ConfigSchema: z.ZodObject<{
21
21
  tools: z.ZodDefault<z.ZodArray<z.ZodString>>;
22
22
  exclude: z.ZodDefault<z.ZodArray<z.ZodString>>;
23
23
  maxBodyChars: z.ZodDefault<z.ZodNumber>;
24
+ summarize: z.ZodDefault<z.ZodEnum<{
25
+ off: "off";
26
+ on: "on";
27
+ }>>;
28
+ summarizeModel: z.ZodDefault<z.ZodEnum<{
29
+ opus: "opus";
30
+ sonnet: "sonnet";
31
+ haiku: "haiku";
32
+ }>>;
24
33
  }, z.core.$strip>>;
25
34
  inject: z.ZodPrefault<z.ZodObject<{
26
35
  sessions: z.ZodDefault<z.ZodNumber>;
27
36
  maxChars: z.ZodDefault<z.ZodNumber>;
28
37
  perPrompt: z.ZodDefault<z.ZodBoolean>;
29
38
  promptResults: z.ZodDefault<z.ZodNumber>;
39
+ minOverlap: z.ZodDefault<z.ZodNumber>;
30
40
  promptMaxChars: z.ZodDefault<z.ZodNumber>;
31
41
  expandTop: z.ZodDefault<z.ZodNumber>;
32
42
  expandMaxChars: z.ZodDefault<z.ZodNumber>;
@@ -15,6 +15,8 @@ export const ConfigSchema = z.object({
15
15
  tools: z.array(z.string()).default(['Edit', 'Write', 'Bash', 'NotebookEdit']),
16
16
  exclude: z.array(z.string()).default(['.env', 'secrets', 'node_modules', '.git/']),
17
17
  maxBodyChars: z.number().int().positive().default(4000),
18
+ summarize: z.enum(['off', 'on']).default('off'),
19
+ summarizeModel: z.enum(['opus', 'sonnet', 'haiku']).default('opus'),
18
20
  })
19
21
  .prefault({}),
20
22
  inject: z
@@ -23,6 +25,7 @@ export const ConfigSchema = z.object({
23
25
  maxChars: z.number().int().positive().default(6000),
24
26
  perPrompt: z.boolean().default(true),
25
27
  promptResults: z.number().int().positive().max(10).default(4),
28
+ minOverlap: z.number().int().min(0).default(1),
26
29
  promptMaxChars: z.number().int().positive().default(700),
27
30
  expandTop: z.number().int().min(0).max(3).default(0),
28
31
  expandMaxChars: z.number().int().positive().default(900),
@@ -0,0 +1,11 @@
1
+ import type { LanguageSpec } from './rules.js';
2
+ import type { SymbolKind } from '../../types.js';
3
+ export declare const BASIC_FINGERPRINT: string;
4
+ export declare const BASIC_LANGUAGES: LanguageSpec[];
5
+ export interface BasicDeclaration {
6
+ name: string;
7
+ kind: SymbolKind;
8
+ line: number;
9
+ }
10
+ export declare function declarationsIn(source: string): BasicDeclaration[];
11
+ export declare function callsIn(source: string): BasicDeclaration[];
@@ -0,0 +1,156 @@
1
+ const LABELS = {
2
+ c: ['.c', '.h'],
3
+ cpp: ['.cc', '.cpp', '.cxx', '.hpp', '.hh'],
4
+ csharp: ['.cs'],
5
+ java: ['.java'],
6
+ kotlin: ['.kt', '.kts'],
7
+ swift: ['.swift'],
8
+ 'objective-c': ['.m', '.mm'],
9
+ scala: ['.scala'],
10
+ php: ['.php'],
11
+ perl: ['.pl', '.pm'],
12
+ lua: ['.lua'],
13
+ r: ['.r', '.R'],
14
+ julia: ['.jl'],
15
+ dart: ['.dart'],
16
+ elixir: ['.ex', '.exs'],
17
+ erlang: ['.erl'],
18
+ clojure: ['.clj', '.cljs', '.cljc'],
19
+ haskell: ['.hs'],
20
+ ocaml: ['.ml', '.mli'],
21
+ fsharp: ['.fs', '.fsx'],
22
+ groovy: ['.groovy', '.gradle'],
23
+ shell: ['.sh', '.bash', '.zsh'],
24
+ powershell: ['.ps1', '.psm1'],
25
+ sql: ['.sql'],
26
+ zig: ['.zig'],
27
+ nim: ['.nim'],
28
+ crystal: ['.cr'],
29
+ solidity: ['.sol'],
30
+ vala: ['.vala'],
31
+ };
32
+ const KEYWORDS = {
33
+ class: 'class',
34
+ struct: 'class',
35
+ object: 'class',
36
+ record: 'class',
37
+ actor: 'class',
38
+ contract: 'class',
39
+ module: 'class',
40
+ defmodule: 'class',
41
+ namespace: 'class',
42
+ interface: 'interface',
43
+ protocol: 'interface',
44
+ trait: 'interface',
45
+ typeclass: 'interface',
46
+ enum: 'enum',
47
+ type: 'type',
48
+ typedef: 'type',
49
+ data: 'type',
50
+ newtype: 'type',
51
+ def: 'function',
52
+ defp: 'function',
53
+ defn: 'function',
54
+ defun: 'function',
55
+ function: 'function',
56
+ func: 'function',
57
+ fn: 'function',
58
+ sub: 'function',
59
+ proc: 'function',
60
+ method: 'method',
61
+ };
62
+ const DECLARATION = new RegExp(`^[\\t ]*(?:[\\w@\\[\\]]+[\\t ]+){0,3}(${Object.keys(KEYWORDS).join('|')})[\\t ]+([A-Za-z_][\\w]*)`);
63
+ const CALLABLE = /^[\t ]*(?:[A-Za-z_][\w:<>,.\[\]*&\t ]*[\t *&]+)?([A-Za-z_]\w*)[\t ]*\([^;=]*\)[\t ]*(?:const[\t ]*)?\{[\t ]*$/;
64
+ const CALL = /\b([A-Za-z_]\w{2,})[\t ]*\(/g;
65
+ const NOT_A_CALL = new Set([
66
+ 'if',
67
+ 'for',
68
+ 'while',
69
+ 'switch',
70
+ 'catch',
71
+ 'return',
72
+ 'with',
73
+ 'else',
74
+ 'elsif',
75
+ 'unless',
76
+ 'until',
77
+ 'when',
78
+ 'case',
79
+ 'do',
80
+ 'end',
81
+ 'and',
82
+ 'not',
83
+ 'or',
84
+ 'sizeof',
85
+ 'typeof',
86
+ 'defined',
87
+ 'print',
88
+ 'printf',
89
+ 'echo',
90
+ 'assert',
91
+ 'require',
92
+ 'import',
93
+ 'include',
94
+ 'new',
95
+ 'delete',
96
+ 'throw',
97
+ 'super',
98
+ 'self',
99
+ 'this',
100
+ ...Object.keys(KEYWORDS),
101
+ ]);
102
+ export const BASIC_FINGERPRINT = [
103
+ DECLARATION.source,
104
+ CALLABLE.source,
105
+ CALL.source,
106
+ Object.keys(LABELS).join(','),
107
+ ].join('|');
108
+ export const BASIC_LANGUAGES = Object.entries(LABELS).map(([label, extensions]) => ({
109
+ id: 'basic',
110
+ label,
111
+ extensions,
112
+ definitions: [],
113
+ references: [],
114
+ basic: true,
115
+ }));
116
+ export function declarationsIn(source) {
117
+ const found = [];
118
+ const lines = source.split('\n');
119
+ for (let index = 0; index < lines.length; index++) {
120
+ const line = lines[index] ?? '';
121
+ if (line.length > 400)
122
+ continue;
123
+ const keyword = DECLARATION.exec(line);
124
+ if (keyword) {
125
+ const kind = KEYWORDS[keyword[1] ?? ''];
126
+ const name = keyword[2];
127
+ if (kind && name && !KEYWORDS[name])
128
+ found.push({ name, kind, line: index + 1 });
129
+ continue;
130
+ }
131
+ const callable = CALLABLE.exec(line);
132
+ const name = callable?.[1];
133
+ if (name && !NOT_A_CALL.has(name))
134
+ found.push({ name, kind: 'function', line: index + 1 });
135
+ }
136
+ return found;
137
+ }
138
+ const MAX_CALLS = 200;
139
+ export function callsIn(source) {
140
+ const found = [];
141
+ const lines = source.split('\n');
142
+ for (let index = 0; index < lines.length && found.length < MAX_CALLS; index++) {
143
+ const line = lines[index] ?? '';
144
+ if (line.length > 400)
145
+ continue;
146
+ if (DECLARATION.test(line) || CALLABLE.test(line))
147
+ continue;
148
+ for (const match of line.matchAll(CALL)) {
149
+ const name = match[1];
150
+ if (!name || NOT_A_CALL.has(name))
151
+ continue;
152
+ found.push({ name, kind: 'function', line: index + 1 });
153
+ }
154
+ }
155
+ return found;
156
+ }
@@ -2,5 +2,6 @@ import type { LanguageSpec } from './rules.js';
2
2
  export type { DefinitionRule, LanguageSpec, ReferenceRule } from './rules.js';
3
3
  export declare const LANGUAGES: LanguageSpec[];
4
4
  export declare const DYNAMIC_LANGUAGES: string[];
5
+ export { BASIC_FINGERPRINT, callsIn, declarationsIn } from './basic.js';
5
6
  export declare function languageFor(path: string): LanguageSpec | null;
6
7
  export declare function languageNames(): string;
@@ -1,11 +1,14 @@
1
+ import { BASIC_LANGUAGES } from './basic.js';
1
2
  import { javascript, tsx, typescript } from './ecmascript.js';
2
3
  import { go } from './go.js';
3
4
  import { python } from './python.js';
5
+ import { ruby } from './ruby.js';
4
6
  import { rust } from './rust.js';
5
- export const LANGUAGES = [typescript, tsx, javascript, python, go, rust];
6
- export const DYNAMIC_LANGUAGES = ['python', 'go', 'rust'];
7
+ export const LANGUAGES = [typescript, tsx, javascript, python, go, rust, ruby];
8
+ export const DYNAMIC_LANGUAGES = ['python', 'go', 'rust', 'ruby'];
9
+ export { BASIC_FINGERPRINT, callsIn, declarationsIn } from './basic.js';
7
10
  const BY_EXTENSION = new Map();
8
- for (const spec of LANGUAGES) {
11
+ for (const spec of [...LANGUAGES, ...BASIC_LANGUAGES]) {
9
12
  for (const extension of spec.extensions)
10
13
  BY_EXTENSION.set(extension, spec);
11
14
  }
@@ -16,5 +19,5 @@ export function languageFor(path) {
16
19
  return BY_EXTENSION.get(path.slice(dot).toLowerCase()) ?? null;
17
20
  }
18
21
  export function languageNames() {
19
- return LANGUAGES.map((spec) => spec.label).join(', ');
22
+ return `${LANGUAGES.map((spec) => spec.label).join(', ')}, and ${BASIC_LANGUAGES.length} more by pattern`;
20
23
  }
@@ -0,0 +1,2 @@
1
+ import type { LanguageSpec } from './rules.js';
2
+ export declare const ruby: LanguageSpec;
@@ -0,0 +1,16 @@
1
+ export const ruby = {
2
+ id: 'ruby',
3
+ label: 'ruby',
4
+ extensions: ['.rb', '.rake'],
5
+ definitions: [
6
+ { kind: 'method', field: ['name'], symbol: 'method' },
7
+ { kind: 'singleton_method', field: ['name'], symbol: 'method' },
8
+ { kind: 'class', field: ['name'], symbol: 'class' },
9
+ { kind: 'module', field: ['name'], symbol: 'class' },
10
+ ],
11
+ references: [
12
+ { kind: 'call', field: ['method'], relation: 'calls' },
13
+ { kind: 'call', field: ['receiver'], relation: 'references' },
14
+ { kind: 'class', field: ['superclass'], relation: 'extends' },
15
+ ],
16
+ };
@@ -15,4 +15,5 @@ export interface LanguageSpec {
15
15
  extensions: string[];
16
16
  definitions: DefinitionRule[];
17
17
  references: ReferenceRule[];
18
+ basic?: boolean;
18
19
  }
@@ -1,5 +1,4 @@
1
1
  import type { MemoryStore } from '../../store/adapter.js';
2
- /** Names closest to `symbol`, best first. A miss that names no alternative is where people quit. */
3
2
  export declare function nearest(names: Iterable<string>, symbol: string, limit?: number): string[];
4
3
  export declare function suggestFor(store: MemoryStore, project: string, symbol: string): Promise<string[]>;
5
4
  export declare function formatSuggestions(suggestions: string[]): string;
@@ -1,20 +1,17 @@
1
1
  const CANDIDATES = 5000;
2
2
  function tokens(name) {
3
- return (name
3
+ return name
4
4
  .replace(/([a-z0-9])([A-Z])/g, '$1 $2')
5
5
  .split(/[^A-Za-z0-9]+/)
6
6
  .filter(Boolean)
7
7
  .map((part) => part.toLowerCase())
8
- // Stem the plural: "hooks" and "hook" are the same misremembered name.
9
- .map((part) => (part.length > 3 && part.endsWith('s') ? part.slice(0, -1) : part)));
8
+ .map((part) => (part.length > 3 && part.endsWith('s') ? part.slice(0, -1) : part));
10
9
  }
11
10
  function score(candidate, query) {
12
11
  const left = candidate.toLowerCase();
13
12
  const right = query.toLowerCase();
14
13
  if (left === right)
15
14
  return 100;
16
- // Ratios are taken against the LONGER name: without that, one-letter locals
17
- // score as well as the real symbol, because everything contains "e".
18
15
  const longest = Math.max(left.length, right.length);
19
16
  let prefix = 0;
20
17
  while (prefix < left.length && prefix < right.length && left[prefix] === right[prefix]) {
@@ -29,16 +26,12 @@ function score(candidate, query) {
29
26
  const byContains = contains ? (Math.min(left.length, right.length) / longest) * 100 : 0;
30
27
  return Math.max(byPrefix, byToken, byContains);
31
28
  }
32
- /** Names closest to `symbol`, best first. A miss that names no alternative is where people quit. */
33
29
  export function nearest(names, symbol, limit = 5) {
34
30
  const scored = new Map();
35
31
  for (const name of names) {
36
- // Two-character locals are never what someone misremembered.
37
32
  if (name === symbol || name.length < 3)
38
33
  continue;
39
34
  const value = score(name, symbol);
40
- // Below this, matches are incidental ("widget" contains "get") and a wrong
41
- // suggestion costs more than none: it sends you looking for the wrong thing.
42
35
  if (value >= 40)
43
36
  scored.set(name, Math.max(scored.get(name) ?? 0, value));
44
37
  }
@@ -6,6 +6,7 @@ export interface Reference {
6
6
  relation: EdgeRelation;
7
7
  line: number;
8
8
  from: CodeSymbol | null;
9
+ weak?: boolean;
9
10
  }
10
11
  export interface Extraction {
11
12
  symbols: CodeSymbol[];
@@ -1,11 +1,50 @@
1
1
  import { observationId } from '../../capture/identity.js';
2
2
  import { redact } from '../../capture/turn-extractor.js';
3
+ import { callsIn, declarationsIn } from '../languages/index.js';
3
4
  import { languageHandle, loadParser } from '../parser.js';
4
5
  const CALLABLE = new Set(['function', 'method', 'class']);
5
- const IDENTIFIER = /^[\w$]+$/;
6
+ const IDENTIFIER = /^[\w$]+[?!]?$/;
6
7
  export function symbolId(project, file, name, kind) {
7
8
  return observationId('graph', 0, `${project}\0${file}\0${name}\0${kind}`);
8
9
  }
10
+ function extractByPattern(file, project) {
11
+ const lines = file.source.split('\n');
12
+ const symbols = [];
13
+ const spans = [];
14
+ for (const declaration of declarationsIn(file.source)) {
15
+ const symbol = {
16
+ id: symbolId(project, file.path, declaration.name, declaration.kind),
17
+ project,
18
+ name: declaration.name,
19
+ kind: declaration.kind,
20
+ file: file.path,
21
+ line: declaration.line,
22
+ lang: file.spec.label,
23
+ signature: redact((lines[declaration.line - 1] ?? '').trim()).slice(0, 200),
24
+ };
25
+ symbols.push(symbol);
26
+ spans.push({ start: declaration.line, end: declaration.line, symbol });
27
+ }
28
+ const references = callsIn(file.source).map((call) => ({
29
+ file: file.path,
30
+ name: call.name,
31
+ relation: 'calls',
32
+ line: call.line,
33
+ from: nearestAbove(call.line, spans),
34
+ weak: true,
35
+ }));
36
+ return { symbols, references };
37
+ }
38
+ function nearestAbove(line, spans) {
39
+ let best = null;
40
+ for (const span of spans) {
41
+ if (span.start > line)
42
+ continue;
43
+ if (!best || span.start > best.start)
44
+ best = span;
45
+ }
46
+ return best?.symbol ?? null;
47
+ }
9
48
  function resolveField(node, path) {
10
49
  let current = node;
11
50
  for (const name of path) {
@@ -16,7 +55,7 @@ function resolveField(node, path) {
16
55
  return current;
17
56
  }
18
57
  function unquote(text) {
19
- return text.replace(/^['"`]|['"`]$/g, '');
58
+ return text.replace(/^['"`]|['"`]$/g, '').replace(/^[<(:\s]+|[)\s]+$/g, '');
20
59
  }
21
60
  function enclosing(line, spans) {
22
61
  let best = null;
@@ -38,6 +77,8 @@ function enclosing(line, spans) {
38
77
  return (best ?? fallback)?.symbol ?? null;
39
78
  }
40
79
  export function extractFile(file, project) {
80
+ if (file.spec.basic)
81
+ return extractByPattern(file, project);
41
82
  const parser = loadParser();
42
83
  const root = parser.parse(languageHandle(parser, file.spec.id), file.source).root();
43
84
  const lines = file.source.split('\n');
@@ -5,7 +5,7 @@ export interface SourceFile {
5
5
  source: string;
6
6
  hash: string;
7
7
  }
8
- export declare const SCAN_VERSION = 2;
8
+ export declare const SCAN_VERSION = 3;
9
9
  export declare function hashOf(bytes: Buffer): string;
10
10
  export declare function listFiles(root: string): string[];
11
11
  export declare function sourceFiles(root: string): string[];
@@ -2,12 +2,13 @@ import { execFileSync } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
3
  import { readFileSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
- import { LANGUAGES, languageFor } from '../languages/index.js';
5
+ import { BASIC_FINGERPRINT, LANGUAGES, languageFor } from '../languages/index.js';
6
6
  const MAX_BUFFER = 64 * 1024 * 1024;
7
7
  const MAX_FILE_BYTES = 1024 * 1024;
8
- export const SCAN_VERSION = 2;
8
+ export const SCAN_VERSION = 3;
9
9
  const RULES_FINGERPRINT = createHash('sha256')
10
10
  .update(JSON.stringify(LANGUAGES))
11
+ .update(BASIC_FINGERPRINT)
11
12
  .digest('hex')
12
13
  .slice(0, 12);
13
14
  const CACHE_KEY = `v${SCAN_VERSION}:${RULES_FINGERPRINT}\n`;
@@ -19,6 +19,8 @@ export function resolveEdges(project, symbols, references) {
19
19
  const candidates = byName.get(reference.name) ?? [];
20
20
  const local = candidates.find((candidate) => candidate.file === reference.file);
21
21
  const target = local ?? candidates[0];
22
+ if (!target && reference.weak)
23
+ continue;
22
24
  let confidence = 'EXTRACTED';
23
25
  let score = SAME_FILE;
24
26
  if (!local && candidates.length === 1) {
@@ -1,5 +1,4 @@
1
1
  export declare const DECLARED: Set<string>;
2
2
  export declare function isSymbol(pattern: string): boolean;
3
3
  export declare function isWord(pattern: string): boolean;
4
- /** Symbols a shell command searches the tree for, which the code graph also answers. */
5
4
  export declare function symbolsGreppedIn(command: string): string[];
@@ -1,14 +1,13 @@
1
1
  const SYMBOL = /^(?=.{4,})(?:[a-z]+(?:[A-Z][a-zA-Z0-9]*)+|[A-Z][a-z0-9]+(?:[A-Z][a-zA-Z0-9]*)*|[a-z][a-z0-9]*(?:_[a-z0-9]+)+)$/;
2
2
  const WORD = /^[a-z][a-z0-9]{3,}$/;
3
3
  export const DECLARED = new Set(['function', 'method', 'class', 'interface', 'type', 'enum']);
4
- const GREP = /\b(?:grep|rg)\b((?:\s+(?:-[ABC]\s?\d+|-[tTg]\s\S+|-{1,2}[\w-]+(?:=\S+)?))*)\s+(?:(['"])(.+?)\2|(\S+))([^;&|]*)/g;
4
+ const GREP = /(?:^|[;&|(`]|\$\(|\bdo\s+|\bthen\s+|\belse\s+|\bxargs\s+|\bgit\s+|\bsudo\s+|\btime\s+|-exec\s+)\s*(?:grep|rg)\b((?:\s+(?:-[ABC]\s?\d+|-[tTg]\s\S+|-{1,2}[\w-]+(?:=\S+)?))*)\s+(?:(['"])(.+?)\2|(\S+))([^;&|]*)/gm;
5
5
  export function isSymbol(pattern) {
6
6
  return SYMBOL.test(pattern);
7
7
  }
8
8
  export function isWord(pattern) {
9
9
  return WORD.test(pattern);
10
10
  }
11
- /** Symbols a shell command searches the tree for, which the code graph also answers. */
12
11
  export function symbolsGreppedIn(command) {
13
12
  const found = new Set();
14
13
  for (const match of command.matchAll(GREP)) {
@@ -19,11 +18,12 @@ export function symbolsGreppedIn(command) {
19
18
  continue;
20
19
  if (/-\w*v/.test(flags))
21
20
  continue;
22
- const before = command.slice(0, match.index);
21
+ const invocation = match[0];
22
+ const piped = /^\s*\|/.test(invocation);
23
23
  const searchesTree = /-\w*[rR]\b|--recursive/.test(flags) ||
24
24
  /^\s+[^-|;&>][^\s|;&>]*/.test(rest) ||
25
- /\bgit\s+$/.test(before) ||
26
- (match[0].startsWith('rg') && !/\|\s*$/.test(before));
25
+ /\bgit\s+(?:grep|rg)\b/.test(invocation) ||
26
+ (/(?:^|[\s;&|(])rg\b/.test(invocation) && !piped);
27
27
  if (searchesTree)
28
28
  found.add(pattern);
29
29
  }
@@ -1,2 +1,6 @@
1
1
  import type { Observation, ObservationIndexEntry } from '../types.js';
2
+ export declare function overlapCount(prompt: string, entry: {
3
+ title: string;
4
+ snippet?: string | undefined;
5
+ }): number;
2
6
  export declare function renderPromptContext(entries: ObservationIndexEntry[], maxChars: number, expanded?: Observation[], expandMaxChars?: number): string | null;
@@ -1,4 +1,14 @@
1
+ import { meaningfulTokens } from '../search/stopwords.js';
1
2
  import { toShortId } from '../util/shortid.js';
3
+ export function overlapCount(prompt, entry) {
4
+ const wanted = new Set(meaningfulTokens(prompt.toLowerCase()));
5
+ const offered = new Set(meaningfulTokens(`${entry.title} ${entry.snippet ?? ''}`.toLowerCase()));
6
+ let shared = 0;
7
+ for (const word of wanted)
8
+ if (offered.has(word))
9
+ shared++;
10
+ return shared;
11
+ }
2
12
  export function renderPromptContext(entries, maxChars, expanded = [], expandMaxChars = 900) {
3
13
  if (entries.length === 0)
4
14
  return null;
@@ -2,7 +2,7 @@
2
2
  import { spawn } from 'node:child_process';
3
3
  import { dirname, resolve } from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
- import { clearCursor, flushSession } from '../capture/index.js';
5
+ import { aiSummary, clearCursor, flushSession, observationsFromTurns, readTranscript, transcriptPathFor, } from '../capture/index.js';
6
6
  import { isDue, readState } from '../update.js';
7
7
  import { createContext } from '../context.js';
8
8
  import { readPayload, runHook } from './payload.js';
@@ -18,12 +18,21 @@ await runHook(async () => {
18
18
  const ctx = await createContext();
19
19
  try {
20
20
  const result = await flushSession(ctx, sessionId, project, payload.transcript_path);
21
+ let summary = result.summary;
22
+ if (ctx.config.capture.summarize === 'on') {
23
+ const path = payload.transcript_path ?? transcriptPathFor(project, sessionId);
24
+ const { turns } = readTranscript(path, 0);
25
+ const observations = observationsFromTurns(turns, sessionId, project, ctx.config);
26
+ const ai = await aiSummary(observations.map((obs) => `${obs.title}\n${obs.body}`).join('\n\n'), ctx.config.capture.summarizeModel);
27
+ if (ai)
28
+ summary = `AI: ${ai}`;
29
+ }
21
30
  await ctx.store.upsertSession({
22
31
  id: sessionId,
23
32
  project,
24
33
  startedAt: Date.now(),
25
34
  endedAt: Date.now(),
26
- ...(result.summary ? { summary: result.summary } : {}),
35
+ ...(summary ? { summary } : {}),
27
36
  });
28
37
  clearCursor(sessionId);
29
38
  if (ctx.config.updates !== 'off' && isDue(readState())) {
@@ -50,9 +50,13 @@ await runHook(async () => {
50
50
  const ctx = await createContext();
51
51
  try {
52
52
  const sessions = await ctx.store.recentSessions(project, ctx.config.inject.sessions);
53
+ const scanned = (await ctx.store.scannedFiles(project)).length;
54
+ const scanHint = 'Code graph not built yet — run `claude-db scan` once to enable ' +
55
+ 'find_usages and the symbol-grep hook.';
53
56
  if (sessions.length === 0) {
54
57
  emitContext('<project-memory>none yet for this project; ' +
55
- 'it is recorded as you work</project-memory>\n');
58
+ 'it is recorded as you work</project-memory>\n' +
59
+ (scanned === 0 ? `${scanHint}\n` : ''));
56
60
  return;
57
61
  }
58
62
  const lines = ['<project-memory>'];
@@ -96,10 +100,13 @@ await runHook(async () => {
96
100
  lines.push("Search this project's full history with the memory MCP tools before " +
97
101
  'asking the user to re-explain prior decisions.');
98
102
  }
103
+ if (scanned === 0)
104
+ lines.push(scanHint);
99
105
  const notice = ctx.config.updates === 'off' ? null : updateNotice();
100
106
  if (notice)
101
107
  lines.push(notice);
102
- emitContext(`${lines.join('\n')}\n`);
108
+ const body = lines.join('\n');
109
+ emitContext(`${body}\n(context ≈ ${Math.round(body.length / 4)} tokens)\n`);
103
110
  await refreshGraphQuietly(ctx.store, project);
104
111
  }
105
112
  finally {
@@ -3,7 +3,7 @@ import { flushSession } from '../capture/index.js';
3
3
  import { createContext } from '../context.js';
4
4
  import { emitContext, readPayload, runHook } from './payload.js';
5
5
  import { markShown, readShown } from './shown.js';
6
- import { renderPromptContext } from './relevance.js';
6
+ import { overlapCount, renderPromptContext } from './relevance.js';
7
7
  import { isSearchable } from '../util/prompt.js';
8
8
  import { resolveProject } from '../util/project.js';
9
9
  import { silenceSqliteWarning } from '../util/warnings.js';
@@ -27,7 +27,10 @@ await runHook(async () => {
27
27
  project,
28
28
  limit: ctx.config.inject.promptResults,
29
29
  });
30
- const entries = found.filter((entry) => !shown.has(entry.id));
30
+ const floor = ctx.config.inject.minOverlap;
31
+ const entries = found
32
+ .filter((entry) => !shown.has(entry.id))
33
+ .filter((entry) => floor === 0 || overlapCount(prompt, entry) >= floor);
31
34
  if (entries.length === 0)
32
35
  return;
33
36
  const toExpand = entries.slice(0, ctx.config.inject.expandTop);
@@ -38,7 +41,7 @@ await runHook(async () => {
38
41
  if (!block)
39
42
  return;
40
43
  markShown(sessionId, entries.map((entry) => entry.id));
41
- emitContext(`${block}\n`);
44
+ emitContext(`${block}\n(context ≈ ${Math.round(block.length / 4)} tokens)\n`);
42
45
  }
43
46
  finally {
44
47
  await ctx.close();
@@ -25,7 +25,7 @@ export function register(server, ctx) {
25
25
  'truncated answer says there is more'),
26
26
  }, (args) => get_observations(ctx, args));
27
27
  }
28
- async function timeline(ctx, { observation_id, before, after, }) {
28
+ async function timeline(ctx, { observation_id, before, after }) {
29
29
  const entries = await ctx.search.timeline({ observationId: observation_id, before, after });
30
30
  if (entries.length === 0) {
31
31
  const [found] = await ctx.search.getObservations([observation_id]);