Skip to content

fix: atomic config save, stdin-free prompter tests, credential env lock - #162

Merged
mrnasil merged 2 commits into
mainfrom
test/credential-env-lock
Oct 11, 2026
Merged

mrnasil merged 2 commits into
mainfrom
test/credential-env-lock

Conversation

@mrnasil

@mrnasil mrnasil commented Oct 11, 2026

Copy link
Copy Markdown
Member

Summary

Three tests that failed or hung intermittently. Two of them were product bugs.

1. config::write_atomic was not atomic (and raced). It removed config.toml and then renamed the temp over it. Two parallel saves raced on the remove (NotFound; flaky write_atomic_parallel_saves_do_not_collide in Coverage on #161), and a crash between the two steps left no config at all. std::fs::rename already replaces atomically on Unix and Windows (MoveFileExW + MOVEFILE_REPLACE_EXISTING), so the save is now rename-only with the existing single retry. The parallel-save test passed 30/30 under a Linux stress run and 15/15 on Windows.

2. Stdin prompter tests hung the agent suite. StdinPrompter::ask / StdinQuestionPrompter::ask read the process stdin synchronously inside the async block. The tests' 2 s timeout cannot cancel a blocking read, and their is_terminal skip does not catch an open pipe. So with stdin left open the read never returned, the runtime never shut down, and other stdin readers queued behind the lock: cargo test -p whycodes-agent hung, both in a background loop and in a harness. The prompters now take an AnswerInput (fn() -> Box<dyn BufRead>, default: the locked process stdin). Tests still call ask, with std::io::empty(). Production behavior is unchanged. With stdin held open the suite passed 3/3 in 32 s; before, it hung on the first run.

3. Credential env-var race. rate_limit_retries_same_model_on_next_credential and failover_api_key_walks_named_credentials both set and restore WHYCODES_CREDENTIAL_LANE and the named-credential vars. Run in parallel, one restored the other's vars mid-turn, and the 429 failover found no second key (flaky in Coverage on #154 and #161). Both now hold a test-only CREDENTIAL_ENV_LOCK.

Test plan

  • config lib 109 passed; parallel-save stress 30/30 (Linux), 15/15 (Windows)
  • agent lib 553 passed ×3 with stdin held open (was hung)
  • fmt, clippy (agent/config/cli), ratchets

🤖 Generated with Claude Code

mrnasil and others added 2 commits October 11, 2026 06:20
1. config: write_atomic removed the destination before the rename.
   Two parallel saves raced on that remove (NotFound, flaky in Coverage
   on #161), and a crash between remove and rename left no config.toml.
   rename replaces atomically on Unix and Windows (MOVEFILE_REPLACE_
   EXISTING), so it is now rename-only with the one retry. Parallel-save
   test: 30/30 on Linux, 15/15 on Windows.

2. agent: StdinPrompter / StdinQuestionPrompter read the process stdin
   inside the async ask. The read blocks the runtime thread, so the
   tests' 2 s timeout could not cancel it; with stdin left open (a
   terminal or a pipe) the tests never ended and other tests queued on
   the stdin lock — `cargo test -p whycodes-agent` hung. The prompters
   take an AnswerInput (default: locked process stdin); tests pass an
   empty reader and still go through `ask`. With stdin held open: 3/3
   runs in 32 s, was hung on the first.

3. agent: rate_limit_retries_same_model_on_next_credential and
   failover_api_key_walks_named_credentials both set and restore the
   credential env vars; run in parallel, one restored the other's vars
   mid-turn and the 429 failover found no second key (flaky in
   Coverage on #154 and #161). Both now hold CREDENTIAL_ENV_LOCK.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rename-only saves no longer lose a remove race, so tests never took the
'first rename failed, retry succeeded' arm and config fell to 2406/2407.
The same logic as two always-run lines: let-else on the first rename,
map_err on the retry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit fb05a7b into main Oct 11, 2026
7 checks passed
@mrnasil
mrnasil deleted the test/credential-env-lock branch October 11, 2026 03:34
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