Skip to content

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

fix: server.logLevel is written into every bridge child config and then ignored by the bridge (PIN-099)

Luigi Agosti4a6456d66126
diff --git a/cmd/pinchtab/cmd_bridge.go b/cmd/pinchtab/cmd_bridge.go
index c574c74f..0b44fc9f 100644
--- a/cmd/pinchtab/cmd_bridge.go
+++ b/cmd/pinchtab/cmd_bridge.go
@@ -120,7 +120,7 @@ func init() {
bridgeCmd.Flags().StringVar(&bridgeBind, "bind", "", "Bind address for the bridge HTTP server (overrides config server.bind)")
bridgeCmd.Flags().StringVar(&bridgePort, "port", "", "Port for the bridge HTTP server (overrides config server.port)")
bridgeCmd.Flags().StringVar(&bridgeBrowser, "browser", "", "Browser to use: chrome, cloak, or ghost-chrome (overrides config)")
- bridgeCmd.Flags().StringVar(&bridgeLogLevel, "log-level", "", "Minimum log level: debug, info (default), warn or error (overrides config server.logLevel)")
+ bridgeCmd.Flags().StringVar(&bridgeLogLevel, "log-level", "", "Minimum log level: debug, info (default), warn or error (overrides config server.logLevel; the bridge has no -v because it has no startup banner to show)")
bridgeCmd.Flags().StringVar(&bridgeRemoteBrowserName, "remote-browser-name", "", "Opaque label for the externally-managed browser; surfaces in /stealth/status")
rootCmd.AddCommand(bridgeCmd)
}
diff --git a/cmd/pinchtab/cmd_bridge_test.go b/cmd/pinchtab/cmd_bridge_test.go
index 107dc004..ee3cdc3e 100644
--- a/cmd/pinchtab/cmd_bridge_test.go
+++ b/cmd/pinchtab/cmd_bridge_test.go
@@ -1,11 +1,17 @@
package main
import (
+ "encoding/json"
+ "log/slog"
+ "os"
+ "path/filepath"
+ "strings"
"testing"
_ "github.com/pinchtab/pinchtab/internal/browsers/all"
"github.com/pinchtab/pinchtab/internal/browsers/runtimekit"
"github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/safelog"
)
func TestValidateBridgeCDPURL(t *testing.T) {
@@ -122,3 +128,117 @@ func TestApplyBridgeBrowserTargetClearsUnmatchedDefaultTarget(t *testing.T) {
t.Fatalf("resolved provider = %q, want ghost-chrome", got)
}
}
+
+// The bridge holds the CDP session, so it owns the logging a debug level is
+// actually wanted for. It reads the same server.logLevel the server does, and the
+// orchestrator writes that key into every child config — so before this was wired,
+// the setting looked plumbed end to end and was dropped at the last step.
+func TestBridgeResolvesTheConfiguredLogLevel(t *testing.T) {
+ t.Cleanup(func() { safelog.SetLevel(safelog.DefaultLevel) })
+
+ for _, tc := range []struct {
+ name string
+ configLevel string
+ flag string
+ want slog.Level
+ }{
+ {name: "flagless bridge takes the configured level", configLevel: "warn", want: slog.LevelWarn},
+ {name: "flag overrides the configured level", configLevel: "warn", flag: "debug", want: slog.LevelDebug},
+ {name: "blank flag leaves the configured level", configLevel: "warn", flag: " ", want: slog.LevelWarn},
+ {name: "neither is the default", want: safelog.DefaultLevel},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ safelog.SetLevel(slog.LevelInfo)
+ cfg := &config.RuntimeConfig{LogLevel: tc.configLevel}
+ resolveLogLevel(cfg, tc.flag, false)
+ if got := safelog.CurrentLevel(); got != tc.want {
+ t.Errorf("level = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}
+
+// Parity with the server is a structural property, not a coincidence of two
+// tables: both commands hand their (flag, config) pair to the same function, and
+// neither decides anything itself. Asserting the resolved levels match would pass
+// even if the bridge grew its own copy, so the source is where this is pinned —
+// exactly one precedence function in the package, no second copy in the bridge.
+func TestBridgeAndServerResolveTheLogLevelThroughOneFunction(t *testing.T) {
+ bridgeSrc, err := os.ReadFile("cmd_bridge.go")
+ if err != nil {
+ t.Fatal(err)
+ }
+ serverSrc, err := os.ReadFile("cmd_server.go")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !strings.Contains(string(bridgeSrc), "resolveLogLevel(cfg, bridgeLogLevel, false)") {
+ t.Error("cmd_bridge.go no longer resolves the log level, so server.logLevel is silently ignored in the bridge")
+ }
+ if !strings.Contains(string(serverSrc), "resolveLogLevel(cfg, logLevel, verbose)") {
+ t.Error("cmd_server.go no longer routes through resolveLogLevel")
+ }
+ for _, own := range []string{"cfg.LogLevel = ", "safelog.SetLevel("} {
+ if strings.Contains(string(bridgeSrc), own) {
+ t.Errorf("cmd_bridge.go contains %q — the bridge decides the level itself instead of sharing the server's precedence", own)
+ }
+ }
+
+ declarations := 0
+ for _, name := range []string{"cmd_bridge.go", "cmd_server.go", "cmd_server_ensure.go", "cmd_server_background.go", "root.go"} {
+ raw, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ declarations += strings.Count(string(raw), "func resolveLogLevel(")
+ }
+ if declarations != 1 {
+ t.Errorf("found %d resolveLogLevel declarations across the command files, want exactly 1", declarations)
+ }
+
+ if bridgeCmd.Flags().Lookup("log-level") == nil {
+ t.Error("bridge has no --log-level flag, so it cannot override the configured level the way --bind and --port do")
+ }
+ if bridgeCmd.Flags().Lookup("verbose") != nil {
+ t.Error("bridge grew a -v; if that is wanted it must mean what it means on the server, and the flag help and docs must stop saying the bridge has none")
+ }
+ if help := bridgeCmd.Flags().Lookup("log-level").Usage; !strings.Contains(help, "server.logLevel") || !strings.Contains(help, "-v") {
+ t.Errorf("--log-level help %q must name the config key it overrides and explain the missing -v", help)
+ }
+}
+
+// The path that made the setting look plumbed: the orchestrator builds each child
+// config through config.FileConfigFromRuntime and writes it under the instance
+// state dir, and the child bridge then loads that file. This drives both halves —
+// the written file and the load — so "carried but ignored" cannot come back.
+func TestBridgeResolvesTheLevelFromAWrittenChildConfig(t *testing.T) {
+ t.Cleanup(func() { safelog.SetLevel(safelog.DefaultLevel) })
+ safelog.SetLevel(slog.LevelInfo)
+
+ childConfig := config.FileConfigFromRuntime(&config.RuntimeConfig{
+ Token: "child-token",
+ LogLevel: "warn",
+ })
+ encoded, err := json.MarshalIndent(childConfig, "", " ")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(encoded), `"logLevel": "warn"`) {
+ t.Fatalf("child config does not carry the level, so no bridge could read it: %s", encoded)
+ }
+
+ path := filepath.Join(t.TempDir(), "config.json")
+ if err := os.WriteFile(path, encoded, 0600); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("PINCHTAB_CONFIG", path)
+ t.Setenv("PINCHTAB_TOKEN", "")
+
+ cfg := loadConfig()
+ resolveLogLevel(cfg, "", false)
+
+ if got := safelog.CurrentLevel(); got != slog.LevelWarn {
+ t.Fatalf("bridge started from a written child config resolved %v, want warn", got)
+ }
+}
diff --git a/cmd/pinchtab/cmd_server_test.go b/cmd/pinchtab/cmd_server_test.go
index 9bfda27e..a3a66108 100644
--- a/cmd/pinchtab/cmd_server_test.go
+++ b/cmd/pinchtab/cmd_server_test.go
@@ -217,41 +217,3 @@ func TestServerCommandAssignsTheLogLevelOnlyInsideResolveLogLevel(t *testing.T)
t.Error("the server command no longer calls resolveLogLevel with the flag and the verbose flag")
}
}
-
-// The bridge is where a browser-level fault is diagnosed, and server.logLevel now
-// travels into every child bridge config, so a bridge that ignored the key would
-// drop the operator's debug request exactly where they asked for it.
-func TestBridgeResolvesTheConfiguredLogLevel(t *testing.T) {
- t.Cleanup(func() { safelog.SetLevel(safelog.DefaultLevel) })
-
- for _, tc := range []struct {
- name string
- configLevel string
- flag string
- want slog.Level
- }{
- {name: "config alone is honoured", configLevel: "warn", want: slog.LevelWarn},
- {name: "flag beats config", configLevel: "warn", flag: "debug", want: slog.LevelDebug},
- {name: "neither is the default", want: safelog.DefaultLevel},
- } {
- t.Run(tc.name, func(t *testing.T) {
- safelog.SetLevel(slog.LevelInfo)
- cfg := &config.RuntimeConfig{LogLevel: tc.configLevel}
- resolveLogLevel(cfg, tc.flag, false)
- if got := safelog.CurrentLevel(); got != tc.want {
- t.Errorf("level = %v, want %v", got, tc.want)
- }
- })
- }
-
- if bridgeCmd.Flags().Lookup("log-level") == nil {
- t.Error("bridge has no --log-level flag, so it cannot override the configured level the way --bind and --port do")
- }
- raw, err := os.ReadFile("cmd_bridge.go")
- if err != nil {
- t.Fatal(err)
- }
- if !strings.Contains(string(raw), "resolveLogLevel(cfg, bridgeLogLevel, false)") {
- t.Error("cmd_bridge.go no longer resolves the log level, so server.logLevel is silently ignored in the bridge")
- }
-}
diff --git a/docs/commands.md b/docs/commands.md
index 6c437c92..b114513f 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -10,6 +10,7 @@ pinchtab server --log-level warn # Record warnings and errors only
pinchtab server stop # Stop the running server (foreground or background)
pinchtab server restart # Stop + restart in background (applies config changes)
pinchtab bridge # Start the bridge-only runtime
+pinchtab bridge --log-level debug # Bridge threshold (same precedence as server)
pinchtab mcp # Start the MCP stdio server
pinchtab daemon # Show daemon status
pinchtab daemon install # Install as a background service
@@ -29,8 +30,15 @@ and it raises the level to debug only when neither of the other two is set.
A daemon-installed server and the server a bare `pinchtab nav` auto-starts both run
`pinchtab server` with no flags, so `server.logLevel` is the only way to set their
-threshold (`pinchtab config set server.logLevel warn`). `pinchtab bridge` takes the
-same key and the same `--log-level` flag.
+threshold (`pinchtab config set server.logLevel warn`).
+
+Everything in this paragraph applies to `pinchtab bridge` as well: it reads the same
+`server.logLevel`, accepts the same `--log-level`, and resolves them with the same
+precedence — which matters because the bridge holds the CDP session, so it owns the
+target-crash, instance-lifecycle and selector-resolution logging. The one difference
+is that `bridge` has no `-v`: `-v` also switches on the server's startup banner, and
+the bridge has no banner to switch on, so `--log-level debug` is how you raise a
+bridge. Orchestrator-spawned bridges inherit the level through their child config.
## Navigation
diff --git a/internal/orchestrator/child_config_test.go b/internal/orchestrator/child_config_test.go
new file mode 100644
index 00000000..c4ffb8b0
--- /dev/null
+++ b/internal/orchestrator/child_config_test.go
@@ -0,0 +1,55 @@
+package orchestrator
+
+import (
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+// Every spawned bridge reads the file this writes, so the operator's log level has
+// to survive the write. This is the orchestrator half of the chain — the child
+// bridge's half, that it resolves the level it reads, lives in cmd/pinchtab.
+func TestWriteChildConfigCarriesTheLogLevel(t *testing.T) {
+ stateDir := t.TempDir()
+ o := fakeOrch(&config.RuntimeConfig{
+ Port: "9867",
+ DefaultBrowser: config.BrowserChrome,
+ LogLevel: "warn",
+ })
+
+ path, err := o.writeChildConfig(nil, "9999", 12345, "/tmp/profile", stateDir, true, nil, nil)
+ if err != nil {
+ t.Fatalf("write child config: %v", err)
+ }
+ raw, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.Contains(string(raw), `"logLevel": "warn"`) {
+ t.Fatalf("child config on disk does not carry the level, so the spawned bridge cannot read it:\n%s", raw)
+ }
+}
+
+// An unset level must not write a value, or a child inherits a level the operator
+// never chose and the bridge can no longer tell "unset" from "configured info".
+func TestWriteChildConfigOmitsAnUnsetLogLevel(t *testing.T) {
+ stateDir := t.TempDir()
+ o := fakeOrch(&config.RuntimeConfig{
+ Port: "9867",
+ DefaultBrowser: config.BrowserChrome,
+ })
+
+ path, err := o.writeChildConfig(nil, "9999", 12345, "/tmp/profile", stateDir, true, nil, nil)
+ if err != nil {
+ t.Fatalf("write child config: %v", err)
+ }
+ raw, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if strings.Contains(string(raw), "logLevel") {
+ t.Fatalf("child config carries a logLevel key with none configured:\n%s", raw)
+ }
+}