diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index aa92389..fa00d8d 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -6,7 +6,13 @@ Reviewed commit `e9a12d4` against all eight stages of ## 1. P2 — Inactive Runtime Schema Values Break Snapshot Ownership -Status: open. Relevant stages: 1 and 6. +Status: complete. Relevant stages: 1 and 6. + +Resolution: inactive runtime inline schemas are omitted from execution-owned +contracts, without validating them. Public regressions cover struct/map, cyclic, +and unencodable values across inactive modes, preserved legacy path metadata, +and independent prepared details. Configured inspection and active schemas retain +their existing ownership behavior. Locations: diff --git a/internal/usecase/runner.go b/internal/usecase/runner.go index 90e19dd..69e727a 100644 --- a/internal/usecase/runner.go +++ b/internal/usecase/runner.go @@ -692,6 +692,11 @@ func resolveOutputContract(ctx context.Context, def *domain.PromptDefinition, ov if contract.Format == "" { contract.Format = domain.FormatText } + if contract.ValidationMode != domain.ValidationJSONSchema { + // Inactive runtime values are not execution metadata. Dropping them + // avoids retaining arbitrary caller-owned Go objects. + contract.Schema = nil + } if contract.ValidationMode == domain.ValidationJSONSchema && contract.Schema != nil { value, err := validate.DecodeInlineSchema(ctx, contract.Schema) if err != nil { diff --git a/output_contract_contract_test.go b/output_contract_contract_test.go index 9cbb102..3e7b5d1 100644 --- a/output_contract_contract_test.go +++ b/output_contract_contract_test.go @@ -74,17 +74,33 @@ func TestPreparationRejectsInvalidOutputContractWithPublicError(t *testing.T) { } } -func TestInactiveSchemaDoesNotInterpretCyclicValue(t *testing.T) { +func TestInactiveRuntimeSchemaIsNotRetained(t *testing.T) { cycle := map[string]any{} cycle["self"] = cycle engine := newSimpleEngine(t, "message") - prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{PromptID: "prompt", Validation: &promptkit.OutputContract{Format: promptkit.FormatText, ValidationMode: promptkit.ValidationNone, Schema: cycle}}) - if err != nil { - t.Fatal(err) - } - copy := prepared.OutputContract.Schema.(map[string]any) - copy["self"].(map[string]any)["changed"] = true - if copy["changed"] != true || cycle["changed"] != nil { - t.Fatal("inactive schema copy shares caller storage") + type schemaValue struct{ Properties map[string]any } + original := schemaValue{Properties: map[string]any{"title": "original"}} + for _, mode := range []promptkit.ValidationMode{promptkit.ValidationNone, promptkit.ValidationBasic, promptkit.ValidationJSON} { + for _, value := range []any{original, cycle, make(chan int)} { + request := promptkit.RunRequest{PromptID: "prompt", Validation: &promptkit.OutputContract{Format: promptkit.FormatText, ValidationMode: mode, Schema: value, SchemaPath: "ignored.json"}} + prepared, err := engine.Prepare(context.Background(), request) + if err != nil || prepared.OutputContract.Schema != nil || prepared.OutputContract.SchemaPath != "ignored.json" { + t.Fatalf("prepare inactive %s schema: %+v, %v", mode, prepared, err) + } + handle, err := engine.PrepareExecution(context.Background(), request) + if err != nil { + t.Fatal(err) + } + original.Properties["title"] = "caller mutation" + details := handle.Details() + if details.OutputContract.Schema != nil { + t.Fatal("inactive schema retained in details") + } + details.OutputContract.Schema = map[string]any{"title": "details mutation"} + if handle.Details().OutputContract.Schema != nil { + t.Fatal("details mutation changed retained state") + } + handle.Discard() + } } } diff --git a/types.go b/types.go index 97f0477..e6dfc80 100644 --- a/types.go +++ b/types.go @@ -508,7 +508,8 @@ type OutputContract struct { ValidationMode ValidationMode `json:"validation_mode"` // Schema supplies an inline JSON Schema. Nil or a value encoding as JSON // null means no selection; false and an empty object are selections. In - // JSON Schema mode the value must be JSON-encodable. + // JSON Schema mode the value must be JSON-encodable. Runtime replacements + // in other modes ignore this value and omit it from prepared details. Schema any `json:"schema,omitempty"` // SchemaPath identifies a schema under the applicable configured root. SchemaPath string `json:"schema_path"`