Skip to content

fix(macos): back off failed Gateway connects - #116114

Merged
vincentkoc merged 2 commits into
mainfrom
perf-macos-runtime-profile-v5
Jul 29, 2026
Merged

vincentkoc merged 2 commits into
mainfrom
perf-macos-runtime-profile-v5

Conversation

@vincentkoc

@vincentkoc vincentkoc commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

What Problem This Solves

Fixes an issue where macOS app surfaces could repeatedly retry a failed Gateway connection, creating rapid WebSocket connection bursts and repeating endpoint/process work when the shared connection could not load its persisted device identity.

Why This Change Was Made

Failed initial connections now arm a monotonic exponential cooldown on the shared GatewayChannelActor before another socket is created. Concurrent callers coalesce behind that one delayed attempt, successful connections reset the cooldown, device-token recovery remains fast, and cancellation or shutdown cannot create a stale socket after the delay.

User Impact

When Gateway authentication or local device identity access is temporarily unavailable, the native app avoids a hot reconnect loop and stays substantially quieter in CPU and system work while still retrying automatically.

Evidence

  • Live observation before the fix: 39 failed Gateway WebSocket connects in two minutes, with clusters roughly 50-150 ms apart, plus repeated endpoint discovery and Gateway process probes.
  • A 15-second process sample repeatedly captured device identity loading and Gateway connection work; short CPU samples spiked to 7.2% during the retry bursts.
  • swift test --package-path apps/macos --filter GatewayChannelConnect: 13 passed.
  • swift test --package-path apps/macos --filter 'GatewayChannel(Disconnect|Request|DeviceTokenRetry|Shutdown)': 20 passed.
  • swift test --package-path apps/macos --filter 'transport cancellation does not publish readiness failure': passed 5 consecutive runs after excluding URL-loading cancellation from the cooldown.
  • SwiftFormat: 0 files require formatting.
  • SwiftLint strict: 0 violations across all touched files.
  • git diff --check: clean.
  • Autoreview: clean, no accepted or actionable findings.
  • Maintainer lifecycle decision: approve the bounded shared cooldown. Device-token recovery remains capped at 250 ms, Swift and URL-loading cancellation do not arm the cooldown, and shutdown revalidates state after the suspension.
  • An after-fix live trace was not added because reproducing the persisted-identity failure requires disturbing the live app state; deterministic current-head tests cover coalescing, recovery, cancellation, and shutdown instead.
  • AI-assisted; the implementation and validation evidence were reviewed before submission.

@vincentkoc vincentkoc self-assigned this Jul 29, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review July 29, 2026 22:53
@openclaw-barnacle openclaw-barnacle Bot added app: macos App: macos size: M maintainer Maintainer-authored PR labels Jul 29, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 29, 2026
@clawsweeper

clawsweeper Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed July 29, 2026, 7:17 PM ET / 23:17 UTC.

ClawSweeper review

What this changes

The PR adds bounded exponential backoff to the shared macOS Gateway connection path so concurrent callers coalesce behind one delayed retry instead of repeatedly creating failed WebSocket connections.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

Keep this PR open for explicit macOS-owner merge judgment. The patch has no concrete correctness or security finding from the reviewed change, but it deliberately changes the shared Gateway reconnect schedule after failure; the protected maintainer label and prior review both require a human to confirm that recovery latency, cancellation, and shutdown semantics are acceptable.

Priority: P1
Reviewed head: f6d810c5fabe06b95d8ca1a97c958f9f44d54c8f
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch is focused and has meaningful targeted coverage, while direct after-fix live runtime evidence is absent and the reconnect-policy tradeoff still needs owner confirmation.
Proof confidence 🦐 gold shrimp (3/6) Not applicable: This is a repository-member PR, so the external-contributor real-behavior-proof gate does not apply; the body nevertheless supplies before-fix live observations and focused after-fix test evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a repository-member PR, so the external-contributor real-behavior-proof gate does not apply; the body nevertheless supplies before-fix live observations and focused after-fix test evidence.
Evidence reviewed 5 items Changed reconnect boundary: The branch adds GatewayConnectFailureBackoff and invokes it from the shared Gateway channel path, making failed connection retries wait before another socket is created while preserving coalescing through the existing shared attempt.
Failure policy implementation: The new backoff helper records a monotonic retry deadline, caps device-token retry delay at 250 ms, excludes Swift and URL-loading cancellation, resets after success, and clears the observed deadline after waiting.
Focused regression coverage: The PR adds connect-path coverage for caller coalescing behind a failed-connect delay, alongside the submitted focused macOS test runs for connect, disconnect, request, device-token retry, and shutdown behavior.
Findings None None.
Security None None.

How this fits together

The macOS app uses a shared Gateway channel to connect native surfaces to the local or remote Gateway. Connection attempts authenticate, establish a WebSocket, and then serve requests; this change inserts retry scheduling before a replacement socket is created after failure.

flowchart LR
  A[macOS app surfaces] --> B[Shared Gateway channel]
  B --> C[Connect attempt]
  C --> D{Connected?}
  D -->|Yes| E[Serve Gateway requests]
  D -->|No| F[Shared failure cooldown]
  F --> C
  C --> G[Cancellation or shutdown]
  G --> B
Loading

Decision needed

Question Recommendation
Should the macOS Gateway owner accept the shared failed-connect cooldown, including its 500 ms to 30 s progression and 250 ms device-token-recovery cap, as the intended recovery contract before merge? Accept the bounded cooldown: Confirm that reduced reconnect churn outweighs delayed retries after ordinary Gateway failures, then merge with the focused lifecycle coverage.

Why: This is a product and lifecycle tradeoff rather than a mechanical defect: the patch prevents connection bursts by intentionally delaying recovery after failure, and tests cannot determine the acceptable user-visible recovery schedule.

Before merge

  • Resolve merge risk (P1) - A shared cooldown intentionally trades rapid recovery for bounded retry work; a macOS owner should confirm the selected 500 ms to 30 s schedule remains acceptable when persisted device identity becomes available shortly after an initial failure.
  • Resolve merge risk (P1) - The delay occurs in the shared connect attempt, so merge safety depends on the asserted cancellation and shutdown revalidation behavior remaining correct across native app lifecycle paths.
Agent review details

Security

None.

PR surface

Other +157. Total +157 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 0 0 0 0
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 4 188 31 +157
Total 4 188 31 +157

Review metrics

Metric Value Why it matters
Shared retry policy 1 shared Gateway reconnect schedule changed The change affects every macOS surface that joins the common channel after a failed connection, not only the initiating caller.

Merge-risk options

Maintainer options:

  1. Confirm and accept the recovery tradeoff (recommended)
    A macOS owner can confirm the bounded shared cooldown is the intended behavior for temporarily unavailable device identity or Gateway authentication, then land the patch.
  2. Revise the failure schedule first
    If the intended recovery contract requires a shorter or more selective delay, update the policy and focused lifecycle tests before merge.

Technical review

Best possible solution:

Retain one shared, cancellation-safe reconnect cooldown and merge only after the macOS owner confirms the bounded delay and device-token fast-retry behavior are the intended native-app lifecycle contract.

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

No high-confidence current-main reproduction was independently established in this review. The submitted live observation and the changed shared connection path make the reported retry burst source-reproducible, but the persisted-identity failure was not rerun against the reviewed head.

Is this the best way to solve the issue?

Unclear pending owner confirmation. A single shared, monotonic cooldown is the narrow maintainable place to stop competing retries, but only the macOS owner can confirm that its recovery timing is the best supported lifecycle contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9e041cd3867f.

Labels

Label justifications:

  • P1: Unbounded failed Gateway reconnect bursts can repeatedly consume native-app CPU and endpoint/process work during a core connection failure.
  • merge-risk: 🚨 availability: The PR deliberately delays shared Gateway recovery after failure, so an incorrect schedule or lifecycle interaction could keep native app surfaces disconnected longer than intended.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a repository-member PR, so the external-contributor real-behavior-proof gate does not apply; the body nevertheless supplies before-fix live observations and focused after-fix test evidence.

Evidence

What I checked:

  • Changed reconnect boundary: The branch adds GatewayConnectFailureBackoff and invokes it from the shared Gateway channel path, making failed connection retries wait before another socket is created while preserving coalescing through the existing shared attempt. (apps/shared/OpenClawKit/Sources/OpenClawKit/GatewayChannel.swift, f6d810c5fabe)
  • Failure policy implementation: The new backoff helper records a monotonic retry deadline, caps device-token retry delay at 250 ms, excludes Swift and URL-loading cancellation, resets after success, and clears the observed deadline after waiting. (apps/shared/OpenClawKit/Sources/OpenClawKit/GatewayConnectFailureBackoff.swift, f6d810c5fabe)
  • Focused regression coverage: The PR adds connect-path coverage for caller coalescing behind a failed-connect delay, alongside the submitted focused macOS test runs for connect, disconnect, request, device-token retry, and shutdown behavior. (apps/macos/Tests/OpenClawIPCTests/GatewayChannelConnectTests.swift, f6d810c5fabe)
  • Review continuity: The prior completed ClawSweeper cycle at the same head reported no line-level findings and asked for macOS-owner confirmation of delayed recovery, cancellation, and shutdown semantics; the supplied current context contains no later source revision that resolves that owner decision. (f6d810c5fabe)
  • Current-main check: Current main is 9e041cd3867f31a0f39403ca4becb3b31fa57ab4, newer than this PR's base, but the supplied review evidence does not establish that main already contains this reconnect policy. This is therefore not eligible for an implemented-on-main close. (9e041cd3867f)

Likely related people:

  • vincentkoc: The supplied history attributes both commits implementing the current shared Gateway retry behavior to this repository member; the PR also records a bounded lifecycle policy for this path. (role: recent macOS Gateway contributor; confidence: medium; commits: 1e3b3c76d688, f6d810c5fabe; files: apps/shared/OpenClawKit/Sources/OpenClawKit/GatewayChannel.swift, apps/shared/OpenClawKit/Sources/OpenClawKit/GatewayConnectFailureBackoff.swift, apps/macos/Tests/OpenClawIPCTests/GatewayChannelConnectTests.swift)

Rank-up moves

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

  • Have the macOS Gateway owner confirm the intended failure-delay and device-token-recovery contract before merge.

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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-07-29T22:57:18.637Z sha 1e3b3c7 :: needs maintainer review before merge. :: none
  • reviewed 2026-07-29T23:10:51.276Z sha f6d810c :: needs maintainer review before merge. :: none

@vincentkoc
vincentkoc merged commit de67565 into main Jul 29, 2026
63 of 67 checks passed
@vincentkoc
vincentkoc deleted the perf-macos-runtime-profile-v5 branch July 29, 2026 23:23
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 30, 2026
* fix(macos): back off failed gateway connects

* fix(macos): skip backoff for transport cancellation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: macos App: macos 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: M 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.

1 participant