dsh-live-compact 1.1.7 → 1.1.8
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 +8 -0
- package/README.md +11 -8
- package/index.js +177 -19
- package/package.json +1 -1
- package/plan.js +32 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# 版本变更
|
|
2
2
|
|
|
3
|
+
## 1.1.8
|
|
4
|
+
|
|
5
|
+
- **DSH 轮末兼容**:把原生 `turn/end` 的 `max-tokens` 也视为已收尾边界;即使下一轮已经开始,已发布的上一轮边界仍会进入维护。
|
|
6
|
+
- **真实小切恢复**:补上 `turn/end` 到维护闸的桥接、同会话维护任务的 `pre-step` 等待,并在 agent 被 DSH 销毁时忽略 teardown 取消,让已经开始的本地小切完成;用户取消、宿主取消和 30 秒超时仍会中止。
|
|
7
|
+
- **planner 边界加固**:云端跨题回退只允许跨完整校验的插件通知;真实 user 或来源不完整的通知保持硬边界,避免恶意/损坏来源被误切入 spine 卡片。
|
|
8
|
+
- **回归证据**:完整 gate 573/573、Cordis 4.1 兼容门禁 22/22;GLM 5.3 Flash 单题 post-fix WebUniverse 任务 30/30,真实提交后水位从 80,778 降到 78,675;DS V4.1 Flash post-fix acceptance 三题为 30/30、18/24、22/22。
|
|
9
|
+
- #49 的轮末维护冻结路径已覆盖受控 headless 复现;Windows 原现场仍需用户环境复测,不在本条中宣称已完成现场验收。
|
|
10
|
+
|
|
3
11
|
## 1.1.7
|
|
4
12
|
|
|
5
13
|
- **billion-context 兼容**:保留 1.1.6 的让位行为;billion-context 接管官方压缩时 Live Compact 不再同时改写会话。
|
package/README.md
CHANGED
|
@@ -9,13 +9,16 @@
|
|
|
9
9
|
完整数据、原始口径和限制见 [docs/benchmarks.md](docs/benchmarks.md),结构化数字见 [docs/benchmark-results.json](docs/benchmark-results.json)。
|
|
10
10
|
|
|
11
11
|
- **1.0.5 失忆对比**:同一真实 DeepSeek 会话,15 道回忆题两边都是 15/15;Live Compact 的日志中段细节两次都是 3/3,原版一次 1/3、一次 3/3。
|
|
12
|
-
- **历史 DSH 长链路**:早期 150 道短题和 20 道 WebUniverse 连续任务都保留了原版/Live Compact 对照表;它们是历史实测,不是 1.1.
|
|
12
|
+
- **历史 DSH 长链路**:早期 150 道短题和 20 道 WebUniverse 连续任务都保留了原版/Live Compact 对照表;它们是历史实测,不是 1.1.8 的新验收。
|
|
13
13
|
- **OpenCode 移植参考**:独立项目 [opencode-live-compact](https://github.com/Haoming9527/opencode-live-compact) 把插件移植到 OpenCode,和 OpenCode 自带的 autocompact 做了对照:在 OpenCode 2.2.6 + GPT-6 Luna 上跑 Aider 150 和 Web-Bench React 20,任务分别 150/150、20/20 且最终浏览器测试 59/59;估算费用分别低 14.3% 和 42.1%。这是 OpenCode 项目的结果,用来说明移植后的可复核测评方法,不冒充 DSH 运行结果。
|
|
14
14
|
|
|
15
15
|

|
|
16
16
|
|
|
17
17
|

|
|
18
18
|
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+
|
|
19
22
|
## 云端和本地怎么用
|
|
20
23
|
|
|
21
24
|
- **用 DeepSeek、GLM 这类云端 API**,按下面的命令安装即可。先看答案是否正确,再一起看缓存命中、未命中输入和实际费用。命中率的百分比不是费用本身,输入变少时要连同 token 数一起比较。
|
|
@@ -28,13 +31,13 @@
|
|
|
28
31
|
headless:
|
|
29
32
|
|
|
30
33
|
```sh
|
|
31
|
-
dsh plugin --profile headless add dsh-live-compact@1.1.
|
|
34
|
+
dsh plugin --profile headless add dsh-live-compact@1.1.8
|
|
32
35
|
```
|
|
33
36
|
|
|
34
37
|
网页端:
|
|
35
38
|
|
|
36
39
|
```sh
|
|
37
|
-
dsh plugin --profile web add dsh-live-compact@1.1.
|
|
40
|
+
dsh plugin --profile web add dsh-live-compact@1.1.8
|
|
38
41
|
```
|
|
39
42
|
|
|
40
43
|
想先装到本机看版本:
|
|
@@ -186,8 +189,8 @@ Windows 会重新设置备份访问权限,失败时停止写备份并保留原
|
|
|
186
189
|
从 npm 装。headless 和网页是两个 profile,要各装一次。桌面端是另一个 profile,普通 `dsh plugin --profile desktop` 会被拒绝。
|
|
187
190
|
|
|
188
191
|
```sh
|
|
189
|
-
dsh plugin --profile headless add dsh-live-compact@1.1.
|
|
190
|
-
dsh plugin --profile web add dsh-live-compact@1.1.
|
|
192
|
+
dsh plugin --profile headless add dsh-live-compact@1.1.8
|
|
193
|
+
dsh plugin --profile web add dsh-live-compact@1.1.8
|
|
191
194
|
```
|
|
192
195
|
|
|
193
196
|
想先装到本机看版本:
|
|
@@ -200,9 +203,9 @@ dsh-live-compact --version
|
|
|
200
203
|
上不了 npm 时的镜像安装命令:
|
|
201
204
|
|
|
202
205
|
```sh
|
|
203
|
-
dsh plugin --profile headless add https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.
|
|
204
|
-
dsh plugin --profile web add https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.
|
|
205
|
-
npm install -g --force "https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.
|
|
206
|
+
dsh plugin --profile headless add https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.8.tgz
|
|
207
|
+
dsh plugin --profile web add https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.8.tgz
|
|
208
|
+
npm install -g --force "https://download.merkyorlynn.com/downloads/plugins/dsh-live-compact-1.1.8.tgz"
|
|
206
209
|
dsh-live-compact --version
|
|
207
210
|
```
|
|
208
211
|
|
package/index.js
CHANGED
|
@@ -228,19 +228,58 @@ function isCheckpoint(session, seq) {
|
|
|
228
228
|
return text === MARK || text.startsWith(`${MARK}\n`) || text.startsWith(`${MARK}\r\n`);
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
function isCuttableTurnEnd(event) {
|
|
232
|
+
const reason = event?.data?.reason;
|
|
233
|
+
return reason?.kind === "completed" ||
|
|
234
|
+
reason?.kind === "max-tokens" ||
|
|
235
|
+
(reason?.kind === "other" && reason.label === "max-tokens");
|
|
236
|
+
}
|
|
237
|
+
|
|
231
238
|
function latestTurnCompleted(events) {
|
|
232
239
|
let lastStart = -1;
|
|
233
240
|
let lastEnd = -1;
|
|
234
|
-
let
|
|
241
|
+
let completed = false;
|
|
235
242
|
for (let index = 0; index < (events || []).length; index += 1) {
|
|
236
243
|
const event = events[index];
|
|
237
244
|
if (event?.type === "turn/start") lastStart = index;
|
|
238
245
|
if (event?.type === "turn/end") {
|
|
239
246
|
lastEnd = index;
|
|
240
|
-
|
|
247
|
+
completed = isCuttableTurnEnd(event);
|
|
241
248
|
}
|
|
242
249
|
}
|
|
243
|
-
return lastEnd > lastStart &&
|
|
250
|
+
return lastEnd > lastStart && completed;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function latestCompletedTurnEnd(events) {
|
|
254
|
+
let found = null;
|
|
255
|
+
for (const event of events || []) {
|
|
256
|
+
if (event?.type === "turn/end" && isCuttableTurnEnd(event)) found = event;
|
|
257
|
+
}
|
|
258
|
+
return found;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function maintenanceOperationSignal(agentSignal, timeoutSignal) {
|
|
262
|
+
const controller = new AbortController();
|
|
263
|
+
const forwardAgent = () => {
|
|
264
|
+
const reason = agentSignal?.reason;
|
|
265
|
+
// DSH disposes a completed CLI agent while its last maintenance callback
|
|
266
|
+
// is still settling. Disposal is lifecycle teardown, not a user cancel;
|
|
267
|
+
// let the bounded local operation finish in that case.
|
|
268
|
+
if (reason?.kind === "disposed") return;
|
|
269
|
+
if (!controller.signal.aborted) controller.abort(reason);
|
|
270
|
+
};
|
|
271
|
+
const forwardTimeout = () => {
|
|
272
|
+
if (!controller.signal.aborted) controller.abort(timeoutSignal?.reason);
|
|
273
|
+
};
|
|
274
|
+
if (agentSignal) {
|
|
275
|
+
agentSignal.addEventListener("abort", forwardAgent, { once: true });
|
|
276
|
+
if (agentSignal.aborted) forwardAgent();
|
|
277
|
+
}
|
|
278
|
+
if (timeoutSignal) {
|
|
279
|
+
timeoutSignal.addEventListener("abort", forwardTimeout, { once: true });
|
|
280
|
+
if (timeoutSignal.aborted) forwardTimeout();
|
|
281
|
+
}
|
|
282
|
+
return controller.signal;
|
|
244
283
|
}
|
|
245
284
|
|
|
246
285
|
function spineCheckpointSource(compactionId) {
|
|
@@ -259,13 +298,17 @@ function eventRole(session, seq) {
|
|
|
259
298
|
|
|
260
299
|
// progress-rules 0.2.1's pre-step contract, not a general notice permission.
|
|
261
300
|
// Session source metadata does not authenticate producer identity. Unknown/extra metadata
|
|
262
|
-
// stays a boundary; neither body text nor the raw role is used as a substitute.
|
|
301
|
+
// stays a boundary; neither body text nor the raw role is used as a substitute. A real
|
|
302
|
+
// user event that is not the complete contract is therefore a hard boundary for the
|
|
303
|
+
// cloud fallback path; only a fully validated plugin notice may be crossed there.
|
|
263
304
|
function classifyUserEvent(session, seq) {
|
|
264
305
|
const event = session.eventAt(seq);
|
|
265
306
|
const data = event?.data;
|
|
266
307
|
const source = data?.source;
|
|
267
308
|
const content = data?.content;
|
|
268
|
-
const
|
|
309
|
+
const progressNotice = event?.type === "user/message" && !isCheckpoint(session, seq)
|
|
310
|
+
&& source?.kind === "progress-rules" && source.form === "notice";
|
|
311
|
+
const pluginNotice = progressNotice
|
|
269
312
|
&& data?.role === "user"
|
|
270
313
|
&& typeof data.id === "string" && data.id.length > 0
|
|
271
314
|
&& !Object.hasOwn(event, "source")
|
|
@@ -276,7 +319,11 @@ function classifyUserEvent(session, seq) {
|
|
|
276
319
|
&& Array.isArray(content) && content.length === 1
|
|
277
320
|
&& content[0]?.type === "text" && typeof content[0].text === "string"
|
|
278
321
|
&& Object.keys(content[0]).length === 2;
|
|
279
|
-
return {
|
|
322
|
+
return {
|
|
323
|
+
pluginNotice,
|
|
324
|
+
untrustedNotice: event?.type === "user/message" && !pluginNotice,
|
|
325
|
+
isGenuineUserBoundary: event?.type === "user/message" && !pluginNotice,
|
|
326
|
+
};
|
|
280
327
|
}
|
|
281
328
|
|
|
282
329
|
function cutOk(fn, session, seq) {
|
|
@@ -629,6 +676,10 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
629
676
|
try {
|
|
630
677
|
if (this.standDown?.()) return;
|
|
631
678
|
this.noteEvent(session, event);
|
|
679
|
+
if (event?.type === "turn/end" && isCuttableTurnEnd(event)) {
|
|
680
|
+
const state = this.sessionState.get(sessionKey({ session }));
|
|
681
|
+
if (state?.liveAgent) this.scheduleTurnEndMaintenance(state.liveAgent, event.seq, event);
|
|
682
|
+
}
|
|
632
683
|
if (event?.type !== "tool/result") return;
|
|
633
684
|
const id = event.data?.message?.toolCallId;
|
|
634
685
|
if (!id) return;
|
|
@@ -666,22 +717,28 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
666
717
|
noteShadowWarning(ctx.logger, error);
|
|
667
718
|
}
|
|
668
719
|
});
|
|
720
|
+
ctx.on("agent/created", ({ agent }) => {
|
|
721
|
+
if (this.standDown?.()) return;
|
|
722
|
+
this.stateFor(agent);
|
|
723
|
+
});
|
|
669
724
|
ctx.on("agent/status", ({ agent, status }) => {
|
|
670
725
|
if (status !== "idle") return;
|
|
671
726
|
if (this.standDown?.()) return;
|
|
672
727
|
const state = this.stateFor(agent);
|
|
673
|
-
if (state.
|
|
728
|
+
if (state.turnEndScheduled) return;
|
|
674
729
|
const events = agent.session.snapshotEvents();
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
730
|
+
if (state.turnEndSeq <= state.lastMaintenanceTurnEndSeq && latestTurnCompleted(events)) {
|
|
731
|
+
const completed = latestCompletedTurnEnd(events);
|
|
732
|
+
if (completed && Number.isInteger(completed.seq)) {
|
|
733
|
+
state.turnEndSeq = completed.seq;
|
|
734
|
+
state.turnEndEvent = completed;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
this.startTurnMaintenance(agent, events);
|
|
738
|
+
});
|
|
739
|
+
ctx.on("agent/pre-step", async ({ agent, signal }, next) => {
|
|
740
|
+
await this.waitForMaintenance(agent, signal);
|
|
741
|
+
return typeof next === "function" ? next() : undefined;
|
|
685
742
|
});
|
|
686
743
|
} catch (error) {
|
|
687
744
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -711,13 +768,14 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
711
768
|
const key = sessionKey(agent);
|
|
712
769
|
let state = this.sessionState.get(key);
|
|
713
770
|
if (state) {
|
|
771
|
+
state.liveAgent = agent;
|
|
714
772
|
this.sessionState.delete(key);
|
|
715
773
|
this.sessionState.set(key, state);
|
|
716
774
|
return state;
|
|
717
775
|
}
|
|
718
776
|
state = {
|
|
719
777
|
key,
|
|
720
|
-
liveAgent:
|
|
778
|
+
liveAgent: agent,
|
|
721
779
|
warming: false,
|
|
722
780
|
localOnly: false,
|
|
723
781
|
approvedCard: null,
|
|
@@ -725,6 +783,11 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
725
783
|
lastPrefixTokens: 0,
|
|
726
784
|
turnBase: null,
|
|
727
785
|
maxTurnGrowth: 0,
|
|
786
|
+
maintenanceJob: null,
|
|
787
|
+
turnEndScheduled: false,
|
|
788
|
+
turnEndPending: false,
|
|
789
|
+
turnEndSeq: -1,
|
|
790
|
+
lastMaintenanceTurnEndSeq: -1,
|
|
728
791
|
};
|
|
729
792
|
this.sessionState.set(key, state);
|
|
730
793
|
if (this.sessionState.size > MAX_TRACKED_SESSIONS) {
|
|
@@ -737,6 +800,95 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
737
800
|
return state;
|
|
738
801
|
}
|
|
739
802
|
|
|
803
|
+
startTurnMaintenance(agent, events = agent.session.snapshotEvents()) {
|
|
804
|
+
if (this.standDown?.()) return;
|
|
805
|
+
const state = this.stateFor(agent);
|
|
806
|
+
const boundaryCompleted = Number.isInteger(state.turnEndSeq) && state.turnEndSeq >= 0;
|
|
807
|
+
if (!latestTurnCompleted(events) && !boundaryCompleted) return;
|
|
808
|
+
if (state.warming) {
|
|
809
|
+
// A later idle/turn-end can arrive while capture catch-up or a cut is
|
|
810
|
+
// still running. Preserve it unconditionally; comparing only seqs drops
|
|
811
|
+
// same-turn lifecycle notifications in headless runs.
|
|
812
|
+
state.turnEndPending = true;
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if (state.turnEndSeq <= state.lastMaintenanceTurnEndSeq) return;
|
|
816
|
+
if (state.turnEndSeq > state.lastMaintenanceTurnEndSeq) {
|
|
817
|
+
state.lastMaintenanceTurnEndSeq = state.turnEndSeq;
|
|
818
|
+
}
|
|
819
|
+
state.warming = true;
|
|
820
|
+
let job;
|
|
821
|
+
job = this.finishTurn(agent, events, { deferCapture: true })
|
|
822
|
+
.catch((error) => noteShadowWarning(this.ctx?.logger, error))
|
|
823
|
+
.finally(() => {
|
|
824
|
+
if (!state.pendingMaintenance) state.warming = false;
|
|
825
|
+
if (state.maintenanceJob === job) state.maintenanceJob = null;
|
|
826
|
+
this.pendingCuts.delete(job);
|
|
827
|
+
if (state.turnEndPending && !state.warming) {
|
|
828
|
+
state.turnEndPending = false;
|
|
829
|
+
this.scheduleTurnEndMaintenance(agent, state.turnEndSeq);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
state.maintenanceJob = job;
|
|
833
|
+
this.pendingCuts.add(job);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
scheduleTurnEndMaintenance(agent, seq, boundaryEvent = null) {
|
|
837
|
+
if (this.standDown?.()) return;
|
|
838
|
+
const state = this.stateFor(agent);
|
|
839
|
+
if (Number.isInteger(seq)) {
|
|
840
|
+
if (seq >= state.turnEndSeq && boundaryEvent) state.turnEndEvent = boundaryEvent;
|
|
841
|
+
state.turnEndSeq = Math.max(state.turnEndSeq, seq);
|
|
842
|
+
}
|
|
843
|
+
if (state.turnEndScheduled) return;
|
|
844
|
+
state.turnEndScheduled = true;
|
|
845
|
+
scheduleImmediate(async () => {
|
|
846
|
+
state.turnEndScheduled = false;
|
|
847
|
+
if (this.standDown?.()) return;
|
|
848
|
+
// `turn/end` is the host's persisted turn boundary. Some CLI agents
|
|
849
|
+
// expose `whenIdle()` but never resolve it after disposal, and DSH does
|
|
850
|
+
// not emit a matching `agent/status=idle`. Waiting here would hold the
|
|
851
|
+
// de-duplication flag forever and suppress every later turn-end pass.
|
|
852
|
+
// The pre-step barrier still waits for an active maintenance job before
|
|
853
|
+
// the next provider request, so no idle wait is needed on this path.
|
|
854
|
+
const snapshot = agent.session.snapshotEvents();
|
|
855
|
+
const boundary = state.turnEndEvent;
|
|
856
|
+
const events = boundary && !snapshot.some((event) => event?.seq === boundary.seq)
|
|
857
|
+
? [...snapshot, boundary]
|
|
858
|
+
: snapshot;
|
|
859
|
+
const boundaryCompleted = Number.isInteger(state.turnEndSeq) && state.turnEndSeq >= 0;
|
|
860
|
+
if (!latestTurnCompleted(events) && !boundaryCompleted) return;
|
|
861
|
+
if (state.warming) {
|
|
862
|
+
if (state.turnEndSeq > state.lastMaintenanceTurnEndSeq) state.turnEndPending = true;
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
this.startTurnMaintenance(agent, events);
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
async waitForMaintenance(agent, signal) {
|
|
870
|
+
const state = this.stateFor(agent);
|
|
871
|
+
while (!signal?.aborted) {
|
|
872
|
+
const jobs = [];
|
|
873
|
+
if (state.maintenanceJob) jobs.push(state.maintenanceJob);
|
|
874
|
+
const catchup = this.pendingCatchups.get(state.key);
|
|
875
|
+
if (catchup?.promise) jobs.push(catchup.promise);
|
|
876
|
+
if (!jobs.length) break;
|
|
877
|
+
const settled = Promise.allSettled(jobs);
|
|
878
|
+
if (!signal) {
|
|
879
|
+
await settled;
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
let abort;
|
|
883
|
+
const aborted = new Promise((resolve) => {
|
|
884
|
+
abort = () => resolve();
|
|
885
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
886
|
+
});
|
|
887
|
+
await Promise.race([settled, aborted]);
|
|
888
|
+
signal.removeEventListener("abort", abort);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
740
892
|
chunkPlan(session, chunk) {
|
|
741
893
|
const messages = [];
|
|
742
894
|
let on = false;
|
|
@@ -933,8 +1085,10 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
933
1085
|
.finally(() => {
|
|
934
1086
|
state.pendingMaintenance = false;
|
|
935
1087
|
state.warming = false;
|
|
1088
|
+
if (state.maintenanceJob === job) state.maintenanceJob = null;
|
|
936
1089
|
this.pendingCuts.delete(job);
|
|
937
1090
|
});
|
|
1091
|
+
state.maintenanceJob = job;
|
|
938
1092
|
this.pendingCuts.add(job);
|
|
939
1093
|
});
|
|
940
1094
|
}
|
|
@@ -978,7 +1132,11 @@ export default class SpineCompactionEngine extends BasicCompactionEngine {
|
|
|
978
1132
|
try {
|
|
979
1133
|
await agent.runMaintenance(async (agentSignal) => {
|
|
980
1134
|
if (this.standDown?.()) return;
|
|
981
|
-
|
|
1135
|
+
// DSH disposes a completed CLI agent by aborting maintenance with the
|
|
1136
|
+
// lifecycle marker `{ kind: "disposed" }`. A local cut has already
|
|
1137
|
+
// started at that point, so let it finish under the bounded timeout;
|
|
1138
|
+
// user/host cancellation reasons still abort the operation.
|
|
1139
|
+
const op = maintenanceOperationSignal(agentSignal, signal);
|
|
982
1140
|
let measurement = this.ctx.tokenMeter.measure(agent.session);
|
|
983
1141
|
const settleTurnBaseline = () => {
|
|
984
1142
|
state.turnBase = measurement.totalTokens;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-live-compact",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "In-place newest-turn compaction for DeepSeek Harness. Compress while the task runs and keep the prefix.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "MerkyorLynn <268568828+MerkyorLynn@users.noreply.github.com>",
|
package/plan.js
CHANGED
|
@@ -867,6 +867,7 @@ function expandBack(items, end, stopAt, { crossUser, chunkMax }) {
|
|
|
867
867
|
while (start > stopAt) {
|
|
868
868
|
const prev = start - 1;
|
|
869
869
|
if (items[prev].checkpoint) break;
|
|
870
|
+
if (crossUser && items[prev].untrustedNotice) break;
|
|
870
871
|
if (!crossUser && isUserBoundary(items[prev])) break;
|
|
871
872
|
if (tokens + (items[prev].tokens || 0) > chunkMax) break;
|
|
872
873
|
start = prev;
|
|
@@ -904,7 +905,7 @@ export function selectCutIndexes(items, {
|
|
|
904
905
|
const lastUser = items.reduce((found, item, index) => (isUserBoundary(item) ? index : found), -1);
|
|
905
906
|
const firstUser = items.findIndex(isUserBoundary);
|
|
906
907
|
const floor = firstUser >= 0 ? firstUser + 1 : 0;
|
|
907
|
-
|
|
908
|
+
let end = walkOffTail(items, limit);
|
|
908
909
|
if (end < lowerBound) return null;
|
|
909
910
|
// 切点右边还有卡片,说明这一刀在已缓存前缀里。不切。
|
|
910
911
|
// A checkpoint to the right means this span is inside the cached prefix. Do not cut.
|
|
@@ -927,12 +928,36 @@ export function selectCutIndexes(items, {
|
|
|
927
928
|
}
|
|
928
929
|
if ((items[end].tokens || 0) > pointerMax && items[end].type === "tool") return { start: end, end, pointerOnly: true, gain: 0 };
|
|
929
930
|
const phase = expandBack(items, end, lowerBound, { crossUser: false, chunkMax });
|
|
930
|
-
//
|
|
931
|
-
//
|
|
932
|
-
if (phase.gain
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
931
|
+
// First prefer the newest phase after the latest user boundary.
|
|
932
|
+
// Prefer the newest phase after the latest user boundary.
|
|
933
|
+
if (phase.gain >= chunkMin) {
|
|
934
|
+
if (phase.tokens <= chunkMax && balanced(items, phase.start, phase.end)
|
|
935
|
+
&& !(lastUser >= phase.start && lastUser <= phase.end)) return phase;
|
|
936
|
+
return null;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
// Cloud sessions may have several independent user tasks. If the newest
|
|
940
|
+
// phase is too small, retreat before the latest user and compact an older
|
|
941
|
+
// tool span. Keep the latest user itself out of the span; older user
|
|
942
|
+
// messages remain as JSON lines in the card so their constraints survive.
|
|
943
|
+
// Local sessions deliberately keep the old strict no-cross-user behavior.
|
|
944
|
+
end = retreatBefore(items, end, lastUser);
|
|
945
|
+
if (end < lowerBound || end < floor) return pointer;
|
|
946
|
+
// Retreating before the latest user can move the candidate to the left of
|
|
947
|
+
// a newer checkpoint. That span is already in the cached prefix and is not
|
|
948
|
+
// a legal fallback cut.
|
|
949
|
+
if (cutCrossesNewerCheckpoint(items, end)) return pointer;
|
|
950
|
+
if ((items[end].tokens || 0) > pointerMax && items[end].type === "tool") {
|
|
951
|
+
return { start: end, end, pointerOnly: true, gain: 0 };
|
|
952
|
+
}
|
|
953
|
+
const crossUserFloor = Math.max(lowerBound, floor);
|
|
954
|
+
const extended = expandBack(items, end, crossUserFloor - 1, { crossUser: true, chunkMax });
|
|
955
|
+
if (extended.start < crossUserFloor) extended.start = crossUserFloor;
|
|
956
|
+
extended.gain = nonUserGain(items, extended.start, extended.end);
|
|
957
|
+
extended.tokens = spanTokens(items, extended.start, extended.end);
|
|
958
|
+
if (extended.tokens > chunkMax || extended.gain < chunkMin || !balanced(items, extended.start, extended.end)) return pointer;
|
|
959
|
+
if (lastUser >= extended.start && lastUser <= extended.end) return pointer;
|
|
960
|
+
return extended;
|
|
936
961
|
}
|
|
937
962
|
|
|
938
963
|
// pointerOnly 扩到 tool-call 之后,这一段仍不能包含最后一条 user 或 checkpoint。
|