@inkeep/impulse-owned-process
v0.3.0
Published
A child process with a deadline and an owner, on effect/process: every piped stream drained under a stated disposition, one termination path with group-gone evidence, a bounded line sink, and a hand-off with start identity.
Keywords
Readme
@inkeep/impulse-owned-process
A child process with a deadline and an owner, on effect 4.1's effect/process. Every run has a deadline and a
termination grace, every piped stream is drained to its end, and the result is a ProcessRecord: the exit, what each
stream carried, and how the run was ended. The interface is §7.1 of the CWS foundation note, revision 3
(design note,
interface digest),
plus capture(n, "keep-ends") and the omitted field it records, and termination.group, the evidence behind
groupGone, both added for shared CI's check-affected, and an owner-loss tether.
Install
The package is public on npm. Install it with its peer, any effect 4.1.x from 4.1.2 (~4.1.2):
npm install @inkeep/impulse-owned-process [email protected]
pnpm add @inkeep/impulse-owned-process [email protected]- It is ESM with TypeScript declarations, for TypeScript 5.9 or later with
strict, aseffectrequires. It declares noenginesfield. New-group runs require Node 26.10.0 or later; the owner checks this before spawning anything. The suite passed on Node 26.10.0 on macOS arm64; the nightly uses the repository pin, Node 26.12.1. - Its entry needs a
ChildProcessSpawner:NodeServices.layerfrom@effect/platform-node4.1.x provides it (below). - Inside this repository, a member depends on it with
"@inkeep/impulse-owned-process": "workspace:*"instead. - Releases follow
packages/README.md, "Publishing a member to npm".@inkeep/impulse-sshdepends on this package, so each of its versions is published here first.
Exports
| Export | What it is |
| ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| run(request, { stop }?) | Runs a child to its end and returns its ProcessRecord; a non-zero exit is data |
| checked(request, { stop }?) | run, failing ExitFailed unless the exit code is 0 |
| owned(request) | A scoped child: pid, await, stop(reason) and release |
| processStartTime(pid), sameProcess(identity) | A process's start identity, and whether a pid is still that process (same, reused, gone) |
| capture, forward, ignore, lines | What happens to stdout and stderr; lines is a Sink that splits on \n, \r\n or \r and cuts a line at maxLineBytes |
| ProcessRecord, HandOff, ProcessIdentity, ProcessError | Effect Schemas, so a record or an error crosses a process boundary as data. The records are plain structs, so every schema-valid value encodes, a copy included; ProcessError is a tagged error class |
| OutputRecord | Part of ProcessRecord: what happened to stdout or stderr. Captured holds the bytes kept, the total, whether it was truncated, where a cut was made and whether the stream closed; Forwarded holds how many bytes were passed on, the total, whether it was truncated and whether the stream closed; Ignored holds nothing |
| ProcessExit | Part of ProcessRecord: Exited with its code, or Signaled with its signal |
| StdinRecord | Part of ProcessRecord: None, or Fed with the bytes written and how stdin ended (source-done, child-closed or stopped) |
| Termination | Part of ProcessRecord: what ended the child (deadline, stop, output-limit, or nothing), the reason and when, whether its process group was seen gone, and the evidence for that in group: the group's last answer and the members read |
| ProcessErrorReason | The reason a ProcessError carries: SpawnFailed, DeadlineExceeded, OutputLimit, ExitFailed or ReleaseRefused, with the ProcessRecord where there is one |
Using it
- The environment is complete.
envis everything the child sees; nothing is inherited, and the package adds nothing. Underoffload, carryOT_RUN_ID,OT_OWNERand thePATHthat putsoffload's labellingdockerfirst through theenvyou pass. A caller that leaves them out loses the run's attribution for anydockerits child runs. - Provide
ChildProcessSpawnerat your entry, withNodeServices.layerfrom@effect/platform-node, never inside a layer. Import it from@effect/platform-node/NodeServices: the package root reacheseffect/rpc, whose declarations name the DOM-onlyTransferable, so a Node project withskipLibCheck: falseand noDOMlibrary fails to type-check (Effect ledger, Effect-TS/effect#9785). - Say what each stream does.
stdoutandstderrhave no default. Data that must arrive whole takes an uncappedforwardorcapture(n, "terminate");capture(n, "keep-tail")and a cappedforwardare for logs and stderr tails.capture(n, "keep-ends")keeps the first half ofn, rounded down, and the last half, rounded up, for output whose first lines say what went wrong. When it drops bytes, its record says where:omitted: { at, bytes }is the offset inbyteswhere the head ends and the tail begins, and how many bytes were dropped there. Only akeep-endsrecord that dropped bytes carriesomitted. - A
stdinsource that fails never reaches the child as EOF. The run fails with the source's own error, and the package holds the child's stdin open until the stop has ended the child, so a child never reads truncated input as complete. Effect's ownstdinstream option would destroy the pipe at once, and report the failure nowhere. A run the caller interrupts keeps the same promise: the child is stopped before its feed is interrupted, because fromeffect4.0.6 an interrupted feed destroys the pipe too. - One termination path. A deadline, a
stopand aterminateoverflow all send SIGTERM to the child's process group under oneterminationGracedeadline when that grace covers the spawner's documented final one-second wait. The spawner receives the time remaining after that wait, then the same deadline bounds group confirmation. A shorter grace reserves the configured grace for SIGKILL and may include the platform tail.termination.bysays which one ended the run. Astopeffect that dies stops the child the same way, and the run fails with that defect. Interrupting the fiber stops the child the same way and returns no record. UnderprocessGroup: "inherit"the signals go to the child's pid,groupGonereadsnot-applicableandtermination.groupisnull. - Confirming the group gone. The probe,
kill(-pgid, 0), repeats until the group answers ESRCH (observed) or the sharedterminationGracedeadline is spent, so a zombie member reaped within it reads as gone. A group that still answers then has its members read once, and is probed once more. It readsobservedwhen that probe answers ESRCH, or when the read listed at least one member and every one of them is a zombie, its state starting withZ. Otherwise it readsnot-observedwhen the probe succeeded andunknownwhen it failed, so a member that is not a zombie, or a read that fails, keeps the group unconfirmed, and EPERM alone never reads as gone. A group of zombies answers EPERM on macOS and success on Linux. The confirmation runs as the run's last finalizer, after the spawner's release, so no interruption cuts it short, one that arrives while a stopped run confirms its group included: it returns once the group answers ESRCH, or once the sharedterminationGracedeadline is spent and the members have been read with the remaining allowance; if no allowance remains, the evidence isUnreadable. termination.groupis the evidence.lastProbeis the last probe's answer:Succeeded, orFailedwith its errnocode.membersisNotReadwhen the group answered ESRCH in time,Readwith each listed member'spidandstate, orUnreadablewithwhy. The read is/bin/ps -A -o pid=,pgid=,stat=underLC_ALL=C, a run withterminationGraceas both its deadline and its grace. It sees whatpslists; on Linux that is what/procshows, and a/procmounted withhidepiddoes not show another user's processes.- An owner-loss tether. If the owning process dies without interrupting the run (SIGKILL, a crash,
process.exit), the child's group is still stopped the one way. Each run with its own group starts a tether:/bin/sh, in a session of its own, blocked reading a pipe that only the owner holds. When the owner dies, the pipe reaches end of file, and the tether sends SIGTERM to the group, waitsterminationGrace, sends SIGKILL to a group that still answers, probes until it no longer answers orterminationGraceis spent again, and exits. Any failed probe counts as gone: ESRCH, or EPERM, which macOS answers for a group left with only zombies.- Acquisition is protected too. A new group's first process waits on a private startup pipe. Only after its
tether has started does the owner send the executable, arguments and complete environment through that pipe.
The waiting process uses Node's POSIX
process.execveto become the command without changing its PID or streams. Owner loss before the startup message closes the pipe, so the command never starts; after it, the tether stops the group. A partial message never executes. New-group runs require Node 26.10.0 or newer. The owner checks the exact semantic version and availability ofprocess.execvebefore spawning the launcher or the tether. A runtime below the floor or withoutprocess.execvefails with typedSpawnFailed/ENOSYSat the call. Node 22 and 24 abort on a failed exec instead of returning its errno. The repository and the O6 and offload consumers pin Node 26.12.1. - Only end of file makes it signal. A line disarms it: the tether reads it and exits without signalling. When the
run ends, after the group is confirmed gone, and when a child is released, before
releasereturns, the package writes the tether one line before closing its pipe. So the tether never signals the group's ID once the group has gone, whatever order the pipe and the tether end in. - The SIGKILL lands slightly after the grace: about 1.1 s for a 1 s grace on macOS and 1.0 s on Linux. The tether
counts the grace in 100 ms polls, and each poll forks a
sleep. - How the tether is owned and reaped. While the owner lives, the tether belongs to the run. After its disarming
line the package waits up to
terminationGracefor it to exit, and stops one still running with SIGTERM. After the owner dies, init or the nearest subreaper reaps it, at most about twiceterminationGracelater. It starts nothing that outlives it. - A child handed off by
releaseis exempt. The hand-off is how a child outlives its owner on purpose. - Under
processGroup: "inherit"there is no tether. The child shares the owner's group, and a carrier lease in@inkeep/impulse-sshrelies on such a child outliving its owner. - A tether that cannot be started fails the run as
SpawnFailed, once the child has been stopped. - A spawner you substitute sees the tether too. It is spawned through your
ChildProcessSpawneras/bin/sh -c <script> owned-process-tether <pgid> <polls>, so a double must answer it: a process that ends when its stdin receives a line, or when it is killed, is enough. One that ignores its stdin holds each run's end for up toterminationGrace. The command itself first appears as Node runninginternal/launch, with fd 3 carrying the startup message and fd 4 reporting exec errors. Node marks inherited descriptors close-on-exec during startup, so neither startup descriptor reaches the command; a failed exec leaves fd 4 open to report the errno. A replacement spawner must consume fd 3 and close fd 4 for a successful startup, or report a JSON object with acodeon fd 4. Executable and environment values travel in the pipe, never argv.
- Acquisition is protected too. A new group's first process waits on a private startup pipe. Only after its
tether has started does the owner send the executable, arguments and complete environment through that pipe.
The waiting process uses Node's POSIX
releasehands a child off. It takes only a child with no pipe (nostdin,ignoreon both outputs), reads its start identity while the package still holds it, and returns aHandOff. Signal a released pid only aftersameProcess(identity)answerssame.- The start identity is
/bin/ps -o lstart= -p <pid>underLC_ALL=CandTZ=UTC. Its checks pass on macOS and on Linux (Ubuntu 26.04.1, procps-ng 4.0.4, run on O6-H1 on 2026-10-22).gonemeans the pid answered ESRCH; apsread that fails while the pid lives is aProcessError, nevergone.
Checks
pnpm test runs the Vitest suite against real child processes: process groups, signals, deadlines, output caps, stdin,
lines, identity and release, plus the effect/process behaviours the package relies on (test/effect-contract.test.ts).
The descriptor-inheritance check uses /usr/bin/python3 to call fstat without Node opening new descriptors at the
same numbers. The packages suite in scripts/ci/suites.mjs runs it nightly.
The repository pins effect, @effect/platform-node and @effect/vitest to 4.1.2. The contract tests select their
expectations from the installed @effect/platform-node-shared version, including the process-group behavior introduced
in 4.0.7.
