Repository navigation
fix(server): Pi usage and session import read the instance's home - #34726
Conversation
Reproduces #34717: with HOME set on the Pi provider instance, the default sessions directory and ~/ forms of PI_CODING_AGENT_DIR and PI_CODING_AGENT_SESSION_DIR resolve against the server's home. AI-assisted; verified with the tests and tools listed above.
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.
ApprovabilityVerdict: Approved at Macroscope's review found this PR approvable — Pi usage and session discovery now use the configured instance home consistently, including Windows USERPROFILE, with the existing host-home fallback. This is a bounded local path-resolution correction without destructive or permission-changing effects. You can add or adjust custom eligibility rules. Learn more. |
Thread transfer impact✅ Thread transfer remains within every enforced ceiling.
Baseline: Scenario and decoded snapshot size10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Pi resolves
~against the HOME (USERPROFILE on win32) in its own process environment. piUsageReader.directories computed that instance home but only used it for a --session-dir launch argument, so it passed HostProcess.HomeDirectory to resolvePiSessionsDir. As a result, the default ~/.pi/agent/sessions and/ forms of PI_CODING_AGENT_DIR and PI_CODING_AGENT_SESSION_DIR resolved against the server's home. AgentSessionScanner's Pi terminal-session import had the same bug, since it called resolvePiSessionsDir with the server homeDir. The fix adds a small exported helper, piHome(environment, hostHome, platform), in packages/provider-pi/src/server/usage.ts, which returns the instance's HOME/USERPROFILE and falls back to the host home. Both the usage reader and the scanner now pass that home to resolvePiSessionsDir. The environment the scanner gets already merges the host environment, so when the instance sets no HOME it falls back to the host's HOME as before. The repro branch's three UsageService rows now pass. I added one instance (HOME plus PI_CODING_AGENT_DIR=/custom-pi) to the scanner's existing 'honors provider instance home directory environment variables' test; it fails on base and passes with the fix. No new regex or parser.Fixes #34717
Reproduction on main
Commit origin/main 7462f23. Started an isolated dev server (T3CODE_DEV_SEED=0 vp run dev) whose settings.json gave the Pi provider instance one environment variable, HOME=/instance-home, with no launch arguments and no PI_CODING_AGENT_* variables. The server process's own home has no
/.pi directory. Wrote one synthetic Pi session JSONL (session entry plus an assistant message with usage input 34717 and output 100, dated today) to /.pi/agent/sessions/proj/s.jsonl. Opened Usage > Tokens > 30 days in the web client: there was no Pi row, only providers from the server's home. Control: added PI_CODING_AGENT_SESSION_DIR=/.pi/agent/sessions (absolute) to the same instance and reloaded Usage: Pi appeared with 1 session and 34.8K tokens. So the session file is valid and only the home used to resolve the path is wrong. Also added three rows to the existing 'discovers Pi history in its default home and directory overrides' table in apps/server/src/usage/UsageService.test.ts (instance HOME with the default directory, with PI_CODING_AGENT_DIR=/custom-pi, and with PI_CODING_AGENT_SESSION_DIR=~/custom-sessions). Each fails on main, e.g. expected '/pi-home/.pi/agent/sessions', received '/.pi/agent/sessions'.Evidence
Configure Pi with only an instance HOME override. Write one synthetic session under that home's default Pi sessions directory with 34,717 input and 100 output tokens, dated today. Open Usage > Tokens > 30 days, filter to Pi, and wait for the scan to finish. Both captures use Chromium at 1280×800, light theme, and fresh isolated state.
Scenario and synthetic seed · Before recording · After recording
scenarios:prove exited 0: failsOnBase=true, passesOnHead=true. Captures were visually inspected.
AI-assisted; verified with the tests and tools listed above.
Not shown:
Tests
vp test run src/usage/UsageService.test.ts -t 'discovers Pi history' with the fix stashed: 1 failed (repro confirmed). vp test run src/usage/UsageService.test.ts src/project/AgentSessionScanner.test.ts with the fix: 120 passed. The new scanner row with the fix stashed: failed (missing pi-home workspace); with the fix: passed. vp run check:changed: format, lint, knip, graphql, typecheck (9 packages) and tests (3 files, 122 tests) all passed.
Not in this change
~against the server home rather than an instance HOME; not part of this Pi report.AI-assisted; verified with the tests and tools listed above.