Skip to content

fix(daemon-client): clear remote URL and token on timeout once response starts or /health answers - #5044

Merged
thymikee merged 1 commit into
callstack:mainfrom
imranbarbhuiya:fix/remote-open-runner-timeout
Nov 26, 2026
Merged

thymikee merged 1 commit into
callstack:mainfrom
imranbarbhuiya:fix/remote-open-runner-timeout

Conversation

@imranbarbhuiya

@imranbarbhuiya imranbarbhuiya commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #5004.

Every remote timeout blamed the URL and auth token, even when the daemon was only slow, such as a first iOS open starting the XCTest runner past the 90 s window.

When a sent remote request times out:

  • If a 2xx NDJSON progress stream already opened on /rpc, that proves URL and token with no extra request (details.remoteResponseStarted: true).
  • Otherwise (non-streaming callers such as --json, MCP, and the SDK, or a stream with no progress yet), the client probes /health once (1 s cap) requiring a 2xx with a contract daemon or proxy service (details.remoteHealthAnswered). /health checks no token, but /rpc rejects a wrong one immediately, so a timed-out request passed it.

With either proof, the hint clears the URL and token. For open, it notes the daemon canceled the open (directly and through agent-device-proxy after #5058), so retrying is safe; Apple platforms also name runner startup and prepare ios-runner. Other commands point to remote host logs. No proof or an unsent request keeps the old hint.

Before / after

open com.apple.Preferences --platform ios against a remote daemon emitting Starting XCTest runner... until canceled:

# base 9f7751eeb (direct and through agent-device-proxy)
Starting XCTest runner...
Next: Run snapshot to check the screen before you retry. Verify the remote daemon URL, auth token, and remote host logs.

# head 547b40627 (direct and through agent-device-proxy; no post-timeout /health; daemon cancels at 90 s)
Starting XCTest runner...
Next: Run snapshot to check the screen before you retry. The remote daemon already started answering this request, so its URL and auth token are not the cause. A first Apple open can outlast the request window while the host builds and starts the XCTest runner. The daemon canceled this open, so running it again is safe even if the app launched. For a longer startup window, run agent-device prepare ios-runner with the same --platform first.

# head 547b40627 (--json, non-streaming; single post-timeout /health)
"hint": "The remote daemon still answers /health, so its URL and auth token are not the cause. A first Apple open can outlast the request window while the host builds and starts the XCTest runner. The daemon canceled this open, so running it again is safe even if the app launched. For a longer startup window, run agent-device prepare ios-runner with the same --platform first."

Validation

Head 547b40627 (rebased onto 9f7751eeb).

  • check:affected --base upstream/main --run --head 547b40627: format, lint, typecheck, layering, fallow, build, and vitest-related pass; check:daemon-wire-compat (0 changed), check:doc-links, and check:doc-prose pass.
  • vitest run --project unit-core on packages/daemon-client/src/: 416 passed; 19/19 mutants killed.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the fix. The code at 0ed6ea4 is ready.

Not blocking, and fine to take or leave: the /health probe adds 17 lines to daemon-client-transport.ts, which is why lint fails (see below); the JSDoc at https://github.com/callstack/agent-device/blob/0ed6ea4/packages/daemon-client/src/daemon-client-transport.ts#L431 says a wrong token is refused at once, but /health does not check the token and only /rpc refuses it, so the comment and PR body could say that; no test pins the 1 s bound on the post-timeout /health, so one row where the stand-in holds the second connection open and asserts remoteHealthAnswered is false in under about 2 s would cover it; the remote hint in https://github.com/callstack/agent-device/blob/0ed6ea4/packages/daemon-client/src/daemon-client-timeout.ts#L236 repeats the canceled/prepare wording of the local tail and gives snapshot no prepare advice, which suits a follow-up issue; and answersRemoteHealth accepts any 2xx, so it could also require the daemon service field when set.

The "Lint, Typecheck & Package" check fails in oxlint with "File has too many lines (1004)" on daemon-client-transport.ts, and the PR causes it: the file is 987 lines on main and 1004 once this change lands, against a limit of 1000. No conflicts. Before merge, move the post-timeout /health probe out of that file (for example next to the timeout and liveness code) without a lint exemption, then re-run the cancelled Body rules checks.

Not verified here: a live run against a real remote daemon or tunnel, cancel-on-disconnect through a reverse proxy or gateway (it was traced only on the direct daemon HTTP server), the claim that the client has no typed runner-startup signal, and the vitest suites locally (CI ran them).

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@imranbarbhuiya
imranbarbhuiya force-pushed the fix/remote-open-runner-timeout branch from 0ed6ea4 to 5d77193 Compare November 23, 2026 19:50
@imranbarbhuiya

Copy link
Copy Markdown
Contributor Author
  • Done in 5d77193 (after rebasing onto ecb2089): the post-timeout /health decision now lives in daemon-client-timeout.ts beside the liveness probe. handleRequestTimeout gets a readRemoteHealth reader from the transport and decides whether to ask. daemon-client-transport.ts is 994 lines with no lint exemption.
  • Done in 5d77193: the JSDoc now says /health checks no token, and that /rpc refuses a wrong token at once, so a request that timed out had already passed that check. The PR body says the same.
  • Done in 5d77193: a new route test holds the post-timeout /health connection open, then asserts remoteHealthAnswered: false and that the timeout returns in under 2 s. Raising the probe bound to 3 s fails it.
  • Done in 5d77193: a 2xx counts only when the payload names no service, or a service from the fix(daemon-client): flag proxy links explicitly and sanitize /health labels in the mismatch message #5032 allow-list (isHealthService). A /health success that names another service keeps the URL and token hint. The parser marks such a payload with the client-local serviceUnrecognized, which the wire ledger acks as additive. New route rows cover the daemon service and a foreign service.
  • Partly done in 5d77193: the prepare ios-runner step is now one shared phrase, used by both the local snapshot tail and the remote open hint. The cancelled wording stays separate: unifying it would change the local hint's tested text or push the remote open hint past the 400-character detail cap. Prepare advice for a remote snapshot timeout is left out of this PR.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update.

The earlier review at 0ed6ea4 found no code problems, and 5d77193 keeps it that way. The file-length lint failure from 0ed6ea4 is fixed: the lint job now passes at 994 lines.

CI is green. The only check not passing is Body rules, which was cancelled (not failed) when the rebase and force-push replaced the run. It exercises no code route. There are no conflicts. Re-running that check is the only step left before merge.

Not verified here: a live run against a real remote daemon or tunnel, and cancel-on-disconnect through a reverse proxy or gateway (the earlier trace covered the direct daemon HTTP server only). A local vitest re-run was also not done, since CI ran the suites at this head. The claim that raising the probe bound to 3 s fails the bound test was reasoned from the code, not run.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 23, 2026
@imranbarbhuiya

Copy link
Copy Markdown
Contributor Author

Cancel-on-disconnect, checked with the real daemon HTTP server and the real agent-device-proxy in front of it, the daemon's open blocking until its request is cancelled:

  • Direct: the open is cancelled when the client gives up (90.59 s).
  • Through the proxy: it is not; the open was still running 15 s after the CLI exited. Through the client transport, 2 s and 5 s windows were cancelled and 10 s and 30 s windows were not.

So in 40015cc, when /health answers as agent-device-proxy, the hint no longer says a retry is safe and says the open may still be running on the daemon. Direct connections keep the old wording. The proxy not forwarding the disconnect is outside this PR and affects every proxied command.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 24, 2026
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. The code at 40015cc looks good, and the earlier review of 5d77193 still holds with no new findings. The only check not passing is Body rules, which was cancelled by a newer push rather than failed, and it covers the PR title and body, not packages/daemon-client. Please re-run it before merge. Not verified here: the proxy cancel-on-disconnect measurement was only cross-read against daemon-proxy.ts, the new route test row was judged from the code without a mutation run, and whether a third-party reverse proxy in front of a bare daemon forwards the disconnect. The base did not retry on any route, so that last case is not a regression. No conflicts.

@thymikee thymikee added ready-for-human Valid work that needs human implementation, judgment, or maintainer merge and removed ready-for-human Valid work that needs human implementation, judgment, or maintainer merge labels Nov 24, 2026
@thymikee

Copy link
Copy Markdown
Member

This branch now conflicts with main. Please rebase onto current main; the next review only checks that the rebase keeps the reviewed change intact.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the reproduction. Two changes would make this smaller now that #5058 is merged:

  • Drop viaProxy and the proxy branch of the hint. fix(proxy): keep forwarded requests reachable so a client disconnect reaches the daemon #5058 makes the proxy forward the client disconnect, so "behind a proxy the timed-out open may still be running" no longer holds.
  • Check whether "the response started before the timeout" can replace the /health probe. A streamed CLI request has already received 2xx headers and progress events from the remote daemon, which proves the URL and token. If it can, drop the probe, serviceUnrecognized and the wire-ledger edits; keep the probe only for remote callers that do not stream progress, if any of them need this hint.

The branch also needs a rebase onto current main.

@imranbarbhuiya
imranbarbhuiya force-pushed the fix/remote-open-runner-timeout branch from 40015cc to 547b406 Compare November 26, 2026 14:38
@imranbarbhuiya imranbarbhuiya changed the title fix(daemon-client): stop blaming the URL and token when a timed-out remote daemon still answers fix(daemon-client): clear remote URL and token on timeout once response starts or /health answers Nov 26, 2026
@imranbarbhuiya

Copy link
Copy Markdown
Contributor Author

Rebased onto 9f7751eeb and updated at 547b40627:

  • Dropped viaProxy and the proxy branch of the hint. Re-verified against the real daemon HTTP server behind agent-device-proxy with fix(proxy): keep forwarded requests reachable so a client disconnect reaches the daemon #5058 in: both direct and proxied open are now canceled by the daemon at 90 s when the client disconnects.
  • Streamed response signal vs /health probe:
    • sendHttpRequest now tracks when /rpc opens a 2xx application/x-ndjson progress stream (details.remoteResponseStarted: true). When that has happened before the timeout, the client skips /health completely (zero extra connections) and tells the caller the daemon had already started answering this request.
    • Checking every remote caller showed three callers that do not set meta.requestProgress (agent-device ... --json in src/cli.ts, MCP in src/mcp/command-tools.ts, and @agent-device/client in packages/client/src/index.ts), plus streamed requests where no progress event fired before the timeout (http-server.ts flushes NDJSON headers on the first writeProgressEnvelope, and warm .xctestrun runner launches skip Starting XCTest runner... when buildMs === 0). All of those still surface error.hint.
    • So the 1 s /health probe stays only as a fallback when no progress stream started (remoteResponseStarted: false). Requiring health.service !== undefined (which readHealthLink already filters through isHealthService) drops serviceUnrecognized, leaves RemoteDaemonHealth and readHealthPayload untouched, and removes all test/wire-compat/ledger.json edits (check:daemon-wire-compat: 0 declarations changed).
  • timeoutRequestContext is inlined into its one caller so daemon-client-transport.ts stays under the 1,000-line cap with the new fields.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the rework. No code issues remain at 547b406: the conflict from the earlier review is resolved, the proxy branch and serviceUnrecognized are gone along with the wire-ledger edits, and a request whose progress stream has started now skips the /health probe, which remains only for the other sent remote requests.

Not verified here: no local tests or mutation runs were done, the live measurement of direct and proxied open cancellation was not reproduced, and it was not checked whether a third-party reverse proxy in front of a bare daemon forwards the disconnect.

The only check not passing is Body rules, which was cancelled by a newer push and checks the PR title and body, so it is likely unrelated to this code.

It needs a re-run before merge.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 26, 2026
@thymikee
thymikee merged commit a66bfdb into callstack:main Nov 26, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A first remote open that starts the iOS runner times out with a URL and token hint

2 participants