@mnaimfaizy/agent-kit 1.0.0-beta.12 → 1.0.0-beta.14
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 +6 -5
- package/harnesses.json +48 -0
- package/package.json +3 -2
- package/src/checks/review-response.mjs +54 -27
- package/src/checks/secrets-and-variables.mjs +198 -40
- package/src/cli.mjs +9 -0
- package/src/deep-scan/agent.mjs +77 -116
- package/src/deep-scan/credential.mjs +32 -18
- package/src/deep-scan/docker-driver.mjs +35 -13
- package/src/deep-scan/grounding-url.mjs +185 -0
- package/src/deep-scan/lifecycle.mjs +51 -31
- package/src/deep-scan/model-proxy.mjs +36 -366
- package/src/deep-scan/preflight.mjs +68 -1
- package/src/deep-scan/recipe.mjs +25 -8
- package/src/deep-scan/sbx-driver.mjs +30 -20
- package/src/deep-scan.mjs +36 -11
- package/src/harness/claude-code.mjs +229 -0
- package/src/harness/facts.mjs +120 -0
- package/src/harness/install-layer.mjs +58 -0
- package/src/harness/support-levels.mjs +49 -0
- package/src/harness-live/assess.mjs +268 -0
- package/src/harness-live/host.mjs +305 -0
- package/src/harness-live/probes.mjs +159 -0
- package/src/harness-live/run.mjs +133 -0
- package/src/harness-live/stage.mjs +151 -0
- package/src/harness-live/suite.mjs +300 -0
- package/src/init.mjs +28 -12
- package/src/model-proxy/core.mjs +549 -0
- package/src/model-proxy/profiles/anthropic.mjs +197 -0
- package/src/model-proxy/stub-dialects/anthropic.mjs +110 -0
- package/src/model-proxy/stub.mjs +174 -0
- package/src/deep-scan/claude-code.mjs +0 -25
|
@@ -10,21 +10,41 @@
|
|
|
10
10
|
// gets an environment with the model credential variables removed, and the
|
|
11
11
|
// sandbox gets only a placeholder.
|
|
12
12
|
|
|
13
|
+
import { randomBytes } from "node:crypto";
|
|
13
14
|
import { firstLine } from "../exec.mjs";
|
|
15
|
+
import { claudeCode } from "../harness/claude-code.mjs";
|
|
14
16
|
import { GITHUB_TOKEN_VARS } from "./preflight.mjs";
|
|
15
17
|
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The credentials the harness accepts, from its launch plan: each kind's
|
|
20
|
+
* variable is also the one it is read from by default.
|
|
21
|
+
*/
|
|
22
|
+
const { accepted, scrub } = claudeCode().credentials;
|
|
18
23
|
|
|
19
|
-
/**
|
|
20
|
-
|
|
24
|
+
/**
|
|
25
|
+
* The start of the key the sandboxed agent holds in place of a real one;
|
|
26
|
+
* the proxy swaps it for the real key.
|
|
27
|
+
*/
|
|
28
|
+
export const PLACEHOLDER_API_KEY = accepted["api-key"].placeholder;
|
|
21
29
|
|
|
22
30
|
/**
|
|
23
|
-
* The
|
|
24
|
-
*
|
|
25
|
-
* like a token, in case Claude Code checks the prefix.
|
|
31
|
+
* The start of the token the sandboxed agent holds on the subscription
|
|
32
|
+
* path; the proxy swaps the bearer value it sends.
|
|
26
33
|
*/
|
|
27
|
-
export const PLACEHOLDER_OAUTH_TOKEN =
|
|
34
|
+
export const PLACEHOLDER_OAUTH_TOKEN = accepted.subscription.placeholder;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The run's caller key: a placeholder with 24 random bytes added, made once
|
|
38
|
+
* per run. The sandboxed Claude Code holds it in place of a credential, and
|
|
39
|
+
* the model proxy forwards only a request that carries it, so a process in
|
|
40
|
+
* the sandbox that never read Claude Code's environment cannot spend the
|
|
41
|
+
* real credential. It is worth nothing outside the run's proxy.
|
|
42
|
+
* @param {"api-key" | "subscription"} kind
|
|
43
|
+
*/
|
|
44
|
+
export function callerKeyFor(kind) {
|
|
45
|
+
const start = kind === SUBSCRIPTION ? PLACEHOLDER_OAUTH_TOKEN : PLACEHOLDER_API_KEY;
|
|
46
|
+
return `${start}-${randomBytes(24).toString("hex")}`;
|
|
47
|
+
}
|
|
28
48
|
|
|
29
49
|
/** The kinds `--credential` takes: an API key (the default) or the opt-in. */
|
|
30
50
|
export const API_KEY = "api-key";
|
|
@@ -32,14 +52,8 @@ export const SUBSCRIPTION = "subscription";
|
|
|
32
52
|
const CREDENTIAL_KINDS = Object.freeze([API_KEY, SUBSCRIPTION]);
|
|
33
53
|
|
|
34
54
|
/** Model credential variables, removed from every command the launcher runs. */
|
|
35
|
-
export const CREDENTIAL_VARS = Object.freeze(
|
|
36
|
-
|
|
37
|
-
"ANTHROPIC_AUTH_TOKEN",
|
|
38
|
-
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
39
|
-
]);
|
|
40
|
-
|
|
41
|
-
/** The variable Claude Code reads a subscription (OAuth) token from. */
|
|
42
|
-
const OAUTH_TOKEN_VAR = "CLAUDE_CODE_OAUTH_TOKEN";
|
|
55
|
+
export const CREDENTIAL_VARS = Object.freeze(scrub);
|
|
56
|
+
|
|
43
57
|
/** How a Claude subscription (OAuth) token starts; an API key starts sk-ant-api. */
|
|
44
58
|
const OAUTH_TOKEN_PREFIX = "sk-ant-oat";
|
|
45
59
|
|
|
@@ -72,7 +86,7 @@ export function credentialKind(value) {
|
|
|
72
86
|
*/
|
|
73
87
|
export function credentialSource(value, kind = API_KEY) {
|
|
74
88
|
if (value === undefined) {
|
|
75
|
-
return { kind: "env", name: kind
|
|
89
|
+
return { kind: "env", name: accepted[kind].env };
|
|
76
90
|
}
|
|
77
91
|
if (value.startsWith("env:")) {
|
|
78
92
|
const name = value.slice(4);
|
|
@@ -119,7 +133,7 @@ export function readCredential({ source, env, runner, kind = API_KEY }) {
|
|
|
119
133
|
);
|
|
120
134
|
}
|
|
121
135
|
const { name } = source;
|
|
122
|
-
if (!token && name.toUpperCase() ===
|
|
136
|
+
if (!token && name.toUpperCase() === accepted[SUBSCRIPTION].env) return notAnApiKey(name);
|
|
123
137
|
const command = `npx @mnaimfaizy/agent-kit deep-scan${token ? " --credential subscription" : ""}`;
|
|
124
138
|
const value = valueOf(env, name);
|
|
125
139
|
if (!value) {
|
|
@@ -32,8 +32,10 @@
|
|
|
32
32
|
// scan's only route to the internet.
|
|
33
33
|
|
|
34
34
|
import { isIPv4 } from "node:net";
|
|
35
|
-
import { layerDockerfile } from "
|
|
35
|
+
import { layerDockerfile, pinnedHarnessImage } from "../harness/install-layer.mjs";
|
|
36
36
|
import { startEgressProxy } from "./egress.mjs";
|
|
37
|
+
import { refuseMusl } from "./preflight.mjs";
|
|
38
|
+
import { imageDigestNote } from "./recipe.mjs";
|
|
37
39
|
import {
|
|
38
40
|
OTHER_HOST_PORT,
|
|
39
41
|
probeFailure,
|
|
@@ -60,10 +62,12 @@ export const ISOLATED_OPTIONS = Object.freeze([
|
|
|
60
62
|
]);
|
|
61
63
|
|
|
62
64
|
/**
|
|
63
|
-
* @param {{ runtime: import("./runtime.mjs").Runtime, id: string, call: Call }} opts
|
|
65
|
+
* @param {{ runtime: import("./runtime.mjs").Runtime, id: string, call: Call, plan: import("../harness/claude-code.mjs").LaunchPlan }} opts
|
|
66
|
+
* `plan` is the harness's launch plan: its install layer goes on the image.
|
|
64
67
|
* @returns {import("./lifecycle.mjs").Driver}
|
|
65
68
|
*/
|
|
66
|
-
export function dockerDriver({ runtime, id, call }) {
|
|
69
|
+
export function dockerDriver({ runtime, id, call, plan }) {
|
|
70
|
+
const { displayName } = plan.identity;
|
|
67
71
|
const name = `agent-kit-deep-${id}`;
|
|
68
72
|
const label = `${LABEL}=${id}`;
|
|
69
73
|
const docker = (args, opts) => call("docker", args, opts);
|
|
@@ -90,11 +94,26 @@ export function dockerDriver({ runtime, id, call }) {
|
|
|
90
94
|
let hostGateway = null;
|
|
91
95
|
/** The relay image, once built. */
|
|
92
96
|
let relayImage = null;
|
|
97
|
+
/** What the sandbox runs: the Harness image, or the run's layered image. */
|
|
98
|
+
let sandboxImage = null;
|
|
93
99
|
|
|
94
100
|
return {
|
|
95
101
|
container: name,
|
|
96
102
|
|
|
97
103
|
async prepare(steps, { archive, out, branch, short }) {
|
|
104
|
+
const harness = pinnedHarnessImage(plan.installLayer);
|
|
105
|
+
// No image of the recipe's: the sandbox runs the Harness image, which
|
|
106
|
+
// is not this run's to remove.
|
|
107
|
+
if (steps.dockerfile === undefined && steps.image === undefined) {
|
|
108
|
+
out(
|
|
109
|
+
`Image: pulling the Harness image ${harness}, which holds the pinned ${displayName}; the recipe names no image.`,
|
|
110
|
+
);
|
|
111
|
+
must(await docker(["pull", harness]), `pulling the Harness image ${harness}`, {
|
|
112
|
+
showOutput: true,
|
|
113
|
+
});
|
|
114
|
+
sandboxImage = harness;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
98
117
|
let base;
|
|
99
118
|
if (steps.dockerfile !== undefined) {
|
|
100
119
|
out(`Image: building ${steps.dockerfile} from ${branch} (${short}).`);
|
|
@@ -112,17 +131,20 @@ export function dockerDriver({ runtime, id, call }) {
|
|
|
112
131
|
} else {
|
|
113
132
|
base = steps.image;
|
|
114
133
|
out(`Image: pulling ${base}.`);
|
|
134
|
+
const note = imageDigestNote(base);
|
|
135
|
+
if (note) out(note);
|
|
115
136
|
must(await docker(["pull", base]), `pulling ${base}`, { showOutput: true });
|
|
116
137
|
}
|
|
117
|
-
out(
|
|
118
|
-
|
|
119
|
-
must(
|
|
120
|
-
await docker(["build", "--label", label, "--tag", name, "-"], {
|
|
121
|
-
stdin: layerDockerfile(base),
|
|
122
|
-
}),
|
|
123
|
-
`layering Claude Code on ${base}`,
|
|
124
|
-
{ showOutput: true },
|
|
138
|
+
out(
|
|
139
|
+
`Image: layering the pinned ${displayName}, auto-updater off, from the Harness image ${harness}.`,
|
|
125
140
|
);
|
|
141
|
+
resources.push(["image", name]);
|
|
142
|
+
const layered = await docker(["build", "--label", label, "--tag", name, "-"], {
|
|
143
|
+
stdin: layerDockerfile(base, plan.installLayer),
|
|
144
|
+
});
|
|
145
|
+
refuseMusl(layered, steps);
|
|
146
|
+
must(layered, `layering ${displayName} on ${base}`, { showOutput: true });
|
|
147
|
+
sandboxImage = name;
|
|
126
148
|
},
|
|
127
149
|
|
|
128
150
|
/**
|
|
@@ -161,7 +183,7 @@ export function dockerDriver({ runtime, id, call }) {
|
|
|
161
183
|
"/workspace",
|
|
162
184
|
"--entrypoint",
|
|
163
185
|
"tail",
|
|
164
|
-
|
|
186
|
+
sandboxImage,
|
|
165
187
|
"-f",
|
|
166
188
|
"/dev/null",
|
|
167
189
|
]),
|
|
@@ -190,7 +212,7 @@ export function dockerDriver({ runtime, id, call }) {
|
|
|
190
212
|
);
|
|
191
213
|
},
|
|
192
214
|
|
|
193
|
-
/**
|
|
215
|
+
/** The harness is already in the image. */
|
|
194
216
|
async installAgent() {},
|
|
195
217
|
|
|
196
218
|
/** The setup egress was opened with the network: the proxy's variables. */
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// Grounding URLs are fetched on the host, by `deep-scan`, and the body is
|
|
2
|
+
// copied into the sandbox, which treats everything in it as untrusted. So a
|
|
3
|
+
// grounding fetch must reach only what the sandbox could reach on the public
|
|
4
|
+
// web: an `https:` page on a public host, by its DNS name, with no redirect
|
|
5
|
+
// (a redirect could name any address). Each address the name resolves to is
|
|
6
|
+
// checked, and the connection goes to the address that was checked, so a DNS
|
|
7
|
+
// answer that changes between the check and the connection cannot swap it.
|
|
8
|
+
//
|
|
9
|
+
// Node core only (ADR 0001): `node:https` with a checking `lookup`, since the
|
|
10
|
+
// global fetch takes no lookup of its own.
|
|
11
|
+
|
|
12
|
+
import { lookup as dnsLookup } from "node:dns";
|
|
13
|
+
import { Agent, request } from "node:https";
|
|
14
|
+
import { BlockList, isIP } from "node:net";
|
|
15
|
+
import { Readable } from "node:stream";
|
|
16
|
+
|
|
17
|
+
/** A grounding entry with a scheme is a URL; anything else is a repository path. */
|
|
18
|
+
const SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//;
|
|
19
|
+
|
|
20
|
+
/** True when a grounding entry is a URL to fetch, not a repository path. */
|
|
21
|
+
export function isGroundingUrl(source) {
|
|
22
|
+
return SCHEME.test(String(source));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What is wrong with a grounding URL as written, or null when it may be
|
|
27
|
+
* fetched: it must be `https:`, and name a fully qualified host by its DNS
|
|
28
|
+
* name (no IP literal, single-label name or localhost).
|
|
29
|
+
*/
|
|
30
|
+
export function groundingUrlProblem(source) {
|
|
31
|
+
let url;
|
|
32
|
+
try {
|
|
33
|
+
url = new URL(String(source));
|
|
34
|
+
} catch {
|
|
35
|
+
return "is not a valid URL";
|
|
36
|
+
}
|
|
37
|
+
if (url.protocol !== "https:") return "must be an https:// URL";
|
|
38
|
+
// WHATWG URL parsing has already turned 0x7f.1, 2130706433 and similar
|
|
39
|
+
// into dotted IPv4, and keeps an IPv6 literal in brackets.
|
|
40
|
+
const host = url.hostname;
|
|
41
|
+
if (host.startsWith("[") || isIP(host)) {
|
|
42
|
+
return "must name its host by a DNS name, not an IP address";
|
|
43
|
+
}
|
|
44
|
+
const name = host.replace(/\.$/, "").toLowerCase();
|
|
45
|
+
if (name === "localhost" || name.endsWith(".localhost")) {
|
|
46
|
+
return "must name a public host, not localhost";
|
|
47
|
+
}
|
|
48
|
+
if (!name.includes("."))
|
|
49
|
+
return "must name a fully qualified public host, not a single-label name";
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Addresses a grounding fetch never connects to: unspecified, loopback,
|
|
54
|
+
// private, shared (CGNAT), link-local (cloud metadata among them), the
|
|
55
|
+
// documentation and benchmarking ranges, multicast and reserved; and the IPv6
|
|
56
|
+
// forms that embed an IPv4 address (mapped, NAT64, 6to4, Teredo), so none can
|
|
57
|
+
// carry a private one through. One list per family: a BlockList matches an
|
|
58
|
+
// IPv4 address against IPv6 rules by its mapped form, so `::ffff:0:0/96` in a
|
|
59
|
+
// shared list would refuse every IPv4 address.
|
|
60
|
+
const NOT_PUBLIC_V4 = new BlockList();
|
|
61
|
+
const NOT_PUBLIC_V6 = new BlockList();
|
|
62
|
+
for (const [net, prefix] of [
|
|
63
|
+
["0.0.0.0", 8],
|
|
64
|
+
["10.0.0.0", 8],
|
|
65
|
+
["100.64.0.0", 10],
|
|
66
|
+
["127.0.0.0", 8],
|
|
67
|
+
["169.254.0.0", 16],
|
|
68
|
+
["172.16.0.0", 12],
|
|
69
|
+
["192.0.0.0", 24],
|
|
70
|
+
["192.0.2.0", 24],
|
|
71
|
+
["192.88.99.0", 24],
|
|
72
|
+
["192.168.0.0", 16],
|
|
73
|
+
["198.18.0.0", 15],
|
|
74
|
+
["198.51.100.0", 24],
|
|
75
|
+
["203.0.113.0", 24],
|
|
76
|
+
["224.0.0.0", 4],
|
|
77
|
+
["240.0.0.0", 4],
|
|
78
|
+
]) {
|
|
79
|
+
NOT_PUBLIC_V4.addSubnet(net, prefix, "ipv4");
|
|
80
|
+
}
|
|
81
|
+
for (const [net, prefix] of [
|
|
82
|
+
["::", 128],
|
|
83
|
+
["::1", 128],
|
|
84
|
+
["::ffff:0:0", 96],
|
|
85
|
+
["64:ff9b::", 96],
|
|
86
|
+
["64:ff9b:1::", 48],
|
|
87
|
+
["100::", 64],
|
|
88
|
+
["2001::", 32],
|
|
89
|
+
["2001:db8::", 32],
|
|
90
|
+
["2002::", 16],
|
|
91
|
+
["fc00::", 7],
|
|
92
|
+
["fe80::", 10],
|
|
93
|
+
["fec0::", 10],
|
|
94
|
+
["ff00::", 8],
|
|
95
|
+
]) {
|
|
96
|
+
NOT_PUBLIC_V6.addSubnet(net, prefix, "ipv6");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** True when `address` is a public unicast IP address. */
|
|
100
|
+
export function isPublicAddress(address) {
|
|
101
|
+
const family = isIP(String(address));
|
|
102
|
+
if (family === 0) return false;
|
|
103
|
+
try {
|
|
104
|
+
if (family === 4) return !NOT_PUBLIC_V4.check(String(address), "ipv4");
|
|
105
|
+
return !NOT_PUBLIC_V6.check(String(address), "ipv6");
|
|
106
|
+
} catch {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A `lookup` for sockets that resolves as `dns.lookup` does, and fails when
|
|
113
|
+
* any address the name resolves to is not public. The socket then connects
|
|
114
|
+
* only to the addresses returned here.
|
|
115
|
+
*/
|
|
116
|
+
export function publicLookup(hostname, options, callback) {
|
|
117
|
+
const opts = typeof options === "function" ? {} : (options ?? {});
|
|
118
|
+
const done = typeof options === "function" ? options : callback;
|
|
119
|
+
dnsLookup(hostname, { ...opts, all: true }, (error, addresses) => {
|
|
120
|
+
if (error) return done(error);
|
|
121
|
+
const list = Array.isArray(addresses) ? addresses : [];
|
|
122
|
+
if (list.length === 0) {
|
|
123
|
+
return done(Object.assign(new Error(`${hostname} has no address`), { code: "ENOTFOUND" }));
|
|
124
|
+
}
|
|
125
|
+
const blocked = list.find((a) => !isPublicAddress(a.address));
|
|
126
|
+
if (blocked) {
|
|
127
|
+
return done(
|
|
128
|
+
Object.assign(
|
|
129
|
+
new Error(`${hostname} resolves to ${blocked.address}, which is not a public address`),
|
|
130
|
+
{ code: "ENOTPUBLIC" },
|
|
131
|
+
),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (opts.all) return done(null, list);
|
|
135
|
+
return done(null, list[0].address, list[0].family);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Its own agent: no keep-alive, and no proxy from the environment.
|
|
140
|
+
const AGENT = new Agent({ keepAlive: false });
|
|
141
|
+
const NULL_BODY = new Set([204, 205, 304]);
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The fetch `deep-scan` uses for grounding URLs: one GET, over `https:`
|
|
145
|
+
* only, to a public address checked by `publicLookup`, with no redirect
|
|
146
|
+
* followed. It answers with a `Response`, as the global fetch does, so the
|
|
147
|
+
* caller reads it the same way. `init.signal` stops it.
|
|
148
|
+
*/
|
|
149
|
+
export function publicHttpsFetch(url, init = {}) {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const problem = groundingUrlProblem(url);
|
|
152
|
+
if (problem) {
|
|
153
|
+
reject(new Error(`the URL ${problem}`));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const req = request(
|
|
157
|
+
url,
|
|
158
|
+
{
|
|
159
|
+
method: "GET",
|
|
160
|
+
agent: AGENT,
|
|
161
|
+
lookup: publicLookup,
|
|
162
|
+
signal: init.signal,
|
|
163
|
+
headers: { accept: "*/*", "user-agent": "agent-kit-deep-scan" },
|
|
164
|
+
},
|
|
165
|
+
(res) => {
|
|
166
|
+
try {
|
|
167
|
+
const headers = new Headers();
|
|
168
|
+
for (const [name, value] of Object.entries(res.headers)) {
|
|
169
|
+
for (const one of [value].flat()) if (one !== undefined) headers.append(name, one);
|
|
170
|
+
}
|
|
171
|
+
const status = res.statusCode ?? 0;
|
|
172
|
+
let body = null;
|
|
173
|
+
if (NULL_BODY.has(status)) res.resume();
|
|
174
|
+
else body = Readable.toWeb(res);
|
|
175
|
+
resolve(new Response(body, { status, headers }));
|
|
176
|
+
} catch (error) {
|
|
177
|
+
res.destroy();
|
|
178
|
+
reject(error);
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
req.on("error", reject);
|
|
183
|
+
req.end();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// The Deep scan sandbox lifecycle (#369), from image to teardown, on the
|
|
2
|
-
// runtime `selectRuntime` chose. In order: prepare the image (
|
|
2
|
+
// runtime `selectRuntime` chose. In order: prepare the image (the Harness
|
|
3
|
+
// image, or the recipe's with the harness copied from it, ADR 0029: a pinned
|
|
3
4
|
// Claude Code, its auto-updater off); start the sandbox with no mount; copy
|
|
4
5
|
// in an export of the trusted ref (never the working tree) and the grounding
|
|
5
6
|
// files, and nothing else; run the setup phase (`setup`, `start`, `ready`,
|
|
@@ -29,9 +30,10 @@ import { mkdtempSync, writeFileSync } from "node:fs";
|
|
|
29
30
|
import { tmpdir } from "node:os";
|
|
30
31
|
import { join } from "node:path";
|
|
31
32
|
import { firstLine } from "../exec.mjs";
|
|
32
|
-
import { agentBrief,
|
|
33
|
-
import {
|
|
33
|
+
import { agentBrief, agentPlan, agentScript, BRIEF_PATH, turnsMarker } from "./agent.mjs";
|
|
34
|
+
import { callerKeyFor } from "./credential.mjs";
|
|
34
35
|
import { dockerDriver, LABEL } from "./docker-driver.mjs";
|
|
36
|
+
import { groundingUrlProblem, isGroundingUrl } from "./grounding-url.mjs";
|
|
35
37
|
import {
|
|
36
38
|
LimitReached,
|
|
37
39
|
STOPPED_EXIT,
|
|
@@ -47,7 +49,7 @@ import { ENDS_HERE, indent, Interrupted, outputLines, StepFailed } from "./steps
|
|
|
47
49
|
import { tarOf } from "./tar.mjs";
|
|
48
50
|
import { checkVerificationReport, FINDINGS_PATH, verificationBrief } from "./verify.mjs";
|
|
49
51
|
|
|
50
|
-
export {
|
|
52
|
+
export { LABEL };
|
|
51
53
|
|
|
52
54
|
/** Where the sandbox holds the repository, the grounding and the report. */
|
|
53
55
|
export const WORKSPACE = "/workspace";
|
|
@@ -64,9 +66,6 @@ export const MAX_GROUNDING_BYTES = 10 * 1024 * 1024;
|
|
|
64
66
|
export const MAX_EXPORT_BYTES = 1024 * 1024 * 1024;
|
|
65
67
|
const GROUNDING_TIMEOUT_MS = 60_000;
|
|
66
68
|
|
|
67
|
-
/** What the Agent step line says for a model or effort not asked for. */
|
|
68
|
-
const CLAUDE_DEFAULT = "Claude Code's default";
|
|
69
|
-
|
|
70
69
|
/** Why a line is withheld from a world-readable log. */
|
|
71
70
|
const PUBLIC_LOG = "the Public-log rule: this log is world-readable in GitHub Actions";
|
|
72
71
|
|
|
@@ -85,7 +84,7 @@ function credentialRefusedLine(kind, from) {
|
|
|
85
84
|
return `The model API refused the credential: every answer was 401 or 403, so ${what}`;
|
|
86
85
|
}
|
|
87
86
|
|
|
88
|
-
/** How many lines of
|
|
87
|
+
/** How many lines of the harness's error output a failed agent step shows. */
|
|
89
88
|
const AGENT_ERROR_LINES = 20;
|
|
90
89
|
|
|
91
90
|
/** The setup phase's fields, in the order they run: services before seeding. */
|
|
@@ -223,8 +222,12 @@ export async function runLifecycle(opts) {
|
|
|
223
222
|
return r;
|
|
224
223
|
};
|
|
225
224
|
const id = randomBytes(6).toString("hex");
|
|
225
|
+
// The harness's launch plan: the drivers put its install layer on the image.
|
|
226
|
+
const plan = agentPlan({ kind: opts.credentialKind });
|
|
226
227
|
const driver =
|
|
227
|
-
runtime.id === "sbx"
|
|
228
|
+
runtime.id === "sbx"
|
|
229
|
+
? sbxDriver({ id, call, plan })
|
|
230
|
+
: dockerDriver({ runtime, id, call, plan });
|
|
228
231
|
|
|
229
232
|
let code;
|
|
230
233
|
let thrown = null;
|
|
@@ -316,7 +319,7 @@ async function lifecycle({
|
|
|
316
319
|
return r.stdout;
|
|
317
320
|
};
|
|
318
321
|
|
|
319
|
-
// 1. Image, and its pinned
|
|
322
|
+
// 1. Image, and its pinned harness (or the runtime's refusal).
|
|
320
323
|
await driver.prepare(steps, { archive, out, branch, short });
|
|
321
324
|
|
|
322
325
|
// 2. The sandbox: no mount, no host environment, no Docker socket.
|
|
@@ -340,7 +343,7 @@ async function lifecycle({
|
|
|
340
343
|
const grounding = [];
|
|
341
344
|
for (const [i, source] of steps.grounding.entries()) {
|
|
342
345
|
const where = `grounding[${i}]`;
|
|
343
|
-
if (
|
|
346
|
+
if (isGroundingUrl(source)) {
|
|
344
347
|
const path = `agent-kit/grounding/urls/${i}-${basename(source)}`;
|
|
345
348
|
fetched.push({
|
|
346
349
|
path,
|
|
@@ -420,12 +423,16 @@ async function lifecycle({
|
|
|
420
423
|
|
|
421
424
|
// 6. The model route: the key-injecting model proxy on this machine, the
|
|
422
425
|
// scan's only way out, and the check from the host that nothing else is.
|
|
426
|
+
// The run's caller key: the sandboxed Claude Code holds it, and the proxy
|
|
427
|
+
// forwards only a request that carries it.
|
|
428
|
+
const callerKey = callerKeyFor(credentialKind);
|
|
423
429
|
let proxy = null;
|
|
424
430
|
const route = await driver.openModelRoute(async (bind) => {
|
|
425
431
|
proxy = await startModelProxy({
|
|
426
432
|
credential,
|
|
427
433
|
kind: credentialKind,
|
|
428
434
|
bind,
|
|
435
|
+
callerKey,
|
|
429
436
|
maxTokens: limits.tokens,
|
|
430
437
|
onCap: () => clocks.stop(TOKENS, `the run's ${limits.tokens}-token cap`),
|
|
431
438
|
...(modelApi !== undefined ? { upstream: modelApi } : {}),
|
|
@@ -441,7 +448,7 @@ async function lifecycle({
|
|
|
441
448
|
`Model route: only the model proxy on your machine (${route.note}), which forwards to the model API alone, with your ${credentialKind === "subscription" ? "token as its bearer value" : "key"}; checked from the host.`,
|
|
442
449
|
);
|
|
443
450
|
|
|
444
|
-
// 7. The agent step:
|
|
451
|
+
// 7. The agent step: the harness with a placeholder key, handed its brief.
|
|
445
452
|
// A Verification run's finding file goes in with it, after the setup phase
|
|
446
453
|
// and the cut, so no setup step, which has registry egress, can read it.
|
|
447
454
|
const paths = { workspace: WORKSPACE, grounding: GROUNDING_DIR, report: REPORT_PATH };
|
|
@@ -481,8 +488,19 @@ async function lifecycle({
|
|
|
481
488
|
? "copying the launcher's brief and the finding file in"
|
|
482
489
|
: "copying the launcher's brief in",
|
|
483
490
|
);
|
|
491
|
+
const plan = agentPlan({
|
|
492
|
+
baseUrl: route.url,
|
|
493
|
+
maxTurns: limits.turns,
|
|
494
|
+
model,
|
|
495
|
+
effort,
|
|
496
|
+
kind: credentialKind,
|
|
497
|
+
callerKey,
|
|
498
|
+
});
|
|
499
|
+
const { displayName, version } = plan.identity;
|
|
500
|
+
/** What the line says for a model or effort not asked for. */
|
|
501
|
+
const unasked = `${displayName}'s default`;
|
|
484
502
|
out(
|
|
485
|
-
`Agent step:
|
|
503
|
+
`Agent step: ${displayName} ${version} (${subscription ? "no --bare: the subscription path, with a placeholder token; project settings and MCP servers not loaded" : "--bare, a placeholder key"}, web tools off, at most ${limits.turns} turns; requested model: ${model ?? unasked}, effort: ${effort ?? unasked}), handed ${verification ? "the Verification-run procedure, the finding file" : "the deep procedure"}, the Threat pack and the Scan recipe.`,
|
|
486
504
|
);
|
|
487
505
|
clocks.end();
|
|
488
506
|
// The scan wall clock: the agent step alone.
|
|
@@ -490,25 +508,16 @@ async function lifecycle({
|
|
|
490
508
|
/** @type {import("../exec.mjs").StartResult | null} null when a limit stopped it */
|
|
491
509
|
let agent = null;
|
|
492
510
|
try {
|
|
493
|
-
agent = await driver.exec(
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
report: REPORT_PATH,
|
|
497
|
-
workspace: WORKSPACE,
|
|
498
|
-
maxTurns: limits.turns,
|
|
499
|
-
...(model !== undefined ? { model } : {}),
|
|
500
|
-
...(effort !== undefined ? { effort } : {}),
|
|
501
|
-
kind: credentialKind,
|
|
502
|
-
}),
|
|
503
|
-
{ env: agentEnv(credentialKind) },
|
|
504
|
-
);
|
|
511
|
+
agent = await driver.exec(agentScript({ plan, report: REPORT_PATH, workspace: WORKSPACE }), {
|
|
512
|
+
env: plan.launch.env,
|
|
513
|
+
});
|
|
505
514
|
} catch (e) {
|
|
506
515
|
if (!(e instanceof LimitReached)) throw e;
|
|
507
516
|
}
|
|
508
517
|
clocks.end();
|
|
509
|
-
//
|
|
510
|
-
if (agent && agent.stdout.toString("utf8").split(/\r?\n/).includes(
|
|
511
|
-
clocks.stop(TURNS,
|
|
518
|
+
// The harness's own turn limit ended it: the script's fixed marker says so.
|
|
519
|
+
if (agent && agent.stdout.toString("utf8").split(/\r?\n/).includes(turnsMarker(plan))) {
|
|
520
|
+
clocks.stop(TURNS, `${displayName}'s ${limits.turns}-turn limit`);
|
|
512
521
|
}
|
|
513
522
|
// The scan is over: no route out is left open while the report comes out.
|
|
514
523
|
await driver.close();
|
|
@@ -529,7 +538,7 @@ async function lifecycle({
|
|
|
529
538
|
out(`${stoppedStatus(stopped)} Copying out what report the agent left.`);
|
|
530
539
|
} else if (agent && agent.status !== 0 && publicLog) {
|
|
531
540
|
err(
|
|
532
|
-
`The agent step exited ${agent.status ?? "abnormally"}; copying out what report it left.
|
|
541
|
+
`The agent step exited ${agent.status ?? "abnormally"}; copying out what report it left. ${displayName}'s error output stays in the sandbox (${PUBLIC_LOG}); run deep-scan locally to see it.`,
|
|
533
542
|
);
|
|
534
543
|
} else if (agent && agent.status !== 0) {
|
|
535
544
|
const lines = agent.stderr
|
|
@@ -541,7 +550,7 @@ async function lifecycle({
|
|
|
541
550
|
`The agent step exited ${agent.status ?? "abnormally"}; copying out what report it left.`,
|
|
542
551
|
...(lines.length
|
|
543
552
|
? [
|
|
544
|
-
|
|
553
|
+
`${displayName}'s error output (its last ${AGENT_ERROR_LINES} lines at most; the rest of its output stays in the sandbox):`,
|
|
545
554
|
...indent(lines.slice(-AGENT_ERROR_LINES).join("\n")),
|
|
546
555
|
]
|
|
547
556
|
: []),
|
|
@@ -651,13 +660,24 @@ async function fetchGrounding(fetch, url, where, clocks) {
|
|
|
651
660
|
const fail = (why) =>
|
|
652
661
|
new StepFailed([`FAILED ${where}: could not fetch ${url}: ${why}`, ENDS_HERE]);
|
|
653
662
|
const tooLarge = `larger than ${MAX_GROUNDING_BYTES / 1024 / 1024} MiB`;
|
|
663
|
+
// The recipe check refuses these first; this holds for any caller.
|
|
664
|
+
const problem = groundingUrlProblem(url);
|
|
665
|
+
if (problem) throw fail(`the URL ${problem}`);
|
|
654
666
|
// The request and the body read share one error path: an interrupt or a
|
|
655
667
|
// limit that fires mid-download stops the run as it would anywhere else.
|
|
656
668
|
try {
|
|
657
669
|
const res = await fetch(url, {
|
|
658
|
-
redirect
|
|
670
|
+
// A redirect could name any address, so none is followed.
|
|
671
|
+
redirect: "manual",
|
|
659
672
|
signal: AbortSignal.any([clocks.signal(), AbortSignal.timeout(GROUNDING_TIMEOUT_MS)]),
|
|
660
673
|
});
|
|
674
|
+
if (res.status >= 300 && res.status < 400) {
|
|
675
|
+
res.body?.cancel().catch(() => {});
|
|
676
|
+
const location = res.headers.get("location");
|
|
677
|
+
throw fail(
|
|
678
|
+
`it redirects (HTTP ${res.status}${location ? ` to ${location}` : ""}); name the final https:// URL in the recipe`,
|
|
679
|
+
);
|
|
680
|
+
}
|
|
661
681
|
if (!res.ok) throw fail(`HTTP ${res.status}`);
|
|
662
682
|
if (Number(res.headers.get("content-length") ?? "0") > MAX_GROUNDING_BYTES) {
|
|
663
683
|
throw fail(tooLarge);
|