Skip to content

fix(storage): repair stale drift-sample CHECK and per-row batch swallow - #3547

Merged
Sinity merged 1 commit into
masterfrom
feature/storage/repair-drift-check-and-batch-swallow
Aug 2, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/storage/repair-drift-check-and-batch-swallow

Conversation

@Sinity

@Sinity Sinity commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

Repairs the last two live consequences of polylogue-sd9s: an already-bootstrapped ops.db keeping a stale 3-value schema_drift_samples.classification CHECK 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 from DriftClassification via literal_check — but that fix only changes the DDL text used by a fresh CREATE TABLE. CREATE TABLE IF NOT EXISTS never rewrites an existing table's constraints, so an ops.db bootstrapped before #3451 keeps rejecting known_field_unread forever on every reopen. Confirmed live: /realm/db/polylogue/ops.db's schema_drift_samples table 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 one try/except sqlite3.Error: return 0. Each record_schema_drift_sample call 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-CHECK known_field_unread row, or any other constraint violation, e.g. an unrecognized origin token), the function returned early with written=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: extracted SCHEMA_DRIFT_SAMPLES_DDL as its own constant (embedded into OPS_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 from initialize_archive_tier's ArchiveTier.OPS branch. It inspects sqlite_master.sql for the live schema_drift_samples table; if the CHECK predates known_field_unread, it drops and recreates the table from SCHEMA_DRIFT_SAMPLES_DDL. ops.db is 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 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.

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-CHECK IntegrityError, a bootstrap-convergence repair test (stale table → initialize_archive_tier → CHECK now includes known_field_unread, insert succeeds), a known_field_unread persistence regression test, and a batch-continuation test. Anti-vacuity: temporarily reverting only the drift_sentinel_sampling.py change reproduces the pre-fix bug on the new test (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). Also ran automatically and passed via the pre-push hook.

Ref polylogue-sd9s

@coderabbitai

coderabbitai Bot commented Aug 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 1 minute

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3f5966b4-03ee-42e8-932a-c9a8047733c2

📥 Commits

Reviewing files that changed from the base of the PR and between c529ed6 and 1a1daf7.

📒 Files selected for processing (5)
  • polylogue/schemas/drift_sentinel_sampling.py
  • polylogue/storage/sqlite/archive_tiers/bootstrap.py
  • polylogue/storage/sqlite/archive_tiers/ops.py
  • tests/unit/schemas/test_drift_sentinel_sampling.py
  • tests/unit/storage/test_schema_drift_samples.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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]>
@Sinity
Sinity force-pushed the feature/storage/repair-drift-check-and-batch-swallow branch from f6a2148 to 1a1daf7 Compare August 2, 2026 12:00
@Sinity
Sinity merged commit 9032b4b into master Aug 2, 2026
3 checks passed
@Sinity
Sinity deleted the feature/storage/repair-drift-check-and-batch-swallow branch August 2, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant