Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Gate forwarder unload on an installed forwarder, not on the state directory

shukieshah1ffb30cdad8c
diff --git a/cli/beacon/internal/endpoint/asymptote/connect.go b/cli/beacon/internal/endpoint/asymptote/connect.go
index abd45130..6be0ec06 100644
--- a/cli/beacon/internal/endpoint/asymptote/connect.go
+++ b/cli/beacon/internal/endpoint/asymptote/connect.go
@@ -20,6 +20,7 @@ type Forwarder interface {
Supported() bool
UnsupportedReason() string
Label() string
+ UnitPath() (string, error)
WriteUnit(vectorBin, configPath string) (string, error)
Load() error
Unload() error
@@ -218,11 +219,12 @@ func Disconnect(opts DisconnectOptions) error {
manager = service.ForwarderManager{UserMode: opts.UserMode}
}
var problems []error
- // Only touch the service manager when this endpoint has managed-ingest state. Uninstall
+ // Only touch the service manager when a forwarder was actually installed here. Uninstall
// calls Disconnect unconditionally, and a bootout against the live launchd domain from
// an endpoint that was never connected would stop a forwarder belonging to a different
- // install (or to the developer running the test suite).
- if hasManagedState(opts.UserMode) {
+ // install (or to the developer running the test suite). A directory holding only the
+ // pinned install id from a cancelled connect is not a forwarder.
+ if forwarderInstalled(opts.UserMode, manager) {
if err := manager.Unload(); err != nil {
problems = append(problems, fmt.Errorf("stop forwarder: %w", err))
}
@@ -237,10 +239,21 @@ func Disconnect(opts DisconnectOptions) error {
return errors.Join(problems...)
}
-// hasManagedState reports whether anything under the asymptote directory exists.
-func hasManagedState(userMode bool) bool {
- _, err := os.Stat(Dir(userMode))
- return err == nil
+// forwarderInstalled reports whether Connect got as far as installing a forwarder: the
+// rendered config, an enrollment record, or the service unit itself exists.
+func forwarderInstalled(userMode bool, manager Forwarder) bool {
+ if Connected(userMode) {
+ return true
+ }
+ if _, err := os.Stat(EnrollmentPath(userMode)); err == nil {
+ return true
+ }
+ if path, err := manager.UnitPath(); err == nil && path != "" {
+ if _, err := os.Stat(path); err == nil {
+ return true
+ }
+ }
+ return false
}
// updateConfig records (or clears) the non-secret managed_ingest block in config.json. A
diff --git a/cli/beacon/internal/endpoint/asymptote/connect_test.go b/cli/beacon/internal/endpoint/asymptote/connect_test.go
index e13c8c34..0e2ff958 100644
--- a/cli/beacon/internal/endpoint/asymptote/connect_test.go
+++ b/cli/beacon/internal/endpoint/asymptote/connect_test.go
@@ -20,6 +20,7 @@ import (
type fakeForwarder struct {
supported bool
+ unitPath string
written []string
loads int
unloads int
@@ -30,13 +31,24 @@ type fakeForwarder struct {
func (f *fakeForwarder) Supported() bool { return f.supported }
func (f *fakeForwarder) UnsupportedReason() string { return "fake forwarder unsupported" }
func (f *fakeForwarder) Label() string { return "fake.forwarder" }
+func (f *fakeForwarder) UnitPath() (string, error) { return f.unitPath, nil }
func (f *fakeForwarder) WriteUnit(vectorBin, configPath string) (string, error) {
f.written = append(f.written, vectorBin+" --config "+configPath)
- return "/tmp/fake.unit", nil
+ if f.unitPath != "" {
+ if err := os.WriteFile(f.unitPath, []byte("unit"), 0o644); err != nil {
+ return "", err
+ }
+ }
+ return f.unitPath, nil
}
func (f *fakeForwarder) Load() error { f.loads++; return f.loadErr }
func (f *fakeForwarder) Unload() error { f.unloads++; return nil }
-func (f *fakeForwarder) RemoveUnits() { f.removed++ }
+func (f *fakeForwarder) RemoveUnits() {
+ f.removed++
+ if f.unitPath != "" {
+ _ = os.Remove(f.unitPath)
+ }
+}
func (f *fakeForwarder) Status() service.Status {
return service.Status{Label: "fake.forwarder", Loaded: f.loads > 0, Running: f.loads > 0}
}
@@ -438,3 +450,34 @@ func TestLoopbackIngestURLIsAcceptedEndToEnd(t *testing.T) {
}
}
}
+
+func TestDisconnectIgnoresALeftoverInstallIDFromACancelledConnect(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ // A connect that was cancelled after pinning the id leaves only asymptote/install-id.
+ if err := WriteInstallID(true, "abc123"); err != nil {
+ t.Fatal(err)
+ }
+ fwd := &fakeForwarder{supported: true}
+ if err := Disconnect(DisconnectOptions{UserMode: true, Forwarder: fwd}); err != nil {
+ t.Fatal(err)
+ }
+ if fwd.unloads != 0 || fwd.removed != 0 {
+ t.Fatalf("a pinned install id alone must not touch the service manager: unloads=%d removed=%d", fwd.unloads, fwd.removed)
+ }
+ if _, err := os.Stat(Dir(true)); !os.IsNotExist(err) {
+ t.Fatal("the leftover directory should still be cleaned up")
+ }
+
+ // A unit file on disk, with nothing else, is still a forwarder to stop.
+ fwd = &fakeForwarder{supported: true, unitPath: filepath.Join(t.TempDir(), "forwarder.plist")}
+ if err := os.WriteFile(fwd.unitPath, []byte("unit"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if err := Disconnect(DisconnectOptions{UserMode: true, Forwarder: fwd}); err != nil {
+ t.Fatal(err)
+ }
+ if fwd.unloads != 1 {
+ t.Fatalf("an installed unit must be unloaded, unloads=%d", fwd.unloads)
+ }
+}