Skip to content

refactor(storage): extract MATERIALIZATION_COLUMNS_SQL for insight DDLs - #1380

Merged
Sinity merged 1 commit into
masterfrom
refactor/insight-ddl-composer
May 19, 2026
Merged

Sinity merged 1 commit into
masterfrom
refactor/insight-ddl-composer

Conversation

@Sinity

@Sinity Sinity commented May 19, 2026

Copy link
Copy Markdown
Owner

Summary

Every session-insight table repeated the same 7-9 materializer-lifecycle columns by hand. Extract into a single composable SQL fragment in schema_ddl_insight_common.py; migrate all six insight tables across three DDL files. Rendered SQL is byte-identical (SHA-256 verified per file).

Problem

Six insight tables (session_profiles, session_work_events, session_phases, session_tag_rollups, day_session_summaries, plus the aggregates trio) each spelled out the lifecycle column set — materializer_version, materialized_at, source_updated_at, source_sort_key, input_high_water_mark, input_high_water_mark_source, input_row_count — in their CREATE TABLE statements. Any drift between sites would let one materializer silently skip a lifecycle column on a fresh schema; nothing structural prevented that.

Solution

  • polylogue/storage/sqlite/schema_ddl_insight_common.py exposes:
    • MATERIALIZATION_COLUMNS_SQL — the canonical 7-column block (with source_sort_key).
    • MATERIALIZATION_COLUMNS_SQL_NO_SORT_KEY — variant for work_threads, the lone insight table that legitimately omits source_sort_key (threads have no single source row to sort by).
    • materialization_columns_sql(*, with_source_sort_key=True) for callers who want the variant inline.
  • Three DDL modules migrated to compose from the helper: schema_ddl_insight_profiles.py (-8 LoC), schema_ddl_insight_timelines.py (-13 LoC), schema_ddl_insight_aggregates.py (-19 LoC).
  • provider_name and canonical_session_date intentionally stay per-table:
    • provider_name is part of the composite PK in session_tag_rollups/day_session_summaries and precedes the lifecycle block (column ordering matters on disk).
    • canonical_session_date placement varies between tables and is absent from aggregates — it's actually domain, not lifecycle.

Verification

  • Rendered SQL diff per file is empty. Captured CREATE TABLE strings before and after via SHA-256: cc284a02…, 587c20ba…, 5b9ebcde… — identical pre- and post-ruff format.
  • nix develop --command pytest tests/unit/storage -q → no regressions (2 pre-existing failures on the parity-adapter tests inherited from master baseline, unrelated).
  • devtools verify-topology clean (helper module + LoC counts updated).

Drift findings worth follow-up

(Documented in the commit message for archaeology; not addressed in this PR.)

  • work_threads is the lone lifecycle-bearing table missing source_sort_key. Either intentional (no sortable source row) or an oversight. Preserved as-is via the _NO_SORT_KEY variant.
  • provider_name placement varies between composite-PK and FK-keyed tables. Not a bug — composite PKs cluster the key columns together — but it's why provider_name can't be folded into the shared block.
  • canonical_session_date was originally on the brief as a candidate lifecycle column. Reality: it's domain-specific (placement varies, absent from aggregates). Excluded for the right reason.

@coderabbitai

coderabbitai Bot commented May 19, 2026 •

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 30 minutes and 45 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 68e2f2f4-6c8e-439d-8188-c6a9d0f0cda0

📥 Commits

Reviewing files that changed from the base of the PR and between 9743e00 and 9f42b0b.

📒 Files selected for processing (6)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/storage/sqlite/schema_ddl_insight_aggregates.py
  • polylogue/storage/sqlite/schema_ddl_insight_common.py
  • polylogue/storage/sqlite/schema_ddl_insight_profiles.py
  • polylogue/storage/sqlite/schema_ddl_insight_timelines.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/insight-ddl-composer

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 and usage tips.

@Sinity
Sinity force-pushed the refactor/insight-ddl-composer branch from 0b73301 to 5f8e924 Compare May 19, 2026 16:18
Sinity added a commit that referenced this pull request May 19, 2026
…#1384)

## Summary

PR #1370 (typed ranked-result envelope) changed `polylogue list --format
json` for search-mode queries to emit `{"hits": [...], "limit": N,
"next_cursor": ...}` rather than a bare JSON array. The CLI parity
adapter at `tests/infra/surfaces.py:307` still expected a bare array and
asserted `not isinstance(parsed, list)`, causing both
`test_search_unique_token_parity` and `test_search_shared_token_parity`
to fail on every search-mode case on master.

## Solution

When the parsed CLI output is a dict carrying a `hits` array (the typed
envelope shape), peel the `hits` and continue with the existing per-row
id extraction. The list-mode and search-hit-mode shapes still work
unchanged — they hit the same row loop directly.

Updated the inline doc to enumerate all three shapes the adapter now
handles.

## Verification

- `nix develop --command pytest
tests/unit/storage/test_surface_parity_adapters.py -q` → **10 passed**
(was 2 failures on master).
- No production code touched; test-infra only.

## Scope

Single-file, test-infra-only change. Should land before #1379 / #1380 /
#1382 (or alongside) so those PRs' CI runs are unmasked by this
pre-existing failure.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Every session-insight DDL file (`schema_ddl_insight_profiles`,
`schema_ddl_insight_timelines`, `schema_ddl_insight_aggregates`) repeated
the same materializer-lifecycle columns (`materializer_version`,
`materialized_at`, `source_updated_at`, optional `source_sort_key`,
`input_high_water_mark`, `input_high_water_mark_source`,
`input_row_count`) by hand on every CREATE TABLE. Drift between copies
would silently weaken the lifecycle contract.

Extract a `schema_ddl_insight_common` module exposing
`MATERIALIZATION_COLUMNS_SQL` (with `source_sort_key`),
`MATERIALIZATION_COLUMNS_SQL_NO_SORT_KEY` (for `work_threads`, which has
no per-row sort key), and a `materialization_columns_sql(...)` composer.
Each insight DDL now splices the shared fragment between its domain
columns. Rendered DDL strings are byte-identical before vs after across
all three files (verified by SHA + full string comparison).

Drift noted but preserved: `work_threads` omits `source_sort_key`;
`provider_name` and `canonical_session_date` placements vary across
tables (sometimes before lifecycle, sometimes mid-table) and so are
intentionally NOT folded into the shared block — folding them would
change column order on disk.
@Sinity
Sinity force-pushed the refactor/insight-ddl-composer branch from 5f8e924 to 9f42b0b Compare May 19, 2026 16:27
@Sinity
Sinity enabled auto-merge (squash) May 19, 2026 16:27
@Sinity
Sinity merged commit 14a596a into master May 19, 2026
3 of 5 checks passed
@Sinity
Sinity deleted the refactor/insight-ddl-composer branch May 19, 2026 16:27
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