Make search copy collapse set-based with an indexed key #103

Merged
Collaborator

Add schema v10's guarded virtual copy_key column and covering index, with an idempotent migration using table_xinfo. Share set-based collapse queries across search, triage counts and keyword ranking while preserving filters, representative selection, pagination totals and FTS fallback.

Add reference-SQL equivalence, tool-output, migration, unsafe-date and query-plan tests. Update schema assertions and document migration and benchmark usage. Existing copy tests remain unchanged.

Validation: 356 filtered tests passed; make check passed with 658 tests passed, 8 skipped and 18 deselected, plus lint, typing and vulnerability audit.

Benchmark medians on voyager, macOS arm64, Python 3.14.5, SQLite 3.50.4; identical script, 210,000 rows, one warm-up and five runs. Baseline 3af5d34 used a temporary source export; new code ran in the dispatch worktree.

Search | baseline ms | new ms | ratio
none | 438.660 | 249.447 | 0.569x
account=a | 320.699 | 210.141 | 0.655x
sender=user7@ | 284.263 | 318.800 | 1.121x

All three meet the 1.5x bound. Optional ungated FTS medians: lorem 586.923 -> 714.880 ms; 1234 0.054 -> 0.154 ms. The approved spec-time no-schema prototype missed at 1.66–1.69x, supporting the v10 approach under ruling R1.

Closes #96
Co-Authored-By: gpt-6.1-sol noreply@openai.com

Rework at the gate (architect, 2026-11-01)

Two commits after 30547f1, on the operator's rework rider: build the duplicate-key set from rows that pass the filters, and show the bench inside the image.

  • 9602aec applied the filters inside dup. Inside the image this was slower. SQLite 3.40.1 does not treat the index on the virtual copy_key column as covering, so grouping a broad filter by key read every row.
  • 868e450 finds candidate Message-IDs from messages_copies (message_id, from_addr), which covers. It then groups only their filter-passing rows by copy_key. The duplicate-key set is built from filter-passing rows only.
-WITH dup AS (SELECT copy_key FROM messages WHERE copy_key IS NOT NULL
-             GROUP BY copy_key HAVING COUNT(*) > 1)
+WITH cand AS MATERIALIZED (SELECT message_id FROM messages WHERE message_id IS NOT NULL
+                          GROUP BY message_id HAVING COUNT(*) > 1),
+     dup AS (SELECT c.copy_key FROM cand x CROSS JOIN messages c ON c.message_id = x.message_id
+             WHERE c.copy_key IS NOT NULL {filters} GROUP BY c.copy_key HAVING COUNT(*) > 1)

test_account_plan_looks_up_ranked_copies_by_key now asserts the covering candidate scan and bans a scan of every key (copy_key>?). It fails on 30547f1 and on 9602aec, and passes on 868e450.

Bench inside the image

Image built from 868e450 (linux/arm64, Python 3.12.16, SQLite 3.40.1). The bench script is unchanged. One container alternated head and 3af5d34 five times; the podman VM has 2 GiB of memory. Separate containers drifted by up to 2x for the same code, so only same-container pairs count.

Search 3af5d34 median ms head median ms ratio of medians paired ratios
none 497.1 498.6 1.00 1.72 0.65 1.02 1.57 0.62
account=a 351.1 244.0 0.69 0.85 0.70 0.67 1.32 0.64
sender=user7@ 278.3 377.5 1.36 1.34 1.26 1.33 2.54 1.28

All three ratios of medians are within 1.5x. One sender pair reads 2.54x, in a round where other pairs also swung.

Checks on 868e450

  • make check: ruff, mypy and pip-audit clean; 666 passed, 18 deselected (the model tests ran).
  • make test-integration: 18 passed against GreenMail and Stalwart.

The write set is unchanged: store.py and tests/test_copy_collapse.py only.

🤖 Generated with Claude Code

