dsh-session-matrix 0.16.6 → 0.17.1
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 +21 -9
- package/lib/index.js +103 -11
- package/package.json +2 -2
- package/test/risk-matrix.test.mjs +109 -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,14 +2641,18 @@ 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
|
+
'Agent 干活的时候,上下文会被不断压缩:早期对话换成摘要,路径、命令、报错最先丢。'
|
|
2645
|
+
+ '压缩了多少次,面板已经帮你数出来了——就是卡片上的「已压缩 N 次」。'),
|
|
2638
2646
|
h('div', { key: 'b' },
|
|
2639
|
-
'
|
|
2640
|
-
+ '
|
|
2647
|
+
'我们调研了 Claude Code 社区的经验:同一个会话压缩到第 2 次,'
|
|
2648
|
+
+ '就说明任务已经超过一个会话该装的量;压到 3 次以上,模型开始忘掉早期的要求和约束,'
|
|
2649
|
+
+ '硬撑下去会越做越差,甚至反复返工。'),
|
|
2641
2650
|
h('div', { key: 'c' },
|
|
2642
|
-
'
|
|
2643
|
-
+ '
|
|
2651
|
+
'所以面板的提示是两条线一起看的:占用率到临界线 = 该收尾当前这一步;'
|
|
2652
|
+
+ '压缩过 2 次以上再到临界 = 该交接了,开个新会话接着干。'),
|
|
2653
|
+
h('div', { key: 'd' },
|
|
2654
|
+
'从没压缩过的新会话第一次到临界线不用慌——历史还完整,继续干活就行。'
|
|
2655
|
+
+ '刚压缩完 10 分钟内摘要最新鲜,提示也会自动放宽。'),
|
|
2644
2656
|
]),
|
|
2645
2657
|
// The three lines the panel paints, computed for the session that is
|
|
2646
2658
|
// about to receive the prompt — a different model means different
|
package/lib/index.js
CHANGED
|
@@ -202,16 +202,90 @@ 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 fullCompactions = 0
|
|
222
|
+
let pruneCompactions = 0
|
|
223
|
+
let lastCompactionAt = null
|
|
224
|
+
for (const event of events) {
|
|
225
|
+
let kind = null
|
|
226
|
+
let time = Number(event?.time)
|
|
227
|
+
if (event?.type === 'user/message' && event.data?.source?.kind === 'compact-checkpoint') {
|
|
228
|
+
kind = 'full' // full compaction: history rebuilt into a summary
|
|
229
|
+
} else if (event?.type === 'compaction/prune') {
|
|
230
|
+
kind = 'prune' // tool-result pruning: outputs dropped, no checkpoint
|
|
231
|
+
} else {
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
if (kind === 'full') fullCompactions += 1
|
|
235
|
+
else pruneCompactions += 1
|
|
236
|
+
if (Number.isFinite(time)) lastCompactionAt = Math.max(lastCompactionAt ?? time, time)
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
compactions: fullCompactions + pruneCompactions,
|
|
240
|
+
fullCompactions,
|
|
241
|
+
pruneCompactions,
|
|
242
|
+
lastCompactionAt,
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Occupancy decides WHEN compaction looms; the compaction count decides
|
|
248
|
+
* WHETHER the session is worth handing off. Community consensus (Claude Code
|
|
249
|
+
* ecosystem) is that two compactions in one session already mean the task was
|
|
250
|
+
* scoped wrong, and every further pass stacks information loss on top.
|
|
251
|
+
*
|
|
252
|
+
* Matrix (docs/32 §3):
|
|
253
|
+
* 0 compactions — a fresh session first crossing the critical line is still
|
|
254
|
+
* fully usable: the history has never been rebuilt, so the alarm is
|
|
255
|
+
* demoted to warn and flagged as firstApproach instead of pushing a
|
|
256
|
+
* handoff the user does not need yet.
|
|
257
|
+
* 1–2 compactions — critical occupancy escalates to a handoff suggestion.
|
|
258
|
+
* ≥3 compactions — warn occupancy is already a handoff suggestion: history
|
|
259
|
+
* has been rebuilt repeatedly and detail loss is stacked.
|
|
260
|
+
* A compaction in the last ten minutes leaves the freshest possible summary,
|
|
261
|
+
* so the level is demoted one step regardless of the matrix.
|
|
262
|
+
*
|
|
263
|
+
* @param stats - `{ compactions, lastCompactionAt }` from the log pipeline;
|
|
264
|
+
* `compactions === null` means "not parsed yet" and keeps the old
|
|
265
|
+
* occupancy-only verdict rather than guessing.
|
|
266
|
+
*/
|
|
267
|
+
export function riskLevel(ratio, point, cfg, stats) {
|
|
206
268
|
if (ratio == null) return 'unknown'
|
|
269
|
+
let level
|
|
207
270
|
if (!point) {
|
|
208
|
-
|
|
209
|
-
|
|
271
|
+
level = ratio >= 0.85 ? 'crit' : ratio >= 0.7 ? 'warn' : 'ok'
|
|
272
|
+
} else if (ratio >= point.ratio * cfg.criticalFactor) {
|
|
273
|
+
level = 'crit'
|
|
274
|
+
} else if (ratio >= point.ratio * cfg.warnFactor) {
|
|
275
|
+
level = 'warn'
|
|
276
|
+
} else {
|
|
210
277
|
return 'ok'
|
|
211
278
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
279
|
+
|
|
280
|
+
const count = stats?.compactions
|
|
281
|
+
const firstApproach = count === 0 && level === 'crit'
|
|
282
|
+
if (firstApproach) level = 'warn'
|
|
283
|
+
else if (typeof count === 'number' && count >= 3 && level === 'warn') level = 'crit'
|
|
284
|
+
|
|
285
|
+
const fresh = stats?.lastCompactionAt != null
|
|
286
|
+
&& Date.now() - stats.lastCompactionAt < COMPACTION_FRESH_MS
|
|
287
|
+
if (fresh) level = level === 'crit' ? 'warn' : level === 'warn' ? 'ok' : level
|
|
288
|
+
return level
|
|
215
289
|
}
|
|
216
290
|
|
|
217
291
|
/**
|
|
@@ -716,7 +790,10 @@ export function apply(ctx, config = {}) {
|
|
|
716
790
|
// dsh-context), use the split reconstructed from the session log; the
|
|
717
791
|
// lifetime totals times `peakFactor` is the last resort.
|
|
718
792
|
const projectionBuckets = costOfBuckets(timeline, priceBook.lookup, deepseekCny)
|
|
719
|
-
|
|
793
|
+
// Compaction counts come from the log pipeline regardless of where the
|
|
794
|
+
// usage buckets came from — a projection-priced session still has a log.
|
|
795
|
+
const logStats = LOG_SPLITS.get(id)
|
|
796
|
+
const split = projectionBuckets ? null : logStats
|
|
720
797
|
const logBuckets = !projectionBuckets && split && (split.peak || split.off)
|
|
721
798
|
? costOfBuckets(
|
|
722
799
|
{ cost: { [providerName ?? 'deepseek']: { [modelName ?? 'unknown']: { peak: split.peak, off: split.off } } } },
|
|
@@ -774,6 +851,7 @@ export function apply(ctx, config = {}) {
|
|
|
774
851
|
? `子代理${stamp ? ' · ' + stamp : ''}`
|
|
775
852
|
: `未命名会话${stamp ? ' · ' + stamp : ''}`
|
|
776
853
|
|
|
854
|
+
const rowRisk = riskLevel(ratio, point, cfg, logStats)
|
|
777
855
|
return {
|
|
778
856
|
id,
|
|
779
857
|
title,
|
|
@@ -810,7 +888,10 @@ export function apply(ctx, config = {}) {
|
|
|
810
888
|
costPeakBuckets: buckets ? buckets.peakBuckets : 0,
|
|
811
889
|
updatedAt: values?.sessionListMetadata?.lastPromptAt ?? header.createdAt ?? null,
|
|
812
890
|
activityDays,
|
|
813
|
-
|
|
891
|
+
compactions: logStats?.compactions ?? null,
|
|
892
|
+
lastCompactionAt: logStats?.lastCompactionAt ?? null,
|
|
893
|
+
firstApproach: rowRisk === 'warn' && logStats?.compactions === 0 && ratio >= (point?.ratio ?? Infinity) * cfg.criticalFactor,
|
|
894
|
+
risk: rowRisk,
|
|
814
895
|
contextAvailable: total != null,
|
|
815
896
|
}
|
|
816
897
|
}
|
|
@@ -1051,13 +1132,15 @@ export function apply(ctx, config = {}) {
|
|
|
1051
1132
|
const handle = await persistence.open(id, 'read')
|
|
1052
1133
|
try {
|
|
1053
1134
|
const result = await handle.read()
|
|
1054
|
-
const
|
|
1135
|
+
const events = result?.events ?? []
|
|
1136
|
+
const split = splitUsageEvents(events)
|
|
1055
1137
|
const entry = {
|
|
1056
1138
|
revision,
|
|
1057
1139
|
size,
|
|
1058
1140
|
at: Date.now(),
|
|
1059
1141
|
peak: split?.peak ?? null,
|
|
1060
1142
|
off: split?.off ?? null,
|
|
1143
|
+
...countCheckpoints(events),
|
|
1061
1144
|
}
|
|
1062
1145
|
LOG_SPLITS.set(id, entry)
|
|
1063
1146
|
cache.delete(id)
|
|
@@ -1146,8 +1229,17 @@ export function apply(ctx, config = {}) {
|
|
|
1146
1229
|
const stamp = { mtime: item.stat.mtimeMs, size: item.stat.size }
|
|
1147
1230
|
try {
|
|
1148
1231
|
const buf = await fs.readFile(item.file)
|
|
1149
|
-
const
|
|
1150
|
-
|
|
1232
|
+
const text = decompressFrames(buf, zstd)
|
|
1233
|
+
const split = splitUsageText(text)
|
|
1234
|
+
LOG_SPLITS.set(item.id, {
|
|
1235
|
+
...stamp,
|
|
1236
|
+
at: Date.now(),
|
|
1237
|
+
peak: split?.peak ?? null,
|
|
1238
|
+
off: split?.off ?? null,
|
|
1239
|
+
...countCheckpoints(text.split('\n').map((line) => {
|
|
1240
|
+
try { return JSON.parse(line) } catch { return null }
|
|
1241
|
+
})),
|
|
1242
|
+
})
|
|
1151
1243
|
// The DTO cache may hold a totals-priced copy: drop it so the next
|
|
1152
1244
|
// collect re-prices with the buckets we just found.
|
|
1153
1245
|
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.1",
|
|
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,109 @@
|
|
|
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: 'compaction/prune', time: 350, data: { shadowedTokenCount: 5000 } },
|
|
83
|
+
{ type: 'user/message', time: 400, data: { source: { kind: 'compact-checkpoint', compactionId: 'b' } } },
|
|
84
|
+
{ type: 'compaction/prune', time: 450, data: { shadowedTokenCount: 8000 } },
|
|
85
|
+
]
|
|
86
|
+
const r = countCheckpoints(events)
|
|
87
|
+
assert(r.compactions === 4, `expected total 4, got ${r.compactions}`)
|
|
88
|
+
assert(r.fullCompactions === 2, `expected 2 full, got ${r.fullCompactions}`)
|
|
89
|
+
assert(r.pruneCompactions === 2, `expected 2 prunes, got ${r.pruneCompactions}`)
|
|
90
|
+
assert(r.lastCompactionAt === 450, `expected last time 450, got ${r.lastCompactionAt}`)
|
|
91
|
+
})
|
|
92
|
+
check('countCheckpoints:没有压缩动作时返回 0 / null', () => {
|
|
93
|
+
const { compactions, lastCompactionAt } = countCheckpoints([
|
|
94
|
+
{ type: 'user/message', time: 1, data: {} },
|
|
95
|
+
])
|
|
96
|
+
assert(compactions === 0 && lastCompactionAt === null, 'empty stream must be 0/null')
|
|
97
|
+
})
|
|
98
|
+
check('countCheckpoints:纯剪枝会话(无全量压缩)也计入', () => {
|
|
99
|
+
const r = countCheckpoints([
|
|
100
|
+
{ type: 'compaction/prune', time: 10, data: {} },
|
|
101
|
+
{ type: 'compaction/prune', time: 20, data: {} },
|
|
102
|
+
])
|
|
103
|
+
assert(r.compactions === 2 && r.fullCompactions === 0 && r.pruneCompactions === 2,
|
|
104
|
+
'prune-only session must count')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
console.log(`risk-matrix.test.mjs — ${pass.length} passed, ${fail.length} failed`)
|
|
108
|
+
for (const label of fail) console.log(` ✗ ${label}`)
|
|
109
|
+
process.exitCode = fail.length ? 1 : 0
|