refactor(src): Phase 10.2 batch 4 - aggregate + api_hooks + context_presets + external_editor

aggregate.py (1 site):
- compute_file_stats returns Result[dict[str, int]]. The 2 SILENT_SWALLOW
  sites (ast.parse + open) now append to errors list. Callers in
  gui_2.py updated to extract result.data from the cache.

api_hooks.py (1 site):
- WebSocketServer._handler - was 2 except ...: pass (JSONDecodeError +
  ConnectionClosed). Now logs warnings instead of silently swallowing.
  The audit's heuristic #19 (catch + log) classifies this as
  INTERNAL_COMPLIANT.

context_presets.py (1 site):
- ContextPresetManager.load_all returns Result[Dict[str, ContextPreset]].
  Caller in app_controller.py (load_context_preset) updated to check
  result.ok.

external_editor.py (1 site):
- _find_vscode_in_registry returns Result[Optional[str]]. The 1
  SILENT_SWALLOW site (subprocess.run) now appends to errors.
  Caller in ExternalEditorLauncher._resolve_vscode updated to extract
  result.data.

Tests updated to check result.ok and use result.data.
This commit is contained in:
ed
2026-06-17 22:38:17 -04:00
parent 89ce7ad770
commit 35bac5eda7
7 changed files with 41 additions and 28 deletions
+9 -6
View File
@@ -13,6 +13,8 @@ This is essential for keeping prompt tokens low while giving the AI enough struc
to use the MCP tools to fetch only what it needs.
"""
import ast
from src.result_types import Result, ErrorInfo, ErrorKind
import glob
import os
import re
@@ -86,12 +88,13 @@ def group_files_by_dir(files: list[Any]) -> dict[str, list[Any]]:
grouped[dir_name].append(f)
return grouped
def compute_file_stats(abs_path: str) -> dict[str, int]:
def compute_file_stats(abs_path: str) -> Result[dict[str, int]]:
"""
Computes lines and basic AST stats for a file.
[C: src/gui_2.py:App._stats_worker, tests/test_context_composition_phase3.py:test_compute_file_stats]
"""
stats = {"lines": 0, "ast_elements": 0}
errors: list[ErrorInfo] = []
try:
with open(abs_path, 'r', encoding='utf-8') as f:
content = f.read()
@@ -100,11 +103,11 @@ def compute_file_stats(abs_path: str) -> dict[str, int]:
try:
tree = ast.parse(content)
stats["ast_elements"] = sum(1 for node in ast.walk(tree) if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)))
except (SyntaxError, ValueError):
pass
except (OSError, SyntaxError):
pass
return stats
except (SyntaxError, ValueError) as e:
errors.append(ErrorInfo(kind=ErrorKind.INTERNAL, message=f"ast.parse failed: {e}", source=f"aggregate.compute_file_stats[{abs_path}]", original=e))
except (OSError, SyntaxError) as e:
errors.append(ErrorInfo(kind=ErrorKind.NOT_FOUND, message=str(e), source=f"aggregate.compute_file_stats[{abs_path}]", original=e))
return Result(data=stats, errors=errors)
def build_discussion_section(history: list[Any]) -> str:
"""