
    `gj	                    X    d Z ddlmZ ddlZddlZddlmZ ddlmZ dgZ	 G d de      Z
y)u  Shared daemon-thread ThreadPoolExecutor.

Stdlib ``ThreadPoolExecutor`` workers are non-daemon AND are registered in
``concurrent.futures.thread._threads_queues``, whose atexit hook
(``_python_exit``) joins every worker unconditionally — even after
``shutdown(wait=False)``.  A single wedged worker (tool blocked on network
I/O, hung provider daemon, stuck subagent) therefore blocks interpreter
exit forever.  This is the root cause of multi-minute CLI exits on long
sessions: every abandoned concurrent-tool batch leaves workers that the
exit hook insists on joining.

``DaemonThreadPoolExecutor`` spawns daemon workers and skips the
``_threads_queues`` registration, so:

  - ``_python_exit`` never joins them, and
  - the interpreter's non-daemon thread join at shutdown skips them.

Semantics are otherwise identical (initializer/initargs, work queue,
idle-thread reuse).  Use it for any pool whose work is best-effort or
independently interruptible and must never hold the process open:
concurrent tool execution, background memory sync, catalog fan-out,
subagent timeout wrappers.  Do NOT use it for work that must complete
before exit (durable writes) — those belong on foreground threads with
explicit bounded joins.
    )annotationsN)ThreadPoolExecutor)_workerDaemonThreadPoolExecutorc                      e Zd ZdZddZy)r   zCThreadPoolExecutor variant whose workers do not block process exit.c                   | j                   j                  d      ry | j                  fd}t        | j                        }|| j
                  k  rd| j                  xs | |fz  }t        j                  |t        t        j                  | |      | j                  | j                  | j                  fd      }|j                          | j                  j                  |       y y )Nr   )timeoutc                &    |j                  d        y )N)put)_qs     D/root/.hermes/venv/lib/python3.12/site-packages/tools/daemon_pool.py
weakref_cbzADaemonThreadPoolExecutor._adjust_thread_count.<locals>.weakref_cb.   s    EE$K    z%s_%dT)nametargetargsdaemon)_idle_semaphoreacquire_work_queuelen_threads_max_workers_thread_name_prefix	threadingThreadr   weakrefref_initializer	_initargsstartadd)selfr   num_threadsthread_namets        r   _adjust_thread_countz-DaemonThreadPoolExecutor._adjust_thread_count(   s     '''2 ,, 	 $--(***!T%=%=%E{$SSK   KKj1$$%%NN	 
A GGIMMa  +r   N)returnNone)__name__
__module____qualname____doc__r(    r   r   r   r   %   s
    M!r   )r.   
__future__r   r   r   concurrent.futuresr   concurrent.futures.threadr   __all__r   r/   r   r   <module>r4      s0   4 #   1 -%
&!1 !r   