@ponbac/token-ledger 0.5.1 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +13 -2
  2. package/dist/main.js +182 -44
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -451,6 +451,11 @@ Keep developer identity alongside exported reports when combining them across a
451
451
 
452
452
  ## Sync to toki2
453
453
 
454
+ Copilot requests routed through a ChatGPT OAuth proxy can use
455
+ [local billing hints](docs/billing-hints.md). On Windows the proxy appends metadata to
456
+ `%LOCALAPPDATA%\token-ledger\billing-hints\chatgpt-proxy.jsonl`; no path configuration
457
+ is needed. Usage stays attributed to Copilot, with ChatGPT/Codex as its subscription payer.
458
+
454
459
  `sync` uploads this machine's usage to a [toki2](https://github.com/ponbac/toki2)
455
460
  server, which combines every developer's machines for team reporting and billing.
456
461
  Run it on **every machine** where you use a coding agent, and schedule it daily.
@@ -711,9 +716,15 @@ Claude Code deletes transcripts after 30 days by default, so keep `--since` with
711
716
  the last 30 days; `sync` warns when it is not. Once uploaded, the server keeps the
712
717
  long-term record.
713
718
 
719
+ Each upload carries the time its scan began, and the server refuses one older than the
720
+ last it stored from this machine, so a slow sync cannot overwrite a newer one. The newer
721
+ upload may cover another window or tools, so `sync` then scans the same window again and
722
+ sends it once more; if that is refused too, it says so and exits `7`.
723
+
714
724
  Exit codes: `0` synced, `1` invalid input or settings, `2` refused by `--strict`,
715
725
  `3` server unreachable or temporarily failing (retry later), `4` token or machine
716
- refused, `5` unsupported payload version (upgrade token-ledger), `6` payload rejected.
726
+ refused, `5` unsupported payload version (upgrade token-ledger), `6` payload rejected,
727
+ `7` superseded twice by newer syncs from this machine (run it again).
717
728
 
718
729
  ## Accuracy and limitations
719
730
 
@@ -762,7 +773,7 @@ For manual publication:
762
773
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
763
774
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
764
775
  the repository with lifecycle scripts disabled, and exercises the installed CLI.
765
- 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.5.1.tgz` (filename follows version).
776
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.6.1.tgz` (filename follows version).
766
777
  4. Inspect `npm publish ./dist/npm --dry-run`.
767
778
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
768
779
  prompt if requested.
package/dist/main.js CHANGED
@@ -32686,8 +32686,48 @@ function dayWindow(since, until, timeZone) {
32686
32686
  };
32687
32687
  }
32688
32688
 
32689
+ // packages/core/src/billing-hints.ts
32690
+ class BillingHintsError extends exports_Schema.TaggedError()("BillingHintsError", {
32691
+ message: exports_Schema.String
32692
+ }) {
32693
+ }
32694
+ var Hint = exports_Schema.fromJsonString(exports_Schema.Struct({
32695
+ requestId: exports_Schema.NonEmptyString,
32696
+ billingProvider: exports_Schema.Literal("codex"),
32697
+ authMode: exports_Schema.Literal("chatgpt")
32698
+ }));
32699
+ var readBillingHints = exports_Effect.fn("BillingHints.read")(function* (location2) {
32700
+ const fs = yield* exports_FileSystem.FileSystem;
32701
+ const path = yield* exports_Path.Path;
32702
+ const entries3 = yield* fs.readDirectory(location2.directory).pipe(exports_Effect.result);
32703
+ if (exports_Result.isFailure(entries3)) {
32704
+ if (!location2.required && exports_Predicate.isTagged(entries3.failure.reason, "NotFound"))
32705
+ return new Set;
32706
+ return yield* new BillingHintsError({ message: "Cannot read the billing hints directory." });
32707
+ }
32708
+ const requests = new Set;
32709
+ for (const entry of entries3.success.toSorted()) {
32710
+ if (!entry.endsWith(".jsonl"))
32711
+ continue;
32712
+ const contents = yield* fs.readFileString(path.join(location2.directory, entry)).pipe(exports_Effect.mapError(() => new BillingHintsError({ message: `Cannot read ${JSON.stringify(entry)}.` })));
32713
+ let lineNumber = 0;
32714
+ for (const line of contents.split(`
32715
+ `)) {
32716
+ lineNumber++;
32717
+ if (!line.trim())
32718
+ continue;
32719
+ const hint = yield* exports_Schema.decodeUnknownEffect(Hint)(line).pipe(exports_Effect.mapError(() => new BillingHintsError({
32720
+ message: `Invalid row in ${JSON.stringify(entry)} at line ${lineNumber}.`
32721
+ })));
32722
+ requests.add(hint.requestId);
32723
+ }
32724
+ }
32725
+ return requests;
32726
+ });
32727
+
32689
32728
  // packages/core/src/model.ts
32690
32729
  var Provider = exports_Schema.Literals(["codex", "claude", "grok", "copilot", "pi"]);
32730
+ var BillingProvider = exports_Schema.Literals(["codex", "claude", "grok", "copilot"]);
32691
32731
  var Tokens = exports_Schema.Struct({
32692
32732
  input: exports_Schema.Natural,
32693
32733
  cacheRead: exports_Schema.Natural,
@@ -32713,6 +32753,7 @@ var ModelPrice = exports_Schema.Struct({
32713
32753
  cacheWrite: exports_Schema.NullOr(Rate)
32714
32754
  });
32715
32755
  var Configuration = exports_Schema.Struct({
32756
+ billingHintsDirectory: exports_Schema.optionalKey(exports_Schema.NonEmptyString),
32716
32757
  sources: exports_Schema.optionalKey(exports_Schema.Array(Source)),
32717
32758
  projects: exports_Schema.optionalKey(exports_Schema.Array(ProjectMapping)),
32718
32759
  prices: exports_Schema.optionalKey(exports_Schema.Record(exports_Schema.String, ModelPrice))
@@ -32729,6 +32770,7 @@ var Day = exports_Schema.String.check(exports_Schema.isPattern(/^\d{4}-\d{2}-\d{
32729
32770
  return Number.isFinite(date2.getTime()) && date2.toISOString().slice(0, 10) === s;
32730
32771
  }));
32731
32772
  var ReportRequest = exports_Schema.Struct({
32773
+ billingHints: exports_Schema.optionalKey(exports_Schema.Struct({ directory: exports_Schema.NonEmptyString, required: exports_Schema.Boolean })),
32732
32774
  since: Day,
32733
32775
  until: Day,
32734
32776
  timeZone: exports_Schema.TimeZoneNamed,
@@ -34689,7 +34731,7 @@ function repositoryReference(attributes) {
34689
34731
  const remote = attributes["copilot_chat.repo.remote_url"];
34690
34732
  return remote === undefined ? null : RepositoryReference.Remote({ value: remote });
34691
34733
  }
34692
- function copilotParser(file2) {
34734
+ function copilotParser(file2, chatgptRequests = new Set) {
34693
34735
  const spans = new Map;
34694
34736
  const candidates = [];
34695
34737
  const warning = "Copilot coverage starts when telemetry was enabled; only inference requests are counted.";
@@ -34734,7 +34776,7 @@ function copilotParser(file2) {
34734
34776
  return skipped;
34735
34777
  const record3 = {
34736
34778
  provider: "copilot",
34737
- id: a["gen_ai.response.id"] === undefined ? `${span2.traceId}:${span2.spanId}` : `response:${a["gen_ai.response.id"]}`,
34779
+ id: `${span2.traceId}:${span2.spanId}`,
34738
34780
  session: a["gen_ai.conversation.id"] ?? a["session.id"] ?? file2,
34739
34781
  timestamp: ms,
34740
34782
  model: a["gen_ai.response.model"] ?? a["gen_ai.request.model"] ?? "unknown",
@@ -34742,12 +34784,33 @@ function copilotParser(file2) {
34742
34784
  repository: repositoryReference(a),
34743
34785
  tokens: { input: input - cacheRead - cacheWrite, output, cacheRead, cacheWrite }
34744
34786
  };
34745
- candidates.push({ record: record3, spanKey, isSpan: exportedSpan !== null });
34787
+ candidates.push({
34788
+ record: record3,
34789
+ spanKey,
34790
+ responseId: a["gen_ai.response.id"],
34791
+ isSpan: exportedSpan !== null
34792
+ });
34746
34793
  return empty14;
34747
34794
  },
34748
34795
  finish() {
34796
+ const eventResponses = new Map;
34797
+ for (const { spanKey, responseId, isSpan } of candidates)
34798
+ if (!isSpan && spanKey !== undefined && responseId !== undefined)
34799
+ eventResponses.set(spanKey, (eventResponses.get(spanKey) ?? new Set).add(responseId));
34800
+ const identified = candidates.map((candidate) => {
34801
+ const shared = candidate.isSpan && candidate.responseId === undefined && candidate.spanKey !== undefined ? eventResponses.get(candidate.spanKey) : undefined;
34802
+ const responseId = shared?.size === 1 ? [...shared][0] : candidate.responseId;
34803
+ if (responseId === undefined)
34804
+ return candidate;
34805
+ const record3 = { ...candidate.record, id: `response:${responseId}` };
34806
+ return {
34807
+ ...candidate,
34808
+ responseId,
34809
+ record: chatgptRequests.has(responseId) ? { ...record3, billingProvider: "codex" } : record3
34810
+ };
34811
+ });
34749
34812
  const preferred = new Map;
34750
- for (const candidate of candidates) {
34813
+ for (const candidate of identified) {
34751
34814
  let repository = candidate.record.repository;
34752
34815
  let key = candidate.spanKey;
34753
34816
  const visited = new Set;
@@ -34772,7 +34835,7 @@ function copilotParser(file2) {
34772
34835
  });
34773
34836
  }
34774
34837
  }
34775
- const records = candidates.map((candidate) => preferred.get(candidate.record.id)?.record ?? candidate.record);
34838
+ const records = identified.map((candidate) => preferred.get(candidate.record.id)?.record ?? candidate.record);
34776
34839
  candidates.length = 0;
34777
34840
  spans.clear();
34778
34841
  return { ...empty14, records, warnings: records.length > 0 ? [warning] : [] };
@@ -35023,6 +35086,7 @@ var SyncClient = exports_Schema.Struct({
35023
35086
  });
35024
35087
  var Cost = exports_Schema.Number.check(exports_Schema.isFinite(), exports_Schema.isGreaterThanOrEqualTo(0));
35025
35088
  var SyncBucket = exports_Schema.Struct({
35089
+ billingProvider: exports_Schema.optionalKey(BillingProvider),
35026
35090
  hourStart: UtcHour,
35027
35091
  sessionKey: SessionKey,
35028
35092
  project: NonEmptyText,
@@ -35032,7 +35096,7 @@ var SyncBucket = exports_Schema.Struct({
35032
35096
  records: exports_Schema.Int.check(exports_Schema.isGreaterThan(0)),
35033
35097
  estimatedCostUsd: exports_Schema.NullOr(Cost),
35034
35098
  unpricedRecords: exports_Schema.Natural
35035
- }).check(exports_Schema.makeFilter((bucket) => bucket.unpricedRecords <= bucket.records && bucket.estimatedCostUsd === null === bucket.unpricedRecords > 0));
35099
+ }).check(exports_Schema.makeFilter((bucket) => bucket.billingProvider !== bucket.provider && bucket.unpricedRecords <= bucket.records && bucket.estimatedCostUsd === null === (bucket.unpricedRecords === bucket.records)));
35036
35100
  var SyncCoverage = exports_Schema.Struct({
35037
35101
  provider: Provider,
35038
35102
  status: Coverage.fields.status,
@@ -35051,8 +35115,16 @@ var ProviderHint = exports_Schema.Struct({
35051
35115
  hourStart: UtcHour,
35052
35116
  plan: NonEmptyText
35053
35117
  });
35118
+ function partlyPriced(bucket) {
35119
+ return bucket.unpricedRecords > 0 && bucket.unpricedRecords < bucket.records;
35120
+ }
35121
+ function payloadVersion(buckets) {
35122
+ if (buckets.some(partlyPriced))
35123
+ return 3;
35124
+ return buckets.some((bucket) => bucket.billingProvider !== undefined) ? 2 : 1;
35125
+ }
35054
35126
  var SyncPayload = exports_Schema.Struct({
35055
- version: exports_Schema.Literal(1),
35127
+ version: exports_Schema.Literals([1, 2, 3]),
35056
35128
  machine: SyncMachine,
35057
35129
  clientVersion: NonEmptyText,
35058
35130
  timeZone: NonEmptyText,
@@ -35066,14 +35138,18 @@ var SyncPayload = exports_Schema.Struct({
35066
35138
  }),
35067
35139
  buckets: exports_Schema.Array(SyncBucket),
35068
35140
  coverage: exports_Schema.Array(SyncCoverage),
35069
- providerHints: exports_Schema.optionalKey(exports_Schema.Array(ProviderHint))
35070
- }).check(exports_Schema.makeFilter((payload) => distinct(payload.buckets, (bucket) => JSON.stringify([
35141
+ providerHints: exports_Schema.optionalKey(exports_Schema.Array(ProviderHint)),
35142
+ snapshotAt: exports_Schema.optionalKey(UtcInstant)
35143
+ }).check(exports_Schema.makeFilter((payload) => (payload.version >= 2 || payload.buckets.every((bucket) => bucket.billingProvider === undefined)) && (payload.version >= 3 || !payload.buckets.some(partlyPriced)) && distinct(payload.buckets, (bucket) => JSON.stringify([
35071
35144
  bucket.hourStart,
35072
35145
  bucket.sessionKey,
35073
35146
  bucket.project,
35074
35147
  bucket.provider,
35148
+ bucket.billingProvider ?? null,
35075
35149
  bucket.model
35076
- ])) && distinct(payload.coverage, (entry) => entry.provider) && distinct(payload.providerHints ?? [], (hint) => JSON.stringify([hint.provider, hint.hourStart, hint.plan])), { expected: "unique bucket keys, coverage providers, and plan hints" }), exports_Schema.makeFilter((payload) => {
35150
+ ])) && distinct(payload.coverage, (entry) => entry.provider) && distinct(payload.providerHints ?? [], (hint) => JSON.stringify([hint.provider, hint.hourStart, hint.plan])), {
35151
+ expected: "unique bucket keys, coverage providers, and plan hints, and a version carrying every bucket"
35152
+ }), exports_Schema.makeFilter((payload) => {
35077
35153
  const replaced = replacedProviders(payload.coverage);
35078
35154
  return [...payload.buckets, ...payload.providerHints ?? []].every((entry) => replaced.has(entry.provider) && entry.hourStart >= payload.window.start && entry.hourStart < payload.window.end);
35079
35155
  }));
@@ -35097,7 +35173,14 @@ function sessionBuckets(pricing) {
35097
35173
  const hourStart = Math.floor(record3.timestamp / hour) * hour;
35098
35174
  const project = storable(name);
35099
35175
  const model = storable(record3.model);
35100
- const key = JSON.stringify([hourStart, record3.provider, record3.session, project, model]);
35176
+ const key = JSON.stringify([
35177
+ hourStart,
35178
+ record3.provider,
35179
+ record3.billingProvider ?? null,
35180
+ record3.session,
35181
+ project,
35182
+ model
35183
+ ]);
35101
35184
  const previous = totals.get(key);
35102
35185
  const cost = priceTokens(record3.tokens, lookupPrice(pricing, record3.model));
35103
35186
  totals.set(key, {
@@ -35109,17 +35192,20 @@ function sessionBuckets(pricing) {
35109
35192
  });
35110
35193
  },
35111
35194
  finish() {
35112
- return [...totals.values()].map(({ record: record3, project, model, hourStart, usage }) => ({
35113
- hourStart: new Date(hourStart).toISOString(),
35114
- sessionKey: sessionKey(record3.provider, record3.session),
35115
- project,
35116
- provider: record3.provider,
35117
- model,
35118
- tokens: usage.tokens,
35119
- records: usage.records,
35120
- estimatedCostUsd: usage.estimatedCostUsd,
35121
- unpricedRecords: usage.unpricedRecords
35122
- })).toSorted((a, b) => a.hourStart.localeCompare(b.hourStart) || a.provider.localeCompare(b.provider) || a.sessionKey.localeCompare(b.sessionKey) || a.project.localeCompare(b.project) || a.model.localeCompare(b.model));
35195
+ return [...totals.values()].map(({ record: record3, project, model, hourStart, usage }) => {
35196
+ const bucket = {
35197
+ hourStart: new Date(hourStart).toISOString(),
35198
+ sessionKey: sessionKey(record3.provider, record3.session),
35199
+ project,
35200
+ provider: record3.provider,
35201
+ model,
35202
+ tokens: usage.tokens,
35203
+ records: usage.records,
35204
+ estimatedCostUsd: usage.unpricedRecords === usage.records ? null : usage.pricedCostUsd,
35205
+ unpricedRecords: usage.unpricedRecords
35206
+ };
35207
+ return record3.billingProvider === undefined ? bucket : { ...bucket, billingProvider: record3.billingProvider };
35208
+ }).toSorted((a, b) => a.hourStart.localeCompare(b.hourStart) || a.provider.localeCompare(b.provider) || a.sessionKey.localeCompare(b.sessionKey) || a.project.localeCompare(b.project) || a.model.localeCompare(b.model) || (a.billingProvider ?? "").localeCompare(b.billingProvider ?? ""));
35123
35209
  }
35124
35210
  };
35125
35211
  }
@@ -35177,14 +35263,18 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35177
35263
  const request3 = yield* exports_Schema.decodeUnknownEffect(ReportRequest)(input).pipe(exports_Effect.mapError(() => new InvalidRequest({
35178
35264
  message: "Expected an ordered inclusive day window, a named time zone, and valid sources, mappings, and prices."
35179
35265
  })));
35180
- if (request3.sources.some((source) => !path.isAbsolute(source.path)) || request3.projects.some((mapping) => mapping.paths.some((prefix3) => !path.isAbsolute(prefix3)))) {
35266
+ if (request3.billingHints !== undefined && !path.isAbsolute(request3.billingHints.directory) || request3.sources.some((source) => !path.isAbsolute(source.path)) || request3.projects.some((mapping) => mapping.paths.some((prefix3) => !path.isAbsolute(prefix3)))) {
35181
35267
  return yield* new InvalidRequest({
35182
- message: "Source and project mapping paths must be absolute."
35268
+ message: "Source, billing hint, and project mapping paths must be absolute."
35183
35269
  });
35184
35270
  }
35185
35271
  return request3;
35186
35272
  });
35187
- const scan2 = exports_Effect.fn("Ledger.scan")(function* (request3, start, locate, collect2, notice) {
35273
+ const scan2 = exports_Effect.fn("Ledger.scan")(function* (request3, start, locate, collect2, notice, onWarning) {
35274
+ const hints = yield* (notice !== undefined && request3.billingHints !== undefined && request3.sources.some((source) => source.provider === "copilot") ? readBillingHints(request3.billingHints).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path)) : exports_Effect.succeed(new Set)).pipe(exports_Effect.result);
35275
+ const chatgptRequests = exports_Result.isSuccess(hints) ? hints.success : new Set;
35276
+ if (exports_Result.isFailure(hints))
35277
+ yield* onWarning?.(`Billing hints: ${hints.failure.message} Copilot will not be uploaded. Repair the hints and rerun sync with --since <first missed day>.`) ?? exports_Effect.void;
35188
35278
  const resolveProject = yield* projectResolver(request3.projects).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
35189
35279
  const seen = new Set;
35190
35280
  const visitedFiles = new Set;
@@ -35200,7 +35290,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35200
35290
  let failedFiles = 0;
35201
35291
  let missingRoot = false;
35202
35292
  const warnings = new Set;
35203
- const copilot = source.provider === "copilot" ? copilotParser(source.path) : undefined;
35293
+ const copilot = source.provider === "copilot" ? copilotParser(source.path, chatgptRequests) : undefined;
35204
35294
  const place = exports_Effect.fn("Ledger.placeRecord")(function* (record3, slot) {
35205
35295
  const id2 = `${record3.provider}:${record3.id}`;
35206
35296
  if (seen.has(id2)) {
@@ -35286,14 +35376,17 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35286
35376
  }
35287
35377
  if (copilot?.finish !== undefined)
35288
35378
  yield* accept2(copilot.finish());
35289
- const status = missingRoot ? "missing" : failedFiles > 0 && files === 0 ? "failed" : failedFiles > 0 || malformedLines > 0 || skippedRecords > 0 ? "partial" : "ok";
35379
+ const hintsFailed = source.provider === "copilot" && exports_Result.isFailure(hints);
35380
+ if (hintsFailed)
35381
+ warnings.add("Billing hints could not be read; stored Copilot usage will be kept.");
35382
+ const status = hintsFailed ? "failed" : missingRoot ? "missing" : failedFiles > 0 && files === 0 ? "failed" : failedFiles > 0 || malformedLines > 0 || skippedRecords > 0 ? "partial" : "ok";
35290
35383
  coverage.push(() => ({
35291
35384
  provider: source.provider,
35292
35385
  source: source.path,
35293
35386
  status,
35294
35387
  files,
35295
35388
  filesBeforeWindow,
35296
- unreadable: missingRoot ? 0 : failedFiles,
35389
+ unreadable: (missingRoot ? 0 : failedFiles) + (hintsFailed ? 1 : 0),
35297
35390
  malformedLines,
35298
35391
  skippedRecords,
35299
35392
  duplicates,
@@ -35336,7 +35429,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35336
35429
  coverage
35337
35430
  });
35338
35431
  });
35339
- const payloadFrom = exports_Effect.fn("Ledger.payloadFrom")(function* (input, clientInput, history) {
35432
+ const payloadFrom = exports_Effect.fn("Ledger.payloadFrom")(function* (input, clientInput, history, onWarning) {
35340
35433
  const request3 = yield* decode5(input);
35341
35434
  const client = yield* exports_Schema.decodeUnknownEffect(SyncClient)(clientInput).pipe(exports_Effect.mapError(() => new InvalidRequest({
35342
35435
  message: "Expected a machine UUID and label and a client version."
@@ -35346,19 +35439,21 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35346
35439
  const buckets = sessionBuckets(request3.pricing);
35347
35440
  const hints = planHints();
35348
35441
  let earliest = requested.start;
35442
+ const snapshotAt = yield* exports_Clock.currentTimeMillis;
35349
35443
  const coverage = yield* scan2(request3, from, (timestamp2) => timestamp2 >= from && timestamp2 < requested.end ? timestamp2 : undefined, (record3, attribution, timestamp2) => {
35350
35444
  earliest = Math.min(earliest, timestamp2);
35351
35445
  buckets.add(record3, attribution.shareable ? attribution.project : unattributed);
35352
35446
  }, (plan) => {
35353
35447
  earliest = Math.min(earliest, plan.timestamp);
35354
35448
  hints.add(plan);
35355
- });
35449
+ }, onWarning);
35356
35450
  const since = earliest < requested.start ? exports_DateTime.formatIsoDate(exports_DateTime.setZone(exports_DateTime.makeUnsafe(earliest), request3.timeZone)) : request3.since;
35357
35451
  const window = since === request3.since ? requested : hourRange(dayWindow(since, request3.until, request3.timeZone));
35358
35452
  const summary = providerCoverage(coverage);
35359
35453
  const replaced = replacedProviders(summary);
35454
+ const uploadedBuckets = buckets.finish().filter((bucket) => replaced.has(bucket.provider));
35360
35455
  const payload = yield* SyncPayload.makeEffect({
35361
- version: 1,
35456
+ version: payloadVersion(uploadedBuckets),
35362
35457
  machine: client.machine,
35363
35458
  clientVersion: client.version,
35364
35459
  timeZone: request3.timeZone.id,
@@ -35373,9 +35468,10 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35373
35468
  fetchedAt: request3.pricing.fetchedAt,
35374
35469
  source: request3.pricing.source
35375
35470
  },
35376
- buckets: buckets.finish().filter((bucket) => replaced.has(bucket.provider)),
35471
+ buckets: uploadedBuckets,
35377
35472
  coverage: summary,
35378
- providerHints: hints.finish().filter((hint) => replaced.has(hint.provider))
35473
+ providerHints: hints.finish().filter((hint) => replaced.has(hint.provider)),
35474
+ snapshotAt: new Date(snapshotAt).toISOString()
35379
35475
  }).pipe(exports_Effect.mapError(() => new InvalidRequest({
35380
35476
  message: "Report data must satisfy the sync payload contract."
35381
35477
  })));
@@ -35383,8 +35479,8 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35383
35479
  });
35384
35480
  return Ledger.of({
35385
35481
  report,
35386
- syncPayload: (request3, client) => exports_Effect.map(payloadFrom(request3, client, false), ({ payload }) => payload),
35387
- historyPayload: (request3, client) => payloadFrom(request3, client, true)
35482
+ syncPayload: (request3, client, onWarning) => exports_Effect.map(payloadFrom(request3, client, false, onWarning), ({ payload }) => payload),
35483
+ historyPayload: (request3, client, onWarning) => payloadFrom(request3, client, true, onWarning)
35388
35484
  });
35389
35485
  }));
35390
35486
  }
@@ -42699,7 +42795,7 @@ var runWith2 = (command, config) => {
42699
42795
  // packages/cli/package.json
42700
42796
  var package_default = {
42701
42797
  name: "@ponbac/token-ledger",
42702
- version: "0.5.1",
42798
+ version: "0.6.1",
42703
42799
  private: true,
42704
42800
  type: "module",
42705
42801
  bin: {
@@ -42728,6 +42824,18 @@ var package_default = {
42728
42824
  url: "https://github.com/ponbac/token-ledger/issues"
42729
42825
  }
42730
42826
  };
42827
+
42828
+ // packages/cli/src/billing-hints.ts
42829
+ import { posix as posix2, win32 as win322 } from "node:path";
42830
+ var dataHome = exports_Effect.gen(function* () {
42831
+ const path = yield* exports_Path.Path;
42832
+ return yield* exports_Config.String("XDG_DATA_HOME").pipe(exports_Config.option, exports_Effect.map(exports_Option.filter((directory) => path.isAbsolute(directory))));
42833
+ });
42834
+ function billingHintsDirectory(platform, home, localAppData, xdgDataHome) {
42835
+ const path = platform === "win32" ? win322 : posix2;
42836
+ const root = exports_Match.value(platform).pipe(exports_Match.when("win32", () => exports_Option.getOrElse(exports_Option.filter(localAppData, (directory) => path.isAbsolute(directory)), () => path.join(home, "AppData", "Local"))), exports_Match.when("darwin", () => path.join(home, "Library", "Application Support")), exports_Match.orElse(() => exports_Option.getOrElse(xdgDataHome, () => path.join(home, ".local", "share"))));
42837
+ return path.join(root, "token-ledger", "billing-hints");
42838
+ }
42731
42839
  // node_modules/.bun/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
42732
42840
  function createScanner(text2, ignoreTrivia = false) {
42733
42841
  const len = text2.length;
@@ -43875,7 +43983,12 @@ function run6(outcome, program, args2, hint = "") {
43875
43983
  });
43876
43984
  }
43877
43985
  function quotable(job, forbidden2, described) {
43878
- const offending = [job.nodeDirectory, ...exports_Option.toArray(job.configHome), ...job.flags].find((value5) => forbidden2.test(value5));
43986
+ const offending = [
43987
+ job.nodeDirectory,
43988
+ ...exports_Option.toArray(job.configHome),
43989
+ ...exports_Option.toArray(job.dataHome),
43990
+ ...job.flags
43991
+ ].find((value5) => forbidden2.test(value5));
43879
43992
  return offending === undefined ? exports_Effect.succeed(job) : exports_Effect.fail(new ScheduleError({
43880
43993
  message: `Cannot schedule with ${JSON.stringify(offending)}: it contains ${described}.`
43881
43994
  }));
@@ -43884,6 +43997,7 @@ var posixWord = (value5) => /^[\w@+=:,./-]+$/.test(value5) ? value5 : `'${value5
43884
43997
  var posixScript = (job) => [
43885
43998
  "cd /",
43886
43999
  ...exports_Option.toArray(job.configHome).map((directory) => `export XDG_CONFIG_HOME=${posixWord(directory)}`),
44000
+ ...exports_Option.toArray(job.dataHome).map((directory) => `export XDG_DATA_HOME=${posixWord(directory)}`),
43887
44001
  `export PATH=${posixWord(job.nodeDirectory)}:"$PATH"`,
43888
44002
  `exec npx --yes ${packageSpec} ${["sync", ...job.flags].map(posixWord).join(" ")}`
43889
44003
  ].join("; ");
@@ -44174,13 +44288,13 @@ var startupSteps = exports_Effect.fn("Copilot.startupSteps")(function* (host, ta
44174
44288
  onSome: (file3) => path.basename(file3)
44175
44289
  });
44176
44290
  const exported = `export ${variable}=${homeWord(path, host, target, posixWord)}`;
44177
- const posix2 = (file3) => ({
44291
+ const posix3 = (file3) => ({
44178
44292
  file: file3,
44179
44293
  line: exported,
44180
44294
  satisfies: exportsTo(target, host.home),
44181
44295
  others: []
44182
44296
  });
44183
- const startup = yield* exports_Match.value(shell).pipe(exports_Match.when("zsh", () => exports_Effect.succeed(exports_Option.some(posix2(path.join(exports_Option.getOrElse(host.zshDirectory, () => host.home), ".zshrc"))))), exports_Match.when("bash", () => exports_Effect.map(bashStartup(host), (file3) => exports_Option.some(posix2(file3)))), exports_Match.when("fish", () => {
44297
+ const startup = yield* exports_Match.value(shell).pipe(exports_Match.when("zsh", () => exports_Effect.succeed(exports_Option.some(posix3(path.join(exports_Option.getOrElse(host.zshDirectory, () => host.home), ".zshrc"))))), exports_Match.when("bash", () => exports_Effect.map(bashStartup(host), (file3) => exports_Option.some(posix3(file3)))), exports_Match.when("fish", () => {
44184
44298
  const directory = path.join(exports_Option.getOrElse(host.configHome, () => path.join(host.home, ".config")), "fish");
44185
44299
  const line2 = `set -gx ${variable} ${homeWord(path, host, target, fishWord)}`;
44186
44300
  const snippets = path.join(directory, "conf.d");
@@ -45583,7 +45697,7 @@ function syncSummary(payload, request3, storedBuckets) {
45583
45697
  const dollars = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
45584
45698
  const sessions = new Set(payload.buckets.map((bucket) => bucket.sessionKey)).size;
45585
45699
  const cost = payload.buckets.reduce((sum3, bucket) => sum3 + (bucket.estimatedCostUsd ?? 0), 0);
45586
- const unknown2 = payload.buckets.some((bucket) => bucket.estimatedCostUsd === null);
45700
+ const unknown2 = payload.buckets.some((bucket) => bucket.unpricedRecords > 0);
45587
45701
  return [
45588
45702
  `Synced ${terminalText(payload.machine.label)} · ${request3.since} through ${request3.until} (${payload.timeZone})`,
45589
45703
  [
@@ -45614,6 +45728,11 @@ class SyncUnauthorized extends exports_Schema.TaggedError()("SyncUnauthorized",
45614
45728
  class SyncUnsupportedVersion extends exports_Schema.TaggedError()("SyncUnsupportedVersion", { message: exports_Schema.String }) {
45615
45729
  }
45616
45730
 
45731
+ class SyncSuperseded extends exports_Schema.TaggedError()("SyncSuperseded", {
45732
+ message: exports_Schema.String
45733
+ }) {
45734
+ }
45735
+
45617
45736
  class SyncRejected extends exports_Schema.TaggedError()("SyncRejected", {
45618
45737
  status: exports_Schema.Number,
45619
45738
  message: exports_Schema.String
@@ -45646,8 +45765,10 @@ var readResponse = exports_Effect.fn("Sync.readResponse")(function* (response, v
45646
45765
  }))), exports_Match.when(403, () => exports_Effect.fail(new SyncUnauthorized({
45647
45766
  status,
45648
45767
  message: "This API token may not upload for this machine ID."
45768
+ }))), exports_Match.when(409, () => exports_Effect.fail(new SyncSuperseded({
45769
+ message: "Not stored: superseded by a newer sync from this machine. Run sync again to send this window; if every sync says this, check this machine's clock."
45649
45770
  }))), exports_Match.when(422, () => exports_HttpClientResponse.schemaBodyJson(VersionRejection)(response).pipe(exports_Effect.mapError(() => new SyncRejected({ status, message: `The server rejected the upload (${status}).` })), exports_Effect.flatMap((rejection) => exports_Effect.fail(rejection.supportedVersions.includes(version) ? new SyncRejected({ status, message: `The server rejected the upload (${status}).` }) : new SyncUnsupportedVersion({
45650
- message: `The server accepts payload versions ${rejection.supportedVersions.join(", ") || "none"}, not ${version}; upgrade token-ledger.`
45771
+ message: `The server accepts payload versions ${rejection.supportedVersions.join(", ") || "none"}, not ${version}; update Toki or use a compatible token-ledger version.`
45651
45772
  }))))), exports_Match.orElse(() => exports_Effect.fail(new SyncRejected({ status, message: `The server rejected the upload (${status}).` }))));
45652
45773
  });
45653
45774
  var upload = exports_Effect.fn("Sync.upload")(function* (server, token, payload) {
@@ -45760,6 +45881,13 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
45760
45881
  if (pricing.status === "unavailable")
45761
45882
  yield* exports_Console.error("Pricing unavailable: unknown rates remain unpriced. Configure prices or rerun online.");
45762
45883
  const request3 = yield* ReportRequest.makeEffect({
45884
+ billingHints: configuration.billingHintsDirectory === undefined ? {
45885
+ directory: billingHintsDirectory(process.platform, home, yield* exports_Config.String("LOCALAPPDATA").pipe(exports_Config.option), yield* dataHome),
45886
+ required: false
45887
+ } : {
45888
+ directory: expand(configuration.billingHintsDirectory, path.dirname(configFile)),
45889
+ required: true
45890
+ },
45763
45891
  since: exports_Option.getOrElse(flags.since, () => defaultSince(today)),
45764
45892
  until: exports_Option.getOrElse(flags.until, () => today),
45765
45893
  timeZone,
@@ -45812,7 +45940,10 @@ var sync6 = exports_Command.make("sync", {
45812
45940
  const reachable = exports_Option.isSome(server) && exports_Option.isSome(token);
45813
45941
  if (exports_Option.isNone(unlisted2) && exports_Option.isNone(flags.since) && (reachable || flags.dryRun))
45814
45942
  yield* exports_Console.error(`${reachable ? "Could not ask the server whether this machine has uploaded before" : "No server and API token to ask whether this machine has uploaded before"}, so the window is ${request3.since} through ${request3.until}. If this is its first upload, send its earlier history with --since.`);
45815
- const first = whole ? yield* ledger.historyPayload(request3, client) : { payload: yield* ledger.syncPayload(request3, client), since: request3.since };
45943
+ const first = whole ? yield* ledger.historyPayload(request3, client, exports_Console.error) : {
45944
+ payload: yield* ledger.syncPayload(request3, client, exports_Console.error),
45945
+ since: request3.since
45946
+ };
45816
45947
  if (whole)
45817
45948
  yield* exports_Console.error(`First upload from this machine: taking in all the history found, from ${first.since}. Later syncs cover the last 14 days.${flags.provider === "all" ? "" : ` Only ${flags.provider} is included; the other tools' earlier history is not sent later by itself, so send it with --provider and --since.`}`);
45818
45949
  const examine = exports_Effect.fn("CLI.examine")(function* (payload) {
@@ -45848,19 +45979,25 @@ var sync6 = exports_Command.make("sync", {
45848
45979
  const send = (payload) => upload(server.value, token.value, payload).pipe(exports_Effect.result);
45849
45980
  const usual = exports_Effect.fn("CLI.usual")(function* (reason) {
45850
45981
  yield* exports_Console.error(`${reason} Uploading ${request3.since} through ${request3.until} instead; send earlier history in parts with --since and --until.`);
45851
- const payload = yield* ledger.syncPayload(request3, client);
45982
+ const payload = yield* ledger.syncPayload(request3, client, exports_Console.error);
45852
45983
  return (yield* examine(payload)) && flags.strict ? exports_Option.none() : exports_Option.some({ sent: { payload, since: request3.since }, outcome: yield* send(payload) });
45853
45984
  });
45854
45985
  const still = !whole || !(yield* hasUploaded(server.value, token.value, machine.id).pipe(exports_Effect.orElseSucceed(() => true)));
45855
45986
  let result6 = still ? exports_Option.some({ sent: first, outcome: yield* send(first.payload) }) : yield* usual("This machine has uploaded since it was asked about.");
45856
45987
  if (whole && exports_Option.isSome(result6) && exports_Result.isFailure(result6.value.outcome) && exports_Match.value(result6.value.outcome.failure).pipe(exports_Match.tag("SyncRejected", () => still), exports_Match.orElse(() => false)))
45857
45988
  result6 = yield* usual(result6.value.outcome.failure.message);
45989
+ if (exports_Option.isSome(result6) && exports_Result.isFailure(result6.value.outcome) && exports_Predicate.isTagged(result6.value.outcome.failure, "SyncSuperseded")) {
45990
+ const { since } = result6.value.sent;
45991
+ yield* exports_Console.error(`A newer sync from this machine reached the server first; scanning ${since} through ${request3.until} again.`);
45992
+ const payload = yield* ledger.syncPayload({ ...request3, since }, client, exports_Console.error);
45993
+ result6 = (yield* examine(payload)) && flags.strict ? exports_Option.none() : exports_Option.some({ sent: { payload, since }, outcome: yield* send(payload) });
45994
+ }
45858
45995
  if (exports_Option.isNone(result6))
45859
45996
  return yield* refused;
45860
45997
  const { sent, outcome } = result6.value;
45861
45998
  if (exports_Result.isFailure(outcome)) {
45862
45999
  yield* exports_Console.error(outcome.failure.message);
45863
- process.exitCode = exports_Match.value(outcome.failure).pipe(exports_Match.tag("SyncUnreachable", () => 3), exports_Match.tag("SyncUnauthorized", () => 4), exports_Match.tag("SyncUnsupportedVersion", () => 5), exports_Match.tag("SyncRejected", () => 6), exports_Match.exhaustive);
46000
+ process.exitCode = exports_Match.value(outcome.failure).pipe(exports_Match.tag("SyncUnreachable", () => 3), exports_Match.tag("SyncUnauthorized", () => 4), exports_Match.tag("SyncUnsupportedVersion", () => 5), exports_Match.tag("SyncRejected", () => 6), exports_Match.tag("SyncSuperseded", () => 7), exports_Match.exhaustive);
45864
46001
  return;
45865
46002
  }
45866
46003
  yield* exports_Console.log(syncSummary(sent.payload, { ...request3, since: sent.since }, outcome.success));
@@ -45904,6 +46041,7 @@ var scheduleInstall = exports_Command.make("install", {
45904
46041
  at: flags.at,
45905
46042
  nodeDirectory: path.dirname(process.execPath),
45906
46043
  configHome: yield* configHome,
46044
+ dataHome: yield* dataHome,
45907
46045
  flags: [
45908
46046
  ...exports_Option.match(config, { onNone: () => [], onSome: (file2) => ["--config", file2] }),
45909
46047
  ...exports_Option.match(flags.timeZone, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.5.1",
3
+ "version": "0.6.1",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {