This week I added a new backend to Withmere, the personal AI environment I build and use on my Mac. The backend is pi, Mario Zechner’s open-source coding agent, and its first job is narrow: one of my agents gets a shell, a file editor and one project directory to work in. Withmere keeps the authority it already had over tools, the conversation record and resuming a session; pi runs the model and its native tools.
An agent with a shell starts processes. So the backend’s contract included a promise about the end of its life: when Withmere closes the backend, the work the agent started stops, and Withmere has checked that it stopped. That promise is what lets me treat “closed” as a fact when I later look at the project and ask what the agent did.
The check that broke the promise was small. An offline fake model, with no
provider and no credentials, asked the shell to run one Node command. That
command started a child process with Node’s detached: true option and exited.
The child waited two seconds, a delay written into the fixture, and then wrote
a file in the project. Withmere settled the turn, closed the backend, killed the
process group it owned and verified the group was gone. Then the file appeared.
The default test suite had passed 4045 tests the same night, and an earlier run of the installed-runtime verifier had passed 39 cases. None of them started a child that left its process group. Both were green because neither asked.
Killing the group is the reasonable approach
Every Unix process belongs to a process group, identified by a number, the PGID. A child inherits its parent’s group, and a signal sent to the negative of a PGID reaches every process currently in that group. That makes the group the natural handle for “this command and everything it started”: a shell pipeline, a test runner, a build that forks compilers. Kill the group and you kill the tree, without walking it.
Most tools that run other people’s commands rely on this. pi’s own
bash tool
starts each shell as the leader of its own group and, on abort or timeout,
sends SIGKILL to that group. Bazel’s process wrapper does the same and goes
further on macOS: it waits until the group is truly empty before returning, so
that a compiler still dying cannot interfere with outputs Bazel has moved on to.
Withmere’s close worked the same way. It recorded the group of the pi process
it launched, kept ordinary shell children inside it, and after close confirmed
that nothing in that group remained.
All of that is correct. The agent’s process group really was empty.
A process group is an address
A process can leave its group. setsid() and setpgid() are ordinary system
calls; Node exposes the first as detached: true, and daemons, dev servers and
background helpers use it routinely. The child gets a new PGID, so a signal to
the old one no longer reaches it. When its parent exits, it is reparented to
launchd and is no longer in the parent’s tree either. Nothing about this
requires privilege or intent to hide.
Here is the case without Withmere or pi, in plain Node:
import { spawn, execFileSync } from "node:child_process";
import { existsSync, rmSync } from "node:fs";
import { setTimeout as sleep } from "node:timers/promises";
const groupAlive = (pgid) => {
try { execFileSync("pgrep", ["-g", String(pgid)]); return true; } catch { return false; }
};
async function trial(label, detachGrandchild) {
const file = `late-write-${label}.txt`;
rmSync(file, { force: true });
// The "tool call": a shell in its own process group, like an agent's bash tool.
const inner = `require('child_process').spawn('/bin/sh',['-c','sleep 1; echo late > ${file}'],` +
`{detached:${detachGrandchild},stdio:'ignore'}).unref()`;
const tool = spawn("/bin/sh", ["-c", `node -e "${inner}"; sleep 30`], { detached: true, stdio: "ignore" });
await sleep(300);
process.kill(-tool.pid, "SIGKILL"); // "close": kill the whole group
await sleep(100);
const gone = !groupAlive(tool.pid);
await sleep(1500);
console.log(`${label.padEnd(14)} group gone: ${gone} file appeared after close: ${existsSync(file)}`);
}
await trial("same-group", false);
await trial("detached", true);
On macOS 26.5 with Node 25.8.0, four runs gave the same output:
same-group group gone: true file appeared after close: false
detached group gone: true file appeared after close: true
The only difference between the trials is one option on the grandchild. In
both, the kill succeeds and pgrep correctly reports the group empty. I have
not run this on Linux; the point of the next section is that Linux offers a
different answer, not a different pgrep.
The name of the operation hides the problem. “Kill the process tree” sounds like a statement about everything the command started. On Unix it is a statement about whoever currently answers to one address. Four things that usually coincide had come apart:
- Parentage: who started a process. It changes when a parent exits.
- Group membership: where a signal can reach it. The process can change it.
- Ownership: which component is responsible for its execution.
- Write capability: what it can still change.
The last one decided the outcome. The backend’s shell runs under a macOS sandbox profile that allows writes to the project and a scratch directory and denies the rest. That rule is attached to the process when it starts and is inherited by its children. It does not expire when the agent is closed, because the sandbox has no idea the agent exists. A detached child keeps the same permission to write to the project that the agent’s shell had.
Three promises that sound alike
Separating those four things makes it possible to say precisely what a lifecycle mechanism gives you. There are three different guarantees here, and tools are often described as if they offered all of them.
Lifetime containment. Everything the work started stays inside a unit the
supervisor owns, and ending the unit ends all of it, whatever the processes do
with setsid, groups or parents.
Write restriction. Whatever survives can only write where it was allowed. This limits the damage. It is not containment: a surviving process with a project-wide write rule can still write to the project.
Best-effort cleanup. Signals to a PID, a parent’s tree or a group stop the ordinary children. They do not establish membership for a child that left.
Withmere’s backend had the third and the second. Its contract was written as if it had the first.
Who has closed the gap, and what it costs
On Linux the gap is closed by making membership something the process cannot
change. A cgroup is
inherited at fork, and creating a new session or process group does not move a
process out of it. Writing to cgroup.kill kills the whole subtree, including
processes forked concurrently, and cgroup.events reports when the subtree is
no longer populated, which gives “stopped” an observable meaning. systemd’s
default KillMode=control-group
stops a service by killing everything in its cgroup. runc had an
open issue about
processes remaining after container teardown; the
fix, merged in June 2023,
uses the container’s private PID namespace or its cgroup as the boundary, not
a PGID. Even there the guarantee has conditions. runc’s own source warns that
rootless setups without cgroup delegation can leave processes behind, and
bubblewrap’s tracker discusses why dying with the parent is not enough on its
own. The boundary has to
be owned before the untrusted work starts.
macOS has no cgroups and no PID namespaces available to ordinary user-space tools. The person who wrote Bazel’s macOS process-group waiting says so directly: a subprocess that creates a new process group escapes the algorithm, and Bazel accepts that limitation; the wrapper is not a security mechanism. I find that sentence more useful than most solutions, because it states the boundary of the guarantee where a caller will read it.
I tried the cheap fix first. macOS sandbox profiles can deny process-control
operations, so a probe denied process-info* and process-info-setcontrol
and spawned ordinary and detached children under it. Both were created. The
sandbox controls what a process may touch, not which group it joins.
Two stronger options remained, each with a real cost.
Run the shell in a VM. Apple’s open-source
container tool runs each workload in its
own lightweight Linux VM, and
stop()
tears down the guest and stops the VM. Once the VM has stopped, nothing inside
it is running, whatever its PGID. The cost is that the agent’s shell is no
longer macOS. The project’s toolchain, paths and native binaries would have
to work inside Linux, and every command would cross a VM boundary. I have not
installed or tested it; it requires macOS 26 on Apple silicon.
Take writes away from the shell. A disposable probe ran the shell with read access to the project and write access only to scratch. Direct writes and late writes from a detached child were both denied. That closes the consequence I cared about, but it changes what the shell is for: the agent could still edit files through its file tools, but a formatter, a code generator or a build that writes into the tree would stop working from the shell.
What I promised instead
I chose neither. The contract now says what the mechanism can enforce. Closing the backend stops the process group Withmere registered and the executions Withmere itself started on the agent’s behalf, and verifies that. A shell descendant that detaches itself may survive close and keep its write access to the project and scratch directory. That is written down as an accepted limitation in the backend’s milestone record, next to the reproduction that found it.
The verifier changed shape to match. The detached-child case is no longer a
required case that the backend can pass by luck; it is a separate diagnostic
that reports what actually happened. On the amended run all 39 required cases
passed, and the diagnostic reported effect_after_close=true. The original
failure is kept as a failure in the record, not relabelled as fixed, and the
milestone notes say that a future run where no late write appears is an
observation, not proof of containment.
Why accept it? Withmere is a single-user system on a machine I control. Its security commitments say plainly that it does not provide strong process isolation between programs running as the same user, and that no security claim may exceed what is enforced. The agent works in one project under version control, and the remaining gap needs a command that detaches a child from its group. A VM boundary would buy a stronger sentence at the price of a different operating system for every shell command. That trade may become worth it if the agent’s scope grows. It is not worth it now, and making it quietly by keeping the old promise would have been the worst of the options.
What changed in the end is less the code than what a green check means. Before, “closed and verified” read as a statement about everything the agent had started. Now it is a statement about the group Withmere owns, and the one case that can leave that group is reported on its own line every time the verifier runs. Closing an agent closes what the closer can name. The useful part is saying, in the contract, which things those are.