feat(commands): multi-select ordered command pipeline with step reordering and single-pass composition (AI-05, AI-07)

This commit is contained in:
Yun Chan 2026-10-31 02:54:54 +09:00
parent f49acf0105
commit 10af20a0ad
157 changed files with 29455 additions and 913 deletions

View file

@ -0,0 +1,19 @@
"""Bound local inference workers so foreground input has scheduling headroom.
Keep the policy aligned with src/main/utils/inference-cpu-budget.ts.
This limits inference workers, not process affinity or total process CPU usage.
"""
import os
def inference_cpu_threads(available_cpus: int | None) -> int:
if available_cpus is None or available_cpus < 1:
return 1
return max(1, min(4, available_cpus - 2))
def local_inference_cpu_threads() -> int:
# Python 3.13+ exposes the CPUs available to this process, including affinity.
count = getattr(os, "process_cpu_count", os.cpu_count)()
return inference_cpu_threads(count)

View file

@ -39,6 +39,7 @@ import numpy as np
import uvicorn
from fastapi import FastAPI, File, Form, Request, UploadFile
from fastapi.responses import JSONResponse
from cpu_budget import local_inference_cpu_threads
from device_policy import (
CPU_CHOICE,
@ -185,8 +186,8 @@ def _disable_gpu(reason: BaseException) -> None:
def _cpu_threads() -> int:
"""CPU 추론에 사용할 스레드 수 (과도한 점유 방지 위해 8로 상한)."""
return max(1, min(8, os.cpu_count() or 4))
"""입력 앱과 IME 에 여유를 남기는 CPU 추론 스레드 상한."""
return local_inference_cpu_threads()
# ── 모델 다운로드 헬퍼 ─────────────────────────────────────

View file

@ -0,0 +1,30 @@
"""Scheduling headroom, including small PCs and restricted process CPU capacity."""
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import cpu_budget
class CpuBudgetTest(unittest.TestCase):
def test_reserves_headroom_and_caps_workers(self) -> None:
for cpus, expected in [(1, 1), (2, 1), (3, 1), (4, 2), (6, 4), (8, 4), (16, 4), (64, 4)]:
with self.subTest(cpus=cpus):
self.assertEqual(cpu_budget.inference_cpu_threads(cpus), expected)
def test_unknown_capacity_uses_one_worker(self) -> None:
for cpus in (None, 0, -1):
with self.subTest(cpus=cpus):
self.assertEqual(cpu_budget.inference_cpu_threads(cpus), 1)
def test_prefers_process_capacity_when_available(self) -> None:
with mock.patch.object(cpu_budget.os, "process_cpu_count", return_value=4, create=True):
with mock.patch.object(cpu_budget.os, "cpu_count", return_value=64):
self.assertEqual(cpu_budget.local_inference_cpu_threads(), 2)
if __name__ == "__main__":
unittest.main()

View file

@ -168,6 +168,7 @@ class FakeWhisperModel:
self.source = source
self.device = device
self.compute_type = compute_type
self.cpu_threads = cpu_threads
FakeWhisperModel.created.append(self)
def transcribe(self, audio: np.ndarray, **_kwargs: object) -> tuple[Iterator[_Segment], _Info]:
@ -232,6 +233,16 @@ class SidecarGpuFallbackTest(unittest.TestCase):
self.assertEqual(self._transcribe().status_code, 200)
self.assertTrue(self.client.get("/health").json()["gpu"])
def test_primary_aux_and_cpu_fallback_use_worker_budget(self) -> None:
_FakeGpuEnv.cublas_missing = True
with mock.patch("cpu_budget.os.process_cpu_count", return_value=4, create=True):
primary = self.client.post("/load", json={"model_id": "medium"})
auxiliary = self.client.post("/load", json={"model_id": "small", "slot": "aux"})
self.assertEqual(primary.status_code, 200)
self.assertEqual(auxiliary.status_code, 200)
self.assertEqual([m.device for m in FakeWhisperModel.created], ["cuda", "cpu", "cpu"])
self.assertEqual([m.cpu_threads for m in FakeWhisperModel.created], [2, 2, 2])
def test_missing_cublas_detected_at_load_uses_cpu(self) -> None:
_FakeGpuEnv.cublas_missing = True
res = self.client.post("/load", json={"model_id": "medium"})

View file

@ -250,7 +250,11 @@ def _caret_rect_and_offset(auto: Any, control: Any) -> tuple[Optional[dict], Opt
def _wake_chromium_accessibility() -> bool:
_woken_hwnds: dict[int, float] = {}
_WAKE_TTL_SECONDS = 60.0
def _wake_chromium_accessibility(force: bool = False) -> bool:
"""Chromium 계열에 "보조기술이 있다" 고 알려 접근성 트리를 열게 한다.
Chromium 문서: 브라우저가 EVENT_SYSTEM_ALERT + custom object id 1 을 알리고,
@ -259,6 +263,7 @@ def _wake_chromium_accessibility() -> bool:
LegacyIAccessible 만 노출돼 텍스트를 읽을 수 없다(실측: edit=false src=legacy len=0).
SendMessage 대신 SendMessageTimeout 을 쓰는 이유: 대상이 바쁘면 무한 대기한다.
이미 깨운 HWND 는 _WAKE_TTL_SECONDS 동안 재전송하지 않아 타이핑 중 대상 창 UI 스레드 지연을 막는다.
"""
try:
import ctypes # noqa: PLC0415
@ -268,10 +273,19 @@ def _wake_chromium_accessibility() -> bool:
WM_GETOBJECT = 0x003D
OBJID_CLIENT = 0xFFFFFFFC
hwnd = user32.GetForegroundWindow()
hwnd = int(user32.GetForegroundWindow() or 0)
if not hwnd:
return False
now = time.monotonic()
last_woken = _woken_hwnds.get(hwnd)
if not force and last_woken is not None and (now - last_woken) < _WAKE_TTL_SECONDS:
return True
if len(_woken_hwnds) > 128:
_woken_hwnds.clear()
_woken_hwnds[hwnd] = now
result = ctypes.c_void_p()
# 1 = 보조기술 감지용 custom object id
user32.SendMessageTimeoutW(
@ -280,7 +294,7 @@ def _wake_chromium_accessibility() -> bool:
wintypes.WPARAM(0),
wintypes.LPARAM(1),
0x0002, # SMTO_ABORTIFHUNG
250,
120,
ctypes.byref(result),
)
# 클라이언트 영역 객체도 함께 요청해 AX 트리를 확실히 깨운다.
@ -290,7 +304,7 @@ def _wake_chromium_accessibility() -> bool:
wintypes.WPARAM(0),
wintypes.LPARAM(OBJID_CLIENT & 0xFFFFFFFF),
0x0002,
250,
120,
ctypes.byref(result),
)
return True