From 3722544c007c27250067588e3221c23fa603ff37 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 20 Jun 2026 17:09:00 -0400 Subject: [PATCH] fix(ai_client): add 'global' declarations to _set_tool_preset_result Bug: Phase 11 sites 5+6 migration extracted _set_tool_preset_result and _set_bias_profile_result helpers. The _set_tool_preset_result helper modifies _active_tool_preset, _tool_approval_modes, _agent_tools without declaring them as global, which causes the assignments to create LOCAL variables instead of modifying the module-level globals. This regression broke tests/test_bias_integration.py::test_set_tool_preset_with_objects: preset = ToolPreset(name='ObjTest', categories={'General': [Tool(name='read_file', approval='auto')]}) with patch('src.tool_presets.ToolPresetManager.load_all', return_value={'ObjTest': preset}): ai_client.set_tool_preset('ObjTest') assert ai_client._agent_tools['read_file'] is True # Fails: KeyError 'read_file' (the helper created a local _agent_tools, # not modifying the module global; set_tool_preset legacy then ran # cache-invalidation but never assigned _agent_tools to the test's view) Fix: Add 'global _active_tool_preset, _tool_approval_modes, _agent_tools' declaration to _set_tool_preset_result. The original set_tool_preset had this declaration at the top; the helper extraction lost it. Audit: no audit change (the helper still classifies as BOUNDARY_CONVERSION via Heuristic A 'returns Result' pattern). --- src/ai_client.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ai_client.py b/src/ai_client.py index 0db21274..83603196 100644 --- a/src/ai_client.py +++ b/src/ai_client.py @@ -524,7 +524,12 @@ def _set_tool_preset_result(preset_name: Optional[str]) -> Result[None]: capturing the original exception. The legacy caller (set_tool_preset) calls this helper for the load step; on Result errors, the caller still completes (state remains partially-set; the cache invalidation runs). + + IMPORTANT: This function MODIFIES module-level globals (_active_tool_preset, + _tool_approval_modes, _agent_tools). Without 'global' declarations, the + assignments would create local variables that are discarded on return. """ + global _active_tool_preset, _tool_approval_modes, _agent_tools if not preset_name or preset_name == "None": return Result(data=None) try: