Files
loki/scripts/test
CatoandClaude Opus 5 46ea5d57a5 fix(loki): a severed answer looked exactly like a finished one (#793)
Found by using the chat. Asked Loki a question on prod; it answered in 10.8s
and the reply ended mid-word:

  - The active Claude sessions reported right now are running on `research-

An unclosed backtick, a dangling hyphen, and nothing anywhere saying the
answer was incomplete. The stored row is 243 chars and genuinely ends there,
so this was severed at generation, not at render.

`grep -rn finish_reason src/ home/` returned ZERO. The field rides the same
frames this code already parses for `usage` — the one field that separates a
finished answer from a severed one — and both readers dropped it:
readBufferedBody destructures only `message`, readStreamedBody only `delta`
and `usage`. So there was no way for anything to know, and a fragment was
stored, billed, and rendered exactly like a complete reply.

This is not new. `frontier/propose.ts` carries a comment about an answer
"cut in half. Observed in prod at maxTokens 3000", worked around by raising a
number rather than by noticing the condition. And sse-stream.ts opens by
warning that "a stream that drops frames does not fail — it silently
truncates an answer", then never reads the vendor's own statement about it.

What this does NOT do is guess. It reads the reason, classifies it, and says
so. `cutOff` is carried on the turn but nothing renders it yet: what a severed
answer should LOOK like is a product decision, and the grounding `Violation`
channel that would be its natural home is owned by @bitbaum/ai-kit, so
widening that is a change to make there, deliberately.

Three rules worth keeping, each pinned:

- ORDERING. The frame carrying `finish_reason` routinely carries an empty
  delta, so the stop reason must be read BEFORE the `if (!delta) return`
  guard. Read after it, the field is dropped and the answer looks whole
  forever — the original bug, reintroduced.
- SILENCE IS A THIRD STATE. A vendor that omits the field has told us
  nothing. Reporting that as a problem would fire on every quiet provider,
  and a warning that always fires is one nobody reads.
- AN UNKNOWN REASON IS SUSPICIOUS, NOT FINE. Vendors add stop reasons; a new
  one meaning "cut off" must not read as healthy merely because the switch
  predates it. Unrecognised values are surfaced verbatim, named.

Measured before writing any of it: 1 of 183 stored assistant replies is
truncated — this one. Rare, which is exactly why it needs a machine to notice
rather than a reader. (My first detector claimed 28; it was wrong — 11 of 12
samples simply ended with a URL. The honest count came from reading them.)

Proved by MUTATION, both ways: moving the read below the delta guard fails 2
of 9 cases; defaulting an unknown reason to "fine" fails 1.

verify: format:check, tsc, lint (0 errors, 2 pre-existing warnings),
check:design (10), check:desktop, test:home 140/140. test:unit 203/205 —
activity-time-zone-stable and telegram-allowlist fail with "probe failed:"
and an empty reason; both need a live server and this diff touches neither
them nor anything they import.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 09:38:00 -07:00
..
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00