Index observed CI pipeline identities
This commit is contained in:
@@ -514,6 +514,13 @@ build.
|
||||
|
||||
## 12. CI run bookkeeping performs quadratic membership scans
|
||||
|
||||
**Completed:** Observation and cleanup index recorded ID/number pairs with a
|
||||
transient map while retaining ordered saved evidence. Repeated observations,
|
||||
new reruns, record round trips, and missing/mismatched recovery identities are
|
||||
covered; active-run and deletion guards remain intact. The rehearsal suite and
|
||||
focused recovery regressions pass. Membership bookkeeping is expected O(R)
|
||||
per pass with O(R) index space.
|
||||
|
||||
**Priority:** P3. **Stages:** 12, 13.
|
||||
|
||||
**Relevant code:** `observe` and `containsID` in the
|
||||
|
||||
@@ -196,8 +196,9 @@ func quiescent(ctx context.Context, client woodpecker.Client, attempt *Attempt)
|
||||
if len(matches) == 0 {
|
||||
return errors.New("cleanup pending: no matching CI pipeline has reached completion")
|
||||
}
|
||||
if len(attempt.PipelineIDs) != len(attempt.PipelineNumbers) {
|
||||
return errors.New("cleanup pending: recorded CI identities are incomplete")
|
||||
known, err := recordedPipelines(*attempt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cleanup pending: %w", err)
|
||||
}
|
||||
listedNumbers := make(map[int64]int64, len(matches))
|
||||
for _, listed := range matches {
|
||||
@@ -211,9 +212,10 @@ func quiescent(ctx context.Context, client woodpecker.Client, attempt *Attempt)
|
||||
var workflows []WorkflowObservation
|
||||
var jobs []JobObservation
|
||||
for _, listed := range matches {
|
||||
if !containsID(attempt.PipelineIDs, listed.ID) {
|
||||
if _, exists := known[listed.ID]; !exists {
|
||||
attempt.PipelineIDs = append(attempt.PipelineIDs, listed.ID)
|
||||
attempt.PipelineNumbers = append(attempt.PipelineNumbers, listed.Number)
|
||||
known[listed.ID] = listed.Number
|
||||
}
|
||||
detail, err := client.DiagnosticPipeline(ctx, listed.Number, attempt.Manifest.Tag, attempt.Manifest.Commit)
|
||||
if err != nil {
|
||||
|
||||
@@ -345,6 +345,40 @@ func TestCleanupDoesNotInferQuiescenceFromMissingPipeline(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupPreservesRecordedPipelineIdentities(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
ids []int64
|
||||
numbers []int64
|
||||
}{
|
||||
{name: "missing previously observed run", ids: []int64{3, 7}, numbers: []int64{4, 8}},
|
||||
{name: "changed pipeline number", ids: []int64{3}, numbers: []int64{9}},
|
||||
{name: "incomplete evidence", ids: []int64{3}},
|
||||
{name: "conflicting recorded numbers", ids: []int64{3, 3}, numbers: []int64{4, 9}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
repo, bare, attempt, store, backend, _, server := cleanupFixture(t)
|
||||
defer server.Close()
|
||||
attempt.PipelineIDs, attempt.PipelineNumbers = test.ids, test.numbers
|
||||
if err := store.Save(attempt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := Cleanup(t.Context(), cleanupRequest(store, backend, server))
|
||||
if err == nil || result.Cleanup != "pending" || backend.deletes != 0 {
|
||||
t.Fatalf("result=%+v error=%v deletes=%d", result, err, backend.deletes)
|
||||
}
|
||||
endpoint, err := git.ObserveEndpoint(t.Context(), repo, bare, "origin")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
remote, err := endpoint.QueryTag(t.Context(), repo, attempt.Manifest.Tag)
|
||||
if err != nil || remote.State != git.RefPresent {
|
||||
t.Fatalf("remote=%+v error=%v", remote, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupCompletesLocalWorkAfterRemoteCleanupWasRecorded(t *testing.T) {
|
||||
repo, bare, attempt, store, backend, _, server := cleanupFixture(t)
|
||||
server.Close()
|
||||
|
||||
@@ -243,6 +243,10 @@ func Start(ctx context.Context, request StartRequest) (Outcome, error) {
|
||||
func observe(ctx context.Context, client woodpecker.Client, workflow, step string, store *Store, attempt Attempt, result Outcome) (outcome Outcome, resultErr error) {
|
||||
defer func() { outcome.Attempt = attempt }()
|
||||
save := func() error { result.Attempt = attempt; return store.Save(attempt) }
|
||||
known, err := recordedPipelines(attempt)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
attempt.Outcomes["workflow"] = "unknown"
|
||||
@@ -262,9 +266,14 @@ func observe(ctx context.Context, client woodpecker.Client, workflow, step strin
|
||||
jobs := make([]JobObservation, 0)
|
||||
workflows := make([]WorkflowObservation, 0)
|
||||
for _, listed := range pipelines {
|
||||
if !containsID(attempt.PipelineIDs, listed.ID) {
|
||||
if number, exists := known[listed.ID]; exists {
|
||||
if number != listed.Number {
|
||||
return result, errors.Join(errors.New("recorded CI pipeline number changed"), save())
|
||||
}
|
||||
} else {
|
||||
attempt.PipelineIDs = append(attempt.PipelineIDs, listed.ID)
|
||||
attempt.PipelineNumbers = append(attempt.PipelineNumbers, listed.Number)
|
||||
known[listed.ID] = listed.Number
|
||||
}
|
||||
detail, err := client.DiagnosticPipeline(ctx, listed.Number, attempt.Manifest.Tag, attempt.Manifest.Commit)
|
||||
if err != nil {
|
||||
@@ -403,13 +412,20 @@ func observe(ctx context.Context, client woodpecker.Client, workflow, step strin
|
||||
}
|
||||
}
|
||||
}
|
||||
func containsID(values []int64, value int64) bool {
|
||||
for _, item := range values {
|
||||
if item == value {
|
||||
return true
|
||||
}
|
||||
|
||||
func recordedPipelines(attempt Attempt) (map[int64]int64, error) {
|
||||
if len(attempt.PipelineIDs) != len(attempt.PipelineNumbers) {
|
||||
return nil, errors.New("recorded CI identities are incomplete")
|
||||
}
|
||||
return false
|
||||
known := make(map[int64]int64, len(attempt.PipelineIDs))
|
||||
for index, id := range attempt.PipelineIDs {
|
||||
number := attempt.PipelineNumbers[index]
|
||||
if previous, exists := known[id]; exists && previous != number {
|
||||
return nil, errors.New("recorded CI pipeline has conflicting numbers")
|
||||
}
|
||||
known[id] = number
|
||||
}
|
||||
return known, nil
|
||||
}
|
||||
|
||||
func matchesPlannedAssets(planned []string, observed []ArtifactEvidence) bool {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -94,15 +95,24 @@ func TestStartRecordsBeforeDispatchAndObservesExactJob(t *testing.T) {
|
||||
fmt.Fprint(w, `[]`)
|
||||
return
|
||||
}
|
||||
if lists.Add(1) == 1 {
|
||||
pass := lists.Add(1)
|
||||
if pass == 1 {
|
||||
fmt.Fprint(w, `[]`)
|
||||
return
|
||||
}
|
||||
m := recordManifest(t, repo)
|
||||
if pass == 2 {
|
||||
json.NewEncoder(w).Encode([]woodpecker.Pipeline{{ID: 2, Number: 7, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: "running"}})
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode([]woodpecker.Pipeline{{ID: 2, Number: 7, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: "success"}, {ID: 3, Number: 8, Event: "tag", Ref: "refs/tags/unrelated", Commit: m.Commit, Status: "success"}, {ID: 4, Number: 9, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: "running"}})
|
||||
case "/api/repos/42/pipelines/7":
|
||||
state := "success"
|
||||
if lists.Load() == 2 {
|
||||
state = "running"
|
||||
}
|
||||
m := recordManifest(t, repo)
|
||||
json.NewEncoder(w).Encode(woodpecker.Pipeline{ID: 2, Number: 7, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: "success", Workflows: []woodpecker.Workflow{{ID: 11, Name: "release", State: "success", Steps: []woodpecker.Step{{ID: 19, Name: "deliver", State: "success"}}}}})
|
||||
json.NewEncoder(w).Encode(woodpecker.Pipeline{ID: 2, Number: 7, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: state, Workflows: []woodpecker.Workflow{{ID: 11, Name: "release", State: state, Steps: []woodpecker.Step{{ID: 19, Name: "deliver", State: state}}}}})
|
||||
case "/api/repos/42/pipelines/9":
|
||||
m := recordManifest(t, repo)
|
||||
json.NewEncoder(w).Encode(woodpecker.Pipeline{ID: 4, Number: 9, Event: "tag", Ref: "refs/tags/" + m.Tag, Commit: m.Commit, Status: "running", Workflows: []woodpecker.Workflow{{ID: 12, Name: "release", State: "running", Steps: []woodpecker.Step{{ID: 20, Name: "deliver", State: "running"}}}}})
|
||||
@@ -123,13 +133,16 @@ func TestStartRecordsBeforeDispatchAndObservesExactJob(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Workflow != "complete" || result.Verification != "complete" || result.Attempt.PageID != 13 || len(result.Attempt.PipelineIDs) != 2 || result.Attempt.PipelineNumbers[0] != 7 || result.Attempt.PipelineNumbers[1] != 9 || len(result.Attempt.Jobs) != 2 || result.RecordPath == "" || lists.Load() < 2 {
|
||||
if result.Workflow != "complete" || result.Verification != "complete" || result.Attempt.PageID != 13 || len(result.Attempt.PipelineIDs) != 2 || result.Attempt.PipelineNumbers[0] != 7 || result.Attempt.PipelineNumbers[1] != 9 || len(result.Attempt.Jobs) != 2 || result.RecordPath == "" || lists.Load() < 3 {
|
||||
t.Fatalf("result=%+v", result)
|
||||
}
|
||||
saved, err := LoadAttempt(result.RecordPath)
|
||||
if err != nil || saved.JobEvidence == nil || saved.JobEvidence.Verification != "complete" || saved.Manifest.Commit != commit {
|
||||
t.Fatalf("saved=%+v err=%v", saved, err)
|
||||
}
|
||||
if !slices.Equal(saved.PipelineIDs, []int64{2, 4}) || !slices.Equal(saved.PipelineNumbers, []int64{7, 9}) {
|
||||
t.Fatalf("recorded pipeline order changed: IDs=%v numbers=%v", saved.PipelineIDs, saved.PipelineNumbers)
|
||||
}
|
||||
content, err := os.ReadFile(result.RecordPath)
|
||||
if err != nil || strings.Contains(string(content), "wood-token") {
|
||||
t.Fatalf("record leaked credential or could not be read: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user