oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "encoding/json"
5 "errors"
6 "slices"
7
8 "amadan.net/oficina/equipo/internal/wire"
9 "gorm.io/gorm"
10)
11
12func (t *txn) selectedProject(person, id string) (projectRecord, error) {
13 var p projectRecord
14 err := t.db.First(&p, "id = ? AND state = ?", id, wire.ProjectActive).Error
15 if errors.Is(err, gorm.ErrRecordNotFound) {
16 return p, ErrNotFound
17 }
18 if err != nil {
19 return p, err
20 }
21 allowed, err := t.allowed(person)
22 if err != nil {
23 return p, err
24 }
25 if !slices.Contains(allowed, p.Repo) {
26 return p, ErrNotAllowed
27 }
28 return p, nil
29}
30
31func (t *txn) turnProject(r turnRecord) (string, error) {
32 var m messageRecord
33 err := t.db.Where("person = ? AND turn_id = ? AND origin = ? AND coalesce(project_id, '') <> ''", r.Person, r.ID, originComposer).Order("seq DESC").First(&m).Error
34 if errors.Is(err, gorm.ErrRecordNotFound) {
35 return "", nil
36 }
37 if err != nil {
38 return "", err
39 }
40 p, err := t.messageProject(m)
41 return p.Repo, err
42}
43
44func (t *txn) matchesProjectWrite(r turnRecord, v *wire.ToolCall, repo string) (bool, error) {
45 var in struct {
46 Repo string `json:"repo"`
47 Task string `json:"task"`
48 }
49 if json.Unmarshal(v.Args, &in) != nil {
50 return false, ErrInvalid
51 }
52 if v.Name == wire.ToolRecordTask {
53 if p, err := t.namedProject(r.Person, in.Repo); err != nil {
54 return false, err
55 } else if p != nil {
56 in.Repo = p.Repo
57 }
58 return NormalizeRepo(in.Repo) == repo, nil
59 }
60 k, err := t.loadTask(in.Task)
61 if errors.Is(err, ErrNotFound) {
62 return true, nil
63 }
64 if err != nil {
65 return false, err
66 }
67 var p projectRecord
68 if err := t.db.First(&p, "id = ?", k.ProjectID).Error; err != nil {
69 return false, err
70 }
71 return p.Repo == repo, nil
72}
73
74// A selection released after removal or a changed runner report stays released.
75// Preserve the original request for history and the idempotency contract.
76func (t *txn) messageProject(m messageRecord) (projectRecord, error) {
77 key := "reply:project-lost:" + m.ID
78 var n int64
79 if err := t.db.Model(&messageRecord{}).Where("person = ? AND lane = ? AND idempotency_key = ?", m.Person, m.Lane, key).Count(&n).Error; err != nil {
80 return projectRecord{}, err
81 }
82 if n > 0 {
83 return projectRecord{}, nil
84 }
85 p, err := t.selectedProject(m.Person, m.ProjectID)
86 if !errors.Is(err, ErrNotFound) && !errors.Is(err, ErrNotAllowed) {
87 return p, err
88 }
89 notice := messageRecord{ID: newID("m_"), Person: m.Person, Lane: m.Lane, TurnID: m.TurnID, IdempotencyKey: key, Origin: originGenerated, Role: wire.RoleCoordinator, Text: t.s.authority.Copy("projects.selection_lost"), Revision: 1, CreatedAt: t.now()}
90 if err := t.db.Create(&notice).Error; err != nil {
91 return projectRecord{}, err
92 }
93 if err := t.s.authority.Audit(t.db, m.Person, "project.selection_lost", m.ID); err != nil {
94 return projectRecord{}, err
95 }
96 return projectRecord{}, t.messageEvent(notice)
97}
98