Skip to content

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

fix: scheduler records one activity event per task so scheduled actions reach the dashboard feed (PIN-386)

Luigi Agostia2572643693d
diff --git a/docs/architecture/scheduler.md b/docs/architecture/scheduler.md
index c152519f..b5e1bb8a 100644
--- a/docs/architecture/scheduler.md
+++ b/docs/architecture/scheduler.md
@@ -80,6 +80,15 @@ The scheduler takes the orchestrator directly as its `InstanceResolver`: `Resolv
The orchestrator also implements `RequestAuthorizer`. Before dispatch the executor calls `AuthorizeTabRequest(tabId, req)`, which routes through the orchestrator's single hop-auth owner `applyInstanceAuth`: the request carries the instance's bearer token and, on a trusted child hop, the internal token. That is what lets the instance honor the `X-PinchTab-*` identity headers instead of stripping them at ingress, so the instance attributes the action to `scheduler` rather than `client`. Managed child instances run with activity recording off, so that attribution is persisted only by an instance that records activity. A resolver that does not implement `RequestAuthorizer` sends no credential, and an instance that requires one refuses the task with `401`.
+### Activity Recording
+
+The scheduler runs in the same process as the server's activity recorder, so it records each dispatched task there directly through an `ActivitySink` (`internal/server` passes the dashboard-feed recorder into `New`). One event per task carries source `scheduler`, the task's `agentId` and `tabId`, the action kind, and the outcome status and duration once the executor returns.
+
+This sink is the single source of a scheduled action in the dashboard feed, recorded once and independent of any instance-level recording:
+
+- Managed child instances run with activity recording off, so the child never records the forwarded action and there is no duplicate.
+- An attached external bridge records the forwarded action into its own recorder and feed, not the server's, so it does not double-count in the dashboard stream either.
+
## Dispatch Lifecycle
A task moves through these internal states:
diff --git a/internal/scheduler/activity_test.go b/internal/scheduler/activity_test.go
new file mode 100644
index 00000000..ef31b793
--- /dev/null
+++ b/internal/scheduler/activity_test.go
@@ -0,0 +1,98 @@
+package scheduler
+
+import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+)
+
+func TestSchedulerRecordsOneActivityEventPerTask(t *testing.T) {
+ instance := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ if err := json.NewEncoder(w).Encode(map[string]bool{"success": true}); err != nil {
+ t.Errorf("encode failed: %v", err)
+ }
+ }))
+ defer instance.Close()
+
+ parts := strings.Split(instance.URL, ":")
+ port := parts[len(parts)-1]
+
+ rec, err := activity.NewRecorder(activity.Config{
+ Enabled: true,
+ RetentionDays: 1,
+ Events: activity.EventSourceConfig{Scheduler: true},
+ }, t.TempDir())
+ if err != nil {
+ t.Fatalf("new recorder: %v", err)
+ }
+
+ cfg := DefaultConfig()
+ cfg.WorkerCount = 1
+ s := New(cfg, &mockResolver{port: port}, rec)
+ s.Start()
+ defer s.Stop()
+
+ task, err := s.Submit(SubmitRequest{
+ AgentID: "agent-1",
+ Action: "click",
+ TabID: "tab-1",
+ Ref: "e14",
+ })
+ if err != nil {
+ t.Fatalf("submit failed: %v", err)
+ }
+
+ deadline := time.After(5 * time.Second)
+ for {
+ if got := s.GetTask(task.ID); got != nil && got.GetState() == StateDone {
+ break
+ }
+ select {
+ case <-deadline:
+ t.Fatalf("task did not complete in time")
+ default:
+ time.Sleep(20 * time.Millisecond)
+ }
+ }
+
+ var events []activity.Event
+ poll := time.After(2 * time.Second)
+ for {
+ events, err = rec.Query(activity.Filter{Source: "scheduler"})
+ if err != nil {
+ t.Fatalf("query: %v", err)
+ }
+ if len(events) > 0 {
+ break
+ }
+ select {
+ case <-poll:
+ t.Fatalf("no scheduler activity event recorded")
+ default:
+ time.Sleep(20 * time.Millisecond)
+ }
+ }
+
+ if len(events) != 1 {
+ t.Fatalf("scheduler activity events = %d, want exactly 1", len(events))
+ }
+ evt := events[0]
+ if evt.Source != "scheduler" {
+ t.Errorf("source = %q, want scheduler", evt.Source)
+ }
+ if evt.AgentID != "agent-1" {
+ t.Errorf("agentId = %q, want agent-1", evt.AgentID)
+ }
+ if evt.TabID != "tab-1" {
+ t.Errorf("tabId = %q, want tab-1", evt.TabID)
+ }
+ if evt.Action != "click" {
+ t.Errorf("action = %q, want click", evt.Action)
+ }
+}
diff --git a/internal/scheduler/handlers_test.go b/internal/scheduler/handlers_test.go
index 5903efe9..dfb6bb75 100644
--- a/internal/scheduler/handlers_test.go
+++ b/internal/scheduler/handlers_test.go
@@ -26,7 +26,7 @@ func setupHandlerTest(t *testing.T) (*Scheduler, *http.ServeMux, *httptest.Serve
cfg := DefaultConfig()
cfg.WorkerCount = 1
- s := New(cfg, &mockResolver{port: port})
+ s := New(cfg, &mockResolver{port: port}, nil)
s.noAutoStart = true
mux := http.NewServeMux()
diff --git a/internal/scheduler/inflight_accounting_test.go b/internal/scheduler/inflight_accounting_test.go
index 0696f929..10f84063 100644
--- a/internal/scheduler/inflight_accounting_test.go
+++ b/internal/scheduler/inflight_accounting_test.go
@@ -43,7 +43,7 @@ func newBlockingScheduler(t *testing.T, perAgent, global, workers int) (*Schedul
cfg.WorkerCount = workers
cfg.MaxInflight = global
cfg.MaxPerAgentFlight = perAgent
- s := New(cfg, &mockResolver{port: "9999"})
+ s := New(cfg, &mockResolver{port: "9999"}, nil)
exec := newBlockingExecutor(workers + 2)
s.executor = exec
t.Cleanup(func() {
diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go
index b07e6d02..05a1b758 100644
--- a/internal/scheduler/scheduler.go
+++ b/internal/scheduler/scheduler.go
@@ -8,9 +8,17 @@ import (
"sync"
"time"
+ "github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/config"
)
+// ActivitySink records one activity event per dispatched task into the process
+// recorder that feeds the dashboard stream.
+type ActivitySink interface {
+ Enabled() bool
+ Record(activity.Event) error
+}
+
// InstanceResolver finds the localhost port for a given tab ID.
type InstanceResolver interface {
ResolveTabInstance(tabID string) (port string, err error)
@@ -109,6 +117,7 @@ type Scheduler struct {
queue *TaskQueue
results *ResultStore
executor TaskExecutor
+ activity ActivitySink
metrics *Metrics
// tracks all live tasks (queued + in-flight) for lookup by ID.
@@ -127,8 +136,9 @@ type Scheduler struct {
noAutoStart bool // testing only: suppress ensureRunning from Submit
}
-// New creates a scheduler with the given config and instance resolver.
-func New(cfg Config, resolver InstanceResolver) *Scheduler {
+// New creates a scheduler with the given config, instance resolver, and activity
+// sink. A nil sink disables per-task activity recording.
+func New(cfg Config, resolver InstanceResolver, sink ActivitySink) *Scheduler {
withDefaults(&cfg)
return &Scheduler{
@@ -136,6 +146,7 @@ func New(cfg Config, resolver InstanceResolver) *Scheduler {
queue: NewTaskQueue(cfg.MaxQueueSize, cfg.MaxPerAgent),
results: NewResultStore(cfg.ResultTTL),
executor: &actionEndpointExecutor{resolver: resolver, client: &http.Client{Timeout: 60 * time.Second}},
+ activity: sink,
metrics: newMetrics(),
live: make(map[string]*Task),
cancels: make(map[string]context.CancelFunc),
@@ -408,9 +419,37 @@ func (s *Scheduler) dispatch(t *Task) {
slog.Info("task completed", "task", t.ID, "agent", t.AgentID, "action", t.Action, "latencyMs", latency.Milliseconds())
}
+ s.recordActivity(t, execErr, latency)
s.finishTask(t)
}
+func (s *Scheduler) recordActivity(t *Task, execErr error, latency time.Duration) {
+ if s.activity == nil || !s.activity.Enabled() {
+ return
+ }
+ status := http.StatusOK
+ var errMsg string
+ if execErr != nil {
+ status = http.StatusBadGateway
+ errMsg = execErr.Error()
+ }
+ if err := s.activity.Record(activity.Event{
+ Timestamp: timeNow().UTC(),
+ Source: "scheduler",
+ AgentID: t.AgentID,
+ Method: http.MethodPost,
+ Path: fmt.Sprintf("/tabs/%s/action", t.TabID),
+ Status: status,
+ DurationMs: latency.Milliseconds(),
+ TabID: t.TabID,
+ Action: t.Action,
+ Ref: t.Ref,
+ Error: errMsg,
+ }); err != nil {
+ slog.Warn("scheduler activity recording failed", "task", t.ID, "err", err)
+ }
+}
+
// finishTask publishes a task's terminal state. It does NOT release an in-flight
// slot: it is reached from the deadline reaper and from Cancel for tasks that were
// still queued, and dispatch owns the slot for the ones that were not.
diff --git a/internal/scheduler/scheduler_defaults_test.go b/internal/scheduler/scheduler_defaults_test.go
index 4ba33ea8..d9c9e41a 100644
--- a/internal/scheduler/scheduler_defaults_test.go
+++ b/internal/scheduler/scheduler_defaults_test.go
@@ -14,7 +14,7 @@ import (
// second set of literals has appeared inside New, which is how the defaults drifted
// out of reach of `config get` in the first place.
func TestNewFillsEveryKnobFromDefaultConfig(t *testing.T) {
- got := New(Config{}, nil).cfg
+ got := New(Config{}, nil, nil).cfg
if want := DefaultConfig(); got != want {
t.Errorf("New(Config{}).cfg = %+v, want DefaultConfig() %+v", got, want)
}
@@ -128,7 +128,7 @@ func TestBatchSizeIsUnchangedAndComesThroughTheConversionSite(t *testing.T) {
if got := ConfigFromRuntime(runtime).MaxBatchSize; got != 5 {
t.Errorf("a configured maxBatchSize of 5 converted to %d; the conversion drops it, so config set writes a value nothing enforces", got)
}
- if got := New(ConfigFromRuntime(runtime), nil).cfg.MaxBatchSize; got != 5 {
+ if got := New(ConfigFromRuntime(runtime), nil, nil).cfg.MaxBatchSize; got != 5 {
t.Errorf("the running scheduler enforces %d, want the configured 5", got)
}
}
diff --git a/internal/scheduler/scheduler_test.go b/internal/scheduler/scheduler_test.go
index a8d0e06b..121ec15e 100644
--- a/internal/scheduler/scheduler_test.go
+++ b/internal/scheduler/scheduler_test.go
@@ -41,7 +41,7 @@ func newTestScheduler(t *testing.T) (*Scheduler, *httptest.Server) {
cfg.MaxPerAgentFlight = 3
resolver := &mockResolver{port: port}
- s := New(cfg, resolver)
+ s := New(cfg, resolver, nil)
s.noAutoStart = true
return s, executor
@@ -192,7 +192,7 @@ func TestSchedulerDispatchAndComplete(t *testing.T) {
cfg.MaxPerAgentFlight = 5
resolver := &mockResolver{port: port}
- s := New(cfg, resolver)
+ s := New(cfg, resolver, nil)
s.Start()
defer s.Stop()
@@ -240,7 +240,7 @@ func TestSchedulerDispatchFailure(t *testing.T) {
cfg := DefaultConfig()
cfg.WorkerCount = 1
- s := New(cfg, &mockResolver{port: port})
+ s := New(cfg, &mockResolver{port: port}, nil)
s.Start()
defer s.Stop()
@@ -276,7 +276,7 @@ func TestSchedulerResolverError(t *testing.T) {
cfg := DefaultConfig()
cfg.WorkerCount = 1
- s := New(cfg, &mockResolver{err: fmt.Errorf("no instance")})
+ s := New(cfg, &mockResolver{err: fmt.Errorf("no instance")}, nil)
s.Start()
defer s.Stop()
diff --git a/internal/server/activity_recorder.go b/internal/server/activity_recorder.go
index 15fb431e..06431ddd 100644
--- a/internal/server/activity_recorder.go
+++ b/internal/server/activity_recorder.go
@@ -45,5 +45,5 @@ func (r dashboardActivityRecorder) Query(filter activity.Filter) ([]activity.Eve
}
func shouldBroadcastDashboardActivity(evt activity.Event) bool {
- return evt.Source == "client"
+ return evt.Source == "client" || evt.Source == "scheduler"
}
diff --git a/internal/server/activity_recorder_test.go b/internal/server/activity_recorder_test.go
index 33072c84..d8c7e90b 100644
--- a/internal/server/activity_recorder_test.go
+++ b/internal/server/activity_recorder_test.go
@@ -78,6 +78,30 @@ func TestDashboardActivityRecorderBroadcastsClientEvents(t *testing.T) {
}
}
+func TestDashboardActivityRecorderBroadcastsSchedulerEvents(t *testing.T) {
+ base := &stubRecorder{}
+ dash := dashboard.NewDashboard(nil)
+ rec := newDashboardActivityRecorder(base, dash)
+
+ err := rec.Record(activity.Event{
+ Timestamp: time.Now().UTC(),
+ Source: "scheduler",
+ Method: http.MethodPost,
+ Path: "/tabs/tab_1/action",
+ AgentID: "agent-1",
+ TabID: "tab_1",
+ Action: "click",
+ Status: http.StatusOK,
+ DurationMs: 12,
+ })
+ if err != nil {
+ t.Fatalf("Record() error = %v", err)
+ }
+ if len(dash.RecentEvents()) != 1 {
+ t.Fatalf("dashboard recent events = %d, want 1", len(dash.RecentEvents()))
+ }
+}
+
// failingRecorder stands in for a store that cannot reach its disk.
type failingRecorder struct {
calls int
diff --git a/internal/server/server.go b/internal/server/server.go
index 9bbd457c..dcc3d918 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -275,7 +275,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
if cfg.Scheduler.Enabled {
schedCfg := scheduler.ConfigFromRuntime(cfg.Scheduler)
- sched = scheduler.New(schedCfg, orch)
+ sched = scheduler.New(schedCfg, orch, liveActivity)
sched.RegisterHandlers(mux)
slog.Info("scheduler enabled (on-demand)", "strategy", schedCfg.Strategy, "workers", schedCfg.WorkerCount)
}