dsh-auto-compaction-engine 0.0.0-stage → 0.2.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/README.md +8 -2
- package/lib/index.js +650 -0
- package/lib/plan.js +79 -0
- package/lib/status.js +52 -0
- package/lib/summary.js +147 -0
- package/package.json +45 -4
- package/scripts/preset-scope.mjs +409 -0
- package/scripts/row-native.yml +6 -0
- package/scripts/row-plugin.yml +10 -0
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-auto-compaction-engine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
引擎包(提供 `ctx.compaction`)。完整说明见仓库根目录 README:
|
|
4
|
+
|
|
5
|
+
https://github.com/1010231693/dsh-auto-compaction#readme
|
|
6
|
+
|
|
7
|
+
在 Agent 预设里替换 `compaction-basic` 那一行即可使用;自带 `scripts/preset-scope.mjs` 做可逆切换。
|
|
8
|
+
|
|
9
|
+
> 0.2.0 起引擎改名为 `dsh-auto-compaction-engine`;`dsh-auto-compaction` 现在是设置页/状态提示的 bundle 包。
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-handoff-compaction
|
|
3
|
+
*
|
|
4
|
+
* Idle-time handoff compaction for deepseek-harness.
|
|
5
|
+
*
|
|
6
|
+
* At a configurable share of the model window (default 90%) this backend waits
|
|
7
|
+
* for the agent to go idle, then asks the routed model — replaying the current
|
|
8
|
+
* conversation as the prompt prefix so the provider's cache is reused — for a
|
|
9
|
+
* handoff checkpoint, and replaces the whole compactable surface with that one
|
|
10
|
+
* message. The next user turn therefore starts from system prompt + tools +
|
|
11
|
+
* handoff text, and nothing else.
|
|
12
|
+
*
|
|
13
|
+
* It subclasses the stock basic backend so selection, tool-pair balance,
|
|
14
|
+
* durable compaction markers, flushes and the idle admission reservation are
|
|
15
|
+
* the proven implementation; only the trigger point, the summarization
|
|
16
|
+
* template and the idle gate differ.
|
|
17
|
+
*/
|
|
18
|
+
import z from "@deepseek-ai/schemastery";
|
|
19
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { homedir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
import { BlockAssembler, LlmError, contentHasImage } from "@deepseek-ai/dsh-llm";
|
|
23
|
+
import BasicCompactionEngine from "@deepseek-ai/dsh-compaction-basic";
|
|
24
|
+
import { decideTrigger, shouldAttempt } from "./plan.js";
|
|
25
|
+
import { writeStatus } from "./status.js";
|
|
26
|
+
import {
|
|
27
|
+
continuationInstruction,
|
|
28
|
+
expectedSections,
|
|
29
|
+
looksTruncated,
|
|
30
|
+
mergeSummary,
|
|
31
|
+
rewriteInstruction,
|
|
32
|
+
stripTruncationMarkers,
|
|
33
|
+
validateSummary,
|
|
34
|
+
} from "./summary.js";
|
|
35
|
+
|
|
36
|
+
const LOG = "[handoff]";
|
|
37
|
+
const MAX_SUMMARY_ATTEMPTS = 3;
|
|
38
|
+
|
|
39
|
+
const SETTINGS_DIR = join(homedir(), ".dsh-handoff-compaction");
|
|
40
|
+
// Test harnesses point this at a missing file so the live sidecar cannot leak
|
|
41
|
+
// into a case that expects the configured ratio.
|
|
42
|
+
const SETTINGS_FILE = process.env.DSH_HANDOFF_SETTINGS_FILE ?? join(SETTINGS_DIR, "settings.json");
|
|
43
|
+
const DEFAULT_TEMPLATE_FILE = join(SETTINGS_DIR, "template.md");
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Live settings written by the companion UI package (dsh-handoff-compaction-ui).
|
|
47
|
+
* Read fresh on every decision, cached by mtime so a turn never blocks on disk.
|
|
48
|
+
*/
|
|
49
|
+
let settingsCache = { mtimeMs: -1, value: undefined };
|
|
50
|
+
function readSettings() {
|
|
51
|
+
try {
|
|
52
|
+
const stat = statSync(SETTINGS_FILE);
|
|
53
|
+
if (stat.mtimeMs === settingsCache.mtimeMs) return settingsCache.value;
|
|
54
|
+
const parsed = JSON.parse(readFileSync(SETTINGS_FILE, "utf8"));
|
|
55
|
+
settingsCache = { mtimeMs: stat.mtimeMs, value: parsed && typeof parsed === "object" ? parsed : undefined };
|
|
56
|
+
return settingsCache.value;
|
|
57
|
+
} catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function templatePathFrom(configured) {
|
|
63
|
+
if (typeof configured === "string" && configured.trim().length > 0) return configured.trim();
|
|
64
|
+
const env = process.env.DSH_HANDOFF_TEMPLATE;
|
|
65
|
+
if (typeof env === "string" && env.trim().length > 0) return env.trim();
|
|
66
|
+
return DEFAULT_TEMPLATE_FILE;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Read the handoff template, falling back to the built-in text. */
|
|
70
|
+
function readTemplate(filePath, autoCreate) {
|
|
71
|
+
try {
|
|
72
|
+
if (!existsSync(filePath)) {
|
|
73
|
+
if (!autoCreate) return { text: undefined, source: "builtin (file missing)" };
|
|
74
|
+
mkdirSync(join(filePath, ".."), { recursive: true });
|
|
75
|
+
writeFileSync(filePath, BUILTIN_INSTRUCTION + "\n", "utf8");
|
|
76
|
+
return { text: BUILTIN_INSTRUCTION, source: "builtin (written to " + filePath + ")" };
|
|
77
|
+
}
|
|
78
|
+
const text = readFileSync(filePath, "utf8").replace(/^\uFEFF/, "");
|
|
79
|
+
if (text.trim().length < 20) return { text: undefined, source: "builtin (file empty)" };
|
|
80
|
+
return { text, source: filePath };
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return { text: undefined, source: `builtin (read failed: ${error instanceof Error ? error.message : String(error)})` };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
const BUILTIN_INSTRUCTION = [
|
|
88
|
+
"你现在要为本会话写一份交接文档:我会用它替换掉上面的全部对话记录,然后由你在一个空白的上下文里继续同一件事。写漏的东西将永久丢失。",
|
|
89
|
+
"",
|
|
90
|
+
"只输出 Markdown,严格按下面小节顺序,每节用短横线要点,不许省略任何小节;没有内容写「(无)」。",
|
|
91
|
+
"",
|
|
92
|
+
"## 用户目标与原始要求",
|
|
93
|
+
"- 用户最初想做什么、之后怎么改过口;关键原话逐字引用",
|
|
94
|
+
"",
|
|
95
|
+
"## 当前任务状态",
|
|
96
|
+
"- 在做什么、进行到哪一步、哪些已完成、哪些没完成",
|
|
97
|
+
"",
|
|
98
|
+
"## 涉及的文件与代码",
|
|
99
|
+
"- 精确路径:为什么重要、改了什么、还有哪里没改",
|
|
100
|
+
"",
|
|
101
|
+
"## 已验证的事实与命令",
|
|
102
|
+
"- 跑过的命令、测试结果、报错原文、环境约束(版本、端口、路径)",
|
|
103
|
+
"",
|
|
104
|
+
"## 关键决策与被放弃的方案",
|
|
105
|
+
"- 选了什么、为什么;试过但放弃的方案及放弃原因",
|
|
106
|
+
"",
|
|
107
|
+
"## 待办与下一步",
|
|
108
|
+
"- 用户明确要求但尚未完成的项;紧接着要执行的第一步",
|
|
109
|
+
"",
|
|
110
|
+
"## 风险与注意事项",
|
|
111
|
+
"- 容易踩的坑、用户的偏好与禁忌",
|
|
112
|
+
"",
|
|
113
|
+
"规则:",
|
|
114
|
+
"- 保留精确的路径、命令、错误串、标识符、数值、函数签名;不要概括成\"修改了相关文件\"这类废话。",
|
|
115
|
+
"- 如果对话里有用户粘贴的长文档或上一轮交接文档(例如包在 <compacted-summary> 标记里的那份),不要原样照抄:保留仍然生效的硬约束(路径、命令、版本、验收标准)并逐字引用其关键句,其余概括;过时的内容删掉,最终只输出一份同样结构的文档。",
|
|
116
|
+
"- 如果用户的早期消息给出了一份磁盘上的工程交接文档的绝对路径,必须逐字保留该路径并注明\"需要时重新读取该文件\";不要把它的内容当成必须在本文档里复述的东西。",
|
|
117
|
+
"- 不要提到这份交接文档本身,也不要说上下文被压缩或摘要。",
|
|
118
|
+
"- 只输出文档正文,不要调用任何工具。",
|
|
119
|
+
"",
|
|
120
|
+
"写入时还要记住下面两条,它们会被交给后续的你执行:",
|
|
121
|
+
"- 更早的对话消息不会留在你的上下文里,也没有任何工具或磁盘检索能把它们找回来。不要去搜索会话文件、日志或历史记录还原旧对话;文档里没写的,就当作不知道,直接说不知道。",
|
|
122
|
+
"- 用户问起\"之前说过什么\"时,以本文档为准;文档没有逐字保留的内容(例如旧回答的原文),直接说明已经被压缩掉,不要编造,也不要为了找它去翻文件。",
|
|
123
|
+
].join("\n");
|
|
124
|
+
|
|
125
|
+
function finishError(finish) {
|
|
126
|
+
switch (finish.kind) {
|
|
127
|
+
case "error":
|
|
128
|
+
case "aborted":
|
|
129
|
+
return new LlmError(finish.failure.message, finish.failure.code, finish.failure);
|
|
130
|
+
case "max-tokens": {
|
|
131
|
+
const error = new Error("handoff summary truncated at the token cap (incomplete checkpoint)");
|
|
132
|
+
error.code = "MAX_TOKENS";
|
|
133
|
+
return error;
|
|
134
|
+
}
|
|
135
|
+
default:
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function mergeUsage(left, right) {
|
|
141
|
+
if (left === undefined) return right;
|
|
142
|
+
if (right === undefined) return left;
|
|
143
|
+
const merged = { ...left };
|
|
144
|
+
for (const key of Object.keys(right)) {
|
|
145
|
+
const value = right[key];
|
|
146
|
+
if (typeof value === "number" && typeof merged[key] === "number") merged[key] += value;
|
|
147
|
+
else if (merged[key] === undefined) merged[key] = value;
|
|
148
|
+
}
|
|
149
|
+
return merged;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function errorCodeOf(error) {
|
|
153
|
+
return error !== null && typeof error === "object" && typeof error.code === "string" ? error.code : "";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export default class HandoffCompaction extends BasicCompactionEngine {
|
|
157
|
+
static inject = ["llm", "tokenMeter", "sessions"];
|
|
158
|
+
|
|
159
|
+
static Config = z.object({
|
|
160
|
+
thresholdRatio: z.number(),
|
|
161
|
+
headroomTokens: z.number(),
|
|
162
|
+
retainRatio: z.number(),
|
|
163
|
+
retainTokens: z.number(),
|
|
164
|
+
summarizationProvider: z.string(),
|
|
165
|
+
summarizationModel: z.string(),
|
|
166
|
+
maxTokens: z.number(),
|
|
167
|
+
compactionRetries: z.number(),
|
|
168
|
+
maxOverflowRetries: z.number(),
|
|
169
|
+
contextWindowOverride: z.number(),
|
|
170
|
+
reservedCompletionTokensOverride: z.number(),
|
|
171
|
+
idleDelayMs: z.number(),
|
|
172
|
+
minIntervalMs: z.number(),
|
|
173
|
+
minTokenGrowth: z.number(),
|
|
174
|
+
timeoutMs: z.number(),
|
|
175
|
+
dryRun: z.boolean(),
|
|
176
|
+
handoffInstruction: z.string(),
|
|
177
|
+
traceFile: z.string(),
|
|
178
|
+
traceConsole: z.boolean(),
|
|
179
|
+
templateFile: z.string(),
|
|
180
|
+
presetId: z.string(),
|
|
181
|
+
autoCreateTemplate: z.boolean(),
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
constructor(ctx, config = {}) {
|
|
185
|
+
const {
|
|
186
|
+
contextWindowOverride,
|
|
187
|
+
reservedCompletionTokensOverride,
|
|
188
|
+
idleDelayMs,
|
|
189
|
+
minIntervalMs,
|
|
190
|
+
minTokenGrowth,
|
|
191
|
+
timeoutMs,
|
|
192
|
+
dryRun,
|
|
193
|
+
handoffInstruction,
|
|
194
|
+
traceFile,
|
|
195
|
+
traceConsole,
|
|
196
|
+
templateFile,
|
|
197
|
+
presetId,
|
|
198
|
+
autoCreateTemplate,
|
|
199
|
+
...base
|
|
200
|
+
} = config;
|
|
201
|
+
// The stock trigger is registered so the plugin switch can hand compaction
|
|
202
|
+
// back to the native engine live; compactIfNeeded() suppresses it while the
|
|
203
|
+
// handoff path is enabled.
|
|
204
|
+
super(ctx, { ...base, auto: true });
|
|
205
|
+
this.handoff = {
|
|
206
|
+
instruction: typeof handoffInstruction === "string" && handoffInstruction.trim().length > 0 ? handoffInstruction : undefined,
|
|
207
|
+
contextWindowOverride: contextWindowOverride ?? 0,
|
|
208
|
+
reservedOverride: reservedCompletionTokensOverride ?? -1,
|
|
209
|
+
idleDelayMs: idleDelayMs ?? 1_500,
|
|
210
|
+
minIntervalMs: minIntervalMs ?? 60_000,
|
|
211
|
+
minTokenGrowth: minTokenGrowth ?? 2_048,
|
|
212
|
+
timeoutMs: timeoutMs ?? 600_000,
|
|
213
|
+
dryRun: dryRun ?? false,
|
|
214
|
+
templateFile: templatePathFrom(templateFile ?? readSettings()?.templateFile),
|
|
215
|
+
presetId: typeof presetId === "string" ? presetId : "",
|
|
216
|
+
autoCreateTemplate: autoCreateTemplate !== false,
|
|
217
|
+
};
|
|
218
|
+
this.handoff.traceFile = typeof traceFile === "string" ? traceFile : "";
|
|
219
|
+
this.handoff.traceConsole = traceConsole === true;
|
|
220
|
+
// Materialise the template so the settings card can point at a real file.
|
|
221
|
+
try { readTemplate(this.handoff.templateFile, this.handoff.autoCreateTemplate); } catch { /* never block construction */ }
|
|
222
|
+
this.attempts = new WeakMap();
|
|
223
|
+
this.running = new WeakSet();
|
|
224
|
+
this.timers = new WeakMap();
|
|
225
|
+
this.agents = new WeakMap();
|
|
226
|
+
ctx.on("agent/status", ({ agent, status }) => {
|
|
227
|
+
if (agent?.session) this.agents.set(agent.session, agent);
|
|
228
|
+
if (status === "idle") this.scheduleCheck(agent, "agent/status idle");
|
|
229
|
+
});
|
|
230
|
+
// A finished turn is the gap the user described; agent/status is not the only
|
|
231
|
+
// place DSH reports it, so both paths are wired.
|
|
232
|
+
ctx.on("session/event", (session, event) => {
|
|
233
|
+
if (event?.type !== "turn/end") return;
|
|
234
|
+
const owner = this.agents.get(session);
|
|
235
|
+
if (owner !== undefined) this.scheduleCheck(owner, "turn/end");
|
|
236
|
+
});
|
|
237
|
+
this.log(`ready (threshold ${this.describeThreshold()}, dryRun=${this.handoff.dryRun}, trace=${this.handoff.traceFile || (this.handoff.traceConsole ? "console" : "off")})`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
describeThreshold() {
|
|
241
|
+
return `${Math.round((this.config.thresholdRatio ?? 0.9) * 100)}%`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Settings switch. Missing settings or a missing field keeps the plugin on;
|
|
246
|
+
* only an explicit `enabled: false` hands compaction back to the stock engine.
|
|
247
|
+
*/
|
|
248
|
+
handoffEnabled() {
|
|
249
|
+
const live = readSettings();
|
|
250
|
+
return !(live !== undefined && live.enabled === false);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The base registers its between-step pressure / overflow triggers whenever
|
|
255
|
+
* `auto` is on. While the handoff path is enabled they must stay inert, so
|
|
256
|
+
* the plugin still only compacts from its idle gate. Disabled: delegate, and
|
|
257
|
+
* DSH's native compaction behaves exactly as without the plugin.
|
|
258
|
+
*/
|
|
259
|
+
async compactIfNeeded(agent, trigger, signal) {
|
|
260
|
+
if (this.handoffEnabled()) return null;
|
|
261
|
+
return super.compactIfNeeded(agent, trigger, signal);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
log(message, level = "info") {
|
|
265
|
+
const line = `${new Date().toISOString()} ${LOG} ${message}`;
|
|
266
|
+
if (this.handoff?.traceConsole === true) {
|
|
267
|
+
try { console.error(line); } catch { /* trace must never break the turn */ }
|
|
268
|
+
}
|
|
269
|
+
const file = this.handoff?.traceFile;
|
|
270
|
+
if (typeof file === "string" && file.length > 0) {
|
|
271
|
+
try { appendFileSync(file, line + "\n"); } catch { /* trace must never break the turn */ }
|
|
272
|
+
}
|
|
273
|
+
const write = this.ctx.logger?.[level] ?? this.ctx.logger?.info;
|
|
274
|
+
if (typeof write === "function") write.call(this.ctx.logger, `${LOG} ${message}`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Debounce the idle signal so a turn that immediately resumes does not trigger work. */
|
|
278
|
+
scheduleCheck(agent, why = "idle") {
|
|
279
|
+
const previous = this.timers.get(agent);
|
|
280
|
+
if (previous !== undefined) clearTimeout(previous);
|
|
281
|
+
const timer = setTimeout(() => {
|
|
282
|
+
this.timers.delete(agent);
|
|
283
|
+
void this.checkAndHandoff(agent, why);
|
|
284
|
+
}, this.handoff.idleDelayMs);
|
|
285
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
286
|
+
this.timers.set(agent, timer);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
routedTarget(agent) {
|
|
290
|
+
const routed = agent.session?.requestHeader?.()?.config;
|
|
291
|
+
if (routed?.provider && routed?.model) return { provider: routed.provider, model: routed.model };
|
|
292
|
+
const options = agent.options ?? {};
|
|
293
|
+
if (options.provider && options.model) return { provider: options.provider, model: options.model };
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async checkAndHandoff(agent, why = "idle") {
|
|
298
|
+
if (this.running.has(agent)) return;
|
|
299
|
+
if (!this.handoffEnabled()) return;
|
|
300
|
+
try {
|
|
301
|
+
const session = agent.session;
|
|
302
|
+
if (!session) return;
|
|
303
|
+
const measurement = this.ctx.tokenMeter.measure(session);
|
|
304
|
+
const target = this.routedTarget(agent);
|
|
305
|
+
if (target === undefined) return this.log(`${why}: no routed provider/model yet; skipping`);
|
|
306
|
+
|
|
307
|
+
const info = await this.ctx.llm.resolveModelInfo(target.provider, target.model);
|
|
308
|
+
const headerMaxTokens = session.requestHeader?.()?.config?.maxTokens;
|
|
309
|
+
const reserved = this.handoff.reservedOverride >= 0
|
|
310
|
+
? this.handoff.reservedOverride
|
|
311
|
+
: (headerMaxTokens ?? info.defaultMaxTokens ?? 0);
|
|
312
|
+
const contextWindow = this.handoff.contextWindowOverride > 0
|
|
313
|
+
? this.handoff.contextWindowOverride
|
|
314
|
+
: info.context?.contextWindow;
|
|
315
|
+
|
|
316
|
+
const live = readSettings();
|
|
317
|
+
const scope = live?.scope === "preset" ? "preset" : "global";
|
|
318
|
+
const selectedPresets = Array.isArray(live?.presets) ? live.presets : [];
|
|
319
|
+
if (scope === "preset" && this.handoff.presetId !== "" && !selectedPresets.includes(this.handoff.presetId)) {
|
|
320
|
+
return this.log(`${why}: skipped by scope (preset "${this.handoff.presetId}" not selected)`);
|
|
321
|
+
}
|
|
322
|
+
const liveRatio = typeof live?.thresholdRatio === "number" && live.thresholdRatio > 0 && live.thresholdRatio <= 1
|
|
323
|
+
? live.thresholdRatio
|
|
324
|
+
: undefined;
|
|
325
|
+
|
|
326
|
+
const decision = decideTrigger({
|
|
327
|
+
totalTokens: measurement.totalTokens,
|
|
328
|
+
contextWindow,
|
|
329
|
+
reservedCompletionTokens: reserved,
|
|
330
|
+
headroomTokens: this.config.headroomTokens ?? 0,
|
|
331
|
+
thresholdRatio: liveRatio ?? this.config.thresholdRatio ?? 0.9,
|
|
332
|
+
});
|
|
333
|
+
if (!decision.trigger) {
|
|
334
|
+
return this.log(`${why}: ${measurement.totalTokens}/${decision.thresholdTokens ?? "?"} tokens${decision.ok === false ? ` (${decision.reason})` : ""}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const last = this.attempts.get(agent);
|
|
338
|
+
if (!shouldAttempt({
|
|
339
|
+
now: Date.now(),
|
|
340
|
+
last,
|
|
341
|
+
tokens: measurement.totalTokens,
|
|
342
|
+
thresholdTokens: decision.thresholdTokens,
|
|
343
|
+
minIntervalMs: this.handoff.minIntervalMs,
|
|
344
|
+
minTokenGrowth: this.handoff.minTokenGrowth,
|
|
345
|
+
})) {
|
|
346
|
+
return this.log(`over threshold (${measurement.totalTokens} >= ${decision.thresholdTokens}) but holding: retry gate`);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.attempts.set(agent, { at: Date.now(), tokens: measurement.totalTokens });
|
|
350
|
+
if (this.handoff.dryRun) {
|
|
351
|
+
return this.log(`DRY-RUN: would hand off now (${measurement.totalTokens} >= ${decision.thresholdTokens} of an assumed ${contextWindow}-token window)`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
this.running.add(agent);
|
|
355
|
+
writeStatus(session.id, {
|
|
356
|
+
phase: "running",
|
|
357
|
+
step: "prepare",
|
|
358
|
+
startedAt: Date.now(),
|
|
359
|
+
totalTokens: measurement.totalTokens,
|
|
360
|
+
thresholdTokens: decision.thresholdTokens,
|
|
361
|
+
contextWindow,
|
|
362
|
+
timeoutMs: this.handoff.timeoutMs,
|
|
363
|
+
provider: target.provider,
|
|
364
|
+
model: target.model,
|
|
365
|
+
});
|
|
366
|
+
try {
|
|
367
|
+
const result = await this.compactNow(agent, AbortSignal.timeout(this.handoff.timeoutMs));
|
|
368
|
+
if (result === null || result === undefined) {
|
|
369
|
+
writeStatus(session.id, {
|
|
370
|
+
phase: "done",
|
|
371
|
+
step: "done",
|
|
372
|
+
completedAt: Date.now(),
|
|
373
|
+
shadowedNodes: 0,
|
|
374
|
+
shadowedTokens: 0,
|
|
375
|
+
note: "no compactable range",
|
|
376
|
+
});
|
|
377
|
+
this.log("idle: no compactable range; nothing replaced", "warn");
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
writeStatus(session.id, {
|
|
381
|
+
phase: "done",
|
|
382
|
+
step: "done",
|
|
383
|
+
completedAt: Date.now(),
|
|
384
|
+
shadowedNodes: result.shadowedSeqs.length,
|
|
385
|
+
shadowedTokens: result.shadowedTokenCount,
|
|
386
|
+
missingSections: [],
|
|
387
|
+
retrying: false,
|
|
388
|
+
});
|
|
389
|
+
this.log(`committed: replaced ${result.shadowedSeqs.length} node(s) (~${result.shadowedTokenCount} tokens) with the handoff checkpoint`);
|
|
390
|
+
} catch (error) {
|
|
391
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
392
|
+
writeStatus(agent.session?.id, {
|
|
393
|
+
phase: "failed",
|
|
394
|
+
step: "failed",
|
|
395
|
+
failedAt: Date.now(),
|
|
396
|
+
error: message,
|
|
397
|
+
errorCode: errorCodeOf(error),
|
|
398
|
+
});
|
|
399
|
+
throw error;
|
|
400
|
+
} finally {
|
|
401
|
+
this.running.delete(agent);
|
|
402
|
+
}
|
|
403
|
+
} catch (error) {
|
|
404
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
405
|
+
writeStatus(agent.session?.id, {
|
|
406
|
+
phase: "failed",
|
|
407
|
+
step: "failed",
|
|
408
|
+
failedAt: Date.now(),
|
|
409
|
+
error: message,
|
|
410
|
+
errorCode: errorCodeOf(error),
|
|
411
|
+
});
|
|
412
|
+
this.log(`failed: ${message}`, "warn");
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Wrap the stock transaction so manual /compact and automatic idle
|
|
418
|
+
* compaction share the same status sidecar. The base method still owns the
|
|
419
|
+
* surface lock, summarization and commit.
|
|
420
|
+
*/
|
|
421
|
+
async compactNow(agent, signal, sourceCommandId) {
|
|
422
|
+
// Manual /compact while the plugin is off stays a stock native compaction:
|
|
423
|
+
// no handoff template, no plugin status sidecar.
|
|
424
|
+
if (!this.handoffEnabled()) return super.compactNow(agent, signal, sourceCommandId);
|
|
425
|
+
const sessionId = agent?.session?.id;
|
|
426
|
+
let totalTokens;
|
|
427
|
+
try { totalTokens = this.ctx.tokenMeter.measure(agent.session).totalTokens; } catch { totalTokens = undefined; }
|
|
428
|
+
writeStatus(sessionId, {
|
|
429
|
+
phase: "running",
|
|
430
|
+
step: "prepare",
|
|
431
|
+
startedAt: Date.now(),
|
|
432
|
+
timeoutMs: this.handoff.timeoutMs,
|
|
433
|
+
...(typeof totalTokens === "number" ? { totalTokens } : {}),
|
|
434
|
+
retrying: false,
|
|
435
|
+
writing: false,
|
|
436
|
+
missingSections: [],
|
|
437
|
+
error: "",
|
|
438
|
+
errorCode: "",
|
|
439
|
+
});
|
|
440
|
+
try {
|
|
441
|
+
const result = await super.compactNow(agent, signal, sourceCommandId);
|
|
442
|
+
writeStatus(sessionId, {
|
|
443
|
+
phase: "done",
|
|
444
|
+
step: "done",
|
|
445
|
+
completedAt: Date.now(),
|
|
446
|
+
shadowedNodes: Array.isArray(result?.shadowedSeqs) ? result.shadowedSeqs.length : 0,
|
|
447
|
+
shadowedTokens: typeof result?.shadowedTokenCount === "number" ? result.shadowedTokenCount : 0,
|
|
448
|
+
missingSections: [],
|
|
449
|
+
retrying: false,
|
|
450
|
+
writing: false,
|
|
451
|
+
...(result === null || result === undefined ? { note: "no compactable range" } : {}),
|
|
452
|
+
});
|
|
453
|
+
return result;
|
|
454
|
+
} catch (error) {
|
|
455
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
456
|
+
writeStatus(sessionId, {
|
|
457
|
+
phase: "failed",
|
|
458
|
+
step: "failed",
|
|
459
|
+
failedAt: Date.now(),
|
|
460
|
+
error: message,
|
|
461
|
+
errorCode: errorCodeOf(error),
|
|
462
|
+
});
|
|
463
|
+
throw error;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Handoff template instead of the stock compaction instruction. */
|
|
468
|
+
async summarize(input, agent, signal) {
|
|
469
|
+
// Native pressure/overflow compaction runs through this same hook, so the
|
|
470
|
+
// stock prompt must come back the moment the plugin is switched off.
|
|
471
|
+
if (!this.handoffEnabled()) return super.summarize(input, agent, signal);
|
|
472
|
+
const latest = agent.session.requestHeader?.()?.config;
|
|
473
|
+
const configured = this.config.summarizationProvider && this.config.summarizationModel
|
|
474
|
+
? { provider: this.config.summarizationProvider, model: this.config.summarizationModel }
|
|
475
|
+
: undefined;
|
|
476
|
+
const agentTarget = agent.options?.provider && agent.options?.model
|
|
477
|
+
? { provider: agent.options.provider, model: agent.options.model }
|
|
478
|
+
: undefined;
|
|
479
|
+
const target = configured ?? latest ?? agentTarget;
|
|
480
|
+
if (target === undefined) throw new Error("no provider/model available for the handoff summary");
|
|
481
|
+
// Replaying the prefix only reuses the provider cache when the auxiliary
|
|
482
|
+
// request carries the same routing knobs as the routed one. Strata writes
|
|
483
|
+
// the reasoning directive into the prefix, so a missing effort breaks the
|
|
484
|
+
// cache a few thousand tokens in. Only inherit it when the summary target
|
|
485
|
+
// is the same model that produced the latest request.
|
|
486
|
+
const sameTarget = latest !== undefined && target.provider === latest.provider && target.model === latest.model;
|
|
487
|
+
const reasoningEffort = sameTarget ? latest.reasoningEffort : undefined;
|
|
488
|
+
|
|
489
|
+
const resolved = this.handoff.instruction === undefined
|
|
490
|
+
? readTemplate(this.handoff.templateFile, this.handoff.autoCreateTemplate)
|
|
491
|
+
: { text: this.handoff.instruction, source: "config handoffInstruction" };
|
|
492
|
+
const instruction = typeof resolved.text === "string" && resolved.text.trim().length > 0
|
|
493
|
+
? resolved.text
|
|
494
|
+
: BUILTIN_INSTRUCTION;
|
|
495
|
+
this.log(`template: ${resolved.source}${resolved.text === undefined ? " (fallback)" : ""}`);
|
|
496
|
+
|
|
497
|
+
const expected = expectedSections(instruction);
|
|
498
|
+
const baseMessages = [
|
|
499
|
+
...input.messages,
|
|
500
|
+
Object.freeze({ role: "user", content: [{ type: "text", text: instruction }] }),
|
|
501
|
+
];
|
|
502
|
+
const baseOptions = {
|
|
503
|
+
provider: target.provider,
|
|
504
|
+
model: target.model,
|
|
505
|
+
toolHistory: agent.session.toolHistory(),
|
|
506
|
+
...(input.tools === undefined ? {} : { tools: [...input.tools] }),
|
|
507
|
+
maxTokens: this.config.maxTokens,
|
|
508
|
+
...(reasoningEffort === undefined ? {} : { reasoningEffort }),
|
|
509
|
+
sessionId: agent.session.id,
|
|
510
|
+
purpose: "compaction",
|
|
511
|
+
...(signal === undefined ? {} : { signal }),
|
|
512
|
+
};
|
|
513
|
+
const sessionId = agent.session?.id;
|
|
514
|
+
|
|
515
|
+
let combined = "";
|
|
516
|
+
let rawOutput = [];
|
|
517
|
+
let usage;
|
|
518
|
+
let validation = {
|
|
519
|
+
ok: expected.length === 0,
|
|
520
|
+
missing: [...expected],
|
|
521
|
+
truncated: false,
|
|
522
|
+
endsWithHeading: false,
|
|
523
|
+
reason: "not attempted",
|
|
524
|
+
};
|
|
525
|
+
const attempts = [];
|
|
526
|
+
|
|
527
|
+
let liveTotalTokens;
|
|
528
|
+
try { liveTotalTokens = this.ctx.tokenMeter.measure(agent.session).totalTokens; } catch { liveTotalTokens = undefined; }
|
|
529
|
+
writeStatus(sessionId, {
|
|
530
|
+
phase: "running",
|
|
531
|
+
step: "summary",
|
|
532
|
+
summaryAttempt: 1,
|
|
533
|
+
retrying: false,
|
|
534
|
+
writing: false,
|
|
535
|
+
missingSections: [],
|
|
536
|
+
...(typeof liveTotalTokens === "number" ? { totalTokens: liveTotalTokens } : {}),
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
for (let attempt = 1; attempt <= MAX_SUMMARY_ATTEMPTS; attempt += 1) {
|
|
540
|
+
signal?.throwIfAborted?.();
|
|
541
|
+
const messages = attempt === 1
|
|
542
|
+
? baseMessages
|
|
543
|
+
: attempt === 2
|
|
544
|
+
? [
|
|
545
|
+
...baseMessages,
|
|
546
|
+
Object.freeze({ role: "assistant", content: [{ type: "text", text: combined }] }),
|
|
547
|
+
Object.freeze({ role: "user", content: [{ type: "text", text: continuationInstruction(validation) }] }),
|
|
548
|
+
]
|
|
549
|
+
: [
|
|
550
|
+
...baseMessages,
|
|
551
|
+
Object.freeze({
|
|
552
|
+
role: "user",
|
|
553
|
+
content: [{ type: "text", text: rewriteInstruction(validation, this.config.maxTokens) }],
|
|
554
|
+
}),
|
|
555
|
+
];
|
|
556
|
+
|
|
557
|
+
const result = await this.runSummaryRequest(baseOptions, messages);
|
|
558
|
+
const truncatedNow = looksTruncated(result.text);
|
|
559
|
+
const clean = stripTruncationMarkers(result.text);
|
|
560
|
+
combined = attempt === 1
|
|
561
|
+
? clean
|
|
562
|
+
: attempt === 2
|
|
563
|
+
? mergeSummary(combined, clean)
|
|
564
|
+
: clean;
|
|
565
|
+
rawOutput = result.rawOutput;
|
|
566
|
+
usage = mergeUsage(usage, result.usage);
|
|
567
|
+
validation = validateSummary(combined, expected);
|
|
568
|
+
if (truncatedNow) {
|
|
569
|
+
validation = {
|
|
570
|
+
...validation,
|
|
571
|
+
ok: false,
|
|
572
|
+
truncated: true,
|
|
573
|
+
reason: validation.reason.length > 0 ? `${validation.reason}; truncation marker` : "truncation marker",
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
attempts.push({
|
|
577
|
+
attempt,
|
|
578
|
+
finish: result.finishKind,
|
|
579
|
+
chars: result.text.length,
|
|
580
|
+
ok: validation.ok,
|
|
581
|
+
});
|
|
582
|
+
if (validation.ok) break;
|
|
583
|
+
if (attempt < MAX_SUMMARY_ATTEMPTS) {
|
|
584
|
+
const next = attempt + 1;
|
|
585
|
+
this.log(`summary incomplete after attempt ${attempt}: ${validation.reason}; ${attempt === 1 ? "continuing" : "rewriting"}`);
|
|
586
|
+
writeStatus(sessionId, {
|
|
587
|
+
phase: "running",
|
|
588
|
+
step: "summary",
|
|
589
|
+
summaryAttempt: next,
|
|
590
|
+
retrying: true,
|
|
591
|
+
writing: next === MAX_SUMMARY_ATTEMPTS,
|
|
592
|
+
missingSections: validation.missing,
|
|
593
|
+
summaryChars: combined.length,
|
|
594
|
+
summaryAttempts: attempts,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (!validation.ok) {
|
|
600
|
+
const error = new Error(`handoff summary incomplete after ${attempts.length} attempt(s): ${validation.reason}`);
|
|
601
|
+
error.code = "INCOMPLETE_SUMMARY";
|
|
602
|
+
error.attempts = attempts;
|
|
603
|
+
throw error;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const finalText = combined.trim();
|
|
607
|
+
if (finalText.length === 0) throw new Error("handoff summary produced no text content");
|
|
608
|
+
writeStatus(sessionId, {
|
|
609
|
+
phase: "running",
|
|
610
|
+
step: "commit",
|
|
611
|
+
summaryAttempt: attempts.length,
|
|
612
|
+
retrying: false,
|
|
613
|
+
writing: false,
|
|
614
|
+
missingSections: [],
|
|
615
|
+
summaryChars: finalText.length,
|
|
616
|
+
summaryAttempts: attempts,
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
summary: [{ type: "text", text: finalText }],
|
|
621
|
+
rawOutput,
|
|
622
|
+
llmStreamCall: true,
|
|
623
|
+
provider: target.provider,
|
|
624
|
+
model: target.model,
|
|
625
|
+
maxTokens: this.config.maxTokens,
|
|
626
|
+
...(usage === undefined ? {} : { usage }),
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/** One summary LLM call; max-tokens is returned as a partial, not thrown. */
|
|
631
|
+
async runSummaryRequest(baseOptions, messages) {
|
|
632
|
+
const assembler = new BlockAssembler();
|
|
633
|
+
for await (const chunk of this.ctx.llm.stream({ ...baseOptions, messages })) assembler.push(chunk);
|
|
634
|
+
const finish = assembler.finish ?? { kind: "stop" };
|
|
635
|
+
if (finish.kind === "error" || finish.kind === "aborted") {
|
|
636
|
+
const error = finishError(finish);
|
|
637
|
+
if (error !== undefined) throw error;
|
|
638
|
+
}
|
|
639
|
+
const rawOutput = assembler.blocks();
|
|
640
|
+
if (contentHasImage(rawOutput)) throw new LlmError("handoff summary cannot contain image output", "UNSUPPORTED_CONTENT");
|
|
641
|
+
const text = rawOutput.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
642
|
+
if (text.trim().length === 0) throw new Error("handoff summary produced no text content");
|
|
643
|
+
return {
|
|
644
|
+
rawOutput,
|
|
645
|
+
text,
|
|
646
|
+
finishKind: finish.kind,
|
|
647
|
+
usage: assembler.usage,
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
}
|