| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/conversation" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/ownermode" |
| 6 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 7 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 8 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 9 | "context" |
| 10 | "gorm.io/gorm" |
| 11 | "gorm.io/plugin/dbresolver" |
| 12 | "slices" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | type CoordinatorBinding = runnerproto.CoordinatorBinding |
| 17 | type CoordinatorTurn = runnerproto.CoordinatorTurn |
| 18 | type CoordinatorClaim = runnerproto.CoordinatorClaim |
| 19 | type CoordinatorContext = runnerproto.CoordinatorContext |
| 20 | |
| 21 | func CoordinatorGrantRef(person string) string { |
| 22 | return team.CoordinatorGrantRef(normalizeAddress(person)) |
| 23 | } |
| 24 | |
| 25 | var errCoordinatorWaitingConnection = &refusal{copyText("coordinator.waiting_connection"), ErrUnavailable} |
| 26 | |
| 27 | func (s *Store) coordinatorSelection(tx *gorm.DB, b conversation.BindingRecord) (Selection, error) { |
| 28 | return ownermode.SelectBinding(tx, b.CoordinatorBinding, b.MemberRevision, b.ConnectionRevision, func(tx *gorm.DB, person string) (int64, error) { |
| 29 | m, err := currentRunnerMember(tx, person) |
| 30 | return m.Revision, err |
| 31 | }, decodeConnection, s.coordinatorEligible, s.isolationSnapshot, ErrForbidden, errCoordinatorWaitingConnection, storeError) |
| 32 | } |
| 33 | func (s *Store) coordinatorEligible(tx *gorm.DB, c connectionRecord, r ConnectionReport) (bool, error) { |
| 34 | ok, _, err := s.eligible(tx, c, r, s.runnerClock.Now().UTC()) |
| 35 | return ok, err |
| 36 | } |
| 37 | func (s *Store) coordinatorApplication() *conversation.Application { |
| 38 | var core *team.Store |
| 39 | if s.execution != nil { |
| 40 | core = s.execution.Core |
| 41 | } |
| 42 | return &conversation.Application{BindingSelection: s.coordinatorSelection, WaitingConnection: errCoordinatorWaitingConnection, Copy: copyText, BindingRelease: s.coordinatorRelease, DecodeConnection: decodeConnection, BindingEligible: s.coordinatorEligible, Projects: func(tx *gorm.DB) *setup.ProjectsStore { return s.projectsStore().At(tx) }, ProjectCreated: copyText("project_setup.created_message"), ProjectDeclined: copyText("project_setup.declined_message"), Now: s.runnerClock.Now, Core: core, Write: s.write, Read: func(ctx context.Context, fn func(*gorm.DB) error) error { |
| 43 | return s.db.G.WithContext(ctx).Clauses(dbresolver.Read).Transaction(fn) |
| 44 | }, Conversation: conversationAt, Normalize: normalizeAddress, Audit: appendAudit, Invalid: ErrInvalid, Conflict: ErrConflict, Forbidden: ErrForbidden, Unavailable: ErrUnavailable, NotFound: ErrNotFound, |
| 45 | BindingIdentity: func(tx *gorm.DB, actor Actor, runner, connection string) (int64, int64, error) { |
| 46 | member, err := currentRunnerMember(tx, actor.Address) |
| 47 | if err != nil { |
| 48 | return 0, 0, err |
| 49 | } |
| 50 | var c connectionRecord |
| 51 | if err = tx.First(&c, "id=? AND runner_id=? AND person=?", connection, runner, actor.Address).Error; err != nil { |
| 52 | return 0, 0, storeError(err) |
| 53 | } |
| 54 | instance, err := runnerInstance(tx) |
| 55 | if err != nil { |
| 56 | return 0, 0, err |
| 57 | } |
| 58 | r, err := runnerAt(tx, instance, runner) |
| 59 | if err != nil { |
| 60 | return 0, 0, err |
| 61 | } |
| 62 | if r.Person != actor.Address { |
| 63 | return 0, 0, ErrForbidden |
| 64 | } |
| 65 | return member.Revision, c.Revision, nil |
| 66 | }, LimitsAuthority: func(tx *gorm.DB, actor Actor, person string) error { |
| 67 | member, err := currentRunnerMember(tx, actor.Address) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | if member.Role != RoleOwner { |
| 72 | return ErrForbidden |
| 73 | } |
| 74 | _, err = currentRunnerMember(tx, person) |
| 75 | return err |
| 76 | }} |
| 77 | } |
| 78 | func (s *Store) Coordinator(ctx context.Context, a Actor) (CoordinatorBinding, error) { |
| 79 | return s.coordinatorApplication().Coordinator(ctx, a) |
| 80 | } |
| 81 | func (s *Store) SaveCoordinatorBinding(ctx context.Context, a Actor, r, c string, m Mutation) (CoordinatorBinding, error) { |
| 82 | return s.coordinatorApplication().SaveCoordinatorBinding(ctx, a, r, c, m) |
| 83 | } |
| 84 | func (s *Store) ResetCoordinator(ctx context.Context, a Actor, m Mutation) (CoordinatorBinding, error) { |
| 85 | return s.coordinatorApplication().ResetCoordinator(ctx, a, m) |
| 86 | } |
| 87 | func (s *Store) QueueCoordinatorTurn(ctx context.Context, a Actor, id string, p int64, m Mutation) (CoordinatorTurn, error) { |
| 88 | return s.coordinatorApplication().QueueCoordinatorTurn(ctx, a, id, p, m) |
| 89 | } |
| 90 | func (s *Store) RetryCoordinatorTurn(ctx context.Context, a Actor, id string, m Mutation) (CoordinatorTurn, error) { |
| 91 | return s.coordinatorApplication().RetryCoordinatorTurn(ctx, a, id, m) |
| 92 | } |
| 93 | func (s *Store) SetCoordinatorLimits(ctx context.Context, a Actor, p string, l team.Limits, m Mutation) error { |
| 94 | return s.coordinatorApplication().SetCoordinatorLimits(ctx, a, p, l, m) |
| 95 | } |
| 96 | func coordinatorAudit(tx *gorm.DB, b conversation.BindingRecord, kind, id string) error { |
| 97 | return tx.Create(&AuditEvent{Actor: "coordinator:" + b.ID, DelegatingPerson: b.Person, Kind: kind, RecordID: id, At: time.Now().UTC()}).Error |
| 98 | } |
| 99 | func (s *Store) coordinatorProject(tx *gorm.DB, person string, projectID int64, sel Selection) (Project, error) { |
| 100 | var p Project |
| 101 | if err := tx.First(&p, projectID).Error; err != nil { |
| 102 | return p, storeError(err) |
| 103 | } |
| 104 | choice, ids, err := choiceAt(tx, person, projectID) |
| 105 | if err != nil { |
| 106 | return p, err |
| 107 | } |
| 108 | if !slices.Contains(ids, sel.ConnectionID) || choice.PinnedID != "" && choice.PinnedID != sel.ConnectionID { |
| 109 | return p, ErrForbidden |
| 110 | } |
| 111 | return p, nil |
| 112 | } |
| 113 | |
| 114 | type CoordinatorToolCall = runnerproto.CoordinatorToolCall |
| 115 | type CoordinatorToolResult = runnerproto.CoordinatorToolResult |
| 116 | type ClarificationQuestion = runnerproto.ClarificationQuestion |
| 117 | |
| 118 | func (e *Execution) CallCoordinatorTool(ctx context.Context, p RunnerPrincipal, c CoordinatorClaim, call CoordinatorToolCall) (CoordinatorToolResult, error) { |
| 119 | return e.coordinatorEngine().CallCoordinatorTool(ctx, p, c, call) |
| 120 | } |
| 121 | func (s *Store) AnswerClarification(ctx context.Context, a Actor, id, optionID, text string, m Mutation) error { |
| 122 | return s.coordinatorApplication().AnswerClarification(ctx, a, id, optionID, text, m) |
| 123 | } |
| 124 | |
| 125 | func (e *Execution) PollCoordinator(ctx context.Context, p RunnerPrincipal) ([]CoordinatorTurn, error) { |
| 126 | return e.coordinatorEngine().PollCoordinator(ctx, p) |
| 127 | } |
| 128 | |