Skip to content

fix(audit): blank --agent silently returns every agent's audit records - #193704

Closed
wangmiao0668000666 wants to merge 1 commit into
openclaw:mainfrom
wangmiao0668000666:fix/audit-blank-filters
Closed

wangmiao0668000666 wants to merge 1 commit into
openclaw:mainfrom
wangmiao0668000666:fix/audit-blank-filters

Conversation

@wangmiao0668000666

Copy link
Copy Markdown
Contributor

What Problem This Solves

openclaw audit --agent "" (or --agent "$AGENT" where the variable is empty) exits 0 and returns every agent's audit records — byte-identical to running openclaw audit with no filter at all. The same command with --agent nonexistent correctly returns an empty page, so an explicitly passed empty value is silently treated as "flag not provided" while any non-empty value is honored. --session, --run, --execution, --channel, and --cursor behave the same way: the blank filter is dropped before the query is built.

The --execution case is the sharpest: openclaw audit --execution xyz fails with --execution requires --explain., but openclaw audit --execution "" succeeds and lists activity — a blank value silently bypasses a validation that a non-blank value triggers.

Root cause: auditListCommand builds AuditActivityListParams with truthy spreads (...(options.agentId ? { agentId: options.agentId } : {}) and the same for sessionKey, runId, executionId, channel, cursor), so "" never reaches the Gateway as a filter. The same function already rejects blank --after / --before / --kind / --status / --direction (bounds added in #137335), so the free-string filters are the inconsistent half.

User Impact

User impact: an operator who scopes an audit query with an empty variable now gets --agent must not be blank and exit 1 instead of a successful report that silently widens the query to all agents, sessions, runs, and channels — before any Gateway call is made. Non-empty values, omitted flags, and every other audit path behave exactly as before.

Why This Change Was Made

Adds a 13-line guard at the top of auditListCommand that mirrors the repository's existing agentCliCommand loop in src/commands/agent-via-gateway.ts (["--agent", opts.agent], … → ${flag} must not be blank), covering the six free-string list filters. It runs before the --explain branch and before params is constructed, so a present blank filter can never be normalized away.

Not introducing: no new error type, no config validation, no Gateway/protocol change, no new owner or export, no schema/API change. The --limit blank path keeps its existing 1 and 500 rejection, and --kind/--status/--direction/--after/--before keep their existing validators — this change only closes the free-string half of the same contract.

Evidence

Base 37706a74a81129f8ec85289399a2703abf98c2b4 (branch base = origin/main at branch creation) → Candidate daba319ff1d884e144c71126b57548e85023e341 (branch fix/audit-blank-filters).

Environment: Linux x64, Node v24.22.1, normally built openclaw.mjs (OPENCLAW_RUN_NODE_SKIP_DTS_BUILD=1 pnpm build:docker), isolated OPENCLAW_HOME + OPENCLAW_STATE_DIR + isolated HOME, and a dedicated isolated Gateway started by this proof (openclaw gateway run --port 18999 --allow-unconfigured) so audit has something to query. The isolated ledger contains exactly one real record, written by the Gateway itself:

TIME                         KIND       DIRECTION  CHANNEL  STATUS     AGENT  RUN               ACTION
2026-11-05T19:00:08.706Z     agent_run  -          -        succeeded  main   d268d961-0f0f-41e… agent.run.finished

That record makes the byte-identity comparisons non-trivial: a dropped filter has to return it, and an honored filter has to hide it. Every case below was run twice and compared with cmp (exit code + stdout + stderr), not by eye.

Shipped CLI (each leg run at least twice)

case base candidate
audit (no filter) ×2 exit 0, header + the record above exit 0, byte-identical to base
audit --agent "" ×2 exit 0, byte-identical to audit — the blank filter is dropped and main's record is returned although a specific agent was requested exit 1, stderr --agent must not be blank
audit --agent nonexistent ×2 exit 0, header only (a real value is honored) exit 0, byte-identical to base
audit --execution "" ×2 exit 0, returns the record (blank dropped; should have failed the --explain rule) exit 1, stderr --execution must not be blank
audit --execution xyz ×2 exit 1, stderr --execution requires --explain. exit 1, byte-identical to base
audit --cursor "" ×2 exit 0, byte-identical to audit (blank cursor dropped) exit 1, stderr --cursor must not be blank
audit --session "" (candidate spot check) ×2 — (covered by the unit test below) exit 1, stderr --session must not be blank

The decisive pair: on base, audit --agent "" returns the record and audit --agent nonexistent does not — the same query with an empty value is wider than the query with an explicit non-existent value.

Regression RED → GREEN (remote, real vitest)

Remote proof host (Docker lexiflow-all:latest, Node v24.22.1, OPENCLAW_VITEST_RUNTIME=node); src/commands/audit.ts and src/commands/audit.test.ts verified md5-identical to git show origin/main:<path> before the RED run:

sh node_modules/.bin/vitest run --config test/vitest/vitest.commands.config.ts \
  src/commands/audit.test.ts --maxWorkers=1
  • base production + extended test → RED: 6 failed | 25 passed (31). The six failures are exactly the new cases — rejects invalid --agent / --session / --run / --execution / --channel / --cursor before calling the Gateway — each with AssertionError: promise resolved "undefined" instead of rejecting at src/commands/audit.test.ts:76.
  • candidate production + the same test → GREEN: 31 passed (31).
  • Sibling on the candidate: src/gateway/server-methods/audit.test.ts (root matrix) → 1 passed (1) file, 17 passed (17). auditListCommand's only production caller is src/cli/program/register.audit.ts, which has no dedicated test file, and no other test file imports auditListCommand (grep over origin/main).

Local gates

oxlint (v1.88.0) src/commands/audit.ts src/commands/audit.test.ts → 0 findings, exit 0
node scripts/run-format.mts --check <both files> → All matched files use the correct format
node scripts/check-test-lines-ratchet.mts --base 37706a74a81 → Test lines OK: 6496710; ceiling: 6496752; slack: 42
git diff --check → clean

Line counts

Whole-PR delta: production +13/-0 (1 file), tests +6/-0 (1 file), config/test-lines-allocations/audit-blank-filters.txt = 6 for exactly this PR's net test growth. Shared scalar config/test-lines-baseline.txt untouched. On a clean 37706a74a81 the ratchet reports Test lines OK: 6496704; ceiling: 6496746; slack: 42, so this branch stays inside the ceiling with the allocation applied.

Negative claims

  • Not claimed: --limit "" (already rejected by the existing 1 and 500 bound), --kind/--status/--direction/--after/--before (already validated by fix(cli): reject blank audit bounds #137335 and untouched here).
  • Not claimed: records for a second agent or a non-default channel — the isolated ledger holds one agent_run for main, so the widening is proven against that single record, not against multi-tenant data.
  • Not claimed: audit --explain behavior for blank selectors beyond the guard itself; the guard runs before that branch, but the proof exercises the activity-list path.

Focused Checks and Simplification

  • 13 production lines, 1 production file; the guard is a straight-line loop at the existing command entry, lifted from the in-repo agentCliCommand precedent — no helper, no wrapper, no new export.
  • Test merged into the file that already owns the contract (audit.test.ts) by extending the existing rejects invalid $flag before calling the Gateway it.each, so the new cases reuse the existing callGateway-not-called assertion instead of adding a new harness.
  • Test-lines: this PR adds an allocation of exactly its +6 growth and leaves the shared scalar alone.

Compatibility and Merge Risk

Two-way door: reverting restores the previous silent fallback without migrations or persisted state changes. Blast radius is auditListCommand's validation of six free-string filters; the guard runs before any option is consumed and before callGateway, so no Gateway request, --explain branch, or output path changes for non-blank input. Unchanged adjacent behavior: omitted flags, non-empty valid values, non-empty unknown values (still an empty page), --limit, all enum/timestamp filters, --json, and every non-audit command.

Collision check (run against origin/main minutes before opening): the only open PR that touches these two files is #141004 (feat(audit): record observed runtime skill usage, different purpose). Its src/commands/audit.ts hunks sit at lines 63–234 (record type, row rendering, filter helpers, legacy-param mapping) and never reach auditListCommand at line 506, so this PR's guard does not overlap them. In src/commands/audit.test.ts both PRs append cases to the same rejects invalid $flag before calling the Gateway it.each array — adjacent-line additions that resolve trivially on rebase, with no shared semantic change. Every other candidate from the per-file gh api pulls/<n>/files sweep (#181303, #193525, #191070, #184328) is unrelated to blank filters, and merged #137335 covered only the timestamp/enum bounds.

@clawsweeper

clawsweeper Bot commented Nov 5, 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.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations size: XS labels Nov 5, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 5, 2026
@clawsweeper

clawsweeper Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Reject explicitly blank audit selectors and cursors before they can silently become omitted query filters.

Example: Run openclaw audit --agent "" against a ledger containing a successful run for agent main.

  • Before: The command succeeds and returns the main record as an unfiltered query.
  • After: The command exits with an error saying --agent must not be blank before contacting the Gateway.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A small, complete bug fix with relevant shipped-CLI proof, meaningful regression coverage, passing exact-head gates, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (terminal): The supplied pinned base/head terminal results exercise the built audit CLI against an isolated Gateway with a real record, demonstrate blank-filter rejection after the fix, and preserve omitted and nonblank controls; supporting regression results report six base failures and a passing candidate suite.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: An explicitly empty audit filter can produce a successful report containing activity outside the requested selection.
Reason: The change completes the existing strict-input contract without adding configuration, changing valid queries, or expanding audit collection or reader permissions.

Merge readiness

✅ Ready for maintainer review

Keep this PR: it fixes a current audit-query bug with a focused change, and no actionable correctness or security findings remain.

Priority: P2
Reviewed head: daba319ff1d884e144c71126b57548e85023e341

Before merge

None.

Findings

None.

Agent review details

How this fits together

The audit CLI maps operator flags into bounded Gateway queries, which read retained audit metadata and return text or JSON pages.

flowchart LR
  A[Operator flags] --> B[Audit CLI registration]
  B --> C[Audit command validation]
  C --> D[Gateway audit RPC]
  D --> E[Audit ledger]
  E --> F[Text or JSON page]
Loading

Technical review

Best possible solution:

Retain this focused validation at the existing audit command owner and proceed through the normal landing workflow.

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

The current-main truthy parameter construction establishes the failure directly, and the contributor supplies a pinned built-CLI reproduction with a real Gateway record; this review executed no target code.

Is this the best way to solve the issue?

Yes: explicit blank input must fail before normalization can erase it, and the smallest complete fix is the proposed loop at auditListCommand with regression coverage in its existing suite.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 3543fda490ec.

Provenance checked

  • Audit activity query construction keeps the original intent (f533469: The original audit feature established bounded, metadata-only filtered queries for authorized operators; the new guard preserves that purpose.)
  • Execution identity inspection keeps the original intent (618fba9: Exact execution selection and bounded run discovery avoid silently selecting the wrong execution; rejecting blank selectors supports that invariant.)
  • Explicit blank audit input keeps the original intent (d30daf5: The prior fix intentionally rejected present-but-empty bounds instead of treating them as omitted values.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit f533469 adds src/commands/audit.ts:87 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: f53346944db0; files: src/commands/audit.ts)
  • Josh Avant: Raw commit 618fba9 adds src/commands/audit.ts:456 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 618fba92bf01; files: src/commands/audit.ts)
  • Alix-007: Raw commit d30daf5 adds src/commands/audit.ts:70 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: d30daf549693; files: src/commands/audit.ts)

PR surface

Source +13, Tests +6, Other +2. Total +21 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 13 0 +13
Tests 1 6 0 +6
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 1 2 0 +2
Total 3 21 0 +21

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Review metrics

Metric Value Why it matters
Production versus regression growth Production +13/-0; tests +6/-0 The growth is justified: one command-boundary loop covers six independently supplied options, with regression cases extending the existing table.

Labels

Label changes:

  • add P2: Empty shell-expanded selectors can silently broaden an operator's audit report; the impact is limited to this advanced CLI query workflow.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Empty shell-expanded selectors can silently broaden an operator's audit report; the impact is limited to this advanced CLI query workflow.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 5, 2026, 3:04 PM ET / 20:04 UTC.

@wangmiao0668000666

Copy link
Copy Markdown
Contributor Author

Closing to free PR quota; audit blank-filter fix is superseded in priority by the same-blank-input family already landed (#198965). Branch + commits retained for revival when maintainer picks up the audit blank-input area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands Command implementations P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant