fix(prompts): a failed rebuild of the button pictures keeps the last good file #486

Merged
HumanBean merged 1 commit from jarynclouatre/Longshot:fix/prompt-cache-keep-good into main 2026-11-15 21:55:35 +00:00
Contributor

Type: fix · Risk: low · Tested on: Linux x86-64 (game-free checks only) · Needs: none

Why

When a start writes the button prompts' cache again (after the Button prompts face or a binding changed) and the write fails part way, for example on a full disk, cache/prompts/Common.bntx.zs (or Common.blarc.zs) is left cut short while its key file still holds the old setting's key. Back on the old setting the key matches, the file exists, and the cut file is given to the game at every start.

write_patched in host/prompts.c opens out with fopen(out, "wb"), which empties the old file before the new one is known good. On a failure the old key stays, and the reuse check is only "key equal and the file exists".

None filed. Found reading write_patched.

How to reproduce

Seen on: found reading the code; not seen in the game
Settings and switches: Change the Button prompts face or a binding, then return to the old setting.
Steps: Start with a cached file, change the face or a binding, and start again with a write that fails part way, for example on a full disk. Return to the old setting and start again.
Expected: A failed rewrite leaves the last good file available for the old setting.
Actual: The file is cut short while its key still holds the old setting's key, so the cut file is given to the game at every start on that setting.

What changed

The button pictures are written to a temporary file before replacing the cached file. A failed write keeps the last good file, and an interrupted replacement leaves no key so the next start writes again.

  • prompts (host/prompts.c, write_patched): writes to out.tmp beside the file, with every write and the fclose checked. Then it removes the key file, moves the temporary file over out (MoveFileExA with MOVEFILE_REPLACE_EXISTING) and writes the new key. On any failure the temporary file is deleted and the old file and its key stay as they were, which is still a valid pair. A stop between the steps leaves no key, so the next start writes again. Both callers (textures and layouts) go through this function, and each has its own key file.
  • tests: new tests/test_prompt_cache_errors.py compiles write_patched and its zstd helpers from host/prompts.c, with the three Windows file calls as stand-ins on POSIX calls. Setting A is written; setting B's write fails at a file size limit (RLIMIT_FSIZE, SIGXFSZ ignored); A's file bytes and key are unchanged and no .tmp is left; A again serves the same bytes; B with room replaces file and key, and the file holds the game's bytes with B's run. Skipped on Windows (no file size limit there). tests/test_mods_size_table.py's routing fixture builds prompts.c over pieces of host/posix/win32_posix.c and now takes its DeleteFileA and MoveFileExA too.

Measurements

Verdict: the new checks fail on main and pass here; the suite has no new failures.

Hardware: none used (game-free checks on a Linux x86-64 PC, gcc 15.2, Python 3.14)
Scene: none, the game was not run
Method: 2026-11-14. python3 -I -B tests/run_all.py --jobs 4, two runs each (main twice, then the branches of this batch in turn, twice). The new test also ran against main's host/prompts.c. Branch: fix/prompt-cache-keep-good.
How to reproduce: the commands above in a checkout of each commit

Before: main @ 9c9d69ed After: this PR @ d690b06b
tests/run_all.py (run 1, run 2) 485 passed, 2 failed, 83 skipped (both) 486 passed, 2 failed, 83 skipped (both)
failing checks test_bake_portability.py, test_walk_key.py the same two
tests/test_prompt_cache_errors.py FAIL: a failed write of setting B changed setting A's file (200000 bytes, was 300019) pass

test_bake_portability.py fails here for a missing Python module (zstandard). test_walk_key.py's fixture doesn't link on main (undefined reference to hos_oot_autoload_input).

Flags and settings

None.

Testing

Tested: the game-free suite on a Linux PC, as in Measurements. host/prompts.c compiles (-fsyntax-only -Wall -Wextra) with llvm-mingw 20261104's x86_64-w64-mingw32-clang with no warnings, as main's, and whole with gcc in tests/test_mods_size_table.py's fixture.

Not tested: the game, and a full disk on Windows.

Must be tested: None.

Risks

What could break: #218 changes the fopen calls in the same function to hos_fopen_*; whichever lands second needs a small rebase.

Turning it off: Reverting the commit puts the write in place back.

Compatibility: A rewrite now needs room for a second copy of the file for a moment (a few MB).

Checklist

Everyone

  • One topic.
  • The title is a Conventional Commit (type(scope): description) of the status line's type.
  • Why gives the reason plainly and links the issues that fit (the tracker searched).
  • Measurements has a verdict, or one line saying why there is nothing to measure.
  • Testing names what was tested, what wasn't, and what must still be tested, where and how.
  • Rebased on current main.
  • python -I -B tests/run_all.py passes (any failure that also fails on main is named).
  • No game files, dumps, build output or generated files.
  • third_party/ untouched (a new folder with a PROVENANCE.md instead).
  • The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled.

When it applies

  • Numbers from the same PC, scene and settings, at least 2 interleaved runs each, the median reported.
  • Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • A game-free test added for the fix, failing on main and passing here.
  • A check under tests/ that reads changed source updated in this PR, not weakened.
**Type:** fix · **Risk:** low · **Tested on:** Linux x86-64 (game-free checks only) · **Needs:** none ## Why When a start writes the button prompts' cache again (after the Button prompts face or a binding changed) and the write fails part way, for example on a full disk, `cache/prompts/Common.bntx.zs` (or `Common.blarc.zs`) is left cut short while its key file still holds the old setting's key. Back on the old setting the key matches, the file exists, and the cut file is given to the game at every start. `write_patched` in `host/prompts.c` opens `out` with `fopen(out, "wb")`, which empties the old file before the new one is known good. On a failure the old key stays, and the reuse check is only "key equal and the file exists". None filed. Found reading `write_patched`. ## How to reproduce **Seen on:** found reading the code; not seen in the game **Settings and switches:** Change the Button prompts face or a binding, then return to the old setting. **Steps:** Start with a cached file, change the face or a binding, and start again with a write that fails part way, for example on a full disk. Return to the old setting and start again. **Expected:** A failed rewrite leaves the last good file available for the old setting. **Actual:** The file is cut short while its key still holds the old setting's key, so the cut file is given to the game at every start on that setting. ## What changed The button pictures are written to a temporary file before replacing the cached file. A failed write keeps the last good file, and an interrupted replacement leaves no key so the next start writes again. - prompts (`host/prompts.c`, `write_patched`): writes to `out.tmp` beside the file, with every write and the `fclose` checked. Then it removes the key file, moves the temporary file over `out` (`MoveFileExA` with `MOVEFILE_REPLACE_EXISTING`) and writes the new key. On any failure the temporary file is deleted and the old file and its key stay as they were, which is still a valid pair. A stop between the steps leaves no key, so the next start writes again. Both callers (textures and layouts) go through this function, and each has its own key file. - tests: new `tests/test_prompt_cache_errors.py` compiles `write_patched` and its zstd helpers from `host/prompts.c`, with the three Windows file calls as stand-ins on POSIX calls. Setting A is written; setting B's write fails at a file size limit (`RLIMIT_FSIZE`, SIGXFSZ ignored); A's file bytes and key are unchanged and no `.tmp` is left; A again serves the same bytes; B with room replaces file and key, and the file holds the game's bytes with B's run. Skipped on Windows (no file size limit there). `tests/test_mods_size_table.py`'s routing fixture builds `prompts.c` over pieces of `host/posix/win32_posix.c` and now takes its `DeleteFileA` and `MoveFileExA` too. ## Measurements **Verdict:** the new checks fail on main and pass here; the suite has no new failures. Hardware: none used (game-free checks on a Linux x86-64 PC, gcc 15.2, Python 3.14) Scene: none, the game was not run Method: 2026-11-14. `python3 -I -B tests/run_all.py --jobs 4`, two runs each (main twice, then the branches of this batch in turn, twice). The new test also ran against main's `host/prompts.c`. Branch: `fix/prompt-cache-keep-good`. How to reproduce: the commands above in a checkout of each commit | | Before: main @ 9c9d69ed | After: this PR @ d690b06b | |---|---|---| | `tests/run_all.py` (run 1, run 2) | 485 passed, 2 failed, 83 skipped (both) | 486 passed, 2 failed, 83 skipped (both) | | failing checks | `test_bake_portability.py`, `test_walk_key.py` | the same two | | `tests/test_prompt_cache_errors.py` | `FAIL: a failed write of setting B changed setting A's file (200000 bytes, was 300019)` | pass | `test_bake_portability.py` fails here for a missing Python module (zstandard). `test_walk_key.py`'s fixture doesn't link on main (`undefined reference to hos_oot_autoload_input`). ### Flags and settings None. ## Testing **Tested:** the game-free suite on a Linux PC, as in Measurements. `host/prompts.c` compiles (`-fsyntax-only -Wall -Wextra`) with llvm-mingw 20261104's x86_64-w64-mingw32-clang with no warnings, as main's, and whole with gcc in `tests/test_mods_size_table.py`'s fixture. **Not tested:** the game, and a full disk on Windows. **Must be tested:** None. ## Risks **What could break:** #218 changes the `fopen` calls in the same function to `hos_fopen_*`; whichever lands second needs a small rebase. **Turning it off:** Reverting the commit puts the write in place back. **Compatibility:** A rewrite now needs room for a second copy of the file for a moment (a few MB). ## Checklist ### Everyone - [x] One topic. - [x] The title is a Conventional Commit (`type(scope): description`) of the status line's type. - [x] Why gives the reason plainly and links the issues that fit (the tracker searched). - [x] Measurements has a verdict, or one line saying why there is nothing to measure. - [x] Testing names what was tested, what wasn't, and what must still be tested, where and how. - [x] Rebased on current `main`. - [x] `python -I -B tests/run_all.py` passes (any failure that also fails on `main` is named). - [x] No game files, dumps, build output or generated files. - [x] `third_party/` untouched (a new folder with a `PROVENANCE.md` instead). - [x] The license is OK: GPL-3.0-or-later, nothing incompatible and nothing decompiled. ### When it applies - [x] Numbers from the same PC, scene and settings, at least 2 interleaved runs each, the median reported. - [x] Host code builds on both Windows (llvm-mingw) and Linux, with UTF-8 paths. - [x] A game-free test added for the fix, failing on `main` and passing here. - [x] A check under `tests/` that reads changed source updated in this PR, not weakened.
fix(prompts): a failed rebuild of the button pictures keeps the last good file
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 8s
beanstalk/approval Test farm not requested
d690b06b18
write_patched opened the cached file (Common.bntx.zs or Common.blarc.zs) for
writing before the new one was known good. A write that failed part way, a
full disk for one, left the file cut short with the old setting's key still
beside it. Back on the old setting the key matched and the cut file was given
to the game at every start.

The file is now written beside the old one (.tmp), the key is removed, the
new file is moved over the old and the new key is written. A failed write
deletes the temporary file and leaves the old file and its key as they were.
A stop between the steps leaves no key, so the next start writes again.
jarynclouatre changed title from WIP: fix(prompts): a failed rebuild of the button pictures keeps the last good file to fix(prompts): a failed rebuild of the button pictures keeps the last good file 2026-11-14 16:31:22 +00:00
Contributor

Published test-only supplement: NitnekLeBorgne/Longshot@49e496dd42

I prepared a one-file, test-only supplement to your existing cache-publication fix: tests/test_prompt_cache_publication.py. It does not change host/prompts.c or replace your PR. The test compiles the real write_patched, its raw-zstd helpers and run-order helpers from the selected source, with explicitly named POSIX stand-ins for the file calls.

The added cases use only 300,000 synthetic bytes, and cover both cache naming conventions:

  • Remove the synthetic input after writing and decoding A; a request for B must fail without changing A's bytes/key or leaving a temporary file. A must still be served with its input absent, and restoring the input must allow B to replace both decoded data and key.
  • Refuse DeleteFileA only on the key path: return false, preserve A/key, never call MoveFileExA, and allow temporary-file cleanup and a later retry.
  • Refuse MoveFileExA after the key was really deleted: return false, preserve A's data, leave the key absent, clean up the temporary file, and rebuild B/key on retry.

Measurements are functional test output, not FPS. Against the pinned earlier main 543846ebb08ded4ce75d364403bc4dcf24e83e85, the missing-source assertion fails: the last-good 300,019-byte cache is truncated to zero bytes. The unchanged owner head d690b06b182d24b9056fcf55905720b06c2621e7 passes all six family/scenario combinations. The same final test bytes were replayed in two main/head/main/head rounds. Baseline replay selects only missing-source and stops at its first Common.bntx preservation failure; no baseline pass is claimed for the other scenarios or family.

Your existing tests/test_prompt_cache_errors.py also passes unchanged in a separate bounded replay of its RLIMIT_FSIZE case. One private, unshipped mutant that ignores a refused key deletion is rejected by the new test; that is a sensitivity control, not a defect in your head.

Reproduce from the owner head with the supplemental patch applied:

python -I -B tests/test_prompt_cache_publication.py
python -I -B tests/run_all.py --jobs 1 --timeout 60 --strict test_prompt_cache_publication
# To replay a different, explicitly retained source:
python -I -B tests/test_prompt_cache_publication.py --source /path/to/prompts.c --cc gcc --case missing-source --verbose

Linux x86-64, Python 3.14.9, GCC 13.3.0. This validates a sliced-source unit fixture, not native Win32, whole-caller execution, a full PR build, current-main integration or a game run. Full-suite prerequisites such as zstandard, fontTools, shader tools and other toolchains were not provisioned. Main moved to 1be60eae813865b0845fb8bb05b1d7cb9b39e51f during the private work; that newer main was not tested or merged into this fixture. The owner head remained unchanged at the final anonymous cache-busted GET.

Frozen patch SHA-256: 709fc76ba90a3dcb766feedd148c442e7be614893c4fc0947951ca79828bb770.

The branch adds only that test above your unchanged pinned head; it does not replace your production fix or claim current-main integration. The historical main hashes above describe the retained private runs. Separate independent specification and quality reviews passed on the unchanged test bytes; this is not full-PR approval.

Assisted-by: Hermes Agent (OpenAI GPT-6.1-sol)
Co-Authored-By: Claudia (OpenAI GPT-6.1-sol)

Published test-only supplement: https://git.magicbean.dev/NitnekLeBorgne/Longshot/commit/49e496dd4201bee403ca8df19132ed075423b0fa I prepared a one-file, test-only supplement to your existing cache-publication fix: `tests/test_prompt_cache_publication.py`. It does not change `host/prompts.c` or replace your PR. The test compiles the real `write_patched`, its raw-zstd helpers and run-order helpers from the selected source, with explicitly named POSIX stand-ins for the file calls. The added cases use only 300,000 synthetic bytes, and cover both cache naming conventions: - Remove the synthetic input after writing and decoding A; a request for B must fail without changing A's bytes/key or leaving a temporary file. A must still be served with its input absent, and restoring the input must allow B to replace both decoded data and key. - Refuse `DeleteFileA` only on the key path: return false, preserve A/key, never call `MoveFileExA`, and allow temporary-file cleanup and a later retry. - Refuse `MoveFileExA` after the key was really deleted: return false, preserve A's data, leave the key **absent**, clean up the temporary file, and rebuild B/key on retry. Measurements are functional test output, not FPS. Against the pinned earlier main `543846ebb08ded4ce75d364403bc4dcf24e83e85`, the missing-source assertion fails: the last-good 300,019-byte cache is truncated to zero bytes. The unchanged owner head `d690b06b182d24b9056fcf55905720b06c2621e7` passes all six family/scenario combinations. The same final test bytes were replayed in two main/head/main/head rounds. Baseline replay selects only missing-source and stops at its first `Common.bntx` preservation failure; no baseline pass is claimed for the other scenarios or family. Your existing `tests/test_prompt_cache_errors.py` also passes unchanged in a separate bounded replay of its `RLIMIT_FSIZE` case. One private, unshipped mutant that ignores a refused key deletion is rejected by the new test; that is a sensitivity control, not a defect in your head. Reproduce from the owner head with the supplemental patch applied: ```sh python -I -B tests/test_prompt_cache_publication.py python -I -B tests/run_all.py --jobs 1 --timeout 60 --strict test_prompt_cache_publication # To replay a different, explicitly retained source: python -I -B tests/test_prompt_cache_publication.py --source /path/to/prompts.c --cc gcc --case missing-source --verbose ``` Linux x86-64, Python 3.14.9, GCC 13.3.0. This validates a sliced-source unit fixture, not native Win32, whole-caller execution, a full PR build, current-main integration or a game run. Full-suite prerequisites such as `zstandard`, `fontTools`, shader tools and other toolchains were not provisioned. Main moved to `1be60eae813865b0845fb8bb05b1d7cb9b39e51f` during the private work; that newer main was not tested or merged into this fixture. The owner head remained unchanged at the final anonymous cache-busted GET. Frozen patch SHA-256: `709fc76ba90a3dcb766feedd148c442e7be614893c4fc0947951ca79828bb770`. The branch adds only that test above your unchanged pinned head; it does not replace your production fix or claim current-main integration. The historical main hashes above describe the retained private runs. Separate independent specification and quality reviews passed on the unchanged test bytes; this is not full-PR approval. Assisted-by: Hermes Agent (OpenAI GPT-6.1-sol) Co-Authored-By: Claudia (OpenAI GPT-6.1-sol)
HumanBean approved these changes 2026-11-15 21:49:07 +00:00
HumanBean left a comment

Read the diff and the surrounding write_patched; nothing was run.

write_patched now writes out.tmp, checks every write and the fclose, and only then removes the key file, moves the temporary file over out and writes the new key. Each failure path leaves a consistent state:

  • A failed write deletes the temporary file, so the old file and its key are untouched.
  • A refused key delete (nothing has moved yet) or a refused move (key already gone) keeps the old data, and the missing key means the next start rewrites.
  • A stop between the steps also leaves no key, so the next start rewrites.
  • MoveFileExA and MOVEFILE_REPLACE_EXISTING exist in both the Windows API and the POSIX shim (host/posix/windows.h, host/posix/win32_posix.c). The size-table fixture pulling in the two shim functions is the right test update.
  • The tmp buffer is MAX_PATH + 8 and snprintf truncates safely. A stale .tmp left by a hard kill is truncated by the next fopen(..., "wb").

The new tests/test_prompt_cache_errors.py compiles the real function, fails on main for the stated reason (cut file served on the old key) and passes here. The description backs the diff, and the "Not tested" line (game, full disk on Windows) is honest. Risk is low. The only game-visible effect is the cache rewrite on a settings change, and the fix stops that rewrite from corrupting the old pair.

Merge order with the sibling PRs: #485 edits other hunks of host/prompts.c (first_pad, current_binds, cmap4_bind) and other tests, so I see no overlap with this PR. The #218 fopen to hos_fopen_* note in Risks is the only likely small rebase. NitnekLeBorgne's test-only supplement is a separate branch and does not block this one.

Read the diff and the surrounding `write_patched`; nothing was run. `write_patched` now writes `out.tmp`, checks every write and the `fclose`, and only then removes the key file, moves the temporary file over `out` and writes the new key. Each failure path leaves a consistent state: - A failed write deletes the temporary file, so the old file and its key are untouched. - A refused key delete (nothing has moved yet) or a refused move (key already gone) keeps the old data, and the missing key means the next start rewrites. - A stop between the steps also leaves no key, so the next start rewrites. - `MoveFileExA` and `MOVEFILE_REPLACE_EXISTING` exist in both the Windows API and the POSIX shim (`host/posix/windows.h`, `host/posix/win32_posix.c`). The size-table fixture pulling in the two shim functions is the right test update. - The `tmp` buffer is `MAX_PATH + 8` and `snprintf` truncates safely. A stale `.tmp` left by a hard kill is truncated by the next `fopen(..., "wb")`. The new `tests/test_prompt_cache_errors.py` compiles the real function, fails on main for the stated reason (cut file served on the old key) and passes here. The description backs the diff, and the "Not tested" line (game, full disk on Windows) is honest. Risk is low. The only game-visible effect is the cache rewrite on a settings change, and the fix stops that rewrite from corrupting the old pair. Merge order with the sibling PRs: https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/485 edits other hunks of `host/prompts.c` (`first_pad`, `current_binds`, `cmap4_bind`) and other tests, so I see no overlap with this PR. The #218 `fopen` to `hos_fopen_*` note in Risks is the only likely small rebase. NitnekLeBorgne's test-only supplement is a separate branch and does not block this one.
HumanBean deleted branch fix/prompt-cache-keep-good 2026-11-15 21:55:36 +00:00
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 8s
beanstalk/approval Test farm not requested
Some workflows are waiting to be reviewed.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!486
No description provided.