High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor: give the X-PinchTab-* wire header names one home in internal/api/types with a single-home census (PIN-383)
Luigi Agosti7d3f436a664d
diff --git a/cmd/pinchtab/tab_state.go b/cmd/pinchtab/tab_state.goindex 1d6ddb68..0e44b4f0 100644--- a/cmd/pinchtab/tab_state.go+++ b/cmd/pinchtab/tab_state.go@@ -8,6 +8,7 @@ import ("strings""time"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/cli/apiclient""github.com/pinchtab/pinchtab/internal/cli/clistate""github.com/spf13/cobra"@@ -166,7 +167,7 @@ func (tabStateStore) probe(tabID string) tabProbeResult {if err != nil {return tabProbeInconclusive}- req.Header.Set("X-PinchTab-Source", "client")+ req.Header.Set(types.HeaderSource, "client")if token != "" {if strings.HasPrefix(token, "ses_") {req.Header.Set("Authorization", "Session "+token)diff --git a/dashboard/src/generated/types.ts b/dashboard/src/generated/types.tsindex 59fe1b56..4c32c090 100644--- a/dashboard/src/generated/types.ts+++ b/dashboard/src/generated/types.ts@@ -1,5 +1,36 @@// Code generated by tygo. DO NOT EDIT.+//////////+// source: headers.go++/**+ * The X-PinchTab-* wire headers that form the CLI/MCP-to-server contract. They+ * live here, the leaf both clients and the server import, so a one-character+ * drift on one side is a compile error rather than a silent no-op. Request-side+ * tags travel in the body instead, because the strip middleware drops inbound+ * X-PinchTab-* headers from public clients; these are the response headers+ * (Vocab, Tab-Id) and the source tag the trusted hops set.+ */+export const HeaderVocab = "X-PinchTab-Vocab";+/**+ * The X-PinchTab-* wire headers that form the CLI/MCP-to-server contract. They+ * live here, the leaf both clients and the server import, so a one-character+ * drift on one side is a compile error rather than a silent no-op. Request-side+ * tags travel in the body instead, because the strip middleware drops inbound+ * X-PinchTab-* headers from public clients; these are the response headers+ * (Vocab, Tab-Id) and the source tag the trusted hops set.+ */+export const HeaderTabID = "X-PinchTab-Tab-Id";+/**+ * The X-PinchTab-* wire headers that form the CLI/MCP-to-server contract. They+ * live here, the leaf both clients and the server import, so a one-character+ * drift on one side is a compile error rather than a silent no-op. Request-side+ * tags travel in the body instead, because the strip middleware drops inbound+ * X-PinchTab-* headers from public clients; these are the response headers+ * (Vocab, Tab-Id) and the source tag the trusted hops set.+ */+export const HeaderSource = "X-PinchTab-Source";+//////////// source: health.godiff --git a/internal/activity/context.go b/internal/activity/context.goindex 82e05156..d87d032b 100644--- a/internal/activity/context.go+++ b/internal/activity/context.go@@ -11,19 +11,23 @@ import ("sync/atomic""time"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/authn""github.com/pinchtab/pinchtab/internal/browserops""github.com/pinchtab/pinchtab/internal/httpx")const (- HeaderAgentID = "X-Agent-Id"- HeaderPTSessionID = "X-PinchTab-Session-Id"- HeaderPTSource = "X-PinchTab-Source"+ HeaderAgentID = "X-Agent-Id"+ HeaderPTSessionID = "X-PinchTab-Session-Id"+ // HeaderPTSource and HeaderPTTabID are the two identity headers that also form+ // part of the CLI/MCP wire contract, so their spelling lives once in api/types+ // and these alias it. The rest are activity-only and single-homed here.+ HeaderPTSource = types.HeaderSourceHeaderPTInstance = "X-PinchTab-Instance-Id"HeaderPTProfileID = "X-PinchTab-Profile-Id"HeaderPTProfile = "X-PinchTab-Profile-Name"- HeaderPTTabID = "X-PinchTab-Tab-Id"+ HeaderPTTabID = types.HeaderTabIDHeaderPTTabCreated = "X-PinchTab-Tab-Created")diff --git a/internal/api/types/headers.go b/internal/api/types/headers.gonew file mode 100644index 00000000..72386fad--- /dev/null+++ b/internal/api/types/headers.go@@ -0,0 +1,13 @@+package types++// The X-PinchTab-* wire headers that form the CLI/MCP-to-server contract. They+// live here, the leaf both clients and the server import, so a one-character+// drift on one side is a compile error rather than a silent no-op. Request-side+// tags travel in the body instead, because the strip middleware drops inbound+// X-PinchTab-* headers from public clients; these are the response headers+// (Vocab, Tab-Id) and the source tag the trusted hops set.+const (+ HeaderVocab = "X-PinchTab-Vocab"+ HeaderTabID = "X-PinchTab-Tab-Id"+ HeaderSource = "X-PinchTab-Source"+)diff --git a/internal/api/types/headers_census_test.go b/internal/api/types/headers_census_test.gonew file mode 100644index 00000000..18470d63--- /dev/null+++ b/internal/api/types/headers_census_test.go@@ -0,0 +1,123 @@+package types++import (+ "fmt"+ "go/ast"+ "go/parser"+ "go/token"+ "sort"+ "strconv"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/srccensus"+)++const (+ moduleRoot = "../../.."+ minModuleFiles = 300+ headerHomeFile = "internal/api/types/headers.go"+)++// exemptHeaders are the X-PinchTab-* names that legitimately live outside the wire-header+// home. Each is already single-homed and belongs to a different contract, so the census+// allows its one spelling to sit in the named file rather than in api/types. A new wire+// header must go in headers.go instead of being added here.+var exemptHeaders = map[string]string{+ "X-PinchTab-Internal-Token": "internal/handlers/trust.go: server-only trusted-hop token, never part of the CLI/MCP wire contract",+ "X-PinchTab-Event": "internal/scheduler/webhook.go: outbound event-webhook contract, separate from the wire headers",+ "X-PinchTab-Task-ID": "internal/scheduler/webhook.go: outbound event-webhook contract, separate from the wire headers",+ "X-PinchTab-Session-Id": "internal/activity/context.go: activity-only identity header, already single-homed",+ "X-PinchTab-Instance-Id": "internal/activity/context.go: activity-only identity header, already single-homed",+ "X-PinchTab-Profile-Id": "internal/activity/context.go: activity-only identity header, already single-homed",+ "X-PinchTab-Profile-Name": "internal/activity/context.go: activity-only identity header, already single-homed",+ "X-PinchTab-Tab-Created": "internal/activity/context.go: activity-only identity header, already single-homed",+}++// pinchtabHeaderLiterals maps each X-PinchTab-* string literal to the module-relative files+// it is spelled in. It reads AST string literals only, so a comment naming a header (this+// package's own doc comment does) is not counted.+func pinchtabHeaderLiterals(t *testing.T, files []srccensus.SourceFile) map[string][]string {+ t.Helper()+ out := map[string][]string{}+ for _, f := range files {+ parsed, err := parser.ParseFile(token.NewFileSet(), f.Name, f.Text, 0)+ if err != nil {+ t.Fatalf("parse %s: %v", f.Name, err)+ }+ ast.Inspect(parsed, func(n ast.Node) bool {+ lit, ok := n.(*ast.BasicLit)+ if !ok || lit.Kind != token.STRING {+ return true+ }+ val, err := strconv.Unquote(lit.Value)+ if err == nil && strings.HasPrefix(val, "X-PinchTab-") {+ out[val] = append(out[val], f.Name)+ }+ return true+ })+ }+ return out+}++func singleHomeViolations(headers map[string][]string) []string {+ var violations []string+ for header, locs := range headers {+ if len(locs) != 1 {+ sort.Strings(locs)+ violations = append(violations, fmt.Sprintf("%s is spelled in %d places, want exactly one so a one-character drift is a compile error, not a silent no-op: %v", header, len(locs), locs))+ continue+ }+ if _, exempt := exemptHeaders[header]; exempt {+ if locs[0] == headerHomeFile {+ violations = append(violations, fmt.Sprintf("%s is listed as exempt but sits in the wire-header home %s", header, headerHomeFile))+ }+ continue+ }+ if locs[0] != headerHomeFile {+ violations = append(violations, fmt.Sprintf("%s is a wire header spelled in %s; move it to %s, or if it is not a wire header add it to exemptHeaders with a reason", header, locs[0], headerHomeFile))+ }+ }+ sort.Strings(violations)+ return violations+}++func TestEachWireHeaderLiteralIsSingleHomed(t *testing.T) {+ headers := pinchtabHeaderLiterals(t, srccensus.Tree(t, moduleRoot, minModuleFiles))++ for _, wire := range []string{HeaderVocab, HeaderTabID, HeaderSource} {+ locs := headers[wire]+ if len(locs) != 1 || locs[0] != headerHomeFile {+ t.Errorf("wire header %s must be spelled exactly once, in %s; found %v", wire, headerHomeFile, locs)+ }+ }++ if v := singleHomeViolations(headers); len(v) > 0 {+ t.Errorf("X-PinchTab-* header names are not single-homed:\n%s", strings.Join(v, "\n"))+ }+}++// The guard bites: a second spelling of a wire header is reported as a violation.+func TestCensusFlagsADuplicateSpelling(t *testing.T) {+ planted := []srccensus.SourceFile{+ {Name: headerHomeFile, Text: "package types\nconst A = \"X-PinchTab-Vocab\"\n"},+ {Name: "internal/somewhere/dup.go", Text: "package somewhere\nconst B = \"X-PinchTab-Vocab\"\n"},+ }+ headers := pinchtabHeaderLiterals(t, planted)+ if len(headers["X-PinchTab-Vocab"]) != 2 {+ t.Fatalf("the AST census did not see the planted duplicate: %v", headers["X-PinchTab-Vocab"])+ }+ if len(singleHomeViolations(headers)) == 0 {+ t.Fatal("a duplicated wire-header spelling passed the census; a typo on one side would silently disable the feature")+ }+}++// A comment that names a header is not a spelling: the AST walk sees literals only.+func TestCensusIgnoresHeadersNamedInComments(t *testing.T) {+ commentOnly := []srccensus.SourceFile{+ {Name: "internal/foo/foo.go", Text: "package foo\n// mentions X-PinchTab-Vocab in prose only\nconst X = 1\n"},+ }+ if got := pinchtabHeaderLiterals(t, commentOnly)["X-PinchTab-Vocab"]; len(got) != 0 {+ t.Fatalf("a header named only in a comment was counted as a spelling: %v", got)+ }+}diff --git a/internal/cli/apiclient/client.go b/internal/cli/apiclient/client.goindex 7a64d1db..4dc7288f 100644--- a/internal/cli/apiclient/client.go+++ b/internal/cli/apiclient/client.go@@ -9,11 +9,13 @@ import ("path/filepath""strings""time"++ "github.com/pinchtab/pinchtab/internal/api/types")const (- vocabHeader = "X-PinchTab-Vocab"- vocabTabIDHeader = "X-PinchTab-Tab-Id"+ vocabHeader = types.HeaderVocab+ vocabTabIDHeader = types.HeaderTabIDvocabStoreLimit = 16)diff --git a/internal/cli/apiclient/transport.go b/internal/cli/apiclient/transport.goindex a6ae8f03..eb2e12e4 100644--- a/internal/cli/apiclient/transport.go+++ b/internal/cli/apiclient/transport.go@@ -8,6 +8,8 @@ import ("net/http""net/url""strings"++ "github.com/pinchtab/pinchtab/internal/api/types")// request describes a single API call. body is a JSON payload (nil = no body;@@ -74,7 +76,7 @@ func doRequest(client *http.Client, token string, r request) (int, []byte, error}func setClientHeaders(req *http.Request, token string) {- req.Header.Set("X-PinchTab-Source", "client")+ req.Header.Set(types.HeaderSource, "client")if token == "" {return}diff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex cf4bc28b..a8e81c0a 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -14,6 +14,7 @@ import ("time""github.com/pinchtab/pinchtab/internal/activity"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/bridge""github.com/pinchtab/pinchtab/internal/browserops""github.com/pinchtab/pinchtab/internal/browsers"@@ -416,7 +417,7 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionRreturn req, true}-const vocabHeader = "X-PinchTab-Vocab"+const vocabHeader = types.HeaderVocabconst vocabSupersededCode = "vocab_superseded"diff --git a/internal/mcp/client.go b/internal/mcp/client.goindex 011c8115..a6b8fc2e 100644--- a/internal/mcp/client.go+++ b/internal/mcp/client.go@@ -14,9 +14,10 @@ import ("time""github.com/pinchtab/pinchtab/internal/activity"+ "github.com/pinchtab/pinchtab/internal/api/types")-const vocabHeader = "X-PinchTab-Vocab"+const vocabHeader = types.HeaderVocab// vocabStore holds the last snapshot vocabulary token per tab so an interaction// echoes it back and a ref minted under a superseded snapshot is refused rather