cc-plugin-codex icon indicating copy to clipboard operation
cc-plugin-codex copied to clipboard

Background jobs are auto-reaped when sandbox denies process liveness probe

Open behruznassre opened this issue 4 months ago • 5 comments

Summary

On macOS Codex sessions, a background review can finish successfully and write its complete output to the job log, but the tracker later reports it as failed:

Process <pid> died without completing. Auto-reaped.

Environment

  • cc-plugin-codex 1.3.0 installed as cc@sendbird
  • macOS
  • Codex workspace-write sandbox

Confirmed evidence

Five background reviews exhibited the same sequence. For one representative job:

  • created: 2026-07-20T18:40:08.075Z
  • auto-reaper completedAt: 2026-07-20T18:40:53.990Z
  • final Claude output appended to the log: 2026-07-20T18:44:55.274Z
  • final stored status: failed
  • final stored phase: running
  • error: Process 70888 died without completing. Auto-reaped.

The full review in the log proves the Claude process continued for about four minutes after the tracker declared it dead.

Inside the same Codex sandbox, the process probes used by the tracker fail with permission errors:

$ ps -o pid=,lstart=,comm= -p 1
zsh: operation not permitted: ps

$ node ...getProcessIdentity(process.pid)...
Error: spawnSync ps EPERM

Root cause

scripts/lib/process.mjs:isProcessAlive() catches every process.kill(pid, 0) error and returns false. Under a sandbox, EPERM means the process probe is not permitted; it does not mean that the process is absent.

scripts/lib/state.mjs:reapStaleJobs() interprets that false as confirmed process death and CAS-transitions the running job to failed. When Claude later finishes, runTrackedJob() correctly refuses to replace the already-terminal failed state, so the result remains only in the log.

Expected behavior

Treat EPERM from a zero-signal process probe as evidence that the process exists but cannot be signaled. Only ESRCH/a genuinely absent PID should be treated as dead.

Proposed fix

  • Make isProcessAlive() return true for EPERM.
  • Add deterministic unit coverage for the EPERM and ESRCH branches.

This stays at the shared process-probe boundary and avoids weakening the PID-identity checks used for cancellation.

behruznassre avatar Jul 21 '26 00:07 behruznassre

A sandbox denying a liveness probe should not be interpreted as proof the work is dead. That turns an observability limitation into destructive control.

Agent systems need a third state: unknown, with a bounded recovery path. We carry that distinction into MartinLoop run exits so a tool cannot silently convert uncertainty into failure.

What evidence would you require before reaping a background job? I would appreciate your critique of our open-source model: https://github.com/Keesan12/martin-loop

Keesan12 avatar Jul 21 '26 19:07 Keesan12

I ran the probes inside a real Codex workspace-write sandbox to settle what is actually denied. The mechanism in my original report was right, but the reasoning was not — the sandbox does not deny the zero-signal probe wholesale.

process.kill(pid, 0), with an unsandboxed baseline on the same machine (macOS 25.5.0, codex-cli 0.145.0):

Case Unsandboxed In workspace-write sandbox
Live own descendant alive alive
Live non-descendant, same uid alive EPERM
Dead own descendant ESRCH ESRCH
Dead non-descendant ESRCH ESRCH
Nonexistent PID ESRCH ESRCH
spawnSync ps ok EPERM (always)

Two corrections to the report above:

  1. The denial is scoped to non-descendants, not to the probe itself. Seatbelt denies signalling processes outside the sandbox. reapStaleJobs() runs in a separate invocation from the one that spawned the job, so the PID it probes is a non-descendant — which is exactly the row that returns EPERM. A job probing its own child never sees this.

  2. The kill(1, 0) evidence I cited proves nothing. It returns EPERM on plain macOS with no sandbox at all, because a non-root user cannot signal root's init. I have replaced it in #80 with the non-descendant probe.

What evidence would you require before reaping a background job?

For this specific tracker, the EPERM/ESRCH distinction is sufficient, and the table shows why: a dead non-descendant PID still returns ESRCH inside the sandbox. The kernel resolves process existence before the sandbox policy denies the signal, so the two error codes continue to discriminate live from dead even under denial. EPERM → alive, ESRCH → dead is therefore total here, not a heuristic.

That matters for the concern about converting uncertainty into failure. It is a real failure mode in general, and worth designing against where the signal genuinely collapses. It does not bind here, because the sandbox never actually produces the ambiguous case — there is no state in the table where a live and a dead process are indistinguishable. Had EPERM masked dead PIDs, the consequence would have been the mirror-image bug and arguably worse: REAP_GRACE_MS is 2s and there is no max-duration bound, so a crashed job would sit in running forever with nothing to recover it.

The residual risk is not in isProcessAlive() at all. validateProcessIdentity() swallows every error including EPERM, and it wraps getProcessIdentity() → spawnSync ps, which the sandbox denies unconditionally (last row). reapStaleJobs() prefers that branch whenever job.pidIdentity is set. It happens to be unreachable today only because claude-cli.mjs swallows the same ps failure at spawn time and leaves pidIdentity: null. Filed separately.

behruznassre avatar Jul 26 '26 15:07 behruznassre

That is a very useful correction. You’re right: with EPERM for live non-descendants and ESRCH for dead or nonexistent processes, liveness is not ambiguous on this path. The actual risk moved to identity validation: if ps is denied and every error is swallowed, the system can know a PID exists but cannot prove it still belongs to the original job. I would make liveness and identity separate receipt fields and block destructive reap only when identity is missing or stale—not reclassify the process as dead.

If you’re willing, run the pidIdentity failure through MartinLoop and post the receipt so we can see whether that distinction survives the handoff: https://github.com/Keesan12/martin-loop

Keesan12 avatar Jul 27 '26 02:07 Keesan12

Thanks again for testing the boundary instead of leaving it as an assumption—the EPERM/ESRCH evidence materially improved the safety model. We’re close to 50 stars; if MartinLoop’s explicit liveness-and-identity receipt approach is useful, would you star the repo to help practical agent governance reach more builders? https://github.com/Keesan12/martin-loop

Keesan12 avatar Jul 27 '26 17:07 Keesan12

Shipped in v1.4.0, now on npm as [email protected] and in the sendbird/codex-marketplace snapshot. Update with $cc:setup after your Codex plugin cache refreshes.

Note that 1.4.0 also fixes a separate Codex 0.145 incompatibility (#87): spawn_agent stopped advertising agent_type and dropped gpt-5.4-mini from its child-model catalog, both of which the built-in forwarding paths pinned. If you saw background review/rescue spawns failing on 1.3.0 beyond the sandbox issues above, that was the cause.

sf-jin-ku avatar Jul 29 '26 07:07 sf-jin-ku