A directory the model knows about, and @ to name a file in it
An agent chat used to open with the model knowing the name of a machine and nothing about what was on it, so the first two rounds of every reply went on finding out. It now gets a listing: one read-only command, `git ls-files` where that works and `find` otherwise, falling back to an SFTP walk that always does. git first because a repository already carries somebody's considered list of what is not part of the project, and reproducing it by hand is how an index ends up mostly build output. The listing is budgeted rather than dumped. A tree of a thousand files is worse than no tree -- it costs the window on every request forever and buries the four names that mattered -- so directories that will not fit are shown as a count and the model is told to open one itself. Collapsing picks the deepest and largest first: by saving alone it would take `src/` before `src/web/static/vendor/`, because it contains it, and lose every name worth having. Read from a cache and never fetched. `harness.context_variables` is synchronous and sits on the request path; the walk happens in the generation setup, which is async and already doing network work, with a short wait. A chat whose first reply outruns its first walk simply has no listing that turn and the fragment disappears rather than appearing as an empty heading. Then `@`, over the same index and over the library, and `/` for commands with an Alt-based keyboard for the same jobs. A mentioned file arrives as contents, not a reference -- a small model asked to call file_read often does not bother -- and it arrives with its absolute path and the machine it came from, because a model handed `main.py` cannot tell which of four it is and cannot name it back when asked to change something. The rule that matters for `/`: a message that merely starts with a slash still sends. `//` escapes and an unrecognised command is posted as written. Swallowing somebody's message is a much worse failure than an unknown command. Two exceptions to Manual mode now, not one. Browsing and indexing are a person acting, not a model, so neither passes through policy.py -- the same argument the terminal panel rests on. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,331 @@
|
||||
"""`@` attachments: what the picker offers, and what the model is told it got.
|
||||
|
||||
The point of the second half is the one the feature was asked for: a model
|
||||
handed a file called `main.py` cannot tell which of four it is looking at, and
|
||||
cannot name it back when asked to change something. So the path and the machine
|
||||
travel with the contents.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import select
|
||||
|
||||
from lembas.db.models import Attachment, Message, SshProfile, User
|
||||
from lembas.services import chat as chat_service
|
||||
from lembas.services import settings_store
|
||||
from lembas.services.agent import index as index_service
|
||||
from lembas.services.agent import ssh as ssh_service
|
||||
|
||||
asyncssh = pytest.importorskip("asyncssh")
|
||||
|
||||
|
||||
class _Server(asyncssh.SSHServer):
|
||||
def begin_auth(self, username: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def box(tmp_path):
|
||||
"""A real SFTP server on its own loop, with a small tree to mention from."""
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
root = tmp_path / "work"
|
||||
(root / "src").mkdir(parents=True)
|
||||
(root / "src" / "main.py").write_text("print('hello')\n")
|
||||
(root / "README.md").write_text("# Project\n")
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
thread = threading.Thread(target=loop.run_forever, daemon=True)
|
||||
thread.start()
|
||||
|
||||
async def start():
|
||||
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)
|
||||
return server, port, line, fingerprint
|
||||
|
||||
server, port, line, fingerprint = asyncio.run_coroutine_threadsafe(start(), loop).result(10)
|
||||
try:
|
||||
yield {"port": port, "host_key": line, "fingerprint": fingerprint, "root": str(root)}
|
||||
finally:
|
||||
|
||||
async def stop():
|
||||
server.close()
|
||||
await server.wait_closed()
|
||||
|
||||
asyncio.run_coroutine_threadsafe(stop(), loop).result(10)
|
||||
loop.call_soon_threadsafe(loop.stop)
|
||||
thread.join(timeout=5)
|
||||
|
||||
|
||||
def _profile(db, box) -> SshProfile:
|
||||
settings_store.update(db, {"enabled": True}, key=settings_store.AGENTS)
|
||||
user = db.scalars(select(User)).first()
|
||||
profile = SshProfile(
|
||||
owner_id=user.id,
|
||||
name="Container",
|
||||
host="127.0.0.1",
|
||||
port=box["port"],
|
||||
username="tester",
|
||||
host_key=box["host_key"],
|
||||
host_fingerprint=box["fingerprint"],
|
||||
default_dir=box["root"],
|
||||
)
|
||||
db.add(profile)
|
||||
db.commit()
|
||||
return profile
|
||||
|
||||
|
||||
def _index(profile, box, paths=("README.md", "src/main.py")):
|
||||
index_service._CACHE[(profile.id, box["root"])] = index_service.ProjectIndex(
|
||||
paths=tuple(paths), total=len(paths), source="git", built_at=time.monotonic()
|
||||
)
|
||||
|
||||
|
||||
# --- The picker --------------------------------------------------------------
|
||||
def test_the_picker_offers_project_files(client: TestClient, db, registered, box):
|
||||
profile = _profile(db, box)
|
||||
_index(profile, box)
|
||||
|
||||
body = client.get(
|
||||
"/api/files/mention-picker",
|
||||
params={"q": "main", "profile_id": profile.id, "project_dir": box["root"]},
|
||||
).text
|
||||
|
||||
assert "src/main.py" in body
|
||||
assert "README.md" not in body # filtered by the query
|
||||
|
||||
|
||||
def test_the_picker_never_waits_on_a_machine(client: TestClient, db, registered, box):
|
||||
"""No listing built yet means no files offered, not a connection opened.
|
||||
|
||||
This is a keystroke-latency path. Building the index here would put an SSH
|
||||
round trip between a letter and the menu.
|
||||
"""
|
||||
profile = _profile(db, box)
|
||||
|
||||
body = client.get(
|
||||
"/api/files/mention-picker",
|
||||
params={"profile_id": profile.id, "project_dir": box["root"]},
|
||||
).text
|
||||
|
||||
assert "main.py" not in body
|
||||
|
||||
|
||||
def test_the_picker_refuses_somebody_elses_connection(client: TestClient, db, registered, box):
|
||||
"""An id in a query string is not an authorisation, and this lists the
|
||||
contents of somebody's machine."""
|
||||
profile = _profile(db, box)
|
||||
_index(profile, box)
|
||||
|
||||
client.post("/auth/logout", follow_redirects=False)
|
||||
client.post(
|
||||
"/auth/register",
|
||||
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
|
||||
follow_redirects=False,
|
||||
)
|
||||
|
||||
body = client.get(
|
||||
"/api/files/mention-picker",
|
||||
params={"profile_id": profile.id, "project_dir": box["root"]},
|
||||
).text
|
||||
|
||||
assert "main.py" not in body
|
||||
|
||||
|
||||
def test_a_plain_chat_gets_a_picker_with_no_file_half(client: TestClient, db, registered):
|
||||
"""`@` works everywhere; only the project half needs a connection."""
|
||||
response = client.get("/api/files/mention-picker")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "In the project" not in response.text
|
||||
|
||||
|
||||
# --- Attaching ---------------------------------------------------------------
|
||||
def test_a_mentioned_file_arrives_with_its_contents(client: TestClient, db, registered, box):
|
||||
profile = _profile(db, box)
|
||||
|
||||
client.post(
|
||||
"/api/files/from-project",
|
||||
data={"profile_id": profile.id, "path": "src/main.py"},
|
||||
)
|
||||
|
||||
attachment = db.scalars(select(Attachment)).one()
|
||||
assert "print('hello')" in attachment.extracted_text
|
||||
assert attachment.filename == "main.py"
|
||||
|
||||
|
||||
def test_the_model_is_told_which_file_and_where(client: TestClient, db, registered, box):
|
||||
"""The whole reason the columns exist. `main.py` alone is not an answer to
|
||||
"which one", and a model cannot name a file back that it was never given
|
||||
the path of."""
|
||||
profile = _profile(db, box)
|
||||
client.post(
|
||||
"/api/files/from-project",
|
||||
data={"profile_id": profile.id, "path": "src/main.py"},
|
||||
)
|
||||
|
||||
attachment = db.scalars(select(Attachment)).one()
|
||||
message = Message(chat_id=None, role="user", content="")
|
||||
message.attachments = [attachment]
|
||||
|
||||
block = chat_service.document_context(message)
|
||||
|
||||
assert 'path="src/main.py"' in block
|
||||
assert 'from="Container"' in block
|
||||
assert 'name="main.py"' in block
|
||||
|
||||
|
||||
def test_a_quote_in_a_path_cannot_break_out_of_the_tag(client: TestClient, db, registered):
|
||||
"""These are attribute values in a tag we write. A path containing a quote
|
||||
would otherwise close it early and the rest would read as instructions."""
|
||||
from lembas.services import files as files_service
|
||||
|
||||
user = db.scalars(select(User)).first()
|
||||
attachment = files_service.store_text(
|
||||
db,
|
||||
user_id=user.id,
|
||||
chat_id=None,
|
||||
filename="x.txt",
|
||||
text="body",
|
||||
source_path='/tmp/a"><script>.txt',
|
||||
source_label='Box" evil',
|
||||
)
|
||||
message = Message(chat_id=None, role="user", content="")
|
||||
message.attachments = [attachment]
|
||||
|
||||
block = chat_service.document_context(message)
|
||||
|
||||
assert "<script>" not in block
|
||||
assert block.count("<document") == 1
|
||||
|
||||
|
||||
def test_a_mentioned_directory_attaches_its_listing(client: TestClient, db, registered, box):
|
||||
""""@ that folder" is a reasonable thing to mean, and the listing is what
|
||||
it means -- better than refusing."""
|
||||
profile = _profile(db, box)
|
||||
|
||||
client.post("/api/files/from-project", data={"profile_id": profile.id, "path": "src/"})
|
||||
|
||||
attachment = db.scalars(select(Attachment)).one()
|
||||
assert "main.py" in attachment.extracted_text
|
||||
|
||||
|
||||
def test_somebody_elses_connection_cannot_be_read_from(client: TestClient, db, registered, box):
|
||||
profile = _profile(db, box)
|
||||
|
||||
client.post("/auth/logout", follow_redirects=False)
|
||||
client.post(
|
||||
"/auth/register",
|
||||
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
|
||||
follow_redirects=False,
|
||||
)
|
||||
settings_store.update(db, {"default_permissions": {"tools.agent": True}})
|
||||
|
||||
body = client.post(
|
||||
"/api/files/from-project",
|
||||
data={"profile_id": profile.id, "path": "src/main.py"},
|
||||
).text
|
||||
|
||||
assert "not available" in body
|
||||
assert db.scalars(select(Attachment)).all() == []
|
||||
|
||||
|
||||
def test_a_missing_file_is_an_error_chip_not_a_crash(client: TestClient, db, registered, box):
|
||||
profile = _profile(db, box)
|
||||
|
||||
response = client.post(
|
||||
"/api/files/from-project",
|
||||
data={"profile_id": profile.id, "path": "nowhere.txt"},
|
||||
)
|
||||
|
||||
# 200 with a readable chip, like every other attach failure: htmx swaps the
|
||||
# body either way, and an error somebody can read beats a console message.
|
||||
assert response.status_code == 200
|
||||
assert "no file" in response.text.lower()
|
||||
|
||||
|
||||
def test_an_ordinary_upload_carries_no_provenance(client: TestClient, db, registered):
|
||||
"""A file dragged in from a laptop has no address this instance could
|
||||
honestly report, so the tag stays as it was."""
|
||||
from lembas.services import files as files_service
|
||||
|
||||
user = db.scalars(select(User)).first()
|
||||
attachment = files_service.store(
|
||||
db, user_id=user.id, chat_id=None, payload=b"hello", filename="notes.txt"
|
||||
)
|
||||
message = Message(chat_id=None, role="user", content="")
|
||||
message.attachments = [attachment]
|
||||
|
||||
block = chat_service.document_context(message)
|
||||
|
||||
assert "path=" not in block
|
||||
assert "from=" not in block
|
||||
|
||||
|
||||
# --- /usage ------------------------------------------------------------------
|
||||
def test_usage_sums_what_every_reply_recorded(client: TestClient, db, registered, make_chat):
|
||||
"""Summed from what was stored rather than recomputed. An endpoint that
|
||||
reported nothing contributed an estimate at the time, and re-deriving it
|
||||
now would make the totals move under a chat nobody had touched."""
|
||||
from lembas.db.models import ROLE_ASSISTANT, Chat
|
||||
|
||||
chat_id = make_chat()
|
||||
chat = db.get(Chat, chat_id)
|
||||
for prompt, completion in ((100, 20), (150, 30)):
|
||||
db.add(
|
||||
Message(
|
||||
chat_id=chat.id,
|
||||
role=ROLE_ASSISTANT,
|
||||
content="hi",
|
||||
usage_json={
|
||||
"prompt_tokens": prompt,
|
||||
"completion_tokens": completion,
|
||||
"total_tokens": prompt + completion,
|
||||
},
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
body = client.get(f"/api/chats/{chat_id}/usage").text
|
||||
|
||||
assert "300" in body # 250 sent + 50 written
|
||||
assert "2 replies" in body
|
||||
|
||||
|
||||
def test_usage_is_owner_only(client: TestClient, db, registered, make_chat):
|
||||
"""No admin branch, unlike the inspector beside it: these numbers describe
|
||||
somebody's conversation, and reading one is not a configuration act."""
|
||||
chat_id = make_chat()
|
||||
|
||||
client.post("/auth/logout", follow_redirects=False)
|
||||
client.post(
|
||||
"/auth/register",
|
||||
data={"name": "Sam", "email": "sam@shire.test", "password": "potatoes-po-ta-toes"},
|
||||
follow_redirects=False,
|
||||
)
|
||||
|
||||
assert client.get(f"/api/chats/{chat_id}/usage").status_code == 404
|
||||
|
||||
|
||||
def test_an_unknown_window_size_is_not_reported_as_zero(
|
||||
client: TestClient, db, registered, make_chat
|
||||
):
|
||||
"""Unknown is not zero. Nobody has said how big this model's window is, so
|
||||
there is no percentage and compaction will never fire."""
|
||||
chat_id = make_chat()
|
||||
|
||||
body = client.get(f"/api/chats/{chat_id}/usage").text
|
||||
|
||||
assert "never compact itself" in body
|
||||
Reference in New Issue
Block a user