|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 8 | +) |
| 9 | + |
| 10 | +func boolPtr(b bool) *bool { v := b; return &v } |
| 11 | + |
| 12 | +func TestDerive_truthTable(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + in spec.ResolvedToolSafety |
| 16 | + want Decision |
| 17 | + }{ |
| 18 | + {"trusted_no_explicit_approval", spec.ResolvedToolSafety{Trusted: true, SideEffects: true, RequiresApproval: false}, DecisionAllow}, |
| 19 | + {"untrusted_read_only", spec.ResolvedToolSafety{Trusted: false, SideEffects: false, RequiresApproval: false}, DecisionAllow}, |
| 20 | + {"untrusted_mutating", spec.ResolvedToolSafety{Trusted: false, SideEffects: true, RequiresApproval: true}, DecisionRequireApproval}, |
| 21 | + {"explicit_requires_approval", spec.ResolvedToolSafety{Trusted: true, SideEffects: false, RequiresApproval: true}, DecisionRequireApproval}, |
| 22 | + } |
| 23 | + for _, tt := range tests { |
| 24 | + t.Run(tt.name, func(t *testing.T) { |
| 25 | + if got := Derive(tt.in); got != tt.want { |
| 26 | + t.Fatalf("Derive() = %q want %q", got, tt.want) |
| 27 | + } |
| 28 | + }) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestCheckToolCall_safetyFallback_requiresApprovalWithoutApprove(t *testing.T) { |
| 33 | + g := testGraphWithTools("slack") |
| 34 | + g.Tools["slack"].Spec.Safety = &spec.ToolSafety{Trusted: boolPtr(false), SideEffects: boolPtr(true)} |
| 35 | + ev := NewEvaluator(g, nil) |
| 36 | + err := ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 37 | + Run: RunContext{}, |
| 38 | + Uses: "tool.slack.message.send", |
| 39 | + }) |
| 40 | + if err == nil { |
| 41 | + t.Fatal("expected denial") |
| 42 | + } |
| 43 | + d, ok := AsDenied(err) |
| 44 | + if !ok || d.Reason != ReasonApprovalRequired { |
| 45 | + t.Fatalf("got %v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestCheckToolCall_safetyFallback_trustedAllows(t *testing.T) { |
| 50 | + g := testGraphWithTools("slack") |
| 51 | + g.Tools["slack"].Spec.Safety = &spec.ToolSafety{Trusted: boolPtr(true)} |
| 52 | + ev := NewEvaluator(g, nil) |
| 53 | + err := ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 54 | + Run: RunContext{}, |
| 55 | + Uses: "tool.slack.message.send", |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestCheckToolCall_safetyFallback_approveGrants(t *testing.T) { |
| 63 | + g := testGraphWithTools("slack") |
| 64 | + ev := NewEvaluator(g, nil) |
| 65 | + err := ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 66 | + Run: RunContext{ApprovedActions: []string{"tool.slack.message.send"}}, |
| 67 | + Uses: "tool.slack.message.send", |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestCheckToolCall_explicitPolicyRuleBeforeSafety(t *testing.T) { |
| 75 | + g := testGraphWithTools("github") |
| 76 | + g.Tools["github"].Spec.Safety = &spec.ToolSafety{Trusted: boolPtr(true)} |
| 77 | + pol := &spec.PolicySpec{ |
| 78 | + Approvals: &spec.PolicyApprovals{ |
| 79 | + RequiredFor: []string{"tool.github.pull_request.merge"}, |
| 80 | + }, |
| 81 | + } |
| 82 | + ev := NewEvaluator(g, pol) |
| 83 | + err := ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 84 | + Run: RunContext{}, |
| 85 | + Uses: "tool.github.pull_request.merge", |
| 86 | + }) |
| 87 | + if err == nil { |
| 88 | + t.Fatal("expected explicit policy approval denial") |
| 89 | + } |
| 90 | + d, ok := AsDenied(err) |
| 91 | + if !ok || d.Reason != ReasonApprovalRequired { |
| 92 | + t.Fatalf("got %v", err) |
| 93 | + } |
| 94 | + // trusted safety would allow, but explicit rule wins |
| 95 | + err = ev.CheckToolCall(context.Background(), ToolCallContext{ |
| 96 | + Run: RunContext{}, |
| 97 | + Uses: "tool.github.pull_request.get", |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("trusted tool without explicit rule should allow: %v", err) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestEffectiveToolDecision_explicitVsDerived(t *testing.T) { |
| 105 | + g := testGraphWithTools("github") |
| 106 | + g.Tools["github"].Spec.Safety = &spec.ToolSafety{Trusted: boolPtr(true)} |
| 107 | + pol := &spec.PolicySpec{ |
| 108 | + Approvals: &spec.PolicyApprovals{ |
| 109 | + RequiredFor: []string{"tool.github.pull_request.merge"}, |
| 110 | + }, |
| 111 | + } |
| 112 | + td := EffectiveToolDecision(g, pol, "github") |
| 113 | + if td.Decision != DecisionRequireApproval || td.Source != SourceExplicitPolicyRule { |
| 114 | + t.Fatalf("prefix rule: %+v", td) |
| 115 | + } |
| 116 | + td = EffectiveToolDecision(g, nil, "github") |
| 117 | + if td.Decision != DecisionAllow || td.Source != SourceSafetyMetadata { |
| 118 | + t.Fatalf("trusted metadata: %+v", td) |
| 119 | + } |
| 120 | +} |
0 commit comments