@logictan/dsh-verbatim-compaction 0.7.0 → 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 CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
  所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description`,失败的调用另记一行首行错误)。模型输出固定的四节:`## Asked` / `## Did` / `## Concluded` / `## Overturned`。
64
64
 
65
- 空节直接删掉,不留 `(none)` 占位。判据同时接受 `## 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`、`无法连接,改用代理` 这类真内容照原样保留。
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,8 @@
82
82
 
83
83
  缓存的键是提取材料的哈希(用户请求 + 助手正文 + 工具轨迹),不是 slot 位置:位置会随历史增长而错位,内容变了哈希就变,不会读到过期块。缓存是进程内的,重启即失效——代价只是重新提取一次,与加缓存前行为一致。
84
84
 
85
+ `## Asked` 记的是「助手问了什么 + 用户选了什么」。原来指令写「record the question, never the answer」——本意是防模型把用户请求抄进这一节,却被照字面执行:实测材料里两条 `ANSWER` 都在(其中一条把整个设计方向改成「游戏化、赛博朋克、提取 3D 模型」),模型只写了问题。
86
+
85
87
  `## Asked` 由代码移到最后(`moveAskedLast`),因为用户的回复常常只有「8399」「继续」,只有紧接在提问后面才读得懂。模型用过三种写法——`## Asked`、`**Asked**:`、`- Asked:`——三种都认。
86
88
 
87
89
  `ask_user_question` 的问题与用户选择都会进材料。它没有 `description` 参数,载荷在 `questions[]`,原来只留下 `TOOL ask_user_question` 五个字——问题是什么、用户选了什么全丢。答案在工具结果里(`answers[].selected`,或用户自己打字时的 `answers[].custom`),实测 31 次提问里 10 次是打字回答。
package/lib/index.js CHANGED
@@ -247,40 +247,56 @@ export function isErrorResult(event) {
247
247
  * @returns the same text with empty sections removed, or an empty string when
248
248
  * nothing substantive remains.
249
249
  */
250
- export function pruneEmptySections(body) {
251
- const lines = body.split('\n');
250
+ export function pruneEmptySections(block) {
251
+ const lines = block.split('\n');
252
252
  const kept = [];
253
- let pendingHeading;
254
- let pendingBody = [];
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 = [];
255
279
  const flush = () => {
256
- if (pendingHeading === undefined) {
257
- kept.push(...pendingBody);
258
- pendingBody = [];
259
- return;
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);
260
286
  }
261
- const substantive = pendingBody.some((line) => {
262
- const text = line.replace(/^\s*(?:[-*]|\d+\.)\s*/, '').trim();
263
- if (text.length === 0)
264
- return false;
265
- return !/^(?:\(?none\)?|n\/a|na|-{1,2}|无|沒有|没有)$/i.test(text);
266
- });
267
- if (substantive)
268
- kept.push(pendingHeading, ...pendingBody);
269
- pendingHeading = undefined;
270
- pendingBody = [];
287
+ else {
288
+ kept.push(...under);
289
+ }
290
+ heading = undefined;
291
+ under = [];
271
292
  };
272
- // A heading is either `## Name` or the bare name on its own line. Models
273
- // sometimes answer without the `##`; a real run returned bare `Asked` / `Did` /
274
- // `Concluded` lines, so a `^#{1,6}`-only test left `Concluded\n- (none)` in the
275
- // digest as body text and the placeholder reached the reader.
276
- const known = /^(Asked|Did|Concluded|Overturned)$/i;
277
293
  for (const line of lines) {
278
- if (/^#{1,6}\s/.test(line) || known.test(line.trim())) {
294
+ if (headingOf(line) !== undefined) {
279
295
  flush();
280
- pendingHeading = line;
296
+ heading = line;
281
297
  continue;
282
298
  }
283
- pendingBody.push(line);
299
+ under.push(line);
284
300
  }
285
301
  flush();
286
302
  return kept.join('\n').replace(/\n{3,}/g, '\n\n').trim();
@@ -293,9 +309,10 @@ const EXTRACT_INSTRUCTION = [
293
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.',
294
310
  '',
295
311
  '## Asked',
296
- '- what the ASSISTANT asked the USER in this turn: the question it put, and the options it offered',
297
- '- the next user message is usually the answer, so record the question, never the answer',
298
- '- 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.',
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.',
299
316
  '',
300
317
  '## Did',
301
318
  '- what was actually done, concretely',
@@ -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(body: string): string;
99
+ export declare function pruneEmptySections(block: string): string;
100
100
  /**
101
101
  * Build the compact per-turn trace fed to the summarizer.
102
102
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logictan/dsh-verbatim-compaction",
3
- "version": "0.7.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",