@logictan/dsh-verbatim-compaction 0.4.0 → 0.5.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 +7 -1
- package/lib/index.js +78 -6
- package/lib/types/index.d.ts +17 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,7 +59,9 @@
|
|
|
59
59
|
|
|
60
60
|
空节直接删掉,不留 `(none)` 占位。实测一份真实提取块里 `## Asked` 和 `## Overturned` 都是 `(none)`,删掉后 312 → 271 字符;按 130 轮算,这些占位合计约 900 token 只是在记录「什么都没发生」。清理在代码里做(`pruneEmptySections`),不只依赖提示词——模型仍写了 `(none)` 也不会白花 token。判据是整节每一行去掉项目符号后都只是 `(none)` / `none` / `n/a` / `无` 之类的占位;`none of the tests passed`、`无法连接,改用代理` 这类真内容照原样保留。
|
|
61
61
|
|
|
62
|
-
`## Asked`
|
|
62
|
+
`## Asked` 记的是「**助手**向**用户**提了什么、给了哪些选项」。这一节最容易错成「把用户请求改写一遍」——真实一次压缩里,模型把用户那段中文请求翻成英文塞进了 `Asked`,而用户原话就在它上面逐字抄着,同一件事写了两遍。
|
|
63
|
+
|
|
64
|
+
两个原因都已在代码里堵住:材料里原来的标签是 `USER REQUEST:`,模型有理由读成「这一轮问了什么」,现在改成 `USER INSTRUCTION (already in the digest verbatim; never copy it into Asked)`;正文为空的 slot 会显式带上 `ASKED: omit — this turn produced no assistant prose`,因为那一轮有 96 条助手消息、150 次工具调用却只有 156 字符正文,模型无米可炊就只好改写用户请求。实测 123 条真人消息里 43 条(35%)不超过 12 个字符——`3`、`1`、`同意`、`做`、`继续`——而 46% 是在回答上一轮助手的提问。只逐字保留「3」而不保留它在回答什么问题,等于记下一个决定却没记下决定的是什么。
|
|
63
65
|
|
|
64
66
|
模型用的是会话自己的路由(`session.requestHeader()?.config`),和宿主原生压缩同一个取法,不需要单独配置凭据。
|
|
65
67
|
|
|
@@ -69,6 +71,10 @@
|
|
|
69
71
|
- **工具调用从 `assistant/message` 的 block 里取**。日志里没有独立的 `tool/call` 事件类型,按它过滤会一条都取不到。
|
|
70
72
|
- **提取失败就退回逐字正文**。模型不可达、超时、输出为空时,该轮保留原来的助手正文;压缩本身不能因此失败,否则会话死于上下文溢出,比丢细节更糟。降级是逐轮的:一批里某一轮没答,只有那一轮退回正文。
|
|
71
73
|
|
|
74
|
+
提取结果按内容哈希缓存,同一次会话内复用。`digestSource` 每次压缩都从原始日志重建,而原始事件永远留在日志里,所以同一轮会被反复读到。实测那个压缩了 33 次的会话:共提取 142 轮,其中 **129 轮(91%)在更早的压缩里已经提取过**;全机 370 个会话、456 次压缩里,**81% 的压缩工作量是这种重复**(有压缩的 88 个会话中 70 个压缩过 2 次以上)。
|
|
75
|
+
|
|
76
|
+
缓存的键是提取材料的哈希(用户请求 + 助手正文 + 工具轨迹),不是 slot 位置:位置会随历史增长而错位,内容变了哈希就变,不会读到过期块。缓存是进程内的,重启即失效——代价只是重新提取一次,与加缓存前行为一致。
|
|
77
|
+
|
|
72
78
|
提取按批调用:每 8 轮一次,一轮输出一个 `=== TURN n ===` 块。早期实现是每轮一次调用,122 轮就是 131 次往返,单轮 400 token 的输出上限乘以 131 就可能超过整个摘要上限。批处理把往返压到约 17 次,并共享 prompt 前缀以复用 provider 的 KV cache。
|
|
73
79
|
|
|
74
80
|
用户原话在布局上**先占额度**,提取块不得挤占。实测那个 122 轮会话:区间内真人原话共 469 字符(117 token),而每轮一块 420 字符的提取共 55020 字符——提取是正文的 117 倍,任何让二者竞争的布局都会把正文挤掉,而正文是「请用pixi管理开发环境」这类永不过期的约束,提取描述的却是已经做完的工作。
|
package/lib/index.js
CHANGED
|
@@ -207,7 +207,9 @@ const EXTRACT_INSTRUCTION = [
|
|
|
207
207
|
'For EVERY item, emit exactly one block, introduced by `=== SLOT <key> ===` using that same key. Under each heading write at most two terse lines. OMIT a heading entirely when it has no real information — do not write "(none)" and do not invent. An item where nothing substantive happened may be omitted altogether.',
|
|
208
208
|
'',
|
|
209
209
|
'## Asked',
|
|
210
|
-
'- the
|
|
210
|
+
'- what the ASSISTANT asked the USER in this turn: the question it put, and the options it offered',
|
|
211
|
+
'- the next user message is usually the answer, so record the question, never the answer',
|
|
212
|
+
'- NEVER restate or translate the user request here. It is already in the digest verbatim, directly above this block; copying it is the single most common way this section goes wrong.',
|
|
211
213
|
'',
|
|
212
214
|
'## Did',
|
|
213
215
|
'- what was actually done, concretely',
|
|
@@ -434,6 +436,39 @@ function withExtractions(sections, extracted) {
|
|
|
434
436
|
function turnKey(sectionIndex, turnIndex) {
|
|
435
437
|
return `${sectionIndex}:${turnIndex}`;
|
|
436
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* Cache key for one slot's extraction material: a hash of everything the model
|
|
441
|
+
* would be shown.
|
|
442
|
+
*
|
|
443
|
+
* The material, not the slot's position or turn number: positions shift as
|
|
444
|
+
* history grows and numbers repeat, but identical material must produce an
|
|
445
|
+
* identical extraction. Hashing the exact string `buildExtractionMaterial`
|
|
446
|
+
* returns means a change anywhere in the user request, the prose, or the tool
|
|
447
|
+
* trace invalidates the entry — nothing can silently go stale.
|
|
448
|
+
*
|
|
449
|
+
* FNV-1a, 32-bit, two seeds. Not cryptographic: the risk here is an accidental
|
|
450
|
+
* collision between two turns, and 64 bits over a few hundred entries makes that
|
|
451
|
+
* vanishingly unlikely.
|
|
452
|
+
*
|
|
453
|
+
* @param events - one slot's events, in log order.
|
|
454
|
+
* @returns a stable hex key.
|
|
455
|
+
*/
|
|
456
|
+
function materialKey(events) {
|
|
457
|
+
const prose = events
|
|
458
|
+
.filter((event) => event.type === 'assistant/message')
|
|
459
|
+
.map((event) => textOf(event))
|
|
460
|
+
.filter((text) => text.length > 0)
|
|
461
|
+
.join('\n');
|
|
462
|
+
const material = buildExtractionMaterial(events, prose);
|
|
463
|
+
let a = 0x811c9dc5;
|
|
464
|
+
let b = 0x01000193;
|
|
465
|
+
for (let index = 0; index < material.length; index += 1) {
|
|
466
|
+
const code = material.charCodeAt(index);
|
|
467
|
+
a = Math.imul(a ^ code, 0x01000193) >>> 0;
|
|
468
|
+
b = Math.imul(b ^ (code + index), 0x85ebca6b) >>> 0;
|
|
469
|
+
}
|
|
470
|
+
return `${a.toString(16)}${b.toString(16)}`;
|
|
471
|
+
}
|
|
437
472
|
/**
|
|
438
473
|
* Render one section to digest text, truncating assistant prose to `capChars`.
|
|
439
474
|
*
|
|
@@ -677,9 +712,16 @@ export function buildExtractionMaterial(turn, prose) {
|
|
|
677
712
|
const hints = collectHints(turn);
|
|
678
713
|
if (userRequest.length === 0 && prose.length === 0 && hints.length === 0)
|
|
679
714
|
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';
|
|
680
721
|
return [
|
|
681
|
-
`USER
|
|
722
|
+
`USER INSTRUCTION (already in the digest verbatim; never copy it into Asked):\n${userRequest.slice(0, 4000) || '(none)'}`,
|
|
682
723
|
`ASSISTANT PROSE:\n${prose.slice(0, 6000) || '(none)'}`,
|
|
724
|
+
asked,
|
|
683
725
|
`TOOL TRACE:\n${hints || '(none)'}`,
|
|
684
726
|
].join('\n\n');
|
|
685
727
|
}
|
|
@@ -778,6 +820,23 @@ async function extractBatch(ctx, agent, batch, signal) {
|
|
|
778
820
|
* subclass cannot diverge from the host's transaction semantics.
|
|
779
821
|
*/
|
|
780
822
|
export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
823
|
+
/**
|
|
824
|
+
* Extraction results, keyed by a hash of the material they were derived from.
|
|
825
|
+
*
|
|
826
|
+
* `digestSource` rebuilds from the raw log on every compaction, and raw events
|
|
827
|
+
* never leave the log, so the same turn is re-read on every later compaction.
|
|
828
|
+
* Measured on the session that compacted 33 times: 142 turns extracted, 129 of
|
|
829
|
+
* them (91%) already extracted earlier. Across all 370 local sessions, 81% of
|
|
830
|
+
* compaction work is that repeat.
|
|
831
|
+
*
|
|
832
|
+
* Keyed by content, not by slot position: a position holds different content
|
|
833
|
+
* once history grows, so a positional key would serve a stale block. A turn
|
|
834
|
+
* whose prose changed hashes differently and is extracted again.
|
|
835
|
+
*
|
|
836
|
+
* Process-local and bounded. A restart loses it, costing one re-extraction —
|
|
837
|
+
* the same behaviour as before this cache existed.
|
|
838
|
+
*/
|
|
839
|
+
extractionCache = new Map();
|
|
781
840
|
/**
|
|
782
841
|
* Build the replacement checkpoint content from the session log.
|
|
783
842
|
*
|
|
@@ -873,13 +932,26 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
873
932
|
}
|
|
874
933
|
}
|
|
875
934
|
const extracted = new Map();
|
|
876
|
-
|
|
935
|
+
const missing = [];
|
|
936
|
+
for (const slot of slots) {
|
|
937
|
+
const cached = this.extractionCache.get(materialKey(slot.events));
|
|
938
|
+
if (cached === undefined)
|
|
939
|
+
missing.push(slot);
|
|
940
|
+
else
|
|
941
|
+
extracted.set(slot.key, cached);
|
|
942
|
+
}
|
|
943
|
+
for (let index = 0; index < missing.length; index += TURNS_PER_CALL) {
|
|
877
944
|
if (signal?.aborted === true)
|
|
878
945
|
break;
|
|
879
|
-
const batch =
|
|
946
|
+
const batch = missing.slice(index, index + TURNS_PER_CALL);
|
|
880
947
|
const blocks = await extractBatch(this.ctx, agent, batch, signal);
|
|
881
|
-
for (const
|
|
882
|
-
|
|
948
|
+
for (const slot of batch) {
|
|
949
|
+
const block = blocks.get(slot.key);
|
|
950
|
+
if (block === undefined)
|
|
951
|
+
continue;
|
|
952
|
+
extracted.set(slot.key, block);
|
|
953
|
+
this.extractionCache.set(materialKey(slot.events), block);
|
|
954
|
+
}
|
|
883
955
|
}
|
|
884
956
|
return extracted;
|
|
885
957
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -231,6 +231,23 @@ export declare function buildExtractionMaterial(turn: readonly SessionEvent[], p
|
|
|
231
231
|
* subclass cannot diverge from the host's transaction semantics.
|
|
232
232
|
*/
|
|
233
233
|
export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
234
|
+
/**
|
|
235
|
+
* Extraction results, keyed by a hash of the material they were derived from.
|
|
236
|
+
*
|
|
237
|
+
* `digestSource` rebuilds from the raw log on every compaction, and raw events
|
|
238
|
+
* never leave the log, so the same turn is re-read on every later compaction.
|
|
239
|
+
* Measured on the session that compacted 33 times: 142 turns extracted, 129 of
|
|
240
|
+
* them (91%) already extracted earlier. Across all 370 local sessions, 81% of
|
|
241
|
+
* compaction work is that repeat.
|
|
242
|
+
*
|
|
243
|
+
* Keyed by content, not by slot position: a position holds different content
|
|
244
|
+
* once history grows, so a positional key would serve a stale block. A turn
|
|
245
|
+
* whose prose changed hashes differently and is extracted again.
|
|
246
|
+
*
|
|
247
|
+
* Process-local and bounded. A restart loses it, costing one re-extraction —
|
|
248
|
+
* the same behaviour as before this cache existed.
|
|
249
|
+
*/
|
|
250
|
+
private readonly extractionCache;
|
|
234
251
|
/**
|
|
235
252
|
* Build the replacement checkpoint content from the session log.
|
|
236
253
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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",
|