Background Task Runner
Run long tasks in the background so the user can ask other questions simultaneously.
When to Use
Use this when the user gives you a long task (预计耗时 > 30秒) and you want to stay responsive.
Trigger phrases:
- "后台跑" / "先派后台"
- "不要阻塞" / "别卡住"
- "先做别的" / "边跑边聊"
- "background" / "异步"
- 任何需要 > 30 秒的任务,即使没明确说后台
Do NOT use for:
- Very quick tasks (< 10秒, 如查余额、删文件)
- Tasks where the user needs the result immediately
- Interactive tasks that need mid-step user input
Workflow
Step 1: Spawn the background subagent
spawn_subagent(
task="详细的任务描述,包含具体步骤...",
background=True
)
The task parameter should include:
- The exact goal
- Every step with commands/paths
- What to do on success / failure
- What to do at the end (e.g. send file to user, summarize results)
It returns task_id immediately.
Step 2: Store task_id in MEMORY.md
Update MEMORY.md with the pending task info:
## 后台任务
- task_id: xxx
- description: 周报生成 (2026.06.29-2026.07.05)
- started_at: 2026-07-02 14:30
- status: running
Step 3: Reply immediately
Tell the user the task is running in the background and they can ask other things.
Step 4: On each subsequent user message
Before handling the new request, check the pending task:
check_agent_task(task_id='xxx')
If the task is done, summarize the results for the user and clear the task from MEMORY.md.
If still running, handle the new request normally.
Example
User: "做这周监管周报" Agent: (spawns background task, stores task_id, replies immediately) "已经派到后台跑了!你先问别的,搞完我告诉你"
User: "查下东华能源" Agent: (checks task → still running, responds normally) "东华能源(002221),深交所7月1日公开谴责..."
User: "好了没?" Agent: (checks task → done, summarizes) "好了!共 1 条案例,报告已生成,发给你"
Important Notes
spawn_subagentwithbackground=Truereturns immediately (0.1s)- The subagent gets a fresh empty session by default
fork=Trueinherits current session state (use sparingly)- Subagent can use all the same tools (browser, shell, send_file, etc.)
- Each task_id is one-shot — once done, it cannot be resumed
- If the agent restarts or session ends, pending task_id is lost
- Store only ONE active task_id at a time in MEMORY.md
⚠️ 重要教训:浏览器隔离
问题: 子任务如果使用 --cdp 连接当前浏览器,会跟用户抢同一个浏览器窗口。子任务在扫36局时,用户会看到浏览器在 CSRC 各局页面之间乱跳,影响正常使用。
扩展:不同频道也不能共享浏览器 — 微信和飞书的任务如果共用一个浏览器,一个频道在扫36局,另一个频道的用户也会看到浏览器在跳,互相干扰。
解决方案:
- 后台任务启动独立 Playwright 浏览器(
playwright.sync_api直接 launch),不连接现有 CDP - 不同频道的任务各用各的浏览器实例,互不干扰
- 不可使用
--cdp参数连接当前共享的浏览器




