diff mbox series

[auh,v2,1/3] modules/state: rework state to include both original and updated version

Message ID 20261203212050.799982-1-alex@linutronix.de
State New
Headers show
Series [auh,v2,1/3] modules/state: rework state to include both original and updated version | expand

Commit Message

Alexander Kanavin Dec. 3, 2026, 9:20 p.m. UTC
The state information in incremental mode did not keep the original versions
that recipes were updated from, only the new versions. This is problematic
when upstreams release new versions in quick succession: if a layer updates
a recipe to an intermediate version after AUH produced an update from the original
version to latest version, AUH will not re-roll the update from an intermediate
to latest because it doesn't track the original versions, and the update
will only be retried after a retry period (typically 30 days) expires.

Additionally, it would be useful to know when the original version, or the latest
version was first seen by AUH; if either period grows too much, that means the AUH upgrade
attempts are being ignored, the recipe isn't being updated, and the situation needs
to be looked at.

The patch reworks the state format from being a dict of recipes containing a dict
of (latest) versions to a flat dict of recipes that contains the original version,
the new version, when either was first seen, and when an AUH did the update. The
rest of the code is adapted to that; it's difficult to test all the corner cases, but
I confirmed that at least the update and the skipping after work as they should.

Note that AUH will not try to migrate the v1 state to v2 state. It will simply
detect that the state is not usable, and re-run the full update cycle to repopulate
state.

AI-Generated: Claude Sonnet 5.5
Signed-off-by: Alexander Kanavin <alex@linutronix.de>
---
 modules/state.py  | 77 +++++++++++++++++++++--------------------------
 upgrade-helper.py |  8 ++---
 2 files changed, 39 insertions(+), 46 deletions(-)
diff mbox series

Patch

diff --git a/modules/state.py b/modules/state.py
index d70972b..9a10d32 100644
--- a/modules/state.py
+++ b/modules/state.py
@@ -32,7 +32,7 @@  RESULT_SUCCESS = "success"
 RESULT_FAILURE = "failure"
 
 STATE_FILENAME = "auh-state.json"
-STATE_VERSION = 1
+STATE_VERSION = 2
 
 SECONDS_PER_DAY = 86400
 DEFAULT_RETRY_SAME_VERSION_DAYS = 30
@@ -41,7 +41,9 @@  DEFAULT_SUCCESS_MAX_AGE_DAYS = 30
 
 
 class State:
-    """Tracks upgrade attempts as {recipe: {version: {timestamp, result}}}."""
+    """Tracks upgrade attempts as a flat dict, one entry per recipe:
+    {recipe: {oldversion, oldversion_firstseen, newversion, newversion_firstseen,
+              timestamp, result}}."""
 
     def __init__(self, state_dir,
                  retry_same_version_days=DEFAULT_RETRY_SAME_VERSION_DAYS,
@@ -53,10 +55,9 @@  class State:
         self.success_max_age = success_max_age_days * SECONDS_PER_DAY
         self.retry_any_version = retry_any_version_days * SECONDS_PER_DAY
         self.any_version_recipes = set(retry_any_version_recipes or [])
-        self.data = self._load()
-        self._prune()
+        self.data = self._load_state_file()
 
-    def _load(self):
+    def _load_state_file(self):
         if os.path.exists(self.path):
             try:
                 with open(self.path) as f:
@@ -76,38 +77,46 @@  class State:
             json.dump({"version": STATE_VERSION, "recipes": self.data},
                       f, indent=2)
 
-    def record(self, pn, version, result):
-        entry = {"timestamp": time.time(), "result": result}
-        if pn not in self.data:
-            self.data[pn] = {}
-        self.data[pn][version] = entry
-
-    def should_skip(self, pn, version):
-        """Return True if this recipe/version should not be attempted."""
-        return self.check(pn, version)['skip']
-
-    def skip_reason(self, pn, version):
-        """Return a human-readable string explaining why pn/version is being skipped."""
-        return self.check(pn, version)['reason']
-
-    def check(self, pn, version):
+    def record(self, pn, old_version, new_version, result):
+        current_time = time.time()
+        oldversion_firstseen = current_time
+        newversion_firstseen = current_time
+
+        if pn in self.data:
+            if self.data[pn]['oldversion'] == old_version:
+                oldversion_firstseen = self.data[pn]['oldversion_firstseen']
+            if self.data[pn]['newversion'] == new_version:
+                newversion_firstseen = self.data[pn]['newversion_firstseen']
+
+        self.data[pn] = {"timestamp": current_time,
+                         "result": result,
+                         "oldversion": old_version,
+                         "oldversion_firstseen": oldversion_firstseen,
+                         "newversion": new_version,
+                         "newversion_firstseen": newversion_firstseen,
+                         }
+
+    def check(self, pn, old_version, new_version):
         """Return {'skip': bool, 'reason': str} for a recipe/version pair.
 
         For any_version_recipes, any recent attempt (any version) causes a skip.
-        For normal recipes, only a matching version entry causes a skip.
+        For normal recipes, only an entry for the same old and new version
+        causes a skip.
         """
+
+        entry = self.data.get(pn, {})
+
+        if not entry:
+            return {'skip': False, 'reason': ''}
+
         if pn in self.any_version_recipes:
-            entry = self._newest_entry(pn)
             max_age = self.retry_any_version
         else:
-            entry = self.data.get(pn, {}).get(version)
-            if entry is None:
-                return {'skip': False, 'reason': ''}
             max_age = self.success_max_age \
                 if entry.get("result") == RESULT_SUCCESS \
                 else self.retry_same_version
 
-        if not entry:
+        if pn not in self.any_version_recipes and (entry['oldversion'] != old_version or entry['newversion'] != new_version):
             return {'skip': False, 'reason': ''}
 
         ts = entry.get("timestamp", 0)
@@ -122,19 +131,3 @@  class State:
         return {'skip': True,
                 'reason': "%slast attempt %s result=%s; will retry in %d day(s)"
                           % (prefix, when, result, retry_in)}
-
-    def _newest_entry(self, pn):
-        """Return the most recent entry for a recipe across all versions."""
-        versions = self.data.get(pn, {})
-        return max(versions.values(), key=lambda e: e.get("timestamp", 0),
-                   default={})
-
-    def _prune(self):
-        """Remove entries that check() would no longer skip."""
-        for pn in list(self.data):
-            versions = self.data[pn]
-            for ver in list(versions):
-                if not self.check(pn, ver)['skip']:
-                    del versions[ver]
-            if not versions:
-                del self.data[pn]
diff --git a/upgrade-helper.py b/upgrade-helper.py
index 56b1f02..8e47e40 100755
--- a/upgrade-helper.py
+++ b/upgrade-helper.py
@@ -498,10 +498,10 @@  class Updater(object):
         if self.state:
             kept = []
             for g in pkggroups_ctx:
-                pn, npv = g['pkgs'][0]['PN'], g['pkgs'][0]['NPV']
-                r = self.state.check(pn, npv)
+                pn, opv, npv = g['pkgs'][0]['PN'], g['pkgs'][0]['PV'], g['pkgs'][0]['NPV']
+                r = self.state.check(pn, opv, npv)
                 if r['skip']:
-                    I(" %s %s: skipping (%s)" % (pn, npv, r['reason']))
+                    I(" %s %s -> %s: skipping (%s)" % (pn, opv, npv, r['reason']))
                     self.statistics.skip(g, r['reason'])
                 else:
                     kept.append(g)
@@ -579,7 +579,7 @@  class Updater(object):
                 # upgrades them atomically; individual outcomes are not tracked.
                 for pkg_ctx in g['pkgs']:
                     I(" %s %s: recording result=%s" % (pkg_ctx['PN'], pkg_ctx['NPV'], result))
-                    self.state.record(pkg_ctx['PN'], pkg_ctx['NPV'], result)
+                    self.state.record(pkg_ctx['PN'], pkg_ctx['PV'], pkg_ctx['NPV'], result)
 
         if self.state:
             self.state.save()