High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: daemon stop and uninstall report success for a service that was… (PIN-098)
Luigi Agosti4cc56f1f63fe
diff --git a/cmd/pinchtab/cmd_daemon.go b/cmd/pinchtab/cmd_daemon.goindex 70d6aa78..eb30f83d 100644--- a/cmd/pinchtab/cmd_daemon.go+++ b/cmd/pinchtab/cmd_daemon.go@@ -54,7 +54,8 @@ func dispatchDaemonCommand(subcommand string, jsonOut bool) int {if !declared {return printDaemonUsage(subcommand)}- if handled, code := applyDaemonNotInstalledPolicy(subcommand, policy); handled {+ notInstalled, code, refused := applyDaemonNotInstalledPolicy(subcommand, policy)+ if refused {return code}@@ -72,9 +73,9 @@ func dispatchDaemonCommand(subcommand string, jsonOut bool) int {case "restart":printDaemonManagerResult(manager.Restart())case "stop":- printDaemonManagerResult(manager.Stop())+ handleDaemonStop(manager, notInstalled)case "uninstall":- handleDaemonUninstall(manager)+ handleDaemonUninstall(manager, notInstalled)default:return printDaemonUsage(subcommand)}@@ -95,28 +96,14 @@ func isDaemonStatusSubcommand(subcommand string) bool {return false}-// daemonNotInstalledPolicy is what one lifecycle verb does when the service is not-// installed. Every verb DECLARES its answer: the previous guard skipped the check for-// anything that was not start or restart, so stop and uninstall bypassed it by-// construction and affirmed work they had not done. A verb missing from the table below-// is not silently unguarded — it does not dispatch at all.type daemonNotInstalledPolicy intconst (- // daemonRefuse: exit 1 with the install remedy, touching no manager.daemonRefuse daemonNotInstalledPolicy = iota- // daemonNoOp: exit 0 saying plainly that there was nothing to do.daemonNoOp- // daemonProceed: the not-installed state is this verb's normal input.daemonProceed)-// The contract is idempotent for stop and uninstall, and that is not a fresh choice —-// it is the one the manager layer already made. launchdManager.Stop swallows the-// not-loaded error and Uninstall swallows os.ErrNotExist on removing the plist, both-// deliberately, so refusing here would contradict the layer below and break the-// provisioning chain `daemon stop && daemon install && daemon start`, which never-// reaches install if stop exits 1.var daemonNotInstalledPolicies = map[string]daemonNotInstalledPolicy{"install": daemonProceed,"start": daemonRefuse,@@ -125,21 +112,14 @@ var daemonNotInstalledPolicies = map[string]daemonNotInstalledPolicy{"uninstall": daemonNoOp,}-// daemonNothingToDo is the honest wording for a no-op verb. It states the observed fact-// and what did not happen, where " [ok] Pinchtab daemon stopped." asserted an action-// that was never performed — which an operator, or a provisioning script reading exit 0-// plus that sentence, takes as confirmation.-var daemonNothingToDo = map[string]string{- "stop": "Background service is not installed; nothing to stop.",- "uninstall": "Background service is not installed; nothing to uninstall.",+var daemonNotInstalledResults = map[string]string{+ "stop": "Background service is not installed; asked the service manager to stop any leftover job.",+ "uninstall": "Background service is not installed; asked the service manager to remove any leftover job.",}-// applyDaemonNotInstalledPolicy asks the installation question ONCE and lets the verb's-// policy decide the wording and the exit code. It reports handled=true when the verb is-// finished and must not reach a manager.-func applyDaemonNotInstalledPolicy(subcommand string, policy daemonNotInstalledPolicy) (bool, int) {+func applyDaemonNotInstalledPolicy(subcommand string, policy daemonNotInstalledPolicy) (notInstalled bool, code int, refused bool) {if policy == daemonProceed {- return false, 0+ return false, 0, false}installed, err := daemonInstallationStatus()@@ -147,25 +127,30 @@ func applyDaemonNotInstalledPolicy(subcommand string, policy daemonNotInstalledPif policy == daemonRefuse {fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle,fmt.Sprintf("cannot determine whether the background service is installed; refusing to %s: %v", subcommand, err)))- return true, 1+ return false, 1, true}- // Could-not-read is not absence, and must never be rendered as one: "nothing to- // stop" built from an unknown is a false statement. The managers already tolerate- // a service that is not there, so the honest move is to attempt the operation and- // report whatever they say.- return false, 0+ return false, 0, false}if installed {- return false, 0+ return false, 0, false}-if policy == daemonRefuse {fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle,"background service is not installed; install it first with: pinchtab daemon install"))- return true, 1+ return false, 1, true+ }+ return true, 0, false+}++func daemonLifecycleMessage(subcommand string, notInstalled bool, message string) string {+ if notInstalled {+ return daemonNotInstalledResults[subcommand]}- fmt.Println(cli.StyleStdout(cli.SuccessStyle, " [ok] ") + daemonNothingToDo[subcommand])- return true, 0+ return message+}++func printDaemonOK(message string) {+ fmt.Println(cli.StyleStdout(cli.SuccessStyle, " [ok] ") + message)}func handleDaemonInstall(manager daemon.Manager) {@@ -186,14 +171,11 @@ func handleDaemonInstall(manager daemon.Manager) {if err != nil {printDaemonActionError(manager, fmt.Sprintf("daemon install failed: %v", err))}- fmt.Println(cli.StyleStdout(cli.SuccessStyle, " [ok] ") + message)+ printDaemonOK(message)warnPrimaryChromeMacOS(loadConfig())printDaemonFollowUp()}-// warnPrimaryChromeMacOS surfaces the issue #583 collision at install time:-// on macOS, auto-launching the user's daily Google Chrome for headless-// automation can stop their normal Chrome from opening a window.func warnPrimaryChromeMacOS(cfg *config.RuntimeConfig) {effective := runtimekit.ResolveEffectiveBrowser(cfg)if effective.ID != config.BrowserChrome || !chrome.IsPrimaryChromeBinaryMacOS(effective.Binary) {@@ -209,10 +191,19 @@ func warnPrimaryChromeMacOS(cfg *config.RuntimeConfig) {" to a dedicated automation browser."))}-func handleDaemonUninstall(manager daemon.Manager) {+func handleDaemonStop(manager daemon.Manager, notInstalled bool) {+ message, err := manager.Stop()+ if err != nil {+ printDaemonManagerResult(message, err)+ return+ }+ printDaemonOK(daemonLifecycleMessage("stop", notInstalled, message))+}++func handleDaemonUninstall(manager daemon.Manager, notInstalled bool) {message, err := manager.Uninstall()if err != nil {printDaemonActionError(manager, err.Error())}- fmt.Println(cli.StyleStdout(cli.SuccessStyle, " [ok] ") + message)+ printDaemonOK(daemonLifecycleMessage("uninstall", notInstalled, message))}diff --git a/cmd/pinchtab/cmd_daemon_test.go b/cmd/pinchtab/cmd_daemon_test.goindex ca5332f5..43a91641 100644--- a/cmd/pinchtab/cmd_daemon_test.go+++ b/cmd/pinchtab/cmd_daemon_test.go@@ -140,9 +140,6 @@ func TestDaemonActionsReachManager(t *testing.T) {}{{sub: "start", installed: true, want: []string{"Start"}},{sub: "restart", installed: true, want: []string{"Restart"}},- // installed:true on purpose. This case used to read installed:false and expect- // the manager to be called, which encoded the defect: stop reached the manager- // with nothing installed and then affirmed it had stopped something.{sub: "stop", installed: true, want: []string{"Stop"}},{sub: "uninstall", installed: true, want: []string{"Uninstall"}},}@@ -249,23 +246,13 @@ func TestPrintDaemonOverviewIncludesStatusAndHints(t *testing.T) {}}-// The no-op verbs must not affirm work they did not do. " [ok] Pinchtab daemon-// stopped." is an affirmative statement, and an operator — or a provisioning script-// reading exit 0 beside it — takes it as confirmation that a service was stopped.-// Exit 0 stays: the manager layer already swallows not-loaded and os.ErrNotExist-// deliberately, and `daemon stop && daemon install && daemon start` never reaches-// install if stop refuses.func TestDaemonStopAndUninstallReportAnHonestNoOpWhenNotInstalled(t *testing.T) {- for _, tc := range []struct {- sub string- claim string- nothing string- }{- {sub: "stop", claim: "stopped", nothing: "nothing to stop"},- {sub: "uninstall", claim: "uninstalled", nothing: "nothing to uninstall"},+ for _, tc := range []struct{ sub, claim string }{+ {sub: "stop", claim: "stopped"},+ {sub: "uninstall", claim: "uninstalled"},} {t.Run(tc.sub, func(t *testing.T) {- manager, constructed := stubDaemonSeams(t, false, nil)+ stubDaemonSeams(t, false, nil)var code intoutput := captureStdout(t, func() {@@ -278,26 +265,42 @@ func TestDaemonStopAndUninstallReportAnHonestNoOpWhenNotInstalled(t *testing.T)if strings.Contains(output, tc.claim) {t.Errorf("output claims the service was %s when nothing was installed: %q", tc.claim, output)}- if !strings.Contains(strings.ToLower(output), tc.nothing) {- t.Errorf("output %q does not say there was %s", output, tc.nothing)+ if strings.Contains(strings.ToLower(output), "nothing to") {+ t.Errorf("output %q predicts there was nothing to do; the installation probe cannot support that claim about a running job", output)}if !strings.Contains(strings.ToLower(output), "not installed") {t.Errorf("output %q does not state the observed fact", output)}- if len(manager.calls) != 0 {- t.Errorf("manager was asked to act with nothing installed: %v", manager.calls)+ })+ }+}++func TestDaemonNoOpVerbsStillActWhenNotInstalled(t *testing.T) {+ for _, tc := range []struct{ sub, want string }{+ {sub: "stop", want: "Stop"},+ {sub: "uninstall", want: "Uninstall"},+ } {+ t.Run(tc.sub, func(t *testing.T) {+ manager, constructed := stubDaemonSeams(t, false, nil)++ var code int+ captureStdout(t, func() {+ code = dispatchDaemonCommand(tc.sub, false)+ })++ if code != 0 {+ t.Fatalf("exit code = %d, want 0", code)}- if *constructed != 0 {- t.Errorf("manager constructed %d times, want 0", *constructed)+ if *constructed != 1 {+ t.Fatalf("manager constructed %d times, want 1 — an absent service file is not an absent job", *constructed)+ }+ if strings.Join(manager.calls, ",") != tc.want {+ t.Errorf("manager calls = %v, want [%s] — a job left loaded without its service file has no other way out", manager.calls, tc.want)}})}}-// Could-not-read is not absence. Rendering an unknown as "nothing to stop" is a false-// statement built from a failed probe, so the no-op verbs must never say it when the-// status query itself failed. The managers already tolerate a missing service, so the-// honest behaviour is to attempt the operation and report what they say.func TestDaemonStopAndUninstallDoNotClaimNothingToDoWhenTheStateIsUnknown(t *testing.T) {for _, tc := range []struct{ sub, want string }{{sub: "stop", want: "Stop"},@@ -326,8 +329,6 @@ func TestDaemonStopAndUninstallDoNotClaimNothingToDoWhenTheStateIsUnknown(t *tes}}-// The absence assertion: the no-op wording must not become unconditional. With a service-// actually installed, stop still reports the real stopped message from the manager.func TestDaemonStopStillReportsTheRealResultWhenInstalled(t *testing.T) {manager, _ := stubDaemonSeams(t, true, nil)@@ -350,10 +351,6 @@ func TestDaemonStopStillReportsTheRealResultWhenInstalled(t *testing.T) {}}-// The guard used to be a two-name allowlist, so every verb that was not start or restart-// inherited "no check" by construction. A verb must now DECLARE its not-installed-// behaviour — which is not the same as being forced to refuse — and one that does not-// declare cannot dispatch at all, so the omission is loud rather than silent.func TestEveryDispatchedDaemonVerbDeclaresItsNotInstalledPolicy(t *testing.T) {raw, err := os.ReadFile("cmd_daemon.go")if err != nil {@@ -382,10 +379,10 @@ func TestEveryDispatchedDaemonVerbDeclaresItsNotInstalledPolicy(t *testing.T) {}}for verb, policy := range daemonNotInstalledPolicies {- if policy == daemonNoOp && daemonNothingToDo[verb] == "" {+ if policy == daemonNoOp && daemonNotInstalledResults[verb] == "" {t.Errorf("%q is a no-op verb with no honest wording, so it would print an empty claim", verb)}- if policy != daemonNoOp && daemonNothingToDo[verb] != "" {+ if policy != daemonNoOp && daemonNotInstalledResults[verb] != "" {t.Errorf("%q is not a no-op verb but carries no-op wording", verb)}}