pi-web-ui 0.113.0 → 0.114.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +69 -1
- package/README.md +117 -252
- package/README.zh-CN.md +145 -223
- package/README.zh-TW.md +145 -223
- package/bin/device-hub-cli.mjs +577 -0
- package/bin/pi-device-hub.mjs +9 -0
- package/bin/pi-web-ui.mjs +145 -20
- package/dist/gh-comment-781.md +5 -0
- package/dist/server/agent-service-daemon.js +102 -24
- package/dist/server/agent-service.js +2296 -1121
- package/dist/server/approval-rules.js +29 -3
- package/dist/server/ask-user-question-tool.js +8 -8
- package/dist/server/atomic-file.js +35 -14
- package/dist/server/attachment-store.js +71 -35
- package/dist/server/attachments.js +18 -8
- package/dist/server/canvas/canvas-agent-tool.js +65 -15
- package/dist/server/canvas/canvas-dispatch.js +61 -20
- package/dist/server/canvas/canvas-orchestrator.js +516 -199
- package/dist/server/canvas/canvas-scheduler.js +99 -0
- package/dist/server/canvas/canvas-store.js +107 -54
- package/dist/server/canvas/canvas-validation.js +131 -0
- package/dist/server/canvas/canvas-workspace.js +284 -0
- package/dist/server/client-id-utils.js +8 -0
- package/dist/server/client-state.js +104 -22
- package/dist/server/communicate-tool.js +76 -0
- package/dist/server/compact-context-tool.js +2 -3
- package/dist/server/compaction-instructions.js +75 -0
- package/dist/server/compaction-markers.js +7 -2
- package/dist/server/compaction-state.js +84 -0
- package/dist/server/composer-drafts.js +43 -40
- package/dist/server/context-evidence.js +346 -0
- package/dist/server/control-socket.js +6 -2
- package/dist/server/conversation-communication.js +368 -0
- package/dist/server/conversation-read-tool.js +3 -4
- package/dist/server/conversation-tool-names.js +14 -0
- package/dist/server/conversation-tool.js +57 -0
- package/dist/server/delegate-mode.js +17 -4
- package/dist/server/device-center/bot-catalog.js +88 -0
- package/dist/server/device-center/bot-execution.js +106 -0
- package/dist/server/device-center/bots.js +170 -0
- package/dist/server/device-center/collab-service.js +153 -0
- package/dist/server/device-center/collab-tool.js +184 -0
- package/dist/server/device-center/connector.js +332 -0
- package/dist/server/device-center/desktop-linux.js +372 -0
- package/dist/server/device-center/desktop-native.js +413 -0
- package/dist/server/device-center/desktop-page.js +232 -0
- package/dist/server/device-center/desktop-setup.js +30 -0
- package/dist/server/device-center/desktop.js +387 -0
- package/dist/server/device-center/device-portals.js +241 -0
- package/dist/server/device-center/execution-failure.js +70 -0
- package/dist/server/device-center/executor.js +371 -0
- package/dist/server/device-center/file-sync.js +493 -0
- package/dist/server/device-center/hub-auth.js +69 -0
- package/dist/server/device-center/hub-page.js +68 -0
- package/dist/server/device-center/hub-setup.js +214 -0
- package/dist/server/device-center/hub.js +829 -0
- package/dist/server/device-center/local-page.js +17 -0
- package/dist/server/device-center/local.js +476 -0
- package/dist/server/device-center/main.js +118 -0
- package/dist/server/device-center/native-desktop/macos.js +192 -0
- package/dist/server/device-center/native-desktop/windows.js +118 -0
- package/dist/server/device-center/path-portal.js +120 -0
- package/dist/server/device-center/relay.js +959 -0
- package/dist/server/device-center/runtime.js +253 -0
- package/dist/server/device-center/smoke-preview.js +3 -0
- package/dist/server/device-center/store.js +122 -0
- package/dist/server/device-center/task-error.js +14 -0
- package/dist/server/device-center/transfers.js +293 -0
- package/dist/server/device-center/workspaces.js +45 -0
- package/dist/server/dispatch-domain-handlers.js +83 -27
- package/dist/server/dsh/dsh-agent-service.js +277 -158
- package/dist/server/dsh/dsh-client.js +12 -2
- package/dist/server/durable/durable-agent-service.js +1 -1
- package/dist/server/evidence-tool-hooks.js +49 -0
- package/dist/server/file-save.js +95 -0
- package/dist/server/file-transfer-routes.js +97 -47
- package/dist/server/files-service.js +110 -34
- package/dist/server/goal-review-gate.js +16 -6
- package/dist/server/goal-service.js +54 -30
- package/dist/server/http-proxy.js +120 -0
- package/dist/server/index.js +236 -232
- package/dist/server/load-timing.js +31 -0
- package/dist/server/mcp-bridge.js +219 -94
- package/dist/server/mcp-hot-reload.js +31 -5
- package/dist/server/mcp-manager.js +192 -93
- package/dist/server/mcp-oauth-login.js +169 -0
- package/dist/server/mcp-oauth-ui.js +137 -0
- package/dist/server/mcp-oauth.js +395 -0
- package/dist/server/mcp-project-runtime.js +207 -0
- package/dist/server/message-window.js +74 -21
- package/dist/server/net-guard.js +122 -20
- package/dist/server/payload-limits.js +17 -0
- package/dist/server/pending-question.js +82 -0
- package/dist/server/plan-mode.js +338 -137
- package/dist/server/plugin-assets.js +8 -4
- package/dist/server/plugin-chat-attachments.js +48 -0
- package/dist/server/plugin-facilities.js +67 -220
- package/dist/server/plugin-installer.js +2 -1
- package/dist/server/plugin-removal.js +49 -0
- package/dist/server/plugin-secrets.js +179 -0
- package/dist/server/plugins.js +262 -92
- package/dist/server/preset-fields.js +49 -0
- package/dist/server/preset-plugin-dependencies.js +57 -0
- package/dist/server/preset-share.js +112 -72
- package/dist/server/protocol-version.js +1 -1
- package/dist/server/queue-modes.js +10 -0
- package/dist/server/remote-ssh-service.js +160 -67
- package/dist/server/request-context.js +164 -0
- package/dist/server/sandbox-data.js +655 -0
- package/dist/server/sandbox-engines.js +7 -0
- package/dist/server/sandbox-runtime-gate.js +41 -0
- package/dist/server/sandbox-service.js +139 -698
- package/dist/server/sandbox-tool.js +21 -4
- package/dist/server/scheduler-tasks.js +63 -48
- package/dist/server/serialize.js +48 -19
- package/dist/server/session-branch.js +25 -3
- package/dist/server/session-parser.js +28 -18
- package/dist/server/session-scan.js +130 -0
- package/dist/server/session-search.js +51 -37
- package/dist/server/settings-service.js +267 -111
- package/dist/server/setup-config.js +58 -0
- package/dist/server/skill-maintenance.js +112 -24
- package/dist/server/subagent-helpers.js +4 -1
- package/dist/server/subagent-notify.js +4 -1
- package/dist/server/subagent-security-gate.js +52 -11
- package/dist/server/subagent-task.js +27 -0
- package/dist/server/subagents.js +217 -46
- package/dist/server/tabs.js +9 -1
- package/dist/server/terminals.js +467 -363
- package/dist/server/tool-guards.js +42 -6
- package/dist/server/tool-manager.js +47 -20
- package/dist/server/tool-overrides.js +13 -11
- package/dist/server/tool-prompt-overrides.js +32 -2
- package/dist/server/tool-prompts.js +1 -1
- package/dist/server/turn-timing.js +11 -0
- package/dist/server/universal/session-store.js +48 -6
- package/dist/server/universal/universal-service.js +1 -1
- package/dist/server/universal/universal-session.js +334 -231
- package/dist/server/uploads.js +64 -5
- package/dist/server/webui-context.js +6 -6
- package/dist/server/worktree-service.js +32 -24
- package/dist/server/ws-pending-queue.js +73 -27
- package/dist/test-artifact.md +9 -0
- package/dist/test-preview-modal.md +11 -0
- package/dist/test-report.html +20 -0
- package/docs/device-center.md +142 -0
- package/docs/device-desktop.md +48 -0
- package/package.json +4 -1
- package/plugin-sdk/index.d.ts +45 -9
- package/plugin-sdk/ssh-host-trust.mjs +225 -0
- package/plugins/catalog.json +1 -1
- package/themes/aetheris.css +1 -107
- package/themes/ayu-light.css +5 -29
- package/themes/bases/flow-base.css +0 -6
- package/themes/bases/statusbar-flat-base.css +112 -0
- package/themes/bases/zhujian-base.css +1 -0
- package/themes/bases/zhupi-base.css +1 -0
- package/themes/catppuccin-latte.css +4 -27
- package/themes/catppuccin.css +4 -35
- package/themes/claude-code-dark.css +1 -0
- package/themes/codex.css +4 -28
- package/themes/cyberpunk.css +3 -61
- package/themes/dark-teal.css +0 -1
- package/themes/dazzle.css +3 -73
- package/themes/everforest-light.css +4 -27
- package/themes/geist.css +4 -28
- package/themes/gruvbox-light.css +4 -27
- package/themes/kanagawa-lotus.css +4 -27
- package/themes/md-preview.css +3 -213
- package/themes/mist.css +4 -24
- package/themes/nord.css +4 -35
- package/themes/one-dark.css +4 -37
- package/themes/paper.css +4 -16
- package/themes/rose-pine-dawn.css +4 -28
- package/themes/sakura.css +4 -24
- package/themes/solarized-light.css +4 -27
- package/themes/tokyo-night.css +3 -34
- package/themes/white.css +4 -26
- package/themes/zhujian-dark.css +1 -0
- package/themes/zhupi-dark.css +1 -0
- package/web/dist/.build-assets.json +1 -0
- package/web/dist/assets/BgTasksModal-CS7UNzcw.js +1 -0
- package/web/dist/assets/BgTasksModal-Dk-tp78W.js +1 -0
- package/web/dist/assets/CanvasStudio-BEJZE5BI.js +1579 -0
- package/web/dist/assets/CanvasStudio-CgUNHqos.js +1579 -0
- package/web/dist/assets/DeviceCenter-C8_CbxQ3.js +3 -0
- package/web/dist/assets/DeviceCenter-CVblvhNX.js +3 -0
- package/web/dist/assets/Dropdown-Oug0DDte.js +1 -0
- package/web/dist/assets/FilePreview-CvdX6nr6.js +2 -0
- package/web/dist/assets/FilePreview-kCZhfBAF.js +2 -0
- package/web/dist/assets/GlobalSearchModal-BLH63yYP.js +1 -0
- package/web/dist/assets/GlobalSearchModal-DAkvlvp6.js +1 -0
- package/web/dist/assets/GroupConversation-Cgo4xbpa.js +1 -0
- package/web/dist/assets/GroupConversation-DZ71jEi8.js +1 -0
- package/web/dist/assets/ModelConfigModal-BpWdFdQC.js +2 -0
- package/web/dist/assets/PiSetupModal-mkWPSxMj.js +1 -0
- package/web/dist/assets/ProviderOAuthControls-VK_jqXYS.js +1 -0
- package/web/dist/assets/SCMPanel-BP1TUT-V.js +2 -0
- package/web/dist/assets/SCMPanel-BscPm9kf.js +2 -0
- package/web/dist/assets/SettingsModal-C8UgMjVM.js +20 -0
- package/web/dist/assets/SettingsModal-VynB7jNb.js +20 -0
- package/web/dist/assets/TerminalPanel-D6cPRFei.js +3 -0
- package/web/dist/assets/TerminalPanel-DL20uKgU.js +3 -0
- package/web/dist/assets/TuiOverlayModal-Dst3BmT8.js +1 -0
- package/web/dist/assets/TuiOverlayModal-NCkpNtTp.js +1 -0
- package/web/dist/assets/UniversalPathPicker-CfQFc_c3.js +1 -0
- package/web/dist/assets/UniversalPathPicker-DwxwNcFw.js +1 -0
- package/web/dist/assets/app-globals-CnO6KlEM.js +325 -0
- package/web/dist/assets/desktop-DPyTy4M3.js +1 -0
- package/web/dist/assets/es-DWMIuDU2.js +2 -0
- package/web/dist/assets/index-CPgAH5q7.js +83 -0
- package/web/dist/assets/index-Cje8iugL.js +83 -0
- package/web/dist/assets/index-DnKeiDbj.css +1 -0
- package/web/dist/assets/markdown-BggnuQ4H.js +40 -0
- package/web/dist/assets/markdown-C8LL_u4z.css +1 -0
- package/web/dist/assets/math-1uRJCsA9.js +257 -0
- package/web/dist/assets/math-BorAY7qD.css +1 -0
- package/web/dist/assets/plugin-host-CqA4yW6w.js +1 -0
- package/web/dist/assets/shortcut-stack-hhrDyRBn.js +1 -0
- package/web/dist/assets/theme-BqdAjEjF.js +1 -0
- package/web/dist/assets/uuid-BzDIGCwP.js +1 -0
- package/web/dist/index.html +15 -9
- package/web/dist/sw.js +1 -1
- package/web/dist/assets/TerminalPanel-3wojLfLk.js +0 -3
- package/web/dist/assets/index-A8YXwnpZ.js +0 -2008
- package/web/dist/assets/index-BwL7qBh6.css +0 -1
- package/web/dist/assets/markdown-BJZlfvdP.js +0 -296
- package/web/dist/assets/markdown-DSNUiDzp.css +0 -1
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, lstatSync } from "node:fs";
|
|
2
|
+
import { copyFile, chmod, lstat, mkdir, readdir, readFile, rename, rm, rmdir } from "node:fs/promises";
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
+
import { dirname, join, resolve, basename, parse } from "node:path";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { writeAtomicSync } from "./atomic-file.js";
|
|
7
|
+
import { SANDBOX_IGNORED_DIRS, isSameOrSubpath } from "./sandbox-service.js";
|
|
8
|
+
const repositories = new Map();
|
|
9
|
+
const wire = (p) => resolve(p).replace(/\\/g, "/");
|
|
10
|
+
const hash = (v) => createHash("sha256").update(v).digest("hex");
|
|
11
|
+
const ignored = (n) => n === ".git" ||
|
|
12
|
+
SANDBOX_IGNORED_DIRS.has(n) ||
|
|
13
|
+
[".DS_Store", "Thumbs.db", "desktop.ini"].includes(n) ||
|
|
14
|
+
n.startsWith(".pi-tmp");
|
|
15
|
+
function atomic(path, value) {
|
|
16
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
17
|
+
safeRoot(dirname(path));
|
|
18
|
+
if (existsSync(path) && lstatSync(path).isSymbolicLink())
|
|
19
|
+
throw new Error(`Linked metadata refused: ${path}`);
|
|
20
|
+
writeAtomicSync(path, JSON.stringify(value), { fsync: true });
|
|
21
|
+
}
|
|
22
|
+
function safeRoot(path) {
|
|
23
|
+
const abs = resolve(path);
|
|
24
|
+
let cur = abs;
|
|
25
|
+
while (true) {
|
|
26
|
+
if (existsSync(cur) && lstatSync(cur).isSymbolicLink())
|
|
27
|
+
throw new Error(`Linked directory refused: ${cur}`);
|
|
28
|
+
const parent = dirname(cur);
|
|
29
|
+
if (parent === cur)
|
|
30
|
+
break;
|
|
31
|
+
cur = parent;
|
|
32
|
+
}
|
|
33
|
+
if (!existsSync(abs) || !lstatSync(abs).isDirectory())
|
|
34
|
+
throw new Error(`Directory unavailable: ${abs}`);
|
|
35
|
+
return realpathSync(abs);
|
|
36
|
+
}
|
|
37
|
+
async function createSafeDirectories(path) {
|
|
38
|
+
const missing = [];
|
|
39
|
+
let current = resolve(path);
|
|
40
|
+
while (!existsSync(current)) {
|
|
41
|
+
missing.push(current);
|
|
42
|
+
const parent = dirname(current);
|
|
43
|
+
if (parent === current)
|
|
44
|
+
throw new Error(`Directory unavailable: ${path}`);
|
|
45
|
+
current = parent;
|
|
46
|
+
}
|
|
47
|
+
safeRoot(current);
|
|
48
|
+
for (const dir of missing.reverse()) {
|
|
49
|
+
safeRoot(dirname(dir));
|
|
50
|
+
await mkdir(dir);
|
|
51
|
+
safeRoot(dir);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function protectedRoot(path) {
|
|
55
|
+
const real = safeRoot(path);
|
|
56
|
+
if (real === parse(real).root || wire(real) === wire(homedir()))
|
|
57
|
+
throw new Error("System or home root refused");
|
|
58
|
+
return real;
|
|
59
|
+
}
|
|
60
|
+
async function tree(root) {
|
|
61
|
+
safeRoot(root);
|
|
62
|
+
const out = Object.create(null);
|
|
63
|
+
let bytes = 0;
|
|
64
|
+
let count = 0;
|
|
65
|
+
async function walk(dir, prefix) {
|
|
66
|
+
for (const ent of await readdir(dir, { withFileTypes: true })) {
|
|
67
|
+
if (ignored(ent.name))
|
|
68
|
+
continue;
|
|
69
|
+
const rel = prefix ? `${prefix}/${ent.name}` : ent.name;
|
|
70
|
+
const path = join(root, rel);
|
|
71
|
+
const st = await lstat(path);
|
|
72
|
+
if (++count > 15000)
|
|
73
|
+
throw new Error("Sandbox file budget exceeded");
|
|
74
|
+
if (st.isSymbolicLink()) {
|
|
75
|
+
out[rel] = "link";
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (st.isDirectory()) {
|
|
79
|
+
out[rel] = "dir";
|
|
80
|
+
await walk(path, rel);
|
|
81
|
+
}
|
|
82
|
+
else if (st.isFile()) {
|
|
83
|
+
bytes += st.size;
|
|
84
|
+
if (st.size > 64 * 1024 * 1024 || bytes > 512 * 1024 * 1024)
|
|
85
|
+
throw new Error("Sandbox byte budget exceeded");
|
|
86
|
+
out[rel] = `file:${st.mode & 0o777}:${hash(await readFile(path))}`;
|
|
87
|
+
}
|
|
88
|
+
else
|
|
89
|
+
throw new Error(`Unsupported file: ${rel}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
await walk(root, "");
|
|
93
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => a.localeCompare(b)));
|
|
94
|
+
}
|
|
95
|
+
async function fingerprint(path) {
|
|
96
|
+
try {
|
|
97
|
+
const st = await lstat(path);
|
|
98
|
+
if (st.isSymbolicLink())
|
|
99
|
+
return "link";
|
|
100
|
+
if (st.isDirectory())
|
|
101
|
+
return "dir";
|
|
102
|
+
if (st.isFile())
|
|
103
|
+
return `file:${st.mode & 0o777}:${hash(await readFile(path))}`;
|
|
104
|
+
return "unsupported";
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
if (error.code === "ENOENT")
|
|
108
|
+
return undefined;
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function copy(root, dest) {
|
|
113
|
+
const realSource = safeRoot(root), realDest = safeRoot(dest);
|
|
114
|
+
if (isSameOrSubpath(realDest, realSource) || isSameOrSubpath(realSource, realDest))
|
|
115
|
+
throw new Error("Source and destination trees overlap");
|
|
116
|
+
const before = await tree(root);
|
|
117
|
+
let copiedFiles = 0;
|
|
118
|
+
let skippedFiles = 0;
|
|
119
|
+
for (const [rel, value] of Object.entries(before)) {
|
|
120
|
+
if (value === "link") {
|
|
121
|
+
skippedFiles++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (value === "dir")
|
|
125
|
+
await mkdir(join(dest, rel), { recursive: true });
|
|
126
|
+
else {
|
|
127
|
+
await mkdir(dirname(join(dest, rel)), { recursive: true });
|
|
128
|
+
await copyFile(join(root, rel), join(dest, rel));
|
|
129
|
+
await chmod(join(dest, rel), (await lstat(join(root, rel))).mode & 0o777);
|
|
130
|
+
if ((await fingerprint(join(dest, rel))) !== value)
|
|
131
|
+
throw new Error(`Copied content changed: ${rel}`);
|
|
132
|
+
copiedFiles++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (hash(JSON.stringify(before)) !== hash(JSON.stringify(await tree(root))))
|
|
136
|
+
throw new Error("Source changed while copying");
|
|
137
|
+
return { copiedFiles, skippedFiles };
|
|
138
|
+
}
|
|
139
|
+
export class SandboxManager {
|
|
140
|
+
sandboxesRoot;
|
|
141
|
+
registryPath;
|
|
142
|
+
repo;
|
|
143
|
+
constructor(dataDir) {
|
|
144
|
+
this.sandboxesRoot = wire(join(dataDir, "sandboxes"));
|
|
145
|
+
this.registryPath = join(this.sandboxesRoot, "registry.json");
|
|
146
|
+
const key = process.platform === "win32" ? this.sandboxesRoot.toLowerCase() : this.sandboxesRoot;
|
|
147
|
+
let repo = repositories.get(key);
|
|
148
|
+
if (!repo) {
|
|
149
|
+
repo = { items: new Map(), queues: new Map(), busy: new Set(), leases: new Map() };
|
|
150
|
+
if (existsSync(this.registryPath)) {
|
|
151
|
+
const raw = JSON.parse(readFileSync(this.registryPath, "utf8"));
|
|
152
|
+
if (raw.version !== 1 || !Array.isArray(raw.sandboxes))
|
|
153
|
+
throw new Error("Invalid sandbox registry");
|
|
154
|
+
for (const info of raw.sandboxes) {
|
|
155
|
+
if (!info ||
|
|
156
|
+
typeof info.id !== "string" ||
|
|
157
|
+
!/^[a-zA-Z0-9_-]+$/.test(info.id) ||
|
|
158
|
+
wire(info.path) !== wire(join(this.sandboxesRoot, info.id)))
|
|
159
|
+
throw new Error("Invalid managed sandbox identity");
|
|
160
|
+
if (existsSync(info.path))
|
|
161
|
+
safeRoot(info.path);
|
|
162
|
+
repo.items.set(info.id, Object.freeze({ ...info }));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
repositories.set(key, repo);
|
|
166
|
+
}
|
|
167
|
+
this.repo = repo;
|
|
168
|
+
}
|
|
169
|
+
commit(items) {
|
|
170
|
+
atomic(this.registryPath, { version: 1, sandboxes: [...items.values()] });
|
|
171
|
+
this.repo.items = items;
|
|
172
|
+
}
|
|
173
|
+
baselinePath(info) {
|
|
174
|
+
return join(this.sandboxesRoot, ".baselines", `${info.id}.json`);
|
|
175
|
+
}
|
|
176
|
+
baseline(info) {
|
|
177
|
+
const path = this.baselinePath(info);
|
|
178
|
+
if (!existsSync(path))
|
|
179
|
+
throw new Error("Sandbox baseline missing; rebuild sandbox before applying changes");
|
|
180
|
+
safeRoot(dirname(path));
|
|
181
|
+
if (lstatSync(path).isSymbolicLink() || !lstatSync(path).isFile())
|
|
182
|
+
throw new Error("Invalid sandbox baseline file");
|
|
183
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
184
|
+
if (raw.version !== 1 || !raw.files || typeof raw.files !== "object")
|
|
185
|
+
throw new Error("Invalid sandbox baseline; rebuild sandbox");
|
|
186
|
+
for (const [rel, value] of Object.entries(raw.files)) {
|
|
187
|
+
if (!rel ||
|
|
188
|
+
rel.includes("\\") ||
|
|
189
|
+
rel.startsWith("/") ||
|
|
190
|
+
rel.split("/").some((p) => p === ".." || p === "." || !p || ignored(p)) ||
|
|
191
|
+
!isSameOrSubpath(join(info.path, rel), info.path) ||
|
|
192
|
+
!/^(dir|link|file:[0-9]+:[a-f0-9]{64})$/.test(value))
|
|
193
|
+
throw new Error("Invalid sandbox baseline path or value");
|
|
194
|
+
}
|
|
195
|
+
return Object.assign(Object.create(null), raw.files);
|
|
196
|
+
}
|
|
197
|
+
list() {
|
|
198
|
+
return [...this.repo.items.values()].map((i) => ({ ...i })).sort((a, b) => b.createdAt - a.createdAt);
|
|
199
|
+
}
|
|
200
|
+
get(target) {
|
|
201
|
+
return this.list().find((i) => i.id === target || wire(i.path) === wire(target));
|
|
202
|
+
}
|
|
203
|
+
isManagedRoot(cwd) {
|
|
204
|
+
return isSameOrSubpath(cwd, this.sandboxesRoot);
|
|
205
|
+
}
|
|
206
|
+
getByCwd(cwd) {
|
|
207
|
+
if (!cwd)
|
|
208
|
+
return undefined;
|
|
209
|
+
const info = this.list().find((i) => isSameOrSubpath(cwd, i.path));
|
|
210
|
+
if (!info && this.isManagedRoot(cwd))
|
|
211
|
+
throw new Error("Managed sandbox identity unavailable");
|
|
212
|
+
if (info) {
|
|
213
|
+
if (!existsSync(this.registryPath) || !lstatSync(this.registryPath).isFile())
|
|
214
|
+
throw new Error("Sandbox registry unavailable");
|
|
215
|
+
safeRoot(info.path);
|
|
216
|
+
}
|
|
217
|
+
return info;
|
|
218
|
+
}
|
|
219
|
+
getExecutionIdentity(cwd, captured) {
|
|
220
|
+
const info = this.getByCwd(cwd);
|
|
221
|
+
if ((info || captured || this.isManagedRoot(cwd)) && !existsSync(this.registryPath))
|
|
222
|
+
throw new Error("Sandbox registry unavailable");
|
|
223
|
+
if (captured &&
|
|
224
|
+
(!info || info.id !== captured.id || info.createdAt !== captured.createdAt || info.path !== captured.path))
|
|
225
|
+
throw new Error("Sandbox execution identity lost");
|
|
226
|
+
if (info)
|
|
227
|
+
this.assertAvailable(cwd);
|
|
228
|
+
return info;
|
|
229
|
+
}
|
|
230
|
+
assertAvailable(cwd) {
|
|
231
|
+
const info = this.getByCwd(cwd);
|
|
232
|
+
if (info && this.repo.busy.has(info.id))
|
|
233
|
+
throw new Error("Sandbox lifecycle operation in progress");
|
|
234
|
+
}
|
|
235
|
+
acquireExecution(cwd, captured) {
|
|
236
|
+
const info = this.getExecutionIdentity(cwd, captured);
|
|
237
|
+
if (!info)
|
|
238
|
+
return () => { };
|
|
239
|
+
this.repo.leases.set(info.id, (this.repo.leases.get(info.id) ?? 0) + 1);
|
|
240
|
+
let released = false;
|
|
241
|
+
return () => {
|
|
242
|
+
if (!released) {
|
|
243
|
+
released = true;
|
|
244
|
+
this.repo.leases.set(info.id, (this.repo.leases.get(info.id) ?? 1) - 1);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
require(target) {
|
|
249
|
+
const info = this.get(target) ?? this.getByCwd(target);
|
|
250
|
+
if (!info)
|
|
251
|
+
throw new Error(`Sandbox not found: ${target}`);
|
|
252
|
+
safeRoot(info.path);
|
|
253
|
+
return info;
|
|
254
|
+
}
|
|
255
|
+
async withLifecycle(target, action) {
|
|
256
|
+
const info = this.require(target);
|
|
257
|
+
const prev = this.repo.queues.get(info.id) ?? Promise.resolve();
|
|
258
|
+
let release;
|
|
259
|
+
const gate = new Promise((r) => (release = r));
|
|
260
|
+
this.repo.queues.set(info.id, gate);
|
|
261
|
+
this.repo.busy.add(info.id);
|
|
262
|
+
await prev.catch(() => { });
|
|
263
|
+
let active = true;
|
|
264
|
+
let used = false;
|
|
265
|
+
const check = () => {
|
|
266
|
+
if (!active || used)
|
|
267
|
+
throw new Error("Expired or already used lifecycle handle");
|
|
268
|
+
used = true;
|
|
269
|
+
this.require(info.id);
|
|
270
|
+
if (this.repo.leases.get(info.id))
|
|
271
|
+
throw new Error("Sandbox is running; stop execution before mutation");
|
|
272
|
+
};
|
|
273
|
+
const pending = [];
|
|
274
|
+
const track = (mutation) => {
|
|
275
|
+
const task = (async () => {
|
|
276
|
+
check();
|
|
277
|
+
return mutation();
|
|
278
|
+
})();
|
|
279
|
+
pending.push(task);
|
|
280
|
+
void task.catch(() => { });
|
|
281
|
+
return task;
|
|
282
|
+
};
|
|
283
|
+
let result;
|
|
284
|
+
let actionFailed = false;
|
|
285
|
+
let actionError;
|
|
286
|
+
let failures = [];
|
|
287
|
+
try {
|
|
288
|
+
try {
|
|
289
|
+
result = await action(info, {
|
|
290
|
+
reset: () => track(() => this.resetUnlocked(info)),
|
|
291
|
+
destroy: () => track(() => this.destroyUnlocked(info)),
|
|
292
|
+
applyToSource: (rev) => track(() => this.applyUnlocked(info, rev)),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
actionFailed = true;
|
|
297
|
+
actionError = error;
|
|
298
|
+
}
|
|
299
|
+
active = false;
|
|
300
|
+
const results = await Promise.allSettled(pending);
|
|
301
|
+
failures = results.filter((entry) => entry.status === "rejected").map((entry) => entry.reason);
|
|
302
|
+
}
|
|
303
|
+
finally {
|
|
304
|
+
active = false;
|
|
305
|
+
if (this.repo.queues.get(info.id) === gate) {
|
|
306
|
+
this.repo.queues.delete(info.id);
|
|
307
|
+
this.repo.busy.delete(info.id);
|
|
308
|
+
}
|
|
309
|
+
release();
|
|
310
|
+
}
|
|
311
|
+
if (actionFailed) {
|
|
312
|
+
const additional = failures.filter((error) => error !== actionError);
|
|
313
|
+
if (additional.length)
|
|
314
|
+
throw new AggregateError([actionError, ...additional], "Sandbox lifecycle operation failed");
|
|
315
|
+
throw actionError;
|
|
316
|
+
}
|
|
317
|
+
if (failures.length)
|
|
318
|
+
throw failures[0];
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
async create(opts) {
|
|
322
|
+
mkdirSync(this.sandboxesRoot, { recursive: true });
|
|
323
|
+
safeRoot(this.sandboxesRoot);
|
|
324
|
+
const id = `sandbox-${randomUUID()}`;
|
|
325
|
+
const path = wire(join(this.sandboxesRoot, id));
|
|
326
|
+
const sourcePath = opts.sourcePath ? wire(protectedRoot(opts.sourcePath)) : undefined;
|
|
327
|
+
if (sourcePath &&
|
|
328
|
+
(isSameOrSubpath(sourcePath, this.sandboxesRoot) || isSameOrSubpath(this.sandboxesRoot, sourcePath)))
|
|
329
|
+
throw new Error("Cannot import a managed sandbox");
|
|
330
|
+
const info = {
|
|
331
|
+
id,
|
|
332
|
+
name: opts.name?.trim() || basename(sourcePath ?? id),
|
|
333
|
+
path,
|
|
334
|
+
sourcePath,
|
|
335
|
+
ephemeral: opts.ephemeral ?? true,
|
|
336
|
+
createdAt: Date.now(),
|
|
337
|
+
};
|
|
338
|
+
await mkdir(path);
|
|
339
|
+
try {
|
|
340
|
+
const stats = sourcePath ? await copy(sourcePath, path) : { copiedFiles: 0, skippedFiles: 0 };
|
|
341
|
+
atomic(this.baselinePath(info), { version: 1, files: await tree(path) });
|
|
342
|
+
const items = new Map(this.repo.items);
|
|
343
|
+
items.set(id, Object.freeze(info));
|
|
344
|
+
this.commit(items);
|
|
345
|
+
return { sandbox: { ...info }, ...stats };
|
|
346
|
+
}
|
|
347
|
+
catch (error) {
|
|
348
|
+
await rm(path, { recursive: true });
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
persist(target) {
|
|
353
|
+
const info = this.require(target);
|
|
354
|
+
if (this.repo.busy.has(info.id))
|
|
355
|
+
throw new Error("Sandbox lifecycle operation in progress");
|
|
356
|
+
const updated = { ...info, ephemeral: false };
|
|
357
|
+
const items = new Map(this.repo.items);
|
|
358
|
+
items.set(info.id, Object.freeze(updated));
|
|
359
|
+
this.commit(items);
|
|
360
|
+
return updated;
|
|
361
|
+
}
|
|
362
|
+
reset(target) {
|
|
363
|
+
return this.withLifecycle(target, (_i, op) => op.reset());
|
|
364
|
+
}
|
|
365
|
+
async resetUnlocked(info) {
|
|
366
|
+
const staging = join(this.sandboxesRoot, `.pi-tmp-${randomUUID()}`);
|
|
367
|
+
const backup = `${staging}-old`;
|
|
368
|
+
await mkdir(staging);
|
|
369
|
+
let swapped = false;
|
|
370
|
+
const baseline = this.baselinePath(info);
|
|
371
|
+
const oldBaseline = existsSync(baseline) ? readFileSync(baseline) : undefined;
|
|
372
|
+
try {
|
|
373
|
+
const stats = info.sourcePath
|
|
374
|
+
? await copy(protectedRoot(info.sourcePath), staging)
|
|
375
|
+
: { copiedFiles: 0, skippedFiles: 0 };
|
|
376
|
+
const files = await tree(staging);
|
|
377
|
+
safeRoot(info.path);
|
|
378
|
+
await rename(info.path, backup);
|
|
379
|
+
try {
|
|
380
|
+
await rename(staging, info.path);
|
|
381
|
+
swapped = true;
|
|
382
|
+
atomic(baseline, { version: 1, files });
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
if (swapped)
|
|
386
|
+
await rename(info.path, staging);
|
|
387
|
+
await rename(backup, info.path);
|
|
388
|
+
if (oldBaseline)
|
|
389
|
+
atomic(baseline, JSON.parse(oldBaseline.toString("utf8")));
|
|
390
|
+
throw error;
|
|
391
|
+
}
|
|
392
|
+
await rm(backup, { recursive: true });
|
|
393
|
+
return { sandbox: info, ...stats };
|
|
394
|
+
}
|
|
395
|
+
finally {
|
|
396
|
+
if (existsSync(staging))
|
|
397
|
+
await rm(staging, { recursive: true });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async promote(target, targetPath) {
|
|
401
|
+
return this.withLifecycle(target, async (info) => {
|
|
402
|
+
if (this.repo.leases.get(info.id))
|
|
403
|
+
throw new Error("Sandbox is running");
|
|
404
|
+
const trimmed = targetPath.trim();
|
|
405
|
+
if (!trimmed)
|
|
406
|
+
throw new Error("Target path is required");
|
|
407
|
+
const dest = resolve(trimmed === "~" ? homedir() : /^~[\\/\\\\]/.test(trimmed) ? join(homedir(), trimmed.slice(2)) : trimmed);
|
|
408
|
+
if (isSameOrSubpath(dest, this.sandboxesRoot))
|
|
409
|
+
throw new Error("Target must be outside managed sandboxes");
|
|
410
|
+
if (existsSync(dest)) {
|
|
411
|
+
protectedRoot(dest);
|
|
412
|
+
if ((await readdir(dest)).length)
|
|
413
|
+
throw new Error("Target directory is not empty");
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
await createSafeDirectories(dest);
|
|
417
|
+
protectedRoot(dest);
|
|
418
|
+
}
|
|
419
|
+
const stats = await copy(info.path, dest);
|
|
420
|
+
return { sandbox: info, targetPath: wire(dest), copiedFiles: stats.copiedFiles };
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
async diffWithSource(target) {
|
|
424
|
+
const info = this.require(target);
|
|
425
|
+
const base = this.baseline(info);
|
|
426
|
+
const current = await tree(info.path);
|
|
427
|
+
const source = info.sourcePath ? await tree(protectedRoot(info.sourcePath)) : {};
|
|
428
|
+
const added = [], modified = [], deleted = [];
|
|
429
|
+
let unchangedCount = 0;
|
|
430
|
+
for (const rel of new Set([...Object.keys(base), ...Object.keys(current)])) {
|
|
431
|
+
if (base[rel] === "dir" && current[rel] === "dir")
|
|
432
|
+
continue;
|
|
433
|
+
if (base[rel] === current[rel]) {
|
|
434
|
+
if (base[rel]?.startsWith("file:"))
|
|
435
|
+
unchangedCount++;
|
|
436
|
+
}
|
|
437
|
+
else if (!base[rel])
|
|
438
|
+
added.push(rel);
|
|
439
|
+
else if (!current[rel])
|
|
440
|
+
deleted.push(rel);
|
|
441
|
+
else
|
|
442
|
+
modified.push(rel);
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
sandbox: info,
|
|
446
|
+
sourcePath: info.sourcePath,
|
|
447
|
+
added: added.sort(),
|
|
448
|
+
modified: modified.sort(),
|
|
449
|
+
deleted: deleted.sort(),
|
|
450
|
+
unchangedCount,
|
|
451
|
+
expectedRevision: hash(JSON.stringify([base, current, source])),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
applyToSource(target, expectedRevision) {
|
|
455
|
+
return this.withLifecycle(target, (_i, op) => op.applyToSource(expectedRevision));
|
|
456
|
+
}
|
|
457
|
+
async applyUnlocked(info, expectedRevision) {
|
|
458
|
+
if (!info.sourcePath)
|
|
459
|
+
throw new Error("Sandbox has no source project");
|
|
460
|
+
const source = protectedRoot(info.sourcePath);
|
|
461
|
+
const base = this.baseline(info);
|
|
462
|
+
const current = await tree(info.path);
|
|
463
|
+
const original = await tree(source);
|
|
464
|
+
const revision = hash(JSON.stringify([base, current, original]));
|
|
465
|
+
if (!expectedRevision)
|
|
466
|
+
throw new Error("Sandbox preview revision required; refresh preview");
|
|
467
|
+
if (revision !== expectedRevision)
|
|
468
|
+
throw new Error("Sandbox or source changed; refresh preview");
|
|
469
|
+
const changes = [...new Set([...Object.keys(base), ...Object.keys(current)])].filter((r) => base[r] !== current[r] && original[r] !== current[r]);
|
|
470
|
+
for (const rel of changes) {
|
|
471
|
+
if (base[rel] === "dir" &&
|
|
472
|
+
!current[rel] &&
|
|
473
|
+
Object.keys(original).some((p) => p.startsWith(rel + "/") && !Object.hasOwn(base, p)))
|
|
474
|
+
throw new Error(`Sandbox directory conflict: ${rel}`);
|
|
475
|
+
if (original[rel] !== base[rel] && original[rel] !== current[rel])
|
|
476
|
+
throw new Error(`Sandbox conflict: ${rel}`);
|
|
477
|
+
if (base[rel] === "link" || current[rel] === "link" || original[rel] === "link")
|
|
478
|
+
throw new Error(`Linked target refused: ${rel}`);
|
|
479
|
+
if (base[rel] && current[rel] && (base[rel] === "dir" || current[rel] === "dir"))
|
|
480
|
+
throw new Error(`File type conflict: ${rel}`);
|
|
481
|
+
let parent = dirname(rel);
|
|
482
|
+
while (parent !== ".") {
|
|
483
|
+
if (original[parent] && original[parent] !== "dir")
|
|
484
|
+
throw new Error(`Unsafe target ancestor: ${parent}`);
|
|
485
|
+
parent = dirname(parent);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const stage = join(source, `.pi-tmp-${randomUUID()}`);
|
|
489
|
+
await mkdir(stage);
|
|
490
|
+
const undo = [];
|
|
491
|
+
const madeDirs = [];
|
|
492
|
+
let copiedFiles = 0, deletedFiles = 0;
|
|
493
|
+
let retainRecovery = false;
|
|
494
|
+
try {
|
|
495
|
+
for (let i = 0; i < changes.length; i++) {
|
|
496
|
+
const rel = changes[i];
|
|
497
|
+
if (current[rel]?.startsWith("file:")) {
|
|
498
|
+
await copyFile(join(info.path, rel), join(stage, `new-${i}`));
|
|
499
|
+
await chmod(join(stage, `new-${i}`), (await lstat(join(info.path, rel))).mode & 0o777);
|
|
500
|
+
const staged = await lstat(join(stage, `new-${i}`));
|
|
501
|
+
if (`file:${staged.mode & 0o777}:${hash(await readFile(join(stage, `new-${i}`)))}` !== current[rel])
|
|
502
|
+
throw new Error(`Sandbox changed during staging: ${rel}`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (revision !== hash(JSON.stringify([base, await tree(info.path), await tree(source)])))
|
|
506
|
+
throw new Error("Source or sandbox changed while staging");
|
|
507
|
+
atomic(join(stage, "recovery.json"), {
|
|
508
|
+
version: 1,
|
|
509
|
+
source,
|
|
510
|
+
sandboxId: info.id,
|
|
511
|
+
changes: changes.map((rel, index) => ({
|
|
512
|
+
rel,
|
|
513
|
+
backup: `old-${index}`,
|
|
514
|
+
staged: `new-${index}`,
|
|
515
|
+
original: original[rel],
|
|
516
|
+
desired: current[rel],
|
|
517
|
+
})),
|
|
518
|
+
});
|
|
519
|
+
for (let i = 0; i < changes.length; i++) {
|
|
520
|
+
const rel = changes[i];
|
|
521
|
+
const value = current[rel];
|
|
522
|
+
const path = join(source, rel);
|
|
523
|
+
protectedRoot(source);
|
|
524
|
+
if (base[rel] === "dir" && !value)
|
|
525
|
+
continue;
|
|
526
|
+
safeRoot(dirname(path));
|
|
527
|
+
if (original[rel] && !existsSync(path))
|
|
528
|
+
throw new Error(`Source disappeared before commit: ${rel}`);
|
|
529
|
+
if (value === "dir") {
|
|
530
|
+
if (!existsSync(path)) {
|
|
531
|
+
await mkdir(path);
|
|
532
|
+
madeDirs.push(path);
|
|
533
|
+
}
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
if (base[rel] === "dir")
|
|
537
|
+
continue;
|
|
538
|
+
if (existsSync(path)) {
|
|
539
|
+
const st = await lstat(path);
|
|
540
|
+
if (st.isFile() && `file:${st.mode & 0o777}:${hash(await readFile(path))}` !== original[rel])
|
|
541
|
+
throw new Error(`Source changed before commit: ${rel}`);
|
|
542
|
+
if (st.isSymbolicLink() || !st.isFile() || st.nlink > 1)
|
|
543
|
+
throw new Error(`Unsafe linked or changed target: ${rel}`);
|
|
544
|
+
const backup = join(stage, `old-${i}`);
|
|
545
|
+
await rename(path, backup);
|
|
546
|
+
let installed = false;
|
|
547
|
+
undo.push(async () => {
|
|
548
|
+
safeRoot(dirname(path));
|
|
549
|
+
if ((await fingerprint(path)) !== (installed ? value : undefined))
|
|
550
|
+
throw new Error(`Concurrent source change during rollback: ${rel}`);
|
|
551
|
+
if (installed && value)
|
|
552
|
+
await rm(path);
|
|
553
|
+
await rename(backup, path);
|
|
554
|
+
});
|
|
555
|
+
if (value) {
|
|
556
|
+
await rename(join(stage, `new-${i}`), path);
|
|
557
|
+
installed = true;
|
|
558
|
+
copiedFiles++;
|
|
559
|
+
}
|
|
560
|
+
else
|
|
561
|
+
deletedFiles++;
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
let installed = false;
|
|
565
|
+
undo.push(async () => {
|
|
566
|
+
safeRoot(dirname(path));
|
|
567
|
+
if ((await fingerprint(path)) !== (installed ? value : undefined))
|
|
568
|
+
throw new Error(`Concurrent source change during rollback: ${rel}`);
|
|
569
|
+
if (installed && value)
|
|
570
|
+
await rm(path);
|
|
571
|
+
});
|
|
572
|
+
if (value) {
|
|
573
|
+
await rename(join(stage, `new-${i}`), path);
|
|
574
|
+
installed = true;
|
|
575
|
+
copiedFiles++;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
for (const rel of changes
|
|
580
|
+
.filter((rel) => base[rel] === "dir" && !current[rel])
|
|
581
|
+
.sort((a, b) => b.split("/").length - a.split("/").length)) {
|
|
582
|
+
const path = join(source, rel);
|
|
583
|
+
safeRoot(path);
|
|
584
|
+
await rmdir(path);
|
|
585
|
+
undo.push(async () => {
|
|
586
|
+
safeRoot(dirname(path));
|
|
587
|
+
if ((await fingerprint(path)) !== undefined)
|
|
588
|
+
throw new Error(`Concurrent directory change during rollback: ${rel}`);
|
|
589
|
+
await mkdir(path);
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
// Keep the import baseline: subsequent previews continue to show the sandbox's changes.
|
|
593
|
+
return { sandbox: info, sourcePath: info.sourcePath, copiedFiles, deletedFiles };
|
|
594
|
+
}
|
|
595
|
+
catch (error) {
|
|
596
|
+
const failures = [];
|
|
597
|
+
for (const rollback of undo.reverse())
|
|
598
|
+
try {
|
|
599
|
+
await rollback();
|
|
600
|
+
}
|
|
601
|
+
catch (e) {
|
|
602
|
+
failures.push(e);
|
|
603
|
+
}
|
|
604
|
+
for (const dir of madeDirs.reverse())
|
|
605
|
+
try {
|
|
606
|
+
await rmdir(dir);
|
|
607
|
+
}
|
|
608
|
+
catch (e) {
|
|
609
|
+
failures.push(e);
|
|
610
|
+
}
|
|
611
|
+
if (failures.length) {
|
|
612
|
+
retainRecovery = true;
|
|
613
|
+
throw new AggregateError([error, ...failures], `Sandbox rollback failed; recovery files retained at ${stage}`);
|
|
614
|
+
}
|
|
615
|
+
throw error;
|
|
616
|
+
}
|
|
617
|
+
finally {
|
|
618
|
+
if (!retainRecovery)
|
|
619
|
+
await rm(stage, { recursive: true });
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
destroy(target) {
|
|
623
|
+
if (!this.get(target) && !this.getByCwd(target))
|
|
624
|
+
return Promise.resolve(undefined);
|
|
625
|
+
return this.withLifecycle(target, (_i, op) => op.destroy());
|
|
626
|
+
}
|
|
627
|
+
async destroyUnlocked(info) {
|
|
628
|
+
this.require(info.id);
|
|
629
|
+
const tombstone = join(this.sandboxesRoot, `.pi-tmp-destroy-${info.id}-${randomUUID()}`);
|
|
630
|
+
await rename(info.path, tombstone);
|
|
631
|
+
try {
|
|
632
|
+
const items = new Map(this.repo.items);
|
|
633
|
+
items.delete(info.id);
|
|
634
|
+
this.commit(items);
|
|
635
|
+
}
|
|
636
|
+
catch (error) {
|
|
637
|
+
try {
|
|
638
|
+
await rename(tombstone, info.path);
|
|
639
|
+
}
|
|
640
|
+
catch (rollback) {
|
|
641
|
+
throw new AggregateError([error, rollback], `Sandbox destroy rollback failed; contents retained at ${tombstone}`);
|
|
642
|
+
}
|
|
643
|
+
throw error;
|
|
644
|
+
}
|
|
645
|
+
try {
|
|
646
|
+
await rm(tombstone, { recursive: true });
|
|
647
|
+
}
|
|
648
|
+
catch (error) {
|
|
649
|
+
throw new Error(`Sandbox deregistered but cleanup failed; recovery contents retained at ${tombstone}`, {
|
|
650
|
+
cause: error,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
return info;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** These external runtimes cannot enforce the host jail before native tool execution. */
|
|
2
|
+
export function assertEngineWorkspaceSupported(manager, cwd, engine) {
|
|
3
|
+
manager.assertAvailable(cwd);
|
|
4
|
+
if (manager.getExecutionIdentity(cwd)) {
|
|
5
|
+
throw new Error(`Engine "${engine}" does not support managed sandbox execution: native tools cannot be restricted before execution. Use the pi engine.`);
|
|
6
|
+
}
|
|
7
|
+
}
|