Skip to content

refactor(ocap-kernel): transport-neutral channel seam (netlayer phase 1)#970

Draft
rekmarks wants to merge 2 commits into
rekm/netlayer-plansfrom
rekm/netlayer-1
Draft

refactor(ocap-kernel): transport-neutral channel seam (netlayer phase 1)#970
rekmarks wants to merge 2 commits into
rekm/netlayer-plansfrom
rekm/netlayer-1

Conversation

@rekmarks

Copy link
Copy Markdown
Member

Netlayer Phase 1 — internal seam refactor

Phase 1 of the 6-phase pluggable-netlayer effort (issue #968; see
docs/plans/netlayer/master.md and docs/plans/netlayer/phase-1.md). This is the
first PR in the stack; Phases 2–6 build on it.

Mechanical, behavior-preserving refactor of the ocap-kernel remote-comms subsystem
to create a transport-neutral internal seam that later phases extract into standalone
@metamask/netlayer* packages. No runtime behavior changes; log strings and
error-classification outcomes are preserved.

What changed

  • Replaced the libp2p-typed Channel with a neutral NetworkChannel
    ({ peerId, read, write, close, setInactivityTimeout }) and introduced a
    ChannelProvider type describing a channel-based transport.
  • Moved every libp2p touchpoint (lpStream framing, inactivity timeout, close-event
    diagnostics, raw read-error sniffing/mapping) into connection-factory.ts, now the
    sole ChannelProvider. transport.ts, channel-utils.ts, handshake.ts, and
    types.ts are libp2p-import-free.
  • Added neutral error classes to @metamask/kernel-errors: ChannelResetError,
    IntentionalDisconnectError, MessageTooLargeError. connection-factory.ts maps raw
    libp2p read errors onto them so the engine never imports libp2p error types.
  • Split transport.ts into makeChannelNetlayer (synchronous engine) plus a
    signature-compatible initTransport wrapper. The runtimes' initTransport call sites
    are unchanged.

Deviations from the phase-1 plan (recorded in master.md)

  • ChannelProvider.closeChannel(channel) is single-arg (peerId dropped; carried by
    NetworkChannel.peerId). Graceful-close/abort body moved to private #closeStream.
  • dialIdempotentdial; onInboundConnectiononInboundChannel.
  • The three neutral error classes carry no data (cause-only).
  • AbortController wiring uses option (A): initTransport owns the controller, shares
    signal with the provider and the whole stopController with the engine.
  • makeChannelNetlayer is intentionally not exported from the package barrel.

Verification

  • yarn lint:fix, yarn build, yarn test:dev:quiet (root) all green.
  • @metamask/kernel-errors and @metamask/ocap-kernel unit suites pass; coverage is at
    or above baseline on all four metrics for both packages.
  • @ocap/kernel-test integration, @metamask/kernel-node-runtime, and
    @metamask/kernel-browser-runtime suites pass.
  • Grep audits confirm no libp2p imports in transport.ts/channel-utils.ts/
    handshake.ts/types.ts; libp2p error types named only in connection-factory.ts
    (+ its test); runtimes' initTransport call sites unchanged.

🤖 Generated with Claude Code

…ayer phase 1)

Phase 1 of the pluggable-netlayer effort (issue #968). Mechanical, behavior-
preserving refactor of the remote-comms subsystem to create a transport-neutral
internal seam that later phases extract into standalone @metamask/netlayer*
packages.

- Replace the libp2p-typed `Channel` with a neutral `NetworkChannel`
  (`{ peerId, read, write, close, setInactivityTimeout }`) and add a
  `ChannelProvider` type describing a channel-based transport.
- Move all libp2p touchpoints (lpStream framing, inactivity timeout,
  close-event diagnostics, read-error sniffing/mapping) into
  `connection-factory.ts`, the sole `ChannelProvider`. `transport.ts`,
  `channel-utils.ts`, `handshake.ts`, and `types.ts` are libp2p-import-free.
- Add neutral error classes to @metamask/kernel-errors: `ChannelResetError`,
  `IntentionalDisconnectError`, `MessageTooLargeError`; the provider maps raw
  libp2p read errors onto them so the engine never imports libp2p error types.
- Split `transport.ts` into `makeChannelNetlayer` (synchronous engine) plus a
  signature-compatible `initTransport` wrapper; runtimes are untouched.

Deviations recorded in docs/plans/netlayer/master.md; phase-2.md revised
against the landed code.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 71.49%
⬆️ +0.13%
8901 / 12449
🔵 Statements 71.32%
⬆️ +0.13%
9051 / 12690
🔵 Functions 72.7%
⬆️ +0.13%
2152 / 2960
🔵 Branches 64.95%
⬆️ +0.09%
3587 / 5522
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
packages/kernel-errors/src/constants.ts 93.75%
🟰 ±0%
100%
🟰 ±0%
80%
🟰 ±0%
90.9%
🟰 ±0%
20
packages/kernel-errors/src/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/kernel-errors/src/errors/ChannelResetError.ts 100% 100% 100% 100%
packages/kernel-errors/src/errors/IntentionalDisconnectError.ts 100% 100% 100% 100%
packages/kernel-errors/src/errors/MessageTooLargeError.ts 100% 100% 100% 100%
packages/kernel-errors/src/errors/index.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/remotes/types.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/remotes/platform/channel-utils.ts 100%
🟰 ±0%
80%
⬇️ -5.71%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/remotes/platform/connection-factory.ts 95.41%
⬆️ +0.65%
91.17%
⬆️ +1.89%
97.87%
⬆️ +0.51%
95.37%
⬆️ +0.67%
182-184, 194, 500, 695-696, 701-705, 764, 777
packages/ocap-kernel/src/remotes/platform/handshake.ts 96.49%
🟰 ±0%
92.85%
🟰 ±0%
75%
🟰 ±0%
96.49%
🟰 ±0%
75, 81
packages/ocap-kernel/src/remotes/platform/peer-state-manager.ts 100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
100%
🟰 ±0%
packages/ocap-kernel/src/remotes/platform/reconnection-lifecycle.ts 88.42%
🟰 ±0%
90.24%
🟰 ±0%
80%
🟰 ±0%
88.42%
🟰 ±0%
123-127, 147-148, 246-251, 299-300
packages/ocap-kernel/src/remotes/platform/transport.ts 82.49%
⬆️ +1.67%
84.11%
⬆️ +0.35%
73.52%
⬇️ -0.76%
82.49%
⬆️ +1.67%
146, 149, 170, 205-206, 225-226, 276-277, 295-304, 345, 379-397, 421, 512, 555-558, 578, 602-607, 610-611, 623-627, 681, 713, 731-733, 742, 755, 835, 864
Generated in workflow #4504 for commit 6587aa0 by the Vitest Coverage Report Action

Add Unreleased changelog entries for the netlayer phase 1 changes: the three
neutral transport error classes in @metamask/kernel-errors and the
transport-neutral channel seam refactor in @MetaMask/ocap-kernel.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@rekmarks rekmarks changed the base branch from main to rekm/netlayer-plans July 10, 2026 23: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