Skip to content

fix(daemon): rewrite http.py — 10 broad except blocks, undefined _get_or_create_polylogue, asyncio.run() per request #783

Closed

Description

@Sinity

Problem

daemon/http.py has 10 broad except Exception blocks (density: 1 per 47 lines) that map ALL errors to identical 500 responses. Contains an undefined function call (_get_or_create_polylogue() at L425) that will crash the reset endpoint with NameError. Uses asyncio.run() per request inside thread-pool workers, creating nested event loop risks. 80% of the file is repetitive error-handling boilerplate.

Proposed fix

  • Replace the if self.path.startswith(...) routing with a dispatch table
  • Define a @safe_handler decorator that discriminates exception types (NameError→500, sqlite3.OperationalError→503, ValueError→400, unexpected→500 with logging)
  • Fix the missing _get_or_create_polylogue function or inline Polylogue() creation
  • Replace BaseHTTPRequestHandler with a lightweight ASGI framework or at minimum factor routing

Acceptance criteria

  • _get_or_create_polylogue defined and wired
  • Exception discrimination: different error types produce different HTTP status codes
  • Broad except blocks reduced from 10 to under 3
  • Reset endpoint works when daemon API is enabled
  • No regression in health/status/conversations/messages/facets/sources endpoints

Ref: deep audit B1, B2, R1. Ref #717.

Activity

  1. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Resolved by PR #787. Daemon http.py rewritten with @daemon_safe_handler decorator, dispatch table routing, _get_or_create_polylogue defined, except Exception reduced from 10 to 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions