diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..c2931b3 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,82 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Project + +Cowork Local (user-facing brand "Cowork-Local BamBOO") is a local-first PySide6 desktop app: multi-turn AI agents, per-project workspaces with GraphRAG, scheduled agent tasks (Kanban), MCP connectors, a sandbox security layer, model routing, and a monitoring dashboard. User config lives in `~/.cowork_local`. The internal name `cowork_local` / `APP_NAME` must not be rebranded. Only `DISPLAY_NAME` is the brand. + +## The package-name quirk (read first) + +The repository root **is** the `cowork_local` package: `__init__.py` and `__main__.py` sit at the root, and code imports itself as `cowork_local.*` or through relative imports. This checkout's folder is not named `cowork_local`, so: + +- **Running the app:** `python -m cowork_local` works only from the parent of a folder literally named `cowork_local`. On Windows, `install.bat` (once; add `--dev` for test deps, `--system` to skip the venv) builds a venv under `%LOCALAPPDATA%\CoworkLocal` and creates a junction `%LOCALAPPDATA%\CoworkLocal\launcher\\cowork_local` pointing at this checkout. After that, use `run.bat`. The MS365 MCP server is spawned as `python -m cowork_local.mcp_servers.ms365_server`, so the junction is needed for subprocesses too. +- **Never create a `.venv` inside the repo.** The quality gates walk the whole tree. +- **Tests:** the root `conftest.py` and `tests/conftest.py` bind `sys.modules["cowork_local"]` to this checkout, so pytest works whatever the folder is named. Tests use both import styles: top-level (`from providers.base import ...`) and qualified (`from cowork_local.core... import ...`). Characterization tests that spawn `python -c "from cowork_local..."` subprocesses still need a real `cowork_local` directory on `PYTHONPATH`. CI checks out into `cowork_local/` for this reason. + +## Commands + +```bash +python -m pip install -r requirements.txt # single requirements file (includes pytest) +python -m pytest tests -q # full suite (what CI runs) +python -m pytest tests/unit/test_schedule_calculator.py -q # one file +python -m pytest tests/unit/test_schedule_calculator.py -k name -q # one test +python -m pytest tests/e2e/test_smoke.py -v # release smoke test + +python scripts/run_quality_gate.py # all CASAN gates + pytest +python scripts/run_quality_gate.py --skip-tests # static gates only +python scripts/check_imports.py # domain/ + application/ must not import PySide6/PyQt/ui/app +python scripts/audit_security.py # no plaintext credentials (CI also runs --self-test) +python scripts/check_loc.py # <= 400 lines per production file +python scripts/check_orphan_modules.py # every production module must be reachable by import +``` + +Widget tests run headless with `QT_QPA_PLATFORM=offscreen`. `tools/check_*.py` are standalone offscreen smoke checkers against a real `MainWindow` built on a copy of `~/.cowork_local` (for example `python tools/check_nav.py`). Some of them still import private names re-exported from `app.py`, so keep those re-exports. Set `COWORK_PERF_TRACE=1` to log timing spans from `performance.py`. + +## Architecture + +Target design is 4-tier Clean Architecture (`docs/architecture/ADR-001-layered-architecture.md`): + +- `domain/`: pure stdlib entities, frozen request snapshots (`ConversationExecutionRequest`), `AgentEvent`, tool/provider descriptors, `ScheduleCalculator`. +- `application/`: pure-Python use-case services (conversations, model_routing, scheduling, workspaces, monitoring, workflows). **No Qt.** Must run headless. +- `infrastructure/`: adapters: config (`JsonConfigRepository` over `AtomicJsonFile`), OS-keyring `SecretStore`, providers, MCP (`McpToolSourceManager`), sandbox, filesystem, telemetry, and Qt bridges (`infrastructure/qt`). +- `presentation/`: PySide6 widgets by feature (`shell`, `chat`, `co4e`, `dashboard`, `scheduling`, `workspace`, `monitoring`, `graph`, `settings`, ...). Widgets call `application/` services. They don't touch persistence or run LLM calls on the GUI thread. Agent work runs in worker threads and reaches the UI as `AgentEvent`s through Qt signal bridges. + +The refactor is **incomplete**. Legacy top-level packages are still live and imported by the app: +- `ui/`: older tabs such as `cowork_tab`, `workspace_tab`, `monitoring_tab`, `settings_dialog`, `chat_panel`, and `co4e_*`. +- `core/`: agents, the Co4E flow runner, task scheduler, skills, tools, audit/usage tracking, routing. +- `providers/`: `base`, `anthropic`, `openai_compat`, `factory`. +- `security/`: validators, command risk classifier. +- Root modules: `config.py`, `state.py`. + +`docs/architecture/dormant-code.md` lists deprecated pieces, such as `state.py::active_project_id` and the monolithic `core/tools.py`. New layers must not import dormant code. + +Wiring: +- `__main__.py` → `app.run()`. +- `presentation/shell/bootstrap.py` is the **Composition Root**. It builds `AppContext` (`state.py`) around `JsonConfigRepository` plus `KeyringAdapter`, falling back to the config file when no keyring exists. +- `run()` then seeds the built-in skills (`skill_library/*.skill`) and the Co4E flows, applies the theme, and opens `presentation/shell/main_window.MainWindow`. +- Pages are registered in `presentation/shell/page_registry.py`. + +Other cross-cutting pieces: +- `i18n/`: `tr(key, **kw)` with en/ja/vi (default `vi`). Long-lived widgets must use `bind_text(...)` or `on_language_changed(...)` so a language switch re-applies their text. Transient dialogs just call `tr()` at construction. +- `theme/`: palettes and QSS. Use theme tokens, not hard-coded colors. +- `mcp_servers/`: bundled MCP servers (MS365, project_context). +- `agent/`: a markdown instruction library for UI/UX bug-fix agents, not runtime code. + +Step-by-step recipes for adding a provider, a tool or MCP server, or a screen are in `docs/governance/contributor-recipes.md`. + +## Rules enforced by gates and review + +- **400-line limit** for every production file. This covers `domain`, `application`, `infrastructure`, `presentation`, `ui`, `core`, `providers`, `security`, `mcp_servers`, `i18n`, `theme`, and root `.py` files. Legacy oversized files have per-file caps in `scripts/check_loc.py` that may only go down. Split files; never raise a cap. +- **No orphan modules.** `check_orphan_modules.py` has an `ALLOWLIST` that may only shrink. Wire up or delete a module instead of allowlisting it. +- **Secrets** belong in the OS keyring, never in `config.json` or the code. `.env.example` is not auto-loaded. Tests never use live provider credentials (use `tests/fakes/`: `FakeProvider`, `FakeToolExecutor`, `FakeToolPolicyGateway`, `FakeConfigRepository`/`FakeSecretStore`, `FakeClock`, ...). +- **Test layout:** `tests/unit` (fast, no I/O), `tests/contracts`, `tests/integration` (Qt offscreen), `tests/characterization` (pinned legacy behavior during refactors), `tests/e2e`, `tests/ui`, plus flat `tests/test_*.py`. +- **Startup housekeeping** (seeding, pruning) is wrapped in `try/except` and must never block app launch. +- **Comments:** the ADR asks for English comments. Existing code mixes English and Vietnamese docstrings and comments, so match the surrounding file. + +## Git workflow + +- Branches: `feat/`, `fix/`, `test/`, `docs/`, `perf/`, `refactor/`. Core AI work uses `core-ai/-`. +- Commits use Conventional Commit prefixes (`feat:`, `fix:`, `test:`, `docs:`, `refactor:`, `perf:`, `chore:`). +- One logical change per PR, using `.gitea/PULL_REQUEST_TEMPLATE.md`. The remote is a Gitea instance, and CI (`.gitea/workflows/ci.yaml`, Python 3.11) runs on PRs to `main`. +- Critical areas listed in `SECURITY.md` get extra review. diff --git a/application/network/__init__.py b/application/network/__init__.py new file mode 100644 index 0000000..d1abf7e --- /dev/null +++ b/application/network/__init__.py @@ -0,0 +1,5 @@ +"""Application services for the "Block network" switch (Sandbox Security Layer).""" + +from .network_guard import NetworkBlockedError, bind, ensure_allowed, is_blocked, refusal + +__all__ = ["NetworkBlockedError", "bind", "ensure_allowed", "is_blocked", "refusal"] diff --git a/application/network/network_guard.py b/application/network/network_guard.py new file mode 100644 index 0000000..0d655f4 --- /dev/null +++ b/application/network/network_guard.py @@ -0,0 +1,61 @@ +"""One gate for every outbound connection the app makes on its own. + +The "Block network" switch (``agent_security.block_network``) used to be read +only where agent tools run, so Microsoft 365, Teams, connector test buttons, +scheduled task scripts, pip auto-installs and HTML previews still reached the +internet while Monitoring said "Network: blocked". Each of those now asks +this module first. + +The AI provider path (chat, model list, model test) deliberately does NOT go +through here: with the switch on the user still talks to the model, but the +app fetches nothing else on the model's or its own behalf. + +The module holds a *reader*, not a copy of the flag: the Composition Root +binds it to the live config once (``presentation/shell/bootstrap.py``), so a +change saved in Settings takes effect on the very next call. Nothing bound +(unit tests, helper subprocesses) means "not blocked", the pre-switch default. +""" +from __future__ import annotations + +import threading +from typing import Callable, Optional + +_lock = threading.Lock() +_reader: Optional[Callable[[], bool]] = None + + +class NetworkBlockedError(PermissionError): + """Raised by :func:`ensure_allowed` while the switch is on.""" + + +def bind(reader: Optional[Callable[[], bool]]) -> None: + """Install the callable that says whether the network is blocked right now.""" + global _reader + with _lock: + _reader = reader + + +def is_blocked() -> bool: + """True while "Block network" is on. A failing reader counts as blocked.""" + reader = _reader + if reader is None: + return False + try: + return bool(reader()) + except Exception: # noqa: BLE001 - fail closed: an unreadable switch must not open the network + return True + + +def refusal(purpose: str) -> str: + """The message shown (to the user or the model) when ``purpose`` is refused.""" + return (f"{purpose}: network access is blocked by the Sandbox Security Layer " + "(\"Block network\" is on in Settings). Only the AI provider may be reached.") + + +def ensure_allowed(purpose: str) -> None: + """Raise :class:`NetworkBlockedError` if ``purpose`` may not go online now.""" + if is_blocked(): + raise NetworkBlockedError(refusal(purpose)) + + +__all__ = ["NetworkBlockedError", "bind", "ensure_allowed", "is_blocked", "refusal"] diff --git a/application/workspaces/file_preview_helpers.py b/application/workspaces/file_preview_helpers.py index 3f1c78f..868db5c 100644 --- a/application/workspaces/file_preview_helpers.py +++ b/application/workspaces/file_preview_helpers.py @@ -21,9 +21,14 @@ def pptx_available() -> bool: try: from cowork_local.core.deps import ensure_module - _PPTX_READY = ensure_module("pptx", "python-pptx") is not None + ready = ensure_module("pptx", "python-pptx") is not None except Exception: # noqa: BLE001 - _PPTX_READY = False + ready = False + from ..network import network_guard + + if ready or not network_guard.is_blocked(): + _PPTX_READY = ready # a refusal under "Block network" is retried later + return ready return _PPTX_READY diff --git a/config.py b/config.py index e40be0c..b05951e 100644 --- a/config.py +++ b/config.py @@ -100,11 +100,11 @@ DEFAULT_CONFIG: Dict[str, Any] = { "resource_limit_cpu_percent": 80, # 0 = unlimited; caps a run_command/install_package process TREE's total CPU% "resource_limit_memory_mb": 2048, # 0 = unlimited; caps total RSS memory (MB) "resource_limit_disk_mb": 512, # 0 = unlimited; caps total disk read+write (MB) - # Cut the agent off the network: proxy env pointed at a black hole for - # agent-run shell commands, PLUS a flat refusal from every tool tagged - # ToolCapability.NETWORK (fetch_url, jira_*, install_package) — those - # reach the net in-process, where the proxy trick has nothing to act on. - "block_network": True, + # "Block network": every outbound connection except the AI provider is + # refused (application/network/network_guard.py), and agent shell + # commands / task scripts run in a network-less AppContainer. + # OFF on first launch — the user turns it on in Settings. + "block_network": False, # Allow the agent's fetch_url tool to read web pages / online documents / # SharePoint-OneDrive share links. Its own toggle — reading a URL for info # is safe and useful, so this defaults ON — but block_network outranks it: diff --git a/core/chat_agent.py b/core/chat_agent.py index 9c28ef7..4cdb452 100644 --- a/core/chat_agent.py +++ b/core/chat_agent.py @@ -527,6 +527,15 @@ def run_cowork( preview = {"kind": "info", "title": name, "text": str(args)} emit({"type": "tool_proposed", "id": tc_id, "name": name, "args": args, "preview": preview}) + if ctx.block_network and not name.startswith("ms365_local__"): + result = {"ok": False, "output": ( + f"{name}: network access is blocked by the Sandbox Security Layer " + '("Block network for agent-run commands" is on in Settings).')} + emit({"type": "tool_result", "id": tc_id, "name": name, + "ok": False, "output": result["output"]}) + messages.append({"role": "tool", "tool_call_id": tc_id, "name": name, + "content": result["output"]}) + continue # R05-T04: MCP/connector tools used to run with NO permission # check at all — this is what closes that gap. Same policy, # same gate object as the built-in tools below. diff --git a/core/code_agent.py b/core/code_agent.py index 24752cb..f65259d 100644 --- a/core/code_agent.py +++ b/core/code_agent.py @@ -327,7 +327,12 @@ def run_code( else: emit({"type": "tool_start", "id": tc_id, "name": name}) if is_extra and extra_executor is not None: - result = extra_executor(name, args) + if ctx.block_network and not name.startswith("ms365_local__"): + result = {"ok": False, "output": ( + f"{name}: network access is blocked by the Sandbox Security Layer " + '("Block network for agent-run commands" is on in Settings).')} + else: + result = extra_executor(name, args) else: def on_output(line: str, _id=tc_id, _name=name) -> None: emit({"type": "tool_output", "id": _id, "name": _name, "delta": line}) diff --git a/core/deps.py b/core/deps.py index 8ec5d10..dfc99c7 100644 --- a/core/deps.py +++ b/core/deps.py @@ -91,6 +91,7 @@ def run_cancellable( on_output: Optional[Callable[[str], None]] = None, env: Optional[Dict[str, str]] = None, limits: Optional[Dict[str, float]] = None, + isolate_network: bool = False, ) -> Tuple[Optional[int], str, bool, bool, bool]: """Run a subprocess so the Stop button can actually interrupt it. @@ -117,17 +118,27 @@ def run_cancellable( a failure to create/assign the job just means the existing taskkill fallback is used, same as before this was added. + ``isolate_network`` runs ``args`` as a shell command that the OS keeps + off the network (see ``infrastructure/sandbox/network_isolation.py``); + if that isolation cannot be set up the command is NOT run. + Returns ``(returncode, combined_output, cancelled, timed_out, resource_exceeded)``; on a failure to even launch the process, ``returncode`` is ``None`` and the output holds the launch error.""" cancel = cancel or (lambda: False) popen_kwargs = {} if sys.platform == "win32" else {"start_new_session": True} try: - proc = subprocess.Popen( - args, shell=shell, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - text=True, bufsize=1, env=env, **popen_kwargs, - ) - except OSError as exc: + if isolate_network: + from ..infrastructure.sandbox.network_isolation import spawn_without_network + + command = args if isinstance(args, str) else subprocess.list2cmdline(args) + proc = spawn_without_network(command, cwd, env) + else: + proc = subprocess.Popen( + args, shell=shell, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + text=True, bufsize=1, env=env, **popen_kwargs, + ) + except (OSError, RuntimeError) as exc: # RuntimeError: NetworkIsolationUnavailable return None, str(exc), False, False, False with _active_pids_lock: @@ -266,6 +277,10 @@ def ensure_module(module: str, package: str | None = None): pkg = package or module if pkg in _FAILED or not _can_pip(): return None + from ..application.network import network_guard + + if network_guard.is_blocked(): + return None # not cached in _FAILED: retried once the network is back ok, _ = pip_install(pkg) if not ok: _FAILED.add(pkg) @@ -339,6 +354,10 @@ def pip_install(package: str, cancel: Optional[CancelFn] = None, name) is NOT retried, since repeating it would just waste time.""" if not _can_pip(): return False, "This packaged build can't install packages at runtime." + from ..application.network import network_guard + + if network_guard.is_blocked(): + return False, network_guard.refusal(f"pip install {package}") exe = python or sys.executable attempt = 0 while True: diff --git a/core/doc_extract.py b/core/doc_extract.py index 04ffd74..1cbadb1 100644 --- a/core/doc_extract.py +++ b/core/doc_extract.py @@ -94,17 +94,28 @@ def find_input_files(folder: Path, exts: set[str] | None = None, capped at ``max_files`` (0 = unlimited), ``total_matched`` is the count before that cap, so a caller can report how many were skipped.""" exts = exts or INPUT_EXTS + # Do not sort an unbounded recursive tree merely to return a small prefix. + # The caller receives a stable lexical order for the bounded result, while + # traversal stops as soon as the configured file budget is reached. + files: list[Path] = [] + total = 0 try: - matched = sorted( - f for f in folder.rglob("*") - if f.is_file() - and not any(part.startswith(".") for part in f.relative_to(folder).parts) - and f.suffix.lower() in exts - ) + for f in folder.rglob("*"): + if not f.is_file(): + continue + try: + relative = f.relative_to(folder) + except ValueError: + continue + if any(part.startswith(".") for part in relative.parts) or f.suffix.lower() not in exts: + continue + total += 1 + if max_files <= 0 or len(files) < max_files: + files.append(f) except OSError: return [], 0 - files = matched if max_files <= 0 else matched[:max_files] - return files, len(matched) + files.sort(key=lambda p: str(p).lower()) + return files, total def find_soffice() -> str | None: diff --git a/core/ext_connectors.py b/core/ext_connectors.py index ea2cd27..5d4890e 100644 --- a/core/ext_connectors.py +++ b/core/ext_connectors.py @@ -132,8 +132,11 @@ class RestApiConnector: def call(self, args: Dict[str, Any]) -> Dict[str, Any]: """Gọi API theo tham số model đưa ra, đi qua lớp TLS có ghim chứng chỉ nội bộ.""" + from ..application.network import network_guard from .tls_trust import request_any_method as tls_request + if network_guard.is_blocked(): + return {"ok": False, "output": network_guard.refusal(self.display_name)} method = str(args.get("method", "GET")).upper() path = str(args.get("path", "")).lstrip("/") url = urljoin(self.base_url, path) @@ -164,10 +167,13 @@ class RestApiConnector: def test_connection(self) -> Tuple[bool, str]: """Thử kết nối tới endpoint; trả về (thành công, thông điệp).""" + from ..application.network import network_guard from .tls_trust import request as tls_request if not self.base_url.strip("/"): return False, "No base URL configured." + if network_guard.is_blocked(): + return False, network_guard.refusal(self.display_name) headers = {} if self.api_key: headers[self.auth_header] = ( diff --git a/core/history.py b/core/history.py index 5d14fc6..a9e31a3 100644 --- a/core/history.py +++ b/core/history.py @@ -16,6 +16,17 @@ from datetime import datetime from pathlib import Path from typing import Any, Dict, List +from ..performance import span + +_LIST_CACHE: dict[tuple[str, str, int], List[Dict[str, Any]]] = {} + + +def _invalidate_history_cache(directory: Path) -> None: + prefix = str(Path(directory).resolve()) + for key in list(_LIST_CACHE): + if key[0] == prefix: + _LIST_CACHE.pop(key, None) + def new_session_id() -> str: """Id phiên mới theo mốc thời gian, chính xác tới mili giây.""" @@ -78,6 +89,7 @@ def save_conversation( # R06-T02: atomic write - see infrastructure/persistence/json/atomic_write.py. from ..infrastructure.persistence.json.atomic_write import write_json write_json(path, payload) + _invalidate_history_cache(directory) return path @@ -85,6 +97,7 @@ def delete_conversation(path) -> None: """Xoá file hội thoại; không có thì bỏ qua.""" try: Path(path).unlink() + _invalidate_history_cache(Path(path).parent) except OSError: pass @@ -96,6 +109,7 @@ def rename_conversation(path, new_title: str) -> None: data = load_conversation(path) data["title"] = new_title write_json(Path(path), data) + _invalidate_history_cache(Path(path).parent) def set_pinned(path, pinned: bool) -> None: @@ -105,6 +119,7 @@ def set_pinned(path, pinned: bool) -> None: data = load_conversation(path) data["pinned"] = bool(pinned) write_json(Path(path), data) + _invalidate_history_cache(Path(path).parent) def load_conversation(path: Path) -> Dict[str, Any]: @@ -197,8 +212,16 @@ def list_conversations(directory: Optional[Path] = None, query: str = "") -> Lis if not directory or not directory.exists(): return [] q = (query or "").strip().lower() + try: + cache_key = (str(directory.resolve()), q, directory.stat().st_mtime_ns) + except OSError: + return [] + cached = _LIST_CACHE.get(cache_key) + if cached is not None: + return [dict(item) for item in cached] items: List[Dict[str, Any]] = [] - for path in directory.glob("*.json"): + with span("history.list", query=bool(q)): + for path in directory.glob("*.json"): try: data = json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): @@ -221,4 +244,10 @@ def list_conversations(directory: Optional[Path] = None, query: str = "") -> Lis }) # pinned first, then most recent items.sort(key=lambda d: (not d["pinned"], -d["mtime"])) + _LIST_CACHE[cache_key] = [dict(item) for item in items] + # Keep this bounded; old directory signatures become unreachable after a + # write and should not grow process memory forever. + if len(_LIST_CACHE) > 256: + for old in list(_LIST_CACHE)[:64]: + _LIST_CACHE.pop(old, None) return items diff --git a/core/jira_tool.py b/core/jira_tool.py index ef5db00..167724c 100644 --- a/core/jira_tool.py +++ b/core/jira_tool.py @@ -85,8 +85,10 @@ def get_issue_by_url(config: Dict[str, Any] | None, url: str) -> str: def _get(config: Dict[str, Any], path: str, params: dict = None): """Gọi Jira REST API bằng xác thực cơ bản, qua lớp TLS có ghim chứng chỉ nội bộ.""" + from ..application.network import network_guard from . import tls_trust + network_guard.ensure_allowed("Jira") c = _conf(config) url = c["base_url"].rstrip("/") + path # Same TLS auto-recovery the LLM provider calls get (core/tls_trust.py) — diff --git a/core/link_fetch.py b/core/link_fetch.py index 455e5b4..0344e31 100644 --- a/core/link_fetch.py +++ b/core/link_fetch.py @@ -147,6 +147,12 @@ def fetch_link_preview(url: str) -> str: return "" if not re.match(r"^https?://", url, re.IGNORECASE): return f"[Link: {url}] (not a fetchable http(s) URL — referenced by address only)" + # Every caller (fetch_url, task link attachments, ...) passes through here, + # so this one check covers the paths that never saw a ToolContext. + from ..application.network import network_guard + + if network_guard.is_blocked(): + return f"[Link: {url}] (not fetched — {network_guard.refusal('link fetch')})" # SharePoint / OneDrive share links are rewritten to their direct-download # form so the shared FILE itself is fetched and parsed (like an attachment), # not the share page's HTML shell. diff --git a/core/mcp_client.py b/core/mcp_client.py index aa4c9de..37bf990 100644 --- a/core/mcp_client.py +++ b/core/mcp_client.py @@ -88,7 +88,14 @@ class McpServerConnection: def start(self, timeout: float = 15.0) -> None: """Spawn the server subprocess and complete the MCP handshake. Raises :class:`McpServerError` on failure (bad command, the server - crashed on startup, the handshake timed out, ...).""" + crashed on startup, the handshake timed out, ...). + + Refused while "Block network" is on: a server process is free to open + any socket it likes, so the only safe server is one never started.""" + from ..application.network import network_guard + + if network_guard.is_blocked(): + raise McpServerError(network_guard.refusal(f"MCP server '{self.name}'")) self._thread = threading.Thread(target=self._run_loop, daemon=True) self._thread.start() if not self._ready.wait(timeout): @@ -174,6 +181,10 @@ class McpServerConnection: def call_tool(self, qualified_name: str, args: Dict[str, Any]) -> Dict[str, Any]: """``extra_executor``-shaped result: ``{"ok": bool, "output": str}``.""" + from ..application.network import network_guard + + if network_guard.is_blocked(): + return {"ok": False, "output": network_guard.refusal(f"MCP server '{self.name}'")} tool_name = qualified_name.split(_SEP, 1)[1] if _SEP in qualified_name else qualified_name try: result = self._run_coro(self._session.call_tool(tool_name, args or {})) diff --git a/core/ms365_auth.py b/core/ms365_auth.py index 693640e..0a7fd88 100644 --- a/core/ms365_auth.py +++ b/core/ms365_auth.py @@ -137,8 +137,34 @@ def _app(tenant_id: str, client_id: str): return app, cache +def _cached_account_offline() -> Optional[dict]: + """First account in the saved token cache, read without any MSAL network setup.""" + try: + import msal + + accounts = _load_cache().find(msal.TokenCache.CredentialType.ACCOUNT) + except Exception: # noqa: BLE001 - no msal / unreadable cache = not signed in + return None + return accounts[0] if accounts else None + + +def _ensure_network(action: str) -> None: + """Turn a "Block network" refusal into the error type callers already handle.""" + from ..application.network import network_guard + + if network_guard.is_blocked(): + raise Ms365AuthError(network_guard.refusal(action)) + + def signed_in_account(tenant_id: str, client_id: str) -> Optional[dict]: """The cached account, if any — a local cache lookup, no network call.""" + from ..application.network import network_guard + + if network_guard.is_blocked(): + # Building the MSAL app fetches the tenant's OpenID configuration, so + # read the token cache directly instead: the UI still sees who is + # signed in without the app reaching login.microsoftonline.com. + return _cached_account_offline() try: app, _cache = _app(tenant_id, client_id) except Ms365AuthError: @@ -156,6 +182,7 @@ def sign_in_device_code(tenant_id: str, client_id: str, on_code: Callable[[dict] ``verification_uri_complete`` (URL with the code pre-filled, when the tenant returns it) and ``message`` (the full human-readable instruction). Returns the MSAL token result dict; raises Ms365AuthError on failure/timeout.""" + _ensure_network("Microsoft 365 sign-in") app, cache = _app(tenant_id, client_id) flow = app.initiate_device_flow(scopes=SCOPES) if "user_code" not in flow: @@ -183,6 +210,7 @@ def get_access_token(tenant_id: str, client_id: str) -> str: """Silently reuse the cached sign-in. Raises Ms365AuthError when there is no valid session — the caller (a Graph call) should surface that as a normal tool failure telling the user to sign in again from Settings.""" + _ensure_network("Microsoft 365") app, cache = _app(tenant_id, client_id) accounts = app.get_accounts() if not accounts: @@ -228,6 +256,7 @@ def sign_out_default(config=None) -> None: def sign_out(tenant_id: str, client_id: str) -> None: """Đăng xuất và xoá token của một tenant/client khỏi kho.""" try: + _ensure_network("Microsoft 365 sign-out") # chặn mạng: chỉ xoá kho token bên dưới app, cache = _app(tenant_id, client_id) for acc in app.get_accounts(): app.remove_account(acc) diff --git a/core/ms365_graph.py b/core/ms365_graph.py index 402ae16..1b00012 100644 --- a/core/ms365_graph.py +++ b/core/ms365_graph.py @@ -43,6 +43,10 @@ def _request(method: str, url: str, token: str, **kwargs) -> requests.Response: """Gọi Graph API, tự ghép ``GRAPH_BASE`` cho đường dẫn tương đối và đổi lỗi HTTP thành :class:`Ms365GraphError` kèm thông điệp đọc được. """ + from ..application.network import network_guard + + if network_guard.is_blocked(): + raise Ms365GraphError(network_guard.refusal("Microsoft 365 (Graph)")) if not url.startswith("http"): url = f"{GRAPH_BASE}{url}" headers = _headers(token, kwargs.pop("headers", None)) diff --git a/core/projects.py b/core/projects.py index f6bef6b..98a90a8 100644 --- a/core/projects.py +++ b/core/projects.py @@ -24,6 +24,7 @@ project — nothing about it is special-cased in the UI. from __future__ import annotations import json +import os import re from dataclasses import asdict, dataclass, field from datetime import datetime @@ -82,6 +83,53 @@ class Project: return (base or WORKSPACES_DIR) / self.project_id +def _norm_dir(path) -> str: + """Đường dẫn đã chuẩn hoá để đem ra so sánh. + + Bung ``~``, đưa về tuyệt đối, rồi ``normcase`` — trên Windows thì + ``D:/Work`` và ``d:/work`` là cùng một thư mục, nên so chuỗi thô sẽ + cho hai project chiếm chung một chỗ mà không ai biết. + """ + return os.path.normcase(os.path.abspath(os.path.expanduser(str(path)))) + + +def _cham_nhau(a: str, b: str) -> bool: + """Hai thư mục đã chuẩn hoá có chạm nhau không: trùng, hoặc lồng nhau. + + Lồng nhau cũng tính, vì lý do tồn tại của sandbox là "agent của project này + không bao giờ chạm được file của project kia" (xem docstring đầu module). + Đứng ở thư mục cha thì đọc/ghi được toàn bộ thư mục con, nên cha-con vẫn là + chạm nhau dù hai đường dẫn không giống nhau. + """ + return a == b or a.startswith(b + os.sep) or b.startswith(a + os.sep) + + +def folder_conflict(path, *, ignore_id: str = "", + directory: Path = None) -> Optional[Project]: + """Project khác đang chiếm ``path``, hoặc ``None`` nếu chưa ai chiếm. + + Mỗi thư mục chỉ được thuộc về một project: thư mục làm việc vừa là sandbox + vừa là kho kiến thức dùng chung của project, nên hai project dùng chung một + thư mục là đọc lẫn dữ liệu của nhau. + + So theo thư mục THỰC SỰ đang dùng (``workspace_dir()``), không phải theo + ``output_dir``: project chưa đặt thư mục riêng vẫn đang chiếm thư mục quản + lý sẵn của nó, và chính thư mục đó là thứ hay bị chọn nhầm. + + ``ignore_id`` là project đang sửa — giữ nguyên thư mục của chính nó thì + không phải là trùng. + """ + if not str(path).strip(): + return None + muon = _norm_dir(path) + for project in list_projects(directory): + if project.project_id == ignore_id: + continue + if _cham_nhau(muon, _norm_dir(project.workspace_dir())): + return project + return None + + def _starter_project() -> Project: """An ordinary (deletable, renamable) project seeded when the projects folder is empty, so the app always opens with somewhere to chat.""" diff --git a/core/sandbox_manager.py b/core/sandbox_manager.py index 6264106..0a4c665 100644 --- a/core/sandbox_manager.py +++ b/core/sandbox_manager.py @@ -218,8 +218,13 @@ class SandboxManager: timeout_sec: int, cancel: Optional[Callable[[], bool]] = None, ) -> Dict[str, Any]: - """Dispatch execution to the selected backend.""" - if backend == "direct": + """Dispatch execution to the selected backend. + + With the network blocked every backend is replaced by the same OS-level + isolation: the backends below only ever set proxy env vars, which + anything that ignores proxies (raw sockets, ping, .NET WebClient...) + walked straight past.""" + if block_network or backend == "direct": return self._run_direct(command, workdir, block_network, timeout_sec, cancel) if backend == "integrity_job_wfp": @@ -274,7 +279,8 @@ class SandboxManager: env = os.environ.copy() if block_network: from .deps import network_blocked_env - env = network_blocked_env(env) + env = network_blocked_env(env) # belt and braces on top of the OS block + return self._run_network_isolated(command, workdir, env, timeout_sec, cancel) if cancel is not None: from .deps import run_cancellable @@ -334,4 +340,42 @@ class SandboxManager: "stderr": str(exc), "returncode": -1, "sandbox": "direct", - } \ No newline at end of file + } + + @staticmethod + def _run_network_isolated( + command: str, + workdir: str, + env: Dict[str, str], + timeout_sec: int, + cancel: Optional[Callable[[], bool]] = None, + ) -> Dict[str, Any]: + """Run ``command`` in a process the OS keeps off the network. + + Fail-closed: when the isolation cannot be set up the command is + refused (``sandbox == "blocked"``), never run with the network open.""" + from .deps import run_cancellable + + rc, output, cancelled, timed_out, exceeded = run_cancellable( + command, cwd=workdir or None, timeout=timeout_sec, cancel=cancel, + shell=True, env=env, isolate_network=True, + ) + if rc is None and not (cancelled or timed_out or exceeded): + return {"ok": False, "stdout": "", "returncode": -1, "sandbox": "blocked", + "stderr": ("Command refused: network is blocked and the command could " + f"not be isolated from the network ({output.strip()}).")} + if cancelled: + stderr = "Cancelled by user." + elif timed_out: + stderr = f"Timeout after {timeout_sec}s" + elif exceeded: + stderr = "Resource limit exceeded." + else: + stderr = "" + return { + "ok": rc == 0 and not (cancelled or timed_out or exceeded), + "stdout": output, + "stderr": stderr, + "returncode": rc if rc is not None else -1, + "sandbox": "network_isolated", + } diff --git a/core/task_executors.py b/core/task_executors.py index 57de9a9..1147f10 100644 --- a/core/task_executors.py +++ b/core/task_executors.py @@ -19,7 +19,6 @@ in Waiting Input), so executors here run with an auto gate. """ from __future__ import annotations -import subprocess import time import uuid from datetime import datetime @@ -30,6 +29,7 @@ from . import agent_roles from . import agent_security from . import projects from .permissions import PermissionGate +from .task_script import run_script as _run_script from .tasks import ARTIFACTS_DIR, resolve_input_text from .tools import ToolContext @@ -358,18 +358,6 @@ def _run_agent(ctx, task_type: str, prompt: str, out_dir: Path, return _last_assistant_text(messages), timed_out(), incomplete -def _run_script(command: str, out_dir: Path, timeout_sec: int) -> str: - """Chạy một task kiểu script bằng shell trong thư mục kết quả, có hạn giờ.""" - if not command.strip(): - raise RuntimeError("Script task has no command configured.") - proc = subprocess.run(command, shell=True, cwd=str(out_dir), - capture_output=True, text=True, timeout=max(1, timeout_sec)) - output = (proc.stdout or "") + (("\n[stderr]\n" + proc.stderr) if proc.stderr else "") - if proc.returncode != 0: - raise RuntimeError(f"Script exited with code {proc.returncode}:\n{output[-2000:]}") - return output - - def execute_task(ctx, task: Dict[str, Any], run_id: str, emit: Optional[EmitFn] = None, cancel: Optional[CancelFn] = None, tasks_dir: Path = None) -> Dict[str, Any]: diff --git a/core/task_script.py b/core/task_script.py new file mode 100644 index 0000000..213ac59 --- /dev/null +++ b/core/task_script.py @@ -0,0 +1,47 @@ +"""Run a scheduled task of type ``script`` (tách khỏi ``task_executors.py``). + +Khi công tắc "Chặn mạng" đang bật, lệnh của task chạy trong tiến trình bị hệ +điều hành cắt mạng — giống ``run_command`` của agent. Trước đây task script +chạy thẳng bằng ``subprocess.run``, không sandbox, nên lên mạng tự do. +""" +from __future__ import annotations + +import subprocess +from pathlib import Path + + +def run_script(command: str, out_dir: Path, timeout_sec: int) -> str: + """Chạy một task kiểu script bằng shell trong thư mục kết quả, có hạn giờ.""" + if not command.strip(): + raise RuntimeError("Script task has no command configured.") + from ..application.network import network_guard + + if network_guard.is_blocked(): + return _run_script_without_network(command, out_dir, timeout_sec) + proc = subprocess.run(command, shell=True, cwd=str(out_dir), + capture_output=True, text=True, timeout=max(1, timeout_sec)) + output = (proc.stdout or "") + (("\n[stderr]\n" + proc.stderr) if proc.stderr else "") + if proc.returncode != 0: + raise RuntimeError(f"Script exited with code {proc.returncode}:\n{output[-2000:]}") + return output + + +def _run_script_without_network(command: str, out_dir: Path, timeout_sec: int) -> str: + """Như :func:`run_script`, nhưng tiến trình không có mạng; không cô lập được thì không chạy.""" + from .deps import network_blocked_env, run_cancellable + + rc, output, _cancelled, timed_out, _exceeded = run_cancellable( + command, cwd=str(out_dir), timeout=max(1, timeout_sec), shell=True, + env=network_blocked_env(), isolate_network=True, + ) + if timed_out: + raise subprocess.TimeoutExpired(command, timeout_sec) + if rc is None: + raise RuntimeError("Script not run: network is blocked and the script could not be " + f"isolated from the network ({output.strip()}).") + if rc != 0: + raise RuntimeError(f"Script exited with code {rc} (network blocked):\n{output[-2000:]}") + return output + + +__all__ = ["run_script"] diff --git a/core/teams.py b/core/teams.py index fd3fe92..578cc0e 100644 --- a/core/teams.py +++ b/core/teams.py @@ -43,6 +43,10 @@ class TeamsNotifier: """Post a notification. Returns ``(ok, detail)``.""" if not self.configured: return False, "Teams webhook URL is not configured." + from ..application.network import network_guard + + if network_guard.is_blocked(): + return False, network_guard.refusal("Teams notification") # Workflows webhooks expect an Adaptive Card; classic connectors expect a # MessageCard. Try both, then a plain-text fallback. diff --git a/docs/PERFORMANCE_CHECKPOINT.md b/docs/PERFORMANCE_CHECKPOINT.md new file mode 100644 index 0000000..5d3311d --- /dev/null +++ b/docs/PERFORMANCE_CHECKPOINT.md @@ -0,0 +1,23 @@ +Source HEAD: db80289 (preserved) +Branch: perf/fsg-performance + +Baseline: app import 1517ms; config 934ms; MainWindow 1742ms (offscreen, local machine). + +Packets completed: +- P0: measured constructor with cProfile; dominant cost was provider model discovery (~0.7s network worker) and eager Workspace composition. +- P2: cache history listing by directory mtime/query and coalesce sidebar refresh bursts. +- P3: batch streaming Markdown/layout renders at 40ms; final content remains intact. +- P4: bounded attachment discovery avoids sorting a full recursive tree when a cap is set. +- P5: instrument monitoring log refresh; existing 30-day bounded window retained. +- P6: defer provider model discovery to the first Qt event-loop turn. + +After: config 452ms; MainWindow 599ms in the same offscreen smoke benchmark (discovery no longer blocks construction). +Streaming render count is now bounded by batch cadence rather than token count. +Representative history benchmark: 1,000 files 383.6ms cold / 0.8ms cached on this machine. + +Relevant commits: c5cb258 (perf: defer discovery and reduce UI refresh work). +Remaining bottleneck: eager Workspace/Co4E/Folder widget construction and import-time PySide6 overhead. + +Closure pass (starting HEAD 58b5220): Workspace now keeps Co4E, Folder, and GraphRAG as tab placeholders and creates each once on first selection. MainWindow benchmark: 357.6ms; first opens Co4E 143.1ms, Folder 148.0ms, GraphRAG 270.6ms; repeat opens 0.0–2.4ms. Focused lazy navigation/project tests: 15 passed. Pytest temp failures were ACL/path setup issues, not production assertions; a pre-created writable repository-local temp base allowed the focused gates to pass. +Remaining startup cost is base PySide6/application import and eager Cowork shell; further lazy work is not justified without broader architectural risk. +Performance initiative status: closed for this pass. diff --git a/i18n/settings_dialog.py b/i18n/settings_dialog.py index 2544101..723aa40 100644 --- a/i18n/settings_dialog.py +++ b/i18n/settings_dialog.py @@ -19,13 +19,13 @@ STRINGS: Dict[str, Dict[str, str]] = { "vi": "Cho phép agent lấy dữ liệu từ URL (trang web, link SharePoint / OneDrive)"}, "settings.allow_url_fetch_tooltip": { "en": ("Lets the agent's fetch_url tool read web pages, online documents and " - "SharePoint/OneDrive share links to search & process them. Separate from " - "'Block network' (which only sandboxes shell commands). Default: on."), + "SharePoint/OneDrive share links to search & process them. 'Block network' " + "overrides this switch. Default: on."), "ja": "エージェントのfetch_urlツールがWebページ・オンライン文書・SharePoint/OneDrive共有リンクを" - "読み取れるようにします。「ネットワークをブロック」(シェルコマンド用)とは別です。既定: オン。", + "読み取れるようにします。「ネットワークをブロック」がオンの場合はそちらが優先されます。既定: オン。", "vi": ("Cho phép tool fetch_url của agent đọc trang web, tài liệu online và link chia sẻ " - "SharePoint/OneDrive để tìm kiếm & xử lý. Tách biệt với 'Chặn mạng' (chỉ áp cho lệnh " - "shell). Mặc định: bật.")}, + "SharePoint/OneDrive để tìm kiếm & xử lý. 'Chặn mạng' được ưu tiên hơn công tắc " + "này. Mặc định: bật.")}, "settings.test_internet": { "en": "Test Internet", "ja": "インターネット接続テスト", "vi": "Kiểm tra Internet"}, "settings.test_internet_tooltip": { @@ -39,12 +39,18 @@ STRINGS: Dict[str, Dict[str, str]] = { "en": "Testing internet access…", "ja": "インターネット接続をテスト中…", "vi": "Đang kiểm tra truy cập internet…"}, "settings.sandbox_block_network_tooltip": { - "en": ("Policy-level control (proxy env vars point at a black hole) — not a kernel " - "firewall. Combine with the command whitelist above for defense in depth."), - "ja": "ポリシーレベルの制御です(プロキシ環境変数をブラックホールに向ける)— カーネルレベルの" - "ファイアウォールではありません。上のコマンドホワイトリストと併用してください。", - "vi": "Kiểm soát ở tầng chính sách (trỏ biến môi trường proxy vào hố đen) — không phải " - "firewall tầng kernel. Kết hợp với whitelist lệnh ở trên để phòng thủ nhiều lớp."}, + "en": ("Only the AI provider (chat, model list, model test) may reach the network. " + "Agent shell commands and task scripts run in a Windows AppContainer with no " + "network access; fetch_url, Jira, connectors/MCP, Microsoft 365, Teams, " + "connector tests, pip auto-install and remote content in HTML previews are refused."), + "ja": "ネットワークに接続できるのはAIプロバイダー(チャット・モデル一覧・モデルテスト)のみです。" + "エージェントのシェルコマンドとタスクスクリプトはネットワークなしのWindows AppContainerで実行され、" + "fetch_url・Jira・コネクタ/MCP・Microsoft 365・Teams・接続テスト・pip自動インストール・" + "HTMLプレビューの外部リソースは拒否されます。", + "vi": "Chỉ nhà cung cấp AI (chat, tải danh sách model, thử model) được ra mạng. Lệnh shell " + "của agent và task script chạy trong Windows AppContainer không có mạng; fetch_url, " + "Jira, connector/MCP, Microsoft 365, Teams, nút Test, tự cài thư viện và tài nguyên " + "web trong xem trước HTML đều bị từ chối."}, "settings.sandbox_unlimited": {"en": "Unlimited", "ja": "無制限", "vi": "Không giới hạn"}, "settings.sandbox_cpu_label": {"en": "CPU limit", "ja": "CPU 制限", "vi": "Giới hạn CPU"}, "settings.sandbox_memory_label": {"en": "Memory limit", "ja": "メモリ制限", "vi": "Giới hạn bộ nhớ"}, diff --git a/i18n/sidebar.py b/i18n/sidebar.py index de94a5c..49d26d5 100644 --- a/i18n/sidebar.py +++ b/i18n/sidebar.py @@ -57,6 +57,17 @@ STRINGS: Dict[str, Dict[str, str]] = { "en": "Another project is already called \"{name}\". Project names must be unique — the list shows nothing but the name, so two of them cannot be told apart.", "ja": "「{name}」という名前のプロジェクトが既にあります。一覧には名前しか出ないため、同じ名前が二つあると区別できません。", "vi": "Đã có project khác tên \"{name}\". Tên project phải khác nhau — danh sách chỉ hiện tên, trùng tên là không phân biệt được."}, + "workspace.folder_taken_title": { + "en": "Folder already used", "ja": "フォルダーが重複しています", + "vi": "Thư mục đã được dùng"}, + "workspace.folder_taken_body": { + "en": "Project \"{name}\" already works in {folder}. One folder belongs to one project only — the folder is that project's sandbox and shared knowledge, so sharing it lets two projects read and overwrite each other's files. Pick another folder.", + "ja": "プロジェクト「{name}」が既に {folder} を使用しています。フォルダーは 1 つのプロジェクト専用です — フォルダーはそのプロジェクトのサンドボックス兼共有ナレッジなので、共有すると互いのファイルを読み書きしてしまいます。別のフォルダーを選んでください。", + "vi": "Project \"{name}\" đang làm việc trong {folder}. Mỗi thư mục chỉ thuộc về một project — thư mục vừa là sandbox vừa là kho kiến thức chung của project đó, dùng chung là hai project đọc và ghi đè file của nhau. Hãy chọn thư mục khác."}, + "workspace.folder_shared_warning": { + "en": "⚠ This folder is also used by project \"{name}\". One folder belongs to one project only — pick another folder for one of them.", + "ja": "⚠ このフォルダーはプロジェクト「{name}」でも使われています。フォルダーは 1 つのプロジェクト専用です — どちらかに別のフォルダーを指定してください。", + "vi": "⚠ Thư mục này đang được project \"{name}\" dùng chung. Mỗi thư mục chỉ thuộc về một project — hãy đổi thư mục cho một trong hai."}, "workspace.instructions_placeholder": { "en": "e.g. \"All answers in Vietnamese. We are building the X reporting tool; always follow the naming rules …\"", "ja": "例:「回答はすべて日本語で。X レポートツールを開発中。命名規則に従うこと …」", diff --git a/infrastructure/filesystem/command_tools.py b/infrastructure/filesystem/command_tools.py index 8d88e40..06f6a39 100644 --- a/infrastructure/filesystem/command_tools.py +++ b/infrastructure/filesystem/command_tools.py @@ -76,11 +76,11 @@ def run_command(ctx: ToolContext, args: Dict[str, Any], denial = "Command blocked by security policy: " + "; ".join(risk.reasons) return {"ok": False, "output": denial} - # Every sandbox backend's network block is a proxy-env-var trick (see - # core/deps.py::network_blocked_env) — it does nothing against a tool - # that reaches the network without an HTTP proxy (ping/ICMP, nslookup/ - # direct DNS, ssh/ftp/raw TCP...). Deny those BY NAME here instead, so - # "Chặn mạng cho lệnh do agent chạy" actually blocks them too. + # With the network blocked, SandboxManager runs the command in an OS-level + # network-less process (AppContainer on Windows — see + # infrastructure/sandbox/network_isolation.py). Tools that exist only to + # reach the network (ping, nslookup, ssh...) are still denied BY NAME + # first: the model gets a clear reason instead of a cryptic socket error. if ctx.block_network: bypass_tool = command_bypasses_network_proxy(command) if bypass_tool: diff --git a/infrastructure/sandbox/appcontainer_process.py b/infrastructure/sandbox/appcontainer_process.py new file mode 100644 index 0000000..04a8e61 --- /dev/null +++ b/infrastructure/sandbox/appcontainer_process.py @@ -0,0 +1,392 @@ +"""Spawn a shell command inside a Windows AppContainer with NO network capability. + +Why this exists +--------------- +The old "block network" for agent shell commands only pointed the proxy env +vars at a dead port (``core/deps.py::network_blocked_env``). Anything that +ignores proxies (``Invoke-WebRequest -NoProxy``, raw sockets, ``certutil``, +.NET ``WebClient``...) still reached the internet. An AppContainer token that +is granted no ``internetClient``/``privateNetworkClientServer`` capability is +refused by the kernel firewall for every outbound connection, loopback +included, no matter which tool makes it. No admin rights are needed. + +An AppContainer can only open files whose ACL admits its SID (or ALL +APPLICATION PACKAGES). System32 and Program Files already do; the workdir and +the app's own Python install do not, so :func:`spawn` grants the profile SID +access to those folders first (an extra ACE, nothing is removed). + +:class:`AppContainerProcess` quacks like ``subprocess.Popen`` for the subset +``core/deps.py::_run_cancellable_body`` uses (``pid``, ``stdout``/``stderr`` +text streams, ``poll``/``wait``/``kill``/``returncode``), so the Stop button, +timeouts, Job Objects and resource limits keep working unchanged. +""" +from __future__ import annotations + +import io +import locale +import os +import subprocess +import sys +import threading +from typing import Dict, Iterable, Optional + +PROFILE_NAME = "cowork_local.agent_netblock" +_IS_WINDOWS = sys.platform == "win32" + +if _IS_WINDOWS: + import ctypes + import msvcrt + from ctypes import wintypes + + _k32 = ctypes.WinDLL("kernel32", use_last_error=True) + _adv = ctypes.WinDLL("advapi32", use_last_error=True) + _uenv = ctypes.WinDLL("userenv", use_last_error=True) + + class _SECURITY_CAPABILITIES(ctypes.Structure): + _fields_ = [("AppContainerSid", ctypes.c_void_p), ("Capabilities", ctypes.c_void_p), + ("CapabilityCount", wintypes.DWORD), ("Reserved", wintypes.DWORD)] + + class _STARTUPINFOW(ctypes.Structure): + _fields_ = [("cb", wintypes.DWORD), ("lpReserved", wintypes.LPWSTR), + ("lpDesktop", wintypes.LPWSTR), ("lpTitle", wintypes.LPWSTR), + ("dwX", wintypes.DWORD), ("dwY", wintypes.DWORD), + ("dwXSize", wintypes.DWORD), ("dwYSize", wintypes.DWORD), + ("dwXCountChars", wintypes.DWORD), ("dwYCountChars", wintypes.DWORD), + ("dwFillAttribute", wintypes.DWORD), ("dwFlags", wintypes.DWORD), + ("wShowWindow", wintypes.WORD), ("cbReserved2", wintypes.WORD), + ("lpReserved2", ctypes.c_void_p), ("hStdInput", wintypes.HANDLE), + ("hStdOutput", wintypes.HANDLE), ("hStdError", wintypes.HANDLE)] + + class _STARTUPINFOEXW(ctypes.Structure): + _fields_ = [("StartupInfo", _STARTUPINFOW), ("lpAttributeList", ctypes.c_void_p)] + + class _PROCESS_INFORMATION(ctypes.Structure): + _fields_ = [("hProcess", wintypes.HANDLE), ("hThread", wintypes.HANDLE), + ("dwProcessId", wintypes.DWORD), ("dwThreadId", wintypes.DWORD)] + + class _SECURITY_ATTRIBUTES(ctypes.Structure): + _fields_ = [("nLength", wintypes.DWORD), ("lpSecurityDescriptor", ctypes.c_void_p), + ("bInheritHandle", wintypes.BOOL)] + + _uenv.CreateAppContainerProfile.restype = ctypes.c_long + _uenv.CreateAppContainerProfile.argtypes = [ + wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.LPCWSTR, ctypes.c_void_p, + wintypes.DWORD, ctypes.POINTER(ctypes.c_void_p)] + _uenv.DeriveAppContainerSidFromAppContainerName.restype = ctypes.c_long + _uenv.DeriveAppContainerSidFromAppContainerName.argtypes = [ + wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_void_p)] + _uenv.GetAppContainerFolderPath.restype = ctypes.c_long + _uenv.GetAppContainerFolderPath.argtypes = [ + wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_wchar_p)] + _adv.ConvertSidToStringSidW.restype = wintypes.BOOL + _adv.ConvertSidToStringSidW.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_wchar_p)] + _k32.InitializeProcThreadAttributeList.restype = wintypes.BOOL + _k32.InitializeProcThreadAttributeList.argtypes = [ + ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(ctypes.c_size_t)] + _k32.UpdateProcThreadAttribute.restype = wintypes.BOOL + _k32.UpdateProcThreadAttribute.argtypes = [ + ctypes.c_void_p, wintypes.DWORD, ctypes.c_size_t, ctypes.c_void_p, + ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p] + _k32.DeleteProcThreadAttributeList.argtypes = [ctypes.c_void_p] + _k32.CreatePipe.restype = wintypes.BOOL + _k32.CreatePipe.argtypes = [ctypes.POINTER(wintypes.HANDLE), ctypes.POINTER(wintypes.HANDLE), + ctypes.POINTER(_SECURITY_ATTRIBUTES), wintypes.DWORD] + _k32.SetHandleInformation.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD] + _k32.CreateProcessW.restype = wintypes.BOOL + _k32.CreateProcessW.argtypes = [ + wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p, wintypes.BOOL, + wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR, ctypes.POINTER(_STARTUPINFOEXW), + ctypes.POINTER(_PROCESS_INFORMATION)] + _k32.ResumeThread.argtypes = [wintypes.HANDLE] + _k32.WaitForSingleObject.restype = wintypes.DWORD + _k32.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD] + _k32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)] + _k32.TerminateProcess.argtypes = [wintypes.HANDLE, wintypes.UINT] + _k32.CloseHandle.argtypes = [wintypes.HANDLE] + _k32.GetStdHandle.restype = wintypes.HANDLE + _k32.OpenProcess.restype = wintypes.HANDLE + + _ALREADY_EXISTS = ctypes.c_long(0x800700B7).value + _PROC_THREAD_ATTRIBUTE_HANDLE_LIST = 0x00020002 + _PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES = 0x00020009 + _EXTENDED_STARTUPINFO_PRESENT = 0x00080000 + _CREATE_UNICODE_ENVIRONMENT = 0x00000400 + _CREATE_NO_WINDOW = 0x08000000 + _CREATE_SUSPENDED = 0x00000004 + _STARTF_USESTDHANDLES = 0x00000100 + _HANDLE_FLAG_INHERIT = 0x00000001 + _STILL_ACTIVE = 259 + +_profile_lock = threading.Lock() +_profile: Dict[str, object] = {} +_granted: set = set() + + +class NetworkIsolationUnavailable(RuntimeError): + """This machine cannot start a network-less process — callers must refuse to run.""" + + +def is_supported() -> bool: + """True on Windows builds that ship the AppContainer API (Windows 8+).""" + if not _IS_WINDOWS: + return False + try: + return bool(_uenv.CreateAppContainerProfile) + except AttributeError: + return False + + +def _profile_sid(): + """``(sid pointer, sid string, temp folder)`` of the shared no-network profile.""" + with _profile_lock: + if _profile: + return _profile["sid"], _profile["sid_str"], _profile["temp"] + sid = ctypes.c_void_p() + hr = _uenv.CreateAppContainerProfile(PROFILE_NAME, "Cowork Local agent (no network)", + "Agent shell commands with the network blocked", + None, 0, ctypes.byref(sid)) + if hr == _ALREADY_EXISTS: + hr = _uenv.DeriveAppContainerSidFromAppContainerName(PROFILE_NAME, ctypes.byref(sid)) + if hr != 0 or not sid.value: + raise NetworkIsolationUnavailable(f"AppContainer profile error 0x{hr & 0xFFFFFFFF:08X}") + text = ctypes.c_wchar_p() + if not _adv.ConvertSidToStringSidW(sid, ctypes.byref(text)): + raise NetworkIsolationUnavailable("Could not read the AppContainer SID") + sid_str = text.value + folder = ctypes.c_wchar_p() + temp = "" + if _uenv.GetAppContainerFolderPath(sid_str, ctypes.byref(folder)) == 0 and folder.value: + temp = os.path.join(folder.value, "Temp") + os.makedirs(temp, exist_ok=True) + _profile.update(sid=sid, sid_str=sid_str, temp=temp) + return sid, sid_str, temp + + +_PERMS = {"write": "(OI)(CI)(M)", "read": "(OI)(CI)(RX)", "read_here": "(OI)(NP)(RX)"} + + +def _qt_package_dir() -> str: + """Folder of the installed PySide6/Qt binaries ('' if PySide6 is absent).""" + try: + import importlib.util + + spec = importlib.util.find_spec("PySide6") + except (ImportError, ValueError): + return "" + return os.path.dirname(spec.origin) if spec and spec.origin else "" + + +def _covers(folder: str, target: str) -> bool: + """True if ``target`` is ``folder`` itself or lies somewhere below it.""" + if not folder or not target: + return False + folder, target = os.path.normcase(folder), os.path.normcase(target) + return target == folder or target.startswith(folder.rstrip("\\/") + os.sep) + + +def _icacls(*args: str) -> subprocess.CompletedProcess: + return subprocess.run(["icacls", *args], capture_output=True, text=True, + creationflags=_CREATE_NO_WINDOW) + + +def grant_access(path: str, sid_str: str, mode: str) -> None: + """Let the AppContainer SID open ``path``. + + ``mode`` is ``"write"``/``"read"`` (inherited by everything below) or + ``"read_here"`` (this folder and the files directly in it, no deeper). + + An inherited ACE must never reach the Qt WebEngine binaries: Chromium's + sandboxed render process then fails to load Qt6WebEngineCore.dll + (STATUS_DLL_NOT_FOUND) and every web view in the app goes blank. A folder + that contains the PySide6 install is therefore refused. + """ + path = os.path.abspath(path) + key = (os.path.normcase(path), mode) + if key in _granted or not os.path.exists(path): + return + if mode != "read_here" and _covers(path, _qt_package_dir()): + raise NetworkIsolationUnavailable( + f"Refusing to sandbox a folder that contains the app's Qt runtime: {path}") + perm = _PERMS[mode] + listing = (_icacls(path).stdout or "").lower() + if f"{sid_str}:{perm}".lower() not in listing: + done = _icacls(path, "/grant", f"*{sid_str}:{perm}", "/Q", "/C") + if done.returncode != 0: + raise NetworkIsolationUnavailable( + f"Could not grant the sandbox access to {path}: {(done.stderr or done.stdout).strip()}") + _granted.add(key) + + +def _repair_inherited_grant(path: str, sid_str: str) -> None: + """Drop an inherited grant that an earlier build put on the app's venv.""" + key = (os.path.normcase(os.path.abspath(path)), "repaired") + if key in _granted or not os.path.isdir(path): + return + listing = (_icacls(path).stdout or "").lower() + if f"{sid_str}:(oi)(ci)".lower() in listing: + _icacls(path, "/remove:g", f"*{sid_str}", "/Q", "/C") + _granted.add(key) + + +def _interpreter_grants(): + """``(folder, mode)`` pairs that let the sandbox run the app's Python. + + The base install is read-only and holds no Qt; a venv only needs its root + (``pyvenv.cfg``) and ``Scripts`` — never ``Lib/site-packages``. + """ + qt_dir = _qt_package_dir() + if _covers(sys.base_prefix, qt_dir): + # PySide6 lives in the base install itself: expose only the executable + # and the compiled stdlib modules, never the tree holding Qt. + grants = [(sys.base_prefix, "read_here"), (os.path.join(sys.base_prefix, "DLLs"), "read")] + else: + grants = [(sys.base_prefix, "read")] + if os.path.normcase(sys.prefix) != os.path.normcase(sys.base_prefix): + grants += [(sys.prefix, "read_here"), (os.path.join(sys.prefix, "Scripts"), "read")] + return [(folder, mode) for folder, mode in grants + if mode == "read_here" or not _covers(folder, qt_dir)] + + +def _env_block(env: Dict[str, str]) -> ctypes.Array: + """Sorted, double-NUL-terminated UTF-16 environment block for CreateProcessW.""" + items = sorted(env.items(), key=lambda kv: kv[0].upper()) + text = "".join(f"{k}={v}\0" for k, v in items if k and "=" not in k) + "\0" + return ctypes.create_unicode_buffer(text, len(text)) + + +def _pipe(): + """Anonymous pipe; only the child's (write) end is inheritable.""" + sa = _SECURITY_ATTRIBUTES(ctypes.sizeof(_SECURITY_ATTRIBUTES), None, True) + read, write = wintypes.HANDLE(), wintypes.HANDLE() + if not _k32.CreatePipe(ctypes.byref(read), ctypes.byref(write), ctypes.byref(sa), 0): + raise ctypes.WinError(ctypes.get_last_error()) + _k32.SetHandleInformation(read, _HANDLE_FLAG_INHERIT, 0) + return read, write + + +class AppContainerProcess: + """The ``subprocess.Popen`` subset that ``deps._run_cancellable_body`` relies on.""" + + def __init__(self, handle, pid: int, stdout: io.TextIOBase, stderr: io.TextIOBase): + """Wrap an already-started process handle and its two output streams.""" + self._handle = handle + self.pid = pid + self.stdout = stdout + self.stderr = stderr + self.returncode: Optional[int] = None + + def poll(self) -> Optional[int]: + """Exit code if the process has finished, else None.""" + if self.returncode is None and self._handle: + code = wintypes.DWORD() + if _k32.GetExitCodeProcess(self._handle, ctypes.byref(code)) and code.value != _STILL_ACTIVE: + self.returncode = ctypes.c_int32(code.value).value + _k32.CloseHandle(self._handle) + self._handle = None + return self.returncode + + def wait(self, timeout: Optional[float] = None) -> int: + """Block until exit; raise ``subprocess.TimeoutExpired`` like Popen does.""" + if self.returncode is None and self._handle: + ms = 0xFFFFFFFF if timeout is None else int(timeout * 1000) + if _k32.WaitForSingleObject(self._handle, ms) != 0: + raise subprocess.TimeoutExpired("appcontainer", timeout) + return self.poll() + + def kill(self) -> None: + """Terminate the process (the Job Object in deps kills its children).""" + if self.returncode is None and self._handle: + _k32.TerminateProcess(self._handle, 1) + + def communicate(self, timeout: Optional[float] = None): + """Read both streams to the end and wait; returns ``(stdout, stderr)``.""" + chunks: Dict[str, str] = {} + + def _drain(name, stream): + chunks[name] = stream.read() + + readers = [threading.Thread(target=_drain, args=(n, s), daemon=True) + for n, s in (("out", self.stdout), ("err", self.stderr))] + for t in readers: + t.start() + for t in readers: + t.join(timeout) + self.wait(timeout) + return chunks.get("out", ""), chunks.get("err", "") + + +def spawn(command: str, cwd: Optional[str], env: Optional[Dict[str, str]], + readable_dirs: Iterable[str] = ()) -> AppContainerProcess: + """Start ``cmd.exe /c command`` in the no-network AppContainer. + + Raises :class:`NetworkIsolationUnavailable` when that cannot be done — + callers must then refuse the command rather than run it with the network on. + """ + if not is_supported(): + raise NetworkIsolationUnavailable("AppContainer is only available on Windows") + sid, sid_str, temp = _profile_sid() + cwd = os.path.abspath(cwd or os.getcwd()) + _repair_inherited_grant(sys.prefix, sid_str) + grant_access(cwd, sid_str, "write") + for folder, mode in [*_interpreter_grants(), *((d, "read") for d in readable_dirs if d)]: + grant_access(folder, sid_str, mode) + + child_env = dict(os.environ if env is None else env) + if temp: + child_env["TEMP"] = child_env["TMP"] = temp + child_env.setdefault("PYTHONIOENCODING", "utf-8") + env_block = _env_block(child_env) + + out_r, out_w = _pipe() + err_r, err_w = _pipe() + handles = (wintypes.HANDLE * 2)(out_w, err_w) + caps = _SECURITY_CAPABILITIES(sid, None, 0, 0) + size = ctypes.c_size_t() + _k32.InitializeProcThreadAttributeList(None, 2, 0, ctypes.byref(size)) + attr = ctypes.create_string_buffer(size.value) + pi = _PROCESS_INFORMATION() + try: + if not (_k32.InitializeProcThreadAttributeList(attr, 2, 0, ctypes.byref(size)) + and _k32.UpdateProcThreadAttribute( + attr, 0, _PROC_THREAD_ATTRIBUTE_SECURITY_CAPABILITIES, ctypes.byref(caps), + ctypes.sizeof(caps), None, None) + and _k32.UpdateProcThreadAttribute( + attr, 0, _PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handles, + ctypes.sizeof(handles), None, None)): + raise NetworkIsolationUnavailable(str(ctypes.WinError(ctypes.get_last_error()))) + si = _STARTUPINFOEXW() + si.StartupInfo.cb = ctypes.sizeof(si) + si.StartupInfo.dwFlags = _STARTF_USESTDHANDLES + si.StartupInfo.hStdOutput = out_w + si.StartupInfo.hStdError = err_w + si.lpAttributeList = ctypes.addressof(attr) + comspec = os.environ.get("COMSPEC") or r"C:\Windows\System32\cmd.exe" + cmdline = ctypes.create_unicode_buffer(f'"{comspec}" /d /s /c "{command}"') + flags = (_EXTENDED_STARTUPINFO_PRESENT | _CREATE_UNICODE_ENVIRONMENT + | _CREATE_NO_WINDOW | _CREATE_SUSPENDED) + if not _k32.CreateProcessW(None, cmdline, None, None, True, flags, + ctypes.addressof(env_block), cwd, + ctypes.byref(si), ctypes.byref(pi)): + raise NetworkIsolationUnavailable( + f"Could not start the sandboxed command: {ctypes.WinError(ctypes.get_last_error())}") + _k32.ResumeThread(pi.hThread) + _k32.CloseHandle(pi.hThread) + except BaseException: + for h in (out_r, err_r): + _k32.CloseHandle(h) + raise + finally: + _k32.DeleteProcThreadAttributeList(attr) + _k32.CloseHandle(out_w) + _k32.CloseHandle(err_w) + + def _stream(handle) -> io.TextIOBase: + fd = msvcrt.open_osfhandle(handle.value, os.O_RDONLY) + return io.TextIOWrapper(io.FileIO(fd, "rb"), encoding=locale.getpreferredencoding(False), + errors="replace") + + return AppContainerProcess(pi.hProcess, pi.dwProcessId, _stream(out_r), _stream(err_r)) + + +__all__ = ["AppContainerProcess", "NetworkIsolationUnavailable", "PROFILE_NAME", + "grant_access", "is_supported", "spawn"] diff --git a/infrastructure/sandbox/network_isolation.py b/infrastructure/sandbox/network_isolation.py new file mode 100644 index 0000000..210e7d5 --- /dev/null +++ b/infrastructure/sandbox/network_isolation.py @@ -0,0 +1,46 @@ +"""Start a shell command that the operating system keeps off the network. + +Used whenever "Block network" is on for agent ``run_command`` calls and for +scheduled script tasks. The contract is fail-closed: if isolation cannot be +set up, :class:`NetworkIsolationUnavailable` is raised and the caller refuses +the command instead of running it with the network open. + +* Windows: an AppContainer with no network capability + (:mod:`.appcontainer_process`). +* macOS: ``sandbox-exec`` with a profile that denies every network operation. +* Linux: ``unshare --net`` in a new user namespace (an empty network namespace + has only a downed loopback). If unprivileged namespaces are disabled, + ``unshare`` itself fails and the command never runs. +""" +from __future__ import annotations + +import shutil +import subprocess +import sys +from typing import Dict, Optional + +from .appcontainer_process import NetworkIsolationUnavailable + +_MACOS_PROFILE = "(version 1)(allow default)(deny network*)" + + +def spawn_without_network(command: str, cwd: Optional[str], env: Optional[Dict[str, str]]): + """A ``Popen``-like process running ``command`` through the shell, with no network.""" + if sys.platform == "win32": + from . import appcontainer_process + + return appcontainer_process.spawn(command, cwd, env) + if sys.platform == "darwin" and shutil.which("sandbox-exec"): + argv = ["sandbox-exec", "-p", _MACOS_PROFILE, "/bin/sh", "-c", command] + elif sys.platform.startswith("linux") and shutil.which("unshare"): + argv = ["unshare", "--user", "--map-root-user", "--net", "/bin/sh", "-c", command] + else: + raise NetworkIsolationUnavailable( + "No network isolation is available on this system (needs AppContainer, " + "sandbox-exec or unshare).") + return subprocess.Popen(argv, cwd=cwd, env=env, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, bufsize=1, + start_new_session=True) + + +__all__ = ["NetworkIsolationUnavailable", "spawn_without_network"] diff --git a/performance.py b/performance.py new file mode 100644 index 0000000..1a5884b --- /dev/null +++ b/performance.py @@ -0,0 +1,31 @@ +"""Tiny opt-in performance tracing helpers. + +Tracing is disabled by default and emits only timings/counts, never prompts, +credentials, file contents, or provider payloads. +""" +from __future__ import annotations + +import logging +import os +import time +from contextlib import contextmanager + +_LOG = logging.getLogger("cowork.performance") + + +def enabled() -> bool: + return os.environ.get("COWORK_PERF_TRACE", "").strip().lower() in {"1", "true", "yes"} + + +@contextmanager +def span(name: str, **fields): + if not enabled(): + yield + return + started = time.perf_counter() + try: + yield + finally: + elapsed = (time.perf_counter() - started) * 1000.0 + safe = " ".join(f"{k}={v}" for k, v in fields.items()) + _LOG.info("perf %s %.1fms%s", name, elapsed, f" {safe}" if safe else "") diff --git a/presentation/chat/chat_agents.py b/presentation/chat/chat_agents.py index d280edb..a9a8ae0 100644 --- a/presentation/chat/chat_agents.py +++ b/presentation/chat/chat_agents.py @@ -115,10 +115,23 @@ class ChatAgentsMixin: if err: self.status_message.emit(tr("chatpanel.agent_list_error", err=err)) - w = AgentWorker(job) - w.finished_ok.connect(done) - self._agent_worker = w - w.start() + # Model discovery can involve a provider/network request. Constructing + # the chat panel during startup must not wait for it; schedule it after + # the first event-loop turn so the initial shell can paint immediately. + from PySide6.QtCore import QTimer + + if getattr(self, "_agent_refresh_pending", False): + return + self._agent_refresh_pending = True + + def start_worker() -> None: + self._agent_refresh_pending = False + w = AgentWorker(job) + w.finished_ok.connect(done) + self._agent_worker = w + w.start() + + QTimer.singleShot(0, start_worker) def _populate_agents(self, models, keep: str) -> None: """Đổ danh sách vào bộ chọn Agent. diff --git a/presentation/chat/chat_history_widget.py b/presentation/chat/chat_history_widget.py index b1898ee..223377d 100644 --- a/presentation/chat/chat_history_widget.py +++ b/presentation/chat/chat_history_widget.py @@ -51,6 +51,8 @@ class MessageBubble(QFrame): super().__init__() self.role = role self._text = "" + self._stream_pending = False + self._render_count = 0 self._collapsible = collapsible self._title = title self._head = None @@ -164,12 +166,20 @@ class MessageBubble(QFrame): def append_delta(self, delta: str) -> None: """Nối thêm một mẩu văn bản đang phát dần từ model rồi vẽ lại dạng markdown.""" self._text += delta - self.set_markdown(self._text) + if not self._stream_pending: + self._stream_pending = True + QTimer.singleShot(40, self.flush_stream) + + def flush_stream(self) -> None: + if self._stream_pending: + self._stream_pending = False + self.set_markdown(self._text) def set_markdown(self, text: str) -> None: """Đặt toàn bộ nội dung, hiển thị dạng markdown, rồi co giãn lại chiều cao.""" self._text = text self.body.setMarkdown(text) + self._render_count += 1 self._autosize() if self._collapsible: self._update_head() @@ -393,4 +403,3 @@ class ChatView(QScrollArea): ChatHistoryWidget = ChatView __all__ = ["ChatView", "ChatHistoryWidget", "MessageBubble"] - diff --git a/presentation/folder/office_document_renderer.py b/presentation/folder/office_document_renderer.py index ea6985d..3aef705 100644 --- a/presentation/folder/office_document_renderer.py +++ b/presentation/folder/office_document_renderer.py @@ -110,7 +110,10 @@ class OfficeDocumentRenderer: if self._engine is None: try: from PySide6.QtWebEngineWidgets import QWebEngineView + + from .offline_web_page import install_offline_page self._engine = QWebEngineView() + install_offline_page(self._engine) self._owner.stack.addWidget(self._engine) except Exception: # noqa: BLE001 self._engine = None diff --git a/presentation/folder/offline_web_page.py b/presentation/folder/offline_web_page.py new file mode 100644 index 0000000..31ba6b3 --- /dev/null +++ b/presentation/folder/offline_web_page.py @@ -0,0 +1,66 @@ +"""HTML preview page that loads nothing from the web while "Block network" is on. + +``QWebEngineView.setHtml`` happily fetches every ````, +``