Skip to content

refactor(state): split openclaw-state-db into focused modules under the line limit - #110666

Merged
steipete merged 1 commit into
mainfrom
claude/state-db-split
Jul 18, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/state-db-split

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

src/state/openclaw-state-db.ts was ~1940 lines with a grandfathered max-lines suppression - the largest state module and the one every schema-safety change this week had to wade through. Third of the four oversized modules being retired (after #110550, #110664).

Why This Change Was Made

Behavior-neutral decomposition: the entry module (openclaw-state-db.ts, 440 lines) keeps the open/close lifecycle, cache/latch wiring, schema orchestration, and writes, with its exported surface verified unchanged (19/19 names) so no importer moves. Extracted siblings: contracts/constants (32), metadata + schema-version + maintenance validation (120), permissions (54), audit-schema detection/repair (342), legacy data backfills (549), additive columns (229), migration detection/schema repair (153), and the startup-checkpoint database (80). Suppression and baseline entry removed; raw-SQLite modules joined the existing kysely-guardrails lifecycle allowlist group (moved code, same justification).

User Impact

None - internal module layout only.

Evidence

  • Full state/doctor suite sweep green: node scripts/run-vitest.mjs src/state/openclaw-state-db.test.ts src/state/openclaw-agent-db.test.ts src/state/openclaw-database-verify.test.ts src/state/openclaw-database-preflight.test.ts src/state/openclaw-database-maintenance.test.ts src/commands/doctor-state-sqlite-compact.test.ts (154 tests; two multi-process barrier tests initially starved under a saturated machine and pass reliably solo and on a quiet box - load sensitivity, not regression).
  • Kysely guardrails, max-lines ratchet, deadcode exports (0), import cycles (0), formatting, git diff --check: green. pnpm check:test-types clean after rebasing past the transient audit-extra.async.ts breakage on main (fix(security): restore fs import dropped by bounded manifest-read change #110585/fix(security): bound skill-file audit read (restore removed fs dependency) #110589).
  • Structured Codex autoreview (gpt-5.6-sol, xhigh): clean, no actionable findings.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts size: XL maintainer Maintainer-authored PR labels Jul 18, 2026
@steipete steipete self-assigned this Jul 18, 2026
@steipete
steipete force-pushed the claude/state-db-split branch from ee91796 to 737c1c9 Compare July 18, 2026 11:49
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. labels Jul 18, 2026
@clawsweeper

clawsweeper Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 18, 2026, 8:33 AM ET / 12:33 UTC.

Summary
The branch splits src/state/openclaw-state-db.ts into focused contracts, migration, maintenance, permission, schema-repair, backfill, additive-column, and startup-checkpoint modules while retaining the public entry module and removing its max-lines baseline exemption.

PR surface: Source +117, Other +5. Total +122 across 11 files.

Reproducibility: not applicable. as a bug reproduction: this PR claims a behavior-neutral refactor. The relevant high-confidence validation is a real fresh-open and existing-database reopen/upgrade run, which has not been supplied.

Review metrics: none identified.

Stored data model
Persistent data-model change detected: database schema: src/state/openclaw-state-db-audit-migration.ts, database schema: src/state/openclaw-state-db-schema-additive.ts, database schema: src/state/openclaw-state-db-schema-repair.ts, database schema: src/state/openclaw-state-db-startup-checkpoint.ts, migration/backfill/repair: scripts/check-kysely-guardrails.mjs, migration/backfill/repair: src/state/openclaw-state-db-legacy-backfills.ts, and 20 more. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add redacted terminal or runtime-log evidence for a fresh shared-state database initialization.
  • [P1] Add redacted terminal or runtime-log evidence for reopening/upgrading an existing shared-state database.
  • Have the state-database owner review that proof and the extracted migration ordering.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body reports focused tests, guardrails, type checks, formatting, and CI, but contains no after-fix real runtime proof for fresh state-database initialization and existing-database reopen/upgrade. Add redacted terminal output or runtime logs; redact private paths, IP addresses, endpoints, and credentials. Updating the PR body should trigger re-review, or a maintainer can request @clawsweeper re-review. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Moving schema-open, migration, repair, and checkpoint ordering can break an existing persisted state database or startup even when the exported entry-module names and focused tests remain unchanged.
  • [P1] The PR has no after-fix real runtime evidence for both a fresh database and a pre-existing database reopen/upgrade path, leaving the upgrade-safety claim unresolved before merge.

Maintainer options:

  1. Prove fresh and upgrade database behavior (recommended)
    Before merge, add redacted after-fix terminal output or runtime logs for fresh initialization and an existing shared-state database reopen/upgrade, then let the state owner review the evidence.
  2. Accept the refactor on static evidence
    A state owner may intentionally accept the remaining upgrade-risk based on the focused suite and CI, but that leaves direct runtime equivalence unproven.
  3. Pause until state-owner capacity is available
    Defer the split if its readability benefit does not justify immediate review of the migration and startup boundary.

Next step before merge

  • [P1] The remaining merge gate is contributor-supplied real runtime proof and protected state-owner judgment, not a narrow mechanical repair ClawSweeper can safely make.

Maintainer decision needed

  • Question: Does the state-database owner accept this behavior-neutral decomposition after reviewing fresh-install and existing-database upgrade/open proof?
  • Rationale: The patch rearranges the compatibility-sensitive persistence and schema-repair boundary; tests and static checks do not by themselves establish the real startup and upgrade behavior required for this protected PR.
  • Likely owner: steipete — steipete is the assigned author of the proposed state-database decomposition and is the closest available routing candidate in the supplied history.
  • Options:
    • Require runtime proof, then approve (recommended): Ask for redacted terminal output or runtime logs showing a fresh database open plus an existing database reopen/upgrade before accepting the refactor.
    • Pause the refactor: Keep the current monolith until the state owner can allocate review time for migration-order equivalence.

Security
Cleared: The diff changes internal TypeScript state and guardrail code only; it introduces no dependency source, workflow, package-resolution, secret-handling, or supply-chain execution surface.

Review details

Best possible solution:

Keep one public state-database facade, land the focused modules only after redacted runtime proof shows successful fresh initialization and successful reopen/upgrade of an existing state database, then obtain the state owner’s approval for the compatibility-sensitive move.

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

Not applicable as a bug reproduction: this PR claims a behavior-neutral refactor. The relevant high-confidence validation is a real fresh-open and existing-database reopen/upgrade run, which has not been supplied.

Is this the best way to solve the issue?

Unclear: splitting the oversized module into ownership-focused siblings is a reasonable maintainability direction, but it is not yet proven to be the best safe implementation until fresh and upgrade runtime behavior is demonstrated.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against af2662f25a85.

Label changes

Label changes:

  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • remove rating: 🦪 silver shellfish: Current PR rating is rating: 🧂 unranked krab, so this older rating label is no longer current.

Label justifications:

  • P2: This is an internal cleanup of shared persisted-state opening and migration code with bounded but non-trivial user impact if equivalence regresses.
  • merge-risk: 🚨 compatibility: The moved code validates, repairs, and migrates existing shared SQLite database schemas during open.
  • merge-risk: 🚨 session-state: The shared state database contains session-related migrations and backfills, so changed sequencing could affect persisted session data.
  • merge-risk: 🚨 availability: Database open, permissions, validation, and startup checkpoint paths can prevent gateway startup if the refactor changes their behavior.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body reports focused tests, guardrails, type checks, formatting, and CI, but contains no after-fix real runtime proof for fresh state-database initialization and existing-database reopen/upgrade. Add redacted terminal output or runtime logs; redact private paths, IP addresses, endpoints, and credentials. Updating the PR body should trigger re-review, or a maintainer can request @clawsweeper re-review. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +117, Other +5. Total +122 across 11 files.

View PR surface stats
Area Files Added Removed Net
Source 9 1631 1514 +117
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 2 6 1 +5
Total 11 1637 1515 +122

What I checked:

  • Persisted-state lifecycle remains the central changed surface: The PR retains the state database entry module but moves audit repair, legacy backfills, additive schema work, maintenance/version validation, permissions, schema repair, and startup checkpoints into new siblings; these paths govern opening and upgrading the shared SQLite state database. (src/state/openclaw-state-db.ts:1, 7fc80095729d)
  • Compatibility-sensitive migration code was extracted: The new repair module retains legacy-table removal, schema detection, and state-database migration decisions, so equivalence needs proof against both fresh and existing state databases rather than export-surface checking alone. (src/state/openclaw-state-db-schema-repair.ts:17, 7fc80095729d)
  • Existing review gate remains unmet: The prior ClawSweeper review requested after-fix runtime evidence for a fresh state database and an existing-database reopen/upgrade path. The current PR body still provides focused tests, guardrails, types, formatting, and CI claims, but no redacted terminal output, runtime logs, recording, or linked live artifact for those paths. (7fc80095729d)
  • Protected maintainer handling applies: The PR carries the maintainer label and is assigned, so conservative cleanup must leave it open for explicit human handling rather than close or dispatch an automated repair. (7fc80095729d)

Likely related people:

  • steipete: The PR is assigned to steipete, who authored this state refactor and the related merged oversized-module refactors cited in the PR body; the available review context does not provide a separate current-main blame trail for the state module. (role: assigned recent refactor contributor; confidence: low; commits: 7fc80095729d, 0d91b07f5bea, f6de912efec8; files: src/state/openclaw-state-db.ts, src/state/openclaw-state-db-schema-repair.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (2 earlier review cycles)
  • reviewed 2026-07-18T11:53:14.967Z sha 737c1c942d239bc6704bd2af5aa8dac0579956d2 :: needs real behavior proof before merge. :: none
  • reviewed 2026-07-18T12:03:58.178Z sha 0ea42c4b280ef47498a68ce82dc5712e52679f57 :: needs real behavior proof before merge. :: none

@steipete
steipete force-pushed the claude/state-db-split branch from 0ea42c4 to 7fc8009 Compare July 18, 2026 12:30
@clawsweeper clawsweeper Bot added 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. labels Jul 18, 2026
@steipete
steipete merged commit e01a880 into main Jul 18, 2026
127 of 129 checks passed
@steipete
steipete deleted the claude/state-db-split branch July 18, 2026 12:48
@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

maintainer Maintainer-authored PR merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. scripts Repository scripts size: XL status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant