Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233"""Verify that the visible transcript rows hold while older pages load and replies arrive."""
from __future__ import annotations
import asyncio
from kit import Reviewfrom seed import Daemon
PAGE_BACK = r"""(async () => { const realFetch = window.fetch; const sizes = []; let release; const gate = new Promise(resolve => { release = resolve; }); window.fetch = async function(input, init) { const url = new URL(input instanceof Request ? input.url : input, location.href); const history = /^\/api\/sessions\/[^/]+\/history$/.test(url.pathname); if (history && url.searchParams.has('before')) await gate; const response = await realFetch.call(this, input, init); if (history) sizes.push([url.search, (await response.clone().json()).items?.length]); return response; }; const frame = () => new Promise(requestAnimationFrame); const wait = async (condition) => { const end = performance.now() + 5000; while (!condition()) { if (performance.now() > end) throw new Error('browser condition not reached'); await frame(); } }; [...document.querySelectorAll('aside .row .main')] .find(row => row.querySelector('.name').textContent.trim() === 'a huge chat') .click(); const view = () => document.querySelector('.scroller'); await wait(() => view()?.scrollTop > 500 && view().querySelector('.thread:not(.waiting)')); await frame(); await frame(); const read = () => { const box = view().getBoundingClientRect(); const rows = [...view().querySelectorAll('.rows > [data-key]')].map(element => { const row = element.getBoundingClientRect(); return {id: element.dataset.key, top: row.top-box.top, bottom:row.bottom-box.top}; }); return { unique: new Set(rows.map(row => row.id)).size === rows.length, visible: rows.filter(row => row.bottom > 0 && row.top < box.height), }; }; view().scrollTop = 160; view().dispatchEvent(new Event('scroll')); await frame(); await frame(); const before = read(); release(); await wait(() => sizes.some(([query])=>query.includes('before='))); await wait(() => !view().querySelector('.quiet')); for (let count = 0; count < 10; count++) await frame(); const after=read(); return {before,after,sizes};})()"""
async def check(ui: Review) -> None: """Run on the seeded huge chat at desktop size to expose large height corrections.""" await ui.size(1280, 800) await ui.fresh("canvas") result = await ui.js(PAGE_BACK) before = result["before"] after = result["after"] assert any("before=" in query and count > 0 for query, count in result["sizes"]), ( result ) assert before["unique"] and after["unique"], result before_ids = [row["id"] for row in before["visible"]] after_ids = [row["id"] for row in after["visible"]] assert before_ids == ["97"], result assert after_ids == before_ids, result assert abs(before["visible"][0]["top"] - after["visible"][0]["top"]) <= 1, result
SWITCH_CHATS = r"""(async () => { const frame = () => new Promise(requestAnimationFrame); const wait = async (condition) => { const deadline = performance.now() + 6500; while (!condition()) { if (performance.now() > deadline) throw new Error('browser condition not reached'); await frame(); } }; const view = () => [...document.querySelectorAll('.scroller')] .find(element => element.getBoundingClientRect().left === 0); const ready = () => view()?.querySelector('.thread:not(.waiting)'); const read = () => { const scroller = view(); const box = scroller.getBoundingClientRect(); const rows = [...scroller.querySelectorAll('.rows > [data-key]')]; const visible = rows.filter(element => { const row = element.getBoundingClientRect(); return row.bottom > box.top && row.top < box.bottom; }); return { ids: visible.map(element => element.dataset.key), top: visible[0]?.getBoundingClientRect().top - box.top, unique: new Set(rows.map(element => element.dataset.key)).size === rows.length, }; }; const open = async (name) => { const button = () => [...document.querySelectorAll('button.tile,button.row')] .find(element => element.innerText.includes(name)); await wait(button); button().click(); await wait(ready); }; const back = async () => { document.querySelector('button[aria-label="back to sessions"]').click(); await wait(() => !view()); };
await open('a huge chat'); await wait(() => view().scrollTop > 500); const scroller = view(); scroller.scrollTop = 160; scroller.dispatchEvent(new Event('scroll')); await wait(() => !scroller.querySelector('.quiet')); for (let count = 0; count < 12; count++) await frame(); const initial = read();
await back(); await open('hello chat'); await wait(() => read().ids.length > 0); const other = read(); await back(); await open('a huge chat'); for (let count = 0; count < 12; count++) await frame(); return { initial, other, restored: read() };})()"""
async def check_phone_switch(ui: Review) -> None: """Switch away from a paged-in reply and return to the same visible rows.""" await ui.size(390, 844) await ui.fresh(None) result = await ui.js(SWITCH_CHATS) initial = result["initial"] other = result["other"] restored = result["restored"] assert initial["unique"] and other["unique"] and restored["unique"], result assert initial["ids"][0] == "97", result assert other["ids"][:2] == ["1", "2"], result assert restored["ids"] == initial["ids"], result assert abs(restored["top"] - initial["top"]) <= 1, result
SCROLLER = ( "[...document.querySelectorAll('.scroller')]" ".sort((a, b) => b.clientHeight - a.clientHeight)[0]")
# Whenever the chat moves its scroll, consecutive rows must sit one gap apart.TORN = ( f"() => {{ const view = {SCROLLER};" + r""" const gap = parseFloat(getComputedStyle(view.querySelector('.rows')).rowGap) || 0; window.__torn = []; const own = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollTop'); Object.defineProperty(Element.prototype, 'scrollTop', { configurable: true, get() { return own.get.call(this); }, set(value) { if (this === view) { const rows = [...view.querySelectorAll('.rows > [data-index]')]; for (let i = 1; i < rows.length; i++) { const [a, b] = [rows[i - 1], rows[i]]; if (+b.dataset.index !== +a.dataset.index + 1) continue; const room = b.offsetTop - a.offsetTop - a.offsetHeight - gap; if (Math.abs(room) > 100) { window.__torn.push([+b.dataset.index, Math.round(room)]); break; } } } own.set.call(this, value); }, });}""")
async def check_history_arriving(ui: Review) -> None: """Scroll up a long chat while replies keep arriving. A reply makes Svelte add and drop rows a flush before it updates the rows it keeps, and the chat once read its place from that layout and sent the reader to the end.""" await ui.size(1500, 830) await ui.fresh("canvas") await ui.js( """() => [...document.querySelectorAll('aside .row')] .find((row) => row.textContent.includes('a brisk chat')) .querySelector('button[aria-label="open full screen"]').click()""" ) await asyncio.sleep(2.5) session = (await ui.windows())[0]["id"].removeprefix("session:") for _ in range(8): await ui.js(f"() => {{ {SCROLLER}.scrollTop = 0; }}") await asyncio.sleep(1) await ui.js(TORN) daemon = Daemon(ui.home, ui.env) daemon.connect() results = [] for _ in range(10): await ui.js( f"() => {{ const view = {SCROLLER}; view.scrollTop = view.scrollHeight; }}" ) await asyncio.sleep(1.2) await ui.js("() => { window.__torn = []; }") done = asyncio.Event() replying = asyncio.create_task(reply(daemon, session, done)) for _ in range(30): await ui.wheel(750, 415, 0, -120, wait=0.08) done.set() await replying await asyncio.sleep(0.8) results.append(await ui.js("() => window.__torn")) assert not any(results), results
async def reply(daemon: Daemon, session: str, done: asyncio.Event) -> None: """Sends short messages until done, each once the last one's turn has ended.""" while not done.is_set(): await asyncio.to_thread(daemon.say, session, "brief again")