oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "bytes"
5 "context"
6 "database/sql"
7 "encoding/json"
8
9 "amadan.net/oficina/equipo/internal/equipo/team"
10 "amadan.net/oficina/equipo/internal/runner/checks"
11 "amadan.net/oficina/equipo/internal/runner/provider"
12 "amadan.net/oficina/equipo/internal/runner/workspace"
13 "amadan.net/oficina/equipo/internal/runnerproto"
14)
15
16// Witness validates against supervisor observations, never submitted labels.
17// Its journal must be provided by the trusted runner transport at startup.
18type Witness struct {
19 journal *Journal
20}
21
22func NewWitness(j *Journal) (*Witness, error) {
23 if j == nil {
24 return nil, provider.ErrContract
25 }
26 return &Witness{journal: j}, nil
27}
28func (w *Witness) record(ctx context.Context, kind, key string, v any) error {
29 return w.journal.saveState(ctx, "witness-"+kind, key, v)
30}
31func (w *Witness) read(ctx context.Context, kind, key string, out any) error {
32 var b []byte
33 if err := w.journal.db.QueryRowContext(ctx, "SELECT body FROM journal_supervisor WHERE kind=? AND state_key=?", "witness-"+kind, key).Scan(&b); err != nil {
34 return provider.ErrUncertain
35 }
36 if json.Unmarshal(b, out) != nil {
37 return provider.ErrUncertain
38 }
39 return nil
40}
41func (w *Witness) recordArtifact(ctx context.Context, c RunClaim, spec team.ArtifactSpec) error {
42 return w.record(ctx, "artifact", artifactKey(c.Scope, c.Handle, spec), spec)
43}
44func artifactKey(scope team.Scope, h team.Handle, spec team.ArtifactSpec) string {
45 return runnerproto.EventHash([]any{attemptKey(scope, h), spec.Role, team.ContentDigest(spec.Payload)})
46}
47func (w *Witness) Artifact(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error {
48 if q.Artifact == nil || a.ID != q.AssignmentID {
49 return provider.ErrIdentity
50 }
51 var saved team.ArtifactSpec
52 if err := w.read(ctx, "artifact", artifactKey(q.Scope, q.Handle, *q.Artifact), &saved); err != nil {
53 return err
54 }
55 if saved.Role != q.Artifact.Role || !bytes.Equal(saved.Payload, q.Artifact.Payload) {
56 return provider.ErrIdentity
57 }
58 return nil
59}
60func (w *Witness) Prepare(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) (runnerproto.PreparedBug, error) {
61 var p runnerproto.PreparedBug
62 if a.ID != q.AssignmentID {
63 return p, provider.ErrIdentity
64 }
65 if err := w.read(ctx, "prepare", a.ID, &p); err != nil {
66 return p, err
67 }
68 var submitted runnerproto.PreparedBug
69 if json.Unmarshal(q.Prepared, &submitted) != nil || !bytes.Equal(marshal(p), marshal(submitted)) {
70 return p, provider.ErrIdentity
71 }
72 return p, nil
73}
74func (w *Witness) Provenance(ctx context.Context, _ *sql.Tx, scope team.Scope, h team.Handle) (team.Provenance, error) {
75 var a provider.Agent
76 if err := w.read(ctx, "session", attemptKey(scope, h), &a); err != nil {
77 var p team.Provenance
78 if err = w.read(ctx, "runner-provenance", attemptKey(scope, h), &p); err != nil {
79 return p, err
80 }
81 return p, nil
82 }
83 return team.Provenance{Producer: a.AgentID, Provider: a.Provider, ModelFamily: a.Family, Context: a.ProviderSessionID}, nil
84}
85func (w *Witness) installExecutor(_ RunClaim, e *checks.Executor) { e.SetAttestationStore(w) }
86func (w *Witness) SaveAttestation(ctx context.Context, key string, p team.Provenance) error {
87 return w.record(ctx, "executor", key, p)
88}
89func (w *Witness) LoadAttestation(ctx context.Context, key string) (team.Provenance, error) {
90 var p team.Provenance
91 err := w.read(ctx, "executor", key, &p)
92 return p, err
93}
94func (w *Witness) ExecutorEvidence(ctx context.Context, _ *sql.Tx, scope team.Scope, h team.Handle, spec team.ArtifactSpec) (team.Provenance, error) {
95 p, err := w.LoadAttestation(ctx, checks.AttestationKey(scope, h, spec))
96 if err != nil || p.Producer != "equipo-checks" || p.Provider != "executor" || p.Context != h.Runner {
97 return team.Provenance{}, provider.ErrIdentity
98 }
99 return p, nil
100}
101func (w *Witness) Events(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error {
102 var e runnerproto.Event
103 if runnerproto.DecodeLimit(bytes.NewReader(q.Event), &e, runnerproto.MaxEventBytes+4096) != nil || e.Validate() != nil || e.AssignmentID != a.ID {
104 return provider.ErrIdentity
105 }
106 var raw []byte
107 err := w.journal.db.QueryRowContext(ctx, "SELECT body FROM journal_events WHERE event_key=?", e.DedupKey()).Scan(&raw)
108 if err != nil {
109 return provider.ErrUncertain
110 }
111 var saved runnerproto.Event
112 if json.Unmarshal(raw, &saved) != nil || saved.ContentHash() != e.ContentHash() {
113 return provider.ErrIdentity
114 }
115 return nil
116}
117func (w *Witness) Receipt(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) (team.Receipt, error) {
118 var receipt team.Receipt
119 key := runnerproto.EventHash([]any{q.Scope, q.Handle.Attempt, q.Handle.Fence, q.ActionID})
120 if err := w.read(ctx, "receipt", key, &receipt); err != nil {
121 return receipt, err
122 }
123 if q.Receipt != nil && !bytes.Equal(marshal(receipt), marshal(q.Receipt)) {
124 return receipt, provider.ErrIdentity
125 }
126 return receipt, nil
127}
128func (w *Witness) Question(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error {
129 if q.Question == nil || q.Question.AssignmentID != a.ID {
130 return provider.ErrIdentity
131 }
132 var saved runnerproto.Question
133 if err := w.read(ctx, "question", q.Question.ID, &saved); err != nil {
134 return err
135 }
136 if saved.AgentID != q.Question.AgentID || saved.SessionID != q.Question.SessionID || saved.PermissionID != q.Question.PermissionID || !bytes.Equal(saved.RawRequest, q.Question.RawRequest) {
137 return provider.ErrIdentity
138 }
139 return nil
140}
141func (w *Witness) Containment(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error {
142 var proof ContainmentProof
143 if json.Unmarshal(q.Proof, &proof) != nil {
144 return provider.ErrIdentity
145 }
146 if proof.Scope != q.Scope || proof.Handle != q.Handle {
147 return provider.ErrIdentity
148 }
149 if proof.Version != 2 {
150 return provider.ErrIdentity
151 }
152
153 var states []attemptState
154 if err := w.journal.readStates(ctx, "attempt", &states); err != nil {
155 return err
156 }
157 for _, s := range states {
158 if s.Claim.Assignment.ID == a.ID && s.Proof != nil && bytes.Equal(marshal(s.Proof), marshal(proof)) {
159 return nil
160 }
161 }
162 return provider.ErrIdentity
163}
164
165func (w *Witness) PublicationAuthorization(ctx context.Context, d team.Dispatch) (runnerproto.PublicationAuthorization, error) {
166 var auth runnerproto.PublicationAuthorization
167 if err := w.read(ctx, "publication", runnerproto.EventHash(d), &auth); err != nil {
168 return auth, err
169 }
170 if !bytes.Equal(marshal(auth.Ticket.Dispatch), marshal(d)) {
171 return auth, provider.ErrIdentity
172 }
173 return auth, nil
174}
175
176func (w *Witness) PrepareBug(ctx context.Context, m *workspace.Manager, a runnerproto.Assignment, target team.TargetBinding) (runnerproto.PreparedBug, error) {
177 if m == nil || a.ID == "" || a.Kind != "prepare" {
178 return runnerproto.PreparedBug{}, provider.ErrContract
179 }
180 prepared, err := m.Prepare(ctx, target)
181 if err != nil {
182 return prepared, err
183 }
184 if err = w.record(ctx, "prepare", a.ID, prepared); err != nil {
185 return runnerproto.PreparedBug{}, err
186 }
187 return prepared, nil
188}
189