feat(mcp,editor): Option A+B storage + 10 agent deliverables (Phase 7)

Ships the combined filesystem/Supabase storage adapter + MCP scene
lifecycle tools + Next.js API routes + editor /scene/[id] route, so
an MCP save is directly openable at /scene/<id> without any
injection hack. End-to-end verified: 10/10 e2e steps pass.

Storage (A1/A2/A3):
- SceneStore interface + error classes + slug helpers
- FilesystemSceneStore at $PASCAL_DATA_DIR (defaults XDG/~/.pascal)
  with atomic writes, .index sidecar, optimistic locking
- SupabaseSceneStore with scenes + scene_revisions tables, RLS
  migration SQL, mock-backed unit tests
- createSceneStore(env) auto-selects based on SUPABASE_URL +
  SUPABASE_SERVICE_ROLE_KEY

MCP tools (A4, A8, A9, A10):
- save_scene / load_scene / list_scenes / delete_scene / rename_scene
- list_templates / create_from_template (3 seed templates:
  empty-studio, two-bedroom, garden-house)
- generate_variants (7 mutation kinds, seeded RNG, save=true|false)
- photo_to_scene (vision sampling → scene graph → save)

Editor (A5, A6):
- /api/scenes + /api/scenes/[id] with RFC 7232 If-Match locking
- /scene/[id] and /scenes route pages with save button, SceneLoader
- Removed the window.__pascalScene dev injection hack

Security + UX edges (A7, A8):
- AssetUrl Zod validator: asset:// blob: data:image/ /path https:
  (http://localhost for dev) + PASCAL_ALLOWED_ASSET_ORIGINS env
  allowlist. Hardens scan.url, guide.url, item.asset.src,
  material.texture.url, MaterialMaps.*Map
- Auto-frame camera on empty→non-empty scene transition
  (camera-controls:fit-scene emitter event)

Shared utilities:
- rehydrateSiteChildren() extracted to packages/mcp/src/lib/ and
  used by both create-from-template and generate-variants to work
  around the SiteNode.children-as-objects vs. ids inconsistency
  (CROSS_CUTTING §2)
- Storage + MCP subpath exports added to packages/mcp/package.json
  (CROSS_CUTTING §4)

Tests: 293 pass / 0 fail across 40 files (was 142 pre-Phase-7).
Biome: clean.

Phase-7 e2e script at packages/mcp/test-reports/phase7-e2e.ts:
MCP HTTP + editor Next.js both point at $PASCAL_DATA_DIR =
/tmp/pascal-e2e, save_scene from MCP, GET /api/scenes/<id> from
editor server, /scenes list page renders all saved scenes, scene
page renders SceneLoader, delete_scene works.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adrian Perez
2026-04-18 19:29:28 +02:00
co-authored by Claude Opus 4.7
parent 42bd05db9c
commit e8d0b13ff5
81 changed files with 8933 additions and 1213 deletions
@@ -0,0 +1,76 @@
-- 0001_scenes.sql
-- Initial Pascal scene storage schema.
--
-- Creates:
-- * projects — minimal project rows owned by an auth.users row
-- * scenes — the current state of a scene (graph_json + metadata)
-- * scene_revisions — append-only revision log keyed by (scene_id, version)
--
-- Row-level security is enabled on all three tables. Owners get full access
-- to their own rows; anonymous users can read scenes flagged public = true.
-- The `service_role` key bypasses RLS, which is how the MCP server writes
-- on behalf of users.
-- Projects (minimal — we'll extend in a later PR)
create table if not exists projects (
id uuid primary key default gen_random_uuid(),
owner_id uuid references auth.users(id) on delete cascade,
name text not null,
created_at timestamptz not null default now()
);
-- Scenes
create table if not exists scenes (
id text primary key, -- slug; keeps URLs stable
project_id uuid references projects(id) on delete cascade,
owner_id uuid references auth.users(id) on delete set null,
name text not null check (length(name) between 1 and 200),
graph_json jsonb not null,
thumbnail_url text,
version int not null default 1 check (version >= 1),
public boolean not null default false,
size_bytes int not null default 0,
node_count int not null default 0,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists idx_scenes_owner on scenes(owner_id);
create index if not exists idx_scenes_project on scenes(project_id);
create index if not exists idx_scenes_updated on scenes(updated_at desc);
-- Revision history
create table if not exists scene_revisions (
scene_id text references scenes(id) on delete cascade,
version int not null,
graph_json jsonb not null,
author_kind text not null check (author_kind in ('human', 'mcp', 'agent')),
author_id uuid references auth.users(id) on delete set null,
created_at timestamptz not null default now(),
primary key (scene_id, version)
);
-- RLS
alter table scenes enable row level security;
alter table scene_revisions enable row level security;
alter table projects enable row level security;
-- owner can do everything; anon can read public=true; service_role bypasses
create policy scenes_owner_all on scenes
for all using (auth.uid() = owner_id) with check (auth.uid() = owner_id);
create policy scenes_public_read on scenes
for select using (public = true);
create policy revisions_owner_read on scene_revisions
for select using (
exists (select 1 from scenes where scenes.id = scene_revisions.scene_id and scenes.owner_id = auth.uid())
);
create policy projects_owner_all on projects
for all using (auth.uid() = owner_id) with check (auth.uid() = owner_id);
-- updated_at trigger
create or replace function tg_touch_updated() returns trigger as $$
begin new.updated_at := now(); return new; end;
$$ language plpgsql;
create trigger scenes_touch_updated before update on scenes
for each row execute function tg_touch_updated();