Avoid redundant smoke report copying

This commit is contained in:
2026-10-13 21:12:17 +00:00
parent 4cda3674c4
commit 5dc6370a97
4 changed files with 127 additions and 25 deletions

View File

@@ -211,6 +211,12 @@ func toDomainExecutionTargetOverride(override *ExecutionTargetOverride) (*domain
} }
func fromDomainExecutionTarget(target domain.ExecutionTarget) ExecutionTarget { func fromDomainExecutionTarget(target domain.ExecutionTarget) ExecutionTarget {
return fromDomainExecutionTargetWithOwnedParams(target, copyAnyMap(target.ExtraParams))
}
// fromDomainExecutionTargetWithOwnedParams projects scalar settings and takes
// ownership of extraParams, which the caller has already copied.
func fromDomainExecutionTargetWithOwnedParams(target domain.ExecutionTarget, extraParams map[string]any) ExecutionTarget {
return ExecutionTarget{ return ExecutionTarget{
BackendID: target.BackendID, BackendID: target.BackendID,
Endpoint: target.Endpoint, Endpoint: target.Endpoint,
@@ -222,7 +228,7 @@ func fromDomainExecutionTarget(target domain.ExecutionTarget) ExecutionTarget {
ServiceTier: target.ServiceTier, ServiceTier: target.ServiceTier,
ReasoningEffort: target.ReasoningEffort, ReasoningEffort: target.ReasoningEffort,
APIKeyEnv: target.APIKeyEnv, APIKeyEnv: target.APIKeyEnv,
ExtraParams: copyAnyMap(target.ExtraParams), ExtraParams: extraParams,
} }
} }

View File

@@ -1,6 +1,6 @@
# Smoke Test Implementation Review Findings # Smoke Test Implementation Review Findings
Status: Findings 1–3 completed; Finding 4 open. Reviews completed 2026-10-13. Status: All four findings completed 2026-10-13.
Reviewed implementation commits after `8fbd058` through Reviewed implementation commits after `8fbd058` through
`2f735aedf9b5dcc98164292db46ae1eab3e15cde`, against the `2f735aedf9b5dcc98164292db46ae1eab3e15cde`, against the
@@ -141,7 +141,7 @@ consumer examples, and `git diff --check`.
## Finding 4: Consolidate Smoke Report Ownership Copying ## Finding 4: Consolidate Smoke Report Ownership Copying
Priority: P3. Status: open. Maintainability/efficiency consolidation candidate. Priority: P3. Status: completed 2026-10-13.
Follow-up review covers the implementation and remediation commits through Follow-up review covers the implementation and remediation commits through
`5ea6f06a6d0be32ecb68bc98513f02f29be1cae3`. `5ea6f06a6d0be32ecb68bc98513f02f29be1cae3`.
@@ -197,6 +197,17 @@ Focused ownership tests passed on this revision:
'TestSmokeTestConcludedInferenceMismatchHasNilGoErrorAndDetachedValues|TestCloneSmokeTestReportOwnsMutableValues' 'TestSmokeTestConcludedInferenceMismatchHasNilGoErrorAndDetachedValues|TestCloneSmokeTestReportOwnsMutableValues'
-count=1`. No implementation was changed during the duplication review. -count=1`. No implementation was changed during the duplication review.
Completion: the facade now copies mutable values directly into the public report,
without first cloning an intermediate domain report. Provider parameters retain
the existing JSON-value copy/error handling and pass through the shared target
projection without another copy; credentials remain excluded. Domain cloning
continues to serve its separate internal ownership boundary. Focused conversion
coverage protects retained source values, independently converted results, nested
maps/slices, optional facts/timings, attempts, credential exclusion, and copy errors.
Existing public and domain ownership tests remain unchanged. Validation passed
with `GOWORK=off`: focused ownership tests, `go test -race ./...`, `go vet ./...`,
the three offline consumer examples, and `git diff --check`.
## Initial Review Validation ## Initial Review Validation
Passed with `GOWORK=off`: Passed with `GOWORK=off`:

View File

@@ -0,0 +1,84 @@
package promptkit
import (
"encoding/json"
"reflect"
"strings"
"testing"
"time"
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
)
func TestSmokeReportConversionOwnsMutableValues(t *testing.T) {
applicable, reachable := true, true
status := 200
wait, duration := 3*time.Millisecond, 5*time.Millisecond
source := domain.SmokeTestReport{
Applicable: &applicable,
Target: domain.SmokeTestTarget{EffectiveSettings: domain.ExecutionTarget{
APIKey: "synthetic-private-key",
ExtraParams: map[string]any{"nested": []any{map[string]any{"value": "original"}}},
}},
Facts: domain.SmokeTestFacts{EndpointReachable: &reachable},
Timing: domain.SmokeTestTiming{CapacityWait: &wait},
Attempts: []domain.SmokeTestAttempt{{
Endpoint: "https://example.test/chat/completions",
HTTPStatus: &status,
Facts: domain.SmokeTestFacts{EndpointReachable: &reachable},
Timing: domain.SmokeTestTiming{RequestDuration: &duration},
}},
}
converted, err := fromDomainSmokeTestReport(source)
if err != nil {
t.Fatal(err)
}
other, err := fromDomainSmokeTestReport(source)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(converted)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(encoded), source.Target.EffectiveSettings.APIKey) {
t.Fatal("converted report included a credential")
}
*converted.Applicable = false
*converted.Facts.EndpointReachable = false
*converted.Timing.CapacityWait = time.Second
converted.Attempts[0].Endpoint = "changed"
*converted.Attempts[0].HTTPStatus = 503
*converted.Attempts[0].Facts.EndpointReachable = false
*converted.Attempts[0].Timing.RequestDuration = time.Second
converted.Target.EffectiveSettings.ExtraParams["nested"].([]any)[0].(map[string]any)["value"] = "changed"
if !applicable || !reachable || status != 200 || wait != 3*time.Millisecond || duration != 5*time.Millisecond {
t.Fatal("converted report shared optional scalar pointers with its source")
}
if source.Attempts[0].Endpoint != "https://example.test/chat/completions" {
t.Fatal("converted report shared its attempt slice with its source")
}
if got := source.Target.EffectiveSettings.ExtraParams["nested"].([]any)[0].(map[string]any)["value"]; got != "original" {
t.Fatalf("converted report changed source provider parameters: %v", got)
}
later, err := fromDomainSmokeTestReport(source)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(other, later) {
t.Fatal("converted report mutation affected another result")
}
}
func TestSmokeReportConversionPreservesProviderMapCopyError(t *testing.T) {
report, err := fromDomainSmokeTestReport(domain.SmokeTestReport{
Target: domain.SmokeTestTarget{EffectiveSettings: domain.ExecutionTarget{
ExtraParams: map[string]any{"unsupported": func() {}},
}},
})
if err == nil || report != nil {
t.Fatalf("invalid provider data produced report=%+v, error=%v", report, err)
}
}

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"gitea.maximumdirect.net/eric/promptkit/internal/domain" "gitea.maximumdirect.net/eric/promptkit/internal/domain"
"gitea.maximumdirect.net/eric/promptkit/internal/jsonvalue"
) )
// SmokeTestKind identifies one built-in diagnostic operation. // SmokeTestKind identifies one built-in diagnostic operation.
@@ -294,35 +295,35 @@ func toDomainSmokeTestRequest(request SmokeTestRequest) (domain.SmokeTestRequest
} }
func fromDomainSmokeTestReport(report domain.SmokeTestReport) (*SmokeTestReport, error) { func fromDomainSmokeTestReport(report domain.SmokeTestReport) (*SmokeTestReport, error) {
owned, err := domain.CloneSmokeTestReport(report) extraParams, err := jsonvalue.CopyMap(report.Target.EffectiveSettings.ExtraParams)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := &SmokeTestReport{ result := &SmokeTestReport{
Kind: SmokeTestKind(owned.Kind), Kind: SmokeTestKind(report.Kind),
Supported: owned.Supported, Supported: report.Supported,
Applicable: copySmokeTestBoolPtr(owned.Applicable), Applicable: copySmokeTestBoolPtr(report.Applicable),
Completed: owned.Completed, Completed: report.Completed,
Reason: SmokeTestReason(owned.Reason), Reason: SmokeTestReason(report.Reason),
Target: SmokeTestTarget{ Target: SmokeTestTarget{
BackendID: owned.Target.BackendID, BackendID: report.Target.BackendID,
ProfileID: owned.Target.ProfileID, ProfileID: report.Target.ProfileID,
PromptID: owned.Target.PromptID, PromptID: report.Target.PromptID,
PromptVersion: owned.Target.PromptVersion, PromptVersion: report.Target.PromptVersion,
DefaultSourceBackendID: owned.Target.DefaultSourceBackendID, DefaultSourceBackendID: report.Target.DefaultSourceBackendID,
SelectionSource: owned.Target.SelectionSource, SelectionSource: report.Target.SelectionSource,
Endpoint: owned.Target.Endpoint, Endpoint: report.Target.Endpoint,
EffectiveSettings: fromDomainExecutionTarget(owned.Target.EffectiveSettings), EffectiveSettings: fromDomainExecutionTargetWithOwnedParams(report.Target.EffectiveSettings, extraParams),
SettingsPresence: fromDomainExecutionTargetPresence(owned.Target.SettingsPresence), SettingsPresence: fromDomainExecutionTargetPresence(report.Target.SettingsPresence),
}, },
Facts: fromDomainSmokeTestFacts(owned.Facts), Facts: fromDomainSmokeTestFacts(report.Facts),
Timing: fromDomainSmokeTestTiming(owned.Timing), Timing: fromDomainSmokeTestTiming(report.Timing),
Usage: fromDomainObservedTokenUsage(owned.Usage), Usage: fromDomainObservedTokenUsage(report.Usage),
Failure: ExecutionFailureKind(owned.Failure), Failure: ExecutionFailureKind(report.Failure),
} }
if owned.Attempts != nil { if report.Attempts != nil {
result.Attempts = make([]SmokeTestAttempt, len(owned.Attempts)) result.Attempts = make([]SmokeTestAttempt, len(report.Attempts))
for index, attempt := range owned.Attempts { for index, attempt := range report.Attempts {
result.Attempts[index] = SmokeTestAttempt{ result.Attempts[index] = SmokeTestAttempt{
Dispatched: attempt.Dispatched, Dispatched: attempt.Dispatched,
InitialEndpoint: attempt.InitialEndpoint, InitialEndpoint: attempt.InitialEndpoint,