mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 11:47:42 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work.
> - The native Runner starts `opencode serve` (pinned to 1.18.34) for
each OpenCode turn, with its own fresh config directory and a fresh
isolated `HOME`.
> - #23372, #23391 and #23428 showed that the kernel OOM killer stops
`opencode serve` at the 8 GiB sandbox limit about 50 seconds into light
turns. The memory is OpenCode's own resident set. The model traffic is
small and complete when OpenCode dies.
> - A local reproduction finds the cause. On every project bootstrap,
OpenCode installs `@opencode-ai/plugin` into its global config directory
with npm's Arborist. The install runs inside the `opencode serve`
process. It downloads about 114 MB into a fresh npm cache and keeps the
JavaScript event loop busy while it allocates. OpenCode stops answering
HTTP, emits no events, and does not send the next model request.
> - OpenCode skips this install when the directory has `node_modules`
and a `package-lock.json` that lists the dependency. Today only launches
with configured secrets write that marker, as part of output custody.
> - This pull request writes the marker for every launch.
> - The benefit is that OpenCode no longer runs npm at startup, so it
stays responsive and its memory stays flat.
## Linked Issues or Issue Description
Refs #23372 (kernel OOM evidence). Refs #23391 (memory attribution).
Refs #23428 (model traffic counts).
**What happened?**
Native OpenCode turns on cloud sandboxes fail with
`opencode_server_exited` and SIGKILL. The failure record shows
`memoryKill: observed`, an OpenCode resident set of about 8 GB at the 8
GiB limit, and one or two small, completed model responses. The run log
stops for about 42 to 48 seconds before the kill. The failures happen
with and without tool calls. Agents with configured secrets are not
affected, because output custody already writes the install marker.
**Expected behavior**
`opencode serve` stays responsive after startup. It does not download or
install packages, and its memory does not grow while it waits for a
model or a tool.
**Steps to reproduce**
1. Start `opencode serve` 1.18.34 with an empty
`$XDG_CONFIG_HOME/opencode` that has only `opencode.json`, and an empty
`HOME`, as the Runner does for an agent without configured secrets.
2. Send one request, for example `GET /config`, so that OpenCode
bootstraps the project.
3. Within about 8 seconds, OpenCode uses 100 to 150% CPU on its
JavaScript main thread. It stops answering HTTP (`GET /config` times
out), and its memory grows. `$XDG_CONFIG_HOME/opencode/node_modules` and
`$HOME/.npm/_cacache` fill with `effect`, `zod`, `yaml`, `@ai-sdk/*` and
other packages.
**Paperclip version or commit**
`master` at debfa75a7a.
## What Changed
- New `config-dependencies.ts`:
`markOpenCodeConfigDependenciesInstalled(configDirectory)` creates
`node_modules` and writes `package-lock.json` with `@opencode-ai/plugin`
in its root dependencies. It keeps an existing lock that already lists
the package, for example one that a completed install wrote. It replaces
a lock that does not list it, with a temporary file and a rename.
- `startRuntime` calls it for OpenCode's global config directory on
every launch, before OpenCode starts. This applies with or without
configured secrets.
- Output custody no longer writes the marker, and
`prepareOpenCodeOutputCustody` no longer takes `configDirectory`. The
launch now does this once for every agent.
- `scripts/qualify-opencode-runtime.test.mjs` starts OpenCode directly.
It now records the dependency as installed for both secret cases, as the
Runner launch does.
- The Runner loads only local plugin files (`file://…`), which import
nothing from `@opencode-ai/plugin`. So nothing needs the skipped
install.
- With `OPENCODE_DISABLE_PROJECT_CONFIG=true`, which the Runner already
sets, OpenCode does not install into a workspace `.opencode` directory.
I checked this with a workspace that has a `.opencode` directory.
## Verification
- `npx vitest run src/drivers/opencode/config-dependencies.test.ts`: 3/3
pass. The tests copy OpenCode 1.18.34's install decision (`Npm.install`:
`node_modules` must exist, and the lock root must list every dependency
that `package.json` and the install would add). They check a fresh
directory, that an existing lock stays unchanged, and that a lock which
does not list the package is replaced. With the helper turned into a
no-op, 2 of 3 fail.
- New driver test: a launch with no configured secrets leaves
`node_modules` and a lock that lists `@opencode-ai/plugin` in the config
directory, and no plugin in `opencode.json`. CI runs it with the full
driver suite.
- Real OpenCode 1.18.34, idle, with the config directory prepared by
this helper:
| Config directory | OpenCode CPU | OpenCode RSS | `GET /config` | npm
cache |
| --- | ---: | ---: | ---: | ---: |
| As on `master` without secrets | 127–148% | 0.6 GB and climbing |
times out after about 8 s | about 114 MB |
| With this change | 1–2% | 311 MB, 485 MB after a prompt | 2–3 ms |
none |
- Real OpenCode 1.18.34 with a mock provider and one `bash` call that
runs `sleep 30`: before the change, OpenCode never sent the reply, and
its memory churned between 1.1 and 1.9 GB (a peak of 3.1 GB in one run).
With the change, the tool result went back to the model, and the turn
completed after 37 s. Memory stayed at about 510 MB and CPU stayed near
0%.
- `PAPERCLIP_OPENCODE_QUALIFY=1 node --test
scripts/qualify-opencode-runtime.test.mjs` with the real OpenCode
1.18.34, using type-stripped builds of the two changed modules, three
runs each. Before the qualification change, the protected case failed
every time after about 16 s, the same as the first CI run (16.9 s).
After it, all three tests passed every time, and both secret cases took
about 2.3 s. The qualification now records the dependency as installed
for both cases, as every Runner launch does.
- I could not run the full runner suite and the typecheck in my sandbox.
Its dependencies are not installed there. CI runs them on this branch.
## Risks
- Low risk. The change writes two small files in a directory that the
Runner owns, before OpenCode starts.
- If a future OpenCode version needs `@opencode-ai/plugin` in the config
directory, its plugins could fail to load. Today the Runner loads only
its own local plugin file, which has no imports from that package. A new
OpenCode version must pass qualification before the Runner uses it.
- OpenCode's other startup installs, for example of npm plugins named in
config, are not affected. The Runner configures none.
## 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>