@ponbac/token-ledger 0.11.0 → 0.11.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 +17 -1
  2. package/dist/main.js +122 -27
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -447,6 +447,22 @@ their common Git directory. Relative config paths resolve from the config file;
447
447
  `~/` resolves from the current developer's home. Directory mappings respect path
448
448
  boundaries, so `/work/app` never matches `/work/application`.
449
449
 
450
+ The CLI remembers network repository identities in its local application data directory
451
+ (`$XDG_DATA_HOME/token-ledger/repositories`, normally
452
+ `~/.local/share/token-ledger/repositories`; on Windows under `%LOCALAPPDATA%`, and on
453
+ macOS under `~/Library/Application Support`). Once a scan has saved a checkout or
454
+ worktree's network origin, later scans keep its repository even after that directory is deleted.
455
+ The nearest live or remembered repository wins, and explicit mappings take precedence; an existing directory
456
+ without Git is never assigned from an old identity. Each directory is keyed by a
457
+ hash, and saved identities omit credentials. Observing a local or absent origin
458
+ invalidates its former network identity, including after the directory is deleted.
459
+ The same applies to a remembered root observed to exist without Git metadata.
460
+ A history read or write failure stops
461
+ a real upload rather than replacing known attribution with missing metadata.
462
+ Local reports and dry runs warn about history failures and continue; they also
463
+ remember identities when the history directory is writable.
464
+ This history cannot recover directories deleted before the first scan with it.
465
+
450
466
  Grok logs currently lack project metadata in the usage parser. Copilot CLI can put
451
467
  repository metadata on a parent agent span rather than the counted request span;
452
468
  Token Ledger associates the two. If an export has no repository metadata, use a
@@ -946,7 +962,7 @@ For manual publication:
946
962
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
947
963
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
948
964
  the repository with lifecycle scripts disabled, and exercises the installed CLI.
949
- 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.11.0.tgz` (filename follows version).
965
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.11.1.tgz` (filename follows version).
950
966
  4. Inspect `npm publish ./dist/npm --dry-run`.
951
967
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
952
968
  prompt if requested.
package/dist/main.js CHANGED
@@ -32791,6 +32791,10 @@ var Day = exports_Schema.String.check(exports_Schema.isPattern(/^\d{4}-\d{2}-\d{
32791
32791
  return Number.isFinite(date2.getTime()) && date2.toISOString().slice(0, 10) === s;
32792
32792
  }));
32793
32793
  var ReportRequest = exports_Schema.Struct({
32794
+ repositoryHistory: exports_Schema.optionalKey(exports_Schema.Struct({
32795
+ directory: exports_Schema.NonEmptyString,
32796
+ required: exports_Schema.Boolean
32797
+ })),
32794
32798
  billingHints: exports_Schema.optionalKey(exports_Schema.Struct({ directory: exports_Schema.NonEmptyString, required: exports_Schema.Boolean })),
32795
32799
  since: Day,
32796
32800
  until: Day,
@@ -35011,6 +35015,63 @@ function priceRecord(record3, vendor) {
35011
35015
  return { usd, base };
35012
35016
  }
35013
35017
 
35018
+ // packages/core/src/repository-history.ts
35019
+ import { createHash as createHash3 } from "node:crypto";
35020
+ class RepositoryHistoryError extends exports_Schema.TaggedError()("RepositoryHistoryError", { message: exports_Schema.String }) {
35021
+ }
35022
+ var Entry = exports_Schema.Struct({
35023
+ version: exports_Schema.Literal(1),
35024
+ repository: exports_Schema.NullOr(exports_Schema.NonEmptyString.check(exports_Schema.isPattern(/^[^/\s@]+\/[^\r\n]+$/u)))
35025
+ });
35026
+ var repositoryHistory = exports_Effect.fn("RepositoryHistory.open")(function* (config, onWarning) {
35027
+ const fs = yield* exports_FileSystem.FileSystem;
35028
+ const path = yield* exports_Path.Path;
35029
+ const pending = new Map;
35030
+ const loaded = new Map;
35031
+ const directory = config?.directory;
35032
+ const filename = (root) => `${createHash3("sha256").update(path.resolve(root)).digest("hex")}.json`;
35033
+ const recover = (error2) => config?.required ? exports_Effect.fail(error2) : onWarning?.(error2.message) ?? exports_Effect.void;
35034
+ const read2 = exports_Effect.fn("RepositoryHistory.read")(function* (root) {
35035
+ if (directory === undefined)
35036
+ return null;
35037
+ const key = filename(root);
35038
+ const observed = pending.get(key);
35039
+ if (observed !== undefined)
35040
+ return observed;
35041
+ const cached3 = loaded.get(key);
35042
+ if (cached3 !== undefined)
35043
+ return cached3;
35044
+ const file2 = path.join(directory, key);
35045
+ const saved = yield* fs.readFileString(file2).pipe(exports_Effect.catchTag("PlatformError", (error2) => exports_Predicate.isTagged(error2.reason, "NotFound") ? exports_Effect.succeed(null) : exports_Effect.fail(error2)), exports_Effect.flatMap((text2) => text2 === null ? exports_Effect.succeed(null) : exports_Schema.decodeUnknownEffect(exports_Schema.fromJsonString(Entry))(text2)), exports_Effect.mapError(() => new RepositoryHistoryError({
35046
+ message: `Cannot read repository history at ${file2}. Fix its permissions or repair its contents before syncing. If it is damaged beyond repair, remove only this file; attribution for that deleted directory will then be unknown until restored.`
35047
+ })), exports_Effect.catchTag("RepositoryHistoryError", (error2) => recover(error2).pipe(exports_Effect.as(null))));
35048
+ loaded.set(key, saved);
35049
+ return saved;
35050
+ });
35051
+ const remember = (root, repository) => {
35052
+ const entry = exports_Schema.decodeUnknownOption(Entry)({ version: 1, repository });
35053
+ if (directory !== undefined && exports_Option.isSome(entry))
35054
+ pending.set(filename(root), entry.value);
35055
+ };
35056
+ const flush = exports_Effect.fn("RepositoryHistory.save")(function* () {
35057
+ if (directory === undefined || pending.size === 0)
35058
+ return;
35059
+ yield* exports_Effect.gen(function* () {
35060
+ yield* fs.makeDirectory(directory, { recursive: true, mode: 448 });
35061
+ for (const [key, entry] of pending) {
35062
+ const temporary = yield* fs.makeTempFileScoped({ directory, prefix: ".repository-" });
35063
+ yield* fs.writeFileString(temporary, JSON.stringify(entry) + `
35064
+ `);
35065
+ yield* fs.chmod(temporary, 384);
35066
+ yield* fs.rename(temporary, path.join(directory, key));
35067
+ }
35068
+ }).pipe(exports_Effect.scoped, exports_Effect.mapError(() => new RepositoryHistoryError({
35069
+ message: `Cannot save repository history in ${directory}.${config?.required ? " Sync stopped to preserve attribution." : ""}`
35070
+ })), exports_Effect.catchTag("RepositoryHistoryError", recover));
35071
+ });
35072
+ return { read: read2, remember, flush };
35073
+ });
35074
+
35014
35075
  // packages/core/src/projects.ts
35015
35076
  var scpRemote = /^(?:[^/@\s]+@)?(\[[^\]\s]+\]|[^/:\\\s]+):((?!\/\/).+)$/;
35016
35077
  function repositoryIdentity(remote) {
@@ -35029,9 +35090,10 @@ function isNetworkRemote(remote) {
35029
35090
  const parsed = URL.parse(value5);
35030
35091
  return parsed !== null && parsed.host !== "" ? ["http:", "https:", "ssh:", "git:", "ftp:", "ftps:"].includes(parsed.protocol) : scpRemote.test(value5);
35031
35092
  }
35032
- var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings) {
35093
+ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings, historyConfig, onWarning) {
35033
35094
  const fs = yield* exports_FileSystem.FileSystem;
35034
35095
  const path = yield* exports_Path.Path;
35096
+ const history = yield* repositoryHistory(historyConfig, onWarning);
35035
35097
  const resolved = new Map;
35036
35098
  const readOptional = (file2) => fs.readFileString(file2).pipe(exports_Effect.option);
35037
35099
  const discover = exports_Effect.fn("Projects.discover")(function* (cwd) {
@@ -35040,14 +35102,14 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
35040
35102
  return cached3;
35041
35103
  let current = cwd;
35042
35104
  let root = cwd;
35043
- let remote = null;
35105
+ let repository = null;
35044
35106
  while (true) {
35045
35107
  const git = path.join(current, ".git");
35046
- const stat3 = yield* fs.stat(git).pipe(exports_Effect.option);
35047
- if (exports_Option.isSome(stat3)) {
35108
+ const stat3 = yield* fs.stat(git).pipe(exports_Effect.result);
35109
+ if (exports_Result.isSuccess(stat3)) {
35048
35110
  root = current;
35049
35111
  let gitDirectory = git;
35050
- if (stat3.value.type === "File") {
35112
+ if (stat3.success.type === "File") {
35051
35113
  const pointer = yield* readOptional(git);
35052
35114
  const match10 = /^gitdir:\s*(.+)\s*$/m.exec(exports_Option.getOrElse(pointer, () => ""));
35053
35115
  if (match10?.[1])
@@ -35061,26 +35123,40 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
35061
35123
  const config = yield* readOptional(path.join(gitDirectory, "config"));
35062
35124
  const origin = /\[remote "origin"\]([^[]*)/.exec(exports_Option.getOrElse(config, () => ""));
35063
35125
  const url = /^\s*url\s*=\s*(.+)$/m.exec(origin?.[1] ?? "");
35064
- if (url?.[1])
35065
- remote = url[1].trim();
35126
+ if (url?.[1]) {
35127
+ const remote = url[1].trim();
35128
+ repository = RepositoryReference.Remote({ value: remote });
35129
+ }
35130
+ if (exports_Option.isSome(config))
35131
+ history.remember(current, url?.[1] && isNetworkRemote(url[1].trim()) ? repositoryIdentity(url[1].trim()) : null);
35066
35132
  break;
35067
35133
  }
35134
+ if (historyConfig !== undefined && exports_Predicate.isTagged(stat3.failure.reason, "NotFound")) {
35135
+ const exists2 = yield* fs.exists(current).pipe(exports_Effect.option);
35136
+ if (exports_Option.isSome(exists2)) {
35137
+ const previous = yield* history.read(current);
35138
+ if (previous !== null && !exists2.value) {
35139
+ root = current;
35140
+ repository = previous.repository === null ? null : RepositoryReference.Identity({ value: previous.repository });
35141
+ break;
35142
+ }
35143
+ if (previous !== null && previous.repository !== null)
35144
+ history.remember(current, null);
35145
+ }
35146
+ }
35068
35147
  const parent = path.dirname(current);
35069
35148
  if (parent === current)
35070
35149
  break;
35071
35150
  current = parent;
35072
35151
  }
35073
- const identity3 = { root, remote };
35152
+ const identity3 = { root, repository };
35074
35153
  resolved.set(cwd, identity3);
35075
35154
  return identity3;
35076
35155
  });
35077
- return exports_Effect.fn("Projects.resolve")(function* (record3, fixedProject) {
35156
+ const resolve7 = exports_Effect.fn("Projects.resolve")(function* (record3, fixedProject) {
35078
35157
  if (fixedProject !== undefined)
35079
35158
  return { project: fixedProject, shareable: true };
35080
35159
  const cwd = record3.cwd;
35081
- const identity3 = cwd === null ? null : yield* discover(cwd);
35082
- const rawRemote = record3.repository?.value ?? identity3?.remote ?? null;
35083
- const remote = rawRemote === null ? null : repositoryIdentity(rawRemote);
35084
35160
  let bestPath = null;
35085
35161
  for (const mapping of mappings) {
35086
35162
  for (const prefix3 of mapping.paths) {
@@ -35092,17 +35168,21 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
35092
35168
  }
35093
35169
  if (bestPath !== null)
35094
35170
  return { project: bestPath.project, shareable: true };
35171
+ const identity3 = record3.repository !== null || cwd === null ? null : yield* discover(cwd);
35172
+ const reference = record3.repository ?? identity3?.repository ?? null;
35173
+ const remote = reference === null ? null : exports_Predicate.isTagged(reference, "Identity") ? reference.value : repositoryIdentity(reference.value);
35095
35174
  for (const mapping of mappings) {
35096
35175
  if (remote !== null && mapping.repositories.some((entry) => repositoryIdentity(entry) === remote))
35097
35176
  return { project: mapping.project, shareable: true };
35098
35177
  }
35099
- if (rawRemote !== null && remote !== null)
35178
+ if (reference !== null && remote !== null)
35100
35179
  return {
35101
35180
  project: remote,
35102
- shareable: remote !== "" && (record3.repository?._tag === "Identity" || isNetworkRemote(rawRemote))
35181
+ shareable: remote !== "" && (exports_Predicate.isTagged(reference, "Identity") || isNetworkRemote(reference.value))
35103
35182
  };
35104
35183
  return { project: identity3?.root ?? "Unassigned", shareable: false };
35105
35184
  });
35185
+ return { resolve: resolve7, flush: history.flush };
35106
35186
  });
35107
35187
 
35108
35188
  // packages/core/src/providers/shared.ts
@@ -36064,7 +36144,7 @@ function piParser(file2) {
36064
36144
  }
36065
36145
 
36066
36146
  // packages/core/src/sync.ts
36067
- import { createHash as createHash3 } from "node:crypto";
36147
+ import { createHash as createHash4 } from "node:crypto";
36068
36148
  var hour = 3600000;
36069
36149
  var UtcInstant = exports_Schema.String.check(exports_Schema.isPattern(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/), exports_Schema.makeFilter((s) => {
36070
36150
  const ms = Date.parse(s);
@@ -36205,7 +36285,7 @@ function hourRange(range3) {
36205
36285
  };
36206
36286
  }
36207
36287
  function sessionKey(provider, session) {
36208
- return createHash3("sha256").update(`${provider}\x00${session}`).digest("hex").slice(0, 32);
36288
+ return createHash4("sha256").update(`${provider}\x00${session}`).digest("hex").slice(0, 32);
36209
36289
  }
36210
36290
  function sessionBuckets(pricing) {
36211
36291
  const totals = new Map;
@@ -36304,9 +36384,9 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36304
36384
  const request3 = yield* exports_Schema.decodeUnknownEffect(ReportRequest)(input).pipe(exports_Effect.mapError(() => new InvalidRequest({
36305
36385
  message: "Expected an ordered inclusive day window, a named time zone, and valid sources, mappings, and prices."
36306
36386
  })));
36307
- 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)))) {
36387
+ if (request3.repositoryHistory !== undefined && !path.isAbsolute(request3.repositoryHistory.directory) || 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)))) {
36308
36388
  return yield* new InvalidRequest({
36309
- message: "Source, billing hint, and project mapping paths must be absolute."
36389
+ message: "Source, repository history, billing hint, and project mapping paths must be absolute."
36310
36390
  });
36311
36391
  }
36312
36392
  return request3;
@@ -36317,7 +36397,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36317
36397
  const routing = notice !== undefined && request3.billingHints !== undefined && exports_Result.isSuccess(hints) ? chatgptRequests : undefined;
36318
36398
  if (exports_Result.isFailure(hints))
36319
36399
  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;
36320
- const resolveProject = yield* projectResolver(request3.projects).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
36400
+ const projects = yield* projectResolver(request3.projects, request3.repositoryHistory, onWarning).pipe(exports_Effect.provideService(exports_FileSystem.FileSystem, fs), exports_Effect.provideService(exports_Path.Path, path));
36321
36401
  const seen = new Set;
36322
36402
  const visitedFiles = new Set;
36323
36403
  const settledFiles = new Set;
@@ -36344,7 +36424,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36344
36424
  return;
36345
36425
  }
36346
36426
  seen.add(id2);
36347
- collect2(record3, yield* resolveProject(record3, source.project), slot);
36427
+ collect2(record3, yield* projects.resolve(record3, source.project), slot);
36348
36428
  });
36349
36429
  const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6, fromSession = false) {
36350
36430
  malformedLines += result6.malformed;
@@ -36432,6 +36512,8 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36432
36512
  }
