@cobia/pi-onward 0.5.2 → 0.5.4
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/CHANGELOG.md +12 -0
- package/README.md +28 -5
- package/package.json +3 -1
- package/src/actions/pi.ts +3 -3
- package/src/actions/registry.ts +7 -7
- package/src/extension/command-display.ts +217 -0
- package/src/extension/command.ts +210 -226
- package/src/extension/settings.ts +2 -4
- package/src/extension/text.ts +96 -85
- package/src/policy/config.ts +3 -3
- package/src/policy/delay.ts +4 -4
- package/src/policy/policy.ts +2 -2
- package/src/policy/rules.ts +20 -22
- package/src/runtime/recovery.ts +49 -44
- package/src/runtime/wait.ts +75 -0
package/src/runtime/recovery.ts
CHANGED
|
@@ -30,6 +30,7 @@ import { resolveLimits } from "../policy/policy.ts";
|
|
|
30
30
|
import { errorText, isRecord } from "../util.ts";
|
|
31
31
|
import { Episode } from "./episode.ts";
|
|
32
32
|
import { INPUT_RECORD, TaskPrompt } from "./task-prompt.ts";
|
|
33
|
+
import { waitForDelay } from "./wait.ts";
|
|
33
34
|
|
|
34
35
|
export interface RunState {
|
|
35
36
|
phase: "idle" | "waiting" | "submitted" | "retrying" | "queued" | "executing" | "stopped";
|
|
@@ -145,7 +146,14 @@ export class Recovery {
|
|
|
145
146
|
!isActive(this.settings.config)
|
|
146
147
|
)
|
|
147
148
|
return;
|
|
148
|
-
if (
|
|
149
|
+
if (
|
|
150
|
+
event.outcome === "aborted" ||
|
|
151
|
+
ctx.signal?.aborted ||
|
|
152
|
+
event.continue ||
|
|
153
|
+
ctx.hasPendingMessages() ||
|
|
154
|
+
event.context.pendingMessages.length
|
|
155
|
+
)
|
|
156
|
+
return;
|
|
149
157
|
const entry = event.context.contextEntries.at(-1);
|
|
150
158
|
const message = entry?.messages.at(-1);
|
|
151
159
|
if (!entry || !isFailure(message) || !this.episode.observe(entry.sourceEntry.id)) return;
|
|
@@ -161,11 +169,11 @@ export class Recovery {
|
|
|
161
169
|
const definition = this.registry.get(hit.stage.action.use)?.definition;
|
|
162
170
|
const problem = this.registry.check(hit.stage.action);
|
|
163
171
|
if (problem || !definition) {
|
|
164
|
-
this.finish(ctx, { kind: "failed", message: problem ?? "
|
|
172
|
+
this.finish(ctx, { kind: "failed", message: problem ?? "动作未加载" }, hit);
|
|
165
173
|
return;
|
|
166
174
|
}
|
|
167
175
|
if (!definition.modes.includes(ctx.mode)) {
|
|
168
|
-
this.finish(ctx, { kind: "failed", message: `${definition.name}
|
|
176
|
+
this.finish(ctx, { kind: "failed", message: `${definition.name} 不支持 ${ctx.mode} 模式` }, hit);
|
|
169
177
|
return;
|
|
170
178
|
}
|
|
171
179
|
const chain = this.episode.chain;
|
|
@@ -192,12 +200,18 @@ export class Recovery {
|
|
|
192
200
|
this.setState(ctx, { phase: "waiting", hit, invocationId: invocation.id });
|
|
193
201
|
const delay = retryDelay(this.episode.failures - hit.stage.minFailures + 1, hit.stage.delay);
|
|
194
202
|
this.notice(ctx, `${formatDelay(delay)}后执行 ${definition.name}(连续失败 ${this.episode.failures} 次)`);
|
|
195
|
-
await
|
|
203
|
+
const wait = await waitForDelay(delay, signal, invocation.sessionId);
|
|
196
204
|
if (!this.current(invocation, ctx)) return;
|
|
197
|
-
if (
|
|
205
|
+
if (wait === "aborted" || signal.aborted) {
|
|
198
206
|
this.cancel(ctx, "等待已取消");
|
|
199
207
|
return;
|
|
200
208
|
}
|
|
209
|
+
if (wait === "queued" || ctx.hasPendingMessages()) {
|
|
210
|
+
// Pi drains the queue. Only a consumed human task should reset the failure budget.
|
|
211
|
+
this.pending = undefined;
|
|
212
|
+
this.setState(ctx, { phase: "idle" });
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
201
215
|
if (definition.phase === "settled") {
|
|
202
216
|
this.setState(ctx, { phase: "queued", hit, invocationId: invocation.id });
|
|
203
217
|
return;
|
|
@@ -242,7 +256,7 @@ export class Recovery {
|
|
|
242
256
|
);
|
|
243
257
|
if (!command) {
|
|
244
258
|
this.pending = undefined;
|
|
245
|
-
this.finish(ctx, { kind: "failed", message: "
|
|
259
|
+
this.finish(ctx, { kind: "failed", message: "onward 的分派命令不可用" }, invocation.hit);
|
|
246
260
|
return;
|
|
247
261
|
}
|
|
248
262
|
// Pi defers this command until ALL agent_settled handlers finish.
|
|
@@ -345,21 +359,19 @@ export class Recovery {
|
|
|
345
359
|
return definition.execute(params, { ...context, signal });
|
|
346
360
|
if (definition.phase === "settled" && context.phase === "settled")
|
|
347
361
|
return definition.execute(params, { ...context, signal });
|
|
348
|
-
return { kind: "failed", message: "
|
|
362
|
+
return { kind: "failed", message: "动作的执行阶段与当前上下文不符" };
|
|
349
363
|
};
|
|
350
364
|
const result = await raceAbort(execute(), signal);
|
|
351
365
|
if (signal.aborted)
|
|
352
366
|
return {
|
|
353
367
|
kind: "unknown",
|
|
354
|
-
message: timeout.signal.aborted
|
|
355
|
-
? "Action timed out; outcome unknown; not replayed"
|
|
356
|
-
: "Execution cancelled; side-effect outcome unknown",
|
|
368
|
+
message: timeout.signal.aborted ? "动作执行超时,结果未知,不会重放" : "执行已取消,副作用是否发生未知",
|
|
357
369
|
};
|
|
358
370
|
if (!validResult(result, invocation.definition))
|
|
359
|
-
return { kind: "unknown", message: "
|
|
371
|
+
return { kind: "unknown", message: "动作返回了无效的结果,不会重放" };
|
|
360
372
|
return result;
|
|
361
373
|
} catch (error) {
|
|
362
|
-
return { kind: "unknown", message:
|
|
374
|
+
return { kind: "unknown", message: `动作抛出异常,结果未知:${errorText(error)}` };
|
|
363
375
|
} finally {
|
|
364
376
|
clearTimeout(timer);
|
|
365
377
|
}
|
|
@@ -414,28 +426,33 @@ export class Recovery {
|
|
|
414
426
|
}
|
|
415
427
|
export function stateText(state: RunState, failures: number): string | undefined {
|
|
416
428
|
if (state.phase === "idle") return undefined;
|
|
417
|
-
const phases = {
|
|
418
|
-
waiting: "⏳",
|
|
419
|
-
submitted: "⏳",
|
|
420
|
-
retrying: "⏩",
|
|
421
|
-
queued: "⏳",
|
|
422
|
-
executing: "🚀",
|
|
423
|
-
stopped: "🛑",
|
|
424
|
-
};
|
|
425
|
-
const results: Record<ActionResult["kind"], string> = {
|
|
426
|
-
continue: "⏩",
|
|
427
|
-
completed: "🎉",
|
|
428
|
-
dispatched: "📤",
|
|
429
|
-
handoff: "🔀",
|
|
430
|
-
stopped: "🛑",
|
|
431
|
-
failed: "💥",
|
|
432
|
-
cancelled: "🚫",
|
|
433
|
-
unknown: "🎲",
|
|
434
|
-
};
|
|
435
429
|
const result = state.phase === "stopped" ? state.result : undefined;
|
|
436
|
-
const icon = result ? results[result] : phases[state.phase];
|
|
437
430
|
const detail = result === "failed" ? " 动作失败" : result === "unknown" ? " 结果未知" : "";
|
|
438
|
-
return `${
|
|
431
|
+
return `${stateIcon(state)} onward${detail} · 失败${failures}`;
|
|
432
|
+
}
|
|
433
|
+
const PHASE_ICONS: Record<Exclude<RunState["phase"], "idle">, string> = {
|
|
434
|
+
waiting: "⏳",
|
|
435
|
+
submitted: "⏳",
|
|
436
|
+
retrying: "⏩",
|
|
437
|
+
queued: "⏳",
|
|
438
|
+
executing: "🚀",
|
|
439
|
+
stopped: "🛑",
|
|
440
|
+
};
|
|
441
|
+
const RESULT_ICONS: Record<ActionResult["kind"], string> = {
|
|
442
|
+
continue: "⏩",
|
|
443
|
+
completed: "🎉",
|
|
444
|
+
dispatched: "📤",
|
|
445
|
+
handoff: "🔀",
|
|
446
|
+
stopped: "🛑",
|
|
447
|
+
failed: "💥",
|
|
448
|
+
cancelled: "🚫",
|
|
449
|
+
unknown: "🎲",
|
|
450
|
+
};
|
|
451
|
+
/** 恢复进行中的图标:状态栏和菜单、status 标题共用,同一状态只有一个图标。 */
|
|
452
|
+
export function stateIcon(state: RunState): string {
|
|
453
|
+
if (state.phase === "idle") return "⏩";
|
|
454
|
+
const result = state.phase === "stopped" ? state.result : undefined;
|
|
455
|
+
return result ? RESULT_ICONS[result] : PHASE_ICONS[state.phase];
|
|
439
456
|
}
|
|
440
457
|
function validResult(value: unknown, definition: ActionDefinition): value is ActionResult {
|
|
441
458
|
if (!isRecord(value) || typeof value.kind !== "string") return false;
|
|
@@ -454,18 +471,6 @@ function validResult(value: unknown, definition: ActionDefinition): value is Act
|
|
|
454
471
|
return value.message === undefined || typeof value.message === "string";
|
|
455
472
|
return ["stopped", "failed", "cancelled", "unknown"].includes(value.kind) && typeof value.message === "string";
|
|
456
473
|
}
|
|
457
|
-
async function wait(ms: number, signal: AbortSignal): Promise<void> {
|
|
458
|
-
if (signal.aborted || ms <= 0) return;
|
|
459
|
-
await new Promise<void>((resolve) => {
|
|
460
|
-
const done = () => {
|
|
461
|
-
clearTimeout(timer);
|
|
462
|
-
signal.removeEventListener("abort", done);
|
|
463
|
-
resolve();
|
|
464
|
-
};
|
|
465
|
-
const timer = setTimeout(done, ms);
|
|
466
|
-
signal.addEventListener("abort", done, { once: true });
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
474
|
async function raceAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T | undefined> {
|
|
470
475
|
let abort: () => void = () => {};
|
|
471
476
|
try {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Agent } from "@earendil-works/pi-agent-core";
|
|
2
|
+
|
|
3
|
+
// Pi releases the turn signal before agent_before_settle. Its extension API has no
|
|
4
|
+
// event for a session abort or an accepted queue item at that boundary. Observe the
|
|
5
|
+
// three host operations only while a delay is outstanding; never interpret TUI input.
|
|
6
|
+
const WAITERS = Symbol.for("@cobia/pi-onward/boundary-waiters");
|
|
7
|
+
type Interruption = "aborted" | "queued";
|
|
8
|
+
type Wake = (reason: Interruption) => void;
|
|
9
|
+
type Waiters = { bySession: Map<string, Wake>; detach: () => void };
|
|
10
|
+
type ObservedAbort = Agent["abort"] & { [WAITERS]?: Waiters };
|
|
11
|
+
|
|
12
|
+
function observeHost(sessionId: string, wake: Wake): () => void {
|
|
13
|
+
const host = Agent.prototype;
|
|
14
|
+
let waiters = (host.abort as ObservedAbort)[WAITERS];
|
|
15
|
+
if (!waiters) {
|
|
16
|
+
const bySession = new Map<string, Wake>();
|
|
17
|
+
const observe = <A extends unknown[], T>(operation: (this: Agent, ...args: A) => T, reason: Interruption) =>
|
|
18
|
+
function (this: Agent, ...args: A): T {
|
|
19
|
+
const result = operation.apply(this, args);
|
|
20
|
+
const id = this.sessionId;
|
|
21
|
+
const wake = id && bySession.get(id);
|
|
22
|
+
if (wake && reason === "aborted") wake(reason);
|
|
23
|
+
else if (wake) {
|
|
24
|
+
// A producer can retract its input before returning control to Pi.
|
|
25
|
+
queueMicrotask(() => {
|
|
26
|
+
if (bySession.get(id) === wake && this.hasQueuedMessages()) wake("queued");
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
const { abort, steer, followUp } = host;
|
|
32
|
+
const onAbort = observe(abort, "aborted");
|
|
33
|
+
const onSteer = observe(steer, "queued");
|
|
34
|
+
const onFollowUp = observe(followUp, "queued");
|
|
35
|
+
waiters = {
|
|
36
|
+
bySession,
|
|
37
|
+
detach() {
|
|
38
|
+
// A later extension may have installed a wrapper of its own.
|
|
39
|
+
if (host.abort === onAbort) host.abort = abort;
|
|
40
|
+
if (host.steer === onSteer) host.steer = steer;
|
|
41
|
+
if (host.followUp === onFollowUp) host.followUp = followUp;
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
host.abort = Object.assign(onAbort, { [WAITERS]: waiters });
|
|
45
|
+
host.steer = onSteer;
|
|
46
|
+
host.followUp = onFollowUp;
|
|
47
|
+
}
|
|
48
|
+
waiters.bySession.set(sessionId, wake);
|
|
49
|
+
return () => {
|
|
50
|
+
if (waiters.bySession.get(sessionId) !== wake) return;
|
|
51
|
+
waiters.bySession.delete(sessionId);
|
|
52
|
+
if (!waiters.bySession.size) waiters.detach();
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function waitForDelay(
|
|
57
|
+
ms: number,
|
|
58
|
+
signal: AbortSignal,
|
|
59
|
+
sessionId: string,
|
|
60
|
+
): Promise<"elapsed" | Interruption> {
|
|
61
|
+
if (signal.aborted) return "aborted";
|
|
62
|
+
if (ms <= 0) return "elapsed";
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
const done = (result: "elapsed" | Interruption) => {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
signal.removeEventListener("abort", cancel);
|
|
67
|
+
unobserve();
|
|
68
|
+
resolve(result);
|
|
69
|
+
};
|
|
70
|
+
const cancel = () => done("aborted");
|
|
71
|
+
const unobserve = observeHost(sessionId, done);
|
|
72
|
+
const timer = setTimeout(() => done("elapsed"), ms);
|
|
73
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
74
|
+
});
|
|
75
|
+
}
|