dsh-session-matrix 0.17.3 → 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/README.md CHANGED
@@ -168,7 +168,7 @@ DSH 每次压缩都会在会话里留下一个检查点(`compact-checkpoint`
168
168
  | V4.1 Flash `deepseek-flash`(旧名 `deepseek-v4-flash`) | 0.02 | 1 | 4 |
169
169
  | V4 Pro `deepseek-v4-pro` | 0.15 | 4.5 | 13.5 |
170
170
 
171
- 峰时(北京时间工作日 09:00–12:00、14:00–18:00)**×2**。法定节假日暂未建模,仍按工作日峰时计——见下方 Requirements 说明。
171
+ 峰时(北京时间周一至周五 09:00–12:00、14:00–18:00)**×2**。法定节假日全天按谷时计(内置国务院 2026 年放假安排,含调休上班的周末按工作日计);2027 年安排公布后需更新内置表。
172
172
  `contextTimeline.cost` 已按峰谷分桶,直接分桶计算,不做时间推断。
173
173
  非 `deepseek` provider 回退 models.dev 美元价,按 `1 CNY = 0.15 USD` 折算。
174
174
 
@@ -263,7 +263,7 @@ connection.fetch.register({ path: '/api/session-matrix/overview', methods: ['GET
263
263
 
264
264
  - 仅 DeepSeek 模型按人民币计价;其他 provider 走 models.dev 美元价。
265
265
  - 子代理用量按会话独立统计,不并入父会话(脉络里选中主会话时,模块按子树汇总)。
266
- - 法定节假日未建模,峰谷桶由宿主提供。
266
+ - 法定节假日按 2026 年国务院安排内置(节假日全天谷时,调休上班周末计峰时)。
267
267
  - 归档集合读自 `storages/workspace.json`;DSH 若调整该表结构,需同步修改读取逻辑。
268
268
  - macOS / Linux 未在真机验证。代码里没有平台分支,但"没写 `if`"不等于"测过"。
269
269
 
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
@@ -31,11 +31,45 @@ const BEIJING_OFFSET_MS = 8 * 60 * 60 * 1000
31
31
  * @param epochMs - event timestamp.
32
32
  * @returns true when the request bills at the peak rate.
33
33
  */
34
+ /**
35
+ * DeepSeek's own rule (pricing page, confirmed 2026-11): peak is Mon–Fri
36
+ * 09:00–12:00 and 14:00–18:00 Beijing time **excluding Chinese public
37
+ * holidays** — holidays bill off-peak all day, and weekend days swapped to
38
+ * workdays by the same council notice bill as weekdays.
39
+ *
40
+ * Both tables come from the State Council notice for 2026 (gov.cn, 2025-11-04)
41
+ * and cover that calendar year. When the 2027 notice publishes, extend them.
42
+ */
43
+ const HOLIDAYS_2026 = new Set([
44
+ '2026-01-01', '2026-01-02', '2026-01-03',
45
+ ...range('2026-02-15', 9),
46
+ '2026-04-04', '2026-04-05', '2026-04-06',
47
+ ...range('2026-05-01', 5),
48
+ '2026-06-19', '2026-06-20', '2026-06-21',
49
+ '2026-09-25', '2026-09-26', '2026-09-27',
50
+ ...range('2026-10-01', 7),
51
+ ])
52
+ /** 周六/周日调休上班日 — these weekends bill as weekdays. */
53
+ const WORKDAY_WEEKENDS_2026 = new Set([
54
+ '2026-01-04', '2026-02-14', '2026-02-28', '2026-05-09', '2026-09-20', '2026-10-10',
55
+ ])
56
+
57
+ function range(startIso, days) {
58
+ const list = []
59
+ const t = Date.parse(startIso + 'T00:00:00Z')
60
+ for (let i = 0; i < days; i++) list.push(new Date(t + i * 86_400_000).toISOString().slice(0, 10))
61
+ return list
62
+ }
63
+
34
64
  export function isPeakTime(epochMs) {
35
65
  if (!Number.isFinite(epochMs)) return false
36
66
  const shifted = new Date(epochMs + BEIJING_OFFSET_MS)
67
+ const date = shifted.toISOString().slice(0, 10)
68
+ if (HOLIDAYS_2026.has(date)) return false // holidays bill off-peak all day
37
69
  const weekday = shifted.getUTCDay()
38
- if (weekday === 0 || weekday === 6) return false
70
+ if (weekday === 0 || weekday === 6) {
71
+ if (!WORKDAY_WEEKENDS_2026.has(date)) return false
72
+ }
39
73
  const hour = shifted.getUTCHours()
40
74
  return (hour >= 9 && hour < 12) || (hour >= 14 && hour < 18)
41
75
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-matrix",
3
- "version": "0.17.3",
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",
@@ -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
@@ -33,6 +33,20 @@ const assert = (cond, message) => {
33
33
  const beijing = (y, m, d, h, min = 0, sec = 0) => Date.UTC(y, m - 1, d, h - 8, min, sec)
34
34
 
35
35
  // 2026-11-16 is a Monday, 2026-11-14 a Saturday.
36
+ check('法定节假日全天按谷时(DeepSeek 官方规则,2026 国务院安排)', () => {
37
+ assert(isPeakTime(beijing(2026, 1, 1, 10)) === false, '元旦周四 10:00 应为谷时')
38
+ assert(isPeakTime(beijing(2026, 2, 18, 10)) === false, '春节周三 10:00 应为谷时')
39
+ assert(isPeakTime(beijing(2026, 10, 6, 10)) === false, '国庆周二 10:00 应为谷时')
40
+ assert(isPeakTime(beijing(2026, 9, 25, 15)) === false, '中秋周五 15:00 应为谷时')
41
+ })
42
+ check('调休上班的周末按工作日计峰时', () => {
43
+ assert(isPeakTime(beijing(2026, 2, 14, 10)) === true, '春节调休周六 10:00 应为峰时')
44
+ assert(isPeakTime(beijing(2026, 10, 10, 15)) === true, '国庆调休周六 15:00 应为峰时')
45
+ })
46
+ check('普通周末与工作日不受日历影响', () => {
47
+ assert(isPeakTime(beijing(2026, 3, 7, 10)) === false, '普通周六 10:00 谷时')
48
+ assert(isPeakTime(beijing(2026, 11, 19, 10)) === true, '普通周四 10:00 峰时')
49
+ })
36
50
  check('peak windows are 09:00–12:00 and 14:00–18:00 on weekdays', () => {
37
51
  assert(isPeakTime(beijing(2026, 11, 16, 8, 59)) === false, '08:59 should be off-peak')
38
52
  assert(isPeakTime(beijing(2026, 11, 16, 9, 0)) === true, '09:00 should be peak')