diff --git a/calliope-backend/src/calliope/comfyui/client.py b/calliope-backend/src/calliope/comfyui/client.py index c18cbb2f..aa60c4a8 100644 --- a/calliope-backend/src/calliope/comfyui/client.py +++ b/calliope-backend/src/calliope/comfyui/client.py @@ -13,6 +13,11 @@ from calliope.comfyui.registry import IMAGE_CLASSES, AUDIO_CLASSES, VIDEO_CLASSE logger = logging.getLogger("calliope.comfyui") +# Reachability probe budget (HEL-619): matches the connections-page test +# probe (5s). Without it health() inherits the 120s client default, so a +# bad endpoint hangs every queued job for two minutes before failing. +HEALTH_TIMEOUT_SEC = 5.0 + def _surface_error(prefix: str, resp: httpx.Response) -> RuntimeError: """Raise with ComfyUI's own error body instead of an opaque status line. @@ -76,7 +81,9 @@ class ComfyUIClient: async def health(self) -> bool: try: - resp = await self._http.get(f"{self.base_url}/system_stats") + resp = await self._http.get( + f"{self.base_url}/system_stats", timeout=HEALTH_TIMEOUT_SEC + ) return resp.status_code == 200 except Exception: return False diff --git a/calliope-backend/src/calliope/routers/comfyui.py b/calliope-backend/src/calliope/routers/comfyui.py index 21e0727c..eb5ae3fc 100644 --- a/calliope-backend/src/calliope/routers/comfyui.py +++ b/calliope-backend/src/calliope/routers/comfyui.py @@ -77,8 +77,11 @@ def _parsed_url(base_url: str) -> tuple[str, str]: url = (base_url or "").strip() if not url: raise HTTPException(status_code=400, detail="API 地址不能为空") - if "://" not in url: - url = f"http://{url}" + # HEL-619: the server validates format as strictly as the UI already + # does. A schemeless garbled string used to be silently prefixed with + # http:// and then reported as a DNS failure; it is a 400 format error. + if "://" not in url or any(ch.isspace() for ch in url): + raise HTTPException(status_code=400, detail="地址必须是 http:// 或 https:// 开头的有效 URL") parsed = urlparse(url) if parsed.scheme not in ("http", "https") or not parsed.hostname: raise HTTPException(status_code=400, detail="地址必须是 http:// 或 https:// 开头的有效 URL") @@ -135,7 +138,12 @@ async def list_endpoints() -> dict[str, Any]: @router.post("/endpoints") async def create_endpoint(payload: EndpointIn) -> dict[str, Any]: - base_url, _host = _parsed_url(payload.base_url) + base_url, host = _parsed_url(payload.base_url) + # HEL-619: the same SSRF guard as the connectivity test applies on + # save — otherwise a blocked address can be stored and later probed by + # the worker path. Unresolvable hosts still pass (resolution happens + # again at probe time). + _assert_not_ssrf(host) incoming = [ep for ep in settings.comfyui_endpoints or []] incoming.append(payload.model_dump() | {"base_url": base_url}) try: @@ -151,7 +159,8 @@ async def update_endpoint(endpoint_id: str, payload: EndpointPatch) -> dict[str, _stored_endpoint(endpoint_id) base_url = payload.base_url if base_url is not None: - base_url, _host = _parsed_url(base_url) + base_url, host = _parsed_url(base_url) + _assert_not_ssrf(host) # same save-time guard as create (HEL-619) patch = payload.model_dump(exclude_unset=True) if base_url is not None: patch["base_url"] = base_url diff --git a/calliope-backend/src/calliope/routers/settings.py b/calliope-backend/src/calliope/routers/settings.py index 6a892ec2..8a83aa1f 100644 --- a/calliope-backend/src/calliope/routers/settings.py +++ b/calliope-backend/src/calliope/routers/settings.py @@ -107,7 +107,11 @@ async def update_settings(payload: SettingsUpdate) -> dict[str, Any]: if assignments_in is not None: settings.ensure_llm_profiles() ids = {p["id"] for p in settings.llm_profiles} - cleaned: dict[str, str | None] = {} + # HEL-619: per-role INCREMENTAL update. Rapid dropdown switches used + # to send whole cached snapshots that fully overwrote the map, so a + # stale snapshot could wipe a newer choice. Roles absent from the + # payload keep their stored value; a role sent as null is cleared. + cleaned: dict[str, str | None] = dict(settings.agent_llm_assignments or {}) for role, pid in assignments_in.items(): if role not in AGENT_LLM_ROLES: continue diff --git a/calliope-backend/src/calliope/routers/workflows.py b/calliope-backend/src/calliope/routers/workflows.py index 7816c5af..8d817151 100644 --- a/calliope-backend/src/calliope/routers/workflows.py +++ b/calliope-backend/src/calliope/routers/workflows.py @@ -345,10 +345,29 @@ async def update_workflow(workflow_id: int, payload: WorkflowUpdate) -> dict[str async def delete_workflow(workflow_id: int) -> dict[str, bool]: conn = get_db(settings.db_path) try: - cur = conn.execute("DELETE FROM workflows WHERE id = ?", (workflow_id,)) - conn.commit() - if cur.rowcount == 0: + # HEL-619: refuse to delete while queued/running jobs still use the + # workflow — deleting sets their workflow_id NULL and the job dies + # with "工作流已被删除" only after the user waits for it. + row = conn.execute( + "SELECT id FROM workflows WHERE id = ?", (workflow_id,) + ).fetchone() + if not row: raise HTTPException(status_code=404, detail="Workflow not found") + pending = conn.execute( + "SELECT COUNT(*) AS n FROM jobs" + " WHERE workflow_id = ? AND status IN ('pending', 'running')", + (workflow_id,), + ).fetchone()["n"] + if pending: + raise HTTPException( + status_code=409, + detail=( + f"该工作流还有 {pending} 个排队或执行中的任务," + "请先等任务完成或取消任务,再删除该工作流" + ), + ) + conn.execute("DELETE FROM workflows WHERE id = ?", (workflow_id,)) + conn.commit() return {"ok": True} finally: conn.close() diff --git a/calliope-backend/tests/test_comfyui_endpoints.py b/calliope-backend/tests/test_comfyui_endpoints.py index 7d2d13b9..f468d8e5 100644 --- a/calliope-backend/tests/test_comfyui_endpoints.py +++ b/calliope-backend/tests/test_comfyui_endpoints.py @@ -374,6 +374,61 @@ def test_comfy_test_requires_url(client): assert r.status_code == 400 +def test_schemeless_url_is_format_error_everywhere(client): + """HEL-619: a schemeless/garbled address is a 400 format error on every + entry point (probe + save), not a silent http:// prefix that later + surfaces as a DNS-classified probe failure.""" + prev = _snapshot_settings() + try: + for url in ("192.168.0.1:8188", "http:192.168.0.1", "ht!tp://x", "http://a b.test/"): + r = client.post( + "/api/comfyui/endpoints/test", json={"base_url": url, "timeout_sec": 2} + ) + assert r.status_code == 400, url + assert "http://" in r.json()["detail"] or "https://" in r.json()["detail"] + r = client.post( + "/api/comfyui/endpoints", json={"name": "无协议", "base_url": url} + ) + assert r.status_code == 400, url + assert "无协议" not in [ep.get("name") for ep in settings.comfyui_endpoints or []] + finally: + for key, value in prev.items(): + setattr(settings, key, value) + + +def test_save_endpoint_runs_ssrf_guard(client): + """HEL-619: the SSRF guard applies on save (create + edit), not only on + the connectivity probe; loopback stays allowed.""" + prev = _snapshot_settings() + try: + _clear_endpoints() + r = client.post( + "/api/comfyui/endpoints", + json={"name": "元数据", "base_url": "http://169.254.169.254/latest"}, + ) + assert r.status_code == 400 + assert "禁止" in r.json()["detail"] + + r = client.post( + "/api/comfyui/endpoints", + json={"name": "本机", "base_url": "http://127.0.0.1:8188"}, + ) + assert r.status_code == 200 + eid = r.json()["endpoints"][-1]["id"] + + r = client.patch( + f"/api/comfyui/endpoints/{eid}", + json={"base_url": "http://169.254.169.254/latest"}, + ) + assert r.status_code == 400 + # the stored endpoint is untouched + stored = next(ep for ep in settings.comfyui_endpoints if ep["id"] == eid) + assert stored["base_url"] == "http://127.0.0.1:8188" + finally: + for key, value in prev.items(): + setattr(settings, key, value) + + # ---------------------------------------------------------------------- # workflow binding + job snapshots # ---------------------------------------------------------------------- @@ -542,6 +597,41 @@ def test_queue_enqueue_stamps_all_paths(client): setattr(settings, key, value) +def test_workflow_delete_blocked_while_jobs_pending(client): + """HEL-619: deleting a workflow with pending/running jobs is refused + with 409 instead of silently nulling the jobs' workflow_id.""" + from calliope.db import get_db + from calliope.queue.manager import queue_manager + + wf = _import_workflow(client).json() + project = client.post( + "/api/projects", json={"title": "删除保护测试", "idea": "x"} + ).json() + # Pause the lifespan-started worker so the pending job STAYS pending. + queue_manager.paused = True + try: + queue_manager.enqueue( + project_id=project["id"], kind="video", workflow_id=wf["id"], payload={} + ) + + r = client.delete(f"/api/workflows/{wf['id']}") + assert r.status_code == 409 + assert "排队" in r.json()["detail"] + finally: + queue_manager.paused = False + + # job finished → deletion allowed again + conn = get_db(settings.db_path) + try: + conn.execute("UPDATE jobs SET status = 'done' WHERE project_id = ?", (project["id"],)) + conn.commit() + finally: + conn.close() + r = client.delete(f"/api/workflows/{wf['id']}") + assert r.status_code == 200 + assert r.json() == {"ok": True} + + def test_settings_public_dict_masks_endpoint_tokens(client): prev = _snapshot_settings() try: diff --git a/calliope-backend/tests/test_llm_profiles.py b/calliope-backend/tests/test_llm_profiles.py index 9e9576ab..29f4395c 100644 --- a/calliope-backend/tests/test_llm_profiles.py +++ b/calliope-backend/tests/test_llm_profiles.py @@ -216,6 +216,18 @@ def test_settings_accepts_agent_llm_assignments(client): # Unknown role keys are dropped, known roles stored (incl. explicit None) assert body["agent_llm_assignments"] == {"video": a_id, "story": None} + # HEL-619: updates are per-role incremental — a partial payload must + # not wipe roles it does not mention (rapid-switch race). + r = client.post( + "/api/settings", + json={"agent_llm_assignments": {"main": a_id}}, + ) + assert r.status_code == 200 + body = r.json() + assert body["agent_llm_assignments"]["main"] == a_id + assert body["agent_llm_assignments"]["video"] == a_id + assert body["agent_llm_assignments"]["story"] is None + r = client.post( "/api/settings", json={"agent_llm_assignments": {"video": "no-such-profile"}}, diff --git a/calliope-backend/tests/test_shell_tool.py b/calliope-backend/tests/test_shell_tool.py index 9ef70d17..697d85d0 100644 --- a/calliope-backend/tests/test_shell_tool.py +++ b/calliope-backend/tests/test_shell_tool.py @@ -13,6 +13,7 @@ The shell is default-OFF; every guard branch here is a security contract: from __future__ import annotations import asyncio +import os import sys from pathlib import Path @@ -25,6 +26,23 @@ from calliope.agent.harness.registry import ToolContext PYTHON = Path(sys.executable).stem # bare name ("python") — the tool refuses paths +@pytest.fixture(autouse=True, scope="module") +def _interpreter_on_path(): + """Make the bare interpreter name resolvable via PATH (HEL-619). + + The guard resolves the command with ``shutil.which`` against PATH — + running pytest as ``path/to/.venv/bin/python -m pytest`` (or on a box + with no ``python`` alias) leaves the stem unresolvable and every + exec-path test fails for environmental reasons only. Prepending the + interpreter's own bin dir (restored afterwards) pins the lookup to the + interpreter actually running the suite, on every OS. + """ + prev = os.environ.get("PATH", "") + os.environ["PATH"] = f"{Path(sys.executable).parent}{os.pathsep}{prev}" + yield + os.environ["PATH"] = prev + + @pytest.fixture(autouse=True, scope="module") def _never_touch_real_db(): """Redirect data_dir before ANY write in this module (repo test rule).""" @@ -210,12 +228,19 @@ def test_structured_shell_approval_allows(enabled): def test_cwd_outside_workspace_denied(enabled): + """Guard denial tests use PYTHON + a structured approval card (not + ffprobe): the interpreter resolves on every machine, and the card + clears the approval gate so the LATER containment check is what fires + (HEL-619 — ffprobe-absent boxes used to fail these for environmental + reasons).""" registry, _ = build_harness() + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "cwd": str(Path(settings_data_dir_home()))}, + {"command": PYTHON, "cwd": str(settings_data_dir_home())}, ) ) assert out["reason_code"] == "guard_shell_outside_workspace" @@ -230,11 +255,13 @@ def test_sensitive_arg_denied(enabled, tmp_path): registry, _ = build_harness() secret = tmp_path / "calliope_config.json" secret.write_text("{}", encoding="utf-8") + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "args": [str(secret)]}, + {"command": PYTHON, "args": ["-c", "print('x')", str(secret)]}, ) ) assert out["reason_code"] == "guard_shell_denied_path" @@ -244,11 +271,13 @@ def test_db_arg_denied(enabled, tmp_path): registry, _ = build_harness() db = tmp_path / "calliope.db" db.write_bytes(b"") + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "args": [str(db)]}, + {"command": PYTHON, "args": ["-c", "print('x')", str(db)]}, ) ) assert out["reason_code"] == "guard_shell_denied_path" @@ -256,11 +285,13 @@ def test_db_arg_denied(enabled, tmp_path): def test_env_file_arg_denied(enabled): registry, _ = build_harness() + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "args": ["--", ".env"]}, + {"command": PYTHON, "args": ["-c", "print('x')", "--", ".env"]}, ) ) assert out["reason_code"] == "guard_shell_denied_path" @@ -270,11 +301,13 @@ def test_outside_absolute_arg_denied(enabled, tmp_path): registry, _ = build_harness() outside = tmp_path / "movie.mp4" outside.write_bytes(b"00") + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "args": [str(outside)]}, + {"command": PYTHON, "args": ["-c", "print('x')", str(outside)]}, ) ) assert out["reason_code"] == "guard_shell_outside_workspace" @@ -287,16 +320,22 @@ def test_inside_asset_arg_allowed(enabled): registry, _ = build_harness() ref = settings.assets_dir / "ref.mp4" ref.write_bytes(b"00") + sid = _session() + _answer_card(sid, "Yes, run it") out = asyncio.run( registry.execute( - _ctx(), + ToolContext(session_id=sid, project_id=None), "run_command", - {"command": "ffprobe", "args": ["-v", "error", str(ref)]}, + { + # The asset path is a real argv entry the guard must allow + # (and the interpreter actually reads it). + "command": PYTHON, + "args": ["-c", "import sys; print(open(sys.argv[1]).read())", str(ref)], + }, ) ) - # Not a real mp4 — ffprobe fails, but the GUARD must not have denied it. - assert out.get("reason_code") is None - assert out.get("exit_code") != 0 or out.get("ok") is False or out["ok"] is True + assert out["ok"] is True, out + assert "00" in out["stdout"] def test_workspace_relative_cwd_allowed(enabled, tmp_path): diff --git a/calliope-backend/tests/test_video_continue.py b/calliope-backend/tests/test_video_continue.py index ca4ebb67..c5415cb4 100644 --- a/calliope-backend/tests/test_video_continue.py +++ b/calliope-backend/tests/test_video_continue.py @@ -289,7 +289,11 @@ def _stub_comfy(monkeypatch, captured: dict) -> list[str]: prepare_media_inputs handed it; download_image writes out.mp4. Returns the list that collects uploaded local paths. """ - queue_manager.paused = True + # monkeypatch (not bare assignment): queue_manager is a process-level + # singleton — a bare `paused = True` leaks into every later test and + # stalls e.g. test_workbench's worker (HEL-619). monkeypatch restores + # the previous value at teardown. + monkeypatch.setattr(queue_manager, "paused", True) uploads: list[str] = [] async def fake_upload_video(self, path, subfolder="calliope"): diff --git a/calliope-web/src/routes/agents/+page.svelte b/calliope-web/src/routes/agents/+page.svelte index f3cec3ee..b786c1fc 100644 --- a/calliope-web/src/routes/agents/+page.svelte +++ b/calliope-web/src/routes/agents/+page.svelte @@ -18,7 +18,6 @@ type NamedAgent, type SkillAdminEntry, type SkillVersion, - type Settings, canvasApi, } from '$lib/api'; import { AGENT_TASK_PROMPTS, AGENT_TASK_TITLE_KEYS, isAgentTaskKind } from '$lib/agentTasks'; @@ -378,10 +377,10 @@ }, }); - function setAgentModel(s: Settings, role: string, value: string) { - const merged: Record = { ...(s.agent_llm_assignments ?? {}) }; - merged[role] = value === '' ? null : value; - $assignMutation.mutate(merged); + function setAgentModel(role: string, value: string) { + // Single-role delta only (HEL-619): the whole-snapshot replay races + // concurrent switches; the server merges per role. + $assignMutation.mutate({ [role]: value === '' ? null : value }); } // ───────────────────────────────────────────────────────────────────── @@ -718,7 +717,7 @@ setAssignment(s, agent.role, e.currentTarget.value)} + onchange={(e) => setAssignment(agent.role, e.currentTarget.value)} > {#each profiles as p (p.id)} @@ -1195,6 +1196,9 @@ padding: 18px 20px 20px; position: sticky; top: 24px; + /* 384px must be the OUTER width per the a6 spec — without + border-box padding+border inflate it to 426px (HEL-619). */ + box-sizing: border-box; } .form-idle { display: flex;