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>
120 lines
5.3 KiB
Python
120 lines
5.3 KiB
Python
"""McpToolSourceManager - the MCP server connection lifecycle, extracted out
|
|
of ``state.py::AppContext`` (R05-T05).
|
|
|
|
Today ``AppContext.build_mcp_tools`` inlines all of this: a ``_mcp_connections``
|
|
dict, a ``_conn_lock`` guarding check-then-create against concurrent turns (a
|
|
Cowork tab, a Co4E flow and a Scheduled Task can all call it at once), and a
|
|
"start it, cache it, skip it on failure" loop repeated for both the
|
|
admin-configured servers AND the built-in MS365 server
|
|
(``_ms365_builtin_connection``). None of that logic touches Qt; it was only
|
|
ever inline because ``AppContext`` is where the config lived.
|
|
|
|
This class owns the SAME cache/lock/start-or-skip behavior as a standalone,
|
|
directly testable object — ``AppContext`` becomes a thin caller (one instance
|
|
per app, same as it holds one ``RoutingApplicationService``).
|
|
|
|
Pure Python: no Qt. It DOES touch the network/filesystem via
|
|
``core.mcp_client.McpServerConnection`` (a subprocess + asyncio loop), which is
|
|
exactly what makes it infrastructure rather than domain.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
from typing import Dict, List, Optional
|
|
|
|
from cowork_local.core.mcp_client import McpServerConnection
|
|
|
|
|
|
class McpToolSourceManager:
|
|
"""Caches and supervises one :class:`McpServerConnection` per server name.
|
|
|
|
``connection_factory`` defaults to ``McpServerConnection`` itself; tests
|
|
substitute a fake so no real subprocess is spawned (see
|
|
``tests/unit/test_mcp_source_manager.py``).
|
|
"""
|
|
|
|
def __init__(self, connection_factory=McpServerConnection) -> None:
|
|
"""``connection_factory`` tiêm được để test không phải chạy tiến trình con thật.
|
|
|
|
Có khoá riêng vì nhiều lượt chat song song cùng gọi tới đây: kiểm-rồi-tạo mà
|
|
không khoá sẽ dựng hai kết nối cho cùng một máy chủ.
|
|
"""
|
|
self._connections: Dict[str, McpServerConnection] = {}
|
|
self._lock = threading.Lock()
|
|
self._connection_factory = connection_factory
|
|
|
|
def ensure(self, name: str, command: str, args: Optional[List[str]] = None,
|
|
env: Optional[Dict[str, str]] = None) -> Optional[McpServerConnection]:
|
|
"""Return a live connection for ``name``, starting one if there is
|
|
none cached or the cached one's subprocess has died.
|
|
|
|
Serialized under one lock so two turns racing to build their tool
|
|
list at the same moment share one subprocess per server instead of
|
|
each spawning their own (the bug this replaces:
|
|
``AppContext._conn_lock``'s original docstring). Returns ``None`` -
|
|
never raises - when the server fails to start, matching the existing
|
|
"one broken server must not block the turn" behavior.
|
|
"""
|
|
with self._lock:
|
|
existing = self._connections.get(name)
|
|
if existing is not None and existing.is_alive():
|
|
return existing
|
|
if existing is not None:
|
|
self._connections.pop(name, None)
|
|
connection = self._connection_factory(name, command, args or [], env)
|
|
try:
|
|
connection.start()
|
|
except Exception: # noqa: BLE001 - one broken server must not block the turn
|
|
return None
|
|
self._connections[name] = connection
|
|
return connection
|
|
|
|
def get(self, name: str) -> Optional[McpServerConnection]:
|
|
"""The cached connection for ``name``, without starting one."""
|
|
return self._connections.get(name)
|
|
|
|
def is_alive(self, name: str) -> bool:
|
|
"""Kết nối tới một MCP server còn sống không."""
|
|
connection = self._connections.get(name)
|
|
return connection is not None and connection.is_alive()
|
|
|
|
def restart(self, name: str, command: str, args: Optional[List[str]] = None,
|
|
env: Optional[Dict[str, str]] = None) -> Optional[McpServerConnection]:
|
|
"""Force a fresh connection for ``name`` even if the cached one still
|
|
looks alive - for a server the caller knows is misbehaving."""
|
|
with self._lock:
|
|
self._connections.pop(name, None)
|
|
return self.ensure(name, command, args, env)
|
|
|
|
def stop(self, name: str) -> None:
|
|
"""Stop and forget one connection - used when a server becomes
|
|
unavailable by configuration (e.g. MS365 signed out) rather than by
|
|
crashing."""
|
|
with self._lock:
|
|
connection = self._connections.pop(name, None)
|
|
if connection is not None:
|
|
try:
|
|
connection.stop()
|
|
except Exception: # noqa: BLE001 - shutdown must never raise into the caller
|
|
pass
|
|
|
|
def active(self) -> List[McpServerConnection]:
|
|
"""Every currently cached connection - what
|
|
``core/mcp_client.py::build_mcp_tools`` merges tool specs from."""
|
|
return list(self._connections.values())
|
|
|
|
def stop_all(self) -> None:
|
|
"""Terminate every connection's subprocess - called on app shutdown
|
|
so none of them linger as orphan processes."""
|
|
with self._lock:
|
|
connections = list(self._connections.values())
|
|
self._connections.clear()
|
|
for connection in connections:
|
|
try:
|
|
connection.stop()
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
|
|
|
|
__all__ = ["McpToolSourceManager"]
|