oficina / equipo Public

Abilities: Oficina connect needs apps_read

startOficina and finishOficina are the only callers of the Oficina
client's Start and Finish (a source scan pins it), and each first checks
the person may read their apps, refreshing a stale board. The person
API's connect and finish answer 403 ability_off; Equipo's own start and
done pages say "Reading your Oficina apps is turned off for you. <Owner>
can turn it on." (approved copy), or the reviewed forbidden page when
Base gave no name. No connect card is raised without apps_read.
Disconnect is never checked.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev ffe6cb9b1bec972217654dc5f0a2e781dcc74002 parent 2f05b27
7 files changed, +229 −6
  • internal/equipo/chat/http.go +2 −0
  • internal/equipo/chat/store.go +3 −0
  • internal/equipo/chat_wiring.go +4 −0
  • internal/equipo/handlers.go +2 −0
  • internal/equipo/oficina_connect.go +75 −5
  • internal/equipo/templates/empty.html +1 −1
  • internal/thintest/oficina_abilities_contract_test.go +142 −0
diff --git a/internal/equipo/chat/http.go b/internal/equipo/chat/http.go
index b77545dc..4bf07e83 100644
--- a/internal/equipo/chat/http.go
+++ b/internal/equipo/chat/http.go
@@ -399,6 +399,8 @@ func (h *HTTP) fail(w http.ResponseWriter, r *http.Request, err error) {
personapi.WriteError(w, r, 422, "repo_not_allowed", nil)
case errors.Is(err, ErrForbidden):
personapi.WriteError(w, r, 403, "forbidden", nil)
+ case errors.Is(err, ErrAbilityOff):
+ personapi.WriteError(w, r, 403, wire.CodeAbilityOff, nil)
case errors.Is(err, ErrBusy):
personapi.WriteError(w, r, 409, "coordinator_busy", nil)
case errors.Is(err, ErrTaskEnded):
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go
index 24a86650..f9585718 100644
--- a/internal/equipo/chat/store.go
+++ b/internal/equipo/chat/store.go
@@ -31,6 +31,9 @@ var (
ErrConflict = errors.New("chat: idempotency conflict")
ErrTerminal = errors.New("chat: turn already ended")
ErrForbidden = errors.New("chat: forbidden")
+ // ErrAbilityOff: an ability the person needs is off (403 ability_off,
+ // spec 2026-11-26-assistant-abilities).
+ ErrAbilityOff = errors.New("chat: ability off")
ErrExpired = errors.New("chat: cursor expired")
ErrSuperseded = errors.New("chat: runner generation superseded")
ErrUnavailable = errors.New("chat: unavailable")
diff --git a/internal/equipo/chat_wiring.go b/internal/equipo/chat_wiring.go
index 6931298c..2bfac227 100644
--- a/internal/equipo/chat_wiring.go
+++ b/internal/equipo/chat_wiring.go
@@ -98,6 +98,10 @@ func (s *Store) assistRegistry(logger *slog.Logger) *assist.Registry {
if provider != oficinaProvider {
return false
}
+ // No connect card for a person who may not read their apps.
+ if err := c.Allowed(ctx, person, wire.AbilityAppsRead); err != nil {
+ return false
+ }
open, err := c.RaiseConnect(ctx, person, chat.ConnectCard{Provider: oficinaProvider, Question: copyText("assistant.connect.oficina"), Link: s.oficinaLink(ctx, person)})
if err != nil {
logger.Warn(copyOficinaCardFailed, "err", err)
diff --git a/internal/equipo/handlers.go b/internal/equipo/handlers.go
index 07dc1630..ff3a939d 100644
--- a/internal/equipo/handlers.go
+++ b/internal/equipo/handlers.go
@@ -87,6 +87,8 @@ type pageData struct {
Enrolment Runner
Nav []NavItem
Key, Title, EmptyKey, Address, TeamName string
+ // EmptyArg fills the %s in EmptyKey's copy, when it has one.
+ EmptyArg string
Owner bool
Groups []ProjectGroup
Count int
diff --git a/internal/equipo/oficina_connect.go b/internal/equipo/oficina_connect.go
index a2d7a3d1..3077af4c 100644
--- a/internal/equipo/oficina_connect.go
+++ b/internal/equipo/oficina_connect.go
@@ -11,6 +11,7 @@ import (
"strings"
"time"
+ "amadan.net/oficina/equipo/internal/equipo/assist"
"amadan.net/oficina/equipo/internal/equipo/chat"
"amadan.net/oficina/equipo/internal/equipo/identity"
"amadan.net/oficina/equipo/internal/equipo/oficina"
@@ -147,13 +148,69 @@ func (s *Store) oficinaConnect(ctx context.Context, person, returnTo string) (st
if !s.oficinaReturn(returnTo) {
return "", chat.ErrInvalid
}
- dest, err := s.oficina.Start(ctx, normalizeAddress(person), returnTo)
+ dest, err := s.startOficina(ctx, normalizeAddress(person), returnTo)
if err != nil {
- return "", chat.ErrUnavailable
+ return "", connectRefusal(err)
}
return dest, nil
}
+// startOficina and finishOficina are the only ways a consent starts or
+// finishes, from Base or from Equipo's own pages: each first checks the
+// person may read their Oficina apps (spec 2026-11-26-assistant-abilities
+// §6.1), refreshing their board when stale. Disconnect isn't checked.
+func (s *Store) startOficina(ctx context.Context, person, returnTo string) (string, error) {
+ if err := s.Chat().Allowed(ctx, person, wire.AbilityAppsRead); err != nil {
+ return "", err
+ }
+ return s.oficina.Start(ctx, person, returnTo)
+}
+
+func (s *Store) finishOficina(ctx context.Context, person, state, code string) error {
+ if err := s.Chat().Allowed(ctx, person, wire.AbilityAppsRead); err != nil {
+ return err
+ }
+ return s.oficina.Finish(ctx, person, state, code)
+}
+
+// connectRefusal is a start or finish's error for the person API:
+// chat.ErrAbilityOff when reading apps is off for the person, else
+// unavailable.
+func connectRefusal(err error) error {
+ if abilityOff(err) {
+ return chat.ErrAbilityOff
+ }
+ return chat.ErrUnavailable
+}
+
+// abilityOff reports whether err is a refusal because an ability is off.
+func abilityOff(err error) bool {
+ var f assist.Failure
+ return errors.As(err, &f) && f.Code == wire.CodeAbilityOff
+}
+
+// oficinaOffPage is Equipo's page saying reading apps is off for the
+// person, and who can turn it on.
+func (a *application) oficinaOffPage(w http.ResponseWriter, r *http.Request, person string) {
+ ask := ""
+ if out, err := a.store.Chat().OwnAbilities(r.Context(), person); err == nil {
+ ask = out.Ask
+ }
+ if ask == "" {
+ // No name to give: the reviewed "ask an owner" page.
+ ErrorPage(w, r, http.StatusForbidden, "")
+ return
+ }
+ data, err := a.page(r, "settings")
+ if err != nil {
+ a.failure(w, r, err)
+ return
+ }
+ data.Title, data.EmptyKey, data.EmptyArg = "oficina.connect.title", "oficina.connect.ability_off", ask
+ w.Header().Set("Cache-Control", "no-store")
+ renderStatus(w, http.StatusForbidden, "empty", data)
+}
+
// oficinaReturn: an absolute https address (http only on loopback), with
// no user or fragment, on an origin in EQUIPO_ASSISTANT_HOSTS.
func (s *Store) oficinaReturn(raw string) bool {
@@ -180,7 +237,11 @@ func (a *application) oficinaStart(w http.ResponseWriter, r *http.Request) {
ErrorPage(w, r, http.StatusForbidden, "")
return
}
- dest, err := a.store.oficina.Start(r.Context(), normalizeAddress(id.Address), "")
+ dest, err := a.store.startOficina(r.Context(), normalizeAddress(id.Address), "")
+ if abilityOff(err) {
+ a.oficinaOffPage(w, r, normalizeAddress(id.Address))
+ return
+ }
if err != nil {
a.oficinaPage(w, r, http.StatusServiceUnavailable, oficinaResults["unavailable"])
return
@@ -197,7 +258,11 @@ func (s *Store) oficinaFinish(ctx context.Context, person, state, code string) (
if state == "" || code == "" || len(state) > 512 || len(code) > 512 {
return "", chat.ErrInvalid
}
- return s.oficinaFinished(ctx, normalizeAddress(person), s.oficina.Finish(ctx, normalizeAddress(person), state, code))
+ err := s.finishOficina(ctx, normalizeAddress(person), state, code)
+ if abilityOff(err) {
+ return "", chat.ErrAbilityOff
+ }
+ return s.oficinaFinished(ctx, normalizeAddress(person), err)
}
// oficinaFinished is a finished consent's status. Connected, the
@@ -315,8 +380,13 @@ func (a *application) oficinaDone(w http.ResponseWriter, r *http.Request) {
return
}
person := normalizeAddress(id.Address)
+ finished := a.store.finishOficina(r.Context(), person, state, q.Get("oficina_code"))
+ if abilityOff(finished) {
+ a.oficinaOffPage(w, r, person)
+ return
+ }
var err error
- if result, err = a.store.oficinaFinished(r.Context(), person, a.store.oficina.Finish(r.Context(), person, state, q.Get("oficina_code"))); err != nil {
+ if result, err = a.store.oficinaFinished(r.Context(), person, finished); err != nil {
ErrorPage(w, r, http.StatusForbidden, "")
return
}
diff --git a/internal/equipo/templates/empty.html b/internal/equipo/templates/empty.html
index 5f46639e..fc3ab7ed 100644
--- a/internal/equipo/templates/empty.html
+++ b/internal/equipo/templates/empty.html
@@ -1 +1 @@
-{{define "content"}}<div rst-stack><h1>{{copy .Title}}</h1><section equipo-note data-empty-state="{{.EmptyKey}}"><p>{{copy .EmptyKey}}</p></section>{{if eq .Key "connections"}}<p><a href="/conversation/settings/machines">{{copy "runner.title"}}</a></p>{{end}}</div>{{end}}
+{{define "content"}}<div rst-stack><h1>{{copy .Title}}</h1><section equipo-note data-empty-state="{{.EmptyKey}}"><p>{{if .EmptyArg}}{{printf (copy .EmptyKey) .EmptyArg}}{{else}}{{copy .EmptyKey}}{{end}}</p></section>{{if eq .Key "connections"}}<p><a href="/conversation/settings/machines">{{copy "runner.title"}}</a></p>{{end}}</div>{{end}}
diff --git a/internal/thintest/oficina_abilities_contract_test.go b/internal/thintest/oficina_abilities_contract_test.go
new file mode 100644
index 00000000..cb18b6f7
--- /dev/null
+++ b/internal/thintest/oficina_abilities_contract_test.go
@@ -0,0 +1,142 @@
+package thintest
+
+import (
+ "encoding/json"
+ "net/http"
+ "os"
+ "path/filepath"
+ "regexp"
+ "strings"
+ "testing"
+
+ "amadan.net/oficina/equipo/internal/wire"
+)
+
+// readingOff turns reading apps off for the owner at Base, and gives
+// Equipo the hint, as Base does on a change.
+func (o *oficinaRig) readingOff() {
+ o.t.Helper()
+ o.base.SetBoard(o.base.Token, 2, "Paul", wire.AbilityAppsRead)
+ status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "")
+ var out wire.AbilitiesRefreshed
+ if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != 2 {
+ o.t.Fatalf("hint: %d %s", status, body)
+ }
+}
+
+// With reading apps off, no Oficina connect card is raised, and the
+// person API's connect and finish answer 403 ability_off.
+func TestOficinaConnectNeedsAppsRead(t *testing.T) {
+ o := newOficinaRig(t)
+ o.readingOff()
+ o.begin("Put lunch in my calendar.")
+ o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"})
+ if cards := o.connectCards(); len(cards) != 0 {
+ t.Errorf("a connect card was raised: %+v", cards)
+ }
+ for path, body := range map[string]string{
+ "/api/v1/assistant/oficina/connect": `{"return_to":"` + o.base.Origin + `/ask"}`,
+ "/api/v1/assistant/oficina/connect/finish": `{"state":"s","code":"c"}`,
+ } {
+ status, _, out := raw(t, http.MethodPost, o.inst.URL+path, o.client.Token, nil, body)
+ var e wire.Error
+ if status != http.StatusForbidden || json.Unmarshal(out, &e) != nil || e.Code != wire.CodeAbilityOff {
+ t.Errorf("%s: %d %s", path, status, out)
+ }
+ }
+}
+
+// A connection made before reading went off is kept, unused: nothing is
+// listed or read through it, and disconnecting still works.
+func TestExistingOficinaConnectionKeptUnused(t *testing.T) {
+ o := newOficinaRig(t)
+ if status := o.viaBase(); status != "connected" {
+ t.Fatalf("connect: %s", status)
+ }
+ o.readingOff()
+ calls := len(o.fake.Calls())
+ o.begin("What's on my calendar?")
+ if res := o.tool("l1", wire.ToolLook, map[string]any{"query": "calendar"}); strings.Contains(res.Text, "calendar_") {
+ t.Errorf("look offers the apps: %s", res.Text)
+ }
+ if res := o.use("u1", "calendar_list", map[string]any{}); !res.IsError {
+ t.Errorf("use went through: %+v", res)
+ }
+ if n := o.rows("SELECT count(*) FROM oficina_connections"); n != 1 {
+ t.Errorf("%d connections kept", n)
+ }
+ if got := len(o.fake.Calls()); got != calls {
+ t.Errorf("%d requests to connect while reading was off", got-calls)
+ }
+ // Disconnect still revokes.
+ status, _, _ := o.browse(http.MethodPost, o.inst.URL+"/oficina/connect/disconnect")
+ if status != http.StatusOK || o.rows("SELECT count(*) FROM oficina_connections") != 0 {
+ t.Errorf("disconnect while off: %d, %d left", status, o.rows("SELECT count(*) FROM oficina_connections"))
+ }
+}
+
+// Connecting from Equipo's own page doesn't start a consent with reading
+// off: the page says so, naming who can turn it on.
+func TestOficinaBrowserStartNeedsAppsRead(t *testing.T) {
+ o := newOficinaRig(t)
+ o.readingOff()
+ status, location, body := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start")
+ if status != http.StatusForbidden || location != "" || !strings.Contains(body, "Reading your Oficina apps is turned off for you. Paul can turn it on.") {
+ t.Errorf("start: %d %q %.300s", status, location, body)
+ }
+}
+
+// A consent begun with reading on and finished after it went off, through
+// Base or through Equipo's done page, exchanges no code and saves no
+// connection.
+func TestOficinaConsentFinishedAfterRevoke(t *testing.T) {
+ o := newOficinaRig(t)
+ state, code := o.handBack(o.base.Origin + "/ask?open=1")
+ status, start, _ := o.browse(http.MethodGet, o.inst.URL+"/oficina/connect/start")
+ if status != http.StatusSeeOther {
+ t.Fatalf("browser start: %d", status)
+ }
+ _, callback, _ := o.browse(http.MethodGet, start)
+ _, done, _ := o.anonymous(callback)
+ o.readingOff()
+ if _, err := o.a.OficinaFinish(o.ctx, state, code); err == nil || !strings.Contains(err.Error(), "403") {
+ t.Errorf("finish through Base: %v", err)
+ }
+ if status, _, body := o.browse(http.MethodGet, o.inst.URL+done); status != http.StatusForbidden || !strings.Contains(body, "Paul can turn it on") {
+ t.Errorf("done page: %d %.200s", status, body)
+ }
+ if ex, _ := o.fake.Counts(); ex != 0 || o.rows("SELECT count(*) FROM oficina_connections") != 0 {
+ t.Errorf("%d exchanges, %d connections", ex, o.rows("SELECT count(*) FROM oficina_connections"))
+ }
+}
+
+// Only startOficina and finishOficina call the Oficina client's Start
+// and Finish: every way in is checked.
+func TestOnlyCheckedPathsCallOficina(t *testing.T) {
+ files, err := filepath.Glob("../equipo/*.go")
+ if err != nil || len(files) == 0 {
+ t.Fatal(files, err)
+ }
+ call := regexp.MustCompile(`\.oficina\.(Start|Finish)\(`)
+ funcStart := regexp.MustCompile(`(?m)^func (?:\([^)]*\) )?(\w+)`)
+ for _, f := range files {
+ if strings.HasSuffix(f, "_test.go") {
+ continue
+ }
+ src, err := os.ReadFile(f)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, m := range call.FindAllIndex(src, -1) {
+ starts := funcStart.FindAllSubmatchIndex(src[:m[0]], -1)
+ name := ""
+ if len(starts) > 0 {
+ last := starts[len(starts)-1]
+ name = string(src[last[2]:last[3]])
+ }
+ if name != "startOficina" && name != "finishOficina" {
+ t.Errorf("%s: %s calls %s", f, name, src[m[0]:m[1]])
+ }
+ }
+ }
+}