Skip to content

feat: add bounded embedding preflight - #1531

Merged
Sinity merged 3 commits into
masterfrom
feature/feat/bounded-embedding-preflight
May 25, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/feat/bounded-embedding-preflight

Conversation

@Sinity

@Sinity Sinity commented May 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds bounded polylogue embed preflight windows and makes embed backfill preview the exact bounded batch it is about to run. The pending conversation selector now uses conversation_stats when available, so small operator windows do not first aggregate every message in the archive.

Problem

Semantic embedding catch-up is intentionally bounded by --max-conversations and --max-messages, but preflight still estimated the full backlog. Worse, the pending-window query grouped across messages before returning a small window; on the live archive, polylogue embed preflight --max-conversations 10 could time out instead of showing a safe first batch.

Solution

embed preflight accepts the same conversation/message window options as backfill. Backfill passes those options into the confirmation preflight so the operator sees the real planned window. select_pending_conversation_window now reads conversation_stats.message_count when present, falling back to per-conversation counts only when stats are absent, and keeps SQL fragments within the storage SQL-audit contract.

Verification

  • pytest -q tests/unit/cli/test_embed_activation.py::TestPreflightCommand tests/unit/cli/test_embed_activation.py::TestBackfillCommand::test_backfill_passes_bounded_window_options tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_conversations tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_messages tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_conversation_stats_when_available --tb=short — 6 passed.
  • pytest -q tests/unit/storage/test_no_string_interpolated_sql.py::test_no_unaudited_string_interpolated_sql tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_conversation_stats_when_available tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_messages --tb=short — 3 passed.
  • ruff format --check polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py — passed.
  • ruff check polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py — passed.
  • python -m mypy polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py — passed.
  • POLYLOGUE_FORCE_PLAIN=1 timeout 30 polylogue embed preflight --max-conversations 10 — returned a 10-conversation, 2,034-message live archive estimate.
  • devtools verify --quick — passed.
  • devtools verify — passed: 9,450 passed, 2 xfailed under pytest-testmon.

Ref #1503

Summary by CodeRabbit

  • New Features

    • Added --max-conversations and --max-messages options to the embed preflight command for bounded window analysis
    • Preflight reports now indicate windowed status and applied limits
    • Backfill command's preflight step now respects window boundaries
  • Performance

    • Improved pending conversation selection using precomputed statistics when available
  • Documentation

    • Updated command reference locations

Review Change Stack

Problem: semantic embedding catch-up can already be bounded by conversation or message count, but the preflight path still estimated the entire backlog and the selector grouped over every message before returning small windows. On the live archive, a ten-conversation preflight could time out before showing the operator a safe first batch.

What changed: add bounded preflight options, make backfill confirmation preview the same bounded window it will execute, and have pending-window selection use conversation_stats when available so small windows avoid whole-archive message aggregation.

Verification: pytest -q tests/unit/cli/test_embed_activation.py::TestPreflightCommand tests/unit/cli/test_embed_activation.py::TestBackfillCommand::test_backfill_passes_bounded_window_options tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_conversations tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_messages tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_conversation_stats_when_available --tb=short; ruff format --check polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py; ruff check polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py; python -m mypy polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/cli/test_embed_activation.py tests/unit/storage/test_embedding_contracts.py; POLYLOGUE_FORCE_PLAIN=1 timeout 30 polylogue embed preflight --max-conversations 10; devtools verify --quick
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, we couldn't start this review because you've used your available PR reviews for now.

Your plan includes 1 review of capacity. Refill in 50 minutes and 29 seconds.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more review capacity refills, 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 trial, open-source, and free plans. In all cases, review capacity refills continuously over time.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: da52f862-b6b5-447f-b62d-ddd98d927174

📥 Commits

Reviewing files that changed from the base of the PR and between 8959700 and 88cd57b.

📒 Files selected for processing (4)
  • polylogue/api/__init__.py
  • polylogue/cli/commands/embed.py
  • polylogue/storage/embeddings/materialization.py
  • tests/unit/storage/test_embedding_contracts.py
📝 Walkthrough

Walkthrough

The PR extends embedding CLI preflight and backfill commands to accept optional window limits (--max-conversations, --max-messages), scoping pending conversation/message counts to a bounded subset instead of full backlog. PreflightReport now carries windowing metadata; pending counting branches to use select_pending_conversation_window when limits are provided; and that SQL query conditionally uses precomputed conversation_stats table for message counts when available.

Changes

Windowed Pending Conversation Limiting

