From 655df67dd2d954c330c03533782622ee74eebdc4 Mon Sep 17 00:00:00 2001 From: Harsh Date: Thu, 12 Nov 2026 17:22:10 -0800 Subject: [PATCH 1/3] Restart the managed daemon on upgrade when it is behind the new build (#178) service install is now the idempotent upgrade path: it replaces the stable executable (skipping byte-identical reinstalls), reactivates the user service, restarts only when the running daemon is not on the new build, and verifies health/version before reporting success. install upgrades a stale managed daemon instead of reusing it. Activation failures are reported as failures; unchanged reinstalls never restart. --- cmd/agentintercom/install.go | 12 +- cmd/agentintercom/service.go | 237 ++++++++++++-- cmd/agentintercom/service_upgrade_test.go | 359 ++++++++++++++++++++++ docs/installation.md | 18 ++ 4 files changed, 592 insertions(+), 34 deletions(-) create mode 100644 cmd/agentintercom/service_upgrade_test.go diff --git a/cmd/agentintercom/install.go b/cmd/agentintercom/install.go index 56dce70..0609e08 100644 --- a/cmd/agentintercom/install.go +++ b/cmd/agentintercom/install.go @@ -164,7 +164,17 @@ func installCommand(args []string, out io.Writer) error { if err != nil { return err } - if _, err = bus.ReadRunFile(p.RunFile); err != nil && !*noService { + needsService := false + if _, err = bus.ReadRunFile(p.RunFile); err != nil { + needsService = !*noService + } else if !*noService { + // A managed daemon left running from an older install must be + // upgraded to this build instead of silently reused. + if stale, serr := managedDaemonNeedsUpgrade(p); serr == nil && stale { + needsService = true + } + } + if needsService { if err = manageService("install", out); err != nil { return err } diff --git a/cmd/agentintercom/service.go b/cmd/agentintercom/service.go index ef64ecc..8eb8b43 100644 --- a/cmd/agentintercom/service.go +++ b/cmd/agentintercom/service.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "encoding/xml" "errors" "fmt" @@ -19,57 +20,126 @@ import ( const userServiceName = "dev.october.agent-intercom" +// serviceActivationTimeout bounds the post-activation health waits after the +// service manager reports success. Tests override it. +var serviceActivationTimeout = 30 * time.Second + +func stableBinaryPath(paths bus.DaemonPaths) string { + name := "agentintercom" + if runtime.GOOS == "windows" { + name += ".exe" + } + return filepath.Join(paths.DataDir, "bin", name) +} + func stableExecutable() (string, error) { + dest, _, err := installStableExecutable() + return dest, err +} + +func installStableExecutable() (string, bool, error) { source, err := os.Executable() if err != nil { - return "", err + return "", false, err } paths, err := bus.DefaultDaemonPaths() if err != nil { - return "", err + return "", false, err } dir := filepath.Join(paths.DataDir, "bin") if err := os.MkdirAll(dir, 0700); err != nil { - return "", err + return "", false, err } if err := nativePrivateDirectory(dir); err != nil { - return "", err + return "", false, err } - name := "agentintercom" - if runtime.GOOS == "windows" { - name += ".exe" - } - dest := filepath.Join(dir, name) + dest := stableBinaryPath(paths) if filepath.Clean(source) == filepath.Clean(dest) { - return dest, nil + return dest, false, nil + } + if sameExecutableBytes(source, dest) { + return dest, false, nil } in, err := os.Open(source) if err != nil { - return "", err + return "", false, err } defer in.Close() out, err := os.CreateTemp(dir, ".binary-*") if err != nil { - return "", err + return "", false, err } defer os.Remove(out.Name()) defer out.Close() if _, err := io.Copy(out, in); err != nil { - return "", err + return "", false, err } if err := out.Chmod(0700); err != nil { - return "", err + return "", false, err } if err := out.Sync(); err != nil { - return "", err + return "", false, err } if err := out.Close(); err != nil { - return "", err + return "", false, err } if err := os.Rename(out.Name(), dest); err != nil { - return "", err + return "", false, err + } + return dest, true, nil +} + +// sameExecutableBytes reports whether the installed copy already matches the +// running executable, so reinstalling an unchanged build skips the replacement. +func sameExecutableBytes(source, dest string) bool { + before, err := os.ReadFile(source) + if err != nil { + return false + } + after, err := os.ReadFile(dest) + if err != nil { + return false + } + return bytes.Equal(before, after) +} + +// probeDaemon reads the run file and queries the daemon's unauthenticated +// health endpoint for its running version. +func probeDaemon(paths bus.DaemonPaths) (bus.Health, bus.RunFile, error) { + run, err := bus.ReadRunFile(paths.RunFile) + if err != nil { + return bus.Health{}, bus.RunFile{}, err + } + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + health, err := (bus.Client{Address: run.Address}).Health(ctx) + if err != nil { + return bus.Health{}, bus.RunFile{}, err + } + return health, run, nil +} + +// awaitServiceDaemon polls until the daemon answers health and accept agrees, or +// the timeout expires. Activation failures surface here instead of reading as success. +func awaitServiceDaemon(paths bus.DaemonPaths, timeout time.Duration, accept func(bus.Health, bus.RunFile) bool) (bus.Health, bus.RunFile, error) { + deadline := time.Now().Add(timeout) + var lastErr error + for { + health, run, err := probeDaemon(paths) + if err == nil && (accept == nil || accept(health, run)) { + return health, run, nil + } + if err != nil { + lastErr = err + } + if time.Now().After(deadline) { + if lastErr != nil { + return bus.Health{}, bus.RunFile{}, lastErr + } + return bus.Health{}, bus.RunFile{}, errors.New("daemon did not reach the expected state in time") + } + time.Sleep(100 * time.Millisecond) } - return dest, nil } func serviceFile(platform, home, binary string, paths bus.DaemonPaths) (string, []byte, error) { @@ -109,6 +179,15 @@ func serviceCommand(args []string, out io.Writer) error { } func manageService(action string, out io.Writer) error { + return manageServiceOn(runtime.GOOS, action, bus.Version, out) +} + +// manageServiceOn runs the login-service action for one platform. Re-running +// install after upgrading the package is the supported upgrade: it replaces +// the stable executable, reactivates the user service, restarts the managed +// daemon only when it is not already on this build, and verifies the running +// version before reporting success. +func manageServiceOn(platform, action, localVersion string, out io.Writer) error { home, err := os.UserHomeDir() if err != nil { return err @@ -121,18 +200,25 @@ func manageService(action string, out io.Writer) error { if err != nil { return err } + binaryChanged := false + var beforeHealth bus.Health + var beforeRun bus.RunFile + wasRunning := false if action == "install" { - binary, err = stableExecutable() + if beforeHealth, beforeRun, err = probeDaemon(paths); err == nil { + wasRunning = true + } + binary, binaryChanged, err = installStableExecutable() if err != nil { return err } } - path, body, err := serviceFile(runtime.GOOS, home, binary, paths) + path, body, err := serviceFile(platform, home, binary, paths) if err != nil { return err } manager := "systemctl" - if runtime.GOOS == "darwin" { + if platform == "darwin" { manager = "launchctl" } old, err := readConfigFile(path) @@ -153,7 +239,7 @@ func manageService(action string, out io.Writer) error { unit := userServiceName + ".service" domain := "gui/" + strconv.Itoa(os.Getuid()) if action == "status" { - if runtime.GOOS == "linux" { + if platform == "linux" { return run("--user", "status", unit, "--no-pager") } return run("print", domain+"/"+userServiceName) @@ -165,7 +251,7 @@ func manageService(action string, out io.Writer) error { if len(old) == 0 { return nil } - if runtime.GOOS == "linux" { + if platform == "linux" { err = run("--user", "disable", "--now", unit) } else { err = run("bootout", domain, path) @@ -176,26 +262,111 @@ func manageService(action string, out io.Writer) error { if err = os.Remove(path); err != nil { return err } - if runtime.GOOS == "linux" { + if platform == "linux" { return run("--user", "daemon-reload") } return nil } + unitChanged := !bytes.Equal(old, body) + if platform == "darwin" && wasRunning && !binaryChanged && !unitChanged && beforeHealth.RuntimeVersion == localVersion { + fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") + return nil + } if err = writeConfigFile(path, body); err != nil { return err } - if runtime.GOOS == "linux" { - if err = run("--user", "daemon-reload"); err == nil { - err = run("--user", "enable", "--now", unit) - } - } else { - if len(old) > 0 { - _ = run("bootout", domain, path) + if platform == "linux" { + return activateLinuxService(run, unit, paths, localVersion, binaryChanged, unitChanged, wasRunning, beforeRun, out) + } + if len(old) > 0 { + _ = run("bootout", domain, path) + } + if err = run("bootstrap", domain, path); err != nil { + return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) + } + return verifyServiceDaemon(paths, localVersion, wasRunning, beforeRun, "bootstrap", out) +} + +// activateLinuxService reloads user systemd, enables the unit, and restarts an +// already-active daemon only when the installed build changed underneath it. +func activateLinuxService(run func(...string) error, unit string, paths bus.DaemonPaths, localVersion string, binaryChanged, unitChanged, wasRunning bool, beforeRun bus.RunFile, out io.Writer) error { + if err := run("--user", "daemon-reload"); err != nil { + return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) + } + if err := run("--user", "enable", "--now", unit); err != nil { + return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) + } + current, _, err := awaitServiceDaemon(paths, serviceActivationTimeout, nil) + if err != nil { + return fmt.Errorf("service file saved but the daemon did not become healthy; check service status and run service install again: %w", err) + } + if wasRunning && (binaryChanged || unitChanged || current.RuntimeVersion != localVersion) { + if err := run("--user", "restart", unit); err != nil { + return fmt.Errorf("service file saved but restarting the login service failed; the running daemon may still be on the previous build: %w", err) } - err = run("bootstrap", domain, path) + return verifyServiceDaemon(paths, localVersion, wasRunning, beforeRun, "restart", out) + } + if current.RuntimeVersion != localVersion { + return fmt.Errorf("service file saved but the running daemon reports version %q instead of %q; check service status and run service install again", current.RuntimeVersion, localVersion) + } + if wasRunning { + fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") + return nil + } + fmt.Fprintln(out, "Login service installed. It starts with your user session; Linux startup without a login additionally requires administrator-enabled linger.") + return nil +} + +// managedDaemonNeedsUpgrade reports whether a managed login service exists and +// its running daemon differs from this build, either by reported version or by +// installed bytes. Manually started daemons and unreachable ones are left to +// the existing reuse path; only a positively stale managed daemon upgrades. +func managedDaemonNeedsUpgrade(paths bus.DaemonPaths) (bool, error) { + home, err := os.UserHomeDir() + if err != nil { + return false, err } + binary, err := os.Executable() if err != nil { - return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) + return false, err + } + path, _, err := serviceFile(runtime.GOOS, home, binary, paths) + if err != nil { + return false, err + } + owned, err := readConfigFile(path) + if err != nil { + return false, err + } + if len(owned) == 0 || !bytes.Contains(owned, []byte("Managed by Agent Intercom")) { + return false, nil + } + health, _, err := probeDaemon(paths) + if err != nil { + return false, nil + } + if health.RuntimeVersion != bus.Version { + return true, nil + } + return !sameExecutableBytes(binary, stableBinaryPath(paths)), nil +} + +// verifyServiceDaemon waits for the daemon to answer on the new build after a +// restart, requiring a new process identity when one was running before. It +// reports failures instead of letting a stale or missing daemon read as success. +func verifyServiceDaemon(paths bus.DaemonPaths, localVersion string, wasRunning bool, beforeRun bus.RunFile, operation string, out io.Writer) error { + health, run, err := awaitServiceDaemon(paths, serviceActivationTimeout, func(next bus.Health, nextRun bus.RunFile) bool { + if next.RuntimeVersion != localVersion { + return false + } + return !wasRunning || nextRun.PID != beforeRun.PID || nextRun.StartedAt != beforeRun.StartedAt + }) + if err != nil { + return fmt.Errorf("service file saved but the daemon did not come up on the new build after %s; check service status and run service install again: %w", operation, err) + } + if wasRunning { + fmt.Fprintf(out, "Login service upgraded and restarted on version %s (pid %d). Workspaces and messages are preserved; reconnect MCP in your apps.\n", health.RuntimeVersion, run.PID) + return nil } fmt.Fprintln(out, "Login service installed. It starts with your user session; Linux startup without a login additionally requires administrator-enabled linger.") return nil diff --git a/cmd/agentintercom/service_upgrade_test.go b/cmd/agentintercom/service_upgrade_test.go new file mode 100644 index 0000000..112c4af --- /dev/null +++ b/cmd/agentintercom/service_upgrade_test.go @@ -0,0 +1,359 @@ +package main + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/october-dev/agentintercom/bus" +) + +const ( + upgradeVersionA = "9.9.9-A" + upgradeVersionB = "9.9.9-B" + upgradeToken = "0123456789012345678901234567890123456789012" + upgradeBefore = "2026-01-01T00:00:00Z" + upgradeAfter = "2026-01-02T00:00:00Z" +) + +type fakeDaemon struct { + root string + home string + paths bus.DaemonPaths + server *httptest.Server + versionFile string + runFile string + logFile string +} + +func setupFakeDaemon(t *testing.T, manager string) *fakeDaemon { + t.Helper() + if os.PathSeparator == '\\' { + t.Skip("Unix service manager fakes") + } + root := t.TempDir() + home := filepath.Join(root, "home") + data := filepath.Join(root, "data") + runtimeDir := filepath.Join(root, "run") + t.Setenv("HOME", home) + t.Setenv("OCTOBER_BUS_DATA_DIR", data) + t.Setenv("OCTOBER_BUS_RUNTIME_DIR", runtimeDir) + requireNoError(t, os.MkdirAll(runtimeDir, 0700)) + + paths, err := bus.DefaultDaemonPaths() + requireNoError(t, err) + fake := &fakeDaemon{root: root, home: home, paths: paths, versionFile: filepath.Join(root, "version"), runFile: paths.RunFile, logFile: filepath.Join(root, manager+".log")} + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionA), 0600)) + fake.server = httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + version, err := os.ReadFile(fake.versionFile) + if err != nil { + http.Error(response, "no version", http.StatusInternalServerError) + return + } + _ = json.NewEncoder(response).Encode(map[string]string{ + "name": "october-bus", "protocolVersion": bus.ProtocolVersion, + "runtimeVersion": string(version), "status": "ready", "startedAt": upgradeBefore, + }) + })) + t.Cleanup(fake.server.Close) + + bin := filepath.Join(root, "bin") + requireNoError(t, os.MkdirAll(bin, 0700)) + script := bin + "/" + manager + var body string + if manager == "systemctl" { + body = "#!/bin/sh\necho \"$@\" >> \"$FAKE_SYSTEMCTL_LOG\"\n" + + "case \"$*\" in\n" + + " *\"--user restart \"*)\n" + + " [ \"$FAKE_SYSTEMCTL_MODE\" = \"fail-restart\" ] && exit 1\n" + + " [ \"$FAKE_SYSTEMCTL_MODE\" = \"no-flip\" ] && exit 0\n" + + " printf '%s' \"$FAKE_AFTER_VERSION\" > \"$FAKE_VERSION_FILE\"\n" + + " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + + " ;;\n" + + " *\"--user enable --now \"*)\n" + + " [ \"$FAKE_ENABLE_STARTS\" = \"1\" ] && {\n" + + " printf '%s' \"$FAKE_AFTER_VERSION\" > \"$FAKE_VERSION_FILE\"\n" + + " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + + " }\n" + + " ;;\n" + + "esac\nexit 0\n" + } else { + body = "#!/bin/sh\necho \"$@\" >> \"$FAKE_SYSTEMCTL_LOG\"\n" + + "case \"$1\" in\n" + + " bootstrap)\n" + + " printf '%s' \"$FAKE_AFTER_VERSION\" > \"$FAKE_VERSION_FILE\"\n" + + " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + + " ;;\n" + + "esac\nexit 0\n" + } + requireNoError(t, os.WriteFile(script, []byte(body), 0700)) + t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("FAKE_SYSTEMCTL_LOG", fake.logFile) + t.Setenv("FAKE_VERSION_FILE", fake.versionFile) + t.Setenv("FAKE_RUN_FILE", fake.runFile) + t.Setenv("FAKE_ADDRESS", fake.server.URL) + t.Setenv("FAKE_AFTER_VERSION", upgradeVersionB) + return fake +} + +func (f *fakeDaemon) writeRunFile(t *testing.T, pid int, startedAt string) { + t.Helper() + data, err := json.Marshal(bus.RunFile{ + ProtocolVersion: bus.ProtocolVersion, Address: f.server.URL, + PID: pid, StartedAt: startedAt, AdminToken: upgradeToken, + }) + requireNoError(t, err) + requireNoError(t, os.WriteFile(f.runFile, append(data, '\n'), 0600)) +} + +func (f *fakeDaemon) installCurrentBinary(t *testing.T) string { + t.Helper() + source, err := os.Executable() + requireNoError(t, err) + dest := stableBinaryPath(f.paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + content, err := os.ReadFile(source) + requireNoError(t, err) + requireNoError(t, os.WriteFile(dest, content, 0700)) + return dest +} + +func (f *fakeDaemon) writeCurrentUnit(t *testing.T, platform, binary string) { + t.Helper() + path, body, err := serviceFile(platform, f.home, binary, f.paths) + requireNoError(t, err) + requireNoError(t, writeConfigFile(path, body)) +} + +func (f *fakeDaemon) managerLog(t *testing.T) string { + t.Helper() + data, err := os.ReadFile(f.logFile) + if os.IsNotExist(err) { + return "" + } + requireNoError(t, err) + return string(data) +} + +func TestLinuxServiceInstallRestartsStaleDaemon(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + // The installed bytes and unit already match; only the running version is stale. + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + log := fake.managerLog(t) + require(t, strings.Contains(log, "--user daemon-reload"), "missing daemon-reload: %q", log) + require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) + require(t, strings.Contains(log, "--user restart"), "stale daemon was not restarted: %q", log) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + + version, err := os.ReadFile(fake.versionFile) + requireNoError(t, err) + require(t, string(version) == upgradeVersionB, "daemon still on %q", version) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "run file still points at the old process: %#v", run) + health, _, err := probeDaemon(fake.paths) + requireNoError(t, err) + require(t, health.RuntimeVersion == upgradeVersionB, "health still reports %q", health.RuntimeVersion) +} + +func TestLinuxServiceInstallRestartsWhenBytesDiffer(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + // Same reported version (for example two dev builds), but the installed + // copy does not match this executable, so the daemon must still restart. + dest := stableBinaryPath(fake.paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + requireNoError(t, os.WriteFile(dest, []byte("older build"), 0700)) + fake.writeCurrentUnit(t, "linux", dest) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + require(t, strings.Contains(fake.managerLog(t), "--user restart"), "byte change did not restart: %q", out.String()) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) +} + +func TestLinuxServiceReinstallWithoutChangesSkipsRestart(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + log := fake.managerLog(t) + require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) + require(t, !strings.Contains(log, "--user restart"), "unchanged reinstall restarted: %q", log) + require(t, strings.Contains(out.String(), "already current"), "missing current report: %q", out.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 1000 && run.StartedAt == upgradeBefore, "reinstall touched the running daemon: %#v", run) +} + +func TestLinuxServiceInstallReportsRestartFailure(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + t.Setenv("FAKE_SYSTEMCTL_MODE", "fail-restart") + + var out bytes.Buffer + err := manageServiceOn("linux", "install", upgradeVersionB, &out) + require(t, err != nil && strings.Contains(err.Error(), "restarting the login service failed"), "restart failure error = %v", err) + require(t, !strings.Contains(out.String(), "upgraded"), "failure reported as success: %q", out.String()) +} + +func TestLinuxServiceInstallVerifiesNewVersionAfterRestart(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + t.Setenv("FAKE_SYSTEMCTL_MODE", "no-flip") + old := serviceActivationTimeout + serviceActivationTimeout = 2 * time.Second + t.Cleanup(func() { serviceActivationTimeout = old }) + + var out bytes.Buffer + err := manageServiceOn("linux", "install", upgradeVersionB, &out) + require(t, err != nil && strings.Contains(err.Error(), "did not come up on the new build"), "stale verification error = %v", err) + require(t, !strings.Contains(out.String(), "upgraded"), "stale daemon reported as success: %q", out.String()) +} + +func TestLinuxServiceFreshInstallStartsDaemonWithoutRestart(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + t.Setenv("FAKE_ENABLE_STARTS", "1") + + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + log := fake.managerLog(t) + require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) + require(t, !strings.Contains(log, "--user restart"), "fresh install restarted: %q", log) + health, _, err := probeDaemon(fake.paths) + requireNoError(t, err) + require(t, health.RuntimeVersion == upgradeVersionB, "fresh daemon reports %q", health.RuntimeVersion) +} + +func TestDarwinServiceUpgradeRestartsStaleDaemon(t *testing.T) { + fake := setupFakeDaemon(t, "launchctl") + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, "darwin", fake.installCurrentBinary(t)) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("darwin", "install", upgradeVersionB, &out)) + log := fake.managerLog(t) + require(t, strings.Contains(log, "bootout"), "missing bootout: %q", log) + require(t, strings.Contains(log, "bootstrap"), "missing bootstrap: %q", log) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + health, _, err := probeDaemon(fake.paths) + requireNoError(t, err) + require(t, health.RuntimeVersion == upgradeVersionB, "daemon still on %q", health.RuntimeVersion) +} + +func TestDarwinServiceReinstallWithoutChangesTouchesNothing(t *testing.T) { + fake := setupFakeDaemon(t, "launchctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + fake.writeCurrentUnit(t, "darwin", fake.installCurrentBinary(t)) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("darwin", "install", upgradeVersionB, &out)) + require(t, fake.managerLog(t) == "", "unchanged reinstall touched launchctl: %q", fake.managerLog(t)) + require(t, strings.Contains(out.String(), "already current"), "missing current report: %q", out.String()) +} + +func TestStableExecutableSkipsUnchangedCopy(t *testing.T) { + root := t.TempDir() + t.Setenv("OCTOBER_BUS_DATA_DIR", filepath.Join(root, "data")) + t.Setenv("OCTOBER_BUS_RUNTIME_DIR", filepath.Join(root, "run")) + + first, changed, err := installStableExecutable() + requireNoError(t, err) + require(t, changed, "fresh install reported no change") + before, err := os.ReadFile(first) + requireNoError(t, err) + second, changed, err := installStableExecutable() + requireNoError(t, err) + require(t, !changed && second == first, "unchanged reinstall recopied: %q changed=%v", second, changed) + after, err := os.ReadFile(second) + requireNoError(t, err) + require(t, bytes.Equal(before, after), "unchanged reinstall rewrote bytes") + require(t, !sameExecutableBytes(first, filepath.Join(root, "missing")), "missing dest compared equal") +} + +func TestManagedDaemonNeedsUpgrade(t *testing.T) { + if os.PathSeparator == '\\' { + t.Skip("Unix service paths") + } + root := t.TempDir() + home := filepath.Join(root, "home") + t.Setenv("HOME", home) + t.Setenv("OCTOBER_BUS_DATA_DIR", filepath.Join(root, "data")) + t.Setenv("OCTOBER_BUS_RUNTIME_DIR", filepath.Join(root, "run")) + paths, err := bus.DefaultDaemonPaths() + requireNoError(t, err) + requireNoError(t, os.MkdirAll(paths.RuntimeDir, 0700)) + + // No managed service file: a manually started daemon is reused, not upgraded. + stale, err := managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, !stale, "manual daemon flagged for upgrade") + + versionFile := filepath.Join(root, "version") + requireNoError(t, os.WriteFile(versionFile, []byte(bus.Version), 0600)) + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { + version, _ := os.ReadFile(versionFile) + _ = json.NewEncoder(response).Encode(map[string]string{"runtimeVersion": string(version)}) + })) + defer server.Close() + data, err := json.Marshal(bus.RunFile{ + ProtocolVersion: bus.ProtocolVersion, Address: server.URL, + PID: 1000, StartedAt: upgradeBefore, AdminToken: upgradeToken, + }) + requireNoError(t, err) + requireNoError(t, os.WriteFile(paths.RunFile, append(data, '\n'), 0600)) + + // An unmanaged service file is also left alone. + source, err := os.Executable() + requireNoError(t, err) + unmanagedPath, _, err := serviceFile(runtime.GOOS, home, source, paths) + requireNoError(t, err) + requireNoError(t, writeConfigFile(unmanagedPath, []byte("# foreign unit\n"))) + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, !stale, "unmanaged service flagged for upgrade") + + managedPath, managedBody, err := serviceFile(runtime.GOOS, home, source, paths) + requireNoError(t, err) + require(t, managedPath == unmanagedPath, "service file moved: %q", managedPath) + requireNoError(t, writeConfigFile(managedPath, managedBody)) + + // Current version but different installed bytes: stale. + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, stale, "byte-different daemon not flagged") + + // Identical bytes and version: current. + dest := stableBinaryPath(paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + content, err := os.ReadFile(source) + requireNoError(t, err) + requireNoError(t, os.WriteFile(dest, content, 0700)) + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, !stale, "current daemon flagged for upgrade") + + // Older reported version: stale even with identical bytes. + requireNoError(t, os.WriteFile(versionFile, []byte("0.0.0-old"), 0600)) + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, stale, "older version not flagged") +} diff --git a/docs/installation.md b/docs/installation.md index de06a24..2d986f8 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -96,6 +96,24 @@ Linux startup before login additionally requires administrator-enabled linger; macOS LaunchAgents start with the user's GUI login. This command does not change system-wide login policy or install a root service. +## Upgrading the login service + +After upgrading the package, re-run `agentintercom service install` (or +`agentintercom install`, which upgrades a stale managed daemon instead of +reusing it). The flow replaces the stable executable, reactivates the user +service, and restarts the managed daemon only when it is not already on the +new build: on Linux it runs `daemon-reload` and `enable --now`, then `restart` +when the running daemon reports an older version or the installed bytes or +unit changed; on macOS it re-bootstraps unless everything is already current. +Reinstalling an unchanged build never restarts the running daemon. + +Every install verifies the running daemon's health/version before reporting +success, so activation failures are reported as failures, never as a +successful upgrade. The restart keeps the same data directory, preserving +workspaces, accepted messages, credentials, and unrelated app settings; +reconnect MCP in your apps (or restart them) after an upgrade restart so they +attach to the new daemon process. + ## Monitor without consuming messages Inside an execution with agent credentials, `agentintercom inbox --follow` From 6fb50c0a128fdbd1ab433e3dd64e26d077083a97 Mon Sep 17 00:00:00 2001 From: Harsh Date: Fri, 13 Nov 2026 01:30:18 -0800 Subject: [PATCH 2/3] Redesign service upgrade recovery: pending activation, managed/readiness split B1: record a pending-activation marker before copying/activating so a retry after a failed or interrupted attempt reactivates and verifies the replacement process even when files and versions already match (same-version rebuilds, copy-without-activation). 'Already current' now requires positive evidence on both Linux and macOS. B2: drive the restart decision from managed-service state, never HTTP readiness. An unhealthy/unreachable managed daemon is restarted and verified (restart also starts inactive units); normal install propagates genuine inspection errors instead of swallowing probe/helper failures. Unsupported platforms and absent/unmanaged units stay not-applicable so manual-daemon flows are unchanged. Also report the latest observed daemon state on verification timeout, cover unit-only drift, and document the resulting guarantees. --- CHANGELOG.md | 7 + cmd/agentintercom/install.go | 11 +- cmd/agentintercom/service.go | 216 ++++++++++++++++---- cmd/agentintercom/service_upgrade_test.go | 230 +++++++++++++++++++++- docs/installation.md | 40 ++-- docs/operations.md | 4 +- 6 files changed, 442 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 384f9fb..970d330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ integration. Hosted launch and payment qualification are tracked separately. - Add managed Claude Code/Codex installation, user login services, native session attachment, non-consuming inbox monitoring and MCP tool metadata. +- Re-running install after an upgrade now restarts an out-of-date managed + daemon and verifies the replacement process before reporting success. + A pending-activation record makes retries reactivate after a failed or + interrupted attempt (including same-version rebuilds) instead of falsely + reporting "already current", and an unhealthy managed daemon is restarted + and verified rather than silently reused. Unchanged reinstalls still skip + the restart, and manually started daemons keep the existing reuse behavior. - Preserve existing protocol 0.1 health/archive identifiers, local data paths, `OCTOBER_BUS_*` configuration, applied migrations and hosted deployment paths. diff --git a/cmd/agentintercom/install.go b/cmd/agentintercom/install.go index 0609e08..97e29ac 100644 --- a/cmd/agentintercom/install.go +++ b/cmd/agentintercom/install.go @@ -168,11 +168,14 @@ func installCommand(args []string, out io.Writer) error { if _, err = bus.ReadRunFile(p.RunFile); err != nil { needsService = !*noService } else if !*noService { - // A managed daemon left running from an older install must be - // upgraded to this build instead of silently reused. - if stale, serr := managedDaemonNeedsUpgrade(p); serr == nil && stale { - needsService = true + // A managed daemon that is not provably on this build must be + // upgraded instead of silently reused; an unevaluated managed + // installation fails loudly rather than reporting success. + stale, serr := managedDaemonNeedsUpgrade(p) + if serr != nil { + return fmt.Errorf("could not inspect the managed login service; refusing to reuse an unevaluated daemon: %w", serr) } + needsService = stale } if needsService { if err = manageService("install", out); err != nil { diff --git a/cmd/agentintercom/service.go b/cmd/agentintercom/service.go index 8eb8b43..d41cc00 100644 --- a/cmd/agentintercom/service.go +++ b/cmd/agentintercom/service.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "encoding/json" "encoding/xml" "errors" "fmt" @@ -24,6 +25,11 @@ const userServiceName = "dev.october.agent-intercom" // service manager reports success. Tests override it. var serviceActivationTimeout = 30 * time.Second +// errServiceUnsupported reports that a platform has no automatic login-service +// support. Callers must treat it as "not applicable" (manual daemons keep +// their existing reuse path), never as a failed upgrade. +var errServiceUnsupported = errors.New("automatic login services are available on Linux and macOS; on Windows run agentintercom start in your chosen startup task") + func stableBinaryPath(paths bus.DaemonPaths) string { name := "agentintercom" if runtime.GOOS == "windows" { @@ -33,8 +39,54 @@ func stableBinaryPath(paths bus.DaemonPaths) string { } func stableExecutable() (string, error) { - dest, _, err := installStableExecutable() - return dest, err + dest, changed, err := installStableExecutable() + if err != nil { + return "", err + } + if changed { + // A copy without activation must not look current later: record it + // so the next service install activates instead of skipping. + paths, err := bus.DefaultDaemonPaths() + if err != nil { + return "", err + } + if err := markServiceActivationPending(paths, bus.Version); err != nil { + return "", err + } + } + return dest, nil +} + +// pendingServicePath locates the marker recording that a service install +// copied a build (or wrote a unit) without verifying the replacement daemon +// yet. Comparing files on disk cannot identify the running build, and a +// same-version retry after a failed activation would otherwise see matching +// files and falsely report "already current". +func pendingServicePath(paths bus.DaemonPaths) string { + return filepath.Join(paths.DataDir, "service-pending") +} + +func pendingServiceActivation(paths bus.DaemonPaths) bool { + data, err := readConfigFile(pendingServicePath(paths)) + return err == nil && len(data) > 0 +} + +func markServiceActivationPending(paths bus.DaemonPaths, localVersion string) error { + record, err := json.Marshal(map[string]string{ + "version": localVersion, + "createdAt": time.Now().UTC().Format(time.RFC3339Nano), + }) + if err != nil { + return err + } + return writeConfigFile(pendingServicePath(paths), append(record, '\n')) +} + +// clearServiceActivationPending removes the marker best-effort. A stale +// marker only forces an extra restart on the next install; it can never +// produce a false "already current", so clearing must not fail a success. +func clearServiceActivationPending(paths bus.DaemonPaths) { + _ = os.Remove(pendingServicePath(paths)) } func installStableExecutable() (string, bool, error) { @@ -124,6 +176,9 @@ func probeDaemon(paths bus.DaemonPaths) (bus.Health, bus.RunFile, error) { func awaitServiceDaemon(paths bus.DaemonPaths, timeout time.Duration, accept func(bus.Health, bus.RunFile) bool) (bus.Health, bus.RunFile, error) { deadline := time.Now().Add(timeout) var lastErr error + var lastHealth bus.Health + var lastRun bus.RunFile + observed := false for { health, run, err := probeDaemon(paths) if err == nil && (accept == nil || accept(health, run)) { @@ -131,8 +186,17 @@ func awaitServiceDaemon(paths bus.DaemonPaths, timeout time.Duration, accept fun } if err != nil { lastErr = err + observed = false + } else { + // A reachable daemon reporting the wrong state supersedes any + // earlier transport failure; report it, not the stale error. + lastErr = nil + lastHealth, lastRun, observed = health, run, true } if time.Now().After(deadline) { + if observed { + return bus.Health{}, bus.RunFile{}, fmt.Errorf("daemon did not reach the expected state in time (last observed version %q pid %d)", lastHealth.RuntimeVersion, lastRun.PID) + } if lastErr != nil { return bus.Health{}, bus.RunFile{}, lastErr } @@ -167,7 +231,7 @@ func serviceFile(platform, home, binary string, paths bus.DaemonPaths) (string, ` return filepath.Join(home, "Library", "LaunchAgents", userServiceName+".plist"), []byte(body), nil default: - return "", nil, errors.New("automatic login services are available on Linux and macOS; on Windows run agentintercom start in your chosen startup task") + return "", nil, errServiceUnsupported } } @@ -185,8 +249,11 @@ func manageService(action string, out io.Writer) error { // manageServiceOn runs the login-service action for one platform. Re-running // install after upgrading the package is the supported upgrade: it replaces // the stable executable, reactivates the user service, restarts the managed -// daemon only when it is not already on this build, and verifies the running -// version before reporting success. +// daemon unless it is provably already on this build, and verifies the +// replacement process before reporting success. Managed-service state (the +// unit, the installed bytes, the pending marker) drives the restart decision; +// HTTP readiness never gates activation, so an unhealthy managed daemon is +// restarted rather than mistaken for absent or current. func manageServiceOn(platform, action, localVersion string, out io.Writer) error { home, err := os.UserHomeDir() if err != nil { @@ -201,12 +268,28 @@ func manageServiceOn(platform, action, localVersion string, out io.Writer) error return err } binaryChanged := false + hadPending := false + hadProcess := false + wasHealthy := false var beforeHealth bus.Health var beforeRun bus.RunFile - wasRunning := false if action == "install" { - if beforeHealth, beforeRun, err = probeDaemon(paths); err == nil { - wasRunning = true + if run, rerr := bus.ReadRunFile(paths.RunFile); rerr == nil { + hadProcess = true + beforeRun = run + } + if health, _, herr := probeDaemon(paths); herr == nil { + wasHealthy = true + beforeHealth = health + } + // Validate platform support before recording pending state, so an + // unsupported platform never leaves a pending marker behind. + if _, _, err := serviceFile(platform, home, stableBinaryPath(paths), paths); err != nil { + return err + } + hadPending = pendingServiceActivation(paths) + if err := markServiceActivationPending(paths, localVersion); err != nil { + return err } binary, binaryChanged, err = installStableExecutable() if err != nil { @@ -263,20 +346,34 @@ func manageServiceOn(platform, action, localVersion string, out io.Writer) error return err } if platform == "linux" { - return run("--user", "daemon-reload") + if err := run("--user", "daemon-reload"); err != nil { + return err + } } + clearServiceActivationPending(paths) return nil } unitChanged := !bytes.Equal(old, body) - if platform == "darwin" && wasRunning && !binaryChanged && !unitChanged && beforeHealth.RuntimeVersion == localVersion { - fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") - return nil + if platform == "darwin" && !binaryChanged && !unitChanged && !hadPending && wasHealthy && beforeHealth.RuntimeVersion == localVersion { + // Positive evidence only: a healthy daemon on this build with no + // pending activation. Re-probe so a daemon that fell over between + // the probes falls through to reactivation instead of reading current. + if current, _, err := probeDaemon(paths); err == nil && current.RuntimeVersion == localVersion { + clearServiceActivationPending(paths) + fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") + return nil + } } if err = writeConfigFile(path, body); err != nil { return err } if platform == "linux" { - return activateLinuxService(run, unit, paths, localVersion, binaryChanged, unitChanged, wasRunning, beforeRun, out) + return activateLinuxService(serviceActivation{ + run: run, unit: unit, paths: paths, localVersion: localVersion, + binaryChanged: binaryChanged, unitChanged: unitChanged, hadPending: hadPending, + hadProcess: hadProcess, wasHealthy: wasHealthy, + beforeHealth: beforeHealth, beforeRun: beforeRun, out: out, + }) } if len(old) > 0 { _ = run("bootout", domain, path) @@ -284,43 +381,66 @@ func manageServiceOn(platform, action, localVersion string, out io.Writer) error if err = run("bootstrap", domain, path); err != nil { return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) } - return verifyServiceDaemon(paths, localVersion, wasRunning, beforeRun, "bootstrap", out) + return verifyServiceDaemon(paths, localVersion, hadProcess, beforeRun, "bootstrap", out) +} + +// serviceActivation carries the managed-service state observed before a Linux +// activation: what changed on disk, whether a previous attempt left work +// pending, and what (if anything) answered before activation started. +type serviceActivation struct { + run func(...string) error + unit string + paths bus.DaemonPaths + localVersion string + binaryChanged bool + unitChanged bool + hadPending bool + hadProcess bool + wasHealthy bool + beforeHealth bus.Health + beforeRun bus.RunFile + out io.Writer } -// activateLinuxService reloads user systemd, enables the unit, and restarts an -// already-active daemon only when the installed build changed underneath it. -func activateLinuxService(run func(...string) error, unit string, paths bus.DaemonPaths, localVersion string, binaryChanged, unitChanged, wasRunning bool, beforeRun bus.RunFile, out io.Writer) error { +// activateLinuxService reloads user systemd, enables the unit, and restarts +// unless the managed daemon is provably already on this build. Readiness never +// gates activation: an unhealthy or unreachable managed daemon is restarted +// (restart also starts an inactive unit), never skipped. Every restart is +// followed by verification of the replacement process. +func activateLinuxService(activation serviceActivation) error { + run, unit, paths := activation.run, activation.unit, activation.paths + localVersion, out := activation.localVersion, activation.out if err := run("--user", "daemon-reload"); err != nil { return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) } if err := run("--user", "enable", "--now", unit); err != nil { return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) } - current, _, err := awaitServiceDaemon(paths, serviceActivationTimeout, nil) - if err != nil { - return fmt.Errorf("service file saved but the daemon did not become healthy; check service status and run service install again: %w", err) - } - if wasRunning && (binaryChanged || unitChanged || current.RuntimeVersion != localVersion) { - if err := run("--user", "restart", unit); err != nil { - return fmt.Errorf("service file saved but restarting the login service failed; the running daemon may still be on the previous build: %w", err) + if !activation.binaryChanged && !activation.unitChanged && !activation.hadPending && activation.wasHealthy && activation.beforeHealth.RuntimeVersion == localVersion { + // Positive evidence only. Re-probe so a daemon that fell over + // between the probes falls through to restart instead of reading + // current. + if current, _, err := probeDaemon(paths); err == nil && current.RuntimeVersion == localVersion { + clearServiceActivationPending(paths) + fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") + return nil } - return verifyServiceDaemon(paths, localVersion, wasRunning, beforeRun, "restart", out) } - if current.RuntimeVersion != localVersion { - return fmt.Errorf("service file saved but the running daemon reports version %q instead of %q; check service status and run service install again", current.RuntimeVersion, localVersion) + if err := run("--user", "restart", unit); err != nil { + return fmt.Errorf("service file saved but restarting the login service failed; the running daemon may still be on the previous build: %w", err) } - if wasRunning { - fmt.Fprintln(out, "Login service is already current; the running daemon was left untouched.") - return nil - } - fmt.Fprintln(out, "Login service installed. It starts with your user session; Linux startup without a login additionally requires administrator-enabled linger.") - return nil + return verifyServiceDaemon(paths, localVersion, activation.hadProcess, activation.beforeRun, "restart", out) } -// managedDaemonNeedsUpgrade reports whether a managed login service exists and -// its running daemon differs from this build, either by reported version or by -// installed bytes. Manually started daemons and unreachable ones are left to -// the existing reuse path; only a positively stale managed daemon upgrades. +// managedDaemonNeedsUpgrade reports whether a managed login-service upgrade +// must run before normal installation proceeds. Platforms without automatic +// login-service support and installations without a managed unit are not +// applicable: manually started daemons keep the existing reuse path. A managed +// installation whose daemon is unreachable, reports another version, differs +// in installed bytes or unit, or has a pending activation is stale and must +// activate; only a positively current managed daemon is reused. Genuine +// inspection errors are returned so the caller fails loudly instead of +// silently reusing a daemon it could not evaluate. func managedDaemonNeedsUpgrade(paths bus.DaemonPaths) (bool, error) { home, err := os.UserHomeDir() if err != nil { @@ -330,8 +450,11 @@ func managedDaemonNeedsUpgrade(paths bus.DaemonPaths) (bool, error) { if err != nil { return false, err } - path, _, err := serviceFile(runtime.GOOS, home, binary, paths) + path, expected, err := serviceFile(runtime.GOOS, home, stableBinaryPath(paths), paths) if err != nil { + if errors.Is(err, errServiceUnsupported) { + return false, nil + } return false, err } owned, err := readConfigFile(path) @@ -341,30 +464,37 @@ func managedDaemonNeedsUpgrade(paths bus.DaemonPaths) (bool, error) { if len(owned) == 0 || !bytes.Contains(owned, []byte("Managed by Agent Intercom")) { return false, nil } + if pendingServiceActivation(paths) { + return true, nil + } health, _, err := probeDaemon(paths) if err != nil { - return false, nil + return true, nil } if health.RuntimeVersion != bus.Version { return true, nil } + if !bytes.Equal(owned, expected) { + return true, nil + } return !sameExecutableBytes(binary, stableBinaryPath(paths)), nil } // verifyServiceDaemon waits for the daemon to answer on the new build after a -// restart, requiring a new process identity when one was running before. It +// restart, requiring a new process identity when a process existed before. It // reports failures instead of letting a stale or missing daemon read as success. -func verifyServiceDaemon(paths bus.DaemonPaths, localVersion string, wasRunning bool, beforeRun bus.RunFile, operation string, out io.Writer) error { +func verifyServiceDaemon(paths bus.DaemonPaths, localVersion string, hadProcess bool, beforeRun bus.RunFile, operation string, out io.Writer) error { health, run, err := awaitServiceDaemon(paths, serviceActivationTimeout, func(next bus.Health, nextRun bus.RunFile) bool { if next.RuntimeVersion != localVersion { return false } - return !wasRunning || nextRun.PID != beforeRun.PID || nextRun.StartedAt != beforeRun.StartedAt + return !hadProcess || nextRun.PID != beforeRun.PID || nextRun.StartedAt != beforeRun.StartedAt }) if err != nil { return fmt.Errorf("service file saved but the daemon did not come up on the new build after %s; check service status and run service install again: %w", operation, err) } - if wasRunning { + clearServiceActivationPending(paths) + if hadProcess { fmt.Fprintf(out, "Login service upgraded and restarted on version %s (pid %d). Workspaces and messages are preserved; reconnect MCP in your apps.\n", health.RuntimeVersion, run.PID) return nil } diff --git a/cmd/agentintercom/service_upgrade_test.go b/cmd/agentintercom/service_upgrade_test.go index 112c4af..0d879bd 100644 --- a/cmd/agentintercom/service_upgrade_test.go +++ b/cmd/agentintercom/service_upgrade_test.go @@ -57,6 +57,14 @@ func setupFakeDaemon(t *testing.T, manager string) *fakeDaemon { http.Error(response, "no version", http.StatusInternalServerError) return } + if os.Getenv("FAKE_503_UNTIL_FLIP") == "1" && string(version) != os.Getenv("FAKE_AFTER_VERSION") { + response.WriteHeader(http.StatusServiceUnavailable) + _ = json.NewEncoder(response).Encode(map[string]string{ + "name": "october-bus", "protocolVersion": bus.ProtocolVersion, + "runtimeVersion": string(version), "status": "unavailable", "startedAt": upgradeBefore, + }) + return + } _ = json.NewEncoder(response).Encode(map[string]string{ "name": "october-bus", "protocolVersion": bus.ProtocolVersion, "runtimeVersion": string(version), "status": "ready", "startedAt": upgradeBefore, @@ -88,6 +96,7 @@ func setupFakeDaemon(t *testing.T, manager string) *fakeDaemon { body = "#!/bin/sh\necho \"$@\" >> \"$FAKE_SYSTEMCTL_LOG\"\n" + "case \"$1\" in\n" + " bootstrap)\n" + + " [ \"$FAKE_LAUNCHCTL_MODE\" = \"fail-bootstrap\" ] && exit 1\n" + " printf '%s' \"$FAKE_AFTER_VERSION\" > \"$FAKE_VERSION_FILE\"\n" + " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + " ;;\n" + @@ -103,6 +112,18 @@ func setupFakeDaemon(t *testing.T, manager string) *fakeDaemon { return fake } +func (f *fakeDaemon) writeDeadRunFile(t *testing.T) { + t.Helper() + // A run file naming a process that answers nothing: the daemon exists + // (or did) but readiness fails, so activation must restart, not skip. + data, err := json.Marshal(bus.RunFile{ + ProtocolVersion: bus.ProtocolVersion, Address: "http://127.0.0.1:1", + PID: 1000, StartedAt: upgradeBefore, AdminToken: upgradeToken, + }) + requireNoError(t, err) + requireNoError(t, os.WriteFile(f.runFile, append(data, '\n'), 0600)) +} + func (f *fakeDaemon) writeRunFile(t *testing.T, pid int, startedAt string) { t.Helper() data, err := json.Marshal(bus.RunFile{ @@ -211,6 +232,91 @@ func TestLinuxServiceInstallReportsRestartFailure(t *testing.T) { err := manageServiceOn("linux", "install", upgradeVersionB, &out) require(t, err != nil && strings.Contains(err.Error(), "restarting the login service failed"), "restart failure error = %v", err) require(t, !strings.Contains(out.String(), "upgraded"), "failure reported as success: %q", out.String()) + require(t, pendingServiceActivation(fake.paths), "failed activation left no pending evidence for retry") +} + +func TestLinuxServiceRetryAfterFailedSameVersionActivation(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + // Same reported version with different installed bytes; activation fails + // after the stable copy is written. + dest := stableBinaryPath(fake.paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + requireNoError(t, os.WriteFile(dest, []byte("older build"), 0700)) + fake.writeCurrentUnit(t, "linux", dest) + t.Setenv("FAKE_SYSTEMCTL_MODE", "fail-restart") + + var first bytes.Buffer + err := manageServiceOn("linux", "install", upgradeVersionB, &first) + require(t, err != nil, "failed activation returned success: %q", first.String()) + + // Retry with a working manager: files and versions now match, but the + // pending activation must still restart and replace the old process. + t.Setenv("FAKE_SYSTEMCTL_MODE", "") + var second bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &second)) + require(t, strings.Count(fake.managerLog(t), "--user restart") == 2, "retry did not restart: %q", fake.managerLog(t)) + require(t, !strings.Contains(second.String(), "already current"), "retry falsely reported current: %q", second.String()) + require(t, strings.Contains(second.String(), "upgraded and restarted"), "missing upgrade report: %q", second.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "retry left the old process: %#v", run) + require(t, !pendingServiceActivation(fake.paths), "successful retry left a pending marker") +} + +func TestLinuxServiceRecoversUnreachableManagedDaemon(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeDeadRunFile(t) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + + // The first probe fails, but the managed daemon must be restarted and + // verified, never skipped as absent. + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + require(t, strings.Contains(fake.managerLog(t), "--user restart"), "unreachable daemon was not restarted: %q", out.String()) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "run file still points at the dead process: %#v", run) + health, _, err := probeDaemon(fake.paths) + requireNoError(t, err) + require(t, health.RuntimeVersion == upgradeVersionB, "health still reports %q", health.RuntimeVersion) +} + +func TestLinuxServiceRecoversDaemonReturning503(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, "linux", fake.installCurrentBinary(t)) + t.Setenv("FAKE_503_UNTIL_FLIP", "1") + + // The old daemon answers 503 until restart; readiness must not gate + // activation or discard the known version change. + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + require(t, strings.Contains(fake.managerLog(t), "--user restart"), "503 daemon was not restarted: %q", out.String()) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + health, _, err := probeDaemon(fake.paths) + requireNoError(t, err) + require(t, health.RuntimeVersion == upgradeVersionB, "health still reports %q", health.RuntimeVersion) +} + +func TestLinuxServiceUnitOnlyChangeRestarts(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + fake.installCurrentBinary(t) + // Bytes and version match, but the installed unit drifted. + fake.writeCurrentUnit(t, "linux", filepath.Join(fake.root, "elsewhere", "agentintercom")) + + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + require(t, strings.Contains(fake.managerLog(t), "--user restart"), "unit change did not restart: %q", out.String()) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "unit change left the old process: %#v", run) } func TestLinuxServiceInstallVerifiesNewVersionAfterRestart(t *testing.T) { @@ -228,15 +334,19 @@ func TestLinuxServiceInstallVerifiesNewVersionAfterRestart(t *testing.T) { require(t, !strings.Contains(out.String(), "upgraded"), "stale daemon reported as success: %q", out.String()) } -func TestLinuxServiceFreshInstallStartsDaemonWithoutRestart(t *testing.T) { +func TestLinuxServiceFreshInstallStartsDaemonThroughRestart(t *testing.T) { fake := setupFakeDaemon(t, "systemctl") t.Setenv("FAKE_ENABLE_STARTS", "1") + // No daemon is provably current, so install restarts; restart also + // starts an inactive unit, which both starts fresh daemons and recovers + // unhealthy ones without gating on readiness first. var out bytes.Buffer requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) log := fake.managerLog(t) require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) - require(t, !strings.Contains(log, "--user restart"), "fresh install restarted: %q", log) + require(t, strings.Contains(log, "--user restart"), "fresh install skipped restart: %q", log) + require(t, strings.Contains(out.String(), "Login service installed"), "missing install report: %q", out.String()) health, _, err := probeDaemon(fake.paths) requireNoError(t, err) require(t, health.RuntimeVersion == upgradeVersionB, "fresh daemon reports %q", health.RuntimeVersion) @@ -270,6 +380,100 @@ func TestDarwinServiceReinstallWithoutChangesTouchesNothing(t *testing.T) { require(t, strings.Contains(out.String(), "already current"), "missing current report: %q", out.String()) } +func TestDarwinServiceBootstrapFailure(t *testing.T) { + fake := setupFakeDaemon(t, "launchctl") + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, "darwin", fake.installCurrentBinary(t)) + t.Setenv("FAKE_LAUNCHCTL_MODE", "fail-bootstrap") + + var out bytes.Buffer + err := manageServiceOn("darwin", "install", upgradeVersionB, &out) + require(t, err != nil && strings.Contains(err.Error(), "activation failed"), "bootstrap failure error = %v", err) + require(t, !strings.Contains(out.String(), "upgraded"), "failure reported as success: %q", out.String()) + require(t, pendingServiceActivation(fake.paths), "failed activation left no pending evidence for retry") +} + +func TestDarwinServiceRetryAfterFailedSameVersionActivation(t *testing.T) { + fake := setupFakeDaemon(t, "launchctl") + fake.writeRunFile(t, 1000, upgradeBefore) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + dest := stableBinaryPath(fake.paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + requireNoError(t, os.WriteFile(dest, []byte("older build"), 0700)) + fake.writeCurrentUnit(t, "darwin", dest) + t.Setenv("FAKE_LAUNCHCTL_MODE", "fail-bootstrap") + + var first bytes.Buffer + err := manageServiceOn("darwin", "install", upgradeVersionB, &first) + require(t, err != nil, "failed activation returned success: %q", first.String()) + + // Retry with a working manager: the pending activation must reactivate + // even though files and versions now match. + t.Setenv("FAKE_LAUNCHCTL_MODE", "") + var second bytes.Buffer + requireNoError(t, manageServiceOn("darwin", "install", upgradeVersionB, &second)) + require(t, strings.Count(fake.managerLog(t), "bootstrap") == 2, "retry did not reactivate: %q", fake.managerLog(t)) + require(t, !strings.Contains(second.String(), "already current"), "retry falsely reported current: %q", second.String()) + require(t, strings.Contains(second.String(), "upgraded and restarted"), "missing upgrade report: %q", second.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "retry left the old process: %#v", run) +} + +func TestInstallRecoversUnreachableManagedDaemon(t *testing.T) { + manager := "systemctl" + if runtime.GOOS == "darwin" { + manager = "launchctl" + } + fake := setupFakeDaemon(t, manager) + failEnv := "FAKE_SYSTEMCTL_MODE" + failValue := "fail-restart" + if manager == "launchctl" { + failEnv = "FAKE_LAUNCHCTL_MODE" + failValue = "fail-bootstrap" + } + // Saved workspace/app state with a managed daemon that answers nothing. + t.Setenv("FAKE_AFTER_VERSION", bus.Version) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(bus.Version), 0600)) + fake.writeDeadRunFile(t) + fake.writeCurrentUnit(t, runtime.GOOS, fake.installCurrentBinary(t)) + requireNoError(t, bus.SaveScopeToken(fake.paths.DataDir, "default", strings.Repeat("b", 43))) + configPath := filepath.Join(fake.root, "codex-config.toml") + requireNoError(t, os.WriteFile(configPath, []byte("# managed\n"), 0600)) + statePath, err := installState("codex") + requireNoError(t, err) + requireNoError(t, saveInstallState(statePath, []installedConfig{{Path: configPath, Before: nil, After: []byte("# managed\n")}})) + stub := filepath.Join(fake.root, "bin", "codex") + requireNoError(t, os.WriteFile(stub, []byte("#!/bin/sh\nexit 0\n"), 0700)) + + // A failed activation must fail the whole install, never print success. + t.Setenv(failEnv, failValue) + var failed bytes.Buffer + err = installCommand([]string{"--only", "codex"}, &failed) + require(t, err != nil, "failed managed upgrade returned success: %q", failed.String()) + require(t, !strings.Contains(failed.String(), "Reconnect MCP"), "failed install printed success: %q", failed.String()) + + // With a working manager the same command restarts, verifies the + // replacement, and completes the install. + t.Setenv(failEnv, "") + var out bytes.Buffer + requireNoError(t, installCommand([]string{"--only", "codex"}, &out)) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + require(t, strings.Contains(out.String(), "Reconnect MCP"), "missing install success: %q", out.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "install left the dead process: %#v", run) +} + +func TestServicePendingMarkerRoundTrip(t *testing.T) { + paths := bus.DaemonPaths{DataDir: t.TempDir(), RuntimeDir: t.TempDir()} + require(t, !pendingServiceActivation(paths), "fresh paths report pending") + requireNoError(t, markServiceActivationPending(paths, "9.9.9-test")) + require(t, pendingServiceActivation(paths), "marker was not recorded") + clearServiceActivationPending(paths) + require(t, !pendingServiceActivation(paths), "marker was not cleared") +} + func TestStableExecutableSkipsUnchangedCopy(t *testing.T) { root := t.TempDir() t.Setenv("OCTOBER_BUS_DATA_DIR", filepath.Join(root, "data")) @@ -331,7 +535,10 @@ func TestManagedDaemonNeedsUpgrade(t *testing.T) { requireNoError(t, err) require(t, !stale, "unmanaged service flagged for upgrade") - managedPath, managedBody, err := serviceFile(runtime.GOOS, home, source, paths) + // The managed unit references the stable executable path, as a real + // service install writes it. + dest := stableBinaryPath(paths) + managedPath, managedBody, err := serviceFile(runtime.GOOS, home, dest, paths) requireNoError(t, err) require(t, managedPath == unmanagedPath, "service file moved: %q", managedPath) requireNoError(t, writeConfigFile(managedPath, managedBody)) @@ -342,7 +549,6 @@ func TestManagedDaemonNeedsUpgrade(t *testing.T) { require(t, stale, "byte-different daemon not flagged") // Identical bytes and version: current. - dest := stableBinaryPath(paths) requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) content, err := os.ReadFile(source) requireNoError(t, err) @@ -351,9 +557,25 @@ func TestManagedDaemonNeedsUpgrade(t *testing.T) { requireNoError(t, err) require(t, !stale, "current daemon flagged for upgrade") + // A pending activation from a failed attempt: stale even though every + // file and the reported version match. + requireNoError(t, markServiceActivationPending(paths, bus.Version)) + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, stale, "pending activation not flagged") + clearServiceActivationPending(paths) + // Older reported version: stale even with identical bytes. requireNoError(t, os.WriteFile(versionFile, []byte("0.0.0-old"), 0600)) stale, err = managedDaemonNeedsUpgrade(paths) requireNoError(t, err) require(t, stale, "older version not flagged") + requireNoError(t, os.WriteFile(versionFile, []byte(bus.Version), 0600)) + + // A managed daemon that answers nothing is stale: normal install must + // activate (restart and verify) instead of silently reusing it. + server.Close() + stale, err = managedDaemonNeedsUpgrade(paths) + requireNoError(t, err) + require(t, stale, "unreachable managed daemon not flagged") } diff --git a/docs/installation.md b/docs/installation.md index f2d7ce6..95534cb 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -101,20 +101,32 @@ system-wide login policy or install a root service. ## Upgrading the login service After upgrading the package, re-run `agentintercom service install` (or -`agentintercom install`, which upgrades a stale managed daemon instead of -reusing it). The flow replaces the stable executable, reactivates the user -service, and restarts the managed daemon only when it is not already on the -new build: on Linux it runs `daemon-reload` and `enable --now`, then `restart` -when the running daemon reports an older version or the installed bytes or -unit changed; on macOS it re-bootstraps unless everything is already current. -Reinstalling an unchanged build never restarts the running daemon. - -Every install verifies the running daemon's health/version before reporting -success, so activation failures are reported as failures, never as a -successful upgrade. The restart keeps the same data directory, preserving -workspaces, accepted messages, credentials, and unrelated app settings; -reconnect MCP in your apps (or restart them) after an upgrade restart so they -attach to the new daemon process. +`agentintercom install`, which upgrades a managed daemon that is not provably +on the invoking build instead of reusing it). The flow replaces the stable +executable, records a pending activation, reactivates the user service, and +restarts unless the managed daemon is provably already on this build: healthy, +reporting this version, with matching installed bytes and unit and no pending +activation. On Linux it runs `daemon-reload` and `enable --now`, then +`restart` (`restart` also starts an inactive unit, so an unhealthy managed +daemon is recovered rather than skipped); on macOS it re-bootstraps unless +everything is already current. Reinstalling an unchanged build never restarts +the running daemon. + +Every restart is verified before success is reported: the replacement must +answer health on the new version with a new process identity. A failed or +interrupted activation keeps its pending marker, so running the same command +again reactivates and verifies instead of falsely reporting "already +current" — including same-version rebuilds whose files already match. A +managed daemon that is unhealthy or unreachable is restarted and verified, or +the install fails with a non-zero error; it is never silently reused as a +success. Manually started daemons (no managed unit) keep the existing reuse +behavior, and platforms without login-service support are unaffected. + +The restart keeps the same data directory, preserving workspaces, accepted +messages, credentials, and unrelated app settings; reconnect MCP in your apps +(or restart them) after an upgrade restart so they attach to the new daemon +process. Running an older copy can instead downgrade the service; a daemon +that cannot read the current database schema refuses to start. ## Monitor without consuming messages diff --git a/docs/operations.md b/docs/operations.md index 86e782e..fa7a899 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -211,7 +211,9 @@ Startup also refuses a run file naming a possibly live process, to avoid taking The daemon handles interrupt and termination signals and shuts down active HTTP requests before closing SQLite. A service supervisor should restart unexpected exits with bounded backoff. -The repository does not yet ship platform service definitions. Keep credentials out of service arguments and logs when adding one. +`agentintercom service install` writes the managed user-service definition +(systemd user unit on Linux, LaunchAgent plist on macOS). Keep credentials +out of service arguments and logs. ## Recovery From 815d26f7e8d7cc115bc0822dc9a16c97aa73a8f1 Mon Sep 17 00:00:00 2001 From: Harsh Date: Fri, 13 Nov 2026 12:13:31 -0800 Subject: [PATCH 3/3] Fix marker ordering, unreadable-marker handling, and Linux double start - stableExecutable records pending activation before the new bytes become visible; a failed recording leaves the old executable in place. - pendingServiceActivation treats an unreadable/rejected marker as pending, so an unevaluated managed daemon can never read current; the follow-up marker write fails loudly while the file stays unreadable. - Linux activation uses plain enable; restart alone starts an inactive unit, so a stopped service is started exactly once. - Regression tests: copy-without-activation then same-version restart, recording failure keeps old bytes, unreadable marker fails normal install, and enable --now fails the fake manager outright. --- cmd/agentintercom/service.go | 38 +++++--- cmd/agentintercom/service_upgrade_test.go | 114 ++++++++++++++++++++-- docs/installation.md | 8 +- 3 files changed, 136 insertions(+), 24 deletions(-) diff --git a/cmd/agentintercom/service.go b/cmd/agentintercom/service.go index d41cc00..e49434a 100644 --- a/cmd/agentintercom/service.go +++ b/cmd/agentintercom/service.go @@ -39,22 +39,29 @@ func stableBinaryPath(paths bus.DaemonPaths) string { } func stableExecutable() (string, error) { - dest, changed, err := installStableExecutable() + source, err := os.Executable() if err != nil { return "", err } - if changed { - // A copy without activation must not look current later: record it - // so the next service install activates instead of skipping. - paths, err := bus.DefaultDaemonPaths() - if err != nil { - return "", err - } + paths, err := bus.DefaultDaemonPaths() + if err != nil { + return "", err + } + dest := stableBinaryPath(paths) + if filepath.Clean(source) != filepath.Clean(dest) && !sameExecutableBytes(source, dest) { + // Record pending activation BEFORE the new bytes become visible: a + // copy without activation must not look current later, and a managed + // daemon's build must never be replaced without evidence the running + // process is stale. If recording fails, the old bytes stay in place. if err := markServiceActivationPending(paths, bus.Version); err != nil { return "", err } } - return dest, nil + installed, _, err := installStableExecutable() + if err != nil { + return "", err + } + return installed, nil } // pendingServicePath locates the marker recording that a service install @@ -68,7 +75,14 @@ func pendingServicePath(paths bus.DaemonPaths) string { func pendingServiceActivation(paths bus.DaemonPaths) bool { data, err := readConfigFile(pendingServicePath(paths)) - return err == nil && len(data) > 0 + if err != nil { + // An unreadable or rejected marker is not absence: conservatively + // require activation so an unevaluated daemon can never read + // current. The subsequent marker write rechecks the file and fails + // loudly while it stays unreadable. + return true + } + return len(data) > 0 } func markServiceActivationPending(paths bus.DaemonPaths, localVersion string) error { @@ -413,7 +427,9 @@ func activateLinuxService(activation serviceActivation) error { if err := run("--user", "daemon-reload"); err != nil { return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) } - if err := run("--user", "enable", "--now", unit); err != nil { + // Plain enable: the conditional restart below starts an inactive unit, + // so --now would start a stopped service twice. + if err := run("--user", "enable", unit); err != nil { return fmt.Errorf("service file saved but activation failed; run service install again after restoring your user service manager: %w", err) } if !activation.binaryChanged && !activation.unitChanged && !activation.hadPending && activation.wasHealthy && activation.beforeHealth.RuntimeVersion == localVersion { diff --git a/cmd/agentintercom/service_upgrade_test.go b/cmd/agentintercom/service_upgrade_test.go index 0d879bd..a71fe1a 100644 --- a/cmd/agentintercom/service_upgrade_test.go +++ b/cmd/agentintercom/service_upgrade_test.go @@ -86,10 +86,8 @@ func setupFakeDaemon(t *testing.T, manager string) *fakeDaemon { " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + " ;;\n" + " *\"--user enable --now \"*)\n" + - " [ \"$FAKE_ENABLE_STARTS\" = \"1\" ] && {\n" + - " printf '%s' \"$FAKE_AFTER_VERSION\" > \"$FAKE_VERSION_FILE\"\n" + - " printf '{\"protocolVersion\":\"0.1\",\"address\":\"%s\",\"pid\":2000,\"startedAt\":\"" + upgradeAfter + "\",\"adminToken\":\"" + upgradeToken + "\"}' \"$FAKE_ADDRESS\" > \"$FAKE_RUN_FILE\"\n" + - " }\n" + + " echo \"enable must not start the unit\" >&2\n" + + " exit 1\n" + " ;;\n" + "esac\nexit 0\n" } else { @@ -173,7 +171,8 @@ func TestLinuxServiceInstallRestartsStaleDaemon(t *testing.T) { requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) log := fake.managerLog(t) require(t, strings.Contains(log, "--user daemon-reload"), "missing daemon-reload: %q", log) - require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) + require(t, strings.Contains(log, "--user enable "), "missing enable: %q", log) + require(t, !strings.Contains(log, "--now"), "enable must not start the unit: %q", log) require(t, strings.Contains(log, "--user restart"), "stale daemon was not restarted: %q", log) require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) @@ -214,7 +213,8 @@ func TestLinuxServiceReinstallWithoutChangesSkipsRestart(t *testing.T) { var out bytes.Buffer requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) log := fake.managerLog(t) - require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) + require(t, strings.Contains(log, "--user enable "), "missing enable: %q", log) + require(t, !strings.Contains(log, "--now"), "enable must not start the unit: %q", log) require(t, !strings.Contains(log, "--user restart"), "unchanged reinstall restarted: %q", log) require(t, strings.Contains(out.String(), "already current"), "missing current report: %q", out.String()) run, err := bus.ReadRunFile(fake.runFile) @@ -336,16 +336,18 @@ func TestLinuxServiceInstallVerifiesNewVersionAfterRestart(t *testing.T) { func TestLinuxServiceFreshInstallStartsDaemonThroughRestart(t *testing.T) { fake := setupFakeDaemon(t, "systemctl") - t.Setenv("FAKE_ENABLE_STARTS", "1") // No daemon is provably current, so install restarts; restart also // starts an inactive unit, which both starts fresh daemons and recovers - // unhealthy ones without gating on readiness first. + // unhealthy ones without gating on readiness first. Plain enable never + // starts, so exactly one start is requested (the fake fails any + // enable --now outright). var out bytes.Buffer requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) log := fake.managerLog(t) - require(t, strings.Contains(log, "--user enable --now"), "missing enable --now: %q", log) - require(t, strings.Contains(log, "--user restart"), "fresh install skipped restart: %q", log) + require(t, strings.Contains(log, "--user enable "), "missing enable: %q", log) + require(t, !strings.Contains(log, "--now"), "enable must not start the unit: %q", log) + require(t, strings.Count(log, "--user restart") == 1, "fresh install must request exactly one start: %q", log) require(t, strings.Contains(out.String(), "Login service installed"), "missing install report: %q", out.String()) health, _, err := probeDaemon(fake.paths) requireNoError(t, err) @@ -474,6 +476,98 @@ func TestServicePendingMarkerRoundTrip(t *testing.T) { require(t, !pendingServiceActivation(paths), "marker was not cleared") } +func TestStableExecutableCopyWithoutActivationRequiresSameVersionRestart(t *testing.T) { + fake := setupFakeDaemon(t, "systemctl") + requireNoError(t, os.WriteFile(fake.versionFile, []byte(upgradeVersionB), 0600)) + fake.writeRunFile(t, 1000, upgradeBefore) + // Same reported version (for example two dev builds), but the installed + // copy does not match this executable. + dest := stableBinaryPath(fake.paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + requireNoError(t, os.WriteFile(dest, []byte("older build"), 0700)) + fake.writeCurrentUnit(t, "linux", dest) + + // A copy without activation (the install --no-service path) records + // pending state before the new bytes become visible. + installed, err := stableExecutable() + requireNoError(t, err) + require(t, installed == dest, "stable path moved: %q", installed) + require(t, pendingServiceActivation(fake.paths), "copy without activation left no pending evidence") + + // The follow-up same-version service install must restart and replace + // the old process even though files and versions now match. + var out bytes.Buffer + requireNoError(t, manageServiceOn("linux", "install", upgradeVersionB, &out)) + require(t, strings.Contains(fake.managerLog(t), "--user restart"), "same-version activation skipped restart: %q", out.String()) + require(t, !strings.Contains(out.String(), "already current"), "activation falsely reported current: %q", out.String()) + require(t, strings.Contains(out.String(), "upgraded and restarted"), "missing upgrade report: %q", out.String()) + run, err := bus.ReadRunFile(fake.runFile) + requireNoError(t, err) + require(t, run.PID == 2000 && run.StartedAt == upgradeAfter, "activation left the old process: %#v", run) + require(t, !pendingServiceActivation(fake.paths), "successful activation left a pending marker") +} + +func TestStableExecutableMarkerFailureLeavesBytesUnchanged(t *testing.T) { + if os.PathSeparator == '\\' { + t.Skip("Unix permission semantics") + } + root := t.TempDir() + data := filepath.Join(root, "data") + t.Setenv("OCTOBER_BUS_DATA_DIR", data) + t.Setenv("OCTOBER_BUS_RUNTIME_DIR", filepath.Join(root, "run")) + paths, err := bus.DefaultDaemonPaths() + requireNoError(t, err) + dest := stableBinaryPath(paths) + requireNoError(t, os.MkdirAll(filepath.Dir(dest), 0700)) + requireNoError(t, os.WriteFile(dest, []byte("older build"), 0700)) + + // Recording must fail: the data directory is read-only, so no pending + // marker can be written. + requireNoError(t, os.Chmod(data, 0500)) + t.Cleanup(func() { _ = os.Chmod(data, 0700) }) + if probe, perr := os.CreateTemp(data, ".probe-*"); perr == nil { + _ = probe.Close() + _ = os.Remove(probe.Name()) + t.Skip("data directory stayed writable (privileged user?)") + } + _, err = stableExecutable() + require(t, err != nil, "recording failure returned success") + kept, rerr := os.ReadFile(dest) + requireNoError(t, rerr) + require(t, string(kept) == "older build", "failed recording replaced the executable") +} + +func TestInstallFailsOnUnreadablePendingMarker(t *testing.T) { + manager := "systemctl" + if runtime.GOOS == "darwin" { + manager = "launchctl" + } + fake := setupFakeDaemon(t, manager) + // Saved workspace/app state with a daemon that looks current in every + // other respect: same version, matching bytes and unit. + t.Setenv("FAKE_AFTER_VERSION", bus.Version) + requireNoError(t, os.WriteFile(fake.versionFile, []byte(bus.Version), 0600)) + fake.writeRunFile(t, 1000, upgradeBefore) + fake.writeCurrentUnit(t, runtime.GOOS, fake.installCurrentBinary(t)) + requireNoError(t, bus.SaveScopeToken(fake.paths.DataDir, "default", strings.Repeat("b", 43))) + configPath := filepath.Join(fake.root, "codex-config.toml") + requireNoError(t, os.WriteFile(configPath, []byte("# managed\n"), 0600)) + statePath, err := installState("codex") + requireNoError(t, err) + requireNoError(t, saveInstallState(statePath, []installedConfig{{Path: configPath, Before: nil, After: []byte("# managed\n")}})) + stub := filepath.Join(fake.root, "bin", "codex") + requireNoError(t, os.WriteFile(stub, []byte("#!/bin/sh\nexit 0\n"), 0700)) + + // The marker path exists but is rejected (a directory, not a file), so + // the managed daemon cannot be evaluated: install must fail loudly, + // never claim success. + requireNoError(t, os.MkdirAll(pendingServicePath(fake.paths), 0700)) + var out bytes.Buffer + err = installCommand([]string{"--only", "codex"}, &out) + require(t, err != nil, "unreadable marker returned success: %q", out.String()) + require(t, !strings.Contains(out.String(), "Reconnect MCP"), "failed install printed success: %q", out.String()) +} + func TestStableExecutableSkipsUnchangedCopy(t *testing.T) { root := t.TempDir() t.Setenv("OCTOBER_BUS_DATA_DIR", filepath.Join(root, "data")) diff --git a/docs/installation.md b/docs/installation.md index 95534cb..351dabe 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -106,11 +106,13 @@ on the invoking build instead of reusing it). The flow replaces the stable executable, records a pending activation, reactivates the user service, and restarts unless the managed daemon is provably already on this build: healthy, reporting this version, with matching installed bytes and unit and no pending -activation. On Linux it runs `daemon-reload` and `enable --now`, then +activation. On Linux it runs `daemon-reload` and `enable`, then `restart` (`restart` also starts an inactive unit, so an unhealthy managed -daemon is recovered rather than skipped); on macOS it re-bootstraps unless +daemon is recovered rather than skipped, and a stopped service is started +exactly once); on macOS it re-bootstraps unless everything is already current. Reinstalling an unchanged build never restarts -the running daemon. +a healthy running daemon when the installed unit is unchanged and no +activation is pending. Every restart is verified before success is reported: the replacement must answer health on the new version with a new process identity. A failed or