dsh-cost-meter 1.9.38 → 1.9.39

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.
@@ -1,119 +1,119 @@
1
- import { constants } from 'node:fs'
2
- import { open } from 'node:fs/promises'
3
- import { localDayKey, zeroDay } from './store.js'
4
- import { fxRatesOf } from './exchange-rates.js'
5
- import { costOf, providerPriceEntryFor, usdFromCost } from './pricing.js'
6
-
7
- export const EXTERNAL_USAGE_MAX_BYTES = 512 * 1024
8
- const MAX_AGE = 30 * 86400000
9
- const FRESH_AGE = 86400000
10
- const fields = ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning', 'calls', 'cost', 'apiCost']
11
- const object = v => v !== null && typeof v === 'object' && !Array.isArray(v)
12
- const count = v => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= 1e12
13
- const money = v => typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 1e9
14
- const dateKey = v => typeof v === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(v) && !Number.isNaN(Date.parse(v + 'T00:00:00Z')) && new Date(v + 'T00:00:00Z').toISOString().slice(0, 10) === v
15
- const timestamp = v => typeof v === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})$/.test(v) && dateKey(v.slice(0, 10)) ? Date.parse(v) : NaN
16
-
17
- function add(into, day) {
18
- for (const key of fields) into[key] += day[key]
19
- }
20
-
21
- function summary(days, dayKey) {
22
- const today = zeroDay(dayKey)
23
- const month = zeroDay(dayKey.slice(0, 7))
24
- const total = zeroDay('total')
25
- const history = Object.values(days).sort((a, b) => b.date.localeCompare(a.date)).slice(0, 90)
26
- for (const day of Object.values(days)) {
27
- add(total, day)
28
- if (day.date.startsWith(dayKey.slice(0, 7))) add(month, day)
29
- if (day.date === dayKey) add(today, day)
30
- }
31
- return { today, month, total, history }
32
- }
33
-
34
- /** A snapshot replaces its predecessor; it never mutates the DSH ledger. */
35
- export function parseExternalUsageSnapshot(value, config, nowMs = Date.now()) {
36
- if (!object(value)) return null
37
- if (Object.hasOwn(value, 'sources') && !Array.isArray(value.sources)) return null
38
- const fetchedAt = timestamp(value.fetchedAt)
39
- if (!Number.isFinite(fetchedAt) || fetchedAt > nowMs + 300000 || nowMs - fetchedAt > MAX_AGE) return null
40
- const entries = Array.isArray(value.sources) ? value.sources : [value]
41
- if (entries.length < 1 || entries.length > 8) return null
42
- const seen = new Set()
43
- const sources = []
44
- const combinedDays = {}
45
- let recordCount = 0
46
- for (const entry of entries) {
47
- if (!object(entry) || typeof entry.source !== 'string' || entry.source.length < 1 || entry.source.length > 64 || seen.has(entry.source)) return null
48
- seen.add(entry.source)
49
- if (object(entry.days) === Array.isArray(entry.records)) return null // exactly one input mode
50
- const days = {}
51
- if (object(entry.days)) {
52
- if (Object.keys(entry.days).length > 3660) return null
53
- for (const [date, raw] of Object.entries(entry.days)) {
54
- if (!dateKey(date) || date > localDayKey(nowMs) || !object(raw) || !count(raw.input) || !count(raw.output) || !count(raw.cached) || !count(raw.calls) || !money(raw.costUsd)) return null
55
- if (raw.cacheWrite !== undefined && !count(raw.cacheWrite)) return null
56
- if (raw.reasoning !== undefined && !count(raw.reasoning)) return null
57
- days[date] = { ...zeroDay(date), input: raw.input, output: raw.output, cacheRead: raw.cached, cacheWrite: raw.cacheWrite ?? 0, reasoning: raw.reasoning ?? 0, calls: raw.calls, cost: raw.costUsd, apiCost: raw.costUsd }
58
- }
59
- } else {
60
- recordCount += entry.records.length
61
- if (recordCount > 5000) return null
62
- const ids = new Set()
63
- for (const raw of entry.records) {
64
- if (!object(raw) || typeof raw.id !== 'string' || raw.id.length < 1 || raw.id.length > 128 || ids.has(raw.id)) return null
65
- ids.add(raw.id)
66
- const at = timestamp(raw.at)
67
- if (!Number.isFinite(at) || at > nowMs + 300000 || !count(raw.input) || !count(raw.output) || !count(raw.cached)) return null
68
- if (raw.cacheWrite !== undefined && !count(raw.cacheWrite)) return null
69
- if (raw.reasoning !== undefined && !count(raw.reasoning)) return null
70
- if (typeof raw.provider !== 'string' || !raw.provider || typeof raw.model !== 'string' || !raw.model) return null
71
- const resolved = providerPriceEntryFor(raw.provider, raw.model, config?.prices, {
72
- mode: config?.priceMatch === 'exact' ? 'exact' : 'auto', overrides: config?.priceOverrides,
73
- })
74
- if (!resolved.priced) return null
75
- const peak = { enabled: ['deepseek-peak', 'utc-peak'].includes(resolved.billingMode) && config?.peakEnabled === true,
76
- effectiveAtMs: Date.parse(config?.peakEffectiveAt ?? ''), windows: config?.peakWindows, holidays: config?.peakHolidays }
77
- const tokens = { input: raw.input, output: raw.output, cacheRead: raw.cached, cacheWrite: raw.cacheWrite ?? 0, reasoning: raw.reasoning ?? 0 }
78
- const priced = costOf(tokens, resolved.entry, at, peak)
79
- const usd = usdFromCost(priced, resolved.currency, fxRatesOf(config).CNY)
80
- const date = localDayKey(at)
81
- const day = days[date] ?? (days[date] = zeroDay(date))
82
- for (const key of ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning']) day[key] += tokens[key]
83
- day.calls++
84
- day.cost += usd
85
- day.apiCost += usd
86
- }
87
- }
88
- for (const [date, day] of Object.entries(days)) add(combinedDays[date] ?? (combinedDays[date] = zeroDay(date)), day)
89
- sources.push({ source: entry.source, ...summary(days, localDayKey(nowMs)) })
90
- }
91
- return { fetchedAt, stale: nowMs - fetchedAt > FRESH_AGE, sources,
92
- combined: summary(combinedDays, localDayKey(nowMs)) }
93
- }
94
-
95
- /** Bounded, fail-closed read of a regular file. Producers should write a temp file and rename it. */
96
- export async function readExternalUsageSnapshot(path, config, nowMs = Date.now()) {
97
- let file
98
- try {
99
- file = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0))
100
- const stat = await file.stat()
101
- if (!stat.isFile() || stat.size > EXTERNAL_USAGE_MAX_BYTES) return null
102
- const buffer = Buffer.alloc(EXTERNAL_USAGE_MAX_BYTES + 1)
103
- let length = 0
104
- while (length < buffer.length) {
105
- const { bytesRead } = await file.read(buffer, length, buffer.length - length, null)
106
- if (!bytesRead) break
107
- length += bytesRead
108
- }
109
- if (length > EXTERNAL_USAGE_MAX_BYTES) return null
110
- return parseExternalUsageSnapshot(JSON.parse(buffer.subarray(0, length).toString('utf8')), config, nowMs)
111
- } catch { return null } finally { await file?.close().catch(() => {}) }
112
- }
113
-
114
- /** Fresh external usage participates in same-account balance reconciliation. */
115
- export function externalTodayCostForReconcile(snapshot) {
116
- if (snapshot?.stale === true) return null
117
- const cost = snapshot?.combined?.today?.cost
118
- return Number.isFinite(cost) && cost >= 0 ? cost : 0
119
- }
1
+ import { constants } from 'node:fs'
2
+ import { open } from 'node:fs/promises'
3
+ import { localDayKey, zeroDay } from './store.js'
4
+ import { fxRatesOf } from './exchange-rates.js'
5
+ import { costOf, providerPriceEntryFor, usdFromCost } from './pricing.js'
6
+
7
+ export const EXTERNAL_USAGE_MAX_BYTES = 512 * 1024
8
+ const MAX_AGE = 30 * 86400000
9
+ const FRESH_AGE = 86400000
10
+ const fields = ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning', 'calls', 'cost', 'apiCost']
11
+ const object = v => v !== null && typeof v === 'object' && !Array.isArray(v)
12
+ const count = v => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= 1e12
13
+ const money = v => typeof v === 'number' && Number.isFinite(v) && v >= 0 && v <= 1e9
14
+ const dateKey = v => typeof v === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(v) && !Number.isNaN(Date.parse(v + 'T00:00:00Z')) && new Date(v + 'T00:00:00Z').toISOString().slice(0, 10) === v
15
+ const timestamp = v => typeof v === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})$/.test(v) && dateKey(v.slice(0, 10)) ? Date.parse(v) : NaN
16
+
17
+ function add(into, day) {
18
+ for (const key of fields) into[key] += day[key]
19
+ }
20
+
21
+ function summary(days, dayKey) {
22
+ const today = zeroDay(dayKey)
23
+ const month = zeroDay(dayKey.slice(0, 7))
24
+ const total = zeroDay('total')
25
+ const history = Object.values(days).sort((a, b) => b.date.localeCompare(a.date)).slice(0, 90)
26
+ for (const day of Object.values(days)) {
27
+ add(total, day)
28
+ if (day.date.startsWith(dayKey.slice(0, 7))) add(month, day)
29
+ if (day.date === dayKey) add(today, day)
30
+ }
31
+ return { today, month, total, history }
32
+ }
33
+
34
+ /** A snapshot replaces its predecessor; it never mutates the DSH ledger. */
35
+ export function parseExternalUsageSnapshot(value, config, nowMs = Date.now()) {
36
+ if (!object(value)) return null
37
+ if (Object.hasOwn(value, 'sources') && !Array.isArray(value.sources)) return null
38
+ const fetchedAt = timestamp(value.fetchedAt)
39
+ if (!Number.isFinite(fetchedAt) || fetchedAt > nowMs + 300000 || nowMs - fetchedAt > MAX_AGE) return null
40
+ const entries = Array.isArray(value.sources) ? value.sources : [value]
41
+ if (entries.length < 1 || entries.length > 8) return null
42
+ const seen = new Set()
43
+ const sources = []
44
+ const combinedDays = {}
45
+ let recordCount = 0
46
+ for (const entry of entries) {
47
+ if (!object(entry) || typeof entry.source !== 'string' || entry.source.length < 1 || entry.source.length > 64 || seen.has(entry.source)) return null
48
+ seen.add(entry.source)
49
+ if (object(entry.days) === Array.isArray(entry.records)) return null // exactly one input mode
50
+ const days = {}
51
+ if (object(entry.days)) {
52
+ if (Object.keys(entry.days).length > 3660) return null
53
+ for (const [date, raw] of Object.entries(entry.days)) {
54
+ if (!dateKey(date) || date > localDayKey(nowMs) || !object(raw) || !count(raw.input) || !count(raw.output) || !count(raw.cached) || !count(raw.calls) || !money(raw.costUsd)) return null
55
+ if (raw.cacheWrite !== undefined && !count(raw.cacheWrite)) return null
56
+ if (raw.reasoning !== undefined && !count(raw.reasoning)) return null
57
+ days[date] = { ...zeroDay(date), input: raw.input, output: raw.output, cacheRead: raw.cached, cacheWrite: raw.cacheWrite ?? 0, reasoning: raw.reasoning ?? 0, calls: raw.calls, cost: raw.costUsd, apiCost: raw.costUsd }
58
+ }
59
+ } else {
60
+ recordCount += entry.records.length
61
+ if (recordCount > 5000) return null
62
+ const ids = new Set()
63
+ for (const raw of entry.records) {
64
+ if (!object(raw) || typeof raw.id !== 'string' || raw.id.length < 1 || raw.id.length > 128 || ids.has(raw.id)) return null
65
+ ids.add(raw.id)
66
+ const at = timestamp(raw.at)
67
+ if (!Number.isFinite(at) || at > nowMs + 300000 || !count(raw.input) || !count(raw.output) || !count(raw.cached)) return null
68
+ if (raw.cacheWrite !== undefined && !count(raw.cacheWrite)) return null
69
+ if (raw.reasoning !== undefined && !count(raw.reasoning)) return null
70
+ if (typeof raw.provider !== 'string' || !raw.provider || typeof raw.model !== 'string' || !raw.model) return null
71
+ const resolved = providerPriceEntryFor(raw.provider, raw.model, config?.prices, {
72
+ mode: config?.priceMatch === 'exact' ? 'exact' : 'auto', overrides: config?.priceOverrides,
73
+ })
74
+ if (!resolved.priced) return null
75
+ const peak = { enabled: ['deepseek-peak', 'utc-peak'].includes(resolved.billingMode) && config?.peakEnabled === true,
76
+ effectiveAtMs: Date.parse(config?.peakEffectiveAt ?? ''), windows: config?.peakWindows, holidays: config?.peakHolidays }
77
+ const tokens = { input: raw.input, output: raw.output, cacheRead: raw.cached, cacheWrite: raw.cacheWrite ?? 0, reasoning: raw.reasoning ?? 0 }
78
+ const priced = costOf(tokens, resolved.entry, at, peak)
79
+ const usd = usdFromCost(priced, resolved.currency, fxRatesOf(config).CNY)
80
+ const date = localDayKey(at)
81
+ const day = days[date] ?? (days[date] = zeroDay(date))
82
+ for (const key of ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning']) day[key] += tokens[key]
83
+ day.calls++
84
+ day.cost += usd
85
+ day.apiCost += usd
86
+ }
87
+ }
88
+ for (const [date, day] of Object.entries(days)) add(combinedDays[date] ?? (combinedDays[date] = zeroDay(date)), day)
89
+ sources.push({ source: entry.source, ...summary(days, localDayKey(nowMs)) })
90
+ }
91
+ return { fetchedAt, stale: nowMs - fetchedAt > FRESH_AGE, sources,
92
+ combined: summary(combinedDays, localDayKey(nowMs)) }
93
+ }
94
+
95
+ /** Bounded, fail-closed read of a regular file. Producers should write a temp file and rename it. */
96
+ export async function readExternalUsageSnapshot(path, config, nowMs = Date.now()) {
97
+ let file
98
+ try {
99
+ file = await open(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0))
100
+ const stat = await file.stat()
101
+ if (!stat.isFile() || stat.size > EXTERNAL_USAGE_MAX_BYTES) return null
102
+ const buffer = Buffer.alloc(EXTERNAL_USAGE_MAX_BYTES + 1)
103
+ let length = 0
104
+ while (length < buffer.length) {
105
+ const { bytesRead } = await file.read(buffer, length, buffer.length - length, null)
106
+ if (!bytesRead) break
107
+ length += bytesRead
108
+ }
109
+ if (length > EXTERNAL_USAGE_MAX_BYTES) return null
110
+ return parseExternalUsageSnapshot(JSON.parse(buffer.subarray(0, length).toString('utf8')), config, nowMs)
111
+ } catch { return null } finally { await file?.close().catch(() => {}) }
112
+ }
113
+
114
+ /** Fresh external usage participates in same-account balance reconciliation. */
115
+ export function externalTodayCostForReconcile(snapshot) {
116
+ if (snapshot?.stale === true) return null
117
+ const cost = snapshot?.combined?.today?.cost
118
+ return Number.isFinite(cost) && cost >= 0 ? cost : 0
119
+ }
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import { createHash } from 'node:crypto'
11
+ import { setTimeout as delay } from 'node:timers/promises'
11
12
  import { credentialRef } from '@deepseek-ai/dsh-credentials'
