Repository navigation
fix(sandbox): re-establish gateway pairing after cross-sandbox snapshot restore #7437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dbce5cb
fix(sandbox): re-establish gateway pairing after cross-sandbox snapsh…
laitingsheng 1ecde5e
test(snapshot): isolate restore pairing in broad suite
prekshivyas bdf1fd6
docs(snapshot): describe restore gateway pairing
prekshivyas 8e303ff
fix(snapshot): verify restored gateway pairing
prekshivyas b2a6780
merge: sync main into PR #7437
prekshivyas 5ebdc36
test(snapshot): isolate restore pairing verification
prekshivyas 34a16a3
refactor(snapshot): isolate pairing verification adapter
prekshivyas 752daf8
merge: sync main into PR #7437
prekshivyas 9a3b9be
fix(snapshot): pair only restored OpenClaw sandboxes
prekshivyas af4fc2d
merge: sync main into PR #7437
prekshivyas a16b0eb
Merge branch 'main' into fix/snapshot-restore-gateway-pairing
cv bd7e853
Merge branch 'main' into fix/snapshot-restore-gateway-pairing
cv 908ed35
Merge branch 'main' into fix/snapshot-restore-gateway-pairing
prekshivyas 91fcaa0
merge(main): sync gateway pairing restore
prekshivyas a4292bb
merge(main): sync restored gateway pairing
prekshivyas fe04c89
merge(main): sync restored gateway pairing
prekshivyas 63e83b3
merge(main): sync restored gateway pairing
prekshivyas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { | ||
| establishRestoredSandboxGatewayPairing, | ||
| RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT, | ||
| RESTORE_GATEWAY_PAIRING_VERIFY_TIMEOUT_MS, | ||
| } from "./restore-gateway-pairing"; | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("establishRestoredSandboxGatewayPairing", () => { | ||
| it("provokes the scope upgrade before approving it", () => { | ||
| const order: string[] = []; | ||
| const warmupScopeUpgrade = vi.fn(() => order.push("warmup")); | ||
| const autoPairScopeApproval = vi.fn(() => order.push("approve")); | ||
| const verifyGatewayPairing = vi.fn(() => { | ||
| order.push("verify"); | ||
| return true; | ||
| }); | ||
|
|
||
| establishRestoredSandboxGatewayPairing("beta", { | ||
| warmupScopeUpgrade, | ||
| autoPairScopeApproval, | ||
| verifyGatewayPairing, | ||
| }); | ||
|
|
||
| expect(warmupScopeUpgrade).toHaveBeenCalledWith("beta"); | ||
| expect(autoPairScopeApproval).toHaveBeenCalledWith("beta"); | ||
| expect(verifyGatewayPairing).toHaveBeenCalledWith("beta"); | ||
| expect(order).toEqual(["warmup", "approve", "verify"]); | ||
| }); | ||
|
|
||
| it("fails when the pairing warm-up does not complete", () => { | ||
| const warmupScopeUpgrade = vi.fn(() => { | ||
| throw new Error("gateway not up"); | ||
| }); | ||
| const autoPairScopeApproval = vi.fn(); | ||
| const verifyGatewayPairing = vi.fn(() => true); | ||
|
|
||
| expect(() => | ||
| establishRestoredSandboxGatewayPairing("beta", { | ||
| warmupScopeUpgrade, | ||
| autoPairScopeApproval, | ||
| verifyGatewayPairing, | ||
| }), | ||
| ).toThrow("gateway not up"); | ||
| expect(autoPairScopeApproval).not.toHaveBeenCalled(); | ||
| expect(verifyGatewayPairing).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("fails when the authenticated verification run cannot use the restored gateway", () => { | ||
| expect(() => | ||
| establishRestoredSandboxGatewayPairing("beta", { | ||
| warmupScopeUpgrade: vi.fn(), | ||
| autoPairScopeApproval: vi.fn(), | ||
| verifyGatewayPairing: vi.fn(() => false), | ||
| }), | ||
| ).toThrow("authenticated gateway verification run did not succeed"); | ||
| }); | ||
|
|
||
| it("verifies the gateway without forcing a new pairing request", () => { | ||
| expect(RESTORE_GATEWAY_PAIRING_VERIFY_TIMEOUT_MS).toBe(30_000); | ||
| expect(RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT).toContain("openclaw agent --agent main --json"); | ||
| expect(RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT).toContain("scope upgrade pending approval"); | ||
| expect(RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT).toContain("transport"); | ||
| expect(RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT).not.toContain( | ||
| "NEMOCLAW_OPENCLAW_FORCE_DEVICE_PAIRING", | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| import { ROOT } from "../../state/paths"; | ||
| import { WARMUP_SESSION_ID_PREFIX } from "./warmup-session"; | ||
|
|
||
| export const RESTORE_GATEWAY_PAIRING_VERIFY_TIMEOUT_MS = 30_000; | ||
|
|
||
| export const RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT = ` | ||
| PROXY_ENV=/tmp/nemoclaw-proxy-env.sh | ||
| [ -r "$PROXY_ENV" ] && . "$PROXY_ENV" | ||
| command -v openclaw >/dev/null 2>&1 || exit 1 | ||
| output="$( | ||
| openclaw agent --agent main --json -m "ping" \ | ||
| --session-id "${WARMUP_SESSION_ID_PREFIX}restore-verify-$$-$(date +%s)" 2>&1 | ||
| )" | ||
| status=$? | ||
| [ "$status" -eq 0 ] || exit 1 | ||
| if printf '%s\n' "$output" | grep -Eiq \ | ||
| 'EMBEDDED FALLBACK|gateway connect failed|scope upgrade pending approval|device pairing required|pairing required|fallbackFrom[": ]+gateway|transport[": ]+embedded'; then | ||
| exit 1 | ||
| fi | ||
| exit 0 | ||
| `; | ||
|
|
||
| export type RestoreGatewayPairingDeps = { | ||
| warmupScopeUpgrade: (sandboxName: string) => void; | ||
| autoPairScopeApproval: (sandboxName: string) => void; | ||
| verifyGatewayPairing: (sandboxName: string) => boolean; | ||
| }; | ||
|
|
||
| function defaultRestoreGatewayPairingDeps(): RestoreGatewayPairingDeps { | ||
| const warmup: typeof import("./auto-pair-warmup") = require("./auto-pair-warmup"); | ||
| const connect: typeof import("./connect") = require("./connect"); | ||
| return { | ||
| warmupScopeUpgrade: warmup.runSandboxScopeWarmupRun, | ||
| autoPairScopeApproval: connect.runConnectAutoPairApprovalPass, | ||
| verifyGatewayPairing: verifyRestoredSandboxGatewayPairing, | ||
| }; | ||
| } | ||
|
|
||
| export function verifyRestoredSandboxGatewayPairing(targetSandbox: string): boolean { | ||
| const { resolveOpenshell } = | ||
| require("../../adapters/openshell/resolve") as typeof import("../../adapters/openshell/resolve"); | ||
| try { | ||
| const openshellBinary = resolveOpenshell(); | ||
| if (!openshellBinary) return false; | ||
| const result = spawnSync( | ||
| openshellBinary, | ||
| [ | ||
| "sandbox", | ||
| "exec", | ||
| "--name", | ||
| targetSandbox, | ||
| "--", | ||
| "sh", | ||
| "-c", | ||
| RESTORE_GATEWAY_PAIRING_VERIFY_SCRIPT, | ||
| ], | ||
| { | ||
| cwd: ROOT, | ||
| env: process.env, | ||
| stdio: ["ignore", "ignore", "ignore"], | ||
| timeout: RESTORE_GATEWAY_PAIRING_VERIFY_TIMEOUT_MS, | ||
| }, | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| return result.status === 0 && result.error === undefined; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function establishRestoredSandboxGatewayPairing( | ||
| targetSandbox: string, | ||
| deps: RestoreGatewayPairingDeps = defaultRestoreGatewayPairingDeps(), | ||
| ): void { | ||
| try { | ||
| deps.warmupScopeUpgrade(targetSandbox); | ||
| deps.autoPairScopeApproval(targetSandbox); | ||
| if (!deps.verifyGatewayPairing(targetSandbox)) { | ||
| throw new Error("the authenticated gateway verification run did not succeed"); | ||
| } | ||
| } catch (err) { | ||
| throw new Error( | ||
| `could not establish gateway pairing for '${targetSandbox}': ${ | ||
| err instanceof Error ? err.message : String(err) | ||
| }`, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.