Skip to content

fix: every tracked process names its chat; stop ended cron runs' processes and survivors' later children - #201315

Open
obviyus wants to merge 24 commits into
mainfrom
openclaw/faint-krill
Open

obviyus wants to merge 24 commits into
mainfrom
openclaw/faint-krill

Conversation

@obviyus

@obviyus obviyus commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Related: #201157, #199079

What Problem This Solves

#201157 made a hidden subagent's task end and archive stop the chat's processes. Four gaps of the same class remained, all reproduced on isolated Gateways:

  • Isolated cron runs. A cron run session (agent:<id>:cron:<job>:run:<id>) takes no turn after its run, but its background: true services, yieldMs hand-offs and nohup … & survivors kept running until the session reaper ran. The run session is hidden, so nothing showed them. Reproduced with openai/gpt-6.2-sol: after a successful cron run, its HTTP server and nohup process were still alive.
  • Children a survivor forks later. Adopted survivors were stopped per pid. A survivor that starts a child after adoption (a Gateway respawning a worker) left that child running untracked after chat end.
  • macOS without the service marker. An unmanaged macOS Gateway did not hand survivors off, so they were neither tracked nor stopped.
  • The bad state was representable. ProcessSession.scopeKey was optional, so a process could be tracked without an owning chat (adoptSurvivor passed scopeKey ?? "", stopSessionExecProcesses filtered !== undefined, resolveProcessToolScopeKey could return undefined), and records were built and registered separately.

Why This Change Was Made

The chat stays the one owner of its processes, and stopSessionExecProcesses stays the one stop.

  • Ratchet. ProcessSession.scopeKey is a required string. registerProcessSession(params) is the only exported way to track a process; it replaces createProcessSession plus addSession at the four spawn paths (exec runtime, survivor adoption, durable restore, lost-ordinary report). resolveProcessToolScopeKey always returns a scope; an exec tool without a session resolves its agent once through the session-agent owner (resolveExecToolScopeKey, used by tool assembly and createExecTool). runExecProcess and processGatewayAllowlist require the scope, so a spawn call without one does not compile.
  • Teardown census. The shared per-file test teardown fails a file that leaves a live process (not durable, no stop requested) in the scope of a hidden subagent whose rows have all ended, and kills it.
  • Group stop. A survivor carries the process group it ran in at hand-off. Chat end and Gateway stop signal that group (SIGTERM, 5 s grace, SIGKILL) while the survivor still runs in it and it is not the Gateway's own group. process kill on one survivor still stops only that process, so it cannot take siblings with it.
  • Relay on every POSIX host. Agent exec owns its process tree on macOS too, not only on Linux (where it was added for the memory budget, fix: allow large Linux host builds to use the shared memory allowance #193044) or under a service marker. The memory budget stays Linux-only.
  • Cron run sessions. The isolated run's context disposal (moved from run.ts to session-cleanup.ts) stops the :run: session's processes. A yield on the run's commands resumes inside executeCronRun, so it has settled by then. Persistent cron targets are not :run: sessions and are unchanged.
  • canStop: false (audit note) is not a defect: openclaw gateway call connects with least-privilege scopes (operator.read for sessions.processes.list), and canStop correctly says that client may not stop. A write-scoped client (the Control UI) gets true, as session-processes.test.ts asserts. No UI change.

User Impact

  • An isolated cron run's services and leftover processes stop when the run ends. To keep one, use durable: true.
  • At chat end (reset, delete, archive, subagent or cron run end) and Gateway stop, a leftover process's later children stop with it.
  • On a macOS Gateway started outside launchd, cmd & and nohup survivors are tracked, listed by process list, and stopped at chat end, as they already were on Linux and under launchd.
  • Main chats, visible spawned sessions, yielded subagents and the one-shot agent --local hand-off are unchanged.

Evidence

Tests with real child processes, red on origin/main 9d6f4a94adf5, green here:

  • subagent-task-end-processes.lifecycle.test.ts: the survivor now forks a child after adoption. Base: all 5 stop rows fail with that child alive; branch: all pass. New row "complete without a service marker": base leaves the survivor and its child alive; the branch stops both.
  • run.session-processes.test.ts (enters runCronIsolatedAgentTurn): base leaves the run session's service alive; the branch stops it.
  • subagent-process-census.test.ts: the census reports an ended subagent's live process and ignores a running subagent's or one already being stopped. The census ran over the subagent and exec suites with no leak found.
  • Ratchet: tsgo core, core tests and extensions pass. Removing scopeKey from a registerProcessSession or runExecProcess call fails to compile (Property 'scopeKey' is missing).
  • Consumers of the process-registry API (95 files) and the cron run.* suites (39 files): green. Three failures in that run were checked on base: two are load-timing flakes that also fail on base, and one matches the worktree path (it contains "process").
  • Full build with declarations and a packed install smoke (pnpm pack, global install, --version, cron add --help).

Fault matrix on isolated Gateways (env -i, scratch state, service marker unless noted, scripted model). R2 yieldMs hand-off, R3 background: true, R4 nohup &, R5 child a survivor forks after adoption, R6 setsid double fork. Alive after the cell:

Cell Base Branch
Isolated cron run completed R2 R3 R4 R5 R6 R6
Subagent kill R5 R6 R6
Parent delete, child running R5 R6 R6
Archive R5 R6 R6
Subagent completion / failure, parent delete after completion R6 R6 (#201157 cells stay clean)
Subagent yielded on its hand-off R2 kept, resumed R2 kept, resumed
No marker: subagent completion R4 R5 R6 (untracked) R6
Main chat completion all alive, counted unchanged by design

Real model (openai/gpt-6.2-sol, isolated Gateway): an isolated cron job ran a background: true HTTP server, a nohup process and a foreground command; the run ended ok. Base: server and survivor alive afterwards. Branch: nothing left. ps excerpts are in the comment below.

The live Gateway was untouched (launchd pid unchanged, config equal to its backup), and every process the proof started is gone.

Merge danger and blast radius

  • Two-way door: no schema, stored row, protocol or config change. Durable rows written before this keep loading (scopeKey falls back to the row's sessionKey).
  • Cron jobs whose isolated runs started services meant to outlive the run lose them at run end; durable: true keeps them.
  • Group stop signals only a group created for the command; it is never the Gateway's group, and the survivor must still be in it. Accepted edge: a group that empties in the instant between the survivor's exit and the final SIGKILL could be reused.
  • macOS agent exec now runs through the relay anchor: one small Node process per command, as on Linux and launchd-managed macOS.
  • Size: production +324/−188. About 60 of those lines are re-indentation; the remaining growth is group stop (~50), the cron stop (+49 in session-cleanup.ts, −34 in run.ts) and the scope resolver. Tests and fixtures: about +380/−140, mostly mechanical fixture moves.

obviyus and others added 9 commits November 18, 2026 19:17
ProcessSession.scopeKey is a required string and registerProcessSession is
the only exported way to track a process (it replaces createProcessSession
plus addSession). The four spawn paths (exec runtime, survivor adoption,
durable restore, lost ordinary report) register through it, and
resolveProcessToolScopeKey always returns a scope. A session-less exec tool's
processes belong to the session's agent, resolved once in
resolveExecToolScopeKey. Tests build and track fixtures through the registry
test API.

Co-authored-by: obviyus <[email protected]>
The shared per-file teardown checks both registries through their test
APIs: a live, non-durable process with no stop requested that belongs to a
hidden subagent whose rows have all ended fails the file and is killed.

Co-authored-by: obviyus <[email protected]>
Survivors carry the process group they ran in at hand-off. Chat end and
Gateway stop signal that group (SIGTERM, grace, SIGKILL) while the survivor
still runs in it and it is not the Gateway's own group, so children a
survivor forked after adoption stop with it. A single process kill still
stops only that process.

Co-authored-by: obviyus <[email protected]>
Agent exec owns its process tree on every POSIX host, so a command's
survivors are handed off to the service-child relay and adopted by the chat
on an unmanaged macOS Gateway too. Before, only Linux (for its memory budget,
#193044) and marked services did; elsewhere survivors were neither tracked
nor stopped. The shared memory budget stays Linux-only.

Co-authored-by: obviyus <[email protected]>
An isolated cron run session (:run:) takes no turn after its run, so its
services and leftover processes had no manager until the session reaper ran.
The run-context disposal now stops them through stopSessionExecProcesses,
unless the run yielded on a command whose exit continues it. The disposal
moves to session-cleanup.ts beside the other run-session cleanup.

Co-authored-by: obviyus <[email protected]>
A yield on the run's commands resumes inside executeCronRun, so by the
time the run context is disposed the run has ended; an awaited command still
alive then belongs to a wait the executor gave up.

Co-authored-by: obviyus <[email protected]>
…esses and survivors' later children

OpenClaw-Publication: 2e40d2b9-63b0-857b-ae29-59e8afd582c5
@clawsweeper

clawsweeper Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@obviyus

obviyus commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Detailed evidence

ps excerpts, base → branch (scripted model, service marker)
### variant=base-svc cell=c1 (isolated cron run completed) at 20:08:04
--- ps (LA_c1 tree)
36619 36615 36610       00:11 perl -e sleep 1800 LA_c1_R3_bgservice
36620     1 36611       00:11 perl -e sleep 1800 LA_c1_R4_adopted
36621 36616 36609       00:11 perl -e sleep 1800 LA_c1_R2_yield
36624     1 36613       00:11 sh -c sleep 3; perl -e "sleep 1800" LA_c1_R5_late & wait LA_c1_R5_parent
36628     1 36627       00:11 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_c1_R6_setsid
36671 36624 36613       00:08 perl -e sleep 1800 LA_c1_R5_late
### variant=head-svc2 cell=c1 (isolated cron run completed) at 20:55:06
--- ps (LA_c1 tree)
98032     1 98031       00:12 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_c1_R6_setsid
{"key": "agent:main:cron:52a6…", "status": null, "backgroundProcessCount": null}

### variant=base-svc cell=s3 (after sessions.abort(child) [subagent kill]) at 20:05:33
--- ps -eo pid,ppid,pgid,etime,command (LA_s3 tree)
34576     1 34575       00:19 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_s3c_R6_setsid
34639     1 34561       00:16 perl -e sleep 1800 LA_s3c_R5_late
{"key": "agent:main:explicit:la-s3", "parent": null, "status": "done", "hasActiveRun": false, "backgroundProcessCount": null, "archived": false, "suba
{"key": "agent:main:subagent:9762…", "parent": "agent:main:explicit:la-s3", "status": "killed", "hasActiveRun": false, "backgroundProcessCount": nul
### variant=head-svc cell=s3 (after sessions.abort(child) [subagent kill]) at 20:11:06
--- ps -eo pid,ppid,pgid,etime,command (LA_s3 tree)
39674     1 39673       00:19 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_s3c_R6_setsid
{"key": "agent:main:explicit:la-s3", "parent": null, "status": "done", "hasActiveRun": false, "backgroundProcessCount": null, "archived": false, "suba
{"key": "agent:main:subagent:a783…", "parent": "agent:main:explicit:la-s3", "status": "killed", "hasActiveRun": false, "backgroundProcessCount": nul

### variant=base-plain cell=s8 (subagent normal completion) at 20:15:25
--- ps -eo pid,ppid,pgid,etime,command (LA_s8 tree)
47296     1 47291       00:10 perl -e sleep 1800 LA_s8c_R4_adopted
47298     1 47295       00:10 sh -c sleep 3; perl -e "sleep 1800" LA_s8c_R5_late & wait LA_s8c_R5_parent
47303     1 47302       00:10 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_s8c_R6_setsid
47406 47298 47295       00:07 perl -e sleep 1800 LA_s8c_R5_late
{"key": "agent:main:explicit:la-s8", "parent": null, "status": "done", "hasActiveRun": false, "backgroundProcessCount": null, "archived": false, "suba
{"key": "agent:main:subagent:ae1f…", "parent": "agent:main:explicit:la-s8", "status": "done", "hasActiveRun": false, "backgroundProcessCount": null,
### variant=head-plain cell=s8 (subagent normal completion) at 20:16:05
--- ps -eo pid,ppid,pgid,etime,command (LA_s8 tree)
48729     1 48728       00:10 perl -MPOSIX -e exit if fork; POSIX::setsid(); exit if fork; sleep 1800 LA_s8c_R6_setsid
{"key": "agent:main:explicit:la-s8", "parent": null, "status": "done", "hasActiveRun": false, "backgroundProcessCount": null, "archived": false, "suba
{"key": "agent:main:subagent:d467…", "parent": "agent:main:explicit:la-s8", "status": "done", "hasActiveRun": false, "backgroundProcessCount": null,
Real model: isolated cron run, openai/gpt-6.2-sol
=== base-real
### before: the cron run is still in its foreground command (20:11:33)
40904 40903 40898       00:05 node -e require('http').createServer((q,s)=>s.end('ok')).listen(0,'127.0.0.1') RM_cron_R3_server
40954     1 40952       00:03 perl -e sleep 1800 RM_cron_R4_adopted
40994 40993 40991       00:01 perl -e sleep 20 RM_cron_R1_fg
### after: the cron run finished (20:12:13)
40904 40903 40898       00:45 node -e require('http').createServer((q,s)=>s.end('ok')).listen(0,'127.0.0.1') RM_cron_R3_server
40954     1 40952       00:43 perl -e sleep 1800 RM_cron_R4_adopted
{"status": "ok", "sessionKey": "agent:main:cron:1e71…:run:ffe6a21f-dd3b-40c3-8cf8-94805fa70d9f", "summary": "done"}
=== head-real
### before: the cron run is still in its foreground command (20:10:15)
39046 39045 39044       00:07 node -e require('http').createServer((q,s)=>s.end('ok')).listen(0,'127.0.0.1') RM_cron_R3_server
39084     1 39081       00:04 perl -e sleep 1800 RM_cron_R4_adopted
39115 39114 39113       00:00 perl -e sleep 20 RM_cron_R1_fg
### after: the cron run finished (20:10:56)
{"status": "ok", "sessionKey": "agent:main:cron:3b4c…:run:ead25152-6b53-482e-8544-eeb6d4f138bc", "summary": "done"}
Ratchet: a registration without a scope does not compile
src/agents/bash-process-adopted.ts(…): error TS2741: Property 'scopeKey' is missing in type '{ id: string; command: st

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: XL labels Nov 18, 2026
obviyus and others added 3 commits November 18, 2026 21:25
…bers

When the survivor exits on SIGTERM before its group, the members it left
keep the rest of the shared grace before the group is forced.

Co-authored-by: obviyus <[email protected]>
OpenClaw-Publication: 1dc87eeb-cb87-8589-8da8-6eea3950a2e5
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 18, 2026
obviyus and others added 9 commits November 18, 2026 21:42
Only ESRCH proves a survivor's process group empty; EPERM means a member
this Gateway cannot signal still runs. After the grace and SIGKILL, the
stop confirms the group's extinction and otherwise reports uncertain, so
the adopted record and the chat-end failure stay visible.

Observed proof for this PR (isolated Gateways from dist, env -i, scratch
state; base origin/main 9d6f4a9):
- Isolated cron run, real model openai/gpt-6.2-sol: base left the run's
  background HTTP server and nohup process alive after the run ended ok;
  this branch leaves nothing.
- Scripted-model fault matrix through the Gateway: isolated cron run end
  base R2/R3/R4/R5/R6 alive -> branch only the documented setsid R6;
  subagent kill, parent delete and archive: a survivor's later child
  stayed alive on base and is stopped here; macOS without the service
  marker: base left survivors untracked, branch stops them.
- Real-process tests: lifecycle contract table, cron run session,
  group-grace and group-denied cases, red on base or the previous head.

Co-authored-by: obviyus <[email protected]>
OpenClaw-Publication: 8ceac546-c2a9-87f3-9632-bad0a9f3671a
A survivor that ignores SIGTERM reaches the SIGKILL branch; its group's
extinction is now confirmed there as after the grace, so a member the
Gateway cannot signal leaves the stop uncertain.

Co-authored-by: obviyus <[email protected]>
OpenClaw-Publication: 937f6c50-e514-8c30-9623-9b1c8ad0a1e7
An unconfirmed group stop keeps the group on the adopted record: the reaper
settles it only once the group is empty, and a later stop retries the group
instead of reporting success because the original pid exited.

Co-authored-by: obviyus <[email protected]>
OpenClaw-Publication: f34eba88-7033-8a43-99a1-33bc7a823604
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 18, 2026
@clawsweeper

clawsweeper Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: stale review; fresh review needed.

Summary
The latest durable ClawSweeper review was for head e4f8bc756cb083a56285895094a63cd2cd2f7d8d, but the PR head is now 9c5262782724feb9e1c18fb6abf7ebd8f8f5221c. Its old verdict and PR readiness labels are no longer current.

Next step
Comment @clawsweeper re-review when the current head is ready; pushes after the first review are not reviewed automatically.

Review history (1 earlier review cycle)
  • reviewed 2026-11-18T16:58:35.663Z sha e4f8bc7 :: needs changes before merge. :: [P2] Migrate the remaining registry consumers before removing the exports | [P2] Supply scopes in every remaining direct exec caller | [P2] Make the survivor fixture independent of transient process counts

obviyus and others added 3 commits November 18, 2026 23:03
@openclaw-barnacle openclaw-barnacle Bot added the scripts Repository scripts label Nov 18, 2026
@clawsweeper clawsweeper Bot removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. scripts Repository scripts size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants