Files
interactive-story/backend/app/contextwindow.py
JesseMarkowitzandClaude Opus 5 ef25b0a876 Stop re-reading the whole prompt every turn, and let a lost run carry on
M01, the hundred-turn campaign, is the one REQUIRED test still
outstanding. Everything here is about it finishing, and being worth
believing when it does. No requirement changed, no acceptance test was
retired or relaxed, and M11 §P.1's "no performance requirement" still
stands: what changed is the cost of a turn, not what a turn contains.

An inference server caches a prompt by its prefix. The history window
gave up its oldest action every turn, which changed the prompt near the
front and threw that cache away, so nearly the whole prompt was
reprocessed every turn however little had actually changed. The window
now snaps the oldest depth to a block and holds it, stepping every few
turns. Measured on real builder output at an 8,192-token budget: 124.0s
per turn against 362.4s. The cost is history depth, bounded by
TRIM_FRACTION at a quarter of the window, which is the dial between
recent history and speed.

A run that dies no longer starts again from turn one. m11_long_run
checkpoints resume.json after the prologue, after every scheduled step
and after every turn, and --resume reattaches to the same campaign. A
finished run deletes it, so the file's presence means an unfinished run
and starting fresh over one is refused. The model timeout is an option
rather than a hard-coded 600s, a turn that overruns is a failed turn
instead of an unhandled exception that ends the run with no summary,
and a run that has stopped producing turns writes its evidence and
stops.

Two checks could not fail. M04's planted clue went into an add_fact
"detail" key that the event does not define, so it was dropped and
fact_still_in_state could never be true; it is now in "value" and
proved at turn one, which stops a run measuring nothing for hours.
m11_browser degraded silently without a narrator into two failures that
read exactly like a product regression, and now requires one, with
--no-narrator as an explicit opt-out that marks the run partial.

Window discovery speaks Ollama's native API, so against vLLM or
llama.cpp's own server the window goes unverified and the budget
uncapped -- M11's own failure mode reached by another route.
context_window_override lets the operator state what they launched the
server with, and is used only where discovery left a hole: a verified
window always wins, so a declaration can lower an unknown ceiling into
existence and never raise a known one. "verified" still means the
server answered, so window_verified in a turn's provenance keeps the
meaning M11's report counts on.

planning/README.md said the M11 tree was staged rather than committed,
in two places; it was committed and signed. Planning package v3.8.

Backend 1,376 passed, 17 skipped, 0 failed; frontend 161; lint and
build clean. Every M11 harness re-run on this tree: browser 38/0/0,
offline 23/0, identity clean, contrast unchanged, recovery 14/0 on a
small bundle. M01 itself has not been run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E9LiyxBxnMTXV2wRjdyDGB
2026-09-10 06:13:55 -04:00

329 lines
14 KiB
Python

