v1.1: harden context window and narrator protocol boundary

WP-A1 and WP-A2, implemented in sequence, plus the corrective work the owner
asked for at review. Reported in
planning/reports/v1.1/V1.1-WP-A1-A2-REPORT.md (corrective addendum §R).
Planning package v4.2.

WP-A1: context-window safety reserve
- The prompt leaves max(256, ceil(5% of the effective window)) tokens free
  beside the reply. That is 256 at 4,096 and 820 at 16,384. The value is fixed,
  not a setting, and not calibrated per model.
- M6's 64-token margin is gone. Separators and the chat hint are priced
  exactly; tokenizer drift is the reserve's job.
- Protected context that cannot fit raises ContextOverflow before the model
  is called.
- Streams set stream_options.include_usage. Measured on Ollama 0.33, a stream
  sent no usage without it.
- Each sent turn records fits, exceeded, truncation_suspected or unknown.
  The status is returned on the done event, logged when bad, and shown in the
  context inspector. The turn is always kept.
- Accounting is per-attempt data (attempts.ATTEMPT_KEYS).
- Corrective: a cold model is loaded before its turn is built. When the
  window is unverified but the server answered, contextwindow.ensure_window
  makes one bounded POST /api/generate naming only the model. It sends no
  prompt, generates nothing and writes nothing. It then probes again, and the
  turn is built to that answer. If the load fails, or the window is still
  unknown, the turn falls back to the old behaviour.
- Real host, 4,096 window:
  - v1 cold turn: sent 13,875, the server read 2,050.
  - Same turn after the correction: the window was verified, 3,082 sent,
    3,097 read, fits, 499 tokens left beside the reply.
  - Verified turns elsewhere left 275-2,297 tokens against v1's 23-42.

WP-A2: protocol echo and genre-neutral state prompting
- The vocabulary is shown as the JSON object the model sends, not as
  name(field, ...). This costs 121 tokens.
- The example uses character-1, item-1 and location-1.
- The extractor removes shapes anchored to application-owned text:
  - a vocabulary call line;
  - an echoed length hint;
  - the renderer's scene line left last;
  - an empty fence opener.
- Corrective R5: the echoed continue hint is recognised by its own sentence
  ("Output only story text"). A Hard-limit-opened bracket is removed only
  directly above an echo already cut from the same reply.
- Replay of all 518 real v1 replies: 9 changed, 0 flagged, and no story prose
  removed. That is unchanged by R5.
- Replay of 64 v1.1 replies: 3 changed, 0 flagged. The depth-16 instruction
  tail is removed.
- Identity diagnostic after the correction:
  - 0 identity signals;
  - 0 prompt example identifiers proposed;
  - 0/10 stored turns with protocol or instruction shapes.
- 50-turn run: 51 accepted, 0 of 54 stored turns carry protocol.
- SPECS, render.py and validate.py are identical to v1.0.0.

Compatibility: a real v1.0.0 database reads identically on v1.0.0 and v1.1,
field for field, with schema and user_version 94 unchanged. Undo, redo, Save
Point restore, export and import all work on it. There is no schema,
migration or bundle-format change.

Verification: the backend suite passes 1,534 with 17 skipped and 0 failed.
The frontend passes 165/165, and lint and the build are clean. The offline
container and the browser regression were re-run on this tree (see §R.3).

One test was re-calibrated, not weakened: test_history_block_trim's prefix
test had assumed which turn holds the floor at a 2,048 budget.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VvegagkhuCZoFPdv4M1egY
This commit is contained in:
JesseMarkowitz
2026-09-14 16:35:05 -04:00
co-authored by Claude Opus 5
parent ac465ed867
commit d63804f22e
26 changed files with 3903 additions and 61 deletions
@@ -47,6 +47,63 @@ function Section({ title, count, children, open = false, testId }) {
)
}
/* v1.1 WP-A1: what the server said it read, set against what was sent.
*
* Only a turn that was actually sent has this — the next-turn view has not
* been sent yet. The two conditions that mean something went wrong are shown
* as alerts, because the failure they describe is otherwise silent: Ollama
* answers 200 whether or not it cut the front of the prompt off. */
const ACCOUNTING = {
fits: {
title: 'The server read the whole prompt',
body: 'Its count stayed inside the room kept for the reply and the safety margin.',
},
exceeded: {
title: 'The prompt was larger than the server allowed for',
body: 'The server counted more tokens than the safety margin covers, so the '
+ 'reply may have been cut short. The turn is kept.',
alert: true,
},
truncation_suspected: {
title: 'The server may have cut the start of the prompt',
body: 'It read far fewer tokens than were sent, which is what happens when a '
+ 'prompt is larger than the window the model was loaded with. The '
+ 'narrator’s rules and the campaign canon are at the start. The turn is kept.',
alert: true,
},
unknown: {
title: 'The server did not say how much it read',
body: 'Nothing here can confirm whether the whole prompt was used.',
},
}
function AccountingReport({ accounting }) {
if (!accounting) return null
const copy = ACCOUNTING[accounting.status] || ACCOUNTING.unknown
return (
<div
className={copy.alert ? 'notice error' : 'ctx-accounting'}
role={copy.alert ? 'alert' : undefined}
data-testid="ctx-accounting"
data-status={accounting.status}
>
<strong>{copy.title}</strong>
<p>{copy.body}</p>
{accounting.server_prompt_tokens != null && (
<p className="notice-detail">
Sent {accounting.estimate?.toLocaleString()} by this app’s count; the
server read {accounting.server_prompt_tokens.toLocaleString()}.
{accounting.observed_margin != null
&& ` ${accounting.observed_margin.toLocaleString()} tokens were left beside the reply.`}
</p>
)}
{accounting.window_verified === false && (
<p className="notice-detail">The model’s window was not verified for this turn.</p>
)}
</div>
)
}
function TokenBar({ sections, total }) {
if (!sections.length || total <= 0) return null
return (
@@ -122,6 +179,12 @@ export function ContextPanel({
<span>{tokens.output_reserve.toLocaleString()}</span>
</div>
)}
{tokens.safety_reserve > 0 && (
<div className="ctx-token-line dim">
<span>Kept free as a safety margin</span>
<span>{tokens.safety_reserve.toLocaleString()}</span>
</div>
)}
<div className="ctx-token-line dim">
<span>What the model can hold</span>
<span>{tokens.budget.toLocaleString()}</span>
@@ -135,6 +198,8 @@ export function ContextPanel({
)}
</div>
<AccountingReport accounting={report.accounting} />
{failing.length > 0 && (
<div className="notice error" role="alert" data-testid="ctx-derived-failing">
<strong>Background work is failing</strong>
@@ -220,6 +220,50 @@ describe('context inspector (§23, §55)', () => {
expect(tokens).toHaveTextContent('8,000')
})
it('shows the safety margin kept free beside the reply (v1.1 A1)', async () => {
api.getAdventureContext.mockResolvedValue({
...REPORT, tokens: { ...REPORT.tokens, safety_reserve: 410 },
})
await renderWith(<ContextPanel advId="1" refreshKey="x" />)
expect(screen.getByTestId('ctx-tokens')).toHaveTextContent(/safety margin\s*410/)
})
it('says nothing about accounting for a turn that has not been sent', async () => {
await renderWith(<ContextPanel advId="1" refreshKey="x" />)
expect(screen.queryByTestId('ctx-accounting')).toBeNull()
})
it('alerts when the server may have cut the start of a sent prompt (v1.1 A1)', async () => {
vi.spyOn(api, 'getActionContext').mockResolvedValue({
...REPORT,
accounting: {
status: 'truncation_suspected', estimate: 6316, server_prompt_tokens: 2050,
observed_margin: 1546, window_verified: true,
},
})
await renderWith(
<ContextPanel advId="1" inspectActionId="9" refreshKey="x" onClearInspect={() => {}} />)
const el = screen.getByTestId('ctx-accounting')
expect(el).toHaveAttribute('data-status', 'truncation_suspected')
expect(el).toHaveAttribute('role', 'alert')
expect(el).toHaveTextContent('6,316')
expect(el).toHaveTextContent('2,050')
expect(el).toHaveTextContent(/turn is kept/)
})
it('reports an unknown count plainly, without claiming the prompt fit', async () => {
vi.spyOn(api, 'getActionContext').mockResolvedValue({
...REPORT, accounting: { status: 'unknown', server_prompt_tokens: null },
})
await renderWith(
<ContextPanel advId="1" inspectActionId="9" refreshKey="x" onClearInspect={() => {}} />)
const el = screen.getByTestId('ctx-accounting')
expect(el).toHaveAttribute('data-status', 'unknown')
expect(el).not.toHaveAttribute('role')
expect(el).toHaveTextContent(/did not say/)
expect(el.textContent).not.toMatch(/read the whole prompt/)
})
it('shows a retrieved passage with its source, class, heading and score', async () => {
await renderWith(<ContextPanel advId="1" refreshKey="x" />)
const row = document.querySelector('[data-chunk-id="11"]')
+10
View File
@@ -32,6 +32,16 @@
font-variant-numeric: tabular-nums;
}
.ctx-warn { margin: 8px 0 0; color: var(--danger); font-size: 0.76rem; }
/* v1.1 A1: what the server said it read. The two failure states render as a
`.notice.error` alert instead; this is the quiet form for `fits` and
`unknown`. */
.ctx-accounting {
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 7px;
color: var(--text-dim);
}
.ctx-accounting p { margin: 4px 0 0; }
.token-bar {
display: flex;