Files
nanobot/nanobot/session/summary.py
T
chengyongru 3548aec576 fix: use summary checkpoints for all compaction triggers (#5694)
* fix: exclude archived history after manual compaction

* refactor: share summary checkpoints across compaction paths

* fix: use summary checkpoints for every compaction trigger

* test: assert replay starts at the archived boundary

* fix: preserve nothing summaries without raw fallback

* test: assert compaction results positively

* test: verify checkpoint continuation after session reload
2026-09-08 13:16:45 +08:00

59 lines
1.7 KiB
Python

"""Helpers for validated session-summary metadata."""
from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass
from datetime import datetime
from typing import Any, TypedDict, cast
from nanobot.session.history_visibility import is_hidden_history_message
SUMMARY_CONTINUATION_TEXT = (
"Continue the active task from the working-memory checkpoint above."
)
def is_summary_checkpoint(message: Mapping[str, Any]) -> bool:
"""Identify the durable boundary of a replacement summary."""
return (
is_hidden_history_message(message)
and message.get("content") == SUMMARY_CONTINUATION_TEXT
)
class SessionSummary(TypedDict):
text: str
last_active: str
@dataclass(frozen=True, slots=True)
class SessionSummaryCheckpoint:
"""A replacement summary and the raw transcript boundary it covers."""
summary: str
transcript_boundary: int
def session_summary_from_metadata(
metadata: Mapping[str, object] | None,
*,
fallback_last_active: datetime,
) -> SessionSummary | None:
raw: object = metadata.get("_last_summary") if metadata is not None else None
if not isinstance(raw, Mapping):
return None
summary_data = cast(Mapping[str, object], raw)
text = summary_data.get("text")
if not isinstance(text, str) or not text:
return None
raw_last_active = summary_data.get("last_active")
if isinstance(raw_last_active, str):
try:
datetime.fromisoformat(raw_last_active)
last_active = raw_last_active
except ValueError:
last_active = fallback_last_active.isoformat()
else:
last_active = fallback_last_active.isoformat()
return {"text": text, "last_active": last_active}