Avoid duplicating nested error causes

This commit is contained in:
2026-11-05 16:00:39 +00:00
parent ac1e150168
commit 4aeb58f913
3 changed files with 25 additions and 7 deletions

View File

@@ -121,7 +121,12 @@ the selected self-contained contract is not satisfied.
### 3. Avoid Duplicating An Already-Retained Error Cause [P3] ### 3. Avoid Duplicating An Already-Retained Error Cause [P3]
**Status:** Open. **Stages:** 3–4. **Status:** Complete. **Stages:** 3–4.
Completion: ordinary operation projection retains its mapped cause once, without
joining the original subtree a second time. Multi-engine regression coverage
checks exact cause/owner matching and one occurrence of an independent leaf;
specialized projection and redaction tests remain intact.
**Location:** [public error projection](../../errors.go), `mapPublicError`, **Location:** [public error projection](../../errors.go), `mapPublicError`,
particularly lines 26–38. particularly lines 26–38.

View File

@@ -30,12 +30,9 @@ func mapPublicError(err error) error {
if errors.Is(operation.Cause, llm.ErrMalformedResponse) { if errors.Is(operation.Cause, llm.ErrMalformedResponse) {
mapped = errors.Join(ErrMalformedResponse, mapped) mapped = errors.Join(ErrMalformedResponse, mapped)
} }
var internalCapacity *usecase.CapacityError // The projection already retains ordinary causes; adding the original
var internalProvider *llm.ProviderHTTPError // again would duplicate its entire subtree at each enclosing operation.
if errors.As(operation.Cause, &internalCapacity) || errors.As(operation.Cause, &internalProvider) { return errors.Join(owner, mapped)
return errors.Join(owner, mapped)
}
return errors.Join(owner, mapped, operation.Cause)
} }
} }
return mapUnownedPublicError(err) return mapUnownedPublicError(err)

View File

@@ -3,11 +3,27 @@ package promptkit_test
import ( import (
"context" "context"
"errors" "errors"
"strings"
"testing" "testing"
"go.maximumdirect.net/promptkit" "go.maximumdirect.net/promptkit"
) )
func TestNestedErrorsRetainEachCauseWithoutDuplicatingDiagnostics(t *testing.T) {
cause := errors.New("independent-leaf-diagnostic")
for range 10 {
parent := newSimpleEngine(t, "parent", promptkit.WithLLMClient(&fakeLLMClient{err: cause}))
_, err := parent.Run(context.Background(), promptkit.RunRequest{PromptID: "prompt"})
if !errors.Is(err, cause) || !errors.Is(err, promptkit.ErrLLMGenerate) {
t.Fatalf("lost owner or cause: %v", err)
}
if strings.Count(err.Error(), "independent-leaf-diagnostic") != 1 {
t.Fatalf("duplicated leaf: %v", err)
}
cause = err
}
}
type nestedErrorReader struct{ cause error } type nestedErrorReader struct{ cause error }
func (r nestedErrorReader) Read(context.Context, promptkit.ArtifactRef) (*promptkit.Artifact, error) { func (r nestedErrorReader) Read(context.Context, promptkit.ArtifactRef) (*promptkit.Artifact, error) {