High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: the fallback notice calls a caller identified by --agent-id anonymous, so it names an adopted tab as newly opened (PIN-131)
Luigi Agosti2a50af51e3ed
diff --git a/docs/reference/navigate.md b/docs/reference/navigate.mdindex 2c89008b..090e3aa6 100644--- a/docs/reference/navigate.md+++ b/docs/reference/navigate.md@@ -62,7 +62,7 @@ pinchtab nav https://example.com --block-images # Skip images- `POST /tabs/{id}/navigate`, `POST /navigate` with `tabId`, and `pinchtab nav <url> --tab <id>` reuse the specified tab and make it the current tab for later unscoped operations.- `--new-tab` and `newTab:true` force a new tab even if another tab is current.- Commands that operate without `--tab` use the current tracked tab. Focusing or using a tab updates that current-tab pointer; if the pointer is stale, PinchTab falls back to the most recently used tracked tab.-- When the saved current-tab pointer names a tab the server no longer has, `pinchtab nav` retries the navigation without the tab id. For a session or agent-id caller that retry reuses that scope's current tab. For a session-less caller the server opens a **new** tab — the documented anonymous contract — so the CLI prints a `HINT` on stderr naming both the tab that was gone and the new one, rather than reporting plain success while leaving you with two tabs on one URL. Run with `PINCHTAB_SESSION` set to keep a single work surface. An explicit `--tab` never retries: it surfaces the 404.+- When the saved current-tab pointer names a tab the server no longer has, `pinchtab nav` retries the navigation without the tab id. For a session or agent-id caller — by `--agent-id` or `PINCHTAB_AGENT_ID`, either one — that retry reuses that scope's current tab, and the CLI stays silent because nothing was created. For a caller with neither, the server opens a **new** tab — the documented anonymous contract — so the CLI prints a `HINT` on stderr naming both the tab that was gone and the new one, rather than reporting plain success while leaving you with two tabs on one URL. Run with `PINCHTAB_SESSION` set to keep a single work surface. An explicit `--tab` never retries: it surfaces the 404.Rationale: the CLI keeps one obvious work surface by default. Use `--new-tab` when you intentionally want another tab, or `--tab`/`tabId` when you need a specific tab.diff --git a/internal/cli/actions/actions_navigate.go b/internal/cli/actions/actions_navigate.goindex d6748ed6..14899c21 100644--- a/internal/cli/actions/actions_navigate.go+++ b/internal/cli/actions/actions_navigate.go@@ -10,6 +10,7 @@ import ("github.com/pinchtab/pinchtab/internal/cli/apiclient""github.com/pinchtab/pinchtab/internal/cli/output""github.com/spf13/cobra"+ "github.com/spf13/pflag")// Back navigates the current (or specified) tab back in history.@@ -83,14 +84,14 @@ func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Coif jsonOutput {result, usedFallback := postNavigate(client, base, token, req, true)resultTabID := tabIDFromNavigateResult(result)- reportFallbackNewTab(usedFallback, req.tabID, resultTabID)+ reportFallbackNewTab(cmd, usedFallback, req.tabID, resultTabID)apiclient.SuggestNextAction("navigate", result)return resultTabID}result, usedFallback := postNavigate(client, base, token, req, false)resultTabID := tabIDFromNavigateResult(result)- reportFallbackNewTab(usedFallback, req.tabID, resultTabID)+ reportFallbackNewTab(cmd, usedFallback, req.tabID, resultTabID)if resultTabID != "" {fmt.Println(resultTabID)}@@ -105,7 +106,7 @@ func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Co}}- if !isIdentifiedCaller() {+ if !isIdentifiedCaller(cmd) {output.Hint(cli.NoSessionHint)}@@ -209,8 +210,8 @@ func postNavigate(client *http.Client, base, token string, req navigateRequest,// The remedy is deliberately absent: an anonymous navigate already prints// cli.NoSessionHint, which carries the run-with-a-session advice, and repeating it// here would be the same guidance twice on one command.-func reportFallbackNewTab(usedFallback bool, staleTabID, newTabID string) {- if !usedFallback || isIdentifiedCaller() {+func reportFallbackNewTab(cmd *cobra.Command, usedFallback bool, staleTabID, newTabID string) {+ if !usedFallback || isIdentifiedCaller(cmd) {return}if newTabID == "" {@@ -220,9 +221,40 @@ func reportFallbackNewTab(usedFallback bool, staleTabID, newTabID string) {output.Hint(fmt.Sprintf("tab %s no longer exists — opened a new tab %s for this navigation", staleTabID, newTabID))}-func isIdentifiedCaller() bool {+// isIdentifiedCaller reports whether the server will see this call as scoped to a session+// or an agent, which is what decides whether an unscoped retry adopts a tab or creates one.+//+// The agent id has TWO provenances and this must agree with the CLI's own resolution, which+// prefers the --agent-id persistent flag over the environment. Reading only the environment+// calls a flag-identified caller anonymous, and the retry then names an ADOPTED tab as newly+// opened — the inverse of the defect the notice exists to report.+func isIdentifiedCaller(cmd *cobra.Command) bool {return strings.TrimSpace(os.Getenv("PINCHTAB_SESSION")) != "" ||- strings.TrimSpace(os.Getenv("PINCHTAB_AGENT_ID")) != ""+ strings.TrimSpace(os.Getenv("PINCHTAB_AGENT_ID")) != "" ||+ agentIDFlag(cmd) != ""+}++// agentIDFlag reads the root's persistent --agent-id off whichever command is running.+//+// Both lookups are needed. Flags() carries the flag only once cobra has parsed and merged+// the persistent set, so a command tree built directly — every test here, and any caller+// that has not executed — sees nil there and finds it through InheritedFlags(). Checking+// only Flags() would leave the guard reading empty in exactly the tests meant to pin it.+//+// A command with no parent has no root flags to inherit, so an absent flag means anonymous+// rather than an error: newNavigateCmd() is built standalone.+func agentIDFlag(cmd *cobra.Command) string {+ if cmd == nil {+ return ""+ }+ for _, set := range []*pflag.FlagSet{cmd.Flags(), cmd.InheritedFlags()} {+ if flag := set.Lookup("agent-id"); flag != nil {+ if v := strings.TrimSpace(flag.Value.String()); v != "" {+ return v+ }+ }+ }+ return ""}func tabIDFromNavigateResult(result map[string]any) string {diff --git a/internal/cli/actions/actions_navigate_test.go b/internal/cli/actions/actions_navigate_test.goindex 3f434dfe..bcd0fa02 100644--- a/internal/cli/actions/actions_navigate_test.go+++ b/internal/cli/actions/actions_navigate_test.go@@ -573,6 +573,39 @@ func TestNavigateFallbackStaysSilentForAnIdentifiedCaller(t *testing.T) {}}+// The agent id reaches the server from EITHER the --agent-id flag or the environment, so a+// caller identified by the flag alone is identified to the server and its retry adopts that+// scope's current tab. Announcing a new tab there names an EXISTING tab as new — the inverse+// of the defect the notice exists to report, and just as misleading. The env is cleared, so+// only the flag can make this caller identified.+func TestNavigateFallbackStaysSilentForACallerIdentifiedByTheFlagAlone(t *testing.T) {+ t.Setenv("PINCHTAB_SESSION", "")+ t.Setenv("PINCHTAB_AGENT_ID", "")++ m := newMockServer()+ m.setResponse(http.MethodPost, "/tabs/STALE123/navigate", http.StatusNotFound, `{"error":"tab not found"}`)+ m.setResponse(http.MethodPost, "/navigate", http.StatusOK, `{"tabId":"ADOPTED9","status":"ok"}`)+ defer m.close()++ stderr := captureStderr(t, func() {+ captureStdout(t, func() {+ Navigate(m.server.Client(), m.base(), "", "https://pinchtab.com", agentIDFlagCmd(t, "STALE123", "bosch"))+ })+ })++ if strings.Contains(stderr, "new tab") {+ t.Fatalf("stderr = %q, want no new-tab notice: the retry carries X-Agent-Id, so the server adopted that scope's current tab rather than creating one", stderr)+ }+ // The no-session hint reads the same predicate, so it moves with it: a caller already+ // scoped by agent id is not unscoped, whichever provenance supplied the id.+ if strings.Contains(stderr, cli.SessionCreateCommand) {+ t.Errorf("stderr = %q, want no session advice for a caller the server already scopes by agent id", stderr)+ }+ if len(m.requests) != 2 || m.requests[1].Path != "/navigate" {+ t.Fatalf("requests = %+v, want the fallback to still fire", m.requests)+ }+}+// Absence assertion: an ordinary navigate that never hits the fallback gains no// extra output. Without this the notice could fire on every call and the tests// above would still pass.@@ -661,6 +694,22 @@ func staleTabCmd(t *testing.T, tabID string) *cobra.Command {return cmd}+// agentIDFlagCmd attaches the nav command to a root carrying --agent-id as a persistent+// flag, which is how the real tree registers it. The attachment is the point: the flag is+// never local to nav, so a standalone command cannot exercise the lookup at all.+func agentIDFlagCmd(t *testing.T, tabID, agentID string) *cobra.Command {+ t.Helper()++ cmd := staleTabCmd(t, tabID)+ root := &cobra.Command{Use: "pinchtab"}+ root.PersistentFlags().String("agent-id", "", "")+ root.AddCommand(cmd)+ if err := root.PersistentFlags().Set("agent-id", agentID); err != nil {+ t.Fatal(err)+ }+ return cmd+}+// fallbackNoticeLine returns the single HINT line about the vanished tab, failing// when there is none — so a test asserting on its content cannot pass against an// empty string.