36433
36513
  }).pipe(exports_Effect.result);
36434
36514
  if (exports_Result.isFailure(result6)) {
36515
+ if (exports_Predicate.isTagged(result6.failure, "RepositoryHistoryError"))
36516
+ return yield* result6.failure;
36435
36517
  if (entry === source.path && exports_Predicate.isTagged(result6.failure, "PlatformError") && exports_Predicate.isTagged(result6.failure.reason, "NotFound"))
36436
36518
  missingRoot = true;
36437
36519
  failedFiles++;
@@ -36479,10 +36561,15 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36479
36561
  yield* legacy;
36480
36562
  for (const copy4 of inherited)
36481
36563
  yield* copy4;
36564
+ yield* projects.flush();
36482
36565
  return coverage.map((summarize) => summarize());
36483
36566
  });
36484
- const report = exports_Effect.fn("Ledger.report")(function* (input) {
36485
- const request3 = yield* decode5(input);
36567
+ const report = exports_Effect.fn("Ledger.report")(function* (input, onWarning) {
36568
+ const decoded = yield* decode5(input);
36569
+ const request3 = decoded.repositoryHistory === undefined ? decoded : {
36570
+ ...decoded,
36571
+ repositoryHistory: { ...decoded.repositoryHistory, required: false }
36572
+ };
36486
36573
  const window = dayWindow(request3.since, request3.until, request3.timeZone);
36487
36574
  const rows = new Map;
36488
36575
  const coverage = yield* scan2(request3, window, window.dayOf, (record3, { project }, day) => {
@@ -36496,7 +36583,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
36496
36583
  pricePerMillion: base,
36497
36584
  ...addUsage(rows.get(key), record3.tokens, usd)
36498
36585
  });
36499
- });
36586
+ }, undefined, onWarning);
36500
36587
  return UsageReport.make({
36501
36588
  version: 2,
36502
36589
  since: request3.since,
@@ -43933,7 +44020,7 @@ var runWith2 = (command, config) => {
43933
44020
  // packages/cli/package.json
43934
44021
  var package_default = {
43935
44022
  name: "@ponbac/token-ledger",
43936
- version: "0.11.0",
44023
+ version: "0.11.1",
43937
44024
  private: true,
43938
44025
  type: "module",
43939
44026
  bin: {
@@ -43969,10 +44056,14 @@ var dataHome = exports_Effect.gen(function* () {
43969
44056
  const path = yield* exports_Path.Path;
43970
44057
  return yield* exports_Config.String("XDG_DATA_HOME").pipe(exports_Config.option, exports_Effect.map(exports_Option.filter((directory) => path.isAbsolute(directory))));
43971
44058
  });
43972
- function billingHintsDirectory(platform, home, localAppData, xdgDataHome) {
44059
+ function tokenLedgerDataDirectory(platform, home, localAppData, xdgDataHome) {
43973
44060
  const path = platform === "win32" ? win322 : posix2;
43974
44061
  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"))));
43975
- return path.join(root, "token-ledger", "billing-hints");
44062
+ return path.join(root, "token-ledger");
44063
+ }
44064
+ function billingHintsDirectory(platform, home, localAppData, xdgDataHome) {
44065
+ const path = platform === "win32" ? win322 : posix2;
44066
+ return path.join(tokenLedgerDataDirectory(platform, home, localAppData, xdgDataHome), "billing-hints");
43976
44067
  }
43977
44068
  // node_modules/.bun/jsonc-parser@3.3.1/node_modules/jsonc-parser/lib/esm/impl/scanner.js
43978
44069
  function createScanner(text2, ignoreTrivia = false) {
@@ -47413,6 +47504,10 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
47413
47504
  if (pricing.status === "unavailable")
47414
47505
  yield* exports_Console.error("Pricing unavailable: unknown rates remain unpriced. Configure prices or rerun online.");
47415
47506
  const request3 = yield* ReportRequest.makeEffect({
47507
+ repositoryHistory: {
47508
+ directory: path.join(tokenLedgerDataDirectory(process.platform, home, yield* exports_Config.String("LOCALAPPDATA").pipe(exports_Config.option), yield* dataHome), "repositories"),
47509
+ required: !flags.dryRun
47510
+ },
47416
47511
  billingHints: configuration.billingHintsDirectory === undefined ? {
47417
47512
  directory: billingHintsDirectory(process.platform, home, yield* exports_Config.String("LOCALAPPDATA").pipe(exports_Config.option), yield* dataHome),
47418
47513
  required: false
@@ -47437,7 +47532,7 @@ var report = exports_Command.make("report", {
47437
47532
  }, exports_Effect.fn("CLI.report")(function* (flags) {
47438
47533
  const { request: request3 } = yield* usageRequest(flags, (today) => `${today.slice(0, 7)}-01`);
47439
47534
  const ledger = yield* Ledger;
47440
- const result6 = yield* ledger.report(request3);
47535
+ const result6 = yield* ledger.report(request3, exports_Console.error);
47441
47536
  const output = exports_Match.value(flags.json ? "json" : flags.format).pipe(exports_Match.when("json", () => JSON.stringify(result6, null, 2)), exports_Match.when("csv", () => csv(result6)), exports_Match.when("table", () => table2(result6, process.stdout.columns ?? 120, process.stdout.isTTY && process.stdout.hasColors())), exports_Match.exhaustive);
47442
47537
  yield* exports_Console.log(output);
47443
47538
  for (const source of result6.coverage) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {