Skip to content

fix(storage): upgrade v7 archives to schema v8 - #850

Merged
Sinity merged 1 commit into
masterfrom
feature/fix/schema-v7-v8-upgrade
May 6, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/fix/schema-v7-v8-upgrade

Conversation

@Sinity

@Sinity Sinity commented May 6, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds the missing in-place archive upgrade from schema v7 to schema v8 so existing archives can run the daemon build merged in #847.

Problem

#847 removed the legacy batch-run ledger and bumped SCHEMA_VERSION to 8, but the declared upgrade dispatcher still stopped at v6→v7. On the production archive, polylogued started successfully but every live convergence batch failed with Database schema version 7 is incompatible with expected version 8.

Solution

Add a narrow build_v7_to_v8_upgrade_plan() that drops only the retired idx_runs_timestamp index and runs table, include that final step in older upgrade chains, and allow the sync/async schema bootstrap paths to execute the new action. Tests pin both the actual _ensure_schema() upgrade and the fact that the v8 migration carries no unrelated archive-wide work.

Ref #845

Verification

  • ruff format --check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py
  • ruff check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py
  • mypy --strict polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py
  • pytest tests/unit/storage/test_backend.py tests/unit/storage/test_provider_events_schema.py -q → 38 passed in 11.54s
  • devtools verify → verify: all checks passed
  • pre-push devtools verify --quick → verify: all checks passed

Summary by CodeRabbit

  • Chores
    • Added support for database schema version 8 upgrade, which removes the legacy batch-run ledger during the migration process.

Problem: #847 bumped the archive schema to v8 when the legacy runs ledger was removed, but the declared in-place upgrade set still stopped at v6->v7. Existing production v7 archives were rejected by the daemon after deploy.\n\nWhat changed: add the narrow v7->v8 upgrade plan that drops the retired runs index/table, include that final step in older upgrade chains, and allow both sync and async schema bootstrap paths to execute the new action.\n\nVerification: ruff format --check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py; ruff check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py; mypy --strict polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py; pytest tests/unit/storage/test_backend.py tests/unit/storage/test_provider_events_schema.py -q; devtools verify.
Copilot AI review requested due to automatic review settings May 6, 2026 20:40
@coderabbitai

coderabbitai Bot commented May 6, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 12d1b1a6-2828-41a5-95ac-b76869a72140

📥 Commits

Reviewing files that changed from the base of the PR and between 3b2f30a and 4df609a.

📒 Files selected for processing (3)
  • polylogue/storage/sqlite/schema.py
  • polylogue/storage/sqlite/schema_bootstrap.py
  • tests/unit/storage/test_backend.py

📝 Walkthrough

Walkthrough

This PR adds a new schema upgrade path (v7 to v8) that removes a retired batch-run ledger by dropping the runs table and its associated index. The upgrade path is integrated into the schema bootstrap decision logic across multiple version branches and wired into the main schema upgrade orchestration.

Changes

Schema Upgrade Path v7→v8

Layer / File(s) Summary
Type Definitions
polylogue/storage/sqlite/schema_bootstrap.py
SchemaBootstrapDecision.action type union updated to include "upgrade_v7_to_v8" literal option.
Core Upgrade Function
polylogue/storage/sqlite/schema_bootstrap.py
New public function build_v7_to_v8_upgrade_plan() generates DROP statements for the runs table and idx_runs_timestamp index.
Bootstrap Integration
polylogue/storage/sqlite/schema_bootstrap.py
decide_schema_bootstrap() logic updated across version branches (3–7) to append v7→v8 upgrade steps when SCHEMA_VERSION >= 8; v7-specific branch returns full upgrade decision.
Schema Upgrade Wiring
polylogue/storage/sqlite/schema.py
Both _ensure_schema() and ensure_schema_async() functions updated to recognize upgrade_v7_to_v8 in their upgrade action sets.
Tests & Verification
tests/unit/storage/test_backend.py
Two new tests verify v7→v8 upgrade drops the runs table and its index, and confirm the upgrade plan yields correct DROP statements.

Sequence Diagram

