Skip to content

fix(memory): make save_compression_guidelines version bump atomic#1810

Merged
bug-ops merged 2 commits intomainfrom
compression-guidelines-atomic
Mar 15, 2026
Merged

fix(memory): make save_compression_guidelines version bump atomic#1810
bug-ops merged 2 commits intomainfrom
compression-guidelines-atomic

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Mar 15, 2026

Summary

  • Replace read-then-write version increment in save_compression_guidelines with a single atomic INSERT ... SELECT COALESCE(MAX(version), 0) + 1 statement
  • Add migration 033 with UNIQUE(version) constraint on compression_guidelines, with row-level deduplication for pre-existing corrupt data
  • Add two new tests: unique_constraint_prevents_duplicate_version and concurrent_saves_produce_unique_versions (file-backed WAL DB, 8 concurrent writers)

Closes #1799

Test plan

  • cargo nextest run --config-file .github/nextest.toml -p zeph-memory --lib -E 'test(compression_guidelines)' — 10 passed
  • cargo nextest run --config-file .github/nextest.toml --workspace --features full --lib --bins — 5789 passed
  • cargo clippy --workspace --features full -- -D warnings — clean
  • cargo +nightly fmt --check — clean

bug-ops added 2 commits March 15, 2026 04:07
)

Replace the read-then-write version increment with a single atomic
INSERT ... SELECT COALESCE(MAX(version), 0) + 1 statement. SQLite's
single-writer WAL guarantee makes this safe without an explicit
transaction; two concurrent callers can no longer both observe
version=N and both insert version=N+1.

Migration 033 adds a UNIQUE(version) constraint to the
compression_guidelines table. To handle databases that already contain
duplicate version rows from the original bug, the migration
deduplicates by keeping the row with the highest rowid per version
before applying the constraint.

Tests added:
- unique_constraint_prevents_duplicate_version: raw INSERT of a
  duplicate version must fail with a UNIQUE constraint error
- concurrent_saves_produce_unique_versions: 8 concurrent writers on a
  shared file-backed WAL database all produce distinct version numbers

Closes #1799
@github-actions github-actions bot added documentation Improvements or additions to documentation memory zeph-memory crate (SQLite) rust Rust code changes bug Something isn't working size/M Medium PR (51-200 lines) labels Mar 15, 2026
@bug-ops bug-ops enabled auto-merge (squash) March 15, 2026 03:08
@bug-ops bug-ops merged commit 3049bb1 into main Mar 15, 2026
15 checks passed
@bug-ops bug-ops deleted the compression-guidelines-atomic branch March 15, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation memory zeph-memory crate (SQLite) rust Rust code changes size/M Medium PR (51-200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(memory): save_compression_guidelines non-atomic version bump

1 participant