@twinsearth/dsh-master 3.2.10 → 3.3.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.
Files changed (104) hide show
  1. package/README.md +4 -0
  2. package/client.js +50 -0
  3. package/dist-host/contract/client.js +7 -0
  4. package/dist-host/contract/constants.js +16 -2
  5. package/dist-host/contract/index.js +1 -0
  6. package/dist-host/contract/relay.js +239 -0
  7. package/dist-host/cost/attribution.js +183 -0
  8. package/dist-host/cost/audit.js +163 -0
  9. package/dist-host/cost/budget.js +211 -0
  10. package/dist-host/cost/bus.js +95 -0
  11. package/dist-host/cost/cert-chain.js +100 -0
  12. package/dist-host/cost/experiment.js +132 -0
  13. package/dist-host/cost/fang-relay.js +329 -0
  14. package/dist-host/cost/gate.js +189 -0
  15. package/dist-host/cost/host-surface.js +80 -0
  16. package/dist-host/cost/index.js +345 -0
  17. package/dist-host/cost/ledger-report.js +193 -0
  18. package/dist-host/cost/ledger.js +401 -0
  19. package/dist-host/cost/metrics.js +198 -0
  20. package/dist-host/cost/milestones.js +209 -0
  21. package/dist-host/cost/modules/index.js +5 -0
  22. package/dist-host/cost/modules/manifests.js +130 -0
  23. package/dist-host/cost/policy.js +276 -0
  24. package/dist-host/cost/report.js +156 -0
  25. package/dist-host/cost/reuse-report.js +128 -0
  26. package/dist-host/cost/rsi.js +367 -0
  27. package/dist-host/cost/toolface.js +147 -0
  28. package/dist-host/cost/types.js +31 -0
  29. package/dist-host/kernel.js +28 -2
  30. package/dist-host/plugins/cost-optimizer.js +329 -27
  31. package/dist-host/plugins/superagent-router.js +104 -1
  32. package/dist-host/superagent/types.js +8 -0
  33. package/dist-worker/agent-loop.js +449 -7
  34. package/dist-worker/baseline-fit.js +66 -0
  35. package/dist-worker/budget-guard.js +93 -0
  36. package/dist-worker/ccs.js +99 -0
  37. package/dist-worker/cost-hooks.js +115 -0
  38. package/dist-worker/cost-modules.js +330 -0
  39. package/dist-worker/cost-spi.js +3 -0
  40. package/dist-worker/edg.js +372 -0
  41. package/dist-worker/gdr.js +159 -0
  42. package/dist-worker/index.js +80 -2
  43. package/dist-worker/llm-http.js +20 -0
  44. package/dist-worker/sink-http.js +1 -0
  45. package/dist-worker/spill.js +115 -0
  46. package/dist-worker/tool-batch.js +66 -0
  47. package/dist-worker/tool-compact.js +275 -0
  48. package/dist-worker/tool-disclosure.js +90 -0
  49. package/dist-worker/tools.js +16 -0
  50. package/docs/ARCHITECTURE.md +24 -0
  51. package/docs/CHANGELOG.md +66 -0
  52. package/docs/DESIGN-v3.3.0-cost-optimization.md +1058 -0
  53. package/docs/EXPERIMENT-M5-fang-relay.md +117 -0
  54. package/docs/EXPERIMENT-M5-real-ab.md +87 -0
  55. package/docs/THIRD-PARTY.md +87 -0
  56. package/package.json +2 -2
  57. package/src/contract/client.js +7 -0
  58. package/src/contract/constants.js +17 -2
  59. package/src/contract/index.js +1 -0
  60. package/src/contract/relay.js +223 -0
  61. package/src/cost/attribution.ts +186 -0
  62. package/src/cost/audit.ts +170 -0
  63. package/src/cost/budget.ts +226 -0
  64. package/src/cost/bus.ts +97 -0
  65. package/src/cost/cert-chain.ts +102 -0
  66. package/src/cost/experiment.ts +136 -0
  67. package/src/cost/fang-relay.ts +318 -0
  68. package/src/cost/gate.ts +191 -0
  69. package/src/cost/host-surface.ts +97 -0
  70. package/src/cost/index.ts +348 -0
  71. package/src/cost/ledger-report.ts +186 -0
  72. package/src/cost/ledger.ts +385 -0
  73. package/src/cost/metrics.ts +193 -0
  74. package/src/cost/milestones.ts +206 -0
  75. package/src/cost/modules/index.ts +13 -0
  76. package/src/cost/modules/manifests.ts +129 -0
  77. package/src/cost/policy.ts +276 -0
  78. package/src/cost/report.ts +158 -0
  79. package/src/cost/reuse-report.ts +131 -0
  80. package/src/cost/rsi.ts +364 -0
  81. package/src/cost/toolface.ts +151 -0
  82. package/src/cost/types.ts +298 -0
  83. package/src/kernel.ts +27 -2
  84. package/src/plugins/cost-optimizer.ts +313 -26
  85. package/src/plugins/superagent-router.ts +96 -1
  86. package/src/superagent/types.ts +70 -0
  87. package/worker/agent-loop.ts +444 -7
  88. package/worker/baseline-fit.ts +80 -0
  89. package/worker/budget-guard.ts +156 -0
  90. package/worker/ccs.ts +202 -0
  91. package/worker/cost-hooks.ts +133 -0
  92. package/worker/cost-modules.ts +356 -0
  93. package/worker/cost-spi.ts +283 -0
  94. package/worker/edg.ts +424 -0
  95. package/worker/gdr.ts +233 -0
  96. package/worker/index.ts +84 -3
  97. package/worker/llm-http.ts +18 -0
  98. package/worker/sink-http.ts +5 -2
  99. package/worker/spill.ts +133 -0
  100. package/worker/tool-batch.ts +124 -0
  101. package/worker/tool-compact.ts +317 -0
  102. package/worker/tool-disclosure.ts +131 -0
  103. package/worker/tools.ts +553 -531
  104. package/worker/types.ts +191 -0
