Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/cli/logs_episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +17 to +19

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment for firewallBlockedRequestCap refers to BlockedRequestCount, but this cap is about the per-run firewall metric (FirewallAnalysis.BlockedRequests) being truncated by Squid. As written it can be confused with the aggregated episode field EpisodeData.BlockedRequestCount (which can exceed 50 across runs). Consider rewording to explicitly reference FirewallAnalysis.BlockedRequests (and that 50 means “>= 50”, not an exact count).

Suggested change
// 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.
// firewallBlockedRequestCap is the maximum per-run blocked-request count that the
// Squid firewall proxy reports in FirewallAnalysis.BlockedRequests before truncating.
// A FirewallAnalysis.BlockedRequests value equal to this cap means ">= 50", not an
// exact count of 50.

Copilot uses AI. Check for mistakes.
const firewallBlockedRequestCap = 50

// EpisodeEdge represents a deterministic lineage edge between two workflow runs.
type EpisodeEdge struct {
SourceRunID int64 `json:"source_run_id"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 62 additions & 0 deletions pkg/cli/logs_episode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
9 changes: 7 additions & 2 deletions pkg/cli/observability_insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
}
Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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{
Expand Down
76 changes: 76 additions & 0 deletions pkg/cli/observability_insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading