dsh-session-matrix 0.16.4 → 0.16.6
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/lib/index.js +35 -2
- package/lib/usage-split.js +132 -17
- package/package.json +5 -3
- package/scripts/make-reconcile-fixture.mjs +89 -0
- package/test/fixtures/reconcile.json +3063 -0
- package/test/usage-reconcile.test.mjs +71 -0
- package/test/usage-split.test.mjs +46 -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)]),
|