← Back to PRs

#8471: fix(subagent): add defensive checks for undefined string fields

by adam-smeth open 2026-02-04 02:36 View on GitHub →
agents stale
## Problem `sessions_spawn` calls fail with: ``` TypeError: Cannot read properties of undefined (reading 'trim') ``` This happens immediately with `runtime 0s`, suggesting the issue is in session key handling before the agent run actually starts. ## Root Cause Several functions call `.trim()` on string parameters without nullish checks: - `resolveRequesterStoreKey()` - `requesterSessionKey.trim()` - `listSubagentRunsForRequester()` - `requesterSessionKey.trim()` While these have TypeScript types declaring the parameter as `string`, at runtime the value could be undefined from: - Corrupted persisted data in `runs.json` - Edge cases in optional chaining paths - Cross-process RPC data that isn't validated ## Solution 1. **Added nullish coalescing** to `.trim()` calls: ```typescript const raw = (requesterSessionKey ?? "").trim(); ``` 2. **Added validation** when loading subagent registry from disk to skip entries with missing required string fields (`childSessionKey`, `requesterSessionKey`, `requesterDisplayKey`, `task`) ## Changes - `src/agents/subagent-announce.ts`: Add `?? ""` guard in `resolveRequesterStoreKey()` - `src/agents/subagent-registry.ts`: Add `?? ""` guard in `listSubagentRunsForRequester()` - `src/agents/subagent-registry.store.ts`: Validate required fields when loading from disk Fixes #8445 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR hardens subagent session-key handling and registry restore logic to prevent crashes when persisted or RPC-provided fields are unexpectedly `undefined` at runtime. - `resolveRequesterStoreKey` and `listSubagentRunsForRequester` now guard `.trim()` calls with nullish coalescing to avoid `TypeError` during `sessions_spawn` and status/listing flows. - `loadSubagentRegistryFromDisk` now skips corrupted/partial registry entries missing required string fields (`childSessionKey`, `requesterSessionKey`, `requesterDisplayKey`, `task`) so restore/resume paths don’t blow up on malformed `runs.json`. These changes fit into the existing subagent lifecycle/persistence system by making the restore and query surface resilient to invalid stored data while keeping in-memory types unchanged. <h3>Confidence Score: 4/5</h3> - This PR is low-risk and primarily adds defensive guards, but there’s a small behavior-consistency concern around key normalization when filtering runs. - Changes are localized and aimed at preventing a known runtime exception (`.trim()` on undefined) and skipping malformed persisted records. The main remaining risk is subtle behavior mismatch: trimming only the query key may cause legitimate runs with whitespace in stored keys to be unlistable, which can be hard to diagnose. - src/agents/subagent-registry.ts (key normalization/filter semantics) <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs