Omit inactive runtime schemas from prepared snapshots

This commit is contained in:
2026-11-20 16:19:29 +00:00
parent 37582e9c30
commit 1bfb6eb301
4 changed files with 39 additions and 11 deletions

View File

@@ -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:

View File

@@ -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 {

View File

@@ -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()
}
}
}

View File

@@ -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"`