dsh-compaction-weave 0.0.0-stage → 0.3.3
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/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +136 -0
- package/README.md +139 -2
- package/cordis.patch.yml +19 -0
- package/docs/architecture.md +84 -0
- package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
- package/docs/benchmarks/rc2-documentation.json +41 -0
- package/docs/benchmarks/rc2-linux-tests.json +590 -0
- package/docs/benchmarks/rc2-live-and-cost.json +746 -0
- package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
- package/docs/benchmarks/rc4-tests.json +617 -0
- package/docs/benchmarks/review35-compression.json +117 -0
- package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
- package/docs/benchmarks/v0.3.2-tests.json +590 -0
- package/docs/configuration.md +131 -0
- package/docs/development.md +88 -0
- package/docs/en/architecture.md +75 -0
- package/docs/en/configuration.md +131 -0
- package/docs/en/development.md +86 -0
- package/docs/en/feedback.md +47 -0
- package/docs/en/principles.md +235 -0
- package/docs/en/testing.md +368 -0
- package/docs/en/troubleshooting.md +59 -0
- package/docs/feedback.md +47 -0
- package/docs/images/principles-context.en.svg +77 -0
- package/docs/images/principles-context.zh.svg +77 -0
- package/docs/images/principles-pipeline.en.svg +93 -0
- package/docs/images/principles-pipeline.zh.svg +93 -0
- package/docs/images/principles-recovery.en.svg +46 -0
- package/docs/images/principles-recovery.zh.svg +46 -0
- package/docs/principles.md +235 -0
- package/docs/test-results.json +634 -0
- package/docs/testing.md +358 -0
- package/docs/troubleshooting.md +59 -0
- package/lib/abort.js +13 -0
- package/lib/action-state.js +60 -0
- package/lib/atomic-replace.js +18 -0
- package/lib/attachments.js +76 -0
- package/lib/audit-reject.js +161 -0
- package/lib/audit.js +939 -0
- package/lib/cache-policy.js +71 -0
- package/lib/client.js +696 -0
- package/lib/command-compact.js +38 -0
- package/lib/command-heartbeat.js +61 -0
- package/lib/config.js +596 -0
- package/lib/constraint-scope.js +37 -0
- package/lib/continuation-budget.js +69 -0
- package/lib/continuation-pending.js +57 -0
- package/lib/continuation-progress.js +46 -0
- package/lib/continuation.js +287 -0
- package/lib/engine-entry.js +103 -0
- package/lib/engine.js +2485 -0
- package/lib/failure-diagnostics.js +220 -0
- package/lib/history-source.js +74 -0
- package/lib/history.js +712 -0
- package/lib/idle-compaction.js +64 -0
- package/lib/index.js +611 -0
- package/lib/instruction-source.js +163 -0
- package/lib/journal.js +164 -0
- package/lib/manual-error.js +179 -0
- package/lib/memory-policy.js +383 -0
- package/lib/meter.js +387 -0
- package/lib/observation-mask.js +390 -0
- package/lib/proactive.js +108 -0
- package/lib/progress-facts.js +347 -0
- package/lib/progress-state.js +30 -0
- package/lib/prompt.js +194 -0
- package/lib/reconstruct.js +72 -0
- package/lib/reference-input.js +107 -0
- package/lib/region.js +972 -0
- package/lib/source-provenance.js +58 -0
- package/lib/source-recovery.js +22 -0
- package/lib/source-refs.js +137 -0
- package/lib/state.js +46 -0
- package/lib/storage-path.js +42 -0
- package/lib/summary-budget.js +187 -0
- package/lib/summary-config.js +308 -0
- package/lib/summary-progress.js +56 -0
- package/lib/summary-retry.js +27 -0
- package/lib/summary-timeout.js +68 -0
- package/lib/summary.js +476 -0
- package/lib/tools.js +341 -0
- package/lib/transaction-host.js +252 -0
- package/lib/transaction.js +285 -0
- package/lib/validate.js +503 -0
- package/lib/version.js +3 -0
- package/lib/window.js +120 -0
- package/lib/work-report.js +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -4
package/lib/tools.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// 三个回查工具的注册。
|
|
2
|
+
//
|
|
3
|
+
// 压缩会把原文从 surface 上换掉,但原文已经落盘(lib/history.js)。这三个
|
|
4
|
+
// 工具让模型自己把丢掉的东西捞回来 —— 这是 Claude Code「摘要里带 transcript
|
|
5
|
+
// 路径」那条思路在 DSH 里的等价物。
|
|
6
|
+
//
|
|
7
|
+
// 注册走宿主注入的 `ctx.tools`,不 import `@deepseek-ai/dsh-tools`:
|
|
8
|
+
// 插件目录按真实路径解析,裸导入会在加载期失败。
|
|
9
|
+
|
|
10
|
+
import { CompactionArchive, HISTORY_TOOLS, HISTORY_SNAPSHOT_WARNING } from './history.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} ctx
|
|
14
|
+
* @param {Readonly<Record<string, any>>} config
|
|
15
|
+
* @param {CompactionArchive} [archive]
|
|
16
|
+
*/
|
|
17
|
+
export function registerHistoryTools(ctx, config, archive) {
|
|
18
|
+
const tools = ctx?.tools;
|
|
19
|
+
if (!tools || typeof tools.register !== 'function') return false;
|
|
20
|
+
if (!config?.archiveEnabled) return false;
|
|
21
|
+
|
|
22
|
+
const store = archive ?? new CompactionArchive({ root: config?.archiveDir });
|
|
23
|
+
|
|
24
|
+
let registered = 0;
|
|
25
|
+
for (const spec of HISTORY_TOOLS) {
|
|
26
|
+
try {
|
|
27
|
+
tools.register({
|
|
28
|
+
name: spec.name,
|
|
29
|
+
description: spec.description,
|
|
30
|
+
parameters: spec.parameters,
|
|
31
|
+
// 宿主的 `register()` 强制要求 `output.render` 是函数,否则直接抛
|
|
32
|
+
// `tool "x" must declare output { schema, render, presentationMeta? }`。
|
|
33
|
+
// 少了它三个工具会全部注册失败,而且只在日志里留一行 warn —— 用户
|
|
34
|
+
// 侧表现就是"装了但模型看不到 history_* 工具"。
|
|
35
|
+
output: {
|
|
36
|
+
schema: { type: 'string' },
|
|
37
|
+
render: (_args, value) => [{ type: 'text', text: typeof value === 'string' ? value : String(value ?? '') }],
|
|
38
|
+
},
|
|
39
|
+
timeoutMs: 15000,
|
|
40
|
+
execute: (args, exec) => runTool(spec.name, store, args ?? {}, exec),
|
|
41
|
+
});
|
|
42
|
+
registered += 1;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
ctx.logger?.warn?.(`[compaction-weave] failed to register ${spec.name}: ${error?.message ?? error}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (registered > 0) {
|
|
49
|
+
ctx.logger?.info?.(`[compaction-weave] registered ${registered} history tool(s)`);
|
|
50
|
+
}
|
|
51
|
+
return registered === HISTORY_TOOLS.length;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function runTool(toolName, store, args, exec) {
|
|
55
|
+
const sessionId = resolveSessionId(exec);
|
|
56
|
+
if (sessionId === null) {
|
|
57
|
+
return 'history tools are only available inside an agent session.';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (toolName === 'history_list') {
|
|
61
|
+
const limit = clampInt(args.limit, 20, 1, 100);
|
|
62
|
+
let spans;
|
|
63
|
+
try { spans = await store.listSpans(sessionId); } catch (error) { return `Archive listing incomplete: ${error.message}`; }
|
|
64
|
+
if (spans.length === 0) return 'No archived compaction spans in this session yet.';
|
|
65
|
+
return spans
|
|
66
|
+
.slice(0, limit)
|
|
67
|
+
.map((span) => `${span.compactionId} ${span.modified ?? '?'} bytes=${span.bytes ?? '?'}`)
|
|
68
|
+
.join('\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (toolName === 'history_read') {
|
|
72
|
+
const compactionId = typeof args.compactionId === 'string' ? args.compactionId : '';
|
|
73
|
+
if (compactionId.length === 0) return 'history_read requires a compactionId (see history_list).';
|
|
74
|
+
if (args.attachmentId !== undefined) {
|
|
75
|
+
try {
|
|
76
|
+
if (typeof args.attachmentId !== 'string') throw new Error('attachmentId must be a string');
|
|
77
|
+
return JSON.stringify(await store.readAttachment(sessionId, compactionId, args.attachmentId, args.byteOffset ?? 0, args.byteLimit ?? 32768, exec?.signal));
|
|
78
|
+
} catch (error) { return `Cannot read archived attachment: ${error.message}`; }
|
|
79
|
+
}
|
|
80
|
+
// `seq` 是**精确读取入口**:有它就不按 offset 猜页,直接把那一条捞出来。
|
|
81
|
+
// 摘要/引用里只知道 seq 号、不知道它是第几条,按 offset 翻页找等于让调用方
|
|
82
|
+
// 自己数 —— 100 条以上的跨度根本没法用。
|
|
83
|
+
if (args.seq !== undefined && (!Number.isSafeInteger(args.seq) || args.seq < 0)) return 'history_read seq must be a non-negative safe integer.';
|
|
84
|
+
const onlySeq = args.seq ?? null;
|
|
85
|
+
const raw = args.raw === true;
|
|
86
|
+
const full = args.full === true;
|
|
87
|
+
// `raw` 一定要分页:结构化原文没有体积上限,一次全给会直接把上下文撑爆。
|
|
88
|
+
const limit = raw
|
|
89
|
+
? clampInt(args.limit, 20, 1, RAW_PAGE_LIMIT)
|
|
90
|
+
: clampInt(args.limit, 40, 1, 200);
|
|
91
|
+
let result;
|
|
92
|
+
try {
|
|
93
|
+
result = await store.readSpan(sessionId, compactionId);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return `Cannot read ${compactionId}: ${error?.message ?? error}`;
|
|
96
|
+
}
|
|
97
|
+
const entries = Array.isArray(result?.entries) ? result.entries : [];
|
|
98
|
+
// ⚠️ 归档不完整时必须**说出来**,不能只给能读到的那部分。
|
|
99
|
+
// 回查的人是在补记忆,他不知道原文有多少条;悄悄少几条,他就会把"部分"
|
|
100
|
+
// 当"全部",比直接报错危险得多。
|
|
101
|
+
const warning = result?.ok ? '' : formatDamage(compactionId, result);
|
|
102
|
+
if (entries.length === 0) {
|
|
103
|
+
return warning.length > 0 ? warning : `No entries found for ${compactionId}.`;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (onlySeq !== null) {
|
|
107
|
+
const matched = entries.filter((entry) => entry?.seq === onlySeq);
|
|
108
|
+
if (matched.length === 0) {
|
|
109
|
+
const available = entries.map((entry) => entry?.seq).filter((seq) => typeof seq === 'number');
|
|
110
|
+
const range = available.length === 0 ? '?' : `${Math.min(...available)}..${Math.max(...available)}`;
|
|
111
|
+
return `No entry with seq ${onlySeq} in ${compactionId} (this span holds seq ${range}; ${entries.length} entries).${warning}`;
|
|
112
|
+
}
|
|
113
|
+
const entry = matched[0];
|
|
114
|
+
const json = JSON.stringify(entry);
|
|
115
|
+
if (raw || json.length > 60000 || args.charOffset !== undefined || args.charLimit !== undefined) {
|
|
116
|
+
const offset = clampInt(args.charOffset, 0, 0, json.length);
|
|
117
|
+
const length = clampInt(args.charLimit, 60000, 1, 60000);
|
|
118
|
+
const next = Math.min(offset + length, json.length);
|
|
119
|
+
// Valid JSON envelope at every page boundary, even in the middle of a record.
|
|
120
|
+
return JSON.stringify({ compactionId, seq: onlySeq, verified: result.ok === true, warning, snapshotKind:'historical-session-event',currentFileVersion:'not-checked',snapshotWarning:HISTORY_SNAPSHOT_WARNING, encoding: 'JSON text; concatenate jsonChunk values then JSON.parse', charOffset: offset, totalChars: json.length, nextCharOffset: next < json.length ? next : null, jsonChunk: json.slice(offset, next) });
|
|
121
|
+
}
|
|
122
|
+
return `${HISTORY_SNAPSHOT_WARNING}\n\n${warning}# ${compactionId} seq ${onlySeq} (1 of ${entries.length} entries)\n\n${formatEntry(entry, { compactionId, raw, full })}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const offset = clampInt(args.offset, 0, 0, 100000);
|
|
126
|
+
const page = entries.slice(offset, offset + limit);
|
|
127
|
+
if (page.length === 0) {
|
|
128
|
+
return `No entries for ${compactionId} at offset ${offset} (total ${entries.length}).${warning}`;
|
|
129
|
+
}
|
|
130
|
+
const header = `${HISTORY_SNAPSHOT_WARNING}\n\n# ${compactionId} (entries ${offset + 1}-${offset + page.length} of ${entries.length})${raw ? ' [raw]' : ''}`;
|
|
131
|
+
const rendered = [];
|
|
132
|
+
let chars = 0;
|
|
133
|
+
for (const entry of page) {
|
|
134
|
+
const text = formatEntry(entry, { compactionId, raw, full });
|
|
135
|
+
if (text.length > RAW_CHAR_BUDGET) {
|
|
136
|
+
rendered.push(`Entry seq ${entry.seq} exceeds the page budget. Read history_read compactionId=${compactionId} seq=${entry.seq} raw=true charOffset=0 charLimit=60000 and follow nextCharOffset; no record has been cut.`);
|
|
137
|
+
} else if (chars + text.length <= RAW_CHAR_BUDGET) {
|
|
138
|
+
rendered.push(text); chars += text.length;
|
|
139
|
+
} else break;
|
|
140
|
+
}
|
|
141
|
+
const nextOffset = offset + rendered.length;
|
|
142
|
+
const more = nextOffset < entries.length ? `\n\nNext page: offset=${nextOffset} (whole records only).` : '';
|
|
143
|
+
return `${warning}${header}\n\n${rendered.join('\n\n')}${more}`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (toolName === 'history_search') {
|
|
147
|
+
const query = typeof args.query === 'string' ? args.query : '';
|
|
148
|
+
if (query.trim().length === 0) return 'history_search requires a non-empty query.';
|
|
149
|
+
const limit = clampInt(args.limit, 20, 1, 100);
|
|
150
|
+
let result;
|
|
151
|
+
try { result = await store.search(sessionId, query, { limit }); } catch (error) { return `Archive search incomplete: ${error.message}. The query may exist in unreadable history.`; }
|
|
152
|
+
const hits = Array.isArray(result?.hits) ? result.hits : Array.isArray(result) ? result : [];
|
|
153
|
+
// ⚠️ "没搜到"只有在**所有归档都完整读进来**时才能当结论。
|
|
154
|
+
// 有归档读不完整时,那一段里完全可能就有这条 —— 此时说
|
|
155
|
+
// `No archived span contains "X"` 是在用一个更糟的错误回答
|
|
156
|
+
// 用户的补记忆请求:他会以为这条约束从来没存在过。
|
|
157
|
+
const unreadable = Array.isArray(result?.unreadable) ? result.unreadable : [];
|
|
158
|
+
const caveat = unreadable.length === 0
|
|
159
|
+
? ''
|
|
160
|
+
: `\n\n⚠️ ${unreadable.length} archived span(s) could not be read completely (${unreadable
|
|
161
|
+
.map((item) => `${item.compactionId}: ${item.reason}`)
|
|
162
|
+
.join(', ')}); the query may exist in the unreadable part.`;
|
|
163
|
+
if (hits.length === 0) {
|
|
164
|
+
return unreadable.length === 0
|
|
165
|
+
? `No archived span contains "${query}".`
|
|
166
|
+
: `No readable archived span contains "${query}".${caveat}`;
|
|
167
|
+
}
|
|
168
|
+
return (
|
|
169
|
+
hits
|
|
170
|
+
.map((hit) => {
|
|
171
|
+
const mark = hit.incomplete ? ' ⚠️(incomplete archive)' : '';
|
|
172
|
+
return `[${hit.compactionId} seq=${hit.seq ?? '?'} ${hit.role ?? ''}]${mark} ${hit.excerpt}`;
|
|
173
|
+
})
|
|
174
|
+
.join('\n\n') + caveat
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return `Unknown tool: ${toolName}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function resolveSessionId(exec) {
|
|
182
|
+
const candidates = [
|
|
183
|
+
exec?.session?.id,
|
|
184
|
+
exec?.agent?.session?.id,
|
|
185
|
+
exec?.context?.session?.id,
|
|
186
|
+
exec?.sessionId,
|
|
187
|
+
];
|
|
188
|
+
for (const value of candidates) {
|
|
189
|
+
if (typeof value === 'string' && value.length > 0) return value;
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function formatEntry(entry, options = {}) {
|
|
195
|
+
const seq = entry?.seq ?? '?';
|
|
196
|
+
const type = entry?.type ?? 'event';
|
|
197
|
+
const text = typeof entry?.text === 'string' && entry.text.length > 0 ? entry.text : '(no textual content)';
|
|
198
|
+
const extra = [];
|
|
199
|
+
// 工具调用与结果必须能重新配对:压缩之后 surface 上已经没有原始事件,
|
|
200
|
+
// 归档里只留一句 `[tool-call foo]` 的话,"这次调用返回了什么"就永久丢失了。
|
|
201
|
+
if (Array.isArray(entry?.tool)) {
|
|
202
|
+
for (const ref of entry.tool) {
|
|
203
|
+
if (!ref || typeof ref !== 'object') continue;
|
|
204
|
+
const id = ref.callId ?? '?';
|
|
205
|
+
if (ref.kind === 'call') extra.push(`tool-call ${ref.name ?? '?'} id=${id}`);
|
|
206
|
+
else if (ref.kind === 'result') extra.push(`tool-result id=${id}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// 非文本块(图片 / reasoning / 附件)在 `text` 里只剩占位符,
|
|
210
|
+
// 这里把它们的真实结构列出来,否则"原文"其实缺了这些内容却看不出来。
|
|
211
|
+
if (Array.isArray(entry?.blocks)) {
|
|
212
|
+
for (const block of entry.blocks) {
|
|
213
|
+
if (!block || typeof block !== 'object') continue;
|
|
214
|
+
if (block.type === 'text') continue;
|
|
215
|
+
const detail = nonTextSummary(block, { ...options, seq });
|
|
216
|
+
if (detail !== null) extra.push(detail);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const meta = extra.length === 0 ? '' : `\n(${extra.join('; ')})`;
|
|
220
|
+
const head = `--- seq ${seq} [${type}] ---\n${text}${meta}`;
|
|
221
|
+
return options.raw === true ? `${head}\n${rawRecord(entry)}` : head;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 默认视图里 reasoning 正文最多内联这么多字符,超出的走 `seq=` 精确读取。 */
|
|
225
|
+
const INLINE_TEXT_LIMIT = 1000;
|
|
226
|
+
/** `raw: true` 时一次最多返回这么多条 —— 结构化原文没有体积上限,必须设桩。 */
|
|
227
|
+
const RAW_PAGE_LIMIT = 20;
|
|
228
|
+
/** `raw: true` 时单次响应的结构化 JSON 总字符预算。 */
|
|
229
|
+
const RAW_CHAR_BUDGET = 120000;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 给非文本块一句能追溯的摘要(类型 + 关键定位字段 + 可回查正文)。
|
|
233
|
+
*
|
|
234
|
+
* ⚠️ 两条历史教训都写在这里:
|
|
235
|
+
* 1. 宿主的 `ImageBlock` 是 `{ type:'image', attachment: {...} }`,真实引用在
|
|
236
|
+
* **`block.attachment`** 下;只查顶层字段会让一张图退化成 `non-text block: image`,
|
|
237
|
+
* 回查的人拿着这句话既找不到文件、也不知道它多大。
|
|
238
|
+
* 2. `ReasoningBlock` 的正文就在 `block.text`,以前被换成 `text=23 chars` ——
|
|
239
|
+
* 推理内容恰恰是回查时最需要的一句"模型当时怎么想的",只给长度等于丢失。
|
|
240
|
+
*
|
|
241
|
+
* 顶层字段仍然照旧输出(有些插件把 `mimeType`/`url` 直接写在块顶上),
|
|
242
|
+
* 所以老的断言形状不变;附件字段随后追加,长度/尺寸用数字原样给。
|
|
243
|
+
*
|
|
244
|
+
* @param {object} block
|
|
245
|
+
* @param {{ compactionId?: string, seq?: number|string, full?: boolean }} [options]
|
|
246
|
+
* @returns {string|null}
|
|
247
|
+
*/
|
|
248
|
+
function nonTextSummary(block, options = {}) {
|
|
249
|
+
const type = typeof block.type === 'string' ? block.type : 'block';
|
|
250
|
+
const bits = [];
|
|
251
|
+
for (const key of ['mimeType', 'mediaType', 'url', 'path', 'filename', 'name', 'attachmentId', 'id']) {
|
|
252
|
+
const value = block[key];
|
|
253
|
+
if (typeof value === 'string' && value.length > 0) bits.push(`${key}=${value}`);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 宿主规范:图片/文件的真实引用在 `block.attachment` 下。
|
|
257
|
+
const attachment = block.attachment;
|
|
258
|
+
if (attachment !== null && typeof attachment === 'object' && !Array.isArray(attachment)) {
|
|
259
|
+
for (const key of ['attachmentId', 'mediaType', 'name', 'bytes', 'width', 'height']) {
|
|
260
|
+
const value = attachment[key];
|
|
261
|
+
if (typeof value === 'string' && value.length > 0) bits.push(`attachment.${key}=${value}`);
|
|
262
|
+
else if (typeof value === 'number' && Number.isFinite(value)) bits.push(`attachment.${key}=${value}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// 正文必须给出来(reasoning 的思维链、附件的提取文本都在这里)。
|
|
267
|
+
// 只给长度是 V8-09 的原始形态:回查的人拿不到内容,等于没归档。
|
|
268
|
+
if (typeof block.text === 'string' && block.text.length > 0) {
|
|
269
|
+
const text = block.text;
|
|
270
|
+
if (options.full === true || text.length <= INLINE_TEXT_LIMIT) {
|
|
271
|
+
bits.push(`text=${JSON.stringify(text)}`);
|
|
272
|
+
} else {
|
|
273
|
+
// 截断时必须给**精确读取入口**,否则"给个长度"这件事只是换了个说法。
|
|
274
|
+
const pointer = options.compactionId
|
|
275
|
+
? ` (full text: history_read compactionId=${options.compactionId} seq=${options.seq} full=true)`
|
|
276
|
+
: '';
|
|
277
|
+
bits.push(`text=${JSON.stringify(text.slice(0, INLINE_TEXT_LIMIT))}+${text.length - INLINE_TEXT_LIMIT} more chars${pointer}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return bits.length === 0 ? `non-text block: ${type}` : `non-text block: ${type} (${bits.join(', ')})`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 一条归档记录的完整结构化形态(含 `blocks` / `source` / `tool`)。
|
|
286
|
+
* 用缩进 JSON 输出,便于回查时逐字段核对,而不是只看到被 formatEntry 压平的那一行。
|
|
287
|
+
*/
|
|
288
|
+
function rawRecord(entry) {
|
|
289
|
+
const record = { seq: entry?.seq ?? null, type: entry?.type ?? null, role: entry?.role ?? null };
|
|
290
|
+
if (entry?.blocks !== undefined) record.blocks = entry.blocks;
|
|
291
|
+
if (entry?.source !== undefined) record.source = entry.source;
|
|
292
|
+
if (entry?.event !== undefined) record.event = entry.event;
|
|
293
|
+
if (entry?.tool !== undefined) record.tool = entry.tool;
|
|
294
|
+
if (typeof entry?.text === 'string') record.text = entry.text;
|
|
295
|
+
return '```json\n' + JSON.stringify(record, null, 2) + '\n```';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 把 `readSpan` 的损坏状态翻译成给人看的一句话。
|
|
300
|
+
* 每种原因都要说清"缺的是什么",而不是统一一句"读取失败"。
|
|
301
|
+
*/
|
|
302
|
+
function formatDamage(compactionId, result) {
|
|
303
|
+
const reason = result?.reason;
|
|
304
|
+
const head = `⚠️ Archive ${compactionId} is INCOMPLETE — the text below is only part of the original span.\n`;
|
|
305
|
+
if (reason === 'missing') return `${head}The file does not exist (it may have been deleted or never written).\n\n`;
|
|
306
|
+
if (reason === 'unreadable') return `${head}The file exists but could not be read (permissions or I/O error).\n\n`;
|
|
307
|
+
if (reason === 'damaged') {
|
|
308
|
+
const lines = Array.isArray(result?.damaged) ? result.damaged.slice(0, 20).join(', ') : '?';
|
|
309
|
+
return `${head}${result.damaged?.length ?? '?'} line(s) are not valid JSON and were skipped (line ${lines}).\n\n`;
|
|
310
|
+
}
|
|
311
|
+
if (reason === 'truncated') {
|
|
312
|
+
return `${head}The file records ${result.expected} entries but only ${result.actual} are present — it was cut short.\n\n`;
|
|
313
|
+
}
|
|
314
|
+
if (reason === 'checksum') {
|
|
315
|
+
return `${head}Entries do not match the checksum in the header — the content was modified after it was written.\n\n`;
|
|
316
|
+
}
|
|
317
|
+
if (reason === 'version') {
|
|
318
|
+
return `${head}The header declares format version ${String(result?.actual)} but this build reads version ${String(result?.expected)} — the file may come from a different release, so its fields cannot be interpreted safely.\n\n`;
|
|
319
|
+
}
|
|
320
|
+
if (reason === 'identity') {
|
|
321
|
+
return `${head}The header says this span belongs to ${String(result?.actual?.sessionId)}/${String(result?.actual?.compactionId)}, not to the span you asked for (${String(result?.expected?.sessionId)}/${String(result?.expected?.compactionId)}) — the file was moved or renamed.\n\n`;
|
|
322
|
+
}
|
|
323
|
+
if (reason === 'bytes') {
|
|
324
|
+
return `${head}The header records ${String(result?.expected)} bytes but the entries occupy ${String(result?.actual)} — the body was rewritten.\n\n`;
|
|
325
|
+
}
|
|
326
|
+
return `${head}No integrity header is present, so completeness cannot be verified.\n\n`;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function clampInt(value, fallback, min, max) {
|
|
330
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return fallback;
|
|
331
|
+
const v = Math.round(value);
|
|
332
|
+
if (v < min) return min;
|
|
333
|
+
if (v > max) return max;
|
|
334
|
+
return v;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 只给测试用的入口。三个 handler 是纯函数(store, args, exec → string),
|
|
339
|
+
* 直接单测比去合成一个宿主 tools 服务靠谱得多 —— 后者测的是桩,不是逻辑。
|
|
340
|
+
*/
|
|
341
|
+
export const __test = { runTool };
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Reviewed fingerprints from unmodified DSH 0.2.2-rc.1, rc.2 and rc.3.
|
|
2
|
+
// These are private host methods. Unknown implementations still refuse writes.
|
|
3
|
+
// rc.2/rc.3 add batch append; the single-event snapshot and publication guards
|
|
4
|
+
// remain equivalent. commit, publicationEntry and persistence methods match rc.1.
|
|
5
|
+
export const HOST_FINGERPRINTS = {
|
|
6
|
+
"session": {
|
|
7
|
+
"append": [
|
|
8
|
+
"f434d8e94303e06be3f3ecddb599f90c7bff421eeb4ca3920611f68925565751",
|
|
9
|
+
"f90d8979863a27976e0e03bda3254ce5f3a07bc830edeae81e0d60c22127bdaf"
|
|
10
|
+
],
|
|
11
|
+
"commit": "8eee8bb4669ebd6e095704c40965cb44b93e599d0138be06f776b15a57fe5051",
|
|
12
|
+
"publicationEntry": "5911ed4cef7de7b69d5fb57da657215a00a9528e3a144961df57fac7eab15bd6"
|
|
13
|
+
},
|
|
14
|
+
"surface": {
|
|
15
|
+
"validateNext": "e8a6063e0309af191f2c8e3cb6a8e3032a467849491854781dc0150e28eac6eb",
|
|
16
|
+
"_processDelta": "e9aeb7cc40961a937f2d919f84acb58a63d24c62eb49db943b14866e71eb75a7"
|
|
17
|
+
},
|
|
18
|
+
"backend": {
|
|
19
|
+
"open": "4a9b09f882a15c402bc07809aadd56a5c56338d146065d8bc9ba6fa58816d450",
|
|
20
|
+
"encodeEventBatch": "fc3cff5eaac5f2e1ac81e08034983a1f803faca7a5ceaeee76cd8348d403e16a",
|
|
21
|
+
"readStoredLog": "64dad5470fc318fa215e55b15a32216826daf9cd3927aab8f46c7da87a995861",
|
|
22
|
+
"decodeStoredLog": "47e9e101e6c07d263c68ae79d86e85a6f45756c95be81ea8a5f48905830ece34",
|
|
23
|
+
"acquireLease": "4a06d1ec0b52691f6a8831bc57645c01323360edd12789ad0f4c2d252e3a48b8",
|
|
24
|
+
"findLog": "4ad82fe5e5bcab8960793b5da04e489f048e83d26a24c25307163b297e6113ad",
|
|
25
|
+
"persistBatch": "227e0f3b840751bb672f401bdfe8600bac4593f63b05e6822a99b965c9830ef7"
|
|
26
|
+
},
|
|
27
|
+
"handle": {
|
|
28
|
+
"run": "acfab5319099452d174470d07ea94a5e14aba287f07eddceec72d18878cf52f9",
|
|
29
|
+
"enqueueLive": "bd14662c1f08e3bfe210e1415d8d18fdbd256d4fdc1287b3f2f5f505906ee4b1",
|
|
30
|
+
"persistContiguous": "283f4de9c0aa8c0df8c0425081408ad689be4dd520e39257d8b7a504db83f7f4",
|
|
31
|
+
"read": "53bc47892a55dbde60afb652093c5f21974f1150cbe5ee762e830e84b0974877",
|
|
32
|
+
"close": "beb89d9c30645186f394525558c0b745d0818ba1cca073aa316efe0dd95fbeb7"
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const REGEX_PREFIX_WORDS = new Set([
|
|
37
|
+
'return', 'throw', 'case', 'typeof', 'void', 'delete',
|
|
38
|
+
'in', 'instanceof', 'new', 'yield', 'await', 'else', 'of',
|
|
39
|
+
]);
|
|
40
|
+
const REGEX_PREFIX_CHARS = new Set([
|
|
41
|
+
'', '(', '[', '{', ',', ';', ':', '?', '=', '!', '&', '|', '^', '~', '+', '-', '*', '%', '<', '>',
|
|
42
|
+
]);
|
|
43
|
+
const ASI_KEYWORDS = new Set(['return', 'throw', 'break', 'continue', 'yield']);
|
|
44
|
+
const isLineTerminator = (ch) => ch === '\n' || ch === '\r' || ch === '\u2028' || ch === '\u2029';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Token-aware normalizer for host method sources: strips comments and insignificant
|
|
48
|
+
* whitespace between syntax tokens while preserving string/template/regex literal
|
|
49
|
+
* interiors and ASI-sensitive newlines.
|
|
50
|
+
*/
|
|
51
|
+
export function normalizeHostMethodSource(src) {
|
|
52
|
+
const text = String(src ?? '');
|
|
53
|
+
const tokens = [];
|
|
54
|
+
const templateBraceDepths = [];
|
|
55
|
+
let i = 0;
|
|
56
|
+
let lastSig = '';
|
|
57
|
+
|
|
58
|
+
const canStartRegex = () => REGEX_PREFIX_CHARS.has(lastSig) || REGEX_PREFIX_WORDS.has(lastSig);
|
|
59
|
+
|
|
60
|
+
const scanTemplateChunk = (startQuote) => {
|
|
61
|
+
let lit = startQuote;
|
|
62
|
+
while (i < text.length) {
|
|
63
|
+
const ch = text[i];
|
|
64
|
+
lit += ch;
|
|
65
|
+
if (ch === '\\' && i + 1 < text.length) {
|
|
66
|
+
lit += text[++i];
|
|
67
|
+
} else if (ch === '$' && text[i + 1] === '{') {
|
|
68
|
+
lit += '{';
|
|
69
|
+
i += 2;
|
|
70
|
+
tokens.push({ type: 'lit', value: lit });
|
|
71
|
+
templateBraceDepths.push(0);
|
|
72
|
+
lastSig = '{';
|
|
73
|
+
return;
|
|
74
|
+
} else if (ch === '`') {
|
|
75
|
+
i += 1;
|
|
76
|
+
tokens.push({ type: 'lit', value: lit });
|
|
77
|
+
lastSig = '`';
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
i += 1;
|
|
81
|
+
}
|
|
82
|
+
tokens.push({ type: 'lit', value: lit });
|
|
83
|
+
lastSig = '`';
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
while (i < text.length) {
|
|
87
|
+
const c = text[i];
|
|
88
|
+
const n = text[i + 1];
|
|
89
|
+
|
|
90
|
+
if (/\s/.test(c)) {
|
|
91
|
+
let newline = false;
|
|
92
|
+
while (i < text.length && /\s/.test(text[i])) {
|
|
93
|
+
if (isLineTerminator(text[i])) newline = true;
|
|
94
|
+
i += 1;
|
|
95
|
+
}
|
|
96
|
+
tokens.push({ type: 'ws', newline });
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (c === '/' && n === '/') {
|
|
101
|
+
i += 2;
|
|
102
|
+
while (i < text.length && !isLineTerminator(text[i])) i += 1;
|
|
103
|
+
tokens.push({ type: 'ws', newline: true });
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (c === '/' && n === '*') {
|
|
108
|
+
let newline = false;
|
|
109
|
+
i += 2;
|
|
110
|
+
while (i < text.length && !(text[i] === '*' && text[i + 1] === '/')) {
|
|
111
|
+
if (isLineTerminator(text[i])) newline = true;
|
|
112
|
+
i += 1;
|
|
113
|
+
}
|
|
114
|
+
i += 2;
|
|
115
|
+
tokens.push({ type: 'ws', newline });
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (c === '"' || c === "'") {
|
|
120
|
+
const q = c;
|
|
121
|
+
let lit = c;
|
|
122
|
+
i += 1;
|
|
123
|
+
while (i < text.length) {
|
|
124
|
+
const ch = text[i];
|
|
125
|
+
lit += ch;
|
|
126
|
+
if (ch === '\\' && i + 1 < text.length) {
|
|
127
|
+
lit += text[++i];
|
|
128
|
+
} else if (ch === q) {
|
|
129
|
+
i += 1;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
i += 1;
|
|
133
|
+
}
|
|
134
|
+
tokens.push({ type: 'lit', value: lit });
|
|
135
|
+
lastSig = q;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (c === '`') {
|
|
140
|
+
i += 1;
|
|
141
|
+
scanTemplateChunk('`');
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (c === '/' && n !== '/' && n !== '*' && canStartRegex()) {
|
|
146
|
+
let rx = c;
|
|
147
|
+
i += 1;
|
|
148
|
+
let inClass = false;
|
|
149
|
+
while (i < text.length) {
|
|
150
|
+
const ch = text[i];
|
|
151
|
+
rx += ch;
|
|
152
|
+
if (ch === '\\' && i + 1 < text.length) {
|
|
153
|
+
rx += text[++i];
|
|
154
|
+
} else if (ch === '[') {
|
|
155
|
+
inClass = true;
|
|
156
|
+
} else if (ch === ']') {
|
|
157
|
+
inClass = false;
|
|
158
|
+
} else if (ch === '/' && !inClass) {
|
|
159
|
+
i += 1;
|
|
160
|
+
while (i < text.length && /[a-z]/i.test(text[i])) rx += text[i++];
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
i += 1;
|
|
164
|
+
}
|
|
165
|
+
tokens.push({ type: 'lit', value: rx });
|
|
166
|
+
lastSig = '/';
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (/[A-Za-z0-9_$]/.test(c)) {
|
|
171
|
+
let word = '';
|
|
172
|
+
while (i < text.length && /[A-Za-z0-9_$]/.test(text[i])) word += text[i++];
|
|
173
|
+
tokens.push({ type: 'word', value: word });
|
|
174
|
+
lastSig = word;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (c === '{' && templateBraceDepths.length > 0) {
|
|
179
|
+
templateBraceDepths[templateBraceDepths.length - 1] += 1;
|
|
180
|
+
} else if (c === '}' && templateBraceDepths.length > 0) {
|
|
181
|
+
if (templateBraceDepths[templateBraceDepths.length - 1] === 0) {
|
|
182
|
+
templateBraceDepths.pop();
|
|
183
|
+
i += 1;
|
|
184
|
+
scanTemplateChunk('}');
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
templateBraceDepths[templateBraceDepths.length - 1] -= 1;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
tokens.push({ type: 'punct', value: c });
|
|
191
|
+
lastSig = c;
|
|
192
|
+
i += 1;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const isWordChar = (ch) => Boolean(ch && /[A-Za-z0-9_$]/.test(ch));
|
|
196
|
+
let out = '';
|
|
197
|
+
let prevTok = null;
|
|
198
|
+
let sawWs = null;
|
|
199
|
+
|
|
200
|
+
for (const tok of tokens) {
|
|
201
|
+
if (tok.type === 'ws') {
|
|
202
|
+
sawWs = { newline: Boolean(sawWs?.newline || tok.newline) };
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (sawWs && prevTok) {
|
|
206
|
+
if (sawWs.newline && prevTok.type === 'word' && ASI_KEYWORDS.has(prevTok.value)) {
|
|
207
|
+
out += '\n';
|
|
208
|
+
} else {
|
|
209
|
+
const prevEnd = prevTok.value.at(-1);
|
|
210
|
+
const nextStart = tok.value[0];
|
|
211
|
+
const needSpace =
|
|
212
|
+
(isWordChar(prevEnd) && isWordChar(nextStart)) ||
|
|
213
|
+
(prevEnd === '+' && nextStart === '+') ||
|
|
214
|
+
(prevEnd === '-' && nextStart === '-') ||
|
|
215
|
+
(prevEnd === '/' && (nextStart === '/' || nextStart === '*'));
|
|
216
|
+
if (needSpace) out += ' ';
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
out += tok.value;
|
|
220
|
+
prevTok = tok;
|
|
221
|
+
sawWs = null;
|
|
222
|
+
}
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export const HOST_NORMALIZED_FINGERPRINTS = {
|
|
227
|
+
"session": {
|
|
228
|
+
"append": ["0a5d34ede29f33476d94e3e08b2344f8a5c8d419d409eb760dfa65b9d4787aa3"],
|
|
229
|
+
"commit": "de2ebf59a9336c6e75c12f2522cfb7f9a2c83ae76fcd643a331966cd17b6c916",
|
|
230
|
+
"publicationEntry": "fcb945bba56f8a130576be4f327569d5ffc5f8f2a130a8f0163335aa34ec8409"
|
|
231
|
+
},
|
|
232
|
+
"surface": {
|
|
233
|
+
"validateNext": "72786bbcd1e645643c3e50c0ef37eb9f7effc8b03738839db1ea9cbc6c1db41b",
|
|
234
|
+
"_processDelta": "626d2425742ae9963591144c3673c7f87c573d694edc62c7173bf5a76f3c3e7d"
|
|
235
|
+
},
|
|
236
|
+
"backend": {
|
|
237
|
+
"open": "7e60b89db746a6afd206d96b3bc5e745ef0994b6e2eec3a61bd9b5f3ca00482e",
|
|
238
|
+
"encodeEventBatch": "bdb7f9c2314c0545cdcc41bdc25f683d2e59de74b7d9a51c1768bccbf9b6eb2e",
|
|
239
|
+
"readStoredLog": "4f0087fcb311a1cfb0eae366b32401496dce810f3b57526cf83c80303eecf4ce",
|
|
240
|
+
"decodeStoredLog": "3c11e76f7eeb44053f1dc795e79d0dd45f4ba09031bbb2eb611f01e51c0c0711",
|
|
241
|
+
"acquireLease": "2c7b76d6f3c2453670b07cb7f905cb01d9312a6ffe8c3cab7e68e476bba22d6f",
|
|
242
|
+
"findLog": "f54a30fe4c33724f9bca4da01152c68f16d68e70bc7e3e72553c62e288809593",
|
|
243
|
+
"persistBatch": "895b3078c92da08d8a733c1519e0b6cdcd2604c8a37227c89cf27abfae335f0a"
|
|
244
|
+
},
|
|
245
|
+
"handle": {
|
|
246
|
+
"run": "f16aa1f5e75487404551d0560280737626eb2fc3b4426526b804c03455ee1fba",
|
|
247
|
+
"enqueueLive": "4dd0bc4d71c595351c74588b3849f32118c366eda326ced88c285d9e9e4dee7e",
|
|
248
|
+
"persistContiguous": "14d1f5bb4bfab04555180b2f46e85155660c644d56df8e777c36ef7b175abe29",
|
|
249
|
+
"read": "9d5389a3531db3658af3272ebcb0730793245b337fe33385062a40635e6d4518",
|
|
250
|
+
"close": "d9fad510536d300ae26a50348b7967732533847546c0161300f4b2f04df4f4e2"
|
|
251
|
+
}
|
|
252
|
+
};
|