package runner import ( "bytes" "context" "database/sql" "encoding/json" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/checks" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) // Witness validates against supervisor observations, never submitted labels. // Its journal must be provided by the trusted runner transport at startup. type Witness struct { journal *Journal } func NewWitness(j *Journal) (*Witness, error) { if j == nil { return nil, provider.ErrContract } return &Witness{journal: j}, nil } func (w *Witness) record(ctx context.Context, kind, key string, v any) error { return w.journal.saveState(ctx, "witness-"+kind, key, v) } func (w *Witness) read(ctx context.Context, kind, key string, out any) error { var b []byte if err := w.journal.db.QueryRowContext(ctx, "SELECT body FROM journal_supervisor WHERE kind=? AND state_key=?", "witness-"+kind, key).Scan(&b); err != nil { return provider.ErrUncertain } if json.Unmarshal(b, out) != nil { return provider.ErrUncertain } return nil } func (w *Witness) recordArtifact(ctx context.Context, c RunClaim, spec team.ArtifactSpec) error { return w.record(ctx, "artifact", artifactKey(c.Scope, c.Handle, spec), spec) } func artifactKey(scope team.Scope, h team.Handle, spec team.ArtifactSpec) string { return runnerproto.EventHash([]any{attemptKey(scope, h), spec.Role, team.ContentDigest(spec.Payload)}) } func (w *Witness) Artifact(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error { if q.Artifact == nil || a.ID != q.AssignmentID { return provider.ErrIdentity } var saved team.ArtifactSpec if err := w.read(ctx, "artifact", artifactKey(q.Scope, q.Handle, *q.Artifact), &saved); err != nil { return err } if saved.Role != q.Artifact.Role || !bytes.Equal(saved.Payload, q.Artifact.Payload) { return provider.ErrIdentity } return nil } func (w *Witness) Prepare(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) (runnerproto.PreparedBug, error) { var p runnerproto.PreparedBug if a.ID != q.AssignmentID { return p, provider.ErrIdentity } if err := w.read(ctx, "prepare", a.ID, &p); err != nil { return p, err } var submitted runnerproto.PreparedBug if json.Unmarshal(q.Prepared, &submitted) != nil || !bytes.Equal(marshal(p), marshal(submitted)) { return p, provider.ErrIdentity } return p, nil } func (w *Witness) Provenance(ctx context.Context, _ *sql.Tx, scope team.Scope, h team.Handle) (team.Provenance, error) { var a provider.Agent if err := w.read(ctx, "session", attemptKey(scope, h), &a); err != nil { var p team.Provenance if err = w.read(ctx, "runner-provenance", attemptKey(scope, h), &p); err != nil { return p, err } return p, nil } return team.Provenance{Producer: a.AgentID, Provider: a.Provider, ModelFamily: a.Family, Context: a.ProviderSessionID}, nil } func (w *Witness) installExecutor(_ RunClaim, e *checks.Executor) { e.SetAttestationStore(w) } func (w *Witness) SaveAttestation(ctx context.Context, key string, p team.Provenance) error { return w.record(ctx, "executor", key, p) } func (w *Witness) LoadAttestation(ctx context.Context, key string) (team.Provenance, error) { var p team.Provenance err := w.read(ctx, "executor", key, &p) return p, err } func (w *Witness) ExecutorEvidence(ctx context.Context, _ *sql.Tx, scope team.Scope, h team.Handle, spec team.ArtifactSpec) (team.Provenance, error) { p, err := w.LoadAttestation(ctx, checks.AttestationKey(scope, h, spec)) if err != nil || p.Producer != "equipo-checks" || p.Provider != "executor" || p.Context != h.Runner { return team.Provenance{}, provider.ErrIdentity } return p, nil } func (w *Witness) Events(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error { var e runnerproto.Event if runnerproto.DecodeLimit(bytes.NewReader(q.Event), &e, runnerproto.MaxEventBytes+4096) != nil || e.Validate() != nil || e.AssignmentID != a.ID { return provider.ErrIdentity } var raw []byte err := w.journal.db.QueryRowContext(ctx, "SELECT body FROM journal_events WHERE event_key=?", e.DedupKey()).Scan(&raw) if err != nil { return provider.ErrUncertain } var saved runnerproto.Event if json.Unmarshal(raw, &saved) != nil || saved.ContentHash() != e.ContentHash() { return provider.ErrIdentity } return nil } func (w *Witness) Receipt(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) (team.Receipt, error) { var receipt team.Receipt key := runnerproto.EventHash([]any{q.Scope, q.Handle.Attempt, q.Handle.Fence, q.ActionID}) if err := w.read(ctx, "receipt", key, &receipt); err != nil { return receipt, err } if q.Receipt != nil && !bytes.Equal(marshal(receipt), marshal(q.Receipt)) { return receipt, provider.ErrIdentity } return receipt, nil } func (w *Witness) Question(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error { if q.Question == nil || q.Question.AssignmentID != a.ID { return provider.ErrIdentity } var saved runnerproto.Question if err := w.read(ctx, "question", q.Question.ID, &saved); err != nil { return err } if saved.AgentID != q.Question.AgentID || saved.SessionID != q.Question.SessionID || saved.PermissionID != q.Question.PermissionID || !bytes.Equal(saved.RawRequest, q.Question.RawRequest) { return provider.ErrIdentity } return nil } func (w *Witness) Containment(ctx context.Context, a runnerproto.Assignment, q runnerproto.RunnerRequest) error { var proof ContainmentProof if json.Unmarshal(q.Proof, &proof) != nil { return provider.ErrIdentity } if proof.Scope != q.Scope || proof.Handle != q.Handle { return provider.ErrIdentity } if proof.Version != 2 { return provider.ErrIdentity } var states []attemptState if err := w.journal.readStates(ctx, "attempt", &states); err != nil { return err } for _, s := range states { if s.Claim.Assignment.ID == a.ID && s.Proof != nil && bytes.Equal(marshal(s.Proof), marshal(proof)) { return nil } } return provider.ErrIdentity } func (w *Witness) PublicationAuthorization(ctx context.Context, d team.Dispatch) (runnerproto.PublicationAuthorization, error) { var auth runnerproto.PublicationAuthorization if err := w.read(ctx, "publication", runnerproto.EventHash(d), &auth); err != nil { return auth, err } if !bytes.Equal(marshal(auth.Ticket.Dispatch), marshal(d)) { return auth, provider.ErrIdentity } return auth, nil } func (w *Witness) PrepareBug(ctx context.Context, m *workspace.Manager, a runnerproto.Assignment, target team.TargetBinding) (runnerproto.PreparedBug, error) { if m == nil || a.ID == "" || a.Kind != "prepare" { return runnerproto.PreparedBug{}, provider.ErrContract } prepared, err := m.Prepare(ctx, target) if err != nil { return prepared, err } if err = w.record(ctx, "prepare", a.ID, prepared); err != nil { return runnerproto.PreparedBug{}, err } return prepared, nil }