Batch File Processor
Process large numbers of files in parallel using sub-agents, avoiding main agent context overflow.
Workflow
1. List files
CODEBLOCK0
2. Group
Split into batches of 2-4 files each (3 is optimal).
3. Dispatch sub-agents
One sub-agent per batch. Task template:
CODEBLOCK1
Key parameters:
- -
mode: "run" (one-shot task) - INLINECODE1 : 120 (increase to 180 for large files)
- INLINECODE2 : descriptive label, e.g. INLINECODE3
4. Collect results
Sub-agents push results on completion. Use sessions_yield to wait and collect incrementally.
5. Compile output
Once all results are in, the main agent compiles the final deliverable (index file, report, etc.).
Rules
- - 2-4 files per sub-agent — never let one sub-agent process an entire directory sequentially
- Read full file content — no head/tail truncation; partial reads produce incomplete summaries
- Standardize output format — JSON makes it easy for the main agent to parse and merge
- One spawn per turn — system limitation; use multiple spawn + yield cycles
Anti-patterns
| Mistake | Consequence |
|---|
| INLINECODE5 to skim file headers | Poor summary quality, key information missed |
| One sub-agent processes entire directory |
Context overflow, timeout failure |
| Main agent reads all files sequentially | Context window exhausted, later files unreadable |
| One sub-agent per large directory | Large directories timeout, small ones waste capacity |
Benchmarks
70 files → 25 sub-agents (3 files each) → parallel execution → completed in 5 minutes → high accuracy summaries
Task Template Variants
File summarization (default)
CODEBLOCK2
Information extraction
CODEBLOCK3
Content classification
CODEBLOCK4
Code analysis
CODEBLOCK5
批量文件处理器
使用子代理并行处理大量文件,避免主代理上下文溢出。
工作流程
1. 列出文件
bash
find <目录> -type f -name *.md | sort
2. 分组
每批拆分为2-4个文件(3个为最佳)。
3. 调度子代理
每批一个子代理。任务模板:
完整读取以下文件,并为每个文件生成简短摘要(50字以内)。
- 1. /path/to/file1.md
- /path/to/file2.md
- /path/to/file3.md
仅返回JSON数组:
[{file: relative/path/file1.md, summary: ...},...]
关键参数:
- - mode:run(一次性任务)
- runTimeoutSeconds:120(大文件增至180)
- label:描述性标签,例如 idx-project-batch1
4. 收集结果
子代理完成后推送结果。使用 sessions_yield 等待并增量收集。
5. 编译输出
所有结果收集完毕后,主代理编译最终交付物(索引文件、报告等)。
规则
- - 每个子代理处理2-4个文件 — 绝不让单个子代理顺序处理整个目录
- 读取完整文件内容 — 不截断头尾;部分读取会产生不完整摘要
- 标准化输出格式 — JSON便于主代理解析和合并
- 每轮仅生成一个代理 — 系统限制;使用多次生成+产出循环
反模式
| 错误 | 后果 |
|---|
| 使用head -20浏览文件头部 | 摘要质量差,遗漏关键信息 |
| 单个子代理处理整个目录 |
上下文溢出,超时失败 |
| 主代理顺序读取所有文件 | 上下文窗口耗尽,后续文件无法读取 |
| 每个大目录分配一个子代理 | 大目录超时,小目录浪费容量 |
基准测试
70个文件 → 25个子代理(每个3个文件)→ 并行执行 → 5分钟内完成 → 高精度摘要
任务模板变体
文件摘要(默认)
为每个文件生成简短摘要(50字以内)。
信息提取
从每个文件中提取以下字段:项目名称、预算、关键联系人、风险。
返回JSON:[{file: ..., project: ..., budget: ..., contacts: [...], risks: [...]}]
内容分类
检查每个文件是否包含以下主题:安全、合规、迁移。
返回JSON:[{file: ..., hassecurity: true/false, hascompliance: true/false, has_migration: true/false}]
代码分析
分析每个源文件:统计行数、列出导入/依赖项、识别主要函数。
返回JSON:[{file: ..., lines: N, imports: [...], main_functions: [...]}]