Skip to content

fix(agent-server): spare operator docker-exec sessions from the orphan sweeper (#1153) - #1154

Merged
vybe merged 1 commit into
devfrom
feature/1153-spare-docker-exec-orphans
Jun 11, 2026
Merged

vybe merged 1 commit into
devfrom
feature/1153-spare-docker-exec-orphans

Conversation

@dolho

@dolho dolho commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Summary

The periodic cgroup orphan sweeper (#817) SIGKILLed operator docker exec sessions within 0–30s — the same class of actor the allowlist already protects for SSH. docker exec agent-X sh -c '...sleep 60' died with exit 137; weeks of FAISS index-build failures were misdiagnosed as OOM/capacity (a host was upsized for nothing). It also exposed the platform's own exec paths (pre-check hooks, git recovery, session-JSONL reaping, agent terminal — all docker exec).

Root cause

A docker exec entry process matches none of the allowlist sources (no execution id, no TRINITY_EXECUTION_ID, no shared pgid, not sshd) → reaped as an orphan. But it's exactly the operator-debugging case _ssh_session_pids() exists to protect.

Fix

  • orphan_allowlist._docker_exec_session_pids() — inside the container PID namespace a docker exec entry has PPID 0 (real parent is the containerd shim, outside the namespace); genuinely-leaked orphans reparent to PID 1, never 0. PPID 0 is an unspoofable "operator exec session" signal. Allowlist every PPID-0 process (excluding PID 1, already hard-protected) + its descendant tree. Wired into resolve_allowlist. When the session exits, leftover children reparent to PID 1 → fall out of the set → reaped next sweep (the bug: Runaway subprocess after task failure makes agent-server unresponsive, triggering persistent CB cascade #817 guarantee is preserved).
  • orphan_sweep — each real kill logs at WARNING with pid + a length-capped (200) cmd= (was INFO, count-only summary in the loop). dry_run stays INFO. One such line turns a weeks-long "which process keeps dying?" into a 5-minute fix.
  • Docs — TRINITY_COMPATIBLE_AGENT_GUIDE.md gains a "Long-Running & Background Processes (the orphan sweeper)" section: the ~/.trinity/persistent-processes.allow escape hatch and "run maintenance as a platform execution" guidance.

Verification

Unit — tests/unit/test_1153_docker_exec_allowlist.py (5): PPID-0 entry + descendants protected; PID 1 (and its children) excluded; reparented orphan not protected; resolve_allowlist end-to-end; post-exit child reaped. #912 suite still green (13 passed together).

Live (fresh trinity-agent-base:0.6.0 agent, real cgroup sweep mid-flight, sweep_pid = agent-server's pid as in production):

Process Result
30s docker exec loop (PPID 0) spared → exit 0, all 6 outputs
planted sh -c sleep 300 (PPID 1, reparented) reaped ✅
sudo .../sshd spared (SSH unchanged)
python3 /app/agent-server.py spared

AC

  • Long docker exec survives sweep cycles, exits 0
  • Children left behind after session exit ARE reaped (reparented to PID 1)
  • SSH-session protection unchanged
  • Each kill logs one WARNING with pid + length-capped cmdline
  • Unit tests cover the PPID-0 branch (entry + descendants, PID 1 exclusion)
  • Agent-container docs describe the sweeper, the escape hatch, and the "maintenance as platform executions" guidance

Note: agent-runtime change → base image rebuilt (trinity-agent-base:0.6.0/latest); takes effect on newly created/restarted agent containers.

Related to #1153

🤖 Generated with Claude Code

…n sweeper (#1153)

The periodic cgroup orphan sweeper (#817) SIGKILLed operator `docker exec`
sessions within 0–30s — the same class of actor (an operator/platform process
maintaining the agent) that the allowlist already deliberately protects for
SSH. A long `docker exec agent-X sh -c '...sleep...'` died with exit 137; weeks
of FAISS index-build failures were misdiagnosed as OOM/capacity.

Root cause: a `docker exec` entry process matches none of the allowlist sources
(no execution id, no env tag, no shared pgid, not sshd) and was reaped as an
orphan — yet it is exactly the SSH-session case.

Fix:
- orphan_allowlist: new `_docker_exec_session_pids()` — inside the container PID
  namespace a `docker exec` entry has **PPID 0** (real parent is outside the
  namespace); genuinely-leaked orphans reparent to **PID 1**, never 0, so PPID 0
  is an unspoofable "operator exec session" signal. Allowlist every PPID-0
  process (excluding PID 1, already hard-protected) + its descendant tree.
  Wired into `resolve_allowlist`. When the session exits, leftover children
  reparent to PID 1 and fall out → reaped next sweep (#817 guarantee preserved).
- orphan_sweep: each real kill now logs at **WARNING** with `pid` + a
  length-capped `cmd=` (was INFO). One such line turns the weeks-long
  "which process keeps dying?" misdiagnosis into a 5-minute fix. dry_run stays
  INFO. cmdline capped at 200 chars.
- docs: TRINITY_COMPATIBLE_AGENT_GUIDE gains a "Long-Running & Background
  Processes (the orphan sweeper)" section — the `persistent-processes.allow`
  escape hatch and "run maintenance as a platform execution" guidance.

Tests: tests/unit/test_1153_docker_exec_allowlist.py (5) — PPID-0 entry +
descendants protected, PID 1 (and its children) excluded, reparented orphan
not protected, resolve_allowlist end-to-end, post-exit child reaped. #912
suite still green (13 passed together).

Live-verified on a fresh trinity-agent-base:0.6.0 agent: a 30s `docker exec`
loop survived a real mid-flight sweep and exited 0; agent-server + sshd spared
under the production sweep_pid; a planted PPID-1 orphan was reaped.

Related to #1153

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@dolho
dolho requested a review from vybe June 11, 2026 12:17

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr — focused P1 reliability fix, unspoofable PPID-0 signal, full unit + live verification, CI green, security scan clean. Approving.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants