Skip to content

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

fix: rebuild query filters to the agent sources so a busy non-agent source cannot crowd agent events out of the budget (PIN-387)

Luigi Agostia62747af3f0e
diff --git a/internal/activity/activity.go b/internal/activity/activity.go
index ce11bf53..1a0595ef 100644
--- a/internal/activity/activity.go
+++ b/internal/activity/activity.go
@@ -77,6 +77,7 @@ type StepCounts struct {
type Filter struct {
Source string
+ Sources []string
RequestID string
SessionID string
AgentID string
@@ -491,6 +492,9 @@ func (f Filter) matches(evt Event) bool {
if f.Source != "" && normalizeSourceName(evt.Source) != normalizeSourceName(f.Source) {
return false
}
+ if len(f.Sources) > 0 && !matchesAnySource(evt.Source, f.Sources) {
+ return false
+ }
if f.RequestID != "" && evt.RequestID != f.RequestID {
return false
}
@@ -527,6 +531,16 @@ func (f Filter) matches(evt Event) bool {
return true
}
+func matchesAnySource(source string, want []string) bool {
+ normalized := normalizeSourceName(source)
+ for _, w := range want {
+ if normalizeSourceName(w) == normalized {
+ return true
+ }
+ }
+ return false
+}
+
func (s *Store) filePathFor(ts time.Time) string {
return filepath.Join(s.dir, fmt.Sprintf("events-%s.jsonl", ts.UTC().Format(time.DateOnly)))
}
diff --git a/internal/activity/sources.go b/internal/activity/sources.go
index c5b34fe5..a3bbdc7f 100644
--- a/internal/activity/sources.go
+++ b/internal/activity/sources.go
@@ -10,11 +10,24 @@ const (
SourceMCP = "mcp"
)
+// DashboardAgentSources is the one list of sources that count as dashboard agent
+// activity. Both the predicate below and the persisted-log query take their set from
+// here, so the live broadcast, the in-memory filter and the store query that feeds the
+// restart rebuild cannot select different sets.
+func DashboardAgentSources() []string {
+ return []string{SourceClient, SourceScheduler}
+}
+
// 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
+ for _, source := range DashboardAgentSources() {
+ if evt.Source == source {
+ return true
+ }
+ }
+ return false
}
diff --git a/internal/dashboard/activity_ingest.go b/internal/dashboard/activity_ingest.go
index d4ecf07d..262131f2 100644
--- a/internal/dashboard/activity_ingest.go
+++ b/internal/dashboard/activity_ingest.go
@@ -96,8 +96,9 @@ func (d *Dashboard) IngestPersistedAgentActivity(rec activity.Recorder, since ti
}
events, err := rec.Query(activity.Filter{
- Since: since,
- Limit: persistedAgentBootstrapLimit,
+ Sources: activity.DashboardAgentSources(),
+ Since: since,
+ Limit: persistedAgentBootstrapLimit,
})
if err != nil {
return since, err
diff --git a/internal/dashboard/activity_ingest_test.go b/internal/dashboard/activity_ingest_test.go
index 41a71e46..345b6c44 100644
--- a/internal/dashboard/activity_ingest_test.go
+++ b/internal/dashboard/activity_ingest_test.go
@@ -2,6 +2,7 @@ package dashboard
import (
"net/http"
+ "strconv"
"testing"
"time"
@@ -81,3 +82,63 @@ func TestLoadPersistedAgentActivityRestoresScheduledActions(t *testing.T) {
t.Errorf("RecentEvents() request ids = %q, want %q (scheduled action must be in recent history, dashboard request must not)", got, "req-client,req-sched")
}
}
+
+// TestLoadPersistedAgentActivityBudgetIsNotSpentOnNonAgentSources guards the rebuild's
+// query budget: agent events must not be crowded out of the most-recent window by a
+// busy source that is not agent activity. A dashboard left open writes a steady stream
+// of dashboard-source events, so a rebuild that queries every source and filters in
+// memory afterwards spends its whole budget on them and restores few or no agent events.
+func TestLoadPersistedAgentActivityBudgetIsNotSpentOnNonAgentSources(t *testing.T) {
+ store, err := activity.NewStore(t.TempDir(), 30)
+ if err != nil {
+ t.Fatalf("NewStore() error = %v", err)
+ }
+ base := time.Now().UTC().Add(-time.Hour)
+
+ agentIDs := map[string]bool{}
+ record := func(source, agentID string, ts time.Time) {
+ if err := store.Record(activity.Event{
+ Timestamp: ts,
+ Source: source,
+ AgentID: agentID,
+ Method: http.MethodPost,
+ Path: "/tabs/tab/action",
+ Status: http.StatusOK,
+ TabID: "tab",
+ }); err != nil {
+ t.Fatalf("Record(%s) error = %v", source, err)
+ }
+ }
+
+ for i := 0; i < 5; i++ {
+ id := "client-" + strconv.Itoa(i)
+ agentIDs[id] = true
+ record(activity.SourceClient, id, base.Add(time.Duration(i)*time.Second))
+ }
+ for i := 0; i < 3; i++ {
+ id := "sched-" + strconv.Itoa(i)
+ agentIDs[id] = true
+ record(activity.SourceScheduler, id, base.Add(time.Duration(10+i)*time.Second))
+ }
+ for i := 0; i < 1200; i++ {
+ record(activity.SourceDashboard, "", base.Add(time.Duration(100+i)*time.Second))
+ }
+
+ d := NewDashboard(nil)
+ if err := d.LoadPersistedAgentActivity(store); err != nil {
+ t.Fatalf("LoadPersistedAgentActivity() error = %v", err)
+ }
+
+ got := map[string]bool{}
+ for _, a := range d.Agents() {
+ got[a.ID] = true
+ }
+ for id := range agentIDs {
+ if !got[id] {
+ t.Errorf("agent %q dropped from the rebuild; the budget was spent on non-agent sources. rebuilt = %v", id, got)
+ }
+ }
+ if got["anonymous"] {
+ t.Errorf("a non-agent dashboard event was tracked as agent activity; rebuilt = %v", got)
+ }
+}