barebox 0.1.0 → 0.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/CHANGELOG.md +97 -0
- package/README.md +152 -7
- package/barebox.context.md +144 -0
- package/package.json +40 -3
- package/primitives.json +59 -0
- package/src/certs.js +109 -0
- package/src/cli.js +361 -0
- package/src/holds.js +476 -0
- package/src/index.js +5 -0
- package/src/launcher.js +272 -0
- package/src/overlay-walk.js +49 -0
- package/src/overlay.js +1012 -0
- package/src/proxy.js +572 -0
- package/src/run.js +226 -0
- package/src/runtime.js +263 -0
- package/src/wall.js +156 -0
- package/types/certs.d.ts +26 -0
- package/types/cli.d.ts +2 -0
- package/types/holds.d.ts +248 -0
- package/types/index.d.ts +4 -0
- package/types/launcher.d.ts +53 -0
- package/types/overlay-walk.d.ts +1 -0
- package/types/overlay.d.ts +220 -0
- package/types/proxy.d.ts +204 -0
- package/types/run.d.ts +192 -0
- package/types/runtime.d.ts +112 -0
- package/types/wall.d.ts +30 -0
- package/.claude/remember/AGENT_RULES.md +0 -291
- package/docs/product/agent-sandbox-prd.md +0 -418
- package/docs/product/agent-sandbox.md +0 -218
- package/poc/m0/README.md +0 -20
- package/poc/m0/func-fx.txt +0 -15
- package/poc/m0/func-fy.txt +0 -15
- package/poc/m0/launch.sh +0 -43
- package/poc/m0/measure.js +0 -53
- package/poc/m0/measure.txt +0 -23
- package/poc/m0/proxy.js +0 -74
- package/poc/m0/proxy.log +0 -28
- package/poc/m0/red-mem.txt +0 -4
- package/poc/m0/red-proxy.log +0 -13
- package/poc/m0/redteam.sh +0 -118
- package/poc/m0/run-m0.sh +0 -136
package/types/holds.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
3
|
+
* the queue itself never leaves this module. No body: returning whole 1 MB
|
|
4
|
+
* bodies from a status call is never needed, and the ask payload in
|
|
5
|
+
* `asks/<id>.json` carries the preview.
|
|
6
|
+
*
|
|
7
|
+
* @typedef {object} HeldRequest
|
|
8
|
+
* @property {string} id hold id (`h<n>`)
|
|
9
|
+
* @property {string} agent agent name (its socket), never `c<n>`
|
|
10
|
+
* @property {string} method HTTP method
|
|
11
|
+
* @property {string} url full URL
|
|
12
|
+
* @property {string} rule hold-reason string from the decision function
|
|
13
|
+
* @property {number} waitingSec seconds since the hold parked
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* A queue event, as delivered to the `onEvent` callback: `{ type: 'ask' }`
|
|
17
|
+
* when a hold parks, `{ type: 'decided' }` when it settles or its client
|
|
18
|
+
* goes away (`deny` / `client gone`): every ask ends in exactly one.
|
|
19
|
+
*
|
|
20
|
+
* @typedef {{ type: 'ask', ask: AskPayload } | { type: 'decided', id: string, decision: 'allow' | 'deny', reason?: string }} HoldEvent
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Queue event callback.
|
|
24
|
+
*
|
|
25
|
+
* @typedef {(e: HoldEvent) => void} OnEvent
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* The decision-ready ask written to `asks/<id>.json`.
|
|
29
|
+
*
|
|
30
|
+
* @typedef {object} AskPayload
|
|
31
|
+
* @property {string} id hold id (`h<n>`)
|
|
32
|
+
* @property {string} agent agent name (its socket)
|
|
33
|
+
* @property {string} method HTTP method
|
|
34
|
+
* @property {string} url full URL
|
|
35
|
+
* @property {string} rule hold-reason string from the decision function
|
|
36
|
+
* @property {string} letter always `'x'` until M3 owns letters
|
|
37
|
+
* @property {string} bodyPreview request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
38
|
+
* @property {boolean} truncated true when the body exceeded ASK_BODY_LIMIT
|
|
39
|
+
* @property {number} waitingSec 0 at park; readers use the file mtime for a live value
|
|
40
|
+
* @property {string|null} expiresAt ISO-8601 UTC deadline, or null when `timeoutMs: 'never'`
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Decide a parked ask. Resolves `{ decision: 'allow' | 'deny', reason? }`.
|
|
44
|
+
*
|
|
45
|
+
* @typedef {(ask: AskPayload) => Promise<{ decision: string, reason?: string }>} OnAsk
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* Decide whether a request parks. Return a human-readable reason, or null
|
|
49
|
+
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
|
50
|
+
*
|
|
51
|
+
* @typedef {(req: { agent: string, method: string, url: string, headers: Record<string, string | string[] | undefined> }) => string | null} ShouldHold
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Atomic write: tmp file + rename, so readers never see a partial payload.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} file destination path
|
|
57
|
+
* @param {string} text content
|
|
58
|
+
* @returns {void}
|
|
59
|
+
*/
|
|
60
|
+
export function writeFileAtomic(file: string, text: string): void;
|
|
61
|
+
/**
|
|
62
|
+
* The one normalization of decisions, applied once wherever a decision
|
|
63
|
+
* enters (an answer file, or an `onAsk` result). The audit log speaks
|
|
64
|
+
* `allow`/`deny`, which is bareguard's vocabulary.
|
|
65
|
+
*
|
|
66
|
+
* @param {{ decision?: unknown, reason?: unknown } | null} input raw decision pair
|
|
67
|
+
* @param {object} [opts]
|
|
68
|
+
* @param {boolean} [opts.approveAlias] accept `'approve'` as an alias of `'allow'` (answer files only)
|
|
69
|
+
* @returns {{ decision: 'allow' | 'deny', reason?: string } | null} normalized, or null when not a decision at all
|
|
70
|
+
*/
|
|
71
|
+
export function normalizeDecision(input: {
|
|
72
|
+
decision?: unknown;
|
|
73
|
+
reason?: unknown;
|
|
74
|
+
} | null, { approveAlias }?: {
|
|
75
|
+
approveAlias?: boolean | undefined;
|
|
76
|
+
}): {
|
|
77
|
+
decision: "allow" | "deny";
|
|
78
|
+
reason?: string;
|
|
79
|
+
} | null;
|
|
80
|
+
/**
|
|
81
|
+
* Create the hold queue. One queue per runtime, enforced by `claimRuntime`
|
|
82
|
+
* in `createProxy` (a second live proxy on the same runtime never starts):
|
|
83
|
+
* on the file channel the queue owns `asks/` and `answers/` for its
|
|
84
|
+
* runtime, clearing both at start and removing its asks on close, so no
|
|
85
|
+
* answer outlives its queue.
|
|
86
|
+
*
|
|
87
|
+
* @param {object} opts
|
|
88
|
+
* @param {(o: Record<string, unknown>) => void} opts.log audit-log writer
|
|
89
|
+
* @param {ShouldHold} [opts.shouldHold] decision function (default: path-substring `match`)
|
|
90
|
+
* @param {string} [opts.match] path substring that parks a request (default impl when `shouldHold` is absent)
|
|
91
|
+
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
92
|
+
* @param {number} [opts.maxBody] bodies over this are denied at the door, never queued (default 1048576)
|
|
93
|
+
* @param {string|null} [opts.runtime] runtime dir; required for the default file checkpoint (writes `asks/`, polls `answers/`)
|
|
94
|
+
* @param {OnAsk|null} [opts.onAsk] ask seam (bareguard `humanChannel` shape); when present no files are involved
|
|
95
|
+
* @param {number} [opts.pollMs] answer poll interval in ms for the default file checkpoint (default 250, PRD Q9)
|
|
96
|
+
* @param {OnEvent|null} [opts.onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon
|
|
97
|
+
* @param {(entry: object) => void} opts.onApprove forward the parked request upstream
|
|
98
|
+
* @param {(entry: object, reason: string) => void} opts.onDeny refuse the parked request
|
|
99
|
+
* @returns {{ add: (entry: object) => string, decide: (req: { agent: string, method: string, url: string, headers: Record<string, string | string[] | undefined> }) => string | null, abandon: (id: string) => void, abandonSocket: (sock: object) => void, pending: () => HeldRequest[], close: () => void }} queue handle
|
|
100
|
+
*/
|
|
101
|
+
export function createHolds({ log, shouldHold, match, timeoutMs, maxBody, runtime, onAsk, pollMs, onEvent, onApprove, onDeny }: {
|
|
102
|
+
log: (o: Record<string, unknown>) => void;
|
|
103
|
+
shouldHold?: ShouldHold | undefined;
|
|
104
|
+
match?: string | undefined;
|
|
105
|
+
timeoutMs?: string | number | undefined;
|
|
106
|
+
maxBody?: number | undefined;
|
|
107
|
+
runtime?: string | null | undefined;
|
|
108
|
+
onAsk?: OnAsk | null | undefined;
|
|
109
|
+
pollMs?: number | undefined;
|
|
110
|
+
onEvent?: OnEvent | null | undefined;
|
|
111
|
+
onApprove: (entry: object) => void;
|
|
112
|
+
onDeny: (entry: object, reason: string) => void;
|
|
113
|
+
}): {
|
|
114
|
+
add: (entry: object) => string;
|
|
115
|
+
decide: (req: {
|
|
116
|
+
agent: string;
|
|
117
|
+
method: string;
|
|
118
|
+
url: string;
|
|
119
|
+
headers: Record<string, string | string[] | undefined>;
|
|
120
|
+
}) => string | null;
|
|
121
|
+
abandon: (id: string) => void;
|
|
122
|
+
abandonSocket: (sock: object) => void;
|
|
123
|
+
pending: () => HeldRequest[];
|
|
124
|
+
close: () => void;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Default decision: park requests whose path contains `match`.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} match path substring
|
|
130
|
+
* @returns {ShouldHold} decision function
|
|
131
|
+
*/
|
|
132
|
+
export function matchPath(match: string): ShouldHold;
|
|
133
|
+
/** Body preview cap for `asks/<id>.json` (bytes). */
|
|
134
|
+
export const ASK_BODY_LIMIT: 4096;
|
|
135
|
+
/** Default answer poll interval in ms (PRD Q9). */
|
|
136
|
+
export const DEFAULT_POLL_MS: 250;
|
|
137
|
+
/**
|
|
138
|
+
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
139
|
+
* the queue itself never leaves this module. No body: returning whole 1 MB
|
|
140
|
+
* bodies from a status call is never needed, and the ask payload in
|
|
141
|
+
* `asks/<id>.json` carries the preview.
|
|
142
|
+
*/
|
|
143
|
+
export type HeldRequest = {
|
|
144
|
+
/**
|
|
145
|
+
* hold id (`h<n>`)
|
|
146
|
+
*/
|
|
147
|
+
id: string;
|
|
148
|
+
/**
|
|
149
|
+
* agent name (its socket), never `c<n>`
|
|
150
|
+
*/
|
|
151
|
+
agent: string;
|
|
152
|
+
/**
|
|
153
|
+
* HTTP method
|
|
154
|
+
*/
|
|
155
|
+
method: string;
|
|
156
|
+
/**
|
|
157
|
+
* full URL
|
|
158
|
+
*/
|
|
159
|
+
url: string;
|
|
160
|
+
/**
|
|
161
|
+
* hold-reason string from the decision function
|
|
162
|
+
*/
|
|
163
|
+
rule: string;
|
|
164
|
+
/**
|
|
165
|
+
* seconds since the hold parked
|
|
166
|
+
*/
|
|
167
|
+
waitingSec: number;
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* A queue event, as delivered to the `onEvent` callback: `{ type: 'ask' }`
|
|
171
|
+
* when a hold parks, `{ type: 'decided' }` when it settles or its client
|
|
172
|
+
* goes away (`deny` / `client gone`): every ask ends in exactly one.
|
|
173
|
+
*/
|
|
174
|
+
export type HoldEvent = {
|
|
175
|
+
type: "ask";
|
|
176
|
+
ask: AskPayload;
|
|
177
|
+
} | {
|
|
178
|
+
type: "decided";
|
|
179
|
+
id: string;
|
|
180
|
+
decision: "allow" | "deny";
|
|
181
|
+
reason?: string;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Queue event callback.
|
|
185
|
+
*/
|
|
186
|
+
export type OnEvent = (e: HoldEvent) => void;
|
|
187
|
+
/**
|
|
188
|
+
* The decision-ready ask written to `asks/<id>.json`.
|
|
189
|
+
*/
|
|
190
|
+
export type AskPayload = {
|
|
191
|
+
/**
|
|
192
|
+
* hold id (`h<n>`)
|
|
193
|
+
*/
|
|
194
|
+
id: string;
|
|
195
|
+
/**
|
|
196
|
+
* agent name (its socket)
|
|
197
|
+
*/
|
|
198
|
+
agent: string;
|
|
199
|
+
/**
|
|
200
|
+
* HTTP method
|
|
201
|
+
*/
|
|
202
|
+
method: string;
|
|
203
|
+
/**
|
|
204
|
+
* full URL
|
|
205
|
+
*/
|
|
206
|
+
url: string;
|
|
207
|
+
/**
|
|
208
|
+
* hold-reason string from the decision function
|
|
209
|
+
*/
|
|
210
|
+
rule: string;
|
|
211
|
+
/**
|
|
212
|
+
* always `'x'` until M3 owns letters
|
|
213
|
+
*/
|
|
214
|
+
letter: string;
|
|
215
|
+
/**
|
|
216
|
+
* request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
217
|
+
*/
|
|
218
|
+
bodyPreview: string;
|
|
219
|
+
/**
|
|
220
|
+
* true when the body exceeded ASK_BODY_LIMIT
|
|
221
|
+
*/
|
|
222
|
+
truncated: boolean;
|
|
223
|
+
/**
|
|
224
|
+
* 0 at park; readers use the file mtime for a live value
|
|
225
|
+
*/
|
|
226
|
+
waitingSec: number;
|
|
227
|
+
/**
|
|
228
|
+
* ISO-8601 UTC deadline, or null when `timeoutMs: 'never'`
|
|
229
|
+
*/
|
|
230
|
+
expiresAt: string | null;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* Decide a parked ask. Resolves `{ decision: 'allow' | 'deny', reason? }`.
|
|
234
|
+
*/
|
|
235
|
+
export type OnAsk = (ask: AskPayload) => Promise<{
|
|
236
|
+
decision: string;
|
|
237
|
+
reason?: string;
|
|
238
|
+
}>;
|
|
239
|
+
/**
|
|
240
|
+
* Decide whether a request parks. Return a human-readable reason, or null
|
|
241
|
+
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
|
242
|
+
*/
|
|
243
|
+
export type ShouldHold = (req: {
|
|
244
|
+
agent: string;
|
|
245
|
+
method: string;
|
|
246
|
+
url: string;
|
|
247
|
+
headers: Record<string, string | string[] | undefined>;
|
|
248
|
+
}) => string | null;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the CA bundle the sandbox trusts: system roots plus the barebox CA,
|
|
3
|
+
* so public hosts verify as usual and the proxy's minted certs verify too.
|
|
4
|
+
* Only ever reads `caFile` (the CA *cert*); the key stays outside.
|
|
5
|
+
* `SSL_CERT_FILE` *replaces* the system bundle, hence the combine.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} runtime runtime dir the bundle is written into
|
|
8
|
+
* @param {string} caFile barebox CA cert path (from `proxy.caFile`)
|
|
9
|
+
* @returns {string} path of the combined bundle inside `runtime`
|
|
10
|
+
*/
|
|
11
|
+
export function buildCaBundle(runtime: string, caFile: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Start one sandboxed agent. The agent sees a read-only `/usr` and `/etc`,
|
|
14
|
+
* its project and home read-only, the locked drawers masked, no network of
|
|
15
|
+
* its own, and a single front door: anything sent to `localhost:<proxyPort>`
|
|
16
|
+
* is carried out through this agent's socket to the proxy. The agent starts
|
|
17
|
+
* in the project dir (`--chdir proj`), so relative paths resolve there no
|
|
18
|
+
* matter where the caller runs from. The agent always runs in a new session
|
|
19
|
+
* (`--new-session`), detached from the caller's terminal, so it cannot inject
|
|
20
|
+
* keystrokes via TIOCSTI on kernels with `dev.tty.legacy_tiocsti=1`. Its stdin
|
|
21
|
+
* is never the terminal (`stdio: ignore`), so detaching costs nothing.
|
|
22
|
+
*
|
|
23
|
+
* @param {object} opts
|
|
24
|
+
* @param {string} opts.runtime where the runtime tree lives (`socks/` is created inside)
|
|
25
|
+
* @param {string} opts.agent per-agent name; the outside socket is `<runtime>/socks/<agent>.sock` and must already be listening
|
|
26
|
+
* @param {string} opts.proj project directory, bound read-only
|
|
27
|
+
* @param {string[]} opts.command argv to run inside, for example `["python3", "agent.py"]`
|
|
28
|
+
* @param {number} [opts.proxyPort] front-door port inside the sandbox (default 3128)
|
|
29
|
+
* @param {object} [opts.extraEnv] extra environment entries for the inside
|
|
30
|
+
* @param {string|null} [opts.socket] agent socket path, as returned by `proxy.addAgent` (default `<runtime>/socks/<agent>.sock`; the two must agree)
|
|
31
|
+
* @param {string|null} [opts.caFile] barebox CA cert path (from `proxy.caFile`); when set, the sandbox trusts it for HTTPS alongside the system roots
|
|
32
|
+
* @param {boolean} [opts.overlay] when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; diff/commit/rollback address it as `runtimePaths(runtime).overlay(agent)`
|
|
33
|
+
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
34
|
+
* @returns {import('node:child_process').ChildProcess} the bwrap child (stdio piped); `close` gives its exit code
|
|
35
|
+
* @when Reach for this to HARNESS-ONLY start one sandboxed agent run: it creates the sandbox the agent lives in. It does not decide anything; combine with the proxy for the front door and the gate (M3) for policy. Pass overlay:true for a writable tracing-paper run (M4). The agent starts in the project dir.
|
|
36
|
+
* @fails Throws synchronously on a missing/empty `runtime`, `agent`, `proj`, or `command`, on a bad agent name, on a non-integer `proxyPort`, on an over-long socket path, when `socket` disagrees with `<runtime>/socks/<agent>.sock`, when `runtime` IS `HOME` or the project dir (its sockets could not be hidden from the agent), or with overlay on when the runtime sits inside the project or uncommitted changes linger. The child fails at runtime (nonzero exit) when `bwrap` or `socat` is missing inside, or when nothing listens on the agent socket.
|
|
37
|
+
* @example
|
|
38
|
+
* import { launch } from "barebox";
|
|
39
|
+
* const child = launch({ runtime: "/tmp/sbx", agent: "fx", proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
40
|
+
* await new Promise((r) => child.on("close", r)); // child.exitCode === 0
|
|
41
|
+
*/
|
|
42
|
+
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile, overlay, overlayIgnore }: {
|
|
43
|
+
runtime: string;
|
|
44
|
+
agent: string;
|
|
45
|
+
proj: string;
|
|
46
|
+
command: string[];
|
|
47
|
+
proxyPort?: number | undefined;
|
|
48
|
+
extraEnv?: object;
|
|
49
|
+
socket?: string | null | undefined;
|
|
50
|
+
caFile?: string | null | undefined;
|
|
51
|
+
overlay?: boolean | undefined;
|
|
52
|
+
overlayIgnore?: string[] | undefined;
|
|
53
|
+
}): import("node:child_process").ChildProcess;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Judge `bwrap --version` output for overlay support: `--overlay-src` / `--overlay` exist from bubblewrap 0.11.0.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} versionOutput text printed by `bwrap --version` (for example `bubblewrap 0.12.0`)
|
|
5
|
+
* @returns {Error|null} null when the version is at least 0.11.0, otherwise the refusal to throw (unparseable output counts as `unknown`)
|
|
6
|
+
* @fails Never throws; returns the error instead.
|
|
7
|
+
*/
|
|
8
|
+
export function overlaySupportError(versionOutput: string): Error | null;
|
|
9
|
+
/**
|
|
10
|
+
* Refuse the overlay on a bubblewrap older than 0.11.0. Runs `bwrap --version` once per process.
|
|
11
|
+
*
|
|
12
|
+
* @returns {void}
|
|
13
|
+
* @fails Throws `the overlay needs bubblewrap >= 0.11.0 (found X)` when bwrap is older or its version is unreadable. A missing bwrap does not throw here: the real bwrap call then fails as it always did.
|
|
14
|
+
*/
|
|
15
|
+
export function assertOverlaySupport(): void;
|
|
16
|
+
/** The one walker: project, upper, merged view. Skips ignored/commit dirs (never descended); strict UTF-8 names (non-UTF-8 refused loudly); unreadable entries are refused loudly (never chmodded readable).
|
|
17
|
+
*
|
|
18
|
+
* @param {string} root directory to walk
|
|
19
|
+
* @param {Set<string>} ignore path segments invisible to the layer
|
|
20
|
+
* @param {Set<string>|null} [hashOnly] when set, only these relative paths get full entries (hashes, link targets); every other path is still `lstat`ed but records type + mode only
|
|
21
|
+
* @returns {Map<string, {type: string, mode: number, hash?: string, target?: string}>}
|
|
22
|
+
*/
|
|
23
|
+
export function walkLocal(root: string, ignore: Set<string>, hashOnly?: Set<string> | null): Map<string, {
|
|
24
|
+
type: string;
|
|
25
|
+
mode: number;
|
|
26
|
+
hash?: string;
|
|
27
|
+
target?: string;
|
|
28
|
+
}>;
|
|
29
|
+
/** Prepare a fresh upper: refuse leftovers, snapshot the project as the conflict baseline. An empty upper re-snapshots. */
|
|
30
|
+
export function setupOverlay({ runtime, agent, proj, ignore }: {
|
|
31
|
+
runtime: any;
|
|
32
|
+
agent: any;
|
|
33
|
+
proj: any;
|
|
34
|
+
ignore?: string[] | undefined;
|
|
35
|
+
}): {
|
|
36
|
+
upper: string;
|
|
37
|
+
work: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Review one run's tracing paper: every file the sandbox added, modified or deleted, each flagging a project change made after the run started.
|
|
41
|
+
*
|
|
42
|
+
* @param {object} opts
|
|
43
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
44
|
+
* @param {string} opts.agent agent name whose upper to read
|
|
45
|
+
* @returns {OverlayChange[]} one entry per added/modified/deleted path, each flagging a mid-run project change
|
|
46
|
+
* @when Reach for this to review one run's tracing paper: it lists every file the sandboxed run added, modified or deleted, flagging the ones the project changed meanwhile.
|
|
47
|
+
* @fails Throws when no overlay exists for the agent, when the project is gone, when a leftover commit folder blocks, when bubblewrap is older than 0.11.0, or when the overlay view cannot be mounted; never writes anything.
|
|
48
|
+
* @example
|
|
49
|
+
* import { diffOverlay } from "barebox";
|
|
50
|
+
* const changes = diffOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
51
|
+
* for (const c of changes) console.log(c.status, c.path, c.conflict ? "(CONFLICT)" : "");
|
|
52
|
+
*/
|
|
53
|
+
export function diffOverlay({ runtime, agent }: {
|
|
54
|
+
runtime: string;
|
|
55
|
+
agent: string;
|
|
56
|
+
}): OverlayChange[];
|
|
57
|
+
/** Fingerprint one project path now, or null when absent. ENOTDIR also reads as absent: a non-dir prefix (a file becoming a folder) cannot hold the path. */
|
|
58
|
+
export function fpNow(abs: any): string | null;
|
|
59
|
+
/**
|
|
60
|
+
* First path in `paths` whose live fingerprint differs from the snapshot
|
|
61
|
+
* (absent reads as null both sides), or null when nothing moved. A project
|
|
62
|
+
* edit landing between the diff and the swap would otherwise be silently
|
|
63
|
+
* overwritten, so `commitOverlay` refuses when this returns a path.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} proj project directory
|
|
66
|
+
* @param {string[]} paths project-relative paths about to change
|
|
67
|
+
* @param {Map<string, string|null>} lowerFps snapshot fingerprints from the diff
|
|
68
|
+
* @returns {string|null} the first changed path, or null
|
|
69
|
+
*/
|
|
70
|
+
export function changedSince(proj: string, paths: string[], lowerFps: Map<string, string | null>): string | null;
|
|
71
|
+
/** One journal entry (the single shape): a phase-2 step actually taken; undo runs these in reverse.
|
|
72
|
+
*
|
|
73
|
+
* @typedef {object} JournalEntry
|
|
74
|
+
* @property {string} op `aside` (original to `slot`) | `in` (staged content to dest) | `rmdir` | `mkdir` | `chmod` (restore `mode`)
|
|
75
|
+
* @property {string} path project-relative path the step touched
|
|
76
|
+
* @property {string} [slot] `aside`: backup path under `old/`
|
|
77
|
+
* @property {number} [mode] `rmdir`/`chmod`: the original mode to restore
|
|
78
|
+
*/
|
|
79
|
+
/** Undo a journal in reverse, best effort: a failed step is recorded and the rest still runs. Returns exactly the steps left un-undone.
|
|
80
|
+
*
|
|
81
|
+
* @param {JournalEntry[]} journal steps taken, in order
|
|
82
|
+
* @param {string} proj project directory the steps were applied to
|
|
83
|
+
* @returns {string[]} `"<op> <path>"` for every step left un-undone, in undo order
|
|
84
|
+
*/
|
|
85
|
+
export function undoJournal(journal: JournalEntry[], proj: string): string[];
|
|
86
|
+
/**
|
|
87
|
+
* Press one run's tracing paper down: stage the merged view, then swap it into the project by rename. Applies everything or undoes it: a full undo reports "nothing was applied"; a partial one lists exactly the steps not undone, keeps the commit folder (originals in `old/`), and names its path.
|
|
88
|
+
*
|
|
89
|
+
* @param {object} opts
|
|
90
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
91
|
+
* @param {string} opts.agent agent name whose upper to commit
|
|
92
|
+
* @param {string} [opts.onConflict] `refuse` (default) throws before writing anything; `skip` commits the rest and reports the skipped paths
|
|
93
|
+
* @returns {{committed: string[], skipped: {path: string, reason: string}[]}} committed project-relative paths, plus conflicts left uncommitted under `skip`
|
|
94
|
+
* @when Reach for this to press one run's tracing paper down: it stages the sandbox's changes and swaps them into the project by rename, refusing loudly on mid-run conflicts.
|
|
95
|
+
* @fails Throws (writing nothing) when any changed file was also changed in the project mid-run, unless onConflict is `skip`; throws on unsupported file kinds, on a project that moved mid-commit, and when bubblewrap is older than 0.11.0. A phase-2 failure undoes every journaled step: a full undo reports `nothing was applied`, a partial one throws `SBX_COMMIT_FAILED` listing exactly the steps not undone and keeping the commit folder (originals in `old/`) named in the error. Setuid/setgid bits are stripped from committed modes; mtimes are not preserved. A killed commit leaves a folder that blocks the next diff/commit/setup until removed.
|
|
96
|
+
* @example
|
|
97
|
+
* import { commitOverlay } from "barebox";
|
|
98
|
+
* const { committed, skipped } = commitOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
99
|
+
* console.log(`committed ${committed.length} file(s), skipped ${skipped.length}`);
|
|
100
|
+
*/
|
|
101
|
+
export function commitOverlay({ runtime, agent, onConflict }: {
|
|
102
|
+
runtime: string;
|
|
103
|
+
agent: string;
|
|
104
|
+
onConflict?: string | undefined;
|
|
105
|
+
}): {
|
|
106
|
+
committed: string[];
|
|
107
|
+
skipped: {
|
|
108
|
+
path: string;
|
|
109
|
+
reason: string;
|
|
110
|
+
}[];
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Decide what survives a failed commit: the staging folder is deleted only
|
|
114
|
+
* when the undo left nothing behind (`notUndone` empty — nothing was
|
|
115
|
+
* applied); otherwise it is kept, because `old/` holds the only copy of
|
|
116
|
+
* the originals, and the error names the folder. Returns the error to throw.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} commitDir the run's staging folder in the project
|
|
119
|
+
* @param {unknown} failure whatever the commit threw (phase-1 refusals carry no `notUndone`)
|
|
120
|
+
* @returns {unknown} the failure, to throw
|
|
121
|
+
*/
|
|
122
|
+
export function finishFailedCommit(commitDir: string, failure: unknown): unknown;
|
|
123
|
+
/**
|
|
124
|
+
* Throw one run's tracing paper away: drop every sandboxed change and leave the project exactly as it was. Reports exactly what `diffOverlay` returns, taken before removal, so rollback and diff can never disagree; when the project is gone (or the view cannot be mounted) the report is the raw relative paths found in the upper, with no added/modified/deleted inference. Then the overlay dir is removed completely, so the next run starts fresh. Never touches the project.
|
|
125
|
+
*
|
|
126
|
+
* @param {object} opts
|
|
127
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
128
|
+
* @param {string} opts.agent agent name whose upper to discard
|
|
129
|
+
* @returns {{discarded: OverlayChange[], exact: boolean, why: string|null}} the changes that were dropped; `exact` is false for the raw fallback, and then `why` names the actual reason (diff's error, or `project <proj> is gone`, plus the raw-listing reason when that failed too). `why` is null when exact.
|
|
130
|
+
* @when Reach for this to throw one run's tracing paper away: it drops every sandboxed change and leaves the project exactly as it was.
|
|
131
|
+
* @fails Throws when no overlay exists for the agent or its record is corrupt, or when the overlay folder cannot be removed even after a permission repair (the error names the folder). Listing the changes never throws and never invents entries: an unlistable upper reports `discarded: []` with the reasons in `why`. Removal depends on neither listing nor the first permission pass: a partial failure retries removal once after a fresh permission pass. Dropping the upper never touches the project.
|
|
132
|
+
* @example
|
|
133
|
+
* import { rollbackOverlay } from "barebox";
|
|
134
|
+
* const { discarded } = rollbackOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
135
|
+
* console.log(`discarded ${discarded.length} change(s)`);
|
|
136
|
+
*/
|
|
137
|
+
export function rollbackOverlay({ runtime, agent }: {
|
|
138
|
+
runtime: string;
|
|
139
|
+
agent: string;
|
|
140
|
+
}): {
|
|
141
|
+
discarded: OverlayChange[];
|
|
142
|
+
exact: boolean;
|
|
143
|
+
why: string | null;
|
|
144
|
+
};
|
|
145
|
+
/** Path segments invisible to the layer (Q3): never snapshotted, diffed, committed or deleted. */
|
|
146
|
+
export const DEFAULT_IGNORE: string[];
|
|
147
|
+
/** Refused commit: paths changed in the project mid-run. A conflict refusal is a phase-1 failure: nothing was applied — resolve and retry. */
|
|
148
|
+
export class OverlayConflictError extends Error {
|
|
149
|
+
/** @param {{path: string, status: string, reason: string|null}[]} conflicts conflicting changes */
|
|
150
|
+
constructor(conflicts: {
|
|
151
|
+
path: string;
|
|
152
|
+
status: string;
|
|
153
|
+
reason: string | null;
|
|
154
|
+
}[]);
|
|
155
|
+
code: string;
|
|
156
|
+
/** @type {{path: string, status: string, reason: string|null}[]} */
|
|
157
|
+
conflicts: {
|
|
158
|
+
path: string;
|
|
159
|
+
status: string;
|
|
160
|
+
reason: string | null;
|
|
161
|
+
}[];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* One journal entry (the single shape): a phase-2 step actually taken; undo runs these in reverse.
|
|
165
|
+
*/
|
|
166
|
+
export type JournalEntry = {
|
|
167
|
+
/**
|
|
168
|
+
* `aside` (original to `slot`) | `in` (staged content to dest) | `rmdir` | `mkdir` | `chmod` (restore `mode`)
|
|
169
|
+
*/
|
|
170
|
+
op: string;
|
|
171
|
+
/**
|
|
172
|
+
* project-relative path the step touched
|
|
173
|
+
*/
|
|
174
|
+
path: string;
|
|
175
|
+
/**
|
|
176
|
+
* `aside`: backup path under `old/`
|
|
177
|
+
*/
|
|
178
|
+
slot?: string | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* `rmdir`/`chmod`: the original mode to restore
|
|
181
|
+
*/
|
|
182
|
+
mode?: number | undefined;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* One reviewed file change: what the sandbox did at `path`, and whether the project changed underneath it mid-run.
|
|
186
|
+
*/
|
|
187
|
+
export type OverlayChange = {
|
|
188
|
+
/**
|
|
189
|
+
* project-relative path with `/` separators
|
|
190
|
+
*/
|
|
191
|
+
path: string;
|
|
192
|
+
/**
|
|
193
|
+
* `added` | `modified` | `deleted` (`unknown`: rollback's raw fallback, which infers nothing)
|
|
194
|
+
*/
|
|
195
|
+
status: string;
|
|
196
|
+
/**
|
|
197
|
+
* true when the project changed this path after the run started
|
|
198
|
+
*/
|
|
199
|
+
conflict: boolean;
|
|
200
|
+
/**
|
|
201
|
+
* why `conflict` is set, or null when clean
|
|
202
|
+
*/
|
|
203
|
+
reason: string | null;
|
|
204
|
+
/**
|
|
205
|
+
* merged-view kind: `f` file, `d` dir, `l` symlink (set unless deleted)
|
|
206
|
+
*/
|
|
207
|
+
type?: string | undefined;
|
|
208
|
+
/**
|
|
209
|
+
* merged-view permission bits (set unless deleted)
|
|
210
|
+
*/
|
|
211
|
+
mode?: number | undefined;
|
|
212
|
+
/**
|
|
213
|
+
* sha256 of a merged-view regular file
|
|
214
|
+
*/
|
|
215
|
+
hash?: string | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* symlink target of a merged-view symlink
|
|
218
|
+
*/
|
|
219
|
+
target?: string | undefined;
|
|
220
|
+
};
|