oficina / equipo Public

Lookups: conversations end to end against fakes

Task 3.10: spec §10.1's four conversations (the Dublin forecast, "And
tomorrow?" with the earlier question, web off naming who can turn it
on, a museum's hours then a calendar ask) and the versions in between
(a runner below 36, a Base that can't draw answers, whose history reads
as plain text), against a fake backend, TLS pages behind TestDial and
the fake runner answering the two steps. A change in what Base draws
lists the person's operations afresh.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 146563fe1d50db4f7a24cc01773f8cbc32a0cee1 parent fbccfb2
4 files changed, +268 −1
  • internal/equipo/chat/switches.go +8 −1
  • internal/thintest/assistant_ops_contract_test.go +11 −0
  • internal/thintest/client.go +6 −0
  • internal/thintest/lookup_e2e_test.go +243 −0
diff --git a/internal/equipo/chat/switches.go b/internal/equipo/chat/switches.go
index d9f31d22..746fe73c 100644
--- a/internal/equipo/chat/switches.go
+++ b/internal/equipo/chat/switches.go
@@ -207,7 +207,7 @@ func (s *Store) SetCapabilities(ctx context.Context, person, value string) error
if err := s.db.G.WithContext(ctx).Model(&assistantHostRecord{}).Where("person = ?", person).Pluck("coalesce(capabilities, '')", &have).Error; err == nil && len(have) == 1 && have[0] == caps {
return nil
}
- return s.tx(ctx, func(t *txn) error {
+ err := s.tx(ctx, func(t *txn) error {
var h assistantHostRecord
if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil {
return err
@@ -220,6 +220,13 @@ func (s *Store) SetCapabilities(ctx context.Context, person, value string) error
}
return t.db.Model(&assistantHostRecord{}).Where("person = ?", person).Update("capabilities", caps).Error
})
+ if err == nil {
+ // What is offered depends on it (web_lookup): listed afresh.
+ if ops := s.operations(); ops != nil {
+ ops.Forget(person)
+ }
+ }
+ return err
}
// ownAbilities is GET /api/v1/assistant/abilities's answer for the
diff --git a/internal/thintest/assistant_ops_contract_test.go b/internal/thintest/assistant_ops_contract_test.go
index 11032552..69184376 100644
--- a/internal/thintest/assistant_ops_contract_test.go
+++ b/internal/thintest/assistant_ops_contract_test.go
@@ -93,6 +93,9 @@ type opsRig struct {
protocol int
// boardRev counts the boards setBoard has given (build_off tests).
boardRev int64
+ // lookupAnswer, when set, is the person's model answering a lookup
+ // step that arrives while a call waits (lookup_e2e_test.go).
+ lookupAnswer func(wire.LookupStep) wire.LookupResult
}
func newOpsRig(t *testing.T, providers ...assist.Provider) *opsRig {
@@ -177,6 +180,14 @@ func (o *opsRig) await(calls ...string) map[string]wire.ToolResult {
if err != nil {
o.t.Fatalf("waiting for %v: %v", calls, err)
}
+ if step, ok := body.(*wire.LookupStep); ok && o.lookupAnswer != nil {
+ r := o.lookupAnswer(*step)
+ r.Lookup, r.Attempt, r.Nonce, r.Stage = step.Lookup, step.Attempt, step.Nonce, step.Stage
+ if err := o.runner.Send(o.ctx, "lr-"+f.ID, wire.FrameLookupResult, r); err != nil {
+ o.t.Fatal(err)
+ }
+ continue
+ }
res, ok := body.(*wire.ToolResult)
if !ok {
if f.Type != wire.FrameAck {
diff --git a/internal/thintest/client.go b/internal/thintest/client.go
index 0f0ba5f7..a2df6ea1 100644
--- a/internal/thintest/client.go
+++ b/internal/thintest/client.go
@@ -24,6 +24,9 @@ type Client struct {
Origin string
Token string
HTTP *http.Client
+ // Capabilities, when set, goes with each request as Base sends it
+ // (Base-Capabilities): what the person's Base draws.
+ Capabilities string
}
func NewClient(origin, token string) *Client {
@@ -44,6 +47,9 @@ func (c *Client) do(ctx context.Context, method, path string, body, out any) err
return err
}
r.Header.Set("Authorization", "Bearer "+c.Token)
+ if c.Capabilities != "" {
+ r.Header.Set("Base-Capabilities", c.Capabilities)
+ }
if body != nil {
r.Header.Set("Content-Type", "application/json")
}
diff --git a/internal/thintest/lookup_e2e_test.go b/internal/thintest/lookup_e2e_test.go
new file mode 100644
index 00000000..674805ac
--- /dev/null
+++ b/internal/thintest/lookup_e2e_test.go
@@ -0,0 +1,243 @@
+package thintest
+
+import (
+ "context"
+ "crypto/ecdsa"
+ "crypto/elliptic"
+ "crypto/rand"
+ "crypto/tls"
+ "crypto/x509"
+ "crypto/x509/pkix"
+ "math/big"
+ "net/http"
+ "net/http/httptest"
+ "net/netip"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/equipo/chat"
+ "amadan.net/oficina/equipo/internal/websearch"
+ "amadan.net/oficina/equipo/internal/websearch/websearchtest"
+ "amadan.net/oficina/equipo/internal/websource/guard"
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// Lookups end to end (spec 2026-11-26-assistant-abilities §10.1): a fake
+// search backend, fake pages behind the guard's TestDial, and the
+// person's model on the fake runner answering the two steps.
+
+type confirmedNet struct{}
+
+func (confirmedNet) Ready(context.Context) error { return nil }
+func (confirmedNet) Inside(netip.Addr) bool { return false }
+
+// lookupWeb is the public web: one TLS server for every page host.
+type lookupWeb struct {
+ srv *httptest.Server
+ roots *x509.CertPool
+ mu sync.Mutex
+ got []string
+}
+
+func startLookupWeb(t *testing.T) *lookupWeb {
+ t.Helper()
+ key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+ tmpl := &x509.Certificate{SerialNumber: big.NewInt(3), Subject: pkix.Name{CommonName: "web"}, DNSNames: []string{"www.met.ie", "www.museum.ie"}, NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour),
+ KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, IsCA: true}
+ der, _ := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
+ cert, _ := x509.ParseCertificate(der)
+ w := &lookupWeb{roots: x509.NewCertPool()}
+ w.roots.AddCert(cert)
+ w.srv = httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ w.mu.Lock()
+ w.got = append(w.got, r.Host+r.URL.Path)
+ w.mu.Unlock()
+ rw.Header().Set("Content-Type", "text/html")
+ _, _ = rw.Write([]byte("<html><title>" + r.Host + "</title><body><p>Rain at first, then sun. Highs of 11 degrees. Open 10:00 to 17:00.</p></body></html>"))
+ }))
+ w.srv.TLS = &tls.Config{Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}}}
+ w.srv.StartTLS()
+ t.Cleanup(w.srv.Close)
+ return w
+}
+
+// lookupRig is a team rig at protocol 36 whose Base draws answers, with
+// lookups configured on the instance.
+type lookupE2E struct {
+ *teamRig
+ search *websearchtest.Fake
+ web *lookupWeb
+ mu sync.Mutex
+ steps []wire.LookupStep
+}
+
+func newLookupE2E(t *testing.T, protocol int, draws bool) *lookupE2E {
+ t.Helper()
+ o := &lookupE2E{teamRig: newTeamRigAt(t, protocol), search: &websearchtest.Fake{Backend: "kagi", Results: map[string][]websearch.Result{
+ "dublin weather": {{Title: "Dublin forecast", URL: "https://www.met.ie/dublin"}},
+ "dublin weather tomorrow": {{Title: "Dublin tomorrow", URL: "https://www.met.ie/dublin/tomorrow"}},
+ "national museum dublin": {{Title: "National Museum", URL: "https://www.museum.ie/visit"}},
+ }}, web: startLookupWeb(t)}
+ if draws {
+ o.client.Capabilities = wire.CapLookupAnswers
+ }
+ if _, err := o.a.Snapshot(o.ctx); err != nil {
+ t.Fatal(err)
+ }
+ g := guard.Config{Inventory: confirmedNet{}, Resolve: func(context.Context, string) ([]netip.Addr, error) {
+ return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
+ }, TestDial: func(string) (string, *x509.CertPool) { return o.web.srv.Listener.Addr().String(), o.web.roots }}
+ pageCfg := g
+ pageCfg.Policy, pageCfg.Redirects = guard.PolicyLookupPage, guard.MaxRedirects
+ pages, err := guard.New(pageCfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ o.inst.Store.Chat().SetLookups(&chat.LookupConfig{Search: websearch.Chain{Primary: o.search, Usage: &websearchtest.MemUsage{Caps: map[string]int{"kagi": 100}, Month: "2026-11"}}, Pages: pages, Inventory: confirmedNet{}})
+ o.lookupAnswer = func(s wire.LookupStep) wire.LookupResult {
+ o.mu.Lock()
+ o.steps = append(o.steps, s)
+ o.mu.Unlock()
+ q := strings.ToLower(s.Pack.Question)
+ if s.Stage == wire.LookupStageQuery {
+ switch {
+ case strings.Contains(q, "museum"):
+ return wire.LookupResult{Queries: []string{"national museum dublin"}}
+ case strings.Contains(q, "tomorrow"):
+ return wire.LookupResult{Queries: []string{"dublin weather tomorrow"}}
+ }
+ return wire.LookupResult{Queries: []string{"dublin weather"}}
+ }
+ if strings.Contains(q, "museum") {
+ return wire.LookupResult{Answer: "It's open 10:00 to 17:00.", Used: []int{0}}
+ }
+ return wire.LookupResult{Answer: "Rain at first, then sun. Highs of 11 degrees.", Used: []int{0}}
+ }
+ return o
+}
+
+// look asks and runs web_lookup in the turn, ending it with a reply;
+// it returns the lookup's tool result and the reply as read now.
+func (o *lookupE2E) lookUp(words string) (wire.ToolResult, wire.Message) {
+ o.t.Helper()
+ o.ask(words)
+ res := o.use("wl-"+itoa(len(o.runner.Sent)), "web_lookup", map[string]any{})
+ o.reply(o.turn)
+ return res, o.replyOf(o.turn)
+}
+
+func (o *lookupE2E) replyOf(turn string) wire.Message {
+ o.t.Helper()
+ var reply wire.Message
+ o.wait("the reply", func(*Conversation) bool {
+ snap, err := o.a.Snapshot(o.ctx)
+ if err != nil {
+ o.t.Fatal(err)
+ }
+ for _, m := range snap.Messages {
+ if m.TurnID == turn && m.Role == wire.RoleAssistant {
+ reply = m
+ }
+ }
+ return reply.ID != ""
+ })
+ return reply
+}
+
+// "What's the weather in Dublin?": a forecast under the reply, with its
+// source, and the Assistant told only that it was shown.
+func TestWeatherDublinGetsForecast(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolLookup, true)
+ res, reply := o.lookUp("What's the weather in Dublin?")
+ if res.IsError || res.Code != "used" || !strings.Contains(res.Text, `"code":"shown"`) || strings.Contains(res.Text, "Rain") {
+ t.Fatalf("result %+v", res)
+ }
+ if len(reply.Lookups) != 1 || !strings.Contains(reply.Lookups[0].Answer, "11 degrees") || reply.Lookups[0].Sources[0].URL != "https://www.met.ie/dublin" {
+ t.Errorf("reply %+v", reply)
+ }
+ o.mu.Lock()
+ defer o.mu.Unlock()
+ if len(o.steps) != 2 || o.steps[0].Pack.City == "" || o.steps[0].Pack.Earlier != nil || len(o.steps[1].Pages) != 1 {
+ t.Errorf("steps %+v", o.steps)
+ }
+}
+
+// "And tomorrow?" after it: the query writer sees the earlier question.
+func TestAndTomorrowUsesEarlierQuestion(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolLookup, true)
+ o.lookUp("What's the weather in Dublin?")
+ _, reply := o.lookUp("And tomorrow?")
+ o.mu.Lock()
+ query := o.steps[2]
+ o.mu.Unlock()
+ if query.Stage != wire.LookupStageQuery || len(query.Pack.Earlier) != 1 || query.Pack.Earlier[0] != "What's the weather in Dublin?" {
+ t.Errorf("pack %+v", query.Pack)
+ }
+ if len(reply.Lookups) != 1 || reply.Lookups[0].Sources[0].URL != "https://www.met.ie/dublin/tomorrow" {
+ t.Errorf("reply %+v", reply.Lookups)
+ }
+}
+
+// With web off: refused in one line naming who can turn it on, and not
+// offered.
+func TestWebOffOneLineNamesOwner(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolLookup, true)
+ o.setBoard(wire.AbilityWeb)
+ o.ask("What's the weather in Dublin?")
+ res := o.use("wo", "web_lookup", map[string]any{})
+ if !res.IsError || res.Code != wire.CodeAbilityOff || !strings.Contains(res.Text, "Paul") {
+ t.Errorf("result %+v", res)
+ }
+ if look := o.tool("wo-look", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(look.Text, "web_lookup") {
+ t.Errorf("offered with web off: %s", look.Text)
+ }
+ if len(o.web.got) != 0 || o.search.Asked() != 0 {
+ t.Error("something went out")
+ }
+}
+
+// A museum's opening hours, then "put it in my calendar": the Assistant
+// never saw the answer, so it is told to ask for what it needs.
+func TestMuseumThenCalendarAsksForTime(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolLookup, true)
+ res, reply := o.lookUp("When is the National Museum in Dublin open?")
+ if len(reply.Lookups) != 1 || !strings.Contains(reply.Lookups[0].Answer, "10:00") {
+ t.Fatalf("reply %+v", reply)
+ }
+ if strings.Contains(res.Text, "10:00") || !strings.Contains(res.Text, "Tell me the day and time") {
+ t.Errorf("the Assistant's result %s", res.Text)
+ }
+}
+
+// A runner below 36: web_lookup isn't offered, and the reply is as today.
+func TestStep3WithOldRunner(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolChangeDiffs, true)
+ o.ask("What's the weather in Dublin?")
+ if look := o.tool("or-look", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(look.Text, "web_lookup") {
+ t.Errorf("offered at 35: %s", look.Text)
+ }
+ if res := o.use("or", "web_lookup", map[string]any{}); !res.IsError {
+ t.Errorf("ran at 35: %+v", res)
+ }
+}
+
+// A Base that can't draw answers: web_lookup isn't offered; an answer
+// already in the history reads as plain text for it.
+func TestStep3WithOldNative(t *testing.T) {
+ o := newLookupE2E(t, wire.ProtocolLookup, true)
+ o.lookUp("What's the weather in Dublin?")
+ o.client.Capabilities = ""
+ if _, err := o.a.Snapshot(o.ctx); err != nil {
+ t.Fatal(err)
+ }
+ reply := o.replyOf(o.turn)
+ if reply.Lookups != nil || !strings.Contains(reply.Text, "11 degrees") || !strings.Contains(reply.Text, "https://www.met.ie/dublin") {
+ t.Errorf("history for an older client %+v", reply)
+ }
+ o.ask("And tomorrow?")
+ if look := o.tool("on-look", wire.ToolLook, map[string]any{"query": "weather"}); strings.Contains(look.Text, "web_lookup") {
+ t.Errorf("offered to a Base that can't draw it: %s", look.Text)
+ }
+}