@playcraft/cli 0.1.43-beta → 0.1.44-beta
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/dist/commands/tools-batch.js +18 -0
- package/dist/workspace-runtime/batch/control.js +80 -0
- package/dist/workspace-runtime/batch/deliver.js +150 -0
- package/dist/workspace-runtime/batch/freeze.js +102 -0
- package/dist/workspace-runtime/batch/generate.js +205 -0
- package/dist/workspace-runtime/batch/git.js +40 -0
- package/dist/workspace-runtime/batch/occupancy.js +8 -0
- package/dist/workspace-runtime/batch/policy.js +70 -0
- package/dist/workspace-runtime/batch/prepare.js +202 -0
- package/dist/workspace-runtime/batch/processes.js +72 -0
- package/dist/workspace-runtime/batch/records.js +113 -0
- package/dist/workspace-runtime/batch/routes.js +58 -0
- package/dist/workspace-runtime/batch/source-capture.js +159 -0
- package/dist/workspace-runtime/codex/app-server.js +7 -7
- package/dist/workspace-runtime/codex/permissions.js +10 -1
- package/dist/workspace-runtime/production/controller.js +3 -0
- package/dist/workspace-runtime/serve.js +2 -0
- package/dist/workspace-runtime/server/connectivity.js +10 -2
- package/dist/workspace-runtime/server/dispatch.js +4 -0
- package/dist/workspace-runtime/server/http.js +6 -0
- package/package.json +3 -3
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { readFileSync, mkdirSync, writeFileSync, renameSync } from 'node:fs';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
const memory = new WeakMap();
|
|
5
|
+
/** Unattended member execution: workspace-write to the member directory and one controlled temp dir. */
|
|
6
|
+
export function memberExecutionPolicy(cwd, tempDir) {
|
|
7
|
+
return {
|
|
8
|
+
cwd,
|
|
9
|
+
approvalPolicy: 'never',
|
|
10
|
+
sandboxPolicy: {
|
|
11
|
+
type: 'workspaceWrite',
|
|
12
|
+
writableRoots: [cwd, tempDir],
|
|
13
|
+
networkAccess: true,
|
|
14
|
+
excludeTmpdirEnvVar: true,
|
|
15
|
+
excludeSlashTmp: true,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function policyPath(state, sessionId) {
|
|
20
|
+
return join(state.config.stateDir, 'batch-sessions', `${sessionId.replace(/[^a-zA-Z0-9._-]/g, '_')}.json`);
|
|
21
|
+
}
|
|
22
|
+
function parsePolicy(raw) {
|
|
23
|
+
const value = JSON.parse(raw);
|
|
24
|
+
if (value?.approvalPolicy !== 'never' || value.sandboxPolicy?.type !== 'workspaceWrite')
|
|
25
|
+
return undefined;
|
|
26
|
+
if (!value.cwd || !Array.isArray(value.sandboxPolicy.writableRoots) || value.sandboxPolicy.writableRoots.length === 0)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (!value.sandboxPolicy.excludeSlashTmp || !value.sandboxPolicy.excludeTmpdirEnvVar)
|
|
29
|
+
return undefined;
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
export async function bindMemberPolicy(state, sessionId, policy) {
|
|
33
|
+
const path = policyPath(state, sessionId);
|
|
34
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
35
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
36
|
+
writeFileSync(temporary, JSON.stringify(policy), { mode: 0o600 });
|
|
37
|
+
renameSync(temporary, path);
|
|
38
|
+
const map = memory.get(state) ?? new Map();
|
|
39
|
+
map.set(sessionId, policy);
|
|
40
|
+
memory.set(state, map);
|
|
41
|
+
}
|
|
42
|
+
/** Synchronous: the shared session adapter reapplies this on every thread start and resume. */
|
|
43
|
+
export function trustedMemberPolicy(state, sessionId) {
|
|
44
|
+
const cached = memory.get(state)?.get(sessionId);
|
|
45
|
+
if (cached)
|
|
46
|
+
return cached;
|
|
47
|
+
try {
|
|
48
|
+
const parsed = parsePolicy(readFileSync(policyPath(state, sessionId), 'utf8'));
|
|
49
|
+
if (!parsed) {
|
|
50
|
+
return {
|
|
51
|
+
cwd: state.config.workspaceRoot,
|
|
52
|
+
approvalPolicy: 'never',
|
|
53
|
+
sandboxPolicy: { type: 'readOnly', networkAccess: false },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const map = memory.get(state) ?? new Map();
|
|
57
|
+
map.set(sessionId, parsed);
|
|
58
|
+
memory.set(state, map);
|
|
59
|
+
return parsed;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code === 'ENOENT')
|
|
63
|
+
return undefined;
|
|
64
|
+
return {
|
|
65
|
+
cwd: state.config.workspaceRoot,
|
|
66
|
+
approvalPolicy: 'never',
|
|
67
|
+
sandboxPolicy: { type: 'readOnly', networkAccess: false },
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { lstat, mkdir, readFile, rm } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { sandboxBatchMemberPrepareSchema, } from '@playcraft/common/cloud-workspace/runtime';
|
|
6
|
+
import { registerWorkspaceDirectory } from '../workspaces/directories.js';
|
|
7
|
+
import { workspaceContainerRoot } from '../workspaces/registry.js';
|
|
8
|
+
import { copyCredentialHelpers, gitRead, gitWrite } from './git.js';
|
|
9
|
+
import { digestFiles, readSourceFiles, restoreArchive } from './source-capture.js';
|
|
10
|
+
import { groupExited, runProcessGroup } from './processes.js';
|
|
11
|
+
import { filesystemObjectStore, memberStatus, readMember, readSnapshot, readStopMarker, serializeRun, sourceCacheDir, writeMember, } from './records.js';
|
|
12
|
+
const BRANCH = /^remix\/draft-[a-f0-9]{16}$/;
|
|
13
|
+
const LOCKS = ['pnpm-lock.yaml', 'package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock', 'bun.lock', 'bun.lockb'];
|
|
14
|
+
export function frozenInstallCommand() {
|
|
15
|
+
return process.env.PLAYCRAFT_BATCH_INSTALL_COMMAND
|
|
16
|
+
?? 'pnpm install --frozen-lockfile --store-dir /var/cache/playcraft/pnpm-store --ignore-scripts';
|
|
17
|
+
}
|
|
18
|
+
function hashCommand(value) {
|
|
19
|
+
return createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
20
|
+
}
|
|
21
|
+
async function materialize(state, snapshot, destination, store, sourceRoot) {
|
|
22
|
+
const cache = sourceCacheDir(state, snapshot.batchId);
|
|
23
|
+
const cachedDigest = await readFile(`${cache}.digest`, 'utf8').catch(() => '');
|
|
24
|
+
if (cachedDigest.trim() === snapshot.digest && await lstat(join(cache, '.git')).catch(() => undefined)) {
|
|
25
|
+
await gitWrite(dirnameOf(destination), ['clone', '--local', '--no-hardlinks', '--', cache, destination]);
|
|
26
|
+
if (digestFiles(await readSourceFiles(destination)) !== snapshot.digest)
|
|
27
|
+
throw new Error('batch_snapshot_mismatch');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (snapshot.remoteRef) {
|
|
31
|
+
const binding = state.journal.getWorkspace(snapshot.sourceWorkspaceId);
|
|
32
|
+
if (!binding?.repositoryUrl)
|
|
33
|
+
throw new Error('batch_snapshot_mismatch');
|
|
34
|
+
await gitWrite(dirnameOf(destination), ['init', '--initial-branch', 'source', destination]);
|
|
35
|
+
await copyCredentialHelpers(sourceRoot, destination);
|
|
36
|
+
await gitWrite(destination, ['remote', 'add', 'origin', binding.repositoryUrl]);
|
|
37
|
+
try {
|
|
38
|
+
await gitWrite(destination, ['fetch', '--no-tags', 'origin', snapshot.remoteRef]);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new Error('batch_snapshot_mismatch');
|
|
42
|
+
}
|
|
43
|
+
await gitWrite(destination, ['checkout', '--detach', 'FETCH_HEAD']);
|
|
44
|
+
if ((await gitRead(destination, ['rev-parse', 'HEAD'])).trim() !== snapshot.commitHash)
|
|
45
|
+
throw new Error('batch_snapshot_mismatch');
|
|
46
|
+
if (digestFiles(await readSourceFiles(destination)) !== snapshot.digest)
|
|
47
|
+
throw new Error('batch_snapshot_mismatch');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (!snapshot.artifactKey)
|
|
51
|
+
throw new Error('batch_snapshot_mismatch');
|
|
52
|
+
const body = await store.get(snapshot.artifactKey).catch(() => { throw new Error('batch_snapshot_mismatch'); });
|
|
53
|
+
await restoreArchive(body, destination, snapshot.digest);
|
|
54
|
+
}
|
|
55
|
+
function dirnameOf(path) {
|
|
56
|
+
return join(path, '..');
|
|
57
|
+
}
|
|
58
|
+
async function checkoutBranch(root, branchName, commit, repositoryUrl) {
|
|
59
|
+
await gitWrite(root, ['checkout', '-B', branchName, commit]);
|
|
60
|
+
const origin = await gitRead(root, ['remote', 'get-url', 'origin']).catch(() => '');
|
|
61
|
+
if (!origin.trim())
|
|
62
|
+
await gitWrite(root, ['remote', 'add', 'origin', repositoryUrl]);
|
|
63
|
+
else if (origin.trim() !== repositoryUrl)
|
|
64
|
+
await gitWrite(root, ['remote', 'set-url', 'origin', repositoryUrl]);
|
|
65
|
+
await gitWrite(root, ['config', 'user.name', 'PlayCraft']);
|
|
66
|
+
await gitWrite(root, ['config', 'user.email', 'playcraft@localhost']);
|
|
67
|
+
}
|
|
68
|
+
async function resumeBranch(repositoryUrl, branchName, commit, destination, sourceRoot) {
|
|
69
|
+
await gitWrite(dirnameOf(destination), ['clone', '--branch', branchName, '--single-branch', '--', repositoryUrl, destination]);
|
|
70
|
+
await copyCredentialHelpers(sourceRoot, destination);
|
|
71
|
+
if ((await gitRead(destination, ['rev-parse', 'HEAD'])).trim() !== commit && !(await gitRead(destination, ['cat-file', '-e', `${commit}^{commit}`]).then(() => true, () => false))) {
|
|
72
|
+
throw new Error('batch_commit_mismatch');
|
|
73
|
+
}
|
|
74
|
+
await gitWrite(destination, ['checkout', '-B', branchName, commit]);
|
|
75
|
+
}
|
|
76
|
+
export async function assertMemberDependencies(root) {
|
|
77
|
+
let manifest;
|
|
78
|
+
try {
|
|
79
|
+
manifest = await readFile(join(root, 'package.json'), 'utf8');
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
if (error.code === 'ENOENT')
|
|
83
|
+
return 'skip';
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
let names = [];
|
|
87
|
+
try {
|
|
88
|
+
const pkg = JSON.parse(manifest);
|
|
89
|
+
names = [...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {})];
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new Error('dependency_lock_mismatch');
|
|
93
|
+
}
|
|
94
|
+
const present = [];
|
|
95
|
+
for (const lock of LOCKS)
|
|
96
|
+
if (await lstat(join(root, lock)).catch(() => undefined))
|
|
97
|
+
present.push(lock);
|
|
98
|
+
if (present.length > 1)
|
|
99
|
+
throw new Error('dependency_locks_conflict');
|
|
100
|
+
if (present.length === 0)
|
|
101
|
+
return 'skip';
|
|
102
|
+
if (present[0] !== 'pnpm-lock.yaml')
|
|
103
|
+
throw new Error('dependency_lock_mismatch');
|
|
104
|
+
const lock = await readFile(join(root, 'pnpm-lock.yaml'), 'utf8');
|
|
105
|
+
if (names.some(name => !lock.includes(`${name}:`) && !lock.includes(`/${name}@`) && !lock.includes(`'${name}'`) && !lock.includes(`"${name}"`))) {
|
|
106
|
+
throw new Error('dependency_lock_mismatch');
|
|
107
|
+
}
|
|
108
|
+
return 'install';
|
|
109
|
+
}
|
|
110
|
+
export async function prepareBatchMember(state, body, options) {
|
|
111
|
+
if (!body || typeof body !== 'object' || !('occupancyId' in body) || !body.occupancyId)
|
|
112
|
+
throw new Error('occupancy_required');
|
|
113
|
+
const parsed = sandboxBatchMemberPrepareSchema.safeParse(body);
|
|
114
|
+
if (!parsed.success)
|
|
115
|
+
throw new Error(parsed.error.issues.some(issue => issue.path.includes('branchName')) ? 'batch_branch_invalid' : 'invalid_batch_request');
|
|
116
|
+
const command = parsed.data;
|
|
117
|
+
if (!BRANCH.test(command.branchName))
|
|
118
|
+
throw new Error('batch_branch_invalid');
|
|
119
|
+
const hash = hashCommand(command);
|
|
120
|
+
return serializeRun(command.runId, async () => {
|
|
121
|
+
const marker = await readStopMarker(state, command.runId);
|
|
122
|
+
const existing = await readMember(state, command.runId);
|
|
123
|
+
if (existing && command.ownerFence < existing.ownerFence)
|
|
124
|
+
throw new Error('stale_owner');
|
|
125
|
+
if (marker)
|
|
126
|
+
return memberStatus(await sealStopped(state, command, existing));
|
|
127
|
+
if (existing && existing.ownerFence === command.ownerFence && existing.commandHash?.prepare === hash && existing.state !== 'running' && existing.state !== 'failed') {
|
|
128
|
+
return memberStatus(existing);
|
|
129
|
+
}
|
|
130
|
+
if (existing && existing.ownerFence === command.ownerFence && existing.commandHash?.prepare && existing.commandHash.prepare !== hash)
|
|
131
|
+
throw new Error('batch_command_conflict');
|
|
132
|
+
if (existing?.processes.some(process => process.role === 'install' && !process.stopped && !groupExited(process.pid)))
|
|
133
|
+
return memberStatus(existing);
|
|
134
|
+
const snapshot = command.snapshot;
|
|
135
|
+
if ((await readSnapshot(state, snapshot.batchId))?.digest === snapshot.digest) {
|
|
136
|
+
/* The local snapshot record is an accelerator; the command snapshot remains authoritative. */
|
|
137
|
+
}
|
|
138
|
+
let record = await writeMember(state, {
|
|
139
|
+
runId: command.runId, ownerFence: command.ownerFence, occupancyId: command.occupancyId,
|
|
140
|
+
workspaceId: command.workspaceId, directoryName: command.directoryName, phase: 'prepare', state: 'running',
|
|
141
|
+
writersStopped: false, holding: true, stopRequested: false, processes: existing?.processes ?? [],
|
|
142
|
+
commandHash: { ...existing?.commandHash, prepare: hash }, snapshotDigest: snapshot.digest, branchName: command.branchName,
|
|
143
|
+
updatedAt: new Date().toISOString(),
|
|
144
|
+
});
|
|
145
|
+
const parent = await workspaceContainerRoot(state, command.workspaceId);
|
|
146
|
+
const root = join(parent, command.directoryName);
|
|
147
|
+
const staging = join(parent, `.batch-member-${randomUUID()}`);
|
|
148
|
+
const binding = state.journal.getWorkspace(command.workspaceId);
|
|
149
|
+
if (!binding?.repositoryUrl)
|
|
150
|
+
throw new Error('workspace_not_registered');
|
|
151
|
+
const sourceRoot = join(parent, snapshot.sourceDirectoryName);
|
|
152
|
+
try {
|
|
153
|
+
if (!await lstat(root).catch(() => undefined)) {
|
|
154
|
+
await mkdir(parent, { recursive: true });
|
|
155
|
+
if (command.resumeFromCommit)
|
|
156
|
+
await resumeBranch(binding.repositoryUrl, command.branchName, command.resumeFromCommit, staging, sourceRoot);
|
|
157
|
+
else
|
|
158
|
+
await materialize(state, snapshot, staging, options?.objectStore ?? filesystemObjectStore(state), sourceRoot);
|
|
159
|
+
const commit = command.resumeFromCommit ?? snapshot.commitHash;
|
|
160
|
+
await checkoutBranch(staging, command.branchName, commit, binding.repositoryUrl);
|
|
161
|
+
const { rename } = await import('node:fs/promises');
|
|
162
|
+
await rename(staging, root);
|
|
163
|
+
}
|
|
164
|
+
await registerWorkspaceDirectory(state, {
|
|
165
|
+
workspaceId: command.workspaceId, directoryName: command.directoryName, remixId: command.remixId,
|
|
166
|
+
branchName: command.branchName, sourceCommit: command.resumeFromCommit ?? snapshot.commitHash,
|
|
167
|
+
}, state.acceptedLeaseFence);
|
|
168
|
+
const plan = await assertMemberDependencies(root);
|
|
169
|
+
if (plan === 'install') {
|
|
170
|
+
const installed = await runProcessGroup(root, frozenInstallCommand(), 'install', async (process) => {
|
|
171
|
+
record = await writeMember(state, { ...record, processes: [...record.processes, process] });
|
|
172
|
+
});
|
|
173
|
+
record = await writeMember(state, { ...record, processes: record.processes.map(process => process.pid === installed.process.pid ? { ...installed.process, role: 'install' } : process) });
|
|
174
|
+
if (await readStopMarker(state, command.runId))
|
|
175
|
+
return memberStatus(await sealStopped(state, command, record));
|
|
176
|
+
if (installed.code !== 0)
|
|
177
|
+
throw new Error('batch_install_failed');
|
|
178
|
+
}
|
|
179
|
+
const head = (await gitRead(root, ['rev-parse', 'HEAD'])).trim();
|
|
180
|
+
record = await writeMember(state, { ...record, state: 'completed', writersStopped: true, holding: true, headCommit: head, error: undefined });
|
|
181
|
+
return memberStatus(record);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
const code = error instanceof Error && /^[a-z0-9_]+$/.test(error.message) ? error.message : 'batch_prepare_failed';
|
|
185
|
+
record = await writeMember(state, { ...record, state: 'failed', writersStopped: true, holding: false, error: code });
|
|
186
|
+
return memberStatus(record);
|
|
187
|
+
}
|
|
188
|
+
finally {
|
|
189
|
+
await rm(staging, { recursive: true, force: true });
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
async function sealStopped(state, command, existing) {
|
|
194
|
+
return writeMember(state, {
|
|
195
|
+
runId: command.runId, ownerFence: Math.max(command.ownerFence, existing?.ownerFence ?? 0), occupancyId: command.occupancyId,
|
|
196
|
+
workspaceId: command.workspaceId, directoryName: command.directoryName, phase: existing?.phase ?? 'prepare',
|
|
197
|
+
state: 'stopped', writersStopped: true, holding: false, stopRequested: true, processes: existing?.processes ?? [],
|
|
198
|
+
sessionId: existing?.sessionId, operationId: existing?.operationId, headCommit: existing?.headCommit, evidence: existing?.evidence,
|
|
199
|
+
commandHash: existing?.commandHash, snapshotDigest: existing?.snapshotDigest, branchName: existing?.branchName,
|
|
200
|
+
updatedAt: new Date().toISOString(),
|
|
201
|
+
});
|
|
202
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { execFile, spawn } from 'node:child_process';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
+
export async function processBirth(pid) {
|
|
6
|
+
if (process.platform !== 'linux') {
|
|
7
|
+
return promisify(execFile)('ps', ['-p', String(pid), '-o', 'lstart='], { timeout: 2000 }).then(value => value.stdout.trim() || undefined, () => undefined);
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const stat = await readFile(`/proc/${pid}/stat`, 'utf8');
|
|
11
|
+
return stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19];
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export function groupExited(pid) {
|
|
18
|
+
try {
|
|
19
|
+
process.kill(-pid, 0);
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
return error.code === 'ESRCH';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Stops one run's process group. A mismatched birth means the pid was reused, which is not proof
|
|
28
|
+
* that the original writers exited, so the caller must not report writersStopped.
|
|
29
|
+
*/
|
|
30
|
+
export async function stopProcessGroup(record) {
|
|
31
|
+
if (record.stopped || groupExited(record.pid))
|
|
32
|
+
return true;
|
|
33
|
+
if (record.birth && await processBirth(record.pid) !== record.birth)
|
|
34
|
+
return false;
|
|
35
|
+
for (const signal of ['SIGTERM', 'SIGKILL']) {
|
|
36
|
+
try {
|
|
37
|
+
process.kill(-record.pid, signal);
|
|
38
|
+
}
|
|
39
|
+
catch { /* Recheck the group. */ }
|
|
40
|
+
const deadline = Date.now() + 2000;
|
|
41
|
+
while (Date.now() < deadline) {
|
|
42
|
+
if (groupExited(record.pid))
|
|
43
|
+
return true;
|
|
44
|
+
await delay(25);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return groupExited(record.pid);
|
|
48
|
+
}
|
|
49
|
+
export async function runProcessGroup(cwd, command, role, onStart) {
|
|
50
|
+
const child = spawn('bash', ['-lc', command], {
|
|
51
|
+
cwd, detached: true, stdio: ['ignore', 'pipe', 'pipe'],
|
|
52
|
+
env: { PATH: process.env.PATH, HOME: cwd, GIT_TERMINAL_PROMPT: '0' },
|
|
53
|
+
});
|
|
54
|
+
const pid = child.pid;
|
|
55
|
+
if (!pid)
|
|
56
|
+
throw new Error('batch_process_not_started');
|
|
57
|
+
let output = '';
|
|
58
|
+
const collect = (chunk) => { output = (output + String(chunk)).slice(-64 * 1024); };
|
|
59
|
+
child.stdout?.on('data', collect);
|
|
60
|
+
child.stderr?.on('data', collect);
|
|
61
|
+
// Subscribe before any await. A short command can exit during processBirth or onStart,
|
|
62
|
+
// and a late listener never sees that close event.
|
|
63
|
+
const finished = new Promise((resolve) => {
|
|
64
|
+
child.once('error', () => resolve(null));
|
|
65
|
+
child.once('close', resolve);
|
|
66
|
+
});
|
|
67
|
+
const tracked = { pid, birth: await processBirth(pid), stopped: false, role };
|
|
68
|
+
await onStart(tracked);
|
|
69
|
+
const code = await finished;
|
|
70
|
+
tracked.stopped = groupExited(pid);
|
|
71
|
+
return { code, output, process: tracked };
|
|
72
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, readdir, rename, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { batchSourceSnapshotRefSchema, sandboxBatchMemberStatusSchema, } from '@playcraft/common/cloud-workspace/runtime';
|
|
5
|
+
const runs = new Map();
|
|
6
|
+
let freezeChain = Promise.resolve();
|
|
7
|
+
export function serializeFreeze(action) {
|
|
8
|
+
const run = freezeChain.then(action, action);
|
|
9
|
+
freezeChain = run.then(() => undefined, () => undefined);
|
|
10
|
+
return run;
|
|
11
|
+
}
|
|
12
|
+
export function serializeRun(runId, action) {
|
|
13
|
+
const previous = runs.get(runId) ?? Promise.resolve();
|
|
14
|
+
const run = previous.then(action, action);
|
|
15
|
+
runs.set(runId, run.then(() => undefined, () => undefined));
|
|
16
|
+
return run;
|
|
17
|
+
}
|
|
18
|
+
export function snapshotFile(state, batchId) {
|
|
19
|
+
return join(state.config.stateDir, 'batch-sources', `${batchId}.json`);
|
|
20
|
+
}
|
|
21
|
+
export function memberFile(state, runId) {
|
|
22
|
+
return join(state.config.stateDir, 'batch-members', `${runId}.json`);
|
|
23
|
+
}
|
|
24
|
+
export function stopMarkerFile(state, runId) {
|
|
25
|
+
return join(state.config.stateDir, 'batch-stops', `${runId}.json`);
|
|
26
|
+
}
|
|
27
|
+
export function sourceCacheDir(state, batchId) {
|
|
28
|
+
return join(state.config.stateDir, 'batch-source-cache', batchId);
|
|
29
|
+
}
|
|
30
|
+
export function objectFile(state, key) {
|
|
31
|
+
if (key.split('/').some(part => !part || part === '.' || part === '..'))
|
|
32
|
+
throw new Error('batch_object_key_invalid');
|
|
33
|
+
return join(state.config.stateDir, 'batch-objects', key);
|
|
34
|
+
}
|
|
35
|
+
async function writeAtomic(path, body) {
|
|
36
|
+
await mkdir(join(path, '..'), { recursive: true, mode: 0o700 });
|
|
37
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
38
|
+
await writeFile(temporary, body, { mode: 0o600 });
|
|
39
|
+
await rename(temporary, path);
|
|
40
|
+
}
|
|
41
|
+
async function readJson(path) {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error.code === 'ENOENT')
|
|
47
|
+
return undefined;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export async function readSnapshot(state, batchId) {
|
|
52
|
+
const value = await readJson(snapshotFile(state, batchId));
|
|
53
|
+
return value ? batchSourceSnapshotRefSchema.parse(value) : undefined;
|
|
54
|
+
}
|
|
55
|
+
export async function writeSnapshot(state, snapshot) {
|
|
56
|
+
const parsed = batchSourceSnapshotRefSchema.parse(snapshot);
|
|
57
|
+
await writeAtomic(snapshotFile(state, parsed.batchId), JSON.stringify(parsed));
|
|
58
|
+
}
|
|
59
|
+
export async function readMember(state, runId) {
|
|
60
|
+
return readJson(memberFile(state, runId));
|
|
61
|
+
}
|
|
62
|
+
export async function writeMember(state, record) {
|
|
63
|
+
const next = { ...record, updatedAt: new Date().toISOString() };
|
|
64
|
+
await writeAtomic(memberFile(state, record.runId), JSON.stringify(next));
|
|
65
|
+
return next;
|
|
66
|
+
}
|
|
67
|
+
export async function listMembers(state) {
|
|
68
|
+
const directory = join(state.config.stateDir, 'batch-members');
|
|
69
|
+
const names = await readdir(directory).catch(error => {
|
|
70
|
+
if (error.code === 'ENOENT')
|
|
71
|
+
return [];
|
|
72
|
+
throw error;
|
|
73
|
+
});
|
|
74
|
+
const records = [];
|
|
75
|
+
for (const name of names) {
|
|
76
|
+
if (!name.endsWith('.json'))
|
|
77
|
+
continue;
|
|
78
|
+
const record = await readJson(join(directory, name));
|
|
79
|
+
if (record)
|
|
80
|
+
records.push(record);
|
|
81
|
+
}
|
|
82
|
+
return records;
|
|
83
|
+
}
|
|
84
|
+
export async function readStopMarker(state, runId) {
|
|
85
|
+
return readJson(stopMarkerFile(state, runId));
|
|
86
|
+
}
|
|
87
|
+
export async function writeStopMarker(state, runId, ownerFence) {
|
|
88
|
+
const path = stopMarkerFile(state, runId);
|
|
89
|
+
if (await readStopMarker(state, runId))
|
|
90
|
+
return;
|
|
91
|
+
await writeAtomic(path, JSON.stringify({ runId, ownerFence, at: new Date().toISOString() }));
|
|
92
|
+
}
|
|
93
|
+
export function memberStatus(record) {
|
|
94
|
+
return sandboxBatchMemberStatusSchema.parse({
|
|
95
|
+
runId: record.runId,
|
|
96
|
+
ownerFence: record.ownerFence,
|
|
97
|
+
phase: record.phase,
|
|
98
|
+
state: record.state,
|
|
99
|
+
writersStopped: record.writersStopped,
|
|
100
|
+
...(record.sessionId ? { sessionId: record.sessionId } : {}),
|
|
101
|
+
...(record.operationId ? { operationId: record.operationId } : {}),
|
|
102
|
+
...(record.headCommit ? { headCommit: record.headCommit } : {}),
|
|
103
|
+
...(record.evidence ? { evidence: record.evidence } : {}),
|
|
104
|
+
...(record.error ? { error: record.error.slice(0, 128) } : {}),
|
|
105
|
+
updatedAt: record.updatedAt,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
export function filesystemObjectStore(state) {
|
|
109
|
+
return {
|
|
110
|
+
async put(key, body) { await writeAtomic(objectFile(state, key), body); },
|
|
111
|
+
async get(key) { return readFile(objectFile(state, key)); },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { batchSourceFreezeResultSchema } from '@playcraft/common/cloud-workspace/runtime';
|
|
2
|
+
import { stopBatchMember, readBatchMemberStatus } from './control.js';
|
|
3
|
+
import { deliverBatchMember } from './deliver.js';
|
|
4
|
+
import { freezeBatchSource } from './freeze.js';
|
|
5
|
+
import { generateBatchMember } from './generate.js';
|
|
6
|
+
import { prepareBatchMember } from './prepare.js';
|
|
7
|
+
const CLIENT_ERRORS = new Set([
|
|
8
|
+
'occupancy_required', 'invalid_batch_request', 'batch_branch_invalid', 'stale_owner', 'disk_admission_paused',
|
|
9
|
+
'batch_source_push_failed', 'batch_source_upload_failed', 'batch_source_changed', 'batch_source_path_invalid',
|
|
10
|
+
'batch_command_conflict', 'batch_snapshot_mismatch', 'batch_commit_mismatch', 'workspace_not_registered',
|
|
11
|
+
'disk_usage_unavailable', 'batch_object_key_invalid',
|
|
12
|
+
]);
|
|
13
|
+
async function readBody(request) {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
let size = 0;
|
|
16
|
+
for await (const chunk of request) {
|
|
17
|
+
size += chunk.length;
|
|
18
|
+
if (size > 1024 * 1024)
|
|
19
|
+
throw new Error('request_body_too_large');
|
|
20
|
+
chunks.push(Buffer.from(chunk));
|
|
21
|
+
}
|
|
22
|
+
if (!size)
|
|
23
|
+
return {};
|
|
24
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
25
|
+
}
|
|
26
|
+
export async function dispatchBatchRoute(state, request, url) {
|
|
27
|
+
const method = request.method ?? 'GET';
|
|
28
|
+
const path = url.pathname;
|
|
29
|
+
const member = /^\/runtime\/batch-members\/([^/]+)(?:\/(stop))?$/.exec(path);
|
|
30
|
+
if (method === 'GET' && member && !member[2]) {
|
|
31
|
+
const status = await readBatchMemberStatus(state, decodeURIComponent(member[1]));
|
|
32
|
+
return status ? { status: 200, body: status } : { status: 404, body: { error: 'batch_member_not_found' } };
|
|
33
|
+
}
|
|
34
|
+
if (method !== 'POST')
|
|
35
|
+
return undefined;
|
|
36
|
+
try {
|
|
37
|
+
if (path === '/runtime/batch-sources/freeze') {
|
|
38
|
+
const result = batchSourceFreezeResultSchema.parse(await freezeBatchSource(state, await readBody(request)));
|
|
39
|
+
return { status: 200, body: result };
|
|
40
|
+
}
|
|
41
|
+
if (path === '/runtime/batch-members/prepare')
|
|
42
|
+
return { status: 200, body: await prepareBatchMember(state, await readBody(request)) };
|
|
43
|
+
if (path === '/runtime/batch-members/generate')
|
|
44
|
+
return { status: 200, body: await generateBatchMember(state, await readBody(request)) };
|
|
45
|
+
if (path === '/runtime/batch-members/deliver')
|
|
46
|
+
return { status: 200, body: await deliverBatchMember(state, await readBody(request)) };
|
|
47
|
+
if (member?.[2] === 'stop')
|
|
48
|
+
return { status: 200, body: await stopBatchMember(state, decodeURIComponent(member[1]), await readBody(request)) };
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const code = error instanceof Error && CLIENT_ERRORS.has(error.message) ? error.message : undefined;
|
|
53
|
+
if (!code)
|
|
54
|
+
throw error;
|
|
55
|
+
const status = code === 'occupancy_required' || code === 'invalid_batch_request' || code === 'batch_branch_invalid' ? 400 : 409;
|
|
56
|
+
return { status, body: { error: code } };
|
|
57
|
+
}
|
|
58
|
+
}
|