Private
Public Access
TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 10: refactor(gui_2): migrate L728 run() immapp call to Result[T] (Phase 10 site 4)
Extracted _run_immapp_result(app) -> Result[None] helper above the App.run method. ANTI-SLIMING: full Result[T] propagation (NO pass-after-print). The helper returns Result(data=None) on success or Result(data=None, errors=[ErrorInfo]) on exception (logging NOT a drain per the user's principle 2026-06-17). The legacy run() wrapper sets controller._gui_degraded_reason and _last_imgui_assert (the canonical degradation drain), appends to _startup_timeline_errors, and returns WITHOUT the original stderr.print logging. Tests: 2 new tests verify both paths (success and RuntimeError). Audit: L728 reclassified from INTERNAL_SILENT_SWALLOW (10 sites remaining, was 11). New helper L728 is INTERNAL_COMPLIANT.
This commit is contained in:
+37
-27
@@ -745,33 +745,43 @@ def _post_init(self) -> None:
|
|||||||
self.runner_params.callbacks.post_init = _profiled_post_init
|
self.runner_params.callbacks.post_init = _profiled_post_init
|
||||||
self._fetch_models(self.current_provider)
|
self._fetch_models(self.current_provider)
|
||||||
md_options = markdown_helper.get_renderer().options
|
md_options = markdown_helper.get_renderer().options
|
||||||
#Note(Ed): Exception(Thirdparty)
|
def _run_immapp_result(app: "App") -> Result[None]:
|
||||||
try:
|
"""Drain-aware variant of App.run immapp.run() call (L728 INTERNAL_SILENT_SWALLOW).
|
||||||
immapp.run(self.runner_params, add_ons_params=immapp.AddOnsParams(with_markdown_options=md_options))
|
|
||||||
except RuntimeError as _immapp_exc:
|
Extracts the thirdparty immapp.run() invocation from App.run into a
|
||||||
# ImGui scope errors (IM_ASSERT) and other native-bundle exceptions
|
Result-returning helper. On exception (RuntimeError from IM_ASSERT or
|
||||||
# surface as RuntimeError. Per user feedback 2026-06-08, do not
|
other native-bundle errors), converts to ErrorInfo. The legacy run
|
||||||
# silently swallow — record the failure on the controller so the
|
method sets controller._gui_degraded_reason and _last_imgui_assert
|
||||||
# /api/gui_health endpoint and the GUI logs can surface it. Keep the
|
(the canonical degradation drain), appends to _startup_timeline_errors,
|
||||||
# process alive so the hook server (separate thread) can continue
|
and returns. NO logging: logging is NOT a drain per the user's
|
||||||
# serving tests; the next test can detect the degraded state and
|
principle 2026-06-17.
|
||||||
# fail fast with a clear message.
|
|
||||||
if hasattr(self, "controller") and self.controller is not None:
|
[C: src/gui_2.py:App.run (L728 legacy wrapper)]
|
||||||
self.controller._gui_degraded_reason = (
|
"""
|
||||||
f"immapp.run raised {type(_immapp_exc).__name__}: {_immapp_exc}"
|
#Note(Ed): Exception(Thirdparty)
|
||||||
)
|
try:
|
||||||
self.controller._last_imgui_assert = traceback.format_exc()
|
immapp.run(app.runner_params, add_ons_params=immapp.AddOnsParams(with_markdown_options=markdown_helper.get_renderer().options))
|
||||||
print(
|
return Result(data=None)
|
||||||
f"[GUI-DEGRADED] immapp.run raised: {_immapp_exc}",
|
except Exception as e:
|
||||||
file = sys.stderr,
|
return Result(data=None, errors=[ErrorInfo(
|
||||||
flush = True,
|
kind=ErrorKind.INTERNAL,
|
||||||
)
|
message=f"immapp.run raised {type(e).__name__}: {e}",
|
||||||
print(self.controller._last_imgui_assert if hasattr(self, "controller") and self.controller else "",
|
source="gui_2._run_immapp_result",
|
||||||
file=sys.stderr, flush=True)
|
original=e,
|
||||||
return
|
)])
|
||||||
# On exit (only reached on clean shutdown)
|
|
||||||
self.shutdown()
|
run_result = _run_immapp_result(self)
|
||||||
session_logger.close_session()
|
if not run_result.ok:
|
||||||
|
err = run_result.errors[0]
|
||||||
|
if hasattr(self, "controller") and self.controller is not None:
|
||||||
|
self.controller._gui_degraded_reason = err.message
|
||||||
|
self.controller._last_imgui_assert = traceback.format_exception(type(err.original), err.original, err.original.__traceback__) if err.original else ""
|
||||||
|
if not hasattr(self, '_startup_timeline_errors'): self._startup_timeline_errors = []
|
||||||
|
self._startup_timeline_errors.append(("run.immapp", err))
|
||||||
|
return
|
||||||
|
# On exit (only reached on clean shutdown)
|
||||||
|
self.shutdown()
|
||||||
|
session_logger.close_session()
|
||||||
|
|
||||||
def _load_fonts(self) -> None:
|
def _load_fonts(self) -> None:
|
||||||
from src.startup_profiler import startup_profiler
|
from src.startup_profiler import startup_profiler
|
||||||
|
|||||||
@@ -1857,4 +1857,50 @@ def test_phase_10_l612_post_init_callback_result_failure():
|
|||||||
assert "controller not ready" in err.message
|
assert "controller not ready" in err.message
|
||||||
|
|
||||||
|
|
||||||
|
def test_phase_10_l728_run_immapp_result_success():
|
||||||
|
"""
|
||||||
|
L728 _run_immapp_result returns Result(data=None) on success.
|
||||||
|
|
||||||
|
The helper extracts the immapp.run() call from App.run into a Result-returning
|
||||||
|
helper. On success, returns Result(data=None). The legacy run method
|
||||||
|
proceeds to self.shutdown() and session_logger.close_session().
|
||||||
|
"""
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
import src.gui_2 as gui2_mod
|
||||||
|
app = MagicMock()
|
||||||
|
app.runner_params = MagicMock()
|
||||||
|
with patch("src.gui_2.immapp") as mock_immapp:
|
||||||
|
mock_immapp.AddOnsParams.return_value = MagicMock()
|
||||||
|
result = gui2_mod._run_immapp_result(app)
|
||||||
|
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
|
||||||
|
assert result.data is None
|
||||||
|
mock_immapp.run.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_phase_10_l728_run_immapp_result_failure():
|
||||||
|
"""
|
||||||
|
L728 _run_immapp_result returns Result(data=None, errors=[ErrorInfo]) on failure.
|
||||||
|
|
||||||
|
When immapp.run() raises RuntimeError (IM_ASSERT, native bundle crash, etc.),
|
||||||
|
the helper converts to ErrorInfo. The legacy run method sets the
|
||||||
|
controller._gui_degraded_reason and _last_imgui_assert drain attributes,
|
||||||
|
appends to _startup_timeline_errors, and returns.
|
||||||
|
"""
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
import src.gui_2 as gui2_mod
|
||||||
|
app = MagicMock()
|
||||||
|
app.runner_params = MagicMock()
|
||||||
|
app.controller = MagicMock()
|
||||||
|
with patch("src.gui_2.immapp") as mock_immapp:
|
||||||
|
mock_immapp.AddOnsParams.return_value = MagicMock()
|
||||||
|
mock_immapp.run.side_effect = RuntimeError("IM_ASSERT: invalid scope")
|
||||||
|
result = gui2_mod._run_immapp_result(app)
|
||||||
|
assert not result.ok, f"Expected ok=False on failure, got data: {result.data}"
|
||||||
|
assert result.data is None
|
||||||
|
assert result.errors, "Expected at least one error on failure"
|
||||||
|
err = result.errors[0]
|
||||||
|
assert err.source == "gui_2._run_immapp_result"
|
||||||
|
assert "IM_ASSERT" in err.message
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user