diff --git a/pkg/cli/logs_episode.go b/pkg/cli/logs_episode.go index 8456a6e192b..1ee7d250201 100644 --- a/pkg/cli/logs_episode.go +++ b/pkg/cli/logs_episode.go @@ -14,6 +14,11 @@ import ( var logsEpisodeLog = logger.New("cli:logs_episode") +// firewallBlockedRequestCap is the maximum blocked-request count the Squid firewall +// proxy reports before truncating. A BlockedRequestCount equal to this value may +// represent any number of requests >= 50 and should not be treated as an exact figure. +const firewallBlockedRequestCap = 50 + // EpisodeEdge represents a deterministic lineage edge between two workflow runs. type EpisodeEdge struct { SourceRunID int64 `json:"source_run_id"` @@ -61,6 +66,7 @@ type EpisodeData struct { LatestSuccessFallbackCount int `json:"latest_success_fallback_count"` NewMCPFailureRunCount int `json:"new_mcp_failure_run_count"` BlockedRequestIncreaseRunCount int `json:"blocked_request_increase_run_count"` + BlockedRequestAtCap bool `json:"blocked_request_at_cap,omitempty"` ResourceHeavyNodeCount int `json:"resource_heavy_node_count"` PoorControlNodeCount int `json:"poor_control_node_count"` RiskDistribution string `json:"risk_distribution"` @@ -187,6 +193,9 @@ func buildEpisodeData(runs []RunData, processedRuns []ProcessedRun) ([]EpisodeDa acc.metadata.MCPFailureCount += len(pr.MCPFailures) if pr.FirewallAnalysis != nil { acc.metadata.BlockedRequestCount += pr.FirewallAnalysis.BlockedRequests + if pr.FirewallAnalysis.BlockedRequests >= firewallBlockedRequestCap { + acc.metadata.BlockedRequestAtCap = true + } } // Collect per-tool-call metrics for this run. if pr.MCPToolUsage != nil { diff --git a/pkg/cli/logs_episode_test.go b/pkg/cli/logs_episode_test.go index 30739d0c5e6..75cd7ee8bf1 100644 --- a/pkg/cli/logs_episode_test.go +++ b/pkg/cli/logs_episode_test.go @@ -79,6 +79,68 @@ func TestBuildEpisodeDataIncludesToolCalls(t *testing.T) { assert.Empty(t, tc1.Error, "no error expected") } +func TestBuildEpisodeDataSetsBlockedAtCapWhenFirewallCountHitsCap(t *testing.T) { + runs := []RunData{ + { + RunID: 401, + WorkflowName: "firewall-heavy", + Status: "completed", + CreatedAt: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + }, + } + processedRuns := []ProcessedRun{ + { + Run: WorkflowRun{ + DatabaseID: 401, + WorkflowName: "firewall-heavy", + }, + FirewallAnalysis: &FirewallAnalysis{ + TotalRequests: 100, + BlockedRequests: firewallBlockedRequestCap, // exactly at the proxy cap + AllowedRequests: 50, + }, + }, + } + + episodes, _ := buildEpisodeData(runs, processedRuns) + require.Len(t, episodes, 1, "expected one episode") + + ep := episodes[0] + assert.Equal(t, firewallBlockedRequestCap, ep.BlockedRequestCount, "blocked count should be accumulated") + assert.True(t, ep.BlockedRequestAtCap, "BlockedRequestAtCap should be true when count == cap") +} + +func TestBuildEpisodeDataDoesNotSetBlockedAtCapBelowThreshold(t *testing.T) { + runs := []RunData{ + { + RunID: 402, + WorkflowName: "low-block", + Status: "completed", + CreatedAt: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), + }, + } + processedRuns := []ProcessedRun{ + { + Run: WorkflowRun{ + DatabaseID: 402, + WorkflowName: "low-block", + }, + FirewallAnalysis: &FirewallAnalysis{ + TotalRequests: 100, + BlockedRequests: 8, + AllowedRequests: 92, + }, + }, + } + + episodes, _ := buildEpisodeData(runs, processedRuns) + require.Len(t, episodes, 1, "expected one episode") + + ep := episodes[0] + assert.Equal(t, 8, ep.BlockedRequestCount, "blocked count should be accumulated") + assert.False(t, ep.BlockedRequestAtCap, "BlockedRequestAtCap should be false when count is below cap") +} + func TestBuildEpisodeDataNoToolCallsWhenMCPUsageAbsent(t *testing.T) { runs := []RunData{ { diff --git a/pkg/cli/observability_insights.go b/pkg/cli/observability_insights.go index a92104274f9..1869a9c4517 100644 --- a/pkg/cli/observability_insights.go +++ b/pkg/cli/observability_insights.go @@ -32,6 +32,7 @@ type workflowObservabilityStats struct { maxTurns int blocked int totalNet int + blockedAtCap bool } func buildAuditObservabilityInsights(processedRun ProcessedRun, metrics MetricsData, toolUsage []ToolUsageInfo, createdItems []CreatedItemReport) []ObservabilityInsight { @@ -109,7 +110,8 @@ func buildAuditObservabilityInsights(processedRun ProcessedRun, metrics MetricsD if processedRun.FirewallAnalysis.BlockedRequests > 0 { title = "Network friction detected" severity = "medium" - if blockedRate >= 0.5 || processedRun.FirewallAnalysis.BlockedRequests >= 10 { + blockedAtCap := processedRun.FirewallAnalysis.BlockedRequests >= firewallBlockedRequestCap + if blockedRate >= 0.5 || (processedRun.FirewallAnalysis.BlockedRequests >= 10 && !blockedAtCap) { severity = "high" } } @@ -184,6 +186,9 @@ func buildLogsObservabilityInsights(processedRuns []ProcessedRun, toolUsage []To if pr.FirewallAnalysis != nil { stats.blocked += pr.FirewallAnalysis.BlockedRequests stats.totalNet += pr.FirewallAnalysis.TotalRequests + if pr.FirewallAnalysis.BlockedRequests >= firewallBlockedRequestCap { + stats.blockedAtCap = true + } } } @@ -275,7 +280,7 @@ func buildLogsObservabilityInsights(processedRuns []ProcessedRun, toolUsage []To } if networkHotspot != nil { severity := "medium" - if networkRate >= 0.5 || networkHotspot.blocked >= 10 { + if networkRate >= 0.5 || (networkHotspot.blocked >= 10 && !networkHotspot.blockedAtCap) { severity = "high" } insights = append(insights, ObservabilityInsight{ diff --git a/pkg/cli/observability_insights_test.go b/pkg/cli/observability_insights_test.go index 548459607d7..e02d7752812 100644 --- a/pkg/cli/observability_insights_test.go +++ b/pkg/cli/observability_insights_test.go @@ -92,6 +92,82 @@ func TestBuildLogsObservabilityInsights(t *testing.T) { assert.Contains(t, text, "Tool concentration observed") } +func TestBuildAuditObservabilityInsightsSuppressesHighSeverityAtFirewallCap(t *testing.T) { + // blocked == firewallBlockedRequestCap: the absolute-count trigger (>=10) should + // not elevate to "high" because the proxy may have truncated the real count. + processedRun := ProcessedRun{ + Run: WorkflowRun{Turns: 3}, + FirewallAnalysis: &FirewallAnalysis{ + TotalRequests: 200, + BlockedRequests: firewallBlockedRequestCap, // 50 — at cap + AllowedRequests: 150, + }, + } + + insights := buildAuditObservabilityInsights(processedRun, MetricsData{Turns: 3}, nil, nil) + + var networkInsight *ObservabilityInsight + for i := range insights { + if insights[i].Category == "network" { + networkInsight = &insights[i] + break + } + } + require.NotNil(t, networkInsight, "a network insight should be emitted") + assert.Equal(t, "medium", networkInsight.Severity, "severity should stay medium when blocked count is at proxy cap (<=50% block rate)") +} + +func TestBuildAuditObservabilityInsightsHighSeverityWhenHighBlockRate(t *testing.T) { + // Even at cap, a >=50% block rate should still produce high severity. + processedRun := ProcessedRun{ + Run: WorkflowRun{Turns: 3}, + FirewallAnalysis: &FirewallAnalysis{ + TotalRequests: 60, + BlockedRequests: firewallBlockedRequestCap, // 50 out of 60 → 83% block rate + AllowedRequests: 10, + }, + } + + insights := buildAuditObservabilityInsights(processedRun, MetricsData{Turns: 3}, nil, nil) + + var networkInsight *ObservabilityInsight + for i := range insights { + if insights[i].Category == "network" { + networkInsight = &insights[i] + break + } + } + require.NotNil(t, networkInsight, "a network insight should be emitted") + assert.Equal(t, "high", networkInsight.Severity, "severity should be high when block rate >= 50%% even at cap") +} + +func TestBuildLogsObservabilityInsightsSuppressesHighSeverityAtFirewallCap(t *testing.T) { + // A workflow with blocked == firewallBlockedRequestCap and a low block rate + // should produce a "medium" network hotspot, not "high". + processedRuns := []ProcessedRun{ + { + Run: WorkflowRun{WorkflowName: "w1", Conclusion: "success", Turns: 5}, + FirewallAnalysis: &FirewallAnalysis{ + TotalRequests: 500, + BlockedRequests: firewallBlockedRequestCap, // 50/500 = 10% → low rate but >=10 + AllowedRequests: 450, + }, + }, + } + + insights := buildLogsObservabilityInsights(processedRuns, nil) + + var networkInsight *ObservabilityInsight + for i := range insights { + if insights[i].Category == "network" { + networkInsight = &insights[i] + break + } + } + require.NotNil(t, networkInsight, "a network insight should be emitted") + assert.Equal(t, "medium", networkInsight.Severity, "severity should stay medium when blocked count is at proxy cap with low block rate") +} + func TestBuildAuditDataIncludesObservabilityInsights(t *testing.T) { processedRun := ProcessedRun{ Run: WorkflowRun{