Skip to content

Commit 9afde1c

Browse files
authored
feat(pi): include native sessions in usage reports (pingdotgg#17848)
1 parent 6e9c5a8 commit 9afde1c

13 files changed

Lines changed: 567 additions & 16 deletions

File tree

‎apps/mobile/src/features/usage/usageProviders.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const PROVIDER_ORDER: readonly UsageProviderKind[] = [
1212
"cursor",
1313
"opencode",
1414
"antigravity",
15+
"pi",
1516
];
1617

1718
export const PROVIDER_LABEL: Record<UsageProviderKind, string> = {
@@ -21,6 +22,7 @@ export const PROVIDER_LABEL: Record<UsageProviderKind, string> = {
2122
cursor: "Cursor",
2223
opencode: "OpenCode",
2324
antigravity: "Antigravity",
25+
pi: "Pi",
2426
};
2527

2628
/**
@@ -36,6 +38,7 @@ export function useProviderColors(): Record<UsageProviderKind, string> {
3638
cursor: "#8b8b8b",
3739
opencode: "#5b9bbd",
3840
antigravity: "#8c7bd1",
41+
pi: "#4d9f8a",
3942
};
4043
}
4144

‎apps/server/src/provider/builtInDrivers.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export type BuiltInUsageReadersEnv =
7777
| ProviderUsageReaderEnv<typeof GrokDriver>
7878
| ProviderUsageReaderEnv<typeof OpenCodeDriver>
7979
| ProviderUsageReaderEnv<typeof AntigravityDriver>
80-
| ProviderUsageReaderEnv<typeof CursorDriver>;
80+
| ProviderUsageReaderEnv<typeof CursorDriver>
81+
| ProviderUsageReaderEnv<typeof PiDriver>;
8182

8283
/**
8384
* The drivers that keep usage history, in the order the usage page reads
@@ -86,4 +87,12 @@ export type BuiltInUsageReadersEnv =
8687
*/
8788
export const BUILT_IN_USAGE_DRIVERS: ReadonlyArray<
8889
AnyProviderDriver<BuiltInDriversEnv, BuiltInUsageReadersEnv>
89-
> = [ClaudeDriver, CodexDriver, GrokDriver, OpenCodeDriver, AntigravityDriver, CursorDriver];
90+
> = [
91+
ClaudeDriver,
92+
CodexDriver,
93+
GrokDriver,
94+
PiDriver,
95+
OpenCodeDriver,
96+
AntigravityDriver,
97+
CursorDriver,
98+
];

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 314 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
4040
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
4141
import * as AntigravityUsage from "../provider/Drivers/AntigravityUsage.ts";
4242
import * as ProviderHostLive from "../provider/ProviderHostLive.ts";
43-
import type { UsageRecord } from "@t3tools/provider-core/server/usage";
43+
import { totalTokens, type UsageRecord } from "@t3tools/provider-core/server/usage";
4444
import * as UsageService from "./UsageService.ts";
4545

4646
const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
@@ -155,6 +155,7 @@ const layerService = (input: {
155155
Layer.succeed(HostProcess.Environment, {
156156
HOME: input.home,
157157
GROK_HOME: NodePath.join(input.home, "grok"),
158+
PI_CODING_AGENT_DIR: NodePath.join(input.home, "pi"),
158159
OPENCODE_DATA_DIR: NodePath.join(input.home, "opencode"),
159160
ANTIGRAVITY_DATA_DIR: NodePath.join(input.home, "antigravity"),
160161
XDG_CONFIG_HOME: NodePath.join(input.home, "config"),
@@ -269,6 +270,318 @@ function cursorSource(summary: { readonly sources: readonly UsageSource[] }) {
269270
}
270271

271272
describe("UsageService", () => {
273+
it.live(
274+
"reads Pi per-model usage and auxiliary costs once across forks and repeated entries",
275+
() =>
276+
Effect.gen(function* () {
277+
const { home, settings } = yield* setup;
278+
const dir = NodePath.join(home, "pi", "sessions", "project");
279+
const usage = {
280+
input: 10,
281+
output: 5,
282+
cacheRead: 20,
283+
cacheWrite: 3,
284+
reasoning: 2,
285+
totalTokens: 38,
286+
cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: 1 },
287+
};
288+
const parent = {
289+
type: "message",
290+
id: "a123abcd",
291+
parentId: null,
292+
timestamp: "2026-08-01T10:00:00Z",
293+
message: {
294+
role: "assistant",
295+
provider: "anthropic",
296+
model: "requested-model",
297+
responseModel: "actual-model",
298+
usage,
299+
},
300+
};
301+
const auxiliary = [
302+
{
303+
type: "message",
304+
id: "tool",
305+
message: { role: "toolResult", toolName: "classify", usage },
306+
},
307+
{
308+
type: "message",
309+
id: "image",
310+
message: {
311+
role: "toolResult",
312+
toolName: "image",
313+
usage: { ...usage, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
314+
},
315+
},
316+
{ type: "compaction", id: "compact", usage },
317+
{ type: "branch_summary", id: "summary", usage },
318+
].map((entry) => ({ ...entry, timestamp: "2026-08-01T11:00:00Z" }));
319+
yield* Effect.promise(async () => {
320+
await NodeFSP.mkdir(dir, { recursive: true });
321+
await NodeFSP.writeFile(
322+
NodePath.join(dir, "parent.jsonl"),
323+
[
324+
{ type: "session", id: "parent" },
325+
parent,
326+
...auxiliary,
327+
{
328+
type: "usage",
329+
id: "warm",
330+
timestamp: "2026-08-01T12:00:00Z",
331+
kind: "cache_warm",
332+
provider: "openai",
333+
model: "actual-model",
334+
usage: { ...usage, cost: { ...usage.cost, total: 0 } },
335+
},
336+
]
337+
.map((line) => encodeUnknownJsonString(line))
338+
.join("\n") + "\n",
339+
);
340+
await NodeFSP.writeFile(
341+
NodePath.join(dir, "fork.jsonl"),
342+
[
343+
{ type: "session", id: "fork", parentSession: NodePath.join(dir, "parent.jsonl") },
344+
{ ...parent, parentId: "relinked" },
345+
parent,
346+
...auxiliary,
347+
{
348+
...parent,
349+
timestamp: "2026-08-01T13:00:00Z",
350+
message: { ...parent.message, provider: "openai", responseModel: "another-model" },
351+
},
352+
{ type: "get_session_stats", usage: { ...usage, cost: { total: 1000 } } },
353+
]
354+
.map((line) => encodeUnknownJsonString(line))
355+
.join("\n") + "\n",
356+
);
357+
});
358+
yield* Effect.gen(function* () {
359+
const service = yield* UsageService.make;
360+
const first = yield* service.readSummary(WINDOW);
361+
const pi = first.buckets.filter((bucket) => bucket.provider === "pi");
362+
assert.strictEqual(
363+
pi.reduce((total, bucket) => total + bucket.costUsd, 0),
364+
44,
365+
);
366+
assert.strictEqual(
367+
pi.reduce((total, bucket) => total + totalTokens(bucket.totals), 0),
368+
228,
369+
);
370+
assert.deepStrictEqual(pi.map((bucket) => bucket.model).sort(), [
371+
"Tools/summaries",
372+
"anthropic/actual-model",
373+
"openai/actual-model",
374+
"openai/another-model",
375+
]);
376+
const assistant = pi.find((bucket) => bucket.model === "anthropic/actual-model");
377+
assert.deepStrictEqual(assistant?.totals, {
378+
uncachedInputTokens: 10,
379+
outputTokens: 5,
380+
cachedInputTokens: 20,
381+
cacheCreationTokens: 3,
382+
reasoningTokens: 2,
383+
});
384+
assert.strictEqual(assistant?.costSource, "providerReported");
385+
assert.approximately(assistant?.categoryCostUsd?.input ?? NaN, 10 / 38, 1e-6);
386+
assert.approximately(assistant?.categoryCostUsd?.cacheRead ?? NaN, 2 / 38, 1e-6);
387+
assert.approximately(assistant?.categoryCostUsd?.cacheWrite ?? NaN, 6 / 38, 1e-6);
388+
assert.approximately(assistant?.categoryCostUsd?.output ?? NaN, 20 / 38, 1e-6);
389+
assert.strictEqual(assistant?.cacheSavingsUsd, 18);
390+
const warm = pi.find((bucket) => bucket.model === "openai/actual-model");
391+
assert.strictEqual(warm?.costSource, "modelPriced");
392+
assert.strictEqual(warm?.costUsd, 38);
393+
assert.deepStrictEqual(warm?.categoryCostUsd, {
394+
input: 10,
395+
cacheRead: 2,
396+
cacheWrite: 6,
397+
output: 20,
398+
});
399+
assert.strictEqual(warm?.cacheSavingsUsd, 18);
400+
assert.strictEqual(
401+
first.sources.filter((source) => source.fingerprint.provider === "pi").length,
402+
1,
403+
);
404+
// Warm scans retain header state and only charge appended entries.
405+
yield* Effect.promise(() =>
406+
NodeFSP.appendFile(
407+
NodePath.join(dir, "fork.jsonl"),
408+
encodeUnknownJsonString({
409+
...parent,
410+
id: "new",
411+
timestamp: "2026-08-01T14:00:00Z",
412+
message: { ...parent.message, responseModel: "latest-model" },
413+
}) + "\n",
414+
),
415+
);
416+
const grown = yield* service.readSummary(WINDOW);
417+
assert.strictEqual(
418+
grown.buckets
419+
.filter((bucket) => bucket.provider === "pi")
420+
.reduce((total, bucket) => total + bucket.costUsd, 0),
421+
45,
422+
);
423+
yield* service.awaitPersisted;
424+
const restarted = yield* UsageService.make;
425+
assert.deepStrictEqual((yield* restarted.readSummary(WINDOW)).buckets, grown.buckets);
426+
}).pipe(
427+
Effect.scoped,
428+
Effect.provide(
429+
layerService({
430+
prefix: "usage-pi-fork",
431+
home,
432+
ratesDocument: {
433+
"actual-model": {
434+
input_cost_per_token: 1,
435+
output_cost_per_token: 4,
436+
cache_read_input_token_cost: 0.1,
437+
cache_creation_input_token_cost: 2,
438+
},
439+
},
440+
settings: {
441+
...settings,
442+
providerInstances: {
443+
...settings.providerInstances,
444+
[ProviderInstanceId.make("pi-extra")]: {
445+
driver: ProviderDriverKind.make("pi"),
446+
enabled: false,
447+
environment: [
448+
{
449+
name: "PI_CODING_AGENT_DIR",
450+
value: NodePath.join(home, "pi"),
451+
sensitive: false,
452+
},
453+
],
454+
},
455+
},
456+
},
457+
}),
458+
),
459+
);
460+
}).pipe(Effect.scoped),
461+
);
462+
463+
it.live("reports missing Pi history and skips malformed records without losing valid usage", () =>
464+
Effect.gen(function* () {
465+
const { home, settings } = yield* setup;
466+
yield* Effect.gen(function* () {
467+
const service = yield* UsageService.make;
468+
const missing = yield* service.readSummary(WINDOW);
469+
assert.strictEqual(
470+
missing.sources.find((source) => source.fingerprint.provider === "pi")?.status,
471+
"missing",
472+
);
473+
const dir = NodePath.join(home, "pi", "sessions");
474+
yield* Effect.promise(async () => {
475+
await NodeFSP.mkdir(dir, { recursive: true });
476+
await NodeFSP.writeFile(
477+
NodePath.join(dir, "session.jsonl"),
478+
[
479+
"{broken",
480+
"null",
481+
encodeUnknownJsonString({ type: "session", id: "valid" }),
482+
encodeUnknownJsonString({
483+
type: "message",
484+
id: "bad",
485+
timestamp: "invalid",
486+
message: {
487+
role: "assistant",
488+
provider: "openai",
489+
model: "model",
490+
usage: { input: 100 },
491+
},
492+
}),
493+
encodeUnknownJsonString({
494+
type: "message",
495+
id: "valid",
496+
timestamp: "2026-08-01T10:00:00Z",
497+
message: {
498+
role: "assistant",
499+
provider: "openai",
500+
model: "model",
501+
usage: {
502+
input: -1,
503+
cacheRead: "100",
504+
output: 7,
505+
reasoning: 50,
506+
cost: { total: -1 },
507+
},
508+
},
509+
}),
510+
].join("\n") + "\n",
511+
);
512+
});
513+
const summary = yield* service.readSummary(WINDOW);
514+
const pi = summary.buckets.filter((bucket) => bucket.provider === "pi");
515+
assert.strictEqual(pi.length, 1);
516+
assert.strictEqual(pi[0] && totalTokens(pi[0].totals), 7);
517+
assert.strictEqual(pi[0]?.totals.reasoningTokens, 7);
518+
assert.strictEqual(pi[0]?.costSource, "unpriced");
519+
}).pipe(
520+
Effect.scoped,
521+
Effect.provide(layerService({ prefix: "usage-pi-malformed", home, settings })),
522+
);
523+
}).pipe(Effect.scoped),
524+
);
525+
526+
it.live("discovers Pi history in its default home and directory overrides", () =>
527+
Effect.gen(function* () {
528+
const { home, settings } = yield* setup;
529+
for (const [agentDir, sessionDir, relativeDir] of [
530+
[undefined, undefined, ".pi/agent/sessions"],
531+
["~/custom-pi", undefined, "custom-pi/sessions"],
532+
["~/ignored-pi", "~/custom-sessions", "custom-sessions"],
533+
["~/ignored-pi", NodePath.join(home, "absolute-sessions"), "absolute-sessions"],
534+
] as const) {
535+
const dir = NodePath.join(home, relativeDir);
536+
yield* Effect.promise(async () => {
537+
await NodeFSP.mkdir(dir, { recursive: true });
538+
await NodeFSP.writeFile(
539+
NodePath.join(dir, "session.jsonl"),
540+
[
541+
{ type: "session", id: relativeDir },
542+
{
543+
type: "usage",
544+
id: "warm",
545+
timestamp: "2026-08-01T10:00:00Z",
546+
provider: "openai",
547+
model: "model",
548+
usage: { input: 7 },
549+
},
550+
]
551+
.map((line) => encodeUnknownJsonString(line))
552+
.join("\n") + "\n",
553+
);
554+
});
555+
yield* Effect.gen(function* () {
556+
const service = yield* UsageService.make;
557+
const summary = yield* service.readSummary(WINDOW);
558+
const pi = summary.buckets.filter((bucket) => bucket.provider === "pi");
559+
assert.strictEqual(pi.length, 1);
560+
assert.strictEqual(pi[0]?.totals.uncachedInputTokens, 7);
561+
assert.strictEqual(
562+
summary.sources.find((source) => source.fingerprint.provider === "pi")?.fingerprint
563+
.resolvedHomePath,
564+
dir,
565+
);
566+
}).pipe(
567+
Effect.scoped,
568+
Effect.provide(
569+
layerService({
570+
prefix: "usage-pi-" + relativeDir.replaceAll("/", "-"),
571+
home,
572+
settings,
573+
environment: {
574+
PI_CODING_AGENT_DIR: agentDir,
575+
PI_CODING_AGENT_SESSION_DIR: sessionDir,
576+
},
577+
}),
578+
),
579+
Effect.provideService(HostProcess.HomeDirectory, home),
580+
);
581+
}
582+
}).pipe(Effect.scoped),
583+
);
584+
272585
it.live("reads shared managed default and disabled extra account history once", () =>
273586
Effect.gen(function* () {
274587
const { home, settings } = yield* setup;

0 commit comments

Comments
 (0)