From fa6cca4f64a5d7944e648a1732b37e2e8112a770 Mon Sep 17 00:00:00 2001 From: patrick Date: Sun, 1 Nov 2026 14:53:19 -0500 Subject: [PATCH] Don't let failed apt updates make package lists look fresh apt touches /var/lib/apt/lists/partial on every update attempt, including ones that fail, and the agent took the newest of it and the two success stamps. So a host whose apt update failed hourly reported lists an hour old. Use the success stamps (update-success-stamp, update-stamp) and fall back to lists/partial only when neither exists. Co-Authored-By: Claude Opus 5.5 --- agent/kernelwatch-agent | 14 +++++++++----- docs/how-it-works.md | 11 ++++++++--- tests/test_agent.py | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/agent/kernelwatch-agent b/agent/kernelwatch-agent index c9b0ded..7e08156 100755 --- a/agent/kernelwatch-agent +++ b/agent/kernelwatch-agent @@ -228,18 +228,22 @@ phased_packages() { } # Seconds since apt last refreshed its package lists, or empty if unknown. +# Both stamps are touched only after a successful update: update-success-stamp by +# Ubuntu's update-notifier hook and kernelwatch-apt-update.service, update-stamp by +# apt-daily. lists/partial changes on every attempt, failed ones too, so it's only +# a last resort for hosts that have neither (Debian without either timer). apt_lists_age() { local newest=0 f m for f in \ "$ROOT/var/lib/apt/periodic/update-success-stamp" \ - "$ROOT/var/lib/apt/periodic/update-stamp" \ - "$ROOT/var/lib/apt/lists/partial"; do - [[ -e $f ]] || continue + "$ROOT/var/lib/apt/periodic/update-stamp"; do m=$(stat -c %Y "$f" 2>/dev/null) || continue ((m > newest)) && newest=$m done - ((newest > 0)) && printf '%s' $(($(date +%s) - newest)) - return 0 + if ((newest == 0)); then + newest=$(stat -c %Y "$ROOT/var/lib/apt/lists/partial" 2>/dev/null) || return 0 + fi + printf '%s' $(($(date +%s) - newest)) } # Size and free space of the filesystem holding /boot, in KiB, as "SIZE FREE"; diff --git a/docs/how-it-works.md b/docs/how-it-works.md index 1222850..ac70042 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -141,9 +141,14 @@ How the agent works it out: list also on the host page. They never affect status, because Ubuntu doesn't phase security updates and there's nothing to act on until apt offers them. -- **Package list freshness**: the mtime of apt's update stamps. The agent - **doesn't run `apt update`** itself; it relies on the stock `apt-daily.timer`. - If that's disabled, the host shows `update` with "package lists N days old". +- **Package list freshness**: the mtime of apt's success stamps, + `update-success-stamp` (Ubuntu, and `kernelwatch-apt-update.service`) and + `update-stamp` (`apt-daily`), both in `/var/lib/apt/periodic/` and touched + only when `apt update` succeeds. A host with neither falls back to + `/var/lib/apt/lists/partial`, which changes on failed updates too. The agent + **doesn't run `apt update`** itself; it relies on `apt-daily.timer` or + `--with-apt-update`. If the lists stop refreshing, the host shows `update` + with "package lists N days old". The agent sends raw facts and the server decides what they mean, so you can change the status rules without touching the agents. diff --git a/tests/test_agent.py b/tests/test_agent.py index c13a8c4..f8c1845 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -446,6 +446,23 @@ X os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp")) self.assertIsNone(self.report()["apt_lists_age_seconds"]) + def test_newest_success_stamp_wins(self): + self.write_root("var/lib/apt/periodic/update-stamp", age=600) + self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 600, delta=60) + + def test_failed_updates_touching_partial_dont_count_as_fresh(self): + # apt touches lists/partial even when the update fails. + os.makedirs(os.path.join(self.root, "var/lib/apt/lists/partial")) + self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 7200, delta=60) + + def test_partial_is_the_last_resort_without_stamps(self): + os.remove(os.path.join(self.root, "var/lib/apt/periodic/update-success-stamp")) + partial = os.path.join(self.root, "var/lib/apt/lists/partial") + os.makedirs(partial) + t = time.time() - 3600 + os.utime(partial, (t, t)) + self.assertAlmostEqual(self.report()["apt_lists_age_seconds"], 3600, delta=60) + def test_send_without_config_fails_cleanly(self): proc = self.run_agent() self.assertNotEqual(proc.returncode, 0)