packstruct 0.9.0 → 1.1.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,101 @@
1
+ // src/run-capped.mjs - the one gated child runner both spawned surfaces use
2
+ // (the audit's HOST loader and the deep probe's entry reader). Zero deps.
3
+ //
4
+ // Why async spawn and not spawnSync pipes (the QC finding, CI-reproduced):
5
+ // a sync pipe read waits for EOF on stdout, and EOF needs every writer gone -
6
+ // a timeout-killed loader that leaves an ORPHANED grandchild holding the
7
+ // pipe makes spawnSync block forever. Killing is fine; waiting on the
8
+ // orphan's EOF is not. This runner resolves on the direct child's `exit`
9
+ // event - never on EOF - so an orphan can't hold the harness hostage.
10
+ //
11
+ // Caps, both hard (kill on exceed, no soft drift):
12
+ // timeoutMs - the child is SIGKILLed at the deadline; `timedOut` reports it
13
+ // maxBuffer - accumulated stdout+stderr bytes; past the cap the child is
14
+ // killed and `capped` reports it (the cap is an honesty cap:
15
+ // output beyond it is unverifiable output)
16
+ // After `exit` (or a kill), later data events from orphans are ignored and
17
+ // only the bytes captured before exit are returned.
18
+ //
19
+ // Group kill (2026-10-25 leak finding): caps that kill the child alone
20
+ // leaked /bin/sh's node grandchildren when the shell forked instead of
21
+ // exec'ing - a flood loader that survives its own death keeps console.log
22
+ // buffering into a pipe nobody reads, gigabytes per child in seconds (4
23
+ // leaked children ballooned this workspace 104MB -> 3.3GB and starved CI
24
+ // runners mid-suite). The child now spawns detached - its own process
25
+ // group - and every kill path signals the NEGATIVE pid: the direct child,
26
+ // any forked shell, and every grandchild, in one shot.
27
+ //
28
+ // Battlefield note (2026-10-24 QC): the predecessor of this file was the
29
+ // audit's sync spawn with file-stdio scratch - under an output-flooding
30
+ // loader there was NO cap on file growth, and a 20GB scratch file filled the
31
+ // disk before the 120s deadline. The flood name in the tests is the same,
32
+ // but the killer is now the cap, not the clock.
33
+ import { spawn } from 'node:child_process'
34
+
35
+ function makeKill(p, killSignal) {
36
+ // Negated pid = the whole process group (the child is a group leader
37
+ // because the spawn is detached). Falls back to the direct-child kill
38
+ // when the group is already gone; both no-op if the child never spawned.
39
+ return () => {
40
+ if (!p.pid) return
41
+ try { process.kill(-p.pid, killSignal) }
42
+ catch { try { p.kill(killSignal) } catch { /* already gone */ } }
43
+ }
44
+ }
45
+
46
+ export function runCapped({ cmd, args = [], shell = false, cwd, timeoutMs = 5000, maxBuffer = 1024 * 1024, killSignal = 'SIGKILL' }) {
47
+ return new Promise((resolve) => {
48
+ let done = false
49
+ let total = 0
50
+ let capped = false
51
+ let timedOut = false
52
+ let spawnError = null
53
+ const chunks = []
54
+ const stderrChunks = []
55
+ // detached on POSIX: the child leads its own process group, which is
56
+ // what makes the negated-pid group kill total (shell forks die too).
57
+ const p = spawn(cmd, args, { shell, cwd, detached: process.platform !== 'win32', stdio: ['ignore', 'pipe', 'pipe'] })
58
+ const killTree = makeKill(p, killSignal)
59
+ const timer = setTimeout(() => {
60
+ timedOut = true
61
+ killTree()
62
+ }, timeoutMs)
63
+ const finish = () => {
64
+ if (done) return
65
+ done = true
66
+ clearTimeout(timer)
67
+ try { p.stdout.destroy(); p.stderr.destroy() } catch { /* already dead */ }
68
+ resolve({
69
+ status: p.exitCode,
70
+ signal: p.signalCode,
71
+ stdout: Buffer.concat(chunks).toString('utf8'),
72
+ stderr: stderrChunks.join(''),
73
+ capped,
74
+ timedOut,
75
+ spawnError,
76
+ })
77
+ }
78
+ // stdout is capped hard: past maxBuffer the child dies. stderr counts
79
+ // against the same gate so a stderr flood cannot OOM the harness either.
80
+ // A chunk larger than the remaining budget is truncated to it and the
81
+ // child is killed - pushing it whole would return output past the cap
82
+ // (the honesty cap admits nothing it did not measure).
83
+ const absorb = (d, into) => {
84
+ if (done || capped) return
85
+ const remaining = maxBuffer - total
86
+ if (d.length > remaining) {
87
+ if (remaining > 0) into.push(d.subarray(0, remaining))
88
+ total = maxBuffer
89
+ capped = true
90
+ killTree()
91
+ return
92
+ }
93
+ into.push(d)
94
+ total += d.length
95
+ }
96
+ p.stdout.on('data', (d) => absorb(d, chunks))
97
+ p.stderr.on('data', (d) => absorb(d, stderrChunks))
98
+ p.on('error', (e) => { spawnError = e; finish() })
99
+ p.on('exit', finish)
100
+ })
101
+ }