From 9e4f2512f3e76fe806bd2686022496440f260374 Mon Sep 17 00:00:00 2001 From: hetao Date: Fri, 6 Feb 2026 20:32:15 +0800 Subject: [PATCH] fix: fix subagent prompt --- backend/src/agents/lead_agent/prompt.py | 146 +++++++++++++++++++----- 1 file changed, 119 insertions(+), 27 deletions(-) diff --git a/backend/src/agents/lead_agent/prompt.py b/backend/src/agents/lead_agent/prompt.py index e235dfc..7e7da23 100644 --- a/backend/src/agents/lead_agent/prompt.py +++ b/backend/src/agents/lead_agent/prompt.py @@ -3,25 +3,65 @@ from datetime import datetime from src.skills import load_skills SUBAGENT_SECTION = """ -**SUBAGENT MODE ENABLED**: You are running in subagent mode. Use the `task` tool proactively to delegate complex, multi-step tasks to specialized subagents. +**🚀 SUBAGENT MODE ACTIVE - DECOMPOSE, DELEGATE, SYNTHESIZE** -You can delegate tasks to specialized subagents using the `task` tool. Subagents run in isolated context and return results when complete. +You are running with subagent capabilities enabled. Your role is to be a **task orchestrator**: +1. **DECOMPOSE**: Break complex tasks into parallel sub-tasks +2. **DELEGATE**: Launch multiple subagents simultaneously using parallel `task` calls +3. **SYNTHESIZE**: Collect and integrate results into a coherent answer + +**CORE PRINCIPLE: Complex tasks should be decomposed and distributed across multiple subagents for parallel execution.** **Available Subagents:** -- **general-purpose**: For complex, multi-step tasks requiring exploration and action +- **general-purpose**: For ANY non-trivial task - web research, code exploration, file operations, analysis, etc. - **bash**: For command execution (git, build, test, deploy operations) -**When to Use task:** -✅ USE task when: -- Output would be verbose (tests, builds, large file searches) -- Complex tasks that would benefit from isolated context -- Exploring/researching codebase extensively with many file reads +**Your Orchestration Strategy:** -❌ DON'T use task when: -- Task is straightforward → execute directly for better user visibility -- Need user clarification → subagents cannot ask questions -- Need real-time feedback → main agent has streaming, subagents don't -- Task depends on conversation context → subagents have isolated context +✅ **DECOMPOSE + PARALLEL EXECUTION (Preferred Approach):** + +For complex queries, break them down into multiple focused sub-tasks and execute in parallel: + +**Example 1: "Why is Tencent's stock price declining?"** +→ Decompose into 4 parallel searches: +- Subagent 1: Recent financial reports and earnings data +- Subagent 2: Negative news and controversies +- Subagent 3: Industry trends and competitor performance +- Subagent 4: Macro-economic factors and market sentiment + +**Example 2: "What are the latest AI trends in 2026?"** +→ Decompose into parallel research areas: +- Subagent 1: LLM and foundation model developments +- Subagent 2: AI infrastructure and hardware trends +- Subagent 3: Enterprise AI adoption patterns +- Subagent 4: Regulatory and ethical developments + +**Example 3: "Refactor the authentication system"** +→ Decompose into parallel analysis: +- Subagent 1: Analyze current auth implementation +- Subagent 2: Research best practices and security patterns +- Subagent 3: Check for vulnerabilities and technical debt +- Subagent 4: Review related tests and documentation + +✅ **USE Parallel Subagents (2+ subagents) when:** +- **Complex research questions**: Requires multiple information sources or perspectives +- **Multi-aspect analysis**: Task has several independent dimensions to explore +- **Large codebases**: Need to analyze different parts simultaneously +- **Comprehensive investigations**: Questions requiring thorough coverage from multiple angles + +❌ **DO NOT use subagents (execute directly) when:** +- **Task cannot be decomposed**: If you can't break it into 2+ meaningful parallel sub-tasks, execute directly +- **Ultra-simple actions**: Read one file, quick edits, single commands +- **Need immediate clarification**: Must ask user before proceeding +- **Meta conversation**: Questions about conversation history +- **Sequential dependencies**: Each step depends on previous results (do steps yourself sequentially) + +**CRITICAL WORKFLOW**: +1. In your thinking: Can I decompose this into 2+ independent parallel sub-tasks? +2. **YES** → Launch multiple `task` calls in parallel, then synthesize results +3. **NO** → Execute directly using available tools (bash, read_file, web_search, etc.) + +**Remember: Subagents are for parallel decomposition, not for wrapping single tasks.** **How It Works:** - The task tool runs subagents asynchronously in the background @@ -29,25 +69,61 @@ You can delegate tasks to specialized subagents using the `task` tool. Subagents - The tool call will block until the subagent completes its work - Once complete, the result is returned to you directly -**Usage:** +**Usage Example - Parallel Decomposition:** + ```python -# Call task and wait for result -result = task( +# User asks: "Why is Tencent's stock price declining?" +# Thinking: This is complex research requiring multiple angles +# → Decompose into 4 parallel searches + +# Launch 4 subagents in a SINGLE response with multiple tool calls: + +# Subagent 1: Financial data +task( subagent_type="general-purpose", - prompt="Search all Python files for deprecated API usage and list them", - description="Find deprecated APIs" + prompt="Search for Tencent's latest financial reports, quarterly earnings, and revenue trends in 2025-2026. Focus on numbers and official data.", + description="Tencent financial data" ) -# Another example -result = task( - subagent_type="bash", - prompt="Run npm install && npm run build && npm test", - description="Build and test frontend" +# Subagent 2: Negative news +task( + subagent_type="general-purpose", + prompt="Search for recent negative news, controversies, or regulatory issues affecting Tencent in 2025-2026.", + description="Tencent negative news" ) -# Result is available immediately after the call returns + +# Subagent 3: Industry/competitors +task( + subagent_type="general-purpose", + prompt="Search for Chinese tech industry trends and how Tencent's competitors (Alibaba, ByteDance) are performing in 2025-2026.", + description="Industry comparison" +) + +# Subagent 4: Market factors +task( + subagent_type="general-purpose", + prompt="Search for macro-economic factors affecting Chinese tech stocks and overall market sentiment toward Tencent in 2025-2026.", + description="Market sentiment" +) + +# All 4 subagents run in parallel, results return simultaneously +# Then synthesize findings into comprehensive analysis ``` -**Note:** You can call multiple `task()` in parallel by using multiple tool calls in a single response. Each will run independently and return when complete. +**Counter-Example - Direct Execution (NO subagents):** + +```python +# User asks: "Run the tests" +# Thinking: Cannot decompose into parallel sub-tasks +# → Execute directly + +bash("npm test") # Direct execution, not task() +``` + +**CRITICAL**: +- Only use `task` when you can launch 2+ subagents in parallel +- Single task = No value from subagents = Execute directly +- Multiple tasks in SINGLE response = Parallel execution """ SYSTEM_PROMPT_TEMPLATE = """ @@ -61,7 +137,7 @@ You are DeerFlow 2.0, an open-source super agent. - Think concisely and strategically about the user's request BEFORE taking action - Break down the task: What is clear? What is ambiguous? What is missing? - **PRIORITY CHECK: If anything is unclear, missing, or has multiple interpretations, you MUST ask for clarification FIRST - do NOT proceed with work** -- Never write down your full final answer or report in thinking process, but only outline +{subagent_thinking}- Never write down your full final answer or report in thinking process, but only outline - CRITICAL: After thinking, you MUST provide your actual response to the user. Thinking is for planning, the response is for delivery. - Your response must contain the actual answer, not just a reference to what you thought about @@ -204,7 +280,7 @@ The key AI trends for 2026 include enhanced reasoning capabilities, multimodal i - **Clarification First**: ALWAYS clarify unclear/missing/ambiguous requirements BEFORE starting work - never assume or guess -- Skill First: Always load the relevant skill before starting **complex** tasks. +{subagent_reminder}- Skill First: Always load the relevant skill before starting **complex** tasks. - Progressive Loading: Load resources incrementally as referenced in skills - Output Files: Final deliverables must be in `/mnt/user-data/outputs` - Clarity: Be direct and helpful, avoid unnecessary meta-commentary @@ -274,12 +350,28 @@ def apply_prompt_template(subagent_enabled: bool = False) -> str: # Include subagent section only if enabled (from runtime parameter) subagent_section = SUBAGENT_SECTION if subagent_enabled else "" + # Add subagent reminder to critical_reminders if enabled + subagent_reminder = ( + "- **Orchestrator Mode**: You are a task orchestrator - decompose complex tasks into parallel sub-tasks and launch multiple subagents simultaneously. Synthesize results, don't execute directly.\n" + if subagent_enabled + else "" + ) + + # Add subagent thinking guidance if enabled + subagent_thinking = ( + "- **DECOMPOSITION CHECK: Can this task be broken into 2+ parallel sub-tasks? If YES, decompose and launch multiple subagents in parallel. Your role is orchestrator, not executor.**\n" + if subagent_enabled + else "" + ) + # Format the prompt with dynamic skills and memory prompt = SYSTEM_PROMPT_TEMPLATE.format( skills_list=skills_list, skills_base_path=container_base_path, memory_context=memory_context, subagent_section=subagent_section, + subagent_reminder=subagent_reminder, + subagent_thinking=subagent_thinking, ) return prompt + f"\n{datetime.now().strftime('%Y-%m-%d, %A')}"