Repository navigation
fix(desktop): reconcile Connect policy across runtime generations - #4052
Conversation
The organization Connect policy was delivered to the local runtime through a fixed retry window (20 attempts x 3s) keyed only on the desired boolean. If the runtime became ready after that window, or restarted after a successful delivery, the desired policy stayed known to the app while the runtime state remained missing or stale until an unrelated event or a manual reload. Treat the policy as desired state and reconcile it per target generation: - The desktop runtime now reports a monotonic per-start generation for the embedded server, since sticky ports and persisted tokens make restarts otherwise unobservable to clients. - A generation-aware reconciler records the last successfully applied tuple (target key, desired switch, organization revision) and re-reconciles on desired-state changes, runtime publish events, and an hourly level check. - Stale attempts are invalidated by epoch and serialized behind a request lock so an obsolete workspace, organization, or generation attempt can neither record success nor land after a newer application. - Transient failures back off on a bounded schedule and park in a sanitized pending state that the next runtime observation re-arms; both enable and disable directions converge and unrelated local use is never blocked.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Test evidence — connect-policy-converges-on-a-slow-starting-runtime-and-is-reapplied-to-the-next — 0/0 screenshots passed · 5 assertions✅ 0/0 screenshots passed · 5 assertions · 5 expectations passed · 0 failed · 0 pending
ℹ️ ASSERTION — 1. The organization Connect policy converged on a slow-starting runtimeAlthough the local server delayed its readiness past the policy arrival, the state endpoint reported status=available and connectEnabled=true.
ℹ️ ASSERTION — 2. The persisted Connect state was cleared before the restartconnect-state.json reports missing, so any post-restart Connect state must come from a fresh policy application, not persistence.
ℹ️ ASSERTION — 3. The restart produced a new runtime generationThe restarted local server reported per-start generation 3 where the previous lifetime reported 2.
ℹ️ ASSERTION — 4. The unchanged policy was reapplied to the new runtime generationWith persistence cleared, the restarted server reached status=available and connectEnabled=true again — the desired organization policy was re-delivered to the new generation.
ℹ️ ASSERTION — 5. The reapplied policy persisted to the profileconnect-state.json reports connectEnabled=true again after the reapplication.
Test run created 2026-08-24T23:26:39.379Z · Source: |
There was a problem hiding this comment.
Warden security clearance: clear. No blocking findings (security or desktop↔den sync) in this diff (429f739bdd0fb9918419b01bbc9a932dc54b11b2). Automated clearance satisfies the required-review gate only — a human still reviews and merges. Analysis run
Problem
Organization desktop policy can require OpenWork Connect to be enabled or disabled in the local workspace runtime, and the policy routinely arrives before that runtime is ready. Delivery used a fixed retry window (20 attempts × 3 s, keyed only on the desired boolean): if the local server became ready after ~60 s, or restarted after a successful delivery, the app still "knew" the desired policy but stopped converging the runtime. Nothing reapplied the policy until an unrelated event changed the boolean or the user restarted/reloaded. This is a source-confirmed convergence gap, not a confirmed production incident.
A second gap made restarts invisible even in principle: the embedded server keeps a sticky port and persisted tokens across restarts, so a new server lifetime published a byte-identical connection and clients had no way to observe that their previously delivered state was gone.
Root cause
A bounded retry loop is a delivery mechanism, not a desired-state reconciler. It fires on one edge (the boolean changing), forgets which runtime it delivered to, and gives up permanently after its window. Policy is desired state over a moving target: reconciliation must trigger when either the desired policy or the target runtime generation changes, and success is only meaningful relative to the generation it was applied to.
Invariant
For every active workspace runtime generation, the actual Connect state eventually equals the latest desired organization policy — whether policy precedes readiness, readiness is slow, the runtime restarts, the workspace or organization switches, or the desired value flips in either direction.
Solution
apps/desktop/electron/runtime.mjs,packages/types/src/desktop-ipc.ts): the desktop runtime now assigns a monotonic per-startgenerationto the embedded server and reports it throughopenworkServerInfo, making each server lifetime observable despite sticky ports and tokens.connect-policy-reconciler.ts): one normalized desired tuple (connectEnabled+ organization revision), one target identity (resolved base URL + host token + runtime generation), and a record of the last successfully applied tuple(target key, desired value, revision). Reconciliation runs when the desired state changes, when a runtime publish event (openwork-server-settings-changed) fires — every boot/restart/workspace-reconnect path already dispatches it — and on the existing hourly config tick as a level-based safety net.pending/stalledstate (enum reasons only — no URLs, tokens, or error text) exposed on the desktop-config store; the next observation re-arms it. There is no free-running timer loop, and an already-converged target costs one connection resolution and zero requests per observation.deliverConnectStateand its constants are removed.Preservation
connect-state.json, unchanged in this PR).Proof
Lane: local (Daytona credentials unavailable on this machine — Infisical CLI not installed). All commands ran on the exact head
429f739bdafter rebasing ontoorigin/devc7e378169.bun test tests/connect-policy-reconciler.test.ts/vitest … connect-policy-runtime-restart.e2e.test.tsconnect-state-provenanceconnect-state.jsoncleared and proves reapplication on a new generationbun test tests/connect-policy-reconciler.test.ts{state:"stalled",reason:"apply_failed"})apps/serverconnect-state tests + typecheckbun test src/connect-state.test.ts src/connect-state.inspect.test.ts,tsc --noEmitCommands and results (exact head):
pnpm --dir apps/app typecheck— Passedapps/app: bun test tests/connect-policy-reconciler.test.ts tests/den-desktop-config.test.ts— Passed (22 pass, 0 fail)apps/desktop: node --test electron/runtime.test.mjs— Passed (23 pass, 0 fail)apps/desktop: pnpm typecheck:electron— Passedapps/server: tsc --noEmit+ connect-state tests — PassedOPENWORK_EVAL_E2E_TESTS=1 pnpm --dir evals exec vitest run --config vitest.config.ts --project e2e specs/connect-policy-runtime-restart.e2e.test.ts— Passed… specs/connect-state-provenance.e2e.test.ts— Passedapps/app: bun test --isolate tests/— 882 pass / 7 fail; the same 7 failures reproduce identically on the untouched base commit5c74c2530(verified in a clean scratch worktree), so they are pre-existing and unrelated… specs/first-signin-fresh-profile.e2e.test.ts— Failed on this head and identically on the untouched base5c74c2530(fault-proxy never observes a faulted/api/den/v1/me/orgsrequest; the Connect and organization convergence assertions inside it pass before that witness). Pre-existing red, reported for visibility.Negative proof
connect-state.jsonbefore restarting, so post-restart convergence cannot be explained by persistence — only by reapplication to the observed new generation.Risk and rollback
generationfield is additive toOpenworkServerInfo; bridges that predate it yieldnulland the target identity degrades to base URL + host token (the previous best available signal). No wire or persistence format changes.runtime.mjs, covered by the existing runtime tests.Scope exclusions
This PR does not redesign Desktop enrollment, cross-server handoff, deployment, or release behavior, and does not change the server-side Connect state endpoint or its persistence.