Skip to content

fix(swarm-coordination-registry): [#2226] recompute inactivity cutoff per tick - #2252

Merged
josecelano merged 16 commits into
torrust:developfrom
josecelano:2226-fix-stale-inactivity-cutoff
Sep 18, 2026
Merged

josecelano merged 16 commits into
torrust:developfrom
josecelano:2226-fix-stale-inactivity-cutoff

Conversation

@josecelano

@josecelano josecelano commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Summary

The activity metrics updater now recomputes the peer inactivity cutoff before every update tick
instead of reusing a timestamp captured at job startup.

Problem

run_job computed now - max_peer_timeout once and passed that absolute timestamp to every
tick. Any peer announced after the tracker started therefore had updated > cutoff for the rest
of the process lifetime and was never reported inactive: the
swarm_coordination_registry_peers_inactive_total and ..._torrents_inactive_total gauges
stayed at 0. Peer cleanup has its own job and was unaffected; only the metrics were wrong.

Changes

  • run_job takes max_peer_timeout (the configured duration) instead of a precomputed cutoff;
    bootstrap wiring updated accordingly.
  • The cutoff derivation is extracted into a pure inactivity_cutoff(now, max_peer_timeout)
    and called with CurrentClock::now() on every tick.
  • Doc comments on Peer::updated and the updater clarify that the cutoff is an absolute
    timestamp; the naming question around DurationSinceUnixEpoch is parked as a follow-up draft
    in the issue folder.

Tests

  • activity_metrics_updater::tests::inactivity_cutoff: three pure unit tests (timeout before
    now; zero timeout yields now; now earlier than timeout falls back to the epoch so nothing is
    reported inactive).
  • statistics::tests: collaboration tests running the job against a real Registry and
    Repository through a state-named fixture (JobWithOnePeerAnnouncedAtStartup), with Tokio
    paused time driving the interval and clock::Stopped driving domain time:
    • peer active 1 s after startup with a 2 s timeout;
    • peer inactive 3 s after startup;
    • cutoff recomputed on every update: same job and peer observed active on the first tick and
      inactive on the second.
  • Mutation checks recorded in the commit history: reintroducing the startup-captured cutoff
    fails the after-timeout test; caching the cutoff after the first tick is caught only by the
    two-tick test. Production code was mutated in the working tree and restored, never committed.
  • Missing tests identified during the behaviour inventory are noted in the owning modules and
    tracked by Overhaul: Packages Testing #1347.

Documentation riding along

Lessons from this fix, made repository-owned:

  • write-unit-test skill: inventory what a module decides vs. forwards and choose the test
    level; two independent clocks in async job tests; prove a regression test fails against the
    reintroduced bug.
  • New catalog entry: pure decision function for collaborator-observed outcomes.
  • AGENTS.md: keep intermediate commits; minor process fixes may ride along in the PR that
    produced them.

Validation

cargo test -p torrust-tracker-swarm-coordination-registry --lib statistics: 30 passed.
Pre-commit (8 checks) and pre-push (nightly fmt, nightly check, nightly doc, full stable test
suite) pass on the rebased branch. Runtime verification against a fixed build is recorded in the
issue folder (manual-verification-evidence.md).

Closes #2226
Related to #1347

Copilot AI 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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

This PR fixes the swarm coordination registry activity metrics job so the “inactive peer” cutoff is recomputed on every update tick (instead of using a stale startup cutoff), and adds a regression test using a stopped clock to validate the behavior.

Changes:

  • Pass max_peer_timeout into the registry metrics job and compute the inactivity cutoff inside each tick.
  • Remove the bootstrap-layer helper that computed a single cutoff timestamp at startup.
  • Add a stopped-clock regression test to ensure peers become inactive after time advances post-start.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/bootstrap/jobs/activity_metrics_updater.rs Adjusts bootstrap wiring to pass max_peer_timeout instead of a precomputed cutoff.
packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs Recomputes inactivity cutoff per tick and adds a regression test using a stopped clock.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.79%. Comparing base (2b72ecd) to head (e35a1a6).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2252      +/-   ##
===========================================
+ Coverage    85.49%   85.79%   +0.29%     
===========================================
  Files          353      353              
  Lines        31333    31418      +85     
  Branches     31333    31418      +85     
===========================================
+ Hits         26789    26954     +165     
+ Misses        4165     4087      -78     
+ Partials       379      377       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI 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.

🟡 Changes recommended

The bootstrap job file no longer imports std::future::Future while still using -> impl Future<...>, which will fail compilation.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

…ivity boundary tests

Introduce JobWithOnePeerAnnouncedAtStartup so both boundary tests only state the peer timeout, the elapsed domain time, and the expected inactive peers count. The fixture owns runner startup, the announced peer, the update tick, gauge reading, and cancellation on drop.
…statistics collaboration level

The before/after timeout tests exercise the job together with the real Registry and Repository, so they now live in statistics::tests as collaboration tests. activity_metrics_updater::tests keeps only the job lifecycle unit tests. Missing tests identified while classifying are noted in the owning modules and tracked by the package coverage review (torrust#1347).
… computation

Move the cutoff derivation out of update_activity_metrics into inactivity_cutoff(now, max_peer_timeout) so the module's core decision is directly unit-testable. Pins three cases: cutoff is the timeout before now, a zero timeout yields now, and a now earlier than the timeout falls back to the epoch so nothing is reported inactive. No behaviour change.
… decision function pattern

Lessons from the torrust#2226 test refactoring, made repository-owned instead of agent memory:

- write-unit-test skill: inventory what a module decides vs. forwards before writing tests, place each test at the lowest level that can observe what it asserts, and split reorganisation from refactoring commits. Add a note that async jobs under Tokio paused time involve two independent clocks.
- New catalog entry: extract a pure decision function when a module's own decision is only observable through a collaborator, and move end-to-end tests to the parent module as collaboration tests.

Related to torrust#1347.
…n and make boundary failures self-explanatory

Mutation checks against the torrust#2226 fix showed the before/after pair catches a cutoff captured at job start but not one cached after the first update, because each test fires a single tick. Add a two-tick test on the same job and peer that fails for either variant.

Assertion messages now state the gauge, the elapsed time, and the timeout, so a failure reads as a diagnosis instead of a bare left/right pair.
…troduced bug

Add 'Prove a Regression Test Guards the Bug' to the write-unit-test skill: mutate the fix in the working tree and confirm the test fails, observe two iterations of the same instance for stale-state defects, and put the causal facts in assertion messages so a failure reads as a diagnosis. Two checklist items enforce it for bug fixes.

Learned while verifying torrust#2226: the before/after pair caught the original startup-captured cutoff but not a cutoff cached after the first tick.
… to ride along in PRs

Two maintainer decisions agents kept re-asking about during torrust#2226:

- Never squash or rewrite branch history to hide superseded steps; the mistake-and-correction sequence is training material.
- Minor skill/doc/process improvements learned during the work may ride along in the same PR as separate commits; separate PRs for them is unnecessary bureaucracy.

Copilot AI 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.

🟡 Changes recommended

The new paused-time collaboration tests and the updated write-unit-test guidance assume a current-thread Tokio runtime for thread-local clock::Stopped, but the tests/docs don’t consistently enforce or state that, risking flaky/incorrect results.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (2)

packages/swarm-coordination-registry/src/statistics/mod.rs:238

  • Same determinism issue as the earlier collaboration test: because the job is tokio::spawned and clock::Stopped::local_set is thread-local, this test should force a single-thread Tokio runtime to ensure the spawned job observes the same stopped-clock timeline.
    #[tokio::test(start_paused = true)]

packages/swarm-coordination-registry/src/statistics/mod.rs:257

  • Same determinism issue as the other collaboration tests in this module: without forcing flavor = "current_thread", the tokio::spawned job can run on a different worker thread and miss the test’s clock::Stopped::local_set updates, making the two-tick regression guard flaky.
    #[tokio::test(start_paused = true)]
  • Files reviewed: 17/17 changed files
  • Comments generated: 2
  • Review effort level: Lite

@josecelano

Copy link
Copy Markdown
Member Author

ACK e35a1a6

@josecelano
josecelano merged commit e6dd891 into torrust:develop Sep 18, 2026
26 of 29 checks passed

@da2ce7 da2ce7 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.

This is a post-merge review of the change from #2252 as it stands at a94c3ced8 on the default branch.

What still runs: the activity metrics job takes the configured max_peer_timeout in seconds (packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs:23) and derives the cutoff from CurrentClock::now() on every tick (packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs:75) through the pure inactivity_cutoff (packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs:93), and the startup wiring passes the configured value straight through (src/bootstrap/jobs/activity_metrics_updater.rs:19). The unit tests (packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs:217) and the collaboration tests (packages/swarm-coordination-registry/src/statistics/mod.rs:119) are live. Later commits changed only their surroundings: 162cd58 and 226820b adapted the tests to the registry and repository APIs, d25e92c replaced the two cast allowances with documented expectations, and acda244 moved the issue folder the code comments point to, with both pointers updated. Two statements in the description are broader than the code: the comment on Peer::updated (packages/primitives/src/peer.rs:128) fixes its grammar and links the follow-up draft, while the cutoff clarification lives on inactivity_cutoff (packages/swarm-coordination-registry/src/statistics/activity_metrics_updater.rs:89); and the inactive-torrents gauge did not stay at 0 before the fix, because it is the registry size minus the active torrents, so torrents without peers were already counted there.

[Minor] The inactive-torrents gauge this change repairs is computed as self.len() - active_torrents_total at packages/swarm-coordination-registry/src/swarm/registry.rs:240, a second read of the map's size after the pass instead of a count of what the pass visited. The pass awaits each swarm lock (packages/swarm-coordination-registry/src/swarm/registry.rs:226), and while it waits an announce or the cleanup job can run. Sequence: torrents A and B each hold one active peer and remove_peerless_torrents is on; the pass counts A active and then waits on B's lock; A's peer sends a stopped announce (packages/swarm-coordination-registry/src/swarm/coordinator.rs:40) and the cleanup job removes the now empty A (packages/swarm-coordination-registry/src/swarm/registry.rs:294); the pass then counts B active, so active_torrents_total is 2 while self.len() is 1. A release build wraps the usize subtraction and publishes about 1.8e19 inactive torrents until the next tick; a build with overflow checks panics and ends the job. The subtraction predates this pull request (677deac), and the per-tick cutoff narrows the window rather than widening it, since the job and the cleanup now agree on which peers are inactive. Smallest fix: count inactive torrents in the same loop (else { inactive_torrents_total += 1; }) so both totals come from one set of visited swarms; saturating_sub would stop the wrap but keep the two reads apart.

Checked and found sound: the cutoff is recomputed on every tick from the domain clock with the same derivation and the same updated <= cutoff boundary as peer cleanup (packages/tracker-core/src/torrent/manager.rs:104, packages/swarm-coordination-registry/src/swarm/coordinator.rs:122 and :197), so the gauges and the cleanup agree on which peers are inactive; checked_sub with the epoch fallback cannot panic for any u32 timeout, and production peers are stamped from the clock, so the fallback reports nothing inactive; gauge update errors are logged at error level by set_gauge (packages/swarm-coordination-registry/src/statistics/repository.rs:122) rather than lost; the job still starts only when tracker_usage_statistics is on (src/app.rs:782); and the collaboration tests run on a current-thread runtime, so the thread-local stopped clock they set is the one the spawned job reads, and the two-tick test fails if the cutoff is captured at startup or cached after the first tick.

This branch was previously deployed

1 inactive deployment
coverage — e35a1a68 Deployed Sep 17, 2026 by josecelano via Generate Coverage Report #2155
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.

fix(swarm-coordination-registry): calculate activity-metrics cutoff per update

3 participants