From 32e4beffd9fd1a00b5d84ae5020ea6c7f5257585 Mon Sep 17 00:00:00 2001 From: Kailigithub <12250313+Kailigithub@users.noreply.github.com> Date: Fri, 31 Jul 2026 03:08:12 +0800 Subject: [PATCH] fix(agent_loop): apply agent_before hook return value to context (closes #537) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- agent_loop.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/agent_loop.py b/agent_loop.py index a744f0d26..494db9f5c 100644 --- a/agent_loop.py +++ b/agent_loop.py @@ -46,7 +46,14 @@ 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()) + if isinstance(_ctx, dict): + if 'system_prompt' in _ctx: + messages[0]['content'] = _ctx['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} ...'