Abilities: one switches() read, operator fixed keys, capabilities
switches reads a person's effective abilities in the acting transaction: Base's copy of the board for their verified host (missing, revoked or more than 10 minutes past confirmation denies everything switched, as unavailable), AND the operator's EQUIPO_ABILITIES (off, or owners only), AND apps_write needing apps_read and services_add needing services. Without EQUIPO_ASSISTANT_HOSTS the defaults apply and Equipo's owners administer. A known key Base doesn't send counts as its default, on. Every Assistant route first records Base-Capabilities (presentation only; Draws never makes Can true). An unreadable EQUIPO_ABILITIES refuses boot with an operator log line and the reviewed invalid copy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+532
−13
internal/equipo/abilities_config_test.go+31 −0internal/equipo/app.go+1 −0internal/equipo/chat/abilities_harness_test.go+22 −1internal/equipo/chat/assistant_http.go+30 −11internal/equipo/chat/store.go+2 −1internal/equipo/chat/switches.go+235 −0internal/equipo/chat/switches_test.go+196 −0internal/equipo/config.go+15 −0
diff --git a/internal/equipo/abilities_config_test.go b/internal/equipo/abilities_config_test.go| new file mode 100644 |
| index 00000000..eee3438b |
| --- /dev/null |
| +++ b/internal/equipo/abilities_config_test.go |
| @@ -0,0 +1,31 @@ |
| +package equipo |
| + |
| +import ( |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/equipo/chat" |
| +) |
| + |
| +// EQUIPO_ABILITIES fixes keys off, or for owners only, for everyone on |
| +// the instance; anything it can't read refuses boot. |
| +func TestAbilitiesConfig(t *testing.T) { |
| + load := func(value string) (Config, error) { |
| + env := map[string]string{"EQUIPO_ORIGIN": "https://equipo.bds.oncarlos.com", "EQUIPO_INSTANCE_KEY": "fixture", "CARLOS_MAIL": "submit", "EQUIPO_ABILITIES": value} |
| + return LoadConfig(func(k string) string { return env[k] }) |
| + } |
| + cfg, err := load("web=off,services_add=owners") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if cfg.Abilities["web"] != chat.FixedOff || cfg.Abilities["services_add"] != chat.FixedOwners || len(cfg.Abilities) != 2 { |
| + t.Errorf("fixed %v", cfg.Abilities) |
| + } |
| + if cfg, err := load(""); err != nil || len(cfg.Abilities) != 0 { |
| + t.Errorf("unset: %v %v", cfg.Abilities, err) |
| + } |
| + for _, bad := range []string{"weather=off", "web=maybe"} { |
| + if _, err := load(bad); err == nil { |
| + t.Errorf("%q booted", bad) |
| + } |
| + } |
| +} |
diff --git a/internal/equipo/app.go b/internal/equipo/app.go| index 6df0a879..6d74ae69 100644 |
| --- a/internal/equipo/app.go |
| +++ b/internal/equipo/app.go |
| @@ -195,6 +195,7 @@ func RouterWithStore(d *db.DB, cfg Config, mailer mail.Sender, logger *slog.Logg |
| // The push outbox is written only where a sender will drain it. |
| store.Chat().SetPush(cfg.Push != nil) |
| store.Chat().SetNotificationActions(cfg.NotificationActions) |
| + store.Chat().SetFixed(cfg.Abilities) |
| // What the Assistant's provider calls could not do, by call id only. |
| store.Chat().SetLogger(logger) |
| if cfg.OficinaConnectOrigin != "" { |
diff --git a/internal/equipo/chat/abilities_harness_test.go b/internal/equipo/chat/abilities_harness_test.go| index 3835e011..9dcb8018 100644 |
| --- a/internal/equipo/chat/abilities_harness_test.go |
| +++ b/internal/equipo/chat/abilities_harness_test.go |
| @@ -151,7 +151,14 @@ func newAbilitiesHarness(t *testing.T, hosts ...string) *abilitiesHarness { |
| h.mu.Unlock() |
| return nil |
| }, |
| - Copy: func(key string) string { return key }, |
| + Copy: func(key string) string { return key }, |
| + Me: func(_ *gorm.DB, address string) (wire.Me, error) { |
| + role := "member" |
| + if address == "owner@example.com" { |
| + role = "owner" |
| + } |
| + return wire.Me{Address: address, Role: role}, nil |
| + }, |
| AssistantHosts: lowered, |
| Seal: func(_ string, v any) (string, error) { |
| raw, err := json.Marshal(v) |
| @@ -222,3 +229,17 @@ func (h *abilitiesHarness) row(t *testing.T, person, origin string) abilitiesRec |
| } |
| return r |
| } |
| + |
| +// switchesOf reads the person's switches in a transaction of their own. |
| +func (h *abilitiesHarness) switchesOf(t *testing.T, person string) Switches { |
| + t.Helper() |
| + var sw Switches |
| + if err := h.s.tx(context.Background(), func(tx *txn) error { |
| + var err error |
| + sw, err = tx.switches(person) |
| + return err |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + return sw |
| +} |
diff --git a/internal/equipo/chat/assistant_http.go b/internal/equipo/chat/assistant_http.go| index b521f230..682c8d9b 100644 |
| --- a/internal/equipo/chat/assistant_http.go |
| +++ b/internal/equipo/chat/assistant_http.go |
| @@ -20,7 +20,7 @@ import ( |
| // host says where the person's Base operations live. |
| func (h *HTTP) MountAssistant(r chi.Router) { |
| lane := h.Store.Lane(wire.LaneAssistant) |
| - r.Get("/snapshot", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Get("/snapshot", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.oficinaCatchUp(r, p) |
| h.warmAssistant(r, p) |
| out, err := lane.Snapshot(r.Context(), p.Address) |
| @@ -35,19 +35,19 @@ func (h *HTTP) MountAssistant(r chi.Router) { |
| } |
| writeJSON(w, 200, out) |
| })) |
| - r.Get("/messages", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Get("/messages", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.messagesWith(w, r, p, lane.Messages) |
| })) |
| - r.Post("/messages", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Post("/messages", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.sendWith(w, r, p, lane.Send) |
| })) |
| - r.Post("/turns/{id}/cancel", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Post("/turns/{id}/cancel", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.cancelWith(w, r, p, lane.Cancel) |
| })) |
| - r.Post("/cards/{id}/answer", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Post("/cards/{id}/answer", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.assistantAnswer(w, r, p, lane) |
| })) |
| - r.Get("/events", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Get("/events", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.oficinaCatchUp(r, p) |
| h.warmAssistant(r, p) |
| read := lane.Events |
| @@ -56,18 +56,37 @@ func (h *HTTP) MountAssistant(r chi.Router) { |
| } |
| h.stream(w, r, p, lane.lane, read) |
| })) |
| - r.Put("/host", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Put("/host", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.assistantHost(w, r, p, lane) |
| })) |
| - r.Post("/cards/{id}/carry_on", h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + r.Post("/cards/{id}/carry_on", h.assistantBearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| h.carryOn(w, r, p) |
| })) |
| - r.Put("/time-zone", h.bearer(h.setAssistantZone)) |
| + r.Put("/time-zone", h.assistantBearer(h.setAssistantZone)) |
| if h.OficinaConnect != nil { |
| - r.Post("/oficina/connect", h.bearer(h.oficinaConnect)) |
| + r.Post("/oficina/connect", h.assistantBearer(h.oficinaConnect)) |
| } |
| if h.OficinaFinish != nil { |
| - r.Post("/oficina/connect/finish", h.bearer(h.oficinaFinish)) |
| + r.Post("/oficina/connect/finish", h.assistantBearer(h.oficinaFinish)) |
| + } |
| +} |
| + |
| +// assistantBearer is bearer for the Assistant's routes: before anything |
| +// else, it records the Base-Capabilities the person's Base sent |
| +// (noteCapabilities). |
| +func (h *HTTP) assistantBearer(f func(http.ResponseWriter, *http.Request, personapi.PersonPrincipal)) http.HandlerFunc { |
| + return h.bearer(func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| + h.noteCapabilities(r, p) |
| + f(w, r, p) |
| + }) |
| +} |
| + |
| +// noteCapabilities records what the person's Base says it draws |
| +// (Base-Capabilities), presentation only. The request never waits on it |
| +// failing. |
| +func (h *HTTP) noteCapabilities(r *http.Request, p personapi.PersonPrincipal) { |
| + if err := h.Store.SetCapabilities(r.Context(), p.Address, r.Header.Get("Base-Capabilities")); err != nil && h.Logger != nil { |
| + h.Logger.Warn("assistant capabilities", "err", err) |
| } |
| } |
diff --git a/internal/equipo/chat/store.go b/internal/equipo/chat/store.go| index 423382f4..33ed71cf 100644 |
| --- a/internal/equipo/chat/store.go |
| +++ b/internal/equipo/chat/store.go |
| @@ -127,8 +127,9 @@ type Store struct { |
| // test's failing card); nil projects as usual. |
| projectJobHook func(*txn, handoffRecord) error |
| // abilities is what the store keeps in memory about people's boards |
| - // (abilities_fetch.go). |
| + // (abilities_fetch.go); fixed the operator's fixed keys (SetFixed). |
| abilities abilityState |
| + fixed atomic.Pointer[Fixed] |
| } |
| // SetFrameTap sees every frame the instance sends a runner, as sent |
diff --git a/internal/equipo/chat/switches.go b/internal/equipo/chat/switches.go| new file mode 100644 |
| index 00000000..663d0700 |
| --- /dev/null |
| +++ b/internal/equipo/chat/switches.go |
| @@ -0,0 +1,235 @@ |
| +package chat |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "slices" |
| + "strings" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// One function decides what a person's Assistant may do (spec |
| +// 2026-11-26-assistant-abilities §5.5): switches, read in the transaction |
| +// that acts. Can comes from Base over the host channel and decides what |
| +// may happen; Draws comes from Base-Capabilities, an unauthenticated |
| +// header, and only decides presentation. |
| + |
| +// abilitiesExpiry is how long a copy of the board counts after the last |
| +// answer that counted (§5.3). |
| +const abilitiesExpiry = 10 * time.Minute |
| + |
| +// Switches is a person's effective abilities: Base's copy AND the |
| +// operator's fixed keys AND the implications (§3). A missing, revoked or |
| +// expired copy denies everything switched. |
| +type Switches struct { |
| + abilities map[string]bool |
| + admin bool |
| + ask string |
| + caps []string |
| + // known: there is a copy that counts (else every refusal is |
| + // unavailable, not ability_off). |
| + known bool |
| +} |
| + |
| +// Can reports whether the person may use ability. |
| +func (s Switches) Can(ability string) bool { return s.abilities[ability] } |
| + |
| +// Admin reports whether the person administers their Base (the owner): |
| +// what MCP's review gate reads. |
| +func (s Switches) Admin() bool { return s.admin } |
| + |
| +// Draws reports whether the person's Base, and every client they use, |
| +// draws capability: presentation only. |
| +func (s Switches) Draws(capability string) bool { return slices.Contains(s.caps, capability) } |
| + |
| +// Off are the keys that are off, in §3's order. |
| +func (s Switches) Off() []string { |
| + var out []string |
| + for _, k := range wire.Abilities { |
| + if !s.abilities[k] { |
| + out = append(out, k) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// Ask is who can change the person's switches (Base's owner's name). |
| +func (s Switches) Ask() string { return s.ask } |
| + |
| +// Refusal is the code a refusal under ability gives: ability_off when |
| +// the switch is off, unavailable when there is no copy that counts. |
| +func (s Switches) Refusal(ability string) string { |
| + if !s.known { |
| + return "unavailable" |
| + } |
| + return wire.CodeAbilityOff |
| +} |
| + |
| +// Fixed is the operator's narrowing for everyone (EQUIPO_ABILITIES, |
| +// §5.4): a key off, or only for people who administer their Base. |
| +type Fixed map[string]string |
| + |
| +// The values of a fixed key. |
| +const ( |
| + FixedOff = "off" |
| + FixedOwners = "owners" |
| +) |
| + |
| +// ParseFixed reads EQUIPO_ABILITIES: comma-separated key=off or |
| +// key=owners entries. An unknown key or value, or a key given twice, is |
| +// an error. |
| +func ParseFixed(raw string) (Fixed, error) { |
| + out := Fixed{} |
| + for _, entry := range strings.Split(raw, ",") { |
| + entry = strings.TrimSpace(entry) |
| + if entry == "" { |
| + continue |
| + } |
| + key, value, ok := strings.Cut(entry, "=") |
| + key, value = strings.TrimSpace(key), strings.TrimSpace(value) |
| + switch { |
| + case !ok || !wire.KnownAbility(key): |
| + return nil, fmt.Errorf("EQUIPO_ABILITIES: unknown ability in %q", entry) |
| + case value != FixedOff && value != FixedOwners: |
| + return nil, fmt.Errorf("EQUIPO_ABILITIES: %q must be off or owners", entry) |
| + case out[key] != "": |
| + return nil, fmt.Errorf("EQUIPO_ABILITIES: %s given twice", key) |
| + } |
| + out[key] = value |
| + } |
| + return out, nil |
| +} |
| + |
| +// SetFixed sets the operator's fixed keys. |
| +func (s *Store) SetFixed(f Fixed) { |
| + c := Fixed{} |
| + for k, v := range f { |
| + c[k] = v |
| + } |
| + s.fixed.Store(&c) |
| +} |
| + |
| +// Fixed is the operator's fixed keys. |
| +func (s *Store) Fixed() Fixed { |
| + if p := s.fixed.Load(); p != nil { |
| + return *p |
| + } |
| + return Fixed{} |
| +} |
| + |
| +// switches reads the person's switches in the caller's transaction: their |
| +// host and its stored board, the operator's fixed keys and the newest |
| +// capabilities. A read error denies, as everything off. |
| +func (t *txn) switches(person string) (Switches, error) { |
| + var h assistantHostRecord |
| + if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| + return Switches{}, err |
| + } |
| + sw := Switches{abilities: map[string]bool{}, caps: splitCaps(h.Capabilities)} |
| + if len(t.s.authority.AssistantHosts) == 0 { |
| + // A test instance: the defaults, and Equipo's owners administer. |
| + for _, k := range wire.Abilities { |
| + sw.abilities[k] = true |
| + } |
| + sw.known = true |
| + if t.s.authority.Me != nil { |
| + me, err := t.s.authority.Me(t.db, person) |
| + if err != nil && !errors.Is(err, ErrNotFound) && !errors.Is(err, ErrForbidden) { |
| + return Switches{}, err |
| + } |
| + sw.admin = err == nil && me.Role == "owner" |
| + } |
| + } else if h.Person != "" && slices.Contains(t.s.authority.AssistantHosts, h.Origin) { |
| + var r abilitiesRecord |
| + if err := t.db.Where("person = ? AND origin = ?", person, h.Origin).Limit(1).Find(&r).Error; err != nil { |
| + return Switches{}, err |
| + } |
| + if r.Person != "" && r.RevokedAt == nil && r.ConfirmedAt != nil && !t.now().After(r.ConfirmedAt.Add(abilitiesExpiry)) { |
| + b, err := r.board() |
| + if err != nil { |
| + return Switches{}, err |
| + } |
| + for _, k := range wire.Abilities { |
| + // A key Base doesn't send is one it doesn't know yet: its |
| + // default, on, as Base itself would have it. |
| + on, sent := b.Abilities[k] |
| + sw.abilities[k] = on || !sent |
| + } |
| + sw.admin, sw.ask, sw.known = b.Admin, b.Ask, true |
| + } |
| + } |
| + for k, v := range t.s.Fixed() { |
| + if v == FixedOff || v == FixedOwners && !sw.admin { |
| + sw.abilities[k] = false |
| + } |
| + } |
| + if !sw.abilities[wire.AbilityAppsRead] { |
| + sw.abilities[wire.AbilityAppsWrite] = false |
| + } |
| + if !sw.abilities[wire.AbilityServices] { |
| + sw.abilities[wire.AbilityServicesAdd] = false |
| + } |
| + return sw, nil |
| +} |
| + |
| +// splitCaps is a Base-Capabilities value's known capabilities, in order, |
| +// each once. |
| +func splitCaps(raw string) []string { |
| + var out []string |
| + for _, c := range strings.Split(raw, ",") { |
| + c = strings.TrimSpace(c) |
| + if (c == wire.CapJobActions || c == wire.CapLookupAnswers) && !slices.Contains(out, c) { |
| + out = append(out, c) |
| + } |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |
| + |
| +// SetCapabilities records the newest Base-Capabilities value the |
| +// person's Base sent (its known capabilities), writing only on change. A |
| +// person who never registered a host gets a row of their own for it, |
| +// which names no host. |
| +func (s *Store) SetCapabilities(ctx context.Context, person, value string) error { |
| + caps := strings.Join(splitCaps(value), ",") |
| + var have []string |
| + 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 { |
| + var h assistantHostRecord |
| + if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| + return err |
| + } |
| + if h.Person == "" { |
| + return t.db.Create(&assistantHostRecord{Person: person, Capabilities: caps, UpdatedAt: t.now()}).Error |
| + } |
| + if h.Capabilities == caps { |
| + return nil |
| + } |
| + return t.db.Model(&assistantHostRecord{}).Where("person = ?", person).Update("capabilities", caps).Error |
| + }) |
| +} |
| + |
| +// ownAbilities is GET /api/v1/assistant/abilities's answer for the |
| +// person: every key, the operator's fixed keys and who to ask. |
| +func (t *txn) ownAbilities(person string) (wire.AssistantAbilities, error) { |
| + sw, err := t.switches(person) |
| + if err != nil { |
| + return wire.AssistantAbilities{}, err |
| + } |
| + out := wire.AssistantAbilities{Abilities: map[string]bool{}, Fixed: []string{}, Ask: sw.ask} |
| + for _, k := range wire.Abilities { |
| + out.Abilities[k] = sw.Can(k) |
| + } |
| + fixed := t.s.Fixed() |
| + for _, k := range wire.Abilities { |
| + if fixed[k] != "" { |
| + out.Fixed = append(out.Fixed, k) |
| + } |
| + } |
| + return out, nil |
| +} |
diff --git a/internal/equipo/chat/switches_test.go b/internal/equipo/chat/switches_test.go| new file mode 100644 |
| index 00000000..675d318e |
| --- /dev/null |
| +++ b/internal/equipo/chat/switches_test.go |
| @@ -0,0 +1,196 @@ |
| +package chat |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +func allOn(t *testing.T, sw Switches, want bool) { |
| + t.Helper() |
| + for _, k := range wire.Abilities { |
| + if sw.Can(k) != want { |
| + t.Errorf("%s: Can %v, want %v", k, sw.Can(k), want) |
| + } |
| + } |
| +} |
| + |
| +// With no EQUIPO_ASSISTANT_HOSTS (a test instance), every key is on and |
| +// an Equipo owner administers. |
| +func TestSwitchesDefaultsWithoutHosts(t *testing.T) { |
| + h := newAbilitiesHarness(t) |
| + allOn(t, h.switchesOf(t, "owner@example.com"), true) |
| + if !h.switchesOf(t, "owner@example.com").Admin() || h.switchesOf(t, abPerson).Admin() { |
| + t.Error("admin is not the Equipo owner") |
| + } |
| + if off := h.switchesOf(t, abPerson).Off(); len(off) != 0 { |
| + t.Errorf("off %v", off) |
| + } |
| +} |
| + |
| +// With EQUIPO_ASSISTANT_HOSTS set, a person with no verified Base host |
| +// has everything switched off, as unavailable. |
| +func TestNoVerifiedHostNothingSwitched(t *testing.T) { |
| + base := startBoardBase(t, "host-token-0123456789abcdef") |
| + h := newAbilitiesHarness(t, base.origin()) |
| + sw := h.switchesOf(t, abPerson) |
| + allOn(t, sw, false) |
| + if code := sw.Refusal(wire.AbilityWeb); code != "unavailable" { |
| + t.Errorf("refusal %q", code) |
| + } |
| + // A host registered but never verified (no board) is the same. |
| + h.register(t, abPerson, base.origin(), base.token) |
| + allOn(t, h.switchesOf(t, abPerson), false) |
| + if _, _, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil { |
| + t.Fatal(err) |
| + } |
| + allOn(t, h.switchesOf(t, abPerson), true) |
| +} |
| + |
| +// Base unreachable for more than 10 minutes past the last answer that |
| +// counted: everything switched is refused until a fetch succeeds. |
| +func TestAbilitiesExpireAfter10Minutes(t *testing.T) { |
| + h, base := boardBase(t) |
| + refresh(t, h) |
| + h.advance(10 * time.Minute) |
| + allOn(t, h.switchesOf(t, abPerson), true) |
| + base.set(func(f *fakeBoardBase) { f.status = 503 }) |
| + refresh(t, h) |
| + h.advance(time.Second) |
| + sw := h.switchesOf(t, abPerson) |
| + allOn(t, sw, false) |
| + if code := sw.Refusal(wire.AbilityBuild); code != "unavailable" { |
| + t.Errorf("refusal %q", code) |
| + } |
| + base.set(func(f *fakeBoardBase) { f.status = 200 }) |
| + refresh(t, h) |
| + allOn(t, h.switchesOf(t, abPerson), true) |
| +} |
| + |
| +// The operator's fixed keys narrow for everyone: web=off for the admin |
| +// too, services_add=owners for the admin only. |
| +func TestAbilitiesOperatorFixed(t *testing.T) { |
| + h, base := boardBase(t) |
| + fixed, err := ParseFixed("web=off, services_add=owners") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + h.s.SetFixed(fixed) |
| + refresh(t, h) |
| + sw := h.switchesOf(t, abPerson) |
| + if sw.Can(wire.AbilityWeb) || sw.Can(wire.AbilityServicesAdd) || !sw.Can(wire.AbilityServices) || sw.Refusal(wire.AbilityWeb) != wire.CodeAbilityOff { |
| + t.Errorf("member: web %v services_add %v", sw.Can(wire.AbilityWeb), sw.Can(wire.AbilityServicesAdd)) |
| + } |
| + base.set(func(f *fakeBoardBase) { f.board.Admin, f.board.Revision = true, 2 }) |
| + refresh(t, h) |
| + if sw := h.switchesOf(t, abPerson); sw.Can(wire.AbilityWeb) || !sw.Can(wire.AbilityServicesAdd) { |
| + t.Errorf("admin: web %v services_add %v", sw.Can(wire.AbilityWeb), sw.Can(wire.AbilityServicesAdd)) |
| + } |
| + for _, bad := range []string{"web=on", "future=off", "web", "web=off,web=owners", "=off"} { |
| + if _, err := ParseFixed(bad); err == nil { |
| + t.Errorf("%q accepted", bad) |
| + } |
| + } |
| + if f, err := ParseFixed(""); err != nil || len(f) != 0 { |
| + t.Errorf("empty: %v %v", f, err) |
| + } |
| +} |
| + |
| +// apps_write needs apps_read, and services_add needs services. |
| +func TestAppsWriteImpliesRead(t *testing.T) { |
| + h, base := boardBase(t) |
| + base.set(func(f *fakeBoardBase) { f.board.Abilities["apps_read"], f.board.Abilities["services"] = false, false }) |
| + refresh(t, h) |
| + sw := h.switchesOf(t, abPerson) |
| + if sw.Can(wire.AbilityAppsWrite) || sw.Can(wire.AbilityServicesAdd) || !sw.Can(wire.AbilityBuild) { |
| + t.Errorf("apps_write %v services_add %v", sw.Can(wire.AbilityAppsWrite), sw.Can(wire.AbilityServicesAdd)) |
| + } |
| + if got := sw.Off(); len(got) != 4 || got[0] != wire.AbilityAppsRead || got[1] != wire.AbilityAppsWrite { |
| + t.Errorf("off %v", got) |
| + } |
| +} |
| + |
| +// Switches are read in the caller's transaction: a board update made |
| +// meanwhile is not seen until it commits after; and a read error denies. |
| +func TestSwitchesReadInsideTx(t *testing.T) { |
| + h, base := boardBase(t) |
| + refresh(t, h) |
| + updated := make(chan error, 1) |
| + err := h.s.tx(context.Background(), func(tx *txn) error { |
| + first, err := tx.switches(abPerson) |
| + if err != nil { |
| + return err |
| + } |
| + go func() { |
| + base.set(func(f *fakeBoardBase) { f.board.Abilities["build"], f.board.Revision = false, 2 }) |
| + _, _, err := h.s.RefreshAbilities(context.Background(), abPerson) |
| + updated <- err |
| + }() |
| + time.Sleep(100 * time.Millisecond) |
| + second, err := tx.switches(abPerson) |
| + if err != nil { |
| + return err |
| + } |
| + if first.Can(wire.AbilityBuild) != second.Can(wire.AbilityBuild) || !second.Can(wire.AbilityBuild) { |
| + t.Errorf("the update showed inside the transaction") |
| + } |
| + return nil |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := <-updated; err != nil { |
| + t.Fatal(err) |
| + } |
| + if h.switchesOf(t, abPerson).Can(wire.AbilityBuild) { |
| + t.Error("the update didn't apply after") |
| + } |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + cancel() |
| + tx := &txn{s: h.s, ctx: ctx, db: h.s.db.G.WithContext(ctx)} |
| + sw, err := tx.switches(abPerson) |
| + if err == nil || sw.Can(wire.AbilityWeb) { |
| + t.Errorf("a read error: %v, web %v", err, sw.Can(wire.AbilityWeb)) |
| + } |
| + if !errors.Is(err, context.Canceled) { |
| + t.Logf("read error %v", err) |
| + } |
| +} |
| + |
| +// Draws comes from Base-Capabilities and only decides presentation: it |
| +// never makes Can true. |
| +func TestCapableButNotAllowed(t *testing.T) { |
| + h, base := boardBase(t) |
| + base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false }) |
| + refresh(t, h) |
| + if err := h.s.SetCapabilities(context.Background(), abPerson, "job_actions,lookup_answers,future"); err != nil { |
| + t.Fatal(err) |
| + } |
| + sw := h.switchesOf(t, abPerson) |
| + if !sw.Draws(wire.CapJobActions) || !sw.Draws(wire.CapLookupAnswers) || sw.Draws("future") || sw.Can(wire.AbilityBuild) { |
| + t.Errorf("draws %v can %v", sw.Draws(wire.CapJobActions), sw.Can(wire.AbilityBuild)) |
| + } |
| +} |
| + |
| +// Capabilities are recorded for a person who never registered a host. |
| +func TestCapabilityRecordedWithoutPutHost(t *testing.T) { |
| + h := newAbilitiesHarness(t, "https://base.example.com") |
| + if err := h.s.SetCapabilities(context.Background(), abPerson, wire.CapLookupAnswers); err != nil { |
| + t.Fatal(err) |
| + } |
| + if sw := h.switchesOf(t, abPerson); !sw.Draws(wire.CapLookupAnswers) || sw.Can(wire.AbilityWeb) { |
| + t.Errorf("draws %v can %v", sw.Draws(wire.CapLookupAnswers), sw.Can(wire.AbilityWeb)) |
| + } |
| + if _, _, ok, err := h.s.Host(context.Background(), abPerson); ok || err != nil { |
| + t.Errorf("a host appeared: %v %v", ok, err) |
| + } |
| + if err := h.s.SetCapabilities(context.Background(), abPerson, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if h.switchesOf(t, abPerson).Draws(wire.CapLookupAnswers) { |
| + t.Error("an empty header kept the capability") |
| + } |
| +} |
diff --git a/internal/equipo/config.go b/internal/equipo/config.go| index 44a01b1e..6e684d70 100644 |
| --- a/internal/equipo/config.go |
| +++ b/internal/equipo/config.go |
| @@ -1,10 +1,12 @@ |
| package equipo |
| import ( |
| + "amadan.net/oficina/equipo/internal/equipo/chat" |
| "amadan.net/oficina/equipo/internal/equipo/oficina" |
| "amadan.net/oficina/equipo/internal/equipo/push" |
| "amadan.net/oficina/home/connect" |
| "errors" |
| + "log/slog" |
| "net" |
| "net/http" |
| "net/url" |
| @@ -58,6 +60,10 @@ type Config struct { |
| // Oficina apps. |
| OficinaConnectOrigin string |
| OficinaConnectClientID string |
| + // Abilities are the operator's fixed abilities (EQUIPO_ABILITIES, |
| + // key=off or key=owners, comma-separated): they narrow for everyone |
| + // on the instance (spec 2026-11-26-assistant-abilities §5.4). |
| + Abilities chat.Fixed |
| } |
| func LoadConfig(getenv func(string) string) (Config, error) { |
| @@ -89,6 +95,15 @@ func LoadConfig(getenv func(string) string) (Config, error) { |
| cfg.AssistantHosts = append(cfg.AssistantHosts, origin) |
| } |
| } |
| + fixed, err := chat.ParseFixed(getenv("EQUIPO_ABILITIES")) |
| + if err != nil { |
| + // The operator's line says what is wrong; the error is the |
| + // reviewed one for a value that can't be read. |
| + slog.Default().Error("config", "err", err) |
| + //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| + return Config{}, errors.New(copyInvalid) |
| + } |
| + cfg.Abilities = fixed |
| if cfg.OficinaConnectOrigin = strings.TrimSpace(getenv("EQUIPO_OFICINA_CONNECT_ORIGIN")); cfg.OficinaConnectOrigin != "" { |
| cfg.OficinaConnectClientID = strings.TrimSpace(getenv("EQUIPO_OFICINA_CONNECT_CLIENT_ID")) |
| } |