@logictan/dsh-verbatim-compaction 0.11.1 → 0.12.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 +9 -5
- package/lib/index.js +73 -160
- package/lib/types/index.d.ts +30 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
|
|
61
61
|
只抄正文救不了「正文为空」的轮次。实测一个 13878 事件的会话:1973 条 assistant 消息里只有 317 条带正文,其中一轮跑了 342 次工具调用却一个字没写,纯逐字摘要把它压成一行 `tools: bash×281`——活干了,记录没了。
|
|
62
62
|
|
|
63
|
-
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description
|
|
63
|
+
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description`,失败的调用另记一行首行错误)。模型输出四个字段:`asked` / `did` / `concluded` / `overturned`,标题与项目符号由渲染器拼装。
|
|
64
64
|
|
|
65
65
|
空节直接不出现:「没有内容」是一个空数组,不是需要事后识别的占位符。字段化之前是自由 markdown,模型写 `## Overturned\n- (none)` 就得在代码里再认一遍占位符,实测一份真实提取块里 `## Asked` 和 `## Overturned` 都是 `(none)`,删掉后 312 → 271 字符;按 130 轮算,这些占位合计约 900 token 只是在记录「什么都没发生」。现在这类占位在解析阶段就被滤掉,不再有一个专门的清理函数。`none of the tests passed`、`无法连接,改用代理` 这类真内容照原样保留。
|
|
66
66
|
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
- **工具调用从 `assistant/message` 的 block 里取**。日志里没有独立的 `tool/call` 事件类型,按它过滤会一条都取不到。
|
|
79
79
|
- **提取失败就退回逐字正文**。模型不可达、超时、输出为空时,该轮保留原来的助手正文;压缩本身不能因此失败,否则会话死于上下文溢出,比丢细节更糟。降级是逐轮的:一批里某一轮没答,只有那一轮退回正文。**但降级不再无声**:整批拿不到可用 JSON、或单槽位在令牌上限被截断,都会打一条 `verbatim-compaction:` 开头的 warn,写明受影响的 slot key。没有这条日志时,插件退化成纯逐字摘要这件事在运行期完全不可观测——这正是「JSON 经常不生效却查不到」的成因。
|
|
80
80
|
|
|
81
|
-
提取调用的 `maxTokens` **在每槽位预算之外另加一笔固定的思考余量**(`EXTRACT_TOKENS_PER_TURN × 槽位数 + EXTRACT_THINKING_RESERVE_TOKENS
|
|
81
|
+
提取调用的 `maxTokens` **在每槽位预算之外另加一笔固定的思考余量**(`EXTRACT_TOKENS_PER_TURN × 槽位数 + EXTRACT_THINKING_RESERVE_TOKENS`)。加入 `asked` 后每槽位预算从 220 提到 900:真实 8 槽批次上思考单独就占 1057–4086 token,`220 × 8 + 1000` 曾把一次调用从 JSON 中间截断,解析出 0 个槽位、整批丢失;`900 × n + 1000` 每次都能解析出全部槽位。`maxTokens` 把思考与正文算在一起,而思考是**每次调用的固定开销,不随批次变小而变小**——这正是「按槽位线性给上限」必然失败的原因。对真实路由(deepseek-flash,真实量级材料)实测:单槽位按旧公式 `220 × 1` 给上限,**4 次全部解析出 0 个槽位**(思考块吃光了整个配额);改成 `220 × 1 + 1000` 后 **4 次全部成功**,1/2/4/8 槽位各 4 次同样全通过。
|
|
82
82
|
|
|
83
83
|
同时把 effort **钉在路由声明的最便宜档**,而不是沿用路由默认档。思考与正文共用同一笔 `maxTokens`,默认档通常是最贵那档,会挤占正文预算。实测 2 槽位、带余量:`low` 花 375 个思考 token、3/3 解析成功;`high` 花 931、只有 2/3。
|
|
84
84
|
|
|
@@ -92,11 +92,15 @@
|
|
|
92
92
|
|
|
93
93
|
提取调用返回 **JSON 字段**(`{"slots":[{"key","did","concluded","overturned"}]}`),标题、项目符号、空行全部由代码拼装。此前是自由 markdown,模型会把标题和正文挤在同一行——真实产出里出现过 `… 重出 4 屏 Concluded Stitch 会回填真实车牌`,事后无法用正则判断句子在哪结束、下一个标题从哪开始。字段化之后这个歧义不存在。
|
|
94
94
|
|
|
95
|
-
`## Asked`
|
|
95
|
+
`## Asked` 由**模型**输出,和 `did`/`concluded`/`overturned` 并列成 JSON 字段:`asked: [{question, options}]`。曾经改成由代码从 `ask_user_question` 的调用与结果里确定性生成,那个方案在「提问走的是工具」时才成立,而实测里提问大量出现在正文末尾,工具调用根本没有。
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
**为什么不能只靠日志读。** 两件事需要判断,而不是取字段:其一,提问不在可靠的位置——231 个「引出了短回复」的真实轮次里,34% 的提问后面还有正文(p90 = 7 段,最多 24 段),形如 `## 需要你决定` 后面又跟一节 `## 还没验证的`;其二,选项通常也不在提问旁边——28 个纯数字回复的轮次里,28 个都提供过编号列表,但只有 26/231 轮那份列表落在提问本段或前一段,多数隔着自己的小标题在上面。要认出「这个提问指的是哪份列表」需要判断,日志里只有文字。
|
|
98
98
|
|
|
99
|
-
`##
|
|
99
|
+
**`answer` 字段被明确去掉。** 用户的回复就是下一节的 `## user` 行,逐字渲染;再写进 `asked.answer` 等于同一句话印两遍。指令里对应加了一条「不要报告用户的回复」,渲染上 `## Asked` 是块的最后一节,所以回复永远紧接在它下面。
|
|
100
|
+
|
|
101
|
+
`## Asked` 的位置由渲染器保证在最后,不靠模型。选项在问题下面缩进编号列出,`- 问题` 一行、` 1. …` 各行,用户的 `1` 因此有东西可指。
|
|
102
|
+
|
|
103
|
+
**选项自带的编号会被剥掉。** 模型会把源列表的 `1.` 一起抄进 `options`,渲染器再加一次编号就变成 `1. 1. …`;实测 5 次里 3 次如此,所以在解析阶段用正则剥掉,而不是只靠指令。
|
|
100
104
|
|
|
101
105
|
`ask_user_question` 不计入工具调用上限(120 条)。它是「问了什么」的唯一记录,而答案按调用 id 匹配、不受上限约束;提问被截断时答案会变成孤立一行——实测一轮 292 次调用,提问排在后面被切掉,用户的选择就此丢失。
|
|
102
106
|
|
package/lib/index.js
CHANGED
|
@@ -233,27 +233,25 @@ export function isErrorResult(event) {
|
|
|
233
233
|
}
|
|
234
234
|
/** Directive appended as the final user message of the extraction call. */
|
|
235
235
|
const EXTRACT_INSTRUCTION = [
|
|
236
|
-
'You are summarizing a batch of turns from a coding session for a compaction checkpoint.',
|
|
237
|
-
'Each item is introduced by a line `=== SLOT <key> (turn <n>) ===` and contains the user request, the assistant prose (may be empty), and a trace of the tool calls it made (descriptions only) plus any results the log marked as errors.',
|
|
238
|
-
'',
|
|
239
236
|
'Reply with ONE JSON object and nothing else. No prose, no code fence, no markdown.',
|
|
240
|
-
'Shape: {"slots":[{"key":"<the slot key>","did":["…"],"concluded":["…"],"overturned":["…"]}]}',
|
|
241
|
-
'',
|
|
242
|
-
'
|
|
243
|
-
'- `
|
|
244
|
-
'-
|
|
245
|
-
'-
|
|
246
|
-
'- Write only the content of a line. Never write a heading, a bullet, or a field name inside a string.',
|
|
247
|
-
'- Do not describe the tool calls themselves ("ran bash", "explored the repo"). State what came out of them.',
|
|
248
|
-
'- Do not repeat a conclusion an earlier turn already stated.',
|
|
237
|
+
'Shape: {"slots":[{"key":"<the slot key>","did":["…"],"concluded":["…"],"overturned":["…"],"asked":[{"question":"…","options":["…"]}]}]}',
|
|
238
|
+
'Rules:',
|
|
239
|
+
'- `key` must be copied exactly from the `=== SLOT <key> ===` line. One entry per slot, in the order given. Never skip a slot.',
|
|
240
|
+
'- `did`: what was done, at most two short strings. `concluded`: findings a later turn needs, at most two. `overturned`: earlier conclusions this turn disproved.',
|
|
241
|
+
'- Each field is an array of short strings. Use [] for a field with nothing to say.',
|
|
242
|
+
'- Never write a heading, a bullet, or a field name inside a string.',
|
|
249
243
|
'- Never invent. If the material does not support a claim, leave the field empty.',
|
|
244
|
+
// A question is not reliably the last paragraph: measured over 231 real turns
|
|
245
|
+
// that drew a short reply, 34% had prose after it (p90 = 7 paragraphs, max 24).
|
|
246
|
+
'- `asked`: one entry per question this turn put to the user and was still waiting on when it ended. A question is anywhere in the prose, not only the last paragraph.',
|
|
247
|
+
// And the options are usually further up: in 26 of 231 turns was the list in the
|
|
248
|
+
// question's own paragraph or the one before it.
|
|
249
|
+
'- `options`: what the user could pick from, in order, one short line each, with their own numbering stripped. The list is often a paragraph or two above the question. Use [] when the question offered none.',
|
|
250
|
+
// The reply is the next turn's `## user` line, already verbatim in the digest.
|
|
251
|
+
// Asking for it here printed the same sentence twice.
|
|
252
|
+
"- Use [] for `asked` when the turn asked nothing. Do not report the user's reply.",
|
|
250
253
|
'',
|
|
251
|
-
'
|
|
252
|
-
'',
|
|
253
|
-
// Measured on a real 32-slot session: whole batches came back in English while
|
|
254
|
-
// the material was Chinese, so the digest mixed two languages for one
|
|
255
|
-
// conversation.
|
|
256
|
-
'Write the strings in the same language as the material. A session whose requests are Chinese gets Chinese text; do not switch to English.',
|
|
254
|
+
'Write strings in the same language as the material. A session whose requests are Chinese gets Chinese text; do not switch to English.',
|
|
257
255
|
].join('\n');
|
|
258
256
|
/**
|
|
259
257
|
* Parse the model's JSON reply into per-slot extractions.
|
|
@@ -296,6 +294,31 @@ export function parseExtraction(answer) {
|
|
|
296
294
|
.filter((item) => !/^(?:\(?none\)?|n\/a|na|-{1,2}|无|沒有|没有)$/i.test(item))
|
|
297
295
|
.filter((item) => !/^(?:未提问|无提问|没有提问|no questions? (?:were |was )?asked|none asked)[。.!!]*$/i.test(item))
|
|
298
296
|
: []);
|
|
297
|
+
// Asked is a list of objects, not of strings: a question without its options
|
|
298
|
+
// turns the user's `1` into a number that selects nothing.
|
|
299
|
+
const pairs = (value) => (Array.isArray(value)
|
|
300
|
+
? value
|
|
301
|
+
.flatMap((item) => {
|
|
302
|
+
if (typeof item !== 'object' || item === null)
|
|
303
|
+
return [];
|
|
304
|
+
const record = item;
|
|
305
|
+
const question = typeof record['question'] === 'string'
|
|
306
|
+
? record['question'].replace(/\s+/g, ' ').trim()
|
|
307
|
+
: '';
|
|
308
|
+
if (question.length === 0)
|
|
309
|
+
return [];
|
|
310
|
+
const options = Array.isArray(record['options'])
|
|
311
|
+
? record['options']
|
|
312
|
+
.filter((option) => typeof option === 'string')
|
|
313
|
+
// The renderer supplies the numbering, so a model that echoes the source
|
|
314
|
+
// list's own `1.` would print `1. 1. …`. Stripped here rather than asked
|
|
315
|
+
// for in the prompt alone: three of five real runs kept it.
|
|
316
|
+
.map((option) => option.replace(/\s+/g, ' ').replace(/^\s*\d{1,2}\s*[.、)]\s*/, '').trim())
|
|
317
|
+
.filter((option) => option.length > 0)
|
|
318
|
+
: [];
|
|
319
|
+
return [{ question, options }];
|
|
320
|
+
})
|
|
321
|
+
: []);
|
|
299
322
|
for (const slot of slots) {
|
|
300
323
|
if (typeof slot !== 'object' || slot === null)
|
|
301
324
|
continue;
|
|
@@ -307,6 +330,7 @@ export function parseExtraction(answer) {
|
|
|
307
330
|
did: strings(record['did']),
|
|
308
331
|
concluded: strings(record['concluded']),
|
|
309
332
|
overturned: strings(record['overturned']),
|
|
333
|
+
asked: pairs(record['asked']),
|
|
310
334
|
});
|
|
311
335
|
}
|
|
312
336
|
return out;
|
|
@@ -330,12 +354,35 @@ export function renderExtractionBlock(extraction) {
|
|
|
330
354
|
section('Did', extraction.did);
|
|
331
355
|
section('Concluded', extraction.concluded);
|
|
332
356
|
section('Overturned', extraction.overturned);
|
|
357
|
+
// Asked goes last and its options are listed under it, so the user's reply — the
|
|
358
|
+
// next `## user` line — lands directly under the list it chose from.
|
|
359
|
+
if (extraction.asked.length > 0) {
|
|
360
|
+
lines.push('## Asked');
|
|
361
|
+
for (const pair of extraction.asked) {
|
|
362
|
+
lines.push(`- ${pair.question}`);
|
|
363
|
+
// Indented under the question so a reader cannot mistake an option for a
|
|
364
|
+
// separate question, and numbered so the reply's digit has something to
|
|
365
|
+
// point at.
|
|
366
|
+
for (const [index, option] of pair.options.entries()) {
|
|
367
|
+
lines.push(` ${index + 1}. ${option}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
333
371
|
return lines.join('\n');
|
|
334
372
|
}
|
|
335
373
|
/** Turns handed to one extraction call. */
|
|
336
374
|
const TURNS_PER_CALL = 8;
|
|
337
|
-
/**
|
|
338
|
-
|
|
375
|
+
/**
|
|
376
|
+
* Output tokens allowed per turn in an extraction call.
|
|
377
|
+
*
|
|
378
|
+
* Raised from 220 when `asked` was added. The model thinks before it answers and
|
|
379
|
+
* thinking is charged to the same `maxTokens`, so the answer's own budget has to
|
|
380
|
+
* cover both. Measured on real 8-slot batches: thinking alone ran 1057–4086
|
|
381
|
+
* tokens, and a cap of `220 x 8 + 1000` cut one run off mid-JSON, which parsed to
|
|
382
|
+
* zero slots and lost the whole batch. `900` per slot plus the reserve below
|
|
383
|
+
* parsed 8 of 8 on every attempt.
|
|
384
|
+
*/
|
|
385
|
+
const EXTRACT_TOKENS_PER_TURN = 900;
|
|
339
386
|
/**
|
|
340
387
|
* Fixed thinking reserve added to every extraction call's `maxTokens`.
|
|
341
388
|
*
|
|
@@ -485,81 +532,6 @@ function pathArgumentOf(argumentsJson) {
|
|
|
485
532
|
return undefined;
|
|
486
533
|
}
|
|
487
534
|
}
|
|
488
|
-
/**
|
|
489
|
-
* The question an `ask_user_question` call put, with its options, or undefined.
|
|
490
|
-
*
|
|
491
|
-
* @param argumentsJson - the call's raw arguments.
|
|
492
|
-
* @returns one line per question, options appended in brackets.
|
|
493
|
-
*/
|
|
494
|
-
function questionTextOf(argumentsJson) {
|
|
495
|
-
let parsed;
|
|
496
|
-
try {
|
|
497
|
-
parsed = JSON.parse(argumentsJson);
|
|
498
|
-
}
|
|
499
|
-
catch {
|
|
500
|
-
return undefined;
|
|
501
|
-
}
|
|
502
|
-
if (typeof parsed !== 'object' || parsed === null)
|
|
503
|
-
return undefined;
|
|
504
|
-
const questions = parsed['questions'];
|
|
505
|
-
if (!Array.isArray(questions))
|
|
506
|
-
return undefined;
|
|
507
|
-
const lines = questions
|
|
508
|
-
.map((question) => {
|
|
509
|
-
if (typeof question !== 'object' || question === null)
|
|
510
|
-
return '';
|
|
511
|
-
const record = question;
|
|
512
|
-
const text = typeof record['question'] === 'string'
|
|
513
|
-
? record['question']
|
|
514
|
-
: typeof record['header'] === 'string' ? record['header'] : '';
|
|
515
|
-
if (text.trim().length === 0)
|
|
516
|
-
return '';
|
|
517
|
-
const options = Array.isArray(record['options'])
|
|
518
|
-
? record['options']
|
|
519
|
-
.map((option) => (typeof option === 'object' && option !== null
|
|
520
|
-
? String(option['label'] ?? '')
|
|
521
|
-
: ''))
|
|
522
|
-
.filter((label) => label.length > 0)
|
|
523
|
-
: [];
|
|
524
|
-
return options.length === 0 ? text.trim() : `${text.trim()} [${options.join(' / ')}]`;
|
|
525
|
-
})
|
|
526
|
-
.filter((line) => line.length > 0);
|
|
527
|
-
return lines.length > 0 ? lines.join('\n') : undefined;
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
* The answer carried by an `ask_user_question` result, or undefined.
|
|
531
|
-
*
|
|
532
|
-
* @param text - the result body.
|
|
533
|
-
* @returns the chosen labels, or the typed reply when nothing was chosen.
|
|
534
|
-
*/
|
|
535
|
-
function answerTextOf(text) {
|
|
536
|
-
let parsed;
|
|
537
|
-
try {
|
|
538
|
-
parsed = JSON.parse(text);
|
|
539
|
-
}
|
|
540
|
-
catch {
|
|
541
|
-
return undefined;
|
|
542
|
-
}
|
|
543
|
-
if (typeof parsed !== 'object' || parsed === null)
|
|
544
|
-
return undefined;
|
|
545
|
-
const answers = parsed['answers'];
|
|
546
|
-
if (!Array.isArray(answers))
|
|
547
|
-
return undefined;
|
|
548
|
-
const chosen = answers
|
|
549
|
-
.flatMap((answer) => {
|
|
550
|
-
if (typeof answer !== 'object' || answer === null)
|
|
551
|
-
return [];
|
|
552
|
-
const record = answer;
|
|
553
|
-
const selected = record['selected'];
|
|
554
|
-
const picked = Array.isArray(selected) ? selected.map((value) => String(value)) : [];
|
|
555
|
-
// A typed answer arrives with `selected: []` and the text in `custom`.
|
|
556
|
-
const typed = typeof record['custom'] === 'string' && record['custom'].trim().length > 0
|
|
557
|
-
? [record['custom'].trim()] : [];
|
|
558
|
-
return [...picked, ...typed];
|
|
559
|
-
})
|
|
560
|
-
.filter((value) => value.length > 0);
|
|
561
|
-
return chosen.length > 0 ? chosen.join(' / ') : undefined;
|
|
562
|
-
}
|
|
563
535
|
/** The turn number an event belongs to, when the log records one. */
|
|
564
536
|
function turnOf(event) {
|
|
565
537
|
const turn = event.data.turn;
|
|
@@ -567,7 +539,7 @@ function turnOf(event) {
|
|
|
567
539
|
}
|
|
568
540
|
/** A fresh, empty turn accumulator. */
|
|
569
541
|
function emptyTurn(turn) {
|
|
570
|
-
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: []
|
|
542
|
+
return { turn, user: [], assistant: [], tools: new Map(), resultBytes: 0, errors: [], writes: [] };
|
|
571
543
|
}
|
|
572
544
|
/**
|
|
573
545
|
* Group append-origin surface events into per-turn digest material.
|
|
@@ -601,8 +573,6 @@ export function buildSections(events) {
|
|
|
601
573
|
// objective on every round, so recording each copy would repeat it dozens of
|
|
602
574
|
// times across a long autonomous run.
|
|
603
575
|
const recordedObjectives = new Set();
|
|
604
|
-
// Question text by tool-call id, so the matching result can be read as its answer.
|
|
605
|
-
const pendingQuestions = new Map();
|
|
606
576
|
for (const event of events) {
|
|
607
577
|
if (event.type === 'turn/start') {
|
|
608
578
|
const turn = turnOf(event);
|
|
@@ -633,16 +603,6 @@ export function buildSections(events) {
|
|
|
633
603
|
const text = textOf(event);
|
|
634
604
|
currentTurn.resultBytes += text.length;
|
|
635
605
|
const message = messageOf(event);
|
|
636
|
-
// A question's own result is its answer. Recorded here rather than left to the
|
|
637
|
-
// model, which dropped or reworded it depending on its heading style.
|
|
638
|
-
const callId = message?.toolCallId;
|
|
639
|
-
if (callId !== undefined) {
|
|
640
|
-
const question = pendingQuestions.get(callId);
|
|
641
|
-
if (question !== undefined) {
|
|
642
|
-
pendingQuestions.delete(callId);
|
|
643
|
-
currentTurn.asked.push({ question, answer: answerTextOf(text) ?? '(no answer recorded)' });
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
606
|
if (message?.isError === true) {
|
|
647
607
|
const firstLine = text.split('\n')[0] ?? '';
|
|
648
608
|
currentTurn.errors.push(firstLine.slice(0, 100));
|
|
@@ -683,11 +643,6 @@ export function buildSections(events) {
|
|
|
683
643
|
if (text.length > 0)
|
|
684
644
|
currentTurn.assistant.push(text);
|
|
685
645
|
for (const call of toolCallsOf(event)) {
|
|
686
|
-
if (call.name === 'ask_user_question') {
|
|
687
|
-
const question = questionTextOf(call.arguments);
|
|
688
|
-
if (question !== undefined)
|
|
689
|
-
pendingQuestions.set(call.id, question);
|
|
690
|
-
}
|
|
691
646
|
currentTurn.tools.set(call.name, (currentTurn.tools.get(call.name) ?? 0) + 1);
|
|
692
647
|
if (!WRITE_TOOLS.has(call.name))
|
|
693
648
|
continue;
|
|
@@ -774,47 +729,6 @@ function materialKey(events) {
|
|
|
774
729
|
* The tool pointer line stays either way: it is one line, and it is the only
|
|
775
730
|
* record of what was actually run when the prose and extraction both say little.
|
|
776
731
|
*/
|
|
777
|
-
/**
|
|
778
|
-
* Move the `## Asked` section to the end of one extraction block.
|
|
779
|
-
*
|
|
780
|
-
* The digest reads as a conversation, and the user's next message is often just
|
|
781
|
-
* "8399" or "继续". That reply only makes sense beside the question it answers, so
|
|
782
|
-
* Asked belongs at the bottom of the block. The model puts it first about as often
|
|
783
|
-
* as last, so the order is fixed here rather than requested in the prompt.
|
|
784
|
-
*
|
|
785
|
-
* @param block - one extraction block, possibly malformed.
|
|
786
|
-
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
787
|
-
*/
|
|
788
|
-
export function moveAskedLast(block, asked = []) {
|
|
789
|
-
const lines = block.split('\n');
|
|
790
|
-
// Recognise every form the model has used for this section, in both languages, so
|
|
791
|
-
// it can be dropped. The content is regenerated from the log instead: four format
|
|
792
|
-
// changes across runs each broke either the position or the wording, and the log
|
|
793
|
-
// already holds both halves verbatim.
|
|
794
|
-
const isAsked = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]?\s*$/i.test(line);
|
|
795
|
-
const isAskedInline = (line) => /^\s*(?:[-*]\s+)?(?:\*\*)?(?:Asked|问)(?:\*\*)?\s*[::]/i.test(line);
|
|
796
|
-
const isOther = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(?:Did|Concluded|Overturned|做|结论|推翻)(?:\*\*)?\s*[::]?/i.test(line);
|
|
797
|
-
const start = lines.findIndex((line) => isAsked(line) || isAskedInline(line));
|
|
798
|
-
const kept = [...lines];
|
|
799
|
-
if (start >= 0) {
|
|
800
|
-
const inline = isAskedInline(kept[start] ?? '') && !isAsked(kept[start] ?? '');
|
|
801
|
-
let end = start + 1;
|
|
802
|
-
if (!inline) {
|
|
803
|
-
while (end < kept.length && !isOther(kept[end] ?? ''))
|
|
804
|
-
end += 1;
|
|
805
|
-
}
|
|
806
|
-
kept.splice(start, end - start);
|
|
807
|
-
}
|
|
808
|
-
while (kept.length > 0 && (kept.at(-1) ?? '').trim().length === 0)
|
|
809
|
-
kept.pop();
|
|
810
|
-
if (asked.length === 0)
|
|
811
|
-
return kept.join('\n');
|
|
812
|
-
const block_lines = asked.map(({ question, answer }) => {
|
|
813
|
-
const q = question.split('\n').join(' ');
|
|
814
|
-
return `- ${q} → ${answer}`;
|
|
815
|
-
});
|
|
816
|
-
return [...kept, '', '## Asked', ...block_lines].join('\n');
|
|
817
|
-
}
|
|
818
732
|
function renderSection(section, capChars) {
|
|
819
733
|
const lines = [];
|
|
820
734
|
for (const turn of section.turns) {
|
|
@@ -827,18 +741,17 @@ function renderSection(section, capChars) {
|
|
|
827
741
|
if (capChars !== undefined && block.length > capChars) {
|
|
828
742
|
block = `${block.slice(0, capChars)}\n…(+${block.length - capChars} chars)`;
|
|
829
743
|
}
|
|
830
|
-
//
|
|
831
|
-
//
|
|
832
|
-
//
|
|
833
|
-
// — see moveAskedLast being applied after the trailer is joined.
|
|
744
|
+
// The trailer goes *above* the block, not below it. `## Asked` is the block's
|
|
745
|
+
// last section and the user's reply is the very next line, so an errors line
|
|
746
|
+
// in between would separate the reply from the question it answers.
|
|
834
747
|
const trailer = [];
|
|
835
748
|
if (turn.errors.length > 0) {
|
|
836
749
|
trailer.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
837
750
|
}
|
|
838
751
|
if (turn.writes.length > 0)
|
|
839
752
|
trailer.push(` wrote: ${turn.writes.join(', ')}`);
|
|
840
|
-
const body = trailer.length > 0 ? `${
|
|
841
|
-
lines.push(`### assistant\n${
|
|
753
|
+
const body = trailer.length > 0 ? `${trailer.join('\n')}\n${block}` : block;
|
|
754
|
+
lines.push(`### assistant\n${body}`);
|
|
842
755
|
}
|
|
843
756
|
else {
|
|
844
757
|
let body = turn.assistant.join('\n');
|
package/lib/types/index.d.ts
CHANGED
|
@@ -85,6 +85,36 @@ export interface Extraction {
|
|
|
85
85
|
did: string[];
|
|
86
86
|
concluded: string[];
|
|
87
87
|
overturned: string[];
|
|
88
|
+
/**
|
|
89
|
+
* What this turn asked the user and what came back, when it asked anything.
|
|
90
|
+
*
|
|
91
|
+
* Written by the model rather than read off the log. The two cases the log
|
|
92
|
+
* cannot cover are the reason:
|
|
93
|
+
*
|
|
94
|
+
* - A question is often not in the last line. Over 231 real turns that drew a
|
|
95
|
+
* short reply, 34% had prose after the question (p90 = 7 paragraphs, max
|
|
96
|
+
* 24) — a `## 需要你决定` block followed by `## 还没验证的`. The log records
|
|
97
|
+
* text, not which paragraph was the question.
|
|
98
|
+
* - A bare `1` is meaningless without the list it selects from, and that list
|
|
99
|
+
* is usually a paragraph or more above the question, under its own heading.
|
|
100
|
+
* Reading it out needs judgement about which list the question refers to.
|
|
101
|
+
*
|
|
102
|
+
* The model already reads the same prose to fill `did`/`concluded`, so this
|
|
103
|
+
* costs one more field rather than another pass.
|
|
104
|
+
*/
|
|
105
|
+
asked: AskedPair[];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* One question the assistant put to the user, with the options it offered.
|
|
109
|
+
*
|
|
110
|
+
* No `answer` field: the reply is the next section's `## user` line, already in the
|
|
111
|
+
* digest verbatim. Carrying it here as well printed the same sentence twice.
|
|
112
|
+
*/
|
|
113
|
+
export interface AskedPair {
|
|
114
|
+
/** The question, one line. */
|
|
115
|
+
question: string;
|
|
116
|
+
/** The options as offered, each one line; empty when the question offered none. */
|
|
117
|
+
options: string[];
|
|
88
118
|
}
|
|
89
119
|
/**
|
|
90
120
|
* Parse the model's JSON reply into per-slot extractions.
|
|
@@ -131,17 +161,6 @@ interface Turn {
|
|
|
131
161
|
resultBytes: number;
|
|
132
162
|
errors: string[];
|
|
133
163
|
writes: string[];
|
|
134
|
-
/**
|
|
135
|
-
* Question/answer pairs the assistant put to the user in this turn.
|
|
136
|
-
*
|
|
137
|
-
* Rendered by code rather than by the model: the model changed its Asked format
|
|
138
|
-
* four times across runs, and each change silently broke the position or the
|
|
139
|
-
* content. Both halves are in the log, so there is nothing to summarise.
|
|
140
|
-
*/
|
|
141
|
-
asked: {
|
|
142
|
-
question: string;
|
|
143
|
-
answer: string;
|
|
144
|
-
}[];
|
|
145
164
|
/**
|
|
146
165
|
* The extracted summary for this turn, when extraction succeeded.
|
|
147
166
|
*
|
|
@@ -168,32 +187,6 @@ interface Section {
|
|
|
168
187
|
* @returns one section per non-injected user message.
|
|
169
188
|
*/
|
|
170
189
|
export declare function buildSections(events: readonly SessionEvent[]): Section[];
|
|
171
|
-
/**
|
|
172
|
-
* Render one section to digest text, truncating assistant prose to `capChars`.
|
|
173
|
-
*
|
|
174
|
-
* When the turn carries an extraction, that extraction *replaces* the assistant
|
|
175
|
-
* prose. Keeping both was the duplication this design removes: 58.2% of
|
|
176
|
-
* assistant characters are substantive conclusions that the extraction restates,
|
|
177
|
-
* and the doubled copy pushed real history out through the budget.
|
|
178
|
-
*
|
|
179
|
-
* The tool pointer line stays either way: it is one line, and it is the only
|
|
180
|
-
* record of what was actually run when the prose and extraction both say little.
|
|
181
|
-
*/
|
|
182
|
-
/**
|
|
183
|
-
* Move the `## Asked` section to the end of one extraction block.
|
|
184
|
-
*
|
|
185
|
-
* The digest reads as a conversation, and the user's next message is often just
|
|
186
|
-
* "8399" or "继续". That reply only makes sense beside the question it answers, so
|
|
187
|
-
* Asked belongs at the bottom of the block. The model puts it first about as often
|
|
188
|
-
* as last, so the order is fixed here rather than requested in the prompt.
|
|
189
|
-
*
|
|
190
|
-
* @param block - one extraction block, possibly malformed.
|
|
191
|
-
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
192
|
-
*/
|
|
193
|
-
export declare function moveAskedLast(block: string, asked?: readonly {
|
|
194
|
-
question: string;
|
|
195
|
-
answer: string;
|
|
196
|
-
}[]): string;
|
|
197
190
|
/**
|
|
198
191
|
* Render the digest under a hard character budget.
|
|
199
192
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.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",
|