dsh-session-matrix 0.16.6 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -5
- package/lib/client.js +19 -8
- package/lib/index.js +90 -11
- package/package.json +2 -2
- package/test/risk-matrix.test.mjs +97 -0
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
<a href="https://www.npmjs.com/package/dsh-session-matrix"><img src="https://img.shields.io/npm/v/dsh-session-matrix?label=npm&color=blue" alt="npm"></a>
|
|
11
11
|
<a href="https://github.com/Wanqiu12345/dsh-session-matrix/actions/workflows/ci.yml"><img src="https://github.com/Wanqiu12345/dsh-session-matrix/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
12
12
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="MIT"></a>
|
|
13
|
+
<a href="https://dsh.market/"><img src="https://raw.githubusercontent.com/2BingLing/dsh-market/master/assets/readme/badge-listed-zh.svg" alt="DSH Market"></a>
|
|
13
14
|
<img src="https://img.shields.io/badge/DSH-%E2%89%A50.2.2--rc.1-8a2be2" alt="DSH >= 0.2.2-rc.1">
|
|
14
15
|
<img src="https://img.shields.io/badge/runtime%20deps-0-brightgreen" alt="no runtime deps">
|
|
15
16
|
</p>
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
<a href="#vs-context"><b>与 dsh-context</b></a>
|
|
28
29
|
</p>
|
|
29
30
|
|
|
30
|
-

|
|
31
32
|
|
|
32
33
|
---
|
|
33
34
|
|
|
@@ -64,7 +65,9 @@
|
|
|
64
65
|
- 每格挂载的是 DSH 原生 `conversation.content` 工厂实例:消息流、工具调用行、审批、
|
|
65
66
|
Chat/Trajectory 切换、composer 全是原生的,**可以直接打字**。
|
|
66
67
|
|
|
67
|
-

|
|
69
|
+
|
|
70
|
+

|
|
68
71
|
|
|
69
72
|
<a id="graph"></a>
|
|
70
73
|
|
|
@@ -81,7 +84,7 @@
|
|
|
81
84
|
- **节点尺寸 = 累计用量**,谁最重一眼能看出来。
|
|
82
85
|
- 选中任一节点,下方三个模块按该节点的子树重新计算。
|
|
83
86
|
|
|
84
|
-

|
|
85
88
|
|
|
86
89
|
### 三个模块
|
|
87
90
|
|
|
@@ -93,7 +96,7 @@
|
|
|
93
96
|
|
|
94
97
|
「当前占用」和「合计」不是同一个量:前者是此刻占着多少,后者是历史上一共读过多少。
|
|
95
98
|
|
|
96
|
-

|
|
97
100
|
|
|
98
101
|
## 上下文监控
|
|
99
102
|
|
|
@@ -115,6 +118,20 @@ pressureBudget = messageBudget − policy.headroomTokens # headroom 默认
|
|
|
115
118
|
占用率取**服务端返回的 prompt 大小**(未命中 + 缓存读 + 缓存写),叠加最后一次采样后的增量,
|
|
116
119
|
不使用字符数估算。
|
|
117
120
|
|
|
121
|
+
### 占用率之外:压缩次数才是「要不要交接」的依据
|
|
122
|
+
|
|
123
|
+
占用率只回答「离压缩点还有多远」;是否值得交接,看的是这个会话**已经压缩过几次**。
|
|
124
|
+
DSH 每次压缩都会在会话里留下一个检查点(`compact-checkpoint`),插件从会话日志里把它们数出来:
|
|
125
|
+
|
|
126
|
+
| 压缩次数 | 到达临界线时 |
|
|
127
|
+
|:--|:--|
|
|
128
|
+
| **0 次** | 标「首次接近」——历史完整,会话仍可用,不推交接 |
|
|
129
|
+
| **1–2 次** | 标「临界」——建议交接:细节损失已经开始叠加 |
|
|
130
|
+
| **≥3 次** | 一过注意线就升级「临界」——社区共识:两次压缩即任务范围失控 |
|
|
131
|
+
|
|
132
|
+
刚压缩完 10 分钟内摘要最新鲜,提示自动降半级;日志尚未解析到的会话如实显示
|
|
133
|
+
「压缩次数未知」,不猜。
|
|
134
|
+
|
|
118
135
|
<a id="handoff"></a>
|
|
119
136
|
|
|
120
137
|
## 交接协议 v1
|
|
@@ -133,7 +150,7 @@ pressureBudget = messageBudget − policy.headroomTokens # headroom 默认
|
|
|
133
150
|
|
|
134
151
|
插件负责复制与定位:目标窗格会闪烁一次,避免粘错会话。
|
|
135
152
|
|
|
136
|
-

|
|
137
154
|
|
|
138
155
|
## 费用口径
|
|
139
156
|
|
package/lib/client.js
CHANGED
|
@@ -1977,9 +1977,13 @@ window.__ModuleLoader__.load({
|
|
|
1977
1977
|
risk ? h('span', {
|
|
1978
1978
|
key: 'rk',
|
|
1979
1979
|
className: `aw-risk ${risk} tip-r`,
|
|
1980
|
-
'data-tip':
|
|
1981
|
-
?
|
|
1982
|
-
:
|
|
1980
|
+
'data-tip': session.firstApproach
|
|
1981
|
+
? `首次接近临界线(压缩点 × ${Math.round((th?.criticalFactor ?? DEFAULT_FACTORS.criticalFactor) * 100)}%):还没有压缩过,历史完整可用;建议收尾当前这一步后观察`
|
|
1982
|
+
: risk === 'crit'
|
|
1983
|
+
? `已到临界线,建议交接${typeof session.compactions === 'number' && session.compactions > 0
|
|
1984
|
+
? `(已压缩 ${session.compactions} 次,细节损失叠加)` : ''}`
|
|
1985
|
+
: `接近压缩点(压缩点 × ${Math.round((th?.warnFactor ?? DEFAULT_FACTORS.warnFactor) * 100)}%)${typeof session.compactions === 'number' && session.compactions > 0
|
|
1986
|
+
? `,已压缩 ${session.compactions} 次` : ''}:再增长一段就会触发自动压缩`,
|
|
1983
1987
|
}, risk === 'crit' ? T.levelCrit : T.levelWarn) : null,
|
|
1984
1988
|
h('button', {
|
|
1985
1989
|
key: 'open',
|
|
@@ -2599,6 +2603,10 @@ window.__ModuleLoader__.load({
|
|
|
2599
2603
|
h('span', { key: 'h', className: 'aw-hcard-h' }, [
|
|
2600
2604
|
h('span', { key: 't', className: 't' }, s.title || T.noTitle),
|
|
2601
2605
|
paneIds.includes(s.id) ? h('span', { key: 'g', className: 'tag' }, '已载入') : null,
|
|
2606
|
+
s.firstApproach ? h('span', { key: 'fa', className: 'tag' }, '首次接近 · 未压缩') : null,
|
|
2607
|
+
typeof s.compactions === 'number' && s.compactions > 0
|
|
2608
|
+
? h('span', { key: 'k', className: 'tag' }, `已压缩 ${s.compactions} 次`)
|
|
2609
|
+
: null,
|
|
2602
2610
|
].filter(Boolean)),
|
|
2603
2611
|
// One bar, two facts: how full it is, and where DSH would compact.
|
|
2604
2612
|
h('span', { key: 'bar', className: 'aw-hcard-bar' }, [
|
|
@@ -2633,13 +2641,16 @@ window.__ModuleLoader__.load({
|
|
|
2633
2641
|
h('p', { className: 'aw-hsec', key: 'w1' }, T.whyTitle),
|
|
2634
2642
|
h('div', { className: 'aw-hp', key: 'w2' }, [
|
|
2635
2643
|
h('div', { key: 'a' },
|
|
2636
|
-
'
|
|
2637
|
-
+ '
|
|
2644
|
+
'占用率只回答「离压缩点还有多远」;是否值得交接,看的是压缩次数。'
|
|
2645
|
+
+ '每压缩一次,早期对话就被换成一层摘要,路径、命令、报错最先丢——损失是叠加的。'),
|
|
2638
2646
|
h('div', { key: 'b' },
|
|
2639
|
-
'
|
|
2640
|
-
+ '
|
|
2647
|
+
'占用率达到临界线,说的是「该收尾当前这一步了」。但一个从未压缩过的新会话第一次到达临界,'
|
|
2648
|
+
+ '历史是完整的,会话仍然好用——面板只会标「首次接近」,不会推交接。'),
|
|
2641
2649
|
h('div', { key: 'c' },
|
|
2642
|
-
'
|
|
2650
|
+
'已压缩 2 次再达临界,说明任务范围已经超过一个会话该装的量,建议交接;'
|
|
2651
|
+
+ '压缩 ≥3 次的会话,一过注意线就升级提醒。刚压缩完 10 分钟内摘要最新鲜,提示会自动降半级。'),
|
|
2652
|
+
h('div', { key: 'd' },
|
|
2653
|
+
'压缩点本身照搬官方规则:窗口 − 最大输出 − 4K 余量(与 dsh-compaction-basic 一致)。'
|
|
2643
2654
|
+ '1,048,576 的窗口配 256,000 的输出预留,压缩点是 788,480(75.2%)。'),
|
|
2644
2655
|
]),
|
|
2645
2656
|
// The three lines the panel paints, computed for the session that is
|
package/lib/index.js
CHANGED
|
@@ -202,16 +202,77 @@ function compactPointOf(window, output) {
|
|
|
202
202
|
* @param cfg - thresholds as fractions of `point`.
|
|
203
203
|
* @returns risk level.
|
|
204
204
|
*/
|
|
205
|
-
|
|
205
|
+
/**
|
|
206
|
+
* Count compaction checkpoints in one session's event stream.
|
|
207
|
+
*
|
|
208
|
+
* Every compaction run persists a `user/message` whose source carries
|
|
209
|
+
* `{ kind: 'compact-checkpoint', compactionId }` — manual /compact included
|
|
210
|
+
* (its source also has `sourceCommandId`). Counting them is the only way to
|
|
211
|
+
* know how many times a session's history has been rebuilt: the host exposes
|
|
212
|
+
* no compaction projection, and `contextTimeline.requests` is a two-slot ring
|
|
213
|
+
* buffer that only ever shows the most recent one.
|
|
214
|
+
* @param events - persisted session events (or parsed log lines).
|
|
215
|
+
* @returns `{ compactions, lastCompactionAt }` — zero when the stream has none.
|
|
216
|
+
*/
|
|
217
|
+
/** A summary younger than this is considered fresh; freshness demotes one level. */
|
|
218
|
+
const COMPACTION_FRESH_MS = 10 * 60_000
|
|
219
|
+
|
|
220
|
+
export function countCheckpoints(events) {
|
|
221
|
+
let compactions = 0
|
|
222
|
+
let lastCompactionAt = null
|
|
223
|
+
for (const event of events) {
|
|
224
|
+
if (event?.type !== 'user/message') continue
|
|
225
|
+
if (event.data?.source?.kind !== 'compact-checkpoint') continue
|
|
226
|
+
compactions += 1
|
|
227
|
+
const time = Number(event.time)
|
|
228
|
+
if (Number.isFinite(time)) lastCompactionAt = Math.max(lastCompactionAt ?? time, time)
|
|
229
|
+
}
|
|
230
|
+
return { compactions, lastCompactionAt }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Occupancy decides WHEN compaction looms; the compaction count decides
|
|
235
|
+
* WHETHER the session is worth handing off. Community consensus (Claude Code
|
|
236
|
+
* ecosystem) is that two compactions in one session already mean the task was
|
|
237
|
+
* scoped wrong, and every further pass stacks information loss on top.
|
|
238
|
+
*
|
|
239
|
+
* Matrix (docs/32 §3):
|
|
240
|
+
* 0 compactions — a fresh session first crossing the critical line is still
|
|
241
|
+
* fully usable: the history has never been rebuilt, so the alarm is
|
|
242
|
+
* demoted to warn and flagged as firstApproach instead of pushing a
|
|
243
|
+
* handoff the user does not need yet.
|
|
244
|
+
* 1–2 compactions — critical occupancy escalates to a handoff suggestion.
|
|
245
|
+
* ≥3 compactions — warn occupancy is already a handoff suggestion: history
|
|
246
|
+
* has been rebuilt repeatedly and detail loss is stacked.
|
|
247
|
+
* A compaction in the last ten minutes leaves the freshest possible summary,
|
|
248
|
+
* so the level is demoted one step regardless of the matrix.
|
|
249
|
+
*
|
|
250
|
+
* @param stats - `{ compactions, lastCompactionAt }` from the log pipeline;
|
|
251
|
+
* `compactions === null` means "not parsed yet" and keeps the old
|
|
252
|
+
* occupancy-only verdict rather than guessing.
|
|
253
|
+
*/
|
|
254
|
+
export function riskLevel(ratio, point, cfg, stats) {
|
|
206
255
|
if (ratio == null) return 'unknown'
|
|
256
|
+
let level
|
|
207
257
|
if (!point) {
|
|
208
|
-
|
|
209
|
-
|
|
258
|
+
level = ratio >= 0.85 ? 'crit' : ratio >= 0.7 ? 'warn' : 'ok'
|
|
259
|
+
} else if (ratio >= point.ratio * cfg.criticalFactor) {
|
|
260
|
+
level = 'crit'
|
|
261
|
+
} else if (ratio >= point.ratio * cfg.warnFactor) {
|
|
262
|
+
level = 'warn'
|
|
263
|
+
} else {
|
|
210
264
|
return 'ok'
|
|
211
265
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
266
|
+
|
|
267
|
+
const count = stats?.compactions
|
|
268
|
+
const firstApproach = count === 0 && level === 'crit'
|
|
269
|
+
if (firstApproach) level = 'warn'
|
|
270
|
+
else if (typeof count === 'number' && count >= 3 && level === 'warn') level = 'crit'
|
|
271
|
+
|
|
272
|
+
const fresh = stats?.lastCompactionAt != null
|
|
273
|
+
&& Date.now() - stats.lastCompactionAt < COMPACTION_FRESH_MS
|
|
274
|
+
if (fresh) level = level === 'crit' ? 'warn' : level === 'warn' ? 'ok' : level
|
|
275
|
+
return level
|
|
215
276
|
}
|
|
216
277
|
|
|
217
278
|
/**
|
|
@@ -716,7 +777,10 @@ export function apply(ctx, config = {}) {
|
|
|
716
777
|
// dsh-context), use the split reconstructed from the session log; the
|
|
717
778
|
// lifetime totals times `peakFactor` is the last resort.
|
|
718
779
|
const projectionBuckets = costOfBuckets(timeline, priceBook.lookup, deepseekCny)
|
|
719
|
-
|
|
780
|
+
// Compaction counts come from the log pipeline regardless of where the
|
|
781
|
+
// usage buckets came from — a projection-priced session still has a log.
|
|
782
|
+
const logStats = LOG_SPLITS.get(id)
|
|
783
|
+
const split = projectionBuckets ? null : logStats
|
|
720
784
|
const logBuckets = !projectionBuckets && split && (split.peak || split.off)
|
|
721
785
|
? costOfBuckets(
|
|
722
786
|
{ cost: { [providerName ?? 'deepseek']: { [modelName ?? 'unknown']: { peak: split.peak, off: split.off } } } },
|
|
@@ -774,6 +838,7 @@ export function apply(ctx, config = {}) {
|
|
|
774
838
|
? `子代理${stamp ? ' · ' + stamp : ''}`
|
|
775
839
|
: `未命名会话${stamp ? ' · ' + stamp : ''}`
|
|
776
840
|
|
|
841
|
+
const rowRisk = riskLevel(ratio, point, cfg, logStats)
|
|
777
842
|
return {
|
|
778
843
|
id,
|
|
779
844
|
title,
|
|
@@ -810,7 +875,10 @@ export function apply(ctx, config = {}) {
|
|
|
810
875
|
costPeakBuckets: buckets ? buckets.peakBuckets : 0,
|
|
811
876
|
updatedAt: values?.sessionListMetadata?.lastPromptAt ?? header.createdAt ?? null,
|
|
812
877
|
activityDays,
|
|
813
|
-
|
|
878
|
+
compactions: logStats?.compactions ?? null,
|
|
879
|
+
lastCompactionAt: logStats?.lastCompactionAt ?? null,
|
|
880
|
+
firstApproach: rowRisk === 'warn' && logStats?.compactions === 0 && ratio >= (point?.ratio ?? Infinity) * cfg.criticalFactor,
|
|
881
|
+
risk: rowRisk,
|
|
814
882
|
contextAvailable: total != null,
|
|
815
883
|
}
|
|
816
884
|
}
|
|
@@ -1051,13 +1119,15 @@ export function apply(ctx, config = {}) {
|
|
|
1051
1119
|
const handle = await persistence.open(id, 'read')
|
|
1052
1120
|
try {
|
|
1053
1121
|
const result = await handle.read()
|
|
1054
|
-
const
|
|
1122
|
+
const events = result?.events ?? []
|
|
1123
|
+
const split = splitUsageEvents(events)
|
|
1055
1124
|
const entry = {
|
|
1056
1125
|
revision,
|
|
1057
1126
|
size,
|
|
1058
1127
|
at: Date.now(),
|
|
1059
1128
|
peak: split?.peak ?? null,
|
|
1060
1129
|
off: split?.off ?? null,
|
|
1130
|
+
...countCheckpoints(events),
|
|
1061
1131
|
}
|
|
1062
1132
|
LOG_SPLITS.set(id, entry)
|
|
1063
1133
|
cache.delete(id)
|
|
@@ -1146,8 +1216,17 @@ export function apply(ctx, config = {}) {
|
|
|
1146
1216
|
const stamp = { mtime: item.stat.mtimeMs, size: item.stat.size }
|
|
1147
1217
|
try {
|
|
1148
1218
|
const buf = await fs.readFile(item.file)
|
|
1149
|
-
const
|
|
1150
|
-
|
|
1219
|
+
const text = decompressFrames(buf, zstd)
|
|
1220
|
+
const split = splitUsageText(text)
|
|
1221
|
+
LOG_SPLITS.set(item.id, {
|
|
1222
|
+
...stamp,
|
|
1223
|
+
at: Date.now(),
|
|
1224
|
+
peak: split?.peak ?? null,
|
|
1225
|
+
off: split?.off ?? null,
|
|
1226
|
+
...countCheckpoints(text.split('\n').map((line) => {
|
|
1227
|
+
try { return JSON.parse(line) } catch { return null }
|
|
1228
|
+
})),
|
|
1229
|
+
})
|
|
1151
1230
|
// The DTO cache may hold a totals-priced copy: drop it so the next
|
|
1152
1231
|
// collect re-prices with the buckets we just found.
|
|
1153
1232
|
cache.delete(item.id)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-matrix",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "A multi-agent workspace for DeepSeek Harness: run parallel sessions, visualize agent relationships, monitor context usage, and manage task handoffs.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"deepseek-harness",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"scripts": {
|
|
73
|
-
"test": "node test/host.test.mjs && node test/usage-split.test.mjs && node test/usage-reconcile.test.mjs && node test/usage-vs-projection.test.mjs",
|
|
73
|
+
"test": "node test/host.test.mjs && node test/usage-split.test.mjs && node test/usage-reconcile.test.mjs && node test/risk-matrix.test.mjs && node test/usage-vs-projection.test.mjs",
|
|
74
74
|
"prepublishOnly": "npm test"
|
|
75
75
|
}
|
|
76
76
|
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-factor risk matrix tests — docs/32 §3.
|
|
3
|
+
*
|
|
4
|
+
* The point of this matrix is the first row: a fresh session (zero
|
|
5
|
+
* compactions) crossing the critical occupancy line for the first time is
|
|
6
|
+
* still fully usable — its history has never been rebuilt — so it must NOT
|
|
7
|
+
* read as a handoff alarm. Occupancy decides WHEN compaction looms; the
|
|
8
|
+
* compaction count decides WHETHER a handoff is worth it.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { riskLevel, countCheckpoints } from '../lib/index.js'
|
|
12
|
+
|
|
13
|
+
const pass = []
|
|
14
|
+
const fail = []
|
|
15
|
+
const check = (label, fn) => {
|
|
16
|
+
try { fn(); pass.push(label) } catch (error) { fail.push(`${label}: ${error.message}`) }
|
|
17
|
+
}
|
|
18
|
+
const assert = (cond, message) => { if (!cond) throw new Error(message) }
|
|
19
|
+
|
|
20
|
+
// 1M window, 256k output → point = 788,480 (75.2%), like the README example.
|
|
21
|
+
const POINT = { ratio: 788480 / 1048576 }
|
|
22
|
+
const CFG = { warnFactor: 0.8, criticalFactor: 0.92 }
|
|
23
|
+
const CRIT = POINT.ratio * CFG.criticalFactor
|
|
24
|
+
const WARN = POINT.ratio * CFG.warnFactor
|
|
25
|
+
const FRESH = Date.now() - 5 * 60_000 // 5 minutes ago
|
|
26
|
+
|
|
27
|
+
check('0 次压缩:临界占用降级为注意(首次接近,不推交接)', () => {
|
|
28
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, { compactions: 0 }) === 'warn',
|
|
29
|
+
'first approach at critical must read warn')
|
|
30
|
+
})
|
|
31
|
+
check('0 次压缩:注意占用为注意', () => {
|
|
32
|
+
assert(riskLevel(WARN + 1, POINT, CFG, { compactions: 0 }) === 'warn',
|
|
33
|
+
'first approach below critical is warn')
|
|
34
|
+
})
|
|
35
|
+
check('1–2 次压缩:临界占用为临界(建议交接)', () => {
|
|
36
|
+
for (const n of [1, 2]) {
|
|
37
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, { compactions: n }) === 'crit',
|
|
38
|
+
`${n} compactions at critical must read crit`)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
check('≥3 次压缩:注意占用即升级为临界', () => {
|
|
42
|
+
for (const n of [3, 5, 9]) {
|
|
43
|
+
assert(riskLevel(WARN + 1, POINT, CFG, { compactions: n }) === 'crit',
|
|
44
|
+
`${n} compactions at warn must read crit`)
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
check('≥3 次压缩但占用正常:仍为正常(刚重建的历史有全部余量)', () => {
|
|
48
|
+
assert(riskLevel(0.1, POINT, CFG, { compactions: 4 }) === 'ok',
|
|
49
|
+
'fresh headroom must not stay flagged')
|
|
50
|
+
})
|
|
51
|
+
check('刚压缩完 10 分钟内:临界降级为注意(摘要最新鲜)', () => {
|
|
52
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, { compactions: 3, lastCompactionAt: FRESH }) === 'warn',
|
|
53
|
+
'fresh summary demotes crit to warn')
|
|
54
|
+
})
|
|
55
|
+
check('压缩发生在 10 分钟前:新鲜度豁免不适用', () => {
|
|
56
|
+
const old = Date.now() - 11 * 60_000
|
|
57
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, { compactions: 2, lastCompactionAt: old }) === 'crit',
|
|
58
|
+
'stale summary keeps crit')
|
|
59
|
+
})
|
|
60
|
+
check('压缩次数未知:维持占用单指标行为', () => {
|
|
61
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, null) === 'crit', 'null stats keeps crit')
|
|
62
|
+
assert(riskLevel(CRIT + 1, POINT, CFG, { compactions: null }) === 'crit',
|
|
63
|
+
'null count keeps crit')
|
|
64
|
+
})
|
|
65
|
+
check('无压缩点(未知模型):沿用 85%/70% 兜底', () => {
|
|
66
|
+
assert(riskLevel(0.86, null, CFG, { compactions: 0 }) === 'warn', '0.86 fallback warn')
|
|
67
|
+
assert(riskLevel(0.9, null, CFG, { compactions: 4 }) === 'crit', '0.9 fallback crit')
|
|
68
|
+
})
|
|
69
|
+
check('占用未知:unknown', () => {
|
|
70
|
+
assert(riskLevel(null, POINT, CFG, { compactions: 0 }) === 'unknown')
|
|
71
|
+
})
|
|
72
|
+
check('占用低于注意线恒为 ok(新鲜度豁免也不越级)', () => {
|
|
73
|
+
assert(riskLevel(0.1, POINT, CFG, { compactions: 2, lastCompactionAt: FRESH }) === 'ok',
|
|
74
|
+
'low occupancy stays ok even when fresh')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
check('countCheckpoints:从事件流数检查点并取最新时间', () => {
|
|
78
|
+
const events = [
|
|
79
|
+
{ type: 'user/message', time: 100, data: { content: 'hi' } },
|
|
80
|
+
{ type: 'user/message', time: 200, data: { source: { kind: 'compact-checkpoint', compactionId: 'a' } } },
|
|
81
|
+
{ type: 'assistant/message', time: 300, data: { usage: {} } },
|
|
82
|
+
{ type: 'user/message', time: 400, data: { source: { kind: 'compact-checkpoint', compactionId: 'b' } } },
|
|
83
|
+
]
|
|
84
|
+
const { compactions, lastCompactionAt } = countCheckpoints(events)
|
|
85
|
+
assert(compactions === 2, `expected 2 checkpoints, got ${compactions}`)
|
|
86
|
+
assert(lastCompactionAt === 400, `expected last time 400, got ${lastCompactionAt}`)
|
|
87
|
+
})
|
|
88
|
+
check('countCheckpoints:没有检查点时返回 0 / null', () => {
|
|
89
|
+
const { compactions, lastCompactionAt } = countCheckpoints([
|
|
90
|
+
{ type: 'user/message', time: 1, data: {} },
|
|
91
|
+
])
|
|
92
|
+
assert(compactions === 0 && lastCompactionAt === null, 'empty stream must be 0/null')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
console.log(`risk-matrix.test.mjs — ${pass.length} passed, ${fail.length} failed`)
|
|
96
|
+
for (const label of fail) console.log(` ✗ ${label}`)
|
|
97
|
+
process.exitCode = fail.length ? 1 : 0
|