Skip to content

test(seam): forbid host SDK imports outside the adapter layer - #643

Open
abogartz wants to merge 3 commits into
mainfrom
test/580-seam-host-sdk
Open

abogartz wants to merge 3 commits into
mainfrom
test/580-seam-host-sdk

Conversation

@abogartz

@abogartz abogartz commented Dec 2, 2026

Copy link
Copy Markdown
Owner

What the rule now forbids

The seam walker in test/host/import-boundary.test.ts already policed internal layering, but an external package specifier passed through it unchecked, so shared source could import a host SDK directly without the suite noticing. The same check() now classifies host SDK specifiers and rejects them from any source outside src/adapters/ (allowed inside), across every import form the walker sees: static import, import type, bare side-effect, export *, named re-export, import(), require, import = require(), typeof import(), and template literals with no expressions.

Membership is derived, not guessed: every SDK package the shipped adapters import at main (@opencode/plugin, @earendil-works/pi-coding-agent, @oh-my-pi/pi-ai, @oh-my-pi/pi-coding-agent, solid-js), plus the host-SDK inventory already maintained in scripts/test-adapter-package.ts (@earendil-works/pi-ai, @oh-my-pi/pi-catalog, @opentui/core, @opentui/solid) and the retired Pi package named in README.pi.md (@mariozechner/pi-coding-agent). Matching is by package root: a member or any subpath of it is the SDK; a package that merely contains a host name is not.

Measured baseline (re-measured at origin/main, not copied from the ticket)

  • Before / after, shared source outside src/adapters/: 0 files import a host SDK — the ticket's claim, confirmed.
  • Adapter layer: 8 files import one (the ticket's "3" matches the count of SDK families per adapter, not files): opencode/plugin.ts, opencode/tui/panel.tsx, oh-my-pi/completion.ts, oh-my-pi/context.ts, oh-my-pi/lifetime.ts, pi/compaction.ts, pi/completion.ts, pi/runtime.ts.

The zero is asserted as a live scan (shared source contains no host SDK import today), so the day shared source reaches for a harness package the suite fails naming file, line and specifier.

Negative control (the rule bites)

Planting a shared file with import { Plugin } from "@opencode/plugin/promise/plugin"; failed two tests with:

src/core/host-sdk-probe.ts:1: host SDK import outside src/adapters @opencode/plugin/promise/plugin

The same import inside an adapter file passes. Pin fixtures cover all ten import forms shared-fail / adapter-pass, plus a boundary case bank: forks and renamed packages (solid-js-router, @opencode/plugin-community, opentui-themes) pass; real-SDK subpaths (@opencode/plugin/tui/context, solid-js/store, …) fail; scoped and bare roots both fail; opencode-self-improvement/sdk stays the own-package guard's verdict (unsupported dependency, asserted verbatim so the two guards never double-report); computed and empty specifiers keep their existing silent pass for shared source.

Deliberately not caught

  • Computed specifiers (import(variable), require(prefix + "solid-js")) from shared source — judging them would break legitimate lazy loading; unchanged, with the reason pinned in a fixture.
  • Alias/absolute escapes from shared source (#alias, file:) — the walker resolves nothing, so an alias aimed at an SDK path is invisible; same pre-existing limitation as the other rules.
  • Strings that are not imports (comments, the @jsxImportSource @opentui/solid pragma, package.json manifests).
  • Files outside the scanned src/ tree (root forwarders, tests, scripts).

Refs #580

Closes #580

The seam walker policed internal layering but let shared source import a
host SDK package directly. The same check() now rejects a host SDK
specifier (package root or subpath) from any source outside
src/adapters/ and allows it inside, across every import form: static,
import type, export *, named re-export, import(), require,
import-equals-require, typeof import() and expressionless template
literals. Host SDK membership comes from the adapter layer's own imports
plus the inventory in scripts/test-adapter-package.ts and README.pi.md;
matching is by package root, so packages that merely contain a host name
are untouched.

Measured baseline (re-measured at origin/main): 0 shared files import a
host SDK; 8 adapter files do (opencode/plugin.ts, opencode/tui/panel.tsx,
oh-my-pi/completion.ts, oh-my-pi/context.ts, oh-my-pi/lifetime.ts,
pi/compaction.ts, pi/completion.ts, pi/runtime.ts). After the change the
same zero is asserted as a live scan, so the day shared source reaches
for a harness package the suite goes red naming file, line and
specifier.

Negative control: planting a shared file with
import { Plugin } from "@opencode/plugin/promise/plugin";
fails two tests with
src/core/host-sdk-probe.ts:1: host SDK import outside src/adapters
@opencode/plugin/promise/plugin
(the probe was removed before commit).

@ocsi-reviewer ocsi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review: REQUEST_CHANGES — head 2bc96e782f812f22effda35b665e0108d2708c2c

One file (test/host/import-boundary.test.ts, +144/−3); nothing under src/ moved (git diff --stat origin/main..HEAD confirms). The rule itself is sound and bites; the request is for one deterministic cross-platform defect in the new assertions, red in CI at this head.

Blocking: test-windows fails deterministically — the violation message is built from an un-normalized path

CI run 46795704539 at this head: 9 jobs green, test-windows red. The full run completed — 2231 pass / 36 skip / 1 fail; bun did not stop early. The verbatim diff:

-   "src/runtime/setup.ts:1: unsupported dependency opencode-self-improvement/sdk",
+   "src\runtime\setup.ts:1: unsupported dependency opencode-self-improvement/sdk",
(fail) the own-package guard owns opencode-self-improvement specifiers

Root cause, in the code as written: violations() normalizes the relative path once (const rel = relative(ROOT, file).replaceAll("\\", "/")), and the host-SDK push correctly uses rel — which is why the other new verbatim pin (src/runtime/setup.ts:2: host SDK import outside src/adapters …) passes on Windows. But reject() and the "core imports" push recompute relative(ROOT, file) raw, so on Windows they emit src\runtime\setup.ts. The scanned files lists are separately normalized after scanSync, so the existing toContain("opencode/plugin.ts")-style assertions pass — the asymmetry is only inside the message producers. Exactly one test pins a message produced by the raw sites, which is exactly the one test that is red; this is not a flake and the count is not suspicious.

Fix at the producer, not the assertion: use the already-computed rel in reject() and the "core imports" push (two sites; rel is in scope). Rationale: the function already normalizes once and already uses it in one push, so the raw sites are an oversight, not a decision; per-assertion normalization would leave the rule emitting platform-dependent text, and this PR's own style — pinning messages verbatim — guarantees the next pin trips the same wire. It also makes the message a Windows developer can act on. The rule's logic is not in question; only its message plumbing.

The rule bites: negative control and mutation matrix reproduced

  • Negative control reproduced at this head: planting src/core/host-sdk-probe.ts with import { Plugin } from "@opencode/plugin/promise/plugin"; fails exactly shared source cannot depend on adapters or their entry forwarders and shared source contains no host SDK import today, naming src/core/host-sdk-probe.ts:1: host SDK import outside src/adapters @opencode/plugin/promise/plugin; removing it restores 11 pass / 0 fail.
  • All seven new tests are killed by targeted mutations (baseline suite 11 tests: 4 pre-existing + 7 new):
    • allow-side widened to all of src/ → cannot reach a host SDK in any import form and subpath is the same host SDK fail;
    • walker drops ExportAllDeclaration → the any-form test fails (plus two pre-existing);
    • host SDK forbidden everywhere (guard dropped) → the adapter layer owns every host SDK import fails;
    • membership matched by substring → a package that merely contains a host name fails;
    • own-package guard removed → the own-package guard test fails;
    • package-root extraction broken (scope no longer sliced) → any-form + subpath tests fail;
    • computed specifiers rejected → computed or empty specifier test fails.
  • One honest limitation, pinned rather than hidden: deleting a form from hostSdkImportForms kills nothing — the bank is the coverage declaration. Cross-checked by hand: the bank's 10 forms map onto every node kind the walker inspects (ImportDeclaration value/type/bare, ExportAllDeclaration, ExportNamedDeclaration source, ImportExpression incl. expressionless template literal, require() call, TSExternalModuleReference, TSImportType). No tautologies.

Membership verdicts (measured at this head)

Package Verdict
@opencode/plugin host SDK — the Opencode plugin interface; live adapter imports (type + /tui subpaths)
@earendil-works/pi-coding-agent host SDK — Pi extension interface; 3 live adapter type-imports
@oh-my-pi/pi-coding-agent host SDK — live dynamic import() in oh-my-pi/lifetime.ts (runner subpath); version sourced from integration/oh-my-pi/package.json
@oh-my-pi/pi-ai host SDK — live adapter imports (type in context.ts, dynamic in completion.ts)
solid-js host SDK as defined — belongs; see below
@opentui/solid prophylactic as an import — declared peer+dev dependency; appears in adapter source only as the @jsxImportSource pragma, which the walker cannot see and the PR lists under "deliberately not caught"
@earendil-works/pi-ai prophylactic — declared dependency (dev 1.7.0), host-SDK inventory in scripts/test-adapter-package.ts; no import in src/
@oh-my-pi/pi-catalog prophylactic — declared dependency (dev 19.2.1), in the same inventory; no import in src/
@opentui/core prophylactic — declared peer+dev dependency, in the same inventory; no import in src/
@mariozechner/pi-coding-agent prophylactic/historical — not in package.json at all; guarded against reintroduction of the retired Pi package named in README.pi.md

Every member traces to one of the four sources the code comment declares (adapter imports, scripts/test-adapter-package.ts inventory, integration/ manifests, README.pi.md); no member is a generic library.

solid-js: it belongs. It is not merely "a popular UI library" for this package — it is declared under peerDependencies (>=1.9.0), i.e. the engine ships assuming the harness provides it, and scripts/test-adapter-package.ts installs the engine for a plain Node consumer without it and typechecks that, later adding host SDK packages only to typecheck adapters. Shared code importing solid-js would therefore break the Node-only consumer and hard-wire the opencode TUI — exactly the failure class #580 was filed for. The cost of a wrong member is real but bounded: shared code would be forbidden a library it legitimately needs, and the rule's package-root-only matching (pinned by the solid-js-router / @opencode/plugin-community fixtures) keeps the blast radius from name-lookalikes.

The true file count, and the correction to #580's "3"

Counted at this head with the walker's own import forms (static, import type, bare side-effect, export *, named re-export, import(), expressionless template literal, require, import = require, typeof import()), a file counting once even with multiple imports:

Adapter layer: 8 files. opencode/plugin.ts (import type), opencode/tui/panel.tsx (static solid-js + @opencode/plugin/tui types), oh-my-pi/completion.ts (dynamic import()), oh-my-pi/context.ts (import type), oh-my-pi/lifetime.ts (dynamic import()), pi/compaction.ts, pi/completion.ts, pi/runtime.ts (import type ×3). Shared source outside src/adapters/: 0, reproduced with the same walker and asserted live.

  • The fixer's 8 is correct; a from "…" grep gives 6 because oh-my-pi/completion.ts and oh-my-pi/lifetime.ts reach their SDK only through import() — the form the grep cannot see.
  • #580's "3" is wrong at its own baseline. Re-counted at ad176f9 with the full form set: 2 files (opencode/plugin.ts, opencode/tui/panel.tsx — the pi and oh-my-pi adapters had no host-SDK imports yet). "3" is not a file count at ad176f9 or at this head; the nearest thing it matches is the number of adapter directories importing an SDK at the current head (opencode, pi, oh-my-pi). Acceptance's "the adapter layer's existing three imports stay clean" should read eight.

Gates

  • bun run typecheck: clean.
  • bun test test/host/import-boundary.test.ts: 11 pass / 0 fail (macOS). bun test test/host/: 265 pass / 0 fail.
  • bun run lint (biome check + format:check): exit 0. COMMENT_LINT_BASE=$(git rev-parse origin/main) bun run lint:comments: clean, 20 added comment lines checked — no ticket numbers, names, or review narrative.
  • CI run 46795704539 at exactly 2bc96e7: coverage, test, lint, smoke, smoke-stable, pi (ubuntu + windows), oh-my-pi (ubuntu + windows) green; test-windows failure as above. One commit normalizing the two message producers should turn the head green.

…e site

The reject() unsupported-dependency site and the core-imports push in
test/host/import-boundary.test.ts interpolated the raw relative(ROOT, file)
into their messages while the host-SDK site used the normalised rel, so on
Windows those two messages carried backslash separators where the tests pin
forward slashes and test-windows alone went red. Both sites now read from
rel, the single normalised path, and a new guard test feeds violations() a
file path with embedded backslashes: every message must stay backslash-free
with / separators, so a raw platform path fails on macOS and Linux too.

Refs #643 #580
…depth

The guard built all three message sites from one backslash-named file
and pinned the total message count. `runtime\nested` is a single
directory name on macOS and Linux, so `..` in the core-imports
specifier climbed out of it and the violation fired; on Windows it is
two directories, the climb stopped one level short, the specifier
resolved outside the adapter tree, and the count assertion went red
over a traversal-depth difference that has nothing to do with message
text (#643, #580).

Assert per (file, source) pair instead: the own-package and host-SDK
sites, whose verdicts never resolve a path, keep the backslash-named
file so a raw platform path leaking into a message stays red on every
OS; the core-imports site resolves from a forward-slash nested file so
its adapter target is identical by construction on every platform.
Each pair now requires only non-empty, backslash-free messages under
the file's portable prefix, so a count difference can neither
masquerade as a portability failure nor hide one.

@ocsi-reviewer ocsi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review of the delta 2bc96e7..f565cf7 — head f565cf77045b7c8d13968ded53a7f799ac2e33d7. Approving.

Delta scope: two commits (b02f664, f565cf7), one file, +36/−4 — exactly what was asked. Nothing previously accepted moved outside the two message sites and the new guard test; the diff touches no rule logic, no host-SDK root set, no import-form bank, and none of the seven previously mutation-killed tests.

1. Both sites normalised at the producer. reject() and the core-imports push now interpolate the existing rel. grep -n 'relative(ROOT' test/host/import-boundary.test.ts returns exactly two hits — :64 (rel) and :113 (target) — both followed by .replaceAll("\\", "/"). All three found.push message sites (:81, :97, :128) use rel; no third helper, no per-assertion workaround.

2. Guard is platform-deterministic by construction — arithmetic re-run independently with node on path.win32 and path.posix (ROOT = C:\repo / /repo):

  • Backslash-named file raw rel: win32 src\runtime\nested\setup.ts, posix src/runtime\nested/setup.ts — both contain a literal backslash, so the own-package and host-SDK pairs fail the no-backslash check on Windows and on macOS/Linux; after replaceAll both are src/runtime/nested/setup.ts, matching the asserted prefix on every platform.
  • Core-imports pair from the forward-slash nested file: ../../adapters/opencode/identity.ts resolves to src/adapters/opencode/identity.ts, startsWith("src/adapters/") true on both win32 and posix — the pair produces its violation everywhere, as the comment claims.
  • Traversal divergence confirmed: from the backslash-named file the same ../.. specifier lands on the adapter under win32 but on adapters/opencode/identity.ts (outside src/, no violation) under posix. The comment's warning that .. "pops a platform-dependent number of components from such a name" is exactly right.

3. The comment overclaims nothing. It states plainly that the core-imports pair's no-backslash check "can only bite on Windows, where the raw path actually differs", and it explicitly disclaims any assertion depending on traversal depth through the backslash-named directory — the overclaim that caused the second red run is gone, and the wording now matches the arithmetic above.

4. Guard bites — reproduced. Reintroducing raw relative(ROOT, file) into reject() turned the seam file red on macOS with the no-backslash assertion:
380 | expect(message.includes("\\")).toBe(false) → Expected: false / Received: true, (fail) every violation message reports one portable path. git restore afterwards; git status --porcelain empty and the file green again (12 pass). So if someone reintroduces the defect, this guard catches it on the nearest local run, not only on Windows.

Gates at this head (all run against the committed tree): bun run typecheck clean; bun test test/host/import-boundary.test.ts 12 pass / 0 fail; bun test test/host/ 266 pass / 0 fail (36 files); bun run lint exit 0; COMMENT_LINT_BASE=$(git rev-parse origin/main) bun run lint:comments clean (35 added comment lines checked).

CI at f565cf7: run 46800947444 completed success — verified job-by-job, all ten jobs green including test-windows, which was red at both prior heads.

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