Skip to content

fix(devices): always route stale-approve operator to openclaw devices list - #80779

Closed
luijoc wants to merge 1 commit into
openclaw:mainfrom
luijoc:fix/devices-approve-stale-requestid-error
Closed

luijoc wants to merge 1 commit into
openclaw:mainfrom
luijoc:fix/devices-approve-stale-requestid-error

Conversation

@luijoc

@luijoc luijoc commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

AI-assisted: drafted with Claude (Opus 4.7). Bug surfaced by the agent
while operating an OpenClaw 2026.5.7 ECS Fargate deployment in production;
full transcript available on request.

Update (rebased 2026-05-14): Post-refactor in fe25ed214e ("refactor(cli): lazy-load devices runtime"), the
approve fallback code moved from src/cli/devices-cli.ts to src/cli/devices-cli.runtime.ts. This PR's fix
now applies in the new file; the bug (return null silent path → bare unknown requestId caller log) is
still present on main and the diff/tests remain the same shape, just at the new file location.

Summary

openclaw devices approve <requestId> on a loopback gateway has a local
file fallback path (src/cli/devices-cli.runtime.ts:220-271 (post-refactor; was src/cli/devices-cli.ts:280-358)) that approves
directly against ~/.openclaw/devices/{pending.json,paired.json} when
the CLI cannot dial the gateway as an admin. The fallback compares the
user-supplied requestId against requestId extracted from the
gateway's pairing-required close error. Three branches today:

  1. Gateway error names a different requestId → throw
    FALLBACK_STATE_MISMATCH_MESSAGE with Missing requestId: <gatewayId>.
    Actionable. ✅
  2. Gateway error names the same requestId, but local pending lacks it
    → throw the same state-mismatch error. Actionable. ✅
  3. Gateway error names no requestId, and local pending lacks the
    user-supplied id → silent return null. The caller at the caller line in the runtime file
    only logs defaultRuntime.error("unknown requestId"). ❌

Branch 3 fires whenever the gateway emits a not-paired close
(buildPairingConnectErrorMessage in
src/gateway/protocol/connect-error-details.ts:275-281 returns the
bare "pairing required" string without a parenthetical) and the
user-supplied requestId is no longer in pending.json. The most common
reason for that disappearance is supersession: the gateway dial just
called requestDevicePairing → reconcilePendingPairingRequests →
canRefreshSingle returned false because the incoming and existing
scope sets diverge (samePendingApprovalSnapshot requires exact set
equality, src/infra/device-pairing.ts:314-331), so the reconciler
replaced the entry with a fresh requestId. Other causes: the 5
minute TTL (PENDING_TTL_MS at src/infra/device-pairing.ts:132) or
running approve under a different OPENCLAW_PROFILE/OPENCLAW_STATE_DIR.

The operator is left with unknown requestId and no pointer to the
current pending state or what to do next.

Reproduction (before)

ECS Fargate container running ghcr.io/openclaw/openclaw:2026.5.7,
Linux x86_64, loopback gateway, paired operator device with only
operator.read. Generated a pending request via
openclaw devices approve --latest preview (which builds a
scope-upgrade request for the full CLI_DEFAULT_OPERATOR_SCOPES set),
then ran the explicit approve:

$ openclaw devices approve 11e7be5a-a243-4855-9439-ccc9291449ba --json
gateway connect failed: GatewayClientRequestError: scope upgrade pending approval (requestId: 1dce2124-3c2d-4fa6-98f9-77ccf1e422d1)
gateway connect failed: GatewayClientRequestError: scope upgrade pending approval (requestId: 34a9feb9-847c-48d7-8a42-057c4d3dab82)
unknown requestId

11e7be5a was the user-supplied id from the preview. The dial built a
new pending whose scope set was a narrower subset of the user-supplied
one (just operator.pairing to call device.pair.approve); the
reconciler replaced rather than refreshed and 11e7be5a was gone by
the time the fallback ran. The operator has no way to discover from
this output that the current pending entry exists locally.

Fix

Replace the silent return null with a thrown Error whose message
includes:

  • the user-supplied (stale) requestId
  • a one-line list of plausible causes (supersession during dial, TTL
    prune, profile mismatch)
  • a count of other pending entries visible in this profile
  • a routing line: openclaw devices list

The diagnostic never prints a direct Rerun: openclaw devices approve <id>
command
, even when only one other pending entry is visible. Three
plausible causes (TTL prune, profile/state mismatch, multi-device
profile churn) can each produce a single visible pending that is
unrelated to the operator's intent. The earlier ClawSweeper review
(P2: Single pending request can be unrelated) called this out
explicitly. Routing through openclaw devices list forces the
operator to verify deviceId + requested scopes before approving,
matching the existing implicit-approve --latest flow which is also
preview-only.

No behavior change to the approval semantics in any branch. The other
two error paths (mismatched gatewayRequestId, gatewayRequestId matches
but pending missing) keep the existing buildFallbackStateMismatchError
text; only the previously silent return null path now throws an
actionable error.

After

No other pending entries

$ openclaw devices approve req-stale --json
[openclaw] Reason: Pairing requestId "req-stale" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
No other pending requests are visible in this profile. List pending requests with: openclaw devices list

One other pending entry (still routes to list — could be unrelated)

$ openclaw devices approve 11111111-1111-1111-1111-111111111111 --json
[openclaw] Reason: Pairing requestId "11111111-1111-1111-1111-111111111111" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
1 other pending request is visible in this profile. Inspect device id and requested scopes before approving:
  openclaw devices list

Multiple other pending entries

$ openclaw devices approve 11111111-1111-1111-1111-111111111111 --json
[openclaw] Reason: Pairing requestId "11111111-1111-1111-1111-111111111111" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
4 other pending requests are visible in this profile. Inspect device id and requested scopes before approving:
  openclaw devices list

Tests

Extends src/cli/devices-cli.test.ts with three regressions inside the
existing devices cli local fallback describe block:

  • "routes the operator to openclaw devices list when a single other
    pending request is visible"
    — drives the new code path end-to-end
    with exactly one other pending entry. Both device.pair.list (the
    precheck scope resolver) and device.pair.approve reject with a
    bare pairing required message (no requestId),
    approveDevicePairing returns undefined for the supplied id, and
    a freshly mocked listDevicePairing is the only way to surface the
    alternative. Asserts the rejected error contains the
    1 other pending request is visible line + the openclaw devices list
    routing, and asserts the error does NOT contain a Rerun: line.
  • "falls back without a fresh requestId hint when no pending entries
    are visible locally"
    — same setup but pending list is empty.
    Asserts the error switches to No other pending requests are visible.
  • "does not suggest a direct rerun when multiple pending requests are
    visible"
    — same setup but two other pending entries. Asserts the
    error routes to openclaw devices list AND does not contain a
    Rerun: line.
$ pnpm test src/cli/devices-cli.test.ts
Test Files  1 passed (1)
Tests       32 passed (32)

$ pnpm check:changed
[exit 0]

$ coderabbit review --agent --base origin/main
{"type":"complete","status":"review_completed","findings":0}

No changes to CHANGELOG.md per contributor convention.

Why the CLI layer and not the dial/reconciler

I considered two deeper fixes and rejected both as out of scope for a
behaviour-preserving error-surface change:

  1. Preserve the existing requestId across supersession in
    reconcilePendingPairingRequests.
    samePendingApprovalSnapshot
    gating refresh on exact scope-set equality is intentional — the
    replacement branch already merges scopes upward
    (src/infra/device-pairing.ts:546-558), so the new pending covers
    the user's intent; only the identifier changes. Allowing refresh on
    superset/subset relationships would change persisted-state
    semantics on every cron's connect handshake, well beyond the bug
    this addresses.
  2. Auto-redirect approval to the gateway-supplied alternative.
    approve <id> is a contract the operator typed explicitly;
    silently approving a different id under the same command would
    surprise scripts and audit trails. The CLI's existing pattern (the
    mismatched-id branch at devices-cli.ts:311-313) chooses error over
    redirect, and this PR keeps that posture. The diagnostic always
    routes through openclaw devices list so the operator chooses
    explicitly after seeing deviceId + scopes.

Why not auto-suggest the single visible pending entry

ClawSweeper flagged this on the first revision of this PR
(P2: Single pending request can be unrelated). Earlier revisions of
this fix attempted to print
Rerun: openclaw devices approve <id> whenever exactly one alternative
pending request was visible. Counter-examples:

  • TTL prune. PENDING_TTL_MS = 5 minutes. If the operator's stale
    id expired and a different agent (with a different deviceId)
    subsequently created a pending request, the local profile shows one
    pending entry — but it is not the replacement for the stale request.
  • Profile/state-dir mismatch. The CLI may be looking at a
    different OPENCLAW_STATE_DIR than the gateway. The one visible
    local pending request is local-only and unrelated to the gateway's
    current view.
  • Multi-device profile. Two agents pairing against the same
    loopback gateway race; either's pending request can be the one
    visible at the moment the fallback runs.

Routing through openclaw devices list for the operator to inspect
deviceId + requested scopes before approving is the same safety the
existing --latest/implicit-approve preview flow uses
(src/cli/devices-cli.ts:703+).

Real Behavior Proof

Behavior or issue addressed: openclaw devices approve <id> on a
loopback gateway returns the bare error unknown requestId whenever
the supplied requestId is missing from local pending.json and the
gateway's close error did not carry a requestId in its parenthetical.
The operator has no way to discover from this output that other
pending entries exist or what to do next.

Real environment tested:

  1. Original bug: live ECS Fargate container running
    ghcr.io/openclaw/openclaw:2026.5.7 on Linux x86_64, loopback
    gateway with auth.mode: "token", accessed via
    aws ecs execute-command. Captured unknown requestId from the
    patched-CLI repro path.
  2. Patched verification: built the patched CLI from this branch
    (pnpm build) and ran it against a local openclaw gateway --dev
    instance with an isolated OPENCLAW_STATE_DIR=/tmp/openclaw-pr-evidence
    on macOS arm64 (Node 22.18.0).

Steps run after the patch:

Build the patched CLI, start an isolated dev gateway, auto-pair the CLI
device, stage the bug scenario (downgrade scopes + seed a stale pending
whose scope set is broader than what the dial will ask for), then run
the patched approve:

cd ~/Projects/public/openclaw
pnpm build

OPENCLAW_STATE_DIR=/tmp/openclaw-pr-evidence \
  node ./openclaw.mjs gateway --dev --port 18789 --auth none --bind loopback &

OPENCLAW_STATE_DIR=/tmp/openclaw-pr-evidence \
  node ./openclaw.mjs devices list --token "$TOKEN" --timeout 3000

OPENCLAW_STATE_DIR=/tmp/openclaw-pr-evidence \
  node ./openclaw.mjs devices approve 11111111-1111-1111-1111-111111111111 \
    --token "$TOKEN" --timeout 3000 --json

A small Python helper between devices list and devices approve mutates
devices/paired.json (downgrade scopes to operator.read) and
devices/pending.json (insert the stale id with the broader scope set);
omitted here for brevity.

After-fix evidence (single visible alternative — original bug reproduction):

[openclaw] Reason: Pairing requestId "11111111-1111-1111-1111-111111111111" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
1 other pending request is visible in this profile. Inspect device id and requested scopes before approving:
  openclaw devices list

After-fix evidence (multi-pending case — staged with 4 fake pending entries from different deviceIds):

[openclaw] Reason: Pairing requestId "11111111-1111-1111-1111-111111111111" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
4 other pending requests are visible in this profile. Inspect device id and requested scopes before approving:
  openclaw devices list

After-fix evidence (no other pending case — also covered by unit regression):

[openclaw] Reason: Pairing requestId "<id>" is not in the local pending state.
It may have been superseded by a fresh request during the gateway dial, expired (5-minute TTL), or never been created in this profile.
No other pending requests are visible in this profile. List pending requests with: openclaw devices list

Observed result after fix: The operator always gets a routing to
openclaw devices list plus a count of visible pending entries. They
never get a direct Rerun: command that could approve an unrelated
device's request. The original ECS-Fargate bug scenario matches the
single-visible-alternative case (after --latest-preview-driven
supersession), with the operator now seeing the actionable diagnostic
instead of the dead-end unknown requestId.

What was not tested: I did not rebuild the OpenClaw container
image with this patch and redeploy it onto the original ECS Fargate
task. The local dev-gateway reproduction stages the exact failure
path (reconcilePendingPairingRequests replacing the user-supplied
pending entry during the gateway dial, then the local fallback
running with no matching id), and the patched CLI is the actual
binary built from this branch's commits. A maintainer who wants live
ECS confirmation can stage the patched image, run the two-command
repro above, and read the new error text.

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 11, 2026
@clawsweeper

clawsweeper Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

Close: current main now covers the stale devices approve recovery that this PR set out to fix, with a safer sanitized recovery message and a merged root-cause fix for subset request-id churn; the remaining branch-specific diagnostic is obsolete and would be riskier to land.

Root-cause cluster
Relationship: superseded
Canonical: #98146
Summary: This PR is superseded by merged current-main device approval recovery work; the remaining broader pairing-deadlock issue is adjacent and still separately tracked.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Review details

Best possible solution:

Keep the current-main combination of stable subset request IDs and sanitized openclaw devices list recovery, and do not merge this stale branch.

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

No. The old branch-base behavior was source-reproducible, but current main now emits a sanitized openclaw devices list recovery message instead of bare unknown requestId.

Is this the best way to solve the issue?

No. This branch was a plausible narrow fix when opened, but the best current solution is the merged main path from #98145 and #98146.

Security review:

Security review needs attention: The branch would introduce an unsanitized terminal/log diagnostic for a CLI-supplied request id in the device approval flow.

  • [medium] Unsanitized request id in approval diagnostic — src/cli/devices-cli.runtime.ts:196
    The added stale-request diagnostic interpolates staleRequestId directly into terminal-facing output; current main's superseding recovery uses sanitizeForLog for the same user-supplied value.
    Confidence: 0.93

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • RomneyDa: Authored and merged the current-main diagnostic recovery and request-id churn fixes that supersede this branch. (role: recent recovery contributor; confidence: high; commits: 9003042c5fa0, f749de4f0a6a; files: src/cli/devices-cli.runtime.ts, src/cli/devices-cli.test.ts, src/infra/device-pairing.ts)
  • hxy91819: Authored the merged same-device replacement approval recovery in the same CLI fallback and test surface. (role: adjacent approval recovery contributor; confidence: medium; commits: 6ea907cec1b3; files: src/cli/devices-cli.runtime.ts, src/cli/devices-cli.test.ts, src/infra/device-pairing-churn.test.ts)
  • vincentkoc: Authored the earlier merged device fallback state guard that touched the same devices approve fallback area before the runtime split. (role: recent CLI fallback contributor; confidence: medium; commits: 01377ddbe284; files: src/cli/devices-cli.ts, src/cli/devices-cli.test.ts)

Codex review notes: model internal, reasoning high; reviewed against 9d68f877ac3e; fix evidence: commit 9003042c5fa0, main fix timestamp 2026-07-01T05:17:42Z.

@luijoc
luijoc force-pushed the fix/devices-approve-stale-requestid-error branch from 7311275 to 1179430 Compare May 11, 2026 22:02
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 11, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 11, 2026
@luijoc
luijoc force-pushed the fix/devices-approve-stale-requestid-error branch from 1179430 to 49a2462 Compare May 11, 2026 23:08
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 11, 2026
@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed proof: sufficient ClawSweeper judged the real behavior proof convincing. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 11, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 12, 2026
@luijoc luijoc closed this May 14, 2026
@luijoc
luijoc force-pushed the fix/devices-approve-stale-requestid-error branch from 49a2462 to 83b8289 Compare May 14, 2026 14:25
@openclaw-barnacle openclaw-barnacle Bot added size: XS and removed size: S proof: supplied External PR includes structured after-fix real behavior proof. proof: sufficient ClawSweeper judged the real behavior proof convincing. labels May 14, 2026
@luijoc luijoc reopened this May 14, 2026
@luijoc luijoc changed the title fix(devices): surface fresh pending requestId when local approve fallback can't find supplied id fix(devices): always route stale-approve operator to openclaw devices list May 14, 2026
@openclaw-barnacle openclaw-barnacle Bot added the proof: supplied External PR includes structured after-fix real behavior proof. label May 14, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 14, 2026
@RomneyDa

Copy link
Copy Markdown
Member

Heads up: this PR needs to be updated against current main before the new required Dependency Guard check can pass.

@luijoc
luijoc force-pushed the fix/devices-approve-stale-requestid-error branch from 3a8b38e to ab1d5ba Compare May 29, 2026 17:15
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 29, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 29, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels May 29, 2026
@luijoc
luijoc force-pushed the fix/devices-approve-stale-requestid-error branch from ab1d5ba to 50b3b17 Compare May 29, 2026 18:29
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 29, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 29, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 14, 2026
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 22, 2026
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Jun 30, 2026
@steipete

Copy link
Copy Markdown
Contributor

Thanks for the detailed production proof. Closing as superseded. Current main fixes both sides of this failure: #98145 preserves the request ID for covered subset re-requests, and #98146 replaces the dead-end error with a sanitized openclaw devices list recovery path plus regression coverage. Both fixes are published in v2026.7.1-beta.1.

The remaining branch-only pending-count diagnostic is no longer needed and lacks current main's input sanitization. If a distinct current-release approval failure remains, please open a focused report with redacted device-request state.

@steipete steipete closed this Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants