agent-supervisor-mcp 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.en.md +208 -0
- package/README.md +206 -2
- package/package.json +76 -4
- package/skills/agent-supervisor/SKILL.md +139 -0
- package/src/app/acceptance.js +263 -0
- package/src/app/auto-cleanup.js +91 -0
- package/src/app/cancel.js +57 -0
- package/src/app/cleanup-facts.js +101 -0
- package/src/app/cleanup-plan.js +166 -0
- package/src/app/compose.js +17 -0
- package/src/app/context-compiler.js +83 -0
- package/src/app/dispatch.js +398 -0
- package/src/app/followup.js +104 -0
- package/src/app/land-chain.js +134 -0
- package/src/app/landing.js +357 -0
- package/src/app/ownership.js +57 -0
- package/src/app/rebase.js +216 -0
- package/src/app/restore-worktree.js +51 -0
- package/src/app/resume.js +179 -0
- package/src/app/run-state.js +26 -0
- package/src/app/settled.js +28 -0
- package/src/app/side-chat.js +370 -0
- package/src/app/sse.js +20 -0
- package/src/app/start-run.js +77 -0
- package/src/app/verify-push.js +114 -0
- package/src/app/worktree-setup.js +218 -0
- package/src/domain/acceptance-baseline.js +65 -0
- package/src/domain/acceptance-spec.js +30 -0
- package/src/domain/cleanup-policy.js +44 -0
- package/src/domain/command-outcome.js +6 -0
- package/src/domain/failure-class.js +63 -0
- package/src/domain/owned-changes.js +27 -0
- package/src/domain/prompt.js +161 -0
- package/src/domain/side-turn-view.js +73 -0
- package/src/domain/status.js +50 -0
- package/src/domain/thread-health.js +33 -0
- package/src/domain/truncate.js +148 -0
- package/src/domain/worker-branch.js +6 -0
- package/src/domain/worker-changes.js +58 -0
- package/src/domain/worker-env.js +14 -0
- package/src/domain/worker-preflight.js +167 -0
- package/src/domain/worker-report.js +85 -0
- package/src/domain/worker-report.schema.json +10 -0
- package/src/infra/bin-resolver.js +287 -0
- package/src/infra/cleanup-hint.js +29 -0
- package/src/infra/crash-guard.js +44 -0
- package/src/infra/external-output.js +29 -0
- package/src/infra/git.js +61 -0
- package/src/infra/handoff.js +38 -0
- package/src/infra/paths.js +34 -0
- package/src/infra/process.js +59 -0
- package/src/infra/sandbox-roots.js +106 -0
- package/src/infra/skill-sync.js +109 -0
- package/src/infra/stage-store.js +39 -0
- package/src/infra/store/db.js +265 -0
- package/src/infra/store/event-body.js +82 -0
- package/src/infra/store/events.js +149 -0
- package/src/infra/store/facts.js +338 -0
- package/src/infra/store/reconcile.js +173 -0
- package/src/infra/store/records.js +171 -0
- package/src/infra/store/runner-readers.js +16 -0
- package/src/infra/store/schema.js +306 -0
- package/src/infra/store/sessions.js +138 -0
- package/src/infra/store/tasks.js +351 -0
- package/src/infra/update/auto-update-worker.js +186 -0
- package/src/infra/update/auto-update.js +143 -0
- package/src/infra/update/update-check.js +246 -0
- package/src/infra/workspace-kind.js +165 -0
- package/src/infra/worktree.js +361 -0
- package/src/interface/bin/acceptance-cli.js +62 -0
- package/src/interface/bin/cleanup.js +81 -0
- package/src/interface/bin/monitor.jsx +301 -0
- package/src/interface/bin/setup.js +342 -0
- package/src/interface/bin/stage.js +17 -0
- package/src/interface/bin/web-server.js +53 -0
- package/src/interface/mcp/handlers/delivery.js +165 -0
- package/src/interface/mcp/handlers/dispatch.js +236 -0
- package/src/interface/mcp/handlers/facts.js +204 -0
- package/src/interface/mcp/handlers/overview.js +106 -0
- package/src/interface/mcp/handlers/results.js +262 -0
- package/src/interface/mcp/handlers/review.js +115 -0
- package/src/interface/mcp/handlers/wait.js +141 -0
- package/src/interface/mcp/meta.js +7 -0
- package/src/interface/mcp/result.js +34 -0
- package/src/interface/mcp/schemas.js +24 -0
- package/src/interface/mcp/server.js +64 -0
- package/src/interface/mcp/tools/deliver.js +48 -0
- package/src/interface/mcp/tools/dispatch.js +90 -0
- package/src/interface/mcp/tools/optional.js +57 -0
- package/src/interface/mcp/tools/read.js +115 -0
- package/src/interface/mcp/tools/run.js +57 -0
- package/src/interface/mcp/tools/shapes.js +67 -0
- package/src/interface/mcp/tools/track.js +84 -0
- package/src/interface/mcp/update-state.js +34 -0
- package/src/interface/mcp/views/overview.js +183 -0
- package/src/interface/mcp/views/task.js +224 -0
- package/src/interface/web/app.js +226 -0
- package/src/interface/web/views.js +227 -0
- package/src/mcp-server.js +4 -0
- package/src/run-host/entry.js +26 -0
- package/src/run-host/finalize.js +243 -0
- package/src/run-host/launch.js +49 -0
- package/src/run-host/live-run.js +76 -0
- package/src/run-host/outcome-facts.js +61 -0
- package/src/run-host/streams.js +102 -0
- package/src/run-host/track.js +132 -0
- package/src/runners/claude/adapter.js +118 -0
- package/src/runners/claude/events.js +100 -0
- package/src/runners/claude/tool-guard.js +48 -0
- package/src/runners/codex/adapter.js +157 -0
- package/src/runners/codex/chat-chunks.js +97 -0
- package/src/runners/codex/event-parser.js +266 -0
- package/src/runners/codex/goal-store.js +54 -0
- package/src/runners/codex/mcp-config.js +39 -0
- package/src/runners/codex/tool-guard.js +184 -0
- package/src/runners/contract-check.js +82 -0
- package/src/runners/contract.js +53 -0
- package/src/runners/registry.js +45 -0
- package/web/dist/assets/index-D-A2xlpx.js +154 -0
- package/web/dist/assets/index-f8esISCp.css +2 -0
- package/web/dist/index.html +15 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// Is the running copy the latest published one? Asked once per MCP start and
|
|
2
|
+
// again every few hours, answered from the npm registry, cached on disk so a
|
|
3
|
+
// burst of MCP processes does not turn into a burst of registry calls.
|
|
4
|
+
//
|
|
5
|
+
// Two things are compared. The version, against the registry's `latest`
|
|
6
|
+
// dist-tag. And the tarball integrity: a global install records the
|
|
7
|
+
// integrity of what npm unpacked in node_modules/.package-lock.json, and
|
|
8
|
+
// the registry says what the integrity of that version should be. A
|
|
9
|
+
// mismatch means the files on disk are not the published build.
|
|
10
|
+
import { runGit } from "../git.js";
|
|
11
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
12
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
13
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
import { dataDir } from "../paths.js";
|
|
16
|
+
|
|
17
|
+
const PACKAGE_NAME = "agent-supervisor-mcp";
|
|
18
|
+
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
|
19
|
+
const packageJson = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
|
|
20
|
+
|
|
21
|
+
export const INSTALL_COMMAND = `npm i -g ${PACKAGE_NAME}@latest`;
|
|
22
|
+
const DEFAULT_REGISTRY = "https://registry.npmjs.org";
|
|
23
|
+
const CACHE_TTL_MS = 60 * 60 * 1000;
|
|
24
|
+
const cachePath = join(dataDir, "update-check.json");
|
|
25
|
+
|
|
26
|
+
function registryUrl() {
|
|
27
|
+
return (process.env.AGENT_SUPERVISOR_REGISTRY?.trim() || DEFAULT_REGISTRY).replace(/\/+$/, "");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function timeoutMs() {
|
|
31
|
+
const raw = Number(process.env.AGENT_SUPERVISOR_UPDATE_TIMEOUT_MS ?? 4000);
|
|
32
|
+
return Number.isFinite(raw) && raw > 0 ? raw : 4000;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function updateCheckDisabled() {
|
|
36
|
+
return /^(1|true|yes)$/i.test(process.env.AGENT_SUPERVISOR_NO_UPDATE_CHECK?.trim() ?? "");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// "1.2.3" -> [1,2,3]; a prerelease tag sorts below the release it precedes.
|
|
40
|
+
export function parseVersion(text) {
|
|
41
|
+
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?/.exec(String(text ?? "").trim());
|
|
42
|
+
if (!match) return null;
|
|
43
|
+
return { parts: [Number(match[1]), Number(match[2]), Number(match[3])], prerelease: match[4] ?? null };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function compareVersions(a, b) {
|
|
47
|
+
const left = parseVersion(a);
|
|
48
|
+
const right = parseVersion(b);
|
|
49
|
+
if (!left || !right) return null;
|
|
50
|
+
for (let i = 0; i < 3; i += 1) {
|
|
51
|
+
if (left.parts[i] !== right.parts[i]) return left.parts[i] < right.parts[i] ? -1 : 1;
|
|
52
|
+
}
|
|
53
|
+
if (left.prerelease === right.prerelease) return 0;
|
|
54
|
+
if (left.prerelease === null) return 1;
|
|
55
|
+
if (right.prerelease === null) return -1;
|
|
56
|
+
return left.prerelease < right.prerelease ? -1 : 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// What is actually on disk: the version from package.json, plus whichever
|
|
60
|
+
// provenance is available - npm's recorded integrity for a global install,
|
|
61
|
+
// the HEAD commit for a git checkout.
|
|
62
|
+
// A copy that npx fetched lives under npm's cache (.../_npx/<hash>/...).
|
|
63
|
+
// npx resolves `latest` on every start, so such a copy must never be
|
|
64
|
+
// auto-installed over a global one.
|
|
65
|
+
export function isNpxPath(path) {
|
|
66
|
+
return String(path).split(/[\\/]/).includes("_npx");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Who put this copy here. Pure, so it can be tested on made-up paths.
|
|
70
|
+
// git - a checkout (also what `npm link` resolves to, since node follows
|
|
71
|
+
// the symlink before import.meta.url is set)
|
|
72
|
+
// npx - npm's exec cache; npx refreshes it by itself
|
|
73
|
+
// npm - a directory npm installed, global or project-local (the
|
|
74
|
+
// auto-updater tells those apart with `npm root -g` before writing)
|
|
75
|
+
export function installSource(root, { exists = existsSync } = {}) {
|
|
76
|
+
if (exists(join(root, ".git"))) return "git";
|
|
77
|
+
if (isNpxPath(root)) return "npx";
|
|
78
|
+
if (basename(dirname(root)) === "node_modules") return "npm";
|
|
79
|
+
return "unknown";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function installedPackage() {
|
|
83
|
+
const info = { name: PACKAGE_NAME, version: packageJson.version, path: packageRoot, integrity: null, commit: null, source: installSource(packageRoot) };
|
|
84
|
+
// A project-local install records the package's integrity in the hidden
|
|
85
|
+
// lockfile one level up. A global install has no such record (each global
|
|
86
|
+
// package is its own root), so integrity stays null and only the version
|
|
87
|
+
// is compared.
|
|
88
|
+
const lockPath = join(packageRoot, "..", ".package-lock.json");
|
|
89
|
+
if (info.source === "npm" && existsSync(lockPath)) {
|
|
90
|
+
try {
|
|
91
|
+
const lock = JSON.parse(readFileSync(lockPath, "utf8"));
|
|
92
|
+
const entry = lock.packages?.[`node_modules/${PACKAGE_NAME}`];
|
|
93
|
+
if (entry?.integrity) info.integrity = entry.integrity;
|
|
94
|
+
} catch {
|
|
95
|
+
// unreadable lock: integrity stays unknown
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (info.source === "git") {
|
|
99
|
+
try {
|
|
100
|
+
info.commit = runGit(packageRoot, ["rev-parse", "HEAD"], { timeout: 5000, stderr: "ignore" });
|
|
101
|
+
} catch {
|
|
102
|
+
// a checkout without a usable git: still a checkout
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return info;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function fetchRegistry() {
|
|
109
|
+
const controller = new AbortController();
|
|
110
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs());
|
|
111
|
+
try {
|
|
112
|
+
const response = await fetch(`${registryUrl()}/${PACKAGE_NAME}`, {
|
|
113
|
+
signal: controller.signal,
|
|
114
|
+
headers: { accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8" }
|
|
115
|
+
});
|
|
116
|
+
if (!response.ok) throw new Error(`registry answered ${response.status}`);
|
|
117
|
+
const body = await response.json();
|
|
118
|
+
const latestVersion = body?.["dist-tags"]?.latest;
|
|
119
|
+
if (!latestVersion || !parseVersion(latestVersion)) throw new Error("registry reply has no usable dist-tags.latest");
|
|
120
|
+
const dist = body.versions?.[latestVersion]?.dist ?? {};
|
|
121
|
+
return {
|
|
122
|
+
latest: {
|
|
123
|
+
version: latestVersion,
|
|
124
|
+
integrity: dist.integrity ?? null,
|
|
125
|
+
shasum: dist.shasum ?? null,
|
|
126
|
+
published_at: body.time?.[latestVersion] ?? null
|
|
127
|
+
},
|
|
128
|
+
versions: Object.fromEntries(
|
|
129
|
+
Object.entries(body.versions ?? {}).map(([version, meta]) => [version, { integrity: meta?.dist?.integrity ?? null }])
|
|
130
|
+
)
|
|
131
|
+
};
|
|
132
|
+
} finally {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function readCache() {
|
|
138
|
+
try {
|
|
139
|
+
return JSON.parse(await readFile(cachePath, "utf8"));
|
|
140
|
+
} catch {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function writeCache(payload) {
|
|
146
|
+
try {
|
|
147
|
+
await mkdir(dataDir, { recursive: true });
|
|
148
|
+
await writeFile(cachePath, JSON.stringify(payload));
|
|
149
|
+
} catch {
|
|
150
|
+
// a cache that cannot be written only costs a registry call next time
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Pure: given what is installed and what the registry said, what to tell.
|
|
155
|
+
export function evaluateUpdate(installed, registry, { source, error }) {
|
|
156
|
+
const cmp = registry ? compareVersions(installed.version, registry.latest.version) : null;
|
|
157
|
+
const updateAvailable = cmp !== null && cmp < 0;
|
|
158
|
+
const expected = registry?.versions?.[installed.version]?.integrity ?? null;
|
|
159
|
+
const integrityMatches = installed.integrity && expected ? installed.integrity === expected : null;
|
|
160
|
+
let notice = null;
|
|
161
|
+
if (updateAvailable) {
|
|
162
|
+
notice = `agent-supervisor-mcp ${installed.version} is installed but ${registry.latest.version} is published. Run \`${INSTALL_COMMAND}\` and restart the MCP client.`;
|
|
163
|
+
} else if (integrityMatches === false) {
|
|
164
|
+
notice = `The installed agent-supervisor-mcp ${installed.version} does not match the published tarball (integrity differs). Reinstall with \`${INSTALL_COMMAND}\`.`;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
checked_at: new Date().toISOString(),
|
|
168
|
+
source,
|
|
169
|
+
error: error ?? null,
|
|
170
|
+
installed: { version: installed.version, integrity: installed.integrity, commit: installed.commit, source: installed.source, path: installed.path },
|
|
171
|
+
latest: registry?.latest ?? null,
|
|
172
|
+
update_available: updateAvailable,
|
|
173
|
+
integrity_matches: integrityMatches,
|
|
174
|
+
install_command: INSTALL_COMMAND,
|
|
175
|
+
notice
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
let inflight = null;
|
|
180
|
+
|
|
181
|
+
// The full answer. `force` skips the on-disk cache. Never throws: a registry
|
|
182
|
+
// that is down yields source: "offline" with the reason in `error`.
|
|
183
|
+
export async function checkForUpdate({ force = false } = {}) {
|
|
184
|
+
if (updateCheckDisabled()) {
|
|
185
|
+
return { ...evaluateUpdate(installedPackage(), null, { source: "disabled", error: null }), disabled: true };
|
|
186
|
+
}
|
|
187
|
+
if (inflight) return inflight;
|
|
188
|
+
inflight = (async () => {
|
|
189
|
+
const installed = installedPackage();
|
|
190
|
+
if (!force) {
|
|
191
|
+
const cached = await readCache();
|
|
192
|
+
const age = cached?.checked_at ? Date.now() - Date.parse(cached.checked_at) : Number.POSITIVE_INFINITY;
|
|
193
|
+
if (cached?.registry && age >= 0 && age < CACHE_TTL_MS && cached.registry_url === registryUrl()) {
|
|
194
|
+
return evaluateUpdate(installed, cached.registry, { source: "cache", error: null });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const registry = await fetchRegistry();
|
|
199
|
+
await writeCache({ checked_at: new Date().toISOString(), registry_url: registryUrl(), registry });
|
|
200
|
+
return evaluateUpdate(installed, registry, { source: "registry", error: null });
|
|
201
|
+
} catch (error) {
|
|
202
|
+
const cached = await readCache();
|
|
203
|
+
if (cached?.registry && cached.registry_url === registryUrl()) {
|
|
204
|
+
return evaluateUpdate(installed, cached.registry, { source: "stale-cache", error: error.message });
|
|
205
|
+
}
|
|
206
|
+
return evaluateUpdate(installed, null, { source: "offline", error: error.message });
|
|
207
|
+
}
|
|
208
|
+
})().finally(() => {
|
|
209
|
+
inflight = null;
|
|
210
|
+
});
|
|
211
|
+
return inflight;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// The small object that rides along in tool results. Null when there is
|
|
215
|
+
// nothing to say, so the common case costs no bytes.
|
|
216
|
+
// `autoUpdate` is the auto-update record (see auto-update.js); when it says
|
|
217
|
+
// the install is already under way or done, the notice stops asking the user
|
|
218
|
+
// to run npm and says what to do instead: start a new session.
|
|
219
|
+
export function updateNotice(result, autoUpdate = null) {
|
|
220
|
+
if (!result || !result.notice) return null;
|
|
221
|
+
const field = {
|
|
222
|
+
update_available: result.update_available,
|
|
223
|
+
installed_version: result.installed.version,
|
|
224
|
+
latest_version: result.latest?.version ?? null,
|
|
225
|
+
integrity_matches: result.integrity_matches,
|
|
226
|
+
install_command: result.install_command,
|
|
227
|
+
notice: result.notice
|
|
228
|
+
};
|
|
229
|
+
if (result.update_available && autoUpdate && autoUpdate.version === field.latest_version) {
|
|
230
|
+
const name = `${PACKAGE_NAME} ${field.latest_version}`;
|
|
231
|
+
if (autoUpdate.state === "done") {
|
|
232
|
+
field.install_command = null;
|
|
233
|
+
field.notice = `${name} has been installed in the background; this session still runs ${field.installed_version}. Start a new session (restart the MCP client) to use it.`;
|
|
234
|
+
} else if (autoUpdate.state === "started" || autoUpdate.state === "running") {
|
|
235
|
+
field.install_command = null;
|
|
236
|
+
field.notice = `${name} is being installed in the background; this session still runs ${field.installed_version}. The next session starts on it once the install finishes.`;
|
|
237
|
+
} else if (autoUpdate.state === "failed") {
|
|
238
|
+
field.notice = `${field.notice} (automatic install failed: ${autoUpdate.reason ?? "unknown"})`;
|
|
239
|
+
}
|
|
240
|
+
field.auto_update = { state: autoUpdate.state, version: autoUpdate.version, ...(autoUpdate.reason ? { reason: autoUpdate.reason } : {}) };
|
|
241
|
+
} else if (result.update_available && result.installed.source === "npx") {
|
|
242
|
+
field.install_command = null;
|
|
243
|
+
field.notice = `${PACKAGE_NAME} ${field.latest_version} is published; this npx-started session runs ${field.installed_version}. The next session fetches the new one by itself.`;
|
|
244
|
+
}
|
|
245
|
+
return field;
|
|
246
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { runGit } from "./git.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { createReadStream, existsSync } from "node:fs";
|
|
4
|
+
import { lstat, mkdir, readdir, readlink, rm } from "node:fs/promises";
|
|
5
|
+
import { join, relative, resolve } from "node:path";
|
|
6
|
+
import { scratchDir } from "./paths.js";
|
|
7
|
+
|
|
8
|
+
export const WORKSPACE_KINDS = ["git_worktree", "read_only_in_place", "scratch", "in_place_write"];
|
|
9
|
+
const MAX_MANIFEST_ENTRIES = 20000;
|
|
10
|
+
|
|
11
|
+
function workspaceError(code, message) {
|
|
12
|
+
const error = new Error(message);
|
|
13
|
+
error.code = code;
|
|
14
|
+
return error;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isInside(root, path) {
|
|
18
|
+
const rel = relative(root, path);
|
|
19
|
+
return rel === "" || (!rel.startsWith("..") && !rel.includes(`..${process.platform === "win32" ? "\\" : "/"}`));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isGitCheckout(cwd) {
|
|
23
|
+
try {
|
|
24
|
+
return runGit(cwd, ["rev-parse", "--is-inside-work-tree"], { stderr: "ignore", timeout: 15000 }) === "true";
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function selectedKind({ sourceRoot, workspaceKind, sandbox }) {
|
|
31
|
+
if (workspaceKind != null && !WORKSPACE_KINDS.includes(workspaceKind)) {
|
|
32
|
+
throw workspaceError("invalid_workspace_kind", `workspace.kind must be one of ${WORKSPACE_KINDS.join(", ")}`);
|
|
33
|
+
}
|
|
34
|
+
if (workspaceKind) return workspaceKind;
|
|
35
|
+
if (isGitCheckout(sourceRoot)) return "git_worktree";
|
|
36
|
+
if (sandbox === "read-only") return "read_only_in_place";
|
|
37
|
+
throw workspaceError("workspace_kind_required", "a non-Git worker that can write must declare workspace.kind: scratch or in_place_write; the supervisor never writes a user directory by default");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function validateKind({ kind, sourceRoot, sandbox, ownedPaths, networkAccess, acceptance, externalOutputs, inPlaceAuthorization, isPreflight }) {
|
|
41
|
+
if (kind === "git_worktree") {
|
|
42
|
+
if (!isGitCheckout(sourceRoot)) throw workspaceError("git_worktree_requires_git", "workspace.kind git_worktree requires a Git checkout with a resolvable commit; use read_only_in_place, scratch or explicitly authorized in_place_write for a non-Git directory");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (kind === "read_only_in_place") {
|
|
46
|
+
if (sandbox !== "read-only" || ownedPaths.length !== 0 || networkAccess) {
|
|
47
|
+
throw workspaceError("invalid_read_only_workspace", "read_only_in_place requires sandbox read-only, owned_paths: [] and sandbox.network_access: false");
|
|
48
|
+
}
|
|
49
|
+
if (externalOutputs.length) throw workspaceError("invalid_read_only_workspace", "read_only_in_place cannot declare workspace.external_outputs");
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (kind === "scratch") {
|
|
53
|
+
if (sandbox !== "workspace-write" || ownedPaths.length !== 0) {
|
|
54
|
+
throw workspaceError("invalid_scratch_workspace", "scratch requires sandbox workspace-write and owned_paths: []; it never lands project changes");
|
|
55
|
+
}
|
|
56
|
+
if (externalOutputs.length) throw workspaceError("invalid_scratch_workspace", "scratch cannot write workspace.external_outputs; keep generated material inside the supervisor scratch directory");
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (isGitCheckout(sourceRoot)) {
|
|
60
|
+
throw workspaceError("in_place_write_requires_non_git", "in_place_write is only for an explicitly authorized non-Git directory; use git_worktree for a Git checkout");
|
|
61
|
+
}
|
|
62
|
+
if (sandbox !== "workspace-write" || ownedPaths.length === 0 || !String(inPlaceAuthorization ?? "").trim()) {
|
|
63
|
+
throw workspaceError("invalid_in_place_workspace", "in_place_write requires sandbox workspace-write, nonempty owned_paths and an explicit workspace.in_place_authorization");
|
|
64
|
+
}
|
|
65
|
+
if (!isPreflight && (!Array.isArray(acceptance) || acceptance.length === 0)) {
|
|
66
|
+
throw workspaceError("in_place_acceptance_required", "in_place_write requires explicit acceptance; file changes in a user directory cannot be accepted by worker exit status alone");
|
|
67
|
+
}
|
|
68
|
+
for (const path of ownedPaths) {
|
|
69
|
+
if (!isInside(sourceRoot, resolve(sourceRoot, path))) {
|
|
70
|
+
throw workspaceError("in_place_path_outside_root", `in_place_write ownedPath escapes the authorized source root: ${path}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function sha256File(path) {
|
|
76
|
+
const hash = createHash("sha256");
|
|
77
|
+
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
|
78
|
+
return hash.digest("hex");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function manifestEntry(root, absolute, entries) {
|
|
82
|
+
if (entries.length >= MAX_MANIFEST_ENTRIES) {
|
|
83
|
+
throw workspaceError("workspace_manifest_too_large", `workspace manifest exceeds ${MAX_MANIFEST_ENTRIES} entries; narrow the declared in-place paths before dispatching`);
|
|
84
|
+
}
|
|
85
|
+
const path = relative(root, absolute) || ".";
|
|
86
|
+
let info;
|
|
87
|
+
try {
|
|
88
|
+
info = await lstat(absolute);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error.code === "ENOENT") {
|
|
91
|
+
entries.push({ path, type: "missing" });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
if (info.isFile()) {
|
|
97
|
+
entries.push({ path, type: "file", bytes: info.size, sha256: await sha256File(absolute) });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (info.isSymbolicLink()) {
|
|
101
|
+
entries.push({ path, type: "symlink", target: await readlink(absolute) });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (info.isDirectory()) {
|
|
105
|
+
entries.push({ path, type: "directory" });
|
|
106
|
+
const children = await readdir(absolute);
|
|
107
|
+
for (const child of children.sort((left, right) => left.localeCompare(right))) {
|
|
108
|
+
await manifestEntry(root, join(absolute, child), entries);
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
entries.push({ path, type: "other" });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function captureWorkspaceManifest(sourceRoot, paths) {
|
|
116
|
+
const root = resolve(sourceRoot);
|
|
117
|
+
const entries = [];
|
|
118
|
+
for (const path of [...new Set(paths.map((path) => resolve(root, path)))].sort((left, right) => left.localeCompare(right))) {
|
|
119
|
+
await manifestEntry(root, path, entries);
|
|
120
|
+
}
|
|
121
|
+
const canonical = JSON.stringify(entries);
|
|
122
|
+
return {
|
|
123
|
+
algorithm: "sha256",
|
|
124
|
+
root,
|
|
125
|
+
paths: paths.map((path) => resolve(root, path)),
|
|
126
|
+
entry_count: entries.length,
|
|
127
|
+
sha256: createHash("sha256").update(canonical).digest("hex"),
|
|
128
|
+
entries
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function prepareWorkspace(options) {
|
|
133
|
+
const { cwd, workspaceKind = null, sandbox, ownedPaths, networkAccess, acceptance } = options;
|
|
134
|
+
const { externalOutputs = [], inPlaceAuthorization = null, taskId, isPreflight = false } = options;
|
|
135
|
+
const sourceRoot = resolve(cwd);
|
|
136
|
+
if (!existsSync(sourceRoot)) throw workspaceError("workspace_source_missing", `workspace source directory does not exist: ${sourceRoot}`);
|
|
137
|
+
const kind = selectedKind({ sourceRoot, workspaceKind, sandbox });
|
|
138
|
+
validateKind({ kind, sourceRoot, sandbox, ownedPaths, networkAccess, acceptance, externalOutputs, inPlaceAuthorization, isPreflight });
|
|
139
|
+
if (kind !== "scratch") {
|
|
140
|
+
const manifestPaths = kind === "in_place_write" ? ownedPaths : [];
|
|
141
|
+
return {
|
|
142
|
+
kind,
|
|
143
|
+
sourceRoot,
|
|
144
|
+
executionCwd: sourceRoot,
|
|
145
|
+
manifestPaths,
|
|
146
|
+
beforeManifest: manifestPaths.length ? await captureWorkspaceManifest(sourceRoot, manifestPaths) : null,
|
|
147
|
+
landable: kind === "git_worktree",
|
|
148
|
+
cleanable: kind === "git_worktree"
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const executionCwd = join(scratchDir, taskId);
|
|
152
|
+
await mkdir(scratchDir, { recursive: true });
|
|
153
|
+
await rm(executionCwd, { recursive: true, force: true });
|
|
154
|
+
await mkdir(executionCwd, { recursive: true });
|
|
155
|
+
return {
|
|
156
|
+
kind,
|
|
157
|
+
sourceRoot,
|
|
158
|
+
executionCwd,
|
|
159
|
+
manifestPaths: [],
|
|
160
|
+
beforeManifest: null,
|
|
161
|
+
landable: false,
|
|
162
|
+
cleanable: false,
|
|
163
|
+
scratchPath: executionCwd
|
|
164
|
+
};
|
|
165
|
+
}
|