test(audit): fix critical test suite deadlocks and write exhaustive architectural report
- Fix 'Triple Bingo' history synchronization explosion during streaming - Implement stateless event buffering in ApiHookClient to prevent dropped events - Ensure 'tool_execution' events emit consistently across all LLM providers - Add hard timeouts to all background thread wait() conditions - Add thorough teardown cleanup to conftest.py's reset_ai_client fixture - Write highly detailed report_gemini.md exposing asyncio lifecycle flaws
This commit is contained in:
236
src/gui_2.py
236
src/gui_2.py
@@ -103,6 +103,9 @@ class App:
|
||||
def __init__(self) -> None:
|
||||
# Initialize controller and delegate state
|
||||
self.controller = AppController()
|
||||
# Restore legacy PROVIDERS to controller if needed (it already has it via delegation if set on class level, but let's be explicit)
|
||||
if not hasattr(self.controller, 'PROVIDERS'):
|
||||
self.controller.PROVIDERS = PROVIDERS
|
||||
self.controller.init_state()
|
||||
self.controller.start_services(self)
|
||||
|
||||
@@ -116,55 +119,9 @@ class App:
|
||||
self._pending_dialog_lock = self.controller._pending_dialog_lock
|
||||
self._api_event_queue_lock = self.controller._api_event_queue_lock
|
||||
|
||||
# UI-specific initialization
|
||||
self._init_ui_actions()
|
||||
|
||||
def _init_ui_actions(self) -> None:
|
||||
# Set up UI-specific action maps
|
||||
self._clickable_actions: dict[str, Callable[..., Any]] = {
|
||||
'btn_reset': self._handle_reset_session,
|
||||
'btn_gen_send': self._handle_generate_send,
|
||||
'btn_md_only': self._handle_md_only,
|
||||
'btn_approve_script': self._handle_approve_script,
|
||||
'btn_reject_script': self._handle_reject_script,
|
||||
'btn_project_save': self._cb_project_save,
|
||||
'btn_disc_create': self._cb_disc_create,
|
||||
'btn_mma_plan_epic': self._cb_plan_epic,
|
||||
'btn_mma_accept_tracks': self._cb_accept_tracks,
|
||||
'btn_mma_start_track': self._cb_start_track,
|
||||
'btn_mma_create_track': lambda: self._cb_create_track(self.ui_new_track_name, self.ui_new_track_desc, self.ui_new_track_type),
|
||||
'btn_approve_tool': self._handle_approve_tool,
|
||||
'btn_approve_mma_step': self._handle_approve_mma_step,
|
||||
'btn_approve_spawn': self._handle_approve_spawn,
|
||||
}
|
||||
self._predefined_callbacks: dict[str, Callable[..., Any]] = {
|
||||
'_test_callback_func_write_to_file': self._test_callback_func_write_to_file
|
||||
}
|
||||
self._discussion_names_cache: list[str] = []
|
||||
self._discussion_names_dirty: bool = True
|
||||
|
||||
def _handle_approve_script(self, user_data=None) -> None:
|
||||
"""Approves the currently pending PowerShell script."""
|
||||
with self._pending_dialog_lock:
|
||||
dlg = self._pending_dialog
|
||||
if dlg:
|
||||
with dlg._condition:
|
||||
dlg._approved = True
|
||||
dlg._done = True
|
||||
dlg._condition.notify_all()
|
||||
self._pending_dialog = None
|
||||
|
||||
def _handle_reject_script(self, user_data=None) -> None:
|
||||
"""Rejects the currently pending PowerShell script."""
|
||||
with self._pending_dialog_lock:
|
||||
dlg = self._pending_dialog
|
||||
if dlg:
|
||||
with dlg._condition:
|
||||
dlg._approved = False
|
||||
dlg._done = True
|
||||
dlg._condition.notify_all()
|
||||
self._pending_dialog = None
|
||||
|
||||
def _handle_approve_tool(self, user_data=None) -> None:
|
||||
"""UI-level wrapper for approving a pending tool execution ask."""
|
||||
self._handle_approve_ask()
|
||||
@@ -210,194 +167,9 @@ class App:
|
||||
|
||||
# ---------------------------------------------------------------- logic
|
||||
|
||||
def _process_pending_gui_tasks(self) -> None:
|
||||
if not self._pending_gui_tasks:
|
||||
return
|
||||
with self._pending_gui_tasks_lock:
|
||||
tasks = self._pending_gui_tasks[:]
|
||||
self._pending_gui_tasks.clear()
|
||||
for task in tasks:
|
||||
try:
|
||||
action = task.get("action")
|
||||
if action == "refresh_api_metrics":
|
||||
self._refresh_api_metrics(task.get("payload", {}), md_content=self.last_md or None)
|
||||
elif action == "handle_ai_response":
|
||||
payload = task.get("payload", {})
|
||||
text = payload.get("text", "")
|
||||
stream_id = payload.get("stream_id")
|
||||
is_streaming = payload.get("status") == "streaming..."
|
||||
if stream_id:
|
||||
if is_streaming:
|
||||
if stream_id not in self.mma_streams: self.mma_streams[stream_id] = ""
|
||||
self.mma_streams[stream_id] += text
|
||||
else:
|
||||
self.mma_streams[stream_id] = text
|
||||
if stream_id == "Tier 1":
|
||||
if "status" in payload:
|
||||
self.ai_status = payload["status"]
|
||||
else:
|
||||
if is_streaming:
|
||||
self.ai_response += text
|
||||
else:
|
||||
self.ai_response = text
|
||||
self.ai_status = payload.get("status", "done")
|
||||
self._trigger_blink = True
|
||||
if not stream_id:
|
||||
self._token_stats_dirty = True
|
||||
if self.ui_auto_add_history and not stream_id:
|
||||
role = payload.get("role", "AI")
|
||||
with self._pending_history_adds_lock:
|
||||
self._pending_history_adds.append({
|
||||
"role": role,
|
||||
"content": self.ai_response,
|
||||
"collapsed": False,
|
||||
"ts": project_manager.now_ts()
|
||||
})
|
||||
elif action == "mma_stream_append":
|
||||
payload = task.get("payload", {})
|
||||
stream_id = payload.get("stream_id")
|
||||
text = payload.get("text", "")
|
||||
if stream_id:
|
||||
if stream_id not in self.mma_streams:
|
||||
self.mma_streams[stream_id] = ""
|
||||
self.mma_streams[stream_id] += text
|
||||
elif action == "show_track_proposal":
|
||||
self.proposed_tracks = task.get("payload", [])
|
||||
self._show_track_proposal_modal = True
|
||||
elif action == "mma_state_update":
|
||||
payload = task.get("payload", {})
|
||||
self.mma_status = payload.get("status", "idle")
|
||||
self.active_tier = payload.get("active_tier")
|
||||
self.mma_tier_usage = payload.get("tier_usage", self.mma_tier_usage)
|
||||
self.active_tickets = payload.get("tickets", [])
|
||||
track_data = payload.get("track")
|
||||
if track_data:
|
||||
tickets = []
|
||||
for t_data in self.active_tickets:
|
||||
tickets.append(Ticket(**t_data))
|
||||
self.active_track = Track(
|
||||
id=track_data.get("id"),
|
||||
description=track_data.get("title", ""),
|
||||
tickets=tickets
|
||||
)
|
||||
elif action == "set_value":
|
||||
item = task.get("item")
|
||||
value = task.get("value")
|
||||
if item in self._settable_fields:
|
||||
attr_name = self._settable_fields[item]
|
||||
setattr(self, attr_name, value)
|
||||
if item == "gcli_path":
|
||||
if not ai_client._gemini_cli_adapter:
|
||||
ai_client._gemini_cli_adapter = ai_client.GeminiCliAdapter(binary_path=str(value))
|
||||
else:
|
||||
ai_client._gemini_cli_adapter.binary_path = str(value)
|
||||
elif action == "click":
|
||||
item = task.get("item")
|
||||
user_data = task.get("user_data")
|
||||
if item == "btn_project_new_automated":
|
||||
self._cb_new_project_automated(user_data)
|
||||
elif item == "btn_mma_load_track":
|
||||
self._cb_load_track(str(user_data or ""))
|
||||
elif item in self._clickable_actions:
|
||||
# Check if it's a method that accepts user_data
|
||||
import inspect
|
||||
func = self._clickable_actions[item]
|
||||
try:
|
||||
sig = inspect.signature(func)
|
||||
if 'user_data' in sig.parameters:
|
||||
func(user_data=user_data)
|
||||
else:
|
||||
func()
|
||||
except Exception:
|
||||
func()
|
||||
elif action == "select_list_item":
|
||||
item = task.get("listbox", task.get("item"))
|
||||
value = task.get("item_value", task.get("value"))
|
||||
if item == "disc_listbox":
|
||||
self._switch_discussion(str(value or ""))
|
||||
elif task.get("type") == "ask":
|
||||
self._pending_ask_dialog = True
|
||||
self._ask_request_id = task.get("request_id")
|
||||
self._ask_tool_data = task.get("data", {})
|
||||
elif action == "clear_ask":
|
||||
if self._ask_request_id == task.get("request_id"):
|
||||
self._pending_ask_dialog = False
|
||||
self._ask_request_id = None
|
||||
self._ask_tool_data = None
|
||||
elif action == "custom_callback":
|
||||
cb = task.get("callback")
|
||||
args = task.get("args", [])
|
||||
if callable(cb):
|
||||
try: cb(*args)
|
||||
except Exception as e: print(f"Error in direct custom callback: {e}")
|
||||
elif cb in self._predefined_callbacks:
|
||||
self._predefined_callbacks[cb](*args)
|
||||
elif action == "mma_step_approval":
|
||||
dlg = MMAApprovalDialog(str(task.get("ticket_id") or ""), str(task.get("payload") or ""))
|
||||
self._pending_mma_approval = task
|
||||
if "dialog_container" in task:
|
||||
task["dialog_container"][0] = dlg
|
||||
elif action == 'refresh_from_project':
|
||||
self._refresh_from_project()
|
||||
elif action == "mma_spawn_approval":
|
||||
spawn_dlg = MMASpawnApprovalDialog(
|
||||
str(task.get("ticket_id") or ""),
|
||||
str(task.get("role") or ""),
|
||||
str(task.get("prompt") or ""),
|
||||
str(task.get("context_md") or "")
|
||||
)
|
||||
self._pending_mma_spawn = task
|
||||
self._mma_spawn_prompt = task.get("prompt", "")
|
||||
self._mma_spawn_context = task.get("context_md", "")
|
||||
self._mma_spawn_open = True
|
||||
self._mma_spawn_edit_mode = False
|
||||
if "dialog_container" in task:
|
||||
task["dialog_container"][0] = spawn_dlg
|
||||
except Exception as e:
|
||||
print(f"Error executing GUI task: {e}")
|
||||
|
||||
def _process_pending_history_adds(self) -> None:
|
||||
"""Synchronizes pending history entries to the active discussion and project state."""
|
||||
with self._pending_history_adds_lock:
|
||||
items = self._pending_history_adds[:]
|
||||
self._pending_history_adds.clear()
|
||||
if not items:
|
||||
return
|
||||
self._scroll_disc_to_bottom = True
|
||||
for item in items:
|
||||
item.get("role", "unknown")
|
||||
if item.get("role") and item["role"] not in self.disc_roles:
|
||||
self.disc_roles.append(item["role"])
|
||||
disc_sec = self.project.get("discussion", {})
|
||||
discussions = disc_sec.get("discussions", {})
|
||||
disc_data = discussions.get(self.active_discussion)
|
||||
if disc_data is not None:
|
||||
if item.get("disc_title", self.active_discussion) == self.active_discussion:
|
||||
if self.disc_entries is not disc_data.get("history"):
|
||||
if "history" not in disc_data:
|
||||
disc_data["history"] = []
|
||||
disc_data["history"].append(project_manager.entry_to_str(item))
|
||||
disc_data["last_updated"] = project_manager.now_ts()
|
||||
with self._disc_entries_lock:
|
||||
self.disc_entries.append(item)
|
||||
|
||||
def shutdown(self) -> None:
|
||||
"""Cleanly shuts down the app's background tasks and saves state."""
|
||||
self.controller.stop_services()
|
||||
# Join other threads if they exist
|
||||
if self.send_thread and self.send_thread.is_alive():
|
||||
self.send_thread.join(timeout=1.0)
|
||||
if self.models_thread and self.models_thread.is_alive():
|
||||
self.models_thread.join(timeout=1.0)
|
||||
|
||||
# Final State persistence
|
||||
try:
|
||||
ai_client.cleanup() # Destroy active API caches to stop billing
|
||||
self._flush_to_project()
|
||||
self._save_active_project()
|
||||
self._flush_to_config()
|
||||
save_config(self.config)
|
||||
except: pass
|
||||
self.controller.shutdown()
|
||||
|
||||
def _test_callback_func_write_to_file(self, data: str) -> None:
|
||||
"""A dummy function that a custom_callback would execute for testing."""
|
||||
|
||||
Reference in New Issue
Block a user