@pugi/cli 0.1.0-beta.7 → 0.1.0-beta.9

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.
@@ -0,0 +1,495 @@
1
+ /**
2
+ * apply_patch tool — α7.7 Phase 1.
3
+ *
4
+ * Accepts a unified diff (the format produced by `git diff` and
5
+ * consumed by `git apply`) and lands it atomically into the workspace.
6
+ * This is the third edit primitive alongside the α6.6 4-layer diff
7
+ * escalation: where the layers escalate from minimal `oldString`/
8
+ * `newString` blocks up to full-file rewrites, apply_patch covers the
9
+ * unified-diff dialect that OpenAI Codex and most external tools emit.
10
+ *
11
+ * Why we have both:
12
+ *
13
+ * - The 4-layer escalation maximises model-side success rate on
14
+ * conversational edits (Claude / Gemini / OpenAI all have a
15
+ * preferred dialect that maps onto one of the layers).
16
+ * - apply_patch is the "external tools speak this" path. A model
17
+ * emits a single unified diff (the format `git diff` produces),
18
+ * and we run it through `git apply` with the same security gate
19
+ * the layers use.
20
+ *
21
+ * Security: every file mentioned in the patch goes through the same
22
+ * `applySecurityGate` chokepoint as the layers (see
23
+ * `src/core/edits/security-gate.ts`). A patch that touches
24
+ * `../../etc/passwd`, `.env`, or a workspace-local symlink to a protected
25
+ * file is rejected BEFORE `git apply` runs. Symlink escape, protected
26
+ * file, and path traversal are all covered by the same gate the layers
27
+ * inherit — we never roll our own resolver here.
28
+ *
29
+ * Atomicity: a multi-file patch either lands entirely or not at all.
30
+ * `git apply --check` validates the patch end-to-end against the
31
+ * working tree first; only on a clean check do we run the real apply.
32
+ * If the apply still fails partway (extremely rare — usually a race
33
+ * with another writer), we run `git checkout -- <each file>` to roll
34
+ * the tree back. This keeps the dispatcher's invariant: a tool result
35
+ * of `ok: false` means the workspace is unchanged.
36
+ *
37
+ * Idempotency: applying the same patch twice rejects the second with
38
+ * `already_applied`. `git apply` itself returns success only when the
39
+ * patch's pre-image matches the working tree, so a second invocation
40
+ * naturally fails. We translate the specific failure mode into a
41
+ * dedicated reason so callers can short-circuit retry loops.
42
+ *
43
+ * Brand voice: ASCII only, no emoji, no banned words.
44
+ */
45
+ import { spawnSync } from 'node:child_process';
46
+ import { existsSync, rmSync } from 'node:fs';
47
+ import { resolve, sep } from 'node:path';
48
+ import { applySecurityGate } from '../core/edits/security-gate.js';
49
+ import { gateOnCancellation, OperatorAbortedError } from './file-tools.js';
50
+ import { recordToolCall, recordToolResult, recordFileMutation } from '../core/session.js';
51
+ /**
52
+ * Parse the file paths referenced in a unified diff. We look for both
53
+ * `diff --git a/X b/Y` headers (preferred) and the fallback
54
+ * `+++ b/<path>` lines that plain `diff -u` emits. The full set of
55
+ * touched paths feeds the security gate — EVERY file goes through
56
+ * `applySecurityGate` before we trust `git apply` to do anything.
57
+ *
58
+ * Security (R1 fix 2026-05-26, PR #413 r1): git emits C-style quoted
59
+ * path headers when a path contains "unusual" bytes (high bits, control
60
+ * chars, double-quote, backslash) and `core.quotePath` is true (the
61
+ * default). The literal header looks like
62
+ * `diff --git "a/.env" "b/.env"`. Before this fix the regex captured
63
+ * the literal `"b/.env"` string and the security gate's basename match
64
+ * never saw `.env` — `basename('"b/.env"')` is `'.env"'` (note the
65
+ * trailing quote) which does NOT match the `.env` protected pattern.
66
+ * `git apply` then de-quoted the header and happily landed on the real
67
+ * `.env`. We strip the surrounding quotes + decode the C-style escapes
68
+ * via `unquoteGitPath` BEFORE passing to the security gate so the
69
+ * basename matcher sees the real target.
70
+ */
71
+ export function extractPatchPaths(patch) {
72
+ const paths = new Set();
73
+ for (const line of patch.split('\n')) {
74
+ if (line.startsWith('diff --git ')) {
75
+ // `diff --git a/foo b/bar` — paths can contain spaces only when
76
+ // quoted by git's own diff machinery (rare). The robust extractor
77
+ // matches the `b/...` half because rename diffs carry the new
78
+ // name there.
79
+ // Two variants: unquoted (`a/foo b/bar`) and C-style quoted
80
+ // (`"a/foo" "b/bar"`). We try the quoted form first because the
81
+ // unquoted regex below would accept the literal quote as part of
82
+ // the path otherwise.
83
+ const quoted = line.match(/^diff --git "a\/(.+)" "b\/(.+)"$/);
84
+ if (quoted) {
85
+ if (quoted[1])
86
+ paths.add(unquoteGitPath(quoted[1]));
87
+ if (quoted[2])
88
+ paths.add(unquoteGitPath(quoted[2]));
89
+ continue;
90
+ }
91
+ const match = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
92
+ if (match) {
93
+ if (match[1])
94
+ paths.add(unquoteGitPath(match[1]));
95
+ if (match[2])
96
+ paths.add(unquoteGitPath(match[2]));
97
+ }
98
+ continue;
99
+ }
100
+ if (line.startsWith('+++ ')) {
101
+ const after = line.slice(4).trim();
102
+ if (after === '/dev/null')
103
+ continue;
104
+ const stripped = stripQuotedHalf(after, 'b/');
105
+ if (stripped)
106
+ paths.add(stripTimestampSuffix(stripped));
107
+ continue;
108
+ }
109
+ if (line.startsWith('--- ')) {
110
+ const after = line.slice(4).trim();
111
+ if (after === '/dev/null')
112
+ continue;
113
+ const stripped = stripQuotedHalf(after, 'a/');
114
+ if (stripped)
115
+ paths.add(stripTimestampSuffix(stripped));
116
+ }
117
+ }
118
+ return Array.from(paths);
119
+ }
120
+ /**
121
+ * Strip the leading `a/` or `b/` prefix from a `---` / `+++` line,
122
+ * handling both unquoted (`b/.env`) and C-style quoted (`"b/.env"`)
123
+ * variants. The returned path is fully de-quoted so the security gate
124
+ * sees the real basename. Returns null when the line does not parse.
125
+ */
126
+ function stripQuotedHalf(after, prefix) {
127
+ // Quoted form: `"b/path with \"escapes\""`. Detect surrounding quotes
128
+ // first, strip them, then peel the prefix, then unquote the inner
129
+ // C-style escapes.
130
+ if (after.startsWith('"') && after.endsWith('"') && after.length >= 2) {
131
+ const inner = after.slice(1, -1);
132
+ const peeled = inner.startsWith(prefix) ? inner.slice(prefix.length) : inner;
133
+ return unquoteGitPath(peeled);
134
+ }
135
+ const trimmed = after.startsWith(prefix) ? after.slice(prefix.length) : after;
136
+ return trimmed;
137
+ }
138
+ /**
139
+ * Decode git's C-style path quoting. When `core.quotePath` is true
140
+ * (default) git writes paths with high-bit / control / quote bytes as
141
+ * C-string escapes inside double quotes:
142
+ *
143
+ * `"\.env"` -> `.env` (backslash before . is just a literal)
144
+ * `"a\"b"` -> `a"b` (escaped double-quote)
145
+ * `"a\\b"` -> `a\b` (escaped backslash)
146
+ * `"a\tb"` -> `a` + TAB + `b`
147
+ * `"a\341\210\264"` -> `a` + UTF-8 bytes 0xe1 0x88 0xb4
148
+ *
149
+ * Accepts a path that is EITHER already unquoted (passed through) OR an
150
+ * inner string previously stripped of its surrounding quotes. The
151
+ * function is idempotent on already-clean ASCII paths.
152
+ *
153
+ * Reference: git source `quote.c::unquote_c_style`.
154
+ */
155
+ export function unquoteGitPath(s) {
156
+ // If the caller passed us a wrapped string (`"foo"`), peel it now.
157
+ if (s.startsWith('"') && s.endsWith('"') && s.length >= 2) {
158
+ s = s.slice(1, -1);
159
+ }
160
+ // Fast path: no backslash means no C-style escapes, return as-is.
161
+ if (!s.includes('\\'))
162
+ return s;
163
+ const out = [];
164
+ for (let i = 0; i < s.length; i += 1) {
165
+ const ch = s[i];
166
+ if (ch !== '\\') {
167
+ // Single-byte ASCII or multi-byte JS string char; the byte we
168
+ // emit must match its UTF-8 encoding so the security gate sees
169
+ // the same bytes the filesystem will. JS strings are UTF-16; we
170
+ // bounce through Buffer to get the canonical UTF-8 bytes.
171
+ const bytes = Buffer.from(ch ?? '', 'utf8');
172
+ for (const b of bytes)
173
+ out.push(b);
174
+ continue;
175
+ }
176
+ const next = s[i + 1];
177
+ if (next === undefined) {
178
+ // Trailing backslash with no follower — emit literal.
179
+ out.push(0x5c);
180
+ continue;
181
+ }
182
+ // Three-digit octal escape: `\NNN` (each digit 0-7).
183
+ if (next >= '0' && next <= '7' && i + 3 < s.length + 1) {
184
+ const oct = s.slice(i + 1, i + 4);
185
+ if (/^[0-7]{3}$/.test(oct)) {
186
+ out.push(Number.parseInt(oct, 8));
187
+ i += 3;
188
+ continue;
189
+ }
190
+ }
191
+ switch (next) {
192
+ case 'a':
193
+ out.push(0x07);
194
+ break;
195
+ case 'b':
196
+ out.push(0x08);
197
+ break;
198
+ case 't':
199
+ out.push(0x09);
200
+ break;
201
+ case 'n':
202
+ out.push(0x0a);
203
+ break;
204
+ case 'v':
205
+ out.push(0x0b);
206
+ break;
207
+ case 'f':
208
+ out.push(0x0c);
209
+ break;
210
+ case 'r':
211
+ out.push(0x0d);
212
+ break;
213
+ case '"':
214
+ out.push(0x22);
215
+ break;
216
+ case '\\':
217
+ out.push(0x5c);
218
+ break;
219
+ default:
220
+ // Unknown escape — emit the escape char as a literal so we
221
+ // don't silently drop bytes. Mirrors git's own permissive
222
+ // behaviour.
223
+ out.push(next.charCodeAt(0));
224
+ }
225
+ i += 1;
226
+ }
227
+ return Buffer.from(out).toString('utf8');
228
+ }
229
+ /**
230
+ * `diff -u` (non-git) emits trailing tab-prefixed timestamps after the
231
+ * path: `--- foo.ts\t2026-05-25 10:00:00`. Strip those so the security
232
+ * gate sees the clean path.
233
+ */
234
+ function stripTimestampSuffix(path) {
235
+ const tab = path.indexOf('\t');
236
+ return tab >= 0 ? path.slice(0, tab) : path;
237
+ }
238
+ /**
239
+ * Apply a unified-diff patch to the workspace. Routes every mentioned
240
+ * file through the shared security gate before invoking `git apply`.
241
+ */
242
+ export function applyPatch(ctx, patch, opts = {}) {
243
+ const toolCallId = recordToolCall(ctx.session, 'apply_patch', `${patch.length} bytes`);
244
+ try {
245
+ gateOnCancellation(ctx, 'apply_patch');
246
+ }
247
+ catch (error) {
248
+ if (error instanceof OperatorAbortedError) {
249
+ recordToolResult(ctx.session, toolCallId, 'cancelled', error.message);
250
+ throw error;
251
+ }
252
+ throw error;
253
+ }
254
+ if (patch.trim().length === 0) {
255
+ const result = {
256
+ ok: false,
257
+ filesChanged: [],
258
+ reason: 'empty_patch',
259
+ detail: 'patch body is empty',
260
+ };
261
+ recordToolResult(ctx.session, toolCallId, 'error', 'empty_patch');
262
+ return result;
263
+ }
264
+ const paths = extractPatchPaths(patch);
265
+ if (paths.length === 0) {
266
+ const result = {
267
+ ok: false,
268
+ filesChanged: [],
269
+ reason: 'invalid_patch',
270
+ detail: 'no `diff --git` or `+++` headers found in patch',
271
+ };
272
+ recordToolResult(ctx.session, toolCallId, 'error', 'invalid_patch');
273
+ return result;
274
+ }
275
+ // SECURITY GATE — reuse the α6.6 chokepoint. Every path in the patch
276
+ // is validated against:
277
+ // 1. workspace containment (no ../../ escapes)
278
+ // 2. protected-file basenames (.env, *.pem, id_rsa, etc.)
279
+ // 3. symlink escape (an in-workspace symlink pointing to /etc/hosts
280
+ // or a protected basename gets rejected here)
281
+ for (const file of paths) {
282
+ const gate = applySecurityGate(file, { cwd: ctx.root, toolName: 'layer-c' });
283
+ if (!gate.ok) {
284
+ const result = {
285
+ ok: false,
286
+ filesChanged: [],
287
+ reason: gate.reason,
288
+ detail: `${file}: ${gate.detail}`,
289
+ };
290
+ recordToolResult(ctx.session, toolCallId, 'error', `${gate.reason}: ${file}`);
291
+ return result;
292
+ }
293
+ }
294
+ // `git apply --check` validates the patch end-to-end against the
295
+ // working tree. A passing check is the gate for the actual apply.
296
+ const checkArgs = ['apply', '--check'];
297
+ if (opts.baseSha)
298
+ checkArgs.push('--3way');
299
+ checkArgs.push('-');
300
+ const check = runGit(checkArgs, ctx.root, patch);
301
+ if (check.status === 127) {
302
+ // No git binary on PATH. Rare on a developer machine but possible
303
+ // in slim containers / CI images. Surface a dedicated reason so
304
+ // the operator's message says "install git" not "patch is bad".
305
+ const result = {
306
+ ok: false,
307
+ filesChanged: [],
308
+ reason: 'git_unavailable',
309
+ detail: 'git not found on PATH',
310
+ };
311
+ recordToolResult(ctx.session, toolCallId, 'error', 'git_unavailable');
312
+ return result;
313
+ }
314
+ if (check.status !== 0) {
315
+ // Decide whether this is the "already applied" case or a real
316
+ // failure. `git apply --check` rejects an already-applied patch
317
+ // with stderr containing patterns like "patch does not apply" or
318
+ // "already exists in working directory". The simpler signal is
319
+ // the stderr string containing `already exists in working directory`
320
+ // (git's own message for a creating patch landing twice) — that's
321
+ // the only path we treat as `already_applied` here. Other stderr
322
+ // surfaces fall through to `check_failed` so the operator sees the
323
+ // raw reason.
324
+ const stderr = check.stderr.toLowerCase();
325
+ if (stderr.includes('already exists in working directory')) {
326
+ const result = {
327
+ ok: false,
328
+ filesChanged: [],
329
+ reason: 'already_applied',
330
+ detail: 'patch creates a path that already exists — likely already applied',
331
+ };
332
+ recordToolResult(ctx.session, toolCallId, 'error', 'already_applied');
333
+ return result;
334
+ }
335
+ const result = {
336
+ ok: false,
337
+ filesChanged: [],
338
+ reason: 'check_failed',
339
+ detail: check.stderr.trim() || 'git apply --check rejected the patch',
340
+ };
341
+ recordToolResult(ctx.session, toolCallId, 'error', `check_failed: ${result.detail}`);
342
+ return result;
343
+ }
344
+ if (opts.dryRun) {
345
+ const result = {
346
+ ok: true,
347
+ filesChanged: paths,
348
+ };
349
+ recordToolResult(ctx.session, toolCallId, 'success', `dry-run ok, ${paths.length} files`);
350
+ return result;
351
+ }
352
+ // R1 fix (2026-05-26, PR #413 r1, Fix 6): snapshot which paths exist
353
+ // BEFORE the apply so rollbackFiles can decide between
354
+ // `git checkout -- <file>` (for files that existed) and `fs.rmSync`
355
+ // (for files the patch was creating that may have been half-written
356
+ // before the failure). Without this snapshot, `git checkout`
357
+ // gracefully no-ops on a never-tracked file and the partial creation
358
+ // is left behind.
359
+ const preExisting = new Map();
360
+ for (const p of paths) {
361
+ preExisting.set(p, existsSync(resolve(ctx.root, p)));
362
+ }
363
+ const applyArgs = ['apply'];
364
+ if (opts.baseSha)
365
+ applyArgs.push('--3way');
366
+ applyArgs.push('-');
367
+ const apply = runGit(applyArgs, ctx.root, patch);
368
+ if (apply.status !== 0) {
369
+ // Apply failed AFTER --check passed. This is almost always a TOCTOU
370
+ // (another writer touched a file between the two git calls).
371
+ // Rollback ANY partial mutation so the workspace stays consistent.
372
+ const rollback = rollbackFiles(ctx.root, paths, preExisting);
373
+ const detail = apply.stderr.trim() || 'git apply failed after passing --check';
374
+ if (!rollback.ok) {
375
+ const result = {
376
+ ok: false,
377
+ filesChanged: [],
378
+ reason: 'rollback_failed',
379
+ detail: `${detail}; rollback also failed: ${rollback.detail}`,
380
+ };
381
+ recordToolResult(ctx.session, toolCallId, 'error', 'rollback_failed');
382
+ return result;
383
+ }
384
+ const result = {
385
+ ok: false,
386
+ filesChanged: [],
387
+ reason: 'apply_failed',
388
+ detail,
389
+ };
390
+ recordToolResult(ctx.session, toolCallId, 'error', `apply_failed: ${detail}`);
391
+ return result;
392
+ }
393
+ // Audit-log every file the patch mutated. The before/after hashes
394
+ // are NOT recorded (git owns the staging area for that); the
395
+ // mutation entry is enough for `pugi undo` to surface "apply_patch
396
+ // touched these files" in the timeline.
397
+ for (const file of paths) {
398
+ recordFileMutation(ctx.session, {
399
+ toolCallId,
400
+ path: file,
401
+ operation: 'update',
402
+ });
403
+ }
404
+ recordToolResult(ctx.session, toolCallId, 'success', `applied ${paths.length} files`);
405
+ return { ok: true, filesChanged: paths };
406
+ }
407
+ /**
408
+ * Roll back any partial mutation by checking files out from HEAD. Used
409
+ * only on the rare path where `git apply` fails AFTER `git apply --check`
410
+ * passed.
411
+ *
412
+ * R1 fix (2026-05-26, PR #413 r1, Fix 6): a multi-file patch that
413
+ * creates new files leaves them on disk when `git apply` fails partway —
414
+ * `git checkout -- <file>` does NOT delete a path that was never tracked
415
+ * (the file was created by the failed apply). We split paths into two
416
+ * groups using the pre-apply snapshot:
417
+ *
418
+ * - existed-before -> `git checkout -- <file>` restores tracked content.
419
+ * - created-by-apply -> `fs.rmSync(file, { force: true })` removes the
420
+ * half-written file so the workspace ends up identical to its
421
+ * pre-apply state.
422
+ *
423
+ * This keeps the dispatcher's invariant: a tool result of `ok: false`
424
+ * means the workspace is unchanged.
425
+ */
426
+ function rollbackFiles(cwd, paths, preExisting) {
427
+ if (paths.length === 0)
428
+ return { ok: true };
429
+ // We only attempt to roll back files that are inside the workspace
430
+ // and were resolved by the security gate. A path that escaped the
431
+ // gate would have already aborted us above.
432
+ const safePaths = paths.filter((p) => {
433
+ const abs = resolve(cwd, p);
434
+ return abs === cwd || abs.startsWith(cwd + sep);
435
+ });
436
+ if (safePaths.length === 0)
437
+ return { ok: true };
438
+ const toCheckout = [];
439
+ const toRemove = [];
440
+ for (const p of safePaths) {
441
+ if (preExisting.get(p))
442
+ toCheckout.push(p);
443
+ else
444
+ toRemove.push(p);
445
+ }
446
+ // Unlink files that the patch was creating. `force: true` swallows
447
+ // ENOENT so a creation that never got far enough to write the file
448
+ // is a no-op. We record every unlink failure but keep going so a
449
+ // single permission error on one file doesn't strand the others.
450
+ const removeFailures = [];
451
+ for (const p of toRemove) {
452
+ const abs = resolve(cwd, p);
453
+ try {
454
+ rmSync(abs, { force: true });
455
+ }
456
+ catch (error) {
457
+ removeFailures.push(`${p}: ${error instanceof Error ? error.message : String(error)}`);
458
+ }
459
+ }
460
+ if (toCheckout.length > 0) {
461
+ const result = runGit(['checkout', '--', ...toCheckout], cwd);
462
+ if (result.status !== 0) {
463
+ const detail = [result.stderr.trim(), ...removeFailures].filter(Boolean).join('; ');
464
+ return { ok: false, detail };
465
+ }
466
+ }
467
+ if (removeFailures.length > 0) {
468
+ return { ok: false, detail: `rollback unlink failed: ${removeFailures.join('; ')}` };
469
+ }
470
+ return { ok: true };
471
+ }
472
+ function runGit(args, cwd, stdin) {
473
+ // R1 fix (2026-05-26, PR #413 r1, P2 #13): force the English C locale
474
+ // for the git child process. The `already_applied` reason-coding
475
+ // below greps stderr for the literal English string
476
+ // "already exists in working directory"; on a host where git was
477
+ // installed with a translated message catalog (de_DE / ru_RU / etc.)
478
+ // the substring match would silently miss and the operator would see
479
+ // `check_failed` instead of `already_applied`. C locale (also
480
+ // LC_ALL) guarantees the canonical message regardless of host env.
481
+ return spawnSync('git', args, {
482
+ cwd,
483
+ input: stdin,
484
+ encoding: 'utf8',
485
+ maxBuffer: 64 * 1024 * 1024,
486
+ env: { ...process.env, LANG: 'C', LC_ALL: 'C' },
487
+ });
488
+ }
489
+ /**
490
+ * Test-only surface for the apply-patch heuristics. Specs poke
491
+ * `extractPatchPaths` directly to assert on the path-parsing layer
492
+ * without paying for a real git invocation.
493
+ */
494
+ export const __test__ = { extractPatchPaths, runGit, unquoteGitPath };
495
+ //# sourceMappingURL=apply-patch.js.map
@@ -0,0 +1,189 @@
1
+ import { gateOnCancellation, OperatorAbortedError } from './file-tools.js';
2
+ import { recordToolCall, recordToolResult } from '../core/session.js';
3
+ /** Cap for any single LSP tool's payload size. Keeps model context lean. */
4
+ const LSP_PAYLOAD_CAP_BYTES = 8 * 1024;
5
+ export async function lspHover(ctx, lang, file, line, col) {
6
+ const toolCallId = recordToolCall(ctx.session, 'lsp_hover', `${lang}:${file}:${line}:${col}`);
7
+ return guard(ctx, 'lsp_hover', toolCallId, async () => {
8
+ const client = ctx.lspClients?.get(lang);
9
+ if (!client)
10
+ return unavailable(lang);
11
+ const result = await client.hover(file, { line, character: col }, ctx.cancellation);
12
+ if (!result.ok)
13
+ return failure(result);
14
+ if (!result.value) {
15
+ return { ok: true, value: { content: '' } };
16
+ }
17
+ const content = truncate(result.value.content);
18
+ return {
19
+ ok: true,
20
+ value: {
21
+ content: content.text,
22
+ ...(result.value.range ? { range: result.value.range } : {}),
23
+ },
24
+ ...(content.truncated ? { truncated: true } : {}),
25
+ };
26
+ });
27
+ }
28
+ export async function lspDefinition(ctx, lang, file, line, col) {
29
+ const toolCallId = recordToolCall(ctx.session, 'lsp_definition', `${lang}:${file}:${line}:${col}`);
30
+ return guard(ctx, 'lsp_definition', toolCallId, async () => {
31
+ const client = ctx.lspClients?.get(lang);
32
+ if (!client)
33
+ return unavailable(lang);
34
+ const result = await client.definition(file, { line, character: col }, ctx.cancellation);
35
+ if (!result.ok)
36
+ return failure(result);
37
+ const capped = capLocations(result.value);
38
+ return {
39
+ ok: true,
40
+ value: capped.value,
41
+ ...(capped.truncated ? { truncated: true } : {}),
42
+ };
43
+ });
44
+ }
45
+ export async function lspReferences(ctx, lang, file, line, col) {
46
+ const toolCallId = recordToolCall(ctx.session, 'lsp_references', `${lang}:${file}:${line}:${col}`);
47
+ return guard(ctx, 'lsp_references', toolCallId, async () => {
48
+ const client = ctx.lspClients?.get(lang);
49
+ if (!client)
50
+ return unavailable(lang);
51
+ const result = await client.references(file, { line, character: col }, ctx.cancellation);
52
+ if (!result.ok)
53
+ return failure(result);
54
+ const capped = capLocations(result.value);
55
+ return {
56
+ ok: true,
57
+ value: capped.value,
58
+ ...(capped.truncated ? { truncated: true } : {}),
59
+ };
60
+ });
61
+ }
62
+ export async function lspDiagnostics(ctx, lang, file) {
63
+ const toolCallId = recordToolCall(ctx.session, 'lsp_diagnostics', `${lang}:${file}`);
64
+ return guard(ctx, 'lsp_diagnostics', toolCallId, async () => {
65
+ const client = ctx.lspClients?.get(lang);
66
+ if (!client)
67
+ return unavailable(lang);
68
+ const result = await client.diagnostics(file, ctx.cancellation);
69
+ if (!result.ok)
70
+ return failure(result);
71
+ const capped = capDiagnostics(result.value);
72
+ return {
73
+ ok: true,
74
+ value: capped.value,
75
+ ...(capped.truncated ? { truncated: true } : {}),
76
+ };
77
+ });
78
+ }
79
+ async function guard(ctx, toolName, toolCallId, op) {
80
+ try {
81
+ gateOnCancellation(ctx, toolName);
82
+ }
83
+ catch (error) {
84
+ if (error instanceof OperatorAbortedError) {
85
+ recordToolResult(ctx.session, toolCallId, 'cancelled', error.message);
86
+ return { ok: false, reason: 'operator_aborted', detail: error.message };
87
+ }
88
+ throw error;
89
+ }
90
+ try {
91
+ const result = await op();
92
+ if (result.ok) {
93
+ recordToolResult(ctx.session, toolCallId, 'success', summarize(result.value));
94
+ }
95
+ else {
96
+ recordToolResult(ctx.session, toolCallId, 'error', `${result.reason ?? 'error'}: ${result.detail ?? ''}`);
97
+ }
98
+ return result;
99
+ }
100
+ catch (error) {
101
+ const message = error instanceof Error ? error.message : String(error);
102
+ recordToolResult(ctx.session, toolCallId, 'error', message);
103
+ return { ok: false, reason: 'lsp_error', detail: message };
104
+ }
105
+ }
106
+ function unavailable(lang) {
107
+ return {
108
+ ok: false,
109
+ reason: 'lsp_unavailable',
110
+ detail: `no LSP server started for ${lang}. Install the server and re-run ` +
111
+ `with --lsp ${lang}, or fall back to grep.`,
112
+ };
113
+ }
114
+ function failure(result) {
115
+ if (result.ok) {
116
+ // Shouldn't be hit — caller checks first.
117
+ return { ok: true, value: result.value };
118
+ }
119
+ return { ok: false, reason: result.reason, detail: result.detail };
120
+ }
121
+ function summarize(value) {
122
+ if (value === null || value === undefined)
123
+ return 'no result';
124
+ if (Array.isArray(value))
125
+ return `${value.length} items`;
126
+ if (typeof value === 'object')
127
+ return Object.keys(value).join(',');
128
+ return String(value);
129
+ }
130
+ function truncate(text) {
131
+ const bytes = Buffer.byteLength(text, 'utf8');
132
+ if (bytes <= LSP_PAYLOAD_CAP_BYTES)
133
+ return { text, truncated: false };
134
+ // Truncate to the cap byte boundary. We don't try to honor codepoint
135
+ // alignment — UTF-8 surrogate splits show up as a single ? at the
136
+ // boundary, which is acceptable for a debug surface; the dispatcher
137
+ // is the trust boundary for "this is what the model will see".
138
+ const buf = Buffer.from(text, 'utf8').subarray(0, LSP_PAYLOAD_CAP_BYTES);
139
+ return { text: `${buf.toString('utf8')}\n... [truncated]`, truncated: true };
140
+ }
141
+ function capLocations(locations) {
142
+ // Cap at 200 locations OR the byte cap, whichever hits first. The
143
+ // 200 number is the operator-facing "this is a hot symbol" threshold —
144
+ // a richer surface (paginated `pugi lsp references --offset N`) is
145
+ // open backlog.
146
+ const COUNT_CAP = 200;
147
+ if (locations.length === 0)
148
+ return { value: locations, truncated: false };
149
+ const trimmed = locations.slice(0, COUNT_CAP);
150
+ const serialized = JSON.stringify(trimmed);
151
+ if (Buffer.byteLength(serialized, 'utf8') <= LSP_PAYLOAD_CAP_BYTES && trimmed.length === locations.length) {
152
+ return { value: trimmed, truncated: false };
153
+ }
154
+ // Trim by halves until we fit the byte cap. Worst case ~10 iterations
155
+ // for the 200 max, fine for an interactive tool.
156
+ let upper = trimmed.length;
157
+ while (upper > 1) {
158
+ const half = Math.floor(upper / 2);
159
+ const sub = trimmed.slice(0, half);
160
+ if (Buffer.byteLength(JSON.stringify(sub), 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
161
+ return { value: sub, truncated: true };
162
+ }
163
+ upper = half;
164
+ }
165
+ return { value: trimmed.slice(0, 1), truncated: true };
166
+ }
167
+ function capDiagnostics(items) {
168
+ if (items.length === 0)
169
+ return { value: items, truncated: false };
170
+ const serialized = JSON.stringify(items);
171
+ if (Buffer.byteLength(serialized, 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
172
+ return { value: items, truncated: false };
173
+ }
174
+ // Diagnostics are sorted error-first in LSP convention; trim from the
175
+ // tail so we keep the highest-severity items.
176
+ let upper = items.length;
177
+ while (upper > 1) {
178
+ const half = Math.floor(upper / 2);
179
+ const sub = items.slice(0, half);
180
+ if (Buffer.byteLength(JSON.stringify(sub), 'utf8') <= LSP_PAYLOAD_CAP_BYTES) {
181
+ return { value: sub, truncated: true };
182
+ }
183
+ upper = half;
184
+ }
185
+ return { value: items.slice(0, 1), truncated: true };
186
+ }
187
+ /** Test-only surface so specs can poke truncation directly. */
188
+ export const __test__ = { truncate, capLocations, capDiagnostics, LSP_PAYLOAD_CAP_BYTES };
189
+ //# sourceMappingURL=lsp-tools.js.map