Skip to content

fix(demo): self-heal stale demo schema and fix tour budget attribution - #3515

Closed
Sinity wants to merge 1 commit into
masterfrom
feature/demo/self-heal-seed-and-tour-budget
Closed

Sinity wants to merge 1 commit into
masterfrom
feature/demo/self-heal-seed-and-tour-budget

Conversation

@Sinity

@Sinity Sinity commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes two failures in the documented, private-data-free onboarding path that CLAUDE.md and TESTING.md point a cold agent at instead of the live archive: polylogue demo seed failed hard on a stale-schema demo archive with no self-heal, and polylogue demo tour overran its own 30s "first result" latency budget due to a timer-attribution bug.

Problem

Two related failures reported in polylogue-3ycw:

  1. polylogue demo seed against a demo archive left over from an older schema version raised:
    RuntimeError: index.db schema version 46 is not the current index tier version 53; move it aside and rebuild the archive root
    with no path named and no self-heal, even though a demo archive is entirely synthetic, disposable fixture data that this same command regenerates on every run.

  2. polylogue demo tour failed its own latency budget: First result: 70.934s against a 30s budget, Full tour: 86.336s against a 420s budget (which passed). The tour's own report.md showed each of the 4 narrated CLI steps individually took only 4.6-5.3s — the 70.9s "first result" number was dominated by one-time archive seed+verify setup happening before the first narrated step, not by query latency.

I reproduced both against a fresh scratch archive (POLYLOGUE_ARCHIVE_ROOT=/realm/tmp/... polylogue demo seed) — a genuinely fresh root seeds cleanly, confirming issue 1 is not a fundamental demo-seed bug but a missing self-heal for a pre-existing stale-schema demo archive at the resolved root. Reading polylogue/demo/tour.py confirmed issue 2 directly: first_result_s = time.perf_counter() - start, where start is set before the seed/verify calls, not before the narrated command loop.

Solution

  • polylogue/demo/seed.py: added _archive_root_is_demo_owned(), which treats the presence of the demo fixture source directory (demo-fixture-world-source, written only by materialize_demo_source, never by the live daemon) as strong, retroactive-compatible evidence that a root's tier databases hold only synthetic demo content. _self_heal_stale_demo_archive_tiers() then moves aside (<file>.stale-<timestamp>) and rebuilds any tier whose on-disk schema version predates the current code's — but only when that ownership check passes. A stale-schema foreign root (e.g. an operator forgot --root and the default archive_root() resolved to the configured production archive) is never auto-rebuilt; it still raises loudly, protecting the live/durable archive from any auto-mutation.
  • polylogue/storage/sqlite/archive_tiers/bootstrap.py: the (still-raised, for non-demo-owned roots) RuntimeError now names the full path and an explicit rebuild command (polylogue ops reset --index && polylogued run for index.db, mv <path> <path>.stale otherwise) instead of just the bare filename with no remediation.
  • polylogue/demo/tour.py: rebased first_result_s's clock (query_phase_start) to start after the one-time seed+verify setup completes, so the budget measures actual query latency instead of cold-start archive construction. total_duration_s is unchanged (it already comfortably includes setup and wasn't flagged as wrong).
  • polylogue/demo/models.py, polylogue/cli/commands/demo.py: surface healed_tiers on DemoSeedResult and in the CLI seed command's plain-text output.

Verification

Reproduced fresh (scratch POLYLOGUE_ARCHIVE_ROOT, no live archive touched):

$ POLYLOGUE_ARCHIVE_ROOT=/realm/tmp/demo-seed-clean/archive polylogue demo seed
Demo archive ready
  ...
$ # rolled index.db's PRAGMA user_version back to 46 on that same demo-owned archive
$ POLYLOGUE_ARCHIVE_ROOT=/realm/tmp/demo-seed-clean/archive polylogue demo seed
Demo archive ready
  ...
  Self-healed: index.db (stale schema moved aside and rebuilt)
  Verify:       polylogue demo verify
