@logictan/dsh-verbatim-compaction 0.12.1 → 0.13.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 +17 -9
- package/lib/index.js +193 -140
- package/lib/types/index.d.ts +25 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,18 +21,24 @@
|
|
|
21
21
|
(用户原话,逐字,永不改写)
|
|
22
22
|
|
|
23
23
|
### assistant
|
|
24
|
-
## Asked
|
|
25
|
-
- 是否要用 FULL 而不是 INCREMENTAL
|
|
26
24
|
## Did
|
|
27
25
|
- 实测 `auto_vacuum=FULL` 删除后自动缩容:19.61 MB → 0.01 MB
|
|
28
26
|
## Concluded
|
|
29
27
|
- 选 FULL:回收发生在宿主自己的 DELETE 里,不受 busy_timeout=0 影响
|
|
30
28
|
## Overturned
|
|
31
29
|
- 先前认为必须显式执行 VACUUM 才能回收空间
|
|
32
|
-
errors: 1: Error: 命令超过 600 秒未结束,已断开连接
|
|
33
30
|
wrote: /Volumes/LogicExt/Git/BookReader/src/video.js
|
|
31
|
+
## Answers
|
|
32
|
+
- 验证码已发出,请输入手机上收到的验证码
|
|
33
|
+
→ 现在试(推荐)
|
|
34
|
+
## Asked
|
|
35
|
+
- 是否要用 FULL 而不是 INCREMENTAL
|
|
36
|
+
1. 用 FULL
|
|
37
|
+
2. 用 INCREMENTAL
|
|
34
38
|
```
|
|
35
39
|
|
|
40
|
+
`## Answers` 是走 `ask_user_question` 的问答,由代码从日志渲染;`## Asked` 是模型从正文里认出来的提问。两者并列,各管一半——`## Asked` 永远在最后,因为它的用户回复就是下一行。
|
|
41
|
+
|
|
36
42
|
标题不带轮次号:日志里的 `turn` 号既不唯一也不是一轮一个(一个真实会话把两条请求都编号成 1),印出来就是会撒谎的标题。
|
|
37
43
|
|
|
38
44
|
- **用户原话逐字**,不重写、不改写、不摘要。
|
|
@@ -60,7 +66,7 @@
|
|
|
60
66
|
|
|
61
67
|
只抄正文救不了「正文为空」的轮次。实测一个 13878 事件的会话:1973 条 assistant 消息里只有 317 条带正文,其中一轮跑了 342 次工具调用却一个字没写,纯逐字摘要把它压成一行 `tools: bash×281`——活干了,记录没了。
|
|
62
68
|
|
|
63
|
-
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description
|
|
69
|
+
所以提取的材料是三段:用户请求(逐字)、助手正文(逐字)、工具轨迹(每条调用的 `description`)。工具结果一条都不读:唯一曾被读出的东西是失败结果的首行,而那一行永远是传输或退出状态(`命令超过 600 秒未结束`),不是让失败有意义的那部分。模型输出四个字段:`asked` / `did` / `concluded` / `overturned`,标题与项目符号由渲染器拼装。
|
|
64
70
|
|
|
65
71
|
空节直接不出现:「没有内容」是一个空数组,不是需要事后识别的占位符。字段化之前是自由 markdown,模型写 `## Overturned\n- (none)` 就得在代码里再认一遍占位符,实测一份真实提取块里 `## Asked` 和 `## Overturned` 都是 `(none)`,删掉后 312 → 271 字符;按 130 轮算,这些占位合计约 900 token 只是在记录「什么都没发生」。现在这类占位在解析阶段就被滤掉,不再有一个专门的清理函数。`none of the tests passed`、`无法连接,改用代理` 这类真内容照原样保留。
|
|
66
72
|
|
|
@@ -74,7 +80,7 @@
|
|
|
74
80
|
|
|
75
81
|
三件事刻意做成这样:
|
|
76
82
|
|
|
77
|
-
-
|
|
83
|
+
- **工具轨迹不读工具结果**。曾经读失败结果的首行(先用 `/error/i` 匹配,347 条成功结果全部命中;改用 `data.isError` 后信号是对的,但那行文字从没被模型用上)。摘要里既没有 `ERR` 行,也没有 ` errors: N: …` 尾行。唯一的例外是 `ask_user_question` 的答案,它本身就是工具结果,由 `## Answers` 单独承载(见下)。
|
|
78
84
|
- **工具调用从 `assistant/message` 的 block 里取**。日志里没有独立的 `tool/call` 事件类型,按它过滤会一条都取不到。
|
|
79
85
|
- **提取失败就退回逐字正文**。模型不可达、超时、输出为空时,该轮保留原来的助手正文;压缩本身不能因此失败,否则会话死于上下文溢出,比丢细节更糟。降级是逐轮的:一批里某一轮没答,只有那一轮退回正文。**但降级不再无声**:整批拿不到可用 JSON、或单槽位在令牌上限被截断,都会打一条 `verbatim-compaction:` 开头的 warn,写明受影响的 slot key。没有这条日志时,插件退化成纯逐字摘要这件事在运行期完全不可观测——这正是「JSON 经常不生效却查不到」的成因。
|
|
80
86
|
|
|
@@ -96,15 +102,17 @@
|
|
|
96
102
|
|
|
97
103
|
**为什么不能只靠日志读。** 两件事需要判断,而不是取字段:其一,提问不在可靠的位置——231 个「引出了短回复」的真实轮次里,34% 的提问后面还有正文(p90 = 7 段,最多 24 段),形如 `## 需要你决定` 后面又跟一节 `## 还没验证的`;其二,选项通常也不在提问旁边——28 个纯数字回复的轮次里,28 个都提供过编号列表,但只有 26/231 轮那份列表落在提问本段或前一段,多数隔着自己的小标题在上面。要认出「这个提问指的是哪份列表」需要判断,日志里只有文字。
|
|
98
104
|
|
|
99
|
-
**`answer` 字段被明确去掉。**
|
|
105
|
+
**`answer` 字段被明确去掉。** 正文里提的问,回复就是下一节的 `## user` 行,逐字渲染;再写进 `asked.answer` 等于同一句话印两遍。指令里对应加了一条「不要报告用户的回复」,渲染上 `## Asked` 是块的最后一节,所以回复永远紧接在它下面。
|
|
106
|
+
|
|
107
|
+
**但「回复就在下一节 `## user`」只对正文提问成立。** 走 `ask_user_question` 的提问,用户是在工具自己的选择界面里作答的,答案变成**工具结果**,日志里根本没有 user 消息——实测一个真实会话:三个问题全部在界面里答完,相邻事件是 `tool/result`(`{"answers":[{"id","selected","custom"}]}`),没有 `user/message`,而连续两次检查点的摘要里问题和选择全都不在。所以工具问答由代码从日志确定性渲染成 `## Answers` 一节,与模型的 `asked` 并列、互不重叠:`ask_user_question` 不再进工具轨迹,模型也就无从复述它。
|
|
108
|
+
|
|
109
|
+
宿主还有第二种送达方式:用户在工具已经返回 pending 提示之后才答,宿主会补写一条 `user/message`,`source.kind` 是 `user-question-reply`,正文是同一份答案批次 JSON(连 `questions` 一起带上)。这条消息不是新请求,所以不另起 section,答案照样进 `## Answers`——它原本会被「只认 `user`/`goal`」的注入过滤整条丢掉。
|
|
100
110
|
|
|
101
111
|
`## Asked` 的位置由渲染器保证在最后,不靠模型。选项在问题下面缩进编号列出,`- 问题` 一行、` 1. …` 各行,用户的 `1` 因此有东西可指。
|
|
102
112
|
|
|
103
113
|
**选项自带的编号会被剥掉。** 模型会把源列表的 `1.` 一起抄进 `options`,渲染器再加一次编号就变成 `1. 1. …`;实测 5 次里 3 次如此,所以在解析阶段用正则剥掉,而不是只靠指令。
|
|
104
114
|
|
|
105
|
-
`ask_user_question`
|
|
106
|
-
|
|
107
|
-
`ask_user_question` 的问题与用户选择都会进材料。它没有 `description` 参数,载荷在 `questions[]`,原来只留下 `TOOL ask_user_question` 五个字——问题是什么、用户选了什么全丢。答案在工具结果里(`answers[].selected`,或用户自己打字时的 `answers[].custom`),实测 31 次提问里 10 次是打字回答。
|
|
115
|
+
`ask_user_question` 既不进工具轨迹、也不受 120 条调用上限约束,因为它的问答已经整组由代码渲染进 `## Answers`。答案取 `answers[].selected`;用户没选而是自己打字的,答案在 `answers[].custom`(实测 31 次提问里 10 次是打字回答),两条都渲染。
|
|
108
116
|
|
|
109
117
|
一个 section 就是一次请求、一个 turn。日志会在同一次请求里推进 turn 号(实测一个请求的 7 条助手消息记作 turn 2、另 9 条记作 turn 3),按号码切分会把一个请求拆成「空请求 turn + 无用户消息的回复 turn」,摘要里就出现两个 `### assistant`。用户消息本身不带 turn 字段,这条也曾让两者永远对不上。
|
|
110
118
|
|
package/lib/index.js
CHANGED
|
@@ -161,73 +161,108 @@ function toolCallsOf(event) {
|
|
|
161
161
|
}
|
|
162
162
|
return calls;
|
|
163
163
|
}
|
|
164
|
+
/** The `questions[]` payload of an `ask_user_question` call, as `{id → text}`. */
|
|
165
|
+
function questionTextsOf(argumentsJson) {
|
|
166
|
+
const out = new Map();
|
|
167
|
+
let parsed;
|
|
168
|
+
try {
|
|
169
|
+
parsed = JSON.parse(argumentsJson);
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
175
|
+
return out;
|
|
176
|
+
const questions = parsed['questions'];
|
|
177
|
+
if (!Array.isArray(questions))
|
|
178
|
+
return out;
|
|
179
|
+
for (const question of questions) {
|
|
180
|
+
if (typeof question !== 'object' || question === null)
|
|
181
|
+
continue;
|
|
182
|
+
const record = question;
|
|
183
|
+
const id = record['id'];
|
|
184
|
+
const text = typeof record['question'] === 'string' ? record['question'] : undefined;
|
|
185
|
+
if (typeof id === 'string' && text !== undefined)
|
|
186
|
+
out.set(id, text);
|
|
187
|
+
}
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
164
190
|
/**
|
|
165
|
-
*
|
|
191
|
+
* Read one answer batch into per-question answers.
|
|
166
192
|
*
|
|
167
|
-
* The
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
193
|
+
* The batch is `{answers:[{id, selected:[…], custom?}]}`, and it arrives two
|
|
194
|
+
* ways: as the result of an answered `ask_user_question` call, and inside the
|
|
195
|
+
* `user/message` the host writes when the user answers after the call already
|
|
196
|
+
* returned its pending notice. Both carry the same shape, so both are read here.
|
|
171
197
|
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
198
|
+
* A typed answer arrives with `selected: []` and the text in `custom`. Ten of 31
|
|
199
|
+
* real questions in one session were answered that way, and those carried the
|
|
200
|
+
* user's actual observations rather than a menu label.
|
|
201
|
+
*
|
|
202
|
+
* @param text - the JSON body.
|
|
203
|
+
* @param texts - question id to question text, from the call's arguments.
|
|
204
|
+
* @returns one entry per answered question; empty when the body is not a batch.
|
|
175
205
|
*/
|
|
176
|
-
function
|
|
177
|
-
if (askedIds.size === 0)
|
|
178
|
-
return undefined;
|
|
179
|
-
const message = messageOf(event);
|
|
180
|
-
if (message === null)
|
|
181
|
-
return undefined;
|
|
182
|
-
const callId = message.toolCallId;
|
|
183
|
-
if (callId === undefined || !askedIds.has(callId))
|
|
184
|
-
return undefined;
|
|
185
|
-
const text = textOf(event);
|
|
206
|
+
function answersOf(text, texts) {
|
|
186
207
|
let parsed;
|
|
187
208
|
try {
|
|
188
209
|
parsed = JSON.parse(text);
|
|
189
210
|
}
|
|
190
211
|
catch {
|
|
191
|
-
return
|
|
212
|
+
return [];
|
|
192
213
|
}
|
|
193
214
|
if (typeof parsed !== 'object' || parsed === null)
|
|
194
|
-
return
|
|
195
|
-
|
|
215
|
+
return [];
|
|
216
|
+
// A late reply carries its own `questions` array beside the answers, so it does
|
|
217
|
+
// not depend on the call still being in the same turn's events.
|
|
218
|
+
const body = parsed;
|
|
219
|
+
const own = Array.isArray(body['questions']) ? body['questions'] : [];
|
|
220
|
+
const merged = new Map(texts);
|
|
221
|
+
for (const question of own) {
|
|
222
|
+
if (typeof question !== 'object' || question === null)
|
|
223
|
+
continue;
|
|
224
|
+
const record = question;
|
|
225
|
+
const id = record['id'];
|
|
226
|
+
const text = record['question'];
|
|
227
|
+
if (typeof id === 'string' && typeof text === 'string')
|
|
228
|
+
merged.set(id, text);
|
|
229
|
+
}
|
|
230
|
+
const answers = body['answers'];
|
|
196
231
|
if (!Array.isArray(answers))
|
|
197
|
-
return
|
|
198
|
-
const
|
|
199
|
-
|
|
232
|
+
return [];
|
|
233
|
+
const out = [];
|
|
234
|
+
for (const answer of answers) {
|
|
200
235
|
if (typeof answer !== 'object' || answer === null)
|
|
201
|
-
|
|
236
|
+
continue;
|
|
202
237
|
const record = answer;
|
|
203
|
-
const
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
.
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
238
|
+
const id = typeof record['id'] === 'string' ? record['id'] : '';
|
|
239
|
+
const question = merged.get(id);
|
|
240
|
+
if (question === undefined)
|
|
241
|
+
continue;
|
|
242
|
+
const selected = Array.isArray(record['selected'])
|
|
243
|
+
? record['selected'].map((value) => String(value)).filter((value) => value.length > 0)
|
|
244
|
+
: [];
|
|
245
|
+
const typed = typeof record['custom'] === 'string' ? record['custom'].trim() : '';
|
|
246
|
+
if (selected.length === 0 && typed.length === 0)
|
|
247
|
+
continue;
|
|
248
|
+
out.push({ question, selected, typed });
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
215
251
|
}
|
|
216
252
|
/**
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* Reads `data.message.isError` and nothing else. Matching the serialized text
|
|
220
|
-
* for /error/i was tried and is wrong: tool output routinely contains that word
|
|
221
|
-
* (grep misses, compiler logs, dumps of source files), so it labelled every
|
|
222
|
-
* result in a 347-result turn as failed and the summarizer reported the whole
|
|
223
|
-
* turn as a failure.
|
|
253
|
+
* Render one answered question as a digest line pair.
|
|
224
254
|
*
|
|
225
|
-
* @param
|
|
226
|
-
* @returns
|
|
255
|
+
* @param answered - one question and the answer it got.
|
|
256
|
+
* @returns the question line and, indented under it, the answer line.
|
|
227
257
|
*/
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
258
|
+
function renderAnswered(answered) {
|
|
259
|
+
const picked = [...answered.selected];
|
|
260
|
+
if (answered.typed.length > 0)
|
|
261
|
+
picked.push(answered.typed);
|
|
262
|
+
return [
|
|
263
|
+
`- ${answered.question.replace(/\s+/g, ' ').trim()}`,
|
|
264
|
+
` → ${picked.join(' / ').replace(/\s+/g, ' ').trim()}`,
|
|
265
|
+
];
|
|
231
266
|
}
|
|
232
267
|
/** Directive appended as the final user message of the extraction call. */
|
|
233
268
|
const EXTRACT_INSTRUCTION = [
|
|
@@ -458,8 +493,6 @@ const EXTRACT_THINKING_RESERVE_TOKENS = 1000;
|
|
|
458
493
|
const MAX_HINTS_PER_TURN = 120;
|
|
459
494
|
/** Characters kept from one call's description or command head. */
|
|
460
495
|
const HINT_CHARS = 100;
|
|
461
|
-
/** Characters kept from one failing result's first line. */
|
|
462
|
-
const ERROR_CHARS = 140;
|
|
463
496
|
/**
|
|
464
497
|
* The best short description of what one call was for.
|
|
465
498
|
*
|
|
@@ -478,35 +511,6 @@ function hintOf(argumentsJson) {
|
|
|
478
511
|
catch {
|
|
479
512
|
return '';
|
|
480
513
|
}
|
|
481
|
-
// `ask_user_question` carries `questions[]`, not a `description`. Without this
|
|
482
|
-
// branch the hint was the bare tool name, so the extractor never saw what was
|
|
483
|
-
// asked and `## Asked` came out empty or wrong on every turn that used it.
|
|
484
|
-
const questions = parsed['questions'];
|
|
485
|
-
if (Array.isArray(questions)) {
|
|
486
|
-
const asked = questions
|
|
487
|
-
.map((question) => {
|
|
488
|
-
if (typeof question !== 'object' || question === null)
|
|
489
|
-
return '';
|
|
490
|
-
const record = question;
|
|
491
|
-
const text = typeof record['question'] === 'string'
|
|
492
|
-
? record['question']
|
|
493
|
-
: typeof record['header'] === 'string' ? record['header'] : '';
|
|
494
|
-
const options = Array.isArray(record['options'])
|
|
495
|
-
? record['options']
|
|
496
|
-
.map((option) => (typeof option === 'object' && option !== null
|
|
497
|
-
? String(option['label'] ?? '')
|
|
498
|
-
: ''))
|
|
499
|
-
.filter((label) => label.length > 0)
|
|
500
|
-
: [];
|
|
501
|
-
if (text.length === 0)
|
|
502
|
-
return '';
|
|
503
|
-
return options.length === 0 ? text : `${text} [options: ${options.join(' / ')}]`;
|
|
504
|
-
})
|
|
505
|
-
.filter((text) => text.length > 0)
|
|
506
|
-
.join(' | ');
|
|
507
|
-
if (asked.length > 0)
|
|
508
|
-
return asked.replace(/\s+/g, ' ').slice(0, HINT_CHARS * 2);
|
|
509
|
-
}
|
|
510
514
|
for (const key of ['description', 'command', 'query', 'file_path', 'path', 'pattern']) {
|
|
511
515
|
const candidate = parsed[key];
|
|
512
516
|
if (typeof candidate === 'string' && candidate.trim().length > 0) {
|
|
@@ -521,8 +525,10 @@ function hintOf(argumentsJson) {
|
|
|
521
525
|
* This is what lets a turn whose assistant prose is empty still leave a record:
|
|
522
526
|
* in the measured session one turn ran 342 tool calls and emitted zero prose, so
|
|
523
527
|
* the verbatim digest reduced it to a single `tools:` count line. The trace names
|
|
524
|
-
* what was attempted
|
|
525
|
-
*
|
|
528
|
+
* what was attempted, which is enough for a model to state the outcome.
|
|
529
|
+
*
|
|
530
|
+
* Tool results are not read. Their only contribution used to be a first-line
|
|
531
|
+
* error string, and that string was never the part of a failure that mattered.
|
|
526
532
|
*
|
|
527
533
|
* Bounded on purpose — 500 calls would otherwise produce a prompt larger than the
|
|
528
534
|
* material it summarizes.
|
|
@@ -533,49 +539,25 @@ function hintOf(argumentsJson) {
|
|
|
533
539
|
export function collectHints(events) {
|
|
534
540
|
const lines = [];
|
|
535
541
|
let calls = 0;
|
|
536
|
-
// Tool-call ids of this turn's questions, so the matching result can be read as
|
|
537
|
-
// an answer rather than being skipped as a non-error result.
|
|
538
|
-
const askedIds = new Set();
|
|
539
542
|
for (const event of events) {
|
|
540
543
|
// Tool calls are blocks inside `assistant/message`, not standalone events:
|
|
541
544
|
// `digestSource` emits no `tool/call` type at all, so selecting on that type
|
|
542
545
|
// matched nothing and a 342-call turn produced a single hint line.
|
|
543
|
-
if (event.type
|
|
544
|
-
for (const call of toolCallsOf(event)) {
|
|
545
|
-
// A question is never dropped by the cap. It is the only record of what was
|
|
546
|
-
// asked, and its answer is matched by call id rather than by the cap, so
|
|
547
|
-
// dropping the question left an orphan ANSWER line with nothing beside it —
|
|
548
|
-
// a real turn made 292 calls, the question came late, and the user's choice
|
|
549
|
-
// never reached the digest.
|
|
550
|
-
const isQuestion = call.name === 'ask_user_question';
|
|
551
|
-
if (!isQuestion) {
|
|
552
|
-
if (calls >= MAX_HINTS_PER_TURN)
|
|
553
|
-
break;
|
|
554
|
-
calls += 1;
|
|
555
|
-
}
|
|
556
|
-
const hint = hintOf(call.arguments);
|
|
557
|
-
lines.push(hint.length > 0 ? `TOOL ${call.name}: ${hint}` : `TOOL ${call.name}`);
|
|
558
|
-
if (isQuestion)
|
|
559
|
-
askedIds.add(call.id);
|
|
560
|
-
}
|
|
561
|
-
continue;
|
|
562
|
-
}
|
|
563
|
-
if (event.type !== 'tool/result')
|
|
564
|
-
continue;
|
|
565
|
-
// The answer to a question is the tool's own result, not an error. It is the
|
|
566
|
-
// only record of what the user chose, and the next user message is often just
|
|
567
|
-
// "1" — so it has to travel with the question.
|
|
568
|
-
const answered = answerOf(event, askedIds);
|
|
569
|
-
if (answered !== undefined) {
|
|
570
|
-
lines.push(`ANSWER: ${answered}`);
|
|
546
|
+
if (event.type !== 'assistant/message')
|
|
571
547
|
continue;
|
|
548
|
+
for (const call of toolCallsOf(event)) {
|
|
549
|
+
// A question is not a hint. It and its answer are rendered from the log by
|
|
550
|
+
// `renderAnswers`, so listing it here as well only invited the model to
|
|
551
|
+
// restate it in `asked` — the same exchange printed twice, once of them
|
|
552
|
+
// paraphrased.
|
|
553
|
+
if (call.name === 'ask_user_question')
|
|
554
|
+
continue;
|
|
555
|
+
if (calls >= MAX_HINTS_PER_TURN)
|
|
556
|
+
break;
|
|
557
|
+
calls += 1;
|
|
558
|
+
const hint = hintOf(call.arguments);
|
|
559
|
+
lines.push(hint.length > 0 ? `TOOL ${call.name}: ${hint}` : `TOOL ${call.name}`);
|
|
572
560
|
}
|
|
573
|
-
if (!isErrorResult(event))
|
|
574
|
-
continue;
|
|
575
|
-
const firstLine = textOf(event).split('\n')[0] ?? '';
|
|
576
|
-
if (firstLine.trim().length === 0)
|
|
577
|
-
continue;
|
|
578
|
-
lines.push(`ERR ${firstLine.trim().slice(0, ERROR_CHARS)}`);
|
|
579
561
|
}
|
|
580
562
|
return lines.join('\n');
|
|
581
563
|
}
|
|
@@ -599,7 +581,7 @@ function turnOf(event) {
|
|
|
599
581
|
}
|
|
600
582
|
/** A fresh, empty turn accumulator. */
|
|
601
583
|
function emptyTurn(turn, fromRequest = false) {
|
|
602
|
-
return { turn, fromRequest, user: [], assistant: [], tools: new Map(),
|
|
584
|
+
return { turn, fromRequest, user: [], assistant: [], tools: new Map(), writes: [], answered: [] };
|
|
603
585
|
}
|
|
604
586
|
/**
|
|
605
587
|
* Group append-origin surface events into per-turn digest material.
|
|
@@ -633,6 +615,16 @@ export function buildSections(events) {
|
|
|
633
615
|
// objective on every round, so recording each copy would repeat it dozens of
|
|
634
616
|
// times across a long autonomous run.
|
|
635
617
|
const recordedObjectives = new Set();
|
|
618
|
+
// Question id to question text, for every `ask_user_question` call seen so far.
|
|
619
|
+
// The answer batch names questions by id only, and an id like `quota` says
|
|
620
|
+
// nothing on its own.
|
|
621
|
+
const questionTexts = new Map();
|
|
622
|
+
// Call ids of this turn's `ask_user_question` calls. A result is an answer only
|
|
623
|
+
// when its own call asked something: without this every result in the log was
|
|
624
|
+
// JSON-parsed (536 KB across 157 results in one measured session, the largest
|
|
625
|
+
// 49.9 KB) and any output that happened to look like an answer batch — a file
|
|
626
|
+
// dump, a test fixture — would be recorded as something the user chose.
|
|
627
|
+
const questionCallIds = new Set();
|
|
636
628
|
for (const event of events) {
|
|
637
629
|
if (event.type === 'turn/start') {
|
|
638
630
|
const turn = turnOf(event);
|
|
@@ -658,15 +650,22 @@ export function buildSections(events) {
|
|
|
658
650
|
continue;
|
|
659
651
|
}
|
|
660
652
|
if (event.type === 'tool/result') {
|
|
661
|
-
|
|
653
|
+
// An answered question arrives as this call's own result. The call's
|
|
654
|
+
// arguments named the questions; the result carries the choices.
|
|
655
|
+
const callId = messageOf(event)?.toolCallId;
|
|
656
|
+
if (callId === undefined || !questionCallIds.has(callId) || currentTurn === undefined)
|
|
662
657
|
continue;
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
658
|
+
currentTurn.answered.push(...answersOf(textOf(event), questionTexts));
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
// The host answers a question two ways. Inside the waiting window the answer
|
|
662
|
+
// is the tool result above; after the call already returned its pending
|
|
663
|
+
// notice, it is a `user/message` whose source kind is `user-question-reply`
|
|
664
|
+
// and whose body is the same answer batch. That message is not a new request,
|
|
665
|
+
// so it is read here rather than opening a section.
|
|
666
|
+
if (sourceKind(event) === 'user-question-reply') {
|
|
667
|
+
if (currentTurn !== undefined)
|
|
668
|
+
currentTurn.answered.push(...answersOf(textOf(event), questionTexts));
|
|
670
669
|
continue;
|
|
671
670
|
}
|
|
672
671
|
if (event.type !== 'user/message' && event.type !== 'assistant/message')
|
|
@@ -704,6 +703,12 @@ export function buildSections(events) {
|
|
|
704
703
|
currentTurn.assistant.push(text);
|
|
705
704
|
for (const call of toolCallsOf(event)) {
|
|
706
705
|
currentTurn.tools.set(call.name, (currentTurn.tools.get(call.name) ?? 0) + 1);
|
|
706
|
+
if (call.name === 'ask_user_question') {
|
|
707
|
+
for (const [id, question] of questionTextsOf(call.arguments))
|
|
708
|
+
questionTexts.set(id, question);
|
|
709
|
+
questionCallIds.add(call.id);
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
707
712
|
if (!WRITE_TOOLS.has(call.name))
|
|
708
713
|
continue;
|
|
709
714
|
const path = pathArgumentOf(call.arguments);
|
|
@@ -838,30 +843,75 @@ function renderSection(section, level, condensed, sectionIndex) {
|
|
|
838
843
|
if (turn.extracted !== undefined && renderExtractionBlock(turn.extracted, level).length > 0) {
|
|
839
844
|
const block = renderExtractionBlock(turn.extracted, level);
|
|
840
845
|
// The trailer goes *above* the block, not below it. `## Asked` is the block's
|
|
841
|
-
// last section and the user's reply is the very next line, so
|
|
846
|
+
// last section and the user's reply is the very next line, so a trailer line
|
|
842
847
|
// in between would separate the reply from the question it answers.
|
|
843
848
|
const trailer = [];
|
|
844
|
-
if (turn.errors.length > 0) {
|
|
845
|
-
trailer.push(` errors: ${turn.errors.length}: ${turn.errors.slice(0, 3).join(' | ')}`);
|
|
846
|
-
}
|
|
847
849
|
if (turn.writes.length > 0)
|
|
848
850
|
trailer.push(` wrote: ${turn.writes.join(', ')}`);
|
|
849
|
-
const
|
|
851
|
+
const withAnswers = insertAnswers(block, renderAnswers(turn));
|
|
852
|
+
const body = trailer.length > 0 ? `${trailer.join('\n')}\n${withAnswers}` : withAnswers;
|
|
850
853
|
lines.push(`### assistant\n${body}`);
|
|
851
854
|
}
|
|
852
855
|
else {
|
|
853
856
|
const body = turn.assistant.join('\n');
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
857
|
+
const answers = renderAnswers(turn);
|
|
858
|
+
// The heading goes on whenever there is anything to put under it. Emitting
|
|
859
|
+
// the answers bare put `## Answers` directly below `## user`, at the user's
|
|
860
|
+
// own heading level, where it reads as something the user said.
|
|
861
|
+
if (body.length > 0 || answers.length > 0)
|
|
862
|
+
lines.push(`### assistant\n${[body, answers].filter((part) => part.length > 0).join('\n')}`);
|
|
859
863
|
if (turn.writes.length > 0)
|
|
860
864
|
lines.push(` wrote: ${turn.writes.join(', ')}`);
|
|
861
865
|
}
|
|
862
866
|
}
|
|
863
867
|
return lines.join('\n');
|
|
864
868
|
}
|
|
869
|
+
/**
|
|
870
|
+
* Render a turn's tool questions and their answers, or an empty string.
|
|
871
|
+
*
|
|
872
|
+
* This section exists because `## Asked` cannot cover these. `asked` is written by
|
|
873
|
+
* the model from prose, and its contract says the reply is the next `## user` line
|
|
874
|
+
* — true for a question asked in prose, false for one asked through
|
|
875
|
+
* `ask_user_question`: the user answers inside the picker, the answer becomes the
|
|
876
|
+
* tool result, and no user message is ever written. Two consecutive checkpoints of
|
|
877
|
+
* a real session carried neither the question nor the answer.
|
|
878
|
+
*
|
|
879
|
+
* Rendered from the log, so it cannot be paraphrased away, and placed after the
|
|
880
|
+
* assistant block because it belongs to that turn's work.
|
|
881
|
+
*
|
|
882
|
+
* @param turn - one turn's digest material.
|
|
883
|
+
* @returns the `## Answers` section, or an empty string when nothing was asked.
|
|
884
|
+
*/
|
|
885
|
+
function renderAnswers(turn) {
|
|
886
|
+
if (turn.answered.length === 0)
|
|
887
|
+
return '';
|
|
888
|
+
return `## Answers\n${turn.answered.flatMap(renderAnswered).join('\n')}`;
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Place the `## Answers` section just above `## Asked`, or at the end when the
|
|
892
|
+
* block has none.
|
|
893
|
+
*
|
|
894
|
+
* Not at the end unconditionally: `## Asked` is the block's last section because
|
|
895
|
+
* the user's reply — a bare `1` — is the very next line and needs the options
|
|
896
|
+
* directly above it. A second question-and-answer block wedged in between would
|
|
897
|
+
* separate them.
|
|
898
|
+
*
|
|
899
|
+
* @param block - the rendered extraction block.
|
|
900
|
+
* @param answers - the rendered `## Answers` section, or an empty string.
|
|
901
|
+
* @returns the block with the answers placed.
|
|
902
|
+
*/
|
|
903
|
+
function insertAnswers(block, answers) {
|
|
904
|
+
if (answers.length === 0)
|
|
905
|
+
return block;
|
|
906
|
+
// The heading as a whole line, not the bare text: a `did`/`concluded` item is a
|
|
907
|
+
// free string, and one that happens to contain `## Asked` would otherwise be
|
|
908
|
+
// split in the middle of a bullet.
|
|
909
|
+
const at = block.startsWith('## Asked\n') ? 0 : block.indexOf('\n## Asked\n') + 1;
|
|
910
|
+
if (at <= 0 && !block.startsWith('## Asked\n'))
|
|
911
|
+
return `${block}\n${answers}`;
|
|
912
|
+
const head = block.slice(0, at);
|
|
913
|
+
return head.length === 0 ? `${answers}\n${block}` : `${head}${answers}\n${block.slice(at)}`;
|
|
914
|
+
}
|
|
865
915
|
/**
|
|
866
916
|
* Render the digest under a hard character budget.
|
|
867
917
|
*
|
|
@@ -1004,8 +1054,11 @@ export function renderDigest(events, budgetTokens, extracted, condensed) {
|
|
|
1004
1054
|
// key's absence and its undefined value as different types.
|
|
1005
1055
|
delete target.extracted;
|
|
1006
1056
|
target.assistant = [];
|
|
1007
|
-
target.errors = [];
|
|
1008
1057
|
target.writes = [];
|
|
1058
|
+
// The answers belong to the body: they were produced by this turn's own
|
|
1059
|
+
// tool call, so a dropped body that kept them would still print a full
|
|
1060
|
+
// `## Answers` at every tier, and the truncation would never fit.
|
|
1061
|
+
target.answered = [];
|
|
1009
1062
|
}
|
|
1010
1063
|
return trimmed;
|
|
1011
1064
|
};
|
|
@@ -1068,7 +1121,7 @@ export function renderDigest(events, budgetTokens, extracted, condensed) {
|
|
|
1068
1121
|
const keeperTurn = turnAt(positions[lastRequest]);
|
|
1069
1122
|
if (keeperTurn === undefined)
|
|
1070
1123
|
return '';
|
|
1071
|
-
const bare = { ...keeperTurn, assistant: [],
|
|
1124
|
+
const bare = { ...keeperTurn, assistant: [], writes: [], answered: [] };
|
|
1072
1125
|
delete bare.extracted;
|
|
1073
1126
|
return render([{ turns: [bare] }], 0);
|
|
1074
1127
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -67,19 +67,14 @@ export interface SummaryResult {
|
|
|
67
67
|
provider: string;
|
|
68
68
|
model: string;
|
|
69
69
|
}
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*
|
|
79
|
-
* @param event - a `tool/result` event.
|
|
80
|
-
* @returns true only when the log itself marks the result as an error.
|
|
81
|
-
*/
|
|
82
|
-
export declare function isErrorResult(event: SessionEvent): boolean;
|
|
70
|
+
/** One question the user answered through the question tool, with its answer. */
|
|
71
|
+
interface AnsweredQuestion {
|
|
72
|
+
question: string;
|
|
73
|
+
/** The labels the user picked, in the order they were offered. */
|
|
74
|
+
selected: string[];
|
|
75
|
+
/** What the user typed instead of picking, when they did. */
|
|
76
|
+
typed: string;
|
|
77
|
+
}
|
|
83
78
|
/** One slot's extraction, as the model returns it. */
|
|
84
79
|
export interface Extraction {
|
|
85
80
|
did: string[];
|
|
@@ -178,8 +173,10 @@ export interface Condensed {
|
|
|
178
173
|
* This is what lets a turn whose assistant prose is empty still leave a record:
|
|
179
174
|
* in the measured session one turn ran 342 tool calls and emitted zero prose, so
|
|
180
175
|
* the verbatim digest reduced it to a single `tools:` count line. The trace names
|
|
181
|
-
* what was attempted
|
|
182
|
-
*
|
|
176
|
+
* what was attempted, which is enough for a model to state the outcome.
|
|
177
|
+
*
|
|
178
|
+
* Tool results are not read. Their only contribution used to be a first-line
|
|
179
|
+
* error string, and that string was never the part of a failure that mattered.
|
|
183
180
|
*
|
|
184
181
|
* Bounded on purpose — 500 calls would otherwise produce a prompt larger than the
|
|
185
182
|
* material it summarizes.
|
|
@@ -203,9 +200,20 @@ interface Turn {
|
|
|
203
200
|
user: string[];
|
|
204
201
|
assistant: string[];
|
|
205
202
|
tools: Map<string, number>;
|
|
206
|
-
resultBytes: number;
|
|
207
|
-
errors: string[];
|
|
208
203
|
writes: string[];
|
|
204
|
+
/**
|
|
205
|
+
* Questions this turn put to the user through `ask_user_question`, with the
|
|
206
|
+
* answers they got.
|
|
207
|
+
*
|
|
208
|
+
* Read from the log rather than asked of the model, and rendered as its own
|
|
209
|
+
* `## Answers` section. The model's `asked` field covers questions written in
|
|
210
|
+
* prose; a question asked through the tool is answered in the tool's own picker,
|
|
211
|
+
* and that answer is the tool result. Measured on one real session: three such
|
|
212
|
+
* questions were answered inside the picker and no `user/message` was written at
|
|
213
|
+
* all, so both the question and the choice were absent from two consecutive
|
|
214
|
+
* checkpoints.
|
|
215
|
+
*/
|
|
216
|
+
answered: AnsweredQuestion[];
|
|
209
217
|
/**
|
|
210
218
|
* The extracted summary for this turn, when extraction succeeded.
|
|
211
219
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-verbatim-compaction",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.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",
|