settings/mail_trigger.py, GET|PUT /api/mail-trigger-senders, its schema, generated types and the Settings → Mail trigger page are gone; the old URL redirects to the mail-agents plugin's page, where the sender list now lives with the mail agents. /data/mail-trigger-senders.json is carried into the plugin's senders.json once at start (plugins/carryover.py). New owner-only run bridges for plugins (plugins/hubruns.py): runs.start (lean + local-model fallback + machine + meta.plugins.<id>), conversations.send, models.read, workers.read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
7.6 KiB
Python
184 lines
7.6 KiB
Python
"""The run bridges a plugin drives conversations with (mail agents, 2026-11-25):
|
|
``runs.start`` (lean, a local model's fallback, the machine, the plugin's meta),
|
|
``conversations.send``, ``models.read`` and ``workers.read`` — owner only,
|
|
each behind its capability."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
import main
|
|
from plugins import hubruns
|
|
from plugins.store import OWNER_UID, PluginStore
|
|
from toolproxy import access
|
|
|
|
CAPS = ["runs.start", "conversations.send", "models.read", "workers.read"]
|
|
|
|
|
|
class FakeMeta:
|
|
def __init__(self, m=None):
|
|
self.m = m or {}
|
|
|
|
def peek(self, sid):
|
|
return self.m.get(sid) or {}
|
|
|
|
def update(self, sid, patch):
|
|
self.m.setdefault(sid, {}).update(patch)
|
|
|
|
|
|
class TestNormRun(unittest.TestCase):
|
|
def test_a_full_body(self):
|
|
run, why = hubruns.norm_run({
|
|
"prompt": " hi ", "model": "ggml-org/qwen3.8-27b", "harness": "pi", "effort": "HIGH",
|
|
"worker": "local", "lean": {"allow": ["notify-ask", "mail"]},
|
|
"fallback": {"model": "sonnet", "harness": "claude", "wait": 99},
|
|
"title": "📧 x", "services": ["mail", "Bad Tag"], "data": {"preset": "inkling"}})
|
|
self.assertIsNone(why)
|
|
self.assertEqual(run["prompt"], "hi")
|
|
self.assertEqual(run["effort"], "high")
|
|
self.assertEqual(run["lean"], {"allow": ["notify-ask", "mail"]})
|
|
self.assertEqual(run["fallback"]["waitS"], 30 * 60) # capped
|
|
self.assertEqual(run["services"], ["mail"])
|
|
|
|
def test_refusals(self):
|
|
for body in ({}, {"prompt": "x", "harness": "bash"}, {"prompt": "x", "model": "a b"},
|
|
{"prompt": "x", "worker": "../x"}, {"prompt": "x", "fallback": {"model": "x y"}},
|
|
{"prompt": "x", "data": "str"}):
|
|
run, why = hubruns.norm_run(body)
|
|
self.assertIsNone(run, body)
|
|
self.assertTrue(why)
|
|
|
|
|
|
class TestBridges(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tc = TestClient(main.app)
|
|
|
|
def _env(self, td, caps=CAPS, meta=None):
|
|
root = pathlib.Path(td)
|
|
src = root / "src"
|
|
src.mkdir()
|
|
(src / "plugin.json").write_text(json.dumps({"schema": 1, "id": "ma", "name": "M", "capabilities": caps}))
|
|
store = PluginStore(root / "data")
|
|
store.install(OWNER_UID, str(src), is_owner=True, approve=True)
|
|
deps = main.app.state.ai
|
|
self.meta = FakeMeta(meta)
|
|
return [patch.object(deps, "plugin_store", store), patch.object(deps, "meta_store", self.meta)]
|
|
|
|
def _run(self, td, fn, **kw):
|
|
ps = self._env(td, **kw)
|
|
for p in ps:
|
|
p.start()
|
|
try:
|
|
return fn({"Authorization": f"Bearer {access.mint_handler(OWNER_UID, 'ma', ttl=600)}"})
|
|
finally:
|
|
for p in ps:
|
|
p.stop()
|
|
|
|
def test_start_falls_back_when_the_box_cannot_take_it(self):
|
|
sent = {}
|
|
|
|
def send(deps, prompt, **kw):
|
|
sent.update(kw, prompt=prompt)
|
|
return {"sessionId": "s-new"}
|
|
|
|
def go(h):
|
|
with patch("runs.service._send_message", send), \
|
|
patch("runs.sidecar_client._sidecar_call", return_value={"ready": False, "reason": "asleep"}):
|
|
return self.tc.post("/proxy/tools/plugins/ma/hub/runs", headers=h, json={
|
|
"prompt": "do it", "model": "inkling-2-mini", "harness": "pi", "lean": True,
|
|
"fallback": {"model": "sonnet", "harness": "claude"},
|
|
"title": "📧 Hello", "services": ["mail"], "data": {"preset": "inkling"}})
|
|
with tempfile.TemporaryDirectory() as td:
|
|
r = self._run(td, go)
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertEqual(r.json()["fallback"], "asleep")
|
|
self.assertEqual((sent["model"], sent["harness"]), ("sonnet", "claude"))
|
|
self.assertEqual(sent["lean"], {})
|
|
self.assertEqual(sent["worker"], "local")
|
|
m = self.meta.m["s-new"]
|
|
self.assertEqual(m["title"], "📧 Hello")
|
|
self.assertEqual(m["plugins"]["ma"], {"preset": "inkling", "fallback": "asleep"})
|
|
|
|
def test_start_on_a_ready_local_model_keeps_it(self):
|
|
sent = {}
|
|
|
|
def send(deps, prompt, **kw):
|
|
sent.update(kw)
|
|
return {"sessionId": "s2"}
|
|
|
|
def go(h):
|
|
with patch("runs.service._send_message", send), \
|
|
patch("runs.sidecar_client._sidecar_call", return_value={"ready": True}):
|
|
return self.tc.post("/proxy/tools/plugins/ma/hub/runs", headers=h, json={
|
|
"prompt": "x", "model": "inkling-2-mini", "harness": "pi",
|
|
"fallback": {"model": "sonnet"}})
|
|
with tempfile.TemporaryDirectory() as td:
|
|
r = self._run(td, go)
|
|
self.assertIsNone(r.json()["fallback"])
|
|
self.assertEqual(sent["model"], "inkling-2-mini")
|
|
|
|
def test_send_goes_to_the_session(self):
|
|
sent = {}
|
|
|
|
def send(deps, prompt, **kw):
|
|
sent.update(kw, prompt=prompt)
|
|
return {"sessionId": kw["session_id"], "delivered": "inbox"}
|
|
|
|
def go(h):
|
|
with patch("runs.service._send_message", send):
|
|
return self.tc.post("/proxy/tools/plugins/ma/hub/conversations/abcdef12-34/send",
|
|
headers=h, json={"prompt": "reply"})
|
|
with tempfile.TemporaryDirectory() as td:
|
|
r = self._run(td, go)
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertEqual(r.json()["delivered"], "inbox")
|
|
self.assertEqual(sent["session_id"], "abcdef12-34")
|
|
|
|
def test_send_never_reaches_a_guest_conversation(self):
|
|
def go(h):
|
|
return self.tc.post("/proxy/tools/plugins/ma/hub/conversations/abcdef12-34/send",
|
|
headers=h, json={"prompt": "x"})
|
|
with tempfile.TemporaryDirectory() as td:
|
|
r = self._run(td, go, meta={"abcdef12-34": {"owner": "cel"}})
|
|
self.assertEqual(r.status_code, 403)
|
|
|
|
def test_workers_list_the_lab_first(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
r = self._run(td, lambda h: self.tc.get("/proxy/tools/plugins/ma/hub/workers", headers=h))
|
|
self.assertEqual(r.status_code, 200, r.text)
|
|
self.assertEqual(r.json()["workers"][0]["id"], "local")
|
|
|
|
def test_without_the_capability_each_is_refused(self):
|
|
def go(h):
|
|
return [self.tc.post("/proxy/tools/plugins/ma/hub/runs", headers=h, json={"prompt": "x"}).status_code,
|
|
self.tc.post("/proxy/tools/plugins/ma/hub/conversations/abcdef12/send", headers=h,
|
|
json={"prompt": "x"}).status_code,
|
|
self.tc.get("/proxy/tools/plugins/ma/hub/models", headers=h).status_code,
|
|
self.tc.get("/proxy/tools/plugins/ma/hub/workers", headers=h).status_code]
|
|
with tempfile.TemporaryDirectory() as td:
|
|
self.assertEqual(self._run(td, go, caps=[]), [403] * 4)
|
|
|
|
def test_a_guest_caller_is_refused(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
ps = self._env(td)
|
|
for p in ps:
|
|
p.start()
|
|
try:
|
|
tok = access.mint_handler("cel", "ma", ttl=600)
|
|
r = self.tc.post("/proxy/tools/plugins/ma/hub/runs",
|
|
headers={"Authorization": f"Bearer {tok}"}, json={"prompt": "x"})
|
|
self.assertIn(r.status_code, (403, 404))
|
|
finally:
|
|
for p in ps:
|
|
p.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|