@pugi/cli 0.1.0-alpha.9 → 0.1.0-beta.2
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/README.md +33 -0
- package/assets/pugi-mascot.ansi +41 -0
- package/dist/commands/deploy.js +439 -0
- package/dist/core/agents/loader.js +104 -0
- package/dist/core/agents/registry.js +1 -1
- package/dist/core/consensus/anvil-fanout.js +276 -0
- package/dist/core/consensus/diff-capture.js +382 -0
- package/dist/core/consensus/rubric.js +233 -0
- package/dist/core/context/index.js +21 -0
- package/dist/core/context/pugiignore.js +316 -0
- package/dist/core/context/repo-skeleton.js +533 -0
- package/dist/core/context/watcher.js +342 -0
- package/dist/core/context/working-set.js +165 -0
- package/dist/core/edits/dispatch.js +185 -0
- package/dist/core/edits/index.js +15 -0
- package/dist/core/edits/layer-a-apply.js +217 -0
- package/dist/core/edits/layer-b-apply.js +211 -0
- package/dist/core/edits/layer-c-apply.js +160 -0
- package/dist/core/edits/layer-d-ast.js +29 -0
- package/dist/core/edits/marker-parser.js +401 -0
- package/dist/core/edits/security-gate.js +223 -0
- package/dist/core/edits/worktree.js +229 -0
- package/dist/core/engine/native-pugi.js +6 -1
- package/dist/core/engine/prompts.js +4 -1
- package/dist/core/engine/tool-bridge.js +33 -1
- package/dist/core/lsp/client.js +631 -0
- package/dist/core/repl/ask.js +512 -0
- package/dist/core/repl/cancellation.js +98 -0
- package/dist/core/repl/dispatch-fsm.js +220 -0
- package/dist/core/repl/privacy-banner.js +71 -0
- package/dist/core/repl/session.js +1896 -13
- package/dist/core/repl/slash-commands.js +59 -32
- package/dist/core/repl/store/index.js +12 -0
- package/dist/core/repl/store/jsonl-log.js +321 -0
- package/dist/core/repl/store/lockfile.js +155 -0
- package/dist/core/repl/store/session-store.js +792 -0
- package/dist/core/repl/store/types.js +44 -0
- package/dist/core/repl/store/uuid-v7.js +68 -0
- package/dist/core/repl/workspace-context.js +72 -1
- package/dist/core/skills/loader.js +454 -0
- package/dist/core/skills/sources.js +480 -0
- package/dist/core/skills/trust.js +172 -0
- package/dist/runtime/cli.js +767 -10
- package/dist/runtime/commands/agents.js +385 -0
- package/dist/runtime/commands/config.js +338 -8
- package/dist/runtime/commands/lsp.js +184 -0
- package/dist/runtime/commands/patch.js +111 -0
- package/dist/runtime/commands/review-consensus.js +399 -0
- package/dist/runtime/commands/skills.js +401 -0
- package/dist/runtime/commands/worktree.js +133 -0
- package/dist/tools/apply-patch.js +314 -0
- package/dist/tools/file-tools.js +90 -0
- package/dist/tools/lsp-tools.js +189 -0
- package/dist/tools/registry.js +18 -0
- package/dist/tools/web-fetch.js +1 -1
- package/dist/tui/agent-tree-pane.js +9 -0
- package/dist/tui/ask-cli.js +52 -0
- package/dist/tui/ask-modal.js +211 -0
- package/dist/tui/conversation-pane.js +48 -3
- package/dist/tui/input-box.js +48 -5
- package/dist/tui/markdown-render.js +266 -0
- package/dist/tui/repl-render.js +185 -0
- package/dist/tui/repl-splash-mascot.js +130 -0
- package/dist/tui/repl-splash.js +7 -1
- package/dist/tui/repl.js +82 -11
- package/dist/tui/status-bar.js +63 -3
- package/dist/tui/tool-stream-pane.js +91 -0
- package/package.json +11 -5
|
@@ -0,0 +1,314 @@
|
|
|
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
|
package/dist/tools/file-tools.js
CHANGED
|
@@ -6,6 +6,35 @@ import { decidePermission } from '../core/permission.js';
|
|
|
6
6
|
import { createReadRecord, hashContent } from '../core/file-cache.js';
|
|
7
7
|
import { resolveWorkspacePath } from '../core/path-security.js';
|
|
8
8
|
import { recordFileMutation, recordToolCall, recordToolResult } from '../core/session.js';
|
|
9
|
+
/**
|
|
10
|
+
* α6.9 WriteGate marker — thrown by `gateOnCancellation` when the
|
|
11
|
+
* caller supplied a cancellation token that has already aborted. The
|
|
12
|
+
* tool dispatch loop in `tool-bridge.ts` recognises the name and folds
|
|
13
|
+
* the throw into a `status: 'aborted'` tool result rather than a hard
|
|
14
|
+
* error so the loop terminates cleanly.
|
|
15
|
+
*/
|
|
16
|
+
export class OperatorAbortedError extends Error {
|
|
17
|
+
constructor(toolName) {
|
|
18
|
+
super(`operator_aborted: ${toolName} refused — operator cancelled the dispatch.`);
|
|
19
|
+
this.name = 'OperatorAbortedError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* α6.9 WriteGate: refuse the tool dispatch when the active
|
|
24
|
+
* cancellation token has aborted. Idempotent (the token's `isAborted`
|
|
25
|
+
* is a getter, no side effects). Returns void on the happy path so the
|
|
26
|
+
* tool can proceed; throws `OperatorAbortedError` when cancelled.
|
|
27
|
+
*
|
|
28
|
+
* The audit trail still gets the call: `recordToolCall` already fired
|
|
29
|
+
* upstream of this guard so the abort + reason are persisted. The
|
|
30
|
+
* matching `recordToolResult` is fired by the caller in its catch
|
|
31
|
+
* block with `status: 'cancelled'` (see existing path for `error`).
|
|
32
|
+
*/
|
|
33
|
+
export function gateOnCancellation(ctx, toolName) {
|
|
34
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
35
|
+
throw new OperatorAbortedError(toolName);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
9
38
|
/**
|
|
10
39
|
* Re-check the permission decision against the *resolved* real path so
|
|
11
40
|
* a workspace-local symlink (`alias -> .env`) cannot bypass the protected
|
|
@@ -42,6 +71,13 @@ function permissionGatedResolve(ctx, inputPath, action, toolName) {
|
|
|
42
71
|
}
|
|
43
72
|
export function readTool(ctx, path) {
|
|
44
73
|
const toolCallId = recordToolCall(ctx.session, 'read', path);
|
|
74
|
+
// α6.9 WriteGate: fail fast on operator cancel BEFORE permission
|
|
75
|
+
// decision so a half-second post-cancel race never lands the read.
|
|
76
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
77
|
+
const reason = 'operator_aborted: read refused';
|
|
78
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
79
|
+
throw new OperatorAbortedError('read');
|
|
80
|
+
}
|
|
45
81
|
const decision = decidePermission({ tool: 'read', kind: 'read', target: path }, ctx.settings, ctx.root);
|
|
46
82
|
if (decision.decision !== 'allow') {
|
|
47
83
|
const reason = `Permission ${decision.decision} for read ${path}: ${decision.reason}`;
|
|
@@ -64,6 +100,14 @@ export function readTool(ctx, path) {
|
|
|
64
100
|
}
|
|
65
101
|
export function writeTool(ctx, path, content) {
|
|
66
102
|
const toolCallId = recordToolCall(ctx.session, 'write', path);
|
|
103
|
+
// α6.9 WriteGate: refuse the write when the operator has cancelled
|
|
104
|
+
// the dispatch. The audit log captures the cancellation reason so a
|
|
105
|
+
// post-mortem can distinguish operator_aborted from settings-deny.
|
|
106
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
107
|
+
const reason = 'operator_aborted: write refused';
|
|
108
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
109
|
+
throw new OperatorAbortedError('write');
|
|
110
|
+
}
|
|
67
111
|
const decision = decidePermission({ tool: 'write', kind: 'edit', target: path }, ctx.settings, ctx.root);
|
|
68
112
|
if (decision.decision !== 'allow') {
|
|
69
113
|
const reason = `Permission ${decision.decision} for write ${path}: ${decision.reason}`;
|
|
@@ -95,6 +139,15 @@ export function writeTool(ctx, path, content) {
|
|
|
95
139
|
}
|
|
96
140
|
export function editTool(ctx, path, oldString, newString) {
|
|
97
141
|
const toolCallId = recordToolCall(ctx.session, 'edit', path);
|
|
142
|
+
// α6.9 WriteGate: refuse the edit when the operator has cancelled
|
|
143
|
+
// the dispatch. Edits are higher-risk than reads — surface the abort
|
|
144
|
+
// BEFORE we even consult permissions so a cancel-during-tool-loop
|
|
145
|
+
// never partially mutates the workspace.
|
|
146
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
147
|
+
const reason = 'operator_aborted: edit refused';
|
|
148
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
149
|
+
throw new OperatorAbortedError('edit');
|
|
150
|
+
}
|
|
98
151
|
const decision = decidePermission({ tool: 'edit', kind: 'edit', target: path }, ctx.settings, ctx.root);
|
|
99
152
|
if (decision.decision !== 'allow') {
|
|
100
153
|
const reason = `Permission ${decision.decision} for edit ${path}: ${decision.reason}`;
|
|
@@ -140,6 +193,14 @@ export function editTool(ctx, path, oldString, newString) {
|
|
|
140
193
|
}
|
|
141
194
|
export function globTool(ctx, pattern) {
|
|
142
195
|
const toolCallId = recordToolCall(ctx.session, 'glob', pattern);
|
|
196
|
+
// α6.9 WriteGate: cancel-aware short-circuit. Glob is read-only but
|
|
197
|
+
// can be expensive on large trees; respecting the abort here keeps
|
|
198
|
+
// the tool loop responsive when the operator hits Ctrl+C mid-scan.
|
|
199
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
200
|
+
const reason = 'operator_aborted: glob refused';
|
|
201
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
202
|
+
throw new OperatorAbortedError('glob');
|
|
203
|
+
}
|
|
143
204
|
// Pugi globs are workspace-scoped. Reject any pattern that could enumerate
|
|
144
205
|
// outside the workspace:
|
|
145
206
|
// 1. absolute paths (`/etc/**/*`) — globSync resolves these against `/`
|
|
@@ -169,11 +230,28 @@ export function globTool(ctx, pattern) {
|
|
|
169
230
|
}
|
|
170
231
|
export function grepTool(ctx, query) {
|
|
171
232
|
const toolCallId = recordToolCall(ctx.session, 'grep', query);
|
|
233
|
+
// α6.9 WriteGate: refuse before scanning. Grep walks the whole
|
|
234
|
+
// workspace and can take seconds on a large repo; check abort first
|
|
235
|
+
// so a cancel mid-scan returns immediately rather than after the
|
|
236
|
+
// full walk completes.
|
|
237
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
238
|
+
const reason = 'operator_aborted: grep refused';
|
|
239
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
240
|
+
throw new OperatorAbortedError('grep');
|
|
241
|
+
}
|
|
172
242
|
const files = globTool(ctx, '**/*').filter((path) => !path.endsWith('/'));
|
|
173
243
|
const matches = [];
|
|
174
244
|
for (const path of files) {
|
|
175
245
|
if (matches.length >= 200)
|
|
176
246
|
break;
|
|
247
|
+
// α6.9 WriteGate: poll abort inside the file loop so a cancel
|
|
248
|
+
// arriving mid-scan terminates early. The per-file branch keeps
|
|
249
|
+
// the responsiveness bounded by the slowest single-file read.
|
|
250
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
251
|
+
const reason = `operator_aborted: grep stopped mid-scan after ${matches.length} matches`;
|
|
252
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
253
|
+
throw new OperatorAbortedError('grep');
|
|
254
|
+
}
|
|
177
255
|
// Permission gate every file read individually — grep used to bypass
|
|
178
256
|
// `decidePermission` and could surface lines from protected files
|
|
179
257
|
// (.env, *.sql, *.pem, ~/.ssh/**) when invoked from a directory walk.
|
|
@@ -241,6 +319,18 @@ export const BASH_DEFAULT_TIMEOUT_MS = 30_000;
|
|
|
241
319
|
export const BASH_CHILD_MAXBUFFER = 10 * 1024 * 1024;
|
|
242
320
|
export function bashTool(ctx, command, options = {}) {
|
|
243
321
|
const toolCallId = recordToolCall(ctx.session, 'bash', command);
|
|
322
|
+
// α6.9 WriteGate: bash is the highest-risk tool surface. Refuse
|
|
323
|
+
// before the destructive-pattern classifier even runs so a
|
|
324
|
+
// cancelled dispatch never spawns a child process. Note: this is
|
|
325
|
+
// pre-spawn cancellation only; once the /bin/sh -c process is
|
|
326
|
+
// running, the synchronous spawnSync wait blocks until it exits or
|
|
327
|
+
// the 30s timeout fires. Phase 2 will wire SIGTERM forwarding via
|
|
328
|
+
// an async wrapper.
|
|
329
|
+
if (ctx.cancellation && ctx.cancellation.isAborted) {
|
|
330
|
+
const reason = 'operator_aborted: bash refused';
|
|
331
|
+
recordToolResult(ctx.session, toolCallId, 'cancelled', reason);
|
|
332
|
+
throw new OperatorAbortedError('bash');
|
|
333
|
+
}
|
|
244
334
|
const decision = decidePermission({ tool: 'bash', kind: 'bash', target: command }, ctx.settings, ctx.root);
|
|
245
335
|
if (decision.decision !== 'allow') {
|
|
246
336
|
const reason = `Permission ${decision.decision} for bash: ${decision.reason}`;
|
|
@@ -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
|
package/dist/tools/registry.js
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
const registry = [
|
|
2
|
+
// α7.7: unified-diff patch apply. Routes through the same security
|
|
3
|
+
// gate as Layer A/B/C, so the risk class matches `edit`/`write`
|
|
4
|
+
// (medium — writes inside the workspace, never to protected files).
|
|
5
|
+
{ name: 'apply_patch', permission: 'edit', risk: 'medium', concurrencySafe: false, m1: true },
|
|
2
6
|
{ name: 'bash', permission: 'bash', risk: 'high', concurrencySafe: false, m1: true },
|
|
3
7
|
{ name: 'edit', permission: 'edit', risk: 'medium', concurrencySafe: false, m1: true },
|
|
4
8
|
{ name: 'glob', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
5
9
|
{ name: 'grep', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
10
|
+
// α7.7: LSP read-only surface. Server runs locally, no Anvil
|
|
11
|
+
// round-trip. Concurrency-safe because every operation reads
|
|
12
|
+
// server state without mutating workspace files.
|
|
13
|
+
{ name: 'lsp_definition', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
14
|
+
{ name: 'lsp_diagnostics', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
15
|
+
{ name: 'lsp_hover', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
16
|
+
{ name: 'lsp_references', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
6
17
|
{ name: 'question', permission: 'none', risk: 'low', concurrencySafe: false, m1: true },
|
|
7
18
|
{ name: 'read', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
8
19
|
{ name: 'skill', permission: 'read', risk: 'low', concurrencySafe: true, m1: true },
|
|
@@ -11,6 +22,13 @@ const registry = [
|
|
|
11
22
|
{ name: 'task_list', permission: 'none', risk: 'low', concurrencySafe: true, m1: true },
|
|
12
23
|
{ name: 'task_update', permission: 'none', risk: 'low', concurrencySafe: false, m1: true },
|
|
13
24
|
{ name: 'web_fetch', permission: 'network', risk: 'medium', concurrencySafe: true, m1: true },
|
|
25
|
+
// α7.7: scratch worktree management. `worktree_create` writes nothing
|
|
26
|
+
// dangerous (a clone under `.pugi/worktrees/`); `worktree_promote`
|
|
27
|
+
// applies a diff back to the main tree, so it shares the `edit`
|
|
28
|
+
// risk class. `worktree_drop` is the cleanup primitive.
|
|
29
|
+
{ name: 'worktree_create', permission: 'edit', risk: 'low', concurrencySafe: false, m1: true },
|
|
30
|
+
{ name: 'worktree_drop', permission: 'edit', risk: 'low', concurrencySafe: false, m1: true },
|
|
31
|
+
{ name: 'worktree_promote', permission: 'edit', risk: 'medium', concurrencySafe: false, m1: true },
|
|
14
32
|
{ name: 'write', permission: 'edit', risk: 'medium', concurrencySafe: false, m1: true },
|
|
15
33
|
];
|
|
16
34
|
export const toolRegistry = registry.sort((a, b) => a.name.localeCompare(b.name));
|