Skip to content

fix(tui): follow a daemon that restarted on a new port - #5318

Merged
kovtcharov-amd merged 10 commits into
amd:mainfrom
kovtcharov:claude/rel-tui-daemon-rediscover
Oct 19, 2026
Merged

kovtcharov-amd merged 10 commits into
amd:mainfrom
kovtcharov:claude/rel-tui-daemon-rediscover

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Before: after the daemon restarted on a new port, Cancel, Respond, Confirm and the full-access toggle in the TUI failed with "connection refused", which says nothing about what happened. After: the TUI finds the restarted daemon and says plainly that the restart ended the agent session and nothing was sent; the next message starts a fresh session there. A run cannot be carried over, because the daemon stops its sidecars on shutdown and the next one starts fresh. Lands after v0.25.0.

Sending a new message already re-read instance.json (through EnsureAgent) before this change. What was broken was every call made against the session the TUI already held.

Reproduced by TestCancelAfterADaemonRestartSaysTheRunEnded and TestDoDoesNotCarryARunScopedCallToARestartedDaemon: on main the first fails with "connection refused", and the second can't compile because main has no way to tell run-scoped calls apart. Both pass here.

Test plan

  • go vet ./... clean; go test -p 1 ./internal/daemon/ ./internal/client/ ./internal/ui/preflight/ passes
  • Run-scoped calls (cancel, answer, confirmation, follow-up, permission decision, full access) are never sent to a restarted daemon; the client still learns the new instance
  • Other relayed calls ensure the agent on the restarted daemon, then replay (TestDoEnsuresTheAgentBeforeReplayingARelayCallToARestartedDaemon)
  • A call whose connection drops after a daemon received it is never replayed (TestDoNeverReplaysARequestThatReachedTheDaemon)
  • A replacement that fails its checks keeps its typed VersionError/StaleError in the chain, plus the original failure
  • Live, real TUI against a fake daemon that, like the real one, has no sidecar after a restart (isolated homes): /full-access confirm after the restart said "GAIA's daemon restarted, which ended the agent session this belonged to. Nothing was sent", and the restarted daemon received no /bypass. The next message ensured the agent and was answered. Main says "actively refused it".
🔍 Technical details
  • Client.Do re-reads instance.json only on a dial failure (*net.OpError with Op == "dial", reachable through the new RequestError.Unwrap). The new instance goes through the same verify and CheckVersion as attach.
  • Request.RunScoped marks calls about state in a running sidecar. They return DaemonRestartedError together with the new instance. Other /v1/<agent>/… calls run ensureOn (split out of EnsureAgent, with its agents-floor check) on the new daemon first.
  • A restart on the same port shows up as a rotated token (401) rather than a refused connection; that path takes the same step, so it is covered too (TestDoDoesNotCarryARunScopedCallAcrossASamePortRestart).
  • SSEClient.do keeps whichever instance answered, so the six calls that used to drop it also follow a plain token rotation.
  • The client test fake answers 503 "no running sidecar" after a restart until the agent is ensured, the way the real daemon does.
  • The log names the dial's own failure (refused, timed out, unreachable).
  • No overlap with fix(lemonade): wait for GAIA's restarted model server instead of failing the turn #5264/fix(daemon): closing a TUI session no longer kills the shared model server #5282 (daemon close) or feat(tui): reopen past sessions and see which ones need you #5237 (sessions).

@github-actions github-actions Bot added the tui Go terminal UI (gaia-tui) label Oct 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve

After a daemon restart lands on a new port, the TUI's Cancel, Respond, Confirm and similar calls now find the restarted daemon instead of failing with "connection refused" until the user relaunches. The retry only fires when the connection never opened, so nothing can run twice, and a test pins that. When no replacement daemon is recorded, the original error still reaches the user unchanged. I found no blocking issues. go test -p 1 ./internal/daemon/ ./internal/client/ passes on the PR head.

Real-world evidence

N/A: no evidence bundle was produced for this run. The change is in the TUI's daemon client, which this lane doesn't drive. The verdict rests on static review plus the new Go tests, which reproduce the restart against a fake daemon. A live check would be: start a session, restart gaia daemon so it picks a new port, then press Cancel.

🔍 Technical details

Issues

  • 🟢 The "unusable replacement" branch has no test (internal/daemon/client.go, the rerr != nil path in Do). Nothing tests the case where instance.json names a new daemon that then fails verify or CheckVersion, so the combined error message isn't pinned. A test could rewrite instance.json to a closed port with a new token and assert the error keeps the original operation and adds the reason.
  • 🟢 Misleading wording when only the token changed. If instance.json has the same port but a new token, and the daemon there is still down, the error says "instance.json now names another daemon". That reads oddly when the port didn't change. Something like "instance.json was rewritten, but the daemon it names is not reachable either" would be accurate in both cases.

Strengths

  • Replay is limited to a net.OpError with Op == "dial", reached through the new RequestError.Unwrap. TestDoNeverReplaysARequestThatReachedTheDaemon uses a hijacked connection to prove a request the server already received is never resent.
  • SSEClient.do uses compare-and-swap on s.inst (if s.inst == inst), so a concurrent Send that already attached elsewhere isn't overwritten. Routing all six call sites through it also fixes the existing gap where a token refresh was thrown away (resp, _, err).
  • No silent fallback: a failed instance.json read is logged and the original dial error is returned, and a broken replacement adds its reason with %w instead of hiding the cause.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Live check passed: after a daemon restart on a new port, turning on full access in the TUI reached the restarted daemon on this branch, while the same step on main failed with "connection refused".

How it ran: the real TUI binary (one built from main, one from this branch merged with #5319) drove the installed-binary flagship over the daemon transport, with isolated HOME, GAIA_HOME, GAIA_DAEMON_HOME, GAIA_TUI_HOME and GAIA_MEMORY_DB. The daemon was a fake with its own instance.json, so nothing touched the shared daemon or Lemonade. After one turn, the daemon was stopped and started again on another port with a new token, then /full-access → /full-access confirm was typed.

  • This branch: [!] FULL ACCESS IS ON. The restarted daemon logged POST /v1/gaia/sessions/<id>/bypass {"enabled":true}.
  • main: Could not change permission mode: … Post "http://127.0.0.1:4352/…/bypass": dial tcp 127.0.0.1:4352: connectex: No connection could be made because the target machine actively refused it. The restarted daemon received nothing.

Also pushed since the review: a test for the "instance.json names a dead replacement" path, and wording that fits when only the token changed.

🔍 Technical details
  • Full access is the clearest vector because SetFullAccess reuses the cached instance between turns. A new turn re-ensures and re-reads instance.json on both builds, so it can't show the bug.
  • A restart mid-turn ends that turn's relay stream on both builds, so Cancel then has no live run. The between-turn calls (full access, memory, follow-up, permission decision, cancel of a run whose stream survives) are what this fixes.
  • Screenshots (SVG from the control API) were captured for both builds.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Review follow-up: the restart case is now modelled the way the real daemon behaves. Run-scoped calls are no longer replayed; they say the restart ended the agent session.

  • The daemon stops its sidecars and the next one never adopts them. So cancel, answer, confirmation, follow-up, permission decision and full access now fail with "GAIA's daemon restarted, which ended the agent session this belonged to. Nothing was sent", instead of a 503 from the new daemon. Other relayed calls ensure the agent first, then replay.
  • The test fake has no sidecar after a restart until the agent is ensured, as the real daemon does
  • A replacement that fails its checks keeps its VersionError/StaleError in the chain (%w)
  • The description no longer claims Send was broken: it already re-read instance.json through EnsureAgent
  • Nits: the log names the dial's own failure; tests for the dead-replacement and version-mismatch branches; no port-reuse Fatalf; the stale check.go comment is fixed
  • Live re-check, against a fake daemon with no sidecar after a restart: full access after the restart showed the restart message, and the new daemon received nothing for it; the next message was answered there.

My earlier live comment showed full access reaching the restarted daemon. That relied on the fake keeping the sidecar's state across the restart, which the real daemon never does, so that result no longer applies.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Second review round: a daemon that restarts on the same port is handled like one on a new port. It shows up as a rotated token (401), not a refused connection, and that path now takes the same step: run-scoped calls get the restart message, and relayed calls ensure the agent first. Covered by TestDoDoesNotCarryARunScopedCallAcrossASamePortRestart and TestDoEnsuresTheAgentBeforeReplayingAcrossASamePortRestart, which also remove the port-reuse flake.

ensureOn now runs the agents-floor check, and the instance it returns is the one used.

Separately, a commit by mistake added local preview entries to .claude/launch.json. A later commit restores the file, so the PR's net diff doesn't touch it.

@kovtcharov

Copy link
Copy Markdown
Contributor Author

Third review round: the same-port token-refresh path already returns DaemonRestartedError for run-scoped calls since 820beb27c (TestDoDoesNotCarryARunScopedCallAcrossASamePortRestart).

TestDoNeverReplaysARequestThatReachedTheDaemon now drops the connection with a reset (linger 0). Forcing isDialFailure to return true makes it fail, so it pins the rule. The cancelled CI re-runs on this push.

After a daemon restart on a new port, Cancel, Respond and Confirm failed with "connection refused": the TUI re-read instance.json only after a 401, and a dead port never answers 401.

- When a call cannot connect, the client re-reads instance.json once and, if it names a different daemon that passes the usual checks, sends the call there. A failed dial reached no server, so nothing can run twice; a call that reached a daemon and then lost its connection is never replayed.
- If instance.json names nothing new, the original error stands; if it names a daemon that fails its checks, both reasons are reported.
- The SSE client keeps the instance that answered, so later calls go straight to the new daemon.
…daemon

The message now says instance.json was rewritten rather than naming "another daemon", which read oddly when only the token changed.
…g it to a daemon that never had it

The real daemon stops its sidecars on shutdown and the next one starts fresh, so replaying Cancel, Respond, Confirm or a full-access toggle to the restarted daemon only got back a 503 "no running sidecar".

- Calls about a run or session (cancel, answer, confirmation, follow-up, permission decision, full access) are marked run-scoped. When the daemon behind them restarted, they are not sent; the user is told the restart ended the agent session and to send the message again.
- Other relayed calls ensure the agent on the restarted daemon before they are replayed.
- A replacement daemon that fails its checks keeps its typed error (%w), so version and stale-instance checks still recognise it.
- The log names the dial's own failure; the test fake now has no sidecar after a restart, as the real daemon does.
A daemon that comes back on the same port shows up as a rotated token instead of a refused connection. The 401 path replayed run-scoped calls there unchecked, without ensuring the agent the restarted daemon no longer had.

- The dial-failure path and the token-rotation path now share one step: a run-scoped call returns DaemonRestartedError, and a relayed call ensures its agent first. The ensured instance is the one used.
- ensureOn runs the agents-floor check that EnsureAgent ran before it, so a replacement too old for the relay is refused before the call.
- The preflight rotation fake answers the ensure call the way the real daemon does.
The dropping fake now resets the connection (linger 0) instead of closing it cleanly, the failure a crashed daemon produces. Forcing isDialFailure to true makes the test fail, so it pins the rule.
@kovtcharov-amd
kovtcharov-amd force-pushed the claude/rel-tui-daemon-rediscover branch from b7eced9 to 093147b Compare October 19, 2026 06:27
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 19, 2026
Merged via the queue into amd:main with commit 1c1546e Oct 19, 2026
47 of 49 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/rel-tui-daemon-rediscover branch October 19, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tui Go terminal UI (gaia-tui)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants