fix(gemini): drop ttl values whose expiry Google cannot store (past the year 9999)

This commit is contained in:
mateo-berri 2026-09-19 01:00:27 -07:00
parent 3684e5cbcb
commit 5ad1847835
2 changed files with 7 additions and 6 deletions

View file

@ -6,6 +6,7 @@ Why separate file? Make it easy to see how transformation works
import re
from collections.abc import Sequence
from datetime import datetime, timezone
from types import MappingProxyType
from typing import Final, Literal
@ -89,7 +90,7 @@ def extract_ttl_from_cached_messages(messages: list[AllMessageValues]) -> str |
_TTL_PATTERN: Final = re.compile(r"^([0-9]*\.?[0-9]+)([smh])$")
_TTL_UNIT_SECONDS: Final = MappingProxyType({"s": 1, "m": 60, "h": 3600})
_PROTOBUF_DURATION_MAX_SECONDS: Final = 315_576_000_000
_LAST_EXPIRY_GOOGLE_ACCEPTS: Final = datetime(9999, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
def _normalize_ttl_to_seconds(ttl: object) -> str | None:
@ -99,7 +100,8 @@ def _normalize_ttl_to_seconds(ttl: object) -> str | None:
if match is None:
return None
seconds: Final = round(float(match.group(1)) * _TTL_UNIT_SECONDS[match.group(2)], 9)
if not 0 < seconds <= _PROTOBUF_DURATION_MAX_SECONDS:
longest_ttl: Final = (_LAST_EXPIRY_GOOGLE_ACCEPTS - datetime.now(timezone.utc)).total_seconds()
if not 0 < seconds <= longest_ttl:
return None
return f"{seconds:.9f}".rstrip("0").rstrip(".") + "s"

View file

@ -21,8 +21,7 @@ class TestTTLNormalization:
("1h", "3600s"),
("0.5h", "1800s"),
("48h", "172800s"),
("315576000000s", "315576000000s"),
("87660000h", "315576000000s"),
("61320000h", "220752000000s"),
],
)
def test_normalizes_supported_units_to_seconds(self, ttl, expected):
@ -45,8 +44,8 @@ class TestTTLNormalization:
"3600ss",
"1 h",
"0.0000000001s",
"315576000001s",
"87660001h",
"251700000000s",
"69920000h",
"9" * 400 + "h",
None,
123,