Files
PaperClipAI/server/src/services/native-runtime
paperclipbugbotandClaude Opus 5.5 43fedd51c5 fix(opencode): record kernel OOM evidence when opencode serve is SIGKILLed (#23372)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The native Runner starts `opencode serve` for each OpenCode agent
turn and reads its event stream.
> - Sometimes `opencode serve` dies by SIGKILL in the middle of a turn.
The run then fails with `opencode_server_exited`.
> - The failure record shows `signal: SIGKILL` and `ECONNREFUSED`, but
it does not show who sent the signal.
> - A SIGKILL alone does not prove a kernel OOM kill. Recent failures
were short turns with few or no tool calls, so the cause is not clear.
> - This pull request records the kernel cgroup v2 OOM evidence when
`opencode serve` dies by a signal.
> - The benefit is that the next failure tells us if the kernel memory
limit killed OpenCode or if something else did.

## Linked Issues or Issue Description

Refs #17398 (the Legacy OpenCode adapter makes sandbox tool commands the
first OOM victims). Refs #16844 (the same protection for Claude).

**What happened?**
On a cloud sandbox, a native OpenCode turn failed with
`opencode_server_exited`. The run failure record had `streamFailure:
server_exited`, `signal: SIGKILL` and `connectionError: ECONNREFUSED`,
and `timeoutFired` was false. The Runner process stayed alive. The
Runner sends SIGKILL to OpenCode only at shutdown or at a failed start,
so the signal came from outside the Runner. The record has no kernel
memory evidence, so we cannot tell if the kernel OOM killer sent it.

**Expected behavior**
When `opencode serve` dies by a signal, the failure record says if the
kernel recorded an OOM kill in its cgroup after launch. It also gives
the memory limit and the peak use.

**Steps to reproduce**
1. Start a native OpenCode turn in a Linux sandbox that uses cgroup v2.
2. Send SIGKILL to the `opencode serve` process while the turn runs.
3. Read `resultJson.nativeTurnFailure` for the failed run. Before this
change, it has no memory evidence.

**Paperclip version or commit**
`master` at 6678554a5a.

## What Changed

- Add `memory-kill-evidence.ts` to the OpenCode driver. It reads the
process cgroup from `/proc/<pid>/cgroup` (unified v2 entry only). It
records the `oom_kill` count from `memory.events` right after spawn.
Later it compares the count and finds the nearest numeric `memory.max`
and that cgroup `memory.peak`. It never reads outside the cgroup root.
It returns nothing when cgroup v2 evidence is not readable.
- Start the probe after `opencode serve` spawns, on Linux only. A new
`memoryKillProbe` driver option sets other paths for tests, or `false`
turns the probe off.
- When the event stream fails and the process died by a signal, add
`memoryKill` (`observed` or `not_observed`), `memoryLimitBytes` and
`memoryPeakBytes` to the `opencodeFailure` record.
- Keep the three fields in the server closed-vocabulary projection
(`native-provider-failure.ts`). Only the two known `memoryKill` values
and positive safe integers pass. They reach
`resultJson.nativeTurnFailure` and the run failure context.
- This is a diagnostic. It does not change how a turn fails or recovers.

## Verification

- `cd packages/paperclip-runner && npx vitest run
src/drivers/opencode/memory-kill-evidence.test.ts`: 4/4 pass. The tests
cover an observed kill, no kill, no limit, no readable cgroup v2 data,
cgroup v1, and a path that tries to leave the cgroup root.
- `cd packages/paperclip-runner && npx vitest run
src/drivers/opencode/opencode-server-driver.test.ts -t "OpenCode stops
mid-turn|classifies a lost event stream"`: 2/2 pass. The SIGKILL
integration test uses a fake cgroup tree. It kills OpenCode right after
the turn opens, with no tool calls. It now expects `memoryKill:
"observed"` and the limit. It fails when the probe is off.
- `cd server && npx vitest run
src/services/native-runtime/native-provider-failure.test.ts`: 63/63
pass.
- These local runs used the parent commit 201abacd. The patch applies to
current `master` with only line offsets. CI runs the full suites on this
branch.

## Risks

- Low risk. The probe reads a few small files: once at spawn and once
after a signal death. All read errors give no evidence. They do not
throw.
- On hosts without cgroup v2 (macOS, Windows, cgroup v1), the probe
reports nothing.
- `memoryKill: "not_observed"` means that the kernel did not count an
OOM kill in the cgroup of OpenCode or its child cgroups. An OOM kill in
a different cgroup (for example a parent limit with another member) can
still kill OpenCode. Read the field with `memoryLimitBytes` and
`memoryPeakBytes`.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in an agent sandbox.

## 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
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] 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-12-01 13:53:03 +00:00
..