From 6786115666ce46d77e3c115e6035bdfbfee22a95 Mon Sep 17 00:00:00 2001 From: Benjiyaya Date: Tue, 1 Sep 2026 02:06:46 +0800 Subject: [PATCH] feat(canvas): canvas + canvas_node tables with project/session cascade scoping --- calliope-backend/src/calliope/db.py | 35 +++++++++++++ calliope-backend/tests/test_canvas_db.py | 67 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 calliope-backend/tests/test_canvas_db.py diff --git a/calliope-backend/src/calliope/db.py b/calliope-backend/src/calliope/db.py index 02553f64..7789f5b4 100644 --- a/calliope-backend/src/calliope/db.py +++ b/calliope-backend/src/calliope/db.py @@ -150,6 +150,40 @@ CREATE TABLE IF NOT EXISTS agent_events ( UNIQUE(session_id, seq) ); CREATE INDEX IF NOT EXISTS idx_agent_events_session ON agent_events(session_id); + +CREATE TABLE IF NOT EXISTS canvas ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + project_id INTEGER REFERENCES projects(id) ON DELETE CASCADE, + agent_session_id INTEGER REFERENCES agent_sessions(id) ON DELETE CASCADE, + title TEXT NOT NULL DEFAULT 'Untitled Canvas', + thumbnail_path TEXT, + viewport_json TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +CREATE INDEX IF NOT EXISTS idx_canvas_project ON canvas(project_id); +CREATE INDEX IF NOT EXISTS idx_canvas_session ON canvas(agent_session_id); + +CREATE TABLE IF NOT EXISTS canvas_node ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + canvas_id INTEGER NOT NULL REFERENCES canvas(id) ON DELETE CASCADE, + type TEXT NOT NULL CHECK(type IN ('entity','image','video','text','workflow')), + title TEXT, + x REAL DEFAULT 0, + y REAL DEFAULT 0, + width REAL, + height REAL, + workflow_id INTEGER NULL, + artifact_path TEXT NULL, + job_id INTEGER NULL, + status TEXT NOT NULL DEFAULT 'idle', + input_values_json TEXT NOT NULL DEFAULT '{}', + entity_type TEXT NULL, + entity_id INTEGER NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); +CREATE INDEX IF NOT EXISTS idx_canvas_node_canvas ON canvas_node(canvas_id); """ @@ -214,6 +248,7 @@ _PATH_COLUMNS = { "locations": ["reference_image_path"], "items": ["reference_image_path"], "scenes": ["env_image_path", "video_path"], + "canvas_node": ["artifact_path"], } diff --git a/calliope-backend/tests/test_canvas_db.py b/calliope-backend/tests/test_canvas_db.py new file mode 100644 index 00000000..f2ea9239 --- /dev/null +++ b/calliope-backend/tests/test_canvas_db.py @@ -0,0 +1,67 @@ +"""Canvas v2.1 Phase 1 — canvas/canvas_node tables, scoping, and cascades.""" +from __future__ import annotations + +import sqlite3 + + +def _get_canvas(client, canvas_id: int): + r = client.get(f"/api/canvas/{canvas_id}") + assert r.status_code == 200 + return r.json() + + +def test_canvas_tables_exist(client): + """Both tables must exist post-migration with the agreed columns.""" + import calliope.config as config_module + from calliope.db import get_db + + conn = get_db(config_module.settings.db_path) + try: + cols = {r[1] for r in conn.execute("PRAGMA table_info(canvas)").fetchall()} + assert {"id", "project_id", "agent_session_id", "title", "viewport_json"} <= cols + node_cols = {r[1] for r in conn.execute("PRAGMA table_info(canvas_node)").fetchall()} + assert { + "id", + "canvas_id", + "type", + "x", + "y", + "artifact_path", + "entity_type", + "entity_id", + } <= node_cols + finally: + conn.close() + + +def test_project_delete_cascades_canvas(client): + pid = client.post("/api/projects", json={"title": "C1", "idea": "x"}).json()["id"] + r = client.post("/api/canvas", json={"project_id": pid, "title": "Bible"}) + assert r.status_code == 200 + canvas_id = r.json()["id"] + + client.delete(f"/api/projects/{pid}") + + rr = client.get(f"/api/canvas/{canvas_id}") + assert rr.status_code == 404 + + +def test_session_delete_cascades_sandbox_canvas(client): + sid = client.post("/api/agent/sessions", json={}).json()["id"] + r = client.post("/api/canvas", json={"agent_session_id": sid}) + assert r.status_code == 200 + canvas_id = r.json()["id"] + + client.delete(f"/api/agent/sessions/{sid}") + + rr = client.get(f"/api/canvas/{canvas_id}") + assert rr.status_code == 404 + + +def test_artifact_path_rebases_on_move(client): + """canvas_node.artifact_path must be registered in _PATH_COLUMNS so a + folder move rebases stored absolute paths (the migration runs at startup).""" + from calliope.db import _PATH_COLUMNS + + assert "canvas_node" in _PATH_COLUMNS + assert "artifact_path" in _PATH_COLUMNS["canvas_node"]