skills: route create_event/next_event through the bridge
Both skills spoke CalDAV directly and declared the account as flat contract vars (nextcloud_url/username/app_password/default_calendar). Bootstrap now stores the account under bridges.nextcloud and only the derived nextcloud_bridge_url/_token reach ctx.config, so the runner asked for the URL and credentials on first fire. Call the bridge instead, like create_task: - create_event: GET /calendars -> SemIf choice -> POST /calendars/events - next_event: GET /calendars -> SemIf choice -> GET /calendars/events The bridge gains two small affordances so the skills keep their behavior: - GET /calendars also returns the configured default calendar name - GET /calendars/events expands recurrences server-side when given a window
This commit is contained in:
1 parent
ace251a829
commit
1a1d28dffd
9 files changed
+486
-748
No files matched your search
@@ -1,15 +1,15 @@
|
||||
"""Hermetic mechanics test for nextcloud.create_event.
|
||||
|
||||
Run from this folder: `python skill.test.py`. No external network: a loopback
|
||||
http.server plays a Nextcloud CalDAV endpoint (PROPFIND discovery + PUT create)
|
||||
and the LLM bridge's `POST /chat`. This proves the body extracts the event title
|
||||
and description through the LLM bridge (caching the call across a `needs_input`
|
||||
resume), parses the date/time from the request, asks for a missing field and
|
||||
resumes, resolves the target calendar with a SemIf choice over the owned
|
||||
calendars (a strong winner is used, otherwise the configured default), writes a
|
||||
real iCalendar PUT, and fails honestly when the LLM bridge or the server errors.
|
||||
It does NOT prove the live integration — only a real run against the user's
|
||||
Nextcloud does.
|
||||
http.server plays the Nextcloud bridge's JSON API (`GET /calendars`,
|
||||
`POST /calendars/events`) and the LLM bridge's `POST /chat`. This proves the body
|
||||
extracts the event title and description through the LLM bridge (caching the call
|
||||
across a `needs_input` resume), parses the date/time from the request, asks for a
|
||||
missing field and resumes, resolves the target calendar with a SemIf choice over
|
||||
the owned calendars (a strong winner is used, otherwise the configured default),
|
||||
creates the event through the bridge, and fails honestly when the LLM bridge or
|
||||
the bridge errors. It does NOT prove the live integration — only a real run
|
||||
against the user's Nextcloud does.
|
||||
"""
|
||||
|
||||
import json
|
||||
@@ -62,11 +62,12 @@ class FakeEngine:
|
||||
)
|
||||
|
||||
|
||||
class CalDavHandler(BaseHTTPRequestHandler):
|
||||
class BridgeHandler(BaseHTTPRequestHandler):
|
||||
calendars = []
|
||||
put_status = 201
|
||||
default = "personal"
|
||||
create_status = 200
|
||||
requests = []
|
||||
puts = []
|
||||
creates = []
|
||||
llm_reply = DEFAULT_LLM_REPLY
|
||||
llm_status = 200
|
||||
llm_requests = []
|
||||
@@ -75,66 +76,63 @@ class CalDavHandler(BaseHTTPRequestHandler):
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
return self.rfile.read(length) if length else b""
|
||||
|
||||
def _send(self, payload, status, content_type):
|
||||
def _send(self, payload, status=200):
|
||||
body = payload if isinstance(payload, bytes) else payload.encode("utf-8")
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_PROPFIND(self):
|
||||
self._read_body()
|
||||
self.requests.append(("PROPFIND", self.path))
|
||||
responses = []
|
||||
for calendar in self.calendars:
|
||||
responses.append(
|
||||
f'<d:response><d:href>/remote.php/dav/calendars/alice/'
|
||||
f'{calendar["name"]}/</d:href>'
|
||||
f"<d:propstat><d:prop>"
|
||||
f'<d:displayname>{calendar["label"]}</d:displayname>'
|
||||
f"<d:resourcetype><d:collection/><c:calendar/></d:resourcetype>"
|
||||
f"</d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat>"
|
||||
f"</d:response>"
|
||||
def do_GET(self):
|
||||
path = urllib.parse.urlparse(self.path).path
|
||||
self.requests.append(("GET", path))
|
||||
if path == "/calendars":
|
||||
self._send(
|
||||
json.dumps({"calendars": self.calendars, "default": self.default})
|
||||
)
|
||||
body = (
|
||||
'<?xml version="1.0" encoding="utf-8"?>'
|
||||
'<d:multistatus xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">'
|
||||
+ "".join(responses)
|
||||
+ "</d:multistatus>"
|
||||
)
|
||||
self._send(body, 207, "application/xml; charset=utf-8")
|
||||
|
||||
def do_PUT(self):
|
||||
body = self._read_body()
|
||||
self.puts.append(
|
||||
{"path": self.path, "body": body, "headers": dict(self.headers)}
|
||||
)
|
||||
self._send(b"", self.put_status, "text/plain")
|
||||
return
|
||||
self._send(json.dumps({"error": "not found"}), 404)
|
||||
|
||||
def do_POST(self):
|
||||
body = self._read_body()
|
||||
if self.path != "/chat":
|
||||
self._send(json.dumps({"error": "not found"}), 404, "application/json")
|
||||
path = urllib.parse.urlparse(self.path).path
|
||||
if path == "/chat":
|
||||
self.llm_requests.append(body)
|
||||
if self.llm_status != 200:
|
||||
self._send(json.dumps({"error": "boom"}), self.llm_status)
|
||||
return
|
||||
self._send(json.dumps({"text": json.dumps(self.llm_reply)}))
|
||||
return
|
||||
self.llm_requests.append(body)
|
||||
if self.llm_status != 200:
|
||||
self._send(json.dumps({"error": "boom"}), self.llm_status, "application/json")
|
||||
if path == "/calendars/events":
|
||||
data = json.loads(body or b"{}")
|
||||
self.creates.append(data)
|
||||
if self.create_status != 200:
|
||||
self._send(json.dumps({"error": "boom"}), self.create_status)
|
||||
return
|
||||
self._send(
|
||||
json.dumps(
|
||||
{
|
||||
"ok": True,
|
||||
"uid": "uid-1",
|
||||
"calendar": data.get("calendar", self.default),
|
||||
"etag": '"etag-1"',
|
||||
}
|
||||
)
|
||||
)
|
||||
return
|
||||
self._send(
|
||||
json.dumps({"text": json.dumps(self.llm_reply)}),
|
||||
200,
|
||||
"application/json",
|
||||
)
|
||||
self._send(json.dumps({"error": "not found"}), 404)
|
||||
|
||||
def log_message(self, *args):
|
||||
pass
|
||||
|
||||
|
||||
def start_server(calendars=None, put_status=201, llm_reply=None, llm_status=200):
|
||||
def start_server(
|
||||
calendars=None, default="personal", create_status=200, llm_reply=None, llm_status=200
|
||||
):
|
||||
handler = type(
|
||||
"Handler",
|
||||
(CalDavHandler,),
|
||||
(BridgeHandler,),
|
||||
{
|
||||
"calendars": calendars
|
||||
if calendars is not None
|
||||
@@ -142,9 +140,10 @@ def start_server(calendars=None, put_status=201, llm_reply=None, llm_status=200)
|
||||
{"name": "personal", "label": "Personal"},
|
||||
{"name": "work", "label": "Work"},
|
||||
],
|
||||
"put_status": put_status,
|
||||
"default": default,
|
||||
"create_status": create_status,
|
||||
"requests": [],
|
||||
"puts": [],
|
||||
"creates": [],
|
||||
"llm_reply": llm_reply if llm_reply is not None else DEFAULT_LLM_REPLY,
|
||||
"llm_status": llm_status,
|
||||
"llm_requests": [],
|
||||
@@ -155,20 +154,18 @@ def start_server(calendars=None, put_status=201, llm_reply=None, llm_status=200)
|
||||
return server, handler, f"http://127.0.0.1:{server.server_address[1]}"
|
||||
|
||||
|
||||
def config(url, default="personal", llm_url=None):
|
||||
def config(url, llm_url=None):
|
||||
return {
|
||||
"nextcloud_url": url,
|
||||
"nextcloud_username": "alice",
|
||||
"nextcloud_app_password": "secret",
|
||||
"nextcloud_default_calendar": default,
|
||||
"nextcloud_bridge_url": url,
|
||||
"nextcloud_bridge_token": "",
|
||||
"llm_bridge_url": url if llm_url is None else llm_url,
|
||||
"llm_bridge_token": "",
|
||||
}
|
||||
|
||||
|
||||
def run(handler, url, text, default="personal", engine=None, llm_url=None):
|
||||
def run(url, text, engine=None, llm_url=None):
|
||||
engine = engine or FakeEngine()
|
||||
ctx = ActionContext(engine=engine, config=config(url, default, llm_url))
|
||||
ctx = ActionContext(engine=engine, config=config(url, llm_url))
|
||||
request = Request(text)
|
||||
action = skill.act(ctx, request)
|
||||
return action, request, engine
|
||||
@@ -186,7 +183,6 @@ def test_example_creates_event_on_default_calendar():
|
||||
# configured default ("personal") wins.
|
||||
engine = FakeEngine(probs={"personal": 0.4, "work": 0.35})
|
||||
action, _request, engine = run(
|
||||
handler,
|
||||
url,
|
||||
"add Ashley's dance recital to the calendar for 5pm Tuesday",
|
||||
engine=engine,
|
||||
@@ -197,17 +193,15 @@ def test_example_creates_event_on_default_calendar():
|
||||
"add Ashley's dance recital to the calendar for 5pm Tuesday"
|
||||
), engine.decisions[0]
|
||||
assert "referred to in the user query" in engine.decisions[0].question
|
||||
assert len(handler.puts) == 1, handler.puts
|
||||
put = handler.puts[0]
|
||||
assert put["path"].startswith("/remote.php/dav/calendars/alice/personal/"), put
|
||||
assert put["path"].endswith(".ics"), put
|
||||
assert put["headers"].get("If-None-Match") == "*"
|
||||
body = put["body"].decode("utf-8")
|
||||
assert "SUMMARY:Ashley's dance recital" in body, body
|
||||
assert "DESCRIPTION:Dance recital at 5pm." in body, body
|
||||
assert len(handler.creates) == 1, handler.creates
|
||||
created = handler.creates[0]
|
||||
assert created["calendar"] == "personal", created
|
||||
assert created["summary"] == "Ashley's dance recital", created
|
||||
assert created["description"] == "Dance recital at 5pm.", created
|
||||
tuesday = next_weekday(FIXED_NOW, 1)
|
||||
assert f"DTSTART:{tuesday.strftime('%Y%m%d')}T170000Z" in body, body
|
||||
assert f"DTEND:{tuesday.strftime('%Y%m%d')}T180000Z" in body, body
|
||||
assert created["start"] == f"{tuesday.isoformat()}T17:00:00+00:00", created
|
||||
assert created["end"] == f"{tuesday.isoformat()}T18:00:00+00:00", created
|
||||
assert created["all_day"] is False, created
|
||||
assert "Ashley's dance recital" in action.action_log, action.action_log
|
||||
print(action.action_log)
|
||||
finally:
|
||||
@@ -220,22 +214,18 @@ def test_missing_time_asks_then_creates():
|
||||
llm_reply={"title": "dentist appointment", "description": ""}
|
||||
)
|
||||
try:
|
||||
action, request, engine = run(
|
||||
handler, url, "add dentist appointment on 2026-11-10"
|
||||
)
|
||||
action, request, engine = run(url, "add dentist appointment on 2026-11-10")
|
||||
assert action.needs_input, "a missing start time must be asked"
|
||||
assert "time" in action.needs_input.lower(), action.needs_input
|
||||
assert handler.puts == []
|
||||
assert handler.creates == []
|
||||
assert len(handler.llm_requests) == 1, "the LLM is called once, then cached"
|
||||
request.user_input = "2pm"
|
||||
ctx = ActionContext(engine=engine, config=config(url))
|
||||
action = skill.act(ctx, request)
|
||||
assert action.needs_input is None, action.needs_input
|
||||
assert len(handler.puts) == 1, handler.puts
|
||||
assert len(handler.creates) == 1, handler.creates
|
||||
assert len(handler.llm_requests) == 1, "the resume must reuse the cached fields"
|
||||
body = handler.puts[0]["body"].decode("utf-8")
|
||||
assert "SUMMARY:dentist appointment" in body, body
|
||||
assert "DTSTART:20261110T140000Z" in body, body
|
||||
assert handler.creates[0]["start"] == "2026-11-10T14:00:00+00:00", handler.creates[0]
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -247,15 +237,14 @@ def test_missing_day_asks_then_creates():
|
||||
llm_reply={"title": "dentist appointment", "description": ""}
|
||||
)
|
||||
try:
|
||||
action, request, engine = run(handler, url, "add dentist appointment at 2pm")
|
||||
action, request, engine = run(url, "add dentist appointment at 2pm")
|
||||
assert action.needs_input, "a missing day must be asked"
|
||||
assert "day" in action.needs_input.lower(), action.needs_input
|
||||
request.user_input = "2026-11-10"
|
||||
ctx = ActionContext(engine=engine, config=config(url))
|
||||
action = skill.act(ctx, request)
|
||||
assert action.needs_input is None, action.needs_input
|
||||
body = handler.puts[0]["body"].decode("utf-8")
|
||||
assert "DTSTART:20261110T140000Z" in body, body
|
||||
assert handler.creates[0]["start"] == "2026-11-10T14:00:00+00:00", handler.creates[0]
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -265,10 +254,10 @@ def test_missing_day_asks_then_creates():
|
||||
def test_missing_title_asks():
|
||||
server, handler, url = start_server(llm_reply={"title": "", "description": ""})
|
||||
try:
|
||||
action, _request, _engine = run(handler, url, "schedule on 2026-11-10 at 2pm")
|
||||
action, _request, _engine = run(url, "schedule on 2026-11-10 at 2pm")
|
||||
assert action.needs_input, "a missing title must be asked"
|
||||
assert "call the event" in action.needs_input.lower(), action.needs_input
|
||||
assert handler.puts == []
|
||||
assert handler.creates == []
|
||||
print(action.needs_input)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -282,7 +271,6 @@ def test_named_calendar_is_used():
|
||||
try:
|
||||
engine = FakeEngine(pick="work")
|
||||
action, _request, engine = run(
|
||||
handler,
|
||||
url,
|
||||
"add team sync to my work calendar on 2026-11-10 at 9am",
|
||||
engine=engine,
|
||||
@@ -293,11 +281,8 @@ def test_named_calendar_is_used():
|
||||
"personal",
|
||||
"work",
|
||||
], engine.decisions[0].options
|
||||
assert handler.puts[0]["path"].startswith(
|
||||
"/remote.php/dav/calendars/alice/work/"
|
||||
), handler.puts[0]
|
||||
body = handler.puts[0]["body"].decode("utf-8")
|
||||
assert "SUMMARY:team sync" in body, body
|
||||
assert handler.creates[0]["calendar"] == "work", handler.creates[0]
|
||||
assert handler.creates[0]["summary"] == "team sync", handler.creates[0]
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -311,16 +296,11 @@ def test_weak_winner_falls_back_to_default():
|
||||
# the configured default ("personal") is used instead.
|
||||
engine = FakeEngine(probs={"personal": 0.45, "work": 0.5})
|
||||
action, _request, engine = run(
|
||||
handler,
|
||||
url,
|
||||
"add standup on 2026-11-10 at 9am",
|
||||
engine=engine,
|
||||
url, "add standup on 2026-11-10 at 9am", engine=engine
|
||||
)
|
||||
assert action.needs_input is None, action.needs_input
|
||||
assert len(engine.decisions) == 1, engine.decisions
|
||||
assert handler.puts[0]["path"].startswith(
|
||||
"/remote.php/dav/calendars/alice/personal/"
|
||||
), handler.puts[0]
|
||||
assert handler.creates[0]["calendar"] == "personal", handler.creates[0]
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -328,24 +308,20 @@ def test_weak_winner_falls_back_to_default():
|
||||
|
||||
|
||||
def test_unknown_default_uses_semif_decision():
|
||||
server, handler, url = start_server(llm_reply={"title": "standup", "description": ""})
|
||||
server, handler, url = start_server(
|
||||
llm_reply={"title": "standup", "description": ""}, default="missing"
|
||||
)
|
||||
try:
|
||||
# The configured default is not among the account's calendars and the
|
||||
# SemIf winner is weak: the winner is used anyway (never blocks).
|
||||
engine = FakeEngine(probs={"personal": 0.3, "work": 0.4})
|
||||
action, _request, engine = run(
|
||||
handler,
|
||||
url,
|
||||
"add standup on 2026-11-10 at 9am",
|
||||
default="missing",
|
||||
engine=engine,
|
||||
url, "add standup on 2026-11-10 at 9am", engine=engine
|
||||
)
|
||||
assert action.needs_input is None, action.needs_input
|
||||
assert len(engine.decisions) == 1, engine.decisions
|
||||
assert action.decisions and len(action.decisions) == 1, action.decisions
|
||||
assert handler.puts[0]["path"].startswith(
|
||||
"/remote.php/dav/calendars/alice/work/"
|
||||
), handler.puts[0]
|
||||
assert handler.creates[0]["calendar"] == "work", handler.creates[0]
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -355,14 +331,13 @@ def test_unknown_default_uses_semif_decision():
|
||||
def test_all_day_event():
|
||||
server, handler, url = start_server(llm_reply={"title": "conference", "description": ""})
|
||||
try:
|
||||
action, _request, _engine = run(
|
||||
handler, url, "add conference all day on 2026-11-10"
|
||||
)
|
||||
action, _request, _engine = run(url, "add conference all day on 2026-11-10")
|
||||
assert action.needs_input is None, action.needs_input
|
||||
body = handler.puts[0]["body"].decode("utf-8")
|
||||
assert "DTSTART;VALUE=DATE:20261110" in body, body
|
||||
assert "DTEND;VALUE=DATE:20261111" in body, body
|
||||
assert "SUMMARY:conference" in body, body
|
||||
created = handler.creates[0]
|
||||
assert created["all_day"] is True, created
|
||||
assert created["start"] == "2026-11-10", created
|
||||
assert created["end"] == "2026-11-11", created
|
||||
assert created["summary"] == "conference", created
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -373,14 +348,13 @@ def test_llm_bridge_failure_is_reported_honestly():
|
||||
server, handler, url = start_server()
|
||||
try:
|
||||
action, request, _engine = run(
|
||||
handler,
|
||||
url,
|
||||
"add standup on 2026-11-10 at 9am",
|
||||
llm_url="http://127.0.0.1:1",
|
||||
)
|
||||
assert "llm bridge" in action.action_log, action.action_log
|
||||
assert action.new_state == request.text, "a failed extraction must not fake a result"
|
||||
assert handler.puts == [], "no event is created when the LLM bridge is down"
|
||||
assert handler.creates == [], "no event is created when the LLM bridge is down"
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -390,12 +364,10 @@ def test_llm_bridge_failure_is_reported_honestly():
|
||||
def test_llm_bridge_error_status_is_reported_honestly():
|
||||
server, handler, url = start_server(llm_status=502)
|
||||
try:
|
||||
action, request, _engine = run(
|
||||
handler, url, "add standup on 2026-11-10 at 9am"
|
||||
)
|
||||
assert "HTTP 502" in action.action_log, action.action_log
|
||||
action, request, _engine = run(url, "add standup on 2026-11-10 at 9am")
|
||||
assert "502" in action.action_log, action.action_log
|
||||
assert action.new_state == request.text
|
||||
assert handler.puts == []
|
||||
assert handler.creates == []
|
||||
print(action.action_log)
|
||||
finally:
|
||||
server.shutdown()
|
||||
@@ -404,12 +376,10 @@ def test_llm_bridge_error_status_is_reported_honestly():
|
||||
|
||||
def test_server_error_is_reported_honestly():
|
||||
server, handler, url = start_server(
|
||||
put_status=500, llm_reply={"title": "standup", "description": ""}
|
||||
create_status=500, llm_reply={"title": "standup", "description": ""}
|
||||
)
|
||||
try:
|
||||
action, request, _engine = run(
|
||||
handler, url, "add standup on 2026-11-10 at 9am"
|
||||
)
|
||||
action, request, _engine = run(url, "add standup on 2026-11-10 at 9am")
|
||||
assert "500" in action.action_log, action.action_log
|
||||
assert action.new_state == request.text, "a failed create must not fake a result"
|
||||
print(action.action_log)
|
||||
@@ -420,7 +390,7 @@ def test_server_error_is_reported_honestly():
|
||||
|
||||
def test_unreachable_server_fails_honestly():
|
||||
action, request, _engine = run(
|
||||
None, "http://127.0.0.1:1", "add standup on 2026-11-10 at 9am"
|
||||
"http://127.0.0.1:1", "add standup on 2026-11-10 at 9am"
|
||||
)
|
||||
assert action.action_log and "nextcloud.create_event" not in action.action_log, action.action_log
|
||||
assert action.new_state == request.text
|
||||
|
||||
Reference in new issue
Block a user