From 55601d0db0d61006b6e3abf65038832b78563d06 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 07:08:44 -0500 Subject: [PATCH] fix(cold-store-reaper): offset-less LastModified parses to None (#2110) An offset-less ISO stamp in an S3 listing parsed successfully to a naive datetime, breaking _parse_s3_list_page's aware-or-None contract: the naive stamp crashed evict_scan's age subtraction on any single such object and the sort/max newest picks of evict_scan/backup_supabase on mixed listings (TypeError: offset-naive vs offset-aware). Per triage 3066's pinned rule, an offset-less stamp is now unparseable -> None: its zone is unknown, it is never reinterpreted as UTC, and None reads as too-young (non-evictable) downstream. Z / explicit-offset / absent behaviour unchanged. Tests: parser contract (offset-less, absent, Z, +05:00), mixed-listing evict_scan (offset-less -> too-young, neighbours unaffected), uniformly-offset-less evict_scan (the age-check trap), backup_supabase newest pick over a mixed listing. Full test file on the laptop: 78 tests OK (skipped=1). --- pkgs/cold-store-reaper/cold_store.py | 8 ++ .../tests/test_cold_store.py | 94 +++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/pkgs/cold-store-reaper/cold_store.py b/pkgs/cold-store-reaper/cold_store.py index 6abb452388..532e900e5c 100755 --- a/pkgs/cold-store-reaper/cold_store.py +++ b/pkgs/cold-store-reaper/cold_store.py @@ -162,6 +162,14 @@ def _parse_s3_list_page(body: bytes): stamp = datetime.fromisoformat(raw.replace("Z", "+00:00")) except ValueError: stamp = None + if stamp is not None and stamp.tzinfo is None: + # Offset-less = zone unknown = unparseable (the + # aware-or-None contract above): never reinterpret + # it as UTC, because eviction deletes objects and + # must not act on a stamp whose zone is unknown. + # None reads as oldest/too-young downstream, which + # is non-evictable by construction (#2110). + stamp = None objects.append( { "key": fields.get("Key") or "", diff --git a/pkgs/cold-store-reaper/tests/test_cold_store.py b/pkgs/cold-store-reaper/tests/test_cold_store.py index 4a598fad17..4cd620f21e 100644 --- a/pkgs/cold-store-reaper/tests/test_cold_store.py +++ b/pkgs/cold-store-reaper/tests/test_cold_store.py @@ -1851,6 +1851,100 @@ def test_verdict_matrix(self): self.assertIn(self.keys["evict1"], self.s3.objects) +class TestOffsetLessLastModified(EvictTestBase): + """#2110: an offset-less LastModified has no zone, so it is + unparseable -> None (the aware-or-None contract of + _parse_s3_list_page). It must never reach a comparison or the + age subtraction as a naive datetime, and it must never be + reinterpreted as UTC: eviction deletes objects, and None reads + as too-young, which is non-evictable by construction.""" + + def _listed(self): + return { + o["key"]: o for o in self.store.s3_list_objects("supabase-backups") + } + + def test_offset_less_stamp_parses_to_none(self): + body = ( + '' + '' + "supabase-backupsfalse" + "a" + "2026-10-01T00:00:00.000" + "1" + "b" + "2026-10-01T00:00:00.000Z" + "1" + "c" + "2026-10-01T05:00:00+05:00" + "1" + "d1" + "" + ).encode() + objects, truncated, _token = cold_store._parse_s3_list_page(body) + self.assertFalse(truncated) + by_key = {o["key"]: o["last_modified"] for o in objects} + self.assertIsNone(by_key["a"]) # offset-less -> None + self.assertIsNone(by_key["d"]) # absent -> None, as before + # Z and explicit-offset stamps are unchanged + self.assertEqual(by_key["b"], datetime(2026, 10, 1, tzinfo=timezone.utc)) + self.assertEqual(by_key["c"].utcoffset(), timedelta(hours=5)) + + def test_mixed_listing_scan_offset_less_is_too_young(self): + # one offset-less stamp among aware neighbours: the sort and + # the age check both used to die with TypeError + self.s3.put(self.keys["evict1"], b"1", "2026-01-05T00:00:00.000") + scan = self.scan_by_key() + self.assertIsNone(scan[self.keys["evict1"]]["last_modified"]) + self.assertEqual(scan[self.keys["evict1"]]["verdict"], "too-young") + self.assertEqual(scan[self.keys["evict2"]]["verdict"], "evictable") + self.assertEqual(self.s3.deleted, []) + + def test_uniformly_offset_less_listing_scan_completes(self): + # every stamp offset-less: the sort survives naive stamps, + # but the age subtraction does not -- the trap a sort-only + # test would miss (triage case 5) + for key in self.keys.values(): + self.s3.put(key, b"x", "2026-01-01T00:00:00.000") + scan = self.scan_by_key() + for name in ("young", "oldtimer", "evict1", "evict2"): + self.assertEqual(scan[self.keys[name]]["verdict"], "too-young") + self.assertEqual( + scan[self.keys["unverified"]]["verdict"], "bundle-unverified" + ) + self.assertEqual(self.s3.deleted, []) + + def test_aware_stamps_unchanged_in_listing(self): + listed = self._listed() + self.assertEqual( + listed[self.keys["oldtimer"]]["last_modified"], + datetime(2026, 6, 1, tzinfo=timezone.utc), + ) + + +class TestOffsetLessBackupPick(S3TestBase): + """backup_supabase picks the newest by LastModified with max(): + a mixed listing used to crash the pick outright (#2110).""" + + def test_newest_pick_ignores_offset_less_stamp(self): + aware_body = b"aware backup payload " * 10 + aware_key = self.key_for(hashlib.sha256(aware_body).hexdigest()) + naive_body = b"naive backup payload " * 10 + naive_key = self.key_for(hashlib.sha256(naive_body).hexdigest()) + self.s3.put(aware_key, aware_body, "2026-10-21T02:00:00.000Z") + # a later wall-clock reading, but its zone is unknown, so it + # is not evidence of newness and must not win the pick + self.s3.put(naive_key, naive_body, "2026-10-23T02:00:00.000") + self.s3.put( + "pg/muse-dev/manifest.json", b"{}", "2026-10-22T02:00:00.000Z" + ) + result = self.store.backup_supabase() + self.assertEqual(result["source_key"], aware_key) + self.assertEqual( + result["sha256"], hashlib.sha256(aware_body).hexdigest() + ) + + class TestEvictApply(EvictTestBase): def test_apply_deletes_only_evictable_and_records(self): scan = self.store.evict_scan(min_age_days=14, keep_newest=2)