fix(tui): stop background cache-warning writes from corrupting TUI screen
Fixes #893
Root causes
Two independent, stackable issues cause the TUI's screen to get corrupted when a background cache shard save fails:
-
Why the write fails on Windows (
Access is denied (os error 5)):fs_atomic::windows_replace_filecallsMoveFileExW(..., MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)exactly once, with no retry. This is a well-known source of transientERROR_ACCESS_DENIED(5) /ERROR_SHARING_VIOLATION(32) on Windows — antivirus, indexing, and cloud-sync agents routinely hold a brief scan handle open on a just-written file.fs_atomic::replace_fileis the single shared atomic-rename primitive used by 10 call sites across both crates, so this failure mode isn't unique to the cache. -
Why the failure corrupts the TUI display:
message_cache::warn_cache_failure_oncealways callstracing::warn!, then falls back to a raweprintln!the first time each failure context is seen — because most non-TUI commands never install a tracing subscriber, sotracing::warn!alone would be silent. But the TUI only installs a subscriber under--debug. In normal (non-debug) use, thateprintln!writes directly to stdio while the TUI owns raw mode and the crossterm alternate screen, corrupting the rendered display exactly as reported.
Fix
fs_atomic.rs: wrap the WindowsMoveFileExWcall in a bounded retry loop (5 attempts, ~10ms × attempt backoff, ≤100ms worst case), retrying only on the two known-transient error codes. Benefits all 10 existingreplace_filecall sites with no call-site changes. Non-Windows path (std::fs::rename) is untouched.- New
tui_signal.rs: a small process-wideAtomicBoolflag (set_tui_active/is_tui_active), mirroring the existing style ofpaths::is_config_dir_overridden(). message_cache.rs: gate theeprintln!fallback inwarn_cache_failure_onceon!tui_signal::is_tui_active().tracing::warn!still fires unconditionally and is unchanged. Non-TUI command behavior is byte-for-byte unchanged.tui/mod.rs: toggle the flag totrueright after the TUI successfully enters the alternate screen, and back tofalsein both terminal-restore paths (normal exit and the panic hook's best-effort restore), so it can never get stuck.
Verification
cargo build -p tokscale-core -p tokscale-cli— cleancargo clippy -p tokscale-core -p tokscale-cli --all-targets -- -D warnings— zero warningscargo test -p tokscale-core— 1216 passed, 0 failed, including a newtui_signal::tests::round_tripstestcargo fmt --check— clean- Manual review of the
unsafe extern "system"MoveFileExWretry loop — no Windows machine available locally to compile/run the#[cfg(windows)]path; relying on the existingbuild-native.ymlWindows CI job plus this manual review
Not tested: actual Windows MoveFileEx retry behavior under real transient file-lock contention (AV/indexer/cloud-sync), since no Windows machine is available in this environment.
Summary by cubic
Prevents TUI screen corruption when background cache save warnings fire by suppressing raw stderr during TUI and making Windows file replacement more resilient. Fixes #893.
-
Bug Fixes
- Windows: retry
MoveFileExWinfs_atomic::replace_file(up to 5 attempts with short backoff) on transientERROR_ACCESS_DENIED/ERROR_SHARING_VIOLATION. Non-Windows unchanged. - TUI: add
tokscale-core::tui_signaland toggle it fromtokscale-cliwhen entering/leaving the TUI; suppressmessage_cache::warn_cache_failure_onceeprintln!while active.tracing::warn!unchanged; non-TUI behavior remains the same.
- Windows: retry
-
Refactors
- Run workspace-wide
cargo fmtto satisfy CI; unrelated files only, no behavior changes.
- Run workspace-wide