@tangle-network/sandbox 0.60.11 → 0.60.12-develop.20261003133034.b5243b7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -0
- package/dist/agent/index.d.ts +4 -4
- package/dist/agent/index.js +2 -2
- package/dist/agent/preview.d.ts +1 -1
- package/dist/{agent-run-outcome-BLsOZXss.d.ts → agent-run-outcome-CG7bb9lr.d.ts} +1 -1
- package/dist/auth/index.d.ts +2 -2
- package/dist/{browser-interactive-control-controller-C-3MDol8.d.ts → browser-interactive-control-controller-DRCfxOQz.d.ts} +1 -1
- package/dist/{client-g9SpDqN4.d.ts → client-BCX8_Mu-.d.ts} +5 -5
- package/dist/{client-CFB_MZ0q.js → client-Cd8xKjQZ.js} +3 -3
- package/dist/collaboration/index.d.ts +1 -1
- package/dist/core.d.ts +5 -5
- package/dist/core.js +2 -2
- package/dist/{index-FPutSe7b.d.ts → index-BgFIcLar.d.ts} +2 -2
- package/dist/index.d.ts +22 -16
- package/dist/index.js +3 -3
- package/dist/interactive-control.d.ts +1 -1
- package/dist/{interactive-controller-iYddZt8b.d.ts → interactive-controller-ykaThYp6.d.ts} +2 -2
- package/dist/{lines-5Y7R5l_M.d.ts → lines-BbvLx7pi.d.ts} +1 -1
- package/dist/lines.d.ts +1 -1
- package/dist/{preview-PqH4_9hA.d.ts → preview-Ddy1fJyc.d.ts} +3 -3
- package/dist/raw-evidence-archive-FxWK99KW.js +454 -0
- package/dist/runtime.d.ts +6 -6
- package/dist/{sandbox-BDqN9Xbx.d.ts → sandbox-BCygk0lS.d.ts} +185 -46
- package/dist/{sandbox-BtzDSIt0.js → sandbox-D3SjjeDp.js} +16 -0
- package/dist/tangle/index.d.ts +1 -1
- package/dist/tangle/index.js +1 -1
- package/dist/{tangle-h87rED9E.js → tangle-fMCqH1ig.js} +1 -1
- package/dist/{types-B-RSmSBY.d.ts → types-BlA8Cly0.d.ts} +232 -232
- package/package.json +7 -5
- /package/dist/{cli-auth-bundle-Dchr0WkF.d.ts → cli-auth-bundle-BMcIiB-I.d.ts} +0 -0
- /package/dist/{errors-CybOpzWU.d.ts → errors-DS32ViPj.d.ts} +0 -0
- /package/dist/{index-C0SSGYYc.d.ts → index-BXcL8P-U.d.ts} +0 -0
- /package/dist/{index-BCftzX-m.d.ts → index-BoQeoNOD.d.ts} +0 -0
- /package/dist/{local-cli-auth-BgIIaUo2.js → local-cli-auth-BH9A-PW_.js} +0 -0
- /package/dist/{terminal-stream-BUMu8Oae.d.ts → terminal-stream-B6bUr0hj.d.ts} +0 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
//#region src/lib/raw-evidence-archive.ts
|
|
3
|
+
const digest = z.string().regex(/^sha256:[a-f0-9]{64}$/).transform((value) => `sha256:${value.slice(7)}`);
|
|
4
|
+
const count = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);
|
|
5
|
+
const text = z.string().min(1);
|
|
6
|
+
const payload = z.object({
|
|
7
|
+
path: z.string().regex(/^payload-[1-9][0-9]*$/),
|
|
8
|
+
sizeBytes: count,
|
|
9
|
+
sha256: digest
|
|
10
|
+
});
|
|
11
|
+
const records = payload.extend({
|
|
12
|
+
format: z.literal("ndjson"),
|
|
13
|
+
records: count
|
|
14
|
+
});
|
|
15
|
+
const scope = z.enum(["session-home", "workspace-session"]);
|
|
16
|
+
const safePath = text.refine((path) => !path.startsWith("/") && !path.includes("\\") && !path.includes("\0") && path.split("/").every((part) => part && part !== "." && part !== ".."));
|
|
17
|
+
const metadata = z.object({
|
|
18
|
+
sourceId: text.optional(),
|
|
19
|
+
rootScope: scope,
|
|
20
|
+
path: safePath,
|
|
21
|
+
kind: z.enum([
|
|
22
|
+
"file",
|
|
23
|
+
"directory",
|
|
24
|
+
"symlink"
|
|
25
|
+
]),
|
|
26
|
+
mode: count,
|
|
27
|
+
uid: count,
|
|
28
|
+
gid: count,
|
|
29
|
+
mtimeMs: z.number().finite(),
|
|
30
|
+
ctimeMs: z.number().finite(),
|
|
31
|
+
sizeBytes: count
|
|
32
|
+
});
|
|
33
|
+
const manifestSchema = z.object({
|
|
34
|
+
schema: z.literal("tangle.raw-evidence-archive.v1"),
|
|
35
|
+
status: z.enum(["captured", "partial"]),
|
|
36
|
+
sessionId: text,
|
|
37
|
+
backendType: z.string().regex(/^[a-z][a-z0-9_-]{0,127}$/),
|
|
38
|
+
nativeSessionId: text.nullable(),
|
|
39
|
+
proofStatus: z.enum(["verified", "unverified"]),
|
|
40
|
+
containerId: z.string().regex(/^[a-f0-9]{64}$/).optional(),
|
|
41
|
+
sidecarImageDigest: digest,
|
|
42
|
+
sidecarBundleRevision: z.string().regex(/^[a-f0-9]{40}$/),
|
|
43
|
+
sidecarBundleChecksum: digest.optional(),
|
|
44
|
+
nativeRoots: z.array(z.object({
|
|
45
|
+
sourceId: text.optional(),
|
|
46
|
+
rootScope: scope,
|
|
47
|
+
path: text
|
|
48
|
+
})),
|
|
49
|
+
files: z.array(metadata.extend({
|
|
50
|
+
sha256: digest.optional(),
|
|
51
|
+
linkTarget: z.string().optional(),
|
|
52
|
+
content: payload.optional()
|
|
53
|
+
})),
|
|
54
|
+
excluded: z.array(metadata.extend({ reason: z.literal("credential") })),
|
|
55
|
+
skipped: z.array(z.object({
|
|
56
|
+
sourceId: text.optional(),
|
|
57
|
+
rootScope: scope,
|
|
58
|
+
path: safePath,
|
|
59
|
+
reason: text
|
|
60
|
+
})),
|
|
61
|
+
inventory: z.record(z.string(), count),
|
|
62
|
+
processIo: records,
|
|
63
|
+
processTerminals: records,
|
|
64
|
+
processSources: z.array(z.object({
|
|
65
|
+
sourceId: text,
|
|
66
|
+
sizeBytes: count,
|
|
67
|
+
sha256: digest,
|
|
68
|
+
content: payload
|
|
69
|
+
})),
|
|
70
|
+
events: z.array(z.object({
|
|
71
|
+
metadata: z.object({
|
|
72
|
+
executionId: text,
|
|
73
|
+
sessionId: text,
|
|
74
|
+
status: z.enum([
|
|
75
|
+
"active",
|
|
76
|
+
"completed",
|
|
77
|
+
"failed",
|
|
78
|
+
"cancelled"
|
|
79
|
+
]),
|
|
80
|
+
startedAt: z.number().finite(),
|
|
81
|
+
completedAt: z.number().finite().optional(),
|
|
82
|
+
eventCount: count,
|
|
83
|
+
lastEventId: z.string()
|
|
84
|
+
}),
|
|
85
|
+
frames: records
|
|
86
|
+
})),
|
|
87
|
+
attempts: z.array(z.object({
|
|
88
|
+
executionId: text,
|
|
89
|
+
ordinal: count.min(1),
|
|
90
|
+
providerSessionId: text,
|
|
91
|
+
nativeSessionIds: z.array(text),
|
|
92
|
+
processIds: z.array(text),
|
|
93
|
+
outcome: z.enum([
|
|
94
|
+
"succeeded",
|
|
95
|
+
"failed",
|
|
96
|
+
"cancelled",
|
|
97
|
+
"unknown"
|
|
98
|
+
]),
|
|
99
|
+
missingReasons: z.array(text)
|
|
100
|
+
})),
|
|
101
|
+
evidenceSources: z.array(z.object({
|
|
102
|
+
executionId: text,
|
|
103
|
+
sourceId: text,
|
|
104
|
+
backendType: text,
|
|
105
|
+
runtimeHome: text,
|
|
106
|
+
credentialPaths: z.array(z.string())
|
|
107
|
+
})),
|
|
108
|
+
completeness: z.object({
|
|
109
|
+
nativeStore: z.boolean(),
|
|
110
|
+
processIo: z.boolean(),
|
|
111
|
+
events: z.boolean()
|
|
112
|
+
}),
|
|
113
|
+
coverageComplete: z.boolean(),
|
|
114
|
+
missingReasons: z.array(text)
|
|
115
|
+
});
|
|
116
|
+
const MAX_METADATA = 64 * 1024 * 1024;
|
|
117
|
+
const MAX_RECORD = 16 * 1024 * 1024;
|
|
118
|
+
/** Node-only directory export. Flat generated member names cannot traverse or create links. */
|
|
119
|
+
async function exportRawEvidenceDirectory(request, sessionId, targetDirectory, options, expected) {
|
|
120
|
+
for (const [name, value] of Object.entries({
|
|
121
|
+
maxBytes: options.maxBytes,
|
|
122
|
+
maxFiles: options.maxFiles,
|
|
123
|
+
maxArchiveBytes: options.maxArchiveBytes ?? options.maxBytes
|
|
124
|
+
})) if (!Number.isSafeInteger(value) || value <= 0) throw new Error(`${name} must be a positive safe integer`);
|
|
125
|
+
if (options.maxFiles > 1e6) throw new Error("maxFiles exceeds 1000000");
|
|
126
|
+
options.signal?.throwIfAborted();
|
|
127
|
+
const fs = await import("node:fs/promises");
|
|
128
|
+
const { dirname, join, resolve } = await import("node:path");
|
|
129
|
+
const { createReadStream } = await import("node:fs");
|
|
130
|
+
const { createHash } = await import("node:crypto");
|
|
131
|
+
const { pipeline } = await import("node:stream/promises");
|
|
132
|
+
const { Transform, Writable } = await import("node:stream");
|
|
133
|
+
const { createGunzip } = await import("node:zlib");
|
|
134
|
+
const { default: tar } = await import("tar-stream");
|
|
135
|
+
const destination = resolve(targetDirectory);
|
|
136
|
+
try {
|
|
137
|
+
await fs.lstat(destination);
|
|
138
|
+
throw new Error("Evidence destination already exists");
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error.code !== "ENOENT") throw error;
|
|
141
|
+
}
|
|
142
|
+
const response = await request(options.signal);
|
|
143
|
+
if (!response.ok) {
|
|
144
|
+
await response.body?.cancel();
|
|
145
|
+
throw new Error(`Raw evidence archive request failed (HTTP ${response.status}); no legacy fallback`);
|
|
146
|
+
}
|
|
147
|
+
if (response.headers.get("content-type")?.includes("application/json")) {
|
|
148
|
+
const body = await readBoundedResponse(response, 64 * 1024, options.signal);
|
|
149
|
+
return z.object({
|
|
150
|
+
status: z.literal("unavailable"),
|
|
151
|
+
sessionId: z.literal(sessionId),
|
|
152
|
+
backendType: text,
|
|
153
|
+
reason: text
|
|
154
|
+
}).parse(JSON.parse(body));
|
|
155
|
+
}
|
|
156
|
+
const expectedManifest = response.headers.get("x-evidence-manifest-sha256");
|
|
157
|
+
if (!expectedManifest || !/^sha256:[a-f0-9]{64}$/.test(expectedManifest)) {
|
|
158
|
+
await response.body?.cancel();
|
|
159
|
+
throw new Error("Evidence archive has no manifest digest");
|
|
160
|
+
}
|
|
161
|
+
await fs.mkdir(dirname(destination), { recursive: true });
|
|
162
|
+
const staging = await fs.mkdtemp(join(dirname(destination), ".sandbox-evidence-"));
|
|
163
|
+
const directory = join(staging, "record");
|
|
164
|
+
await fs.mkdir(directory, { mode: 448 });
|
|
165
|
+
try {
|
|
166
|
+
const archive = join(staging, "archive.tar.gz");
|
|
167
|
+
const file = await fs.open(archive, "wx", 384);
|
|
168
|
+
const hash = createHash("sha256");
|
|
169
|
+
let sizeBytes = 0;
|
|
170
|
+
const reader = response.body?.getReader();
|
|
171
|
+
const abort = () => {
|
|
172
|
+
reader?.cancel(options.signal?.reason).catch(() => {});
|
|
173
|
+
};
|
|
174
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
175
|
+
const maxArchiveBytes = options.maxArchiveBytes ?? Math.min(Number.MAX_SAFE_INTEGER, options.maxBytes + options.maxFiles * 2048 + 1024 * 1024);
|
|
176
|
+
try {
|
|
177
|
+
for (;;) {
|
|
178
|
+
options.signal?.throwIfAborted();
|
|
179
|
+
const chunk = await reader?.read();
|
|
180
|
+
options.signal?.throwIfAborted();
|
|
181
|
+
if (!chunk || chunk.done) break;
|
|
182
|
+
sizeBytes += chunk.value.byteLength;
|
|
183
|
+
if (sizeBytes > maxArchiveBytes) throw new Error("Evidence archive exceeds compressed byte limit");
|
|
184
|
+
hash.update(chunk.value);
|
|
185
|
+
await file.writeFile(chunk.value);
|
|
186
|
+
}
|
|
187
|
+
} finally {
|
|
188
|
+
options.signal?.removeEventListener("abort", abort);
|
|
189
|
+
await file.close();
|
|
190
|
+
await reader?.cancel().catch(() => {});
|
|
191
|
+
reader?.releaseLock();
|
|
192
|
+
}
|
|
193
|
+
const length = response.headers.get("content-length");
|
|
194
|
+
if (length !== null && (!/^\d+$/.test(length) || Number(length) !== sizeBytes)) throw new Error("Evidence archive transfer was truncated");
|
|
195
|
+
const transfer = {
|
|
196
|
+
sizeBytes,
|
|
197
|
+
sha256: hash.digest("hex")
|
|
198
|
+
};
|
|
199
|
+
const members = /* @__PURE__ */ new Map();
|
|
200
|
+
const extract = tar.extract();
|
|
201
|
+
let unpacked = 0;
|
|
202
|
+
let tarBytes = 0;
|
|
203
|
+
const inspectHeader = inspectTarHeaders(options.maxBytes, options.maxFiles);
|
|
204
|
+
const bound = new Transform({
|
|
205
|
+
transform(chunk, _encoding, done) {
|
|
206
|
+
try {
|
|
207
|
+
inspectHeader.write(chunk);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
done(error);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
tarBytes += chunk.length;
|
|
213
|
+
done(tarBytes > options.maxBytes + options.maxFiles * 2048 + 1024 * 1024 ? /* @__PURE__ */ new Error("Evidence tar exceeds byte limit") : null, chunk);
|
|
214
|
+
},
|
|
215
|
+
flush(done) {
|
|
216
|
+
try {
|
|
217
|
+
inspectHeader.end();
|
|
218
|
+
done();
|
|
219
|
+
} catch (error) {
|
|
220
|
+
done(error);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
const consume = async () => {
|
|
225
|
+
try {
|
|
226
|
+
for await (const entry of extract) {
|
|
227
|
+
options.signal?.throwIfAborted();
|
|
228
|
+
const { name, size, type } = entry.header;
|
|
229
|
+
if (type !== "file" || !/^(manifest\.json|payload-[1-9][0-9]*)$/.test(name) || members.has(name)) throw new Error("Unsafe or duplicate evidence archive member");
|
|
230
|
+
if (typeof size !== "number" || !Number.isSafeInteger(size) || size < 0 || name === "manifest.json" && size > MAX_METADATA) throw new Error("Evidence archive member exceeds metadata limit");
|
|
231
|
+
unpacked += size;
|
|
232
|
+
if (unpacked > options.maxBytes || members.size >= options.maxFiles) throw new Error("Evidence archive exceeds expanded limits");
|
|
233
|
+
const handle = await fs.open(join(directory, name), "wx", 384);
|
|
234
|
+
const memberHash = createHash("sha256");
|
|
235
|
+
let bytes = 0;
|
|
236
|
+
try {
|
|
237
|
+
for await (const chunk of entry) {
|
|
238
|
+
options.signal?.throwIfAborted();
|
|
239
|
+
if (!Buffer.isBuffer(chunk)) throw new Error("Evidence member did not provide bytes");
|
|
240
|
+
bytes += chunk.length;
|
|
241
|
+
if (bytes > size) throw new Error("Evidence member exceeds declared size");
|
|
242
|
+
memberHash.update(chunk);
|
|
243
|
+
await handle.writeFile(chunk);
|
|
244
|
+
}
|
|
245
|
+
} finally {
|
|
246
|
+
await handle.close();
|
|
247
|
+
}
|
|
248
|
+
if (bytes !== size) throw new Error("Truncated evidence archive member");
|
|
249
|
+
members.set(name, {
|
|
250
|
+
path: name,
|
|
251
|
+
sizeBytes: bytes,
|
|
252
|
+
sha256: `sha256:${memberHash.digest("hex")}`
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
} catch (error) {
|
|
256
|
+
extract.destroy(error);
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const sink = new Writable({
|
|
261
|
+
write(chunk, _encoding, done) {
|
|
262
|
+
if (extract.write(chunk)) done();
|
|
263
|
+
else extract.once("drain", done);
|
|
264
|
+
},
|
|
265
|
+
final(done) {
|
|
266
|
+
extract.once("finish", done);
|
|
267
|
+
extract.end(null);
|
|
268
|
+
},
|
|
269
|
+
destroy(error, done) {
|
|
270
|
+
extract.destroy(error ?? void 0);
|
|
271
|
+
done(error);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
extract.on("error", (error) => {
|
|
275
|
+
sink.destroy(error);
|
|
276
|
+
});
|
|
277
|
+
extract.on("close", () => {
|
|
278
|
+
if (!sink.writableFinished) sink.destroy(/* @__PURE__ */ new Error("Evidence tar parser closed before completion"));
|
|
279
|
+
});
|
|
280
|
+
const settled = await Promise.allSettled([pipeline(createReadStream(archive), createGunzip(), bound, sink, { signal: options.signal }), consume()]);
|
|
281
|
+
for (const outcome of settled) if (outcome.status === "rejected") throw outcome.reason;
|
|
282
|
+
const manifestRef = members.get("manifest.json");
|
|
283
|
+
if (!manifestRef || manifestRef.sha256 !== expectedManifest) throw new Error("Evidence manifest digest mismatch");
|
|
284
|
+
const manifest = manifestSchema.parse(JSON.parse(await fs.readFile(join(directory, "manifest.json"), "utf8")));
|
|
285
|
+
if (manifest.sessionId !== sessionId || manifest.backendType === "unknown") throw new Error("Evidence identity mismatch");
|
|
286
|
+
if (manifest.proofStatus === "verified" && (!manifest.containerId || !manifest.sidecarBundleChecksum)) throw new Error("Evidence capture proof is incomplete");
|
|
287
|
+
if (expected && (manifest.proofStatus !== "verified" || manifest.containerId !== expected.containerId || manifest.sidecarImageDigest !== expected.imageId || manifest.sidecarBundleRevision !== expected.bundleRevision || manifest.sidecarBundleChecksum !== expected.bundleChecksum)) throw new Error("Evidence does not match current container capture proof");
|
|
288
|
+
const captured = manifest.status === "captured";
|
|
289
|
+
if (captured ? !manifest.coverageComplete || manifest.missingReasons.length || !Object.values(manifest.completeness).every(Boolean) || !manifest.attempts.length : manifest.coverageComplete || !manifest.missingReasons.length) throw new Error("Evidence coverage status is inconsistent");
|
|
290
|
+
if (manifest.inventory.skippedEntries !== manifest.skipped.length) throw new Error("Evidence inventory mismatch");
|
|
291
|
+
const ids = new Set(manifest.attempts.flatMap((attempt) => attempt.nativeSessionIds));
|
|
292
|
+
const aggregateId = ids.size === 1 && manifest.attempts.every((attempt) => attempt.nativeSessionIds.length === 1) ? [...ids][0] : null;
|
|
293
|
+
if (manifest.nativeSessionId !== aggregateId) throw new Error("Evidence native session ID mismatch");
|
|
294
|
+
const verify = (ref) => {
|
|
295
|
+
const member = members.get(ref.path);
|
|
296
|
+
if (!member || member.sha256 !== ref.sha256 || member.sizeBytes !== ref.sizeBytes) throw new Error("Evidence payload hash or size mismatch");
|
|
297
|
+
};
|
|
298
|
+
for (const item of manifest.files) if (item.kind === "file") {
|
|
299
|
+
if (!item.content || item.sha256 !== item.content.sha256 || item.sizeBytes !== item.content.sizeBytes) throw new Error("Evidence native payload mismatch");
|
|
300
|
+
verify(item.content);
|
|
301
|
+
} else if (item.content || item.sha256 || item.kind === "symlink" && item.linkTarget === void 0) throw new Error("Evidence native metadata mismatch");
|
|
302
|
+
for (const item of manifest.processSources) {
|
|
303
|
+
if (item.sha256 !== item.content.sha256 || item.sizeBytes !== item.content.sizeBytes) throw new Error("Evidence process source mismatch");
|
|
304
|
+
verify(item.content);
|
|
305
|
+
}
|
|
306
|
+
for (const ref of [manifest.processIo, manifest.processTerminals]) {
|
|
307
|
+
verify(ref);
|
|
308
|
+
let count = 0;
|
|
309
|
+
for await (const row of readRecords(join(directory, ref.path), options.signal)) {
|
|
310
|
+
count++;
|
|
311
|
+
if (ref === manifest.processIo) {
|
|
312
|
+
const content = payload.parse(row.content);
|
|
313
|
+
verify(content);
|
|
314
|
+
if (content.sizeBytes !== row.sizeBytes || content.sha256 !== row.sha256 || row.contentBase64 !== void 0) throw new Error("Evidence process frame mismatch");
|
|
315
|
+
}
|
|
316
|
+
if (captured && !manifest.attempts.some((attempt) => attempt.executionId === row.executionId && attempt.ordinal === row.ordinal && attempt.providerSessionId === row.providerSessionId && attempt.processIds.includes(String(row.processId)))) throw new Error("Evidence process attribution mismatch");
|
|
317
|
+
}
|
|
318
|
+
if (count !== ref.records) throw new Error("Evidence record count mismatch");
|
|
319
|
+
}
|
|
320
|
+
for (const event of manifest.events) {
|
|
321
|
+
verify(event.frames);
|
|
322
|
+
let count = 0;
|
|
323
|
+
let last = "";
|
|
324
|
+
for await (const row of readRecords(join(directory, event.frames.path), options.signal)) {
|
|
325
|
+
count++;
|
|
326
|
+
if (typeof row.eventId !== "string") throw new Error("Evidence event identity missing");
|
|
327
|
+
last = row.eventId;
|
|
328
|
+
}
|
|
329
|
+
if (count !== event.frames.records || event.metadata.sessionId !== sessionId || captured && (count !== event.metadata.eventCount || last !== event.metadata.lastEventId)) throw new Error("Evidence event sequence mismatch");
|
|
330
|
+
}
|
|
331
|
+
options.signal?.throwIfAborted();
|
|
332
|
+
try {
|
|
333
|
+
await fs.lstat(destination);
|
|
334
|
+
throw new Error("Evidence destination already exists");
|
|
335
|
+
} catch (error) {
|
|
336
|
+
if (error.code !== "ENOENT") throw error;
|
|
337
|
+
}
|
|
338
|
+
await fs.rename(directory, destination);
|
|
339
|
+
await fs.rm(staging, {
|
|
340
|
+
recursive: true,
|
|
341
|
+
force: true
|
|
342
|
+
});
|
|
343
|
+
return {
|
|
344
|
+
status: manifest.status,
|
|
345
|
+
directory: destination,
|
|
346
|
+
manifestPath: join(destination, "manifest.json"),
|
|
347
|
+
manifestSha256: manifestRef.sha256,
|
|
348
|
+
transfer,
|
|
349
|
+
manifest,
|
|
350
|
+
coverageComplete: manifest.coverageComplete,
|
|
351
|
+
missingReasons: manifest.missingReasons
|
|
352
|
+
};
|
|
353
|
+
} catch (error) {
|
|
354
|
+
await fs.writeFile(join(staging, "failure.json"), JSON.stringify({
|
|
355
|
+
status: "incomplete",
|
|
356
|
+
sessionId,
|
|
357
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
358
|
+
}), { mode: 384 });
|
|
359
|
+
throw new Error(`Evidence export failed; partial bytes retained at ${staging}`, { cause: error });
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
async function readBoundedResponse(response, max, signal) {
|
|
363
|
+
const reader = response.body?.getReader();
|
|
364
|
+
let size = 0;
|
|
365
|
+
const chunks = [];
|
|
366
|
+
try {
|
|
367
|
+
for (;;) {
|
|
368
|
+
signal?.throwIfAborted();
|
|
369
|
+
const part = await reader?.read();
|
|
370
|
+
if (!part || part.done) break;
|
|
371
|
+
size += part.value.byteLength;
|
|
372
|
+
if (size > max) throw new Error("Evidence response exceeds metadata limit");
|
|
373
|
+
chunks.push(part.value);
|
|
374
|
+
}
|
|
375
|
+
} finally {
|
|
376
|
+
await reader?.cancel().catch(() => {});
|
|
377
|
+
reader?.releaseLock();
|
|
378
|
+
}
|
|
379
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
380
|
+
}
|
|
381
|
+
async function* readRecords(path, signal) {
|
|
382
|
+
const { createReadStream } = await import("node:fs");
|
|
383
|
+
let pending = Buffer.alloc(0);
|
|
384
|
+
for await (const value of createReadStream(path, {
|
|
385
|
+
highWaterMark: 64 * 1024,
|
|
386
|
+
signal
|
|
387
|
+
})) {
|
|
388
|
+
const chunk = value;
|
|
389
|
+
let start = 0;
|
|
390
|
+
for (let end = chunk.indexOf(10, start); end !== -1; end = chunk.indexOf(10, start)) {
|
|
391
|
+
if (pending.length + end - start > MAX_RECORD) throw new Error("Evidence record exceeds byte limit");
|
|
392
|
+
const line = Buffer.concat([pending, chunk.subarray(start, end)]);
|
|
393
|
+
pending = Buffer.alloc(0);
|
|
394
|
+
start = end + 1;
|
|
395
|
+
yield z.record(z.string(), z.unknown()).parse(JSON.parse(line.toString("utf8")));
|
|
396
|
+
}
|
|
397
|
+
if (pending.length + chunk.length - start > MAX_RECORD) throw new Error("Evidence record exceeds byte limit");
|
|
398
|
+
pending = Buffer.concat([pending, chunk.subarray(start)]);
|
|
399
|
+
}
|
|
400
|
+
if (pending.length) throw new Error("Evidence record is truncated");
|
|
401
|
+
}
|
|
402
|
+
/** Reject extended headers before the tar library can buffer their declared body. */
|
|
403
|
+
function inspectTarHeaders(maxBytes, maxFiles) {
|
|
404
|
+
const header = Buffer.alloc(512);
|
|
405
|
+
let filled = 0;
|
|
406
|
+
let remaining = 0;
|
|
407
|
+
let files = 0;
|
|
408
|
+
let bytes = 0;
|
|
409
|
+
let zeros = 0;
|
|
410
|
+
return {
|
|
411
|
+
write(chunk) {
|
|
412
|
+
let offset = 0;
|
|
413
|
+
while (offset < chunk.length) {
|
|
414
|
+
if (remaining) {
|
|
415
|
+
const length = Math.min(remaining, chunk.length - offset);
|
|
416
|
+
remaining -= length;
|
|
417
|
+
offset += length;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
const length = Math.min(512 - filled, chunk.length - offset);
|
|
421
|
+
chunk.copy(header, filled, offset, offset + length);
|
|
422
|
+
filled += length;
|
|
423
|
+
offset += length;
|
|
424
|
+
if (filled !== 512) continue;
|
|
425
|
+
filled = 0;
|
|
426
|
+
if (header.every((value) => value === 0)) {
|
|
427
|
+
zeros++;
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (zeros || header[156] !== 0 && header[156] !== 48) throw new Error("Evidence archive requires only regular file headers");
|
|
431
|
+
const field = header.subarray(124, 136);
|
|
432
|
+
let size;
|
|
433
|
+
if (field[0] === 128) {
|
|
434
|
+
let large = 0n;
|
|
435
|
+
for (const byte of field.subarray(1)) large = large * 256n + BigInt(byte);
|
|
436
|
+
size = Number(large);
|
|
437
|
+
} else {
|
|
438
|
+
const octal = field.toString("ascii").replace(/\0.*$/s, "").trim();
|
|
439
|
+
if (!/^[0-7]+$/.test(octal)) throw new Error("Invalid evidence tar member size");
|
|
440
|
+
size = Number.parseInt(octal, 8);
|
|
441
|
+
}
|
|
442
|
+
if (!Number.isSafeInteger(size) || size < 0) throw new Error("Invalid evidence tar member size");
|
|
443
|
+
bytes += size;
|
|
444
|
+
if (++files > maxFiles || bytes > maxBytes) throw new Error("Evidence tar exceeds expanded limits");
|
|
445
|
+
remaining = Math.ceil(size / 512) * 512;
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
end() {
|
|
449
|
+
if (filled || remaining || zeros < 2) throw new Error("Evidence tar is truncated");
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
//#endregion
|
|
454
|
+
export { exportRawEvidenceDirectory };
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { _ as TimeoutError, a as FileWriteConflictError, c as NotFoundError, f as SandboxError, g as StateError, h as ServerError, l as PartialFailureError, m as SandboxFailureDetail, n as CapabilityError, p as SandboxErrorJson, s as NetworkError, t as AuthError, u as QuotaError, v as ValidationError } from "./errors-
|
|
6
|
-
import { a as deriveAgentRunOutcome, i as deriveAgentResultOutcome, n as AgentRunOutcomeTracker, r as createAgentRunOutcomeTracker, t as AgentRunOutcome } from "./agent-run-outcome-
|
|
1
|
+
import { A as CommitTaskSessionOptions, Aa as WriteFileOptions, At as FileUsageOptions, Bi as SessionEventStreamOptions, Ci as SandboxTerminalInfo, Cn as ListMessagesOptions, Dr as SandboxEvent, Gi as SessionInfo, H as CreateTaskSessionOptions, Hi as SessionExecutionStatus, Ii as SendSessionMessageOptions, Ji as SessionMessage, Li as SendSessionMessageRequest, Mt as FileWriteResult, Ot as FileTreeOptions, Ri as SentSessionMessage, Si as SandboxTerminalCreateOptions, St as FileReadBatchResult, T as ChunkedUploadResult, Ta as UploadProgress, Ti as SandboxTerminalRequestOptions, Tt as FileRenameResult, V as CreateSessionOptions, Vi as SessionExecutionInfo, Yi as SessionMessageAdmission, da as TaskSessionCommitResult, gi as SandboxRuntimeHealth, jt as FileUsageResult, kt as FileTreeResult, pa as TaskSessionInfo, sr as RenameOptions, ua as TaskSessionChanges, ui as SandboxPortBinding, w as ChunkedUploadOptions, wi as SandboxTerminalManager, xt as FileReadBatchOptions } from "./types-BlA8Cly0.js";
|
|
2
|
+
import { l as ScopedToken, t as TerminalStream } from "./terminal-stream-B6bUr0hj.js";
|
|
3
|
+
import { c as InteractiveSessionIdentity, l as InteractiveSessionNotRunningError, r as browserInteractiveControlHolderId } from "./browser-interactive-control-controller-DRCfxOQz.js";
|
|
4
|
+
import { t as BrowserInteractiveSessionController } from "./interactive-controller-ykaThYp6.js";
|
|
5
|
+
import { _ as TimeoutError, a as FileWriteConflictError, c as NotFoundError, f as SandboxError, g as StateError, h as ServerError, l as PartialFailureError, m as SandboxFailureDetail, n as CapabilityError, p as SandboxErrorJson, s as NetworkError, t as AuthError, u as QuotaError, v as ValidationError } from "./errors-DS32ViPj.js";
|
|
6
|
+
import { a as deriveAgentRunOutcome, i as deriveAgentResultOutcome, n as AgentRunOutcomeTracker, r as createAgentRunOutcomeTracker, t as AgentRunOutcome } from "./agent-run-outcome-CG7bb9lr.js";
|
|
7
7
|
|
|
8
8
|
//#region src/lib/chunked-upload.d.ts
|
|
9
9
|
/** The transport a caller wires this module to — {@link SandboxInstance.runtimeFetch}
|