Skip to content

fix(runtime): let the phone end a native chat stream by the request that opened it - #23058

Closed
brennanb2025 wants to merge 2 commits into
brennanb2025/terminal-unsubscribe-by-requestfrom
brennanb2025/native-chat-unsubscribe-by-request
Closed

brennanb2025 wants to merge 2 commits into
brennanb2025/terminal-unsubscribe-by-requestfrom
brennanb2025/native-chat-unsubscribe-by-request

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​233 $\color{#cf222e}{\Huge{\mathbf{−}}}$​18 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​215
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​41 $\color{#cf222e}{\Huge{\mathbf{−}}}$​17 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​24

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.unsubscribe named 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 requestId to nativeChat.unsubscribe, set to the id of the nativeChat.subscribe request 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:

  • The chat subscription already registers with the desktop's stream registry when the request arrives, before any waiting. It now also passes its request id, so the registry's (connection id, request id) index can find it from that moment, including while it is still locating the transcript file and after the request handler has returned.
  • nativeChat.unsubscribe with requestId calls 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.
  • Without 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.
  • On the phone, both the direct and the relay transports add requestId equal 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.
  • On relay, the check that skips a stop when a newer stream for the same chat is still open keeps comparing only the chat token. requestId is 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:

  • Giving each chat view a unique token on the phone. It would also protect against desktops without this change, but a desktop from before client-supplied chat tokens (Polish desktop native chat view #6641) keys the file watch by agent and session, so a stop naming a per-view token would miss it and the watch would run until the connection closes. It also leaves chat on a different ending mechanism from terminals.
  • A desktop-assigned id sent back in a new early frame. It goes stale when the phone replays its streams after a reconnect, and a new frame needs capability negotiation with older phones.
  • A host capability telling the phone whether requestId is 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

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Dispatcher-level tests on the real runtime service (src/main/runtime/rpc/methods/native-chat-request-unsubscribe.test.ts):

  • The phone opens a new stream for the same chat token on the same connection, then the old request's stop arrives: the new stream keeps running and its file watch stays open. Fails without this change, where the token stop ends the new stream.
  • A stream is ended by its own request id both while its transcript lookup is still pending and after the request handler has returned; the file watch is closed, one end is sent, and the token and sweep paths are never called. Fails if the subscription does not register its request id.
  • An unknown request id, the stop message's own id, and the id of a stream that registers without a request address are all no-ops that leave the live chat running and never touch the token or sweep paths.
  • A socket without a connection id: no-op; in particular the stop does not sweep the shared local chats.
  • A desktop without this change: its schema drops requestId (it is not strict) and the token path ends the stream as today.

Phone tests: the direct and relay transports both send requestId equal 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, mobile tsc --noEmit, oxlint on 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

  • Security: requestId only 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.
  • Cross-platform: TypeScript on the host and phone only, with no platform-specific behavior.
  • Remote SSH: unaffected; this is the phone-to-desktop runtime channel, and the registry lives on the desktop that watches the transcript.
  • Mobile: both phone transports send the new field; the relay's same-chat sibling check still compares the token only.
  • Backwards compatibility: new phone with old desktop drops the field and uses the token as today; old phone with new desktop uses the unchanged token and sweep paths; no new frame, method or capability.
  • Performance: one map entry per live chat subscription on a connection, removed with it; one lookup per stop.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

A requestId identifies exactly one stream on its connection, so ending by request id is deliberately not tied to the unsubscribe method it arrives on: a nativeChat.unsubscribe that 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 end is 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 sendUnsubscribe and its call sites in cancel() in mobile/src/transport/mobile-relay-rpc-streams.ts, and both touch mobile-relay-rpc-stream-cancellation.test.ts (keep both sides). Resolve the relay code by keeping #22939's hasNewerSlotOwner(unsubscribe, sendOrder) check first and building the params with withUnsubscribeRequestId(unsubscribe, requestId) only after it. The signature becomes sendUnsubscribe(unsubscribe, sendOrder, requestId) and every call in cancel() passes stream.sendOrder, id. #22939's slot key is built from the method, the token and the client id, so requestId never enters the comparison.

Merge note: #22943 changes the same line in mobile/src/transport/rpc-client-stream-registry.ts to pass the request id into buildStreamUnsubscribe for session tabs. Keep both: buildStreamUnsubscribe(stream?.method, stream?.params, id) and this.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

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

  • Host request-addressed teardown: nativeChat.subscribe now registers its ctx.requestId with the stream registry, and nativeChat.unsubscribe carrying requestId releases exactly that registration via releaseSubscriptionByRequest — never falling back to the token or prefix sweep. The no-requestId path is unchanged compat code for older phones.
  • Wire contract: optional requestId added to NativeChatUnsubscribe. The schema is non-strict, so an older host drops the field and keeps using the token.
  • Phone transports: new withUnsubscribeRequestId gates the field to terminal.unsubscribe / nativeChat.unsubscribe; both the direct registry (dispose) and the relay (sendUnsubscribe) call it, with the relay's token-only hasLiveOwner sibling 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.

Pullfrog  | View workflow run | Using 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.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

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 subscriptionId token. The desktop has keyed chat feeds by that client token since the first release that had phone chat, so no host change is needed, and every supported desktop gets exact addressing.

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 requestId plumbing for chat redundant. Terminal streams keep the request-addressed path from #23006, because their key is derived by the host.

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