| 1 | package runnerproto |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 12 | ) |
| 13 | |
| 14 | type Event struct { |
| 15 | Version int `json:"version"` |
| 16 | ID string `json:"id"` |
| 17 | TaskID string `json:"task_id"` |
| 18 | AssignmentID string `json:"assignment_id"` |
| 19 | ServerID string `json:"server_id"` |
| 20 | AgentID string `json:"agent_id"` |
| 21 | SessionID string `json:"session_id"` |
| 22 | Epoch string `json:"epoch"` |
| 23 | Kind string `json:"kind"` |
| 24 | TurnID string `json:"turn_id"` |
| 25 | RequestID string `json:"request_id"` |
| 26 | Sequence *int64 `json:"sequence"` |
| 27 | Scope team.Scope `json:"scope"` |
| 28 | Handle team.Handle `json:"handle"` |
| 29 | ActionID team.ActionID `json:"action_id,omitempty"` |
| 30 | Payload json.RawMessage `json:"payload"` |
| 31 | SourceTime time.Time `json:"source_time"` |
| 32 | Isolation team.IsolationSnapshot `json:"isolation"` |
| 33 | } |
| 34 | |
| 35 | func (e Event) Validate() error { |
| 36 | preparation := (e.Kind == "namespace.owner" || e.Kind == "namespace.launch") && e.Scope.Job == 0 && e.Handle.Attempt == 0 |
| 37 | if e.Version != DirectVersion || e.Scope.Account == "" || (!preparation && (e.Scope.Job <= 0 || e.Handle.Attempt <= 0)) || e.Handle.Fence <= 0 || e.Handle.Runner == "" || e.SourceTime.IsZero() || e.SourceTime.Location() != time.UTC || !json.Valid(e.Payload) || len(e.Payload) > MaxEventBytes || e.ActionID < 0 { |
| 38 | return ErrInvalid |
| 39 | } |
| 40 | for _, s := range []string{e.ID, e.TaskID, e.AssignmentID, e.ServerID, e.AgentID, e.SessionID, e.Kind} { |
| 41 | if s == "" || len(s) > 256 || strings.ContainsRune(s, 0) { |
| 42 | return ErrInvalid |
| 43 | } |
| 44 | } |
| 45 | for _, s := range []string{e.Epoch, e.TurnID, e.RequestID} { |
| 46 | if len(s) > 256 || strings.ContainsRune(s, 0) { |
| 47 | return ErrInvalid |
| 48 | } |
| 49 | } |
| 50 | if e.Sequence != nil && (*e.Sequence < 0 || e.Epoch == "") { |
| 51 | return ErrInvalid |
| 52 | } |
| 53 | if (e.Kind == "turn_completed" || e.Kind == "turn_failed") && (e.TurnID == "" || e.ActionID <= 0) { |
| 54 | return ErrInvalid |
| 55 | } |
| 56 | if (e.Kind == "timeline_replacement" || e.Kind == "timeline_ready") && e.Epoch == "" { |
| 57 | return ErrInvalid |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | func EventHash(v any) string { |
| 62 | b, _ := json.Marshal(v) |
| 63 | sum := sha256.Sum256(b) |
| 64 | return hex.EncodeToString(sum[:]) |
| 65 | } |
| 66 | func (e Event) StreamKey() string { |
| 67 | return EventHash([]string{e.AssignmentID, e.ServerID, e.AgentID, e.SessionID}) |
| 68 | } |
| 69 | func (e Event) DedupKey() string { |
| 70 | if e.Sequence != nil { |
| 71 | return EventHash([]any{e.StreamKey(), e.Epoch, *e.Sequence}) |
| 72 | } |
| 73 | stable := e.RequestID |
| 74 | if stable == "" { |
| 75 | stable = e.TurnID |
| 76 | } |
| 77 | if stable == "" { |
| 78 | stable = e.ID |
| 79 | } |
| 80 | return EventHash([]string{e.StreamKey(), e.Kind, stable}) |
| 81 | } |
| 82 | func (e Event) ContentHash() string { |
| 83 | // Delivery IDs may change across history/live overlap; content may not. |
| 84 | e.ID = "" |
| 85 | e.Isolation = team.IsolationSnapshot{} |
| 86 | if e.Sequence == nil && e.Kind != "timeline_replacement" && e.Kind != "timeline_ready" && e.Kind != "timeline_gap" { |
| 87 | e.Epoch = "" |
| 88 | } |
| 89 | var payload any |
| 90 | decoder := json.NewDecoder(bytes.NewReader(e.Payload)) |
| 91 | decoder.UseNumber() |
| 92 | if decoder.Decode(&payload) == nil { |
| 93 | e.Payload, _ = json.Marshal(payload) |
| 94 | } |
| 95 | return EventHash(e) |
| 96 | } |
| 97 | |