Skip to content

#284: # sprout#284 — ci teardown deletes the preview SHA image tags it pushed - #285

Merged
simpros merged 2 commits into
mainfrom
fix-284-teardown-deletes-preview-tags
Nov 9, 2026
Merged

simpros merged 2 commits into
mainfrom
fix-284-teardown-deletes-preview-tags

Conversation

@simpros

@simpros simpros commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Why: teardown should delete the preview SHA image tags it pushed.

Closes #284

@simpros

simpros commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

REQUEST CHANGES (report states REQUEST CHANGES, helper exit 0)

(helper exit 0 disagrees with the stated verdict; the stated verdict counts) ** Review of PR #285 (fix-284-teardown-deletes-preview-tags, head e7c892902d0e38243a4ed9b72c3cb9505ed1097e) against issue #284. The direction is right — teardown now deletes the app tag (and commit-scoped seed tag), best-effort, with forge-specific API clients, and the GitHub head-SHA fix is a genuinely necessary piece. But one of the two refs it deletes is not a per-run artifact at all, and the documented GHCR token story doesn't match the implementation.

Full review report

Verdict

REQUEST CHANGES

Review of PR #285 (fix-284-teardown-deletes-preview-tags, head e7c892902d0e38243a4ed9b72c3cb9505ed1097e) against issue #284. The direction is right — teardown now deletes the app tag (and commit-scoped seed tag), best-effort, with forge-specific API clients, and the GitHub head-SHA fix is a genuinely necessary piece. But one of the two refs it deletes is not a per-run artifact at all, and the documented GHCR token story doesn't match the implementation.

Blocking

1. Teardown deletes content-addressed seed tags, which are shared cache, not "the refs the preview build pushed"

resolveCiPushedImageRefs re-resolves the seed ref through resolveSeedTarget (apps/cli/src/commands/ci-identity.ts:198-203) and throws away the allowReuse flag it just computed. For seeds that declare seed.inputs, that ref is seed-<contenthash> (apps/cli/src/commands/seed-image.ts:107-130), which:

  • is content-addressed and shared across PRs/commits; ensureSeedImage explicitly skips the build+push when the tag already exists (seed-image.ts:145-150, documented at docs/previews.md:276-283);
  • is therefore often a ref this preview never pushed — deleting it contradicts both the function's own docstring ("the exact app + seed refs the preview build pushed") and the issue's requirement ("deletes exactly the refs the preview build pushed");
  • turns the documented reuse optimization off for everyone else: after any single PR closes, the next preview with the same seed content rebuilds the seed;
  • introduces a real race: another preview can pass the docker manifest inspect reuse check, then have this teardown delete the tag before the gateway's pullImage (apps/server/src/docker/engine.ts:134) — that preview's seed then fails with seed_failed for no fault of its own.

apps/cli/src/commands/ci-identity.test.ts:617 locks the current behaviour in (the content-addressed tag is returned as a deletion target), so this looks deliberate rather than accidental — but it needs to be deliberate in the other direction, or the contract in docs/previews.md and this module's docstring have to change and the race acknowledged.

Suggested fix: return { ref, shared } from the resolver (or simply drop the seed ref when seed.value.allowReuse === true), delete only the commit-scoped <sha>-seed tag, and add a unit test asserting a content-addressed seed tag is not a cleanup target. If the author does want shared seed tags removed, that is a separate decision with a docs change and a concurrency answer.

2. GHCR token selection silently prefers a token that cannot delete, and the docs claim a fallback the code doesn't have

resolveGithubTarget (apps/cli/src/commands/image-cleanup.ts:268-289) uses GHCR_DELETE_TOKEN || GITHUB_TOKEN, and the reusable workflow always sets GHCR_DELETE_TOKEN from secrets.REGISTRY_PASSWORD (/.github/workflows/preview.yml:205). REGISTRY_PASSWORD is also the docker login credential (preview.yml:98-99, 152-153) and is very commonly a PAT with write:packages but no delete:packages. In that — arguably the common — setup, cleanup 401/403s and only warns, even though the job's GITHUB_TOKEN (with the caller's packages: write) can delete the package versions. There is no retry with the fallback token.

The docs then state the behaviour that doesn't exist: "Deletion uses REGISTRY_PASSWORD when it is a token with package-delete permission, otherwise GITHUB_TOKEN" (docs/ci-integration.md:160-161). The code cannot tell "is a token with delete permission" before trying, and never falls back after a 401/403.

Fix one side or the other: either retry the delete with GITHUB_TOKEN on unauthorized when the first token wasn't GITHUB_TOKEN (and once warn if both fail), or correct the docs and the workflow default (e.g. only prefer GHCR_DELETE_TOKEN when explicitly opted in). CODE-STYLE.md is explicit that a comment/doc that contradicts the code is a bug.

Non-blocking

  • The motivating leak is only half fixed. On synchronize, every new head SHA pushes a fresh app+seed pair; nothing deletes the previous head's tags, and teardown only knows the final head. A PR with N pushes still leaves N-1 orphaned pairs, which is exactly the growth described in "What broke". This is technically in-spec because AC1 is scoped to "that commit", but the PR claims to close ci teardown should delete the preview SHA image tags it pushed #284 and the registry still grows on active PRs. At minimum file/link a follow-up; ideally clean the previous head on the next preview or list-and-prune versions by run.
  • Silent no-op when the app ref can't be derived. resolveCiPushedImageRefs returns { refs: [], warnings: [] } when resolveImageRef fails (ci-identity.ts:186-187), and the test at ci-identity.test.ts:647 enshrines the silence. Every other failure mode warns; this one makes "teardown ran, nothing was cleaned" look like success.
  • GitHub seed hash is derived from the closed event's checkout, not the head that was built. On closed, actions/checkout gives the PR merge commit, so seed-<hash> can differ from the tag the last preview pushed (base-branch changes to seed.inputs). The cleanup then reports "already absent" for a nonexistent tag and leaves the real one. Persisting the pushed refs at build time, or resolving the seed hash from the head tree, would be robust. (GitLab's stop job is cloned from the MR head, so it is less exposed.)
  • Inconsistent handling of unparseable refs. cleanupGitlab silently drops refs that parseImageRef rejects (image-cleanup.ts:213-217); cleanupGithub warns for the same case (:465-480). A single-label registry host (e.g. an internal registry hostname) also fails parseImageRef and produces a silent no-op on GitLab.
  • GitLab list failures lose their prefix. cleanupGitlab emits repositories.error raw for non-auth failures (:227) while every other warning reads registry tag cleanup skipped: ….
  • deleted is never surfaced. ImageCleanupReport.deleted is only used by tests; the CLI prints warnings only, so an operator has no confirmation that cleanup worked (relevant to the "proven against a real registry" criterion).
  • Test coverage gaps: no case for the GHCR "version also carries other tags → skip" guard (image-cleanup.ts:533-540), no GitLab "repository missing from the list" case, no GHCR 404 package case, no "seed ref not resolved" warning case, and no test that GHCR/custom-registry is skipped with a warning. AC4 asks for best-effort paths alongside the existing teardown tests; the happy paths are well covered, the guards are not.
  • Module size. image-cleanup.ts is 576 lines with two independent forge clients; CODE-STYLE.md asks for small, focused modules. registry-gitlab.ts / registry-github.ts split would match the rest of commands/.
  • PR hygiene (AGENTS.md requires the visual-pr skill): the body is Why: # sprout#284 — … with a malformed Closes ##284 (won't auto-close) and no Change outline; the single commit subject is the same doubled #284: # sprout#284 — ….
  • Acceptance evidence. AC1 requires proof against a real registry (or one forge plus a follow-up ticket); the PR body contains none and there is no e2e/registry harness change. The unit tests mock fetch only.

Verified

  • bun test apps/cli → 383 pass, 0 fail (includes the new ci-teardown-tags.test.ts, identity tests, updated reseed/log tests).
  • bun test templates/preview.test.ts .github/workflows/preview.test.ts → 18 pass, 0 fail.
  • bun run typecheck (turbo build + docs link check + tsc) passes.
  • GitLab job-token auth for the registry API is legitimate: GitLab's Container registry API docs state the API accepts CI_JOB_TOKEN as JOB-TOKEN for the pipeline's own project, so templates/README.md:74-78 and docs/ci-integration.md:49-51 are accurate on that point; 202/204 handling and single-warning-on-403 match that API.
  • gh pr view 285 / gh issue view 284: base main, head matches the requested SHA, 14 files, +1146/−22.

@simpros

simpros commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

APPROVE (report states APPROVE, helper exit 0)

** Re-review of PR #285 (fix-284-teardown-deletes-preview-tags, head 4487e13bab8b01dfb666ec1367888296e020fc80) against issue #284, after the #284: fix round1 commit. Both blockers from the first review are fixed correctly and are covered by tests; the remaining items are follow-ups, not merge blockers. The PR body still needs fixing before merge (see the last section) — it currently will not auto-close #284.

Full review report

Verdict

APPROVE

Re-review of PR #285 (fix-284-teardown-deletes-preview-tags, head 4487e13bab8b01dfb666ec1367888296e020fc80) against issue #284, after the #284: fix round1 commit. Both blockers from the first review are fixed correctly and are covered by tests; the remaining items are follow-ups, not merge blockers. The PR body still needs fixing before merge (see the last section) — it currently will not auto-close #284.

Scope of this pass

  • PR diff at head: 14 files, +1240/−22, base main.
  • Fix commit 4487e13 alone: ci-identity.ts, ci-identity.test.ts, ci-teardown-tags.test.ts, image-cleanup.ts, docs/ci-integration.md, docs/cli-reference.md, docs/getting-started.md.
  • Working tree clean, git rev-parse HEAD = 4487e13…fc80.

Prior blockers: both fixed

1. Content-addressed seed tags are no longer cleanup targets — fixed

resolveCiPushedImageRefs now drops the seed ref when the seed is content-addressed (apps/cli/src/commands/ci-identity.ts:210-213). The guard is exactly right, because allowReuse is the only signal that distinguishes the two seed modes: resolveSeedTarget returns allowReuse: false for a commit-scoped <sha>-seed tag and true for seed-<hash> (apps/cli/src/commands/seed-image.ts:113-130). So the deleted refs are now precisely the per-run artifacts.

  • Unit coverage is inverted as suggested: apps/cli/src/commands/ci-identity.test.ts:617 asserts a content-addressed seed tag is not returned, and the commit-scoped case still asserts both refs.
  • The function docstring was updated to "app + per-run seed refs" (ci-identity.ts:181), matching behaviour.
  • Bonus: this also makes the earlier "seed hash resolved from the closed-event checkout" concern moot — that hash no longer influences what teardown deletes.

2. GHCR token selection now falls back after a refusal — fixed

resolveGithubTarget keeps GHCR_DELETE_TOKEN as the primary token and records GITHUB_TOKEN/GH_TOKEN as fallbackToken when it differs (apps/cli/src/commands/image-cleanup.ts:266-294). deleteGithubVersion retries the same version with the fallback on an unauthorized outcome and returns the second result (image-cleanup.ts:459-481); if both are refused, sawUnauthorized still emits one delete:packages warning (image-cleanup.ts:594).

  • New integration-style test asserts the exact retry sequence per ref: delete-token, then gh-token, twice (apps/cli/src/commands/ci-teardown-tags.test.ts:256-315).
  • The "both refused → warn once" path is still covered (ci-teardown-tags.test.ts:317-364).
  • Docs now describe the implemented behaviour instead of the aspirational one (docs/ci-integration.md:158-176): deletion tries REGISTRY_PASSWORD, retries with GITHUB_TOKEN, and warns once with the missing scope. The docs/ci-integration.md:174-176 guidance to set REGISTRY_PASSWORD only when a delete-capable token is needed also matches the workflow wiring (.github/workflows/preview.yml:205).

In-scope behaviour review (unchanged since round 1, still sound)

  • Order and failure isolation. Cleanup runs only after teardownPreview succeeds, is wrapped in try/catch, and only writes stderr warnings (apps/cli/src/commands/ci-teardown.ts:26-47). Exit code stays 0 for missing tags, failed deletes, and missing permission. AC2/AC3 are met.
  • Ref resolution. App ref = CI_REGISTRY_IMAGE:<locked sha>; seed ref = <app>-seed unless content-addressed. Reseeding/reset use the same identity path, so tags stay consistent with what preview pushed.
  • GitHub head SHA. resolvePullRequestHeadSha (apps/cli/src/identity.ts:176-188) is preferred over GITHUB_SHA, which is the merge commit on pull_request events. That is required for teardown on closed to find the tag the preview built, and it is tested (apps/cli/src/commands/ci-identity.test.ts:441-467). Docs were updated to say <PR head SHA>.
  • Workflow wiring. The Prepare image ref and env blobs step is no longer skipped on closed (.github/workflows/preview.yml:155), so CI_REGISTRY_IMAGE exists for teardown; GHCR_DELETE_TOKEN is passed from REGISTRY_PASSWORD (:205). The step only writes env files, so running it on closed is harmless.
  • GitLab path. Repo-list → tag DELETE with PRIVATE-TOKEN/JOB-TOKEN; CI_JOB_TOKEN access to the project registry API is documented by GitLab ("pass $CI_JOB_TOKEN as the JOB-TOKEN header; only the pipeline's own project"), and the template now cds into app_context so .sprout.yaml resolves (templates/preview.yml:124-129). 202/204 are treated as success, 404 as "already absent", 401/403 as a single scope warning.
  • Docs. docs/ci-integration.md, docs/cli-reference.md, docs/getting-started.md, templates/README.md all describe delete-after-teardown, commit-scoped seed tags, shared content-addressed seed tags (GitHub section), and best-effort semantics. AC5 met.

Verification run

  • bun test apps/cli → 384 pass, 0 fail.
  • bun test templates/preview.test.ts → 18 pass, 0 fail; bun test ./.github/workflows/preview.test.ts → 14 pass, 0 fail.
  • Full bun test → 1298 pass, 11 skip, 0 fail (127 files).
  • bun run typecheck → turbo build 5/5, docs link check OK, docs/site + scripts tsc OK.
  • bun x turbo build --force → 5/5 rebuilt from scratch, no cached results: the new/changed files typecheck cleanly.
  • gh pr view 285 / gh issue view 284: base main, head is the requested SHA, issue ACs read as scoped per commit.

Non-blocking follow-ups

  1. The motivating growth is only half fixed. On synchronize, each new head SHA pushes a fresh app+seed pair and teardown only knows the final head, so an active PR still leaves one orphaned pair per push. This is in-spec (AC1 is per commit), but round 1 asked for "at minimum file/link a follow-up" and no follow-up issue exists. Please open one before merge (e.g. "prune previous head tags on the next preview" or "list-and-prune versions by run").
  2. Silent no-op when the app ref can't be derived. resolveCiPushedImageRefs returns { refs: [], warnings: [] } when resolveImageRef fails (ci-identity.ts:186-187), and a test enshrines the silence. Every other skip warns, so "teardown ran, nothing cleaned" can look like success. A one-line warning would make it diagnosable.
  3. Fallback asymmetry. The GHCR fallback only covers the DELETE call. If the primary token cannot even list package versions (expired/wrong-scope PAT), the lookup fails 401/403 and no fallback is attempted — and the resulting warning still names delete:packages, which misdiagnoses a read failure. On GitLab, GITLAB_TOKEN likewise always shadows CI_JOB_TOKEN with no retry. Low risk in the shipped default (login token can read), but the same class of bug the fallback was added to fix.
  4. {commit_sha} placeholders still resolve from GITHUB_SHA (apps/cli/src/commands/deploy.ts:76), i.e. the merge commit on pull_request events, while the preview image tag is now the head SHA. Adopters referencing a companion image tagged with ${{ github.sha }} stay correct, but a config that points preview.services[].image at the same repo's preview image via {commit_sha} will no longer match the built tag. Worth one sentence in the docs (or align the two deliberately).
  5. Wording drift. templates/README.md:75-77 says teardown deletes "that tag and the seed tag"; content-addressed seed-<hash> tags are left in place (as docs/ci-integration.md now says correctly). Tighten to "commit-scoped seed tag".
  6. Test gaps for guards the happy paths don't exercise: GHCR "version also carries other tags → skip" (image-cleanup.ts:563-570), GHCR 404 package, GitLab "repository missing from the list", and "seed ref not resolved" warning.
  7. Module size. image-cleanup.ts is 606 lines with two independent forge clients; CODE-STYLE.md asks for small focused modules. A registry-gitlab.ts / registry-github.ts split would match the rest of commands/.
  8. PR body / title hygiene (fix before merge, process not code). Per AGENTS.md the body must come from the visual-pr skill, but it is still Why: # sprout#284 — … with a malformed Closes ##284, no Why/Special/Change sections, and the same doubled prefix in the commit subject. Closes ##284 will not auto-close the issue. The body is also the natural place to record the real-registry verification AC1 asks for (or link the follow-up ticket, which AC1 explicitly allows).

Approval rationale

The two blocking defects from round 1 are resolved at the right layer (identity/resolver for the seed tag, target/token plumbing for GHCR), with tests that fail if the behaviour regresses and docs that match the code. The remaining findings are edge cases, missing warnings, test gaps, and PR-description hygiene. None of them puts the in-scope promise at risk, so this is an approval — with the follow-up issue and body fix expected before merge.

@simpros
simpros merged commit 79a12af into main Nov 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ci teardown should delete the preview SHA image tags it pushed

1 participant