Skip to content

perf(coordination): make archive_evidence collection resumable - #3131

Merged
Sinity merged 1 commit into
masterfrom
feature/perf/snappy-surfaces
Jul 18, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/perf/snappy-surfaces

Conversation

@Sinity

@Sinity Sinity commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

build_coordination_envelope's expensive archive_evidence sub-stage (bounded to a 3s deadline in #3116) could never actually finish under sustained polling — it was silently restarted from scratch on every refresh tick, forever.

Problem

_bounded_stage built a throwaway StatusComponentRegistry on every call. Live measurement found archive_evidence alone taking ~10s while bounded to 3s (#3116). CoordinationEnvelopeCache's own outer envelope TTL is ~1s, so every refresh tick abandoned the previous attempt's still-running background thread (which kept executing, unobserved) and launched an equally-doomed new one — the query never got a chance to finish within any single attempt's window, and the coordination status MCP consumer (#3128) would degrade forever instead of eventually converging on the real answer. This is polylogue-20d.17 AC #6 ("exact expensive diagnostics are ... resumable").

Solution

  • _bounded_stage now accepts an optional persistent registry. When given, it's reused as-is (the call argument is ignored — the registry's own collector already knows how to recompute what it needs).
  • CoordinationEnvelopeCache owns one persistent archive_evidence StatusComponentRegistry per cwd (fingerprint-invalidated the same way as the outer envelope registry), threaded into build_coordination_envelope via a new archive_evidence_registry parameter.
  • A later tick that finds the registry's attempt still in flight now observes refreshing immediately instead of restarting the query; once the background attempt actually finishes, every subsequent tick reuses that real result until its own TTL/fingerprint expires.
  • Direct/CLI calls to build_coordination_envelope (detail requests, one-shot processes) are unaffected — they still get a fresh ephemeral registry, since a one-shot process has nothing to resume across.
  • Fixed a self-deadlock found during development: get_or_build held its own lock while calling the new _archive_evidence_registry helper, which re-acquired the same lock (threading.Lock isn't reentrant). Switched to RLock.

The other two AC #6 properties were already satisfied, unchanged by this PR: "bounded" (existing deadline_s), and "limit constrains collection work" — the underlying archive_evidence SQL already uses LIMIT ? in every query (verified by reading _archive_activity_rows and siblings), not fetch-all-then-slice. "Cancellable" means the caller is never blocked past deadline_s (already true); Python cannot safely preempt a blocking SQLite call mid-query, so this does not attempt true thread cancellation.

Verification

  • New test test_coordination_envelope_cache_resumes_slow_archive_evidence_across_ticks proves the collector is invoked exactly once across three ticks (timed out → refreshing → fresh) despite the query taking longer than the deadline — this fails on the pre-fix ephemeral-registry code (each tick would re-invoke the collector, incrementing the call counter each time).
  • devtools test tests/unit/coordination/test_envelope.py tests/unit/mcp/test_status_scope_coordination.py — 28 passed.
  • mypy --strict polylogue/coordination/envelope.py — clean.
  • devtools verify --quick — exit 0.

Ref polylogue-20d.17

Problem: build_coordination_envelope's archive_evidence stage is bounded
to a 3s deadline (#3116), but _bounded_stage built a throwaway
StatusComponentRegistry on every call. A query slower than the deadline
(observed live: ~10s) could never actually finish within any single
attempt -- CoordinationEnvelopeCache's own ~1s outer TTL meant every
refresh tick abandoned the previous attempt's still-running background
thread and launched an equally-doomed new one, forever falling back to
the degraded/empty result instead of ever converging on the real answer
(polylogue-20d.17 AC #6: resumable detail collection).

What changed: _bounded_stage now accepts an optional persistent registry;
CoordinationEnvelopeCache owns one archive_evidence StatusComponentRegistry
per cwd (fingerprint-invalidated the same way as the outer envelope
registry) and threads it through build_coordination_envelope's new
archive_evidence_registry parameter. A later tick that finds the
registry's attempt still in flight now observes "refreshing" immediately
instead of restarting the query, and once the background attempt
actually finishes, every subsequent tick reuses that real result until
its own TTL/fingerprint expires. Direct/CLI calls to
build_coordination_envelope (detail requests, one-shot processes) are
unaffected -- they still get a fresh ephemeral registry, matching
pre-existing behavior, since a one-shot process has nothing to resume
across.

Fixed a self-deadlock introduced during development: CoordinationEnvelopeCache's
get_or_build held its own lock while calling the new
_archive_evidence_registry helper, which re-acquired the same lock --
threading.Lock isn't reentrant. Switched to RLock.

The other two AC #6 properties were already satisfied and are unchanged:
"bounded" (existing deadline_s), "limit constrains collection work" (the
underlying archive_evidence SQL already uses LIMIT ? in every query, not
fetch-all-then-slice -- verified by reading _archive_activity_rows and
siblings). "Cancellable" means the caller is never blocked past
deadline_s (already true); Python cannot safely preempt a blocking SQLite
call mid-query, so this does not attempt true thread cancellation.

Verification: new test
test_coordination_envelope_cache_resumes_slow_archive_evidence_across_ticks
proves the collector is invoked exactly once across three ticks (timed
out, refreshing, fresh) despite the query taking longer than the deadline
-- this fails on the pre-fix ephemeral-registry code (each tick would
re-invoke the collector). devtools test tests/unit/coordination/test_envelope.py
tests/unit/mcp/test_status_scope_coordination.py: 28 passed. mypy --strict
polylogue/coordination/envelope.py: clean. devtools verify --quick: exit 0.

Ref polylogue-20d.17
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai

coderabbitai Bot commented Jul 18, 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: 11 minutes

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

Run ID: 749cc252-2b77-402f-9fa0-a549e95ab9cb

📥 Commits

Reviewing files that changed from the base of the PR and between 5ad26b2 and 9effa24.

📒 Files selected for processing (2)
  • polylogue/coordination/envelope.py
  • tests/unit/coordination/test_envelope.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/perf/snappy-surfaces

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.

@Sinity
Sinity merged commit 4b3b805 into master Jul 18, 2026
3 checks passed
@Sinity
Sinity deleted the feature/perf/snappy-surfaces branch July 18, 2026 21:51
Sinity added a commit that referenced this pull request Jul 28, 2026
…t registry (#3377)

## Summary

Fixes a real daemon bug: the periodic (10s-cadence) status snapshot refresh built a brand-new ephemeral StatusComponentRegistry every tick, so any component slower than its own declared deadline was restarted from scratch and discarded forever, leaking one orphaned collector thread per tick. Live measurement found embedding_readiness_info alone takes ~5s against the real archive against a declared 2.0s deadline -- this component never converged, ever, under the periodic path.

## Problem

This is the same resumability gap PR #3131 already fixed for coordination's archive_evidence stage, but on the daemon status side. None of build_daemon_status's ~14 components had a fingerprint either (AC #5 gap).

## Solution

Extracted the component spec list into _daemon_status_component_specs() (shared by both paths), added periodic_status_component_registry() -- a lazily-built, process-wide persistent registry, fingerprinted on index/ops db + WAL mtimes via a new _daemon_status_fingerprint() helper mirroring coordination's _coordination_fingerprint. Threaded an optional registry param through build_daemon_status/daemon_status_payload, wired only from refresh_status_snapshot's periodic call. One-shot CLI/API status paths are unaffected (registry=None default, unchanged ephemeral behavior).

Investigated the rest of the AC #5 fingerprint gap and the other components; found most (beads, handoff, archive_debt, assertion_candidate_queue) are either already safely bounded or verified (by grepping every call site) to only ever run in one-shot processes with no cross-call state to lose -- documented honestly rather than force-changing things that weren't actually broken (same methodology as polylogue-dhjz).

## Verification

- mypy --strict: clean
- ruff check/format --check: clean
- devtools test tests/unit/daemon/test_daemon_status.py -> 63 passed
- devtools verify --quick -> exit 0 (one pre-existing unrelated failure, test_archive_facade_route_catalog_covers_public_async_facade, confirmed identical on baseline master)
- Anti-vacuity: two new tests (resumability across ticks, fingerprint forcing refresh) both fail when reverting the fix, pass with it restored.
- Live dogfood (read-only, real archive /realm/db/polylogue): tick 0 times out at 2.0s, ticks 1-2 observe refreshing without re-invoking the collector, tick after ~8s returns fresh with real embedding_coverage_percent=44.1.
- CI: GitGuardian/CircleCI quick-gate pass, CodeRabbit review completed clean.

Ref polylogue-20d.17
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