@clearideas/agent-runtime-sandbox 0.9.0 → 0.10.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/NOTICE +2 -2
- package/dist/policy.d.ts +16 -1
- package/dist/policy.js +37 -6
- package/dist/run-sandbox.d.ts +11 -1
- package/dist/run-sandbox.js +213 -38
- package/package.json +3 -3
package/NOTICE
CHANGED
|
@@ -3,6 +3,6 @@ Copyright 2026 Clear Ideas Incorporated
|
|
|
3
3
|
|
|
4
4
|
Clear Ideas is a trademark of Clear Ideas Incorporated.
|
|
5
5
|
|
|
6
|
-
The documentation site includes @clearideas/analytics 1.
|
|
6
|
+
The documentation site includes @clearideas/consent-analytics 0.1.0-rc.1, distributed
|
|
7
7
|
under the MIT License. Its license text is available at
|
|
8
|
-
docs/public/assets/analytics/LICENSE.clearideas-analytics.txt.
|
|
8
|
+
docs/public/assets/analytics/LICENSE.clearideas-consent-analytics.txt.
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WorkspaceMode } from "@clearideas/agent-runtime-contracts";
|
|
1
|
+
import type { RunSandboxUsageLimits, WorkspaceMode } from "@clearideas/agent-runtime-contracts";
|
|
2
2
|
import type { AgentTool, ResolvedWorkspace, RunSandboxCommandRequest } from "@clearideas/agent-runtime-core";
|
|
3
3
|
import { type SandboxInputFile } from "./contracts.js";
|
|
4
4
|
/** Built-in operations served by the runtime-shipped helper. */
|
|
@@ -61,6 +61,21 @@ export interface RunSandboxLimits {
|
|
|
61
61
|
maxOutputBytes: number;
|
|
62
62
|
}
|
|
63
63
|
export declare const DEFAULT_RUN_SANDBOX_LIMITS: RunSandboxLimits;
|
|
64
|
+
/**
|
|
65
|
+
* Narrow host limits to the usage limits a run requested. The host's values
|
|
66
|
+
* are ceilings: a run can lower a limit, never raise it. The command and
|
|
67
|
+
* sandbox-second budgets are resolved by `runSandboxBudget`.
|
|
68
|
+
*/
|
|
69
|
+
export declare const narrowRunSandboxLimits: (host: RunSandboxLimits, requested: RunSandboxUsageLimits | undefined) => RunSandboxLimits;
|
|
70
|
+
/**
|
|
71
|
+
* A run's command and sandbox-second budget: the run's own limits, capped at
|
|
72
|
+
* the host's, or the host's when the run sets none. Reaching it blocks the
|
|
73
|
+
* run for its host, which may grant more up to its own values.
|
|
74
|
+
*/
|
|
75
|
+
export declare const runSandboxBudget: (host: RunSandboxLimits, requested: RunSandboxUsageLimits | undefined) => {
|
|
76
|
+
maxCommands: number;
|
|
77
|
+
maxSeconds: number;
|
|
78
|
+
};
|
|
64
79
|
export interface RunSandboxPolicy {
|
|
65
80
|
commands: Record<string, RunSandboxCommandEntry>;
|
|
66
81
|
shell: RunSandboxShellPolicy;
|
package/dist/policy.js
CHANGED
|
@@ -37,18 +37,49 @@ export const DEFAULT_RUN_SANDBOX_LIMITS = {
|
|
|
37
37
|
cpuCount: 1,
|
|
38
38
|
processLimit: 128,
|
|
39
39
|
workspaceTmpfsMb: 1_024,
|
|
40
|
-
commandTimeoutMs:
|
|
41
|
-
maxCommandTimeoutMs:
|
|
42
|
-
maxCommandsPerRun:
|
|
43
|
-
maxTotalSandboxSeconds:
|
|
40
|
+
commandTimeoutMs: 300_000,
|
|
41
|
+
maxCommandTimeoutMs: 3_600_000,
|
|
42
|
+
maxCommandsPerRun: 2_000,
|
|
43
|
+
maxTotalSandboxSeconds: 14_400,
|
|
44
44
|
maxStdoutBytes: 1_048_576,
|
|
45
45
|
maxStderrBytes: 262_144,
|
|
46
46
|
maxArgs: 64,
|
|
47
47
|
maxArgBytes: 4_096,
|
|
48
48
|
maxTotalStagedBytes: 256 * 1_048_576,
|
|
49
|
-
maxOutputFiles:
|
|
50
|
-
maxOutputBytes:
|
|
49
|
+
maxOutputFiles: 500,
|
|
50
|
+
maxOutputBytes: 500 * 1_048_576,
|
|
51
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* Narrow host limits to the usage limits a run requested. The host's values
|
|
54
|
+
* are ceilings: a run can lower a limit, never raise it. The command and
|
|
55
|
+
* sandbox-second budgets are resolved by `runSandboxBudget`.
|
|
56
|
+
*/
|
|
57
|
+
export const narrowRunSandboxLimits = (host, requested) => {
|
|
58
|
+
if (!requested)
|
|
59
|
+
return host;
|
|
60
|
+
const narrowed = { ...host };
|
|
61
|
+
const apply = (key, value) => {
|
|
62
|
+
if (value !== undefined && value > 0)
|
|
63
|
+
narrowed[key] = Math.min(host[key], value);
|
|
64
|
+
};
|
|
65
|
+
apply("maxCommandTimeoutMs", requested.maxCommandTimeoutMs);
|
|
66
|
+
apply("commandTimeoutMs", requested.commandTimeoutMs);
|
|
67
|
+
apply("maxStdoutBytes", requested.maxStdoutBytes);
|
|
68
|
+
apply("maxStderrBytes", requested.maxStderrBytes);
|
|
69
|
+
apply("maxOutputFiles", requested.maxOutputFiles);
|
|
70
|
+
apply("maxOutputBytes", requested.maxOutputBytes);
|
|
71
|
+
narrowed.commandTimeoutMs = Math.min(narrowed.commandTimeoutMs, narrowed.maxCommandTimeoutMs);
|
|
72
|
+
return narrowed;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* A run's command and sandbox-second budget: the run's own limits, capped at
|
|
76
|
+
* the host's, or the host's when the run sets none. Reaching it blocks the
|
|
77
|
+
* run for its host, which may grant more up to its own values.
|
|
78
|
+
*/
|
|
79
|
+
export const runSandboxBudget = (host, requested) => ({
|
|
80
|
+
maxCommands: Math.min(host.maxCommandsPerRun, requested?.maxCommands ?? host.maxCommandsPerRun),
|
|
81
|
+
maxSeconds: Math.min(host.maxTotalSandboxSeconds, requested?.maxSeconds ?? host.maxTotalSandboxSeconds),
|
|
82
|
+
});
|
|
52
83
|
export const resolveRunSandboxLimits = (partial = {}) => {
|
|
53
84
|
const limits = { ...DEFAULT_RUN_SANDBOX_LIMITS };
|
|
54
85
|
for (const [key, value] of Object.entries(partial)) {
|
package/dist/run-sandbox.d.ts
CHANGED
|
@@ -50,7 +50,17 @@ export interface ProviderRunSandboxOptions extends RunSandboxHostHooks {
|
|
|
50
50
|
export declare const bundledHelperPath: () => string;
|
|
51
51
|
declare class RunSandboxExecutionError extends Error {
|
|
52
52
|
readonly code: string;
|
|
53
|
-
|
|
53
|
+
/** For sandbox_budget_exhausted: the run limit that was reached. */
|
|
54
|
+
readonly limit: {
|
|
55
|
+
name: string;
|
|
56
|
+
used: number;
|
|
57
|
+
value: number;
|
|
58
|
+
} | undefined;
|
|
59
|
+
constructor(code: string, message: string, limit?: {
|
|
60
|
+
name: string;
|
|
61
|
+
used: number;
|
|
62
|
+
value: number;
|
|
63
|
+
});
|
|
54
64
|
}
|
|
55
65
|
/**
|
|
56
66
|
* Opens one provider-backed sandbox per run attempt, stages bound
|
package/dist/run-sandbox.js
CHANGED
|
@@ -4,7 +4,7 @@ import path from "node:path";
|
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import { isSafeWorkspacePath } from "@clearideas/agent-runtime-core";
|
|
6
6
|
import { assertSandboxPath, assertSandboxResourceLimits, } from "./contracts.js";
|
|
7
|
-
import { DEFAULT_HELPER_COMMAND, SANDBOX_HELPER_PATH, SANDBOX_MOUNT_DIRECTORY, SANDBOX_OUTPUT_DIRECTORY, SANDBOX_WORKSPACES_FILE, SandboxPolicyError, admitCommand, describeWorkspaceTools, resolveRunSandboxPolicy, } from "./policy.js";
|
|
7
|
+
import { DEFAULT_HELPER_COMMAND, SANDBOX_HELPER_PATH, SANDBOX_MOUNT_DIRECTORY, SANDBOX_OUTPUT_DIRECTORY, SANDBOX_WORKSPACES_FILE, SandboxPolicyError, admitCommand, describeWorkspaceTools, narrowRunSandboxLimits, resolveRunSandboxPolicy, runSandboxBudget, } from "./policy.js";
|
|
8
8
|
const HELPER_PROTOCOL_VERSION = 1;
|
|
9
9
|
const STAGING_BATCH_BYTES = 4 * 1024 * 1024;
|
|
10
10
|
const MAXIMUM_HELPER_OUTPUT_BYTES = 1_048_576;
|
|
@@ -38,10 +38,13 @@ const appendBounded = (chunks, state, chunk, maximum) => {
|
|
|
38
38
|
const isJsonObject = (value) => value != null && typeof value === "object" && !Array.isArray(value);
|
|
39
39
|
class RunSandboxExecutionError extends Error {
|
|
40
40
|
code;
|
|
41
|
-
|
|
41
|
+
/** For sandbox_budget_exhausted: the run limit that was reached. */
|
|
42
|
+
limit;
|
|
43
|
+
constructor(code, message, limit) {
|
|
42
44
|
super(message);
|
|
43
45
|
this.name = "RunSandboxExecutionError";
|
|
44
46
|
this.code = code;
|
|
47
|
+
this.limit = limit;
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
/**
|
|
@@ -125,14 +128,21 @@ export class ProviderRunSandbox {
|
|
|
125
128
|
fileCount: 0,
|
|
126
129
|
bytes: 0,
|
|
127
130
|
baseline: new Map(),
|
|
131
|
+
reported: new Map(),
|
|
128
132
|
});
|
|
129
133
|
}
|
|
130
|
-
|
|
134
|
+
// Each run's usage limits narrow the host's; they can never raise them.
|
|
135
|
+
const policy = {
|
|
136
|
+
...this.#policy,
|
|
137
|
+
limits: narrowRunSandboxLimits(this.#policy.limits, request.limits),
|
|
138
|
+
};
|
|
139
|
+
const spec = this.#spec(request.runId, attachments, policy.limits);
|
|
131
140
|
const handle = await this.#options.provider.create(spec, request.signal ? { signal: request.signal } : undefined);
|
|
132
141
|
const session = new ProviderRunSandboxSession({
|
|
133
142
|
handle,
|
|
134
143
|
provider: this.#options.provider,
|
|
135
|
-
policy
|
|
144
|
+
policy,
|
|
145
|
+
runBudget: runSandboxBudget(this.#policy.limits, request.limits),
|
|
136
146
|
helperCommand: this.#helperCommand,
|
|
137
147
|
profiles: this.#profiles,
|
|
138
148
|
allowedEnvironment: new Set(this.#options.allowedEnvironment ?? []),
|
|
@@ -191,14 +201,14 @@ export class ProviderRunSandbox {
|
|
|
191
201
|
throw new Error(`Workspace ${workspace.alias} has ${bytes} bytes, exceeding its ${limits.maxTotalBytes}-byte limit.`);
|
|
192
202
|
}
|
|
193
203
|
}
|
|
194
|
-
#spec(runId, attachments) {
|
|
195
|
-
const limits = this.#policy.limits;
|
|
204
|
+
#spec(runId, attachments, limits = this.#policy.limits) {
|
|
196
205
|
return {
|
|
197
206
|
image: this.#options.image,
|
|
198
207
|
workingDirectory: "/workspace",
|
|
199
208
|
network: "none",
|
|
200
209
|
limits: {
|
|
201
|
-
|
|
210
|
+
// A lifetime hint for providers, within the 24-hour resource ceiling.
|
|
211
|
+
timeoutMs: Math.min(limits.maxTotalSandboxSeconds * 1_000 + 60_000, 86_400_000),
|
|
202
212
|
memoryMb: limits.memoryMb,
|
|
203
213
|
cpuCount: limits.cpuCount,
|
|
204
214
|
processLimit: limits.processLimit,
|
|
@@ -223,6 +233,8 @@ class ProviderRunSandboxSession {
|
|
|
223
233
|
#sequence = 0;
|
|
224
234
|
#queue = Promise.resolve();
|
|
225
235
|
#closed = false;
|
|
236
|
+
/** Changes copied back since the last takeChanges(), per alias and path. */
|
|
237
|
+
#pending = new Map();
|
|
226
238
|
constructor(input) {
|
|
227
239
|
this.id = input.handle.id;
|
|
228
240
|
this.#input = input;
|
|
@@ -236,6 +248,53 @@ class ProviderRunSandboxSession {
|
|
|
236
248
|
sandboxSecondsUsed: this.#sandboxSecondsUsed,
|
|
237
249
|
};
|
|
238
250
|
}
|
|
251
|
+
takeChanges() {
|
|
252
|
+
const taken = Object.fromEntries([...this.#pending].map(([alias, pending]) => [
|
|
253
|
+
alias,
|
|
254
|
+
{
|
|
255
|
+
changes: [...pending.changes.values()],
|
|
256
|
+
notPersisted: [...pending.notPersisted.values()],
|
|
257
|
+
},
|
|
258
|
+
]));
|
|
259
|
+
this.#pending.clear();
|
|
260
|
+
return taken;
|
|
261
|
+
}
|
|
262
|
+
/** Fold copied-back changes into the pending set, one entry per path. */
|
|
263
|
+
#remember(filesChanged, notPersisted) {
|
|
264
|
+
const entry = (alias) => {
|
|
265
|
+
let pending = this.#pending.get(alias);
|
|
266
|
+
if (!pending) {
|
|
267
|
+
pending = { changes: new Map(), notPersisted: new Map() };
|
|
268
|
+
this.#pending.set(alias, pending);
|
|
269
|
+
}
|
|
270
|
+
return pending;
|
|
271
|
+
};
|
|
272
|
+
for (const change of filesChanged) {
|
|
273
|
+
const pending = entry(change.alias).changes;
|
|
274
|
+
const earlier = pending.get(change.path);
|
|
275
|
+
const next = {
|
|
276
|
+
path: change.path,
|
|
277
|
+
action: change.action,
|
|
278
|
+
...(change.size !== undefined ? { size: change.size } : {}),
|
|
279
|
+
...(change.sha256 !== undefined ? { sha256: change.sha256 } : {}),
|
|
280
|
+
};
|
|
281
|
+
if (earlier?.action === "created" && change.action === "deleted") {
|
|
282
|
+
pending.delete(change.path); // never existed at the last commit point
|
|
283
|
+
}
|
|
284
|
+
else if (earlier?.action === "created") {
|
|
285
|
+
pending.set(change.path, { ...next, action: "created" });
|
|
286
|
+
}
|
|
287
|
+
else if (earlier?.action === "deleted" && change.action === "created") {
|
|
288
|
+
pending.set(change.path, { ...next, action: "modified" });
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
pending.set(change.path, next);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (const item of notPersisted) {
|
|
295
|
+
entry(item.alias).notPersisted.set(`${item.path}\u0000${item.reason}`, item);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
239
298
|
/** Stage the helper, every non-attached mount, and the mount index. */
|
|
240
299
|
async stage(helperSource) {
|
|
241
300
|
const { provider, handle, request } = this.#input;
|
|
@@ -348,6 +407,7 @@ class ProviderRunSandboxSession {
|
|
|
348
407
|
const changes = await this.#syncBack(mount, controller.signal);
|
|
349
408
|
filesChanged += changes.filesChanged.length;
|
|
350
409
|
ignored += changes.ignoredPaths.length;
|
|
410
|
+
this.#remember(changes.filesChanged, changes.notPersisted);
|
|
351
411
|
}
|
|
352
412
|
}
|
|
353
413
|
finally {
|
|
@@ -405,11 +465,21 @@ class ProviderRunSandboxSession {
|
|
|
405
465
|
: undefined;
|
|
406
466
|
},
|
|
407
467
|
});
|
|
408
|
-
|
|
409
|
-
|
|
468
|
+
// The budget is a run limit: reaching it blocks the run for its host.
|
|
469
|
+
const { runBudget } = this.#input;
|
|
470
|
+
if (this.#commandsUsed >= runBudget.maxCommands) {
|
|
471
|
+
throw new RunSandboxExecutionError("sandbox_budget_exhausted", `The run has used its ${runBudget.maxCommands}-command sandbox limit.`, {
|
|
472
|
+
name: "sandbox.maxCommands",
|
|
473
|
+
used: this.#commandsUsed,
|
|
474
|
+
value: runBudget.maxCommands,
|
|
475
|
+
});
|
|
410
476
|
}
|
|
411
|
-
if (this.#sandboxSecondsUsed >=
|
|
412
|
-
throw new RunSandboxExecutionError("sandbox_budget_exhausted", `The run has used its ${
|
|
477
|
+
if (this.#sandboxSecondsUsed >= runBudget.maxSeconds) {
|
|
478
|
+
throw new RunSandboxExecutionError("sandbox_budget_exhausted", `The run has used its ${runBudget.maxSeconds}-second sandbox limit.`, {
|
|
479
|
+
name: "sandbox.maxSeconds",
|
|
480
|
+
used: Math.round(this.#sandboxSecondsUsed),
|
|
481
|
+
value: runBudget.maxSeconds,
|
|
482
|
+
});
|
|
413
483
|
}
|
|
414
484
|
this.#sequence += 1;
|
|
415
485
|
const partialRecord = {
|
|
@@ -521,13 +591,16 @@ class ProviderRunSandboxSession {
|
|
|
521
591
|
const artifacts = await this.#collectArtifacts(admitted.outputDirectory, signal);
|
|
522
592
|
const filesChanged = [];
|
|
523
593
|
const ignoredPaths = [];
|
|
594
|
+
const notPersisted = [];
|
|
524
595
|
for (const candidate of this.#mounts.values()) {
|
|
525
596
|
if (candidate.workspace.mode !== "read_write")
|
|
526
597
|
continue;
|
|
527
598
|
const changes = await this.#syncBack(candidate, signal);
|
|
528
599
|
filesChanged.push(...changes.filesChanged);
|
|
529
600
|
ignoredPaths.push(...changes.ignoredPaths);
|
|
601
|
+
notPersisted.push(...changes.notPersisted);
|
|
530
602
|
}
|
|
603
|
+
this.#remember(filesChanged, notPersisted);
|
|
531
604
|
const record = {
|
|
532
605
|
id: commandId,
|
|
533
606
|
runId: runRequest.runId,
|
|
@@ -561,8 +634,19 @@ class ProviderRunSandboxSession {
|
|
|
561
634
|
throw new RunSandboxExecutionError("sandbox_command_timeout", `Sandbox command ${admitted.name} timed out after ${admitted.timeoutMs}ms.`);
|
|
562
635
|
}
|
|
563
636
|
let output;
|
|
637
|
+
const unsaved = notPersisted.length > 0
|
|
638
|
+
? {
|
|
639
|
+
notPersisted: notPersisted.map(({ alias, path, reason, message }) => ({
|
|
640
|
+
alias,
|
|
641
|
+
path,
|
|
642
|
+
reason,
|
|
643
|
+
message,
|
|
644
|
+
})),
|
|
645
|
+
}
|
|
646
|
+
: {};
|
|
564
647
|
if (admitted.expectsJson) {
|
|
565
|
-
|
|
648
|
+
const parsed = this.#parseHelperOutput(stdout, exitCode, stderr, artifacts);
|
|
649
|
+
output = isJsonObject(parsed) ? { ...parsed, ...unsaved } : parsed;
|
|
566
650
|
}
|
|
567
651
|
else if (admitted.kind === "code") {
|
|
568
652
|
output = stdout.length > 0 ? stdout : undefined;
|
|
@@ -576,6 +660,7 @@ class ProviderRunSandboxSession {
|
|
|
576
660
|
...(artifacts.length > 0
|
|
577
661
|
? { artifacts: artifacts.map((artifact) => artifact.id) }
|
|
578
662
|
: {}),
|
|
663
|
+
...unsaved,
|
|
579
664
|
};
|
|
580
665
|
}
|
|
581
666
|
return {
|
|
@@ -584,6 +669,7 @@ class ProviderRunSandboxSession {
|
|
|
584
669
|
stdout,
|
|
585
670
|
stderr,
|
|
586
671
|
...(artifacts.length > 0 ? { artifacts } : {}),
|
|
672
|
+
...(notPersisted.length > 0 ? { notPersisted } : {}),
|
|
587
673
|
};
|
|
588
674
|
}
|
|
589
675
|
#helperCommand() {
|
|
@@ -690,73 +776,162 @@ class ProviderRunSandboxSession {
|
|
|
690
776
|
}
|
|
691
777
|
return document;
|
|
692
778
|
}
|
|
779
|
+
/**
|
|
780
|
+
* Copy a read-write mount's changes back to its workspace store. Deletions
|
|
781
|
+
* apply only when the sandbox listing is complete, and changes that would
|
|
782
|
+
* grow the workspace beyond the store's advertised file or byte limit are
|
|
783
|
+
* refused, so the store always stays within the limits it is staged under.
|
|
784
|
+
*/
|
|
693
785
|
async #syncBack(mount, signal) {
|
|
694
786
|
const { provider, handle } = this.#input;
|
|
695
787
|
const { workspace } = mount;
|
|
696
788
|
const alias = workspace.alias;
|
|
697
789
|
const filesChanged = [];
|
|
698
790
|
const ignoredPaths = [];
|
|
699
|
-
const
|
|
791
|
+
const current = new Map();
|
|
792
|
+
const limits = workspace.store.limits ?? {};
|
|
793
|
+
const maxFileBytes = limits.maxFileBytes ?? 1_048_576;
|
|
794
|
+
const options = signal ? { signal } : undefined;
|
|
700
795
|
const snapshot = await this.#snapshot(mount, signal);
|
|
701
796
|
for (const item of snapshot.ignored) {
|
|
702
797
|
ignoredPaths.push({ alias, path: item.path, reason: item.reason });
|
|
798
|
+
current.set(item.path, item.reason === "symlink"
|
|
799
|
+
? {
|
|
800
|
+
reason: "symlink",
|
|
801
|
+
message: "Symbolic links are not saved to the workspace.",
|
|
802
|
+
}
|
|
803
|
+
: {
|
|
804
|
+
reason: "invalid-path",
|
|
805
|
+
message: "Hidden or unsupported names are never saved to the workspace.",
|
|
806
|
+
});
|
|
703
807
|
}
|
|
808
|
+
const tooLarge = {
|
|
809
|
+
reason: "too-large",
|
|
810
|
+
message: `Larger than the workspace's ${maxFileBytes}-byte file limit; not saved.`,
|
|
811
|
+
};
|
|
812
|
+
const overLimit = {
|
|
813
|
+
reason: "workspace-limit",
|
|
814
|
+
message: `Saving it would exceed the workspace limit (${limits.maxFiles ?? "unlimited"} files, ${limits.maxTotalBytes ?? "unlimited"} bytes); not saved. Delete files to make room.`,
|
|
815
|
+
};
|
|
704
816
|
const seen = new Set();
|
|
817
|
+
const writes = [];
|
|
705
818
|
for (const file of snapshot.files) {
|
|
706
819
|
seen.add(file.path);
|
|
707
820
|
const previous = mount.baseline.get(file.path);
|
|
708
821
|
if (file.sha256 == null || file.size > maxFileBytes) {
|
|
709
822
|
if (!previous || previous.sha256 !== undefined) {
|
|
710
823
|
ignoredPaths.push({ alias, path: file.path, reason: "size" });
|
|
824
|
+
current.set(file.path, tooLarge);
|
|
711
825
|
}
|
|
712
826
|
continue;
|
|
713
827
|
}
|
|
714
828
|
if (previous && previous.sha256 === file.sha256)
|
|
715
829
|
continue;
|
|
716
|
-
|
|
830
|
+
writes.push({
|
|
831
|
+
path: file.path,
|
|
832
|
+
size: file.size,
|
|
833
|
+
sha256: file.sha256,
|
|
834
|
+
previous,
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
if (snapshot.truncated) {
|
|
838
|
+
ignoredPaths.push({ alias, path: "*", reason: "limit" });
|
|
839
|
+
current.set("*", {
|
|
840
|
+
reason: "workspace-limit",
|
|
841
|
+
message: `The mount holds more than the workspace's ${limits.maxFiles ?? 5_000}-file limit. Files beyond the limit were not saved, and no deletions were saved.`,
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
let totalBytes = 0;
|
|
845
|
+
for (const entry of mount.baseline.values())
|
|
846
|
+
totalBytes += entry.size;
|
|
847
|
+
let fileCount = mount.baseline.size;
|
|
848
|
+
// An incomplete listing cannot distinguish a deleted file from one that
|
|
849
|
+
// was not listed, so nothing is deleted.
|
|
850
|
+
const deletions = snapshot.truncated
|
|
851
|
+
? []
|
|
852
|
+
: [...mount.baseline.keys()].filter((path) => !seen.has(path));
|
|
853
|
+
for (const previousPath of deletions) {
|
|
854
|
+
try {
|
|
855
|
+
await workspace.store.delete(previousPath, options);
|
|
856
|
+
totalBytes -= mount.baseline.get(previousPath)?.size ?? 0;
|
|
857
|
+
fileCount -= 1;
|
|
858
|
+
mount.baseline.delete(previousPath);
|
|
859
|
+
filesChanged.push({ alias, path: previousPath, action: "deleted" });
|
|
860
|
+
}
|
|
861
|
+
catch (error) {
|
|
862
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
863
|
+
ignoredPaths.push({
|
|
864
|
+
alias,
|
|
865
|
+
path: previousPath,
|
|
866
|
+
reason: `store: ${message}`,
|
|
867
|
+
});
|
|
868
|
+
current.set(previousPath, { reason: "store-rejected", message });
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
// Changes that do not grow the workspace first, then growth in path order
|
|
872
|
+
// while it fits.
|
|
873
|
+
const grows = (write) => !write.previous || write.size > write.previous.size;
|
|
874
|
+
const ordered = [
|
|
875
|
+
...writes.filter((write) => !grows(write)),
|
|
876
|
+
...writes
|
|
877
|
+
.filter(grows)
|
|
878
|
+
.sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0),
|
|
879
|
+
];
|
|
880
|
+
for (const write of ordered) {
|
|
881
|
+
const addedFiles = write.previous ? 0 : 1;
|
|
882
|
+
const addedBytes = write.size - (write.previous?.size ?? 0);
|
|
883
|
+
if (addedBytes > 0 || addedFiles > 0
|
|
884
|
+
? (limits.maxTotalBytes != null &&
|
|
885
|
+
totalBytes + addedBytes > limits.maxTotalBytes) ||
|
|
886
|
+
(limits.maxFiles != null &&
|
|
887
|
+
fileCount + addedFiles > limits.maxFiles)
|
|
888
|
+
: false) {
|
|
889
|
+
ignoredPaths.push({ alias, path: write.path, reason: "limit" });
|
|
890
|
+
current.set(write.path, overLimit);
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
const data = await provider.readFile(handle, assertSandboxPath(`${mount.directory}/${write.path}`), options);
|
|
717
894
|
if (data.byteLength > maxFileBytes) {
|
|
718
|
-
ignoredPaths.push({ alias, path:
|
|
895
|
+
ignoredPaths.push({ alias, path: write.path, reason: "size" });
|
|
896
|
+
current.set(write.path, tooLarge);
|
|
719
897
|
continue;
|
|
720
898
|
}
|
|
721
899
|
try {
|
|
722
|
-
await workspace.store.write(
|
|
723
|
-
|
|
900
|
+
await workspace.store.write(write.path, data, options);
|
|
901
|
+
totalBytes += data.byteLength - (write.previous?.size ?? 0);
|
|
902
|
+
fileCount += addedFiles;
|
|
903
|
+
mount.baseline.set(write.path, {
|
|
724
904
|
size: data.byteLength,
|
|
725
905
|
sha256: sha256(data),
|
|
726
906
|
});
|
|
727
907
|
filesChanged.push({
|
|
728
908
|
alias,
|
|
729
|
-
path:
|
|
730
|
-
action: previous ? "modified" : "created",
|
|
909
|
+
path: write.path,
|
|
910
|
+
action: write.previous ? "modified" : "created",
|
|
731
911
|
size: data.byteLength,
|
|
732
|
-
sha256:
|
|
912
|
+
sha256: write.sha256,
|
|
733
913
|
});
|
|
734
914
|
}
|
|
735
915
|
catch (error) {
|
|
916
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
736
917
|
ignoredPaths.push({
|
|
737
918
|
alias,
|
|
738
|
-
path:
|
|
739
|
-
reason: `store: ${
|
|
919
|
+
path: write.path,
|
|
920
|
+
reason: `store: ${message}`,
|
|
740
921
|
});
|
|
922
|
+
current.set(write.path, { reason: "store-rejected", message });
|
|
741
923
|
}
|
|
742
924
|
}
|
|
743
|
-
|
|
744
|
-
|
|
925
|
+
// Report each unsaved path once per reason; a path that is later saved or
|
|
926
|
+
// removed is forgotten, so it is reported again if the problem returns.
|
|
927
|
+
const notPersisted = [];
|
|
928
|
+
for (const [path, detail] of current) {
|
|
929
|
+
if (mount.reported.get(path) === detail.reason)
|
|
745
930
|
continue;
|
|
746
|
-
|
|
747
|
-
await workspace.store.delete(previousPath, signal ? { signal } : undefined);
|
|
748
|
-
mount.baseline.delete(previousPath);
|
|
749
|
-
filesChanged.push({ alias, path: previousPath, action: "deleted" });
|
|
750
|
-
}
|
|
751
|
-
catch (error) {
|
|
752
|
-
ignoredPaths.push({
|
|
753
|
-
alias,
|
|
754
|
-
path: previousPath,
|
|
755
|
-
reason: `store: ${error instanceof Error ? error.message : String(error)}`,
|
|
756
|
-
});
|
|
757
|
-
}
|
|
931
|
+
notPersisted.push({ alias, path, ...detail });
|
|
758
932
|
}
|
|
759
|
-
|
|
933
|
+
mount.reported = new Map([...current].map(([path, detail]) => [path, detail.reason]));
|
|
934
|
+
return { filesChanged, ignoredPaths, notPersisted };
|
|
760
935
|
}
|
|
761
936
|
async #snapshot(mount, signal) {
|
|
762
937
|
const { provider, handle, policy } = this.#input;
|
|
@@ -826,7 +1001,7 @@ class ProviderRunSandboxSession {
|
|
|
826
1001
|
]
|
|
827
1002
|
: [])
|
|
828
1003
|
: [];
|
|
829
|
-
return { files, ignored };
|
|
1004
|
+
return { files, ignored, truncated: parsed.truncated === true };
|
|
830
1005
|
}
|
|
831
1006
|
}
|
|
832
1007
|
const mediaTypeFor = (filename) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clearideas/agent-runtime-sandbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Provider-neutral sandbox lifecycle and code execution adapters for Clear Ideas Agent Runtime.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Clear Ideas Incorporated",
|
|
@@ -41,8 +41,8 @@
|
|
|
41
41
|
"postpack": "node ../../scripts/cleanup-package-license.mjs"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@clearideas/agent-runtime-contracts": "0.
|
|
45
|
-
"@clearideas/agent-runtime-core": "0.
|
|
44
|
+
"@clearideas/agent-runtime-contracts": "0.10.0",
|
|
45
|
+
"@clearideas/agent-runtime-core": "0.10.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@types/node": "^26.6.6",
|