@logictan/dsh-verbatim-compaction 0.8.0 → 0.10.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 +5 -1
- package/lib/index.js +134 -31
- package/lib/types/index.d.ts +15 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
|
|
63
63
|
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description`,失败的调用另记一行首行错误)。模型输出固定的四节:`## Asked` / `## Did` / `## Concluded` / `## Overturned`。
|
|
64
64
|
|
|
65
|
-
空节直接删掉,不留占位。占位符既包括 `(none)`/`n/a`/`无`,也包括模型爱写的 `未提问。`;判据同时覆盖标题行(`## Asked`、裸写 `Asked`)和行内形式(`- Did: …`、`**Asked
|
|
65
|
+
空节直接删掉,不留占位。占位符既包括 `(none)`/`n/a`/`无`,也包括模型爱写的 `未提问。`;判据同时覆盖标题行(`## Asked`、裸写 `Asked`)和行内形式(`- Did: …`、`**Asked**:…`)——两种形式会混在同一个块里。中文标签(`问:`/`做:`/`结论:`/`推翻:`)同样识别:模型对中文会话会直接改用中文标签,只认英文时 `Asked` 不会被移到最后、空节也不会被清。判据同时接受 `## 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`、`无法连接,改用代理` 这类真内容照原样保留。
|
|
66
66
|
|
|
67
67
|
`## Asked` 记的是「**助手**向**用户**提了什么、给了哪些选项」。这一节最容易错成「把用户请求改写一遍」——真实一次压缩里,模型把用户那段中文请求翻成英文塞进了 `Asked`,而用户原话就在它上面逐字抄着,同一件事写了两遍。
|
|
68
68
|
|
|
@@ -82,10 +82,14 @@
|
|
|
82
82
|
|
|
83
83
|
缓存的键是提取材料的哈希(用户请求 + 助手正文 + 工具轨迹),不是 slot 位置:位置会随历史增长而错位,内容变了哈希就变,不会读到过期块。缓存是进程内的,重启即失效——代价只是重新提取一次,与加缓存前行为一致。
|
|
84
84
|
|
|
85
|
+
`## Asked` 由**代码**从日志直接生成,不使用模型的输出:问题原文、选项、用户的选择(含打字回答)逐字取自 `ask_user_question` 的调用与结果。模型对这一段先后换过四种写法(`## Asked`、`**Asked**:`、`- Asked:`、中文 `问:`),每次换法都会破坏位置或内容,而这两半本来就在日志里,没有可概括的余地。模型写的那段一律丢弃。
|
|
86
|
+
|
|
85
87
|
`## Asked` 记的是「助手问了什么 + 用户选了什么」。原来指令写「record the question, never the answer」——本意是防模型把用户请求抄进这一节,却被照字面执行:实测材料里两条 `ANSWER` 都在(其中一条把整个设计方向改成「游戏化、赛博朋克、提取 3D 模型」),模型只写了问题。
|
|
86
88
|
|
|
87
89
|
`## Asked` 由代码移到最后(`moveAskedLast`),因为用户的回复常常只有「8399」「继续」,只有紧接在提问后面才读得懂。模型用过三种写法——`## Asked`、`**Asked**:`、`- Asked:`——三种都认。
|
|
88
90
|
|
|
91
|
+
`ask_user_question` 不计入工具调用上限(120 条)。它是「问了什么」的唯一记录,而答案按调用 id 匹配、不受上限约束;提问被截断时答案会变成孤立一行——实测一轮 292 次调用,提问排在后面被切掉,用户的选择就此丢失。
|
|
92
|
+
|
|
89
93
|
`ask_user_question` 的问题与用户选择都会进材料。它没有 `description` 参数,载荷在 `questions[]`,原来只留下 `TOOL ask_user_question` 五个字——问题是什么、用户选了什么全丢。答案在工具结果里(`answers[].selected`,或用户自己打字时的 `answers[].custom`),实测 31 次提问里 10 次是打字回答。
|
|
90
94
|
|
|
91
95
|
一个 section 就是一次请求、一个 turn。日志会在同一次请求里推进 turn 号(实测一个请求的 7 条助手消息记作 turn 2、另 9 条记作 turn 3),按号码切分会把一个请求拆成「空请求 turn + 无用户消息的回复 turn」,摘要里就出现两个 `### assistant`。用户消息本身不带 turn 字段,这条也曾让两者永远对不上。
|
package/lib/index.js
CHANGED
|
@@ -256,10 +256,10 @@ export function pruneEmptySections(block) {
|
|
|
256
256
|
const headingOf = (line) => {
|
|
257
257
|
if (/^#{1,6}\s/.test(line))
|
|
258
258
|
return { own: '' };
|
|
259
|
-
if (/^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned)(?:\*\*)?\s*[::]?\s*$/i.test(line)) {
|
|
259
|
+
if (/^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned|问|做|结论|推翻)(?:\*\*)?\s*[::]?\s*$/i.test(line)) {
|
|
260
260
|
return { own: '' };
|
|
261
261
|
}
|
|
262
|
-
const inline = /^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned)(?:\*\*)?\s*[::]\s*(.*)$/i.exec(line);
|
|
262
|
+
const inline = /^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned|问|做|结论|推翻)(?:\*\*)?\s*[::]\s*(.*)$/i.exec(line);
|
|
263
263
|
if (inline !== null)
|
|
264
264
|
return { own: inline[2] ?? '' };
|
|
265
265
|
return undefined;
|
|
@@ -422,12 +422,20 @@ export function collectHints(events) {
|
|
|
422
422
|
// matched nothing and a 342-call turn produced a single hint line.
|
|
423
423
|
if (event.type === 'assistant/message') {
|
|
424
424
|
for (const call of toolCallsOf(event)) {
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
425
|
+
// A question is never dropped by the cap. It is the only record of what was
|
|
426
|
+
// asked, and its answer is matched by call id rather than by the cap, so
|
|
427
|
+
// dropping the question left an orphan ANSWER line with nothing beside it —
|
|
428
|
+
// a real turn made 292 calls, the question came late, and the user's choice
|
|
429
|
+
// never reached the digest.
|
|
430
|
+
const isQuestion = call.name === 'ask_user_question';
|
|
431
|
+
if (!isQuestion) {
|
|
432
|
+
if (calls >= MAX_HINTS_PER_TURN)
|
|
433
|
+
break;
|
|
434
|
+
calls += 1;
|
|
435
|
+
}
|
|
428
436
|
const hint = hintOf(call.arguments);
|
|
429
437
|
lines.push(hint.length > 0 ? `TOOL ${call.name}: ${hint}` : `TOOL ${call.name}`);
|
|
430
|
-
if (
|
|
438
|
+
if (isQuestion)
|
|
431
439
|
askedIds.add(call.id);
|
|
432
440
|
}
|
|
433
441
|
continue;
|
|
@@ -464,6 +472,81 @@ function pathArgumentOf(argumentsJson) {
|
|
|
464
472
|
return undefined;
|
|
465
473
|
}
|
|
466
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* The question an `ask_user_question` call put, with its options, or undefined.
|
|
477
|
+
*
|
|
478
|
+
* @param argumentsJson - the call's raw arguments.
|
|
479
|
+
* @returns one line per question, options appended in brackets.
|
|
480
|
+
*/
|
|
481
|
+
function questionTextOf(argumentsJson) {
|
|
482
|
+
let parsed;
|
|
483
|
+
try {
|
|
484
|
+
parsed = JSON.parse(argumentsJson);
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
return undefined;
|
|
488
|
+
}
|
|
489
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
490
|
+
return undefined;
|
|
491
|
+
const questions = parsed['questions'];
|
|
492
|
+
if (!Array.isArray(questions))
|
|
493
|
+
return undefined;
|
|
494
|
+
const lines = questions
|
|
495
|
+
.map((question) => {
|
|
496
|
+
if (typeof question !== 'object' || question === null)
|
|
497
|
+
return '';
|
|
498
|
+
const record = question;
|
|
499
|
+
const text = typeof record['question'] === 'string'
|
|
500
|
+
? record['question']
|
|
501
|
+
: typeof record['header'] === 'string' ? record['header'] : '';
|
|
502
|
+
if (text.trim().length === 0)
|
|
503
|
+
return '';
|
|
504
|
+
const options = Array.isArray(record['options'])
|
|
505
|
+
? record['options']
|
|
506
|
+
.map((option) => (typeof option === 'object' && option !== null
|
|
507
|
+
? String(option['label'] ?? '')
|
|
508
|
+
: ''))
|
|
509
|
+
.filter((label) => label.length > 0)
|
|
510
|
+
: [];
|
|
511
|
+
return options.length === 0 ? text.trim() : `${text.trim()} [${options.join(' / ')}]`;
|
|
512
|
+
})
|
|
513
|
+
.filter((line) => line.length > 0);
|
|
514
|
+
return lines.length > 0 ? lines.join('\n') : undefined;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* The answer carried by an `ask_user_question` result, or undefined.
|
|
518
|
+
*
|
|
519
|
+
* @param text - the result body.
|
|
520
|
+
* @returns the chosen labels, or the typed reply when nothing was chosen.
|
|
521
|
+
*/
|
|
522
|
+
function answerTextOf(text) {
|
|
523
|
+
let parsed;
|
|
524
|
+
try {
|
|
525
|
+
parsed = JSON.parse(text);
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
return undefined;
|
|
529
|
+
}
|
|
530
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
531
|
+
return undefined;
|
|
532
|
+
const answers = parsed['answers'];
|
|
533
|
+
if (!Array.isArray(answers))
|
|
534
|
+
return undefined;
|
|
535
|
+
const chosen = answers
|
|
536
|
+
.flatMap((answer) => {
|
|
537
|
+
if (typeof answer !== 'object' || answer === null)
|
|
538
|
+
return [];
|
|
539
|
+
const record = answer;
|
|
540
|
+
const selected = record['selected'];
|
|
541
|
+
const picked = Array.isArray(selected) ? selected.map((value) => String(value)) : [];
|
|
542
|
+
// A typed answer arrives with `selected: []` and the text in `custom`.
|
|
543
|
+
const typed = typeof record['custom'] === 'string' && record['custom'].trim().length > 0
|
|
544
|
+
? [record['custom'].trim()] : [];
|
|
545
|
+
return [...picked, ...typed];
|
|
546
|
+
})
|
|
547
|
+
.filter((value) => value.length > 0);
|
|
548
|
+
return chosen.length > 0 ? chosen.join(' / ') : undefined;
|
|
549
|
+
}
|
|
467
550
|
/** The turn number an event belongs to, when the log records one. */
|
|
468
551
|
function turnOf(event) {
|
|
469
552
|
const turn = event.data.turn;
|
|
@@ -471,7 +554,7 @@ function turnOf(event) {
|
|
|
471
554
|
}
|
|
472
555
|
/** A fresh, empty turn accumulator. */
|
|
473
556
|
function emptyTurn(turn) {
|
|
474
|
-
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: [] };
|
|
557
|
+
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: [], asked: [] };
|
|
475
558
|
}
|
|
476
559
|
/**
|
|
477
560
|
* Group append-origin surface events into per-turn digest material.
|
|
@@ -505,6 +588,8 @@ export function buildSections(events) {
|
|
|
505
588
|
// objective on every round, so recording each copy would repeat it dozens of
|
|
506
589
|
// times across a long autonomous run.
|
|
507
590
|
const recordedObjectives = new Set();
|
|
591
|
+
// Question text by tool-call id, so the matching result can be read as its answer.
|
|
592
|
+
const pendingQuestions = new Map();
|
|
508
593
|
for (const event of events) {
|
|
509
594
|
if (event.type === 'turn/start') {
|
|
510
595
|
const turn = turnOf(event);
|
|
@@ -535,6 +620,16 @@ export function buildSections(events) {
|
|
|
535
620
|
const text = textOf(event);
|
|
536
621
|
currentTurn.resultBytes += text.length;
|
|
537
622
|
const message = messageOf(event);
|
|
623
|
+
// A question's own result is its answer. Recorded here rather than left to the
|
|
624
|
+
// model, which dropped or reworded it depending on its heading style.
|
|
625
|
+
const callId = message?.toolCallId;
|
|
626
|
+
if (callId !== undefined) {
|
|
627
|
+
const question = pendingQuestions.get(callId);
|
|
628
|
+
if (question !== undefined) {
|
|
629
|
+
pendingQuestions.delete(callId);
|
|
630
|
+
currentTurn.asked.push({ question, answer: answerTextOf(text) ?? '(no answer recorded)' });
|
|
631
|
+
}
|
|
632
|
+
}
|
|
538
633
|
if (message?.isError === true) {
|
|
539
634
|
const firstLine = text.split('\n')[0] ?? '';
|
|
540
635
|
currentTurn.errors.push(firstLine.slice(0, 100));
|
|
@@ -575,6 +670,11 @@ export function buildSections(events) {
|
|
|
575
670
|
if (text.length > 0)
|
|
576
671
|
currentTurn.assistant.push(text);
|
|
577
672
|
for (const call of toolCallsOf(event)) {
|
|
673
|
+
if (call.name === 'ask_user_question') {
|
|
674
|
+
const question = questionTextOf(call.arguments);
|
|
675
|
+
if (question !== undefined)
|
|
676
|
+
pendingQuestions.set(call.id, question);
|
|
677
|
+
}
|
|
578
678
|
currentTurn.tools.set(call.name, (currentTurn.tools.get(call.name) ?? 0) + 1);
|
|
579
679
|
if (!WRITE_TOOLS.has(call.name))
|
|
580
680
|
continue;
|
|
@@ -672,32 +772,35 @@ function materialKey(events) {
|
|
|
672
772
|
* @param block - one extraction block, possibly malformed.
|
|
673
773
|
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
674
774
|
*/
|
|
675
|
-
export function moveAskedLast(block) {
|
|
775
|
+
export function moveAskedLast(block, asked = []) {
|
|
676
776
|
const lines = block.split('\n');
|
|
677
|
-
//
|
|
678
|
-
//
|
|
679
|
-
//
|
|
680
|
-
|
|
681
|
-
const
|
|
682
|
-
const
|
|
777
|
+
// Recognise every form the model has used for this section, in both languages, so
|
|
778
|
+
// it can be dropped. The content is regenerated from the log instead: four format
|
|
779
|
+
// changes across runs each broke either the position or the wording, and the log
|
|
780
|
+
// already holds both halves verbatim.
|
|
781
|
+
const isAsked = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]?\s*$/i.test(line);
|
|
782
|
+
const isAskedInline = (line) => /^\s*(?:[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]/i.test(line);
|
|
783
|
+
const isOther = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Did|Concluded|Overturned|做|结论|推翻)(?:\*\*)?\s*[::]?/i.test(line);
|
|
683
784
|
const start = lines.findIndex((line) => isAsked(line) || isAskedInline(line));
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
785
|
+
const kept = [...lines];
|
|
786
|
+
if (start >= 0) {
|
|
787
|
+
const inline = isAskedInline(kept[start] ?? '') && !isAsked(kept[start] ?? '');
|
|
788
|
+
let end = start + 1;
|
|
789
|
+
if (!inline) {
|
|
790
|
+
while (end < kept.length && !isOther(kept[end] ?? ''))
|
|
791
|
+
end += 1;
|
|
792
|
+
}
|
|
793
|
+
kept.splice(start, end - start);
|
|
693
794
|
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
795
|
+
while (kept.length > 0 && (kept.at(-1) ?? '').trim().length === 0)
|
|
796
|
+
kept.pop();
|
|
797
|
+
if (asked.length === 0)
|
|
798
|
+
return kept.join('\n');
|
|
799
|
+
const block_lines = asked.map(({ question, answer }) => {
|
|
800
|
+
const q = question.split('\n').join(' ');
|
|
801
|
+
return `- ${q} → ${answer}`;
|
|
802
|
+
});
|
|
803
|
+
return [...kept, '', '## Asked', ...block_lines].join('\n');
|
|
701
804
|
}
|
|
702
805
|
function renderSection(section, capChars) {
|
|
703
806
|
const lines = [];
|
|
@@ -720,7 +823,7 @@ function renderSection(section, capChars) {
|
|
|
720
823
|
if (turn.writes.length > 0)
|
|
721
824
|
trailer.push(` wrote: ${turn.writes.join(', ')}`);
|
|
722
825
|
const body = trailer.length > 0 ? `${block}\n${trailer.join('\n')}` : block;
|
|
723
|
-
lines.push(`### assistant\n${moveAskedLast(body)}`);
|
|
826
|
+
lines.push(`### assistant\n${moveAskedLast(body, turn.asked)}`);
|
|
724
827
|
}
|
|
725
828
|
else {
|
|
726
829
|
let body = turn.assistant.join('\n');
|
package/lib/types/index.d.ts
CHANGED
|
@@ -122,6 +122,17 @@ interface Turn {
|
|
|
122
122
|
resultBytes: number;
|
|
123
123
|
errors: string[];
|
|
124
124
|
writes: string[];
|
|
125
|
+
/**
|
|
126
|
+
* Question/answer pairs the assistant put to the user in this turn.
|
|
127
|
+
*
|
|
128
|
+
* Rendered by code rather than by the model: the model changed its Asked format
|
|
129
|
+
* four times across runs, and each change silently broke the position or the
|
|
130
|
+
* content. Both halves are in the log, so there is nothing to summarise.
|
|
131
|
+
*/
|
|
132
|
+
asked: {
|
|
133
|
+
question: string;
|
|
134
|
+
answer: string;
|
|
135
|
+
}[];
|
|
125
136
|
/**
|
|
126
137
|
* The extracted summary for this turn, when extraction succeeded.
|
|
127
138
|
*
|
|
@@ -170,7 +181,10 @@ export declare function buildSections(events: readonly SessionEvent[]): Section[
|
|
|
170
181
|
* @param block - one extraction block, possibly malformed.
|
|
171
182
|
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
172
183
|
*/
|
|
173
|
-
export declare function moveAskedLast(block: string
|
|
184
|
+
export declare function moveAskedLast(block: string, asked?: readonly {
|
|
185
|
+
question: string;
|
|
186
|
+
answer: string;
|
|
187
|
+
}[]): string;
|
|
174
188
|
/**
|
|
175
189
|
* Render the digest under a hard character budget.
|
|
176
190
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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",
|