Skip to content

fix(gateway): reject an unusable promoted record at the input boundary - #8170

Merged
WillemJiang merged 4 commits into
bytedance:mainfrom
gdut4140:fix/promoted-boundary
Nov 14, 2026
Merged

WillemJiang merged 4 commits into
bytedance:mainfrom
gdut4140:fix/promoted-boundary

Conversation

@gdut4140

@gdut4140 gdut4140 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8157

Why

promoted is a checkpointed reducer channel that both gateway input routes accept from a caller, and merge_promoted reads 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 as todos (#7803) and skill_context (#8091). The thread-state route is worse: it wraps reducer channels in Overwrite, 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:

normalize_input({"promoted": "oops"})                        -> passes through (no 400)
strip_server_owned_state_metadata({"promoted": "oops"})      -> passes through (no 400)
merge_promoted(None, "oops")                                 -> AttributeError: 'str' object has no attribute 'get'
merge_promoted(None, {"names": ["t"]})                       -> KeyError: 'catalog_hash'
merge_promoted({"catalog_hash": "abc", "names": ["t"]}, {"catalog_hash": "abc"})
                                                             -> KeyError: 'names'
merge_promoted({"catalog_hash": "abc", "names": ["t"]}, {"catalog_hash": "abc", "names": "t"})
                                                             -> TypeError: can only concatenate list (not "str") to list
merge_promoted("oops", {"catalog_hash": "abc", "names": ["s"]})   -> AttributeError (a poisoned checkpoint)

A shape check alone is not enough: LangGraph recognises a JSON-erased Overwrite as {"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 real StateGraph with an initialized promoted channel: the value 'oops' lands in state unchecked.

What changed

  • _normalize_external_promoted in app/gateway/services.py requires an object with a non-empty string catalog_hash, a list of strings names, and an optional string turn_id; anything else is a 400 naming the offending field. It mirrors _normalize_external_skill_context.
  • _reject_overwrite_marker refuses both Overwrite dict spellings (the sentinel {"__overwrite__": ...} form and the erased {"type": "__overwrite__", "value": ...} form). _normalize_external_goal now calls the same helper instead of its inline check, so the two validators cannot drift.
  • Both routes apply the validator: normalize_input (input.promoted) and strip_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_promoted treats 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 adding promoted to SERVER_OWNED_STATE_CHANNELS because the validator keeps the channel's semantics and matches the three siblings that already have one.

Surface area

  • Backend API — run input and the thread-state update route answer 400 instead of 500; usable records are unchanged
  • Agents / LangGraph — deferred-tool promotion reducer
  • Frontend UI · [ ] Sandbox · [ ] Skills · [ ] Dependencies
  • Default behavior change — only previously-crashing input is refused
  • Docs / tests / CI — CHANGELOG entry

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 an Overwrite-marker record; controls prove a usable record and None pass unchanged.
  • backend/tests/test_thread_state_promoted.py: the reducer replaces a non-object existing and ignores a non-object new.
  • Red/green: on b883dea with only the tests applied, 11 failed / 19 passed; the three marker cases fail on edc8c82 (DID NOT RAISE) and pass at head. test_gateway_services.py + test_thread_state_promoted.py + test_threads_router.py: 590 passed.
  • ruff check and ruff format --check clean.

@github-actions github-actions Bot added area:agents Agents, subagents, graph wiring, prompts, langgraph.json area:backend Gateway / runtime / core backend under backend/ area:docs Documentation and Markdown only needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/M PR changes 100-300 lines labels Nov 12, 2026
@ShenAC-SAC
ShenAC-SAC requested a balanced review from Copilot November 12, 2026 06:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_promoted tolerate 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

Comment thread backend/app/gateway/services.py Resolved
@gdut4140
gdut4140 force-pushed the fix/promoted-boundary branch from 25aed94 to 62bcdea Compare November 12, 2026 07:31
@gdut4140

Copy link
Copy Markdown
Contributor Author

Rebased onto main (2dc68b2) for the CHANGELOG conflict after #8132/#8144/#8151 merged; both entries and link definitions kept. Same patches as 25aed94 (the Copilot-review fix), head is now 62bcdea. Re-verified after the rebase: test_gateway_services + test_thread_state_promoted + test_threads_router 590 passed, ruff check/format clean.

@willem-bd willem-bd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread CHANGELOG.md Resolved
Comment thread backend/app/gateway/services.py Resolved
@willem-bd

willem-bd commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

@gdut4140, please remove the change to CHANGLOG.md to avoid the conflict.

@gdut4140
gdut4140 force-pushed the fix/promoted-boundary branch from 62bcdea to 272d89f Compare November 13, 2026 07:42
@github-actions github-actions Bot removed the area:docs Documentation and Markdown only label Nov 13, 2026
@gdut4140

Copy link
Copy Markdown
Contributor Author

Removed the CHANGELOG.md change as requested — that was the only file that conflicted.

Rebased onto main (161c3503d); head is now 272d89f. The diff is now four files (+166/−2), same patches as 62bcdea minus the changelog entry:

  • backend/app/gateway/services.py
  • backend/packages/harness/deerflow/agents/thread_state.py
  • backend/tests/test_gateway_services.py
  • backend/tests/test_thread_state_promoted.py

Re-verified after the rebase: test_gateway_services.py + test_thread_state_promoted.py → 404 passed.

Leaving the changelog note out entirely; say the word if you would rather have a one-line entry added back once the order settles.

@WillemJiang WillemJiang added this to the 2.3.0 milestone Nov 13, 2026
@CLAassistant

CLAassistant commented Nov 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@gdut4140
gdut4140 force-pushed the fix/promoted-boundary branch from 24b9884 to 75a7fb8 Compare November 14, 2026 04:45
@WillemJiang
WillemJiang merged commit 3e24894 into bytedance:main Nov 14, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agents Agents, subagents, graph wiring, prompts, langgraph.json area:backend Gateway / runtime / core backend under backend/ needs-validation Touches front/back contract surface; needs real-path validation risk:high High risk: backend API, agents, sandbox, auth, deps, CI size/M PR changes 100-300 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway: caller-supplied promoted state crashes the run with an unhandled 500 (and poisons the thread via the thread-state update route)

5 participants