pi-ultracode 0.1.0 → 0.1.1

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.
package/README.md CHANGED
@@ -1,65 +1,85 @@
1
- # pi-ultracode
1
+ # Ultracode
2
2
 
3
- > Claude-Code-style **ultracode** for [Pi](https://github.com/earendil-works/pi).
3
+ **Ultracode** [Pi](https://github.com/badlogic/pi-mono) 的“全力执行”模式。它把 Pi 的 Agentic Loop 从单线程编码助手升级为能够运行子代理团队的编排器。
4
4
 
5
- `ultracode` is an effort mode that turns Pi into a relentless, multi-agent
6
- orchestrator. While it's on, Pi raises its thinking to **xhigh** and treats
7
- "author and run a workflow" as the **default** for every substantive task —
8
- decomposing work, fanning it out across isolated subagents, and adversarially
9
- verifying findings before committing to an answer. Token cost is not the
10
- constraint; correctness and coverage are.
5
+ 启用 Ultracode 后,主代理会获得一条 `workflow` 工具,以及一段要求它主动使用该工具处理复杂任务的系统提示词。每个子代理都运行在独立的内存 Pi 会话中,拥有标准编码工具、独立上下文,并可选择结构化输出、模型覆盖、Agent Type 和隔离的 git worktree。
11
6
 
12
- This single extension implements the full ultracode surface:
7
+ Ultracode 保留 Pi 自身的核心循环、内存模型、工具系统、扩展 API 与会话格式。它是在 Pi 之上增加编排能力,而不是替换 Pi。
13
8
 
14
- | Pillar | What you get |
9
+ ## 功能
10
+
11
+ | 能力 | 说明 |
15
12
  | --- | --- |
16
- | **Ultracode mode** | `/ultracode on` raises thinking to xhigh, keeps the `workflow` tool active, and injects a standing "orchestrate + verify by default" system block on every turn. Survives reload / resume / fork via session entries. Optional token budget. |
17
- | **The `workflow` tool** | A deterministic JavaScript orchestrator: `agent()`, `parallel()`, `pipeline()`, `phase()`, `log()`, `workflow()` (nesting), `args`, `budget`. Plus per-agent **model overrides**, **custom agent types**, **git-worktree isolation**, a real **token budget**, **resumable runs**, and **script persistence**. |
18
- | **`/workflows` manager** | List recent and in-flight runs with live progress; inspect or abort runs. |
13
+ | `workflow` 工具 | 运行确定性的 JavaScript 编排脚本,并调用 `agent()`、`parallel()`、`pipeline()` `workflow()`。 |
14
+ | 独立子代理 | 每次 `agent()` 调用都会创建一条带标准编码工具的新 Pi 会话。 |
15
+ | 并行执行 | `parallel()` 并发运行任务,且保留输入顺序。 |
16
+ | 流水线 | `pipeline()` 让任务独立流经多个阶段,无需全局屏障。 |
17
+ | 结构化输出 | 传入 JSON Schema,即可通过终止型 `structured_output` 工具获得经校验的对象。 |
18
+ | Agent Type | 通过 `.pi/ultracode/agents/*.md` 定义角色提示词、工具白名单、模型和思考强度。 |
19
+ | 模型覆盖 | 每次 `agent()` 调用都可指定模型模式,例如 `anthropic/claude-sonnet-4`。 |
20
+ | Worktree 隔离 | 让写入型并行代理在临时 git worktree 中运行,并把改动安全合并回共享工作树。 |
21
+ | 进度面板 | 实时状态、活动计数、token 与成本统计、阶段信息,以及可选的 TUI 面板。 |
22
+ | Ultracode 模式 | `/ultracode on` 启用主动编排提示词,并请求当前模型支持的最高思考强度。 |
23
+ | 配额执行 | 可选 token 预算、代理数量限制、并发限制、脚本超时与嵌套深度限制。 |
24
+
25
+ ## 为什么使用工作流
26
+
27
+ 普通的多代理提示通常依赖模型临场决定如何分解与组合任务。Ultracode 采用另一种方式:**模型编写确定性的编排脚本,运行时执行该脚本**。
28
+
29
+ 这样可以清晰表达:
19
30
 
20
- Inspired by Anthropic's [dynamic workflows in Claude Code](https://claude.com/blog/introducing-dynamic-workflows-in-claude-code),
21
- and a superset of the `pi-dynamic-workflows` prototype.
31
+ - 哪些任务可以并行;
32
+ - 哪些阶段依赖前一阶段的结果;
33
+ - 哪些代理应返回结构化数据;
34
+ - 何时需要共享上下文,何时需要隔离;
35
+ - 预算如何限制额外探索;
36
+ - 失败分支应如何降级。
22
37
 
23
- ## Install
38
+ ## 安装
24
39
 
25
40
  ```bash
26
41
  pi install npm:pi-ultracode # or: pi install /path/to/pi-ultracode
27
42
  ```
28
43
 
29
- > **Conflict note:** pi-ultracode registers a tool named `workflow`, the same name
30
- > used by `pi-dynamic-workflows`. pi-ultracode is a strict superset, so remove the
31
- > prototype first: `pi remove npm:pi-dynamic-workflows`.
44
+ > **工具冲突:** pi-ultracode 注册名为 `workflow` 的工具,与 `pi-dynamic-workflows` 相同。pi-ultracode 是后者的严格超集,请先执行 `pi remove npm:pi-dynamic-workflows` 移除原型扩展。
32
45
 
33
- Then in Pi: `/reload`.
46
+ 安装后在 Pi 中运行 `/reload`。
47
+
48
+ 开发仓库也可直接加载:
49
+
50
+ ```bash
51
+ pi -e ./extensions/ultracode.ts
52
+ ```
34
53
 
35
- ## Usage
54
+ 仓库中提供两个兼容入口:
36
55
 
37
- ### Turn ultracode on
56
+ - `extensions/ultracode.ts`:推荐的 Pi 扩展入口;
57
+ - `src/index.ts`:兼容入口,重新导出同一个扩展。
58
+
59
+ ## 快速开始
60
+
61
+ 在 Pi 中启用模式:
38
62
 
39
63
  ```text
40
- /ultracode # TOGGLE on/off
41
- /ultracode on # xhigh thinking + default workflow orchestration
42
- /ultracode on 500k # also cap the per-session output-token budget at ~500k
43
- /ultracode budget 1m # change the budget later
44
- /ultracode status # show current mode
45
- /ultracode off # restore the previous thinking level
64
+ /ultracode on
46
65
  ```
47
66
 
48
- > **Models without `xhigh`:** pi clamps the requested level down to the model's
49
- > maximum (e.g. glm-5.2 tops out at `high`; non-reasoning models fall to `off`) —
50
- > it never errors. `/ultracode status` shows the level that actually applied
51
- > (e.g. `ultracode: on · thinking high`). The **workflow orchestration** half of
52
- > ultracode doesn't depend on the thinking level at all, so it's unaffected by
53
- > the model you run.
67
+ 随后给出复杂任务,例如:
54
68
 
55
- You can also start a session in ultracode mode with the CLI flag:
69
+ ```text
70
+ Review the current branch, identify correctness and compatibility risks,
71
+ fix the justified issues, and verify the result.
72
+ ```
73
+
74
+ 启用后,主代理会被要求在并行检视、迁移、审计或大范围修改明显有益时编写并运行工作流,而不是把所有工作塞进一个上下文。
75
+
76
+ 也可以在启动时启用:
56
77
 
57
78
  ```bash
58
79
  pi --ultracode
59
80
  ```
60
81
 
61
- While on, just describe the task. Pi will write a workflow script and call the
62
- `workflow` tool, with live progress shown inline:
82
+ 工作流运行时会内联显示实时进度,例如:
63
83
 
64
84
  ```text
65
85
  ◆ ▶ audit_repo (4/7 done, 2 running) · 3 cached · 41.2k/500k tok
@@ -72,154 +92,351 @@ While on, just describe the task. Pi will write a workflow script and call the
72
92
  ▶ Verify 0/2 · 2 running
73
93
  ```
74
94
 
75
- Press `Esc` to cancel; running subagents are aborted and surfaced as skipped.
95
+ `Esc` 可取消;运行中的子代理会被终止并显示为 skipped
96
+
97
+ ## 命令
98
+
99
+ ```text
100
+ /ultracode # toggle on/off
101
+ /ultracode on # enable orchestration mode
102
+ /ultracode on 500k # enable it with an output-token budget
103
+ /ultracode off # disable it and restore the previous thinking level
104
+ /ultracode status # show status and the configured budget
105
+ /ultracode budget 500k # set a token budget
106
+ /ultracode budget off # remove the budget
107
+ /workflows # toggle the workflow run panel
108
+ /workflows <runId> # show one run's detail
109
+ /workflows clear # hide the panel
110
+ /workflows abort # abort active runs and hide the panel
111
+ ```
112
+
113
+ `/workflows` 面板是执行命令时生成的静态快照;再次运行该命令可刷新,`/workflows clear` 只隐藏面板,不删除 registry 记录。工作流执行期间的实时进度显示在工具结果中。为缩小隐私边界,进度只包含结构化生命周期状态和安全摘要,不捕获、持久化或展示子代理的原始流式响应正文。
76
114
 
77
- ### Manage runs
115
+ 状态行会显示实际生效的思考强度,例如:
78
116
 
79
117
  ```text
80
- /workflows # toggle the run panel above the editor (run again to hide)
81
- /workflows wf_ab12-3 # show one run's detail
82
- /workflows clear # hide the panel
83
- /workflows abort # abort all active runs (and hide the panel)
118
+ ultracode: on · max
119
+ ultracode: on · xhigh
120
+ ultracode: on · high
121
+ ultracode: on · off
84
122
  ```
85
123
 
86
- The panel is a static snapshot taken when you run the command, so re-run
87
- `/workflows` to refresh it, or `/workflows clear` to dismiss it. Live progress
88
- while a workflow runs is shown inline in the tool result, not in this panel.
124
+ Ultracode 请求的是**当前模型支持的最高强度**,不是固定标签。对支持 `max` 的模型(包括 GPT-5.6 Sol)使用 `max`;能力较低的模型由 Pi 正常钳制。对不认识 `max` 的旧版 Pi,Ultracode 会自动回退到 `xhigh`。
89
125
 
90
- ## Workflow script shape
126
+ 显式覆盖始终优先:
91
127
 
92
- A workflow is plain JavaScript whose first statement is a **pure literal**
93
- `export const meta`:
128
+ - 每次调用的模型后缀,例如 `model: "openai/gpt-5.6-sol:high"`;
129
+ - Agent Type frontmatter 中的 `thinking:`;
130
+ - 显式 `:off`、`:high`、`:xhigh` 或 `:max`。
131
+
132
+ 模型切换或手动降低强度时,只要 Ultracode 仍启用,它就会重新请求该模型的最高可用强度。关闭或退出时,会恢复启用前的会话强度与原始默认偏好。若当前模型暂时无法表示原始强度,恢复意图会保留到后续兼容模型;中间模型的钳制结果不会覆盖原始意图。
133
+
134
+ ## 工作原理
135
+
136
+ 扩展注册单个 `workflow` 工具。工具输入是 JavaScript 源码,其中必须包含纯字面量 `meta` 导出:
94
137
 
95
138
  ```js
96
139
  export const meta = {
97
- name: 'review_changes',
98
- description: 'Review changed files across dimensions, verify each finding',
99
- phases: [{ title: 'Review' }, { title: 'Verify' }],
100
- }
140
+ name: "review_and_fix",
141
+ description: "Review independent areas, then verify the combined result",
142
+ };
143
+ ```
144
+
145
+ 脚本可使用下列全局变量:
146
+
147
+ - `agent(prompt, options)`
148
+ - `parallel(thunks)`
149
+ - `pipeline(items, ...stages)`
150
+ - `phase(title)`
151
+ - `log(message)`
152
+ - `workflow(nameOrRef, args)`
153
+ - `args`
154
+ - `cwd`
155
+ - `budget`
156
+
157
+ `agent()` 的主要选项:
158
+
159
+ | 选项 | 作用 |
160
+ | --- | --- |
161
+ | `label` | 进度中显示的 2–5 个词短名称。 |
162
+ | `phase` | 显式指定进度阶段。 |
163
+ | `schema` | 普通 JSON Schema;成功时返回经 `structured_output` 校验的对象。 |
164
+ | `model` | 按模式覆盖模型,也可带 `:off`、`:high`、`:xhigh` 或 `:max`。 |
165
+ | `agentType` | 使用内置或自定义角色。 |
166
+ | `isolation: "worktree"` | 在临时 git worktree 中运行写入型代理。 |
167
+
168
+ 为保证可恢复和确定性,脚本不提供 Node.js 模块加载能力,也不允许直接使用 `fs`、网络、`Date.now()`、`new Date()` 或 `Math.random()`。
101
169
 
102
- const DIMENSIONS = [
103
- { key: 'bugs', prompt: 'Find correctness bugs in the diff.' },
104
- { key: 'perf', prompt: 'Find performance regressions in the diff.' },
105
- ]
170
+ ### 取消与清理
171
+
172
+ `Esc` 取消工作流时,取消信号会覆盖子代理初始化、异步 preflight 和流式执行窗口。运行器会等待进行中的 `abort()` 完成后再释放会话;清理失败不会覆盖原始 prompt 或取消错误。兼容旧版 Pi 时创建的临时 fallback 会话也遵循同一清理规则。
173
+
174
+ ### 直接调用
175
+
176
+ ```js
177
+ export const meta = {
178
+ name: "single_review",
179
+ description: "Run one focused reviewer",
180
+ };
181
+
182
+ const result = await agent(
183
+ "Inspect src/mode.ts for lifecycle bugs. Return concise findings.",
184
+ { label: "mode review" },
185
+ );
186
+
187
+ export default result;
188
+ ```
189
+
190
+ ### 并行检视
191
+
192
+ `parallel()` 接收 thunk,而不是已经启动的 Promise:
193
+
194
+ ```js
195
+ export const meta = {
196
+ name: "parallel_review",
197
+ description: "Review independent concerns concurrently",
198
+ };
199
+
200
+ const tasks = [
201
+ ["correctness", "Review correctness and lifecycle behavior."],
202
+ ["tests", "Review test coverage and missing regressions."],
203
+ ["docs", "Review README and parity documentation."],
204
+ ];
205
+
206
+ const results = await parallel(
207
+ tasks.map(([label, prompt]) => () => agent(prompt, { label })),
208
+ );
209
+
210
+ export default results.filter(Boolean);
211
+ ```
212
+
213
+ ### 流水线
214
+
215
+ 除非后续阶段确实需要所有前置结果,否则优先使用 `pipeline()`。不同条目可独立推进:
216
+
217
+ ```js
218
+ export const meta = {
219
+ name: "review_pipeline",
220
+ description: "Inspect and verify several modules independently",
221
+ };
222
+
223
+ const files = ["src/mode.ts", "src/workflow/agent-runner.ts"];
106
224
 
107
- // pipeline: each dimension verifies as soon as its review completes (no barrier).
108
225
  const results = await pipeline(
109
- DIMENSIONS,
110
- (d) => agent(d.prompt, { label: 'review:' + d.key, phase: 'Review', schema: FINDINGS }),
111
- (review) => parallel((review.findings ?? []).map((f) => () =>
112
- agent('Adversarially verify, default to refuted: ' + f.title,
113
- { label: 'verify:' + f.file, phase: 'Verify', agentType: 'code-reviewer', schema: VERDICT })
114
- .then((v) => ({ ...f, verdict: v }))
115
- )),
116
- )
226
+ files,
227
+ async (file) => agent(
228
+ `Inspect ${file} and report concrete risks.`,
229
+ { label: `inspect ${file}` },
230
+ ),
231
+ async (finding, file) => agent(
232
+ `Verify this finding against ${file}:\n\n${finding}`,
233
+ { label: `verify ${file}` },
234
+ ),
235
+ );
236
+
237
+ export default results.filter(Boolean);
238
+ ```
117
239
 
118
- const confirmed = results.flat().filter(Boolean).filter((f) => f.verdict?.isReal)
119
- return { confirmed }
240
+ ### 结构化输出
120
241
 
121
- const FINDINGS = { type: 'object', properties: { findings: { type: 'array', items: { type: 'object' } } } }
122
- const VERDICT = { type: 'object', properties: { isReal: { type: 'boolean' }, why: { type: 'string' } }, required: ['isReal'] }
242
+ 不传 `schema` 时,`agent()` 返回子代理最后一段非空 assistant 文本;传入普通 JSON Schema 时,则返回经校验的对象:
243
+
244
+ ```js
245
+ export const meta = {
246
+ name: "structured_audit",
247
+ description: "Collect machine-readable findings",
248
+ };
249
+
250
+ const finding = await agent(
251
+ "Audit the mode lifecycle and return one finding.",
252
+ {
253
+ label: "lifecycle audit",
254
+ schema: {
255
+ type: "object",
256
+ properties: {
257
+ severity: { enum: ["low", "medium", "high"] },
258
+ summary: { type: "string" },
259
+ },
260
+ required: ["severity", "summary"],
261
+ additionalProperties: false,
262
+ },
263
+ },
264
+ );
265
+
266
+ export default finding;
123
267
  ```
124
268
 
125
- ### Globals
269
+ 使用 schema 的代理必须调用 `structured_output`。否则该 `agent()` 分支会失败并返回 `null`;工作流本身仍可继续,因此脚本应过滤或显式处理失败分支,而不会把自由文本伪装成结构化结果。
126
270
 
127
- | Global | Description |
128
- | --- | --- |
129
- | `agent(prompt, opts)` | Spawn an isolated subagent. Returns its final text, or a validated object when `opts.schema` is set. |
130
- | `parallel(thunks)` | Run `() => agent(...)` thunks concurrently. **A barrier.** Failures become `null`. |
131
- | `pipeline(items, ...stages)` | Run each item through stages independently (no barrier). Stages get `(prev, original, index)`. **The default** for multi-stage work. |
132
- | `workflow(nameOrRef, args)` | Run a saved workflow (by name) or `{ scriptPath }` inline, sharing the run's concurrency cap, agent counter, and budget. One level of nesting. |
133
- | `phase(title)` | Mark the current progress group. |
134
- | `log(message)` | Append a workflow-level log line. |
135
- | `args`, `cwd` | The tool's `args` value; the working directory. |
136
- | `budget` | `{ total, spent(), remaining() }` — real output-token budget. |
137
-
138
- ### `agent()` options
139
-
140
- | Option | Effect |
141
- | --- | --- |
142
- | `label` | Short (2-5 word) name shown in live progress. |
143
- | `phase` | Assign this agent to a progress group explicitly (use inside `parallel`/`pipeline`). |
144
- | `schema` | Plain JSON Schema; the subagent returns a validated object via a terminating `structured_output` tool. |
145
- | `model` | Override the subagent model by pattern, e.g. `'sonnet'` or `'provider/id:high'`. |
146
- | `agentType` | Use a custom role: built-ins `claude`, `general-purpose`, `Explore`, `Plan`, `code-reviewer`, or your own (below). |
147
- | `isolation: 'worktree'` | Run the agent in a throwaway git worktree (for parallel file mutation). Changes are applied back to the working tree under a lock; an unchanged worktree is auto-removed. |
271
+ ## Agent Type
148
272
 
149
- ### Determinism
273
+ 项目级角色定义放在:
150
274
 
151
- Scripts run in a `vm` sandbox. `Date.now()`, `new Date()`, `Math.random()`,
152
- `require`/`import`, `fs`, and network APIs are unavailable — this keeps `meta`
153
- parseable and runs **reproducible and resumable**. Pass timestamps via `args`;
154
- vary randomness by agent index.
275
+ ```text
276
+ .pi/ultracode/agents/*.md
277
+ ```
278
+
279
+ 用户级角色定义放在:
280
+
281
+ ```text
282
+ ~/.pi/ultracode/agents/*.md
283
+ ```
155
284
 
156
- ## Custom agent types
285
+ 同时兼容旧的用户级目录 `~/.pi/agent/agents/*.md`。
157
286
 
158
- Drop a Markdown file with frontmatter under
159
- `.pi/ultracode/agents/<name>.md` (project) or `~/.pi/ultracode/agents/<name>.md`
160
- (user):
287
+ 示例:
161
288
 
162
- ```markdown
289
+ ```md
163
290
  ---
164
- name: security
165
- description: Security-focused reviewer
166
- tools: read, grep, find, bash
167
- model: sonnet
291
+ name: reviewer
292
+ description: Read-only reviewer focused on correctness and regressions
293
+ tools: read, bash
294
+ model: anthropic/claude-sonnet-4
168
295
  thinking: high
169
296
  ---
170
- You are a security reviewer. Hunt for injection, authz gaps, and unsafe deserialization.
171
- Cite exact file:line evidence and prefer false negatives over invented findings.
297
+
298
+ Inspect the requested area. Do not modify files. Report only evidence-backed findings.
172
299
  ```
173
300
 
174
- Then `agent('Audit auth.ts', { agentType: 'security' })`.
301
+ 工作流中的用法:
175
302
 
176
- ## Saved & resumable runs
303
+ ```js
304
+ await agent("Review the current diff.", {
305
+ label: "diff review",
306
+ agentType: "reviewer",
307
+ });
308
+ ```
177
309
 
178
- Every run persists its script and a JSONL journal under
179
- `<sessionDir>/ultracode-runs/<runId>.{workflow.js,jsonl}`. To resume after a
180
- pause, kill, or script edit, call the tool again with `resumeFromRunId`: the
181
- longest unchanged prefix of `agent()` calls returns cached results instantly;
182
- the first changed/new call and everything after it run live.
310
+ 内置类型包括 `claude`、`general-purpose`、`Explore`、`Plan` `code-reviewer`。
183
311
 
184
- Save reusable workflows under `.pi/ultracode/workflows/<name>.workflow.js` and
185
- run them with the tool's `name` parameter or `workflow('<name>')`.
312
+ Agent Type 可控制:
186
313
 
187
- ## Library modules
314
+ - 角色提示词;
315
+ - 工具白名单;
316
+ - 默认模型;
317
+ - 默认思考强度。
188
318
 
189
- | File | Purpose |
190
- | --- | --- |
191
- | `src/prompts.ts` | Ultracode system block + workflow tool guidelines. |
192
- | `src/mode.ts` | The ultracode mode controller (toggle, thinking, persistence, injection). |
193
- | `src/commands.ts` | `/ultracode` and `/workflows` commands. |
194
- | `src/workflow/parser.ts` | AST-validated, deterministic script parser. |
195
- | `src/workflow/runtime.ts` | The sandboxed runtime (agent/parallel/pipeline/phase/log/workflow/budget). |
196
- | `src/workflow/agent-runner.ts` | In-memory subagent runner (model, agent type, worktree, usage). |
197
- | `src/workflow/worktree.ts` | Git worktree isolation. |
198
- | `src/workflow/agent-types.ts` | Custom agent-type discovery. |
199
- | `src/workflow/journal.ts` | Run journal + resume. |
200
- | `src/workflow/registry.ts` | In-process run registry for `/workflows`. |
201
- | `src/workflow/display.ts` | Live progress snapshots and renderers. |
202
- | `src/workflow/structured-output.ts` / `json-schema.ts` | Terminating structured output + JSON-Schema TypeBox. |
203
- | `src/workflow/tool.ts` | The Pi `workflow` tool. |
204
- | `extensions/ultracode.ts` | Extension entrypoint. |
205
-
206
- ## Development
319
+ 每次调用的选项优先于 Agent Type 默认值。
320
+
321
+ ## 模型选择
322
+
323
+ `model` 接受 provider/model 模式,并可带思考强度后缀:
324
+
325
+ ```js
326
+ await agent("Analyze this failure.", {
327
+ label: "failure analysis",
328
+ model: "openai/gpt-5.6-sol:max",
329
+ });
330
+ ```
331
+
332
+ 若未指定 model,则子代理继承工作流工具创建时的 Pi 当前模型。Ultracode 启用时,默认子代理强度同样请求 `max`,并按每个代理实际选择的模型独立钳制。Ultracode 不会自动启用 GPT-5.6 `pro` 模式,也不会隐式设置 token 预算。
333
+
334
+ ## Worktree 隔离
335
+
336
+ 并行写入同一工作树容易产生竞态。对会修改文件的代理使用:
337
+
338
+ ```js
339
+ await agent("Implement the requested change and run focused tests.", {
340
+ label: "implementation",
341
+ isolation: "worktree",
342
+ });
343
+ ```
344
+
345
+ 运行器会:
346
+
347
+ 1. 创建临时 git worktree;
348
+ 2. 在隔离目录运行代理;
349
+ 3. 捕获 worktree diff;
350
+ 4. 检查共享工作树是否漂移;
351
+ 5. 通过 `git apply --3way` 应用补丁;
352
+ 6. 在失败时保存 rescue patch;
353
+ 7. 成功且安全时清理临时 worktree。
354
+
355
+ 如果 diff 捕获、rescue 保存或补丁集成异常,运行器会故障安全地保留 worktree,并在日志中给出路径,避免丢失代理改动。确认内容后可手动恢复或删除;超过保留阈值的 Ultracode worktree 会由后续运行回收。
356
+
357
+ 使用隔离前,共享仓库必须处于干净状态。
358
+
359
+ ## 已保存的工作流与可恢复运行
360
+
361
+ 可复用工作流可放在:
362
+
363
+ ```text
364
+ .pi/ultracode/workflows/<name>.workflow.js
365
+ ~/.pi/ultracode/workflows/<name>.workflow.js
366
+ ```
367
+
368
+ 可通过工具的 `name`、`scriptPath`,或脚本内调用来运行:
369
+
370
+ ```js
371
+ await workflow("saved_workflow_name", { target: "src" });
372
+ ```
373
+
374
+ 嵌套限制为一层,且共享父运行的并发上限、代理计数与 token 预算。
375
+
376
+ 每次运行都会把脚本和 JSONL journal 保存到:
377
+
378
+ ```text
379
+ <sessionDir>/ultracode-runs/<runId>.workflow.js
380
+ <sessionDir>/ultracode-runs/<runId>.jsonl
381
+ ```
382
+
383
+ 暂停、终止或修改脚本后,可再次调用 `workflow` 工具并传入 `resumeFromRunId`。最长的未变 `agent()` 调用前缀会立即返回缓存结果;第一个变化或新增的调用及其后续调用会实时执行。
384
+
385
+ ## 预算与限制
386
+
387
+ 默认 token 预算为无限。只有显式配置后,`budget.total` 才是有限值。
388
+
389
+ 动态探索必须同时检查预算是否存在:
390
+
391
+ ```js
392
+ while (budget.total && budget.remaining() > 50_000) {
393
+ // additional bounded exploration
394
+ break;
395
+ }
396
+ ```
397
+
398
+ 若不检查 `budget.total`,无限预算下的 `remaining()` 是 `Infinity`,循环可能一直运行到代理数量上限。
399
+
400
+ 其他限制:
401
+
402
+ - 最大代理数;
403
+ - 最大并发数;
404
+ - 工作流脚本超时;
405
+ - 嵌套深度;
406
+ - 结构化输出校验;
407
+ - 静态禁止不确定性与危险全局变量。
408
+
409
+ ## 会话与兼容性
410
+
411
+ Ultracode 模式状态通过自定义 session entry 持久化。恢复、reload、fork 和 `/tree` 导航都会按当前 branch 重新读取状态;被丢弃分支中的 entry 不会错误启用模式。旧版 Pi 或旧模型会把 `max` 兼容回退为 `xhigh`,而不会把未知值静默变成 `off`。
412
+
413
+ 扩展清理只管理自己的 widget、status 与运行状态,不会调用 Pi 的全局 `ui.clear()`,因此不会清除其他扩展的 UI。
414
+
415
+ ## 开发
207
416
 
208
417
  ```bash
209
- npm test # node --test over the parser, runtime, journal, agent types, mode, and extension
210
- npm run typecheck # tsc --noEmit (requires a local TypeScript)
418
+ npm run typecheck
419
+ npm test
420
+ npm pack --dry-run
211
421
  ```
212
422
 
213
- The test suite injects a mock subagent runner, so it runs without a model. A
214
- faithful end-to-end check (real subagents) just calls `runWorkflow` with the
215
- default runner.
423
+ 项目源码位于:
424
+
425
+ - `extensions/ultracode.ts`:扩展入口和 Pi 生命周期接线;
426
+ - `src/mode.ts`:Ultracode 模式状态、恢复与强度管理;
427
+ - `src/thinking.ts`:共享思考强度词汇与兼容性检测;
428
+ - `src/workflow/`:脚本验证、运行时、子代理执行、worktree 和进度显示;
429
+ - `test/`:单元、扩展和回归测试;
430
+ - `docs/parity.md`:与原生 Pi 能力边界的说明。
431
+
432
+ 直接运行 Node 测试时没有 Pi 的模块加载器,需要把真实的 `acorn` 与 Pi 提供的 peer 包链接到 `node_modules/`;`npm run typecheck` 还要求本地可用的 TypeScript。通过 `pi install` 加载扩展时,Pi 会提供这些 peer 依赖。
433
+
434
+ ## 设计原则
435
+
436
+ Ultracode 的目标不是“尽可能多地启动代理”,而是:
216
437
 
217
- **Dependencies.** The only hard runtime dependency is `acorn`; Pi provides
218
- `typebox` and `@earendil-works/*` to extensions at load time. When running the
219
- node test runner directly (which has no Pi loader), link those peers into
220
- `node_modules/` — e.g. symlink them from your Pi install — alongside the
221
- real `acorn` that `pi install` / `npm install` provides.
438
+ > 在任务确实适合拆分、并行、独立验证或跨上下文扩展时,使用确定性编排获得更高置信度;否则继续使用 Pi 简洁直接的单代理循环。
222
439
 
223
- ## License
440
+ ## 许可证
224
441
 
225
442
  MIT