The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
ci: cut CI wall time and runner minutes (#1160)
kyle-elliott-asymptote235572ed6fa2
diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.ymlindex 9ed5c117..3bb00d3b 100644--- a/.github/actions/setup-go/action.yml+++ b/.github/actions/setup-go/action.yml@@ -1,8 +1,8 @@name: Set up Godescription: >- actions/setup-go plus the two things this repository needs on top of it: a- module cache that actually restores, and a module download that survives a- transient proxy timeout.+ actions/setup-go plus the three things this repository needs on top of it: a+ build cache that keeps up with the code, caches that only main writes, and a+ module download that survives a transient proxy timeout.inputs:go-version-file:@@ -16,43 +16,90 @@ inputs:front so a slow proxy fails a step that retries rather than a test stepthat does not.required: true+ cache-key:+ description: >+ Extra build-cache discriminator for matrix legs of one job that compile+ different things, such as one cross-build per target. The job id is+ already part of the key.+ required: false+ default: ""runs:using: compositesteps:- # actions/setup-go looks for go.sum in the working directory and walks up.- # This repository keeps five modules in subdirectories and none at the- # root, so the default lookup finds nothing, logs "Restore cache failed:- # Dependencies file is not found", and every job re-downloads every module- # from proxy.golang.org on every run. Naming the go.sum files explicitly is- # what turns the module cache on at all.+ # The caches are handled below rather than by actions/setup-go. Its cache is+ # keyed on go.sum alone and saved only on a miss, so once go.sum stops+ # changing the build cache it restores is frozen at that commit: every+ # source change since is recompiled and every test re-run, on every job.+ # Two caches replace it:#- # The list is per-job rather than all five everywhere on purpose: it is- # hashed into the cache key, so a job that needs the collector's OTel tree- # gets an entry of its own instead of restoring -- and then never- # re-saving over -- the smaller entry written by a job that only builds the- # CLI.- - id: sums+ # modules keyed on the go.sum files, like before. Downloaded modules+ # only change when go.sum does.+ # build keyed on the commit, restored from the newest earlier entry+ # for the same job, so it tracks main and `go test` can reuse+ # results for packages a change did not touch.+ #+ # Only pushes to main save. A pull request restores main's entries and+ # writes nothing: per-PR entries of 500-800 MB each are what kept the+ # repository at its 10 GB cache limit and evicting main's.+ - uses: actions/setup-go@v7+ with:+ go-version-file: ${{ inputs.go-version-file }}+ cache: false++ - id: keysshell: bashenv:MODULES: ${{ inputs.modules }}+ JOB: ${{ github.job }}${{ inputs.cache-key && format('-{0}', inputs.cache-key) || '' }}run: |set -euo pipefail+ sums=()+ while read -r dir; do+ dir="${dir%/}"+ [ -n "$dir" ] || continue+ test -f "$dir/go.sum" || { echo "no go.sum in $dir" >&2; exit 1; }+ sums+=("$dir/go.sum")+ done <<< "$MODULES"+ # git is on every runner image; sha256sum and shasum are not both everywhere.+ sum_hash=$(cat "${sums[@]}" | git hash-object --stdin)+ # ImageOS (ubuntu24, macos15, win25) because a new runner image can ship a different C+ # toolchain, and cgo objects built against the old one must not be reused.+ base="go-${RUNNER_OS}-${RUNNER_ARCH}-${ImageOS:-unknown}-$(go env GOVERSION)"{- echo 'paths<<SUMS_EOF'- while read -r dir; do- dir="${dir%/}"- [ -n "$dir" ] || continue- test -f "$dir/go.sum" || { echo "no go.sum in $dir" >&2; exit 1; }- echo "$dir/go.sum"- done <<< "$MODULES"- echo 'SUMS_EOF'+ echo "gomodcache=$(go env GOMODCACHE)"+ echo "gocache=$(go env GOCACHE)"+ echo "mod-key=${base}-mod-${sum_hash}"+ echo "mod-prefix=${base}-mod-"+ echo "build-key=${base}-build-${JOB}-${GITHUB_SHA}"+ echo "build-prefix=${base}-build-${JOB}-"} >> "$GITHUB_OUTPUT"- - uses: actions/setup-go@v5+ - if: github.ref == 'refs/heads/main'+ uses: actions/cache@v6with:- go-version-file: ${{ inputs.go-version-file }}- cache-dependency-path: ${{ steps.sums.outputs.paths }}+ path: ${{ steps.keys.outputs.gomodcache }}+ key: ${{ steps.keys.outputs.mod-key }}+ restore-keys: ${{ steps.keys.outputs.mod-prefix }}+ - if: github.ref != 'refs/heads/main'+ uses: actions/cache/restore@v6+ with:+ path: ${{ steps.keys.outputs.gomodcache }}+ key: ${{ steps.keys.outputs.mod-key }}+ restore-keys: ${{ steps.keys.outputs.mod-prefix }}++ - if: github.ref == 'refs/heads/main'+ uses: actions/cache@v6+ with:+ path: ${{ steps.keys.outputs.gocache }}+ key: ${{ steps.keys.outputs.build-key }}+ restore-keys: ${{ steps.keys.outputs.build-prefix }}+ - if: github.ref != 'refs/heads/main'+ uses: actions/cache/restore@v6+ with:+ path: ${{ steps.keys.outputs.gocache }}+ key: ${{ steps.keys.outputs.build-key }}+ restore-keys: ${{ steps.keys.outputs.build-prefix }}# A cache hit makes this a no-op. On a miss it is where the download# happens, and it retries: `go test` and `go build` fetch modules inlinediff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex ac99dcab..0835b774 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -9,6 +9,13 @@ on:permissions:contents: read+# A newer push to the same pull request makes the running checks moot, so they are cancelled+# rather than left to hold Windows and macOS runners for ten minutes. Pushes to main are keyed by+# commit, so every main commit still gets a full run.+concurrency:+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}+jobs:typescript-sdk:runs-on: ubuntu-latest@@ -17,8 +24,8 @@ jobs:matrix:node-version: [20, 22, 24]steps:- - uses: actions/checkout@v4- - uses: actions/setup-node@v4+ - uses: actions/checkout@v7+ - uses: actions/setup-node@v7with:node-version: ${{ matrix.node-version }}cache: npm@@ -65,8 +72,8 @@ jobs:env:BROWSER_CHANNEL: ${{ matrix.browser }}steps:- - uses: actions/checkout@v4- - uses: actions/setup-node@v4+ - uses: actions/checkout@v7+ - uses: actions/setup-node@v7with:node-version: 22cache: npm@@ -137,7 +144,7 @@ jobs:- name: Cache Playwright browsersif: matrix.browser == 'chromium'id: pw-cache- uses: actions/cache@v4+ uses: actions/cache@v6with:path: ~/.cache/ms-playwrightkey: playwright-${{ runner.os }}-${{ steps.pw.outputs.version }}@@ -176,7 +183,7 @@ jobs:# retains on failure; the HTML report alone is not enough to debug from.- name: Upload Playwright report and tracesif: failure()- uses: actions/upload-artifact@v4+ uses: actions/upload-artifact@v7with:name: browser-extension-playwright-report-${{ matrix.browser }}path: |@@ -188,7 +195,7 @@ jobs:go-test:runs-on: macos-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: oven-sh/setup-bun@v2with:bun-version: latest@@ -281,7 +288,7 @@ jobs:beacon-sandbox:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:go-version-file: beacon-sandbox/go.mod@@ -303,15 +310,22 @@ jobs:terraform:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4- - uses: hashicorp/setup-terraform@v3+ - uses: actions/checkout@v7+ - uses: hashicorp/setup-terraform@v4with:terraform_version: 1.14.3terraform_wrapper: false# Hermetic: the tests mock the AWS and Google providers, so no cloud account or# credentials are involved. Only the provider downloads reach the network.- name: Check Terraform modules+ env:+ # Eight inits download the same AWS and Google providers; one shared directory makes that+ # two downloads. There are no committed lock files for the cache to conflict with: each+ # init writes a fresh one, so the cache is allowed to satisfy it.+ TF_PLUGIN_CACHE_DIR: ${{ runner.temp }}/terraform-plugin-cache+ TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE: "true"run: |+ mkdir -p "$TF_PLUGIN_CACHE_DIR"terraform fmt -check -recursive -diff terraformfor module in terraform/*/; doterraform -chdir="$module" init -backend=false -input=false@@ -345,9 +359,11 @@ jobs:goarch: amd64make_target: build-windows-amd64steps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:+ # Each target compiles a different package set, so each keeps its own build cache.+ cache-key: ${{ matrix.goos }}-${{ matrix.goarch }}modules: |cli/beaconcli/beacon-hooks@@ -401,14 +417,14 @@ jobs:run:working-directory: cli/beacon/internal/endpoint/dashboard/testdata/lens-e2esteps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:modules: |cli/beaconcli/beacon-hookspkg/asymptoteobserve- - uses: actions/setup-node@v4+ - uses: actions/setup-node@v7with:node-version: 22cache: npm@@ -424,7 +440,7 @@ jobs:run: npx playwright test- name: Upload Playwright report and tracesif: failure()- uses: actions/upload-artifact@v4+ uses: actions/upload-artifact@v7with:name: lens-e2e-playwright-reportpath: |@@ -436,7 +452,7 @@ jobs:linux-test:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:modules: |@@ -480,7 +496,23 @@ jobs:run:shell: pwshsteps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7+ # Two settings, both measured on this runner (#1160):+ #+ # - TEMP, TMP, GOTMPDIR and GOCACHE go to RUNNER_TEMP on D:. TEMP and the Go cache default to+ # C:, the runner's OS disk, where a small-file create or rename costs 0.6 ms against about 0.1+ # ms on D: and a stat of a missing file 0.19 ms against 0.007 ms -- and every t.TempDir(),+ # every SQLite fixture and every test-cache fingerprint is made of those operations. Written+ # to GITHUB_ENV before setup-go, so the build cache it restores and saves follows to D: too.+ # - Every go test here runs with -count=1. Go's test-result cache fingerprints each file and+ # directory a test touched, before the run to look the result up and after it to store it.+ # On this runner that bookkeeping cost about 60 s per session package against 14 s of tests,+ # and checking a fully cached result took as long as running the tests again -- so it never+ # pays here. The Linux and macOS jobs keep test caching, where it is cheap.+ - name: Keep temporary files and the Go build cache on D+ run: |+ foreach ($name in 'TEMP', 'TMP', 'GOTMPDIR') { "$name=$env:RUNNER_TEMP" >> $env:GITHUB_ENV }+ "GOCACHE=$(Join-Path $env:RUNNER_TEMP go-build)" >> $env:GITHUB_ENV- uses: ./.github/actions/setup-gowith:modules: |@@ -521,7 +553,7 @@ jobs:- name: Test the ported packages on Windowsworking-directory: cli/beaconrun: |- go test `+ go test -count=1 `. `./cmd/... `./internal/account/... `@@ -584,23 +616,23 @@ jobs:- name: Test the ported packages with the race detectorworking-directory: cli/beaconrun: |- go test -race `+ go test -race -count=1 `./internal/endpoint/writer/... `./internal/endpoint/inventory/... `./internal/endpoint/service/...- name: Test hook adapter on Windowsworking-directory: cli/beacon-hooks- run: go test ./...+ run: go test -count=1 ./...- name: Test collector exporter on Windowsworking-directory: collector-builder/exporter/beaconjsonexporter- run: go test ./...+ run: go test -count=1 ./...- name: Test observe SDK and threat rules on Windowsworking-directory: pkg/asymptoteobserve- run: go test ./...+ run: go test -count=1 ./...# Runs the generated-PowerShell parse test, which skips on a machine without pwsh.- name: Test the sandbox harness on Windowsworking-directory: beacon-sandbox- run: go test ./...+ run: go test -count=1 ./...# Exercises the real systemd install path: unit generation, systemctl enable/active,# Restart=always actually restarting, and uninstall leaving nothing behind. Needs systemd as@@ -609,7 +641,7 @@ jobs:linux-systemd-smoke:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:# The exporter is listed because builder.yaml compiles it into the@@ -645,7 +677,7 @@ jobs:linux-package-smoke:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:# The exporter is listed because builder.yaml compiles it into the@@ -663,25 +695,23 @@ jobs:cd collector-builder# builder mkdirs its output path without -p, so the parent has to exist.mkdir -p dist- for arch in amd64 arm64; do- GOOS=linux GOARCH=$arch CGO_ENABLED=0 "$(go env GOPATH)/bin/builder" --config builder.yaml- mkdir -p dist/beacon-otelcol/linux_$arch- cp dist/beacon-otelcol/beacon-otelcol dist/beacon-otelcol/linux_$arch/beacon-otelcol- done- # The goreleaser before-hook stages every target, so each one must exist here. These are- # stand-ins: this job checks that packaging works, not that the binaries run, so a Linux- # build stood in a darwin directory is enough to exercise the archive and package steps.- for arch in amd64 arm64; do- mkdir -p dist/beacon-otelcol/darwin_$arch- cp dist/beacon-otelcol/linux_$arch/beacon-otelcol dist/beacon-otelcol/darwin_$arch/beacon-otelcol+ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 "$(go env GOPATH)/bin/builder" --config builder.yaml+ mkdir -p dist/beacon-otelcol/linux_amd64+ cp dist/beacon-otelcol/beacon-otelcol dist/beacon-otelcol/linux_amd64/beacon-otelcol+ # The goreleaser before-hook stages every release target, so each one must exist here.+ # Only linux/amd64 is built and packaged below; the rest are stand-ins that are copied+ # and never used.+ for target in linux_arm64 darwin_amd64 darwin_arm64; do+ mkdir -p dist/beacon-otelcol/$target+ cp dist/beacon-otelcol/linux_amd64/beacon-otelcol dist/beacon-otelcol/$target/beacon-otelcoldone- # Windows is amd64 only, and carries the .exe the release archive ships -- the same name+ # Windows carries the .exe the release archive ships -- the same name# build-release-collectors.sh stages, so the before-hook finds it under either script.mkdir -p dist/beacon-otelcol/windows_amd64cp dist/beacon-otelcol/linux_amd64/beacon-otelcol dist/beacon-otelcol/windows_amd64/beacon-otelcol.exe# The real Vector build, not a stand-in: the smoke test runs it and checks it is static.# Cached by the script's hash, which is where the version is pinned.- - uses: actions/cache@v4+ - uses: actions/cache@v6id: vector-cachewith:path: cli/beacon/release-vector@@ -689,13 +719,22 @@ jobs:- name: Download Vector for the Linux packagesif: steps.vector-cache.outputs.cache-hit != 'true'run: sh packaging/linux/fetch-vector.sh- - uses: goreleaser/goreleaser-action@v6+ # The version release.yml publishes with, so this smoke builds what a release would.+ - uses: goreleaser/goreleaser-action@v7with:- version: latest+ version: "v2.13.3"install-only: true+ # linux/amd64 only: it is the one target the steps below install and run. Building all five+ # here, serially and with -a, took about six of this job's ten minutes for archives and+ # arm64 packages nothing looked at. release-check still builds every target, on every pull+ # request that touches the release config or packaging.- name: Build packagesworking-directory: cli/beacon- run: goreleaser release --snapshot --clean --skip=publish,homebrew,announce --parallelism 1+ run: |+ yq '(.builds[].goos) = ["linux"] | (.builds[].goarch) = ["amd64"] | del(.builds[].ignore)' \+ .goreleaser.yaml > "$RUNNER_TEMP/goreleaser-linux-amd64.yaml"+ goreleaser release --config "$RUNNER_TEMP/goreleaser-linux-amd64.yaml" \+ --snapshot --clean --skip=publish,homebrew,announce --parallelism 1# Every generated Vector pack has to load in the Vector the packages ship. Vector 0.57 and 0.58# changed how ${VAR} expands and broke most of them with no Beacon change at all. Uses the CLI# goreleaser just built, which carries the real embedded hooks binary.@@ -718,7 +757,7 @@ jobs:package-smoke:runs-on: macos-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:modules: |@@ -758,7 +797,7 @@ jobs:run:shell: pwshsteps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:# The exporter is listed because builder.yaml compiles it into thediff --git a/.github/workflows/claude-code-review.yml b/.github/workflows/claude-code-review.ymlindex 10c12c0e..f08c2778 100644--- a/.github/workflows/claude-code-review.yml+++ b/.github/workflows/claude-code-review.yml@@ -25,7 +25,7 @@ jobs:contents: readpull-requests: writesteps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7with:fetch-depth: 1diff --git a/.github/workflows/claude-security-review.yml b/.github/workflows/claude-security-review.ymlindex b2308e4c..67d31ebc 100644--- a/.github/workflows/claude-security-review.yml+++ b/.github/workflows/claude-security-review.yml@@ -26,7 +26,7 @@ jobs:runs-on: ubuntu-latesttimeout-minutes: 30steps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7with:ref: ${{ github.event.pull_request.head.sha || github.sha }}fetch-depth: 2diff --git a/.github/workflows/release-check.yml b/.github/workflows/release-check.ymlindex 6af747fa..6d587e8b 100644--- a/.github/workflows/release-check.yml+++ b/.github/workflows/release-check.yml@@ -18,11 +18,16 @@ on:permissions:contents: read+# A newer push to the same pull request supersedes the running snapshot build.+concurrency:+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}+jobs:release-check:runs-on: ubuntu-lateststeps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7with:fetch-depth: 0- uses: ./.github/actions/setup-go@@ -36,7 +41,7 @@ jobs:cli/beaconcli/beacon-hookscollector-builder/exporter/beaconjsonexporter- - uses: goreleaser/goreleaser-action@v6+ - uses: goreleaser/goreleaser-action@v7with:# Keep formulas working until we intentionally migrate the tap to casks.version: "v2.13.3"diff --git a/.github/workflows/windows-sandbox.yml b/.github/workflows/windows-sandbox.ymlindex 3755defb..af5348ef 100644--- a/.github/workflows/windows-sandbox.yml+++ b/.github/workflows/windows-sandbox.yml@@ -57,7 +57,7 @@ jobs:run:shell: pwshsteps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v7- uses: ./.github/actions/setup-gowith:@@ -505,7 +505,7 @@ jobs:# host-state and argv metadata are recorded even when collection itself failed.- name: Upload collected artifactsif: always()- uses: actions/upload-artifact@v4+ uses: actions/upload-artifact@v7with:name: beacon-sandbox-runspath: beacon-sandbox/runs/diff --git a/cli/beacon/cmd/ci_test.go b/cli/beacon/cmd/ci_test.goindex 313bdc22..9cb8e567 100644--- a/cli/beacon/cmd/ci_test.go+++ b/cli/beacon/cmd/ci_test.go@@ -22,6 +22,7 @@ import (// beacon, and what it passed is parsed by the real command's flags.func TestCIActionPassesUploadsToTheCommandsItRuns(t *testing.T) {testenv.RequirePOSIXExecutableFixtures(t)+ testenv.RequireCommands(t, "bash")data, err := os.ReadFile(filepath.Join("..", "..", "..", "action.yml"))if err != nil {t.Fatalf("read action.yml: %v", err)diff --git a/cli/beacon/cmd/git_test.go b/cli/beacon/cmd/git_test.goindex 90159299..40b1fca4 100644--- a/cli/beacon/cmd/git_test.go+++ b/cli/beacon/cmd/git_test.go@@ -52,9 +52,7 @@ func runGit(t *testing.T, args ...string) (string, error) {// which is how a real invocation finds git's configuration.func gitCommandRepo(t *testing.T) string {t.Helper()- if _, err := exec.LookPath("git"); err != nil {- t.Skip("git not installed")- }+ testenv.RequireCommands(t, "git")home := t.TempDir()testenv.SetHome(t, home)for _, kv := range gitlink.IsolatedEnv(home) {diff --git a/cli/beacon/cmd/history_env_test.go b/cli/beacon/cmd/history_env_test.goindex 7490de74..76735ea8 100644--- a/cli/beacon/cmd/history_env_test.go+++ b/cli/beacon/cmd/history_env_test.go@@ -8,6 +8,7 @@ import ("github.com/asymptote-labs/agent-beacon/cli/beacon/internal/account"endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv")// Every test in this package runs with the local history pointed at a path that does not exist, so@@ -16,13 +17,19 @@ import (// that a runtime-specific environment variable points outside the test's home. The share commands// also see an endpoint that is not connected to Beacon Cloud, whatever the machine's own (user or// system) enrollment and privacy mode, and no BEACON_API_URL, so neither changes what a test sends-// or where; a test that needs a connection sets shareAutoCloud itself.+// or where; a test that needs a connection sets shareAutoCloud itself. PATH is an empty directory,+// so harness discovery finds what a test puts there and nothing the machine has installed; a test+// that runs a real command declares it with testenv.RequireCommands.func TestMain(m *testing.M) {dir, err := os.MkdirTemp("", "beacon-cmd-test")if err != nil {fmt.Fprintln(os.Stderr, err)os.Exit(1)}+ if err := testenv.IsolatePath(dir); err != nil {+ fmt.Fprintln(os.Stderr, err)+ os.Exit(1)+ }_ = os.Setenv(endpointconfig.HistoryStoreEnv, filepath.Join(dir, "absent", "history.db"))_ = os.Setenv(endpointBackfillEnv, "0")// A wizard install under test that reaches the end of a Beacon Cloud connect must not opendiff --git a/cli/beacon/cmd/mcp_connect_e2e_test.go b/cli/beacon/cmd/mcp_connect_e2e_test.goindex 2322b40e..1f9ae967 100644--- a/cli/beacon/cmd/mcp_connect_e2e_test.go+++ b/cli/beacon/cmd/mcp_connect_e2e_test.go@@ -9,6 +9,8 @@ import ("strings""testing""time"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv")// These tests run the harnesses' own CLIs against the configs `beacon mcp connect` writes, in a@@ -21,6 +23,9 @@ func realCLI(t *testing.T, name string) string {if testing.Short() {t.Skip("real-CLI tests are skipped in -short mode")}+ // The point is the CLI as the user installed it, interpreter and all, so this test gets the+ // machine's whole PATH rather than the empty one the package runs with.+ testenv.UseHostPath(t)path, err := exec.LookPath(name)if err != nil {t.Skipf("%s is not installed", name)diff --git a/cli/beacon/internal/testenv/testenv.go b/cli/beacon/internal/testenv/testenv.goindex 78f7737d..72469c8c 100644--- a/cli/beacon/internal/testenv/testenv.go+++ b/cli/beacon/internal/testenv/testenv.go@@ -12,11 +12,65 @@package testenvimport (+ "os"+ "os/exec""path/filepath""runtime"+ "slices"+ "strings""testing")+// hostPath is PATH as the test binary inherited it. Package initialization runs before any+// TestMain, so this is the machine's PATH even after IsolatePath has replaced it.+var hostPath = os.Getenv("PATH")++// IsolatePath points PATH at an empty directory under dir for the rest of the test binary's life.+// Call it from TestMain, after creating dir.+//+// PATH is the other half of what harness discovery reads, alongside the home directory SetHome+// redirects. Inherited, it made results depend on the machine: a discovered agent CLI is run for+// its --version, so the same test took different paths on a developer's laptop and in CI. It also+// made every discovery a stat of each PATH directory for each of some thirty CLI names -- times+// twelve PATHEXT extensions on Windows -- and go test fingerprints every path a test touched for+// its test cache. On a Windows runner that fingerprinting cost about as much as the tests did.+//+// A test that needs a real command declares it with RequireCommands.+func IsolatePath(dir string) error {+ empty := filepath.Join(dir, "empty-path")+ if err := os.MkdirAll(empty, 0o755); err != nil {+ return err+ }+ return os.Setenv("PATH", empty)+}++// RequireCommands gives the test a PATH holding only the directories the machine's PATH resolves+// the named commands to, and skips the test when one is not installed. It is the declared+// exception to IsolatePath, for a test that runs a real git or shell.+func RequireCommands(t *testing.T, names ...string) {+ t.Helper()+ t.Setenv("PATH", hostPath)+ var dirs []string+ for _, name := range names {+ path, err := exec.LookPath(name)+ if err != nil {+ t.Skipf("%s is not installed", name)+ }+ if dir := filepath.Dir(path); !slices.Contains(dirs, dir) {+ dirs = append(dirs, dir)+ }+ }+ t.Setenv("PATH", strings.Join(dirs, string(os.PathListSeparator)))+}++// UseHostPath restores the machine's whole PATH for one test. It is for a test whose point is to+// run software as the user installed it -- an agent CLI that is a script needs its interpreter,+// wherever that lives -- and so cannot name everything it depends on.+func UseHostPath(t *testing.T) {+ t.Helper()+ t.Setenv("PATH", hostPath)+}+// SetHome redirects the user's home directory for the duration of a test, on every platform.//// Both variables are set because different code reaches for different ones: os.UserHomeDir prefersdiff --git a/cli/beacon/internal/testenv/testenv_test.go b/cli/beacon/internal/testenv/testenv_test.goindex b820e029..d6ab6489 100644--- a/cli/beacon/internal/testenv/testenv_test.go+++ b/cli/beacon/internal/testenv/testenv_test.go@@ -2,8 +2,10 @@ package testenvimport ("os"+ "os/exec""path/filepath""runtime"+ "strings""testing")@@ -61,3 +63,44 @@ func TestHasPOSIXFileModesMatchesWhatStatReports(t *testing.T) {t.Fatalf("HasPOSIXFileModes() = %t, but a 0600 file reads back as %o", HasPOSIXFileModes(), info.Mode().Perm())}}++// RequireCommands is only worth having if it narrows: the declared command must resolve to the+// file the machine's PATH finds, a command the test did not declare must not resolve at all, and a+// declared command that is not installed must skip the test rather than let it run without it.+func TestRequireCommandsKeepsOnlyTheDeclaredCommands(t *testing.T) {+ exe := func(dir, name string) string {+ if runtime.GOOS == "windows" {+ name += ".exe"+ }+ path := filepath.Join(dir, name)+ if err := os.WriteFile(path, []byte("stub"), 0o755); err != nil {+ t.Fatal(err)+ }+ return path+ }+ declaredDir, otherDir := t.TempDir(), t.TempDir()+ declared := exe(declaredDir, "beacon-declared")+ exe(otherDir, "beacon-undeclared")+ saved := hostPath+ hostPath = strings.Join([]string{declaredDir, otherDir}, string(os.PathListSeparator))+ t.Cleanup(func() { hostPath = saved })++ t.Run("declared", func(t *testing.T) {+ RequireCommands(t, "beacon-declared")+ if got, err := exec.LookPath("beacon-declared"); err != nil || got != declared {+ t.Fatalf("LookPath(declared) = %q, %v; want %q", got, err, declared)+ }+ if got, err := exec.LookPath("beacon-undeclared"); err == nil {+ t.Fatalf("LookPath(undeclared) = %q; a command the test did not declare must not resolve", got)+ }+ })++ ranWithoutIt := false+ t.Run("missing", func(t *testing.T) {+ RequireCommands(t, "beacon-not-installed")+ ranWithoutIt = true+ })+ if ranWithoutIt {+ t.Fatal("a test that requires a missing command ran instead of skipping")+ }+}diff --git a/pkg/asymptoteobserve/runtimelogaccess_windows_test.go b/pkg/asymptoteobserve/runtimelogaccess_windows_test.goindex 1d2a87ff..4fe1b840 100644--- a/pkg/asymptoteobserve/runtimelogaccess_windows_test.go+++ b/pkg/asymptoteobserve/runtimelogaccess_windows_test.go@@ -58,15 +58,23 @@ func TestTheRestrictedMarkerOnWindows(t *testing.T) {}}-// The marker counts only when no account but SYSTEM and Administrators could have written it. This-// one sits in the test's temp directory, which gives the test's own account full control of its-// files; once that account is no longer trusted the marker is ignored, whether or not the test runs-// elevated (an elevated account's files are owned by Administrators, but the grant remains).+// The marker counts only when no account but SYSTEM and Administrators could have written it. Here+// the test's own account is granted write on the marker and then no longer trusted, so the marker+// must be ignored, whether or not the test runs elevated (an elevated account's files are owned by+// Administrators, but the grant remains). The grant is made explicitly rather than inherited: a temp+// directory under the user's profile grants the account its own files, but one elsewhere -- a CI+// runner's work drive -- may give only SYSTEM and Administrators, and there the marker really is+// trusted.func TestAMarkerAnOrdinaryAccountCouldWriteIsIgnored(t *testing.T) {log := systemLogFixture(t)if err := SetRuntimeLogAccess(log, LogAccessRestricted); err != nil {t.Fatal(err)}+ account := logAccessMarkerTrustedSID+ marker := LogAccessMarkerPath(log)+ if out, err := exec.Command("icacls", marker, "/grant", "*"+account+":(W)").CombinedOutput(); err != nil {+ t.Fatalf("icacls: %v: %s", err, strings.TrimSpace(string(out)))+ }logAccessMarkerTrustedSID = ""if RuntimeLogAccessRestricted(log) {t.Fatal("a marker an ordinary account can write was trusted")