Skip to content

Commit 8217ed4

Browse files
feat(usage): show cost and tokens by project
1 parent 61c3794 commit 8217ed4

11 files changed

Lines changed: 669 additions & 15 deletions

File tree

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ import * as CursorKeychain from "@t3tools/provider-cursor/server/CursorKeychain"
191191
import * as CursorUsageAccounts from "@t3tools/provider-cursor/server/CursorUsageAccounts";
192192
import * as UsageService from "./usage/UsageService.ts";
193193
import * as CodexUsageHistory from "./usage/CodexUsageHistory.ts";
194+
import * as UsageProjectAttribution from "./usage/UsageProjectAttribution.ts";
194195
import * as RuntimeLayer from "./orchestration-v2/runtimeLayer.ts";
195196
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
196197
import * as ThreadSearch from "./orchestration-v2/ThreadSearch.ts";
@@ -290,6 +291,7 @@ const layerUsage = UsageService.layer.pipe(
290291
AntigravityUsage.layer,
291292
MuseUsageHome.layer,
292293
CodexUsageHistory.layer,
294+
UsageProjectAttribution.layer,
293295
CursorUsageAccounts.layer.pipe(
294296
Layer.provide(CursorAccountReader.layer.pipe(Layer.provide(CursorKeychain.layer))),
295297
),
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { ProjectId, type UsageProject } from "@t3tools/contracts";
2+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3+
import * as Context from "effect/Context";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import * as Schema from "effect/Schema";
7+
import * as SqlClient from "effect/sql/SqlClient";
8+
import * as SqlSchema from "effect/sql/SqlSchema";
9+
10+
export class UsageProjectAttributionReadError extends Schema.TaggedError<UsageProjectAttributionReadError>()(
11+
"UsageProjectAttributionReadError",
12+
{ cause: Schema.Defect() },
13+
) {
14+
override get message(): string {
15+
return "Could not read provider session projects for usage.";
16+
}
17+
}
18+
19+
const SessionProjectRow = Schema.Struct({
20+
driver: Schema.String,
21+
nativeId: Schema.String,
22+
projectId: ProjectId,
23+
title: Schema.String,
24+
workspaceRoot: Schema.String,
25+
});
26+
27+
export const sessionKey = (driver: string, nativeId: string) => `${driver}\u0000${nativeId}`;
28+
29+
export class UsageProjectAttribution extends Context.Service<
30+
UsageProjectAttribution,
31+
{
32+
/** Keyed by `sessionKey`; deleted projects keep their past usage. */
33+
readonly read: Effect.Effect<
34+
ReadonlyMap<string, UsageProject>,
35+
UsageProjectAttributionReadError
36+
>;
37+
}
38+
>()("t3/usage/UsageProjectAttribution") {}
39+
40+
const make = Effect.gen(function* () {
41+
const sql = yield* SqlClient.SqlClient;
42+
43+
// Worktree threads keep their project's id, so their sessions land on the project.
44+
const selectSessionProjects = SqlSchema.findAll({
45+
Request: Schema.Void,
46+
Result: SessionProjectRow,
47+
execute: () => sql`
48+
SELECT
49+
provider_thread.driver AS "driver",
50+
json_extract(provider_thread.payload_json, '$.nativeThreadRef.nativeId') AS "nativeId",
51+
project.project_id AS "projectId",
52+
project.title AS "title",
53+
project.workspace_root AS "workspaceRoot"
54+
FROM orchestration_v2_projection_provider_threads AS provider_thread
55+
JOIN orchestration_v2_projection_threads AS thread
56+
ON thread.thread_id = provider_thread.thread_id
57+
JOIN projection_projects AS project
58+
ON project.project_id = thread.project_id
59+
WHERE provider_thread.driver IS NOT NULL
60+
AND json_extract(provider_thread.payload_json, '$.nativeThreadRef.nativeId') IS NOT NULL
61+
`,
62+
});
63+
64+
const read = selectSessionProjects(undefined).pipe(
65+
NodeSqliteClient.readOnly,
66+
Effect.mapError((cause) => new UsageProjectAttributionReadError({ cause })),
67+
Effect.map((rows) => {
68+
const projects = new Map<string, UsageProject | null>();
69+
for (const row of rows) {
70+
const key = sessionKey(row.driver, row.nativeId);
71+
const existing = projects.get(key);
72+
// A session claimed by threads in different projects belongs to neither.
73+
if (existing !== undefined) {
74+
if (existing?.projectId !== row.projectId) projects.set(key, null);
75+
continue;
76+
}
77+
const title =
78+
row.title.trim() ||
79+
row.workspaceRoot.split(/[\\/]/).findLast((part) => part.length > 0) ||
80+
"Untitled project";
81+
projects.set(key, { projectId: row.projectId, title });
82+
}
83+
const resolved = new Map<string, UsageProject>();
84+
for (const [key, project] of projects) if (project !== null) resolved.set(key, project);
85+
return resolved;
86+
}),
87+
Effect.withSpan("UsageProjectAttribution.read"),
88+
);
89+
90+
return UsageProjectAttribution.of({ read });
91+
});
92+
93+
export const layer = Layer.effect(UsageProjectAttribution, make);

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

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1515
import { mergeUsage } from "@t3tools/shared/usageMerge";
1616
import {
1717
EnvironmentId,
18+
ProjectId,
1819
ProviderDriverKind,
1920
ProviderInstanceId,
2021
UsageDay,
@@ -42,6 +43,7 @@ import * as ServerConfig from "../config.ts";
4243
import * as ServerSettings from "../serverSettings.ts";
4344
import * as Sqlite from "../persistence/Sqlite.ts";
4445
import * as CodexUsageHistory from "./CodexUsageHistory.ts";
46+
import * as UsageProjectAttribution from "./UsageProjectAttribution.ts";
4547
import * as CursorAccountReader from "@t3tools/provider-cursor/server/CursorAccountReader";
4648
import * as CursorKeychain from "@t3tools/provider-cursor/server/CursorKeychain";
4749
import * as CursorUsageAccounts from "@t3tools/provider-cursor/server/CursorUsageAccounts";
@@ -159,7 +161,11 @@ const layerService = (input: {
159161
Layer.provideMerge(AntigravityUsage.layer),
160162
Layer.provideMerge(MuseUsageHome.layer),
161163
Layer.provideMerge(ProviderHostLive.layer),
162-
Layer.provideMerge(CodexUsageHistory.layer.pipe(Layer.provideMerge(Sqlite.layerMemory))),
164+
Layer.provideMerge(
165+
Layer.merge(CodexUsageHistory.layer, UsageProjectAttribution.layer).pipe(
166+
Layer.provideMerge(Sqlite.layerMemory),
167+
),
168+
),
163169
Layer.provideMerge(Layer.mock(BackgroundPolicy.BackgroundPolicy)({})),
164170
Layer.provideMerge(
165171
Layer.mock(ServerSecretStore.ServerSecretStore)({
@@ -2402,6 +2408,109 @@ describe("UsageService", () => {
24022408
}).pipe(Effect.scoped),
24032409
);
24042410

2411+
it.effect("totals usage per T3 project from provider thread bindings", () =>
2412+
Effect.gen(function* () {
2413+
const { transcript, settings, home } = yield* setup;
2414+
const piTranscript = NodePath.join(home, "pi", "sessions", "bound.jsonl");
2415+
yield* Effect.promise(async () => {
2416+
await NodeFSP.mkdir(NodePath.dirname(piTranscript), { recursive: true });
2417+
await NodeFSP.writeFile(
2418+
piTranscript,
2419+
[
2420+
{ type: "session", id: "pi-header-id" },
2421+
{
2422+
type: "usage",
2423+
id: "pi-event",
2424+
timestamp: "2026-08-01T10:00:00Z",
2425+
provider: "openai",
2426+
model: "model",
2427+
usage: { output: 17 },
2428+
},
2429+
]
2430+
.map((line) => encodeUnknownJsonString(line))
2431+
.join("\n") + "\n",
2432+
);
2433+
});
2434+
yield* Effect.promise(() =>
2435+
NodeFSP.writeFile(
2436+
transcript,
2437+
claudeLine(1, 3, "claude-fable-5", "bound") +
2438+
claudeLine(2, 5, "claude-fable-5", "worktree") +
2439+
claudeLine(3, 7, "claude-fable-5", "ambiguous") +
2440+
claudeLine(4, 11, "claude-fable-5", "terminal") +
2441+
claudeLine(5, 13, "claude-fable-5", "deleted-project"),
2442+
),
2443+
);
2444+
const projectRows = (summary: UsageSummary) =>
2445+
(summary.projectTotals ?? [])
2446+
.filter((row) => row.provider !== "pi")
2447+
.map((row) => [String(row.projectId ?? null), row.totals.outputTokens, row.sessions])
2448+
.toSorted(([a], [b]) => String(a).localeCompare(String(b)));
2449+
2450+
yield* Effect.gen(function* () {
2451+
const sql = yield* SqlClient.SqlClient;
2452+
const project = (id: string, title: string, deletedAt: string | null = null) =>
2453+
sql`INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at, deleted_at)
2454+
VALUES (${id}, ${title}, ${`/work/${id}`}, '[]', '2026-08-01', '2026-08-01', ${deletedAt})`;
2455+
const thread = (id: string, projectId: string) =>
2456+
sql`INSERT INTO orchestration_v2_projection_threads (thread_id, project_id, title, default_provider, runtime_mode, interaction_mode, created_at, updated_at, payload_json)
2457+
VALUES (${id}, ${projectId}, ${id}, 'claudeAgent', 'full-access', 'default', '2026-08-01', '2026-08-01', '{}')`;
2458+
const binding = (id: string, threadId: string, nativeId: string, driver = "claudeAgent") =>
2459+
sql`INSERT INTO orchestration_v2_projection_provider_threads (provider_thread_id, thread_id, provider, driver, status, updated_at, payload_json)
2460+
VALUES (${id}, ${threadId}, ${driver}, ${driver}, 'idle', '2026-08-01', ${encodeUnknownJsonString(
2461+
{ nativeThreadRef: { driver, nativeId } },
2462+
)})`;
2463+
yield* project("p-app", "App");
2464+
yield* project("p-other", "Other");
2465+
yield* project("p-gone", "Gone", "2026-08-02");
2466+
yield* thread("t-main", "p-app");
2467+
yield* thread("t-worktree", "p-app");
2468+
yield* thread("t-other", "p-other");
2469+
yield* thread("t-gone", "p-gone");
2470+
yield* binding("pt-1", "t-main", "bound");
2471+
yield* binding("pt-2", "t-worktree", "worktree");
2472+
yield* binding("pt-3", "t-main", "ambiguous");
2473+
yield* binding("pt-4", "t-other", "ambiguous");
2474+
yield* binding("pt-5", "t-gone", "deleted-project");
2475+
// Another driver's identical native id must not claim a Claude session.
2476+
yield* binding("pt-6", "t-other", "terminal", "codex");
2477+
yield* binding("pt-7", "t-main", piTranscript, "pi");
2478+
yield* binding("pt-8", "t-other", "pi-header-id", "pi");
2479+
2480+
const service = yield* UsageService.make;
2481+
const summary = yield* service.readSummary(WINDOW);
2482+
assert.deepStrictEqual(projectRows(summary), [
2483+
["null", 18, 2],
2484+
["p-app", 8, 2],
2485+
["p-gone", 13, 1],
2486+
]);
2487+
assert.deepStrictEqual(
2488+
summary.projectTotals
2489+
?.filter((row) => row.provider === "pi")
2490+
.map((row) => [row.projectId, row.totals.outputTokens, row.sessions]),
2491+
[[ProjectId.make("p-app"), 17, 1]],
2492+
);
2493+
assert.deepStrictEqual(
2494+
summary.projects?.toSorted((a, b) => a.projectId.localeCompare(b.projectId)),
2495+
[
2496+
{ projectId: ProjectId.make("p-app"), title: "App" },
2497+
{ projectId: ProjectId.make("p-gone"), title: "Gone" },
2498+
],
2499+
);
2500+
assert.strictEqual(totalOutputTokens(summary), 56);
2501+
2502+
yield* sql`DROP TABLE orchestration_v2_projection_provider_threads`;
2503+
const unattributed = yield* service.readSummary({ ...WINDOW, awaitRefresh: true });
2504+
assert.strictEqual(unattributed.projectTotals, undefined);
2505+
assert.strictEqual(unattributed.projects, undefined);
2506+
assert.strictEqual(totalOutputTokens(unattributed), 56);
2507+
}).pipe(
2508+
Effect.scoped,
2509+
Effect.provide(layerService({ prefix: "usage-service-project-test", home, settings })),
2510+
);
2511+
}).pipe(Effect.scoped),
2512+
);
2513+
24052514
it.live("reprices unchanged transcripts when custom prices are added, edited, or removed", () =>
24062515
Effect.gen(function* () {
24072516
const { transcript, settings, home } = yield* setup;

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
2+
type ProjectId,
23
type ProviderInstanceConfig,
34
ProviderInstanceId,
45
USAGE_CONTRACT_VERSION,
56
type ServerSettings as ServerSettingsValue,
67
type UsageProviderKind,
8+
type UsageProject,
79
type UsageSource,
810
type UsagePricing,
911
type UsageSummary,
@@ -56,6 +58,7 @@ import {
5658
type CachedFile,
5759
type ScanCache,
5860
} from "./usageScanCache.ts";
61+
import * as UsageProjectAttribution from "./UsageProjectAttribution.ts";
5962

6063
const LITELLM_RATES_URL =
6164
"https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
@@ -82,6 +85,13 @@ const scanReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
8285
driver.usage?.kind === "scan" ? [{ driver, reader: driver.usage }] : [],
8386
);
8487

88+
// Provider threads key native sessions by driver kind (claudeAgent), not usage provider (claude).
89+
const driverKindByProvider = new Map(
90+
BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
91+
driver.usage === undefined ? [] : [[driver.usage.provider, driver.driverKind] as const],
92+
),
93+
);
94+
8595
const transcriptFormats = new Map(
8696
transcriptReaders.map(({ reader }) => [reader.provider, reader.format] as const),
8797
);
@@ -156,6 +166,7 @@ export const make = Effect.gen(function* () {
156166
const hostEnvironment = yield* HostProcess.Environment;
157167
const hostname = yield* HostProcess.Hostname;
158168
const readerContext = yield* Effect.context<BuiltInUsageReadersEnv>();
169+
const projectAttribution = yield* UsageProjectAttribution.UsageProjectAttribution;
159170

160171
const fileCache: ScanCache = new Map();
161172
// Reads from concurrent windows can still hold the original record after it is corrected.
@@ -707,13 +718,18 @@ export const make = Effect.gen(function* () {
707718

708719
const retentionCutoffMs = startedAtMs - CACHE_RETENTION_DAYS * 24 * 60 * 60 * 1000;
709720

710-
const [, scannedDirs] = yield* Effect.all(
721+
const [, scannedDirs, sessionProjects] = yield* Effect.all(
711722
[
712723
ensureRates(false),
713724
collectDirs(windowStartMs, settings, retentionCutoffMs, input.awaitRefresh === true),
725+
// Without projects the summary omits its breakdown; totals never depend on it.
726+
projectAttribution.read.pipe(
727+
Effect.catch((error) => Effect.logWarning(error.message).pipe(Effect.as(null))),
728+
),
714729
],
715-
{ concurrency: 2 },
730+
{ concurrency: 3 },
716731
);
732+
const referencedProjects = new Map<ProjectId, UsageProject>();
717733

718734
const aggregator = new UsageAggregator({
719735
timeZone: input.timeZone,
@@ -725,6 +741,22 @@ export const make = Effect.gen(function* () {
725741
priceOverrides: createOverrideRateTable(settings.usagePriceOverrides),
726742
modelAliases: resolveModelAliases(settings.usageModelAliases),
727743
...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }),
744+
...(sessionProjects === null
745+
? {}
746+
: {
747+
attributeProject: (record: UsageRecord, transcriptPath?: string) => {
748+
const driverKind = driverKindByProvider.get(record.provider);
749+
// Pi binds threads to the session file, rather than the header's display id.
750+
const nativeId = record.provider === "pi" ? transcriptPath : record.sessionId;
751+
const project =
752+
driverKind === undefined || nativeId === undefined || nativeId.length === 0
753+
? undefined
754+
: sessionProjects.get(UsageProjectAttribution.sessionKey(driverKind, nativeId));
755+
if (project === undefined) return null;
756+
referencedProjects.set(project.projectId, project);
757+
return project.projectId;
758+
},
759+
}),
728760
});
729761

730762
const sources: UsageSource[] = [];
@@ -833,7 +865,7 @@ export const make = Effect.gen(function* () {
833865
eventOccurrences.set(key, occurrence);
834866
usageRecord = { ...record, dedupeKey: key + ":" + occurrence };
835867
}
836-
if (aggregator.add(usageRecord, dir) && record.sessionId.length > 0) {
868+
if (aggregator.add(usageRecord, dir, file.path) && record.sessionId.length > 0) {
837869
sessionIds.add(record.sessionId);
838870
}
839871
}
@@ -884,6 +916,9 @@ export const make = Effect.gen(function* () {
884916
pricing: pricing(),
885917
scanDurationMs: Math.max(0, finishedAtMs - startedAtMs),
886918
...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }),
919+
...(aggregated.projectTotals === undefined
920+
? {}
921+
: { projectTotals: aggregated.projectTotals, projects: [...referencedProjects.values()] }),
887922
} satisfies UsageSummary;
888923
});
889924

0 commit comments

Comments
 (0)