fix(server): stop agent jobs that escape the CLI's process group on macOS and Windows #33691
Closedc-ehrlich(GitHub profile, opens in a new tab)wants to merge 1 commit into
main fromrefs/pull/33691/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -0,0 +306,21 @@306+ }),307+ );308+ yield* ensurePolling;309+ });310+311+ // Runs when the agent exits: whatever it started and left behind is stopped.312+ const stop: AgentScopeShape["stop"] = Effect.fn("AgentScope.stop")(function* (unit) {313+ threadByUnit.delete(unit);314+ const targets = yield* lock.withPermits(1)(315+ Effect.gen(function* () {316+ const session = sessions.get(unit);317+ if (session === undefined) return undefined;318+ sessions.delete(unit);319+ if (sessionByThread.get(session.threadId) === unit)320+ sessionByThread.delete(session.threadId);321+ const rows = yield* readTable;322+ if (rows === undefined) {323+ yield* Effect.logWarning("Could not stop an exited agent's processes");324+ return undefined;325+ }326+ return { processes: trackedProcesses(rows, session.processes), rows };@@ -0,0 +240,21 @@240+ // one starting up while the previous one shuts down, keeps its agents.241+ const recoverLeftovers = Effect.fn("trackedAgentScope.recoverLeftovers")(function* () {242+ const entries = yield* fileSystem243+ .readDirectory(ownersDir)244+ .pipe(Effect.orElseSucceed((): ReadonlyArray<string> => []));245+ const previous = entries.filter((entry) => INSTANCE_FILE.test(entry) && entry !== instance);246+ if (previous.length === 0) return;247+ const rows = yield* readTable;248+ if (rows === undefined) return;249+ for (const entry of previous) {250+ const file = path.join(ownersDir, entry);251+ const text = yield* fileSystem.readFileString(file).pipe(Effect.orElseSucceed(() => ""));252+ const [ownerLine = "", ...lines] = text.split("\n");253+ const [owner] = parseRecord(ownerLine);254+ if (owner !== undefined && trackedProcesses(rows, new Map([owner])).has(owner[0])) continue;255+ const targets = trackedProcesses(rows, parseRecord(lines.join("\n")));256+ if (targets.size > 0) {257+ yield* Effect.logWarning("Stopping agent processes left by a previous server", {258+ count: targets.size,259+ });260+ yield* terminate(targets, rows);@@ -0,0 +279,21 @@279+ return Effect.gen(function* () {280+ while (sessions.size > 0) {281+ yield* Effect.sleep(platform === "win32" ? WINDOWS_POLL_INTERVAL : POSIX_POLL_INTERVAL);282+ yield* refresh;283+ }284+ }).pipe(285+ Effect.ensuring(Effect.sync(() => (polling = false))),286+ Effect.forkIn(layerScope),287+ Effect.asVoid,288+ );289+ });290+291+ const adopt: AgentScopeShape["adopt"] = Effect.fn("AgentScope.adopt")(function* (unit, pid) {292+ const threadId = threadByUnit.get(unit);293+ if (threadId === undefined) return;294+ yield* ready;295+ yield* lock.withPermits(1)(296+ Effect.gen(function* () {297+ const rows = yield* readTable;298+ const root = rows?.find((row) => row.pid === pid);299+ if (rows === undefined || root === undefined || !threadByUnit.has(unit)) return;
c-ehrlich(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed