Skip to content

fix(#843): invalidate the config-flatten and env caches when a layer is deleted - #844

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/843
Oct 2, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/843

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

_remember_cfg_flatten_cache_load (scripts/log.sh) and _remember_env_cache_load (scripts/lib-env-cache.sh) decide whether their cache is still fresh with [ "$_f" -nt "$_src" ], checked against each config layer. -nt is true when the layer file doesn't exist. That's the right answer for a layer that never existed. It's the wrong answer for a layer that existed when the cache was written and has since been deleted, because deleting a file changes no mtime that -nt looks at. As a result, the cache kept serving the deleted layer's values indefinitely.

Both caches now record which of their fixed layers existed when the cache was written. On load, if that record doesn't match which layers exist now, the cache is rejected, regardless of what -nt says.

  • scripts/log.sh: adds a header line, #RCFG_EXISTS=<one 0/1 digit per layer>, read second, after the #REMEMBER_DIR= identity line. A malformed value is treated the way a malformed identity line already is: the cache file is deleted and rejected. A record that doesn't match is treated as stale: rejected, but not deleted.
  • scripts/lib-env-cache.sh: prefixes each existing CACHE_CONFIG=<path> value with 1: or 0: instead of adding a line. A separate line pushed the per-prompt hook over its read limit by one, which tests/test_hot_path_cost_pin_330.py caught. The prefix is removed as a fixed two-character prefix, not split on the first colon, so a Windows drive-letter path is unaffected.
  • Upgrading: a cache written by an older release is missing the new line or prefix, so it is rejected once and rebuilt.

Closes #843. Reported in #842 by @books-around-trees.

Testing

  • tests/test_config_flatten_cache_668.py: 3 new tests.
    • A deleted layer invalidates the cache. This test was written first and watched fail before the fix.
    • Control: the cache is reused when nothing changed.
    • A layer that never existed doesn't force a rebuild.
  • tests/test_env_cache_deleted_layer_843.py: the same three cases for the env cache, run through scripts/user-prompt-hook.sh. The file is skipped on Windows (it needs a bash hook subprocess), and is listed in docs/windows-skip-triage.md.
  • Full pytest: 2803 passed, 59 skipped, 0 failed.
  • Review: two agents during development, then five parallel reviewers covering CLAUDE.md rules, bugs, git history, earlier PR comments and code comments. None found a defect.

Known gap (not a regression)

A separate review flagged a narrow race. The publisher checks which layers exist when it writes the cache, but the cached values were read earlier, during the layer merge. If a layer is deleted in that window (a few milliseconds), the cache records it as absent while still holding its values. Later loads then see "absent then, absent now" and accept the cache. The same window exists at scripts/lib-env-cache.sh publish. It also existed before this change. Closing it would mean recording existence during the merge and passing that through to both publishers. Left as a possible follow-up.

Cross-platform claims

  • Observed on macOS (stock bash 3.2.57): both fixes work end to end, and the full suite passes.
  • Reasoned, not observed, on Linux and Windows Git Bash: the 1:/0: prefix with a Windows path, and how -nt behaves against a missing file on Git Bash. CI covers the matrix.

🤖 Generated with Claude Code

[AI-generated]

…is deleted

`-nt` is true against a config layer that does not exist, which is the
right answer for a layer that NEVER existed, but the same loop treats a
layer that EXISTED when the cache was published and was since DELETED
identically -- deleting a file changes no mtime a `-nt` check looks at, so
that case kept serving the deleted layer's values indefinitely (split out
of #842, reported by @books-around-trees).

Both caches now also record which of their standard layers existed at
publish time, and reject the cache if that no longer matches:

- scripts/log.sh's flattened-config cache gets a second header line,
  `#RCFG_EXISTS=<0/1 manifest>`, validated the same way the existing
  identity line is.
- scripts/lib-env-cache.sh's resolved-environment cache embeds a `1:`/`0:`
  prefix into each existing `CACHE_CONFIG=` value instead of adding a new
  line -- a separate header line there blew the #330/#395 hot-path `read`
  builtin budget by exactly one when tried first.

New tests pin the deletion case against both caches, each paired with a
positive control (an unchanged run must still hit the cache) and a case
for a layer that never existed (must still count as a hit).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
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.

Deleting a config layer leaves its values in effect: the config caches treat a vanished layer as unchanged

1 participant