Skills now have a single `act(ctx, request)` body phase. The free-string `Prediction.text` handoff, the `Prediction` type, and the predict phase are gone. - Resolution is pipeline-owned: the tiered config merge (global -> category -> skill) fills contract vars; the authoring-time SemIf search now searches the whole cascade; unresolved vars pause pre-act (`pre_act`, renamed from `pre_predict`) and are asked of the human one at a time. - The data contract is a module-level `CONTRACT` constant in the body, parsed by `parse_contract`; `contract.json` is a derived mirror. The separate `generate_data_contract` codegen call is removed. - Candidate selection stays in the body: a set of candidates (bridge endpoint, direct API, hard-coded list) gets a SemIf query, and the decisions ride back on `ActionResult.decisions` to be logged with the run outcome (phase `act`). - `assess:outcome`/`assess:requeue` state carries the resolved inputs (secrets redacted by name) so the decision sees the full query -> resolution -> action path. - Authoring validation enforces: compiles + `act`; `INTEGRATION` flat and valid; `CONTRACT` flat str->str; every CONTRACT key actually read from `ctx.config`. - Test-regen ladder narrowed to code/test (the contract rides in the code). SKILL.md/TESTGEN.md rewritten; seeds (calendar.next_event, simplex.next_message, simplex.connect_link) rewritten to act-only + CONTRACT; docs updated. BREAKING: persisted bodies in data/skills/ must be regenerated.
175 lines
5.9 KiB
Python
175 lines
5.9 KiB
Python
"""Hermetic mechanics test for simplex.connect_link.
|
|
|
|
Run from this folder: `python skill.test.py`. No external network: a loopback
|
|
http.server plays the standalone SimpleX forwarding bridge. This proves the body health-
|
|
checks the bridge, fetches the contact link, reports a freshly created link, and
|
|
fails honestly when the bridge has no provider or is unreachable. It does NOT
|
|
prove the live integration — only a real run against the bridge and a real
|
|
simplex-chat daemon does.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
import threading
|
|
import urllib.parse
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
from semif_agent.decisions import Request
|
|
from semif_agent.skills import ActionContext
|
|
|
|
import skill
|
|
|
|
|
|
class BridgeHandler(BaseHTTPRequestHandler):
|
|
address = {}
|
|
address_status = 200
|
|
requests = []
|
|
tokens = []
|
|
|
|
def _send(self, payload, status=200):
|
|
body = json.dumps(payload).encode("utf-8")
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def do_GET(self):
|
|
self.requests.append(self.path)
|
|
self.tokens.append(self.headers.get("X-Semif-Token"))
|
|
url = urllib.parse.urlparse(self.path)
|
|
if url.path == "/health":
|
|
self._send({"ok": True, "platform": "simplex"})
|
|
elif url.path == "/address":
|
|
self._send(self.address, self.address_status)
|
|
else:
|
|
self._send({"error": "not found"}, 404)
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
|
|
def start_server(address=None, address_status=200):
|
|
handler = type(
|
|
"Handler",
|
|
(BridgeHandler,),
|
|
{
|
|
"address": address or {},
|
|
"address_status": address_status,
|
|
"requests": [],
|
|
"tokens": [],
|
|
},
|
|
)
|
|
server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
return server, handler, f"http://127.0.0.1:{server.server_address[1]}"
|
|
|
|
|
|
def config(url, token=""):
|
|
return {"simplex_bridge_url": url, "simplex_bridge_token": token}
|
|
|
|
|
|
def test_reports_existing_link():
|
|
server, handler, url = start_server(
|
|
{"short_link": "simplex:/contact#abc", "full_link": "https://smp/short", "created": False}
|
|
)
|
|
try:
|
|
ctx = ActionContext(engine=None, config=config(url))
|
|
request = Request("give me your simplex link")
|
|
action = skill.act(ctx, request)
|
|
assert "simplex:/contact#abc" in action.action_log, action.action_log
|
|
assert "https://smp/short" in action.action_log, action.action_log
|
|
assert "current" in action.action_log, action.action_log
|
|
assert handler.requests == ["/health", "/address"], handler.requests
|
|
print(action.action_log)
|
|
finally:
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
|
|
def test_reports_freshly_created_link():
|
|
server, handler, url = start_server(
|
|
{"short_link": "simplex:/contact#new", "full_link": "", "created": True}
|
|
)
|
|
try:
|
|
ctx = ActionContext(engine=None, config=config(url))
|
|
request = Request("create a simplex invitation link")
|
|
action = skill.act(ctx, request)
|
|
assert "simplex:/contact#new" in action.action_log, action.action_log
|
|
assert "created" in action.action_log, action.action_log
|
|
print(action.action_log)
|
|
finally:
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
|
|
def test_no_provider_is_reported_honestly():
|
|
server, handler, url = start_server({"error": "address lookup is not available"}, 503)
|
|
try:
|
|
ctx = ActionContext(engine=None, config=config(url))
|
|
request = Request("give me your simplex link")
|
|
action = skill.act(ctx, request)
|
|
assert "503" in action.action_log, action.action_log
|
|
assert action.new_state == request.text, "a failed lookup must not fake a result"
|
|
print(action.action_log)
|
|
finally:
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
|
|
def test_empty_link_is_reported_honestly():
|
|
server, handler, url = start_server({"short_link": "", "full_link": "", "created": False})
|
|
try:
|
|
ctx = ActionContext(engine=None, config=config(url))
|
|
request = Request("give me your simplex link")
|
|
action = skill.act(ctx, request)
|
|
assert "no SimpleX" in action.action_log, action.action_log
|
|
assert action.new_state == request.text
|
|
print(action.action_log)
|
|
finally:
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
|
|
def test_unreachable_bridge_fails_honestly():
|
|
ctx = ActionContext(engine=None, config=config("http://127.0.0.1:1"))
|
|
request = Request("give me your simplex link")
|
|
action = skill.act(ctx, request)
|
|
assert action.action_log.startswith("simplex.connect_link:"), action.action_log
|
|
assert action.new_state == request.text
|
|
print(action.action_log)
|
|
|
|
|
|
def test_auth_token_is_sent_when_configured():
|
|
server, handler, url = start_server(
|
|
{"short_link": "simplex:/contact#abc", "full_link": "", "created": False}
|
|
)
|
|
try:
|
|
ctx = ActionContext(engine=None, config=config(url, token="sekret"))
|
|
request = Request("give me your simplex link")
|
|
action = skill.act(ctx, request)
|
|
assert "simplex:/contact#abc" in action.action_log, action.action_log
|
|
assert set(handler.tokens) == {"sekret"}, handler.tokens
|
|
print(action.action_log)
|
|
finally:
|
|
server.shutdown()
|
|
server.server_close()
|
|
|
|
|
|
def main():
|
|
test_reports_existing_link()
|
|
test_reports_freshly_created_link()
|
|
test_no_provider_is_reported_honestly()
|
|
test_empty_link_is_reported_honestly()
|
|
test_unreachable_bridge_fails_honestly()
|
|
test_auth_token_is_sent_when_configured()
|
|
print("ok")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except AssertionError as exc:
|
|
print(f"FAILED: {exc}")
|
|
sys.exit(1)
|