惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
H
Help Net Security
Jina AI
Jina AI
V
V2EX
G
Google Developers Blog
B
Blog
GbyAI
GbyAI
U
Unit 42
爱范儿
爱范儿
腾讯CDC
Engineering at Meta
Engineering at Meta
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
小众软件
小众软件
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
博客园 - 聂微东
The Cloudflare Blog
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(cli): guard device fallback state · openclaw/openclaw...
vincentkoc · 2026-05-06 · via Recent Commits to openclaw:main

@@ -3,7 +3,10 @@ import { buildGatewayConnectionDetails, callGateway } from "../gateway/call.js";

33

import { ADMIN_SCOPE, PAIRING_SCOPE, type OperatorScope } from "../gateway/method-scopes.js";

44

import { isLoopbackHost } from "../gateway/net.js";

55

import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../gateway/protocol/client-info.js";

6-

import { readConnectPairingRequiredMessage } from "../gateway/protocol/connect-error-details.js";

6+

import {

7+

readConnectPairingRequiredMessage,

8+

type ConnectPairingRequiredDetails,

9+

} from "../gateway/protocol/connect-error-details.js";

710

import {

811

approveDevicePairing,

912

formatDevicePairingForbiddenMessage,

@@ -82,6 +85,8 @@ type DevicePairingList = {

82858386

const FALLBACK_NOTICE = "Direct scope access failed; using local fallback.";

8487

const DEFAULT_DEVICES_TIMEOUT_MS = 10_000;

88+

const FALLBACK_STATE_MISMATCH_MESSAGE =

89+

"Gateway requires device pairing, but local fallback pairing state does not contain the gateway request.";

8590

const OPERATOR_ROLE = "operator";

8691

const OPERATOR_SCOPE_PREFIX = "operator.";

8792

const KNOWN_NON_ADMIN_OPERATOR_SCOPES = new Set<OperatorScope>([

@@ -143,24 +148,56 @@ function isDevicePairingApprovalDenied(error: unknown): boolean {

143148

);

144149

}

145150146-

function shouldUseLocalPairingFallback(opts: DevicesRpcOpts, error: unknown): boolean {

151+

function resolveLocalPairingFallback(

152+

opts: DevicesRpcOpts,

153+

error: unknown,

154+

): { details: ConnectPairingRequiredDetails } | null {

147155

const message = normalizeLowercaseStringOrEmpty(normalizeErrorMessage(error));

148-

if (!readConnectPairingRequiredMessage(message)) {

149-

return false;

156+

const details = readConnectPairingRequiredMessage(message);

157+

if (!details) {

158+

return null;

150159

}

151160

if (typeof opts.url === "string" && opts.url.trim().length > 0) {

152161

// Explicit --url might point at a remote/tunneled gateway; never silently

153162

// switch to local pairing files in that case.

154-

return false;

163+

return null;

155164

}

156165

const connection = buildGatewayConnectionDetails();

157166

if (connection.urlSource !== "local loopback") {

158-

return false;

167+

return null;

159168

}

160169

try {

161-

return isLoopbackHost(new URL(connection.url).hostname);

170+

return isLoopbackHost(new URL(connection.url).hostname) ? { details } : null;

162171

} catch {

163-

return false;

172+

return null;

173+

}

174+

}

175+176+

function buildFallbackStateMismatchError(details: ConnectPairingRequiredDetails): Error {

177+

return new Error(

178+

[

179+

details.requestId

180+

? `${FALLBACK_STATE_MISMATCH_MESSAGE} Missing requestId: ${details.requestId}.`

181+

: FALLBACK_STATE_MISMATCH_MESSAGE,

182+

"The running gateway is probably using a different OPENCLAW_PROFILE or OPENCLAW_STATE_DIR than this CLI.",

183+

"Rerun with the same profile/state-dir as the gateway, or pass --token/--password so the CLI can approve through the gateway.",

184+

].join("\n"),

185+

);

186+

}

187+188+

function assertLocalFallbackMatchesGatewayRequest(

189+

details: ConnectPairingRequiredDetails,

190+

list: DevicePairingList,

191+

) {

192+

const requestId = normalizeOptionalString(details.requestId);

193+

if (!requestId) {

194+

return;

195+

}

196+

const hasRequest = (list.pending ?? []).some(

197+

(request) => normalizeOptionalString(request.requestId) === requestId,

198+

);

199+

if (!hasRequest) {

200+

throw buildFallbackStateMismatchError(details);

164201

}

165202

}

166203

@@ -176,17 +213,20 @@ async function listPairingWithFallback(opts: DevicesRpcOpts): Promise<DevicePair

176213

try {

177214

return parseDevicePairingList(await callGatewayCli("device.pair.list", opts, {}));

178215

} catch (error) {

179-

if (!shouldUseLocalPairingFallback(opts, error)) {

216+

const fallback = resolveLocalPairingFallback(opts, error);

217+

if (!fallback) {

180218

throw error;

181219

}

182-

if (opts.json !== true) {

183-

defaultRuntime.log(theme.warn(FALLBACK_NOTICE));

184-

}

185220

const local = await listDevicePairing();

186-

return {

221+

const list = {

187222

pending: local.pending as PendingDevice[],

188223

paired: local.paired.map((device) => redactLocalPairedDevice(device)),

189224

};

225+

assertLocalFallbackMatchesGatewayRequest(fallback.details, list);

226+

if (opts.json !== true) {

227+

defaultRuntime.log(theme.warn(FALLBACK_NOTICE));

228+

}

229+

return list;

190230

}

191231

}

192232

@@ -211,23 +251,31 @@ async function approvePairingWithFallback(

211251

{ scopes: [ADMIN_SCOPE] },

212252

);

213253

}

214-

if (!shouldUseLocalPairingFallback(opts, error)) {

254+

const fallback = resolveLocalPairingFallback(opts, error);

255+

if (!fallback) {

215256

throw error;

216257

}

217-

if (opts.json !== true) {

218-

defaultRuntime.log(theme.warn(FALLBACK_NOTICE));

258+

const gatewayRequestId = normalizeOptionalString(fallback.details.requestId);

259+

if (gatewayRequestId && gatewayRequestId !== requestId) {

260+

throw buildFallbackStateMismatchError(fallback.details);

219261

}

220262

const approved = await approveDevicePairing(requestId, {

221263

// Local CLI fallback already assumes direct machine access; treat it as an

222264

// explicit admin approval path instead of relying on missing caller scopes.

223265

callerScopes: ["operator.admin"],

224266

});

225267

if (!approved) {

268+

if (gatewayRequestId && gatewayRequestId === requestId) {

269+

throw buildFallbackStateMismatchError(fallback.details);

270+

}

226271

return null;

227272

}

228273

if (approved.status === "forbidden") {

229274

throw new Error(formatDevicePairingForbiddenMessage(approved), { cause: error });

230275

}

276+

if (opts.json !== true) {

277+

defaultRuntime.log(theme.warn(FALLBACK_NOTICE));

278+

}

231279

return {

232280

requestId,

233281

device: redactLocalPairedDevice(approved.device),