Skip to content

fix(cli): never fall back to local state when a remote Gateway is unreachable - #159223

Merged
steipete merged 2 commits into
mainfrom
fix/live2-sessions-cleanup-remote
Sep 27, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/live2-sessions-cleanup-remote

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Two CLI commands try the configured Gateway first and, if it is unreachable, fall back to editing local state offline:

  • openclaw sessions cleanup --enforce (Gateway sessions.cleanup, since 4967bcb)
  • openclaw agents delete <id> (Gateway agents.delete)

The fallback did not check which Gateway failed. With a remote Gateway, typically reached through an SSH tunnel on a loopback URL, a dead tunnel produced the same pre-connect transport failure as a stopped local Gateway, so the CLI reported success after mutating the wrong machine: sessions cleanup pruned the local session store and agents delete deleted the agent's local config, workspace, and session state, while the remote Gateway the operator meant to change was untouched. agents delete did the same when a remote Gateway required credentials.

Why This Change Was Made

Both commands carried their own inline "this failure proves the Gateway never got the request" check, and neither asked whether the configured target was the local backend. They now share one decision, resolveGatewayMutationFallback (src/gateway/call-mutation-fallback.ts), which combines the existing pre-connect/credentials checks with isImplicitLocalGatewayTarget. Offline fallback stays available only for the implicit local Gateway; the attempted URL is pinned (expectUrl) so the call cannot drift to another target. For a non-local target the command fails, leaves local state unchanged, and says to restore the connection/credentials or run the command on the Gateway host. Explicit offline repair (sessions cleanup --store <path>) is unchanged. Docs for both commands describe the rule.

User Impact

With a remote Gateway, a broken connection no longer makes sessions cleanup --enforce or agents delete silently change this machine instead; the command fails with guidance. Local-Gateway users see no change.

Evidence

  • sessions-cleanup.test.ts: remote/loopback-tunnel connection failure cases fail on main ("promise resolved instead of rejecting") and pass.
  • Agents delete: four regressions (remote unreachable, remote credentials-required, and target pinning) fail against main and pass.
  • On a Blacksmith Testbox via Crabbox: 227 tests across the sessions-cleanup, agents-delete, and src/gateway/call.test.ts suites passed; node scripts/check-changed.mjs passed.
  • Codex autoreview: scoped-clean ("patch is correct", 0.90).
  • Transport failures are simulated; no live SSH tunnel was torn down.

Production LOC: +52 / −10 (includes the shared 18-line decision). Tests: +128 / −31. Docs: +13 / −1.

Restrict automatic offline cleanup to the configured local Gateway. Pin the resolved RPC destination while preserving its authentication selection, so remote configurations and environment URL overrides cannot silently clean local stores when their connection fails.

Regression: two refused-loopback-tunnel cases fail on the original code and pass with the fix. Blacksmith validation passed 191 focused tests and check-changed. The changed command suite took 18.684s wall with one worker. Independent review found no actionable issues through P2.
Share the pre-dispatch fallback decision with sessions cleanup under the Gateway call owner. Pin agent deletion to the classified config and URL; remote transport and credential failures now leave local agent state intact and explain how to restore the connection or run on the Gateway host. Preserve local credential fallback with skipped cron cleanup and keep ambiguous transport errors unchanged.

Four remote-target regression cases failed before the fix against the agents-delete implementation identical to origin/main. Blacksmith Testbox passed all 227 tests across the agents deletion, sessions cleanup, and Gateway call suites (41.36s total), plus check-changed. The new regression cases took 0.5-0.7s each. Isolated review was clean through P2 after replacing an expired reviewer login with the existing Testbox API credential. Local hooks were skipped to honor the remote-only check requirement.
@clawsweeper

clawsweeper Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review September 26, 2026 22:35
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime commands Command implementations size: M labels Sep 26, 2026
@clawsweeper

clawsweeper Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed September 26, 2026, 6:51 PM ET / 22:51 UTC.

ClawSweeper review

What this changes

The CLI checks the selected Gateway before allowing agent deletion or session cleanup to fall back to local state, pins the request URL, and documents the remote-failure behavior.

Merge readiness

⛔ Blocked before merge - 3 items remain

Keep open. Current main still has the wrong-machine fallback. The proposed guard addresses it, but the newly surfaced remote session-cleanup error gives misleading local recovery advice, and the upgrade behavior needs direct confirmation.

Priority: P0
Reviewed head: 0cee712795a41aff6ea3c919c81424038438197e

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The core safety guard has focused regression coverage, while remote recovery guidance and installed-upgrade behavior remain unresolved.
Proof confidence 🦐 gold shrimp (3/6) Not applicable: The repository member-authored PR is exempt from contributor real-behavior proof. Its command-level tests mock Gateway failures and check that local writers are not called; the PR reports no live tunnel run. No stored-data format changes.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The repository member-authored PR is exempt from contributor real-behavior proof. Its command-level tests mock Gateway failures and check that local writers are not called; the PR reports no live tunnel run. No stored-data format changes.
Evidence reviewed 8 items Current-main behavior: The current-main session command treats a preconnect close as permission to run local cleanup, without checking the selected Gateway.
Current-main agent deletion: The current-main agent command likewise permits local deletion after a preconnect close or missing Gateway credentials.
Proposed target guard: The introduced shared decision permits local fallback only for the implicit local target; both commands pass a selected URL to the Gateway call.
Findings 1 actionable finding [P2] Give remote session-cleanup failures a remote recovery hint
Security None None.

How this fits together

These CLI commands ask a Gateway to change agent or session state. If that request fails before connecting, the commands decide whether an offline write to this machine is safe.

flowchart LR
A[Delete or cleanup command] --> B[Config and selected URL]
B --> C[Classify Gateway target]
C --> D[Gateway request]
D --> H[Gateway state updated]
D --> E{Preconnect failure?}
E -->|Implicit local| F[Local state cleanup]
E -->|Remote target| G[Error without local write]
Loading

Before merge

  • Give remote session-cleanup failures a remote recovery hint (P2) - This new non-local failure path rethrows the generic unreachable error. For a refused loopback SSH tunnel, that error advises openclaw gateway run or openclaw gateway status on the CLI machine, although the intended Gateway is remote. Add a command-specific hint to restore the tunnel or run on the Gateway host, as the agent-delete path does.
  • Resolve merge risk (P0) - Remote-configured operators will lose automatic local fallback during an outage and must restore the connection or run an explicit local repair. The documented change has simulated coverage, but no installed-client upgrade observation.
  • Complete next step (P2) - Add remote-specific recovery guidance to the session-cleanup failure path and assert the CLI-visible error for remote config and URL overrides.

Findings

  • [P2] Give remote session-cleanup failures a remote recovery hint — src/commands/sessions-cleanup.ts:261-263
Agent review details

Security

None.

PR surface

Source +42, Tests +97, Docs +12. Total +151 across 9 files.

View PR surface stats
Area Files Added Removed Net
Source 3 52 10 +42
Tests 4 128 31 +97
Docs 2 13 1 +12
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 9 193 42 +151

Review metrics

None.

Merge-risk options

Maintainer options:

  1. Verify the upgrade path (recommended)
    Confirm an existing remote configuration fails without local writes during an outage and that an implicit local configuration still permits offline fallback.
  2. Accept simulated coverage
    Land the intentional fail-closed change using the documented recovery path and focused simulated regressions as the available upgrade evidence.

Technical review

Best possible solution:

Keep the implicit-local fallback and explicit --store repair path, and give remote session-cleanup failures a clear tunnel or Gateway-host recovery instruction.

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

No live reproduction was performed in this read-only review. Current-main source establishes the remote-to-local fallback path, and the PR reports baseline-failing simulated cases.

Is this the best way to solve the issue?

Yes for target safety: the shared target check and URL pin address wrong-machine writes. The session-cleanup failure path also needs recovery advice that matches a remote target.

Full review comments:

  • [P2] Give remote session-cleanup failures a remote recovery hint — src/commands/sessions-cleanup.ts:261-263
    This new non-local failure path rethrows the generic unreachable error. For a refused loopback SSH tunnel, that error advises openclaw gateway run or openclaw gateway status on the CLI machine, although the intended Gateway is remote. Add a command-specific hint to restore the tunnel or run on the Gateway host, as the agent-delete path does.
    Confidence: 0.91

Overall correctness: patch is incorrect
Overall confidence: 0.85

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P0: The existing failure path can report success after deleting agent or session state on the wrong machine.
  • add merge-risk: 🚨 compatibility: A remote Gateway outage will now stop commands that previously fell back to local edits, so existing operator recovery changes.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.

Label justifications:

  • P0: The existing failure path can report success after deleting agent or session state on the wrong machine.
  • merge-risk: 🚨 compatibility: A remote Gateway outage will now stop commands that previously fell back to local edits, so existing operator recovery changes.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.

Evidence

Acceptance criteria:

  • [P1] pnpm test src/commands/sessions-cleanup.test.ts --maxWorkers=1.
  • [P1] node scripts/check-changed.mjs.

What I checked:

  • Current-main behavior: The current-main session command treats a preconnect close as permission to run local cleanup, without checking the selected Gateway. (src/commands/sessions-cleanup.ts:251, e9dc62b26f85)
  • Current-main agent deletion: The current-main agent command likewise permits local deletion after a preconnect close or missing Gateway credentials. (src/commands/agents.commands.delete.ts:134, e9dc62b26f85)
  • Proposed target guard: The introduced shared decision permits local fallback only for the implicit local target; both commands pass a selected URL to the Gateway call. (src/gateway/call-mutation-fallback.ts:6, 0cee712795a4)
  • CLI guidance gap: The new remote session-cleanup path rethrows the transport error. Its generic text advises running or checking a Gateway locally, while the selected target may be the far end of an SSH tunnel; agent deletion adds remote-specific recovery guidance. (src/commands/sessions-cleanup.ts:261, 0cee712795a4)
  • Gateway error text: The generic unreachable error says to start the Gateway or check its status; the CLI presents expected Gateway transport errors without adding a command-specific remedy. (src/gateway/call.ts:607, 0cee712795a4)
  • Regression coverage: New tests simulate remote-config and environment-override failures and assert that local cleanup or deletion does not run. They mock the Gateway call; the PR body explicitly says no live SSH tunnel was torn down. (src/commands/sessions-cleanup.test.ts:273, 0cee712795a4)

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Sebastien Tardif: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Masato Hoshino: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rank-up moves

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

  • Add and assert a CLI-visible remote recovery hint for session cleanup.
  • Confirm local fallback and remote fail-closed behavior with an existing configuration across upgrade.

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.

@steipete
steipete merged commit e682334 into main Sep 27, 2026
239 of 243 checks passed
@steipete
steipete deleted the fix/live2-sessions-cleanup-remote branch September 27, 2026 00:22
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 27, 2026
…eachable (openclaw#159223)

* fix(sessions): preserve remote cleanup destination on connection failure

Restrict automatic offline cleanup to the configured local Gateway. Pin the resolved RPC destination while preserving its authentication selection, so remote configurations and environment URL overrides cannot silently clean local stores when their connection fails.

Regression: two refused-loopback-tunnel cases fail on the original code and pass with the fix. Blacksmith validation passed 191 focused tests and check-changed. The changed command suite took 18.684s wall with one worker. Independent review found no actionable issues through P2.

* fix(agents): preserve remote deletion targets on pre-connect failure

Share the pre-dispatch fallback decision with sessions cleanup under the Gateway call owner. Pin agent deletion to the classified config and URL; remote transport and credential failures now leave local agent state intact and explain how to restore the connection or run on the Gateway host. Preserve local credential fallback with skipped cron cleanup and keep ambiguous transport errors unchanged.

Four remote-target regression cases failed before the fix against the agents-delete implementation identical to origin/main. Blacksmith Testbox passed all 227 tests across the agents deletion, sessions cleanup, and Gateway call suites (41.36s total), plus check-changed. The new regression cases took 0.5-0.7s each. Isolated review was clean through P2 after replacing an expired reviewer login with the existing Testbox API credential. Local hooks were skipped to honor the remote-only check requirement.

Co-authored-by: Peter Steinberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations docs Improvements or additions to documentation gateway Gateway runtime size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant