Repository navigation
fix(runtime): let the phone end a native chat stream by the request that opened it - #23058
brennanb2025 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Host request-addressed teardown:
nativeChat.subscribenow registers itsctx.requestIdwith the stream registry, andnativeChat.unsubscribecarryingrequestIdreleases exactly that registration viareleaseSubscriptionByRequest— never falling back to the token or prefix sweep. The no-requestIdpath is unchanged compat code for older phones. - Wire contract: optional
requestIdadded toNativeChatUnsubscribe. The schema is non-strict, so an older host drops the field and keeps using the token. - Phone transports: new
withUnsubscribeRequestIdgates the field toterminal.unsubscribe/nativeChat.unsubscribe; both the direct registry (dispose) and the relay (sendUnsubscribe) call it, with the relay's token-onlyhasLiveOwnersibling skip still evaluated first so old hosts don't evict a live sibling. - Tests: a dispatcher-level suite on the real runtime service covers own-request release while setup is pending and after it settles, stale/unknown/self/unaddressed ids as no-ops, a connection-id-less no-op, and the legacy token fallback; the direct and relay transport tests were updated to assert the new field.
I traced the registry path (register removes the replaced entry's request index synchronously before installing the new one, so a stale requestId is a no-op even though cleanup is async), the connection scoping (releaseByRequest keys on the caller's per-socket connectionId, unique per MobileSocketWiring/relay socket), and the frame-id identity (unique rpc-<n>-<Date.now()> direct ids; relay forwards the E2EE payload so the inner id is preserved). The method-agnostic release is deliberate and documented. The one gap — the desktop web client still unsubscribing by token — is explicitly deferred in the PR body.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
…lper The direct transport's terminal stop now uses the same helper as native chat and relay, so one list decides which stops carry the request id. The native chat token path is also used by the desktop web client, so its compatibility label now says so.
|
Closing as superseded by #22939. This PR gave native chat streams request-addressed unsubscribe, so a stale unsubscribe could not end a newer feed. #22939 now reaches the same guarantee more simply: each phone chat subscription sends its own With a unique token per subscription, two chat screens never evict each other and an unsubscribe can only name its own feed. That makes this PR's host index entry and |

ELI5
When you leave an agent chat on your phone, the phone tells the desktop "stop sending me updates for chat X". If the phone opened chat X twice in quick succession (for example going back and forth between screens), the desktop could not tell which opening the "stop" was about, so a late "stop" for the old opening could end the new one and the chat on your phone would stop updating. Now the phone says "stop the updates I asked for with request 42", and the desktop ends exactly that one. This is the same fix #23006 made for terminals, applied to agent chats.
What Changed
Before:
nativeChat.unsubscribenamed the chat stream by its token, which is the agent and session id (or a token the phone supplies). That token is the same every time the phone opens that chat. If the phone opened a new stream for the chat and then closed the old one, and the old one's stop reached the desktop after the new stream had registered, the desktop ended the new stream. The chat then stopped showing new messages until it was reopened.After: the phone adds
requestIdtonativeChat.unsubscribe, set to the id of thenativeChat.subscriberequest that opened the stream. A desktop with this change ends only the stream that request opened. If that request already ended, was replaced, or never registered, the stop does nothing, and it never falls back to the token or to the "end every chat on this connection" sweep. The token is still sent, so a desktop without this change drops the new field and behaves exactly as today.The mechanism, reusing what #23006 added:
nativeChat.unsubscribewithrequestIdcalls the registry's release-by-request, which releases only that exact registration. A request that has since been replaced by a newer stream on the same token is a no-op.requestId, the existing token and sweep paths are unchanged and marked as compatibility code for the desktop web client and phones that predate the field.requestIdequal to the frame id they sent the subscribe with. One small function decides which stop messages carry it (terminal and chat), and both transports use it.requestIdis added after that decision, so against a desktop without this change the check still fires and the newer stream is not ended.Why
The chat stream has the same identity problem as the terminal stream: ending it names what it watches rather than the request that created it. The registry index added in #23006 already addresses a stream by its request on every transport, whether or not the request handler is still running, so chat only needs to register with its request id and use that path. The chat handler returns as soon as its file watch is set up, which is why ending it through the per-request dispatch cancellation was never an option: that cancellation is gone by the time the phone leaves.
Alternatives considered:
requestIdis understood. Not needed: an older desktop drops the unknown field and uses the token, which is today's behavior, and the relay sibling check stays token-based for exactly that case.Linked Issue
Part of #22928
Stacked on #23006, which adds request-addressed ending for terminal streams; this PR's host part uses its registry index.
Visual Proof
Pending: live QA runs after the review loop
Testing
Dispatcher-level tests on the real runtime service (
src/main/runtime/rpc/methods/native-chat-request-unsubscribe.test.ts):endis sent, and the token and sweep paths are never called. Fails if the subscription does not register its request id.localchats.requestId(it is not strict) and the token path ends the stream as today.Phone tests: the direct and relay transports both send
requestIdequal to the subscribe's frame id, with the token kept. On relay, a live newer stream for the same chat token still suppresses the old stream's stop, for chat as well as terminal. Each of the tests above was checked to fail with the corresponding part of the change removed.Checks run locally:
pnpm tc:node, mobiletsc --noEmit,oxlinton the changed files,pnpm run check:code-quality:changed,pnpm run check:max-lines-ratchet,pnpm run verify:rpc-params-catalog, the host RPC, rpc-contract and cross-version wire tests (320 files; one orchestration downgrade test times out identically without this change), and the mobile transport tests (136 files).AI Disclosure
Review
requestIdonly selects among registrations on the caller's own connection, so a phone cannot end another socket's stream; there is no new method or permission.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
A
requestIdidentifies exactly one stream on its connection, so ending by request id is deliberately not tied to the unsubscribe method it arrives on: anativeChat.unsubscribethat names one of this connection's terminal requests ends that terminal stream, just as cancelling that request would. It only ever reaches the caller's own connection, and neither phone transport can send a mismatched pair.Merge note: land this after #22939, or in the same phone release, as with #23006. Without #22939, a relay phone that closes the newer of two streams for one chat while the older one's
endis still arriving sends no stop, because the relay sees the older stream as a live sibling. The older stream's own stop then names its already-replaced request and does nothing, so the newer stream's file watch keeps running until the connection closes. On main that stale stop happened to end it. #22939's directional sibling check sends the right stop in that case.Merge note: this composes with #22939, which makes the relay sibling check directional (it skips a stop only for a same-slot stream sent after the cancelled one) and already applies it to chat. Both PRs change
sendUnsubscribeand its call sites incancel()inmobile/src/transport/mobile-relay-rpc-streams.ts, and both touchmobile-relay-rpc-stream-cancellation.test.ts(keep both sides). Resolve the relay code by keeping #22939'shasNewerSlotOwner(unsubscribe, sendOrder)check first and building the params withwithUnsubscribeRequestId(unsubscribe, requestId)only after it. The signature becomessendUnsubscribe(unsubscribe, sendOrder, requestId)and every call incancel()passesstream.sendOrder, id. #22939's slot key is built from the method, the token and the client id, sorequestIdnever enters the comparison.Merge note: #22943 changes the same line in
mobile/src/transport/rpc-client-stream-registry.tsto pass the request id intobuildStreamUnsubscribefor session tabs. Keep both:buildStreamUnsubscribe(stream?.method, stream?.params, id)andthis.sendRpc(unsubscribe.method, withUnsubscribeRequestId(unsubscribe, id)).Opening the same chat again on one connection still replaces the earlier stream rather than running both, so this change stops a late stop from ending the newer stream but does not let two views of one chat stream side by side.
The desktop web client's chat view still ends its stream by token (its view's own token when it has one). It is outside this phone change and can move to the same path later.
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)