From 678bbd8d5fe40dd66a9cb4fa5eaa85c41786af46 Mon Sep 17 00:00:00 2001 From: Kailigithub Date: Wed, 17 Jun 2026 03:06:12 +0800 Subject: [PATCH] fix(agent_loop): apply agent_before hook return value to context The plugin hook system in plugins/hooks.py already supports returning a dict to mutate the agent context, but agent_runner_loop() discarded the return value of _hook('agent_before', ...). This prevented plugins from modifying system_prompt / user_input / initial_user_content. Capture the return value; when a dict is returned, apply its overrides to the messages list. Non-dict returns and missing keys are ignored, so existing hooks that only inspect ctx (e.g. logging/metrics) continue to work without modification. Closes #537 --- agent_loop.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/agent_loop.py b/agent_loop.py index 7afb58507..ee4fdab57 100644 --- a/agent_loop.py +++ b/agent_loop.py @@ -46,7 +46,15 @@ def agent_runner_loop(client, system_prompt, user_input, handler, tools_schema, {"role": "user", "content": initial_user_content if initial_user_content is not None else user_input} ] turn = 0; handler.max_turns = max_turns - _hook('agent_before', locals()) + _ctx = _hook('agent_before', locals()) # plugins may return dict to override system_prompt/user_input + if isinstance(_ctx, dict): + if 'system_prompt' in _ctx: + system_prompt = _ctx['system_prompt'] + messages[0]['content'] = system_prompt + if _ctx.get('initial_user_content') is not None: + messages[1]['content'] = _ctx['initial_user_content'] + elif 'user_input' in _ctx and initial_user_content is None: + messages[1]['content'] = _ctx['user_input'] while turn < handler.max_turns: turn += 1; turnstr = f'LLM Running (Turn {turn}) ...' if handler.parent.task_dir: turnstr = f'Turn {turn} ...'