Repository navigation
fix(gateway): reject an unusable promoted record at the input boundary - #8170
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Run input still accepts reserved Overwrite markers that bypass merge_promoted on initialized channels, allowing malformed promotion state to persist instead of returning 400.
1 open issue
What changed in this PR
This PR hardens DeerFlow’s deferred-tool promotion state by validating caller-supplied promoted records at the Gateway boundary and allowing recovery from stored non-object values.
Changes:
- Adds promotion-record validation to run input and thread-state updates.
- Makes
merge_promotedtolerate non-object stored values and updates. - Adds boundary and reducer regression tests, plus a changelog entry.
| File | Description |
|---|---|
CHANGELOG.md |
Documents input validation and checkpoint recovery. |
backend/tests/test_thread_state_promoted.py |
Tests recovery from non-object promotion values. |
backend/tests/test_gateway_services.py |
Tests rejected and accepted promotion records at both boundaries. |
backend/packages/harness/deerflow/agents/thread_state.py |
Guards the promotion reducer against non-object values. |
backend/app/gateway/services.py |
Validates promotion records while preserving state-update metadata sanitation. |
Review effort: Balanced
25aed94 to
62bcdea
Compare
|
Rebased onto main ( |
willem-bd
left a comment
There was a problem hiding this comment.
Reviewed at head 62bcdea. The approach checks out end to end: both routes validate before a value can reach the channel — normalize_input for input.promoted and strip_server_owned_state_metadata for values.promoted (which the thread-state update route wraps in Overwrite in routers/threads.py), and the marker rejection covers both dict spellings, closing the gap Copilot's review flagged. The reducer-side non-Mapping guards also make a pre-existing poisoned checkpoint recover instead of failing every later promotion write, which the pure-boundary alternatives (including the sibling PR) do not attempt.
Relation to sibling PR #8158 (same issue, near-identical title): this is a competing implementation, and the finding posted on #8158 does not apply here — that PR's dedicated values.promoted branch dropped the _strip_external_metadata_from_message_like sanitization the generic else branch previously applied, while this PR keeps it explicitly (services.py, with a comment saying so). This PR is strictly broader: it adds the shared _reject_overwrite_marker helper, the erased-Overwrite form, and reducer-side poison recovery, so if both merge they will conflict in strip_server_owned_state_metadata and the gateway tests; this one subsumes #8158's scope. Note also that this PR deliberately keeps promoted caller-writable (validated) rather than rejecting it as a server-owned channel — the docstring itself says only the server writes it, so the stricter option is defensible, but the documented tradeoff is reasonable and matches the sibling.
Two non-blocking inline notes: a changelog housekeeping item, and a test suggestion pinning the metadata strip that differentiates this PR from #8158.
|
@gdut4140, please remove the change to CHANGLOG.md to avoid the conflict. |
62bcdea to
272d89f
Compare
|
Removed the Rebased onto
Re-verified after the rebase: Leaving the changelog note out entirely; say the word if you would rather have a one-line entry added back once the order settles. |
24b9884 to
75a7fb8
Compare

Fixes #8157
Why
promotedis a checkpointed reducer channel that both gateway input routes accept from a caller, andmerge_promotedreads the caller-supplied side unguarded:new.get("turn_id")(L177),new["catalog_hash"](L178),new["names"](L182). A malformed value therefore fails the run while the state update merges — an unhandled 500 at the boundary, the same class astodos(#7803) andskill_context(#8091). The thread-state route is worse: it wraps reducer channels inOverwrite, so the bad shape is persisted, and every later genuine promotion write (tool_search,deferred_tool_filter_middleware) keeps crashing on the stored value.Reproduced on
main@b883dea, through the real functions:A shape check alone is not enough: LangGraph recognises a JSON-erased
Overwriteas{"type": "__overwrite__", "value": ...}, so a record that carries that marker next to valid fields is applied directly to the channel and the reducer never sees it. Verified on a realStateGraphwith an initializedpromotedchannel: the value'oops'lands in state unchecked.What changed
_normalize_external_promotedinapp/gateway/services.pyrequires an object with a non-empty stringcatalog_hash, a list of stringsnames, and an optional stringturn_id; anything else is a 400 naming the offending field. It mirrors_normalize_external_skill_context._reject_overwrite_markerrefuses bothOverwritedict spellings (the sentinel{"__overwrite__": ...}form and the erased{"type": "__overwrite__", "value": ...}form)._normalize_external_goalnow calls the same helper instead of its inline check, so the two validators cannot drift.normalize_input(input.promoted) andstrip_server_owned_state_metadata(values.promoted), so a bad value cannot reach the reducer or be persisted. The state route keeps the generic metadata strip the newer branch would otherwise bypass.merge_promotedtreats a non-object value from either side as unusable instead of merging it. That is what lets a thread already poisoned before this change recover: its stored non-object value is dropped on the next promotion write, rather than failing every write on it. Chosen over addingpromotedtoSERVER_OWNED_STATE_CHANNELSbecause the validator keeps the channel's semantics and matches the three siblings that already have one.Surface area
Bug fix verification
backend/tests/test_gateway_services.py: 10 unusable shapes rejected with 400 on run input (string-channel,list-channel,missing-catalog-hash,missing-names,empty-catalog-hash,names-not-a-list,names-not-strings,turn-id-not-a-string,erased-overwrite-marker,sentinel-overwrite-marker); the thread-state route rejects the plain value and anOverwrite-marker record; controls prove a usable record andNonepass unchanged.backend/tests/test_thread_state_promoted.py: the reducer replaces a non-objectexistingand ignores a non-objectnew.b883deawith only the tests applied, 11 failed / 19 passed; the three marker cases fail onedc8c82(DID NOT RAISE) and pass at head.test_gateway_services.py+test_thread_state_promoted.py+test_threads_router.py: 590 passed.ruff checkandruff format --checkclean.