Unify structured call completion and retain typed failures
This commit is contained in:
@@ -38,7 +38,7 @@ func adapterFixtureWithPrompt(t *testing.T, replies []reply, retries, repairs in
|
||||
var count atomic.Int32
|
||||
t.Setenv("ADAPTER_KEY", "adapter-secret")
|
||||
dir := t.TempDir()
|
||||
resources := "backends:\n - id: offline\n endpoint: https://provider.invalid/v1\n api_key_env: ADAPTER_KEY\n concurrency_limit: 1\n queue_capacity: 0\nprofiles:\n - id: base\n backend: offline\n model: full-model\n"
|
||||
resources := "backends:\n - id: offline\n endpoint: https://provider.invalid/v1\n api_key_env: ADAPTER_KEY\n concurrency_limit: 1\n queue_capacity: 0\nprofiles:\n - id: base\n backend: offline\n api_key_required: true\n api_key_env: ADAPTER_KEY\n model: full-model\n - id: short-attempt\n backend: offline\n model: full-model\n timeout_seconds: 1\n"
|
||||
for name, body := range map[string]string{"config.yml": "version: 3\n", "prompts.yml": resources + operatorPrompt} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -170,13 +170,13 @@ func TestPreparedAdapterRepairsAndClassifies(t *testing.T) {
|
||||
if len(records) != 2 || records[0].Finished.IsZero() == false || records[1].Finished.IsZero() || records[1].Context.Role != role {
|
||||
t.Fatalf("logical records: %+v", records)
|
||||
}
|
||||
if tc.malformed && records[1].FinalContent == "" {
|
||||
if tc.malformed && (response.FinalContent == "" || len(response.Content) != 0) {
|
||||
t.Fatal("invalid final content lost")
|
||||
}
|
||||
if len(events) == 0 || events[len(events)-1].Kind != pk.ExecutionFinished || int(events[len(events)-1].RepairCalls) != tc.repairCalls || int(events[len(events)-1].TransientRetries) != tc.transientRetries {
|
||||
t.Fatalf("execution counts: %+v", events)
|
||||
}
|
||||
if err == nil && (out == nil || string(response.Content) != valid || response.TotalTokens != 5*(tc.repairCalls+1) || response.Model != "full-model") {
|
||||
if err == nil && (out == nil || string(response.Content) != valid || response.Model != "full-model") {
|
||||
t.Fatalf("decoded response: %+v, %v", response, out)
|
||||
}
|
||||
})
|
||||
@@ -404,7 +404,7 @@ func TestPreparedAdapterCancellationStopsRetryWait(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPreparedAdapterDistinguishesOperationTimeoutFromParentCancellation(t *testing.T) {
|
||||
blocking := func(*http.Request) { time.Sleep(500 * time.Millisecond) }
|
||||
blocking := func(r *http.Request) { <-r.Context().Done() }
|
||||
runtime, _, _ := adapterFixture(t, []reply{{200, `{"corrections":[]}`}}, 0, 0, blocking)
|
||||
runtime.Config.LLM.OperationTimeout = 200 * time.Millisecond
|
||||
client, err := runtime.NewRoleClient("proposal", "base", runtime.Config.LLM, CallHooks{})
|
||||
@@ -414,7 +414,7 @@ func TestPreparedAdapterDistinguishesOperationTimeoutFromParentCancellation(t *t
|
||||
var output map[string]any
|
||||
response, err := client.CompleteStructured(context.Background(), adapterRequest(responseschema.CorrectionSetKey), &output)
|
||||
var failure *structuredoutput.Failure
|
||||
if !errors.As(err, &failure) || failure.Class != structuredoutput.FailureRecoverable || failure.Reason != outcomes.ReasonOperationTimeout || !response.Execution.Available {
|
||||
if !errors.As(err, &failure) || failure.Class != structuredoutput.FailureRecoverable || failure.Reason != outcomes.ReasonOperationTimeout || !errors.Is(err, pk.ErrOperationTimeout) || !response.Execution.Available {
|
||||
t.Fatalf("local timeout: failure=%#v counts=%+v", failure, response.Execution)
|
||||
}
|
||||
|
||||
@@ -426,6 +426,58 @@ func TestPreparedAdapterDistinguishesOperationTimeoutFromParentCancellation(t *t
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedAdapterDistinguishesAttemptTimeoutFromParentDeadline(t *testing.T) {
|
||||
blocking := func(r *http.Request) { <-r.Context().Done() }
|
||||
runtime, count, _ := adapterFixture(t, []reply{{200, `{"corrections":[]}`}}, 0, 0, blocking)
|
||||
client, err := runtime.NewRoleClient("proposal", "short-attempt", runtime.Config.LLM, CallHooks{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
parentBudget time.Duration
|
||||
class outcomes.FailureClass
|
||||
reason outcomes.ReasonCode
|
||||
}{
|
||||
{"attempt", 5 * time.Second, outcomes.FailureRecoverable, outcomes.ReasonTransportFailed},
|
||||
{"parent", 300 * time.Millisecond, outcomes.FailureCanceled, outcomes.ReasonCanceled},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
parent, cancel := context.WithTimeout(context.Background(), tc.parentBudget)
|
||||
defer cancel()
|
||||
before := count.Load()
|
||||
var output map[string]any
|
||||
response, err := client.CompleteStructured(parent, adapterRequest(responseschema.CorrectionSetKey), &output)
|
||||
var failure *structuredoutput.Failure
|
||||
if !errors.As(err, &failure) || failure.Class != tc.class || failure.Reason != tc.reason ||
|
||||
!errors.Is(err, context.DeadlineExceeded) || errors.Is(err, pk.ErrOperationTimeout) ||
|
||||
!response.Execution.Available || count.Load() != before+1 {
|
||||
t.Fatalf("deadline classification: failure=%#v error=%v counts=%+v requests=%d", failure, err, response.Execution, count.Load()-before)
|
||||
}
|
||||
if tc.class == outcomes.FailureRecoverable && (!errors.Is(err, pk.ErrTransport) || parent.Err() != nil) {
|
||||
t.Fatalf("missing transport cause or parent also expired: error=%v parent=%v", err, parent.Err())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedAdapterMissingCredentialRetainsSetupCause(t *testing.T) {
|
||||
runtime, count, _ := adapterFixture(t, nil, 0, 0, nil)
|
||||
t.Setenv("ADAPTER_KEY", "")
|
||||
client, err := runtime.NewRoleClient("proposal", "base", runtime.Config.LLM, CallHooks{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output map[string]any
|
||||
_, err = client.CompleteStructured(context.Background(), adapterRequest(responseschema.CorrectionSetKey), &output)
|
||||
var failure *structuredoutput.Failure
|
||||
if !errors.As(err, &failure) || failure.Class != outcomes.FailureFatal || failure.Reason != outcomes.ReasonSetupFailure ||
|
||||
!errors.Is(err, pk.ErrAPIKeyEnvMissing) || !strings.Contains(err.Error(), "ADAPTER_KEY") ||
|
||||
!strings.Contains(err.Error(), `proposal profile "base"`) || count.Load() != 0 {
|
||||
t.Fatalf("missing credential classification: failure=%#v error=%v requests=%d", failure, err, count.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparedAdapterClassifiesSetupAndDomainDecodeFailures(t *testing.T) {
|
||||
runtime, _, _ := adapterFixture(t, []reply{{200, `{"validations":[{"correction_index":9999999999999999999999999,"approved":true,"reason":"ok"}]}`}}, 0, 0, nil)
|
||||
client, err := runtime.NewRoleClient("validation", "base", runtime.Config.LLM, CallHooks{})
|
||||
@@ -477,7 +529,7 @@ func TestPreparedAdapterUnknownValidationFailureRemainsFatal(t *testing.T) {
|
||||
parent, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
cause := fmt.Errorf("%w: embedded contract defect", pk.ErrValidation)
|
||||
failure := classifiedFailure(parent, parent, cause, nil)
|
||||
failure := classifiedFailure(parent, cause, nil)
|
||||
if failure.Class != structuredoutput.FailureFatal || failure.Reason != outcomes.ReasonSetupFailure || !errors.Is(failure, pk.ErrValidation) {
|
||||
t.Fatalf("unknown validation failure: %#v", failure)
|
||||
}
|
||||
@@ -488,7 +540,7 @@ func TestPreparedAdapterUnknownValidationFailureRemainsFatal(t *testing.T) {
|
||||
func TestOperationTimeoutDuringPreparationIsRecoverable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cause := errors.Join(pk.ErrPromptLoad, pk.ErrOperationTimeout, context.DeadlineExceeded)
|
||||
failure := classifiedFailure(ctx, ctx, cause, nil)
|
||||
failure := classifiedFailure(ctx, cause, nil)
|
||||
if failure.Class != structuredoutput.FailureRecoverable || failure.Reason != outcomes.ReasonOperationTimeout || !errors.Is(failure, pk.ErrOperationTimeout) {
|
||||
t.Fatalf("failure = %#v", failure)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user