Skip to content

perf(storage): stop materializing action_pairs text copies - #3159

Merged
Sinity merged 1 commit into
masterfrom
feature/perf/action-pairs-drop-text-copies
Jul 19, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/perf/action-pairs-drop-text-copies

Conversation

@Sinity

@Sinity Sinity commented Jul 19, 2026

Copy link
Copy Markdown
Owner

Summary

action_pairs stops storing copies of tool_input/output_text; it now keeps only join/rank/outcome columns, and the actions VIEW re-joins blocks for the text at read time so every reader keeps byte-identical payloads.

Problem

dbstat on the live rebuild generation showed action_pairs at 4.1GB for 868,522 rows (~4.7KB/row) — every tool interaction existed ~3x on disk (blocks.text/blocks.tool_input, action_pairs.output_text/action_pairs.tool_input, plus FTS). The ~4.7KB rows live in SQLite overflow-page chains, so a per-session DELETE FROM action_pairs WHERE session_id=? pays dozens of scattered page reads per row. strace on a live whale-session delete confirmed the mechanism: 27K distinct pread64/s concentrated in the action_pairs tail with zero sample overlap 30s apart — pure forward progress through a colossal random-IO workload, turning a whale session replace into hours of IO. This is tracked as polylogue-2i2w.

Solution

  • action_pairs (polylogue/storage/sqlite/archive_tiers/index.py) drops tool_input/output_text; surviving columns: tool_use_block_id, tool_result_block_id, session_id, message_id, tool_id, use_rank, tool_name, semantic_type, tool_command, tool_path, is_error, exit_code.
  • action_pairs_refresh_sql (polylogue/storage/sqlite/action_pairs.py) no longer selects/inserts the dropped columns.
  • The actions VIEW is rewritten to INNER JOIN blocks by tool_use_block_id (NOT NULL FK, cascade) and LEFT JOIN blocks by tool_result_block_id (nullable, SET NULL) to re-serve tool_input/output_text at read time — same column names/order as before, so every consumer is unaffected.
  • Consumer audit (rg across storage/repository, insights, daemon, mcp, cli, api, devtools): every reader of pair text goes through the actions view. None reads action_pairs.output_text/tool_input directly except the DDL/refresh/lifecycle machinery this PR edits (action_pairs.py, write.py's refresh_action_pairs calls, schema_bootstrap.py's stat1 seed rows, archive_verification.py's planner-stats table-name list). delegation_facts_source/delegation_facts (subagent-dispatch cohort) reads through the view too and is transparently unaffected (verified via its own tests, unchanged).
  • Index schema version 40 -> 41 (derived tier, no migration chain per the schema regime — docs/internals.md § Schema Versioning Model). Added IndexDeltaDeclaration(version=41, classes=(CACHE_REMOVAL, VIEW_ONLY)) to polylogue/storage/sqlite/lifecycle.py since the transition is copy-forward safe (no parser semantics involved). Added the matching docs/internals.md changelog entry.
  • Noted this bump on polylogue-bo9n and polylogue-v6i3 per the schema-regime batching convention — their own decisions (session_events aggregation, FTS bulk-mode skip) remain open and are not implemented here.

Discovered, filed separately (not fixed here, to keep blast radius narrow)

  • polylogue-5h5y: index schema v40 (query_unit_frame_state, PR feat(query): bind query_units continuations to the archive epoch #3068) never got a declared delta class in lifecycle.py — devtools lab policy schema-versioning already failed before this PR (confirmed by reverting my changes to HEAD and re-running the lint: identical missing: [40] failure).
  • polylogue-m8nj: delegation_facts still materializes its own (smaller, subagent-dispatch-only) copy of instruction_payload/artifact_text.

Verification

  • devtools test tests/unit/sources/test_codex_event_stream_contract.py tests/unit/storage/test_archive_tiers_ddl.py tests/unit/storage/test_archive_tiers_write.py tests/unit/storage/test_archive_tiers_assertions.py tests/unit/storage/test_planner_statistics_seed.py tests/unit/maintenance/test_archive_verification.py tests/unit/storage/test_schema_policy_contracts.py tests/unit/insights/test_tool_usage.py tests/unit/storage/test_delegations_view.py tests/unit/pipeline/test_delegation_provider_fixtures.py tests/unit/storage/test_schema_safety.py → all green except 4 pre-existing failures in test_tool_usage.py/test_delegations_view.py (unknown database user_tier / unable to open database file) — confirmed identical failure set on unmodified HEAD (reverted files, re-ran, same 4 failures), unrelated to this change.
  • devtools verify --quick → exit 0 (ran automatically again via the pre-push hook).
  • devtools render all --check → exit 0, no out of sync.
  • devtools lab policy docs-drift → "zero unhandled drift".
  • devtools lab policy schema-versioning → 1 pre-existing failure (v40 gap, tracked as polylogue-5h5y), no new failures introduced by v41.
  • Byte-equivalence proof: tests/unit/storage/test_archive_tiers_ddl.py already pins exact output_text/tool_command/is_error/exit_code values through the actions view across matched/unmatched/error/reemitted-tool_id/variant-tie/empty-string-tool_id scenarios — all pass unchanged against the new join-based view (this is the golden-fixture proof: values were pinned before this change and are reproduced by the rewritten view). test_agent_action_and_delegation_views_are_indexed_projections (asserts USING INDEX in the actions query plan, no WINDOW/WITH in the view SQL) also passes unchanged. Added test_action_pairs_does_not_materialize_text_copies to lock the trimmed column set in place as a regression guard.
  • Index-size estimate (not directly measured — no live archive access from this isolated worktree): the removed bytes are essentially all of the ~4.7KB/row footprint (surviving columns are short strings/ints/ids already part of that row), so action_pairs should collapse from ~4.1GB to a small fraction of that (likely low hundreds of MB, in-page, no overflow chains) on a real rebuild — most of the measured 4.1GB is expected to be reclaimed from index.db's ~19.6GB total. Confirming this needs a real polylogue ops reset --index && polylogued run + dbstat pass, which is the coordinator's call.

Ref polylogue-2i2w

Problem: dbstat on the live rebuild generation showed action_pairs at
4.1GB for 868,522 rows (~4.7KB/row) -- every tool interaction existed
~3x on disk (blocks.text/tool_input, action_pairs.output_text/tool_input,
FTS). The ~4.7KB rows live in SQLite overflow-page chains, so a
per-session `DELETE FROM action_pairs WHERE session_id=?` pays dozens of
scattered page reads per row, turning a whale session replace into hours
of random IO (confirmed via strace: 27K distinct pread64/s, offsets
concentrated in the action_pairs tail, zero sample overlap 30s apart).

Solution: action_pairs now keeps only join/rank/outcome columns
(tool_use_block_id, tool_result_block_id, session_id, message_id,
tool_id, use_rank, tool_name, semantic_type, tool_command, tool_path,
is_error, exit_code); tool_input/output_text are dropped. The `actions`
VIEW is rewritten to re-join `blocks` by tool_use_block_id (INNER,
NOT NULL FK) and tool_result_block_id (LEFT, nullable) at read time, so
every reader keeps byte-identical payloads with zero caller change.

Consumer audit (rg across storage/repository, insights, daemon, MCP,
CLI, api, devtools) found every reader goes through the `actions` view;
none touches action_pairs.output_text/tool_input directly except the
DDL/refresh/lifecycle machinery this PR edits. delegation_facts reads
through the view transparently too (verified unchanged).

Index schema version 40 -> 41 (derived tier, no migration chain per the
schema regime); added an IndexDeltaDeclaration(version=41,
classes=(CACHE_REMOVAL, VIEW_ONLY)) since the transition is copy-forward
safe (no parser semantics involved). Noted the bump on polylogue-bo9n and
polylogue-v6i3 (their own decisions remain open, not implemented here).

Discovered pre-existing gaps while auditing, filed separately rather than
folding into this PR's blast radius: polylogue-5h5y (index schema v40
never got a declared delta class -- confirmed independent of this change
by reverting and re-running the lint) and polylogue-m8nj (delegation_facts
still materializes its own, smaller, instruction_payload/artifact_text
copy for the subagent-dispatch cohort).

Verification:
- devtools test on directly-touched + consumer test files (action_pairs
  refresh SQL, actions/delegations views, planner-stats plan shape,
  archive_verification, schema policy contracts, tool_usage insights,
  delegation query units) -- all green except 4 pre-existing failures in
  test_tool_usage.py/test_delegations_view.py ("unknown database
  user_tier" / "unable to open database file"), confirmed identical on
  unmodified HEAD.
- devtools verify --quick: exit 0.
- devtools render all --check: exit 0, no "out of sync".
- devtools lab policy docs-drift: zero unhandled drift.
- devtools lab policy schema-versioning: 1 pre-existing failure (v40 gap,
  polylogue-5h5y), no new failures from v41.
- Byte-equivalence proof: tests/unit/storage/test_archive_tiers_ddl.py
  already pins exact output_text/tool_command/is_error/exit_code values
  through the `actions` view across matched/unmatched/error/reemitted-
  tool_id/variant-tie/empty-string scenarios; all pass unchanged against
  the new join-based view. Added
  test_action_pairs_does_not_materialize_text_copies to lock the trimmed
  column set in place.

Ref polylogue-2i2w

Co-Authored-By: Claude <[email protected]>
@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 19, 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: 4 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: a63a6b06-55c8-4406-8dec-e6fd0687a138

📥 Commits

Reviewing files that changed from the base of the PR and between bc9381e and d0f272f.

📒 Files selected for processing (6)
  • docs/internals.md
  • polylogue/storage/sqlite/action_pairs.py
  • polylogue/storage/sqlite/archive_tiers/index.py
  • polylogue/storage/sqlite/lifecycle.py
  • tests/unit/sources/test_codex_event_stream_contract.py
  • tests/unit/storage/test_archive_tiers_ddl.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/perf/action-pairs-drop-text-copies

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 8b8d5b1 into master Jul 19, 2026
3 checks passed
@Sinity
Sinity deleted the feature/perf/action-pairs-drop-text-copies branch July 19, 2026 14:31
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