From f68ccf30c7dc3238675a63448fe8f7fceb79aa59 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Tue, 20 Oct 2026 10:07:55 +0200 Subject: [PATCH] Drop the cached update notice when Control, engine or app version changed --- apps/host/src/lib/update-cache.ts | 10 +++++---- apps/host/src/lib/update-check.ts | 2 +- apps/host/tests/update-cache.test.mjs | 8 +++++++ strata_control/engine_component.py | 30 ++++++++++++++++++++++++--- strata_control/updater.py | 4 +++- tests/terminal/test_updater.py | 23 ++++++++++++++++++++ 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/apps/host/src/lib/update-cache.ts b/apps/host/src/lib/update-cache.ts index 643c8d17..11d34e76 100644 --- a/apps/host/src/lib/update-cache.ts +++ b/apps/host/src/lib/update-cache.ts @@ -7,8 +7,8 @@ import type { UpdateCheckResult } from './types'; */ const KEY = 'strata.update-check.v1'; -interface Stored { result: UpdateCheckResult; appVersion: string | null; engineVersion: string | null } -export interface InstalledVersions { appVersion?: string | null; engineVersion?: string | null } +interface Stored { result: UpdateCheckResult; appVersion: string | null; engineVersion: string | null; controlVersion?: string | null } +export interface InstalledVersions { appVersion?: string | null; engineVersion?: string | null; controlVersion?: string | null } const same = (a: string | null | undefined, b: string | null | undefined) => (a ?? null) === (b ?? null); @@ -16,13 +16,15 @@ export function readUpdateCache(now: InstalledVersions): UpdateCheckResult | nul try { const stored = JSON.parse(localStorage.getItem(KEY) ?? 'null') as Stored | null; if (!stored?.result?.strata?.status || !stored.result.engine?.status || !stored.result.checkedAt) return null; - if (!same(stored.appVersion, now.appVersion) || !same(stored.engineVersion, now.engineVersion)) return null; + // Still loading (no app version yet): show nothing rather than a result that may belong to the version before an update. + if (!now.appVersion) return null; + if (!same(stored.appVersion, now.appVersion) || !same(stored.engineVersion, now.engineVersion) || !same(stored.controlVersion, now.controlVersion)) return null; return stored.result; } catch { return null; } } export function saveUpdateCache(result: UpdateCheckResult, now: InstalledVersions) { - try { localStorage.setItem(KEY, JSON.stringify({ result, appVersion: now.appVersion ?? null, engineVersion: now.engineVersion ?? null } satisfies Stored)); } catch { /* Not remembering a result is harmless. */ } + try { localStorage.setItem(KEY, JSON.stringify({ result, appVersion: now.appVersion ?? null, engineVersion: now.engineVersion ?? null, controlVersion: now.controlVersion ?? null } satisfies Stored)); } catch { /* Not remembering a result is harmless. */ } } export function clearUpdateCache() { diff --git a/apps/host/src/lib/update-check.ts b/apps/host/src/lib/update-check.ts index 8512c953..e2ad594c 100644 --- a/apps/host/src/lib/update-check.ts +++ b/apps/host/src/lib/update-check.ts @@ -10,7 +10,7 @@ export function useInstalledVersions(): InstalledVersions { const native = useNative(); const engineVersion = useStore(s => s.engine?.engine.version); const appVersion = appVersionLabel(useStore(s => s.setup?.app.version), native?.version); - return { appVersion: appVersion ?? null, engineVersion: (native?.available ? native.installed?.engine?.version : engineVersion) ?? null }; + return { appVersion: appVersion ?? null, engineVersion: (native?.available ? native.installed?.engine?.version : engineVersion) ?? null, controlVersion: native?.version ?? null }; } /** The one place the app asks the host to check for updates. Called only from a button press. */ diff --git a/apps/host/tests/update-cache.test.mjs b/apps/host/tests/update-cache.test.mjs index 45145f3a..426ca631 100644 --- a/apps/host/tests/update-cache.test.mjs +++ b/apps/host/tests/update-cache.test.mjs @@ -27,6 +27,14 @@ test('a check made for other versions is ignored, so an old "Update available" d assert.equal(readUpdateCache({ appVersion: '0.2.1', engineVersion: '0.1.45' }), null); }); +test('a new Control version, or a version that is not known yet, hides the old result', () => { + saveUpdateCache(result(), { appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.45' }); + assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.45' }).engine.status, 'available'); + assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45', controlVersion: '0.1.46' }), null, 'Control changed'); + assert.equal(readUpdateCache({ appVersion: null, engineVersion: '0.1.45', controlVersion: '0.1.45' }), null, 'app version still loading'); + assert.equal(readUpdateCache({ appVersion: '0.2.0', engineVersion: '0.1.45' }), null, 'Control version not known'); +}); + test('unreadable or blocked storage is harmless', () => { store.set('strata.update-check.v1', '{not json'); assert.equal(readUpdateCache({}), null); diff --git a/strata_control/engine_component.py b/strata_control/engine_component.py index 38f7e901..02d689e4 100644 --- a/strata_control/engine_component.py +++ b/strata_control/engine_component.py @@ -189,14 +189,38 @@ def engine_dir(root: Path | None = None) -> Path: return (root or components_dir()) / "engine" +def newest_installed_version(root: Path | None = None) -> str | None: + """The highest engine version in the component folder, or None. A quick scan; nothing is hashed.""" + try: + found, _ = scan(root) + except Exception: # noqa: BLE001 - informational only + return None + return max((item.version for item in found), key=_version_key, default=None) + + def cached_notice(root: Path | None = None) -> str | None: """"update available (x.y.z): run `strata update`" from the last explicit check, or None. Reads one small local file; it never - touches the network, so showing it at a start does not break the rule that only an update step downloads.""" + touches the network, so showing it at a start does not break the rule that only an update step downloads. + + The notice belongs to the versions the check was made for: it is dropped when Control or the installed engine is not the one + recorded then (an app install, an update, a rollback), and an engine that is not newer than the installed one is never offered.""" + base = root or components_dir() try: - last = (json.loads(((root or components_dir()) / UPDATE_STATE).read_text(encoding="utf-8-sig")).get("last_check") or {}) + last = (json.loads((base / UPDATE_STATE).read_text(encoding="utf-8-sig")).get("last_check") or {}) except (OSError, ValueError, AttributeError): return None - parts = [f"engine {last['engine']}" for _ in [0] if last.get("engine")] + [f"app {last['app']}" for _ in [0] if last.get("app")] + if not isinstance(last, dict): + return None + control = runtime_version() + if "control" in last and last["control"] != control: + return None + engine, app = last.get("engine"), last.get("app") + installed = newest_installed_version(base) + if "engine_installed" in last and last["engine_installed"] != installed: + engine = None + if engine and installed and _version_key(str(engine)) <= _version_key(installed): + engine = None + parts = ([f"engine {engine}"] if engine else []) + ([f"app {app}"] if app else []) return f"update available ({', '.join(parts)}): run `strata update`" if parts else None diff --git a/strata_control/updater.py b/strata_control/updater.py index 2a2e2368..4a6aa94a 100644 --- a/strata_control/updater.py +++ b/strata_control/updater.py @@ -485,7 +485,9 @@ def check(ctx: Context) -> dict: state.data["last_check"] = { "at": (ctx.now or dt.datetime.now(dt.timezone.utc)).isoformat(), "engine": report["engine"]["offered"]["version"] if report["engine"]["status"] in ("available", "install") and report["engine"]["offered"] else None, - "app": report["app"]["offered"] if report["app"]["status"] == "available" else None} + "app": report["app"]["offered"] if report["app"]["status"] == "available" else None, + # the versions this result is for, so a later Control, engine or app change drops the cached notice + "control": ec.runtime_version(), "engine_installed": ec.newest_installed_version(ctx.components)} state.save() return report diff --git a/tests/terminal/test_updater.py b/tests/terminal/test_updater.py index 12025b2f..41bf7f2a 100644 --- a/tests/terminal/test_updater.py +++ b/tests/terminal/test_updater.py @@ -630,6 +630,29 @@ class Cli(UpdaterCase): def common(self): return ["--feed", str(self.release), "--components-dir", str(self.components), "--model-config", str(self.config)] + def test_cached_update_notice_is_dropped_when_versions_change(self): + self.install_current() + self.publish(("0.1.46", fx.GOOD_ENGINE)) + self.assertEqual(self.cli("update", "--check", *self.common())[0], 0) + notice = "update available (engine 0.1.46): run `strata update`" + self.assertEqual(ec.cached_notice(self.components), notice) + state_file = self.components / ec.UPDATE_STATE + saved = json.loads(state_file.read_text(encoding="utf-8")) + self.assertEqual(saved["last_check"]["engine_installed"], "0.1.45") + # a new app brings a new Control: the old result is not shown again + with mock.patch.object(ec, "runtime_version", return_value="9.9.9"): + self.assertIsNone(ec.cached_notice(self.components)) + # the installed engine changed since the check (an update from elsewhere, a rollback) + with mock.patch.object(ec, "newest_installed_version", return_value="0.1.44"): + self.assertIsNone(ec.cached_notice(self.components)) + # an old file without the recorded versions: still never offers an engine that is not newer + saved["last_check"] = {"at": "x", "engine": "0.1.45", "app": None} + state_file.write_text(json.dumps(saved), encoding="utf-8") + self.assertIsNone(ec.cached_notice(self.components)) + saved["last_check"] = {"at": "x", "engine": "0.1.46", "app": None} + state_file.write_text(json.dumps(saved), encoding="utf-8") + self.assertEqual(ec.cached_notice(self.components), notice) + def test_check_update_and_rollback_from_the_terminal(self): self.install_current() self.publish(("0.1.46", fx.GOOD_ENGINE))