@songtonyli/dsh-cli 0.1.58 → 0.1.60
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 +1 -1
- package/bin/dsh.mjs +1 -1
- package/node_modules/@deepseek-ai/dsh-agent-preset/skills/cordis-composition-reference/references/packages.md +1 -0
- package/node_modules/@deepseek-ai/dsh-api-session-controller/lib/client.js +1 -0
- package/node_modules/@deepseek-ai/dsh-client-ui-primitives/package.json +1 -1
- package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
- package/node_modules/@deepseek-ai/dsh-client-ui-slots/package.json +2 -2
- package/node_modules/@deepseek-ai/dsh-sandbox/lib/index.js +59 -4
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/README.i18n.yaml +73 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/README.md +166 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/README.zh.md +166 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/lib/exec-shim.js +227 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/lib/index.js +704 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/package.json +62 -0
- package/node_modules/@deepseek-ai/dsh-sandbox-policy/README.i18n.yaml +2 -2
- package/node_modules/@deepseek-ai/dsh-sandbox-policy/README.md +1 -1
- package/node_modules/@deepseek-ai/dsh-sandbox-policy/README.zh.md +1 -1
- package/node_modules/@deepseek-ai/dsh-sandbox-policy/lib/index.js +23 -0
- package/node_modules/@deepseek-ai/dsh-session/lib/index.js +1 -0
- package/node_modules/@deepseek-ai/dsh-session/lib/types/known-event-types.js +1 -0
- package/node_modules/@deepseek-ai/dsh-session-persistence-jsonl/lib/worker.cjs +1 -0
- package/node_modules/@deepseek-ai/dsh-system-prompt/lib/index.js +1 -0
- package/node_modules/@deepseek-ai/dsh-tool-cordis/lib/types/api-catalog.js +22 -0
- package/node_modules/@deepseek-ai/dsh-tool-fs/README.i18n.yaml +2 -2
- package/node_modules/@deepseek-ai/dsh-tool-fs/README.md +1 -1
- package/node_modules/@deepseek-ai/dsh-tool-fs/README.zh.md +1 -1
- package/node_modules/@deepseek-ai/dsh-tool-fs/lib/index.js +24 -0
- package/node_modules/@deepseek-ai/dsh-tool-fs-search/README.i18n.yaml +4 -4
- package/node_modules/@deepseek-ai/dsh-tool-fs-search/README.md +2 -2
- package/node_modules/@deepseek-ai/dsh-tool-fs-search/README.zh.md +2 -2
- package/node_modules/@deepseek-ai/dsh-tool-fs-search/lib/index.js +28 -3
- package/node_modules/@deepseek-ai/dsh-tool-fs-search/package.json +7 -2
- package/node_modules/@deepseek-ai/dsh-tool-str-replace-editor/README.i18n.yaml +2 -2
- package/node_modules/@deepseek-ai/dsh-tool-str-replace-editor/README.md +1 -1
- package/node_modules/@deepseek-ai/dsh-tool-str-replace-editor/README.zh.md +1 -1
- package/node_modules/@deepseek-ai/dsh-tool-str-replace-editor/lib/index.js +14 -6
- package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +8 -8
- package/node_modules/@deepseek-ai/dsh-tui-app/README.md +13 -7
- package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +13 -7
- package/node_modules/@deepseek-ai/dsh-tui-app/cordis.patch.yml +20 -0
- package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +2403 -2033
- package/node_modules/@deepseek-ai/dsh-tui-app/package.json +1 -0
- package/node_modules/@deepseek-ai/libreoffice-kit/README.md +20 -5
- package/node_modules/@deepseek-ai/libreoffice-kit/README.zh.md +21 -6
- package/node_modules/@deepseek-ai/libreoffice-kit/lib/cli.js +499 -253
- package/node_modules/@deepseek-ai/libreoffice-kit/lib/font-snapshot-worker.js +37 -26
- package/node_modules/@deepseek-ai/libreoffice-kit/lib/index.js +498 -252
- package/node_modules/@deepseek-ai/libreoffice-kit/lib/worker.js +39 -13
- package/node_modules/@deepseek-ai/libreoffice-kit/package.json +5 -3
- package/node_modules/@earendil-works/pi-ai/README.md +4 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +117 -18
- package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/constrained-sampling.js +19 -8
- package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.js +2 -0
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js +5 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/anthropic.js +24 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/azure-openai-responses.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/baseten.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/cerebras.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/radius.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/overflow.js +2 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/provider-retry.js +3 -2
- package/node_modules/@earendil-works/pi-ai/package.json +6 -2
- package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/node_modules/@shikijs/core/dist/index.mjs +15 -12
- package/node_modules/@shikijs/core/package.json +4 -4
- package/node_modules/@shikijs/engine-javascript/dist/engine-compile.mjs +2 -4
- package/node_modules/@shikijs/engine-javascript/dist/engine-raw.mjs +2 -4
- package/node_modules/@shikijs/engine-javascript/dist/index.mjs +1 -1
- package/node_modules/@shikijs/engine-javascript/dist/string-TLTPtCPs.mjs +115 -0
- package/node_modules/@shikijs/engine-javascript/package.json +3 -3
- package/node_modules/@shikijs/engine-oniguruma/package.json +3 -3
- package/node_modules/@shikijs/langs/dist/ada.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/ahk.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/beancount.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/coq.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/csharp.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/css.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/dart.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/dotenv.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/emacs-lisp.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/gn.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/handlebars.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/julia.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/latex.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/luau.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/marko.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/moonbit.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/nextflow-groovy.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/nextflow.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/nushell.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/php.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/powershell.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/ruby.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/rust.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/sql.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/svelte.mjs +1 -1
- package/node_modules/@shikijs/langs/dist/twig.mjs +3 -3
- package/node_modules/@shikijs/langs/dist/typespec.mjs +1 -1
- package/node_modules/@shikijs/langs/package.json +3 -3
- package/node_modules/@shikijs/primitive/dist/index.mjs +2 -1
- package/node_modules/@shikijs/primitive/package.json +3 -3
- package/node_modules/@shikijs/themes/dist/monokai.mjs +1 -1
- package/node_modules/@shikijs/themes/dist/one-dark-pro.mjs +1 -1
- package/node_modules/@shikijs/themes/dist/red.mjs +1 -1
- package/node_modules/@shikijs/themes/dist/solarized-dark.mjs +1 -1
- package/node_modules/@shikijs/themes/dist/solarized-light.mjs +1 -1
- package/node_modules/@shikijs/themes/package.json +3 -3
- package/node_modules/@shikijs/types/package.json +1 -1
- package/node_modules/@smithy/core/dist-cjs/submodules/client/index.js +33 -11
- package/node_modules/@smithy/core/dist-cjs/submodules/schema/index.js +3 -0
- package/node_modules/@smithy/core/dist-es/submodules/client/smithy-client/exceptions.js +33 -11
- package/node_modules/@smithy/core/dist-es/submodules/schema/TypeRegistry.js +3 -0
- package/node_modules/@smithy/core/package.json +1 -1
- package/node_modules/node-addon-native-custom-loader/lib/index.js +23 -10
- package/node_modules/node-addon-native-custom-loader/package.json +6 -1
- package/node_modules/node-addon-require-builtin/package.json +7 -2
- package/node_modules/powershell-utils/index.js +40 -19
- package/node_modules/powershell-utils/package.json +1 -1
- package/node_modules/powershell-utils/readme.md +69 -13
- package/node_modules/shiki/package.json +10 -10
- package/node_modules/wsl-utils/index.js +18 -37
- package/node_modules/wsl-utils/package.json +2 -2
- package/package.json +33 -29
- package/node_modules/@shikijs/engine-javascript/dist/scanner-DX8LRFGE.mjs +0 -77
- package/node_modules/open/node_modules/powershell-utils/index.js +0 -79
- package/node_modules/open/node_modules/powershell-utils/license +0 -9
- package/node_modules/open/node_modules/powershell-utils/package.json +0 -47
- package/node_modules/open/node_modules/powershell-utils/readme.md +0 -209
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { accessSync, constants, existsSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { delimiter, isAbsolute, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import z from "@deepseek-ai/schemastery";
|
|
5
|
+
import { z as z$1 } from "zod";
|
|
6
|
+
import { CommandDefinitionId } from "@deepseek-ai/dsh-commands/brand";
|
|
7
|
+
import { SandboxUnavailableError, canonicalPath, matchesNameGlob } from "@deepseek-ai/dsh-sandbox";
|
|
8
|
+
import { LocalSandboxProvider } from "@deepseek-ai/dsh-sandbox-local";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { execFile } from "node:child_process";
|
|
11
|
+
//#region lib/types/secrets.js
|
|
12
|
+
/**
|
|
13
|
+
* Workspace scan for files the container hides from confined commands. The
|
|
14
|
+
* scan runs when a container starts, because `container` masks only paths
|
|
15
|
+
* that exist at creation; files created later are hidden from host file tools
|
|
16
|
+
* by the read scope but stay readable inside the running container.
|
|
17
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container/secrets
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Find files and symlinks under `root` whose name matches a hidden-name glob,
|
|
21
|
+
* without following symlinked directories.
|
|
22
|
+
* @param root - the canonical workspace root.
|
|
23
|
+
* @param scan - names, depth, and skipped directory names.
|
|
24
|
+
* @returns absolute paths in walk order; unreadable directories are skipped.
|
|
25
|
+
*/
|
|
26
|
+
function findHiddenFiles(root, scan) {
|
|
27
|
+
const found = [];
|
|
28
|
+
const walk = (dir, depth) => {
|
|
29
|
+
let entries;
|
|
30
|
+
try {
|
|
31
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
32
|
+
} catch {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
const path = join(dir, entry.name);
|
|
37
|
+
if (entry.isDirectory()) {
|
|
38
|
+
if (depth < scan.maxDepth && !scan.skipDirs.includes(entry.name)) walk(path, depth + 1);
|
|
39
|
+
} else if (scan.names.some((glob) => matchesNameGlob(entry.name, glob))) found.push(path);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
walk(root, 0);
|
|
43
|
+
return found;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region lib/types/pool.js
|
|
47
|
+
/**
|
|
48
|
+
* The provider's owned containers: one per (canonical workspace root, confined
|
|
49
|
+
* mode), shared by every session and subagent in that workspace and mode. The
|
|
50
|
+
* workspace is bind-mounted at its canonical path, plus its lexical path when
|
|
51
|
+
* that differs; `read-only` mounts it read-only, and `workspace-write` mounts
|
|
52
|
+
* its protected directories (such as `.git`) read-only. Extra read-only
|
|
53
|
+
* directories are mounted at their own paths, and hidden files found at start
|
|
54
|
+
* are masked. A cached container is re-inspected at most once per `recheckMs`
|
|
55
|
+
* and restarted when gone.
|
|
56
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container/pool
|
|
57
|
+
*/
|
|
58
|
+
/** Label naming the owning DSH process id. */
|
|
59
|
+
const PID_LABEL = "dsh.pid";
|
|
60
|
+
/** Whether `path` is an existing directory. */
|
|
61
|
+
function isDirectory(path) {
|
|
62
|
+
return statSync(path, { throwIfNoEntry: false })?.isDirectory() === true;
|
|
63
|
+
}
|
|
64
|
+
/** Owned-container cache over the `container` runtime. */
|
|
65
|
+
var ContainerPool = class {
|
|
66
|
+
runtime;
|
|
67
|
+
options;
|
|
68
|
+
slots = /* @__PURE__ */ new Map();
|
|
69
|
+
constructor(runtime, options) {
|
|
70
|
+
this.runtime = runtime;
|
|
71
|
+
this.options = options;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Return the name of a running container for `policy`, starting one when
|
|
75
|
+
* none is cached or the cached one is gone. Concurrent callers share one start.
|
|
76
|
+
* @param policy - the confined mode and workspace root.
|
|
77
|
+
* @returns the container name.
|
|
78
|
+
* @throws {Error} when the root cannot be mounted or the runtime fails.
|
|
79
|
+
*/
|
|
80
|
+
async ensure(policy) {
|
|
81
|
+
const root = canonicalPath(policy.workspaceRoot);
|
|
82
|
+
if (root.includes(",") || policy.workspaceRoot.includes(",")) throw new Error(`workspace root ${JSON.stringify(policy.workspaceRoot)} contains a comma, which \`container --mount\` cannot express`);
|
|
83
|
+
const key = `${policy.mode}\0${root}`;
|
|
84
|
+
const now = this.options.now();
|
|
85
|
+
let slot = this.slots.get(key);
|
|
86
|
+
if (slot === void 0) {
|
|
87
|
+
const name = `dsh-${createHash("sha256").update(key).digest("hex").slice(0, 12)}-${this.options.pid}`;
|
|
88
|
+
slot = {
|
|
89
|
+
mode: policy.mode,
|
|
90
|
+
root,
|
|
91
|
+
name,
|
|
92
|
+
checkedAt: now,
|
|
93
|
+
ready: this.start(name, policy.mode, root, policy.workspaceRoot)
|
|
94
|
+
};
|
|
95
|
+
this.slots.set(key, slot);
|
|
96
|
+
} else if (now - slot.checkedAt >= this.options.recheckMs) {
|
|
97
|
+
const { name } = slot;
|
|
98
|
+
slot.checkedAt = now;
|
|
99
|
+
slot.ready = slot.ready.then(() => this.start(name, policy.mode, root, policy.workspaceRoot));
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
await slot.ready;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (this.slots.get(key) === slot) this.slots.delete(key);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
return slot.name;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Every owned container.
|
|
111
|
+
* @returns the entries in first-start order.
|
|
112
|
+
*/
|
|
113
|
+
entries() {
|
|
114
|
+
return [...this.slots.values()].map(({ mode, root, name }) => ({
|
|
115
|
+
mode,
|
|
116
|
+
root,
|
|
117
|
+
name
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Delete containers left by DSH processes that no longer exist.
|
|
122
|
+
* @param isAlive - whether a process id names a live process.
|
|
123
|
+
*/
|
|
124
|
+
async sweep(isAlive) {
|
|
125
|
+
const stale = (await this.runtime.list()).filter((container) => {
|
|
126
|
+
const pid = Number(container.labels[PID_LABEL]);
|
|
127
|
+
return Number.isSafeInteger(pid) && pid > 0 && pid !== this.options.pid && !isAlive(pid);
|
|
128
|
+
});
|
|
129
|
+
await Promise.all(stale.map((container) => this.runtime.remove(container.id)));
|
|
130
|
+
}
|
|
131
|
+
/** Delete every owned container, after any in-flight start settles. */
|
|
132
|
+
async dispose() {
|
|
133
|
+
const slots = [...this.slots.values()];
|
|
134
|
+
this.slots.clear();
|
|
135
|
+
await Promise.allSettled(slots.map((slot) => slot.ready));
|
|
136
|
+
await Promise.all(slots.map((slot) => this.runtime.remove(slot.name)));
|
|
137
|
+
}
|
|
138
|
+
async start(name, mode, root, lexical) {
|
|
139
|
+
await this.runtime.ensureService();
|
|
140
|
+
const state = await this.runtime.state(name);
|
|
141
|
+
if (state === "running") return;
|
|
142
|
+
if (state === "stopped") await this.runtime.remove(name);
|
|
143
|
+
const spellings = (path) => lexical === root ? [path] : [path, join(lexical, path.slice(root.length))];
|
|
144
|
+
const bind = (source, readonly) => spellings(source).map((target) => ({
|
|
145
|
+
source,
|
|
146
|
+
target,
|
|
147
|
+
readonly
|
|
148
|
+
}));
|
|
149
|
+
const protectedDirs = mode === "workspace-write" ? this.options.protectedPaths.map((path) => join(root, path)).filter(isDirectory) : [];
|
|
150
|
+
await this.runtime.run({
|
|
151
|
+
name,
|
|
152
|
+
labels: {
|
|
153
|
+
[PID_LABEL]: String(this.options.pid),
|
|
154
|
+
"dsh.workspace": root,
|
|
155
|
+
"dsh.mode": mode
|
|
156
|
+
},
|
|
157
|
+
mounts: [
|
|
158
|
+
...bind(root, mode === "read-only"),
|
|
159
|
+
...protectedDirs.flatMap((dir) => bind(dir, true)),
|
|
160
|
+
...this.options.readOnlyMounts.flatMap((path) => {
|
|
161
|
+
const source = canonicalPath(path);
|
|
162
|
+
return isDirectory(source) ? [...new Set([source, path])].map((target) => ({
|
|
163
|
+
source,
|
|
164
|
+
target,
|
|
165
|
+
readonly: true
|
|
166
|
+
})) : [];
|
|
167
|
+
})
|
|
168
|
+
],
|
|
169
|
+
maskedPaths: findHiddenFiles(root, this.options.hidden).flatMap(spellings)
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region lib/types/runtime.js
|
|
175
|
+
/**
|
|
176
|
+
* Typed calls to the Apple `container` CLI: API-server status and start,
|
|
177
|
+
* container state, detached runs, deletion, and listing. Each call spawns the
|
|
178
|
+
* configured executable once and never touches containers it was not asked
|
|
179
|
+
* about.
|
|
180
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container/runtime
|
|
181
|
+
*/
|
|
182
|
+
/** First non-empty line of CLI output, for error messages. */
|
|
183
|
+
function firstLine(result) {
|
|
184
|
+
return `${result.stderr}\n${result.stdout}`.split("\n").map((line) => line.trim()).find((line) => line !== "") ?? `exit ${result.code}`;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Typed `container` CLI client. Methods reject with an `Error` naming the
|
|
188
|
+
* failed operation and the CLI's first output line.
|
|
189
|
+
*/
|
|
190
|
+
var ContainerRuntime = class {
|
|
191
|
+
options;
|
|
192
|
+
constructor(options) {
|
|
193
|
+
this.options = options;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Ensure the API server answers, starting it once with the default kernel
|
|
197
|
+
* when `autoStart` is set.
|
|
198
|
+
* @throws {Error} when the server is stopped and cannot be started.
|
|
199
|
+
*/
|
|
200
|
+
async ensureService() {
|
|
201
|
+
const status = await this.call(["system", "status"]);
|
|
202
|
+
if (status.code === 0) return;
|
|
203
|
+
if (!this.options.autoStart) throw new Error(`the container API server is not running (${firstLine(status)}); run \`container system start\``);
|
|
204
|
+
const start = await this.call([
|
|
205
|
+
"system",
|
|
206
|
+
"start",
|
|
207
|
+
"--enable-kernel-install"
|
|
208
|
+
]);
|
|
209
|
+
if (start.code !== 0) throw new Error(`container system start failed: ${firstLine(start)}`);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Read a container's state.
|
|
213
|
+
* @param name - the container name.
|
|
214
|
+
* @returns `missing` when inspection fails, otherwise running or stopped.
|
|
215
|
+
*/
|
|
216
|
+
async state(name) {
|
|
217
|
+
const result = await this.call(["inspect", name]);
|
|
218
|
+
if (result.code !== 0) return "missing";
|
|
219
|
+
const [entry] = JSON.parse(result.stdout);
|
|
220
|
+
return entry?.status?.state === "running" ? "running" : "stopped";
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Start a detached container that idles until deleted.
|
|
224
|
+
* @param spec - name, mounts, and labels.
|
|
225
|
+
* @throws {Error} when `container run` fails.
|
|
226
|
+
*/
|
|
227
|
+
async run(spec) {
|
|
228
|
+
const result = await this.call([
|
|
229
|
+
"run",
|
|
230
|
+
"--detach",
|
|
231
|
+
"--init",
|
|
232
|
+
"--rm",
|
|
233
|
+
"--name",
|
|
234
|
+
spec.name,
|
|
235
|
+
...Object.entries(spec.labels).flatMap(([key, value]) => ["--label", `${key}=${value}`]),
|
|
236
|
+
...this.options.cpus === void 0 ? [] : ["--cpus", String(this.options.cpus)],
|
|
237
|
+
...this.options.memory === void 0 ? [] : ["--memory", this.options.memory],
|
|
238
|
+
...spec.mounts.flatMap((mount) => ["--mount", `type=bind,source=${mount.source},target=${mount.target}${mount.readonly ? ",readonly" : ""}`]),
|
|
239
|
+
...spec.maskedPaths.flatMap((path) => ["--masked-path", path]),
|
|
240
|
+
this.options.image,
|
|
241
|
+
"sleep",
|
|
242
|
+
"infinity"
|
|
243
|
+
]);
|
|
244
|
+
if (result.code !== 0) throw new Error(`container run failed: ${firstLine(result)}`);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Delete a container, stopping it first; an absent container is not an error.
|
|
248
|
+
* @param name - the container name.
|
|
249
|
+
*/
|
|
250
|
+
async remove(name) {
|
|
251
|
+
await this.call([
|
|
252
|
+
"delete",
|
|
253
|
+
"--force",
|
|
254
|
+
name
|
|
255
|
+
]);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* List every container with its labels.
|
|
259
|
+
* @returns the containers, or an empty list when listing fails.
|
|
260
|
+
*/
|
|
261
|
+
async list() {
|
|
262
|
+
const result = await this.call([
|
|
263
|
+
"list",
|
|
264
|
+
"--all",
|
|
265
|
+
"--format",
|
|
266
|
+
"json"
|
|
267
|
+
]);
|
|
268
|
+
if (result.code !== 0) return [];
|
|
269
|
+
return JSON.parse(result.stdout).map((entry) => ({
|
|
270
|
+
id: entry.configuration.id,
|
|
271
|
+
labels: entry.configuration.labels ?? {}
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
/** Run the CLI once; a spawn failure (such as a missing executable) rejects. */
|
|
275
|
+
call(args) {
|
|
276
|
+
return new Promise((resolve, reject) => {
|
|
277
|
+
execFile(this.options.executable, args, { maxBuffer: 64 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
278
|
+
if (error !== null && typeof error.code !== "number") {
|
|
279
|
+
reject(/* @__PURE__ */ new Error(`cannot run ${JSON.stringify(this.options.executable)}: ${error.message}`));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
resolve({
|
|
283
|
+
code: error === null ? 0 : error.code,
|
|
284
|
+
stdout,
|
|
285
|
+
stderr
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region lib/types/host.js
|
|
293
|
+
/**
|
|
294
|
+
* Host support for the container backend and the `auto` backend resolution:
|
|
295
|
+
* Apple `container` runs only on macOS on Apple silicon, so `auto` selects
|
|
296
|
+
* the container there when its CLI resolves and the local backend everywhere
|
|
297
|
+
* else.
|
|
298
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container/host
|
|
299
|
+
*/
|
|
300
|
+
/**
|
|
301
|
+
* Whether `executable` resolves to an executable file: an absolute path is
|
|
302
|
+
* checked directly, a bare name against each `PATH` directory.
|
|
303
|
+
* @param executable - the configured `container` CLI.
|
|
304
|
+
* @param path - the `PATH` value.
|
|
305
|
+
* @returns true when an executable file is found.
|
|
306
|
+
*/
|
|
307
|
+
function executableResolves(executable, path) {
|
|
308
|
+
return (isAbsolute(executable) ? [executable] : (path ?? "").split(delimiter).filter((dir) => dir !== "").map((dir) => join(dir, executable))).some((candidate) => {
|
|
309
|
+
try {
|
|
310
|
+
accessSync(candidate, constants.X_OK);
|
|
311
|
+
return true;
|
|
312
|
+
} catch {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Whether this host can run the container backend: macOS on Apple silicon
|
|
319
|
+
* with the `container` CLI resolvable.
|
|
320
|
+
* @param executable - the configured `container` CLI.
|
|
321
|
+
* @param host - platform, architecture, and `PATH`.
|
|
322
|
+
* @returns true when the container backend is usable.
|
|
323
|
+
*/
|
|
324
|
+
function containerSupported(executable, host) {
|
|
325
|
+
return host.platform === "darwin" && host.arch === "arm64" && executableResolves(executable, host.path);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Resolve the configured backend for this host: `auto` becomes `container`
|
|
329
|
+
* where {@link containerSupported} holds and `local` elsewhere; a fixed
|
|
330
|
+
* backend is kept as configured.
|
|
331
|
+
* @param backend - the configured backend.
|
|
332
|
+
* @param supported - whether the container backend is usable here.
|
|
333
|
+
* @returns the deployment default backend.
|
|
334
|
+
*/
|
|
335
|
+
function resolveBackend(backend, supported) {
|
|
336
|
+
if (backend !== "auto") return backend;
|
|
337
|
+
return supported ? "container" : "local";
|
|
338
|
+
}
|
|
339
|
+
//#endregion
|
|
340
|
+
//#region lib/types/session-backend.js
|
|
341
|
+
/**
|
|
342
|
+
* Per-session sandbox-backend choice with the session log as the store. A
|
|
343
|
+
* runtime switch is one log-only `sandbox/backend` event on the session it
|
|
344
|
+
* applies to; the `sandboxBackend` projection unit folds the last one, and the
|
|
345
|
+
* deployment default applies before any switch. The choice survives restart
|
|
346
|
+
* by replay, and two sessions never see each other's choice.
|
|
347
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container/session-backend
|
|
348
|
+
*/
|
|
349
|
+
/** Every {@link SandboxBackend}, for argument validation and option advertisement. */
|
|
350
|
+
const SANDBOX_BACKENDS = ["container", "local"];
|
|
351
|
+
/**
|
|
352
|
+
* Whether `value` names a {@link SandboxBackend}.
|
|
353
|
+
* @param value - untrusted text, such as a slash-command argument.
|
|
354
|
+
* @returns true when `value` is a backend name.
|
|
355
|
+
*/
|
|
356
|
+
function isSandboxBackend(value) {
|
|
357
|
+
return SANDBOX_BACKENDS.includes(value);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Append the one `sandbox/backend` event that switches `session`'s backend;
|
|
361
|
+
* the session's next confined call runs on `backend`.
|
|
362
|
+
* @param session - the session the choice belongs to.
|
|
363
|
+
* @param backend - the backend every later confined call in this session uses.
|
|
364
|
+
*/
|
|
365
|
+
function setSandboxBackend(session, backend) {
|
|
366
|
+
session.append("sandbox/backend", { backend });
|
|
367
|
+
}
|
|
368
|
+
//#endregion
|
|
369
|
+
//#region lib/types/shim.js
|
|
370
|
+
/** Prefix of the single stderr line the shim writes for {@link SHIM_FAILURE_EXIT}. */
|
|
371
|
+
const SHIM_FAILURE_PREFIX = "dsh-container-exec: ";
|
|
372
|
+
/**
|
|
373
|
+
* Encode a shim invocation as the argument list that follows the shim entry.
|
|
374
|
+
* @param args - the invocation; denylist entries are environment names and never contain commas.
|
|
375
|
+
* @returns `[executable, container, denylist, '--', ...argv]`.
|
|
376
|
+
*/
|
|
377
|
+
function encodeShimArgs(args) {
|
|
378
|
+
return [
|
|
379
|
+
args.executable,
|
|
380
|
+
args.container,
|
|
381
|
+
args.denylist.join(","),
|
|
382
|
+
"--",
|
|
383
|
+
...args.argv
|
|
384
|
+
];
|
|
385
|
+
}
|
|
386
|
+
//#endregion
|
|
387
|
+
//#region lib/types/index.js
|
|
388
|
+
/**
|
|
389
|
+
* Apple container sandbox backend. Registers as `ctx.sandbox` and routes each
|
|
390
|
+
* confined call by its session's backend: `local` delegates to the inherited
|
|
391
|
+
* local platform chain, while `container` runs the argv through an exec shim
|
|
392
|
+
* inside a Linux container VM that bind-mounts the workspace at its own
|
|
393
|
+
* absolute path (read-only under `read-only`). Host file effects stay inside
|
|
394
|
+
* the workspace, and the guest cannot read unmounted host paths.
|
|
395
|
+
*
|
|
396
|
+
* Each session's backend is the last `sandbox/backend` event in its log,
|
|
397
|
+
* otherwise the configured default; the shared `/sandbox` command reports and
|
|
398
|
+
* switches it. Before each request, a `sandbox:backend` runtime-context
|
|
399
|
+
* contribution tells the model when its confined commands run in the container.
|
|
400
|
+
* @module @deepseek-ai/dsh-sandbox-apple-container
|
|
401
|
+
*/
|
|
402
|
+
/**
|
|
403
|
+
* Environment names the shim withholds from the guest by default: host-specific
|
|
404
|
+
* paths and session plumbing, plus every credential-shaped name, even one a
|
|
405
|
+
* composition passes deliberately.
|
|
406
|
+
*/
|
|
407
|
+
const DEFAULT_ENV_DENYLIST = [
|
|
408
|
+
"PATH",
|
|
409
|
+
"HOME",
|
|
410
|
+
"TMPDIR",
|
|
411
|
+
"SHELL",
|
|
412
|
+
"USER",
|
|
413
|
+
"LOGNAME",
|
|
414
|
+
"PWD",
|
|
415
|
+
"OLDPWD",
|
|
416
|
+
"SHLVL",
|
|
417
|
+
"SSH_AUTH_SOCK",
|
|
418
|
+
"__CF*",
|
|
419
|
+
"XPC_*",
|
|
420
|
+
"DYLD_*",
|
|
421
|
+
"TERM_PROGRAM*",
|
|
422
|
+
"*KEY*",
|
|
423
|
+
"*TOKEN*",
|
|
424
|
+
"*SECRET*",
|
|
425
|
+
"*PASSWORD*",
|
|
426
|
+
"*PASSWD*",
|
|
427
|
+
"*CREDENTIAL*",
|
|
428
|
+
"*COOKIE*",
|
|
429
|
+
"*PRIVATE*",
|
|
430
|
+
"AWS_*"
|
|
431
|
+
];
|
|
432
|
+
/** File names hidden from confined commands and host file tools by default: environment files, private keys, and credential stores. */
|
|
433
|
+
const DEFAULT_HIDDEN_FILES = [
|
|
434
|
+
".env",
|
|
435
|
+
".env.*",
|
|
436
|
+
".envrc",
|
|
437
|
+
"*.pem",
|
|
438
|
+
"*.key",
|
|
439
|
+
"*.p12",
|
|
440
|
+
"*.pfx",
|
|
441
|
+
"id_rsa*",
|
|
442
|
+
"id_dsa*",
|
|
443
|
+
"id_ecdsa*",
|
|
444
|
+
"id_ed25519*",
|
|
445
|
+
".npmrc",
|
|
446
|
+
".pypirc",
|
|
447
|
+
".netrc",
|
|
448
|
+
".git-credentials"
|
|
449
|
+
];
|
|
450
|
+
/** Stderr text of a write the read-only workspace mount refused. */
|
|
451
|
+
const CONTAINER_DENIAL_SIGNATURES = ["read-only file system"];
|
|
452
|
+
/** The shim's own failure line, emitted only when the command never started. */
|
|
453
|
+
const CONTAINER_RUNNER_FAILURE_RULES = [{
|
|
454
|
+
allowedExitCodes: [125],
|
|
455
|
+
fatalSignatures: [SHIM_FAILURE_PREFIX]
|
|
456
|
+
}];
|
|
457
|
+
/** The sandbox-backend projection's state schema. */
|
|
458
|
+
const sandboxBackendStateSchema = z$1.union([z$1.literal("container"), z$1.literal("local")]).nullable();
|
|
459
|
+
/**
|
|
460
|
+
* The exec-shim launch prefix for a provider module at `moduleUrl`: the built
|
|
461
|
+
* sibling `exec-shim.js` under plain Node when present, otherwise the source
|
|
462
|
+
* entry through tsx pinned to this checkout's TypeScript paths.
|
|
463
|
+
* @param moduleUrl - the provider module's `import.meta.url`.
|
|
464
|
+
* @returns `[node, ...loader, entry]`.
|
|
465
|
+
*/
|
|
466
|
+
function shimInvocation(moduleUrl) {
|
|
467
|
+
const built = fileURLToPath(new URL("./exec-shim.js", moduleUrl));
|
|
468
|
+
if (existsSync(built)) return [process.execPath, built];
|
|
469
|
+
const source = fileURLToPath(new URL("./exec-shim.ts", moduleUrl));
|
|
470
|
+
const tsconfig = fileURLToPath(new URL("../../../../tsconfig.base.json", moduleUrl));
|
|
471
|
+
const registration = `import { register } from ${JSON.stringify(import.meta.resolve("tsx/esm/api"))}; register({ tsconfig: ${JSON.stringify(tsconfig)} });`;
|
|
472
|
+
return [
|
|
473
|
+
process.execPath,
|
|
474
|
+
"--import",
|
|
475
|
+
`data:text/javascript,${encodeURIComponent(registration)}`,
|
|
476
|
+
source
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
/** Whether `pid` names a live process; a permission refusal still proves existence. */
|
|
480
|
+
function processAlive(pid) {
|
|
481
|
+
try {
|
|
482
|
+
process.kill(pid, 0);
|
|
483
|
+
return true;
|
|
484
|
+
} catch (error) {
|
|
485
|
+
return error.code === "EPERM";
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/** The message of a rejection from the pool or runtime, which reject only with `Error`. */
|
|
489
|
+
function messageOf(error) {
|
|
490
|
+
return error.message;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Container-backed process-sandbox provider (`ctx.sandbox`). Owns one
|
|
494
|
+
* container per confined mode and workspace and deletes them when disposed;
|
|
495
|
+
* containers left by dead DSH processes are deleted at load.
|
|
496
|
+
*/
|
|
497
|
+
var AppleContainerSandboxProvider = class extends LocalSandboxProvider {
|
|
498
|
+
static Config = z.intersect([LocalSandboxProvider.Config, z.object({
|
|
499
|
+
backend: z.union([
|
|
500
|
+
"auto",
|
|
501
|
+
"container",
|
|
502
|
+
"local"
|
|
503
|
+
]).default("auto"),
|
|
504
|
+
executable: z.string().default("container"),
|
|
505
|
+
image: z.string().default("node:22-bookworm"),
|
|
506
|
+
cpus: z.natural(),
|
|
507
|
+
memory: z.string(),
|
|
508
|
+
autoStart: z.boolean().default(true),
|
|
509
|
+
recheckMs: z.natural().default(1e4),
|
|
510
|
+
envDenylist: z.array(z.string()).default([...DEFAULT_ENV_DENYLIST]),
|
|
511
|
+
readOnlyMounts: z.array(z.string()).default([]),
|
|
512
|
+
protectedPaths: z.array(z.string()).default([".git"]),
|
|
513
|
+
hiddenFiles: z.array(z.string()).default([...DEFAULT_HIDDEN_FILES]),
|
|
514
|
+
hiddenFilesMaxDepth: z.natural().default(8),
|
|
515
|
+
hiddenFilesSkipDirs: z.array(z.string()).default(["node_modules", ".git"])
|
|
516
|
+
})]);
|
|
517
|
+
static inject = ["sessions", "sessionProjections"];
|
|
518
|
+
/** The backend for sessions without a recorded choice, after `auto` resolution. */
|
|
519
|
+
defaultBackend;
|
|
520
|
+
/** Whether this host can run the container backend. */
|
|
521
|
+
containerSupported;
|
|
522
|
+
executable;
|
|
523
|
+
image;
|
|
524
|
+
envDenylist;
|
|
525
|
+
readOnlyMounts;
|
|
526
|
+
hiddenFiles;
|
|
527
|
+
runtime;
|
|
528
|
+
pool;
|
|
529
|
+
constructor(ctx, config) {
|
|
530
|
+
super(ctx, config);
|
|
531
|
+
this.executable = config.executable;
|
|
532
|
+
const host = {
|
|
533
|
+
platform: process.platform,
|
|
534
|
+
arch: process.arch,
|
|
535
|
+
path: process.env.PATH
|
|
536
|
+
};
|
|
537
|
+
this.containerSupported = containerSupported(this.executable, host);
|
|
538
|
+
this.defaultBackend = resolveBackend(config.backend, this.containerSupported);
|
|
539
|
+
this.image = config.image;
|
|
540
|
+
this.envDenylist = config.envDenylist;
|
|
541
|
+
if (this.envDenylist.some((entry) => entry === "" || entry.includes(","))) throw new Error("sandbox-apple-container: envDenylist entries must be non-empty and contain no comma");
|
|
542
|
+
if (config.cpus === 0) throw new Error("sandbox-apple-container: cpus must be positive");
|
|
543
|
+
this.readOnlyMounts = config.readOnlyMounts;
|
|
544
|
+
if (this.readOnlyMounts.some((path) => !isAbsolute(path) || path.includes(","))) throw new Error("sandbox-apple-container: readOnlyMounts entries must be absolute paths without a comma");
|
|
545
|
+
const protectedPaths = config.protectedPaths;
|
|
546
|
+
if (protectedPaths.some((path) => path === "" || isAbsolute(path) || path.includes(",") || path.split(/[\\/]/u).includes(".."))) throw new Error("sandbox-apple-container: protectedPaths entries must be workspace-relative paths without `..` or a comma");
|
|
547
|
+
this.hiddenFiles = config.hiddenFiles;
|
|
548
|
+
this.runtime = new ContainerRuntime({
|
|
549
|
+
executable: this.executable,
|
|
550
|
+
image: this.image,
|
|
551
|
+
autoStart: config.autoStart,
|
|
552
|
+
...config.cpus === void 0 ? {} : { cpus: config.cpus },
|
|
553
|
+
...config.memory === void 0 ? {} : { memory: config.memory }
|
|
554
|
+
});
|
|
555
|
+
this.pool = new ContainerPool(this.runtime, {
|
|
556
|
+
pid: process.pid,
|
|
557
|
+
recheckMs: config.recheckMs,
|
|
558
|
+
now: Date.now,
|
|
559
|
+
protectedPaths,
|
|
560
|
+
readOnlyMounts: this.readOnlyMounts,
|
|
561
|
+
hidden: {
|
|
562
|
+
names: this.hiddenFiles,
|
|
563
|
+
maxDepth: config.hiddenFilesMaxDepth,
|
|
564
|
+
skipDirs: config.hiddenFilesSkipDirs
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
ctx.sessionProjections.register({
|
|
568
|
+
key: "sandboxBackend",
|
|
569
|
+
stateVersion: 1,
|
|
570
|
+
stateSchema: sandboxBackendStateSchema,
|
|
571
|
+
init: () => null,
|
|
572
|
+
apply: (state, event) => event.type === "sandbox/backend" ? event.data.backend : state
|
|
573
|
+
});
|
|
574
|
+
ctx.effect(() => () => this.pool.dispose());
|
|
575
|
+
if (this.defaultBackend === "container") this.pool.sweep(processAlive).catch((error) => {
|
|
576
|
+
ctx.logger.warn(`sandbox-apple-container: removing stale containers failed: ${messageOf(error)}`);
|
|
577
|
+
});
|
|
578
|
+
ctx.inject(["sandboxPolicy"], (policyCtx) => {
|
|
579
|
+
const mode = policyCtx.sandboxPolicy.defaultMode;
|
|
580
|
+
if (this.defaultBackend !== "container" || mode === "danger-full-access") return;
|
|
581
|
+
this.pool.ensure({
|
|
582
|
+
mode,
|
|
583
|
+
workspaceRoot: policyCtx.sandboxPolicy.workspaceRoot
|
|
584
|
+
}).catch((error) => {
|
|
585
|
+
ctx.logger.warn(`sandbox-apple-container: starting the ${mode} container failed: ${messageOf(error)}`);
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
ctx.inject(["systemPrompt", "sandboxPolicy"], (scope) => {
|
|
589
|
+
scope.systemPrompt.context({
|
|
590
|
+
name: "sandbox:backend",
|
|
591
|
+
order: scope.systemPrompt.getContextOrder("SANDBOX_BACKEND"),
|
|
592
|
+
text: (context) => {
|
|
593
|
+
const session = context.agent?.session;
|
|
594
|
+
if (session === void 0 || this.backendFor(session) !== "container") return "";
|
|
595
|
+
const policy = scope.sandboxPolicy.resolve({ session });
|
|
596
|
+
if (policy.mode === "danger-full-access") return "";
|
|
597
|
+
return `Commands confined by the DSH file sandbox run inside a Linux container from image ${JSON.stringify(this.image)}. Only the session workspace ${JSON.stringify(policy.workspaceRoot)} is shared with the host; files written elsewhere stay inside the container and are not visible to file tools. File tools cannot read host paths outside the workspace, and secret files such as \`.env\` and private keys are hidden from commands and file tools.`;
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
});
|
|
601
|
+
ctx.inject(["commands"], (commandCtx) => {
|
|
602
|
+
commandCtx.commands.register({
|
|
603
|
+
definitionId: CommandDefinitionId("@deepseek-ai/dsh-sandbox-apple-container"),
|
|
604
|
+
name: "sandbox",
|
|
605
|
+
description: "Show or switch where confined commands run (/sandbox container, /sandbox local)",
|
|
606
|
+
input: { hint: "<container|local>" },
|
|
607
|
+
handler: async ({ agent, rawInput }) => {
|
|
608
|
+
const backend = rawInput.trim();
|
|
609
|
+
if (backend === "") return {
|
|
610
|
+
kind: "success",
|
|
611
|
+
text: await this.describe(agent.session)
|
|
612
|
+
};
|
|
613
|
+
if (!isSandboxBackend(backend)) return {
|
|
614
|
+
kind: "error",
|
|
615
|
+
text: `unknown backend "${backend}" (available: container, local)`
|
|
616
|
+
};
|
|
617
|
+
if (backend === "container" && !this.containerSupported) return {
|
|
618
|
+
kind: "error",
|
|
619
|
+
text: `Apple container is unavailable on this host: it needs macOS on Apple silicon with ${JSON.stringify(this.executable)} installed`
|
|
620
|
+
};
|
|
621
|
+
setSandboxBackend(agent.session, backend);
|
|
622
|
+
return {
|
|
623
|
+
kind: "success",
|
|
624
|
+
text: backend === "container" ? `backend container (image ${this.image})` : "backend local"
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Wrap `argv` for `policy` on its session's backend: the inherited local
|
|
632
|
+
* chain for `local`, or the exec shim against this workspace and mode's
|
|
633
|
+
* container for `container`.
|
|
634
|
+
* @param argv - the exact argv the caller is about to spawn.
|
|
635
|
+
* @param policy - the file-effect policy this execution runs under.
|
|
636
|
+
* @param signal - cancellation before the container is ready.
|
|
637
|
+
* @returns the confined argv with full enforcement, the read-only-mount
|
|
638
|
+
* denial dialect, and the shim's runner-failure rule; throws
|
|
639
|
+
* `SANDBOX_UNAVAILABLE` when the container cannot be started.
|
|
640
|
+
*/
|
|
641
|
+
async confine(argv, policy, signal) {
|
|
642
|
+
if (this.policyBackend(policy) === "local") return super.confine(argv, policy, signal);
|
|
643
|
+
signal?.throwIfAborted();
|
|
644
|
+
let container;
|
|
645
|
+
try {
|
|
646
|
+
container = await this.pool.ensure(policy);
|
|
647
|
+
} catch (error) {
|
|
648
|
+
throw new SandboxUnavailableError(policy.mode, `${messageOf(error)}; run /sandbox local to confine commands on this host instead`);
|
|
649
|
+
}
|
|
650
|
+
signal?.throwIfAborted();
|
|
651
|
+
return {
|
|
652
|
+
argv: [...shimInvocation(import.meta.url), ...encodeShimArgs({
|
|
653
|
+
executable: this.executable,
|
|
654
|
+
container,
|
|
655
|
+
denylist: this.envDenylist,
|
|
656
|
+
argv
|
|
657
|
+
})],
|
|
658
|
+
enforcement: "full",
|
|
659
|
+
denialSignatures: CONTAINER_DENIAL_SIGNATURES,
|
|
660
|
+
runnerFailureRules: CONTAINER_RUNNER_FAILURE_RULES
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* The host read scope of a container-backed confined policy: the workspace
|
|
665
|
+
* (canonical and as spelled) and the extra read-only mounts, minus the
|
|
666
|
+
* hidden files. The local backend and `danger-full-access` confine no reads.
|
|
667
|
+
* @param policy - the calling session's resolved policy.
|
|
668
|
+
* @returns the scope, or `undefined` when reads are unconfined.
|
|
669
|
+
*/
|
|
670
|
+
readScope(policy) {
|
|
671
|
+
if (policy.mode === "danger-full-access" || this.policyBackend(policy) === "local") return void 0;
|
|
672
|
+
return {
|
|
673
|
+
roots: [...new Set([
|
|
674
|
+
policy.workspaceRoot,
|
|
675
|
+
canonicalPath(policy.workspaceRoot),
|
|
676
|
+
...this.readOnlyMounts
|
|
677
|
+
])],
|
|
678
|
+
hiddenNames: this.hiddenFiles
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
/** The backend of the session a policy names; agentless and unknown sessions use the default. */
|
|
682
|
+
policyBackend(policy) {
|
|
683
|
+
return this.backendFor(policy.sessionId === void 0 ? void 0 : this.ctx.sessions.get(policy.sessionId));
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* The backend a session's confined calls use.
|
|
687
|
+
* @param session - the calling session, or undefined for agentless calls.
|
|
688
|
+
* @returns the session's last recorded choice, else the configured default.
|
|
689
|
+
*/
|
|
690
|
+
backendFor(session) {
|
|
691
|
+
return (session === void 0 ? void 0 : this.ctx.sessionProjections.stateOf(session, "sandboxBackend")) ?? this.defaultBackend;
|
|
692
|
+
}
|
|
693
|
+
/** The `/sandbox` status line for `session`. */
|
|
694
|
+
async describe(session) {
|
|
695
|
+
if (this.backendFor(session) === "local") return "backend local: confined commands run on this host under the local sandbox (/sandbox container switches)";
|
|
696
|
+
const containers = await Promise.all(this.pool.entries().map(async (entry) => {
|
|
697
|
+
const state = await this.runtime.state(entry.name).catch((error) => `unknown (${messageOf(error)})`);
|
|
698
|
+
return `${entry.mode} ${entry.name} ${state} at ${entry.root}`;
|
|
699
|
+
}));
|
|
700
|
+
return `backend container: image ${this.image}; ${containers.length === 0 ? "no container started yet" : containers.join(", ")} (/sandbox local switches)`;
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
//#endregion
|
|
704
|
+
export { AppleContainerSandboxProvider, AppleContainerSandboxProvider as default, DEFAULT_ENV_DENYLIST, DEFAULT_HIDDEN_FILES, SANDBOX_BACKENDS, containerSupported, executableResolves, isSandboxBackend, resolveBackend, setSandboxBackend, shimInvocation };
|