@cobia/pi-onward 0.3.1 → 0.4.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 +186 -171
- package/docs/actions.md +97 -0
- package/example.json +18 -57
- package/onward.schema.json +86 -90
- package/package.json +9 -2
- package/src/actions/builtins.ts +25 -0
- package/src/actions/pi.ts +70 -0
- package/src/actions/protocol.ts +136 -0
- package/src/actions/registry.ts +112 -0
- package/src/extension/autocomplete.ts +1 -3
- package/src/extension/command.ts +157 -184
- package/src/extension/index.ts +1 -12
- package/src/extension/install.ts +10 -7
- package/src/extension/render.ts +2 -10
- package/src/extension/settings.ts +81 -77
- package/src/extension/text.ts +309 -438
- package/src/policy/config.ts +35 -85
- package/src/policy/delay.ts +10 -27
- package/src/policy/failure.ts +3 -31
- package/src/policy/policy.ts +63 -157
- package/src/policy/rules.ts +124 -101
- package/src/policy/schema.ts +65 -72
- package/src/runtime/episode.ts +19 -0
- package/src/runtime/recovery.ts +470 -0
- package/src/runtime/task-prompt.ts +82 -0
- package/src/extension/onward.ts +0 -208
- package/src/policy/tracker.ts +0 -64
package/src/extension/onward.ts
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 一轮以错误收尾、或者正常结束却是空回复,并且命中规则时,在 `agent_before_settle` 边界上等一会儿,再要 pi 接着跑:
|
|
3
|
-
* - message:追加一条看得见的消息(默认「继续」),模型照常读到;
|
|
4
|
-
* - resend:用 `context_edit` 把失败的那条从模型上下文里省略,重发同一个请求。失败的记录仍留在会话里。
|
|
5
|
-
*
|
|
6
|
-
* 等待和续跑都在这一轮之内完成,不靠落定后的定时器,所以 `pi -p`、RPC、subagent 子会话里一样生效。
|
|
7
|
-
*/
|
|
8
|
-
import type {
|
|
9
|
-
AgentBeforeSettleEvent,
|
|
10
|
-
AgentBeforeSettleEventResult,
|
|
11
|
-
ExtensionContext,
|
|
12
|
-
MessageEndEvent,
|
|
13
|
-
SessionBoundaryDraft,
|
|
14
|
-
} from "@earendil-works/pi-coding-agent";
|
|
15
|
-
import { type Hit, matchFailure } from "../policy/config.ts";
|
|
16
|
-
import { isSuccess } from "../policy/failure.ts";
|
|
17
|
-
import { renderPrompt } from "../policy/policy.ts";
|
|
18
|
-
import { type Progress, type Retry, Tracker } from "../policy/tracker.ts";
|
|
19
|
-
import type { Settings } from "./settings.ts";
|
|
20
|
-
import { errorExcerpt, giveUpNotice, giveUpStatus, retryNotice, retryStatus, say } from "./text.ts";
|
|
21
|
-
|
|
22
|
-
type Ctx = Pick<ExtensionContext, "sessionManager" | "hasUI" | "ui">;
|
|
23
|
-
|
|
24
|
-
/** 状态栏里本扩展的那一格。 */
|
|
25
|
-
const STATUS_KEY = "pi-onward";
|
|
26
|
-
|
|
27
|
-
/** 续跑消息的类型,render.ts 按它画。 */
|
|
28
|
-
export const CUSTOM_TYPE = "pi-onward";
|
|
29
|
-
|
|
30
|
-
/** 续跑消息带的 details:只给界面画标签,模型只读到 content。 */
|
|
31
|
-
export interface ContinueDetails {
|
|
32
|
-
rule: string;
|
|
33
|
-
attempt: number;
|
|
34
|
-
max: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** 怎么接着跑:resend 省略失败的那条重发,message 追加一条文字消息。 */
|
|
38
|
-
export type Continue =
|
|
39
|
-
| Extract<SessionBoundaryDraft, { type: "context_edit" }>
|
|
40
|
-
| (Extract<SessionBoundaryDraft, { type: "custom_message" }> & { content: string; details: ContinueDetails });
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 每个会话由哪个实例负责。同一个会话装了两份(例如 subagent 既按包加载、又按路径加载了一份副本),
|
|
44
|
-
* 两份各自计数:一份到上限停手,另一份照样续跑,互相接力永远停不下来。所以只让先到的那份干活;
|
|
45
|
-
* reload 和子会话销毁前 pi 都会发 session_shutdown,在那里交还。
|
|
46
|
-
*/
|
|
47
|
-
const shared = globalThis as typeof globalThis & { __piOnwardOwners?: Map<string, object> };
|
|
48
|
-
shared.__piOnwardOwners ??= new Map();
|
|
49
|
-
const owners = shared.__piOnwardOwners;
|
|
50
|
-
|
|
51
|
-
export class Onward {
|
|
52
|
-
private readonly settings: Settings;
|
|
53
|
-
private readonly tracker = new Tracker();
|
|
54
|
-
/**
|
|
55
|
-
* 接下来的 agent_start 是我们 `continue: true` 出来的。pi 会为它重新发 agent_start,
|
|
56
|
-
* 按新一轮清零的话计数永远到不了上限,会死循环。
|
|
57
|
-
*/
|
|
58
|
-
private continuing = false;
|
|
59
|
-
/** 正在等待的那次续跑;关掉、恢复默认、会话结束时打断它。 */
|
|
60
|
-
private waiting: AbortController | undefined;
|
|
61
|
-
/**
|
|
62
|
-
* 状态栏里写着什么:retrying 是第几次续跑,这一轮结束就清掉;gave-up 是停了,留到恢复或用户发新消息。
|
|
63
|
-
* 只有干活的那份会写,所以清的时候不用再问是谁的。
|
|
64
|
-
*/
|
|
65
|
-
private statusBar: "retrying" | "gave-up" | undefined;
|
|
66
|
-
|
|
67
|
-
constructor(settings: Settings) {
|
|
68
|
-
this.settings = settings;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
agentStart(ctx: Ctx): void {
|
|
72
|
-
if (!this.owns(ctx)) return;
|
|
73
|
-
if (!this.continuing) this.reset(ctx);
|
|
74
|
-
this.continuing = false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
messageEnd(event: MessageEndEvent, ctx: Ctx): void {
|
|
78
|
-
// 续跑之后只要正常返回过一次就算恢复了:后面再出错是新的一串,次数从头算。空回复不算正常返回。
|
|
79
|
-
if (this.owns(ctx) && isSuccess(event.message)) this.reset(ctx);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** 等待期间这一轮仍算忙:按 Esc 打断等待;用户输入的消息优先,我们让路。 */
|
|
83
|
-
async beforeSettle(
|
|
84
|
-
event: AgentBeforeSettleEvent,
|
|
85
|
-
ctx: ExtensionContext,
|
|
86
|
-
): Promise<AgentBeforeSettleEventResult | undefined> {
|
|
87
|
-
this.continuing = false;
|
|
88
|
-
if (!this.owns(ctx)) return undefined;
|
|
89
|
-
// 别的扩展已经要求继续:再插一条会改掉它要重发的内容。
|
|
90
|
-
if (event.outcome === "aborted" || event.continue || ctx.hasPendingMessages()) return undefined;
|
|
91
|
-
const entry = event.context.contextEntries.at(-1);
|
|
92
|
-
const hit = matchFailure(this.settings.config, entry?.messages.at(-1));
|
|
93
|
-
if (!entry || !hit) return undefined;
|
|
94
|
-
const step = this.advance(hit, ctx);
|
|
95
|
-
if (!step) return undefined;
|
|
96
|
-
const draft: Continue =
|
|
97
|
-
hit.policy.action === "resend"
|
|
98
|
-
? { type: "context_edit", targetId: entry.sourceEntry.id, replacement: null }
|
|
99
|
-
: {
|
|
100
|
-
type: "custom_message",
|
|
101
|
-
customType: CUSTOM_TYPE,
|
|
102
|
-
content: this.prompt(hit, step),
|
|
103
|
-
display: true,
|
|
104
|
-
details: { rule: hit.rule.name, attempt: step.attempt, max: hit.policy.maxRetries },
|
|
105
|
-
};
|
|
106
|
-
say(ctx, retryNotice(step, draft, hit.failure));
|
|
107
|
-
this.setStatus(ctx, "retrying", retryStatus(hit, step));
|
|
108
|
-
if (await this.wait(step.delay, ctx.signal)) return undefined;
|
|
109
|
-
// 等的时候用户发了消息:pi 会接着处理它,这算用户开的新一轮。
|
|
110
|
-
if (ctx.hasPendingMessages()) {
|
|
111
|
-
this.reset(ctx);
|
|
112
|
-
return undefined;
|
|
113
|
-
}
|
|
114
|
-
this.continuing = true;
|
|
115
|
-
// 边界结果会整体替换前面扩展提出的条目,所以带上它们。
|
|
116
|
-
return { entries: [...event.entries, draft], continue: true };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 这一轮彻底结束了;pi 不接受续跑时,`continuing` 不能留到下一轮。没有接着跑(按了 Esc、错误没命中规则),
|
|
121
|
-
* 「第几次」就不再成立;停了的提示留着。
|
|
122
|
-
*/
|
|
123
|
-
settled(ctx: Ctx): void {
|
|
124
|
-
this.continuing = false;
|
|
125
|
-
if (this.statusBar === "retrying") this.setStatus(ctx, undefined);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** 关掉、恢复默认时:打断正在等的续跑,清零计数。 */
|
|
129
|
-
stop(ctx: Ctx): void {
|
|
130
|
-
this.waiting?.abort();
|
|
131
|
-
this.reset(ctx);
|
|
132
|
-
this.continuing = false;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
shutdown(ctx: Ctx): void {
|
|
136
|
-
this.stop(ctx);
|
|
137
|
-
const session = ctx.sessionManager.getSessionId();
|
|
138
|
-
if (owners.get(session) === this) owners.delete(session);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
progress(): Progress {
|
|
142
|
-
return this.tracker.progress();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private owns(ctx: Ctx): boolean {
|
|
146
|
-
const session = ctx.sessionManager.getSessionId();
|
|
147
|
-
const owner = owners.get(session);
|
|
148
|
-
if (owner === undefined) owners.set(session, this);
|
|
149
|
-
return owner === undefined || owner === this;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** 再试就返回这一步;该停了就提醒并返回 undefined。 */
|
|
153
|
-
private advance(hit: Hit, ctx: ExtensionContext): Retry | undefined {
|
|
154
|
-
const step = this.tracker.next(hit.rule.name, hit.failure, hit.policy);
|
|
155
|
-
if (step.retry) return step;
|
|
156
|
-
if (step.notify) say(ctx, giveUpNotice(step));
|
|
157
|
-
this.setStatus(ctx, "gave-up", giveUpStatus(hit, step));
|
|
158
|
-
return undefined;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** 计数从头算,状态栏也清掉。 */
|
|
162
|
-
private reset(ctx: Ctx): void {
|
|
163
|
-
this.tracker.reset();
|
|
164
|
-
if (this.statusBar !== undefined) this.setStatus(ctx, undefined);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** 没有界面时(`pi -p`)不写;RPC 下 pi 把它发给客户端。 */
|
|
168
|
-
private setStatus(ctx: Ctx, status: "retrying" | "gave-up" | undefined, text?: string): void {
|
|
169
|
-
this.statusBar = status;
|
|
170
|
-
if (ctx.hasUI) ctx.ui.setStatus(STATUS_KEY, text);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private prompt(hit: Hit, step: Retry): string {
|
|
174
|
-
return renderPrompt(hit.policy.prompt, {
|
|
175
|
-
attempt: step.attempt,
|
|
176
|
-
max: hit.policy.maxRetries,
|
|
177
|
-
error: errorExcerpt(hit.failure, 120),
|
|
178
|
-
rule: hit.rule.name,
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** 返回 true 表示被打断。 */
|
|
183
|
-
private async wait(ms: number, signal: AbortSignal | undefined): Promise<boolean> {
|
|
184
|
-
const waiting = new AbortController();
|
|
185
|
-
this.waiting = waiting;
|
|
186
|
-
try {
|
|
187
|
-
return await sleep(ms, signal ? AbortSignal.any([signal, waiting.signal]) : waiting.signal);
|
|
188
|
-
} finally {
|
|
189
|
-
if (this.waiting === waiting) this.waiting = undefined;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/** 不 unref:等待期间这一轮还忙,进程不该因为只剩它在等就退出。 */
|
|
195
|
-
function sleep(ms: number, signal: AbortSignal): Promise<boolean> {
|
|
196
|
-
if (signal.aborted) return Promise.resolve(true);
|
|
197
|
-
if (ms <= 0) return Promise.resolve(false);
|
|
198
|
-
return new Promise((resolve) => {
|
|
199
|
-
const finish = (aborted: boolean): void => {
|
|
200
|
-
clearTimeout(timer);
|
|
201
|
-
signal.removeEventListener("abort", onAbort);
|
|
202
|
-
resolve(aborted);
|
|
203
|
-
};
|
|
204
|
-
const onAbort = (): void => finish(true);
|
|
205
|
-
const timer = setTimeout(() => finish(false), ms);
|
|
206
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
207
|
-
});
|
|
208
|
-
}
|
package/src/policy/tracker.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 上次正常返回之后的连续失败计数。每条规则各自计次数,预算互不挤占;同一个错误的连续次数不分规则:
|
|
3
|
-
* 不管落到哪条规则,同一个错误连着出现都说明没恢复。扩展在正常返回或用户开新一轮时 reset。
|
|
4
|
-
*/
|
|
5
|
-
import { retryDelay } from "./delay.ts";
|
|
6
|
-
import { errorSignature, type Failure } from "./failure.ts";
|
|
7
|
-
import type { Policy } from "./policy.ts";
|
|
8
|
-
|
|
9
|
-
/** 为什么不再续跑:规则说停、同一个错误太多次、次数用完。 */
|
|
10
|
-
export type GiveUpReason = "stop" | "same-error" | "budget";
|
|
11
|
-
|
|
12
|
-
export type Retry = { retry: true; attempt: number; delay: number };
|
|
13
|
-
export type GiveUp = { retry: false; reason: GiveUpReason; attempt: number; streak: number; notify: boolean };
|
|
14
|
-
export type Step = Retry | GiveUp;
|
|
15
|
-
|
|
16
|
-
/** 这一串失败里一共续跑了几次,同一个错误连续几次。 */
|
|
17
|
-
export interface Progress {
|
|
18
|
-
used: number;
|
|
19
|
-
streak: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class Tracker {
|
|
23
|
-
private readonly attempts = new Map<string, number>();
|
|
24
|
-
private streak = 0;
|
|
25
|
-
private signature: string | undefined;
|
|
26
|
-
private warned = false;
|
|
27
|
-
|
|
28
|
-
/** 记一次失败,给出下一步:等多久再试,还是停。 */
|
|
29
|
-
next(ruleName: string, failure: Failure, policy: Policy): Step {
|
|
30
|
-
const signature = errorSignature(failure);
|
|
31
|
-
this.streak = signature === this.signature ? this.streak + 1 : 1;
|
|
32
|
-
this.signature = signature;
|
|
33
|
-
const used = this.attempts.get(ruleName) ?? 0;
|
|
34
|
-
const reason = giveUpReason(used, this.streak, policy);
|
|
35
|
-
if (reason) {
|
|
36
|
-
// 停手只提醒一次,恢复之前再撞到也不刷屏。
|
|
37
|
-
const notify = !this.warned;
|
|
38
|
-
this.warned = true;
|
|
39
|
-
return { retry: false, reason, attempt: used, streak: this.streak, notify };
|
|
40
|
-
}
|
|
41
|
-
this.attempts.set(ruleName, used + 1);
|
|
42
|
-
return { retry: true, attempt: used + 1, delay: retryDelay(used + 1, policy.delay) };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
reset(): void {
|
|
46
|
-
this.attempts.clear();
|
|
47
|
-
this.streak = 0;
|
|
48
|
-
this.signature = undefined;
|
|
49
|
-
this.warned = false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
progress(): Progress {
|
|
53
|
-
let used = 0;
|
|
54
|
-
for (const n of this.attempts.values()) used += n;
|
|
55
|
-
return { used, streak: this.streak };
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function giveUpReason(used: number, streak: number, policy: Policy): GiveUpReason | undefined {
|
|
60
|
-
if (policy.action === "stop") return "stop";
|
|
61
|
-
if (streak > policy.maxSameError) return "same-error";
|
|
62
|
-
if (used >= policy.maxRetries) return "budget";
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|