tokenusage-cli 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ const { readSqliteJsonRows, readSqliteJsonRowsAsync } = require("./sqlite-reader
13
13
  const wsl = require("./wsl-probe");
14
14
  const { resolveInstallPaths } = require("./install-resolver");
15
15
  const { expandHome } = require("./scan-roots");
16
+ const { basenameKey } = require("./project-key");
16
17
  const {
17
18
  consumeUsageDelta,
18
19
  createUsageDeltaState,
@@ -778,7 +779,7 @@ async function parseRolloutIncremental({
778
779
  }
779
780
  }
780
781
 
781
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
782
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
782
783
  const projectBucketsQueued = projectEnabled
783
784
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
784
785
  : 0;
@@ -1318,7 +1319,7 @@ async function parseClaudeIncremental({
1318
1319
  }
1319
1320
  }
1320
1321
 
1321
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
1322
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
1322
1323
  const projectBucketsQueued = projectEnabled
1323
1324
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
1324
1325
  : 0;
@@ -1493,7 +1494,7 @@ async function parseGeminiIncremental({
1493
1494
  }
1494
1495
  }
1495
1496
 
1496
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
1497
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
1497
1498
  const projectBucketsQueued = projectEnabled
1498
1499
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
1499
1500
  : 0;
@@ -1644,7 +1645,7 @@ async function parseOpencodeIncremental({
1644
1645
  }
1645
1646
  }
1646
1647
 
1647
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
1648
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
1648
1649
  const projectBucketsQueued = projectEnabled
1649
1650
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
1650
1651
  : 0;
@@ -2005,7 +2006,7 @@ async function parseOpenclawIncremental({
2005
2006
  }
2006
2007
  }
2007
2008
 
2008
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
2009
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
2009
2010
  const projectBucketsQueued = projectEnabled
2010
2011
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
2011
2012
  : 0;
@@ -2655,11 +2656,14 @@ async function parseRolloutRecords({
2655
2656
  projectState,
2656
2657
  event.projectKey,
2657
2658
  source,
2659
+ event.model,
2658
2660
  event.bucketStart,
2659
2661
  event.projectRef,
2660
2662
  );
2661
2663
  addTotals(projectBucket.totals, event.delta);
2662
- projectTouchedBuckets.add(projectBucketKey(event.projectKey, source, event.bucketStart));
2664
+ projectTouchedBuckets.add(
2665
+ projectBucketKey(event.projectKey, source, event.model, event.bucketStart),
2666
+ );
2663
2667
  }
2664
2668
  eventsAggregated += 1;
2665
2669
  }
@@ -2900,8 +2904,8 @@ async function parseRolloutRecords({
2900
2904
 
2901
2905
  const bucket = getHourlyBucket(hourlyState, source, model, bucketStart);
2902
2906
  addTotals(bucket.totals, delta);
2903
- // Only the model bucket gets the subset: the project bucket below carries
2904
- // no model, so no per-model rate can be applied to it anyway.
2907
+ // Priority subsets annotate per-model rate calculations, so both the
2908
+ // aggregate and the (now model-dimensioned) project bucket carry them.
2905
2909
  if (isPriorityServiceTier(serviceTier)) addPriorityUsage(bucket.totals, delta);
2906
2910
  touchedBuckets.add(bucketKey(source, model, bucketStart));
2907
2911
  if (currentProjectKey && projectState && projectTouchedBuckets) {
@@ -2909,11 +2913,13 @@ async function parseRolloutRecords({
2909
2913
  projectState,
2910
2914
  currentProjectKey,
2911
2915
  source,
2916
+ model,
2912
2917
  bucketStart,
2913
2918
  currentProjectRef,
2914
2919
  );
2915
2920
  addTotals(projectBucket.totals, delta);
2916
- projectTouchedBuckets.add(projectBucketKey(currentProjectKey, source, bucketStart));
2921
+ if (isPriorityServiceTier(serviceTier)) addPriorityUsage(projectBucket.totals, delta);
2922
+ projectTouchedBuckets.add(projectBucketKey(currentProjectKey, source, model, bucketStart));
2917
2923
  }
2918
2924
  eventsAggregated += 1;
2919
2925
  }
@@ -3219,11 +3225,12 @@ async function parseClaudeFile({
3219
3225
  projectState,
3220
3226
  projectKey,
3221
3227
  source,
3228
+ model,
3222
3229
  bucketStart,
3223
3230
  projectRef,
3224
3231
  );
3225
3232
  addTotals(projectBucket.totals, delta);
3226
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
3233
+ projectTouchedBuckets.add(projectBucketKey(projectKey, source, model, bucketStart));
3227
3234
  }
3228
3235
  eventsAggregated += 1;
3229
3236
  }
@@ -3340,11 +3347,14 @@ async function parseGeminiFile({
3340
3347
  projectState,
3341
3348
  projectKey,
3342
3349
  source,
3350
+ projectModel,
3343
3351
  bucketStart,
3344
3352
  projectRef,
3345
3353
  );
3346
3354
  addTotals(projectBucket.totals, projectDelta);
3347
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
3355
+ projectTouchedBuckets.add(
3356
+ projectBucketKey(projectKey, source, projectModel, bucketStart),
3357
+ );
3348
3358
  }
3349
3359
  projectTotals = currentTotals;
3350
3360
  }
@@ -3494,11 +3504,12 @@ async function parseOpencodeMessageFile({
3494
3504
  projectState,
3495
3505
  projectKey,
3496
3506
  source,
3507
+ model,
3497
3508
  bucketStart,
3498
3509
  projectRef,
3499
3510
  );
3500
3511
  addTotals(projectBucket.totals, delta);
3501
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
3512
+ projectTouchedBuckets.add(projectBucketKey(projectKey, source, model, bucketStart));
3502
3513
  }
3503
3514
  return {
3504
3515
  messageKey,
@@ -3510,16 +3521,155 @@ async function parseOpencodeMessageFile({
3510
3521
  };
3511
3522
  }
3512
3523
 
3513
- async function enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets }) {
3514
- if (!touchedBuckets || touchedBuckets.size === 0) return 0;
3524
+ // Contract v2 (2026-11-03): hourly rows carry project_key. The hourly bucket
3525
+ // still owns the aggregate for (source, model, hour); a project bucket holds
3526
+ // the attributed subset for the same key. Split mode writes one row per
3527
+ // project plus a residual row under project_key "" (docs/cli-contract.md
3528
+ // §2.4 "project attribution"), so the rows add up to the aggregate and
3529
+ // nothing is double counted. Only model-dimensioned buckets take part: legacy
3530
+ // three-part buckets (project|source|hour) cannot be matched to a model and
3531
+ // stay inside the residual.
3532
+ const UNATTRIBUTED_PROJECT_KEY = "";
3533
+
3534
+ function indexProjectBucketsByGroup(projectState) {
3535
+ const index = new Map();
3536
+ const buckets = projectState?.buckets;
3537
+ if (!buckets || typeof buckets !== "object") return index;
3538
+ for (const bucket of Object.values(buckets)) {
3539
+ if (!bucket || typeof bucket !== "object" || !bucket.totals) continue;
3540
+ if (typeof bucket.hour_start !== "string" || !bucket.hour_start) continue;
3541
+ if (typeof bucket.model !== "string" || !bucket.model) continue;
3542
+ if (typeof bucket.project_key !== "string" || !bucket.project_key) continue;
3543
+ const source = normalizeSourceInput(bucket.source) || DEFAULT_SOURCE;
3544
+ const model = normalizeModelInput(bucket.model) || DEFAULT_MODEL;
3545
+ const groupKey = groupBucketKey(source, bucket.hour_start);
3546
+ let byModel = index.get(groupKey);
3547
+ if (!byModel) {
3548
+ byModel = new Map();
3549
+ index.set(groupKey, byModel);
3550
+ }
3551
+ let projects = byModel.get(model);
3552
+ if (!projects) {
3553
+ projects = new Map();
3554
+ byModel.set(model, projects);
3555
+ }
3556
+ projects.set(bucket.project_key, bucket);
3557
+ }
3558
+ return index;
3559
+ }
3560
+
3561
+ function totalsRowFields(totals) {
3562
+ return {
3563
+ input_tokens: totals.input_tokens,
3564
+ cached_input_tokens: totals.cached_input_tokens,
3565
+ cache_creation_input_tokens: totals.cache_creation_input_tokens,
3566
+ output_tokens: totals.output_tokens,
3567
+ reasoning_output_tokens: totals.reasoning_output_tokens,
3568
+ total_tokens: totals.total_tokens,
3569
+ billable_total_tokens: totals.billable_total_tokens ?? totals.total_tokens,
3570
+ total_cost_usd: totals.total_cost_usd || 0,
3571
+ conversation_count: totals.conversation_count,
3572
+ };
3573
+ }
3574
+
3575
+ function appendProjectSplitRows({ group, model, aggregateBucket, projects, toAppend }) {
3576
+ if (!projects || projects.size === 0) return;
3577
+ const attributed = initTotals();
3578
+ const projectKeys = [...projects.keys()].sort();
3579
+ // The server stores only the basename (docs/cli-contract.md §2.4), so two
3580
+ // local projects such as `alice/api` and `bob/api` share one cloud identity.
3581
+ // Merge their totals under that normalized key here: emitting two rows with
3582
+ // the same wire key would make the server's request dedupe keep just one of
3583
+ // them and silently drop the other project's usage.
3584
+ const normalizedProjects = new Map();
3585
+ for (const projectKey of projectKeys) {
3586
+ const projectBucket = projects.get(projectKey);
3587
+ if (!projectBucket.totals) continue;
3588
+ addTotals(attributed, projectBucket.totals);
3589
+ const normalizedKey = basenameKey(projectKey);
3590
+ if (!normalizedKey) continue;
3591
+ let entry = normalizedProjects.get(normalizedKey);
3592
+ if (!entry) {
3593
+ entry = { totals: initTotals(), usagePrecision: null, buckets: [] };
3594
+ normalizedProjects.set(normalizedKey, entry);
3595
+ }
3596
+ addTotals(entry.totals, projectBucket.totals);
3597
+ entry.usagePrecision = entry.usagePrecision || projectBucket.usage_precision || null;
3598
+ entry.buckets.push(projectBucket);
3599
+ }
3600
+
3601
+ for (const [normalizedKey, entry] of normalizedProjects) {
3602
+ const totals = entry.totals;
3603
+ const usagePrecision = entry.usagePrecision || aggregateBucket.usage_precision || null;
3604
+ const marker = usagePrecision ? `${totalsKey(totals)}|${usagePrecision}` : totalsKey(totals);
3605
+ // Every contributing bucket must agree that this normalized row was
3606
+ // already queued; one changed contributor re-emits the merged total.
3607
+ if (entry.buckets.every((bucket) => bucket.queuedHourlyKey === marker)) continue;
3608
+ toAppend.push(
3609
+ JSON.stringify({
3610
+ source: group.source,
3611
+ model,
3612
+ hour_start: group.hourStart,
3613
+ project_key: normalizedKey,
3614
+ ...totalsRowFields(totals),
3615
+ usage_precision: usagePrecision || undefined,
3616
+ ...prioritySubsetOf(totals),
3617
+ ...creditsSubsetOf(totals),
3618
+ }),
3619
+ );
3620
+ for (const bucket of entry.buckets) bucket.queuedHourlyKey = marker;
3621
+ }
3622
+
3623
+ const residual = cloneTotals(aggregateBucket.totals);
3624
+ subtractTotals(residual, attributed);
3625
+ clampTotalsNonNegative(residual);
3626
+ const usagePrecision = aggregateBucket.usage_precision || null;
3627
+ const marker = usagePrecision ? `${totalsKey(residual)}|${usagePrecision}` : totalsKey(residual);
3628
+ if (aggregateBucket.queuedResidualKey === marker) return;
3629
+ toAppend.push(
3630
+ JSON.stringify({
3631
+ source: group.source,
3632
+ model,
3633
+ hour_start: group.hourStart,
3634
+ project_key: UNATTRIBUTED_PROJECT_KEY,
3635
+ ...totalsRowFields(residual),
3636
+ usage_precision: usagePrecision || undefined,
3637
+ ...prioritySubsetOf(residual),
3638
+ ...creditsSubsetOf(residual),
3639
+ }),
3640
+ );
3641
+ aggregateBucket.queuedResidualKey = marker;
3642
+ }
3643
+
3644
+ async function enqueueTouchedBuckets({
3645
+ queuePath,
3646
+ hourlyState,
3647
+ touchedBuckets,
3648
+ projectState = null,
3649
+ projectTouchedBuckets = null,
3650
+ }) {
3651
+ if (!touchedBuckets || touchedBuckets.size === 0) {
3652
+ // A project backfill only moves projectState (an upgrade re-reads history
3653
+ // for attribution without touching the hourly totals). Still emit the
3654
+ // split rows so the cloud learns the attribution for those hours.
3655
+ if (!projectState || !projectTouchedBuckets || projectTouchedBuckets.size === 0) return 0;
3656
+ }
3515
3657
 
3516
3658
  const touchedGroups = new Set();
3517
- for (const bucketStart of touchedBuckets) {
3659
+ for (const bucketStart of touchedBuckets || []) {
3518
3660
  const parsed = parseBucketKey(bucketStart);
3519
3661
  const hourStart = parsed.hourStart;
3520
3662
  if (!hourStart) continue;
3521
3663
  touchedGroups.add(groupBucketKey(parsed.source, hourStart));
3522
3664
  }
3665
+ if (projectState && projectTouchedBuckets) {
3666
+ for (const key of projectTouchedBuckets) {
3667
+ const bucket = projectState.buckets?.[key];
3668
+ if (!bucket || typeof bucket.hour_start !== "string" || !bucket.hour_start) continue;
3669
+ if (typeof bucket.model !== "string" || !bucket.model) continue;
3670
+ touchedGroups.add(groupBucketKey(bucket.source, bucket.hour_start));
3671
+ }
3672
+ }
3523
3673
  if (touchedGroups.size === 0) return 0;
3524
3674
 
3525
3675
  const groupQueued =
@@ -3600,9 +3750,32 @@ async function enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets })
3600
3750
  }
