Files
PaperClipAI/server/src/services/native-runtime/remote-runner-recovery-diagnostics.test.ts
T
DottaandClaude Opus 5.5 ca34aca777 fix(native): report a Runner caught mid-exit as not running (#21987)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A warm agent session keeps its Runner alive in a sandbox between
turns. The server closes an idle session after 5 minutes. A monitor loop
polls the Runner process with `REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT`
until the process exits.
> - The check reads `/proc/<pid>/cmdline` and exits 48
(`process_runner_id_missing`) when the command line does not hold
`--runner-id`.
> - A Linux process that is exiting releases its memory before it
becomes a zombie. For that short time its command line reads as empty,
but the process still answers `kill -0` and its stat is not yet `Z`.
> - So a check that lands in that window reports exit 48, and the close
fails although the Runner is exiting as asked.
> - This pull request makes the check wait up to 1 second for a process
of the saved generation whose command line is empty. If the process
becomes a zombie or goes away, the check exits 45
(`process_not_running`).
> - The benefit is that an idle warm close no longer fails, and is no
longer logged as a failure, when it catches the Runner while it exits.

## Linked Issues or Issue Description

Refs #21853

**What happened?**
Restart-chaos rerun 11 A1 (master 22d3f9a03c, wave
w-20261117T084017-s7106) logged `idle warm native session close failed`
with `runner_remote_process_identity_unavailable
[process_verification/process_runner_id_missing]` for run `6871fda4`
(t011, Codex, Daytona), close attempt 1, at 09:09:33Z.

**Evidence**
- The run succeeded at 09:04:34Z. The idle close ran 5 minutes later.
The server logged `transport_close_requested` at 09:09:33Z, then the
close failure in the same second.
- The Runner marker holds pid 714, with a saved process generation
(`linux:<boot>:<ticks>`). So the generation check passed, and the
process at pid 714 was still the Runner.
- The Runner's log-rotation watcher (`while kill -0 "$pid"; do sleep 15;
…`) started its last `sleep 15` at 09:09:21Z, so pid 714 was alive then.
The sandbox capture at 09:09:35Z shows no pid 714. So the Runner exited
between 09:09:21Z and 09:09:35Z, during the close.
- Exit 48 needs a live pid whose generation matches and whose command
line lacks `--runner-id`. The only way for the same process to lose its
command line is the exit window, after the kernel releases its memory
and before it becomes a zombie.
- The next evidence line shows the sandbox Runner `suspended`,
`settled`, identity matching, and no Runner process.
`warmCloseFailureProvesOwnerGone` accepted this proof (#21853) and
released the owner on attempt 1 (`warm native session's Runner or
sandbox is gone; released its owner`). No retry was needed.
`warm-shutdown.json` reports pass, 0 leaked sandboxes and the
environment deleted.

**Expected behavior**
A check that catches the Runner while it exits reports
`process_not_running`, so the monitor confirms the exit and the idle
close completes cleanly.

**Why the close failed spuriously**
The monitor loop in `native-session-executor.ts` accepts only exit 0
(alive, poll again) or exit 45 (exit confirmed). Any other exit code
throws `RemoteRunnerRecoveryError`, which fails the close.

## What Changed

-
`server/src/services/native-runtime/remote-runner-recovery-diagnostics.ts`:
in `REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT`, when the command line lacks
the runner id, is empty, and the saved process generation matched, the
check polls `/proc/<pid>/stat` and `kill -0` every 50 ms for up to 1
second. It exits 45 when the process becomes `Z` or `X`, or is gone.
Otherwise it exits 48, as before.
-
`server/src/services/native-runtime/remote-runner-recovery-diagnostics.test.ts`:
two regression tests. A process of the saved generation with an empty
command line and state `R` becomes a zombie, or is reaped, while the
check runs: the check exits 45. The existing cases are unchanged: a live
process with an empty command line (state `S`) still exits 48, and
without a saved generation a zombie still exits 48. The helper timeout
goes from 2 to 5 seconds, because the unchanged empty-command-line case
now waits 1 second.

## Verification

- `cd server && npx vitest run
src/services/native-runtime/remote-runner-recovery-diagnostics.test.ts
src/services/native-runtime/sandbox-runner-port-holders.test.ts`: 18
passed.
- Red on master: with the master script, both new tests fail with
`expected 48 to be 45`.
- `pnpm verify` selected the full server Vitest suite, because many
server modules import this file. I stopped that local run so that it did
not load the machine while a chaos wave ran. Required CI supplies the
broad coverage.- No chaos wave was run, and no Daytona sandbox was
touched. Rerun 11 holds the slot.

## Risks

- Low risk. The wait runs only when the command line is empty and the
saved generation matches, so it applies only to the Runner's own process
while it exits. A live process that keeps an empty command line still
exits 48, after 1 extra second.
- A check without a saved generation does not change, because there a
pid can be reused.
- The script changes no persisted state. Markers, state files and
checkpoints saved by the previous release are read the same way.
- The wait is in the recovery and close checks only. It adds no work
before `turn.start`.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits) for the log analysis, code and
tests.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 04:31:45 -06:00

212 lines
13 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
REMOTE_RECOVERY_MARKER_READ_SCRIPT, REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT, REMOTE_RUNNER_EXIT_EVIDENCE_MAX_CHARS,
REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, RemoteRunnerRecoveryError, boundRemoteRunnerExitEvidence, describeExitedRemoteRunnerReason,
readRemoteRunnerRecoveryDiagnostic, readRemoteRunnerRecoveryError, remoteRecoveryProbeReason,
} from "./remote-runner-recovery-diagnostics.js";
function shellExit(script: string, args: string[]) {
try {
execFileSync("sh", ["-c", script, "recovery-test", ...args], { stdio: "pipe", timeout: 5_000 });
return 0;
} catch (error) {
return (error as { status: number }).status;
}
}
describe("remote recovery diagnostic shell receipts", () => {
it("keeps permission-denied liveness uncertain", async () => {
const root = await mkdtemp(join(tmpdir(), "remote-probe-denied-"));
const marker = join(root, "marker");
const hook = join(root, "denied.cjs");
try {
await writeFile(marker, `nonce\n${process.pid}\n2026-10-26T00:00:00.000Z\nrunner\n`);
await writeFile(hook, 'process.kill = () => { throw Object.assign(new Error("denied"), { code: "EPERM" }); };');
const script = `NODE_OPTIONS="--require=${hook}"; export NODE_OPTIONS; kill() { return 1; }; ` + REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT;
expect(shellExit(script, [marker, "nonce", "runner", String(process.pid)])).toBe(46);
expect(() => process.kill(process.pid, 0)).not.toThrow();
} finally { await rm(root, { recursive: true, force: true }); }
});
it.each([
{ fingerprint: "linux:abcd-1234:100", code: 45 },
{ fingerprint: "linux:abcd-1234:101", code: 47 },
{ fingerprint: "", code: 48 },
])("requires the saved generation before accepting a zombie receipt (%j)", async ({ fingerprint, code }) => {
const root = await mkdtemp(join(tmpdir(), "remote-probe-zombie-"));
const marker = join(root, "marker");
try {
await writeFile(marker, `nonce\n${process.pid}\n2026-10-26T00:00:00.000Z\nrunner\n`);
await writeFile(join(root, "cmdline"), "");
await writeFile(join(root, "stat"), `${process.pid} (runner (child)) ${["Z", ...Array(18).fill("0"), 100].join(" ")}\n`);
await writeFile(join(root, "boot_id"), "abcd-1234\n");
const script = REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT
.replaceAll('"/proc/$pid/cmdline"', JSON.stringify(join(root, "cmdline")))
.replaceAll('"/proc/" + pid + "/cmdline"', JSON.stringify(join(root, "cmdline")))
.replaceAll('"/proc/" + pid + "/stat"', JSON.stringify(join(root, "stat")))
.replaceAll('"/proc/sys/kernel/random/boot_id"', JSON.stringify(join(root, "boot_id")));
expect(shellExit(script, [marker, "nonce", "runner", String(process.pid), fingerprint])).toBe(code);
} finally { await rm(root, { recursive: true, force: true }); }
});
it.each([
{ cmdline: ["paperclip-runnerd", "--runner-id", "runner"], code: 0 },
{ cmdline: ["paperclip-runnerd", "--state-dir", "/state"], code: 48 },
{ cmdline: [""], code: 48 },
])("reports a live process without its runner id apart from a changed marker (%j)", async ({ cmdline, code }) => {
const root = await mkdtemp(join(tmpdir(), "remote-probe-runner-id-"));
const marker = join(root, "marker");
try {
await writeFile(marker, `nonce\n${process.pid}\n2026-11-16T00:28:42.758Z\nrunner\n`);
await writeFile(join(root, "cmdline"), cmdline.join("\0"));
await writeFile(join(root, "stat"), `${process.pid} (runnerd) ${["S", ...Array(18).fill("0"), 100].join(" ")}\n`);
await writeFile(join(root, "boot_id"), "abcd-1234\n");
const script = REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT
.replaceAll('"/proc/" + pid + "/cmdline"', JSON.stringify(join(root, "cmdline")))
.replaceAll('"/proc/" + pid + "/stat"', JSON.stringify(join(root, "stat")))
.replaceAll('"/proc/sys/kernel/random/boot_id"', JSON.stringify(join(root, "boot_id")));
expect(shellExit(script, [marker, "nonce", "runner", String(process.pid), "linux:abcd-1234:100"])).toBe(code);
expect(remoteRecoveryProbeReason("process_verification", 48, false)).toBe("process_runner_id_missing");
await writeFile(marker, `nonce\n${process.pid}\n2026-11-16T00:28:42.758Z\nother-runner\n`);
expect(shellExit(script, [marker, "nonce", "runner", String(process.pid), "linux:abcd-1234:100"])).toBe(44);
expect(remoteRecoveryProbeReason("process_verification", 44, false)).toBe("process_identity_changed");
} finally { await rm(root, { recursive: true, force: true }); }
});
it.each([
{ exit: 'fs.writeFileSync(stat, real.call(fs, stat, "utf8").replace(" R ", " Z "))', name: "becomes a zombie" },
{ exit: "fs.rmSync(stat)", name: "is reaped" },
])("reports a Runner caught mid-exit, with its command line already released, as not running once it $name", async ({ exit }) => {
const root = await mkdtemp(join(tmpdir(), "remote-probe-exiting-"));
const marker = join(root, "marker");
const stat = join(root, "stat");
const hook = join(root, "exit-on-second-stat-read.cjs");
try {
await writeFile(marker, `nonce\n${process.pid}\n2026-11-17T08:51:21.931Z\nrunner\n`);
await writeFile(join(root, "cmdline"), "");
await writeFile(stat, `${process.pid} (MainThread) ${["R", ...Array(18).fill("0"), 100].join(" ")}\n`);
await writeFile(join(root, "boot_id"), "abcd-1234\n");
await writeFile(hook, [
'const fs = require("node:fs");',
`const stat = ${JSON.stringify(stat)};`,
"const real = fs.readFileSync;",
"let reads = 0;",
`fs.readFileSync = function (path, ...rest) { if (path === stat && ++reads === 2) ${exit}; return real.call(this, path, ...rest); };`,
].join("\n"));
const script = `NODE_OPTIONS="--require=${hook}"; export NODE_OPTIONS; ` + REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT
.replaceAll('"/proc/" + pid + "/cmdline"', JSON.stringify(join(root, "cmdline")))
.replaceAll('"/proc/" + pid + "/stat"', JSON.stringify(stat))
.replaceAll('"/proc/sys/kernel/random/boot_id"', JSON.stringify(join(root, "boot_id")));
expect(shellExit(script, [marker, "nonce", "runner", String(process.pid), "linux:abcd-1234:100"])).toBe(45);
} finally { await rm(root, { recursive: true, force: true }); }
});
it("distinguishes missing, unsafe and valid marker reads without exposing their contents", async () => {
const root = await mkdtemp(join(tmpdir(), "remote-recovery-read-"));
const marker = join(root, "marker");
try {
expect(shellExit(REMOTE_RECOVERY_MARKER_READ_SCRIPT, [marker])).toBe(31);
await writeFile(join(root, "target"), "private marker");
await symlink(join(root, "target"), marker);
expect(shellExit(REMOTE_RECOVERY_MARKER_READ_SCRIPT, [marker])).toBe(32);
await rm(marker);
await writeFile(marker, "private marker");
expect(shellExit(REMOTE_RECOVERY_MARKER_READ_SCRIPT, [marker])).toBe(0);
} finally { await rm(root, { recursive: true, force: true }); }
});
it("distinguishes a torn marker from a changed identity before signalling any process", async () => {
const root = await mkdtemp(join(tmpdir(), "remote-recovery-check-"));
const marker = join(root, "marker");
const args = [marker, "nonce", "runner", String(process.pid)];
try {
expect(shellExit(REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT, args)).toBe(41);
await writeFile(marker, "nonce\n");
expect(shellExit(REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT, args)).toBe(43);
await writeFile(marker, `other-nonce\n${process.pid}\n2026-10-14T00:00:00.000Z\nrunner\n`);
expect(shellExit(REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT, args)).toBe(44);
} finally { await rm(root, { recursive: true, force: true }); }
});
it("reads only closed labels through bounded causes and contains hostile getters", () => {
const diagnostic = { stage: "process_verification", reason: "process_generation_changed" } as const;
const error = new RemoteRunnerRecoveryError(diagnostic);
expect(readRemoteRunnerRecoveryError(new Error("private outer text", { cause: error }))).toEqual(diagnostic);
expect(readRemoteRunnerRecoveryError(Object.assign(new Error("private"), {
code: error.code, remoteRunnerRecovery: { stage: "private", reason: "process_generation_changed" },
}))).toBeNull();
expect(readRemoteRunnerRecoveryError(Object.defineProperty({}, "code", { get() { throw new Error("private"); } }))).toBeNull();
const cyclic: { cause?: unknown } = {};
cyclic.cause = cyclic;
expect(readRemoteRunnerRecoveryError(cyclic)).toBeNull();
});
it("reports a retired owned child as unavailable before checking its command line", async () => {
const root = await mkdtemp(join(tmpdir(), "remote-recovery-retired-"));
const marker = join(root, "marker");
const pid = execFileSync("sh", ["-c", "printf '%s' $$"], { encoding: "utf8" }).trim();
try {
await writeFile(marker, `nonce\n${pid}\n2026-10-14T00:00:00.000Z\nrunner\n`);
expect(shellExit(REMOTE_RECOVERY_PROCESS_CHECK_SCRIPT, [marker, "nonce", "runner", pid])).toBe(45);
} finally { await rm(root, { recursive: true, force: true }); }
});
});
describe("exited remote runner evidence", () => {
it("prints bounded tails of the exit diagnostic and redirected stderr, never through a symlink", async () => {
const root = await mkdtemp(join(tmpdir(), "remote-exit-evidence-"));
const directory = join(root, "diagnostics");
try {
expect(execFileSync("sh", ["-c", REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, "t", directory], { encoding: "utf8" })).toBe("");
await mkdir(directory);
await writeFile(join(directory, "runnerd.stderr.log"), "paperclip-runnerd: transport reconnect grace exceeded");
await writeFile(join(directory, "runnerd.process-stderr.log"), `${"x".repeat(10_000)}\nlast stderr line`);
const output = execFileSync("sh", ["-c", REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, "t", directory], { encoding: "utf8" });
expect(output).toContain("== runnerd.stderr.log\npaperclip-runnerd: transport reconnect grace exceeded");
expect(output).toContain("== runnerd.process-stderr.log\n");
expect(output).toContain("last stderr line");
expect(output.length).toBeLessThan(4_200);
// Each file keeps its share: a long stderr tail cannot crowd out the exit diagnostic.
const bounded = boundRemoteRunnerExitEvidence(output, text => text)!;
expect(bounded.length).toBeLessThanOrEqual(REMOTE_RUNNER_EXIT_EVIDENCE_MAX_CHARS);
expect(bounded).toContain("paperclip-runnerd: transport reconnect grace exceeded");
expect(bounded).toContain("last stderr line");
// The first candidate directory with evidence wins; a later shared directory is read only when it has none.
const shared = join(root, "shared");
await mkdir(shared);
await writeFile(join(shared, "runnerd.stderr.log"), "shared runner exit");
expect(execFileSync("sh", ["-c", REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, "t", directory, shared], { encoding: "utf8" })).not.toContain("shared runner exit");
expect(execFileSync("sh", ["-c", REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, "t", join(root, "missing"), shared], { encoding: "utf8" }))
.toBe("== runnerd.stderr.log\nshared runner exit\n");
await writeFile(join(root, "secret"), "private");
await rm(join(directory, "runnerd.stderr.log"));
await symlink(join(root, "secret"), join(directory, "runnerd.stderr.log"));
expect(execFileSync("sh", ["-c", REMOTE_RUNNER_EXIT_EVIDENCE_SCRIPT, "t", directory], { encoding: "utf8" })).not.toContain("private");
} finally { await rm(root, { recursive: true, force: true }); }
});
it("names the durable exit reason, redacted and bounded", () => {
const redact = (text: string) => text.replaceAll("sk-secret", "[redacted]");
expect(describeExitedRemoteRunnerReason({ lifecycle: "suspended", recoverableFailure: "transport_reconnect_grace_exceeded",
diagnostics: ["a", "b", "c", "d", "token sk-secret", 7] }, redact)).toEqual({
runnerRecoverableFailure: "transport_reconnect_grace_exceeded", runnerDiagnostics: ["b", "c", "d", "token [redacted]"] });
expect(describeExitedRemoteRunnerReason({ recoverableFailure: "free text with spaces" }, redact)).toEqual({});
expect(describeExitedRemoteRunnerReason(null, redact)).toEqual({});
expect(boundRemoteRunnerExitEvidence(" ", redact)).toBeNull();
expect(boundRemoteRunnerExitEvidence("y".repeat(9_000), redact)!.length).toBeLessThanOrEqual(REMOTE_RUNNER_EXIT_EVIDENCE_MAX_CHARS);
});
it("keeps the exit reason through the recorded event payload", () => {
const diagnostic = { stage: "process_verification", reason: "process_not_running", runnerLifecycle: "ready", runnerSettled: false,
runnerRecoverableFailure: "lease_expired_requires_bootstrap", runnerDiagnostics: ["connection lease expired"],
runnerExitEvidence: "== runnerd.stderr.log\npaperclip-runnerd: connection lease expired" };
expect(readRemoteRunnerRecoveryDiagnostic(JSON.parse(JSON.stringify(diagnostic)))).toEqual(diagnostic);
expect(readRemoteRunnerRecoveryDiagnostic({ ...diagnostic, runnerRecoverableFailure: "bad value", runnerDiagnostics: [1],
runnerExitEvidence: 4 })).toEqual({ stage: "process_verification", reason: "process_not_running", runnerLifecycle: "ready", runnerSettled: false });
});
});