Skip model progress after caller cancellation

This commit is contained in:
2026-11-17 12:54:34 +00:00
parent b3ed91d593
commit 19755e1d47
3 changed files with 47 additions and 7 deletions

View File

@@ -29,6 +29,41 @@ type reply struct {
content string
}
func TestCanceledCallSkipsProgress(t *testing.T) {
for _, tc := range []struct {
name string
cancelBefore bool
wantStarts int
}{
{name: "canceled before start", cancelBefore: true},
{name: "canceled during start", wantStarts: 1},
} {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if tc.cancelBefore {
cancel()
}
starts, finishes := 0, 0
client := RoleClient{hooks: CallHooks{
OnProgress: func(_ structuredoutput.DiagnosticContext, counts *outcomes.ExecutionCounts, _ error) {
if counts == nil {
starts++
cancel()
} else {
finishes++
}
},
}}
var result map[string]any
_, err := client.CompleteStructured(ctx, structuredoutput.Request{}, &result)
if !errors.Is(err, context.Canceled) || starts != tc.wantStarts || finishes != 0 {
t.Fatalf("progress starts = %d, finishes = %d, error = %v", starts, finishes, err)
}
})
}
}
func adapterFixture(t *testing.T, replies []reply, retries, repairs int, hook func(*http.Request)) (*Runtime, *atomic.Int32, string) {
return adapterFixtureWithPrompt(t, replies, retries, repairs, hook, "")
}