#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]
channel: telegram
extensions: memory-lancedb
scripts
agents
Cluster:
UI Enhancements and Fixes
## Summary
Plugin hooks (`before_agent_start`, `agent_end`) were passing `"agent"` as the `agentId` in hook context instead of the actual agent ID (e.g. `"main"`, `"nisha"`).
## Root Cause
Session keys follow the format `agent:<agentId>:<channel>:...`. The code used:
```typescript
params.sessionKey?.split(":")[0] ?? "main"
```
`split(":")[0]` always returns `"agent"` (the literal prefix), not the actual agent ID at index `[1]`.
## Fix
Replace with the existing `resolveAgentIdFromSessionKey()` utility (from `src/routing/session-key.ts`) which correctly parses the session key format.
## Impact
This broke per-agent memory namespacing for any plugin using `ctx.agentId` in hooks. For example, `memory-lancedb` was:
- **Auto-recalling** from namespace `"agent"` instead of `"main"`
- **Auto-capturing** to namespace `"agent"` instead of `"main"`
## Changes
- `src/agents/pi-embedded-runner/run/attempt.ts`: 1 import added, 2 lines fixed
Fixes #7300
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes incorrect `agentId` extraction in hook contexts by switching from `sessionKey.split(":")[0]` (which always yields the `agent` prefix) to `resolveAgentIdFromSessionKey()`. In addition, it expands the plugin hook surface to include an `agent_bootstrap` hook (and wires it into bootstrap processing), and introduces broader changes around session storage (per-session metadata files + background syncing), browser CDP connection caching, cron `force` behavior, and the bundled `session-memory` hook gaining a LanceDB/Gateway target.
The primary correctness fix (agentId parsing) matches the described root cause. The larger set of changes introduces a few areas where concurrency/path handling can lead to silent misrouting or lost updates (session store), and where configuration validation is weakened (`hooks` schema passthrough).
<h3>Confidence Score: 2/5</h3>
- This PR fixes a real bug but includes several broader behavioral changes that warrant follow-up before merging.
- While the agentId parsing fix is straightforward, the PR also introduces a new per-session metadata store and changes update semantics in `updateSessionStoreEntry` that can lose concurrent updates and can mis-resolve agentId by regex-parsing filesystem paths. The hook config schema relaxation and direct Gateway API calls in the bundled hook also increase the chance of silent misconfiguration or drift.
- src/config/sessions/store.ts, src/config/sessions/per-session-store.ts, src/config/zod-schema.hooks.ts, src/hooks/bundled/session-memory/handler.ts
<!-- greptile_other_comments_section -->
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#19177: fix: use parseAgentSessionKey instead of fragile split pattern
by El-Patronum · 2026-02-17
87.5%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
82.7%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
82.3%
#10383: feat(hooks): add agentId to hook mappings
by quentintou · 2026-02-06
80.9%
#19422: fix: pass session context to plugin tool hooks in toToolDefinitions
by namabile · 2026-02-17
80.7%
#20336: fix(sessions): resolve transcriptPath using agentId when storePath ...
by Limitless2023 · 2026-02-18
80.6%
#22977: fix(ui): resolve agent names from config in session dropdown
by NikhilGaddam · 2026-02-21
80.3%
#10663: feat(hooks): add agentId support for webhook routing
by nityatrish · 2026-02-06
80.3%
#9800: feat(hooks): allow webhook mappings to route to specific agents via a…
by bogdanClawdy · 2026-02-05
79.8%
#19565: feat: add agent lifecycle hook events (session, message, error)
by tag-assistant · 2026-02-17
79.1%