$ polylogue demo tour --out-dir /realm/tmp/demo-tour-clean
Polylogue demo tour: passed
  First result: 2.680s
  Full tour:    19.651s
  • devtools test tests/unit/demo/ tests/unit/cli/test_demo_command.py tests/unit/storage/test_index_fast_forward_executor.py — 59 passed.
  • New tests:
    • test_seed_demo_archive_self_heals_a_stale_schema_on_a_demo_owned_root — rolls an already-seeded demo archive's index.db back to an old PRAGMA user_version and asserts a second seed_demo_archive() call self-heals (healed_tiers == ("index.db",)) instead of raising.
    • test_seed_demo_archive_never_self_heals_a_non_demo_owned_root — a stale-schema root without the demo fixture source directory still raises the original RuntimeError; nothing is moved aside. This is the control proving self-heal never touches a foreign/live archive.
    • test_first_result_budget_excludes_one_time_archive_setup (new file tests/unit/demo/test_demo_tour_timing.py) — injects a real 6s delay around the actual seed_demo_archive call and shrinks the budget to 6s; asserts the injected delay does not leak into first_result_s.
  • Anti-vacuity confirmed by hand: temporarily reverting the tour.py timer rebase (first_result_s = time.perf_counter() - start) made the new tour test fail exactly as expected — first_result_s=9.5s against the shrunk 6s budget, with problems=('first result exceeded 6s budget: 9.505s',). Restored the fix before committing.
  • devtools verify --quick: exit 0 (ruff format/check, mypy --strict, render all --check, layering/closure-matrix/schema-versioning lanes, pre-push hook run automatically on git push).

Acceptance criteria (from polylogue-3ycw)

  • (a) "demo seed should detect+auto-rebuild a stale schema version rather than erroring with a manual, underspecified remediation instruction" — satisfied, scoped to demo-owned roots only (see Solution above for why an unconditional auto-rebuild would be unsafe).
  • (b) "demo tour's budget should exclude one-time fixture/archive setup from the 'first result' timer" — satisfied.
  • The demo verify "0/expected" cascade reported in the bead was a downstream symptom of (a) (verifying a never-actually-seeded archive); no separate fix needed once (a) lands.

Ref polylogue-3ycw

Problem:
`polylogue demo seed`/`demo tour` are the documented private-data-free
onboarding path (CLAUDE.md, TESTING.md), but both failed on first try
(polylogue-3ycw):

1. `demo seed` against a demo archive left over from an older schema
   version raised `RuntimeError: index.db schema version N is not the
   current index tier version M; move it aside and rebuild the archive
   root` with no path named and no self-heal, even though a demo archive
   is entirely synthetic, disposable fixture data that this same command
   regenerates every run.
2. `demo tour` failed its own 30s "first result" latency budget (70.9s
   measured) even though each of the 4 narrated CLI steps individually
   took only 4.6-5.3s: `first_result_s` was measured from the tour's very
   start, so it silently included one-time seed+verify archive-construction
   time rather than actual query latency.

Solution:
- `polylogue/demo/seed.py`: add `_archive_root_is_demo_owned()`, which
  treats the presence of the demo fixture source directory
  (`demo-fixture-world-source`, written only by `materialize_demo_source`,
  never by the live daemon) as proof a root's tier databases hold only
  synthetic content. `_self_heal_stale_demo_archive_tiers()` then moves
  aside (`<file>.stale-<ts>`) and rebuilds any tier whose on-disk schema
  version predates the current code, but ONLY when that ownership check
  passes -- a stale-schema *foreign* root (e.g. an operator forgot
  `--root` and the default `archive_root()` resolved to the configured
  production archive) is never auto-rebuilt, it still raises loudly.
- `polylogue/storage/sqlite/archive_tiers/bootstrap.py`: the (still-raised,
  for non-demo-owned roots) `RuntimeError` now names the full path and an
  explicit rebuild command instead of just the bare filename.
- `polylogue/demo/tour.py`: rebase `first_result_s`'s clock to start after
  the one-time seed+verify setup completes, so the budget measures actual
  query latency instead of cold-start archive construction.
- `polylogue/demo/models.py`, `polylogue/cli/commands/demo.py`: surface
  `healed_tiers` on `DemoSeedResult`/the CLI `seed` command output.

Verification:
- Reproduced fresh: `POLYLOGUE_ARCHIVE_ROOT=<scratch> polylogue demo seed`
  succeeds cleanly; rolling `index.db`'s `PRAGMA user_version` back on that
  same (demo-owned) archive and re-running `demo seed` now self-heals
  (`healed_tiers: ["index.db"]`, `index.db.stale-<ts>` backup written)
  instead of raising. `polylogue demo tour --out-dir <scratch>` passes:
  "First result: 2.680s / Full tour: 19.651s".
- `devtools test tests/unit/demo/ tests/unit/cli/test_demo_command.py
  tests/unit/storage/test_index_fast_forward_executor.py` -- 59 passed.
- New tests: `test_seed_demo_archive_self_heals_a_stale_schema_on_a_demo_owned_root`
  and `test_seed_demo_archive_never_self_heals_a_non_demo_owned_root`
  (tests/unit/demo/test_demo_seed_verify.py), and
  `test_first_result_budget_excludes_one_time_archive_setup`
  (tests/unit/demo/test_demo_tour_timing.py, new file). Anti-vacuity
  confirmed by hand: reverting the `tour.py` timer rebase makes the new
  tour test fail with `first_result_s=9.5s` against a shrunk 6s budget and
  a "first result exceeded" problem; the self-heal tests exercise the real
  `polylogue.demo.seed.seed_demo_archive` production entry point.
- `devtools verify --quick`: exit 0 (ruff format/check, mypy --strict,
  render all --check, layering/closure-matrix/schema-versioning lanes).

Ref polylogue-3ycw

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 1, 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: 10 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: a029ad81-8f53-4086-9ef5-72b1aa55d2fe

📥 Commits

Reviewing files that changed from the base of the PR and between 078d3c0 and d81f668.

📒 Files selected for processing (7)
  • polylogue/cli/commands/demo.py
  • polylogue/demo/models.py
  • polylogue/demo/seed.py
  • polylogue/demo/tour.py
  • polylogue/storage/sqlite/archive_tiers/bootstrap.py
  • tests/unit/demo/test_demo_seed_verify.py
  • tests/unit/demo/test_demo_tour_timing.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.

@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: d81f66870b

ℹ️ 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".

Comment thread polylogue/demo/seed.py Resolved
Comment on lines +218 to +221
rebuild_command = (
"polylogue ops reset --index && polylogued run"
if tier is ArchiveTier.INDEX
else f"mv {path} {path}.stale"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Target the stale archive in the rebuild command

When initialize_archive_database() is called for a path outside the configured archive root, this executable hint resets a different archive: ops reset resolves its target exclusively through archive_root() (polylogue/cli/commands/reset.py:49-56), while polylogued run --root controls watched source roots rather than the archive (polylogue/daemon/cli.py:2685-2692). This occurs with explicit demo roots and library callers; copying the hint can delete the configured production index while leaving the reported stale database untouched. Scope the command with POLYLOGUE_ARCHIVE_ROOT=<path.parent> or avoid presenting it as a directly runnable example.

Useful? React with 👍 / 👎.

@Sinity

Sinity commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #3516, which cherry-picks this PR's self-heal commit and adds the archive-root collision guard (polylogue-o3a1t) plus the ownership-manifest hardening (polylogue-wyvio) in one coherent branch. Closing to avoid parallel unmerged branches touching the same functions.

@Sinity Sinity closed this Aug 1, 2026
Sinity added a commit that referenced this pull request Aug 1, 2026
…socket and demo-hardening findings

Merged PR #3511 (polylogue-3b607) and PR #3512 (polylogue-373yt), closed
both. Filed polylogue-kadx3 (P0: daemon UDS socket path not archive-scoped,
found live during PR #3517 perf work) and polylogue-dl6af (3 new review
findings on PR #3516's demo self-heal guard). Closed PR #3515 as superseded
by #3516.
Sinity added a commit that referenced this pull request Aug 1, 2026
…3516)

## Summary

`polylogue demo seed` shares its default archive-root resolution
(`archive_root()`/`polylogue.toml`) with the live daemon, so with no
`--root`/`POLYLOGUE_ARCHIVE_ROOT` override it can silently seed
synthetic
fixture content into a real production archive. Separately, the
self-heal
gate added by PR #3515 (`_archive_root_is_demo_owned()`) only proved "a
demo
seed touched this root at some point", not "this root is exclusively
synthetic" — so a root that ever received demo content via that same
collision (or a deliberate `--force`) could later have a schema mismatch
authorize moving aside real, potentially irreplaceable `user.db` data.
This
PR closes both.

## Problem

- **polylogue-o3a1t**: `demo seed` *adds* demo sessions rather than
refusing
to run against existing content. An operator or agent who forgets
`--root`
gets ambient resolution through `polylogue.toml`/XDG defaults — on a
real
  machine this can be the live archive.
- **polylogue-wyvio**: PR #3515's self-heal gate trusts
`DEMO_SOURCE_DIRNAME`'s mere presence as proof a root's tiers are safe
to
  move aside and rebuild on a schema mismatch. That marker persists
unconditionally, including on a root that already held real content when
  first seeded, so a later schema drift on that mixed-content root could
  authorize moving aside `source.db`/`user.db` (durable, irreplaceable
assertions/corrections/saved queries), not just the rebuildable
`index.db`.

Both stem from the same root cause (demo seed sharing `archive_root()`
with
the live daemon) and are fixed together per the review finding on #3515.

## Solution

- `polylogue/demo/seed.py`:
- `_guard_demo_seed_target()` raises `DemoSeedTargetUnsafeError` (naming
the resolved path and session count) when the root is not demo-owned,
already holds real ingested sessions, and neither an explicit root nor
    `--force` was given. A brand-new/empty root is unaffected.
  - Replaced the marker-based `_archive_root_is_demo_owned()` with a
    manifest-backed version. `_record_demo_ownership_if_undetermined()`
writes a permanent `demo-archive-ownership.json` the *first* time a root
is ever touched, recording whether it held real content at that exact
moment. Self-heal now trusts only this manifest — a root that ever held
    real content (even via `--force`) can never regain self-heal
eligibility by being reseeded later; its schema mismatches keep raising
    loudly instead of silently rebuilding.
- `polylogue/cli/commands/demo.py`, `polylogue/demo/tour.py`: thread
`explicit_root` through every `seed_demo_archive()` call site, computed
as
  `--root is not None or POLYLOGUE_ARCHIVE_ROOT in os.environ`, so the
  cloud/CI explicit-root lane keeps working with zero friction.

**Design choice**: option 1 from the brief (refuse + require explicit
consent) rather than moving the default off `archive_root()` entirely —
this preserves today's zero-friction default for fresh/empty
environments
(CI, cloud sandboxes, a brand-new operator machine) while gating only
the
genuinely dangerous case.

**Branch note**: this branch starts from PR #3515's self-heal commit
(`d81f66870`, cherry-picked cleanly onto current `master`) since #3515
was
still open at review time and this PR hardens the exact gate it
introduces.
#3515 and this PR should be reconciled at merge time (either merge this
one
instead of #3515, or merge #3515 first and rebase this one — they touch
the
same functions).