@@ -0,0 +1,186 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Master contributors — see LICENSE-CORE.
3
+ // @ts-nocheck — host JS→TS 迁移阶段:运行时行为优先,类型注解逐步补齐(worker/ 仍严格检查)。
4
+
5
+ /**
6
+ * 3.3.0 系统层 · attribution(归因,设计 §5.4)。
7
+ *
8
+ * 三条硬规则:
9
+ * 1. **每条 savedTokens 必须带 moduleId**(缺失 ⇒ `module-missing`,不猜、不默认);
10
+ * 2. 差额进 `unattributed`,报表**单列**——未归因收益不得算作任何模块收益;
11
+ * 3. 归因算法(工具面):`baselineOutputTokens(toolName, 长度) − actualOutputTokens`,
12
+ * baseline 由 A1 固定拟合式给出(`worker/baseline-fit.ts`,参数在 docs §5.4.1 冻结)。
13
+ *
14
+ * 另守 §5.3 口径:`shadow` 只累计「本可节省」,`enforce` 才写入 execution 账本且
15
+ * `totalTokens=0`(不重复计入已消耗 token)。
16
+ */
17
+
18
+ import { COST_MODULES } from './types.ts'
19
+
20
+ function finiteNonNegative(value) {
21
+ const n = Number(value)
22
+ return Number.isFinite(n) && n >= 0 ? Math.floor(n) : null
23
+ }
24
+
25
+ /**
26
+ * @param {{ledger: {append: Function}, now?: () => number}} options
27
+ */
28
+ export function createAttribution(options = {}) {
29
+ const ledger = options.ledger
30
+ if (!ledger || typeof ledger.append !== 'function') throw new Error('attribution requires a ledger')
31
+ const now = typeof options.now === 'function' ? options.now : () => Date.now()
32
+ const allowed = new Set(COST_MODULES)
33
+ const modules = new Map()
34
+ const tasks = new Map()
35
+
36
+ const moduleRow = (module) => {
37
+ if (!modules.has(module)) modules.set(module, { module, events: 0, projectedTokens: 0, enforceSavedTokens: 0 })
38
+ return modules.get(module)
39
+ }
40
+ const taskRow = (taskId, module) => {
41
+ const key = `${taskId ?? ''}|${module}`
42
+ if (!tasks.has(key)) tasks.set(key, { key, taskId: taskId === undefined ? null : String(taskId), module, events: 0, projectedTokens: 0, enforceSavedTokens: 0 })
43
+ return tasks.get(key)
44
+ }
45
+
46
+ /**
47
+ * @param {{taskId?: string, module: string, savedTokens: number, mode?: string, meta?: object,
48
+ * forkId?: string, sessionId?: string}} input
49
+ */
50
+ function attribute(input = {}) {
51
+ const saved = finiteNonNegative(input.savedTokens)
52
+ if (saved === null) return { ok: false, code: 'saved-tokens-invalid', message: 'savedTokens must be a non-negative number' }
53
+ // §5.4-1:moduleId 必填,缺失不猜默认值。
54
+ if (input.module === undefined || input.module === null || String(input.module).trim() === '') {
55
+ return { ok: false, code: 'module-missing', message: 'every savedTokens must carry a moduleId' }
56
+ }
57
+ const module = String(input.module)
58
+ if (!allowed.has(module)) return { ok: false, code: 'module-invalid', message: `module must be one of ${COST_MODULES.join('/')}` }
59
+ const enforce = String(input.mode ?? 'shadow') === 'enforce'
60
+ const row = moduleRow(module)
61
+ const perTask = taskRow(input.taskId, module)
62
+
63
+ if (!enforce) {
64
+ row.events += 1
65
+ row.projectedTokens += saved
66
+ perTask.events += 1
67
+ perTask.projectedTokens += saved
68
+ return {
69
+ ok: true,
70
+ mode: 'shadow',
71
+ module,
72
+ projectedTokens: saved,
73
+ shadow: { events: row.events, projectedTokens: row.projectedTokens, appliedTokens: row.enforceSavedTokens },
74
+ at: now(),
75
+ }
76
+ }
77
+
78
+ const appended = ledger.append({
79
+ account: 'execution',
80
+ module,
81
+ source: 'tool',
82
+ phase: 'tool',
83
+ taskId: input.taskId === undefined ? null : String(input.taskId),
84
+ forkId: input.forkId,
85
+ sessionId: input.sessionId,
86
+ step: input.step,
87
+ turn: input.turn,
88
+ totalTokens: 0,
89
+ savedTokens: saved,
90
+ meta: { kind: 'attribution', ...(input.meta && typeof input.meta === 'object' ? input.meta : {}), mode: 'enforce' },
91
+ })
92
+ if (!appended.ok) return { ok: false, code: appended.code, message: appended.message }
93
+ row.events += 1
94
+ row.enforceSavedTokens += saved
95
+ perTask.events += 1
96
+ perTask.enforceSavedTokens += saved
97
+ return {
98
+ ok: true,
99
+ mode: 'enforce',
100
+ module,
101
+ savedTokens: saved,
102
+ ledger: { id: appended.entry.id },
103
+ shadow: { events: row.events, projectedTokens: row.projectedTokens, appliedTokens: row.enforceSavedTokens },
104
+ at: now(),
105
+ }
106
+ }
107
+
108
+ /**
109
+ * §5.4-2:按「总额 − 各模块之和」结算,差额自动进 `unattributed`。
110
+ * 任何未提供 moduleId 的条目都会被拒绝(模块收益不得凭空出现)。
111
+ *
112
+ * @param {{taskId?: string, totalSavedTokens: number, items?: Array<{module: string, savedTokens: number}>,
113
+ * mode?: string, forkId?: string, sessionId?: string}} input
114
+ */
115
+ function attributeTotal(input = {}) {
116
+ const total = finiteNonNegative(input.totalSavedTokens)
117
+ if (total === null) return { ok: false, code: 'saved-tokens-invalid', message: 'totalSavedTokens must be a non-negative number' }
118
+ const items = Array.isArray(input.items) ? input.items : []
119
+ let attributed = 0
120
+ const applied = []
121
+ for (const item of items) {
122
+ const out = attribute({
123
+ taskId: input.taskId,
124
+ module: item?.module,
125
+ savedTokens: item?.savedTokens,
126
+ mode: input.mode,
127
+ forkId: input.forkId,
128
+ sessionId: input.sessionId,
129
+ meta: { kind: 'module-share' },
130
+ })
131
+ if (!out.ok) return out
132
+ attributed += finiteNonNegative(item?.savedTokens) ?? 0
133
+ applied.push({ module: String(item.module), savedTokens: finiteNonNegative(item?.savedTokens) ?? 0 })
134
+ }
135
+ if (attributed > total) {
136
+ return { ok: false, code: 'attribution-overflow', message: `attributed ${attributed} exceeds total ${total}` }
137
+ }
138
+ const residual = total - attributed
139
+ if (residual > 0) {
140
+ const out = attribute({
141
+ taskId: input.taskId,
142
+ module: 'unattributed',
143
+ savedTokens: residual,
144
+ mode: input.mode,
145
+ forkId: input.forkId,
146
+ sessionId: input.sessionId,
147
+ meta: { kind: 'residual' },
148
+ })
149
+ if (!out.ok) return out
150
+ }
151
+ return { ok: true, totalSavedTokens: total, attributedTokens: attributed, unattributedTokens: residual, items: applied, at: now() }
152
+ }
153
+
154
+ function snapshot() {
155
+ const moduleRows = [...modules.values()]
156
+ .map((item) => Object.freeze({ ...item }))
157
+ .sort((a, b) => b.projectedTokens + b.enforceSavedTokens - (a.projectedTokens + a.enforceSavedTokens))
158
+ const taskRows = [...tasks.values()]
159
+ .map((item) => Object.freeze({ ...item }))
160
+ .sort((a, b) => b.projectedTokens + b.enforceSavedTokens - (a.projectedTokens + a.enforceSavedTokens))
161
+ const named = moduleRows.filter((row) => row.module !== 'unattributed')
162
+ const unattributed = moduleRows.filter((row) => row.module === 'unattributed')
163
+ const sum = (rows, key) => rows.reduce((total, row) => total + row[key], 0)
164
+ return Object.freeze({
165
+ ok: true,
166
+ modules: moduleRows,
167
+ tasks: taskRows,
168
+ /** 模块收益(不含 unattributed)——报表只能把这一项当「模块业绩」。 */
169
+ attributedSavedTotal: sum(named, 'enforceSavedTokens'),
170
+ attributedProjectedTotal: sum(named, 'projectedTokens'),
171
+ /** 未归因收益:单列,永不并入上面的模块业绩。 */
172
+ unattributedSavedTotal: sum(unattributed, 'enforceSavedTokens'),
173
+ unattributedProjectedTotal: sum(unattributed, 'projectedTokens'),
174
+ projectedTotal: sum(moduleRows, 'projectedTokens'),
175
+ enforceSavedTotal: sum(moduleRows, 'enforceSavedTokens'),
176
+ })
177
+ }
178
+
179
+ function reset() {
180
+ modules.clear()
181
+ tasks.clear()
182
+ return { ok: true }
183
+ }
184
+
185
+ return Object.freeze({ attribute, attributeTotal, snapshot, reset })
186
+ }
@@ -0,0 +1,170 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Master contributors — see LICENSE-CORE.
3
+ // @ts-nocheck — host JS→TS 迁移阶段:运行时行为优先,类型注解逐步补齐(worker/ 仍严格检查)。
4
+
5
+ /**
6
+ * 3.3.0 系统层 · audit(哈希链审计,设计 §5.5)。
7
+ *
8
+ * 结构约束:
9
+ * * **只追加**:没有 update/delete/rewrite API,记录 `Object.freeze`;
10
+ * * 每条记录含 `prevHash` 与自身 `hash`,任何篡改或截断都会在 `verify()` 断链;
11
+ * * 每次替换写一条 `SubstitutionCert`(edgeId / toolName / schemaHash / guardExpr /
12
+ * receiptDigest / replacedCallbackId / viaNativeDispatcher: true),
13
+ * 并与 ledger 事件用同一 `taskId + step` 关联 —— 报表可回放「这一省是怎么来的」。
14
+ */
15
+
16
+ import crypto from 'node:crypto'
17
+
18
+ /** SubstitutionCert 必填字段;缺任何一项都不得落链(否则证书无法复核)。 */
19
+ const CERT_REQUIRED = Object.freeze([
20
+ 'edgeId',
21
+ 'toolName',
22
+ 'schemaHash',
23
+ 'guardExpr',
24
+ 'receiptDigest',
25
+ 'replacedCallbackId',
26
+ 'taskId',
27
+ ])
28
+
29
+ function canonical(value) {
30
+ if (Array.isArray(value)) return `[${value.map((item) => canonical(item)).join(',')}]`
31
+ if (value && typeof value === 'object') {
32
+ const keys = Object.keys(value).sort()
33
+ return `{${keys.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(',')}}`
34
+ }
35
+ if (typeof value === 'number') return Number.isFinite(value) ? String(value) : 'null'
36
+ if (value === undefined) return 'null'
37
+ return JSON.stringify(value) ?? 'null'
38
+ }
39
+
40
+ function hashOf(seq, at, type, actor, payload, prevHash) {
41
+ const body = canonical([seq, at, type, actor, payload, prevHash])
42
+ return `sha256:${crypto.createHash('sha256').update(body).digest('hex')}`
43
+ }
44
+
45
+ /**
46
+ * @param {{now?: () => number, maxRecords?: number}} [options]
47
+ */
48
+ export function createAuditChain(options = {}) {
49
+ const now = typeof options.now === 'function' ? options.now : () => Date.now()
50
+ const maxRecords = Math.max(1, Number(options.maxRecords ?? 2000) || 2000)
51
+ const records = []
52
+ let seq = 0
53
+ let dropped = 0
54
+
55
+ function append({ type, actor = 'cost-system', payload = {} } = {}) {
56
+ seq += 1
57
+ const at = now()
58
+ const prevHash = records.length > 0 ? records[records.length - 1].hash : ''
59
+ const record = Object.freeze({
60
+ seq,
61
+ at,
62
+ type: String(type ?? 'unknown'),
63
+ actor: String(actor ?? 'cost-system'),
64
+ payload: Object.freeze({ ...payload }),
65
+ prevHash,
66
+ hash: hashOf(seq, at, String(type ?? 'unknown'), String(actor ?? 'cost-system'), payload, prevHash),
67
+ })
68
+ records.push(record)
69
+ if (records.length > maxRecords) {
70
+ records.shift()
71
+ dropped += 1
72
+ }
73
+ return record
74
+ }
75
+
76
+ /**
77
+ * 写一条 SubstitutionCert(§5.5)。fail-closed:
78
+ * * 必填字段缺失/空白 ⇒ 拒绝;
79
+ * * `viaNativeDispatcher` 必须严格为 `true`(非原生派发不得替换);
80
+ * * `taskId + step` 是回放键,step 必须是非负整数。
81
+ */
82
+ function appendSubstitution(cert = {}) {
83
+ const missing = CERT_REQUIRED.filter((key) => {
84
+ const value = cert?.[key]
85
+ return value === undefined || value === null || String(value).trim() === ''
86
+ })
87
+ if (missing.length > 0) {
88
+ return { ok: false, code: 'cert-invalid', message: `substitution cert missing ${missing.join(',')}`, missing }
89
+ }
90
+ if (cert.viaNativeDispatcher !== true) {
91
+ return { ok: false, code: 'cert-not-native', message: 'substitution must be executed via the native dispatcher' }
92
+ }
93
+ const step = Number(cert.step)
94
+ if (!Number.isInteger(step) || step < 0) {
95
+ return { ok: false, code: 'cert-step-invalid', message: 'step must be a non-negative integer' }
96
+ }
97
+ const savedTokens = Number(cert.savedTokens ?? 0)
98
+ if (!Number.isFinite(savedTokens) || savedTokens < 0) {
99
+ return { ok: false, code: 'cert-saved-invalid', message: 'savedTokens must be a non-negative number' }
100
+ }
101
+ const payload = Object.freeze({
102
+ kind: 'substitution-cert',
103
+ edgeId: String(cert.edgeId),
104
+ toolName: String(cert.toolName),
105
+ schemaHash: String(cert.schemaHash),
106
+ guardExpr: String(cert.guardExpr),
107
+ receiptDigest: String(cert.receiptDigest),
108
+ replacedCallbackId: String(cert.replacedCallbackId),
109
+ /** M4:落链即代表「已经过原生派发器真的派发」;证书链核对直接读这一位。 */
110
+ dispatched: true,
111
+ viaNativeDispatcher: true,
112
+ taskId: String(cert.taskId),
113
+ step,
114
+ forkId: cert.forkId === undefined ? null : String(cert.forkId),
115
+ sessionId: cert.sessionId === undefined ? null : String(cert.sessionId),
116
+ ticketId: cert.ticketId === undefined ? null : String(cert.ticketId),
117
+ moduleId: cert.moduleId === undefined ? null : String(cert.moduleId),
118
+ savedTokens,
119
+ })
120
+ const record = append({ type: 'cost.substitution', actor: 'cost-ccs', payload })
121
+ return { ok: true, cert: payload, record }
122
+ }
123
+
124
+ function verify(source) {
125
+ const list = Array.isArray(source) ? source : records
126
+ let prevHash = ''
127
+ let expectedSeq = 1
128
+ for (const record of list) {
129
+ if (record.seq !== expectedSeq) return { ok: false, count: list.length, brokenAt: record.seq, reason: 'seq-gap' }
130
+ if (record.prevHash !== prevHash) return { ok: false, count: list.length, brokenAt: record.seq, reason: 'prev-hash-mismatch' }
131
+ const expected = hashOf(record.seq, record.at, record.type, record.actor, record.payload, record.prevHash)
132
+ if (record.hash !== expected) return { ok: false, count: list.length, brokenAt: record.seq, reason: 'hash-mismatch' }
133
+ prevHash = record.hash
134
+ expectedSeq += 1
135
+ }
136
+ return { ok: true, count: list.length, brokenAt: null, reason: null }
137
+ }
138
+
139
+ function snapshot(limit = 50) {
140
+ const n = Math.max(1, Number(limit) || 50)
141
+ return Object.freeze({
142
+ ok: true,
143
+ count: records.length,
144
+ dropped,
145
+ verified: verify(),
146
+ records: records.slice(-n),
147
+ })
148
+ }
149
+
150
+ function reset() {
151
+ records.length = 0
152
+ seq = 0
153
+ dropped = 0
154
+ return { ok: true }
155
+ }
156
+
157
+ /** 取某任务/步骤的证书(回放用;不改链)。 */
158
+ function substitutions(filter = {}) {
159
+ const taskId = filter.taskId === undefined ? null : String(filter.taskId)
160
+ const step = filter.step === undefined ? null : Number(filter.step)
161
+ const rows = records
162
+ .filter((record) => record.type === 'cost.substitution')
163
+ .filter((record) => (taskId === null ? true : record.payload.taskId === taskId))
164
+ .filter((record) => (step === null ? true : record.payload.step === step))
165
+ return Object.freeze(rows.map((record) => Object.freeze({ ...record.payload, seq: record.seq, at: record.at, hash: record.hash })))
166
+ }
167
+
168
+ // 只追加:对外**不暴露**任何修改/删除入口(records() 返回副本)。
169
+ return Object.freeze({ append, appendSubstitution, verify, snapshot, reset, records: () => records.slice(), substitutions })
170
+ }
@@ -0,0 +1,226 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Master contributors — see LICENSE-CORE.
3
+ // @ts-nocheck — host JS→TS 迁移阶段:运行时行为优先,类型注解逐步补齐(worker/ 仍严格检查)。
4
+
5
+ /**
6
+ * 3.3.0 系统层 · budget(预算闸,设计 §5.2)。
7
+ *
8
+ * 规格要点:
9
+ * * 规则:`unit(task|fork|day) + limitTokens? + limitCost? + onExhaust(hard-stop|degrade)`;
10
+ * * **检查点只在 ① beforeModelCall**(`authorize()` 即该检查点,其它地方不得自行扣减);
11
+ * * 超限返回 `masterError('budget-exhausted', …)` 形状 + 结构化 `remaining`;
12
+ * * **fail-closed**:额度不足不得「先跑着看」——拒绝时**不扣额度**、不放行;
13
+ * * `degrade` 档只允许降级到**已在白名单**的廉价模型,绝不引入新 provider。
14
+ *
15
+ * 成本维度(`limitCost`)需要调用方给出本次预估成本(`cost` 字段);
16
+ * 未提供成本时成本维度不可判定,会显式标 `costTracked: false`,而不是假装达标。
17
+ */
18
+
19
+ import { DEFAULT_BUDGET_LIMITS } from './types.ts'
20
+
21
+ const UNITS = ['task', 'fork', 'day']
22
+ const EXHAUST_ACTIONS = ['hard-stop', 'degrade']
23
+
24
+ /** 与 kernel.masterError 同形状(host 侧不反向依赖 kernel,避免子系统耦合)。 */
25
+ function masterError(code, message, detail) {
26
+ const out = { ok: false, code: String(code), error: String(message ?? code) }
27
+ if (detail !== undefined) out.detail = detail
28
+ return out
29
+ }
30
+
31
+ function positiveLimit(value) {
32
+ const n = Number(value)
33
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : null
34
+ }
35
+
36
+ function keyOf(unit, key) {
37
+ return `${unit}:${String(key ?? 'default')}`
38
+ }
39
+
40
+ /**
41
+ * @param {{
42
+ * limits?: Array<{unit:string, limitTokens?:number, limitCost?:number, onExhaust?:string, degradeTo?:string}>,
43
+ * degradeModels?: string[], // degrade 允许的廉价模型白名单(不在此列表 ⇒ 一律 hard-stop)
44
+ * now?: () => number,
45
+ * }} [options]
46
+ */
47
+ export function createBudget(options = {}) {
48
+ const now = typeof options.now === 'function' ? options.now : () => Date.now()
49
+ const source = Array.isArray(options.limits) && options.limits.length > 0 ? options.limits : DEFAULT_BUDGET_LIMITS
50
+ const degradeModels = Object.freeze(
51
+ (Array.isArray(options.degradeModels) ? options.degradeModels : [])
52
+ .map((item) => String(item ?? '').trim())
53
+ .filter(Boolean),
54
+ )
55
+ const limits = []
56
+ const invalid = []
57
+ for (const item of source) {
58
+ const unit = String(item?.unit ?? '')
59
+ const limitTokens = item?.limitTokens === undefined ? null : positiveLimit(item.limitTokens)
60
+ const limitCost = item?.limitCost === undefined ? null : positiveLimit(item.limitCost)
61
+ if (!UNITS.includes(unit) || (limitTokens === null && limitCost === null)) {
62
+ // 既无 token 上限也无成本上限的规则没有意义:配置错误必须显式暴露(fail-closed)。
63
+ invalid.push({ unit, limitTokens: item?.limitTokens, limitCost: item?.limitCost })
64
+ continue
65
+ }
66
+ if (limits.some((rule) => rule.unit === unit)) {
67
+ // 同一 unit 只能有一条规则(先注册先得),否则判定口径不确定。
68
+ invalid.push({ unit, reason: 'duplicate-unit' })
69
+ continue
70
+ }
71
+ const onExhaust = EXHAUST_ACTIONS.includes(String(item?.onExhaust)) ? String(item.onExhaust) : 'hard-stop'
72
+ const degradeTo = item?.degradeTo === undefined ? null : String(item.degradeTo)
73
+ // degrade 档必须在白名单内,否则等于开了个后门:降级为 hard-stop。
74
+ const effective = onExhaust === 'degrade' && degradeTo !== null && degradeModels.includes(degradeTo)
75
+ ? 'degrade'
76
+ : 'hard-stop'
77
+ if (onExhaust === 'degrade' && effective !== 'degrade') {
78
+ invalid.push({ unit, degradeTo, reason: 'degrade-model-not-allowlisted' })
79
+ }
80
+ limits.push(Object.freeze({
81
+ unit,
82
+ limitTokens,
83
+ limitCost,
84
+ onExhaust: effective,
85
+ degradeTo: effective === 'degrade' ? degradeTo : null,
86
+ }))
87
+ }
88
+ const limitOf = (unit) => limits.find((item) => item.unit === unit) ?? null
89
+ const consumed = new Map()
90
+ const used = (unit, key) => consumed.get(keyOf(unit, key)) ?? { tokens: 0, cost: 0 }
91
+
92
+ /**
93
+ * ① beforeModelCall 的唯一检查点。
94
+ * @param {{unit: string, key?: string, tokens?: number, cost?: number}} input
95
+ */
96
+ function authorize(input = {}) {
97
+ const unit = String(input.unit)
98
+ const key = String(input.key ?? 'default')
99
+ const requested = Number(input.tokens ?? 0)
100
+ const requestedCost = Number(input.cost ?? 0)
101
+ if (!Number.isFinite(requested) || requested < 0) {
102
+ return masterError('tokens-invalid', 'tokens must be a non-negative number')
103
+ }
104
+ if (!Number.isFinite(requestedCost) || requestedCost < 0) {
105
+ return masterError('cost-invalid', 'cost must be a non-negative number')
106
+ }
107
+ const limit = limitOf(unit)
108
+ if (!limit) {
109
+ return { ok: true, unlimited: true, unit, key, requested, remaining: null, costTracked: false, at: now() }
110
+ }
111
+
112
+ const current = used(unit, key)
113
+ const remainingTokens = limit.limitTokens === null ? null : Math.max(0, limit.limitTokens - current.tokens)
114
+ const remainingCost = limit.limitCost === null ? null : Math.max(0, limit.limitCost - current.cost)
115
+ const tokenExhausted = limit.limitTokens !== null && requested > remainingTokens
116
+ const costExhausted = limit.limitCost !== null && requestedCost > remainingCost
117
+ const costTracked = limit.limitCost === null ? false : Number(input.cost) > 0
118
+
119
+ if (tokenExhausted || costExhausted) {
120
+ const reason = tokenExhausted ? 'tokens' : 'cost'
121
+ // degrade 档:只降级到白名单里的廉价模型;否则一律 fail-closed。
122
+ if (limit.onExhaust === 'degrade' && limit.degradeTo && degradeModels.includes(limit.degradeTo)) {
123
+ return {
124
+ ok: true,
125
+ degraded: true,
126
+ action: 'degrade',
127
+ unit,
128
+ key,
129
+ model: limit.degradeTo,
130
+ reason,
131
+ limitTokens: limit.limitTokens,
132
+ limitCost: limit.limitCost,
133
+ consumedTokens: current.tokens,
134
+ consumedCost: current.cost,
135
+ remainingTokens,
136
+ remainingCost,
137
+ requested,
138
+ requestedCost,
139
+ costTracked,
140
+ onExhaust: 'degrade',
141
+ at: now(),
142
+ }
143
+ }
144
+ // fail-closed:拒绝且**不扣额度**,绝不「先跑着看」。
145
+ return masterError('budget-exhausted', `预算不足:${unit}/${key} 剩余 ${remainingTokens ?? '∞'} token`, {
146
+ unit,
147
+ key,
148
+ reason,
149
+ limitTokens: limit.limitTokens,
150
+ limitCost: limit.limitCost,
151
+ consumedTokens: current.tokens,
152
+ consumedCost: current.cost,
153
+ remainingTokens,
154
+ remainingCost,
155
+ requested,
156
+ requestedCost,
157
+ costTracked,
158
+ onExhaust: limit.onExhaust,
159
+ /** 剩余信息同时平铺在顶层,便于调用方不需解 detail。 */
160
+ remaining: { tokens: remainingTokens, cost: remainingCost },
161
+ at: now(),
162
+ })
163
+ }
164
+
165
+ const nextTokens = current.tokens + Math.floor(requested)
166
+ const nextCost = current.cost + requestedCost
167
+ consumed.set(keyOf(unit, key), { tokens: nextTokens, cost: nextCost })
168
+ return {
169
+ ok: true,
170
+ unit,
171
+ key,
172
+ requested,
173
+ requestedCost,
174
+ consumedTokens: nextTokens,
175
+ consumedCost: nextCost,
176
+ limitTokens: limit.limitTokens,
177
+ limitCost: limit.limitCost,
178
+ remainingTokens: limit.limitTokens === null ? null : limit.limitTokens - nextTokens,
179
+ remainingCost: limit.limitCost === null ? null : limit.limitCost - nextCost,
180
+ costTracked,
181
+ at: now(),
182
+ }
183
+ }
184
+
185
+ function refund({ unit, key = 'default', tokens = 0, cost = 0 } = {}) {
186
+ const n = Number(tokens)
187
+ const c = Number(cost)
188
+ if (!Number.isFinite(n) || n < 0 || !Number.isFinite(c) || c < 0 || (n === 0 && c === 0)) {
189
+ return masterError('refund-invalid', 'refund requires positive tokens and/or cost')
190
+ }
191
+ const k = keyOf(String(unit), key)
192
+ const current = consumed.get(k) ?? { tokens: 0, cost: 0 }
193
+ const next = { tokens: Math.max(0, current.tokens - Math.floor(n)), cost: Math.max(0, current.cost - c) }
194
+ consumed.set(k, next)
195
+ return { ok: true, unit: String(unit), key: String(key), consumedTokens: next.tokens, consumedCost: next.cost, at: now() }
196
+ }
197
+
198
+ function state() {
199
+ const rows = []
200
+ for (const limit of limits) {
201
+ for (const [k, value] of consumed.entries()) {
202
+ const prefix = `${limit.unit}:`
203
+ if (!k.startsWith(prefix)) continue
204
+ rows.push(Object.freeze({
205
+ unit: limit.unit,
206
+ key: k.slice(prefix.length),
207
+ consumedTokens: value.tokens,
208
+ consumedCost: value.cost,
209
+ limitTokens: limit.limitTokens,
210
+ limitCost: limit.limitCost,
211
+ remainingTokens: limit.limitTokens === null ? null : Math.max(0, limit.limitTokens - value.tokens),
212
+ remainingCost: limit.limitCost === null ? null : Math.max(0, limit.limitCost - value.cost),
213
+ onExhaust: limit.onExhaust,
214
+ }))
215
+ }
216
+ }
217
+ return Object.freeze({ ok: true, limits, invalid, degradeModels: degradeModels.slice(), rows, at: now() })
218
+ }
219
+
220
+ function reset() {
221
+ consumed.clear()
222
+ return { ok: true }
223
+ }
224
+
225
+ return Object.freeze({ authorize, refund, state, reset, limits: () => limits.slice(), degradeModels: () => degradeModels.slice() })
226
+ }
@@ -0,0 +1,97 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Master contributors — see LICENSE-CORE.
3
+ // @ts-nocheck — host JS→TS 迁移阶段:运行时行为优先,类型注解逐步补齐(worker/ 仍严格检查)。
4
+
5
+ /**
6
+ * 3.3.0 系统层 · bus(事件总线与回流出口,设计 §5.6)。
7
+ *
8
+ * 对外只暴露三种内部事件(冒号命名,与 Router 事件通道一致):
9
+ * * `cost:event` —— 账本/归因/预算等一般成本事件(payload.kind 区分);
10
+ * * `cost:degraded` —— 模块降级(不伪装成节省);
11
+ * * `cost:substitution` —— 替换证书(与审计链同源)。
12
+ *
13
+ * 可选对外出口(3.3.0 不阻塞主线):
14
+ * * `web` —— Worker→Router 既有事件通道(Router 落库后供状态页/分身信息栏读取);
15
+ * * `fang` —— 瓮城「产物回流」;未就绪时状态里必须显式 `available: false` + 原因,
16
+ * **不得**静默丢弃或假装成功。
17
+ */
18
+
19
+ export const COST_EVENT_TYPES = Object.freeze(['cost:event', 'cost:degraded', 'cost:substitution'])
20
+
21
+ /**
22
+ * @param {{maxEvents?: number, now?: () => number, relays?: Array<{name:string, publish?:Function, available?:boolean, reason?:string}>}} [options]
23
+ */
24
+ export function createCostBus(options = {}) {
25
+ const now = typeof options.now === 'function' ? options.now : () => Date.now()
26
+ const maxEvents = Math.max(1, Number(options.maxEvents ?? 200) || 200)
27
+ const events = []
28
+ const subscribers = new Set()
29
+ /** 外部出口(web / fang);available=false 的出口只记录状态,不投递。 */
30
+ const relays = (Array.isArray(options.relays) ? options.relays : [])
31
+ .filter((relay) => relay && typeof relay === 'object' && relay.name)
32
+ .map((relay) => ({
33
+ name: String(relay.name),
34
+ publish: typeof relay.publish === 'function' ? relay.publish : null,
35
+ available: relay.available === true && typeof relay.publish === 'function',
36
+ reason: String(relay.reason ?? ''),
37
+ delivered: 0,
38
+ failed: 0,
39
+ }))
40
+ let dropped = 0
41
+
42
+ function publish(type, payload = {}) {
43
+ const event = Object.freeze({ type: String(type ?? 'unknown'), at: now(), payload: Object.freeze({ ...payload }) })
44
+ events.push(event)
45
+ if (events.length > maxEvents) {
46
+ events.shift()
47
+ dropped += 1
48
+ }
49
+ for (const listener of [...subscribers]) {
50
+ try { listener(event) } catch { /* 订阅者异常不影响成本链路 */ }
51
+ }
52
+ for (const relay of relays) {
53
+ if (!relay.available || !relay.publish) continue
54
+ try {
55
+ relay.publish(event)
56
+ relay.delivered += 1
57
+ } catch {
58
+ relay.failed += 1
59
+ // 出口失败不得影响主流程,但必须留痕(failed 计数),不静默吞掉。
60
+ }
61
+ }
62
+ return event
63
+ }
64
+
65
+ function subscribe(listener) {
66
+ if (typeof listener !== 'function') return () => {}
67
+ subscribers.add(listener)
68
+ return () => { subscribers.delete(listener) }
69
+ }
70
+
71
+ function snapshot(limit = 50) {
72
+ const n = Math.max(1, Number(limit) || 50)
73
+ return Object.freeze({
74
+ ok: true,
75
+ count: events.length,
76
+ dropped,
77
+ subscribers: subscribers.size,
78
+ events: events.slice(-n),
79
+ /** §5.6 出口状态:不可用必须显式说明原因,不假装成功。 */
80
+ relays: relays.map((relay) => Object.freeze({
81
+ name: relay.name,
82
+ available: relay.available,
83
+ reason: relay.reason,
84
+ delivered: relay.delivered,
85
+ failed: relay.failed,
86
+ })),
87
+ })
88
+ }
89
+
90
+ function reset() {
91
+ events.length = 0
92
+ dropped = 0
93
+ return { ok: true }
94
+ }
95
+
96
+ return Object.freeze({ publish, subscribe, snapshot, reset })
97
+ }