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>
121 lines
4.8 KiB
Python
121 lines
4.8 KiB
Python
"""Command tools - run_command, install_package (R05-T02).
|
|
|
|
Moved verbatim out of ``core/tools.py`` (see ``file_tools.py`` for why). These
|
|
two are the ones today's hand-written permission gate in
|
|
``core/chat_agent.py`` singles out by literal name
|
|
(``name in ("run_command", "install_package")``) — R05-T03 replaces that
|
|
tuple with a capability lookup, but the tools themselves are unchanged here.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from .tool_context import CancelFn, ToolContext
|
|
|
|
COMMAND_TIMEOUT = 120 # seconds
|
|
|
|
_SNAPSHOT_SKIP = {".git", "__pycache__", "node_modules", ".scratch", ".venv",
|
|
".idea", ".mypy_cache", ".pytest_cache"}
|
|
|
|
|
|
def _snapshot(workdir: Path) -> Dict[str, Any]:
|
|
"""Map of file path -> (mtime, size) under the workdir (noise dirs skipped)."""
|
|
snap: Dict[str, Any] = {}
|
|
try:
|
|
for dirpath, dirnames, filenames in os.walk(str(workdir)):
|
|
dirnames[:] = [d for d in dirnames if d not in _SNAPSHOT_SKIP]
|
|
for fn in filenames:
|
|
full = os.path.join(dirpath, fn)
|
|
try:
|
|
st = os.stat(full)
|
|
snap[full] = (st.st_mtime_ns, st.st_size)
|
|
except OSError:
|
|
pass
|
|
if len(snap) > 5000:
|
|
return snap
|
|
except OSError:
|
|
pass
|
|
return snap
|
|
|
|
|
|
def _sandbox_python(ctx: ToolContext, cancel: Optional[CancelFn] = None,
|
|
on_output=None) -> Optional[str]:
|
|
"""Lazily create/reuse this ctx's project sandbox venv (Code tab only —
|
|
``ctx.sandbox``); returns its python path, or None to use the app's own."""
|
|
if not ctx.sandbox:
|
|
return None
|
|
from cowork_local.core.deps import ensure_project_venv
|
|
|
|
py = ensure_project_venv(ctx.workdir, cancel=cancel, on_output=on_output)
|
|
return str(py) if py else None
|
|
|
|
|
|
def run_command(ctx: ToolContext, args: Dict[str, Any],
|
|
cancel: Optional[CancelFn] = None,
|
|
on_output=None) -> Dict[str, Any]:
|
|
"""Chạy một lệnh shell trong thư mục làm việc, có hạn giờ và có sandbox.
|
|
|
|
Biến môi trường được lọc và mạng bị chặn theo cấu hình an toàn — agent chạy
|
|
lệnh không được thừa hưởng toàn bộ môi trường của người dùng.
|
|
"""
|
|
from cowork_local.core.deps import network_blocked_env, run_cancellable, sandbox_env
|
|
from cowork_local.core.sandbox_manager import ExecutionConfig, SandboxManager
|
|
from cowork_local.security.command_risk_classifier import classify_command
|
|
|
|
command = str(args.get("command", "")).strip()
|
|
if not command:
|
|
return {"ok": False, "output": "Empty command."}
|
|
|
|
# --- Security validation pipeline ---
|
|
risk = classify_command(command, is_cowork_mode=ctx.flatten_writes)
|
|
if risk.blocked:
|
|
denial = "Command blocked by security policy: " + "; ".join(risk.reasons)
|
|
return {"ok": False, "output": denial}
|
|
|
|
# Route through SandboxManager for risk-based isolation
|
|
mgr = SandboxManager(ExecutionConfig(
|
|
enabled=True,
|
|
block_network_by_default=ctx.block_network,
|
|
is_cowork_mode=ctx.flatten_writes,
|
|
))
|
|
sandbox_result = mgr.run(
|
|
command=command,
|
|
workdir=str(ctx.workdir),
|
|
block_network=ctx.block_network,
|
|
timeout_sec=COMMAND_TIMEOUT,
|
|
cancel=cancel,
|
|
)
|
|
# Sandbox ALWAYS executes (never double-run). Return its result directly.
|
|
if sandbox_result.get("sandbox") == "blocked":
|
|
return {"ok": False, "output": sandbox_result.get("stderr", "Command blocked")}
|
|
out = sandbox_result.get("stdout", "").strip() or "(no output)"
|
|
err = sandbox_result.get("stderr", "")
|
|
rc = sandbox_result.get("returncode", -1)
|
|
if err:
|
|
out = f"{out}\n{err}" if out else err
|
|
return {"ok": sandbox_result.get("ok", False), "output": f"[exit {rc}]\n{out}"}
|
|
|
|
|
|
def install_package(ctx: ToolContext, args: Dict[str, Any],
|
|
cancel: Optional[CancelFn] = None,
|
|
on_output=None) -> Dict[str, Any]:
|
|
"""Cài một gói Python vào môi trường phụ trợ của lượt chạy.
|
|
|
|
Cài vào venv riêng chứ không vào Python của hệ thống — một task không được
|
|
phép làm hỏng môi trường của cả máy.
|
|
"""
|
|
from cowork_local.core.deps import pip_install
|
|
|
|
package = str(args.get("package", "")).strip()
|
|
if not package:
|
|
return {"ok": False, "output": "No package specified."}
|
|
python = _sandbox_python(ctx, cancel, on_output)
|
|
ok, detail = pip_install(package, cancel=cancel, on_output=on_output, python=python)
|
|
head = f"Installed {package}." if ok else f"Could not install {package}."
|
|
return {"ok": ok, "output": f"{head}\n{detail}"}
|
|
|
|
|
|
__all__ = ["COMMAND_TIMEOUT", "run_command", "install_package", "_snapshot"]
|