Skip to content

Optimization: Run NiceGUI ask dialog in-process in daemon #1152

Description

@itdove

Description

Currently, the daemon's /api/prompt handler spawns a subprocess for all ask dialog display tiers (tkinter, NiceGUI, Textual). This was implemented in #1151 to fix the daemon orphaning issue caused by tkinter's macOS main-thread requirement.

However, NiceGUI is browser-based and has no main-thread restriction. It can safely run in-process in the daemon, which would:

  • Eliminate ~100-200ms subprocess spawn overhead
  • Simplify the call chain for NiceGUI dialogs
  • Still provide the same user experience

Current Behavior

All ask dialogs use subprocess delegation:

# daemon/rest_api.py:_handle_prompt
result = _show_via_subprocess(violation, fallback, timeout)

This spawns ai-guardian prompt --mode ask which tries: tkinter → NiceGUI → Textual → fallback.

Proposed Improvement

Optimize the daemon's _handle_prompt to:

  1. Try NiceGUI in-process first (safe, no main-thread requirement)
  2. Fall back to subprocess for tkinter/Textual (required for those tiers)
# Pseudocode
if _nicegui_available():
    result = _NiceGuiAskDialog(violation, timeout).run()
if result is None:
    result = _show_via_subprocess(violation, fallback, timeout)

Why Not All In-Process?

  • tkinter: MUST use subprocess on macOS (tk.Tk() requires main thread)
  • NiceGUI: CAN run in-process (browser-based, no thread restrictions) ← optimization opportunity
  • Textual: MUST use subprocess (daemon has no controlling terminal)

Benefits

  • Faster response time for NiceGUI users (~100-200ms saved)
  • No change in safety/isolation (NiceGUI already proven stable in-process in the tray)

Related

Priority

Low — current subprocess approach works correctly, this is a performance optimization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions