Skip to content

fix(cli): reject an unconfigured --agent across every operator selector - #126983

Merged
steipete merged 1 commit into
mainfrom
steipete/unknown-agent-sweep
Aug 21, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/unknown-agent-sweep

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

#126954 fixed skills --agent. It turned out to be one instance of a class. Two more surfaces accepted an agent id naming nothing, and one of them wrote it to disk.

sandbox explain — exit 0, full confident report for an agent that does not exist:

$ openclaw agents list
Agents:
- main (default)

$ openclaw sandbox explain --agent nope-agent
Effective sandbox:
  agentId: nope-agent
  sessionKey: agent:nope-agent:main
  effectiveHostWorkspaceRoot: <workspace>/nope-agent      <- directory does not exist
Sandbox tool policy:
  allow (default): exec, process, read, write, edit, ...
Elevated:
  enabled: true
$ echo $?
0

approvals allowlist add — exit 0, and it persists:

$ openclaw approvals allowlist add "rm -rf /" --agent nope-agent
Writing local approvals.
$ echo $?
0
// state/openclaw.sqlite#exec_approvals_config  (agents subtree only)
"agents": { "nope-agent": { "allowlist": [ { "pattern": "rm -rf /", "id": "..." } ] } }

The operator believes they approved an exec pattern. Nothing was approved, nothing will ever read that key, and nothing told them.

Why This Change Was Made

Sweeping option("--agent" across src/cli and src/commands, each hit was classified as a selector (names the thing being operated on — must validate) or a filter (narrows a list — may legitimately return empty).

Selectors now route the explicit value through resolveConfiguredAgentId, the helper that already backs models, memory, sessions list, hooks, and every capability-cli surface:

surface before after
sandbox explain exit 0, invented report exit 1, Unknown agent id
approvals allowlist add/remove exit 0, persisted exit 1, nothing written
channels resolve accepted validated
sessions export-trajectory accepted validated
sessions archive / delete dead-end hint (below) validated
backup enable / git create / sqlite create accepted validated

Blank-only guards close the empty-shell-variable hole in hooks, sessions list/cleanup/tail/compact, migrate, and agent turns.

Filters are deliberately unchanged and were checked individually: audit, usage-cost, agent bindings, backup verify/restore, and sandbox recreate all match existing records and correctly report no matches without mutating anything. Cron is gateway-owned and already rejects an unavailable agent server-side (cron job agent is unavailable: nope-agent), so it is untouched.

The sessions archive/delete case is a dead-end, not a silent failure

It already failed visibly, but handed back a remediation that cannot run:

$ openclaw sessions archive "agent:ghost:main" --agent ghost
archive agent:ghost:main: Session not found. Run openclaw sessions list --agent ghost --json to choose a valid key.

$ openclaw sessions list --agent ghost
Unknown agent id "ghost". Run openclaw agents list to see configured agents.

Validating locally — exactly as sessions list already does in the same command family — makes that suggestion runnable. A gateway roster fetch was considered and rejected: it adds an agents.list round-trip to every archive/delete, duplicates policy the server owns, and would make the local agents list hint wrong against a remote gateway.

approvals validates against the config for its actual target: loadConfigForApprovalsTarget already fetches remote config via config.get for --gateway/--node, so a remote node is checked against its own roster, and an unavailable config fails closed before any write.

User Impact

Every operator-supplied --agent now behaves the same way across the CLI. No filter semantics change; no valid invocation changes.

Production LOC: +140/-35, net +105 across thirteen command boundaries.

Evidence

Both headline defects were reproduced against a real pnpm build dist of main in an isolated OPENCLAW_STATE_DIR + HOME before any code was written, and the persisted approvals row was read back out of SQLite to confirm the write.

Tests — all 11 touched suites, 256 passed + 1 pre-existing skip, rc=0:

exec-approvals-cli, hooks-cli.toggle, agent-via-gateway, backup-schedule, backup-sqlite, channels.resolve, export-trajectory, migrate/context, sandbox-explain, sessions-compact, sessions-lifecycle.

New coverage asserts, per surface: unknown id exits nonzero; blank id exits nonzero; for approvals, that updateExecApprovals is never called, the snapshot's agents map is unchanged, and no Writing local approvals. line is emitted; and that default, explicit *, and configured ids still work. Pre-fix, the new sandbox-explain (2) and exec-approvals (4) regressions failed as intended.

node scripts/check-changed.mjs: all gates pass. $autoreview: clean, no accepted or actionable findings, correctness 0.98.

`skills` (#126954) turned out to be one instance of a class. Two more surfaces
accepted an agent id that names nothing, and one of them wrote it to disk:

- `sandbox explain --agent nope-agent` exited 0 and printed a complete policy
  report, including `Elevated: enabled: true` and a workspace root that does
  not exist, for an agent `openclaw agents list` does not know.
- `approvals allowlist add "<pattern>" --agent nope-agent` exited 0, printed
  `Writing local approvals.`, and persisted the entry under a key nothing will
  ever read. The operator believes they approved an exec pattern; nothing was
  approved. This is the severe one: a false record of an approval.

Sweeping `option("--agent"` across the CLI found the rest. Each hit was
classified as a selector (names the thing operated on, must validate) or a
filter (narrows a list, may legitimately return empty). Selectors now route the
explicit value through `resolveConfiguredAgentId`, the helper that already backs
`models`, `memory`, `sessions list`, `hooks`, and every capability surface:
`channels resolve`, `sessions export-trajectory`, `sessions archive/delete`,
`backup enable`, `backup git create`, `backup sqlite create`. Blank-only guards
close the empty-shell-variable hole in `hooks`, `sessions` list/cleanup/tail/
compact, `migrate`, and agent turns.

Filters are deliberately unchanged: `audit`, `usage-cost`, agent bindings,
`backup verify/restore`, and `sandbox recreate` all match existing records and
correctly report no matches. Cron is gateway-owned and already rejects an
unavailable agent server-side; it is untouched.

`sessions archive/delete` was not silent -- it failed with `Session not found.
Run openclaw sessions list --agent ghost --json to choose a valid key.` But that
suggested command itself exits 1 with `Unknown agent id "ghost"`, so the
remediation handed to the operator could not run. Validating locally, exactly as
`sessions list` already does, keeps the hint runnable without adding a roster
round-trip to the gateway.

Production +140/-38.
@clawsweeper

clawsweeper Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes commands Command implementations docker Docker and sandbox tooling size: L maintainer Maintainer-authored PR labels Aug 21, 2026
@steipete
steipete marked this pull request as ready for review August 21, 2026 03:56
@steipete
steipete merged commit 46dcc57 into main Aug 21, 2026
299 checks passed
@steipete
steipete deleted the steipete/unknown-agent-sweep branch August 21, 2026 04:08
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 21, 2026
…or (openclaw#126983)

`skills` (openclaw#126954) turned out to be one instance of a class. Two more surfaces
accepted an agent id that names nothing, and one of them wrote it to disk:

- `sandbox explain --agent nope-agent` exited 0 and printed a complete policy
  report, including `Elevated: enabled: true` and a workspace root that does
  not exist, for an agent `openclaw agents list` does not know.
- `approvals allowlist add "<pattern>" --agent nope-agent` exited 0, printed
  `Writing local approvals.`, and persisted the entry under a key nothing will
  ever read. The operator believes they approved an exec pattern; nothing was
  approved. This is the severe one: a false record of an approval.

Sweeping `option("--agent"` across the CLI found the rest. Each hit was
classified as a selector (names the thing operated on, must validate) or a
filter (narrows a list, may legitimately return empty). Selectors now route the
explicit value through `resolveConfiguredAgentId`, the helper that already backs
`models`, `memory`, `sessions list`, `hooks`, and every capability surface:
`channels resolve`, `sessions export-trajectory`, `sessions archive/delete`,
`backup enable`, `backup git create`, `backup sqlite create`. Blank-only guards
close the empty-shell-variable hole in `hooks`, `sessions` list/cleanup/tail/
compact, `migrate`, and agent turns.

Filters are deliberately unchanged: `audit`, `usage-cost`, agent bindings,
`backup verify/restore`, and `sandbox recreate` all match existing records and
correctly report no matches. Cron is gateway-owned and already rejects an
unavailable agent server-side; it is untouched.

`sessions archive/delete` was not silent -- it failed with `Session not found.
Run openclaw sessions list --agent ghost --json to choose a valid key.` But that
suggested command itself exits 1 with `Unknown agent id "ghost"`, so the
remediation handed to the operator could not run. Validating locally, exactly as
`sessions list` already does, keeps the hint runnable without adding a roster
round-trip to the gateway.

Production +140/-38.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cli CLI command changes commands Command implementations docker Docker and sandbox tooling maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant