Skip to content

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 never installed, because the not-installed guard is a two-name allowlist (PIN-098)

Luigi Agosti948cb745c0a6
diff --git a/cmd/pinchtab/cmd_daemon.go b/cmd/pinchtab/cmd_daemon.go
index a08c6599..b304e0e3 100644
--- a/cmd/pinchtab/cmd_daemon.go
+++ b/cmd/pinchtab/cmd_daemon.go
@@ -50,9 +50,12 @@ func dispatchDaemonCommand(subcommand string, jsonOut bool) int {
return 0
}
- if err := requireDaemonInstalled(subcommand); err != nil {
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, err.Error()))
- return 1
+ policy, declared := daemonNotInstalledPolicies[subcommand]
+ if !declared {
+ return printDaemonUsage(subcommand)
+ }
+ if handled, code := applyDaemonNotInstalledPolicy(subcommand, policy); handled {
+ return code
}
manager, err := daemonCurrentManager()
@@ -73,13 +76,17 @@ func dispatchDaemonCommand(subcommand string, jsonOut bool) int {
case "uninstall":
handleDaemonUninstall(manager)
default:
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, fmt.Sprintf("unknown daemon command: %s", subcommand)))
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.MutedStyle, "Usage: pinchtab daemon <status|install|start|restart|stop|uninstall>"))
- return 2
+ return printDaemonUsage(subcommand)
}
return 0
}
+func printDaemonUsage(subcommand string) int {
+ fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, fmt.Sprintf("unknown daemon command: %s", subcommand)))
+ fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.MutedStyle, "Usage: pinchtab daemon <status|install|start|restart|stop|uninstall>"))
+ return 2
+}
+
func isDaemonStatusSubcommand(subcommand string) bool {
switch subcommand {
case "", "status", "help", "--help", "-h":
@@ -88,18 +95,77 @@ func isDaemonStatusSubcommand(subcommand string) bool {
return false
}
-func requireDaemonInstalled(subcommand string) error {
- if subcommand != "start" && subcommand != "restart" {
- return nil
+// 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 int
+
+const (
+ // 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,
+ "restart": daemonRefuse,
+ "stop": daemonNoOp,
+ "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.",
+}
+
+// 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) {
+ if policy == daemonProceed {
+ return false, 0
}
+
installed, err := daemonInstallationStatus()
if err != nil {
- return fmt.Errorf("cannot determine whether the background service is installed; refusing to %s: %w", subcommand, err)
+ if 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
+ }
+ // 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
}
if installed {
- return nil
+ return false, 0
+ }
+
+ 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 fmt.Errorf("background service is not installed; install it first with: pinchtab daemon install")
+ fmt.Println(cli.StyleStdout(cli.SuccessStyle, " [ok] ") + daemonNothingToDo[subcommand])
+ return true, 0
}
func handleDaemonInstall(manager daemon.Manager) {
diff --git a/cmd/pinchtab/cmd_daemon_test.go b/cmd/pinchtab/cmd_daemon_test.go
index 7732634e..3eef22cb 100644
--- a/cmd/pinchtab/cmd_daemon_test.go
+++ b/cmd/pinchtab/cmd_daemon_test.go
@@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
+ "os"
"strings"
"testing"
@@ -139,7 +140,11 @@ func TestDaemonActionsReachManager(t *testing.T) {
}{
{sub: "start", installed: true, want: []string{"Start"}},
{sub: "restart", installed: true, want: []string{"Restart"}},
- {sub: "stop", installed: false, want: []string{"Stop"}},
+ // 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"}},
}
for _, tc := range cases {
t.Run(tc.sub, func(t *testing.T) {
@@ -243,3 +248,145 @@ 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"},
+ } {
+ t.Run(tc.sub, func(t *testing.T) {
+ manager, constructed := stubDaemonSeams(t, false, nil)
+
+ var code int
+ output := captureStdout(t, func() {
+ code = dispatchDaemonCommand(tc.sub, false)
+ })
+
+ if code != 0 {
+ t.Fatalf("exit code = %d, want 0 — the contract is idempotent", code)
+ }
+ 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), "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)
+ }
+ if *constructed != 0 {
+ t.Errorf("manager constructed %d times, want 0", *constructed)
+ }
+ })
+ }
+}
+
+// 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"},
+ {sub: "uninstall", want: "Uninstall"},
+ } {
+ t.Run(tc.sub, func(t *testing.T) {
+ manager, _ := stubDaemonSeams(t, false, errors.New("permission denied"))
+
+ var code int
+ output := captureStdout(t, func() {
+ code = dispatchDaemonCommand(tc.sub, false)
+ })
+
+ if code != 0 {
+ t.Fatalf("exit code = %d, want 0", code)
+ }
+ for _, forbidden := range []string{"nothing to", "not installed"} {
+ if strings.Contains(strings.ToLower(output), forbidden) {
+ t.Errorf("output %q claims %q from a state that could not be read", output, forbidden)
+ }
+ }
+ if strings.Join(manager.calls, ",") != tc.want {
+ t.Errorf("manager calls = %v, want [%s] — an unknown state must be attempted, not assumed absent", manager.calls, tc.want)
+ }
+ })
+ }
+}
+
+// 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)
+
+ var code int
+ output := captureStdout(t, func() {
+ code = dispatchDaemonCommand("stop", false)
+ })
+
+ if code != 0 {
+ t.Fatalf("exit code = %d, want 0", code)
+ }
+ if !strings.Contains(output, "Pinchtab daemon stopped.") {
+ t.Errorf("output %q is not the manager's real result", output)
+ }
+ if strings.Contains(strings.ToLower(output), "nothing to stop") {
+ t.Errorf("the no-op wording is unconditional: %q", output)
+ }
+ if strings.Join(manager.calls, ",") != "Stop" {
+ t.Errorf("manager calls = %v, want [Stop]", manager.calls)
+ }
+}
+
+// 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 {
+ t.Fatal(err)
+ }
+ src := string(raw)
+
+ body := src[strings.Index(src, "func dispatchDaemonCommand("):]
+ body = body[:strings.Index(body, "\nfunc ")]
+
+ var verbs []string
+ for _, line := range strings.Split(body, "\n") {
+ line = strings.TrimSpace(line)
+ if !strings.HasPrefix(line, `case "`) {
+ continue
+ }
+ verbs = append(verbs, strings.TrimSuffix(strings.TrimPrefix(line, `case "`), `":`))
+ }
+ if len(verbs) == 0 {
+ t.Fatal("found no dispatched verbs; this census would pass vacuously")
+ }
+
+ for _, verb := range verbs {
+ if _, declared := daemonNotInstalledPolicies[verb]; !declared {
+ t.Errorf("dispatchDaemonCommand handles %q but it declares no not-installed policy, so it inherits no guard", verb)
+ }
+ }
+ for verb, policy := range daemonNotInstalledPolicies {
+ if policy == daemonNoOp && daemonNothingToDo[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] != "" {
+ t.Errorf("%q is not a no-op verb but carries no-op wording", verb)
+ }
+ }
+}