fix(desktop): resolve workspaces before declaring "not found" + stop host-service manifest steals
Links
- Plan/investigation:
plans/20260815-cli-workspace-open-race.md
Summary
- A workspace opened right after CLI creation (or any time the renderer's mirror is stale) showed "Workspace not found" / a blank screen for up to 30–50s. The route now actively resolves the ID against the hosts before judging: forced refetch, verdict only after a post-request fetch settles (capped 5s). Measured: missed-broadcast open 27s → 0.6s, dead-ID verdict 51s → 0.2s, normal open unchanged (0.2s).
- Host-service instances could silently steal each other's CLI routing manifest (last-writer-wins) or delete it on quit, rerouting every CLI write to an instance the desktop renderer isn't listening to. Booting instances now yield to a live healthy holder, and the coordinator only removes a manifest its own child wrote.
Why / Context
Reported by Roshvan (wattdata): an agent recycled a workspace via superset workspaces delete/create/open; the deep link landed on "Workspace not found" until he manually reopened it later. Root cause chain (full failure-mode matrix in the plan doc):
- The renderer's workspace mirror converges only via fire-and-forget
workspace:changedevents (no replay on reconnect) plus a 30s fallback refetch — a just-created row can trail its own deep link. isReadywaits on every known host's list; one unreachable teammate host hangs the fetch ~25s×2 (retry: 1bounds attempts, not duration), blanking every not-in-cache route for ~50s. Both of Rosh's rendered states reproduce from these two paths.- A second live host-service (DMG test-drive, canary+stable,
superset start, update-survivor) overwrote the manifest, making every CLI create broadcast-invisible to the renderer. Found live in the wild: a prod manifest pointing at a host-service run from a mounted 1.22.0 DMG a day earlier.
How It Works
useWorkspaceMissVerdict(new, co-located with the v2-workspace route): on routing to an ID the mirror lacks, fire onecache.refetchAll()and hold the loading shell; renderWorkspaceNotFoundStateonly once a fetch that started after the request settles without the row, or the 5s cap fires — never from pre-existing cache state, and independent ofisReady. Suspended while a create transaction or failed-create entry owns the ID.useHostWorkspacescache ops gainrefetchAll()/hasLiveTargets.- Host-service boot:
shouldYieldManifest— yield to a live + health-probed holder; dead or unhealthy holders forfeit. Coordinatorstop()checksmanifest.pidbeforeremoveManifest.
Manual QA Checklist
- Missed-broadcast repro (row inserted on host with no event, then routed to): renders in ~0.6s, never flashes not-found — was blank 27s (verified over CDP against the dev app; before/after captured on video)
- Genuinely nonexistent ID: "Workspace not found" in ~0.2s — was ~51s blank
- Normal create → open: unchanged (~0.2s)
- Deleting the currently-viewed workspace still lands on not-found (existing behavior)
Before/after screen recording available (27s, timer overlay): ask Kiet or see ~/Downloads/workspace-notfound-before-after.mp4 — will attach below.
Testing
bun run typecheck(desktop green)biome checkon all touched filesbun test— 53 tests across the three touched suites: verdict gate/window unit tests,shouldYieldManifestmatrix, 3 new coordinator stop-ownership tests. Each fix was mutated out to confirm its test fails.
Design Decisions
- Verdict at the route instead of hardening the event bus: every failure mode (missed event, wrong instance, stale snapshot, hanging host) is one refetch away from the shared host.db, so the single choke point where the wrong conclusion is drawn covers them all; host-side event replay would be a protocol change for the same outcome.
- Yield-to-healthy-holder instead of flock single-instancing: no Node-native flock; pid-alive + health probe reuses the exact adoption semantics the coordinator already trusts, and a wedged holder still forfeits.
Known Limitations
- Two host-services on one org dir still both serve (only the manifest is protected); cross-instance UI staleness heals via the route verdict + 30s refetch. A
superset doctorduplicate-instance check is listed as follow-up in the plan doc. - The ~5s verdict cap trades a slightly slower "not found" for never falsely declaring it; hosts that answer fast verdict in ~0.2s.
Summary by cubic
Fixes false or slow “Workspace not found,” heals gaps after event‑bus reopen, and prevents host‑service instances from stealing or deleting the CLI routing manifest. The route now verifies against live hosts before declaring a miss; the manifest is owned, yielded to a healthy holder, reclaimed safely, and only removed by its owner.
-
Renderer: adds
useWorkspaceMissVerdictto defer “not found” until a post‑mountcache.refetchAll()settles; caps at 5s, declares an immediate miss when offline with a settled mirror, waits for host enumeration to avoid remote‑host flashes, and cancels stale windows on navigation.useHostWorkspacesexposescache.refetchAll(),cache.hasLiveTargets, andhostsSettled, and invalidates a host’shost-servicequeries on any bus reopen (tracked per host URL). -
Host‑service: yields the manifest to a live, healthy holder (
shouldYieldManifestviaisProcessAlive+pollHealthCheck), reclaims every 15s if the holder goes away, and clears the reclaim timer on shutdown. Coordinator removes a manifest only when held by its own child (on stop, cancelled or failed start, and child exit). Manifest writes are atomic (write‑then‑rename), and unreadable manifests are left untouched. -
Rollout: “not found” may delay up to 5s on slow hosts; verify routing in multi‑host orgs and when quitting one desktop while another runs. No migrations or config changes.
Summary by CodeRabbit
-
Bug Fixes
- Improved workspace opening after CLI-created workspaces, reducing stale, delayed, or incorrect workspace screens.
- Workspace pages now verify that a workspace is genuinely unavailable before showing a not-found state.
- Improved refresh handling when workspace data is delayed or temporarily unavailable.
- Prevented active desktop instances from overwriting each other’s host-service ownership information.
- Preserved valid host-service registrations during shutdown while removing stale entries.
- Improved recovery after host reconnection and refreshed workspace data more reliably.
-
Documentation
- Added an implementation plan documenting workspace-opening race conditions, safeguards, and verification results.