Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: the stale-tab remedy tells the user to retry, but on 'tab handoff/resume/handoff-status' the retry sends an empty tab id and the CLI misdiagnoses it as an outdated server (PIN-220)

Luigi Agosti8c51a343331f
diff --git a/cmd/pinchtab/cmd_cli_browser_nav.go b/cmd/pinchtab/cmd_cli_browser_nav.go
index 71c3f6a4..7248a1bd 100644
--- a/cmd/pinchtab/cmd_cli_browser_nav.go
+++ b/cmd/pinchtab/cmd_cli_browser_nav.go
@@ -1,6 +1,9 @@
package main
import (
+ "errors"
+ "strings"
+
browseractions "github.com/pinchtab/pinchtab/internal/cli/actions"
"github.com/pinchtab/pinchtab/internal/urls"
"github.com/spf13/cobra"
@@ -103,50 +106,80 @@ func newTabCloseCmd() *cobra.Command {
}
}
+// errNoCurrentTab refuses locally instead of issuing /tabs//<verb>: an empty path
+// segment 404s at the mux, which the CLI then misdiagnoses as an outdated server.
+var errNoCurrentTab = errors.New("no current tab: pass a tab id, or run `pinchtab nav <url>` to open one")
+
+func mustResolveTabArg(args []string) (string, error) {
+ tabID := resolveTabArg(args)
+ if strings.TrimSpace(tabID) == "" {
+ return "", errNoCurrentTab
+ }
+ return tabID, nil
+}
+
func newTabHandoffCmd() *cobra.Command {
return &cobra.Command{
- Use: "handoff [id]",
- Short: "Pause tab automation for human handoff",
- Long: "Mark a tab as paused_handoff so action routes block until resumed or timeout expires. Defaults to the current tab from the state file.",
- Args: cobra.MaximumNArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "handoff [id]",
+ Short: "Pause tab automation for human handoff",
+ Long: "Mark a tab as paused_handoff so action routes block until resumed or timeout expires. Defaults to the current tab from the state file.",
+ Args: cobra.MaximumNArgs(1),
+ SilenceUsage: true,
+ SilenceErrors: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
reason, _ := cmd.Flags().GetString("reason")
timeoutMS, _ := cmd.Flags().GetInt("timeout-ms")
- tabID := resolveTabArg(args)
+ tabID, err := mustResolveTabArg(args)
+ if err != nil {
+ return err
+ }
runCLI(func(rt cliRuntime) {
browseractions.TabHandoff(rt.client, rt.base, rt.token, tabID, reason, timeoutMS, cmd)
})
+ return nil
},
}
}
func newTabResumeCmd() *cobra.Command {
return &cobra.Command{
- Use: "resume [id]",
- Short: "Resume a paused_handoff tab",
- Long: "Resume automation on a paused tab. Defaults to the current tab from the state file.",
- Args: cobra.MaximumNArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
+ Use: "resume [id]",
+ Short: "Resume a paused_handoff tab",
+ Long: "Resume automation on a paused tab. Defaults to the current tab from the state file.",
+ Args: cobra.MaximumNArgs(1),
+ SilenceUsage: true,
+ SilenceErrors: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
status, _ := cmd.Flags().GetString("status")
- tabID := resolveTabArg(args)
+ tabID, err := mustResolveTabArg(args)
+ if err != nil {
+ return err
+ }
runCLI(func(rt cliRuntime) {
browseractions.TabResume(rt.client, rt.base, rt.token, tabID, status, cmd)
})
+ return nil
},
}
}
func newTabHandoffStatusCmd() *cobra.Command {
return &cobra.Command{
- Use: "handoff-status [id]",
- Short: "Show handoff status for a tab",
- Long: "Show handoff status. Defaults to the current tab from the state file.",
- Args: cobra.MaximumNArgs(1),
- Run: func(cmd *cobra.Command, args []string) {
- tabID := resolveTabArg(args)
+ Use: "handoff-status [id]",
+ Short: "Show handoff status for a tab",
+ Long: "Show handoff status. Defaults to the current tab from the state file.",
+ Args: cobra.MaximumNArgs(1),
+ SilenceUsage: true,
+ SilenceErrors: true,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ tabID, err := mustResolveTabArg(args)
+ if err != nil {
+ return err
+ }
runCLI(func(rt cliRuntime) {
browseractions.TabHandoffStatus(rt.client, rt.base, rt.token, tabID, cmd)
})
+ return nil
},
}
}
diff --git a/cmd/pinchtab/cmd_cli_browser_nav_test.go b/cmd/pinchtab/cmd_cli_browser_nav_test.go
new file mode 100644
index 00000000..bf9ce1a4
--- /dev/null
+++ b/cmd/pinchtab/cmd_cli_browser_nav_test.go
@@ -0,0 +1,63 @@
+package main
+
+import (
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/spf13/cobra"
+)
+
+func TestTabHandoffFamilyRefusesLocallyOnAnEmptyTabID(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Errorf("a request was issued for an empty tab id: %s %s — the refusal must happen before the wire, or the mux 404s /tabs//<verb> and the CLI misdiagnoses the server as outdated", r.Method, r.URL.Path)
+ }))
+ t.Cleanup(srv.Close)
+ t.Setenv("PINCHTAB_SERVER", srv.URL)
+ t.Setenv("PINCHTAB_SESSION", "")
+ t.Setenv("PINCHTAB_AGENT_ID", "")
+ t.Setenv("XDG_STATE_HOME", t.TempDir())
+ orig := resolveTabStateEndpoint
+ resolveTabStateEndpoint = func() (string, string) { return srv.URL, "" }
+ t.Cleanup(func() { resolveTabStateEndpoint = orig })
+
+ for name, build := range map[string]func() *cobra.Command{
+ "handoff": newTabHandoffCmd,
+ "resume": newTabResumeCmd,
+ "handoff-status": newTabHandoffStatusCmd,
+ } {
+ t.Run(name, func(t *testing.T) {
+ cmd := build()
+ cmd.SetArgs([]string{})
+ err := cmd.Execute()
+ if !errors.Is(err, errNoCurrentTab) {
+ t.Fatalf("err = %v, want the local no-current-tab refusal", err)
+ }
+ for _, lever := range []string{"tab id", "pinchtab nav"} {
+ if !strings.Contains(err.Error(), lever) {
+ t.Errorf("refusal %q does not name the %q lever", err, lever)
+ }
+ }
+ })
+ }
+}
+
+func TestMustResolveTabArgPassesAnExplicitOrCachedID(t *testing.T) {
+ t.Setenv("PINCHTAB_SESSION", "")
+ t.Setenv("PINCHTAB_AGENT_ID", "")
+ t.Setenv("XDG_STATE_HOME", t.TempDir())
+ orig := resolveTabStateEndpoint
+ resolveTabStateEndpoint = func() (string, string) { return "http://127.0.0.1:19999", "" }
+ t.Cleanup(func() { resolveTabStateEndpoint = orig })
+
+ if id, err := mustResolveTabArg([]string{"TAB123"}); err != nil || id != "TAB123" {
+ t.Fatalf("explicit id: got %q, %v", id, err)
+ }
+
+ defaultTabState.write("CACHED42")
+ if id, err := mustResolveTabArg(nil); err != nil || id != "CACHED42" {
+ t.Fatalf("cached id: got %q, %v — the refusal must not swallow the cache fallback", id, err)
+ }
+}