@ponbac/token-ledger 0.2.0 → 0.3.0

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 +19 -11
  2. package/dist/main.js +135 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -93,15 +93,16 @@ node packages/cli/dist/main.js report
93
93
 
94
94
  ## Collection coverage
95
95
 
96
- | Provider | Default input | Coverage |
97
- | -------------------------- | ------------------------------------------------------ | ---------------------------------------------------------------------------- |
98
- | Codex | `~/.codex/sessions` and `archived_sessions` | Rollout token events, model switches, session and working-directory metadata |
99
- | Claude Code | `~/.claude/projects` | Assistant usage records; repeated message blocks counted once |
100
- | Grok Build | `~/.grok/sessions/**/updates.jsonl` | Saved completed turns and their model breakdowns |
101
- | Copilot CLI / VS Code Chat | `~/.copilot/otel` or `COPILOT_OTEL_FILE_EXPORTER_PATH` | JSONL request spans, VS Code inference events, and SQLite span exports |
102
-
103
- `CODEX_HOME`, `CLAUDE_CONFIG_DIR`, `GROK_HOME`, and `COPILOT_HOME` override the usual
104
- homes. Configuration supports additional accounts, machines' exported histories,
96
+ | Provider | Default input | Coverage |
97
+ | -------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------- |
98
+ | Codex | `~/.codex/sessions` and `archived_sessions` | Rollout token events, model switches, session and working-directory metadata |
99
+ | Claude Code | `~/.claude/projects` | Assistant usage records; repeated message blocks counted once |
100
+ | Grok Build | `~/.grok/sessions/**/updates.jsonl` | Saved completed turns and their model breakdowns |
101
+ | Copilot CLI / VS Code Chat | `~/.copilot/otel` or `COPILOT_OTEL_FILE_EXPORTER_PATH` | JSONL request spans, VS Code inference events, and SQLite span exports |
102
+ | Pi | `~/.pi/agent/sessions` or `PI_CODING_AGENT_SESSION_DIR` | Assistant messages on every branch, plus usage, compaction, and summary entries |
103
+
104
+ `CODEX_HOME`, `CLAUDE_CONFIG_DIR`, `GROK_HOME`, `COPILOT_HOME`, and `PI_CODING_AGENT_DIR`
105
+ override the usual homes. Configuration supports additional accounts, machines' exported histories,
105
106
  and arbitrary source directories. Histories are opened read-only, including work
106
107
  performed outside T3 Code.
107
108
 
@@ -577,7 +578,7 @@ refused, `5` unsupported payload version (upgrade token-ledger), `6` payload rej
577
578
  - JSONL files are streamed, while Copilot buffers accounting records and span links
578
579
  per source for attribution; SQLite imports read the selected accounting rows. Each
579
580
  run rescans its sources, and memory grows with observed usage records.
580
- - Transcripts are append-only, so a Codex, Claude Code, or Grok file last modified more
581
+ - Transcripts are append-only, so a Codex, Claude Code, Grok, or Pi file last modified more
581
582
  than 36 hours before the window starts is not opened; coverage counts these as
582
583
  `filesBeforeWindow`. This trusts modification times: a history restored with times
583
584
  earlier than its last events is missed, so name such a file as the source itself,
@@ -590,6 +591,13 @@ refused, `5` unsupported payload version (upgrade token-ledger), `6` payload rej
590
591
  leading burst; it is not lost usage, so coverage stays `ok`. `partial` means real loss:
591
592
  malformed lines, usage events that could not be normalized, or, in a report, paths
592
593
  that could not be read.
594
+ - Pi `/fork` and `/clone` sessions copy their parent's entries unchanged. Each copied
595
+ request is counted once, for the session that made it; when that session's file is not
596
+ among the files read, it is counted for the fork. Pi names no model for nested tool
597
+ work or for compaction and branch summaries; those tokens are reported under the model
598
+ `unknown` and stay unpriced. Pi's own recorded costs are not used.
599
+ - A Pi `sessionDir` setting is not read; when sessions live outside the default
600
+ directory, set `PI_CODING_AGENT_SESSION_DIR` or configure a `pi` source.
593
601
  - A truncated or malformed JSONL line is skipped and counted. No prompts are printed
594
602
  in diagnostics. An active session can change during a scan; this is not a transactional snapshot.
595
603
  - Coverage diagnostics describe every opened file, including its events outside the
@@ -611,7 +619,7 @@ For manual publication:
611
619
  1. Log in with `npm login` using an account with publish access to `@ponbac/token-ledger`.
612
620
  2. Run `bun run check`. The CLI test packs the package, installs it offline outside
613
621
  the repository with lifecycle scripts disabled, and exercises the installed CLI.
614
- 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.2.0.tgz` (filename follows version).
622
+ 3. Run `bun run pack` to create `dist/ponbac-token-ledger-0.3.0.tgz` (filename follows version).
615
623
  4. Inspect `npm publish ./dist/npm --dry-run`.
616
624
  5. Publish with `npm publish ./dist/npm --access public`; complete npm's authentication
617
625
  prompt if requested.
package/dist/main.js CHANGED
@@ -32687,7 +32687,7 @@ function dayWindow(since, until, timeZone) {
32687
32687
  }
32688
32688
 
32689
32689
  // packages/core/src/model.ts
32690
- var Provider = exports_Schema.Literals(["codex", "claude", "grok", "copilot"]);
32690
+ var Provider = exports_Schema.Literals(["codex", "claude", "grok", "copilot", "pi"]);
32691
32691
  var Tokens = exports_Schema.Struct({
32692
32692
  input: exports_Schema.Natural,
32693
32693
  cacheRead: exports_Schema.Natural,
@@ -34428,6 +34428,7 @@ var projectResolver = exports_Effect.fn("Projects.resolver")(function* (mappings
34428
34428
  // packages/core/src/providers/shared.ts
34429
34429
  var empty14 = {
34430
34430
  records: [],
34431
+ inherited: [],
34431
34432
  hints: [],
34432
34433
  malformed: 0,
34433
34434
  skipped: 0,
@@ -34898,6 +34899,103 @@ function grokParser(file2) {
34898
34899
  };
34899
34900
  }
34900
34901
 
34902
+ // packages/core/src/providers/pi.ts
34903
+ var decodeKind = exports_Schema.decodeUnknownOption(exports_Schema.Struct({ type: exports_Schema.String }));
34904
+ var decodeHeader = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
34905
+ id: exports_Schema.optionalKey(exports_Schema.String),
34906
+ timestamp: exports_Schema.optionalKey(exports_Schema.String),
34907
+ cwd: exports_Schema.optionalKey(exports_Schema.String),
34908
+ parentSession: exports_Schema.optionalKey(exports_Schema.String)
34909
+ }));
34910
+ var decodeWrapper = exports_Schema.decodeUnknownOption(exports_Schema.Struct({ message: exports_Schema.Unknown }));
34911
+ var decodeCarrier = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
34912
+ role: exports_Schema.optionalKey(exports_Schema.String),
34913
+ usage: exports_Schema.optionalKey(exports_Schema.Unknown)
34914
+ }));
34915
+ var decodeStamp = exports_Schema.decodeUnknownOption(exports_Schema.Struct({ id: exports_Schema.optionalKey(exports_Schema.String), timestamp: exports_Schema.String }));
34916
+ var decodeNames = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
34917
+ provider: exports_Schema.optionalKey(exports_Schema.String),
34918
+ model: exports_Schema.optionalKey(exports_Schema.String),
34919
+ responseModel: exports_Schema.optionalKey(exports_Schema.String)
34920
+ }));
34921
+ var decodeCounts = exports_Schema.decodeUnknownOption(exports_Schema.Struct({
34922
+ input: exports_Schema.Natural,
34923
+ output: exports_Schema.Natural,
34924
+ cacheRead: exports_Schema.Natural,
34925
+ cacheWrite: exports_Schema.Natural
34926
+ }));
34927
+ var unattributedModel = "unknown";
34928
+ var chatGptSignIn = "openai-codex";
34929
+ function piParser(file2) {
34930
+ let session = file2;
34931
+ let cwd = null;
34932
+ let sawHeader = false;
34933
+ let forked = null;
34934
+ return {
34935
+ parse(line, lineNumber) {
34936
+ const raw2 = exports_Option.getOrNull(decodeJson(line));
34937
+ if (raw2 === null)
34938
+ return malformed;
34939
+ const type3 = exports_Option.getOrNull(decodeKind(raw2))?.type;
34940
+ if (type3 === "session") {
34941
+ if (sawHeader)
34942
+ return empty14;
34943
+ const header = exports_Option.getOrNull(decodeHeader(raw2));
34944
+ if (header === null)
34945
+ return malformed;
34946
+ sawHeader = true;
34947
+ session = header.id ?? file2;
34948
+ cwd = header.cwd ?? null;
34949
+ forked = header.parentSession === undefined ? null : timestamp(header.timestamp);
34950
+ return empty14;
34951
+ }
34952
+ const wrapper = type3 === "message" ? exports_Option.getOrNull(decodeWrapper(raw2)) : { message: raw2 };
34953
+ const carrier = wrapper === null ? null : exports_Option.getOrNull(decodeCarrier(wrapper.message));
34954
+ if (wrapper === null || carrier === null)
34955
+ return type3 === "message" ? malformed : empty14;
34956
+ const assistant = type3 === "message" && carrier.role === "assistant";
34957
+ const attributed = assistant || type3 === "usage";
34958
+ const counted = attributed || type3 === "message" && carrier.role === "toolResult" || type3 === "compaction" || type3 === "branch_summary";
34959
+ const usage = carrier.usage;
34960
+ if (usage === undefined || usage === null)
34961
+ return attributed ? skipped : empty14;
34962
+ if (!counted)
34963
+ return skipped;
34964
+ const stamp = exports_Option.getOrNull(decodeStamp(raw2));
34965
+ const names = exports_Option.getOrNull(decodeNames(wrapper.message));
34966
+ const count2 = exports_Option.getOrNull(decodeCounts(usage));
34967
+ const ms = timestamp(stamp?.timestamp);
34968
+ if (stamp === null || names === null || count2 === null || ms === null)
34969
+ return skipped;
34970
+ const model = attributed ? names.responseModel ?? names.model : undefined;
34971
+ const record3 = {
34972
+ provider: "pi",
34973
+ id: stamp.id === undefined ? `${file2}:${lineNumber}` : `${stamp.id}:${stamp.timestamp}`,
34974
+ session,
34975
+ timestamp: ms,
34976
+ model: model ?? unattributedModel,
34977
+ cwd,
34978
+ repository: null,
34979
+ tokens: count2
34980
+ };
34981
+ const copied = forked !== null && ms < forked;
34982
+ return {
34983
+ ...empty14,
34984
+ records: copied ? [] : [record3],
34985
+ inherited: copied ? [record3] : [],
34986
+ hints: attributed && names.provider === chatGptSignIn && totalTokens(count2) > 0 ? [{ timestamp: ms, plan: names.provider }] : [],
34987
+ warnings: [
34988
+ ...sawHeader ? [] : ["Pi session header is missing; attribution uses the file identity."],
34989
+ ...stamp.id === undefined ? ["Some Pi entries lack stable IDs; forked sessions may overlap."] : [],
34990
+ ...model === undefined ? [
34991
+ "Pi tool and summary usage names no model; these tokens remain unpriced unless an explicit rate is supplied."
34992
+ ] : []
34993
+ ]
34994
+ };
34995
+ }
34996
+ };
34997
+ }
34998
+
34901
34999
  // packages/core/src/sync.ts
34902
35000
  import { createHash as createHash2 } from "node:crypto";
34903
35001
  var hour = 3600000;
@@ -35091,6 +35189,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35091
35189
  const visitedFiles = new Set;
35092
35190
  const settledFiles = new Set;
35093
35191
  const coverage = [];
35192
+ const inherited = [];
35094
35193
  for (const source of request3.sources) {
35095
35194
  let files = 0;
35096
35195
  let filesBeforeWindow = 0;
@@ -35101,6 +35200,15 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35101
35200
  let missingRoot = false;
35102
35201
  const warnings = new Set;
35103
35202
  const copilot = source.provider === "copilot" ? copilotParser(source.path) : undefined;
35203
+ const place = exports_Effect.fn("Ledger.placeRecord")(function* (record3, slot) {
35204
+ const id2 = `${record3.provider}:${record3.id}`;
35205
+ if (seen.has(id2)) {
35206
+ duplicates++;
35207
+ return;
35208
+ }
35209
+ seen.add(id2);
35210
+ collect2(record3, yield* resolveProject(record3, source.project), slot);
35211
+ });
35104
35212
  const accept2 = exports_Effect.fn("Ledger.acceptRecords")(function* (result6) {
35105
35213
  malformedLines += result6.malformed;
35106
35214
  skippedRecords += result6.skipped;
@@ -35112,15 +35220,13 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35112
35220
  }
35113
35221
  for (const record3 of result6.records) {
35114
35222
  const slot = locate(record3.timestamp);
35115
- if (slot === undefined || totalTokens(record3.tokens) === 0)
35116
- continue;
35117
- const id2 = `${record3.provider}:${record3.id}`;
35118
- if (seen.has(id2)) {
35119
- duplicates++;
35120
- continue;
35121
- }
35122
- seen.add(id2);
35123
- collect2(record3, yield* resolveProject(record3, source.project), slot);
35223
+ if (slot !== undefined && totalTokens(record3.tokens) > 0)
35224
+ yield* place(record3, slot);
35225
+ }
35226
+ for (const record3 of result6.inherited) {
35227
+ const slot = locate(record3.timestamp);
35228
+ if (slot !== undefined && totalTokens(record3.tokens) > 0)
35229
+ inherited.push(place(record3, slot));
35124
35230
  }
35125
35231
  });
35126
35232
  const scanFile = exports_Effect.fn("Ledger.scanFile")(function* (file2, info2) {
@@ -35137,7 +35243,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35137
35243
  return;
35138
35244
  }
35139
35245
  visitedFiles.add(identity3);
35140
- const parser = exports_Match.value(source.provider).pipe(exports_Match.when("codex", () => codexParser(canonical)), exports_Match.when("claude", () => claudeParser(canonical)), exports_Match.when("grok", () => grokParser(canonical)), exports_Match.when("copilot", () => copilot ?? copilotParser(canonical)), exports_Match.exhaustive);
35246
+ const parser = exports_Match.value(source.provider).pipe(exports_Match.when("codex", () => codexParser(canonical)), exports_Match.when("claude", () => claudeParser(canonical)), exports_Match.when("grok", () => grokParser(canonical)), exports_Match.when("copilot", () => copilot ?? copilotParser(canonical)), exports_Match.when("pi", () => piParser(canonical)), exports_Match.exhaustive);
35141
35247
  let lineNumber = 0;
35142
35248
  files++;
35143
35249
  const lines = source.provider === "copilot" && canonical.endsWith(".db") ? exports_Stream.fromIterableEffect(copilotDatabaseLines(canonical)) : fs.stream(canonical).pipe(exports_Stream.decodeText, exports_Stream.splitLines);
@@ -35180,7 +35286,7 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35180
35286
  if (copilot?.finish !== undefined)
35181
35287
  yield* accept2(copilot.finish());
35182
35288
  const status = missingRoot ? "missing" : failedFiles > 0 && files === 0 ? "failed" : failedFiles > 0 || malformedLines > 0 || skippedRecords > 0 ? "partial" : "ok";
35183
- coverage.push({
35289
+ coverage.push(() => ({
35184
35290
  provider: source.provider,
35185
35291
  source: source.path,
35186
35292
  status,
@@ -35191,9 +35297,11 @@ class Ledger extends exports_Context.Service()("token-ledger/Ledger") {
35191
35297
  skippedRecords,
35192
35298
  duplicates,
35193
35299
  warnings: [...warnings]
35194
- });
35300
+ }));
35195
35301
  }
35196
- return coverage;
35302
+ for (const copy4 of inherited)
35303
+ yield* copy4;
35304
+ return coverage.map((summarize) => summarize());
35197
35305
  });
35198
35306
  const report = exports_Effect.fn("Ledger.report")(function* (input) {
35199
35307
  const request3 = yield* decode5(input);
@@ -42575,7 +42683,7 @@ var runWith2 = (command, config) => {
42575
42683
  // packages/cli/package.json
42576
42684
  var package_default = {
42577
42685
  name: "@ponbac/token-ledger",
42578
- version: "0.2.0",
42686
+ version: "0.3.0",
42579
42687
  private: true,
42580
42688
  type: "module",
42581
42689
  bin: {
@@ -44028,7 +44136,7 @@ function usageFlags(sinceDefault, strict) {
44028
44136
  since: exports_Flag.String("since").pipe(exports_Flag.withSchema(Day), exports_Flag.optional, exports_Flag.withDescription(`First local day, inclusive; defaults to ${sinceDefault}`)),
44029
44137
  until: exports_Flag.String("until").pipe(exports_Flag.withSchema(Day), exports_Flag.optional, exports_Flag.withDescription("Last local day, inclusive; defaults to today")),
44030
44138
  timeZone: exports_Flag.String("time-zone").pipe(exports_Flag.withSchema(exports_Schema.TimeZoneNamedFromString), exports_Flag.optional, exports_Flag.withDescription("IANA time zone for days and the window, such as Europe/Stockholm; defaults to the system zone")),
44031
- provider: exports_Flag.Literals("provider", ["all", "codex", "claude", "grok", "copilot"]).pipe(exports_Flag.withDefault("all")),
44139
+ provider: exports_Flag.Literals("provider", ["all", "codex", "claude", "grok", "copilot", "pi"]).pipe(exports_Flag.withDefault("all")),
44032
44140
  project: exports_Flag.String("project").pipe(exports_Flag.withSchema(ProjectName), exports_Flag.optional, exports_Flag.withDescription("Assign all selected usage to this project")),
44033
44141
  config: exports_Flag.String("config").pipe(exports_Flag.optional, exports_Flag.withDescription("JSON config; otherwise reads ./token-ledger.json when present")),
44034
44142
  offline: exports_Flag.Boolean("offline").pipe(exports_Flag.withDefault(false), exports_Flag.withDescription("Use only cached and configured prices")),
@@ -44053,6 +44161,8 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
44053
44161
  const grokHome = yield* exports_Config.String("GROK_HOME").pipe(exports_Config.withDefault(path.join(home, ".grok")));
44054
44162
  const copilotHome = yield* exports_Config.String("COPILOT_HOME").pipe(exports_Config.withDefault(path.join(home, ".copilot")));
44055
44163
  const copilotFile = yield* exports_Config.String("COPILOT_OTEL_FILE_EXPORTER_PATH").pipe(exports_Config.option);
44164
+ const piHome = yield* exports_Config.String("PI_CODING_AGENT_DIR").pipe(exports_Config.withDefault(path.join(home, ".pi", "agent")));
44165
+ const piSessions = yield* exports_Config.String("PI_CODING_AGENT_SESSION_DIR").pipe(exports_Config.option);
44056
44166
  const defaults2 = [
44057
44167
  { provider: "codex", path: path.join(expand(codexHome), "sessions") },
44058
44168
  { provider: "codex", path: path.join(expand(codexHome), "archived_sessions") },
@@ -44064,6 +44174,13 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
44064
44174
  onNone: () => path.join(expand(copilotHome), "otel"),
44065
44175
  onSome: (value5) => expand(value5)
44066
44176
  })
44177
+ },
44178
+ {
44179
+ provider: "pi",
44180
+ path: exports_Option.match(piSessions, {
44181
+ onNone: () => path.join(expand(piHome), "sessions"),
44182
+ onSome: (value5) => expand(value5)
44183
+ })
44067
44184
  }
44068
44185
  ];
44069
44186
  let sources = configuration.sources === undefined ? defaults2 : configuration.sources.map((source) => ({
@@ -44075,7 +44192,7 @@ var usageRequest = exports_Effect.fn("CLI.usageRequest")(function* (flags, defau
44075
44192
  if (exports_Option.isSome(flags.source)) {
44076
44193
  if (flags.provider === "all")
44077
44194
  return yield* new CliError2({
44078
- message: "--source requires --provider codex, claude, grok, or copilot."
44195
+ message: "--source requires --provider codex, claude, grok, copilot, or pi."
44079
44196
  });
44080
44197
  sources = [{ provider: flags.provider, path: expand(flags.source.value) }];
44081
44198
  }
@@ -44206,7 +44323,7 @@ var scheduleInstall = exports_Command.make("install", {
44206
44323
  at: exports_Flag.String("at").pipe(exports_Flag.withDefault("12:30"), exports_Flag.withSchema(TimeOfDay), exports_Flag.withDescription("Daily local time as HH:MM; defaults to 12:30")),
44207
44324
  config: exports_Flag.String("config").pipe(exports_Flag.optional, exports_Flag.withDescription("JSON config for the scheduled sync; ./token-ledger.json is not read")),
44208
44325
  timeZone: exports_Flag.String("time-zone").pipe(exports_Flag.withSchema(exports_Schema.TimeZoneNamedFromString), exports_Flag.optional, exports_Flag.withDescription("IANA time zone for the scheduled sync; defaults to the system zone")),
44209
- provider: exports_Flag.Literals("provider", ["all", "codex", "claude", "grok", "copilot"]).pipe(exports_Flag.withDefault("all")),
44326
+ provider: exports_Flag.Literals("provider", ["all", "codex", "claude", "grok", "copilot", "pi"]).pipe(exports_Flag.withDefault("all")),
44210
44327
  dryRun: exports_Flag.Boolean("dry-run").pipe(exports_Flag.withDefault(false), exports_Flag.withDescription("Print the files and commands; change nothing"))
44211
44328
  }, exports_Effect.fn("CLI.scheduleInstall")(function* (flags) {
44212
44329
  const fs = yield* exports_FileSystem.FileSystem;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ponbac/token-ledger",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Local AI token usage and API-equivalent cost reports by project",
5
5
  "license": "MIT",
6
6
  "engines": {