fd4db76c64
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>
177 lines
6.5 KiB
Python
177 lines
6.5 KiB
Python
"""Reading and writing a file for somebody who is about to edit it.
|
|
|
|
The model-facing `read_file`/`write_file` pair is deliberately untouched: what
|
|
they return is a contract a model has been shown, and it is the right contract
|
|
for a model. It is the wrong one here, and these are the cases that say why.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from lembas.services.agent import ssh as ssh_service
|
|
from lembas.services.agent.base import Conflict, ExecError
|
|
|
|
asyncssh = pytest.importorskip("asyncssh")
|
|
|
|
|
|
class _Server(asyncssh.SSHServer):
|
|
def begin_auth(self, username: str) -> bool:
|
|
return False
|
|
|
|
|
|
@pytest.fixture
|
|
async def machine(tmp_path):
|
|
project = tmp_path / "project"
|
|
project.mkdir()
|
|
server = await asyncssh.create_server(
|
|
_Server,
|
|
"127.0.0.1",
|
|
0,
|
|
server_host_keys=[asyncssh.generate_private_key("ssh-ed25519")],
|
|
sftp_factory=True,
|
|
)
|
|
port = next(iter(server.sockets)).getsockname()[1]
|
|
line, fingerprint = await ssh_service.capture_host_key("127.0.0.1", port)
|
|
try:
|
|
yield {"port": port, "host_key": line, "dir": str(project), "path": project}
|
|
finally:
|
|
server.close()
|
|
await server.wait_closed()
|
|
|
|
|
|
def _executor(machine) -> ssh_service.SshExecutor:
|
|
return ssh_service.SshExecutor(
|
|
{
|
|
"host": "127.0.0.1",
|
|
"port": machine["port"],
|
|
"username": "tester",
|
|
"auth": "password",
|
|
"credential": "",
|
|
"host_key": machine["host_key"],
|
|
},
|
|
machine["dir"],
|
|
)
|
|
|
|
|
|
# --- Fidelity ------------------------------------------------------------------
|
|
async def test_an_escape_sequence_survives_a_round_trip(machine):
|
|
"""The whole reason this is not `read_file`. That one ends in
|
|
`clean_output`, which strips ANSI escapes -- right for the output of a
|
|
command, and here it means opening a file and pressing Save rewrites it
|
|
with the escapes gone."""
|
|
original = "red \x1b[31mtext\x1b[0m here\n"
|
|
(machine["path"] / "colours.txt").write_text(original)
|
|
executor = _executor(machine)
|
|
|
|
opened = await executor.read_text("colours.txt")
|
|
assert opened.text == original
|
|
|
|
await executor.write_text("colours.txt", opened.text, if_unchanged=opened.revision)
|
|
assert (machine["path"] / "colours.txt").read_text() == original
|
|
|
|
|
|
async def test_the_model_facing_read_still_strips_them(machine):
|
|
"""Pinned as a pair: the contract a model was shown has not moved."""
|
|
(machine["path"] / "colours.txt").write_text("red \x1b[31mtext\x1b[0m here\n")
|
|
text = await _executor(machine).read_file("colours.txt")
|
|
assert "\x1b[31m" not in text
|
|
|
|
|
|
async def test_undecodable_bytes_are_reported_rather_than_replaced(machine):
|
|
"""errors="replace" would hand back U+FFFD for every one of them, and
|
|
saving that back is how a file is quietly destroyed."""
|
|
(machine["path"] / "blob.bin").write_bytes(b"\xff\xfe\x00\x01binary")
|
|
opened = await _executor(machine).read_text("blob.bin")
|
|
assert opened.binary is True
|
|
assert opened.text == ""
|
|
|
|
|
|
async def test_a_nul_byte_early_on_reads_as_binary(machine):
|
|
(machine["path"] / "blob.bin").write_bytes(b"text\x00more text")
|
|
assert (await _executor(machine).read_text("blob.bin")).binary is True
|
|
|
|
|
|
async def test_utf8_beyond_ascii_is_not_binary(machine):
|
|
(machine["path"] / "note.txt").write_text("a mallorn tree — Lothlórien\n")
|
|
opened = await _executor(machine).read_text("note.txt")
|
|
assert opened.binary is False
|
|
assert "Lothlórien" in opened.text
|
|
|
|
|
|
# --- Size ------------------------------------------------------------------------
|
|
async def test_a_large_file_opens_truncated(machine):
|
|
(machine["path"] / "big.log").write_text("x" * (ssh_service.MAX_READ_BYTES + 500))
|
|
opened = await _executor(machine).read_text("big.log")
|
|
assert opened.truncated is True
|
|
assert len(opened.text) == ssh_service.MAX_READ_BYTES
|
|
|
|
|
|
async def test_an_oversize_write_is_refused_not_truncated(machine):
|
|
"""`write_file` truncates because a model is told how many bytes it wrote.
|
|
Somebody pressing Save would lose the tail with nothing said."""
|
|
executor = _executor(machine)
|
|
(machine["path"] / "big.txt").write_text("small")
|
|
|
|
with pytest.raises(ExecError, match="Nothing was written"):
|
|
await executor.write_text("big.txt", "y" * (ssh_service.MAX_WRITE_BYTES + 1))
|
|
|
|
assert (machine["path"] / "big.txt").read_text() == "small"
|
|
|
|
|
|
# --- Conflict ---------------------------------------------------------------------
|
|
async def test_a_file_that_moved_underneath_refuses_the_save(machine):
|
|
import os
|
|
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
executor = _executor(machine)
|
|
opened = await executor.read_text("note.txt")
|
|
|
|
# Somebody else's editor, a build, a checkout. The size differs, so this
|
|
# does not depend on the filesystem's mtime resolution.
|
|
target.write_text("something else entirely\n")
|
|
os.utime(target, (0, 0))
|
|
|
|
with pytest.raises(Conflict):
|
|
await executor.write_text("note.txt", "beta\n", if_unchanged=opened.revision)
|
|
|
|
assert target.read_text() == "something else entirely\n"
|
|
|
|
|
|
async def test_a_save_with_no_token_overwrites(machine):
|
|
"""Which is what Overwrite on the conflict card does."""
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
await _executor(machine).write_text("note.txt", "beta\n")
|
|
assert target.read_text() == "beta\n"
|
|
|
|
|
|
async def test_a_new_file_can_be_created(machine):
|
|
"""Open a path that is not there, type, Save. The stat finds nothing and
|
|
there is nothing for the token to disagree with."""
|
|
executor = _executor(machine)
|
|
await executor.write_text("fresh.txt", "hello\n", if_unchanged="0:0")
|
|
assert (machine["path"] / "fresh.txt").read_text() == "hello\n"
|
|
|
|
|
|
async def test_the_revision_moves_after_a_write(machine):
|
|
"""Or the second save from the same tab would always conflict."""
|
|
target = machine["path"] / "note.txt"
|
|
target.write_text("alpha\n")
|
|
executor = _executor(machine)
|
|
|
|
opened = await executor.read_text("note.txt")
|
|
written = await executor.write_text(
|
|
"note.txt", "much longer contents\n", if_unchanged=opened.revision
|
|
)
|
|
assert written.revision != opened.revision
|
|
|
|
await executor.write_text("note.txt", "again\n", if_unchanged=written.revision)
|
|
assert target.read_text() == "again\n"
|
|
|
|
|
|
async def test_reading_something_that_is_not_there_says_so(machine):
|
|
with pytest.raises(ExecError, match="no file"):
|
|
await _executor(machine).read_text("nowhere.txt")
|