"""`@` 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">