Skip to content

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

fix(scheduler): a scheduled action records the instance's own failure status instead of a flat 502

Luigi Agosti7191317bc73c
diff --git a/docs/architecture/scheduler.md b/docs/architecture/scheduler.md
index ed42af55..aabea42b 100644
--- a/docs/architecture/scheduler.md
+++ b/docs/architecture/scheduler.md
@@ -82,7 +82,7 @@ The orchestrator also implements `RequestAuthorizer`. Before dispatch the execut
### 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. The event always reaches the live dashboard stream (`/api/events`); it is written to the activity log, and so returned by `/api/activity` after a reload, only when `observability.activity.events.scheduler` is enabled, which is off by default like the other non-client sources. Any executor failure, including an instance's own 4xx, is recorded as status `502` with the executor's error text.
+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. The event always reaches the live dashboard stream (`/api/events`); it is written to the activity log, and so returned by `/api/activity` after a reload, only when `observability.activity.events.scheduler` is enabled, which is off by default like the other non-client sources. A failure response from the instance is recorded with the instance's own status (a `404` for a selector that matched nothing) and the executor's error text; a transport failure that never produced a response records as `502`.
This sink is the single source of a scheduled action in the dashboard feed, recorded once and independent of any instance-level recording:
diff --git a/internal/scheduler/activity_test.go b/internal/scheduler/activity_test.go
index ef31b793..adeec017 100644
--- a/internal/scheduler/activity_test.go
+++ b/internal/scheduler/activity_test.go
@@ -12,12 +12,44 @@ import (
)
func TestSchedulerRecordsOneActivityEventPerTask(t *testing.T) {
- instance := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ evt := recordScheduledTask(t, StateDone, 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)
}
- }))
+ })
+ 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)
+ }
+ if evt.Status != http.StatusOK {
+ t.Errorf("status = %d, want 200", evt.Status)
+ }
+}
+
+func TestSchedulerRecordsTheInstanceStatusOnFailure(t *testing.T) {
+ evt := recordScheduledTask(t, StateFailed, func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "selector matched no element", http.StatusNotFound)
+ })
+ if evt.Status != http.StatusNotFound {
+ t.Errorf("status = %d, want the instance's 404, not a flat 502", evt.Status)
+ }
+ if !strings.Contains(evt.Error, "selector matched no element") {
+ t.Errorf("error = %q, want the instance's body", evt.Error)
+ }
+}
+
+func recordScheduledTask(t *testing.T, want TaskState, instanceHandler http.HandlerFunc) activity.Event {
+ t.Helper()
+ instance := httptest.NewServer(instanceHandler)
defer instance.Close()
parts := strings.Split(instance.URL, ":")
@@ -50,7 +82,7 @@ func TestSchedulerRecordsOneActivityEventPerTask(t *testing.T) {
deadline := time.After(5 * time.Second)
for {
- if got := s.GetTask(task.ID); got != nil && got.GetState() == StateDone {
+ if got := s.GetTask(task.ID); got != nil && got.GetState() == want {
break
}
select {
@@ -82,17 +114,5 @@ func TestSchedulerRecordsOneActivityEventPerTask(t *testing.T) {
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)
- }
+ return events[0]
}
diff --git a/internal/scheduler/executor.go b/internal/scheduler/executor.go
index 94e72b24..2ac4b35e 100644
--- a/internal/scheduler/executor.go
+++ b/internal/scheduler/executor.go
@@ -58,6 +58,17 @@ func buildActionBody(t *Task) map[string]any {
return body
}
+// InstanceError is the action endpoint's own failure response, kept typed so the
+// recorded activity event carries the instance's status rather than a flat 502.
+type InstanceError struct {
+ Status int
+ Body string
+}
+
+func (e *InstanceError) Error() string {
+ return fmt.Sprintf("executor returned %d: %s", e.Status, e.Body)
+}
+
func (e *actionEndpointExecutor) Execute(ctx context.Context, t *Task) (any, error) {
if t.TabID == "" {
return nil, fmt.Errorf("tabId is required for task execution")
@@ -110,7 +121,7 @@ func (e *actionEndpointExecutor) Execute(ctx context.Context, t *Task) (any, err
}
if resp.StatusCode >= 400 {
- return nil, fmt.Errorf("executor returned %d: %s", resp.StatusCode, string(respBody))
+ return nil, &InstanceError{Status: resp.StatusCode, Body: string(respBody)}
}
var result any
diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go
index e97dfede..63ee3343 100644
--- a/internal/scheduler/scheduler.go
+++ b/internal/scheduler/scheduler.go
@@ -2,6 +2,7 @@ package scheduler
import (
"context"
+ "errors"
"fmt"
"log/slog"
"net/http"
@@ -145,7 +146,7 @@ func New(cfg Config, resolver InstanceResolver, sink ActivitySink) *Scheduler {
cfg: cfg,
queue: NewTaskQueue(cfg.MaxQueueSize, cfg.MaxPerAgent),
results: NewResultStore(cfg.ResultTTL),
- executor: &actionEndpointExecutor{resolver: resolver, client: &http.Client{Timeout: 60 * time.Second}},
+ executor: NewActionExecutor(resolver),
activity: sink,
metrics: newMetrics(),
live: make(map[string]*Task),
@@ -432,6 +433,10 @@ func (s *Scheduler) recordActivity(t *Task, execErr error, latency time.Duration
if execErr != nil {
status = http.StatusBadGateway
errMsg = execErr.Error()
+ var upstream *InstanceError
+ if errors.As(execErr, &upstream) {
+ status = upstream.Status
+ }
}
if err := s.activity.Record(activity.Event{
Timestamp: timeNow().UTC(),