@logictan/dsh-verbatim-compaction 0.6.1 → 0.8.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 +11 -1
- package/lib/index.js +226 -45
- package/lib/types/index.d.ts +24 -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
|
-
|
|
65
|
+
空节直接删掉,不留占位。占位符既包括 `(none)`/`n/a`/`无`,也包括模型爱写的 `未提问。`;判据同时覆盖标题行(`## Asked`、裸写 `Asked`)和行内形式(`- Did: …`、`**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,6 +82,16 @@
|
|
|
82
82
|
|
|
83
83
|
缓存的键是提取材料的哈希(用户请求 + 助手正文 + 工具轨迹),不是 slot 位置:位置会随历史增长而错位,内容变了哈希就变,不会读到过期块。缓存是进程内的,重启即失效——代价只是重新提取一次,与加缓存前行为一致。
|
|
84
84
|
|
|
85
|
+
`## Asked` 记的是「助手问了什么 + 用户选了什么」。原来指令写「record the question, never the answer」——本意是防模型把用户请求抄进这一节,却被照字面执行:实测材料里两条 `ANSWER` 都在(其中一条把整个设计方向改成「游戏化、赛博朋克、提取 3D 模型」),模型只写了问题。
|
|
86
|
+
|
|
87
|
+
`## Asked` 由代码移到最后(`moveAskedLast`),因为用户的回复常常只有「8399」「继续」,只有紧接在提问后面才读得懂。模型用过三种写法——`## Asked`、`**Asked**:`、`- Asked:`——三种都认。
|
|
88
|
+
|
|
89
|
+
`ask_user_question` 的问题与用户选择都会进材料。它没有 `description` 参数,载荷在 `questions[]`,原来只留下 `TOOL ask_user_question` 五个字——问题是什么、用户选了什么全丢。答案在工具结果里(`answers[].selected`,或用户自己打字时的 `answers[].custom`),实测 31 次提问里 10 次是打字回答。
|
|
90
|
+
|
|
91
|
+
一个 section 就是一次请求、一个 turn。日志会在同一次请求里推进 turn 号(实测一个请求的 7 条助手消息记作 turn 2、另 9 条记作 turn 3),按号码切分会把一个请求拆成「空请求 turn + 无用户消息的回复 turn」,摘要里就出现两个 `### assistant`。用户消息本身不带 turn 字段,这条也曾让两者永远对不上。
|
|
92
|
+
|
|
93
|
+
一个 section 只提取一次,提取块写给该 section 的第一个 turn——如果每个 turn 都拿到同一份事件,模型会对同一段材料答两遍(实测出现过两个完全相同的 `## Concluded` 块和 `(duplicate of SLOT 4:0)` 占位)。
|
|
94
|
+
|
|
85
95
|
提取按批调用:每 8 轮一次,一轮输出一个 `=== TURN n ===` 块。早期实现是每轮一次调用,122 轮就是 131 次往返,单轮 400 token 的输出上限乘以 131 就可能超过整个摘要上限。批处理把往返压到约 17 次,并共享 prompt 前缀以复用 provider 的 KV cache。
|
|
86
96
|
|
|
87
97
|
用户原话在布局上**先占额度**,提取块不得挤占。实测那个 122 轮会话:区间内真人原话共 469 字符(117 token),而每轮一块 420 字符的提取共 55020 字符——提取是正文的 117 倍,任何让二者竞争的布局都会把正文挤掉,而正文是「请用pixi管理开发环境」这类永不过期的约束,提取描述的却是已经做完的工作。
|
package/lib/index.js
CHANGED
|
@@ -159,10 +159,62 @@ function toolCallsOf(event) {
|
|
|
159
159
|
const calls = [];
|
|
160
160
|
for (const block of message.content) {
|
|
161
161
|
if (block.type === 'tool-call')
|
|
162
|
-
calls.push({ name: block.name, arguments: block.arguments });
|
|
162
|
+
calls.push({ id: block.id, name: block.name, arguments: block.arguments });
|
|
163
163
|
}
|
|
164
164
|
return calls;
|
|
165
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* The answer the user gave to one `ask_user_question` call, or undefined.
|
|
168
|
+
*
|
|
169
|
+
* The answer is the tool's own result — `{"answers":[{"id":"…","selected":[…]}]}`
|
|
170
|
+
* — not an error, so without this the result was skipped and the only record of
|
|
171
|
+
* what the user chose never reached the extractor. The user's next message is
|
|
172
|
+
* often just "1" or "继续", which is unreadable without the question beside it.
|
|
173
|
+
*
|
|
174
|
+
* @param event - any event; non-results and unrelated results yield undefined.
|
|
175
|
+
* @param askedIds - tool-call ids of this turn's questions.
|
|
176
|
+
* @returns the chosen labels, or undefined when this result answers no question.
|
|
177
|
+
*/
|
|
178
|
+
function answerOf(event, askedIds) {
|
|
179
|
+
if (askedIds.size === 0)
|
|
180
|
+
return undefined;
|
|
181
|
+
const message = messageOf(event);
|
|
182
|
+
if (message === null)
|
|
183
|
+
return undefined;
|
|
184
|
+
const callId = message.toolCallId;
|
|
185
|
+
if (callId === undefined || !askedIds.has(callId))
|
|
186
|
+
return undefined;
|
|
187
|
+
const text = textOf(event);
|
|
188
|
+
let parsed;
|
|
189
|
+
try {
|
|
190
|
+
parsed = JSON.parse(text);
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
196
|
+
return undefined;
|
|
197
|
+
const answers = parsed['answers'];
|
|
198
|
+
if (!Array.isArray(answers))
|
|
199
|
+
return undefined;
|
|
200
|
+
const chosen = answers
|
|
201
|
+
.flatMap((answer) => {
|
|
202
|
+
if (typeof answer !== 'object' || answer === null)
|
|
203
|
+
return [];
|
|
204
|
+
const record = answer;
|
|
205
|
+
const selected = record['selected'];
|
|
206
|
+
const picked = Array.isArray(selected) ? selected.map((value) => String(value)) : [];
|
|
207
|
+
// A typed answer arrives with `selected: []` and the text in `custom`. Ten
|
|
208
|
+
// of 31 real questions were answered this way, and those carried the user's
|
|
209
|
+
// actual observations rather than a menu label.
|
|
210
|
+
const typed = typeof record['custom'] === 'string' ? [record['custom']] : [];
|
|
211
|
+
return [...picked, ...typed];
|
|
212
|
+
})
|
|
213
|
+
.filter((value) => value.length > 0);
|
|
214
|
+
if (chosen.length === 0)
|
|
215
|
+
return undefined;
|
|
216
|
+
return chosen.join(' / ').replace(/\s+/g, ' ').slice(0, HINT_CHARS * 2);
|
|
217
|
+
}
|
|
166
218
|
/**
|
|
167
219
|
* Whether one tool result is a failure.
|
|
168
220
|
*
|
|
@@ -195,40 +247,56 @@ export function isErrorResult(event) {
|
|
|
195
247
|
* @returns the same text with empty sections removed, or an empty string when
|
|
196
248
|
* nothing substantive remains.
|
|
197
249
|
*/
|
|
198
|
-
export function pruneEmptySections(
|
|
199
|
-
const lines =
|
|
250
|
+
export function pruneEmptySections(block) {
|
|
251
|
+
const lines = block.split('\n');
|
|
200
252
|
const kept = [];
|
|
201
|
-
|
|
202
|
-
|
|
253
|
+
// A section is one heading plus the lines under it. The heading may be a line of
|
|
254
|
+
// its own (`## Asked`, bare `Asked`) or carry its body inline (`- Did: …`,
|
|
255
|
+
// `**Asked**:…`). Both forms occur in real output, sometimes in the same block.
|
|
256
|
+
const headingOf = (line) => {
|
|
257
|
+
if (/^#{1,6}\s/.test(line))
|
|
258
|
+
return { own: '' };
|
|
259
|
+
if (/^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned)(?:\*\*)?\s*[::]?\s*$/i.test(line)) {
|
|
260
|
+
return { own: '' };
|
|
261
|
+
}
|
|
262
|
+
const inline = /^\s*(?:[-*]\s+)?(?:\*\*)?(Asked|Did|Concluded|Overturned)(?:\*\*)?\s*[::]\s*(.*)$/i.exec(line);
|
|
263
|
+
if (inline !== null)
|
|
264
|
+
return { own: inline[2] ?? '' };
|
|
265
|
+
return undefined;
|
|
266
|
+
};
|
|
267
|
+
// Placeholders for "nothing here" that models actually write. `未提问` came from a
|
|
268
|
+
// real run and cost a line on every turn that asked no question.
|
|
269
|
+
const placeholder = (text) => {
|
|
270
|
+
const trimmed = text.trim();
|
|
271
|
+
if (trimmed.length === 0)
|
|
272
|
+
return true;
|
|
273
|
+
if (/^(?:\(?none\)?|n\/a|na|-{1,2}|无|沒有|没有)$/i.test(trimmed))
|
|
274
|
+
return true;
|
|
275
|
+
return /^(?:未提问|无提问|没有提问|未提出|no questions? (?:were |was )?asked|none asked|no question)[。.!!]*$/i.test(trimmed);
|
|
276
|
+
};
|
|
277
|
+
let heading;
|
|
278
|
+
let under = [];
|
|
203
279
|
const flush = () => {
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
280
|
+
if (heading !== undefined) {
|
|
281
|
+
const own = headingOf(heading)?.own ?? '';
|
|
282
|
+
const substantive = (own.length > 0 && !placeholder(own))
|
|
283
|
+
|| under.some((line) => !placeholder(line.replace(/^\s*(?:[-*]|\d+\.)\s*/, '')));
|
|
284
|
+
if (substantive)
|
|
285
|
+
kept.push(heading, ...under);
|
|
208
286
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
});
|
|
215
|
-
if (substantive)
|
|
216
|
-
kept.push(pendingHeading, ...pendingBody);
|
|
217
|
-
pendingHeading = undefined;
|
|
218
|
-
pendingBody = [];
|
|
287
|
+
else {
|
|
288
|
+
kept.push(...under);
|
|
289
|
+
}
|
|
290
|
+
heading = undefined;
|
|
291
|
+
under = [];
|
|
219
292
|
};
|
|
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;
|
|
225
293
|
for (const line of lines) {
|
|
226
|
-
if (
|
|
294
|
+
if (headingOf(line) !== undefined) {
|
|
227
295
|
flush();
|
|
228
|
-
|
|
296
|
+
heading = line;
|
|
229
297
|
continue;
|
|
230
298
|
}
|
|
231
|
-
|
|
299
|
+
under.push(line);
|
|
232
300
|
}
|
|
233
301
|
flush();
|
|
234
302
|
return kept.join('\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
@@ -241,9 +309,10 @@ const EXTRACT_INSTRUCTION = [
|
|
|
241
309
|
'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.',
|
|
242
310
|
'',
|
|
243
311
|
'## Asked',
|
|
244
|
-
'- what the ASSISTANT asked the USER in this turn
|
|
245
|
-
'- the
|
|
246
|
-
'-
|
|
312
|
+
'- what the ASSISTANT asked the USER in this turn, and what the user chose',
|
|
313
|
+
'- the trace shows an `ANSWER:` line for a question the user answered, including answers they typed instead of picking an option. Record it: a later turn cannot rebuild which option was chosen, and a typed answer often redirects the whole task',
|
|
314
|
+
'- the user may also answer in their next message, which appears in the digest as plain text. Prefer the `ANSWER:` line when both exist',
|
|
315
|
+
'- 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. Recording the answer is not copying the request — the answer is the user replying to your question.',
|
|
247
316
|
'',
|
|
248
317
|
'## Did',
|
|
249
318
|
'- what was actually done, concretely',
|
|
@@ -255,6 +324,11 @@ const EXTRACT_INSTRUCTION = [
|
|
|
255
324
|
'- earlier conclusions this turn disproved or replaced',
|
|
256
325
|
'',
|
|
257
326
|
'Do not describe the tool calls themselves ("ran bash", "explored the repo"). State what came out of them. Do not repeat a conclusion the previous turn already stated. If the trace is too thin to support a claim, write "(none)".',
|
|
327
|
+
'',
|
|
328
|
+
// Measured on a real 32-slot session: whole batches came back in English while
|
|
329
|
+
// the material was Chinese, so the digest mixed two languages for one
|
|
330
|
+
// conversation.
|
|
331
|
+
'Write in the same language as the material. A session whose requests are Chinese gets Chinese blocks; do not switch to English.',
|
|
258
332
|
].join('\n');
|
|
259
333
|
/** Turns handed to one extraction call. */
|
|
260
334
|
const TURNS_PER_CALL = 8;
|
|
@@ -284,6 +358,35 @@ function hintOf(argumentsJson) {
|
|
|
284
358
|
catch {
|
|
285
359
|
return '';
|
|
286
360
|
}
|
|
361
|
+
// `ask_user_question` carries `questions[]`, not a `description`. Without this
|
|
362
|
+
// branch the hint was the bare tool name, so the extractor never saw what was
|
|
363
|
+
// asked and `## Asked` came out empty or wrong on every turn that used it.
|
|
364
|
+
const questions = parsed['questions'];
|
|
365
|
+
if (Array.isArray(questions)) {
|
|
366
|
+
const asked = questions
|
|
367
|
+
.map((question) => {
|
|
368
|
+
if (typeof question !== 'object' || question === null)
|
|
369
|
+
return '';
|
|
370
|
+
const record = question;
|
|
371
|
+
const text = typeof record['question'] === 'string'
|
|
372
|
+
? record['question']
|
|
373
|
+
: typeof record['header'] === 'string' ? record['header'] : '';
|
|
374
|
+
const options = Array.isArray(record['options'])
|
|
375
|
+
? record['options']
|
|
376
|
+
.map((option) => (typeof option === 'object' && option !== null
|
|
377
|
+
? String(option['label'] ?? '')
|
|
378
|
+
: ''))
|
|
379
|
+
.filter((label) => label.length > 0)
|
|
380
|
+
: [];
|
|
381
|
+
if (text.length === 0)
|
|
382
|
+
return '';
|
|
383
|
+
return options.length === 0 ? text : `${text} [options: ${options.join(' / ')}]`;
|
|
384
|
+
})
|
|
385
|
+
.filter((text) => text.length > 0)
|
|
386
|
+
.join(' | ');
|
|
387
|
+
if (asked.length > 0)
|
|
388
|
+
return asked.replace(/\s+/g, ' ').slice(0, HINT_CHARS * 2);
|
|
389
|
+
}
|
|
287
390
|
for (const key of ['description', 'command', 'query', 'file_path', 'path', 'pattern']) {
|
|
288
391
|
const candidate = parsed[key];
|
|
289
392
|
if (typeof candidate === 'string' && candidate.trim().length > 0) {
|
|
@@ -310,6 +413,9 @@ function hintOf(argumentsJson) {
|
|
|
310
413
|
export function collectHints(events) {
|
|
311
414
|
const lines = [];
|
|
312
415
|
let calls = 0;
|
|
416
|
+
// Tool-call ids of this turn's questions, so the matching result can be read as
|
|
417
|
+
// an answer rather than being skipped as a non-error result.
|
|
418
|
+
const askedIds = new Set();
|
|
313
419
|
for (const event of events) {
|
|
314
420
|
// Tool calls are blocks inside `assistant/message`, not standalone events:
|
|
315
421
|
// `digestSource` emits no `tool/call` type at all, so selecting on that type
|
|
@@ -321,11 +427,21 @@ export function collectHints(events) {
|
|
|
321
427
|
calls += 1;
|
|
322
428
|
const hint = hintOf(call.arguments);
|
|
323
429
|
lines.push(hint.length > 0 ? `TOOL ${call.name}: ${hint}` : `TOOL ${call.name}`);
|
|
430
|
+
if (call.name === 'ask_user_question')
|
|
431
|
+
askedIds.add(call.id);
|
|
324
432
|
}
|
|
325
433
|
continue;
|
|
326
434
|
}
|
|
327
435
|
if (event.type !== 'tool/result')
|
|
328
436
|
continue;
|
|
437
|
+
// The answer to a question is the tool's own result, not an error. It is the
|
|
438
|
+
// only record of what the user chose, and the next user message is often just
|
|
439
|
+
// "1" — so it has to travel with the question.
|
|
440
|
+
const answered = answerOf(event, askedIds);
|
|
441
|
+
if (answered !== undefined) {
|
|
442
|
+
lines.push(`ANSWER: ${answered}`);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
329
445
|
if (!isErrorResult(event))
|
|
330
446
|
continue;
|
|
331
447
|
const firstLine = textOf(event).split('\n')[0] ?? '';
|
|
@@ -442,9 +558,19 @@ export function buildSections(events) {
|
|
|
442
558
|
currentTurn.user.push(text);
|
|
443
559
|
continue;
|
|
444
560
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
561
|
+
// A user message carries no `turn` field while the reply carries one, so a
|
|
562
|
+
// plain turn-number comparison opened a second turn for the same request: every
|
|
563
|
+
// real section ended up with an empty request turn plus a reply turn holding no
|
|
564
|
+
// user text, and the digest printed `## user` and `### assistant` twice.
|
|
565
|
+
//
|
|
566
|
+
// The turn number only separates work when the current turn already holds some,
|
|
567
|
+
// i.e. when the log really moved on to a later turn.
|
|
568
|
+
// A section is one request, so it is one turn. The log keeps advancing the turn
|
|
569
|
+
// number inside a single request (7 assistant messages as turn 2, then 9 more as
|
|
570
|
+
// turn 3), and splitting on the number gave every section a second turn whose
|
|
571
|
+
// block was the tail of the same request's prose.
|
|
572
|
+
if (currentTurn === undefined)
|
|
573
|
+
currentTurn = openTurn(eventTurn ?? lastTurn);
|
|
448
574
|
const text = textOf(event);
|
|
449
575
|
if (text.length > 0)
|
|
450
576
|
currentTurn.assistant.push(text);
|
|
@@ -535,6 +661,44 @@ function materialKey(events) {
|
|
|
535
661
|
* The tool pointer line stays either way: it is one line, and it is the only
|
|
536
662
|
* record of what was actually run when the prose and extraction both say little.
|
|
537
663
|
*/
|
|
664
|
+
/**
|
|
665
|
+
* Move the `## Asked` section to the end of one extraction block.
|
|
666
|
+
*
|
|
667
|
+
* The digest reads as a conversation, and the user's next message is often just
|
|
668
|
+
* "8399" or "继续". That reply only makes sense beside the question it answers, so
|
|
669
|
+
* Asked belongs at the bottom of the block. The model puts it first about as often
|
|
670
|
+
* as last, so the order is fixed here rather than requested in the prompt.
|
|
671
|
+
*
|
|
672
|
+
* @param block - one extraction block, possibly malformed.
|
|
673
|
+
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
674
|
+
*/
|
|
675
|
+
export function moveAskedLast(block) {
|
|
676
|
+
const lines = block.split('\n');
|
|
677
|
+
// Three heading forms observed from the same model: `## Asked`, `**Asked**:` and
|
|
678
|
+
// `- Asked:`. Recognising only the first left Asked at the top whenever the model
|
|
679
|
+
// changed style.
|
|
680
|
+
const isAsked = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?Asked(?:\*\*)?\s*[::]?\s*$/i.test(line);
|
|
681
|
+
const isAskedInline = (line) => /^\s*(?:[-*]\s+)?(?:\*\*)?Asked(?:\*\*)?\s*[::]/i.test(line);
|
|
682
|
+
const isOther = (line) => /^\s*(?:#{0,6}\s*|[-*]\s+)?(?:\*\*)?(Did|Concluded|Overturned)(?:\*\*)?\s*[::]?/i.test(line);
|
|
683
|
+
const start = lines.findIndex((line) => isAsked(line) || isAskedInline(line));
|
|
684
|
+
if (start < 0)
|
|
685
|
+
return block;
|
|
686
|
+
// A heading form owns the lines until the next heading; an inline `- Asked: x`
|
|
687
|
+
// form owns only its own line.
|
|
688
|
+
const inline = isAskedInline(lines[start] ?? '') && !isAsked(lines[start] ?? '');
|
|
689
|
+
let end = start + 1;
|
|
690
|
+
if (!inline) {
|
|
691
|
+
while (end < lines.length && !isOther(lines[end] ?? ''))
|
|
692
|
+
end += 1;
|
|
693
|
+
}
|
|
694
|
+
const asked = lines.slice(start, end);
|
|
695
|
+
while (asked.length > 0 && (asked.at(-1) ?? '').trim().length === 0)
|
|
696
|
+
asked.pop();
|
|
697
|
+
const rest = [...lines.slice(0, start), ...lines.slice(end)];
|
|
698
|
+
while (rest.length > 0 && (rest.at(-1) ?? '').trim().length === 0)
|
|
699
|
+
rest.pop();
|
|
700
|
+
return [...rest, '', ...asked].join('\n');
|
|
701
|
+
}
|
|
538
702
|
function renderSection(section, capChars) {
|
|
539
703
|
const lines = [];
|
|
540
704
|
for (const turn of section.turns) {
|
|
@@ -545,7 +709,18 @@ function renderSection(section, capChars) {
|
|
|
545
709
|
if (capChars !== undefined && block.length > capChars) {
|
|
546
710
|
block = `${block.slice(0, capChars)}\n…(+${block.length - capChars} chars)`;
|
|
547
711
|
}
|
|
548
|
-
|
|
712
|
+
// Errors and writes sit after the block: Asked has to be the last thing in the
|
|
713
|
+
// turn, and the errors line directly under `## user` read as if the user had
|
|
714
|
+
// said it. Asked is inside the block, so the trailer goes before Asked instead
|
|
715
|
+
// — see moveAskedLast being applied after the trailer is joined.
|
|
716
|
+
const trailer = [];
|
|
717
|
+
if (turn.errors.length > 0) {
|
|
718
|
+
trailer.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
719
|
+
}
|
|
720
|
+
if (turn.writes.length > 0)
|
|
721
|
+
trailer.push(` wrote: ${turn.writes.join(', ')}`);
|
|
722
|
+
const body = trailer.length > 0 ? `${block}\n${trailer.join('\n')}` : block;
|
|
723
|
+
lines.push(`### assistant\n${moveAskedLast(body)}`);
|
|
549
724
|
}
|
|
550
725
|
else {
|
|
551
726
|
let body = turn.assistant.join('\n');
|
|
@@ -554,12 +729,12 @@ function renderSection(section, capChars) {
|
|
|
554
729
|
}
|
|
555
730
|
if (body.length > 0)
|
|
556
731
|
lines.push(`### assistant\n${body}`);
|
|
732
|
+
if (turn.errors.length > 0) {
|
|
733
|
+
lines.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
734
|
+
}
|
|
735
|
+
if (turn.writes.length > 0)
|
|
736
|
+
lines.push(` wrote: ${turn.writes.join(', ')}`);
|
|
557
737
|
}
|
|
558
|
-
if (turn.errors.length > 0) {
|
|
559
|
-
lines.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
560
|
-
}
|
|
561
|
-
if (turn.writes.length > 0)
|
|
562
|
-
lines.push(` wrote: ${turn.writes.join(', ')}`);
|
|
563
738
|
}
|
|
564
739
|
return lines.join('\n');
|
|
565
740
|
}
|
|
@@ -974,14 +1149,20 @@ export default class VerbatimCompactionEngine extends BasicCompactionEngine {
|
|
|
974
1149
|
if (sectionIndex >= 0)
|
|
975
1150
|
grouped[sectionIndex]?.push(event);
|
|
976
1151
|
}
|
|
1152
|
+
// One slot per section, addressed to the section's first turn.
|
|
1153
|
+
//
|
|
1154
|
+
// The material is about what the section did as a whole, so handing it to
|
|
1155
|
+
// every turn in the section made the model answer twice with the same block:
|
|
1156
|
+
// a real section holding two turns produced two identical `## Concluded`
|
|
1157
|
+
// bodies plus `(duplicate of SLOT 4:0)` placeholders in the digest. The extra
|
|
1158
|
+
// turns keep their prose and their own headings; only the first carries the
|
|
1159
|
+
// section's extraction.
|
|
977
1160
|
const slots = [];
|
|
978
1161
|
for (const [sectionIndex, section] of sections.entries()) {
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
slots.push({ key: turnKey(sectionIndex, turnIndex), turn: turn.turn, events });
|
|
984
|
-
}
|
|
1162
|
+
const first = section.turns[0];
|
|
1163
|
+
if (first === undefined)
|
|
1164
|
+
continue;
|
|
1165
|
+
slots.push({ key: turnKey(sectionIndex, 0), turn: first.turn, events: grouped[sectionIndex] ?? [] });
|
|
985
1166
|
}
|
|
986
1167
|
const extracted = new Map();
|
|
987
1168
|
const missing = [];
|
package/lib/types/index.d.ts
CHANGED
|
@@ -96,7 +96,7 @@ export declare function isErrorResult(event: SessionEvent): boolean;
|
|
|
96
96
|
* @returns the same text with empty sections removed, or an empty string when
|
|
97
97
|
* nothing substantive remains.
|
|
98
98
|
*/
|
|
99
|
-
export declare function pruneEmptySections(
|
|
99
|
+
export declare function pruneEmptySections(block: string): string;
|
|
100
100
|
/**
|
|
101
101
|
* Build the compact per-turn trace fed to the summarizer.
|
|
102
102
|
*
|
|
@@ -148,6 +148,29 @@ interface Section {
|
|
|
148
148
|
* @returns one section per non-injected user message.
|
|
149
149
|
*/
|
|
150
150
|
export declare function buildSections(events: readonly SessionEvent[]): Section[];
|
|
151
|
+
/**
|
|
152
|
+
* Render one section to digest text, truncating assistant prose to `capChars`.
|
|
153
|
+
*
|
|
154
|
+
* When the turn carries an extraction, that extraction *replaces* the assistant
|
|
155
|
+
* prose. Keeping both was the duplication this design removes: 58.2% of
|
|
156
|
+
* assistant characters are substantive conclusions that the extraction restates,
|
|
157
|
+
* and the doubled copy pushed real history out through the budget.
|
|
158
|
+
*
|
|
159
|
+
* The tool pointer line stays either way: it is one line, and it is the only
|
|
160
|
+
* record of what was actually run when the prose and extraction both say little.
|
|
161
|
+
*/
|
|
162
|
+
/**
|
|
163
|
+
* Move the `## Asked` section to the end of one extraction block.
|
|
164
|
+
*
|
|
165
|
+
* The digest reads as a conversation, and the user's next message is often just
|
|
166
|
+
* "8399" or "继续". That reply only makes sense beside the question it answers, so
|
|
167
|
+
* Asked belongs at the bottom of the block. The model puts it first about as often
|
|
168
|
+
* as last, so the order is fixed here rather than requested in the prompt.
|
|
169
|
+
*
|
|
170
|
+
* @param block - one extraction block, possibly malformed.
|
|
171
|
+
* @returns the same block with Asked last; unchanged when Asked is absent or already last.
|
|
172
|
+
*/
|
|
173
|
+
export declare function moveAskedLast(block: string): string;
|
|
151
174
|
/**
|
|
152
175
|
* Render the digest under a hard character budget.
|
|
153
176
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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",
|