Files
cowork-local/core/graph_server.py
T
anhtnm1andClaude Opus 5 e29a0ccdbd refactor: vá 4 hồi quy, tách 4 file chạm trần LOC, docstring lên 100%
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>
2026-08-30 10:41:45 +09:00

134 lines
5.8 KiB
Python

"""Serve the D3 Structure (RAG) graph over localhost for the default browser.
This is what keeps the FULL D3 knowledge-graph experience (drag/zoom, legend
filters, search, tooltips, click-a-node-to-open-its-folder) available in
builds without QtWebEngine — e.g. the standalone PyInstaller .exe. The tab
renders the same HTML as the embedded WebEngine view, but hands it to this
tiny HTTP server and opens the user's browser at its URL; node clicks come
back over an ``/open`` request instead of the QWebChannel bridge.
Security: the server binds to 127.0.0.1 only and every request must carry a
random per-session token, so another local process (or a web page attempting
DNS rebinding) can neither read the graph nor trigger folder-opens.
"""
from __future__ import annotations
import secrets
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from typing import Callable, Optional
from urllib.parse import parse_qs, urlparse
_PLACEHOLDER = ("<!DOCTYPE html><html><body style='background:#111;color:#ddd;"
"font-family:sans-serif'><p>No graph yet — scan one in the "
"Structure (RAG) tab first.</p></body></html>")
class GraphServer:
"""Lazy singleton-per-instance localhost server for the D3 graph page."""
def __init__(self) -> None:
"""Chuẩn bị máy chủ; chưa mở cổng nào.
Một token ngẫu nhiên được sinh ngay lúc này và mọi yêu cầu đều phải mang
nó: máy chủ nghe trên localhost, nhưng mọi tiến trình khác trên cùng máy đều
gọi được localhost.
"""
self._html = _PLACEHOLDER
self._token = secrets.token_urlsafe(16)
self._lock = threading.Lock()
self._httpd: Optional[ThreadingHTTPServer] = None
self._thread: Optional[threading.Thread] = None
self._open_cb: Optional[Callable[[str], None]] = None
# ---- content / callbacks ----------------------------------------
def set_html(self, html: str) -> None:
"""Đặt nội dung HTML sẽ phục vụ; có khoá vì luồng nền ghi còn luồng HTTP đọc."""
with self._lock:
self._html = html
def set_open_callback(self, cb: Callable[[str], None]) -> None:
"""Called (from the server thread) with the node's storage path."""
self._open_cb = cb
# ---- lifecycle ----------------------------------------------------
@property
def running(self) -> bool:
"""Máy chủ có đang chạy không."""
return self._httpd is not None
@property
def url(self) -> str:
"""URL đầy đủ kèm token; '' nếu chưa chạy."""
if self._httpd is None:
return ""
port = self._httpd.server_address[1]
return f"http://127.0.0.1:{port}/?t={self._token}"
def start(self) -> str:
"""Start (idempotent) and return the tokenised URL to open."""
if self._httpd is not None:
return self.url
server = self
class Handler(BaseHTTPRequestHandler):
"""Handler HTTP: chỉ phục vụ đúng trang đồ thị, và chỉ khi token khớp."""
def log_message(self, *_a) -> None: # keep the GUI console silent
"""Tắt log của thư viện chuẩn — nếu không, console GUI bị ngập request."""
pass
def _authorized(self, query: dict) -> bool:
"""Kiểm token trong query, so sánh theo kiểu chống dò thời gian.
Máy chủ này nghe trên localhost nhưng vẫn cần token: mọi tiến trình khác
trên cùng máy đều gọi được nó.
"""
supplied = (query.get("t") or [""])[0]
return secrets.compare_digest(supplied, server._token)
def do_GET(self) -> None: # noqa: N802 - stdlib naming
"""Trả trang đồ thị khi token đúng; sai token thì trả 403."""
parsed = urlparse(self.path)
query = parse_qs(parsed.query)
if not self._authorized(query):
self.send_error(403)
return
if parsed.path == "/":
with server._lock:
body = server._html.encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(body)))
# The page must never end up cached with a stale graph.
self.send_header("Cache-Control", "no-store")
self.end_headers()
self.wfile.write(body)
elif parsed.path == "/open":
path = (query.get("path") or [""])[0]
cb = server._open_cb
if path and cb is not None:
try:
cb(path)
except Exception: # noqa: BLE001 - never kill the server
pass
self.send_response(204)
self.end_headers()
else:
self.send_error(404)
# Port 0 = let the OS pick a free port; loopback only.
self._httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
self._httpd.daemon_threads = True
self._thread = threading.Thread(target=self._httpd.serve_forever,
name="graph-server", daemon=True)
self._thread.start()
return self.url
def stop(self) -> None:
"""Dừng máy chủ và giải phóng cổng."""
httpd, self._httpd = self._httpd, None
if httpd is not None:
httpd.shutdown()
httpd.server_close()
self._thread = None