feat(agent): add module-level debug logging for canvas execution flow (#16200)

Summary

Add module-level debug logging to track Agent canvas execution flow
(Closes #9306), enabling developers to diagnose component invocation,
input/output states, and variable resolution without modifying
production code.

Also fix related bugs in message.py: re.sub backreference issue and
unawaited _save_to_memory coroutine causing silent memory save failures.

Changes

agent/canvas.py: log workflow start, component invocation, and component
completion
agent/component/agent_with_tools.py: log Agent parameter resolution and
LLM invocation path; standardize json.dumps usage
agent/component/base.py: log get_input() variable resolution branches
agent/component/message.py: fix re.sub backreference issue; properly
await _save_to_memory coroutine

Design

Uses module-level loggers (logging.getLogger(__name__)) to support
selective debugging: LOG_LEVELS=agent=DEBUG
Zero performance impact in production (INFO level by default)
Works with existing PUT /system/config/log API for runtime level changes

Closes #9306

Note: While adding debug logging, I discovered and fixed two related
bugs in message.py:
- re.sub replacement value was interpreted as regex backreference
instead of literal string
- _save_to_memory coroutine was not properly awaited, causing silent
failures

---------

Co-authored-by: wills <willsgao@163.com>
This commit is contained in:
Willsgao
2026-06-29 09:41:16 +08:00
committed by yzc
parent dc07b6ca8f
commit 78db4e949b
4 changed files with 61 additions and 5 deletions

View File

@@ -34,6 +34,8 @@ from common.connection_utils import timeout
from common.mcp_tool_call_conn import MCPToolBinding, MCPToolCallSession, mcp_tool_metadata_to_openai_tool
from rag.prompts.generator import citation_plus, citation_prompt, full_question, kb_prompt, message_fit_in, structured_output_prompt
_logger = logging.getLogger(__name__)
class AgentParam(LLMParam, ToolParamBase):
"""
@@ -194,6 +196,14 @@ class Agent(LLM, ToolBase):
if self.check_if_canceled("Agent processing"):
return
_logger.debug(
"[Agent] _invoke_async called. Component: %s, Keys in kwargs: %s, user_prompt: %s, tools count: %d",
self._id,
list(kwargs.keys()),
json.dumps(kwargs.get("user_prompt", ""), ensure_ascii=False, default=str)[:300],
len(self.tools) if self.tools else 0,
)
if kwargs.get("user_prompt"):
usr_pmt = ""
if kwargs.get("reasoning"):
@@ -205,10 +215,13 @@ class Agent(LLM, ToolBase):
else:
usr_pmt = str(kwargs["user_prompt"])
self._param.prompts = [{"role": "user", "content": usr_pmt}]
_logger.debug("[Agent] Built user prompt with length=%d, reasoning=%s, context=%s",
len(usr_pmt), bool(kwargs.get("reasoning")), bool(kwargs.get("context")))
if not self.tools:
if self.check_if_canceled("Agent processing"):
return
_logger.debug("[Agent] No tools configured. Delegating to LLM._invoke_async. prompt_count=%d", len(self._param.prompts) if self._param.prompts else 0)
return await LLM._invoke_async(self, **kwargs)
prompt, msg, user_defined_prompt = self._prepare_prompt_variables()
@@ -223,11 +236,13 @@ class Agent(LLM, ToolBase):
ex = self.exception_handler()
has_message_downstream = any(self._canvas.get_component_obj(cid).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))
return
msg = self._fit_messages(prompt, msg)
self._append_system_prompt(msg, schema_prompt)
_logger.debug("[Agent] Calling LLM with %d messages, has_schema=%s", len(msg), bool(schema_prompt))
ans = await self._generate_async(msg)
if ans.find("**ERROR**") >= 0:
@@ -257,6 +272,7 @@ class Agent(LLM, ToolBase):
artifact_md = self._collect_tool_artifact_markdown(existing_text=ans)
if artifact_md:
ans += "\n\n" + artifact_md
_logger.debug("[Agent] Final output. content_length=%d, has_artifact=%s", len(ans), bool(artifact_md))
self.set_output("content", ans)
return ans