pi-goal-x 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,432 @@
1
+ # pi-goal Agent 流程设计
2
+
3
+ 这份文档用于理解当前 `pi-goal` 项目的 agent 流程设计:用户如何提出目标,执行 agent 如何工作,独立 auditor 如何审计,runtime 如何维护状态、ledger、UI 与自动续跑。
4
+
5
+ ## 1. 核心心智模型
6
+
7
+ `pi-goal` 不是另一个通用 agent,而是一个 pi extension。它给主 coding agent 加上一层“长期目标运行时”:目标需要被用户明确确认,执行过程有生命周期,完成时必须经过独立审计。
8
+
9
+ 当前系统里有四个角色:
10
+
11
+ | 角色 | 职责 |
12
+ |---|---|
13
+ | 用户 | 拥有意图。启动目标、确认草案、选择 focus、恢复/清理/中止目标。 |
14
+ | 执行 agent | 在已确认的 focused goal 上工作。可以暂停、终止、请求完成。 |
15
+ | 审计 agent | 一个独立的 in-memory pi agent session,负责检查完成声明是否真的满足目标。 |
16
+ | `pi-goal` runtime | 维护目标状态、工具可见性、prompt、ledger、UI widget 与自动续跑。 |
17
+
18
+ 核心原则是:
19
+
20
+ > 用户拥有意图;执行 agent 完成工作;审计 agent 独立验证;runtime 负责协调与记录。
21
+
22
+ ## 2. 总体流程
23
+
24
+ ```text
25
+ 用户命令
26
+ -> pi-goal command handler
27
+ -> 进入轻量 confirmation 或更新 focus/lifecycle 状态
28
+ -> runtime 重新计算 prompt 与 tool surface
29
+ -> 执行 agent 按 focused goal 工作
30
+ -> tool call / turn event 更新 accounting 与 ledger
31
+ -> 执行 agent 调用 update_goal 请求完成
32
+ -> 独立 auditor agent 检查完成声明
33
+ -> 只有 auditor approval 才归档为 complete
34
+ ```
35
+
36
+ 一次成功运行大致是:
37
+
38
+ ```text
39
+ /goals 或 /sisyphus
40
+ -> 发送轻量 intent/confirmation 指令
41
+ -> agent 询问必要澄清问题,或在请求足够明确时直接 proposal
42
+ -> agent 调用 propose_goal_draft
43
+ -> 用户确认
44
+ -> 写入 active goal 文件并设置 focus
45
+ -> agent 跨一个或多个 turn 执行工作
46
+ -> agent 调用 update_goal(status="complete")
47
+ -> 对话中出现 Goal audit started
48
+ -> auditor session 检查真实产物
49
+ -> 对话中出现 Goal audit approved
50
+ -> Goal complete,并归档目标
51
+ ```
52
+
53
+ ## 3. 主要状态容器
54
+
55
+ `extensions/goal.ts` 里维护一个项目级 open goal pool 和一个 session-local focus:
56
+
57
+ ```ts
58
+ let goalsById = new Map<string, GoalRecord>();
59
+ let focusedGoalId: string | null = null;
60
+ ```
61
+
62
+ 含义:
63
+
64
+ - `goalsById` 从 `.pi/goals/active_goal_*.md` 文件恢复。
65
+ - `focusedGoalId` 从当前 session branch 的 `pi-goal-focus` entry 恢复。
66
+ - focus 不写入 goal markdown,因此不同 pi session branch 可以 focus 不同目标。
67
+ - `state.goal` 是 focused goal 的便捷 getter/setter。
68
+
69
+ 还有一些运行时瞬态状态:
70
+
71
+ | 状态 | 作用 |
72
+ |---|---|
73
+ | `confirmationIntent` | 当前 `/goals` 或 `/sisyphus` 的轻量确认意图,只保存 focus、原始 topic 和开始时间。 |
74
+ | `tweakDraftingFor` | 当前 `/goal-tweak` 流程对应的 goal id。 |
75
+ | `continuationQueuedFor` / `continuationScheduledFor` | 避免同一 goal 重复排队 auto-continue。 |
76
+ | `runningGoalId` | 防止 focus 改变后旧 tool call 继续作用在错误 goal 上。 |
77
+ | `goalWorkToolCalledThisTurn` | empty-turn guard:只有本 turn 做了有意义工作才继续 auto-continue。 |
78
+ | `turnStoppedFor` | pause/abort/complete/tweak 后阻止同一 turn 继续乱调用工具。 |
79
+ | `postCompactReminderPending` | session compact 后给下一轮 agent 注入 deterministic resync prompt。 |
80
+ | `activeGetGoalTurnsByGoalId` | 统计重复 `get_goal`,用于 soft nudge,不是 hard block。 |
81
+
82
+ ## 4. 持久化:Goal 文件与 Ledger
83
+
84
+ ### 4.1 Goal 文件
85
+
86
+ open 和 archived goals 存在 `.pi/goals/` 下:
87
+
88
+ ```text
89
+ .pi/goals/active_goal_<timestamp>_<id>.md
90
+ .pi/goals/archived/goal_<timestamp>_<id>.md
91
+ ```
92
+
93
+ 每个 goal 文件包含:
94
+
95
+ 1. extension 管理的 metadata;
96
+ 2. 用户可编辑的 `# Goal Prompt`;
97
+ 3. 状态和使用量等信息。
98
+
99
+ 在执行重要操作前,runtime 会重新读取 focused active goal 文件并合并磁盘状态。这样外部编辑、归档、删除、暂停等操作会优先于旧内存状态。
100
+
101
+ ### 4.2 Ledger 文件
102
+
103
+ runtime 还会把生命周期事件追加到:
104
+
105
+ ```text
106
+ .pi/goals/goal_events.jsonl
107
+ ```
108
+
109
+ 常见事件包括:
110
+
111
+ - `goal_created`
112
+ - `goal_focused` / `goal_unfocused`
113
+ - `goal_paused`
114
+ - `goal_resumed`
115
+ - `completion_requested`
116
+ - `audit_started`
117
+ - `audit_result`
118
+ - `goal_completed`
119
+ - `goal_aborted`
120
+ - `goal_tweaked`
121
+
122
+ ledger 的作用:
123
+
124
+ - 提供 durable history;
125
+ - 支持 compaction 后恢复目标上下文;
126
+ - 保存 auditor rejection,让后续 prompt 能提醒 agent 先解决审计意见;
127
+ - 支持重建 terminal goals 和近期事件。
128
+
129
+ ledger append 是 best-effort:写入失败不应该让用户的生命周期动作崩溃。
130
+
131
+ ## 5. 命令体系:用户拥有意图
132
+
133
+ 高层意图只能由用户命令启动或改变。
134
+
135
+ | 命令 | 作用 |
136
+ |---|---|
137
+ | `/goals <topic>` | 开始 regular goal intent discussion,可澄清、研究、拷问后 proposal。 |
138
+ | `/sisyphus <topic>` | 开始 Sisyphus 风格 intent discussion,重点拷问 ordered steps、done criteria 和 blockers。 |
139
+ | `/goals-set <objective>` | 直接创建并启动 regular goal,不进入 draft discussion。 |
140
+ | `/sisyphus-set <objective>` | 直接创建并启动 Sisyphus goal,不进入 draft discussion。 |
141
+ | `/goal-list` | 列出 `.pi/goals/` 下所有 open goals。 |
142
+ | `/goal-focus` | 让用户选择当前 session focus。 |
143
+ | `/goal-status` / `/goal` | 显示 focused goal 状态和其他 open goals 提示。 |
144
+ | `/goal-tweak <change>` | 修改 focused goal,但也要先走 tweak drafting。 |
145
+ | `/goal-pause` | 用户暂停 focused active goal。 |
146
+ | `/goal-resume` | 在策略允许时恢复 paused goal。 |
147
+ | `/goal-clear` | 归档 focused goal,或取消 goal confirmation。 |
148
+ | `/goal-abort` | 中止/归档 focused goal,或取消 goal confirmation。 |
149
+ | `/goal-settings` | 配置 auditor provider/model/thinking。 |
150
+
151
+ agent 不能任意切换 focus。如果有多个 open goals 且当前 session 没有 focus,需要目标的命令会让用户选择一个 goal。
152
+
153
+ ## 6. Goal Confirmation 流程
154
+
155
+ confirmation 是收集用户意图的轻量对话阶段,不是正式执行阶段,也不是长期运行状态机。
156
+
157
+ 当前设计:
158
+
159
+ - `propose_goal_draft` 常驻可见,但只有 `/goals` 或 `/sisyphus` 产生的 confirmation intent 存在时会通过 validator;
160
+ - agent 可以用 `goal_question` 或 `goal_questionnaire` 询问具体问题,也可以用普通对话澄清;
161
+ - 如果用户请求已经非常完整,可以直接 proposal;
162
+ - 如果能直接改善 goal contract,允许 targeted read-only research/reconnaissance;
163
+ - 直接 `create_goal` 仍然隐藏并拒绝,discussion-based 创建必须经过 `propose_goal_draft` 和用户确认;
164
+ - `/goals-set` 与 `/sisyphus-set` 是用户显式 direct set 快捷路径,会直接创建目标并开始执行。
165
+
166
+ `confirmationIntent` 形态:
167
+
168
+ ```ts
169
+ interface GoalConfirmationIntent {
170
+ focus: "goal" | "sisyphus";
171
+ originalTopic: string;
172
+ startedAt: number;
173
+ }
174
+ ```
175
+
176
+ 典型流程:
177
+
178
+ ```text
179
+ /goals topic
180
+ -> 创建 confirmationIntent
181
+ -> 发送普通 confirmation 指令
182
+ -> agent 澄清问题或在目标足够清楚时直接 proposal
183
+ -> propose_goal_draft 校验 focus / objective
184
+ -> 用户看到确认对话框
185
+ -> Confirm:创建并 focus goal
186
+ -> Continue Chatting:继续 clarification,不创建 goal
187
+ ```
188
+
189
+ 确认后,系统会把完整最终目标打印到 transcript,并写入 `.pi/goals/active_goal_*.md`。使用 `/goals-set` 或 `/sisyphus-set` 时没有确认对话,命令参数会直接写入 active goal 文件并成为当前 session focus。
190
+
191
+ ## 7. Tool Surface 与 Runtime Gates
192
+
193
+ `syncGoalTools()` 会在状态变化时重新计算当前可用工具。
194
+
195
+ 重要工具:
196
+
197
+ | 工具 | 作用 |
198
+ |---|---|
199
+ | `get_goal` | 读取 focused goal 状态。重复调用只触发 soft nudge,不 hard block。 |
200
+ | `goal_question` / `goal_questionnaire` | goal confirmation / tweak drafting 中的结构化用户对话。 |
201
+ | `propose_goal_draft` | 提交 goal 草案给用户确认;没有 confirmation intent 时会被 validator 拒绝。 |
202
+ | `apply_goal_tweak` | 提交并应用 goal 修改。 |
203
+ | `update_goal` | 请求完成目标,并触发独立审计。 |
204
+ | `pause_goal` | agent 因真实 blocker 暂停目标。 |
205
+ | `abort_goal` | agent 因目标废弃、不可行、不安全等原因中止目标。 |
206
+ | `step_complete` | 隐藏的 legacy no-op;Sisyphus 不再使用 step counter。 |
207
+ | `create_goal` | 隐藏且拒绝;普通创建必须走 proposal/confirmation。 |
208
+
209
+ 当前仍保留的 hard gates 是小而机械的:
210
+
211
+ - 直接 `create_goal` 拒绝;
212
+ - mismatched confirmation mode proposal 拒绝;
213
+ - 非法 lifecycle transition 拒绝;
214
+ - stop tool 成功后,同一 turn 里阻止继续调用非允许工具;
215
+ - stale continuation message 会被 neutralize;
216
+ - completion 必须 auditor `<approved/>`。
217
+
218
+ 已经移除或软化的硬限制:
219
+
220
+ - auto-continue 不按固定轮数停止;
221
+ - 重复 `get_goal` 不再 hard block,只 soft nudge;
222
+ - `propose_goal_draft` 前不再强制 runtime question gate;
223
+ - confirmation 阶段不依赖 hard whitelist、question counter 或 hidden prompt identity,更多由 prompt 指导。
224
+
225
+ ## 8. 执行循环与 Auto-Continue
226
+
227
+ goal active 且 `autoContinue` 为 true 时,runtime 会在适当时机排队 continuation prompt。
228
+
229
+ 事件驱动流程:
230
+
231
+ ```text
232
+ turn_start
233
+ -> 重置 per-turn flags
234
+ -> begin usage accounting
235
+
236
+ tool_call
237
+ -> 执行 post-stop block
238
+ -> 统计重复 get_goal 用于 soft nudge
239
+ -> 标记 meaningful work tool
240
+
241
+ tool_execution_end
242
+ -> 统计 elapsed time
243
+
244
+ turn_end
245
+ -> 统计 assistant tokens
246
+ -> 如果 assistant 被 abort,则 pause goal
247
+ -> 从磁盘刷新 goal
248
+ -> 如果本 turn 有 meaningful work 且 goal 仍 active,则 queueContinuation
249
+ ```
250
+
251
+ empty-turn guard 仍然存在:如果一个 turn 只是聊天,没有做任何有意义的 goal-work tool,就不会继续 auto-continue。它只防止纯聊天循环烧 token,不按固定轮数停止。
252
+
253
+ `get_goal`、question tools、draft proposal tools 不算实际执行进度。
254
+
255
+ ## 9. Completion 与可见 Audit 阶段
256
+
257
+ completion 不信任执行 agent 单方声明,而是一个双 agent 协议。
258
+
259
+ ### 9.1 执行 agent 请求完成
260
+
261
+ 执行 agent 调用:
262
+
263
+ ```json
264
+ {
265
+ "status": "complete",
266
+ "completionSummary": "说明完成了什么,以及有哪些证据"
267
+ }
268
+ ```
269
+
270
+ `update_goal` 会先校验 focused goal 是否可以完成,然后写入 `completion_requested` ledger event。
271
+
272
+ ### 9.2 对话中出现 audit started
273
+
274
+ 运行 auditor 前,runtime 会插入一条独立可见消息,custom type 是 `pi-goal-audit-event`:
275
+
276
+ ```text
277
+ Goal audit started
278
+ Auditor: I am starting the independent completion audit.
279
+ Goal id: ...
280
+ Auditor model: ...
281
+ Completion claim: ...
282
+ ```
283
+
284
+ 这让 audit 成为 transcript 里一个明确的 agentic 阶段,而不是隐藏在 `update_goal` tool result 里。
285
+
286
+ ### 9.3 独立 auditor session
287
+
288
+ `runGoalCompletionAuditor()` 会创建一个独立 in-memory pi session:
289
+
290
+ - 使用同一个 cwd;
291
+ - 默认使用当前 pi model,除非 auditor settings 覆盖;
292
+ - 不加载 extension resources 和 skills;
293
+ - 工具限制为 `read`、`grep`、`find`、`ls`、`bash`;
294
+ - auditor prompt 要求语义审计;
295
+ - 最后一行必须是 `<approved/>` 或 `<disapproved/>`。
296
+
297
+ Auditor 收到的信息:
298
+
299
+ - 完整 goal objective;
300
+ - executor completion claim;
301
+ - 当前 goal metadata 和 detailed summary。
302
+
303
+ ### 9.4 对话中出现 audit result
304
+
305
+ 如果通过,runtime 插入:
306
+
307
+ ```text
308
+ Goal audit approved
309
+ Auditor: I approve this completion claim.
310
+ Auditor model: ...
311
+
312
+ Audit Report
313
+ ...
314
+ <approved/>
315
+ ```
316
+
317
+ 如果拒绝,runtime 插入:
318
+
319
+ ```text
320
+ Goal audit rejected
321
+
322
+ Goal completion rejected by independent auditor.
323
+ Auditor model: ...
324
+ Auditor error: ...
325
+
326
+ Audit Report 或 rejection reason
327
+ ```
328
+
329
+ ### 9.5 归档 complete
330
+
331
+ 只有 audit approved 后,runtime 才会:
332
+
333
+ 1. 统计剩余 usage;
334
+ 2. 将 goal stop 为 `complete`;
335
+ 3. 归档 goal 文件;
336
+ 4. 清空 session focus;
337
+ 5. 写入 `goal_completed` ledger event;
338
+ 6. 返回最终 `Goal complete.` tool result。
339
+
340
+ 最终 completion result 不再需要重复完整 auditor report,因为 auditor 已经作为单独对话阶段出现。
341
+
342
+ ## 10. Pause、Abort 与 Post-Stop 行为
343
+
344
+ agent 可以在真实 blocker 下调用 `pause_goal`。用户也可以用 `/goal-pause` 或 abort active run 来暂停目标。
345
+
346
+ `pause_goal`、`abort_goal`、`update_goal`、`apply_goal_tweak` 成功后,会设置 `turnStoppedFor`。之后同一个 turn 里,`tool_call` hook 会阻止额外的非允许工具调用。这个 hard gate 仍然保留:生命周期已经 stop 后,agent 应该总结并交还控制,而不是继续修改文件。
347
+
348
+ pause 与 abort 的区别:
349
+
350
+ - pause 表示目标之后可能恢复;
351
+ - abort 表示目标废弃、不可行、不安全或用户取消,应归档。
352
+
353
+ 两者都会写入 ledger。
354
+
355
+ ## 11. Compaction 与 Auditor Rejection 记忆
356
+
357
+ session compaction 发生时,`session_compact` 可能设置 `postCompactReminderPending`。下一次 `before_agent_start` 会注入 deterministic compaction summary。
358
+
359
+ summary 来自:
360
+
361
+ - focused goal;
362
+ - other open goals;
363
+ - recent ledger events;
364
+ - terminal goals;
365
+ - latest auditor rejection。
366
+
367
+ 如果 focused goal 最近一次 audit 是 `disapproved`,active / paused prompt 会显式提醒 auditor rejection,避免 agent 不处理问题就反复请求完成。
368
+
369
+ ## 12. 当前流程图
370
+
371
+ ```text
372
+ User
373
+ |
374
+ | /goals 或 /sisyphus
375
+ v
376
+ Confirmation runtime
377
+ |-- 发送轻量 intent/confirmation 指令
378
+ |-- 暴露 dialogue/proposal tools
379
+ v
380
+ Executor agent
381
+ |-- 询问用户或提交草案
382
+ v
383
+ User confirmation
384
+ |-- Continue Chatting -> 继续 clarification
385
+ |-- Confirm -> 创建并 focus goal
386
+ v
387
+ Execution runtime
388
+ |-- active prompt + work tools
389
+ |-- meaningful work 后 auto-continue
390
+ |-- accounting/ledger
391
+ v
392
+ Executor agent
393
+ |-- 正常 read/write/bash/edit 工作
394
+ |-- pause_goal / abort_goal / update_goal
395
+ v
396
+ Completion request
397
+ |-- 对话中出现 Goal audit started
398
+ v
399
+ Auditor agent session
400
+ |-- read-only-oriented inspection
401
+ |-- 输出 <approved/> 或 <disapproved/>
402
+ v
403
+ Visible audit result
404
+ |-- rejected -> goal 保持 open
405
+ |-- approved -> 归档 goal
406
+ v
407
+ Final completion report
408
+ ```
409
+
410
+ ## 13. 代码入口索引
411
+
412
+ | 主题 | 文件 |
413
+ |---|---|
414
+ | extension orchestration、commands、tools、hooks | `extensions/goal.ts` |
415
+ | goal record 类型和 normalization | `extensions/goal-record.ts` |
416
+ | lifecycle policy 与最终 report | `extensions/goal-policy.ts` |
417
+ | lightweight confirmation prompt 与 proposal validation | `extensions/goal-draft.ts` |
418
+ | 独立 auditor session | `extensions/goal-auditor.ts` |
419
+ | durable ledger | `extensions/goal-ledger.ts` |
420
+ | post-compaction summary | `extensions/goal-compaction.ts` |
421
+ | goal files 与 archive IO | `extensions/storage/goal-files.ts` |
422
+ | active / continuation prompts | `extensions/prompts/goal-prompts.ts` |
423
+ | UI widget | `extensions/widgets/goal-widget.ts` |
424
+
425
+ ## 14. 重要设计取舍
426
+
427
+ - runtime 仍是 TypeScript hook-driven 架构,不是集中式 OTP orchestrator。
428
+ - ledger 是 best-effort event log,不是事务数据库。
429
+ - audit 的模型/session 是独立的,但可见 transcript 消息由 `pi-goal` 在 auditor 返回后桥接出来。
430
+ - 多个 hard limits 已经改成 soft guidance,但 completion 和 post-stop safety 仍是 hard gate。
431
+ - auto-continue 不按固定轮数停止;剩余刹车是用户 pause/abort、completion、post-stop gate 和 empty-turn guard。
432
+ - 多个 open goals 是 durable project state;focus 是 human-owned session state。