diff --git a/backend/packages/harness/deerflow/agents/middlewares/artifact_capture_middleware.py b/backend/packages/harness/deerflow/agents/middlewares/artifact_capture_middleware.py index 5f61b5cb49..82bbc09507 100644 --- a/backend/packages/harness/deerflow/agents/middlewares/artifact_capture_middleware.py +++ b/backend/packages/harness/deerflow/agents/middlewares/artifact_capture_middleware.py @@ -93,7 +93,7 @@ class ArtifactCaptureMiddleware(AgentMiddleware[AgentState]): messages = state.get("messages") or [] existing = state.get("tool_artifacts") or [] - existing_handles = {entry.get("handle") for entry in existing if isinstance(entry, dict)} + existing_handles = {handle for entry in existing if isinstance(entry, dict) and isinstance(handle := entry.get("handle"), str) and handle} processed = set(state.get("tool_artifact_processed") or []) newly_processed: list[str] = [] occurrences: dict[str, int] = {} @@ -140,7 +140,11 @@ class ArtifactCaptureMiddleware(AgentMiddleware[AgentState]): # registration rides along in the same update, and settling the scan as # "quiet" on a transient miss would lose the consumption mark forever. registry_items: list[Any] = [*(state.get("tool_artifacts") or []), *(pending_entries or [])] - handle_map: dict[str, ArtifactEntry] = {entry["handle"]: cast(ArtifactEntry, entry) for entry in registry_items if isinstance(entry, dict)} + # A checkpoint can carry a row the reducer skipped: written by an + # integration, restored from an older version, or edited through the + # state route. Key only usable handles, as merge_tool_artifacts does, + # so one bad row cannot abort every later model call. + handle_map: dict[str, ArtifactEntry] = {entry["handle"]: cast(ArtifactEntry, entry) for entry in registry_items if isinstance(entry, dict) and isinstance(entry.get("handle"), str) and entry["handle"]} messages = state.get("messages") or [] consumed_updates: list[ArtifactEntry] = [] @@ -169,7 +173,10 @@ class ArtifactCaptureMiddleware(AgentMiddleware[AgentState]): if entry is None: had_unresolved = True continue - consumed = list(entry.get("consumed_by") or []) + raw_consumed = entry.get("consumed_by") + # The field is a list; a caller-written string must not + # become a character list, and a non-iterable would raise. + consumed = raw_consumed if isinstance(raw_consumed, list) else [] if tool_call_id in consumed: continue updated: dict[str, Any] = {**entry, "consumed_by": [*consumed, tool_call_id]} diff --git a/backend/tests/test_artifact_capture_middleware.py b/backend/tests/test_artifact_capture_middleware.py index 01a15faa6d..deb23e99e4 100644 --- a/backend/tests/test_artifact_capture_middleware.py +++ b/backend/tests/test_artifact_capture_middleware.py @@ -350,6 +350,64 @@ class TestCapture: third = middleware.before_model(third_state, _runtime("t")) assert third is None or "tool_artifacts" not in (third or {}) + def test_ignores_stored_rows_without_a_usable_handle(self): + """A checkpoint row the reducer tolerates must not abort every later model call.""" + middleware = ArtifactCaptureMiddleware() + handle = generate_handle("thread_1", "call_1", 0) + entry = { + "handle": handle, + "artifact_type": "file", + "real_ref": "/mnt/user-data/outputs/report.md", + "display_name": "report.md", + "tool_name": "write_file", + "mime_type": None, + "consumed_by": [], + } + state = { + "tool_artifacts": [ + {"display_name": "legacy.md"}, + {"handle": None}, + {"handle": 7}, + {"handle": ""}, + {"handle": [handle]}, + {"handle": {"legacy": handle}}, + entry, + ], + "messages": [ + AIMessage(content="", tool_calls=[{"name": "read_file", "args": {"path": handle}, "id": "call_2", "type": "tool_call"}]), + ], + } + + out = middleware.before_model(state, _runtime()) + + assert out is not None + assert out["tool_artifacts"] == [{**entry, "consumed_by": ["call_2"]}] + + def test_treats_a_non_list_consumed_by_as_empty(self): + """Only a list can be extended; a caller-written string must not become a char list.""" + middleware = ArtifactCaptureMiddleware() + handle = generate_handle("thread_1", "call_1", 0) + entry = { + "handle": handle, + "artifact_type": "file", + "real_ref": "/mnt/user-data/outputs/report.md", + "display_name": "report.md", + "tool_name": "write_file", + "mime_type": None, + "consumed_by": "stale-value", + } + state = { + "tool_artifacts": [entry], + "messages": [ + AIMessage(content="", tool_calls=[{"name": "read_file", "args": {"path": handle}, "id": "call_2", "type": "tool_call"}]), + ], + } + + out = middleware.before_model(state, _runtime()) + + assert out is not None + assert out["tool_artifacts"][0]["consumed_by"] == ["call_2"] + def test_capture_and_consumption_updates_concatenate(self): """Both a fresh capture and a consumption update in one before_model call must survive.