Skip to content

Commit 236d2fd

Browse files
saphidclaude
authored andcommitted
fix(pi): usage reports include sessions stored in a --session-dir launch argument
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8b44f42 commit 236d2fd

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,11 +1269,12 @@ describe("UsageService", () => {
12691269
it.live("discovers Pi history in its default home and directory overrides", () =>
12701270
Effect.gen(function* () {
12711271
const { home, settings } = yield* setup;
1272-
for (const [agentDir, sessionDir, relativeDir] of [
1273-
[undefined, undefined, ".pi/agent/sessions"],
1274-
["~/custom-pi", undefined, "custom-pi/sessions"],
1275-
["~/ignored-pi", "~/custom-sessions", "custom-sessions"],
1276-
["~/ignored-pi", NodePath.join(home, "absolute-sessions"), "absolute-sessions"],
1272+
for (const [agentDir, sessionDir, relativeDir, launchArgs] of [
1273+
[undefined, undefined, ".pi/agent/sessions", undefined],
1274+
["~/custom-pi", undefined, "custom-pi/sessions", undefined],
1275+
["~/ignored-pi", "~/custom-sessions", "custom-sessions", undefined],
1276+
["~/ignored-pi", NodePath.join(home, "absolute-sessions"), "absolute-sessions", undefined],
1277+
["~/ignored-pi", undefined, "launch-sessions", "--session-dir ~/launch-sessions"],
12771278
] as const) {
12781279
const dir = NodePath.join(home, relativeDir);
12791280
yield* Effect.promise(async () => {
@@ -1312,7 +1313,19 @@ describe("UsageService", () => {
13121313
layerService({
13131314
prefix: "usage-pi-" + relativeDir.replaceAll("/", "-"),
13141315
home,
1315-
settings,
1316+
settings:
1317+
launchArgs === undefined
1318+
? settings
1319+
: {
1320+
...settings,
1321+
providerInstances: {
1322+
...settings.providerInstances,
1323+
[ProviderInstanceId.make("pi")]: {
1324+
driver: ProviderDriverKind.make("pi"),
1325+
config: { launchArgs },
1326+
},
1327+
},
1328+
},
13161329
environment: {
13171330
PI_CODING_AGENT_DIR: agentDir,
13181331
PI_CODING_AGENT_SESSION_DIR: sessionDir,

‎packages/provider-pi/src/server/mcpInjection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const RESERVED_PI_LAUNCH_ARGUMENTS = new Set([
3232
"-v",
3333
]);
3434

35-
const PI_ARGUMENTS_WITH_VALUES = new Set([
35+
export const PI_ARGUMENTS_WITH_VALUES = new Set([
3636
"--api-key",
3737
"--append-system-prompt",
3838
"--exclude-tools",

‎packages/provider-pi/src/server/usage.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "@t3tools/provider-core/server/usage";
1515
import * as HostProcess from "@t3tools/shared/HostProcess";
1616
import type { PiSettings } from "../settings.ts";
17+
import { PI_ARGUMENTS_WITH_VALUES, resolvePiLaunchArgs } from "./mcpInjection.ts";
1718

1819
const PiScanState = Schema.Struct({ sessionId: Schema.mutableKey(Schema.String) });
1920
const Usage = Schema.Struct({
@@ -128,13 +129,34 @@ export const piUsageReader: ProviderUsageReader<PiSettings, Path.Path> = {
128129
kind: "transcripts",
129130
provider: "pi",
130131
format: piUsageFormat,
131-
directories: Effect.fn("piUsageReader.directories")(function* ({ environment }) {
132+
directories: Effect.fn("piUsageReader.directories")(function* ({ config, environment }) {
132133
const path = yield* Path.Path;
133134
const home = yield* HostProcess.HomeDirectory;
134-
return [{ dir: resolvePiSessionsDir(environment, home, path) }];
135+
const sessionDirArg = config === undefined ? undefined : piLaunchSessionDir(config.launchArgs);
136+
const launchDir = sessionDirArg === undefined ? undefined : expandHomePath(sessionDirArg, home);
137+
// --session-dir moves only T3-launched sessions; terminal sessions stay in Pi's own directory.
138+
return [
139+
...(launchDir !== undefined && path.isAbsolute(launchDir)
140+
? [{ dir: path.resolve(launchDir) }]
141+
: []),
142+
{ dir: resolvePiSessionsDir(environment, home, path) },
143+
];
135144
}),
136145
};
137146

147+
function piLaunchSessionDir(launchArgs: string): string | undefined {
148+
const resolved = resolvePiLaunchArgs(launchArgs);
149+
if (!resolved.ok) return undefined;
150+
let sessionDir: string | undefined;
151+
for (let index = 0; index < resolved.args.length; index += 1) {
152+
const arg = resolved.args[index];
153+
if (arg === undefined || !PI_ARGUMENTS_WITH_VALUES.has(arg)) continue;
154+
index += 1;
155+
if (arg === "--session-dir") sessionDir = resolved.args[index] || undefined;
156+
}
157+
return sessionDir;
158+
}
159+
138160
/** Where Pi stores session JSONL files for a provider environment, matching Pi's own lookup. */
139161
export function resolvePiSessionsDir(
140162
environment: NodeJS.ProcessEnv,

0 commit comments

Comments
 (0)