-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy patheligibility.go
More file actions
417 lines (396 loc) · 15 KB
/
Copy patheligibility.go
File metadata and controls
417 lines (396 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright (c) 2025-2026, Audionut and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package releaseworkflow
import (
"errors"
"fmt"
"slices"
"strings"
"time"
"github.com/autobrr/upbrr/pkg/api"
)
type downstreamStage string
const (
downstreamStageMedia downstreamStage = "media"
downstreamStageDescriptions downstreamStage = "descriptions"
downstreamStageUpload downstreamStage = "upload"
)
type downstreamTrackerSet struct {
orderedIDs []api.TrackerID
projections api.TrackerReleaseProjectionSet
trackerApproval *api.TrackerApprovalSnapshotRef
authority api.WorkflowFingerprint
}
func (s downstreamTrackerSet) TrackerIDs() []api.TrackerID {
return append([]api.TrackerID(nil), s.orderedIDs...)
}
func (s downstreamTrackerSet) Projections() api.TrackerReleaseProjectionSet {
return s.projections
}
func (s downstreamTrackerSet) TrackerApproval() *api.TrackerApprovalSnapshotRef {
if s.trackerApproval == nil {
return nil
}
ref := *s.trackerApproval
return &ref
}
// ProjectionEligibleForDownstream reports whether retained projection and
// duplicate state permit media, description, and upload-plan work.
func ProjectionEligibleForDownstream(
projection api.TrackerReleaseProjection,
dupe api.TrackerDupeAssessment,
hasDupe bool,
) bool {
if !projection.UploadReady || projection.Readiness != api.ReadinessStatusReady || !hasDupe {
return false
}
if dupe.Status == api.StageStatusFailed || dupe.Decision == api.DupeDecisionPending || dupe.Decision == api.DupeDecisionAccepted {
return false
}
return true
}
// DownstreamEligibleProjections applies the shared retained eligibility
// predicate while preserving projection order and snapshot identity.
func DownstreamEligibleProjections(
projections api.TrackerReleaseProjectionSet,
dupes api.DupeAssessment,
) api.TrackerReleaseProjectionSet {
dupeByTracker := make(map[api.TrackerID]api.TrackerDupeAssessment, len(dupes.Results))
for _, result := range dupes.Results {
dupeByTracker[result.TrackerID] = result
}
eligible := projections
eligible.Projections = make([]api.TrackerReleaseProjection, 0, len(projections.Projections))
for _, projection := range projections.Projections {
dupe, exists := dupeByTracker[projection.TrackerID]
if ProjectionEligibleForDownstream(projection, dupe, exists) {
eligible.Projections = append(eligible.Projections, projection)
}
}
return eligible
}
func currentDownstreamEvidence(state *State, now time.Time) (api.TrackerReleaseProjectionSet, api.DupeAssessment, error) {
workflow := state.Workflow
if workflow.Release == nil || workflow.Selection == nil || workflow.TrackerProjections == nil ||
workflow.TrackerPreflight == nil || workflow.Dupes == nil {
return api.TrackerReleaseProjectionSet{}, api.DupeAssessment{}, fmt.Errorf(
"%w: downstream tracker evidence is incomplete",
ErrInvalidTransition,
)
}
projections, projectionsOK := state.Projections[workflow.TrackerProjections.ID]
preflight, preflightOK := state.Preflights[workflow.TrackerPreflight.ID]
dupes, dupesOK := state.Dupes[workflow.Dupes.ID]
if !projectionsOK || !preflightOK || !dupesOK ||
projections.Revision != workflow.TrackerProjections.Revision ||
preflight.Revision != workflow.TrackerPreflight.Revision ||
dupes.Revision != workflow.Dupes.Revision ||
projections.Preflight == nil || *projections.Preflight != *workflow.TrackerPreflight ||
dupes.ProjectionSet != *workflow.TrackerProjections ||
dupes.Selection != *workflow.Selection ||
!preflight.ExpiresAt.After(now) || !dupes.ExpiresAt.After(now) ||
preflight.Status != api.StageStatusReady || !dupesAllowContinuation(&projections, &dupes) {
return api.TrackerReleaseProjectionSet{}, api.DupeAssessment{}, fmt.Errorf(
"%w: downstream tracker evidence is stale or unresolved",
ErrInvalidTransition,
)
}
return projections, dupes, nil
}
func trackerApprovalCandidates(
state *State,
now time.Time,
) (api.TrackerReleaseProjectionSet, api.DupeAssessment, []api.TrackerID, error) {
projections, dupes, err := currentDownstreamEvidence(state, now)
if err != nil {
return api.TrackerReleaseProjectionSet{}, api.DupeAssessment{}, nil, err
}
dupeByTracker := make(map[api.TrackerID]api.TrackerDupeAssessment, len(dupes.Results))
for _, result := range dupes.Results {
dupeByTracker[result.TrackerID] = result
}
for _, projection := range projections.Projections {
if !projection.UploadReady || projection.Readiness != api.ReadinessStatusReady {
continue
}
dupe, exists := dupeByTracker[projection.TrackerID]
if !exists || dupe.Decision == api.DupeDecisionPending {
return api.TrackerReleaseProjectionSet{}, dupes, nil, nil
}
}
eligible := DownstreamEligibleProjections(projections, dupes)
ids := make([]api.TrackerID, len(eligible.Projections))
for index, projection := range eligible.Projections {
ids[index] = projection.TrackerID
}
return eligible, dupes, ids, nil
}
func trackerApprovalActionInput(
state *State,
dupes api.DupeAssessment,
candidateIDs []api.TrackerID,
) (api.WorkflowFingerprint, error) {
workflow := state.Workflow
fingerprint, err := api.CanonicalWorkflowFingerprint(struct {
Mode TrackerDecisionMode
WorkflowID api.WorkflowID
Release api.ReleaseSnapshotRef
Selection api.TrackerSelectionRef
ProjectionSet api.TrackerReleaseProjectionSetRef
Preflight api.TrackerPreflightAssessmentRef
Dupes api.DupeAssessmentRef
DupeInput api.WorkflowFingerprint
CandidateTrackerIDs []api.TrackerID
}{
Mode: normalizeTrackerDecisionMode(state.TrackerDecisionMode),
WorkflowID: workflow.ID,
Release: *workflow.Release,
Selection: *workflow.Selection,
ProjectionSet: *workflow.TrackerProjections,
Preflight: *workflow.TrackerPreflight,
Dupes: *workflow.Dupes,
DupeInput: dupes.InputFingerprint,
CandidateTrackerIDs: append([]api.TrackerID(nil), candidateIDs...),
})
if err != nil {
return "", fmt.Errorf("fingerprint tracker approval action: %w", err)
}
return fingerprint, nil
}
func trackerApprovalFingerprint(
actionInput api.WorkflowFingerprint,
candidateIDs []api.TrackerID,
approvedIDs []api.TrackerID,
) (api.WorkflowFingerprint, error) {
fingerprint, err := api.CanonicalWorkflowFingerprint(struct {
ActionInput api.WorkflowFingerprint
CandidateTrackerIDs []api.TrackerID
ApprovedTrackerIDs []api.TrackerID
}{
ActionInput: actionInput,
CandidateTrackerIDs: candidateIDs,
ApprovedTrackerIDs: approvedIDs,
})
if err != nil {
return "", fmt.Errorf("fingerprint tracker approval: %w", err)
}
return fingerprint, nil
}
func projectedTrackerApprovalAction(state *State, now time.Time) (*api.RequiredAction, api.WorkflowFingerprint, error) {
if normalizeTrackerDecisionMode(state.TrackerDecisionMode) != TrackerDecisionModePostDupeGate ||
state.Workflow.TrackerApproval != nil {
return nil, "", nil
}
eligible, dupes, candidateIDs, err := trackerApprovalCandidates(state, now)
if err != nil || len(candidateIDs) == 0 {
return nil, "", nil
}
fingerprint, err := trackerApprovalActionInput(state, dupes, candidateIDs)
if err != nil {
return nil, "", err
}
options := make([]api.RequiredActionOption, len(eligible.Projections))
for index, projection := range eligible.Projections {
label := strings.TrimSpace(projection.DisplayName)
if label == "" {
label = string(projection.TrackerID)
}
options[index] = api.RequiredActionOption{Value: string(projection.TrackerID), Label: label}
}
expiresAt := dupes.ExpiresAt
return &api.RequiredAction{
ID: api.RequiredActionID("approve-trackers-" + string(fingerprint[:16])),
Kind: api.RequiredActionApproveTrackers,
Status: api.RequiredActionStatusPending,
WorkflowRevision: state.Workflow.Revision,
Prompt: "Select trackers to continue through preparation and upload.",
Options: options,
CreatedAt: dupes.CreatedAt,
ExpiresAt: &expiresAt,
}, fingerprint, nil
}
func currentTrackerApproval(
state *State,
now time.Time,
candidates []api.TrackerID,
dupes api.DupeAssessment,
) (api.TrackerApprovalSnapshot, error) {
ref := state.Workflow.TrackerApproval
if ref == nil {
return api.TrackerApprovalSnapshot{}, fmt.Errorf("%w: tracker approval is required", ErrInvalidTransition)
}
approval, ok := state.TrackerApprovals[ref.ID]
workflow := state.Workflow
if !ok || approval.Revision != ref.Revision ||
approval.WorkflowID != workflow.ID ||
workflow.Release == nil || approval.Release != *workflow.Release ||
workflow.Selection == nil || approval.Selection != *workflow.Selection ||
workflow.TrackerProjections == nil || approval.ProjectionSet != *workflow.TrackerProjections ||
workflow.TrackerPreflight == nil || approval.Preflight != *workflow.TrackerPreflight ||
workflow.Dupes == nil || approval.Dupes != *workflow.Dupes ||
!dupes.ExpiresAt.After(now) || !slices.Equal(approval.CandidateTrackerIDs, candidates) {
return api.TrackerApprovalSnapshot{}, fmt.Errorf("%w: tracker approval is stale", ErrInvalidTransition)
}
actionInput, err := trackerApprovalActionInput(state, dupes, candidates)
if err != nil {
return api.TrackerApprovalSnapshot{}, err
}
expectedFingerprint, err := trackerApprovalFingerprint(
actionInput,
approval.CandidateTrackerIDs,
approval.ApprovedTrackerIDs,
)
if err != nil {
return api.TrackerApprovalSnapshot{}, err
}
if approval.InputFingerprint != expectedFingerprint {
return api.TrackerApprovalSnapshot{}, fmt.Errorf("%w: tracker approval fingerprint is stale", ErrInvalidTransition)
}
return approval, nil
}
func resolveDownstreamTrackerSet(
state *State,
requested []api.TrackerID,
stage downstreamStage,
now time.Time,
) (downstreamTrackerSet, error) {
projections, dupes, err := currentDownstreamEvidence(state, now)
if err != nil {
return downstreamTrackerSet{}, err
}
eligible := DownstreamEligibleProjections(projections, dupes)
candidates := make([]api.TrackerID, len(eligible.Projections))
for index, projection := range eligible.Projections {
candidates[index] = projection.TrackerID
}
baseIDs := candidates
var approvalRef *api.TrackerApprovalSnapshotRef
authority, err := api.CanonicalWorkflowFingerprint(struct {
Mode TrackerDecisionMode
Candidates []api.TrackerID
}{
Mode: normalizeTrackerDecisionMode(state.TrackerDecisionMode),
Candidates: candidates,
})
if err != nil {
return downstreamTrackerSet{}, fmt.Errorf("fingerprint downstream tracker authority: %w", err)
}
switch normalizeTrackerDecisionMode(state.TrackerDecisionMode) {
case TrackerDecisionModePostDupeGate:
approval, approvalErr := currentTrackerApproval(state, now, candidates, dupes)
if approvalErr != nil {
return downstreamTrackerSet{}, approvalErr
}
baseIDs = approval.ApprovedTrackerIDs
ref := api.TrackerApprovalSnapshotRef{ID: approval.ID, Revision: approval.Revision}
approvalRef = &ref
authority = approval.InputFingerprint
case TrackerDecisionModeWebUIControls:
}
base := make(map[api.TrackerID]struct{}, len(baseIDs))
for _, trackerID := range baseIDs {
base[normalizeDownstreamTrackerID(trackerID)] = struct{}{}
}
if (stage == downstreamStageDescriptions || stage == downstreamStageUpload) && state.Workflow.Media != nil {
media, ok := state.Media[state.Workflow.Media.ID]
if !ok || media.Revision != state.Workflow.Media.Revision {
return downstreamTrackerSet{}, fmt.Errorf("%w: retained media is stale", ErrInvalidTransition)
}
blocked := make([]string, 0, len(base))
for trackerID := range base {
if _, failed := TrackerImageHostFailure(media, trackerID); failed {
delete(base, trackerID)
blocked = append(blocked, string(trackerID))
}
}
// An empty set here would otherwise travel silently into the upload
// plan and only surface as a contract validation error about missing
// target tracker IDs, which names neither the stage nor the cause.
if len(base) == 0 && len(blocked) > 0 {
slices.Sort(blocked)
return downstreamTrackerSet{}, fmt.Errorf(
"%w: image hosting failed for every downstream tracker (%s)",
ErrInvalidTransition,
strings.Join(blocked, ", "),
)
}
}
selected := base
if len(requested) > 0 {
selected = make(map[api.TrackerID]struct{}, len(requested))
for _, trackerID := range requested {
trackerID = normalizeDownstreamTrackerID(trackerID)
if trackerID == "" {
return downstreamTrackerSet{}, fmt.Errorf("%w: downstream tracker ID is required", ErrInvalidTransition)
}
if _, allowed := base[trackerID]; !allowed {
return downstreamTrackerSet{}, fmt.Errorf(
"%w: tracker %s is outside current downstream authority",
ErrInvalidTransition,
trackerID,
)
}
if _, duplicate := selected[trackerID]; duplicate {
return downstreamTrackerSet{}, fmt.Errorf(
"%w: tracker %s appears more than once in the downstream selection",
ErrInvalidTransition,
trackerID,
)
}
selected[trackerID] = struct{}{}
}
}
filtered := projections
filtered.Projections = make([]api.TrackerReleaseProjection, 0, len(selected))
orderedIDs := make([]api.TrackerID, 0, len(selected))
for _, projection := range eligible.Projections {
if _, included := selected[projection.TrackerID]; !included {
continue
}
filtered.Projections = append(filtered.Projections, projection)
orderedIDs = append(orderedIDs, projection.TrackerID)
}
if len(requested) > 0 && len(orderedIDs) != len(requested) {
return downstreamTrackerSet{}, errors.New("downstream tracker selection could not be resolved exactly")
}
return downstreamTrackerSet{
orderedIDs: orderedIDs,
projections: filtered,
trackerApproval: approvalRef,
authority: authority,
}, nil
}
func normalizeDownstreamTrackerID(value api.TrackerID) api.TrackerID {
return api.TrackerID(strings.ToUpper(strings.TrimSpace(string(value))))
}
// TrackerImageHostFailure returns the terminal image-host failure that blocks
// one tracker in the retained media snapshot.
func TrackerImageHostFailure(media api.MediaArtifactSet, trackerID api.TrackerID) (api.WorkflowFailure, bool) {
trackerID = normalizeDownstreamTrackerID(trackerID)
if trackerID == "" {
return api.WorkflowFailure{}, false
}
for _, failure := range media.Failures {
if failure.Failure.Operation != api.OperationKindImageHosting {
continue
}
if normalizeDownstreamTrackerID(failure.TrackerID) == trackerID {
return failure, true
}
}
return api.WorkflowFailure{}, false
}
// DownstreamEligibleProjectionsAfterMedia removes trackers with terminal
// tracker-scoped image-host failures from the regular post-dupe eligible set.
func DownstreamEligibleProjectionsAfterMedia(
projections api.TrackerReleaseProjectionSet,
dupes api.DupeAssessment,
media api.MediaArtifactSet,
) api.TrackerReleaseProjectionSet {
eligible := DownstreamEligibleProjections(projections, dupes)
eligible.Projections = slices.DeleteFunc(eligible.Projections, func(projection api.TrackerReleaseProjection) bool {
_, failed := TrackerImageHostFailure(media, projection.TrackerID)
return failed
})
return eligible
}