Skip to content

fix(plugin): validate ledger migration render and preserve alias defaults (#2576) - #2614

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1487/fix-ledger-migration-alias
Oct 24, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1487/fix-ledger-migration-alias

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Priority: P1 | Area: config/plugin | Labels: fix,P1,area:config,area:plugin | Milestone: v0.0.28 | Task: CTX-1487 | Refs #2576

Summary

Follow-up to merged #2600 (commit 5a8f8b71) for the defects found in
independent review. The feature stays; this hardens migration and generated
defaults.

F1 (P1) — migration could commit a ledger the parser rejects

  • PluginState::render now wraps a capability set one entry per line when the
    inline form would approach the parser's 1024-byte line bound. A capability
    id is closed at 512 bytes, so every wrapped line fits.
  • migrate_legacy_ledger now calls the same pre-commit guard as save_state:
    it re-parses the rendered bytes and requires the same records back. A
    rejected render is a hard StateInvalid error; no Lua ledger is written and
    bitty-plugins.toml is not moved.
  • The guard is shared (validate_rendered_ledger), so a save_state render
    drift now also fails closed instead of only syntax-checking.

Exact pre-fix repro (now a regression test):
legacy TOML with two 500-byte fs.read: grants rendered a 1049-byte
granted = [...] line; migration exited 0 and moved the TOML, the next command
exited 4 StateInvalid, and doctor --repair quarantined the ledger. After
the fix the same fixture migrates, every line is <= 1024 bytes, the next
list succeeds, both grants survive, .bak matches the original bytes, and
doctor does not quarantine.

F2 (P2) — generated defaults shadowed a short-alias settings file

  • ensure_generated_plugin_config now passes the plugin as a discovered-id
    claimant, so plugin_config_paths resolves the <name>.lua alias path and
    the existing alias guard is live. An existing alias (or canonical/legacy
    file) means no canonical file is generated.
  • Regression: with plugins/cfg.lua present, install creates no
    plugins/xuepoo.cfg.lua, and config show still selects lua-alias with
    the user's show = false.

F3 (P3 docs + behavior)

  • bitty-manual: read-only plugin doctor writes nothing; the next
    state-loading command or doctor --repair migrates. .bak mode and the
    no-shadow rule are documented. Docs-only PR:
    docs(plugins): correct legacy-ledger migration trigger (#2576) bitty-manual#76 (merged).
  • doctor --repair now skips the write when the Lua ledger is current and has
    no stale records, so it no longer churns .bak or rewrites healthy bytes.

Minor hardening

  • Parser docs now describe control-character handling accurately (raw controls
    rejected; \n/\r/\t escapes decode but identifier grammars reject them).
  • Added tests for an invalid settings schema, a special-file legacy source
    (directory), and safe mode with a present invalid ledger (proves the file
    is never opened).
  • Completion-candidate tests now use bitty-plugins.lua; they were writing a
    legacy TOML at the Lua path and silently returning only store candidates.

Validation

  • cargo fmt --all -- --check
  • cargo clippy -p bitty-config -p bitty-terminal --all-targets -- -D warnings
  • cargo test -p bitty-config — 499 passed
  • cargo test -p bitty-terminal --bin bitty — 1232 passed
  • cargo test -p bitty-terminal --test cli_plugin — 53 passed
  • bitty-manual just check — prettier, markdownlint, schema verification,
    package build passed
  • New regressions: long-capability migration, migration render refusal before
    commit, alias no-shadow, doctor repair no-op, invalid schema, special-file
    legacy source, safe mode with a present ledger.

Residual risks

  • Migration I/O failures (read-only/ownership) remain best-effort warnings:
    the legacy ledger stays readable and migration retries on later commands.
    Only a rejected render is a hard failure.
  • Windows rename-over-existing still uses the pre-checked remove-then-rename
    fallback; this PR does not change it.
  • CodeRabbit was rate-limited on feat(plugin): data-only Lua ledger and generated per-plugin defaults (#2576) #2600 and may review this follow-up.

…defaults

F1: capability sets now wrap one entry per line when the inline form
would approach the parser's 1024-byte line bound. Migration re-parses
the rendered Lua and requires the same records before the atomic
commit; a rejected render is a hard StateInvalid error that leaves the
legacy TOML and writes no Lua ledger. Regression: two 500-byte fs.read
grants migrate, parse on the next command, keep both grants, and retain
the .bak.

F2: generated defaults resolve the short-alias path instead of passing
a None discovered set, so an existing <name>.lua user file is never
shadowed by a newly created canonical file. Regression covers install
with cfg.lua present and config show selecting the alias value.

F3: plugin doctor --repair leaves an already-current ledger
byte-identical and no longer replaces .bak on every run. Manual docs
now state that read-only doctor writes nothing.

Also: parser control-character docs corrected, .bak mode documented,
special-file legacy source and invalid settings schema tests added, and
a safe-mode test proves a present ledger is never opened. Candidate
tests now write/read bitty-plugins.lua.

Refs #2576
Task: CTX-1487
@Xuepoo Xuepoo added this to the v0.0.28 milestone Oct 24, 2026
@Xuepoo Xuepoo added fix Bug fix P1 Priority: high area:config Area: configuration area:plugin Area: plugin host labels Oct 24, 2026
@coderabbitai

coderabbitai Bot commented Oct 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 13 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3381c446-a129-497f-a636-0238f5ad29bc
Commits

Reviewing files that changed from the base of the PR and between 1500664 and a3070d5.

Files selected for processing: 6
  • CHANGELOG.md
  • crates/bitty-config/src/plugin_ledger.rs
  • crates/bitty-terminal/src/candidates.rs
  • crates/bitty-terminal/src/plugin.rs
  • crates/bitty-terminal/src/runtime_inspect.rs
  • crates/bitty-terminal/tests/cli_plugin.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@Xuepoo Xuepoo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent review (guard tick, separate agent) — PASS

Reviewed head a3070d5a (ctx-1487/fix-ledger-migration-alias), 6 files, +425/−73.

Checks performed:

  1. gh pr checks 2614 on head a3070d5a: 18 SUCCESS / 2 SKIPPED / 0 fail / 0 pending — Quality gates (1)+(2), Linux X11, Linux Wayland, Windows, macOS ARM64, MSRV 1.85, Supply chain, CodeQL (actions+rust), VM tier (PR), compat matrices, Extension pin drift all green.
  2. Diff scope matches the stated intent: bitty-config/src/plugin_ledger.rs (render/validate), bitty-terminal/src/{plugin,runtime_inspect,candidates}.rs, tests/cli_plugin.rs, CHANGELOG.md. No dependency-edge, no lockfile, no CI-config change.
  3. Root-cause confirmation: the main-branch red (candidates::tests::plugins_union_state_and_store_sorted, candidates.rs:415, left = ["aaa.installed","middle.installed"]) is caused by the test fixture still writing the legacy TOML ledger after the Lua-ledger migration (5a8f8b71). This PR rewrites the fixture to emit a valid bitty-plugins.lua via PluginState::render() and keeps the malformed/oversized fail-soft cases on the Lua path — i.e. it fixes the production-vs-fixture mismatch without weakening the assertions.
  4. Production reader behaviour unchanged for the legacy path: legacy TOML fallback assertions are retained in the same test module, so the migration stays backwards compatible.

Verdict: PASS. This PR is the unblock for main's red and for the four code PRs currently inheriting the same failing test (2611/2599/2588/2567, plus 2615/2616).

@Xuepoo
Xuepoo merged commit 7c5a5a9 into main Oct 24, 2026
20 checks passed
@Xuepoo
Xuepoo deleted the ctx-1487/fix-ledger-migration-alias branch October 24, 2026 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:config Area: configuration area:plugin Area: plugin host fix Bug fix P1 Priority: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant