Skip to content

refactor(storage): dedup 2 of 10 remaining archive.py query_* methods - #3432

Merged
Sinity merged 2 commits into
masterfrom
feature/refactor/query-side-dedup
Jul 31, 2026
Merged

refactor(storage): dedup 2 of 10 remaining archive.py query_* methods#3432
Sinity merged 2 commits into
masterfrom
feature/refactor/query-side-dedup

Conversation

@Sinity

@Sinity Sinity commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Summary

Finishes polylogue-aif4, PR #3427's follow-up bead scoping the 10
query_* methods left untouched after that PR's two exact-duplicate
extractions. 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_names shape 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 an
honest 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:

  1. query_actions and query_session_actions both project the
    identical sixteen-column action shape (actions view joined to
    sessions/messages) — hydration already went through the shared
    _archive_action_query_row(), but the SELECT column list text itself
    was hand-duplicated byte-for-byte in both methods.
  2. query_unit_counts and query_unit_multi_counts each hand-maintained
    an 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 the
    file-query projection) and wired both query_actions and
    query_session_actions to it. Extracted _QUERY_UNIT_ROW_ALIAS and
    _query_unit_from_sql_by_unit() and wired both query_unit_counts and
    query_unit_multi_counts to them.

Left alone, with reasons (per aif4's honest-audit framing):

  • query_session_action_occurrences — selects from raw blocks
    (aliased u/r, no follow-up relation) instead of the actions view,
    deliberately staying cheap on very large sessions per its own
    docstring. Output columns rhyme with query_actions but the column
    sources 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 done
    in Realize the free-threaded parse win + start table-driving query SELECTs #3427), query_blocks, query_assertions — each a single one-off
    projection 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, not archive.py — out of scope for this
    bead.

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 (via
    devtools verify --quick, exit 0; steps 03-mypy and 04-render-all
    both passed).
  • devtools verify --quick (pre-push gate) — exit 0, all 19 steps green.
  • Behavior-preserving refactor, no test changed:
    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.
  • Not run: the heavy full test suite (per-PR CI skip convention; runs
    post-merge).

Ref polylogue-aif4.

Co-Authored-By: Claude noreply@anthropic.com

Sinity and others added 2 commits July 31, 2026 09:57
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>
@Sinity
Sinity merged commit 6e93c62 into master Jul 31, 2026
2 checks passed
@Sinity
Sinity deleted the feature/refactor/query-side-dedup branch July 31, 2026 08:06
@coderabbitai

coderabbitai Bot commented Jul 31, 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: 38 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 Plus

Run ID: a3a1185e-1d4b-4399-9d6d-9b4a07f85647

📥 Commits

Reviewing files that changed from the base of the PR and between 2774661 and a7054b7.

📒 Files selected for processing (1)
  • polylogue/storage/sqlite/archive_tiers/archive.py

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 added a commit that referenced this pull request Jul 31, 2026
PR #3432 (query-side dedup follow-up to #3427) merged as 6e93c62.

Co-Authored-By: Claude <noreply@anthropic.com>
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