#22705: fix(agents): merge before_agent_start hook systemPrompt into session system prompt
channel: msteams
agents
size: S
Previously, the before_agent_start hook's systemPrompt return value was collected but never actually merged into the session's system prompt. This prevented extensions like memory-flex from injecting user profiles and memories into the AI model's system context.
Changes:
- Change systemPromptText from const to let to allow modification
- Call before_agent_start hook before createAgentSession
- Merge hookResult.systemPrompt into systemPromptText if provided
- Add debug logging for successful systemPrompt injection
- Add error handling for hook failures
This enables memory-flex and other extensions to inject context into the system prompt that affects model behavior without appearing in the user-visible message list.
Fixes the issue where before_agent_start hook's systemPrompt was ignored, requiring post-build patching scripts for bundled versions.
## Change Type
- [x] Bug fix
## Linked Issue/PR
- Closes #8176
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a bug where the `before_agent_start` hook's `systemPrompt` return value was collected but never merged into the session's system prompt. The fix moves hook execution to before `createAgentSession` and properly merges the returned `systemPrompt` into `systemPromptText`.
**Key changes:**
- Moved `before_agent_start` hook call to execute before session creation (previously ran after)
- Changed `systemPromptText` from `const` to `let` to allow modification
- Added string concatenation logic to merge hook's `systemPrompt` into `systemPromptText`
- Cached hook result to avoid duplicate execution later in the flow
- Added debug logging and error handling for hook failures
**Test changes:**
- Updated MS Teams test to use `pathToFileURL` for Windows compatibility
- Made optional `sharePointSiteId` parameter explicit in test
The implementation correctly ensures that extensions like memory-flex can now inject context into the system prompt that affects model behavior.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The changes are well-contained and follow a clear refactoring pattern: moving hook execution earlier in the flow and properly utilizing its return value. The logic is straightforward (changing const to let, adding string concatenation, caching results), includes proper error handling, and the test changes are benign compatibility improvements. No breaking changes or risky operations introduced.
- No files require special attention.
<sub>Last reviewed commit: b16700b</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#14602: fix(plugins): hook systemPrompt gets collected then thrown away (#1...
by yinghaosang · 2026-02-12
85.3%
#11921: feat(hooks): support systemPrompt injection in before_agent_start hook
by jungdaesuh · 2026-02-08
81.7%
#6017: feat(hooks): add systemPrompt and tools to before_agent_start event
by yajatns · 2026-02-01
80.7%
#11732: feat(plugins): add injectMessages to before_agent_start hook
by antra-tess · 2026-02-08
78.4%
#7301: fix(hooks): use resolveAgentIdFromSessionKey instead of split(":")[0]
by tsukhani · 2026-02-02
77.5%
#19177: fix: use parseAgentSessionKey instead of fragile split pattern
by El-Patronum · 2026-02-17
77.5%
#8178: feat: allow before_agent_start to override the prompt
by richardfogaca · 2026-02-03
77.3%
#7091: feat: add pre-answer hooks system for automatic context injection
by dizhaky · 2026-02-02
77.0%
#21954: Prevent bootstrap hook truncation crash in prompt report
by graysurf · 2026-02-20
76.4%
#10327: Fix: persist original prompt to transcript, not plugin-modified pro...
by GodsBoy · 2026-02-06
75.8%