Skip to content

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

fix: one predicate decides dashboard agent activity for live and restart so scheduled actions survive a restart (PIN-387)

Luigi Agosti82ee3413aa8e
diff --git a/cmd/pinchtab/cmd_cli_runtime.go b/cmd/pinchtab/cmd_cli_runtime.go
index 3518daa1..3c539fe7 100644
--- a/cmd/pinchtab/cmd_cli_runtime.go
+++ b/cmd/pinchtab/cmd_cli_runtime.go
@@ -283,7 +283,7 @@ func (t agentHeaderTransport) RoundTrip(req *http.Request) (*http.Response, erro
cloned := req.Clone(req.Context())
cloned.Header = req.Header.Clone()
- cloned.Header.Set(activity.HeaderPTSource, "client")
+ cloned.Header.Set(activity.HeaderPTSource, activity.SourceClient)
if id := normalizeCLIAgentID(t.agentID); id != "" {
cloned.Header.Set(activity.HeaderAgentID, id)
}
diff --git a/internal/activity/activity.go b/internal/activity/activity.go
index 3035c293..ce11bf53 100644
--- a/internal/activity/activity.go
+++ b/internal/activity/activity.go
@@ -452,19 +452,19 @@ func clampQueryLimit(limit int) int {
func (s *Store) shouldRecordSource(source string) bool {
switch normalizeSourceName(source) {
- case "client":
+ case SourceClient:
return true
- case "dashboard":
+ case SourceDashboard:
return s.events.Dashboard
- case "server":
+ case SourceServer:
return s.events.Server
- case "bridge":
+ case SourceBridge:
return s.events.Bridge
- case "orchestrator":
+ case SourceOrchestrator:
return s.events.Orchestrator
- case "scheduler":
+ case SourceScheduler:
return s.events.Scheduler
- case "mcp":
+ case SourceMCP:
return s.events.MCP
default:
return s.events.Other
@@ -669,7 +669,7 @@ func appendJSONL(path string, line []byte) error {
func shouldWritePrimaryLog(source string) bool {
switch normalizeSourceName(source) {
- case "", "server", "bridge":
+ case "", SourceServer, SourceBridge:
return true
default:
return false
diff --git a/internal/activity/context.go b/internal/activity/context.go
index d87d032b..46dc2137 100644
--- a/internal/activity/context.go
+++ b/internal/activity/context.go
@@ -123,10 +123,10 @@ func sourceFor(r *http.Request, fallback string) string {
}
creds := authn.CredentialsFromRequest(r)
if creds.Method == authn.MethodCookie {
- return "dashboard"
+ return SourceDashboard
}
if creds.Method == authn.MethodHeader || creds.Method == authn.MethodSession {
- return "client"
+ return SourceClient
}
return fallback
}
diff --git a/internal/activity/sources.go b/internal/activity/sources.go
new file mode 100644
index 00000000..c5b34fe5
--- /dev/null
+++ b/internal/activity/sources.go
@@ -0,0 +1,20 @@
+package activity
+
+const (
+ SourceClient = "client"
+ SourceDashboard = "dashboard"
+ SourceServer = "server"
+ SourceBridge = "bridge"
+ SourceOrchestrator = "orchestrator"
+ SourceScheduler = "scheduler"
+ SourceMCP = "mcp"
+)
+
+// IsDashboardAgentActivity is the one predicate for "does this event belong in the
+// dashboard's per-agent views". The live broadcast and the persisted-log rebuild both
+// call it, so a source counted while the server runs is the same source restored after
+// a restart; two separately spelled predicates drifted once and dropped scheduled
+// actions from the per-agent summaries after any restart.
+func IsDashboardAgentActivity(evt Event) bool {
+ return evt.Source == SourceClient || evt.Source == SourceScheduler
+}
diff --git a/internal/activity/sources_census_test.go b/internal/activity/sources_census_test.go
new file mode 100644
index 00000000..5976beb6
--- /dev/null
+++ b/internal/activity/sources_census_test.go
@@ -0,0 +1,172 @@
+package activity
+
+import (
+ "fmt"
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "sort"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/srccensus"
+)
+
+const (
+ moduleRoot = "../.."
+ minModuleFiles = 300
+ sourceHomeFile = "internal/activity/sources.go"
+)
+
+// sourceVocab is derived from the source consts, not hand-listed, so a new SourceX
+// const joins the census automatically.
+var sourceVocab = map[string]bool{
+ SourceClient: true,
+ SourceDashboard: true,
+ SourceServer: true,
+ SourceBridge: true,
+ SourceOrchestrator: true,
+ SourceScheduler: true,
+ SourceMCP: true,
+}
+
+// configExemptPrefix is excluded because the config editor and JSON carry a section
+// vocabulary ("scheduler", "dashboard", …) that happens to share these spellings but
+// names config sections, not activity event sources; it must not be forced onto the
+// activity source consts.
+const configExemptPrefix = "internal/config/"
+
+// bareSourceLiteralSites returns each place a source-vocabulary string is spelled as a
+// bare literal in a source context: as a `Source:` composite-literal value, either side
+// of a `.Source ==` / `!=` comparison, or anywhere inside package activity (whose home is
+// the source vocabulary) other than the const block itself. A comment naming a source is
+// not a literal, so the AST walk does not count it.
+func bareSourceLiteralSites(t *testing.T, files []srccensus.SourceFile) []string {
+ t.Helper()
+ var sites []string
+ for _, f := range files {
+ if f.Name == sourceHomeFile || strings.HasPrefix(f.Name, configExemptPrefix) {
+ continue
+ }
+ inActivityPkg := strings.HasPrefix(f.Name, "internal/activity/")
+ parsed, err := parser.ParseFile(token.NewFileSet(), f.Name, f.Text, 0)
+ if err != nil {
+ t.Fatalf("parse %s: %v", f.Name, err)
+ }
+ flag := func(node ast.Node, why string) {
+ lit, ok := node.(*ast.BasicLit)
+ if !ok || lit.Kind != token.STRING {
+ return
+ }
+ val, err := strconv.Unquote(lit.Value)
+ if err != nil || !sourceVocab[val] {
+ return
+ }
+ sites = append(sites, fmt.Sprintf("%s: %q (%s)", f.Name, val, why))
+ }
+ ast.Inspect(parsed, func(n ast.Node) bool {
+ switch node := n.(type) {
+ case *ast.KeyValueExpr:
+ if key, ok := node.Key.(*ast.Ident); ok && key.Name == "Source" {
+ flag(node.Value, "Source: composite-literal value")
+ }
+ case *ast.BinaryExpr:
+ if node.Op != token.EQL && node.Op != token.NEQ {
+ return true
+ }
+ if isSourceSelector(node.X) {
+ flag(node.Y, ".Source comparison operand")
+ }
+ if isSourceSelector(node.Y) {
+ flag(node.X, ".Source comparison operand")
+ }
+ case *ast.BasicLit:
+ if inActivityPkg {
+ flag(node, "bare literal in package activity")
+ }
+ }
+ return true
+ })
+ }
+ sort.Strings(sites)
+ return sites
+}
+
+func isSourceSelector(e ast.Expr) bool {
+ sel, ok := e.(*ast.SelectorExpr)
+ return ok && sel.Sel.Name == "Source"
+}
+
+func TestNoBareSourceVocabularyLiteralOutsideConstBlock(t *testing.T) {
+ if sites := bareSourceLiteralSites(t, srccensus.Tree(t, moduleRoot, minModuleFiles)); len(sites) > 0 {
+ t.Errorf("source-vocabulary strings are spelled as bare literals; reference the SourceX consts in %s so a one-character drift is a compile error, not a silent divergence:\n%s", sourceHomeFile, strings.Join(sites, "\n"))
+ }
+}
+
+// The census bites: the exact drift PIN-386 left — one predicate comparing evt.Source
+// against a bare "scheduler" while the other used the const — is reported.
+func TestSourceCensusFlagsAPlantedComparison(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/server/evil.go", Text: "package server\nfunc f(evt struct{ Source string }) bool { return evt.Source == \"scheduler\" }\n"},
+ }
+ if len(bareSourceLiteralSites(t, planted)) == 0 {
+ t.Fatal("a bare evt.Source == \"scheduler\" comparison passed the census; the two predicates could silently drift again")
+ }
+}
+
+// The exemption bites in both directions: a config-section literal is not forced onto
+// the activity source consts, even in a Source-shaped composite literal.
+func TestSourceCensusExcludesConfigByPath(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/config/editor_set.go", Text: "package config\ntype e struct{ Source string }\nvar _ = e{Source: \"scheduler\"}\n"},
+ }
+ if len(bareSourceLiteralSites(t, planted)) != 0 {
+ t.Fatal("a config-section string was flagged; the config vocabulary shares the spelling but is a different vocabulary")
+ }
+}
+
+// A source named only in a comment is not a spelling: the AST walk sees literals only.
+func TestSourceCensusIgnoresComments(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/foo/foo.go", Text: "package foo\n// mentions scheduler and client in prose only\nconst X = 1\n"},
+ }
+ if got := bareSourceLiteralSites(t, planted); len(got) != 0 {
+ t.Fatalf("a source named only in a comment was counted: %v", got)
+ }
+}
+
+// TestDashboardAgentActivityVerdictPerSource pins which sources are agent activity. The
+// live broadcast and the persisted-log rebuild both call IsDashboardAgentActivity, so
+// this table is the one place the two paths' agreement is decided.
+func TestDashboardAgentActivityVerdictPerSource(t *testing.T) {
+ want := map[string]bool{
+ SourceClient: true,
+ SourceScheduler: true,
+ SourceDashboard: false,
+ SourceServer: false,
+ SourceBridge: false,
+ SourceOrchestrator: false,
+ SourceMCP: false,
+ }
+ for source := range sourceVocab {
+ if _, ok := want[source]; !ok {
+ t.Fatalf("source const %q has no pinned agent-activity verdict; add it to the table", source)
+ }
+ }
+ for source, expected := range want {
+ if got := IsDashboardAgentActivity(Event{Source: source}); got != expected {
+ t.Errorf("IsDashboardAgentActivity(source=%q) = %v, want %v", source, got, expected)
+ }
+ }
+}
+
+// TestLiveAndRestartPathsCallTheSamePredicate is why the single table above suffices:
+// both the live broadcast and the persisted-log rebuild delegate to the one predicate,
+// so neither can drift from the table.
+func TestLiveAndRestartPathsCallTheSamePredicate(t *testing.T) {
+ for _, dir := range []string{"../server", "../dashboard"} {
+ pkg := srccensus.Load(t, dir, 2)
+ pkg.Calls(t, "activity.IsDashboardAgentActivity")
+ }
+}
diff --git a/internal/dashboard/activity_ingest.go b/internal/dashboard/activity_ingest.go
index 2f2b5afd..d4ecf07d 100644
--- a/internal/dashboard/activity_ingest.go
+++ b/internal/dashboard/activity_ingest.go
@@ -96,9 +96,8 @@ func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since ti
}
events, err := rec.Query(activity.Filter{
- Source: "client",
- Since: since,
- Limit: persistedAgentBootstrapLimit,
+ Since: since,
+ Limit: persistedAgentBootstrapLimit,
})
if err != nil {
return since, err
@@ -106,7 +105,7 @@ func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since ti
latest := since
for _, evt := range events {
- if evt.Timestamp.After(latest) {
+ if activity.IsDashboardAgentActivity(evt) && evt.Timestamp.After(latest) {
latest = evt.Timestamp
}
}
@@ -115,12 +114,12 @@ func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since ti
return latest, nil
}
-// ingestActivityBatch filters persisted activity to the trackable client subset,
-// converts to live events, and records them.
+// ingestActivityBatch filters persisted activity to the dashboard agent-activity
+// subset, converts to live events, and records them.
func (d *Dashboard) ingestActivityBatch(events []activity.Event) {
batch := make([]apiTypes.ActivityEvent, 0, len(events))
for _, evt := range events {
- if !shouldTrackPersistedAgentActivity(evt) {
+ if !activity.IsDashboardAgentActivity(evt) {
continue
}
batch = append(batch, activityEventToLiveEvent(evt))
@@ -152,10 +151,6 @@ func (d *Dashboard) LoadPersistedAgentActivity(rec activity.Recorder) error {
return err
}
-func shouldTrackPersistedAgentActivity(evt activity.Event) bool {
- return evt.Source == "client"
-}
-
func (d *Dashboard) rememberEventIDLocked(id string) {
if id == "" {
return
diff --git a/internal/dashboard/activity_ingest_test.go b/internal/dashboard/activity_ingest_test.go
new file mode 100644
index 00000000..41a71e46
--- /dev/null
+++ b/internal/dashboard/activity_ingest_test.go
@@ -0,0 +1,83 @@
+package dashboard
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+)
+
+// TestLoadPersistedAgentActivityRestoresScheduledActions drives the restart path
+// through a real on-disk store so the Query filter is exercised, not stubbed: a
+// scheduled action must survive a restart in the per-agent summary and the recent
+// history exactly as a client action does. It fails on HEAD, where the rebuild both
+// queried Source:"client" only and tracked Source=="client" only.
+func TestLoadPersistedAgentActivityRestoresScheduledActions(t *testing.T) {
+ store, err := activity.NewStore(t.TempDir(), 30)
+ if err != nil {
+ t.Fatalf("NewStore() error = %v", err)
+ }
+ now := time.Now().UTC()
+
+ events := []activity.Event{
+ {
+ Timestamp: now.Add(-2 * time.Minute),
+ Source: activity.SourceClient,
+ RequestID: "req-client",
+ AgentID: "agent-client",
+ Method: http.MethodPost,
+ Path: "/tabs/tab_1/action",
+ Status: http.StatusOK,
+ TabID: "tab_1",
+ Action: "click",
+ },
+ {
+ Timestamp: now.Add(-1 * time.Minute),
+ Source: activity.SourceScheduler,
+ RequestID: "req-sched",
+ AgentID: "agent-sched",
+ Method: http.MethodPost,
+ Path: "/tabs/tab_2/action",
+ Status: http.StatusOK,
+ TabID: "tab_2",
+ Action: "reload",
+ },
+ {
+ Timestamp: now,
+ Source: activity.SourceDashboard,
+ RequestID: "req-dash",
+ Method: http.MethodGet,
+ Path: "/agents",
+ Status: http.StatusOK,
+ },
+ }
+ for _, evt := range events {
+ if err := store.Record(evt); err != nil {
+ t.Fatalf("Record(%s) error = %v", evt.Source, err)
+ }
+ }
+
+ d := NewDashboard(nil)
+ if err := d.LoadPersistedAgentActivity(store); err != nil {
+ t.Fatalf("LoadPersistedAgentActivity() error = %v", err)
+ }
+
+ agentIDs := map[string]bool{}
+ for _, a := range d.Agents() {
+ agentIDs[a.ID] = true
+ }
+ if !agentIDs["agent-sched"] {
+ t.Errorf("scheduled action dropped out of the per-agent summary after restart; agents = %v", agentIDs)
+ }
+ if !agentIDs["agent-client"] {
+ t.Errorf("client action missing from the per-agent summary; agents = %v", agentIDs)
+ }
+ if agentIDs["anonymous"] {
+ t.Errorf("dashboard's own request was tracked as agent activity; agents = %v", agentIDs)
+ }
+
+ if got := eventRequestIDs(d.RecentEvents()); got != "req-client,req-sched" {
+ t.Errorf("RecentEvents() request ids = %q, want %q (scheduled action must be in recent history, dashboard request must not)", got, "req-client,req-sched")
+ }
+}
diff --git a/internal/orchestrator/health_transport.go b/internal/orchestrator/health_transport.go
index 7ed84ce9..eaae3c7b 100644
--- a/internal/orchestrator/health_transport.go
+++ b/internal/orchestrator/health_transport.go
@@ -14,7 +14,7 @@ import (
"github.com/pinchtab/pinchtab/internal/sanitize"
)
-const orchestratorActivitySource = "orchestrator"
+const orchestratorActivitySource = activity.SourceOrchestrator
type remoteTab struct {
ID string `json:"id"`
diff --git a/internal/scheduler/executor.go b/internal/scheduler/executor.go
index c89003d5..94e72b24 100644
--- a/internal/scheduler/executor.go
+++ b/internal/scheduler/executor.go
@@ -84,7 +84,7 @@ func (e *actionEndpointExecutor) Execute(ctx context.Context, t *Task) (any, err
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
- req.Header.Set(activity.HeaderPTSource, "scheduler")
+ req.Header.Set(activity.HeaderPTSource, activity.SourceScheduler)
req.Header.Set(activity.HeaderPTTabID, t.TabID)
if t.AgentID != "" {
req.Header.Set(activity.HeaderAgentID, t.AgentID)
diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go
index 05a1b758..e97dfede 100644
--- a/internal/scheduler/scheduler.go
+++ b/internal/scheduler/scheduler.go
@@ -435,7 +435,7 @@ func (s *Scheduler) recordActivity(t *Task, execErr error, latency time.Duration
}
if err := s.activity.Record(activity.Event{
Timestamp: timeNow().UTC(),
- Source: "scheduler",
+ Source: activity.SourceScheduler,
AgentID: t.AgentID,
Method: http.MethodPost,
Path: fmt.Sprintf("/tabs/%s/action", t.TabID),
diff --git a/internal/server/activity_recorder.go b/internal/server/activity_recorder.go
index 06431ddd..9194d5f6 100644
--- a/internal/server/activity_recorder.go
+++ b/internal/server/activity_recorder.go
@@ -31,7 +31,7 @@ func (r dashboardActivityRecorder) Record(evt activity.Event) error {
if r.base != nil && r.base.Enabled() {
err = r.base.Record(evt)
}
- if r.dash != nil && shouldBroadcastDashboardActivity(evt) {
+ if r.dash != nil && activity.IsDashboardAgentActivity(evt) {
r.dash.RecordActivityEvent(evt)
}
return err
@@ -43,7 +43,3 @@ func (r dashboardActivityRecorder) Query(filter activity.Filter) ([]activity.Eve
}
return r.base.Query(filter)
}
-
-func shouldBroadcastDashboardActivity(evt activity.Event) bool {
- return evt.Source == "client" || evt.Source == "scheduler"
-}