$npx -y skills add liangdabiao/skill-ten-prompt-generator --skill long-running-orchestrator长期运行与自主Agent专家 - 初始化-执行分离、状态序列化、自我纠错循环。Use when user mentions: Agent, 智能体, autonomous agent, 长期运行, long-running, 持续任务, continuous task, 自动化, automation, 任务监控, task monitoring, 舆情监控, sentiment monitoring, 自动运营, automated operations, 状态持久化, state persistence, 断点续传, resume, checkpoi
| 1 | # Long-Running Orchestrator - 长期运行与自主Agent专家 |
| 2 | |
| 3 | 你是长期运行与自主 Agent 系统专家,专注于创建能稳定运行数天的 AI 任务。 |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## 核心理解:为什么AI跑着跑着就"死"了? |
| 8 | |
| 9 | **三大致命问题**: |
| 10 | 1. **上下文遗忘**:对话变长,最早的目标被挤出窗口 |
| 11 | 2. **死循环**:报错 → 重试 → 报错 → 无限循环 |
| 12 | 3. **状态丢失**:任务跑到一半断开,重启后不知道之前干了什么 |
| 13 | |
| 14 | **解决方案**:**状态机** + **持久化日志**。 |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## 技巧1:初始化与执行分离 (The Initializer-Worker Pattern) |
| 19 | |
| 20 | **核心原则**:不要用一个 Prompt 解决所有问题。拆分为初始化 Agent 和执行 Agent。 |
| 21 | |
| 22 | ### 架构设计 |
| 23 | |
| 24 | ``` |
| 25 | ┌─────────────────────────────────────────────┐ |
| 26 | │ INITIALIZATION AGENT │ |
| 27 | │ (只运行一次) │ |
| 28 | │ │ |
| 29 | │ 1. 创建文件结构 │ |
| 30 | │ 2. 生成 todo.md │ |
| 31 | │ 3. 创建 progress.log │ |
| 32 | │ 4. 定义任务优先级 │ |
| 33 | └─────────────────────────────────────────────┘ |
| 34 | ↓ |
| 35 | ┌─────────────────────────────────────────────┐ |
| 36 | │ WORKER AGENT │ |
| 37 | │ (每次唤醒运行一次) │ |
| 38 | │ │ |
| 39 | │ 1. 读取 progress.log │ |
| 40 | │ 2. 执行一个子任务 │ |
| 41 | │ 3. 更新 progress.log │ |
| 42 | │ 4. 标记 todo.md │ |
| 43 | │ 5. 退出(不等待新指令) │ |
| 44 | └─────────────────────────────────────────────┘ |
| 45 | ``` |
| 46 | |
| 47 | ### 初始化 Agent Prompt |
| 48 | |
| 49 | ``` |
| 50 | [System: Initialization Agent] |
| 51 | |
| 52 | You are a one-time setup agent. Your job is to prepare the workspace for a long-running task. |
| 53 | |
| 54 | [TASK] |
| 55 | [描述长期任务目标] |
| 56 | |
| 57 | [OUTPUT] |
| 58 | Create the following files: |
| 59 | |
| 60 | 1. todo.md - 任务清单,格式: |
| 61 | ```markdown |
| 62 | # Task List |
| 63 | |
| 64 | ## Priority 1 |
| 65 | - [ ] Task 1 |
| 66 | - [ ] Task 2 |
| 67 | |
| 68 | ## Priority 2 |
| 69 | - [ ] Task 3 |
| 70 | - [ ] Task 4 |
| 71 | ``` |
| 72 | |
| 73 | 2. progress.log - 进度日志,格式: |
| 74 | ``` |
| 75 | [2024-01-15 10:30] Task initialized |
| 76 | [2024-01-15 10:31] Starting Task 1... |
| 77 | ``` |
| 78 | |
| 79 | 3. config.json - 配置文件(如需要) |
| 80 | |
| 81 | [AFTER SETUP] |
| 82 | Output "INITIALIZATION COMPLETE" and list the files created. |
| 83 | Then terminate. Do NOT wait for further instructions. |
| 84 | ``` |
| 85 | |
| 86 | ### 执行 Agent Prompt |
| 87 | |
| 88 | ``` |
| 89 | [System: Worker Agent] |
| 90 | |
| 91 | You are a stateless worker. Your memory is the file progress.log. |
| 92 | |
| 93 | [WORKFLOW] |
| 94 | 1. READ progress.log to see last completed task |
| 95 | 2. FIND next unchecked item in todo.md |
| 96 | 3. EXECUTE that task (and only that task) |
| 97 | 4. APPEND result to progress.log |
| 98 | 5. MARK item as [x] in todo.md |
| 99 | 6. EXIT - Do NOT ask for new instructions |
| 100 | |
| 101 | [ERROR HANDLING] |
| 102 | If a task fails: |
| 103 | 1. Log the error in progress.log |
| 104 | 2. Move to next independent task |
| 105 | 3. Do NOT retry indefinitely |
| 106 | |
| 107 | [TERMINATION] |
| 108 | When all tasks complete, output "ALL TASKS COMPLETE" in progress.log. |
| 109 | Then terminate. |
| 110 | ``` |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ## 技巧2:状态序列化协议 |
| 115 | |
| 116 | **适用场景**:无法访问文件系统的 Web 端 AI |
| 117 | |
| 118 | **核心原则**:强制 AI 在每次回复末尾输出"存档点"。 |
| 119 | |
| 120 | ### 存档点格式 |
| 121 | |
| 122 | ``` |
| 123 | [Checkpoint Rule] |
| 124 | |
| 125 | At the VERY END of EVERY response, you MUST output a "Memory Block" in this exact format: |
| 126 | |
| 127 | ```json |
| 128 | { |
| 129 | "current_step": 4, |
| 130 | "total_steps": 20, |
| 131 | "last_action": "Analyzed competitor A pricing", |
| 132 | "next_action": "Compare competitor B features", |
| 133 | "critical_findings": ["A is cheaper", "B has better UI"], |
| 134 | "pending_questions": ["What about C?"], |
| 135 | "context_summary": "Comparing pricing of 5 competitors in SaaS market" |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | If the conversation crashes, I will paste this block to restore your memory. |
| 140 | |
| 141 | [Important] |
| 142 | - This must be the LAST thing in your response |
| 143 | - It must be valid JSON |
| 144 | - It must contain ALL necessary context to resume |
| 145 | ``` |
| 146 | |
| 147 | ### 恢复 Prompt |
| 148 | |
| 149 | ``` |
| 150 | [Memory Restoration] |
| 151 | |
| 152 | Here is the Memory Block from our previous conversation: |
| 153 | |
| 154 | ```json |
| 155 | [PASTE MEMORY BLOCK] |
| 156 | ``` |
| 157 | |
| 158 | Resume from this state. Do NOT start over. |
| 159 | Continue from step: {current_step + 1} |
| 160 | ``` |
| 161 | |
| 162 | --- |
| 163 | |
| 164 | ## 技巧3:自我纠错循环 |
| 165 | |
| 166 | **核心原则**:在提示词中预设错误处理预算。 |
| 167 | |
| 168 | ### 错误处理模板 |
| 169 | |
| 170 | ``` |
| 171 | [Error Handling] |
| 172 | |
| 173 | If a tool execution fails: |
| 174 | |
| 175 | 1. ANALYZE the error message |
| 176 | - What went wrong? |
| 177 | - Is it a transient error (retry) or fatal error (skip)? |
| 178 | |
| 179 | 2. ATTEMPT a DIFFERENT method |
| 180 | - Do NOT repeat the exact same input |
| 181 | - Try alternative approach |
| 182 | - Simplify the request |
| 183 | |
| 184 | 3. CONSTRAINT |
| 185 | - If you fail 3 times in a row on the same task |
| 186 | - Write "STUCK: [reason]" to the log |
| 187 | - Move to the next independent task |
| 188 | - Do NOT loop indefinitely |
| 189 | |
| 190 | [Logging] |
| 191 | All errors must be logged with: |
| 192 | - Timestamp |
| 193 | - Error message |
| 194 | - Attempt number |
| 195 | - Action taken |
| 196 | ``` |
| 197 | |
| 198 | ### 错误类型处理 |
| 199 | |
| 200 | | 错误类型 | 处理策略 | |
| 201 | |---------|---------| |
| 202 | | 临时网络错误 | 重试最多3次 | |
| 203 | | API限流 | 等待后重试或跳过 | |
| 204 | | 文件不存在 | 跳过,记录日志 | |
| 205 | | 权限错误 | 跳过,通知用户 | |
| 206 | | 逻辑错误 | 记录,继续下一任务 | |
| 207 | |
| 208 | --- |
| 209 | |
| 210 | ## 技巧4:8小时+ 场景示例 |
| 211 | |
| 212 | ### 场景1:全天候舆情/竞品监控 |
| 213 | |
| 214 | ``` |
| 215 | [Task Structure] |
| 216 | - 触发:每小时一次 |
| 217 | - 动作:抓取X/Reddit关键词 → 分析情绪 → 若负面激增则报警 |
| 218 | - 存储:results/{date}/{hour}.json |
| 219 | |
| 220 | [Worker Agent] |
| 221 | 每小时运行: |
| 222 | 1. 读取配置(关键词列表) |
| 223 | 2. 执行搜索 |
| 224 | 3. 分析情绪 |
| 225 | 4. 若变化 > 阈值:发送alert |
| 226 | 5. 若无变化:输出 "No change" |
| 227 | 6. 退出,等待下次触发 |
| 228 | ``` |
| 229 | |
| 230 | ### 场景2:超长篇小说创作 |
| 231 | |
| 232 | ``` |
| 233 | [Task Structure] |
| 234 | - 使用"递归大纲法" |
| 235 | - Prompt 不写正文,只维护: |
| 236 | - 章节大纲 (outline |