claude-db 0.8.4 → 0.9.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 (53) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/dist/capture/flush.js +3 -1
  3. package/dist/capture/progress.js +41 -9
  4. package/dist/capture/transcript.js +8 -2
  5. package/dist/capture/turn-extractor.js +2 -2
  6. package/dist/cli/commands/graph.js +12 -3
  7. package/dist/cli/commands/sync.js +9 -3
  8. package/dist/cli/commands/transfer.js +60 -10
  9. package/dist/cli/commands/view.js +1 -1
  10. package/dist/graph/languages/basic.js +5 -0
  11. package/dist/graph/languages/ecmascript.js +15 -0
  12. package/dist/graph/languages/rules.d.ts +1 -0
  13. package/dist/graph/parser.d.ts +1 -0
  14. package/dist/graph/query/lookup.js +7 -3
  15. package/dist/graph/scan/extract.d.ts +3 -1
  16. package/dist/graph/scan/extract.js +56 -22
  17. package/dist/graph/scan/files.d.ts +2 -1
  18. package/dist/graph/scan/files.js +4 -3
  19. package/dist/graph/scan/index.d.ts +1 -0
  20. package/dist/graph/scan/index.js +11 -1
  21. package/dist/graph/scan/resolve.js +17 -6
  22. package/dist/hooks/grep-symbols.js +1 -1
  23. package/dist/hooks/relevance.d.ts +1 -1
  24. package/dist/hooks/relevance.js +3 -1
  25. package/dist/hooks/session-start.js +1 -1
  26. package/dist/hooks/user-prompt.js +3 -2
  27. package/dist/store/adapter.d.ts +1 -0
  28. package/dist/store/mongo/graph.js +7 -0
  29. package/dist/store/mongo/index.d.ts +1 -0
  30. package/dist/store/mongo/index.js +3 -0
  31. package/dist/store/mongo/observations.js +11 -3
  32. package/dist/store/mongo/search.js +7 -5
  33. package/dist/store/mongo/sessions.d.ts +1 -0
  34. package/dist/store/mongo/sessions.js +8 -5
  35. package/dist/store/mongo/vectorSearch.js +1 -1
  36. package/dist/store/postgres/graph.js +7 -2
  37. package/dist/store/postgres/index.d.ts +1 -0
  38. package/dist/store/postgres/index.js +4 -1
  39. package/dist/store/postgres/insert.d.ts +1 -1
  40. package/dist/store/postgres/insert.js +2 -1
  41. package/dist/store/postgres/meta.js +16 -4
  42. package/dist/store/postgres/observations.js +10 -3
  43. package/dist/store/postgres/sessions.d.ts +1 -0
  44. package/dist/store/postgres/sessions.js +4 -0
  45. package/dist/store/sqlite/graph.js +6 -2
  46. package/dist/store/sqlite/index.d.ts +1 -0
  47. package/dist/store/sqlite/index.js +3 -0
  48. package/dist/store/sqlite/observations.js +10 -3
  49. package/dist/store/sqlite/sessions.d.ts +1 -0
  50. package/dist/store/sqlite/sessions.js +4 -0
  51. package/dist/types.d.ts +3 -0
  52. package/dist/usages/find.js +10 -6
  53. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,120 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0
4
+
5
+ A correctness release. Everything below was found by reviewing the whole
6
+ codebase rather than reported, so most of it had been failing quietly.
7
+
8
+ **Stored graphs rebuild on the first scan after upgrading.** Extraction and
9
+ edge ids both changed, so `SCAN_VERSION` is bumped and the old rows are
10
+ discarded rather than left to disagree with the new ones.
11
+
12
+ ### Fixed
13
+
14
+ - **`claude-db merge` could permanently delete observations.** `list()` ordered
15
+ by `created_at` alone while `after` was a strict `>`, so any row sharing a
16
+ timestamp with a batch boundary was stepped over by every caller that pages
17
+ through the store. `merge` copies page by page and then removes the source
18
+ project, deleting exactly the rows its own loop had skipped. Git-seeded
19
+ observations take the commit date, which has one-second resolution, so ties
20
+ are routine. Paging is now a keyset walk over `(created_at, id)`.
21
+ `export` and `sync` were dropping the same rows without deleting them.
22
+
23
+ - **A session's last turn was lost when the prompt was still being written.**
24
+ The flush cursor advanced to the raw end of the transcript even when the file
25
+ ended mid-line, so the next read began inside the JSON, the entry was dropped,
26
+ and the prompt with all its work never appeared again. The cursor now stops at
27
+ the last complete line.
28
+
29
+ - **`find_usages` hid the real caller of any method.** Inbound edges were
30
+ matched on the destination name, but a member call is stored under its
31
+ qualified name, so the actual `repo.save()` caller was fetched and then
32
+ discarded, leaving only the class that declares it.
33
+
34
+ - **A repeated name in one file pointed its edge at the wrong declaration.**
35
+ 0.8.5 gave each declaration its own id, but the resolver still matched by
36
+ name and took the first one in the file, so the second `run()` got an edge to
37
+ the first, stamped at the confidence tier that means "read from the syntax".
38
+
39
+ - **`implements` never produced an edge, and neither did `extends` in `.js` or
40
+ `.jsx`.** Those rules capture a whole clause, whose text includes the keyword,
41
+ and the whitespace guard dropped it.
42
+
43
+ - **An incremental `scan` broke every cross-file edge it touched**, rewriting it
44
+ with no destination at full confidence, until someone ran `--force`.
45
+
46
+ - **Two edges on one line could overwrite each other**, because the edge id
47
+ ignored the relation and the call it came from. `trim(value.trim())` lost the
48
+ direct call entirely.
49
+
50
+ - **A file over 1 MB or containing a NUL made the graph reparse the whole repo
51
+ forever**, and kept its stale symbols indefinitely.
52
+
53
+ - **A standing rule stopped being injected** once a project had more than 100
54
+ preferences, and `view` showed months-old rows as the current state past 500
55
+ observations. Both asked for the oldest N and sorted afterwards.
56
+
57
+ - **Committed work was announced as "not committed yet".** Landed work was
58
+ closed before the new rows were inserted, so a session's final turn was always
59
+ left open. Renamed files were also mis-parsed out of `git status`, closing
60
+ work whose rename had not landed.
61
+
62
+ - **A memory dropped by the prompt budget was suppressed for the rest of the
63
+ session**, having been marked as shown without being rendered.
64
+
65
+ - **An unclosed `<private>` tag was not redacted**, so a pasted credential was
66
+ stored and injected back into context. The README promises it is stripped.
67
+
68
+ - **The grep hook ignored `grep --recursive <symbol>`**, because its
69
+ invert-flag guard also matched `--recursive` and `--devices`.
70
+
71
+ - **`export` and `import` carried no sessions**, so a restored database reported
72
+ "none yet for this project" against a full memory. One truncated line also
73
+ aborted `import` after earlier batches had committed, with no report of what
74
+ had landed.
75
+
76
+ - **Postgres and Mongo diverged from SQLite in four places**: Mongo read a
77
+ zero-row timeline request as unlimited, scored Atlas vector hits on a
78
+ different scale so the relevance floor passed everything, and moved a
79
+ session's start time to when it ended; Postgres kept a stale vector when a row
80
+ was re-inserted without one, and could leave keyword search permanently broken
81
+ if it crashed while rebuilding the `tsv` column.
82
+
83
+ - **`usages --mode path X --target Y` searched for a target named `--target`.**
84
+
85
+ ### Added
86
+
87
+ - `ListFilter` takes `newest` and `afterId`; `EdgeFilter` takes `dstName`;
88
+ stores expose `sessionProjects()`.
89
+
90
+ ## 0.8.5
91
+
92
+ ### Added
93
+
94
+ - **Using a type now produces a graph edge.**
95
+ ([#19](https://github.com/Avijit07x/claude-db/issues/19))
96
+ A type in argument, return, annotation or type-argument position matched no
97
+ rule, so all 985 interfaces and type aliases on one 214k-line repo had zero
98
+ inbound edges and `find_usages` reported every one of them as unused.
99
+ `type_identifier` is now a reference rule. Matching it directly would also
100
+ hit declaration sites and emit a self-edge for each, so `ReferenceRule` takes
101
+ an optional `excludeParents`, and the rule skips the seven parents that mean
102
+ a type is being declared rather than used. Measured on this repo: 2,387
103
+ reference edges and zero self-edges. This completes the second half of the
104
+ issue; rendering a component shipped in 0.8.4.
105
+
106
+ ### Fixed
107
+
108
+ - **Two declarations sharing a name and kind in one file collapsed into one
109
+ symbol.** ([#18](https://github.com/Avijit07x/claude-db/issues/18))
110
+ `symbolId` hashed project, file, name and kind with nothing positional, so
111
+ any two functions in a file that each declared `const total` produced a
112
+ single symbol and one of them was lost. On claude-db's own source that was 89
113
+ colliding groups and 115 symbols never stored. The id now carries the
114
+ occurrence index of the name within its file. The first occurrence keeps the
115
+ id it always had, so existing graphs do not churn; only the second and later
116
+ duplicates gain new ids. `SCAN_VERSION` is bumped so stored graphs rebuild.
117
+
3
118
  ## 0.8.4
4
119
 
5
120
  ### Fixed
@@ -8,18 +8,20 @@ export async function flushSession(ctx, sessionId, project, transcriptPath, rebu
8
8
  const path = transcriptPath ?? transcriptPathFor(project, sessionId);
9
9
  const cursor = readCursor(sessionId);
10
10
  const { turns, nextOffset } = readTranscript(path, cursor);
11
- await closeLandedWork(ctx.store, project);
12
11
  if (turns.length === 0) {
12
+ await closeLandedWork(ctx.store, project);
13
13
  writeCursor(sessionId, nextOffset);
14
14
  return { observations: 0, summary: null };
15
15
  }
16
16
  const observations = observationsFromTurns(turns, sessionId, project, ctx.config);
17
17
  if (observations.length === 0) {
18
+ await closeLandedWork(ctx.store, project);
18
19
  writeCursor(sessionId, nextOffset);
19
20
  return { observations: 0, summary: null };
20
21
  }
21
22
  await embedObservations(ctx, observations);
22
23
  await ctx.store.insertObservations(observations);
24
+ await closeLandedWork(ctx.store, project);
23
25
  const previous = rebuild ? null : await ctx.store.getSession(sessionId);
24
26
  const summary = summarize(observations, previous?.summary);
25
27
  await ctx.store.upsertSession({
@@ -8,10 +8,21 @@ function uncommittedFiles(project) {
8
8
  encoding: 'utf8',
9
9
  maxBuffer: MAX_BUFFER,
10
10
  });
11
- return new Set(raw
12
- .split('\0')
13
- .filter((entry) => entry.length > 3)
14
- .map((entry) => join(project, entry.slice(3))));
11
+ const files = new Set();
12
+ const fields = raw.split('\0');
13
+ for (let i = 0; i < fields.length; i += 1) {
14
+ const entry = fields[i];
15
+ if (entry === undefined || entry.length <= 3)
16
+ continue;
17
+ files.add(join(project, entry.slice(3)));
18
+ if (/^[RC]/.test(entry) || /^.[RC]/.test(entry)) {
19
+ const origin = fields[i + 1];
20
+ if (origin !== undefined && origin.length > 0)
21
+ files.add(join(project, origin));
22
+ i += 1;
23
+ }
24
+ }
25
+ return files;
15
26
  }
16
27
  catch {
17
28
  return null;
@@ -21,13 +32,34 @@ export async function closeLandedWork(store, project) {
21
32
  const dirty = uncommittedFiles(project);
22
33
  if (!dirty)
23
34
  return 0;
24
- const open = await store.list({ project, status: 'open', limit: OPEN_LIMIT });
25
- const landed = open
26
- .filter((obs) => obs.files.every((file) => !dirty.has(file)))
27
- .map((obs) => obs.id);
35
+ const landed = [];
36
+ let after;
37
+ let afterId;
38
+ for (;;) {
39
+ const batch = await store.list({
40
+ project,
41
+ status: 'open',
42
+ ...(after === undefined ? {} : { after }),
43
+ ...(afterId === undefined ? {} : { afterId }),
44
+ limit: OPEN_LIMIT,
45
+ });
46
+ if (batch.length === 0)
47
+ break;
48
+ for (const obs of batch) {
49
+ if (obs.files.every((file) => !dirty.has(file)))
50
+ landed.push(obs.id);
51
+ }
52
+ const last = batch[batch.length - 1];
53
+ if (!last)
54
+ break;
55
+ after = last.createdAt;
56
+ afterId = last.id;
57
+ if (batch.length < OPEN_LIMIT)
58
+ break;
59
+ }
28
60
  return store.closeObservations(landed);
29
61
  }
30
62
  export async function openWork(store, project) {
31
- const open = await store.list({ project, status: 'open', limit: OPEN_LIMIT });
63
+ const open = await store.list({ project, status: 'open', limit: OPEN_LIMIT, newest: true });
32
64
  return open.sort((a, b) => b.createdAt - a.createdAt);
33
65
  }
@@ -31,8 +31,10 @@ export function readTranscript(path, fromOffset = 0) {
31
31
  }
32
32
  catch { }
33
33
  }
34
+ const lastNewline = raw.lastIndexOf('\n');
35
+ const complete = lastNewline < 0 ? start : start + Buffer.byteLength(raw.slice(0, lastNewline + 1), 'utf8');
34
36
  const turns = groupIntoTurns(entries);
35
- const nextOffset = turns.length > 0 ? (turns[turns.length - 1]?.offset ?? size) : size;
37
+ const nextOffset = turns.length > 0 ? (turns[turns.length - 1]?.offset ?? complete) : complete;
36
38
  return { turns, nextOffset };
37
39
  }
38
40
  finally {
@@ -42,8 +44,12 @@ export function readTranscript(path, fromOffset = 0) {
42
44
  function groupIntoTurns(entries) {
43
45
  const turns = [];
44
46
  let current = null;
47
+ let lastKnown = 0;
45
48
  for (const { entry, offset } of entries) {
46
- const timestamp = Date.parse(entry.timestamp ?? '') || Date.now();
49
+ const parsed = Date.parse(entry.timestamp ?? '');
50
+ const timestamp = Number.isNaN(parsed) ? lastKnown : parsed;
51
+ if (!Number.isNaN(parsed))
52
+ lastKnown = parsed;
47
53
  if (entry.type === 'user') {
48
54
  const text = extractText(entry.message?.content);
49
55
  if (!text || entry.isCompactSummary || isSyntheticPrompt(text))
@@ -8,7 +8,7 @@ export function observationsFromTurns(turns, sessionId, project, config) {
8
8
  .map((turn) => buildObservation(turn, sessionId, project, config));
9
9
  }
10
10
  function withoutPrivate(turn) {
11
- const strip = (text) => text.replace(/<private>[\s\S]*?<\/private>/gi, '[private]');
11
+ const strip = (text) => text.replace(/<private>[\s\S]*?(?:<\/private>|$)/gi, '[private]');
12
12
  return { ...turn, prompt: strip(turn.prompt), reasoning: strip(turn.reasoning) };
13
13
  }
14
14
  function isSubstantive(turn, config) {
@@ -165,7 +165,7 @@ function shortPath(path) {
165
165
  function isExcluded(file, patterns) {
166
166
  return patterns.some((pattern) => file.includes(pattern));
167
167
  }
168
- const PRIVATE_BLOCK = /<private>[\s\S]*?<\/private>/gi;
168
+ const PRIVATE_BLOCK = /<private>[\s\S]*?(?:<\/private>|$)/gi;
169
169
  const PRIVATE_KEY = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g;
170
170
  export function redact(text) {
171
171
  return text
@@ -13,8 +13,15 @@ export async function cmdScan(argv) {
13
13
  const known = force
14
14
  ? new Map()
15
15
  : new Map((await ctx.store.scannedFiles(project)).map((f) => [f.path, f.hash]));
16
+ const existing = force ? [] : await ctx.store.findSymbols({ project, limit: 200000 });
16
17
  const started = Date.now();
17
- const scan = scanRepository({ root, project, known, ...(force ? { force } : {}) });
18
+ const scan = scanRepository({
19
+ root,
20
+ project,
21
+ known,
22
+ ...(force ? { force } : {}),
23
+ ...(existing.length > 0 ? { existing } : {}),
24
+ });
18
25
  if (force)
19
26
  await ctx.store.removeGraph(project);
20
27
  else if (scan.changed.length > 0)
@@ -43,7 +50,8 @@ export async function cmdUsages(argv) {
43
50
  const context = Number(valueOf(argv, '--context') ?? 0);
44
51
  const limit = Number(valueOf(argv, '--limit') ?? 100);
45
52
  const mode = valueOf(argv, '--mode') ?? 'text';
46
- const words = withoutFlags(argv, ['--path', '--context', '--limit', '--mode'])
53
+ const targetFlag = valueOf(argv, '--target');
54
+ const words = withoutFlags(argv, ['--path', '--context', '--limit', '--mode', '--target'])
47
55
  .filter((arg) => arg !== '--regex')
48
56
  .join(' ')
49
57
  .trim();
@@ -69,7 +77,8 @@ export async function cmdUsages(argv) {
69
77
  console.error(`Unknown --mode "${mode}". Use text, usages, explain or path.`);
70
78
  process.exit(1);
71
79
  }
72
- const [symbol, target] = words.split(/\s+/);
80
+ const [symbol, positional] = words.split(/\s+/);
81
+ const target = targetFlag ?? positional;
73
82
  if (mode === 'path' && !target) {
74
83
  console.error('--mode path needs two symbols: claude-db usages --mode path <from> <to>');
75
84
  process.exit(1);
@@ -79,16 +79,22 @@ async function syncSessions(local, remote) {
79
79
  return moved;
80
80
  }
81
81
  async function eachRemoteObservation(store, visit) {
82
- let after = 0;
82
+ let after;
83
+ let afterId;
83
84
  for (;;) {
84
- const batch = await store.list({ after, limit: BATCH });
85
+ const batch = await store.list({
86
+ ...(after === undefined ? {} : { after }),
87
+ ...(afterId === undefined ? {} : { afterId }),
88
+ limit: BATCH,
89
+ });
85
90
  if (batch.length === 0)
86
91
  return;
87
92
  await visit(batch);
88
93
  const last = batch[batch.length - 1];
89
94
  if (!last)
90
95
  return;
91
- after = last.createdAt === after ? after + 1 : last.createdAt;
96
+ after = last.createdAt;
97
+ afterId = last.id;
92
98
  if (batch.length < BATCH)
93
99
  return;
94
100
  }
@@ -1,4 +1,5 @@
1
1
  import { BATCH } from '../constants.js';
2
+ const SESSION_LIMIT = 10000;
2
3
  import { createContext } from '../../context.js';
3
4
  import { embedObservations } from '../../capture/index.js';
4
5
  import { loadConfig } from '../../config/index.js';
@@ -6,10 +7,16 @@ import { readFileSync } from 'node:fs';
6
7
  import { resolveProject } from '../../util/project.js';
7
8
  import { valueOf } from '../args.js';
8
9
  export async function eachObservation(ctx, filter, visit) {
9
- let after = 0;
10
+ let after;
11
+ let afterId;
10
12
  let total = 0;
11
13
  for (;;) {
12
- const batch = await ctx.store.list({ ...filter, after, limit: BATCH });
14
+ const batch = await ctx.store.list({
15
+ ...filter,
16
+ ...(after === undefined ? {} : { after }),
17
+ ...(afterId === undefined ? {} : { afterId }),
18
+ limit: BATCH,
19
+ });
13
20
  if (batch.length === 0)
14
21
  return total;
15
22
  await visit(batch);
@@ -17,7 +24,8 @@ export async function eachObservation(ctx, filter, visit) {
17
24
  const last = batch[batch.length - 1];
18
25
  if (!last)
19
26
  return total;
20
- after = last.createdAt === after ? after + 1 : last.createdAt;
27
+ after = last.createdAt;
28
+ afterId = last.id;
21
29
  if (batch.length < BATCH)
22
30
  return total;
23
31
  }
@@ -30,7 +38,22 @@ export async function cmdExport(argv) {
30
38
  for (const obs of batch)
31
39
  process.stdout.write(`${JSON.stringify(obs)}\n`);
32
40
  });
33
- process.stderr.write(`${count} observation(s) exported.\n`);
41
+ const projects = all
42
+ ? [
43
+ ...new Set([
44
+ ...(await ctx.store.listProjects()).map((entry) => entry.project),
45
+ ...(await ctx.store.sessionProjects()),
46
+ ]),
47
+ ]
48
+ : [resolveProject(undefined)];
49
+ let sessions = 0;
50
+ for (const project of projects) {
51
+ for (const session of await ctx.store.recentSessions(project, SESSION_LIMIT)) {
52
+ process.stdout.write(`${JSON.stringify({ ...session, _type: 'session' })}\n`);
53
+ sessions += 1;
54
+ }
55
+ }
56
+ process.stderr.write(`${count} observation(s) and ${sessions} session(s) exported.\n`);
34
57
  }
35
58
  finally {
36
59
  await ctx.close();
@@ -46,20 +69,47 @@ export async function cmdImport(path) {
46
69
  .filter((line) => line.trim().length > 0);
47
70
  const ctx = await createContext();
48
71
  let imported = 0;
72
+ let skipped = 0;
73
+ let restored = 0;
49
74
  try {
50
75
  for (let i = 0; i < lines.length; i += BATCH) {
51
- const batch = lines
52
- .slice(i, i + BATCH)
53
- .map((line) => JSON.parse(line))
54
- .filter((obs) => typeof obs.id === 'string' && typeof obs.project === 'string');
55
- await ctx.store.insertObservations(batch);
76
+ const batch = [];
77
+ for (const line of lines.slice(i, i + BATCH)) {
78
+ let parsed;
79
+ try {
80
+ parsed = JSON.parse(line);
81
+ }
82
+ catch {
83
+ skipped += 1;
84
+ continue;
85
+ }
86
+ if (parsed._type === 'session') {
87
+ const { _type, ...session } = parsed;
88
+ void _type;
89
+ if (typeof session.id === 'string' && typeof session.project === 'string') {
90
+ await ctx.store.upsertSession(session);
91
+ restored += 1;
92
+ }
93
+ else
94
+ skipped += 1;
95
+ continue;
96
+ }
97
+ if (typeof parsed.id === 'string' && typeof parsed.project === 'string')
98
+ batch.push(parsed);
99
+ else
100
+ skipped += 1;
101
+ }
102
+ if (batch.length > 0)
103
+ await ctx.store.insertObservations(batch);
56
104
  imported += batch.length;
57
105
  }
58
106
  }
59
107
  finally {
60
108
  await ctx.close();
61
109
  }
62
- console.log(`Imported ${imported} observation(s).`);
110
+ console.log(`Imported ${imported} observation(s)` +
111
+ `${restored > 0 ? `, ${restored} session(s)` : ''}` +
112
+ `${skipped > 0 ? `, skipped ${skipped} unreadable line(s)` : ''}.`);
63
113
  }
64
114
  export async function cmdPrune(argv) {
65
115
  const days = Number(valueOf(argv, '--older-than') ?? NaN);
@@ -7,7 +7,7 @@ import { resolveProject } from '../../util/project.js';
7
7
  import { toShortId } from '../../util/shortid.js';
8
8
  import { renderPage } from './view-page.js';
9
9
  async function collect(ctx, project) {
10
- const all = (await ctx.store.list({ project, limit: 500 })).sort((a, b) => b.createdAt - a.createdAt);
10
+ const all = (await ctx.store.list({ project, limit: 500, newest: true })).sort((a, b) => b.createdAt - a.createdAt);
11
11
  const kinds = {};
12
12
  for (const obs of all)
13
13
  kinds[obs.kind] = (kinds[obs.kind] ?? 0) + 1;
@@ -104,6 +104,11 @@ export const BASIC_FINGERPRINT = [
104
104
  CALLABLE.source,
105
105
  CALL.source,
106
106
  Object.keys(LABELS).join(','),
107
+ Object.entries(KEYWORDS)
108
+ .map(([word, kind]) => `${word}:${kind}`)
109
+ .sort()
110
+ .join(','),
111
+ [...NOT_A_CALL].sort().join(','),
107
112
  ].join('|');
108
113
  export const BASIC_LANGUAGES = Object.entries(LABELS).map(([label, extensions]) => ({
109
114
  id: 'basic',
@@ -19,9 +19,24 @@ const TYPED_DEFINITIONS = [
19
19
  { kind: 'type_alias_declaration', field: ['name'], symbol: 'type' },
20
20
  { kind: 'enum_declaration', field: ['name'], symbol: 'enum' },
21
21
  ];
22
+ const TYPE_DECLARATION_SITES = [
23
+ 'interface_declaration',
24
+ 'type_alias_declaration',
25
+ 'class_declaration',
26
+ 'abstract_class_declaration',
27
+ 'enum_declaration',
28
+ 'type_parameter',
29
+ 'implements_clause',
30
+ ];
22
31
  const TYPED_REFERENCES = [
23
32
  { kind: 'extends_clause', field: ['value'], relation: 'extends' },
24
33
  { kind: 'implements_clause', field: [], relation: 'implements' },
34
+ {
35
+ kind: 'type_identifier',
36
+ field: [],
37
+ relation: 'references',
38
+ excludeParents: TYPE_DECLARATION_SITES,
39
+ },
25
40
  ];
26
41
  export const typescript = {
27
42
  id: 'TypeScript',
@@ -9,6 +9,7 @@ export interface ReferenceRule {
9
9
  field: string[];
10
10
  relation: EdgeRelation;
11
11
  namePattern?: RegExp;
12
+ excludeParents?: string[];
12
13
  }
13
14
  export interface LanguageSpec {
14
15
  id: string;
@@ -13,6 +13,7 @@ export interface AstNode {
13
13
  kind(): string;
14
14
  range(): AstRange;
15
15
  field(name: string): AstNode | null;
16
+ parent(): AstNode | null;
16
17
  findAll(matcher: {
17
18
  rule: {
18
19
  kind: string;
@@ -29,11 +29,15 @@ export async function queryGraph(store, project, query) {
29
29
  const ids = answer.definitions.map((symbol) => symbol.id);
30
30
  const edges = await store.findEdges({
31
31
  project,
32
- ...(ids.length > 0 ? { srcIds: ids, dstIds: ids } : {}),
32
+ ...(ids.length > 0 ? { srcIds: ids, dstIds: ids } : { dstName: query.symbol }),
33
33
  limit: Math.max(query.limit * 10, 500),
34
34
  });
35
- answer.inbound = edges.filter((edge) => edge.dstName === query.symbol);
36
- answer.outbound = edges.filter((edge) => ids.includes(edge.srcId) && edge.dstName !== query.symbol);
35
+ const idSet = new Set(ids);
36
+ const pointsAtSymbol = (edge) => edge.dstName === query.symbol ||
37
+ edge.dstName.endsWith(`.${query.symbol}`) ||
38
+ (edge.dstId !== '' && idSet.has(edge.dstId));
39
+ answer.inbound = edges.filter(pointsAtSymbol);
40
+ answer.outbound = edges.filter((edge) => idSet.has(edge.srcId) && !pointsAtSymbol(edge));
37
41
  answer.empty = answer.definitions.length === 0 && answer.inbound.length === 0;
38
42
  if (answer.empty)
39
43
  answer.suggestions = await suggestFor(store, project, query.symbol);
@@ -7,10 +7,12 @@ export interface Reference {
7
7
  line: number;
8
8
  from: CodeSymbol | null;
9
9
  weak?: boolean;
10
+ to?: string;
11
+ origin?: string;
10
12
  }
11
13
  export interface Extraction {
12
14
  symbols: CodeSymbol[];
13
15
  references: Reference[];
14
16
  }
15
- export declare function symbolId(project: string, file: string, name: string, kind: string): string;
17
+ export declare function symbolId(project: string, file: string, name: string, kind: string, index?: number): string;
16
18
  export declare function extractFile(file: SourceFile, project: string): Extraction;
@@ -4,16 +4,27 @@ import { callsIn, declarationsIn } from '../languages/index.js';
4
4
  import { languageHandle, loadParser } from '../parser.js';
5
5
  const CALLABLE = new Set(['function', 'method', 'class']);
6
6
  const IDENTIFIER = /^[\w$]+[?!]?$/;
7
- export function symbolId(project, file, name, kind) {
8
- return observationId('graph', 0, `${project}\0${file}\0${name}\0${kind}`);
7
+ export function symbolId(project, file, name, kind, index = 0) {
8
+ const seed = `${project}\0${file}\0${name}\0${kind}`;
9
+ return observationId('graph', 0, index === 0 ? seed : `${seed}\0${index}`);
10
+ }
11
+ function counter() {
12
+ const seen = new Map();
13
+ return (name, kind) => {
14
+ const key = `${name}\0${kind}`;
15
+ const index = seen.get(key) ?? 0;
16
+ seen.set(key, index + 1);
17
+ return index;
18
+ };
9
19
  }
10
20
  function extractByPattern(file, project) {
11
21
  const lines = file.source.split('\n');
12
22
  const symbols = [];
13
23
  const spans = [];
24
+ const occurrence = counter();
14
25
  for (const declaration of declarationsIn(file.source)) {
15
26
  const symbol = {
16
- id: symbolId(project, file.path, declaration.name, declaration.kind),
27
+ id: symbolId(project, file.path, declaration.name, declaration.kind, occurrence(declaration.name, declaration.kind)),
17
28
  project,
18
29
  name: declaration.name,
19
30
  kind: declaration.kind,
@@ -76,6 +87,18 @@ function enclosing(line, spans) {
76
87
  }
77
88
  return (best ?? fallback)?.symbol ?? null;
78
89
  }
90
+ function namedTargets(node) {
91
+ if (!/\s/.test(node.text().trim()))
92
+ return [node];
93
+ const inner = [];
94
+ for (const kind of ['type_identifier', 'identifier']) {
95
+ try {
96
+ inner.push(...node.findAll({ rule: { kind } }));
97
+ }
98
+ catch { }
99
+ }
100
+ return inner.length > 0 ? inner : [node];
101
+ }
79
102
  export function extractFile(file, project) {
80
103
  if (file.spec.basic)
81
104
  return extractByPattern(file, project);
@@ -84,6 +107,7 @@ export function extractFile(file, project) {
84
107
  const lines = file.source.split('\n');
85
108
  const symbols = [];
86
109
  const spans = [];
110
+ const occurrence = counter();
87
111
  for (const rule of file.spec.definitions) {
88
112
  for (const node of root.findAll({ rule: { kind: rule.kind } })) {
89
113
  const named = resolveField(node, rule.field);
@@ -92,7 +116,7 @@ export function extractFile(file, project) {
92
116
  continue;
93
117
  const line = named.range().start.line + 1;
94
118
  const symbol = {
95
- id: symbolId(project, file.path, name, rule.symbol),
119
+ id: symbolId(project, file.path, name, rule.symbol, occurrence(name, rule.symbol)),
96
120
  project,
97
121
  name,
98
122
  kind: rule.symbol,
@@ -120,29 +144,39 @@ export function extractFile(file, project) {
120
144
  relation: 'defines',
121
145
  line: span.start,
122
146
  from: owner,
147
+ to: span.symbol.id,
123
148
  });
124
149
  }
125
150
  for (const rule of file.spec.references) {
126
151
  for (const node of root.findAll({ rule: { kind: rule.kind } })) {
127
- const target = rule.field.length === 0 ? node : resolveField(node, rule.field);
128
- const raw = target?.text();
129
- if (!target || !raw)
130
- continue;
131
- const name = unquote(raw.trim());
132
- if (!name || /\s/.test(name))
133
- continue;
134
- if (rule.relation === 'references' && name.includes('.'))
152
+ const matched = rule.field.length === 0 ? node : resolveField(node, rule.field);
153
+ if (!matched)
135
154
  continue;
136
- if (rule.namePattern && !rule.namePattern.test(name))
137
- continue;
138
- const line = target.range().start.line + 1;
139
- references.push({
140
- file: file.path,
141
- name,
142
- relation: rule.relation,
143
- line,
144
- from: enclosing(line, spans),
145
- });
155
+ const targets = rule.field.length === 0 ? namedTargets(matched) : [matched];
156
+ for (const target of targets) {
157
+ const raw = target.text();
158
+ if (!raw)
159
+ continue;
160
+ const name = unquote(raw.trim());
161
+ if (!name || /\s/.test(name))
162
+ continue;
163
+ if (rule.relation === 'references' && name.includes('.'))
164
+ continue;
165
+ if (rule.namePattern && !rule.namePattern.test(name))
166
+ continue;
167
+ if (rule.excludeParents?.includes(node.parent()?.kind() ?? ''))
168
+ continue;
169
+ const line = target.range().start.line + 1;
170
+ const span = node.range();
171
+ references.push({
172
+ file: file.path,
173
+ name,
174
+ relation: rule.relation,
175
+ line,
176
+ from: enclosing(line, spans),
177
+ origin: `${span.start.line}:${span.start.column}-${span.end.line}:${span.end.column}`,
178
+ });
179
+ }
146
180
  }
147
181
  }
148
182
  return { symbols, references };