agentic-workflow-manager 3.5.0 → 3.6.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.
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runCommand = runCommand;
4
+ const child_process_1 = require("child_process");
5
+ const DEFAULT_MAX_BUFFER = 64 * 1024 * 1024;
6
+ const DEFAULT_KILL_GRACE_MS = 2_000;
7
+ /** After SIGKILL, resolve regardless. A sensor must never hang the gate. */
8
+ const POST_KILL_GRACE_MS = 1_000;
9
+ /**
10
+ * Kill an entire process tree, not just its root.
11
+ *
12
+ * This is the reason `execSync` had to go. `execSync(cmd, { timeout })` spawns
13
+ * `/bin/sh -c cmd` and, on the deadline, SIGTERMs *that shell only*. A sensor
14
+ * command is almost always a wrapper (`npx tsc --noEmit`, `npm test`), so the
15
+ * tool doing the actual work is a grandchild: it survives, gets reparented to
16
+ * init, and keeps burning CPU. Every timeout then leaves a full tsc/eslint
17
+ * running, which makes the next run slower, which makes it time out too. The
18
+ * leak compounds — a repo that was fine at 200 files becomes ungateable at 2000
19
+ * for reasons that have nothing to do with its size.
20
+ *
21
+ * `detached: true` puts the child in its own process group (pgid === pid) so a
22
+ * negative-pid kill reaches every descendant at once.
23
+ */
24
+ function killTree(pid, signal) {
25
+ if (process.platform === 'win32') {
26
+ // Windows has no process groups in the POSIX sense; taskkill /T walks the tree.
27
+ try {
28
+ (0, child_process_1.execFile)('taskkill', ['/pid', String(pid), '/T', '/F'], () => { });
29
+ }
30
+ catch { /* ignore */ }
31
+ return;
32
+ }
33
+ try {
34
+ process.kill(-pid, signal); // negative pid → the whole group
35
+ }
36
+ catch {
37
+ // Group already gone (normal race with a process exiting on its own),
38
+ // or we never became a group leader. Fall back to the direct child.
39
+ try {
40
+ process.kill(pid, signal);
41
+ }
42
+ catch { /* already dead */ }
43
+ }
44
+ }
45
+ /**
46
+ * Run a shell command to completion, a deadline, or an output cap — whichever
47
+ * comes first — and always return what was collected.
48
+ *
49
+ * Two guarantees `execSync` could not give:
50
+ * 1. Cutting a run short kills the whole process tree (see `killTree`).
51
+ * 2. Output produced before the cut is returned, not discarded. A timeout that
52
+ * throws away 60s of eslint output costs double: the wall clock, and then
53
+ * the re-run the caller has to do to learn anything at all.
54
+ */
55
+ function runCommand(cmd, opts) {
56
+ const maxBuffer = opts.maxBuffer ?? DEFAULT_MAX_BUFFER;
57
+ const killGraceMs = opts.killGraceMs ?? DEFAULT_KILL_GRACE_MS;
58
+ return new Promise((resolve) => {
59
+ let stdout = '';
60
+ let stderr = '';
61
+ let timedOut = false;
62
+ let overflowed = false;
63
+ let settled = false;
64
+ const timers = [];
65
+ const child = (0, child_process_1.spawn)(cmd, {
66
+ shell: true,
67
+ cwd: opts.cwd,
68
+ detached: process.platform !== 'win32',
69
+ // stdin closed: a sensor must never block waiting for input, and the
70
+ // EOF also tells watch-mode-capable tools (vitest, jest) to run once.
71
+ stdio: ['ignore', 'pipe', 'pipe'],
72
+ });
73
+ const later = (fn, ms) => {
74
+ const t = setTimeout(fn, ms);
75
+ t.unref?.();
76
+ timers.push(t);
77
+ return t;
78
+ };
79
+ const finish = (extra) => {
80
+ if (settled)
81
+ return;
82
+ settled = true;
83
+ timers.forEach(clearTimeout);
84
+ resolve({ stdout, stderr, code: null, signal: null, timedOut, overflowed, ...extra });
85
+ };
86
+ /** Cut the run short: kill the tree, escalate, and never hang waiting for it. */
87
+ const cutShort = () => {
88
+ if (settled || child.pid === undefined)
89
+ return;
90
+ const pid = child.pid;
91
+ killTree(pid, 'SIGTERM');
92
+ later(() => killTree(pid, 'SIGKILL'), killGraceMs);
93
+ // If `close` still has not fired after the escalation, stop waiting.
94
+ // Whatever is holding the pipe open is no longer our problem to block on.
95
+ later(() => { child.unref(); finish({ signal: 'SIGKILL' }); }, killGraceMs + POST_KILL_GRACE_MS);
96
+ };
97
+ const collect = (into) => (chunk) => {
98
+ if (settled || overflowed)
99
+ return;
100
+ const text = String(chunk);
101
+ const current = into === 'out' ? stdout : stderr;
102
+ const room = maxBuffer - current.length;
103
+ const next = current + (text.length > room ? text.slice(0, room) : text);
104
+ if (into === 'out')
105
+ stdout = next;
106
+ else
107
+ stderr = next;
108
+ if (next.length >= maxBuffer) {
109
+ overflowed = true;
110
+ cutShort();
111
+ }
112
+ };
113
+ child.stdout?.on('data', collect('out'));
114
+ child.stderr?.on('data', collect('err'));
115
+ child.stdout?.on('error', () => { });
116
+ child.stderr?.on('error', () => { });
117
+ child.on('error', (err) => finish({ spawnError: err }));
118
+ child.on('close', (code, signal) => finish({ code, signal }));
119
+ later(() => { timedOut = true; cutShort(); }, opts.timeout);
120
+ });
121
+ }
@@ -27,8 +27,8 @@ function registerSensorsCommand(program) {
27
27
  .option('--fast', 'run fast sensors only (tsc, lint)')
28
28
  .option('--slow', 'run slow sensors only (semgrep, mutation)')
29
29
  .option('--all', 'run all sensors regardless of speed')
30
- .action((opts) => {
31
- const output = (0, run_1.runSensors)({ fast: opts.fast, slow: opts.slow, all: opts.all });
30
+ .action(async (opts) => {
31
+ const output = await (0, run_1.runSensors)({ fast: opts.fast, slow: opts.slow, all: opts.all });
32
32
  // Emit the verdict ALWAYS — an empty `sensors` with overall:'not_certified'
33
33
  // must be visible, never a silent exit-0 that reads as "clean".
34
34
  process.stdout.write(JSON.stringify(output, null, 2) + '\n');
@@ -51,9 +51,9 @@ function registerSensorsCommand(program) {
51
51
  sensors
52
52
  .command('baseline')
53
53
  .description('snapshot current findings as accepted — sensors then fail only on NEW ones')
54
- .action(() => {
54
+ .action(async () => {
55
55
  const manifestDir = (0, run_1.findManifestDir)(process.cwd());
56
- const output = (0, run_1.runSensors)({ all: true, ignoreBaseline: true });
56
+ const output = await (0, run_1.runSensors)({ all: true, ignoreBaseline: true });
57
57
  const baseline = (0, baseline_1.buildBaseline)(output.sensors.map(s => ({ name: s.name, errors: s.errors })));
58
58
  const writeDir = manifestDir ?? process.cwd();
59
59
  (0, baseline_1.writeBaseline)(writeDir, baseline);
@@ -6,10 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.applyBaseline = applyBaseline;
7
7
  exports.reconcilePack = reconcilePack;
8
8
  exports.findManifestDir = findManifestDir;
9
+ exports.resolveConcurrency = resolveConcurrency;
9
10
  exports.runSensors = runSensors;
10
- const child_process_1 = require("child_process");
11
11
  const fs_1 = __importDefault(require("fs"));
12
+ const os_1 = __importDefault(require("os"));
12
13
  const path_1 = __importDefault(require("path"));
14
+ const exec_1 = require("./exec");
13
15
  const tsc_1 = require("./formatters/tsc");
14
16
  const eslint_1 = require("./formatters/eslint");
15
17
  const semgrep_1 = require("./formatters/semgrep");
@@ -22,9 +24,11 @@ const MANIFEST_FILE = '.awm/sensors.json';
22
24
  const DEFAULT_FAST_TIMEOUT = 10_000;
23
25
  const DEFAULT_SLOW_TIMEOUT = 120_000;
24
26
  // Sensor JSON output can be several MB on large repos (e.g. `eslint --format json`
25
- // with thousands of findings). execSync defaults to a 1MB buffer and kills the
26
- // child with SIGTERM when exceeded — which previously surfaced as a false "timeout".
27
+ // with thousands of findings). A 1MB cap killed the child with SIGTERM when
28
+ // exceeded — which previously surfaced as a false "timeout".
27
29
  const MAX_BUFFER = 64 * 1024 * 1024;
30
+ /** Hard ceiling on parallel sensors: past this, they only contend for the same cores. */
31
+ const MAX_CONCURRENCY = 4;
28
32
  /**
29
33
  * Apply the baseline to a sensor result: keep only findings not already accepted.
30
34
  * `status` becomes 'pass' when every finding was baseline-suppressed. Results
@@ -125,75 +129,117 @@ function getFormatter(name) {
125
129
  function isExitCodeSensor(name) {
126
130
  return name === 'test';
127
131
  }
128
- function runSensor(name, cmd, timeout, cwd) {
129
- try {
130
- const raw = (0, child_process_1.execSync)(cmd, { encoding: 'utf-8', timeout, cwd, maxBuffer: MAX_BUFFER, stdio: ['pipe', 'pipe', 'pipe'] });
131
- const errors = getFormatter(name)(raw);
132
- return { name, status: errors.length > 0 ? 'fail' : 'pass', errors };
132
+ async function runSensor(name, cmd, timeout, cwd) {
133
+ const res = await (0, exec_1.runCommand)(cmd, { timeout, cwd, maxBuffer: MAX_BUFFER });
134
+ const format = getFormatter(name);
135
+ // The shell itself never started (bad cwd, no shell). Nothing ran.
136
+ if (res.spawnError) {
137
+ return {
138
+ name,
139
+ status: 'fail',
140
+ errors: [{ message: `sensor could not be started: ${res.spawnError.message}` }],
141
+ };
133
142
  }
134
- catch (err) {
135
- // Output exceeded maxBuffer child is killed before output can be read.
136
- // Check this BEFORE the SIGTERM branch (ENOBUFS kills with SIGTERM too).
137
- // Nothing could be read, so nothing was certified.
138
- if (err.code === 'ENOBUFS') {
139
- return { name, status: 'inconclusive', errors: [], skipReason: `output exceeded ${MAX_BUFFER} bytes` };
140
- }
141
- // Genuine timeout: execSync kills with SIGTERM after `timeout` ms. The
142
- // sensor produced no verdict — inconclusive, not a benign skip.
143
- if (err.code === 'ETIMEDOUT' || (err.killed && err.signal === 'SIGTERM')) {
144
- return { name, status: 'inconclusive', errors: [], skipReason: `timeout after ${timeout}ms` };
145
- }
146
- // Non-zero exit — the normal path for linters/typecheckers that found
147
- // findings. Parse the output; if it yields findings, that's a fail.
148
- const raw = String((err.stdout ?? '') + (err.stderr ?? ''));
149
- const errors = getFormatter(name)(raw);
150
- if (errors.length > 0)
151
- return { name, status: 'fail', errors };
152
- // A missing tool (binary not installed) must NOT pass silently — the gate
153
- // cannot certify what it could not run. Treat it as a fail with a clear message.
154
- //
155
- // Exit 127 is the POSIX signal for "command not found" and is the only check
156
- // here that holds across shells and locales: bash writes `command not found`
157
- // but dash — `/bin/sh` on Debian/Ubuntu, hence most CI runners and containers
158
- // — writes `not found`, so matching shell text alone read an absent tool as a
159
- // benign skip. `err.code` does not cover it either: that is ENOENT only when
160
- // spawning the shell itself fails, not when the shell starts and the command
161
- // inside it is missing. The ENOBUFS and timeout branches are evaluated above,
162
- // so reaching here with status 127 means the command did not exist.
163
- //
164
- // A wrapper (`npm test`, `npx …`) that exits 127 because a binary it invokes
165
- // is absent is classified the same way, deliberately: the gate still ran
166
- // nothing and still cannot certify anything.
167
- const lower = raw.toLowerCase();
168
- const toolMissing = err.status === 127 || // POSIX: command not found
169
- err.code === 'ENOENT' || // execSync spawn failure (no shell)
170
- lower.includes('command not found') || // bash, zsh
171
- // cmd.exe reports an absent binary with exit 1, so 127 does not cover
172
- // Windows; this exact phrase does. Kept narrow on purpose — a loose
173
- // `not found` would also match a tool that ran and said "not found"
174
- // for reasons of its own.
175
- lower.includes('is not recognized as an internal or external command') ||
176
- lower.includes('enoent') ||
177
- lower.includes('could not determine executable');
178
- if (toolMissing) {
143
+ // Cut short by the deadline or the output cap. The run is NOT a verdict — but
144
+ // whatever it printed before being cut is still evidence, and throwing it away
145
+ // is what forced the caller to re-run the same command by hand to learn
146
+ // anything. Findings in the partial output are real findings; their absence
147
+ // proves nothing, so a clean partial can never be `pass`.
148
+ if (res.timedOut || res.overflowed) {
149
+ const reason = res.timedOut
150
+ ? `timeout after ${timeout}ms`
151
+ : `output exceeded ${MAX_BUFFER} bytes`;
152
+ const errors = format(res.stdout + res.stderr);
153
+ if (errors.length > 0) {
179
154
  return {
180
155
  name,
181
156
  status: 'fail',
182
- errors: [{ message: `sensor tool not available: ${raw.slice(0, 200)}` }],
157
+ errors,
158
+ incomplete: `${reason} — findings below are from partial output; the run did not finish`,
183
159
  };
184
160
  }
185
- // Exit-code sensors (tests): any genuine non-zero exit is a real failure,
186
- // even when no per-line findings can be parsed from the output.
187
- if (isExitCodeSensor(name)) {
188
- return { name, status: 'fail', errors: [{ message: `SENSOR[${name}] failed (exit ${err.status})` }] };
189
- }
190
- // Residual case: it exited non-zero, the tool exists, and no finding
191
- // could be parsed. We do not know what happened — say so instead of
192
- // reporting a benign skip.
193
- return { name, status: 'inconclusive', errors: [], skipReason: `exit ${err.status}: ${raw.slice(0, 200)}` };
161
+ return { name, status: 'inconclusive', errors: [], skipReason: reason };
162
+ }
163
+ if (res.code === 0) {
164
+ const errors = format(res.stdout);
165
+ return { name, status: errors.length > 0 ? 'fail' : 'pass', errors };
166
+ }
167
+ // Non-zero exit the normal path for linters/typecheckers that found
168
+ // findings. Parse the output; if it yields findings, that's a fail.
169
+ const raw = res.stdout + res.stderr;
170
+ const errors = format(raw);
171
+ if (errors.length > 0)
172
+ return { name, status: 'fail', errors };
173
+ // A missing tool (binary not installed) must NOT pass silently — the gate
174
+ // cannot certify what it could not run. Treat it as a fail with a clear message.
175
+ //
176
+ // Exit 127 is the POSIX signal for "command not found" and is the only check
177
+ // here that holds across shells and locales: bash writes `command not found`
178
+ // but dash — `/bin/sh` on Debian/Ubuntu, hence most CI runners and containers
179
+ // — writes `not found`, so matching shell text alone read an absent tool as a
180
+ // benign skip. A failure to spawn the shell itself is a different thing and
181
+ // is handled above via `spawnError`. The cut-short branches are evaluated
182
+ // above too, so reaching here with status 127 means the command did not exist.
183
+ //
184
+ // A wrapper (`npm test`, `npx …`) that exits 127 because a binary it invokes
185
+ // is absent is classified the same way, deliberately: the gate still ran
186
+ // nothing and still cannot certify anything.
187
+ const lower = raw.toLowerCase();
188
+ const toolMissing = res.code === 127 || // POSIX: command not found
189
+ lower.includes('command not found') || // bash, zsh
190
+ // cmd.exe reports an absent binary with exit 1, so 127 does not cover
191
+ // Windows; this exact phrase does. Kept narrow on purpose — a loose
192
+ // `not found` would also match a tool that ran and said "not found"
193
+ // for reasons of its own.
194
+ lower.includes('is not recognized as an internal or external command') ||
195
+ lower.includes('enoent') ||
196
+ lower.includes('could not determine executable');
197
+ if (toolMissing) {
198
+ return {
199
+ name,
200
+ status: 'fail',
201
+ errors: [{ message: `sensor tool not available: ${raw.slice(0, 200)}` }],
202
+ };
194
203
  }
204
+ // Exit-code sensors (tests): any genuine non-zero exit is a real failure,
205
+ // even when no per-line findings can be parsed from the output.
206
+ if (isExitCodeSensor(name)) {
207
+ return { name, status: 'fail', errors: [{ message: `SENSOR[${name}] failed (exit ${res.code})` }] };
208
+ }
209
+ // Residual case: it exited non-zero, the tool exists, and no finding
210
+ // could be parsed. We do not know what happened — say so instead of
211
+ // reporting a benign skip.
212
+ return { name, status: 'inconclusive', errors: [], skipReason: `exit ${res.code}: ${raw.slice(0, 200)}` };
213
+ }
214
+ /**
215
+ * How many sensors may run at once. Sensors are separate processes over the same
216
+ * tree, so they parallelise cleanly — but each one (tsc, eslint, depcruise) is
217
+ * largely single-threaded, and oversubscribing the box just makes every sensor
218
+ * slower and more likely to hit its own deadline. Leave a core for the agent.
219
+ */
220
+ function resolveConcurrency(manifest, sensorCount) {
221
+ const configured = Number(process.env.AWM_SENSORS_CONCURRENCY ?? manifest.concurrency);
222
+ if (Number.isFinite(configured) && configured >= 1)
223
+ return Math.min(Math.floor(configured), sensorCount);
224
+ const cores = os_1.default.cpus()?.length ?? 2;
225
+ return Math.max(1, Math.min(MAX_CONCURRENCY, cores - 1, sensorCount));
226
+ }
227
+ /** Run `tasks` with at most `limit` in flight, preserving input order in the output. */
228
+ async function pooled(tasks, limit) {
229
+ const results = new Array(tasks.length);
230
+ let next = 0;
231
+ const worker = async () => {
232
+ while (true) {
233
+ const i = next++;
234
+ if (i >= tasks.length)
235
+ return;
236
+ results[i] = await tasks[i]();
237
+ }
238
+ };
239
+ await Promise.all(Array.from({ length: Math.min(limit, tasks.length) }, worker));
240
+ return results;
195
241
  }
196
- function runSensors(opts = {}) {
242
+ async function runSensors(opts = {}) {
197
243
  const startCwd = opts.cwd ?? process.cwd();
198
244
  const manifestDir = findManifestDir(startCwd);
199
245
  if (!manifestDir)
@@ -204,31 +250,38 @@ function runSensors(opts = {}) {
204
250
  const reconciled = reconcilePack(manifestDir, manifest);
205
251
  const activeManifest = reconciled.manifest;
206
252
  const cwd = manifestDir; // ejecutar sensores y baseline desde donde vive el manifest
207
- const results = [];
208
253
  // Baseline suppresses already-accepted findings so sensors fail only on NEW
209
254
  // ones (essential on repos with a large pre-existing baseline). Absent file or
210
255
  // --ignore-baseline → every finding counts (backward-compatible).
211
256
  const baseline = opts.ignoreBaseline ? null : (0, baseline_1.readBaseline)(cwd);
257
+ // Sensors are independent processes over the same tree, so they run
258
+ // concurrently rather than one-after-another: wall clock becomes the slowest
259
+ // sensor instead of the sum of all of them. Tasks are built — and dispatched —
260
+ // in manifest order, so the reported order stays stable.
261
+ const tasks = [];
262
+ const settled = (r) => () => Promise.resolve(r);
212
263
  for (const [name, config] of Object.entries(activeManifest.sensors)) {
213
264
  const isFast = config.fast ?? false;
214
265
  if (!shouldRun(isFast, opts))
215
266
  continue;
216
267
  if (config.enabled === false) {
217
- results.push({ name, status: 'skipped', errors: [], skipReason: 'disabled' });
268
+ tasks.push(settled({ name, status: 'skipped', errors: [], skipReason: 'disabled' }));
218
269
  continue;
219
270
  }
220
271
  if (!config.cmd) {
221
272
  // Enabled but with nothing to run: broken config, not a deliberate
222
273
  // opt-out. `enabled: false` is how a sensor is turned off.
223
- results.push({ name, status: 'inconclusive', errors: [], skipReason: 'no cmd configured' });
274
+ tasks.push(settled({ name, status: 'inconclusive', errors: [], skipReason: 'no cmd configured' }));
224
275
  continue;
225
276
  }
277
+ const cmd = config.cmd;
226
278
  const timeout = config.timeout ?? (isFast ? DEFAULT_FAST_TIMEOUT : DEFAULT_SLOW_TIMEOUT);
227
- let result = runSensor(name, config.cmd, timeout, cwd);
228
- if (baseline)
229
- result = applyBaseline(result, baseline[name]);
230
- results.push(result);
279
+ tasks.push(async () => {
280
+ const result = await runSensor(name, cmd, timeout, cwd);
281
+ return baseline ? applyBaseline(result, baseline[name]) : result;
282
+ });
231
283
  }
284
+ const results = await pooled(tasks, resolveConcurrency(activeManifest, tasks.length));
232
285
  // `fail` outranks `inconclusive`: when something is broken AND something
233
286
  // could not be measured, the broken thing is the actionable verdict.
234
287
  let overall = results.some(r => r.status === 'fail') ? 'fail'
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.spawnFailed = exports.overflowed = exports.timedOut = exports.exited = exports.ok = void 0;
4
+ /**
5
+ * Builders for the `ExecResult` shape that `runCommand` returns. Sensor tests
6
+ * mock the exec boundary rather than `child_process` directly: `runCommand`
7
+ * never throws, so a mocked run is a value, not an exception.
8
+ */
9
+ const base = { stdout: '', stderr: '', code: null, signal: null, timedOut: false, overflowed: false };
10
+ /** Clean run: exit 0. */
11
+ const ok = (stdout = '') => ({ ...base, stdout, code: 0 });
12
+ exports.ok = ok;
13
+ /** Ran to completion with a non-zero exit code. */
14
+ const exited = (code, stdout = '', stderr = '') => ({ ...base, stdout, stderr, code });
15
+ exports.exited = exited;
16
+ /** Cut short by the deadline. `stdout` is whatever it managed to print first. */
17
+ const timedOut = (stdout = '', stderr = '') => ({ ...base, stdout, stderr, signal: 'SIGKILL', timedOut: true });
18
+ exports.timedOut = timedOut;
19
+ /** Cut short by the output cap. */
20
+ const overflowed = (stdout = '') => ({ ...base, stdout, signal: 'SIGKILL', overflowed: true });
21
+ exports.overflowed = overflowed;
22
+ /** The shell itself never started. */
23
+ const spawnFailed = (message = 'ENOENT') => ({ ...base, spawnError: Object.assign(new Error(message), { code: 'ENOENT' }) });
24
+ exports.spawnFailed = spawnFailed;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const os_1 = __importDefault(require("os"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const exec_1 = require("../../../src/commands/sensors/exec");
10
+ const onPosix = process.platform !== 'win32' ? describe : describe.skip;
11
+ /** Poll until `fn()` is true or the budget runs out. Avoids fixed sleeps. */
12
+ async function until(fn, budgetMs = 4000) {
13
+ const deadline = Date.now() + budgetMs;
14
+ while (Date.now() < deadline) {
15
+ if (fn())
16
+ return true;
17
+ await new Promise(r => setTimeout(r, 25));
18
+ }
19
+ return fn();
20
+ }
21
+ describe('runCommand — exit codes and output', () => {
22
+ it('returns stdout and code 0 for a clean command', async () => {
23
+ const r = await (0, exec_1.runCommand)('echo hello', { timeout: 5000, cwd: process.cwd() });
24
+ expect(r.code).toBe(0);
25
+ expect(r.stdout.trim()).toBe('hello');
26
+ expect(r.timedOut).toBe(false);
27
+ expect(r.overflowed).toBe(false);
28
+ });
29
+ it('captures stderr and a non-zero exit code without throwing', async () => {
30
+ const r = await (0, exec_1.runCommand)('echo oops 1>&2; exit 3', { timeout: 5000, cwd: process.cwd() });
31
+ expect(r.code).toBe(3);
32
+ expect(r.stderr).toMatch(/oops/);
33
+ expect(r.timedOut).toBe(false);
34
+ });
35
+ it('reports 127 for a command that does not exist', async () => {
36
+ const r = await (0, exec_1.runCommand)('awm-definitely-not-a-real-binary-xyz', { timeout: 5000, cwd: process.cwd() });
37
+ expect(r.code).toBe(127);
38
+ });
39
+ });
40
+ describe('runCommand — output cap', () => {
41
+ it('stops at maxBuffer, flags overflow, and keeps what it read', async () => {
42
+ // 200 lines of ~50 bytes each, capped at 1KB.
43
+ const r = await (0, exec_1.runCommand)(`for i in $(seq 1 200); do echo "line-$i-padding-padding-padding-padding"; done`, {
44
+ timeout: 10_000, cwd: process.cwd(), maxBuffer: 1024,
45
+ });
46
+ expect(r.overflowed).toBe(true);
47
+ expect(r.stdout.length).toBeLessThanOrEqual(1024);
48
+ // The point of the cap change: what was read is still usable, not discarded.
49
+ expect(r.stdout).toMatch(/line-1-/);
50
+ });
51
+ });
52
+ onPosix('runCommand — timeout', () => {
53
+ it('flags the timeout and returns the output produced before the deadline', async () => {
54
+ const r = await (0, exec_1.runCommand)('echo partial-finding; sleep 30', { timeout: 700, cwd: process.cwd() });
55
+ expect(r.timedOut).toBe(true);
56
+ // This is the whole point of dropping execSync: 700ms of work is not thrown away.
57
+ expect(r.stdout).toMatch(/partial-finding/);
58
+ });
59
+ it('kills the grandchild process, not just the shell it spawned', async () => {
60
+ // Models `npx tsc --noEmit`: the sensor command is a wrapper that spawns the
61
+ // real tool. execSync SIGTERMs only the shell it started, leaving the tool
62
+ // running and reparented to init — the leak that compounds across retries.
63
+ const dir = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), 'awm-exec-group-'));
64
+ const beat = path_1.default.join(dir, 'beat');
65
+ const worker = path_1.default.join(dir, 'worker.js');
66
+ fs_1.default.writeFileSync(worker, `
67
+ const fs = require('fs');
68
+ setInterval(() => fs.writeFileSync(${JSON.stringify(beat)}, String(Date.now())), 30);
69
+ setTimeout(() => {}, 60000);
70
+ `);
71
+ try {
72
+ const r = await (0, exec_1.runCommand)(`sh -c "node ${worker} & wait"`, { timeout: 800, cwd: dir });
73
+ expect(r.timedOut).toBe(true);
74
+ // The worker must have been alive before the kill, or the test proves nothing.
75
+ expect(await until(() => fs_1.default.existsSync(beat))).toBe(true);
76
+ const atKill = fs_1.default.readFileSync(beat, 'utf-8');
77
+ const stillBeating = await until(() => fs_1.default.readFileSync(beat, 'utf-8') !== atKill, 1000);
78
+ expect(stillBeating).toBe(false);
79
+ }
80
+ finally {
81
+ fs_1.default.rmSync(dir, { recursive: true, force: true });
82
+ }
83
+ }, 15_000);
84
+ });
85
+ describe('runCommand — spawn failure', () => {
86
+ it('surfaces a spawn error instead of hanging', async () => {
87
+ const r = await (0, exec_1.runCommand)('echo hi', { timeout: 5000, cwd: path_1.default.join(os_1.default.tmpdir(), 'awm-no-such-dir-xyz') });
88
+ expect(r.spawnError).toBeDefined();
89
+ expect(r.code).not.toBe(0);
90
+ });
91
+ });