Skip to content

fix: leave the transient SQLite SHM out of the replica signature - #65

Merged
twoimo merged 2 commits into
mainfrom
fix/replica-shm-signature
Nov 23, 2026
Merged

twoimo merged 2 commits into
mainfrom
fix/replica-shm-signature

Conversation

@twoimo

@twoimo twoimo commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Why

Every open PR in twoimo/alden should be resolved on its merits. The 22 stale codex/alden-* draft PRs cannot merge as-is and will be closed, so useful fixes from that lineage get ported to main first, and only when the defect reproduces on main. This ports the SHM part of lineage commit 410fc39 ("fix: rebuild private SQLite SHM and type bounded snapshot retries").

What was broken

src/context_sync_replica.rs included the source -shm in the copy signature. The -shm is SQLite's transient WAL index. When no other connection holds the database, each new reader is the first connection. It runs WAL recovery and rewrites the -shm, while the main DB and the WAL stay identical. A reader that opens during each copy attempt therefore uses up all three attempts. context-sync-local then reports a failed snapshot, and the DB watcher stays fenced and delays delivery. This is an availability problem, not a wrong send.

Measured on a scratch WAL database (Python sqlite3, macOS). A separate process that reads while another connection holds the DB left the -shm stat unchanged in 6 of 6 runs. With no holder, each read-only reader changed only the -shm mtime in 3 of 3 runs. The DB and WAL stayed the same. How often production hits the no-holder state is not measured.

Fix

The signature and the copy now cover only the main DB and the WAL. The replica still opens with SQLITE_OPEN_READ_ONLY and query_only, and SQLite rebuilds the replica's own -shm in the private temp directory. Every other safeguard stays as it was: regular-file checks, the size check, three bounded attempts, fail-closed exhaustion, and the source identity checks in open_fresh. The rest of 410fc39 (typed retry error, clonefile flags, ctime/device fields, watcher changes, docs) is not ported. Main already has its own retry plumbing, and none of those parts had a reproduced defect.

Red, then green

The repro test lands first (7a07cf3). Run against that commit in a detached worktree:

test context_sync_replica::tests::context_sync_local_replica_ignores_shm_rebuilt_by_another_reader ... FAILED
Error: consistent isolated context sync SQLite replica unavailable

Caused by:
    context sync SQLite source changed during isolated copy
test result: FAILED. 4 passed; 1 failed; 0 ignored; 0 measured; 248 filtered out

The test's hook asserts that, on every attempt, the reader left the DB and WAL signatures unchanged and changed only the -shm. With the fix (e6284ee):

test context_sync_replica::tests::context_sync_local_replica_ignores_shm_rebuilt_by_another_reader ... ok
test context_sync_replica::tests::context_sync_local_replica_copies_wal_and_rebuilds_shm_privately ... ok
test context_sync_replica::tests::context_sync_local_replica_fails_closed_when_source_changes_on_all_three_attempts ... ok
test result: ok. 5 passed; 0 failed

The existing fail-closed test still passes. A WAL write on every attempt still exhausts the retry.

CI steps run locally (macOS, Rust 1.95.0)

  • cargo test passed, 1173 passed and 0 failed.
  • cargo fmt --check passed.
  • cargo clippy --all-targets -- -D warnings passed.
  • cargo build && ./target/debug/openkakao-cli --version printed openkakao-cli 1.9.1.

Tests ran only against scratch databases. No KakaoTalk database, watcher, or send was touched.

🤖 Generated with Claude Code

twoimo and others added 2 commits November 23, 2026 12:25
…rce SHM

When no connection holds the source database, every reader that opens it
runs WAL recovery and rewrites the -shm, while the DB and WAL stay
byte-identical. The replica signature includes the -shm, so a reader that
opens during each copy attempt exhausts all three attempts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The -shm is SQLite's WAL index. The first connection to open a database
rebuilds it from the DB and WAL, so it can change while the data does not.
Including it in the signature rejected consistent copies and, with a
reader opening during each attempt, exhausted the bounded retry.

The replica now signs and copies only the main DB and WAL. It still opens
read-only with query_only, and SQLite rebuilds the replica's own -shm in
the private temp directory. Ported from lineage commit 410fc39.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@twoimo

twoimo commented Nov 23, 2026

Copy link
Copy Markdown
Owner Author

Decision: merge.

Evidence:

  • This ports the reproduced part of lineage commit 410fc39 from the codex/alden-* drafts being closed. Red: on 7a07cf3 (test only), context_sync_local_replica_ignores_shm_rebuilt_by_another_reader fails with context sync SQLite source changed during isolated copy. The source DB and WAL are unchanged, and only the -shm was rebuilt by another reader. Green on e6284ee.
  • Local CI for the macOS job on e6284ee (Rust 1.95.0) passed. cargo test passed 1173 with 0 failed. cargo fmt --check and cargo clippy --all-targets -- -D warnings passed. cargo build && ./target/debug/openkakao-cli --version printed openkakao-cli 1.9.1.
  • An independent reviewer (Opus) gave PASS+NOTES. Copying only the DB and WAL cannot yield torn data. On first open, SQLite recovers the wal-index from the WAL and accepts frames only up to the last commit frame with valid salts and checksums. A DB/WAL pair copied at different instants is still caught by the size and mtime signature, and the source is still only stat'd, cloned and read under query_only=1. cargo test context_sync passed 11 with 0 failed. Its notes are not blocking: a checkpoint-during-copy test and the non-atomic byte-copy fallback both predate this PR.
  • GitHub-hosted Actions are blocked by billing, so the zero-step check failures are infra state.

@twoimo
twoimo merged commit a6767c4 into main Nov 23, 2026
4 checks passed
This was referenced Nov 23, 2026
twoimo added a commit that referenced this pull request Nov 23, 2026
PRs #59 to #65 merged and the 22 codex/alden-* drafts closed with their
branches kept. List the user-visible fixes under [Unreleased].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@twoimo
twoimo deleted the fix/replica-shm-signature branch November 23, 2026 04:07
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