← Back to PRs

#20851: fix: harden resolveUserPath and compact against undefined workspaceDir

by davidrudduck open 2026-02-19 10:55 View on GitHub →
agents size: XS trusted-contributor
## Summary - Guard `resolveUserPath()` against `undefined`/`null` input — previously crashed with `Cannot read properties of undefined (reading 'trim')` - Update `compactEmbeddedPiSessionDirect()` to use `resolveRunWorkspaceDir()` instead of calling `resolveUserPath()` directly with a potentially undefined `params.workspaceDir` ## Root cause Sub-agent compaction passes `params.workspaceDir` to `resolveUserPath()`, but `workspaceDir` can be `undefined` when the session was created without an explicit workspace. The `.trim()` call on line 241 of `utils.ts` crashes on `undefined`. ## Changes - `src/utils.ts`: Early return `""` for falsy input in `resolveUserPath()` - `src/agents/pi-embedded-runner/compact.ts`: Use `resolveRunWorkspaceDir()` for proper workspace resolution (consistent with `runEmbeddedAttempt`) - `src/utils.test.ts`: Test covering `undefined` and `null` inputs ## Test plan - [x] `pnpm build` passes - [x] `pnpm test src/utils.test.ts` — 26 tests pass, including new undefined/null guard test - [x] Rebased on latest `main`, no conflicts --- _Re-opened from #12172 (auto-closed during fork maintenance)._ <!-- greptile_comment --> <h3>Greptile Summary</h3> Fixed a crash in `resolveUserPath()` when passed `undefined` or `null` values. The function previously crashed with "Cannot read properties of undefined (reading 'trim')" when `workspaceDir` was undefined during sub-agent compaction. The PR adds an early return for falsy inputs and updates `compactEmbeddedPiSessionDirect()` to use `resolveRunWorkspaceDir()` for proper workspace resolution with fallback handling, matching the pattern already used in `runEmbeddedAttempt()`. Includes test coverage for the undefined/null guard. <h3>Confidence Score: 5/5</h3> - Safe to merge with no risk - defensive fix with proper test coverage - The changes are defensive programming improvements that prevent crashes. The fix in `resolveUserPath()` adds a simple guard against falsy inputs, and the update to use `resolveRunWorkspaceDir()` aligns with existing patterns in the codebase. Tests confirm the fix works correctly, and the changes make the code more robust without altering intended behavior. - No files require special attention <sub>Last reviewed commit: e7298f3</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs