Skip to content

refactor: extract WslGatewayKeepAliveService from App.xaml.cs - #605

Merged
shanselman merged 1 commit into
openclaw:masterfrom
AlexAlves87:refactor/extract-wsl-keepalive
Jun 7, 2026
Merged

shanselman merged 1 commit into
openclaw:masterfrom
AlexAlves87:refactor/extract-wsl-keepalive

Conversation

@AlexAlves87

@AlexAlves87 AlexAlves87 commented May 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

App.xaml.cs carried the WSL gateway keepalive logic inline: a fire-and-forget task that keeps the local gateway's WSL2 VM alive after the tray exits, plus cleanup of stale keepalive processes and markers. This moves that concern into a dedicated WslGatewayKeepAliveService.

What changed

  • New Services/WslGatewayKeepAliveService.cs — holds the nine methods (~220 lines) that previously lived in App: the entry task, distro resolution, stale-process/marker cleanup, and the WSL process spawn.
  • App.xaml.cs — the startup seam in OnLaunchedAsync now constructs the service and delegates; the original methods are removed.
  • The two App fields the code reads, _settings and _gatewayRegistry, enter the service as Func<> delegates instead of direct field access. Nothing else changes — same logic, same ordering, same threading, same side effects.
  • The keep-alive window (_keepAliveWindow, the WinUI XamlRoot anchor) is an unrelated concern and stays in App.

Testing

  • ./build.ps1
  • dotnet test ./tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj --no-restore
  • dotnet test ./tests/OpenClaw.Tray.Tests/OpenClaw.Tray.Tests.csproj --no-restore

No new tests: the decision logic is already covered by WslKeepAlivePolicyTests (unchanged); the moved methods are process/filesystem orchestration that was not unit-tested before and is not behavior-changed here.

Proof

Manual run after the extraction. With the local WSL gateway active, the keepalive fires through the new service on startup, identical to the pre-refactor path:

Grabacion.de.pantalla.2026-05-30.191314.mp4
[2026-05-30 19:13:09.126] [INFO] [WslKeepAlive] Started keepalive for OpenClawGateway (PID 40764).

Notes

No behavior change is intended. The seam (Task.Run before InitializeGatewayClient(), off the UI thread), the WSL command (wsl.exe -d <distro> -- sleep infinity), distro resolution precedence, and the stale-cleanup path are all preserved exactly.

Addresses part of #554.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Move the WSL gateway keepalive logic (the fire-and-forget task that keeps
the local gateway's WSL2 VM alive after the tray exits, plus stale-process
and stale-marker cleanup) out of App.xaml.cs into a dedicated service.

App.xaml.cs kept nine private methods (~220 lines) for this concern. They
are moved verbatim into Services/WslGatewayKeepAliveService.cs; the only
adaptation is that the two App fields they read, _settings and
_gatewayRegistry, now enter as Func<> delegates instead of direct field
access. No logic, ordering, threading, or side effect changes.

The keep-alive *window* (_keepAliveWindow, the WinUI XamlRoot anchor) is a
separate concern and stays in App.

Seam unchanged: OnLaunchedAsync still fires the work via Task.Run before
InitializeGatewayClient(), off the UI thread.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clawsweeper

clawsweeper Bot commented May 30, 2026 •

Copy link
Copy Markdown

Codex review: needs maintainer review before merge. Reviewed May 30, 2026, 1:46 PM ET / 17:46 UTC.

Summary
The PR moves WSL gateway keepalive startup, distro resolution, stale marker/process cleanup, and WSL process spawning from App.xaml.cs into a new WslGatewayKeepAliveService, with App delegating during launch.

Reproducibility: not applicable. This PR is a behavior-preserving refactor rather than a bug report. The relevant runtime path is the WSL keepalive startup, which the attached recording and log line demonstrate after the extraction.

Review metrics: 1 noteworthy metric.

  • Diff Scope: 2 files changed, +252/-227. The PR is a narrow extraction rather than a broad App.xaml.cs rewrite.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster ✨ media proof bonus
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • none.

Risk before merge

  • [P1] This path keeps the app-owned WSL gateway alive after tray exit; unit tests cover WslKeepAlivePolicy but not the moved process/filesystem orchestration, so CI should be paired with the provided runtime proof before merge.

Maintainer options:

  1. Accept With CI And Runtime Proof (recommended)
    Maintainers can accept the residual WSL-orchestration risk if required validation stays green and the inspected terminal recording is enough proof for this refactor.
  2. Ask For Extra Characterization
    If maintainers want less residual risk, ask for a focused characterization seam around service delegation or stale cleanup before merge.

Next step before merge

  • [P2] No repair lane is needed because there are no actionable review findings; the remaining action is normal maintainer merge judgment with CI and validation gates.

Security
Cleared: No concrete security or supply-chain issue found; the PR moves existing local WSL and PowerShell process orchestration without adding dependencies, workflows, secrets handling, or new privilege boundaries.

Review details

Best possible solution:

Land the focused extraction once normal CI/build validation is green, leaving the broader god-file decomposition tracked in #554.

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

Not applicable: this PR is a behavior-preserving refactor rather than a bug report. The relevant runtime path is the WSL keepalive startup, which the attached recording and log line demonstrate after the extraction.

Is this the best way to solve the issue?

Yes. Extracting the keepalive concern into a dedicated service while preserving the Task.Run launch point before InitializeGatewayClient() is the narrow maintainable path; broader App decomposition should remain separate.

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against eb06fba21c44.

Label changes

Label changes:

  • add P3: This is a low-risk maintainability refactor with no confirmed broken user workflow in the PR itself.
  • add merge-risk: 🚨 availability: The diff moves startup keepalive orchestration for the local WSL gateway, where a regression could leave the gateway VM without the intended background keepalive.
  • add proof: sufficient: Contributor real behavior proof is sufficient. Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.
  • add proof: 🎥 video: Contributor real behavior proof includes video or recording evidence. Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (recording): Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.

Label justifications:

  • P3: This is a low-risk maintainability refactor with no confirmed broken user workflow in the PR itself.
  • merge-risk: 🚨 availability: The diff moves startup keepalive orchestration for the local WSL gateway, where a regression could leave the gateway VM without the intended background keepalive.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (recording): Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.
  • proof: sufficient: Contributor real behavior proof is sufficient. Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.
  • proof: 🎥 video: Contributor real behavior proof includes video or recording evidence. Sufficient: the inspected MP4 shows the tray launched from a local build and openclaw-tray.log reporting the WSL keepalive started after the refactor.
Evidence reviewed

What I checked:

  • Repository policy applied: AGENTS.md was read fully; its connection, MCP, onboarding, and validation guidance made this a startup/gateway-sensitive review rather than a purely mechanical file split. (AGENTS.md:32, eb06fba21c44)
  • Current main still has the inline implementation: Current main still calls Task.Run(TryEnsureLocalGatewayKeepAliveAsync) from launch and defines the WSL keepalive method in App.xaml.cs, so this PR is not already implemented on main. (src/OpenClaw.Tray.WinUI/App.xaml.cs:604, eb06fba21c44)
  • PR diff is a narrow extraction: The patch adds WslGatewayKeepAliveService and changes the launch call to instantiate that service and invoke TryEnsureAsync, while removing the old private methods from App.xaml.cs. (src/OpenClaw.Tray.WinUI/Services/WslGatewayKeepAliveService.cs:17, 7d9bcf7fac40)
  • Existing coverage is policy-focused: WslKeepAlivePolicyTests covers the start decision, distro resolution, stale distro selection, command-line matching, and marker parsing, but the moved process/filesystem orchestration remains runtime-smoke territory. (tests/OpenClaw.Tray.Tests/WslKeepAlivePolicyTests.cs:6, eb06fba21c44)
  • Existing E2E expectation matches the proof target: The E2E setup test waits for the WSL keepalive log line and checks for a sleep infinity process, which matches the behavior the PR proof demonstrates. (tests/OpenClaw.E2ETests/Setup/SetupAndConnectTests.cs:164, eb06fba21c44)
  • Real behavior proof inspected: The downloaded MP4 is a 1920x1080 recording; inspected frames show a PowerShell run of the tray from a local build and a tailed log line reporting [WslKeepAlive] Started keepalive for OpenClawGateway (PID 40764). (7d9bcf7fac40)

Likely related people:

  • ranjeshj: Recent history shows WSL setup hardening in SetupSteps.cs plus warning-clean App work, which is adjacent to the keepalive startup and setup-managed distro behavior this PR moves. (role: recent WSL setup and App adjacent contributor; confidence: medium; commits: 98d0760cb5b6, ff3a7cc53736, a727197dd709; files: src/OpenClaw.SetupEngine/SetupSteps.cs, src/OpenClaw.Tray.WinUI/App.xaml.cs)
  • shanselman: Recent history includes App lifecycle/refactor hardening, and the related god-file issue discussion identifies staged App/ConnectionPage decomposition as ongoing follow-up work. (role: recent App lifecycle/refactor contributor and reviewer of related cleanup; confidence: medium; commits: 0d4fcbd50ad5; files: src/OpenClaw.Tray.WinUI/App.xaml.cs)
  • kmahone: Recent local dev setup work touched WSL diagnostics and setup flow in SetupSteps.cs, which is adjacent to the app-owned WSL gateway keepalive path. (role: adjacent WSL setup contributor; confidence: low; commits: 9106725567af; files: src/OpenClaw.SetupEngine/SetupSteps.cs)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added proof: sufficient Contributor real behavior proof is sufficient. proof: 🎥 video Contributor real behavior proof includes video or recording evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P3 Low-risk cleanup, docs, polish, ergonomics, or speculative feature. merge-risk: 🚨 availability 🚨 Merging this PR could cause crashes, hangs, restart loops, stalls, or process outages. labels May 30, 2026
@shanselman
shanselman merged commit b175439 into openclaw:master Jun 7, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 availability 🚨 Merging this PR could cause crashes, hangs, restart loops, stalls, or process outages. P3 Low-risk cleanup, docs, polish, ergonomics, or speculative feature. proof: sufficient Contributor real behavior proof is sufficient. proof: 🎥 video Contributor real behavior proof includes video or recording evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants