From 8eb76274e6771259c0dbac26648b3c0df599f58f Mon Sep 17 00:00:00 2001 From: Gaoshou101 Date: Wed, 2 Dec 2026 13:14:04 +0800 Subject: [PATCH 1/2] fix(test): seed TestGuardOwedAbsoluteMeasuredCommand with CheckContext Fixes #3008 TestGuardOwedAbsoluteMeasuredCommand hardcoded Context: make([]string, 8) on the inherited synthetic check while executing the portable rerun through the live bash check observer, which records the runner's checkContextNames (GOOS, GOARCH, GOFLAGS, CGO_ENABLED, GOWORK, GORACE, NODE_ENV, PYTEST_ADDOPTS). When any of those variables was set in the environment, the context mismatch kept the saved absolute failure owed even after a matching portable rerun. Export verification.CheckContext() (also reused by CoversUnrun), use it to seed the inherited check, and add TestGuardOwedAbsoluteMeasuredCommandContext to verify both matching non-empty context clearing and cross-context holding. --- cmd/codeaf/factory_guard_stale_test.go | 31 ++++++++++++++++++- .../factory/verification/go_requirements.go | 7 +---- internal/factory/verification/receipts.go | 10 ++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/cmd/codeaf/factory_guard_stale_test.go b/cmd/codeaf/factory_guard_stale_test.go index 6872603773..c9baf60321 100644 --- a/cmd/codeaf/factory_guard_stale_test.go +++ b/cmd/codeaf/factory_guard_stale_test.go @@ -251,7 +251,7 @@ func TestGuardOwedAbsoluteMeasuredCommand(t *testing.T) { t.Fatal(err) } failed := 7 - old := verification.Check{Args: []string{"/usr/local/go/bin/go", "test", "./..."}, Command: "/usr/local/go/bin/go test ./...", Cwd: root, Context: make([]string, 8), Exit: &failed, Before: at, After: at, ID: "saved", Source: "old"} + old := verification.Check{Args: []string{"/usr/local/go/bin/go", "test", "./..."}, Command: "/usr/local/go/bin/go test ./...", Cwd: root, Context: verification.CheckContext(), Exit: &failed, Before: at, After: at, ID: "saved", Source: "old"} if err := verification.Inherit(file, []verification.Check{old}); err != nil { t.Fatal(err) } @@ -271,6 +271,35 @@ func TestGuardOwedAbsoluteMeasuredCommand(t *testing.T) { } } +func TestGuardOwedAbsoluteMeasuredCommandContext(t *testing.T) { + root, file, run := staleGuardFixture(t) + at, err := verification.SnapshotAt(root) + if err != nil { + t.Fatal(err) + } + t.Setenv("GOOS", "linux") + t.Setenv("GOWORK", "off") + failed := 7 + old := verification.Check{Args: []string{"/usr/local/go/bin/go", "test", "./..."}, Command: "/usr/local/go/bin/go test ./...", Cwd: root, Context: verification.CheckContext(), Exit: &failed, Before: at, After: at, ID: "saved", Source: "old"} + if err := verification.Inherit(file, []verification.Check{old}); err != nil { + t.Fatal(err) + } + door := &receiptStageDoor{StageDoor: &repairReportDoor{}, file: file, dir: root, requireFresh: true, requireAnyCheck: true} + t.Cleanup(door.closeVerification) + // A rerun under a different target context must keep the saved failure owed. + t.Setenv("GOOS", "darwin") + run("go test ./...") + if err := door.Report(t.Context(), factory.StageResult{Done: true}); err == nil || !strings.Contains(err.Error(), "'go' 'test' './...'") { + t.Fatalf("different GOOS context must keep saved failure owed: %v", err) + } + // A portable rerun in the matching non-empty context clears it. + t.Setenv("GOOS", "linux") + run("go test ./...") + if err := door.Report(t.Context(), factory.StageResult{Done: true}); err != nil { + t.Fatalf("matching context portable rerun did not clear saved absolute failure: %v", err) + } +} + func TestGuardOwedAbsoluteUnmeasuredCommand(t *testing.T) { root, file, run := staleGuardFixture(t) guardUnrunJournal(t, file, "/usr/local/go/bin/go test ./...", "refused") diff --git a/internal/factory/verification/go_requirements.go b/internal/factory/verification/go_requirements.go index 2464f7d2e7..83a571c911 100644 --- a/internal/factory/verification/go_requirements.go +++ b/internal/factory/verification/go_requirements.go @@ -2,7 +2,6 @@ package verification import ( "encoding/json" - "os" "path" "slices" "sort" @@ -210,11 +209,7 @@ func CoversUnrun(command, cwd string, pass Check) bool { if len(args) == 0 || args[0] != "go" { return false } - var context []string - for _, name := range checkContextNames { - context = append(context, os.Getenv(name)) - } - old, ok := goRequirement(Check{Args: args, Cwd: cwd, Context: context}) + old, ok := goRequirement(Check{Args: args, Cwd: cwd, Context: CheckContext()}) if !ok || pass.Cwd != cwd || pass.Diagnostic || pass.Problem != "" || pass.Exit == nil || *pass.Exit != 0 || pass.Before.Head == "" || pass.Before != pass.After { return false } diff --git a/internal/factory/verification/receipts.go b/internal/factory/verification/receipts.go index 11409836e1..d35ba25c55 100644 --- a/internal/factory/verification/receipts.go +++ b/internal/factory/verification/receipts.go @@ -26,6 +26,16 @@ const SetupFailurePrefix = "Could not record verification:" var checkContextNames = [...]string{"GOOS", "GOARCH", "GOFLAGS", "CGO_ENABLED", "GOWORK", "GORACE", "NODE_ENV", "PYTEST_ADDOPTS"} +// CheckContext captures the execution-scope environment values recorded for +// observed checks. +func CheckContext() []string { + ctx := make([]string, len(checkContextNames)) + for i, name := range checkContextNames { + ctx[i] = os.Getenv(name) + } + return ctx +} + const originalCommandPrefix = "# codeaf-check-original:" const observerShellGuard = `if [ -n "${BASH_VERSION-}" ]; then` From 2026e9598eebfa2866c41ea8b46cc0912b4313b3 Mon Sep 17 00:00:00 2001 From: Gaoshou101 Date: Wed, 2 Dec 2026 13:15:56 +0800 Subject: [PATCH 2/2] docs(changelog): add unreleased entry for #3017 --- .../unreleased/3017-guard-owed-absolute-context.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 docs/changes/unreleased/3017-guard-owed-absolute-context.md diff --git a/docs/changes/unreleased/3017-guard-owed-absolute-context.md b/docs/changes/unreleased/3017-guard-owed-absolute-context.md new file mode 100644 index 0000000000..26e9e0af60 --- /dev/null +++ b/docs/changes/unreleased/3017-guard-owed-absolute-context.md @@ -0,0 +1,8 @@ +--- +kind: internal +title: TestGuardOwedAbsoluteMeasuredCommand seeds inherited context from CheckContext +pr: 3017 +surface: [factory] +invalidates: + - "Was true: `TestGuardOwedAbsoluteMeasuredCommand` seeded its inherited synthetic check with an all-empty `make([]string, 8)` context, so any runner with `GOOS`, `GOARCH`, `GOFLAGS`, `CGO_ENABLED`, `GOWORK`, `GORACE`, `NODE_ENV`, or `PYTEST_ADDOPTS` exported saw an environment mismatch on dev. Is true now: `verification.CheckContext()` supplies the active execution-scope context so the test passes on any clean checkout regardless of those host variables." +---