Skip to content

Keep X-ray's unreachable moments honest around reloads, starts and stops - #2648

Open
frenck wants to merge 4 commits into
mainfrom
frenck/xray-unreachable-followups
Open

frenck wants to merge 4 commits into
mainfrom
frenck/xray-unreachable-followups

Conversation

@frenck

@frenck frenck commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Description

Follow-ups to the review of #2637, which merged with ten open threads. Seven were real, this fixes them.

  • A reload of Spook starts every device's minute anew. The time a device reported was kept over a reload, so the time Spook was not loaded counted as reporting. A device written down as gone that reports across a reload was dropped too early, and one going right after the reload got an exact moment it did not earn. Stopping the tracker now clears it.
  • A registry change while Home Assistant starts waits for settling. Disabling and enabling a device during the start wiped its stored moment. Settling decides, like for everything else then.
  • Nothing is noted or forgotten while Home Assistant stops. Settling, or the timer for a device that is back, could still change what the final write keeps. The check moved out of the tracker into the memory: async_note and async_forget refuse while Home Assistant stops. Every path that changes what is written down ends in one of those two, so a new path cannot forget it.
  • The benchmark times what it claims. The crossing it timed hit a device that still reported. It now times the last entity of a device going, and coming back.

Three threads are declined, with the reason on each: callbacks run right away in core (no queued flap), a store file without a version raises for every store in Home Assistant, and a device that is not back for a minute keeps its old moment on purpose.

Motivation and Context

The moment X-ray shows has to be honest. These were the edges where it was not.

How has this been tested?

Each fix has a test that fails without it, checked by taking the fix out again:

  • test_a_reload_of_spook_starts_the_minute_anew: dropped a minute after the reload, not at it and not never.
  • test_a_registry_change_while_starting_keeps_the_moment
  • test_nothing_is_noted_or_forgotten_while_home_assistant_stops: a device going, one coming back, one removed, one disabled, and the recovery timer, all during a stop.
  • test_settling_while_home_assistant_stops_notes_nothing
  • test_devices_back_at_once_wait_for_one_timer: 20 devices back together look for the next one back twice; one by one, at most once more each.

The benchmark, locally: a value 0.12 µs, a device going 7.4 µs, coming back 5.9 µs, settling 2.9 ms. Targeted tests and ruff locally, the rest in CI.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I did not edit machine-translated files in documentation/translations/output/.

Follow-ups to the review of #2637:

- A reload of Spook starts every device's minute anew.
- A registry change while Home Assistant starts waits for settling.
- Nothing is noted or forgotten while Home Assistant stops: one guard,
  in the memory.
- The benchmark times a device going and coming back.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 17:20
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementors. label Dec 4, 2026
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The unreachable tracker now prevents memory changes and return-timer scheduling while Home Assistant stops. It defers device-registry changes until settling completes and resets reporting timestamps when stopped. Tests cover shutdown, reload timing, device returns, malformed memory, and large-home transitions.

Changes: Unreachable Tracker

Layer / File(s) Summary
Memory lifecycle and shutdown
custom_components/spook/xray_unreachable.py, tests/test_xray_unreachable.py
_Memory retains Home Assistant and blocks note, forget, and timer scheduling while it is stopping. Stopping clears reporting timestamps. Tests cover shutdown behavior, malformed memory input, and stale-device cleanup.
Startup settling and reload timing
custom_components/spook/xray_unreachable.py, tests/test_xray_unreachable.py
Device-registry changes are ignored until settling completes. Tests cover startup and reload timing, including preservation of stored unreachable moments.
Return handling and performance checks
tests/test_xray_unreachable.py
Tests check batched and staggered device returns, lookup counts, and separate timing for state changes and settling in a large home.

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

Change: Bug fix

Possibly related PRs

  • frenck/spook#2637: Adds the persistent unreachable tracker and state model that this PR updates.
Merge Risk: Minimal · up to fdf84

This change makes recorded unreachable moments more accurate around Spook reloads and Home Assistant starts and stops. The supplied evidence shows targeted tests for these flows and no remaining actionable merge risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main theme of the change: X-ray's unreachable moments are kept accurate across Spook reloads, Home Assistant starts, and stops. The changeset covers each of these: stop-time guards…
Description check Passed The description relates directly to the changeset. It lists the seven review fixes, including reload handling, registry changes during startup, the stop-time guard in async_note and async_forget, and …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main theme of the change: X-ray's unreachable moments are kept accurate across Spook reloads, Home Assistant starts, and stops. The changeset covers each of these: stop-time guards in _Memory, clearing reporting state on Spook stop, registry changes during startup settling, and the benchmark. The title is specific, though long at 72 characters.

Full details: Description check

Explanation

The description relates directly to the changeset. It lists the seven review fixes, including reload handling, registry changes during startup, the stop-time guard in async_note and async_forget, and the benchmark change. It names the tests that cover each fix and states the declined review threads.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A rabbit checks the clock at night,
Then lets shutdown pass without a write.
Devices settle, returns appear,
The tracker waits, then marks them clear.
One quiet hop, and tests take flight.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d06bbc5a-e0e4-457e-aa2d-35bc583d97d3
Commits

Reviewing files that changed from the base of the PR and between 6de19c1 and fdf8441.

Files selected for processing: 1

  • tests/test_xray_unreachable.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Shutdown recovery can repeatedly schedule overdue callbacks with zero delay, and the benchmark omits the transition work it claims to measure.

2 open issues
What changed in this PR

Refines X-ray’s unreachable-device timestamps across Spook reloads and Home Assistant startup and shutdown, following #2637.

Changes:

  • Restarts reporting observation after reloads and defers registry handling until settling.
  • Prevents stored moments from changing during shutdown.
  • Adds lifecycle and recovery-timer tests and revises transition benchmarks.
File Description
tests/​test_xray_unreachable.py Adds lifecycle regressions and timer checks; revises benchmarks.
custom_components/​spook/​xray_unreachable.py Updates observation resets, startup handling, and shutdown persistence guards.

Review effort: Balanced

Comment thread tests/test_xray_unreachable.py Resolved
Nothing is forgotten then, so the timer for a device past its minute fired
again and again. And the benchmark times each change as the first one.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 17:45
coderabbitai[bot]
coderabbitai Bot previously approved these changes Dec 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Reloading before startup settles can erase reporting history, causing a device that recovers and then becomes unavailable to retain an outdated outage timestamp.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Seed reporting history on startup to catch pre-settle outages

custom_components/​spook/​xray_unreachable.py:398

If Spook reloads before startup settling while a recorded device is already reporting, clearing this history leaves its recovery unobserved until settling unless another relevant event arrives. A device that reports for several minutes after reload and goes unavailable within a minute of settling therefore keeps its old outage timestamp. Seed reporting history from current device states in async_start, without changing stored moments before settling, and add a regression test for this reload case.

2 resolved since last review

Review effort: Balanced

A reload of Spook before settling cleared who reports, and a device that
reported all along kept its old moment when it went right after settling.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 17:56
@frenck

frenck commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

On the review note about a reload before settling: real, and a side effect of clearing who reports when Spook stops. Fixed in 6de19c1: starting the tracker now notes every device that reports at that moment, so a reload while Home Assistant starts sees them from the reload on. test_a_reload_of_spook_while_starting_sees_who_reports covers it, and failed before the fix.

../Frenck

                       

Blogging my personal ramblings at frenck.dev
Or subscribe to my free bi-weekly newsletter: frenck.dev/newsletter

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The early return preserves stale reporting history across startup disable/re-enable events, which can clear a stored outage or mark a later departure exact before a fresh minute of reporting.

1 open issue

Review effort: Balanced

coderabbitai[bot]
coderabbitai Bot previously approved these changes Dec 4, 2026
Copilot AI balanced review requested due to automatic review settings December 4, 2026 18:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the device-unreachable timestamp changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants