Skip to content

test(opencode): cover the opencode2 host-env branch and stop inheriting ORCA_OPENCODE_AGENT - #22547

Merged
nwparker merged 2 commits into
mainfrom
nwparker/opencode2-wsl-overlay-coverage
Sep 24, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/opencode2-wsl-overlay-coverage

Conversation

@nwparker

@nwparker nwparker commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​88 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​88
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​17 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​17

ELI5

Two test-only fixes. One adds coverage for a path that no test could reach, so a bug there would have shipped silently. The other stops a test failing for everyone who runs it from inside an Orca OpenCode pane — which is how most of us develop this repo.

What Changed

Test-only. No production code.

1. The opencode2 branch of buildPtyHostEnv was unreachable by any test. The shared pty-ipc mock registry exported no openCode2HookService at all, so nothing could exercise the v2 side of the host-env assembly. A regression dropping the variant argument would have passed CI.

  • src/main/ipc/pty-ipc-mock-registry.ts, pty-ipc-suite-environment.ts — add the missing v2 service mock
  • src/main/ipc/pty-daemon-spawn-agent-home-env.test.ts — a WSL spawn must ask the guest for the matching major
  • src/main/agent-hooks/wsl-guest-plugin-install.test.ts — v1 and v2 guest overlay dirs stay independent
  • src/relay/plugin-overlay.test.ts — a remote config root holding the other major's stale Orca plugin must not have it mirrored in

Each was mutation-checked: reverting getOpenCodeOverlayDir(distro, openCodeAgent) → (distro) fails 2 tests; removing the cross-major skip in mirrorOpenCodeConfig fails 2 more.

2. hook-plugin-module-contract.test.ts inherited ORCA_OPENCODE_AGENT. The file saved and restored that variable but never set it. The generated plugin self-disables when it names a different major, so run from a shell that exports it, the plugin returned an empty hook set and the contract failed for the wrong reason — TypeError: hooks?.event is not a function. Fixed by deleting it in beforeEach, the way the sibling opencode2 suite already does.

Why

Both came out of investigating a Slack report that "Orca SSH WSL's opencode v2 orca-overlay is starting to report errors."

That report is #22506 seen from a host where it applies, not a WSL-specific defect — and the host matrix is the useful part:

Host Duplicate-id crash? Why
Local Yes hook-service.ts writes both majors into the canonical ~/.config/opencode/plugins/
SSH (no user OPENCODE_CONFIG_DIR) Yes relay does the same on the remote
SSH (user sets OPENCODE_CONFIG_DIR) No overlay branch; mirrorOpenCodeConfig skips both Orca filenames
WSL No per-pane guest overlays under per-major roots, each holding exactly one Orca plugin
SSH→Windows→WSL No Windows relay writes the Windows config; the guest reads a different Linux path

Verified on a real WSL2 Ubuntu-24.04 guest (via orca --environment awin), not just reasoned:

OVERLAY 4135c743…  plugins=1 files=orca-opencode-status.js
OVERLAY 63c62516…  plugins=1 files=orca-opencode-status.js
…8 overlays total, exactly one plugin each

~/.orca-relay/opencode2-overlays does not exist, and ~/.config/opencode/plugins does not exist in the guest — so the canonical dir is untouched and there is nowhere in WSL for the two files to meet. The guest's own OpenCode log shows it reading config from /home/<user>/.orca-relay/opencode-overlays/<hash>/opencode.json — direct evidence that OPENCODE_CONFIG_DIR stays guest-side rather than leaking a Windows userData path.

Two corrections to my original wording, from that verification:

  1. The isolation is per-pane (sha256 of the pane key) under per-major roots, plus mirrorOpenCodeConfig excluding both plugin filenames (plugin-overlay.ts:194-201). Saying "per-major" understated it.
  2. The overlay branch only runs when the user already has a source config dir (relay-agent-hook-runtime.ts:98-107). With no source dir the relay falls back to installOpenCodePlugin into the canonical dir — the same collision site as local. In the verified guest the user does have ~/.config/opencode, so the overlay branch is what actually ran; a guest without one would not be immune.

So WSL is strictly better isolated than local, and SSH is merely equal to it. There is no worse WSL variant and no wrong-variant delivery — ORCA_OPENCODE_AGENT reaches both hosts correctly, and 7b97551acfb already blocks a Windows overlay path from crossing into a guest. The fix for that reporter is #22544, not anything WSL-shaped.

Two things I settled from OpenCode source (v2.0.15) rather than by inference, because the obvious hypothesis was that a local userData path leaks into a guest and errors:

  • OPENCODE_CONFIG_DIR is still honoured by v2 (packages/cli/src/index.ts:117, server-process.ts:36 → Global.layerWith({config})), so the overlay mechanism is not obsolete.
  • A missing config dir is not an error — packages/util/src/global.ts:66 does mkdir(config, { recursive: true }), and plugin discovery swallows a missing plugins/ (Effect.orElseSucceed(() => [])). A meaningless overlay path would produce a silently wrong config root, not an error message. That ruled the hypothesis out, and is why this PR carries no production change.

The second fix is unrelated to WSL but was found the same way — it cost the investigation a wasted run, and it will cost every other contributor one.

Linked Issue

Refs #22506

Visual Proof

N/A — test-only; no user-visible surface is touched.

Testing

Platforms: macOS for the unit tests. The WSL rows of the matrix were since verified on a real WSL2 Ubuntu-24.04 guest (see Why). SSH-to-Windows remains derived rather than measured.

  • pnpm test src/main/ipc src/relay src/main/agent-hooks src/main/opencode → 720 files, 7096 passed, 24 skipped

  • pnpm tc clean; pnpm run check:code-quality:changed passed

  • Contract-test fix verified against all three states: ORCA_OPENCODE_AGENT=opencode, =opencode2, and unset — 9/9 in each. Before it, two of the three failed.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • No production code changes, so no runtime, security, performance, or wire impact.
  • One gap left open deliberately: the SSH relay's variant expression (relay-agent-hook-runtime.ts:88-90) still has no test. It duplicates the WSL one pinned here, but testing it needs RelayAgentHookRuntime with a real hook server and a homedir() override, and materializeOmpFreshConfig() writes to the real ~/.orca-relay. A HOME-mutating test looked more fragile than the coverage was worth. Flagging rather than forcing it.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test pass locally

@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: fac09530-ab03-470a-8d77-b9869f45c844

📥 Commits

Reviewing files that changed from the base of the PR and between 519bde8 and 2e135d8.

📒 Files selected for processing (6)
  • src/main/agent-hooks/wsl-guest-plugin-install.test.ts
  • src/main/ipc/pty-daemon-spawn-agent-home-env.test.ts
  • src/main/ipc/pty-ipc-mock-registry.ts
  • src/main/ipc/pty-ipc-suite-environment.ts
  • src/main/opencode/hook-plugin-module-contract.test.ts
  • src/relay/plugin-overlay.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

The changes add tests for variant-specific plugin overlays and WSL guest overlay directories. A parameterized daemon test checks that the selected agent determines the overlay and environment values. IPC test mocks now provide a separate OpenCode 2 environment mock, and contract tests clear the agent environment variable before each test.

Merge Risk: ⚪ Minimal · up to 2e135

The added tests and environment cleanup are ready to merge after normal checks; no actionable issue remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 6 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the two main changes: coverage for the opencode2 host-environment branch and prevention of ORCA_OPENCODE_AGENT inheritance.
Description check ✅ Passed The description is detailed and aligned with the template. It explains the changes, motivation, linked issue, testing, platform coverage, and checklist status. The Review section is empty, and build v…
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Two minor suggestions inline — no correctness issues, the added tests are sound.

Reviewed changes

  • v2 host-env mock — pty-ipc-mock-registry.ts / pty-ipc-suite-environment.ts add a distinct openCode2HookService mock, so an opencode2 launch no longer dereferences undefined in assembly.ts and the v2 branch is reachable at all.
  • WSL overlay selection — pty-daemon-spawn-agent-home-env.test.ts pins getOpenCodeOverlayDir(distro, openCodeAgent) per major, asserting the recorded variant argument and the resulting ORCA_OPENCODE_AGENT / OPENCODE_CONFIG_DIR / ORCA_OPENCODE_CONFIG_DIR.
  • Guest overlay independence — wsl-guest-plugin-install.test.ts pins independent dir / dir2 propagation, including the v2-only case.
  • Cross-major mirror skip — plugin-overlay.test.ts asserts the other major's stale Orca plugin is not mirrored into the overlay.
  • Test-env isolation — hook-plugin-module-contract.test.ts clears ORCA_OPENCODE_AGENT in beforeEach so an inherited value can't self-disable the plugin.

I read the full diff and ran the four changed files plus the pty-daemon suite (66 tests pass). The assertions are exact rather than loose — the WSL test compares spy.mock.calls.map(([, agent]) => agent) with toEqual([expectedAgent]), so dropping the variant argument produces [undefined] and fails, matching the mutation check described in the PR body. The mirror test binds both OPENCODE_PLUGIN_FILE/OPENCODE2_PLUGIN_FILE in the skip list, so removing either constant fails the relevant case.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

// Why: the generated plugin self-disables when this names a different major,
// so an inherited value from the developer's own Orca pane would leave
// `hooks.event` undefined and fail the contract for the wrong reason.
delete process.env.ORCA_OPENCODE_AGENT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: ORCA_OPENCODE_AGENT is deleted here but is not in ENV_KEYS, so afterEach never restores the value it captured. The sibling hook-plugin-opencode2-setup.test.ts includes it in ENV_KEYS and restores it; adding it here keeps the save/restore symmetric and avoids leaking the deletion to any test that runs later in the same worker. Not blocking — the deletes re-run per test within this file.

...(launchAgent ? { launchAgent } : {})
})
expect(spy.mock.calls.map(([, agent]) => agent)).toEqual([expectedAgent])
expect(env.ORCA_OPENCODE_AGENT).toBe(expectedAgent)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this test pins the guest-overlay variant argument, but the other v2 variant selection in assembly.ts:93 (openCodeAgent === 'opencode2' ? openCode2HookService : openCodeHookService) is still unasserted — a regression that always picked the v1 service would pass, because the WSL branch overwrites OPENCODE_CONFIG_DIR from the guest dir. Since openCode2BuildPtyEnvMock exists specifically to distinguish this, a single assertion that the matching service mock was called would realize the registry comment's stated intent.

…he relay

OpenCode 2 dies with "Duplicate plugin ID" when two plugin files share an id,
so which overlay a guest or remote pane is pointed at decides whether the agent
starts. Nothing failed if that selection regressed:

- the WSL spawn path never asserted which major it asks the guest relay for,
  and the shared pty-ipc mock had no openCode2HookService at all, so no test
  could reach the opencode2 branch of buildPtyHostEnv;
- requestGuestOpenCodeOverlayDir had no coverage for the v2 guest dir;
- PluginOverlayManager had no case for a remote config root that still holds
  the other major's stale Orca plugin.

Tests only; no behavior change. Each new case was mutation-checked against the
production line it guards.
…E_AGENT

The generated plugin self-disables when ORCA_OPENCODE_AGENT names a different
major, and the contract test saved and restored that variable without ever
setting it. Run from a shell that has it — which is any shell inside an Orca
OpenCode pane, i.e. how this repo is usually developed — the plugin returned an
empty hook set and the contract failed for the wrong reason.

Delete it in beforeEach, the way the opencode2 setup test already pins it.
Verified the file passes with the variable set to either major and unset;
before this it failed for two of the three.
@nwparker
nwparker force-pushed the nwparker/opencode2-wsl-overlay-coverage branch from 2e135d8 to 79eff5b Compare September 23, 2026 23:08
@nwparker
nwparker merged commit 57bf732 into main Sep 24, 2026
100 of 104 checks passed
@nwparker
nwparker deleted the nwparker/opencode2-wsl-overlay-coverage branch September 24, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant