mirror of
https://github.com/software-mansion/argent.git
synced 2026-09-14 19:27:14 +08:00
1a7e97fb14
Adds the executor a flow `script:` step runs on: a fresh Node child per step, a protocol over its IPC channel, and the two watchdogs that make a hung or runaway script the runner's problem rather than the host's. The step itself is not here. This branch stops at the executor and its unit tests; `feat/flow-script-step` stacks the YAML directive, the runner integration, and the reference docs on top. ## What it does - **One child per step.** Spawned with an old-space heap limit, an explicit working directory, and an environment built from an allowlist rather than copied from the tool server. - **A deadline and a lifeline.** A separate watchdog holds the deadline, so a script that wedges the event loop still dies on time; a second one reaps the whole process tree when the parent goes away, so a grandchild cannot outlive the run. - **A concurrency queue.** Slots are bounded per server. A step that never gets one is refused with a message that says so, and an aborted run frees its slot immediately. - **Log budgets.** 64 KiB per step and 256 KiB per run, counted on the bytes the report keeps: redaction and V8 frame collapsing both run before anything is counted, so what the limits bound is the size of the report rather than the size of the script's writes. - **Secret hold-back.** The scrub walks a chunk and stops where a value could still begin, so neither half of a value split across two chunks is released on its own, and a shorter value is never taken where the longer one containing it has not arrived yet. - **A failure taxonomy.** Twelve kinds, split into what the script did (it threw, it did not load, it exited non-zero, it wrote an unusable `output`) and what the host did to it (a limit, a signal, a spawn that failed, a queue slot it never got). ## Docs `packages/docs/docs/reference/configuration.mdx` lists the two configuration keys this branch adds, `scripts.maxTimeoutMs` and `scripts.heapLimitMb`. The `script:` step itself is documented with the step, on `feat/flow-script-step`. ## Verification `npm run build`, `npx eslint . --max-warnings 0`, `npx prettier --check .`, `npm run knip`, `npm run typecheck:scripts`, the test typecheck across every workspace, the tool-server suite (4719 passed, 1 skipped) and `npm run test:scripts` (92 tests) are green, as is `npx docusaurus build`. The executor's own behaviour is covered by the eight `test/flows/script/` files added here, against real child processes, and the compiled `dist/` executor was driven against its copied runner assets over every path the review reached. Each review fix carries the run that reproduced it before the change and the mutation that proves the new test fails without it: the prefix-secret leak and all three npm `node-options` routes were reproduced end to end through the executor against real child processes, and the five coverage findings were confirmed by re-applying the exact mutation each thread named. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added reliable execution of trusted flow scripts with concurrency control, cancellation, timeouts, memory limits, logging, and detailed failure reporting. * Added safeguards for process cleanup, watchdog termination, output validation, and secret redaction. * Added global configuration for maximum script runtime and memory usage. * **Bug Fixes** * Improved handling of script failures, stalled processes, malformed output, and child-process termination. * **Documentation** * Documented script resource limits and global configuration behavior. * **Chores** * Updated builds and packaging to include required flow-script runtime assets. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Hubert Gancarczyk <claude-hubert.gancarczyk@swmansion.com>
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import * as fs from "node:fs";
|
|
import * as path from "node:path";
|
|
|
|
const WORKSPACE_ROOT = path.resolve(__dirname, "../../../..");
|
|
|
|
/**
|
|
* Fixture scripts live under the workspace's own `node_modules` so a bare
|
|
* specifier resolves: Node walks up from the script file looking for
|
|
* `node_modules`, and a directory under `os.tmpdir()` has no ancestor that holds
|
|
* one. `node_modules` is git-ignored, so a fixture left behind by a crashed run
|
|
* never dirties the tree.
|
|
*/
|
|
const FIXTURE_ROOT = path.join(WORKSPACE_ROOT, "node_modules", ".cache", "argent-flow-scripts");
|
|
|
|
export interface ScriptWorkspace {
|
|
readonly dir: string;
|
|
write(name: string, source: string): string;
|
|
resolve(name: string): string;
|
|
cleanup(): void;
|
|
}
|
|
|
|
let counter = 0;
|
|
|
|
const STALE_FIXTURE_MS = 60 * 60 * 1000;
|
|
|
|
export function createScriptWorkspace(label = "ws"): ScriptWorkspace {
|
|
fs.mkdirSync(FIXTURE_ROOT, { recursive: true });
|
|
pruneStaleFixtures();
|
|
const dir = fs.mkdtempSync(path.join(FIXTURE_ROOT, `${label}-`));
|
|
return {
|
|
dir,
|
|
write(name, source) {
|
|
const file = path.join(dir, name);
|
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
fs.writeFileSync(file, source);
|
|
return file;
|
|
},
|
|
resolve(name) {
|
|
return path.join(dir, name);
|
|
},
|
|
cleanup() {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* The root is under `node_modules`, so nothing else ever sweeps it. The age
|
|
* bound is what makes this safe beside a concurrent run: an hour is far longer
|
|
* than the whole suite takes, so a fixture another vitest worker is using now
|
|
* is never old enough to be taken.
|
|
*/
|
|
function pruneStaleFixtures(): void {
|
|
const cutoff = Date.now() - STALE_FIXTURE_MS;
|
|
let entries: string[];
|
|
try {
|
|
entries = fs.readdirSync(FIXTURE_ROOT);
|
|
} catch {
|
|
return;
|
|
}
|
|
for (const entry of entries) {
|
|
const candidate = path.join(FIXTURE_ROOT, entry);
|
|
try {
|
|
if (fs.statSync(candidate).mtimeMs > cutoff) continue;
|
|
fs.rmSync(candidate, { recursive: true, force: true });
|
|
} catch {
|
|
// Raced with another worker's own cleanup — the outcome wanted.
|
|
}
|
|
}
|
|
}
|
|
|
|
export function uniqueName(prefix: string): string {
|
|
counter += 1;
|
|
return `${prefix}-${process.pid}-${counter}`;
|
|
}
|
|
|
|
export const SOURCE_RUNNER_DIR = path.resolve(__dirname, "../../src/tools/flows/script");
|