Private
Public Access
0
0

conductor(plan): Phase 2 complete - io_pool + warmup foundation in place

Phase 2 of startup_speedup_20260606 is done.

Tasks:
  T2.1 (Red)   tests/test_io_pool.py         1354679e  4 tests
  T2.2 (Green) src/io_pool.py                1354679e  make_io_pool() factory
  T2.3 (Red)   tests/test_warmup.py          1354679e  10 tests
  T2.4 (Green) src/warmup.py                 1354679e  WarmupManager
  T2.5 (Wire)  AppController integration     922c5ad9  io_pool + warmup in __init__ + 5 public delegation methods
  T2.6 (Plan)  this commit

What now exists:
  - make_io_pool() returns a 4-worker ThreadPoolExecutor named 'controller-io-N'
  - WarmupManager class with submit/status/is_done/wait/on_complete/reset
  - AppController creates self._io_pool + self._warmup early in __init__
  - Warmup is submitted immediately (jobs run concurrent with the rest of init)
  - Public API: controller.warmup_status(), controller.is_warmup_done(),
    controller.wait_for_warmup(timeout), controller.on_warmup_complete(cb)
  - controller._compute_warmup_list() returns 9 always + 2 conditional (fastapi)
  - shutdown() now also shuts down the io_pool

Currently the warmup is a no-op for modules already imported at the top
of app_controller.py (fastapi, requests). Phase 3 will remove those
top-level imports; the warmup infrastructure will then start doing
real work.

18/18 tests passing (4 io_pool + 10 warmup + 4 test_app_controller_*).

