fix(agent): close runtime context persistence gaps
This commit is contained in:
1 parent
f75d3519db
commit
2eb7398f34
10 files changed
+235
-20
No files matched your search
+42
-3
@@ -65,7 +65,7 @@ class SessionClient:
|
||||
return snapshot_from_session(session)
|
||||
|
||||
def get(self, session_key: str) -> SessionSnapshot | None:
|
||||
"""Return a session snapshot without creating a new session on disk."""
|
||||
"""Return a display-safe snapshot without creating a new session on disk."""
|
||||
cached = self._loop.sessions._cache.get(session_key)
|
||||
if cached is not None:
|
||||
return snapshot_from_session(cached)
|
||||
@@ -89,8 +89,47 @@ class SessionClient:
|
||||
]
|
||||
|
||||
def export(self, session_key: str) -> SessionSnapshot | None:
|
||||
"""Return a full session snapshot suitable for JSON serialization."""
|
||||
return self.get(session_key)
|
||||
"""Return a trusted full snapshot, including model-only runtime context."""
|
||||
cached = self._loop.sessions._cache.get(session_key)
|
||||
if cached is not None:
|
||||
return snapshot_from_session(cached, include_runtime_context=True)
|
||||
payload = self._loop.sessions.read_session_file(session_key)
|
||||
if payload is None:
|
||||
return None
|
||||
return snapshot_from_payload(payload, include_runtime_context=True)
|
||||
|
||||
async def restore(
|
||||
self,
|
||||
snapshot: SessionSnapshot,
|
||||
*,
|
||||
session_key: str | None = None,
|
||||
save: bool = True,
|
||||
) -> SessionSnapshot:
|
||||
"""Restore a trusted snapshot into an empty session."""
|
||||
key = session_key or snapshot.key
|
||||
if not key:
|
||||
raise ValueError("restored snapshots must include a session key")
|
||||
session = self._loop.sessions.get_or_create(key)
|
||||
if session.messages:
|
||||
raise ValueError(f"restore target session is not empty: {key}")
|
||||
session.metadata.update(deepcopy(snapshot.metadata))
|
||||
|
||||
for raw in snapshot.messages:
|
||||
if "role" not in raw or "content" not in raw:
|
||||
raise ValueError("restored messages must include role and content")
|
||||
role = str(raw["role"]).strip()
|
||||
if role not in self._VALID_ROLES:
|
||||
raise ValueError(f"unsupported message role: {role!r}")
|
||||
extra = {
|
||||
field: deepcopy(value)
|
||||
for field, value in raw.items()
|
||||
if field not in {"role", "content"}
|
||||
}
|
||||
session.add_message(role, deepcopy(raw["content"]), **extra)
|
||||
|
||||
if save:
|
||||
self._loop.sessions.save(session)
|
||||
return snapshot_from_session(session)
|
||||
|
||||
def clear(self, session_key: str) -> SessionSnapshot:
|
||||
"""Clear one session and persist the empty session."""
|
||||
|
||||
+23
-9
@@ -79,7 +79,7 @@ class StreamEvent:
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SessionSnapshot:
|
||||
"""A durable snapshot of one nanobot session."""
|
||||
"""A serializable session snapshot; trusted exports may include internal context."""
|
||||
|
||||
key: str
|
||||
messages: list[dict[str, Any]]
|
||||
@@ -121,27 +121,41 @@ class SessionInfo:
|
||||
}
|
||||
|
||||
|
||||
def snapshot_from_session(session: Any) -> SessionSnapshot:
|
||||
def snapshot_from_session(
|
||||
session: Any,
|
||||
*,
|
||||
include_runtime_context: bool = False,
|
||||
) -> SessionSnapshot:
|
||||
messages = deepcopy(session.messages)
|
||||
if not include_runtime_context:
|
||||
messages = public_history_messages(messages)
|
||||
return SessionSnapshot(
|
||||
key=session.key,
|
||||
created_at=session.created_at.isoformat(),
|
||||
updated_at=session.updated_at.isoformat(),
|
||||
metadata=deepcopy(session.metadata),
|
||||
messages=public_history_messages(session.messages),
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
|
||||
def snapshot_from_payload(payload: Mapping[str, Any]) -> SessionSnapshot:
|
||||
def snapshot_from_payload(
|
||||
payload: Mapping[str, Any],
|
||||
*,
|
||||
include_runtime_context: bool = False,
|
||||
) -> SessionSnapshot:
|
||||
messages = [
|
||||
deepcopy(dict(message))
|
||||
for message in list(payload.get("messages") or [])
|
||||
if isinstance(message, Mapping)
|
||||
]
|
||||
if not include_runtime_context:
|
||||
messages = public_history_messages(messages)
|
||||
return SessionSnapshot(
|
||||
key=str(payload.get("key") or ""),
|
||||
created_at=payload.get("created_at"),
|
||||
updated_at=payload.get("updated_at"),
|
||||
metadata=deepcopy(dict(payload.get("metadata") or {})),
|
||||
messages=public_history_messages(
|
||||
message
|
||||
for message in list(payload.get("messages") or [])
|
||||
if isinstance(message, Mapping)
|
||||
),
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user