Skip to content

Establish one daemon-readiness contract to stop per-call-site startup races - #1552

Merged
malibio merged 4 commits into
mainfrom
worktree-issue-1525-daemon-readiness-contract
Jul 6, 2026
Merged

malibio merged 4 commits into
mainfrom
worktree-issue-1525-daemon-readiness-contract

Conversation

@malibio

@malibio malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Closes #1525

malibio and others added 3 commits July 6, 2026 20:48
…aemon (closes #1525)

daemon-status.ts consumed only the pushed daemon-status Tauri event, which
raced the webview's own listener registration and never re-fired after
startup — an unattended daemon recovery had no signal path, and the Retry
button bypassed the shared store entirely with its own ad-hoc invoke() call.
Redesign it around a transport-agnostic status source that pulls the current
status on subscribe (closing the lost-first-event race) in addition to
consuming pushed events, and give Retry a real path through the shared
contract via the new refreshDaemonStatus().

Add integration coverage that was previously entirely absent: a Rust test
that spawns a real headless nodespaced and proves the readiness contract
(check_daemon_socket/wait_for_daemon and the real check_daemon_status command
body) against it, and a TypeScript e2e test that proves a daemon-dependent
store genuinely fails while the daemon is down and self-heals with real data
once it's reachable — using the existing DaemonTestHarness, extended with a
startDeferred() variant that doesn't block on daemon readiness.

Co-Authored-By: Claude <[email protected]>
The 5s poll window for the reconnect-triggered load to land was too tight
under contention (observed failing once under the pre-push hook, right
after a fresh cargo build left the machine busy) — widen it to 20s and
surface a real diagnostic (a direct getAllSchemas() call) if it still times
out, instead of a bare "expected 0 to be > 0".

Co-Authored-By: Claude <[email protected]>
Debugging an intermittent failure under pre-push load surfaced a second
readiness layer distinct from the daemon's socket: DaemonTestHarness.
startDeferred() starts the dev-proxy before the daemon binds its socket, and
the proxy's gRPC-js client — constructed once at proxy startup against a
not-yet-existing socket — owns its own reconnect/backoff state independent
of the daemon's actual socket becoming reachable moments later. A store
reload triggered right as the socket became reachable could fail silently
against a proxy channel still mid-backoff.

Add waitUntilProxyReady(), which polls a real data-plane round-trip (HTTP ->
dev-proxy -> gRPC -> daemon) instead of just the socket, and use it in the
readiness e2e test. Verified separately (scratch Rust test, not committed)
that production's tonic lazy channel does NOT have this gap — a call issued
right after the daemon binds succeeds immediately, no cached backoff state.
This is specific to the gRPC-js dev-proxy path used only by this test
harness. Follow-up issue to be filed for that gap on its own.

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

Net positive, well above the bar for this repo. This closes a real, previously-unrecoverable startup race (lost first daemon-status event with no re-fire path) with a clean transport-agnostic DaemonStatusSource abstraction, and — unusually for infra work — backs it with integration coverage against a real spawned daemon rather than mocks on both the Rust and TypeScript sides. The e2e work also caught and fixed a genuine second-order bug in the test harness itself (gRPC-js proxy channel backoff) rather than papering over the flake, which is exactly the right instinct. All three acceptance criteria in #1525 are met: single readiness contract shared by check_daemon_socket/wait_for_daemon and the Tauri command body, an integration test covering not-ready → recovered, and clean lint/test runs (confirmed: bun run test 146/146 files, 3943/3943 tests passing; cargo clippy --lib --tests -p nodespace-app clean; cargo test -p nodespace-app --test daemon_readiness_test --no-run compiles).

Nothing here blocks merge. Two findings worth a look, one is genuinely dead code.

Findings

Suggested Improvements

  • packages/desktop-app/src-tauri/tests/daemon_readiness_test.rs:92-113, :118-136: the NODESPACED_SOCKET env-var save/restore in check_daemon_status_command_agrees_with_the_real_daemon and check_daemon_status_command_reports_not_running_for_an_unbound_socket is not panic-safe — the restore runs as plain code after the assertions, not in a Drop guard or catch_unwind. If either assertion fails (which is exactly the scenario a broken build would produce), the env var leaks unrestored, and since integration-test binaries run all tests in one process by default, a later test in this file (or a future one added to it) could silently read the leaked value. This mirrors the pre-existing pattern in grpc_client.rs's resolve_socket_path_honors_env_override_then_falls_back test, so it's consistent with house convention rather than a new regression — but the new file adds an explicit tokio::sync::Mutex specifically because integration-test binaries parallelize by default (correct reasoning), which makes the leak-on-panic gap more likely to actually bite here than in the single-test unit-test module it's modeled on. A struct EnvGuard { prev: Option<OsString> } with a Drop impl that restores would close this for a few extra lines and could be reused by both tests.

  • packages/desktop-app/src/tests/e2e/daemon-readiness.e2e.ts: the final commit (c0e9f4a3) reverted the prior commit's (9a8f272d) widening of the schemas-landed poll window from 5s back down, and dropped its diagnostic error message (direct getAllSchemas() call surfaced on timeout), reasoning that waitUntilProxyReady() now proves the round-trip works before the poll starts. That's a defensible call given the root cause (proxy backoff) is now waited-out up front, but the commit message for c0e9f4a3 doesn't mention that it's reverting 9a8f272d's hardening — someone reading 9a8f272d in isolation later (git log/git blame) will believe the 20s window + diagnostic is still the current state. Not a functional issue, just a paper trail nit; worth a one-line mention in commit history or PR description if you want future-you to not have to re-discover this by diffing commits.

Nitpicks

  • Nit: packages/desktop-app/src/tests/e2e/daemon-harness.ts:394 (waitUntilDaemonReady) is dead code — its only call site in the e2e test was replaced by waitUntilProxyReady() in the last commit (per the module doc comment in daemon-readiness.e2e.ts:20-21, which explains why the socket-only check is insufficient, but the method itself was left behind rather than removed). isDaemonReachable() is not redundant — it's still live, used by harnessStatusSource()'s getCurrent() — so no action needed there.

  • Nit: the daemon_status_body factoring in lib.rs (extracting the #[tauri::command] body into a pub async fn to dodge the E0255 hidden-item collision) is sound and verified — cargo clippy --lib --tests -p nodespace-app and a standalone cargo test -p nodespace-app --test daemon_readiness_test --no-run both compile clean, and the one-line check_daemon_status wrapper means the annotated command and the tested body can't drift. Nothing to fix, just confirming the approach holds up since it's easy to get this kind of macro-collision workaround subtly wrong.

  • Nit: packages/desktop-app/src/lib/services/daemon-status.ts's startDaemonStatusListener fires both getCurrent() (pull) and subscribe() (push) concurrently with no defined ordering between them — if a push event and the initial pull resolve out of order, the later one to settle wins regardless of which observed the more recent state. In practice this is a narrow window (both are near-instant on startup) and applyStatus is idempotent per payload, so this isn't worth blocking on, just flagging as a place a future higher-frequency push source could introduce a stale-overwrite.

Process note (not a blocker for this PR)

bun run test:all does not execute either new test: rust:test runs cargo test --lib -p nodespace-core (unit tests in the core lib crate only, not nodespace-app's tests/ integration tests), and test:e2e isn't part of test:all at all. Combined with the repo having no PR-gating CI workflow yet (only release.yml exists), both the new Rust integration test and the new TS e2e test currently only run when someone remembers to invoke them manually — which is a little ironic given this PR's own commit history shows the e2e test flaked once already under real-world load. This is a pre-existing gap in test:all's scope rather than something #1525 was scoped to fix, but since it directly affects whether this PR's new coverage actually protects anything going forward, it's worth a fast follow-up issue if one doesn't already exist.

- Add a Drop-based EnvGuard for the Rust integration test's NODESPACED_SOCKET
  mutation, so a panicking assertion can't leak the mutated env var to
  whichever test acquires ENV_MUTEX next (the manual save/restore only ran
  on the non-panicking path).
- Remove daemon-harness.ts's waitUntilDaemonReady(), left as dead code after
  its only call site was replaced by waitUntilProxyReady() (which waits on
  the real data-plane round-trip, not just the daemon's socket).

Co-Authored-By: Claude <[email protected]>
@malibio

malibio commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed both fixable review findings in 6d6a615:

  • Added a Drop-based EnvGuard (in tests/support/mod.rs) so a panicking assertion in the Rust integration test can't leak the mutated NODESPACED_SOCKET env var to a later test.
  • Removed daemon-harness.ts's dead waitUntilDaemonReady(), left behind after its only call site was replaced by waitUntilProxyReady().

Not addressed (both explicitly called non-blocking, and out of this issue's scope):

Full test suite + pre-push gate re-verified green after these changes.

@malibio
malibio merged commit 02da79f into main Jul 6, 2026
@malibio
malibio deleted the worktree-issue-1525-daemon-readiness-contract branch July 6, 2026 20:12
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.

Establish one daemon-readiness contract to stop per-call-site startup races

1 participant