Repository navigation
fix(storage): repair stale drift-sample CHECK and per-row batch swallow - #3547
Conversation
|
Warning Review limit reached
Next review available in: 1 minute Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem polylogue-sd9s: DriftClassification has 4 members (unseen_shape, new_field, field_changed, known_field_unread). PR #3451 already generated the schema_drift_samples.classification CHECK from DriftClassification via literal_check, but that fix only affects a *fresh* CREATE TABLE -- an ops.db bootstrapped before #3451 keeps its stale 3-value CHECK forever, since CREATE TABLE IF NOT EXISTS never rewrites an existing table's constraints. Confirmed live: /realm/db/polylogue/ops.db still carries the pre-fix CHECK. Separately, record_schema_drift_observations_to_ops_sync wrapped its whole per-observation write loop in one try/except sqlite3.Error, so a single CHECK-rejected row (e.g. known_field_unread against a stale live CHECK, or any bad origin token) aborted the loop immediately and returned 0 -- silently discarding every other observation in the same batch, not just the bad one. Solution - bootstrap.py: add _ensure_schema_drift_samples_check, called from initialize_archive_tier's OPS branch. It inspects sqlite_master.sql for the live table; if the CHECK predates known_field_unread, drops and recreates the table from the new SCHEMA_DRIFT_SAMPLES_DDL constant (extracted from ops.py's OPS_DDL so the two can't drift apart). Losing the disposable telemetry rows on repair is accepted (ops.db is a disposable tier per CLAUDE.md). - drift_sentinel_sampling.py: catch sqlite3.IntegrityError per observation inside the loop (log at warning, not debug, since a CHECK violation here means the schema and the writer have drifted apart -- the exact defect class this sentinel exists to surface) and continue to the next observation. A non-constraint sqlite3.Error (locked db, disk full) still stops the batch as before, since each record_schema_drift_sample call is already its own commit. Verification - devtools test tests/unit/schemas/test_drift_sentinel_sampling.py tests/unit/storage/test_schema_drift_samples.py -- 15 passed, including: a reproduction of the pre-fix stale-CHECK IntegrityError, a bootstrap convergence repair test, a known_field_unread persistence regression test, and a batch-continuation test (temporarily reverting the sampling fix reproduces the bug: written==0 instead of 2). - devtools test tests/unit/storage/test_archive_tiers_ops_write.py tests/unit/cli/test_schema_drift_status.py tests/unit/daemon/test_fts_identity_convergence.py -- 24 passed. - devtools verify --quick -- exit 0 (format, lint, mypy --strict, render-all-check, layering, schema-versioning policy). Ref polylogue-sd9s Co-Authored-By: Claude <[email protected]>
f6a2148 to
1a1daf7
Compare
Summary
Repairs the last two live consequences of polylogue-sd9s: an already-bootstrapped ops.db keeping a stale 3-value
schema_drift_samples.classificationCHECK forever, and a batch-write loop that silently discarded every other observation in a batch when one row hit a CHECK violation.Problem
DriftClassification(polylogue/schemas/drift_sentinel.py:48) has 4 members:unseen_shape,new_field,field_changed,known_field_unread. PR #3451 (merged 2026-07-31, same day this bead was filed) already fixed the ops-tier DDL's hand-written 3-value CHECK by generating it fromDriftClassificationvialiteral_check— but that fix only changes the DDL text used by a freshCREATE TABLE.CREATE TABLE IF NOT EXISTSnever rewrites an existing table's constraints, so an ops.db bootstrapped before #3451 keeps rejectingknown_field_unreadforever on every reopen. Confirmed live:/realm/db/polylogue/ops.db'sschema_drift_samplestable still carries the old 3-value CHECK.Separately,
record_schema_drift_observations_to_ops_sync(polylogue/schemas/drift_sentinel_sampling.py) wrapped its whole per-observation write loop in onetry/except sqlite3.Error: return 0. Eachrecord_schema_drift_samplecall is already its own commit (with conn:), so earlier rows in a batch were safely persisted on disk — but the moment any single observation's INSERT violated a CHECK (a stale-CHECKknown_field_unreadrow, or any other constraint violation, e.g. an unrecognizedorigintoken), the function returned early withwritten=0, meaning: (a) the caller's return-count lied about what actually got written, and (b) every observation after the bad one in that same batch call was silently skipped, never attempted.Solution
polylogue/storage/sqlite/archive_tiers/ops.py: extractedSCHEMA_DRIFT_SAMPLES_DDLas its own constant (embedded intoOPS_DDL) so it can be reused for repair without a second, hand-copied definition.polylogue/storage/sqlite/archive_tiers/bootstrap.py: added_ensure_schema_drift_samples_check, called frominitialize_archive_tier'sArchiveTier.OPSbranch. It inspectssqlite_master.sqlfor the liveschema_drift_samplestable; if the CHECK predatesknown_field_unread, it drops and recreates the table fromSCHEMA_DRIFT_SAMPLES_DDL.ops.dbis a disposable tier (per CLAUDE.md), so losing the bounded telemetry rows on repair needs no migration ceremony.polylogue/schemas/drift_sentinel_sampling.py: moved the connection open/init into its own try/except (unchanged best-effort contract for a missing/unreachable ops.db), then catchsqlite3.IntegrityErrorper observation inside the loop — log atwarning(notdebug, since a CHECK violation here means the schema and the writer have drifted apart, the exact defect class this sentinel exists to surface) and continue to the next observation. A non-constraintsqlite3.Error(locked db, disk full) still stops the batch, as before.Verification
devtools test tests/unit/schemas/test_drift_sentinel_sampling.py tests/unit/storage/test_schema_drift_samples.py— 15 passed, including: a reproduction of the pre-feat(storage): wire literal_check into DDL, fix a drift CHECK gap #3451-style stale-CHECKIntegrityError, a bootstrap-convergence repair test (stale table →initialize_archive_tier→ CHECK now includesknown_field_unread, insert succeeds), aknown_field_unreadpersistence regression test, and a batch-continuation test. Anti-vacuity: temporarily reverting only thedrift_sentinel_sampling.pychange reproduces the pre-fix bug on the new test (written == 0instead of2).devtools test tests/unit/storage/test_archive_tiers_ops_write.py tests/unit/cli/test_schema_drift_status.py tests/unit/daemon/test_fts_identity_convergence.py— 24 passed.devtools verify --quick— exit 0 (format, lint, mypy --strict, render-all-check, layering, schema-versioning policy). Also ran automatically and passed via the pre-push hook.Ref polylogue-sd9s