Skip to content

Commit 4546e55

Browse files
committed
fix(usage): resolve relative transcript homes per workspace
- Resolve relative CLAUDE_CONFIG_DIR values from every active and archived workspace - Pass workspace paths through usage summary collection
1 parent 511770b commit 4546e55

4 files changed

Lines changed: 100 additions & 19 deletions

File tree

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ const encodeScanCacheFile = Schema.encodeEffect(ScanCacheJson);
8888
export class UsageService extends Context.Service<
8989
UsageService,
9090
{
91-
readonly readSummary: (input: UsageSummaryInput) => Effect.Effect<UsageSummary, UsageReadError>;
91+
readonly readSummary: (
92+
input: UsageSummaryInput,
93+
workspaceCwds?: readonly string[],
94+
) => Effect.Effect<UsageSummary, UsageReadError>;
9295
}
9396
>()("t3/usage/UsageService") {}
9497

@@ -184,7 +187,9 @@ export const make = Effect.gen(function* () {
184187
});
185188

186189
/** Resolves the transcript directory for each provider. */
187-
const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* () {
190+
const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* (
191+
workspaceCwds: readonly string[],
192+
) {
188193
// A settings failure must surface as an error: swallowing it here would
189194
// present "zero usage from every provider" as a valid answer.
190195
const settings = yield* settingsService.getSettings.pipe(
@@ -201,7 +206,7 @@ export const make = Effect.gen(function* () {
201206
),
202207
);
203208

204-
return yield* resolveUsageTranscriptSources(settings);
209+
return yield* resolveUsageTranscriptSources(settings, process.env, workspaceCwds);
205210
});
206211

207212
/**
@@ -269,7 +274,10 @@ export const make = Effect.gen(function* () {
269274
return records;
270275
});
271276

272-
const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) {
277+
const readSummary = Effect.fn("UsageService.readSummary")(function* (
278+
input: UsageSummaryInput,
279+
workspaceCwds: readonly string[] = [config.cwd],
280+
) {
273281
if (input.sinceDay > input.untilDay) {
274282
return yield* new UsageReadError({
275283
reason: "invalidWindow",
@@ -308,7 +316,9 @@ export const make = Effect.gen(function* () {
308316
const hostId = NodeOS.hostname();
309317
// The source resolvers ask for Path themselves; satisfy them from the
310318
// instance we already hold so readSummary stays context-free.
311-
const dirs = yield* resolveTranscriptDirs().pipe(Effect.provideService(Path.Path, path));
319+
const dirs = yield* resolveTranscriptDirs(workspaceCwds).pipe(
320+
Effect.provideService(Path.Path, path),
321+
);
312322
const windowStart = DateTime.make(`${input.sinceDay}T00:00:00Z`);
313323
if (Option.isNone(windowStart)) {
314324
return yield* new UsageReadError({

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,35 @@ it.layer(NodeServices.layer)("resolveUsageTranscriptSources", (it) => {
103103
}),
104104
);
105105

106+
it.effect("resolves a relative CLAUDE_CONFIG_DIR from every workspace cwd", () =>
107+
Effect.gen(function* () {
108+
const path = yield* Path.Path;
109+
const settings = decodeSettings({
110+
providerInstances: {
111+
claudeAgent: {
112+
driver: "claudeAgent",
113+
config: {},
114+
environment: [{ name: "CLAUDE_CONFIG_DIR", value: "relative-config" }],
115+
},
116+
codex: { driver: "codex", config: {} },
117+
},
118+
});
119+
120+
const sources = yield* resolveUsageTranscriptSources(settings, {}, [
121+
"/tmp/workspace-a",
122+
"/tmp/workspace-b",
123+
]);
124+
125+
assert.deepEqual(
126+
sources.filter((source) => source.provider === "claude"),
127+
[
128+
{ provider: "claude", dir: path.resolve("/tmp/workspace-a/relative-config/projects") },
129+
{ provider: "claude", dir: path.resolve("/tmp/workspace-b/relative-config/projects") },
130+
],
131+
);
132+
}),
133+
);
134+
106135
it.effect("honors per-instance CODEX_HOME when homePath is empty", () =>
107136
Effect.gen(function* () {
108137
const path = yield* Path.Path;

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,32 @@ function configuredUsageInstances(settings: ServerSettings): readonly ProviderIn
6565
* environment config dirs store transcripts directly under `projects`; only
6666
* the default installation nests them under `~/.claude/projects`.
6767
*/
68-
const resolveClaudeInstanceTranscriptDir = Effect.fn(
69-
"UsageTranscriptSources.resolveClaudeInstanceTranscriptDir",
68+
const resolveClaudeInstanceTranscriptDirs = Effect.fn(
69+
"UsageTranscriptSources.resolveClaudeInstanceTranscriptDirs",
7070
)(function* (
7171
config: ClaudeSettings,
7272
instance: ProviderInstanceConfig,
7373
baseEnvironment: NodeJS.ProcessEnv,
74+
workspaceCwds: readonly string[],
7475
) {
7576
const path = yield* Path.Path;
7677
if (config.homePath.trim().length > 0) {
77-
return path.join(yield* resolveClaudeHomePath(config), "projects");
78+
return [path.join(yield* resolveClaudeHomePath(config), "projects")];
7879
}
7980

8081
const environment = mergeProviderInstanceEnvironment(instance.environment, baseEnvironment);
8182
const environmentHome = environment.CLAUDE_CONFIG_DIR?.trim() ?? "";
8283
if (environmentHome.length > 0) {
8384
// Environment variables are passed directly to the subprocess and do
84-
// not receive shell-style tilde expansion.
85-
return path.join(path.resolve(environmentHome), "projects");
85+
// not receive shell-style tilde expansion. Relative values resolve from
86+
// each workspace cwd used to launch Claude.
87+
const configDirs = path.isAbsolute(environmentHome)
88+
? [path.resolve(environmentHome)]
89+
: workspaceCwds.map((cwd) => path.resolve(cwd, environmentHome));
90+
return configDirs.map((configDir) => path.join(configDir, "projects"));
8691
}
8792

88-
return path.join(NodeOS.homedir(), ".claude", "projects");
93+
return [path.join(NodeOS.homedir(), ".claude", "projects")];
8994
});
9095

9196
/**
@@ -119,7 +124,11 @@ const resolveCodexInstanceLayout = Effect.fn("UsageTranscriptSources.resolveCode
119124
/** Resolve unique transcript roots for every configured Claude/Codex instance. */
120125
export const resolveUsageTranscriptSources = Effect.fn(
121126
"UsageTranscriptSources.resolveUsageTranscriptSources",
122-
)(function* (settings: ServerSettings, baseEnvironment: NodeJS.ProcessEnv = process.env) {
127+
)(function* (
128+
settings: ServerSettings,
129+
baseEnvironment: NodeJS.ProcessEnv = process.env,
130+
workspaceCwds: readonly string[] = [process.cwd()],
131+
) {
123132
const path = yield* Path.Path;
124133
const sources: UsageTranscriptSource[] = [];
125134
const seen = new Set<string>();
@@ -135,10 +144,13 @@ export const resolveUsageTranscriptSources = Effect.fn(
135144
if (instance.driver === CLAUDE_DRIVER) {
136145
const decoded = decodeClaudeSettings(instance.config ?? {});
137146
if (Option.isNone(decoded)) continue;
138-
append({
139-
provider: "claude",
140-
dir: yield* resolveClaudeInstanceTranscriptDir(decoded.value, instance, baseEnvironment),
141-
});
147+
const dirs = yield* resolveClaudeInstanceTranscriptDirs(
148+
decoded.value,
149+
instance,
150+
baseEnvironment,
151+
workspaceCwds,
152+
);
153+
for (const dir of dirs) append({ provider: "claude", dir });
142154
continue;
143155
}
144156

‎apps/server/src/ws.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
type RelayClientInstallProgressEvent,
4545
type ServerSelfUpdateError,
4646
type ServerSelfUpdateProgressEvent,
47+
UsageReadError,
4748
type FilesystemBrowseFailure,
4849
FilesystemBrowseError,
4950
AssetWorkspaceContextNotFoundError,
@@ -1564,9 +1565,38 @@ const makeWsRpcLayer = (
15641565
},
15651566
),
15661567
[WS_METHODS.serverGetUsageSummary]: (input) =>
1567-
observeRpcEffect(WS_METHODS.serverGetUsageSummary, usage.readSummary(input), {
1568-
"rpc.aggregate": "server",
1569-
}),
1568+
observeRpcEffect(
1569+
WS_METHODS.serverGetUsageSummary,
1570+
Effect.gen(function* () {
1571+
const [active, archived] = yield* Effect.all(
1572+
[
1573+
projectionSnapshotQuery.getShellSnapshot(),
1574+
projectionSnapshotQuery.getArchivedShellSnapshot(),
1575+
],
1576+
{ concurrency: "unbounded" },
1577+
).pipe(
1578+
Effect.mapError(
1579+
(cause) =>
1580+
new UsageReadError({
1581+
reason: "scanFailed",
1582+
detail: "Project workspaces could not be read.",
1583+
cause,
1584+
}),
1585+
),
1586+
);
1587+
const workspaceCwds = new Set([config.cwd]);
1588+
for (const snapshot of [active, archived]) {
1589+
for (const project of snapshot.projects) {
1590+
workspaceCwds.add(project.workspaceRoot);
1591+
}
1592+
for (const thread of snapshot.threads) {
1593+
if (thread.worktreePath !== null) workspaceCwds.add(thread.worktreePath);
1594+
}
1595+
}
1596+
return yield* usage.readSummary(input, [...workspaceCwds]);
1597+
}),
1598+
{ "rpc.aggregate": "server" },
1599+
),
15701600
[WS_METHODS.serverRetryResourceTelemetry]: (_input) =>
15711601
observeRpcEffect(WS_METHODS.serverRetryResourceTelemetry, resourceTelemetry.retry, {
15721602
"rpc.aggregate": "server",

0 commit comments

Comments
 (0)