The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 04:30 UTC
fix(pi): usage reports include sessions stored in a --session-dir launch argument (#30690)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
Alex Southwell committed
Nov 5, 2026 at 18:12 UTC
df0cc36dea0c36d155b39f58e33fef66b426be19
3 files changed
+73
-9
apps/server/src/usage/UsageService.test.ts
+45
-6
@@ -1275,11 +1275,27 @@ describe("UsageService", () => {
1275
it.live("discovers Pi history in its default home and directory overrides", () =>
1276
Effect.gen(function* () {
1277
const { home, settings } = yield* setup;
1278
- for (const [agentDir, sessionDir, relativeDir] of [
1279
- [undefined, undefined, ".pi/agent/sessions"],
1280
- ["~/custom-pi", undefined, "custom-pi/sessions"],
1281
- ["~/ignored-pi", "~/custom-sessions", "custom-sessions"],
1282
- ["~/ignored-pi", NodePath.join(home, "absolute-sessions"), "absolute-sessions"],
1278
+ for (const [agentDir, sessionDir, relativeDir, launchArgs, instanceHome, platform] of [
1279
+ [undefined, undefined, ".pi/agent/sessions", undefined],
1280
+ ["~/custom-pi", undefined, "custom-pi/sessions", undefined],
1281
+ ["~/ignored-pi", "~/custom-sessions", "custom-sessions", undefined],
1282
+ ["~/ignored-pi", NodePath.join(home, "absolute-sessions"), "absolute-sessions", undefined],
1283
+ ["~/ignored-pi", undefined, "launch-sessions", "--session-dir ~/launch-sessions"],
1284
+ [
1285
+ "~/ignored-pi",
1286
+ undefined,
1287
+ "instance-home /launch-sessions",
1288
+ "--session-dir ~/launch-sessions",
1289
+ "instance-home ",
1290
+ ],
1291
+ [
1292
+ "~/ignored-pi",
1293
+ undefined,
1294
+ "windows-profile/launch-sessions",
1295
+ "--session-dir ~/launch-sessions",
1296
+ "windows-profile",
1297
+ "win32",
1298
+ ],
1299
] as const) {
1300
const dir = NodePath.join(home, relativeDir);
1301
yield* Effect.promise(async () => {
@@ -1318,11 +1334,34 @@ describe("UsageService", () => {
1334
layerService({
1335
prefix: "usage-pi-" + relativeDir.replaceAll("/", "-"),
1336
home,
1321
- settings,
1337
+ settings:
1338
+ launchArgs === undefined
1339
+ ? settings
1340
+ : {
1341
+ ...settings,
1342
+ providerInstances: {
1343
+ ...settings.providerInstances,
1344
+ [ProviderInstanceId.make("pi")]: {
1345
+ driver: ProviderDriverKind.make("pi"),
1346
+ config: { launchArgs },
1347
+ environment:
1348
+ instanceHome === undefined
1349
+ ? []
1350
+ : [
1351
+ {
1352
+ name: platform === "win32" ? "USERPROFILE" : "HOME",
1353
+ value: NodePath.join(home, instanceHome),
1354
+ sensitive: false,
1355
+ },
1356
+ ],
1357
+ },
1358
+ },
1359
+ },
1360
environment: {
1361
PI_CODING_AGENT_DIR: agentDir,
1362
PI_CODING_AGENT_SESSION_DIR: sessionDir,
1363
},
1364
+ platform: platform ?? "linux",
1365
}),
1366
),
1367
Effect.provideService(HostProcess.HomeDirectory, home),
packages/provider-pi/src/server/mcpInjection.ts
+1
-1
@@ -32,7 +32,7 @@ const RESERVED_PI_LAUNCH_ARGUMENTS = new Set([
32
"-v",
33
]);
34
35
-const PI_ARGUMENTS_WITH_VALUES = new Set([
35
+export const PI_ARGUMENTS_WITH_VALUES = new Set([
36
"--api-key",
37
"--append-system-prompt",
38
"--exclude-tools",
packages/provider-pi/src/server/usage.ts
+27
-2
@@ -14,6 +14,7 @@ import {
14
} from "@t3tools/provider-core/server/usage";
15
import * as HostProcess from "@t3tools/shared/HostProcess";
16
import type { PiSettings } from "../settings.ts";
17
+import { PI_ARGUMENTS_WITH_VALUES, resolvePiLaunchArgs } from "./mcpInjection.ts";
18
19
const PiScanState = Schema.Struct({ sessionId: Schema.mutableKey(Schema.String) });
20
const Usage = Schema.Struct({
@@ -128,13 +129,37 @@ export const piUsageReader: ProviderUsageReader<PiSettings, Path.Path> = {
129
kind: "transcripts",
130
provider: "pi",
131
format: piUsageFormat,
131
- directories: Effect.fn("piUsageReader.directories")(function* ({ environment }) {
132
+ directories: Effect.fn("piUsageReader.directories")(function* ({ config, environment }) {
133
const path = yield* Path.Path;
134
const home = yield* HostProcess.HomeDirectory;
134
- return [{ dir: resolvePiSessionsDir(environment, home, path) }];
135
+ const platform = yield* HostProcess.Platform;
136
+ const launchHome = (platform === "win32" ? environment.USERPROFILE : environment.HOME) || home;
137
+ const sessionDirArg = config === undefined ? undefined : piLaunchSessionDir(config.launchArgs);
138
+ const launchDir =
139
+ sessionDirArg === undefined ? undefined : expandHomePath(sessionDirArg, launchHome);
140
+ // --session-dir moves only T3-launched sessions; terminal sessions stay in Pi's own directory.
141
+ return [
142
+ ...(launchDir !== undefined && path.isAbsolute(launchDir)
143
+ ? [{ dir: path.resolve(launchDir) }]
144
+ : []),
145
+ { dir: resolvePiSessionsDir(environment, home, path) },
146
+ ];
147
}),
148
};
149
150
+function piLaunchSessionDir(launchArgs: string): string | undefined {
151
+ const resolved = resolvePiLaunchArgs(launchArgs);
152
+ if (!resolved.ok) return undefined;
153
+ let sessionDir: string | undefined;
154
+ for (let index = 0; index < resolved.args.length; index += 1) {
155
+ const arg = resolved.args[index];
156
+ if (arg === undefined || !PI_ARGUMENTS_WITH_VALUES.has(arg)) continue;
157
+ index += 1;
158
+ if (arg === "--session-dir") sessionDir = resolved.args[index] || undefined;
159
+ }
160
+ return sessionDir;
161
+}
162
+
163
/** Where Pi stores session JSONL files for a provider environment, matching Pi's own lookup. */
164
export function resolvePiSessionsDir(
165
environment: NodeJS.ProcessEnv,