"""M11: what the inference server will *actually* accept, as opposed to what we budgeted.
M8 found the failure this module exists to prevent. The application budgets a
prompt up to `Settings.context_token_budget` — 16,384 by default — while Ollama
enforces a window of its own, and on a machine with no VRAM that window defaults
to **4,096**. The request still returns HTTP 200. Nothing warns anybody. What
actually happens is worse than an error: `llama.cpp` drops the **oldest** tokens,
and the oldest tokens in this application are the system block — the narrator
rules and the campaign canon. The symptom is a narrator that forgets canon deep
into a long session, with nothing on screen explaining why, and every acceptance
test that reads a returned 200 as success passing throughout.
The invariant M11 requires:
The application must not silently budget more narrator input
than the configured Ollama runtime will actually accept.
Note the word *silently*. There are two honest outcomes and this module produces
both: either the window is **verified**, in which case the budget is capped to it
so the prompt physically cannot overflow; or it is **unverified**, in which case
the assembly says so, in the context report, on the connection test, and in the
turn's stored provenance. What must not happen is the third thing — assembling
16,384 tokens against a 4,096-token server and calling the result a turn.
## Why this is not solved by sending `num_ctx`
It was tried, and it is documented in `DEVELOPMENT.md`. Ollama's
OpenAI-compatible endpoint accepts `num_ctx` — nested in `options` or at the top
level — returns 200, and ignores it. Worse, it reloads the model at its own
default, so priming the server through the native API first does not help
either: the next request resets the window. The window is a property of how the
model is loaded, not of the request, so the only things that change it are a
model with `num_ctx` baked in (`/api/create`) or `OLLAMA_CONTEXT_LENGTH` on the
server. Both are operator actions. This module's job is not to change the
window; it is to find out what it is and refuse to lie about it.
## How the window is found
Ollama's native API sits beside the OpenAI-compatible one on the same host, so
this asks the server the application is already talking to, and nothing else. No
new destination, the same endpoint policy, the same TLS trust store.
/api/ps a loaded model reports `context_length`: the window the runtime
is enforcing *right now*. This is the truth when it is available.
/api/show an unloaded model may carry `num_ctx` in its baked parameters,
which is the window it will load with; `model_info` carries the
architecture's own ceiling, which caps everything else.
`/api/ps` is asked first because a model that is loaded has already settled the
question. `/api/show` answers it for a model that is not loaded yet, which is the
ordinary case at the start of a session.
## What it deliberately does not do
It does not hard-code 4,096, which would cripple a correctly configured
deployment; it does not raise the budget, which is the operator's decision; it
does not fall back to a cloud probe, a bundled table of model sizes, or a guess
from the model's name. An unknown window is reported as unknown.
## The server that cannot be asked
Discovery above is Ollama's native API. Nothing restricts `endpoint_url` to
Ollama — any allowed address serving an OpenAI-compatible `/v1` is accepted —
and on vLLM, llama.cpp's own server, or anything else, `/api/ps` and `/api/show`
are simply not there. Discovery then fails exactly as designed and the window is
reported unknown, which is honest but leaves the invariant at the top of this
file unenforced: the budget stands at whatever is configured, and if that server
enforces a smaller window it drops the oldest tokens again.
`context_window_override` is the operator's answer to that. It is a number the
operator states because they know how the server was launched, and it is used
**only when the server could not be asked**:
verified window -> always wins; a declaration cannot raise it
no verified window -> the declaration becomes the ceiling, source DECLARED
neither -> unknown, exactly as before
This does not weaken what `verified` claims. `verified` still means the server
itself answered, so `window_verified` in a turn's provenance keeps the meaning
the M11 report gives it, and a declared window is identifiable as a declaration
wherever it appears. What the declaration buys is enforcement: the prompt is
capped, so the failure mode is a shorter prompt rather than a silently truncated
one.
"""
from __future__ import annotations
import logging
import re
import time
from dataclasses import dataclass
import httpx
from . import endpoints, tlstrust
log = logging.getLogger(__name__)
#: Short, because this sits in the turn path. A server that does not answer in
#: two seconds has told us what we need to know: we cannot verify the window
#: right now, and the turn should proceed unverified rather than stall.
PROBE_TIMEOUT = 2.0
CONNECT_TIMEOUT = 1.5
#: A verified window is stable — it changes when an operator reloads a model —
#: so it is worth keeping. A failure is cached too, and for much less time,
#: because the commonest cause is a server that is starting up.
POSITIVE_TTL = 600.0
NEGATIVE_TTL = 60.0
#: Sources, in the order of how much they prove.
LOADED = "loaded" # /api/ps: what the runtime is enforcing now
PARAMETERS = "parameters" # /api/show: what the model will load with
DECLARED = "declared" # the operator said so; the server could not be asked
UNKNOWN = "unknown"
#: Sources that mean *the server answered*, as opposed to somebody asserting.
FROM_SERVER = (LOADED, PARAMETERS)
@dataclass(frozen=True)
class Window:
"""What was learned about the server's input window, and how."""
#: The total context in tokens — input *and* output share it — or None when
#: it could not be determined.
tokens: int | None
#: One of LOADED, PARAMETERS, UNKNOWN.
source: str
#: The architecture's own ceiling, when the server reported one. Useful to a
#: reader deciding whether raising the window is even possible.
model_max: int | None = None
#: Why the window is unknown, or how it was found. Shown to the user.
detail: str = ""
@property
def verified(self) -> bool:
"""The **server** answered. An operator's declaration is not this.
Kept narrow on purpose. `window_verified` travels in every turn's stored
provenance and the M11 report counts on it meaning one thing: that the
runtime was asked and replied. A declaration is a person's claim about a
server, which is worth acting on and is not the same evidence.
"""
return self.tokens is not None and self.source in FROM_SERVER
@property
def enforceable(self) -> bool:
"""There is a number to cap the prompt to, whoever supplied it."""
return self.tokens is not None
UNVERIFIED = Window(tokens=None, source=UNKNOWN, detail="not checked")
_cache: dict[tuple[str, str], tuple[float, Window]] = {}
def native_base(endpoint_url: str) -> str:
"""The Ollama-native base beside an OpenAI-compatible endpoint.
`https://host:1234/v1` -> `https://host:1234`. Anything else is used as
given, because an endpoint that is not shaped like Ollama's is one this
cannot interrogate and should not guess about.
"""
trimmed = (endpoint_url or "").rstrip("/")
return re.sub(r"/v1$", "", trimmed)
def effective_budget(configured: int, window: Window | int | None) -> int:
"""The budget the prompt may actually use.
The whole enforcement, in one line: a known window is a ceiling — whether
the server reported it or the operator declared it. The configured budget
still wins when it is *smaller*, because a reader who has deliberately asked
for a shorter prompt should get one.
"""
tokens = window.tokens if isinstance(window, Window) else window
if tokens is None or tokens <= 0:
return configured
return min(configured, tokens)
def cache_clear() -> None:
"""Forgets what was learned. Called when the endpoint or model changes."""
_cache.clear()
async def probe(endpoint_url: str, model: str, *,
declared: int | None = None, use_cache: bool = True) -> Window:
"""What window `model` gets, asked of the server and only then declared.
Returns `UNVERIFIED` for every discovery failure — refused endpoint,
unreachable server, TLS failure, a server with no Ollama-native API, an
unparseable answer — unless `declared` supplies a number to fall back on.
The caller cannot act differently on those failures and the reader is told
the same thing either way: the window could not be checked.
`declared` is `Settings.context_window_override`. It never overrides a
verified answer, so an operator cannot talk the application into a bigger
prompt than the runtime will read; it only fills a gap discovery left.
"""
if not endpoint_url or not model:
return _declared_or(declared,
Window(None, UNKNOWN,
detail="no endpoint or model configured"))
discovered = await _discover(endpoint_url, model, use_cache=use_cache)
return _declared_or(declared, discovered)
def _declared_or(declared: int | None, discovered: Window) -> Window:
"""The operator's number, but only where the server left a hole.
A verified window always wins. That ordering is the whole safety property:
a declaration can lower an unknown ceiling into existence, never raise a
known one.
"""
if discovered.verified:
return discovered
if not declared or declared <= 0:
return discovered
return Window(
declared, DECLARED, discovered.model_max,
f"{declared:,} tokens, declared in settings — the server was not able "
f"to say ({discovered.detail})",
)
async def _discover(endpoint_url: str, model: str, *,
use_cache: bool = True) -> Window:
"""The server's own answer, cached. Knows nothing about declarations.
The cache holds only what was discovered, so changing the declared override
takes effect on the next turn without having to clear anything: the
declaration is layered on afterwards, in `_declared_or`.
"""
key = (endpoint_url, model)
now = time.monotonic()
if use_cache:
hit = _cache.get(key)
if hit is not None and hit[0] > now:
return hit[1]
window = await _ask(endpoint_url, model)
ttl = POSITIVE_TTL if window.verified else NEGATIVE_TTL
_cache[key] = (now + ttl, window)
return window
async def _ask(endpoint_url: str, model: str) -> Window:
# The same policy the turn itself is held to. A window probe must not be a
# way to reach an address inference may not (ADR 011, H12).
reason = endpoints.rejection_reason(endpoint_url)
if reason is not None:
return Window(None, UNKNOWN, detail=f"endpoint not allowed — {reason}")
base = native_base(endpoint_url)
try:
async with httpx.AsyncClient(
timeout=httpx.Timeout(PROBE_TIMEOUT, connect=CONNECT_TIMEOUT),
verify=tlstrust.ssl_context(),
) as client:
loaded = await _loaded_window(client, base, model)
if loaded is not None:
tokens, ceiling = loaded
return Window(
tokens, LOADED, ceiling,
f"{tokens:,} tokens, reported by the running model",
)
return await _declared_window(client, base, model)
except (httpx.HTTPError, ValueError, TypeError, KeyError) as exc:
log.debug("context window probe failed for %s: %s", base, exc)
return Window(None, UNKNOWN, detail=f"could not ask the server ({type(exc).__name__})")
async def _loaded_window(client, base: str, model: str):
"""`/api/ps`: the window a resident model is actually being served with."""
resp = await client.get(f"{base}/api/ps")
if resp.status_code != 200:
return None
for entry in (resp.json() or {}).get("models") or []:
if entry.get("name") == model or entry.get("model") == model:
tokens = entry.get("context_length")
if isinstance(tokens, int) and tokens > 0:
return tokens, None
return None
async def _declared_window(client, base: str, model: str) -> Window:
"""`/api/show`: what the model will load with, and its architectural cap."""
resp = await client.post(f"{base}/api/show", json={"model": model})
if resp.status_code != 200:
return Window(
None, UNKNOWN,
detail=f"the server did not describe the model (HTTP {resp.status_code})",
)
body = resp.json() or {}
ceiling = _architecture_ceiling(body.get("model_info") or {})
declared = _num_ctx(body.get("parameters"))
if declared is None:
return Window(
None, UNKNOWN, ceiling,
detail=(
"the model sets no num_ctx, so the server will load it at its own "
"default — which is 4,096 where there is no VRAM"
),
)
tokens = min(declared, ceiling) if ceiling else declared
return Window(
tokens, PARAMETERS, ceiling,
f"{tokens:,} tokens, from the model's own num_ctx",
)
def _num_ctx(parameters) -> int | None:
"""Reads `num_ctx` out of the plain-text parameter block Ollama returns."""
if not isinstance(parameters, str):
return None
match = re.search(r"^\s*num_ctx\s+(\d+)\s*$", parameters, re.MULTILINE)
return int(match.group(1)) if match else None
def _architecture_ceiling(model_info: dict) -> int | None:
"""`<arch>.context_length` — the largest window this model can have."""
for key, value in model_info.items():
if key.endswith(".context_length") and isinstance(value, int) and value > 0:
return value
return None