Skip to content

fix(demo): refuse default-root collision and harden self-heal ownership - #3516

Merged
Sinity merged 3 commits into
masterfrom
feature/fix/demo-seed-archive-root-safety-guard
Aug 1, 2026
Merged

Sinity merged 3 commits into
masterfrom
feature/fix/demo-seed-archive-root-safety-guard

Conversation

@Sinity

@Sinity Sinity commented Aug 1, 2026

Copy link
Copy Markdown
Owner

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 fix(demo): self-heal stale demo schema and fix tour budget attribution #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

Sinity and others added 2 commits August 1, 2026 14:35
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]>
…ership

Problem:
'polylogue demo seed' with no --root/POLYLOGUE_ARCHIVE_ROOT override
resolves through the same archive_root()/polylogue.toml chain the live
daemon uses -- on a real operator machine this default can silently
resolve to the live production archive. demo seed ADDS synthetic sessions
rather than refusing to run against existing content, so the collision
seeds fixture rows directly into a real archive (polylogue-o3a1t).

Separately, a review of PR #3515's self-heal gate
(_archive_root_is_demo_owned()) found it trusts DEMO_SOURCE_DIRNAME's mere
presence as proof of "safe to move aside and rebuild on a schema
mismatch". That marker only proves a demo seed touched a root at some
point, not that the root's tiers are exclusively synthetic -- so the
default-root collision above (or a --force override) leaves the marker
permanently on a MIXED-content root, and any later schema drift on that
real archive would satisfy the gate and authorize moving aside source.db
or the irreplaceable user.db (assertions/corrections/saved queries)
(polylogue-wyvio).

Solution:
- polylogue/demo/seed.py: add _guard_demo_seed_target(), which raises
  DemoSeedTargetUnsafeError naming the resolved path 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 -- there is nothing there to protect.
- Replace 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 on the FIRST call that ever
  touches a root, recording whether it held real content at that exact
  moment. Self-heal now trusts only this manifest, so 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.
- polylogue/cli/commands/demo.py, polylogue/demo/tour.py: thread
  explicit_root through every seed_demo_archive() call site so an
  operator-provided --root/POLYLOGUE_ARCHIVE_ROOT is never second-guessed
  (the cloud/CI explicit-root lane keeps working with zero friction).

Verification:
- Reproduced via the real CLI in a scratch fixture (not /realm/db/polylogue):
  a synthetic non-demo archive with one real session, resolved as the
  DEFAULT root through a scratch polylogue.toml with no --root/env
  override, made 'polylogue demo seed' exit 1 with the guard's message
  naming the resolved path. The same archive via explicit --root or
  POLYLOGUE_ARCHIVE_ROOT seeded successfully with no friction (20
  sessions = 19 demo + 1 real), matching the required CI/cloud-lane
  behavior. A genuinely fresh/empty root via the same default-resolution
  path seeded cleanly (19 sessions, exit 0).
- devtools test tests/unit/demo/ tests/unit/cli/test_demo_command.py
  tests/unit/storage/test_index_fast_forward_executor.py -- 63 passed.
  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 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).
- devtools verify --quick: exit 0 (ruff format/check, mypy --strict,
  render all --check, layering/closure-matrix/schema-versioning lanes).

AC matrix (polylogue-o3a1t):
- Refuse default-root collision against real content: satisfied.
- Preserve default resolution for fresh/empty roots: satisfied.
- CI/cloud explicit-root lane unaffected: satisfied.

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

This branch also incorporates PR #3515's self-heal commit
(d81f668, cherry-picked) as its starting point, since #3515 was still
open and this fix hardens the exact gate it introduces; #3515 and this PR
should be reconciled at merge time rather than landed independently.

Ref polylogue-o3a1t
Ref polylogue-wyvio

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: 19 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: 65e44475-0593-4b29-9909-1a74c19c3939

📥 Commits

Reviewing files that changed from the base of the PR and between 75b4b7b and 43a2dc5.

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

ℹ️ 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 thread polylogue/demo/seed.py Resolved
Comment thread polylogue/cli/commands/demo.py Resolved
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.
Problem: three CodeRabbit findings on PR #3516 (the self-heal ownership
guard for `demo seed`) surfaced gaps that would still let self-heal
silently move aside real durable archive content.

1. `demo_only: true` in the ownership manifest was permanent proof of
   exclusive demo ownership even after the same root later accumulated
   real content through normal use, so a later schema-drift self-heal
   would move aside the now-real `source.db`/`user.db`.
2. A missing or unreadable `index.db` was classified as "0 sessions",
   which could record `demo_only: true` even when the durable tiers
   (`source.db`, `user.db`) held real content -- `index.db` is the
   explicitly rebuildable tier and can legitimately be absent (e.g.
   after `polylogue ops reset --index`).
3. `POLYLOGUE_ARCHIVE_ROOT=""` was treated as an explicit root override
   by the CLI's collision-guard check, but `archive_root()` itself
   treats empty/whitespace as unset and falls back to the ambient
   default -- so a blank env var silently bypassed the guard while
   landing in the live fallback archive.

What changed:
- `polylogue/demo/seed.py`: `_archive_root_has_real_content` now checks
  `source.db`'s `raw_sessions` and `user.db`'s `assertions` directly,
  treating an unreadable tier as unsafe (not empty) and only falling
  back to the index count once durable tiers independently prove
  freshness (gap 2). `_guard_demo_seed_target` now uses this
  tier-aware predicate instead of the index-only session count.
  `_archive_root_is_demo_owned` now revalidates the manifest's
  `demo_only: true` bit against a `demo_session_ids` baseline recorded
  after every completed demo-only seed (`_refresh_demo_ownership_session_ids`);
  any current session id outside that baseline means real content
  arrived since, so self-heal refuses (gap 1).
- `polylogue/cli/commands/demo.py`: `_root_is_explicit` now strips
  `POLYLOGUE_ARCHIVE_ROOT` and checks truthiness instead of mere
  `os.environ` membership, mirroring `archive_root()`'s own emptiness
  rule (gap 3); `receipts_command` now reuses the same helper instead
  of a duplicate inline check with the same bug.

Verification:
- `devtools test tests/unit/demo/ tests/unit/cli/test_demo_command.py`
  -> 59 passed.
- Anti-vacuity: reverted each of the three fixes individually and
  confirmed its new regression test fails on the unfixed code
  (collision guard doesn't fire / manifest records `demo_only: true`
  over real durable content / self-heal proceeds without raising),
  then restored the fix and reconfirmed green.
- `devtools verify --quick` -> exit 0 (ruff format/check, mypy --strict,
  render all --check, layering, schema-versioning policy, etc., all ok).

Ref polylogue-dl6af, polylogue-o3a1t, polylogue-wyvio
@Sinity
Sinity merged commit 8c93778 into master Aug 1, 2026
3 checks passed
@Sinity
Sinity deleted the feature/fix/demo-seed-archive-root-safety-guard branch August 1, 2026 13:36
Sinity added a commit that referenced this pull request Aug 1, 2026
Merged PR #3516 (demo self-heal ownership hardening), closed
polylogue-o3a1t/wyvio/dl6af. Filed 3 beads from a devtools structural
audit: dead schema-fast-forward one-offs (polylogue-s9irb), bead-loader
duplication across 6+ files (polylogue-2ux5m), and an unregistered
benchmark command (polylogue-fjg91).
@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.F028 / P1: fixed-already at the checked revision: polylogue/demo/seed.py:204-211 reads source.db raw_sessions and user.db assertions independently of index.db and treats nonempty or unreadable durable evidence as unsafe. Missing or unreadable index evidence therefore cannot establish fresh ownership while either durable tier contains the real rows described in the finding.
  • 64.F029 / P1: fixed-already at the checked revision: The historical demo_only flag is no longer sufficient: polylogue/demo/seed.py:349-360 rereads each tier's current IDs and refuses any ID outside the prior demo baseline. An unreadable tier also refuses ownership at lines 355-358, so later real sessions or assertions invalidate self-heal eligibility.

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

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

  • 60.F011 / P1: fixed-already at the checked revision: polylogue/cli/commands/demo.py:56 strips the archive-root environment value before deciding that it is explicit. Empty and whitespace-only values no longer bypass the default-root collision guard.

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