"""Session setup, ingress failures, workspace repair, and image and file inspection.""" import base64 import hashlib import re import json from pathlib import Path import sqlite3 import time import urllib.error import urllib.parse from harness import ( LOOPBACK_REMOTE, cell_result, exclusive, operation_id, python, text as reply, ) from image_limits_test import png from integration_support import IntegrationScenario, contents class IntegrationTest(IntegrationScenario): # exclusive: changes scheduler timing and installs a global database trigger @exclusive def test_scheduler_reports_a_failed_occurrence_write_and_retries(self): def prepare(app): app.env["ALBEDO_SCHEDULE_TICK_MS"] = "50" app = self.app_for("chat_completions", prepare=prepare) session = app.session() with sqlite3.connect(app.home / "albedo.sqlite") as db: db.execute( "CREATE TRIGGER reject_advance BEFORE DELETE ON schedules " "BEGIN SELECT RAISE(FAIL, 'fixture advance failed'); END" ) db.execute( "INSERT INTO schedules(session,kind,prompt,next_at) VALUES(?,'once',?,0)", (session, "scheduled fixture"), ) deadline = time.monotonic() + 15 while "fixture advance failed" not in (app.home / "daemon.log").read_text(): self.assertLess(time.monotonic(), deadline) time.sleep(0.05) self.assertIn( "schedule occurrence not queued", (app.home / "daemon.log").read_text(), ) with sqlite3.connect(app.home / "albedo.sqlite") as db: self.assertEqual( db.execute("SELECT COUNT(*) FROM schedules").fetchone()[0], 1 ) # The prompt is stored with its advance, so a failed advance # queued nothing. self.assertEqual( db.execute( "SELECT COUNT(*) FROM mail WHERE recipient=?", (session,) ).fetchone()[0], 0, ) db.execute("DROP TRIGGER reject_advance") while True: with sqlite3.connect(app.home / "albedo.sqlite") as db: if db.execute("SELECT COUNT(*) FROM schedules").fetchone()[0] == 0: break self.assertLess(time.monotonic(), deadline) time.sleep(0.05) app.idle(session) self.assertTrue(self.provider.requests) # exclusive: renames the global session_family table @exclusive def test_deletion_refuses_a_failed_child_lookup(self): app = self.app_for("chat_completions") session = app.session() with app.api(f"/sessions/{session}?view=configuration") as response: response.read() etag = response.headers["ETag"] family = app.family_revision(session) with sqlite3.connect(app.home / "albedo.sqlite") as db: db.execute("ALTER TABLE session_family RENAME TO unavailable_family") for scope in ("leaf", "subtree"): with self.subTest(scope=scope): with self.assertRaises(urllib.error.HTTPError) as caught: app.api( f"/sessions/{session}?view=configuration&scope={scope}" f"&family_revision={family}", method="DELETE", headers={"If-Match": etag}, ).close() self.assertGreaterEqual(caught.exception.code, 400) with sqlite3.connect(app.home / "albedo.sqlite") as db: self.assertIsNotNone( db.execute( "SELECT id FROM sessions WHERE id=?", (session,) ).fetchone() ) with sqlite3.connect(app.home / "albedo.sqlite") as db: db.execute("ALTER TABLE unavailable_family RENAME TO session_family") with app.api( f"/sessions/{session}?view=configuration&scope=subtree" f"&family_revision={family}", method="DELETE", headers={"If-Match": etag}, ) as response: self.assertEqual(json.load(response)["deleted_ids"], [session]) # exclusive: removes a legacy schema column and restarts the daemon @exclusive def test_uploaded_images_survive_upgrade_from_unclassified_transcripts(self): original = png(2, 3) probe = {"code": ""} observed = [] def script(request): if request["input"][-1].get("type") == "function_call_output": observed.append(cell_result(request["input"][-1]["output"])) return reply("image access checked") return ( python(probe["code"]) if probe["code"] else reply("stored before upgrade") ) self.provider.script = script app = self.app_for("responses") session = app.session() app.api( f"/sessions/{session}/inputs/{operation_id()}", { "kind": "message", "text": "keep this upload", "images": [ { "mime_type": "image/png", "data": base64.b64encode(original).decode(), } ], }, method="PUT", ).close() app.idle(session) def legacy_layout(_app): with sqlite3.connect(app.home / "albedo.sqlite") as db: for name in ( "transcript_uploads", "transcript_users", "transcript_fits", "transcript_pending_class", ): db.execute(f"DROP INDEX IF EXISTS {name}") db.execute("ALTER TABLE transcript DROP COLUMN row_class") app.restart(prepare=legacy_layout) probe["code"] = ( f"assert await files.image() == {original!r}\nprint('original upload survives schema upgrade')" ) def check_image(): app.prompt(session, "inspect the original upload").close() app.idle(session) self.assertEqual(observed[-1]["status"], "ok", observed[-1]) self.assertIn( "original upload survives schema upgrade", observed[-1]["output"] ) check_image() app.restart() check_image() def test_uploaded_image_bytes_are_available_to_python_without_daemon_paths(self): originals = [png(2, 3), png(1200, 10)] probe = {"code": ""} observed = [] def script(request): if request["input"][-1].get("type") == "function_call_output": observed.append(cell_result(request["input"][-1]["output"])) return reply("image access checked") return python(probe["code"]) if probe["code"] else reply("noted") self.provider.script = script profile = { "baseUrl": self.provider.url, "apiKey": "fixture-key", "model": "fixture-model", "protocol": "responses", } loose, strict = f"loose-{self.provider.route}", f"strict-{self.provider.route}" app = self.app_for( "responses", providers={loose: profile, strict: dict(profile, imageEdge=1000)}, ) session = app.session() probe["code"] = ( "import struct\n" f"assert await files.image() == {originals[0]!r}\n" f"assert await files.image(index=1) == {originals[1]!r}\n" "assert struct.unpack('>II', (await files.image(index=1))[16:24]) == (1200, 10)\n" "print('original image bytes verified')" ) app.api( f"/sessions/{session}/inputs/{operation_id()}", { "kind": "message", "text": "crop the uploaded image", "images": [ {"mime_type": "image/png", "data": base64.b64encode(data).decode()} for data in originals ], }, method="PUT", ).close() app.idle(session) history = self.history(app, session) self.assertEqual(observed[-1]["status"], "ok", observed[-1]) self.assertIn("original image bytes verified", observed[-1]["output"]) entry = next(row["position"] for row in history if row["kind"] == "user") # A text-only message can mention the storage tag without hiding the # actual upload. Explicit entry lookup remains scoped to this session. other = app.session() probe["code"] = "print('another session has its own image')" app.api( f"/sessions/{other}/inputs/{operation_id()}", { "kind": "message", "text": "another session", "images": [ { "mime_type": "image/png", "data": base64.b64encode(png(6, 7)).decode(), } ], }, method="PUT", ).close() app.idle(other) foreign = next( row["position"] for row in self.history(app, other) if row["kind"] == "user" ) # Cross a history page, then switch to a provider that fits the earlier # image. Python must still receive its original 1200px bytes. probe["code"] = "" for turn in range(63): app.prompt(session, f"user_image is just prose; follow-up {turn}").close() app.idle(session) self.change( app, session, {"provider_profile": strict, "model": "fixture-model"} ) probe["code"] = ( f"assert await files.image(entry={entry}, index=1) == {originals[1]!r}\n" f"assert await files.image() == {originals[0]!r}\n" f"for entry_id, index in [({foreign}, 0), ({entry}, 2)]:\n" " try:\n" " await files.image(entry=entry_id, index=index)\n" " except FilesError as error:\n" " assert error.code == 'image_unavailable', str(error)\n" " else:\n" " raise AssertionError('image outside this selection was returned')\n" "try:\n" " await files.image(index=-1)\n" "except ValueError:\n" " pass\n" "else:\n" " raise AssertionError('invalid index was accepted')\n" "print('image history and session scope verified')" ) app.prompt( session, "crop again; user_image and stored_data are just prose" ).close() app.idle(session) self.assertEqual(observed[-1]["status"], "ok", observed[-1]) self.assertIn( "image history and session scope verified", observed[-1]["output"] ) self.assertTrue( any(row["kind"] == "image_fit" for row in self.history(app, session)) ) # Pause after a real first-page reply, then cancel the Python task. # No second page may be requested after cancellation. probe["code"] = ( "import asyncio\n" "from albedo_plugins import files as files_module\n" "original_host = files_module.host\n" "page_ready = asyncio.Event()\n" "pages = []\n" "async def paused_host(method, args):\n" " result = await original_host(method, args)\n" " pages.append(result)\n" " assert 'before' in result, result\n" " page_ready.set()\n" " await asyncio.Future()\n" " return result\n" "files_module.host = paused_host\n" "pending_image = asyncio.create_task(files.image())\n" "try:\n" " await asyncio.wait_for(page_ready.wait(), 5)\n" " pending_image.cancel()\n" " try:\n" " await pending_image\n" " except asyncio.CancelledError:\n" " pass\n" " else:\n" " raise AssertionError('image lookup ignored cancellation')\n" " assert len(pages) == 1, pages\n" "finally:\n" " files_module.host = original_host\n" " pending_image.cancel()\n" "print('image lookup cancellation verified')" ) app.prompt(session, "cancel the user_image lookup").close() app.idle(session) self.assertEqual(observed[-1]["status"], "ok", observed[-1]) self.assertIn("image lookup cancellation verified", observed[-1]["output"]) probe["code"] = ( LOOPBACK_REMOTE + "try:\n" + f" assert await rem.files.image(index=1) == {originals[1]!r}\n" + " print('remote original image bytes verified')\n" + "finally:\n await rem.close()" ) app.prompt(session, "inspect the original image from a remote kernel").close() app.idle(session) self.assertEqual(observed[-1]["status"], "ok", observed[-1]) self.assertIn("remote original image bytes verified", observed[-1]["output"]) def test_attached_files_are_readable_by_the_model_and_retries_keep_their_bytes( self, ): payloads = { "résumé