@logictan/dsh-verbatim-compaction 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -3
- package/lib/index.js +92 -33
- 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 次非单调)。
|
|
@@ -57,7 +62,7 @@
|
|
|
57
62
|
|
|
58
63
|
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description`,失败的调用另记一行首行错误)。模型输出固定的四节:`## Asked` / `## Did` / `## Concluded` / `## Overturned`。
|
|
59
64
|
|
|
60
|
-
空节直接删掉,不留 `(none)`
|
|
65
|
+
空节直接删掉,不留 `(none)` 占位。判据同时接受 `## Asked` 和裸写 `Asked`——实测模型有时不带 `##`,只认 `^#{1,6}` 会让 `Concluded\n- (none)` 原样留在摘要里。实测一份真实提取块里 `## Asked` 和 `## Overturned` 都是 `(none)`,删掉后 312 → 271 字符;按 130 轮算,这些占位合计约 900 token 只是在记录「什么都没发生」。清理在代码里做(`pruneEmptySections`),不只依赖提示词——模型仍写了 `(none)` 也不会白花 token。判据是整节每一行去掉项目符号后都只是 `(none)` / `none` / `n/a` / `无` 之类的占位;`none of the tests passed`、`无法连接,改用代理` 这类真内容照原样保留。
|
|
61
66
|
|
|
62
67
|
`## Asked` 记的是「**助手**向**用户**提了什么、给了哪些选项」。这一节最容易错成「把用户请求改写一遍」——真实一次压缩里,模型把用户那段中文请求翻成英文塞进了 `Asked`,而用户原话就在它上面逐字抄着,同一件事写了两遍。
|
|
63
68
|
|
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) {
|
|
@@ -188,8 +217,13 @@ export function pruneEmptySections(body) {
|
|
|
188
217
|
pendingHeading = undefined;
|
|
189
218
|
pendingBody = [];
|
|
190
219
|
};
|
|
220
|
+
// A heading is either `## Name` or the bare name on its own line. Models
|
|
221
|
+
// sometimes answer without the `##`; a real run returned bare `Asked` / `Did` /
|
|
222
|
+
// `Concluded` lines, so a `^#{1,6}`-only test left `Concluded\n- (none)` in the
|
|
223
|
+
// digest as body text and the placeholder reached the reader.
|
|
224
|
+
const known = /^(Asked|Did|Concluded|Overturned)$/i;
|
|
191
225
|
for (const line of lines) {
|
|
192
|
-
if (/^#{1,6}\s/.test(line)) {
|
|
226
|
+
if (/^#{1,6}\s/.test(line) || known.test(line.trim())) {
|
|
193
227
|
flush();
|
|
194
228
|
pendingHeading = line;
|
|
195
229
|
continue;
|
|
@@ -351,6 +385,10 @@ export function buildSections(events) {
|
|
|
351
385
|
lastTurn = turn;
|
|
352
386
|
return next;
|
|
353
387
|
};
|
|
388
|
+
// One objective per section, and never twice: the host re-injects the same
|
|
389
|
+
// objective on every round, so recording each copy would repeat it dozens of
|
|
390
|
+
// times across a long autonomous run.
|
|
391
|
+
const recordedObjectives = new Set();
|
|
354
392
|
for (const event of events) {
|
|
355
393
|
if (event.type === 'turn/start') {
|
|
356
394
|
const turn = turnOf(event);
|
|
@@ -358,6 +396,23 @@ export function buildSections(events) {
|
|
|
358
396
|
lastTurn = turn;
|
|
359
397
|
continue;
|
|
360
398
|
}
|
|
399
|
+
const objective = goalObjective(event);
|
|
400
|
+
if (objective !== undefined) {
|
|
401
|
+
if (current === undefined) {
|
|
402
|
+
current = { turns: [] };
|
|
403
|
+
sections.push(current);
|
|
404
|
+
}
|
|
405
|
+
if (recordedObjectives.has(objective))
|
|
406
|
+
continue;
|
|
407
|
+
recordedObjectives.add(objective);
|
|
408
|
+
// A goal round opens a section like a user request does, and its objective
|
|
409
|
+
// takes the user slot: it is the request this turn serves.
|
|
410
|
+
current = { turns: [] };
|
|
411
|
+
sections.push(current);
|
|
412
|
+
currentTurn = openTurn(turnOf(event) ?? lastTurn);
|
|
413
|
+
currentTurn.user.push(objective);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
361
416
|
if (event.type === 'tool/result') {
|
|
362
417
|
if (currentTurn === undefined)
|
|
363
418
|
continue;
|
|
@@ -500,13 +555,6 @@ function renderSection(section, capChars) {
|
|
|
500
555
|
if (body.length > 0)
|
|
501
556
|
lines.push(`### assistant\n${body}`);
|
|
502
557
|
}
|
|
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
558
|
if (turn.errors.length > 0) {
|
|
511
559
|
lines.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
512
560
|
}
|
|
@@ -894,34 +942,45 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
894
942
|
// Walk the same sections the renderer will use, so a key addresses the exact
|
|
895
943
|
// slot the block will be written into. Grouping by turn number instead lost
|
|
896
944
|
// blocks whenever the log reused a number.
|
|
945
|
+
// Partition the source into the same sections `buildSections` produced, then
|
|
946
|
+
// hand each section to its slot. A slot is a section, not a single message:
|
|
947
|
+
// the earlier version stopped at the first message, so a turn with 69
|
|
948
|
+
// assistant messages and 112 tool calls contributed 2 events out of 164 and
|
|
949
|
+
// the extractor reported "no work observed" for a turn that did plenty.
|
|
897
950
|
const sections = buildSections(source);
|
|
951
|
+
// Attribute events to sections by replaying the same walk `buildSections`
|
|
952
|
+
// does, so the two can never disagree about where a section starts. An
|
|
953
|
+
// independent scan drifted by one section on the fixture: `buildSections`
|
|
954
|
+
// opens a new section for an unclosed turn without consuming a user message.
|
|
955
|
+
const grouped = [];
|
|
956
|
+
let sectionIndex = -1;
|
|
957
|
+
const openedObjectives = new Set();
|
|
958
|
+
for (const event of source) {
|
|
959
|
+
const objective = goalObjective(event);
|
|
960
|
+
let startsSection = false;
|
|
961
|
+
if (objective !== undefined) {
|
|
962
|
+
if (!openedObjectives.has(objective)) {
|
|
963
|
+
openedObjectives.add(objective);
|
|
964
|
+
startsSection = true;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
else if (event.type === 'user/message' && !isInjection(event) && !isCheckpoint(event)) {
|
|
968
|
+
startsSection = true;
|
|
969
|
+
}
|
|
970
|
+
if (startsSection) {
|
|
971
|
+
sectionIndex += 1;
|
|
972
|
+
grouped[sectionIndex] = [];
|
|
973
|
+
}
|
|
974
|
+
if (sectionIndex >= 0)
|
|
975
|
+
grouped[sectionIndex]?.push(event);
|
|
976
|
+
}
|
|
898
977
|
const slots = [];
|
|
899
|
-
let lastTurn;
|
|
900
|
-
let cursor = 0;
|
|
901
978
|
for (const [sectionIndex, section] of sections.entries()) {
|
|
979
|
+
// A section may hold more than one turn; all of them see the section's
|
|
980
|
+
// events, because the material is about what the section did as a whole.
|
|
981
|
+
const events = grouped[sectionIndex] ?? [];
|
|
902
982
|
for (const [turnIndex, turn] of section.turns.entries()) {
|
|
903
|
-
|
|
904
|
-
// were built from the same array, so the cursor never rewinds.
|
|
905
|
-
const collected = [];
|
|
906
|
-
while (cursor < source.length) {
|
|
907
|
-
const event = source[cursor];
|
|
908
|
-
cursor += 1;
|
|
909
|
-
if (event === undefined)
|
|
910
|
-
break;
|
|
911
|
-
if (event.type === 'turn/start') {
|
|
912
|
-
const turnNumber = turnOf(event);
|
|
913
|
-
if (turnNumber !== undefined)
|
|
914
|
-
lastTurn = turnNumber;
|
|
915
|
-
collected.push(event);
|
|
916
|
-
continue;
|
|
917
|
-
}
|
|
918
|
-
collected.push(event);
|
|
919
|
-
if (event.type === 'user/message' || event.type === 'assistant/message') {
|
|
920
|
-
if (!isInjection(event) && !isCheckpoint(event))
|
|
921
|
-
break;
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
slots.push({ key: turnKey(sectionIndex, turnIndex), turn: turn.turn, events: collected });
|
|
983
|
+
slots.push({ key: turnKey(sectionIndex, turnIndex), turn: turn.turn, events });
|
|
925
984
|
}
|
|
926
985
|
}
|
|
927
986
|
const extracted = new Map();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
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",
|