代码提交
This commit is contained in:
1 parent
4ac7bc21cd
commit
17cf849361
110 files changed
+17007
-518
No files matched your search
@@ -0,0 +1,433 @@
|
||||
"""Concurrency regressions from docs/WORKFLOW_STUDIO_BACKEND_REMEDIATION_ZH.md.
|
||||
|
||||
Covers the P1-01/02/03 rework with the races the old two-step implementations
|
||||
lost: atomic human-input resume, draft optimistic locking, publish
|
||||
version-number allocation and event ``seq`` allocation — plus the sink's
|
||||
drop-not-publish rule and the shared JSON Schema validator.
|
||||
|
||||
The SQL paths run against a real ``aiosqlite`` database (not mocks) because the
|
||||
conditional-UPDATE semantics only exist in SQL. ``connect_args={"timeout": 30}``
|
||||
gives busy-waiting writers room so the interleavings exercise CAS rather than
|
||||
lock errors.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.workflow_events import (
|
||||
MemoryWorkflowEventStore,
|
||||
SqlWorkflowEventStore,
|
||||
WorkflowRunEventRow,
|
||||
)
|
||||
from deerflow.persistence.workflow_runs import MemoryWorkflowRunStore, SqlWorkflowRunStore
|
||||
from deerflow.persistence.workflows import (
|
||||
MemoryWorkflowStore,
|
||||
WorkflowDraftConflictError,
|
||||
WorkflowRepository,
|
||||
)
|
||||
from deerflow.workflows.runtime.sink import PersistingWorkflowEventSink
|
||||
from deerflow.workflows.schema_validation import schema_issues
|
||||
|
||||
|
||||
def _graph(marker: str) -> dict[str, Any]:
|
||||
return {
|
||||
"schemaVersion": "1.0",
|
||||
"id": f"wf_{marker}",
|
||||
"name": marker,
|
||||
"nodes": [{"id": "start_1", "type": "start", "config": {}}, {"id": "end_1", "type": "output", "config": {"mapping": {}}}],
|
||||
"edges": [{"id": "e1", "source": "start_1", "target": "end_1"}],
|
||||
}
|
||||
|
||||
|
||||
async def _paused_run(store: Any, *, token: str = "tok-resume") -> str:
|
||||
"""Drive a fresh run to ``awaiting_input`` the way the executor does."""
|
||||
run, _created = await store.create_run(
|
||||
{"workflow_id": "wf-1", "workflow_version_id": "v-1", "owner_id": "user-a", "context": {"depth": 0}}
|
||||
)
|
||||
run_id = run["id"]
|
||||
claimed = await store.claim_run(run_id, lease_owner="worker-1", lease_until=datetime.now(UTC) + timedelta(seconds=60))
|
||||
assert claimed is not None and claimed["status"] == "running"
|
||||
paused = await store.set_awaiting_input(
|
||||
run_id,
|
||||
lease_owner="worker-1",
|
||||
pending_input={"nodeId": "ask", "prompt": "确认"},
|
||||
resume_token=token,
|
||||
context={"loop_state": {"step": 2}, "resume_payload": None},
|
||||
)
|
||||
assert paused is not None and paused["status"] == "awaiting_input"
|
||||
return run_id
|
||||
|
||||
|
||||
# ── SQL fixture ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def session_factory(tmp_path):
|
||||
engine = create_async_engine(
|
||||
f"sqlite+aiosqlite:///{tmp_path / 'workflow-concurrency.db'}",
|
||||
connect_args={"timeout": 30},
|
||||
)
|
||||
async with engine.begin() as conn:
|
||||
import deerflow.persistence.workflow_events.model # noqa: F401
|
||||
import deerflow.persistence.workflow_runs.model # noqa: F401
|
||||
import deerflow.persistence.workflows.model # noqa: F401
|
||||
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
yield async_sessionmaker(engine, expire_on_commit=False)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
# ── P1-01 atomic resume ─────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_resume_writes_payload_and_context_in_one_step() -> None:
|
||||
store = MemoryWorkflowRunStore()
|
||||
run_id = await _paused_run(store)
|
||||
|
||||
resumed = await store.resume_with_payload(
|
||||
run_id,
|
||||
"tok-resume",
|
||||
{"nodeId": "ask", "action": "submit", "values": {"comment": "同意"}},
|
||||
)
|
||||
assert resumed is not None and resumed["status"] == "queued"
|
||||
# The checkpoint survived the resume; only the payload slot was written.
|
||||
assert resumed["context"]["loop_state"] == {"step": 2}
|
||||
assert resumed["context"]["resume_payload"]["values"] == {"comment": "同意"}
|
||||
assert resumed["resume_token"] is None and resumed["pending_input"] is None
|
||||
|
||||
# One-shot token: a replay of the same token can never resume again.
|
||||
assert await store.resume_with_payload(run_id, "tok-resume", {"values": {"comment": "再次"}}) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_concurrent_resume_single_winner() -> None:
|
||||
store = MemoryWorkflowRunStore()
|
||||
run_id = await _paused_run(store)
|
||||
|
||||
results = await asyncio.gather(
|
||||
store.resume_with_payload(run_id, "tok-resume", {"values": {"comment": "A"}}),
|
||||
store.resume_with_payload(run_id, "tok-resume", {"values": {"comment": "B"}}),
|
||||
)
|
||||
winners = [r for r in results if r is not None]
|
||||
assert len(winners) == 1, results
|
||||
final = await store.get_run(run_id)
|
||||
assert final is not None and final["status"] == "queued"
|
||||
assert final["context"]["resume_payload"]["values"]["comment"] in ("A", "B")
|
||||
assert final["context"]["resume_payload"] == winners[0]["context"]["resume_payload"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_resume_rejects_wrong_token() -> None:
|
||||
store = MemoryWorkflowRunStore()
|
||||
run_id = await _paused_run(store)
|
||||
assert await store.resume_with_payload(run_id, "not-the-token", {"values": {}}) is None
|
||||
still = await store.get_run(run_id)
|
||||
assert still is not None and still["status"] == "awaiting_input" and still["resume_token"] == "tok-resume"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_concurrent_resume_single_winner(session_factory) -> None:
|
||||
store = SqlWorkflowRunStore(session_factory)
|
||||
run_id = await _paused_run(store)
|
||||
|
||||
results = await asyncio.gather(
|
||||
store.resume_with_payload(run_id, "tok-resume", {"nodeId": "ask", "action": "submit", "values": {"comment": "A"}}),
|
||||
store.resume_with_payload(run_id, "tok-resume", {"nodeId": "ask", "action": "submit", "values": {"comment": "B"}}),
|
||||
)
|
||||
winners = [r for r in results if r is not None]
|
||||
assert len(winners) == 1, results
|
||||
final = await store.get_run(run_id)
|
||||
assert final is not None and final["status"] == "queued"
|
||||
assert final["resume_token"] is None and final["pending_input"] is None
|
||||
assert final["context"]["loop_state"] == {"step": 2}
|
||||
assert final["context"]["resume_payload"] == winners[0]["context"]["resume_payload"]
|
||||
|
||||
|
||||
# ── P1-02 draft optimistic locking + publish counter ───────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_stale_draft_save_is_rejected_without_overwrite(session_factory) -> None:
|
||||
store = WorkflowRepository(session_factory)
|
||||
created = await store.create_definition({"name": "draft", "owner_id": "user-a", "draft_graph": _graph("v1")})
|
||||
wf_id = created["id"]
|
||||
|
||||
saved = await store.save_draft(wf_id, expected_revision=0, graph=_graph("v2"))
|
||||
assert saved["draft_revision"] == 1
|
||||
|
||||
# The editor that still holds revision 0 must lose, not overwrite.
|
||||
with pytest.raises(WorkflowDraftConflictError) as exc:
|
||||
await store.save_draft(wf_id, expected_revision=0, graph=_graph("stale"))
|
||||
assert exc.value.current_revision == 1
|
||||
fresh = await store.get_definition(wf_id, include_draft=True)
|
||||
assert fresh is not None and fresh["draft_graph"]["name"] == "v2"
|
||||
assert fresh["draft_revision"] == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_concurrent_draft_saves_yield_exactly_one_winner(session_factory) -> None:
|
||||
store = WorkflowRepository(session_factory)
|
||||
created = await store.create_definition({"name": "race", "owner_id": "user-a", "draft_graph": _graph("base")})
|
||||
wf_id = created["id"]
|
||||
|
||||
results = await asyncio.gather(
|
||||
store.save_draft(wf_id, expected_revision=0, graph=_graph("editor-a")),
|
||||
store.save_draft(wf_id, expected_revision=0, graph=_graph("editor-b")),
|
||||
return_exceptions=True,
|
||||
)
|
||||
winners = [r for r in results if not isinstance(r, BaseException)]
|
||||
conflicts = [r for r in results if isinstance(r, WorkflowDraftConflictError)]
|
||||
assert len(winners) == 1 and len(conflicts) == 1, results
|
||||
fresh = await store.get_definition(wf_id, include_draft=True)
|
||||
assert fresh is not None and fresh["draft_revision"] == 1
|
||||
assert fresh["draft_graph"]["name"] == winners[0]["draft_graph"]["name"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_concurrent_publishes_get_adjacent_unique_version_numbers(session_factory) -> None:
|
||||
from deerflow.persistence.workflows.model import WorkflowDefinitionRow
|
||||
|
||||
store = WorkflowRepository(session_factory)
|
||||
created = await store.create_definition({"name": "publish-race", "owner_id": "user-a", "draft_graph": _graph("p")})
|
||||
wf_id = created["id"]
|
||||
|
||||
async def _publish(n: int) -> dict[str, Any]:
|
||||
return await store.publish_version(
|
||||
wf_id,
|
||||
graph=_graph(f"p{n}"),
|
||||
graph_hash=f"hash-{n}",
|
||||
published_by="user-a",
|
||||
change_note=f"v{n}",
|
||||
)
|
||||
|
||||
publishers = 5
|
||||
versions = await asyncio.gather(*[_publish(n) for n in range(publishers)])
|
||||
numbers = sorted(v["version_number"] for v in versions)
|
||||
assert numbers == [1, 2, 3, 4, 5]
|
||||
listed = await store.list_versions(wf_id)
|
||||
assert sorted(v["version_number"] for v in listed) == [1, 2, 3, 4, 5]
|
||||
# The counter must sit past every allocated number.
|
||||
async with session_factory() as session:
|
||||
row = await session.get(WorkflowDefinitionRow, wf_id)
|
||||
assert row is not None and row.next_version_number == 6
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_publish_after_legacy_versions_never_reuses_a_number(session_factory) -> None:
|
||||
"""A legacy DB backfilled the counter to 1 while versions already existed."""
|
||||
from sqlalchemy import update
|
||||
|
||||
from deerflow.persistence.workflows.model import WorkflowDefinitionRow
|
||||
|
||||
store = WorkflowRepository(session_factory)
|
||||
created = await store.create_definition({"name": "legacy", "owner_id": "user-a", "draft_graph": _graph("l")})
|
||||
wf_id = created["id"]
|
||||
for n in (1, 2, 3):
|
||||
await store.publish_version(wf_id, graph=_graph(f"l{n}"), graph_hash=f"h{n}", published_by="user-a")
|
||||
async with session_factory() as session:
|
||||
await session.execute(
|
||||
update(WorkflowDefinitionRow).where(WorkflowDefinitionRow.id == wf_id).values(next_version_number=1)
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
version = await store.publish_version(wf_id, graph=_graph("l4"), graph_hash="h4", published_by="user-a")
|
||||
assert version["version_number"] == 4
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_publish_numbers_survive_a_stale_counter() -> None:
|
||||
store = MemoryWorkflowStore()
|
||||
created = await store.create_definition({"name": "mem", "owner_id": "user-a", "draft_graph": _graph("m")})
|
||||
wf_id = created["id"]
|
||||
for n in (1, 2):
|
||||
await store.publish_version(wf_id, graph=_graph(f"m{n}"), graph_hash=f"h{n}", published_by="user-a")
|
||||
# Simulate the legacy backfill: counter behind the existing versions.
|
||||
store._definitions[wf_id]["next_version_number"] = 1
|
||||
version = await store.publish_version(wf_id, graph=_graph("m3"), graph_hash="h3", published_by="user-a")
|
||||
assert version["version_number"] == 3
|
||||
|
||||
|
||||
# ── P1-03 event seq counter ─────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_concurrent_appends_get_unique_monotonic_seqs(session_factory) -> None:
|
||||
runs = SqlWorkflowRunStore(session_factory)
|
||||
events = SqlWorkflowEventStore(session_factory)
|
||||
run, _created = await runs.create_run(
|
||||
{"workflow_id": "wf-1", "workflow_version_id": "v-1", "owner_id": "user-a"}
|
||||
)
|
||||
|
||||
appended = await asyncio.gather(
|
||||
*[
|
||||
events.append(run_id=run["id"], workflow_id="wf-1", version_id="v-1", event_type="node.progress", payload={"i": i})
|
||||
for i in range(10)
|
||||
]
|
||||
)
|
||||
seqs = sorted(int(row["seq"]) for row in appended)
|
||||
assert seqs == list(range(1, 11))
|
||||
|
||||
stored = await events.list_after(run["id"])
|
||||
assert [e["seq"] for e in stored] == list(range(1, 11))
|
||||
assert await events.max_seq(run["id"]) == 10
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_seq_counter_self_heals_legacy_rows(session_factory) -> None:
|
||||
"""Legacy DBs got the counter column backfilled to 0 with events present."""
|
||||
runs = SqlWorkflowRunStore(session_factory)
|
||||
events = SqlWorkflowEventStore(session_factory)
|
||||
run, _created = await runs.create_run(
|
||||
{"workflow_id": "wf-1", "workflow_version_id": "v-1", "owner_id": "user-a"}
|
||||
)
|
||||
async with session_factory() as session:
|
||||
for seq in (1, 2, 3):
|
||||
session.add(
|
||||
WorkflowRunEventRow(
|
||||
run_id=run["id"],
|
||||
workflow_id="wf-1",
|
||||
version_id="v-1",
|
||||
seq=seq,
|
||||
event_type="node.started",
|
||||
payload_json="{}",
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
row = await events.append(run_id=run["id"], workflow_id="wf-1", version_id="v-1", event_type="node.completed")
|
||||
assert row["seq"] == 4
|
||||
assert await events.max_seq(run["id"]) == 4
|
||||
# The healed counter keeps allocating past the recovered maximum.
|
||||
nxt = await events.append(run_id=run["id"], workflow_id="wf-1", version_id="v-1", event_type="run.completed")
|
||||
assert nxt["seq"] == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_append_without_run_row_falls_back_to_max_seq(session_factory) -> None:
|
||||
"""Event store used standalone (no run row) still allocates from MAX(seq)+1."""
|
||||
events = SqlWorkflowEventStore(session_factory)
|
||||
orphan = f"run-{uuid.uuid4().hex[:8]}"
|
||||
first = await events.append(run_id=orphan, workflow_id="wf-1", version_id="v-1", event_type="run.created")
|
||||
second = await events.append(run_id=orphan, workflow_id="wf-1", version_id="v-1", event_type="run.started")
|
||||
assert (first["seq"], second["seq"]) == (1, 2)
|
||||
|
||||
|
||||
# ── sink: persist-then-publish, never ghost events ──────────────────────
|
||||
|
||||
|
||||
class _ExplodingEventStore(MemoryWorkflowEventStore):
|
||||
async def append(self, **_kwargs: Any) -> dict[str, Any]:
|
||||
raise RuntimeError("event log unavailable")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sink_drops_unpersisted_events_instead_of_publishing_ghosts() -> None:
|
||||
published: list[dict[str, Any]] = []
|
||||
|
||||
async def _publish(frame: dict[str, Any]) -> None:
|
||||
published.append(frame)
|
||||
|
||||
sink = PersistingWorkflowEventSink(
|
||||
_ExplodingEventStore(),
|
||||
run_id="run-1",
|
||||
workflow_id="wf-1",
|
||||
version_id="v-1",
|
||||
publish=_publish,
|
||||
)
|
||||
envelope = await sink.emit("node.started", data={"chunk": "hi"}, node_id="n1")
|
||||
|
||||
assert envelope.seq == 0 # no fabricated seq
|
||||
assert envelope.data["eventPersistenceDegraded"] is True
|
||||
assert envelope.data["chunk"] == "hi"
|
||||
assert published == [] # nothing went live: no ghost frame on the wire
|
||||
assert sink.degraded_count == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sink_publishes_only_after_successful_persist() -> None:
|
||||
published: list[dict[str, Any]] = []
|
||||
|
||||
async def _publish(frame: dict[str, Any]) -> None:
|
||||
published.append(frame)
|
||||
|
||||
store = MemoryWorkflowEventStore()
|
||||
sink = PersistingWorkflowEventSink(store, run_id="run-1", workflow_id="wf-1", version_id="v-1", publish=_publish)
|
||||
envelope = await sink.emit("run.started", data={"attempt": 1})
|
||||
|
||||
assert envelope.seq == 1
|
||||
assert len(published) == 1
|
||||
assert published[0]["seq"] == 1 and published[0]["data"] == {"attempt": 1}
|
||||
assert "eventPersistenceDegraded" not in published[0]["data"]
|
||||
assert [e["seq"] for e in await store.list_after("run-1")] == [1]
|
||||
|
||||
|
||||
# ── P2 shared JSON Schema validator ─────────────────────────────────────
|
||||
|
||||
|
||||
def test_schema_issues_skips_empty_schemas() -> None:
|
||||
assert schema_issues({"anything": 1}, {}) == []
|
||||
assert schema_issues({"anything": 1}, None) == []
|
||||
assert schema_issues({"topic": "ok"}, {"type": "object", "properties": {}}) == []
|
||||
|
||||
|
||||
def test_schema_issues_reports_nesting_paths_in_chinese() -> None:
|
||||
schema = {
|
||||
"type": "object",
|
||||
"required": ["topic", "options"],
|
||||
"properties": {
|
||||
"topic": {"type": "string"},
|
||||
"mode": {"enum": ["fast", "slow"]},
|
||||
"options": {
|
||||
"type": "array",
|
||||
"items": {"type": "object", "required": ["name"], "properties": {"name": {"type": "string"}}},
|
||||
},
|
||||
"meta": {"type": "object", "additionalProperties": False},
|
||||
},
|
||||
}
|
||||
issues = schema_issues(
|
||||
{
|
||||
"topic": 123,
|
||||
"mode": "turbo",
|
||||
"options": [{"name": "ok"}, {"title": "x"}],
|
||||
"meta": {"extra": "b"},
|
||||
},
|
||||
schema,
|
||||
)
|
||||
by_path = {tuple(i["path"]): i for i in issues}
|
||||
|
||||
assert "类型不正确" in by_path[("topic",)]["message"]
|
||||
assert "不在允许范围" in by_path[("mode",)]["message"]
|
||||
# Array element violations keep their index in the path.
|
||||
assert ("options", 1) in by_path and "缺少必填字段:name" in by_path[("options", 1)]["message"]
|
||||
# additionalProperties errors point at the offending object/field.
|
||||
meta_issues = [i for i in issues if i["path"] and i["path"][0] == "meta"]
|
||||
assert meta_issues and any("额外" in i["message"] for i in meta_issues)
|
||||
|
||||
|
||||
def test_schema_issues_reports_missing_top_level_keys() -> None:
|
||||
issues = schema_issues({}, {"type": "object", "required": ["topic"]})
|
||||
assert issues == [{"path": [], "message": "输入 缺少必填字段:topic"}]
|
||||
|
||||
|
||||
def test_schema_issues_tolerates_malformed_schemas() -> None:
|
||||
# A non-string pattern only explodes during validation, not construction.
|
||||
issues = schema_issues("abc", {"pattern": 123})
|
||||
assert issues == [{"path": [], "message": "schema 定义本身无效,无法用于校验"}]
|
||||
|
||||
|
||||
def test_schema_issues_caps_reported_errors() -> None:
|
||||
schema = {"type": "object", "properties": {f"f{i}": {"type": "string"} for i in range(30)}}
|
||||
value = {f"f{i}": i for i in range(30)}
|
||||
issues = schema_issues(value, schema)
|
||||
assert len(issues) == 21 # 20 concrete issues + the ellipsis marker
|
||||
assert issues[-1]["message"].startswith("……其余问题已省略")
|
||||
@@ -0,0 +1,240 @@
|
||||
"""Phase-1 workflow definition store + validator + Coze compat tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.gateway.routers import workflow_resources, workflows, workflows_coze_compat
|
||||
from deerflow.persistence.workflows import (
|
||||
MemoryWorkflowStore,
|
||||
WorkflowDraftConflictError,
|
||||
make_workflow_store,
|
||||
)
|
||||
from deerflow.workflows.samples import load_sample_graph
|
||||
from deerflow.workflows.validator import validate_workflow_graph
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_store_create_draft_publish() -> None:
|
||||
store = make_workflow_store(None)
|
||||
assert isinstance(store, MemoryWorkflowStore)
|
||||
|
||||
created = await store.create_definition(
|
||||
{
|
||||
"name": "专题报告",
|
||||
"owner_id": "user_a",
|
||||
"draft_graph": load_sample_graph("start_output").model_dump(by_alias=True),
|
||||
}
|
||||
)
|
||||
assert created["draft_revision"] == 0
|
||||
assert created["status"] == "draft"
|
||||
wf_id = created["id"]
|
||||
|
||||
saved = await store.save_draft(
|
||||
wf_id,
|
||||
expected_revision=0,
|
||||
graph=load_sample_graph("start_output").model_dump(by_alias=True),
|
||||
)
|
||||
assert saved["draft_revision"] == 1
|
||||
|
||||
with pytest.raises(WorkflowDraftConflictError) as exc:
|
||||
await store.save_draft(
|
||||
wf_id,
|
||||
expected_revision=0,
|
||||
graph=load_sample_graph("start_output").model_dump(by_alias=True),
|
||||
)
|
||||
assert exc.value.current_revision == 1
|
||||
|
||||
graph = load_sample_graph("start_output")
|
||||
version = await store.publish_version(
|
||||
wf_id,
|
||||
graph=graph.model_dump(by_alias=True),
|
||||
graph_hash=graph.graph_hash(),
|
||||
published_by="user_a",
|
||||
change_note="v1",
|
||||
)
|
||||
assert version["version_number"] == 1
|
||||
assert version["graph_hash"] == graph.graph_hash()
|
||||
|
||||
definition = await store.get_definition(wf_id)
|
||||
assert definition is not None
|
||||
assert definition["status"] == "active"
|
||||
|
||||
versions = await store.list_versions(wf_id)
|
||||
assert len(versions) == 1
|
||||
loaded = await store.get_version(version["id"])
|
||||
assert loaded is not None
|
||||
assert loaded["graph"]["id"] == "wf_start_output"
|
||||
|
||||
|
||||
def test_validate_start_output_and_report_samples() -> None:
|
||||
start_output = load_sample_graph("start_output")
|
||||
assert validate_workflow_graph(start_output) == []
|
||||
|
||||
report = load_sample_graph("report_generation")
|
||||
issues = validate_workflow_graph(report)
|
||||
assert issues == [], [i.model_dump(by_alias=True) for i in issues]
|
||||
|
||||
|
||||
def test_validate_rejects_missing_output_and_cycle() -> None:
|
||||
issues = validate_workflow_graph(
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf_bad",
|
||||
"nodes": [
|
||||
{"id": "start_1", "type": "start", "config": {}},
|
||||
{"id": "a", "type": "transform", "config": {"operations": []}},
|
||||
{"id": "b", "type": "transform", "config": {"operations": []}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start_1", "target": "a"},
|
||||
{"id": "e2", "source": "a", "target": "b"},
|
||||
{"id": "e3", "source": "b", "target": "a"},
|
||||
],
|
||||
}
|
||||
)
|
||||
codes = {i.code for i in issues}
|
||||
assert "WORKFLOW_SCHEMA_INVALID" in codes # missing output
|
||||
assert "WORKFLOW_GRAPH_CYCLE" in codes
|
||||
|
||||
|
||||
def test_workflow_http_api_and_coze_compat(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
|
||||
class _Cfg:
|
||||
workflows = WorkflowConfig()
|
||||
|
||||
monkeypatch.setattr(workflows, "get_app_config", lambda: _Cfg())
|
||||
monkeypatch.setattr(workflow_resources, "get_app_config", lambda: _Cfg())
|
||||
monkeypatch.setattr(workflows_coze_compat, "get_app_config", lambda: _Cfg())
|
||||
|
||||
app = FastAPI()
|
||||
store = MemoryWorkflowStore()
|
||||
app.state.workflow_store = store
|
||||
app.include_router(workflow_resources.router)
|
||||
app.include_router(workflows.router)
|
||||
app.include_router(workflows_coze_compat.router)
|
||||
app.include_router(workflows_coze_compat.chrome_router)
|
||||
|
||||
async def _fake_user(_request=None):
|
||||
return "user_a"
|
||||
|
||||
monkeypatch.setattr(workflows, "get_current_user", _fake_user)
|
||||
monkeypatch.setattr(workflows, "get_optional_user_from_request", lambda _r: None)
|
||||
monkeypatch.setattr(workflow_resources, "get_current_user", _fake_user)
|
||||
monkeypatch.setattr(workflows_coze_compat, "get_current_user", _fake_user)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
created = client.post("/api/workflows", json={"name": "demo"}).json()
|
||||
wf_id = created["id"]
|
||||
assert created["draft_revision"] == 0
|
||||
|
||||
graph = load_sample_graph("start_output").model_dump(by_alias=True)
|
||||
saved = client.put(
|
||||
f"/api/workflows/{wf_id}/draft",
|
||||
json={"expectedRevision": 0, "graph": graph},
|
||||
).json()
|
||||
assert saved["draft_revision"] == 1
|
||||
|
||||
conflict = client.put(
|
||||
f"/api/workflows/{wf_id}/draft",
|
||||
json={"expectedRevision": 0, "graph": graph},
|
||||
)
|
||||
assert conflict.status_code == 409
|
||||
|
||||
validated = client.post(f"/api/workflows/{wf_id}/validate", json={}).json()
|
||||
assert validated["ok"] is True
|
||||
|
||||
published = client.post(
|
||||
f"/api/workflows/{wf_id}/publish",
|
||||
json={"changeNote": "first", "expectedRevision": 1},
|
||||
).json()
|
||||
assert published["version_number"] == 1
|
||||
|
||||
types = client.get("/api/workflows/node-types").json()
|
||||
assert len(types["nodeTypes"]) == 13
|
||||
|
||||
canvas = client.post("/api/workflow_api/canvas", json={"workflowId": wf_id}).json()
|
||||
assert canvas["code"] == 0
|
||||
workflow_brief = canvas["data"]["workflow"]
|
||||
assert workflow_brief["workflow_id"] == wf_id
|
||||
assert workflow_brief["is_published"] is True
|
||||
# Never opened in the Coze editor → built-in start→end canvas fallback.
|
||||
assert json.loads(workflow_brief["schema_json"])["nodes"]
|
||||
|
||||
coze_canvas = json.dumps(
|
||||
{
|
||||
"nodes": [
|
||||
{"id": "100001", "type": "1", "meta": {"position": {"x": 0, "y": 0}}},
|
||||
{"id": "900001", "type": "2", "meta": {"position": {"x": 400, "y": 0}}},
|
||||
],
|
||||
"edges": [{"sourceNodeID": "100001", "targetNodeID": "900001"}],
|
||||
}
|
||||
)
|
||||
saved = client.post(
|
||||
"/api/workflow_api/save",
|
||||
json={"workflow_id": wf_id, "schema": coze_canvas},
|
||||
)
|
||||
assert saved.status_code == 200, saved.text
|
||||
assert saved.json()["code"] == 0
|
||||
|
||||
reloaded = client.post("/api/workflow_api/canvas", json={"workflowId": wf_id}).json()
|
||||
# Storage canonicalises to compact JSON; compare as documents.
|
||||
assert json.loads(reloaded["data"]["workflow"]["schema_json"]) == json.loads(coze_canvas)
|
||||
# The Coze save must round-trip the canvas without touching the graph.
|
||||
definition = client.get(f"/api/workflows/{wf_id}").json()
|
||||
assert definition["draft_graph"]["nodes"], "execution graph must pass through unchanged"
|
||||
|
||||
templates = client.post("/api/workflow_api/node_template_list").json()
|
||||
assert templates["code"] == 0
|
||||
template_list = templates["data"]["template_list"]
|
||||
assert len(template_list) == 13
|
||||
# Coze playground 形状:node_type 数字串 + 分类表
|
||||
assert all("node_type" in item and "name" in item for item in template_list)
|
||||
assert templates["data"]["cate_list"]
|
||||
|
||||
account = client.post("/api/passport/account/info/v2/").json()
|
||||
assert account["code"] == 0
|
||||
assert account["data"]["user_id"] == "user_a"
|
||||
assert account["data"]["login_status"] == "logined"
|
||||
|
||||
playground_spaces = client.post("/api/playground_api/space/list").json()
|
||||
assert playground_spaces["code"] == 0
|
||||
assert playground_spaces["data"]["has_personal_space"] is True
|
||||
assert playground_spaces["data"]["bot_space_list"][0]["id"] == "1"
|
||||
|
||||
listed = client.post("/api/space/list").json()
|
||||
assert listed["code"] == 0
|
||||
assert listed["bot_space_list"][0]["id"] == "1"
|
||||
assert listed["data"]["bot_space_list"][0]["name"] == "默认空间"
|
||||
assert listed["data"]["bot_space_list"][0]["space_type"] == 1
|
||||
|
||||
saved_space = client.post("/api/playground_api/space/save").json()
|
||||
assert saved_space["code"] == 0
|
||||
assert saved_space["data"]["id"] == "1"
|
||||
|
||||
models = client.post("/api/bot/get_type_list").json()
|
||||
assert models["code"] == 0
|
||||
assert models["data"]["model_list"] == []
|
||||
|
||||
copied = client.post(f"/api/workflows/{wf_id}/copy")
|
||||
assert copied.status_code == 200, copied.text
|
||||
assert copied.json()["id"] != wf_id
|
||||
assert copied.json()["name"].endswith("副本")
|
||||
|
||||
|
||||
def test_coze_studio_paths_are_csrf_exempt() -> None:
|
||||
from app.gateway.csrf_middleware import is_coze_studio_compat_path
|
||||
|
||||
assert is_coze_studio_compat_path("/api/playground_api/space/list")
|
||||
assert is_coze_studio_compat_path("/api/passport/account/info/v2/")
|
||||
assert is_coze_studio_compat_path("/api/workflow_api/canvas")
|
||||
assert is_coze_studio_compat_path("/api/space/list")
|
||||
assert is_coze_studio_compat_path("/api/bot/get_type_list")
|
||||
assert not is_coze_studio_compat_path("/api/workflows")
|
||||
assert not is_coze_studio_compat_path("/api/v1/auth/me")
|
||||
@@ -0,0 +1,688 @@
|
||||
"""Phase 2/3 tests: expressions, scheduler semantics, policies."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.persistence.workflow_events import MemoryWorkflowEventStore
|
||||
from deerflow.persistence.workflow_runs import MemoryWorkflowRunStore
|
||||
from deerflow.workflows.errors import WorkflowError
|
||||
from deerflow.workflows.expressions import evaluate_condition, render_template, render_value
|
||||
from deerflow.workflows.nodes import build_default_registry
|
||||
from deerflow.workflows.runtime import (
|
||||
CancelToken,
|
||||
PersistingWorkflowEventSink,
|
||||
RunContext,
|
||||
WorkflowEngine,
|
||||
WorkflowPaused,
|
||||
WorkflowRuntimeDeps,
|
||||
)
|
||||
from deerflow.workflows.schemas import WorkflowGraph
|
||||
from deerflow.workflows.security.http_policy import enforce_http_target
|
||||
from deerflow.workflows.security.sql_policy import assert_read_only, assert_tables_allowed, enforce_limit
|
||||
from deerflow.workflows.validator import validate_workflow_graph
|
||||
|
||||
|
||||
def _graph(nodes: list[dict[str, Any]], edges: list[dict[str, Any]], **kwargs: Any) -> WorkflowGraph:
|
||||
return WorkflowGraph.model_validate(
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-test",
|
||||
"name": "test",
|
||||
"nodes": nodes,
|
||||
"edges": edges,
|
||||
**kwargs,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def _run(
|
||||
graph: WorkflowGraph,
|
||||
inputs: dict[str, Any] | None = None,
|
||||
*,
|
||||
deps: WorkflowRuntimeDeps | None = None,
|
||||
resume_payload: dict[str, Any] | None = None,
|
||||
config: WorkflowConfig | None = None,
|
||||
) -> tuple[Any, list[dict[str, Any]]]:
|
||||
run_store = MemoryWorkflowRunStore()
|
||||
event_store = MemoryWorkflowEventStore()
|
||||
published: list[dict[str, Any]] = []
|
||||
|
||||
async def publish(event: dict[str, Any]) -> None:
|
||||
published.append(event)
|
||||
|
||||
sink = PersistingWorkflowEventSink(event_store, run_id="run-1", workflow_id=graph.id, version_id="v1", publish=publish)
|
||||
ctx = RunContext(
|
||||
run_id="run-1",
|
||||
workflow_id=graph.id,
|
||||
version_id="v1",
|
||||
owner_id="user-a",
|
||||
graph=graph,
|
||||
inputs=inputs or {},
|
||||
deps=deps or WorkflowRuntimeDeps(),
|
||||
cancel=CancelToken(),
|
||||
emit=sink.emit,
|
||||
resume_payload=resume_payload,
|
||||
)
|
||||
engine = WorkflowEngine(build_default_registry(config or WorkflowConfig()), run_store=run_store)
|
||||
result = await engine.run(ctx)
|
||||
return result, published
|
||||
|
||||
|
||||
# ── expressions ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_expression_rendering_and_paths() -> None:
|
||||
state = {
|
||||
"inputs": {"topic": "储能"},
|
||||
"nodes": {"n1": {"data": {"items": [{"title": "A"}, {"title": "B"}], "score": 0.9}}},
|
||||
"run": {"id": "r1"},
|
||||
"loop": {},
|
||||
"env": {},
|
||||
}
|
||||
assert render_template("主题:{{ inputs.topic }}", state) == "主题:储能"
|
||||
assert render_template("{{ nodes.n1.data.items[1].title }}", state) == "B"
|
||||
# A lone placeholder keeps the native type.
|
||||
assert render_value("{{ nodes.n1.data.score }}", state) == 0.9
|
||||
assert render_value({"a": ["{{ inputs.topic }}"]}, state) == {"a": ["储能"]}
|
||||
# Missing paths resolve to empty rather than exploding.
|
||||
assert render_template("{{ nodes.nope.data.x }}", state) == ""
|
||||
|
||||
|
||||
def test_expression_rejects_unknown_roots_and_code() -> None:
|
||||
state = {"inputs": {}, "nodes": {}, "run": {}, "loop": {}, "env": {}}
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
render_template("{{ __import__('os').system('id') }}", state)
|
||||
assert exc.value.code == "WORKFLOW_EXPRESSION_INVALID"
|
||||
with pytest.raises(WorkflowError):
|
||||
render_template("{{ os.environ.SECRET }}", state)
|
||||
|
||||
|
||||
def test_condition_evaluation() -> None:
|
||||
state = {
|
||||
"inputs": {"count": 5, "tags": ["a", "b"], "name": "储能报告"},
|
||||
"nodes": {"n1": {"data": {"ok": True}}},
|
||||
"run": {},
|
||||
"loop": {"n1": 2},
|
||||
"env": {},
|
||||
}
|
||||
assert evaluate_condition("inputs.count > 3", state) is True
|
||||
assert evaluate_condition("inputs.count > 3 and nodes.n1.data.ok", state) is True
|
||||
assert evaluate_condition("inputs.count > 9 or nodes.n1.data.ok == true", state) is True
|
||||
assert evaluate_condition("'a' in inputs.tags", state) is True
|
||||
assert evaluate_condition("inputs.name contains '储能'", state) is True
|
||||
assert evaluate_condition("not nodes.n1.data.ok", state) is False
|
||||
assert evaluate_condition("loop.n1 < 5", state) is True
|
||||
|
||||
|
||||
# ── scheduler ───────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_output_skeleton_emits_ordered_events() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start", "name": "开始"},
|
||||
{"id": "end", "type": "output", "name": "输出", "config": {"mapping": {"echo": "{{ inputs.topic }}"}}},
|
||||
],
|
||||
[{"id": "e1", "source": "start", "target": "end"}],
|
||||
)
|
||||
result, events = await _run(graph, {"topic": "储能"})
|
||||
assert result.output == {"echo": "储能"}
|
||||
seqs = [e["seq"] for e in events]
|
||||
assert seqs == sorted(seqs) and seqs[0] == 1
|
||||
kinds = [e["event"] for e in events]
|
||||
assert kinds == [
|
||||
"node.started",
|
||||
"node.completed",
|
||||
"node.started",
|
||||
"node.completed",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_condition_prunes_untaken_branch() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "gate",
|
||||
"type": "condition",
|
||||
"config": {
|
||||
"branches": [{"name": "big", "expression": "inputs.n > 10"}],
|
||||
"defaultBranch": "small",
|
||||
},
|
||||
},
|
||||
{"id": "big", "type": "transform", "config": {"operations": [{"op": "set", "target": "path", "value": "big"}]}},
|
||||
{"id": "small", "type": "transform", "config": {"operations": [{"op": "set", "target": "path", "value": "small"}]}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"path": "{{ nodes.small.data.path }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "gate"},
|
||||
{"id": "e2", "source": "gate", "target": "big", "sourcePort": "big"},
|
||||
{"id": "e3", "source": "gate", "target": "small", "sourcePort": "small"},
|
||||
{"id": "e4", "source": "small", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, _ = await _run(graph, {"n": 1})
|
||||
assert result.output == {"path": "small"}
|
||||
assert "big" not in result.node_results
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_parallel_fanout_joins_at_merge() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "a", "type": "transform", "config": {"operations": [{"op": "set", "target": "v", "value": "A"}]}},
|
||||
{"id": "b", "type": "transform", "config": {"operations": [{"op": "set", "target": "v", "value": "B"}]}},
|
||||
{"id": "join", "type": "merge", "config": {"strategy": "all"}},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"joined": "{{ nodes.join.data.succeeded }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "a"},
|
||||
{"id": "e2", "source": "start", "target": "b"},
|
||||
{"id": "e3", "source": "a", "target": "join"},
|
||||
{"id": "e4", "source": "b", "target": "join"},
|
||||
{"id": "e5", "source": "join", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, _ = await _run(graph)
|
||||
assert sorted(result.output["joined"]) == ["a", "b"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_loop_reruns_body_and_respects_cap() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "body", "type": "transform", "config": {"operations": [{"op": "set", "target": "tick", "value": "1"}]}},
|
||||
{
|
||||
"id": "gate",
|
||||
"type": "loop",
|
||||
"config": {"maxIterations": 3, "continueWhen": "true", "bodyEntry": "body"},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"iterations": "{{ nodes.gate.data.iteration }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "body"},
|
||||
{"id": "e2", "source": "body", "target": "gate"},
|
||||
{"id": "e3", "source": "gate", "target": "body", "sourcePort": "continue"},
|
||||
{"id": "e4", "source": "gate", "target": "end", "sourcePort": "done"},
|
||||
],
|
||||
settings={"maxLoopIterations": 5},
|
||||
)
|
||||
result, _ = await _run(graph)
|
||||
# maxIterations=3 means the gate stops handing out the continue port at 2.
|
||||
assert result.output["iterations"] == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_max_steps_limit_is_enforced() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "a", "type": "transform"},
|
||||
{"id": "end", "type": "output"},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "a"},
|
||||
{"id": "e2", "source": "a", "target": "end"},
|
||||
],
|
||||
settings={"maxSteps": 2},
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph)
|
||||
assert exc.value.code == "WORKFLOW_MAX_STEPS"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_input_pauses_then_resumes() -> None:
|
||||
nodes = [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "ask",
|
||||
"type": "human_input",
|
||||
"config": {"prompt": "请确认 {{ inputs.topic }}", "formSchema": {"required": ["comment"]}, "actions": ["submit"]},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"comment": "{{ nodes.ask.data.values.comment }}"}}},
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "start", "target": "ask"},
|
||||
{"id": "e2", "source": "ask", "target": "end"},
|
||||
]
|
||||
graph = _graph(nodes, edges)
|
||||
with pytest.raises(WorkflowPaused) as paused:
|
||||
await _run(graph, {"topic": "储能"})
|
||||
assert paused.value.node_id == "ask"
|
||||
assert paused.value.pending_input["prompt"] == "请确认 储能"
|
||||
assert paused.value.pending_input["formSchema"]["required"] == ["comment"]
|
||||
|
||||
result, _ = await _run(
|
||||
graph,
|
||||
{"topic": "储能"},
|
||||
resume_payload={"nodeId": "ask", "action": "submit", "values": {"comment": "同意"}},
|
||||
)
|
||||
assert result.output == {"comment": "同意"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_input_rejects_missing_required_field() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "ask", "type": "human_input", "config": {"formSchema": {"required": ["comment"]}}},
|
||||
{"id": "end", "type": "output"},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "ask"},
|
||||
{"id": "e2", "source": "ask", "target": "end"},
|
||||
],
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph, resume_payload={"nodeId": "ask", "action": "submit", "values": {}})
|
||||
assert exc.value.code == "WORKFLOW_HUMAN_INPUT_INVALID"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_node_failure_can_be_tolerated_with_on_error_continue() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "boom",
|
||||
"type": "transform",
|
||||
"config": {"onError": "continue", "operations": [{"op": "not_a_real_op"}]},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"ok": "true"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "boom"},
|
||||
{"id": "e2", "source": "boom", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, events = await _run(graph)
|
||||
assert result.output == {"ok": "true"}
|
||||
assert result.node_results["boom"]["metadata"]["failed"] is True
|
||||
assert any(e["event"] == "node.failed" for e in events)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_node_streams_deltas_and_parses_json() -> None:
|
||||
async def run_agent(**kwargs: Any) -> dict[str, Any]:
|
||||
on_delta = kwargs["on_delta"]
|
||||
await on_delta("思考")
|
||||
await on_delta("完成")
|
||||
assert "储能" in kwargs["prompt"]
|
||||
return {"text": '```json\n{"title": "储能报告"}\n```', "usage": {"tokens": 12}}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "writer",
|
||||
"type": "agent",
|
||||
"config": {
|
||||
"agentId": "a1",
|
||||
"promptTemplate": "写一份关于 {{ inputs.topic }} 的报告",
|
||||
"responseMode": "json",
|
||||
"responseSchema": {"required": ["title"]},
|
||||
"parallelGroupId": "round-1",
|
||||
"roundIndex": 1,
|
||||
"agentName": "报告撰写智能体",
|
||||
"agentAvatarUrl": "https://assets.example.com/writer.png",
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"title": "{{ nodes.writer.data.json.title }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "writer"},
|
||||
{"id": "e2", "source": "writer", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, events = await _run(graph, {"topic": "储能"}, deps=WorkflowRuntimeDeps(run_agent=run_agent))
|
||||
assert result.output == {"title": "储能报告"}
|
||||
deltas = [(e["data"].get("delta") or e["data"].get("text")) for e in events if e["event"] == "node.output.delta"]
|
||||
assert "".join(deltas) == "思考完成"
|
||||
assert all(e["data"].get("channel") == "answer" for e in events if e["event"] == "node.output.delta")
|
||||
started = next(event for event in events if event["event"] == "node.started" and event["nodeId"] == "writer")
|
||||
assert started["data"]["nodeKind"] == "agent"
|
||||
assert started["data"]["parallelGroupId"] == "round-1"
|
||||
assert started["data"]["roundIndex"] == 1
|
||||
assert started["data"]["agent"] == {
|
||||
"agentId": "a1",
|
||||
"name": "报告撰写智能体",
|
||||
"avatarUrl": "https://assets.example.com/writer.png",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_capability_becomes_typed_error() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "writer", "type": "agent", "config": {"agentId": "a1", "promptTemplate": "hi"}},
|
||||
],
|
||||
[{"id": "e1", "source": "start", "target": "writer"}],
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph)
|
||||
assert exc.value.code == "WORKFLOW_RESOURCE_MISSING"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancellation_stops_the_run() -> None:
|
||||
run_store = MemoryWorkflowRunStore()
|
||||
event_store = MemoryWorkflowEventStore()
|
||||
graph = _graph(
|
||||
[{"id": "start", "type": "start"}, {"id": "end", "type": "output"}],
|
||||
[{"id": "e1", "source": "start", "target": "end"}],
|
||||
)
|
||||
sink = PersistingWorkflowEventSink(event_store, run_id="run-x", workflow_id=graph.id, version_id="v1")
|
||||
cancel = CancelToken()
|
||||
cancel.cancel()
|
||||
ctx = RunContext(
|
||||
run_id="run-x",
|
||||
workflow_id=graph.id,
|
||||
version_id="v1",
|
||||
owner_id="u",
|
||||
graph=graph,
|
||||
inputs={},
|
||||
deps=WorkflowRuntimeDeps(),
|
||||
cancel=cancel,
|
||||
emit=sink.emit,
|
||||
)
|
||||
engine = WorkflowEngine(build_default_registry(), run_store=run_store)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await engine.run(ctx)
|
||||
assert exc.value.code == "WORKFLOW_CANCELLED"
|
||||
|
||||
|
||||
# ── policies ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_sql_policy_blocks_writes_and_stacked_statements() -> None:
|
||||
assert assert_read_only("SELECT id FROM t WHERE x = :x").startswith("SELECT")
|
||||
assert assert_read_only("WITH c AS (SELECT 1) SELECT * FROM c").startswith("WITH")
|
||||
for bad in (
|
||||
"DELETE FROM t",
|
||||
"SELECT 1; DROP TABLE t",
|
||||
"SELECT 1 -- ;\n; DROP TABLE t",
|
||||
"SELECT * FROM t INTO OUTFILE '/tmp/x'",
|
||||
"SELECT pg_sleep(10)",
|
||||
"UPDATE t SET a = 1",
|
||||
):
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
assert_read_only(bad)
|
||||
assert exc.value.code == "WORKFLOW_SQL_POLICY_DENIED"
|
||||
|
||||
|
||||
def test_sql_limit_is_appended_once() -> None:
|
||||
assert enforce_limit("SELECT 1", 10) == "SELECT 1 LIMIT 10"
|
||||
assert enforce_limit("SELECT 1 LIMIT 5", 10) == "SELECT 1 LIMIT 5"
|
||||
|
||||
|
||||
def test_sql_table_allowlist_blocks_unlisted_tables() -> None:
|
||||
assert_tables_allowed("SELECT id FROM report_source", ["report_source"])
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
assert_tables_allowed("SELECT id FROM secrets", ["report_source"])
|
||||
assert exc.value.code == "WORKFLOW_SQL_POLICY_DENIED"
|
||||
|
||||
|
||||
def test_downstream_node_reference_is_rejected_at_publish() -> None:
|
||||
issues = validate_workflow_graph(
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf_ref",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "early",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "x", "value": "{{ nodes.late.data.x }}"}]},
|
||||
},
|
||||
{
|
||||
"id": "late",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "x", "value": "1"}]},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"x": "{{ nodes.late.data.x }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "early"},
|
||||
{"id": "e2", "source": "early", "target": "late"},
|
||||
{"id": "e3", "source": "late", "target": "end"},
|
||||
],
|
||||
}
|
||||
)
|
||||
assert any(i.code == "WORKFLOW_EXPRESSION_INVALID" and i.node_id == "early" for i in issues)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_policy_blocks_private_and_metadata_targets() -> None:
|
||||
policy = WorkflowConfig().http
|
||||
for url in (
|
||||
"http://127.0.0.1:8001/admin",
|
||||
"http://169.254.169.254/latest/meta-data/",
|
||||
"http://localhost/",
|
||||
"file:///etc/passwd",
|
||||
"http://example.com:22/",
|
||||
):
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await enforce_http_target(url, policy)
|
||||
assert exc.value.code in ("WORKFLOW_HTTP_POLICY_DENIED", "WORKFLOW_HTTP_FAILED")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_policy_allows_private_when_configured() -> None:
|
||||
policy = WorkflowConfig().http.model_copy(update={"allow_private_networks": True, "allowed_ports": [8001]})
|
||||
target = await enforce_http_target("http://127.0.0.1:8001/ping", policy)
|
||||
assert target["host"] == "127.0.0.1"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_event_sink_persists_before_publishing() -> None:
|
||||
store = MemoryWorkflowEventStore()
|
||||
seen: list[int] = []
|
||||
|
||||
async def publish(event: dict[str, Any]) -> None:
|
||||
# Persisted rows must already be readable when the live copy arrives.
|
||||
rows = await store.list_after("r1", after_seq=0)
|
||||
seen.append(len(rows))
|
||||
|
||||
sink = PersistingWorkflowEventSink(store, run_id="r1", workflow_id="w1", version_id="v1", publish=publish)
|
||||
await sink.emit("run.started")
|
||||
await sink.emit("run.completed")
|
||||
assert seen == [1, 2]
|
||||
assert [r["seq"] for r in await store.list_after("r1")] == [1, 2]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_store_lease_and_idempotency() -> None:
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
store = MemoryWorkflowRunStore()
|
||||
payload = {
|
||||
"workflow_id": "w1",
|
||||
"workflow_version_id": "v1",
|
||||
"owner_id": "u1",
|
||||
"idempotency_key": "k1",
|
||||
"input": {"a": 1},
|
||||
}
|
||||
run, created = await store.create_run(payload)
|
||||
assert created is True
|
||||
again, created_again = await store.create_run(payload)
|
||||
assert created_again is False and again["id"] == run["id"]
|
||||
|
||||
until = datetime.now(UTC) + timedelta(seconds=30)
|
||||
claimed = await store.claim_run(run["id"], lease_owner="w-a", lease_until=until)
|
||||
assert claimed is not None and claimed["status"] == "running"
|
||||
# A second worker cannot steal a live lease.
|
||||
assert await store.claim_run(run["id"], lease_owner="w-b", lease_until=until) is None
|
||||
assert await store.renew_lease(run["id"], lease_owner="w-a", lease_until=until) is True
|
||||
assert await store.renew_lease(run["id"], lease_owner="w-b", lease_until=until) is False
|
||||
|
||||
# Expired lease is reclaimable (crash recovery).
|
||||
await store.renew_lease(run["id"], lease_owner="w-a", lease_until=datetime.now(UTC) - timedelta(seconds=1))
|
||||
assert await store.claim_run(run["id"], lease_owner="w-b", lease_until=until) is not None
|
||||
|
||||
paused = await store.set_awaiting_input(run["id"], lease_owner="w-b", pending_input={"nodeId": "ask"}, resume_token="t1")
|
||||
assert paused is not None and paused["status"] == "awaiting_input"
|
||||
assert await store.consume_resume_token(run["id"], "wrong") is None
|
||||
resumed = await store.consume_resume_token(run["id"], "t1")
|
||||
assert resumed is not None and resumed["status"] == "queued"
|
||||
# One-shot: the same token cannot be replayed.
|
||||
assert await store.consume_resume_token(run["id"], "t1") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_node_disabled_by_default() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "calc", "type": "code", "config": {"source": "output['x'] = 1"}},
|
||||
],
|
||||
[{"id": "e1", "source": "start", "target": "calc"}],
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph)
|
||||
assert exc.value.code == "WORKFLOW_CODE_SANDBOX_FAILED"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_node_executes_when_enabled() -> None:
|
||||
config = WorkflowConfig()
|
||||
config.code.enabled = True
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "calc",
|
||||
"type": "code",
|
||||
"config": {
|
||||
"source": "output['doubled'] = inputs['n'] * 2",
|
||||
"inputs": {"n": "{{ inputs.n }}"},
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"doubled": "{{ nodes.calc.data.doubled }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "calc"},
|
||||
{"id": "e2", "source": "calc", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, _ = await _run(graph, {"n": 21}, config=config)
|
||||
assert result.output == {"doubled": 42}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_code_node_cannot_open_sockets() -> None:
|
||||
config = WorkflowConfig()
|
||||
config.code.enabled = True
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "calc",
|
||||
"type": "code",
|
||||
"config": {"source": "import socket\nsocket.socket()"},
|
||||
},
|
||||
],
|
||||
[{"id": "e1", "source": "start", "target": "calc"}],
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph, config=config)
|
||||
assert exc.value.code == "WORKFLOW_CODE_SANDBOX_FAILED"
|
||||
|
||||
|
||||
def test_asyncio_smoke() -> None:
|
||||
# Guards against an event-loop policy regression on Windows runners.
|
||||
assert asyncio.get_event_loop_policy() is not None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skill_node_invokes_agent_with_restricted_skills() -> None:
|
||||
seen: dict[str, Any] = {}
|
||||
|
||||
async def run_agent(**kwargs: Any) -> dict[str, Any]:
|
||||
seen.update(kwargs)
|
||||
return {"text": "已完成检索", "thread_id": "thr-1"}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "skill",
|
||||
"type": "skill",
|
||||
"config": {
|
||||
"agentId": "collector",
|
||||
"skillNames": ["web_search"],
|
||||
"promptTemplate": "查 {{ inputs.topic }}",
|
||||
"mode": "agent_skill",
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"text": "{{ nodes.skill.data.text }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "skill"},
|
||||
{"id": "e2", "source": "skill", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, _published = await _run(graph, {"topic": "储能"}, deps=WorkflowRuntimeDeps(run_agent=run_agent))
|
||||
assert result.output == {"text": "已完成检索"}
|
||||
assert seen["agent_id"] == "collector"
|
||||
assert seen["skill_names"] == ["web_search"]
|
||||
assert "储能" in seen["prompt"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_callable_skill_mode_is_rejected() -> None:
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "skill",
|
||||
"type": "skill",
|
||||
"config": {"agentId": "collector", "skillNames": ["web_search"], "mode": "callable_skill"},
|
||||
},
|
||||
],
|
||||
[{"id": "e1", "source": "start", "target": "skill"}],
|
||||
)
|
||||
with pytest.raises(WorkflowError) as exc:
|
||||
await _run(graph)
|
||||
assert exc.value.code == "WORKFLOW_SKILL_FAILED"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subworkflow_node_folds_child_output() -> None:
|
||||
async def run_subworkflow(**kwargs: Any) -> dict[str, Any]:
|
||||
assert kwargs["workflow_id"] == "child-wf"
|
||||
assert kwargs["version_id"] == "child-v1"
|
||||
assert kwargs["inputs"] == {"topic": "储能"}
|
||||
return {"run_id": "child-run", "status": "completed", "output": {"title": "子报告"}}
|
||||
|
||||
graph = _graph(
|
||||
[
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "child",
|
||||
"type": "subworkflow",
|
||||
"config": {
|
||||
"workflowId": "child-wf",
|
||||
"versionId": "child-v1",
|
||||
"inputMapping": {"topic": "{{ inputs.topic }}"},
|
||||
},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"title": "{{ nodes.child.data.title }}"}}},
|
||||
],
|
||||
[
|
||||
{"id": "e1", "source": "start", "target": "child"},
|
||||
{"id": "e2", "source": "child", "target": "end"},
|
||||
],
|
||||
)
|
||||
result, _ = await _run(graph, {"topic": "储能"}, deps=WorkflowRuntimeDeps(run_subworkflow=run_subworkflow))
|
||||
assert result.output == {"title": "子报告"}
|
||||
@@ -0,0 +1,707 @@
|
||||
"""Run lifecycle tests: HTTP APIs, dispatcher + executor, SSE, pause/resume, embed.
|
||||
|
||||
Everything runs inside one event loop (httpx ASGI transport rather than
|
||||
``TestClient``) because the in-memory stores hold ``asyncio`` primitives that
|
||||
must not be shared across loops.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.gateway.routers import workflow_data_sources, workflow_embed, workflow_resources
|
||||
from app.gateway.routers import workflow_runs as runs_router
|
||||
from app.gateway.workflow_dispatcher import WorkflowRunDispatcher
|
||||
from app.gateway.workflow_executor import WorkflowRunExecutor
|
||||
from app.gateway.workflow_live_hub import WorkflowLiveHub
|
||||
from deerflow.config.workflow_config import WorkflowConfig
|
||||
from deerflow.persistence.workflow_data_sources import MemoryWorkflowDataSourceStore
|
||||
from deerflow.persistence.workflow_events import MemoryWorkflowEventStore
|
||||
from deerflow.persistence.workflow_runs import MemoryWorkflowRunStore
|
||||
from deerflow.persistence.workflows import MemoryWorkflowStore
|
||||
|
||||
REPORT_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-demo",
|
||||
"name": "demo",
|
||||
"inputSchema": {"required": ["topic"]},
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "shape",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "title", "value": "{{ inputs.topic }} 报告"}]},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"title": "{{ nodes.shape.data.title }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "shape"},
|
||||
{"id": "e2", "source": "shape", "target": "end"},
|
||||
],
|
||||
}
|
||||
|
||||
FAIL_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-fail",
|
||||
"name": "fail",
|
||||
"inputSchema": {"required": ["topic"]},
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "shape",
|
||||
"type": "transform",
|
||||
"config": {"operations": [{"op": "set", "target": "title", "value": "{{ inputs.topic }}"}]},
|
||||
},
|
||||
{"id": "boom", "type": "code", "config": {"source": "output['x'] = 1"}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "shape"},
|
||||
{"id": "e2", "source": "shape", "target": "boom"},
|
||||
],
|
||||
}
|
||||
|
||||
PAUSE_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-pause",
|
||||
"name": "pause",
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{
|
||||
"id": "ask",
|
||||
"type": "human_input",
|
||||
"config": {"prompt": "确认", "formSchema": {"required": ["comment"]}, "actions": ["submit"]},
|
||||
},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"comment": "{{ nodes.ask.data.values.comment }}"}}},
|
||||
],
|
||||
"edges": [
|
||||
{"id": "e1", "source": "start", "target": "ask"},
|
||||
{"id": "e2", "source": "ask", "target": "end"},
|
||||
],
|
||||
}
|
||||
|
||||
TYPED_INPUT_GRAPH: dict[str, Any] = {
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf-typed",
|
||||
"name": "typed",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"required": ["topic"],
|
||||
"properties": {"topic": {"type": "string"}, "depth": {"type": "integer"}},
|
||||
},
|
||||
"nodes": [
|
||||
{"id": "start", "type": "start"},
|
||||
{"id": "end", "type": "output", "config": {"mapping": {"topic": "{{ inputs.topic }}"}}},
|
||||
],
|
||||
"edges": [{"id": "e1", "source": "start", "target": "end"}],
|
||||
}
|
||||
|
||||
|
||||
class Harness:
|
||||
def __init__(self, app: FastAPI, client: httpx.AsyncClient, config: WorkflowConfig) -> None:
|
||||
self.app = app
|
||||
self.client = client
|
||||
self.config = config
|
||||
|
||||
@property
|
||||
def runs(self) -> MemoryWorkflowRunStore:
|
||||
return self.app.state.workflow_run_store
|
||||
|
||||
async def drive(self, rounds: int = 4) -> None:
|
||||
"""Dispatch and wait for the launched run tasks, as the loop would."""
|
||||
executor = self.app.state.workflow_executor
|
||||
for _ in range(rounds):
|
||||
await self.app.state.workflow_dispatcher.dispatch_once()
|
||||
pending = [t for t in executor._tasks.values() if not t.done()]
|
||||
if pending:
|
||||
await asyncio.wait(pending, timeout=10)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
async def publish(self, graph: dict[str, Any], *, name: str = "demo") -> tuple[str, str]:
|
||||
store = self.app.state.workflow_store
|
||||
definition = await store.create_definition({"name": name, "owner_id": "user-a", "draft_graph": graph})
|
||||
version = await store.publish_version(
|
||||
definition["id"],
|
||||
graph=graph,
|
||||
graph_hash="hash-1",
|
||||
published_by="user-a",
|
||||
change_note="init",
|
||||
)
|
||||
return definition["id"], version["id"]
|
||||
|
||||
async def start(self, workflow_id: str, version_id: str, **body: Any) -> dict[str, Any]:
|
||||
payload = {"versionId": version_id, "inputs": {"topic": "储能"}, **body}
|
||||
response = await self.client.post(f"/api/workflows/{workflow_id}/runs", json=payload)
|
||||
assert response.status_code == 200, response.text
|
||||
return response.json()
|
||||
|
||||
async def events(self, run_id: str, *, after: int = 0) -> list[dict[str, Any]]:
|
||||
response = await self.client.get(f"/api/workflows/runs/{run_id}/events?after={after}")
|
||||
assert response.status_code == 200, response.text
|
||||
return response.json()["events"]
|
||||
|
||||
|
||||
def _sse_events(body: str) -> list[dict[str, Any]]:
|
||||
return [json.loads(line[len("data: ") :]) for line in body.splitlines() if line.startswith("data: ")]
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def harness(monkeypatch: pytest.MonkeyPatch):
|
||||
config = WorkflowConfig()
|
||||
|
||||
class _Cfg:
|
||||
workflows = config
|
||||
|
||||
async def _user(_request=None) -> str:
|
||||
return "user-a"
|
||||
|
||||
async def _optional(_request=None):
|
||||
return None
|
||||
|
||||
for module in (runs_router, workflow_data_sources, workflow_embed, workflow_resources):
|
||||
monkeypatch.setattr(module, "get_app_config", lambda: _Cfg())
|
||||
monkeypatch.setattr(module, "get_current_user", _user)
|
||||
if hasattr(module, "get_optional_user_from_request"):
|
||||
monkeypatch.setattr(module, "get_optional_user_from_request", _optional)
|
||||
|
||||
app = FastAPI()
|
||||
app.state.workflow_store = MemoryWorkflowStore()
|
||||
app.state.workflow_run_store = MemoryWorkflowRunStore()
|
||||
app.state.workflow_event_store = MemoryWorkflowEventStore()
|
||||
app.state.workflow_data_source_store = MemoryWorkflowDataSourceStore()
|
||||
app.state.workflow_live_hub = WorkflowLiveHub()
|
||||
app.state.checkpointer = None
|
||||
app.state.store = None
|
||||
app.state.workflow_executor = WorkflowRunExecutor(
|
||||
app,
|
||||
run_store=app.state.workflow_run_store,
|
||||
event_store=app.state.workflow_event_store,
|
||||
workflow_store=app.state.workflow_store,
|
||||
data_source_store=app.state.workflow_data_source_store,
|
||||
live_publisher=app.state.workflow_live_hub.publish,
|
||||
config=config,
|
||||
)
|
||||
app.state.workflow_dispatcher = WorkflowRunDispatcher(
|
||||
app.state.workflow_run_store,
|
||||
app.state.workflow_executor,
|
||||
event_store=app.state.workflow_event_store,
|
||||
live_publisher=app.state.workflow_live_hub.publish,
|
||||
config=config,
|
||||
)
|
||||
app.include_router(workflow_data_sources.router)
|
||||
app.include_router(workflow_embed.router)
|
||||
app.include_router(workflow_resources.router)
|
||||
app.include_router(runs_router.router)
|
||||
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
yield Harness(app, client, config)
|
||||
|
||||
|
||||
# ── run lifecycle ───────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_start_executes_and_completes(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id, idempotencyKey="k-1")
|
||||
assert run["status"] == "queued" and run["created"] is True
|
||||
|
||||
again = await harness.start(workflow_id, version_id, idempotencyKey="k-1")
|
||||
assert again["id"] == run["id"] and again["created"] is False
|
||||
|
||||
await harness.drive()
|
||||
|
||||
detail = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert detail["status"] == "completed"
|
||||
assert detail["output"] == {"title": "储能 报告"}
|
||||
assert {n["nodeId"]: n["status"] for n in detail["nodeRuns"]} == {
|
||||
"start": "completed",
|
||||
"shape": "completed",
|
||||
"end": "completed",
|
||||
}
|
||||
|
||||
events = await harness.events(run["id"])
|
||||
seqs = [e["seq"] for e in events]
|
||||
assert seqs == sorted(seqs) and len(set(seqs)) == len(seqs)
|
||||
kinds = [e["event"] for e in events]
|
||||
assert kinds[0] == "run.created" and kinds[1] == "run.started"
|
||||
assert kinds[-1] == "run.completed"
|
||||
assert kinds.count("node.completed") == 3
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_required_input_is_rejected_before_queueing(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
response = await harness.client.post(f"/api/workflows/{workflow_id}/runs", json={"versionId": version_id, "inputs": {}})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_INPUT_INVALID"
|
||||
assert await harness.runs.list_runs(limit=10) == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_typed_input_schema_violations_return_structured_400(harness: Harness) -> None:
|
||||
"""Wrong types (not just missing keys) fail at start with field paths."""
|
||||
workflow_id, version_id = await harness.publish(TYPED_INPUT_GRAPH, name="typed")
|
||||
response = await harness.client.post(
|
||||
f"/api/workflows/{workflow_id}/runs",
|
||||
json={"versionId": version_id, "inputs": {"topic": 123, "depth": "many"}},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
detail = response.json()["detail"]
|
||||
assert detail["code"] == "WORKFLOW_INPUT_INVALID"
|
||||
paths = {tuple(e["path"]) for e in detail["details"]["errors"]}
|
||||
assert ("topic",) in paths and ("depth",) in paths
|
||||
assert await harness.runs.list_runs(limit=10) == []
|
||||
|
||||
ok = await harness.client.post(
|
||||
f"/api/workflows/{workflow_id}/runs",
|
||||
json={"versionId": version_id, "inputs": {"topic": "储能", "depth": 2}},
|
||||
)
|
||||
assert ok.status_code == 200, ok.text
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unpublished_workflow_cannot_run(harness: Harness) -> None:
|
||||
definition = await harness.app.state.workflow_store.create_definition({"name": "draft-only", "owner_id": "user-a", "draft_graph": REPORT_GRAPH})
|
||||
response = await harness.client.post(f"/api/workflows/{definition['id']}/runs", json={"inputs": {"topic": "x"}})
|
||||
assert response.status_code == 400
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_VERSION_NOT_FOUND"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_events_replay_from_cursor(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
|
||||
everything = await harness.events(run["id"])
|
||||
tail = await harness.events(run["id"], after=2)
|
||||
assert [e["seq"] for e in tail] == [e["seq"] for e in everything[2:]]
|
||||
aliased = await harness.client.get(f"/api/workflows/runs/{run['id']}/events?after_seq=2")
|
||||
assert aliased.status_code == 200
|
||||
assert [e["seq"] for e in aliased.json()["events"]] == [e["seq"] for e in everything[2:]]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_replays_and_closes_on_terminal_event(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
|
||||
async with harness.client.stream("GET", f"/api/workflows/runs/{run['id']}/stream") as response:
|
||||
assert response.status_code == 200
|
||||
body = "".join([chunk async for chunk in response.aiter_text()])
|
||||
|
||||
events = _sse_events(body)
|
||||
assert [e["seq"] for e in events] == sorted(e["seq"] for e in events)
|
||||
assert events[-1]["event"] == "run.completed"
|
||||
assert events[-1]["data"]["output"] == {"title": "储能 报告"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancel_before_dispatch_reaches_terminal_state(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
|
||||
cancelled = (await harness.client.post(f"/api/workflows/runs/{run['id']}/cancel")).json()
|
||||
assert cancelled["cancelled"] is True and cancelled["status"] == "cancel_requested"
|
||||
|
||||
await harness.drive()
|
||||
final = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert final["status"] == "cancelled"
|
||||
kinds = [e["event"] for e in await harness.events(run["id"])]
|
||||
assert "run.cancel_requested" in kinds and "run.cancelled" in kinds
|
||||
# The graph never ran, so no node ever started.
|
||||
assert not any(k.startswith("node.") for k in kinds)
|
||||
|
||||
repeat = (await harness.client.post(f"/api/workflows/runs/{run['id']}/cancel")).json()
|
||||
assert repeat["cancelled"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_human_input_pause_and_resume_round_trip(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(PAUSE_GRAPH, name="pause")
|
||||
run = await harness.start(workflow_id, version_id, inputs={})
|
||||
await harness.drive()
|
||||
|
||||
paused = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert paused["status"] == "awaiting_input"
|
||||
assert paused["pendingInput"]["nodeId"] == "ask"
|
||||
assert paused["pendingInput"]["prompt"] == "确认"
|
||||
# The resume token travels on the event stream only, never on the run view.
|
||||
assert "resumeToken" not in json.dumps(paused)
|
||||
|
||||
awaiting = next(e for e in await harness.events(run["id"]) if e["event"] == "run.awaiting_input")
|
||||
token = awaiting["data"]["resumeToken"]
|
||||
|
||||
bad = await harness.client.post(
|
||||
f"/api/workflows/runs/{run['id']}/resume",
|
||||
json={"resumeToken": "nope", "action": "submit", "values": {"comment": "ok"}},
|
||||
)
|
||||
assert bad.status_code == 409
|
||||
assert bad.json()["detail"]["code"] == "WORKFLOW_RESUME_TOKEN_INVALID"
|
||||
|
||||
resumed = await harness.client.post(
|
||||
f"/api/workflows/runs/{run['id']}/resume",
|
||||
json={"resumeToken": token, "action": "submit", "values": {"comment": "同意"}},
|
||||
)
|
||||
assert resumed.status_code == 200 and resumed.json()["status"] == "queued"
|
||||
|
||||
await harness.drive()
|
||||
done = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert done["status"] == "completed"
|
||||
assert done["output"] == {"comment": "同意"}
|
||||
kinds = [e["event"] for e in await harness.events(run["id"])]
|
||||
# start was replayed rather than re-executed: three nodes, three completions.
|
||||
assert kinds.count("node.completed") == 3
|
||||
assert "run.resumed" in kinds
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resume_is_rejected_when_not_paused(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
response = await harness.client.post(f"/api/workflows/runs/{run['id']}/resume", json={"resumeToken": "x", "action": "submit"})
|
||||
assert response.status_code == 409
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_RUN_NOT_RESUMABLE"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_concurrent_run_limit(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
harness.config.max_concurrent_runs_per_user = 1
|
||||
await harness.start(workflow_id, version_id)
|
||||
second = await harness.client.post(
|
||||
f"/api/workflows/{workflow_id}/runs",
|
||||
json={"versionId": version_id, "inputs": {"topic": "b"}},
|
||||
)
|
||||
assert second.status_code == 429
|
||||
assert second.json()["detail"]["code"] == "WORKFLOW_LIMIT_EXCEEDED"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_other_users_run_is_not_visible(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
|
||||
async def _other(_request=None) -> str:
|
||||
return "user-b"
|
||||
|
||||
monkeypatch.setattr(runs_router, "get_current_user", _other)
|
||||
response = await harness.client.get(f"/api/workflows/runs/{run['id']}")
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_FORBIDDEN"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_expired_lease_is_reclaimed_by_another_worker(harness: Harness) -> None:
|
||||
"""Restart recovery: a run left ``running`` by a dead worker is picked up."""
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
|
||||
claimed = await harness.runs.claim_run(run["id"], lease_owner="dead-worker", lease_until=datetime.now(UTC) + timedelta(seconds=30))
|
||||
assert claimed is not None and claimed["status"] == "running"
|
||||
await harness.runs.renew_lease(run["id"], lease_owner="dead-worker", lease_until=datetime.now(UTC) - timedelta(seconds=1))
|
||||
|
||||
await harness.drive()
|
||||
final = await harness.runs.get_run(run["id"])
|
||||
assert final["status"] == "completed"
|
||||
assert final["lease_owner"] is None
|
||||
assert final["attempt"] == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_disabled_workflows_return_503(harness: Harness) -> None:
|
||||
harness.config.enabled = False
|
||||
response = await harness.client.get("/api/workflows/runs/anything")
|
||||
assert response.status_code == 503
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retry_failed_run_replays_completed_nodes(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(FAIL_GRAPH, name="fail")
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
failed = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert failed["status"] == "failed"
|
||||
|
||||
response = await harness.client.post(f"/api/workflows/runs/{run['id']}/retry")
|
||||
assert response.status_code == 201, response.text
|
||||
retried = response.json()
|
||||
assert retried["created"] is True
|
||||
assert retried["id"] != run["id"]
|
||||
assert retried["retryOfRunId"] == run["id"]
|
||||
assert retried["status"] == "queued"
|
||||
|
||||
await harness.drive()
|
||||
again = (await harness.client.get(f"/api/workflows/runs/{retried['id']}")).json()
|
||||
assert again["status"] == "failed"
|
||||
assert again["retryOfRunId"] == run["id"]
|
||||
resumed = next(e for e in await harness.events(retried["id"]) if e["event"] == "run.resumed")
|
||||
assert set(resumed["data"]["replayedNodes"]) == {"start", "shape"}
|
||||
original = (await harness.client.get(f"/api/workflows/runs/{run['id']}")).json()
|
||||
assert original["status"] == "failed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retry_rejected_unless_failed(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
queued = await harness.client.post(f"/api/workflows/runs/{run['id']}/retry")
|
||||
assert queued.status_code == 409
|
||||
assert queued.json()["detail"]["code"] == "WORKFLOW_RUN_NOT_RETRYABLE"
|
||||
|
||||
await harness.drive()
|
||||
completed = await harness.client.post(f"/api/workflows/runs/{run['id']}/retry")
|
||||
assert completed.status_code == 409
|
||||
assert completed.json()["detail"]["code"] == "WORKFLOW_RUN_NOT_RETRYABLE"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_last_event_id_beats_after_query(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
everything = await harness.events(run["id"])
|
||||
header_from = everything[1]["seq"]
|
||||
|
||||
async with harness.client.stream(
|
||||
"GET",
|
||||
f"/api/workflows/runs/{run['id']}/stream?after=0",
|
||||
headers={"Last-Event-ID": str(header_from)},
|
||||
) as response:
|
||||
assert response.status_code == 200
|
||||
body = "".join([chunk async for chunk in response.aiter_text()])
|
||||
|
||||
events = _sse_events(body)
|
||||
assert events[0]["seq"] == header_from + 1
|
||||
assert events[-1]["event"] == "run.completed"
|
||||
assert [e["seq"] for e in events] == sorted(e["seq"] for e in events)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_reconnect_storm_is_ordered_and_complete(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
expected = [e["seq"] for e in await harness.events(run["id"])]
|
||||
|
||||
async def reconnect(after: int) -> list[int]:
|
||||
async with harness.client.stream(
|
||||
"GET",
|
||||
f"/api/workflows/runs/{run['id']}/stream?after={after}",
|
||||
headers={"Last-Event-ID": str(after)},
|
||||
) as response:
|
||||
body = "".join([chunk async for chunk in response.aiter_text()])
|
||||
events = _sse_events(body)
|
||||
seqs = [e["seq"] for e in events]
|
||||
assert seqs == sorted(set(seqs))
|
||||
assert events[-1]["event"] == "run.completed"
|
||||
return seqs
|
||||
|
||||
waves = await asyncio.gather(*[reconnect(after) for after in (0, 0, 1, 2, 3) for _ in range(4)])
|
||||
full = [w for w in waves if w and w[0] == expected[0]]
|
||||
assert full
|
||||
assert all(w == expected for w in full)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_dispatchers_only_one_claims_a_run(harness: Harness) -> None:
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
rival = WorkflowRunDispatcher(
|
||||
harness.app.state.workflow_run_store,
|
||||
harness.app.state.workflow_executor,
|
||||
event_store=harness.app.state.workflow_event_store,
|
||||
live_publisher=harness.app.state.workflow_live_hub.publish,
|
||||
config=harness.config,
|
||||
)
|
||||
started = await asyncio.gather(
|
||||
harness.app.state.workflow_dispatcher.dispatch_once(),
|
||||
rival.dispatch_once(),
|
||||
)
|
||||
assert sum(started) == 1
|
||||
pending = [t for t in harness.app.state.workflow_executor._tasks.values() if not t.done()]
|
||||
if pending:
|
||||
await asyncio.wait(pending, timeout=10)
|
||||
final = await harness.runs.get_run(run["id"])
|
||||
assert final is not None and final["status"] == "completed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retention_cleaner_purges_old_runs_and_events(harness: Harness) -> None:
|
||||
from app.gateway.workflow_retention import WorkflowRetentionCleaner
|
||||
|
||||
workflow_id, version_id = await harness.publish(REPORT_GRAPH)
|
||||
run = await harness.start(workflow_id, version_id)
|
||||
await harness.drive()
|
||||
stale = datetime.now(UTC) - timedelta(days=120)
|
||||
harness.runs._runs[run["id"]]["finished_at"] = stale.isoformat()
|
||||
for event in harness.app.state.workflow_event_store._events.get(run["id"], []):
|
||||
event["created_at"] = stale.isoformat()
|
||||
|
||||
cleaner = WorkflowRetentionCleaner(
|
||||
harness.runs,
|
||||
harness.app.state.workflow_event_store,
|
||||
config=harness.config,
|
||||
)
|
||||
result = await cleaner.sweep()
|
||||
assert result["runs"] == 1
|
||||
assert result["events"] >= 1
|
||||
assert await harness.runs.get_run(run["id"]) is None
|
||||
assert await harness.app.state.workflow_event_store.list_after(run["id"]) == []
|
||||
|
||||
|
||||
# ── data sources ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_data_source_secret_is_write_only(harness: Harness) -> None:
|
||||
created = await harness.client.post(
|
||||
"/api/workflows/data-sources",
|
||||
json={
|
||||
"name": "reporting",
|
||||
"kind": "sql",
|
||||
"dsn": "postgresql+asyncpg://reader:s3cret@db.internal:5432/analytics",
|
||||
"maxRows": 500,
|
||||
},
|
||||
)
|
||||
assert created.status_code == 200, created.text
|
||||
body = created.json()
|
||||
assert "s3cret" not in json.dumps(body) and "reader" not in json.dumps(body)
|
||||
assert body["masked_target"].startswith("postgresql+asyncpg://***@")
|
||||
assert body["max_rows"] == 500
|
||||
|
||||
listed = (await harness.client.get("/api/workflows/data-sources")).json()
|
||||
assert "s3cret" not in json.dumps(listed)
|
||||
assert [s["id"] for s in listed["dataSources"]] == [body["id"]]
|
||||
|
||||
catalog = (await harness.client.get("/api/workflows/resources/data-sources")).json()
|
||||
assert catalog["dataSources"][0]["id"] == body["id"]
|
||||
|
||||
fetched = (await harness.client.get(f"/api/workflows/data-sources/{body['id']}")).json()
|
||||
assert fetched["id"] == body["id"] and "s3cret" not in json.dumps(fetched)
|
||||
|
||||
introspected = (await harness.client.post(f"/api/workflows/data-sources/{body['id']}/introspect")).json()
|
||||
assert introspected["tables"] == []
|
||||
|
||||
valid = await harness.client.post(
|
||||
f"/api/workflows/data-sources/{body['id']}/validate-query",
|
||||
json={"statement": "SELECT 1"},
|
||||
)
|
||||
assert valid.status_code == 200 and valid.json()["ok"] is True
|
||||
denied = await harness.client.post(
|
||||
f"/api/workflows/data-sources/{body['id']}/validate-query",
|
||||
json={"statement": "DELETE FROM t"},
|
||||
)
|
||||
assert denied.status_code == 400
|
||||
|
||||
# The executor is the only reader of the plaintext.
|
||||
resolved = await harness.app.state.workflow_data_source_store.resolve_dsn(body["id"])
|
||||
assert resolved["dsn"].endswith("/analytics")
|
||||
|
||||
deleted = (await harness.client.delete(f"/api/workflows/data-sources/{body['id']}")).json()
|
||||
assert deleted["deleted"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sql_data_source_requires_a_dsn(harness: Harness) -> None:
|
||||
response = await harness.client.post("/api/workflows/data-sources", json={"name": "bad", "kind": "sql"})
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_http_data_source_keeps_headers_write_only_and_exposes_resource_metadata(
|
||||
harness: Harness,
|
||||
) -> None:
|
||||
created = await harness.client.post(
|
||||
"/api/workflows/data-sources",
|
||||
json={
|
||||
"name": "crm-http",
|
||||
"description": "查询 CRM 客户",
|
||||
"kind": "http",
|
||||
"baseUrl": "https://crm.example.com/v1/customers",
|
||||
"allowedMethods": ["get", "POST", "GET"],
|
||||
"headers": {"Authorization": "Bearer header-secret"},
|
||||
},
|
||||
)
|
||||
assert created.status_code == 200, created.text
|
||||
body = created.json()
|
||||
assert body["kind"] == "http"
|
||||
assert body["base_url"] == "https://crm.example.com/v1/customers"
|
||||
assert body["allowed_methods"] == ["GET", "POST"]
|
||||
assert body["masked_target"] == "https://crm.example.com/v1/customers"
|
||||
assert "header-secret" not in json.dumps(body)
|
||||
|
||||
listed = (await harness.client.get("/api/workflows/data-sources")).json()
|
||||
assert "header-secret" not in json.dumps(listed)
|
||||
|
||||
catalog = (await harness.client.get("/api/workflows/resources/data-sources")).json()
|
||||
item = catalog["dataSources"][0]
|
||||
assert item["dataSourceId"] == body["id"]
|
||||
assert item["baseUrl"] == "https://crm.example.com/v1/customers"
|
||||
assert item["allowedMethods"] == ["GET", "POST"]
|
||||
assert item["readOnly"] is False
|
||||
assert "header-secret" not in json.dumps(catalog)
|
||||
|
||||
# The executor is the sole plaintext reader, including for HTTP headers.
|
||||
resolved = await harness.app.state.workflow_data_source_store.resolve_dsn(body["id"])
|
||||
assert resolved["kind"] == "http"
|
||||
assert resolved["base_url"] == "https://crm.example.com/v1/customers"
|
||||
assert json.loads(resolved["dsn"])["headers"]["Authorization"] == "Bearer header-secret"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_shared_data_source_requires_admin(harness: Harness) -> None:
|
||||
response = await harness.client.post(
|
||||
"/api/workflows/data-sources",
|
||||
json={"name": "shared", "kind": "sql", "dsn": "sqlite+aiosqlite:///x.db", "shared": True},
|
||||
)
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
# ── embed ───────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_embed_ticket_requires_allowlisted_origin(harness: Harness) -> None:
|
||||
workflow_id, _ = await harness.publish(REPORT_GRAPH)
|
||||
response = await harness.client.post(
|
||||
"/api/workflows/embed/tickets",
|
||||
json={"workflowId": workflow_id, "origin": "https://evil.example.com"},
|
||||
)
|
||||
assert response.status_code == 403
|
||||
assert response.json()["detail"]["code"] == "WORKFLOW_EMBED_ORIGIN_DENIED"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_embed_ticket_round_trip(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("WORKFLOW_SECRET_KEY", "unit-test-embed-signing-key")
|
||||
harness.config.embed.allowed_origins = ["https://host.example.com"]
|
||||
|
||||
workflow_id, _ = await harness.publish(REPORT_GRAPH)
|
||||
issued = await harness.client.post(
|
||||
"/api/workflows/embed/tickets",
|
||||
json={"workflowId": workflow_id, "origin": "https://host.example.com/app"},
|
||||
)
|
||||
assert issued.status_code == 200, issued.text
|
||||
assert issued.json()["frameAncestors"] == ["https://host.example.com"]
|
||||
ticket = issued.json()["ticket"]
|
||||
|
||||
verified = await harness.client.post(
|
||||
"/api/workflows/embed/verify",
|
||||
json={"ticket": ticket, "origin": "https://host.example.com"},
|
||||
)
|
||||
assert verified.status_code == 200
|
||||
assert verified.json()["workflowId"] == workflow_id
|
||||
|
||||
replay = await harness.client.post("/api/workflows/embed/verify", json={"ticket": ticket})
|
||||
assert replay.status_code == 401
|
||||
@@ -0,0 +1,194 @@
|
||||
"""Phase-0 workflow protocol contract tests.
|
||||
|
||||
Locks graph schema, event envelope, error codes, and shipped sample fixtures.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from deerflow.workflows.errors import (
|
||||
ALL_WORKFLOW_ERROR_CODES,
|
||||
WorkflowError,
|
||||
WorkflowErrorBody,
|
||||
)
|
||||
from deerflow.workflows.events import (
|
||||
ALL_WORKFLOW_EVENT_TYPES,
|
||||
TERMINAL_EVENT_TYPES,
|
||||
WorkflowEventEnvelope,
|
||||
)
|
||||
from deerflow.workflows.samples import list_node_samples, load_node_sample, load_sample_graph
|
||||
from deerflow.workflows.schemas import (
|
||||
ALL_NODE_TYPES,
|
||||
SCHEMA_VERSION,
|
||||
AgentNodeConfig,
|
||||
NodeResult,
|
||||
SqlReadNodeConfig,
|
||||
StartRunRequest,
|
||||
WorkflowGraph,
|
||||
WorkflowNode,
|
||||
)
|
||||
|
||||
|
||||
def test_schema_version_frozen() -> None:
|
||||
assert SCHEMA_VERSION == "1.0"
|
||||
|
||||
|
||||
def test_all_node_types_have_samples() -> None:
|
||||
samples = set(list_node_samples())
|
||||
assert samples == set(ALL_NODE_TYPES)
|
||||
for node_type in ALL_NODE_TYPES:
|
||||
raw = load_node_sample(node_type)
|
||||
node = WorkflowNode.model_validate(raw)
|
||||
assert node.type == node_type
|
||||
|
||||
|
||||
def test_report_generation_sample_loads() -> None:
|
||||
graph = load_sample_graph("report_generation")
|
||||
assert graph.id == "wf_report_generation"
|
||||
assert graph.schema_version == "1.0"
|
||||
types = {n.type for n in graph.nodes}
|
||||
assert "sql_read" in types
|
||||
assert "agent" in types
|
||||
assert "condition" in types
|
||||
assert "loop" in types
|
||||
assert "merge" in types
|
||||
assert "output" in types
|
||||
digest = graph.graph_hash()
|
||||
assert len(digest) == 64
|
||||
assert digest == graph.graph_hash() # deterministic
|
||||
|
||||
|
||||
def test_start_output_sample_loads() -> None:
|
||||
graph = load_sample_graph("start_output")
|
||||
assert len(graph.nodes) == 2
|
||||
assert len(graph.edges) == 1
|
||||
|
||||
|
||||
def test_duplicate_node_id_rejected() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
WorkflowGraph.model_validate(
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf_bad",
|
||||
"nodes": [
|
||||
{"id": "a", "type": "start"},
|
||||
{"id": "a", "type": "output"},
|
||||
],
|
||||
"edges": [],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_edge_target_must_exist() -> None:
|
||||
with pytest.raises(ValidationError):
|
||||
WorkflowGraph.model_validate(
|
||||
{
|
||||
"schemaVersion": "1.0",
|
||||
"id": "wf_bad",
|
||||
"nodes": [{"id": "start_1", "type": "start"}],
|
||||
"edges": [{"id": "e1", "source": "start_1", "target": "missing"}],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_camel_case_roundtrip() -> None:
|
||||
graph = load_sample_graph("start_output")
|
||||
wire = json.loads(graph.model_dump_json(by_alias=True))
|
||||
assert "schemaVersion" in wire
|
||||
assert "inputSchema" in wire
|
||||
assert "runTimeoutSeconds" in wire["settings"]
|
||||
again = WorkflowGraph.model_validate(wire)
|
||||
assert again.graph_hash() == graph.graph_hash()
|
||||
|
||||
|
||||
def test_node_result_and_typed_configs() -> None:
|
||||
result = NodeResult.model_validate(
|
||||
{
|
||||
"data": {"answer": "ok"},
|
||||
"artifacts": [
|
||||
{
|
||||
"artifactId": "a1",
|
||||
"name": "r.md",
|
||||
"mimeType": "text/markdown",
|
||||
"path": "/mnt/data/r.md",
|
||||
}
|
||||
],
|
||||
"warnings": [],
|
||||
}
|
||||
)
|
||||
assert result.artifacts[0].artifact_id == "a1"
|
||||
|
||||
agent = AgentNodeConfig.model_validate(
|
||||
{
|
||||
"agentId": "x",
|
||||
"promptTemplate": "hi",
|
||||
"responseMode": "json",
|
||||
"threadMode": "isolated_per_node",
|
||||
}
|
||||
)
|
||||
assert agent.agent_id == "x"
|
||||
|
||||
sql = SqlReadNodeConfig.model_validate(
|
||||
{
|
||||
"dataSourceId": "ds_1",
|
||||
"statement": "SELECT 1",
|
||||
"parameters": {},
|
||||
"maxRows": 10,
|
||||
}
|
||||
)
|
||||
assert sql.data_source_id == "ds_1"
|
||||
|
||||
|
||||
def test_start_run_request() -> None:
|
||||
req = StartRunRequest.model_validate(
|
||||
{
|
||||
"versionId": "wv_03",
|
||||
"inputs": {"topic": "市场分析"},
|
||||
"idempotencyKey": "client-uuid",
|
||||
}
|
||||
)
|
||||
assert req.version_id == "wv_03"
|
||||
assert req.execution_mode == "normal"
|
||||
|
||||
|
||||
def test_event_envelope_fixture() -> None:
|
||||
path = Path(__file__).resolve().parents[1] / "packages/harness/deerflow/workflows/samples/event_envelope_example.json"
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
env = WorkflowEventEnvelope.model_validate(raw)
|
||||
assert env.event_type == "node.output.delta"
|
||||
assert env.seq == 42
|
||||
sse = env.to_sse_dict()
|
||||
assert sse["event"] == "node.output.delta"
|
||||
assert sse["runId"] == "run_01"
|
||||
frame = env.to_sse_frame()
|
||||
assert frame.startswith("id: 42\n")
|
||||
assert "event: node.output.delta\n" in frame
|
||||
assert '"schemaVersion":"1.0"' in frame.replace(" ", "")
|
||||
|
||||
|
||||
def test_event_type_set_frozen() -> None:
|
||||
assert "run.completed" in ALL_WORKFLOW_EVENT_TYPES
|
||||
assert "run.failed" in TERMINAL_EVENT_TYPES
|
||||
assert "run.cancelled" in TERMINAL_EVENT_TYPES
|
||||
assert len(ALL_WORKFLOW_EVENT_TYPES) == 18
|
||||
|
||||
|
||||
def test_error_body_and_exception() -> None:
|
||||
assert "WORKFLOW_SQL_POLICY_DENIED" in ALL_WORKFLOW_ERROR_CODES
|
||||
err = WorkflowError(
|
||||
"WORKFLOW_SQL_POLICY_DENIED",
|
||||
"SQL 节点只允许单条只读查询",
|
||||
node_id="sql_1",
|
||||
details={"rule": "single_read_statement"},
|
||||
)
|
||||
body = err.to_body()
|
||||
assert isinstance(body, WorkflowErrorBody)
|
||||
wire = body.model_dump(by_alias=True)
|
||||
assert wire["nodeId"] == "sql_1"
|
||||
assert wire["code"] == "WORKFLOW_SQL_POLICY_DENIED"
|
||||
assert wire["retryable"] is False
|
||||
Reference in new issue
Block a user