M11: what the server will actually read
The release-validation milestone, and the thing it had to settle first was whether any of the earlier evidence meant what it said. M8 measured a deployment enforcing a 4,096-token input window while the application budgeted 16,384. Every request returned 200. What Ollama does with the excess is drop the oldest tokens, and the oldest tokens here are the system block — the narrator's rules and the campaign canon. A hundred-turn certification against that server would have looked perfect and proved nothing, which is why this milestone could not begin with a hundred turns. So the application asks now. Ollama's window is a property of how a model was loaded rather than of the request — sending num_ctx is accepted, ignored, and worse, reloads the model at the server's own default — so the only honest move is to find out and then tell the truth about it. /api/ps reports what a resident model is being served with, /api/show what an unloaded one will load with, both on the same host inference already uses, through the same endpoint policy and the same TLS trust store. A verified window is a ceiling on the budget; an unverified one leaves the budget alone and is recorded as unverified in the turn's own provenance, so an old turn can be asked afterwards whether it was built against a checked window. There is no third behaviour, and in particular no hard-coded 4,096: a number the server did not say would be right on one machine and wrong on the next. The proof that this is doing something is a campaign whose canon sits at the front of the prompt, 120 turns of history, and a 4,096-token window. The canon is still there afterwards and the oldest history is gone. The same campaign built the old way produces a prompt more than twice the window — the defect, reproduced, so the fix is measured against it rather than asserted. Two defects the validation found on its own, and they are the same defect twice: something was true and nobody was told. A manual state correction of four changes with one bad reference applied three, returned 201, and said nothing — while recording the refusal on the audit row nobody reads. It came to light because the identity diagnostic's own fixture was refused that way and the whole run proceeded on a campaign with no scene, which would have read as a model failure. And the narration-length setting moved no number: brief, medium and long each became one English sentence, while the numeric hint the model actually reads was derived from the global reply cap and said the same thing for all three. Both now say what they did. The other two post-M8 findings are closed as well. The tab said AI D&D, which no document had ever claimed it did not; it says Interactive Story now, with the open campaign first, and the name is the owner's decision rather than a find-and-replace to something narrower than the engine. After an Undo the reader could not tell where they had landed; the control row now ends with "Moment 11 · later story ahead", from the server's own answer, in the word the transcript already uses, with none of head, branch or depth anywhere near it. The identity diagnostic exists and the root cause does not. That campaign was destroyed, so no cause can be established — what M11 owes the finding is something that can classify the next occurrence, and a diagnostic that makes only the judgements a program can honestly make: duplicate keys, shared names, protagonist drift, state and context disagreeing. Whether prose misattributed a line is left to a person reading it beside its prompt, because a regex cannot read dialogue and one that pretended to would produce exactly the confident wrong answer this finding is about. Its detectors are proved to fire against a planted second Alice. Two entities may still share a display name. That was checked first, as the finding asked, and left permitted: a mother and a daughter, or a stranger giving a false name, are ordinary fiction, and refusing them to guard against a model mistake would refuse the wrong thing. What was missing was that it happened silently. It is reported now. Evidence, not inference: a hundred accepted turns against a real narrator with genuine process restarts; a real browser against the built SPA; a container with no network at all; a campaign moved into a data directory that never existed. Each was discarded and re-run whenever the product changed under it, and the runs that were thrown away are listed in the report with the reason, along with ten defects in the harnesses themselves — because a harness that has only ever agreed with itself is not evidence, and two of M8's five harness defects were masking real ones. No dependency was added, removed or upgraded. No acceptance test was retired, relaxed or reclassified. M11 is implemented and verified; it is not accepted, and there is no release tag. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qyn3oRd4D6pi72nKBG725B
This commit is contained in:
co-authored by
Claude Opus 5
parent
1013c94eb1
commit
144406cd48
+107
-10
@@ -23,7 +23,7 @@ from dataclasses import dataclass
|
||||
import tiktoken
|
||||
from sqlalchemy.orm import object_session
|
||||
|
||||
from .. import derived, models, narrative, summaries, worldstate
|
||||
from .. import contextwindow, derived, models, narrative, summaries, worldstate
|
||||
from ..knowledge import inject as knowledge_inject
|
||||
from ..knowledge import records as knowledge_records
|
||||
from . import encoding, history
|
||||
@@ -64,6 +64,29 @@ MIN_LENGTH_FLOOR_WORDS = 60
|
||||
# reader who wants longer turns can ask for them in the author's note.
|
||||
MAX_LENGTH_FLOOR_WORDS = 300
|
||||
|
||||
#: M11, post-M8 finding C: what the campaign's own narration-length choice means
|
||||
#: in words. Until M11 the choice became one English sentence in the campaign's
|
||||
#: instructions and moved no number at all, while the numeric hint below was
|
||||
#: derived from the *global* `max_output_tokens` and therefore read identically
|
||||
#: for brief, medium and long — at the default cap, "must not exceed 506 words,
|
||||
#: and it should not stop short of about 177" whichever the reader picked. A
|
||||
#: setting with a visible control and no measurable effect is worse than no
|
||||
#: setting, because the reader spends trust on it.
|
||||
#:
|
||||
#: These bands are (floor, ceiling) in words. They are a design decision made
|
||||
#: here rather than a ratified requirement — `BUILD-MILESTONES.md` records
|
||||
#: "Brief ~100-200 words" as a candidate — and they are deliberately wide enough
|
||||
#: that a scene can breathe inside one.
|
||||
LENGTH_BANDS = {
|
||||
"brief": (70, 180),
|
||||
"medium": (150, 380),
|
||||
"long": (320, 700),
|
||||
}
|
||||
#: Where the floor lands when a band's ceiling has to be cut down to fit the
|
||||
#: token cap: keep it proportional rather than letting it collide with the
|
||||
#: ceiling.
|
||||
BAND_FLOOR_SHARE = 0.5
|
||||
|
||||
|
||||
# Built from the table vendored in `encoding.py`, not fetched: the upstream
|
||||
# `tiktoken.get_encoding("cl100k_base")` downloads it on first use, and this
|
||||
@@ -111,16 +134,49 @@ class Section:
|
||||
return count_tokens(self.text)
|
||||
|
||||
|
||||
def length_hint(max_output_tokens: int) -> str:
|
||||
def length_hint(max_output_tokens: int, narration_length: str = "") -> str:
|
||||
"""Ask for a turn that fits inside the output cap, stated as a word budget.
|
||||
|
||||
Returns an empty string when the cap is too small to state usefully. The
|
||||
model can exceed the hint, so the hint earns its tokens only when there is
|
||||
enough room for that overshoot to stay inside the cap.
|
||||
|
||||
M11: `narration_length` is the campaign's own choice — `brief`, `medium` or
|
||||
`long`, or empty for a campaign that never made one. It narrows the range
|
||||
*within* what the token cap allows; it can never widen it, because the cap
|
||||
is what the endpoint will actually emit and a hint that asked for more than
|
||||
that would be asking for a truncated turn.
|
||||
|
||||
**The generation budget is deliberately not touched.** Capping
|
||||
`max_output_tokens` per length would make a brief turn likelier to hit the
|
||||
endpoint's limit mid-sentence, and the state block is emitted *last* — so
|
||||
the first thing a truncated reply loses is the turn's state. That is the
|
||||
trade `BUILD-MILESTONES.md` names when it says "do not hard-truncate prose".
|
||||
"""
|
||||
words = int((max_output_tokens - LENGTH_HEADROOM) * WORDS_PER_TOKEN * LENGTH_BUFFER)
|
||||
if words < MIN_LENGTH_HINT_WORDS:
|
||||
return ""
|
||||
|
||||
band = LENGTH_BANDS.get((narration_length or "").strip().lower())
|
||||
if band is not None:
|
||||
band_floor, band_ceiling = band
|
||||
# The cap still wins. A `long` campaign on a 300-token reply cap gets
|
||||
# the cap's number, not 700, and the floor moves down with it.
|
||||
words = min(words, band_ceiling)
|
||||
floor = min(band_floor, int(words * BAND_FLOOR_SHARE))
|
||||
tail = (
|
||||
" Finish the narration and append the state block well inside the limit."
|
||||
)
|
||||
if floor < MIN_LENGTH_FLOOR_WORDS:
|
||||
return (
|
||||
f"[Hard limit: this turn must not exceed {words} words. Write only as "
|
||||
f"much as the moment needs — a typical turn is much shorter.{tail}]"
|
||||
)
|
||||
return (
|
||||
f"[Hard limit: this turn must not exceed {words} words, and it should not "
|
||||
f"stop short of about {floor}. Prefer the lower end of that range unless "
|
||||
f"the scene genuinely needs more.{tail}]"
|
||||
)
|
||||
tail = " Finish the narration and append the state block well inside the limit."
|
||||
|
||||
# State the number as a ceiling, never as a budget. In measurements, the
|
||||
@@ -291,6 +347,7 @@ def build_context(
|
||||
memory_bank: dict | None = None,
|
||||
exclude_action_id: int | None = None,
|
||||
knowledge: knowledge_records.Result | None = None,
|
||||
window: contextwindow.Window | None = None,
|
||||
) -> tuple[str, str, dict]:
|
||||
"""Returns (system_text, story_text, context_report). `memory_bank` is the
|
||||
result of memorybank.retrieve_memories (None when the bank is off);
|
||||
@@ -302,7 +359,22 @@ def build_context(
|
||||
embedding call, this function is synchronous, and a prompt builder that can
|
||||
make network requests is a prompt builder that can fail halfway through a
|
||||
prompt. None means the campaign has no library, or the caller did not ask.
|
||||
|
||||
M11: `window` is what the inference server was found to actually accept
|
||||
(`contextwindow.probe`), and it arrives the same way and for the same
|
||||
reason — asking the server is a network call and this function does not make
|
||||
those. A **verified** window is a ceiling on the configured budget, which is
|
||||
the whole of M11's no-silent-overflow invariant: the prompt this returns
|
||||
cannot be longer than what the runtime will read, so `llama.cpp` never gets
|
||||
the chance to drop the system block off the front. `None` means nobody
|
||||
checked, and then the configured budget stands and the report says it was
|
||||
not verified.
|
||||
"""
|
||||
# M11: the budget every section below is priced against. Capped by what the
|
||||
# server was verified to accept; the configured value when nothing was
|
||||
# verified, or when the reader has asked for something smaller.
|
||||
budget = contextwindow.effective_budget(settings.context_token_budget, window)
|
||||
|
||||
script_mem = _script_memory(adventure)
|
||||
# M7: priced before anything else, because the answer changes what is left.
|
||||
# `plan` prices only the protected half — the untrusted-data rule and any
|
||||
@@ -310,7 +382,7 @@ def build_context(
|
||||
knowledge_plan = knowledge_inject.plan(
|
||||
knowledge if knowledge is not None else knowledge_records.Result(),
|
||||
count_tokens,
|
||||
settings.context_token_budget,
|
||||
budget,
|
||||
)
|
||||
|
||||
# ----- The static block, which is identical on every turn -----
|
||||
@@ -431,7 +503,7 @@ def build_context(
|
||||
if isinstance(script_mem.get("frontMemory"), str):
|
||||
front_memory = script_mem["frontMemory"].strip()
|
||||
|
||||
length_note = length_hint(settings.max_output_tokens)
|
||||
length_note = length_hint(settings.max_output_tokens, adventure.narration_length)
|
||||
|
||||
# The live sections sit below the history, but they are still part of the
|
||||
# prompt, so they still count against the budget. `world_lore` is the
|
||||
@@ -465,7 +537,7 @@ def build_context(
|
||||
# what it absorbs does not scale with the budget.
|
||||
output_reserve = max(0, settings.max_output_tokens) + OUTPUT_SAFETY_MARGIN
|
||||
protected = reserved + output_reserve
|
||||
if protected >= settings.context_token_budget:
|
||||
if protected >= budget:
|
||||
# Failing here is the point. The alternative — carrying on with a token
|
||||
# or two of history — builds a prompt that is known to overflow, and
|
||||
# the reader gets a truncated reply with no explanation. §32: "fail
|
||||
@@ -473,11 +545,18 @@ def build_context(
|
||||
raise ContextOverflow(
|
||||
f"The protected context needs {protected} tokens "
|
||||
f"({reserved} of prompt plus {output_reserve} reserved for the "
|
||||
f"reply) but the context budget is {settings.context_token_budget}. "
|
||||
"Raise the context budget, lower the maximum reply length, or "
|
||||
"shorten the campaign's canon, instructions and persona."
|
||||
f"reply) but the context budget is {budget}. "
|
||||
+ (
|
||||
"That budget is what this server was found to accept, so raising "
|
||||
"the setting alone will not help — load the model with a larger "
|
||||
"window. Or lower the maximum reply length, or shorten the "
|
||||
"campaign's canon, instructions and persona."
|
||||
if budget < settings.context_token_budget else
|
||||
"Raise the context budget, lower the maximum reply length, or "
|
||||
"shorten the campaign's canon, instructions and persona."
|
||||
)
|
||||
)
|
||||
available = settings.context_token_budget - protected
|
||||
available = budget - protected
|
||||
|
||||
# ----- M7: retrieved imported knowledge, out of a share of `available` -----
|
||||
#
|
||||
@@ -644,12 +723,30 @@ def build_context(
|
||||
# protected was subtracted.
|
||||
"tokens": {
|
||||
"total": count_tokens(system_text) + count_tokens(story_text),
|
||||
"budget": settings.context_token_budget,
|
||||
"budget": budget,
|
||||
"configured_budget": settings.context_token_budget,
|
||||
"output_reserve": output_reserve,
|
||||
"protected": reserved,
|
||||
"available_for_history": available,
|
||||
"history_spent": spent,
|
||||
},
|
||||
# M11: what the server was found to accept, and how. `verified` false
|
||||
# means nobody could check — the prompt was built to the configured
|
||||
# budget and may be larger than the runtime will read. This travels in
|
||||
# the stored snapshot, so a turn taken against an unverified window is
|
||||
# identifiable afterwards rather than indistinguishable from a safe one.
|
||||
"window": {
|
||||
"verified": (window.verified if window is not None else False),
|
||||
"tokens": (window.tokens if window is not None else None),
|
||||
"source": (window.source if window is not None else contextwindow.UNKNOWN),
|
||||
"model_max": (window.model_max if window is not None else None),
|
||||
"detail": (window.detail if window is not None else "not checked"),
|
||||
"capped": (
|
||||
window is not None
|
||||
and window.verified
|
||||
and window.tokens < settings.context_token_budget
|
||||
),
|
||||
},
|
||||
"cards": card_records,
|
||||
"memories": memory_bank,
|
||||
# M6: which summary was used, and which stretch of story it covers, so
|
||||
|
||||
Reference in New Issue
Block a user