## Verification

Reproduced via the real CLI in scratch fixtures under `/realm/tmp/`
(never
touching `/realm/db/polylogue`):

- A synthetic non-demo archive with one real session, resolved as the
  **default** root via a scratch `polylogue.toml` with no `--root`/env
override → `polylogue demo seed` exits 1 with the guard's message naming
  the resolved path and session count.
- The same real-content archive via explicit `--root` → seeds
successfully,
  session_count 20 (19 demo + 1 real), no friction.
- The same real-content archive via explicit `POLYLOGUE_ARCHIVE_ROOT`
(the
  cloud/CI lane) → seeds successfully, session_count 20, no friction.
- A genuinely fresh/empty root via the same default-resolution path →
seeds
  cleanly, session_count 19, exit 0.

Automated:
- `devtools test tests/unit/demo/ tests/unit/cli/test_demo_command.py
tests/unit/storage/test_index_fast_forward_executor.py` — 63 passed.
- `devtools verify --quick` — exit 0 (ruff format/check, mypy --strict,
render all --check, layering/closure-matrix/schema-versioning lanes).

New/changed tests in `tests/unit/demo/test_demo_seed_verify.py`:
- `test_seed_demo_archive_refuses_the_default_root_collision`
- `test_seed_demo_archive_explicit_root_bypasses_the_collision_guard`
-
`test_seed_demo_archive_never_self_heals_a_root_that_held_real_content`
- `test_seed_demo_archive_grants_self_heal_to_a_genuinely_fresh_root`

**Anti-vacuity**: all four exercise the real
`polylogue.demo.seed.seed_demo_archive` production entry point (no
test-local reimplementation). Deleting the `_guard_demo_seed_target`
call
makes the first test fail by seeding successfully instead of raising.
Reverting `_archive_root_is_demo_owned` to trust `DEMO_SOURCE_DIRNAME`
alone
makes the third test fail (the second seed call self-heals instead of
raising the schema-mismatch error).

## AC matrix

**polylogue-o3a1t** (default-root collision):
- Refuse seeding synthetic content into a real, non-demo-owned root with
no explicit root/force — satisfied.
- Preserve zero-friction default resolution for fresh/empty roots —
satisfied.
- CI/cloud explicit-root (`POLYLOGUE_ARCHIVE_ROOT`) lane unaffected —
satisfied.

**polylogue-wyvio** (self-heal gate hardening):
- Self-heal no longer trusts marker persistence alone — satisfied.
- A root that held real content at first touch can never regain
self-heal eligibility, even after reseeding — satisfied.
- Fixed in the same lane/branch as polylogue-o3a1t — satisfied.

Ref polylogue-o3a1t
Ref polylogue-wyvio

---------

Co-authored-by: Claude <[email protected]>
@Sinity
Sinity deleted the feature/demo/self-heal-seed-and-tour-budget branch August 10, 2026 13:11
@Sinity

Sinity commented Sep 30, 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.

  • 64.F027 / P1: fixed-already at the checked revision: polylogue/demo/seed.py:340 requires an ownership manifest explicitly declaring demo_only, not just a fixture directory. Current source, user and index row identities are then checked against the recorded baseline at lines 349-360 before self-healing is authorized.

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