Cursor Agent CLI Dispatch
Orchestrate cursor agent CLI with concurrency control, timeout recovery, and retry.
Hard Constraints (from testing)
| Constraint | Value | Source |
|---|
| PTY required | INLINECODE1 mandatory | No output without PTY |
| Max safe concurrency |
6 parallel tasks | Tested 8 OK, 6 is safe margin |
| Plan timeout (single file) | 60-120s | Tested on .gd files |
| Plan timeout (multi-file) | >180s, WILL timeout | Don't attempt |
| Execute timeout | 60-90s typical | Up to 180s for complex |
|
--output-format json | BROKEN in PTY | Use text format only |
| Empty output = failure | Check output size > 20 bytes | Retry if empty |
Command Template
CODEBLOCK0
| Mode | Flags | Timeout | Use |
|---|
| Plan | INLINECODE3 | 180s | Read-only analysis |
| Execute |
--yolo | 180s | Auto-approve changes |
| Ask |
--mode ask | 120s | Q&A, no changes |
Concurrency Control Protocol
State Tracking
Before launching cursor tasks, track them via process(action=list). Each running exec session with cursor agent counts toward the limit.
Launch Rules
CODEBLOCK1
Concurrency Limits by Task Type
| Scenario | Max Concurrent | Why |
|---|
| Plan-only (read) | 6 | No file conflicts |
| Execute (write) |
3 | Avoid git/file conflicts |
| Mixed plan+execute | 4 | Balance safety and speed |
| Same-file tasks | 1 (serial only) | NEVER parallel on same file |
Timeout & Recovery Protocol
Detection
A task is stuck when:
- -
process(action=poll, timeout=T) returns with process still running after timeout - Exit code 143 (SIGTERM) or 137 (SIGKILL) = was killed by timeout
- Output is empty (< 20 bytes) after completion = silent failure
Recovery Steps
CODEBLOCK2
Simplification Strategy
When a task times out, simplify before retrying:
| Original | Simplified |
|---|
| "Scan all dungeon scripts for bugs" | "Scan dungeon_manager.gd for null refs" |
| "Fix 5 issues in this file" |
"Fix issue #1 and #2 only" |
| "Analyze architecture of combat system" | "List all functions in arpg_monster.gd" |
Cleanup Protocol
When to Clean Up
- - Before starting a new batch of tasks
- After any task failure
- On session start (check for orphans)
How to Clean Up
CODEBLOCK3
Orphan Detection
Cursor agent processes that outlive their exec session become orphans:
CODEBLOCK4
Four Dispatch Chains
Chain A: Direct Execute
For simple tasks (1-3 files, clear logic). Single exec call.
CODEBLOCK5
Chain B: Plan → Review → Execute ⭐
For complex tasks. Most reliable chain.
CODEBLOCK6
Chain C: Parallel Batch
For independent tasks across different files. Use background exec.
CODEBLOCK7
Rules:
- - Max 4 parallel execute tasks (6 for plan-only)
- NEVER parallel on same file
- Check for git conflicts after all complete
- If any fails: kill remaining → fix conflict → retry failed ones
Chain D: Plan Global → Serial Execute
For large refactors with dependencies.
CODEBLOCK8
Prompt Engineering for Cursor Agent
Plan Prompts (what works)
Good: "检查 scripts/dungeon/dungeon_manager.gd 中所有可能返回 null 的函数调用,以及调用方是否有 null 检查"
Good: "列出 中所有 func 的名字"
Bad: "扫描所有文件找出所有问题" (too broad, will timeout)
Execute Prompts (what works)
Good: Include exact line numbers + expected code change + verify command + git commit
Bad: Vague "fix the bugs" without specifics
Template
CODEBLOCK9
Model Selection
| Task | Model | Timeout |
|---|
| Default | INLINECODE8 | — |
| Simple edits |
auto | 120s |
| Complex analysis |
auto +
--plan | 180s |
| Code review |
auto +
--mode ask | 120s |
INLINECODE14 routes to best available model (typically opus-4.6-thinking). Don't override unless needed.
Session Resume
CODEBLOCK10
Timing Benchmarks
v2.6.12 (tested 2026-03-06 evening, cursor 2.6.12)
| Task Type | Time | Concurrency Tested |
|---|
| List functions (small file <50 lines) | 8-14s | 6/6 parallel ✅ |
| List functions (medium file ~600 lines) |
17s | single ✅ |
| List functions (large file ~1000 lines) | 40s | single ✅ |
| Simple execute (add comment, 1 file) | 16-19s | 3/3 parallel ✅ |
| Ask mode (simple Q&A) | 8s | single ✅ |
| Complex analysis (null-check scan, 660 lines) | 84s | single ✅ |
| Long complex prompt (3+ concerns, multi-file ref) | >180s | TIMEOUT ⚠️ |
| Execute with broad prompt (3 bugs, multi-concern) | >180s | TIMEOUT ⚠️ |
Key Findings (v2.6.12)
- - Simple tasks (list/comment/ask) are MUCH faster than v2026.02.27 (8-40s vs 55-120s)
- Complex analysis still takes 60-120s — similar to old version
- Broad prompts (3+ separate concerns) STILL timeout — must split into 1-concern-per-task
- Silent failures (empty output) reduced but still occur on timeout
- 6 parallel plan = all pass in ~53s wall clock (individual 8-14s each)
- 3 parallel execute = all pass in ~25s wall clock (individual 16-19s each)
Failure Mode: Prompt Complexity (NOT concurrency)
Timeouts correlate with prompt complexity, not concurrency:
- - 6 simple tasks parallel → all pass in <20s each
- 1 complex task with 3+ concerns → timeout at 180s
- Solution: ALWAYS split multi-concern prompts. One concern per cursor task.
Previous version (v2026.02.27, tested 2026-03-06 morning)
| Task Type | Time | Concurrency Tested |
|---|
| List functions (1 file) | 15s | 8/8 passed |
| Null-check scan (1 file) |
80-120s | 4/4 parallel ✅ |
| Execute fix (1 file, 1-4 issues) | 55-90s | 3/3 parallel ✅ |
| Multi-file analysis | >180s | TIMEOUT |
Validated Workflows
Workflow 1: Parallel Plan → Review → Parallel Execute (2026-03-06)
CODEBLOCK11
Workflow 2: Skip Plan, Direct Execute (2026-03-06 evening)
When you already know exactly what to change, skip Plan and go straight to Execute:
CODEBLOCK12
Key insight: Plan can always be parallelized (read-only). Execute can be parallelized
only when tasks touch different files. Review is always serial (agent decides).
Critical: Keep prompts focused — 1 file, 1 concern, specific line numbers.
For model list and CLI parameters, see references/models-and-params.md.
Cursor Agent CLI 调度
通过并发控制、超时恢复和重试机制来编排 cursor agent CLI。
硬性约束(来自测试)
| 约束项 | 值 | 来源 |
|---|
| 需要PTY | 必须设置 pty: true | 无PTY则无输出 |
| 最大安全并发数 |
6个并行任务 | 测试8个OK,6个为安全余量 |
| 计划超时(单文件) | 60-120秒 | 在.gd文件上测试 |
| 计划超时(多文件) | >180秒,必定超时 | 请勿尝试 |
| 执行超时 | 通常60-90秒 | 复杂情况可达180秒 |
| --output-format json | 在PTY中已损坏 | 仅使用文本格式 |
| 空输出 = 失败 | 检查输出大小 > 20字节 | 空则重试 |
命令模板
exec(
command: cd <项目> && cursor agent -p --trust [FLAGS] --model auto --workspace . <提示词>,
pty: true,
timeout: <秒数>
)
| 模式 | 标志 | 超时 | 用途 |
|---|
| 计划 | --plan | 180秒 | 只读分析 |
| 执行 |
--yolo | 180秒 | 自动批准更改 |
| 询问 | --mode ask | 120秒 | 问答,无更改 |
并发控制协议
状态追踪
在启动cursor任务前,通过 process(action=list) 追踪它们。每个正在运行的cursor agent exec会话都计入限制。
启动规则
- 1. 检查:process(action=list) → 统计命令中包含cursor agent的会话数
- 如果计数 >= MAX_CONCURRENT(默认4,最大6):等待或排队
- 启动:exec(command=..., pty=true, timeout=T, background=true)
- 记录:sessionId + taskId + startTime
- 轮询:process(action=poll, sessionId=X, timeout=T*1000)
按任务类型的并发限制
| 场景 | 最大并发数 | 原因 |
|---|
| 仅计划(只读) | 6 | 无文件冲突 |
| 执行(写入) |
3 | 避免git/文件冲突 |
| 混合计划+执行 | 4 | 平衡安全性和速度 |
| 同文件任务 | 1(仅串行) | 绝不对同一文件并行操作 |
超时与恢复协议
检测
当以下情况发生时,任务被判定为卡住:
- - process(action=poll, timeout=T) 返回时进程仍在运行且已超时
- 退出码143(SIGTERM)或137(SIGKILL)= 被超时杀死
- 完成后输出为空(< 20字节)= 静默失败
恢复步骤
- 1. 检测:轮询返回仍在运行或退出码143/137或输出为空
- 终止:process(action=kill, sessionId=X)
- 等待:3秒冷却(避免API速率限制)
- 简化:缩小提示词范围(更少的文件,更简单的问题)
- 重试:使用简化后的提示词重新启动,相同或更长的超时时间
- 最大重试次数:总共2次。2次失败后 → 报告给用户
简化策略
当任务超时时,在重试前进行简化:
| 原始 | 简化后 |
|---|
| 扫描所有地牢脚本查找bug | 扫描dungeon_manager.gd查找空引用 |
| 修复此文件中的5个问题 |
仅修复问题#1和#2 |
| 分析战斗系统的架构 | 列出arpg_monster.gd中的所有函数 |
清理协议
何时清理
- - 开始新一批任务之前
- 任何任务失败后
- 会话启动时(检查孤儿进程)
如何清理
- 1. process(action=list) → 查找所有与cursor相关的会话
- 对每个会话:检查是否仍在运行
- 如果运行超过10分钟:process(action=kill, sessionId=X)
- 验证:ps aux | grep cursor agent -p | grep -v grep
- 核选项:pkill -f cursor agent -p(杀死所有cursor CLI代理)
孤儿进程检测
比exec会话存活时间更长的cursor agent进程会成为孤儿进程:
bash
查找孤儿cursor agent进程
ps aux | grep cursor agent -p | grep -v grep
杀死特定孤儿进程
kill
四种调度链
链A:直接执行
适用于简单任务(1-3个文件,逻辑清晰)。单次exec调用。
exec(
command: cd <项目> && cursor agent -p --trust --yolo --model auto --workspace . <任务>,
pty: true, timeout: 180
)
→ 轮询 → 验证输出不为空 → 完成
链B:计划 → 审查 → 执行 ⭐
适用于复杂任务。最可靠的链。
步骤1 — 计划(pty:true, timeout:180):
检查 <文件> 中会导致运行时崩溃的问题。重点:<具体关注点>。只列会崩溃的。
步骤2 — 审查(代理决定):
解析输出 → 评分🔴🟠🟡🟢 → 选择本轮修复项
步骤3 — 执行(pty:true, timeout:180):
修复 <文件> 中 N 个问题:[粘贴选中的项,包含行号和修复代码]。
修复后运行:<验证命令>。确认无错误后 git commit。
步骤4 — 验证:
运行无头构建/测试以确认
链C:并行批处理
适用于不同文件上的独立任务。使用后台exec。
启动(遵守并发限制)
exec(cmd=cursor agent ... 任务1, pty:true, background:true) → session1
exec(cmd=cursor agent ... 任务2, pty:true, background:true) → session2
exec(cmd=cursor agent ... 任务3, pty:true, background:true) → session3
监控
process(action=poll, sessionId=session1, timeout=180000)
process(action=poll, sessionId=session2, timeout=180000)
process(action=poll, sessionId=session3, timeout=180000)
全部验证
运行无头构建/测试
规则:
- - 最多4个并行执行任务(仅计划时为6个)
- 绝不对同一文件并行操作
- 全部完成后检查git冲突
- 如果任何任务失败:终止剩余任务 → 解决冲突 → 重试失败的任务
链D:全局计划 → 串行执行
适用于具有依赖关系的大型重构。
- 1. 对每个文件进行计划(可并行,最多6个)
- 收集所有发现 → 按依赖关系排序
- 按依赖顺序执行(串行)
- 每次执行后验证,再进行下一次
- 如果验证失败:停止,修复,重新计划剩余部分
Cursor Agent的提示词工程
计划提示词(有效的)
好的:检查 scripts/dungeon/dungeon_manager.gd 中所有可能返回 null 的函数调用,以及调用方是否有 null 检查
好的:列出 <文件> 中所有 func 的名字
不好的:扫描所有文件找出所有问题(太宽泛,会超时)
执行提示词(有效的)
好的:包含精确的行号 + 预期的代码更改 + 验证命令 + git commit
不好的:模糊的修复bug,没有具体说明
模板
计划:检查 <文件> 中 <具体关注点>。只列会崩溃/卡死的问题,给出行号和修复方案。
执行:修复 <文件> 中 N 个问题:
- 1. 第 X 行:<当前代码> → <修复后代码>
- 第 Y 行:<当前代码> → <修复后代码>
修复后运行:<验证命令>
确认无错误后 git commit -m <消息>。
模型选择
auto | 120秒 |
| 复杂分析 | auto + --plan | 180秒 |
| 代码审查 | auto + --mode ask | 120秒 |
auto 路由到最佳可用模型(通常为opus-4.6-thinking)。除非必要,否则不要覆盖。
会话恢复
bash
cursor agent