#18647: fix(agents): guard against undefined paths in injectedFiles
agents
size: XS
Cluster:
Bootstrap and Plugin Fixes
## Summary
- Filter `injectedFiles` to exclude entries with `undefined` or empty `path` before iterating in `buildInjectedWorkspaceFiles`, preventing a `TypeError: Cannot read properties of undefined (reading 'replace')` crash
- Matches the existing guard pattern already applied in `system-prompt.ts` (`validContextFiles` filter at line 603)
- Adds regression test verifying the function handles `undefined` and empty paths without crashing
## Root Cause
When workspace bootstrap files exceed `bootstrapMaxChars`, the bootstrap context builder can produce `EmbeddedContextFile` entries where `path` is `undefined`. The `buildInjectedWorkspaceFiles` function in `system-prompt-report.ts` calls `file.path.replace(/\\/g, "/")` without checking for this, causing a TypeError that crashes **all agents** before any API call.
The same guard was already added to `buildAgentSystemPrompt` in `system-prompt.ts` (the `validContextFiles` filter), but was missing from the report builder.
## Test plan
- [x] Added test: `does not crash when injectedFiles contain entries with undefined path`
- [x] All 5 tests in `system-prompt-report.test.ts` pass
- [x] Verified fix resolves the crash on a live gateway (v2026.2.15) with Gemini agents
Fixes #17947
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds a defensive filter in `buildInjectedWorkspaceFiles` (`system-prompt-report.ts`) to exclude `EmbeddedContextFile` entries with `undefined` or empty `path` before iterating. This prevents a `TypeError` crash when `file.path.replace(/\\\\/g, "/")` is called on an undefined path — a scenario that occurs when workspace bootstrap files exceed `bootstrapMaxChars`.
- Mirrors the existing `validContextFiles` guard already present in `system-prompt.ts:603`
- Includes a regression test covering both `undefined` and empty-string path cases
- Minimal, focused change that addresses a real production crash
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it adds a defensive filter that prevents a runtime crash without changing any existing behavior for valid inputs.
- The change is minimal, well-scoped, and follows an established pattern already used elsewhere in the codebase (system-prompt.ts:603). It only filters out invalid entries that would cause a TypeError, and includes a regression test. No risk of breaking existing functionality.
- No files require special attention.
<sub>Last reviewed commit: 8ea6a4e</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#22723: fix: guard against undefined path in buildInjectedWorkspaceFiles — ...
by Fratua · 2026-02-21
86.5%
#22786: fix: guard against undefined file.path in system-prompt-report
by miloudbelarebia · 2026-02-21
83.7%
#21954: Prevent bootstrap hook truncation crash in prompt report
by graysurf · 2026-02-20
82.5%
#20851: fix: harden resolveUserPath and compact against undefined workspaceDir
by davidrudduck · 2026-02-19
82.0%
#12956: fix: guard .trim() calls on potentially undefined workspaceDir
by omair445 · 2026-02-10
79.3%
#7569: fix: add null checks to prevent TypeError in subagent spawn
by kaigritun · 2026-02-03
78.6%
#22424: fix: prevent crash when onUpdate is truthy but not callable (fixes ...
by mcaxtr · 2026-02-21
77.6%
#7085: test: skip flaky workspace-paths & safe-bins tests on non-Linux/CI ...
by ThinkIbrokeIt · 2026-02-02
77.2%
#15999: fix: handle null/undefined in activity feed filters
by pamnlambert · 2026-02-14
76.2%
#18997: fix: improve context overflow error messages and docs
by realhoratiobot · 2026-02-17
76.2%