Skip to content

refactor(host): open application channels over supplied streams - #7028

Merged
me2seeks merged 6 commits into
apache:rust-previewfrom
me2seeks:refactor/host-channel-opener
Nov 20, 2026
Merged

me2seeks merged 6 commits into
apache:rust-previewfrom
me2seeks:refactor/host-channel-opener

Conversation

@me2seeks

@me2seeks me2seeks commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Separate the Host client stream-opener work from #7011. Local IPC behavior stays unchanged. A remote embedding can supply a new async stream for each Host channel and reuse the existing authenticated WebSocket protocol. No WSL, peer engine, or crypto dependency changes are included.

Refs #7011

Verification

  • Windows: cargo test --locked -p maka-host-runtime --test integration host_opens_independent_supplied_channels passed. It exercises invalid Bearer rejection, independent call channels, and closing the remote session without retiring the Host.
  • cargo fmt -p maka-host-runtime -- --check and staged ASF header and whitespace checks passed.
  • Cross-machine smoke: native Windows Rust client reached an isolated Ubuntu Host via SSH local forward (not direct reachability), completed independent HostLogs reads, and reconnected without re-finalizing the active credential.

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Maka implemented and tested the client stream abstraction and split this PR.

Checklist

  • Tests cover the change and fail without it
  • Format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

English

