Skip to content

refactor(gateway): extract source-agnostic desktop relay core - #122455

Merged
steipete merged 3 commits into
mainfrom
steipete/desktop-relay-core-extract
Aug 12, 2026
Merged

steipete merged 3 commits into
mainfrom
steipete/desktop-relay-core-extract

Conversation

@steipete

@steipete steipete commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

The Gateway's remote-desktop observer is hardwired to cloud-worker environments: the observer lifecycle, token minting, WebSocket relay, and view-only enforcement all live inside src/gateway/worker-environments/ and assume an SSH-forwarded unix socket. Upcoming work adds more desktop sources (the Gateway host's own screen, paired nodes), which need the same relay pipeline without the worker coupling.

Why This Change Was Made

Pure extraction refactor, zero behavior change: the source-agnostic core moves to src/gateway/desktop/ — an RfbAttachment union (unix socket | loopback TCP), a generic desktop session registry (8-observer cap, single controller with control-taken eviction, 60s linger, owner-epoch fencing, typed stale-owner/stopped errors), the observe-token + WS relay bridge, and the RFB view-only filter. desktop-tunnel.ts keeps only worker-specific SSH acquisition and app launch, plugged into the shared registry as start/teardown hooks; its external API is unchanged. The observe WS path moves from /worker-desktop/observe to /desktop/observe — private contract, clients consume wsPath verbatim from the worker.desktop.observe RPC result. Also drops a now() option that was dead code before the refactor.

User Impact

No user-visible change. Cloud-worker desktop observers (Labs cloudWorkers.desktop) behave identically: same RPC surface, same token format and TTL, same observer semantics, same view-only enforcement. This unblocks follow-up PRs that add a Gateway-host desktop source and node desktop streaming.

Evidence

  • P1 race fix: the shared registry's stopSuperseded() now fences only strictly older owner epochs, preserving a same-epoch observe session when app launch claimed the epoch first.
  • Regression coverage: desktop-tunnel.test.ts reproduces the launch/observe ordering and proves the same-epoch desktop session remains alive.
  • node scripts/run-vitest.mjs src/gateway/desktop src/gateway/worker-environments: 177 files, 2,013 tests passed on head 57bc9790217, including the new P1 regression.
  • node --import tsx scripts/check-deadcode-exports.mts: production, full-tree, and script scans passed with 0 entries.
  • Exact-head CI run 31570657246: green.
  • Codex autoreview over the rebased full bundle: no accepted/actionable findings.
  • git diff --check clean.

@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@steipete
steipete marked this pull request as ready for review August 12, 2026 05:23
@steipete
steipete requested a review from a team as a code owner August 12, 2026 05:23
@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Aug 12, 2026
@clawsweeper clawsweeper Bot added merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Aug 12, 2026
@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed August 12, 2026, 2:03 AM ET / 06:03 UTC.

ClawSweeper review

What this changes

This PR moves the cloud-worker desktop relay into a source-agnostic Gateway desktop module while retaining worker-specific SSH acquisition and app launching.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep open: the extraction introduces a same-owner launch/observe race that can stop a newly created desktop session and fail the observer connection.

Priority: P1
Reviewed head: faa487c2ae11c1a1a39a4852195e6c3f9fc7fc5a

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The extraction has meaningful targeted coverage, but the owner-epoch race is a merge-blocking correctness defect.
Proof confidence 🌊 off-meta tidepool Not applicable: This maintainer-authored internal relay refactor is covered by targeted Gateway and desktop harness evidence; contributor real-setup proof is not required.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This maintainer-authored internal relay refactor is covered by targeted Gateway and desktop harness evidence; contributor real-setup proof is not required.
Evidence reviewed 5 items PR-head fence stops any session: The worker adapter unconditionally stops the registry entry while fencing a newly claimed owner epoch, including an entry that may already belong to that same epoch.
Current-main behavior fences only stale owners: The pre-refactor owner fence checks that the active desktop entry has a lower epoch before stopping it, preserving a same-epoch entry created concurrently.
Concurrent caller path: Desktop observation acquires the tunnel using the record owner epoch, while app launch uses the same epoch; either RPC can win the first post-replacement operation.
Findings 1 actionable finding [P1] Fence only older desktop sessions
Security None None.

How this fits together

A worker desktop RPC acquires an RFB endpoint and returns a short-lived WebSocket path to Control UI. Gateway upgrades that path, attaches an observer to the desktop lifecycle, and relays RFB traffic with view-only filtering.

flowchart LR
  A[Control UI desktop request] --> B[Worker desktop RPC]
  B --> C[Desktop session registry]
  C --> D[Observer token]
  D --> E[Gateway WebSocket upgrade]
  E --> F[RFB relay]
  F --> G[Worker desktop attachment]
Loading

Before merge

  • Fence only older desktop sessions (P1) - launchApp can claim a new epoch before its async body reaches this helper. If a same-epoch acquire() creates the new registry entry first, this unconditional stop tears it down and the observer fails with “stopped before connecting.” Current main stopped only entries whose epoch was lower; preserve that invariant and cover the launch/observe race.
  • Resolve merge risk (P1) - Merging can make a desktop observer fail when app launch wins the first owner-epoch operation and a same-epoch observation starts before launch fencing completes.
  • Complete next step (P2) - A narrow, source-proven lifecycle repair and regression test can be applied without changing the RPC, token, or UI contract.

Findings

  • [P1] Fence only older desktop sessions — src/gateway/worker-environments/desktop-tunnel.ts:108-109
Agent review details

Security

None.

PR surface

Source +163, Tests +41. Total +204 across 19 files.

View PR surface stats
Area Files Added Removed Net
Source 11 464 301 +163
Tests 8 95 54 +41
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 19 559 355 +204

Review metrics

Metric Value Why it matters
Production versus test delta production +464/-301 (net +163); tests +95/-54 (net +41) The production growth is concentrated in extracting the shared desktop lifecycle registry; the added race regression should remain focused in the existing worker desktop suite.

Stored data model

Persistent data-model change detected: durable storage schema: src/gateway/worker-environments/desktop-observe.test.ts, durable storage schema: src/gateway/worker-environments/desktop-observe.ts, durable storage schema: src/gateway/worker-environments/desktop-tunnel.ts, durable storage schema: src/gateway/worker-environments/environment-access.test.ts, durable storage schema: src/gateway/worker-environments/environment-access.ts, durable storage schema: src/gateway/worker-environments/tunnel.ts, and 7 more. Confirm migration or upgrade compatibility proof before merge.

Merge-risk options

Maintainer options:

  1. Preserve same-epoch sessions before merge (recommended)
    Fence only sessions owned by an older epoch, then add a focused launch/observe race regression before merging.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Preserve same-epoch desktop sessions during concurrent launch and observe, add a focused race regression, and rerun the desktop tunnel and Gateway upgrade tests.

Technical review

Best possible solution:

Preserve a same-epoch session during fencing and add a regression test for concurrent first launch and observation after an owner advance.

Do we have a high-confidence way to reproduce the issue?

Yes: start app launch as the first operation for a new owner epoch, begin same-epoch desktop acquisition before its fencing step runs, and observe that the unconditional stop rejects the new acquisition.

Is this the best way to solve the issue?

No: the shared registry is the right owner, but the worker adapter must retain the prior lower-epoch fence invariant rather than stopping every current entry.

Full review comments:

  • [P1] Fence only older desktop sessions — src/gateway/worker-environments/desktop-tunnel.ts:108-109
    launchApp can claim a new epoch before its async body reaches this helper. If a same-epoch acquire() creates the new registry entry first, this unconditional stop tears it down and the observer fails with “stopped before connecting.” Current main stopped only entries whose epoch was lower; preserve that invariant and cover the launch/observe race.
    Confidence: 0.98

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 6752f8b4f133.

Labels

Label changes:

  • add P1: The introduced race can interrupt the active desktop-observation workflow for a worker after an owner epoch advances.
  • add merge-risk: 🚨 availability: The unconditional fence can tear down a valid same-epoch RFB session before the observer connects.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored internal relay refactor is covered by targeted Gateway and desktop harness evidence; contributor real-setup proof is not required.

Label justifications:

  • P1: The introduced race can interrupt the active desktop-observation workflow for a worker after an owner epoch advances.
  • merge-risk: 🚨 availability: The unconditional fence can tear down a valid same-epoch RFB session before the observer connects.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Not applicable: This maintainer-authored internal relay refactor is covered by targeted Gateway and desktop harness evidence; contributor real-setup proof is not required.

