oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "encoding/json"
6
7 "amadan.net/oficina/equipo/internal/equipo/team"
8 "amadan.net/oficina/equipo/internal/runnerproto"
9 "gorm.io/gorm"
10)
11
12// AttachSupervisedExecution installs instance-side validators. The runner
13// verifies its local observations before submitting authenticated reports.
14func (s *Store) AttachSupervisedExecution(ctx context.Context, path string, options team.Options) (*Execution, error) {
15 options.Provenance = s.supervisedProvenance
16 options.ExecutorEvidence = s.supervisedExecutorEvidence
17 options.Adapters = map[team.Operation]team.Adapter{team.OpProviderCall: s.RunnerAdapter(team.OpProviderCall), team.OpForgePublish: s.RunnerAdapter(team.OpForgePublish)}
18 e, err := s.AttachExecution(ctx, path, options)
19 if err != nil {
20 return nil, err
21 }
22 e.SetRunnerValidators(instanceValidators())
23 return e, nil
24}
25func (e *Execution) concludeAt(ctx context.Context, tx *gorm.DB, a runnerAssignment, q RunnerRequest) (any, error) {
26 if a.Kind != "step" || a.State != "contained" {
27 return nil, ErrForbidden
28 }
29 var task Task
30 if err := tx.First(&task, "id=?", a.TaskID).Error; err != nil {
31 return nil, storeError(err)
32 }
33 readWork := task.Kind == "recommendation" || task.Kind == "investigation"
34 if !readWork && a.StepKey != "publish" {
35 return nil, ErrForbidden
36 }
37 if task.State == "waiting_merge" || task.State == "waiting_checkpoint" || task.State == "answered" {
38 return nil, nil
39 }
40 if readWork {
41 var unfinished int64
42 if err := tx.Raw("SELECT count(*) FROM steps WHERE job_id=? AND state IN ('pending','running')", q.Scope.Job).Scan(&unfinished).Error; err != nil {
43 return nil, err
44 }
45 if unfinished > 0 {
46 return nil, nil
47 }
48 }
49 report, err := e.Core.Conclude(ctx, q.Scope, a.Person)
50 if err != nil {
51 return nil, err
52 }
53 state := "failed"
54 if report.Satisfied {
55 state = "waiting_merge"
56 if readWork {
57 state = "answered"
58 if task.Kind == "investigation" {
59 state = "waiting_checkpoint"
60 }
61 }
62 }
63 if err = tx.Model(&Task{}).Where("id=?", a.TaskID).Select("State").Updates(&Task{State: state}).Error; err != nil {
64 return nil, storeError(err)
65 }
66 return report, appendAudit(tx, Actor{Address: a.Person}, "task.conclude", a.TaskID)
67}
68
69// RecoverClaim returns cleanup authority for an exact journaled claim. Zero
70// deadlines prevent the reply from being reused as dispatch authority.
71func (e *Execution) RecoverClaim(ctx context.Context, p RunnerPrincipal, q ClaimRequest) (ClaimResponse, error) {
72 var result ClaimResponse
73 if q.Version != 2 || q.AssignmentID == "" || q.RequestID == "" || len(q.RequestID) > 256 {
74 return result, ErrInvalid
75 }
76 err := e.atomic(ctx, func(ctx context.Context, tx *gorm.DB) error {
77 if _, err := runnerPrincipalAt(tx, p, e.store.runnerClock.Now().UTC()); err != nil {
78 return err
79 }
80 var a runnerAssignment
81 if err := tx.First(&a, "id=?", q.AssignmentID).Error; err != nil {
82 return storeError(err)
83 }
84 if a.ProtocolVersion != runnerproto.DirectVersion {
85 return ErrForbidden
86 }
87 if a.RunnerID != p.RunnerID || a.Person != p.Person {
88 return ErrForbidden
89 }
90 if a.ClaimRequest != q.RequestID || a.Revision != q.Revision || a.State == "pending" {
91 result.NotClaimed = true
92 return nil
93 }
94 var cleanup runnerCleanup
95 if err := tx.First(&cleanup, "assignment_id=?", a.ID).Error; err != nil {
96 return storeError(err)
97 }
98 if !e.store.runnerClock.Now().Before(cleanup.ExpiresAt) {
99 result.CleanupExpired = true
100 return nil
101 }
102 value, err := assignmentValue(a)
103 if err != nil {
104 return err
105 }
106 var brief RunnerBrief
107 if json.Unmarshal([]byte(a.Brief), &brief) != nil {
108 return ErrUnavailable
109 }
110 token := runnerSecret()
111 if err := tx.Model(&runnerCleanup{}).Where("id=?", cleanup.ID).Select("TokenHash").Updates(&runnerCleanup{TokenHash: runnerHash(runnerproto.CleanupAudience, token)}).Error; err != nil {
112 return storeError(err)
113 }
114 result = ClaimResponse{Scope: value.Scope, Handle: team.Handle{Attempt: team.AttemptID(a.AttemptID), Fence: a.Fence, Runner: a.RunnerID}, Brief: brief, CleanupToken: token, CleanupExpiresAt: cleanup.ExpiresAt}
115 return appendAudit(tx, Actor{Address: p.Person}, "runner.recover_claim", a.ID)
116 })
117 return result, err
118}
119