Skip to content

test(daemon): concurrency/race invariants suite (#1182) - #1206

Merged
Sinity merged 1 commit into
masterfrom
feature/test/daemon-concurrency-1182-v2
May 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/test/daemon-concurrency-1182-v2

Conversation

@Sinity

@Sinity Sinity commented May 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a consolidated daemon concurrency/race suite that pins documented invariants from docs/internals.md under threaded and asyncio interleavings, plus Hypothesis-driven randomized orderings. Closes the AC list on #1182.

Problem

#997 status flagged daemon concurrency tests as "partially covered piecemeal but no consolidated suite." Race-prone surfaces — blob GC + lease handshake (commit_archive_write_effects → acquire_blob_leases → release window), FTS trigger suspend/restore lifecycle, cursor monotonicity under concurrent batch convergence, and live_ingest_attempt worker handoff under shutdown — had no single suite asserting the invariants. A regression that removed the lease table, skipped the lease check, dropped the SIGKILL recovery pass, or weakened the cursor monotonicity contract would not have been caught by the existing tests.

Solution

Four new files, all using threading (and hypothesis for randomized interleavings):

  • tests/unit/storage/test_blob_gc_concurrency.py — lease acquired before raw-row commit blocks GC; release without commit makes blob collectable; durable DB reference alone protects without a lease; threaded acquire/commit/GC loop with 20 commits + continuous GC never deletes a referenced blob; Hypothesis test covers N distinct blobs with shuffled commit ordering.
  • tests/unit/storage/test_fts_trigger_lifecycle.py — suspend_fts_triggers_sync drops all six expected triggers; restore_fts_triggers_sync is idempotent (no duplicates); simulated SIGKILL between suspend and restore leaves detectable drift and is recoverable by a fresh restore; concurrent suspend/restore threads converge; Hypothesis arbitrary sequences end in a recoverable state with no spurious triggers.
  • tests/unit/daemon/test_live_watcher_concurrency.py — CursorStore.set(..., allow_backward=False) rejects regressions under same parser fingerprint; allow_backward=True is the documented escape hatch; N-thread monotone-offset race lands at the max committed offset; concurrent raw_conversations upserts on same raw_id converge to exactly one row; Hypothesis pins same-fingerprint cursor = max(offsets).
  • tests/unit/daemon/test_ingest_worker_handoff.py — begin_ingest_attempt persists a durable running row; opening a fresh CursorStore on a DB with a stale running row marks it abandoned/interrupted (the SIGKILL recovery path); a completed row is not clobbered by reopen; concurrent begin_ingest_attempt calls produce distinct attempt IDs; mid-flight phase updates are visible to independent readers.

pragma: no cover annotations on defensive thread error-capture branches carry inline justifications as required by verify-suppressions.

Note on #1190 (run_blob_gc unlinks at flat path instead of blob_path): the suite's assertions are framed around the DB-level lease/reference invariant (deletion count and exists() semantics that remain correct under the bug), so none of the new tests trip #1190. The orphan-collection test documents this explicitly.

Verification

nix develop -c pytest tests/unit/storage/test_blob_gc_concurrency.py \
  tests/unit/storage/test_fts_trigger_lifecycle.py \
  tests/unit/daemon/test_live_watcher_concurrency.py \
  tests/unit/daemon/test_ingest_worker_handoff.py -x
# 21 passed in 8.47s

nix develop -c devtools verify --quick
# exit_code: 0 (after `devtools render-pages` for the unrelated .cache/site drift)

Closes #1182
Ref #997

Summary by CodeRabbit

  • Tests
    • Added comprehensive unit tests for ingest worker shutdown recovery and handoff invariants.
    • Added concurrency tests for cursor operations and concurrent data ingestion to verify atomicity and prevent regressions.
    • Added tests for blob garbage collection lease management and concurrent collection scenarios.
    • Added tests for search indexing trigger lifecycle and recovery after simulated failures.

Review Change Stack

Adds a consolidated suite that pins documented daemon concurrency
invariants under interleaved load:

- tests/unit/storage/test_blob_gc_concurrency.py — lease bridges
  acquire-blob → write-row commit window; threaded acquire/commit/GC
  loop never deletes a referenced blob; hypothesis interleaves
  randomized commit orderings against multiple blobs.
- tests/unit/storage/test_fts_trigger_lifecycle.py — suspend/restore
  contract: drops all six expected triggers, restore is idempotent,
  simulated SIGKILL between suspend and restore is recovered by a
  fresh restore call; hypothesis exercises arbitrary lifecycle
  sequences.
- tests/unit/daemon/test_live_watcher_concurrency.py — CursorStore
  rejects backward writes under the same parser fingerprint; N-thread
  monotone-offset race ends at the max committed offset; concurrent
  raw_conversations upserts converge to a single row by raw_id;
  hypothesis pins same-fingerprint cursor = max(offsets).
- tests/unit/daemon/test_ingest_worker_handoff.py — begin_ingest_attempt
  persists a durable running row; simulated SIGKILL row becomes
  abandoned/interrupted on next CursorStore open; finished attempts
  are not clobbered by the recovery pass; concurrent begin calls
  produce distinct attempt_ids; in-flight phase update is visible
  to independent readers.

All tests use threading and (for stress files) hypothesis with
@settings(deadline=None) and run in under 10s combined.

Closes #1182
@Sinity
Sinity merged commit 5b9868c into master May 17, 2026
2 checks passed
@Sinity
Sinity deleted the feature/test/daemon-concurrency-1182-v2 branch May 17, 2026 20:31
@coderabbitai

coderabbitai Bot commented May 17, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 39f6be0c-e7a1-4091-aba5-9416f47da4dc

📥 Commits

Reviewing files that changed from the base of the PR and between b526dd4 and 5668b45.

📒 Files selected for processing (4)
  • tests/unit/daemon/test_ingest_worker_handoff.py
  • tests/unit/daemon/test_live_watcher_concurrency.py
  • tests/unit/storage/test_blob_gc_concurrency.py
  • tests/unit/storage/test_fts_trigger_lifecycle.py

📝 Walkthrough

Walkthrough

Four new test modules establish concurrent invariants for daemon handoff recovery, cursor monotonicity, blob GC lease protection, and FTS trigger lifecycle. The suite covers persistence under shutdown, race-free cursor advancement, orphan-free lease semantics, and trigger repair recovery.

Changes

Concurrency Test Suite

Layer / File(s) Summary
Ingest Worker Handoff Invariants
tests/unit/daemon/test_ingest_worker_handoff.py
begin_ingest_attempt immediately persists a running row; reopening the store converts prior running rows to abandoned with an error; completed attempts are terminal; concurrent begins produce distinct attempt_id values; in-flight phase updates remain visible across connections.
Cursor and Raw Ingest Concurrency
tests/unit/daemon/test_live_watcher_concurrency.py
Backward cursor writes are rejected under the same parser_fingerprint when allow_backward=False; new fingerprints permit rewind; concurrent writers never regress the cursor and converge near the maximum offset; concurrent INSERT OR IGNORE raw ingestion remains idempotent; arbitrary offset sequences result in maximum offset storage.
Blob GC Lease Concurrency
tests/unit/storage/test_blob_gc_concurrency.py
Active leases prevent blob deletion during concurrent GC; releasing leases enables orphan collection; raw conversation rows protect blobs without leases; threaded writes that acquire leases, insert DB rows, and release do not lose blobs; random commit/GC interleavings preserve all committed references; lease predicates correctly track lifecycle.
FTS Trigger Lifecycle
tests/unit/storage/test_fts_trigger_lifecycle.py
suspend_fts_triggers_sync removes all six triggers; simulated SIGKILL between suspend and restore leaves triggers absent and restore_fts_triggers_sync recovers them; restore is idempotent; concurrent suspend/restore on separate connections converge to all six triggers; arbitrary suspend/restore interleaving is always recoverable with only expected triggers present.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

A rabbit weaves four test suites tight,
Concurrency covered left and right,
Handoffs hold, cursors align,
Leases protect, and triggers shine! 🐰✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/test/daemon-concurrency-1182-v2

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 and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5668b45c8a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +87 to +89
assert record.byte_size >= max_expected - 100, (
f"cursor regressed: got {record.byte_size}, expected >= {max_expected - 100}. "
"Concurrent writers caused offset rollback."

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Assert exact maximum cursor offset after concurrent writes

This test is documented as proving that concurrent writers converge to the maximum committed offset, but the assertion accepts any final value >= max_expected - 100. In this setup that still passes when the highest offsets (e.g., 2430, 2420, 2410, 2400) are missing, so a regression that drops the top writes can go undetected while the test remains green. Tightening this to require the exact expected max would match the stated invariant and prevent false positives.

Useful? React with 👍 / 👎.

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.

test(daemon): consolidated concurrency and race suite for live watcher, cursors, FTS repair, blob leases

1 participant