Skip to content

fix(pi): point pi at config dir via PI_CODING_AGENT_DIR instead of redirecting HOME#243

Open
dgokeeffe wants to merge 1 commit into
databricks:mainfrom
dgokeeffe:fix/pi-config-dir-env
Open

fix(pi): point pi at config dir via PI_CODING_AGENT_DIR instead of redirecting HOME#243
dgokeeffe wants to merge 1 commit into
databricks:mainfrom
dgokeeffe:fix/pi-config-dir-env

Conversation

@dgokeeffe

Copy link
Copy Markdown

Problem

Pi redirected $HOME to APP_DIR/pi-home to isolate its config (~/.pi/agent), but this broke macOS keychain default resolution under ucode. The Security framework looks for the login keychain under the redirected HOME, finds none, and security default-keychain returns "A default keychain could not be found". As a result gh auth, the git credential helper, and any keychain-backed tool failed inside pi.

Root cause

Pi does honor a config-dir env var. From the installed package (config.js):

export const ENV_AGENT_DIR = `${APP_NAME.toUpperCase()}_CODING_AGENT_DIR`; // → PI_CODING_AGENT_DIR
export function getAgentDir() {
    const envDir = process.env[ENV_AGENT_DIR];
    if (envDir) return expandTildePath(envDir);          // env wins
    return join(homedir(), CONFIG_DIR_NAME, "agent");     // else ~/.pi/agent
}

So the $HOME redirect was unnecessary — ucode can isolate pi's config the same way it isolates opencode (XDG_CONFIG_HOME) and gemini (GEMINI_CLI_HOME): by pointing the agent at a config dir via env var, without touching $HOME.

Fix

build_runtime_env now sets PI_CODING_AGENT_DIR = PI_CONFIG_DIR instead of HOME = PI_UCODE_HOME:

 def build_runtime_env(token: str) -> dict[str, str]:
     env = os.environ.copy()
     env["OAUTH_TOKEN"] = token
-    env["HOME"] = str(PI_UCODE_HOME)
+    env["PI_CODING_AGENT_DIR"] = str(PI_CONFIG_DIR)
     return env

Config isolation is preserved — models.json, settings.json, and sessions still land under APP_DIR/pi-home/.pi/agent (the existing PI_CONFIG_DIR). But $HOME stays the user's real home, so the login keychain stays discoverable and gh/git credential helper/keychain-backed tools work unmodified.

Test changes

The two pi e2e sites monkeypatched PI_UCODE_HOME/PI_CONFIG_PATH to redirect pi at a tmp home. They now also patch:

  • PI_CONFIG_DIR — read by build_runtime_env to set the env var
  • PI_SETTINGS_PATH / PI_SETTINGS_BACKUP_PATH — previously masked because the HOME redirect made pi read settings from the un-patched real APP_DIR path; without the redirect, _write_settings would write to the real path while pi reads from the tmp PI_CONFIG_DIR

The pi unit test assertion (env["HOME"] == ...env["PI_CODING_AGENT_DIR"] == ...) is updated to match.

Verification

  • uv run pytest tests/test_agent_pi.py tests/test_e2e_user_agent.py::TestPiUserAgent — 42 passed
  • uv run pytest tests/ -q (full unit suite, e2e excluded) — 943 passed
  • uv run ruff check on changed files — clean
  • Confirmed PI_CODING_AGENT_DIR is respected by inspecting the installed pi package source and by running pi --list-models with the env var set to a throwaway config dir (pi reported loading <dir>/models.json)

…directing HOME

Pi honors the PI_CODING_AGENT_DIR env var to resolve its config
directory (~/.pi/agent), so redirecting /Users/david.okeeffe to APP_DIR/pi-home was
unnecessary. The HOME redirect broke macOS keychain default resolution
under ucode: the Security framework looks for the login keychain under
the redirected HOME, finds none, and security default-keychain returns
'A default keychain could not be found'. As a result gh auth, the git
credential helper, and any keychain-backed tool failed inside pi.

Setting PI_CODING_AGENT_DIR to the existing PI_CONFIG_DIR preserves
config isolation (models.json/settings.json/sessions still land under
APP_DIR/pi-home/.pi/agent) while leaving /Users/david.okeeffe as the user's real home,
so the login keychain stays discoverable.

Tests: the two pi e2e sites monkeypatched PI_UCODE_HOME/PI_CONFIG_PATH
to redirect pi at a tmp home. They now also patch PI_CONFIG_DIR (read by
build_runtime_env) and PI_SETTINGS_PATH/PI_SETTINGS_BACKUP_PATH (previously
masked because the HOME redirect made pi read settings from the un-patched
real APP_DIR path).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant