fix(server): agents stop when the server is killed #18648
Closedgustavohariel(GitHub profile, opens in a new tab)wants to merge 6 commits into
main fromrefs/pull/18648/headopened View on GitHubjuliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
coderabbitai[bot](GitHub profile, opens in a new tab) requested changes
packages/shared/src/AgentScope.test.ts lines 56 to 64OutdatedResolvedby gustavohariel(GitHub profile, opens in a new tab)@@ -29,0 +44,21 @@44+ const agentPid = await new Promise<number>((resolve) =>45+ server.stdout.once("data", (chunk: Buffer) => resolve(Number(chunk.toString().trim()))),46+ );47+ const isAlive = () => {48+ try {49+ process.kill(agentPid, 0);50+ return true;51+ } catch {52+ return false;53+ }54+ };55+ // setpriv has set the signal once the agent has exec'd into its command.56+ await vi.waitFor(() =>57+ expect(NodeFS.readFileSync(`/proc/${agentPid}/cmdline`, "utf8")).toBe(58+ "sleep\u0000300\u0000",59+ ),60+ );61+62+ server.kill("SIGKILL");63+64+ await vi.waitFor(() => expect(isAlive()).toBe(false), { timeout: 5_000 });packages/shared/src/AgentScope.ts line 132Resolvedby gustavohariel(GitHub profile, opens in a new tab)@@ -97,10 +97,17 @@9797 * spawned PID, stdio, and signals all belong to the agent itself.9898 * `systemd-run --scope` registers its own PID as the scope and then runs the9999 * command, so the agent and all of its children start inside the scope.100+ *101+ * With `setpriv`, the agent also gets SIGTERM when the server dies. A scope102+ * takes the agent out of the server's cgroup, so when the server is killed103+ * (the service launcher's SIGKILL after its grace period, a crash) nothing104+ * else stops the agent, and it keeps working and billing with nobody reading it.100105 */101106 export function agentScopeCommand(input: {102107 readonly command: string;103108 readonly args: ReadonlyArray<string>;104109 readonly scope?: AgentSystemdScope | undefined;110+ readonly setpriv?: string | undefined;105111 }): AgentScopeCommand {106- const { command, args, scope } = input;112+ const { command, args, scope, setpriv } = input;113+ const dieWithServer = setpriv === undefined ? [] : [setpriv, "--pdeathsig", "TERM", "--"];github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -83,3 +83,5 @@8383 'exec "$@"',8484 ].join("\n");858586+const SCOPE_WATCH_SCRIPT = [87+ "while :; do",
coderabbitai[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge closed this
Sign in to join this conversation.
Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This PR changes the lifecycle of all applicable Linux agents by adding parent-death signaling and a background systemd-scope watcher, making it a nontrivial production process-management change. An unresolved Medium finding also identifies a startup race, so the change warrants human review.
Not approved because:
- 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).