← Back to PRs

#22705: fix(agents): merge before_agent_start hook systemPrompt into session system prompt

by mushuiyu422 open 2026-02-21 14:55 View on GitHub →
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