Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 2x 2x 2x 7x 7x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 1x 1x 2x 2x 2x 2x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 2x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {execFile, spawn} from 'node:child_process';
import {cp, mkdir, readFile, readdir, rm, stat, writeFile} from 'node:fs/promises';
import {dirname} from 'node:path';
import {promisify} from 'node:util';
import {
WorkspaceCommandDisabled,
WorkspaceCommandTimeout,
WorkspaceEscape,
workspaceResolve,
workspaceRootPath,
} from './workspacePath.ts';
const exec = promisify(execFile);
/**
* What a ticket's workspace is: a directory, the files in it, and the one way a command runs there.
*
* The operations are pure functions of a configuration and a request, which is what lets them be
* tested in a temporary directory without a platform (`@@D-710`). The adapter that carries them is a
* binding, not a second implementation.
*
* A command is `execFile` with an argv array and never a shell, it runs with the ticket directory as
* its cwd, it is killed with its process group when it outlives the timeout, its output is capped, and
* its environment is a configured allowlist rather than everything this process holds — because a
* build tool that reads a model key out of the environment is the failure this prevents.
*/
export interface IWorkspaceConfig {
/** Where every ticket's directory lives. */
root: string;
/** A directory copied into a fresh workspace, when one is configured. */
template?: string;
shell?: {
/** Off unless a deployment says otherwise. */
enabled?: boolean;
timeoutMs?: number;
/** Bytes of combined output kept; the rest is dropped rather than buffered. */
maxOutputBytes?: number;
/** Environment names a command is allowed to see. `PATH` is always passed. */
env?: Record<string, string>;
};
}
export interface IWorkspaceCommand {
ticketId: string;
command: string;
args?: string[];
/** A relative path inside the ticket, or the ticket root. */
path?: string;
timeoutMs?: number;
}
export interface IWorkspaceCommandResult {
exitCode: number;
stdout: string;
stderr: string;
truncated: boolean;
}
const DEFAULT_TIMEOUT_MS = 600_000;
const DEFAULT_MAX_OUTPUT = 200_000;
/** A fresh workspace: the directory, and the template's contents when one is configured. */
export const workspacePrepare = async (
config: IWorkspaceConfig,
params: {ticketId: string},
): Promise<{workspacePath: string; baseCommit?: string}> => {
const base = workspaceRootPath(config.root, params.ticketId);
await mkdir(base, {recursive: true});
if (config.template) {
const empty = (await readdir(base)).length === 0;
if (empty) await cp(config.template, base, {recursive: true});
}
return {workspacePath: base, ...(await baseCommitOf(base))};
};
/** The commit a rollback would return to, when the workspace is a repository. */
export const baseCommitOf = async (workspacePath: string): Promise<{baseCommit?: string}> => {
try {
const {stdout} = await exec('git', ['rev-parse', 'HEAD'], {cwd: workspacePath});
return {baseCommit: stdout.trim()};
} catch {
// Not a repository is an ordinary state: the template was a directory, not a checkout.
return {};
}
};
export const workspaceRead = async (
config: IWorkspaceConfig,
params: {ticketId: string; path: string},
): Promise<{content: string}> => ({
content: await readFile(workspaceResolve(config.root, params.ticketId, params.path), 'utf8'),
});
export const workspaceWrite = async (
config: IWorkspaceConfig,
params: {ticketId: string; path: string; content: string},
): Promise<{path: string; bytes: number}> => {
const full = workspaceResolve(config.root, params.ticketId, params.path);
await mkdir(dirname(full), {recursive: true});
await writeFile(full, params.content, 'utf8');
return {path: full, bytes: Buffer.byteLength(params.content)};
};
export const workspaceList = async (
config: IWorkspaceConfig,
params: {ticketId: string; path?: string},
): Promise<{
entries: Array<{name: string; kind: 'file' | 'directory' | 'other'; bytes?: number}>;
}> => {
const full = workspaceResolve(config.root, params.ticketId, params.path);
const dirents = await readdir(full, {withFileTypes: true});
const entries = await Promise.all(
dirents.map(async dirent => {
const kind = dirent.isDirectory() ? 'directory' : dirent.isFile() ? 'file' : 'other';
if (kind !== 'file') return {name: dirent.name, kind} as const;
const stats = await stat(`${full}/${dirent.name}`);
return {name: dirent.name, kind, bytes: stats.size} as const;
}),
);
return {entries};
};
/**
* One command, in the ticket directory, with no shell in between.
*
* `detached` is what makes the timeout able to kill a process *group*: a build that spawns children
* would otherwise leave them running after its parent is gone.
*/
export const workspaceRun = async (
config: IWorkspaceConfig,
params: IWorkspaceCommand,
): Promise<IWorkspaceCommandResult> => {
const shell = config.shell ?? {};
if (shell.enabled !== true) throw new WorkspaceCommandDisabled();
const cwd = workspaceResolve(config.root, params.ticketId, params.path);
const timeoutMs = params.timeoutMs ?? shell.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const maxOutput = shell.maxOutputBytes ?? DEFAULT_MAX_OUTPUT;
const env: Record<string, string> = {
...(process.env.PATH && {PATH: process.env.PATH}),
...(shell.env ?? {}),
};
return await new Promise<IWorkspaceCommandResult>((resolve, reject) => {
const child = spawn(params.command, params.args ?? [], {
cwd,
env,
detached: true,
stdio: ['ignore', 'pipe', 'pipe'],
});
let stdout = '';
let stderr = '';
let truncated = false;
const keep = (into: string, chunk: Buffer): string => {
if (into.length >= maxOutput) {
truncated = true;
return into;
}
return into + chunk.toString('utf8').slice(0, maxOutput - into.length);
};
child.stdout?.on('data', chunk => (stdout = keep(stdout, chunk as Buffer)));
child.stderr?.on('data', chunk => (stderr = keep(stderr, chunk as Buffer)));
let settled = false;
const timer = setTimeout(() => {
if (settled) return;
settled = true;
if (child.pid !== undefined) {
try {
process.kill(-child.pid, 'SIGKILL');
} catch {
child.kill('SIGKILL');
}
}
reject(
new WorkspaceCommandTimeout(`${params.command} timed out after ${timeoutMs} ms`),
);
}, timeoutMs);
timer.unref?.();
child.on('error', error => {
if (settled) return;
settled = true;
clearTimeout(timer);
reject(error);
});
child.on('close', code => {
if (settled) return;
settled = true;
clearTimeout(timer);
resolve({exitCode: code ?? -1, stdout, stderr, truncated});
});
});
};
/** Back to the state the workspace started in: tracked files restored, untracked ones removed. */
export const workspaceRollback = async (
config: IWorkspaceConfig,
params: {ticketId: string; baseCommit?: string},
): Promise<{restored: boolean; baseCommit?: string}> => {
const cwd = workspaceRootPath(config.root, params.ticketId);
try {
await exec('git', ['checkout', '--', '.'], {cwd});
await exec('git', ['clean', '-fd'], {cwd});
if (params.baseCommit) await exec('git', ['reset', '--hard', params.baseCommit], {cwd});
return {restored: true, ...(params.baseCommit && {baseCommit: params.baseCommit})};
} catch {
return {restored: false};
}
};
/** Purge a ticket's directory — what a refused recovery does (`@@D-705`). */
export const workspaceRemove = async (
config: IWorkspaceConfig,
params: {ticketId: string},
): Promise<{removed: boolean; workspacePath: string}> => {
const base = workspaceRootPath(config.root, params.ticketId);
await rm(base, {recursive: true, force: true});
return {removed: true, workspacePath: base};
};
export {WorkspaceEscape};
|