mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-27 17:00:37 +00:00
fix(artifacts): skip unusable stored rows during consumption tracking (#8525)
* fix(artifacts): skip unusable stored rows during consumption tracking A checkpoint row that merge_tool_artifacts skips can still reach the consumption scan. Keying every dict by entry["handle"] raised KeyError from before_model on every later model call, and a non-list consumed_by either raised or became a character list. Filter the registry the way the reducer does and treat a non-list consumed_by as empty. * fix(artifacts): filter invalid handles before capture --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
parent
fc0d4bd7bc
commit
b5e9999b15
@ -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]}
|
||||
|
||||
@ -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.
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user