-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverdict_test.go
More file actions
267 lines (246 loc) · 8.61 KB
/
Copy pathverdict_test.go
File metadata and controls
267 lines (246 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package agenthooks
import (
"context"
"errors"
"strings"
"testing"
"time"
)
// Every decision type satisfies the sealed Decision interface.
var (
_ Decision = ToolPreDecision{}
_ Decision = PromptDecision{}
_ Decision = StopDecision{}
_ Decision = ToolPostDecision{}
_ Decision = SessionStartDecision{}
_ Decision = coreDecision{}
)
func testToolPreEvent(p Provider, tool string) *ToolPreEvent {
s := SessionInfo{ID: "sess-1", TurnID: "turn-1"}
return &ToolPreEvent{
Event: Event{Provider: p, NativeName: "PreToolUse", Kind: KindToolPre, Session: s},
Tool: makeToolCall(s, tool, "tu-1", []byte(`{"command":"rm -rf /tmp/x"}`), nil),
}
}
func TestDecisionKindString(t *testing.T) {
cases := map[DecisionKind]string{
DecisionNoDecision: "no-decision",
DecisionAllow: "allow",
DecisionDeny: "deny",
DecisionAsk: "ask",
DecisionAcceptPrompt: "accept-prompt",
DecisionBlockPrompt: "block-prompt",
DecisionFinish: "finish",
DecisionContinue: "continue",
DecisionObserved: "observed",
DecisionFlagOutput: "flag-output",
DecisionReplaceOutput: "replace-output",
DecisionContinueSession: "continue-session",
}
for k, want := range cases {
if got := k.String(); got != want {
t.Errorf("DecisionKind(%d).String() = %q, want %q", int(k), got, want)
}
}
if got := DecisionKind(99).String(); got != "decision-kind(99)" {
t.Errorf("unknown kind stringifies as %q", got)
}
}
func TestDecisionReadAccessors(t *testing.T) {
d := Deny("no shells").WithContext("a").WithContext("b").WithSystemMessage("careful")
if d.Kind() != DecisionDeny || d.Reason() != "no shells" || d.SystemMessage() != "careful" {
t.Errorf("common accessors: kind=%v reason=%q sys=%q", d.Kind(), d.Reason(), d.SystemMessage())
}
if got := d.Context(); len(got) != 2 || got[0] != "a" || got[1] != "b" {
t.Errorf("Context() = %v, want [a b]", got)
}
if _, ok := Allow().UpdatedInput(); ok {
t.Error("UpdatedInput must report absent without WithUpdatedInput")
}
if v, ok := Allow().WithUpdatedInput(map[string]any{"command": "true"}).UpdatedInput(); !ok || v == nil {
t.Errorf("UpdatedInput() = %v, %v; want value, true", v, ok)
}
if ContinueWith("more").Instruction() != "more" {
t.Error("Instruction must return the ContinueWith payload")
}
if Finish().Kind() != DecisionFinish || ContinueWith("x").Kind() != DecisionContinue {
t.Error("stop decision kinds wrong")
}
if v, ok := ReplaceOutput("redacted").ReplacedOutput(); !ok || v != "redacted" {
t.Errorf("ReplacedOutput() = %v, %v; want redacted, true", v, ok)
}
if _, ok := Observed().ReplacedOutput(); ok {
t.Error("ReplacedOutput must report absent on Observed")
}
if FlagOutput("stale").Reason() != "stale" {
t.Error("Reason on FlagOutput")
}
if AcceptPrompt().Kind() != DecisionAcceptPrompt || BlockPrompt("x").Kind() != DecisionBlockPrompt {
t.Error("prompt decision kinds wrong")
}
if ContinueSession().Kind() != DecisionContinueSession {
t.Error("session decision kind wrong")
}
var zero ToolPreDecision
if zero.Kind() != DecisionNoDecision {
t.Error("zero-value decision must be neutral")
}
}
// TestDecisionBlocks is the truth table for the Blocks classification over
// every decision constructor: true exactly for the kinds whose intent is
// "the action is prevented" (deny, block-prompt). An ask defers to a human —
// it does not block.
func TestDecisionBlocks(t *testing.T) {
cases := []struct {
name string
decision Decision
want bool
}{
// tool.pre / permission.request
{"NoDecision", NoDecision(), false},
{"Allow", Allow(), false},
{"Deny", Deny("no shells"), true},
{"AskUser", AskUser("confirm?"), false},
// prompt.submitted
{"AcceptPrompt", AcceptPrompt(), false},
{"BlockPrompt", BlockPrompt("secrets"), true},
// agent.stop / subagent.stop
{"Finish", Finish(), false},
{"ContinueWith", ContinueWith("keep going"), false},
// tool.post / tool.error
{"Observed", Observed(), false},
{"FlagOutput", FlagOutput("stale"), false},
{"ReplaceOutput", ReplaceOutput("redacted"), false},
// session.start
{"ContinueSession", ContinueSession(), false},
}
for _, tc := range cases {
if got := tc.decision.Blocks(); got != tc.want {
t.Errorf("%s.Blocks() = %v, want %v", tc.name, got, tc.want)
}
}
var zero ToolPreDecision
if zero.Blocks() {
t.Error("zero-value (neutral) decision must not block")
}
if !Deny("x").WithContext("c").WithSystemMessage("s").Blocks() {
t.Error("modifiers must not change the Blocks classification")
}
}
func TestContextAccessorDoesNotAlias(t *testing.T) {
d := NoDecision().WithContext("keep")
got := d.Context()
got[0] = "mutated"
if d.Context()[0] != "keep" {
t.Error("Context must return a copy, not the internal slice")
}
}
func TestDecideReturnsWinningDecision(t *testing.T) {
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
return Deny("blocked").WithContext("why"), nil
})
d, err := r.Decide(context.Background(), testToolPreEvent(ProviderClaudeCode, "Bash"))
if err != nil {
t.Fatalf("Decide: %v", err)
}
if d.Kind() != DecisionDeny || d.Reason() != "blocked" {
t.Errorf("got kind=%v reason=%q", d.Kind(), d.Reason())
}
if _, ok := d.(ToolPreDecision); !ok {
t.Errorf("Decide must return the concrete decision type, got %T", d)
}
}
func TestDecideSkipsCapabilityDegradation(t *testing.T) {
// Codex cannot express ask: Run degrades it per policy, Decide must not.
r := quietRunner(WithPolicy(Policy{Unsupported: Degrade, AskFallback: FallbackDeny}))
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
return AskUser("confirm?"), nil
})
d, err := r.Decide(context.Background(), testToolPreEvent(ProviderCodex, "shell"))
if err != nil {
t.Fatalf("Decide: %v", err)
}
if d.Kind() != DecisionAsk {
t.Errorf("Decide must return the raw ask (no applyPolicy), got %v", d.Kind())
}
}
func TestDecideNeutralIsZeroDecision(t *testing.T) {
r := quietRunner()
d, err := r.Decide(context.Background(), testToolPreEvent(ProviderClaudeCode, "Bash"))
if err != nil {
t.Fatalf("Decide: %v", err)
}
if d == nil || d.Kind() != DecisionNoDecision {
t.Errorf("exhausted pipeline must be the neutral zero decision, got %v", d)
}
}
func TestDecideStageErrorReturnsError(t *testing.T) {
sentinel := errors.New("stage boom")
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
return NoDecision(), sentinel
})
d, err := r.Decide(context.Background(), testToolPreEvent(ProviderClaudeCode, "Bash"))
if !errors.Is(err, sentinel) {
t.Errorf("stage error must return as-is, got %v", err)
}
if d != nil {
t.Errorf("decision must be nil on error, got %v", d)
}
}
func TestDecidePanicBecomesError(t *testing.T) {
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
panic("handler bug")
})
_, err := r.Decide(context.Background(), testToolPreEvent(ProviderClaudeCode, "Bash"))
if err == nil || !strings.Contains(err.Error(), "panic") {
t.Errorf("panic must convert to an error, got %v", err)
}
}
func TestDecideHonorsContextDeadline(t *testing.T) {
r := quietRunner()
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
time.Sleep(2 * time.Second) // ignores ctx on purpose
return Allow(), nil
})
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
start := time.Now()
_, err := r.Decide(ctx, testToolPreEvent(ProviderClaudeCode, "Bash"))
if elapsed := time.Since(start); elapsed > time.Second {
t.Errorf("deadline not enforced: took %v", elapsed)
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("want deadline error, got %v", err)
}
}
func TestDecideRunsObserversAndMiddleware(t *testing.T) {
r := quietRunner()
var observed, wrapped bool
r.OnAny(func(ctx context.Context, e *Event) error {
observed = true
return nil
})
r.Use(func(ctx context.Context, typed any, next Next) (Decision, error) {
wrapped = true
return next(ctx, typed)
})
r.OnToolPre(func(ctx context.Context, e *ToolPreEvent) (ToolPreDecision, error) {
return Deny("no"), nil
})
d, err := r.Decide(context.Background(), testToolPreEvent(ProviderClaudeCode, "Bash"))
if err != nil || d.Kind() != DecisionDeny {
t.Fatalf("got %v, %v", d, err)
}
if !observed || !wrapped {
t.Errorf("observers/middleware must run in Decide: observed=%v wrapped=%v", observed, wrapped)
}
}
func TestDecideRejectsNonEvent(t *testing.T) {
r := quietRunner()
if _, err := r.Decide(context.Background(), 42); err == nil {
t.Error("Decide must reject values that are not agenthooks events")
}
}