mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-02 05:38:12 +00:00
fix: rebind MemoryScope/MemorySlice to fresh Memory after checkpoint restore
This commit is contained in:
@@ -484,8 +484,36 @@ class Crew(FlowTrackable, BaseModel):
|
||||
if self.checkpoint_train is not None:
|
||||
self._train = self.checkpoint_train
|
||||
|
||||
self._rebind_memory_views()
|
||||
self._restore_event_scope()
|
||||
|
||||
def _rebind_memory_views(self) -> None:
|
||||
"""Reattach a live ``Memory`` to restored ``MemoryScope``/``MemorySlice`` views.
|
||||
|
||||
Checkpoint JSON omits the live ``Memory`` dependency on scope/slice
|
||||
views, so after restore they raise ``RuntimeError`` on first use.
|
||||
Build a fresh ``Memory`` and bind it to any unbound views on the
|
||||
crew and its agents.
|
||||
"""
|
||||
from crewai.memory.memory_scope import MemoryScope, MemorySlice
|
||||
from crewai.memory.unified_memory import Memory
|
||||
|
||||
fresh: Memory | None = None
|
||||
|
||||
def _ensure(view: Any) -> None:
|
||||
nonlocal fresh
|
||||
if not isinstance(view, MemoryScope | MemorySlice):
|
||||
return
|
||||
if view._memory is not None:
|
||||
return
|
||||
if fresh is None:
|
||||
fresh = Memory()
|
||||
view.bind(fresh)
|
||||
|
||||
_ensure(self.memory)
|
||||
for agent in self.agents:
|
||||
_ensure(agent.memory)
|
||||
|
||||
def _restore_event_scope(self) -> None:
|
||||
"""Rebuild the event scope stack from the checkpoint's event record."""
|
||||
from crewai.events.base_events import set_emission_counter
|
||||
|
||||
@@ -1098,6 +1098,11 @@ class Flow(BaseModel, Generic[T], metaclass=FlowMeta):
|
||||
}
|
||||
if self.checkpoint_state is not None:
|
||||
self._restore_state(self.checkpoint_state)
|
||||
if (
|
||||
isinstance(self.memory, MemoryScope | MemorySlice)
|
||||
and self.memory._memory is None
|
||||
):
|
||||
self.memory.bind(Memory())
|
||||
restore_event_scope(())
|
||||
reset_last_event_id()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user