← Back to PRs

#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]

by tsukhani open 2026-02-02 17:17 View on GitHub →
channel: telegram extensions: memory-lancedb scripts agents
## 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