oficina / base Public

Keep Home from waiting on a slow or closed Assistant chat

Home read the docked chat's conversation before drawing anything, with
the client's 20 s limit, so a stalled Equipo held up the whole page.
Home now gives the read 1.5 s and, past it, draws the chat open with
its composer and no claim that the conversation is empty; the panel's
script reads it in. With the chat closed Home does not read it at all.
The script now shows or hides the empty line itself after each read.

Tests: a stalled snapshot answered promptly on Home (webtest and
browser), no read with the chat closed, and an empty conversation's
line shown once the script reads it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev e00529d0683c143e9ad6a0a0e591a7cf8129abc5 parent b9b76d9
6 files changed, +147 −5
  • internal/browsertest/ask_test.go +46 −0
  • internal/look/static/ask.js +2 −0
  • internal/web/ask.go +21 −1
  • internal/web/home.go +13 −3
  • internal/web/templates/partials/chat.html +1 −1
  • internal/webtest/home_test.go +64 −0
diff --git a/internal/browsertest/ask_test.go b/internal/browsertest/ask_test.go
index 20859d0..a9a6ec2 100644
--- a/internal/browsertest/ask_test.go
+++ b/internal/browsertest/ask_test.go
@@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"strconv"
+ "sync"
"testing"
"time"
@@ -324,6 +325,51 @@ func TestAskDockedOpenByDefault(t *testing.T) {
}
}
+// Home never waits on Equipo: closed, Home reads nothing and the script
+// reads the conversation as the chat opens (an empty one saying so);
+// open, with the read stalled, Home comes up well inside the client's own
+// 20 s with the chat open, and the script reads the conversation in.
+func TestAskHomeDoesNotWaitOnEquipo(t *testing.T) {
+ rig, f, b := askBaseWith(t, nil)
+ openPanel(t, rig)
+ drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel [data-ask-close]`)))
+ askClosed(t, b, true)
+ before := len(f.Calls("/api/v1/assistant/snapshot"))
+ drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`button.base-btn`)), chromedp.Sleep(300*time.Millisecond))
+ if n := len(f.Calls("/api/v1/assistant/snapshot")) - before; n != 0 {
+ t.Errorf("Home with the chat closed read the conversation %d times", n)
+ }
+ drive(t, rig, chromedp.Click(chromedp.CSS(`button.base-btn`)),
+ chromedp.Poll[chromedp.Void](settledJS, chromedp.WithPollingTimeout(5*time.Second)),
+ chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), chromedp.Sleep(100*time.Millisecond))
+ if got := harness.Evaluate[string](rig, `(() => { const e = document.querySelector("#ask-panel [data-empty]");
+ return e.hidden + " " + document.querySelectorAll("#ask-panel .thread > li").length; })()`); got != "false 0" {
+ t.Errorf("opened on an empty conversation, the empty line hidden and lines: %s", got)
+ }
+ askClosed(t, b, false)
+
+ f.Say("Your week is clear.")
+ entered, hold := f.HoldNextSnapshot()
+ release := sync.OnceFunc(hold)
+ defer release()
+ start := time.Now()
+ drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)))
+ if took := time.Since(start); took > 5*time.Second {
+ t.Errorf("Home took %v with the conversation's read stalled", took)
+ }
+ select {
+ case <-entered:
+ default:
+ t.Fatal("Home never read the conversation")
+ }
+ drive(t, rig, chromedp.Poll[chromedp.Void](`[...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.textContent === "Your week is clear.")`,
+ chromedp.WithPollingTimeout(10*time.Second)))
+ if !harness.Evaluate[bool](rig, settledJS) || harness.Evaluate[bool](rig, `!document.querySelector("#ask-panel [data-empty]").hidden`) {
+ t.Error("the chat is not open with its conversation")
+ }
+ release()
+}
+
// The X slides the docked chat out and Home takes the width (the apps grid
// gains a column); the Base button shows, with focus. A reload keeps it
// closed. The Base button, or "/", brings it back, and a reload keeps it
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index 76c96c0..6b9d2d8 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -696,6 +696,8 @@ function start(root) {
(j.cards || []).forEach(addCard);
}
s.cards.forEach(addCard);
+ // Home may have drawn the chat unread, saying nothing of it.
+ empty.hidden = thread.children.length > 0;
if (viewing && !item("data-job", viewing)) closeView(false);
else if (focused) item("data-job", viewing).querySelector("[data-job-close]").focus({ preventScroll: true });
setTurn(s.turn, s.status);
diff --git a/internal/web/ask.go b/internal/web/ask.go
index 4cbf972..cd49074 100644
--- a/internal/web/ask.go
+++ b/internal/web/ask.go
@@ -180,6 +180,10 @@ type askView struct {
// Text is what the person wrote and Equipo did not take, kept in the
// field.
Text string
+ // Unread is a conversation Home did not read (its chat was closed, or
+ // Equipo was slow to answer): drawn without saying it is empty, for
+ // the panel's script to read.
+ Unread bool
}
// jobView is a job card as the chat draws it, a pill, with the go-ahead
@@ -314,8 +318,24 @@ func (v *askView) fill(s equipo.Snapshot) {
// has answered, it records who the bearer speaks for, and registers the
// member's host if Equipo does not hold it yet.
func (a *app) askSnapshot(r *http.Request, me store.Member, l store.EquipoLink, token string, v *askView) int64 {
- s, err := a.ask.client.Snapshot(r.Context(), token)
+ return a.askSnapshotWithin(r, me, l, token, v, 0)
+}
+
+// askSnapshotWithin is askSnapshot giving Equipo at most wait to answer
+// (0: the client's own limit). Past it, v stays as it was, marked Unread.
+func (a *app) askSnapshotWithin(r *http.Request, me store.Member, l store.EquipoLink, token string, v *askView, wait time.Duration) int64 {
+ ctx := r.Context()
+ if wait > 0 {
+ var cancel context.CancelFunc
+ ctx, cancel = context.WithTimeout(ctx, wait)
+ defer cancel()
+ }
+ s, err := a.ask.client.Snapshot(ctx, token)
switch {
+ case err != nil && wait > 0 && errors.Is(ctx.Err(), context.DeadlineExceeded) && r.Context().Err() == nil:
+ a.logger.Warn("ask: snapshot slow", "wait", wait)
+ v.Unread = true
+ return 0
case errors.Is(err, equipo.ErrUnauthenticated):
a.askLost(r.Context(), me, l.DeviceID)
v.State = askReconnect
diff --git a/internal/web/home.go b/internal/web/home.go
index 3583c2e..4c9d793 100644
--- a/internal/web/home.go
+++ b/internal/web/home.go
@@ -94,6 +94,10 @@ func greeting(t time.Time, loc *time.Location, name string) string {
return "Good evening, " + name + "."
}
+// homeSnapshotWait is how long Home waits for Equipo to read the docked
+// chat's conversation before it draws the chat without it.
+const homeSnapshotWait = 1500 * time.Millisecond
+
func (a *app) home(w http.ResponseWriter, r *http.Request) {
me, _ := CurrentMember(r)
ms, err := store.Members(r.Context(), a.db)
@@ -156,9 +160,15 @@ func (a *app) home(w http.ResponseWriter, r *http.Request) {
state = askUnavailable
}
v := a.newAskView(homeState(state), "home")
- // Docked, the conversation is on Home from the first paint.
- if state == askReady {
- a.askSnapshot(r, me, l, sec.Token, &v)
+ // Docked open, the conversation is on Home from the first paint,
+ // if Equipo answers in time: Home never waits on it. Closed, it
+ // is not read; the panel's script reads it when the chat opens.
+ switch {
+ case state != askReady:
+ case me.AskClosed:
+ v.Unread = true
+ default:
+ a.askSnapshotWithin(r, me, l, sec.Token, &v, homeSnapshotWait)
}
page.Ask, page.AskOpen = &v, !me.AskClosed
}
diff --git a/internal/web/templates/partials/chat.html b/internal/web/templates/partials/chat.html
index ec1a102..c80038e 100644
--- a/internal/web/templates/partials/chat.html
+++ b/internal/web/templates/partials/chat.html
@@ -43,7 +43,7 @@
{{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 .Jobs .Cards}} hidden{{end}}>Ask about your work, or ask Base to get something done.</p>
+<p class="ask-empty" data-empty{{if or .Messages .Jobs .Cards .Unread}} hidden{{end}}>Ask about your work, or ask Base to get something done.</p>
{{with .Problem}}<p class="ask-problem" role="alert">{{.}}</p>{{end}}
{{/* Without a runner that can answer, the conversation shows and says so; what the person wrote stays in the field. */}}<p class="ask-problem ask-runner" data-when="norunner"{{if not (.Shows "norunner")}} hidden{{end}} role="alert">Ask Base isn't set up for you yet. <a href="{{.Equipo}}" target="_blank" rel="noopener">Finish setting it up</a>, then try again.</p>
<p class="ask-problem ask-runner" data-when="oldrunner"{{if not (.Shows "oldrunner")}} hidden{{end}} role="alert">Ask Base needs an update first. <a href="{{.Equipo}}" target="_blank" rel="noopener">Update it</a>, then try again.</p>
diff --git a/internal/webtest/home_test.go b/internal/webtest/home_test.go
index 49379d0..fe06391 100644
--- a/internal/webtest/home_test.go
+++ b/internal/webtest/home_test.go
@@ -6,6 +6,7 @@ import (
"net/url"
"regexp"
"strings"
+ "sync"
"testing"
"time"
@@ -340,3 +341,66 @@ func TestHomeDocksAskOpenByDefault(t *testing.T) {
t.Errorf("/ask/dock without Equipo: %d", r.Code)
}
}
+
+// Home never waits on Equipo: with the conversation's read stalled, Home
+// answers well inside the client's own 20 s, the chat docked open with
+// its composer and nothing claimed about the conversation (the panel's
+// script reads it). With the chat closed Home does not read it at all.
+func TestHomeDoesNotWaitOnAStalledSnapshot(t *testing.T) {
+ a, f := newAskApp(t)
+ a.connectEquipo(f)
+ f.Say("Your week is clear.")
+ entered, hold := f.HoldNextSnapshot()
+ release := sync.OnceFunc(hold)
+ defer release()
+ start := time.Now()
+ done := make(chan Resp, 1)
+ go func() { done <- a.get("/") }()
+ var home Resp
+ select {
+ case home = <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("Home waited more than 5 s on a stalled snapshot")
+ }
+ select {
+ case <-entered:
+ default:
+ t.Fatal("Home never asked for the conversation")
+ }
+ if took := time.Since(start); home.Code != 200 || took > 4*time.Second {
+ t.Fatalf("Home: %d after %v", home.Code, took)
+ }
+ if deskClosed(t, home.Body) {
+ t.Error("Home drew the chat closed")
+ }
+ for _, want := range []string{
+ `<aside id="ask-panel" class="ask-panel glass"`,
+ `<div class="ask-chat" data-when="ready norunner oldrunner">`,
+ `<form class="composer" method="post" action="/ask/messages">`,
+ `<p class="ask-empty" data-empty hidden>`,
+ `<div class="ask-note" data-when="unavailable" hidden>`,
+ } {
+ if !strings.Contains(home.Body, want) {
+ t.Errorf("Home with a stalled snapshot lacks %q:\n%s", want, home.Body)
+ }
+ }
+ if strings.Contains(home.Body, "Your week is clear.") {
+ t.Error("the stalled snapshot was drawn")
+ }
+ release()
+
+ // Closed: Home draws the chat for the script to open, and asks Equipo
+ // nothing.
+ if r := a.post("/ask/dock", url.Values{"open": {"0"}}); r.Code != 303 {
+ t.Fatalf("close: %d", r.Code)
+ }
+ before := len(f.Calls("/api/v1/assistant/snapshot"))
+ closed := a.get("/")
+ if n := len(f.Calls("/api/v1/assistant/snapshot")) - before; n != 0 {
+ t.Errorf("Home with the chat closed read the conversation %d times", n)
+ }
+ if !deskClosed(t, closed.Body) || !strings.Contains(closed.Body, `<form class="composer" method="post" action="/ask/messages">`) ||
+ !strings.Contains(closed.Body, `<p class="ask-empty" data-empty hidden>`) {
+ t.Errorf("Home with the chat closed:\n%s", closed.Body)
+ }
+}