-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[EPD-197] Warn locally when a persisted flow gets a non-UUID id input (AMP parity) #6539
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
Open
joaomdmoura
wants to merge
2
commits into
main
Choose a base branch
from
joao/epd-197-amp-non-uuid-id-input-rejected-with-422-on-amp-undocumented
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−1
Open
Changes from all commits
Commits
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
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,129 @@ | ||
| """Tests for the non-UUID flow state id warning (CrewAI AMP parity). | ||
|
|
||
| CrewAI AMP deployments reject non-UUID ``inputs.id`` values with HTTP 422, | ||
| while the OSS library accepts any string locally. A persisted flow kicked off | ||
| with a non-UUID ``id`` should surface a one-time warning so the mismatch is | ||
| caught before deployment — without changing local behavior. | ||
| """ | ||
|
|
||
| import os | ||
| import warnings | ||
| from uuid import uuid4 | ||
|
|
||
| from crewai.flow.flow import Flow, start | ||
| from crewai.flow.persistence import persist | ||
| from crewai.flow.persistence.sqlite import SQLiteFlowPersistence | ||
|
|
||
|
|
||
| AMP_WARNING_MATCH = "CrewAI AMP" | ||
|
|
||
|
|
||
| def _build_persisted_flow_class(persistence): | ||
| class PersistedFlow(Flow[dict]): | ||
| initial_state = dict | ||
|
|
||
| @start() | ||
| @persist(persistence) | ||
| def init_step(self): | ||
| self.state["message"] = "ran" | ||
|
|
||
| return PersistedFlow | ||
|
|
||
|
|
||
| def test_non_uuid_id_with_persistence_warns_once_and_still_runs(tmp_path): | ||
| """A non-UUID `id` input on a persisted flow warns once but runs fine.""" | ||
| db_path = os.path.join(tmp_path, "test_flows.db") | ||
| persistence = SQLiteFlowPersistence(db_path) | ||
| flow = _build_persisted_flow_class(persistence)(persistence=persistence) | ||
|
|
||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| flow.kickoff(inputs={"id": "session-1"}) | ||
|
|
||
| amp_warnings = [ | ||
| w for w in caught if AMP_WARNING_MATCH in str(w.message) | ||
| ] | ||
| assert len(amp_warnings) == 1, ( | ||
| f"expected exactly one AMP parity warning, got {len(amp_warnings)}" | ||
| ) | ||
| assert issubclass(amp_warnings[0].category, UserWarning) | ||
| message = str(amp_warnings[0].message) | ||
| assert "422" in message | ||
| assert "UUID" in message | ||
|
|
||
| # Behavior is unchanged: the flow ran and kept the non-UUID id locally. | ||
| assert flow.state["id"] == "session-1" | ||
| assert flow.state["message"] == "ran" | ||
| assert persistence.load_state("session-1") is not None | ||
|
|
||
|
|
||
| def test_valid_uuid_id_with_persistence_does_not_warn(tmp_path): | ||
| """A valid UUID `id` input on a persisted flow emits no AMP warning.""" | ||
| db_path = os.path.join(tmp_path, "test_flows.db") | ||
| persistence = SQLiteFlowPersistence(db_path) | ||
| flow = _build_persisted_flow_class(persistence)(persistence=persistence) | ||
| valid_id = str(uuid4()) | ||
|
|
||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| flow.kickoff(inputs={"id": valid_id}) | ||
|
|
||
| amp_warnings = [ | ||
| w for w in caught if AMP_WARNING_MATCH in str(w.message) | ||
| ] | ||
| assert amp_warnings == [] | ||
| assert flow.state["id"] == valid_id | ||
| assert flow.state["message"] == "ran" | ||
|
|
||
|
|
||
| def test_non_uuid_id_with_method_level_persist_only_warns(tmp_path): | ||
| """Method-level @persist (no instance/class persistence) still warns. | ||
|
|
||
| Method-level @persist never sets `flow.persistence`, but the flow still | ||
| saves state under the provided id, so the AMP parity warning must fire. | ||
| """ | ||
| db_path = os.path.join(tmp_path, "test_flows.db") | ||
| persistence = SQLiteFlowPersistence(db_path) | ||
| # No persistence= constructor arg: only the method is persisted. | ||
| flow = _build_persisted_flow_class(persistence)() | ||
| assert flow.persistence is None | ||
|
|
||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| flow.kickoff(inputs={"id": "session-1"}) | ||
|
|
||
| amp_warnings = [ | ||
| w for w in caught if AMP_WARNING_MATCH in str(w.message) | ||
| ] | ||
| assert len(amp_warnings) == 1, ( | ||
| f"expected exactly one AMP parity warning, got {len(amp_warnings)}" | ||
| ) | ||
|
|
||
| # Behavior is unchanged: the method-level @persist saved the state. | ||
| assert flow.state["id"] == "session-1" | ||
| assert flow.state["message"] == "ran" | ||
| assert persistence.load_state("session-1") is not None | ||
|
|
||
|
|
||
| def test_non_uuid_id_without_persistence_does_not_warn(): | ||
| """Without persistence, a non-UUID `id` input emits no AMP warning.""" | ||
|
|
||
| class PlainFlow(Flow[dict]): | ||
| initial_state = dict | ||
|
|
||
| @start() | ||
| def init_step(self): | ||
| self.state["message"] = "ran" | ||
|
|
||
| flow = PlainFlow() | ||
|
|
||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always") | ||
| flow.kickoff(inputs={"id": "session-1"}) | ||
|
|
||
| amp_warnings = [ | ||
| w for w in caught if AMP_WARNING_MATCH in str(w.message) | ||
| ] | ||
| assert amp_warnings == [] | ||
| assert flow.state["id"] == "session-1" | ||
| assert flow.state["message"] == "ran" |
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.