RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(agent): preserve content semantics with mixed Message branches (#19565)
Bruce-Yiia5670d280932
diff --git a/agent/component/agent_with_tools.py b/agent/component/agent_with_tools.pyindex f5d49b6659..28cf590f20 100644--- a/agent/component/agent_with_tools.py+++ b/agent/component/agent_with_tools.py@@ -237,7 +237,18 @@ class Agent(LLM, ToolBase):component = self._canvas.get_component(self._id)downstreams = component["downstream"] if component else []ex = self.exception_handler()- has_message_downstream = any(self._canvas.get_component_obj(cid).component_name.lower() == "message" for cid in downstreams)+ # Defer to an unconsumed partial ONLY when every downstream consumer is+ # a Message: Message knows how to consume the partial (it streams it).+ # Any other consumer (Agent/LLM, Fillup, Invoke, ...) would receive the+ # opaque partial object instead of the semantic answer -- e.g. LLM+ # prompt normalization stringifies it into "functools.partial(...)",+ # leaking the bound system prompt/message state. A mixed graph therefore+ # takes the eager path so every consumer sees the same answer string.+ # (An empty downstream list keeps the previous eager behavior. A+ # dangling downstream ID -- retained by the canvas/DSL but missing+ # from the loaded components -- must not raise here: it is simply+ # ineligible for streaming, so the eager path runs instead.)+ has_message_downstream = bool(downstreams) and all((component := self._canvas.get_component(cid)) is not None and component["obj"].component_name.lower() == "message" for cid in downstreams)if has_message_downstream and not (ex and ex["goto"]) and not output_schema:_logger.debug("[Agent] Entering streaming mode (has message downstream)")self.set_output("content", partial(self.stream_output_with_tools_async, prompt, deepcopy(msg), user_defined_prompt))diff --git a/agent/component/llm.py b/agent/component/llm.pyindex a25a2c476c..078b829a3c 100644--- a/agent/component/llm.py+++ b/agent/component/llm.py@@ -471,7 +471,19 @@ class LLM(ComponentBase):downstreams = self._canvas.get_component(self._id)["downstream"] if self._canvas.get_component(self._id) else []ex = self.exception_handler()- if any([self._canvas.get_component_obj(cid).component_name.lower() == "message" for cid in downstreams]) and not (ex and ex["goto"]):+ # Defer to an unconsumed partial ONLY when every downstream consumer is+ # a Message: Message knows how to consume the partial (it streams it).+ # Any other consumer (Agent/LLM, Fillup, Invoke, ...) would receive the+ # opaque partial object instead of the semantic answer -- e.g. LLM+ # prompt normalization stringifies it into "functools.partial(...)",+ # leaking the bound system prompt/message state. A mixed graph therefore+ # takes the eager path so every consumer sees the same answer string.+ # (An empty downstream list keeps the previous eager behavior. A+ # dangling downstream ID -- retained by the canvas/DSL but missing+ # from the loaded components -- must not raise here: it is simply+ # ineligible for streaming, so the eager path runs instead.)+ has_message_downstream = bool(downstreams) and all((component := self._canvas.get_component(cid)) is not None and component["obj"].component_name.lower() == "message" for cid in downstreams)+ if has_message_downstream and not (ex and ex["goto"]):self.set_output("content", partial(self._stream_output_async, prompt, deepcopy(msg)))returndiff --git a/internal/agent/canvas/direct_message_downstream_test.go b/internal/agent/canvas/direct_message_downstream_test.gonew file mode 100644index 0000000000..ed1e09f312--- /dev/null+++ b/internal/agent/canvas/direct_message_downstream_test.go@@ -0,0 +1,190 @@+// Package canvas — directMessageDownstream unit tests.+package canvas++import "testing"++// TestDirectMessageDownstream covers the mixed-downstream contract:+// deferral is only allowed when every direct downstream is a Message.+func TestDirectMessageDownstream(t *testing.T) {+ cases := []struct {+ name string+ canvas *Canvas+ cpnID string+ deferred bool+ }{+ {+ name: "nil canvas",+ canvas: nil,+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "unknown component",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0"},+ },+ },+ },+ cpnID: "missing",+ deferred: false,+ },+ {+ name: "no downstream keeps eager",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "sole message downstream defers",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0"},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: true,+ },+ {+ name: "two message downstreams defer",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0", "message_1"},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ "message_1": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: true,+ },+ {+ // Regression for the mixed-branch bug: an Agent feeding both an+ // Agent and a Message must stay eager, otherwise the downstream+ // Agent receives the opaque deferred stream instead of the answer.+ name: "mixed agent and message downstream stays eager",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"agent_1", "message_0"},+ },+ "agent_1": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "message first then agent still stays eager",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0", "agent_1"},+ },+ "agent_1": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "non-message downstream only keeps eager",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"fillup_0"},+ },+ "fillup_0": {+ Obj: CanvasComponentObj{ComponentName: "Fillup"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "dangling downstream reference stays eager",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0", "ghost_0"},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "Message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: false,+ },+ {+ name: "component name is case insensitive",+ canvas: &Canvas{+ Components: map[string]CanvasComponent{+ "agent_0": {+ Obj: CanvasComponentObj{ComponentName: "Agent"},+ Downstream: []string{"message_0"},+ },+ "message_0": {+ Obj: CanvasComponentObj{ComponentName: "message"},+ Downstream: []string{},+ },+ },+ },+ cpnID: "agent_0",+ deferred: true,+ },+ }++ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ if got := directMessageDownstream(tc.canvas, tc.cpnID); got != tc.deferred {+ t.Errorf("directMessageDownstream(%q) = %v, want %v", tc.cpnID, got, tc.deferred)+ }+ })+ }+}diff --git a/internal/agent/canvas/scheduler.go b/internal/agent/canvas/scheduler.goindex 65d4986ef6..ecc788823c 100644--- a/internal/agent/canvas/scheduler.go+++ b/internal/agent/canvas/scheduler.go@@ -739,24 +739,30 @@ func BuildWorkflow(ctx context.Context, c *Canvas) (*compose.Workflow[map[stringreturn wf, nil}-// directMessageDownstream: only a direct-// Message child enables lazy Agent execution. Intermediate nodes must not+// directMessageDownstream reports whether a component may hand a deferred+// stream to its downstream consumers. Only a direct Message child enables lazy+// Agent execution, and only when EVERY direct downstream is a Message.+//+// A mixed graph (Agent -> [Agent, Message]) must keep eager execution: the+// deferred stream is opaque to non-Message consumers, which would otherwise+// observe the lazy object instead of the semantic answer. An empty downstream+// list keeps eager execution as before. Intermediate nodes must not// accidentally change the Agent's execution mode.func directMessageDownstream(c *Canvas, cpnID string) bool {if c == nil {return false}comp, ok := c.Components[cpnID]- if !ok {+ if !ok || len(comp.Downstream) == 0 {return false}for _, downID := range comp.Downstream {down, ok := c.Components[downID]- if ok && strings.EqualFold(down.Obj.ComponentName, "Message") {- return true+ if !ok || !strings.EqualFold(down.Obj.ComponentName, "Message") {+ return false}}- return false+ return true}func wireWorkflowTerminals(diff --git a/test/unit_test/agent/component/test_agent_message_branch_content.py b/test/unit_test/agent/component/test_agent_message_branch_content.pynew file mode 100644index 0000000000..9299bb93f3--- /dev/null+++ b/test/unit_test/agent/component/test_agent_message_branch_content.py@@ -0,0 +1,215 @@+import asyncio+import sys+from functools import partial+from types import SimpleNamespace+from unittest.mock import MagicMock++import pytest++try:+ import cv2 # noqa: F401 — availability probe only+except ImportError:+ # Preflight runners may lack the system OpenGL lib (libGL.so.1) required+ # by the non-headless opencv build pulled in via deepdoc vision OCR.+ # These tests never touch vision/OCR, so fall back to a stub instead of+ # breaking test collection on such runners.+ sys.modules["cv2"] = MagicMock()++import agent.component.llm as llm_module+from agent.component.agent_with_tools import Agent++SECRET_AGENT_SYSTEM_PROMPT = "SECRET_AGENT1_SYSTEM_PROMPT_s3cr3t"+SECRET_USER_QUESTION = "USER_Q_SECRET_XYZ"+PRODUCED_ANSWER = "GLORP_ANSWER_42"+++class _FakeCanvas:+ """Minimum canvas surface the exercised methods touch (no model, no DB)."""++ def __init__(self):+ self.vars = {}+ self.components = {}+ self.objs = {}+ self.globals = {}+ self.task_id = "test-task"++ def get_component(self, cid):+ # Mirrors real Canvas.get_component: None for a missing ID (a+ # dangling downstream reference retained by the DSL).+ return self.components.get(cid)++ def get_component_obj(self, cid):+ return self.objs[cid]++ def get_variable_value(self, exp):+ if exp.endswith("@_references"):+ return self.vars.get(exp)+ return self.vars[exp]++ def get_component_name(self, cid):+ obj = self.objs.get(cid)+ if obj is None:+ obj = {k.lower(): v for k, v in self.objs.items()}[cid.lower()]+ return obj.component_name.lower()++ def get_tenant_id(self):+ return "tenant1"++ def get_history(self, n):+ return []++ def get_reference(self):+ return {"chunks": []}++ def is_reff(self, v):+ return False++ def is_canceled(self):+ return False+++def _make_param(prompts, sys_prompt=""):+ return SimpleNamespace(+ inputs={},+ outputs={},+ prompts=prompts,+ sys_prompt=sys_prompt,+ cite=False,+ visual_files_var=None,+ message_history_window_size=13,+ llm_id="test-llm",+ max_retries=0,+ delay_after_error=0,+ exception_method=None,+ exception_default_value=None,+ exception_goto=None,+ debug_inputs={},+ )+++def _make_agent(canvas, cid, param, tools):+ agent = Agent.__new__(Agent)+ agent._canvas = canvas+ agent._id = cid+ agent._param = param+ agent.tools = tools+ agent.chat_mdl = SimpleNamespace(max_length=8192)+ agent.imgs = []+ return agent+++def no_model_resolution(monkeypatch):+ """_invoke_async resolves tenant model config through the DB; stub it+ so the test needs no MySQL/credentials/model (mirrors the LLM suite)."""+ monkeypatch.setattr(llm_module, "resolve_model_type", lambda tenant, ref: ["chat"])+ monkeypatch.setattr(llm_module, "resolve_model_config", lambda tenant, mtype, ref: {})+ monkeypatch.setattr(llm_module, "LLMBundle", lambda *a, **k: SimpleNamespace(max_length=8192))+++def _set_downstream(canvas, cid, component_name):+ """Register a downstream component the way real Canvas entries look:+ the components-dict entry carries the live ``obj`` used by the+ streaming-eligibility predicate, for both ``get_component`` and+ ``get_component_obj`` lookup."""+ canvas.components[cid] = {"downstream": [], "obj": SimpleNamespace(component_name=component_name)}+ canvas.objs[cid] = SimpleNamespace(component_name=component_name)+++def _run_agent2_with_upstream(upstream_value):+ """Agent2 references Agent1@content; returns its normalized user input."""+ canvas = _FakeCanvas()+ canvas.vars["Agent1@content"] = upstream_value+ canvas.objs["agent1"] = SimpleNamespace(component_name="Agent")++ param = _make_param(+ [{"role": "user", "content": "Use this: {Agent1@content}"}],+ sys_prompt="AGENT2_SYS",+ )+ agent2 = _make_agent(canvas, "agent2", param, {"retrieval_0": object()})+ _, msg, _ = agent2._prepare_prompt_variables()+ return next(m for m in msg if m.get("role") == "user")["content"]+++def test_no_message_downstream_yields_eager_answer_string(no_model_resolution):+ """Without a direct Message downstream, Agent content is the eager+ answer string (pre-existing contract)."""+ canvas = _FakeCanvas()+ canvas.components["agent1"] = {"downstream": ["agent2"]}+ _set_downstream(canvas, "agent2", "Agent")++ param = _make_param([{"role": "user", "content": "What is glorp?"}], sys_prompt="SYS")+ agent1 = _make_agent(canvas, "agent1", param, {"retrieval_0": object()})+ agent1._fit_messages = lambda prompt, msg: (msg, None)++ async def fake_generate(msg):+ return PRODUCED_ANSWER++ agent1._generate_async = fake_generate++ result = asyncio.run(agent1._invoke_async())+ content = agent1._param.outputs["content"]["value"]+ assert result == PRODUCED_ANSWER+ assert isinstance(content, str)+ assert content == PRODUCED_ANSWER+++def test_pure_message_downstream_keeps_deferred_streaming_partial(no_model_resolution):+ """A sole direct Message downstream still gets the deferred streaming+ partial (streaming behavior preserved, no model call made here)."""+ canvas = _FakeCanvas()+ canvas.components["agent1"] = {"downstream": ["msg1"]}+ _set_downstream(canvas, "msg1", "Message")++ param = _make_param(+ [{"role": "user", "content": SECRET_USER_QUESTION}],+ sys_prompt=SECRET_AGENT_SYSTEM_PROMPT,+ )+ agent1 = _make_agent(canvas, "agent1", param, {"retrieval_0": object()})++ asyncio.run(agent1._invoke_async())+ content = agent1._param.outputs["content"]["value"]+ assert isinstance(content, partial)+ assert content.args[0] == SECRET_AGENT_SYSTEM_PROMPT+++def test_mixed_agent_and_message_downstream_sees_answer_not_partial(no_model_resolution):+ """Regression for https://github.com/infiniflow/ragflow/issues/19556.++ With a mixed graph (Agent1 -> Agent2 plus Agent1 -> Message), Agent1's+ semantic content must be the eager answer string -- never a partial --+ so Agent2's input cannot leak bound system prompt/message state.+ """+ canvas = _FakeCanvas()+ canvas.components["agent1"] = {"downstream": ["agent2", "msg1"]}+ _set_downstream(canvas, "agent2", "Agent")+ _set_downstream(canvas, "msg1", "Message")++ param = _make_param(+ [{"role": "user", "content": SECRET_USER_QUESTION}],+ sys_prompt=SECRET_AGENT_SYSTEM_PROMPT,+ )+ agent1 = _make_agent(canvas, "agent1", param, {"retrieval_0": object()})+ agent1._fit_messages = lambda prompt, msg: (msg, None)+ calls = []++ async def fake_generate(msg):+ calls.append(1)+ return PRODUCED_ANSWER++ agent1._generate_async = fake_generate+ asyncio.run(agent1._invoke_async())++ content = agent1._param.outputs["content"]["value"]+ assert content == PRODUCED_ANSWER+ assert not isinstance(content, partial)+ # Both consumers share the single eager result: one generation call.+ assert len(calls) == 1++ normalized = _run_agent2_with_upstream(content)+ assert "functools.partial" not in normalized+ assert SECRET_AGENT_SYSTEM_PROMPT not in normalized+ assert PRODUCED_ANSWER in normalizeddiff --git a/test/unit_test/agent/component/test_llm_message_branch_content.py b/test/unit_test/agent/component/test_llm_message_branch_content.pynew file mode 100644index 0000000000..f464a24fe0--- /dev/null+++ b/test/unit_test/agent/component/test_llm_message_branch_content.py@@ -0,0 +1,286 @@+import asyncio+from functools import partial+from types import SimpleNamespace++import pytest++import agent.component.llm as llm_module+from agent.component.llm import LLM, LLMParam++SECRET_LLM_SYSTEM_PROMPT = "SECRET_LLM_SYSTEM_PROMPT_s3cr3t"+SECRET_USER_QUESTION = "USER_Q_SECRET_XYZ"+PRODUCED_ANSWER = "GLORP_ANSWER_42"+++class _FakeCanvas:+ """Minimum canvas surface LLM._invoke_async touches (no model, no DB)."""++ def __init__(self):+ self.vars = {}+ self.components = {}+ self.objs = {}+ self.globals = {}+ self.task_id = "test-task"++ def get_component(self, cid):+ # Mirrors real Canvas.get_component: None for a missing ID (a+ # dangling downstream reference retained by the DSL).+ return self.components.get(cid)++ def get_component_obj(self, cid):+ return self.objs[cid]++ def get_variable_value(self, exp):+ if exp.endswith("@_references"):+ return self.vars.get(exp)+ return self.vars[exp]++ def get_component_name(self, cid):+ obj = self.objs.get(cid)+ if obj is None:+ obj = {k.lower(): v for k, v in self.objs.items()}[cid.lower()]+ return obj.component_name.lower()++ def get_tenant_id(self):+ return "tenant1"++ def get_history(self, n):+ return []++ def get_reference(self):+ return {"chunks": []}++ def is_reff(self, v):+ return False++ def is_canceled(self):+ return False+++def _make_param(prompts, sys_prompt=""):+ param = LLMParam()+ param.prompts = prompts+ param.sys_prompt = sys_prompt+ param.debug_inputs = {}+ return param+++def _make_llm(canvas, cid, param):+ cpn = LLM.__new__(LLM)+ cpn._canvas = canvas+ cpn._id = cid+ cpn._param = param+ cpn.chat_mdl = SimpleNamespace(max_length=8192)+ cpn.imgs = []+ return cpn+++def _link(canvas, cid, component_name, downstream=()):+ """Register a downstream component the way a loaded canvas would: the+ component dict carries the live ``obj`` while ``objs`` backs the legacy+ ``get_component_obj`` lookup."""+ canvas.components[cid] = {"downstream": list(downstream), "obj": SimpleNamespace(component_name=component_name)}+ canvas.objs[cid] = SimpleNamespace(component_name=component_name)+++def no_model_resolution(monkeypatch):+ """_prepare_prompt_variables resolves tenant model config through the DB;+ stub it so the test needs no MySQL/credentials/model."""+ monkeypatch.setattr(llm_module, "resolve_model_type", lambda tenant, ref: ["chat"])+ monkeypatch.setattr(llm_module, "resolve_model_config", lambda tenant, mtype, ref: {})+ monkeypatch.setattr(llm_module, "LLMBundle", lambda *a, **k: SimpleNamespace(max_length=8192))+++def _run_llm2_with_upstream(upstream_value):+ """A downstream LLM references LLM1@content; return its normalized user input."""+ canvas = _FakeCanvas()+ canvas.vars["LLM1@content"] = upstream_value+ canvas.objs["llm1"] = SimpleNamespace(component_name="LLM")++ param = _make_param([{"role": "user", "content": "Use this: {LLM1@content}"}])+ llm2 = _make_llm(canvas, "llm2", param)+ _, msg, _ = llm2._prepare_prompt_variables()+ return next(m for m in msg if m.get("role") == "user")["content"]+++def test_no_message_downstream_yields_eager_answer_string(no_model_resolution):+ """Without a direct Message downstream, content is the eager answer string+ (pre-existing contract)."""+ canvas = _FakeCanvas()+ canvas.components["llm1"] = {"downstream": ["llm2"]}+ _link(canvas, "llm2", "LLM")++ llm1 = _make_llm(canvas, "llm1", _make_param([{"role": "user", "content": "What is glorp?"}], "SYS"))++ async def fake_generate(msg, **kwargs):+ return PRODUCED_ANSWER++ llm1._generate_async = fake_generate++ asyncio.run(llm1._invoke_async())+ content = llm1._param.outputs["content"]["value"]+ assert isinstance(content, str)+ assert content == PRODUCED_ANSWER+++def test_pure_message_downstream_keeps_deferred_streaming_partial(no_model_resolution):+ """A sole direct Message downstream still gets the deferred streaming partial+ (streaming behavior preserved, no model call made here)."""+ canvas = _FakeCanvas()+ canvas.components["llm1"] = {"downstream": ["msg1"]}+ _link(canvas, "msg1", "Message")++ llm1 = _make_llm(canvas, "llm1", _make_param([{"role": "user", "content": SECRET_USER_QUESTION}], SECRET_LLM_SYSTEM_PROMPT))++ async def refusing_generate(msg, **kwargs):+ raise AssertionError("Message-only graph must not generate eagerly")++ llm1._generate_async = refusing_generate++ asyncio.run(llm1._invoke_async())+ content = llm1._param.outputs["content"]["value"]+ assert isinstance(content, partial)+ assert content.args[0] == SECRET_LLM_SYSTEM_PROMPT+++def test_mixed_llm_and_message_downstream_sees_answer_not_partial(no_model_resolution):+ """Regression for the no-tool path of #19556.++ A mixed graph (LLM1 -> LLM2 plus LLM1 -> Message) must keep eager+ execution: LLM2 would otherwise stringify the deferred partial and observe+ the upstream system prompt instead of the semantic answer.+ """+ canvas = _FakeCanvas()+ canvas.components["llm1"] = {"downstream": ["llm2", "msg1"]}+ _link(canvas, "llm2", "LLM")+ _link(canvas, "msg1", "Message")++ llm1 = _make_llm(canvas, "llm1", _make_param([{"role": "user", "content": SECRET_USER_QUESTION}], SECRET_LLM_SYSTEM_PROMPT))+ calls = []++ async def fake_generate(msg, **kwargs):+ calls.append(1)+ return PRODUCED_ANSWER++ llm1._generate_async = fake_generate+ asyncio.run(llm1._invoke_async())++ content = llm1._param.outputs["content"]["value"]+ assert content == PRODUCED_ANSWER+ assert not isinstance(content, partial)+ # Both consumers share the single eager result: one generation call.+ assert len(calls) == 1++ normalized = _run_llm2_with_upstream(content)+ assert "functools.partial" not in normalized+ assert SECRET_LLM_SYSTEM_PROMPT not in normalized+ assert PRODUCED_ANSWER in normalized+++def test_no_tool_agent_delegates_to_llm_mixed_branch(no_model_resolution):+ """A no-tool Agent falls back to LLM._invoke_async (agent_with_tools.py),+ so the mixed-branch contract must hold there too."""+ from agent.component.agent_with_tools import Agent, AgentParam++ canvas = _FakeCanvas()+ canvas.components["agent1"] = {"downstream": ["agent2", "msg1"]}+ _link(canvas, "agent2", "Agent")+ _link(canvas, "msg1", "Message")++ param = AgentParam()+ param.prompts = [{"role": "user", "content": SECRET_USER_QUESTION}]+ param.sys_prompt = SECRET_LLM_SYSTEM_PROMPT+ param.debug_inputs = {}++ agent1 = Agent.__new__(Agent)+ agent1._canvas = canvas+ agent1._id = "agent1"+ agent1._param = param+ agent1.chat_mdl = SimpleNamespace(max_length=8192)+ agent1.imgs = []+ agent1.tools = {}+ calls = []++ async def fake_generate(msg, **kwargs):+ calls.append(1)+ return PRODUCED_ANSWER++ agent1._generate_async = fake_generate+ asyncio.run(agent1._invoke_async())++ content = agent1._param.outputs["content"]["value"]+ assert content == PRODUCED_ANSWER+ assert not isinstance(content, partial)+ assert len(calls) == 1+++def test_dangling_downstream_id_stays_eager_without_raising(no_model_resolution):+ """A downstream ID missing from the loaded components (dangling DSL+ reference) must not raise: it is ineligible for streaming, so the LLM+ takes the eager path -- matching the Go helper, which already treats+ missing IDs as eager."""+ canvas = _FakeCanvas()+ canvas.components["llm1"] = {"downstream": ["msg1", "ghost_0"]}+ _link(canvas, "msg1", "Message")+ # ghost_0 is intentionally absent: a dangling reference.++ llm1 = _make_llm(canvas, "llm1", _make_param([{"role": "user", "content": SECRET_USER_QUESTION}], SECRET_LLM_SYSTEM_PROMPT))+ calls = []++ async def fake_generate(msg, **kwargs):+ calls.append(1)+ return PRODUCED_ANSWER++ llm1._generate_async = fake_generate+ asyncio.run(llm1._invoke_async())++ content = llm1._param.outputs["content"]["value"]+ assert content == PRODUCED_ANSWER+ assert not isinstance(content, partial)+ assert len(calls) == 1+++def test_tool_agent_dangling_downstream_id_stays_eager(no_model_resolution):+ """Same dangling-ID contract on the tool-enabled Agent path+ (AgentWithTools._invoke_async)."""+ from agent.component.agent_with_tools import Agent, AgentParam++ canvas = _FakeCanvas()+ canvas.components["agent1"] = {"downstream": ["msg1", "ghost_0"]}+ _link(canvas, "msg1", "Message")+ # ghost_0 is intentionally absent: a dangling reference.++ param = AgentParam()+ param.prompts = [{"role": "user", "content": SECRET_USER_QUESTION}]+ param.sys_prompt = SECRET_LLM_SYSTEM_PROMPT+ param.debug_inputs = {}++ agent1 = Agent.__new__(Agent)+ agent1._canvas = canvas+ agent1._id = "agent1"+ agent1._param = param+ agent1.chat_mdl = SimpleNamespace(max_length=8192)+ agent1.imgs = []+ agent1.tools = {"retrieval_0": object()}+ agent1._fit_messages = lambda prompt, msg: (msg, None)+ calls = []++ async def fake_generate(msg, **kwargs):+ calls.append(1)+ return PRODUCED_ANSWER++ agent1._generate_async = fake_generate+ result = asyncio.run(agent1._invoke_async())++ content = agent1._param.outputs["content"]["value"]+ assert result == PRODUCED_ANSWER+ assert content == PRODUCED_ANSWER+ assert not isinstance(content, partial)+ assert len(calls) == 1