12
13
  import { fetchWithRetry, readJsonBounded } from './net.js'
13
14
  import {
@@ -227,14 +228,14 @@ function gatewayRetryDelay(response) {
227
228
  return retryAfterMs(response) || GATEWAY_RETRY_DELAY_MS
228
229
  }
229
230
 
230
- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
231
+ const sleep = (ms, signal) => delay(ms, undefined, { signal })
231
232
 
232
233
  async function discardResponse(response) {
233
234
  // 重试前释放未消费的错误响应,避免多账号轮询占住连接池。
234
235
  try { await response?.body?.cancel() } catch { /* 清理失败不掩盖原始 HTTP 错误。 */ }
235
236
  }
236
237
 
237
- async function readJsonResponse(response, phase, { maxBytes = MAX_BODY_BYTES } = {}) {
238
+ async function readJsonResponse(response, phase, { maxBytes = MAX_BODY_BYTES, signal } = {}) {
238
239
  if (!response || typeof response.ok !== 'boolean') throw errorOf('CPA_RESPONSE_INVALID', `${phase} response is invalid`)
239
240
  if (!response.ok) {
240
241
  await discardResponse(response)
@@ -246,8 +247,11 @@ async function readJsonResponse(response, phase, { maxBytes = MAX_BODY_BYTES } =
246
247
  await discardResponse(response)
247
248
  throw errorOf('CPA_RESPONSE_INVALID', `${phase} response is too large`)
248
249
  }
249
- return await readJsonBounded(response, maxBytes)
250
+ const body = await readJsonBounded(response, maxBytes, { signal })
251
+ signal?.throwIfAborted()
252
+ return body
250
253
  } catch (error) {
254
+ signal?.throwIfAborted()
251
255
  if (error?.code === 'CPA_RESPONSE_INVALID') throw error
252
256
  throw errorOf('CPA_RESPONSE_INVALID', error?.code === 'RESPONSE_TOO_LARGE'
253
257
  ? `${phase} response is too large` : `${phase} response body is not readable JSON`)
@@ -256,18 +260,20 @@ async function readJsonResponse(response, phase, { maxBytes = MAX_BODY_BYTES } =
256
260
 
257
261
  export async function cpaManagementFetch(url, key, init = {}, options = {}) {
258
262
  const fetchImpl = options.fetchImpl ?? fetch
263
+ const signal = init.signal ?? options.signal
259
264
  const headers = { ...(init.headers ?? {}), 'X-Management-Key': key }
260
265
  let response
261
266
  try {
262
- response = await fetchWithRetry(url, { ...init, headers, redirect: 'manual' }, {
267
+ response = await fetchWithRetry(url, { ...init, headers, redirect: 'manual', signal }, {
263
268
  attempts: 2, timeoutMs: init.timeoutMs ?? 15_000, fetchImpl,
264
269
  })
265
270
  } catch (error) {
271
+ signal?.throwIfAborted()
266
272
  if (error?.code) throw error
267
273
  throw errorOf('CPA_OUTER_HTTP_ERROR', 'management request failed', { status: 0 })
268
274
  }
269
- if (response.status >= 300 && response.status < 400) throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status })
270
- return readJsonResponse(response, new URL(url).pathname)
275
+ if (response.status >= 300 && response.status < 400) { await discardResponse(response); throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status }) }
276
+ return readJsonResponse(response, new URL(url).pathname, { signal })
271
277
  }
272
278
 
273
279
  async function fetchManagement(source, path, managementKey, options = {}) {
@@ -281,21 +287,22 @@ async function fetchManagement(source, path, managementKey, options = {}) {
281
287
  for (let attempt = 0; attempt < GATEWAY_RESPONSE_ATTEMPTS; attempt++) {
282
288
  let response
283
289
  try {
284
- response = await fetchWithRetry(url, { method: 'GET', headers, redirect: 'manual' }, {
290
+ response = await fetchWithRetry(url, { method: 'GET', headers, redirect: 'manual', signal: options.signal }, {
285
291
  attempts: 2, timeoutMs: 15_000, fetchImpl,
286
292
  })
287
293
  } catch (error) {
294
+ options.signal?.throwIfAborted()
288
295
  if (error?.code) throw error
289
296
  throw errorOf('CPA_OUTER_HTTP_ERROR', 'management request failed', { status: 0 })
290
297
  }
291
- if (response.status >= 300 && response.status < 400) throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status })
298
+ if (response.status >= 300 && response.status < 400) { await discardResponse(response); throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status }) }
292
299
  // 管理端临时过载/限流时按 Retry-After 或短退避再试一次。
293
300
  if (isRetryableGatewayStatus(response.status) && attempt + 1 < GATEWAY_RESPONSE_ATTEMPTS) {
294
301
  await discardResponse(response)
295
- await sleep(gatewayRetryDelay(response))
302
+ await sleep(gatewayRetryDelay(response), options.signal)
296
303
  continue
297
304
  }
298
- return { payload: await readJsonResponse(response, path), serverVersion: headerValue(response.headers, 'x-cpa-version') }
305
+ return { payload: await readJsonResponse(response, path, options), serverVersion: headerValue(response.headers, 'x-cpa-version') }
299
306
  }
300
307
  throw errorOf('CPA_OUTER_HTTP_ERROR', 'management request failed', { status: 0 })
301
308
  }
@@ -415,6 +422,7 @@ function normalizeInnerEnvelope(payload, phase) {
415
422
  }
416
423
 
417
424
  async function apiCall(source, managementKey, account, request, options = {}) {
425
+ options.signal?.throwIfAborted()
418
426
  const fetchImpl = options.fetchImpl ?? fetch
419
427
  // 副作用端点硬闸(FORBIDDEN_REQUEST_MARKERS 的实际执行点):任何请求(URL 或
420
428
  // body 序列化)命中消费/补全端点标记即拒绝发出——深度防御,防未来 adapter
@@ -448,25 +456,27 @@ async function apiCall(source, managementKey, account, request, options = {}) {
448
456
  signal: options.signal,
449
457
  }, { attempts: 2, timeoutMs: 30_000, fetchImpl })
450
458
  } catch (error) {
459
+ options.signal?.throwIfAborted()
451
460
  if (error?.code) throw error
452
461
  throw errorOf('CPA_OUTER_HTTP_ERROR', 'CPA api-call failed')
453
462
  }
454
- if (response.status >= 300 && response.status < 400) throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status })
463
+ if (response.status >= 300 && response.status < 400) { await discardResponse(response); throw errorOf('CPA_REDIRECT_REFUSED', 'CPA redirect refused', { status: response.status }) }
455
464
  // api-call 是副作用闸门已校验的只读额度请求;仅对暂态管理端状态重放一次。
456
465
  if (isRetryableGatewayStatus(response.status) && attempt + 1 < GATEWAY_RESPONSE_ATTEMPTS) {
457
466
  await discardResponse(response)
458
- await sleep(gatewayRetryDelay(response))
467
+ await sleep(gatewayRetryDelay(response), options.signal)
459
468
  continue
460
469
  }
461
470
  try {
462
- const envelope = await readJsonResponse(response, 'api-call')
471
+ const envelope = await readJsonResponse(response, 'api-call', options)
463
472
  return normalizeInnerEnvelope(envelope, 'api-call')
464
473
  } catch (error) {
474
+ options.signal?.throwIfAborted()
465
475
  // CPA 把上游 HTTP 状态装进 JSON envelope。429/5xx 同样属于暂态,
466
476
  // 但认证、策略与格式错误绝不重试。
467
477
  if (error?.code === 'CPA_INNER_HTTP_ERROR' && isRetryableGatewayStatus(error.status)
468
478
  && attempt + 1 < GATEWAY_RESPONSE_ATTEMPTS) {
469
- await sleep(GATEWAY_RETRY_DELAY_MS)
479
+ await sleep(GATEWAY_RETRY_DELAY_MS, options.signal)
470
480
  continue
471
481
  }
472
482
  throw error
@@ -476,6 +486,7 @@ async function apiCall(source, managementKey, account, request, options = {}) {
476
486
  }
477
487
 
478
488
  async function queryApiAccount(source, managementKey, account, options = {}) {
489
+ options.signal?.throwIfAborted()
479
490
  const adapter = GATEWAY_PROVIDER_ADAPTERS[account.provider]
480
491
  if (!adapter || typeof adapter.requests !== 'function') return publicAccount(account, { status: 'unsupported', message: 'provider adapter is unavailable' })
481
492
  if (account.provider === 'codex' && !account.authIndex) return publicAccount(account, { status: 'capability_missing', message: 'PROVIDER_CAPABILITY_MISSING: Codex account metadata is missing' })
@@ -490,6 +501,7 @@ async function queryApiAccount(source, managementKey, account, options = {}) {
490
501
  bodies[index] = await apiCall(source, managementKey, account, requests[index], options)
491
502
  if (account.provider !== 'xai') break
492
503
  } catch (error) {
504
+ options.signal?.throwIfAborted()
493
505
  lastError = error
494
506
  if (account.provider === 'antigravity' && (error.code === 'CPA_INNER_HTTP_ERROR' || error.status === 404 || error.status === 403)) continue
495
507
  if (account.provider === 'xai') continue
@@ -546,6 +558,7 @@ async function queryApiAccount(source, managementKey, account, options = {}) {
546
558
  )
547
559
  return publicAccount(account, { status: 'ok', windows: parsed.windows, plan: parsed.plan })
548
560
  } catch (error) {
561
+ options.signal?.throwIfAborted()
549
562
  const code = error?.code || 'PROVIDER_PARSE_ERROR'
550
563
  return publicAccount(account, { status: 'error', message: safeMessage(code, error?.message) })
551
564
  }
@@ -581,6 +594,7 @@ async function queryWorkBuddy(source, managementKey, discovered, options = {}) {
581
594
  })
582
595
  })
583
596
  } catch (error) {
597
+ options.signal?.throwIfAborted()
584
598
  const code = error?.status === 404 ? 'WORKBUDDY_PLUGIN_UNAVAILABLE' : error?.code || 'CPA_OUTER_HTTP_ERROR'
585
599
  return discovered.map(account => publicAccount(account, { status: 'error', message: safeMessage(code, error?.message) }))
586
600
  }
@@ -608,10 +622,12 @@ export const emptyGatewayQuotaSource = emptyGatewayQuota
608
622
 
609
623
  /** Query one source. The returned object is safe for strict public state. */
610
624
  export async function queryGatewayQuota(ctx, rawSource, options = {}) {
625
+ options.signal?.throwIfAborted()
611
626
  const source = validateGatewaySource(rawSource)
612
627
  const clock = typeof options.now === 'function' ? options.now : () => typeof options.now === 'number' ? options.now : Date.now()
613
628
  const attemptedAt = Number(clock()) || Date.now()
614
629
  const key = await resolveManagementKey(ctx, source)
630
+ options.signal?.throwIfAborted()
615
631
  const base = { ...emptyGatewayQuota(source), attemptedAt, keyConfigured: key.configured, keySource: key.source }
616
632
  if (!source.enabled || source.display === 'off') return { ...base, status: 'off' }
617
633
  if (!key.configured) return { ...base, status: 'error', message: 'CPA_AUTH_FAILED: management key is not configured' }
@@ -623,6 +639,7 @@ export async function queryGatewayQuota(ctx, rawSource, options = {}) {
623
639
  authPayload = response.payload
624
640
  version = response.serverVersion
625
641
  } catch (error) {
642
+ options.signal?.throwIfAborted()
626
643
  return { ...base, status: 'error', message: safeMessage(error?.code || 'CPA_OUTER_HTTP_ERROR', error?.message) }
627
644
  }
628
645
  let discovery
@@ -634,7 +651,9 @@ export async function queryGatewayQuota(ctx, rawSource, options = {}) {
634
651
  const normal = discovery.accounts.filter(account => account.provider !== 'workbuddy')
635
652
  const work = discovery.accounts.filter(account => account.provider === 'workbuddy')
636
653
  const apiAccounts = await runLimited(normal, DEFAULT_CONCURRENCY, account => queryApiAccount(source, key.value, account, { ...options, fetchImpl }))
654
+ options.signal?.throwIfAborted()
637
655
  const workAccounts = work.length > 0 ? await queryWorkBuddy(source, key.value, work, { ...options, fetchImpl }) : []
656
+ options.signal?.throwIfAborted()
638
657
  const accounts = [...apiAccounts, ...workAccounts]
639
658
  const successes = accounts.filter(account => account.status === 'ok').length
640
659
  const failures = accounts.filter(account => account.status === 'error' || account.status === 'capability_missing').length
package/lib/index.js CHANGED
@@ -1627,6 +1627,7 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
1627
1627
  ctx.effect(() => () => {
1628
1628
  active = false
1629
1629
  qwenLogin?.controller.abort()
1630
+ for (const cache of Object.values(gatewayQuotaCaches)) cache.controller?.abort()
1630
1631
  gatewayQuotaCaches = {}
1631
1632
  resetQueryCache('balance')
1632
1633
  resetQueryCache('goQuota')
@@ -1697,6 +1698,7 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
1697
1698
  next[source.id] = previous
1698
1699
  }
1699
1700
  }
1701
+ for (const [id, cache] of Object.entries(gatewayQuotaCaches)) if (next[id] !== cache) cache.controller?.abort()
1700
1702
  gatewayQuotaCaches = next
1701
1703
  }
1702
1704
  const resolveGatewayKeyConfigured = async (serviceCtx, source) => {
@@ -2247,8 +2249,8 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
2247
2249
  if (!currentSource()) return emptyGatewayForSource(source)
2248
2250
  let cache = gatewayQuotaCaches[id]
2249
2251
  if (cache === undefined || cache.configFingerprint !== configFingerprint || cache.keyConfigured !== configured) {
2250
- // Preserve only a valid LKG when config/key identity is unchanged. A changed
2251
- // source starts empty; an old in-flight task is intentionally detached.
2252
+ // A changed source cancels its old request and starts with an empty cache.
2253
+ cache?.controller?.abort()
2252
2254
  cache = { fetchedAt: 0, value: emptyGatewayForSource(source), lastGood: null, sourceFingerprint: fingerprint,
2253
2255
  configFingerprint, keyConfigured: configured }
2254
2256
  gatewayQuotaCaches[id] = cache
@@ -2267,7 +2269,8 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
2267
2269
  cache = gatewayQuotaCaches[id]
2268
2270
  if (cache === undefined || cache.inFlight !== undefined) return ensureGatewayQuota(source, true)
2269
2271
  }
2270
- const task = queryGatewayQuota(ctx, source).then(value => {
2272
+ const controller = new AbortController()
2273
+ const task = queryGatewayQuota(ctx, source, { signal: controller.signal }).then(value => {
2271
2274
  const active = gatewayQuotaCaches[id]
2272
2275
  if (active?.inFlight !== task || active.sourceFingerprint !== fingerprint) return value
2273
2276
  const ok = gatewayValueIsLastGood(value)
@@ -2288,11 +2291,11 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
2288
2291
  return next
2289
2292
  }).finally(() => {
2290
2293
  if (gatewayQuotaCaches[id]?.inFlight === task) {
2291
- const { inFlight: _drop, ...rest } = gatewayQuotaCaches[id]
2294
+ const { inFlight: _drop, controller: _controller, ...rest } = gatewayQuotaCaches[id]
2292
2295
  gatewayQuotaCaches[id] = rest
2293
2296
  }
2294
2297
  })
2295
- gatewayQuotaCaches[id] = { ...cache, inFlight: task, sourceFingerprint: fingerprint }
2298
+ gatewayQuotaCaches[id] = { ...cache, inFlight: task, controller, sourceFingerprint: fingerprint }
2296
2299
  return task
2297
2300
  }
2298
2301
 
@@ -2726,7 +2729,7 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
2726
2729
  // 的下一次刷新才会真正带上新密钥(否则 15 分钟内仍展示旧值/旧错误)。
2727
2730
  if (customVar !== null) {
2728
2731
  invalidateCustomBalanceCachesForVar(customVar)
2729
- for (const source of gatewaySourceConfigs()) if (managementKeyVarOf(source) === customVar) delete gatewayQuotaCaches[source.id]
2732
+ for (const source of gatewaySourceConfigs()) if (managementKeyVarOf(source) === customVar) { gatewayQuotaCaches[source.id]?.controller?.abort(); delete gatewayQuotaCaches[source.id] }
2730
2733
  }
2731
2734
  else resetQueryCache(key)
2732
2735
  if (key === 'balance') {
@@ -2772,7 +2775,7 @@ function createService(ctx, ledger, openRouterPrices, openDesignPrices, codexSub
2772
2775
  }
2773
2776
  if (customVar !== null) {
2774
2777
  invalidateCustomBalanceCachesForVar(customVar)
2775
- for (const source of gatewaySourceConfigs()) if (managementKeyVarOf(source) === customVar) delete gatewayQuotaCaches[source.id]
2778
+ for (const source of gatewaySourceConfigs()) if (managementKeyVarOf(source) === customVar) { gatewayQuotaCaches[source.id]?.controller?.abort(); delete gatewayQuotaCaches[source.id] }
2776
2779
  }
2777
2780
  else resetQueryCache(key)
2778
2781
  if (key === 'balance') {
@@ -74,9 +74,15 @@ export async function archiveSealedJournals(root, { maxFiles = 16, minAgeMs = 30
74
74
  const temp = path + '.' + randomUUID() + '.tmp'
75
75
  try {
76
76
  await pipeline(createReadStream(path), createGzip(), createWriteStream(temp, { flags: 'wx', mode: 0o600 }))
77
+ const handle = await open(temp, 'r+')
78
+ try { await handle.sync() } finally { await handle.close() }
77
79
  const after = await lstat(path)
78
- if (after.isSymbolicLink() || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ino !== after.ino) continue
80
+ if (after.isSymbolicLink() || before.size !== after.size || before.mtimeMs !== after.mtimeMs || before.ctimeMs !== after.ctimeMs || before.ino !== after.ino) continue
79
81
  await rename(temp, path + '.gz')
82
+ if (process.platform !== 'win32') {
83
+ const directory = await open(dir, 'r')
84
+ try { await directory.sync() } finally { await directory.close() }
85
+ }
80
86
  await unlink(path)
81
87
  archived++
82
88
  } finally { await unlink(temp).catch(error => { if (error.code !== 'ENOENT') throw error }) }
@@ -1,57 +1,57 @@
1
- /** Restore daily amounts from complete recorded session rows, without repricing. */
2
- import { FX_FIELDS } from './exchange-rates.js'
3
- const USAGE_FIELDS = ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning', 'calls']
4
- const valid = value => typeof value === 'number' && Number.isFinite(value) && value >= 0
5
-
6
- export function repairDailySessionTotals(days) {
7
- const repaired = []
8
- for (const [date, day] of Object.entries(days ?? {})) {
9
- const rows = day?.sessions
10
- if (!Array.isArray(rows) || !rows.length || !(day.calls > 0)) continue
11
- const ids = new Set()
12
- const frozenKeys = FX_FIELDS.filter(key => rows.every(row => valid(row?.[key])))
13
- const sums = Object.fromEntries([...USAGE_FIELDS, 'cost', 'apiCost', ...frozenKeys].map(key => [key, 0]))
14
- const models = new Map()
15
- let complete = true
16
- for (const row of rows) {
17
- if (!row?.id || ids.has(row.id)) { complete = false; break }
18
- ids.add(row.id)
19
- for (const key of Object.keys(sums)) {
20
- const value = key === 'apiCost' ? row.apiCost ?? row.cost : row[key] ?? 0
21
- if (!valid(value)) { complete = false; break }
22
- sums[key] += value
23
- }
24
- if (!complete || sums.apiCost > sums.cost + 1e-8) { complete = false; break }
25
- for (const [key, bucket] of Object.entries(row.byProviderModel ?? {})) {
26
- if (models.has(key) && models.get(key) === null) continue
27
- const sum = models.get(key) ?? Object.fromEntries(Object.keys(sums).map(field => [field, 0]))
28
- let usable = bucket !== null && typeof bucket === 'object'
29
- for (const field of Object.keys(sum)) {
30
- if (FX_FIELDS.includes(field) && !valid(bucket?.[field])) { delete sum[field]; continue }
31
- const value = field === 'apiCost' ? bucket?.apiCost ?? bucket?.cost : bucket?.[field] ?? 0
32
- if (!valid(value)) { usable = false; break }
33
- sum[field] += value
34
- }
35
- models.set(key, usable ? sum : null)
36
- }
37
- }
38
- // Anonymous usage or previously pruned details have a usage/call remainder.
39
- // Preserve that remainder and its money instead of inventing attribution.
40
- if (!complete || !Object.values(sums).every(valid) || !USAGE_FIELDS.every(key => valid(day[key] ?? 0) && sums[key] === (day[key] ?? 0))) continue
41
- let changed = Math.abs(sums.cost - (day.cost ?? 0)) > 1e-8 || Math.abs(sums.apiCost - (day.apiCost ?? day.cost ?? 0)) > 1e-8
42
- if (changed) { day.cost = sums.cost; day.apiCost = sums.apiCost }
43
- for (const key of frozenKeys) if (Math.abs(sums[key] - (day[key] ?? 0)) > 1e-8) { day[key] = sums[key]; changed = true }
44
- for (const [key, sum] of models) {
45
- const bucket = day.byProviderModel?.[key]
46
- if (!sum || !bucket || !sum.calls || !Object.values(sum).every(valid) || !USAGE_FIELDS.every(field => sum[field] === (bucket[field] ?? 0))) continue
47
- if (Math.abs(sum.cost - (bucket.cost ?? 0)) <= 1e-8 && Math.abs(sum.apiCost - (bucket.apiCost ?? bucket.cost ?? 0)) <= 1e-8
48
- && frozenKeys.every(key => !valid(sum[key]) || Math.abs(sum[key] - (bucket[key] ?? 0)) <= 1e-8)) continue
49
- for (const key of frozenKeys) if (valid(sum[key])) bucket[key] = sum[key]
50
- bucket.cost = sum.cost
51
- bucket.apiCost = sum.apiCost
52
- changed = true
53
- }
54
- if (changed) repaired.push(date)
55
- }
56
- return repaired
57
- }
1
+ /** Restore daily amounts from complete recorded session rows, without repricing. */
2
+ import { FX_FIELDS } from './exchange-rates.js'
3
+ const USAGE_FIELDS = ['input', 'output', 'cacheRead', 'cacheWrite', 'reasoning', 'calls']
4
+ const valid = value => typeof value === 'number' && Number.isFinite(value) && value >= 0
5
+
6
+ export function repairDailySessionTotals(days) {
7
+ const repaired = []
8
+ for (const [date, day] of Object.entries(days ?? {})) {
9
+ const rows = day?.sessions
10
+ if (!Array.isArray(rows) || !rows.length || !(day.calls > 0)) continue
11
+ const ids = new Set()
12
+ const frozenKeys = FX_FIELDS.filter(key => rows.every(row => valid(row?.[key])))
13
+ const sums = Object.fromEntries([...USAGE_FIELDS, 'cost', 'apiCost', ...frozenKeys].map(key => [key, 0]))
14
+ const models = new Map()
15
+ let complete = true
16
+ for (const row of rows) {
17
+ if (!row?.id || ids.has(row.id)) { complete = false; break }
18
+ ids.add(row.id)
19
+ for (const key of Object.keys(sums)) {
20
+ const value = key === 'apiCost' ? row.apiCost ?? row.cost : row[key] ?? 0
21
+ if (!valid(value)) { complete = false; break }
22
+ sums[key] += value
23
+ }
24
+ if (!complete || sums.apiCost > sums.cost + 1e-8) { complete = false; break }
25
+ for (const [key, bucket] of Object.entries(row.byProviderModel ?? {})) {
26
+ if (models.has(key) && models.get(key) === null) continue
27
+ const sum = models.get(key) ?? Object.fromEntries(Object.keys(sums).map(field => [field, 0]))
28
+ let usable = bucket !== null && typeof bucket === 'object'
29
+ for (const field of Object.keys(sum)) {
30
+ if (FX_FIELDS.includes(field) && !valid(bucket?.[field])) { delete sum[field]; continue }
31
+ const value = field === 'apiCost' ? bucket?.apiCost ?? bucket?.cost : bucket?.[field] ?? 0
32
+ if (!valid(value)) { usable = false; break }
33
+ sum[field] += value
34
+ }
35
+ models.set(key, usable ? sum : null)
36
+ }
37
+ }
38
+ // Anonymous usage or previously pruned details have a usage/call remainder.
39
+ // Preserve that remainder and its money instead of inventing attribution.
40
+ if (!complete || !Object.values(sums).every(valid) || !USAGE_FIELDS.every(key => valid(day[key] ?? 0) && sums[key] === (day[key] ?? 0))) continue
41
+ let changed = Math.abs(sums.cost - (day.cost ?? 0)) > 1e-8 || Math.abs(sums.apiCost - (day.apiCost ?? day.cost ?? 0)) > 1e-8
42
+ if (changed) { day.cost = sums.cost; day.apiCost = sums.apiCost }
43
+ for (const key of frozenKeys) if (Math.abs(sums[key] - (day[key] ?? 0)) > 1e-8) { day[key] = sums[key]; changed = true }
44
+ for (const [key, sum] of models) {
45
+ const bucket = day.byProviderModel?.[key]
46
+ if (!sum || !bucket || !sum.calls || !Object.values(sum).every(valid) || !USAGE_FIELDS.every(field => sum[field] === (bucket[field] ?? 0))) continue
47
+ if (Math.abs(sum.cost - (bucket.cost ?? 0)) <= 1e-8 && Math.abs(sum.apiCost - (bucket.apiCost ?? bucket.cost ?? 0)) <= 1e-8
48
+ && frozenKeys.every(key => !valid(sum[key]) || Math.abs(sum[key] - (bucket[key] ?? 0)) <= 1e-8)) continue
49
+ for (const key of frozenKeys) if (valid(sum[key])) bucket[key] = sum[key]
50
+ bucket.cost = sum.cost
51
+ bucket.apiCost = sum.apiCost
52
+ changed = true
53
+ }
54
+ if (changed) repaired.push(date)
55
+ }
56
+ return repaired
57
+ }