Skip to main content

Module agent_roots

Module agent_roots 

Source
Expand description

The agent CLIs srv is running, kept on disk for the launcher.

On Unix each agent CLI leads its own process group (srv spawns it with process_group(0)), so a signal sent to one CLI’s group can’t reach srv, and srv’s teardown of an agent takes the CLI’s MCP servers with it. The cost: when srv itself dies without closing its agents (SIGKILL, a crash, a force-kill after its exit deadline), killing srv’s group no longer takes the CLIs. This file is how the launcher finds them then.

srv writes <data_dir>/agent-roots.json (temp file + rename, so a reader never sees half of it) whenever an agent CLI starts or exits, and deletes it on a clean exit. It names srv’s pid, its Linux cgroup scope when it has one, and each CLI by pid and start time. Once srv has stopped, kill_orphans ends what is left:

  • Linux with srv’s delegated scope: cgroup.kill on each agent cgroup (agent-*) in it, which holds everything that agent started however it detached.
  • Everywhere on Unix, and as the fallback: kill(-pid, SIGKILL) for each recorded CLI that is still the same process (its start time matches) and still leads its own group. A pid alone is never signalled: it may have been reused since srv wrote it.

Structs§

AgentRoots
What srv writes.
Cleanup
What kill_orphans did, for the caller’s log.
Root
One agent CLI: a pid, and when that process started (process::process_start_time), so a reused pid is never mistaken for it.

Constants§

FILE_NAME
File name, in srv’s data dir next to srv.lock.

Functions§

is_alive
Whether a recorded CLI’s group is still there to end: its leader is still the process that was recorded (same start time), or the leader is gone but its helpers still hold the group (process::orphaned_group_alive). A pid that now belongs to some other process is not ours.
kill_agent_cgroups
Write 1 to cgroup.kill in every agent-* cgroup in scope; srv’s own cgroup and anything else in the scope are left alone. Returns how many were killed. Public for srv’s cgroup tests.
kill_orphans
End the agent CLIs a stopped srv (srv_pid) left running, then delete the file. Call only once that srv has exited. None when there is no file, or it was written by another srv (left in place).
path
read
remove
Delete the file (srv’s clean exit). Already gone is fine.
write
Replace the file atomically: write a temp file beside it, then rename.