sequenceDiagram
    participant DB as SQLite Database
    participant Bootstrap as Schema Bootstrap
    participant Executor as Schema Upgrade Executor
    participant Verify as Verification

    DB->>Bootstrap: Check current schema version (v7)
    Bootstrap->>Bootstrap: Determine upgrade path (v7→v8)
    Bootstrap->>Executor: build_v7_to_v8_upgrade_plan()
    Executor-->>Bootstrap: [DROP TABLE runs, DROP INDEX idx_runs_timestamp]
    Bootstrap->>DB: Execute upgrade statements
    DB->>DB: Remove runs table & index
    DB-->>Executor: Upgrade complete (now v8)
    Executor->>Verify: Confirm runs table absent
    Verify-->>Executor: ✓ Upgrade successful
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

  • Sinity/polylogue#742: Introduces a similar pattern for v8→v9 schema upgrade, adding new build_v*_to_v*_upgrade_plan function and extending SchemaBootstrapDecision.action union with new version-to-version upgrade actions.

Poem

🐰 Hops through the schema with glee,
v7 to v8, runs table set free!
Drop the old ledger, clean and bright,
Database migrations done just right! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a schema upgrade path from v7 to v8, which is the primary purpose of the PR.
Description check ✅ Passed The description follows the required template with all key sections present: Summary, Problem, Solution, Verification, and Changelog reference. It clearly explains the context, the issue, the fix, and verification steps performed.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/fix/schema-v7-v8-upgrade

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds the missing SQLite archive upgrade step from schema v7 to v8 so existing production archives can be opened by runtimes expecting schema v8 (post-#847 removal of the legacy batch-run ledger).

Changes:

  • Introduces a v7→v8 upgrade plan that drops the retired runs table and idx_runs_timestamp index.
  • Wires the new upgrade action into both sync (_ensure_schema) and async (ensure_schema_async) schema bootstrap/upgrade dispatch.
  • Adds unit tests that pin (a) v7 archives upgrade in-place to v8 and (b) the v7→v8 migration remains narrowly scoped to the run ledger removal.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/unit/storage/test_backend.py Adds regression tests covering v7→v8 in-place upgrade behavior and migration scope.
polylogue/storage/sqlite/schema.py Allows _ensure_schema / ensure_schema_async to execute the new upgrade_v7_to_v8 decision path.
polylogue/storage/sqlite/schema_bootstrap.py Implements build_v7_to_v8_upgrade_plan() and integrates it into upgrade chains and bootstrap decisions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Sinity
Sinity merged commit eef06cb into master May 6, 2026
5 of 6 checks passed
@Sinity
Sinity deleted the feature/fix/schema-v7-v8-upgrade branch May 6, 2026 20:43
Sinity added a commit that referenced this pull request May 6, 2026
## Summary

Keeps `polylogued status` responsive while the daemon is converging a large archive.

## Problem

During the Sinnix deployment of #847/#850, the daemon was healthy and had live attempt state in SQLite, but `polylogued status --format json` exceeded a 30s timeout while convergence was starting. The status path was doing work that scales with archive size or takes deep archive locks: recursive blob-size walking, deep readiness imports, async insight status, and duplicate status probes.

## Solution

Make the local daemon status path bounded: blob size is reported as unmeasured `0`, FTS readiness and insight freshness use small read-only SQLite probes, and JSON rendering reuses the typed `DaemonStatus` probe results instead of recomputing them. The live busy-daemon probe that timed out before returned in 1.7s with this branch.

Ref #845

## Verification

- `ruff format --check polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `ruff check polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `mypy --strict polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `pytest tests/unit/daemon/test_daemon_status.py -q` → `8 passed in 10.84s`
- `.venv/bin/polylogued status --format json` against the live busy daemon → returned in `1.731s`
- `devtools verify` → `verify: all checks passed`
- pre-push `devtools verify --quick` → `verify: all checks passed`
Sinity added a commit that referenced this pull request May 6, 2026
## Summary

Follow-up to the daemon-first #845 work after deploying #847/#850/#851
on the workstation. The live daemon exposed two deployment-scale
problems: status payloads became huge with thousands of retryable live
cursor failures, and full-ingest attempts could look stale while the
worker was actually busy inside storage replacement. The same live run
also exposed an unindexed SQLite foreign-key path that made conversation
replacement scan millions of provider events.

## Problem

During the Sinnix switch, `polylogued status --format json` returned
quickly after #851 but emitted thousands of file paths. The active
attempt was marked stale in `full_blob_copy`, while `py-spy` showed the
daemon was actually in `_process_ingest_batch_sync ->
replace_conversation_runtime_state_sync`. The live archive has ~4.0M
`provider_events` rows, and `provider_events.source_message_id`
referenced `messages(message_id)` without an index, so deleting messages
during replacement caused repeated child-table scans and ~91GB of read
I/O before I stopped the old service.

## Solution

- Bound live cursor file samples in daemon status while preserving exact
failed/excluded/retry counts.
- Heartbeat full-ingest file scanning, blob materialization, and
worker-write handoff with current path and payload-read counters.
- Add schema v9 with indexes on `provider_events(source_message_id)` and
`attachment_refs(message_id)` so message deletion during replacement has
supporting FK indexes; v8 archives upgrade in place.
- Split live-batch helper code and the new focused tests out of
oversized modules, then refresh topology projections.

## Verification

- `ruff check polylogue/sources/live/batch.py
polylogue/sources/live/batch_support.py
polylogue/storage/sqlite/schema_bootstrap.py
tests/unit/sources/test_live_batch_support.py
tests/unit/storage/test_schema_upgrades.py
tests/unit/sources/test_live_watcher.py
tests/unit/storage/test_backend.py` -> all checks passed.
- `mypy polylogue/sources/live/batch.py
polylogue/sources/live/batch_support.py
polylogue/storage/sqlite/schema_bootstrap.py
tests/unit/sources/test_live_watcher.py
tests/unit/sources/test_live_batch_support.py
tests/unit/storage/test_backend.py
tests/unit/storage/test_schema_upgrades.py` -> success, no issues.
- `pytest tests/unit/sources/test_live_watcher.py
tests/unit/sources/test_live_batch_support.py
tests/unit/daemon/test_daemon_status.py
tests/unit/storage/test_backend.py
tests/unit/storage/test_schema_upgrades.py
tests/unit/devtools/test_verify_file_budgets.py -q` -> 110 passed.
- `devtools verify-file-budgets` -> clean.
- `devtools render-all --check` -> OK.
- `devtools verify` -> all checks passed.
- pre-push quick verification -> all checks passed.

Ref #845
Sinity added a commit that referenced this pull request May 7, 2026
## Summary

Adds the missing in-place archive upgrade from schema v7 to schema v8 so existing archives can run the daemon build merged in #847.

## Problem

#847 removed the legacy batch-run ledger and bumped `SCHEMA_VERSION` to 8, but the declared upgrade dispatcher still stopped at v6→v7. On the production archive, `polylogued` started successfully but every live convergence batch failed with `Database schema version 7 is incompatible with expected version 8`.

## Solution

Add a narrow `build_v7_to_v8_upgrade_plan()` that drops only the retired `idx_runs_timestamp` index and `runs` table, include that final step in older upgrade chains, and allow the sync/async schema bootstrap paths to execute the new action. Tests pin both the actual `_ensure_schema()` upgrade and the fact that the v8 migration carries no unrelated archive-wide work.

Ref #845

## Verification

- `ruff format --check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py`
- `ruff check polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py`
- `mypy --strict polylogue/storage/sqlite/schema.py polylogue/storage/sqlite/schema_bootstrap.py tests/unit/storage/test_backend.py`
- `pytest tests/unit/storage/test_backend.py tests/unit/storage/test_provider_events_schema.py -q` → `38 passed in 11.54s`
- `devtools verify` → `verify: all checks passed`
- pre-push `devtools verify --quick` → `verify: all checks passed`

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

* **Chores**
  * Added support for database schema version 8 upgrade, which removes the legacy batch-run ledger during the migration process.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sinity added a commit that referenced this pull request May 7, 2026
## Summary

Keeps `polylogued status` responsive while the daemon is converging a large archive.

## Problem

During the Sinnix deployment of #847/#850, the daemon was healthy and had live attempt state in SQLite, but `polylogued status --format json` exceeded a 30s timeout while convergence was starting. The status path was doing work that scales with archive size or takes deep archive locks: recursive blob-size walking, deep readiness imports, async insight status, and duplicate status probes.

## Solution

Make the local daemon status path bounded: blob size is reported as unmeasured `0`, FTS readiness and insight freshness use small read-only SQLite probes, and JSON rendering reuses the typed `DaemonStatus` probe results instead of recomputing them. The live busy-daemon probe that timed out before returned in 1.7s with this branch.

Ref #845

## Verification

- `ruff format --check polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `ruff check polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `mypy --strict polylogue/daemon/status.py tests/unit/daemon/test_daemon_status.py`
- `pytest tests/unit/daemon/test_daemon_status.py -q` → `8 passed in 10.84s`
- `.venv/bin/polylogued status --format json` against the live busy daemon → returned in `1.731s`
- `devtools verify` → `verify: all checks passed`
- pre-push `devtools verify --quick` → `verify: all checks passed`
Sinity added a commit that referenced this pull request May 7, 2026
## Summary

Follow-up to the daemon-first #845 work after deploying #847/#850/#851
on the workstation. The live daemon exposed two deployment-scale
problems: status payloads became huge with thousands of retryable live
cursor failures, and full-ingest attempts could look stale while the
worker was actually busy inside storage replacement. The same live run
also exposed an unindexed SQLite foreign-key path that made conversation
replacement scan millions of provider events.

## Problem

During the Sinnix switch, `polylogued status --format json` returned
quickly after #851 but emitted thousands of file paths. The active
attempt was marked stale in `full_blob_copy`, while `py-spy` showed the
daemon was actually in `_process_ingest_batch_sync ->
replace_conversation_runtime_state_sync`. The live archive has ~4.0M
`provider_events` rows, and `provider_events.source_message_id`
referenced `messages(message_id)` without an index, so deleting messages
during replacement caused repeated child-table scans and ~91GB of read
I/O before I stopped the old service.

## Solution

- Bound live cursor file samples in daemon status while preserving exact
failed/excluded/retry counts.
- Heartbeat full-ingest file scanning, blob materialization, and
worker-write handoff with current path and payload-read counters.
- Add schema v9 with indexes on `provider_events(source_message_id)` and
`attachment_refs(message_id)` so message deletion during replacement has
supporting FK indexes; v8 archives upgrade in place.
- Split live-batch helper code and the new focused tests out of
oversized modules, then refresh topology projections.

## Verification

- `ruff check polylogue/sources/live/batch.py
polylogue/sources/live/batch_support.py
polylogue/storage/sqlite/schema_bootstrap.py
tests/unit/sources/test_live_batch_support.py
tests/unit/storage/test_schema_upgrades.py
tests/unit/sources/test_live_watcher.py
tests/unit/storage/test_backend.py` -> all checks passed.
- `mypy polylogue/sources/live/batch.py
polylogue/sources/live/batch_support.py
polylogue/storage/sqlite/schema_bootstrap.py
tests/unit/sources/test_live_watcher.py
tests/unit/sources/test_live_batch_support.py
tests/unit/storage/test_backend.py
tests/unit/storage/test_schema_upgrades.py` -> success, no issues.
- `pytest tests/unit/sources/test_live_watcher.py
tests/unit/sources/test_live_batch_support.py
tests/unit/daemon/test_daemon_status.py
tests/unit/storage/test_backend.py
tests/unit/storage/test_schema_upgrades.py
tests/unit/devtools/test_verify_file_budgets.py -q` -> 110 passed.
- `devtools verify-file-budgets` -> clean.
- `devtools render-all --check` -> OK.
- `devtools verify` -> all checks passed.
- pre-push quick verification -> all checks passed.

Ref #845
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.

2 participants