Layer / File(s) Summary
Data Structure & Signature Updates
polylogue/cli/commands/embed.py
PreflightReport gains windowed, max_conversations, max_messages fields; _read_pending_message_count and _build_preflight_report signatures accept optional window limit parameters.
CLI Option Integration & Command Updates
polylogue/cli/commands/embed.py
embed preflight command adds --max-conversations and --max-messages click options; embed backfill rebuilds its preflight estimate using the selected backfill window limits.
Window-Aware Counting & Report Population
polylogue/cli/commands/embed.py
_build_preflight_report calls _read_pending_message_count with provided window limits; _read_pending_message_count branches to select_pending_conversation_window when limits are provided; report populates windowing metadata and window-scoped pending totals.
Window-Conditional Output Rendering
polylogue/cli/commands/embed.py
_render_preflight prints window-specific or overall pending conversation/message counts depending on whether report.windowed is true.
SQL Optimization Using Conversation Stats
polylogue/storage/embeddings/materialization.py
select_pending_conversation_window conditionally uses conversation_stats table (when present) for precomputed per-conversation message counts, with fallback to subquery when unavailable, replacing prior approach that always joined messages and counted rows.
CLI & Storage Testing
tests/unit/cli/test_embed_activation.py, tests/unit/storage/test_embedding_contracts.py
Adds TestPreflightCommand test verifying window options are forwarded; updates TestBackfillCommand test to assert window limit propagation; introduces conversation_stats table DDL helper and test confirming select_pending_conversation_window uses conversation stats when available.
Reference Documentation Update
docs/verification-catalog.md
Updates polylogue embed subcommand source-location line numbers in verification catalog.

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly Related PRs

  • Sinity/polylogue#1327: Extends the embedding activation flow by introducing initial window-bounded preflight/backfill capability that this PR now integrates into the CLI commands and optimizes with conversation_stats SQL branching.

Poem

🐰 Windowed wisdom flows so clear,
Bounded counts bring balance here,
Stats pre-counted, swift and lean,
Pending conversations foreseen!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: add bounded embedding preflight' clearly and concisely describes the main feature addition in the changeset.
Description check ✅ Passed The PR description follows the template structure with all required sections (Summary, Problem, Solution, Verification, Changelog) completed with substantial detail.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/bounded-embedding-preflight

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 89597007d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@polylogue/cli/commands/embed.py`:
- Line 74: The CLI is importing a substrate internal function
select_pending_conversation_window from
polylogue.storage.embeddings.materialization; change the CLI to call a public
API entrypoint instead—add or expose a wrapper in polylogue.api
(api/__init__.py) that calls select_pending_conversation_window (or equivalent
materialization logic) and then update polylogue/cli/commands/embed.py to import
from polylogue.api and call that public function (e.g., a newly exported
select_pending_conversation_window or named wrapper) so the CLI only depends on
the public api surface.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: bf4630db-ac78-4c29-8b47-e0220eea6c05

📥 Commits

Reviewing files that changed from the base of the PR and between f42f053 and 8959700.

📒 Files selected for processing (5)
  • docs/verification-catalog.md
  • polylogue/cli/commands/embed.py
  • polylogue/storage/embeddings/materialization.py
  • tests/unit/cli/test_embed_activation.py
  • tests/unit/storage/test_embedding_contracts.py

Comment thread polylogue/cli/commands/embed.py Resolved
Problem: the bounded embedding selector used conversation_stats for every bounded window. Those stats can lag direct message writes, which makes --max-messages and bounded cost preflight underestimate the real selected batch.

What changed: keep conversation_stats for conversation-count windows, but force live message counts whenever max_messages is present. Add a regression with stale stats proving the message cap follows actual message rows.

Verification: pytest -q tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_conversation_stats_when_available tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_live_counts_for_message_bound tests/unit/storage/test_embedding_contracts.py::test_pending_window_honors_max_messages tests/unit/cli/test_embed_activation.py::TestPreflightCommand::test_preflight_passes_bounded_window_options --tb=short; ruff format --check polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py; ruff check polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py; python -m mypy polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py
@Sinity

Sinity commented May 25, 2026

Copy link
Copy Markdown
Owner Author

Addressed the review finding in eedd014. is now used only when is absent; message-bounded windows use live message counts so the cap and cost estimate are not understated by stale stats. Added with deliberately stale stats. Focused checks and pre-push {
"timestamp": "2026-05-25T01:18:29.552836+00:00",
"git_head": "eedd014d5e9e664b932a61b2594cdad5adbe8883",
"tier": "quick",
"steps": [
{
"name": "ruff format",
"duration_s": 0.01,
"exit": 0
},
{
"name": "ruff check",
"duration_s": 0.01,
"exit": 0
},
{
"name": "mypy",
"duration_s": 0.49,
"exit": 0
},
{
"name": "render-all",
"duration_s": 2.56,
"exit": 0
},
{
"name": "verify-topology",
"duration_s": 0.25,
"exit": 0
},
{
"name": "verify-layering",
"duration_s": 0.83,
"exit": 0
},
{
"name": "verify-file-budgets",
"duration_s": 0.31,
"exit": 0
},
{
"name": "verify-provider-meta-policy",
"duration_s": 0.3,
"exit": 0
},
{
"name": "verify-test-ownership",
"duration_s": 1.35,
"exit": 0
},
{
"name": "verify-closure-matrix",
"duration_s": 0.26,
"exit": 0
},
{
"name": "verify-schema-roundtrip",
"duration_s": 1.15,
"exit": 0
},
{
"name": "verify-suppressions",
"duration_s": 4.99,
"exit": 0
},
{
"name": "verify-manifests",
"duration_s": 2.49,
"exit": 0
},
{
"name": "verify-ci-workflows",
"duration_s": 0.29,
"exit": 0
},
{
"name": "verify-doc-commands",
"duration_s": 2.21,
"exit": 0
},
{
"name": "verify-witness-lifecycle",
"duration_s": 1.79,
"exit": 0
},
{
"name": "verify-witness-coverage",
"duration_s": 9.99,
"exit": 0
},
{
"name": "verify-lane-assertions",
"duration_s": 1.09,
"exit": 0
},
{
"name": "verify-test-infra-currency",
"duration_s": 0.31,
"exit": 0
},
{
"name": "verify-test-clock-hygiene",
"duration_s": 1.28,
"exit": 0
},
{
"name": "verification-impact check",
"duration_s": 5.83,
"exit": 0
}
],
"total_duration_s": 38.1,
"exit_code": 0
} passed.

Problem: embed preflight imported the storage materialization helper directly from the CLI callback, crossing the CLI/substrate boundary even though this is an operator-facing public workflow.

What changed: expose select_pending_embedding_conversation_window from polylogue.api and have the CLI call that public API wrapper for bounded preflight windows.

Verification: pytest -q tests/unit/storage/test_embedding_contracts.py::test_pending_window_uses_live_counts_for_message_bound tests/unit/cli/test_embed_activation.py::TestPreflightCommand tests/unit/cli/test_embed_activation.py::TestBackfillCommand::test_backfill_passes_bounded_window_options --tb=short; ruff format --check polylogue/api/__init__.py polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py tests/unit/cli/test_embed_activation.py; ruff check polylogue/api/__init__.py polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py tests/unit/cli/test_embed_activation.py; python -m mypy polylogue/api/__init__.py polylogue/cli/commands/embed.py polylogue/storage/embeddings/materialization.py tests/unit/storage/test_embedding_contracts.py tests/unit/cli/test_embed_activation.py
@Sinity

Sinity commented May 25, 2026

Copy link
Copy Markdown
Owner Author

Review fixes pushed.

  • conversation_stats is now used only when max_messages is absent; message-bounded windows use live message counts so caps and cost estimates are not understated by stale stats.
  • Added test_pending_window_uses_live_counts_for_message_bound with deliberately stale stats.
  • Routed bounded pending-window selection through polylogue.api.select_pending_embedding_conversation_window so the CLI no longer imports the storage materialization helper directly.

Verification after the fixes:

  • focused pytest for bounded preflight/storage tests passed.
  • focused ruff/mypy passed.
  • pre-push devtools verify --quick passed at 88cd57b0.

@Sinity
Sinity merged commit 6c2b86d into master May 25, 2026
7 checks passed
@Sinity
Sinity deleted the feature/feat/bounded-embedding-preflight branch May 25, 2026 01:22
Sinity added a commit that referenced this pull request May 25, 2026
## Summary

`polylogue embed status` now prints explicit bounded next actions for
semantic-search activation and first catch-up. `docs/search.md`
documents the same activation flow, including the distinction between
fast conversation-count windows and hard live-count message windows.

## Problem

After #1531/#1532, the mechanics for bounded embedding catch-up and
daemon progress existed, but the operator still had to infer the safe
first commands from status fields and docs spread across
search/architecture. On the live archive this matters: embeddings are
disabled, the key is present, and there are 7,951 pending conversations,
so the next step must be obvious and bounded.

## Solution

Embedding status now prints:

- activation command when config is disabled,
- bounded preflight command,
- bounded backfill command,
- daemon-enabled wording when the daemon stage is already enabled.

Search docs now include the activation/catch-up sequence and explicitly
state that `--max-messages` uses live counts because it is a hard cap,
while `--max-conversations` may use materialized stats for speed.

## Verification

- `pytest -q tests/unit/cli/test_embed_status_fast.py --tb=short` — 8
passed.
- `ruff format --check polylogue/cli/shared/embed_stats.py
tests/unit/cli/test_embed_status_fast.py` — passed.
- `ruff check polylogue/cli/shared/embed_stats.py
tests/unit/cli/test_embed_status_fast.py` — passed.
- `python -m mypy polylogue/cli/shared/embed_stats.py
tests/unit/cli/test_embed_status_fast.py` — passed.
- `POLYLOGUE_FORCE_PLAIN=1 timeout 20 polylogue embed status` — live
read-only smoke showed the new activation/preflight/backfill lines
against the real archive.
- `devtools render-all --check` — passed after refreshing the docs site
cache.
- `devtools verify --quick` — passed.

Ref #1503
@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

  • 73.F058 / P2: fixed-already at the checked revision: The archive window at polylogue/storage/embeddings/materialization.py:635 reads ds.message_count from the current desired-message relation, and its ceiling at :628 uses that same count. Stale conversation_stats is no longer the eligibility/window-sizing authority.

Only the rows listed above are dispositioned. Held findings remain open.

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