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
fix(selfupdate): pick an update artifact the host can actually install
shukieshah10bbb4ef156f
diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex cc29a356..e3a8e845 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -225,19 +225,32 @@ jobs:# download them and checksum the bytes that were actually released rather than# trusting a value computed somewhere else.mkdir -p dist/linux- for f in "beacon_${VERSION}_linux_amd64.deb" "beacon_${VERSION}_linux_arm64.deb"; do- if gh release download "$TAG" --repo "$REPO" --pattern "$f" --dir dist/linux; then- echo "downloaded $f"- else- echo "::warning::$f not found on the release; omitting it from update-manifest.json"- fi+ # Both formats, both arches. A single artifact per architecture is not enough: .deb and+ # .rpm hosts share an architecture, and handing an RPM host a .deb makes `endpoint update+ # --apply` fail at the install step after downloading and verifying.+ for arch in amd64 arm64; do+ for fmt in deb rpm; do+ f="beacon_${VERSION}_linux_${arch}.${fmt}"+ if gh release download "$TAG" --repo "$REPO" --pattern "$f" --dir dist/linux; then+ echo "downloaded $f"+ else+ echo "::warning::$f not found on the release; omitting it from update-manifest.json"+ fi+ donedoneset -- "$VERSION" "$TAG" "$REPO" "$APPLE_TEAM_ID" \"darwin_arm64=$(ls dist/macos/*-${VERSION}-arm64.pkg)"for arch in amd64 arm64; do- deb="dist/linux/beacon_${VERSION}_linux_${arch}.deb"- [ -f "$deb" ] && set -- "$@" "linux_${arch}=$deb"+ for fmt in deb rpm; do+ pkg="dist/linux/beacon_${VERSION}_linux_${arch}.${fmt}"+ # Written as an if, not `[ -f x ] && set -- ...`: under `set -e` a failing AND-list at+ # the end of a loop body exits the step, which would turn the missing-artifact warning+ # above into a hard failure of the whole release.+ if [ -f "$pkg" ]; then+ set -- "$@" "linux_${arch}_${fmt}=$pkg"+ fi+ donedonesh packaging/release/gen-update-manifest.sh "$@" > dist/macos/update-manifest.jsoncat dist/macos/update-manifest.jsondiff --git a/cli/beacon/internal/endpoint/selfupdate/apply.go b/cli/beacon/internal/endpoint/selfupdate/apply.goindex c62f0c65..43e64f04 100644--- a/cli/beacon/internal/endpoint/selfupdate/apply.go+++ b/cli/beacon/internal/endpoint/selfupdate/apply.go@@ -141,10 +141,10 @@ func (a *Applier) Apply(ctx context.Context) (ApplyResult, error) {}result.ToVersion = manifest.Version- artifact, ok := manifest.ArtifactFor(updatecheck.RuntimeArchKey())+ artifact, key, ok := selectArtifact(manifest, a.AllowInsecureTest)if !ok {- a.emit(false, result, fmt.Sprintf("no update artifact for %s", updatecheck.RuntimeArchKey()))- return result, fmt.Errorf("no update artifact for %s", updatecheck.RuntimeArchKey())+ a.emit(false, result, fmt.Sprintf("no update artifact this host can install (looked for %s)", key))+ return result, fmt.Errorf("no update artifact this host can install (looked for %s)", key)}// Serialize concurrent updaters.@@ -448,10 +448,20 @@ func linuxPackageExt() string {return ".rpm"}// Neither is present, so no native install is possible. Assume deb so the resulting failure- // names dpkg, which is actionable, rather than the absence of the macOS installer.+ // names dpkg, which is actionable, rather than the absence of the macOS installer. Callers that+ // need to know whether an install is possible at all must ask hasPackageManager, not this.return ".deb"}+// hasPackageManager reports whether this host has any native package tool.+func hasPackageManager() bool {+ if _, err := exec.LookPath("dpkg"); err == nil {+ return true+ }+ _, err := exec.LookPath("rpm")+ return err == nil+}+// installDir is the root of the installed tree under the active prefix.func (a *Applier) installDir() string {return filepath.Join(a.prefix(), "opt", "beacon")@@ -579,6 +589,58 @@ func versionLineMatches(out, want string) bool {return false}+// selectArtifact picks the release artifact this host can actually install.+//+// One key per architecture is not enough on Linux, because two incompatible package formats share+// an architecture. A manifest that published only the .deb for linux_arm64 would hand a Fedora host+// a .deb, and installerCommand would dutifully run dpkg -- which is not installed there. The update+// would fail at the last step, after downloading and verifying, with an error about a missing+// program rather than about the wrong format.+//+// So a format-qualified key is preferred (linux_arm64_rpm), and the bare architecture key is+// accepted only when what it points at is installable here. That keeps a single-format manifest+// working on the platform it was built for while refusing, clearly, on the platform it was not.+// extractsTarballs is the applier's AllowInsecureTest seam: with it on, `install` expands a tarball+// into a temp prefix, so a tarball artifact is genuinely installable. It is threaded through rather+// than assumed, because the answer to "can this host install this" differs between the two modes and+// guessing either way breaks something real.+func selectArtifact(m updatecheck.UpdateManifest, extractsTarballs bool) (updatecheck.Artifact, string, bool) {+ arch := updatecheck.RuntimeArchKey()+ if runtime.GOOS != "linux" {+ a, ok := m.ArtifactFor(arch)+ return a, arch, ok+ }+ format := strings.TrimPrefix(linuxPackageExt(), ".")+ qualified := arch + "_" + format+ if a, ok := m.ArtifactFor(qualified); ok {+ return a, qualified, true+ }+ if a, ok := m.ArtifactFor(arch); ok && installableHere(a.URL, extractsTarballs) {+ return a, arch, true+ }+ return updatecheck.Artifact{}, qualified, false+}++// installableHere reports whether a URL names something this host can install.+//+// A native package qualifies only in the format this host has a tool for. A tarball qualifies only+// when the caller extracts tarballs: in production `install` does not, so a .tar.gz reaches+// installerCommand's default branch and is handed to the macOS `installer`, which cannot open it and+// does not exist on Linux at all. Accepting one unconditionally would mean downloading and+// checksum-verifying an artifact and only then discovering it is uninstallable.+func installableHere(url string, extractsTarballs bool) bool {+ if strings.HasSuffix(url, ".tar.gz") || strings.HasSuffix(url, ".tgz") {+ return extractsTarballs+ }+ if !strings.HasSuffix(url, ".deb") && !strings.HasSuffix(url, ".rpm") {+ return false+ }+ // linuxPackageExt falls back to .deb when neither tool exists, so the absence of a package+ // manager has to be checked separately or a .deb would look installable on a host with no dpkg+ // at all.+ return hasPackageManager() && strings.HasSuffix(url, linuxPackageExt())+}+// packageExt names the staged artifact so the installer that runs on it can dispatch correctly.//// The URL wins when it already carries a recognised extension, because the manifest is thediff --git a/cli/beacon/internal/endpoint/selfupdate/apply_test.go b/cli/beacon/internal/endpoint/selfupdate/apply_test.goindex 31beaa5e..b259aa04 100644--- a/cli/beacon/internal/endpoint/selfupdate/apply_test.go+++ b/cli/beacon/internal/endpoint/selfupdate/apply_test.go@@ -46,14 +46,41 @@ func makeBeaconTarball(t *testing.T, version string) ([]byte, string) {// manifestServer serves a manifest pointing at /artifact.tar.gz and the artifact.func manifestServer(t *testing.T, version, sha string, artifact []byte) *httptest.Server {+ return manifestServerNamed(t, version, sha, artifact, "/artifact.tar.gz")+}++// nativeManifestServer publishes the artifact under a name and key this host would really install,+// for the tests that exercise the production install path rather than the tarball seam.+//+// On Linux the artifact key carries the package format, because .deb and .rpm hosts share an+// architecture. A tarball name would resolve to no artifact at all there, so a test written for+// macOS's `installer` would fail before reaching what it meant to check.+func nativeManifestServer(t *testing.T, version, sha string, artifact []byte) *httptest.Server {+ t.Helper()+ if runtime.GOOS != "linux" {+ return manifestServerNamed(t, version, sha, artifact, "/artifact.pkg")+ }+ ext := linuxPackageExt()+ return manifestServerNamed(t, version, sha, artifact, "/artifact"+ext,+ updatecheck.RuntimeArchKey()+"_"+strings.TrimPrefix(ext, "."))+}++// manifestServerNamed serves one artifact at the given path, keyed by archKey (defaulting to this+// platform's).+func manifestServerNamed(t *testing.T, version, sha string, artifact []byte, artifactPath string,+ archKey ...string) *httptest.Server {t.Helper()+ key := updatecheck.RuntimeArchKey()+ if len(archKey) > 0 {+ key = archKey[0]+ }mux := http.NewServeMux()srv := httptest.NewServer(mux)mux.HandleFunc("/manifest.json", func(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, `{"schema":1,"version":%q,"team_id":"TEAMID","artifacts":{%q:{"url":%q,"sha256":%q}}}`,- version, updatecheck.RuntimeArchKey(), srv.URL+"/artifact.tar.gz", sha)+ version, key, srv.URL+artifactPath, sha)})- mux.HandleFunc("/artifact.tar.gz", func(w http.ResponseWriter, r *http.Request) {+ mux.HandleFunc(artifactPath, func(w http.ResponseWriter, r *http.Request) {_, _ = w.Write(artifact)})return srv@@ -256,6 +283,13 @@ func TestApplyHealthFailRollsBack(t *testing.T) {}func TestApplyGatekeeperAbortsBeforeInstall(t *testing.T) {+ if runtime.GOOS != "darwin" {+ // Gatekeeper is an Apple mechanism, and verifyGatekeeper is skipped elsewhere by design, so+ // there is no signature step for an installer to run after. The non-Darwin contract --+ // checksum enforcement still aborts before any installer -- is asserted separately in+ // TestApplyChecksumMismatchAbortsBeforeInstall.+ t.Skip("Gatekeeper verification is macOS-only")+ }artifact, sha := makeBeaconTarball(t, "9.9.9")srv := manifestServer(t, "9.9.9", sha, artifact)defer srv.Close()@@ -331,7 +365,9 @@ func TestVerifyGatekeeperFailsStaplerValidationFailure(t *testing.T) {func TestApplyRestartsCollectorBeforeHealthCheck(t *testing.T) {artifact, sha := makeBeaconTarball(t, "9.9.9")- srv := manifestServer(t, "9.9.9", sha, artifact)+ // The production install path, not the extraction seam -- the stubbed runner below stands in+ // for the platform's installer. So the artifact has to be named like one this host installs.+ srv := nativeManifestServer(t, "9.9.9", sha, artifact)defer srv.Close()a := NewApplier("0.0.1")@@ -350,7 +386,7 @@ func TestApplyRestartsCollectorBeforeHealthCheck(t *testing.T) {switch filepath.Base(name) {case "pkgutil":return "Developer ID Installer: Example (TEAMID)", nil- case "stapler", "spctl", "installer":+ case "stapler", "spctl", "installer", "dpkg", "rpm":return "", nilcase "beacon":return "beacon version 9.9.9 (new) built on test", nil@@ -563,3 +599,112 @@ func TestPackageExtPrefersTheURLThenThePlatform(t *testing.T) {t.Errorf("on linux the fallback should be a native package format, got %q", got)}}++// Two package formats share one architecture on Linux, so an architecture key alone cannot say+// which artifact a host can install. These pin the behavior that keeps a Fedora host from+// downloading, verifying, and then failing to install a .deb.+func TestSelectArtifactPrefersTheFormatQualifiedKey(t *testing.T) {+ if runtime.GOOS != "linux" {+ t.Skip("format-qualified keys only exist on Linux")+ }+ arch := updatecheck.RuntimeArchKey()+ format := strings.TrimPrefix(linuxPackageExt(), ".")+ m := updatecheck.UpdateManifest{Artifacts: map[string]updatecheck.Artifact{+ arch: {URL: "https://x/wrong.tar.gz", SHA256: strings.Repeat("a", 64)},+ arch + "_" + format: {URL: "https://x/right." + format, SHA256: strings.Repeat("b", 64)},+ arch + "_somethingelse": {URL: "https://x/other.pkg", SHA256: strings.Repeat("c", 64)},+ }}+ got, key, ok := selectArtifact(m, false)+ if !ok {+ t.Fatal("a manifest with a format-qualified key must resolve")+ }+ if key != arch+"_"+format {+ t.Errorf("key = %q, want the format-qualified one", key)+ }+ if !strings.HasSuffix(got.URL, "."+format) {+ t.Errorf("URL = %q, want the artifact for this host's package format", got.URL)+ }+}++func TestSelectArtifactRefusesAForeignPackageFormat(t *testing.T) {+ if runtime.GOOS != "linux" || !hasPackageManager() {+ t.Skip("needs a Linux host with a package manager")+ }+ // Whichever format this host cannot install.+ foreign := ".rpm"+ if linuxPackageExt() == ".rpm" {+ foreign = ".deb"+ }+ m := updatecheck.UpdateManifest{Artifacts: map[string]updatecheck.Artifact{+ updatecheck.RuntimeArchKey(): {URL: "https://x/pkg" + foreign, SHA256: strings.Repeat("a", 64)},+ }}+ if _, key, ok := selectArtifact(m, false); ok {+ t.Errorf("resolved a %s artifact on a host that cannot install one", foreign)+ } else if !strings.Contains(key, strings.TrimPrefix(linuxPackageExt(), ".")) {+ t.Errorf("the refusal names %q; it should name the format this host wanted", key)+ }+}++// A tarball under the architecture key must be refused, not accepted. `install` extracts a tarball+// only under the test seam; in production it goes to the macOS `installer`, which does not exist on+// Linux. Refusing here means the failure arrives before a download rather than after one.+func TestSelectArtifactRefusesATarballOnLinux(t *testing.T) {+ if runtime.GOOS != "linux" {+ t.Skip("Linux-only fallback")+ }+ m := updatecheck.UpdateManifest{Artifacts: map[string]updatecheck.Artifact{+ updatecheck.RuntimeArchKey(): {URL: "https://x/beacon.tar.gz", SHA256: strings.Repeat("a", 64)},+ }}+ if _, _, ok := selectArtifact(m, false); ok {+ t.Error("a tarball is not installable in production and must not resolve")+ }+ // With the extraction seam on, the same artifact is installable -- which is why the answer is+ // threaded through rather than decided from the host alone.+ if _, _, ok := selectArtifact(m, true); !ok {+ t.Error("a tarball must resolve when the caller extracts tarballs")+ }+}++func TestSelectArtifactOnDarwinUsesTheArchKeyUnchanged(t *testing.T) {+ if runtime.GOOS != "darwin" {+ t.Skip("darwin-only behavior")+ }+ arch := updatecheck.RuntimeArchKey()+ m := updatecheck.UpdateManifest{Artifacts: map[string]updatecheck.Artifact{+ arch: {URL: "https://x/BeaconEndpointAgent.pkg", SHA256: strings.Repeat("a", 64)},+ }}+ got, key, ok := selectArtifact(m, false)+ if !ok || key != arch || !strings.HasSuffix(got.URL, ".pkg") {+ t.Fatalf("selectArtifact = %+v, %q, %v; macOS must be unchanged", got, key, ok)+ }+}++// The platform-independent half of the guarantee TestApplyGatekeeperAbortsBeforeInstall makes on+// macOS: nothing is installed unless the bytes matched. Gatekeeper adds assurance on macOS and has+// no Linux equivalent, but SHA-256 is enforced everywhere, and this asserts it on every platform+// rather than leaving Linux with no coverage of "verify before install".+func TestApplyChecksumMismatchAbortsBeforeInstall(t *testing.T) {+ artifact, _ := makeBeaconTarball(t, "9.9.9")+ // A syntactically valid checksum that does not match the artifact.+ srv := manifestServer(t, "9.9.9", strings.Repeat("0", 64), artifact)+ defer srv.Close()++ a := NewApplier("0.0.1")+ a.ManifestURL = srv.URL + "/manifest.json"+ a.StageDir = t.TempDir()+ a.InstallPrefix = t.TempDir()+ a.LogPath = filepath.Join(t.TempDir(), "runtime.jsonl")+ a.SkipGatekeeper = true // isolate the checksum: this test is not about signatures+ var calls []string+ a.run = func(ctx context.Context, name string, args ...string) (string, error) {+ calls = append(calls, name)+ return "", nil+ }++ if _, err := a.Apply(context.Background()); err == nil {+ t.Fatal("a checksum mismatch must fail the update")+ }+ if len(calls) != 0 {+ t.Errorf("nothing may run after a checksum mismatch; calls=%v", calls)+ }+}diff --git a/packaging/release/gen-update-manifest.sh b/packaging/release/gen-update-manifest.shindex 4e658344..cd6d5e2c 100755--- a/packaging/release/gen-update-manifest.sh+++ b/packaging/release/gen-update-manifest.sh@@ -4,12 +4,17 @@# Usage:# gen-update-manifest.sh VERSION TAG REPO TEAM_ID \# darwin_arm64=/path/BeaconEndpointAgent-1.0.6-arm64.pkg \-# linux_amd64=/path/beacon_1.0.6_linux_amd64.deb \-# linux_arm64=/path/beacon_1.0.6_linux_arm64.deb+# linux_amd64_deb=/path/beacon_1.0.6_linux_amd64.deb \+# linux_amd64_rpm=/path/beacon_1.0.6_linux_amd64.rpm## Keys are GOOS_GOARCH, matching updatecheck.RuntimeArchKey() on the consumer side. A bare arch# (arm64=...) is still accepted and treated as darwin, so existing release tooling keeps working.#+# Linux keys carry the package format as well, because .deb and .rpm hosts share an architecture and+# an unqualified key cannot say which of them an artifact is for. The consumer looks for+# linux_<arch>_<format> first and only falls back to linux_<arch> when what it points at is+# installable on that host.+## Lives under packaging/release/ rather than packaging/macos/ because it now describes artifacts# for both platforms.#