Add schema v10's guarded virtual copy_key column and covering index, with an idempotent migration using table_xinfo. Share set-based collapse queries across search, triage counts and keyword ranking while preserving filters, representative selection, pagination totals and FTS fallback. Add reference-SQL equivalence, tool-output, migration, unsafe-date and query-plan tests. Update schema assertions and document migration and benchmark usage. Existing copy tests remain unchanged. Validation: 356 filtered tests passed; make check passed with 658 tests passed, 8 skipped and 18 deselected, plus lint, typing and vulnerability audit. Benchmark medians on voyager, macOS arm64, Python 3.14.5, SQLite 3.50.4; identical script, 210,000 rows, one warm-up and five runs. Baseline 3af5d34 used a temporary source export; new code ran in the dispatch worktree. Search | baseline ms | new ms | ratio none | 438.660 | 249.447 | 0.569x account=a | 320.699 | 210.141 | 0.655x sender=user7@ | 284.263 | 318.800 | 1.121x All three meet the 1.5x bound. Optional ungated FTS medians: lorem 586.923 -> 714.880 ms; 1234 0.054 -> 0.154 ms. The approved spec-time no-schema prototype missed at 1.66–1.69x, supporting the v10 approach under ruling R1. Closes #96 Co-Authored-By: gpt-6.1-sol <noreply@openai.com> ## Rework at the gate (architect, 2026-11-01) Two commits after 30547f1, on the operator's rework rider: build the duplicate-key set from rows that pass the filters, and show the bench inside the image. - **9602aec** applied the filters inside `dup`. Inside the image this was slower. SQLite 3.40.1 does not treat the index on the virtual `copy_key` column as covering, so grouping a broad filter by key read every row. - **868e450** finds candidate Message-IDs from `messages_copies (message_id, from_addr)`, which covers. It then groups only their filter-passing rows by `copy_key`. The duplicate-key set is built from filter-passing rows only. ```diff -WITH dup AS (SELECT copy_key FROM messages WHERE copy_key IS NOT NULL - GROUP BY copy_key HAVING COUNT(*) > 1) +WITH cand AS MATERIALIZED (SELECT message_id FROM messages WHERE message_id IS NOT NULL + GROUP BY message_id HAVING COUNT(*) > 1), + dup AS (SELECT c.copy_key FROM cand x CROSS JOIN messages c ON c.message_id = x.message_id + WHERE c.copy_key IS NOT NULL {filters} GROUP BY c.copy_key HAVING COUNT(*) > 1) ``` `test_account_plan_looks_up_ranked_copies_by_key` now asserts the covering candidate scan and bans a scan of every key (`copy_key>?`). It fails on 30547f1 and on 9602aec, and passes on 868e450. ### Bench inside the image Image built from 868e450 (linux/arm64, Python 3.12.16, SQLite 3.40.1). The bench script is unchanged. One container alternated head and 3af5d34 five times; the podman VM has 2 GiB of memory. Separate containers drifted by up to 2x for the same code, so only same-container pairs count. | Search | 3af5d34 median ms | head median ms | ratio of medians | paired ratios | |---|---|---|---|---| | none | 497.1 | 498.6 | 1.00 | 1.72 0.65 1.02 1.57 0.62 | | account=a | 351.1 | 244.0 | 0.69 | 0.85 0.70 0.67 1.32 0.64 | | sender=user7@ | 278.3 | 377.5 | 1.36 | 1.34 1.26 1.33 2.54 1.28 | All three ratios of medians are within 1.5x. One sender pair reads 2.54x, in a round where other pairs also swung. ### Checks on 868e450 - `make check`: ruff, mypy and pip-audit clean; 666 passed, 18 deselected (the model tests ran). - `make test-integration`: 18 passed against GreenMail and Stalwart. The write set is unchanged: `store.py` and `tests/test_copy_collapse.py` only. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Add schema v10's guarded virtual copy_key column and covering index, with an idempotent migration using table_xinfo. Share set-based collapse queries across search, triage counts and keyword ranking while preserving filters, representative selection, pagination totals and FTS fallback.

Add reference-SQL equivalence, tool-output, migration, unsafe-date and query-plan tests. Update schema assertions and document migration and benchmark usage. Existing copy tests remain unchanged.

Validation: 356 filtered tests passed; make check passed with 658 tests passed, 8 skipped and 18 deselected, plus lint, typing and vulnerability audit.

Benchmark medians on voyager, macOS arm64, Python 3.14.5, SQLite 3.50.4; identical script, 210,000 rows, one warm-up and five runs. Baseline 3af5d34 used a temporary source export; new code ran in the dispatch worktree.

Search | baseline ms | new ms | ratio
none | 438.660 | 249.447 | 0.569x
account=a | 320.699 | 210.141 | 0.655x
sender=user7@ | 284.263 | 318.800 | 1.121x

All three meet the 1.5x bound. Optional ungated FTS medians: lorem 586.923 -> 714.880 ms; 1234 0.054 -> 0.154 ms. The approved spec-time no-schema prototype missed at 1.66–1.69x, supporting the v10 approach under ruling R1.

Closes #96
Co-Authored-By: gpt-6.1-sol <noreply@openai.com>
Without a query, the dup CTE grouped every copy key in the table. Under
the image's SQLite 3.40.1 a sender filter then ranked all copies, about
2x slower than before the collapse. The filters now apply inside dup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Grouping filter-passing rows by copy_key read every row of a broad
filter: under SQLite 3.40.1 the virtual copy_key index does not cover.
Candidates now come from messages_copies, which covers message_id, and
only their filter-passing rows are grouped by key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.