Skip to content

refactor(cli): read-only database access for pure-read commands - #110732

Merged
steipete merged 1 commit into
mainfrom
claude/cli-readonly-reads
Jul 18, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/cli-readonly-reads

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Corruption incident #101290 was triggered by CLI one-shot processes opening the live databases writable alongside the running gateway - and a survey showed how broad that surface is: even pure-read commands join the full writable lifecycle (journal recovery, WAL setup, permissions repair, and a shared-DB agent_databases registration on every agent open). There was a read-only helper for the state DB but none for agent DBs.

Why This Change Was Made

First, deliberately narrow tranche of the read-only program - pure reads only, zero product-behavior change:

  • New withOpenClawAgentDatabaseReadOnly (src/state/openclaw-agent-db-readonly.ts): read-only connection, newer-schema gate, owner check, typed not-found for missing databases; never creates, registers, migrates, or repairs. Aligned with the just-landed agent-db module split (imports its schema helpers rather than duplicating).
  • Converted verified SELECT-only paths: sessions export-trajectory and sandbox explain session loads, fleet list/status/doctor, onboard recommendations, separable sandbox list registry reads, and the TUI remembered-session startup read (its writer is untouched).
  • Regression proof that reads stay reads: a converted read leaves agent_databases without a new row.

Deliberately out of scope (maintainer product decisions, documented in the survey): status/health aggregation via RPC, config-health observation defaults, maintenance-on-read commands (pairing list, commitments list, worktrees list), and cache-writing list commands (promotions, model catalog, marketplace).

User Impact

Read-only commands no longer take write locks or mutate shared state as a side effect - fresh installs and concurrent-gateway scenarios behave identically, with less exposure to the #101290 corruption class.

Evidence

  • 191/191 tests green across the eight touched suites (Blacksmith Testbox run 29647585830), including: readonly newer-schema refusal, missing-database not-found, and the no-registration regression test.
  • Deadcode exports, kysely guardrails (new helper allowlisted in the read-only group), pnpm check:test-types, git diff --check: green.
  • Structured Codex autoreview: clean (one narrow finding rejected with rationale - agent-id normalization before path resolution mirrors the writable open).

@steipete
steipete requested a review from a team as a code owner July 18, 2026 14:29
@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts commands Command implementations docker Docker and sandbox tooling agents Agent runtime and tooling fleet size: M maintainer Maintainer-authored PR labels Jul 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d1413f985

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -122,7 +122,8 @@ export async function exportTrajectoryCommand(
const storePath = resolvedOpts.store
? resolveStorePath(resolvedOpts.store, { agentId: targetAgentId })
: resolveStorePath(getRuntimeConfig().session?.store, { agentId: targetAgentId });
const entry = loadSessionEntry({
// CLI reads must not join the Gateway's writable SQLite lifecycle (#101290).
const entry = loadSessionEntryReadOnly({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Finish making trajectory export read-only

When exporting a SQLite-backed trajectory, this read-only lookup is followed by exportTrajectoryForCommand, whose SQLite path calls loadTranscriptEvents (src/trajectory/export.ts:202) and loadSqliteTrajectoryRuntimeEvents (src/trajectory/export.ts:366); those still reach openOpenClawAgentDatabase in src/config/sessions/session-accessor.sqlite-read.ts:45 and src/trajectory/runtime-store.sqlite.ts:98, which registers the agent DB and joins the writable lifecycle. So openclaw sessions export-trajectory still mutates agent_databases/takes write-side SQLite setup in the exact corruption-avoidance scenario this comment claims is converted; either keep this command out of the read-only tranche or add read-only transcript/runtime readers before switching it.

Useful? React with 👍 / 👎.

@steipete
steipete force-pushed the claude/cli-readonly-reads branch from 4d1413f to 45ae836 Compare July 18, 2026 14:36
@steipete steipete self-assigned this Jul 18, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 45ae83668d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +43 to +44
const sqlite = requireNodeSqlite();
const db = new sqlite.DatabaseSync(pathname, { readOnly: true });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refuse quarantined agent DBs before read-only opens

When an agent DB has already been quarantined by integrity verification, this helper still opens it read-only and executes the caller's SELECTs; unlike openOpenClawAgentDatabase (src/state/openclaw-agent-db.ts:166) it never consults readOpenClawDatabaseQuarantine, and verification persists those rows in src/state/openclaw-database-verify.impl.ts:167. The converted loadSessionEntryReadOnly commands can therefore export/explain data from a known corrupt database instead of failing closed with doctor guidance; add the same quarantine gate before constructing the read-only handle.

Useful? React with 👍 / 👎.

Comment thread src/fleet/registry.ts
return [];
}
// CLI reads must not join the Gateway's writable SQLite lifecycle (#101290).
return withOpenClawStateDatabaseReadOnly(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep state quarantine on read-only fleet reads

After this conversion, fleet list/status/doctor reaches the shared DB through withOpenClawStateDatabaseReadOnly; that helper opens the file directly, so a shared-state quarantine row that openOpenClawStateDatabase would refuse at src/state/openclaw-state-db.ts:339 is bypassed. In the scenario where verification has quarantined state/openclaw.sqlite, these read commands still query a known corrupt DB and can report bogus cells; gate the read-only state path on the persisted quarantine before querying.

Useful? React with 👍 / 👎.

@steipete
steipete merged commit d8846a1 into main Jul 18, 2026
119 checks passed
@steipete
steipete deleted the claude/cli-readonly-reads branch July 18, 2026 14:42
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

agents Agent runtime and tooling commands Command implementations docker Docker and sandbox tooling fleet maintainer Maintainer-authored PR scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant