Repository navigation
perf(storage): rescue perf items from #809 fold (#1314) - #1334
Conversation
Fold the four perf items from #809 that were orphaned by the #809 → #815 → #817 split: 1. _SESSION_INSIGHT_REBUILD_PAGE_SIZE: 1 → 50. The message-budget chunker is the actual safety net for pathologically large conversations; the page size only controls per-conversation SQL round-trips. 2. message_fts_readiness: call the cheap LIMIT-1 probe from search instead of full COUNT(*). Fixed the probe to read messages_fts_docsize (the shadow table) rather than the contentless virtual surface, so the "FTS exists but empty" state still trips the readiness gate. 3. get_provider_metrics_rows: read pre-aggregates from conversation_stats (one row per conv) instead of scanning messages. Role-keyed splits still come from messages via the idx_messages_provider_stats covering index. 4. get_messages / get_messages_paginated / get_messages_batch: mutate the freshly-constructed MessageRecord instances in place rather than calling pydantic model_copy per message. tests/unit/storage/test_perf_rescue_1314.py pins each invariant by counting SQL operations against tier_small_db. Closes #1314 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (6)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5d96170df4
ℹ️ 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".
|
Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.
Only the rows listed above are dispositioned. Held findings remain open. |
Summary
Fold the four perf items from #809 that were dropped during the
#809 → #815 → #817 split (caught by the #1310 fall-through audit).
All four cluster around the session-insight rebuild + read hot path
and are independently small, so they ship in one cohesive PR.
Problem
Issue #1314 enumerated four orphaned perf items:
_SESSION_INSIGHT_REBUILD_PAGE_SIZE = 1atpolylogue/storage/insights/session/rebuild.pycaused~17K SQL round-trips for ~4K conversations on production
archives.
SELECT COUNT(*) FROM messages_fts(_docsize)for FTS-readiness verification — daemon convergence
(
polylogue/daemon/convergence_stages.py) already maintainsthe strict invariant, so the per-query check was redundant.
get_provider_metrics_rowsscannedmessagesforper-conversation aggregates that
conversation_statsalreadystores pre-aggregated (one row per conversation).
get_messages/get_messages_paginated/get_messages_batchcalled pydantic
model_copyper message in the hot hydration pathjust to attach content blocks — constructing a second BaseModel
instance per row.
#817 slice D (#1243) covers the compound-index and ANALYZE work,
but these four items had no current scope.
Solution
polylogue/storage/insights/session/rebuild.py: raise_SESSION_INSIGHT_REBUILD_PAGE_SIZEto 50. The message-budgetchunker (
_chunk_conversation_ids_by_message_budget_sync,budget = 5,000 messages) remains the real safety net for
pathological conversations; the page size only controls
per-conversation SQL round-trips.
polylogue/storage/sqlite/queries/conversations_search.py:search_conversation_hitsandsearch_conversation_evidence_hitsnow call
message_fts_readiness_async(conn, verify_total_rows=False),skipping the full
COUNT(*) FROM messages_fts_docsize/SELECT COUNT(*) FROM messagesscans per query. Daemonconvergence is the canonical strict-readiness gate.
polylogue/storage/fts/fts_lifecycle.py: the cheap probe usedSELECT 1 FROM messages_fts LIMIT 1, butmessages_ftsis acontentless FTS5 virtual table —
DELETE FROM messages_ftsdoesnot remove rows from the virtual surface (the surface mirrors
messages) so the probe missed the "FTS exists but empty"corruption case. Switched to
messages_fts_docsize(the FTS5shadow table that does honour deletes), which preserves the
test_fetch_search_results_raises_when_message_index_is_incompletecontract.
polylogue/storage/sqlite/queries/stats.py:get_provider_metrics_rowsnow pullsconversation_count,message_count,tool_use_count,thinking_count,conversations_with_tools,conversations_with_thinkingfromconversation_stats(one row per conversation, far smaller thanthe messages table). Role-keyed splits (
user_message_count,assistant_message_count, word sums) still aggregate frommessagesvia theidx_messages_provider_statscoveringindex — they aren't pre-aggregated. The
ProviderMetricsRowreturn contract is preserved.
polylogue/storage/sqlite/query_store_archive.py: replaced themessage.model_copy(update={\"content_blocks\": ...})listcomprehensions with in-place
message.content_blocks = ...assignment in
get_messages,get_messages_paginated, andget_messages_batch. TheMessageRecordinstances are freshlyconstructed by
_row_to_messageand aren't shared.tests/unit/storage/test_perf_rescue_1314.py: regression guardsfor each invariant. The tests count SQL operations (not wall-clock
time) against the
tier_small_dbfixture so they stay portable.Verification
nix develop -c devtools verify --quick→ exit_code 0(ruff format/check, mypy --strict, render-all --check,
layering/topology/lints).
nix develop -c pytest tests/unit/storage/test_perf_rescue_1314.py→ 4 passed in 8s (page size, FTS readiness probe, provider
metrics SQL routing, hydration in-place mutation).
nix develop -c pytest tests/unit/storage/test_fts5.py tests/unit/storage/test_fts_bloat_invariants.py tests/unit/core/test_query_retrieval_candidates.py→ 77 passed.Confirms the
messages_fts_docsizeprobe still trips the"FTS exists but empty" contract.
nix develop -c pytest tests/unit -q -m \"not scale_medium and not scale_large\" -p no:randomly --testmon-noselect→ 7876 passed, 2 xfailed (no regressions from the changes; one
pre-existing socket-timeout flake in
test_web_reader.py).Before/after on the #1183
scale_smallfixture (~100 convs,~1K msgs, single-threaded, median of 15 runs):
get_provider_metrics_rowssearch_conversation_hitsget_messagesrebuild_session_insights_syncThe page-size win is bounded on the small tier because the
fixture only has ~100 conversations; the production-shape benefit
is roughly proportional to conversation count.
Closes #1314