94 files, +1,395 -6,578. Three files are new; twenty-four are gone. The milestone is subtraction, and what is left is the single-user local storyteller the specification describes. Removed in full: campaign scripting and its QuickJS sandbox; multi-user accounts, guest sessions, login, registration and the shared demo key; the visitor-analytics tables, dashboard and page beacon; the access log of sign-ins, addresses and devices; per-IP and per-user rate limiting and quotas; Render deployment config; Postgres and psycopg; cloud inference providers, the API-key field and the key encryption that existed to store it; session-cookie signing. None of it was hidden behind a flag — the routes are gone and answer 404. Two things were kept that the brief allowed keeping. The `users` table and its foreign keys stay as an internal ownership detail, because rewriting them out means a migration across most of the schema to delete a column that costs nothing; nothing creates a second user and no request carries an identity. Five inert tables and four inert columns stay for the same reason, so an M1 campaign database opens unchanged. The one addition is app/endpoints.py, which decides where a story may be sent. Loopback, RFC1918, link-local, unique-local and CGNAT — an explicit allowlist of networks, not a guess at what `ipaddress` means by "private", which calls the documentation ranges private and IPv6 loopback reserved. Every address a hostname resolves to must be in it, so a split answer does not squeak through, and the rule runs both when the endpoint is saved and before every outbound request, because a name that resolved to the LAN this morning can resolve elsewhere this afternoon. Known cloud hosts are named in the refusal so the error says why rather than looking like broken DNS. TLS is never traded against it: M1's shared trust context is intact on all four clients and there is no way to skip verification. The hardcoded 120-second model timeout is now a setting. That was not theoretical — on this GPU-less four-core host a cold load of qwen2.5:3b-instruct took 648.9 seconds to produce the first turn, while turns 2 to 5 of the same campaign took 3.6 to 13.1. Connect stays short at 10s so a wrong address still fails fast; the read timeout defaults to 300s and is bounded at 3600, because "wait longer" must stay a number. Two defects found while testing and fixed here. An unknown /api path fell through the SPA catch-all and came back as HTML with status 200, so a client asking for JSON parsed a web page instead of learning the route was gone. And AIDND_CORS_ORIGINS accepted "*", which on an unauthenticated loopback API would hand every page on the Internet a write handle on the campaign database; it now refuses to start. Verified rather than assumed. Offline, on a network with no route out and no DNS: five turns, retry with both takes retained, restart with an identical transcript digest, a failed model call leaving the accepted AI-turn count untouched, and a capture with zero non-loopback unicast packets. Against a real second machine on the LAN over HTTPS with a private CA: four turns, restart, and a capture showing 289 packets to the approved host, 344 loopback, zero anywhere else, zero DNS queries. Cloud and public endpoints refused with their reasons; no API key settable; every removed route 404. 604 backend tests pass, down from 648 by the fifteen retired with the subsystems they tested and up by the twenty-nine added for the endpoint policy and the removed surface. The scripting tests were not deleted: eight files used a JavaScript counter as instrumentation for the state snapshot and rollback machinery, which M2 does not touch, so the counter moved to the world-state engine and those tests still assert what they always did. Frontend lint and build are clean; the image builds, and its wheel-building stage is gone with quickjs. No M3 work. Undo is still destructive and there is still no Redo.
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
"""Resolving the one local user. **There is no authentication in this product.**
|
|
|
|
The module keeps its name so the dependency every router already depends on
|
|
keeps working, but nothing here authenticates anybody. The Adventure
|
|
Storyteller is a single-user application that binds to loopback: whoever can
|
|
reach the API is the person who started it, and there is nobody else to tell
|
|
them apart from.
|
|
|
|
Upstream had two modes. `AIDND_MULTI_USER` selected a hosted deployment with
|
|
signed session cookies, guest accounts, registration, login, a shared demo API
|
|
key with a per-day cap, "power users", and an owner allowlist for the analytics
|
|
dashboard. M2 removed all of it: this product has no hosted mode to protect, and
|
|
every one of those surfaces was a way for the application to be reached by
|
|
someone other than its owner.
|
|
|
|
What is left is the local path that upstream already had. Every request
|
|
resolves to one automatically created user row.
|
|
|
|
The `users` table and the `user_id` foreign keys on scenarios, adventures and
|
|
settings stay. They are an **internal ownership detail**, not an account
|
|
system: nothing creates a second user, nothing logs in, and no request carries
|
|
an identity. They remain because rewriting them out would mean a migration
|
|
across most of the schema to delete a column that costs nothing and keeps every
|
|
existing M1 database readable.
|
|
"""
|
|
|
|
from datetime import timezone
|
|
|
|
from fastapi import Depends, Request
|
|
from sqlalchemy.orm import Session
|
|
|
|
from . import models
|
|
from .database import get_db
|
|
|
|
|
|
def local_user(db: Session) -> models.User:
|
|
"""Returns the single implicit user, creating it on first use.
|
|
|
|
A migration gives this user ownership of data written before per-user rows
|
|
existed, so an older database resolves to the row that already owns its
|
|
campaigns rather than to a fresh empty one.
|
|
"""
|
|
user = (
|
|
db.query(models.User)
|
|
.filter(models.User.email.is_(None), models.User.is_guest.is_(False))
|
|
.order_by(models.User.id)
|
|
.first()
|
|
)
|
|
if user is None:
|
|
user = models.User(is_guest=False)
|
|
db.add(user)
|
|
db.commit()
|
|
return user
|
|
|
|
|
|
def _touch(user: models.User, db: Session) -> None:
|
|
now = models.utcnow()
|
|
last = user.last_seen_at
|
|
if last is not None and last.tzinfo is None:
|
|
# SQLite returns DateTime columns without a timezone. They were stored
|
|
# as UTC.
|
|
last = last.replace(tzinfo=timezone.utc)
|
|
if last is None or (now - last).total_seconds() > 3600:
|
|
user.last_seen_at = now
|
|
db.commit()
|
|
|
|
|
|
def get_current_user(
|
|
request: Request, db: Session = Depends(get_db)
|
|
) -> models.User:
|
|
"""The dependency every router uses. It always succeeds.
|
|
|
|
`request` is unused and kept so the signature stays a FastAPI dependency
|
|
the routers can depend on unchanged.
|
|
"""
|
|
user = local_user(db)
|
|
_touch(user, db)
|
|
return user
|