dsh-session-matrix 0.17.4 → 0.17.5

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/client.js CHANGED
@@ -1534,7 +1534,7 @@ window.__ModuleLoader__.load({
1534
1534
  usage.cacheWrite += s.usage.cacheWrite ?? 0
1535
1535
  usage.output += s.usage.output ?? 0
1536
1536
  }
1537
- if (typeof s.costCny === 'number') {
1537
+ if (Number.isFinite(s.costCny)) {
1538
1538
  costSum += s.costCny
1539
1539
  costSessions += 1
1540
1540
  }
package/lib/index.js CHANGED
@@ -567,15 +567,33 @@ export function costOfBuckets(timeline, lookup, cnyLookup = () => null) {
567
567
  const remote = local ? null : lookup(provider, model)
568
568
  if (!local && !remote) continue
569
569
  if (local) official += 1
570
- for (const [kind, used] of Object.entries(byKind ?? {})) {
571
- if (!used) continue
570
+ for (const [kind, usedRaw] of Object.entries(byKind ?? {})) {
571
+ // A malformed bucket (host projection glitch) must degrade to zero,
572
+ // never NaN — one NaN here poisons the aggregate cost for the whole
573
+ // subtree. The host has never emitted cacheWriteTokens (grepped the
574
+ // full asar): DeepSeek bills a cache write as uncached input, which
575
+ // the uncached bucket already carries.
576
+ if (!usedRaw || typeof usedRaw !== 'object') continue
572
577
  buckets += 1
573
578
  if (kind === 'peak') peakBuckets += 1
574
579
  const factor = kind === 'peak' ? PEAK_FACTOR : 1
580
+ const take = (v) => {
581
+ const n = Number(v)
582
+ return Number.isFinite(n) && n > 0 ? n : 0
583
+ }
584
+ const used = {
585
+ uncached: take(usedRaw.uncached),
586
+ cacheRead: take(usedRaw.cacheRead),
587
+ cacheWrite: take(usedRaw.cacheWrite),
588
+ output: take(usedRaw.output),
589
+ }
575
590
  if (local) {
591
+ // DeepSeek bills a cache write as uncached input — the official table
592
+ // has no separate write row, and the host has never emitted the field.
593
+ // Price it at miss if a future host starts reporting it.
576
594
  cny += factor * (used.uncached * local.miss
577
595
  + used.cacheRead * local.hit
578
- + used.cacheWrite * local.hit
596
+ + used.cacheWrite * local.miss
579
597
  + used.output * local.out) / 1_000_000
580
598
  } else {
581
599
  cny += factor * costOf(used, remote) * CNY_PER_USD
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-matrix",
3
- "version": "0.17.4",
3
+ "version": "0.17.5",
4
4
  "description": "A panoramic console for DeepSeek Harness: run multiple native sessions and subagents in parallel, see the agent graph, get compaction warnings, and hand work off before context is lost.",
5
5
  "keywords": [
6
6
  "deepseek-harness",
@@ -18,11 +18,11 @@
18
18
  "agent-handoff",
19
19
  "handoff-protocol",
20
20
  "agent-network",
21
- "\u591a\u667a\u80fd\u4f53\u5de5\u4f5c\u53f0",
22
- "\u591a\u4f1a\u8bdd\u7ba1\u7406",
23
- "\u4f1a\u8bdd\u77e9\u9635",
24
- "\u4e0a\u4e0b\u6587\u538b\u7f29\u9884\u8b66",
25
- "\u4e0a\u4e0b\u6587\u76d1\u63a7"
21
+ "多智能体工作台",
22
+ "多会话管理",
23
+ "会话矩阵",
24
+ "上下文压缩预警",
25
+ "上下文监控"
26
26
  ],
27
27
  "homepage": "https://github.com/Wanqiu12345/dsh-session-matrix#readme",
28
28
  "repository": {
@@ -75,7 +75,7 @@
75
75
  }
76
76
  },
77
77
  "scripts": {
78
- "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",
78
+ "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 && node test/cost-buckets.test.mjs",
79
79
  "prepublishOnly": "npm test"
80
80
  }
81
81
  }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * costOfBuckets edge cases — malformed host projections must degrade to zero,
3
+ * never NaN, because one NaN poisons the subtree aggregate.
4
+ */
5
+
6
+ import { costOfBuckets } from '../lib/index.js'
7
+
8
+ const pass = []
9
+ const fail = []
10
+ const check = (label, fn) => {
11
+ try { fn(); pass.push(label) } catch (error) { fail.push(`${label}: ${error.message}`) }
12
+ }
13
+ const assert = (cond, message) => { if (!cond) throw new Error(message) }
14
+
15
+ const CNY = () => ({ miss: 1, hit: 0.02, out: 4 })
16
+ const PEAK = { uncached: 1_000_000, cacheRead: 0, cacheWrite: 0, output: 1_000_000 }
17
+ // peak 1M miss ×1 + 1M out ×4 → ¥5;峰时 ×2 → ¥10
18
+
19
+ check('正常桶:峰时 ×2 计费', () => {
20
+ const r = costOfBuckets({ cost: { deepseek: { 'deepseek-flash': { peak: PEAK } } } }, () => null, CNY)
21
+ assert(Math.abs(r.cny - 10) < 1e-9, `peak cost should be ¥10, got ${r.cny}`)
22
+ assert(r.peakBuckets === 1 && r.buckets === 1, 'bucket counts')
23
+ })
24
+
25
+ check('畸形桶(数字/缺失字段):跳过,不产生 NaN', () => {
26
+ const r = costOfBuckets({ cost: { deepseek: { 'deepseek-flash': { peak: 42, off: { uncached: 'x', cacheRead: null } } } } }, () => null, CNY)
27
+ assert(r !== null && Number.isFinite(r.cny), `expected finite, got ${r && r.cny}`)
28
+ })
29
+
30
+ check('桶内负值与非数字字段:按 0 处理', () => {
31
+ const r = costOfBuckets({
32
+ cost: { deepseek: { 'deepseek-flash': { peak: { uncached: -5, cacheRead: 'x', cacheWrite: 'x', output: NaN } } } },
33
+ }, () => null, CNY)
34
+ assert(r.cny === 0, `all-invalid fields must bill ¥0, got ${r.cny}`)
35
+ })
36
+ check('缓存写入按未命中价计(DeepSeek 无独立写入价)', () => {
37
+ const r = costOfBuckets({
38
+ cost: { deepseek: { 'deepseek-flash': { peak: { uncached: 0, cacheRead: 0, cacheWrite: 1_000_000, output: 0 } } } },
39
+ }, () => null, CNY)
40
+ assert(Math.abs(r.cny - 2) < 1e-9, `1M write ×1 ×2 = ¥2, got ${r.cny}`)
41
+ })
42
+
43
+ check('非对象桶整体跳过', () => {
44
+ const r = costOfBuckets({ cost: { deepseek: { 'deepseek-flash': { peak: null, off: 7 } } } }, () => null, CNY)
45
+ assert(r === null, 'no valid buckets → null')
46
+ })
47
+
48
+ check('缓存命中按命中价计', () => {
49
+ const r = costOfBuckets({
50
+ cost: { deepseek: { 'deepseek-flash': { peak: { uncached: 0, cacheRead: 1_000_000, output: 0 } } } },
51
+ }, () => null, CNY)
52
+ assert(Math.abs(r.cny - 0.04) < 1e-9, `1M hit ×0.02 ×2 = ¥0.04, got ${r.cny}`)
53
+ })
54
+
55
+ console.log(`cost-buckets.test.mjs — ${pass.length} passed, ${fail.length} failed`)
56
+ for (const label of fail) console.log(` ✗ ${label}`)
57
+ process.exitCode = fail.length ? 1 : 0