Skip to content

fix(desktop): reconcile Connect policy across runtime generations - #4052

Merged
reachjalil merged 1 commit into
devfrom
fix/connect-policy-runtime-convergence
Aug 24, 2026
Merged

reachjalil merged 1 commit into
devfrom
fix/connect-policy-runtime-convergence

Conversation

@reachjalil

Copy link
Copy Markdown
Member

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

  • Runtime generation (apps/desktop/electron/runtime.mjs, packages/types/src/desktop-ipc.ts): the desktop runtime now assigns a monotonic per-start generation to the embedded server and reports it through openworkServerInfo, making each server lifetime observable despite sticky ports and tokens.
  • Reconciler (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.
  • Stale-attempt invalidation: every trigger starts a new epoch; older attempts are abandoned at each await point, never record success, and all policy requests are serialized behind a lock with an in-lock staleness re-check, so an obsolete workspace/organization/generation attempt can neither record success nor land after a newer application.
  • Bounded backoff: transient failures (runtime not ready, request failure) retry on a capped schedule (1 s → 30 s, 7 attempts per epoch). Exhaustion parks the reconciler in a sanitized pending/stalled state (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.
  • The superseded finite-retry deliverConnectState and its constants are removed.

Preservation

  • Local-only users: no desired policy → reconciler stays idle; nothing is pushed.
  • A ready runtime receives policy immediately (no waits when the target resolves first try).
  • Idempotent: re-observing a converged target issues no requests; applying an already-applied value is a no-op PUT.
  • Workspace switching cannot cross-apply: the target is resolved per attempt and stale epochs are invalidated, so a late previous-workspace attempt cannot mutate or be recorded against the new runtime.
  • Organization switching reasserts only the new organization's desired state (revision is part of the desired tuple; equal booleans still reassert once).
  • Both enable and disable converge; temporary unavailability parks recoverably instead of failing permanently; runtime configuration unrelated to Connect is untouched (the server PUT writes only connect-state.json, unchanged in this PR).
  • Nothing gates on the sync state, so unaffected local functionality keeps working while reconciliation is pending.

Proof

Lane: local (Daytona credentials unavailable on this machine — Infisical CLI not installed). All commands ran on the exact head 429f739bd after rebasing onto origin/dev c7e378169.

# Claim Where proven Command Verdict
1 Policy before readiness; runtime ready after the old retry window; still converges unit: backoff exhaustion + readiness observation re-arms (fake waits); e2e: 5 s delayed local server converges bun test tests/connect-policy-reconciler.test.ts / vitest … connect-policy-runtime-restart.e2e.test.ts Passed
2 Runtime ready before policy; immediate convergence unit "applies immediately…"; e2e connect-state-provenance same Passed
3 Restart reapplies unchanged policy to the new generation unit "reapplies an unchanged policy…"; e2e restarts the server with connect-state.json cleared and proves reapplication on a new generation same Passed
4/5 false-over-true and true-over-false converge unit "converges both policy directions" bun test tests/connect-policy-reconciler.test.ts Passed
6 Workspace A cannot mutate workspace B after a switch unit "a late result from an obsolete generation is discarded" (target switch mid-flight; stale result never recorded) same Passed
7 Org A cannot overwrite org B policy after a switch unit "a stale queued attempt abandons its request" (serialized lock; middle epoch never issues its request) same Passed
8 Late result from obsolete generation ignored unit, same tests as 6/7 same Passed
9 Transient failure retries with bounded backoff and converges unit "retries transient apply failures…" (observed delays = schedule) same Passed
10 Permanent failure surfaces a sanitized recoverable state, non-blocking unit "permanent failure parks…" (state is exactly {state:"stalled",reason:"apply_failed"}) same Passed
11 Idempotent; no uncontrolled request loop unit "re-observing a converged target…" (repeat observations ⇒ 1 request, 0 timers) same Passed
12 Unrelated runtime configuration intact server Connect PUT path unchanged; apps/server connect-state tests + typecheck bun test src/connect-state.test.ts src/connect-state.inspect.test.ts, tsc --noEmit Passed

Commands and results (exact head):

  • pnpm --dir apps/app typecheck — Passed
  • apps/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 — Passed
  • apps/server: tsc --noEmit + connect-state tests — Passed
  • OPENWORK_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 — Passed
  • apps/app: bun test --isolate tests/ — 882 pass / 7 fail; the same 7 failures reproduce identically on the untouched base commit 5c74c2530 (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 base 5c74c2530 (fault-proxy never observes a faulted /api/den/v1/me/orgs request; the Connect and organization convergence assertions inside it pass before that witness). Pre-existing red, reported for visibility.

Negative proof

  • Stale attempts: with an apply in flight against generation A, switching the target to generation B discards A's completion — the recorded tuple and final state belong to B (unit-asserted).
  • Serialized requests: three rapid desired changes produce exactly two requests; the stale middle attempt is abandoned inside the request lock and never reaches the server (unit-asserted).
  • No uncontrolled loop: repeated runtime observations of a converged target issue zero policy requests and schedule zero timers (unit-asserted).
  • The restart e2e clears connect-state.json before restarting, so post-restart convergence cannot be explained by persistence — only by reapplication to the observed new generation.

Risk and rollback

  • The generation field is additive to OpenworkServerInfo; bridges that predate it yield null and the target identity degrades to base URL + host token (the previous best available signal). No wire or persistence format changes.
  • The reconciler is renderer-local, reads the same desktop-config source of truth as before, and introduces no second policy authority. Reverting this commit restores the previous retry behavior wholesale.
  • Runtime lifecycle risk is limited to one counter increment inside the existing successful-start commit path in 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.

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.
@vercel

vercel Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
openwork-app Ready Ready Preview Aug 24, 2026 11:32pm
openwork-den Ready Ready Preview Aug 24, 2026 11:32pm
openwork-den-worker-proxy Ready Ready Preview Aug 24, 2026 11:32pm
openwork-diagnostics Ready Ready Preview Aug 24, 2026 11:32pm
openwork-landing Building Building Preview, v0 Aug 24, 2026 11:32pm

@reachjalil

reachjalil commented Aug 24, 2026 •

Copy link
Copy Markdown
Member Author

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

screenshots not uploaded (no BLOB_READ_WRITE_TOKEN)

ℹ️ ASSERTION — 1. The organization Connect policy converged on a slow-starting runtime

Although the local server delayed its readiness past the policy arrival, the state endpoint reported status=available and connectEnabled=true.

  • ✅ PASS The organization Connect policy converged on a slow-starting runtime — Although 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 restart

connect-state.json reports missing, so any post-restart Connect state must come from a fresh policy application, not persistence.

  • ✅ PASS The persisted Connect state was cleared before the restart — connect-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 generation

The restarted local server reported per-start generation 3 where the previous lifetime reported 2.

  • ✅ PASS The restart produced a new runtime generation — The 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 generation

With persistence cleared, the restarted server reached status=available and connectEnabled=true again — the desired organization policy was re-delivered to the new generation.

  • ✅ PASS The unchanged policy was reapplied to the new runtime generation — With 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 profile

connect-state.json reports connectEnabled=true again after the reapplication.

  • ✅ PASS The reapplied policy persisted to the profile — connect-state.json reports connectEnabled=true again after the reapplication.

Test run created 2026-08-24T23:26:39.379Z · Source: evals/results/test-runs/2026-08-24T23-26-39-378Z-connect-policy-converges-on-a-slow-starting-runtime-and-is-reapplied-to-the-next/test-run.json · Repro: pnpm --dir evals artifacts:publish -- --pr 4052 --test-run 2026-08-24T23-26-39-378Z-connect-policy-converges-on-a-slow-starting-runtime-and-is-reapplied-to-the-next

@diff-warden diff-warden Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@reachjalil
reachjalil merged commit 8fc2d61 into dev Aug 24, 2026
25 checks passed

This branch was successfully deployed

5 active deployments
Preview – openwork-den-worker-proxy — 429f739b Deployed Aug 24, 2026 by vercel[bot]
Preview – openwork-landing — 429f739b Deployed Aug 24, 2026 by vercel[bot]
Preview – openwork-den — 429f739b Deployed Aug 24, 2026 by vercel[bot]
Preview – openwork-app — 429f739b Deployed Aug 24, 2026 by vercel[bot]
Preview – openwork-diagnostics — 429f739b Deployed Aug 24, 2026 by vercel[bot]
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.

1 participant