tokensafe 4.4.7 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ import urllib.request
17
17
  import urllib.error
18
18
  from typing import Dict, Any, Optional, Tuple
19
19
 
20
- CURRENT_VERSION = "4.4.7"
20
+ CURRENT_VERSION = "4.5.0"
21
21
  CACHE_TTL = 86400 # 24 hours
22
22
  # When the cloud is unreachable we must NOT cache the synthesized "up to date"
23
23
  # answer for a full day — otherwise an update released right after an outage
@@ -83,6 +83,21 @@ class VersionChecker:
83
83
  except Exception:
84
84
  return None
85
85
 
86
+ @staticmethod
87
+ def parse_semver(v: str) -> Tuple[int, int, int]:
88
+ """Parses a version string into a (major, minor, patch) integer tuple."""
89
+ clean = str(v or "").strip().lstrip("v")
90
+ parts = re.findall(r"\d+", clean)
91
+ nums = [int(p) for p in parts[:3]]
92
+ while len(nums) < 3:
93
+ nums.append(0)
94
+ return (nums[0], nums[1], nums[2])
95
+
96
+ @classmethod
97
+ def is_newer_version(cls, candidate: str, current: str) -> bool:
98
+ """Returns True strictly when candidate is semantically strictly greater than current."""
99
+ return cls.parse_semver(candidate) > cls.parse_semver(current)
100
+
86
101
  @classmethod
87
102
  def save_cache(cls, data: Dict[str, Any]):
88
103
  cache_path = cls.get_cache_path()
@@ -159,6 +174,13 @@ class VersionChecker:
159
174
  if resp.status == 200:
160
175
  data = json.loads(resp.read().decode("utf-8"))
161
176
  data["checked_at"] = now
177
+ # Guarantee client_version is recorded in cache
178
+ if not data.get("client_version"):
179
+ data["client_version"] = current_version
180
+ # Sanity check: never report an update if latest is not strictly newer
181
+ latest_ver = data.get("latest_version", current_version)
182
+ if data.get("update_available") and not cls.is_newer_version(latest_ver, current_version):
183
+ data["update_available"] = False
162
184
  # TS1 sunset: transparently swap in the reissued TS2 key.
163
185
  # Never persist the key inside version_cache.json — it is a secret.
164
186
  reissued = data.pop("reissued_license_key", "")
@@ -168,6 +190,11 @@ class VersionChecker:
168
190
  LicenseManager.save_license(reissued)
169
191
  except Exception:
170
192
  pass
193
+ # Preserve last OS notification timestamps across cache refreshes
194
+ if cached and cls._cache_matches_version(cached, current_version):
195
+ for k in ("last_os_notify_version", "last_os_notify_at"):
196
+ if k in cached and k not in data:
197
+ data[k] = cached[k]
171
198
  data.pop("ttl", None) # authoritative server answer: full 24h cache
172
199
  cls.save_cache(data)
173
200
  return data
@@ -204,6 +231,11 @@ class VersionChecker:
204
231
  latest = info.get("latest_version", current_version)
205
232
  cmd = info.get("update_command", "tokensafe update")
206
233
 
234
+ # Sanity check: never show banner for downgrade or equal version
235
+ if update_available and not cls.is_newer_version(latest, current_version):
236
+ update_available = False
237
+ force_update = False
238
+
207
239
  if force_update:
208
240
  return (
209
241
  f"\n{RED}{BOLD}╔════════════════════════════════════════════════════════════════════════╗{RESET}\n"
@@ -357,6 +389,10 @@ class VersionChecker:
357
389
  latest = info.get("latest_version", current_version)
358
390
  cmd = info.get("update_command", "tokensafe update")
359
391
 
392
+ # Sanity check: never notify if latest is not strictly newer (prevents downgrades)
393
+ if not cls.is_newer_version(latest, current_version) and not force:
394
+ return False
395
+
360
396
  if not update_available and not force:
361
397
  return False
362
398
 
@@ -426,10 +462,10 @@ class VersionChecker:
426
462
  last_notified_ver = cached.get("last_os_notify_version", "")
427
463
  last_notified_at = cached.get("last_os_notify_at", 0)
428
464
 
429
- # If cache is valid and no update is available, skip completely
465
+ # If cache is valid and no update is available (or latest is <= current), skip completely
430
466
  # (ttl-aware: offline fallback entries expire after FALLBACK_CACHE_TTL)
431
467
  ttl = int(cached.get("ttl", CACHE_TTL))
432
- if now - checked_at < ttl and not update_available:
468
+ if now - checked_at < ttl and (not update_available or not cls.is_newer_version(latest, current_version)):
433
469
  return
434
470
 
435
471
  # If update is available but user was already notified within 24h, skip