Hồi quy đã vá
-------------
F-12 Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
`chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
`composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
gắn 1 tệp, khớp bản trước refactor.
F-01 Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
`folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
`ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
tiên hỏng thì đổi provider chính là lúc phải thử lại.
F-07 Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
`check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.
F-11 Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
và không bao giờ chạy.
Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:
graph_renderer.py -> graph_scene_builder.py + graph_export.py
co4e_workflow_service.py -> co4e_run_history.py
json_config_repository.py -> config_sections.py
agents_admin_tab.py -> shared/agent_kind_visuals.py
File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.
Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.
Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.
859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
137 lines
5.6 KiB
Python
137 lines
5.6 KiB
Python
"""Periodic reassessment scheduler (Qt layer).
|
|
|
|
No APScheduler dependency — this mirrors the app's existing ``TaskScheduler``:
|
|
a lightweight ``QTimer`` ticks periodically and, when the configured interval
|
|
has elapsed since the last assessment, launches a background reassess on a
|
|
daemon thread (so the UI never blocks). It also expires stale Manual-mode
|
|
pending switches on each tick.
|
|
|
|
Reassessment is expensive (it spends real tokens), so the cadence is
|
|
deliberately coarse — default every 24h, configurable via
|
|
``routing.reassess_interval_hours`` (0 disables the periodic run entirely).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from datetime import datetime, timezone
|
|
from typing import Any, Optional
|
|
|
|
from PySide6.QtCore import QObject, QTimer, Signal
|
|
|
|
logger = logging.getLogger("cowork_local.routing")
|
|
|
|
# How often the timer wakes to CHECK whether a reassess is due. The actual
|
|
# reassess cadence is governed by reassess_interval_hours; this is just the
|
|
# polling granularity (cheap — it only reads a timestamp).
|
|
_TICK_MS = 30 * 60 * 1000 # 30 minutes
|
|
|
|
|
|
class RoutingScheduler(QObject):
|
|
"""Drives periodic reassessment + pending-switch expiry for a service."""
|
|
|
|
reassess_started = Signal()
|
|
reassess_finished = Signal(int) # number of models assessed
|
|
|
|
def __init__(self, ctx: Any, service: Any, parent: Optional[QObject] = None) -> None:
|
|
"""Dựng bộ hẹn giờ chạy thăm dò định kỳ. Chưa chạy cho tới khi gọi ``start()``."""
|
|
super().__init__(parent)
|
|
self.ctx = ctx
|
|
self.service = service
|
|
self._timer = QTimer(self)
|
|
self._timer.setInterval(_TICK_MS)
|
|
self._timer.timeout.connect(self.tick)
|
|
|
|
# -- lifecycle ------------------------------------------------------ #
|
|
def start(self) -> None:
|
|
"""Begin periodic checks. Does NOT force an immediate reassess — the
|
|
first one happens when the interval is genuinely due (or never, if the
|
|
store is fresh), to avoid a burst of API calls at every app launch."""
|
|
self.tick()
|
|
self._timer.start()
|
|
|
|
def stop(self) -> None:
|
|
"""Dừng hẹn giờ."""
|
|
self._timer.stop()
|
|
|
|
# -- tick ----------------------------------------------------------- #
|
|
def _interval_hours(self) -> float:
|
|
"""Chu kỳ chấm điểm lại, tính bằng giờ; giá trị lạ thì coi như tắt."""
|
|
try:
|
|
return float(self.ctx.config.routing.get("reassess_interval_hours", 24) or 0)
|
|
except Exception: # noqa: BLE001
|
|
return 24.0
|
|
|
|
def _hours_since_last(self) -> Optional[float]:
|
|
"""Số giờ kể từ lần chấm điểm gần nhất; ``None`` nếu chưa chấm lần nào."""
|
|
last = self.service.store.last_updated()
|
|
if not last:
|
|
return None # never assessed
|
|
try:
|
|
dt = datetime.fromisoformat(last)
|
|
if dt.tzinfo is None:
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
|
return (datetime.now(timezone.utc) - dt).total_seconds() / 3600.0
|
|
except (ValueError, TypeError):
|
|
return None
|
|
|
|
def _routing_enabled_anywhere(self) -> bool:
|
|
"""Is routing actually in use? True if the global mode is auto/manual OR
|
|
any chat surface overrides to auto/manual. When everything is Off, the
|
|
assessment scores would never be consulted — so we don't spend tokens
|
|
probing for them (no surprise cost on a fresh install)."""
|
|
try:
|
|
routing = self.ctx.config.routing
|
|
if (routing.get("switch_mode") or "off") in ("auto", "manual"):
|
|
return True
|
|
for m in (routing.get("surface_modes") or {}).values():
|
|
if m in ("auto", "manual"):
|
|
return True
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
return False
|
|
|
|
def is_due(self) -> bool:
|
|
"""Đã đến lúc chấm điểm lại chưa.
|
|
|
|
Tắt định tuyến ở mọi bề mặt thì KHÔNG dò — dò model là lượt gọi có tính phí,
|
|
không được tiêu tiền cho một tính năng người dùng đã tắt.
|
|
"""
|
|
if not self._routing_enabled_anywhere():
|
|
return False # routing off everywhere → don't probe (would be wasted cost)
|
|
interval = self._interval_hours()
|
|
if interval <= 0:
|
|
return False # periodic reassess disabled
|
|
since = self._hours_since_last()
|
|
if since is None:
|
|
return True # never assessed → due once routing is actually enabled
|
|
return since >= interval
|
|
|
|
def tick(self) -> None:
|
|
"""Expire stale pending switches; reassess if the interval is due."""
|
|
try:
|
|
self.service.sweep_pending()
|
|
except Exception: # noqa: BLE001
|
|
logger.exception("routing.scheduler: sweep_pending failed")
|
|
|
|
if not self.is_due() or self.service.is_reassessing():
|
|
return
|
|
|
|
logger.info("routing.scheduler: reassess is due — starting background run")
|
|
self.reassess_started.emit()
|
|
|
|
def _done(result) -> None:
|
|
"""Chấm điểm xong: báo ra ngoài số model đã đánh giá."""
|
|
self.reassess_finished.emit(len(result or {}))
|
|
|
|
self.service.reassess_background(on_done=_done)
|
|
|
|
def trigger_now(self) -> None:
|
|
"""Force an out-of-band reassess (e.g. Settings' 'Reassess now' button)."""
|
|
if self.service.is_reassessing():
|
|
return
|
|
self.reassess_started.emit()
|
|
self.service.reassess_background(on_done=lambda r: self.reassess_finished.emit(len(r or {})))
|
|
|
|
|
|
__all__ = ["RoutingScheduler"]
|