Commit fb47068
feat(ocap-kernel): leverage libp2p v3 features in remote communications (#915)
Closes #903
## Summary
Adopts several libp2p v3 features to improve error handling,
observability, and robustness of the remote communications layer:
- **Async protocol handlers with auto-abort** — inbound connection
handler is now `async`, so handler rejections automatically abort the
stream instead of silently dropping errors
- **Fine-grained stream close events** — v3 streams emit close events
with `{ error, local }` properties for better diagnostics distinguishing
local vs remote and clean vs error closes
- **Stream inactivity timeout** — configurable
`streamInactivityTimeoutMs` (default 120s) detects dead streams via
bidirectional silence, with a 5s minimum floor to prevent self-DoS
- **`peer:disconnect` safety net** — listens for peer-level disconnect
events as a fallback when `readChannel` cleanup misses a disconnection,
with `signal.aborted` guard to avoid spurious reconnection during
shutdown
- **`StreamResetError` detection** — recognizes remote stream resets
without permanently marking the peer as intentionally closed (prevents
malicious peers from suppressing reconnection)
- **Graceful `closeChannel` rewrite** — uses `channel.stream.close()`
directly instead of the fragile `unwrap()` pattern, with abort fallback
- **Graceful shutdown** — `stop()` sends FIN to remote peers so they see
clean stream ends rather than `StreamResetError`
- **`Channel` type gains `stream`** — direct access to stream lifecycle
APIs, avoiding `unwrap()`
- **`TooManyOutboundProtocolStreamsError` handling** — re-throws
immediately since trying other addresses won't help
- **`writeWithTimeout` stream status guard** — fast-fails on
already-closed streams before attempting a write
- **`RemoteHandle.giveUp()` method** — transport-level give-up now
properly cleans up (stops ACK retransmission + rejects pending messages
+ rejects pending URL redemptions)
- **Flaky multi-peer reconnection E2E test** — fixed by adding
`maxConnectionAttemptsPerMinute: 500` (rate limiter was the root cause),
parallel restarts, and `ackTimeoutMs: 5000`
## Test plan
New unit tests cover stream status fast-fail (`it.each` over 4
statuses), `peer:disconnect` handler (registration, forwarding, relay
filtering, shutdown guard), stream close event diagnostics (4 scenarios
via `it.each`), `TooManyOutboundProtocolStreamsError` rethrow,
`StreamResetError` reconnection behavior, `closeChannel`
graceful/abort/double-fail paths, `stop()` stream closing,
`RemoteHandle.giveUp()`, and `streamInactivityTimeoutMs` query string
round-trip. New E2E test file (`libp2p-v3-features.test.ts`) covers
stream inactivity timeout recovery end-to-end.
## Suggested review order
1. `packages/ocap-kernel/src/remotes/types.ts` — `Channel` type gains
`stream`, new `PeerDisconnectHandler` and `streamInactivityTimeoutMs`
option. Read this first to understand the type changes everything else
builds on.
2. `packages/ocap-kernel/src/remotes/platform/constants.ts` — new
`STREAM_INACTIVITY_TIMEOUT_MS` and `MIN_STREAM_INACTIVITY_TIMEOUT_MS`
constants.
3. `packages/ocap-kernel/src/remotes/platform/channel-utils.ts` + test —
`writeWithTimeout` stream status guard (small, self-contained).
4. `packages/ocap-kernel/src/remotes/platform/connection-factory.ts` +
test — `peer:disconnect` listener, async inbound handler, `closeChannel`
rewrite, `TooManyOutboundProtocolStreamsError`. Core plumbing.
5. `packages/ocap-kernel/src/remotes/platform/transport.ts` + test —
biggest file: `StreamResetError` handling, `registerChannel` stream
events/timeout, `peer:disconnect` safety net, graceful `stop()`. Builds
on 3 and 4.
6. `packages/ocap-kernel/src/remotes/kernel/RemoteHandle.ts` + test —
new `giveUp()` method and `#handleAckTimeout` cleanup path.
7. `packages/ocap-kernel/src/remotes/kernel/RemoteManager.ts` + test —
`#handleRemoteGiveUp` wiring to `giveUp()`.
8. `packages/kernel-node-runtime/test/e2e/remote-comms.test.ts` — flaky
multi-peer reconnection fix (rate limit + parallel restarts).
9. `packages/kernel-node-runtime/test/e2e/libp2p-v3-features.test.ts` —
new E2E tests for the v3 features.
10. `packages/kernel-browser-runtime/src/utils/comms-query-string.ts` +
test — `streamInactivityTimeoutMs` query string support (trivial).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches core remote-communications transport/retry logic (stream
lifecycle, reconnection triggers, and give-up cleanup), which can affect
message delivery and connection stability; changes are well-covered by
new/updated unit and E2E tests.
>
> **Overview**
> Improves remote comms robustness by adopting libp2p v3
stream/connection signals: `Channel` now carries the underlying
`stream`, inbound protocol handlers can be async, and the transport
configures per-stream inactivity timeouts and logs detailed stream close
diagnostics.
>
> Reworks failure handling and cleanup paths: fast-fail writes on
non-`open` streams, treat `StreamResetError`/`peer:disconnect` as
reconnection signals (with relay filtering and shutdown guards), handle
`TooManyOutboundProtocolStreamsError` as a non-retryable dial error, and
make shutdown/`closeChannel` prefer graceful `stream.close()` with abort
fallback.
>
> Fixes give-up semantics so pending messages and URL redemptions are
always rejected via new `RemoteHandle.giveUp()` and updated
`RemoteManager` wiring, adds `streamInactivityTimeoutMs` support to
comms query strings, and expands/adjusts unit + E2E coverage (including
a new `libp2p-v3-features` E2E suite and less-flaky multi-peer
reconnection timing/rate-limit settings).
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
52fb402. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent da4f95c commit fb47068
18 files changed
Lines changed: 965 additions & 135 deletions
File tree
- packages
- kernel-browser-runtime/src/utils
- kernel-cli/src/commands
- kernel-node-runtime/test/e2e
- ocap-kernel/src/remotes
- kernel
- platform
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
31 | 34 | | |
32 | 35 | | |
33 | 36 | | |
| |||
Lines changed: 291 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
0 commit comments