| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "amadan.net/oficina/equipo/internal/wire/forgekey" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | func (t *txn) askPlacement(r turnRecord, p wire.Placement) (wire.ToolResult, error) { |
| 15 | if repo, err := t.turnProject(r); err != nil { |
| 16 | return wire.ToolResult{}, err |
| 17 | } else if repo != "" { |
| 18 | return wire.ToolResult{Code: "found", Repo: repo, Text: fmt.Sprintf(t.s.authority.Copy("projects.selected"), repo)}, nil |
| 19 | } |
| 20 | if err := t.placementRunner(r.Person); err != nil { |
| 21 | return refused("runner_too_old", toolJobsTooOld), nil |
| 22 | } |
| 23 | if len(p.Repos) > 5 || len(p.Repos) == 0 && p.NewRepo == "" { |
| 24 | return refused("invalid", toolBadInput), nil |
| 25 | } |
| 26 | allowed, err := t.allowed(r.Person) |
| 27 | if err != nil { |
| 28 | return wire.ToolResult{}, err |
| 29 | } |
| 30 | data := placementData{Placement: p, Labels: []string{}} |
| 31 | for i, repo := range p.Repos { |
| 32 | repo = NormalizeRepo(repo) |
| 33 | if !slices.Contains(allowed, repo) || slices.Contains(data.Repos[:i], repo) { |
| 34 | return refused("invalid", toolBadInput), nil |
| 35 | } |
| 36 | data.Repos[i] = repo |
| 37 | label := fmt.Sprintf(t.s.authority.Copy("placement.add"), ProjectName(repo)) |
| 38 | if len(label) > wire.MaxOption { |
| 39 | return refused("invalid", toolBadInput), nil |
| 40 | } |
| 41 | data.Labels = append(data.Labels, label) |
| 42 | } |
| 43 | if p.NewRepo != "" { |
| 44 | data.NewRepo = NormalizeRepo(p.NewRepo) |
| 45 | if !forgekey.NewHubRepo(data.NewRepo) || slices.Contains(allowed, data.NewRepo) { |
| 46 | return refused("invalid", toolBadInput), nil |
| 47 | } |
| 48 | h, err := t.hello(r.Person) |
| 49 | if err != nil { |
| 50 | return wire.ToolResult{}, err |
| 51 | } |
| 52 | ns := strings.Split(ProjectName(data.NewRepo), "/")[0] |
| 53 | if !slices.Contains(h.CreateIn, ns) { |
| 54 | return refused("no_create", toolNoCreate), nil |
| 55 | } |
| 56 | data.NewLabel = fmt.Sprintf(t.s.authority.Copy("placement.new_named"), ProjectName(data.NewRepo)) |
| 57 | if len(data.NewLabel) > wire.MaxOption { |
| 58 | return refused("invalid", toolBadInput), nil |
| 59 | } |
| 60 | if data.NewFirst { |
| 61 | data.Labels = append([]string{data.NewLabel}, data.Labels...) |
| 62 | } else { |
| 63 | data.Labels = append(data.Labels, data.NewLabel) |
| 64 | } |
| 65 | } |
| 66 | raw, _ := json.Marshal(data) |
| 67 | options, _ := json.Marshal(data.Labels) |
| 68 | now := t.now() |
| 69 | c := cardRecord{ID: newID("c_"), Person: r.Person, Kind: wire.CardQuestion, State: wire.CardOpen, TurnID: r.ID, Question: t.s.authority.Copy("placement.question"), Options: string(options), Placement: string(raw), Revision: 1, CreatedAt: now, UpdatedAt: now, Lane: r.Lane} |
| 70 | if err := t.db.Create(&c).Error; err != nil { |
| 71 | return wire.ToolResult{}, err |
| 72 | } |
| 73 | if err := t.s.authority.Audit(t.db, r.Person, "placement.asked", c.ID); err != nil { |
| 74 | return wire.ToolResult{}, err |
| 75 | } |
| 76 | if err := t.cardEvent(c); err != nil { |
| 77 | return wire.ToolResult{}, err |
| 78 | } |
| 79 | return wire.ToolResult{Code: "asked", Text: toolAsked}, nil |
| 80 | } |
| 81 | |
| 82 | func (t *txn) placementRunner(person string) error { |
| 83 | id, err := t.s.authority.PrimaryRunner(t.db, person) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | var runner runnerSessionRecord |
| 88 | if err := t.db.First(&runner, "runner_id = ?", id).Error; err != nil { |
| 89 | return err |
| 90 | } |
| 91 | if runner.Protocol < wire.ProtocolPlacement { |
| 92 | return ErrConflict |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func (t *txn) validatePlacementAnswer(c cardRecord, choice string) error { |
| 98 | if c.Placement == "" { |
| 99 | return nil |
| 100 | } |
| 101 | if err := t.placementRunner(c.Person); err != nil { |
| 102 | return err |
| 103 | } |
| 104 | var p placementData |
| 105 | if json.Unmarshal([]byte(c.Placement), &p) != nil { |
| 106 | return ErrInvalid |
| 107 | } |
| 108 | allowed, err := t.allowed(c.Person) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | offset := 0 |
| 113 | if p.NewFirst && p.NewRepo != "" { |
| 114 | offset = 1 |
| 115 | } |
| 116 | for i, repo := range p.Repos { |
| 117 | i += offset |
| 118 | if i < len(p.Labels) && choice == p.Labels[i] { |
| 119 | if slices.Contains(allowed, repo) { |
| 120 | return nil |
| 121 | } |
| 122 | return ErrNotAllowed |
| 123 | } |
| 124 | } |
| 125 | if p.NewRepo != "" && choice == p.NewLabel { |
| 126 | h, err := t.hello(c.Person) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | if !slices.Contains(h.CreateIn, strings.Split(ProjectName(p.NewRepo), "/")[0]) { |
| 131 | return ErrNotAllowed |
| 132 | } |
| 133 | return nil |
| 134 | } |
| 135 | return ErrInvalid |
| 136 | } |
| 137 | |
| 138 | func (t *txn) placementPending(r turnRecord) (bool, error) { |
| 139 | var n int64 |
| 140 | err := t.db.Model(&cardRecord{}).Where("person = ? AND turn_id = ? AND state = ? AND coalesce(placement, '') <> ''", r.Person, r.ID, wire.CardOpen).Count(&n).Error |
| 141 | return n > 0, err |
| 142 | } |
| 143 | |
| 144 | // MoveChoices reads the destinations available to the creator, under the |
| 145 | // viewing owner's current authority. An owner never substitutes their runner. |
| 146 | func (s *Store) MoveChoices(ctx context.Context, person, id string) (out []string, err error) { |
| 147 | err = s.tx(ctx, func(t *txn) error { |
| 148 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | if err := t.isOwner(person); err != nil { |
| 152 | return err |
| 153 | } |
| 154 | k, err := t.loadTask(id) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if k.MoveProject != "" { |
| 159 | return nil |
| 160 | } |
| 161 | out, err = t.allowed(k.Creator) |
| 162 | return err |
| 163 | }) |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | // A card answer settles placement for its answer turn. The coordinator may |
| 168 | // refine the brief, but cannot substitute a different repository afterwards. |
| 169 | func (t *txn) placementWrite(r turnRecord, v *wire.ToolCall) (bool, error) { |
| 170 | if repo, err := t.turnProject(r); err != nil { |
| 171 | return false, err |
| 172 | } else if repo != "" { |
| 173 | return t.matchesProjectWrite(r, v, repo) |
| 174 | } |
| 175 | c, seq, found, err := t.answeredPlacement(r) |
| 176 | if err != nil || !found { |
| 177 | return !found, err |
| 178 | } |
| 179 | var p placementData |
| 180 | if err := json.Unmarshal([]byte(c.Placement), &p); err != nil { |
| 181 | return false, err |
| 182 | } |
| 183 | if p.MoveTask != "" { |
| 184 | return false, nil |
| 185 | } |
| 186 | repo, err := t.placementChoice(r, c, p, seq) |
| 187 | if err != nil { |
| 188 | return false, err |
| 189 | } |
| 190 | var in struct { |
| 191 | Repo string `json:"repo"` |
| 192 | Task string `json:"task"` |
| 193 | } |
| 194 | if json.Unmarshal(v.Args, &in) != nil { |
| 195 | return false, ErrInvalid |
| 196 | } |
| 197 | if v.Name == wire.ToolRecordTask { |
| 198 | if p, err := t.namedProject(r.Person, in.Repo); err != nil { |
| 199 | return false, err |
| 200 | } else if p != nil { |
| 201 | in.Repo = p.Repo |
| 202 | } |
| 203 | return NormalizeRepo(in.Repo) == repo, nil |
| 204 | } |
| 205 | k, err := t.loadTask(in.Task) |
| 206 | if errors.Is(err, ErrNotFound) { |
| 207 | return true, nil |
| 208 | } |
| 209 | if err != nil { |
| 210 | return false, err |
| 211 | } |
| 212 | var project projectRecord |
| 213 | if err := t.db.First(&project, "id = ?", k.ProjectID).Error; err != nil { |
| 214 | return false, err |
| 215 | } |
| 216 | return project.Repo == repo, nil |
| 217 | } |
| 218 | |
| 219 | func (t *txn) placementChoice(r turnRecord, c cardRecord, p placementData, seq int64) (string, error) { |
| 220 | repo := p.NewRepo |
| 221 | offset := 0 |
| 222 | if p.NewFirst && p.NewRepo != "" { |
| 223 | offset = 1 |
| 224 | } |
| 225 | for i, choice := range p.Labels { |
| 226 | if choice == c.Choice && i >= offset && i-offset < len(p.Repos) { |
| 227 | repo = p.Repos[i-offset] |
| 228 | } |
| 229 | } |
| 230 | // Only a later direct composer instruction can replace the selected project. |
| 231 | var followups []messageRecord |
| 232 | if err := t.db.Where("person = ? AND turn_id = ? AND seq > ? AND origin = ? AND coalesce(lane, '') = ''", r.Person, r.ID, seq, originComposer).Order("seq").Find(&followups).Error; err != nil { |
| 233 | return "", err |
| 234 | } |
| 235 | allowed, err := t.allowed(r.Person) |
| 236 | if err != nil { |
| 237 | return "", err |
| 238 | } |
| 239 | for _, m := range followups { |
| 240 | if chosen := typedProjectChoice(m.Text, allowed); chosen != "" { |
| 241 | repo = chosen |
| 242 | } else if withdrawsPlacement(m.Text) { |
| 243 | repo = "" |
| 244 | } |
| 245 | } |
| 246 | return repo, nil |
| 247 | } |
| 248 | |