Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(minions): shell jobs wait for stdout/stderr EOF after the reap, so output read after 'exit' is kept
garrytan61418fc11200
diff --git a/src/core/minions/handlers/shell.ts b/src/core/minions/handlers/shell.tsindex d900f037ab..484dccf0a3 100644--- a/src/core/minions/handlers/shell.ts+++ b/src/core/minions/handlers/shell.ts@@ -50,6 +50,11 @@ const STDERR_TAIL_MAX_BYTES = 16 * 1024;* flush state, exit cleanly; non-behaving scripts get reaped. */const KILL_GRACE_MS = 5000;+/** After the job's process group is gone, how long to wait for stdout/stderr to reach EOF. `'exit'`+ * can fire before the last chunk is read; only `'close'` means the pipes ended. A process that left+ * the group (setsid) and still holds a pipe gets this long before the tails are taken without it. */+const STDIO_DRAIN_MS = 2000;+export interface ShellJobParams {/** Shell command. Spawned via `/bin/sh -c cmd`. Exactly one of cmd or argv is required. */cmd?: string;@@ -311,6 +316,7 @@ export async function shellHandler(ctx: MinionJobContext): Promise<ShellJobResulproc.stdout?.on('data', (c: Buffer) => stdoutTail.append(c));proc.stderr?.on('data', (c: Buffer) => stderrTail.append(c));+ const stdioClosed = new Promise<void>(resolve => proc.on('close', () => resolve()));// Wire BOTH signals to the kill sequence. `ctx.signal` fires on timeout /// cancel / lock-loss; `ctx.shutdownSignal` fires only on worker SIGTERM/SIGINT.@@ -356,6 +362,9 @@ export async function shellHandler(ctx: MinionJobContext): Promise<ShellJobResul// SIGTERM) is terminated and confirmed gone before the job settles.await reapTree(treeAlive, signalTree);});+ let drainTimer: ReturnType<typeof setTimeout> | undefined;+ await Promise.race([stdioClosed, new Promise<void>(resolve => { drainTimer = setTimeout(resolve, STDIO_DRAIN_MS); })]);+ clearTimeout(drainTimer);const duration_ms = Date.now() - startedAt;// Assemble tails, then optionally scrub resolved inherit values out beforediff --git a/test/minions-shell-process-tree.test.ts b/test/minions-shell-process-tree.test.tsindex 26351c2608..b62e0a1276 100644--- a/test/minions-shell-process-tree.test.ts+++ b/test/minions-shell-process-tree.test.ts@@ -112,4 +112,16 @@ describePosix('#5062 shell handler process tree', () => {expect(alive(bg)).toBe(false);});}, 15_000);++ test('output that reaches the pipe after the leader exits is still in stdout_tail', async () => {+ if (!Bun.which('setsid')) return;+ const d = scratch();+ // The writer leaves the job's process group first (so the reap cannot take it), then writes after the leader exits.+ const cmd = `setsid sh -c 'touch ${d}/left; sleep 0.3; echo late' &\nwhile [ ! -f ${d}/left ]; do sleep 0.01; done\necho early`;+ await withEnv({ GBRAIN_ALLOW_SHELL_JOBS: '1' }, async () => {+ const result = await shellHandler(makeCtx({ cmd, cwd: d }, new AbortController().signal)) as { exit_code: number; stdout_tail: string };+ expect(result.exit_code).toBe(0);+ expect(result.stdout_tail).toBe('early\nlate\n');+ });+ }, 15_000);});