Skip to content

Answer each page route with a starlette response - #777

Merged
max-sixty merged 1 commit into
mainfrom
starlette-routes
Sep 18, 2026
Merged

max-sixty merged 1 commit into
mainfrom
starlette-routes

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

TODO.md's item 25. #774 moved the transport to starlette and uvicorn, but the per-request object kept BaseHTTPRequestHandler's shape: a route left its answer on self.response after assembling a _headers list through send_header and end_headers, and a close_connection flag decided whether the answer ended the connection. That is a compatibility layer inside a finished cutover, so it goes rather than getting renamed.

Behaviour at the HTTP boundary is unchanged, deliberately: same status codes, same header sets on the same paths, same refusals, same Connection: close on a declared body no route drained.

Reading the diff

skills/leaf/scripts/leaf/http.py carries the change; worker/server.py and skills/leaf/scripts/leaf/mcp_page.py are the two transports that subclass it, and hosting.py is the seam to uvicorn.

Routes return responses. _get, _post, _select_page and the _serve_* helpers each return a starlette response, or None where they have nothing to answer with. self.response is gone.

One place for the shared headers. _delivery_headers() returns the Leaf-* delivery identity, the key cookie, nosniff, and Connection: close, and respond() applies it once to whatever the route returned. The website adapter extends it ({"Leaf-Session": "active", **super()._delivery_headers()}) instead of overriding end_headers. The key gate was already the single gate in _answer and stayed there. The layer gate stays on the POST path, where it needs the view revision the request names; response_layer and set_cookie are both decided inside routes, so ASGI middleware would still leave the routes writing to request.state and would make the website's extra header a second middleware wrapping an app that does not otherwise exist.

Construction. Handler becomes PageEndpoint, bound to its page by an ordinary constructor rather than by a type() subclass, so handler_for becomes page_endpoint and hands back a partial. LeafHTTPServer takes that endpoint and now owns the two facts that were living on the handler class: server_id, the incarnation a tab watches, and viewed_at, the news stream's reader-recency throttle.

Fallout. self.command becomes self.method; the path and query arrive already apart, removing the fourteen urlsplit(self.path) calls that split them again per route; layer is derived from layer_identity rather than stored beside it, which drops an assignment each multiplexed transport had to remember; and the website adapter's bootstrap binding, which nothing read, is deleted.

Checks

Green locally: ruff, the everyday suite (1138), and a complete uv run pytest tests --run-nightly (2703). No TypeScript changed, so neither the worker nor the browser-framework gate is in scope.

This was written by Claude Code on behalf of max-sixty

#774 moved the transport to starlette and uvicorn, but the per-request object
kept `BaseHTTPRequestHandler`'s shape: a route left its answer on
`self.response` after assembling a `_headers` list through `send_header` and
`end_headers`, and a `close_connection` flag decided whether the answer ended
the connection.

Each route now returns the response it answers with. The headers every response
carries — the `Leaf-*` delivery identity, the key cookie, `nosniff`, and
`Connection: close` where a route declined to drain a declared body — are one
`_delivery_headers()`, applied once in `respond()`; the website adapter extends
that instead of overriding `end_headers`. The key gate stays the single gate in
`_answer`, and the layer gate stays on the POST path, where it needs the view
revision the request names.

`Handler` becomes `PageEndpoint`, bound to its page by an ordinary constructor
rather than by a `type()` subclass, so `handler_for` becomes `page_endpoint` and
hands back a `partial`. The server incarnation id and the news stream's
reader-recency throttle move onto `LeafHTTPServer`, which is what they are facts
about. `self.command` becomes `self.method`, and the path and query arrive
already apart, which removes the fourteen `urlsplit(self.path)` calls that split
them again per route. `layer` is derived from `layer_identity` rather than
stored beside it, and the website adapter's `bootstrap` binding, which nothing
read, is deleted.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit d8c873d into main Sep 18, 2026
5 checks passed
@max-sixty
max-sixty deleted the starlette-routes branch September 18, 2026 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants