fix(usage): aggregate transcripts from every provider instance #6603
Closedeffektsvk(GitHub profile, opens in a new tab)wants to merge 5 commits into
main fromrefs/pull/6603/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/usage/usageTranscriptSources.ts lines 157 to 162Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +120,21 @@120+ sources.push(source);121+ };122+123+ for (const instance of configuredUsageInstances(settings)) {124+ if (instance.driver === CLAUDE_DRIVER) {125+ const decoded = decodeClaudeSettings(instance.config ?? {});126+ if (Option.isNone(decoded)) continue;127+ const homePath = yield* resolveClaudeInstanceHome(decoded.value, instance, baseEnvironment);128+ append({129+ provider: "claude",130+ dir: yield* resolveClaudeTranscriptDir(homePath),131+ });132+ continue;133+ }134+135+ if (instance.driver === CODEX_DRIVER) {136+ const decoded = decodeCodexSettings(instance.config ?? {});137+ if (Option.isNone(decoded)) continue;138+ const layout = yield* resolveCodexHomeLayout(decoded.value);139+ append({ provider: "codex", dir: path.join(layout.sharedHomePath, "sessions") });140+ }apps/server/src/usage/UsageService.ts line 332OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -340,17 +322,11 @@340322 const windowStartMs =341323 (hourlyWindow?.sinceTimeMs ?? DateTime.toEpochMillis(windowStart.value)) - MTIME_SLACK_MS;342324343- const aggregator = new UsageAggregator({344- timeZone: input.timeZone,345- sinceDay: input.sinceDay,346- untilDay: input.untilDay,347- resolution: input.resolution ?? "day",348- ...hourlyWindow,349- rates,350- });351-352325 const sources: UsageSource[] = [];326+ const buckets: UsageSummary["buckets"][number][] = [];353327 const livePaths = new Set<string>();354328 const walkedRoots: string[] = [];355329356330 for (const { provider, dir } of dirs) {331+ const sourceIndex = sources.length;332+ const aggregator = new UsageAggregator({@@ -0,0 +84,21 @@84+ // not receive shell-style tilde expansion.85+ return path.resolve(environmentHome);86+ }87+88+ return path.resolve(NodeOS.homedir());89+ },90+);91+92+/**93+ * Claude's config dir is the home itself when overridden, but older/default94+ * layouts nest transcripts under `<home>/.claude/projects`. Probe both.95+ */96+const resolveClaudeTranscriptDir = Effect.fn("UsageTranscriptSources.resolveClaudeTranscriptDir")(97+ function* (homePath: string) {98+ const fileSystem = yield* FileSystem.FileSystem;99+ const path = yield* Path.Path;100+ const nested = path.join(homePath, ".claude", "projects");101+ const nestedExists = yield* fileSystem102+ .exists(nested)103+ .pipe(Effect.catchCause(() => Effect.succeed(false)));104+ return nestedExists ? nested : path.join(homePath, "projects");
- apps/server/src/usage/usageTranscriptSources.ts line 162Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +120,21 @@120+ sources.push(source);121+ };122+123+ for (const instance of configuredUsageInstances(settings)) {124+ if (instance.driver === CLAUDE_DRIVER) {125+ const decoded = decodeClaudeSettings(instance.config ?? {});126+ if (Option.isNone(decoded)) continue;127+ const homePath = yield* resolveClaudeInstanceHome(decoded.value, instance, baseEnvironment);128+ append({129+ provider: "claude",130+ dir: yield* resolveClaudeTranscriptDir(homePath),131+ });132+ continue;133+ }134+135+ if (instance.driver === CODEX_DRIVER) {136+ const decoded = decodeCodexSettings(instance.config ?? {});137+ if (Option.isNone(decoded)) continue;138+ const layout = yield* resolveCodexHomeLayout(decoded.value);139+ append({ provider: "codex", dir: path.join(layout.sharedHomePath, "sessions") });140+ }
- apps/server/src/usage/usageTranscriptSources.ts line 85OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +65,21 @@65+ * environment config dirs store transcripts directly under `projects`; only66+ * the default installation nests them under `~/.claude/projects`.67+ */68+const resolveClaudeInstanceTranscriptDir = Effect.fn(69+ "UsageTranscriptSources.resolveClaudeInstanceTranscriptDir",70+)(function* (71+ config: ClaudeSettings,72+ instance: ProviderInstanceConfig,73+ baseEnvironment: NodeJS.ProcessEnv,74+) {75+ const path = yield* Path.Path;76+ if (config.homePath.trim().length > 0) {77+ return path.join(yield* resolveClaudeHomePath(config), "projects");78+ }79+80+ const environment = mergeProviderInstanceEnvironment(instance.environment, baseEnvironment);81+ const environmentHome = environment.CLAUDE_CONFIG_DIR?.trim() ?? "";82+ if (environmentHome.length > 0) {83+ // Environment variables are passed directly to the subprocess and do84+ // not receive shell-style tilde expansion.85+ return path.join(path.resolve(environmentHome), "projects");
- @@ -1568,2 +1568,19 @@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+ );
- apps/server/src/ws.ts line 1573OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1564,6 +1565,9 @@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 workspaceCwds = yield* resolveUsageWorkspaceCwds(1572+ config.cwd,1573+ Effect.all(
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
aecb3271-c57b-4984-b8ea-2e455df8a351You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share