Files
PaperClipAI/packages/adapters/opencode-local
797680a5ff fix(opencode-local): keep global instructions and skill paths stable across runs (#17209)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The `opencode_local` adapter runs each heartbeat with a runtime
OpenCode config. It copies the user OpenCode config directory into a new
`mkdtemp` directory for every run.
> - OpenCode writes the path of the global `AGENTS.md` ("Instructions
from: <path>") and the location of each config skill into the system
prompt.
> - Because the directory name is random, the system prompt is different
in every run, at about token 2,000.
> - Provider prompt caching matches a prompt prefix only. Each run
therefore pays for the full prompt again, and the model waits longer
before it answers.
> - This pull request loads the global `AGENTS.md` and the config skill
folders from the source config directory instead of copying them into
the per-run directory.
> - The benefit is a stable system prompt across runs, so providers can
reuse cached prompt tokens.

## Linked Issues or Issue Description

Refs #16678
Refs #15440

I found no issue or pull request for this problem. #16678 and #15440
also change how this adapter builds the runtime OpenCode config
(run-scoped MCP servers). They do not change the copy of `AGENTS.md` or
skills.

**What happened?**

Every `opencode_local` run with a global `~/.config/opencode/AGENTS.md`
sent a different system prompt. With OpenRouter and
`deepseek/deepseek-v4.1-flash-1017`, each run read only 1,792–2,048
cached tokens of about 48,000 prompt tokens. A short message ("halo")
took 9–20 seconds, depending on the model, and the full prompt was
billed every time.

**Expected behavior**

Two runs of the same agent with the same OpenCode config send the same
system prompt. Prompt caching then covers the system prompt.

**Steps to reproduce**

1. Put an `AGENTS.md` (and optionally a `skills/<name>/SKILL.md`) in
`~/.config/opencode`.
2. Create an agent with the `opencode_local` adapter and an OpenRouter
model that supports implicit prompt caching.
3. Run the agent twice with a short prompt.
4. Compare `cachedInputTokens` in the usage of the two runs. It stays at
about 2,000 tokens.
5. Run `XDG_CONFIG_HOME=<per-run dir> opencode debug skill` for two
per-run directories. The skill `location` contains the random
`paperclip-opencode-config-*` directory.

**Agent adapter(s) involved**

`opencode_local` (OpenCode 1.18.34; the same config keys exist in
1.19.3).

**Relevant logs or output**

System prompt layout that OpenCode sends, captured with an OpenCode
plugin that records `experimental.chat.system.transform`:

| Characters | Content | Same in each run? |
|---|---|---|
| 0–8,740 | OpenCode base prompt | Yes |
| about 8,970 | `Instructions from:
/tmp/paperclip-opencode-config-<random>/opencode/AGENTS.md` | No |
| about 13,000 to end | `<available_skills>`, with the `location` of
config skills under the same random directory | No |

## What Changed

- `packages/adapters/opencode-local/src/server/runtime-config.ts`: after
the adapter copies the source config directory, it removes the copied
`AGENTS.md` and the copied `skill`/`skills` folders from the per-run
directory. It adds the source `AGENTS.md` path to `instructions` and the
source skill folders to `skills.paths` in the runtime `opencode.json`.
Existing `instructions`, `skills.paths` and `skills.urls` values stay in
the config. The adapter adds a note to the run when it does this.
- `packages/adapters/opencode-local/src/server/runtime-config.test.ts`:
a test for two runs from the same source config: different per-run
directories, no copied `AGENTS.md` or `skills`, the same `instructions`
and `skills` values, and existing user entries kept. A test for a source
config without `AGENTS.md` or skills: the adapter does not add
`instructions` or `skills`.

## Verification

- `pnpm --filter @paperclipai/adapter-opencode-local exec vitest run`
passes (84 tests).
- `pnpm --filter @paperclipai/adapter-opencode-local exec tsc --noEmit`
passes.
- `opencode debug skill` and `opencode debug config` with a per-run
directory from this change: all 26 skills load, the config skill
`location` is the source path, and no `paperclip-opencode-config-*` path
is in the output.
- Direct `opencode run` test (DeepSeek through OpenRouter, "halo"): with
a new random config directory for each run, 0 of 6 runs read cached
tokens past 2,000. With a fixed config directory, the system prompt was
the same byte for byte in three runs.
- Self-hosted instance (Linux, systemd user unit, embedded Postgres),
one `opencode_local` agent with `deepseek/deepseek-v4.1-flash-1017`,
before and after this change:

| Run | Cached input tokens before | Cached input tokens after |
|---|---|---|
| First message of a new session | 1,792 | 1,536–1,792 |
| Second message in the same session | 1,792 | 7,680–29,978 |

The first message of a new session still reads about 1,800 cached
tokens. OpenRouter sends requests for this model to many providers, and
each provider keeps its own cache.

## Risks

- Low. Remote execution targets do not use this code path. Configs
without a global `AGENTS.md` or config skills do not change.
- Behavior shift: OpenCode now loads the global `AGENTS.md` through
`instructions`. It is the first entry, before other configured
instructions. The text in the prompt is the same. Only the path in
"Instructions from" changes to the source path.
- The run reads `AGENTS.md` and the config skills from the source
directory, not from a copy. The adapter does not change these files.
- `skills.paths` must be a supported OpenCode config key. OpenCode
1.18.34 and 1.19.3 support it.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), via Claude Code CLI, with tool
use (file edits, shell, test runs) and extended reasoning. A human
reviewed the diff.

## 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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
2026-12-05 00:08:54 -08:00
..
2026-03-12 13:09:22 -05:00

OpenCode local adapter

Runtime configuration

prepareOpenCodeRuntimeConfig stages an isolated OpenCode configuration directory on the host for both local and remote execution targets. It copies the operator configuration and applies the runtime provider and permission settings. Its returned XDG_CONFIG_HOME points to that host directory.

For remote execution, execute and testEnvironment pass the staged directory to prepareAdapterExecutionTargetRuntime as the xdgConfig asset. They replace XDG_CONFIG_HOME with the asset's remote destination before running OpenCode. The helper's cleanup function removes the host staging directory.