Skip to content

Commit 87efc09

Browse files
fix(server): honor configured transcript directories
1 parent 0f6ac7d commit 87efc09

4 files changed

Lines changed: 58 additions & 30 deletions

File tree

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,17 @@ describe("UsageService", () => {
170170
const firstScanStarted = yield* Deferred.make<void>();
171171
const secondScanStarted = yield* Deferred.make<void>();
172172
const releaseRates = yield* Deferred.make<void>();
173-
let homeProbes = 0;
173+
let homeResolutions = 0;
174174
const service = yield* UsageService.make.pipe(
175175
Effect.provideService(FileSystem.FileSystem, {
176176
...fileSystem,
177-
exists: (path) =>
178-
fileSystem.exists(path).pipe(
177+
realPath: (path) =>
178+
fileSystem.realPath(path).pipe(
179179
Effect.tap(() => {
180-
if (path !== NodePath.join(home, "claude", ".claude", "projects"))
181-
return Effect.void;
182-
homeProbes += 1;
180+
if (path !== NodePath.join(home, "claude", "projects")) return Effect.void;
181+
homeResolutions += 1;
183182
return Deferred.succeed(
184-
homeProbes === 1 ? firstScanStarted : secondScanStarted,
183+
homeResolutions === 1 ? firstScanStarted : secondScanStarted,
185184
undefined,
186185
);
187186
}),
@@ -218,6 +217,50 @@ describe("UsageService", () => {
218217
}).pipe(Effect.scoped),
219218
);
220219

220+
for (const source of ["explicit", "environment"] as const) {
221+
it.live(`reads the configured ${source} Claude projects when a nested tree exists`, () =>
222+
Effect.gen(function* () {
223+
const { transcript, settings, home } = yield* setup;
224+
const configDir = NodePath.join(home, "claude");
225+
const nestedDir = NodePath.join(configDir, ".claude", "projects", "proj");
226+
yield* Effect.promise(() => NodeFSP.mkdir(nestedDir, { recursive: true }));
227+
yield* Effect.promise(() => NodeFSP.writeFile(transcript, claudeLine(1, 5)));
228+
yield* Effect.promise(() =>
229+
NodeFSP.writeFile(NodePath.join(nestedDir, "session.jsonl"), claudeLine(2, 99)),
230+
);
231+
const configuredSettings = {
232+
providers: settings.providers,
233+
providerInstances: {
234+
claudeAgent: {
235+
driver: "claudeAgent" as const,
236+
config: { homePath: source === "explicit" ? configDir : "" },
237+
environment: [{ name: "CLAUDE_CONFIG_DIR", value: configDir }],
238+
},
239+
},
240+
};
241+
const service = yield* UsageService.make.pipe(
242+
Effect.provide(
243+
serviceLayers({
244+
prefix: `usage-service-config-dir-${source}`,
245+
home,
246+
settings: configuredSettings,
247+
}),
248+
),
249+
);
250+
const summary = yield* service.readSummary(WINDOW);
251+
assert.strictEqual(totalOutputTokens(summary), 5);
252+
const canonicalDir = yield* Effect.promise(() =>
253+
NodeFSP.realpath(NodePath.join(configDir, "projects")),
254+
);
255+
assert.strictEqual(
256+
summary.sources.find((source) => source.fingerprint.provider === "claude")?.fingerprint
257+
.resolvedHomePath,
258+
canonicalDir,
259+
);
260+
}).pipe(Effect.scoped),
261+
);
262+
}
263+
221264
it.live("canonicalizes aliased homes and keeps missing homes visible", () =>
222265
Effect.gen(function* () {
223266
const platform = yield* HostProcessPlatform;

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,6 @@ export const make = Effect.gen(function* () {
218218
Effect.withSpan("UsageService.refreshRates"),
219219
);
220220

221-
/**
222-
* Claude's config dir is the home itself when overridden, but a default
223-
* install nests transcripts under `~/.claude/projects`. Probe both.
224-
*/
225-
const resolveClaudeTranscriptDir = (homePath: string) =>
226-
Effect.gen(function* () {
227-
const nested = path.join(homePath, ".claude", "projects");
228-
const nestedExists = yield* fileSystem
229-
.exists(nested)
230-
.pipe(Effect.catchCause(() => Effect.succeed(false)));
231-
return nestedExists ? nested : path.join(homePath, "projects");
232-
});
233-
234221
// A settings failure must not silently discard custom rates or transcript homes.
235222
const readSettings = settingsService.getSettings.pipe(
236223
Effect.catchCause(
@@ -279,9 +266,7 @@ export const make = Effect.gen(function* () {
279266
});
280267

281268
for (const home of homes.claudeHomePaths) {
282-
// Distinct homes can probe to the same transcript dir (e.g. `~/x` with
283-
// a nested `.claude` next to `~/x/.claude` itself), so dedupe post-probe.
284-
yield* pushDir("claude", yield* resolveClaudeTranscriptDir(home));
269+
yield* pushDir("claude", path.join(home, "projects"));
285270
}
286271
for (const dir of homes.codexSessionDirs) {
287272
yield* pushDir("codex", dir);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ it.layer(NodeServices.layer)("usageProviderHomes", (it) => {
4141
path.resolve(NodeOS.homedir(), ".claude-max"),
4242
path.resolve(NodeOS.homedir(), ".claude-pro"),
4343
// Synthesized legacy `claudeAgent` instance: the default home.
44-
path.resolve(NodeOS.homedir()),
44+
path.resolve(NodeOS.homedir(), ".claude"),
4545
]);
4646
expect(homes.codexSessionDirs).toEqual([
4747
path.join(path.resolve(NodeOS.homedir(), ".codex-work"), "sessions"),
@@ -65,7 +65,7 @@ it.layer(NodeServices.layer)("usageProviderHomes", (it) => {
6565

6666
// `claude_alias` and the synthesized legacy instance share the
6767
// default home; scanning it twice would double count every record.
68-
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir())]);
68+
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir(), ".claude")]);
6969
}),
7070
);
7171

@@ -80,7 +80,7 @@ it.layer(NodeServices.layer)("usageProviderHomes", (it) => {
8080

8181
const homes = yield* resolveUsageProviderHomes(settings, {});
8282

83-
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir())]);
83+
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir(), ".claude")]);
8484
}),
8585
);
8686

@@ -110,7 +110,7 @@ it.layer(NodeServices.layer)("usageProviderHomes", (it) => {
110110

111111
expect(homes.claudeHomePaths).toEqual([
112112
path.join(NodeOS.homedir(), ".claude-tilde"),
113-
path.resolve(NodeOS.homedir()),
113+
path.resolve(NodeOS.homedir(), ".claude"),
114114
]);
115115
expect(homes.codexSessionDirs).toEqual([
116116
path.join(NodeOS.homedir(), ".codex", "sessions"),
@@ -208,7 +208,7 @@ it.layer(NodeServices.layer)("usageProviderHomes", (it) => {
208208
CODEX_HOME: path.join(NodeOS.homedir(), ".codex-ambient"),
209209
});
210210

211-
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir())]);
211+
expect(homes.claudeHomePaths).toEqual([path.resolve(NodeOS.homedir(), ".claude")]);
212212
expect(homes.codexSessionDirs).toEqual([path.join(NodeOS.homedir(), ".codex", "sessions")]);
213213
}),
214214
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const decodeClaudeSettings = Schema.decodeUnknownEffect(ClaudeSettings);
3131
const decodeCodexSettings = Schema.decodeUnknownEffect(CodexSettings);
3232

3333
export interface UsageProviderHomes {
34-
/** One entry per distinct Claude home; transcripts nest under it. */
34+
/** One entry per distinct Claude config directory; transcripts live in its projects child. */
3535
readonly claudeHomePaths: readonly string[];
3636
/** One entry per distinct Codex `sessions` directory. */
3737
readonly codexSessionDirs: readonly string[];
@@ -78,7 +78,7 @@ export const resolveUsageProviderHomes = Effect.fn("resolveUsageProviderHomes")(
7878
}
7979
const environment = mergeProviderInstanceEnvironment(envelope.environment, hostEnvironment);
8080
const configDir = environmentHomePath(environment["CLAUDE_CONFIG_DIR"]);
81-
pushUnique(claudeHomePaths, configDir ?? (yield* resolveClaudeHomePath(config)));
81+
pushUnique(claudeHomePaths, configDir ?? path.join(NodeOS.homedir(), ".claude"));
8282
} else if (envelope.driver === "codex") {
8383
const config = yield* decodeCodexSettings(envelope.config ?? {}).pipe(
8484
Effect.catchCause(() => Effect.succeed(null)),

0 commit comments

Comments
 (0)