@foldrun/core 18.1.0 → 18.3.0
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/platform.d.ts +5 -0
- package/dist/src/run-container.d.ts +12 -0
- package/dist/src/run-container.js +6 -0
- package/dist/src/runner.d.ts +4 -1
- package/dist/src/runner.js +24 -2
- package/dist/src/scan.d.ts +17 -0
- package/dist/src/scan.js +30 -0
- package/dist/src/snapshot-archive.d.ts +37 -0
- package/dist/src/snapshot-archive.js +259 -0
- package/dist/src/store.d.ts +7 -0
- package/package.json +3 -2
- package/src/platform.ts +5 -0
- package/src/run-container.ts +14 -0
- package/src/runner.ts +25 -1
- package/src/scan.ts +30 -0
- package/src/snapshot-archive.ts +252 -0
- package/src/store.ts +5 -0
package/dist/src/platform.d.ts
CHANGED
|
@@ -163,6 +163,11 @@ export interface PlatformHooks {
|
|
|
163
163
|
* trace as not enforced.
|
|
164
164
|
*/
|
|
165
165
|
confinesBrowsing?(isolation: string): boolean;
|
|
166
|
+
/** Can this executor run a snapshot step (RunInContainerArgs.snapshot:
|
|
167
|
+
* stage the folder, give back outputs/ alone, make the patch)? A run with a
|
|
168
|
+
* snapshot on any executor that cannot fails before anything starts.
|
|
169
|
+
* Default: none can. */
|
|
170
|
+
holdsSnapshot?(isolation: string): boolean;
|
|
166
171
|
/**
|
|
167
172
|
* Does this isolation mode wall a step whose `network:` is restricted and
|
|
168
173
|
* enforced to the egress proxy alone (a `filtered` pod)? True: such a
|
|
@@ -279,6 +279,10 @@ export declare const APPLY_MTIMES_JS: string;
|
|
|
279
279
|
*/
|
|
280
280
|
export declare function mergeAppends(was: Buffer | string | null, host: Buffer, next: Buffer): Buffer | null;
|
|
281
281
|
export interface ApplyOptions {
|
|
282
|
+
/** Take back only what is under this workspace-relative folder (with its
|
|
283
|
+
* trailing `/`) — a snapshot run's outputs (Part B3): the folder it was
|
|
284
|
+
* given, and anything it copied anywhere else, never reach the workspace. */
|
|
285
|
+
keepOnly?: string;
|
|
282
286
|
/** What the host handed back into this sandbox mid-step — a `sandbox: own`
|
|
283
287
|
* job's files (delegate-tools.ts), path → sha256 — as the HOST recorded
|
|
284
288
|
* it (StepRecord.handedBack). The sandbox's own note of the same is only
|
|
@@ -576,6 +580,14 @@ export declare function dockerCpus(cpus: string, available?: number): string | n
|
|
|
576
580
|
export declare function heldCores(cpus: string, isolation: string | undefined, available?: number): number;
|
|
577
581
|
export interface RunInContainerArgs {
|
|
578
582
|
workspaceRoot: string;
|
|
583
|
+
/** A snapshot run (Part B3): the folder a person sent, by id, for this
|
|
584
|
+
* step to work on. Only an executor that says it can (`supportsSnapshot`
|
|
585
|
+
* on its IsolatedStepRunner) is ever given one: it stages the folder at
|
|
586
|
+
* agents/<name>/snapshot/, takes back only that agent's outputs/, and
|
|
587
|
+
* leaves the patch there as outputs/snapshot.patch. */
|
|
588
|
+
snapshot?: {
|
|
589
|
+
id: string;
|
|
590
|
+
};
|
|
579
591
|
/** A person approved this step before it ran: configuration and code it
|
|
580
592
|
* changed are kept, not refused (copy-back.ts, FOL-96). */
|
|
581
593
|
configWrites?: boolean;
|
|
@@ -365,11 +365,17 @@ export function applyContainerChanges(hostWs, containerWs, baseline, note, diver
|
|
|
365
365
|
// (kubectl cp already skips symlinks; this makes the two tiers agree.)
|
|
366
366
|
if (entry.isSymbolicLink())
|
|
367
367
|
continue;
|
|
368
|
+
const posix = rel.replaceAll("\\", "/");
|
|
368
369
|
if (entry.isDirectory()) {
|
|
370
|
+
const d = posix + "/";
|
|
371
|
+
if (opts.keepOnly && !(d.startsWith(opts.keepOnly) || opts.keepOnly.startsWith(d)))
|
|
372
|
+
continue;
|
|
369
373
|
if (allowedBack(rel + "/"))
|
|
370
374
|
walk(abs);
|
|
371
375
|
continue;
|
|
372
376
|
}
|
|
377
|
+
if (opts.keepOnly && !posix.startsWith(opts.keepOnly))
|
|
378
|
+
continue;
|
|
373
379
|
if (!allowedBack(rel))
|
|
374
380
|
continue;
|
|
375
381
|
let next = fs.readFileSync(abs);
|
package/dist/src/runner.d.ts
CHANGED
|
@@ -592,7 +592,10 @@ export declare function createFlowRun(tenant: string, workspace: string, steps:
|
|
|
592
592
|
a2a?: {
|
|
593
593
|
task: string;
|
|
594
594
|
key: string;
|
|
595
|
-
}
|
|
595
|
+
},
|
|
596
|
+
/** A snapshot run's folder (Part B3): the id POST …/snapshots answered.
|
|
597
|
+
* Its own trailing argument, as a2a is, so the signature only widened. */
|
|
598
|
+
snapshot?: string): RunRecord;
|
|
596
599
|
export declare function startFlowRun(tenant: string, workspace: string, steps: FlowStep[], flowName: string, modelOverride?: string | null, tags?: string[], effortOverride?: string | null, startedBy?: string | null, opts?: {
|
|
597
600
|
test?: boolean;
|
|
598
601
|
}): RunRecord;
|
package/dist/src/runner.js
CHANGED
|
@@ -388,6 +388,11 @@ export function readAgentsMd(dir) {
|
|
|
388
388
|
return null;
|
|
389
389
|
}
|
|
390
390
|
const workspaceRootOf = (agentDir) => path.join(agentDir, "..", "..");
|
|
391
|
+
/** What a snapshot step is told (Part B3). */
|
|
392
|
+
const SNAPSHOT_NOTE = "The folder you were sent to work on is in snapshot/ (inside your working directory). Make your changes there, as files: " +
|
|
393
|
+
"when you finish, foldrun turns what changed in snapshot/ into a patch the person applies. " +
|
|
394
|
+
"Only snapshot/ (as that patch) and your outputs/ come back from this step; anything you write elsewhere is not kept. " +
|
|
395
|
+
"Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out.";
|
|
391
396
|
/**
|
|
392
397
|
* Configuration from AGENTS.md, nearest-wins: the workspace's overrides the
|
|
393
398
|
* account's, key by key.
|
|
@@ -2704,6 +2709,17 @@ mayDefer) {
|
|
|
2704
2709
|
if (stepHookNames.length)
|
|
2705
2710
|
push("info", `hooks: ${stepHookNames.join(", ")}`);
|
|
2706
2711
|
const isolation = process.env.FOLDRUN_RUN_ISOLATION;
|
|
2712
|
+
// A snapshot run (Part B3): the folder is on the run record, set when the
|
|
2713
|
+
// run was made. Only an executor that says it can hold one may run it —
|
|
2714
|
+
// in-process, the step would work in the real workspace folder, and no
|
|
2715
|
+
// copy-back stands between what it writes and the workspace (Codex).
|
|
2716
|
+
const snapshotId = runId ? readRun(tenant, path.basename(workspaceRootOf(agentDir)), runId)?.snapshot?.id : undefined;
|
|
2717
|
+
if (snapshotId) {
|
|
2718
|
+
const able = isolatedRun() && isolation !== "container" && Boolean(platform.isolation[isolation]) && platform.holdsSnapshot?.(isolation) === true;
|
|
2719
|
+
if (!able) {
|
|
2720
|
+
throw new Error("this run has a snapshot (a folder sent to work on), and this install cannot hold one: it needs the k8s sandbox (FOLDRUN_RUN_ISOLATION=k8s). Nothing was run.");
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2707
2723
|
if (isolatedRun()) {
|
|
2708
2724
|
// The isolated path: the whole loop — model, built-in tools, scripts —
|
|
2709
2725
|
// runs inside a throwaway container (a docker sibling, or a pod), and
|
|
@@ -3077,6 +3093,7 @@ mayDefer) {
|
|
|
3077
3093
|
const runIsolated = isolation === "container" ? runStepInContainer : platform.isolation[isolation];
|
|
3078
3094
|
const isolatedArgs = (modelEnv, keyName = MODEL_KEY_NAME) => ({
|
|
3079
3095
|
workspaceRoot,
|
|
3096
|
+
...(snapshotId ? { snapshot: { id: snapshotId } } : {}),
|
|
3080
3097
|
libraryRoot: libraryDir(tenant),
|
|
3081
3098
|
galleryRoot: platform.galleryDir() ?? undefined,
|
|
3082
3099
|
// Keys this step's dependency cache. Per-account by design — see
|
|
@@ -3086,7 +3103,8 @@ mayDefer) {
|
|
|
3086
3103
|
agentRel: path.relative(workspaceRoot, agentDir).replaceAll("\\", "/"),
|
|
3087
3104
|
// Its file tools inside the sandbox get the same allowance (FOL-96).
|
|
3088
3105
|
configWrites: step.approve === true && Boolean(step.approvedAt),
|
|
3089
|
-
|
|
3106
|
+
// A snapshot step is told where its folder is, and what comes back.
|
|
3107
|
+
prompt: snapshotId ? `${prompt}\n\n${SNAPSHOT_NOTE}` : prompt,
|
|
3090
3108
|
model,
|
|
3091
3109
|
effort,
|
|
3092
3110
|
systemPrompt,
|
|
@@ -4285,7 +4303,10 @@ export function readSchemaFile(workspaceRoot, rel) {
|
|
|
4285
4303
|
export function createFlowRun(tenant, workspace, steps, flowName, status, tags = [], startedBy = null, opts = {},
|
|
4286
4304
|
/** An A2A task's correlation (foldrun-platform flow-admission.ts) — its
|
|
4287
4305
|
* own trailing argument, so the signature only widened. */
|
|
4288
|
-
a2a
|
|
4306
|
+
a2a,
|
|
4307
|
+
/** A snapshot run's folder (Part B3): the id POST …/snapshots answered.
|
|
4308
|
+
* Its own trailing argument, as a2a is, so the signature only widened. */
|
|
4309
|
+
snapshot) {
|
|
4289
4310
|
let flowMaxOut;
|
|
4290
4311
|
try {
|
|
4291
4312
|
flowMaxOut = readFlow(tenant, workspace, flowName)?.maxOutputTokens;
|
|
@@ -4302,6 +4323,7 @@ a2a) {
|
|
|
4302
4323
|
...(opts.test ? { test: true } : {}),
|
|
4303
4324
|
// An outside agent asked, over A2A: the task it serves, and whose key.
|
|
4304
4325
|
...(a2a ? { trigger: "a2a", a2a: { task: a2a.task, key: a2a.key } } : {}),
|
|
4326
|
+
...(snapshot ? { snapshot: { id: snapshot } } : {}),
|
|
4305
4327
|
startedAt: new Date().toISOString(),
|
|
4306
4328
|
finishedAt: null,
|
|
4307
4329
|
steps: steps.map((s) => ({
|
package/dist/src/scan.d.ts
CHANGED
|
@@ -93,6 +93,23 @@ export declare function untrustedTextFindings(text: string): {
|
|
|
93
93
|
line: number;
|
|
94
94
|
message: string;
|
|
95
95
|
}[];
|
|
96
|
+
/**
|
|
97
|
+
* Secrets in files about to LEAVE this machine (a snapshot upload, Part B3,
|
|
98
|
+
* 10 Nov 2026): every file, with nothing the files themselves can turn off —
|
|
99
|
+
* no foldrun-ignore, no vendored-path skip (Codex: a repo comment must not
|
|
100
|
+
* silence a key on its way out). A file that is not text (a NUL in its
|
|
101
|
+
* first 8 KB) cannot be read for secrets and is returned as unscanned, for
|
|
102
|
+
* the caller to say so rather than pass it quietly.
|
|
103
|
+
*/
|
|
104
|
+
export declare function uploadSecretFindings(files: ScanFile[]): {
|
|
105
|
+
findings: {
|
|
106
|
+
rule: string;
|
|
107
|
+
file: string;
|
|
108
|
+
line?: number;
|
|
109
|
+
message: string;
|
|
110
|
+
}[];
|
|
111
|
+
unscanned: string[];
|
|
112
|
+
};
|
|
96
113
|
/** The known field a key was probably meant to be, or null. */
|
|
97
114
|
export declare function nearestField(key: string, known: readonly string[]): string | null;
|
|
98
115
|
/** What a run leaves in an agent's folder — the Claude CLI's `.cache/` logs,
|
package/dist/src/scan.js
CHANGED
|
@@ -388,6 +388,9 @@ const SECRET_SHAPES = [
|
|
|
388
388
|
[/\bnpm_[A-Za-z0-9]{36}\b/g, "an npm token"],
|
|
389
389
|
[/\bhf_[A-Za-z0-9]{34,}\b/g, "a Hugging Face token"],
|
|
390
390
|
[/\blin_api_[A-Za-z0-9]{40}\b/g, "a Linear key"],
|
|
391
|
+
// foldrun's own (FOL-154): API, gateway, A2A and deploy keys, and the
|
|
392
|
+
// prefixes before them. 48 hex: the exact shape minted, never `fr_api_…`.
|
|
393
|
+
[/\b(?:fr_(?:api|gw|a2a|dep)|md[ak])_[0-9a-f]{48}\b/g, "a foldrun key"],
|
|
391
394
|
[/\bdop_v1_[a-f0-9]{64}\b/g, "a DigitalOcean token"],
|
|
392
395
|
[/\bshp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}\b/g, "a Shopify token"],
|
|
393
396
|
[/\bre_[A-Za-z0-9]{8,}_[A-Za-z0-9]{16,}\b/g, "a Resend key"],
|
|
@@ -598,6 +601,33 @@ export function untrustedTextFindings(text) {
|
|
|
598
601
|
scanContent({ path: "untrusted", content: text }, text.split(/\r?\n/), (rule, _file, line, _text, message) => out.push({ rule, line: line ?? 0, message }), true);
|
|
599
602
|
return out;
|
|
600
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* Secrets in files about to LEAVE this machine (a snapshot upload, Part B3,
|
|
606
|
+
* 10 Nov 2026): every file, with nothing the files themselves can turn off —
|
|
607
|
+
* no foldrun-ignore, no vendored-path skip (Codex: a repo comment must not
|
|
608
|
+
* silence a key on its way out). A file that is not text (a NUL in its
|
|
609
|
+
* first 8 KB) cannot be read for secrets and is returned as unscanned, for
|
|
610
|
+
* the caller to say so rather than pass it quietly.
|
|
611
|
+
*/
|
|
612
|
+
export function uploadSecretFindings(files) {
|
|
613
|
+
const findings = [];
|
|
614
|
+
const unscanned = [];
|
|
615
|
+
const seen = new Set();
|
|
616
|
+
for (const f of files) {
|
|
617
|
+
if (f.content.slice(0, 8000).includes("\u0000")) {
|
|
618
|
+
unscanned.push(f.path);
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
scanSecrets(f, f.content.split(/\r?\n/), (rule, file, line, _text, message) => {
|
|
622
|
+
const key = `${rule}\0${file}\0${line ?? ""}`;
|
|
623
|
+
if (seen.has(key))
|
|
624
|
+
return;
|
|
625
|
+
seen.add(key);
|
|
626
|
+
findings.push({ rule, file, ...(line ? { line } : {}), message });
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
return { findings, unscanned };
|
|
630
|
+
}
|
|
601
631
|
/** The text hidden in tag characters: each is U+E0000 + an ASCII code. */
|
|
602
632
|
function decodeTags(s) {
|
|
603
633
|
let out = "";
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface SnapshotEntry {
|
|
2
|
+
/** Relative, `/`-separated. */
|
|
3
|
+
path: string;
|
|
4
|
+
/** 0o644 or 0o755. */
|
|
5
|
+
mode: number;
|
|
6
|
+
content: Buffer;
|
|
7
|
+
}
|
|
8
|
+
export interface SnapshotLimits {
|
|
9
|
+
maxFiles: number;
|
|
10
|
+
maxFileBytes: number;
|
|
11
|
+
maxTotalBytes: number;
|
|
12
|
+
maxCompressedBytes: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const SNAPSHOT_LIMITS: SnapshotLimits;
|
|
15
|
+
/** Why an archive was refused, in words a person can act on. */
|
|
16
|
+
export declare class SnapshotRefused extends Error {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
/** The path rules, for the packer to check before it packs, and the reader after. */
|
|
20
|
+
export declare function snapshotPathProblem(p: string): string | null;
|
|
21
|
+
/** A deterministic gzipped tar of these files (directories are implied). */
|
|
22
|
+
export declare function packSnapshot(entries: SnapshotEntry[], limits?: SnapshotLimits): {
|
|
23
|
+
archive: Buffer;
|
|
24
|
+
sha256: string;
|
|
25
|
+
files: number;
|
|
26
|
+
bytes: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Unpack an archive a stranger sent, under every rule above. Throws
|
|
30
|
+
* SnapshotRefused naming the first thing wrong; nothing is written anywhere —
|
|
31
|
+
* the caller gets the entries to store.
|
|
32
|
+
*/
|
|
33
|
+
export declare function unpackSnapshot(archive: Buffer, limits?: SnapshotLimits): {
|
|
34
|
+
entries: SnapshotEntry[];
|
|
35
|
+
sha256: string;
|
|
36
|
+
bytes: number;
|
|
37
|
+
};
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Snapshot archives (Part B3, 10 Nov 2026): a folder from the user's machine,
|
|
2
|
+
// as one gzipped tar, for an agent on foldrun to work on.
|
|
3
|
+
//
|
|
4
|
+
// One writer and one strict reader, here, so the CLI (which packs) and the
|
|
5
|
+
// platform (which unpacks what a stranger sent) hold the same rules:
|
|
6
|
+
//
|
|
7
|
+
// - only regular files and directories — no links, devices, FIFOs, sparse
|
|
8
|
+
// files, PAX or GNU long-name headers (each a way to make a path mean
|
|
9
|
+
// something other than it says);
|
|
10
|
+
// - every path relative, `/`-separated, no `.`/`..`/empty segment, no
|
|
11
|
+
// backslash, NUL or control character, ≤ 255 bytes and ≤ 32 levels;
|
|
12
|
+
// - no path twice, also when only its case differs, and no file where a
|
|
13
|
+
// directory is (`a` and `a/b`);
|
|
14
|
+
// - limits on file count, one file's size and the whole, enforced while
|
|
15
|
+
// reading — before a byte past them is held;
|
|
16
|
+
// - header checksums verified; nothing after the end-of-archive blocks;
|
|
17
|
+
// - modes are 0644 or 0755 (the owner's exec bit), nothing else kept.
|
|
18
|
+
//
|
|
19
|
+
// The writer is deterministic: entries sorted, mtime 0, uid/gid 0, no names
|
|
20
|
+
// — the same folder is the same bytes, so a snapshot is known by its hash.
|
|
21
|
+
import crypto from "node:crypto";
|
|
22
|
+
import zlib from "node:zlib";
|
|
23
|
+
export const SNAPSHOT_LIMITS = {
|
|
24
|
+
maxFiles: 20_000,
|
|
25
|
+
maxFileBytes: 10 * 1024 * 1024,
|
|
26
|
+
maxTotalBytes: 200 * 1024 * 1024,
|
|
27
|
+
maxCompressedBytes: 50 * 1024 * 1024,
|
|
28
|
+
};
|
|
29
|
+
const MAX_PATH = 255;
|
|
30
|
+
const MAX_DEPTH = 32;
|
|
31
|
+
/** Why an archive was refused, in words a person can act on. */
|
|
32
|
+
export class SnapshotRefused extends Error {
|
|
33
|
+
constructor(message) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "SnapshotRefused";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** The path rules, for the packer to check before it packs, and the reader after. */
|
|
39
|
+
export function snapshotPathProblem(p) {
|
|
40
|
+
if (!p)
|
|
41
|
+
return "an empty path";
|
|
42
|
+
if (Buffer.byteLength(p) > MAX_PATH)
|
|
43
|
+
return `a path longer than ${MAX_PATH} bytes`;
|
|
44
|
+
if (p.startsWith("/"))
|
|
45
|
+
return "an absolute path";
|
|
46
|
+
if (/[\u0000-\u001f\u007f\\]/.test(p))
|
|
47
|
+
return "a backslash or control character in a path";
|
|
48
|
+
const parts = p.split("/");
|
|
49
|
+
if (parts.length > MAX_DEPTH)
|
|
50
|
+
return `a path deeper than ${MAX_DEPTH} folders`;
|
|
51
|
+
if (parts.some((s) => s === "" || s === "." || s === ".."))
|
|
52
|
+
return "an empty, `.` or `..` segment";
|
|
53
|
+
// The files, never a repository: a `.git` sent in could become git's own
|
|
54
|
+
// configuration (hooks, fsmonitor) wherever git later reads the folder.
|
|
55
|
+
if (parts.some((s) => s.toLowerCase() === ".git"))
|
|
56
|
+
return "a `.git` folder or file — send the files, not the repository";
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The set of paths in one archive, with every clash rule in one place for the
|
|
61
|
+
* writer and the reader alike (Codex: the writer packed what the reader then
|
|
62
|
+
* refused). Case-insensitive throughout: `A` and `a/b` cannot both exist on
|
|
63
|
+
* a case-insensitive filesystem, so neither may be here.
|
|
64
|
+
*/
|
|
65
|
+
class PathSet {
|
|
66
|
+
kinds = new Map();
|
|
67
|
+
parents = new Set();
|
|
68
|
+
add(p, isDir) {
|
|
69
|
+
const key = p.toLowerCase();
|
|
70
|
+
if (this.kinds.has(key))
|
|
71
|
+
throw new SnapshotRefused(`${p}: there twice (paths may not differ only by case)`);
|
|
72
|
+
const segs = key.split("/");
|
|
73
|
+
for (let i = 1; i < segs.length; i++) {
|
|
74
|
+
const parent = segs.slice(0, i).join("/");
|
|
75
|
+
if (this.kinds.get(parent) === "file")
|
|
76
|
+
throw new SnapshotRefused(`${p}: inside ${p.split("/").slice(0, i).join("/")}, which is a file`);
|
|
77
|
+
}
|
|
78
|
+
if (!isDir && this.parents.has(key))
|
|
79
|
+
throw new SnapshotRefused(`${p}: a file where another entry makes it a folder`);
|
|
80
|
+
this.kinds.set(key, isDir ? "dir" : "file");
|
|
81
|
+
for (let i = 1; i < segs.length; i++)
|
|
82
|
+
this.parents.add(segs.slice(0, i).join("/"));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// ------------------------------------------------------------------ writer
|
|
86
|
+
function octal(n, width) {
|
|
87
|
+
return n.toString(8).padStart(width - 1, "0") + "\0";
|
|
88
|
+
}
|
|
89
|
+
function header(path, size, mode, type) {
|
|
90
|
+
const h = Buffer.alloc(512, 0);
|
|
91
|
+
// ustar: name (100) and prefix (155); split at a "/" when it is long.
|
|
92
|
+
let name = path;
|
|
93
|
+
let prefix = "";
|
|
94
|
+
if (Buffer.byteLength(name) > 100) {
|
|
95
|
+
const cut = path.lastIndexOf("/", path.length - 1);
|
|
96
|
+
let at = -1;
|
|
97
|
+
for (let i = cut; i > 0; i = path.lastIndexOf("/", i - 1)) {
|
|
98
|
+
if (Buffer.byteLength(path.slice(i + 1)) <= 100 && Buffer.byteLength(path.slice(0, i)) <= 155) {
|
|
99
|
+
at = i;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (at < 0)
|
|
104
|
+
throw new SnapshotRefused(`${path}: too long to store`);
|
|
105
|
+
prefix = path.slice(0, at);
|
|
106
|
+
name = path.slice(at + 1);
|
|
107
|
+
}
|
|
108
|
+
h.write(name, 0, 100, "utf8");
|
|
109
|
+
h.write(octal(mode, 8), 100, 8, "ascii");
|
|
110
|
+
h.write(octal(0, 8), 108, 8, "ascii"); // uid
|
|
111
|
+
h.write(octal(0, 8), 116, 8, "ascii"); // gid
|
|
112
|
+
h.write(octal(size, 12), 124, 12, "ascii");
|
|
113
|
+
h.write(octal(0, 12), 136, 12, "ascii"); // mtime
|
|
114
|
+
h.write(" ", 148, 8, "ascii"); // checksum placeholder
|
|
115
|
+
h.write(type, 156, 1, "ascii");
|
|
116
|
+
h.write("ustar\u000000", 257, 8, "ascii");
|
|
117
|
+
h.write(prefix, 345, 155, "utf8");
|
|
118
|
+
let sum = 0;
|
|
119
|
+
for (const b of h)
|
|
120
|
+
sum += b;
|
|
121
|
+
h.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, 8, "ascii");
|
|
122
|
+
return h;
|
|
123
|
+
}
|
|
124
|
+
/** A deterministic gzipped tar of these files (directories are implied). */
|
|
125
|
+
export function packSnapshot(entries, limits = SNAPSHOT_LIMITS) {
|
|
126
|
+
if (entries.length > limits.maxFiles)
|
|
127
|
+
throw new SnapshotRefused(`${entries.length} files — the most is ${limits.maxFiles}`);
|
|
128
|
+
const sorted = [...entries].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
129
|
+
const parts = [];
|
|
130
|
+
let total = 0;
|
|
131
|
+
const paths = new PathSet();
|
|
132
|
+
for (const e of sorted) {
|
|
133
|
+
const problem = snapshotPathProblem(e.path);
|
|
134
|
+
if (problem)
|
|
135
|
+
throw new SnapshotRefused(`${e.path}: ${problem}`);
|
|
136
|
+
paths.add(e.path, false);
|
|
137
|
+
if (e.content.length > limits.maxFileBytes)
|
|
138
|
+
throw new SnapshotRefused(`${e.path}: ${e.content.length} bytes — the most for one file is ${limits.maxFileBytes}`);
|
|
139
|
+
total += e.content.length;
|
|
140
|
+
if (total > limits.maxTotalBytes)
|
|
141
|
+
throw new SnapshotRefused(`more than ${limits.maxTotalBytes} bytes in all`);
|
|
142
|
+
parts.push(header(e.path, e.content.length, e.mode === 0o755 ? 0o755 : 0o644, "0"));
|
|
143
|
+
parts.push(e.content);
|
|
144
|
+
const pad = (512 - (e.content.length % 512)) % 512;
|
|
145
|
+
if (pad)
|
|
146
|
+
parts.push(Buffer.alloc(pad, 0));
|
|
147
|
+
}
|
|
148
|
+
parts.push(Buffer.alloc(1024, 0));
|
|
149
|
+
const archive = zlib.gzipSync(Buffer.concat(parts), { level: 9, mtime: 0 });
|
|
150
|
+
// gzip's header carries an mtime and an OS byte; both fixed, so the bytes are the folder's alone.
|
|
151
|
+
archive.writeUInt32LE(0, 4);
|
|
152
|
+
archive[9] = 255;
|
|
153
|
+
if (archive.length > limits.maxCompressedBytes)
|
|
154
|
+
throw new SnapshotRefused(`${archive.length} bytes compressed — the most is ${limits.maxCompressedBytes}`);
|
|
155
|
+
return { archive, sha256: crypto.createHash("sha256").update(archive).digest("hex"), files: sorted.length, bytes: total };
|
|
156
|
+
}
|
|
157
|
+
// ------------------------------------------------------------------ reader
|
|
158
|
+
function readString(b, start, len) {
|
|
159
|
+
const s = b.subarray(start, start + len);
|
|
160
|
+
const nul = s.indexOf(0);
|
|
161
|
+
return s.subarray(0, nul < 0 ? len : nul).toString("utf8");
|
|
162
|
+
}
|
|
163
|
+
function readOctal(b, start, len, what) {
|
|
164
|
+
const raw = readString(b, start, len).trim();
|
|
165
|
+
if (raw === "")
|
|
166
|
+
return 0;
|
|
167
|
+
if (!/^[0-7]+$/.test(raw))
|
|
168
|
+
throw new SnapshotRefused(`a header's ${what} is not a number`);
|
|
169
|
+
const n = parseInt(raw, 8);
|
|
170
|
+
if (!Number.isSafeInteger(n))
|
|
171
|
+
throw new SnapshotRefused(`a header's ${what} is out of range`);
|
|
172
|
+
return n;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Unpack an archive a stranger sent, under every rule above. Throws
|
|
176
|
+
* SnapshotRefused naming the first thing wrong; nothing is written anywhere —
|
|
177
|
+
* the caller gets the entries to store.
|
|
178
|
+
*/
|
|
179
|
+
export function unpackSnapshot(archive, limits = SNAPSHOT_LIMITS) {
|
|
180
|
+
if (archive.length > limits.maxCompressedBytes)
|
|
181
|
+
throw new SnapshotRefused(`${archive.length} bytes compressed — the most is ${limits.maxCompressedBytes}`);
|
|
182
|
+
let tar;
|
|
183
|
+
try {
|
|
184
|
+
// The tar is at most the files, their headers and padding: past that, a bomb.
|
|
185
|
+
const ceiling = limits.maxTotalBytes + (limits.maxFiles + 2) * 1024 + 1024;
|
|
186
|
+
tar = zlib.gunzipSync(archive, { maxOutputLength: ceiling });
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
const code = err.code;
|
|
190
|
+
throw new SnapshotRefused(code === "ERR_BUFFER_TOO_LARGE" || /larger than/i.test(String(err.message)) ? `it unpacks past ${limits.maxTotalBytes} bytes` : "it is not a gzip archive");
|
|
191
|
+
}
|
|
192
|
+
const entries = [];
|
|
193
|
+
const paths = new PathSet();
|
|
194
|
+
// Every entry counts against maxFiles, folders too, before its path is
|
|
195
|
+
// recorded: an archive of folders alone would otherwise pass every limit
|
|
196
|
+
// and fill memory with paths (Codex). Our writer emits no folder entries.
|
|
197
|
+
let count = 0;
|
|
198
|
+
let total = 0;
|
|
199
|
+
let off = 0;
|
|
200
|
+
let ended = false;
|
|
201
|
+
while (off + 512 <= tar.length) {
|
|
202
|
+
const h = tar.subarray(off, off + 512);
|
|
203
|
+
if (h.every((x) => x === 0)) {
|
|
204
|
+
const next = tar.subarray(off + 512, off + 1024);
|
|
205
|
+
if (next.length === 512 && next.every((x) => x === 0)) {
|
|
206
|
+
ended = true;
|
|
207
|
+
off += 1024;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
throw new SnapshotRefused("a lone empty block before the end");
|
|
211
|
+
}
|
|
212
|
+
let sum = 0;
|
|
213
|
+
for (let i = 0; i < 512; i++)
|
|
214
|
+
sum += i >= 148 && i < 156 ? 32 : h[i];
|
|
215
|
+
if (readOctal(h, 148, 8, "checksum") !== sum)
|
|
216
|
+
throw new SnapshotRefused("a header's checksum is wrong");
|
|
217
|
+
if (readString(h, 257, 6) !== "ustar")
|
|
218
|
+
throw new SnapshotRefused("not a ustar archive");
|
|
219
|
+
const type = String.fromCharCode(h[156]);
|
|
220
|
+
const name = readString(h, 0, 100);
|
|
221
|
+
const prefix = readString(h, 345, 155);
|
|
222
|
+
const p0 = prefix ? `${prefix}/${name}` : name;
|
|
223
|
+
const isDir = type === "5";
|
|
224
|
+
const p = isDir ? p0.replace(/\/+$/, "") : p0;
|
|
225
|
+
if (type !== "0" && type !== "\0" && !isDir) {
|
|
226
|
+
const what = { "1": "a hard link", "2": "a symbolic link", "3": "a character device", "4": "a block device", "6": "a FIFO", x: "an extended (PAX) header", g: "a global PAX header", L: "a GNU long name", K: "a GNU long link", S: "a sparse file" }[type] ?? `an entry of type ${JSON.stringify(type)}`;
|
|
227
|
+
throw new SnapshotRefused(`${p || "(unnamed)"}: ${what} — only plain files and folders`);
|
|
228
|
+
}
|
|
229
|
+
const problem = snapshotPathProblem(p);
|
|
230
|
+
if (problem)
|
|
231
|
+
throw new SnapshotRefused(`${p}: ${problem}`);
|
|
232
|
+
const size = readOctal(h, 124, 12, "size");
|
|
233
|
+
if (isDir && size !== 0)
|
|
234
|
+
throw new SnapshotRefused(`${p}: a folder with a size`);
|
|
235
|
+
if (size > limits.maxFileBytes)
|
|
236
|
+
throw new SnapshotRefused(`${p}: ${size} bytes — the most for one file is ${limits.maxFileBytes}`);
|
|
237
|
+
if (++count > limits.maxFiles)
|
|
238
|
+
throw new SnapshotRefused(`more than ${limits.maxFiles} entries`);
|
|
239
|
+
paths.add(p, isDir);
|
|
240
|
+
off += 512;
|
|
241
|
+
if (!isDir) {
|
|
242
|
+
if (off + size > tar.length)
|
|
243
|
+
throw new SnapshotRefused(`${p}: cut short`);
|
|
244
|
+
total += size;
|
|
245
|
+
if (total > limits.maxTotalBytes)
|
|
246
|
+
throw new SnapshotRefused(`more than ${limits.maxTotalBytes} bytes in all`);
|
|
247
|
+
if (entries.length + 1 > limits.maxFiles)
|
|
248
|
+
throw new SnapshotRefused(`more than ${limits.maxFiles} files`);
|
|
249
|
+
const mode = readOctal(h, 100, 8, "mode");
|
|
250
|
+
entries.push({ path: p, mode: mode & 0o100 ? 0o755 : 0o644, content: Buffer.from(tar.subarray(off, off + size)) });
|
|
251
|
+
off += size + ((512 - (size % 512)) % 512);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (!ended)
|
|
255
|
+
throw new SnapshotRefused("no end-of-archive marker");
|
|
256
|
+
if (tar.subarray(off).some((x) => x !== 0))
|
|
257
|
+
throw new SnapshotRefused("data after the end of the archive");
|
|
258
|
+
return { entries, sha256: crypto.createHash("sha256").update(archive).digest("hex"), bytes: total };
|
|
259
|
+
}
|
package/dist/src/store.d.ts
CHANGED
|
@@ -1635,6 +1635,13 @@ export interface RunRecord {
|
|
|
1635
1635
|
task: string;
|
|
1636
1636
|
key: string;
|
|
1637
1637
|
};
|
|
1638
|
+
/** A snapshot run (Part B3): the folder a person sent (its sha256), staged
|
|
1639
|
+
* for the step at agents/<name>/snapshot/; the patch of what the step
|
|
1640
|
+
* changed is outputs/snapshot.patch. Set when the run is made, never by a
|
|
1641
|
+
* step. */
|
|
1642
|
+
snapshot?: {
|
|
1643
|
+
id: string;
|
|
1644
|
+
};
|
|
1638
1645
|
steps: StepRecord[];
|
|
1639
1646
|
}
|
|
1640
1647
|
/** Merge one part (the input, on the call; the output, on its completion)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foldrun/core",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.3.0",
|
|
4
4
|
"description": "Agents are just folders. The format, the runner, the checks.",
|
|
5
5
|
"license": "FSL-1.1-ALv2",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"./providers",
|
|
34
34
|
"./subagents",
|
|
35
35
|
"./tool-names",
|
|
36
|
-
"./scan"
|
|
36
|
+
"./scan",
|
|
37
|
+
"./snapshot-archive"
|
|
37
38
|
]
|
|
38
39
|
},
|
|
39
40
|
"main": "./dist/index.js",
|
package/src/platform.ts
CHANGED
|
@@ -168,6 +168,11 @@ export interface PlatformHooks {
|
|
|
168
168
|
* trace as not enforced.
|
|
169
169
|
*/
|
|
170
170
|
confinesBrowsing?(isolation: string): boolean;
|
|
171
|
+
/** Can this executor run a snapshot step (RunInContainerArgs.snapshot:
|
|
172
|
+
* stage the folder, give back outputs/ alone, make the patch)? A run with a
|
|
173
|
+
* snapshot on any executor that cannot fails before anything starts.
|
|
174
|
+
* Default: none can. */
|
|
175
|
+
holdsSnapshot?(isolation: string): boolean;
|
|
171
176
|
/**
|
|
172
177
|
* Does this isolation mode wall a step whose `network:` is restricted and
|
|
173
178
|
* enforced to the egress proxy alone (a `filtered` pod)? True: such a
|
package/src/run-container.ts
CHANGED
|
@@ -482,6 +482,10 @@ export function mergeAppends(was: Buffer | string | null, host: Buffer, next: Bu
|
|
|
482
482
|
}
|
|
483
483
|
|
|
484
484
|
export interface ApplyOptions {
|
|
485
|
+
/** Take back only what is under this workspace-relative folder (with its
|
|
486
|
+
* trailing `/`) — a snapshot run's outputs (Part B3): the folder it was
|
|
487
|
+
* given, and anything it copied anywhere else, never reach the workspace. */
|
|
488
|
+
keepOnly?: string;
|
|
485
489
|
/** What the host handed back into this sandbox mid-step — a `sandbox: own`
|
|
486
490
|
* job's files (delegate-tools.ts), path → sha256 — as the HOST recorded
|
|
487
491
|
* it (StepRecord.handedBack). The sandbox's own note of the same is only
|
|
@@ -552,10 +556,14 @@ export function applyContainerChanges(
|
|
|
552
556
|
// A link to a directory threw EISDIR and aborted the whole copy-out.
|
|
553
557
|
// (kubectl cp already skips symlinks; this makes the two tiers agree.)
|
|
554
558
|
if (entry.isSymbolicLink()) continue;
|
|
559
|
+
const posix = rel.replaceAll("\\", "/");
|
|
555
560
|
if (entry.isDirectory()) {
|
|
561
|
+
const d = posix + "/";
|
|
562
|
+
if (opts.keepOnly && !(d.startsWith(opts.keepOnly) || opts.keepOnly.startsWith(d))) continue;
|
|
556
563
|
if (allowedBack(rel + "/")) walk(abs);
|
|
557
564
|
continue;
|
|
558
565
|
}
|
|
566
|
+
if (opts.keepOnly && !posix.startsWith(opts.keepOnly)) continue;
|
|
559
567
|
if (!allowedBack(rel)) continue;
|
|
560
568
|
let next: Buffer = fs.readFileSync(abs);
|
|
561
569
|
// What the step was handed: its bytes (a staging dir) or its hash.
|
|
@@ -1861,6 +1869,12 @@ export function heldCores(cpus: string, isolation: string | undefined, available
|
|
|
1861
1869
|
|
|
1862
1870
|
export interface RunInContainerArgs {
|
|
1863
1871
|
workspaceRoot: string;
|
|
1872
|
+
/** A snapshot run (Part B3): the folder a person sent, by id, for this
|
|
1873
|
+
* step to work on. Only an executor that says it can (`supportsSnapshot`
|
|
1874
|
+
* on its IsolatedStepRunner) is ever given one: it stages the folder at
|
|
1875
|
+
* agents/<name>/snapshot/, takes back only that agent's outputs/, and
|
|
1876
|
+
* leaves the patch there as outputs/snapshot.patch. */
|
|
1877
|
+
snapshot?: { id: string };
|
|
1864
1878
|
/** A person approved this step before it ran: configuration and code it
|
|
1865
1879
|
* changed are kept, not refused (copy-back.ts, FOL-96). */
|
|
1866
1880
|
configWrites?: boolean;
|
package/src/runner.ts
CHANGED
|
@@ -460,6 +460,13 @@ export function readAgentsMd(dir: string): { data: Record<string, unknown>; body
|
|
|
460
460
|
|
|
461
461
|
const workspaceRootOf = (agentDir: string) => path.join(agentDir, "..", "..");
|
|
462
462
|
|
|
463
|
+
/** What a snapshot step is told (Part B3). */
|
|
464
|
+
const SNAPSHOT_NOTE =
|
|
465
|
+
"The folder you were sent to work on is in snapshot/ (inside your working directory). Make your changes there, as files: " +
|
|
466
|
+
"when you finish, foldrun turns what changed in snapshot/ into a patch the person applies. " +
|
|
467
|
+
"Only snapshot/ (as that patch) and your outputs/ come back from this step; anything you write elsewhere is not kept. " +
|
|
468
|
+
"Do not create links or a .git folder in snapshot/ — links make the patch refused, and .git is left out.";
|
|
469
|
+
|
|
463
470
|
/**
|
|
464
471
|
* Configuration from AGENTS.md, nearest-wins: the workspace's overrides the
|
|
465
472
|
* account's, key by key.
|
|
@@ -2904,6 +2911,17 @@ async function runStep(
|
|
|
2904
2911
|
if (stepHookNames.length) push("info", `hooks: ${stepHookNames.join(", ")}`);
|
|
2905
2912
|
|
|
2906
2913
|
const isolation = process.env.FOLDRUN_RUN_ISOLATION;
|
|
2914
|
+
// A snapshot run (Part B3): the folder is on the run record, set when the
|
|
2915
|
+
// run was made. Only an executor that says it can hold one may run it —
|
|
2916
|
+
// in-process, the step would work in the real workspace folder, and no
|
|
2917
|
+
// copy-back stands between what it writes and the workspace (Codex).
|
|
2918
|
+
const snapshotId = runId ? readRun(tenant, path.basename(workspaceRootOf(agentDir)), runId)?.snapshot?.id : undefined;
|
|
2919
|
+
if (snapshotId) {
|
|
2920
|
+
const able = isolatedRun() && isolation !== "container" && Boolean(platform.isolation[isolation!]) && platform.holdsSnapshot?.(isolation!) === true;
|
|
2921
|
+
if (!able) {
|
|
2922
|
+
throw new Error("this run has a snapshot (a folder sent to work on), and this install cannot hold one: it needs the k8s sandbox (FOLDRUN_RUN_ISOLATION=k8s). Nothing was run.");
|
|
2923
|
+
}
|
|
2924
|
+
}
|
|
2907
2925
|
if (isolatedRun()) {
|
|
2908
2926
|
// The isolated path: the whole loop — model, built-in tools, scripts —
|
|
2909
2927
|
// runs inside a throwaway container (a docker sibling, or a pod), and
|
|
@@ -3285,6 +3303,7 @@ async function runStep(
|
|
|
3285
3303
|
const runIsolated = isolation === "container" ? runStepInContainer : platform.isolation[isolation!]!;
|
|
3286
3304
|
const isolatedArgs = (modelEnv: Record<string, string | undefined>, keyName = MODEL_KEY_NAME) => ({
|
|
3287
3305
|
workspaceRoot,
|
|
3306
|
+
...(snapshotId ? { snapshot: { id: snapshotId } } : {}),
|
|
3288
3307
|
libraryRoot: libraryDir(tenant),
|
|
3289
3308
|
galleryRoot: platform.galleryDir() ?? undefined,
|
|
3290
3309
|
// Keys this step's dependency cache. Per-account by design — see
|
|
@@ -3294,7 +3313,8 @@ async function runStep(
|
|
|
3294
3313
|
agentRel: path.relative(workspaceRoot, agentDir).replaceAll("\\", "/"),
|
|
3295
3314
|
// Its file tools inside the sandbox get the same allowance (FOL-96).
|
|
3296
3315
|
configWrites: step.approve === true && Boolean(step.approvedAt),
|
|
3297
|
-
|
|
3316
|
+
// A snapshot step is told where its folder is, and what comes back.
|
|
3317
|
+
prompt: snapshotId ? `${prompt}\n\n${SNAPSHOT_NOTE}` : prompt,
|
|
3298
3318
|
model,
|
|
3299
3319
|
effort,
|
|
3300
3320
|
systemPrompt,
|
|
@@ -4499,6 +4519,9 @@ export function createFlowRun(
|
|
|
4499
4519
|
/** An A2A task's correlation (foldrun-platform flow-admission.ts) — its
|
|
4500
4520
|
* own trailing argument, so the signature only widened. */
|
|
4501
4521
|
a2a?: { task: string; key: string },
|
|
4522
|
+
/** A snapshot run's folder (Part B3): the id POST …/snapshots answered.
|
|
4523
|
+
* Its own trailing argument, as a2a is, so the signature only widened. */
|
|
4524
|
+
snapshot?: string,
|
|
4502
4525
|
): RunRecord {
|
|
4503
4526
|
let flowMaxOut: number | undefined;
|
|
4504
4527
|
try {
|
|
@@ -4515,6 +4538,7 @@ export function createFlowRun(
|
|
|
4515
4538
|
...(opts.test ? { test: true } : {}),
|
|
4516
4539
|
// An outside agent asked, over A2A: the task it serves, and whose key.
|
|
4517
4540
|
...(a2a ? { trigger: "a2a" as const, a2a: { task: a2a.task, key: a2a.key } } : {}),
|
|
4541
|
+
...(snapshot ? { snapshot: { id: snapshot } } : {}),
|
|
4518
4542
|
startedAt: new Date().toISOString(),
|
|
4519
4543
|
finishedAt: null,
|
|
4520
4544
|
steps: steps.map((s) => ({
|
package/src/scan.ts
CHANGED
|
@@ -481,6 +481,9 @@ const SECRET_SHAPES: [RegExp, string][] = [
|
|
|
481
481
|
[/\bnpm_[A-Za-z0-9]{36}\b/g, "an npm token"],
|
|
482
482
|
[/\bhf_[A-Za-z0-9]{34,}\b/g, "a Hugging Face token"],
|
|
483
483
|
[/\blin_api_[A-Za-z0-9]{40}\b/g, "a Linear key"],
|
|
484
|
+
// foldrun's own (FOL-154): API, gateway, A2A and deploy keys, and the
|
|
485
|
+
// prefixes before them. 48 hex: the exact shape minted, never `fr_api_…`.
|
|
486
|
+
[/\b(?:fr_(?:api|gw|a2a|dep)|md[ak])_[0-9a-f]{48}\b/g, "a foldrun key"],
|
|
484
487
|
[/\bdop_v1_[a-f0-9]{64}\b/g, "a DigitalOcean token"],
|
|
485
488
|
[/\bshp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}\b/g, "a Shopify token"],
|
|
486
489
|
[/\bre_[A-Za-z0-9]{8,}_[A-Za-z0-9]{16,}\b/g, "a Resend key"],
|
|
@@ -679,6 +682,33 @@ export function untrustedTextFindings(text: string): { rule: string; line: numbe
|
|
|
679
682
|
return out;
|
|
680
683
|
}
|
|
681
684
|
|
|
685
|
+
/**
|
|
686
|
+
* Secrets in files about to LEAVE this machine (a snapshot upload, Part B3,
|
|
687
|
+
* 10 Nov 2026): every file, with nothing the files themselves can turn off —
|
|
688
|
+
* no foldrun-ignore, no vendored-path skip (Codex: a repo comment must not
|
|
689
|
+
* silence a key on its way out). A file that is not text (a NUL in its
|
|
690
|
+
* first 8 KB) cannot be read for secrets and is returned as unscanned, for
|
|
691
|
+
* the caller to say so rather than pass it quietly.
|
|
692
|
+
*/
|
|
693
|
+
export function uploadSecretFindings(files: ScanFile[]): { findings: { rule: string; file: string; line?: number; message: string }[]; unscanned: string[] } {
|
|
694
|
+
const findings: { rule: string; file: string; line?: number; message: string }[] = [];
|
|
695
|
+
const unscanned: string[] = [];
|
|
696
|
+
const seen = new Set<string>();
|
|
697
|
+
for (const f of files) {
|
|
698
|
+
if (f.content.slice(0, 8000).includes("\u0000")) {
|
|
699
|
+
unscanned.push(f.path);
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
scanSecrets(f, f.content.split(/\r?\n/), (rule, file, line, _text, message) => {
|
|
703
|
+
const key = `${rule}\0${file}\0${line ?? ""}`;
|
|
704
|
+
if (seen.has(key)) return;
|
|
705
|
+
seen.add(key);
|
|
706
|
+
findings.push({ rule, file, ...(line ? { line } : {}), message });
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
return { findings, unscanned };
|
|
710
|
+
}
|
|
711
|
+
|
|
682
712
|
/** The text hidden in tag characters: each is U+E0000 + an ASCII code. */
|
|
683
713
|
function decodeTags(s: string): string {
|
|
684
714
|
let out = "";
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Snapshot archives (Part B3, 10 Nov 2026): a folder from the user's machine,
|
|
2
|
+
// as one gzipped tar, for an agent on foldrun to work on.
|
|
3
|
+
//
|
|
4
|
+
// One writer and one strict reader, here, so the CLI (which packs) and the
|
|
5
|
+
// platform (which unpacks what a stranger sent) hold the same rules:
|
|
6
|
+
//
|
|
7
|
+
// - only regular files and directories — no links, devices, FIFOs, sparse
|
|
8
|
+
// files, PAX or GNU long-name headers (each a way to make a path mean
|
|
9
|
+
// something other than it says);
|
|
10
|
+
// - every path relative, `/`-separated, no `.`/`..`/empty segment, no
|
|
11
|
+
// backslash, NUL or control character, ≤ 255 bytes and ≤ 32 levels;
|
|
12
|
+
// - no path twice, also when only its case differs, and no file where a
|
|
13
|
+
// directory is (`a` and `a/b`);
|
|
14
|
+
// - limits on file count, one file's size and the whole, enforced while
|
|
15
|
+
// reading — before a byte past them is held;
|
|
16
|
+
// - header checksums verified; nothing after the end-of-archive blocks;
|
|
17
|
+
// - modes are 0644 or 0755 (the owner's exec bit), nothing else kept.
|
|
18
|
+
//
|
|
19
|
+
// The writer is deterministic: entries sorted, mtime 0, uid/gid 0, no names
|
|
20
|
+
// — the same folder is the same bytes, so a snapshot is known by its hash.
|
|
21
|
+
|
|
22
|
+
import crypto from "node:crypto";
|
|
23
|
+
import zlib from "node:zlib";
|
|
24
|
+
|
|
25
|
+
export interface SnapshotEntry {
|
|
26
|
+
/** Relative, `/`-separated. */
|
|
27
|
+
path: string;
|
|
28
|
+
/** 0o644 or 0o755. */
|
|
29
|
+
mode: number;
|
|
30
|
+
content: Buffer;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SnapshotLimits {
|
|
34
|
+
maxFiles: number;
|
|
35
|
+
maxFileBytes: number;
|
|
36
|
+
maxTotalBytes: number;
|
|
37
|
+
maxCompressedBytes: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const SNAPSHOT_LIMITS: SnapshotLimits = {
|
|
41
|
+
maxFiles: 20_000,
|
|
42
|
+
maxFileBytes: 10 * 1024 * 1024,
|
|
43
|
+
maxTotalBytes: 200 * 1024 * 1024,
|
|
44
|
+
maxCompressedBytes: 50 * 1024 * 1024,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const MAX_PATH = 255;
|
|
48
|
+
const MAX_DEPTH = 32;
|
|
49
|
+
|
|
50
|
+
/** Why an archive was refused, in words a person can act on. */
|
|
51
|
+
export class SnapshotRefused extends Error {
|
|
52
|
+
constructor(message: string) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "SnapshotRefused";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The path rules, for the packer to check before it packs, and the reader after. */
|
|
59
|
+
export function snapshotPathProblem(p: string): string | null {
|
|
60
|
+
if (!p) return "an empty path";
|
|
61
|
+
if (Buffer.byteLength(p) > MAX_PATH) return `a path longer than ${MAX_PATH} bytes`;
|
|
62
|
+
if (p.startsWith("/")) return "an absolute path";
|
|
63
|
+
if (/[\u0000-\u001f\u007f\\]/.test(p)) return "a backslash or control character in a path";
|
|
64
|
+
const parts = p.split("/");
|
|
65
|
+
if (parts.length > MAX_DEPTH) return `a path deeper than ${MAX_DEPTH} folders`;
|
|
66
|
+
if (parts.some((s) => s === "" || s === "." || s === "..")) return "an empty, `.` or `..` segment";
|
|
67
|
+
// The files, never a repository: a `.git` sent in could become git's own
|
|
68
|
+
// configuration (hooks, fsmonitor) wherever git later reads the folder.
|
|
69
|
+
if (parts.some((s) => s.toLowerCase() === ".git")) return "a `.git` folder or file — send the files, not the repository";
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The set of paths in one archive, with every clash rule in one place for the
|
|
75
|
+
* writer and the reader alike (Codex: the writer packed what the reader then
|
|
76
|
+
* refused). Case-insensitive throughout: `A` and `a/b` cannot both exist on
|
|
77
|
+
* a case-insensitive filesystem, so neither may be here.
|
|
78
|
+
*/
|
|
79
|
+
class PathSet {
|
|
80
|
+
private kinds = new Map<string, "file" | "dir">();
|
|
81
|
+
private parents = new Set<string>();
|
|
82
|
+
add(p: string, isDir: boolean): void {
|
|
83
|
+
const key = p.toLowerCase();
|
|
84
|
+
if (this.kinds.has(key)) throw new SnapshotRefused(`${p}: there twice (paths may not differ only by case)`);
|
|
85
|
+
const segs = key.split("/");
|
|
86
|
+
for (let i = 1; i < segs.length; i++) {
|
|
87
|
+
const parent = segs.slice(0, i).join("/");
|
|
88
|
+
if (this.kinds.get(parent) === "file") throw new SnapshotRefused(`${p}: inside ${p.split("/").slice(0, i).join("/")}, which is a file`);
|
|
89
|
+
}
|
|
90
|
+
if (!isDir && this.parents.has(key)) throw new SnapshotRefused(`${p}: a file where another entry makes it a folder`);
|
|
91
|
+
this.kinds.set(key, isDir ? "dir" : "file");
|
|
92
|
+
for (let i = 1; i < segs.length; i++) this.parents.add(segs.slice(0, i).join("/"));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ------------------------------------------------------------------ writer
|
|
97
|
+
|
|
98
|
+
function octal(n: number, width: number): string {
|
|
99
|
+
return n.toString(8).padStart(width - 1, "0") + "\0";
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function header(path: string, size: number, mode: number, type: "0" | "5"): Buffer {
|
|
103
|
+
const h = Buffer.alloc(512, 0);
|
|
104
|
+
// ustar: name (100) and prefix (155); split at a "/" when it is long.
|
|
105
|
+
let name = path;
|
|
106
|
+
let prefix = "";
|
|
107
|
+
if (Buffer.byteLength(name) > 100) {
|
|
108
|
+
const cut = path.lastIndexOf("/", path.length - 1);
|
|
109
|
+
let at = -1;
|
|
110
|
+
for (let i = cut; i > 0; i = path.lastIndexOf("/", i - 1)) {
|
|
111
|
+
if (Buffer.byteLength(path.slice(i + 1)) <= 100 && Buffer.byteLength(path.slice(0, i)) <= 155) {
|
|
112
|
+
at = i;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (at < 0) throw new SnapshotRefused(`${path}: too long to store`);
|
|
117
|
+
prefix = path.slice(0, at);
|
|
118
|
+
name = path.slice(at + 1);
|
|
119
|
+
}
|
|
120
|
+
h.write(name, 0, 100, "utf8");
|
|
121
|
+
h.write(octal(mode, 8), 100, 8, "ascii");
|
|
122
|
+
h.write(octal(0, 8), 108, 8, "ascii"); // uid
|
|
123
|
+
h.write(octal(0, 8), 116, 8, "ascii"); // gid
|
|
124
|
+
h.write(octal(size, 12), 124, 12, "ascii");
|
|
125
|
+
h.write(octal(0, 12), 136, 12, "ascii"); // mtime
|
|
126
|
+
h.write(" ", 148, 8, "ascii"); // checksum placeholder
|
|
127
|
+
h.write(type, 156, 1, "ascii");
|
|
128
|
+
h.write("ustar\u000000", 257, 8, "ascii");
|
|
129
|
+
h.write(prefix, 345, 155, "utf8");
|
|
130
|
+
let sum = 0;
|
|
131
|
+
for (const b of h) sum += b;
|
|
132
|
+
h.write(sum.toString(8).padStart(6, "0") + "\0 ", 148, 8, "ascii");
|
|
133
|
+
return h;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** A deterministic gzipped tar of these files (directories are implied). */
|
|
137
|
+
export function packSnapshot(entries: SnapshotEntry[], limits: SnapshotLimits = SNAPSHOT_LIMITS): { archive: Buffer; sha256: string; files: number; bytes: number } {
|
|
138
|
+
if (entries.length > limits.maxFiles) throw new SnapshotRefused(`${entries.length} files — the most is ${limits.maxFiles}`);
|
|
139
|
+
const sorted = [...entries].sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
140
|
+
const parts: Buffer[] = [];
|
|
141
|
+
let total = 0;
|
|
142
|
+
const paths = new PathSet();
|
|
143
|
+
for (const e of sorted) {
|
|
144
|
+
const problem = snapshotPathProblem(e.path);
|
|
145
|
+
if (problem) throw new SnapshotRefused(`${e.path}: ${problem}`);
|
|
146
|
+
paths.add(e.path, false);
|
|
147
|
+
if (e.content.length > limits.maxFileBytes) throw new SnapshotRefused(`${e.path}: ${e.content.length} bytes — the most for one file is ${limits.maxFileBytes}`);
|
|
148
|
+
total += e.content.length;
|
|
149
|
+
if (total > limits.maxTotalBytes) throw new SnapshotRefused(`more than ${limits.maxTotalBytes} bytes in all`);
|
|
150
|
+
parts.push(header(e.path, e.content.length, e.mode === 0o755 ? 0o755 : 0o644, "0"));
|
|
151
|
+
parts.push(e.content);
|
|
152
|
+
const pad = (512 - (e.content.length % 512)) % 512;
|
|
153
|
+
if (pad) parts.push(Buffer.alloc(pad, 0));
|
|
154
|
+
}
|
|
155
|
+
parts.push(Buffer.alloc(1024, 0));
|
|
156
|
+
const archive = zlib.gzipSync(Buffer.concat(parts), { level: 9, mtime: 0 } as zlib.ZlibOptions);
|
|
157
|
+
// gzip's header carries an mtime and an OS byte; both fixed, so the bytes are the folder's alone.
|
|
158
|
+
archive.writeUInt32LE(0, 4);
|
|
159
|
+
archive[9] = 255;
|
|
160
|
+
if (archive.length > limits.maxCompressedBytes) throw new SnapshotRefused(`${archive.length} bytes compressed — the most is ${limits.maxCompressedBytes}`);
|
|
161
|
+
return { archive, sha256: crypto.createHash("sha256").update(archive).digest("hex"), files: sorted.length, bytes: total };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ------------------------------------------------------------------ reader
|
|
165
|
+
|
|
166
|
+
function readString(b: Buffer, start: number, len: number): string {
|
|
167
|
+
const s = b.subarray(start, start + len);
|
|
168
|
+
const nul = s.indexOf(0);
|
|
169
|
+
return s.subarray(0, nul < 0 ? len : nul).toString("utf8");
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function readOctal(b: Buffer, start: number, len: number, what: string): number {
|
|
173
|
+
const raw = readString(b, start, len).trim();
|
|
174
|
+
if (raw === "") return 0;
|
|
175
|
+
if (!/^[0-7]+$/.test(raw)) throw new SnapshotRefused(`a header's ${what} is not a number`);
|
|
176
|
+
const n = parseInt(raw, 8);
|
|
177
|
+
if (!Number.isSafeInteger(n)) throw new SnapshotRefused(`a header's ${what} is out of range`);
|
|
178
|
+
return n;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Unpack an archive a stranger sent, under every rule above. Throws
|
|
183
|
+
* SnapshotRefused naming the first thing wrong; nothing is written anywhere —
|
|
184
|
+
* the caller gets the entries to store.
|
|
185
|
+
*/
|
|
186
|
+
export function unpackSnapshot(archive: Buffer, limits: SnapshotLimits = SNAPSHOT_LIMITS): { entries: SnapshotEntry[]; sha256: string; bytes: number } {
|
|
187
|
+
if (archive.length > limits.maxCompressedBytes) throw new SnapshotRefused(`${archive.length} bytes compressed — the most is ${limits.maxCompressedBytes}`);
|
|
188
|
+
let tar: Buffer;
|
|
189
|
+
try {
|
|
190
|
+
// The tar is at most the files, their headers and padding: past that, a bomb.
|
|
191
|
+
const ceiling = limits.maxTotalBytes + (limits.maxFiles + 2) * 1024 + 1024;
|
|
192
|
+
tar = zlib.gunzipSync(archive, { maxOutputLength: ceiling });
|
|
193
|
+
} catch (err) {
|
|
194
|
+
const code = (err as { code?: string }).code;
|
|
195
|
+
throw new SnapshotRefused(code === "ERR_BUFFER_TOO_LARGE" || /larger than/i.test(String((err as Error).message)) ? `it unpacks past ${limits.maxTotalBytes} bytes` : "it is not a gzip archive");
|
|
196
|
+
}
|
|
197
|
+
const entries: SnapshotEntry[] = [];
|
|
198
|
+
const paths = new PathSet();
|
|
199
|
+
// Every entry counts against maxFiles, folders too, before its path is
|
|
200
|
+
// recorded: an archive of folders alone would otherwise pass every limit
|
|
201
|
+
// and fill memory with paths (Codex). Our writer emits no folder entries.
|
|
202
|
+
let count = 0;
|
|
203
|
+
let total = 0;
|
|
204
|
+
let off = 0;
|
|
205
|
+
let ended = false;
|
|
206
|
+
while (off + 512 <= tar.length) {
|
|
207
|
+
const h = tar.subarray(off, off + 512);
|
|
208
|
+
if (h.every((x) => x === 0)) {
|
|
209
|
+
const next = tar.subarray(off + 512, off + 1024);
|
|
210
|
+
if (next.length === 512 && next.every((x) => x === 0)) {
|
|
211
|
+
ended = true;
|
|
212
|
+
off += 1024;
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
throw new SnapshotRefused("a lone empty block before the end");
|
|
216
|
+
}
|
|
217
|
+
let sum = 0;
|
|
218
|
+
for (let i = 0; i < 512; i++) sum += i >= 148 && i < 156 ? 32 : h[i]!;
|
|
219
|
+
if (readOctal(h, 148, 8, "checksum") !== sum) throw new SnapshotRefused("a header's checksum is wrong");
|
|
220
|
+
if (readString(h, 257, 6) !== "ustar") throw new SnapshotRefused("not a ustar archive");
|
|
221
|
+
const type = String.fromCharCode(h[156]!);
|
|
222
|
+
const name = readString(h, 0, 100);
|
|
223
|
+
const prefix = readString(h, 345, 155);
|
|
224
|
+
const p0 = prefix ? `${prefix}/${name}` : name;
|
|
225
|
+
const isDir = type === "5";
|
|
226
|
+
const p = isDir ? p0.replace(/\/+$/, "") : p0;
|
|
227
|
+
if (type !== "0" && type !== "\0" && !isDir) {
|
|
228
|
+
const what = { "1": "a hard link", "2": "a symbolic link", "3": "a character device", "4": "a block device", "6": "a FIFO", x: "an extended (PAX) header", g: "a global PAX header", L: "a GNU long name", K: "a GNU long link", S: "a sparse file" }[type] ?? `an entry of type ${JSON.stringify(type)}`;
|
|
229
|
+
throw new SnapshotRefused(`${p || "(unnamed)"}: ${what} — only plain files and folders`);
|
|
230
|
+
}
|
|
231
|
+
const problem = snapshotPathProblem(p);
|
|
232
|
+
if (problem) throw new SnapshotRefused(`${p}: ${problem}`);
|
|
233
|
+
const size = readOctal(h, 124, 12, "size");
|
|
234
|
+
if (isDir && size !== 0) throw new SnapshotRefused(`${p}: a folder with a size`);
|
|
235
|
+
if (size > limits.maxFileBytes) throw new SnapshotRefused(`${p}: ${size} bytes — the most for one file is ${limits.maxFileBytes}`);
|
|
236
|
+
if (++count > limits.maxFiles) throw new SnapshotRefused(`more than ${limits.maxFiles} entries`);
|
|
237
|
+
paths.add(p, isDir);
|
|
238
|
+
off += 512;
|
|
239
|
+
if (!isDir) {
|
|
240
|
+
if (off + size > tar.length) throw new SnapshotRefused(`${p}: cut short`);
|
|
241
|
+
total += size;
|
|
242
|
+
if (total > limits.maxTotalBytes) throw new SnapshotRefused(`more than ${limits.maxTotalBytes} bytes in all`);
|
|
243
|
+
if (entries.length + 1 > limits.maxFiles) throw new SnapshotRefused(`more than ${limits.maxFiles} files`);
|
|
244
|
+
const mode = readOctal(h, 100, 8, "mode");
|
|
245
|
+
entries.push({ path: p, mode: mode & 0o100 ? 0o755 : 0o644, content: Buffer.from(tar.subarray(off, off + size)) });
|
|
246
|
+
off += size + ((512 - (size % 512)) % 512);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (!ended) throw new SnapshotRefused("no end-of-archive marker");
|
|
250
|
+
if (tar.subarray(off).some((x) => x !== 0)) throw new SnapshotRefused("data after the end of the archive");
|
|
251
|
+
return { entries, sha256: crypto.createHash("sha256").update(archive).digest("hex"), bytes: total };
|
|
252
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -4002,6 +4002,11 @@ export interface RunRecord {
|
|
|
4002
4002
|
* first id, kept across rotations) that asked. A correlation only — the
|
|
4003
4003
|
* protocol's own state lives in the platform's a2a_tasks. */
|
|
4004
4004
|
a2a?: { task: string; key: string };
|
|
4005
|
+
/** A snapshot run (Part B3): the folder a person sent (its sha256), staged
|
|
4006
|
+
* for the step at agents/<name>/snapshot/; the patch of what the step
|
|
4007
|
+
* changed is outputs/snapshot.patch. Set when the run is made, never by a
|
|
4008
|
+
* step. */
|
|
4009
|
+
snapshot?: { id: string };
|
|
4005
4010
|
steps: StepRecord[];
|
|
4006
4011
|
}
|
|
4007
4012
|
|