Skip to content

fix(home): resolve user home via getUserHome everywhere for Windows - #309

Merged
jeff-r2026 merged 1 commit into
mainfrom
worktree-fix-windows-home
Aug 24, 2026
Merged

jeff-r2026 merged 1 commit into
mainfrom
worktree-fix-windows-home

Conversation

@jeff-r2026

Copy link
Copy Markdown
Collaborator

Problem

Reported by @MyNameisPI on #301: on a normal Windows PowerShell session, teamai init git@… fails with

Local config saved to undefined/.teamai/config.yaml
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

Windows commonly exposes only USERPROFILE, not HOME. #301 added getUserHome() (HOME → USERPROFILE → os.homedir()) but only wired it into types.ts. Every other module still read a bare process.env.HOME, which on Windows is undefined — ?? '' silently degrades to a relative path, and a non-null assertion crashes.

Fix

  • Route every remaining home lookup through getUserHome() (30 source files).
  • getUserHome() now always returns a non-empty path, so drop the dead if (!home) guards it made unreachable, and remove a stale Chinese error string in uninstall.ts (violated the English-output rule).
  • Fix getVotesLocalDir() to build its path with path.join instead of a hardcoded / separator (also Windows-incorrect).
  • Add a guard test that fails if any source file outside utils/home.ts reads process.env.HOME directly, preventing regressions.
  • Add a USERPROFILE-fallback regression test.

Test Plan

  • npx tsc --noEmit — clean
  • npx vitest run — 2140 passed (159 files)
  • npm run build — success
  • Guard test confirms no bare process.env.HOME remains in production code
  • E2E: ran built CLI with HOME unset and only USERPROFILE set — teamai doctor runs and resolves ~/.teamai/... correctly instead of crashing

🤖 Generated with Claude Code

PR #301 introduced getUserHome() (HOME → USERPROFILE → os.homedir) but only
wired it into types.ts. The rest of the codebase still read a bare
`process.env.HOME`, so on a normal Windows session (where only USERPROFILE is
set) `teamai init` printed "Local config saved to undefined/..." and then
crashed with ERR_INVALID_ARG_TYPE — the exact failure reported on #301.

Route every remaining home lookup through getUserHome(). Because it now always
returns a non-empty path, drop the dead `if (!home)` guards it made
unreachable (including one in uninstall.ts that used to abort the whole command
when HOME was unset — i.e. uninstall was fully broken on Windows). Fix
getVotesLocalDir(), the import/recall/search-index paths and the debug-log path
to build with path.join instead of a hardcoded "/" separator.

Harden getUserHome() with a final os.tmpdir() fallback: os.homedir() is
documented to return '' when the home can't be resolved, which would make every
derived path cwd-relative; the guarantee of a non-empty absolute path is what
lets the callers above drop their guards safely.

Add a guard test that fails if any source file outside utils/home.ts reads
process.env.HOME directly, plus USERPROFILE- and tmpdir-fallback regression
tests.

Co-Authored-By: Claude Fable 5 <[email protected]>
@jeff-r2026
jeff-r2026 force-pushed the worktree-fix-windows-home branch from 0f793d3 to d8ad12d Compare August 24, 2026 07:11
@jeff-r2026

Copy link
Copy Markdown
Collaborator Author

Ran an adversarial self-review across all 30 changed files (semantic-equivalence + mechanical-correctness passes). Both came back clean — every site is either behavior-preserving or a genuine bug fix. Pushed one hardening change on top:

  • getUserHome() now ends with an os.tmpdir() fallback. os.homedir() is documented to return '' when the home can't be resolved (passwd-less uid, env -i), which would make callers' path.join(getUserHome(), '.teamai', …) degrade to a cwd-relative path. Guaranteeing a non-empty absolute return is precisely what lets the removed if (!home) guards be dropped safely. Added a regression test for it.

Verification: tsc --noEmit clean · 2141 unit tests · 64 e2e tests · guard test confirms zero bare process.env.HOME in src/ outside utils/home.ts · manually ran the built CLI with HOME unset + only USERPROFILE set (doctor resolves ~/.teamai/... correctly, no undefined, no crash).

@jeff-r2026
jeff-r2026 merged commit 0a6ae13 into main Aug 24, 2026
5 checks passed
@jeff-r2026
jeff-r2026 deleted the worktree-fix-windows-home branch August 24, 2026 07:13
jeff-r2026 added a commit that referenced this pull request Aug 24, 2026
…ome (#310)

Follow-up to #309. That PR ended getUserHome()'s fallback chain with
`|| os.tmpdir()` to guarantee a non-empty path. But callers join the result
onto `.teamai/...` to write credentials (api-key.ts) and to resolve and execute
a node binary (builtin-hooks.ts resolveCodebuddyNode/resolveWorkbuddyNode), so
falling back to a world-writable shared directory is a code-execution and
credential-exposure vector: an attacker who can write /tmp/.codebuddy-server/.../node
would get their binary run.

Throw when none of HOME, USERPROFILE, or os.homedir() resolves instead. The
empty-string case os.tmpdir() was guarding against (cwd-relative paths) is also
covered by throwing. On any real machine at least one of the three is set, so
this path is only reached in pathological contexts (passwd-less uid, `env -i`)
where refusing to run is the correct, safe behavior.

Update the regression test to assert the throw.

Co-authored-by: Claude Fable 5 <[email protected]>
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…encent#309)

PR Tencent#301 introduced getUserHome() (HOME → USERPROFILE → os.homedir) but only
wired it into types.ts. The rest of the codebase still read a bare
`process.env.HOME`, so on a normal Windows session (where only USERPROFILE is
set) `teamai init` printed "Local config saved to undefined/..." and then
crashed with ERR_INVALID_ARG_TYPE — the exact failure reported on Tencent#301.

Route every remaining home lookup through getUserHome(). Because it now always
returns a non-empty path, drop the dead `if (!home)` guards it made
unreachable (including one in uninstall.ts that used to abort the whole command
when HOME was unset — i.e. uninstall was fully broken on Windows). Fix
getVotesLocalDir(), the import/recall/search-index paths and the debug-log path
to build with path.join instead of a hardcoded "/" separator.

Harden getUserHome() with a final os.tmpdir() fallback: os.homedir() is
documented to return '' when the home can't be resolved, which would make every
derived path cwd-relative; the guarantee of a non-empty absolute path is what
lets the callers above drop their guards safely.

Add a guard test that fails if any source file outside utils/home.ts reads
process.env.HOME directly, plus USERPROFILE- and tmpdir-fallback regression
tests.

Co-authored-by: Claude Fable 5 <[email protected]>
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…ome (Tencent#310)

Follow-up to Tencent#309. That PR ended getUserHome()'s fallback chain with
`|| os.tmpdir()` to guarantee a non-empty path. But callers join the result
onto `.teamai/...` to write credentials (api-key.ts) and to resolve and execute
a node binary (builtin-hooks.ts resolveCodebuddyNode/resolveWorkbuddyNode), so
falling back to a world-writable shared directory is a code-execution and
credential-exposure vector: an attacker who can write /tmp/.codebuddy-server/.../node
would get their binary run.

Throw when none of HOME, USERPROFILE, or os.homedir() resolves instead. The
empty-string case os.tmpdir() was guarding against (cwd-relative paths) is also
covered by throwing. On any real machine at least one of the three is set, so
this path is only reached in pathological contexts (passwd-less uid, `env -i`)
where refusing to run is the correct, safe behavior.

Update the regression test to assert the throw.

Co-authored-by: Claude Fable 5 <[email protected]>
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