mingdao-harness 0.6.22 → 0.6.23
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/docs/CONFIG.md +4 -3
- package/package.json +1 -1
- package/src/atomic-write.js +116 -40
- package/src/session.js +21 -0
- package/src/web/server.js +11 -28
package/docs/CONFIG.md
CHANGED
|
@@ -406,9 +406,10 @@ completion 计费,防止推理吃满上限时空轮白烧)、`compactTrigger
|
|
|
406
406
|
|
|
407
407
|
`GET /api/session?file=` 的每条消息**只增** `ts`(ISO 字符串)。旧会话(v0.6.21 及以前写的
|
|
408
408
|
JSONL 没有 `ts`)返回 `"ts": null`,其余字段语义不变;WebUI 对 `ts` 为 `null` 的消息不渲染时间,
|
|
409
|
-
不会出现 `Invalid Date` / `undefined
|
|
410
|
-
|
|
411
|
-
|
|
409
|
+
不会出现 `Invalid Date` / `undefined`。`ts` 由 `session.appendMessages` **统一补齐**(v0.6.23,§3.87):
|
|
410
|
+
CLI / REPL / 调度 / Web 写入都走这一个入口,只补缺少 `ts` 的消息、已有值原样保留(重复写入与压缩
|
|
411
|
+
重写不刷新时间);读取路径不回填、不改写旧文件,因此旧会话仍返回 `null`。前端不自行推算历史消息时间。
|
|
412
|
+
分组(今天/昨天/本周/更早)与时间显示都在 `src/web/app.js`;分组只在会话列表刷新时计算一次,不引入轮询。
|
|
412
413
|
|
|
413
414
|
## 模型名称以 API 为准(v0.6.19,§3.67)
|
|
414
415
|
|
package/package.json
CHANGED
package/src/atomic-write.js
CHANGED
|
@@ -26,12 +26,79 @@ import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
26
26
|
import { setTimeout as sleepAsync } from 'node:timers/promises';
|
|
27
27
|
import { procAlive } from './proc.js';
|
|
28
28
|
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// 瞬时拒绝(Windows)与有界重试(v0.6.23,§3.86)
|
|
31
|
+
//
|
|
32
|
+
// 证据:CI Windows 腿的 BUG-068(8 个子进程并发 saveTaskStateMerge)曾出现 7/8、6/8,
|
|
33
|
+
// rerun 换运气;macOS/Linux 万条×5 轮、64 进程×5 轮 0 丢。子进程把 saveTaskStateMerge 的
|
|
34
|
+
// {ok:false} 丢进 stdio:'ignore' 后照常 exit 0,所以父进程只看到一个没有归因的差数。
|
|
35
|
+
// 复查后确认:丢更新不是原因,**瞬时拒绝被当成永久失败**才是——NTFS 上刚 unlink 的
|
|
36
|
+
// `.lock` 仍处于 delete-pending 时 `open(…,'wx')` 会被拒 EACCES/EPERM;Defender/索引器
|
|
37
|
+
// 正在扫描刚写出的 `.tmp` 时 `rename` 会被拒 EACCES/EPERM/EBUSY。这两种都不是"权限配错",
|
|
38
|
+
// 也不是"别人长期持锁",而是**争用的一种**:等几十~几百毫秒就会自己消失。
|
|
39
|
+
//
|
|
40
|
+
// 所以把 EACCES/EPERM/EBUSY 与 EEXIST 一样纳入"等待 + 重试",但**单独给 2 秒预算**:
|
|
41
|
+
// · 初始 5ms:锁刚被 unlink 时 delete-pending 通常几个 tick 就清掉,早试能少等;
|
|
42
|
+
// · 每次 ×2、封顶 100ms:既不在前 100ms 里高频敲文件系统(杀软扫描时高频重试只会更糟),
|
|
43
|
+
// 又保证晚期的恢复延迟不超过 100ms;
|
|
44
|
+
// · 总预算 2000ms:Windows 杀软/索引器的单文件扫描窗口在数十~数百 ms 量级,
|
|
45
|
+
// 2s 有约 10~100 倍余量;同时**有界**——真正的 ACL/只读目录问题不会把进程永久冻住,
|
|
46
|
+
// 也不会把一次保存拖成整个 WebUI 的几十秒卡顿(同步锁仍走 Atomics.wait)。
|
|
47
|
+
// · EEXIST(确实有人在持锁)不适用这个 2s 预算:它继续走原来的 timeoutMs(5s)/staleMs(4s)。
|
|
48
|
+
// 预算耗尽时**原样抛出原始错误**(保留 errno/path),不折成"锁超时"——后者会把权限/杀软
|
|
49
|
+
// 问题伪装成死锁,用户按提示删锁也没用。
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
const TRANSIENT_DENY_CODES = new Set(['EACCES', 'EPERM', 'EBUSY']);
|
|
52
|
+
const TRANSIENT_RETRY_INITIAL_MS = 5;
|
|
53
|
+
const TRANSIENT_RETRY_MAX_MS = 100;
|
|
54
|
+
const TRANSIENT_RETRY_BUDGET_MS = 2000;
|
|
55
|
+
|
|
56
|
+
/** 某个 errno 是否属于「等一会儿可能自己好」的瞬时拒绝。 @param {any} code */
|
|
57
|
+
export function isTransientDenyCode(code) {
|
|
58
|
+
return TRANSIENT_DENY_CODES.has(String(code || ''));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 退避参数(导出供诊断与测试断言「5–10ms 起 / 封顶 / 总预算有界」)。 */
|
|
62
|
+
export function retryDefaults() {
|
|
63
|
+
return {
|
|
64
|
+
initialMs: TRANSIENT_RETRY_INITIAL_MS,
|
|
65
|
+
maxDelayMs: TRANSIENT_RETRY_MAX_MS,
|
|
66
|
+
budgetMs: TRANSIENT_RETRY_BUDGET_MS,
|
|
67
|
+
codes: [...TRANSIENT_DENY_CODES],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
72
|
+
const sleepMs = (/** @type {number} */ ms) => Atomics.wait(sleepBuf, 0, 0, ms);
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 在 deadline 之前对瞬时拒绝做指数退避重试;非瞬时错误与预算耗尽都**原样抛出**。
|
|
76
|
+
* deadline 由调用方传入:一次原子写的 write+rename 共享同一份预算,避免两段各等 2 秒。
|
|
77
|
+
* @template T @param {() => T} fn @param {number} deadline @returns {T}
|
|
78
|
+
*/
|
|
79
|
+
function retryTransientSync(fn, deadline) {
|
|
80
|
+
let delay = TRANSIENT_RETRY_INITIAL_MS;
|
|
81
|
+
for (;;) {
|
|
82
|
+
try {
|
|
83
|
+
return fn();
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (!isTransientDenyCode(/** @type {any} */ (err)?.code)) throw err;
|
|
86
|
+
const remain = deadline - Date.now();
|
|
87
|
+
if (remain <= 0) throw err;
|
|
88
|
+
sleepMs(Math.min(delay, remain));
|
|
89
|
+
delay = Math.min(delay * 2, TRANSIENT_RETRY_MAX_MS);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
29
94
|
export function atomicWriteFileSync(/** @type {string} */ target, /** @type {string|Buffer} */ data, /** @type {any} */ options = {}) {
|
|
30
95
|
const dir = path.dirname(target);
|
|
31
96
|
const tmp = path.join(dir, `.${path.basename(target)}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.tmp`);
|
|
97
|
+
const deadline = Date.now() + TRANSIENT_RETRY_BUDGET_MS;
|
|
32
98
|
try {
|
|
33
|
-
|
|
34
|
-
fs.
|
|
99
|
+
// Windows:杀软/索引器短暂持有新建的 .tmp(EACCES/EPERM/EBUSY)或拒绝覆盖目标,都等一等再试。
|
|
100
|
+
retryTransientSync(() => fs.writeFileSync(tmp, data, options), deadline);
|
|
101
|
+
retryTransientSync(() => fs.renameSync(tmp, target), deadline);
|
|
35
102
|
} catch (err) {
|
|
36
103
|
try {
|
|
37
104
|
fs.unlinkSync(tmp);
|
|
@@ -81,8 +148,6 @@ export function atomicWriteJsonSync(/** @type {string} */ target, /** @type {any
|
|
|
81
148
|
|
|
82
149
|
// 极简互斥锁:O_EXCL 创建 lockfile;持锁期间执行 fn(同步/异步);异常/完成释放。
|
|
83
150
|
// 进程崩溃遗留的陈旧锁自动回收,避免永久卡死。
|
|
84
|
-
const sleepBuf = new Int32Array(new SharedArrayBuffer(4));
|
|
85
|
-
const sleepMs = (/** @type {number} */ ms) => Atomics.wait(sleepBuf, 0, 0, ms);
|
|
86
151
|
|
|
87
152
|
// 可重入集合按**调用链**隔离(见文件头注释:异步临界区会 yield,进程级 Set 会让并发任务互相穿透)
|
|
88
153
|
const lockScope = new AsyncLocalStorage();
|
|
@@ -95,16 +160,23 @@ function runWithHeld(fn) {
|
|
|
95
160
|
}
|
|
96
161
|
|
|
97
162
|
/**
|
|
98
|
-
*
|
|
99
|
-
*
|
|
163
|
+
* 尝试独占创建锁文件。**返回结构化结果而不是用异常表达"忙"**:
|
|
164
|
+
* · `{ok:true}` —— 拿到了锁;
|
|
165
|
+
* · `{ok:false, code:'EEXIST', error}` —— 锁确实被别人持有(按 timeoutMs 等待,可回收陈旧锁);
|
|
166
|
+
* · `{ok:false, code:'EACCES'|'EPERM'|'EBUSY', error}` —— Windows 的**瞬时拒绝**:NTFS
|
|
167
|
+
* delete-pending / 杀软/索引器扫描 / 共享冲突。它既不是"权限配错"也不是"长期持锁",
|
|
168
|
+
* 由调用方走独立的 2s 退避预算;预算耗尽时抛 `error`(保留 errno 原文)。
|
|
169
|
+
* 其余错误(路径非法等)仍**直接抛出**——那不是争用,装成争用只会掩盖真问题。
|
|
100
170
|
* @param {string} lockPath
|
|
171
|
+
* @returns {{ok: true} | {ok: false, code: string, error: any}}
|
|
101
172
|
*/
|
|
102
173
|
function tryAcquire(lockPath) {
|
|
103
174
|
let fd;
|
|
104
175
|
try {
|
|
105
176
|
fd = fs.openSync(lockPath, 'wx');
|
|
106
177
|
} catch (err) {
|
|
107
|
-
|
|
178
|
+
const code = String(/** @type {any} */ (err)?.code || '');
|
|
179
|
+
if (code === 'EEXIST' || isTransientDenyCode(code)) return { ok: false, code, error: err };
|
|
108
180
|
throw err;
|
|
109
181
|
}
|
|
110
182
|
try {
|
|
@@ -124,7 +196,7 @@ function tryAcquire(lockPath) {
|
|
|
124
196
|
} finally {
|
|
125
197
|
fs.closeSync(fd);
|
|
126
198
|
}
|
|
127
|
-
return true;
|
|
199
|
+
return { ok: true };
|
|
128
200
|
}
|
|
129
201
|
|
|
130
202
|
/** @param {string} lockPath */
|
|
@@ -217,39 +289,34 @@ export function withFileLockSync(/** @type {string} */ lockPath, /** @type {() =
|
|
|
217
289
|
if (held.has(lockPath)) return fn();
|
|
218
290
|
fs.mkdirSync(path.dirname(lockPath), { recursive: true });
|
|
219
291
|
const t0 = Date.now();
|
|
292
|
+
let delay = TRANSIENT_RETRY_INITIAL_MS;
|
|
293
|
+
// 第一次遇到瞬时拒绝的时刻(EEXIST 不参与这个预算,见文件头参数说明)
|
|
294
|
+
let transientSince = 0;
|
|
220
295
|
for (;;) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// (100% CPU,连超时分支都不可达)。acquiring 在 fn 开始执行前置 false。
|
|
224
|
-
let acquiring = true;
|
|
225
|
-
try {
|
|
226
|
-
if (!tryAcquire(lockPath)) {
|
|
227
|
-
const e = /** @type {any} */ (new Error('EEXIST'));
|
|
228
|
-
e.code = 'EEXIST';
|
|
229
|
-
throw e;
|
|
230
|
-
}
|
|
296
|
+
const got = tryAcquire(lockPath);
|
|
297
|
+
if (got.ok) {
|
|
231
298
|
held.add(lockPath);
|
|
232
|
-
|
|
299
|
+
// v0.4.7:fn 在 try/finally 里直接执行,**不进入抢锁的 catch**——
|
|
300
|
+
// 否则 fn 自身抛出的 EEXIST 会被当成抢锁失败,重抢后再次执行 fn → 同步死循环。
|
|
233
301
|
try {
|
|
234
302
|
return fn();
|
|
235
303
|
} finally {
|
|
236
304
|
held.delete(lockPath);
|
|
237
305
|
releaseLock(lockPath);
|
|
238
306
|
}
|
|
239
|
-
} catch (err) {
|
|
240
|
-
if (!acquiring) throw err; // fn 自身的异常绝不进入锁重试
|
|
241
|
-
if (/** @type {any} */ (err).code !== 'EEXIST') throw err;
|
|
242
|
-
// 审计 BUG-047(实测复现):回收陈旧锁的分支原先直接 `continue`,**跳过了下面的超时判断**——
|
|
243
|
-
// 当陈旧锁删不掉(只读目录 / EPERM / EIO;实测 chmod 555 即可复现)时 reclaimIfStale
|
|
244
|
-
// 永远返回 true,于是无限空转:99% CPU、5 秒超时失效、进程不返回。
|
|
245
|
-
// 「回收过」不构成「可以不计时地重试」,两条路径都必须过超时闸。
|
|
246
|
-
const reclaimed = reclaimIfStale(lockPath, staleMs);
|
|
247
|
-
if (Date.now() - t0 > timeoutMs) {
|
|
248
|
-
throw lockTimeoutError(lockPath, timeoutMs);
|
|
249
|
-
}
|
|
250
|
-
sleepMs(25);
|
|
251
|
-
if (reclaimed) continue; // 回收后立刻重试;否则同样按 25ms 节流再试
|
|
252
307
|
}
|
|
308
|
+
if (got.code === 'EEXIST') {
|
|
309
|
+
// 审计 BUG-047:回收陈旧锁的分支不得绕过超时闸,否则陈旧锁删不掉(只读目录/EPERM)
|
|
310
|
+
// 时 reclaimIfStale 恒返回 true → 无限空转、超时失效。
|
|
311
|
+
reclaimIfStale(lockPath, staleMs);
|
|
312
|
+
if (Date.now() - t0 > timeoutMs) throw lockTimeoutError(lockPath, timeoutMs);
|
|
313
|
+
} else {
|
|
314
|
+
// EACCES/EPERM/EBUSY:Windows 的瞬时拒绝,走独立的 2s 预算;耗尽时抛原始 errno。
|
|
315
|
+
if (!transientSince) transientSince = Date.now();
|
|
316
|
+
if (Date.now() - transientSince >= TRANSIENT_RETRY_BUDGET_MS) throw got.error;
|
|
317
|
+
}
|
|
318
|
+
sleepMs(delay);
|
|
319
|
+
delay = Math.min(delay * 2, TRANSIENT_RETRY_MAX_MS);
|
|
253
320
|
}
|
|
254
321
|
});
|
|
255
322
|
}
|
|
@@ -262,15 +329,21 @@ export function withFileLockSync(/** @type {string} */ lockPath, /** @type {() =
|
|
|
262
329
|
* 为什么值得单独一个函数而不是把同步版改成异步:调用链里不少地方本身就是同步的
|
|
263
330
|
* (CLI 一次性命令、调度守护进程内部的读-改-写),强行异步化会把 async 传染到 24 个调用点,
|
|
264
331
|
* 回归风险大于收益。做法是**逐个判断**:请求路径上的迁移,纯同步链保留同步版。
|
|
332
|
+
*
|
|
333
|
+
* 等待策略与同步版逐字一致:EEXIST 走 timeoutMs,EACCES/EPERM/EBUSY 走 2s 瞬时预算;
|
|
334
|
+
* `pollMs` 是**首次**等待时长,之后每次 ×2、封顶 100ms(旧版是固定 25ms 轮询)。
|
|
265
335
|
* @param {string} lockPath @param {() => any} fn
|
|
266
336
|
*/
|
|
267
|
-
export async function withFileLock(/** @type {string} */ lockPath, /** @type {() => any} */ fn, { timeoutMs = DEFAULT_TIMEOUT_MS, staleMs = DEFAULT_STALE_MS, pollMs =
|
|
337
|
+
export async function withFileLock(/** @type {string} */ lockPath, /** @type {() => any} */ fn, { timeoutMs = DEFAULT_TIMEOUT_MS, staleMs = DEFAULT_STALE_MS, pollMs = TRANSIENT_RETRY_INITIAL_MS } = {}) {
|
|
268
338
|
return runWithHeld(async (held) => {
|
|
269
339
|
if (held.has(lockPath)) return fn();
|
|
270
340
|
fs.mkdirSync(path.dirname(lockPath), { recursive: true });
|
|
271
341
|
const t0 = Date.now();
|
|
342
|
+
let delay = Number(pollMs) > 0 ? Number(pollMs) : TRANSIENT_RETRY_INITIAL_MS;
|
|
343
|
+
let transientSince = 0;
|
|
272
344
|
for (;;) {
|
|
273
|
-
|
|
345
|
+
const got = tryAcquire(lockPath);
|
|
346
|
+
if (got.ok) {
|
|
274
347
|
held.add(lockPath);
|
|
275
348
|
try {
|
|
276
349
|
return await fn();
|
|
@@ -281,13 +354,16 @@ export async function withFileLock(/** @type {string} */ lockPath, /** @type {()
|
|
|
281
354
|
releaseLock(lockPath);
|
|
282
355
|
}
|
|
283
356
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
throw lockTimeoutError(lockPath, timeoutMs);
|
|
357
|
+
if (got.code === 'EEXIST') {
|
|
358
|
+
// 与同步版同一条修正(审计 BUG-047):回收分支不得绕过超时闸,否则陈旧锁删不掉时永远自旋
|
|
359
|
+
reclaimIfStale(lockPath, staleMs);
|
|
360
|
+
if (Date.now() - t0 > timeoutMs) throw lockTimeoutError(lockPath, timeoutMs);
|
|
361
|
+
} else {
|
|
362
|
+
if (!transientSince) transientSince = Date.now();
|
|
363
|
+
if (Date.now() - transientSince >= TRANSIENT_RETRY_BUDGET_MS) throw got.error;
|
|
288
364
|
}
|
|
289
|
-
await sleepAsync(
|
|
290
|
-
|
|
365
|
+
await sleepAsync(delay);
|
|
366
|
+
delay = Math.min(delay * 2, TRANSIENT_RETRY_MAX_MS);
|
|
291
367
|
}
|
|
292
368
|
});
|
|
293
369
|
}
|
package/src/session.js
CHANGED
|
@@ -60,9 +60,30 @@ export function createSession(home) {
|
|
|
60
60
|
return { file, name: path.basename(file) };
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
/**
|
|
64
|
+
* v0.6.23(§3.87):会话消息 `ts` 的**唯一补齐点**。
|
|
65
|
+
*
|
|
66
|
+
* 为什么放在这一层:CLI / REPL / 调度 worker / Web 落盘最终都走 appendMessages;
|
|
67
|
+
* 此前只有 Web server 在落盘前自己 stamp,导致旧会话与非 Web 会话的消息在界面上没有时间。
|
|
68
|
+
* 规则:只给**缺少 ts** 的消息补一个 ISO 字符串,已有 ts 原样保留(重复写入不刷新时间);
|
|
69
|
+
* 就地写回同一批消息对象,这样自动压缩的整文件重写(rewriteSession)能带上刚写下的 ts。
|
|
70
|
+
* 读取路径不调用它:旧会话缺 ts 仍返回 null,绝不回填/改写历史文件。
|
|
71
|
+
* @param {any[]} messages
|
|
72
|
+
* @param {Date} [now]
|
|
73
|
+
*/
|
|
74
|
+
export function stampMessageTs(messages, now = new Date()) {
|
|
75
|
+
const ts = now.toISOString();
|
|
76
|
+
for (const m of messages) {
|
|
77
|
+
if (!m || typeof m !== 'object' || m.ts) continue;
|
|
78
|
+
m.ts = ts;
|
|
79
|
+
}
|
|
80
|
+
return messages;
|
|
81
|
+
}
|
|
82
|
+
|
|
63
83
|
/** @param {any} file @param {any} messages */
|
|
64
84
|
export function appendMessages(file, messages) {
|
|
65
85
|
if (!messages?.length) return;
|
|
86
|
+
stampMessageTs(messages); // 单一来源:所有写入路径在这里补 ts(已有 ts 不覆盖)
|
|
66
87
|
const lines = messages.map((/** @type {any} */ m) => JSON.stringify(m)).join('\n') + '\n';
|
|
67
88
|
appendFilePrivateSync(file, lines);
|
|
68
89
|
}
|
package/src/web/server.js
CHANGED
|
@@ -77,24 +77,8 @@ import { syncStatus, syncLogin, syncLogout, syncPush, syncPull, syncRemoteList,
|
|
|
77
77
|
|
|
78
78
|
const INDEX_HTML = path.join(path.dirname(fileURLToPath(import.meta.url)), 'index.html');
|
|
79
79
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*
|
|
83
|
-
* 只补缺 ts 的消息(`{...m, ts}` 不改动原对象),已有 ts 的原样保留:
|
|
84
|
-
* 旧会话/旧消息不会被"补写"成另一个时间;压缩重写时也不会把历史时间冲掉。
|
|
85
|
-
* 时间只在服务端落盘这一刻取,前端渲染用同一字段——不引入第二套时间来源。
|
|
86
|
-
* @template T
|
|
87
|
-
* @param {T[]} messages
|
|
88
|
-
* @param {Date} [now]
|
|
89
|
-
* @returns {T[]}
|
|
90
|
-
*/
|
|
91
|
-
function stampMessageTs(messages, now = new Date()) {
|
|
92
|
-
const ts = now.toISOString();
|
|
93
|
-
return messages.map((/** @type {any} */ m) => {
|
|
94
|
-
if (!m || typeof m !== 'object' || m.ts) return m;
|
|
95
|
-
return { ...m, ts };
|
|
96
|
-
});
|
|
97
|
-
}
|
|
80
|
+
// v0.6.23(§3.87):Web 落盘**不再自建 stamp**——ts 的唯一补齐点是 `src/session.js` 的
|
|
81
|
+
// `appendMessages()`(CLI / REPL / 调度 / Web 共用);下面两处 chat 落盘都只调它,不引入第二套时间来源。
|
|
98
82
|
|
|
99
83
|
/** 合法权限模式(与 /api/config 的校验同一口径:非法值一律忽略,绝不 fail-open)。 */
|
|
100
84
|
const PERMISSION_MODES = new Set(['ask', 'auto', 'readonly']);
|
|
@@ -902,13 +886,14 @@ export async function runWebServer({ host = '127.0.0.1', port = 3820, authToken,
|
|
|
902
886
|
}
|
|
903
887
|
messages.push({ role: 'user', content: built.content });
|
|
904
888
|
// 落盘用文本版(图文数组只发给模型,会话文件保持字符串可渲染)。
|
|
905
|
-
// v0.6.
|
|
906
|
-
//
|
|
907
|
-
|
|
908
|
-
|
|
889
|
+
// v0.6.23(§3.87):ts 由 session.appendMessages 统一补齐(唯一来源);把落盘对象上的 ts
|
|
890
|
+
// 同步回内存里的这条消息,后续自动压缩整文件重写时才不会把这一轮刚写下的时间冲掉。
|
|
891
|
+
/** @type {any} */
|
|
892
|
+
const persistUser = { role: 'user', content: built.persistText };
|
|
893
|
+
await withSessionLock(session.file, () => appendMessages(session.file, [persistUser]));
|
|
909
894
|
{
|
|
910
895
|
const lastUserMsg = messages[messages.length - 1];
|
|
911
|
-
if (lastUserMsg && typeof lastUserMsg === 'object' && !lastUserMsg.ts) lastUserMsg.ts =
|
|
896
|
+
if (lastUserMsg && typeof lastUserMsg === 'object' && !lastUserMsg.ts && persistUser.ts) lastUserMsg.ts = persistUser.ts;
|
|
912
897
|
}
|
|
913
898
|
let persistedBefore = messages.length;
|
|
914
899
|
|
|
@@ -1091,13 +1076,11 @@ export async function runWebServer({ host = '127.0.0.1', port = 3820, authToken,
|
|
|
1091
1076
|
// 会话级路由统计(Hermes C2 升级检测):累计工具步数与截断次数,粘滞 flash 会话复杂度上来后自动升 planner
|
|
1092
1077
|
const st = session.routeStats || { steps: 0, truncated: 0 };
|
|
1093
1078
|
session.routeStats = { steps: st.steps + (io.stats().toolCount || 0), truncated: st.truncated + (r.truncated ? 1 : 0) };
|
|
1094
|
-
// v0.6.
|
|
1095
|
-
//
|
|
1079
|
+
// v0.6.23(§3.87):本回合新增的 assistant/tool 消息由 appendMessages 就地补 ts,
|
|
1080
|
+
// 内存数组随之带上时间戳——之后自动压缩整文件重写保留这些时间(历史消息不动)。
|
|
1096
1081
|
{
|
|
1097
1082
|
const tail = messages.slice(persistedBefore);
|
|
1098
|
-
|
|
1099
|
-
for (let i = 0; i < stamped.length; i += 1) messages[persistedBefore + i] = stamped[i];
|
|
1100
|
-
await withSessionLock(session.file, () => appendMessages(session.file, stamped));
|
|
1083
|
+
await withSessionLock(session.file, () => appendMessages(session.file, tail));
|
|
1101
1084
|
}
|
|
1102
1085
|
io.printUsageLine({ modelName: runModel, usage: r.usage, durationMs: r.durationMs });
|
|
1103
1086
|
// v0.4.4:补记最后一轮 + 兜底总结的剩余 usage(自动续跑轮已在 onUsage 逐轮入账,避免重复计)
|