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(vector): use the Homebrew link, not the keg, for beside-the-CLI binaries
shukieshah941adc960bcf
diff --git a/cli/beacon/internal/brewpath/brewpath.go b/cli/beacon/internal/brewpath/brewpath.gonew file mode 100644index 00000000..13efc0a3--- /dev/null+++ b/cli/beacon/internal/brewpath/brewpath.go@@ -0,0 +1,27 @@+// Package brewpath maps a path inside a Homebrew keg to the link that survives upgrades.+package brewpath++import (+ "os"+ "path/filepath"+ "strings"+)++// Stable returns <prefix>/bin/<name> for a path inside a Homebrew Cellar keg when that link+// exists, and path unchanged otherwise.+//+// Homebrew installs each version under <prefix>/Cellar/<formula>/<version> and links its binaries+// into <prefix>/bin. os.Executable reports the keg on Linux, where it reads /proc/self/exe, and a+// service unit or search result built from that path breaks at the next `brew upgrade`, which+// deletes the old keg. The link keeps resolving to whichever version is current.+func Stable(path string) string {+ idx := strings.Index(path, string(filepath.Separator)+"Cellar"+string(filepath.Separator))+ if idx <= 0 {+ return path+ }+ linked := filepath.Join(path[:idx], "bin", filepath.Base(path))+ if _, err := os.Stat(linked); err != nil {+ return path+ }+ return linked+}diff --git a/cli/beacon/internal/brewpath/brewpath_test.go b/cli/beacon/internal/brewpath/brewpath_test.gonew file mode 100644index 00000000..e82df273--- /dev/null+++ b/cli/beacon/internal/brewpath/brewpath_test.go@@ -0,0 +1,30 @@+package brewpath++import (+ "os"+ "path/filepath"+ "testing"+)++func TestStableMapsAKegPathToItsLink(t *testing.T) {+ prefix := t.TempDir()+ keg := filepath.Join(prefix, "Cellar", "beacon", "1.3.20", "bin", "beacon-vector")+ linked := filepath.Join(prefix, "bin", "beacon-vector")++ // Without the link there is nothing better to return.+ if got := Stable(keg); got != keg {+ t.Fatalf("Stable(keg) with no link = %q, want %q", got, keg)+ }+ if err := os.MkdirAll(filepath.Dir(linked), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(linked, nil, 0o755); err != nil {+ t.Fatal(err)+ }+ if got := Stable(keg); got != linked {+ t.Fatalf("Stable(keg) = %q, want the link %q", got, linked)+ }+ if got := Stable("/opt/beacon/bin/vector"); got != "/opt/beacon/bin/vector" {+ t.Fatalf("a path outside a Cellar changed: %q", got)+ }+}diff --git a/cli/beacon/internal/endpoint/asymptote/vector.go b/cli/beacon/internal/endpoint/asymptote/vector.goindex c94d82bf..15861566 100644--- a/cli/beacon/internal/endpoint/asymptote/vector.go+++ b/cli/beacon/internal/endpoint/asymptote/vector.go@@ -10,6 +10,8 @@ import ("runtime""strconv""strings"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/brewpath")// MinVectorVersion is the oldest Vector the forwarder template is validated against; the@@ -131,9 +133,11 @@ func defaultVectorSearchPaths() []string {// Beside the CLI first, which is what an extracted Linux release archive and a// Homebrew-on-Linux install look like. Only under ArchiveVectorName: a plain vector beside// the CLI in a Homebrew bin directory is whatever Vector the user linked there, and it- // must not outrank the pinned keg below.+ // must not outrank the pinned keg below. Under Homebrew the running CLI is in a versioned+ // keg, and connect writes this path into the forwarder's service unit, so it is mapped to+ // the <prefix>/bin link that survives `brew upgrade`.if executable, err := currentExecutable(); err == nil {- paths = append(paths, filepath.Join(filepath.Dir(executable), ArchiveVectorName))+ paths = append(paths, brewpath.Stable(filepath.Join(filepath.Dir(executable), ArchiveVectorName)))}paths = append(paths, PackagedVectorPath)prefixes := homebrewPrefixes()diff --git a/cli/beacon/internal/endpoint/asymptote/vector_test.go b/cli/beacon/internal/endpoint/asymptote/vector_test.goindex 57ecdb8d..8a8ef42d 100644--- a/cli/beacon/internal/endpoint/asymptote/vector_test.go+++ b/cli/beacon/internal/endpoint/asymptote/vector_test.go@@ -224,3 +224,35 @@ func TestVectorNotFoundMessageNamesWhereVectorComesFrom(t *testing.T) {t.Error("the Linux message should not send people to a macOS install command")}}++// Under Homebrew the running CLI is in a versioned keg, and connect writes the Vector path into the+// forwarder's service unit. The keg path breaks at the next `brew upgrade`, so FindVector has to+// return the <prefix>/bin link instead.+func TestFindVectorReturnsTheHomebrewLinkNotTheKeg(t *testing.T) {+ isolateVectorDiscovery(t)+ vectorSearchPaths = defaultVectorSearchPaths+ prefix := t.TempDir()+ kegBin := filepath.Join(prefix, "Cellar", "beacon", "1.3.20", "bin")+ if err := os.MkdirAll(kegBin, 0o755); err != nil {+ t.Fatal(err)+ }+ keg := filepath.Join(kegBin, ArchiveVectorName)+ if err := os.Rename(fakeVector(t, "0.56.0", 0), keg); err != nil {+ t.Fatal(err)+ }+ link := filepath.Join(prefix, "bin", ArchiveVectorName)+ if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.Symlink(keg, link); err != nil {+ t.Fatal(err)+ }+ oldExe := currentExecutable+ currentExecutable = func() (string, error) { return filepath.Join(kegBin, "beacon"), nil }+ t.Cleanup(func() { currentExecutable = oldExe })++ got, err := FindVector("")+ if err != nil || got.Path != link {+ t.Fatalf("FindVector() = %+v, %v; want the Homebrew link %s, not the keg", got, err, link)+ }+}diff --git a/cli/beacon/internal/endpoint/collector/candidates_test.go b/cli/beacon/internal/endpoint/collector/candidates_test.goindex e442d85a..05193573 100644--- a/cli/beacon/internal/endpoint/collector/candidates_test.go+++ b/cli/beacon/internal/endpoint/collector/candidates_test.go@@ -1,6 +1,7 @@package collectorimport (+ "os""path/filepath""runtime""strings"@@ -40,6 +41,33 @@ func TestDefaultBinaryCandidatesLooksForTheNameOnDisk(t *testing.T) {}}+// A Homebrew CLI runs from its versioned keg. The collector path found here goes into the service+// unit, and the keg is deleted at the next `brew upgrade`, so the sibling has to be the+// <prefix>/bin link. PATH would usually find the link first, but not under sudo on Linuxbrew.+func TestDefaultBinaryCandidatesUseTheHomebrewLink(t *testing.T) {+ prefix := t.TempDir()+ kegBin := filepath.Join(prefix, "Cellar", "beacon", "1.3.20", "bin")+ link := filepath.Join(prefix, "bin", binaryFileName())+ for _, dir := range []string{kegBin, filepath.Dir(link)} {+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ }+ if err := os.WriteFile(filepath.Join(kegBin, binaryFileName()), nil, 0o755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(link, nil, 0o755); err != nil {+ t.Fatal(err)+ }+ old := currentExecutable+ currentExecutable = func() (string, error) { return filepath.Join(kegBin, "beacon"), nil }+ t.Cleanup(func() { currentExecutable = old })++ if got := defaultBinaryCandidates()[0]; got != link {+ t.Fatalf("first candidate = %q, want the Homebrew link %q", got, link)+ }+}+func TestPackagedBinaryPathsAreAbsoluteAndPlatformShaped(t *testing.T) {paths := packagedBinaryPaths()if len(paths) == 0 {diff --git a/cli/beacon/internal/endpoint/collector/collector.go b/cli/beacon/internal/endpoint/collector/collector.goindex 925c31d5..676dcd55 100644--- a/cli/beacon/internal/endpoint/collector/collector.go+++ b/cli/beacon/internal/endpoint/collector/collector.go@@ -10,6 +10,7 @@ import ("strings""time"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/brewpath"endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config")@@ -78,8 +79,13 @@ func DiscoverBinary(configured string) string {// collector at all.func defaultBinaryCandidates() []string {paths := packagedBinaryPaths()+ // A Homebrew CLI runs from its versioned keg, and the collector path found here goes into+ // the service unit, so it is mapped to the <prefix>/bin link that survives `brew upgrade`.+ // PATH usually finds that link first, but not under sudo, whose secure_path leaves out the+ // Linuxbrew prefix.if executable, err := currentExecutable(); err == nil {- paths = append([]string{filepath.Join(filepath.Dir(executable), binaryFileName())}, paths...)+ sibling := brewpath.Stable(filepath.Join(filepath.Dir(executable), binaryFileName()))+ paths = append([]string{sibling}, paths...)}return paths}diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.goindex 141cf469..db934789 100644--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go@@ -8,6 +8,7 @@ import ("strings""time"+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/brewpath""github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/asymptote"endpointcollector "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/collector"endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"@@ -140,18 +141,9 @@ func DefaultInventoryJobProgram(userMode bool) string {return "beacon"}-// stableProgramPath keeps the scheduled unit pointing at a path that survives upgrades. A-// Homebrew binary reports its versioned Cellar keg, which `brew upgrade` deletes; the linked-// `<prefix>/bin/beacon` is the path that keeps resolving, so that is what the unit records.-func stableProgramPath(exe string) string {- if idx := strings.Index(exe, string(filepath.Separator)+"Cellar"+string(filepath.Separator)); idx > 0 {- linked := filepath.Join(exe[:idx], "bin", filepath.Base(exe))- if _, err := os.Stat(linked); err == nil {- return linked- }- }- return exe-}+// stableProgramPath keeps the scheduled unit pointing at a path that survives upgrades: a Homebrew+// binary's linked <prefix>/bin/beacon rather than its versioned keg, which `brew upgrade` deletes.+func stableProgramPath(exe string) string { return brewpath.Stable(exe) }// InventoryHeartbeatStatus is the status view of the scheduled inventory job.type InventoryHeartbeatStatus struct {