Repository navigation
feat(session-py): support snapshot session management for Graph and Swarm #4240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7ff162d
feat(session-py): add multiAgent snapshot scope
50bae63
feat(session-py): persist Graph and Swarm snapshots
ea21066
test(session-py): cover multi-agent snapshot persistence and restore
14f3a30
test(session-py): update VALID_SCOPES assertion for multiAgent scope
901dff6
Merge branch 'main' into agent-tasks/3662
nicolas-m-romero 4408ed0
Merge branch 'main' into agent-tasks/3662
nicolas-m-romero 914d50d
Merge branch 'main' into agent-tasks/3662
nicolas-m-romero 933a5fe
fix(session-py): harden multi-agent snapshot restore
5b807e7
Merge branch 'main' into agent-tasks/3662
nicolas-m-romero 31cda7c
refactor(session-py): align restore guard with TypeScript
af67b17
refactor(multiagent-py): mark snapshot helpers internal
7f00b9e
Merge remote-tracking branch 'origin/main' into agent-tasks/3662
d0c5c21
refactor(session-py): use snake case for multi-agent scope
f2bcb58
refactor(session-py): address multi-agent API feedback
e8545c1
Merge remote-tracking branch 'origin/main' into agent-tasks/3662
c90b115
refactor(session-py): remove redundant parity comments
4d7d043
fix(session-py): restore multiAgent snapshot contract
53e1862
Merge remote-tracking branch 'origin/main' into agent-tasks/3662
4b38b74
docs(session-py): document multi-agent snapshots
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """Snapshot capture and restore for multi-agent orchestrators (Graph and Swarm). | ||
|
|
||
| Wraps an orchestrator's :meth:`~strands.multiagent.base.MultiAgentBase.serialize_state` | ||
| output in the versioned :class:`~strands.types._snapshot.Snapshot` envelope so the persisted | ||
| format can evolve, mirroring the TypeScript SDK's ``multiagent/snapshot.ts``. | ||
|
|
||
| Well-known keys in ``data``: | ||
|
|
||
| - ``orchestrator_id`` — orchestrator identity, verified on load. | ||
| - ``state`` — the orchestrator's serialized execution state. | ||
| """ | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from ..types._snapshot import SNAPSHOT_SCHEMA_VERSION, Snapshot | ||
| from ..types.exceptions import SnapshotException | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .base import MultiAgentBase | ||
|
|
||
|
|
||
| def take_snapshot(orchestrator: "MultiAgentBase", *, app_data: dict[str, Any] | None = None) -> Snapshot: | ||
| """Capture an orchestrator's execution state as a versioned snapshot. | ||
|
|
||
| Args: | ||
| orchestrator: The Graph or Swarm to capture. | ||
| app_data: Application-owned data. Strands does not read or modify it. | ||
|
|
||
| Returns: | ||
| A ``multiAgent``-scope snapshot wrapping the orchestrator's serialized state. | ||
| """ | ||
| return Snapshot( | ||
| scope="multiAgent", | ||
| schema_version=SNAPSHOT_SCHEMA_VERSION, | ||
| data={"orchestrator_id": orchestrator.id, "state": orchestrator.serialize_state()}, | ||
| app_data=app_data or {}, | ||
| ) | ||
|
|
||
|
|
||
| def load_snapshot(orchestrator: "MultiAgentBase", snapshot: Snapshot) -> None: | ||
| """Restore an orchestrator's execution state from a snapshot. | ||
|
|
||
| Args: | ||
| orchestrator: The Graph or Swarm to restore into. | ||
| snapshot: The snapshot to load. | ||
|
|
||
| Raises: | ||
| SnapshotException: If the snapshot is not a current-schema ``multiAgent`` snapshot, | ||
| or its ``orchestrator_id`` does not match ``orchestrator.id``. | ||
| """ | ||
| snapshot.validate() | ||
| if snapshot.scope != "multiAgent": | ||
| raise SnapshotException(f"Expected snapshot scope 'multiAgent', got {snapshot.scope!r}") | ||
|
|
||
| snapshot_id = snapshot.data.get("orchestrator_id") | ||
| if snapshot_id != orchestrator.id: | ||
| raise SnapshotException(f"Snapshot orchestrator id mismatch: expected {orchestrator.id!r}, got {snapshot_id!r}") | ||
|
|
||
| orchestrator.deserialize_state(snapshot.data["state"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.