Evidence

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs src/gateway/worker-environments/desktop-tunnel.test.ts.
  • [P1] node scripts/run-vitest.mjs src/gateway/desktop src/gateway/server.plugin-node-capability-auth.test.ts.
  • [P1] git diff --check.

What I checked:

Likely related people:

  • steipete: Authored the original cloud-worker desktop observer feature and this extraction; recent Gateway history also includes the desktop stream admission work. (role: feature owner and recent area contributor; confidence: high; commits: 8fdf7570a17f, 61ab6a8f9d15, faa487c2ae11; files: src/gateway/worker-environments/desktop-tunnel.ts, src/gateway/server-http.ts, ui/src/components/desktop/desktop-panel.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Fence only lower-epoch sessions and add the focused concurrent launch/observe regression.
  • Refresh exact-head Gateway desktop tests after the repair.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

Split the cloud-worker desktop observer into a reusable core under
src/gateway/desktop/ so upcoming desktop sources (gateway host, nodes)
plug into one relay pipeline:

- attachment.ts: RfbAttachment union (unix socket | loopback TCP)
- session-registry.ts: generic observer lifecycle (8-observer cap,
  controller eviction, 60s linger, owner-epoch fencing) with typed
  stale-owner/stopped errors
- observe-bridge.ts: single-use observe tokens + WS relay, path moves
  to /desktop/observe (clients consume wsPath verbatim)
- rfb-view-only-filter.ts: pure move

desktop-tunnel.ts keeps only worker SSH acquisition and app launch,
plugged into the shared registry; external API unchanged. Behavior
is identical; worker desktop tests pass unmodified apart from import
and path renames. Drops the never-used now() option.
The desktop core extraction replaced the owner fence's "stop strictly
older owners" check with an unconditional session stop. A launcher that
claims an owner epoch first, then reaches its async fencing pass after a
same-epoch observe has already created the session, tore that session
down and failed the observer with "stopped before connecting".

Restore the original invariant in the registry that owns it:
stopSuperseded() retires an entry only when its epoch is strictly lower
than the claimant's, so peers sharing a generation keep the session.

The regression test drives launch-then-observe at one epoch and fails on
the pre-fix code inside fenceReplacedOwners.
@steipete
steipete force-pushed the steipete/desktop-relay-core-extract branch from faa487c to 57bc979 Compare August 12, 2026 06:36
@steipete
steipete merged commit 07d9f9c into main Aug 12, 2026
129 checks passed
@steipete
steipete deleted the steipete/desktop-relay-core-extract branch August 12, 2026 06:58
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 13, 2026
…aw#122455)

* refactor(gateway): extract source-agnostic desktop relay core

Split the cloud-worker desktop observer into a reusable core under
src/gateway/desktop/ so upcoming desktop sources (gateway host, nodes)
plug into one relay pipeline:

- attachment.ts: RfbAttachment union (unix socket | loopback TCP)
- session-registry.ts: generic observer lifecycle (8-observer cap,
  controller eviction, 60s linger, owner-epoch fencing) with typed
  stale-owner/stopped errors
- observe-bridge.ts: single-use observe tokens + WS relay, path moves
  to /desktop/observe (clients consume wsPath verbatim)
- rfb-view-only-filter.ts: pure move

desktop-tunnel.ts keeps only worker SSH acquisition and app launch,
plugged into the shared registry; external API unchanged. Behavior
is identical; worker desktop tests pass unmodified apart from import
and path renames. Drops the never-used now() option.

* fix(gateway): fence only superseded desktop sessions

The desktop core extraction replaced the owner fence's "stop strictly
older owners" check with an unconditional session stop. A launcher that
claims an owner epoch first, then reaches its async fencing pass after a
same-epoch observe has already created the session, tore that session
down and failed the observer with "stopped before connecting".

Restore the original invariant in the registry that owns it:
stopSuperseded() retires an entry only when its epoch is strictly lower
than the claimant's, so peers sharing a generation keep the session.

The regression test drives launch-then-observe at one epoch and fails on
the pre-fix code inside fenceReplacedOwners.

* refactor(gateway): drop unused WorkerDesktopTunnels type export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant