Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/agents/sandbox/memory/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import io
import json
import re
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
Expand All @@ -13,6 +14,11 @@
from ..session.base_sandbox_session import BaseSandboxSession
from ..workspace_paths import sandbox_path_str

_OFFSET_TIMESTAMP = re.compile(
r"(?P<clock>[0-9-]+[T ][0-9:]+)(?:[.,](?P<fraction>[0-9]+))?"
r"(?:Z|(?P<hours>[+-][0-9]{2})(?::?(?P<minutes>[0-9]{2}))?)"
)


def decode_payload(payload: object) -> str:
if isinstance(payload, str):
Expand Down Expand Up @@ -225,14 +231,21 @@ async def _list_current_selection_items(self) -> list[PhaseTwoSelectionItem]:


def _updated_at_sort_key(raw_memory: str) -> tuple[int, str]:
for line in raw_memory.splitlines():
if line.startswith("updated_at:"):
_, value = line.split(":", maxsplit=1)
updated_at = value.strip()
if not updated_at or updated_at == "unknown":
return (0, "")
return (1, updated_at)
return (0, "")
updated_at = _extract_metadata_value(raw_memory, "updated_at")
if not updated_at or updated_at == "unknown":
return (0, "")
match = _OFFSET_TIMESTAMP.fullmatch(updated_at)
if match is None:
return (1, updated_at)
offset = f"{match['hours'] or '+00'}:{match['minutes'] or '00'}"
try:
clock = datetime.fromisoformat(match["clock"] + offset)
utc = clock.astimezone(timezone.utc).replace(tzinfo=None)
except (ValueError, OverflowError):
return (1, updated_at)
# Compare the original fraction separately so datetime cannot truncate it to microseconds.
fraction = (match["fraction"] or "").rstrip("0")
return (1, f"{utc.isoformat(timespec='seconds')}.{fraction}")


def _extract_selection_item(raw_memory: str) -> PhaseTwoSelectionItem | None:
Expand Down
22 changes: 19 additions & 3 deletions tests/sandbox/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,23 @@ def test_updated_at_sort_key_places_unknown_timestamps_last() -> None:


@pytest.mark.asyncio
async def test_phase_two_selection_tracks_added_retained_and_removed_rollouts() -> None:
@pytest.mark.parametrize(
("older_timestamp", "newer_timestamp"),
[
pytest.param("2025-03-01T00:00:00Z", "2025-03-02T00:00:00Z", id="utc"),
pytest.param("2026-11-17T03:30:00+00:00", "2026-11-16T23:30:00-05:00", id="mixed-offsets"),
pytest.param("2026-11-17T03:30:00.1Z", "2026-11-16T23:30:00.1-05:00", id="tenths"),
pytest.param("2026-11-17T03:30:00+0000", "2026-11-16T23:30:00-0500", id="compact-offsets"),
pytest.param(
"2026-11-17T03:30:00.123456001Z",
"2026-11-17T03:30:00.123456002Z",
id="nanoseconds",
),
],
)
async def test_phase_two_selection_tracks_added_retained_and_removed_rollouts(
older_timestamp: str, newer_timestamp: str
) -> None:
client = FilesystemTestSandboxClient()
session = await client.create(manifest=Manifest())

Expand All @@ -609,7 +625,7 @@ async def test_phase_two_selection_tracks_added_retained_and_removed_rollouts()
await storage.ensure_layout()
old_item = PhaseTwoSelectionItem(
rollout_id="old-rollout",
updated_at="2025-03-01T00:00:00Z",
updated_at=older_timestamp,
rollout_path="sessions/old-rollout.jsonl",
rollout_summary_file="rollout_summaries/old-rollout.md",
terminal_state="completed",
Expand All @@ -627,7 +643,7 @@ async def test_phase_two_selection_tracks_added_retained_and_removed_rollouts()
storage.raw_memories_dir / "new-rollout.md",
_raw_memory_record(
rollout_id="new-rollout",
updated_at="2025-03-02T00:00:00Z",
updated_at=newer_timestamp,
rollout_summary_file="rollout_summaries/new-rollout.md",
raw_memory="new raw",
),
Expand Down
Loading