mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-26 13:16:24 +00:00
* feat: support client context capabilities (cherry picked from commit b7c223f9415dcd545cbe05a5f5a6e47073a42d68) [Totoro-qaq: resolved onto main. The persisted-reminder middleware hunks and their tests are dropped (delivery is redesigned in a follow-up); the RunCreateRequest description moved to app/gateway/run_models.py.] * fix: address client context review comments (cherry picked from commit bce6da6b819228eda58e887872720b6f9b5fab69) [Totoro-qaq: middleware hunks dropped with the redesigned delivery.] * feat(agents): deliver client context as a request-scoped data block The cherry-picked #3106 put context.client into the persisted first-turn reminder, with change detection and a "not provided" clear marker. Main has since moved the date reminder to a SystemMessage, made request-scoped context (the project block, #5443) request-only, and agents/AGENTS.md now requires user-influenced values to ride the HumanMessage data channel, never system text. Render runtime.context["client"] on every model call in DynamicContextMiddleware.wrap_model_call/awrap_model_call as one hidden HumanMessage right after the leading SystemMessages, where DurableContext puts its data. It is never a state update and never flagged dynamic_context_reminder, so nothing is checkpointed, summarized or replayed, and a run without a client gets no block. An unchanged client keeps the previous request as the next request's prefix. The renderer sanitizes again (direct graph callers bypass the Gateway), html-escapes values and frames them with frame_untrusted_text: the same --- BEGIN/END USER INPUT --- boundary the user's own turn and the MCP task notification get. The lead prompt treats everything outside those markers as framework data, so unframed values would rank above the user's own text; framed, context.client can say nothing the same caller could not already say in input.messages. client_context joins _BLOCKED_TAG_NAMES so a forged block in user input or a tool result is escaped, and one static, value-free sentence in the lead prompt names the block as untrusted hints, never instructions, permissions or identity. The now unused empty-context marker is removed. * fix(gateway): log dropped context.client fields without values A run request is never rejected over context.client: unknown fields, nested values and malformed entries are silently dropped, the same leniency as the other context keys. That left no signal when a custom client sent something the Gateway ignored (review on #3106). Route both the config.context.client and the body context.client paths through one helper that emits a debug line whenever sanitizing changed the value. Like the malformed client_timezone branch, the line carries no values; unlike client_timezone, client.name and preference strings are free caller-controlled text, so they are never logged, not even sanitized. A clean client, an explicit null or an absent key logs nothing. Tests also pin the config-only path (sanitized before the early return) and the end-to-end start_run admission: the sanitized client reaches config["context"] and never configurable. * fix(runtime): bound numeric client context preferences String preferences are capped at 200 characters, but integers were kept at any size. A JSON body may carry integers of up to 4300 digits, so 32 of them rendered a block of about 138,000 characters, re-sent on every model call of the run. In-process callers could also pass an int past the str() digit limit, which failed the model call, or a number subclass with its own __str__. Keep integers within JavaScript's Number.MAX_SAFE_INTEGER, the range a browser client can send exactly, and coerce numbers to plain int and float. * docs(api): describe client context as advisory request-scoped hints The cherry-picked section described a hidden reminder that no longer exists. Document what a custom client can rely on: the allow-list and its limits (strings, the integer range, and only the first 32 entries of each map), debug-level logging of dropped fields, runtime-only forwarding, and delivery as a per-request user-role block, framed like user input, rather than system text or thread history. Only top-level context.client is documented. An explicit config.context.client is sanitized for the run as well, but the run record keeps the caller's config as sent, so it is not offered as an alternative. It also answers the #3106 review: capabilities and preferences are advisory hints, not enforcement, which belongs outside the model; name is informational and never for authorization; the same object should be sent on every run to keep the cached prefix stable; subagents and embedded DeerFlowClient runs do not receive it, and server-started runs (schedules, IM channels, GitHub agents, MCP notifications, extensions unless the plugin passes one) carry none. * fix(context): declare client delivery and keep client out of configurable The request-only <client_context> block is model-visible, so DynamicContextMiddleware now declares its delivery and the sanitizer's limits in release_policy_parameters(); runs before and after this change no longer share a policy fingerprint. build_run_config copies body.config['configurable'] verbatim and configurable is persisted with checkpoints, so a client placed there is dropped instead of being stored raw. Client context stays runtime-only. Document context.client in the README and in the middleware guide. * fix(context): apply the PII redaction policy to the client block DynamicContextMiddleware adds the <client_context> message inside the outer PiiRedactionMiddleware, and the hidden message is not re-sanitized, so with pii_redaction enabled a preference such as an email address reached the model verbatim while the same address in the user turn was redacted. Pass the rendered block through the shared redact_text seam with this middleware's app_config.pii_redaction, and declare the setting in release_policy_parameters(). * fix(context): keep client hints out of stored run config --------- Co-authored-by: Blair Leng <25675774+BlairLeng@users.noreply.github.com> Co-authored-by: Totoro-qaq <279883115+Totoro-qaq@users.noreply.github.com>
180 lines
6.5 KiB
Python
180 lines
6.5 KiB
Python
"""Tests for GET /api/threads/{thread_id}/runs and /runs/page."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
from _router_auth_helpers import make_authed_test_app
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.gateway.routers import thread_runs
|
|
from deerflow.runtime import DisconnectMode, RunRecord, RunStatus
|
|
|
|
|
|
def _record(run_id: str, created_at: str) -> RunRecord:
|
|
return RunRecord(
|
|
run_id=run_id,
|
|
thread_id="thread-1",
|
|
assistant_id=None,
|
|
status=RunStatus.success,
|
|
on_disconnect=DisconnectMode.cancel,
|
|
created_at=created_at,
|
|
updated_at=created_at,
|
|
)
|
|
|
|
|
|
def _make_app(records: list[RunRecord]) -> tuple:
|
|
app = make_authed_test_app()
|
|
app.include_router(thread_runs.router)
|
|
run_manager = MagicMock()
|
|
run_manager.list_by_thread = AsyncMock(return_value=records)
|
|
app.state.run_manager = run_manager
|
|
return app, run_manager
|
|
|
|
|
|
def test_list_runs_returns_bare_array():
|
|
records = [
|
|
_record("r3", "2026-01-03T00:00:00+00:00"),
|
|
_record("r2", "2026-01-02T00:00:00+00:00"),
|
|
]
|
|
app, run_manager = _make_app(records)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-1/runs")
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert isinstance(body, list)
|
|
assert [row["run_id"] for row in body] == ["r3", "r2"]
|
|
run_manager.list_by_thread.assert_awaited_once()
|
|
kwargs = run_manager.list_by_thread.await_args.kwargs
|
|
assert kwargs.get("limit", 100) == 100
|
|
assert "before_created_at" not in kwargs
|
|
assert "before_run_id" not in kwargs
|
|
|
|
|
|
@pytest.mark.parametrize("endpoint", ["/runs", "/runs/page", "/runs/legacy-client-run"])
|
|
def test_run_reads_omit_historical_client_context_without_mutating_record(endpoint):
|
|
client_context = {"name": "old-client", "preferences": {"note": "synthetic-private-value"}}
|
|
record = _record("legacy-client-run", "2026-01-01T00:00:00+00:00")
|
|
record.kwargs = {
|
|
"config": {
|
|
"context": {"client": client_context, "model_name": "kept"},
|
|
"configurable": {"client": client_context, "thinking_enabled": False},
|
|
"metadata": {"caller_key": "kept"},
|
|
}
|
|
}
|
|
app, run_manager = _make_app([record])
|
|
run_manager.get = AsyncMock(return_value=record)
|
|
|
|
with TestClient(app) as http:
|
|
response = http.get("/api/threads/thread-1" + endpoint)
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
row = payload["data"][0] if endpoint == "/runs/page" else payload[0] if endpoint == "/runs" else payload
|
|
assert row["kwargs"]["config"] == {
|
|
"context": {"model_name": "kept"},
|
|
"configurable": {"thinking_enabled": False},
|
|
"metadata": {"caller_key": "kept"},
|
|
}
|
|
assert "synthetic-private-value" not in response.text
|
|
assert record.kwargs["config"]["context"]["client"] == client_context
|
|
assert record.kwargs["config"]["configurable"]["client"] == client_context
|
|
|
|
|
|
def test_list_runs_ignores_langgraph_sdk_limit_query():
|
|
"""SDK always sends limit=10; honoring it would shrink the existing array."""
|
|
records = [_record(f"r{i}", f"2026-01-{i:02d}T00:00:00+00:00") for i in range(1, 4)]
|
|
app, run_manager = _make_app(records)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-1/runs", params={"limit": 10, "offset": 0})
|
|
|
|
assert response.status_code == 200
|
|
assert isinstance(response.json(), list)
|
|
assert len(response.json()) == 3
|
|
assert "limit" not in run_manager.list_by_thread.await_args.kwargs
|
|
|
|
|
|
def test_runs_page_returns_envelope_and_has_more():
|
|
records = [
|
|
_record("r3", "2026-01-03T00:00:00+00:00"),
|
|
_record("r2", "2026-01-02T00:00:00+00:00"),
|
|
_record("r1", "2026-01-01T00:00:00+00:00"),
|
|
]
|
|
app, run_manager = _make_app(records)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-1/runs/page", params={"limit": 2})
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert [row["run_id"] for row in body["data"]] == ["r3", "r2"]
|
|
assert body["has_more"] is True
|
|
assert body["next_before_created_at"] == "2026-01-02T00:00:00Z"
|
|
assert body["next_before_run_id"] == "r2"
|
|
kwargs = run_manager.list_by_thread.await_args.kwargs
|
|
assert kwargs["limit"] == 3
|
|
assert kwargs["before_created_at"] is None
|
|
assert kwargs["before_run_id"] is None
|
|
|
|
|
|
def test_runs_page_passes_cursor_through():
|
|
app, run_manager = _make_app([_record("r1", "2026-01-01T00:00:00+00:00")])
|
|
with TestClient(app) as client:
|
|
response = client.get(
|
|
"/api/threads/thread-1/runs/page",
|
|
params={
|
|
"limit": 2,
|
|
"before_created_at": "2026-01-02T00:00:00+00:00",
|
|
"before_run_id": "r2",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["has_more"] is False
|
|
assert body["next_before_created_at"] is None
|
|
assert body["next_before_run_id"] is None
|
|
kwargs = run_manager.list_by_thread.await_args.kwargs
|
|
assert kwargs["before_created_at"] == "2026-01-02T00:00:00+00:00"
|
|
assert kwargs["before_run_id"] == "r2"
|
|
|
|
|
|
def test_runs_page_accepts_z_and_space_decoded_offset():
|
|
"""Unencoded +00:00 arrives as a space; Z cursors must round-trip too."""
|
|
app, run_manager = _make_app([_record("r1", "2026-01-01T00:00:00+00:00")])
|
|
with TestClient(app) as client:
|
|
spaced = client.get(
|
|
"/api/threads/thread-1/runs/page",
|
|
params={
|
|
"before_created_at": "2026-01-02T00:00:00 00:00",
|
|
"before_run_id": "r2",
|
|
},
|
|
)
|
|
zoned = client.get(
|
|
"/api/threads/thread-1/runs/page",
|
|
params={
|
|
"before_created_at": "2026-01-02T00:00:00Z",
|
|
"before_run_id": "r2",
|
|
},
|
|
)
|
|
|
|
assert spaced.status_code == 200
|
|
assert zoned.status_code == 200
|
|
assert run_manager.list_by_thread.await_args_list[0].kwargs["before_created_at"] == ("2026-01-02T00:00:00+00:00")
|
|
assert run_manager.list_by_thread.await_args_list[1].kwargs["before_created_at"] == ("2026-01-02T00:00:00+00:00")
|
|
|
|
|
|
def test_runs_page_is_not_captured_as_run_id():
|
|
app, _run_manager = _make_app([])
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-1/runs/page")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"data": [],
|
|
"has_more": False,
|
|
"next_before_created_at": None,
|
|
"next_before_run_id": None,
|
|
}
|