dsh-session-matrix 0.16.5 → 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.
@@ -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
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Reconciliation fixture test — runs everywhere, including CI.
3
+ *
4
+ * `usage-vs-projection.test.mjs` compares against whatever DSH data exists on
5
+ * the machine, so it skips on a clean runner. That left CI green without ever
6
+ * exercising the reconciliation. This fixture is a stripped copy of four real
7
+ * sessions (event type, time, usage payload only — no text, no titles, no tool
8
+ * arguments) together with the totals DSH itself computed for them, so the same
9
+ * comparison runs on every machine.
10
+ *
11
+ * Regenerate with: node scripts/make-reconcile-fixture.mjs (needs local DSH data).
12
+ */
13
+
14
+ import { readFileSync } from 'node:fs'
15
+ import { fileURLToPath } from 'node:url'
16
+ import { dirname, join } from 'node:path'
17
+ import { splitUsageEvents } from '../lib/usage-split.js'
18
+
19
+ const here = dirname(fileURLToPath(import.meta.url))
20
+ const fixture = JSON.parse(readFileSync(join(here, 'fixtures', 'reconcile.json'), 'utf8'))
21
+
22
+ const pass = []
23
+ const fail = []
24
+ const check = (label, fn) => {
25
+ try { fn(); pass.push(label) } catch (error) { fail.push(`${label}: ${error.message}`) }
26
+ }
27
+ const assert = (cond, message) => { if (!cond) throw new Error(message) }
28
+
29
+ check('the fixture is present and non-trivial', () => {
30
+ assert(Array.isArray(fixture.samples) && fixture.samples.length > 0, 'no samples')
31
+ const events = fixture.samples.reduce((n, s) => n + s.events.length, 0)
32
+ assert(events >= 20, `only ${events} events`)
33
+ })
34
+
35
+ for (const sample of fixture.samples) {
36
+ check(`${sample.id} reproduces DSH's totals exactly`, () => {
37
+ const split = splitUsageEvents(sample.events)
38
+ assert(split, 'parser returned nothing for a sample that has usage')
39
+ const got = {
40
+ uncachedInputTokens: split.peak.uncached + split.off.uncached,
41
+ outputTokens: split.peak.output + split.off.output,
42
+ cacheReadTokens: split.peak.cacheRead + split.off.cacheRead,
43
+ cacheWriteTokens: split.peak.cacheWrite + split.off.cacheWrite,
44
+ requests: split.requests,
45
+ }
46
+ for (const [key, want] of Object.entries(sample.expected)) {
47
+ assert(got[key] === want, `${key}: parser ${got[key]} vs DSH ${want}`)
48
+ }
49
+ })
50
+
51
+ check(`${sample.id} splits across the peak/off boundary`, () => {
52
+ // A sample that lands entirely in one bucket would not exercise the time
53
+ // classification at all.
54
+ assert(sample.checks.length > 0, 'sample has no usage')
55
+ const split = splitUsageEvents(sample.events)
56
+ const total = split.peak.uncached + split.off.uncached
57
+ assert(total === sample.expected.uncachedInputTokens,
58
+ `peak ${split.peak.uncached} + off ${split.off.uncached} != ${sample.expected.uncachedInputTokens}`)
59
+ })
60
+ }
61
+
62
+ check('the fixture carries no conversation text', () => {
63
+ const raw = JSON.stringify(fixture)
64
+ for (const word of ['"text"', '"content"', '"title"', '"prompt"']) {
65
+ assert(!raw.includes(word), `fixture contains ${word}`)
66
+ }
67
+ })
68
+
69
+ console.log(`usage-reconcile.test.mjs — ${pass.length} passed, ${fail.length} failed`)
70
+ for (const label of fail) console.log(` ✗ ${label}`)
71
+ process.exitCode = fail.length ? 1 : 0
@@ -82,6 +82,52 @@ check('multiple concatenated frames are all decompressed', () => {
82
82
  assert(split.requests === 2, `requests ${split.requests}`)
83
83
  })
84
84
 
85
+ /**
86
+ * Hand-build a single-frame zstd stream with one raw block.
87
+ *
88
+ * Raw blocks let a test put arbitrary bytes — including the frame magic — inside
89
+ * a frame body, which a compressor would only produce by accident.
90
+ * @param payload - bytes to store verbatim.
91
+ * @param declaredSize - override the frame content size field.
92
+ * @returns a complete frame.
93
+ */
94
+ const rawFrame = (payload, declaredSize = payload.length) => {
95
+ const body = Buffer.from(payload)
96
+ const out = [Buffer.from([0x28, 0xb5, 0x2f, 0xfd])]
97
+ // descriptor: single-segment (0x20), no checksum, no dict, fcs flag 0 → 1 byte
98
+ out.push(Buffer.from([0x20, declaredSize & 0xff]))
99
+ const blockHeader = Buffer.alloc(3)
100
+ const h = 1 | (0 << 1) | (body.length << 3)
101
+ blockHeader[0] = h & 0xff
102
+ blockHeader[1] = (h >>> 8) & 0xff
103
+ blockHeader[2] = (h >>> 16) & 0xff
104
+ out.push(blockHeader, body)
105
+ return Buffer.concat(out)
106
+ }
107
+
108
+ check('a magic inside a block body is not a frame boundary', () => {
109
+ // The payload carries the four magic bytes; a scanner that searched for magic
110
+ // would cut the frame in half here.
111
+ const payload = Buffer.concat([
112
+ Buffer.from('{"type":"note","text":"'),
113
+ Buffer.from([0x28, 0xb5, 0x2f, 0xfd]),
114
+ Buffer.from('"}\n'),
115
+ ])
116
+ const frame = rawFrame(payload)
117
+ const text = decompressFrames(frame, decode)
118
+ assert(text.length === payload.length, 'expected the whole payload, got ' + text.length + ' bytes')
119
+ const twice = decompressFrames(Buffer.concat([frame, frame]), decode)
120
+ assert(twice.length === payload.length * 2, 'two such frames must both decode')
121
+ })
122
+
123
+ check('a frame whose declared size disagrees with its output is dropped', () => {
124
+ // Declares 4 bytes, carries 20: the writer and the bytes disagree, so the
125
+ // frame cannot be trusted even though it decodes.
126
+ const frame = rawFrame(Buffer.from('{"a":1}\n{"b":2}\n'), 4)
127
+ const text = decompressFrames(frame, decode)
128
+ assert(text === '', 'expected nothing, got ' + JSON.stringify(text))
129
+ })
130
+
85
131
  check('a truncated tail keeps what decoded', () => {
86
132
  const whole = Buffer.concat([
87
133
  frame([usageEvent(beijing(2026, 11, 16, 10, 0), 50, 5)]),