Skip to content

Commit f3ec048

Browse files
fix(server): Pi usage and session import read the instance's home
A Pi provider instance with its own HOME (USERPROFILE on Windows) only used it for a --session-dir launch argument. The default ~/.pi/agent/sessions and ~/ forms of PI_CODING_AGENT_DIR and PI_CODING_AGENT_SESSION_DIR resolved against the server's home, so Usage and terminal-session import missed that instance's sessions. Resolve the instance home once with piHome and pass it to resolvePiSessionsDir from both Usage and AgentSessionScanner. Fixes #34717 AI-assisted; verified with the tests and tools listed above.
1 parent e453a25 commit f3ec048

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎apps/server/src/project/AgentSessionScanner.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
660660
const codexWorkspace = yield* makeTempDir("t3code-workspace-codex-");
661661
const piWorkspace = yield* makeTempDir("t3code-workspace-pi-");
662662
const piFlatWorkspace = yield* makeTempDir("t3code-workspace-pi-flat-");
663+
const piInstanceHome = yield* makeTempDir("t3code-pi-instance-home-");
664+
const piHomeWorkspace = yield* makeTempDir("t3code-workspace-pi-home-");
663665

664666
// Pi nests sessions per cwd by default and keeps a custom session directory flat.
665667
for (const [filePath, cwd, mtime] of [
@@ -673,6 +675,11 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
673675
piFlatWorkspace,
674676
"2026-01-04T00:00:00.000Z",
675677
],
678+
[
679+
path.join(piInstanceHome, "custom-pi", "sessions", "--slug--", "2026-01-05_pi.jsonl"),
680+
piHomeWorkspace,
681+
"2026-01-05T00:00:00.000Z",
682+
],
676683
] as const) {
677684
yield* writeTranscript({
678685
filePath,
@@ -731,10 +738,22 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
731738
],
732739
config: {},
733740
},
741+
[ProviderInstanceId.make("pi-home")]: {
742+
driver: ProviderDriverKind.make("pi"),
743+
enabled: true,
744+
environment: [
745+
{ name: "HOME", value: piInstanceHome, sensitive: false },
746+
{ name: "USERPROFILE", value: piInstanceHome, sensitive: false },
747+
{ name: "PI_CODING_AGENT_DIR", value: "~/custom-pi", sensitive: false },
748+
{ name: "PI_CODING_AGENT_SESSION_DIR", value: "", sensitive: false },
749+
],
750+
config: {},
751+
},
734752
},
735753
});
736754

737755
expect(result.candidates.map((candidate) => [candidate.path, candidate.sources])).toEqual([
756+
[piHomeWorkspace, ["pi"]],
738757
[piFlatWorkspace, ["pi"]],
739758
[piWorkspace, ["pi"]],
740759
[codexWorkspace, ["codex"]],

‎apps/server/src/project/AgentSessionScanner.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import * as ProviderSessionRuntime from "../persistence/ProviderSessionRuntime.t
3838
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
3939
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
4040
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
41-
import { resolvePiSessionsDir } from "@t3tools/provider-pi/server/usage";
41+
import { piHome, resolvePiSessionsDir } from "@t3tools/provider-pi/server/usage";
4242
import * as ServerSettings from "../serverSettings.ts";
4343
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
4444
import {
@@ -824,7 +824,8 @@ export const make = Effect.gen(function* () {
824824
const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
825825
const baseDir = path.resolve(serverConfig.baseDir);
826826
const worktreesDir = path.resolve(serverConfig.worktreesDir);
827-
const isWindows = (yield* HostProcess.Platform) === "win32";
827+
const platform = yield* HostProcess.Platform;
828+
const isWindows = platform === "win32";
828829
const foldWorktreeCase = isWindows;
829830
const hostEnvironment = yield* HostProcess.Environment;
830831
const homeDir = yield* HostProcess.HomeDirectory;
@@ -1591,7 +1592,11 @@ export const make = Effect.gen(function* () {
15911592
instance.environment,
15921593
hostEnvironment,
15931594
).pipe(Effect.provideService(HostProcess.HomeDirectory, homeDir));
1594-
homePath = resolvePiSessionsDir(environment, homeDir, path);
1595+
homePath = resolvePiSessionsDir(
1596+
environment,
1597+
piHome(environment, homeDir, platform),
1598+
path,
1599+
);
15951600
} else if (source === "claudeAgent") {
15961601
const config = decodeClaudeSettings(instance.config ?? {});
15971602
if (Option.isNone(config)) continue;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export const piUsageReader: ProviderUsageReader<PiSettings, Path.Path> = {
133133
const path = yield* Path.Path;
134134
const home = yield* HostProcess.HomeDirectory;
135135
const platform = yield* HostProcess.Platform;
136-
const launchHome = (platform === "win32" ? environment.USERPROFILE : environment.HOME) || home;
136+
const launchHome = piHome(environment, home, platform);
137137
const sessionDirArg = config === undefined ? undefined : piLaunchSessionDir(config.launchArgs);
138138
const launchDir =
139139
sessionDirArg === undefined ? undefined : expandHomePath(sessionDirArg, launchHome);
@@ -142,7 +142,7 @@ export const piUsageReader: ProviderUsageReader<PiSettings, Path.Path> = {
142142
...(launchDir !== undefined && path.isAbsolute(launchDir)
143143
? [{ dir: path.resolve(launchDir) }]
144144
: []),
145-
{ dir: resolvePiSessionsDir(environment, home, path) },
145+
{ dir: resolvePiSessionsDir(environment, launchHome, path) },
146146
];
147147
}),
148148
};
@@ -160,6 +160,15 @@ function piLaunchSessionDir(launchArgs: string): string | undefined {
160160
return sessionDir;
161161
}
162162

163+
/** The home Pi resolves `~` against: the instance's HOME (USERPROFILE on Windows), else the host's. */
164+
export function piHome(
165+
environment: NodeJS.ProcessEnv,
166+
hostHome: string,
167+
platform: NodeJS.Platform,
168+
): string {
169+
return (platform === "win32" ? environment.USERPROFILE : environment.HOME) || hostHome;
170+
}
171+
163172
/** Where Pi stores session JSONL files for a provider environment, matching Pi's own lookup. */
164173
export function resolvePiSessionsDir(
165174
environment: NodeJS.ProcessEnv,

0 commit comments

Comments
 (0)