Public: anyone can read this; the crew can change it.

Talk on a phone, still wrong after #16552 landed: conversation history loads… #16561

Closed opened this in answer to
Closed without merging

mason/forge:mason/16560-talk-on-a-phone-still-wrong-after-16552 → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +167 −16
scripts/phone-persona/brief.md +1 −1
@@ -12,4 +12,4 @@ One more experiment beside the forge-to-forge one (#16407): a person who has onl
1212
1313 **What counts as stuck.** A control under 44 px, a button without a name, a sideways scroll, text under the keyboard, a line that never goes ("answering" after the answer), a box that moves while the thread grows, a list that opens at its far end, a top bar that wraps, or a step it cannot finish without leaving the phone.
1414
15 **What it hands back.** Every stuck place becomes one issue: the page, the width, what it did with its thumb, what it saw, what it expected. Each issue is fixed with a test at its route; `test/talk-phone.test.ts` is where Talk's own land, measured at 360 px with touch emulation (`evaluateLoaded(..., touch = true)` in `test/helpers/chrome.ts`).
15 **What it hands back.** Every stuck place becomes one issue: the page, the width, what it did with its thumb, what it saw, what it expected. Each issue is fixed with a test at its route; `test/talk-phone.looks.test.ts` is where Talk's own land, measured at 360 px with touch emulation (`evaluateLoaded(..., touch = true)` in `test/helpers/chrome.ts`).
src/web/pages/talk.ts +1 −1
@@ -645,7 +645,7 @@ async function talkPage(ctx: Ctx, conversation: Conversation, before0: string |
645645 <div class="talk" data-talk data-density="${display.density}" data-motion="${display.motion}" data-enter="${display.enter}" data-fold="${display.steps}" data-user="${user.id}" data-c="${conversation.id}" data-here="${here}" data-paged="${paged ? "1" : "0"}" data-running="${running && !paged ? "1" : "0"}">
646646 ${hereBar(ctx, conversation, context, display, split ? html`<nav class="seg talk-tabs" aria-label="Chat or preview"><a href="?pane=chat${paneQ === "" ? "" : `&${paneQ}`}"${shown === "chat" ? raw(' aria-current="page"') : null}>Chat</a><a href="?pane=preview${paneQ === "" ? "" : `&${paneQ}`}"${shown !== "chat" ? raw(' aria-current="page"') : null}>Preview</a></nav>` : null, picker)}
647647 ${turns.length === DEFAULT_PAGE && turns[0] !== undefined
648 ? html`<p class="talk-more small"><a href="${here}?before=${turns[0].id}">Earlier</a></p>`
648 ? html`<p class="talk-more small"><a href="${here}?before=${turns[0].id}" data-earlier>Earlier</a></p>`
649649 : null}
650650 ${!paged ? null : html`<p class="talk-more small"><a href="${here}">Latest</a></p>`}
651651 <ol class="talk-log" role="log" data-last="${last}" aria-label="Conversation with the forge">
src/web/pages/talk_client.ts +24
@@ -595,6 +595,30 @@ if (root) {
595595 ? (done === 1 ? "1 thing done" : done + " things done") + (steps ? ", " + (steps === 1 ? "1 step" : steps + " steps") : "")
596596 : steps === 1 ? items[0].querySelector("summary").firstChild.textContent.trim() : steps + " steps";
597597 };
598 // Earlier: the older page is fetched and put above what is read, oldest first, and the place being
599 // read stays where it is; without the script the link opens that page by itself.
600 const more = root.querySelector("a[data-earlier]");
601 if (more) more.addEventListener("click", async (e) => {
602 if (e.button || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
603 e.preventDefault();
604 if (more.hasAttribute("aria-busy")) return;
605 more.setAttribute("aria-busy", "true");
606 try {
607 const res = await fetch(more.href, { credentials: "same-origin", headers: { accept: "text/html" } });
608 if (!res.ok) throw new Error("earlier");
609 const doc = new DOMParser().parseFromString(await res.text(), "text/html");
610 const older = doc.querySelector(".talk-log");
611 if (!older) throw new Error("earlier");
612 const anchor = log.firstElementChild, was = anchor ? anchor.getBoundingClientRect().top : 0;
613 stuck = false;
614 log.prepend(...[...older.children].map((n) => document.importNode(n, true)));
615 if (anchor) { const d = anchor.getBoundingClientRect().top - was; if (d) sc().scrollBy({ top: d, behavior: "instant" }); }
616 decorate(log);
617 const next = doc.querySelector("a[data-earlier]");
618 if (next) { more.href = next.getAttribute("href"); more.removeAttribute("aria-busy"); } else more.parentElement.remove();
619 draw();
620 } catch (_) { more.removeAttribute("aria-busy"); location.href = more.href; }
621 });
598622 const decorate = (scope) => {
599623 scope.querySelectorAll(".talk-body pre:not([data-c])").forEach((pre) => {
600624 pre.dataset.c = "1";
test/talk-history-order.test.ts added +92
@@ -0,0 +1,92 @@
1 /**
2 * A long Talk history reads oldest to newest (#16560), without a browser: the latest page is the
3 * last fifty turns in order, Earlier is the fifty before them in order, and the script puts them
4 * above the page being read instead of opening a page that starts at its own end.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { appendTurn, createConversation, listTurns, renameConversation } from "../src/core/conversations.ts";
12 import { createSession } from "../src/core/sessions.ts";
13 import { createUser } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { quietLogs } from "./helpers/globals.ts";
16 import { promote } from "./helpers/members.ts";
17
18 quietLogs();
19 let dir: string;
20 let app: App;
21 let servers: Servers;
22 let token: string;
23 let userId: string;
24 let id: string;
25
26 beforeAll(async () => {
27 dir = await mkdtemp(join(tmpdir(), "forge-talk-order-"));
28 app = createApp({ dataDir: dir, port: 0 });
29 servers = await serve(app);
30 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
31 if (!made.ok) throw new Error(made.error);
32 promote(app.db, made.value);
33 userId = made.value.id;
34 token = createSession(app.db, userId).token;
35 const c = createConversation(app.db, userId);
36 if (!c.ok) throw new Error(c.error);
37 id = c.value.id;
38 for (let i = 0; i < 120; i++) appendTurn(app.db, userId, id, { role: i % 2 === 0 ? "person" : "agent", text: `message ${i}` });
39 }, 30_000);
40 afterAll(async () => {
41 servers.stop();
42 app.close();
43 await rm(dir, { recursive: true, force: true });
44 });
45
46 const firstOfLatest = (): string => {
47 const r = listTurns(app.db, userId, id, { limit: 50 });
48 if (!r.ok) throw new Error(r.error);
49 return r.value[0]!.id;
50 };
51 const get = async (path: string): Promise<string> => (await fetch(`${servers.url}${path}`, { headers: { cookie: `forge_session=${token}` } })).text();
52 const logOf = (page: string): number[] => [...page.slice(page.indexOf('class="talk-log"')).matchAll(/message (\d+)/g)].map((m) => Number(m[1]));
53
54 test("the latest page is 70..119 in order, and its Earlier link names the first of them", async () => {
55 const page = await get(`/talk/${id}`);
56 const nums = logOf(page);
57 expect(nums[0]).toBe(70);
58 expect(nums.at(-1)).toBe(119);
59 expect(nums).toEqual([...nums].sort((a, b) => a - b));
60 const first = firstOfLatest();
61 expect(page).toContain(`href="/talk/${id}?before=${first}" data-earlier`);
62 });
63
64 test("Earlier is 20..69 in order, and a page of its own still opens without the script", async () => {
65 const first = firstOfLatest();
66 const nums = logOf(await get(`/talk/${id}?before=${first}`));
67 expect(nums[0]).toBe(20);
68 expect(nums.at(-1)).toBe(69);
69 expect(nums).toEqual([...nums].sort((a, b) => a - b));
70 });
71
72 test("the script puts Earlier above the page being read and keeps the place", async () => {
73 const { talkJs } = await import("../src/web/pages/talk_client.ts");
74 expect(talkJs).toContain('a[data-earlier]');
75 expect(talkJs).toContain("log.prepend(");
76 expect(talkJs).toContain("scrollBy({ top: d");
77 });
78 test("the chat list is newest first and the script does not follow the end there", async () => {
79 const { talkJs } = await import("../src/web/pages/talk_client.ts");
80 expect(talkJs).toContain('if (!on && status.textContent === ANSWERING) say("")');
81 expect(talkJs).toContain("let stuck = !document.querySelector('[data-view=\"list\"]')");
82 for (let i = 0; i < 20; i++) {
83 const c = createConversation(app.db, userId);
84 if (!c.ok) throw new Error(c.error);
85 renameConversation(app.db, userId, c.value.id, `Chat number ${i}`);
86 appendTurn(app.db, userId, c.value.id, { role: "person", text: "hello" });
87 }
88 const page = await get("/talk?chats=1");
89 expect(page).toContain('data-view="list"');
90 expect(page.indexOf("Chat number 19")).toBeGreaterThan(-1);
91 expect(page.indexOf("Chat number 19")).toBeLessThan(page.indexOf("Chat number 0"));
92 });
test/talk-phone.test.ts → test/talk-phone.looks.test.ts renamed +49 −14
@@ -2,7 +2,10 @@
22 * Talk on a phone (#16442), measured in a real Chromium at 360 px with a finger: the head is one
33 * row of the list, the title, the connection picker, the menu and New; the composer does not move
44 * as the tool-call count grows; the chat list opens at its top; the "answering" line ends with the
5 * answer. Skipped, and says so, where no Chromium starts.
5 * answer; a long history reads oldest to newest, opens at the newest, and Earlier puts older turns
6 * above without moving the place being read. Skipped, and says so, where no Chromium starts; named
7 * `.looks.test.ts` so the check's looks lane runs it (a plain `.test.ts` file is left to the gate,
8 * which has no browser).
69 */
710 import { afterAll, beforeAll, expect, test } from "bun:test";
811 import { mkdtemp, rm } from "node:fs/promises";
@@ -31,6 +34,7 @@ let token: string;
3134 let shortId: string;
3235 let manyId: string;
3336 let listToken: string;
37 let longId: string;
3438
3539 const toolTurns = (userId: string, id: string, n: number): void => {
3640 appendTurn(app.db, userId, id, { role: "person", text: "change the title" });
@@ -53,6 +57,12 @@ beforeAll(async () => {
5357 if (!b.ok) throw new Error(b.error);
5458 manyId = b.value.id;
5559 toolTurns(made.value.id, manyId, 9);
60 // A long history: 120 numbered messages, so the latest page holds 70..119 and Earlier holds
61 // 20..69.
62 const l = createConversation(app.db, made.value.id);
63 if (!l.ok) throw new Error(l.error);
64 longId = l.value.id;
65 for (let i = 0; i < 120; i++) appendTurn(app.db, made.value.id, longId, { role: i % 2 === 0 ? "person" : "agent", text: `message ${i}` });
5666 // A second person with twenty chats, to see where the list opens.
5767 const bob = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
5868 if (!bob.ok) throw new Error(bob.error);
@@ -145,16 +155,41 @@ test.skipIf(chromium === undefined)("the answering line is gone when the answer
145155 }
146156 }, 30_000);
147157
148 test("without a browser: the page's script ends the answering line with the answer and does not follow the end in the chat list", async () => {
149 const { talkJs } = await import("../src/web/pages/talk_client.ts");
150 // Every way an answer ends goes through mode(false), which takes the line away.
151 expect(talkJs).toContain('if (!on && status.textContent === ANSWERING) say("")');
152 expect(talkJs).toContain("let stuck = !document.querySelector('[data-view=\"list\"]')");
153 const res = await fetch(`${servers.url}/talk?chats=1`, { headers: { cookie: `forge_session=${listToken}` } });
154 const page = await res.text();
155 expect(page).toContain('data-view="list"');
156 // Newest first: the list's first row is the latest chat.
157 expect(page).toContain("Chat number 0");
158 expect(page.indexOf("Chat number 19")).toBeLessThan(page.indexOf("Chat number 0"));
159 expect(page.indexOf("Chat number 19")).toBeGreaterThan(-1);
160 });
158 const numbers = `[...document.querySelectorAll(".talk-log .talk-body, .talk-log .talk-person p")].map((e) => +(/message (\\d+)/.exec(e.textContent) || [])[1]).filter((n) => n >= 0)`;
159
160 test.skipIf(chromium === undefined)("a long history reads oldest at the top, newest at the bottom, and opens at the newest", async () => {
161 const s = await phone(`/talk/${longId}`, token, `(async () => {
162 const page = document.scrollingElement, nums = ${numbers};
163 for (let i = 0; i < 200 && page.scrollHeight - innerHeight - page.scrollTop > 2; i++) await new Promise((r) => requestAnimationFrame(r));
164 const last = [...document.querySelectorAll(".talk-log > li")].at(-1).getBoundingClientRect();
165 return JSON.stringify({ nums, y: page.scrollTop, max: page.scrollHeight - innerHeight, bottom: last.bottom, vh: innerHeight });
166 })()`);
167 const seen = JSON.stringify({ ...s, nums: s.nums.length });
168 expect(s.nums, seen).toEqual([...s.nums].sort((a: number, b: number) => a - b));
169 expect(s.nums[0], seen).toBe(70);
170 expect(s.nums.at(-1), seen).toBe(119);
171 expect(s.max - s.y, seen).toBeLessThanOrEqual(2);
172 expect(s.bottom, seen).toBeLessThanOrEqual(s.vh + 1);
173 }, 30_000);
174
175 test.skipIf(chromium === undefined)("Earlier puts the older turns above, in order, and the message being read stays where it was", async () => {
176 const s = await phone(`/talk/${longId}`, token, `(async () => {
177 const frames = async (n) => { for (let i = 0; i < n; i++) await new Promise((r) => requestAnimationFrame(r)); };
178 const page = document.scrollingElement;
179 for (let i = 0; i < 200 && page.scrollHeight - innerHeight - page.scrollTop > 2; i++) await frames(1);
180 window.scrollTo({ top: 0, behavior: "instant" });
181 await frames(5);
182 const first = document.querySelector(".talk-log [id^='t-']");
183 const before = first.getBoundingClientRect().top;
184 document.querySelector("a[data-earlier]").click();
185 for (let i = 0; i < 60 && document.querySelectorAll(".talk-log [id^='t-']").length < 100; i++) await frames(3);
186 await frames(5);
187 return JSON.stringify({ nums: ${numbers}, before, after: first.getBoundingClientRect().top, url: location.search, more: !!document.querySelector("a[data-earlier]") });
188 })()`);
189 const seen = JSON.stringify({ ...s, nums: s.nums.length });
190 expect(s.nums, seen).toEqual([...s.nums].sort((a: number, b: number) => a - b));
191 expect(s.nums[0], seen).toBe(20);
192 expect(s.nums.at(-1), seen).toBe(119);
193 expect(s.url, seen).toBe("");
194 expect(Math.abs(s.after - s.before), seen).toBeLessThanOrEqual(2);
195 }, 30_000);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs