3.0 KiB
Plan: GUI Architecture Refinement & AI-Friendliness
Track ID: gui_architecture_refinement_20260512 Status: [~] Draft
Objective
Reduce nesting and improve compactness of ImGui code in gui_2.py to make it more AI-friendly. Formalize the "defer/scope" patterns (inspired by Go's defer and Ryan Fleury's macros) in the project style guides to prevent PopID / End leaks.
Background & Motivation
The main GUI render loop (_gui_func__abusrd_try_scope) has grown to over 600 lines with deep nesting. Raw imgui.begin() and imgui.end() calls are prone to leaks if an early return occurs or if the return value of begin is ignored. While imscope context managers solve the leak issue, they still introduce nesting. We need a way to keep the code extremely flat (0-1 levels of nesting) while maintaining safety.
Proposed Solution
1. Update Style Guides (python.md & workflow.md)
Introduce a new section explicitly defining the "ImGui Defer Patterns":
- The Context Manager Pattern: Use
with imscope.window("Name"):to automatically handleEnd(). This adds only 1 space of indentation (per project rules). - The Flat Dispatch Pattern: To avoid nesting multiple windows, use dispatch helpers like
self._render_window_if_open(name, render_func)which encapsulate the state-checking,Begin,End, and execution logic.
2. Implement Flat Dispatch Helper
Create a helper method in App:
def _render_window_if_open(self, name: str, render_func: Callable[[], None], flag_condition: bool = True) -> None:
if not flag_condition or not self.show_windows.get(name, False): return
with imscope.window(name, self.show_windows[name]) as (exp, opened):
self.show_windows[name] = bool(opened)
if exp: render_func()
3. Refactor gui_2.py
- Extract inline hub definitions (e.g., "Operations Hub", "Discussion Hub", "AI Settings") from
_gui_func__abusrd_try_scopeinto dedicated methods (_render_operations_hub, etc.). - Replace the massive
if self.show_windows.get...blocks in_gui_func__abusrd_try_scopewith a flat sequence of_render_window_if_opencalls. - Rename
_gui_func__abusrd_try_scopeto a cleaner name (e.g.,_gui_func_body) once stabilized.
Implementation Steps
- Edit
conductor/code_styleguides/python.mdto add "ImGui Defer Patterns" under the "AI-Agent Specific Conventions" or "Anti-OOP" section. - Edit
conductor/workflow.mdto reference the mandatory use ofimscopeor dispatch helpers for ImGui code. - Add
_render_window_if_opentogui_2.py. - Extract
_render_operations_hub,_render_discussion_hub, and_render_ai_settings_hubingui_2.py. - Flatten
_gui_func__abusrd_try_scopeusing the new helper.
Verification & Testing
- Ensure the app launches successfully without
PopIDerrors. - Verify that toggling windows via the menu still opens and closes them correctly.
- Run
uv run pytest tests/test_gui_startup_smoke.pyanduv run pytest tests/test_gui_window_controls.py.