@namzu/lsp 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/CHANGELOG.md ADDED
@@ -0,0 +1,49 @@
1
+ # @namzu/lsp
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9914794: New optional package `@namzu/lsp`: language-server-backed code navigation, so an agent asked for the call sites of a function gets symbol resolution rather than regex matches.
8
+
9
+ The whole navigation surface a namzu agent had was `grep` and `glob`. Ask for every call site of `computeTotal` and you get the comment that mentions it, the string literal that names it, and the unrelated same-named function in another scope — and you **miss** the call site that arrives through a re-export or a destructure, which is exactly the one a rename has to get right.
10
+
11
+ `StdioCodeNavigationProvider` drives one language server over its stdin and stdout: `Content-Length` framing (not JSON lines — a response carrying source text contains newlines), the `initialize`/`shutdown` handshake, request correlation, `textDocument/definition` and `textDocument/references`.
12
+
13
+ **Three answers, not two.** `unsupported` means the server does not implement the operation, so a caller can fall back to `grep` and _say_ the answer is textual; `failed` means something broke and the answer is unknown. Neither is `{ kind: 'locations', locations: [] }`, which means "I looked, and there are none" — the answer a deletion depends on. A provider that answered a missing binary with an empty list would tell an agent a symbol has no callers, and the agent would delete it. So a server that never completes `initialize` produces `failed` naming the binary, within a bounded startup timeout, and the failure is remembered rather than respawning a process per call.
14
+
15
+ In `@namzu/sdk`: an `lsp` builtin, `CodeNavigationProvider` on `ToolContext` the way `sandbox` already arrives, and `getCodeNavigationTools(provider)` which returns **an empty array when there is no provider**. The tool is not registered at all in a run that cannot use it — one that is always present and always answers "unavailable" costs a decision on every turn to say nothing, and teaches a model a capability exists when it does not.
16
+
17
+ Every path is contained through `resolveWithinReal` before it reaches the server, the same containment `read` and `grep` use. A language server indexes a workspace and will answer about anything it is handed; the boundary is the tool's job.
18
+
19
+ `dispose()` sends the shutdown handshake before killing, so a server holding a lock file or mid-write on an index gets to finish, and falls back to `SIGKILL` on a bounded timeout so one that ignores `exit` cannot keep the run alive.
20
+
21
+ - 655cc9d: Code navigation gains `hover` and `symbols`, and routes by file extension so a repository can have more than one language.
22
+
23
+ **`symbols` is the entry point, and its absence made the rest unreachable.** `definition` and `references` both need a line and a character, and an agent starting from a name has neither — so every navigation began with a grep, which is the text path this package exists to replace, reintroduced as a prerequisite. `symbols(query, scope?)` finds a declaration by name with no position at all.
24
+
25
+ `hover(file, line, character)` gives a symbol's resolved type and documentation without opening the file. Its `contents` may be **empty**, and that is a real answer: hovering over whitespace or a comment resolves to nothing, and a caller has to be able to tell that from a server that broke.
26
+
27
+ **Capabilities are READ from the initialize result, never probed.** A server with a workspace index answers `workspace/symbol`; one with only document symbols falls back to `textDocument/documentSymbol`; one declaring neither returns `{ kind: 'unsupported' }` naming both missing capabilities. Sending the request and interpreting whatever error comes back works until a server answers an error for a transient reason, and the fallback then fires for a capability the server has. The `documentSymbol` reply is a tree and is walked — a reader that took only the top level would miss every method, which is most of what a name search is for.
28
+
29
+ **`RoutingCodeNavigationProvider` maps extension to server**, starting one lazily per language on first use and reusing it. A file whose extension maps to nothing gets `{ kind: 'unsupported' }` naming the extension — not a default server, which would send the file to something that cannot read it and answer nothing, which reads as a symbol with no references. A `symbols` call with no scope asks every configured language, and reports `unsupported` rather than an empty list when every server refused, because "nobody looked" is not "the name does not exist".
30
+
31
+ The `lsp` builtin's input is a discriminated union: position is **required** for `definition`/`references`/`hover` and **absent** for `symbols`. Making it unconditionally optional lets a `definition` with no line silently resolve the top of the file; making it unconditionally required forces a `symbols` call to invent two numbers.
32
+
33
+ ## 0.2.0
34
+
35
+ ### Minor Changes
36
+
37
+ - 9914794: New optional package `@namzu/lsp`: language-server-backed code navigation, so an agent asked for the call sites of a function gets symbol resolution rather than regex matches.
38
+
39
+ The whole navigation surface a namzu agent had was `grep` and `glob`. Ask for every call site of `computeTotal` and you get the comment that mentions it, the string literal that names it, and the unrelated same-named function in another scope — and you **miss** the call site that arrives through a re-export or a destructure, which is exactly the one a rename has to get right.
40
+
41
+ `StdioCodeNavigationProvider` drives one language server over its stdin and stdout: `Content-Length` framing (not JSON lines — a response carrying source text contains newlines), the `initialize`/`shutdown` handshake, request correlation, `textDocument/definition` and `textDocument/references`.
42
+
43
+ **Three answers, not two.** `unsupported` means the server does not implement the operation, so a caller can fall back to `grep` and _say_ the answer is textual; `failed` means something broke and the answer is unknown. Neither is `{ kind: 'locations', locations: [] }`, which means "I looked, and there are none" — the answer a deletion depends on. A provider that answered a missing binary with an empty list would tell an agent a symbol has no callers, and the agent would delete it. So a server that never completes `initialize` produces `failed` naming the binary, within a bounded startup timeout, and the failure is remembered rather than respawning a process per call.
44
+
45
+ In `@namzu/sdk`: an `lsp` builtin, `CodeNavigationProvider` on `ToolContext` the way `sandbox` already arrives, and `getCodeNavigationTools(provider)` which returns **an empty array when there is no provider**. The tool is not registered at all in a run that cannot use it — one that is always present and always answers "unavailable" costs a decision on every turn to say nothing, and teaches a model a capability exists when it does not.
46
+
47
+ Every path is contained through `resolveWithinReal` before it reaches the server, the same containment `read` and `grep` use. A language server indexes a workspace and will answer about anything it is handed; the boundary is the tool's job.
48
+
49
+ `dispose()` sends the shutdown handshake before killing, so a server holding a lock file or mid-write on an index gets to finish, and falls back to `SIGKILL` on a bounded timeout so one that ignores `exit` cannot keep the run alive.
package/LICENSE.md ADDED
@@ -0,0 +1,110 @@
1
+ # Functional Source License, Version 1.1, MIT Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-MIT
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 Cogitave
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the MIT license that is effective on the second anniversary of the date we make
91
+ the Software available. On or after that date, you may use the Software under
92
+ the MIT license, in which case the following will apply:
93
+
94
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
95
+ this software and associated documentation files (the "Software"), to deal in
96
+ the Software without restriction, including without limitation the rights to
97
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98
+ of the Software, and to permit persons to whom the Software is furnished to do
99
+ so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all
102
+ copies or substantial portions of the Software.
103
+
104
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
110
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,299 @@
1
+ <!-- okf
2
+ type: Reference
3
+ title: "@namzu/lsp"
4
+ description: >-
5
+ Language-server-backed code navigation for @namzu/sdk. Drives a real language
6
+ server over stdio to resolve a symbol by name, by declaration, by every use
7
+ and by hover, routing one server per language. Separate from the kernel so a
8
+ consumer who never navigates code spawns nothing.
9
+ tags: [readme, package, lsp, code-navigation]
10
+ timestamp: 2026-08-17T00:00:00Z
11
+ status: active
12
+ diataxis: reference
13
+ -->
14
+
15
+ <div align="center">
16
+
17
+ <h1>@namzu/lsp</h1>
18
+
19
+ **Symbol resolution for [`@namzu/sdk`](https://www.npmjs.com/package/@namzu/sdk), so an agent asked for call sites gets the ones `grep` cannot see.**
20
+
21
+ [![License: FSL-1.1-MIT](https://img.shields.io/badge/license-FSL--1.1--MIT-blue.svg)](https://github.com/cogitave/namzu/blob/main/LICENSE.md)
22
+ [![npm](https://img.shields.io/npm/v/@namzu/lsp.svg?label=%40namzu%2Flsp)](https://www.npmjs.com/package/@namzu/lsp)
23
+
24
+ [Install](#install) · [Wire it up](#wire-it-up) · [Four operations](#four-operations) · [Three answers](#three-answers-not-two) · [One server per language](#one-server-per-language) · [Options](#options)
25
+
26
+ </div>
27
+
28
+ ---
29
+
30
+ ## What this is
31
+
32
+ The whole code-navigation surface a namzu agent had was `grep` (regex over
33
+ file text) and `glob` (filename patterns). Ask for every call site of
34
+ `computeTotal` and you get the comment that mentions it, the string literal
35
+ that names it, and the unrelated same-named function in another scope — and
36
+ you **miss** the call site that arrives through a re-export or a destructure,
37
+ which is exactly the one a rename has to get right.
38
+
39
+ This package closes that. It drives a real language server over its stdin and
40
+ stdout — `Content-Length` framing rather than JSON lines, since a response
41
+ carrying source text contains newlines; the `initialize`/`shutdown` handshake;
42
+ request correlation — and implements the `CodeNavigationProvider` seam the
43
+ kernel declares.
44
+
45
+ It is separate from the kernel because the kernel owns the shape a tool
46
+ programs against and this owns the process that answers. A consumer who never
47
+ navigates code carries no extra dependency and spawns no server: the `lsp`
48
+ tool is not registered at all in a run that has no provider.
49
+
50
+ ## Install
51
+
52
+ ```bash
53
+ pnpm add @namzu/sdk @namzu/lsp
54
+ ```
55
+
56
+ `@namzu/sdk` (>=1.0.0) is a peer dependency. This package has no runtime
57
+ dependencies of its own — the transport is `node:child_process` and the wire
58
+ is JSON — and it does not install a language server for you. You bring the
59
+ binary; this drives it.
60
+
61
+ ## Wire it up
62
+
63
+ ```ts
64
+ import { RoutingCodeNavigationProvider } from '@namzu/lsp'
65
+ import { getCodeNavigationTools, ToolRegistry } from '@namzu/sdk'
66
+
67
+ const rootDir = process.cwd()
68
+
69
+ const codeNavigation = new RoutingCodeNavigationProvider({
70
+ routes: [
71
+ { extensions: ['.ts', '.tsx'], server: { command: 'ts-server', args: ['--stdio'], rootDir } },
72
+ { extensions: ['.py'], server: { command: 'py-server', rootDir } },
73
+ ],
74
+ })
75
+
76
+ const registry = new ToolRegistry()
77
+ for (const tool of getCodeNavigationTools(codeNavigation)) registry.register(tool)
78
+
79
+ // …and when the run is over:
80
+ await codeNavigation.dispose()
81
+ ```
82
+
83
+ `getCodeNavigationTools(undefined)` returns `[]`, so the `lsp` tool is **not
84
+ registered at all** in a run that cannot use it. A tool that is always present
85
+ and always answers "unavailable" costs a decision on every turn to say
86
+ nothing, and it teaches a model that a capability exists when it does not.
87
+
88
+ Registration is only half the wiring. The tool reads its provider from
89
+ `ToolContext.codeNavigation`, and nothing in the run loop fills that field in
90
+ for you — `sandbox` reaches a run through the executor, and code navigation
91
+ has no equivalent seam. Set `codeNavigation` on the tool context the tool is
92
+ executed with. A tool that is registered but reaches a context without a
93
+ provider refuses by naming the missing piece rather than answering "no
94
+ results", but that refusal is a wiring bug report, not a mode to run in.
95
+
96
+ For a single-language workspace, skip the router:
97
+
98
+ ```ts
99
+ import { StdioCodeNavigationProvider } from '@namzu/lsp'
100
+
101
+ const codeNavigation = new StdioCodeNavigationProvider({
102
+ command: 'ts-server',
103
+ args: ['--stdio'],
104
+ rootDir: process.cwd(),
105
+ })
106
+ ```
107
+
108
+ ## Four operations
109
+
110
+ ```ts
111
+ import type { CodeNavigationProvider } from '@namzu/lsp'
112
+
113
+ declare const nav: CodeNavigationProvider
114
+
115
+ const byName = await nav.symbols('computeTotal') // no position needed
116
+ const declaredAt = await nav.definition('/repo/src/total.ts', 12, 6)
117
+ const usedAt = await nav.references('/repo/src/total.ts', 12, 6)
118
+ const typeOf = await nav.hover('/repo/src/total.ts', 12, 6)
119
+
120
+ await nav.dispose()
121
+ ```
122
+
123
+ | | |
124
+ |---|---|
125
+ | `symbols(query, scope?)` | Find a declaration **by name**, with no position. |
126
+ | `definition(file, line, character)` | Where the symbol under the position is declared. |
127
+ | `references(file, line, character)` | Everywhere it is used, **excluding** the declaration. |
128
+ | `hover(file, line, character)` | Its resolved type and documentation. |
129
+
130
+ **Start with `symbols`.** The other three need a line and a character, and an
131
+ agent starting from a name has neither — so without it every navigation begins
132
+ with a grep, which is the text path this package exists to replace,
133
+ reintroduced as its own prerequisite.
134
+
135
+ The declaration is left out of `references` deliberately: an agent asking who
136
+ calls this wants the call sites, and including the declaration inflates the
137
+ count by one and reads as a caller that does not exist.
138
+
139
+ Positions are zero-based, matching the wire. Paths come back the other way —
140
+ `file://` stripped and percent-decoding undone — so a caller gets something it
141
+ can hand straight to `readFile`.
142
+
143
+ ## Three answers, not two
144
+
145
+ ```ts
146
+ import type { SourceLocation, SymbolLocation } from '@namzu/lsp'
147
+
148
+ type CodeNavigationResult =
149
+ | { readonly kind: 'locations'; readonly locations: readonly SourceLocation[] }
150
+ | { readonly kind: 'unsupported'; readonly reason: string }
151
+ | { readonly kind: 'failed'; readonly error: string }
152
+
153
+ type SymbolSearchResult =
154
+ | { readonly kind: 'symbols'; readonly symbols: readonly SymbolLocation[] }
155
+ | { readonly kind: 'unsupported'; readonly reason: string }
156
+ | { readonly kind: 'failed'; readonly error: string }
157
+
158
+ type HoverResult =
159
+ | { readonly kind: 'hover'; readonly contents: string }
160
+ | { readonly kind: 'unsupported'; readonly reason: string }
161
+ | { readonly kind: 'failed'; readonly error: string }
162
+ ```
163
+
164
+ `unsupported` means nothing here can answer that question — the server does
165
+ not implement the method, does not declare the capability, or no server is
166
+ configured for the file's language. A caller can fall back to `grep` and *say*
167
+ the answer is textual. `failed` means something broke, and the answer is
168
+ unknown.
169
+
170
+ **Neither is `{ kind: 'locations', locations: [] }`.** An empty list means "I
171
+ looked, and there are none", which is a real answer and the one a deletion
172
+ depends on. A provider that answered a missing binary with an empty list would
173
+ tell an agent a symbol has no callers, and the agent would delete it. So a
174
+ server that never completes `initialize` produces `failed` naming the binary,
175
+ inside a bounded startup timeout — and the failure is *remembered*, so a run
176
+ that asks twenty times does not spawn twenty servers against a binary that is
177
+ not there.
178
+
179
+ `hover` carries the same distinction one level down: `contents` may be the
180
+ empty string, because hovering over whitespace or a comment resolves to
181
+ nothing, and that has to read differently from a server that broke.
182
+
183
+ `SymbolLocation` extends `SourceLocation` with the symbol's `name`, and with
184
+ `symbolKind` and `containerName` when the server supplied them.
185
+
186
+ ### Capabilities are read, not probed
187
+
188
+ What a server can do is taken from the `initialize` **result**, never
189
+ discovered by sending a request and interpreting the error. A server with a
190
+ workspace index answers `workspace/symbol`; one with only document symbols
191
+ falls back to `textDocument/documentSymbol`, which needs a `scope` to look in
192
+ and says so when it is missing; one declaring neither is `unsupported`, naming
193
+ both. `hover` is refused up front only when the server explicitly declares
194
+ `hoverProvider: false`.
195
+
196
+ Sending the request and swallowing the error works until a server answers an
197
+ error for a different reason — a transient one, a malformed query — and the
198
+ fallback then fires for a capability the server has. The handshake already
199
+ carries the answer.
200
+
201
+ Two consequences of that split are worth knowing before you read a result.
202
+ A server with a workspace index gets the query and decides what matches, so
203
+ `scope` chooses which server answers rather than narrowing what it searches;
204
+ the `documentSymbol` fallback is the path that filters, and it filters by
205
+ plain substring on the symbol name. And the `documentSymbol` reply is a
206
+ **tree**, which is walked — a reader that took only the top level would miss
207
+ every method, which is most of what somebody searching by name is looking for.
208
+
209
+ ## One server per language
210
+
211
+ `RoutingCodeNavigationProvider` maps a file extension to a server, starts one
212
+ lazily on first use, and reuses it. Starting every configured server up front
213
+ pays for languages a run never touches; starting one per request pays the
214
+ `initialize` handshake — seconds, for an indexing server — on every call.
215
+ `startedCount()` reports how many are running, which is how the reuse is
216
+ asserted: a per-request spawn is fast enough on a quick machine to pass a
217
+ timing assertion.
218
+
219
+ A file whose extension maps to nothing is `unsupported` **naming the
220
+ extension**. It is not routed to a default, because a default sends the file
221
+ to a server that cannot read it, which answers nothing, which reads as a
222
+ symbol with no references.
223
+
224
+ A `symbols` call with **no** scope asks every configured language and
225
+ concatenates what they found. If nothing was found anywhere, the reason
226
+ matters: a `failed` from any server wins, and when *every* server said
227
+ `unsupported` that is what comes back — "nobody looked" is not "the name does
228
+ not exist".
229
+
230
+ Extensions are lower-cased on the way in, so a `.TS` on a case-insensitive
231
+ filesystem is not reported as an unconfigured language.
232
+
233
+ ## Options
234
+
235
+ ```ts
236
+ import type {
237
+ CodeNavigationRoute,
238
+ RoutingCodeNavigationOptions,
239
+ StdioCodeNavigationOptions,
240
+ } from '@namzu/lsp'
241
+ ```
242
+
243
+ `StdioCodeNavigationOptions` — one server:
244
+
245
+ | Option | Default | Notes |
246
+ |---|---|---|
247
+ | `command` | — | required; the server binary |
248
+ | `args` | none | argv for it, e.g. `['--stdio']` |
249
+ | `rootDir` | — | required; the child's working directory, and the `rootUri` and workspace folder sent in `initialize` |
250
+ | `startupTimeoutMs` | `15_000` | how long `initialize` may take before the provider reports `failed` |
251
+ | `requestTimeoutMs` | `20_000` | how long any one request may take |
252
+ | `env` | inherited | merged **over** the parent environment; omit it and the child inherits unchanged |
253
+
254
+ `RoutingCodeNavigationOptions` — one server per language:
255
+
256
+ | Option | Default | Notes |
257
+ |---|---|---|
258
+ | `routes` | — | required; each a `CodeNavigationRoute` of `extensions` (with the dot) and one `server` |
259
+ | `createProvider` | `new StdioCodeNavigationProvider(…)` | how a route becomes a provider; injectable so a test can count spawns |
260
+
261
+ ## Containment is the tool's job
262
+
263
+ Every path the `lsp` builtin is given is resolved inside the run's working
264
+ directory *before* it reaches the server, through the same containment helper
265
+ `read` and `edit` use. A language server indexes a workspace and will happily
266
+ answer about `../../etc/passwd` if asked; the boundary belongs to the tool,
267
+ not to the process it drives. Calling a provider directly bypasses that, so a host
268
+ that does its own dispatch owns the check.
269
+
270
+ ## Disposal
271
+
272
+ `dispose()` sends the `shutdown`/`exit` handshake **before** killing, so a
273
+ server holding a lock file or mid-write on an index gets to finish, and falls
274
+ back to `SIGKILL` on a bounded timeout so one that ignores `exit` cannot keep
275
+ the run alive. Every in-flight request is rejected rather than left hanging,
276
+ and the provider refuses further work instead of quietly respawning.
277
+
278
+ `RoutingCodeNavigationProvider.dispose()` disposes every server it started.
279
+
280
+ ## Status
281
+
282
+ `0.x`. The two provider classes and the result unions are what the kernel's
283
+ `lsp` builtin programs against, and the surface can still move before 1.0.
284
+ `CodeNavigationProvider`, `SourceLocation`, `SymbolLocation` and the three
285
+ result unions are re-exported here from `@namzu/sdk` rather than re-declared —
286
+ the dependency direction is `sdk ← lsp`, so the kernel owns the shape and this
287
+ package implements it, and a second declaration would be a copy that can
288
+ drift.
289
+
290
+ ## Further reading
291
+
292
+ - [`docs/sdk/tools/code-navigation.md`](../../docs/sdk/tools/code-navigation.md)
293
+ — why the result union has three members, why the tool is absent without a
294
+ provider, and how routing decides which server answers.
295
+
296
+ ## License
297
+
298
+ FSL-1.1-MIT, converting to MIT two years after each release. Same as
299
+ `@namzu/sdk`.
@@ -0,0 +1,6 @@
1
+ export { StdioCodeNavigationProvider } from './stdio.js';
2
+ export type { StdioCodeNavigationOptions } from './stdio.js';
3
+ export { RoutingCodeNavigationProvider } from './routing.js';
4
+ export type { CodeNavigationRoute, RoutingCodeNavigationOptions } from './routing.js';
5
+ export type { CodeNavigationProvider, CodeNavigationResult, HoverResult, SourceLocation, SymbolLocation, SymbolSearchResult, } from './types.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAA;AACxD,YAAY,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAA;AAC5D,OAAO,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAA;AAC5D,YAAY,EAAE,mBAAmB,EAAE,4BAA4B,EAAE,MAAM,cAAc,CAAA;AACrF,YAAY,EACX,sBAAsB,EACtB,oBAAoB,EACpB,WAAW,EACX,cAAc,EACd,cAAc,EACd,kBAAkB,GAClB,MAAM,YAAY,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { StdioCodeNavigationProvider } from './stdio.js';
2
+ export { RoutingCodeNavigationProvider } from './routing.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAA;AAExD,OAAO,EAAE,6BAA6B,EAAE,MAAM,cAAc,CAAA"}
@@ -0,0 +1,60 @@
1
+ import { type StdioCodeNavigationOptions } from './stdio.js';
2
+ import type { CodeNavigationProvider, CodeNavigationResult, HoverResult, SymbolSearchResult } from './types.js';
3
+ /**
4
+ * One server per language, started when its first file is asked about.
5
+ *
6
+ * A single hard-wired server means the capability exists for one language in
7
+ * a repository that will not stay one language — and the failure is quiet:
8
+ * a `.py` file reaches a server that has never parsed Python, which answers
9
+ * nothing, which reads as a symbol with no references.
10
+ *
11
+ * **A file whose extension maps to nothing is `unsupported`, naming the
12
+ * extension.** Not routed to a default, and not an empty result. A default
13
+ * would send the file to a server that cannot read it and produce exactly
14
+ * the quiet wrong answer above; an empty result would say the symbol has no
15
+ * callers.
16
+ *
17
+ * **Lazy, and reused.** Starting every configured server up front pays for
18
+ * languages a run never touches, and starting one per request would pay the
19
+ * initialize handshake — seconds, for an indexing server — on every call.
20
+ */
21
+ export interface CodeNavigationRoute {
22
+ /** Extensions this server answers for, with the dot: `['.ts', '.tsx']`. */
23
+ readonly extensions: readonly string[];
24
+ readonly server: StdioCodeNavigationOptions;
25
+ }
26
+ export interface RoutingCodeNavigationOptions {
27
+ readonly routes: readonly CodeNavigationRoute[];
28
+ /**
29
+ * How a route becomes a provider.
30
+ *
31
+ * Injectable so a test can count spawns — the reuse property is about
32
+ * how many processes exist, and asserting it on wall-clock time would
33
+ * pass on a fast machine with the reuse removed.
34
+ */
35
+ readonly createProvider?: (options: StdioCodeNavigationOptions) => CodeNavigationProvider;
36
+ }
37
+ export declare class RoutingCodeNavigationProvider implements CodeNavigationProvider {
38
+ private readonly byExtension;
39
+ private readonly started;
40
+ private readonly create;
41
+ constructor(options: RoutingCodeNavigationOptions);
42
+ definition(file: string, line: number, character: number): Promise<CodeNavigationResult>;
43
+ references(file: string, line: number, character: number): Promise<CodeNavigationResult>;
44
+ hover(file: string, line: number, character: number): Promise<HoverResult>;
45
+ /**
46
+ * Search by name, across one language or all of them.
47
+ *
48
+ * With a scope, the scope's extension picks the server — the ordinary
49
+ * case, and the cheap one. Without, every configured language answers,
50
+ * because "find this symbol" in a mixed repository means all of it and a
51
+ * caller who wanted one language would have said which.
52
+ */
53
+ symbols(query: string, scope?: string): Promise<SymbolSearchResult>;
54
+ dispose(): Promise<void>;
55
+ /** How many servers are running. For a test asserting reuse. */
56
+ startedCount(): number;
57
+ private routeFor;
58
+ private providerFor;
59
+ }
60
+ //# sourceMappingURL=routing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing.d.ts","sourceRoot":"","sources":["../src/routing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,0BAA0B,EAA+B,MAAM,YAAY,CAAA;AACzF,OAAO,KAAK,EACX,sBAAsB,EACtB,oBAAoB,EACpB,WAAW,EACX,kBAAkB,EAClB,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,WAAW,mBAAmB;IACnC,2EAA2E;IAC3E,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAA;IACtC,QAAQ,CAAC,MAAM,EAAE,0BAA0B,CAAA;CAC3C;AAED,MAAM,WAAW,4BAA4B;IAC5C,QAAQ,CAAC,MAAM,EAAE,SAAS,mBAAmB,EAAE,CAAA;IAC/C;;;;;;OAMG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,0BAA0B,KAAK,sBAAsB,CAAA;CACzF;AAED,qBAAa,6BAA8B,YAAW,sBAAsB;IAC3E,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAyC;IACrE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAyD;IACjF,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiE;gBAE5E,OAAO,EAAE,4BAA4B;IAY3C,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAMxF,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAMxF,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAMhF;;;;;;;OAOG;IACG,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IA4BnE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAK9B,gEAAgE;IAChE,YAAY,IAAI,MAAM;IAItB,OAAO,CAAC,QAAQ;IAWhB,OAAO,CAAC,WAAW;CAOnB"}
@@ -0,0 +1,97 @@
1
+ import { extname } from 'node:path';
2
+ import { StdioCodeNavigationProvider } from './stdio.js';
3
+ export class RoutingCodeNavigationProvider {
4
+ byExtension = new Map();
5
+ started = new Map();
6
+ create;
7
+ constructor(options) {
8
+ this.create = options.createProvider ?? ((server) => new StdioCodeNavigationProvider(server));
9
+ for (const route of options.routes) {
10
+ for (const extension of route.extensions) {
11
+ // Lower-cased once, here. A `.TS` on a case-insensitive filesystem
12
+ // is the same language, and a lookup that missed it would report
13
+ // the extension as unconfigured.
14
+ this.byExtension.set(extension.toLowerCase(), route);
15
+ }
16
+ }
17
+ }
18
+ async definition(file, line, character) {
19
+ const routed = this.routeFor(file);
20
+ if ('reason' in routed)
21
+ return { kind: 'unsupported', reason: routed.reason };
22
+ return await routed.provider.definition(file, line, character);
23
+ }
24
+ async references(file, line, character) {
25
+ const routed = this.routeFor(file);
26
+ if ('reason' in routed)
27
+ return { kind: 'unsupported', reason: routed.reason };
28
+ return await routed.provider.references(file, line, character);
29
+ }
30
+ async hover(file, line, character) {
31
+ const routed = this.routeFor(file);
32
+ if ('reason' in routed)
33
+ return { kind: 'unsupported', reason: routed.reason };
34
+ return await routed.provider.hover(file, line, character);
35
+ }
36
+ /**
37
+ * Search by name, across one language or all of them.
38
+ *
39
+ * With a scope, the scope's extension picks the server — the ordinary
40
+ * case, and the cheap one. Without, every configured language answers,
41
+ * because "find this symbol" in a mixed repository means all of it and a
42
+ * caller who wanted one language would have said which.
43
+ */
44
+ async symbols(query, scope) {
45
+ if (scope) {
46
+ const routed = this.routeFor(scope);
47
+ if ('reason' in routed)
48
+ return { kind: 'unsupported', reason: routed.reason };
49
+ return await routed.provider.symbols(query, scope);
50
+ }
51
+ const routes = [...new Set(this.byExtension.values())];
52
+ if (routes.length === 0) {
53
+ return { kind: 'unsupported', reason: 'No language servers are configured.' };
54
+ }
55
+ const answers = await Promise.all(routes.map(async (route) => await this.providerFor(route).symbols(query)));
56
+ const found = answers.flatMap((a) => (a.kind === 'symbols' ? a.symbols : []));
57
+ if (found.length > 0)
58
+ return { kind: 'symbols', symbols: found };
59
+ // Nothing found anywhere. If EVERY server refused, that is not an empty
60
+ // result — nobody looked. Reporting it as `symbols: []` would tell the
61
+ // caller the name does not exist in the repository.
62
+ const failure = answers.find((a) => a.kind === 'failed');
63
+ if (failure)
64
+ return failure;
65
+ const unsupported = answers.find((a) => a.kind === 'unsupported');
66
+ if (unsupported && answers.every((a) => a.kind === 'unsupported'))
67
+ return unsupported;
68
+ return { kind: 'symbols', symbols: [] };
69
+ }
70
+ async dispose() {
71
+ await Promise.all([...this.started.values()].map(async (p) => await p.dispose()));
72
+ this.started.clear();
73
+ }
74
+ /** How many servers are running. For a test asserting reuse. */
75
+ startedCount() {
76
+ return this.started.size;
77
+ }
78
+ routeFor(file) {
79
+ const extension = extname(file).toLowerCase();
80
+ const route = this.byExtension.get(extension);
81
+ if (!route) {
82
+ return {
83
+ reason: `No language server is configured for "${extension || file}". Configure one, or use \`grep\` and treat the result as textual.`,
84
+ };
85
+ }
86
+ return { provider: this.providerFor(route) };
87
+ }
88
+ providerFor(route) {
89
+ const existing = this.started.get(route);
90
+ if (existing)
91
+ return existing;
92
+ const created = this.create(route.server);
93
+ this.started.set(route, created);
94
+ return created;
95
+ }
96
+ }
97
+ //# sourceMappingURL=routing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routing.js","sourceRoot":"","sources":["../src/routing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC,OAAO,EAAmC,2BAA2B,EAAE,MAAM,YAAY,CAAA;AA6CzF,MAAM,OAAO,6BAA6B;IACxB,WAAW,GAAG,IAAI,GAAG,EAA+B,CAAA;IACpD,OAAO,GAAG,IAAI,GAAG,EAA+C,CAAA;IAChE,MAAM,CAAiE;IAExF,YAAY,OAAqC;QAChD,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,cAAc,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAA;QAC7F,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACpC,KAAK,MAAM,SAAS,IAAI,KAAK,CAAC,UAAU,EAAE,CAAC;gBAC1C,mEAAmE;gBACnE,iEAAiE;gBACjE,iCAAiC;gBACjC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,EAAE,KAAK,CAAC,CAAA;YACrD,CAAC;QACF,CAAC;IACF,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,IAAY,EAAE,IAAY,EAAE,SAAiB;QAC7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