Next: Phase 3 (remove top-level SDK imports from src/ai_client.py).
Expected to fix ~3 audit violations (google.genai, anthropic, openai).
This commit is contained in:
2026-06-06 14:52:04 -04:00
parent 922c5ad9ab
commit 7eb743c6cb
2 changed files with 17 additions and 31 deletions
@@ -29,31 +29,17 @@ Two user constraints, addressed together:
The codebase gets ONE shared `ThreadPoolExecutor` on `AppController` named
`_io_pool`, used for warmup AND any future background work.
- [ ] **T2.1 (Red)** `tests/test_app_controller_io_pool.py`:
- `test_app_controller_has_io_pool`: instantiate `AppController`, assert `hasattr(controller, '_io_pool')` and it's a `ThreadPoolExecutor`
- `test_io_pool_uses_named_threads`: submit a job, assert the executing thread name starts with `controller-io`
- `test_io_pool_size_is_4`: assert `_io_pool._max_workers == 4`
- `test_io_pool_shuts_down_on_close`: call `controller.shutdown()`, assert the pool is shut down
- Confirm FAIL (no `_io_pool` yet)
- [ ] **T2.2 (Green)** In `src/app_controller.py`:
- Add `from concurrent.futures import ThreadPoolExecutor` and `import importlib` at top
- In `__init__`, after the asyncio loop starts and BEFORE the existing HookServer block: `self._io_pool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="controller-io")`
- Add warmup state: `self._warmup_lock`, `self._warmup_done_event`, `self._warmup_status` (dict with `pending`/`completed`/`failed` lists), `self._warmup_callbacks`
- Call `self._submit_warmup_jobs()` at the end of `__init__`
- In `shutdown()` (already exists in `App.shutdown` for the GUI; ensure the AppController has a matching shutdown that calls `self._io_pool.shutdown(wait=False)`)
- [ ] **T2.3 (Red)** `tests/test_warmup_mechanism.py`:
- `test_warmup_jobs_submitted_on_init`: after `AppController.__init__`, assert `len(controller.warmup_status()['pending']) > 0`
- `test_warmup_jobs_complete_within_timeout`: call `controller.wait_for_warmup(timeout=10.0)`, assert True
- `test_warmup_status_reflects_completion`: after `wait_for_warmup`, assert `controller.is_warmup_done() == True` and `len(warmup_status()['pending']) == 0`
- `test_warmup_callback_fires_on_completion`: register a callback via `controller.on_warmup_complete(cb)`, assert it was called once warmup done
- `test_warmup_does_not_block_init`: time `__init__` with a 4-worker pool, assert it returns in < 200ms even though warmup takes longer
- Confirm FAIL (no warmup yet)
- [ ] **T2.4 (Green)** Implement `_submit_warmup_jobs()`, `_compute_warmup_list()`, `_warmup_one()`, `warmup_status()`, `is_warmup_done()`, `wait_for_warmup()`, `on_warmup_complete()` per spec §3.2. Warmup list includes: `google.genai`, `anthropic`, `openai`, `requests`, `src.command_palette`, `src.theme_nerv`, `src.theme_nerv_fx`, `src.markdown_table`, `numpy`. Conditionally adds `fastapi`, `fastapi.security.api_key` if `enable_test_hooks` or `web_host` is set.
- [ ] **T2.5** Run T2.1 and T2.3 tests; confirm PASS
- [ ] **T2.6** Commit: `feat(app_controller): add _io_pool + proactive warmup mechanism` + git note
- [x] **T2.1 (Red)** `tests/test_io_pool.py` (4 tests covering: ThreadPoolExecutor returned, 4 workers, threads named `controller-io-*`, jobs run in parallel via barrier). `[T2.1: 1354679e]`
- [x] **T2.2 (Green)** `src/io_pool.py``make_io_pool()` factory: 4-worker `ThreadPoolExecutor` with `thread_name_prefix="controller-io"`. `[T2.2: 1354679e]`
- [x] **T2.3 (Red)** `tests/test_warmup.py` (10 tests covering: one job per module, status, failures, done event, wait, callbacks, fire-immediately, sys.modules, reset, concurrency). `[T2.3: 1354679e]`
- [x] **T2.4 (Green)** `src/warmup.py``WarmupManager` class with `submit`, `status`, `is_done`, `wait`, `on_complete`, `reset`. Thread-safe (lock-guarded). Public API on AppController: `warmup_status()`, `is_warmup_done()`, `wait_for_warmup()`, `on_warmup_complete()`. Warmup list always includes `google.genai, anthropic, openai, requests, src.command_palette, src.theme_nerv, src.theme_nerv_fx, src.markdown_table, numpy`; conditionally adds `fastapi, fastapi.security.api_key` when `test_hooks_enabled`. `[T2.4: 1354679e]`
- [x] **T2.5** Wire into `AppController.__init__` (right after locks, before subsystem init). Public delegation methods added. `shutdown()` calls `self._io_pool.shutdown(wait=False)`. All 18 tests pass (io_pool + warmup + existing test_app_controller_*). `[T2.5: 922c5ad9]`
- [x] **T2.6** Plan update + commit: this commit.
**Phase 2 checkpoint:** `AppController` owns a 4-thread named pool. Warmup jobs are submitted in `__init__` and complete in the background. `controller.wait_for_warmup()`, `controller.warmup_status()`, and `controller.on_warmup_complete(cb)` are the public API. Main thread does NOT block waiting for warmup.
**NOTE on current effectiveness:** With the current codebase, the warmup is a no-op for modules already imported at the top of `src/app_controller.py` (fastapi, requests, etc. — already in `sys.modules`). The infrastructure is in place; Phase 3 will remove the top-level imports so the warmup actually does work. The warmup already helps for modules NOT at the top of any main-thread-reachable file (e.g., `src.theme_nerv*` if not yet imported).
---
## Phase 3: Remove top-level heavy imports from `src/ai_client.py` (TDD)
@@ -5,12 +5,12 @@
track_id = "startup_speedup_20260606"
name = "Sloppy.py Startup Speedup"
status = "active"
current_phase = 1
current_phase = 2
last_updated = "2026-06-07"
[phases]
phase_1 = { status = "in_progress", checkpoint_sha = "", name = "Audit + Benchmark + Foundation" }
phase_2 = { status = "pending", checkpoint_sha = "", name = "Job Pool + Warmup Foundation" }
phase_1 = { status = "completed", checkpoint_sha = "f9a01258", name = "Audit + Benchmark + Foundation" }
phase_2 = { status = "completed", checkpoint_sha = "f9a01258", name = "Job Pool + Warmup Foundation" }
phase_3 = { status = "pending", checkpoint_sha = "", name = "Remove top-level SDK imports (ai_client)" }
phase_4 = { status = "pending", checkpoint_sha = "", name = "Remove top-level FastAPI imports" }
phase_5 = { status = "pending", checkpoint_sha = "", name = "Remove top-level feature-gated GUI imports" }
@@ -27,12 +27,12 @@ t1_3 = { status = "completed", commit_sha = "5a856536", description = "Add Start
t1_4 = { status = "completed", commit_sha = "6f9a3af2", description = "Write scripts/audit_main_thread_imports.py (static CI gate) + 9 tests" }
t1_5 = { status = "in_progress", commit_sha = "", description = "Commit plan update (this commit)" }
# Phase 2: Job Pool + Warmup Foundation
t2_1 = { status = "pending", commit_sha = "", description = "Red: tests/test_app_controller_io_pool.py" }
t2_2 = { status = "pending", commit_sha = "", description = "Green: add _io_pool ThreadPoolExecutor to AppController" }
t2_3 = { status = "pending", commit_sha = "", description = "Red: tests/test_warmup_mechanism.py" }
t2_4 = { status = "pending", commit_sha = "", description = "Green: implement _submit_warmup_jobs, _compute_warmup_list, _warmup_one, warmup_status, is_warmup_done, wait_for_warmup, on_warmup_complete" }
t2_5 = { status = "pending", commit_sha = "", description = "Confirm T2.1 + T2.3 tests pass" }
t2_6 = { status = "pending", commit_sha = "", description = "Commit T2" }
t2_1 = { status = "completed", commit_sha = "1354679e", description = "Red: tests/test_io_pool.py (4 tests)" }
t2_2 = { status = "completed", commit_sha = "1354679e", description = "Green: src/io_pool.py make_io_pool factory" }
t2_3 = { status = "completed", commit_sha = "1354679e", description = "Red: tests/test_warmup.py (10 tests)" }
t2_4 = { status = "completed", commit_sha = "1354679e", description = "Green: src/warmup.py WarmupManager class" }
t2_5 = { status = "completed", commit_sha = "922c5ad9", description = "Wire _io_pool + warmup into AppController.__init__ + 5 public delegation methods + io_pool shutdown" }
t2_6 = { status = "in_progress", commit_sha = "", description = "Plan update (this commit)" }
# Phase 3: Remove top-level SDK imports
t3_1 = { status = "pending", commit_sha = "", description = "Red: tests/test_ai_client_no_top_level_sdk_imports.py" }
t3_2 = { status = "pending", commit_sha = "", description = "Green: remove top-level SDK imports from src/ai_client.py; add _require_warmed helper; update _send_* to use it" }