Trim comments to the non-obvious why
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s

Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 20:49:03 +00:00
co-authored by Claude Opus 5.5
parent effddd2794
commit 4530cb8dc4
222 changed files with 1173 additions and 5423 deletions
+9 -39
View File
@@ -1,29 +1,8 @@
"""Small Python API service.
"""HTTP skin for `travelodge` and `rowing`.
Caddy proxies https://www.<DOMAIN>/py/... to this container and strips the
/py prefix, so routes here are defined relative to "/". FastAPI's root_path
(set via --root-path on the uvicorn command line) keeps the generated docs
at /py/docs working behind the proxy.
Endpoints
GET / service index
GET /health liveness probe
GET /hello/{name} demo route
GET /hotels/origins the known travel origins the UI offers
POST /hotels/search blocking cheapest-Travelodge search
POST /hotels/search/stream same search, streamed as NDJSON
POST /hotels/scan/stream cheapest-night-in-a-range scan, NDJSON
POST /internal/rowing/read read a rowing display photo with Claude
(Go backend only - not reachable through Caddy)
All the hotel work lives in `travelodge`; this module is the HTTP skin around
it - request validation, a response cache, the one-search-at-a-time guard, and
the NDJSON framing. The rowing reader (`rowing`) is the one route here that
is not public: see the "Internal" sections at the bottom, which open with the
wrapper it is registered through. The hotel routes are consumed by
vue/src/views/hotels/ (SingleStayPanel.vue,
CheapestNightPanel.vue, useHotelForm.js), which fetch these paths with the /py
prefix. Field names in the models below are part of that contract.
Caddy strips /py, so routes are relative to "/" and --root-path keeps
/py/docs working. Field names in the hotel models are a contract with
vue/src/views/hotels/. /internal/ routes are for the Go backend only.
"""
import json
@@ -88,10 +67,7 @@ MAX_SCAN_DAYS = travelodge.SCAN_MAX_DAYS
CACHE_TTL_S = 10 * 60
CACHE_MAX = 200
# The service prices London hotels and London transport, so "today" means today
# in London - not on whatever timezone the server happens to be set to. Using
# the server's local date refused a check-in that was still today for a user
# west of it (and, on a UTC server during BST, accepted one an hour late).
# Dates are London dates, whatever the server's timezone.
LONDON_TZ = ZoneInfo("Europe/London")
@@ -99,15 +75,10 @@ def london_today() -> date:
"""Today's date in London, the reference for "today or later" validation."""
return datetime.now(LONDON_TZ).date()
# Only one live search or scan may run at a time, process-wide. A run can spend
# the entire anonymous TfL budget (50 requests/min), so two concurrent runs
# would simply starve each other into rate-limit backoff. Callers that find the
# slot taken get an immediate "try again" rather than queueing.
# One run at a time: a run can use the whole anonymous TfL budget. Others get
# an immediate "try again".
_search_slot = threading.Semaphore(1)
# Finished responses, keyed by the canonical request body. Bounded as well as
# swept: the one-at-a-time guard makes growth slow, but "slow" is not "never",
# and a response holds every row of a scan. Eviction is insertion order, so a
# repeatedly-requested entry still ages out and is refetched.
# Bounded because one response holds every row of a scan.
_cache = travelodge.TTLCache(CACHE_TTL_S, max_entries=CACHE_MAX)
@@ -726,8 +697,7 @@ MAX_IMAGE_B64_CHARS = 14_000_000
class RowingReadRequest(BaseModel):
"""An image for Claude to read, as the Go backend sends it."""
# The types the Anthropic API accepts for an image block. The Go handler
# checks the same list first so the admin gets its friendlier 400.
# The Anthropic API's image types; the Go handler checks them first.
media_type: Literal["image/jpeg", "image/png", "image/gif", "image/webp"]
data: str = Field(..., min_length=1, max_length=MAX_IMAGE_B64_CHARS, description="Base64 image bytes")