"""Shared AppController I/O pool factory.

Historical note: an earlier revision of this module registered an
``atexit.register(pool.shutdown, wait=False)`` handler here, mirroring
the conftest fix at commit 8957c9a5. That approach was reverted because
it does not solve the Ctrl+C hang in ``sloppy.py`` when a worker is
mid-task (e.g. a long-running Gemini/Anthropic HTTP request): atexit
handlers do not fire at all in that scenario, so the process still hangs
in ``ThreadPoolExecutor.__del__`` -> ``shutdown(wait=True)`` during
finalization.

The production fix lives in ``AppController.__init__`` as a SIGINT
handler that drains the pool and calls ``os._exit(0)``, sidestepping
the broken finalization chain. See commit log for details.
"""

from concurrent.futures import ThreadPoolExecutor


IO_POOL_MAX_WORKERS: int = 8
IO_POOL_THREAD_NAME_PREFIX: str = "controller-io"


def make_io_pool(max_workers: int = IO_POOL_MAX_WORKERS) -> ThreadPoolExecutor:
 """Create the shared AppController I/O pool.

 8 worker threads (per `IO_POOL_MAX_WORKERS = 8` above; bumped from 4
 in commit 4a338486 on 2026-06-06 to relieve contention during batched
 sims), named "controller-io-N". Used for warmup, log pruning, disk-
 bound subsystem init, and any other background work that should not
 spin up its own thread.

 Caller is responsible for shutdown (e.g. controller.shutdown()).
 """
 return ThreadPoolExecutor(
  max_workers=max_workers,
  thread_name_prefix=IO_POOL_THREAD_NAME_PREFIX,
 )
