@ai-zen/agents-sdk 1.0.0 → 1.1.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.
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
+
## [1.1.0] - 2026-11-09
|
|
2
|
+
|
|
3
|
+
> Cancelling a handoff now means "go back to how it was". Before this release `Ctrl+C` only
|
|
4
|
+
> stopped the work: the handoff turn — a multi-KB skill instruction, its tool results and the
|
|
5
|
+
> acknowledgement — stayed in `agent.messages`, so the CLI showed it in the transcript, `↑`
|
|
6
|
+
> history offered it back as a draft, and the next request resent it as conversation history.
|
|
7
|
+
>
|
|
8
|
+
> Worse, on the auto-migration path the interrupt was not honoured at all: `AutoMigratePlugin`
|
|
9
|
+
> drives `migrate()` directly, so a caller-supplied `signal` never reached the service. The
|
|
10
|
+
> aborted `self` round was then read as "the model never wrote the document", burning one retry
|
|
11
|
+
> and one `summary` round — and then migrating anyway.
|
|
12
|
+
>
|
|
13
|
+
> Requires `@ai-zen/agents-core@^4.5.4`. Not a breaking change: the defaults of `handoff()` /
|
|
14
|
+
> `migrate()` are untouched and an external `signal` keeps working.
|
|
15
|
+
|
|
16
|
+
### ✨ Cancellation belongs to the service
|
|
17
|
+
|
|
18
|
+
- **`HandoffService.abort()` (new public method)** — the service owns the cancel handle of the
|
|
19
|
+
run it is executing and merges it with any caller-supplied `signal` (`AbortSignal.any`), so
|
|
20
|
+
`/handoff`, `/migrate` **and auto-migration** share one switch. On an idle service it is a
|
|
21
|
+
no-op (`null` handle), which is what keeps a stale `abort()` from "succeeding" silently later.
|
|
22
|
+
- **Cancel means roll back** — `agent.messages` is truncated back to the length it had before
|
|
23
|
+
the handoff turn, a document created by this run is deleted, and `HandoffAbortedError` is
|
|
24
|
+
thrown. No retry, no degradation, no migration. The cleanup lives in one place so it also
|
|
25
|
+
covers the state that no per-stage `catch` used to see: document written, not yet validated.
|
|
26
|
+
- **Commit point** — once the document passes validation (`isHandoffFileReady`) the run is
|
|
27
|
+
committed: saving the old conversation, pruning history and persisting the new session no
|
|
28
|
+
longer react to cancellation. Stopping there would leave a third state ("user cancelled, old
|
|
29
|
+
conversation archived, history already pruned"), which is worse than finishing.
|
|
30
|
+
- **A cancelled auto-migration is no longer logged as a failure** — `AutoMigratePlugin`
|
|
31
|
+
swallows `HandoffAbortedError` instead of writing "migration failed" through the logger
|
|
32
|
+
(default `console.error`, i.e. a torn frame in a TUI).
|
|
33
|
+
|
|
34
|
+
### 🧪 Tests
|
|
35
|
+
|
|
36
|
+
- 635 unit cases in 53 files — 7 new: service-side `abort()`, message rollback, document removal
|
|
37
|
+
after a late cancel, commit-point semantics, idle `abort()` as a no-op, a `summary` request
|
|
38
|
+
interrupted through the service handle, and the silent cancel in the plugin. `tsc --noEmit`
|
|
39
|
+
is clean for both the production and the test config.
|
|
40
|
+
|
|
1
41
|
## [1.0.0] - 2026-11-03
|
|
2
42
|
|
|
3
43
|
> First stable release. The whole `1.0.0-alpha.x` line — up to and including `alpha.26` — is
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { HandoffAbortedError } from "../runtime/HandoffService.js";
|
|
1
2
|
import { getLogger } from "../shared/logger.js";
|
|
2
3
|
/**
|
|
3
4
|
* 自动迁移插件 —— 只负责「何时触发」,实际迁移统一委托给持有的 `HandoffService`。
|
|
@@ -41,6 +42,10 @@ export class AutoMigratePlugin {
|
|
|
41
42
|
await service.migrate({ agent, promptTokens, handoffTokens, mode: mode ?? undefined });
|
|
42
43
|
}
|
|
43
44
|
catch (err) {
|
|
45
|
+
// 用户主动取消不是失败:服务已把会话退回交接前,这里再报错只会在界面上
|
|
46
|
+
// 凭空多一条「迁移失败」(默认 logger 是 console.error,TUI 下直接撕画面)
|
|
47
|
+
if (err instanceof HandoffAbortedError)
|
|
48
|
+
return;
|
|
44
49
|
getLogger().error(`[autoMigrate] migration failed: ${err?.message ?? err}`);
|
|
45
50
|
}
|
|
46
51
|
}
|
|
@@ -124,6 +124,11 @@ export interface MigrateParams extends HandoffParams {
|
|
|
124
124
|
* 重入:`handoff()` 与 `migrate()` 共用 `busy` 标志。交接回合本身也是一次 `send()`,
|
|
125
125
|
* 若不在 busy 期间让 `AutoMigratePlugin` / `ContextGuardPlugin` 让行,就会「交接触发迁移、
|
|
126
126
|
* 迁移再触发交接」无限递归,或在最满的时刻被护栏直接击杀。
|
|
127
|
+
*
|
|
128
|
+
* 取消:取消句柄由服务自己持有({@link HandoffService.abort}),而不是只认调用方传进来的
|
|
129
|
+
* `signal` —— 自动迁移由 `AutoMigratePlugin` 直接驱动,调用方根本没有机会把 signal 递进来,
|
|
130
|
+
* 只认外部 signal 的后果是「自动迁移期间按 Ctrl+C 会被当成『文档没写出来』,于是重试一轮、
|
|
131
|
+
* 再降级 summary 一轮,最后照样迁移」。取消后一律回到交接前的状态(见 {@link HandoffService.restoreBeforeHandoff})。
|
|
127
132
|
*/
|
|
128
133
|
export declare class HandoffService {
|
|
129
134
|
/** 交接文档兜底模板(仅在读不到 Skill 时使用) */
|
|
@@ -142,6 +147,13 @@ export declare class HandoffService {
|
|
|
142
147
|
private readonly logger;
|
|
143
148
|
/** 交接/迁移进行中标志(插件据此让行) */
|
|
144
149
|
private _busy;
|
|
150
|
+
/**
|
|
151
|
+
* 当前这次交接/迁移的取消句柄(`null` = 没有进行中的交接)。
|
|
152
|
+
*
|
|
153
|
+
* 三条链路(`/handoff`、`/migrate`、自动迁移)共用同一个开关:前两条还有调用方能
|
|
154
|
+
* 传 `signal`,自动迁移没有 —— 句柄放在服务里,三者的取消语义才不会各不相同。
|
|
155
|
+
*/
|
|
156
|
+
private abortController;
|
|
145
157
|
/** 交接回合的工具守卫(同一实例复用 + arm/disarm,避免插件数组无限增长) */
|
|
146
158
|
private guard?;
|
|
147
159
|
/** 已注册过守卫的 agent(每个 agent 只 use 一次) */
|
|
@@ -179,11 +191,21 @@ export declare class HandoffService {
|
|
|
179
191
|
static createPostMessages(handoffPath: string): AgentNS.Message[];
|
|
180
192
|
/** 序列化消息为 `[role]: content` 文本(仅 `summary` 模式使用) */
|
|
181
193
|
static serializeMessages(messages: AgentNS.Message[]): string;
|
|
194
|
+
/**
|
|
195
|
+
* 取消进行中的交接/迁移(用户按 Ctrl+C)。没有进行中的交接时是 no-op。
|
|
196
|
+
*
|
|
197
|
+
* 边界:本方法只保证「停下 + 回退」——不再重试、不降级 summary、不迁移,并把会话
|
|
198
|
+
* 退回交接前。**它不负责立刻掐断正在跑的模型请求**:`self` 模式的回合是
|
|
199
|
+
* `agent.send()`,调用方通常要同时 `agent.abort()`(TUI 就是这么做的);`summary`
|
|
200
|
+
* 模式的那次请求拿的是本服务合并出的 signal,会被真正中断。
|
|
201
|
+
*/
|
|
202
|
+
abort(): void;
|
|
182
203
|
/**
|
|
183
204
|
* 只产出交接文档,**不改动会话**(`/handoff`)。
|
|
184
205
|
*
|
|
185
206
|
* 不清理历史:交接回合(指令 + 工具调用 + 回执)会留在 `agent.messages` 里,
|
|
186
|
-
* 用户可继续让 agent
|
|
207
|
+
* 用户可继续让 agent 打磨这份文档(重写同一路径即可)。被取消时相反:整个交接
|
|
208
|
+
* 回合从 `agent.messages` 里截掉,本次新建的文档也不留。
|
|
187
209
|
*/
|
|
188
210
|
handoff(params: HandoffParams): Promise<HandoffContext>;
|
|
189
211
|
/**
|
|
@@ -194,16 +216,13 @@ export declare class HandoffService {
|
|
|
194
216
|
* - `prune`:物理剔除历史,只留 `definition.messages` + 交接断点。
|
|
195
217
|
*/
|
|
196
218
|
migrate(params: MigrateParams): Promise<HandoffContext>;
|
|
197
|
-
/** 串行化 + 防重入:交接与迁移共用同一把锁 */
|
|
198
|
-
private withBusy;
|
|
199
219
|
/**
|
|
200
|
-
*
|
|
220
|
+
* 串行化 + 防重入:交接与迁移共用同一把锁,也共用同一个取消句柄。
|
|
201
221
|
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
* `readSkillContent` 这类前置 await 期间被取消。调用方只需要认一个类型。
|
|
222
|
+
* 句柄与 `busy` 同寿命:`finally` 里清掉,否则下一次 `abort()` 会往一个已结束的
|
|
223
|
+
* controller 上发出无意义的 abort(看起来「取消成功」,实际什么都没发生)。
|
|
205
224
|
*/
|
|
206
|
-
private
|
|
225
|
+
private withBusy;
|
|
207
226
|
/** 取消点检查:每跨过一个阶段就确认一次,避免取消后继续往下一步冲 */
|
|
208
227
|
private throwIfAborted;
|
|
209
228
|
private fileExists;
|
|
@@ -215,7 +234,27 @@ export declare class HandoffService {
|
|
|
215
234
|
* 留着半成品更糟 —— 下一轮接手方会把不完整的内容当成真实交接读进去。
|
|
216
235
|
*/
|
|
217
236
|
private discardPartialFile;
|
|
237
|
+
/**
|
|
238
|
+
* 跑完一次交接("/handoff" 与 "/migrate" 共用),取消时**由本方法统一回退**。
|
|
239
|
+
*
|
|
240
|
+
* 为什么回退只写在这一处:取消可能从任何一个阶段冒出来(`self` 回合后的主动检查、
|
|
241
|
+
* `summary` 请求被 abort、前置的 `readSkillContent`),分散到各自的 catch 里就会
|
|
242
|
+
* 漏掉「文档已落盘但还没校验通过」这种半成品状态。这里一次拿全三件事:
|
|
243
|
+
* 交接前的消息条数、文档路径、以及「文档本就不存在」这个事实。
|
|
244
|
+
*
|
|
245
|
+
* 取消形态有两类,都要认:{@link HandoffAbortedError}(主动检查)与底层 abort 错误
|
|
246
|
+
* (OpenAI 的 `APIUserAbortError`)。调用方只需要认前者。
|
|
247
|
+
*/
|
|
218
248
|
private runHandoff;
|
|
249
|
+
/**
|
|
250
|
+
* 取消后回到交接前的状态:把交接回合追加的消息全部截掉,并删掉本次新建的文档。
|
|
251
|
+
*
|
|
252
|
+
* 为什么必须截消息:`self` 模式的交接回合是「一条几 KB 的 Skill 指令 user 消息 +
|
|
253
|
+
* 工具结果 + 回执」,留在 `agent.messages` 里既让用户看到半段无关对话,也让下一次
|
|
254
|
+
* 请求把它当历史再发一遍(`↑` 翻历史还会把它翻出来)。`agent.messages` 是唯一真相源,
|
|
255
|
+
* 截回原长度之后,转录、历史、用量全都自动复位,不需要第二处补偿。
|
|
256
|
+
*/
|
|
257
|
+
private restoreBeforeHandoff;
|
|
219
258
|
/** 自交接是否可用:需要 `write` 工具 + `send` 能力 */
|
|
220
259
|
private supportsSelfHandoff;
|
|
221
260
|
/**
|
|
@@ -230,6 +269,9 @@ export declare class HandoffService {
|
|
|
230
269
|
* `agent.abort()` 不抛错(core 把消息标记为 Aborted 后正常返回),否则「用户
|
|
231
270
|
* 按了 Ctrl+C」会被下面的文件校验误判成「模型没写出文档」,于是重试一轮、再降级
|
|
232
271
|
* 一轮 —— 用户以为取消了,实际多烧了两轮全量输入。
|
|
272
|
+
*
|
|
273
|
+
* 取消的善后(删文档、截消息)不在这里做:交给 {@link runHandoff} 的 catch 一处处理,
|
|
274
|
+
* 因为「文档已写出但还没校验」的取消根本进不到本方法的 catch。
|
|
233
275
|
*/
|
|
234
276
|
private runSelfHandoff;
|
|
235
277
|
/** 独立摘要:一次模型调用,用同一份 Skill 正文作为规范 */
|
|
@@ -34,6 +34,16 @@ function isAbortError(err, signal) {
|
|
|
34
34
|
const name = err?.name;
|
|
35
35
|
return name === "AbortError" || name === "APIUserAbortError";
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* 合并两条取消信号:任一触发即视为取消。
|
|
39
|
+
*
|
|
40
|
+
* 服务自身的 `abort()`(见 {@link HandoffService.abort})与调用方传入的 `signal`
|
|
41
|
+
* 都要认:前者是自动迁移唯一的取消入口(那条链路调用方递不进 signal),后者留给
|
|
42
|
+
* 「外层还有自己的超时 / 取消」的调用方。
|
|
43
|
+
*/
|
|
44
|
+
function mergeSignals(own, external) {
|
|
45
|
+
return external ? AbortSignal.any([own, external]) : own;
|
|
46
|
+
}
|
|
37
47
|
/**
|
|
38
48
|
* 任务交接服务。
|
|
39
49
|
*
|
|
@@ -47,6 +57,11 @@ function isAbortError(err, signal) {
|
|
|
47
57
|
* 重入:`handoff()` 与 `migrate()` 共用 `busy` 标志。交接回合本身也是一次 `send()`,
|
|
48
58
|
* 若不在 busy 期间让 `AutoMigratePlugin` / `ContextGuardPlugin` 让行,就会「交接触发迁移、
|
|
49
59
|
* 迁移再触发交接」无限递归,或在最满的时刻被护栏直接击杀。
|
|
60
|
+
*
|
|
61
|
+
* 取消:取消句柄由服务自己持有({@link HandoffService.abort}),而不是只认调用方传进来的
|
|
62
|
+
* `signal` —— 自动迁移由 `AutoMigratePlugin` 直接驱动,调用方根本没有机会把 signal 递进来,
|
|
63
|
+
* 只认外部 signal 的后果是「自动迁移期间按 Ctrl+C 会被当成『文档没写出来』,于是重试一轮、
|
|
64
|
+
* 再降级 summary 一轮,最后照样迁移」。取消后一律回到交接前的状态(见 {@link HandoffService.restoreBeforeHandoff})。
|
|
50
65
|
*/
|
|
51
66
|
export class HandoffService {
|
|
52
67
|
/** 交接文档兜底模板(仅在读不到 Skill 时使用) */
|
|
@@ -65,6 +80,13 @@ export class HandoffService {
|
|
|
65
80
|
logger;
|
|
66
81
|
/** 交接/迁移进行中标志(插件据此让行) */
|
|
67
82
|
_busy = false;
|
|
83
|
+
/**
|
|
84
|
+
* 当前这次交接/迁移的取消句柄(`null` = 没有进行中的交接)。
|
|
85
|
+
*
|
|
86
|
+
* 三条链路(`/handoff`、`/migrate`、自动迁移)共用同一个开关:前两条还有调用方能
|
|
87
|
+
* 传 `signal`,自动迁移没有 —— 句柄放在服务里,三者的取消语义才不会各不相同。
|
|
88
|
+
*/
|
|
89
|
+
abortController = null;
|
|
68
90
|
/** 交接回合的工具守卫(同一实例复用 + arm/disarm,避免插件数组无限增长) */
|
|
69
91
|
guard;
|
|
70
92
|
/** 已注册过守卫的 agent(每个 agent 只 use 一次) */
|
|
@@ -169,14 +191,26 @@ export class HandoffService {
|
|
|
169
191
|
// ==================================================================
|
|
170
192
|
// 公开流程
|
|
171
193
|
// ==================================================================
|
|
194
|
+
/**
|
|
195
|
+
* 取消进行中的交接/迁移(用户按 Ctrl+C)。没有进行中的交接时是 no-op。
|
|
196
|
+
*
|
|
197
|
+
* 边界:本方法只保证「停下 + 回退」——不再重试、不降级 summary、不迁移,并把会话
|
|
198
|
+
* 退回交接前。**它不负责立刻掐断正在跑的模型请求**:`self` 模式的回合是
|
|
199
|
+
* `agent.send()`,调用方通常要同时 `agent.abort()`(TUI 就是这么做的);`summary`
|
|
200
|
+
* 模式的那次请求拿的是本服务合并出的 signal,会被真正中断。
|
|
201
|
+
*/
|
|
202
|
+
abort() {
|
|
203
|
+
this.abortController?.abort();
|
|
204
|
+
}
|
|
172
205
|
/**
|
|
173
206
|
* 只产出交接文档,**不改动会话**(`/handoff`)。
|
|
174
207
|
*
|
|
175
208
|
* 不清理历史:交接回合(指令 + 工具调用 + 回执)会留在 `agent.messages` 里,
|
|
176
|
-
* 用户可继续让 agent
|
|
209
|
+
* 用户可继续让 agent 打磨这份文档(重写同一路径即可)。被取消时相反:整个交接
|
|
210
|
+
* 回合从 `agent.messages` 里截掉,本次新建的文档也不留。
|
|
177
211
|
*/
|
|
178
212
|
async handoff(params) {
|
|
179
|
-
return this.withBusy(() => this.
|
|
213
|
+
return this.withBusy((own) => this.runHandoff({ ...params, signal: mergeSignals(own, params.signal) }, false));
|
|
180
214
|
}
|
|
181
215
|
/**
|
|
182
216
|
* 交接 + 开新会话(`/migrate`、自动迁移)。
|
|
@@ -186,8 +220,11 @@ export class HandoffService {
|
|
|
186
220
|
* - `prune`:物理剔除历史,只留 `definition.messages` + 交接断点。
|
|
187
221
|
*/
|
|
188
222
|
async migrate(params) {
|
|
189
|
-
return this.withBusy(async () => {
|
|
190
|
-
|
|
223
|
+
return this.withBusy(async (own) => {
|
|
224
|
+
// 拿到 ctx 即「已提交」:文档已通过校验,取消不再生效(见 runHandoff 末尾的取消点)。
|
|
225
|
+
// 在 prune 之前再拦一次取消会留下第三种状态 ——「用户点了取消,旧会话却已存档、
|
|
226
|
+
// 历史却已剔除」,比让迁移做完更糟
|
|
227
|
+
const ctx = await this.runHandoff({ ...params, signal: mergeSignals(own, params.signal) }, true);
|
|
191
228
|
const { agent } = ctx;
|
|
192
229
|
const strategy = params.strategy ?? this.defaultStrategy;
|
|
193
230
|
const breakpointMessages = HandoffService.createPostMessages(ctx.handoffPath);
|
|
@@ -215,35 +252,25 @@ export class HandoffService {
|
|
|
215
252
|
// ==================================================================
|
|
216
253
|
// 内部
|
|
217
254
|
// ==================================================================
|
|
218
|
-
/**
|
|
255
|
+
/**
|
|
256
|
+
* 串行化 + 防重入:交接与迁移共用同一把锁,也共用同一个取消句柄。
|
|
257
|
+
*
|
|
258
|
+
* 句柄与 `busy` 同寿命:`finally` 里清掉,否则下一次 `abort()` 会往一个已结束的
|
|
259
|
+
* controller 上发出无意义的 abort(看起来「取消成功」,实际什么都没发生)。
|
|
260
|
+
*/
|
|
219
261
|
async withBusy(fn) {
|
|
220
262
|
if (this._busy)
|
|
221
263
|
throw new Error("A handoff is already in progress, wait for it to finish");
|
|
222
264
|
this._busy = true;
|
|
265
|
+
const controller = new AbortController();
|
|
266
|
+
this.abortController = controller;
|
|
223
267
|
try {
|
|
224
|
-
return await fn();
|
|
268
|
+
return await fn(controller.signal);
|
|
225
269
|
}
|
|
226
270
|
finally {
|
|
227
271
|
this._busy = false;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 把底层各种「取消」形态统一成 {@link HandoffAbortedError}。
|
|
232
|
-
*
|
|
233
|
-
* 取消可能从三处冒上来:`self` 回合后的主动检查(已是 HandoffAbortedError)、
|
|
234
|
-
* `summary` 请求被 abort 时 OpenAI SDK 抛的 `APIUserAbortError`、以及
|
|
235
|
-
* `readSkillContent` 这类前置 await 期间被取消。调用方只需要认一个类型。
|
|
236
|
-
*/
|
|
237
|
-
async runCatchingAbort(params, migrating) {
|
|
238
|
-
try {
|
|
239
|
-
return await this.runHandoff(params, migrating);
|
|
240
|
-
}
|
|
241
|
-
catch (err) {
|
|
242
|
-
if (err instanceof HandoffAbortedError)
|
|
243
|
-
throw err;
|
|
244
|
-
if (isAbortError(err, params.signal))
|
|
245
|
-
throw new HandoffAbortedError();
|
|
246
|
-
throw err;
|
|
272
|
+
if (this.abortController === controller)
|
|
273
|
+
this.abortController = null;
|
|
247
274
|
}
|
|
248
275
|
}
|
|
249
276
|
/** 取消点检查:每跨过一个阶段就确认一次,避免取消后继续往下一步冲 */
|
|
@@ -277,64 +304,101 @@ export class HandoffService {
|
|
|
277
304
|
/* 文件本就没写出来:忽略 */
|
|
278
305
|
}
|
|
279
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* 跑完一次交接("/handoff" 与 "/migrate" 共用),取消时**由本方法统一回退**。
|
|
309
|
+
*
|
|
310
|
+
* 为什么回退只写在这一处:取消可能从任何一个阶段冒出来(`self` 回合后的主动检查、
|
|
311
|
+
* `summary` 请求被 abort、前置的 `readSkillContent`),分散到各自的 catch 里就会
|
|
312
|
+
* 漏掉「文档已落盘但还没校验通过」这种半成品状态。这里一次拿全三件事:
|
|
313
|
+
* 交接前的消息条数、文档路径、以及「文档本就不存在」这个事实。
|
|
314
|
+
*
|
|
315
|
+
* 取消形态有两类,都要认:{@link HandoffAbortedError}(主动检查)与底层 abort 错误
|
|
316
|
+
* (OpenAI 的 `APIUserAbortError`)。调用方只需要认前者。
|
|
317
|
+
*/
|
|
280
318
|
async runHandoff(params, migrating) {
|
|
281
319
|
const { agent, promptTokens, handoffTokens, signal } = params;
|
|
282
320
|
const requestedMode = params.mode ?? this.defaultMode;
|
|
283
321
|
const handoffPath = join(this.handoffDirPath, HandoffService.createHandoffFileName(new Date()));
|
|
284
322
|
const model = this.model ?? agent.model;
|
|
285
323
|
const messageCountBefore = agent.messages.length;
|
|
286
|
-
//
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
if (!ok) {
|
|
312
|
-
this.logger.warn("[handoff] self handoff failed, falling back to standalone summary mode");
|
|
324
|
+
// 只删「本次新建」的文档:交接文件名含时间戳,正常不会撞已有文件,但调用方也可以
|
|
325
|
+
// 把 `handoffDir` 指向人工目录,不管怎样都不该删掉别人的文件
|
|
326
|
+
const existedBefore = await this.fileExists(handoffPath);
|
|
327
|
+
try {
|
|
328
|
+
// 迁移前上下文(不含文档,语义固定)
|
|
329
|
+
const beforeCtx = {
|
|
330
|
+
agent,
|
|
331
|
+
mode: requestedMode,
|
|
332
|
+
requestedMode,
|
|
333
|
+
handoffPath,
|
|
334
|
+
model,
|
|
335
|
+
promptTokens,
|
|
336
|
+
handoffTokens,
|
|
337
|
+
migrating,
|
|
338
|
+
messageCountBefore,
|
|
339
|
+
historyText: "",
|
|
340
|
+
};
|
|
341
|
+
await this.safeHook(this.onBeforeHandoff, beforeCtx);
|
|
342
|
+
this.throwIfAborted(signal);
|
|
343
|
+
// ---- 执行者:self 优先,失败/不可用则单向降级 summary ----
|
|
344
|
+
let mode = requestedMode;
|
|
345
|
+
let handoffDoc;
|
|
346
|
+
let historyText = "";
|
|
347
|
+
if (mode === "self" && !this.supportsSelfHandoff(agent)) {
|
|
348
|
+
this.logger.warn("[handoff] agent has no write tool or send capability, falling back to standalone summary mode");
|
|
313
349
|
mode = "summary";
|
|
314
350
|
}
|
|
351
|
+
if (mode === "self") {
|
|
352
|
+
const ok = await this.runSelfHandoff(agent, handoffPath, signal);
|
|
353
|
+
if (!ok) {
|
|
354
|
+
this.logger.warn("[handoff] self handoff failed, falling back to standalone summary mode");
|
|
355
|
+
mode = "summary";
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (mode === "summary") {
|
|
359
|
+
historyText = HandoffService.serializeMessages(agent.messages.filter((m) => !m.omit));
|
|
360
|
+
handoffDoc = await this.generateSummary(agent, historyText, handoffPath, signal);
|
|
361
|
+
await this.writeHandoffFile(handoffPath, handoffDoc);
|
|
362
|
+
}
|
|
363
|
+
// 最后一道取消点:即使两步都跑完,只要期间被取消就不能算成功。跨过这一步就是
|
|
364
|
+
// 「已提交」——之后的 `onHandoff` 钩子(存旧会话等)与迁移收尾不再响应取消
|
|
365
|
+
this.throwIfAborted(signal);
|
|
366
|
+
const ctx = {
|
|
367
|
+
agent,
|
|
368
|
+
mode,
|
|
369
|
+
requestedMode,
|
|
370
|
+
handoffPath,
|
|
371
|
+
handoffDoc,
|
|
372
|
+
model,
|
|
373
|
+
promptTokens,
|
|
374
|
+
handoffTokens,
|
|
375
|
+
migrating,
|
|
376
|
+
messageCountBefore,
|
|
377
|
+
historyText,
|
|
378
|
+
};
|
|
379
|
+
await this.safeHook(this.onHandoff, ctx);
|
|
380
|
+
return ctx;
|
|
315
381
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
await this.
|
|
382
|
+
catch (err) {
|
|
383
|
+
if (!(err instanceof HandoffAbortedError) && !isAbortError(err, signal))
|
|
384
|
+
throw err;
|
|
385
|
+
await this.restoreBeforeHandoff({ agent, messageCountBefore, handoffPath, existedBefore });
|
|
386
|
+
throw new HandoffAbortedError();
|
|
320
387
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
messageCountBefore
|
|
334
|
-
|
|
335
|
-
};
|
|
336
|
-
await this.safeHook(this.onHandoff, ctx);
|
|
337
|
-
return ctx;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* 取消后回到交接前的状态:把交接回合追加的消息全部截掉,并删掉本次新建的文档。
|
|
391
|
+
*
|
|
392
|
+
* 为什么必须截消息:`self` 模式的交接回合是「一条几 KB 的 Skill 指令 user 消息 +
|
|
393
|
+
* 工具结果 + 回执」,留在 `agent.messages` 里既让用户看到半段无关对话,也让下一次
|
|
394
|
+
* 请求把它当历史再发一遍(`↑` 翻历史还会把它翻出来)。`agent.messages` 是唯一真相源,
|
|
395
|
+
* 截回原长度之后,转录、历史、用量全都自动复位,不需要第二处补偿。
|
|
396
|
+
*/
|
|
397
|
+
async restoreBeforeHandoff(params) {
|
|
398
|
+
const { agent, messageCountBefore, handoffPath, existedBefore } = params;
|
|
399
|
+
if (agent.messages.length > messageCountBefore)
|
|
400
|
+
agent.messages.splice(messageCountBefore);
|
|
401
|
+
await this.discardPartialFile(handoffPath, existedBefore);
|
|
338
402
|
}
|
|
339
403
|
/** 自交接是否可用:需要 `write` 工具 + `send` 能力 */
|
|
340
404
|
supportsSelfHandoff(agent) {
|
|
@@ -354,13 +418,14 @@ export class HandoffService {
|
|
|
354
418
|
* `agent.abort()` 不抛错(core 把消息标记为 Aborted 后正常返回),否则「用户
|
|
355
419
|
* 按了 Ctrl+C」会被下面的文件校验误判成「模型没写出文档」,于是重试一轮、再降级
|
|
356
420
|
* 一轮 —— 用户以为取消了,实际多烧了两轮全量输入。
|
|
421
|
+
*
|
|
422
|
+
* 取消的善后(删文档、截消息)不在这里做:交给 {@link runHandoff} 的 catch 一处处理,
|
|
423
|
+
* 因为「文档已写出但还没校验」的取消根本进不到本方法的 catch。
|
|
357
424
|
*/
|
|
358
425
|
async runSelfHandoff(agent, handoffPath, signal) {
|
|
359
426
|
const guard = this.ensureGuard(agent);
|
|
360
427
|
const skillContent = await this.readSkillContent();
|
|
361
428
|
const cwd = agent.scope?.cwd ?? process.cwd();
|
|
362
|
-
// 取消时只删本次新建的文件,不碰本已存在的同名文件
|
|
363
|
-
const existedBefore = await this.fileExists(handoffPath);
|
|
364
429
|
/*
|
|
365
430
|
* 交接回合的护栏让行由 `ContextGuardPlugin.isSuspended` 负责(接线为
|
|
366
431
|
* `() => handoffService.busy`),这里不再去改写用量数据:
|
|
@@ -387,10 +452,8 @@ export class HandoffService {
|
|
|
387
452
|
return await this.isHandoffFileReady(handoffPath);
|
|
388
453
|
}
|
|
389
454
|
catch (err) {
|
|
390
|
-
if (isAbortError(err, signal))
|
|
391
|
-
await this.discardPartialFile(handoffPath, existedBefore);
|
|
455
|
+
if (isAbortError(err, signal))
|
|
392
456
|
throw new HandoffAbortedError();
|
|
393
|
-
}
|
|
394
457
|
this.logger.error(`[handoff] self handoff round failed: ${err?.message ?? err}`);
|
|
395
458
|
return false;
|
|
396
459
|
}
|