@foldrun/core 1.0.0 → 1.1.1
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/dist/src/completions.js +1 -0
- package/dist/src/deploy.js +5 -0
- package/dist/src/egress.d.ts +21 -0
- package/dist/src/egress.js +8 -0
- package/dist/src/network.d.ts +120 -0
- package/dist/src/network.js +360 -0
- package/dist/src/run-container.d.ts +8 -0
- package/dist/src/run-container.js +2 -0
- package/dist/src/runner.js +128 -16
- package/dist/src/secrets.d.ts +20 -0
- package/dist/src/secrets.js +65 -1
- package/dist/src/starter.js +7 -1
- package/dist/src/store.d.ts +12 -0
- package/dist/src/store.js +32 -1
- package/dist/src/tool-loop.d.ts +45 -0
- package/dist/src/tool-loop.js +133 -0
- package/package.json +2 -1
- package/src/completions.ts +1 -0
- package/src/deploy.ts +4 -0
- package/src/egress.ts +26 -0
- package/src/network.ts +390 -0
- package/src/run-container.ts +6 -0
- package/src/runner.ts +130 -14
- package/src/secrets.ts +69 -1
- package/src/starter.ts +7 -1
- package/src/store.ts +35 -1
- package/src/tool-loop.ts +176 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// A model with tools that live in this process, and nothing else.
|
|
2
|
+
//
|
|
3
|
+
// What the platform's assistant needs (and what the eval judge and the flow
|
|
4
|
+
// draft are a toolless special case of): one Agent SDK query whose ONLY
|
|
5
|
+
// tools are the ones the caller passes — no built-in tool, no file system,
|
|
6
|
+
// no shell, no sub-agent, no MCP server the machine happens to have — run in
|
|
7
|
+
// a throwaway directory, inside a turn, dollar and wall-clock ceiling.
|
|
8
|
+
//
|
|
9
|
+
// The tools run here, in the caller's process, so whatever they read they
|
|
10
|
+
// read with the caller's own scoping; the model only ever sees what a tool
|
|
11
|
+
// returns. That is why this needs no sandbox pod: nothing the model asks for
|
|
12
|
+
// is executed as code.
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import os from "node:os";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { query, createSdkMcpServer, tool } from "@anthropic-ai/claude-agent-sdk";
|
|
18
|
+
import { hostSafeEnv } from "./host-env.js";
|
|
19
|
+
const SERVER = "foldrun_tools";
|
|
20
|
+
/** The Agent SDK's name for one of our tools. */
|
|
21
|
+
export const loopToolName = (name) => `mcp__${SERVER}__${name}`;
|
|
22
|
+
function schemaOf(args) {
|
|
23
|
+
return Object.fromEntries(Object.entries(args).map(([name, a]) => {
|
|
24
|
+
let s = z.string().min(a.optional ? 0 : 1);
|
|
25
|
+
if (a.max)
|
|
26
|
+
s = s.max(a.max);
|
|
27
|
+
if (a.description)
|
|
28
|
+
s = s.describe(a.description);
|
|
29
|
+
return [name, a.optional ? s.optional() : s];
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
export async function runToolLoop(o) {
|
|
33
|
+
const server = createSdkMcpServer({
|
|
34
|
+
name: SERVER,
|
|
35
|
+
version: "1.0.0",
|
|
36
|
+
tools: o.tools.map((t) => tool(t.name, t.description, schemaOf(t.args), async (args) => {
|
|
37
|
+
try {
|
|
38
|
+
const plain = Object.fromEntries(Object.entries(args).map(([k, v]) => [k, v === undefined ? undefined : String(v)]));
|
|
39
|
+
return { content: [{ type: "text", text: await t.run(plain) }] };
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
return { content: [{ type: "text", text: `error: ${err instanceof Error ? err.message : String(err)}` }], isError: true };
|
|
43
|
+
}
|
|
44
|
+
})),
|
|
45
|
+
});
|
|
46
|
+
// Homeless on purpose: there are no file tools, and a throwaway cwd means
|
|
47
|
+
// even a confused call has no tree to touch.
|
|
48
|
+
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "foldrun-loop-"));
|
|
49
|
+
const abort = new AbortController();
|
|
50
|
+
let killed = false;
|
|
51
|
+
const watchdog = setTimeout(() => {
|
|
52
|
+
killed = true;
|
|
53
|
+
abort.abort();
|
|
54
|
+
}, o.wallSeconds * 1000);
|
|
55
|
+
watchdog.unref?.();
|
|
56
|
+
let text = "";
|
|
57
|
+
let costUsd = 0;
|
|
58
|
+
let usage = null;
|
|
59
|
+
let stopped = null;
|
|
60
|
+
try {
|
|
61
|
+
const q = query({
|
|
62
|
+
prompt: o.prompt,
|
|
63
|
+
options: {
|
|
64
|
+
model: o.model,
|
|
65
|
+
systemPrompt: o.system,
|
|
66
|
+
// No built-in tool at all (no Task, so no sub-agents either) — only
|
|
67
|
+
// ours, each pre-approved; anything else is refused unasked.
|
|
68
|
+
tools: [],
|
|
69
|
+
mcpServers: { [SERVER]: server },
|
|
70
|
+
strictMcpConfig: true,
|
|
71
|
+
allowedTools: o.tools.map((t) => loopToolName(t.name)),
|
|
72
|
+
permissionMode: "dontAsk",
|
|
73
|
+
settingSources: [],
|
|
74
|
+
persistSession: false,
|
|
75
|
+
maxTurns: o.maxTurns,
|
|
76
|
+
maxBudgetUsd: o.maxBudgetUsd,
|
|
77
|
+
includePartialMessages: true,
|
|
78
|
+
cwd: scratch,
|
|
79
|
+
abortController: abort,
|
|
80
|
+
env: { ...hostSafeEnv(), ...o.env, CLAUDE_CODE_MAX_OUTPUT_TOKENS: String(o.maxOutputTokens) },
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
let turn = "";
|
|
84
|
+
for await (const message of q) {
|
|
85
|
+
if (message.type === "stream_event") {
|
|
86
|
+
const e = message.event;
|
|
87
|
+
if (e.type === "message_start")
|
|
88
|
+
turn = "";
|
|
89
|
+
if (e.type === "content_block_delta" && e.delta.type === "text_delta") {
|
|
90
|
+
turn += e.delta.text;
|
|
91
|
+
o.onText?.(e.delta.text);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (message.type === "assistant") {
|
|
95
|
+
const said = message.message.content
|
|
96
|
+
.filter((b) => b.type === "text")
|
|
97
|
+
.map((b) => b.text)
|
|
98
|
+
.join("");
|
|
99
|
+
if (said.trim())
|
|
100
|
+
text = said;
|
|
101
|
+
}
|
|
102
|
+
else if (message.type === "result") {
|
|
103
|
+
costUsd = message.total_cost_usd ?? 0;
|
|
104
|
+
const models = Object.values(message.modelUsage ?? {});
|
|
105
|
+
usage = {
|
|
106
|
+
inputTokens: models.reduce((n, m) => n + (m.inputTokens ?? 0) + (m.cacheReadInputTokens ?? 0) + (m.cacheCreationInputTokens ?? 0), 0),
|
|
107
|
+
outputTokens: models.reduce((n, m) => n + (m.outputTokens ?? 0), 0),
|
|
108
|
+
costUsd,
|
|
109
|
+
};
|
|
110
|
+
if (message.subtype === "success") {
|
|
111
|
+
if (message.result?.trim())
|
|
112
|
+
text = message.result;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
stopped = message.subtype;
|
|
116
|
+
if (!text)
|
|
117
|
+
text = turn;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
if (!killed)
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
clearTimeout(watchdog);
|
|
128
|
+
fs.rmSync(scratch, { recursive: true, force: true });
|
|
129
|
+
}
|
|
130
|
+
if (killed)
|
|
131
|
+
stopped = `ran past ${o.wallSeconds}s`;
|
|
132
|
+
return { text, costUsd, usage, stopped };
|
|
133
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foldrun/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Agents are just folders. The format, the runner, the checks.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"./kinds",
|
|
28
28
|
"./layout",
|
|
29
29
|
"./limits",
|
|
30
|
+
"./network",
|
|
30
31
|
"./providers",
|
|
31
32
|
"./subagents",
|
|
32
33
|
"./tool-names"
|
package/src/completions.ts
CHANGED
|
@@ -147,6 +147,7 @@ const FIELDS: Record<string, Completion[]> = {
|
|
|
147
147
|
{ label: "disallowedTools", insert: "disallowedTools:\n - " },
|
|
148
148
|
{ label: "subagents", insert: "subagents:\n - ", hint: "colleagues it may delegate a job to — own context, own tools, no wider than its own" },
|
|
149
149
|
{ label: "limits", insert: "limits:\n web.search: 40\n calls: 300", hint: "per step: a call past a limit is refused, never run" },
|
|
150
|
+
{ label: "network", insert: "network:\n - api.example.com", hint: "sites the sandbox may reach: open | trusted | none, or a list added to the trusted list" },
|
|
150
151
|
{
|
|
151
152
|
label: "provider",
|
|
152
153
|
insert: "provider:\n name: \n token: ${PROVIDER_TOKEN}",
|
package/src/deploy.ts
CHANGED
|
@@ -40,6 +40,7 @@ import { conformanceIssues } from "./okf.ts";
|
|
|
40
40
|
import { timezoneProblem } from "./clock.ts";
|
|
41
41
|
import { webProblems, webWarnings, runnerEngines } from "./providers.ts";
|
|
42
42
|
import { readLimits } from "./limits.ts";
|
|
43
|
+
import { networkProblems } from "./network.ts";
|
|
43
44
|
import { actionProblems } from "./web-actions.ts";
|
|
44
45
|
import { languageProblem } from "./language.ts";
|
|
45
46
|
import { localeProblems } from "./locale.ts";
|
|
@@ -326,6 +327,9 @@ export function deployIssues(files: DeployFile[]): DeployIssue[] {
|
|
|
326
327
|
// `limits:` that cannot be read — a count of 0, a word, web.<not an
|
|
327
328
|
// action> — would run uncapped where the author meant a cap.
|
|
328
329
|
for (const w of readLimits(front.limits).problems) at(f.path, w);
|
|
330
|
+
// `network:` that cannot be read would leave the step on the level
|
|
331
|
+
// above it — wider or narrower than the author wrote.
|
|
332
|
+
for (const w of networkProblems(front.network)) at(f.path, w);
|
|
329
333
|
const lang = languageProblem(front.language);
|
|
330
334
|
if (lang) at(f.path, lang);
|
|
331
335
|
for (const w of localeProblems(front)) at(f.path, w);
|
package/src/egress.ts
CHANGED
|
@@ -31,12 +31,21 @@
|
|
|
31
31
|
// `FOLDRUN_EGRESS` instead, and sends through the proxy like everything
|
|
32
32
|
// else.
|
|
33
33
|
//
|
|
34
|
+
// A secret BOUND to sites in the vault (`foldrun secrets set NAME --site
|
|
35
|
+
// api.example.com`) is never materialised, whatever the step grants: the
|
|
36
|
+
// pod's programs all send through the proxy's forward door (HTTPS_PROXY),
|
|
37
|
+
// and for a bound site the door terminates TLS with the platform's egress CA
|
|
38
|
+
// — which the sandbox trusts — and fills `${NAME}` in the URL and headers on
|
|
39
|
+
// the way out, the way Anthropic's cloud sandboxes attach a network secret.
|
|
40
|
+
// Every other site is a tunnel the proxy does not read.
|
|
41
|
+
//
|
|
34
42
|
// This file is the vocabulary both sides share. The server is the
|
|
35
43
|
// platform's (it needs a long-lived process and a Service); the pod side is
|
|
36
44
|
// here in core, because it is what the runner image runs.
|
|
37
45
|
|
|
38
46
|
import { trimSlashes } from "./paths.ts";
|
|
39
47
|
import type { TestEffect } from "./test-mode.ts";
|
|
48
|
+
import type { NetworkLevel } from "./network.ts";
|
|
40
49
|
|
|
41
50
|
/** `${NAME}` — the shape a secret reference takes everywhere in foldrun. */
|
|
42
51
|
export const PLACEHOLDER = /\$\{([A-Z][A-Z0-9_]*)\}/g;
|
|
@@ -99,11 +108,28 @@ export interface EgressGrant {
|
|
|
99
108
|
* that has to put a rate-limit reset into words — and a reset time is
|
|
100
109
|
* only useful in the calendar the step works to. See refusal.ts. */
|
|
101
110
|
timezone?: string;
|
|
111
|
+
/** Which sites the step may reach through the proxy's forward door
|
|
112
|
+
* (network.ts). Absent on a grant from a runner that predates it: the
|
|
113
|
+
* forward door then allows only the hosts the secrets were granted to. */
|
|
114
|
+
network?: { level: NetworkLevel; hosts: string[] };
|
|
115
|
+
/** Refuse a site off the list (true), or allow it and name it in the
|
|
116
|
+
* trace (false: report only). Set by the platform when it leases. */
|
|
117
|
+
enforce?: boolean;
|
|
102
118
|
}
|
|
103
119
|
|
|
104
120
|
export interface EgressLease {
|
|
105
121
|
/** The base the pod sends through: `http://…/e/<token>`. */
|
|
106
122
|
url: string;
|
|
123
|
+
/** The forward-proxy address for HTTP_PROXY / HTTPS_PROXY —
|
|
124
|
+
* `http://foldrun:<token>@host:port` — when the platform's proxy has the
|
|
125
|
+
* forward door. Absent on an older proxy: programs then go direct. */
|
|
126
|
+
proxy?: string;
|
|
127
|
+
/** The PEM of the CA the forward door signs with for a host a secret is
|
|
128
|
+
* bound to, so the sandbox can trust exactly those interceptions. */
|
|
129
|
+
ca?: string;
|
|
130
|
+
/** Whether a site off the step's list is refused (true) or only named
|
|
131
|
+
* in the trace (false). */
|
|
132
|
+
enforce?: boolean;
|
|
107
133
|
/** The grant is finished being built — publish it. The runner keeps
|
|
108
134
|
* adding grants after the lease exists (the model key's host needs the
|
|
109
135
|
* lease URL first), so a store that is not this process's memory has
|
package/src/network.ts
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// Network: which sites a step's sandbox may reach.
|
|
2
|
+
//
|
|
3
|
+
// network: trusted # the trusted list, plus what the step declares
|
|
4
|
+
// network: open # any public site
|
|
5
|
+
// network: none # only what the step declares
|
|
6
|
+
// network: [api.example.com, "*.example.org"] # add sites; the level stays
|
|
7
|
+
// network: { level: trusted, allow: [api.example.com], defaults: false }
|
|
8
|
+
//
|
|
9
|
+
// Written on an agent, a workspace's AGENTS.md or the account's. The level
|
|
10
|
+
// and `defaults` are nearest-wins (agent, then workspace, then account);
|
|
11
|
+
// `allow` adds up across all three, so an account can name a site every
|
|
12
|
+
// agent may reach without each agent repeating it.
|
|
13
|
+
//
|
|
14
|
+
// Unset, an agent that reads the open web — `web` with fetch or browse — is
|
|
15
|
+
// `open`, because a search result can name any site and an allowlist would
|
|
16
|
+
// break it. Every other agent is `trusted`: package registries, source
|
|
17
|
+
// hosts and cloud SDKs, the list Anthropic's own cloud sandboxes start from
|
|
18
|
+
// (TRUSTED_SITES below), plus the hosts the step itself declares — its
|
|
19
|
+
// `apis:`, its remote MCP servers, its web providers, the sites its secrets
|
|
20
|
+
// are bound to. That is the default the cloud sandboxes this platform is
|
|
21
|
+
// measured against use: least access, widened on purpose.
|
|
22
|
+
//
|
|
23
|
+
// The rule is enforced at the egress proxy, which every request from a
|
|
24
|
+
// sandbox goes through (HTTPS_PROXY in the pod), and, on the Kubernetes
|
|
25
|
+
// tier, by the pod's network policy: a `trusted` or `none` pod reaches the
|
|
26
|
+
// proxy and DNS and nothing else, so a program that ignores the proxy
|
|
27
|
+
// variables fails instead of going round. This file is the vocabulary; the
|
|
28
|
+
// proxy (foldrun-platform egress-proxy.ts) holds the rule per request.
|
|
29
|
+
|
|
30
|
+
/** How wide a step's network is. */
|
|
31
|
+
export type NetworkLevel = "open" | "trusted" | "none";
|
|
32
|
+
|
|
33
|
+
export const NETWORK_LEVELS: readonly NetworkLevel[] = ["open", "trusted", "none"];
|
|
34
|
+
|
|
35
|
+
/** One level's `network:` line, read. Absent fields inherit. */
|
|
36
|
+
export interface NetworkSetting {
|
|
37
|
+
level?: NetworkLevel;
|
|
38
|
+
/** Sites added at this level. */
|
|
39
|
+
allow: string[];
|
|
40
|
+
/** false drops the trusted list, leaving only `allow` and what the step declares. */
|
|
41
|
+
defaults?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What one step may reach, resolved. Plain JSON: it crosses into the egress
|
|
45
|
+
* grant and the run record. */
|
|
46
|
+
export interface NetworkPolicy {
|
|
47
|
+
level: NetworkLevel;
|
|
48
|
+
/** Every site allowed beyond what `open` would allow — the trusted list
|
|
49
|
+
* (unless dropped), the `allow` lists, and the hosts the step declares.
|
|
50
|
+
* Empty for `open`, which needs no list. */
|
|
51
|
+
hosts: string[];
|
|
52
|
+
/** Where the level came from, for the trace: "agent", "workspace",
|
|
53
|
+
* "account", or "default" with the reason. */
|
|
54
|
+
from: string;
|
|
55
|
+
/** Whether the trusted list is part of `hosts`. */
|
|
56
|
+
defaults: boolean;
|
|
57
|
+
/** The sites the files added (`allow`, every level), apart from the
|
|
58
|
+
* trusted list — what the trace and the dashboard show as "added". */
|
|
59
|
+
added: string[];
|
|
60
|
+
/** Hosts the step declares (apis, MCP servers, providers, bound secrets). */
|
|
61
|
+
implied: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The trusted list: the domains Anthropic's Claude Code cloud environments
|
|
66
|
+
* allow at their default "Trusted" level, as published on
|
|
67
|
+
* code.claude.com/docs/en/cloud-environments#default-allowed-domains
|
|
68
|
+
* (read 14 Oct 2026), plus the Debian mirrors the runner image installs
|
|
69
|
+
* `runtime: system:` packages from. `*.` matches the subdomains and the
|
|
70
|
+
* bare domain.
|
|
71
|
+
*/
|
|
72
|
+
export const TRUSTED_SITES: readonly string[] = [
|
|
73
|
+
// Anthropic services
|
|
74
|
+
"api.anthropic.com", "docs.claude.com", "platform.claude.com", "code.claude.com", "claude.ai", "claude.com",
|
|
75
|
+
"support.claude.com", "anthropic.com", "www.anthropic.com",
|
|
76
|
+
// Version control
|
|
77
|
+
"github.com", "www.github.com", "api.github.com", "npm.pkg.github.com", "raw.githubusercontent.com",
|
|
78
|
+
"pkg-npm.githubusercontent.com", "objects.githubusercontent.com", "release-assets.githubusercontent.com",
|
|
79
|
+
"codeload.github.com", "avatars.githubusercontent.com", "camo.githubusercontent.com", "gist.github.com",
|
|
80
|
+
"gitlab.com", "www.gitlab.com", "registry.gitlab.com", "bitbucket.org", "www.bitbucket.org", "api.bitbucket.org",
|
|
81
|
+
// Container registries
|
|
82
|
+
"registry-1.docker.io", "auth.docker.io", "index.docker.io", "hub.docker.com", "www.docker.com",
|
|
83
|
+
"production.cloudflare.docker.com", "production.cloudfront.docker.com", "download.docker.com", "gcr.io",
|
|
84
|
+
"*.gcr.io", "ghcr.io", "mcr.microsoft.com", "*.data.mcr.microsoft.com", "public.ecr.aws",
|
|
85
|
+
// Cloud platforms
|
|
86
|
+
"cloud.google.com", "accounts.google.com", "gcloud.google.com", "*.googleapis.com", "storage.googleapis.com",
|
|
87
|
+
"compute.googleapis.com", "container.googleapis.com", "azure.com", "portal.azure.com", "microsoft.com",
|
|
88
|
+
"www.microsoft.com", "*.microsoftonline.com", "packages.microsoft.com", "dotnet.microsoft.com", "dot.net",
|
|
89
|
+
"visualstudio.com", "dev.azure.com", "*.amazonaws.com", "*.api.aws", "oracle.com", "www.oracle.com", "java.com",
|
|
90
|
+
"www.java.com", "java.net", "www.java.net", "download.oracle.com", "yum.oracle.com", "*.r2.cloudflarestorage.com",
|
|
91
|
+
// JavaScript and Node
|
|
92
|
+
"registry.npmjs.org", "www.npmjs.com", "www.npmjs.org", "npmjs.com", "npmjs.org", "yarnpkg.com",
|
|
93
|
+
"registry.yarnpkg.com", "jsr.io", "npm.jsr.io",
|
|
94
|
+
// Python
|
|
95
|
+
"pypi.org", "www.pypi.org", "files.pythonhosted.org", "pythonhosted.org", "test.pypi.org", "pypi.python.org",
|
|
96
|
+
"pypa.io", "www.pypa.io",
|
|
97
|
+
// Ruby
|
|
98
|
+
"rubygems.org", "www.rubygems.org", "api.rubygems.org", "index.rubygems.org", "ruby-lang.org",
|
|
99
|
+
"www.ruby-lang.org", "rubyforge.org", "www.rubyforge.org", "rubyonrails.org", "www.rubyonrails.org", "rvm.io",
|
|
100
|
+
"get.rvm.io",
|
|
101
|
+
// Rust
|
|
102
|
+
"crates.io", "www.crates.io", "index.crates.io", "static.crates.io", "rustup.rs", "static.rust-lang.org",
|
|
103
|
+
"www.rust-lang.org",
|
|
104
|
+
// Go
|
|
105
|
+
"proxy.golang.org", "sum.golang.org", "index.golang.org", "golang.org", "www.golang.org", "goproxy.io", "pkg.go.dev",
|
|
106
|
+
// JVM
|
|
107
|
+
"maven.org", "repo.maven.org", "central.maven.org", "repo1.maven.org", "repo.maven.apache.org",
|
|
108
|
+
"maven.google.com", "jcenter.bintray.com", "gradle.org", "www.gradle.org", "services.gradle.org",
|
|
109
|
+
"plugins.gradle.org", "plugins-artifacts.gradle.org", "kotlinlang.org", "www.kotlinlang.org", "spring.io",
|
|
110
|
+
"repo.spring.io",
|
|
111
|
+
// Other package managers
|
|
112
|
+
"packagist.org", "www.packagist.org", "repo.packagist.org", "nuget.org", "www.nuget.org", "api.nuget.org",
|
|
113
|
+
"pub.dev", "api.pub.dev", "hex.pm", "www.hex.pm", "cpan.org", "www.cpan.org", "metacpan.org", "www.metacpan.org",
|
|
114
|
+
"api.metacpan.org", "cocoapods.org", "www.cocoapods.org", "cdn.cocoapods.org", "haskell.org", "www.haskell.org",
|
|
115
|
+
"hackage.haskell.org", "swift.org", "www.swift.org",
|
|
116
|
+
// Linux distributions — Anthropic's list, then Debian's (the runner image is Debian)
|
|
117
|
+
"archive.ubuntu.com", "security.ubuntu.com", "ubuntu.com", "www.ubuntu.com", "*.ubuntu.com",
|
|
118
|
+
"ppa.launchpad.net", "launchpad.net", "www.launchpad.net", "*.nixos.org",
|
|
119
|
+
"deb.debian.org", "security.debian.org", "snapshot.debian.org",
|
|
120
|
+
// Development tools and platforms
|
|
121
|
+
"dl.k8s.io", "pkgs.k8s.io", "k8s.io", "www.k8s.io", "releases.hashicorp.com", "apt.releases.hashicorp.com",
|
|
122
|
+
"rpm.releases.hashicorp.com", "archive.releases.hashicorp.com", "hashicorp.com", "www.hashicorp.com",
|
|
123
|
+
"repo.anaconda.com", "conda.anaconda.org", "anaconda.org", "www.anaconda.com", "anaconda.com", "continuum.io",
|
|
124
|
+
"apache.org", "www.apache.org", "archive.apache.org", "downloads.apache.org", "eclipse.org", "www.eclipse.org",
|
|
125
|
+
"download.eclipse.org", "nodejs.org", "www.nodejs.org", "developer.apple.com", "developer.android.com",
|
|
126
|
+
"pkg.stainless.com", "binaries.prisma.sh",
|
|
127
|
+
// Cloud services and monitoring
|
|
128
|
+
"http-intake.logs.datadoghq.com", "*.datadoghq.com", "*.datadoghq.eu", "api.honeycomb.io",
|
|
129
|
+
// Content delivery and mirrors
|
|
130
|
+
"sourceforge.net", "*.sourceforge.net", "packagecloud.io", "*.packagecloud.io", "fonts.googleapis.com",
|
|
131
|
+
"fonts.gstatic.com",
|
|
132
|
+
// Schema and configuration
|
|
133
|
+
"json-schema.org", "www.json-schema.org", "json.schemastore.org", "www.schemastore.org",
|
|
134
|
+
// Model Context Protocol
|
|
135
|
+
"*.modelcontextprotocol.io",
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
/** A site as `network:` takes it: a domain or `*.domain` — no scheme, no
|
|
139
|
+
* path, no port. The same shape `web.browse.allowed_domains` takes. */
|
|
140
|
+
export const SITE = /^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/;
|
|
141
|
+
|
|
142
|
+
/** A host as it arrives — `API.Example.com:443.` — as the lists hold it. */
|
|
143
|
+
export function normalHost(host: string): string {
|
|
144
|
+
let h = host.trim().toLowerCase();
|
|
145
|
+
if (h.startsWith("[")) return h.replace(/^\[|\](:\d+)?$/g, ""); // an IPv6 literal
|
|
146
|
+
h = h.replace(/:\d+$/, "");
|
|
147
|
+
return h.replace(/\.$/, "");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Does `site` (a domain or `*.domain`) cover `host`? `*.example.com`
|
|
151
|
+
* covers `a.example.com`, `a.b.example.com` and `example.com` itself. */
|
|
152
|
+
export function siteCovers(site: string, host: string): boolean {
|
|
153
|
+
const h = normalHost(host);
|
|
154
|
+
const s = site.toLowerCase();
|
|
155
|
+
if (s.startsWith("*.")) {
|
|
156
|
+
const base = s.slice(2);
|
|
157
|
+
return h === base || h.endsWith(`.${base}`);
|
|
158
|
+
}
|
|
159
|
+
return h === s;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** May a step under `policy` reach `host`? `open` reaches any public host —
|
|
163
|
+
* the caller still refuses private addresses, which no level opens. */
|
|
164
|
+
export function networkAllows(policy: Pick<NetworkPolicy, "level" | "hosts">, host: string): boolean {
|
|
165
|
+
if (policy.level === "open") return true;
|
|
166
|
+
return policy.hosts.some((s) => siteCovers(s, host));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A list of sites from YAML's list or one comma-separated line. */
|
|
170
|
+
function siteList(v: unknown): string[] | null {
|
|
171
|
+
if (typeof v === "string") return v.split(",").map((x) => x.trim()).filter(Boolean);
|
|
172
|
+
if (Array.isArray(v) && v.every((x) => typeof x === "string")) return v.map((x) => x.trim()).filter(Boolean);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* One `network:` value, read. Never throws: what cannot be read is a
|
|
178
|
+
* problem `check` and deploy report, and the run treats that level as
|
|
179
|
+
* saying nothing — the next level up, or the default, decides.
|
|
180
|
+
*/
|
|
181
|
+
export function readNetwork(raw: unknown, where = "network"): { setting: NetworkSetting | null; problems: string[] } {
|
|
182
|
+
if (raw === undefined || raw === null) return { setting: null, problems: [] };
|
|
183
|
+
const problems: string[] = [];
|
|
184
|
+
const sites = (v: unknown, key: string): string[] => {
|
|
185
|
+
const list = siteList(v);
|
|
186
|
+
if (!list) {
|
|
187
|
+
problems.push(`${key} is a list of sites, like [api.example.com, "*.example.org"].`);
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
const out: string[] = [];
|
|
191
|
+
for (const s of list.map((x) => x.toLowerCase())) {
|
|
192
|
+
if (/^https?:\/\//.test(s) || s.includes("/")) problems.push(`${key}: ${s} — a site is a domain, no scheme or path: ${s.replace(/^https?:\/\//, "").split("/")[0]}`);
|
|
193
|
+
else if (/:\d+$/.test(s)) problems.push(`${key}: ${s} — a site has no port; every port of the host is covered`);
|
|
194
|
+
else if (!SITE.test(s)) problems.push(`${key}: ${s} is not a domain or *.domain`);
|
|
195
|
+
else out.push(s);
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
};
|
|
199
|
+
const level = (v: unknown, key: string): NetworkLevel | undefined => {
|
|
200
|
+
if (typeof v === "string" && (NETWORK_LEVELS as readonly string[]).includes(v.trim().toLowerCase())) {
|
|
201
|
+
return v.trim().toLowerCase() as NetworkLevel;
|
|
202
|
+
}
|
|
203
|
+
problems.push(`${key} is ${NETWORK_LEVELS.join(", ")} — not ${JSON.stringify(v)}.`);
|
|
204
|
+
return undefined;
|
|
205
|
+
};
|
|
206
|
+
if (typeof raw === "string" && (NETWORK_LEVELS as readonly string[]).includes(raw.trim().toLowerCase())) {
|
|
207
|
+
return { setting: { level: raw.trim().toLowerCase() as NetworkLevel, allow: [] }, problems };
|
|
208
|
+
}
|
|
209
|
+
if (typeof raw === "string" || Array.isArray(raw)) {
|
|
210
|
+
// A bare word that is not a level reads as a site only when it looks
|
|
211
|
+
// like one — `network: closed` is a typo, not a domain called "closed".
|
|
212
|
+
if (typeof raw === "string" && !raw.includes(".") && !raw.includes(",")) {
|
|
213
|
+
return { setting: null, problems: [`${where} is ${NETWORK_LEVELS.join(", ")}, a list of sites, or {level, allow, defaults} — not "${raw}".`] };
|
|
214
|
+
}
|
|
215
|
+
const allow = sites(raw, where);
|
|
216
|
+
return { setting: problems.length && !allow.length ? null : { allow }, problems };
|
|
217
|
+
}
|
|
218
|
+
if (typeof raw === "object") {
|
|
219
|
+
const o = raw as Record<string, unknown>;
|
|
220
|
+
for (const k of Object.keys(o)) {
|
|
221
|
+
if (!["level", "allow", "defaults"].includes(k)) problems.push(`${where}.${k} is not a key — level, allow, defaults.`);
|
|
222
|
+
}
|
|
223
|
+
const setting: NetworkSetting = { allow: [] };
|
|
224
|
+
if (o.level !== undefined) setting.level = level(o.level, `${where}.level`);
|
|
225
|
+
if (o.allow !== undefined) setting.allow = sites(o.allow, `${where}.allow`);
|
|
226
|
+
if (o.defaults !== undefined) {
|
|
227
|
+
if (o.defaults === true || o.defaults === false) setting.defaults = o.defaults;
|
|
228
|
+
else problems.push(`${where}.defaults is true or false — false drops the trusted list.`);
|
|
229
|
+
}
|
|
230
|
+
return { setting, problems };
|
|
231
|
+
}
|
|
232
|
+
return { setting: null, problems: [`${where} is ${NETWORK_LEVELS.join(", ")}, a list of sites, or {level, allow, defaults}.`] };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** `check`'s and deploy's view: the problems only. */
|
|
236
|
+
export function networkProblems(raw: unknown): string[] {
|
|
237
|
+
return readNetwork(raw).problems;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* The step's network, from its three levels and what it declares.
|
|
242
|
+
*
|
|
243
|
+
* `readsWeb` — the step may fetch or browse the open web (the `web` tool
|
|
244
|
+
* with fetch or browse). It decides the default level only; a `network:`
|
|
245
|
+
* written anywhere beats it.
|
|
246
|
+
*/
|
|
247
|
+
export function resolveNetwork(args: {
|
|
248
|
+
account?: unknown;
|
|
249
|
+
workspace?: unknown;
|
|
250
|
+
agent?: unknown;
|
|
251
|
+
readsWeb: boolean;
|
|
252
|
+
implied?: Iterable<string>;
|
|
253
|
+
}): NetworkPolicy {
|
|
254
|
+
const levels: [string, NetworkSetting | null][] = [
|
|
255
|
+
["agent", readNetwork(args.agent).setting],
|
|
256
|
+
["workspace", readNetwork(args.workspace).setting],
|
|
257
|
+
["account", readNetwork(args.account).setting],
|
|
258
|
+
];
|
|
259
|
+
const levelHit = levels.find(([, s]) => s?.level);
|
|
260
|
+
const level: NetworkLevel = levelHit ? levelHit[1]!.level! : args.readsWeb ? "open" : "trusted";
|
|
261
|
+
const from = levelHit ? levelHit[0] : args.readsWeb ? "default — the agent reads the web" : "default";
|
|
262
|
+
const defaultsHit = levels.find(([, s]) => s?.defaults !== undefined);
|
|
263
|
+
const defaults = level === "trusted" && (defaultsHit ? defaultsHit[1]!.defaults! : true);
|
|
264
|
+
const added = [...new Set(levels.flatMap(([, s]) => s?.allow ?? []))];
|
|
265
|
+
const implied = [...new Set([...(args.implied ?? [])].map(normalHost).filter((h) => h && SITE.test(h)))];
|
|
266
|
+
const hosts = level === "open" ? [] : [...new Set([...(defaults ? TRUSTED_SITES : []), ...added, ...implied])];
|
|
267
|
+
return { level, hosts, from, defaults, added, implied };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** One line for the run's trace. */
|
|
271
|
+
export function networkLine(policy: NetworkPolicy, enforced: boolean | null): string {
|
|
272
|
+
const how =
|
|
273
|
+
enforced === null
|
|
274
|
+
? " — not enforced here (no sandbox network)"
|
|
275
|
+
: enforced
|
|
276
|
+
? ""
|
|
277
|
+
: " — report only on this install: a site off the list is allowed and named in the trace";
|
|
278
|
+
if (policy.level === "open") return `network: open (${policy.from}) — any public site${how}`;
|
|
279
|
+
const parts: string[] = [];
|
|
280
|
+
if (policy.defaults) parts.push(`the trusted list (${TRUSTED_SITES.length} sites)`);
|
|
281
|
+
if (policy.added.length) parts.push(`added: ${policy.added.join(", ")}`);
|
|
282
|
+
if (policy.implied.length) parts.push(`declared by the step: ${policy.implied.join(", ")}`);
|
|
283
|
+
const what = parts.length ? parts.join("; ") : "nothing but the platform's own services";
|
|
284
|
+
return `network: ${policy.level} (${policy.from}) — ${what}${how}`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** What the proxy says when it refuses a site. */
|
|
288
|
+
export function refusalText(policy: Pick<NetworkPolicy, "level">, host: string): string {
|
|
289
|
+
return `network: ${host} is not a site this step may reach (network: ${policy.level}). Add it to the agent's network: list — network: [${normalHost(host)}] — or set network: open.`;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** The proxy variables a sandbox's programs read, for a forward-proxy URL
|
|
293
|
+
* (`http://foldrun:<token>@host:port`). Both spellings: curl reads only the
|
|
294
|
+
* lower-case http_proxy, much else only the upper-case. NO_PROXY keeps the
|
|
295
|
+
* cluster's own names — the egress address itself, the account's pods —
|
|
296
|
+
* off the proxy. NODE_USE_ENV_PROXY turns the variables on for Node's own
|
|
297
|
+
* fetch and http (Node 22.21 and later). */
|
|
298
|
+
export function proxyEnv(proxyUrl: string, extraNoProxy: string[] = []): Record<string, string> {
|
|
299
|
+
let egressHost = "";
|
|
300
|
+
try {
|
|
301
|
+
egressHost = new URL(proxyUrl).hostname;
|
|
302
|
+
} catch {
|
|
303
|
+
// a malformed URL is still exported as given; the client says why it fails
|
|
304
|
+
}
|
|
305
|
+
const noProxy = [...new Set(["localhost", "127.0.0.1", "::1", ".svc", ".cluster.local", egressHost, ...extraNoProxy].filter(Boolean))].join(",");
|
|
306
|
+
return {
|
|
307
|
+
HTTP_PROXY: proxyUrl,
|
|
308
|
+
HTTPS_PROXY: proxyUrl,
|
|
309
|
+
http_proxy: proxyUrl,
|
|
310
|
+
https_proxy: proxyUrl,
|
|
311
|
+
NO_PROXY: noProxy,
|
|
312
|
+
no_proxy: noProxy,
|
|
313
|
+
NODE_USE_ENV_PROXY: "1",
|
|
314
|
+
// Node 22 warns "EnvHttpProxyAgent is experimental" on the first request
|
|
315
|
+
// of every process; a script tool's stderr then reads as an error to the
|
|
316
|
+
// model. That one warning, and no other.
|
|
317
|
+
NODE_OPTIONS: "--disable-warning=UNDICI-EHPA",
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The env var the egress CA travels in — its PEM, base64'd so it survives
|
|
322
|
+
* every env channel (docker's env file has no multi-line values). The
|
|
323
|
+
* pod's start writes it to a file and points every TLS client at a bundle
|
|
324
|
+
* that includes it. */
|
|
325
|
+
export const EGRESS_CA_ENV = "FOLDRUN_EGRESS_CA";
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The shell the sandbox's start runs before anything else: when the step
|
|
329
|
+
* carries the egress CA, write it, build a bundle of the system's CAs plus
|
|
330
|
+
* it, and point every TLS client the image has at that bundle. The CA only
|
|
331
|
+
* ever signs for hosts a secret is bound to — every other connection is a
|
|
332
|
+
* tunnel the proxy cannot read — but those clients must trust it there.
|
|
333
|
+
* Sourced after the env file, before system packages install (apt goes
|
|
334
|
+
* through the proxy too). `dir` is a directory the start may write.
|
|
335
|
+
*/
|
|
336
|
+
export function egressCaSh(dir: string, systemBundle = "/etc/ssl/certs/ca-certificates.crt"): string {
|
|
337
|
+
// Without the system's bundle to add to, a bundle of the egress CA alone
|
|
338
|
+
// would make every other site untrusted — so only Node, which ADDS the
|
|
339
|
+
// file to its own roots, is told; nothing else changes.
|
|
340
|
+
return `if [ -n "\${${EGRESS_CA_ENV}:-}" ] && printf '%s' "\$${EGRESS_CA_ENV}" | base64 -d > ${dir}/egress-ca.pem 2>/dev/null; then
|
|
341
|
+
if [ -s ${systemBundle} ] && cat ${systemBundle} ${dir}/egress-ca.pem > ${dir}/ca-bundle.pem; then
|
|
342
|
+
export SSL_CERT_FILE=${dir}/ca-bundle.pem REQUESTS_CA_BUNDLE=${dir}/ca-bundle.pem CURL_CA_BUNDLE=${dir}/ca-bundle.pem PIP_CERT=${dir}/ca-bundle.pem GIT_SSL_CAINFO=${dir}/ca-bundle.pem
|
|
343
|
+
fi
|
|
344
|
+
export NODE_EXTRA_CA_CERTS=${dir}/egress-ca.pem
|
|
345
|
+
fi`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** A site as a YAML flow item: `*.x` would read as an alias, so it is quoted. */
|
|
349
|
+
const siteItem = (site: string) => (/^[a-z0-9]/.test(site) ? site : JSON.stringify(site));
|
|
350
|
+
|
|
351
|
+
/** `network:` as one line of YAML, the shortest of the three shapes that
|
|
352
|
+
* says it; null when there is nothing to say. */
|
|
353
|
+
export function networkYaml(setting: { level?: NetworkLevel; allow?: string[]; defaults?: boolean } | null): string | null {
|
|
354
|
+
if (!setting) return null;
|
|
355
|
+
const allow = [...new Set((setting.allow ?? []).map((s) => s.trim().toLowerCase()).filter(Boolean))];
|
|
356
|
+
const list = `[${allow.map(siteItem).join(", ")}]`;
|
|
357
|
+
if (setting.defaults === false) {
|
|
358
|
+
return `{ ${[setting.level ? `level: ${setting.level}` : "", allow.length ? `allow: ${list}` : "", "defaults: false"].filter(Boolean).join(", ")} }`;
|
|
359
|
+
}
|
|
360
|
+
if (setting.level && allow.length) return `{ level: ${setting.level}, allow: ${list} }`;
|
|
361
|
+
if (setting.level) return setting.level;
|
|
362
|
+
if (allow.length) return list;
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The text of an agent.md (or AGENTS.md) with its `network:` replaced by
|
|
368
|
+
* `setting` — or removed, for null — and every other byte as it was. The
|
|
369
|
+
* old value's indented lines go with it, whatever shape it was written in.
|
|
370
|
+
* Pure string work, so the dashboard runs it in the browser and the CLI on
|
|
371
|
+
* a file, and both write exactly the same line.
|
|
372
|
+
*/
|
|
373
|
+
export function setNetworkSetting(raw: string, setting: { level?: NetworkLevel; allow?: string[]; defaults?: boolean } | null): string {
|
|
374
|
+
const eol = raw.includes("\r\n") ? "\r\n" : "\n";
|
|
375
|
+
const lines = raw.split(eol);
|
|
376
|
+
const value = networkYaml(setting);
|
|
377
|
+
const line = value ? `network: ${value}` : null;
|
|
378
|
+
if (lines[0]?.trim() !== "---") return line ? ["---", line, "---", ...lines].join(eol) : raw;
|
|
379
|
+
const close = lines.findIndex((l, i) => i > 0 && l.trim() === "---");
|
|
380
|
+
if (close < 0) return raw;
|
|
381
|
+
const at = lines.findIndex((l, i) => i > 0 && i < close && /^network\s*:/.test(l));
|
|
382
|
+
if (at < 0) {
|
|
383
|
+
if (line) lines.splice(close, 0, line);
|
|
384
|
+
return lines.join(eol);
|
|
385
|
+
}
|
|
386
|
+
let end = at + 1;
|
|
387
|
+
while (end < close && (/^\s+\S/.test(lines[end]) || /^\s*-\s/.test(lines[end]))) end++;
|
|
388
|
+
lines.splice(at, end - at, ...(line ? [line] : []));
|
|
389
|
+
return lines.join(eol);
|
|
390
|
+
}
|
package/src/run-container.ts
CHANGED
|
@@ -30,6 +30,7 @@ import { divertedSummary, isDivertedPath, overlayTestWrites } from "./test-mode.
|
|
|
30
30
|
import type { EventExtra } from "./step-exec.ts";
|
|
31
31
|
import { isFileValue, fileContent } from "./secrets.ts";
|
|
32
32
|
import { safeTenantSegment, systemInstallSh, SYSTEM_FROM_INPUT_SH, INSTALL_SOCKET, type RuntimeSpec } from "./runtime.ts";
|
|
33
|
+
import { egressCaSh, type NetworkLevel } from "./network.ts";
|
|
33
34
|
import type { ScriptSpec } from "./script-tools.ts";
|
|
34
35
|
import type { ConsultSpec } from "./agent-tools.ts";
|
|
35
36
|
import type { SubagentSpec } from "./subagents.ts";
|
|
@@ -951,6 +952,7 @@ ${CORE_INSTALL}`;
|
|
|
951
952
|
// reads the status file and fails the step with apt's own words.
|
|
952
953
|
const ENTRY = `#!/bin/sh
|
|
953
954
|
set +e
|
|
955
|
+
${egressCaSh("/opt/runner/job")}
|
|
954
956
|
${SYSTEM_FROM_INPUT_SH}
|
|
955
957
|
${systemInstallSh(RUNTIME_CACHE)}
|
|
956
958
|
${INSTALL_HELPER_SH}
|
|
@@ -1201,6 +1203,10 @@ export interface RunInContainerArgs {
|
|
|
1201
1203
|
input: ContainerStepInput;
|
|
1202
1204
|
/** Secret + provider env — crosses as an env file, never argv. */
|
|
1203
1205
|
env: Record<string, string>;
|
|
1206
|
+
/** The step's network (network.ts), for an executor that can wall the
|
|
1207
|
+
* sandbox in itself: the k8s tier labels a filtered pod so its policy
|
|
1208
|
+
* admits the proxy and DNS alone. `enforced` false is report-only. */
|
|
1209
|
+
network?: { level: NetworkLevel; enforced: boolean };
|
|
1204
1210
|
emit: (type: "text" | "tool" | "info" | "error", text: string, extra?: EventExtra) => void;
|
|
1205
1211
|
/** The run this step belongs to. Stamped on the sandbox as a label so a
|
|
1206
1212
|
* person stopping the run can reach the thing actually burning money —
|