@mnemonik/shared 6.50.0 → 6.51.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 (60) hide show
  1. package/dist/ast/astChunker.d.ts +124 -0
  2. package/dist/ast/astChunker.d.ts.map +1 -0
  3. package/dist/ast/astChunker.js +559 -0
  4. package/dist/ast/astChunker.js.map +1 -0
  5. package/dist/ast/grammars.d.ts +170 -0
  6. package/dist/ast/grammars.d.ts.map +1 -0
  7. package/dist/ast/grammars.js +411 -0
  8. package/dist/ast/grammars.js.map +1 -0
  9. package/dist/codeScanner.d.ts +59 -0
  10. package/dist/codeScanner.d.ts.map +1 -1
  11. package/dist/codeScanner.js +259 -2
  12. package/dist/codeScanner.js.map +1 -1
  13. package/dist/index.d.ts +3 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -1
  16. package/dist/index.js.map +1 -1
  17. package/package.json +3 -2
  18. package/queries/bash.tags.scm +7 -0
  19. package/queries/groovy.tags.scm +15 -0
  20. package/queries/powershell.tags.scm +14 -0
  21. package/scripts/vendor-grammars.ts +317 -0
  22. package/src/ast/astChunker.ts +661 -0
  23. package/src/ast/grammars.ts +490 -0
  24. package/src/codeScanner.ts +273 -3
  25. package/src/index.ts +22 -0
  26. package/wasm/bash.tags.scm +7 -0
  27. package/wasm/bash.wasm +0 -0
  28. package/wasm/c-sharp.tags.scm +23 -0
  29. package/wasm/c-sharp.wasm +0 -0
  30. package/wasm/c.tags.scm +9 -0
  31. package/wasm/c.wasm +0 -0
  32. package/wasm/cpp.tags.scm +15 -0
  33. package/wasm/cpp.wasm +0 -0
  34. package/wasm/elixir.tags.scm +54 -0
  35. package/wasm/elixir.wasm +0 -0
  36. package/wasm/go.tags.scm +42 -0
  37. package/wasm/go.wasm +0 -0
  38. package/wasm/groovy.tags.scm +15 -0
  39. package/wasm/groovy.wasm +0 -0
  40. package/wasm/java.tags.scm +20 -0
  41. package/wasm/java.wasm +0 -0
  42. package/wasm/javascript.tags.scm +99 -0
  43. package/wasm/javascript.wasm +0 -0
  44. package/wasm/php.tags.scm +40 -0
  45. package/wasm/php.wasm +0 -0
  46. package/wasm/powershell.tags.scm +14 -0
  47. package/wasm/powershell.wasm +0 -0
  48. package/wasm/python.tags.scm +14 -0
  49. package/wasm/python.wasm +0 -0
  50. package/wasm/ruby.tags.scm +64 -0
  51. package/wasm/ruby.wasm +0 -0
  52. package/wasm/rust.tags.scm +60 -0
  53. package/wasm/rust.wasm +0 -0
  54. package/wasm/scala.tags.scm +66 -0
  55. package/wasm/scala.wasm +0 -0
  56. package/wasm/solidity.tags.scm +43 -0
  57. package/wasm/solidity.wasm +0 -0
  58. package/wasm/tsx.wasm +0 -0
  59. package/wasm/typescript.tags.scm +23 -0
  60. package/wasm/typescript.wasm +0 -0
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The AST chunker: source text in, named line-ranges out. No filesystem, no
3
+ * logging of file contents, one pure entry point (`chunkWithAst`).
4
+ *
5
+ * WHY THIS EXISTS. The heuristic chunker matches a regex whose name pattern
6
+ * requires a leading `function`/`class`/`const` keyword — a keyword a method's
7
+ * first line never has. So it produces method-shaped chunks with no
8
+ * `symbolName`: 1,512 methods in this repo, unnameable and therefore unusable
9
+ * as citation anchors. Measured over the same 1,037 TypeScript files, the
10
+ * heuristic named 16.9% of its 16,604 chunks; the tag queries name 100% of
11
+ * 6,471 definitions.
12
+ *
13
+ * THE NESTING RULE. Emitting both a whole class and each of its methods would
14
+ * duplicate every method body and double the embedding bill, so:
15
+ *
16
+ * - a STRUCTURAL definition — one whose body is a container of other definitions
17
+ * (class, interface, enum, struct, trait, object, module, namespace) — emits a
18
+ * HEADER chunk from its own first line to the line before its first nested
19
+ * definition: the declaration plus the fields that precede the first method.
20
+ * Real contiguous source, never an elision;
21
+ * - every other definition is a LEAF and becomes one chunk over its FULL line
22
+ * range, whatever it happens to contain. A local arrow function is part of
23
+ * what the enclosing function IS, not a sibling of it, so definitions nested
24
+ * inside a leaf are not emitted separately — the leaf's chunk already carries
25
+ * their source verbatim. See `STRUCTURAL_KINDS` for why that distinction is
26
+ * load-bearing rather than cosmetic;
27
+ * - every remaining uncovered line span becomes a `raw` chunk.
28
+ *
29
+ * Coverage is therefore total: every non-blank line of a file belongs to some
30
+ * chunk, replacing the ~50% coverage of the old heuristic and giving citation
31
+ * anchoring a line -> chunk map with no holes.
32
+ *
33
+ * A NEWLINE IS A TERMINATOR, NOT A LINE. `'...}\n'.split('\n')` yields a phantom
34
+ * trailing `''` that is not a line of the file. It is dropped before any length
35
+ * arithmetic, because `memory_file_index.end_line` feeds citation anchoring and
36
+ * the golden eval's containment scoring, and a one-line drift misaligns both
37
+ * silently.
38
+ *
39
+ * CHUNKS ARE LINE-GRANULAR, NOT BYTE-GRANULAR. A chunk's content is always
40
+ * `lines.slice(startLine - 1, endLine).join('\n')`. That is deliberate: the tag
41
+ * queries capture `class_declaration`, which starts at `class` and excludes the
42
+ * `export ` in front of it, and a citation anchor that omitted `export` would
43
+ * not match what a reader sees. Line granularity also keeps content verbatim
44
+ * and contiguous by construction.
45
+ *
46
+ * SYMBOL NAMES ARE BARE. `verifyForFile`, not `CitationManager.verifyForFile`.
47
+ * `memory_file_index.symbol_name` is one column with a partial index on
48
+ * `(project_id, symbol_name)` and citations reference bare names; storing
49
+ * qualified names would break lookup. Qualified names are a deliberate
50
+ * deferral — see the plan's Task 11 gate.
51
+ *
52
+ * WASM MEMORY IS NOT GC'd BY THE JS HEAP. Every `Tree` and `TreeCursor` created
53
+ * here is explicitly deleted in a `finally`. Skipping that leaks across a
54
+ * whole-repo walk in a long-lived daemon (the scanner is one).
55
+ */
56
+ import { type AstLanguageId } from './grammars.js';
57
+ export interface AstChunk {
58
+ /** Verbatim contiguous source: exactly the lines `startLine..endLine`. */
59
+ content: string;
60
+ /** 1-based, inclusive. */
61
+ startLine: number;
62
+ /** 1-based, inclusive. */
63
+ endLine: number;
64
+ /** The existing wire enum. The finer classification lives in `symbolKind`. */
65
+ chunkType: 'function' | 'class' | 'module' | 'raw';
66
+ /**
67
+ * Bare symbol name, never qualified. Present on every chunk that came from a
68
+ * `definition.*` capture — INCLUDING the value-shaped kinds that map to
69
+ * `chunkType: 'raw'` (`variable`, `constant`, `field`, `property`), because the
70
+ * wire enum has no value-shaped member but the identity is real and
71
+ * `memory_file_index.symbol_name` indexes it either way. Absent only on the
72
+ * uncovered-span chunks, which have no capture behind them — those are exactly
73
+ * the chunks with no `symbolKind` either.
74
+ */
75
+ symbolName?: string;
76
+ /** The tag capture verbatim: 'function' | 'class' | 'method' | 'module' | ... */
77
+ symbolKind?: string;
78
+ /** The definition's first line, trimmed, capped at 500 chars. */
79
+ signature?: string;
80
+ }
81
+ export type AstChunkResult = {
82
+ chunks: AstChunk[];
83
+ errorNodes: number;
84
+ parseMs: number;
85
+ /**
86
+ * Definitions that reached no chunk. Two chunks over one line range would
87
+ * collide on `filePath:startLine-endLine`, the staleness key ProjectManager
88
+ * compares, so a second definition sharing a range — `function a() {}
89
+ * function b() {}` on one line — cannot be emitted, and neither can a
90
+ * container whose first nested definition opens on the container's own
91
+ * first line (`class A { m() {} }`). Two captures can also COLLAPSE onto one
92
+ * range while widening: `int f(int), g(int);` binds two declarators to one
93
+ * declaration (see `CollectedDefinitions.collapsedDefinitions`). All three
94
+ * are counted here. The name is then absent from
95
+ * `memory_file_index` and a citation to it resolves as
96
+ * `unresolved_symbol`; counting it is what stops that being invisible.
97
+ */
98
+ droppedDefinitions: number;
99
+ } | {
100
+ unsupported: 'grammar_unavailable' | 'parse_failed' | 'file_too_large';
101
+ detail: string;
102
+ };
103
+ /**
104
+ * Parse ceiling, derived from Task 4's measurement rather than guessed: the
105
+ * largest source file in this repo is 415 KB (`src/agent/ContextAgent.ts`) and
106
+ * the slowest per-file parse across 1,061 files was 108.6 ms. 2 MiB is ~5x that
107
+ * largest real file, which bounds the worst case near half a second — while
108
+ * still refusing the multi-megabyte generated blobs that `MAX_SCANNED_FILE_BYTES`
109
+ * (10 MB) otherwise lets through. Above the ceiling the caller degrades to the
110
+ * heuristic chunker, which is line-bounded and cheap.
111
+ */
112
+ export declare const MAX_AST_PARSE_BYTES: number;
113
+ /**
114
+ * Chunk `content` by syntax for a language with a vendored grammar.
115
+ *
116
+ * Async only because `loadGrammar` is; the grammar is cached per process, so
117
+ * after the first file of a given language there is no per-file async cost.
118
+ * Never throws — every failure is a named `unsupported` reason with a detail the
119
+ * caller can log before degrading.
120
+ */
121
+ export declare function chunkWithAst(content: string, languageId: AstLanguageId, opts?: {
122
+ maxParseBytes?: number;
123
+ }): Promise<AstChunkResult>;
124
+ //# sourceMappingURL=astChunker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"astChunker.d.ts","sourceRoot":"","sources":["../../src/ast/astChunker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AAIH,OAAO,EAAe,KAAK,aAAa,EAAuB,MAAM,eAAe,CAAC;AAErF,MAAM,WAAW,QAAQ;IACvB,0EAA0E;IAC1E,OAAO,EAAE,MAAM,CAAC;IAChB,0BAA0B;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,0BAA0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,SAAS,EAAE,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,CAAC;IACnD;;;;;;;;OAQG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iFAAiF;IACjF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iEAAiE;IACjE,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MAAM,cAAc,GACtB;IACE,MAAM,EAAE,QAAQ,EAAE,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;;OAYG;IACH,kBAAkB,EAAE,MAAM,CAAC;CAC5B,GACD;IAAE,WAAW,EAAE,qBAAqB,GAAG,cAAc,GAAG,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/F;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,QAAkB,CAAC;AAoGnD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,aAAa,EACzB,IAAI,CAAC,EAAE;IAAE,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,GAChC,OAAO,CAAC,cAAc,CAAC,CAiEzB"}