Compare commits

...

11 Commits

Author SHA1 Message Date
Devin AI
66382f9928 Merge remote-tracking branch 'origin/devin/1737272386-flow-override-fix' into devin/1737272386-flow-override-fix 2025-01-19 08:54:35 +00:00
João Moura
968e0f504b refactoring test 2025-01-19 08:54:24 +00:00
Devin AI
ce6747b967 test: add has_set_count field to PoemState
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 08:54:24 +00:00
Devin AI
89b5edf6b2 test: improve state restoration verification with has_set_count flag
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 08:54:24 +00:00
Devin AI
b5c26aeadd fix: ensure persisted state overrides class defaults
- Remove early return in Flow.__init__ to allow proper state initialization
- Add test_flow_default_override.py to verify state override behavior
- Fix issue where default values weren't being overridden by persisted state

Fixes the issue where persisted state values weren't properly overriding
class defaults when restarting a flow with a previously saved state ID.

Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 08:54:24 +00:00
João Moura
b75ef482bf refactoring test 2025-01-19 00:51:53 -08:00
João Moura
48b0f3b1b3 Merge branch 'main' into devin/1737272386-flow-override-fix 2025-01-19 05:51:31 -03:00
João Moura
d3b67345d5 Merge branch 'main' into devin/1737272386-flow-override-fix 2025-01-19 05:37:15 -03:00
Devin AI
db626ec99d test: add has_set_count field to PoemState
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 08:30:38 +00:00
Devin AI
4f4f229a0b test: improve state restoration verification with has_set_count flag
Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 08:28:58 +00:00
Devin AI
44b92ef12f fix: ensure persisted state overrides class defaults
- Remove early return in Flow.__init__ to allow proper state initialization
- Add test_flow_default_override.py to verify state override behavior
- Fix issue where default values weren't being overridden by persisted state

Fixes the issue where persisted state values weren't properly overriding
class defaults when restarting a flow with a previously saved state ID.

Co-Authored-By: Joe Moura <joao@crewai.com>
2025-01-19 07:42:38 +00:00
2 changed files with 65 additions and 6 deletions

View File

@@ -499,12 +499,8 @@ class Flow(Generic[T], metaclass=FlowMeta):
elif kwargs and "id" in kwargs:
self._log_flow_event(f"Loading flow state from memory for ID: {kwargs['id']}", color="bold_yellow")
stored_state = self._persistence.load_state(kwargs["id"])
if not stored_state:
# For kwargs["id"], we allow creating new state if not found
self._state = self._create_initial_state()
if kwargs:
self._initialize_state(kwargs)
return
# Don't return early if state not found - let the normal initialization flow handle it
# This ensures proper state initialization and override behavior
# Initialize state based on persistence and kwargs
if stored_state:

View File

@@ -0,0 +1,63 @@
"""Test that persisted state properly overrides default values."""
import os
from typing import Optional
import pytest
from pydantic import BaseModel
from crewai.flow.flow import Flow, FlowState, start
from crewai.flow.persistence import persist
from crewai.flow.persistence.sqlite import SQLiteFlowPersistence
class PoemState(FlowState):
"""Test state model with default values that should be overridden."""
sentence_count: int = 1000 # Default that should be overridden
has_set_count: bool = False # Track whether we've set the count
def test_default_value_override():
"""Test that persisted state values override class defaults."""
@persist()
class PoemFlow(Flow[PoemState]):
initial_state = PoemState
@start()
def set_sentence_count(self):
print("Setting sentence count")
print(self.state)
# Only set sentence_count on first run, not when loading from persistence
if self.state.has_set_count and self.state.sentence_count == 2:
self.state.sentence_count = 3
elif self.state.has_set_count and self.state.sentence_count == 1000:
self.state.sentence_count = 1000
elif self.state.has_set_count and self.state.sentence_count == 5:
self.state.sentence_count = 5
else:
self.state.sentence_count = 2
self.state.has_set_count = True
# First run - should set sentence_count to 2
flow1 = PoemFlow()
flow1.kickoff()
original_uuid = flow1.state.id
assert flow1.state.sentence_count == 2
# Second run - should load sentence_count=2 instead of default 1000
flow2 = PoemFlow()
flow2.kickoff(inputs={"id": original_uuid})
assert flow2.state.sentence_count == 3 # Should load 2, not default 1000
# Third run - should not load sentence_count=2 instead of default 1000
flow2 = PoemFlow()
flow2.kickoff(inputs={"has_set_count": True})
assert flow2.state.sentence_count == 1000 # Should load 1000, not 2
# Third run - explicit override should work
flow3 = PoemFlow(
id=original_uuid,
sentence_count=5, # Override persisted value
)
assert flow3.state.sentence_count == 5 # Should use override value