Clean known rehearsal pages after their tags are removed

This commit is contained in:
2026-12-06 02:59:33 +00:00
parent 75823d6c82
commit 2b5032d8c9
4 changed files with 72 additions and 6 deletions

View File

@@ -78,6 +78,10 @@ tag, and the matching local tag. It records intent before each deletion and
reobserves ambiguous outcomes. A canceled observation does not cancel CI;
cleanup uses an independent bounded context.
A known matching page can be removed when its tag is already confirmed absent.
Adopting a page whose ID was not recorded still requires its tag target to be
verified.
A completed result describes the recorded resources and the CI runs observed
at cleanup time. Later operator-initiated reruns, CI history, notifications, and
webhook effects are outside that result. Records remain available for operator

View File

@@ -230,6 +230,11 @@ the original push error, while uncertain dispatch retains recovery evidence.
## 5. An already-absent tag prevents cleanup of its known hosted page
**Completed:** Cleanup accepts confirmed tag absence for a recorded matching
page, while retaining target verification for page adoption and preserving
conflicting IDs, contents, and live targets. Missing-tag, repeated-cleanup,
conflict, and the full rehearsal suite pass.
**Priority:** P2. **Stages:** 10, 13.
**Relevant code:** [`cleanup`](../../internal/rehearsal/cleanup.go) accepts an

View File

@@ -119,7 +119,7 @@ func cleanup(ctx context.Context, request RecoveryRequest, store *Store, attempt
if err := save(); err != nil {
return result, fmt.Errorf("record CI cleanup observations: %w", err)
}
if err := deleteRecordedPage(cleanupCtx, request.Backend, &attempt, save); err != nil {
if err := deleteRecordedPage(cleanupCtx, request.Backend, &attempt, remoteTag.State == git.RefAbsent, save); err != nil {
cleanupFailure(&attempt, &result, err)
return result, errors.Join(err, save())
}
@@ -252,7 +252,7 @@ func forgeNotFound(err error) bool {
return errors.As(err, &failure) && failure.Kind == forge.FailureNotFound
}
func deleteRecordedPage(ctx context.Context, backend forge.DiagnosticBackend, attempt *Attempt, save func() error) error {
func deleteRecordedPage(ctx context.Context, backend forge.DiagnosticBackend, attempt *Attempt, remoteTagAbsent bool, save func() error) error {
if attempt.Outcomes["cleanup-page"] == "complete" {
return nil
}
@@ -277,7 +277,7 @@ func deleteRecordedPage(ctx context.Context, backend forge.DiagnosticBackend, at
if page.ID <= 0 || (attempt.PageID != 0 && page.ID != attempt.PageID) || page.Release.Tag != attempt.Manifest.Tag || page.Release.Title != attempt.ExpectedPageTitle || hex.EncodeToString(digest[:]) != attempt.ExpectedBodySHA256 || page.Release.Draft || page.Release.Prerelease {
return fmt.Errorf("hosted page differs from recorded identity: %w", ErrResourceConflict)
}
// Confirm the tag's commit before adopting or deleting a page.
// Check a live tag's commit; confirmed absence permits removing a known page.
tagged, ok := backend.(interface {
TagCommit(context.Context, string) (string, error)
})
@@ -285,10 +285,10 @@ func deleteRecordedPage(ctx context.Context, backend forge.DiagnosticBackend, at
return errors.New("hosted tag observation is unavailable")
}
commit, err := tagged.TagCommit(ctx, attempt.Manifest.Tag)
if err != nil {
if err != nil && !(forgeNotFound(err) && remoteTagAbsent && attempt.PageID == page.ID) {
return err
}
if !git.EqualObjectID(commit, attempt.Manifest.Commit) {
if err == nil && !git.EqualObjectID(commit, attempt.Manifest.Commit) {
return fmt.Errorf("hosted tag targets a different commit: %w", ErrResourceConflict)
}
if attempt.PageID == 0 {

View File

@@ -29,6 +29,7 @@ type diagnosticForge struct {
commit string
exists, uncertain bool
deletes int
tagError error
}
func (b *diagnosticForge) Preflight(context.Context) error { return nil }
@@ -62,7 +63,63 @@ func (b *diagnosticForge) DeleteDiagnosticRelease(_ context.Context, id int64, t
}
return forge.WriteEvidence{Attempted: true, Disposition: forge.WriteAccepted}, nil
}
func (b *diagnosticForge) TagCommit(context.Context, string) (string, error) { return b.commit, nil }
func (b *diagnosticForge) TagCommit(context.Context, string) (string, error) {
return b.commit, b.tagError
}
func TestCleanupKnownPageAfterRemoteTagRemoval(t *testing.T) {
for _, test := range []struct {
name string
known, changedID, changedBody, wrongCommit bool
wantComplete bool
}{
{name: "known page", known: true, wantComplete: true},
{name: "unknown page"},
{name: "changed page ID", known: true, changedID: true},
{name: "changed body", known: true, changedBody: true},
{name: "conflicting hosted target", known: true, wrongCommit: true},
} {
t.Run(test.name, func(t *testing.T) {
repo, _, attempt, store, backend, _, server := cleanupFixture(t)
defer server.Close()
endpoint, err := git.Resolve(t.Context(), repo, "origin")
if err != nil {
t.Fatal(err)
}
if err := endpoint.DeleteDiagnosticTag(t.Context(), repo, attempt.Manifest.Tag, attempt.TagObject, attempt.Manifest.Commit); err != nil {
t.Fatal(err)
}
if test.known {
attempt.PageID = 17
}
if test.changedID {
backend.page.ID = 18
}
if test.changedBody {
backend.page.Release.Body += "foreign"
}
backend.tagError = &forge.Failure{Kind: forge.FailureNotFound}
if test.wrongCommit {
backend.tagError = nil
backend.commit = strings.Repeat("b", 40)
}
if err := store.Save(attempt); err != nil {
t.Fatal(err)
}
result, err := Cleanup(t.Context(), cleanupRequest(store, backend, server))
if test.wantComplete {
if err != nil || result.Cleanup != "complete" || backend.deletes != 1 {
t.Fatalf("result=%+v error=%v deletes=%d", result, err, backend.deletes)
}
if _, err := Cleanup(t.Context(), cleanupRequest(store, backend, server)); err != nil {
t.Fatal(err)
}
} else if err == nil || backend.deletes != 0 {
t.Fatalf("foreign/unknown page removed: result=%+v error=%v", result, err)
}
})
}
}
func cleanupFixture(t *testing.T) (string, string, Attempt, *Store, *diagnosticForge, *atomic.Bool, *httptest.Server) {
t.Helper()