dsh-compaction-weave 0.0.0-stage → 0.3.3
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 +97 -0
- package/LICENSE +21 -0
- package/README.en.md +136 -0
- package/README.md +139 -2
- package/cordis.patch.yml +19 -0
- package/docs/architecture.md +84 -0
- package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
- package/docs/benchmarks/rc2-documentation.json +41 -0
- package/docs/benchmarks/rc2-linux-tests.json +590 -0
- package/docs/benchmarks/rc2-live-and-cost.json +746 -0
- package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
- package/docs/benchmarks/rc4-tests.json +617 -0
- package/docs/benchmarks/review35-compression.json +117 -0
- package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
- package/docs/benchmarks/v0.3.2-tests.json +590 -0
- package/docs/configuration.md +131 -0
- package/docs/development.md +88 -0
- package/docs/en/architecture.md +75 -0
- package/docs/en/configuration.md +131 -0
- package/docs/en/development.md +86 -0
- package/docs/en/feedback.md +47 -0
- package/docs/en/principles.md +235 -0
- package/docs/en/testing.md +368 -0
- package/docs/en/troubleshooting.md +59 -0
- package/docs/feedback.md +47 -0
- package/docs/images/principles-context.en.svg +77 -0
- package/docs/images/principles-context.zh.svg +77 -0
- package/docs/images/principles-pipeline.en.svg +93 -0
- package/docs/images/principles-pipeline.zh.svg +93 -0
- package/docs/images/principles-recovery.en.svg +46 -0
- package/docs/images/principles-recovery.zh.svg +46 -0
- package/docs/principles.md +235 -0
- package/docs/test-results.json +634 -0
- package/docs/testing.md +358 -0
- package/docs/troubleshooting.md +59 -0
- package/lib/abort.js +13 -0
- package/lib/action-state.js +60 -0
- package/lib/atomic-replace.js +18 -0
- package/lib/attachments.js +76 -0
- package/lib/audit-reject.js +161 -0
- package/lib/audit.js +939 -0
- package/lib/cache-policy.js +71 -0
- package/lib/client.js +696 -0
- package/lib/command-compact.js +38 -0
- package/lib/command-heartbeat.js +61 -0
- package/lib/config.js +596 -0
- package/lib/constraint-scope.js +37 -0
- package/lib/continuation-budget.js +69 -0
- package/lib/continuation-pending.js +57 -0
- package/lib/continuation-progress.js +46 -0
- package/lib/continuation.js +287 -0
- package/lib/engine-entry.js +103 -0
- package/lib/engine.js +2485 -0
- package/lib/failure-diagnostics.js +220 -0
- package/lib/history-source.js +74 -0
- package/lib/history.js +712 -0
- package/lib/idle-compaction.js +64 -0
- package/lib/index.js +611 -0
- package/lib/instruction-source.js +163 -0
- package/lib/journal.js +164 -0
- package/lib/manual-error.js +179 -0
- package/lib/memory-policy.js +383 -0
- package/lib/meter.js +387 -0
- package/lib/observation-mask.js +390 -0
- package/lib/proactive.js +108 -0
- package/lib/progress-facts.js +347 -0
- package/lib/progress-state.js +30 -0
- package/lib/prompt.js +194 -0
- package/lib/reconstruct.js +72 -0
- package/lib/reference-input.js +107 -0
- package/lib/region.js +972 -0
- package/lib/source-provenance.js +58 -0
- package/lib/source-recovery.js +22 -0
- package/lib/source-refs.js +137 -0
- package/lib/state.js +46 -0
- package/lib/storage-path.js +42 -0
- package/lib/summary-budget.js +187 -0
- package/lib/summary-config.js +308 -0
- package/lib/summary-progress.js +56 -0
- package/lib/summary-retry.js +27 -0
- package/lib/summary-timeout.js +68 -0
- package/lib/summary.js +476 -0
- package/lib/tools.js +341 -0
- package/lib/transaction-host.js +252 -0
- package/lib/transaction.js +285 -0
- package/lib/validate.js +503 -0
- package/lib/version.js +3 -0
- package/lib/window.js +120 -0
- package/lib/work-report.js +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -4
package/lib/engine.js
ADDED
|
@@ -0,0 +1,2485 @@
|
|
|
1
|
+
import {collectProgressFacts, progressSourceRefs, renderProgressFacts, renderSummaryScope, validateProgressFacts} from './progress-facts.js';
|
|
2
|
+
import {FailureDiagnostics,diagnosticFor,diagnosticText,failureExplanation,transferDiagnostic,diagnosticContext,noteDiagnosticMeasurement} from './failure-diagnostics.js';
|
|
3
|
+
import {collectWorkReport, workReportRef, scopeWorkReport} from './work-report.js';
|
|
4
|
+
import {ProactiveCompaction} from './proactive.js';
|
|
5
|
+
import {idleResumeDue} from './idle-compaction.js';
|
|
6
|
+
import {selectCachePreservingRange} from './cache-policy.js';
|
|
7
|
+
import {sourceBreakerRecoverable} from './source-recovery.js';
|
|
8
|
+
import {readPendingContinuation,assertPendingSource,freezeContinuation,bindContinuationProgress,settleContinuation,releaseUnstartedContinuation} from './continuation-pending.js';
|
|
9
|
+
import {continuationBudgetInfo,continuationBudgetText} from './continuation-budget.js';
|
|
10
|
+
import { prepareContinuation, planContinuationMemory, residentConstraints, hasContinuation, continuationEligible, selectContinuationRange } from './continuation.js';
|
|
11
|
+
import { Buffer } from 'node:buffer';
|
|
12
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
13
|
+
import { resolveSummaryCapacity, planSummaryRange, measureSummaryRange, combineCompactionResults, capacityLimit } from './summary-budget.js';
|
|
14
|
+
import { combineSignals } from './abort.js';
|
|
15
|
+
import { SessionStateStore } from './state.js';
|
|
16
|
+
import { dirname, join } from 'node:path';
|
|
17
|
+
const activeTransactions = new Set();
|
|
18
|
+
import { materializeUserMessageSection } from './validate.js';
|
|
19
|
+
import { reconstructDisputedLines } from './reconstruct.js';
|
|
20
|
+
// 压缩引擎主体。
|
|
21
|
+
//
|
|
22
|
+
// 本文件不 import 任何 `@deepseek-ai/*` 包。插件目录与 profile 目录都解析不到
|
|
23
|
+
// 这些包(本地是 junction 安装,target 已失效,裸导入在加载期即
|
|
24
|
+
// ERR_MODULE_NOT_FOUND)。宿主侧也不对引擎做 `instanceof CompactionEngine`
|
|
25
|
+
// 检查,所以鸭子类型实现三个方法即可被 `/compact` 和 agent-loop 正常消费。
|
|
26
|
+
//
|
|
27
|
+
// 三个方法(与官方 CompactionEngine 抽象接口一致):
|
|
28
|
+
// compactIfNeeded(agent, trigger, signal) → CompactionResult | null
|
|
29
|
+
// compactNow(agent, signal, sourceCommandId?) → CompactionResult | null
|
|
30
|
+
// compactRegion(start, end, agent, signal?) → CompactionResult
|
|
31
|
+
//
|
|
32
|
+
// 七层落点:
|
|
33
|
+
// L1 窗口 → lib/window.js(固定 window,独立 safetyWindow 单调收紧)
|
|
34
|
+
// L2 触发 → compactIfNeeded 里 thresholdTokens 判定
|
|
35
|
+
// L3 摘要模型 → lib/summary.js(目标链 + 超时 + 降级)
|
|
36
|
+
// L4 模板 → lib/prompt.js(九段式,近期原话只渲染一份)
|
|
37
|
+
// L5 保留 → collectInstructionMessages + buildCheckpointBlocks
|
|
38
|
+
// L6 溢出收敛 → compaction/summary-error waterfall(多留一组再试)
|
|
39
|
+
// L7 失败熔断 → failureThreshold + 每次成功后清零
|
|
40
|
+
|
|
41
|
+
import { resolveCompactSpec, resolveTargetPolicy } from './config.js';
|
|
42
|
+
import { lockWindow, contextWindowOf, defaultMaxTokensOf } from './window.js';
|
|
43
|
+
import {memoryRequirements, memoryDecisionsForSource, memorySourceRefs, memoryPolicyInstruction, deduplicateNotes, retiredMemoryPresent} from './memory-policy.js';
|
|
44
|
+
import { approxTokens, blocksToText } from './meter.js';
|
|
45
|
+
import { dumpAuditRejection } from './audit-reject.js';
|
|
46
|
+
import {
|
|
47
|
+
selectCompactableRange,
|
|
48
|
+
validateSurfaceRegion,
|
|
49
|
+
inspectCompactionEntryState,
|
|
50
|
+
assertCompactionInactive,
|
|
51
|
+
buildSummarizationInput,
|
|
52
|
+
collectInstructionMessages,
|
|
53
|
+
selectSessionMemory,
|
|
54
|
+
buildCheckpointBlocks,
|
|
55
|
+
collectArchivedFiles,
|
|
56
|
+
preservedForSource,
|
|
57
|
+
systemHead,
|
|
58
|
+
} from './region.js';
|
|
59
|
+
import { summarizeWithChain, resolveSummaryTargets, latestDurableTarget } from './summary.js';
|
|
60
|
+
import { asHostManualError } from './manual-error.js';
|
|
61
|
+
import {summaryTimeoutInfo,summaryTimeoutText} from './summary-timeout.js';
|
|
62
|
+
import { CHECKPOINT_PREAMBLE } from './prompt.js';
|
|
63
|
+
import { CompactionArchive, spanEntries } from './history.js';
|
|
64
|
+
import { planObservationMask, commitObservationMask } from './observation-mask.js';
|
|
65
|
+
import { CompactionJournal } from './journal.js';
|
|
66
|
+
import {auditMemorySelection,collectReferenceMessages} from './constraint-scope.js';
|
|
67
|
+
import {auditInstructionProvenance} from './audit.js';
|
|
68
|
+
import {
|
|
69
|
+
validateSummaryStructure,
|
|
70
|
+
validateCheckpointCoverage,
|
|
71
|
+
validateUserMessageSection,
|
|
72
|
+
} from './validate.js';
|
|
73
|
+
|
|
74
|
+
const CONTEXT_WINDOW_EXCEEDED_CODE = 'CONTEXT_WINDOW_EXCEEDED';
|
|
75
|
+
|
|
76
|
+
const COMPACT_CHECKPOINT_KIND = 'compact-checkpoint';
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 这次失败是不是"被取消"而不是"出错"。
|
|
80
|
+
*
|
|
81
|
+
* 两种来源都要认:
|
|
82
|
+
* - 错误对象自己带 `ABORTED`(`lib/summary.js` 里超时/中止时这么标);
|
|
83
|
+
* - 传入的 signal 已经 aborted,且错误就是那个 reason(agent 收回维护窗口、
|
|
84
|
+
* 用户按 Esc)。
|
|
85
|
+
*
|
|
86
|
+
* 判错的代价不对称:把取消当故障计入熔断,连续三次取消就会让这个会话
|
|
87
|
+
* **永久**不再自动压缩(`_stopped`),而用户其实什么都没做错。
|
|
88
|
+
*/
|
|
89
|
+
function isAbortError(error, signal) {
|
|
90
|
+
if (!error) return false;
|
|
91
|
+
if (error.code === 'ABORTED' || error.name === 'AbortError') return true;
|
|
92
|
+
if (signal?.aborted === true && (error === signal.reason || error?.cause === signal.reason)) return true;
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 官方 `compactCheckpointSource` 的等价实现(纯函数)。
|
|
98
|
+
*
|
|
99
|
+
* 与官方相比多带一个可选的 `preserved`:被压区间里用户原话的**结构化**副本。
|
|
100
|
+
* 没有它,下一代压缩只能从渲染后的文本里反解析原话(分隔符、块边界、用户
|
|
101
|
+
* 原文里恰好出现 `--- seq N ---` 都会让解析出错),或者干脆把最早的原话丢掉
|
|
102
|
+
* —— 两种都会让"逐字保留"在一两代之后名存实亡。
|
|
103
|
+
*
|
|
104
|
+
* `kind` 仍是 `compact-checkpoint`,宿主 `isCompactCheckpointSource` 照常认得;
|
|
105
|
+
* 多出来的字段不影响识别(宿主只检查 `source.kind`)。
|
|
106
|
+
*/
|
|
107
|
+
export function compactCheckpointSource(compactionId, sourceCommandId, preserved) {
|
|
108
|
+
const source = { kind: COMPACT_CHECKPOINT_KIND, compactionId };
|
|
109
|
+
if (sourceCommandId !== undefined) source.sourceCommandId = sourceCommandId;
|
|
110
|
+
if (Array.isArray(preserved) && preserved.length > 0) source.preserved = preserved;
|
|
111
|
+
return Object.freeze(source);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function isCompactCheckpointSource(source) {
|
|
115
|
+
return Boolean(source) && source.kind === COMPACT_CHECKPOINT_KIND;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let compactionCounter = 0;
|
|
119
|
+
function nextCompactionId() {
|
|
120
|
+
compactionCounter += 1;
|
|
121
|
+
const stamp = Date.now().toString(36);
|
|
122
|
+
const rand = Math.random().toString(36).slice(2, 8);
|
|
123
|
+
return `weave-${stamp}-${rand}-${compactionCounter}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 生成一个消息 id。宿主用 `randomUUID()` 造消息身份
|
|
128
|
+
* (`createMessage` 里 `id: brandString(randomUUID())`),而 `user/message`
|
|
129
|
+
* 事件的校验强制要求非空 `id`。这里不 import `node:crypto` 的具名导出以外的东西,
|
|
130
|
+
* 保持零运行时依赖。
|
|
131
|
+
*/
|
|
132
|
+
let messageCounter = 0;
|
|
133
|
+
function newMessageId() {
|
|
134
|
+
messageCounter += 1;
|
|
135
|
+
const globalCrypto = globalThis.crypto;
|
|
136
|
+
if (globalCrypto && typeof globalCrypto.randomUUID === 'function') {
|
|
137
|
+
return globalCrypto.randomUUID();
|
|
138
|
+
}
|
|
139
|
+
const stamp = Date.now().toString(36);
|
|
140
|
+
const rand = Math.random().toString(36).slice(2, 10);
|
|
141
|
+
return `msg-${stamp}-${rand}-${messageCounter}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class WeaveCompactionEngine {
|
|
145
|
+
/**
|
|
146
|
+
* @param {object} ctx
|
|
147
|
+
* @param {Readonly<Record<string, any>>} config
|
|
148
|
+
*/
|
|
149
|
+
constructor(ctx, config) {
|
|
150
|
+
this.ctx = ctx;
|
|
151
|
+
this._configScope = new AsyncLocalStorage();
|
|
152
|
+
this.config = config;
|
|
153
|
+
this.name = 'compaction';
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* ⚠️ 失败计数与熔断必须按**逻辑会话**记,不能按对象记。
|
|
157
|
+
*
|
|
158
|
+
* 会话对象会被宿主重建:进程重启后从磁盘恢复、UI 里切一次分支、
|
|
159
|
+
* 或者 agent 被重新组装,拿到的都是新对象。按对象记的后果是:
|
|
160
|
+
* 每次重建计数器归零、熔断闸门消失 —— 一个"每次都失败"的会话
|
|
161
|
+
* 只要用户重开一次就又能白试三轮,永远到不了"连续三次就别再试了"。
|
|
162
|
+
*
|
|
163
|
+
* 所以键取 `session.id`;拿不到稳定 id 时才退回对象身份(那时也确实
|
|
164
|
+
* 没有别的办法判断"是不是同一个会话")。
|
|
165
|
+
*
|
|
166
|
+
* @type {Map<object|string, number>} 连续失败次数
|
|
167
|
+
*/
|
|
168
|
+
this._failures = new Map();
|
|
169
|
+
/** @type {WeakMap<object, number>} 溢出重试次数 */
|
|
170
|
+
this._overflowRetries = new WeakMap();
|
|
171
|
+
/** @type {WeakMap<object, number>} 上次压缩后的 token 数,用于 minGrowthTokens */
|
|
172
|
+
this._lastTotal = new WeakMap();
|
|
173
|
+
/** @type {Map<object|string, true>} 已停压的会话(键同 `_failures`) */
|
|
174
|
+
this._stopped = new Map();
|
|
175
|
+
/** @type {Set<object>} 正在压缩的会话,防重入 */
|
|
176
|
+
this._busy = new Set();
|
|
177
|
+
this._recoveryBlocked = new Set();
|
|
178
|
+
this._hostFallbackWarned = new Set();
|
|
179
|
+
/** @type {WeakMap<object, object>} session → 正在为它压缩的 agent */
|
|
180
|
+
this._agentBySession = new WeakMap();
|
|
181
|
+
|
|
182
|
+
/** @type {WeakMap<object, Map<number, number>>} 最近一次计价的 per-seq token */
|
|
183
|
+
this._lastPriced = new WeakMap();
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @type {WeakMap<object, 'priced'|'heuristic'|'mixed'|'unknown'>}
|
|
187
|
+
* 最近一次计量的口径。必须**跟着数字一起往下传**:下游拿 `shadowedTokenCount`
|
|
188
|
+
* 去跟摘要比大小、去算压缩比,如果不知道这个数是实测还是 4 字符一 token 的
|
|
189
|
+
* 粗估,就没法判断"压完只小了一点点"到底是真的还是估算噪声。
|
|
190
|
+
*/
|
|
191
|
+
this._lastPrecision = new WeakMap();
|
|
192
|
+
|
|
193
|
+
this.archive = new CompactionArchive({ root: config.archiveDir });
|
|
194
|
+
this.diagnostics = new FailureDiagnostics(dirname(this.archive.root));
|
|
195
|
+
/** Durable decisions and stock-adapter backup metadata. Successful stock
|
|
196
|
+
* transactions publish only after this journal records committed; cold
|
|
197
|
+
* recovery runs in the root adapter before Session construction. */
|
|
198
|
+
this.journal = CompactionJournal.alongside(this.archive.root);
|
|
199
|
+
this.sessionState = new SessionStateStore(join(dirname(this.archive.root), 'state'));
|
|
200
|
+
this.proactive = new ProactiveCompaction(this);
|
|
201
|
+
this._registerHooks();
|
|
202
|
+
this._provideService();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
get config() { return this._configScope.getStore() ?? this._defaultConfig; }
|
|
206
|
+
set config(value) { this._defaultConfig = value; }
|
|
207
|
+
|
|
208
|
+
/** A complete operation, including retries and awaits, uses one snapshot.
|
|
209
|
+
* Parallel sessions may start with newer settings without changing this one. */
|
|
210
|
+
_withConfig(operation, onConfigError) {
|
|
211
|
+
if (this._configScope.getStore() || !this.readLiveConfig) return operation();
|
|
212
|
+
let config;
|
|
213
|
+
try { config = this.readLiveConfig(); }
|
|
214
|
+
catch (error) { onConfigError?.(error); throw error; }
|
|
215
|
+
this.config = config;
|
|
216
|
+
return this._configScope.run(config, operation);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 插件卸载时调用。这里不主动删 surface 上的任何事件(压缩是持久事实,
|
|
221
|
+
* 卸载插件不应该回滚已经写进会话的历史),只把内存态清掉。
|
|
222
|
+
*/
|
|
223
|
+
dispose() {
|
|
224
|
+
this.proactive.dispose();
|
|
225
|
+
this._busy.clear();
|
|
226
|
+
this._recoveryBlocked.clear();
|
|
227
|
+
this._stopped.clear();
|
|
228
|
+
this._failures.clear();
|
|
229
|
+
this._overflowRetries = new WeakMap();
|
|
230
|
+
this._lastTotal = new WeakMap();
|
|
231
|
+
this._lastPriced = new WeakMap();
|
|
232
|
+
this._lastPrecision = new WeakMap();
|
|
233
|
+
this._agentBySession = new WeakMap();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 熔断与失败计数用的键:优先用稳定逻辑标识(`session.id`),
|
|
238
|
+
* 拿不到才退回对象本身。见构造函数里 `_failures` 的说明。
|
|
239
|
+
* @param {object|undefined} session
|
|
240
|
+
*/
|
|
241
|
+
_keyOf(session) {
|
|
242
|
+
const id = session?.id;
|
|
243
|
+
if (typeof id === 'string' && id.length > 0) return id;
|
|
244
|
+
return session;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ────────────────────────────────────────────────────────────── 服务注册
|
|
248
|
+
|
|
249
|
+
_provideService() {
|
|
250
|
+
const ctx = this.ctx;
|
|
251
|
+
try {
|
|
252
|
+
if (typeof ctx.provide === 'function') {
|
|
253
|
+
ctx.provide('compaction', this);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (typeof ctx.reflect?.provide === 'function') {
|
|
257
|
+
ctx.reflect.provide('compaction', this);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
this.log('warn', 'ctx.provide is unavailable; the compaction service was not registered');
|
|
261
|
+
} catch (error) {
|
|
262
|
+
// 组内可能已经有官方实例(isolate 隔离),此时本引擎只作为备用不被采用。
|
|
263
|
+
this.log('warn', `compaction service registration skipped: ${error?.message ?? error}`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ────────────────────────────────────────────────────────────── 自动钩子
|
|
268
|
+
|
|
269
|
+
_registerHooks() {
|
|
270
|
+
const ctx = this.ctx;
|
|
271
|
+
if (typeof ctx.on !== 'function') return;
|
|
272
|
+
|
|
273
|
+
ctx.on('agent/pre-step', (payload, next) => this._withConfig(async () => {
|
|
274
|
+
const agent = payload?.agent;
|
|
275
|
+
const signal = payload?.signal;
|
|
276
|
+
if (agent && signal && !signal.aborted) {
|
|
277
|
+
// 上一次进程(或上一次崩溃)可能留了一把没关上的压缩锁。补 end 是
|
|
278
|
+
// 幂等的,且必须**早于**任何压缩尝试 —— 否则 `assertCompactionInactive`
|
|
279
|
+
// 会把这次压缩挡回去,锁永远打不开。
|
|
280
|
+
try {
|
|
281
|
+
await this.recoverPending(agent.session);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
const blocked = new Error(`compaction recovery must complete before another model request: ${error?.message ?? error}`);
|
|
284
|
+
blocked.code = 'RECOVERY_REQUIRED'; blocked.compactionStage = 'commit'; blocked.cause = error; this._restoreClaimedInput(agent,payload.messages);
|
|
285
|
+
await this.diagnostics.capture(agent.session,blocked,{trigger:'recovery',reportContext:this._diagnosticContext(agent)});
|
|
286
|
+
throw this._visibleFailure(blocked);
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
const proactive = await this.diagnostics.run(agent.session,{trigger:'proactive',signal,reportContext:this._diagnosticContext(agent)},()=>this.proactive.consume(agent, signal, payload.messages));
|
|
290
|
+
const checkPressure = !proactive || ['skipped','cancelled'].includes(proactive.status);
|
|
291
|
+
const idle = checkPressure ? await this._compactBeforeResume(agent, signal, payload.messages) : null;
|
|
292
|
+
const result = checkPressure && this.config.auto ? await this.compactIfNeeded(agent, 'pressure', signal) : null;
|
|
293
|
+
if (result) this.log('info', `pressure compaction done: ${describeResult(result)}`);
|
|
294
|
+
if(this.config.auto || idle) await this._guardRequestBudget(agent,signal,this.config.auto);
|
|
295
|
+
signal.throwIfAborted?.();
|
|
296
|
+
} catch (error) {
|
|
297
|
+
if (!isAbortError(error,signal)) await this.diagnostics.capture(agent.session,error,{trigger:'automatic',reportContext:this._diagnosticContext(agent)});
|
|
298
|
+
if (summaryTimeoutInfo(error)) { this._restoreClaimedInput(agent,payload.messages); throw this._automaticFailure(agent.session,error); }
|
|
299
|
+
if (['RECOVERY_REQUIRED', 'COMPACTION_CAPACITY_BLOCKED'].includes(error?.code)) { this._restoreClaimedInput(agent,payload.messages); throw this._visibleFailure(error); }
|
|
300
|
+
if (isAbortError(error,signal)) { this._restoreClaimedInput(agent,payload.messages); throw error; }
|
|
301
|
+
this.log('warn', `step compaction failed: ${error?.message ?? error}; stopping before the model request`);
|
|
302
|
+
this._restoreClaimedInput(agent,payload.messages);
|
|
303
|
+
throw this._automaticFailure(agent.session,error);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return next();
|
|
307
|
+
}, () => this._restoreClaimedInput(payload?.agent, payload?.messages)));
|
|
308
|
+
|
|
309
|
+
ctx.on('agent/status', (payload) => {
|
|
310
|
+
const agent = payload?.agent;
|
|
311
|
+
if (!agent) return;
|
|
312
|
+
if (payload.status === 'running') {
|
|
313
|
+
const session = agent.session;
|
|
314
|
+
const open = inspectCompactionEntryState(session).unmatchedCompactionStart;
|
|
315
|
+
const ownOpen = open !== null && String(session.eventAt(open)?.data?.compactionId ?? '').startsWith('weave-');
|
|
316
|
+
// Public status notification precedes turn/start. Prevent a new turn
|
|
317
|
+
// crossing an unfinished compaction, which the stock reader rejects.
|
|
318
|
+
if (this._recoveryBlocked.has(this._keyOf(session)) || ownOpen) {
|
|
319
|
+
agent.cancel({ kind: 'hook', reason: 'Compaction recovery must complete before another turn. Use /compact to inspect recovery.' }, { keepInbox: true });
|
|
320
|
+
this.log('warn', 'Pending compaction blocks a new turn; input remains queued. Recovery journal is retained.');
|
|
321
|
+
}
|
|
322
|
+
} else if (payload.status === 'idle') this._overflowRetries.delete(agent);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
ctx.on('session/event', (session, event) => {
|
|
326
|
+
if (event?.type !== 'assistant/message') return;
|
|
327
|
+
// `_busy` 里放的是 session,不是 agent;agent 只能从 `_agentBySession` 取。
|
|
328
|
+
const agent = this._agentBySession.get(session);
|
|
329
|
+
if (agent) this._overflowRetries.delete(agent);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
ctx.on('agent/request-error', (payload, next) => this._withConfig(async () => {
|
|
333
|
+
const agent = payload?.agent;
|
|
334
|
+
const failure = payload?.failure;
|
|
335
|
+
const signal = payload?.signal;
|
|
336
|
+
if (!agent || signal?.aborted) return next();
|
|
337
|
+
if (failure?.code !== CONTEXT_WINDOW_EXCEEDED_CODE) return next();
|
|
338
|
+
if (!this.config.auto) return next();
|
|
339
|
+
|
|
340
|
+
// 溢出重试上限也要走**逐模型策略**。宿主就是这么做的:
|
|
341
|
+
// `routedTarget(agent.session)` → `resolveTargetPolicy(this.config, target)`
|
|
342
|
+
// → 比较 `policy.maxOverflowRetries`
|
|
343
|
+
// (`dsh-compaction-basic/lib/index.js:861-868`)。
|
|
344
|
+
// 只读全局值的话,给某个模型单独配 `maxOverflowRetries: 0`(明确禁止为它
|
|
345
|
+
// 溢出压缩)会被全局值盖掉,配置写了等于没写。
|
|
346
|
+
const target = latestDurableTarget(agent);
|
|
347
|
+
const policy =
|
|
348
|
+
target === undefined ? this.config : resolveTargetPolicy(this.config, target.provider, target.model);
|
|
349
|
+
|
|
350
|
+
const retries = this._overflowRetries.get(agent) ?? 0;
|
|
351
|
+
if (retries >= policy.maxOverflowRetries) return next();
|
|
352
|
+
|
|
353
|
+
const generation = agent.session?.surface?.replaceGeneration ?? 0;
|
|
354
|
+
try {
|
|
355
|
+
const result = await this.compactIfNeeded(agent, 'context-overflow', signal);
|
|
356
|
+
if (signal?.aborted) return next();
|
|
357
|
+
const advanced = (agent.session?.surface?.replaceGeneration ?? 0) > generation;
|
|
358
|
+
if (!advanced && result === null) return next();
|
|
359
|
+
this._overflowRetries.set(agent, retries + 1);
|
|
360
|
+
return { kind: 'retry' };
|
|
361
|
+
} catch (error) {
|
|
362
|
+
if (signal?.aborted) return next();
|
|
363
|
+
await this.diagnostics.capture(agent.session,error,{trigger:'context-overflow',reportContext:this._diagnosticContext(agent)});
|
|
364
|
+
if (['RECOVERY_REQUIRED', 'COMPACTION_CAPACITY_BLOCKED'].includes(error?.code)) throw this._visibleFailure(error);
|
|
365
|
+
if (!signal?.aborted && !error?.surfaceRestored && (agent.session?.surface?.replaceGeneration ?? 0) > generation) {
|
|
366
|
+
this.log(
|
|
367
|
+
'warn',
|
|
368
|
+
`context-overflow compaction failed after in-memory surface replacement: ${error?.message ?? error}; retrying from the replacement surface`,
|
|
369
|
+
);
|
|
370
|
+
this._overflowRetries.set(agent, retries + 1);
|
|
371
|
+
return { kind: 'retry' };
|
|
372
|
+
}
|
|
373
|
+
this.log('warn', `context-overflow compaction failed: ${error?.message ?? error}`);
|
|
374
|
+
throw this._visibleFailure(error,'上下文超限后的压缩补救未完成。');
|
|
375
|
+
}
|
|
376
|
+
}));
|
|
377
|
+
|
|
378
|
+
// L6:官方把这条做成 waterfall,返回 true = "已同步记录了持久变更,可以重试"。
|
|
379
|
+
// 我们的策略:把保留尾多留一组(reactiveStep 条),重算范围后再压一次。
|
|
380
|
+
ctx.on('compaction/summary-error', (payload, next) => this._withConfig(async () => {
|
|
381
|
+
const session = payload?.session;
|
|
382
|
+
if (!session) return next();
|
|
383
|
+
if (payload?.error?.code === 'SUMMARY_TIMEOUT' || payload?.error?.code === 'SUMMARY_ALL_FAILED') {
|
|
384
|
+
return next();
|
|
385
|
+
}
|
|
386
|
+
const agent = findAgentForSession(this, session);
|
|
387
|
+
if (!agent) return next();
|
|
388
|
+
try {
|
|
389
|
+
const retried = await this._reactiveRetry(agent, payload?.signal);
|
|
390
|
+
return retried === true;
|
|
391
|
+
} catch (error) {
|
|
392
|
+
this.log('warn', `reactive compaction retry failed: ${error?.message ?? error}`);
|
|
393
|
+
return next();
|
|
394
|
+
}
|
|
395
|
+
}));
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* 溢出/prompt-too-long 时的渐进收敛:每次多保留一组最近消息,少压一点。
|
|
400
|
+
* @param {object} agent
|
|
401
|
+
* @param {AbortSignal|undefined} signal
|
|
402
|
+
*/
|
|
403
|
+
async _reactiveRetry(agent, signal) {
|
|
404
|
+
const session = agent?.session;
|
|
405
|
+
if (!session) return false;
|
|
406
|
+
// ⚠️ 熔断闸门必须在**每一个**自动/恢复入口上复查。
|
|
407
|
+
// 这个方法是 `compaction/summary-error` 的监听器调进来的,而那条 waterfall
|
|
408
|
+
// 是外部(宿主或别的插件)发起的 —— 它不知道我们已经"连续三次失败、
|
|
409
|
+
// 别再试了"。少了这一行,用户会看到第 4 次、第 5 次摘要请求照发不误,
|
|
410
|
+
// 每一次都注定失败,只是把失败计数继续往上推。
|
|
411
|
+
this._restoreState(session);
|
|
412
|
+
if (this._stopped.has(this._keyOf(session))) return false;
|
|
413
|
+
// 我们自己正在压这个会话时绝不重入:`_compactSpan` 已经在 catch 路径上
|
|
414
|
+
// 做过渐进收敛了,这里再压一次会和外层事务抢同一个 surface 锁。
|
|
415
|
+
if (this._busy.has(session)) return false;
|
|
416
|
+
const nodes = session.surface?.nodes ?? [];
|
|
417
|
+
if (nodes.length < 3) return false;
|
|
418
|
+
|
|
419
|
+
const measurement = await this._measure(session);
|
|
420
|
+
if (!measurement) return false;
|
|
421
|
+
|
|
422
|
+
let spec;
|
|
423
|
+
try { spec = await this._resolveSpec(agent, measurement, signal); }
|
|
424
|
+
catch (error) { if (!isAbortError(error, signal)) this._recordFailure(agent, session, error); throw error; }
|
|
425
|
+
if (!spec) return false;
|
|
426
|
+
|
|
427
|
+
// ⚠️ 这里的重试也必须**严格缩小**跨度,理由与 `compactIfNeeded` 里的
|
|
428
|
+
// `reselect` 完全相同:`extra` 只把保留量往上推,如果推完选出来的还是
|
|
429
|
+
// 同一段(大尾节点把可压前缀卡住时很常见),这次重试就是拿一模一样的
|
|
430
|
+
// 输入再烧一次摘要调用 —— 三次重试全打在同一段上,最后照样失败,
|
|
431
|
+
// 只是多花了两轮钱。
|
|
432
|
+
let previousEndIdx = -1;
|
|
433
|
+
for (let extra = 1; extra <= this.config.reactiveStep; extra += 1) {
|
|
434
|
+
if (signal?.aborted) return false;
|
|
435
|
+
// 每一轮重试前复查熔断:上一轮的失败可能刚好把它推过了阈值。
|
|
436
|
+
this._restoreState(session);
|
|
437
|
+
if (this._stopped.has(this._keyOf(session))) return false;
|
|
438
|
+
let retain = spec.retainTokens + extra * Math.max(1024, Math.floor(spec.retainTokens * 0.25));
|
|
439
|
+
let range = null;
|
|
440
|
+
for (let step = 0; step < 64; step += 1) {
|
|
441
|
+
const remeasured = step === 0 ? measurement : await this._measure(session);
|
|
442
|
+
if (!remeasured) return false;
|
|
443
|
+
const candidate = selectCompactableRange(session, remeasured, retain);
|
|
444
|
+
if (!candidate) break;
|
|
445
|
+
const endIdx = nodes.indexOf(candidate.end);
|
|
446
|
+
if (previousEndIdx < 0 || (endIdx >= 0 && endIdx < previousEndIdx)) {
|
|
447
|
+
range = candidate;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
retain = Math.ceil(retain * 1.5) + 1024;
|
|
451
|
+
}
|
|
452
|
+
if (!range) {
|
|
453
|
+
this.log('warn', 'reactive recovery found no strictly smaller span; giving up instead of repeating the same request');
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
try {
|
|
457
|
+
await this.compactRegion(range.start, range.end, agent, signal);
|
|
458
|
+
this.log('info', `reactive compaction succeeded with retainTokens=${retain}`);
|
|
459
|
+
return true;
|
|
460
|
+
} catch (error) {
|
|
461
|
+
if (signal?.aborted) return false;
|
|
462
|
+
const endIdx = nodes.indexOf(range.end);
|
|
463
|
+
if (endIdx >= 0) previousEndIdx = endIdx;
|
|
464
|
+
this.log('warn', `reactive attempt ${extra} failed: ${error?.message ?? error}`);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
_restoreClaimedInput(agent,messages) {
|
|
471
|
+
// pre-step runs after Inbox.claim and before user/message is committed.
|
|
472
|
+
// Requeue through the host's durable Inbox API without waking the driver.
|
|
473
|
+
if(!Array.isArray(messages)||!messages.length)return;
|
|
474
|
+
if(typeof agent.inbox?.prepend!=='function')throw new Error('Compaction stopped; host inbox cannot restore claimed input.');
|
|
475
|
+
for(const message of [...messages].reverse()){
|
|
476
|
+
if([...(agent.inbox.nextStep??[]),...(agent.inbox.nextTurn??[])].some(m=>m.id===message.id))continue;
|
|
477
|
+
agent.inbox.prepend('next-step',message);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
failureForCommand(session,commandId) { return this.diagnostics.forCommand(session,commandId); }
|
|
482
|
+
|
|
483
|
+
_visibleFailure(error,prefix='') {
|
|
484
|
+
const visible=new Error(prefix+failureExplanation(error)+diagnosticText(error));
|
|
485
|
+
visible.code=error?.code??'COMPACTION_FAILED';
|
|
486
|
+
visible.compactionStage=error?.compactionStage;
|
|
487
|
+
return transferDiagnostic(error,visible);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
_automaticFailure(session, error) {
|
|
491
|
+
if(error?.code==='COMPACTION_AUTOMATIC_BLOCKED'){
|
|
492
|
+
if(diagnosticFor(error)&&!error.message.includes('诊断编号:'))error.message+=diagnosticText(error);
|
|
493
|
+
return error;
|
|
494
|
+
}
|
|
495
|
+
const key=this._keyOf(session), stopped=this._stopped.has(key);
|
|
496
|
+
const code=typeof error?.code==='string'&&/^[A-Z_]{1,64}$/.test(error.code)?error.code:'COMPACTION_FAILED';
|
|
497
|
+
// AgentLoop records the thrown error in native turn/end; trajectory renders
|
|
498
|
+
// reason.error.message. No synthetic command or unsupported event is used.
|
|
499
|
+
const timeoutText=summaryTimeoutText(error);
|
|
500
|
+
const budgetText=continuationBudgetText(error);
|
|
501
|
+
if(budgetText)return transferDiagnostic(error,Object.assign(new Error(`${budgetText}本轮尚未发送主对话请求。${diagnosticText(error)}`),{code:'COMPACTION_AUTOMATIC_BLOCKED',failureCode:'CONTINUATION_CALL_BUDGET'}));
|
|
502
|
+
const blocked=new Error(timeoutText ? `${timeoutText}本轮尚未发送主对话请求。${stopped?'自动压缩已熔断。':''}` : `自动压缩未能完成,本轮已在发送模型请求前停止(${code})。${stopped?'自动压缩已熔断。':''}原始会话保留;可用 /compact 重试。`);
|
|
503
|
+
if(diagnosticFor(error)) blocked.message=(timeoutText??failureExplanation(error))+`本轮尚未发送主对话请求。${stopped?'自动压缩已熔断。':''}`+'检查后可用 /compact 重试。'+diagnosticText(error);
|
|
504
|
+
blocked.code='COMPACTION_AUTOMATIC_BLOCKED';
|
|
505
|
+
blocked.failureCode=code;
|
|
506
|
+
if(timeoutText){blocked.failureCode='SUMMARY_TIMEOUT';blocked.timeoutMs=summaryTimeoutInfo(error).timeoutMs;}
|
|
507
|
+
return transferDiagnostic(error,blocked);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
async _guardRequestBudget(agent,signal,pressure = true) {
|
|
511
|
+
const measurement=await this._measure(agent.session);
|
|
512
|
+
if(!measurement || signal?.aborted)return;
|
|
513
|
+
const spec=await this._resolveSpec(agent,measurement,signal,{budgetOnly:!pressure});
|
|
514
|
+
if(spec && measurement.totalTokens>=spec.messageBudgetTokens){
|
|
515
|
+
throw this._automaticFailure(agent.session,{code:'COMPACTION_REQUEST_BUDGET_EXCEEDED'});
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
async _compactBeforeResume(agent, signal, messages) {
|
|
520
|
+
const interval = idleResumeDue(agent.session, messages, this.config.idleCompactionMinutes);
|
|
521
|
+
if (!interval) return null;
|
|
522
|
+
signal?.throwIfAborted?.();
|
|
523
|
+
this._restoreState(agent.session);
|
|
524
|
+
if (this._stopped.has(this._keyOf(agent.session))) throw this._automaticFailure(agent.session, {code:'COMPACTION_STOPPED'});
|
|
525
|
+
const result = await this.diagnostics.run(agent.session, {trigger:'idle',signal,reportContext:this._diagnosticContext(agent)},
|
|
526
|
+
() => this._compactManual(agent, signal, undefined, false, true, {idle:true}));
|
|
527
|
+
signal?.throwIfAborted?.();
|
|
528
|
+
this.log('info', `idle resume after ${Math.floor(interval.elapsedMs / 60000)} minute(s): ${result ? 'compaction committed' : 'no shrinkable history'}`);
|
|
529
|
+
return {status:result ? 'compacted' : 'skipped', result};
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
_idleContinuationCanShrink(session, range, policy) {
|
|
533
|
+
const nodes = session.surface.nodes;
|
|
534
|
+
const seqs = nodes.slice(nodes.indexOf(range.start), nodes.indexOf(range.end) + 1);
|
|
535
|
+
const selection = selectSessionMemory(session, seqs, {maxMessages:policy.preserveRecentMessages,maxTokens:policy.preserveRecentUserTokens,approxTokens});
|
|
536
|
+
const progress = collectProgressFacts(session, seqs);
|
|
537
|
+
const workReport = scopeWorkReport(collectWorkReport(session, seqs), progress);
|
|
538
|
+
const preserved = residentConstraints(selection.active, session);
|
|
539
|
+
// This is the same required envelope used by the continuation planner,
|
|
540
|
+
// before working notes. It distinguishes tiny histories from large user
|
|
541
|
+
// messages which cannot shrink under verbatim checkpoints but can continue.
|
|
542
|
+
let base;
|
|
543
|
+
try { ({base} = planContinuationMemory(preserved, policy, {workReport,progress})); }
|
|
544
|
+
catch (error) {
|
|
545
|
+
// No legal continuation can retain these mandatory requirements under
|
|
546
|
+
// the configured envelope. Keep the history; the main budget guard still
|
|
547
|
+
// blocks it if it cannot fit, without charging a futile summary attempt.
|
|
548
|
+
if (error.code === 'CONTINUATION_REQUIREMENTS_TOO_LARGE') return false;
|
|
549
|
+
throw error;
|
|
550
|
+
}
|
|
551
|
+
const blocks = [{type:'text',text:base}];
|
|
552
|
+
const source = seqs.map(seq => session.deriveEventMessage(session.eventAt(seq))).filter(Boolean);
|
|
553
|
+
return canonicalMessageBytes([{role:'user',content:blocks}]) < canonicalMessageBytes(source) &&
|
|
554
|
+
this._estimateCheckpointTokens(blocks) < this._shadowedTokens(session, seqs);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// ────────────────────────────────────────────────────────────── 三个方法
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* 自动压缩入口。
|
|
561
|
+
* @param {object} agent
|
|
562
|
+
* @param {'pressure'|'context-overflow'} trigger
|
|
563
|
+
* @param {AbortSignal} signal
|
|
564
|
+
*/
|
|
565
|
+
async compactIfNeeded(agent, trigger, signal) {
|
|
566
|
+
return this._withConfig(() => this.diagnostics.run(agent?.session,{trigger,signal,reportContext:this._diagnosticContext(agent)},()=>this._compactIfNeededInner(agent,trigger,signal)));
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
_diagnosticContext(agent) {
|
|
570
|
+
// Logging must remain observational even if host model-policy lookup fails.
|
|
571
|
+
try {return diagnosticContext(agent,this._resolvePolicy(agent));}
|
|
572
|
+
catch {return diagnosticContext(agent,this.config);}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
async _compactIfNeededInner(agent, trigger, signal) {
|
|
576
|
+
const session = agent?.session;
|
|
577
|
+
if (!session) return null;
|
|
578
|
+
this._restoreState(session);
|
|
579
|
+
const stopped = this._stopped.has(this._keyOf(session));
|
|
580
|
+
if (this._busy.has(session)) return null;
|
|
581
|
+
if (signal?.aborted) return null;
|
|
582
|
+
let pendingContinuation = readPendingContinuation(this.sessionState, session);
|
|
583
|
+
if (pendingContinuation) { await this.recoverPending(session); pendingContinuation = readPendingContinuation(this.sessionState, session); }
|
|
584
|
+
|
|
585
|
+
const measurement = await this._measure(session);
|
|
586
|
+
if (!measurement) return null;
|
|
587
|
+
|
|
588
|
+
let spec;
|
|
589
|
+
try { spec = await this._resolveSpec(agent, measurement, signal); }
|
|
590
|
+
catch (error) { if (!stopped && !isAbortError(error, signal)) this._recordFailure(agent, session, error); throw error; }
|
|
591
|
+
if (!spec) return null;
|
|
592
|
+
|
|
593
|
+
const total = measurement.totalTokens;
|
|
594
|
+
noteDiagnosticMeasurement(measurement,{thresholdTokens:spec.thresholdTokens});
|
|
595
|
+
if (stopped) {
|
|
596
|
+
if (pendingContinuation || (trigger === 'pressure' && total >= spec.thresholdTokens)) throw this._automaticFailure(session,{code:'COMPACTION_STOPPED'});
|
|
597
|
+
return null;
|
|
598
|
+
}
|
|
599
|
+
if (trigger === 'pressure' && !pendingContinuation) {
|
|
600
|
+
if (total < spec.thresholdTokens) return null;
|
|
601
|
+
// 计量口径不精确时,把"离阈值还有多远"写进日志。
|
|
602
|
+
// 这是唯一能让用户自己判断"我的会话是不是其实早就该压了"的信息 ——
|
|
603
|
+
// 数字本身看不出来它是真分词还是 4 字符/token 的粗估。
|
|
604
|
+
if (measurement.precision !== 'priced' && this.config.debug) {
|
|
605
|
+
this.log(
|
|
606
|
+
'info',
|
|
607
|
+
`token measurement is ${measurement.precision} (not exact); total ${total} vs threshold ${spec.thresholdTokens}`,
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
// 把**同一套容量口径**写进日志:触发判定用的是"输入侧实测/粗估总量",
|
|
611
|
+
// 而窗口预算扣的是"这次请求实际会用的输出上限"(`requestHeader` 里的
|
|
612
|
+
// `maxTokens`,缺省回退适配器默认值)。两者必须能对上账,否则用户看到
|
|
613
|
+
// 的"还有余量"和供应商那边的 413 会是两回事,无从判断该调哪个参数。
|
|
614
|
+
if (this.config.debug) {
|
|
615
|
+
this.log(
|
|
616
|
+
'info',
|
|
617
|
+
`capacity: window ${spec.contextWindow}, reserved output ${spec.contextWindow - spec.messageBudgetTokens}, ` +
|
|
618
|
+
`message budget ${spec.messageBudgetTokens}, headroom ${spec.messageBudgetTokens - spec.pressureBudgetTokens}, ` +
|
|
619
|
+
`trigger at ${spec.thresholdTokens}, current input ${total}`,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
if (this.config.minGrowthTokens > 0) {
|
|
623
|
+
const last = this._lastTotal.get(session);
|
|
624
|
+
if (typeof last === 'number' && total - last < this.config.minGrowthTokens) return null;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
let currentMeasurement = measurement;
|
|
629
|
+
const initialReductionTarget = Math.min(spec.thresholdTokens - 1, Math.floor(currentMeasurement.totalTokens * (1 - (spec.policy.minimumReductionRatio ?? 0.2))));
|
|
630
|
+
noteDiagnosticMeasurement(currentMeasurement,{thresholdTokens:spec.thresholdTokens,targetTokens:initialReductionTarget});
|
|
631
|
+
let reductionTarget = initialReductionTarget;
|
|
632
|
+
const continuationBudget = async range => {
|
|
633
|
+
const fresh = await this._measure(session);
|
|
634
|
+
const nodes = session.surface.nodes;
|
|
635
|
+
const startIndex = nodes.indexOf(range.start), endIndex = nodes.indexOf(range.end);
|
|
636
|
+
const archivedTokens = this._shadowedTokens(session, nodes.slice(startIndex, endIndex + 1));
|
|
637
|
+
const retainedTokens = Math.max(0, (fresh?.totalTokens ?? currentMeasurement.totalTokens) - archivedTokens);
|
|
638
|
+
const available = Math.min(Math.floor(spec.thresholdTokens / 2), reductionTarget - retainedTokens);
|
|
639
|
+
if (available <= 0) throw Object.assign(new Error('Protected system context leaves insufficient room for the requested minimum reduction.'), {code:'CONTINUATION_TOO_LARGE'});
|
|
640
|
+
return available;
|
|
641
|
+
};
|
|
642
|
+
this._busy.add(session);
|
|
643
|
+
this._agentBySession.set(session, agent);
|
|
644
|
+
try {
|
|
645
|
+
// Stage 1: zero-LLM-cost archive-backed observation masking (Complexity Trap / CADOC).
|
|
646
|
+
// On `pressure`, commit Stage 1 only when tool-output savings alone reach `reductionTarget`
|
|
647
|
+
// so we avoid invalidating the warm KV prefix right before a Stage 2 LLM summary.
|
|
648
|
+
// On `context-overflow`, always archive & mask before host pruning so full bytes are saved.
|
|
649
|
+
if (!pendingContinuation && spec.policy.observationMaskingEnabled !== false && spec.policy.archiveEnabled && this.historyToolsAvailable) {
|
|
650
|
+
const maskPlan = planObservationMask(session, currentMeasurement, spec.retainTokens, {
|
|
651
|
+
compactionId: nextCompactionId(),
|
|
652
|
+
thresholdChars: spec.policy.observationMaskThresholdChars,
|
|
653
|
+
headChars: spec.policy.observationMaskHeadChars,
|
|
654
|
+
tailChars: spec.policy.observationMaskTailChars,
|
|
655
|
+
});
|
|
656
|
+
const maskAloneReachesTarget = Boolean(
|
|
657
|
+
maskPlan && (currentMeasurement.totalTokens - maskPlan.totalTokensSaved <= initialReductionTarget),
|
|
658
|
+
);
|
|
659
|
+
if (maskPlan && (trigger === 'context-overflow' || maskAloneReachesTarget)) {
|
|
660
|
+
const flushSession = typeof this.ctx.sessions?.flush === 'function'
|
|
661
|
+
? (targetSession) => this._flushSession(targetSession)
|
|
662
|
+
: null;
|
|
663
|
+
let masked;
|
|
664
|
+
try {
|
|
665
|
+
masked = await commitObservationMask({
|
|
666
|
+
session,
|
|
667
|
+
plan: maskPlan,
|
|
668
|
+
archive: this.archive,
|
|
669
|
+
log: (level, msg) => this.log(level, msg),
|
|
670
|
+
flushSession,
|
|
671
|
+
signal,
|
|
672
|
+
});
|
|
673
|
+
} catch (error) {
|
|
674
|
+
if (!isAbortError(error, signal)) this._recordFailure(agent, session, error);
|
|
675
|
+
throw error;
|
|
676
|
+
}
|
|
677
|
+
if (masked) {
|
|
678
|
+
const afterMask = await this._measure(session);
|
|
679
|
+
if (afterMask) {
|
|
680
|
+
currentMeasurement = afterMask;
|
|
681
|
+
this._lastTotal.set(session, afterMask.totalTokens);
|
|
682
|
+
reductionTarget = Math.min(
|
|
683
|
+
spec.thresholdTokens - 1,
|
|
684
|
+
Math.floor(currentMeasurement.totalTokens * (1 - (spec.policy.minimumReductionRatio ?? 0.2))),
|
|
685
|
+
);
|
|
686
|
+
noteDiagnosticMeasurement(currentMeasurement, { thresholdTokens: spec.thresholdTokens, targetTokens: reductionTarget });
|
|
687
|
+
if (afterMask.totalTokens < spec.thresholdTokens && afterMask.totalTokens <= initialReductionTarget) {
|
|
688
|
+
return masked;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// 溢出时先把超长工具结果剪一遍,再压——这比直接吞整个前缀便宜得多。
|
|
696
|
+
// ⚠️ 裁剪会替换/追加 surface 节点,所以**必须重新计量**:`selectCompactableRange`
|
|
697
|
+
// 会逐项比对 `measurement.nodes` 与 `session.surface.nodes`,拿裁剪前的计量去选区
|
|
698
|
+
// 必然抛 `token-meter surface does not match the current session surface`,
|
|
699
|
+
// 表现为"溢出路径一次摘要都没调就失败"。
|
|
700
|
+
if (trigger === 'context-overflow' && !pendingContinuation) {
|
|
701
|
+
await this._pruneToolResults(session);
|
|
702
|
+
const afterPrune = await this._measure(session);
|
|
703
|
+
if (afterPrune) {
|
|
704
|
+
currentMeasurement = afterPrune;
|
|
705
|
+
reductionTarget = Math.min(spec.thresholdTokens - 1, Math.floor(currentMeasurement.totalTokens * (1 - (spec.policy.minimumReductionRatio ?? 0.2))));
|
|
706
|
+
noteDiagnosticMeasurement(currentMeasurement,{thresholdTokens:spec.thresholdTokens,targetTokens:reductionTarget});
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
// ⚠️ 溢出也**从保留区起步**,不是一上来 retain=0。
|
|
710
|
+
// 设计目标是"不要一次性全部折叠,每次多保留一组再试":retain=0 意味着一次
|
|
711
|
+
// 吞掉尽可能大的前缀,正好与"渐进收敛"相反。保留区给不出可压缩跨度时,
|
|
712
|
+
// 无合法边界时保留原尾部;不能靠吞掉最新消息伪造压缩成功。
|
|
713
|
+
if (pendingContinuation || (spec.policy.continuationEnabled && hasContinuation(session))) {
|
|
714
|
+
let range = pendingContinuation ? {start:pendingContinuation.seqs[0],end:pendingContinuation.seqs.at(-1)} : selectCachePreservingRange(session,currentMeasurement,spec.retainTokens,spec.policy,reductionTarget,{continuation:true}) ?? selectContinuationRange(session);
|
|
715
|
+
if (range) {
|
|
716
|
+
let budget;
|
|
717
|
+
const preflight=candidate=>{
|
|
718
|
+
const nodes=session.surface.nodes,seqs=nodes.slice(nodes.indexOf(candidate.start),nodes.indexOf(candidate.end)+1);
|
|
719
|
+
const selected=selectSessionMemory(session,seqs),progress=collectProgressFacts(session,seqs);
|
|
720
|
+
planContinuationMemory(residentConstraints(selected.active,session),spec.policy,{budgetTokens:budget,estimateTokens:blocks=>this._estimateCheckpointTokens(blocks),workReport:scopeWorkReport(collectWorkReport(session,seqs),progress),progress});
|
|
721
|
+
};
|
|
722
|
+
try {
|
|
723
|
+
budget=pendingContinuation?.budgetTokens ?? await continuationBudget(range);
|
|
724
|
+
if(!pendingContinuation)preflight(range);
|
|
725
|
+
}
|
|
726
|
+
catch(error) {
|
|
727
|
+
// Re-select only before starting a transaction or paid request.
|
|
728
|
+
// Frozen progress must keep its original range and call receipts.
|
|
729
|
+
if(pendingContinuation||!['CONTINUATION_TOO_LARGE','CONTINUATION_REQUIREMENTS_TOO_LARGE'].includes(error.code))throw error;
|
|
730
|
+
const full=selectContinuationRange(session);
|
|
731
|
+
if(!full||full.start===range.start&&full.end===range.end)throw error;
|
|
732
|
+
range=full;budget=await continuationBudget(range);preflight(range);
|
|
733
|
+
}
|
|
734
|
+
return await this._compactRegionCore(range.start,range.end,agent,signal,{manual:false,policy:spec.policy,continuation:true,continuationBudgetTokens:budget});
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
let retainTokens = spec.retainTokens;
|
|
738
|
+
let attempts = 0;
|
|
739
|
+
const maxAttempts = this.config.maxSummaryChunks;
|
|
740
|
+
let lastResult = null;
|
|
741
|
+
let pendingOriginals;
|
|
742
|
+
const chunkResults = [];
|
|
743
|
+
const capacityContext = {}; // Keep provider overflow feedback across this batch.
|
|
744
|
+
const finishBatch = (measured, batchCause = null) => {
|
|
745
|
+
if (lastResult && measured && spec.policy.continuationEnabled && this.historyToolsAvailable && measured.totalTokens > reductionTarget) throw Object.assign(new Error('Compaction leaves too little headroom for stable prefix reuse; switching to bounded continuation.'), {code:'COMPACTION_CAPACITY_BLOCKED'});
|
|
746
|
+
if (measured && (measured.totalTokens >= spec.messageBudgetTokens || (spec.policy.continuationEnabled && this.historyToolsAvailable && measured.totalTokens >= spec.thresholdTokens))) {
|
|
747
|
+
const overMessageBudget = measured.totalTokens >= spec.messageBudgetTokens;
|
|
748
|
+
const limitDesc = overMessageBudget
|
|
749
|
+
? `${spec.messageBudgetTokens} message budget`
|
|
750
|
+
: `${spec.thresholdTokens} compaction threshold (message budget ${spec.messageBudgetTokens})`;
|
|
751
|
+
const suffix = batchCause?.message ? ` (cause: ${batchCause.message})` : '';
|
|
752
|
+
const error = new Error(
|
|
753
|
+
`compaction cannot fit the next main request: ${measured.totalTokens} estimated input tokens >= ${limitDesc}; ${chunkResults.length} chunks committed, remaining history retained; use /compact or a larger-capacity target${suffix}`,
|
|
754
|
+
);
|
|
755
|
+
error.code = 'COMPACTION_CAPACITY_BLOCKED';
|
|
756
|
+
error.compactionIds = chunkResults.flatMap(r => r.compactionIds ?? [r.compactionId]);
|
|
757
|
+
throw error;
|
|
758
|
+
}
|
|
759
|
+
return lastResult;
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
while (attempts < maxAttempts) {
|
|
763
|
+
attempts += 1;
|
|
764
|
+
if (signal?.aborted) return lastResult;
|
|
765
|
+
|
|
766
|
+
const fresh = attempts === 1 ? currentMeasurement : await this._measure(session);
|
|
767
|
+
if (!fresh) return lastResult;
|
|
768
|
+
if (fresh.totalTokens < spec.thresholdTokens && lastResult !== null) return finishBatch(fresh);
|
|
769
|
+
|
|
770
|
+
let range;
|
|
771
|
+
if (pendingOriginals) {
|
|
772
|
+
const live = new Set(session.surface.nodes);
|
|
773
|
+
pendingOriginals = pendingOriginals.filter(seq => live.has(seq));
|
|
774
|
+
if (!pendingOriginals.length) return finishBatch(fresh);
|
|
775
|
+
range = { start: pendingOriginals[0], end: pendingOriginals.at(-1) };
|
|
776
|
+
} else range = selectCachePreservingRange(session, fresh, retainTokens, spec.policy, reductionTarget) ?? selectCompactableRange(session, fresh, retainTokens);
|
|
777
|
+
if (range && !pendingOriginals?.length) {
|
|
778
|
+
const currentNodes = session.surface.nodes;
|
|
779
|
+
pendingOriginals = currentNodes.slice(currentNodes.indexOf(range.start), currentNodes.indexOf(range.end)+1);
|
|
780
|
+
}
|
|
781
|
+
if (!range) {
|
|
782
|
+
// No legal smaller span: preserve the configured recent groups and stop.
|
|
783
|
+
return finishBatch(fresh);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
try {
|
|
787
|
+
lastResult = await this._compactRegionCore(range.start, range.end, agent, signal, {
|
|
788
|
+
manual: false,
|
|
789
|
+
// 摘要输出上限来自**按 provider/model 合并过**的 spec,不是全局配置。
|
|
790
|
+
maxTokens: spec.maxTokens,
|
|
791
|
+
// 合并后的整份策略一起带下去:摘要目标、超时、语言、保留尾都按
|
|
792
|
+
// 同一个 provider/model 解析,不能一半按目标一半按全局。
|
|
793
|
+
policy: spec.policy,
|
|
794
|
+
capacityContext,
|
|
795
|
+
});
|
|
796
|
+
if (lastResult) { chunkResults.push(lastResult); lastResult = combineCompactionResults(chunkResults); }
|
|
797
|
+
} catch (error) {
|
|
798
|
+
if (signal?.aborted) return lastResult;
|
|
799
|
+
const message = error?.message ?? String(error);
|
|
800
|
+
if (/not a balanced boundary|not found in surface/.test(message)) {
|
|
801
|
+
this.log('warn', `range rejected by the surface (${message}); keeping remaining history`);
|
|
802
|
+
return finishBatch(await this._measure(session));
|
|
803
|
+
}
|
|
804
|
+
if (error?.code === 'RECOVERY_REQUIRED') throw error;
|
|
805
|
+
// Keep the original cause while refusing a known-oversized main request.
|
|
806
|
+
try { finishBatch(await this._measure(session), error); }
|
|
807
|
+
catch (blocked) { blocked.cause = error; throw blocked; }
|
|
808
|
+
if (chunkResults.length) {
|
|
809
|
+
error.message = `${chunkResults.length} summary chunk(s) committed; remaining history retained: ${error.message}`;
|
|
810
|
+
error.compactionIds = chunkResults.map(r => r.compactionId);
|
|
811
|
+
}
|
|
812
|
+
throw error;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
const after = await this._measure(session);
|
|
816
|
+
if (!after) return lastResult;
|
|
817
|
+
this._lastTotal.set(session, after.totalTokens);
|
|
818
|
+
if (after.totalTokens < spec.thresholdTokens) return finishBatch(after);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const after = await this._measure(session);
|
|
822
|
+
if (after && after.totalTokens >= spec.thresholdTokens) {
|
|
823
|
+
this.log(
|
|
824
|
+
'warn',
|
|
825
|
+
`compaction still above threshold after ${attempts} attempt(s) (${after.totalTokens} estimated tokens >= threshold ${spec.thresholdTokens})`,
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
return finishBatch(after);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
if (!error?.surfaceMutated && spec.policy.continuationEnabled && spec.policy.archiveEnabled && this.historyToolsAvailable && continuationEligible(error) && !signal?.aborted) {
|
|
831
|
+
const fresh = await this._measure(session);
|
|
832
|
+
const range = selectContinuationRange(session);
|
|
833
|
+
if (range) return await this._compactRegionCore(range.start, range.end, agent, signal, {manual:false,policy:spec.policy,continuation:true,continuationBudgetTokens:await continuationBudget(range)});
|
|
834
|
+
}
|
|
835
|
+
throw error;
|
|
836
|
+
} finally {
|
|
837
|
+
this._busy.delete(session);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* 手动 `/compact`。
|
|
843
|
+
* @param {object} agent
|
|
844
|
+
* @param {AbortSignal} signal
|
|
845
|
+
* @param {string} [sourceCommandId]
|
|
846
|
+
*/
|
|
847
|
+
async compactNow(agent, signal, sourceCommandId, continuation = false) {
|
|
848
|
+
try {
|
|
849
|
+
return await this._withConfig(() => this.diagnostics.run(agent?.session,{trigger:continuation?'manual-continuation':'manual',commandId:sourceCommandId,signal,reportContext:this._diagnosticContext(agent)},()=>this._compactNowInner(agent, signal, sourceCommandId, continuation)));
|
|
850
|
+
} catch (error) {
|
|
851
|
+
// ⚠️ 手动路径的**唯一出口**。宿主的 `/compact` 只把 `ManualCompactionError`
|
|
852
|
+
// 翻译成人话,其它任何 Error 都原样 rethrow(`dsh-command-compact/lib/index.js:65-72`),
|
|
853
|
+
// 用户看到的是命令层未处理异常,而不是"这次为什么没压成"。
|
|
854
|
+
// 收敛点放在这一层,而不是在每个 throw 点各写一遍:手动压缩要穿过四层,
|
|
855
|
+
// 逐点改造时任何一处新增 throw 都会重新长出未处理异常。
|
|
856
|
+
//
|
|
857
|
+
// 而且必须是**宿主那个类**的实例:`instanceof` 只认同一个类对象,
|
|
858
|
+
// 自己声明一个同名同码的类在这里是无效的(见 `manual-error.js`)。
|
|
859
|
+
throw await asHostManualError(this.ctx, error, error?.compactionStage);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
async _compactNowInner(agent, signal, sourceCommandId, continuation = false) {
|
|
864
|
+
const runMaintenance = agent?.runMaintenance;
|
|
865
|
+
if (typeof runMaintenance !== 'function') {
|
|
866
|
+
const error = new Error('manual compaction requires an agent that supports maintenance tasks');
|
|
867
|
+
error.code = 'busy';
|
|
868
|
+
throw error;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// ⚠️ 必须区分两种失败:
|
|
872
|
+
// (a) `runMaintenance` 自己拒绝——agent 不空闲、有排队工作。这才是 busy。
|
|
873
|
+
// (b) 压缩过程里真正的业务错误——鉴权失败、摘要超时、持久化失败。
|
|
874
|
+
// 把 (b) 也一律包成 busy("agent 不空闲"),用户看到的是一句完全不相关
|
|
875
|
+
// 的话,真实原因只躺在 `cause` 里。
|
|
876
|
+
let entered = false;
|
|
877
|
+
// ⚠️ 维护窗口的 signal 要留到 catch 里用。合并后的 `combined` 只能告诉你
|
|
878
|
+
// "中止了",但看不出是**谁**中止的:agent 收回维护窗口(用户按了 Esc、
|
|
879
|
+
// 或同一会话要开始新的一轮)与压缩本身出错,对用户是两件事。
|
|
880
|
+
// 宿主 `dsh-compaction-basic/lib/index.js:1003-1005` 判据是
|
|
881
|
+
// `agentSignal.aborted && operationSignal.reason === agentSignal.reason`:
|
|
882
|
+
// reason 相同说明触发中止的正是维护控制器,而不是命令自己的 signal。
|
|
883
|
+
let maintenanceSignal;
|
|
884
|
+
let combinedSignal;
|
|
885
|
+
try {
|
|
886
|
+
return await runMaintenance.call(agent, async (agentSignal) => {
|
|
887
|
+
entered = true;
|
|
888
|
+
maintenanceSignal = agentSignal;
|
|
889
|
+
const merged = combineSignals([agentSignal, signal]);
|
|
890
|
+
combinedSignal = merged.signal;
|
|
891
|
+
try { return await this._compactManual(agent, merged.signal, sourceCommandId, continuation); }
|
|
892
|
+
finally { merged.dispose(); }
|
|
893
|
+
});
|
|
894
|
+
} catch (error) {
|
|
895
|
+
if (!entered) {
|
|
896
|
+
// 回调根本没跑起来 = 维护窗口没抢到,这确实是 busy。
|
|
897
|
+
const busy = new Error('manual compaction requires an idle agent with no waking queued work');
|
|
898
|
+
busy.code = 'busy';
|
|
899
|
+
busy.cause = error;
|
|
900
|
+
throw busy;
|
|
901
|
+
}
|
|
902
|
+
// 维护窗口被收回 = 取消,不是失败。
|
|
903
|
+
// 不认这一条会有两个后果:用户看到的是"摘要失败"这种完全不对路的提示;
|
|
904
|
+
// 而且取消会被 `_recordFailure` 计入熔断,按三次取消就让会话永久停压。
|
|
905
|
+
const ownerAborted =
|
|
906
|
+
maintenanceSignal?.aborted === true && combinedSignal?.reason === maintenanceSignal.reason;
|
|
907
|
+
if (ownerAborted) {
|
|
908
|
+
const cancelled = new Error(error.compactionIds?.length
|
|
909
|
+
? `manual compaction was cancelled after ${error.compactionIds.length} committed chunk(s); remaining history retained`
|
|
910
|
+
: 'manual compaction was cancelled');
|
|
911
|
+
cancelled.compactionIds = error.compactionIds;
|
|
912
|
+
cancelled.code = 'cancelled';
|
|
913
|
+
cancelled.cause = error;
|
|
914
|
+
throw cancelled;
|
|
915
|
+
}
|
|
916
|
+
// 已经进入压缩流程:原样抛出,保留真实 code / message / cause,
|
|
917
|
+
// 由 `compactNow` 统一转成官方形状。
|
|
918
|
+
throw error;
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
async _compactManual(agent, signal, sourceCommandId, continuation = false, proactive = false, {idle = false} = {}) {
|
|
923
|
+
const manual = !proactive;
|
|
924
|
+
const session = agent?.session;
|
|
925
|
+
if (!session) return null;
|
|
926
|
+
if (this._busy.has(session)) {
|
|
927
|
+
const error = new Error('manual compaction: compaction already in progress');
|
|
928
|
+
error.code = 'busy';
|
|
929
|
+
throw error;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
this._busy.add(session);
|
|
933
|
+
this._agentBySession.set(session, agent);
|
|
934
|
+
try {
|
|
935
|
+
const measurement = await this._measure(session);
|
|
936
|
+
if (!measurement) return null;
|
|
937
|
+
noteDiagnosticMeasurement(measurement);
|
|
938
|
+
// ⚠️ 手动压缩**不能**经过 `_resolveSpec`。
|
|
939
|
+
//
|
|
940
|
+
// `_resolveSpec` 算的是"自动压力压缩该在什么水位触发":它要
|
|
941
|
+
// `contextWindow − reserved − headroomTokens` 必须为正,否则抛
|
|
942
|
+
// `no pressure budget`。那是自动入口的前置条件,跟"我现在想把已有历史
|
|
943
|
+
// 主动压一遍"没有关系。小窗口 + 大 headroom(例如 32768 窗口配默认
|
|
944
|
+
// headroomTokens=33000)时自动预算必然无效,此时手动入口如果也走这条
|
|
945
|
+
// 路,就会在**一个摘要请求都没发**的情况下报"摘要失败",用户看到的是
|
|
946
|
+
// 一个和实际原因完全不相干的错误。
|
|
947
|
+
//
|
|
948
|
+
// 官方 `compactNow`(`dsh-compaction-basic/lib/index.js:985-1001`)就是
|
|
949
|
+
// 以 `retainTokens = 0` 直接选区进事务,不先求压力 spec。
|
|
950
|
+
//
|
|
951
|
+
// 手动压缩要的只有两样:目标策略(决定摘要发给谁、输出上限多少)和
|
|
952
|
+
// 一个选区。策略用不抛错的 `_resolvePolicy`,选区按 0 保留量。
|
|
953
|
+
const resolvedPolicy = this._resolvePolicy(agent);
|
|
954
|
+
// The user chose to compact after a cold interval: retaining old prefix
|
|
955
|
+
// checkpoints for cache reuse would defeat that choice. Other entry points
|
|
956
|
+
// keep their cache policy, and the normal archive/audit/transaction remains.
|
|
957
|
+
const policy = idle ? {...resolvedPolicy, cachePreservePrefix:false} : resolvedPolicy;
|
|
958
|
+
let pendingContinuation = readPendingContinuation(this.sessionState, session);
|
|
959
|
+
if (pendingContinuation) { await this.recoverPending(session); pendingContinuation = readPendingContinuation(this.sessionState, session); }
|
|
960
|
+
if (idle && !pendingContinuation && (policy.idleCompactionMinTokens ?? 0) > 0 && measurement.totalTokens < policy.idleCompactionMinTokens) {
|
|
961
|
+
return null;
|
|
962
|
+
}
|
|
963
|
+
const rollover = pendingContinuation || continuation || (policy.continuationEnabled && hasContinuation(session));
|
|
964
|
+
const cacheRetain=policy.retainTokens??Math.max(4096,Math.floor(measurement.totalTokens*policy.retainRatio));
|
|
965
|
+
const cacheRange=pendingContinuation?null:selectCachePreservingRange(session,measurement,cacheRetain,policy,undefined,{continuation:!!rollover});
|
|
966
|
+
const baseRetain = idle ? (policy.idleRetainTokens ?? 0) : 0;
|
|
967
|
+
const range = pendingContinuation ? {start:pendingContinuation.seqs[0],end:pendingContinuation.seqs.at(-1)} : cacheRange ?? (rollover ? selectContinuationRange(session) : selectCompactableRange(session, measurement, baseRetain));
|
|
968
|
+
if (!range) return null;
|
|
969
|
+
if (idle && !pendingContinuation) {
|
|
970
|
+
// Check usefulness before resolving the summary model's capacity. Tiny
|
|
971
|
+
// history should not become a failed continuation just because its
|
|
972
|
+
// instruction envelope exceeds a small summarizer's request budget.
|
|
973
|
+
const size = measureSummaryRange(session, range, {policy,language:policy.language});
|
|
974
|
+
if (rollover || size.minimumCheckpointBytes >= size.sourceBytes) {
|
|
975
|
+
if (!policy.continuationEnabled || !policy.archiveEnabled || !this.historyToolsAvailable) return null;
|
|
976
|
+
const next = rollover ? range : selectContinuationRange(session);
|
|
977
|
+
if (!next || !this._idleContinuationCanShrink(session, next, policy)) return null;
|
|
978
|
+
return await this._compactRegionCore(next.start, next.end, agent, signal, {manual,sourceCommandId,policy,continuation:true,idle:Boolean(idle)});
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (rollover) return await this._compactRegionCore(range.start, range.end, agent, signal, {manual,sourceCommandId,policy,continuation:true,idle:Boolean(idle)});
|
|
982
|
+
const originalNodes = session.surface.nodes;
|
|
983
|
+
let pending = originalNodes.slice(originalNodes.indexOf(range.start), originalNodes.indexOf(range.end)+1);
|
|
984
|
+
const results = [];
|
|
985
|
+
const capacityContext = {};
|
|
986
|
+
try {
|
|
987
|
+
for (let chunk = 0; chunk < this.config.maxSummaryChunks; chunk++) {
|
|
988
|
+
signal?.throwIfAborted?.();
|
|
989
|
+
const live = new Set(session.surface.nodes); pending = pending.filter(seq => live.has(seq));
|
|
990
|
+
if (!pending.length) return combineCompactionResults(results);
|
|
991
|
+
if (results.length) {
|
|
992
|
+
const remainder = measureSummaryRange(session, { start: pending[0], end: pending.at(-1) }, { language: policy.language });
|
|
993
|
+
if (remainder.minimumCheckpointBytes >= remainder.sourceBytes) {
|
|
994
|
+
this.log('info', 'Remaining messages cannot shrink with the selected memory; keeping them on the surface.');
|
|
995
|
+
return { ...combineCompactionResults(results), retainedOriginalSeqs: pending };
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
const result = await this._compactRegionCore(pending[0], pending.at(-1), agent, signal, {
|
|
999
|
+
manual, maxTokens: policy.maxTokens, policy, capacityContext, skipUnshrinkable:idle, idle:Boolean(idle),
|
|
1000
|
+
...(sourceCommandId === undefined ? {} : { sourceCommandId }),
|
|
1001
|
+
});
|
|
1002
|
+
if (!result) return combineCompactionResults(results);
|
|
1003
|
+
results.push(result);
|
|
1004
|
+
}
|
|
1005
|
+
if (pending.some(seq => session.surface.nodes.includes(seq))) throw Object.assign(new Error(`manual compaction reached maxSummaryChunks=${this.config.maxSummaryChunks}; completed chunks remain committed`), { code: 'SUMMARY_BATCH_LIMIT' });
|
|
1006
|
+
return combineCompactionResults(results);
|
|
1007
|
+
} catch (error) {
|
|
1008
|
+
if (policy.continuationEnabled && policy.archiveEnabled && this.historyToolsAvailable && continuationEligible(error) && !signal?.aborted) {
|
|
1009
|
+
const fresh = await this._measure(session);
|
|
1010
|
+
const rest = selectContinuationRange(session);
|
|
1011
|
+
if (idle && rest && !this._idleContinuationCanShrink(session, rest, policy)) return combineCompactionResults(results);
|
|
1012
|
+
if (rest) return await this._compactRegionCore(rest.start, rest.end, agent, signal, {manual,sourceCommandId,policy,continuation:true});
|
|
1013
|
+
}
|
|
1014
|
+
if (idle && error.code === 'SUMMARY_NOT_SMALLER' && error.localCapacityRejection) return combineCompactionResults(results);
|
|
1015
|
+
if (results.length) {
|
|
1016
|
+
const partial = new Error(`${results.length} summary chunk(s) committed; remaining history retained: ${error.message}`, { cause: error });
|
|
1017
|
+
partial.code = 'commit'; partial.compactionStage = 'commit'; partial.compactionIds = results.map(r => r.compactionId); throw partial;
|
|
1018
|
+
}
|
|
1019
|
+
throw error;
|
|
1020
|
+
}
|
|
1021
|
+
} finally {
|
|
1022
|
+
this._busy.delete(session);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* 压一个明确的 surface 区间。
|
|
1028
|
+
*
|
|
1029
|
+
* 这是 `CompactionEngine` 的抽象方法,语义上只服务于**自动**压缩
|
|
1030
|
+
* (官方在 `compactIfNeeded` 里以 `owner: 'current-turn'` 调用它)。
|
|
1031
|
+
* 手动 `/compact` 走 `_compactManual` → `_compactRegionCore(..., manual: true)`,
|
|
1032
|
+
* 与官方 `compactSurfaceRegion({owner: null})` 的分工一致。
|
|
1033
|
+
*
|
|
1034
|
+
* @param {number} start
|
|
1035
|
+
* @param {number} end
|
|
1036
|
+
* @param {object} agent
|
|
1037
|
+
* @param {AbortSignal} [signal]
|
|
1038
|
+
* @param {string} [sourceCommandId]
|
|
1039
|
+
*/
|
|
1040
|
+
async compactRegion(start,end,agent,signal,sourceCommandId) {
|
|
1041
|
+
return this._withConfig(() => this.diagnostics.run(agent?.session,{trigger:'region',commandId:sourceCommandId,signal,reportContext:this._diagnosticContext(agent)},()=>this._compactRegionInner(start,end,agent,signal,sourceCommandId)));
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
async _compactRegionInner(start, end, agent, signal, sourceCommandId) {
|
|
1045
|
+
// ⚠️ 这条公开入口也要走 `modelPolicies`。它不只是给外部调用的:
|
|
1046
|
+
// 本插件的 `_reactiveRetry`(溢出恢复)就是走这条路进来的。只给
|
|
1047
|
+
// `_compactManual` / `compactIfNeeded` 传 policy 时,同一个模型在
|
|
1048
|
+
// "自动压力压缩"下用目标策略、在"溢出恢复"下退回全局配置 ——
|
|
1049
|
+
// 用户看到的是同一个模型时好时坏。
|
|
1050
|
+
//
|
|
1051
|
+
// ⚠️ 策略解析与预算解析必须分开做(见 `_resolvePolicy`)。区间压缩本身
|
|
1052
|
+
// 不依赖自动压力预算:窗口太小、headroom 太大、保留量组合越界都会让
|
|
1053
|
+
// `_resolveSpec` 抛错,但"摘要发给哪个模型、输出上限多少"依然是有确定答案的。
|
|
1054
|
+
// 早先这里把整个 `_resolveSpec` 的异常吞成 undefined,于是预算一失败就
|
|
1055
|
+
// 连带丢掉目标策略,摘要静默改道到全局模型。
|
|
1056
|
+
const session = agent?.session;
|
|
1057
|
+
this._restoreState(session);
|
|
1058
|
+
if (this._stopped.has(this._keyOf(session))) return null;
|
|
1059
|
+
const policy = this._resolvePolicy(agent);
|
|
1060
|
+
const spec = await this._resolveSpec(agent, undefined, signal).catch(error => {
|
|
1061
|
+
if (error?.code === 'STATE_UNAVAILABLE' || isAbortError(error, signal)) throw error;
|
|
1062
|
+
return undefined; // Explicit region selection does not require a pressure budget.
|
|
1063
|
+
});
|
|
1064
|
+
return this._compactRegionCore(start, end, agent, signal, {
|
|
1065
|
+
manual: false,
|
|
1066
|
+
...(spec?.maxTokens === undefined ? {} : { maxTokens: spec.maxTokens }),
|
|
1067
|
+
policy,
|
|
1068
|
+
...(sourceCommandId === undefined ? {} : { sourceCommandId }),
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* @param {number} start
|
|
1074
|
+
* @param {number} end
|
|
1075
|
+
* @param {object} agent
|
|
1076
|
+
* @param {AbortSignal|undefined} signal
|
|
1077
|
+
* @param {{manual: boolean, sourceCommandId?: string}} options
|
|
1078
|
+
*/
|
|
1079
|
+
async _compactRegionCore(start, end, agent, signal, options) {
|
|
1080
|
+
const { manual } = options;
|
|
1081
|
+
const sourceCommandId = options.sourceCommandId;
|
|
1082
|
+
const session = agent?.session;
|
|
1083
|
+
if (!session) {
|
|
1084
|
+
throw new Error('compactRegion: no session');
|
|
1085
|
+
}
|
|
1086
|
+
signal?.throwIfAborted?.();
|
|
1087
|
+
const transactionKey = `${this.sessionState.root}:${JSON.stringify(session.id)}`;
|
|
1088
|
+
if (activeTransactions.has(transactionKey)) { const e = new Error('compaction already active for this logical session'); e.code = 'busy'; throw e; }
|
|
1089
|
+
activeTransactions.add(transactionKey);
|
|
1090
|
+
try {
|
|
1091
|
+
await this.recoverPending(session, true);
|
|
1092
|
+
signal?.throwIfAborted?.();
|
|
1093
|
+
const pendingContinuation = readPendingContinuation(this.sessionState, session);
|
|
1094
|
+
if (pendingContinuation) {
|
|
1095
|
+
assertPendingSource(session, pendingContinuation);
|
|
1096
|
+
if (!options.continuation) throw Object.assign(new Error('Finish the saved continuation before starting another compaction.'), {code:'CONTINUATION_PENDING_RANGE'});
|
|
1097
|
+
}
|
|
1098
|
+
validateSurfaceRegion(session, start, end);
|
|
1099
|
+
const policy = options.policy ?? this.config;
|
|
1100
|
+
const strictHost = Boolean(policy?.strictHostFingerprints) || Boolean(this.requireTransactionAdapter);
|
|
1101
|
+
let useAtomicCommit = false;
|
|
1102
|
+
if (typeof session.restoreCompactionSurface !== 'function') {
|
|
1103
|
+
if (!this.transactionAdapter) {
|
|
1104
|
+
if (strictHost) {
|
|
1105
|
+
const e = new Error('Stock-host transaction adapter is unavailable; compression is refused before changing history.');
|
|
1106
|
+
e.code = 'RECOVERY_REQUIRED';
|
|
1107
|
+
throw e;
|
|
1108
|
+
}
|
|
1109
|
+
await this._assertNoUnrecoveredAtomicJournal(session);
|
|
1110
|
+
this._warnHostFallbackOnce('stock transaction adapter unavailable');
|
|
1111
|
+
} else {
|
|
1112
|
+
const probe = this.transactionAdapter.probeSupport(session);
|
|
1113
|
+
if (probe.ok) {
|
|
1114
|
+
useAtomicCommit = true;
|
|
1115
|
+
} else if (strictHost) {
|
|
1116
|
+
throw probe.error;
|
|
1117
|
+
} else {
|
|
1118
|
+
await this._assertNoUnrecoveredAtomicJournal(session);
|
|
1119
|
+
this._warnHostFallbackOnce(probe.reason);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
|
|
1125
|
+
const entryState = inspectCompactionEntryState(session);
|
|
1126
|
+
assertCompactionInactive(entryState.unmatchedCompactionStart, entryState.latestEndSeedSeq, 'compaction');
|
|
1127
|
+
|
|
1128
|
+
if (manual && entryState.openTurn !== null) {
|
|
1129
|
+
const error = new Error('manual compaction: the session already has an open turn');
|
|
1130
|
+
error.code = 'busy';
|
|
1131
|
+
throw error;
|
|
1132
|
+
}
|
|
1133
|
+
if (!manual && entryState.openTurn === null) {
|
|
1134
|
+
// 自动压缩必须包在一个 turn 里,否则写进 surface 的事件没有归属。
|
|
1135
|
+
// 抛错会被 agent/pre-step 的处理器接住并降级为"这一轮不压",不会打断对话。
|
|
1136
|
+
throw new Error('compactRegion: no open turn — automatic compaction events must be enclosed in a turn');
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// Prove policy state can be durably written before replacing any context.
|
|
1140
|
+
// A later disk failure still needs the retained journal; this is not rollback.
|
|
1141
|
+
try { this.sessionState.update(session.id, state => state); }
|
|
1142
|
+
catch (cause) {
|
|
1143
|
+
const error = new Error(`compaction state unavailable before replacement: ${cause?.message ?? cause}`);
|
|
1144
|
+
error.code = 'STATE_UNAVAILABLE'; error.cause = cause; throw error;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
if (options.continuation && (!policy.archiveEnabled || !this.historyToolsAvailable)) throw Object.assign(new Error('同会话接续需要可用的完整历史归档和三个历史查询工具;原上下文保留。'), {code:'CONTINUATION_HISTORY_UNAVAILABLE'});
|
|
1148
|
+
const targets = resolveSummaryTargets({ config: policy, agent });
|
|
1149
|
+
const capacity = options.capacityContext?.current ?? await resolveSummaryCapacity(this.ctx, targets, { ...policy, maxTokens: options.maxTokens ?? policy.maxTokens }, signal);
|
|
1150
|
+
if (options.capacityContext) options.capacityContext.current = capacity;
|
|
1151
|
+
const planning = { policy, language: policy.language, historyToolsAvailable: Boolean(this.historyToolsAvailable), pricedInput: seqs => this._shadowedTokens(session, seqs) };
|
|
1152
|
+
let planned;
|
|
1153
|
+
try { planned = options.continuation ? {start,end,limited:false} : planSummaryRange(session, { start, end }, capacity, planning); }
|
|
1154
|
+
catch (error) {
|
|
1155
|
+
// Let the idle caller try continuation or skip a locally non-shrinking
|
|
1156
|
+
// prefix without counting a failed request. Model/audit failures still block.
|
|
1157
|
+
if (options.skipUnshrinkable && error.code === 'SUMMARY_NOT_SMALLER' && error.localCapacityRejection) throw error;
|
|
1158
|
+
if (!isAbortError(error, signal)) this._recordFailure(agent, session, error);
|
|
1159
|
+
throw error;
|
|
1160
|
+
}
|
|
1161
|
+
let preservePrefixCache = false;
|
|
1162
|
+
if (!options.continuation && !options.idle) {
|
|
1163
|
+
const durable = latestDurableTarget(agent);
|
|
1164
|
+
const primary = targets.targets[0];
|
|
1165
|
+
if (durable && primary && primary.provider === durable.provider && primary.model === durable.model) {
|
|
1166
|
+
const verbatim = measureSummaryRange(session, { start: planned.start, end: planned.end }, { ...planning, preservePrefixCache: true });
|
|
1167
|
+
if (verbatim.inputUnits <= capacityLimit(capacity)) preservePrefixCache = true;
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
if (planned.limited) this.log('info', `summary capacity split: ${start}-${end} -> ${planned.start}-${planned.end}, ${planned.inputUnits}/${planned.inputLimit} conservative input units`);
|
|
1171
|
+
let activeVerbatim = preservePrefixCache;
|
|
1172
|
+
const reselect = async (_extra, previous) => {
|
|
1173
|
+
const prior = measureSummaryRange(session, previous, { ...planning, preservePrefixCache: activeVerbatim });
|
|
1174
|
+
const wasVerbatim = activeVerbatim;
|
|
1175
|
+
activeVerbatim = false;
|
|
1176
|
+
capacity.shrinkLimit = Math.min(capacity.shrinkLimit, Math.floor((prior.inputUnits ?? planned.inputUnits) / 2));
|
|
1177
|
+
let next;
|
|
1178
|
+
try { next = planSummaryRange(session, previous, capacity, planning); }
|
|
1179
|
+
catch (error) { if (['SUMMARY_GROUP_TOO_LARGE','SUMMARY_NOT_SMALLER'].includes(error.code)) return null; throw error; }
|
|
1180
|
+
return (next.end !== previous.end || (wasVerbatim && next.inputUnits < prior.inputUnits)) ? next : null;
|
|
1181
|
+
};
|
|
1182
|
+
if (options.continuation) {
|
|
1183
|
+
const nodes=session.surface.nodes;
|
|
1184
|
+
const pending=freezeContinuation(this.sessionState,session,nodes.slice(nodes.indexOf(planned.start),nodes.indexOf(planned.end)+1),options.continuationBudgetTokens);
|
|
1185
|
+
options={...options,continuationPending:pending,continuationBudgetTokens:pending.budgetTokens};
|
|
1186
|
+
}
|
|
1187
|
+
const replaceGenerationBefore = session.surface?.replaceGeneration ?? 0;
|
|
1188
|
+
try {
|
|
1189
|
+
return await this._compactSpan(planned.start, planned.end, agent, signal, {
|
|
1190
|
+
...options, preservePrefixCache, capacity, strictHost, useAtomicCommit, reselect: options.continuation ? undefined : reselect,
|
|
1191
|
+
turn: manual ? null : (entryState.openTurn ?? null),
|
|
1192
|
+
}, 0);
|
|
1193
|
+
} catch (error) {
|
|
1194
|
+
if (options.continuationPending) releaseUnstartedContinuation(this.sessionState, session, options.continuationPending.id);
|
|
1195
|
+
if (typeof session.restoreCompactionSurface === 'function'
|
|
1196
|
+
|| (session.surface?.replaceGeneration ?? 0) > replaceGenerationBefore) {
|
|
1197
|
+
// Stock hosts must also inspect the journal: closing a lifecycle is not
|
|
1198
|
+
// restoration. A changed unresolved surface blocks further model calls.
|
|
1199
|
+
try {
|
|
1200
|
+
const recovery = await this.recoverPending(session, true, isAbortError(error, signal) ? 'cancelled' : 'failed');
|
|
1201
|
+
if (recovery.restored > 0 && error && typeof error === 'object') error.surfaceRestored = true;
|
|
1202
|
+
} catch (recoveryError) {
|
|
1203
|
+
this._recoveryBlocked.add(this._keyOf(session));
|
|
1204
|
+
const blocked = new AggregateError([error, recoveryError], 'compaction failed and restoration must finish before another model request');
|
|
1205
|
+
blocked.code = 'RECOVERY_REQUIRED'; blocked.compactionStage = 'commit'; blocked.cause = error; throw blocked;
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
throw error;
|
|
1209
|
+
}
|
|
1210
|
+
} finally { activeTransactions.delete(transactionKey); }
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* 压一个已经校验过的 span,失败时按 `reactiveStep` 做**渐进收敛**重试。
|
|
1215
|
+
*
|
|
1216
|
+
* 这是 L6 真正的落点。官方把恢复做成 `compaction/summary-error` waterfall,
|
|
1217
|
+
* 但**派发点是 provider 自己的 `regionDependencies().recover`**,不是 agent-loop
|
|
1218
|
+
* 自动提供的 —— 我们换掉引擎之后没有人派发,所以"渐进重试"必须自己接在
|
|
1219
|
+
* catch 路径上,否则永远不可达。
|
|
1220
|
+
*
|
|
1221
|
+
* 事务边界:每次尝试都以 `compaction/start` 开锁、以 `compaction/end` 关锁。
|
|
1222
|
+
* 重试会**重新开一次事务**,而不是带着未结束的锁继续写 —— 这样即使进程崩溃,
|
|
1223
|
+
* 留下的也只是可被 `assertCompactionInactive` 识别的孤儿锁。
|
|
1224
|
+
*
|
|
1225
|
+
* @param {number} start
|
|
1226
|
+
* @param {number} end
|
|
1227
|
+
* @param {object} agent
|
|
1228
|
+
* @param {AbortSignal|undefined} signal
|
|
1229
|
+
* @param {{manual: boolean, sourceCommandId?: string, turn: number|null, reselect?: (extra: number) => {start:number,end:number}|null}} options
|
|
1230
|
+
* @param {number} attempt
|
|
1231
|
+
*/
|
|
1232
|
+
async _compactSpan(start, end, agent, signal, options, attempt) {
|
|
1233
|
+
const { manual } = options;
|
|
1234
|
+
const sourceCommandId = options.sourceCommandId;
|
|
1235
|
+
const session = agent.session;
|
|
1236
|
+
const compactionId = nextCompactionId();
|
|
1237
|
+
const nodes = session.surface?.nodes ?? [];
|
|
1238
|
+
const startIdx = nodes.indexOf(start);
|
|
1239
|
+
const endIdx = nodes.indexOf(end);
|
|
1240
|
+
const shadowedSeqs = nodes.slice(startIdx, endIdx + 1);
|
|
1241
|
+
const shadowedSpanSnapshot = [...shadowedSeqs];
|
|
1242
|
+
|
|
1243
|
+
// 被覆盖范围的 token 数**在这里冻结一次**。提交后重新 `_measure` 只会看到
|
|
1244
|
+
// 新的 surface(旧 seq 已经不在了),再用旧 seq 去查价格 Map 会全部拿到 0,
|
|
1245
|
+
// 于是 `/compact` 报"压缩了 0 个 token"—— 用户看到的就是"压了但没省"。
|
|
1246
|
+
const shadowedTokenCount = this._shadowedTokens(session, shadowedSeqs);
|
|
1247
|
+
const shadowedCanonicalBytes = canonicalMessageBytes(shadowedSeqs.map(seq => session.deriveEventMessage(session.eventAt(seq))).filter(Boolean));
|
|
1248
|
+
// 这个数是实测还是粗估,必须一起交出去(见 `_lastPrecision` 的注释)。
|
|
1249
|
+
// 缺省 `unknown` 而不是 `priced`:不知道就别假装知道。
|
|
1250
|
+
const shadowedTokenPrecision = this._lastPrecision?.get(session) ?? 'unknown';
|
|
1251
|
+
|
|
1252
|
+
const lifecycle = {
|
|
1253
|
+
compactionId,
|
|
1254
|
+
...(sourceCommandId === undefined ? {} : { sourceCommandId }),
|
|
1255
|
+
turn: options.turn ?? null,
|
|
1256
|
+
};
|
|
1257
|
+
const startEvent = await this._append(session, 'compaction/start', lifecycle);
|
|
1258
|
+
|
|
1259
|
+
// ⚠️ 开锁之后立刻落一笔持久记录。
|
|
1260
|
+
// 这一步比 `compaction/start` 事件更强:事件留在会话日志里,但"这次压缩
|
|
1261
|
+
// 到底走到哪一步"要跨进程读得出来,才能在下一次启动时判断有没有留下
|
|
1262
|
+
// 一把没关上的锁。日志写失败立即阻断提交,进入生命周期收尾。
|
|
1263
|
+
// Journal write is inside the lifecycle cleanup boundary below.
|
|
1264
|
+
|
|
1265
|
+
let stage = 'summary';
|
|
1266
|
+
// 事务收尾状态机。见下面「统一收尾」一段:`closing` = 已经开始收尾,
|
|
1267
|
+
// `closed` = `compaction/end` 确实写成功了。
|
|
1268
|
+
let closing = false;
|
|
1269
|
+
let closed = false;
|
|
1270
|
+
let replaced = false;
|
|
1271
|
+
let failure;
|
|
1272
|
+
let flushFailure;
|
|
1273
|
+
let committed = null;
|
|
1274
|
+
let stockCommitted = false;
|
|
1275
|
+
try {
|
|
1276
|
+
await this._journalRecord(session, compactionId, 'started', start, end);
|
|
1277
|
+
const policy = options.policy ?? this.config;
|
|
1278
|
+
let summarized, preserved, checkpointBlocks, memorySelection, progressSelection, validatedReuse = false;
|
|
1279
|
+
if (options.continuation) {
|
|
1280
|
+
({summarized,preserved,checkpointBlocks} = await prepareContinuation({ctx:this.ctx,agent,session,seqs:shadowedSeqs,policy,capacity:options.capacity,chain:resolveSummaryTargets({config:policy,agent}),compactionId,signal,archiveRoot:this.archive.root,checkpointBudgetTokens:options.continuationBudgetTokens,estimateCheckpointTokens:blocks=>this._estimateCheckpointTokens(blocks),manualRetry:manual,requestId:options.sourceCommandId??compactionId,onProgressIdentity:key=>bindContinuationProgress(this.sessionState,session,options.continuationPending.id,key,{manualRetry:manual,requestId:options.sourceCommandId??compactionId})}));
|
|
1281
|
+
Object.assign(summarized.continuation,{pendingId:options.continuationPending.id,pendingSourceSha256:options.continuationPending.sourceSha256});
|
|
1282
|
+
const tokens = this._estimateCheckpointTokens(checkpointBlocks);
|
|
1283
|
+
if (tokens >= shadowedTokenCount || canonicalMessageBytes([{role:'user',content:checkpointBlocks}]) >= shadowedCanonicalBytes) throw Object.assign(new Error('Continuation would not reduce the current context; original history retained.'), {code:'CONTINUATION_NOT_SMALLER'});
|
|
1284
|
+
if (options.continuationBudgetTokens && tokens >= options.continuationBudgetTokens) throw Object.assign(new Error('Continuation requirements exceed the available main-request budget.'), {code:'CONTINUATION_TOO_LARGE'});
|
|
1285
|
+
} else {
|
|
1286
|
+
const preSummarySurfaceNodes = [...(session.surface?.nodes ?? [])];
|
|
1287
|
+
const preSummaryReplaceGen = session.surface?.replaceGeneration ?? 0;
|
|
1288
|
+
const input = buildSummarizationInput(session, shadowedSeqs, { preservePrefixCache: Boolean(options.preservePrefixCache && attempt === 0) });
|
|
1289
|
+
progressSelection=input.progress;
|
|
1290
|
+
const previousSummary = shadowedSeqs.some(seq => session.deriveEventMessage(session.eventAt(seq))?.source?.kind === 'compact-checkpoint') ? 'inline checkpoint' : null;
|
|
1291
|
+
// ⚠️ 用**按 provider/model 合并过的**策略解析摘要目标,不能用全局配置。
|
|
1292
|
+
// 否则 `modelPolicies[i].summarizationProvider/Model` 永远进不了摘要链:
|
|
1293
|
+
// 预算、输出上限按目标模型算,摘要却发给全局默认模型,两套口径对不上。
|
|
1294
|
+
const chain = resolveSummaryTargets({ config: policy, agent });
|
|
1295
|
+
|
|
1296
|
+
memorySelection = selectSessionMemory(session, shadowedSeqs, {
|
|
1297
|
+
maxMessages: policy.preserveRecentMessages, maxTokens: policy.preserveRecentUserTokens, approxTokens,
|
|
1298
|
+
});
|
|
1299
|
+
const expectedUserMessages = [...memorySelection.active,...memorySelection.retained];
|
|
1300
|
+
const referenceMessages=collectReferenceMessages(session,session.surface.nodes);
|
|
1301
|
+
preserved = memorySelection.preserved;
|
|
1302
|
+
if (memorySelection.decisions.some(d=>d.reason==='oldest-quote-evicted') && !policy.archiveEnabled) {
|
|
1303
|
+
throw Object.assign(new Error('Memory selection requires a verified source archive.'), {code:'MEMORY_ARCHIVE_REQUIRED'});
|
|
1304
|
+
}
|
|
1305
|
+
input.messages.push({role:'user',content:[{type:'text',text:memoryPolicyInstruction(memorySelection)}]});
|
|
1306
|
+
|
|
1307
|
+
const buildSpanCheckpoint = summaryBlocks => {
|
|
1308
|
+
const archiveBase = policy.archiveEnabled && this.historyToolsAvailable
|
|
1309
|
+
? { compactionId, startSeq: shadowedSeqs[0], endSeq: shadowedSeqs.at(-1), files: collectArchivedFiles(session, shadowedSeqs) }
|
|
1310
|
+
: null;
|
|
1311
|
+
const build = archive => buildCheckpointBlocks(summaryBlocks, preserved, CHECKPOINT_PREAMBLE, {
|
|
1312
|
+
originalsInSummary: true,
|
|
1313
|
+
workReport: input.workReport,
|
|
1314
|
+
progress: input.progress,
|
|
1315
|
+
...(archive ? { archive } : {}),
|
|
1316
|
+
});
|
|
1317
|
+
if (!archiveBase) return build(null);
|
|
1318
|
+
const withFiles = build(archiveBase);
|
|
1319
|
+
const fits = blocks => this._estimateCheckpointTokens(blocks) < shadowedTokenCount
|
|
1320
|
+
&& canonicalMessageBytes([{ role: 'user', content: blocks }]) < shadowedCanonicalBytes;
|
|
1321
|
+
if (fits(withFiles)) return withFiles;
|
|
1322
|
+
if (archiveBase.files.length > 0) {
|
|
1323
|
+
const withoutFiles = build({ ...archiveBase, files: [] });
|
|
1324
|
+
if (fits(withoutFiles)) return withoutFiles;
|
|
1325
|
+
}
|
|
1326
|
+
return build(null);
|
|
1327
|
+
};
|
|
1328
|
+
|
|
1329
|
+
summarized = await summarizeWithChain({
|
|
1330
|
+
ctx: this.ctx,
|
|
1331
|
+
agent,
|
|
1332
|
+
input,
|
|
1333
|
+
chain,
|
|
1334
|
+
options: {
|
|
1335
|
+
// ⚠️ 必须用 spec 上的值,不能用 `this.config.maxTokens`:
|
|
1336
|
+
// 前者已经按 provider/model 合并过 `modelPolicies`,后者只是全局默认。
|
|
1337
|
+
maxTokens: options.maxTokens ?? policy.maxTokens,
|
|
1338
|
+
singleTarget: true,
|
|
1339
|
+
timeoutMs: policy.summaryTimeoutMs,
|
|
1340
|
+
retryCount: policy.summaryRetryCount,
|
|
1341
|
+
retryDelayMs: policy.summaryRetryDelayMs,
|
|
1342
|
+
language: policy.language,
|
|
1343
|
+
previousSummary,
|
|
1344
|
+
capacity: options.capacity,
|
|
1345
|
+
// 把来源区间的用户消息清单交给模型;最终第六节由引擎逐字生成。
|
|
1346
|
+
// 这张清单和下面校验用的是**同一个** `expectedUserMessages` —— 列给模型看
|
|
1347
|
+
// 的和拿来验收的必须是同一份,否则就是让模型去猜我们的标准。
|
|
1348
|
+
userMessages: preserved,
|
|
1349
|
+
// ⚠️ 内容校验必须放在**目标链内部**跑,不能等链跑完再查。
|
|
1350
|
+
// 三个摘要是按能力排的:主模型只回 `Done.` 时,下一个目标本来还有机会,
|
|
1351
|
+
// 但校验在链外时第一个目标就被判死,整次压缩直接失败 —— 降级链白写了。
|
|
1352
|
+
// 每个目标先生成完整 checkpoint,再校验结构、原话覆盖、体积和约束。
|
|
1353
|
+
// 不合格时继续下一个目标,避免降级链被链外检查提前截断。
|
|
1354
|
+
reconstruct: ({result, verdict, modelRawText}) => {
|
|
1355
|
+
if(verdict.code==='PROGRESS_FACT_CONFLICT')return null;
|
|
1356
|
+
const rebuilt = reconstructDisputedLines({text: modelRawText, userMessages: expectedUserMessages, audit: verdict.audit, language: result.language});
|
|
1357
|
+
if (!rebuilt) return null;
|
|
1358
|
+
const receipts = [...(result.reconstruction ?? []), ...rebuilt.receipts];
|
|
1359
|
+
if (new Set(receipts.flatMap(r=>r.removedLineSha256)).size > 16) return null;
|
|
1360
|
+
return { ...result, rawText: rebuilt.text, summary: [{type:'text',text:rebuilt.text}], reconstruction: receipts };
|
|
1361
|
+
},
|
|
1362
|
+
onRejected: ({ result, verdict, modelRawText, attempt }) => dumpAuditRejection({
|
|
1363
|
+
storageRoot: dirname(this.archive.root), sessionId: session?.id,
|
|
1364
|
+
compactionId, target: `${result?.provider ?? '?'}/${result?.model ?? '?'}`,
|
|
1365
|
+
code: verdict.code, message: verdict.message, attempt,
|
|
1366
|
+
modelRawText, auditedText: result.rawText,
|
|
1367
|
+
report: verdict.audit, userMessages: expectedUserMessages,
|
|
1368
|
+
checkpointBlocks: verdict.audit ? buildSpanCheckpoint(result.summary) : undefined,
|
|
1369
|
+
}),
|
|
1370
|
+
validate: (result) => {
|
|
1371
|
+
const language = result?.language ?? (policy.language === 'zh' ? 'zh' : 'en');
|
|
1372
|
+
const structure = validateSummaryStructure(result?.rawText ?? '', { language });
|
|
1373
|
+
if (!structure.ok) return structure;
|
|
1374
|
+
// 第六节由选定的近期原话确定性生成,模型只需占位。
|
|
1375
|
+
result.modelRawOutput ??= result.summary;
|
|
1376
|
+
const retired = retiredMemoryPresent(result.rawText, memorySelection);
|
|
1377
|
+
if (retired) return {ok:false,code:'RETIRED_MEMORY_REINTRODUCED',message:`Remove the ${retired.reason} request from seq ${retired.seq}; it is no longer active.`};
|
|
1378
|
+
result.rawText = materializeUserMessageSection(deduplicateNotes(result.rawText, preserved), preserved, language);
|
|
1379
|
+
result.summary = [{ type: 'text', text: result.rawText }];
|
|
1380
|
+
const compiled = validateSummaryStructure(result.rawText, { language });
|
|
1381
|
+
const listed = validateUserMessageSection(compiled.userSectionBody, preserved);
|
|
1382
|
+
if (!listed.ok) return listed;
|
|
1383
|
+
const blocks = buildSpanCheckpoint(result.summary);
|
|
1384
|
+
const coverage = validateCheckpointCoverage(blocks, preserved, { expected: preserved });
|
|
1385
|
+
if (!coverage.ok) return coverage;
|
|
1386
|
+
if (this._estimateCheckpointTokens(blocks) >= shadowedTokenCount) return { ok: false, code: 'SUMMARY_NOT_SMALLER', message: 'complete checkpoint is not smaller than its source span' };
|
|
1387
|
+
if (canonicalMessageBytes([{ role: 'user', content: blocks }]) >= shadowedCanonicalBytes) return { ok: false, code: 'SUMMARY_NOT_SMALLER', message: 'complete checkpoint does not reduce canonical UTF-8 message bytes' };
|
|
1388
|
+
const progressVerdict=validateProgressFacts(result.rawText,progressSelection);
|
|
1389
|
+
if(!progressVerdict.ok)return progressVerdict;
|
|
1390
|
+
const provenance=auditInstructionProvenance({userMessages:expectedUserMessages,summaryText:result.rawText,referenceMessages});
|
|
1391
|
+
if(provenance.length)return {ok:false,code:'CONSTRAINT_LOST',message:'Generated instructions or permissions have no matching user source.',audit:{lost:provenance}};
|
|
1392
|
+
if (policy.auditEnabled) {
|
|
1393
|
+
const audit = this._audit(session, shadowedSeqs, result.rawText, compactionId, blocks, policy);
|
|
1394
|
+
if (audit.polarityFlips) return { ok: false, audit, code: 'CONSTRAINT_POLARITY_LOST', message: `summary contradicts a source constraint (seq=${audit.lost.find(item => item.reason === 'polarity')?.seq ?? 'unknown'}, reason=polarity)` };
|
|
1395
|
+
if (policy.auditBlockOnLoss && audit.lost.length) return { ok: false, audit, code: 'CONSTRAINT_LOST', message: `summary failed source constraint audit (seq=${audit.lost[0].seq}, reason=${audit.lost[0].reason})` };
|
|
1396
|
+
}
|
|
1397
|
+
result._validated = { rawText: result.rawText, blocks, surfaceNodes: preSummarySurfaceNodes, replaceGeneration: preSummaryReplaceGen };
|
|
1398
|
+
return listed;
|
|
1399
|
+
},
|
|
1400
|
+
},
|
|
1401
|
+
signal: signal ?? new AbortController().signal,
|
|
1402
|
+
});
|
|
1403
|
+
|
|
1404
|
+
const surfaceNodesNow = session.surface?.nodes ?? [];
|
|
1405
|
+
const validated = summarized?._validated;
|
|
1406
|
+
validatedReuse = Boolean(
|
|
1407
|
+
validated &&
|
|
1408
|
+
validated.rawText === summarized.rawText &&
|
|
1409
|
+
(session.surface?.replaceGeneration ?? 0) === validated.replaceGeneration &&
|
|
1410
|
+
Array.isArray(validated.surfaceNodes) &&
|
|
1411
|
+
validated.surfaceNodes.length === surfaceNodesNow.length &&
|
|
1412
|
+
validated.surfaceNodes.every((seq, idx) => seq === surfaceNodesNow[idx])
|
|
1413
|
+
);
|
|
1414
|
+
if (validatedReuse) {
|
|
1415
|
+
checkpointBlocks = validated.blocks;
|
|
1416
|
+
} else {
|
|
1417
|
+
// L5:把选定的近期原话逐字补回;旧原话留在归档,有效要求仍审计
|
|
1418
|
+
// checkpoint。span 之外的消息本来就在 surface 上原样存在,再抄一遍只是
|
|
1419
|
+
// 把同一段话放两份。
|
|
1420
|
+
//
|
|
1421
|
+
// `preserved` 已经在上面和 `expectedUserMessages` 一起算好了:两份列表
|
|
1422
|
+
// 必须同源、同一次扫描,否则"裁剪后还剩哪些"和"本来有哪些"会各说各话。
|
|
1423
|
+
checkpointBlocks = buildSpanCheckpoint(summarized.summary);
|
|
1424
|
+
|
|
1425
|
+
// ⚠️ 提交前校验:不通过就抛错,走安全失败路径(不替换 surface)。
|
|
1426
|
+
//
|
|
1427
|
+
// 模板里"必须九节""必须列出每一条用户消息"只是**请求**,不是保证。
|
|
1428
|
+
// 模型回一句 `Done.`,模板一个字都不管用 —— 缺了这层校验它会照样被提交,
|
|
1429
|
+
// 而 surface 一旦替换,原文只剩归档,模型再也读不到。
|
|
1430
|
+
//
|
|
1431
|
+
// 结构与第六节的校验已经在目标链内部跑过一遍(为了让降级链生效)。
|
|
1432
|
+
// 这里**再查一次**是有意的:`summarizeWithChain` 可能因为所有目标都不合规
|
|
1433
|
+
// 而抛聚合错误,也可能被将来的改动绕过注入点,而这一段是提交前的最后关口。
|
|
1434
|
+
const structure = validateSummaryStructure(summarized.rawText, {
|
|
1435
|
+
language: summarized.language ?? (policy.language === 'zh' ? 'zh' : 'en'),
|
|
1436
|
+
});
|
|
1437
|
+
if (!structure.ok) {
|
|
1438
|
+
const error = new Error(structure.message);
|
|
1439
|
+
error.code = structure.code;
|
|
1440
|
+
throw error;
|
|
1441
|
+
}
|
|
1442
|
+
// 第六节必须**逐条**交代来源区间里的用户消息。
|
|
1443
|
+
// 只查"这一节非空"不够:写一句"用户希望继续"也是非空,但三条要求里
|
|
1444
|
+
// 有两条根本没被提到,下一个模型读摘要时不会有任何线索知道它们存在。
|
|
1445
|
+
const userSection = validateUserMessageSection(structure.userSectionBody, preserved);
|
|
1446
|
+
if (!userSection.ok) {
|
|
1447
|
+
const error = new Error(userSection.message);
|
|
1448
|
+
error.code = userSection.code;
|
|
1449
|
+
throw error;
|
|
1450
|
+
}
|
|
1451
|
+
// Verify selected `preserved` quotes survive block assembly; `selectSessionMemory`
|
|
1452
|
+
// governs quote eviction across the full span before summarization.
|
|
1453
|
+
const coverage = validateCheckpointCoverage(checkpointBlocks, preserved, {
|
|
1454
|
+
expected: preserved,
|
|
1455
|
+
});
|
|
1456
|
+
if (!coverage.ok) {
|
|
1457
|
+
const error = new Error(coverage.message);
|
|
1458
|
+
error.code = coverage.code;
|
|
1459
|
+
throw error;
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
// ⚠️ 缩小检查必须在**完整 checkpoint 构造之后**做。
|
|
1463
|
+
// 只量"摘要正文"是不够的:L5 保留的用户原话可以远超摘要本身
|
|
1464
|
+
// (摘要 94 tokens、保留 30000 tokens 时,最终 checkpoint 比原文还大),
|
|
1465
|
+
// 于是会出现"压完更大"。
|
|
1466
|
+
const checkpointTokens = this._estimateCheckpointTokens(checkpointBlocks);
|
|
1467
|
+
if (checkpointTokens >= shadowedTokenCount) {
|
|
1468
|
+
const error = new Error(
|
|
1469
|
+
`summary is not smaller than the shadowed content (${checkpointTokens} estimated checkpoint tokens >= ${shadowedTokenCount}, measurement precision: ${shadowedTokenPrecision})`,
|
|
1470
|
+
);
|
|
1471
|
+
error.code = 'SUMMARY_NOT_SMALLER';
|
|
1472
|
+
throw error;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
const checkpointCanonicalBytes = canonicalMessageBytes([{ role: 'user', content: checkpointBlocks }]);
|
|
1476
|
+
if (checkpointCanonicalBytes >= shadowedCanonicalBytes) {
|
|
1477
|
+
const error = new Error(`checkpoint UTF-8 message bytes did not shrink (${checkpointCanonicalBytes} >= ${shadowedCanonicalBytes})`);
|
|
1478
|
+
error.code = 'SUMMARY_NOT_SMALLER'; throw error;
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
// 审计:摘要/检查点是否丢了用户约束。
|
|
1482
|
+
//
|
|
1483
|
+
// ⚠️ 审计结果必须能**阻止提交**。以前它只 `log('warn')`,异常还被
|
|
1484
|
+
// `catch {}` 吞掉 —— 于是"约束被反转"这件事只存在于日志里,用户永远
|
|
1485
|
+
// 不会看到,模型下一轮就按被反转的约束干活。
|
|
1486
|
+
//
|
|
1487
|
+
// 两种发现的处置不同:
|
|
1488
|
+
// - **极性反转**(`Do not delete` → `Delete`):按同源句配对检查,属于词法判断,
|
|
1489
|
+
// 一旦命中就中止这次压缩(原文留在 surface 上,用户可以重试)。
|
|
1490
|
+
// - **词覆盖不足**:同义改写会误报,因此默认**也中止**(`auditBlockOnLoss`),
|
|
1491
|
+
// 但可以显式关掉。理由是代价不对称:误报的代价是"这次没压成、用户可以重试",
|
|
1492
|
+
// 漏报的代价是"约束静默消失、后面整段对话都按错的约束走"。
|
|
1493
|
+
if (policy.auditEnabled) {
|
|
1494
|
+
const report = this._audit(session, shadowedSeqs, summarized.rawText, compactionId, checkpointBlocks, policy);
|
|
1495
|
+
if (report.polarityFlips > 0 || (policy.auditBlockOnLoss && report.lost.filter((item) => item.reason !== 'polarity').length > 0)) {
|
|
1496
|
+
// Diagnostic only: persist the full evidence before throwing.
|
|
1497
|
+
// The await is inside a try that cannot fail the compaction.
|
|
1498
|
+
try {
|
|
1499
|
+
await dumpAuditRejection({
|
|
1500
|
+
storageRoot: dirname(this.archive.root),
|
|
1501
|
+
sessionId: session?.id,
|
|
1502
|
+
compactionId,
|
|
1503
|
+
target: `${summarized?.provider ?? '?'}/${summarized?.model ?? '?'}`,
|
|
1504
|
+
code: report.polarityFlips > 0 ? 'CONSTRAINT_POLARITY_LOST' : 'CONSTRAINT_LOST',
|
|
1505
|
+
message: report.polarityFlips > 0 ? 'summary reverses a user constraint' : 'summary failed source constraint audit',
|
|
1506
|
+
modelRawText: blocksToText(summarized.modelRawOutput ?? summarized.summary ?? []),
|
|
1507
|
+
auditedText: summarized.rawText,
|
|
1508
|
+
checkpointBlocks,
|
|
1509
|
+
report,
|
|
1510
|
+
userMessages: collectInstructionMessages(session, shadowedSeqs, { approxTokens }),
|
|
1511
|
+
});
|
|
1512
|
+
} catch {
|
|
1513
|
+
/* diagnostics must never change the outcome */
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
if (report.polarityFlips > 0) {
|
|
1517
|
+
const error = new Error(
|
|
1518
|
+
`compaction: the summary reverses ${report.polarityFlips} user constraint(s) ` +
|
|
1519
|
+
`(first: "${report.lost.find((item) => item.reason === 'polarity')?.line.slice(0, 120) ?? ''}"); ` +
|
|
1520
|
+
'refusing to replace the original messages with a summary that says the opposite',
|
|
1521
|
+
);
|
|
1522
|
+
error.code = 'CONSTRAINT_POLARITY_LOST';
|
|
1523
|
+
throw error;
|
|
1524
|
+
}
|
|
1525
|
+
const coverageLost = report.lost.filter((item) => item.reason !== 'polarity');
|
|
1526
|
+
if (policy.auditBlockOnLoss && coverageLost.length > 0) {
|
|
1527
|
+
const error = new Error(
|
|
1528
|
+
`compaction: ${coverageLost.length}/${report.checked} user constraint line(s) are missing from the checkpoint ` +
|
|
1529
|
+
`(worst coverage ${report.worstCoverage}; first: "${coverageLost[0].line.slice(0, 120)}"); ` +
|
|
1530
|
+
'refusing to replace the original messages rather than silently dropping a constraint. ' +
|
|
1531
|
+
'Set auditBlockOnLoss: false to accept this tradeoff.',
|
|
1532
|
+
);
|
|
1533
|
+
error.code = 'CONSTRAINT_LOST';
|
|
1534
|
+
throw error;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
// 原文落盘,供 history_* 工具回查。
|
|
1540
|
+
//
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
// ⚠️ 写失败必须**中止压缩**,不能带着 `archivePath: null` 继续提交。
|
|
1544
|
+
// 压缩一旦提交,被折叠的那些消息就从 surface 上消失了 —— 此时 `history_*`
|
|
1545
|
+
// 是唯一还能读到原文的入口。归档悄悄失败却照样替换,等于把"可回查"
|
|
1546
|
+
// 这个保证单方面作废:用户以为历史还在,实际已经读不到了。
|
|
1547
|
+
if (!validatedReuse) {
|
|
1548
|
+
const finalSelection=selectSessionMemory(session,shadowedSeqs,{approxTokens});
|
|
1549
|
+
const finalRetired=retiredMemoryPresent(summarized.rawText,finalSelection);
|
|
1550
|
+
if(finalRetired)throw Object.assign(new Error('Generated notes revive a retired request; original context retained.'),{code:'RETIRED_MEMORY_REINTRODUCED'});
|
|
1551
|
+
const finalProvenance=auditInstructionProvenance({userMessages:[...finalSelection.active,...finalSelection.retained],summaryText:summarized.rawText,referenceMessages:collectReferenceMessages(session,session.surface.nodes)});
|
|
1552
|
+
if(finalProvenance.length)throw Object.assign(new Error('Generated permissions or instructions lack a current user source; original context retained.'),{code:'CONSTRAINT_LOST'});
|
|
1553
|
+
const finalProgress=validateProgressFacts(summarized.rawText,collectProgressFacts(session,shadowedSeqs));
|
|
1554
|
+
if(!finalProgress.ok)throw Object.assign(new Error(finalProgress.message),{code:finalProgress.code});
|
|
1555
|
+
}
|
|
1556
|
+
let archivePath = null;
|
|
1557
|
+
const archiveEntries=policy.archiveEnabled?spanEntries(session,shadowedSeqs):[];
|
|
1558
|
+
if (policy.archiveEnabled) {
|
|
1559
|
+
archivePath = await this.archive.writeSpan({
|
|
1560
|
+
sessionId: session.id,
|
|
1561
|
+
compactionId,
|
|
1562
|
+
entries: archiveEntries,
|
|
1563
|
+
signal,
|
|
1564
|
+
});
|
|
1565
|
+
if (!archivePath) {
|
|
1566
|
+
const error = new Error(
|
|
1567
|
+
'compaction: archiving the shadowed span failed; refusing to replace it on the surface',
|
|
1568
|
+
);
|
|
1569
|
+
error.code = 'ARCHIVE_FAILED';
|
|
1570
|
+
throw error;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
if (options.continuation || policy.archiveEnabled && memorySelection?.decisions.length) {
|
|
1575
|
+
const verified = await this.archive.readSpan(session.id, compactionId);
|
|
1576
|
+
if (!verified.ok || verified.entries.length !== archiveEntries.length || verified.entries.some((entry,i)=>entry.seq!==archiveEntries[i].seq)) throw Object.assign(new Error('Source archive verification failed; original context retained.'), {code:'CONTINUATION_ARCHIVE_INVALID'});
|
|
1577
|
+
}
|
|
1578
|
+
stage = 'commit';
|
|
1579
|
+
|
|
1580
|
+
// ── 提交段(前半:可失败的部分都在替换之前)────────────────────────────
|
|
1581
|
+
// ⚠️ surface 替换是**不可逆**的:宿主的事件日志只追加,`surfaceOp.replace`
|
|
1582
|
+
// 把一段节点收成一个,没有任何"摊开"的操作(`dsh-session/lib/index.js` 里
|
|
1583
|
+
// 找不到 rollback / revert)。所以顺序必须是"能在替换前失败的,全部放在
|
|
1584
|
+
// 替换前":摘要事件写进去、手动路径先等一次持久化边界,都确认成功了,
|
|
1585
|
+
// 才做那个收不回来的替换。
|
|
1586
|
+
//
|
|
1587
|
+
// `user/message` 的事件数据**就是消息本身**(不是 `{message}` 包装),
|
|
1588
|
+
// 而且 `assertMessageEventShape` 要求它带非空 `id`、`role === 'user'`、
|
|
1589
|
+
// `source.kind` 非空、`content` 是数组。少了 `id` 会直接抛
|
|
1590
|
+
// "session event ... lacks an identified message",压缩在最后一步失败。
|
|
1591
|
+
const sourceMemory = memorySelection ? {
|
|
1592
|
+
progressFacts:progressSourceRefs(progressSelection),summaryScope:progressSelection?.scope,
|
|
1593
|
+
workReport:workReportRef(collectWorkReport(session,shadowedSeqs)),
|
|
1594
|
+
memoryRequirements: preservedForSource(memoryRequirements(memorySelection.active)) ?? [],
|
|
1595
|
+
memorySources: memorySourceRefs(memorySelection.active, session),
|
|
1596
|
+
memoryDecisions: memoryDecisionsForSource(memorySelection.decisions,session),
|
|
1597
|
+
} : summarized.sourceMemory ?? {};
|
|
1598
|
+
const checkpointMessage = {
|
|
1599
|
+
id: newMessageId(),
|
|
1600
|
+
role: 'user',
|
|
1601
|
+
content: checkpointBlocks,
|
|
1602
|
+
// 第三参数把原话的结构化副本挂到 source 上,供下一代压缩续传。
|
|
1603
|
+
source: options.continuation ? Object.freeze({...sourceMemory,...compactCheckpointSource(compactionId,sourceCommandId,preservedForSource(preserved)),preserved:preservedForSource(preserved) ?? [],continuation:summarized.continuation}) : Object.freeze({...compactCheckpointSource(compactionId, sourceCommandId, preservedForSource(preserved)),preserved:preservedForSource(preserved) ?? [],...sourceMemory}),
|
|
1604
|
+
};
|
|
1605
|
+
|
|
1606
|
+
// 这里先写 `compaction/summary`:它带 summary / rawOutput / shadowedSeqs,
|
|
1607
|
+
// 就算后面替换失败,这条事件也已经把"这次压缩想做什么"记在日志里了。
|
|
1608
|
+
const summaryEvent = this._appendSync(session, 'compaction/summary', {
|
|
1609
|
+
compactionId,
|
|
1610
|
+
...(sourceCommandId === undefined ? {} : { sourceCommandId }),
|
|
1611
|
+
summary: summarized.summary,
|
|
1612
|
+
rawOutput: summarized.modelRawOutput ?? summarized.summary,
|
|
1613
|
+
...(summarized.reconstruction ? { reconstruction: summarized.reconstruction } : {}),
|
|
1614
|
+
...(summarized.continuation ? {continuation:summarized.continuation} : {}),
|
|
1615
|
+
llmStreamCall: true,
|
|
1616
|
+
shadowedRange: { start, end },
|
|
1617
|
+
shadowedSeqs,
|
|
1618
|
+
shadowedTokenCount,
|
|
1619
|
+
provider: summarized.provider,
|
|
1620
|
+
model: summarized.model,
|
|
1621
|
+
maxTokens: summarized.maxTokens,
|
|
1622
|
+
...(summarized.usage === null || summarized.usage === undefined ? {} : { usage: summarized.usage }),
|
|
1623
|
+
});
|
|
1624
|
+
|
|
1625
|
+
// 替换前的持久化检查点。磁盘满这类故障在这里就会被拦下,
|
|
1626
|
+
// 而此时 surface 一个字节都没动 —— 用户看到的是"这次没压成",
|
|
1627
|
+
// 而不是"压成了但历史读不到了"。
|
|
1628
|
+
if (typeof this.ctx.sessions?.flush === 'function') {
|
|
1629
|
+
try {
|
|
1630
|
+
await this._flushSession(session);
|
|
1631
|
+
} catch (error) {
|
|
1632
|
+
const error2 = new Error(
|
|
1633
|
+
`compaction persistence failed before the surface was replaced: ${error?.message ?? error}`,
|
|
1634
|
+
);
|
|
1635
|
+
error2.code = 'commit';
|
|
1636
|
+
error2.compactionStage = 'commit';
|
|
1637
|
+
error2.cause = error;
|
|
1638
|
+
throw error2;
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
// ⚠️ 替换之前落一笔**持久**的事务记录。
|
|
1643
|
+
// 宿主没有回滚(`applySurfacePlan` 对 replace 做 splice,收进去摊不回来),
|
|
1644
|
+
// 所以"替换之后失败"唯一能补救的,是让下一次启动知道这次事务走到哪了:
|
|
1645
|
+
// 记下 compactionId,下次就能判断"这个会话是不是留了一把没关上的锁"。
|
|
1646
|
+
// 这一步必须在替换**之前** —— 替换和 end 之间不允许有 await。
|
|
1647
|
+
await this._journalRecord(session, compactionId, 'committing', start, end);
|
|
1648
|
+
|
|
1649
|
+
// 替换之前最后确认一次跨度还在(预检查点是一次 await,surface 可能变了)。
|
|
1650
|
+
if (options.continuationPending) assertPendingSource(session, options.continuationPending);
|
|
1651
|
+
const nodesBeforeCommit = session.surface?.nodes ?? [];
|
|
1652
|
+
const sliceBeforeCommit = nodesBeforeCommit.slice(startIdx, endIdx + 1);
|
|
1653
|
+
if (
|
|
1654
|
+
sliceBeforeCommit.length !== shadowedSpanSnapshot.length ||
|
|
1655
|
+
sliceBeforeCommit.some((seq, i) => seq !== shadowedSpanSnapshot[i])
|
|
1656
|
+
) {
|
|
1657
|
+
const error = new Error(
|
|
1658
|
+
'compaction: the selected span changed while the summary was being generated',
|
|
1659
|
+
);
|
|
1660
|
+
error.code = 'SPAN_CHANGED';
|
|
1661
|
+
throw error;
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
// ⚠️ 替换前**最后一次**确认取消状态。
|
|
1665
|
+
// 上面那次 `flush` 是 await:等待期间用户可能按了 Esc、agent 也可能
|
|
1666
|
+
// 收回维护窗口。surface 替换收不回来,所以"取消"必须在它之前生效 ——
|
|
1667
|
+
// 一旦替换执行了,用户得到的回复是 cancelled,历史却被改掉了,
|
|
1668
|
+
// 那是比"压缩失败"严重得多的事。宿主官方实现同样在提交前复查
|
|
1669
|
+
// (`dsh-compaction-basic/lib/index.js:477-485`)。
|
|
1670
|
+
if (signal?.aborted) {
|
|
1671
|
+
const cancelled = new Error('manual compaction was cancelled before the surface was replaced');
|
|
1672
|
+
cancelled.code = 'cancelled';
|
|
1673
|
+
cancelled.cause = signal.reason;
|
|
1674
|
+
throw cancelled;
|
|
1675
|
+
}
|
|
1676
|
+
signal?.throwIfAborted?.();
|
|
1677
|
+
|
|
1678
|
+
// ⚠️ 从替换到 end 写完**不允许有 await**:中途让出事件循环,别的写入
|
|
1679
|
+
// (用户插话、工具结果)会把 surface 改掉,替换边界就失效了 —— 官方
|
|
1680
|
+
// 的实现也是在同一同步段里连写 replacement / end。
|
|
1681
|
+
const strictHost = Boolean(options.strictHost ?? (options.policy ?? this.config)?.strictHostFingerprints) || Boolean(this.requireTransactionAdapter);
|
|
1682
|
+
const useAtomicCommit = options.useAtomicCommit ?? (typeof session.restoreCompactionSurface !== 'function' && Boolean(this.transactionAdapter));
|
|
1683
|
+
const commitProbe = useAtomicCommit && this.transactionAdapter && typeof session.restoreCompactionSurface !== 'function'
|
|
1684
|
+
? this.transactionAdapter.probeSupport(session)
|
|
1685
|
+
: { ok: false };
|
|
1686
|
+
if (!commitProbe.ok && typeof session.restoreCompactionSurface !== 'function') {
|
|
1687
|
+
if (strictHost) {
|
|
1688
|
+
const fallbackError = new Error('Stock-host transaction adapter is unavailable; compression is refused before changing history.');
|
|
1689
|
+
fallbackError.code = 'RECOVERY_REQUIRED';
|
|
1690
|
+
throw commitProbe.error ?? fallbackError;
|
|
1691
|
+
}
|
|
1692
|
+
if (useAtomicCommit) {
|
|
1693
|
+
await this._assertNoUnrecoveredAtomicJournal(session);
|
|
1694
|
+
this._warnHostFallbackOnce(commitProbe.reason ?? 'stock transaction adapter unavailable');
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
if (commitProbe.ok) {
|
|
1698
|
+
const staged = this.transactionAdapter.stage(session, shadow => {
|
|
1699
|
+
const replacement = this._appendSync(shadow, 'user/message', checkpointMessage, {
|
|
1700
|
+
surfaceOp: { op: 'replace', startSeq: start, endSeq: end },
|
|
1701
|
+
sourceEventSeqs: [startEvent?.seq, summaryEvent?.seq, ...shadowedSeqs].filter(seq => typeof seq === 'number'),
|
|
1702
|
+
});
|
|
1703
|
+
const endEvent = this._appendSync(shadow, 'compaction/end', lifecycle);
|
|
1704
|
+
return { replacement, endEvent };
|
|
1705
|
+
});
|
|
1706
|
+
const record = (await this.journal.pending(session.id)).find(r => r.compactionId === compactionId);
|
|
1707
|
+
if (!record || record.corrupt || record.phase !== 'committing') throw new Error('durable preparation journal is missing');
|
|
1708
|
+
const { replacement, endEvent } = await this.transactionAdapter.commit(this, session, record, staged, signal);
|
|
1709
|
+
stockCommitted = true; replaced = true; closing = true; closed = true;
|
|
1710
|
+
committed = { summarized, preserved, archivePath, summaryEvent, replacement, endEvent, checkpointBlocks };
|
|
1711
|
+
} else {
|
|
1712
|
+
const replacement = this._appendSync(session, 'user/message', checkpointMessage, {
|
|
1713
|
+
surfaceOp: { op: 'replace', startSeq: start, endSeq: end },
|
|
1714
|
+
sourceEventSeqs: [startEvent?.seq, summaryEvent?.seq, ...shadowedSeqs].filter(
|
|
1715
|
+
(seq) => typeof seq === 'number',
|
|
1716
|
+
),
|
|
1717
|
+
});
|
|
1718
|
+
replaced = true;
|
|
1719
|
+
|
|
1720
|
+
// ⚠️ 关闭动作只做一次:先立 `closing`,写成功才置 `closed`。
|
|
1721
|
+
// 但**替换已经发生**之后 end 写失败是例外:那时锁会永远开着,会话带着
|
|
1722
|
+
// 孤儿锁一路涨到 413。这种情况下必须再补一次(只补一次),把锁关上 ——
|
|
1723
|
+
// 宁可多一条 end,也不能留下一把永远打不开的锁。
|
|
1724
|
+
closing = true;
|
|
1725
|
+
let endEvent;
|
|
1726
|
+
try {
|
|
1727
|
+
endEvent = this._appendSync(session, 'compaction/end', lifecycle);
|
|
1728
|
+
closed = true;
|
|
1729
|
+
} catch (endError) {
|
|
1730
|
+
this.log('warn', `compaction/end failed after the surface was replaced: ${endError?.message ?? endError}; retrying once to close the lock`);
|
|
1731
|
+
endEvent = this._appendSync(session, 'compaction/end', {
|
|
1732
|
+
...lifecycle,
|
|
1733
|
+
error: `compaction/end failed once and was retried: ${(endError?.message ?? String(endError)).slice(0, 1900)}`,
|
|
1734
|
+
});
|
|
1735
|
+
closed = true;
|
|
1736
|
+
}
|
|
1737
|
+
committed = { summarized, preserved, archivePath, summaryEvent, replacement, endEvent, checkpointBlocks };
|
|
1738
|
+
}
|
|
1739
|
+
} catch (error) {
|
|
1740
|
+
failure = { error, stage: closing ? 'commit' : stage };
|
|
1741
|
+
if (!closing) {
|
|
1742
|
+
closing = true;
|
|
1743
|
+
// `compaction/end.error` 在事件 schema 里是 **string**,不是对象
|
|
1744
|
+
// (`'compaction/end': { compactionId; sourceCommandId?; turn; error?: string }`)。
|
|
1745
|
+
// 塞对象会被 `validateSessionEventData` 直接拒掉,等于连"失败"都记不下来,
|
|
1746
|
+
// 会话就会带着一个永远不闭合的 compaction/start 锁(孤儿锁)一路涨到 413。
|
|
1747
|
+
const closeText = `${error?.code ? `${error.code}: ` : ''}${error?.message ?? String(error)}`;
|
|
1748
|
+
try {
|
|
1749
|
+
await this._append(session, 'compaction/end', { ...lifecycle, error: closeText.slice(0, 2000) });
|
|
1750
|
+
closed = true;
|
|
1751
|
+
} catch (closeError) {
|
|
1752
|
+
// end 都写不进去:真正的失败原因换成它,并按提交段归类。
|
|
1753
|
+
failure = { error: closeError, stage: 'commit' };
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
// ── 统一收尾 ───────────────────────────────────────────────────────────
|
|
1759
|
+
// 不管成功还是失败,只要 end 已经落盘就等一次持久化边界。宿主 JSONL 后端
|
|
1760
|
+
// 把普通事件放进后台队列,只有 `session/flush` 会真的 drain + fsync ——
|
|
1761
|
+
// "返回成功"必须意味着已经落盘。官方同样把 flush 放在事务外统一做
|
|
1762
|
+
// (`dsh-compaction-basic/lib/index.js:511-513`)。
|
|
1763
|
+
if (!stockCommitted && closed && typeof this.ctx.sessions?.flush === 'function') {
|
|
1764
|
+
try {
|
|
1765
|
+
await this._flushSession(session);
|
|
1766
|
+
} catch (error) {
|
|
1767
|
+
flushFailure = error;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
// ⚠️ 持久化等待期间,维护窗口可能已经被收回(`agent.cancel`、用户 Esc)。
|
|
1772
|
+
// flush 是异步的,`await` 之后必须重新确认这次操作还算不算数 —— 否则手动
|
|
1773
|
+
// 命令会在"已经被取消"的情况下回一条 `Compacted …` 成功消息,与宿主的
|
|
1774
|
+
// 取消协议相反。宿主在 flush 之后同样重新检查(`dsh-compaction-basic/lib/index.js:513`)。
|
|
1775
|
+
//
|
|
1776
|
+
// 只对**已提交**的手动事务做这个检查:没提交成功时下面的 failure 分支
|
|
1777
|
+
// 自己会抛,不该被取消语义盖掉真实原因。已经写进去的事件不回滚 —— 取消
|
|
1778
|
+
// 只能决定"这次操作算不算成功",不能撤销已经落盘的 surface 替换。
|
|
1779
|
+
if (!stockCommitted && committed !== null && manual && flushFailure === undefined) {
|
|
1780
|
+
try {
|
|
1781
|
+
signal?.throwIfAborted?.();
|
|
1782
|
+
} catch (abortError) {
|
|
1783
|
+
const cancelled = new Error('manual compaction was cancelled');
|
|
1784
|
+
cancelled.code = 'cancelled';
|
|
1785
|
+
cancelled.cause = abortError;
|
|
1786
|
+
throw cancelled;
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
if (failure !== undefined) {
|
|
1791
|
+
if (failure.error?.code === 'RECOVERY_REQUIRED') { this._recoveryBlocked.add(this._keyOf(session)); throw failure.error; }
|
|
1792
|
+
const error = failure.error;
|
|
1793
|
+
const failedStage = failure.stage;
|
|
1794
|
+
const errorText = `${error?.code ? `${error.code}: ` : ''}${error?.message ?? String(error)}`;
|
|
1795
|
+
|
|
1796
|
+
// 取消不是故障。agent 收回维护窗口、用户按 Esc 都会走到这里,
|
|
1797
|
+
// 计入熔断的后果是"连续取消三次 → 这个会话永久不再自动压缩"。
|
|
1798
|
+
if (!isAbortError(error, signal)) {
|
|
1799
|
+
this._recordFailure(agent, session, error);
|
|
1800
|
+
}
|
|
1801
|
+
if (!replaced && closed && flushFailure === undefined) await this.journal.clear(session.id, compactionId);
|
|
1802
|
+
|
|
1803
|
+
// ── L6:渐进收敛 ──────────────────────────────────────────────────────
|
|
1804
|
+
// 只有在"少压一点可能就能成"的失败上才重试;超时/全部目标失败/摘要不比
|
|
1805
|
+
// 原文小,重试也是白烧一次调用。
|
|
1806
|
+
//
|
|
1807
|
+
// ⚠️ 上面 `_recordFailure` 可能刚刚把会话打入熔断(第 3 次失败)。
|
|
1808
|
+
// 熔断的意义是"别再试了"——如果这里不复查,第 3 次失败之后仍会因为
|
|
1809
|
+
// `attempt < reactiveStep` 而再发一次摘要请求,等于把"3 次封顶"变成 4 次,
|
|
1810
|
+
// 而第 4 次注定失败,只是把账单和延迟都翻倍。
|
|
1811
|
+
if (
|
|
1812
|
+
!replaced &&
|
|
1813
|
+
closed &&
|
|
1814
|
+
flushFailure === undefined &&
|
|
1815
|
+
attempt < this.config.reactiveStep &&
|
|
1816
|
+
!signal?.aborted &&
|
|
1817
|
+
!this._stopped.has(this._keyOf(session)) &&
|
|
1818
|
+
this._isRecoverable(error) &&
|
|
1819
|
+
typeof options.reselect === 'function'
|
|
1820
|
+
) {
|
|
1821
|
+
// 先让观察者知道这次摘要失败了(官方语义:返回 true = 已同步记录持久变更)。
|
|
1822
|
+
await this._dispatchSummaryError(session, shadowedSeqs, error, signal);
|
|
1823
|
+
|
|
1824
|
+
const next = await options.reselect(attempt + 1, { start, end });
|
|
1825
|
+
if (next) {
|
|
1826
|
+
this.log(
|
|
1827
|
+
'warn',
|
|
1828
|
+
`summary failed (${errorText.slice(0, 160)}); retrying with a smaller span ${next.start}-${next.end} (attempt ${attempt + 2})`,
|
|
1829
|
+
);
|
|
1830
|
+
return this._compactSpan(next.start, next.end, agent, signal, options, attempt + 1);
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
// 记下失败发生在哪一段:手动路径要靠它把未知错误码归类
|
|
1835
|
+
// (提交段的未知错误 = commit,其它 = summary)。属性挂在错误对象上而不是
|
|
1836
|
+
// 再包一层,自动压缩路径完全不受影响。
|
|
1837
|
+
if (error && typeof error === 'object' && error.compactionStage === undefined) {
|
|
1838
|
+
error.compactionStage = failedStage;
|
|
1839
|
+
}
|
|
1840
|
+
throw error;
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
if (flushFailure !== undefined) {
|
|
1844
|
+
const flushError = new Error(`compaction persistence failed: ${flushFailure?.message ?? flushFailure}`);
|
|
1845
|
+
flushError.code = 'persistence';
|
|
1846
|
+
flushError.cause = flushFailure;
|
|
1847
|
+
// ⚠️ 持久化失败也要计入熔断。它是"这次压缩没落盘"的**真实失败**,
|
|
1848
|
+
// 前面只把 summary/commit 阶段计进去,于是同一台机器磁盘一直满、
|
|
1849
|
+
// 每一轮都失败、每一轮都不计数 —— 永远触发不了"连续 3 次就别再试了"。
|
|
1850
|
+
this._recordFailure(agent, session, flushError);
|
|
1851
|
+
throw flushError;
|
|
1852
|
+
}
|
|
1853
|
+
if (committed === null) {
|
|
1854
|
+
throw new Error('compaction committed without a result');
|
|
1855
|
+
}
|
|
1856
|
+
const { summarized, preserved, archivePath, summaryEvent, replacement, endEvent, checkpointBlocks } =
|
|
1857
|
+
committed;
|
|
1858
|
+
|
|
1859
|
+
// Native patched fallback only; stock adapter already committed this state.
|
|
1860
|
+
if (!stockCommitted) {
|
|
1861
|
+
// 先完成持久策略更新;写失败仍保留 journal 和原事件快照。
|
|
1862
|
+
try {
|
|
1863
|
+
this.sessionState.update(session.id, state => ({ ...state, failures: 0, stopped: false }));
|
|
1864
|
+
} catch (cause) {
|
|
1865
|
+
const error = new Error(`compaction replaced history but could not save policy state: ${cause?.message ?? cause}`);
|
|
1866
|
+
error.code = 'commit'; error.compactionStage = 'commit'; error.cause = cause;
|
|
1867
|
+
throw error;
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
// The durable success decision is owned by the plugin on EVERY host.
|
|
1871
|
+
// Cleanup failure must not turn a successful stock-host commit into an
|
|
1872
|
+
// unresolved transaction, nor depend on a companion core patch.
|
|
1873
|
+
signal?.throwIfAborted?.();
|
|
1874
|
+
await this._journalRecord(session, compactionId, 'committed', start, end);
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
// 事务走完了:把持久痕迹清掉。留着的文件天然意味着"上次没走完",
|
|
1878
|
+
// 下次启动会被 `recoverPending` 拿去收尾。
|
|
1879
|
+
if (this.journal && typeof this.journal.clear === 'function') {
|
|
1880
|
+
await this.journal.clear(session.id, compactionId);
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
if (options.continuation) settleContinuation(this.sessionState, session);
|
|
1884
|
+
|
|
1885
|
+
this._failures.delete(this._keyOf(session));
|
|
1886
|
+
// ⚠️ 熔断必须能解除。`_stopped` 是"连续失败太多次,别再自动压了"的闸门,
|
|
1887
|
+
// 但成功的这次证明压缩链是通的 —— 如果只清计数不清闸门,一次瞬时故障
|
|
1888
|
+
// (比如一次网络抖动)就会让这个会话**永久**不再自动压缩,一路涨到 413。
|
|
1889
|
+
this._stopped.delete(this._keyOf(session));
|
|
1890
|
+
const after = await this._measure(session);
|
|
1891
|
+
noteDiagnosticMeasurement(after,{stage:'after'});
|
|
1892
|
+
if (after) this._lastTotal.set(session, after.totalTokens);
|
|
1893
|
+
|
|
1894
|
+
// 注意:CompactionResult 的 startSeq / summarySeq / endSeq 是**事件 seq**,
|
|
1895
|
+
// 不是 surface 位置。官方 `commitCompactionBody` 返回的是 `startEvent.seq`
|
|
1896
|
+
// 与 `completeCompaction` 补上的 `endEvent.seq`。被覆盖的范围另由
|
|
1897
|
+
// `shadowedRange` 表达。
|
|
1898
|
+
const result = {
|
|
1899
|
+
compactionId,
|
|
1900
|
+
...(sourceCommandId === undefined ? {} : { sourceCommandId }),
|
|
1901
|
+
startSeq: startEvent?.seq ?? start,
|
|
1902
|
+
summarySeq: summaryEvent?.seq ?? replacement?.seq ?? start,
|
|
1903
|
+
endSeq: endEvent?.seq ?? end,
|
|
1904
|
+
summary: checkpointBlocks,
|
|
1905
|
+
shadowedRange: { start, end },
|
|
1906
|
+
shadowedSeqs,
|
|
1907
|
+
shadowedTokenCount,
|
|
1908
|
+
// 这个数有多可信,一并交给调用方:`/compact` 的输出、日志、审计都要用。
|
|
1909
|
+
// 只给数字不给口径,等于让下游把粗估当实测。
|
|
1910
|
+
shadowedTokenPrecision,
|
|
1911
|
+
provider: summarized.provider,
|
|
1912
|
+
model: summarized.model,
|
|
1913
|
+
...(summarized.continuation ? {continuation:summarized.continuation} : {}),
|
|
1914
|
+
preservedUserMessages: preserved.length,
|
|
1915
|
+
archivePath,
|
|
1916
|
+
};
|
|
1917
|
+
|
|
1918
|
+
if (this.config.debug) {
|
|
1919
|
+
this.log('info', `compacted ${shadowedSeqs.length} seq(s) via ${summarized.provider}/${summarized.model}`);
|
|
1920
|
+
}
|
|
1921
|
+
return result;
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
/** 哪些失败值得"少压一点再试一次"。 */
|
|
1925
|
+
_isRecoverable(error) {
|
|
1926
|
+
const code = error?.code;
|
|
1927
|
+
if (error?.summaryRetryExhausted) return false;
|
|
1928
|
+
// 聚合错误要先看它自己带出来的 recoverable 标记:目标链里只要有一条是
|
|
1929
|
+
// "窗口撑爆",缩小跨度就还有救;否则整体放弃。
|
|
1930
|
+
if (code === 'SUMMARY_ALL_FAILED') return error?.recoverable === true;
|
|
1931
|
+
if (code === 'SUMMARY_TIMEOUT' || code === 'NO_SUMMARY_TARGET') return false;
|
|
1932
|
+
if (code === 'SUMMARY_NOT_SMALLER') return false;
|
|
1933
|
+
if (code === 'ABORTED') return false;
|
|
1934
|
+
if (code === 'MAX_TOKENS' || code === 'UNSUPPORTED_CONTENT') return true;
|
|
1935
|
+
// 窗口溢出要按 code 认,不能只靠 message 正则:适配器给出的原文常常是
|
|
1936
|
+
// "request failed" 之类的通用文案,关键词一个都不在里面。
|
|
1937
|
+
if (code === 'CONTEXT_WINDOW_EXCEEDED') return true;
|
|
1938
|
+
const message = error?.message ?? '';
|
|
1939
|
+
if (/context length|context window|too long|CONTEXT_WINDOW_EXCEEDED/i.test(message)) return true;
|
|
1940
|
+
return false;
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
async _dispatchSummaryError(session, sourceEventSeqs, error, signal) {
|
|
1944
|
+
if (typeof this.ctx.waterfall !== 'function') return false;
|
|
1945
|
+
try {
|
|
1946
|
+
const accepted = this.ctx.waterfall(
|
|
1947
|
+
'compaction/summary-error',
|
|
1948
|
+
{ session, sourceEventSeqs, error, ...(signal === undefined ? {} : { signal }) },
|
|
1949
|
+
() => false,
|
|
1950
|
+
);
|
|
1951
|
+
return typeof accepted?.then === 'function' ? await accepted : accepted;
|
|
1952
|
+
} catch {
|
|
1953
|
+
return false;
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
/**
|
|
1958
|
+
* 估算一段 checkpoint 内容的 token。
|
|
1959
|
+
*
|
|
1960
|
+
* 优先用宿主 `tokenMeter.estimateMessage`(当前宿主使用字符粗估,非模型 tokenizer),
|
|
1961
|
+
* 退化为本地粗估。**必须**用同一个口径去比"是否比原文小",否则两套口径
|
|
1962
|
+
* 各算各的,检查会形同虚设。
|
|
1963
|
+
*/
|
|
1964
|
+
_estimateCheckpointTokens(blocks) {
|
|
1965
|
+
const meter = this.ctx.tokenMeter;
|
|
1966
|
+
if (meter && typeof meter.estimateMessage === 'function') {
|
|
1967
|
+
try {
|
|
1968
|
+
const value = meter.estimateMessage({ id: 'estimate', role: 'user', content: blocks });
|
|
1969
|
+
if (typeof value === 'number' && Number.isFinite(value) && value > 0) return value;
|
|
1970
|
+
} catch {
|
|
1971
|
+
// 退化到本地估算
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
return approxTokens(blocksToText(blocks));
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
// ────────────────────────────────────────────────────────────── 内部工具
|
|
1978
|
+
|
|
1979
|
+
async _flushSession(session) {
|
|
1980
|
+
const participated = await this.ctx.sessions.flush(session);
|
|
1981
|
+
// Real SessionStore returns false when no durability listener owns this session.
|
|
1982
|
+
if (participated === false) {
|
|
1983
|
+
const error = new Error('compaction requires a session persistence listener; flush did not persist the session');
|
|
1984
|
+
error.code = 'persistence'; throw error;
|
|
1985
|
+
}
|
|
1986
|
+
return participated;
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
_recordFailure(agent, session, error) {
|
|
1990
|
+
// Rechecking a spent budget without a new request is a free pause. If this
|
|
1991
|
+
// operation did spend requests before exhausting it, a failed request was
|
|
1992
|
+
// consumed (e.g. audit rejection before a blocked corrective request).
|
|
1993
|
+
// Count that failed operation once; never exempt it because the final
|
|
1994
|
+
// error happens to be the budget guard. Legacy errors have no new-call
|
|
1995
|
+
// evidence and keep the conservative free-pause behavior.
|
|
1996
|
+
const budget=continuationBudgetInfo(error);
|
|
1997
|
+
if(budget&&!(budget.requestsThisAttempt>0))return;
|
|
1998
|
+
const key = this._keyOf(session);
|
|
1999
|
+
this._restoreState(session);
|
|
2000
|
+
const state = this.sessionState.update(session.id, current => {
|
|
2001
|
+
const failures = current.failures + 1;
|
|
2002
|
+
return { ...current, failures, stopped: failures >= this.config.failureThreshold };
|
|
2003
|
+
});
|
|
2004
|
+
const next = state.failures;
|
|
2005
|
+
this._failures.set(key, next);
|
|
2006
|
+
if (next >= this.config.failureThreshold && !this._stopped.has(key)) {
|
|
2007
|
+
this._stopped.set(key, true);
|
|
2008
|
+
this.log(
|
|
2009
|
+
'warn',
|
|
2010
|
+
`compaction disabled for this session after ${next} consecutive failures (last: ${error?.message ?? error}); ` +
|
|
2011
|
+
'automatic retries are stopped; inspect the error and retained recovery journal for commit status',
|
|
2012
|
+
);
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
_audit(session, shadowedSeqs, summaryTextValue, compactionId, checkpointBlocks, policy=this.config) {
|
|
2017
|
+
let selection;
|
|
2018
|
+
try {
|
|
2019
|
+
selection = selectSessionMemory(session, shadowedSeqs, {approxTokens});
|
|
2020
|
+
} catch (error) {
|
|
2021
|
+
const failure = new Error(`compaction: constraint audit could not read source messages: ${error?.message ?? error}`);
|
|
2022
|
+
failure.code = 'AUDIT_UNAVAILABLE'; failure.cause = error; throw failure;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
const report = auditMemorySelection({
|
|
2026
|
+
selection,
|
|
2027
|
+
summaryText: summaryTextValue,
|
|
2028
|
+
// ⚠️ 必须把完整检查点也交进去。用户原话被逐字保留在检查点末尾时,
|
|
2029
|
+
// 摘要正文里当然没有它 —— 那不是丢失。只看摘要正文会把"原文就在
|
|
2030
|
+
// 下游模型能读到的地方"报成约束丢失。
|
|
2031
|
+
checkpointText: Array.isArray(checkpointBlocks)
|
|
2032
|
+
? checkpointBlocks.filter((block) => block?.type === 'text').map((block) => block.text).join('\n')
|
|
2033
|
+
: undefined,
|
|
2034
|
+
threshold: policy.auditCoverageThreshold,
|
|
2035
|
+
includeAllConflicts: true,
|
|
2036
|
+
referenceMessages:collectReferenceMessages(session,session.surface.nodes),
|
|
2037
|
+
});
|
|
2038
|
+
|
|
2039
|
+
const flips = report.lost.filter((item) => item.reason === 'polarity');
|
|
2040
|
+
if (report.lost.length > 0) {
|
|
2041
|
+
this.log(
|
|
2042
|
+
'warn',
|
|
2043
|
+
`constraint audit ${compactionId}: ${report.lost.length}/${report.checked} source sentence(s) failed audit ` +
|
|
2044
|
+
`(${flips.length} polarity reversal(s), worst coverage ${report.worstCoverage}); ` +
|
|
2045
|
+
`seq=${report.lost[0].seq}, reason=${report.lost[0].reason}; source: ${JSON.stringify(report.lost[0].line.slice(0, 120))}; ` +
|
|
2046
|
+
`candidate: ${JSON.stringify(report.lost[0].candidate?.slice(0, 120) ?? null)}`,
|
|
2047
|
+
);
|
|
2048
|
+
} else if (report.checked > 0 && this.config.debug) {
|
|
2049
|
+
this.log('info', `constraint audit ${compactionId}: all ${report.checked} source sentence(s) preserved`);
|
|
2050
|
+
}
|
|
2051
|
+
return report;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
_shadowedTokens(session, shadowedSeqs) {
|
|
2055
|
+
const priced = this._lastPriced?.get(session);
|
|
2056
|
+
let total = 0;
|
|
2057
|
+
for (const seq of shadowedSeqs) {
|
|
2058
|
+
// ⚠️ 缺价时不能用 0 顶替。写 `priced.get(seq) ?? 0` 意味着"没有计价 = 这段
|
|
2059
|
+
// 不占 token",于是被折叠区间的原始体量被系统性低估,"摘要必须比原文小"
|
|
2060
|
+
// 这道门的基准也就跟着虚低 —— 判据还在,但它比的是错的分母。
|
|
2061
|
+
// 没有真实计价就退回字符估算,宁可估算,也不要凭空当零。
|
|
2062
|
+
const cost = priced?.get(seq);
|
|
2063
|
+
if (typeof cost === 'number' && Number.isFinite(cost) && cost > 0) {
|
|
2064
|
+
total += cost;
|
|
2065
|
+
continue;
|
|
2066
|
+
}
|
|
2067
|
+
const event = typeof session.eventAt === 'function' ? session.eventAt(seq) : undefined;
|
|
2068
|
+
const message =
|
|
2069
|
+
typeof session.deriveEventMessage === 'function' ? session.deriveEventMessage(event) : undefined;
|
|
2070
|
+
total += approxTokens(blocksToText(message?.content));
|
|
2071
|
+
}
|
|
2072
|
+
return total;
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
async _measure(session) {
|
|
2076
|
+
const meter = this.ctx.tokenMeter;
|
|
2077
|
+
if (!meter || typeof meter.measure !== 'function') return null;
|
|
2078
|
+
try {
|
|
2079
|
+
const measurement = await meter.measure(session);
|
|
2080
|
+
if (!measurement || typeof measurement !== 'object') return null;
|
|
2081
|
+
|
|
2082
|
+
// 宿主真实口径:`measure()` 是**同步**的,返回
|
|
2083
|
+
// `{logRevision, baseline, surfaceDeltaTokens, totalTokens, surfaceTokens, nodes}`,
|
|
2084
|
+
// 其中 `nodes[]` 元素是 `{seq, tokens, heuristicTokens}` —— 不是裸 seq 数字。
|
|
2085
|
+
// 这里统一归一化成"序号数组 + seq→tokens 的 Map",下游只认这一种形态。
|
|
2086
|
+
const rawNodes = Array.isArray(measurement.nodes) ? measurement.nodes : [];
|
|
2087
|
+
const nodes = [];
|
|
2088
|
+
const priced = new Map();
|
|
2089
|
+
for (const node of rawNodes) {
|
|
2090
|
+
if (typeof node === 'number') {
|
|
2091
|
+
nodes.push(node);
|
|
2092
|
+
continue;
|
|
2093
|
+
}
|
|
2094
|
+
if (!node || typeof node !== 'object' || typeof node.seq !== 'number') continue;
|
|
2095
|
+
nodes.push(node.seq);
|
|
2096
|
+
const cost = typeof node.tokens === 'number' && Number.isFinite(node.tokens) ? node.tokens : node.heuristicTokens;
|
|
2097
|
+
if (typeof cost === 'number' && Number.isFinite(cost)) priced.set(node.seq, cost);
|
|
2098
|
+
}
|
|
2099
|
+
if (nodes.length === 0 && Array.isArray(session.surface?.nodes)) {
|
|
2100
|
+
nodes.push(...session.surface.nodes);
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
const totalTokens =
|
|
2104
|
+
typeof measurement.totalTokens === 'number'
|
|
2105
|
+
? measurement.totalTokens
|
|
2106
|
+
: typeof measurement.total === 'number'
|
|
2107
|
+
? measurement.total
|
|
2108
|
+
: 0;
|
|
2109
|
+
|
|
2110
|
+
// 计量口径要**跟着数字一起交出去**,不能让下游把估算当实测。
|
|
2111
|
+
//
|
|
2112
|
+
// ⚠️ 判断依据不是"`tokens` 这个字段在不在"。宿主 `priceSurface`
|
|
2113
|
+
// (`dsh-token-meter/lib/index.js:611-668`)**总会**返回 `tokens`:
|
|
2114
|
+
// 没有路由图片/文件定价时它就是 `heuristicTokens` 的副本
|
|
2115
|
+
// (`tokens: node.heuristicTokens`),节点本身只有 `heuristicTokens`
|
|
2116
|
+
// 是"4 字符一个 token"的粗估(`analyzeNode` 只算 `estimateMessage`)。
|
|
2117
|
+
// 所以"有 `tokens` 字段"等于什么信息都没有 —— 按它判会把整场粗估
|
|
2118
|
+
// 标成 `priced`,顺带把那句"非精确"的提示也抑制掉。
|
|
2119
|
+
//
|
|
2120
|
+
// 真正的信号有两个,都不在节点上:
|
|
2121
|
+
// - **节点级**:`tokens !== heuristicTokens`,说明路由定价(图片/文件
|
|
2122
|
+
// 的请求投影)真的替换了粗估;
|
|
2123
|
+
// - **整体级**:`baseline.kind === 'usage'`,说明总量里含真实响应 usage。
|
|
2124
|
+
let pricedCount = 0;
|
|
2125
|
+
let heuristicCount = 0;
|
|
2126
|
+
for (const node of rawNodes) {
|
|
2127
|
+
if (!node || typeof node !== 'object') continue;
|
|
2128
|
+
const heuristic = node.heuristicTokens;
|
|
2129
|
+
const tokens = node.tokens;
|
|
2130
|
+
const hasTokens = typeof tokens === 'number' && Number.isFinite(tokens);
|
|
2131
|
+
const hasHeuristic = typeof heuristic === 'number' && Number.isFinite(heuristic);
|
|
2132
|
+
if (!hasTokens && !hasHeuristic) continue;
|
|
2133
|
+
if (!hasTokens) {
|
|
2134
|
+
// 只有粗估:宿主没给它任何实数价格。
|
|
2135
|
+
heuristicCount += 1;
|
|
2136
|
+
continue;
|
|
2137
|
+
}
|
|
2138
|
+
// 只有 `tokens` 没有 `heuristicTokens`:宿主不这样返回,但外部计量服务
|
|
2139
|
+
// (或测试桩)给了实数就当它报了实数。两个都有时,相等 = 粗估副本,
|
|
2140
|
+
// 不等 = 路由定价(图片/文件投影)插过手。
|
|
2141
|
+
if (!hasHeuristic || tokens !== heuristic) pricedCount += 1;
|
|
2142
|
+
else heuristicCount += 1;
|
|
2143
|
+
}
|
|
2144
|
+
const baselineKind = measurement.baseline?.kind;
|
|
2145
|
+
const precision = pricedCount === 0 && heuristicCount === 0
|
|
2146
|
+
? baselineKind === 'usage'
|
|
2147
|
+
? 'priced'
|
|
2148
|
+
: 'unknown'
|
|
2149
|
+
: heuristicCount === 0
|
|
2150
|
+
? 'priced'
|
|
2151
|
+
: pricedCount === 0
|
|
2152
|
+
? baselineKind === 'usage'
|
|
2153
|
+
? 'mixed'
|
|
2154
|
+
: 'heuristic'
|
|
2155
|
+
: 'mixed';
|
|
2156
|
+
|
|
2157
|
+
this._lastPriced = this._lastPriced ?? new WeakMap();
|
|
2158
|
+
this._lastPriced.set(session, priced);
|
|
2159
|
+
this._lastPrecision = this._lastPrecision ?? new WeakMap();
|
|
2160
|
+
this._lastPrecision.set(session, precision);
|
|
2161
|
+
return { nodes, pricedBySeq: priced, totalTokens, precision, raw: measurement };
|
|
2162
|
+
} catch (error) {
|
|
2163
|
+
this.log('warn', `token measurement failed: ${error?.message ?? error}`);
|
|
2164
|
+
return null;
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
/**
|
|
2169
|
+
* 只解析"这个会话该用哪份策略",不碰预算。
|
|
2170
|
+
*
|
|
2171
|
+
* ⚠️ 必须和 `_resolveSpec` 分开。预算计算会因为"窗口太小 / 预留太大 /
|
|
2172
|
+
* 保留量组合越界"抛错,而**摘要该发给谁**跟预算没关系。两者揉在一个函数里,
|
|
2173
|
+
* 任何预算异常都会连带把已经算好的 provider/model 策略丢掉,摘要退回全局
|
|
2174
|
+
* 配置 —— 用户配了 `modelPolicies[i].summarizationProvider` 却被静默改道,
|
|
2175
|
+
* 这种回退比直接报错更糟。
|
|
2176
|
+
*
|
|
2177
|
+
* 同步、无副作用、不抛错(provider/model 缺失时只是匹配不到覆盖项)。
|
|
2178
|
+
*/
|
|
2179
|
+
_resolvePolicy(agent) {
|
|
2180
|
+
const durable = latestDurableTarget(agent);
|
|
2181
|
+
const provider = durable?.provider ?? agent?.options?.provider;
|
|
2182
|
+
const model = durable?.model ?? agent?.options?.model;
|
|
2183
|
+
return resolveTargetPolicy(this.config, provider, model);
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
async _resolveSpec(agent, measurement, signal, {budgetOnly = false} = {}) {
|
|
2187
|
+
const session = agent?.session;
|
|
2188
|
+
const durable = latestDurableTarget(agent);
|
|
2189
|
+
const provider = durable?.provider ?? agent?.options?.provider;
|
|
2190
|
+
const model = durable?.model ?? agent?.options?.model;
|
|
2191
|
+
|
|
2192
|
+
let adapterInfo;
|
|
2193
|
+
try {
|
|
2194
|
+
if (provider && model && typeof this.ctx.llm?.resolveModelInfo === 'function') {
|
|
2195
|
+
adapterInfo = await this.ctx.llm.resolveModelInfo(provider, model, signal);
|
|
2196
|
+
}
|
|
2197
|
+
} catch {
|
|
2198
|
+
adapterInfo = undefined;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
signal?.throwIfAborted?.();
|
|
2202
|
+
let locked;
|
|
2203
|
+
try {
|
|
2204
|
+
this.sessionState.update(session.id, state => {
|
|
2205
|
+
locked = lockWindow({
|
|
2206
|
+
session, persisted: state.window, provider, model,
|
|
2207
|
+
adapterWindow: contextWindowOf(adapterInfo),
|
|
2208
|
+
providerMap: this.config.windowProviderMap,
|
|
2209
|
+
fallback: this.config.windowFallback, clampMin: this.config.windowClampMin,
|
|
2210
|
+
});
|
|
2211
|
+
return { ...state, window: locked };
|
|
2212
|
+
});
|
|
2213
|
+
} catch (cause) {
|
|
2214
|
+
const error = new Error(`durable compaction policy is unavailable: ${cause.message}`);
|
|
2215
|
+
error.code = 'STATE_UNAVAILABLE'; error.cause = cause; throw error;
|
|
2216
|
+
}
|
|
2217
|
+
const reserved = this._reservedCompletionTokens(agent, defaultMaxTokensOf(adapterInfo));
|
|
2218
|
+
// 按 provider+model 应用 `modelPolicies` 覆盖项。
|
|
2219
|
+
// ⚠️ 顺序不能反:先拿到 provider/model(上面从 durable target 解析出来的),
|
|
2220
|
+
// 再拿它去查覆盖项,最后才用合并后的配置算 spec。直接用全局配置算 spec 时,
|
|
2221
|
+
// `modelPolicies` 里的 maxTokens / 阈值会全部静默失效。
|
|
2222
|
+
const effective = resolveTargetPolicy(this.config, provider, model);
|
|
2223
|
+
if (budgetOnly) {
|
|
2224
|
+
const messageBudgetTokens = locked.safetyWindow - reserved;
|
|
2225
|
+
if (messageBudgetTokens <= 0) throw Object.assign(new Error('No message budget remains after reserving completion tokens.'), {code:'COMPACTION_REQUEST_BUDGET_EXCEEDED'});
|
|
2226
|
+
return {messageBudgetTokens,policy:effective};
|
|
2227
|
+
}
|
|
2228
|
+
return {
|
|
2229
|
+
...resolveCompactSpec(effective, locked.safetyWindow, reserved),
|
|
2230
|
+
windowSource: locked.source,
|
|
2231
|
+
lockedWindow: locked.window,
|
|
2232
|
+
safetyWindow: locked.safetyWindow,
|
|
2233
|
+
provider,
|
|
2234
|
+
model,
|
|
2235
|
+
// 合并后的整份策略要跟着 spec 一起往下传:摘要目标、超时、语言、保留尾
|
|
2236
|
+
// 也都按同一个 provider/model 解析,否则会出现"预算按目标模型算、
|
|
2237
|
+
// 摘要却发给全局配置的模型"这种自相矛盾的状态。
|
|
2238
|
+
policy: effective,
|
|
2239
|
+
};
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
/**
|
|
2243
|
+
* 预留多少 token 给模型输出。
|
|
2244
|
+
*
|
|
2245
|
+
* ⚠️ 必须与宿主逐字对齐,只认两个来源:
|
|
2246
|
+
* `requestHeader().config.maxTokens`(这次请求实际会用的输出上限,用户在 UI
|
|
2247
|
+
* 里改的就是它)和适配器报出来的默认上限,两者都没有就是 **0**。
|
|
2248
|
+
*
|
|
2249
|
+
* 两个常见的"想当然"都会算错:
|
|
2250
|
+
* - 把适配器默认值排在 header 前面 → header 写 4096、适配器默认 65536 时
|
|
2251
|
+
* 预留 65536,凭空少算 6 万 token,压缩线被人为抬高一大截。
|
|
2252
|
+
* - 拿不到 header 时退回 `agent.options.maxTokens` 或插件自己的摘要
|
|
2253
|
+
* `maxTokens` → 前者是 agent 创建时的初始值,不等于这次请求;后者是
|
|
2254
|
+
* **摘要请求**的输出上限,跟对话请求的预留毫无关系,8192 是凭空的。
|
|
2255
|
+
*
|
|
2256
|
+
* 宿主实现:`dsh-compaction-basic/lib/index.js:757-759`
|
|
2257
|
+
* `agent.session.requestHeader()?.config.maxTokens ?? defaultMaxTokens ?? 0`
|
|
2258
|
+
*/
|
|
2259
|
+
_reservedCompletionTokens(agent, defaultMaxTokens) {
|
|
2260
|
+
const header = typeof agent?.session?.requestHeader === 'function' ? agent.session.requestHeader() : undefined;
|
|
2261
|
+
const headerMax = header?.config?.maxTokens;
|
|
2262
|
+
const candidate = headerMax ?? defaultMaxTokens ?? 0;
|
|
2263
|
+
if (typeof candidate === 'number' && Number.isFinite(candidate) && candidate >= 0) return candidate;
|
|
2264
|
+
return 0;
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
/**
|
|
2268
|
+
* 落一笔事务日志。
|
|
2269
|
+
*
|
|
2270
|
+
* 写失败必须抛出,阻止不可恢复提交;错误在生命周期边界内统一收尾。
|
|
2271
|
+
*/
|
|
2272
|
+
async _journalRecord(session, compactionId, phase, start, end) {
|
|
2273
|
+
if (!this.journal || typeof this.journal.record !== 'function') return;
|
|
2274
|
+
try {
|
|
2275
|
+
await this.journal.record({
|
|
2276
|
+
sessionId: session?.id,
|
|
2277
|
+
compactionId,
|
|
2278
|
+
phase,
|
|
2279
|
+
failuresBefore: this.sessionState.read(session.id).failures,
|
|
2280
|
+
...(phase === 'started' ? { startEventSeq: session.seq - 1 } : {}),
|
|
2281
|
+
...(start === undefined ? {} : { startSeq: start }),
|
|
2282
|
+
...(end === undefined ? {} : { endSeq: end }),
|
|
2283
|
+
...(phase !== 'committing' ? {} : { snapshot: (session.surface?.nodes ?? []).map(seq => ({ seq, event: session.eventAt(seq), message: session.deriveEventMessage(session.eventAt(seq)) })) }),
|
|
2284
|
+
});
|
|
2285
|
+
} catch (error) {
|
|
2286
|
+
this.log('warn', `compaction journal write failed (${phase}): ${error?.message ?? error}`);
|
|
2287
|
+
error.code ??= 'persistence';
|
|
2288
|
+
throw error;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
|
|
2292
|
+
/**
|
|
2293
|
+
* 收尾上一次没走完的事务。
|
|
2294
|
+
*
|
|
2295
|
+
* review.5 原装事务由根适配器在加载前恢复;此处完成生命周期/日志收尾。
|
|
2296
|
+
* 历史补丁路径仍可使用 rollback;旧版已发布但无备份的未决替换必须阻断。
|
|
2297
|
+
* committed 是成功决策;恢复失败保留日志,前置钩子阻止后续模型请求。
|
|
2298
|
+
* 重复恢复幂等,取消不计失败,其余恢复以 failuresBefore 保留熔断次数。
|
|
2299
|
+
* @param {object} session
|
|
2300
|
+
*/
|
|
2301
|
+
async recoverPending(session, owned = false, failureKind = 'interrupted') {
|
|
2302
|
+
if (!session?.id || !this.journal) return { recovered: 0 };
|
|
2303
|
+
if (this.transactionAdapter?.blocked.has(session.id)) { const error = new Error('Session writer is quarantined; restart DSH to restore the verified transaction backup.'); error.code = 'RECOVERY_REQUIRED'; throw error; }
|
|
2304
|
+
const key = `${this.sessionState.root}:${JSON.stringify(session.id)}`;
|
|
2305
|
+
if (!owned && activeTransactions.has(key)) return { recovered: 0, busy: true };
|
|
2306
|
+
if (!owned) activeTransactions.add(key);
|
|
2307
|
+
try {
|
|
2308
|
+
const pending = await this.journal.pending(session.id);
|
|
2309
|
+
let recovered = 0, restored = 0;
|
|
2310
|
+
for (const record of pending) {
|
|
2311
|
+
if (record.corrupt) throw new Error(`compaction recovery journal is corrupt; preserved for repair: ${record.file}`);
|
|
2312
|
+
if (record.atomic && record.phase !== 'committed' && record.atomic.restored !== true && !this.transactionAdapter) {
|
|
2313
|
+
throw new Error(`pending compaction ${record.compactionId} has an unrecovered atomic backup; restart DSH with a compatible writer or inspect ${record.file}`);
|
|
2314
|
+
}
|
|
2315
|
+
const events = Array.from({ length: session.seq }, (_, i) => session.eventAt(i));
|
|
2316
|
+
const start = events.find(e => e?.type === 'compaction/start' && e.data?.compactionId === record.compactionId);
|
|
2317
|
+
const end = events.find(e => e?.type === 'compaction/end' && e.data?.compactionId === record.compactionId);
|
|
2318
|
+
if (!start) {
|
|
2319
|
+
const state = inspectCompactionEntryState(session);
|
|
2320
|
+
// No destructive operation can precede its start event. A fully replayed
|
|
2321
|
+
// prefix ending before the recorded start is an interrupted pre-publication.
|
|
2322
|
+
if (record.phase === 'started' && Number.isSafeInteger(record.startEventSeq) && (session.firstLiveSeq ?? session.seq) <= record.startEventSeq && state.unmatchedCompactionStart === null) {
|
|
2323
|
+
await this._flushSession(session); await this.journal.clear(session.id, record.compactionId); continue;
|
|
2324
|
+
}
|
|
2325
|
+
throw new Error(`pending compaction ${record.compactionId} is absent from the loaded log; recovery requires the complete session`);
|
|
2326
|
+
}
|
|
2327
|
+
if (record.phase !== 'committed' && typeof session.restoreCompactionSurface !== 'function') {
|
|
2328
|
+
const replacement = events.find(event => event?.type === 'user/message'
|
|
2329
|
+
&& event.data?.source?.kind === 'compact-checkpoint'
|
|
2330
|
+
&& event.data.source.compactionId === record.compactionId
|
|
2331
|
+
&& event.surfaceOp && event.surfaceOp !== 'append');
|
|
2332
|
+
if (replacement) {
|
|
2333
|
+
this._recoveryBlocked.add(this._keyOf(session));
|
|
2334
|
+
if (!end) {
|
|
2335
|
+
const open = inspectCompactionEntryState(session).unmatchedCompactionStart;
|
|
2336
|
+
if (open === null || session.eventAt(open)?.data?.compactionId !== record.compactionId) throw new Error('pending journal does not match the active compaction; refusing to close another transaction');
|
|
2337
|
+
this._appendSync(session, 'compaction/end', { ...start.data, error: 'Compaction failed after replacement; lifecycle closed only. Original surface is NOT restored; recovery is required.' });
|
|
2338
|
+
await this._flushSession(session);
|
|
2339
|
+
}
|
|
2340
|
+
const reason = record.failureKind ?? failureKind;
|
|
2341
|
+
if (record.failureKind === undefined) await this.journal.record({ ...record, sessionId: session.id, failureKind: reason });
|
|
2342
|
+
if (reason !== 'cancelled' && Number.isSafeInteger(record.failuresBefore)) {
|
|
2343
|
+
this.sessionState.update(session.id, current => {
|
|
2344
|
+
const failures = Math.max(current.failures, record.failuresBefore + 1);
|
|
2345
|
+
return { ...current, failures, stopped: failures >= this.config.failureThreshold };
|
|
2346
|
+
});
|
|
2347
|
+
this._restoreState(session);
|
|
2348
|
+
}
|
|
2349
|
+
const error = new Error(`compaction ${record.compactionId} changed the surface without a durable success decision; this host has no supported restoration API. Original events and recovery journal are retained; further model requests are blocked.`);
|
|
2350
|
+
error.code = 'RECOVERY_REQUIRED'; error.compactionStage = 'commit';
|
|
2351
|
+
error.compactionId = record.compactionId; error.replacementSeq = replacement.seq;
|
|
2352
|
+
throw error;
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
if (record.phase !== 'committed' && typeof session.restoreCompactionSurface === 'function') {
|
|
2356
|
+
const rolledBack = session.restoreCompactionSurface(record.compactionId, record.failureKind ?? failureKind);
|
|
2357
|
+
if (rolledBack) {
|
|
2358
|
+
restored++;
|
|
2359
|
+
if (rolledBack.data?.reason !== 'cancelled' && Number.isSafeInteger(record.failuresBefore)) {
|
|
2360
|
+
this.sessionState.update(session.id, current => {
|
|
2361
|
+
const failures = Math.max(current.failures, record.failuresBefore + 1);
|
|
2362
|
+
return { ...current, failures, stopped: failures >= this.config.failureThreshold };
|
|
2363
|
+
});
|
|
2364
|
+
this._restoreState(session);
|
|
2365
|
+
}
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
if (!end) {
|
|
2369
|
+
const state = inspectCompactionEntryState(session);
|
|
2370
|
+
const open = state.unmatchedCompactionStart;
|
|
2371
|
+
if (open === null || session.eventAt(open)?.data?.compactionId !== record.compactionId) throw new Error('pending journal does not match the active compaction; refusing to close another transaction');
|
|
2372
|
+
this._appendSync(session, 'compaction/end', { ...start.data, error: `recovered interrupted compaction (${record.phase}); surface restoration ${typeof session.restoreCompactionSurface === 'function' ? 'checked through host protocol' : 'unavailable on this host'}` });
|
|
2373
|
+
recovered++;
|
|
2374
|
+
}
|
|
2375
|
+
if (typeof this.ctx.sessions?.flush !== 'function') throw new Error('cannot clear recovery journal without a durability checkpoint');
|
|
2376
|
+
await this._flushSession(session);
|
|
2377
|
+
await this.journal.clear(session.id, record.compactionId);
|
|
2378
|
+
}
|
|
2379
|
+
settleContinuation(this.sessionState, session);
|
|
2380
|
+
this._recoveryBlocked.delete(this._keyOf(session));
|
|
2381
|
+
return { recovered, restored };
|
|
2382
|
+
} finally { if (!owned) activeTransactions.delete(key); }
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
_restoreState(session) {
|
|
2386
|
+
if (!session?.id) return;
|
|
2387
|
+
let state = this.sessionState.read(session.id);
|
|
2388
|
+
const key = this._keyOf(session);
|
|
2389
|
+
if(!this._recoveryBlocked.has(key)&&sourceBreakerRecoverable(session,state)){
|
|
2390
|
+
const expectedFailures=state.failures;
|
|
2391
|
+
state=this.sessionState.update(session.id,current=>current.stopped&¤t.failures===expectedFailures&&!current.sourceFragmentRecovery
|
|
2392
|
+
? {...current,failures:0,stopped:false,sourceFragmentRecovery:{version:2,previousFailures:expectedFailures,at:new Date().toISOString()}}
|
|
2393
|
+
: current);
|
|
2394
|
+
if(!state.stopped)this.log('info','Recovered the legacy memory-source circuit breaker after verifying every surviving source; original conversation unchanged.');
|
|
2395
|
+
}
|
|
2396
|
+
this._failures.set(key, state.failures);
|
|
2397
|
+
if (state.stopped) this._stopped.set(key, true);
|
|
2398
|
+
else this._stopped.delete(key);
|
|
2399
|
+
return state;
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
async _pruneToolResults(session) {
|
|
2403
|
+
try {
|
|
2404
|
+
const pruner = typeof this.ctx.get === 'function' ? this.ctx.get('toolResultPruner') : undefined;
|
|
2405
|
+
if (pruner && typeof pruner.pruneSession === 'function') {
|
|
2406
|
+
await pruner.pruneSession(session);
|
|
2407
|
+
}
|
|
2408
|
+
} catch (error) {
|
|
2409
|
+
this.log('warn', `tool-result pruning failed: ${error?.message ?? error}`);
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
/**
|
|
2414
|
+
* 写一条会话事件。
|
|
2415
|
+
*
|
|
2416
|
+
* 这个函数**不能**是 `async`:`session.append` 是同步 API(返回深冻结的
|
|
2417
|
+
* 事件对象),而提交段依赖"三个事件之间不让出事件循环"。声明成 `async`
|
|
2418
|
+
* 会让返回值无条件变成 Promise,`_appendSync` 的守卫就会把每一次正常提交
|
|
2419
|
+
* 都判成误用。需要等待的调用点直接 `await this._append(...)` —— 对非
|
|
2420
|
+
* Promise 的 `await` 是恒等操作。
|
|
2421
|
+
*/
|
|
2422
|
+
_append(session, type, data, options) {
|
|
2423
|
+
if (typeof session.append !== 'function') {
|
|
2424
|
+
throw new Error(`session.append is unavailable; cannot record ${type}`);
|
|
2425
|
+
}
|
|
2426
|
+
return options === undefined ? session.append(type, data) : session.append(type, data, options);
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
/**
|
|
2430
|
+
* 同步版本:提交段要求"三个事件之间不让出事件循环"。
|
|
2431
|
+
* `session.append` 本身就是同步的(返回深冻结事件对象),这里只是把
|
|
2432
|
+
* "返回 Promise" 这种误用挡住 —— 一旦有人在提交段里 `await`,别的写入
|
|
2433
|
+
* 就会插进来把 surface 改掉。
|
|
2434
|
+
*/
|
|
2435
|
+
_appendSync(session, type, data, options) {
|
|
2436
|
+
const result = this._append(session, type, data, options);
|
|
2437
|
+
if (result && typeof result.then === 'function') {
|
|
2438
|
+
throw new Error(
|
|
2439
|
+
`session.append returned a promise for ${type}; the commit段 requires synchronous appends`,
|
|
2440
|
+
);
|
|
2441
|
+
}
|
|
2442
|
+
return result;
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
_warnHostFallbackOnce(reason) {
|
|
2446
|
+
const key = String(reason || 'unknown');
|
|
2447
|
+
if (this._hostFallbackWarned.has(key)) return;
|
|
2448
|
+
this._hostFallbackWarned.add(key);
|
|
2449
|
+
this.log('warn', `host internal fingerprint changed (${key}); falling back to public Session.append replace while keeping pre-replace archive and flush guards`);
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
async _assertNoUnrecoveredAtomicJournal(session) {
|
|
2453
|
+
const records = await this.journal?.pending?.(session.id);
|
|
2454
|
+
if (!Array.isArray(records)) return;
|
|
2455
|
+
for (const record of records) {
|
|
2456
|
+
if (record.corrupt || (record.atomic && record.phase !== 'committed' && record.atomic.restored !== true)) {
|
|
2457
|
+
const error = new Error('compaction recovery journal has an unrecovered atomic backup; refusing fallback modification');
|
|
2458
|
+
error.code = 'RECOVERY_REQUIRED';
|
|
2459
|
+
throw error;
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
log(level, message) {
|
|
2465
|
+
const logger = this.ctx.logger;
|
|
2466
|
+
const text = `[compaction-weave] ${message}`;
|
|
2467
|
+
if (logger && typeof logger[level] === 'function') logger[level](text);
|
|
2468
|
+
else if (logger && typeof logger.info === 'function') logger.info(text);
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
|
|
2472
|
+
function findAgentForSession(engine, session) {
|
|
2473
|
+
return engine._agentBySession?.get(session);
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
function describeResult(result) {
|
|
2477
|
+
const count = Array.isArray(result?.shadowedSeqs) ? result.shadowedSeqs.length : 0;
|
|
2478
|
+
return `${result?.compactionId ?? '?'} (${count} seq(s), ${result?.provider ?? '?'}/${result?.model ?? '?'})`;
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
/** Canonical UTF-8 message payload, excluding source/log-only metadata; not an exact provider tokenizer. */
|
|
2482
|
+
export function canonicalMessageBytes(messages) {
|
|
2483
|
+
const projected = messages.map(message => ({ role: message.role, content: message.content, ...(message.toolCallId === undefined ? {} : { toolCallId: message.toolCallId }) }));
|
|
2484
|
+
return Buffer.byteLength(JSON.stringify(projected), 'utf8');
|
|
2485
|
+
}
|