Skip to content

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

fix(traceability): route the auto-started server's output to server.log instead of /dev/null

Luigi Agosti48d43e18862b
diff --git a/cmd/pinchtab/cmd_server_background.go b/cmd/pinchtab/cmd_server_background.go
index c37c9997..265f5474 100644
--- a/cmd/pinchtab/cmd_server_background.go
+++ b/cmd/pinchtab/cmd_server_background.go
@@ -236,6 +236,23 @@ func spawnDetachedChild(binary string, args []string, out *os.File) (int, error)
return pid, nil
}
+// spawnDetachedServer starts a detached server whose stdout/stderr land in the state
+// dir's server.log. Every detached spawn path goes through here: the banner names that
+// file as the live sink from the --background-child flag in the recorded argv alone, so
+// a second spawn site that passed its own writer (or none) would make the banner vouch
+// for a file nothing writes — which is the dead end this card exists to remove.
+func spawnDetachedServer(stateDir, binary string, args []string) (int, error) {
+ if err := os.MkdirAll(stateDir, 0o755); err != nil {
+ return 0, fmt.Errorf("create state dir: %w", err)
+ }
+ logF, err := os.OpenFile(serverLogFilePath(stateDir), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
+ if err != nil {
+ return 0, fmt.Errorf("open log file: %w", err)
+ }
+ defer func() { _ = logF.Close() }()
+ return spawnDetachedChild(binary, args, logF)
+}
+
func runServerBackground(cfg *config.RuntimeConfig, opts serverBackgroundOptions, addressChanged bool) error {
if err := requireDetachedServerOwnership("background start", addressChanged); err != nil {
return err
@@ -263,21 +280,11 @@ func runServerBackground(cfg *config.RuntimeConfig, opts serverBackgroundOptions
return err
}
- if err := os.MkdirAll(stateDir, 0o755); err != nil {
- return fmt.Errorf("create state dir: %w", err)
- }
- logF, err := os.OpenFile(serverLogFilePath(stateDir), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
- if err != nil {
- return fmt.Errorf("open log file: %w", err)
- }
-
args := backgroundServerArgs(marker, opts)
- pid, err := spawnDetachedChild(binary, args, logF)
+ pid, err := spawnDetachedServer(stateDir, binary, args)
if err != nil {
- _ = logF.Close()
return err
}
- _ = logF.Close()
if err := recordServerPID(stateDir, pid, binary, args, baseURL, marker); err != nil {
return fmt.Errorf("write pid file: %w", err)
diff --git a/cmd/pinchtab/cmd_server_ensure.go b/cmd/pinchtab/cmd_server_ensure.go
index ccacc3e5..5e276251 100644
--- a/cmd/pinchtab/cmd_server_ensure.go
+++ b/cmd/pinchtab/cmd_server_ensure.go
@@ -151,7 +151,7 @@ func autoStartServer() error {
}
args := autoStartServerArgs(marker)
- pid, err := spawnDetachedChild(binary, args, nil)
+ pid, err := spawnDetachedServer(stateDir, binary, args)
if err != nil {
return err
}
diff --git a/cmd/pinchtab/server_log_location_test.go b/cmd/pinchtab/server_log_location_test.go
index 6eacd047..e2f8c8c4 100644
--- a/cmd/pinchtab/server_log_location_test.go
+++ b/cmd/pinchtab/server_log_location_test.go
@@ -2,9 +2,15 @@ package main
import (
"bytes"
+ "os"
"path/filepath"
+ "runtime"
"strings"
"testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/srccensus"
)
func renderAgentHintsString(t *testing.T, st agentStatus) string {
@@ -120,3 +126,89 @@ func TestBannerPrintsTheLogDestinationAndFlagsAStaleServerLog(t *testing.T) {
t.Errorf("the banner does not disown the stale server.log:\n%s", out)
}
}
+
+const minCmdPinchtabSourceFiles = 40
+
+// The banner decides "this server writes server.log" from the --background-child flag in
+// the recorded argv alone. Both detached spawn paths set that flag, so both have to
+// actually write the file. The auto-start path — the one every browser CLI command and
+// `pinchtab mcp` take — passed no writer, Go connected the child to /dev/null, and the
+// banner vouched for a file frozen at whatever a long-dead server left behind.
+func TestDetachedServerSpawnWritesTheLogFileTheBannerNames(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("the probe spawns /bin/sh")
+ }
+ stateDir := filepath.Join(t.TempDir(), "state")
+ const marker = "detached-child-wrote-here"
+
+ if _, err := spawnDetachedServer(stateDir, "/bin/sh", []string{"-c", "echo " + marker}); err != nil {
+ t.Fatalf("spawnDetachedServer: %v", err)
+ }
+
+ logPath := serverLogFilePath(stateDir)
+ deadline := time.Now().Add(10 * time.Second)
+ for {
+ data, err := os.ReadFile(logPath)
+ if err == nil && strings.Contains(string(data), marker) {
+ return
+ }
+ if time.Now().After(deadline) {
+ t.Fatalf("%s never received the child's output (read %q, err %v); the spawn discarded it, so the log the banner names holds nothing this server wrote", logPath, data, err)
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+}
+
+// A unit test on the logging spawn helper stays green while a second spawn site bypasses
+// it — which is exactly how the auto-start path came to write nowhere. Only the census
+// sees a new caller.
+func TestEveryDetachedSpawnGoesThroughTheLoggingHelper(t *testing.T) {
+ pkg := srccensus.Load(t, ".", minCmdPinchtabSourceFiles)
+
+ owner, ok := pkg.Func("spawnDetachedServer")
+ if !ok {
+ t.Fatal("spawnDetachedServer is gone; do not delete this guard — re-point it at whatever now owns wiring a detached server to its log file")
+ }
+
+ var offenders []string
+ for _, site := range pkg.Calls(t, "spawnDetachedChild") {
+ if !pkg.Contains(owner, site) {
+ offenders = append(offenders, site.String())
+ }
+ }
+ if len(offenders) > 0 {
+ t.Errorf("these sites spawn a detached server without routing its output to server.log, so the banner names a log this server never writes and the unredacted failure cause is recoverable from nowhere; call spawnDetachedServer instead:\n%s",
+ strings.Join(offenders, "\n"))
+ }
+}
+
+// serverLogWhereForConfig is where the resolver's premises are gathered, and it had no
+// test: every case above hand-feeds backgroundChild. Drive it from the argv the auto-start
+// path really produces, so a change to autoStartServerArgs that drops the flag is caught
+// where the booleans are derived rather than where they are assumed.
+func TestAutoStartedServerIsRecognisedAsWritingServerLog(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ stateDir := filepath.Join(home, "state")
+
+ args := autoStartServerArgs("marker-abc")
+ if err := writeServerPID(stateDir, serverPIDInfo{
+ PID: os.Getpid(),
+ Executable: "/usr/local/bin/pinchtab",
+ Args: args,
+ Marker: "marker-abc",
+ }); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(serverLogFilePath(stateDir), []byte("old\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+
+ where := serverLogWhereForConfig(&config.RuntimeConfig{StateDir: stateDir}, true)
+ if where.Destination != serverLogFilePath(stateDir) {
+ t.Errorf("destination = %q, want the server.log the auto-started child writes", where.Destination)
+ }
+ if where.StalePath != "" {
+ t.Errorf("the live log was disowned as stale: %q", where.StalePath)
+ }
+}
diff --git a/docs/commands.md b/docs/commands.md
index 763d5222..095f1863 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -29,11 +29,13 @@ file, then `-v`, then the default `info`. `-v` always adds the full startup bann
and it raises the level to debug only when neither of the other two is set.
Where those lines land depends on how the server was started, which is why `pinchtab`
-with no arguments prints a `logs` row naming the live destination: `<stateDir>/server.log`
-for `pinchtab server -b`, `~/.pinchtab/logs/daemon.err.log` for a daemon-installed
-service, and the terminal for a foreground run. A `server.log` left behind by an earlier
-detached run is called out as not being written by the current server, so it cannot be
-mistaken for a live one.
+with no arguments prints a `logs` row naming the live destination. There are four ways a
+server starts, and three destinations: `<stateDir>/server.log` for `pinchtab server -b`
+**and for the server a bare `pinchtab nav` or `pinchtab mcp` auto-starts** — both spawn
+detached and both append to that same file — `~/.pinchtab/logs/daemon.err.log` for a
+daemon-installed service, and the terminal for a foreground run. A `server.log` left
+behind by an earlier detached run is called out as not being written by the current
+server, so it cannot be mistaken for a live one.
A request that fails logs its cause there UNREDACTED — absolute paths intact — under the
same `requestId` the access log records, so a 5xx can be joined to the reason it happened:
@@ -54,7 +56,8 @@ escape hatch when you want the record without the polling.
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`).
+threshold (`pinchtab config set server.logLevel warn`). That also means a 4xx cause is
+not written at the default `info` level; raise the level when you need one.
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