Repository navigation
perf(gateway): serve failed-delivery health counts from a status-first index - #164131
Conversation
Use the existing canonical-index repair owner for the same-version status-first index. Preserve delivery rows, result ordering, and the worker query while covering populated upgrade and rollback. Health polling no longer depends on planner statistics to select the failed range.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 3, 2026, 4:06 AM ET / 08:06 UTC (Revision 3). ClawSweeper reviewWhat this changesThe PR reorders the existing delivery-failure SQLite index so Gateway health counts can select failed entries directly, and adds query-plan, upgrade, and rollback coverage. Merge readiness✅ Ready for maintainer review The PR remains useful: fetched main still uses the queue-first index. No actionable introduced defect was found, and the documented storage tradeoff has compatibility coverage. Likely related people: steipete (high-confidence routing based on recent merged database-admission work). Priority: P2 Review scores
Verification
How this fits togetherGateway health reporting reads failed-message counts from the shared delivery queue through a database worker. Writable database admission maintains the derived index used to group those counts by queue. flowchart TD
A[Delivery queue rows] --> B[Writable database admission]
B --> C[Canonical index repair]
C --> D[Status-first failure index]
E[Gateway health poll] --> F[Database worker count]
D --> F
F --> G[Failure counts by queue]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource 0, Tests +150, Docs +14. Total +164 across 6 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one canonical admission owner for the replacement index while preserving delivery rows, count semantics, worker execution, and the documented rollback contract. Do we have a high-confidence way to reproduce the issue? Yes, the production status-only count and queue-first main index establish the performance mechanism, and the PR reports an intended pre-fix query-plan failure. This review inspected source and coverage without independently executing it. Is this the best way to solve the issue? Yes, reordering the existing derived index is a focused solution that preserves the query and worker boundary while retaining queue-scoped ordering. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 7c53de2d0777. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
History |
Preserve both failed-delivery and meeting-caption index documentation while integrating main 46fc2e3. The delivery-index product delta is unchanged. All five focused suites pass (149 tests, one Linux-only skip); docs validation and independent review through P2 pass.
|
Merged via squash.
|
Use the existing canonical-index repair owner for the same-version status-first index. Preserve delivery rows, result ordering, and the worker query while covering populated upgrade and rollback. Health polling no longer depends on planner statistics to select the failed range.
What Problem This Solves
Routine Gateway health polls can scan completed and pending delivery history to count failures when SQLite has no planner statistics.
User Impact
Dead-letter health counts select the failed range directly, with unchanged results and ordering. Existing databases incur a one-time index rebuild during writable admission; large histories require startup I/O and temporary disk space.
Why This Change Was Made
Reorder the existing
idx_delivery_queue_failedto(status, queue_name, failed_at, id). This keeps the query, store API, worker execution, and steady-state index count unchanged while removing dependence on statistics-driven skip-scan selection.Storage design: The shared-state delivery queue owns canonical
delivery_queue_entriesrows; the nonunique index is derived. This accepted index-only design keeps the schema version unchanged. The existing canonical-index admission owner detects the same-name mismatch, builds a probe and replacement atomically, and retains its integrity validation. No per-request repair is added. Older same-version writable owners can restore their queue-first definition on downgrade or binary rollback without changing rows; strict read-only validation may refuse until that writable repair occurs. Index construction and temporary space scale with retained queue history, and existing database integrity checks remain in force. Row formats, durability, retention, permissions, and configuration are unchanged. No schema-version bump is required.The existing plugin-index repair regression moves to a focused sibling suite, sharing its template with the delivery case rather than growing the oversized state-database test file.
Evidence
The regression captures SQL from the real count kernel. Before the index change it failed on
SCAN ... USING COVERING INDEX; afterward both no-statistics andANALYZEcases selectSEARCH ... (status=?), without a temporary B-tree. Queue-scoped timestamp/ID reads retain their covering search and ordering. Mixed pending/completed/failed rows, nullable failure timestamps, sorted groups, and minima preserve the result contract. Statements per admitted count operation: 1 SELECT before → 1 SELECT after; no new main-thread SQLite access.The populated existing-database fixture proves runtime upgrade, unchanged row values, including JSON bytes, unchanged version metadata, a second reopen without further DDL, and rollback through the same repair owner using the older schema contract. It does not claim an older binary was booted.
Component micro-benchmark: 100,000 completed rows, 100 failed rows, ten queues; median of nine rounds of forty reads after warmup. Results serialize identically.
These measure the SQLite component, not end-to-end health latency. Analyzed databases already benefit from skip-scan; the fresh analyzed comparison was effectively flat. The gain is predictable failed-range selection without statistics. Regression assertions use query plans and statement counts, never timing thresholds.
The five suites below ran on the product candidate before main integration; the affected migration proof was refreshed afterward as described below.
pnpm test src/state/sqlite-query-plan.test.ts --maxWorkers=1pnpm test src/state/openclaw-state-db.index-repair.test.ts --maxWorkers=1pnpm test src/infra/delivery-queue-sqlite.test.ts --maxWorkers=1pnpm test src/infra/delivery-queue-terminal.test.ts --maxWorkers=1pnpm test src/state/openclaw-state-db.test.ts --maxWorkers=1Wall times include repository subprocess compilation and source verification on a shared host. The new query-plan case took 0.813 s; the delivery upgrade/rollback case took 0.506 s. The first pre-fix attempt failed before tests because its artifact inventory overlapped gateway setup; after setup completed, the pre-fix regression failed at the intended scan assertion (266.63 s command wall).
The terminal-pruning suite initially failed because an existing EQP assertion required queue-first equality-term order (132.79 s command wall). Its assertion now accepts either ordering of the same two equality terms while retaining the indexed-search and no-table-scan requirements; row-retention assertions remain unchanged.
Formatting, targeted oxlint, docs MDX validation,
git diff --check, assertion-safety, max-lines, line-cap, and database-worker ratchets passed. Codex autoreview: scoped-clean through P2, no actionable findings.LOC against the integrated main base: production and all non-test/non-doc files +1/-1 (net 0); tests +203/-53 (net +150); docs +14/-0. CI job seconds remain unavailable while CI is pending; landing is separate.
Current main was integrated to resolve the environment-budget conflict; its budget file is unchanged in this PR. The count query and index definition are unchanged from the tested candidate. Main also optimized canonical-index inspection, so the populated index-repair suite was rerun against the integrated owner: 2 passed, 117.36 s command wall (delivery case 0.174 s). The final diff was independently reviewed against that pinned main base.
Landing integration
After the first merge admission, main advanced and conflicted only in
docs/reference/database-schemas/versioning.md. Merge commitfdaf415c3c4ace5f50e479c9229cc77f89e61bedintegrates main46fc2e34f2f2f980f1b496f959df28d1925b7ddb, preserving both the failed-delivery and meeting-caption index documentation. The six-file PR delta against that integrated main is unchanged.Refreshed all five focused suites in one invocation:
pnpm test src/state/sqlite-query-plan.test.ts src/state/openclaw-state-db.index-repair.test.ts src/infra/delivery-queue-sqlite.test.ts src/infra/delivery-queue-terminal.test.ts src/state/openclaw-state-db.test.ts --maxWorkers=1Result: 149 passed, 1 Linux-only skipped; 211.91 s command wall time (210.28 s runner total). Docs MDX validation and
git diff --checkpassed. Fresh Codex autoreview of the integrated six-file candidate was scoped-clean through P2. Previous-head CI run37106670659completed successfully; it remains prior-head evidence, with current-head GitHub gates enforced during landing. No CI rerun was requested.