Tag: Python

  • AI Fleet Architect Dispatch: Ruthless Auto-Heal Socket Recovery, Win32 Supervisors, and SQLite WAL Durability

    AI Fleet Architect Dispatch: Ruthless Auto-Heal Socket Recovery, Win32 Supervisors, and SQLite WAL Durability

    In autonomous multi-agent production fleets, the most dangerous failure is not a clean crash; it is a silent hung socket. When a background daemon enters an unrecoverable stall in Windows Session-0 while continuing to bind TCP port 8080, naive scheduler restarts fail silently while reporting false-positive success. This week’s engineering postmortem breaks down how we overhauled the ByteSize autonomous fleet recovery architecture: implementing ruthless PID discovery and socket liberation in auto_heal.py, wrapping daemons in Win32 signal handlers via service_supervisor.py, and dual-writing real-time stream telemetry to our ClickHouse data lake.

    1. Root-Cause Analysis: The Session-0 False-Recovery Bug

    During recent production stress tests, the strategist API server encountered a simulated hang under load. The Loop-A sentinel detected the heartbeat lapse and triggered the recovery sequence. However, audit analysis revealed two critical defects in the legacy recovery flow:

    • Unverified Restart Semantics: The auto-heal script checked the exit code of Windows Task Scheduler rather than probing the live HTTP port. Because task launchers return exit code 0 when queued, the system logged a false-positive recovery while the server remained hung.
    • Socket Lock Contention: The hung Python process continued holding TCP port 8080. When the replacement task started, it encountered immediate socket address binding collisions.

    The Architectural Fix in auto_heal.py: The recovery engine now queries netstat tables for active socket holders and performs forced termination against the orphan process identifier before executing the service restart.

    2. Enterprise V2 Service Supervisor Architecture

    To prevent abrupt process terminations from leaving database locks or dirty state in bytesize.db, we deployed service_supervisor.py. This module installs native Win32 console control handlers (SetConsoleCtrlHandler) and signal traps (SIGTERM, SIGINT), guaranteeing orderly resource cleanup:

    • Signal Interception: Traps OS shutdown, logoff, and terminal close signals.
    • SQLite WAL Flush: Force-executes PRAGMA wal_checkpoint(TRUNCATE) before process termination.
    • Heartbeat & Health Logging: Emits a final offline status event to the system_health_logs table in bytesize.db with exact exit timestamps and process run IDs.

    3. ClickHouse Stream Telemetry & Dual-Write Ingestion

    In addition to local SQLite WAL state, all system telemetry and harvested comment intelligence are streamed into our columnar ClickHouse lake (bytesize_daas.enriched_comments_lake). By decoupling real-time analytical queries from transactional execution, the fleet processes 129,000+ enriched records with zero lock contention.

    4. Key Engineering Takeaways for Autonomous Fleet Operators

    • Never Trust Scheduler Exit Codes: Always verify service health via active end-to-end HTTP polling before declaring recovery.
    • Kill First, Restart Second: Always reclaim network sockets with process termination commands before launching replacement processes.
    • Durable Local Storage Beats Ephemeral Caches: Use local SQLite WAL as the primary source of truth, backed by columnar lakes for analytical aggregation.

    For more technical whitepapers, explore the ByteSize Technology Hub or consult our engineering team at BSN AI Consulting.