recursive-handoff

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Recursive Handoff

递归式Handoff

Run the same prompt repeatedly with fresh context until a finish condition is met.

在全新的上下文环境中重复执行相同的提示词,直至满足结束条件。

When to Use

使用场景

  • Long-running tasks that would exhaust context
  • Repetitive operations (process items one by one, migrate in batches)
  • Polling/waiting for external state changes
  • Any task where "keep doing X until Y" applies

  • 会耗尽上下文的长时间运行任务
  • 重复性操作(逐个处理项目、批量迁移)
  • 轮询/等待外部状态变更
  • 任何符合“持续执行X直到Y”逻辑的任务

Required Parameters

必填参数

1. Finish Condition

1. 结束条件

You MUST get a finish condition from the user before starting.
The condition must be verifiable programmatically (file check, command output, grep result, API response, etc.)
Examples:
  • "All files in
    src/legacy/
    have been processed"
  • "No more TODO comments exist in the codebase"
  • "The API returns status 200"
  • "The queue is empty"
开始前必须从用户处获取结束条件。
该条件必须可通过编程方式验证(文件检查、命令输出、grep结果、API响应等)
示例:
  • src/legacy/
    目录下的所有文件已处理完成”
  • “代码库中不存在任何TODO注释”
  • “API返回状态码200”
  • “队列已为空”

2. Hard Limit (default: 20)

2. 硬限制(默认值:20)

Maximum iterations before stopping. Prevents runaway loops.

停止前的最大迭代次数,用于防止无限循环。

How It Works

工作原理

┌─────────────────────────────────────────┐
│  Thread 1: Check condition → not met    │
│            Do work → handoff            │
└─────────────────┬───────────────────────┘
┌─────────────────▼───────────────────────┐
│  Thread 2: Check condition → not met    │
│            Do work → handoff            │
└─────────────────┬───────────────────────┘
┌─────────────────▼───────────────────────┐
│  Thread 3: Check condition → MET        │
│            STOP (no handoff)            │
└─────────────────────────────────────────┘
Each iteration:
  1. Check iteration count - stop if limit reached
  2. Check finish condition
  3. If met → stop, report completion
  4. If not met → do one unit of work → handoff to continue

┌─────────────────────────────────────────┐
│  Thread 1: Check condition → not met    │
│            Do work → handoff            │
└─────────────────┬───────────────────────┘
┌─────────────────▼───────────────────────┐
│  Thread 2: Check condition → not met    │
│            Do work → handoff            │
└─────────────────┬───────────────────────┘
┌─────────────────▼───────────────────────┐
│  Thread 3: Check condition → MET        │
│            STOP (no handoff)            │
└─────────────────────────────────────────┘
每次迭代流程:
  1. 检查迭代次数 - 若达到限制则停止
  2. 检查结束条件
  3. 若满足→停止,报告完成状态
  4. 若不满足→执行一个单元的任务→通过handoff继续循环

The Handoff Prompt Template

Handoff提示词模板

RECURSIVE TASK - Iteration [N] of [LIMIT]
RECURSIVE TASK - Iteration [N] of [LIMIT]

Finish Condition

Finish Condition

[CONDITION]
[CONDITION]

How to Check

How to Check

[COMMAND OR METHOD TO VERIFY CONDITION]
[COMMAND OR METHOD TO VERIFY CONDITION]

If Limit Reached or Condition Met

If Limit Reached or Condition Met

Stop. Report: "✅ Complete: [summary]" or "⚠️ Limit reached after [N] iterations" Do NOT handoff.
Stop. Report: "✅ Complete: [summary]" or "⚠️ Limit reached after [N] iterations" Do NOT handoff.

If Condition NOT Met

If Condition NOT Met

Task

Task

[WHAT TO DO THIS ITERATION]
[WHAT TO DO THIS ITERATION]

After Completing Work

After Completing Work

Handoff with follow: true, incrementing iteration count:
[PASTE THIS PROMPT WITH N+1]

---
Handoff with follow: true, incrementing iteration count:
[PASTE THIS PROMPT WITH N+1]

---

Example: Generic Processing Loop

示例:通用处理循环

User: "Process all items in the queue"
Finish condition: Queue is empty
Handoff prompt:
RECURSIVE TASK - Iteration 1 of 20
用户:“处理队列中的所有项目”
**结束条件:**队列为空
Handoff提示词:
RECURSIVE TASK - Iteration 1 of 20

Finish Condition

Finish Condition

Queue is empty
Queue is empty

How to Check

How to Check

[command to check queue length] If result is 0, condition is met.
[command to check queue length] If result is 0, condition is met.

If Limit Reached or Condition Met

If Limit Reached or Condition Met

Stop. Report completion status. Do NOT handoff.
Stop. Report completion status. Do NOT handoff.

If Condition NOT Met

If Condition NOT Met

Task

Task

  1. Get next item from queue
  2. Process it
  3. Remove from queue
  1. Get next item from queue
  2. Process it
  3. Remove from queue

After Completing Work

After Completing Work

Handoff with follow: true, goal:
[THIS PROMPT WITH ITERATION 2 of 20]

---
Handoff with follow: true, goal:
[THIS PROMPT WITH ITERATION 2 of 20]

---

Starting the Loop

启动循环

Once setup is complete, invoke handoff with
follow: true
:
handoff
  goal: [THE FULL HANDOFF PROMPT]
  follow: true
The loop continues autonomously until finish condition is met or limit is reached.
设置完成后,调用handoff并传入
follow: true
handoff
  goal: [THE FULL HANDOFF PROMPT]
  follow: true
循环将自主运行,直至满足结束条件或达到迭代限制。