@jjh32456/dsh-longlive 1.6.8 → 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 +69 -1539
- package/LICENSE +1 -1
- package/QUICKSTART.md +33 -12
- package/README.en.md +17 -7
- package/README.md +29 -10
- package/SPEC.md +5 -5
- package/cordis.patch.yml +17 -12
- package/docs/ANSWER-GUARD.md +92 -0
- package/docs/ARCHITECTURE.md +3 -3
- package/docs/AUTO-RECALL-WIRING.md +1 -1
- package/docs/AUTO-RECALL.md +15 -9
- package/docs/CONFIG.md +143 -37
- package/docs/ERRORS.md +17 -5
- package/docs/FAQ.md +7 -7
- package/docs/FEATURES.md +66 -29
- 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 +3 -2
- package/docs/RESIDENT-WATCHER.md +1 -1
- package/docs/SESSION-WATCH.md +6 -6
- package/docs/TROUBLESHOOTING.md +4 -4
- package/package.json +42 -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 +129 -18
- package/src/activity/model.js +11 -1
- package/src/activity/store.js +22 -3
- 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 +145 -48
- package/src/deps.js +6 -4
- package/src/features.js +158 -8
- 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/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 +1 -1
- 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/config.mjs +5 -5
- package/src/router/service/context.mjs +1 -1
- package/src/router/service/queue.mjs +1 -1
- 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/activity-realtime.test.mjs +0 -959
- package/test/activity-stream.test.mjs +0 -512
- package/test/auto-recall.test.mjs +0 -474
- package/test/auto-truncate.test.mjs +0 -931
- package/test/autobind.test.mjs +0 -399
- 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 -210
- 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 -585
- package/test/filter-shadow.test.mjs +0 -170
- package/test/fixtures/activity-append-child.mjs +0 -29
- 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 -1023
- 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 -184
- 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 -1076
- 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 -315
- package/test/run.mjs +0 -2469
- package/test/safety-defaults.test.mjs +0 -181
- package/test/seeded-load.test.mjs +0 -436
- package/test/session-original.test.mjs +0 -338
- package/test/state-truth.test.mjs +0 -937
- 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/test/version-consistency.test.mjs +0 -286
package/test/metrics.test.mjs
DELETED
|
@@ -1,478 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import assert from 'node:assert/strict';
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import os from 'node:os';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
estimateTokens,
|
|
9
|
-
estimateMessageTokens,
|
|
10
|
-
estimateToolsTokens,
|
|
11
|
-
contentText,
|
|
12
|
-
ESTIMATOR_ID,
|
|
13
|
-
} from '../src/metrics/estimate.mjs';
|
|
14
|
-
import {
|
|
15
|
-
classifyMessage,
|
|
16
|
-
messageOfEvent,
|
|
17
|
-
recallIdsFromText,
|
|
18
|
-
recallIdsFromMessage,
|
|
19
|
-
firstLine,
|
|
20
|
-
} from '../src/metrics/classify.mjs';
|
|
21
|
-
import {
|
|
22
|
-
measureTurn,
|
|
23
|
-
normalizeMetricsConfig,
|
|
24
|
-
scaleToTotal,
|
|
25
|
-
summarizeRows,
|
|
26
|
-
median,
|
|
27
|
-
} from '../src/metrics/measure.mjs';
|
|
28
|
-
import {
|
|
29
|
-
appendTurnRow,
|
|
30
|
-
readTurnLedger,
|
|
31
|
-
readMetricsSummary,
|
|
32
|
-
readCalibration,
|
|
33
|
-
writeCalibration,
|
|
34
|
-
readLastConclusion,
|
|
35
|
-
ledgerPath,
|
|
36
|
-
} from '../src/metrics/store.mjs';
|
|
37
|
-
import { registerMetrics, findUsageEvent, findRequestHeader } from '../src/metrics/index.mjs';
|
|
38
|
-
import { createRouterTools } from '../src/router/service/tools.mjs';
|
|
39
|
-
|
|
40
|
-
function tempRoot() {
|
|
41
|
-
return fs.mkdtempSync(path.join(os.tmpdir(), 'longlive-metrics-'));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
let seqCounter = 0;
|
|
45
|
-
function event(type, data, extra = {}) {
|
|
46
|
-
return { seq: seqCounter++, type, data, surfaceOp: 'append', ...extra };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function resetSeq() {
|
|
50
|
-
seqCounter = 0;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function systemEvent(text, turn = 1) {
|
|
54
|
-
return event('system/message', {
|
|
55
|
-
turn,
|
|
56
|
-
step: 1,
|
|
57
|
-
message: { role: 'system', content: text },
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function userEvent(text, source = null, turn = 0) {
|
|
62
|
-
const message = { role: 'user', content: text, source: source ?? { kind: 'user' } };
|
|
63
|
-
return turn > 0
|
|
64
|
-
? event('user/message', { turn, message })
|
|
65
|
-
: event('user/message', message);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function assistantEvent(text, turn, usage = null) {
|
|
69
|
-
return event('assistant/message', {
|
|
70
|
-
turn,
|
|
71
|
-
step: 1,
|
|
72
|
-
message: { role: 'assistant', content: [{ type: 'text', text }] },
|
|
73
|
-
...(usage ? { usage } : {}),
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function toolEvent(text, turn) {
|
|
78
|
-
return event('tool/result', {
|
|
79
|
-
turn,
|
|
80
|
-
step: 1,
|
|
81
|
-
message: { role: 'tool', content: [{ type: 'text', text }] },
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function turnStart(turn) {
|
|
86
|
-
return event('turn/start', { turn });
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
test('estimate: empty and malformed values are zero', () => {
|
|
90
|
-
assert.equal(estimateTokens(''), 0);
|
|
91
|
-
assert.equal(estimateTokens(null), 0);
|
|
92
|
-
assert.equal(estimateTokens(undefined), 0);
|
|
93
|
-
assert.equal(estimateTokens(42), 0);
|
|
94
|
-
assert.equal(estimateTokens('你好'), 2);
|
|
95
|
-
assert.equal(estimateTokens('abcd'), 2);
|
|
96
|
-
assert.equal(estimateTokens('a'.repeat(350)), 100);
|
|
97
|
-
assert.equal(ESTIMATOR_ID, 'cjk*1+other/3.5');
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('estimate: large strings do not overflow and stay finite', () => {
|
|
101
|
-
const text = '字a'.repeat(1_000_000);
|
|
102
|
-
const tokens = estimateTokens(text);
|
|
103
|
-
assert.ok(Number.isSafeInteger(tokens));
|
|
104
|
-
assert.ok(tokens > 1_000_000);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test('estimate: message text ignores reasoning but counts tool calls', () => {
|
|
108
|
-
const message = {
|
|
109
|
-
role: 'assistant',
|
|
110
|
-
content: [
|
|
111
|
-
{ type: 'reasoning', text: 'x'.repeat(4000) },
|
|
112
|
-
{ type: 'text', text: 'hello' },
|
|
113
|
-
{ type: 'tool-call', name: 'read', arguments: '{"file_path":"a"}' },
|
|
114
|
-
],
|
|
115
|
-
};
|
|
116
|
-
const text = contentText(message.content);
|
|
117
|
-
assert.equal(text.includes('x'.repeat(20)), false);
|
|
118
|
-
assert.equal(text.includes('read'), true);
|
|
119
|
-
assert.equal(estimateMessageTokens({ role: 'assistant', content: [{ type: 'reasoning', text: 'x' }] }), 0);
|
|
120
|
-
assert.ok(estimateMessageTokens(message) > 0);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test('estimate: tools json and empty tools', () => {
|
|
124
|
-
assert.equal(estimateToolsTokens([]), 0);
|
|
125
|
-
assert.equal(estimateToolsTokens(null), 0);
|
|
126
|
-
assert.ok(estimateToolsTokens([{ name: 'read', description: 'abc' }]) > 0);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('classify: system, developer, fixed injection, recall, conclusion, conversation', () => {
|
|
130
|
-
assert.equal(classifyMessage({ role: 'system', content: 'x' }), 'fixed');
|
|
131
|
-
assert.equal(classifyMessage({ role: 'developer', content: 'x' }), 'fixed');
|
|
132
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'agent-instructions' } }), 'fixed');
|
|
133
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'runtime-context', sections: [] } }), 'fixed');
|
|
134
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'session-reference', form: 'recall' } }), 'fixed');
|
|
135
|
-
assert.equal(classifyMessage({ role: 'user', content: '【自动想起】\n- 《A》(a.md · 1)', source: { kind: 'session-reference' } }), 'recall');
|
|
136
|
-
assert.equal(classifyMessage({ role: 'user', content: '【开机卡】x' }), 'conclusion');
|
|
137
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'plugin:dsh-auto-recall' } }), 'recall');
|
|
138
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'plugin:dsh-identity-router' } }), 'conclusion');
|
|
139
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'archive-card' } }), 'conversation');
|
|
140
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x', source: { kind: 'user' } }), 'conversation');
|
|
141
|
-
assert.equal(classifyMessage({ role: 'user', content: 'x' }), 'conversation');
|
|
142
|
-
assert.equal(classifyMessage(null), 'unknown');
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
test('classify: messageOfEvent handles wrapper and bare user message', () => {
|
|
146
|
-
const bare = { role: 'user', content: 'hi' };
|
|
147
|
-
assert.equal(messageOfEvent({ type: 'user/message', data: bare }), bare);
|
|
148
|
-
const wrapped = { role: 'tool', content: 'out' };
|
|
149
|
-
assert.equal(messageOfEvent({ type: 'tool/result', data: { message: wrapped } }), wrapped);
|
|
150
|
-
assert.equal(messageOfEvent({ data: null }), null);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test('recall ids: parse rendered lines, dedupe, tolerate missing fields', () => {
|
|
154
|
-
const text = [
|
|
155
|
-
'【自动想起】线索:',
|
|
156
|
-
'- 《工具链》(topics/toolchain.md · 相关度 1.72)',
|
|
157
|
-
'- 《工具链》(topics/toolchain.md · 相关度 1.10)',
|
|
158
|
-
'- 《无路径》',
|
|
159
|
-
'',
|
|
160
|
-
].join('\n');
|
|
161
|
-
const ids = recallIdsFromText(text);
|
|
162
|
-
assert.equal(ids.length, 1);
|
|
163
|
-
assert.equal(ids[0].path, 'topics/toolchain.md');
|
|
164
|
-
assert.equal(firstLine(text), '【自动想起】线索:');
|
|
165
|
-
assert.deepEqual(recallIdsFromText(''), []);
|
|
166
|
-
assert.deepEqual(recallIdsFromText(null), []);
|
|
167
|
-
assert.deepEqual(recallIdsFromMessage({ source: { references: [{ path: 'a.md', title: 'A' }, { path: 'a.md' }] } }), [{ title: 'A', path: 'a.md' }]);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
test('config: defaults, disable switch, proportional layer targets', () => {
|
|
171
|
-
const defaults = normalizeMetricsConfig({});
|
|
172
|
-
assert.equal(defaults.enabled, true);
|
|
173
|
-
assert.equal(defaults.targetTokens, 40000);
|
|
174
|
-
assert.deepEqual(defaults.layerTargets, { fixed: 25000, recent: 12000, recall: 2000, conclusion: 1000 });
|
|
175
|
-
assert.equal(normalizeMetricsConfig({ metrics: false }).enabled, false);
|
|
176
|
-
assert.equal(normalizeMetricsConfig({ metrics: { enabled: false } }).enabled, false);
|
|
177
|
-
const half = normalizeMetricsConfig({ targetTokens: 20000 });
|
|
178
|
-
assert.equal(half.layerTargets.fixed, 12500);
|
|
179
|
-
assert.equal(half.layerTargets.recent, 6000);
|
|
180
|
-
assert.equal(half.layerTargets.recall, 1000);
|
|
181
|
-
assert.equal(half.layerTargets.conclusion, 500);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test('scaleToTotal: exact sum, empty and zero are safe', () => {
|
|
185
|
-
const scaled = scaleToTotal({ fixed: 10, recent: 5, recall: 1, conclusion: 1, carried: 3 }, 100);
|
|
186
|
-
const sum = Object.values(scaled).reduce((a, b) => a + b, 0);
|
|
187
|
-
assert.equal(sum, 100);
|
|
188
|
-
assert.deepEqual(scaleToTotal({}, 100), {});
|
|
189
|
-
assert.deepEqual(scaleToTotal({ a: 0, b: 0 }, 100), { a: 0, b: 0 });
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('measureTurn: empty input produces a zero row, never throws', () => {
|
|
193
|
-
const row = measureTurn({ turn: 0, sessionId: 's-empty' });
|
|
194
|
-
assert.equal(row.v, 1);
|
|
195
|
-
assert.equal(row.sessionId, 's-empty');
|
|
196
|
-
assert.deepEqual(row.layers, { fixed: 0, recent: 0, recall: 0, conclusion: 0 });
|
|
197
|
-
assert.equal(row.total, 0);
|
|
198
|
-
assert.equal(row.carried, 0);
|
|
199
|
-
assert.equal(row.reconciliation.available, false);
|
|
200
|
-
assert.equal(row.budget.over, false);
|
|
201
|
-
assert.equal(row.budget.applied.length, 0);
|
|
202
|
-
assert.equal(row.source, 'live');
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test('measureTurn: four layers, carry, budget and usage reconciliation', () => {
|
|
206
|
-
resetSeq();
|
|
207
|
-
const events = [
|
|
208
|
-
turnStart(1),
|
|
209
|
-
systemEvent('S'.repeat(350), 1),
|
|
210
|
-
userEvent('A'.repeat(350), { kind: 'agent-instructions' }, 1),
|
|
211
|
-
userEvent('real one', { kind: 'user' }, 1),
|
|
212
|
-
assistantEvent('answer one', 1),
|
|
213
|
-
toolEvent('tool output one', 1),
|
|
214
|
-
event('turn/end', { turn: 1 }),
|
|
215
|
-
turnStart(2),
|
|
216
|
-
userEvent('【自动想起】\n- 《A》(a.md · 1.0)\n- 《B》(b.md · 0.9)', { kind: 'session-reference', form: 'recall' }),
|
|
217
|
-
userEvent('【开机卡】boot conclusion', { kind: 'session-reference' }),
|
|
218
|
-
userEvent('real two', { kind: 'user' }, 2),
|
|
219
|
-
assistantEvent('answer two', 2, {
|
|
220
|
-
inputTokens: 1000,
|
|
221
|
-
outputTokens: 50,
|
|
222
|
-
cacheReadTokens: 2000,
|
|
223
|
-
cacheWriteTokens: 0,
|
|
224
|
-
totalTokens: 3050,
|
|
225
|
-
}),
|
|
226
|
-
];
|
|
227
|
-
const usage = events[events.length - 1].data.usage;
|
|
228
|
-
const row = measureTurn({
|
|
229
|
-
events,
|
|
230
|
-
turn: 2,
|
|
231
|
-
sessionId: 's-layers',
|
|
232
|
-
requestHeader: { tools: [{ name: 'read', description: 'read a file' }] },
|
|
233
|
-
actualUsage: usage,
|
|
234
|
-
config: { keepTurns: 12, targetTokens: 100, layerTargets: { fixed: 10, recent: 10, recall: 10, conclusion: 10 } },
|
|
235
|
-
factor: 1,
|
|
236
|
-
calibrationSamples: 3,
|
|
237
|
-
now: '2026-11-05T00:00:00.000Z',
|
|
238
|
-
});
|
|
239
|
-
assert.ok(row.layers.fixed > 0);
|
|
240
|
-
assert.ok(row.layers.recent > 0);
|
|
241
|
-
assert.ok(row.layers.recall > 0);
|
|
242
|
-
assert.ok(row.layers.conclusion > 0);
|
|
243
|
-
assert.equal(row.total, row.layers.fixed + row.layers.recent + row.layers.recall + row.layers.conclusion + row.carried);
|
|
244
|
-
assert.equal(row.recallCount, 2);
|
|
245
|
-
assert.deepEqual(row.recallIds.sort(), ['a.md', 'b.md']);
|
|
246
|
-
assert.equal(row.actual.requestInputTokens, 3000);
|
|
247
|
-
assert.equal(row.reconciliation.available, true);
|
|
248
|
-
assert.equal(row.reconciliation.delta, row.total - 3000);
|
|
249
|
-
assert.equal(row.budget.over, true);
|
|
250
|
-
assert.equal(row.budget.triggered, true);
|
|
251
|
-
assert.equal(row.budget.applied.length, 0);
|
|
252
|
-
assert.equal(row.budget.note.includes('measure-only'), true);
|
|
253
|
-
assert.equal(row.tokenSource, 'estimate-calibrated');
|
|
254
|
-
assert.equal(row.at, '2026-11-05T00:00:00.000Z');
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
test('measureTurn: older conversation becomes carried', () => {
|
|
258
|
-
resetSeq();
|
|
259
|
-
const events = [
|
|
260
|
-
turnStart(1),
|
|
261
|
-
userEvent('old real', { kind: 'user' }, 1),
|
|
262
|
-
assistantEvent('old answer', 1),
|
|
263
|
-
turnStart(10),
|
|
264
|
-
userEvent('new real', { kind: 'user' }, 10),
|
|
265
|
-
assistantEvent('new answer', 10),
|
|
266
|
-
];
|
|
267
|
-
const row = measureTurn({ events, turn: 10, sessionId: 's', config: { keepTurns: 2 } });
|
|
268
|
-
assert.ok(row.carried > 0);
|
|
269
|
-
assert.equal(row.components.recentTurns, 1);
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
test('measureTurn: missing content, header and usage are safe', () => {
|
|
273
|
-
resetSeq();
|
|
274
|
-
const events = [
|
|
275
|
-
turnStart(1),
|
|
276
|
-
event('user/message', { role: 'user', content: undefined }),
|
|
277
|
-
event('assistant/message', { turn: 1, step: 1, message: { role: 'assistant' } }),
|
|
278
|
-
];
|
|
279
|
-
const row = measureTurn({ events, turn: 1, sessionId: 's' });
|
|
280
|
-
assert.equal(row.actual, null);
|
|
281
|
-
assert.equal(row.reconciliation.available, false);
|
|
282
|
-
assert.equal(row.components.tools, 0);
|
|
283
|
-
assert.equal(row.total, 0);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
test('measureTurn: very large budgets and token sums stay finite', () => {
|
|
287
|
-
resetSeq();
|
|
288
|
-
const events = [
|
|
289
|
-
turnStart(1),
|
|
290
|
-
systemEvent('x'.repeat(10000), 1),
|
|
291
|
-
userEvent('y'.repeat(10000), { kind: 'user' }, 1),
|
|
292
|
-
assistantEvent('z'.repeat(10000), 1, {
|
|
293
|
-
inputTokens: 1_000_000_000,
|
|
294
|
-
outputTokens: 1_000_000,
|
|
295
|
-
cacheReadTokens: 2_000_000_000,
|
|
296
|
-
cacheWriteTokens: 1_000_000_000,
|
|
297
|
-
totalTokens: 4_001_000_000,
|
|
298
|
-
}),
|
|
299
|
-
];
|
|
300
|
-
const row = measureTurn({
|
|
301
|
-
events,
|
|
302
|
-
turn: 1,
|
|
303
|
-
sessionId: 's-big',
|
|
304
|
-
actualUsage: events[events.length - 1].data.usage,
|
|
305
|
-
config: { targetTokens: 1_000_000_000, keepTurns: 12 },
|
|
306
|
-
factor: 2,
|
|
307
|
-
});
|
|
308
|
-
assert.ok(Number.isSafeInteger(row.total));
|
|
309
|
-
assert.equal(row.layers.fixed + row.layers.recent + row.layers.recall + row.layers.conclusion + row.carried, row.total);
|
|
310
|
-
assert.equal(row.actual.requestInputTokens, 4_000_000_000);
|
|
311
|
-
assert.equal(row.budget.over, false);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
test('summarizeRows / median: empty and large values', () => {
|
|
315
|
-
assert.equal(median([]), 0);
|
|
316
|
-
assert.equal(median([3, 1, 2]), 2);
|
|
317
|
-
assert.equal(median([1, 2, 3, 4]), 2.5);
|
|
318
|
-
const empty = summarizeRows([]);
|
|
319
|
-
assert.equal(empty.turns, 0);
|
|
320
|
-
assert.equal(empty.medianTotal, 0);
|
|
321
|
-
const summary = summarizeRows([
|
|
322
|
-
{ at: '2026-01-01T00:00:00Z', sessionId: 'a', total: 1_000_000_000, layers: { fixed: 1 }, cacheHitRate: 0.5, budget: { triggered: false }, reconciliation: { actualInput: 900_000_000, deltaPct: 10, within5pct: false } },
|
|
323
|
-
{ at: '2026-01-02T00:00:00Z', sessionId: 'a', total: 2_000_000_000, layers: { fixed: 2 }, cacheHitRate: 0.7, budget: { triggered: true }, reconciliation: { actualInput: 2_000_000_000, deltaPct: 0, within5pct: true } },
|
|
324
|
-
]);
|
|
325
|
-
assert.equal(summary.turns, 2);
|
|
326
|
-
assert.equal(summary.medianTotal, 1_500_000_000);
|
|
327
|
-
assert.equal(summary.overBudgetTurns, 1);
|
|
328
|
-
assert.equal(summary.within5Turns, 1);
|
|
329
|
-
assert.equal(summary.medianAbsErrorPct, 5);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
test('store: append, read, skip corrupt, filter and limit', () => {
|
|
333
|
-
const root = tempRoot();
|
|
334
|
-
assert.equal(appendTurnRow({ at: '2026-01-01T00:00:00Z', sessionId: 'a', turn: 1, total: 10 }, { memoryRoot: root }).ok, true);
|
|
335
|
-
assert.equal(appendTurnRow({ at: '2026-01-02T00:00:00Z', sessionId: 'b', turn: 2, total: 20 }, { memoryRoot: root }).ok, true);
|
|
336
|
-
fs.appendFileSync(ledgerPath(root), '{not json}\n\n', 'utf8');
|
|
337
|
-
assert.equal(appendTurnRow({ at: '2026-01-03T00:00:00Z', sessionId: 'a', turn: 3, total: 30 }, { memoryRoot: root }).ok, true);
|
|
338
|
-
|
|
339
|
-
const all = readTurnLedger({ memoryRoot: root });
|
|
340
|
-
assert.equal(all.ok, true);
|
|
341
|
-
assert.equal(all.total, 3);
|
|
342
|
-
assert.equal(all.skipped, 1);
|
|
343
|
-
assert.equal(all.rows.length, 3);
|
|
344
|
-
|
|
345
|
-
const filtered = readTurnLedger({ memoryRoot: root, sessionId: 'a', limit: 1 });
|
|
346
|
-
assert.equal(filtered.total, 2);
|
|
347
|
-
assert.equal(filtered.rows.length, 1);
|
|
348
|
-
assert.equal(filtered.rows[0].turn, 3);
|
|
349
|
-
|
|
350
|
-
const range = readTurnLedger({ memoryRoot: root, since: '2026-01-02T00:00:00Z', until: '2026-01-02T23:59:59Z' });
|
|
351
|
-
assert.equal(range.total, 1);
|
|
352
|
-
assert.equal(range.rows[0].turn, 2);
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
test('store: missing ledger is ok with exists:false', () => {
|
|
356
|
-
const root = tempRoot();
|
|
357
|
-
const ledger = readTurnLedger({ memoryRoot: root });
|
|
358
|
-
assert.equal(ledger.ok, true);
|
|
359
|
-
assert.equal(ledger.exists, false);
|
|
360
|
-
assert.deepEqual(ledger.rows, []);
|
|
361
|
-
assert.equal(readTurnLedger({ memoryRoot: '' }).ok, false);
|
|
362
|
-
assert.equal(appendTurnRow({}, { memoryRoot: '' }).ok, false);
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
test('store: summary and calibration roundtrip with clamps', () => {
|
|
366
|
-
const root = tempRoot();
|
|
367
|
-
for (let turn = 1; turn <= 3; turn += 1) {
|
|
368
|
-
appendTurnRow({
|
|
369
|
-
at: `2026-01-0${turn}T00:00:00Z`,
|
|
370
|
-
sessionId: 's',
|
|
371
|
-
turn,
|
|
372
|
-
total: 1000 * turn,
|
|
373
|
-
layers: { fixed: 100, recent: 200, recall: 10, conclusion: 5 },
|
|
374
|
-
reconciliation: { actualInput: 1000 * turn, deltaPct: 0, within5pct: true },
|
|
375
|
-
budget: { triggered: false },
|
|
376
|
-
}, { memoryRoot: root });
|
|
377
|
-
}
|
|
378
|
-
const summary = readMetricsSummary({ memoryRoot: root });
|
|
379
|
-
assert.equal(summary.ok, true);
|
|
380
|
-
assert.equal(summary.available, true);
|
|
381
|
-
assert.equal(summary.summary.turns, 3);
|
|
382
|
-
assert.equal(summary.summary.sessions, 1);
|
|
383
|
-
assert.equal(summary.summary.medianTotal, 2000);
|
|
384
|
-
|
|
385
|
-
assert.equal(writeCalibration(root, { factor: 99, samples: 2, rawSum: 100, actualSum: 120 }).ok, true);
|
|
386
|
-
const state = readCalibration(root);
|
|
387
|
-
assert.equal(state.ok, true);
|
|
388
|
-
assert.equal(state.factor, 2);
|
|
389
|
-
assert.equal(state.samples, 2);
|
|
390
|
-
assert.equal(state.rawSum, 100);
|
|
391
|
-
assert.equal(state.actualSum, 120);
|
|
392
|
-
fs.writeFileSync(path.join(root, '.longlive', 'metrics', 'state.json'), '{bad', 'utf8');
|
|
393
|
-
const broken = readCalibration(root);
|
|
394
|
-
assert.equal(broken.ok, false);
|
|
395
|
-
assert.equal(broken.factor, 1);
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
test('store: lastConclusion is empty without a board and reads a future field', () => {
|
|
399
|
-
const root = tempRoot();
|
|
400
|
-
assert.equal(readLastConclusion({ memoryRoot: root, sessionId: 's' }), '');
|
|
401
|
-
const boardDir = path.join(root, '.longlive', 'router');
|
|
402
|
-
fs.mkdirSync(boardDir, { recursive: true });
|
|
403
|
-
fs.writeFileSync(path.join(boardDir, 'board.json'), JSON.stringify({
|
|
404
|
-
focus: [{ sessionId: 's', goal: 'x', lastConclusion: 'we decided to keep it' }],
|
|
405
|
-
}), 'utf8');
|
|
406
|
-
assert.equal(readLastConclusion({ memoryRoot: root, sessionId: 's' }), 'we decided to keep it');
|
|
407
|
-
assert.equal(readLastConclusion({ memoryRoot: '', sessionId: 's' }), '');
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
test('index: usage/header finders and registerMetrics writes on turn/end', () => {
|
|
411
|
-
const root = tempRoot();
|
|
412
|
-
const events = [
|
|
413
|
-
turnStart(1),
|
|
414
|
-
systemEvent('system', 1),
|
|
415
|
-
userEvent('hello', { kind: 'user' }, 1),
|
|
416
|
-
assistantEvent('answer', 1, { inputTokens: 10, outputTokens: 5, cacheReadTokens: 20, cacheWriteTokens: 0, totalTokens: 35 }),
|
|
417
|
-
event('request/header', { header: { tools: [{ name: 'read' }], config: { model: 'm' } } }),
|
|
418
|
-
event('turn/end', { turn: 1 }),
|
|
419
|
-
];
|
|
420
|
-
assert.equal(findUsageEvent(events, 1).data.usage.inputTokens, 10);
|
|
421
|
-
assert.equal(findRequestHeader(events).tools.length, 1);
|
|
422
|
-
|
|
423
|
-
const listeners = [];
|
|
424
|
-
const ctx = { on(name, handler) { listeners.push({ name, handler }); return () => {}; } };
|
|
425
|
-
const handle = registerMetrics(ctx, { metrics: { enabled: true } }, { memoryRoot: root, now: () => new Date('2026-11-05T00:00:00Z') });
|
|
426
|
-
assert.equal(handle.enabled, true);
|
|
427
|
-
assert.equal(listeners.length, 1);
|
|
428
|
-
assert.equal(listeners[0].name, 'session/event');
|
|
429
|
-
|
|
430
|
-
const session = {
|
|
431
|
-
id: 's-live',
|
|
432
|
-
snapshotEvents: () => events.slice(0, 4),
|
|
433
|
-
requestHeader: () => ({ tools: [{ name: 'read' }] }),
|
|
434
|
-
};
|
|
435
|
-
listeners[0].handler(session, { type: 'turn/end', data: { turn: 1 } });
|
|
436
|
-
const ledger = readTurnLedger({ memoryRoot: root, sessionId: 's-live' });
|
|
437
|
-
assert.equal(ledger.total, 1);
|
|
438
|
-
assert.equal(ledger.rows[0].turn, 1);
|
|
439
|
-
assert.equal(ledger.rows[0].actual.requestInputTokens, 30);
|
|
440
|
-
assert.equal(readCalibration(root).samples, 1);
|
|
441
|
-
|
|
442
|
-
// Duplicate turn/end must not append a second row.
|
|
443
|
-
listeners[0].handler(session, { type: 'turn/end', data: { turn: 1 } });
|
|
444
|
-
assert.equal(readTurnLedger({ memoryRoot: root, sessionId: 's-live' }).total, 1);
|
|
445
|
-
|
|
446
|
-
// A missing root disables the recorder instead of throwing.
|
|
447
|
-
assert.equal(registerMetrics(ctx, {}, {}).enabled, false);
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
test('router_status: ledger field is read-only and degrades without a root', async () => {
|
|
451
|
-
const root = tempRoot();
|
|
452
|
-
appendTurnRow({
|
|
453
|
-
at: '2026-01-01T00:00:00Z',
|
|
454
|
-
sessionId: 's',
|
|
455
|
-
turn: 1,
|
|
456
|
-
total: 123,
|
|
457
|
-
layers: { fixed: 1, recent: 2, recall: 0, conclusion: 0 },
|
|
458
|
-
reconciliation: { actualInput: 123, deltaPct: 0, within5pct: true },
|
|
459
|
-
budget: { triggered: false },
|
|
460
|
-
}, { memoryRoot: root });
|
|
461
|
-
const service = {
|
|
462
|
-
status: () => ({ state: 'idle', runId: 'r1' }),
|
|
463
|
-
deps: { memoryRoot: root },
|
|
464
|
-
};
|
|
465
|
-
const tools = createRouterTools(service);
|
|
466
|
-
const status = tools.find((tool) => tool.name === 'router_status');
|
|
467
|
-
const result = await status.invoke();
|
|
468
|
-
assert.equal(result.ok, true);
|
|
469
|
-
assert.equal(result.state, 'idle');
|
|
470
|
-
assert.equal(result.ledger.ok, true);
|
|
471
|
-
assert.equal(result.ledger.available, true);
|
|
472
|
-
assert.equal(result.ledger.summary.turns, 1);
|
|
473
|
-
|
|
474
|
-
const noRoot = createRouterTools({ status: () => ({ state: 'idle' }), deps: {} });
|
|
475
|
-
const noRootResult = await noRoot.find((tool) => tool.name === 'router_status').invoke();
|
|
476
|
-
assert.equal(noRootResult.ledger.ok, false);
|
|
477
|
-
assert.equal(noRootResult.ledger.code, 'E_MEMORY_ROOT_UNSET');
|
|
478
|
-
});
|