- ToolCallGuardrailConfig + GuardrailEngine with RecordSuccess/RecordFailure - ToolClassification bitmask (idempotent/mutating) with dispatch gating - TurnBudget for token/char accounting with spill-to-disk - AvailabilityCache with TTL + failure grace window - Progressive disclosure engine for tool schema filtering - Tool dispatch executor with parallel/serial classification - RecordSuccess fix: now returns GuardrailDecision (no-progress threshold) - All tests pass with -race, 58 new test functions across 6 test files
134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package tools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestToolClassificationIsIdempotent(t *testing.T) {
|
|
t.Run("zero value is not idempotent", func(t *testing.T) {
|
|
var c ToolClassification
|
|
if c.IsIdempotent() {
|
|
t.Error("zero classification should not be idempotent")
|
|
}
|
|
})
|
|
|
|
t.Run("explicit idempotent flag", func(t *testing.T) {
|
|
if !ClassIdempotent.IsIdempotent() {
|
|
t.Error("ClassIdempotent should be idempotent")
|
|
}
|
|
})
|
|
|
|
t.Run("idempotent is not mutating", func(t *testing.T) {
|
|
if ClassIdempotent.IsMutating() {
|
|
t.Error("idempotent should not be mutating")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestToolClassificationIsMutating(t *testing.T) {
|
|
t.Run("zero value is not mutating", func(t *testing.T) {
|
|
var c ToolClassification
|
|
if c.IsMutating() {
|
|
t.Error("zero classification should not be mutating")
|
|
}
|
|
})
|
|
|
|
t.Run("explicit mutating flag", func(t *testing.T) {
|
|
if !ClassMutating.IsMutating() {
|
|
t.Error("ClassMutating should be mutating")
|
|
}
|
|
})
|
|
|
|
t.Run("mutating is not idempotent", func(t *testing.T) {
|
|
if ClassMutating.IsIdempotent() {
|
|
t.Error("mutating should not be idempotent")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestToolClassificationCombined(t *testing.T) {
|
|
c := ClassIdempotent | ClassMutating
|
|
if !c.IsIdempotent() {
|
|
t.Error("combined should be idempotent")
|
|
}
|
|
if !c.IsMutating() {
|
|
t.Error("combined should be mutating")
|
|
}
|
|
}
|
|
|
|
func TestToolClassificationString(t *testing.T) {
|
|
tests := []struct {
|
|
c ToolClassification
|
|
want string
|
|
}{
|
|
{0, "default"},
|
|
{ClassIdempotent, "idempotent"},
|
|
{ClassMutating, "mutating"},
|
|
{ClassIdempotent | ClassMutating, "idempotent|mutating"},
|
|
{ToolClassification(4), "unknown"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := tt.c.String()
|
|
if got != tt.want {
|
|
t.Errorf("String() = %q, want %q", got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestClassifyEntry(t *testing.T) {
|
|
t.Run("idempotent tools are parallel-safe", func(t *testing.T) {
|
|
e := ToolEntry{
|
|
Name: "reader",
|
|
Handler: noopHandler,
|
|
Classification: ClassIdempotent,
|
|
}
|
|
if got := ClassifyEntry(e); got != ExecParallelSafe {
|
|
t.Errorf("expected parallel-safe, got %v", got)
|
|
}
|
|
})
|
|
|
|
t.Run("mutating tools are never-parallel", func(t *testing.T) {
|
|
e := ToolEntry{
|
|
Name: "writer",
|
|
Handler: noopHandler,
|
|
Classification: ClassMutating,
|
|
}
|
|
if got := ClassifyEntry(e); got != ExecNeverParallel {
|
|
t.Errorf("expected never-parallel, got %v", got)
|
|
}
|
|
})
|
|
|
|
t.Run("unclassified tools default to never-parallel", func(t *testing.T) {
|
|
e := ToolEntry{Name: "unknown", Handler: noopHandler}
|
|
if got := ClassifyEntry(e); got != ExecNeverParallel {
|
|
t.Errorf("expected never-parallel, got %v", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestClassificationJSONRoundTrip(t *testing.T) {
|
|
e := ToolEntry{
|
|
Name: "classified",
|
|
Toolset: "test",
|
|
Description: "A classified tool",
|
|
Handler: noopHandler,
|
|
Classification: ClassIdempotent,
|
|
}
|
|
|
|
data, err := json.Marshal(e)
|
|
if err != nil {
|
|
t.Fatalf("marshal: %v", err)
|
|
}
|
|
|
|
var decoded ToolEntry
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
if decoded.Classification != ClassIdempotent {
|
|
t.Errorf("Classification = %v after round-trip, want idempotent", decoded.Classification)
|
|
}
|
|
}
|