mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
## Description
Closes #4248. The OpenCode 2.x plugin routes models through Headroom by
rewriting model `baseURL`s in `ctx.model.transform`, but it recognized
only the OpenAI wires (`openai-compatible`, `*/chat`, `*/responses`).
Copilot's Claude models use `@opencode/ai/providers/anthropic` (Messages
wire), so they were skipped and their requests never reached the proxy,
while Copilot GPT models kept working.
`modelWireSuffix` now also maps `anthropic`, `anthropic-compatible` and
model-level `*/messages` packages to `/messages`, reusing the existing
rewrite. The config-level `headroom wrap opencode` path cannot cover
Copilot because OpenCode creates that provider dynamically.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
<details>
<summary>changes details</summary>
- `plugins/opencode/src/plugin.ts`: rename `openAiWireSuffix` to
`modelWireSuffix`, map the Anthropic Messages wires to `/messages`.
- `plugins/opencode/README.md`: document that Anthropic Messages models
are rewritten too.
- `plugins/opencode/src/plugin.test.ts`: three new cases plus a narrowed
negative case.
- `headroom/providers/opencode/_dist/index.js`: rebuilt so the wheel
bundle stays byte-identical (CI `cmp` gate).
</details>
## Testing
<details>
<summary>testing details</summary>
- [x] Unit tests pass (`npm test -- --maxWorkers=2`: 66 passed at
current head)
- [x] Type checking passes (`npm run typecheck`)
- [x] Linting: repo lint is Python-only; plugin gates are typecheck,
build, test, bundle `cmp`, all run
- [x] New tests added
- [x] Manual testing performed — actual OpenCode CLI plus real proxy and
controlled upstream; live Copilot not tested
### Test Output
<!-- Paste relevant command output or artifact links here -->
<details><summary>commands and output</summary>
```text
npm test -- --maxWorkers=2 -> 4 files, 66 passed at 95ef917320 | npm run typecheck -> exit 0 | cmp dist-standalone/index.js headroom/providers/opencode/_dist/index.js -> BUNDLE_MATCH
```
The earlier red/green check recorded three new rewrite cases failing
before the fix (`3 failed | 61 passed`). Fresh current-head validation
is 66/66 plugin tests plus a successful no-emit typecheck;
full-repository Python pytest/Ruff were not run for this transport-only
verification.
</details>
</details>
## Real Behavior Proof
- Environment: Current head `95ef9173`, macOS arm64, official OpenCode
2.2.5, Python 3.12.8, Node 26.13.0; real plugin, Anthropic adapter and
Headroom proxy; local SSE upstream; Python-only transport mode.
- Exact command / steps: Build both plugin bundles, then run the runtime
harness with the official CLI in four scenarios. Exact commands, macOS
fixture adaptation, and the committed Linux reproduction are in the
details below.
- Observed result: All four actual CLI scenarios passed; auth retained,
internal headers stripped, streamed text and `step_finish` returned.
Current-head plugin tests 66/66, typecheck and byte-for-byte wheel
bundle comparison pass.
- Not tested: Live Copilot account/discovery or endpoint compatibility,
live z.ai, Rust/compression/cache, TLS, retries, latency, production
rollout, and other OpenCode versions. Full limits are below.
| Actual OpenCode scenario | Plugin | Proxy POSTs | Upstream POSTs |
Observed upstream path |
| --- | --- | --- | --- | --- |
| Baseline | disabled | 0 | 2 | `/messages` |
| Copilot-shaped root URL | enabled | 2 | 2 | `/v1/messages` |
| `/v1` gateway | enabled | 2 | 2 | `/v1/messages` |
| Prefixed gateway | enabled | 0 | 2 | `/api/coding/paas/v4/messages` |
<details>
<summary>Current-head transport evidence and platform-specific harness
setup</summary>
- Environment: Fresh run on 2026-11-04 at 22:10 UTC, PR head
`95ef9173200eb3b0f047e8fe661e1d9924d41c82`; macOS 27.3 arm64, Python
3.12.8, Node 26.13.0, npm 11.20.0, official installed **OpenCode 2.2.5**
CLI, source Headroom 0.42.1-dev, headroom-opencode 0.42.0. OpenCode
loaded the built standalone plugin itself and used its real
`@opencode/ai/providers/anthropic` adapter. Only the upstream API was a
strict-path local SSE fixture. The proxy explicitly ran in **Python-only
degraded mode**, with `HEADROOM_REQUIRE_RUST_CORE=false`, `--no-optimize
--no-cache`; Rust/compression is outside this proof.
- Exact command / steps: Install proxy dependencies with `uv sync
--frozen --extra proxy --no-default-groups --no-install-project --python
<CPython-3.12.8>`. In `plugins/opencode`, run `npm ci --ignore-scripts
--no-audit --no-fund --fetch-timeout=45000`, `npm run build`, and `npm
run build:standalone`. Run `.venv/bin/python
.cache/runtime-proof-macos.py --opencode
/Applications/OpenCode.app/Contents/Resources/opencode-cli --output
.cache/runtime-proof-macos`. This local copy of the checked-in [runtime
harness](https://github.com/headroomlabs-ai/headroom/blob/95ef9173200eb3b0f047e8fe661e1d9924d41c82/plugins/opencode/scripts/runtime-proof.py)
adapts its Linux loopback fixture to macOS as detailed below. For each
scenario the harness starts a real Headroom proxy and upstream, then
executes `opencode-cli run --standalone --print-logs --log-level debug
--format json 'Reply with HEADROOM_RUNTIME_OK.'` with isolated config,
synthetic auth, model-catalog fetching/updates disabled and a 60-second
timeout. It never invokes `headroomSetup` manually or replaces the model
request with `fetch`.
- Observed result: **All four actual CLI scenarios passed in 40.05
seconds.** Both title and user-turn requests traversed the expected
route, preserved the exact synthetic bearer/API-key values, and sent no
internal `x-headroom-*` headers upstream. Every user turn produced the
text event `HEADROOM_RUNTIME_OK` and a completed `step_finish` after
individual SSE events were flushed by the fixture. The rebuilt
standalone bundle matched the checked-in wheel bundle byte-for-byte,
SHA-256
`78fae6e51ef5d1c534a3299216ba3b467131c17594de34f0fe8bb7b5be4b7533`. No
tracked files or lockfiles changed during this run; all eight
proxy/upstream listening ports were closed afterward and no owned
CLI/proxy process remained.
- Not tested: Live GitHub Copilot discovery, subscription auth/token
refresh, Copilot-specific headers, actual model inference, or whether
the live Copilot origin accepts `/v1/messages`. This is a configured
Copilot-shaped provider, **not** native Copilot discovery. The prefix
control uses the real Anthropic adapter, not a live z.ai account or its
specific adapter. Rust core, optimization/compression/cache,
HTTPS/HTTP2, latency/backpressure, retry/rate-limit behavior, production
rollout, and OpenCode versions other than 2.2.5 were not exercised.
The proxied root-URL run recorded the actual OpenCode user agent and
original path at Headroom:
```text
proxy inbound: POST /v1/messages
user-agent: opencode/latest/2.2.5/cli
x-headroom-original-path: /messages
proxy outbound: POST http://[::ffff:7f00:1]:<fixture-port>/v1/messages
upstream: model=claude-sonnet-4-6, stream=true
upstream: authorization and x-api-key exactly matched synthetic fixtures
upstream: anthropic-version=2023-06-01; no x-headroom-* headers
client: text=HEADROOM_RUNTIME_OK; step_finish present
```
The root origin deliberately distinguishes direct `/messages` from
proxied `/v1/messages`. The fixture rejects an unexpected endpoint
rather than answering every path. These observations establish the
plugin/proxy transport boundary; they do **not** establish live Copilot
endpoint compatibility. The `/v1` origin remains `/v1/messages`, and the
prefixed origin remains `/api/coding/paas/v4/messages` with zero model
POSTs reaching Headroom.
The committed harness originally binds `127.0.0.2`, unavailable on this
macOS host (`Errno 49`). The ignored local copy uses an IPv4-mapped
loopback address with an IPv6 HTTP server and WHATWG-normalized fixture
URLs. It also fixes its checkout-relative path after copying, explicitly
allowlists only that fixture origin, sets supported offline/update
flags, creates its isolated proxy cache, accepts an existing isolated
home directory, and bounds each CLI run to 60 seconds. It does not
change production code or the expected endpoint/auth/SSE assertions. No
network interfaces or account settings were changed. All server
processes are scoped to the harness and terminated/waited in cleanup.
For the original independently recorded Linux run, reproducible
commands, and committed machine-readable results, see
[RUNTIME_PROOF.md](https://github.com/headroomlabs-ai/headroom/blob/95ef9173200eb3b0f047e8fe661e1d9924d41c82/plugins/opencode/RUNTIME_PROOF.md)
and
[runtime-proof-result.json](https://github.com/headroomlabs-ai/headroom/blob/95ef9173200eb3b0f047e8fe661e1d9924d41c82/plugins/opencode/scripts/runtime-proof-result.json).
Those artifacts explicitly identify their earlier source revision and
environment; the current-head macOS build hash above is from this fresh
run. Both proofs execute an actual OpenCode 2.x process. The earlier
PR-body experiment that manually supplied a minimal plugin context and
Node `fetch` has been superseded here.
</details>
## Runtime Rollout Safety
- Rollout-managed feature(s): none.
- Minimum rollout channel: n/a.
- Stable/default behavior changed: only previously skipped Anthropic
Messages models are rewritten; the negative test pins the rest.
- Kill switch / disable path: disable the plugin.
- Unsafe override required: no.
- Qualification impact: none expected.
- Rollback path: revert the commit; no state changes.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`
## Screenshots (if applicable)
N/A — no UI change.
## Additional Notes
The rebuilt `_dist` bundle is part of the diff because CI enforces byte
equality with `dist-standalone`. Actual OpenCode transport is now
verified with a controlled upstream. Live provider/account behavior and
the additional limits listed above remain unverified.
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4594"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
---------
Co-authored-by: JD Davis <mxjerrett@gmail.com>
121 lines
6.2 KiB
Markdown
121 lines
6.2 KiB
Markdown
# OpenCode 2.x Anthropic Messages transport proof
|
|
|
|
This records the actual OpenCode runtime boundary requested in
|
|
[PR #4594's review](https://github.com/headroomlabs-ai/headroom/pull/4594#pullrequestreview-5745390972).
|
|
The OpenCode process, its Anthropic adapter, built plugin, and Headroom proxy are real.
|
|
Only the upstream API is a controlled local fixture. No live Copilot credentials are used.
|
|
|
|
## Environment
|
|
|
|
Recorded on **2026-11-04 at 04:23 UTC**, against Headroom source
|
|
`8766328a6c20909520b0a7ac7b5d487900316084`:
|
|
|
|
- Linux 6.18.57, x86_64; Python 3.12.14; Node 24.22.1; npm 11.9.0.
|
|
- Official OpenCode **2.2.5**, from `@opencode/cli-linux-x64-baseline@2.2.5`.
|
|
- `headroom-opencode` 0.42.0; Headroom 0.42.1-dev from this checkout.
|
|
- Built `dist-standalone/index.js`, byte-identical to the checked-in wheel bundle.
|
|
SHA-256: `de88ab2120df79ad67fad3f7a06f8618ea093707cfcf1180921b1ae42e7b1cea`.
|
|
- Real provider adapter: `@opencode/ai/providers/anthropic`, model `claude-sonnet-4-6`.
|
|
This is a configured Copilot-shaped provider, not OpenCode's live Copilot discovery flow.
|
|
- **Python-only degraded proxy mode**, explicitly observed in startup logs because
|
|
this environment has no `headroom._core`. `HEADROOM_REQUIRE_RUST_CORE=false`
|
|
permits that mode; `--no-optimize --no-cache` limits the experiment to transport.
|
|
|
|
## Exact commands / steps
|
|
|
|
From the repository root, prepare the source proxy and build the plugin:
|
|
|
|
```sh
|
|
uv sync --frozen --extra proxy --no-install-project
|
|
npm --prefix plugins/opencode ci
|
|
npm --prefix plugins/opencode test
|
|
npm --prefix plugins/opencode run typecheck
|
|
npm --prefix plugins/opencode run build
|
|
npm --prefix plugins/opencode run build:standalone
|
|
cmp plugins/opencode/dist-standalone/index.js headroom/providers/opencode/_dist/index.js
|
|
```
|
|
|
|
Use an official OpenCode 2.x executable. For the Linux x86_64 run above, the binary
|
|
was installed without executing a package install script:
|
|
|
|
```sh
|
|
npm install --prefix /tmp/opencode-runtime --omit=optional --ignore-scripts \
|
|
--no-save --no-audit --no-fund @opencode/cli-linux-x64-baseline@2.2.5
|
|
OPENCODE_BIN=/tmp/opencode-runtime/node_modules/@opencode/cli-linux-x64-baseline/bin/opencode
|
|
.venv/bin/python plugins/opencode/scripts/runtime-proof.py \
|
|
--opencode "$OPENCODE_BIN" --output /tmp/headroom-opencode-proof
|
|
```
|
|
|
|
The installation/output directories are selectable; `--output` must be new.
|
|
The script requires POSIX process groups and the `127.0.0.2` loopback alias.
|
|
It starts a new real proxy and a strict-path SSE fixture for each scenario, then runs:
|
|
|
|
```sh
|
|
opencode run --standalone --print-logs --log-level debug --format json \
|
|
'Reply with HEADROOM_RUNTIME_OK.'
|
|
```
|
|
|
|
Each run uses isolated HOME/XDG directories, a minimal environment, disabled model
|
|
metadata fetching, and synthetic bearer/API-key values. This isolates default
|
|
credential discovery, not filesystem access. The upstream binds only to `127.0.0.2`,
|
|
which exercises model rewriting without the plugin's `localhost`/`127.0.0.1`
|
|
exclusion. That address is explicitly allowlisted in the test proxy. Ports are
|
|
allocated dynamically. OpenCode loads the built standalone **directory**, whose
|
|
`index.js` is its plugin entrypoint. The script does not call `headroomSetup` itself,
|
|
replace the OpenCode context, or send the model request with a manual `fetch`.
|
|
|
|
## Observed result
|
|
|
|
All four scenarios exited successfully:
|
|
|
|
| Scenario | Plugin | Proxy POSTs | Upstream POSTs | Upstream path |
|
|
| --- | --- | --- | --- | --- |
|
|
| Baseline | disabled | 0 | 2 | `/messages` |
|
|
| Copilot-shaped root URL | enabled | 2 | 2 | `/v1/messages` |
|
|
| `/v1` gateway | enabled | 2 | 2 | `/v1/messages` |
|
|
| Prefixed gateway | enabled | 0 | 2 | `/api/coding/paas/v4/messages` |
|
|
|
|
OpenCode made a title request and a user-turn request in each case. Every upstream
|
|
request retained the synthetic `Authorization` and `x-api-key`, the expected model,
|
|
`stream: true`, and `anthropic-version: 2023-06-01`. None contained `x-headroom-*`.
|
|
Each user turn received `HEADROOM_RUNTIME_OK` as an OpenCode text event, followed by
|
|
`step_finish`. The fixture flushes individual SSE events rather than returning a
|
|
JSON completion.
|
|
|
|
The proxied root-URL run logged these events (irrelevant fields omitted):
|
|
|
|
```text
|
|
04:23:10.316Z msg="loading plugin" entrypoint=file:///<checkout>/plugins/opencode/dist-standalone/index.js
|
|
04:23:10.975 event=proxy_inbound_request method=POST path=/v1/messages
|
|
user-agent=opencode/latest/2.2.5/cli
|
|
x-headroom-base-url=http://127.0.0.2:41341
|
|
x-headroom-original-path=/messages x-headroom-project=copilot-shaped
|
|
04:23:10.979 event=outbound_headers forwarder=anthropic_messages stripped_count=3
|
|
04:23:10.998 event=outbound_request forwarder=streaming method=POST
|
|
path=http://127.0.0.2:41341/v1/messages body_mutated=false source=passthrough
|
|
```
|
|
|
|
The script verifies proxy and upstream counts, exact upstream paths, preserved
|
|
synthetic auth, stripped internal headers, plugin loading, and a completed streamed
|
|
client reply. A fixture request to an unexpected endpoint receives 404. The root
|
|
URL case deliberately records the existing proxy contract of forwarding to
|
|
`/v1/messages`; it does not establish which endpoints a live Copilot account accepts.
|
|
The prefix exclusion is exercised by the real Anthropic adapter with the full
|
|
prefix intact and no model POST reaching Headroom.
|
|
|
|
Machine-readable results are in [runtime-proof-result.json](scripts/runtime-proof-result.json).
|
|
A rerun saves generated config, proxy/OpenCode logs, upstream observations and its
|
|
summary under the selected output directory. Plugin tests also passed **64/64**,
|
|
as did typecheck, both builds, bundle comparison, and Ruff checks for this script.
|
|
|
|
## Not tested
|
|
|
|
- Live `api.githubcopilot.com`, account discovery, subscription authentication,
|
|
token refresh, Copilot-specific headers, or actual model inference.
|
|
- A live z.ai gateway or its provider-specific adapter. The prefix control uses
|
|
the same real Anthropic adapter against a local path fixture.
|
|
- Rust-core behavior, compression/optimization, cache behavior, or production rollout.
|
|
- HTTPS/TLS, HTTP/2, rate limits, retries, or streaming latency/backpressure.
|
|
- OpenCode versions other than 2.2.5. The script rejects non-2.x versions and records
|
|
the actual Rust-core startup mode, which may differ on another machine.
|