Skip to content

fix(build): stop the example-change detector false-firing on a clean tree (#3761) - #3771

Merged
zackees merged 2 commits into
masterfrom
fix/3761-example-change-detector
Jul 31, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/3761-example-change-detector

Conversation

@zackees

@zackees zackees commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Fixes #3761.

What was wrong

bash test paid a full ~20-30s reconfigure on an otherwise clean, fully cached tree, while the build system reported "Using cached example metadata (no changes detected)" in the same run. Two caches, one run, opposite answers.

The three causes named in the issue, all in detect_example_file_changes:

  1. "Cache missing" was reported as "example files changed." With the cache absent there was no path to "unchanged" — the outcome was a guaranteed reconfigure under a misleading reason.
  2. Firing deleted the cache. Only the --update step rewrites it, so any following run that was fully fingerprint-cached left the cache missing and primed the next false positive. One false fire perpetuated itself indefinitely — which is the state the repo was actually in.
  3. The hash was mtime-based. git checkout, git worktree add and branch switches rewrite example mtimes with byte-identical content, so every one of them invalidated the cache.

What changed

  • "cache missing / unreadable — cannot prove examples unchanged" is now reported distinctly from "example files changed", and the reason string is returned to the caller (which already prints it) instead of being printed twice with wording that could drift.
  • The detector never deletes the cache.
  • The hash is content-based. compute_example_files_hash hashes path + SHA256(content) per file.

Further defects found while reviewing the above

  1. The mtime fast path missed in-place edits. get_max_dir_mtime stats directories, never files, and directory mtimes do not move on an edit. So editing an existing example — adding an // @filter: line, say — was reported "unchanged" and the content hash was never consulted. Now paired with a file-mtime probe, and compared with a strict < so an entry landing in the same clock tick as the marker (Windows' clock granularity is ~15ms) falls through to the hash instead of being assumed unchanged.
  2. Nothing re-armed the fast path after a matching hash, so one git checkout would leave every later run paying the full ~0.2s content hash forever. The cache mtime now doubles as a "contents verified at" marker, stamped from before the hash so an edit landing mid-hash is still caught next run.
  3. Build artifacts were in the hash. examples/*/.build, .fbuild, build, bin were included, and a plain bash compile <board> --examples Blink rewrites those — re-creating the same false positive from artifact content instead of artifact mtimes. Pruned with the predicate the mtime scan already used (should_skip_scan_dir, promoted from private so there is one source of truth). 302 → 268 files hashed.

Two more fixed in passing:

  • The cache was written UTF-8 but read back with the platform locale codec (cp1252 on Windows). Any non-ASCII example path or filter string made json.load raise — and combined with no longer deleting the cache, that would have been a permanent per-run reconfigure.
  • _recover_stale_build cleared <build>/example_metadata.cache, but the file lives at <build>/examples/example_metadata.cache. The delete was a silent no-op, so recovery re-adopted the stale example set it was meant to discard.

Verification

On the real tree, before → after:

scenario before after
repeated bash test on a clean tree reconfigure every run 0 reconfigures across 3 runs, cache persists
after a git checkout (identical content) 20-30s reconfigure, every run one 200ms content hash, then 80ms fast path

The reconfigure reason is now accurate and printed once:

[MESON] Reconfiguring build directory (example metadata cache missing - cannot prove examples unchanged)

13 regression tests in ci/tests/test_example_change_detector.py pin each behavior, including the two that are easy to regress silently: in-place content edits must be detected, and the fast path must re-arm.

bash lint clean. Full C++ suite 357/357 (forced, not a replayed cached number — cf. #3763).

Notes for the reviewer

  • Second commit is unrelated. bash lint was already failing on master with two ty diagnostics in ci/autoresearch/phases.py (from the WS2814 change). Fixed here under the repo's fix-all-encountered-errors policy so lint is green; no behavior change.
  • fl_task_executor is flaky, pre-existing. fl::task::run SYSTEM yield paths failed in two full-suite runs and passed in two others, passes in isolation on unmodified master, and passes clean under ASAN/UBSAN (--debug). This PR is Python-only and cannot affect C++ runtime behavior. Filed separately.
  • Not done here: compute_src_files_hash and compute_test_files_hash still use the same mtime-based scheme and have the same git-checkout weakness; detect_test_file_changes still conflates a missing cache with "added N test files" (bounded to one run, so far less costly). Both left out to keep this reviewable — filed as follow-ups.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of example-file additions, removals, renames, and content changes.
    • Corrected stale-build recovery to remove the appropriate example metadata cache.
    • Unreadable files and invalid caches now trigger reliable reconfiguration instead of appearing unchanged.
    • Reconfiguration messages now explain the detected cause.
  • Tests

    • Added comprehensive coverage for cache validation, file changes, artifact filtering, and repositories without examples.

zackees and others added 2 commits July 30, 2026 19:33
…tree

`bash test` paid a full ~20-30s reconfigure on an otherwise clean, fully
cached tree, while the build system itself reported "Using cached example
metadata (no changes detected)" in the same run.

Root causes, all in the example-change detector:

1. A missing cache was reported as "example files changed". With the cache
   absent there was no path to "unchanged", so the outcome was a
   guaranteed reconfigure with a misleading reason.
2. Firing *deleted* the cache. Only the `--update` step rewrites it, so any
   following run that was fully fingerprint-cached left the cache missing
   and primed the next false positive. One false fire perpetuated itself
   indefinitely.
3. The hash was mtime-based. `git checkout`, `git worktree add` and branch
   switches rewrite example mtimes with byte-identical content, so every
   one of them invalidated the cache.

Fixes: report "cache missing/unreadable - cannot prove examples unchanged"
distinctly from "example files changed"; never delete the cache; hash file
contents instead of mtimes.

Three further defects found while reviewing the above:

4. The mtime fast path consulted directory mtimes only (`get_max_dir_mtime`
   stats directories, never files). Directory mtimes do not move on an
   in-place edit, so editing an existing example -- adding an `// @filter:`
   line, say -- was reported as "unchanged" and the content hash was never
   consulted. Now paired with a file-mtime probe, and compared strictly so
   an entry landing in the same clock tick as the marker falls through to
   the hash rather than being assumed unchanged.
5. Nothing re-armed the fast path after a matching hash, so a single
   checkout left every later run paying the full ~0.2s content hash
   forever. The cache mtime now doubles as a "contents verified at" marker,
   stamped from before the hash so an edit landing mid-hash is still seen.
6. The file set included build artifacts under `examples/*/.build`,
   `.fbuild`, `build`, `bin`. A plain `bash compile <board> --examples
   Blink` rewrites those, which would have re-created the same false
   positive from artifact content. Pruned with the predicate the mtime scan
   already used (`should_skip_scan_dir`, promoted from private).

Also fixed nearby:

- The cache was written UTF-8 but read back with the platform locale codec
  (cp1252 on Windows). Any non-ASCII example path or filter string made
  json.load raise; combined with no longer deleting the cache that would
  have been a permanent per-run reconfigure.
- `_recover_stale_build` cleared `<build>/example_metadata.cache`, but the
  file lives at `<build>/examples/example_metadata.cache` -- a silent
  no-op, so recovery re-adopted the stale example set it meant to discard.

Verified on the real tree: repeated `bash test` runs now reconfigure zero
times and the cache persists; a simulated `git checkout` costs one 200ms
content hash and then re-arms to an 80ms fast path, where it previously
cost a 20-30s reconfigure on every run. 13 new regression tests in
ci/tests/test_example_change_detector.py pin each behavior.

Closes #3761

Co-Authored-By: Claude <[email protected]>
`bash lint` failed on master with two `invalid-argument-type` diagnostics:
narrowing an untyped JSON value with a bare `isinstance(x, dict)` leaves ty
with `dict[Never, Never]`, which rejects every string key passed to `.get`.

Re-state the payload shape with an explicit cast so the keys type-check.
No behavior change. Unrelated to #3761, fixed under the repo's
fix-all-encountered-errors policy so lint is green.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR changes Meson example-file tracking from metadata-based to content-based hashing, adds detailed cache validation reasons, corrects cache cleanup, and adds comprehensive tests. It also applies an explicit type cast during WS2814 pattern validation.

Changes

Meson example-cache validation

Layer / File(s) Summary
Content-based example discovery and hashing
ci/meson/example_metadata_cache.py
Example discovery prunes build artifacts, groups supported extensions, and includes stable file ordering. Hashing uses file paths and SHA-256 content digests. Unreadable files produce a distinct hash marker.
Cache detection and reconfiguration wiring
ci/meson/cache_utils.py, ci/meson/meson_setup_phases.py, ci/meson/build_config.py, ci/meson/runner_helpers.py, ci/tests/test_example_change_detector.py
Detection returns specific reasons, validates directory and file mtimes, reads UTF-8 cache data, preserves cache files after changes, and re-stamps verified caches. Build setup reports the returned reason. Cleanup targets the nested cache path. Tests cover cache states, hashing, timestamps, file changes, errors, and missing example directories.

WS2814 typing correction

Layer / File(s) Summary
Typed WS2814 pattern validation
ci/autoresearch/phases.py
Validated pattern entries use an explicit dict[str, Any] cast without changing runtime validation.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant build_config
  participant detect_example_file_changes
  participant example_metadata_cache
  build_config->>detect_example_file_changes: Check example files
  detect_example_file_changes->>example_metadata_cache: Discover files and compute content hash
  example_metadata_cache-->>detect_example_file_changes: Return hash and mtimes
  detect_example_file_changes-->>build_config: Return reconfiguration reason or None
Loading

Possibly related PRs

  • FastLED/FastLED#3739: Both PRs modify Meson cache invalidation and mtime handling, but they update different cache mechanisms and functions.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes two ty lint fixes in an unrelated validator, which are not part of issue #3761. Move the unrelated ty fixes to a separate pull request, or link an issue that explicitly includes them.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix to false-positive example-change detection on clean trees and matches the primary changes.
Linked Issues check ✅ Passed The PR implements issue #3761 requirements: accurate missing-cache reporting, cache preservation, content hashing, and consistent detector behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/3761-example-change-detector

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ci/meson/meson_setup_phases.py`:
- Around line 592-595: Update the inner KeyboardInterrupt handler in the meson
setup flow to re-raise the caught interrupt after calling
handle_keyboard_interrupt(ki), matching the outer handler’s behavior and
ensuring worker-thread interrupts unwind instead of falling through to
cached_example_hash handling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b4d55076-9b3f-442c-bf7e-e0067649b07d

📥 Commits

Reviewing files that changed from the base of the PR and between 5dd05da and 66187fa.

📒 Files selected for processing (7)
  • ci/autoresearch/phases.py
  • ci/meson/build_config.py
  • ci/meson/cache_utils.py
  • ci/meson/example_metadata_cache.py
  • ci/meson/meson_setup_phases.py
  • ci/meson/runner_helpers.py
  • ci/tests/test_example_change_detector.py

Comment thread ci/meson/meson_setup_phases.py Resolved
@zackees
zackees merged commit 1301463 into master Jul 31, 2026
11 of 16 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jul 31, 2026
zackees added a commit that referenced this pull request Jul 31, 2026
handle_keyboard_interrupt() only raises on the main thread; from a worker
it notifies and returns. Without an explicit re-raise the interrupt fell
through to `cached_example_hash = ""`, so a Ctrl-C was reported as
"example metadata cache unreadable" and forced a reconfigure instead of
unwinding. Matches the outer handler in the same function.

Reported by CodeRabbit on #3771.

Co-Authored-By: Claude <[email protected]>
@zackees

zackees commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

Addressed in 93e29de.

Valid finding — fixed. handle_keyboard_interrupt() only re-raises when called on the main thread; from a worker thread it sets the global interrupt flag, notifies the main thread, and returns. So in the inner handler the interrupt fell straight through to cached_example_hash = "", and a Ctrl-C arriving during the cache read was reported as "example metadata cache unreadable" — forcing a full reconfigure out of an interrupt instead of unwinding. Added the explicit raise, matching the outer handler in the same function.

13 regression tests and bash lint still green.


Status note on the two red checks, neither of which this PR causes:

Also worth flagging: master currently has four red checks (the esptool job plus check_teensy32/35/36_size on #3770), so "all green" is not presently a reachable merge bar on this repo.

zackees added a commit that referenced this pull request Jul 31, 2026
…3776)

handle_keyboard_interrupt() only raises on the main thread; from a worker
it notifies and returns. Without an explicit re-raise the interrupt fell
through to `cached_example_hash = ""`, so a Ctrl-C was reported as
"example metadata cache unreadable" and forced a reconfigure instead of
unwinding. Matches the outer handler in the same function.

Reported by CodeRabbit on #3771.

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

build(meson): example-change detector forces a full reconfigure on a clean tree (missing cache == "changed")

1 participant