feat(agent): add take_snapshot() and load_snapshot() methods#1948
feat(agent): add take_snapshot() and load_snapshot() methods#1948zastrowm wants to merge 4 commits intostrands-agents:mainfrom
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
src/strands/agent/agent.py
Outdated
| data = snapshot.data | ||
|
|
||
| if "messages" in data: | ||
| self.messages = data["messages"] |
There was a problem hiding this comment.
Issue: Messages list is assigned by reference, not copied. This means modifying snapshot.data["messages"] after calling load_snapshot() would mutate the agent's messages unexpectedly.
Suggestion: Use list(data["messages"]) to create a shallow copy, consistent with how take_snapshot() captures messages on line 1086.
There was a problem hiding this comment.
✅ Addressed! The code now uses copy.deepcopy(data["messages"]) and includes dedicated tests (test_load_snapshot_messages_are_independent_copy, test_take_snapshot_messages_are_independent_copy) to verify isolation. Nice work on being thorough with the copy semantics.
|
Assessment: Request Changes This PR introduces valuable snapshot functionality that mirrors the TypeScript SDK. The implementation is well-structured with comprehensive tests and good documentation. A couple of items need attention before merging. Review Categories
Thanks for the clean implementation — the selective restore behavior and the distinction between |
|
Assessment: Comment (Updated Review) The previous code quality concern about messages being assigned by reference has been addressed with Remaining Items Before Merge
The implementation is looking solid — ready to proceed once the API review process is initiated. |
Description
Adds
take_snapshot()andload_snapshot()to the PythonAgentclass, mirroring the snapshot API already present in the TypeScript SDK. Snapshots are versioned, JSON-serializable captures of agent state that can be stored and restored across agent instances.Supported fields:
messages,state,conversation_manager_state,interrupt_state,system_prompt. The"session"preset captures all fields exceptsystem_prompt.system_promptis stored as content blocks to preserve caching hints and other block-level metadata across round-trips. Restore is selective — only fields present in the snapshot are applied; absent fields leave the target agent unchanged. Anullvalue forsystem_promptin the snapshot explicitly clears the prompt on restore, distinct from the field being absent entirely.Related Issues
#1138
Documentation PR
In Progress
Type of Change
New feature
Testing
hatch run prepareAdded
tests/strands/agent/test_snapshot.pywith full coverage including:to_dict()/from_dict()app_datastored verbatimresolve_snapshot_fieldspreset → include → exclude orderingsystem_promptclears target agent's prompt on restore; snapshot withoutsystem_promptfield preserves target agent's promptChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.