dsh-vertex-argon 1.5.2 → 1.5.3

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/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  All notable changes to `dsh-vertex-argon` are documented in this file.
4
4
 
5
+ ## [1.5.3] - 2026-10-24
6
+
7
+ ### Fixed
8
+ - **Pre-Checkpoint Real-Time Streaming & Fast-Fail Outer Retry Handoff (`generate-content.js`)**:
9
+ - Streamed incoming `GenerateContent` SSE chunks in real time before the first 32,768-token `continuationToken` checkpoint is acquired (`!token`), eliminating buffering delay on thought summaries, text, and tool calls for `< 32K` turns when `longDecode` is enabled.
10
+ - Surfaced pre-checkpoint stream drops and errors immediately to the DSH outer retry handler on the first hop instead of silently looping 4 internal hops without a checkpoint token, while preserving `pending` checkpoint buffering and internal `continuationToken` resume once a 32K+ checkpoint is active.
11
+ - **Inspector Usage Ledger, Cache Coverage & Hop Accounting (`usage-details.js`, `pricing.js`, `inspector-overview.js`, `client.js`, `generate-content.js`)**:
12
+ - Added `selectGcUsageRows` to strip contiguous unanswered pre-progress `retry-original` hops when a subsequent hop completes, keeping cost and token completeness accurate across historical and multi-hop records.
13
+ - Computed weighted cache-hit percentage across requests with complete valid cache/input counts and recorded `hopTracker.onChunk({ status: 200 })` before throwing on malformed or non-JSON SSE chunks.
14
+
5
15
  ## [1.5.2] - 2026-10-22
6
16
 
7
17
  ### Fixed
package/client.js CHANGED
@@ -614,7 +614,7 @@ window.__ModuleLoader__.load({
614
614
  inspectorOverviewCoverage: '已记录请求', inspectorOverviewCoverageComplete: '完整请求', inspectorOverviewPartial: '部分记录;不是完整总量',
615
615
  inspectorOverviewDenied: '无总览读取权限;请求列表仍可单独查看。', inspectorOverviewWindow: '当前本地保留窗口,非全历史或云账单;最多扫描最近 200 条,再按当前范围取最多 100 条。',
616
616
  inspectorOverviewLimited: '当前范围匹配记录超过显示上限,较早记录不含在内。', inspectorOverviewDates: '记录时间范围',
617
- inspectorOverviewCache: '按输入 Token 加权;仅全部请求的缓存与输入均完整有效时展示。',
617
+ inspectorOverviewCache: '按完整有效请求的输入 Token 加权计算。',
618
618
  inspectorOverviewCost: '已知费用小计(USD)', inspectorOverviewCostComplete: '完整费用估算(USD)',
619
619
  inspectorOverviewFields: '各项 Token 合计',
620
620
  })
@@ -652,7 +652,7 @@ window.__ModuleLoader__.load({
652
652
  inspectorOverviewCoverage: 'Reported requests', inspectorOverviewCoverageComplete: 'Complete requests', inspectorOverviewPartial: 'Partial records; not a complete total',
653
653
  inspectorOverviewDenied: 'No permission to read the overview; individual requests remain available in the list.', inspectorOverviewWindow: 'Current local retained window, not all history or cloud billing; scans at most the latest 200 records, then takes up to 100 for this scope.',
654
654
  inspectorOverviewLimited: 'Matching retained records exceed the display limit; earlier records are excluded.', inspectorOverviewDates: 'Record time range',
655
- inspectorOverviewCache: 'Weighted by input tokens; shown only when every request has complete, valid cache/input counts.',
655
+ inspectorOverviewCache: 'Weighted by input tokens across requests with complete, valid cache/input counts.',
656
656
  inspectorOverviewCost: 'Known cost subtotal (USD)', inspectorOverviewCostComplete: 'Complete cost estimate (USD)',
657
657
  inspectorOverviewFields: 'Token totals by field',
658
658
  })
@@ -1123,9 +1123,11 @@ window.__ModuleLoader__.load({
1123
1123
  const OVERVIEW_CSS = '\n.argon-inspector .ag-scope-overview{margin:14px 0}.argon-inspector .ag-scope-overview .ag-overview{grid-template-columns:repeat(auto-fit,minmax(min(100%,180px),1fr));margin:12px 0}.argon-inspector .ag-overview-fields{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,140px),1fr));gap:14px;margin:12px 0}.argon-inspector .ag-overview-fields dd{margin:4px 0;font-variant-numeric:tabular-nums;font-size:17px}.argon-inspector .ag-overview-fields small{display:block;font-size:11px;color:var(--dsw-alias-label-secondary)}'
1124
1124
  function ScopeOverview({ overview, t, metric, busy, allowed }) {
1125
1125
  const coverage = value => t('inspectorOverviewCoverage') + ': ' + (value?.reportedRequests ?? 0) + '/' + overview.requestCount + ' · ' + t('inspectorOverviewCoverageComplete') + ': ' + (value?.completeRequests ?? 0) + '/' + overview.requestCount
1126
- const total = overview?.tokens?.totalTokens, cost = overview?.cost
1126
+ const cacheCoverage = () => t('inspectorOverviewCoverageComplete') + ': ' + (overview?.cache?.completeRequests ?? 0) + '/' + overview.requestCount + ' · ' + t('inspectorOverviewCache')
1127
+ const total = overview?.tokens?.totalTokens, cost = overview?.cost, cache = overview?.cache
1127
1128
  const money = value => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? '$' + value.toFixed(6) : '—'
1128
1129
  const completeCost = cost?.estimatedUsd != null
1130
+ const completeCache = overview?.requestCount > 0 && cache?.completeRequests === overview.requestCount
1129
1131
  const fields = [['inputTokens', 'inspectorInput'], ['cachedTokens', 'inspectorTokenCached'], ['outputTokens', 'inspectorTokenOutput'], ['thoughtTokens', 'inspectorTokenReasoning'], ['toolTokens', 'inspectorTokenTool']]
1130
1132
  return h('section', { className: 'card ag-scope-overview', 'data-overview': true, 'aria-label': t('inspectorOverview') },
1131
1133
  h('h3', null, t('inspectorOverview')),
@@ -1134,7 +1136,7 @@ window.__ModuleLoader__.load({
1134
1136
  overview.window?.limited ? h('p', { className: 'ag-notice' }, t('inspectorOverviewLimited')) : null,
1135
1137
  h('div', { className: 'ag-overview' },
1136
1138
  metric('overview-total', t('inspectorTokenTotal') + ' · ' + t(total?.complete ? 'inspectorOverviewComplete' : 'inspectorOverviewKnown'), tokenText(total?.knownSubtotal), coverage(total)),
1137
- metric('overview-cache', t('inspectorCacheHitRate'), typeof overview.cache?.percent === 'number' && Number.isFinite(overview.cache.percent) ? overview.cache.percent.toFixed(1) + '%' : '—', t('inspectorOverviewCache')),
1139
+ metric('overview-cache', t('inspectorCacheHitRate') + (typeof cache?.percent === 'number' && Number.isFinite(cache.percent) && !completeCache ? ' · ' + t('inspectorOverviewKnown') : ''), typeof cache?.percent === 'number' && Number.isFinite(cache.percent) ? cache.percent.toFixed(1) + '%' : '—', cacheCoverage()),
1138
1140
  metric('overview-cost', t(completeCost ? 'inspectorOverviewCostComplete' : 'inspectorOverviewCost'), money(completeCost ? cost.estimatedUsd : cost?.knownSubtotalUsd), coverage(cost))),
1139
1141
  h('dl', { className: 'ag-overview-fields', 'aria-label': t('inspectorOverviewFields') }, ...fields.map(([key, label]) => {
1140
1142
  const value = overview.tokens?.[key]
@@ -1147,27 +1149,67 @@ window.__ModuleLoader__.load({
1147
1149
 
1148
1150
  const knownTokens = value => Number.isSafeInteger(value) && value >= 0
1149
1151
  const tokenText = value => knownTokens(value) ? numberFormatter.format(value) : '—'
1152
+ function resolveUsageCounts(usage, protocol) {
1153
+ if (!inspectorObject(usage)) return null
1154
+ const invalid = new Set(Array.isArray(usage.invalidCounts) ? usage.invalidCounts : [])
1155
+ const keys = ['inputTokens', 'cachedTokens', 'outputTokens', 'thoughtTokens', 'toolTokens', 'totalTokens']
1156
+ const result = Object.fromEntries(keys.map(key => [key, invalid.has(key) ? null : (knownTokens(usage[key]) ? usage[key] : null)]))
1157
+ const absent = key => usage[key] == null && !invalid.has(key)
1158
+ if (result.inputTokens !== null) {
1159
+ for (const key of ['cachedTokens', 'toolTokens']) if (absent(key)) result[key] = 0
1160
+ }
1161
+ if (protocol === 'generate-content' && absent('outputTokens') && result.inputTokens !== null && result.totalTokens !== null && result.toolTokens !== null) {
1162
+ const thoughts = absent('thoughtTokens') ? 0 : result.thoughtTokens
1163
+ const accounted = thoughts === null ? null : result.inputTokens + result.toolTokens + thoughts
1164
+ if (accounted !== null && knownTokens(accounted) && accounted === result.totalTokens) result.outputTokens = 0
1165
+ }
1166
+ if (result.outputTokens !== null && absent('thoughtTokens')) result.thoughtTokens = 0
1167
+ return result
1168
+ }
1169
+ function selectGcUsageRows(record) {
1170
+ const details = record?.usageDetails
1171
+ const allRows = (details?.rows ?? []).filter(row => row?.kind === 'gc-hop')
1172
+ const ledgerValid = allRows.length > 0 && allRows.length === record?.hopCount && !details?.omittedRows
1173
+ && allRows.every(row => knownTokens(row.index)) && new Set(allRows.map(row => row.index)).size === allRows.length
1174
+ if (!ledgerValid) return { rows: allRows, ledgerValid: false }
1175
+ const sorted = [...allRows].sort((a, b) => a.index - b.index)
1176
+ const hops = Array.isArray(record?.hops) && !record?.hopsDropped ? record.hops : []
1177
+ const byIndex = new Map(hops.filter(inspectorObject).map(hop => [hop.hopIndex, hop]))
1178
+ let start = 0
1179
+ if (['done', 'max-tokens', 'tool-calls'].includes(record?.outcome) && sorted.every((row, i) => row.index === i + 1)) {
1180
+ while (start + 1 < sorted.length) {
1181
+ const row = sorted[start], hop = byIndex.get(row.index), next = byIndex.get(sorted[start + 1].index)
1182
+ if (row.usage === null && row.complete === false
1183
+ && hop && hop.trigger === (start === 0 ? 'initial' : 'retry-original')
1184
+ && hop.usedContinuationToken === false && hop.chunkCount === 0 && hop.hasCheckpoint === false
1185
+ && ['transport-error', 'api-error'].includes(hop.outcome)
1186
+ && next && next.trigger === 'retry-original' && next.usedContinuationToken === false) start++
1187
+ else break
1188
+ }
1189
+ }
1190
+ return { rows: start > 0 ? sorted.slice(start) : allRows, ledgerValid: true }
1191
+ }
1150
1192
  function extractTokenMix(record) {
1151
1193
  const details = record?.usageDetails
1152
1194
  const keys = ['inputTokens', 'cachedTokens', 'outputTokens', 'thoughtTokens', 'toolTokens', 'totalTokens']
1153
1195
  let usage = null, complete = false, sourceCacheValid = true, sourceTotalsValid = true
1154
1196
  if (record?.protocol === 'generate-content') {
1155
- const rows = (details?.rows ?? []).filter(row => row.kind === 'gc-hop')
1156
- const ledger = rows.length > 0 && rows.length === record.hopCount && !details.omittedRows && new Set(rows.map(row => row.index)).size === rows.length
1197
+ const { rows, ledgerValid: ledger } = selectGcUsageRows(record)
1198
+ const usages = rows.map(row => resolveUsageCounts(row.usage, 'generate-content'))
1157
1199
  usage = Object.fromEntries(keys.map(key => {
1158
- const values = rows.map(row => row.usage?.[key])
1200
+ const values = usages.map(u => u?.[key])
1159
1201
  const sum = ledger && values.every(knownTokens) ? values.reduce((a, b) => a + b, 0) : null
1160
1202
  return [key, knownTokens(sum) ? sum : null]
1161
1203
  }))
1162
- sourceCacheValid = rows.every(row => knownTokens(row.usage?.inputTokens) && knownTokens(row.usage?.cachedTokens) && row.usage.cachedTokens <= row.usage.inputTokens)
1163
- sourceTotalsValid = rows.every(row => {
1164
- const parts = ['inputTokens', 'outputTokens', 'thoughtTokens', 'toolTokens'].map(key => row.usage?.[key])
1165
- return knownTokens(row.usage?.totalTokens) && parts.every(value => value == null || knownTokens(value))
1166
- && parts.filter(knownTokens).reduce((sum, value) => sum + value, 0) === row.usage.totalTokens
1204
+ sourceCacheValid = usages.every(u => knownTokens(u?.inputTokens) && knownTokens(u?.cachedTokens) && u.cachedTokens <= u.inputTokens)
1205
+ sourceTotalsValid = usages.every(u => {
1206
+ const parts = ['inputTokens', 'outputTokens', 'thoughtTokens', 'toolTokens'].map(key => u?.[key])
1207
+ return knownTokens(u?.totalTokens) && parts.every(value => value == null || knownTokens(value))
1208
+ && parts.filter(knownTokens).reduce((sum, value) => sum + value, 0) === u.totalTokens
1167
1209
  })
1168
1210
  complete = ledger && rows.every(row => row.complete === true)
1169
1211
  } else if (record?.protocol === 'interactions') {
1170
- usage = details?.aggregate
1212
+ usage = resolveUsageCounts(details?.aggregate, 'interactions')
1171
1213
  complete = details?.aggregateComplete === true
1172
1214
  }
1173
1215
  const read = key => knownTokens(usage?.[key]) ? usage[key] : null
@@ -1275,10 +1317,11 @@ window.__ModuleLoader__.load({
1275
1317
  h('div', { className: 'ag-subhead' }, t('inspectorStepHopTable') + ' (' + rows.length + ')'),
1276
1318
  h('div', { className: 'ag-hop-list' },
1277
1319
  ...rows.slice(0, 12).map((r, idx) => {
1278
- const pTok = r.usage?.inputTokens
1279
- const oTok = r.usage?.outputTokens
1280
- const tTok = r.usage?.thoughtTokens
1281
- const tot = r.usage?.totalTokens
1320
+ const resolved = resolveUsageCounts(r.usage, r.kind === 'interaction-step' ? 'interactions' : 'generate-content')
1321
+ const pTok = resolved?.inputTokens
1322
+ const oTok = resolved?.outputTokens
1323
+ const tTok = resolved?.thoughtTokens
1324
+ const tot = resolved?.totalTokens
1282
1325
  return h('div', { key: idx, className: 'ag-hop-row' },
1283
1326
  h('span', { className: 'ag-pill ag-pill-muted' }, (r.kind ?? 'hop') + ' #' + (r.index ?? idx)),
1284
1327
  h('span', { className: 'ag-hop-nums' }, t('inspectorInput') + ' ' + tokenText(pTok) + ' · ' + t('inspectorTokenOutput') + ' ' + tokenText(oTok) + ' · ' + t('inspectorTokenReasoning') + ' ' + tokenText(tTok)),
@@ -1,8 +1,8 @@
1
1
  // GenerateContent manual continuation contract (reviewed 2026-10-16):
2
2
  // https://ai.google.dev/gemini-api/docs/generate-content/long-decode#option-2-loop
3
3
  // https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/guides/gemini-4-argon
4
- // Only checkpoint-confirmed chunks may escape a resumable stream. Never append
5
- // partial output to the request, decrease its total budget, or retry committed text.
4
+ // Stream chunks directly before the first 32K checkpoint token; once a checkpoint
5
+ // is active, only checkpoint-confirmed chunks escape. Never append partial output.
6
6
  import { SERVICE_TIERS, serviceTierFromHeader, serviceTierFromTraffic } from './service-level.js'
7
7
  import { classifyRequestRegion } from './pricing.js'
8
8
  export const MAX_CONTINUATION_HOPS = 32
@@ -184,9 +184,18 @@ export async function continueArgonRequestStream(apiClient, original, request, m
184
184
  const stream = await original.call(apiClient, { ...request, body, httpOptions })
185
185
  for await (const chunk of stream) {
186
186
  throwIfAborted(request.abortSignal)
187
- const raw = await chunk.json()
187
+ let raw
188
+ try {
189
+ raw = await chunk.json()
190
+ } catch (error) {
191
+ hopTracker?.onChunk({ status: 200 })
192
+ throw error
193
+ }
188
194
  tracker?.observeResponse?.('gc-chunk', hop, raw)
189
- if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw streamError('Invalid Argon stream response.', 'ARGON_INVALID_RESPONSE')
195
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
196
+ hopTracker?.onChunk({ status: 200 })
197
+ throw streamError('Invalid Argon stream response.', 'ARGON_INVALID_RESPONSE')
198
+ }
190
199
  if (Object.hasOwn(raw, 'error')) {
191
200
  hopTracker?.onChunk({ status: 200 })
192
201
  throw inBandStreamError(raw.error)
@@ -217,6 +226,11 @@ export async function continueArgonRequestStream(apiClient, original, request, m
217
226
  yield wrap(raw, chunk.headers, latestUsage)
218
227
  continue
219
228
  }
229
+ if (!token && !newCheckpoint && !terminal && candidate?.finishReason !== 'CONTINUATION') {
230
+ refreshOriginalUsage()
231
+ yield wrap(raw, chunk.headers, latestUsage)
232
+ continue
233
+ }
220
234
  pendingBytes += Buffer.byteLength(JSON.stringify(raw))
221
235
  if (pendingBytes > MAX_PENDING_BYTES) throw streamError('Argon uncommitted stream exceeded the bounded checkpoint buffer.', 'ARGON_BUFFER_LIMIT')
222
236
  pending.push({ raw, headers: chunk.headers, usage: latestUsage ? { ...latestUsage } : undefined })
@@ -269,9 +283,9 @@ export async function continueArgonRequestStream(apiClient, original, request, m
269
283
  if (!failure && finishReason === 'CONTINUATION' && (!token || token === startToken)) {
270
284
  failure = streamError('Argon CONTINUATION did not provide a new checkpoint token.', 'ARGON_INVALID_CONTINUATION')
271
285
  }
272
- if (failure && !retryable(failure)) {
286
+ if (!token || (failure && !retryable(failure))) {
273
287
  hopTracker?.finish({ error: failure, aborted: false, finishReason, continued: false })
274
- throw failure
288
+ throw failure ?? streamError('Argon stream ended without a terminal finishReason.')
275
289
  }
276
290
  const progress = token !== startToken
277
291
  const cleanContinuation = finishReason === 'CONTINUATION' && !failure && progress
@@ -1,5 +1,5 @@
1
1
  import {estimateDiagnosticCost} from './pricing.js'
2
- import {USAGE_COUNT_KEYS,resolveUsageCounts} from './usage-details.js'
2
+ import {USAGE_COUNT_KEYS,resolveUsageCounts,selectGcUsageRows} from './usage-details.js'
3
3
 
4
4
  const knownCount=value=>Number.isSafeInteger(value)&&value>=0
5
5
  const knownMoney=value=>typeof value==='number'&&Number.isFinite(value)&&value>=0&&value<=Number.MAX_SAFE_INTEGER
@@ -18,9 +18,8 @@ function requestSources(record) {
18
18
  const details=record.usageDetails
19
19
  if(!details)return {rows:[],complete:false}
20
20
  if(record.protocol==='generate-content') {
21
- const rows=(details.rows??[]).filter(row=>row.kind==='gc-hop')
22
- const complete=finalOutcome(record.outcome)&&rows.length>0&&rows.length===record.hopCount&&details.omittedRows===0
23
- &&rows.every(row=>row.complete===true&&knownCount(row.index))&&new Set(rows.map(row=>row.index)).size===rows.length
21
+ const {rows,ledgerValid}=selectGcUsageRows(record)
22
+ const complete=finalOutcome(record.outcome)&&ledgerValid&&rows.every(row=>row.complete===true)
24
23
  return {rows,complete}
25
24
  }
26
25
  if(record.protocol==='interactions')return {rows:[{usage:details.aggregate}],complete:finalOutcome(record.outcome)&&details.aggregateComplete===true}
@@ -35,12 +34,12 @@ function requestSummary(record) {
35
34
  const subtotal=sumKnown(known,knownCount)
36
35
  return [key,{subtotal,reported:known.length>0,complete:complete&&known.length===rows.length&&subtotal!==null}]
37
36
  }))
38
- const cacheComplete=tokens.inputTokens.complete&&tokens.cachedTokens.complete
39
- &&usages.every(usage=>usage.cachedTokens<=usage.inputTokens)
37
+ const cacheInvalid=usages.some(usage=>knownCount(usage?.inputTokens)&&knownCount(usage?.cachedTokens)&&usage.cachedTokens>usage.inputTokens)
38
+ const cacheComplete=tokens.inputTokens.complete&&tokens.cachedTokens.complete&&!cacheInvalid
40
39
  // Keep pricing in one place; a duplicate/invalid hop ledger still cannot be
41
40
  // promoted to a complete estimate even if its individual rows are priced.
42
41
  const cost=['generate-content','interactions'].includes(record.protocol)?estimateDiagnosticCost(record):null
43
- return {tokens,cacheComplete,cost, costComplete:complete&&knownMoney(cost?.estimatedUsd)}
42
+ return {tokens,cacheInvalid,cacheComplete,cost, costComplete:complete&&knownMoney(cost?.estimatedUsd)}
44
43
  }
45
44
 
46
45
  // The caller supplies filtered, bounded records, verified when answer-scoped.
@@ -53,9 +52,13 @@ export function summarizeRequestOverview(records,window={limit:20,limited:false}
53
52
  const completeRequests=requests.filter(request=>request.tokens[key].complete).length
54
53
  return [key,{knownSubtotal,reportedRequests:reported.length,completeRequests,complete:requestCount>0&&completeRequests===requestCount&&knownSubtotal!==null}]
55
54
  }))
56
- const cacheCompleteRequests=requests.filter(request=>request.cacheComplete).length
57
- const percent=requestCount>0&&cacheCompleteRequests===requestCount&&tokens.inputTokens.complete&&tokens.cachedTokens.complete&&tokens.inputTokens.knownSubtotal>0
58
- ?tokens.cachedTokens.knownSubtotal/tokens.inputTokens.knownSubtotal*100:null
55
+ const eligibleCache=requests.filter(request=>request.cacheComplete)
56
+ const cacheCompleteRequests=eligibleCache.length
57
+ const eligibleInput=sumKnown(eligibleCache.map(request=>request.tokens.inputTokens.subtotal),knownCount)
58
+ const eligibleCached=sumKnown(eligibleCache.map(request=>request.tokens.cachedTokens.subtotal),knownCount)
59
+ const cacheOverflow=requests.some(request=>(request.tokens.inputTokens.reported&&request.tokens.inputTokens.subtotal===null)||(request.tokens.cachedTokens.reported&&request.tokens.cachedTokens.subtotal===null))
60
+ const percent=cacheCompleteRequests>0&&!requests.some(request=>request.cacheInvalid)&&!cacheOverflow&&eligibleInput!==null&&eligibleCached!==null&&eligibleInput>0
61
+ ?eligibleCached/eligibleInput*100:null
59
62
  const reportedCosts=requests.filter(request=>knownMoney(request.cost?.knownSubtotalUsd))
60
63
  const costSum=sumKnown(reportedCosts.map(request=>request.cost.knownSubtotalUsd),knownMoney)
61
64
  // A numeric but unrepresentable source cost must not silently disappear.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-vertex-argon",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "Gemini API and Vertex AI Argon for DSH: long decode, explicit service tiers, response and usage inspection, and estimated costs.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/pricing.js CHANGED
@@ -1,4 +1,4 @@
1
- import { resolveUsageCounts } from './usage-details.js'
1
+ import { resolveUsageCounts, selectGcUsageRows } from './usage-details.js'
2
2
 
3
3
  // Public price snapshots, not account billing data. All amounts are USD per 1M tokens.
4
4
  export const PRICE_SNAPSHOT = Object.freeze({
@@ -74,9 +74,10 @@ export function estimateDiagnosticCost(record) {
74
74
  }
75
75
  if(!details) missing.push('usage-details')
76
76
  else if(gc) {
77
- for(const row of details.rows)if(row.kind==='gc-hop')estimate(row.usage,row.complete,row.observedServiceTier,row.index)
77
+ const {rows:gcRows,ledgerValid}=selectGcUsageRows(record)
78
+ for(const row of gcRows)estimate(row.usage,row.complete,row.observedServiceTier,row.index)
78
79
  if(details.omittedRows)missing.push('omitted-usage-rows')
79
- if(rows.length!==record.hopCount||rows.length===0)missing.push('incomplete-hop-ledger')
80
+ if(!ledgerValid||rows.length===0)missing.push('incomplete-hop-ledger')
80
81
  } else estimate(details.aggregate,details.aggregateComplete,record.serviceTier?.observed,0)
81
82
  if(!['done','max-tokens','tool-calls'].includes(record.outcome))missing.push('request-not-complete')
82
83
  for(const row of rows)for(const value of row.missing)if(!missing.includes(value))missing.push(value)
package/usage-details.js CHANGED
@@ -27,6 +27,30 @@ export function resolveUsageCounts(usage, protocol) {
27
27
  return result
28
28
  }
29
29
 
30
+ export function selectGcUsageRows(record) {
31
+ const details = record?.usageDetails
32
+ const allRows = (details?.rows ?? []).filter(row => row?.kind === 'gc-hop')
33
+ const ledgerValid = allRows.length > 0 && allRows.length === record?.hopCount && details?.omittedRows === 0
34
+ && allRows.every(row => count(row.index) !== null) && new Set(allRows.map(row => row.index)).size === allRows.length
35
+ if (!ledgerValid) return { rows: allRows, ledgerValid: false }
36
+ const sorted = [...allRows].sort((a, b) => a.index - b.index)
37
+ const hops = Array.isArray(record?.hops) && !record?.hopsDropped ? record.hops : []
38
+ const byIndex = new Map(hops.filter(object).map(hop => [hop.hopIndex, hop]))
39
+ let start = 0
40
+ if (['done', 'max-tokens', 'tool-calls'].includes(record?.outcome) && sorted.every((row, i) => row.index === i + 1)) {
41
+ while (start + 1 < sorted.length) {
42
+ const row = sorted[start], hop = byIndex.get(row.index), next = byIndex.get(sorted[start + 1].index)
43
+ if (row.usage === null && row.complete === false
44
+ && hop && hop.trigger === (start === 0 ? 'initial' : 'retry-original')
45
+ && hop.usedContinuationToken === false && hop.chunkCount === 0 && hop.hasCheckpoint === false
46
+ && ['transport-error', 'api-error'].includes(hop.outcome)
47
+ && next && next.trigger === 'retry-original' && next.usedContinuationToken === false) start++
48
+ else break
49
+ }
50
+ }
51
+ return { rows: start > 0 ? sorted.slice(start) : allRows, ledgerValid: true }
52
+ }
53
+
30
54
  function modalities(raw, protocol) {
31
55
  if (!Array.isArray(raw) || raw.length > USAGE_MODALITIES.length) return null
32
56
  const result = [], seen = new Set()