@agenthead/graph 0.0.0-stage → 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.
- package/LICENSE +201 -0
- package/README.md +376 -2
- package/THIRD_PARTY_NOTICES.md +474 -0
- package/dist/graph-signals.d.ts +4 -0
- package/dist/graph-signals.js +101 -0
- package/dist/graph-signals.js.map +1 -0
- package/dist/graph-store.d.ts +38 -0
- package/dist/graph-store.js +273 -0
- package/dist/graph-store.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/load-signals.d.ts +7 -0
- package/dist/load-signals.js +35 -0
- package/dist/load-signals.js.map +1 -0
- package/dist/lsp-bridge.d.ts +91 -0
- package/dist/lsp-bridge.js +454 -0
- package/dist/lsp-bridge.js.map +1 -0
- package/dist/lsp-client.d.ts +42 -0
- package/dist/lsp-client.js +233 -0
- package/dist/lsp-client.js.map +1 -0
- package/dist/migrations.d.ts +3 -0
- package/dist/migrations.js +32 -0
- package/dist/migrations.js.map +1 -0
- package/dist/parser.d.ts +3 -0
- package/dist/parser.js +404 -0
- package/dist/parser.js.map +1 -0
- package/dist/resolve-import.d.ts +3 -0
- package/dist/resolve-import.js +71 -0
- package/dist/resolve-import.js.map +1 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,377 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agenthead/graph
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A persistent, incremental **syntactic** code graph for one project. Requires
|
|
4
|
+
Node 22 or newer. It parses `.ts`, `.mts`, `.cts`, `.tsx`, `.js`, `.jsx`, `.mjs`,
|
|
5
|
+
`.cjs` and `.py` using Tree-sitter WebAssembly grammars. No Python interpreter,
|
|
6
|
+
compiler service or native parser build is required.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { GraphStore } from "@agenthead/graph";
|
|
10
|
+
|
|
11
|
+
const graph = new GraphStore("/path/to/project", {
|
|
12
|
+
excludedPaths: [/^generated\//],
|
|
13
|
+
});
|
|
14
|
+
try {
|
|
15
|
+
const report = await graph.index();
|
|
16
|
+
console.log(`${report.reusePercentage.toFixed(1)}% files reused`);
|
|
17
|
+
console.log(graph.definitions("parse"));
|
|
18
|
+
console.log(graph.references("parse", "src/main.ts")); // optional file filter
|
|
19
|
+
console.log(graph.calls("parse")); // terminal callee name; omit to list all
|
|
20
|
+
console.log(graph.imports("src/main.ts"));
|
|
21
|
+
console.log(graph.importers("src/parser.ts")); // unique sorted file paths
|
|
22
|
+
console.log(graph.nameAt("src/main.ts", 3, 10)); // name recorded at a position
|
|
23
|
+
console.log(graph.snapshot()); // complete graph, ordered by file/source order
|
|
24
|
+
} finally {
|
|
25
|
+
graph.close();
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The project directory must already exist. Await `index()` before closing or
|
|
30
|
+
indexing again on the same store. Queries return the last committed snapshot;
|
|
31
|
+
run `index()` again to observe edits and deletions. Paths returned by the API are
|
|
32
|
+
canonical, project-relative and slash separated. File query arguments accept
|
|
33
|
+
relative or absolute paths within the project. Results have one-based lines and
|
|
34
|
+
UTF-16 columns, with exclusive end positions.
|
|
35
|
+
|
|
36
|
+
## Context pack ranking
|
|
37
|
+
|
|
38
|
+
`GraphStore.signals()` returns a detached implementation of core's optional
|
|
39
|
+
`GraphSignals` interface. It resolves task-named paths, basenames, and exact
|
|
40
|
+
case-sensitive symbol names (including qualified names such as `Counter.run`).
|
|
41
|
+
All matching definitions become anchors when a symbol name is ambiguous.
|
|
42
|
+
`distance(fromFiles, toFile)` returns the shortest directed import distance:
|
|
43
|
+
0 for the same indexed file, 1 for a direct dependency, and `undefined` for
|
|
44
|
+
unknown or unreachable files. Resolved imports, re-exports, literal `require()`
|
|
45
|
+
and dynamic imports supply edges; unresolved modules and textual call names do
|
|
46
|
+
not. `impacted(files)` follows those edges in reverse, returning changed files
|
|
47
|
+
and all transitive importers in sorted order. Traversal handles cycles and
|
|
48
|
+
duplicate imports; distances are cached for each supplied root array.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { buildPack } from "@agenthead/core";
|
|
52
|
+
import { loadGraphSignals } from "@agenthead/graph";
|
|
53
|
+
|
|
54
|
+
const pack = buildPack({
|
|
55
|
+
task: "Refactor Counter.run",
|
|
56
|
+
candidates,
|
|
57
|
+
budget: 4_000,
|
|
58
|
+
graphSignals: loadGraphSignals(project),
|
|
59
|
+
changedFiles: ["src/counter.ts"], // optional explicit change roots
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`loadGraphSignals(project, options?)` opens an existing `.agenthead/graph.db`, takes
|
|
64
|
+
a ranking/outline snapshot and closes the connection. It returns `undefined` when no
|
|
65
|
+
index exists, without creating a database or reading source contents. Existing
|
|
66
|
+
graph boundary/exclusion and storage-symlink checks apply. Signals remain usable
|
|
67
|
+
after the connection closes and reflect the last index, so refresh the graph
|
|
68
|
+
after source changes. CLI and MCP pack commands load these signals on each
|
|
69
|
+
request when the index exists; no index preserves their graph-free behavior.
|
|
70
|
+
Signals rerank supplied evidence and do not create additional evidence records.
|
|
71
|
+
The optional `outline(file, hash)` returns cached declaration signatures and
|
|
72
|
+
structure only for matching full-file evidence. `mapPath` in the loader options
|
|
73
|
+
maps prefixed evidence paths consistently across all provider methods; see
|
|
74
|
+
[code graph](../../docs/graph.md) and [packing](../../docs/packing.md).
|
|
75
|
+
|
|
76
|
+
## Extraction and queries
|
|
77
|
+
|
|
78
|
+
Definitions include functions, classes, methods, variable/assignment bindings
|
|
79
|
+
(including destructuring), TS interfaces, type aliases and enums. Function-valued
|
|
80
|
+
variables are functions. Parameters are excluded from definitions. A qualified
|
|
81
|
+
name records enclosing declarations, such as `Counter.run` or `run.result`.
|
|
82
|
+
|
|
83
|
+
Imports retain the literal module specifier and ESM/Python aliases. JS re-exports,
|
|
84
|
+
literal `require()` and literal dynamic `import()` also create import edges.
|
|
85
|
+
CJS variable declarations record namespace bindings and direct object-destructured
|
|
86
|
+
bindings, including aliases and defaults. Dynamic-import bindings are empty;
|
|
87
|
+
TS import-equals records its alias. Calls record the textual callee, its terminal
|
|
88
|
+
name, the enclosing named function, and `call` or `new`. References
|
|
89
|
+
record identifier, type and property occurrences, including JSX names, excluding
|
|
90
|
+
recorded declaration names, parameters, import clauses, object keys, comments
|
|
91
|
+
and strings. Python assignments are syntactic definition occurrences, including
|
|
92
|
+
reassignments.
|
|
93
|
+
|
|
94
|
+
`definitions(name)` and `references(name)` match exact, case-sensitive **names**;
|
|
95
|
+
an optional file narrows the results. They do not resolve shadowing, follow
|
|
96
|
+
aliases across files, infer receiver types or distinguish unrelated same-named
|
|
97
|
+
bindings. Calls are syntax edges with textual targets, not a resolved runtime
|
|
98
|
+
call graph. Dynamic dispatch, computed module specifiers and compiler-level
|
|
99
|
+
language semantics require a later semantic layer. Parse errors are tolerated:
|
|
100
|
+
valid portions of the tree are indexed and `filesWithErrors` reports affected
|
|
101
|
+
files on every run.
|
|
102
|
+
|
|
103
|
+
Relative JS imports resolve against the permitted source inventory using direct
|
|
104
|
+
files, extensionless files and `index` files. NodeNext emitted `.js`, `.jsx`,
|
|
105
|
+
`.mjs` and `.cjs` specifiers also map to their TS source extensions. Python dotted
|
|
106
|
+
absolute imports are rooted at the project; leading dots resolve relative to the
|
|
107
|
+
importing package, including `__init__.py`. Absolute imports also try ancestors
|
|
108
|
+
reached through the importer's `__init__.py` package chain, then top-level `src/`.
|
|
109
|
+
Each Python `from` binding has an import edge and can resolve to a child module.
|
|
110
|
+
Bare JS packages, tsconfig aliases, package exports, Python `sys.path` and
|
|
111
|
+
namespace-package search paths are not interpreted. External/unresolved imports
|
|
112
|
+
remain in the graph with `target: null`; they are never read. Escaped module
|
|
113
|
+
specifiers and nonliteral imports are skipped.
|
|
114
|
+
|
|
115
|
+
## Storage and incremental indexing
|
|
116
|
+
|
|
117
|
+
Graph data lives in the project's **`.agenthead/graph.db`**, with SQLite WAL and
|
|
118
|
+
foreign keys. This separate, rebuildable cache keeps graph schema/parser releases
|
|
119
|
+
independent of immutable evidence migrations and avoids coupling parser tables to
|
|
120
|
+
`EvidenceStore`'s private connection. Schema migration 1 creates file hashes,
|
|
121
|
+
parser versions and indexed symbol/import/call/reference rows. Unknown future
|
|
122
|
+
schema versions fail rather than overwrite data. Evidence remains in `agenthead.db`.
|
|
123
|
+
Close all graph connections before deleting `graph.db` and its sidecars to rebuild.
|
|
124
|
+
|
|
125
|
+
Every run reads every supported file and SHA-256 hashes valid UTF-8 sources.
|
|
126
|
+
Only a new file, a
|
|
127
|
+
changed content hash or an invalidated extractor version causes parsing. Size and
|
|
128
|
+
mtime do not decide reuse. Changed-file entries are replaced; deleted, newly
|
|
129
|
+
excluded or skipped files cascade-delete their entries. All imports are cheaply relinked
|
|
130
|
+
against the new inventory, so adding/deleting targets updates edges from reused
|
|
131
|
+
files without parsing them. No parser state is needed across process restarts.
|
|
132
|
+
|
|
133
|
+
`IndexReport` contains `totalFiles`, `parsedFiles`, `reusedFiles`, `removedFiles`,
|
|
134
|
+
`reusePercentage` (0–100), `filesWithErrors` and `changes`. `totalFiles` includes skipped
|
|
135
|
+
sources; `parsedFiles` and `reusedFiles` exclude them. `changes` lists the paths
|
|
136
|
+
behind the counts, each list sorted: `added` (parsed files new to the graph),
|
|
137
|
+
`changed` (parsed files it already had, including those an extractor version
|
|
138
|
+
change reparsed) and `removed`. Reuse is
|
|
139
|
+
`100 * reusedFiles / totalFiles`; an empty project reports 100%. Source bytes and
|
|
140
|
+
parse results are staged before an atomic database update. Read failures preserve
|
|
141
|
+
the previous graph. Invalid UTF-8 and NUL-containing sources are skipped and
|
|
142
|
+
reported in `filesWithErrors` on every run; their old entries are removed and
|
|
143
|
+
imports targeting them become unresolved.
|
|
144
|
+
Tree-sitter trees and parsers are freed after each parse; grammars are shared.
|
|
145
|
+
|
|
146
|
+
All source enumeration and reads use core `WorkspaceBoundary.listSourceFiles()`
|
|
147
|
+
and `readSource()`. Its default secret-path exclusions and caller exclusions apply
|
|
148
|
+
to both supplied and canonical paths. `.agenthead` is never a source. The graph also
|
|
149
|
+
skips `.git`, `node_modules`, `dist`, `build`, `.next`, `out`, `coverage`,
|
|
150
|
+
`__pycache__`, `.venv` and `venv` components, plus `*.min.js` files. This does not
|
|
151
|
+
interpret `.gitignore`. File symlinks within the boundary
|
|
152
|
+
are deduplicated by canonical path; directory symlinks are not traversed. Queries
|
|
153
|
+
also apply the current exclusions to stored rows. Storage and SQLite sidecars
|
|
154
|
+
reject symlinks. The boundary's documented hard-link and concurrent directory
|
|
155
|
+
replacement limitations still apply. Graph data contains source-derived names and
|
|
156
|
+
callee expressions; it does not pass through evidence's text redaction filter.
|
|
157
|
+
|
|
158
|
+
## Language servers
|
|
159
|
+
|
|
160
|
+
`LspBridge` asks an installed language server for the definitions of, and
|
|
161
|
+
references to, the symbol at a position. When no server is available or healthy,
|
|
162
|
+
it answers from the graph instead. It is optional: nothing starts until a query
|
|
163
|
+
needs a server. It lives in this package because it answers from `GraphStore`
|
|
164
|
+
and checks every path with the graph's boundary. It has its own entry point,
|
|
165
|
+
`@agenthead/graph/lsp`, so importing the package root never loads the code
|
|
166
|
+
that starts processes.
|
|
167
|
+
|
|
168
|
+
The CLI's `agenthead definitions` and `agenthead references` and the MCP server's
|
|
169
|
+
`definitions` and `references` tools use it. Because a language server can run
|
|
170
|
+
code from the project (see Trust), they start servers only when their
|
|
171
|
+
environment has `AGENTHEAD_LSP=on`; then they use the default servers.
|
|
172
|
+
`lspOptionsFromEnv(env)`, exported here, is that switch: `on` gives `{}`, and
|
|
173
|
+
unset, empty or `off` disables both servers, so the graph answers with reason
|
|
174
|
+
`disabled`. Any other value throws. Each command or tool indexes the graph
|
|
175
|
+
first, so the fallback and the files opened for references are current. The
|
|
176
|
+
CLI starts a bridge per command and closes it before exiting; the MCP server
|
|
177
|
+
keeps one for its lifetime, passes it every index report, and closes it on
|
|
178
|
+
shutdown.
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { GraphStore } from "@agenthead/graph";
|
|
182
|
+
import { LspBridge } from "@agenthead/graph/lsp";
|
|
183
|
+
|
|
184
|
+
const graph = new GraphStore("/path/to/project");
|
|
185
|
+
await graph.index();
|
|
186
|
+
const lsp = new LspBridge(graph, {
|
|
187
|
+
startupTimeoutMs: 30_000,
|
|
188
|
+
timeoutMs: 30_000,
|
|
189
|
+
});
|
|
190
|
+
try {
|
|
191
|
+
// After edits, index again and tell the running servers what changed.
|
|
192
|
+
lsp.filesChanged(await graph.index());
|
|
193
|
+
// Any graph location is a position: one-based line, UTF-16 column.
|
|
194
|
+
for (const call of graph.references("parse", "src/main.ts")) {
|
|
195
|
+
const found = await lsp.definitions(call);
|
|
196
|
+
// { source: "lsp", locations: [{ file: "src/parser.ts", line: 3, ... }] }
|
|
197
|
+
// or { source: "graph", reason: "unavailable", locations: [...] }
|
|
198
|
+
for (const definition of found.locations)
|
|
199
|
+
console.log(await lsp.references(definition));
|
|
200
|
+
}
|
|
201
|
+
} finally {
|
|
202
|
+
await lsp.close();
|
|
203
|
+
graph.close();
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**Servers.** `.ts`, `.mts`, `.cts`, `.tsx`, `.js`, `.mjs`, `.cjs` and `.jsx` files
|
|
208
|
+
use `typescript-language-server --stdio`; `.py` files use
|
|
209
|
+
`pyright-langserver --stdio`. Each of the two starts at most once, on its first
|
|
210
|
+
query. Replace one with `typescript` or `python` set to `{ command, args }`, for
|
|
211
|
+
example `{ command: "basedpyright-langserver", args: ["--stdio"] }`, or disable it
|
|
212
|
+
with `false`. `command` is an absolute path, used as given, or a bare name looked
|
|
213
|
+
up on `PATH`. The lookup skips relative entries, and directories and executables
|
|
214
|
+
that resolve inside the project, so it never picks an executable the project
|
|
215
|
+
provides; configure an absolute path to use one from the project. The server runs
|
|
216
|
+
in the project root with AgentHead's environment, minus relative `PATH` entries,
|
|
217
|
+
which would resolve inside the project, and with `TMPDIR`, `TMP` and `TEMP` set to
|
|
218
|
+
the bridge's own temporary directory. The bridge makes that directory, readable
|
|
219
|
+
only by you, in the system temporary directory when its first server starts, and
|
|
220
|
+
`close()` removes it with whatever the servers left there, such as the files of a
|
|
221
|
+
server that was killed. If Node exits without `close()`, it stays. That is not
|
|
222
|
+
isolation (see Trust).
|
|
223
|
+
AgentHead never installs a server and makes no network requests. It starts the
|
|
224
|
+
server without a shell and discards its stderr.
|
|
225
|
+
|
|
226
|
+
**Protocol.** The client speaks JSON-RPC 2.0 over stdio with `Content-Length`
|
|
227
|
+
framing. It sends `initialize` with its `processId`, the project as `rootUri` and
|
|
228
|
+
as the only `workspaceFolders` entry (pyright ignores `rootUri`), UTF-16
|
|
229
|
+
positions, definition links and watched-file changes that the client sends
|
|
230
|
+
without registration, then `initialized`. It passes
|
|
231
|
+
typescript-language-server two options: `disableAutomaticTypingAcquisition`, so
|
|
232
|
+
tsserver does not download `@types` packages, and
|
|
233
|
+
`tsserver.useSyntaxServer: "never"`, because the syntax server answers while the
|
|
234
|
+
project loads and knows only the open files. Queries are
|
|
235
|
+
`textDocument/definition` and `textDocument/references` with
|
|
236
|
+
`includeDeclaration: false`, matching graph references, which exclude declaration
|
|
237
|
+
names. Requests from the server are answered with MethodNotFound and its
|
|
238
|
+
notifications are ignored. From a `LocationLink` the bridge keeps the target
|
|
239
|
+
selection range, the declared name.
|
|
240
|
+
|
|
241
|
+
**Open files.** A server searches only the files it knows. Pyright lists the
|
|
242
|
+
project's files in the background after it starts and reports the end only in a
|
|
243
|
+
log message, and tsserver without a `tsconfig.json` knows only open files and what they
|
|
244
|
+
import. So before asking for references, the bridge opens every file of the same
|
|
245
|
+
language where the graph records the name at the position, and every importer of
|
|
246
|
+
a file that defines it. A file outside that set can still be missed, such as one
|
|
247
|
+
that only uses the symbol under another name imported through a re-export.
|
|
248
|
+
|
|
249
|
+
**Freshness.** A server reads an open file from its client, not from disk. Before
|
|
250
|
+
each query the bridge reads again, through the boundary, every file it has opened
|
|
251
|
+
on that server. It sends `textDocument/didChange` with the full text of a file
|
|
252
|
+
that changed, and `textDocument/didClose` for one that is gone or no longer UTF-8.
|
|
253
|
+
Then it opens the files the query needs with `textDocument/didOpen`. Files stay
|
|
254
|
+
open, so each query reads every file the bridge has opened so far. Files the
|
|
255
|
+
bridge never opened are the server's to read: tsserver watches them for changes,
|
|
256
|
+
but pyright relies on its client to report changes. So pass the report of every
|
|
257
|
+
`graph.index()` to `lsp.filesChanged(report)`. The bridge sends each running
|
|
258
|
+
server `workspace/didChangeWatchedFiles` for the files of its language in
|
|
259
|
+
`report.changes`: added files as created, changed ones as changed, and removed
|
|
260
|
+
ones as deleted, or as changed if the file is still there (one that is no
|
|
261
|
+
longer UTF-8, say). A server that has not started yet gets nothing; it reads the
|
|
262
|
+
files when it starts. Pyright sees an edit to a file the bridge has not opened
|
|
263
|
+
only after an index run reports it.
|
|
264
|
+
|
|
265
|
+
**Results.** A location is kept only if its URI is a `file:` URI whose path passes
|
|
266
|
+
the graph's `WorkspaceBoundary`. Locations outside the project, in excluded files
|
|
267
|
+
(credential files, generated directories such as `node_modules`, and
|
|
268
|
+
`excludedPaths`), in `.agenthead`, or with malformed ranges are dropped. Kept paths
|
|
269
|
+
are canonical (symlinks followed), project-relative and slash separated, and
|
|
270
|
+
locations are deduplicated and sorted. An empty answer from a healthy server is
|
|
271
|
+
returned as it is. A position outside the project or in an excluded file throws
|
|
272
|
+
the boundary's error, as graph queries do.
|
|
273
|
+
|
|
274
|
+
**Fallback.** Otherwise the result is `{ source: "graph", reason, locations }`:
|
|
275
|
+
`graph.definitions(name)` or `graph.references(name)` for the name the graph
|
|
276
|
+
recorded at the position (`nameAt`), kept to files of the same language
|
|
277
|
+
(TypeScript and JavaScript, or Python), with the name matching described above.
|
|
278
|
+
A position with no recorded name gives no locations.
|
|
279
|
+
|
|
280
|
+
| `reason` | When | The server afterwards |
|
|
281
|
+
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
|
|
282
|
+
| `disabled` | The server for the file type is configured `false` | Not started |
|
|
283
|
+
| `unavailable` | No server for the file type, not found or failed to start; it does not offer the request or UTF-16 positions; the file is not UTF-8 | Not started or kept |
|
|
284
|
+
| `timeout` | It did not answer `initialize` within `startupTimeoutMs` (default 30,000) | Stopped |
|
|
285
|
+
| `timeout` | It did not answer the query within `timeoutMs` (default 30,000) | Kept; stopped if it has hung (see below) |
|
|
286
|
+
| `crashed` | It exited, closed its streams or sent a malformed or oversized message | Stopped |
|
|
287
|
+
| `error` | It answered the query with an error | Kept |
|
|
288
|
+
|
|
289
|
+
A query that misses `timeoutMs` is cancelled with `$/cancelRequest` and answered
|
|
290
|
+
from the graph, and the server keeps working. The protocol requires an answer
|
|
291
|
+
even to a cancelled request, so a server that sends none within another
|
|
292
|
+
`timeoutMs` has hung and is stopped. A slow server is never stopped for being
|
|
293
|
+
slow: the first TypeScript request waits until tsserver has loaded the project,
|
|
294
|
+
which can take many seconds on a large one, and later queries get answers once
|
|
295
|
+
it has. A stopped server is not restarted: later queries for its files use the
|
|
296
|
+
graph. Create a new bridge to try again. Stopping kills the server's process
|
|
297
|
+
group, so helpers such as tsserver stop with it. A pending or cancelled request
|
|
298
|
+
keeps Node running until it is answered or its server is stopped. An idle
|
|
299
|
+
bridge does not keep Node running; if Node
|
|
300
|
+
exits without `close()`, the servers read end of input and exit. `close()` sends
|
|
301
|
+
`shutdown` and `exit`, waits within `timeoutMs` for each step, then kills the
|
|
302
|
+
group; later queries throw.
|
|
303
|
+
|
|
304
|
+
**Trust.** A language server is a separate program that runs with your
|
|
305
|
+
permissions. It reads the project itself, beyond what the boundary lets AgentHead
|
|
306
|
+
read and return, and it may run code from the project. typescript-language-server
|
|
307
|
+
prefers the project's own `node_modules/typescript`, whose tsserver loads the
|
|
308
|
+
plugins `tsconfig.json` names from that `node_modules`. Pyright runs `python3` or
|
|
309
|
+
`python` from `PATH`, and a server script starting `#!/usr/bin/env node` runs the
|
|
310
|
+
first `node` on `PATH`; either can be the project's if `PATH` has an absolute
|
|
311
|
+
entry inside the project, such as an activated virtual environment or
|
|
312
|
+
`node_modules/.bin` under `npm run`. Use the bridge only on projects you trust. On Windows, which CI does not test, npm's
|
|
313
|
+
`.cmd` shims cannot start without a shell: configure `node` as the command with
|
|
314
|
+
the server's script as an argument. There, only the server process itself is
|
|
315
|
+
stopped.
|
|
316
|
+
|
|
317
|
+
## Dependencies and licences
|
|
318
|
+
|
|
319
|
+
The runtime and all four grammar artifacts come from the exact pinned
|
|
320
|
+
[`@vscode/tree-sitter-wasm@0.3.1`](https://github.com/microsoft/vscode-tree-sitter-wasm/tree/v0.3.1)
|
|
321
|
+
distribution, which builds the web-tree-sitter bindings and bundles precompiled
|
|
322
|
+
WASM. Pinning the distribution pins the shipped runtime/grammar bytes together;
|
|
323
|
+
there are no install-time grammar downloads or native builds. A small internal
|
|
324
|
+
TypeScript interface describes the CommonJS runtime API because the distribution's
|
|
325
|
+
upstream types are not consistently shipped.
|
|
326
|
+
|
|
327
|
+
The installed distribution declares MIT and supplies a Microsoft MIT notice.
|
|
328
|
+
Its `cgmanifest.json` identifies the Tree-sitter runtime as `0.25.1` at commit
|
|
329
|
+
`fc15f621334a262039ffaded5937e2844f88da61`. Grammar dependencies in its
|
|
330
|
+
`package.json` are source ranges (`^0.23.2` for TypeScript/TSX and `^0.25.0`
|
|
331
|
+
for JavaScript and Python), not exact source revisions.
|
|
332
|
+
|
|
333
|
+
The [package notices](THIRD_PARTY_NOTICES.md) record the distribution's licence
|
|
334
|
+
evidence, every shipped WASM artifact's SHA-256, which four grammars AgentHead loads,
|
|
335
|
+
and the retain decision for the unused grammar payload. They also reproduce
|
|
336
|
+
all supplied runtime dependency legal texts and SQLite's public-domain statement.
|
|
337
|
+
The [root audit](../../THIRD_PARTY_NOTICES.md) covers the other workspaces,
|
|
338
|
+
fixtures and reference solutions.
|
|
339
|
+
|
|
340
|
+
## Verification
|
|
341
|
+
|
|
342
|
+
`test-fixtures/repo` has seven small source files spanning all four grammars.
|
|
343
|
+
`test-fixtures/expected.json` is the hand-checked inventory of definitions, aliases,
|
|
344
|
+
resolved imports, call sites and reference locations. The fixture README explains
|
|
345
|
+
its expected relationships. Tests compare every entry and check end ranges, reopen
|
|
346
|
+
persistence, 100% unchanged reuse (exceeding 95%), single-file changes, deletions,
|
|
347
|
+
relinking, exclusions, symlinks, version invalidation, schema rollback and failures.
|
|
348
|
+
There are no timing assertions.
|
|
349
|
+
|
|
350
|
+
`src/lsp-bridge.test.ts` runs `test-fixtures/fake-lsp-server.mjs`, a scripted
|
|
351
|
+
server that speaks the protocol, splits its frames and sends its own requests and
|
|
352
|
+
notifications. It covers answers, the files opened for references, document sync
|
|
353
|
+
across files, change notifications after indexing, one server per language, the
|
|
354
|
+
fallback when no server is available
|
|
355
|
+
or it cannot serve, `PATH` lookup and the server's `PATH`, a missed start-up
|
|
356
|
+
deadline, a slow request it cancels while keeping the server, a server that
|
|
357
|
+
leaves a cancelled request unanswered (stopped with its child process), each
|
|
358
|
+
bridge's temporary directory, crashes, malformed and
|
|
359
|
+
oversized messages, error answers, dropped locations outside the project, in
|
|
360
|
+
excluded files or malformed, Node exiting with the bridge left open, and the
|
|
361
|
+
entry point. When `typescript-language-server` and `pyright-langserver` are both
|
|
362
|
+
on `PATH`, which CI does not arrange, one more test asks the real servers
|
|
363
|
+
cross-file questions, references first, and again after edits.
|
|
364
|
+
|
|
365
|
+
From the monorepo root, CI runs the same checks:
|
|
366
|
+
|
|
367
|
+
```sh
|
|
368
|
+
npm run lint
|
|
369
|
+
npm run format:check
|
|
370
|
+
npm run typecheck
|
|
371
|
+
npm test
|
|
372
|
+
npm run build
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
The root project references include this package; the shared typecheck and test
|
|
376
|
+
patterns include its source and tests. To run just graph tests after building:
|
|
377
|
+
`npx vitest run packages/graph/src`.
|