fix(pty-daemon): match terminal list replies to their requests
What & why
After a list request to the PTY daemon times out, its eventual reply can complete the next list request with an outdated session snapshot. Overlapping requests also accept the first reply of the expected type.
The daemon advertises support for list request IDs in its existing handshake and echoes the ID on each reply. The host waits for the reply to its own request. Clients and daemons that use the legacy message shape can still list sessions during a rolling upgrade.
Refs #6376, specifically the late list reply described in the triage. The desktop close guard is tracked separately in #9810.
How I tested it
- Reproduced on main with a Unix-socket fixture. The test expires the first request using Node's mock clock, starts a second request, then delivers the two replies in order. Before the fix, the second request returns the first reply's empty list. After the fix, it returns the active session from its own reply.
- Node DaemonClient suite: 15 passed when run on its own. An earlier concurrent run missed expected PTY output in two existing tests.
- Node daemon control-plane suite: 33 passed. A real test daemon advertises the capability, echoes the tagged request, and answers a legacy untagged request.
bun run lint:fix, cleanbun run lint, andbun run typecheckpassed. Typecheck completed 42/42 tasks.TERM=xterm-256color bun run test --continue=alwayscompleted 26/32 tasks. All 190 PTY daemon tests and 9,045 desktop tests passed. The six failing tasks cover the shared date format, agent shell fallback, missingflockin the sandbox suite, and timeouts in API, i18n and host worker/git tests.
Codex assisted with implementation and automated verification.
Isolated reruns of the API URL, i18n source scan, host worker phase, and Git batch checks all passed.
Regression test follow-up
The test advances the list timeout after an output marker confirms the replies were read. A runner deadline cancels pending fixture waits if the marker is missing. The connection settles before disposal, and the list result and its fixture exchange are awaited together.
The final DaemonClient suite passed all 15 tests. Four separate fault injections verified bounded cleanup when the connection is delayed, the second request disconnects, the marker is missing, or the second reply is missing. The missing-list-reply case fails on the mock clock. Clean lint and scoped host-service and daemon typecheck passed on the final head.
The fixture now uses a unique socket directory and awaits the server’s listening event. An occupied-socket run failed promptly with EADDRINUSE and reported no uncaught exception or unhandled rejection. All 15 DaemonClient tests, clean lint, and scoped host-service typecheck passed.
Checklist
- PR title follows conventional commits (
type(scope): subject) -
bun run lintandbun run typecheckpass (CI fails on lint warnings too) - "Allow edits from maintainers" is checked on fork PRs
- I have signed the CLA (the bot asks on your first PR)
Summary by cubic
Fixes a race in the PTY daemon where a timed-out list request's late reply could settle the next list request with an outdated session snapshot.
- Daemons now advertise
supportsListRequestIdsin the handshake and echo the request ID on each list reply; hosts tag list requests with an incrementing ID and accept only the matching reply. - Legacy clients and daemons that omit the ID still work during a rolling upgrade.
- Adds regression tests for delayed and out-of-order list replies, replies from daemons without request IDs, and hardens the test fixtures against bind failures, missing markers, and stalled cleanup.
Refs #6376.
Summary by CodeRabbit
- Bug Fixes
- Fixed an issue where a delayed response to a timed-out terminal session list request could interfere with a later request. Later requests now receive the correct session results.
- Terminal session list responses are matched to their corresponding requests when supported, while compatibility with daemons that omit request identifiers is preserved.
- Daemons advertise support for request identifiers so clients can reliably correlate list responses.
- Tests
- Added coverage for delayed responses and for daemons that include or omit request identifiers.