refactor(storage): dedup 2 of 10 remaining archive.py query_* methods - #3432
Conversation
The unit -> row-alias map and unit -> FROM-clause map used by query_unit_counts and query_unit_multi_counts to dispatch aggregate queries across the seven SQL-backed query units were hand-duplicated byte-for-byte between the two methods. Extract them into module-level _QUERY_UNIT_ROW_ALIAS and _query_unit_from_sql_by_unit() so a new query unit is wired in one place instead of two that can silently drift. Behavior-preserving: both call sites consume the same dict contents as before, unchanged. Ref polylogue-aif4. Co-Authored-By: Claude <noreply@anthropic.com>
query_actions and query_session_actions both project the identical sixteen-column action shape (actions view joined to sessions/messages) byte-for-byte -- hydration already went through the shared _archive_action_query_row(), but the SELECT column list itself was hand-duplicated in both methods. Extract it into module-level _ARCHIVE_ACTION_QUERY_COLUMNS / _ARCHIVE_ACTION_QUERY_SELECT_SQL, following the same (name, source-expr) pattern PR #3427 used for the file-query projection. query_session_action_occurrences is deliberately left alone: it selects from raw blocks (aliased u/r, no follow-up relation) to stay cheap on large sessions, so its column sources genuinely differ even though the output shape rhymes. Behavior-preserving: generated SELECT SQL text is unchanged (same columns, same AS aliases, same order). Ref polylogue-aif4. Co-Authored-By: Claude <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 38 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
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 |
Summary
Finishes polylogue-aif4, PR #3427's follow-up bead scoping the 10
query_*methods left untouched after that PR's two exact-duplicateextractions. Audited all 10 for the genuine drift hazard (hand-duplicated
column lists/dicts that can silently diverge between sibling methods, not
the
TableColumnSpec.select_column_namesshape from the INSERT side,which #3427 already established doesn't fit these bespoke join
projections) and extracted the two instances that qualified.
Problem
polylogue-aif4 named 10 remaining
query_*methods and asked for anhonest audit: which subset shares the bead's stated drift hazard (a
column list or dispatch table hand-copied between sibling methods) versus
which are genuinely one-off projections. Two genuine instances were
found:
query_actionsandquery_session_actionsboth project theidentical sixteen-column action shape (
actionsview joined tosessions/messages) — hydration already went through the shared_archive_action_query_row(), but the SELECT column list text itselfwas hand-duplicated byte-for-byte in both methods.
query_unit_countsandquery_unit_multi_countseach hand-maintainedan identical copy of the unit -> row-alias map and the unit ->
FROM-clause map used to dispatch a terminal aggregate query across the
seven SQL-backed query units — both dicts were byte-identical between
the two methods.
Solution
polylogue/storage/sqlite/archive_tiers/archive.py: extracted_ARCHIVE_ACTION_QUERY_COLUMNS/_ARCHIVE_ACTION_QUERY_SELECT_SQL(same
(output_name, source_expr)pattern PR Realize the free-threaded parse win + start table-driving query SELECTs #3427 used for thefile-query projection) and wired both
query_actionsandquery_session_actionsto it. Extracted_QUERY_UNIT_ROW_ALIASand_query_unit_from_sql_by_unit()and wired bothquery_unit_countsandquery_unit_multi_countsto them.Left alone, with reasons (per aif4's honest-audit framing):
query_session_action_occurrences— selects from rawblocks(aliased
u/r, no follow-up relation) instead of theactionsview,deliberately staying cheap on very large sessions per its own
docstring. Output columns rhyme with
query_actionsbut the columnsources genuinely differ; forcing it onto the shared fragment would
either lose that cost tradeoff or fake follow-up columns that were
never computed.
query_delegations,query_files/query_session_files(already donein Realize the free-threaded parse win + start table-driving query SELECTs #3427),
query_blocks,query_assertions— each a single one-offprojection with no sibling to collapse.
query_runs,query_observed_events,query_context_snapshots—structurally rhyme (relation-CTE prefix + join sessions + hydrate via a
typed projector) but each hydrates through a different domain
function (
projected_run_from_row,observed_event_from_row,context_snapshot_from_row) with different predicate/order-by shapes.aif4's own note says any table-driving here should start from
run_projection_relations.py, notarchive.py— out of scope for thisbead.
No query-shape redesign, no behavior change: same SQL text is generated,
same columns, same aliases, same order.
Verification
mypy --strict polylogue/storage/sqlite/archive_tiers/— clean (viadevtools verify --quick, exit 0; steps03-mypyand04-render-allboth passed).
devtools verify --quick(pre-push gate) — exit 0, all 19 steps green.devtools test tests/unit/cli/test_query_verbs_runtime.py tests/unit/archive/test_query_multi_aggregate.py tests/unit/storage/test_query_unit_time_expression.py— 71 passed.devtools test tests/unit/storage/test_archive_tiers_archive.py tests/unit/cli/test_query_composition_laws.py tests/unit/cli/test_query_expression.py tests/unit/cli/test_query_support_runtime.py— 482 passed, 1 skipped.devtools test tests/unit/cli/test_query_exec_laws.py— 91 passed.Total 644 passed, 1 skipped, 0 failed, across the two extracted
surfaces' full test coverage — no test needed to change.
testsuite (per-PR CI skip convention; runspost-merge).
Ref polylogue-aif4.
Co-Authored-By: Claude noreply@anthropic.com