#19177: fix: use parseAgentSessionKey instead of fragile split pattern
agents
size: XS
Cluster:
UI Enhancements and Fixes
## Summary
- Replace `params.sessionKey?.split(":")[0]` with `parseAgentSessionKey(params.sessionKey)?.agentId` in two hook contexts
- The split pattern silently returns the wrong agentId for agent session keys (e.g. `"agent:mybot:abc"` yields `"agent"` instead of `"mybot"`)
- The `parseAgentSessionKey()` utility in `src/sessions/session-key-utils.ts` already handles this correctly
## Files changed (2 files, ~4 lines each)
- `src/agents/pi-embedded-runner/compact.ts` — `before_compaction` hook context
- `src/auto-reply/reply/commands-core.ts` — `before_reset` hook context
## Test plan
- [x] `pnpm build` passes
- [x] `pnpm check` (lint + format) passes
- [x] `pnpm test` — no new failures (pre-existing failures unrelated to this change)
- [ ] Verify hooks fire with correct agentId for subagent sessions
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Correct bug fix that replaces a broken `sessionKey?.split(":")[0]` pattern with the existing `parseAgentSessionKey()` utility in two hook contexts (`before_compaction` and `before_reset`).
- The old `.split(":")[0]` pattern always extracted the literal `"agent"` prefix from session keys like `"agent:mybot:abc"`, rather than the actual agent ID (`"mybot"`). This meant hooks always received `"agent"` as the `agentId`, which is wrong.
- `parseAgentSessionKey()` from `src/sessions/session-key-utils.ts` correctly returns the second colon-delimited segment as `agentId`, and returns `null` for non-agent or malformed keys (falling back to `"main"` via `?? "main"`).
- No remaining instances of the fragile split pattern exist in the codebase. All other call sites already use `parseAgentSessionKey` or `resolveSessionAgentId`.
- The change is minimal, well-scoped, and introduces no new behavior beyond the bug fix.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it's a straightforward, correct bug fix with no risk of regression.
- The change is minimal (2 lines changed + 2 import additions), uses an existing well-tested utility function, and fixes a clear bug where the agentId was always incorrectly set to the literal string "agent". No behavioral change for the main agent case (both old and new code produce "main" as fallback). No other instances of the broken pattern remain.
- No files require special attention.
<sub>Last reviewed commit: 221230b</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
#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]
by tsukhani · 2026-02-02
87.5%
#22977: fix(ui): resolve agent names from config in session dropdown
by NikhilGaddam · 2026-02-21
82.5%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
81.5%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
80.3%
#19422: fix: pass session context to plugin tool hooks in toToolDefinitions
by namabile · 2026-02-17
80.0%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
80.0%
#22098: fix: isolate agent sessions by explicit --session-id
by AIflow-Labs · 2026-02-20
79.3%
#17788: fix: correct config path in sessions_spawn error hint (v2)
by Limitless2023 · 2026-02-16
79.1%
#19412: fix(status): prefer configured contextTokens over session entry
by rafaelipuente · 2026-02-17
78.9%
#16915: fix: await compaction hooks with timeout to prevent cross-session d...
by maximalmargin · 2026-02-15
78.5%