Skip to content

feat(runtime-host): add full-duplex connection sessions - #1350

Merged
Astro-Han merged 2 commits into
apache:mainfrom
M4n5ter:feat/runtime-host-connection-sessions
Jul 22, 2026
Merged

Astro-Han merged 2 commits into
apache:mainfrom
M4n5ter:feat/runtime-host-connection-sessions

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Jul 22, 2026 •

Copy link
Copy Markdown
Member
English

Summary

This PR establishes the complete full-duplex connection transport/session foundation for the Runtime Host:

  • accepted connections keep a continuous inbound pump and dispatch independent requests concurrently;
  • responses are correlated by requestId and serialized through one bounded FIFO writer, so reverse completion cannot corrupt framing;
  • inbound delivery is bounded by both frame count and encoded bytes, using socket pause/resume backpressure instead of disconnecting a healthy burst before the session can consume it;
  • clean read-side EOF is a typed terminal state: accepted frames drain first, the Host preserves the writable half, and the session flushes replies for work already dispatched before it closes;
  • reset, protocol failure, duplicate active request IDs, the 65th in-flight request, write failure, and overload fail fast and tear down only the offending connection;
  • a connection accepted while the Host composition is still being built resolves the current handler map for each operation, so it becomes usable after Ready without reconnecting.

Boundary and decisions

RuntimeHostConnectionSession owns the lifetime of one accepted connection. The Kernel continues to own operation admission and residency; the session pumps frames, dispatches admitted work, serializes replies, and performs idempotent connection cleanup.

Clean EOF and hard failure intentionally follow different paths. FramedTransport reports read_eof only after its accepted frame queue is exhausted, without disabling writes on the half-open socket. The session then stops accepting input, waits for every already-dispatched admission/handler and writer receipt, and closes after flush. A reset or any other transport/protocol failure retains immediate teardown semantics.

The outbound writer separates synchronous queue admission from asynchronous receipt.flushed completion. Its queue is capped independently at 64 frames and 2 MiB; internal typed causes distinguish the frame and byte bounds without adding a wire-level teardown reason.

FramedTransport retains complete encoded frames while its decoded queue is full, pauses the socket, and resumes as reads release capacity. An oversized partial frame still fails closed, and EOF with a trailing partial frame remains a protocol error rather than clean EOF.

This is the transport/session foundation consumed by later continuity and native-provider slices. Those slices can attach domain behavior without redefining connection flow control or writer semantics.

Scope

This PR does not add Session subscriptions, Interactive domain owners, native-provider frames, surface wiring, or a production cutover. It also deliberately leaves Client-visible teardown reasons and the command outcome journal to the continuity/protocol slice. Existing production entrypoints remain unchanged, and no protocol operation shape changes.

Validation

Real socket and Runtime Host tests cover reverse completion, FIFO flush, the 2 MiB writer cause before the frame limit, clean half-close reply draining, hard reset during pending admission, pre-Ready connections, duplicate/in-flight limits, slow-reader isolation, inbound frame/byte backpressure, and oversized/partial frames. The full Runtime Host suite passes 60 tests; root build, typecheck, lint, and format checks pass.

Tracked by #1167. Architectural context: #853.

简体中文

概要

本 PR 完整建立 Runtime Host 的全双工 connection transport/session 基础:

  • accepted connection 持续运行 inbound pump,并发 dispatch 相互独立的 request;
  • response 通过 requestId 关联,并经过唯一、有界的 FIFO writer 串行写出,因此 handler 逆序完成不会破坏 framing;
  • inbound delivery 同时受 frame 数与 encoded bytes 约束,通过 socket pause/resume 施加背压,避免 session 尚未来得及消费时就把正常 burst 误判为连接故障;
  • clean read-side EOF 是 typed 终态:先排空已经接纳的 frame,Host 保留可写半连接,session 在关闭前 flush EOF 前已 dispatch 工作的回复;
  • reset、protocol failure、duplicate active request ID、第 65 个 in-flight request、写失败与过载保持 fail fast,并且只关闭问题连接;
  • connection 可以在 Host composition 尚未构建完成时被接受,并在每次 operation dispatch 时解析当前 handler map,因此 Host Ready 后无需重连即可正常使用。

边界与决策

RuntimeHostConnectionSession 负责一个 accepted connection 的生命周期。Kernel 继续拥有 operation admission 与 residency;session 负责 pump frame、dispatch 已接纳工作、串行回复与幂等 connection cleanup。

clean EOF 与 hard failure 有意采用不同路径。FramedTransport 只在已经接纳的 frame queue 排空后报告 read_eof,并且不会禁用 half-open socket 的写侧。session 随后停止接收输入,等待 EOF 前已 dispatch 的 admission/handler 与 writer receipt 全部完成,并在 flush 后关闭。reset 或其他 transport/protocol failure 仍立即 teardown。

outbound writer 区分同步 queue admission 与异步 receipt.flushed 完成。队列分别受 64 frame 与 2 MiB 上限约束;内部 typed cause 会区分 frame bound 与 byte bound,但不会因此增加 wire-level teardown reason。

FramedTransport 会在 decoded queue 满时保留完整 encoded frame、暂停 socket,并在 read 释放容量后恢复。超大的 partial frame 仍然 fail closed;EOF 末尾存在 partial frame 时仍属于 protocol error,而不是 clean EOF。

这是后续 continuity 与 native-provider slice 共用的 transport/session 基础。后续 slice 可以接入各自领域行为,无需重新定义 connection flow control 或 writer 语义。

范围

本 PR 不增加 Session subscription、Interactive domain owner、native-provider frame、surface wiring 或 production cutover。Client 可见的 teardown reason 与 command outcome journal 明确保留给 continuity/protocol slice。现有 production entrypoint 保持不变,protocol operation shape 也没有变化。

验证

真实 socket 与 Runtime Host 测试覆盖 handler 逆序完成、FIFO flush、在 frame limit 前触发 2 MiB writer cause、clean half-close 后回复排空、pending admission 期间的 hard reset、pre-Ready connection、duplicate/in-flight 上限、慢读者隔离、inbound frame/byte backpressure,以及 oversized/partial frame。Runtime Host 全量 60 项通过;根级 build、typecheck、lint 与 format check 通过。

由 #1167 跟踪;架构背景见 #853。

@Astro-Han Astro-Han 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.

Approving. The connection seam is in good shape: I verified the body's claims against the code and by running the suite (full runtime-host suite green, plus a 21x soak of the two new suites), and the high-risk paths hold up under tracing: pause/resume pairing, the dual inbound budgets, exactly-once writer receipts, pre-Ready handler resolution, and the admission/disconnect races. One P2 that should be fixed or explicitly tracked before this becomes the frozen seam, plus a few items this slice owes the next ones.

P2: clean EOF leaves read() pending forever on a half-open socket.

On end, #ended is set and #drainInbound() flushes the decoder and sets #decoderEnded, but no terminal state is ever established (framed-transport.ts:57, :163). A later read(0) finds an empty queue and no failure, so it installs a waiter with no timeout that nothing will ever wake (framed-transport.ts:68). I reproduced this with a socket stub: after a clean end with the queue drained, read() stays pending with no timer.

Today both sides use Node's default allowHalfOpen: false, so the close event rejects the waiter and masks this. But this PR positions the connection layer as the seam later slices must not renegotiate, and the EOF terminal state is exactly the kind of semantics that should be pinned here: the body already defines EOF ordering for a trailing partial frame, while clean EOF is left undefined. The first slice that enables allowHalfOpen to drain host-side responses will hit it. The fix is small (establish a terminal state once the decoder drains at EOF); whether EOF means immediate teardown or drain-responses-then-close is a product call that belongs to you.

Owed to the next slices (not blocking):

  • Teardown is reason-less on both sides. Overload, duplicate request IDs, and protocol violations all surface to the client as an undifferentiated close, with nothing recorded host-side. When the continuity slice implements the safe/semantic retry policies, it cannot distinguish "evicted for overload" (retryable on a fresh connection) from transport loss. A close frame or structured host-side record would settle this.
  • Seal-before-flush leaves a response-loss window: command residency releases when the response is computed, not delivered, so shutdown can destroy the transport inside the grace window and drop a response whose effects already settled. That matches the documented "Host-owned and settles" intent, but the continuity slice needs an outcome journal to close it.
  • The writer's 2 MiB byte bound has no in-repo test (I probe-verified the mechanism: it trips before the 64-frame bound and settles receipts exactly once). The first slice with large payloads inherits an untested guardrail.

Test evidence gap worth fixing while cheap:

The "non-reading Client overload" test likely closes the connection via the 65th in-flight request rather than the writer's frame/byte budget, since the loop floods requests as fast as it can. It proves isolation, but not that the writer budget is the eviction cause. Pinning the close reason (or throttling ingress below the in-flight cap) would make it prove what the body claims.

Nothing else turned up: build, typecheck, biome, git diff --check, and the behavior-level assertions in the new tests all check out.

简体中文

通过。connection seam 的整体质量不错:我把 PR 描述里的声明逐条对照代码并跑测试验证过(runtime-host 全量套件通过,两个新套件另做 21 轮 soak),高风险路径在逐行追踪下都成立:pause/resume 配对、双 inbound 预算、writer receipt 恰好一次 settle、pre-Ready 的 handler 解析、admission/disconnect 竞态。有一个 P2 应该在这个 seam 冻结前修复或明确跟踪,另有几项是这个 slice 欠后续 slice 的。

P2:干净 EOF 会让 read() 在 half-open socket 上永远悬挂。

收到 end 时,#ended 被置位,#drainInbound() 排空 decoder 并设置 #decoderEnded,但始终没有建立终态(framed-transport.ts:57、:163)。之后的 read(0) 发现队列为空且没有 failure,于是挂上一个无超时的 waiter,再也没有东西会唤醒它(framed-transport.ts:68)。我用 socket stub 复现过:队列排空后收到干净 end,read() 一直 pending。

今天两侧都用 Node 默认的 allowHalfOpen: false,随后的 close 事件会拒绝 waiter,把问题掩盖了。但这个 PR 把 connection 层定位为后续 slice 不得重新协商的 seam,而 EOF 终态恰恰是应该在这里钉死的语义:PR 描述已经定义了尾随 partial frame 的 EOF 顺序,干净 EOF 却是未定义的。第一个为了排空 host 端响应而打开 allowHalfOpen 的 slice 会 directly 踩中。修复很小(EOF 时 decoder 排空后建立明确终态);EOF 意味着立即 teardown 还是先排空响应再关,是产品语义,由你定。

欠后续 slice 的(不阻塞):

  • teardown 两侧都没有原因信息。过载、重复 request ID、协议违例对 client 都是无差别的 close,host 侧也没有记录。continuity slice 实现 safe/semantic 重试策略时,无法区分"因过载被驱逐"(可在新连接上重试)和"传输层断开"。一个 close frame 或 host 侧结构化记录可以定下这件事。
  • seal-before-flush 留下响应丢失窗口:command 的 residency 在响应算出时释放,而不是送达时,因此 shutdown 可以在宽限窗口内销毁 transport,丢掉一个副作用已经 settle 的响应。这与"Host-owned and settles"的既定语义一致,但 continuity slice 需要 outcome journal 来关上这个口。
  • writer 的 2 MiB 字节上限在仓内没有测试(我用探针验证过机制:它在 64 帧上限之前触发,receipt 恰好 settle 一次)。第一个带大 payload 的 slice 会继承一条没测过的防线。

值得顺手补的测试证据缺口:

"non-reading Client overload"测试里连接实际很可能是被第 65 个 in-flight request 关掉的,而不是 writer 的 frame/byte 预算,因为循环在全速灌请求。它证明了隔离,但没证明关闭原因是 writer 预算。把关闭原因钉死(或把入站速度限制在 in-flight 上限以下),才能让它证明 PR 描述所声称的内容。

其他没有发现问题:build、typecheck、biome、git diff --check,以及新测试里的行为级断言都成立。

@M4n5ter
M4n5ter force-pushed the feat/runtime-host-connection-sessions branch from c29a77b to 3686114 Compare July 22, 2026 16:27
@M4n5ter

M4n5ter commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@Astro-Han This finding was valid. I fixed it in 36861148 and kept the change inside the connection seam.

  • FramedTransport now reports typed read_eof only after accepted frames are drained. Read-side EOF does not disable the writable half; a later real transport failure takes precedence.
  • The Host listener now explicitly uses half-open sockets. On clean EOF, RuntimeHostConnectionSession stops accepting input, waits for every request dispatched before EOF and its writer receipt, then closes after flush.
  • Hard reset, protocol/write failure, or an observable fatal close during that drain still tears down immediately and calls onTeardown exactly once.
  • The writer now distinguishes its internal frame and byte overload causes. A real-socket regression fills valid ~48 KiB frames and proves the 2 MiB byte bound fires before the 64-frame bound, with every accepted receipt rejected by that exact cause.

There is one transport-level boundary worth stating explicitly: after a peer has already sent FIN, some platforms do not expose a second peer-disappearance event until the Host writes again. The session reacts immediately when transport.closed is observable; otherwise the existing writer-failure path observes it at the next reply. I did not add a heartbeat or probe for this.

Client-visible teardown reasons and the command outcome journal remain deliberately deferred to the continuity/protocol slice, as suggested.

The Runtime Host suite is now 60/60, and root build, typecheck, lint, and format checks pass on the rebased branch.

简体中文

@Astro-Han 这个 finding 是有效的。我已在 36861148 中修复,并将改动保持在 connection seam 内。

  • FramedTransport 现在只会在已经接纳的 frame 排空后报告 typed read_eof。读侧 EOF 不会禁用可写半连接;之后出现的真实 transport failure 会取得优先级。
  • Host listener 现在显式使用 half-open socket。clean EOF 后,RuntimeHostConnectionSession 停止接收输入,等待 EOF 前已 dispatch 的全部 request 与 writer receipt,再在 flush 后关闭。
  • hard reset、protocol/write failure,或 drain 期间可观察到的 fatal close 仍会立即 teardown,并确保 onTeardown 恰好调用一次。
  • writer 现在区分内部 frame/byte overload cause。真实 socket 回归使用合法的约 48 KiB frame,证明 2 MiB byte bound 会在 64-frame bound 前触发,并且所有已接纳 receipt 都由同一个真实 cause 拒绝。

这里有一个需要明确的 transport 边界:peer 已经发送 FIN 后,部分平台不会在 Host 再次写入前暴露第二个“peer 已消失”事件。transport.closed 可观察时 session 会立即响应;否则现有 writer-failure 路径会在下一次回复时发现它。本次没有为此增加 heartbeat 或探针。

按照建议,Client 可见的 teardown reason 与 command outcome journal 仍明确留给 continuity/protocol slice。

当前 Runtime Host 全量为 60/60,rebase 后根级 build、typecheck、lint 与 format check 也均通过。

@Astro-Han

Copy link
Copy Markdown
Contributor

LGTM! Merging.

@Astro-Han
Astro-Han merged commit 09e7b0e into apache:main Jul 22, 2026
3 checks passed
@M4n5ter
M4n5ter deleted the feat/runtime-host-connection-sessions branch July 22, 2026 23:58
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.

2 participants