@logictan/dsh-verbatim-compaction 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -3
- package/lib/index.js +51 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
- 选 FULL:回收发生在宿主自己的 DELETE 里,不受 busy_timeout=0 影响
|
|
30
30
|
## Overturned
|
|
31
31
|
- 先前认为必须显式执行 VACUUM 才能回收空间
|
|
32
|
-
tools: bash×280 ssh_exec×44 web_search×6 (408830 bytes)
|
|
33
32
|
errors: 1: Error: 命令超过 600 秒未结束,已断开连接
|
|
34
33
|
wrote: /Volumes/LogicExt/Git/BookReader/src/video.js
|
|
35
34
|
```
|
|
@@ -38,13 +37,19 @@
|
|
|
38
37
|
|
|
39
38
|
- **用户原话逐字**,不重写、不改写、不摘要。
|
|
40
39
|
- **助手正文由提取块替代**,不并列保留——并列就是同一份内容写两遍。
|
|
41
|
-
-
|
|
40
|
+
- 工具结果内容丢弃,工具指针行也删除(实测 122 轮会话里它占 1202 token、6% 的上限,却只多给「跑了几次、多少字节」;提取块本来就拿到了每条调用的 `description`,指针行只是 `## Did` 的低信息量复述)。
|
|
42
41
|
- 错误只留首行(100 字符)。
|
|
43
42
|
- `wrote` 记录本轮写过的路径。
|
|
44
43
|
- 推理块从不进入摘要,也不读取。
|
|
45
44
|
|
|
46
45
|
为什么助手正文不再逐字保留:实测 8 个会话、4400 条助手正文、748743 字符,其中 58.2% 是有实质结论的长文、35.8% 是带数据(数字/代码/引用)的短句、6.1% 是纯过程叙述(「现在我来看…」)。而字面重复几乎为零(段落级完全相同 0.1%)。也就是说,重复不在字面,而在**助手正文与提取块说了同一件事**;两者并列保留就是把这 58.2% 写两遍,还通过预算把真正的历史挤出去。
|
|
47
46
|
|
|
47
|
+
## goal 触发的轮次
|
|
48
|
+
|
|
49
|
+
由 goal 续跑的轮次没有用户消息,任务目标由宿主以 `<goal_round>` 注入。原来按「非 user 即注入」一律丢弃,结果一轮 909 字符、7 条任务的目标在摘要里一个字都没有,只剩一个没有任务说明的助手块。
|
|
50
|
+
|
|
51
|
+
现在保留 `Objective: "..."` 那段,丢弃 `Round: N/256` 计数,并且**同一个 objective 只记一次**——宿主每轮重复注入同一份,全留会在长跑里重复几十遍。objective 放在该轮的 user 位:它就是这一轮在服务的请求。
|
|
52
|
+
|
|
48
53
|
## 摘要源是日志,不是被阴影的区间
|
|
49
54
|
|
|
50
55
|
上一份 checkpoint 每次都被一起阴影掉,所以「只摘要被阴影区间」会把上一份摘要承载的全部历史再丢一次。本插件扫描会话日志里 `surfaceOp === 'append'` 的事件,减去本次压缩后仍留在 surface 上的尾部,重建整段历史——不需要任何累积状态,摘要源集合逐次单调增长(122 次压缩实测 0 次非单调)。
|
|
@@ -61,7 +66,9 @@
|
|
|
61
66
|
|
|
62
67
|
`## Asked` 记的是「**助手**向**用户**提了什么、给了哪些选项」。这一节最容易错成「把用户请求改写一遍」——真实一次压缩里,模型把用户那段中文请求翻成英文塞进了 `Asked`,而用户原话就在它上面逐字抄着,同一件事写了两遍。
|
|
63
68
|
|
|
64
|
-
|
|
69
|
+
原因之一在材料标签:原来写 `USER REQUEST:`,模型有理由读成「这一轮问了什么」,现在改成 `USER INSTRUCTION (already in the digest verbatim; never copy it into Asked)`。另一部分靠指令本身——`## Asked` 的定义写明「助手问、用户答」,并注明照抄用户请求是这一节最常见的出错方式。
|
|
70
|
+
|
|
71
|
+
正文为空的轮次(那个真实案例有 96 条助手消息、150 次工具调用却只有 156 字符正文)本来就没有提问可记,`## Asked` 和 `## Did` 一样走 `pruneEmptySections` 整节省略,不需要单独规则。实测 123 条真人消息里 43 条(35%)不超过 12 个字符——`3`、`1`、`同意`、`做`、`继续`——而 46% 是在回答上一轮助手的提问。只逐字保留「3」而不保留它在回答什么问题,等于记下一个决定却没记下决定的是什么。
|
|
65
72
|
|
|
66
73
|
模型用的是会话自己的路由(`session.requestHeader()?.config`),和宿主原生压缩同一个取法,不需要单独配置凭据。
|
|
67
74
|
|
package/lib/index.js
CHANGED
|
@@ -104,7 +104,36 @@ function sourceKind(event) {
|
|
|
104
104
|
*/
|
|
105
105
|
function isInjection(event) {
|
|
106
106
|
const kind = sourceKind(event);
|
|
107
|
-
|
|
107
|
+
if (kind === undefined)
|
|
108
|
+
return false;
|
|
109
|
+
if (kind === 'goal')
|
|
110
|
+
return false;
|
|
111
|
+
return kind !== 'user';
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The objective carried by a goal round, or undefined for any other message.
|
|
115
|
+
*
|
|
116
|
+
* A goal-triggered turn has no user message at all: the host injects a
|
|
117
|
+
* `<goal_round>` carrying the objective it is working toward. Treating that as
|
|
118
|
+
* ordinary injection dropped the only statement of why the turn existed — a real
|
|
119
|
+
* 909-character, 7-item objective appeared nowhere in the digest, leaving an
|
|
120
|
+
* assistant block with no task above it.
|
|
121
|
+
*
|
|
122
|
+
* Only the objective is kept. `Round: 3/256` is a counter, and the objective
|
|
123
|
+
* repeats unchanged on every round, so the caller records it once.
|
|
124
|
+
*
|
|
125
|
+
* @param event - any event; non-goal events yield undefined.
|
|
126
|
+
* @returns the objective text, or undefined.
|
|
127
|
+
*/
|
|
128
|
+
function goalObjective(event) {
|
|
129
|
+
if (sourceKind(event) !== 'goal')
|
|
130
|
+
return undefined;
|
|
131
|
+
const text = textOf(event);
|
|
132
|
+
const match = /Objective:\s*"([\s\S]*?)"/.exec(text);
|
|
133
|
+
// The objective arrives with its newlines already escaped inside the JSON
|
|
134
|
+
// string, so a seven-item list reaches the digest as one unreadable line.
|
|
135
|
+
const objective = (match?.[1] ?? '').replace(/\\n/g, '\n').trim();
|
|
136
|
+
return objective.length > 0 ? objective : undefined;
|
|
108
137
|
}
|
|
109
138
|
/** Whether this user message is a compaction checkpoint written by a backend. */
|
|
110
139
|
function isCheckpoint(event) {
|
|
@@ -351,6 +380,10 @@ export function buildSections(events) {
|
|
|
351
380
|
lastTurn = turn;
|
|
352
381
|
return next;
|
|
353
382
|
};
|
|
383
|
+
// One objective per section, and never twice: the host re-injects the same
|
|
384
|
+
// objective on every round, so recording each copy would repeat it dozens of
|
|
385
|
+
// times across a long autonomous run.
|
|
386
|
+
const recordedObjectives = new Set();
|
|
354
387
|
for (const event of events) {
|
|
355
388
|
if (event.type === 'turn/start') {
|
|
356
389
|
const turn = turnOf(event);
|
|
@@ -358,6 +391,23 @@ export function buildSections(events) {
|
|
|
358
391
|
lastTurn = turn;
|
|
359
392
|
continue;
|
|
360
393
|
}
|
|
394
|
+
const objective = goalObjective(event);
|
|
395
|
+
if (objective !== undefined) {
|
|
396
|
+
if (current === undefined) {
|
|
397
|
+
current = { turns: [] };
|
|
398
|
+
sections.push(current);
|
|
399
|
+
}
|
|
400
|
+
if (recordedObjectives.has(objective))
|
|
401
|
+
continue;
|
|
402
|
+
recordedObjectives.add(objective);
|
|
403
|
+
// A goal round opens a section like a user request does, and its objective
|
|
404
|
+
// takes the user slot: it is the request this turn serves.
|
|
405
|
+
current = { turns: [] };
|
|
406
|
+
sections.push(current);
|
|
407
|
+
currentTurn = openTurn(turnOf(event) ?? lastTurn);
|
|
408
|
+
currentTurn.user.push(objective);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
361
411
|
if (event.type === 'tool/result') {
|
|
362
412
|
if (currentTurn === undefined)
|
|
363
413
|
continue;
|
|
@@ -500,13 +550,6 @@ function renderSection(section, capChars) {
|
|
|
500
550
|
if (body.length > 0)
|
|
501
551
|
lines.push(`### assistant\n${body}`);
|
|
502
552
|
}
|
|
503
|
-
if (turn.tools.size > 0) {
|
|
504
|
-
const names = [...turn.tools.entries()]
|
|
505
|
-
.sort((left, right) => right[1] - left[1])
|
|
506
|
-
.map(([tool, count]) => `${tool}×${count}`)
|
|
507
|
-
.join(' ');
|
|
508
|
-
lines.push(` tools: ${names} (${turn.resultBytes} bytes)`);
|
|
509
|
-
}
|
|
510
553
|
if (turn.errors.length > 0) {
|
|
511
554
|
lines.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
512
555
|
}
|
|
@@ -712,16 +755,9 @@ export function buildExtractionMaterial(turn, prose) {
|
|
|
712
755
|
const hints = collectHints(turn);
|
|
713
756
|
if (userRequest.length === 0 && prose.length === 0 && hints.length === 0)
|
|
714
757
|
return '';
|
|
715
|
-
// Say it explicitly when there is no prose. A turn with 150 tool calls and 156
|
|
716
|
-
// characters of prose gives the model nothing to quote, and a real run filled
|
|
717
|
-
// Asked with a translation of the user request instead of omitting it.
|
|
718
|
-
const asked = prose.length === 0
|
|
719
|
-
? 'ASKED: omit — this turn produced no assistant prose, so it asked the user nothing'
|
|
720
|
-
: 'ASKED: record only what the assistant asked the user';
|
|
721
758
|
return [
|
|
722
759
|
`USER INSTRUCTION (already in the digest verbatim; never copy it into Asked):\n${userRequest.slice(0, 4000) || '(none)'}`,
|
|
723
760
|
`ASSISTANT PROSE:\n${prose.slice(0, 6000) || '(none)'}`,
|
|
724
|
-
asked,
|
|
725
761
|
`TOOL TRACE:\n${hints || '(none)'}`,
|
|
726
762
|
].join('\n\n');
|
|
727
763
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Deterministic compaction backend that keeps conversation prose verbatim in the checkpoint instead of re-summarizing it: user and assistant text survive word for word, tool results collapse to one-line pointers, reasoning is never read, and no model call is made.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|