Skip to content

[CTX-1876] fix(platform): adopt the legacy plugins-state root (bitty#3236) - #3238

Merged
Xuepoo merged 1 commit into
mainfrom
ctx-1876/windows-store-migration
Nov 18, 2026
Merged

Xuepoo merged 1 commit into
mainfrom
ctx-1876/windows-store-migration

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

What

Completes bitty#3236 (the residual bitty#3226 / PR #3230 left open): after the Windows data root began preferring %LOCALAPPDATA% over $HOME, an install that had HOME exported kept neither of the two entries it wrote to the old root.

PR #3230 moved the store. The plugins-state sibling — each plugin's <plugin-id>/store.json — was still resolved by a second, ad-hoc join at the one call site that needed it (data_home().join("bitty").join("plugins-state")), so it stayed behind in $HOME/.local/share/bitty/plugins-state: the plugins stay installed and silently start from an empty state. That is the same defect class the store had, in the entry the fix did not cover, which is why it is fixed here by removing the second resolution rather than by adding a second migration.

The policy (one resolver)

One-time migration + convergent read-only fallback, now for every data tier instead of the store only:

  • a tier's legacy root is adopted into the current root once, at resolution time (adopt_legacy_store: rename; a bounded, staged copy when the roots are on different volumes);
  • an existing current root always wins and is never merged, so discovery reads exactly one index — no second untrusted source path, R-006/R-015/R-017 unchanged;
  • if the move cannot be made, the resolver keeps reading the legacy tree (plugins stay visible, the next start retries) instead of switching to an empty root;
  • callers that must not write (--safe, bitty plugin doctor without --repair, shell completion) use the read-only variant of the same precedence and move nothing.

PluginDataTier (Store, State) plus tier_root_with_platform_env / legacy_tier_root_with_platform_env / effective_tier_root_with_platform_env / read_only_tier_root_with_platform_env are now the only place that answers "where does a data tier live". The store and the state sibling differ by one enum arm, so a future tier cannot drift out of the policy the way plugins-state did.

Surfaces

Surface Before After
store_root (startup discovery) effective_store_root_with_platform_env (PR #3230) unchanged call, now a PluginDataTier::Store wrapper over the shared body
data_home / data_home_with_platform_env base resolver, also used to build a tier path by hand base resolver only; doc now forbids building a tier from it and names the tier resolver
plugins-state data_home()…join("plugins-state") at the call site — no legacy handling startup_plugins_state_root_with_platform_env → PluginDataTier::State (+ read-only variant under --safe)
bitty plugin CLI (install/remove/doctor/list) effective_/read_only_store_root_with_platform_env (PR #3230) unchanged call; resolved through the same tier body

discover_with_roots now takes the resolved state root as a parameter instead of reading the live one inside the CTX-1115 test seam, so a test can no longer adopt — i.e. move — a developer's real plugins-state tree.

Tests (non-vacuous)

Windows-only, on the real filesystem:

  • tests/windows_legacy_store_migration.rs (#![cfg(windows)]) drives the built bitty.exe with HOME exported, %LOCALAPPDATA% set and $XDG_DATA_HOME unset, plants a real installed package (manifest + lua/init.lua, index with the real manifest_hash and content_digest) in $HOME/.local/share/bitty/plugins, and asserts bitty plugin list --format json lists it with the package name (so the bytes moved, not just the index), that the store now sits under %LOCALAPPDATA%\bitty\plugins, that the legacy tree is gone, and that a second run moves nothing.
  • tests/windows_legacy_store_migration.rs::current_windows_root_untouched_when_no_legacy_store_exists — legacy absent: the command succeeds, nothing is listed, no store is created, no legacy path is invented, and the resolver still answers afterwards.
  • plugin_runtime::tests::windows_legacy_tier_fixture (#[cfg(windows)]), same fixture shape for both tiers: store present/absent, plugins-state present/absent, --safe leaving a present legacy state tree in place while the non-safe startup adopts it, and one install where the store migrates first and the state tier still finds its own legacy tree.

Platform-independent policy tests (they run on every leg and are what the Windows fixture is checked against) keep covering the legacy-root predicate and its exhaustion: XDG set, %LOCALAPPDATA% unset, empty legacy root, existing current root, --safe, and the concurrent-resolution race.

Each Windows test asserts the observable consequence of the adoption (the tree is at the current root, the legacy one is gone, the package/state bytes are readable through it), so it fails whenever the corresponding adoption does; see Discrimination below for the run that proves the state tests bite. No Unix-only assertion is introduced, and the Windows leg compiles the new test targets under -D warnings.

Gates (local, this commit 6affd86c)

Run in the worktree with CARGO_TARGET_DIR outside it; every command below is the exact one run.

gate rc
cargo fmt --all -- --check 0
cargo clippy -p bitty-terminal --all-targets --offline -- -D warnings 0
cargo clippy -p bitty-runtime --all-targets --offline -- -D warnings 0
cargo test -p bitty-terminal --offline 0 — 1667 passed, 0 failed
cargo test -p bitty-runtime --offline 0 — 4011 passed across 243 suites, 0 failed
RUSTFLAGS="-D warnings" cargo check -p bitty-terminal --tests --target x86_64-pc-windows-gnu --offline 0
RUSTFLAGS="-D warnings" cargo check -p bitty-runtime --tests --target x86_64-pc-windows-gnu --offline 0
cargo +1.85 check -p bitty-terminal --tests --offline 0
cargo +1.85 check -p bitty-runtime --tests --offline 0
RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-terminal --no-deps --offline 0
RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-runtime --no-deps --offline 0
just scratch-paths 0

Notes:

  • One earlier full cargo test -p bitty-terminal run failed doctor::tests::dry_run_writes_nothing (no instance-socket receipt) while the machine was also building. It passes 3/3 in isolation and the suite is green in two subsequent full runs; doctor.rs is untouched by this diff.
  • just markdownlint currently reports 12 pre-existing MD060/MD049 issues in five files this PR does not touch (crates/bitty-render/assets/README.md, packaging/README.md, packaging/upgrade.md, specifications/fuzz-campaign-ctx-1877-evidence.md, vendor/winit/PATCHES.md). The CHANGELOG.md entry added here is not among them, and CI's markdownlint step passed at 5a9b3eb3, so this is local tool-resolution drift rather than a repository defect. Not fixed here.
  • just ci-local (the act leg) was not run: it builds a Docker image and needs network access this task cannot rely on. Everything above ran.

Discrimination

Removing the state tier's adoption (calling tier_root_with_platform_env directly instead of effective_tier_root_with_platform_env, i.e. the pre-fix behaviour) makes plugin_runtime::tests::legacy_plugins_state_adoption_covers_the_same_population fail with the state file moved with its tier, while legacy_plugins_state_is_never_adopted_outside_the_windows_home_population still passes — the test bites on the migration, not on the predicate. The mutation was reverted before the commit above and the tree was verified clean (git status --porcelain empty).

Documentation

  • CHANGELOG.md gains an ## [Unreleased] → ### Fixed entry (the release-notes file inside this repository; its newest section is far behind Cargo.toml, so the entry goes under Unreleased rather than inventing a version heading).
  • bitty-terminal-docs is not edited here: its working tree is shared and may hold other agents' edits. The platform-reference text is proposed below as a follow-up for that repository.

Follow-up for bitty-terminal-docs (please apply in its own PR)

configuration/lua-and-xdg.md §"Data, state, cache, and runtime layouts" is still marked candidate layouts and its tree lists plugin-state/ under $XDG_STATE_HOME/bitty/, which no longer describes the shipped code. Proposed replacement for the data tree plus a new note:

$XDG_DATA_HOME/bitty/
├── plugins/        # installed packages + current.json store index
├── plugins-state/  # per-plugin <plugin-id>/store.json (persistent plugin state)
├── themes/
└── runtime/

Platform precedence. Each data tier (plugins, plugins-state) is
resolved, in order, from $XDG_DATA_HOME, %LOCALAPPDATA%, $HOME/.local/share,
%APPDATA%, and each is resolved by the same resolver, so a tier can never
land in a different root from its sibling. The component state root follows
$XDG_STATE_HOME, %LOCALAPPDATA%, $HOME/.local/state.

Legacy roots. Before %LOCALAPPDATA% outranked $HOME (bitty#3226), a
Windows install with HOME exported wrote both tiers under
$HOME/.local/share/bitty. Bitty now adopts such a tree into the current root
once, at first start after the upgrade: the whole directory is moved (copied
through a staged, bounded copy when the two roots are on different volumes),
and only when the current root does not already exist — an existing current
root always wins and is never merged, so exactly one store index is ever
read. If the move cannot be made, Bitty keeps reading the legacy tree and
retries on the next start. --safe, bitty plugin doctor (without
--repair), and shell completion resolve the same precedence without moving
anything. No manual step is required; an operator who wants to migrate by
hand can quit Bitty and move the directory themselves.

Deliberately out of scope

  • bitty_plugin_host::grant::grants_path_for (persisted grants) still resolves $XDG_STATE_HOME > $HOME/.local/state with no %LOCALAPPDATA% arm. It is a security-relevant store, so moving it strands existing grants and needs its own migration — not a diagnostics or plugin-state projection.
  • stall_watchdog::state_file_path keeps its own order (XDG_STATE_HOME > HOME > LOCALAPPDATA); unifying the two state resolvers is a separate change.
  • config_manage resolves the third-party wheel data directory from data_home() directly. wheel owns that layout and has no Bitty tier or pre-change population in this repository.

Closes #3236

…3236)

bitty#3226 made the Windows data root prefer %LOCALAPPDATA% over $HOME.
PR #3230 adopted the plugin store from the pre-change root; the
plugins-state sibling was still resolved by a join at its one call site,
so an upgraded install kept its packages and silently lost the state they
wrote.

Resolve both entries through one data-tier resolver (PluginDataTier::
Store/State): one legacy predicate, one one-time migration, one read-only
variant for --safe, `doctor` without --repair, and shell completion. An
existing current root still wins and is never merged, so discovery reads
exactly one index and the capability model is unchanged.

discover_with_roots now takes the resolved state root instead of reading
the live one inside the CTX-1115 seam, so a test can no longer adopt a
real user's tree.

Tests: a Windows-only fixture drives the built bitty.exe (legacy store
present and absent) and a Windows unit fixture covers both tiers over the
real filesystem; the platform-independent policy tests now cover the
state tier on every leg.

Refs #3236
@Xuepoo Xuepoo added this to the v0.1.0 milestone Nov 18, 2026
@Xuepoo Xuepoo added fix Bug fix P1 Priority: high area:platform Area: platform / windowing labels Nov 18, 2026
@coderabbitai

coderabbitai Bot commented Nov 18, 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 41 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: 3f57e335-6566-49b3-bde1-43b9b1f5a5c9
Commits

Reviewing files that changed from the base of the PR and between 0b2ce65 and 6affd86.

Files selected for processing: 4

  • CHANGELOG.md
  • crates/bitty-runtime/src/plugin_runtime/mod.rs
  • crates/bitty-terminal/src/plugin_runtime.rs
  • crates/bitty-terminal/tests/windows_legacy_store_migration.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

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

Review: adopt the legacy plugins-state root (CTX-1876 / bitty#3236)

Reviewed at 6affd86cbad5fd178e76f19f4a46d5d9679d06ae. Scope: plugin_runtime.rs (tier resolver + seam), tests/windows_legacy_store_migration.rs, the PluginRuntimeConfig::data_dir doc, CHANGELOG. #3230's store adoption is already in main; this adds the State sibling to the same resolver and removes the ad-hoc join at the discovery call site.

What I actually ran (lane worktree, CARGO_TARGET_DIR=.targets/ctx-1876):

  • RUSTFLAGS="-D warnings" cargo check -p bitty-terminal -p bitty-runtime --tests --target x86_64-pc-windows-gnu --offline → Finished in 2.06s, Checking bitty-terminal (real re-check, not a no-op), zero diagnostics.
  • cargo +1.85 check -p bitty-terminal -p bitty-runtime --tests --offline → re-checked bitty-runtime + bitty-terminal in 12.57s (60s CPU), zero warnings on the non-Windows leg.
  • cargo test -p bitty-terminal --offline -- legacy|store_root|adopt → 21 + 1 + 23 tests pass, including both new state tests.
  • Mutation reproduction in /home/fuyu/.hermes/cache/scratch/review-3238/ with its own target dir (details in check 4).

1. Single authority — PASS

One resolver, one legacy predicate, one adoption for both tiers: tier_root_with_platform_env (crates/bitty-terminal/src/plugin_runtime.rs:1236) is the only place a tier path is built; PluginDataTier::{Store,State} (:1204-1225) carries only the leaf name, so plugins and plugins-state cannot drift. The pure, non-adopting form is now #[cfg(test)] (:1256), so no shipped caller can read the pre-adoption root by accident.

Every surface resolves through that body:

  • startup discovery → startup_store_root / startup_plugins_state_root (:1520, :1567), safe mode → read-only variants (:1552-1564)
  • list plugins, list services, config_reload, x → store_root() (:1168; callers list.rs:888, list.rs:1423, x.rs:120, config_reload.rs:819)
  • plugin install|remove|doctor --repair|extension → plugin.rs:5620-5637; doctor without --repair → read-only (plugin.rs:5623)
  • shell completion → candidates.rs:203
  • bitty init → init.rs:3241; --safe snapshot → runtime_inspect.rs:206-221

Grep for a second resolution: grep -rn "plugins-state" crates/ finds no production join outside PLUGIN_STATE_DIR_NAME (:1193); grep -rn 'join("plugins")' finds only tests, config-layer file names, and plugin_settings.rs:1072 (the config plugins/ dir, a different tier). The only remaining direct data_home() caller is the third-party wheel dir (config_manage.rs:89), which is documented as owning its own layout (:1596-1612). No second -state suffix exists.

2. Security invariants — PASS

  • Discovery stays single-authority. The diff adds no discovery root: bundled_roots / third_party_roots / dev_roots are untouched, and the state root is passed as PluginRuntimeConfig::data_dir (:1823, consumed at bitty-runtime/src/plugin_runtime/mod.rs:4927-4934), which is a plugin-writable key-value directory, never a package source. Existing current root ⇒ no merge (legacy_store.rs:256-258), so exactly one index is ever read; capability/deny-by-default code is not in the diff at all (git diff --stat = 4 files, none in the policy host), so R-006/R-015/R-017 are unaffected.
  • Legacy tree is never written by the legal read paths: read_only_store_root (legacy_store.rs:160-168) only stats/read_dirs; safe/doctor/completion resolve through read_only_tier_root_with_platform_env (:1397) and create, move and copy nothing.
  • Failed migration keeps the legacy tree serving: effective_tier_root_with_platform_env returns outcome.store_root(&root, &legacy) (:1360-1363), and Failed/Unreadable re-probe and fall back to the legacy path (legacy_store.rs:113-137). Unreadable explicitly returns before any create_dir_all (legacy_store.rs:241-252), so a transient enumeration failure cannot fork an empty second tree. The tier-generic warning now names the tier (:1354-1386), so the operator sees "plugin state" vs "plugin store".

3. Windows correctness — PASS

  • The cfg(windows) fixtures exercise real paths: windows_legacy_tier_fixture (:6626-6890) plants Users/u/.local/share/bitty/{plugins,plugins-state} plus AppData/Local, calls the real resolvers, and asserts the file/package moved (store.json is_file, rows.len()==1, name == "Legacy Store"), the legacy tree is gone, and a second resolution is idempotent. startup_plugins_state_root_with_platform_env(true, …) is asserted to leave the legacy state in place while the non-safe call adopts it (:6815-6840) — that contrast is what discriminates, not a tautology.
  • No Unix-only assertion: the fixtures use std::fs, Path, to_string_lossy; windows_legacy_store_migration.rs:53 uses SystemTime::now().duration_since(UNIX_EPOCH), which is portable. Both compile under --target x86_64-pc-windows-gnu --tests with -D warnings.
  • Non-Windows leg is clean: cargo +1.85 check … --tests printed no dead_code/unused_imports, so #[cfg(test)] store_root_with_platform_env and the tuple-only helpers do not leak. Both bitty-runtime and bitty-terminal were genuinely recompiled, so this is not a replayed cache hit.

4. Test discrimination — PASS (reproduced, then restored)

Copied the worktree to /home/fuyu/.hermes/cache/scratch/review-3238/repo, mutated exactly one function — effective_plugins_state_root_with_platform_env (:1454-1467) reverted to the pre-PR plain join (tier_root_with_platform_env(PluginDataTier::State, …), i.e. the old data_home().join("bitty").join("plugins-state")) — and ran with a separate CARGO_TARGET_DIR=…/review-3238/target:

test plugin_runtime::tests::legacy_plugins_state_adoption_covers_the_same_population ... FAILED
panicked at crates/bitty-terminal/src/plugin_runtime.rs:6451:9:
the state file moved with its tier
test result: FAILED. 0 passed; 1 failed; 0 ignored; 1666 filtered out

Restore: reverted the mutation, diff -q against the lane file reported identical, and the same target dir then ran legacy_plugins_state_adoption_covers_the_same_population … ok / legacy_plugins_state_is_never_adopted_outside_the_windows_home_population … ok. The claim is real; the lane worktree was not modified.

5. Docs accuracy — PASS

CHANGELOG.md:8-29 is under ## [Unreleased] and makes no release claim (the only dated heading stays ## [0.0.31] - 2026-10-29). Every factual statement checks out against code: precedence $XDG_DATA_HOME > %LOCALAPPDATA% > $HOME/.local/share > %APPDATA% (bitty-runtime/src/component/mod.rs:184-229), component state $XDG_STATE_HOME > %LOCALAPPDATA% > $HOME/.local/state (component/activation.rs:214-226), one-time rename with bounded copy fallback, failed move keeps reading the legacy tree, and the three read-only verbs. PluginRuntimeConfig::data_dir (bitty-runtime/src/plugin_runtime/mod.rs:354-362) now states the caller resolves it and why. The entry does describe #3226/#3230 behaviour that is already in main but was previously undocumented — acceptable for an unreleased bullet, worth keeping in mind at release time.

6. Behavioural risk — PASS

  • Merge with an existing current root: never. new_root_exists (legacy_store.rs:216-218) is a symlink_metadata presence test and short-circuits to NewRootPresent before any create_dir_all/rename (:256-258).
  • Symlinks: the legacy root is classified with symlink_metadata and a symlinked or non-directory root is Absent (legacy_store.rs:187-199), so no adoption follows a link. The copy walk uses non-following entry.file_type() and refuses any non-regular entry with Failed (:397-418) — a planted symlink/FIFO leaves the legacy tree serving instead of producing a partial store.
  • Unbounded copy: genuinely bounded — depth 8 (:49), 8192 entries (:55), 256 MiB (:67), one shared budget decremented across the recursion (:371-411), byte budget checked before each fs::copy. Overrun abandons the staging tree (:319-331) and returns Failed, which keeps the legacy root. There is a unit test for each bound (:568, :793). The only residual is a metadata-vs-copy TOCTOU on a file that grows between len() and fs::copy, bounded in practice by the entry/depth caps — not worth a change.
  • Staging names are leaf-derived (plugins.adopting-<pid> / plugins-state.adopting-<pid>, :344-353), so the state tier cannot collide with the store's staging dir.

Non-blocking notes (not required for this PR)

  • The new plugins_state_root seam is compile-verified but never exercised: the production call passes startup_plugins_state_root(safe_mode) (:1741), while every test call site passes None (:2719, :2834, :3172, :3405, :3572). Because data_dir: None degrades to an in-memory PluginStore (bitty-runtime/src/plugin_runtime/mod.rs:4927-4934), a future regression that drops this argument would be as silent as the bug being fixed. One test threading a real state root through discover_with_roots and asserting <root>/<id>/store.json would close that hole.
  • Same stranding shape exists for the component/extension tier: components_root_for (bitty-runtime/src/component/descriptor.rs:93-103) and component_state_root_for (component/activation.rs:236-247) join bitty/extensions directly with no legacy adoption. Out of scope for bitty#3236, but the CHANGELOG's "one data-root resolver" should not be read as covering it.
  • init_plugin_inventory_for_env (init.rs:3238) documents "nothing is written" while calling the adopting resolver — pre-existing in main, but it is the same doc/reality gap this PR is cleaning up elsewhere.

VERDICT: APPROVE

@Xuepoo
Xuepoo merged commit 84d026f into main Nov 18, 2026
21 checks passed
@Xuepoo
Xuepoo deleted the ctx-1876/windows-store-migration branch November 18, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform Area: platform / windowing fix Bug fix P1 Priority: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(platform): migrate the legacy Windows HOME plugin store after the LOCALAPPDATA precedence change

1 participant