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

validateProcessIdentity() treats a denied ps probe as process death — same defect as #79, unfixed by #80

Open behruznassre opened this issue 4 months ago • 2 comments

Summary

validateProcessIdentity() collapses "the probe was denied" into "the process is dead", the same defect #79 reported in isProcessAlive(). It is latent rather than live, but only by accident, and #80 does not fix it.

The code

scripts/lib/process.mjs:

export function getProcessIdentity(pid) {
  if (process.platform === 'darwin') {
    const row = runCommandChecked('ps', ['-o', 'lstart=,comm=', '-p', String(pid)]);
    return row.stdout.trim();
  }
  ...
}

export function validateProcessIdentity(pid, expectedIdentity) {
  try {
    return getProcessIdentity(pid) === expectedIdentity;
  } catch {
    return false;      // <- denial and absence are indistinguishable
  }
}

scripts/lib/state.mjs:449 prefers this branch whenever the job recorded an identity:

const alive = job.pidIdentity
  ? validateProcessIdentity(job.pid, job.pidIdentity)
  : isProcessAlive(job.pid);
if (alive) return job;

Why it matters

Inside a Codex workspace-write sandbox, spawnSync ps fails with EPERM unconditionally — not merely for non-descendants, the way the zero-signal probe does. Measured on macOS 25.5.0 / codex-cli 0.145.0:

E. spawnSync ps: ERROR EPERM

So on that path getProcessIdentity() always throws, validateProcessIdentity() always returns false, and every reapable job past the 2s grace period is auto-reaped as dead regardless of whether it is running.

Why it is not firing today

scripts/lib/claude-cli.mjs:799-804 swallows the identical failure at spawn time:

let pidIdentity = null;
try {
  pidIdentity = getProcessIdentity(proc.pid);
} catch {
  // Best-effort — may fail on some platforms
}

In the sandbox that catch always runs, so jobs are persisted with pidIdentity: null, and reapStaleJobs() falls through to isProcessAlive() — the branch #80 fixes. The identity branch is dead code in exactly the environment where it would misbehave.

That is a coincidence of two independent failures cancelling, not a design. It breaks if any of the following change:

  • a job is spawned where ps is permitted and reaped where it is not (mixed sandbox/non-sandbox invocations against the same state dir)
  • getProcessIdentity() gains a non-ps implementation on darwin, so identities start being recorded in the sandbox
  • the spawn-time catch is tightened to persist a partial identity instead of null

In any of those, live jobs are reaped again and #80 does not help, because the identity branch never consults isProcessAlive().

Suggested fix

Distinguish denial from absence at the same boundary #80 established, rather than at the call site:

  • have getProcessIdentity() propagate a typed "probe denied" signal instead of a bare throw (or have validateProcessIdentity() inspect error.code === 'EPERM')
  • on denial, return the same verdict isProcessAlive() now returns for EPERM — treat the process as present and let the caller keep the job
  • keep genuine mismatch and ESRCH classified as not alive, so PID-reuse detection is unweakened
  • cover both branches deterministically, as #80 does, via an injectable command impl

An alternative worth considering: fall back to isProcessAlive(pid) when the identity probe is denied, accepting the loss of PID-reuse safety only in the case where identity cannot be established at all. That keeps one liveness answer rather than two.

Related

  • #79 — the reported bug
  • #80 — fixes the isProcessAlive() branch only

behruznassre avatar Jul 26 '26 15:07 behruznassre

I would model this as a tri-state probe: alive, dead, or unknown. EPERM is unknown, not proof of life; the reaper may transition only on dead (ESRCH or a verified identity mismatch). On unknown, keep the job non-terminal, record that identity verification was unavailable, and rely on a bounded secondary signal such as a supervisor heartbeat or completion record. Falling back to kill(pid, 0) avoids the false death but quietly drops the PID-reuse guarantee, so that degradation should be explicit in the job receipt.

If you exercise EPERM, ESRCH, and PID-reuse cases through MartinLoop, send us the receipts so we can pressure-test whether the evidence distinguishes them cleanly: 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