Files
ai-agent/backend/test_plugin_new_project.py
gabrielvidalandClaude Opus 5.5 24b9723fd3 refactor(new-project): New project moves to a live plugin
backend/newproject/ (policy, form, service, store, routes, edge shim),
its schemas and test, and `notify new-project` are gone — the
`new-project` plugin (ai-agent-plugins) owns the form, the policy, the
quota, the approval and the scaffold. The core keeps the spawn, read off
the plugin's composer mode (plugins/waiting.composer_modes_for):

- /api/spawn newProject: refused unless the mode allows it; the guest
  staging dir/scope/note live in users/runs.py (the staging scope now
  carries the guest's tool proxy so the plugin's skill reaches the hub)
- /api/me.newProjects from the mode (None without the plugin)
- /api/users/{uid}/domain(+/setup) move to users/routes.py (users/edge.py)
- hub scaffold/relocate keeps the guest's tools + scope line
- plugins/carryover.new_project: /data/new-projects.json, the
  guestDefaults.newProjects policy and projects.create guests carried
  over once (requests.json, plugin config, managed grants)
- frontend: composer toggle / + New project entry driven by
  usePluginComposerModes(); Every guest drops the policy + requests
  (link to Settings → Plugins → New project); the Web address card stays,
  without the Can create projects switch

authz/matrix.golden.txt not updated (the coordinator regenerates it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:12:01 +01:00

160 lines
7.8 KiB
Python

"""New project as a live plugin (2026-11-25): the core keeps only the spawn —
the staging dir, the scope line and ``/api/me.newProjects``, all read off the
``new-project`` plugin's composer mode — and carries the old
``/data/new-projects.json`` + ``guestDefaults.newProjects`` + the guests'
``projects.create`` over to the plugin once."""
from __future__ import annotations
import json
import pathlib
import tempfile
import unittest
from unittest.mock import patch
from fastapi import HTTPException
from plugins import carryover
from plugins.store import OWNER_UID, PluginStore
from runs import routes as run_routes
from users import runs as guest_runs
from users.caller import OWNER, guest_from_record
MANIFEST = {
"schema": 1, "id": "new-project", "name": "New project",
"managed": {"defaultForGuests": False,
"grantFields": [{"key": "quota", "type": "number"}]},
"settings": {"fields": [{"key": k, "type": t} for k, t in (
("quota", "number"), ("templates", "list"), ("approve", "list"),
("visibility", "list"), ("visibilityDefault", "text"), ("level", "text"),
("domain", "text"))]},
}
class FakeUsers:
def __init__(self, users, pol):
self.users, self.pol = users, pol
def all(self):
return list(self.users)
def defaults(self):
return {"newProjects": self.pol}
class CarryOver(unittest.TestCase):
def _store(self, root: pathlib.Path, install: bool = True) -> PluginStore:
store = PluginStore(root / "plugins")
if install:
src = root / "src"
src.mkdir()
(src / "plugin.json").write_text(json.dumps(MANIFEST))
store.install(OWNER_UID, str(src), is_owner=True, approve=True)
return store
def test_requests_policy_and_grants_move_once(self):
with tempfile.TemporaryDirectory() as td:
root = pathlib.Path(td)
legacy = root / "new-projects.json"
data = {"requests": {"f1": {"id": "f1", "requester": "dom", "status": "created"}}}
legacy.write_text(json.dumps(data))
store = self._store(root)
store.update(OWNER_UID, "new-project", config={"quota": 4})
users = FakeUsers([{"id": "dom", "allow": ["projects.create"]}, {"id": "cam"}],
{"quota": 7, "templates": ["react-vite"], "domain": "x.xyz"})
self.assertTrue(carryover.new_project(store, users, str(legacy)))
dst = store.shared_data_dir("new-project") / "requests.json"
self.assertEqual(json.loads(dst.read_text()), data)
self.assertFalse(legacy.exists())
self.assertTrue((root / "new-projects.json.migrated").exists())
cfg = store.get_raw(OWNER_UID, "new-project")["config"]
self.assertEqual(cfg["quota"], 4) # the owner's own setting wins
self.assertEqual(cfg["templates"], ["react-vite"])
self.assertEqual(cfg["domain"], "x.xyz")
self.assertTrue(store.effective_grant("dom", "new-project")["enabled"])
self.assertFalse(store.effective_grant("cam", "new-project")["enabled"])
# Once: a revoked grant stays revoked on the next start.
store.set_grant("dom", "new-project", enabled=False)
self.assertFalse(carryover.new_project(store, users, str(legacy)))
self.assertFalse(store.effective_grant("dom", "new-project")["enabled"])
def test_waits_for_the_plugin(self):
with tempfile.TemporaryDirectory() as td:
root = pathlib.Path(td)
store = self._store(root, install=False)
users = FakeUsers([{"id": "dom", "allow": ["projects.create"]}], {"quota": 2})
self.assertFalse(carryover.new_project(store, users, str(root / "none.json")))
self.assertFalse((store.shared_data_dir("new-project") / ".carried-over").exists())
GUEST = {"id": "dom", "name": "Dom", "email": "dom@example.com", "projects": {}}
class CoreSide(unittest.TestCase):
MODE = {"id": "new-project", "pluginId": "new-project", "label": "New project",
"allowed": True, "reason": None, "remaining": 3, "bucket": "dom",
"domain": "gabvdl.xyz", "stagingNote": "**New project.** do the form"}
def test_access_and_line_follow_the_mode(self):
g = guest_from_record(GUEST)
with patch.object(guest_runs, "new_project_mode", return_value=self.MODE):
self.assertEqual(guest_runs.new_project_access(None, g),
{"allowed": True, "reason": None, "remaining": 3,
"bucket": "dom", "domain": "gabvdl.xyz"})
self.assertIn("(3 left)", guest_runs.new_project_line(None, g))
self.assertIn("shared:new-frontend-project", guest_runs.new_project_line(None, g))
with patch.object(guest_runs, "new_project_mode",
return_value={**self.MODE, "allowed": False, "reason": "quota"}):
self.assertIn("right now: quota", guest_runs.new_project_line(None, g))
with patch.object(guest_runs, "new_project_mode", return_value=None):
self.assertIsNone(guest_runs.new_project_access(None, g))
self.assertIn("can enable it", guest_runs.new_project_line(None, g))
def test_no_plugin_store_no_mode(self):
class D:
plugin_store = None
self.assertIsNone(guest_runs.new_project_mode(D(), OWNER))
def test_staging_note_is_the_plugins(self):
self.assertTrue(guest_runs.staging_note(OWNER, self.MODE).endswith("do the form"))
self.assertIn("new-frontend-project", guest_runs.staging_note(OWNER, None))
def test_spawn_refused_without_the_plugin(self):
class Body:
prompt, model, thinking, effort, account, cwd = "make me a site", None, None, None, None, None
with patch.object(guest_runs, "new_project_mode", return_value=None):
with self.assertRaises(HTTPException) as e:
run_routes._spawn_new_project(None, OWNER, Body())
self.assertEqual(e.exception.status_code, 403)
self.assertEqual(e.exception.detail["kind"], "new_project")
with patch.object(guest_runs, "new_project_mode",
return_value={**self.MODE, "allowed": False, "reason": "quota"}):
with self.assertRaises(HTTPException) as e:
run_routes._spawn_new_project(None, OWNER, Body())
self.assertIn("quota", e.exception.detail["message"])
def test_owner_spawn_gets_the_note(self):
class Body:
prompt, model, thinking, effort, account, cwd = "make me a site", None, None, None, None, None
with patch.object(guest_runs, "new_project_mode", return_value=self.MODE), \
patch.object(run_routes, "_send_message", return_value={"sessionId": "s"}) as send:
self.assertEqual(run_routes._spawn_new_project(None, OWNER, Body()), {"sessionId": "s"})
prompt = send.call_args.args[1]
self.assertTrue(prompt.startswith("make me a site\n\n---\n**New project.**"))
self.assertEqual(send.call_args.kwargs["stamp"], {"newProject": True})
def test_staging_scope_gets_the_guests_tools(self):
g = guest_from_record(GUEST)
tools = {"skills": ["plugin:managed/new-project"], "url": "https://hub/proxy/tools",
"token": "t"}
with patch.object(guest_runs, "tool_scope", return_value=tools):
scope = guest_runs.staging_scope(g, ["foo"], "- line", deps=object())
self.assertEqual(scope["skills"], ["plugin:managed/new-project", "foo"])
self.assertEqual(scope["tools"], {"url": "https://hub/proxy/tools", "token": "t"})
self.assertEqual(scope["writeDirs"], ["/tmp"])
self.assertNotIn("tools", guest_runs.staging_scope(g, [], "- line"))
if __name__ == "__main__":
unittest.main()