@logictan/dsh-verbatim-compaction 0.9.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 +2 -0
- package/lib/index.js +118 -23
- package/lib/types/index.d.ts +15 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,6 +82,8 @@
|
|
|
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:`——三种都认。
|
package/lib/index.js
CHANGED
|
@@ -472,6 +472,81 @@ function pathArgumentOf(argumentsJson) {
|
|
|
472
472
|
return undefined;
|
|
473
473
|
}
|
|
474
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
|
+
}
|
|
475
550
|
/** The turn number an event belongs to, when the log records one. */
|
|
476
551
|
function turnOf(event) {
|
|
477
552
|
const turn = event.data.turn;
|
|
@@ -479,7 +554,7 @@ function turnOf(event) {
|
|
|
479
554
|
}
|
|
480
555
|
/** A fresh, empty turn accumulator. */
|
|
481
556
|
function emptyTurn(turn) {
|
|
482
|
-
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: [] };
|
|
557
|
+
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: [], asked: [] };
|
|
483
558
|
}
|
|
484
559
|
/**
|
|
485
560
|
* Group append-origin surface events into per-turn digest material.
|
|
@@ -513,6 +588,8 @@ export function buildSections(events) {
|
|
|
513
588
|
// objective on every round, so recording each copy would repeat it dozens of
|
|
514
589
|
// times across a long autonomous run.
|
|
515
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();
|
|
516
593
|
for (const event of events) {
|
|
517
594
|
if (event.type === 'turn/start') {
|
|
518
595
|
const turn = turnOf(event);
|
|
@@ -543,6 +620,16 @@ export function buildSections(events) {
|
|
|
543
620
|
const text = textOf(event);
|
|
544
621
|
currentTurn.resultBytes += text.length;
|
|
545
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
|
+
}
|
|
546
633
|
if (message?.isError === true) {
|
|
547
634
|
const firstLine = text.split('\n')[0] ?? '';
|
|
548
635
|
currentTurn.errors.push(firstLine.slice(0, 100));
|
|
@@ -583,6 +670,11 @@ export function buildSections(events) {
|
|
|
583
670
|
if (text.length > 0)
|
|
584
671
|
currentTurn.assistant.push(text);
|
|
585
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
|
+
}
|
|
586
678
|
currentTurn.tools.set(call.name, (currentTurn.tools.get(call.name) ?? 0) + 1);
|
|
587
679
|
if (!WRITE_TOOLS.has(call.name))
|
|
588
680
|
continue;
|
|
@@ -680,32 +772,35 @@ function materialKey(events) {
|
|
|
680
772
|
* @param block - one extraction block, possibly malformed.
|
|
681
773
|
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
682
774
|
*/
|
|
683
|
-
export function moveAskedLast(block) {
|
|
775
|
+
export function moveAskedLast(block, asked = []) {
|
|
684
776
|
const lines = block.split('\n');
|
|
685
|
-
//
|
|
686
|
-
//
|
|
687
|
-
//
|
|
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.
|
|
688
781
|
const isAsked = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]?\s*$/i.test(line);
|
|
689
782
|
const isAskedInline = (line) => /^\s*(?:[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]/i.test(line);
|
|
690
|
-
const isOther = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(Did|Concluded|Overturned|做|结论|推翻)(?:\*\*)?\s*[::]?/i.test(line);
|
|
783
|
+
const isOther = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Did|Concluded|Overturned|做|结论|推翻)(?:\*\*)?\s*[::]?/i.test(line);
|
|
691
784
|
const start = lines.findIndex((line) => isAsked(line) || isAskedInline(line));
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
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);
|
|
701
794
|
}
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
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');
|
|
709
804
|
}
|
|
710
805
|
function renderSection(section, capChars) {
|
|
711
806
|
const lines = [];
|
|
@@ -728,7 +823,7 @@ function renderSection(section, capChars) {
|
|
|
728
823
|
if (turn.writes.length > 0)
|
|
729
824
|
trailer.push(` wrote: ${turn.writes.join(', ')}`);
|
|
730
825
|
const body = trailer.length > 0 ? `${block}\n${trailer.join('\n')}` : block;
|
|
731
|
-
lines.push(`### assistant\n${moveAskedLast(body)}`);
|
|
826
|
+
lines.push(`### assistant\n${moveAskedLast(body, turn.asked)}`);
|
|
732
827
|
}
|
|
733
828
|
else {
|
|
734
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",
|