Thanks for splitting this out — keeping each call on its own stream with CallChannelOpener pinning the authenticated target, epoch, and session is the right core shape. Before merging, there are a few contract and abstraction issues we should clean up:

  1. Put the shared boundary at "open a channel ready for Host wire framing", not before WebSocket upgrade:

    • Currently Target::open unconditionally runs an HTTP/WebSocket upgrade and Client::connect_websocket unconditionally opens an extra stream for Request::FinalizeAccess. Any transport that already carries raw multiplexed streams (such as the WSL stdio mux path) can't reuse this without forcing an HTTP upgrade on every substream.
    • Instead, let Client accept a transport-neutral OpenChannel (Arc<dyn Fn() -> BoxFuture<'static, io::Result<Channel>> + Send + Sync>) with an optional client_instance: Option<Arc<str>> via Client::connect_on(...), and keep WebSocket upgrade + Bearer auth as an adapter wrapping OpenChannel (with FinalizeAccess kept separate from routine connects so already-active credentials don't pay an extra upgrade + SQLite write transaction on every connect).
    • Also drop + Sync from ChannelIo (AsyncRead + AsyncWrite + Unpin + Send + 'static is sufficient since each stream has a single owner, and + Sync blocks !Sync mux stream wrappers), and return io::Result<Channel> from the opener instead of collapsing errors into String early. Keep local-owner control separate so supplying a stream doesn't imply local control authority.
  2. Allow expected_epoch: Option<Uuid> on remote connect:

    • connect_websocket requires epoch: Uuid, but maka access prepare (Pairing) only produces rootId, credentialId, and credential — there is no epoch in the pairing payload, and Client::status is still local-IPC-only. A real remote client holding pairing info cannot call this constructor (the test works around it by reading host.epoch() in-process).
    • Client::open already supports epoch: Option<Uuid>: accept Option<Uuid> on initial connect, learn the epoch from the authenticated Connected response, and pin it on all subsequent UI and call channels (and ideally allow status over an OpenChannel too).
  3. Don't silently downgrade Client::quit() to close() for remote targets:

    • In request_quit, Target::WebSocket returns self.close().await (Ok(())), which breaks quit()'s contract ("stop its Host when no other session remains") and is inconsistent with publication() / configure() returning Err("Host control requires the local owner"). Reject non-local quit explicitly and let callers invoke close() directly for remote sessions.
  4. Preserve safe diagnostic detail in websocket::connect_on_stream:

    • Right now every non-HTTP failure (premature EOF, I/O error, protocol mismatch) collapses into "Host WebSocket upgrade failed". Keep the HTTP branch status-only, but surface the I/O ErrorKind or safe protocol error category so remote connection failures are debuggable.
  5. Tighten the integration test in remote_host.rs to assert behavior rather than internals:

    • Drop assert_eq!(opened.load(Ordering::Relaxed), 5) — it locks in internal handshake/pairing call counts rather than external behavior.
    • Instead of opening two call channels and immediately dropping them, exercise an actual independent call (e.g. confirm one channel completes HostLogs.read after the other finishes/closes).
    • Note that Host::open uses Options::default() (LifecycleMode::Service), where even a local quit_ephemeral wouldn't retire the Host; if the test is meant to prove closing a remote session does not retire an ephemeral Host, configure LifecycleMode::Ephemeral explicitly, and assert that remote quit/publication/configure are rejected.
简体中文

感谢拆出这个独立 PR——每条 call 独立开流、由 CallChannelOpener 固定已认证的 target / epoch / session,这部分核心方向是对的。合入前有几处契约和抽象边界建议先理干净:

  1. 把共用边界放在“打开一条可直接跑 Host wire 帧的 Channel”,而不是焊在 WebSocket 升级之前:

    • 目前 Target::open 无条件执行 HTTP/WebSocket Upgrade,Client::connect_websocket 每次建连也无条件多开一条流发 Request::FinalizeAccess。像 WSL stdio mux 这种本身已经提供原始流复用的传输都没法直接复用,否则每条子流都要强跑一遍 HTTP Upgrade。
    • 建议让 Client 底层只认传输无关的 OpenChannel(Arc<dyn Fn() -> BoxFuture<'static, io::Result<Channel>> + Send + Sync>)以及可选的 client_instance: Option<Arc<str>>(如 Client::connect_on(...)),把 WebSocket Upgrade + Bearer 鉴权做成包在 OpenChannel 外面的独立适配器(FinalizeAccess 也从常规建连里拆出,避免已激活凭证每次重连都白跑一次握手和 SQLite 写事务)。
    • 同时建议去掉 ChannelIo 上不必要的 + Sync(单条流由单一 owner 独占,AsyncRead + AsyncWrite + Unpin + Send + 'static 就够了,强加 Sync 会挡住不少 !Sync 的 mux 流包装),opener 返回 io::Result<Channel> 保留底层错误类型;本地 owner 控制入口继续独立,不让“能提供 stream”等同于拥有本地控制权。
  2. 远端首次连接允许 expected_epoch: Option<Uuid>:

    • connect_websocket 强制要求传入 epoch: Uuid,但现有 maka access prepare(Pairing)只包含 rootId、credentialId 和 credential,根本没有 epoch,且 Client::status 仍写死 Local IPC。真实远端客户端拿着配对信息根本无法调用这个构造函数(当前测试是靠同进程直接读 host.epoch() 绕过的)。
    • 现有 Client::open 已经支持 epoch: Option<Uuid>:首次连接允许传 None,从认证后的 Connected 响应接收 epoch,后续 UI 和 call channel 一律 pin 死该 epoch(同时也可以顺手支持通过 OpenChannel 查询 status)。
  3. 不要在远端目标上把 Client::quit() 静默降级为 close():

    • request_quit 对 Target::WebSocket 直接返回 self.close().await(Ok(())),违背了 quit() “在无其他 session 时停止 Host”的契约,也与同文件 publication() / configure() 返回 Err("Host control requires the local owner") 不一致。建议对非本地目标明确拒掉 quit,由调用方在远端退出时显式调用 close()。
  4. websocket::connect_on_stream 保留安全的诊断信息:

    • 目前除 Error::Http 外,提前 EOF、I/O 错误、协议响应异常都会被抹成同一句 "Host WebSocket upgrade failed"。HTTP 分支继续只报状态码即可,I/O 错误建议保留 ErrorKind,协议错误保留安全的错误类别,方便远端排障。
  5. 收紧 remote_host.rs 的测试断言,测外部行为而非实现细节:

    • 去掉 assert_eq!(opened.load(Ordering::Relaxed), 5)——它锁死了内部握手/配对/anchor 调用次数,属于典型的实现耦合断言。
    • 不要把两条并发打开的 call channel 直接 drop,实际跑一次独立调用(例如一条结束后另一条仍能完成 HostLogs.read)。
    • 另外 Host::open 用的是默认 Options::default()(LifecycleMode::Service),该模式下就算走本地 quit_ephemeral Host 也不会退;如果测试目标是证明“关闭远端 session 不会误退 ephemeral Host”,fixture 需要显式配置 LifecycleMode::Ephemeral,并补上远端调 quit/publication/configure 被拒绝的断言。

@github-actions github-actions Bot added the effort/L Under 1000 readable lines label Nov 20, 2026
@me2seeks

Copy link
Copy Markdown
Contributor Author

Cross-machine smoke at bc95fe3c71: a native Windows Rust client connected to a disposable Host on a separate Ubuntu machine through an SSH local forward. The Host WebSocket listener remained loopback-only. Pending credentials were rejected until explicitly finalized; initial admission learned and checked the Root and epoch; two independent channels completed HostLogs.read. After close, the same active credential reconnected with the pinned epoch without finalizing again, and both reads completed again. The remote session count returned to zero without retiring the ephemeral Host. This verifies the Host protocol across machines, not direct network reachability or an iOS/P2P path.

@me2seeks
me2seeks marked this pull request as ready for review November 20, 2026 09:19
@me2seeks

Copy link
Copy Markdown
Contributor Author

CI comparison for eaf93d4: the initial Windows Desktop shard failed waiting for MAIN_DRAFT; the targeted rerun failed on the Close side pane control. The unmodified rust-preview base 874817f (run 44678458639, job 136099594884) exhibits both identical assertions in those same tests. This is baseline evidence for those failures, not a request to waive the required check. The other lanes passed.

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM — separating OpenChannel / connect_on from websocket::channel_opener and finalize_access, allowing Option<Uuid> on initial connect, rejecting non-local quit, and tightening the integration test around real concurrent HostLogs.read calls and LifecycleMode::Ephemeral makes this much cleaner. Thanks for the cross-machine smoke test as well.

@me2seeks
me2seeks merged commit 90af3ae into apache:rust-preview Nov 20, 2026
26 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/L Under 1000 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants