From 01847334da0d50205d2e28015f63b550158ed569 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 14:14:17 -0300 Subject: [PATCH 1/3] toolcache: an answer from before the caller's release is asked again, then refused For a minute or two after a release is published, GitHub's /releases/latest still redirects some requests to the release before (#135, rollout report 8: brand ran v20261122.05's tasks under a go tool task built from .06). latestTag recorded and ran that answer. Now an answer before since is asked again after 2, 4 and 8 seconds, and when it stays it is an error that says the command's own release is not visible yet: nothing from before the command runs, and nothing is recorded, so the next run asks. Co-Authored-By: Claude Opus 5.5 --- internal/toolcache/toolcache.go | 42 ++++++++++-- internal/toolcache/toolcache_test.go | 98 ++++++++++++++++++++++++++-- site/src/content/docs/tasks.md | 1 + 3 files changed, 133 insertions(+), 8 deletions(-) diff --git a/internal/toolcache/toolcache.go b/internal/toolcache/toolcache.go index a82dc17..6932548 100644 --- a/internal/toolcache/toolcache.go +++ b/internal/toolcache/toolcache.go @@ -99,12 +99,27 @@ const fresh = 10 * time.Minute // is asked again. Without it a command taken from a release minutes old would run for ten minutes // beside the release before, which has nothing that command and its release added. Releases are // in the order of their names, as v. is. +// +// GitHub's own answer can be before since too: for a minute or two after a release is published, +// /releases/latest still redirects some requests to the one before. That answer is asked again, +// and when it stays it is an error and no release is run: the tasks of the release before are not +// the ones the caller's Taskfile was written for. func Latest(ctx context.Context, base, name, root, since string) (string, error) { - return latest(ctx, base, name, root, since, runtime.GOOS, runtime.GOARCH, time.Now()) + return latest(ctx, base, name, root, since, runtime.GOOS, runtime.GOARCH, time.Now(), sleep) +} + +// sleep waits d, or less when ctx ends first. +func sleep(ctx context.Context, d time.Duration) { + t := time.NewTimer(d) + defer t.Stop() + select { + case <-ctx.Done(): + case <-t.C: + } } -func latest(ctx context.Context, base, name, root, since, goos, goarch string, now time.Time) (string, error) { - tag, err := latestTag(ctx, base, filepath.Join(root, name, "latest"), since, now) +func latest(ctx context.Context, base, name, root, since, goos, goarch string, now time.Time, wait func(context.Context, time.Duration)) (string, error) { + tag, err := latestTag(ctx, base, filepath.Join(root, name, "latest"), since, now, wait) if err != nil { return "", err } @@ -147,22 +162,41 @@ func exists(p string) bool { return err == nil } +// again is how long each further question waits when GitHub's answer is a release before the +// caller's: seconds, because a person is waiting on the command, and fourteen of them in all, +// after which it is theirs to run again. +var again = []time.Duration{2 * time.Second, 4 * time.Second, 8 * time.Second} + // latestTag is the name of base's latest release: the one stamp records when it was written less // than fresh ago and is not before since, else asked of GitHub - where /releases/latest redirects to the release, which no // API quota counts - and recorded; the one stamp records, however old, when GitHub cannot answer. -func latestTag(ctx context.Context, base, stamp, since string, now time.Time) (string, error) { +// An answer before since is not the latest and is not recorded: it is asked again after each of +// again, and is an error when it is still the answer. +func latestTag(ctx context.Context, base, stamp, since string, now time.Time, wait func(context.Context, time.Duration)) (string, error) { raw, _ := os.ReadFile(stamp) //nolint:gosec // this cache's own file recorded := string(raw) if st, err := os.Stat(stamp); recorded != "" && recorded >= since && err == nil && now.Sub(st.ModTime()) < fresh { return recorded, nil } tag, err := askLatest(ctx, base) + for _, d := range again { + if err != nil || tag >= since { + break + } + wait(ctx, d) + tag, err = askLatest(ctx, base) + } if err != nil { if recorded != "" { return recorded, nil } return "", fmt.Errorf("toolcache: the latest release of %s: %w", base, err) } + if tag < since { + return "", fmt.Errorf("toolcache: %s, the release this command is of, is not visible yet: %s/releases/latest still answers %s, "+ + "the release before it, as GitHub does to some requests for a minute or two after a release is published. "+ + "Nothing was run, since what would have run is from before this command: run it again", since, base, tag) + } if err := os.MkdirAll(filepath.Dir(stamp), 0o750); err != nil { return "", fmt.Errorf("toolcache: %w", err) } diff --git a/internal/toolcache/toolcache_test.go b/internal/toolcache/toolcache_test.go index 1c0ff75..2d67cd3 100644 --- a/internal/toolcache/toolcache_test.go +++ b/internal/toolcache/toolcache_test.go @@ -4,12 +4,14 @@ import ( "archive/tar" "bytes" "compress/gzip" + "context" "crypto/sha256" "encoding/hex" "net/http" "net/http/httptest" "os" "path/filepath" + "slices" "strings" "sync/atomic" "testing" @@ -248,7 +250,7 @@ func TestTheLatestReleaseIsAskedForAtMostEveryTenMinutes(t *testing.T) { now := time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) holds := func(at time.Time, want string) { t.Helper() - dir, err := latest(t.Context(), base, "go-tools", root, "", "linux", "amd64", at) + dir, err := latest(t.Context(), base, "go-tools", root, "", "linux", "amd64", at, noWait) if err != nil { t.Fatal(err) } @@ -299,7 +301,7 @@ func TestALatestReleaseThatCannotBeHadIsSaid(t *testing.T) { if tc.name == "no release" { base += "/nothing" } - _, err := latest(t.Context(), base, "go-tools", t.TempDir(), "", tc.goos, "amd64", time.Now()) + _, err := latest(t.Context(), base, "go-tools", t.TempDir(), "", tc.goos, "amd64", time.Now(), noWait) if err == nil || !strings.Contains(err.Error(), tc.says) { t.Errorf("err = %v, want %q", err, tc.says) } @@ -319,7 +321,7 @@ func TestAReleaseBeforeTheCallersIsNotTheLatestHoweverFresh(t *testing.T) { root := t.TempDir() holds := func(since, want string, asked int32) { t.Helper() - dir, err := latest(t.Context(), base, "go-tools", root, since, "linux", "amd64", time.Now()) + dir, err := latest(t.Context(), base, "go-tools", root, since, "linux", "amd64", time.Now(), noWait) if err != nil { t.Fatal(err) } @@ -340,6 +342,94 @@ func TestAReleaseBeforeTheCallersIsNotTheLatestHoweverFresh(t *testing.T) { holds("v20261110.02", "two", 2) } +// noWait is the wait of a test that asks GitHub once, or does not care how long it would have waited. +func noWait(context.Context, time.Duration) {} + +// For a minute or two after a release GitHub answers some requests with the one before. To a +// caller of the new release that is no answer: it is asked again, after a wait each time, and the +// release run and recorded is the caller's own, never the one GitHub said first. +func TestAnAnswerBeforeTheCallersReleaseIsAskedAgain(t *testing.T) { + one, _ := tarball(t, member{name: "mutate", body: "one", mode: 0o755, kind: tar.TypeReg}) + two, _ := tarball(t, member{name: "mutate", body: "two", mode: 0o755, kind: tar.TypeReg}) + tag := "v20261110.01" + g := &github{latest: &tag, releases: map[string][]byte{"v20261110.01": one, "v20261110.02": two}} + base := g.serve(t) + root := t.TempDir() + var waited []time.Duration + // The old release twice, and the new one to the third question. + wait := func(_ context.Context, d time.Duration) { + if waited = append(waited, d); len(waited) == 2 { + tag = "v20261110.02" + } + } + dir, err := latest(t.Context(), base, "go-tools", root, "v20261110.02", "linux", "amd64", time.Now(), wait) + if err != nil { + t.Fatal(err) + } + if got, _ := os.ReadFile(filepath.Join(dir, "mutate")); string(got) != "two" { + t.Errorf("mutate is %q, the release before the caller's", got) + } + if g.asked.Load() != 3 || !slices.Equal(waited, []time.Duration{2 * time.Second, 4 * time.Second}) { + t.Errorf("asked %d times, having waited %v", g.asked.Load(), waited) + } + if got, _ := os.ReadFile(filepath.Join(root, "go-tools", "latest")); string(got) != "v20261110.02" { + t.Errorf("the stamp says %q", got) + } +} + +// When GitHub goes on answering the release before the caller's, nothing is run: the command says +// its own release is not visible yet and to run it again, and records nothing, so the next run asks. +// A release already recorded makes no difference: it is before the caller's too. +func TestAnAnswerThatStaysBeforeTheCallersReleaseRunsNothing(t *testing.T) { + one, _ := tarball(t, member{name: "mutate", body: "one", mode: 0o755, kind: tar.TypeReg}) + tag := "v20261110.01" + g := &github{latest: &tag, releases: map[string][]byte{"v20261110.01": one}} + base := g.serve(t) + for _, recorded := range []string{"", "v20261110.01"} { + root := t.TempDir() + stamp := filepath.Join(root, "go-tools", "latest") + if recorded != "" { + if _, err := latest(t.Context(), base, "go-tools", root, "", "linux", "amd64", time.Now(), noWait); err != nil { + t.Fatal(err) + } + } + g.asked.Store(0) + var waited time.Duration + dir, err := latest(t.Context(), base, "go-tools", root, "v20261110.02", "linux", "amd64", time.Now(), + func(_ context.Context, d time.Duration) { waited += d }) + if err == nil { + t.Fatalf("with %q recorded: ran %s, a release before the caller's", recorded, dir) + } + for _, says := range []string{"v20261110.02, the release this command is of, is not visible yet", "still answers v20261110.01", "run it again"} { + if !strings.Contains(err.Error(), says) { + t.Errorf("with %q recorded: %v, which does not say %q", recorded, err, says) + } + } + if g.asked.Load() != 4 || waited != 14*time.Second { + t.Errorf("with %q recorded: asked %d times over %s", recorded, g.asked.Load(), waited) + } + if got, _ := os.ReadFile(stamp); string(got) != recorded { + t.Errorf("the stamp says %q, and %q was recorded before", got, recorded) + } + } +} + +// The wait between two questions is the time asked for, cut short when the command is. +func TestTheWaitEndsWithTheCommand(t *testing.T) { + start := time.Now() + sleep(t.Context(), 30*time.Millisecond) + if took := time.Since(start); took < 30*time.Millisecond { + t.Errorf("waited %s of 30ms", took) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + start = time.Now() + sleep(ctx, time.Minute) + if took := time.Since(start); took > 10*time.Second { + t.Errorf("a cancelled wait took %s", took) + } +} + // A stamp that says nothing is no release, however fresh: the latest is asked for. func TestAnEmptyStampIsAskedPast(t *testing.T) { archive, _ := tarball(t, member{name: "mutate", body: "x", mode: 0o755, kind: tar.TypeReg}) @@ -353,7 +443,7 @@ func TestAnEmptyStampIsAskedPast(t *testing.T) { if err := os.WriteFile(filepath.Join(root, "go-tools", "latest"), nil, 0o600); err != nil { t.Fatal(err) } - if _, err := latest(t.Context(), base, "go-tools", root, "", "linux", "amd64", time.Now()); err != nil || g.asked.Load() != 1 { + if _, err := latest(t.Context(), base, "go-tools", root, "", "linux", "amd64", time.Now(), noWait); err != nil || g.asked.Load() != 1 { t.Errorf("err = %v, asked %d times", err, g.asked.Load()) } } diff --git a/site/src/content/docs/tasks.md b/site/src/content/docs/tasks.md index f03d7a1..f841049 100644 --- a/site/src/content/docs/tasks.md +++ b/site/src/content/docs/tasks.md @@ -295,6 +295,7 @@ apply there. The ones that do not apply refuse, before running anything: | `include cycle detected between <--> ` (exit 110), for every task | `GO_TOOLS_TASKS` is empty: Task was run directly, or `go.mod` requires a go-tools from before `v0.20261122.4`, whose `go tool task` does not set it | run `go tool task`; `go get github.com/spin-stack/go-tools@latest` | | `Found multiple tasks (versions) included by "common"` (exit 203), for every task | the repository's Taskfile defines a common task itself | delete the repository's own: the name has one meaning | | `No Taskfile found at ".../tasks.yml"` (exit 100), for every task, after a line from `go tool task` that starts `the go-tools release this machine has ... has no tasks.yml` | the release in the cache is from before the tasks shipped, and no newer one could be had: GitHub could not be reached, or `go tool task` was built from a commit that is no release's and the cache named the latest less than ten minutes ago | run it again when `https://github.com/spin-stack/go-tools/releases/latest` can be reached, or ten minutes later | +| `toolcache: v., the release this command is of, is not visible yet: https://github.com/spin-stack/go-tools/releases/latest still answers ...`, for every task | in the first minute or two after a go-tools release, GitHub answers some requests with the release before. `go tool task` asks three more times, 2, 4 and 8 seconds apart, and then runs nothing, since what it would run is from before the release it was compiled from | run it again a minute later | | `label "runs-on/fleet=.../env=production" is unknown`, from `lint:workflows` | a job runs on one of spin-stack's own runners, and actionlint knows only GitHub's labels | name the labels in [`.github/actionlint.yaml`](#put-lintworkflows-in-what-ci-runs) | | `task: Failed to run task "versions": exit status 1` or `exit status 2` (exit 201) | an entry is behind (1), or [could not be read](../versions/#an-entry-check-could-not-read) and may be (2). Task's own exit status is 201 for both, and for any task whose command failed | `go tool task bump NAME=`; for 2, read the row: usually a private upstream and no token | | `versions: there is no actionlint in versions.yaml`, from `lint:workflows` | the task runs `actionlint` at the repository's pin, and there is none | add the `actionlint` entry (kind `module`, source `github.com/rhysd/actionlint/cmd/actionlint`) | From 320ae76cc9ff9e4955b36cf24e2a220656b5970a Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 14:14:17 -0300 Subject: [PATCH 2/3] versions: -by names the command running bump or refresh, and an entry that names it moves An entry's bump: field refuses versions bump and versions refresh, which is what it is for, and also refuses the command it names when that command shells out to them (#135, rollout reports 10 and 11): spin-machine-dev bump and refresh, devcontainer-cli's refresh node, spin's machine:propose. So none of those repositories could mark an entry. -by says who is running the command, in the entry's words, and the entry moves; a person and the common bump pass nothing and are refused as before, and a -by that names another command is refused with both names. check's last line no longer offers go tool task bump when every row behind names a command of its own, which is the only one that moves those rows. Co-Authored-By: Claude Opus 5.5 --- cmd/versions/main.go | 58 ++++++++++++++------- cmd/versions/main_test.go | 38 +++++++++++++- cmd/versions/refresh.go | 4 +- cmd/versions/refresh_test.go | 10 ++++ site/src/content/docs/tasks.md | 4 +- site/src/content/docs/versions/reference.md | 22 ++++++-- versions/versions.go | 3 +- 7 files changed, 112 insertions(+), 27 deletions(-) diff --git a/cmd/versions/main.go b/cmd/versions/main.go index 0bb55ac..c7c46c3 100644 --- a/cmd/versions/main.go +++ b/cmd/versions/main.go @@ -78,12 +78,14 @@ Usage: versions bump [version] pin an entry at a version, the newest by default, and every entry tracked with it at the same; never at an older version or commit than the pin's, unless -back, - and not an entry whose bump: names another command + and not an entry whose bump: names another command, + unless -by says this is that command running it versions refresh ... bump each to its newest, a commit each, and say what moved and what is a new major, left for a person; -propose pushes them as refresh/