Skip to content

Commit 893cdf9

Browse files
committed
fix(server): count every provider instance on the Usage page
The usage scan only read the default Claude and Codex homes from the legacy `providers.<kind>` settings, so transcripts written by additional provider instances (a second Claude install with its own CLAUDE_CONFIG_DIR, a second Codex home) never showed up. Resolve transcript directories from the same merged instance map the provider registry hydrates from, one entry per distinct home, deduped so instances that share a home are not counted twice. Made with Claude Fable 5 in Claude Code.
1 parent 82b8a93 commit 893cdf9

4 files changed

Lines changed: 238 additions & 29 deletions

File tree

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import { HttpClient, HttpClientResponse } from "effect/unstable/http";
3535

3636
import { ServerConfig } from "../config.ts";
3737
import * as ServerSettings from "../serverSettings.ts";
38-
import { resolveClaudeHomePath } from "../provider/Drivers/ClaudeHome.ts";
39-
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
4038
import { UsageAggregator } from "./usageAggregation.ts";
4139
import { parseRateTable, type RateTable } from "./usagePricing.ts";
4240
import {
@@ -51,6 +49,7 @@ import {
5149
pruneScanCache,
5250
type ScanCache,
5351
} from "./usageScanCache.ts";
52+
import { resolveUsageTranscriptDirs } from "./usageTranscriptDirs.ts";
5453
import type { UsageRecord } from "./usageTranscripts.ts";
5554

5655
const LITELLM_RATES_URL =
@@ -184,20 +183,7 @@ export const make = Effect.gen(function* () {
184183
);
185184
});
186185

187-
/**
188-
* Claude's config dir is the home itself when overridden, but a default
189-
* install nests transcripts under `~/.claude/projects`. Probe both.
190-
*/
191-
const resolveClaudeTranscriptDir = (homePath: string) =>
192-
Effect.gen(function* () {
193-
const nested = path.join(homePath, ".claude", "projects");
194-
const nestedExists = yield* fileSystem
195-
.exists(nested)
196-
.pipe(Effect.catchCause(() => Effect.succeed(false)));
197-
return nestedExists ? nested : path.join(homePath, "projects");
198-
});
199-
200-
/** Resolves the transcript directory for each provider. */
186+
/** Resolves the transcript directory for every configured provider instance. */
201187
const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* () {
202188
// A settings failure must surface as an error: swallowing it here would
203189
// present "zero usage from every provider" as a valid answer.
@@ -215,14 +201,7 @@ export const make = Effect.gen(function* () {
215201
),
216202
);
217203

218-
const claudeHome = yield* resolveClaudeHomePath(settings.providers.claudeAgent);
219-
const claudeDir = yield* resolveClaudeTranscriptDir(claudeHome);
220-
const codexLayout = yield* resolveCodexHomeLayout(settings.providers.codex);
221-
222-
return [
223-
{ provider: "claude" as const, dir: claudeDir },
224-
{ provider: "codex" as const, dir: path.join(codexLayout.sharedHomePath, "sessions") },
225-
];
204+
return yield* resolveUsageTranscriptDirs(settings);
226205
});
227206

228207
/**
@@ -327,9 +306,12 @@ export const make = Effect.gen(function* () {
327306
yield* ensureScanCacheLoaded;
328307

329308
const hostId = NodeOS.hostname();
330-
// The home resolvers ask for `Path` themselves; satisfy them from the
331-
// instance we already hold so `readSummary` stays context-free.
332-
const dirs = yield* resolveTranscriptDirs().pipe(Effect.provideService(Path.Path, path));
309+
// The home resolvers ask for `Path` and `FileSystem` themselves; satisfy
310+
// them from the instances we already hold so `readSummary` stays context-free.
311+
const dirs = yield* resolveTranscriptDirs().pipe(
312+
Effect.provideService(Path.Path, path),
313+
Effect.provideService(FileSystem.FileSystem, fileSystem),
314+
);
333315
const windowStart = DateTime.make(`${input.sinceDay}T00:00:00Z`);
334316
if (Option.isNone(windowStart)) {
335317
return yield* new UsageReadError({
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { describe, expect, it } from "@effect/vitest";
3+
import {
4+
DEFAULT_SERVER_SETTINGS,
5+
ProviderDriverKind,
6+
type ProviderInstanceConfig,
7+
ProviderInstanceId,
8+
type ServerSettings,
9+
} from "@t3tools/contracts";
10+
import * as Effect from "effect/Effect";
11+
import * as FileSystem from "effect/FileSystem";
12+
import * as Path from "effect/Path";
13+
14+
import { resolveUsageTranscriptDirs } from "./usageTranscriptDirs.ts";
15+
16+
const CLAUDE = ProviderDriverKind.make("claudeAgent");
17+
const CODEX = ProviderDriverKind.make("codex");
18+
19+
/**
20+
* Legacy homes are pinned to paths that do not exist so the Claude nested
21+
* `.claude/projects` probe resolves the same way on every machine.
22+
*/
23+
const makeSettings = (
24+
root: string,
25+
providerInstances: Record<string, ProviderInstanceConfig> = {},
26+
): ServerSettings => ({
27+
...DEFAULT_SERVER_SETTINGS,
28+
providers: {
29+
...DEFAULT_SERVER_SETTINGS.providers,
30+
claudeAgent: { ...DEFAULT_SERVER_SETTINGS.providers.claudeAgent, homePath: `${root}/claude` },
31+
codex: { ...DEFAULT_SERVER_SETTINGS.providers.codex, homePath: `${root}/codex` },
32+
},
33+
providerInstances: providerInstances as ServerSettings["providerInstances"],
34+
});
35+
36+
it.layer(NodeServices.layer)("usageTranscriptDirs", (it) => {
37+
describe("resolveUsageTranscriptDirs", () => {
38+
it.effect("scans the legacy Claude and Codex homes when no instances are configured", () =>
39+
Effect.gen(function* () {
40+
const path = yield* Path.Path;
41+
const fileSystem = yield* FileSystem.FileSystem;
42+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
43+
44+
const dirs = yield* resolveUsageTranscriptDirs(makeSettings(root));
45+
46+
expect(dirs).toEqual([
47+
{ provider: "codex", dir: path.resolve(root, "codex", "sessions") },
48+
{ provider: "claude", dir: path.resolve(root, "claude", "projects") },
49+
]);
50+
}),
51+
);
52+
53+
it.effect("adds a transcript directory for every extra provider instance", () =>
54+
Effect.gen(function* () {
55+
const path = yield* Path.Path;
56+
const fileSystem = yield* FileSystem.FileSystem;
57+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
58+
59+
const dirs = yield* resolveUsageTranscriptDirs(
60+
makeSettings(root, {
61+
[ProviderInstanceId.make("claude_work")]: {
62+
driver: CLAUDE,
63+
config: { homePath: `${root}/claude-work` },
64+
},
65+
[ProviderInstanceId.make("codex_personal")]: {
66+
driver: CODEX,
67+
config: { homePath: `${root}/codex-personal` },
68+
},
69+
}),
70+
);
71+
72+
expect(dirs).toEqual([
73+
{ provider: "claude", dir: path.resolve(root, "claude-work", "projects") },
74+
{ provider: "codex", dir: path.resolve(root, "codex-personal", "sessions") },
75+
{ provider: "codex", dir: path.resolve(root, "codex", "sessions") },
76+
{ provider: "claude", dir: path.resolve(root, "claude", "projects") },
77+
]);
78+
}),
79+
);
80+
81+
it.effect("prefers a nested .claude/projects directory when the home has one", () =>
82+
Effect.gen(function* () {
83+
const path = yield* Path.Path;
84+
const fileSystem = yield* FileSystem.FileSystem;
85+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
86+
const nested = path.join(root, "claude", ".claude", "projects");
87+
yield* fileSystem.makeDirectory(nested, { recursive: true });
88+
89+
const dirs = yield* resolveUsageTranscriptDirs(makeSettings(root));
90+
91+
expect(dirs.find((entry) => entry.provider === "claude")?.dir).toBe(path.resolve(nested));
92+
}),
93+
);
94+
95+
it.effect("collapses instances that share a home so nothing is counted twice", () =>
96+
Effect.gen(function* () {
97+
const fileSystem = yield* FileSystem.FileSystem;
98+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
99+
100+
const dirs = yield* resolveUsageTranscriptDirs(
101+
makeSettings(root, {
102+
[ProviderInstanceId.make("claude_chrome")]: {
103+
driver: CLAUDE,
104+
config: { homePath: `${root}/claude`, launchArgs: "--chrome" },
105+
},
106+
}),
107+
);
108+
109+
expect(dirs.filter((entry) => entry.provider === "claude")).toHaveLength(1);
110+
}),
111+
);
112+
113+
it.effect("skips drivers without transcripts and configs that fail to decode", () =>
114+
Effect.gen(function* () {
115+
const fileSystem = yield* FileSystem.FileSystem;
116+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-usage-dirs-" });
117+
118+
const dirs = yield* resolveUsageTranscriptDirs(
119+
makeSettings(root, {
120+
[ProviderInstanceId.make("cursor_main")]: {
121+
driver: ProviderDriverKind.make("cursor"),
122+
config: {},
123+
},
124+
[ProviderInstanceId.make("claude_broken")]: {
125+
driver: CLAUDE,
126+
config: { homePath: 42 },
127+
},
128+
}),
129+
);
130+
131+
expect(dirs).toHaveLength(2);
132+
}),
133+
);
134+
});
135+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Resolves the transcript directories the usage scan walks.
3+
*
4+
* Every configured provider instance contributes its own directory. A second
5+
* Claude instance pointed at `~/.claude-work` writes transcripts there, not
6+
* under `~/.claude`, so scanning only the default instance under-reports.
7+
* Instances that resolve to the same directory collapse to one entry so a
8+
* shared home is never counted twice.
9+
*
10+
* @module usageTranscriptDirs
11+
*/
12+
import {
13+
ClaudeSettings,
14+
CodexSettings,
15+
ProviderDriverKind,
16+
type ServerSettings,
17+
type UsageProviderKind,
18+
} from "@t3tools/contracts";
19+
import * as Effect from "effect/Effect";
20+
import * as FileSystem from "effect/FileSystem";
21+
import * as Option from "effect/Option";
22+
import * as Path from "effect/Path";
23+
import * as Schema from "effect/Schema";
24+
25+
import { resolveClaudeHomePath } from "../provider/Drivers/ClaudeHome.ts";
26+
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
27+
import { deriveProviderInstanceConfigMap } from "../provider/Layers/ProviderInstanceRegistryHydration.ts";
28+
29+
export interface UsageTranscriptDir {
30+
readonly provider: UsageProviderKind;
31+
readonly dir: string;
32+
}
33+
34+
const CLAUDE_DRIVER_KIND = ProviderDriverKind.make("claudeAgent");
35+
const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");
36+
37+
const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);
38+
const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);
39+
40+
/**
41+
* Claude's config dir is the home itself when overridden, but a default
42+
* install nests transcripts under `~/.claude/projects`. Probe both.
43+
*/
44+
const resolveClaudeTranscriptDir = Effect.fn("resolveClaudeTranscriptDir")(function* (
45+
homePath: string,
46+
): Effect.fn.Return<string, never, Path.Path | FileSystem.FileSystem> {
47+
const path = yield* Path.Path;
48+
const fileSystem = yield* FileSystem.FileSystem;
49+
const nested = path.join(homePath, ".claude", "projects");
50+
const nestedExists = yield* fileSystem
51+
.exists(nested)
52+
.pipe(Effect.catchCause(() => Effect.succeed(false)));
53+
return nestedExists ? nested : path.join(homePath, "projects");
54+
});
55+
56+
/**
57+
* One transcript directory per distinct provider home. Walks the same merged
58+
* instance map the registry hydrates from, so explicit `providerInstances`
59+
* entries and the legacy `providers.<kind>` mirrors are both covered.
60+
* Instances whose config fails to decode are skipped; the registry already
61+
* reports those as unavailable.
62+
*/
63+
export const resolveUsageTranscriptDirs = Effect.fn("resolveUsageTranscriptDirs")(function* (
64+
settings: ServerSettings,
65+
): Effect.fn.Return<ReadonlyArray<UsageTranscriptDir>, never, Path.Path | FileSystem.FileSystem> {
66+
const path = yield* Path.Path;
67+
const dirs: UsageTranscriptDir[] = [];
68+
const seen = new Set<string>();
69+
const add = (provider: UsageProviderKind, dir: string) => {
70+
const key = `${provider}\0${dir}`;
71+
if (seen.has(key)) return;
72+
seen.add(key);
73+
dirs.push({ provider, dir });
74+
};
75+
76+
for (const instance of Object.values(deriveProviderInstanceConfigMap(settings))) {
77+
if (instance.driver === CLAUDE_DRIVER_KIND) {
78+
const config = decodeClaudeSettings(instance.config ?? {});
79+
if (Option.isNone(config)) continue;
80+
const homePath = yield* resolveClaudeHomePath(config.value);
81+
add("claude", yield* resolveClaudeTranscriptDir(homePath));
82+
} else if (instance.driver === CODEX_DRIVER_KIND) {
83+
const config = decodeCodexSettings(instance.config ?? {});
84+
if (Option.isNone(config)) continue;
85+
const layout = yield* resolveCodexHomeLayout(config.value);
86+
add("codex", path.join(layout.sharedHomePath, "sessions"));
87+
}
88+
}
89+
90+
return dirs;
91+
});

‎docs/user/usage.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
The Usage page combines Codex and Claude Code activity from your connected environments. It reads
44
the providers' local session history and shows API-equivalent token cost, processed tokens, cache
5-
savings, provider shares, and model breakdowns. Subscription billing is separate from the raw token
6-
cost shown here.
5+
savings, provider shares, and model breakdowns. Every configured Codex and Claude Code instance
6+
counts, including extra instances that use their own home directory. Subscription billing is
7+
separate from the raw token cost shown here.
78

89
Use **Past 24h** for an hourly chart covering the exact rolling 24-hour period. The **7 days**,
910
**30 days**, and **90 days** ranges use daily resolution. Cost and token toggles update both the

0 commit comments

Comments
 (0)