fix: suppress duplicate lifecycle events on checkpoint resume

This commit is contained in:
Greyson LaLonde
2026-04-03 23:02:16 +08:00
parent c653d41b89
commit 5ace0bfe4a
2 changed files with 27 additions and 9 deletions

View File

@@ -369,10 +369,12 @@ class Crew(FlowTrackable, BaseModel):
json_str = _Path(path).read_text()
from crewai import RuntimeState
from crewai.events.event_bus import crewai_event_bus
state = RuntimeState.model_validate_json(
json_str, context={"from_checkpoint": True}
)
crewai_event_bus.set_runtime_state(state)
for entity in state.root:
if isinstance(entity, cls):
if entity.execution_context is not None:

View File

@@ -278,7 +278,9 @@ def prepare_kickoff(
from crewai.events.event_context import get_current_parent_id, reset_last_event_id
from crewai.events.types.crew_events import CrewKickoffStartedEvent
if get_current_parent_id() is None:
resuming = crew._kickoff_event_id is not None
if not resuming and get_current_parent_id() is None:
reset_emission_counter()
reset_last_event_id()
@@ -296,15 +298,29 @@ def prepare_kickoff(
normalized = {}
normalized = before_callback(normalized)
started_event = CrewKickoffStartedEvent(crew_name=crew.name, inputs=normalized)
if crew._kickoff_event_id is None:
if resuming:
if crew.verbose:
from crewai.events.utils.console_formatter import ConsoleFormatter
fmt = ConsoleFormatter(verbose=True)
content = fmt.create_status_content(
"Resuming from Checkpoint",
crew.name or "Crew",
"bright_magenta",
ID=str(crew.id),
)
fmt.print_panel(
content, "\U0001f504 Resuming from Checkpoint", "bright_magenta"
)
else:
started_event = CrewKickoffStartedEvent(crew_name=crew.name, inputs=normalized)
crew._kickoff_event_id = started_event.event_id
future = crewai_event_bus.emit(crew, started_event)
if future is not None:
try:
future.result()
except Exception: # noqa: S110
pass
future = crewai_event_bus.emit(crew, started_event)
if future is not None:
try:
future.result()
except Exception: # noqa: S110
pass
crew._task_output_handler.reset()
crew._logging_color = "bold_purple"