fix(agent_loop): apply agent_before hook return value to context (closes #537) - #714
Open
Kailigithub wants to merge 1 commit into
Open
fix(agent_loop): apply agent_before hook return value to context (closes #537)#714Kailigithub wants to merge 1 commit into
Kailigithub wants to merge 1 commit into
Conversation
lsdefine#537) plugins/hooks.py::trigger() already supports returning a dict to mutate agent context, but agent_runner_loop() discarded the return value of _hook('agent_before', ...). Plugins returning updated system_prompt, user_input, or initial_user_content had their changes silently dropped. When the hook returns a dict, apply system_prompt to messages[0] and user_input (or initial_user_content when set) to messages[1]. Backward compatible: hooks returning None or non-dict values leave messages unchanged, so existing read-only hooks (logging, telemetry, langfuse) keep working without modification. Verification: /tmp/test_issue_537.py — 4 cases (system_prompt override, legacy None-return, non-dict return, user_input override). All pass on the fixed code, Test 1 fails on main (messages[0] == 'ORIGINAL prompt' without '[AUGMENTED]' suffix).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
plugins/hooks.py::trigger()already supports returning a dict to mutateagent context (it rebinds
ctx = rwhen a callback returns a dict).But
agent_runner_loop()inagent_loop.pydiscarded the return valueof
_hook('agent_before', locals()). Plugins that wanted to overridesystem_prompt,user_input, orinitial_user_contenthad theirchanges silently dropped.
This is the same class of issue called out in #537.
Fix
Capture the return value of
_hook('agent_before', locals()):system_prompt/user_input/initial_user_contentoverrides to themessageslist.None(the historical contract) or a non-dictvalue, the loop proceeds with the original arguments.
initial_user_contenttakes precedence overuser_input, preservingthe existing argument-resolution order at the top of the function.
Backward compatibility
Existing read-only hooks (
plugins/langfuse_tracing.py::_on_agent_beforereads
user_input;plugins/project_mode.py::inject_project_contextmutates
ctx['messages']in place) keep working without modification:langfuse_tracing._on_agent_beforereturns nothing →_ctxisNone→
isinstance(_ctx, dict)is False → messages unchanged.project_mode.inject_project_contextmutatesctx['messages']inplace and returns nothing → same path as above.
No existing plugin is broken by this change.
Verification
/tmp/test_issue_537.py— 4 standalone tests againstagent_runner_loopusing a stubclientandBaseHandlersubclass:system_promptoverrideNone(legacy contract)user_inputoverrideThree-step dance confirms Test 1 fails on the unfixed code
(
messages[0] == 'ORIGINAL prompt') and passes with the fix(
messages[0] == 'ORIGINAL prompt [AUGMENTED]').ruff check agent_loop.py: 40 pre-existing style findings (compactimports, semicolon-separated statements — intentional per
CONTRIBUTING.md's "Compact and visually uniform"). The fix introduces
zero new ruff findings (verified by
git stash+ re-check).Scope
agent_loop.pytests/(the repo currently has no pytestsuite); verification artifact lives at
/tmp/test_issue_537.pyandis documented above for the maintainer's review.
Closes #537