@jjh32456/dsh-longlive 1.6.7 → 1.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/CHANGELOG.md +70 -1508
- package/LICENSE +1 -1
- package/QUICKSTART.md +34 -13
- package/README.en.md +18 -7
- package/README.md +34 -9
- package/SPEC.md +5 -5
- package/cordis.patch.yml +23 -9
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +4 -3
- package/docs/AUTO-RECALL-WIRING.md +1 -1
- package/docs/AUTO-RECALL.md +15 -9
- package/docs/CONFIG.md +168 -35
- package/docs/ERRORS.md +17 -5
- package/docs/FAQ.md +7 -7
- package/docs/FEATURES.md +96 -28
- package/docs/MIGRATION-1.6.0.md +2 -2
- package/docs/OPERATIONS.md +6 -6
- package/docs/PANEL-INTEGRATION.md +6 -0
- package/docs/PRIVACY.md +88 -0
- package/docs/QUICKSTART.md +5 -5
- package/docs/README.en.md +4 -2
- package/docs/RESIDENT-WATCHER.md +1 -1
- package/docs/SESSION-WATCH.md +6 -6
- package/docs/TROUBLESHOOTING.md +4 -4
- package/package.json +47 -117
- package/runbook.md +2 -2
- package/scripts/backfill-auto-cards.mjs +98 -0
- package/scripts/install-surgery-task.ps1 +5 -2
- package/scripts/lib/session-watch-fixture.mjs +1 -1
- package/scripts/longlive-doctor.mjs +6 -0
- package/scripts/migrate-180.mjs +163 -0
- package/scripts/recall-index-build.mjs +128 -0
- package/scripts/session-watch-drill.mjs +1 -1
- package/scripts/surgery-run.cmd +1 -1
- package/scripts/unattended-surgery.ps1 +5 -2
- package/scripts/verify-card-anchors.mjs +138 -0
- package/src/actions/audit.js +125 -0
- package/src/actions/evidence.js +122 -0
- package/src/actions/fs.js +162 -0
- package/src/activity/index.js +760 -0
- package/src/activity/model.js +351 -0
- package/src/activity/store.js +644 -0
- package/src/activity/thinking.js +176 -0
- package/src/adapters/constants.js +11 -0
- package/src/adapters/engram/data/aliases.json +51 -0
- package/src/adapters/engram/data/policy.json +8 -0
- package/src/adapters/engram/data/routing-rules.json +546 -0
- package/src/adapters/engram/index.js +235 -0
- package/src/adapters/registry.js +315 -0
- package/src/answer/audit.js +193 -0
- package/src/answer/evidence.js +30 -0
- package/src/answer/guard.js +1370 -0
- package/src/answer/index.js +1067 -0
- package/src/answer/query.js +111 -0
- package/src/answer/second-hop.mjs +101 -0
- package/src/auto-truncate.js +1 -1
- package/src/config.js +171 -47
- package/src/deps.js +6 -4
- package/src/features.js +182 -6
- package/src/heartbeat.js +156 -23
- package/src/identity/handover.js +52 -1
- package/src/identity/index.js +13 -5
- package/src/identity/inject-position.js +2 -2
- package/src/index.js +97 -7
- package/src/lifecycle/gap-clocks.js +328 -0
- package/src/lifecycle/index.js +269 -0
- package/src/live.js +22 -2
- package/src/memory/activity-capture.js +229 -0
- package/src/memory/anchors.js +194 -0
- package/src/memory/audit-records.js +222 -0
- package/src/memory/auto-cards.js +401 -0
- package/src/memory/capture.js +442 -0
- package/src/memory/card-anchors.js +203 -0
- package/src/memory/conflicts.js +106 -0
- package/src/memory/decay.js +311 -0
- package/src/memory/distill.js +471 -0
- package/src/memory/event-index.js +917 -0
- package/src/memory/evidence-chain.js +295 -0
- package/src/memory/evidence.js +145 -0
- package/src/memory/filter-runner.js +1 -1
- package/src/memory/filter.js +5 -1
- package/src/memory/forget-v2.js +410 -0
- package/src/memory/forget.js +71 -0
- package/src/memory/frames-activation.js +247 -0
- package/src/memory/fts-query.js +174 -0
- package/src/memory/governance.js +604 -0
- package/src/memory/index.js +1 -0
- package/src/memory/l2-gate.js +169 -0
- package/src/memory/lock.js +121 -0
- package/src/memory/migrate.js +139 -0
- package/src/memory/recall-v2.js +223 -0
- package/src/memory/recall.js +65 -2
- package/src/memory/records.js +425 -0
- package/src/memory/retention.js +128 -0
- package/src/memory/review.js +124 -0
- package/src/memory/trust.js +88 -0
- package/src/memory/version-chain.js +264 -0
- package/src/memory/wal.js +137 -0
- package/src/memory/working-frame.js +516 -0
- package/src/metrics/classify.mjs +5 -3
- package/src/metrics/index.mjs +21 -1
- package/src/metrics/prestep-latency.mjs +300 -0
- package/src/metrics/store.mjs +25 -0
- package/src/policy-profile.js +18 -3
- package/src/recall/anaphora.mjs +87 -0
- package/src/recall/cards.mjs +7 -0
- package/src/recall/embedding.mjs +8 -8
- package/src/recall/entities.mjs +112 -0
- package/src/recall/index.js +235 -19
- package/src/recall/inject-order.mjs +10 -8
- package/src/recall/privacy.mjs +105 -16
- package/src/recall/recall-core.mjs +116 -10
- package/src/recall/shared-visibility.js +302 -0
- package/src/recall/time-normalizer.mjs +196 -0
- package/src/role/activation.js +191 -0
- package/src/role/attention.js +318 -0
- package/src/role/base.js +36 -0
- package/src/role/channels.js +423 -0
- package/src/role/index.js +425 -0
- package/src/role/interfaces.js +115 -0
- package/src/role/personality.js +370 -0
- package/src/role/readers.js +151 -0
- package/src/role/relationships.js +477 -0
- package/src/role/store.js +202 -0
- package/src/role/writeprotect.js +373 -0
- package/src/router/aliases.json +176 -25
- package/src/router/background/agent-runner.mjs +1 -1
- package/src/router/background/anchor.mjs +1 -1
- package/src/router/background/audit.mjs +2 -2
- package/src/router/background/budget-ledger.mjs +2 -2
- package/src/router/background/host-sim.mjs +1 -1
- package/src/router/background/memory.mjs +4 -4
- package/src/router/background/queue.mjs +1 -1
- package/src/router/background/retry.mjs +1 -1
- package/src/router/background/router.mjs +1 -1
- package/src/router/background/stream.mjs +1 -1
- package/src/router/background/tool-policy.mjs +1 -1
- package/src/router/design-headings.json +5 -5
- package/src/router/face/config.mjs +1 -1
- package/src/router/face/identity.mjs +3 -3
- package/src/router/judge.mjs +8 -7
- package/src/router/policies/generic.json +21 -4
- package/src/router/route.js +109 -22
- package/src/router/router.mjs +10 -6
- package/src/router/routing-rules.json +260 -445
- package/src/router/run-sim.mjs +8 -8
- package/src/router/runtime.js +23 -12
- package/src/router/service/board.mjs +95 -15
- package/src/router/service/config.mjs +5 -5
- package/src/router/service/context.mjs +1 -1
- package/src/router/service/hint.mjs +14 -0
- package/src/router/service/queue.mjs +1 -1
- package/src/router/service/service.mjs +27 -11
- package/src/router/shadow.mjs +4 -3
- package/src/surgery-recovery.js +2 -2
- package/src/tool-output.js +2 -2
- package/src/tools.js +39 -7
- package/src/truncate-visible.js +2 -2
- package/src/ui/actions.js +1312 -0
- package/src/ui/panel-api.js +367 -0
- package/src/ui/panel-auth.js +251 -0
- package/src/ui/panel-integrity.js +243 -0
- package/src/ui/routes-readonly.js +695 -0
- package/src/ui/routes.js +3 -0
- package/docs/DEV.md +0 -279
- package/docs/PRESENCE-BUS-WIRING.md +0 -491
- package/docs/PRESENCE-BUS.md +0 -694
- package/docs/SURGERY.md +0 -186
- package/scripts/mark-core-cards.mjs +0 -289
- package/scripts/pack.mjs +0 -392
- package/scripts/router-proposals.mjs +0 -386
- package/src/router/fixtures/M1.json +0 -16
- package/src/router/fixtures/M2.json +0 -16
- package/src/router/fixtures/M3.json +0 -20
- package/src/router/fixtures/M4.json +0 -20
- package/src/router/fixtures/M5.json +0 -45
- package/src/router/fixtures/M6.json +0 -53
- package/src/router/fixtures/M7.json +0 -46
- package/src/router/fixtures/S1.json +0 -20
- package/src/router/fixtures/S2.json +0 -23
- package/src/router/fixtures/S3.json +0 -20
- package/src/router/fixtures/S4.json +0 -34
- package/src/router/fixtures/S5.json +0 -43
- package/src/router/fixtures/S6.json +0 -47
- package/src/router/fixtures/T1.json +0 -23
- package/src/router/fixtures/T2.json +0 -16
- package/src/router/fixtures/T3.json +0 -19
- package/src/router/fixtures/T4.json +0 -16
- package/src/router/fixtures/X1.json +0 -17
- package/src/router/fixtures/X2.json +0 -20
- package/src/router/gen-design-headings.mjs +0 -107
- package/src/router/policies/owner-rulings-2026-11-03.json +0 -62
- package/test/auto-recall.test.mjs +0 -474
- package/test/auto-truncate.test.mjs +0 -931
- package/test/autobind.test.mjs +0 -385
- package/test/bus.test.mjs +0 -1572
- package/test/cli-guard.test.mjs +0 -198
- package/test/conclusion.test.mjs +0 -408
- package/test/config-schema.test.mjs +0 -204
- package/test/d6-output.test.mjs +0 -194
- package/test/d6.test.mjs +0 -395
- package/test/deps.test.mjs +0 -342
- package/test/doctor-setup.test.mjs +0 -297
- package/test/embedding-recall.test.mjs +0 -347
- package/test/feature-flags.test.mjs +0 -568
- package/test/filter-shadow.test.mjs +0 -170
- package/test/fixtures/baseline-0.8.0-registration.json +0 -556
- package/test/fixtures/d6-header-tools.json +0 -1
- package/test/fixtures/lock-child.mjs +0 -67
- package/test/fixtures/pre-step-container.mjs +0 -100
- package/test/fixtures/privacy-memory/topics/pii-unmarked.md +0 -9
- package/test/fixtures/privacy-memory/topics/secret-local-only.md +0 -11
- package/test/fixtures/profile-session-longlive-config.json +0 -20
- package/test/fixtures/proposal-cas-child.mjs +0 -66
- package/test/host-engine-status.test.mjs +0 -307
- package/test/hot-truncate.test.mjs +0 -1349
- package/test/identity-card-marker.test.mjs +0 -307
- package/test/identity-router/run.mjs +0 -721
- package/test/integration-pre-step.test.mjs +0 -553
- package/test/live.test.mjs +0 -1020
- package/test/maintenance-schedule.test.mjs +0 -538
- package/test/memory.test.mjs +0 -2828
- package/test/metrics.test.mjs +0 -478
- package/test/orchestrator.test.mjs +0 -266
- package/test/p1_probe.mjs +0 -97
- package/test/packaging.test.mjs +0 -182
- package/test/portability.test.mjs +0 -343
- package/test/rc4-regressions.test.mjs +0 -562
- package/test/rc5-regressions.test.mjs +0 -197
- package/test/rc6-chinese.test.mjs +0 -394
- package/test/router-1100/apply-off.test.mjs +0 -175
- package/test/router-1100/budget-reconcile.test.mjs +0 -215
- package/test/router-1100/config-context.test.mjs +0 -211
- package/test/router-1100/helpers.mjs +0 -129
- package/test/router-1100/hint-envelope.test.mjs +0 -150
- package/test/router-1100/lock-crossproc.test.mjs +0 -206
- package/test/router-1100/proposal-cas-crossproc.test.mjs +0 -184
- package/test/router-1100/router-apply.test.mjs +0 -372
- package/test/router-1100/router-fixb.test.mjs +0 -602
- package/test/router-1100/router-proposals.test.mjs +0 -405
- package/test/router-1100/router-queue-lease-wal.test.mjs +0 -355
- package/test/router-1100/router-security-cr.test.mjs +0 -333
- package/test/router-1100/service-lifecycle.test.mjs +0 -220
- package/test/router-1100/tools-contract.test.mjs +0 -164
- package/test/router-1100/turn-reflect-tick.test.mjs +0 -89
- package/test/router-1100/v6-contract.test.mjs +0 -158
- package/test/router-1100/v6b-contract.test.mjs +0 -293
- package/test/router-1100/v6c-contract.test.mjs +0 -249
- package/test/router-1100/wal-rotation.test.mjs +0 -375
- package/test/router-1100/wp1-config.test.mjs +0 -170
- package/test/router-1100/wp1-default-off.test.mjs +0 -77
- package/test/router-1100/wp1-model-status.test.mjs +0 -90
- package/test/router-1100/wp1-status.test.mjs +0 -220
- package/test/router-1100/wp1-tools.test.mjs +0 -146
- package/test/router-1100/wp5-cli.test.mjs +0 -240
- package/test/router-1100/wp5-model-auto.test.mjs +0 -418
- package/test/router-1100/wp6-merge.test.mjs +0 -386
- package/test/router-1100/wp6-wake.test.mjs +0 -502
- package/test/router-background/audit.test.mjs +0 -37
- package/test/router-background/budget-ledger.test.mjs +0 -330
- package/test/router-background/cross-process.test.mjs +0 -222
- package/test/router-background/fixtures/cross-child.mjs +0 -85
- package/test/router-background/fixtures/real-shapes.mjs +0 -239
- package/test/router-background/router.test.mjs +0 -1044
- package/test/router-background/stream-retry.test.mjs +0 -358
- package/test/router-background/tool-agent.test.mjs +0 -384
- package/test/router-face/bridge.test.mjs +0 -169
- package/test/router-face/face.test.mjs +0 -316
- package/test/router-failclosed.test.mjs +0 -466
- package/test/router-identity.test.mjs +0 -384
- package/test/router-l1-l2.contract.test.mjs +0 -1075
- package/test/router-memory-tools.test.mjs +0 -262
- package/test/router-policy.test.mjs +0 -225
- package/test/router-runtime.test.mjs +0 -438
- package/test/router-shadow.test.mjs +0 -287
- package/test/router.test.mjs +0 -903
- package/test/run-rc.mjs +0 -299
- package/test/run.mjs +0 -2469
- package/test/safety-defaults.test.mjs +0 -179
- package/test/seeded-load.test.mjs +0 -436
- package/test/session-original.test.mjs +0 -338
- package/test/surgery-recovery.test.mjs +0 -936
- package/test/t7_drive.mjs +0 -151
- package/test/tool-output.test.mjs +0 -393
- package/test/tool-schema.test.mjs +0 -177
- package/test/tools_contract.test.mjs +0 -716
- package/test/truncate-visible.test.mjs +0 -1150
- package/test/ui-flags.test.mjs +0 -477
- package/test/v4-hardening.test.mjs +0 -427
package/src/identity/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// 1. agent/pre-step(step === 1)把「开机卡」插到第一条真实 user 消息之后;
|
|
5
5
|
// 2. session/event 的 turn/end 与 session/disposed 把交接卡草稿写进 outbox。
|
|
6
6
|
//
|
|
7
|
-
//
|
|
7
|
+
// 顺序契约(评审第三轮 mustFix #2):本插件用 `{ prepend:true }` 注册,是 Cordis
|
|
8
8
|
// waterfall 的最外层。开机卡必须在 `await next()` **之前** 原地插入
|
|
9
9
|
// `payload.messages`,因为宿主 inner 闭包引用的是同一个 claimed 数组;等 next()
|
|
10
10
|
// 回来再 append 会得到 `[user, runtime, auto, boot]`(auto 在内层看不到 boot)。
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// tooltip(client.js:8373-8383),boot 在首位会让第 1 轮显示【开机卡】。
|
|
13
13
|
// 目标顺序:`[user, boot, runtime, auto]`。
|
|
14
14
|
//
|
|
15
|
-
// source.kind
|
|
15
|
+
// source.kind(评审第四轮 M1):用宿主的隐藏注入 kind
|
|
16
16
|
// `{ kind:'session-reference', form:'recall', version:1, references:[] }`。
|
|
17
17
|
// 宿主把它投影成 context 节点,isVisibleChatNode=false:聊天里不再出现
|
|
18
18
|
// 【开机卡】用户气泡,但 deriveMessages 仍含正文。可见性由潜望镜面板承担
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { collectOpenCard } from './handover.js';
|
|
25
25
|
import { assertInjectionSource, injectionSource, insertionIndex, placeAfterFirstHuman, sameMessageOrder } from './inject-position.js';
|
|
26
26
|
import { writeHandoverFromState } from './writeback.js';
|
|
27
|
+
import { startPrestepPhase } from '../metrics/prestep-latency.mjs';
|
|
27
28
|
|
|
28
29
|
export const name = 'dsh-identity-router';
|
|
29
30
|
|
|
@@ -80,7 +81,7 @@ export function sessionKeyFor(agent) {
|
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
/**
|
|
83
|
-
*
|
|
84
|
+
* 子代理判定:外部记忆插件用 header.origin === 'subagent',这里照抄并兼容 session 参数。
|
|
84
85
|
* @param {any} sessionOrAgent
|
|
85
86
|
* @returns {boolean}
|
|
86
87
|
*/
|
|
@@ -233,11 +234,15 @@ export function apply(ctx, config = {}) {
|
|
|
233
234
|
if (!agent) return next();
|
|
234
235
|
if (settings.skipSubagents && isSubagentSession(agent)) return next();
|
|
235
236
|
|
|
237
|
+
// B3: boot phase = card collection + in-place splice (timing only; no behavior change).
|
|
238
|
+
const bootPhase = startPrestepPhase(sessionKeyFor(agent), payload?.turn, 'boot');
|
|
239
|
+
const endBootPhase = () => { try { bootPhase.end(); } catch { /* timing only */ } };
|
|
240
|
+
|
|
236
241
|
const record = buildCardFor(agent);
|
|
237
|
-
if (record.opened || record.cardText.trim() === '') return next();
|
|
242
|
+
if (record.opened || record.cardText.trim() === '') { endBootPhase(); return next(); }
|
|
238
243
|
|
|
239
244
|
const createUserMessage = await getCreateUserMessage();
|
|
240
|
-
if (!createUserMessage) return next();
|
|
245
|
+
if (!createUserMessage) { endBootPhase(); return next(); }
|
|
241
246
|
|
|
242
247
|
let message;
|
|
243
248
|
try {
|
|
@@ -250,6 +255,7 @@ export function apply(ctx, config = {}) {
|
|
|
250
255
|
warned.add('inject');
|
|
251
256
|
logWarn(`INJECT_FAILED: ${error?.code ?? error?.message ?? error}`);
|
|
252
257
|
}
|
|
258
|
+
endBootPhase();
|
|
253
259
|
return next();
|
|
254
260
|
}
|
|
255
261
|
|
|
@@ -264,6 +270,8 @@ export function apply(ctx, config = {}) {
|
|
|
264
270
|
if (index !== -1) claimed.splice(index, 1);
|
|
265
271
|
};
|
|
266
272
|
|
|
273
|
+
endBootPhase(); // boot own work done; downstream phases are attributed by their own handlers
|
|
274
|
+
|
|
267
275
|
let decision;
|
|
268
276
|
try {
|
|
269
277
|
decision = await next();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* inject-position.js — 开机卡插入位置的**纯函数**(v4)。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* 评审第三轮 mustFix #2:宿主 TurnNavigator 取该轮**第一个**可见 `kind:'user'`
|
|
5
5
|
* 节点做 tooltip 的 prompt(`dsh-client-ui-chat/lib/client.js:8373-8383`),而
|
|
6
6
|
* `source.kind:'user'` 的注入消息正好会变成可见 user 节点(同文件 :9887-9893)。
|
|
7
7
|
* 所以开机卡不能再 unshift 到最前,否则第 1 轮 tooltip 显示【开机卡】而不是用户提问。
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* 也就是:插到**第一条真实 user 消息之后**;auto-recall 仍在内层 append 到末尾,
|
|
14
14
|
* 它照样看得到 boot(路径级去重继续生效)。
|
|
15
15
|
*
|
|
16
|
-
* v4 /
|
|
16
|
+
* v4 / 评审第四轮 M1:source.kind 从 'user' 改为隐藏注入 kind 'session-reference'
|
|
17
17
|
* (形状 `{kind, form:'recall', version:1, references:[]}`)。宿主把它投影成 context
|
|
18
18
|
* 节点,`isVisibleChatNode()` 返回 false——【开机卡】不再变成聊天里的用户气泡;
|
|
19
19
|
* deriveMessages 仍含正文。代价与可见性归属见 README「可见性」。
|
package/src/index.js
CHANGED
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
// work out of the box; an explicitly configured root is never created here.
|
|
45
45
|
|
|
46
46
|
import fs from 'node:fs';
|
|
47
|
+
import os from 'node:os';
|
|
47
48
|
import path from 'node:path';
|
|
48
49
|
import { registerTools, dshHome, TOOLS } from './tools.js';
|
|
49
50
|
import { resolveCurrentSessionId } from './identity.js';
|
|
@@ -58,13 +59,17 @@ import { recordEngineStatus } from './engine-status.js';
|
|
|
58
59
|
import { configurePolicyProfile } from './policy-profile.js';
|
|
59
60
|
import { createRouterRuntime, createRouterService, createRouterTools, normalizeRouterConfig } from './router/runtime.js';
|
|
60
61
|
import { createRouterFace, normalizeFaceConfig } from './router/face/index.mjs';
|
|
61
|
-
import { registerFeatures } from './features.js';
|
|
62
|
+
import { registerFeatures, resolveActivationSwitches } from './features.js';
|
|
63
|
+
import { registerAnswerGuard } from './answer/index.js';
|
|
64
|
+
import { createAnswerTools } from './answer/query.js';
|
|
62
65
|
import { createConclusionBridge } from './conclusion.js';
|
|
63
66
|
import { checkOptionalDeps, formatMissingDepsWarning } from './deps.js';
|
|
64
67
|
import { scheduleFilterRun } from './memory/filter-runner.js';
|
|
65
68
|
import { registerMetrics } from './metrics/index.mjs';
|
|
66
69
|
import { registerToolOutputTrim } from './tool-output.js';
|
|
67
70
|
import { registerD6 } from './d6/runtime.js';
|
|
71
|
+
import { registerHostLifecycle } from './lifecycle/index.js';
|
|
72
|
+
import { registerPanelApiRoutes } from './ui/panel-api.js';
|
|
68
73
|
import { registerUiRoutes } from './ui/routes.js';
|
|
69
74
|
|
|
70
75
|
export const name = '@jjh32456/dsh-longlive';
|
|
@@ -114,11 +119,18 @@ function startupMemoryRoot(ctx, config, env = process.env) {
|
|
|
114
119
|
return root;
|
|
115
120
|
}
|
|
116
121
|
|
|
122
|
+
let lastFeatureActivation = [];
|
|
123
|
+
|
|
124
|
+
/** 任务 activation evidence: last registerFeatures enabled list (host/probe/read-only). */
|
|
125
|
+
export function featureActivation() {
|
|
126
|
+
return [...lastFeatureActivation];
|
|
127
|
+
}
|
|
128
|
+
|
|
117
129
|
export function apply(ctx, config = {}) {
|
|
118
130
|
const home = config.home ?? dshHome();
|
|
119
131
|
|
|
120
|
-
// 0.8.0-rc.10: optional peers are advisory. A profile without
|
|
121
|
-
// only
|
|
132
|
+
// 0.8.0-rc.10: optional peers are advisory. A profile without an external
|
|
133
|
+
// memory plugin loses only its optional tools; the twenty-two longlive tools and truncation
|
|
122
134
|
// must keep working. This check is read-only, never throws, and never blocks
|
|
123
135
|
// activation. The one line points at the --setup wizard and says it asks
|
|
124
136
|
// first; startup itself never installs anything.
|
|
@@ -204,7 +216,7 @@ export function apply(ctx, config = {}) {
|
|
|
204
216
|
maxRecords: config.routerRuntimeMaxRecords,
|
|
205
217
|
// 1.2.0 影子页签要看全部工具调用,而不只是 memory_* 四个。
|
|
206
218
|
recordAllTools: true,
|
|
207
|
-
// Guard only takes the names this plugin does not register (
|
|
219
|
+
// Guard only takes the names this plugin does not register (external memory tools).
|
|
208
220
|
ownTools: TOOLS.map((tool) => tool.name),
|
|
209
221
|
onError: () => warnOnce(ctx, `R1 记录写入失败(${config.routerRuntimeStatePath ?? '.longlive/router-runtime.jsonl'})— ` +
|
|
210
222
|
'判定照常,工具行为不受影响。'),
|
|
@@ -307,6 +319,13 @@ export function apply(ctx, config = {}) {
|
|
|
307
319
|
}
|
|
308
320
|
}
|
|
309
321
|
|
|
322
|
+
// 1.8.0 activation switches (任务 A1): default ON via Config schema; explicit
|
|
323
|
+
// false or DSH_LONGLIVE_DISABLE_* are independent kill switches.
|
|
324
|
+
const activationSwitches = resolveActivationSwitches(config, {}, process.env);
|
|
325
|
+
const answerTools = activationSwitches.answerGuardEnabled && memoryRoot
|
|
326
|
+
? createAnswerTools(memoryRoot)
|
|
327
|
+
: [];
|
|
328
|
+
|
|
310
329
|
const registered = registerTools(ctx, {
|
|
311
330
|
keepEvents: config.keepEvents ?? 20000,
|
|
312
331
|
recentWriteWindowMs: config.recentWriteWindowMs ?? 60000,
|
|
@@ -339,6 +358,8 @@ export function apply(ctx, config = {}) {
|
|
|
339
358
|
// Only present when config.d6.enabled=true; appended after the normal set
|
|
340
359
|
// so the frozen allowlist can name it without changing the baseline order.
|
|
341
360
|
d6Tools: d6.tools,
|
|
361
|
+
// 1.8.0 answer-guard: read-only answer_audit tool (appended after the baseline 22).
|
|
362
|
+
answerTools,
|
|
342
363
|
// 1.6.0 safety default: worker_spawn/worker_status/worker_kill are visible
|
|
343
364
|
// in the baseline tool table but return "unavailable" until this is true.
|
|
344
365
|
workers: config.workers,
|
|
@@ -418,7 +439,7 @@ export function apply(ctx, config = {}) {
|
|
|
418
439
|
// .longlive/bridge/ (also the source A2's board reads). Failures are silent
|
|
419
440
|
// by construction and never block the turn.
|
|
420
441
|
const conclusionBridge = createConclusionBridge({
|
|
421
|
-
enabled: config.autoConclusion
|
|
442
|
+
enabled: config.autoConclusion !== false,
|
|
422
443
|
memoryRoot,
|
|
423
444
|
now: config.now,
|
|
424
445
|
});
|
|
@@ -441,7 +462,35 @@ export function apply(ctx, config = {}) {
|
|
|
441
462
|
}
|
|
442
463
|
// 0.9.0-rc.1 opt-in features. Every switch defaults to off, in which case
|
|
443
464
|
// this call registers nothing, reads nothing and logs nothing.
|
|
444
|
-
registerFeatures(ctx, config, { memoryRoot, surgeryReceipt });
|
|
465
|
+
const featuresResult = registerFeatures(ctx, config, { memoryRoot, surgeryReceipt });
|
|
466
|
+
|
|
467
|
+
// 1.8.0 answer guard (ON by default): registers the outermost pre-step evidence
|
|
468
|
+
// injection, the system-prompt rule section, the session/event audit listener and
|
|
469
|
+
// the read-only panel route. Disabled by config.answerGuard.enabled === false or
|
|
470
|
+
// DSH_LONGLIVE_DISABLE_ANSWER_GUARD=1 — then zero hooks and zero IO.
|
|
471
|
+
try {
|
|
472
|
+
if (activationSwitches.answerGuardEnabled) {
|
|
473
|
+
const answerGuard = registerAnswerGuard(ctx, {
|
|
474
|
+
...config,
|
|
475
|
+
answerGuard: { ...(config?.answerGuard ?? {}), enabled: true },
|
|
476
|
+
}, {
|
|
477
|
+
memoryRoot,
|
|
478
|
+
home,
|
|
479
|
+
now: config.now,
|
|
480
|
+
knownTools: TOOLS.map((tool) => tool.name),
|
|
481
|
+
});
|
|
482
|
+
featuresResult.answerGuard = {
|
|
483
|
+
enabled: answerGuard.enabled,
|
|
484
|
+
systemPrompt: answerGuard.systemPrompt,
|
|
485
|
+
auditPath: answerGuard.auditPath,
|
|
486
|
+
hooks: answerGuard.hooks,
|
|
487
|
+
};
|
|
488
|
+
} else {
|
|
489
|
+
featuresResult.answerGuard = { enabled: false, reason: 'kill-switch' };
|
|
490
|
+
}
|
|
491
|
+
} catch (error) {
|
|
492
|
+
warnOnce(ctx, '[answer-guard] 注册失败(' + (error?.message ?? error) + ')— 已跳过,插件继续加载。');
|
|
493
|
+
}
|
|
445
494
|
|
|
446
495
|
// D1/D4: measure-only working-set ledger. Never trims, never throws; a
|
|
447
496
|
// missing memoryRoot simply disables the recorder.
|
|
@@ -465,10 +514,51 @@ export function apply(ctx, config = {}) {
|
|
|
465
514
|
// Loader reconciliation reloads this entry with the new value. Profiles
|
|
466
515
|
// without a Web server simply get no route; activation never depends on it.
|
|
467
516
|
try {
|
|
468
|
-
registerUiRoutes(ctx, {
|
|
517
|
+
registerUiRoutes(ctx, {
|
|
518
|
+
config,
|
|
519
|
+
memoryRoot,
|
|
520
|
+
rowId: 'session-longlive',
|
|
521
|
+
// EA-08: memoryV2 registers cap-gated #1 on the same path; never double-register.
|
|
522
|
+
skipFlags: config?.memoryV2?.enabled === true,
|
|
523
|
+
});
|
|
469
524
|
} catch (error) {
|
|
470
525
|
warnOnce(ctx, `设置面板接口注册失败(${error?.message ?? error})— 面板将显示不可用,插件继续加载。`);
|
|
471
526
|
}
|
|
472
527
|
|
|
528
|
+
// 1.6.9 memory/recall/forget v2 panel API (M2-F 16 read-only routes + M2-G
|
|
529
|
+
// 16 actions). Default off: when `memoryV2.enabled` is false nothing is
|
|
530
|
+
// registered and no v2 file is created, so the plugin behaves like 1.6.8.
|
|
531
|
+
if (config?.memoryV2?.enabled === true) {
|
|
532
|
+
try {
|
|
533
|
+
registerPanelApiRoutes(ctx, {
|
|
534
|
+
config,
|
|
535
|
+
memoryRoot,
|
|
536
|
+
rowId: 'session-longlive',
|
|
537
|
+
// P0-1: host-injected capability secret (profile-private env); absent => panel API fails closed.
|
|
538
|
+
hostSecret: process.env.DSH_LONGLIVE_PANEL_SECRET,
|
|
539
|
+
});
|
|
540
|
+
} catch (error) {
|
|
541
|
+
warnOnce(ctx, `面板 v2 API 注册失败(${error?.message ?? error})— 插件继续加载。`);
|
|
542
|
+
}
|
|
543
|
+
// M2-B: host lifecycle + dual gap clocks (host-gap / memory-gap recorded as
|
|
544
|
+
// two separate first-class memories; journal backfill is idempotent).
|
|
545
|
+
try {
|
|
546
|
+
const dshHome = typeof process.env.DSH_HOME === 'string' && process.env.DSH_HOME.trim() !== ''
|
|
547
|
+
? path.resolve(process.env.DSH_HOME.trim())
|
|
548
|
+
: path.join(os.homedir(), '.dsh');
|
|
549
|
+
const lifecycle = registerHostLifecycle(ctx, {
|
|
550
|
+
memoryRoot,
|
|
551
|
+
sessionRoots: [path.join(dshHome, 'sessions')],
|
|
552
|
+
registerExitHook: true,
|
|
553
|
+
hostVersion: ctx?.hostVersion,
|
|
554
|
+
pluginVersion: VERSION,
|
|
555
|
+
});
|
|
556
|
+
if (!lifecycle.ok) warnOnce(ctx, `宿主生命周期入账失败(${lifecycle.code})— 插件继续加载。`);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
warnOnce(ctx, `宿主生命周期入账失败(${error?.message ?? error})— 插件继续加载。`);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
lastFeatureActivation = featuresResult && Array.isArray(featuresResult.enabled) ? featuresResult.enabled : [];
|
|
473
563
|
return registered;
|
|
474
564
|
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// src/lifecycle/gap-clocks.js — M2-B / PLAN §2.7(Lead 2026-11-07 修订版,双时钟)
|
|
2
|
+
//
|
|
3
|
+
// 两个**独立**的一等记忆,不许合并:
|
|
4
|
+
// memory-gap(记忆写入空窗)= bootAt − 记忆内容文件最后写入时间
|
|
5
|
+
// 白名单:topics/*.md、journal/*.md、handover/*.md、flow/**/*.md、STATE.md、
|
|
6
|
+
// .longlive/records.jsonl、.longlive/distill/*.jsonl
|
|
7
|
+
// 排除:.longlive/{hot-truncate,router,filter,live,board,bridge,tool-output}、
|
|
8
|
+
// sessions/、外部记忆库、logs/、backups/、index/(逐文件 stat,绝不用目录 mtime)
|
|
9
|
+
// 整秒口径:floorSec(bootAt) − floorSec(lastContentWriteAt)
|
|
10
|
+
// host-gap(宿主中断)= bootAt − 上次生命周期记录(缺失时退化为上一会话最后**事件**)
|
|
11
|
+
// 端点写死:floorSec(bootAt) − floorSec(lastSessionEventAt),标 uncertain:true
|
|
12
|
+
//
|
|
13
|
+
// 11-06 用例 #1(fixture 写死,单测断言):
|
|
14
|
+
// bootAt = 2026-11-06T20:36:24.799+08:00(c045 createdAt)
|
|
15
|
+
// lastWrite = 2026-11-06T14:10:33.351+08:00(handover/2026-11-06-发布计划.md)
|
|
16
|
+
// memoryGap = 23,151,448 ms(精确)/ 23,151 s(floor)= 6 小时 25 分 51 秒
|
|
17
|
+
// lastSessionEventAt = 2026-11-06T20:28:59.113+08:00(e219 job/registered seq=13051)
|
|
18
|
+
// hostGap = 445,000 ms(floor 口径)= 7 分 25 秒
|
|
19
|
+
//
|
|
20
|
+
// 本模块是纯函数 + 只读扫描,不写任何文件(写 journal/事件由 lifecycle/index.js 负责)。
|
|
21
|
+
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
|
|
25
|
+
import { parseSession } from '../codec.js';
|
|
26
|
+
|
|
27
|
+
export const MS_MINUTE = 60_000;
|
|
28
|
+
export const HOST_GAP_WARN_MS = 3 * MS_MINUTE;
|
|
29
|
+
export const MEMORY_GAP_WARN_MS = 30 * MS_MINUTE;
|
|
30
|
+
|
|
31
|
+
/** memory-gap 内容白名单(rel 前缀,统一 `/` 分隔,相对 memoryRoot)。 */
|
|
32
|
+
export const MEMORY_CONTENT_PATTERNS = Object.freeze([
|
|
33
|
+
/^topics\/[^/]+\.md$/,
|
|
34
|
+
/^journal\/[^/]+\.md$/,
|
|
35
|
+
/^handover\/[^/]+\.md$/,
|
|
36
|
+
/^flow\/.*\.md$/,
|
|
37
|
+
/^STATE\.md$/,
|
|
38
|
+
/^\.longlive\/records\.jsonl$/,
|
|
39
|
+
/^\.longlive\/distill\/[^/]+\.jsonl$/,
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
export function isMemoryContentRel(rel) {
|
|
43
|
+
const normalized = String(rel ?? '').replace(/\\/g, '/').replace(/^\.\//, '');
|
|
44
|
+
if (normalized === '' || normalized.split('/').includes('..')) return false;
|
|
45
|
+
return MEMORY_CONTENT_PATTERNS.some((pattern) => pattern.test(normalized));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function floorSec(ms) {
|
|
49
|
+
return Math.floor(Number(ms) / 1000);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 中文时长文本(验收用固定格式:X 小时 Y 分 Z 秒 / Y 分 Z 秒 / Z 秒)。 */
|
|
53
|
+
export function formatGapText(ms) {
|
|
54
|
+
const total = Math.max(0, Math.round(Number(ms) / 1000));
|
|
55
|
+
const hours = Math.floor(total / 3600);
|
|
56
|
+
const minutes = Math.floor((total % 3600) / 60);
|
|
57
|
+
const seconds = total % 60;
|
|
58
|
+
if (hours > 0) return `${hours} 小时 ${minutes} 分 ${seconds} 秒`;
|
|
59
|
+
if (minutes > 0) return `${minutes} 分 ${seconds} 秒`;
|
|
60
|
+
return `${seconds} 秒`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function walkFiles(root, relBase = '', out = []) {
|
|
64
|
+
let entries = [];
|
|
65
|
+
try {
|
|
66
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
67
|
+
} catch {
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
for (const entry of entries) {
|
|
71
|
+
const full = path.join(root, entry.name);
|
|
72
|
+
const rel = relBase === '' ? entry.name : `${relBase}/${entry.name}`;
|
|
73
|
+
if (entry.isDirectory()) walkFiles(full, rel, out);
|
|
74
|
+
else if (entry.isFile()) out.push(rel);
|
|
75
|
+
}
|
|
76
|
+
return out;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Last memory-content write strictly before bootAt (multi-file whitelist, per-file stat).
|
|
81
|
+
* @returns {{atMs:number, rel:string, sources:Array<{rel:string, mtimeMs:number}>}|null}
|
|
82
|
+
*/
|
|
83
|
+
export function resolveMemoryWriteAt({ memoryRoot, bootAtMs }) {
|
|
84
|
+
if (typeof memoryRoot !== 'string' || memoryRoot === '') return null;
|
|
85
|
+
const boot = Number(bootAtMs);
|
|
86
|
+
if (!Number.isFinite(boot)) return null;
|
|
87
|
+
let candidates = [];
|
|
88
|
+
try {
|
|
89
|
+
candidates = walkFiles(path.resolve(memoryRoot));
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const sources = [];
|
|
94
|
+
for (const rel of candidates) {
|
|
95
|
+
if (!isMemoryContentRel(rel)) continue;
|
|
96
|
+
let stat = null;
|
|
97
|
+
try {
|
|
98
|
+
stat = fs.statSync(path.join(memoryRoot, rel));
|
|
99
|
+
} catch {
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (!stat.isFile() || stat.mtimeMs >= boot) continue;
|
|
103
|
+
sources.push({ rel, mtimeMs: stat.mtimeMs });
|
|
104
|
+
}
|
|
105
|
+
if (sources.length === 0) return null;
|
|
106
|
+
sources.sort((a, b) => b.mtimeMs - a.mtimeMs || a.rel.localeCompare(b.rel));
|
|
107
|
+
return { atMs: sources[0].mtimeMs, rel: sources[0].rel, sources: sources.slice(0, 8) };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Last session event strictly before bootAt, across the newest N session logs.
|
|
112
|
+
* Used as the honest fallback when no lifecycle record exists (uncertain:true).
|
|
113
|
+
*/
|
|
114
|
+
export function resolveLastSessionEventAt({ sessionRoots, bootAtMs, limitSessions = 8 }) {
|
|
115
|
+
const boot = Number(bootAtMs);
|
|
116
|
+
const roots = (Array.isArray(sessionRoots) ? sessionRoots : [sessionRoots]).filter(Boolean);
|
|
117
|
+
const logs = [];
|
|
118
|
+
const stack = roots.map((root) => path.resolve(String(root)));
|
|
119
|
+
const seen = new Set();
|
|
120
|
+
while (stack.length > 0) {
|
|
121
|
+
const dir = stack.pop();
|
|
122
|
+
if (seen.has(dir)) continue;
|
|
123
|
+
seen.add(dir);
|
|
124
|
+
let entries = [];
|
|
125
|
+
try {
|
|
126
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
127
|
+
} catch {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
for (const entry of entries) {
|
|
131
|
+
const full = path.join(dir, entry.name);
|
|
132
|
+
if (entry.isDirectory()) stack.push(full);
|
|
133
|
+
else if (entry.isFile() && entry.name === 'session.v4.jsonl.zstd') {
|
|
134
|
+
try {
|
|
135
|
+
logs.push({ file: full, mtimeMs: fs.statSync(full).mtimeMs });
|
|
136
|
+
} catch {
|
|
137
|
+
/* skip unreadable log */
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (logs.length === 0) return null;
|
|
143
|
+
logs.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
144
|
+
let best = null;
|
|
145
|
+
for (const log of logs.slice(0, limitSessions)) {
|
|
146
|
+
let parsed = null;
|
|
147
|
+
try {
|
|
148
|
+
parsed = parseSession(fs.readFileSync(log.file));
|
|
149
|
+
} catch {
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
for (const event of parsed.events) {
|
|
153
|
+
const at = Number(event?.time);
|
|
154
|
+
if (!Number.isFinite(at) || at >= boot) continue;
|
|
155
|
+
if (best === null || at > best.atMs) best = { atMs: at, rel: log.file };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return best;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function countActivityEvidence({ memoryRoot, fromMs, toMs }) {
|
|
162
|
+
const out = { toolOutputs: 0, boardRows: 0, sessionEvents: 0 };
|
|
163
|
+
// tool-output:窗口内落盘的 tool-output 归档(实测 3 个为用例 #1 的升级证据)
|
|
164
|
+
const toolDir = path.join(memoryRoot, '.longlive', 'tool-output');
|
|
165
|
+
for (const rel of walkFiles(toolDir)) {
|
|
166
|
+
try {
|
|
167
|
+
const stat = fs.statSync(path.join(toolDir, rel));
|
|
168
|
+
if (stat.isFile() && stat.mtimeMs > fromMs && stat.mtimeMs <= toMs) out.toolOutputs += 1;
|
|
169
|
+
} catch {
|
|
170
|
+
/* skip */
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// board 行:窗口内的活动流行(按行内 at/ts 时间戳,缺时间戳的行不计)
|
|
174
|
+
const boardFile = path.join(memoryRoot, '.longlive', 'board', 'activity.jsonl');
|
|
175
|
+
try {
|
|
176
|
+
for (const line of fs.readFileSync(boardFile, 'utf8').split('\n')) {
|
|
177
|
+
if (line.trim() === '') continue;
|
|
178
|
+
let row = null;
|
|
179
|
+
try {
|
|
180
|
+
row = JSON.parse(line);
|
|
181
|
+
} catch {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const at = Number(row?.at ?? row?.ts ?? row?.time);
|
|
185
|
+
if (Number.isFinite(at) && at > fromMs && at <= toMs) out.boardRows += 1;
|
|
186
|
+
}
|
|
187
|
+
} catch {
|
|
188
|
+
/* no board */
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Compute both clocks (separate objects, never merged). */
|
|
194
|
+
export function computeGaps(options = {}) {
|
|
195
|
+
const memoryRoot = typeof options.memoryRoot === 'string' ? path.resolve(options.memoryRoot) : '';
|
|
196
|
+
const bootRaw = typeof options.bootAt === 'string' || typeof options.bootAt === 'number'
|
|
197
|
+
? new Date(options.bootAt).getTime()
|
|
198
|
+
: Number(options.bootAtMs);
|
|
199
|
+
const bootAtMs = Number.isFinite(bootRaw) ? bootRaw : Date.now();
|
|
200
|
+
const sessionRoots = Array.isArray(options.sessionRoots) ? options.sessionRoots : [];
|
|
201
|
+
const lifecycle = options.lifecycle ?? null;
|
|
202
|
+
|
|
203
|
+
// ---- memory-gap ----
|
|
204
|
+
const memoryWrite = options.memoryWriteAt !== undefined
|
|
205
|
+
? (options.memoryWriteAt === null ? null : { atMs: Number(options.memoryWriteAt), rel: 'injected', sources: [] })
|
|
206
|
+
: resolveMemoryWriteAt({ memoryRoot, bootAtMs });
|
|
207
|
+
let memoryGap;
|
|
208
|
+
if (memoryWrite === null || !Number.isFinite(memoryWrite.atMs)) {
|
|
209
|
+
memoryGap = {
|
|
210
|
+
kind: 'unknown',
|
|
211
|
+
gapMs: null,
|
|
212
|
+
gapSec: null,
|
|
213
|
+
gapText: 'unknown',
|
|
214
|
+
from: null,
|
|
215
|
+
to: new Date(bootAtMs).toISOString(),
|
|
216
|
+
sources: [],
|
|
217
|
+
activityEvidence: null,
|
|
218
|
+
uncertain: true,
|
|
219
|
+
};
|
|
220
|
+
} else {
|
|
221
|
+
const gapMsExact = bootAtMs - memoryWrite.atMs;
|
|
222
|
+
const gapSec = floorSec(bootAtMs) - floorSec(memoryWrite.atMs);
|
|
223
|
+
const evidence = options.activityEvidence ?? countActivityEvidence({
|
|
224
|
+
memoryRoot,
|
|
225
|
+
fromMs: memoryWrite.atMs,
|
|
226
|
+
toMs: bootAtMs,
|
|
227
|
+
});
|
|
228
|
+
const activityCount = evidence.toolOutputs + evidence.boardRows + Number(evidence.sessionEvents ?? 0);
|
|
229
|
+
const warn = gapSec * 1000 >= MEMORY_GAP_WARN_MS;
|
|
230
|
+
memoryGap = {
|
|
231
|
+
kind: warn && activityCount > 0 ? 'active-no-memory' : (warn ? 'gap' : 'ok'),
|
|
232
|
+
gapMs: gapMsExact, // exact ms (fixture: 23,151,448)
|
|
233
|
+
gapSec, // floor endpoints (fixture: 23,151)
|
|
234
|
+
gapText: formatGapText(gapSec * 1000),
|
|
235
|
+
from: new Date(memoryWrite.atMs).toISOString(),
|
|
236
|
+
to: new Date(bootAtMs).toISOString(),
|
|
237
|
+
sources: memoryWrite.sources,
|
|
238
|
+
source: memoryWrite.rel,
|
|
239
|
+
activityEvidence: evidence,
|
|
240
|
+
activityCount,
|
|
241
|
+
warn,
|
|
242
|
+
uncertain: false,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// ---- host-gap ----
|
|
247
|
+
let hostFrom = null;
|
|
248
|
+
let hostSource = null;
|
|
249
|
+
let prevExitForm = lifecycle?.prevExitForm ?? 'unknown';
|
|
250
|
+
let uncertain = false;
|
|
251
|
+
if (Number.isFinite(options.lastSessionEventAtMs)) {
|
|
252
|
+
hostFrom = Number(options.lastSessionEventAtMs);
|
|
253
|
+
hostSource = options.lastSessionEventSource ?? 'injected-session-event';
|
|
254
|
+
uncertain = true;
|
|
255
|
+
} else if (lifecycle && Number.isFinite(lifecycle.lastShutdownAtMs)) {
|
|
256
|
+
hostFrom = Number(lifecycle.lastShutdownAtMs);
|
|
257
|
+
hostSource = 'lifecycle:shutdown';
|
|
258
|
+
} else if (lifecycle && Number.isFinite(lifecycle.lastActivityAtMs)) {
|
|
259
|
+
hostFrom = Number(lifecycle.lastActivityAtMs);
|
|
260
|
+
hostSource = 'lifecycle:lastActivity';
|
|
261
|
+
uncertain = true;
|
|
262
|
+
} else if (sessionRoots.length > 0) {
|
|
263
|
+
const lastEvent = resolveLastSessionEventAt({ sessionRoots, bootAtMs });
|
|
264
|
+
if (lastEvent !== null) {
|
|
265
|
+
hostFrom = lastEvent.atMs;
|
|
266
|
+
hostSource = 'session-last-event';
|
|
267
|
+
uncertain = true;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
let hostGap;
|
|
271
|
+
if (hostFrom === null || !Number.isFinite(hostFrom)) {
|
|
272
|
+
hostGap = {
|
|
273
|
+
kind: 'unknown',
|
|
274
|
+
gapMs: null,
|
|
275
|
+
gapSec: null,
|
|
276
|
+
gapText: 'unknown',
|
|
277
|
+
from: null,
|
|
278
|
+
to: new Date(bootAtMs).toISOString(),
|
|
279
|
+
source: 'unknown',
|
|
280
|
+
prevExitForm,
|
|
281
|
+
uncertain: true,
|
|
282
|
+
};
|
|
283
|
+
} else {
|
|
284
|
+
// floor 端点口径(fixture: 445,000ms / 445s)
|
|
285
|
+
const gapSec = Math.max(0, floorSec(bootAtMs) - floorSec(hostFrom));
|
|
286
|
+
hostGap = {
|
|
287
|
+
kind: gapSec * 1000 >= HOST_GAP_WARN_MS ? 'gap' : 'ok',
|
|
288
|
+
gapMs: gapSec * 1000,
|
|
289
|
+
rawGapMs: Math.max(0, bootAtMs - hostFrom),
|
|
290
|
+
gapSec,
|
|
291
|
+
gapText: formatGapText(gapSec * 1000),
|
|
292
|
+
from: new Date(hostFrom).toISOString(),
|
|
293
|
+
to: new Date(bootAtMs).toISOString(),
|
|
294
|
+
source: hostSource,
|
|
295
|
+
prevExitForm,
|
|
296
|
+
warn: gapSec * 1000 >= HOST_GAP_WARN_MS,
|
|
297
|
+
uncertain,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
schema: 'gap-clocks/v1',
|
|
303
|
+
bootAt: new Date(bootAtMs).toISOString(),
|
|
304
|
+
memoryGap,
|
|
305
|
+
hostGap,
|
|
306
|
+
// 双钟分别记录、不合并(用例 #1:23,151,448 ms vs 445,000 ms)
|
|
307
|
+
merged: false,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Render the two boot-card lines (each ≤80 chars; host line is droppable). */
|
|
312
|
+
export function renderGapLines(gaps) {
|
|
313
|
+
const lines = [];
|
|
314
|
+
if (gaps?.memoryGap?.kind !== 'unknown') {
|
|
315
|
+
const time = gaps.memoryGap.from?.slice(11, 19) ?? '?';
|
|
316
|
+
const evidence = gaps.memoryGap.activityEvidence;
|
|
317
|
+
const activityText = evidence && (evidence.toolOutputs + evidence.boardRows) > 0
|
|
318
|
+
? ` · 期间活动证据 ${evidence.toolOutputs + evidence.boardRows} 个`
|
|
319
|
+
: '';
|
|
320
|
+
lines.push(`▍记忆空窗 ${gaps.memoryGap.gapText}(上次记忆写入 ${time}${activityText})`.slice(0, 80));
|
|
321
|
+
}
|
|
322
|
+
if (gaps?.hostGap?.kind !== 'unknown') {
|
|
323
|
+
const from = gaps.hostGap.from?.slice(11, 19) ?? '?';
|
|
324
|
+
const to = gaps.hostGap.to?.slice(11, 19) ?? '?';
|
|
325
|
+
lines.push(`▍宿主中断 ${gaps.hostGap.gapText}(${from} → ${to} · 上次退出:${gaps.hostGap.prevExitForm ?? '未知'})`.slice(0, 80));
|
|
326
|
+
}
|
|
327
|
+
return lines;
|
|
328
|
+
}
|