Files
LLeMbas/tests/test_layout_bounds.py
Jaroslav Beneš 5766446b84 Files, open beside the conversation
A third side panel, built the way the terminal is and filled the way the
inspector is: tabs holding open files. Project files over SFTP in an agent
chat; notes, skills, knowledge documents, this chat's text attachments and its
own scratch document everywhere. Read with pygments, edited in a plain
textarea, saved with a conflict check.

A bug found on the way in, and the reason this needed its own read path.
`ssh.read_file` ends in `clean_output`, which strips ANSI escapes and decodes
with errors="replace" -- right for the output of a command, and fatal for an
editor: open a file containing an escape byte, press Save, and you have
silently rewritten it with the escapes gone and every undecodable byte replaced
by U+FFFD. `read_text`/`write_text` decode strictly, report binary rather than
mangling it, carry an mtime:size token for a file that moved underneath, and
refuse an oversize write rather than truncating -- `write_file` truncates
because a model is told how many bytes it wrote, and somebody pressing Save is
not. The model-facing pair is untouched: what it returns is a contract a model
has been shown. A truncated read opens read-only for the mirror-image reason.

Six sources go through one dispatch table, for the reason tool_labels.py is a
table: six independently written permission checks is how one ends up written
slightly differently, and that failure looks like editing somebody else's note.

A save on a project file bypasses agent/policy.py, which makes it the fourth
documented exception to "the modes do not govern the keyboard" and the first
that writes. Same argument as the terminal panel -- whoever owns the credential
could write the file with scp -- but the consequence is larger and is now said
out loud rather than left to be inferred.

The model opens tabs from the file tools it was already calling, so no new
schema and no tokens. It never brings one to the front: an agent reads forty
files in a long reply, and taking the screen each time would drag somebody
through all of them and lose any edit in progress. Only the strip is streamed,
guarded on truthiness so the frame can never blank itself -- an empty one would
close every open tab, the approval card you could press twice with the sign
reversed. Both halves are settled on the server, which is why canvas.js needs
no guard against a swap at all.

No vendored editor. CodeMirror 6 needs a bundler, which is hard rule 1;
CodeMirror 5 would be a larger payload than xterm on every page, and xterm is
the one heavy dependency precisely because it loads only where it can be used.
So: server-rendered highlighting for reading, a textarea for writing, and the
panel says there is no colour while you type rather than pretending.

Also here: a scratch document per chat, with `scratch_write` at RISK_READ on
plan_update's argument, and a test pinning the three numbers that decide a
panel's width -- LAYOUT_BOUNDS drops an unknown variable silently, so a panel
missing from it has a drag handle that works and forgets.

Driven under a DOM stub and against the running application.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-04 09:21:03 +02:00

75 lines
2.7 KiB
Python

"""Panel widths: three numbers per panel, in three files, that must agree.
`set_layout` drops a CSS variable it does not recognise, and it drops it
silently -- an older browser sending a key a newer release removed must not fail
the whole request. The cost of that kindness is that a panel whose width is
missing from `LAYOUT_BOUNDS` is one whose drag handle appears to work, moves the
edge, and forgets by the next page load. Nothing anywhere says so.
So the three are pinned here: the allowlist entry, the `data-resize-min` on the
handle, and the `--*-width-min` token the CSS clamps with.
"""
from __future__ import annotations
import re
from pathlib import Path
import lembas
from lembas.api.preferences import LAYOUT_BOUNDS
ROOT = Path(lembas.__file__).parent
TOKENS = (ROOT / "web/static/css/tokens.css").read_text(encoding="utf-8")
TEMPLATES = ROOT / "web/templates"
# The panels with a drag handle, and the template each handle lives in.
PANELS = {
"--terminal-width": "chat/_terminal.html",
"--canvas-width": "chat/_canvas.html",
}
# 1rem, everywhere in this application.
REM = 16
def _resize_min(template: str) -> int:
text = (TEMPLATES / template).read_text(encoding="utf-8")
found = re.search(r'data-resize-min="(\d+)"', text)
assert found, f"{template} has a resize handle with no minimum"
return int(found.group(1))
def _token_min(name: str) -> int:
found = re.search(rf"{re.escape(name)}-min:\s*([\d.]+)rem", TOKENS)
assert found, f"{name}-min is not declared in tokens.css"
return int(float(found.group(1)) * REM)
def test_every_dragged_panel_is_in_the_allowlist():
"""Without the entry the drag is silently discarded on the way to the
account, so the width survives in one browser and vanishes in the next."""
missing = [name for name in PANELS if name not in LAYOUT_BOUNDS]
assert not missing, f"not in LAYOUT_BOUNDS: {missing}"
def test_the_three_minimums_agree():
for name, template in PANELS.items():
assert LAYOUT_BOUNDS[name][0] == _resize_min(template) == _token_min(name), name
def test_no_bound_lets_a_panel_become_unreachable():
"""A width outside these is a panel somebody cannot see well enough to drag
back, which is the other half of what the allowlist is for."""
for name, (low, high) in LAYOUT_BOUNDS.items():
assert 0 < low < high, name
def test_the_canvas_starts_wider_than_the_terminal():
"""A source line is longer than eighty columns once nothing is re-wrapping
it, and this one holds prose as well."""
widths = {
name: float(re.search(rf"{re.escape(name)}:\s*([\d.]+)rem", TOKENS).group(1))
for name in PANELS
}
assert widths["--canvas-width"] > widths["--terminal-width"]