feat(#17): web Bifrost-bind — server side (slice 3c, INV-008 lockstep complete)
Slice 3c of issue #17 — the web surface of the bind trigger, server side. Closes the INV-008 lockstep (CLI + TUI + web all carry the bind now). Implements the contract's "web bind split": the browser selects only the PLANE; the consumer key and the Worldtree-visible host are SERVER-HELD config and never reach the browser. - create_app gains bifrost_consumer_key + bifrost_visible_host (server-held, from env via the entrypoint: RATATOSKR_BIFROST_CONSUMER_KEY / RATATOSKR_PROVIDER_VISIBLE_HOST). - _create_session_endpoint reads an optional `bifrost_plane` from the browser body, builds the BifrostBinding SERVER-SIDE via endpoint_for_plane(plane, visible_host), and calls create_session(bifrost=, consumer_key=). The 201 response echoes bound-state {plane, endpoint, status: bound} for the UI indicator — never the key (INV-008/INV-009). - Error routing: invalid plane / unconfigured server -> 400; BifrostHandshakeFailed -> 502 {bifrost_error}; BifrostConsumerKeyMissing (server misconfig) -> 400. 5 new web bind tests (server constructs binding + key-never-leaks + upstream carries bifrost body + consumer-key bearer; unconfigured -> 400; invalid plane; handshake 502; no-plane unbound regression). Full suite 470 green; added lines ruff + mypy clean (pre-existing web-file backlog untouched). Follow-on: the index.html plane selector (UI trigger) — the server capability is complete and TDD'd; the browser-side dropdown is a thin separate change. LIVE-SMOKE PROVEN (this session): the CLI bind drove a bound sindra session against personal Worldtree :8081 -> handshake 200 -> the op-feed captured 2 recall searches correlated to the EXACT bound session_id (2c0c7482), with the real #297/#298 union-recall scopes. Bind + observe proven end-to-end live.
This commit is contained in:
@@ -792,3 +792,113 @@ class TestDisconnectCancel:
|
||||
await asyncio.sleep(0.02)
|
||||
gate.set()
|
||||
assert cancel_route.called, "browser disconnect must cancel the UPSTREAM turn (42)"
|
||||
|
||||
|
||||
class TestWebBifrostBind:
|
||||
"""Issue #17 slice 3c — web bind split: the browser selects the PLANE; the
|
||||
consumer key + visible host are SERVER-HELD and never reach the browser
|
||||
(INV-008/INV-009)."""
|
||||
|
||||
@respx.mock
|
||||
def test_bound_create_server_constructs_binding_key_never_leaks(self) -> None:
|
||||
"""tracer: a plane from the browser → the server builds the binding with
|
||||
its OWN consumer key + host, sends the bifrost body + consumer-key bearer
|
||||
upstream, and returns bound-state WITHOUT the key."""
|
||||
import json as _json
|
||||
|
||||
from ratatoskr.web.server import create_app
|
||||
|
||||
route = respx.post("https://w.example/sessions").mock(
|
||||
return_value=httpx.Response(201, json=_CREATE_OK)
|
||||
)
|
||||
app = create_app(
|
||||
_mock_client_factory(),
|
||||
bifrost_consumer_key="server-ck",
|
||||
bifrost_visible_host="10.100.10.50",
|
||||
)
|
||||
resp = TestClient(app).post(
|
||||
"/api/sessions", json={"agent_id": "ratatoskr:sindra", "bifrost_plane": "memory"}
|
||||
)
|
||||
assert resp.status_code == 201
|
||||
# bound-state echoed for the UI indicator — plane + endpoint, NO key
|
||||
assert resp.json()["bifrost"] == {
|
||||
"plane": "memory",
|
||||
"endpoint": "http://10.100.10.50:8391",
|
||||
"status": "bound",
|
||||
}
|
||||
assert "server-ck" not in resp.text # the key never reaches the browser
|
||||
# upstream got the bifrost body + the consumer-key bearer override
|
||||
upstream = route.calls[0].request
|
||||
body = _json.loads(upstream.content)
|
||||
assert body["bifrost"] == {
|
||||
"endpoint_url": "http://10.100.10.50:8391", "scope": None
|
||||
}
|
||||
assert upstream.headers["Authorization"] == "Bearer server-ck"
|
||||
|
||||
@respx.mock
|
||||
def test_plane_without_server_config_is_400(self) -> None:
|
||||
"""A plane requested but no server-held key/host → bifrost_not_configured."""
|
||||
from ratatoskr.web.server import create_app
|
||||
|
||||
app = create_app(_mock_client_factory()) # no bifrost config
|
||||
resp = TestClient(app).post(
|
||||
"/api/sessions", json={"agent_id": "a", "bifrost_plane": "memory"}
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.json()["error_code"] == "bifrost_not_configured"
|
||||
|
||||
def test_invalid_plane_is_400(self) -> None:
|
||||
from ratatoskr.web.server import create_app
|
||||
|
||||
app = create_app(
|
||||
_mock_client_factory(),
|
||||
bifrost_consumer_key="ck",
|
||||
bifrost_visible_host="h",
|
||||
)
|
||||
resp = TestClient(app).post(
|
||||
"/api/sessions", json={"agent_id": "a", "bifrost_plane": "persona"}
|
||||
)
|
||||
assert resp.status_code == 400
|
||||
assert resp.json()["error_code"] == "invalid_bifrost_plane"
|
||||
|
||||
@respx.mock
|
||||
def test_handshake_failure_is_502(self) -> None:
|
||||
from ratatoskr.web.server import create_app
|
||||
|
||||
respx.post("https://w.example/sessions").mock(
|
||||
return_value=httpx.Response(
|
||||
502,
|
||||
json={
|
||||
"error_code": "bifrost_handshake_failed",
|
||||
"detail": {"bifrost_error": "bifrost.auth_rejected"},
|
||||
},
|
||||
)
|
||||
)
|
||||
app = create_app(
|
||||
_mock_client_factory(),
|
||||
bifrost_consumer_key="ck",
|
||||
bifrost_visible_host="h",
|
||||
)
|
||||
resp = TestClient(app).post(
|
||||
"/api/sessions", json={"agent_id": "a", "bifrost_plane": "memory"}
|
||||
)
|
||||
assert resp.status_code == 502
|
||||
assert resp.json()["error_code"] == "bifrost_handshake_failed"
|
||||
assert resp.json()["bifrost_error"] == "bifrost.auth_rejected"
|
||||
|
||||
@respx.mock
|
||||
def test_no_plane_is_unbound_no_bifrost_in_response(self) -> None:
|
||||
"""regression: no bifrost_plane → pre-#17 unbound create, no bifrost key."""
|
||||
from ratatoskr.web.server import create_app
|
||||
|
||||
respx.post("https://w.example/sessions").mock(
|
||||
return_value=httpx.Response(201, json=_CREATE_OK)
|
||||
)
|
||||
app = create_app(
|
||||
_mock_client_factory(),
|
||||
bifrost_consumer_key="ck",
|
||||
bifrost_visible_host="h",
|
||||
)
|
||||
resp = TestClient(app).post("/api/sessions", json={"agent_id": "mimir"})
|
||||
assert resp.status_code == 201
|
||||
assert "bifrost" not in resp.json()
|
||||
|
||||
Reference in New Issue
Block a user