@pugi/cli 0.1.0-beta.3 → 0.1.0-beta.5

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.
@@ -1,314 +0,0 @@
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 { resolve, sep } from 'node:path';
47
- import { applySecurityGate } from '../core/edits/security-gate.js';
48
- import { gateOnCancellation, OperatorAbortedError } from './file-tools.js';
49
- import { recordToolCall, recordToolResult, recordFileMutation } from '../core/session.js';
50
- /**
51
- * Parse the file paths referenced in a unified diff. We look for both
52
- * `diff --git a/X b/Y` headers (preferred) and the fallback
53
- * `+++ b/<path>` lines that plain `diff -u` emits. The full set of
54
- * touched paths feeds the security gate — EVERY file goes through
55
- * `applySecurityGate` before we trust `git apply` to do anything.
56
- */
57
- export function extractPatchPaths(patch) {
58
- const paths = new Set();
59
- for (const line of patch.split('\n')) {
60
- if (line.startsWith('diff --git ')) {
61
- // `diff --git a/foo b/bar` — paths can contain spaces only when
62
- // quoted by git's own diff machinery (rare). The robust extractor
63
- // matches the `b/...` half because rename diffs carry the new
64
- // name there.
65
- const match = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
66
- if (match) {
67
- if (match[1])
68
- paths.add(match[1]);
69
- if (match[2])
70
- paths.add(match[2]);
71
- }
72
- continue;
73
- }
74
- if (line.startsWith('+++ ')) {
75
- const after = line.slice(4).trim();
76
- if (after === '/dev/null')
77
- continue;
78
- const trimmed = after.startsWith('b/') ? after.slice(2) : after;
79
- if (trimmed)
80
- paths.add(stripTimestampSuffix(trimmed));
81
- continue;
82
- }
83
- if (line.startsWith('--- ')) {
84
- const after = line.slice(4).trim();
85
- if (after === '/dev/null')
86
- continue;
87
- const trimmed = after.startsWith('a/') ? after.slice(2) : after;
88
- if (trimmed)
89
- paths.add(stripTimestampSuffix(trimmed));
90
- }
91
- }
92
- return Array.from(paths);
93
- }
94
- /**
95
- * `diff -u` (non-git) emits trailing tab-prefixed timestamps after the
96
- * path: `--- foo.ts\t2026-05-25 10:00:00`. Strip those so the security
97
- * gate sees the clean path.
98
- */
99
- function stripTimestampSuffix(path) {
100
- const tab = path.indexOf('\t');
101
- return tab >= 0 ? path.slice(0, tab) : path;
102
- }
103
- /**
104
- * Apply a unified-diff patch to the workspace. Routes every mentioned
105
- * file through the shared security gate before invoking `git apply`.
106
- */
107
- export function applyPatch(ctx, patch, opts = {}) {
108
- const toolCallId = recordToolCall(ctx.session, 'apply_patch', `${patch.length} bytes`);
109
- try {
110
- gateOnCancellation(ctx, 'apply_patch');
111
- }
112
- catch (error) {
113
- if (error instanceof OperatorAbortedError) {
114
- recordToolResult(ctx.session, toolCallId, 'cancelled', error.message);
115
- throw error;
116
- }
117
- throw error;
118
- }
119
- if (patch.trim().length === 0) {
120
- const result = {
121
- ok: false,
122
- filesChanged: [],
123
- reason: 'empty_patch',
124
- detail: 'patch body is empty',
125
- };
126
- recordToolResult(ctx.session, toolCallId, 'error', 'empty_patch');
127
- return result;
128
- }
129
- const paths = extractPatchPaths(patch);
130
- if (paths.length === 0) {
131
- const result = {
132
- ok: false,
133
- filesChanged: [],
134
- reason: 'invalid_patch',
135
- detail: 'no `diff --git` or `+++` headers found in patch',
136
- };
137
- recordToolResult(ctx.session, toolCallId, 'error', 'invalid_patch');
138
- return result;
139
- }
140
- // SECURITY GATE — reuse the α6.6 chokepoint. Every path in the patch
141
- // is validated against:
142
- // 1. workspace containment (no ../../ escapes)
143
- // 2. protected-file basenames (.env, *.pem, id_rsa, etc.)
144
- // 3. symlink escape (an in-workspace symlink pointing to /etc/hosts
145
- // or a protected basename gets rejected here)
146
- for (const file of paths) {
147
- const gate = applySecurityGate(file, { cwd: ctx.root, toolName: 'layer-c' });
148
- if (!gate.ok) {
149
- const result = {
150
- ok: false,
151
- filesChanged: [],
152
- reason: gate.reason,
153
- detail: `${file}: ${gate.detail}`,
154
- };
155
- recordToolResult(ctx.session, toolCallId, 'error', `${gate.reason}: ${file}`);
156
- return result;
157
- }
158
- }
159
- // `git apply --check` validates the patch end-to-end against the
160
- // working tree. A passing check is the gate for the actual apply.
161
- const checkArgs = ['apply', '--check'];
162
- if (opts.baseSha)
163
- checkArgs.push('--3way');
164
- checkArgs.push('-');
165
- const check = runGit(checkArgs, ctx.root, patch);
166
- if (check.status === 127) {
167
- // No git binary on PATH. Rare on a developer machine but possible
168
- // in slim containers / CI images. Surface a dedicated reason so
169
- // the operator's message says "install git" not "patch is bad".
170
- const result = {
171
- ok: false,
172
- filesChanged: [],
173
- reason: 'git_unavailable',
174
- detail: 'git not found on PATH',
175
- };
176
- recordToolResult(ctx.session, toolCallId, 'error', 'git_unavailable');
177
- return result;
178
- }
179
- if (check.status !== 0) {
180
- // Decide whether this is the "already applied" case or a real
181
- // failure. `git apply --check` rejects an already-applied patch
182
- // with stderr containing `error: patch failed` and at least one
183
- // hunk that mentions "patch does not apply" or "already exists".
184
- // We use a conservative heuristic: when EVERY targeted file in the
185
- // diff is present and the patch's pre-image is missing, treat it
186
- // as already_applied. The simpler signal is the stderr string
187
- // containing `already exists in working directory` (git's own
188
- // message for a creating patch landing twice) or `does not apply`.
189
- const stderr = check.stderr.toLowerCase();
190
- if (stderr.includes('already exists in working directory') || isLikelyAlreadyApplied(check.stderr, patch)) {
191
- const result = {
192
- ok: false,
193
- filesChanged: [],
194
- reason: 'already_applied',
195
- detail: 'patch pre-image does not match working tree — patch was likely already applied',
196
- };
197
- recordToolResult(ctx.session, toolCallId, 'error', 'already_applied');
198
- return result;
199
- }
200
- const result = {
201
- ok: false,
202
- filesChanged: [],
203
- reason: 'check_failed',
204
- detail: check.stderr.trim() || 'git apply --check rejected the patch',
205
- };
206
- recordToolResult(ctx.session, toolCallId, 'error', `check_failed: ${result.detail}`);
207
- return result;
208
- }
209
- if (opts.dryRun) {
210
- const result = {
211
- ok: true,
212
- filesChanged: paths,
213
- };
214
- recordToolResult(ctx.session, toolCallId, 'success', `dry-run ok, ${paths.length} files`);
215
- return result;
216
- }
217
- const applyArgs = ['apply'];
218
- if (opts.baseSha)
219
- applyArgs.push('--3way');
220
- applyArgs.push('-');
221
- const apply = runGit(applyArgs, ctx.root, patch);
222
- if (apply.status !== 0) {
223
- // Apply failed AFTER --check passed. This is almost always a TOCTOU
224
- // (another writer touched a file between the two git calls).
225
- // Rollback ANY partial mutation so the workspace stays consistent.
226
- const rollback = rollbackFiles(ctx.root, paths);
227
- const detail = apply.stderr.trim() || 'git apply failed after passing --check';
228
- if (!rollback.ok) {
229
- const result = {
230
- ok: false,
231
- filesChanged: [],
232
- reason: 'rollback_failed',
233
- detail: `${detail}; rollback also failed: ${rollback.detail}`,
234
- };
235
- recordToolResult(ctx.session, toolCallId, 'error', 'rollback_failed');
236
- return result;
237
- }
238
- const result = {
239
- ok: false,
240
- filesChanged: [],
241
- reason: 'apply_failed',
242
- detail,
243
- };
244
- recordToolResult(ctx.session, toolCallId, 'error', `apply_failed: ${detail}`);
245
- return result;
246
- }
247
- // Audit-log every file the patch mutated. The before/after hashes
248
- // are NOT recorded (git owns the staging area for that); the
249
- // mutation entry is enough for `pugi undo` to surface "apply_patch
250
- // touched these files" in the timeline.
251
- for (const file of paths) {
252
- recordFileMutation(ctx.session, {
253
- toolCallId,
254
- path: file,
255
- operation: 'update',
256
- });
257
- }
258
- recordToolResult(ctx.session, toolCallId, 'success', `applied ${paths.length} files`);
259
- return { ok: true, filesChanged: paths };
260
- }
261
- /**
262
- * Heuristic for the "patch already applied" case beyond git's explicit
263
- * `already exists` string. When `git apply --check` rejects with
264
- * `patch does not apply` AND every hunk's target file exists with
265
- * matching post-image lines, the patch is effectively a no-op repeat.
266
- * We keep the heuristic minimal because the explicit string covers the
267
- * dominant case; full hunk inspection would require parsing the patch
268
- * (which is what `git apply` does in the first place).
269
- */
270
- function isLikelyAlreadyApplied(stderr, _patch) {
271
- return stderr.toLowerCase().includes('patch does not apply') === false ? false : false;
272
- // Intentionally conservative for now — the explicit `already exists`
273
- // case above covers the only path that's reliably distinguishable
274
- // from a malformed-patch failure. A richer detector can land in
275
- // α7.7b when we have a corpus of real "already_applied" stderr samples.
276
- }
277
- /**
278
- * Roll back any partial mutation by checking files out from HEAD. Used
279
- * only on the rare path where `git apply` fails AFTER `git apply --check`
280
- * passed.
281
- */
282
- function rollbackFiles(cwd, paths) {
283
- if (paths.length === 0)
284
- return { ok: true };
285
- // We only attempt to roll back files that are inside the workspace
286
- // and were resolved by the security gate. A path that escaped the
287
- // gate would have already aborted us above.
288
- const safePaths = paths.filter((p) => {
289
- const abs = resolve(cwd, p);
290
- return abs === cwd || abs.startsWith(cwd + sep);
291
- });
292
- if (safePaths.length === 0)
293
- return { ok: true };
294
- const result = runGit(['checkout', '--', ...safePaths], cwd);
295
- if (result.status !== 0) {
296
- return { ok: false, detail: result.stderr.trim() };
297
- }
298
- return { ok: true };
299
- }
300
- function runGit(args, cwd, stdin) {
301
- return spawnSync('git', args, {
302
- cwd,
303
- input: stdin,
304
- encoding: 'utf8',
305
- maxBuffer: 64 * 1024 * 1024,
306
- });
307
- }
308
- /**
309
- * Test-only surface for the apply-patch heuristics. Specs poke
310
- * `extractPatchPaths` directly to assert on the path-parsing layer
311
- * without paying for a real git invocation.
312
- */
313
- export const __test__ = { extractPatchPaths, isLikelyAlreadyApplied, runGit };
314
- //# sourceMappingURL=apply-patch.js.map
@@ -1,189 +0,0 @@
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