@mnaimfaizy/agent-kit 1.0.0-alpha.12 → 1.0.0-beta.10
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 +14 -6
- package/bin/agent-kit.mjs +3 -1
- package/package.json +1 -1
- package/src/callers.mjs +73 -10
- package/src/checks/kit-owned-trees.mjs +47 -4
- package/src/checks/result.mjs +8 -2
- package/src/checks/review-response.mjs +205 -0
- package/src/checks/rulesets.mjs +7 -2
- package/src/checks/scan-recipe.mjs +56 -0
- package/src/checks/secrets-and-variables.mjs +65 -15
- package/src/cli.mjs +306 -19
- package/src/deep-scan/agent.mjs +410 -0
- package/src/deep-scan/claude-code.mjs +25 -0
- package/src/deep-scan/credential.mjs +215 -0
- package/src/deep-scan/docker-driver.mjs +696 -0
- package/src/deep-scan/egress.mjs +150 -0
- package/src/deep-scan/lifecycle.mjs +703 -0
- package/src/deep-scan/limits.mjs +126 -0
- package/src/deep-scan/model-proxy.mjs +378 -0
- package/src/deep-scan/preflight.mjs +78 -0
- package/src/deep-scan/recipe.mjs +447 -0
- package/src/deep-scan/relay-script.mjs +98 -0
- package/src/deep-scan/relay.mjs +130 -0
- package/src/deep-scan/runtime.mjs +181 -0
- package/src/deep-scan/sbx-driver.mjs +501 -0
- package/src/deep-scan/steps.mjs +47 -0
- package/src/deep-scan/tar.mjs +84 -0
- package/src/deep-scan/verify.mjs +272 -0
- package/src/deep-scan.mjs +474 -0
- package/src/doctor.mjs +57 -16
- package/src/exec.mjs +225 -23
- package/src/init.mjs +653 -0
- package/src/kit-owned-paths.mjs +5 -3
- package/src/kit-tag.mjs +175 -28
- package/src/local-tree.mjs +142 -27
- package/src/local-writes.mjs +278 -0
- package/src/prompt.mjs +42 -0
- package/src/safe-paths.mjs +68 -0
- package/src/tags.mjs +15 -5
- package/src/templates.mjs +137 -0
- package/src/upgrade.mjs +288 -0
- package/src/workflow-call.mjs +3 -5
- package/src/yaml-subset.mjs +134 -24
package/src/exec.mjs
CHANGED
|
@@ -1,36 +1,238 @@
|
|
|
1
|
-
// Runs
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// Runs commands as argument arrays, never through a shell, so no string from a
|
|
2
|
+
// Consumer file or a GitHub response is ever parsed as shell syntax. Each
|
|
3
|
+
// subcommand gets a runner that runs only the commands on its own allowlist:
|
|
4
|
+
// `init`, `upgrade` and `doctor` reach only `git` and `gh`, and `deep-scan`
|
|
5
|
+
// alone adds the accepted sandbox runtimes' CLIs and the 1Password CLI, which
|
|
6
|
+
// reads its model credential (ADR 0019). There is no
|
|
7
|
+
// second runner that bypasses this. Every module takes a runner as a
|
|
8
|
+
// parameter; tests inject a fake one, or a fake spawn behind the real
|
|
9
|
+
// allowlist. A runner also has `start`, the same command run without
|
|
10
|
+
// blocking (`deep-scan`'s long sandbox steps), so an interrupt can stop the
|
|
11
|
+
// step and the sandbox still be torn down; it checks the same allowlist.
|
|
4
12
|
|
|
5
|
-
import { spawnSync } from "node:child_process";
|
|
13
|
+
import { spawn as spawnChild, spawnSync } from "node:child_process";
|
|
6
14
|
|
|
7
|
-
|
|
15
|
+
/** The sandbox runtimes' command-line tools, which only `deep-scan` may run. */
|
|
16
|
+
export const RUNTIME_CLIS = Object.freeze([
|
|
17
|
+
"sbx",
|
|
18
|
+
"docker",
|
|
19
|
+
"runsc",
|
|
20
|
+
"kata-runtime",
|
|
21
|
+
"firecracker",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/** The secret managers' command-line tools, which only `deep-scan` may run. */
|
|
25
|
+
export const SECRET_MANAGER_CLIS = Object.freeze(["op"]);
|
|
26
|
+
|
|
27
|
+
const ALLOWLISTS = Object.freeze({
|
|
28
|
+
init: Object.freeze(["git", "gh"]),
|
|
29
|
+
upgrade: Object.freeze(["git", "gh"]),
|
|
30
|
+
doctor: Object.freeze(["git", "gh"]),
|
|
31
|
+
"deep-scan": Object.freeze(["git", ...RUNTIME_CLIS, ...SECRET_MANAGER_CLIS]),
|
|
32
|
+
});
|
|
8
33
|
|
|
9
34
|
/**
|
|
10
|
-
* @typedef {{ status: number | null, stdout: string, stderr: string }} RunResult
|
|
11
|
-
* @typedef {
|
|
35
|
+
* @typedef {{ status: number | null, stdout: string, stderr: string, code?: string }} RunResult
|
|
36
|
+
* @typedef {{ status: number | null, stdout: Buffer, stderr: string, code?: string }} BufferResult
|
|
37
|
+
* @typedef {{ status: number | null, signal: string | null, aborted: boolean, truncated?: boolean, stdout: Buffer, stderr: string }} StartResult
|
|
38
|
+
* `aborted` is true when `opts.signal` stopped the command; `truncated`
|
|
39
|
+
* when its output passed `opts.maxOutput`, and it was killed for it.
|
|
40
|
+
* @typedef {(cmd: string, args: string[], opts?: { cwd?: string, env?: NodeJS.ProcessEnv, stdin?: Buffer | string, signal?: AbortSignal, maxOutput?: number }) => Promise<StartResult>} Start
|
|
41
|
+
* @typedef {((cmd: string, args: string[], opts?: { cwd?: string, env?: NodeJS.ProcessEnv, encoding?: "utf8" | "buffer", maxBuffer?: number }) => RunResult) & { start: Start }} Runner
|
|
42
|
+
* With `encoding: "buffer"`, stdout is the raw bytes (a Buffer): use it for
|
|
43
|
+
* file contents, which need not be UTF-8. stderr is always text. `start`
|
|
44
|
+
* runs a command without blocking, feeds it `stdin`, and kills it when
|
|
45
|
+
* `signal` aborts.
|
|
12
46
|
*/
|
|
13
47
|
|
|
14
|
-
/**
|
|
15
|
-
export function
|
|
16
|
-
|
|
17
|
-
if (!
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
48
|
+
/** The commands a subcommand's runner may run. */
|
|
49
|
+
export function allowedCommands(subcommand) {
|
|
50
|
+
const list = Object.hasOwn(ALLOWLISTS, subcommand) ? ALLOWLISTS[subcommand] : null;
|
|
51
|
+
if (!list) throw new Error(`no process allowlist for ${JSON.stringify(subcommand)}`);
|
|
52
|
+
return list;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const MAX_OUTPUT = 64 * 1024 * 1024;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* How much of a child's error output is kept: its start and its end. Only
|
|
59
|
+
* the first line or the last lines are ever shown, and the output can come
|
|
60
|
+
* from untrusted code (a setup step), so it must not fill the heap.
|
|
61
|
+
*/
|
|
62
|
+
const MAX_ERR_HEAD = 64 * 1024;
|
|
63
|
+
const MAX_ERR_TAIL = 1024 * 1024;
|
|
64
|
+
const ERR_CUT = "\n[... error output cut ...]\n";
|
|
65
|
+
|
|
66
|
+
/** A collector for a child's error output that keeps its head and a tail ring. */
|
|
67
|
+
function errorCollector() {
|
|
68
|
+
const head = [];
|
|
69
|
+
let headSize = 0;
|
|
70
|
+
let tail = [];
|
|
71
|
+
let tailSize = 0;
|
|
72
|
+
let cut = false;
|
|
73
|
+
return {
|
|
74
|
+
push(b) {
|
|
75
|
+
if (headSize < MAX_ERR_HEAD) {
|
|
76
|
+
const take = b.subarray(0, MAX_ERR_HEAD - headSize);
|
|
77
|
+
head.push(take);
|
|
78
|
+
headSize += take.length;
|
|
79
|
+
b = b.subarray(take.length);
|
|
80
|
+
}
|
|
81
|
+
if (!b.length) return;
|
|
82
|
+
tail.push(b);
|
|
83
|
+
tailSize += b.length;
|
|
84
|
+
while (tail.length > 1 && tailSize - tail[0].length >= MAX_ERR_TAIL) {
|
|
85
|
+
tailSize -= tail.shift().length;
|
|
86
|
+
cut = true;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
text() {
|
|
90
|
+
let rest = Buffer.concat(tail);
|
|
91
|
+
if (rest.length > MAX_ERR_TAIL) {
|
|
92
|
+
rest = rest.subarray(rest.length - MAX_ERR_TAIL);
|
|
93
|
+
cut = true;
|
|
94
|
+
}
|
|
95
|
+
return `${Buffer.concat(head).toString("utf8")}${cut ? ERR_CUT : ""}${rest.toString("utf8")}`;
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The runner for one subcommand. `spawn` and `spawnAsync` are the process
|
|
102
|
+
* boundary (blocking and not), replaceable only by tests; the allowlist check
|
|
103
|
+
* always runs first.
|
|
104
|
+
* @param {string} subcommand
|
|
105
|
+
* @param {{ spawn?: typeof spawnSync, spawnAsync?: typeof spawnChild }} [opts]
|
|
106
|
+
* @returns {Runner}
|
|
107
|
+
*/
|
|
108
|
+
export function runnerFor(subcommand, { spawn = spawnSync, spawnAsync = spawnChild } = {}) {
|
|
109
|
+
return makeRunner(allowedCommands(subcommand), subcommand, spawn, spawnAsync);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function makeRunner(list, who, spawn, spawnAsync = spawnChild) {
|
|
113
|
+
const allowed = new Set(list);
|
|
114
|
+
const names = `${list.slice(0, -1).join(", ")} and ${list.at(-1)}`;
|
|
115
|
+
const check = (cmd, args) => {
|
|
116
|
+
if (!allowed.has(cmd)) {
|
|
117
|
+
throw new Error(`refusing to run ${cmd}: ${who} may run only ${names}`);
|
|
118
|
+
}
|
|
119
|
+
if (!Array.isArray(args) || args.some((a) => typeof a !== "string")) {
|
|
120
|
+
throw new TypeError("runner arguments must be an array of strings");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
const envFor = (opts) => ({
|
|
124
|
+
...process.env,
|
|
125
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
126
|
+
GH_PROMPT_DISABLED: "1",
|
|
127
|
+
...opts.env,
|
|
27
128
|
});
|
|
28
|
-
|
|
29
|
-
|
|
129
|
+
/** @type {Start} */
|
|
130
|
+
const start = (cmd, args, opts = {}) => {
|
|
131
|
+
check(cmd, args);
|
|
132
|
+
return new Promise((resolve) => {
|
|
133
|
+
let done = false;
|
|
134
|
+
let aborted = false;
|
|
135
|
+
const out = [];
|
|
136
|
+
const err = errorCollector();
|
|
137
|
+
let size = 0;
|
|
138
|
+
let truncated = false;
|
|
139
|
+
const cap = opts.maxOutput ?? MAX_OUTPUT;
|
|
140
|
+
const finish = (result) => {
|
|
141
|
+
if (done) return;
|
|
142
|
+
done = true;
|
|
143
|
+
opts.signal?.removeEventListener("abort", onAbort);
|
|
144
|
+
resolve({ aborted, ...(truncated ? { truncated } : {}), ...result });
|
|
145
|
+
};
|
|
146
|
+
let child;
|
|
147
|
+
const onAbort = () => {
|
|
148
|
+
aborted = true;
|
|
149
|
+
child?.kill("SIGTERM");
|
|
150
|
+
};
|
|
151
|
+
try {
|
|
152
|
+
child = spawnAsync(cmd, args, {
|
|
153
|
+
cwd: opts.cwd,
|
|
154
|
+
env: envFor(opts),
|
|
155
|
+
shell: false,
|
|
156
|
+
windowsHide: true,
|
|
157
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
158
|
+
});
|
|
159
|
+
} catch (e) {
|
|
160
|
+
finish({
|
|
161
|
+
status: null,
|
|
162
|
+
signal: null,
|
|
163
|
+
stdout: Buffer.alloc(0),
|
|
164
|
+
stderr: `${cmd}: ${e.message}`,
|
|
165
|
+
});
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
child.stdout.on("data", (b) => {
|
|
169
|
+
size += b.length;
|
|
170
|
+
if (size <= cap) out.push(b);
|
|
171
|
+
else if (opts.maxOutput !== undefined && !truncated) {
|
|
172
|
+
// Past the caller's cap: read no more of it.
|
|
173
|
+
truncated = true;
|
|
174
|
+
child.kill("SIGKILL");
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
child.stderr.on("data", (b) => err.push(b));
|
|
178
|
+
child.on("error", (e) =>
|
|
179
|
+
finish({
|
|
180
|
+
status: null,
|
|
181
|
+
signal: null,
|
|
182
|
+
stdout: Buffer.alloc(0),
|
|
183
|
+
stderr: `${cmd}: ${e.message}`,
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
child.on("close", (status, signal) =>
|
|
187
|
+
finish({
|
|
188
|
+
status,
|
|
189
|
+
signal,
|
|
190
|
+
stdout: Buffer.concat(out),
|
|
191
|
+
stderr: err.text(),
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
if (opts.signal?.aborted) onAbort();
|
|
195
|
+
else opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
196
|
+
child.stdin.on("error", () => {});
|
|
197
|
+
child.stdin.end(opts.stdin ?? undefined);
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
function run(cmd, args, opts = {}) {
|
|
201
|
+
check(cmd, args);
|
|
202
|
+
const result = spawn(cmd, args, {
|
|
203
|
+
cwd: opts.cwd,
|
|
204
|
+
env: envFor(opts),
|
|
205
|
+
encoding: "buffer",
|
|
206
|
+
shell: false,
|
|
207
|
+
windowsHide: true,
|
|
208
|
+
maxBuffer: opts.maxBuffer ?? MAX_OUTPUT,
|
|
209
|
+
});
|
|
210
|
+
const raw = opts.encoding === "buffer";
|
|
211
|
+
if (result.error) {
|
|
212
|
+
return {
|
|
213
|
+
status: null,
|
|
214
|
+
stdout: raw ? Buffer.alloc(0) : "",
|
|
215
|
+
stderr: `${cmd}: ${result.error.message}`,
|
|
216
|
+
...(result.error.code ? { code: result.error.code } : {}),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const stdout = result.stdout ?? Buffer.alloc(0);
|
|
220
|
+
return {
|
|
221
|
+
status: result.status,
|
|
222
|
+
stdout: raw ? stdout : stdout.toString("utf8"),
|
|
223
|
+
stderr: (result.stderr ?? Buffer.alloc(0)).toString("utf8"),
|
|
224
|
+
};
|
|
30
225
|
}
|
|
31
|
-
|
|
226
|
+
run.start = start;
|
|
227
|
+
return run;
|
|
32
228
|
}
|
|
33
229
|
|
|
230
|
+
/**
|
|
231
|
+
* A `git` and `gh` runner, the allowlist `init`, `upgrade` and `doctor` share.
|
|
232
|
+
* @type {Runner}
|
|
233
|
+
*/
|
|
234
|
+
export const systemRunner = makeRunner(["git", "gh"], "agent-kit", spawnSync);
|
|
235
|
+
|
|
34
236
|
/**
|
|
35
237
|
* The line of a command's stderr that says what went wrong, for short
|
|
36
238
|
* messages: the first `fatal:` or `error:` line, else the first line that is
|