3601
3751
 
3602
3752
  const codexDominants = collectCodexDominantModels(hourlyState);
3753
+ const projectBucketsByGroup = indexProjectBucketsByGroup(projectState);
3603
3754
 
3604
3755
  const toAppend = [];
3605
3756
  for (const group of groupedBuckets.values()) {
3757
+ const projectModels = projectBucketsByGroup.get(groupBucketKey(group.source, group.hourStart));
3758
+ // Split only when every model of the hour has project attribution: the
3759
+ // rows then add up to the untouched aggregate (project totals + residual),
3760
+ // so totals never move and nothing is counted twice. Mixed hours fall
3761
+ // back to the plain aggregate row; those project totals stay unattributed.
3762
+ const canSplit =
3763
+ projectModels &&
3764
+ projectModels.size > 0 &&
3765
+ group.buckets.size > 0 &&
3766
+ [...group.buckets.keys()].every((model) => projectModels.has(model));
3767
+ if (canSplit) {
3768
+ for (const [model, bucket] of group.buckets.entries()) {
3769
+ appendProjectSplitRows({
3770
+ group,
3771
+ model,
3772
+ aggregateBucket: bucket,
3773
+ projects: projectModels.get(model),
3774
+ toAppend,
3775
+ });
3776
+ }
3777
+ continue;
3778
+ }
3606
3779
  const unknownBucket = group.buckets.get(DEFAULT_MODEL) || null;
3607
3780
  const dominantModel = pickDominantModel(group.buckets);
3608
3781
  let alignedModel = null;
@@ -3848,23 +4021,51 @@ async function enqueueTouchedProjectBuckets({
3848
4021
 
3849
4022
  await ensureDir(path.dirname(projectQueuePath));
3850
4023
 
3851
- const toAppend = [];
4024
+ // Contract v2 (2026-11-03): project buckets gained a model dimension so the
4025
+ // hourly queue can be split per project. The local project queue keeps its
4026
+ // original (project, source, hour) wire shape: one cumulative row per group,
4027
+ // summed across the hour's models. Rows are last-wins, so a rewrite of the
4028
+ // same group is harmless.
4029
+ const groups = new Map();
3852
4030
  for (const key of projectTouchedBuckets) {
3853
4031
  const bucket = projectState.buckets[key];
3854
4032
  if (!bucket || !bucket.totals) continue;
3855
- const totals = bucket.totals;
3856
- const queuedKey = totalsKey(totals);
3857
- if (bucket.queuedKey === queuedKey) continue;
3858
- const projectRef = typeof bucket.project_ref === "string" ? bucket.project_ref : null;
3859
- const projectKey = typeof bucket.project_key === "string" ? bucket.project_key : null;
3860
- if (!projectRef || !projectKey) continue;
4033
+ if (typeof bucket.project_key !== "string" || !bucket.project_key) continue;
4034
+ if (typeof bucket.project_ref !== "string" || !bucket.project_ref) continue;
4035
+ if (typeof bucket.source !== "string" || typeof bucket.hour_start !== "string") continue;
4036
+ const groupKey = JSON.stringify([bucket.project_key, bucket.source, bucket.hour_start]);
4037
+ let group = groups.get(groupKey);
4038
+ if (!group) {
4039
+ group = {
4040
+ projectKey: bucket.project_key,
4041
+ projectRef: bucket.project_ref,
4042
+ source: bucket.source,
4043
+ hourStart: bucket.hour_start,
4044
+ changed: false,
4045
+ };
4046
+ groups.set(groupKey, group);
4047
+ }
4048
+ if (bucket.queuedKey !== totalsKey(bucket.totals)) group.changed = true;
4049
+ }
3861
4050
 
4051
+ const toAppend = [];
4052
+ for (const group of groups.values()) {
4053
+ if (!group.changed) continue;
4054
+ const totals = initTotals();
4055
+ const members = [];
4056
+ for (const bucket of Object.values(projectState.buckets)) {
4057
+ if (!bucket || !bucket.totals) continue;
4058
+ if (bucket.project_key !== group.projectKey) continue;
4059
+ if (bucket.source !== group.source || bucket.hour_start !== group.hourStart) continue;
4060
+ addTotals(totals, bucket.totals);
4061
+ members.push(bucket);
4062
+ }
3862
4063
  toAppend.push(
3863
4064
  JSON.stringify({
3864
- project_ref: projectRef,
3865
- project_key: projectKey,
3866
- source: bucket.source,
3867
- hour_start: bucket.hour_start,
4065
+ project_ref: group.projectRef,
4066
+ project_key: group.projectKey,
4067
+ source: group.source,
4068
+ hour_start: group.hourStart,
3868
4069
  input_tokens: totals.input_tokens,
3869
4070
  cached_input_tokens: totals.cached_input_tokens,
3870
4071
  cache_creation_input_tokens: totals.cache_creation_input_tokens,
@@ -3876,7 +4077,7 @@ async function enqueueTouchedProjectBuckets({
3876
4077
  conversation_count: totals.conversation_count,
3877
4078
  }),
3878
4079
  );
3879
- bucket.queuedKey = queuedKey;
4080
+ for (const member of members) member.queuedKey = totalsKey(member.totals);
3880
4081
  }
3881
4082
 
3882
4083
  if (toAppend.length > 0) {
@@ -4292,12 +4493,13 @@ function subtractCountedOpencodeMessage({
4292
4493
  projectState,
4293
4494
  countedAt.projectKey,
4294
4495
  source,
4496
+ countedAt.model,
4295
4497
  countedAt.bucketStart,
4296
4498
  countedAt.projectRef,
4297
4499
  );
4298
4500
  subtractTotals(projectBucket.totals, counted);
4299
4501
  projectTouchedBuckets.add(
4300
- projectBucketKey(countedAt.projectKey, source, countedAt.bucketStart),
4502
+ projectBucketKey(countedAt.projectKey, source, countedAt.model, countedAt.bucketStart),
4301
4503
  );
4302
4504
  }
4303
4505
  return true;
@@ -4363,10 +4565,14 @@ function getHourlyBucket(state, source, model, hourStart) {
4363
4565
  return bucket;
4364
4566
  }
4365
4567
 
4366
- function getProjectBucket(state, projectKey, source, hourStart, projectRef) {
4568
+ function getProjectBucket(state, projectKey, source, model, hourStart, projectRef) {
4367
4569
  const buckets = state.buckets;
4368
4570
  const normalizedSource = normalizeSourceInput(source) || DEFAULT_SOURCE;
4369
- const key = projectBucketKey(projectKey, normalizedSource, hourStart);
4571
+ // `model == null` means this parser cannot reliably pair project usage with
4572
+ // an hourly model (separate attribution pass or cursor drift). Such buckets
4573
+ // keep the legacy three-part key and are never split out of an aggregate.
4574
+ const normalizedModel = model == null ? null : normalizeModelInput(model) || DEFAULT_MODEL;
4575
+ const key = projectBucketKey(projectKey, normalizedSource, normalizedModel, hourStart);
4370
4576
  let bucket = buckets[key];
4371
4577
  if (!bucket || typeof bucket !== "object") {
4372
4578
  bucket = {
@@ -4377,6 +4583,7 @@ function getProjectBucket(state, projectKey, source, hourStart, projectRef) {
4377
4583
  source: normalizedSource,
4378
4584
  hour_start: hourStart,
4379
4585
  };
4586
+ if (normalizedModel) bucket.model = normalizedModel;
4380
4587
  buckets[key] = bucket;
4381
4588
  return bucket;
4382
4589
  }
@@ -4392,6 +4599,7 @@ function getProjectBucket(state, projectKey, source, hourStart, projectRef) {
4392
4599
  if (projectRef) bucket.project_ref = projectRef;
4393
4600
  if (projectKey) bucket.project_key = projectKey;
4394
4601
  bucket.source = normalizedSource;
4602
+ if (normalizedModel) bucket.model = normalizedModel;
4395
4603
  bucket.hour_start = hourStart;
4396
4604
 
4397
4605
  return bucket;
@@ -4613,9 +4821,16 @@ function bucketKey(source, model, hourStart) {
4613
4821
  return `${safeSource}${BUCKET_SEPARATOR}${safeModel}${BUCKET_SEPARATOR}${hourStart}`;
4614
4822
  }
4615
4823
 
4616
- function projectBucketKey(projectKey, source, hourStart) {
4824
+ // Contract v2 (2026-11-03): a project bucket is per (project, source, model,
4825
+ // hour) so it can be subtracted from the matching per-model hourly aggregate
4826
+ // when usage rows are split by project_key. Buckets written before the model
4827
+ // dimension existed keep their old `project|source|hour` key and are never
4828
+ // touched again (their usage stays in the unattributed residual).
4829
+ function projectBucketKey(projectKey, source, model, hourStart) {
4617
4830
  const safeSource = normalizeSourceInput(source) || DEFAULT_SOURCE;
4618
- return `${projectKey}${BUCKET_SEPARATOR}${safeSource}${BUCKET_SEPARATOR}${hourStart}`;
4831
+ if (model == null) return `${projectKey}${BUCKET_SEPARATOR}${safeSource}${BUCKET_SEPARATOR}${hourStart}`;
4832
+ const safeModel = normalizeModelInput(model) || DEFAULT_MODEL;
4833
+ return `${projectKey}${BUCKET_SEPARATOR}${safeSource}${BUCKET_SEPARATOR}${safeModel}${BUCKET_SEPARATOR}${hourStart}`;
4619
4834
  }
4620
4835
 
4621
4836
  function groupBucketKey(source, hourStart) {
@@ -6378,13 +6593,14 @@ async function parseOpencodeDbIncremental({
6378
6593
  projectState,
6379
6594
  attribution.projectKey,
6380
6595
  defaultSource,
6596
+ model,
6381
6597
  bucketStart,
6382
6598
  attribution.projectRef,
6383
6599
  );
6384
6600
  addTotals(projectBucket.totals, contribution);
6385
6601
  clampTotalsNonNegative(projectBucket.totals);
6386
6602
  projectTouchedBuckets.add(
6387
- projectBucketKey(attribution.projectKey, defaultSource, bucketStart),
6603
+ projectBucketKey(attribution.projectKey, defaultSource, model, bucketStart),
6388
6604
  );
6389
6605
  }
6390
6606
 
@@ -6411,7 +6627,7 @@ async function parseOpencodeDbIncremental({
6411
6627
  }
6412
6628
  }
6413
6629
 
6414
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
6630
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
6415
6631
  const projectBucketsQueued = projectEnabled
6416
6632
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
6417
6633
  : 0;
@@ -6726,12 +6942,13 @@ async function parseQoderDbIncremental({
6726
6942
  projectState,
6727
6943
  previous.projectKey,
6728
6944
  sourceKey,
6945
+ previous.model,
6729
6946
  previous.bucketStart,
6730
6947
  previous.projectRef || null,
6731
6948
  );
6732
6949
  subtractTotals(oldProjectBucket.totals, previousTotals);
6733
6950
  projectTouchedBuckets.add(
6734
- projectBucketKey(previous.projectKey, sourceKey, previous.bucketStart),
6951
+ projectBucketKey(previous.projectKey, sourceKey, previous.model, previous.bucketStart),
6735
6952
  );
6736
6953
  }
6737
6954
  }
@@ -6744,11 +6961,12 @@ async function parseQoderDbIncremental({
6744
6961
  projectState,
6745
6962
  projectKey,
6746
6963
  sourceKey,
6964
+ model,
6747
6965
  bucketStart,
6748
6966
  projectRef,
6749
6967
  );
6750
6968
  addTotals(projectBucket.totals, currentTotals);
6751
- projectTouchedBuckets.add(projectBucketKey(projectKey, sourceKey, bucketStart));
6969
+ projectTouchedBuckets.add(projectBucketKey(projectKey, sourceKey, model, bucketStart));
6752
6970
  }
6753
6971
  qoderState.messages[messageKey] = {
6754
6972
  totals: currentTotals,
@@ -6779,6 +6997,8 @@ async function parseQoderDbIncremental({
6779
6997
  queuePath,
6780
6998
  hourlyState,
6781
6999
  touchedBuckets,
7000
+ projectState,
7001
+ projectTouchedBuckets,
6782
7002
  });
6783
7003
  const projectBucketsQueued = projectEnabled
6784
7004
  ? await enqueueTouchedProjectBuckets({
@@ -7130,9 +7350,9 @@ async function parseQoderNewIncremental({
7130
7350
  subtractTotals(oldBucket.totals, prev.totals);
7131
7351
  touchedBuckets.add(bucketKey(sourceKey, prev.model, prev.bucketStart));
7132
7352
  if (projectEnabled && prev.projectKey) {
7133
- const oldProjectBucket = getProjectBucket(projectState, prev.projectKey, sourceKey, prev.bucketStart, prev.projectRef || null);
7353
+ const oldProjectBucket = getProjectBucket(projectState, prev.projectKey, sourceKey, prev.model, prev.bucketStart, prev.projectRef || null);
7134
7354
  subtractTotals(oldProjectBucket.totals, prev.totals);
7135
- projectTouchedBuckets.add(projectBucketKey(prev.projectKey, sourceKey, prev.bucketStart));
7355
+ projectTouchedBuckets.add(projectBucketKey(prev.projectKey, sourceKey, prev.model, prev.bucketStart));
7136
7356
  }
7137
7357
  }
7138
7358
  if (cur) {
@@ -7142,10 +7362,10 @@ async function parseQoderNewIncremental({
7142
7362
  // addTotals handles total_cost_usd via USD_TICKS, but credits cost is small; ensure it accumulates
7143
7363
  touchedBuckets.add(bucketKey(sourceKey, cur.model, cur.bucketStart));
7144
7364
  if (projectEnabled && cur.projectKey) {
7145
- const projectBucket = getProjectBucket(projectState, cur.projectKey, sourceKey, cur.bucketStart, cur.projectRef);
7365
+ const projectBucket = getProjectBucket(projectState, cur.projectKey, sourceKey, cur.model, cur.bucketStart, cur.projectRef);
7146
7366
  addTotals(projectBucket.totals, cur.totals);
7147
7367
  if (cur.totals.usage_precision) projectBucket.usage_precision = cur.totals.usage_precision;
7148
- projectTouchedBuckets.add(projectBucketKey(cur.projectKey, sourceKey, cur.bucketStart));
7368
+ projectTouchedBuckets.add(projectBucketKey(cur.projectKey, sourceKey, cur.model, cur.bucketStart));
7149
7369
  }
7150
7370
  qoderState.messages[key] = {
7151
7371
  totals: cur.totals,
@@ -7171,10 +7391,10 @@ async function parseQoderNewIncremental({
7171
7391
  if (cur.totals.usage_precision) bucket.usage_precision = cur.totals.usage_precision;
7172
7392
  touchedBuckets.add(bucketKey(sourceKey, cur.model, cur.bucketStart));
7173
7393
  if (projectEnabled && cur.projectKey) {
7174
- const projectBucket = getProjectBucket(projectState, cur.projectKey, sourceKey, cur.bucketStart, cur.projectRef);
7394
+ const projectBucket = getProjectBucket(projectState, cur.projectKey, sourceKey, cur.model, cur.bucketStart, cur.projectRef);
7175
7395
  addTotals(projectBucket.totals, cur.totals);
7176
7396
  if (cur.totals.usage_precision) projectBucket.usage_precision = cur.totals.usage_precision;
7177
- projectTouchedBuckets.add(projectBucketKey(cur.projectKey, sourceKey, cur.bucketStart));
7397
+ projectTouchedBuckets.add(projectBucketKey(cur.projectKey, sourceKey, cur.model, cur.bucketStart));
7178
7398
  }
7179
7399
  qoderState.messages[key] = {
7180
7400
  totals: cur.totals,
@@ -7188,7 +7408,7 @@ async function parseQoderNewIncremental({
7188
7408
  eventsAggregated += 1;
7189
7409
  }
7190
7410
 
7191
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
7411
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
7192
7412
  const projectBucketsQueued = projectEnabled
7193
7413
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
7194
7414
  : 0;
@@ -11680,11 +11900,12 @@ async function parseWorkbuddyIncremental({
11680
11900
  projectState,
11681
11901
  projectKey,
11682
11902
  source,
11903
+ null,
11683
11904
  bucketStart,
11684
11905
  projectRef,
11685
11906
  );
11686
11907
  addTotals(projectBucket.totals, delta);
11687
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
11908
+ projectTouchedBuckets.add(projectBucketKey(projectKey, source, null, bucketStart));
11688
11909
  },
11689
11910
  );
11690
11911
 
@@ -11980,6 +12201,8 @@ async function parseWorkbuddyIncremental({
11980
12201
  queuePath,
11981
12202
  hourlyState,
11982
12203
  touchedBuckets,
12204
+ projectState,
12205
+ projectTouchedBuckets,
11983
12206
  });
11984
12207
  const updatedAt = new Date().toISOString();
11985
12208
  hourlyState.updatedAt = updatedAt;
@@ -15072,12 +15295,13 @@ async function parseDevinIncremental({
15072
15295
  projectState,
15073
15296
  previous.projectKey,
15074
15297
  DEVIN_SOURCE,
15298
+ previous.model,
15075
15299
  previous.bucketStart,
15076
15300
  previous.projectRef || null,
15077
15301
  );
15078
15302
  subtractTotals(oldProjectBucket.totals, previousTotals);
15079
15303
  projectTouchedBuckets.add(
15080
- projectBucketKey(previous.projectKey, DEVIN_SOURCE, previous.bucketStart),
15304
+ projectBucketKey(previous.projectKey, DEVIN_SOURCE, previous.model, previous.bucketStart),
15081
15305
  );
15082
15306
  }
15083
15307
  }
@@ -15090,12 +15314,13 @@ async function parseDevinIncremental({
15090
15314
  projectState,
15091
15315
  projectKey,
15092
15316
  DEVIN_SOURCE,
15317
+ event.model,
15093
15318
  bucketStart,
15094
15319
  projectRef,
15095
15320
  );
15096
15321
  addTotals(projectBucket.totals, event.totals);
15097
15322
  projectTouchedBuckets.add(
15098
- projectBucketKey(projectKey, DEVIN_SOURCE, bucketStart),
15323
+ projectBucketKey(projectKey, DEVIN_SOURCE, event.model, bucketStart),
15099
15324
  );
15100
15325
  }
15101
15326
  // The ledger records only what was added: owning session, model,
@@ -15130,6 +15355,8 @@ async function parseDevinIncremental({
15130
15355
  queuePath,
15131
15356
  hourlyState,
15132
15357
  touchedBuckets,
15358
+ projectState,
15359
+ projectTouchedBuckets,
15133
15360
  });
15134
15361
  const projectBucketsQueued = projectEnabled
15135
15362
  ? await enqueueTouchedProjectBuckets({
@@ -16154,12 +16381,13 @@ async function parseDroidIncremental({
16154
16381
  projectState,
16155
16382
  projectKey,
16156
16383
  "droid",
16384
+ model,
16157
16385
  bucketStart,
16158
16386
  projectRef,
16159
16387
  );
16160
16388
  addTotals(projectBucket.totals, projectDelta);
16161
16389
  projectTouchedBuckets.add(
16162
- projectBucketKey(projectKey, "droid", bucketStart),
16390
+ projectBucketKey(projectKey, "droid", model, bucketStart),
16163
16391
  );
16164
16392
  }
16165
16393
  projectSessionTotals[sessionId] = {
@@ -16221,6 +16449,8 @@ async function parseDroidIncremental({
16221
16449
  queuePath,
16222
16450
  hourlyState,
16223
16451
  touchedBuckets,
16452
+ projectState,
16453
+ projectTouchedBuckets,
16224
16454
  });
16225
16455
  const projectBucketsQueued = projectEnabled
16226
16456
  ? await enqueueTouchedProjectBuckets({
@@ -16721,15 +16951,17 @@ async function parseOmpLikeIncremental({
16721
16951
  total_tokens: totalTokens,
16722
16952
  conversation_count: 1,
16723
16953
  };
16954
+ const projectModel = normalizeModelInput(msg.model) || fallbackModel;
16724
16955
  const projectBucket = getProjectBucket(
16725
16956
  projectState,
16726
16957
  projectKey,
16727
16958
  source,
16959
+ projectModel,
16728
16960
  bucketStart,
16729
16961
  projectRef,
16730
16962
  );
16731
16963
  addTotals(projectBucket.totals, delta);
16732
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
16964
+ projectTouchedBuckets.add(projectBucketKey(projectKey, source, projectModel, bucketStart));
16733
16965
  projectSeenIds.add(entryId);
16734
16966
  });
16735
16967
  }
@@ -16757,6 +16989,8 @@ async function parseOmpLikeIncremental({
16757
16989
  queuePath,
16758
16990
  hourlyState,
16759
16991
  touchedBuckets,
16992
+ projectState,
16993
+ projectTouchedBuckets,
16760
16994
  });
16761
16995
  const projectBucketsQueued = projectEnabled
16762
16996
  ? await enqueueTouchedProjectBuckets({
@@ -17334,15 +17568,19 @@ async function parsePiLikeIncremental({
17334
17568
  total_tokens: totalTokens,
17335
17569
  conversation_count: 1,
17336
17570
  };
17571
+ const projectModel = normalizeModelInput(msg.model) || fallbackModel;
17337
17572
  const projectBucket = getProjectBucket(
17338
17573
  projectState,
17339
17574
  projectKey,
17340
17575
  sourceForProvider(msg.provider),
17576
+ projectModel,
17341
17577
  bucketStart,
17342
17578
  projectRef,
17343
17579
  );
17344
17580
  addTotals(projectBucket.totals, delta);
17345
- projectTouchedBuckets.add(projectBucketKey(projectKey, sourceForProvider(msg.provider), bucketStart));
17581
+ projectTouchedBuckets.add(
17582
+ projectBucketKey(projectKey, sourceForProvider(msg.provider), projectModel, bucketStart),
17583
+ );
17346
17584
  projectSeenIds.add(entryId);
17347
17585
  }
17348
17586
  }
@@ -17365,6 +17603,8 @@ async function parsePiLikeIncremental({
17365
17603
  queuePath,
17366
17604
  hourlyState,
17367
17605
  touchedBuckets,
17606
+ projectState,
17607
+ projectTouchedBuckets,
17368
17608
  });
17369
17609
  const projectBucketsQueued = projectEnabled
17370
17610
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
@@ -21540,12 +21780,13 @@ async function parseGrokBuildIncremental({
21540
21780
  projectState,
21541
21781
  projectKey,
21542
21782
  "grok",
21783
+ pending.model,
21543
21784
  pending.hourStartStr,
21544
21785
  projectRef,
21545
21786
  );
21546
21787
  addTotals(projectBucket.totals, pending.delta);
21547
21788
  projectTouchedBuckets.add(
21548
- projectBucketKey(projectKey, "grok", pending.hourStartStr),
21789
+ projectBucketKey(projectKey, "grok", pending.model, pending.hourStartStr),
21549
21790
  );
21550
21791
  }
21551
21792
 
@@ -21558,12 +21799,13 @@ async function parseGrokBuildIncremental({
21558
21799
  projectState,
21559
21800
  projectKey,
21560
21801
  "grok",
21802
+ projectLastModel || DEFAULT_MODEL,
21561
21803
  projectFinalTouchedHourStart,
21562
21804
  projectRef,
21563
21805
  );
21564
21806
  addTotals(projectBucket.totals, { conversation_count: deltaMessageCount });
21565
21807
  projectTouchedBuckets.add(
21566
- projectBucketKey(projectKey, "grok", projectFinalTouchedHourStart),
21808
+ projectBucketKey(projectKey, "grok", projectLastModel || DEFAULT_MODEL, projectFinalTouchedHourStart),
21567
21809
  );
21568
21810
  }
21569
21811
 
@@ -21592,7 +21834,7 @@ async function parseGrokBuildIncremental({
21592
21834
  if (projectEnabled) pruneMissingGrokProjectUpdateOffsets(projectUpdateOffsets);
21593
21835
 
21594
21836
  let bucketsQueued = queuePath
21595
- ? await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets })
21837
+ ? await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets })
21596
21838
  : 0;
21597
21839
 
21598
21840
  // After a semantics migration, retract stale grok queue keys that the full
@@ -22323,11 +22565,12 @@ async function parseAntigravityIncremental({
22323
22565
  projectState,
22324
22566
  c.projectKey,
22325
22567
  c.source,
22568
+ c.model,
22326
22569
  c.bucketStart,
22327
22570
  c.projectRef || null,
22328
22571
  );
22329
22572
  subtractTotals(oldProjectBucket.totals, c.totals);
22330
- projectTouchedBuckets.add(projectBucketKey(c.projectKey, c.source, c.bucketStart));
22573
+ projectTouchedBuckets.add(projectBucketKey(c.projectKey, c.source, c.model, c.bucketStart));
22331
22574
  }
22332
22575
  }
22333
22576
  }
@@ -22482,7 +22725,7 @@ async function parseAntigravityIncremental({
22482
22725
  }
22483
22726
  }
22484
22727
 
22485
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
22728
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
22486
22729
  const projectBucketsQueued = projectEnabled
22487
22730
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
22488
22731
  : 0;
@@ -22970,11 +23213,12 @@ async function parseAntigravityFile({
22970
23213
  projectState,
22971
23214
  projectKey,
22972
23215
  source,
23216
+ model,
22973
23217
  bucketStart,
22974
23218
  projectRef,
22975
23219
  );
22976
23220
  addTotals(projectBucket.totals, delta);
22977
- projectTouchedBuckets.add(projectBucketKey(projectKey, source, bucketStart));
23221
+ projectTouchedBuckets.add(projectBucketKey(projectKey, source, model, bucketStart));
22978
23222
  }
22979
23223
  }
22980
23224
  const isNewForCounter = watermarkLine > 0 ? i >= watermarkLine : true;
@@ -25668,8 +25912,8 @@ async function parseCommandCodeIncremental({
25668
25912
  apply(getHourlyBucket(hourlyState, COMMAND_CODE_SOURCE, value.model, value.bucketStart).totals, totals);
25669
25913
  touchedBuckets.add(bucketKey(COMMAND_CODE_SOURCE, value.model, value.bucketStart));
25670
25914
  if (projectEnabled && value.projectKey) {
25671
- apply(getProjectBucket(projectState, value.projectKey, COMMAND_CODE_SOURCE, value.bucketStart, value.projectRef).totals, totals);
25672
- projectTouchedBuckets.add(projectBucketKey(value.projectKey, COMMAND_CODE_SOURCE, value.bucketStart));
25915
+ apply(getProjectBucket(projectState, value.projectKey, COMMAND_CODE_SOURCE, value.model, value.bucketStart, value.projectRef).totals, totals);
25916
+ projectTouchedBuckets.add(projectBucketKey(value.projectKey, COMMAND_CODE_SOURCE, value.model, value.bucketStart));
25673
25917
  }
25674
25918
  }
25675
25919
 
@@ -25738,7 +25982,7 @@ async function parseCommandCodeIncremental({
25738
25982
  prepareBuckets(touchedBuckets, hourlyState, cursors?.hourly);
25739
25983
  if (projectEnabled) prepareBuckets(projectTouchedBuckets, projectState, cursors?.projectHourly);
25740
25984
 
25741
- const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets });
25985
+ const bucketsQueued = await enqueueTouchedBuckets({ queuePath, hourlyState, touchedBuckets, projectState, projectTouchedBuckets });
25742
25986
  const projectBucketsQueued = projectEnabled
25743
25987
  ? await enqueueTouchedProjectBuckets({ projectQueuePath, projectState, projectTouchedBuckets })
25744
25988
  : 0;
@@ -25769,6 +26013,12 @@ async function parseCommandCodeIncremental({
25769
26013
  }
25770
26014
 
25771
26015
  module.exports = {
26016
+ enqueueTouchedBuckets,
26017
+ enqueueTouchedProjectBuckets,
26018
+ projectBucketKey,
26019
+ getProjectBucket,
26020
+ initTotals,
26021
+ totalsKey,
25772
26022
  parseTraeIncremental,
25773
26023
  listRolloutFiles,
25774
26024
  listRolloutFilesDeep,