| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/assist" |
| 9 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | // When a person's board is fetched again (spec 2026-11-26-assistant- |
| 15 | // abilities §5.3): on Base's hint, at the start of a person turn, every |
| 16 | // minute for people with work under way, and before an act outside a |
| 17 | // turn, each when the copy is older than a minute. |
| 18 | |
| 19 | const ( |
| 20 | // abilitiesStale is how old a copy may be before an act fetches again. |
| 21 | abilitiesStale = 60 * time.Second |
| 22 | // hintThrottle is how soon after a fetch the hint fetches again. |
| 23 | hintThrottle = 5 * time.Second |
| 24 | ) |
| 25 | |
| 26 | // refreshIfStale fetches the person's board, outside any transaction and |
| 27 | // bounded as every fetch is, when their copy is older than a minute or |
| 28 | // missing. A person with no host, or an instance without |
| 29 | // EQUIPO_ASSISTANT_HOSTS, has nothing to fetch. |
| 30 | func (s *Store) refreshIfStale(ctx context.Context, person string) error { |
| 31 | if len(s.authority.AssistantHosts) == 0 { |
| 32 | return nil |
| 33 | } |
| 34 | var stale bool |
| 35 | err := s.read(ctx, func(tx *gorm.DB) error { |
| 36 | var h assistantHostRecord |
| 37 | if err := tx.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil || h.Origin == "" { |
| 38 | return err |
| 39 | } |
| 40 | var r abilitiesRecord |
| 41 | if err := tx.Where("person = ? AND origin = ?", person, h.Origin).Limit(1).Find(&r).Error; err != nil { |
| 42 | return err |
| 43 | } |
| 44 | stale = r.ConfirmedAt == nil || s.now().Sub(*r.ConfirmedAt) > abilitiesStale |
| 45 | return nil |
| 46 | }) |
| 47 | if err != nil || !stale { |
| 48 | return err |
| 49 | } |
| 50 | _, _, err = s.RefreshAbilities(ctx, person) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // AbilitiesHint is Base's hint that the person's switches changed: |
| 55 | // fetched within the call, unless a fetch ran less than hintThrottle ago. |
| 56 | // status is 200 with the revision Equipo now holds, or 202 when it |
| 57 | // couldn't fetch or didn't try. The hint carries no data. |
| 58 | func (s *Store) AbilitiesHint(ctx context.Context, person string) (revision int64, status int, err error) { |
| 59 | if s.fetchedWithin(person, hintThrottle) { |
| 60 | return 0, http.StatusAccepted, nil |
| 61 | } |
| 62 | revision, fresh, err := s.RefreshAbilities(ctx, person) |
| 63 | if err != nil || !fresh { |
| 64 | return 0, http.StatusAccepted, err |
| 65 | } |
| 66 | return revision, http.StatusOK, nil |
| 67 | } |
| 68 | |
| 69 | // RefreshActiveAbilities refreshes, when stale, the board of every person |
| 70 | // with a turn in flight or a job not finished: housekeeping's minute. A |
| 71 | // person whose fetch fails is logged and the rest go on. |
| 72 | func (s *Store) RefreshActiveAbilities(ctx context.Context) error { |
| 73 | if len(s.authority.AssistantHosts) == 0 { |
| 74 | return nil |
| 75 | } |
| 76 | var people []string |
| 77 | if err := s.read(ctx, func(tx *gorm.DB) error { |
| 78 | return tx.Raw(`SELECT person FROM chat_turns WHERE state IN ? UNION SELECT person FROM chat_handoffs WHERE state = ?`, wire.InFlight, handoffOpen).Scan(&people).Error |
| 79 | }); err != nil { |
| 80 | return err |
| 81 | } |
| 82 | for _, p := range people { |
| 83 | if ctx.Err() != nil { |
| 84 | return ctx.Err() |
| 85 | } |
| 86 | if err := s.refreshIfStale(ctx, p); err != nil { |
| 87 | s.logAbilities(p, err) |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | // refreshBeforeAct fetches the person's board, when stale, before an act |
| 94 | // outside a turn (Carry on now, a card's answer, a merge), outside its |
| 95 | // transaction. The act never waits on it failing: its checks read |
| 96 | // whatever copy there is, and an expired one denies. |
| 97 | func (s *Store) refreshBeforeAct(ctx context.Context, person string) { |
| 98 | if err := s.refreshIfStale(ctx, person); err != nil { |
| 99 | s.logAbilities(person, err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Allowed checks the person may use ability now, for an act outside a |
| 104 | // turn (connecting Oficina, Oficina's authenticated reads): a stale copy |
| 105 | // is fetched first, outside any transaction, then the switches are read |
| 106 | // in a short one. A refusal is an assist.Failure with ability_off, or |
| 107 | // unavailable when there is no copy that counts. |
| 108 | func (s *Store) Allowed(ctx context.Context, person, ability string) error { |
| 109 | if err := s.refreshIfStale(ctx, person); err != nil { |
| 110 | s.logAbilities(person, err) |
| 111 | } |
| 112 | var sw Switches |
| 113 | if err := s.read(ctx, func(tx *gorm.DB) error { |
| 114 | var err error |
| 115 | sw, err = (&txn{s: s, ctx: ctx, db: tx}).switches(person) |
| 116 | return err |
| 117 | }); err != nil { |
| 118 | return assist.Refusal{Code: "unavailable", Message: toolOpUnavailable} |
| 119 | } |
| 120 | if !sw.Can(ability) { |
| 121 | res := s.abilityRefusal(sw, ability) |
| 122 | return assist.Refusal{Code: res.Code, Message: res.Text} |
| 123 | } |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | // OwnAbilities is the person's own effective abilities (GET |
| 128 | // /api/v1/assistant/abilities). |
| 129 | func (s *Store) OwnAbilities(ctx context.Context, person string) (out wire.AssistantAbilities, err error) { |
| 130 | err = s.read(ctx, func(tx *gorm.DB) error { |
| 131 | var err error |
| 132 | out, err = (&txn{s: s, ctx: ctx, db: tx}).ownAbilities(person) |
| 133 | return err |
| 134 | }) |
| 135 | return out, err |
| 136 | } |
| 137 | |
| 138 | // ownAbilitiesRoute is GET abilities: the caller's own board, as Base's |
| 139 | // Settings line shows it. |
| 140 | func (h *HTTP) ownAbilitiesRoute(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 141 | if err := h.Store.refreshIfStale(r.Context(), p.Address); err != nil && h.Logger != nil { |
| 142 | h.Logger.Warn("abilities refresh", "err", err) |
| 143 | } |
| 144 | out, err := h.Store.OwnAbilities(r.Context(), p.Address) |
| 145 | if err != nil { |
| 146 | h.fail(w, r, err) |
| 147 | return |
| 148 | } |
| 149 | writeJSON(w, http.StatusOK, out) |
| 150 | } |
| 151 | |
| 152 | // abilitiesHint is POST abilities/refresh, with an empty body: 200 |
| 153 | // {revision} once fetched, 202 otherwise. |
| 154 | func (h *HTTP) abilitiesHint(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 155 | rev, status, err := h.Store.AbilitiesHint(r.Context(), p.Address) |
| 156 | if err != nil && h.Logger != nil { |
| 157 | h.Logger.Warn("abilities hint", "err", err) |
| 158 | } |
| 159 | if status != http.StatusOK { |
| 160 | w.WriteHeader(http.StatusAccepted) |
| 161 | return |
| 162 | } |
| 163 | writeJSON(w, http.StatusOK, wire.AbilitiesRefreshed{Revision: rev}) |
| 164 | } |
| 165 | |
| 166 | // turnStartRefresh fetches the person's board, when stale, before a |
| 167 | // request that starts a person turn. The request never waits on it |
| 168 | // failing: the turn's acts read whatever copy there is. |
| 169 | func (h *HTTP) turnStartRefresh(r *http.Request, p personapi.PersonPrincipal) { |
| 170 | if err := h.Store.refreshIfStale(r.Context(), p.Address); err != nil && h.Logger != nil { |
| 171 | h.Logger.Warn("abilities refresh", "err", err) |
| 172 | } |
| 173 | } |
| 174 | |