oficina / base Public

Keep the Ask conversation at its newest line, above the status row

The log scrolled to its bottom only when a line was appended, and before
the status row ("Equipo is answering…") and Stop appeared below it.
Their arrival shrank the log without moving its scroll position, so the
newest bubble ended half hidden behind them, and nothing brought it back
until the first streamed word.

The panel now keeps the log pinned while the person is within 80px of
its bottom: a ResizeObserver on the log and a MutationObserver on its
lines put it back whenever its size or content changes. Sending always
goes to the bottom. Scrolled up to read, new lines leave it put and a
"Jump to latest" button appears (new, Review 6). Scrolling is instant.
The log, the status row and the composer are a flex column, the footer
never over the log, in the popover, the phone sheet and /ask (which now
fits the screen with its own scrolling log).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev cd42405ba6d95d95af7c3a4747e47bb98b38bd19 parent 6fb6030
6 files changed, +189 −7
  • docs/copy-pending.md +1 −0
  • internal/browsertest/ask_test.go +14 −1
  • internal/browsertest/scroll_test.go +127 −0
  • internal/look/static/app.css +11 −3
  • internal/look/static/ask.js +34 −2
  • internal/web/templates/partials/chat.html +2 −1
diff --git a/docs/copy-pending.md b/docs/copy-pending.md
index f016e2a..5a3d553 100644
--- a/docs/copy-pending.md
+++ b/docs/copy-pending.md
@@ -294,6 +294,7 @@ Base's own words:
| File | Context | Text |
|---|---|---|
| internal/web/ask_cards.go, ask.js | A publish card whose site failed to publish (Equipo's site state "failed"; Equipo has no short line for it) | Publishing failed. |
+| internal/web/templates/partials/chat.html | Ask panel and /ask: a small button at the foot of the conversation, shown when something new arrives while the person has scrolled up to read | Jump to latest |
Equipo's words, reused as Equipo has them (internal/equipo/copy.go on its base-publish branch; Equipo reviews them):
diff --git a/internal/browsertest/ask_test.go b/internal/browsertest/ask_test.go
index 74132d1..83c04b8 100644
--- a/internal/browsertest/ask_test.go
+++ b/internal/browsertest/ask_test.go
@@ -47,6 +47,14 @@ func askBase(t *testing.T) (*harness.Rig, *equipotest.Fake) {
// askBaseWith is askBase with a last say over Base's configuration.
func askBaseWith(t *testing.T, tweak func(*config.Config)) (*harness.Rig, *equipotest.Fake, *baseApp) {
+ t.Helper()
+ return askBaseOn(t, tweak, nil)
+}
+
+// askBaseOn is askBaseWith on a screen of view's size, set before anything
+// loads (a phone: taps then land where they are drawn); nil is 1280x800,
+// set after signing in.
+func askBaseOn(t *testing.T, tweak func(*config.Config), view chromedp.Action[chromedp.Void]) (*harness.Rig, *equipotest.Fake, *baseApp) {
t.Helper()
f := equipotest.New(t)
f.Reply = func(string) string {
@@ -62,8 +70,13 @@ func askBaseWith(t *testing.T, tweak func(*config.Config)) (*harness.Rig, *equip
b = newBase(t, cfg)
return b.handler
})
+ if view != nil {
+ drive(t, rig, view)
+ }
signInByEmail(t, rig, rig.Origin, b.mail)
- drive(t, rig, chromedp.EmulateViewport(1280, 800))
+ if view == nil {
+ drive(t, rig, chromedp.EmulateViewport(1280, 800))
+ }
connect := `form[action="/ask/connect"] button`
drive(t, rig, chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS(connect)),
chromedp.Click(chromedp.CSS(connect)), chromedp.WaitVisible(chromedp.CSS(`p.code`)))
diff --git a/internal/browsertest/scroll_test.go b/internal/browsertest/scroll_test.go
new file mode 100644
index 0000000..462d835
--- /dev/null
+++ b/internal/browsertest/scroll_test.go
@@ -0,0 +1,127 @@
+//go:build browser
+
+package browsertest
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/chromedp/chromedp"
+
+ "amadan.net/rastrillo/rastrillo/harness"
+)
+
+// lastVisible is "" when the conversation's last bubble is wholly inside
+// the log, above the status row and the composer, and the log is at its
+// bottom; else what is wrong.
+const lastVisible = `(() => {
+ const p = document.querySelector("#ask-panel"), log = p.querySelector(".thread");
+ const items = log.querySelectorAll(":scope > li"), last = items[items.length - 1];
+ if (!last) return "no bubbles";
+ const l = log.getBoundingClientRect(), b = last.getBoundingClientRect();
+ const foot = p.querySelector(".ask-foot").getBoundingClientRect(), comp = p.querySelector("form.composer").getBoundingClientRect();
+ const out = [];
+ if (b.bottom > l.bottom + 1) out.push("last bubble ends at " + Math.round(b.bottom) + ", below the log's " + Math.round(l.bottom));
+ if (b.bottom > foot.top + 1 && foot.height > 0) out.push("last bubble under the status row");
+ if (l.bottom > comp.top + 1) out.push("log runs under the composer");
+ if (foot.height > 0 && l.bottom > foot.top + 1) out.push("log runs under the status row");
+ if (log.scrollHeight - log.scrollTop - log.clientHeight > 2) out.push("log not at its bottom");
+ return out.join("; ");
+})()`
+
+// The conversation follows its newest line in the popover and on a phone:
+// after sending it is at the bottom, with the last bubble wholly above the
+// status row; streaming keeps it there; scrolled up, it stays put and
+// offers Jump to latest.
+func TestAskScrollFollowsNewest(t *testing.T) {
+ for _, size := range []struct {
+ name string
+ w, h int64
+ }{{"popover", 1280, 800}, {"phone", 390, 844}} {
+ t.Run(size.name, func(t *testing.T) {
+ var view chromedp.Action[chromedp.Void]
+ if size.name == "phone" {
+ view = chromedp.EmulateViewport(size.w, size.h, chromedp.EmulateMobile, chromedp.EmulateScale(3))
+ }
+ rig, f, _ := askBaseOn(t, nil, view)
+ f.Reply = func(string) string {
+ return strings.Repeat("Here is a long answer that streams in word by word. ", 12)
+ }
+ for i := 1; i <= 30; i++ {
+ f.Say(fmt.Sprintf("Earlier line %d of a long conversation.", i))
+ }
+ if size.name == "phone" {
+ drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`a.ask-icon`)),
+ chromedp.Click(chromedp.CSS(`a.ask-icon`)), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)))
+ } else {
+ openPanel(t, rig)
+ }
+ wait := func(js string) {
+ t.Helper()
+ drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
+ }
+ wait(`document.querySelectorAll("#ask-panel .thread > li").length >= 30`)
+ if got := harness.Evaluate[string](rig, lastVisible); got != "" {
+ t.Errorf("on opening: %s", got)
+ }
+
+ // Send while the coordinator has not started: the person's bubble
+ // is wholly above "Equipo is answering…" and Stop.
+ send := func(text string) {
+ t.Helper()
+ // Enter sends, as a person does on a keyboard; scrolled to the
+ // top first, so sending must bring the log back down.
+ drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 0`),
+ chromedp.SendKeys(chromedp.CSS(`#ask-text`), text+"\r"))
+ }
+ f.Hold = true
+ send("Can we fix a bug in Tito?")
+ if _, err := chromedp.Run(rig.Context(), chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel form.stop").hidden && document.querySelector("#ask-panel .ask-status").textContent !== ""`, chromedp.WithPollingTimeout(10*time.Second))); err != nil {
+ t.Fatalf("no answering status: %v; person bubbles %d, status %q, field %q", err,
+ harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .msg.person").length`),
+ harness.Evaluate[string](rig, `document.querySelector("#ask-panel .ask-status").textContent`),
+ harness.Evaluate[string](rig, `document.querySelector("#ask-text").value`))
+ }
+ drive(t, rig, chromedp.Sleep(200*time.Millisecond))
+ if got := harness.Evaluate[string](rig, lastVisible); got != "" {
+ t.Errorf("sent, waiting for an answer: %s", got)
+ }
+ drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel form.stop button`)))
+ wait(`document.querySelector("#ask-panel form.stop").hidden`)
+ f.Hold = false
+
+ // Send: the log goes to the bottom, and stays there as the reply
+ // streams in under "Equipo is answering…".
+ send("What can I do")
+ wait(`(() => { const d = document.querySelector("#ask-panel .msg.draft"); return d && d.textContent.length > 120 })()`)
+ if got := harness.Evaluate[string](rig, lastVisible); got != "" {
+ t.Errorf("while streaming: %s", got)
+ }
+ wait(`!document.querySelector("#ask-panel .msg.draft") && document.querySelector("#ask-panel .ask-status").textContent === ""`)
+ if got := harness.Evaluate[string](rig, lastVisible); got != "" {
+ t.Errorf("after the reply: %s", got)
+ }
+
+ // Scrolled up to read: a new line does not move the log.
+ drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.querySelector("#ask-panel .thread").scrollTop = 100`),
+ chromedp.Sleep(200*time.Millisecond)) // the scroll event, a frame later
+ f.Say("A new line while you read.")
+ wait(`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "A new line while you read.")`)
+ if got := harness.Evaluate[int](rig, `Math.round(document.querySelector("#ask-panel .thread").scrollTop)`); got != 100 {
+ t.Errorf("reading earlier lines, the log moved to %d", got)
+ }
+ jump := `#ask-panel button.jump`
+ wait(`!document.querySelector("#ask-panel button.jump").hidden`)
+ if got := harness.Evaluate[string](rig, `document.querySelector("`+jump+`").textContent`); got != "Jump to latest" {
+ t.Errorf("jump control reads %q", got)
+ }
+ drive(t, rig, chromedp.Click(chromedp.CSS(jump)))
+ wait(`document.querySelector("#ask-panel button.jump").hidden`)
+ if got := harness.Evaluate[string](rig, lastVisible); got != "" {
+ t.Errorf("after Jump to latest: %s", got)
+ }
+ })
+ }
+}
diff --git a/internal/look/static/app.css b/internal/look/static/app.css
index e2c6666..32abf0a 100644
--- a/internal/look/static/app.css
+++ b/internal/look/static/app.css
@@ -469,7 +469,15 @@ dialog.ask-panel::backdrop { background: transparent; }
.ask-note { padding: 22px 20px; display: flex; flex-direction: column; gap: 14px; align-items: flex-start; }
.ask-note p { margin: 0; color: var(--haze); }
.ask-chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
-.thread { flex: 1; min-height: 0; overflow-y: auto; list-style: none; margin: 0; padding: 18px 16px; display: flex; flex-direction: column; gap: 10px; }
+/* The log scrolls; the status row (with Stop) and the composer are the
+ footer below it, never over it. Jump to latest floats at the log's foot
+ when the person has scrolled up and something new arrives. */
+.log { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; }
+.jump { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); min-height: 36px; padding: 0 14px; border: 0; border-radius: 18px;
+ font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; color: var(--on-signal); background: var(--signal);
+ box-shadow: 0 6px 18px var(--glass-lift) var(--glass-shadow); }
+.ask-foot, .composer { flex: none; }
+.thread { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; list-style: none; margin: 0; padding: 18px 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 86%; padding: 10px 14px; border-radius: 14px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; line-height: 1.5; }
.msg.person { align-self: flex-end; background: var(--mine); box-shadow: inset 0 0 0 1px var(--mine-edge); border-bottom-right-radius: 4px; }
.msg.coordinator { align-self: flex-start; background: var(--theirs); box-shadow: inset 0 0 0 1px var(--theirs-edge); border-bottom-left-radius: 4px; }
@@ -491,7 +499,7 @@ dialog.ask-panel::backdrop { background: transparent; }
.ask-page { display: flex; flex-direction: column; padding: 22px 0 0; }
.ask-page .crumb { padding: 0 24px; }
.ask-page .ask-head { padding-left: 24px; }
-.ask-page .thread { max-height: none; }
+.ask-page { height: calc(100dvh - 36px); min-height: 420px; }
.signin p.code { margin: 6px 0 18px; font-size: 32px; font-weight: 600; letter-spacing: .14em; text-align: center; color: var(--frost);
font-variant-numeric: tabular-nums; font-variation-settings: "STYL" 0; }
body:has(dialog.ask-panel[open]) { overflow: hidden; }
@@ -544,7 +552,7 @@ main { min-height: 100dvh; }
dialog.ask-panel .ask-empty, dialog.ask-panel .ask-problem { padding: 0 12px 12px; }
.thread { padding: 14px 12px; }
.msg { max-width: 92%; }
- .ask-page { min-height: calc(100dvh - 20px); }
+ .ask-page { height: calc(100dvh - 20px); }
}
/* With reduced motion the chat just shows: no slide, no scale, and Home
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index a0aa71c..5cd9dfe 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -39,9 +39,37 @@ function start(root) {
return Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
}
- function scroll() {
+ // The log follows its newest line while the person is at (or within
+ // 80px of) its bottom. Anything that changes its height (a new line, a
+ // card, a streamed word, or the status row and Stop appearing below it)
+ // keeps it there; if they have scrolled up to read, it stays put and
+ // Jump to latest appears. Scrolling is always instant.
+ const jump = root.querySelector("button.jump");
+ let pinned = true;
+ const atBottom = () => thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80;
+ function toBottom() {
thread.scrollTop = thread.scrollHeight;
+ pinned = true;
+ if (jump) jump.hidden = true;
+ }
+ function scroll() {
empty.hidden = thread.children.length > 0;
+ if (pinned) toBottom();
+ else if (jump) jump.hidden = false;
+ }
+ thread.addEventListener("scroll", () => {
+ pinned = atBottom();
+ if (pinned && jump) jump.hidden = true;
+ });
+ if (jump) jump.addEventListener("click", toBottom);
+ // The log's own box shrinks when the status row or a taller composer
+ // takes room below it, and its content grows with every word: either
+ // way, a pinned log goes back to its bottom.
+ if (window.ResizeObserver) {
+ const keep = () => { if (pinned) toBottom(); };
+ const ro = new ResizeObserver(keep);
+ ro.observe(thread);
+ new MutationObserver(keep).observe(thread, { childList: true, subtree: true, characterData: true });
}
function item(attr, id) {
@@ -275,10 +303,11 @@ function start(root) {
show(s.state);
if (s.state !== "ready" || !live()) return;
thread.replaceChildren();
+ pinned = true;
s.messages.forEach(addMessage);
s.cards.forEach(addCard);
setTurn(s.turn, s.status);
- scroll();
+ toBottom();
listen(s.cursor);
}
@@ -331,8 +360,11 @@ function start(root) {
const sent = await post(composer);
if (!sent) return;
field.value = "";
+ // Sending always goes to the bottom, wherever the person had scrolled.
+ pinned = true;
addMessage(sent.message);
setTurn(sent.turn);
+ toBottom();
});
field.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !e.shiftKey && !e.isComposing) {
diff --git a/internal/web/templates/partials/chat.html b/internal/web/templates/partials/chat.html
index c6d30e4..8c4112c 100644
--- a/internal/web/templates/partials/chat.html
+++ b/internal/web/templates/partials/chat.html
@@ -27,10 +27,11 @@
<p>Equipo isn't answering. Try again in a minute.</p>
</div>
<div class="ask-chat" data-when="ready"{{if not (.Shows "ready")}} hidden{{end}}>
-<ol class="thread" role="log" aria-label="Conversation">
+<div class="log"><ol class="thread" role="log" aria-label="Conversation">
{{range .Messages}}<li class="msg {{.Role}}" data-id="{{.ID}}">{{if eq .Role "coordinator"}}{{$.Rich .Text}}{{else}}{{.Text}}{{end}}</li>
{{end}}{{range .Cards}}{{template "ask-card" .}}
{{end}}</ol>
+<button class="jump" type="button" hidden>Jump to latest</button></div>
<p class="ask-empty" data-empty{{if or .Messages .Cards}} hidden{{end}}>Ask about your work, or ask Base to get something done.</p>
{{with .Problem}}<p class="ask-problem" role="alert">{{.}}</p>{{end}}
<div class="ask-foot">