diff --git a/crates/ghostlight-dungeon/src/outcome.rs b/crates/ghostlight-dungeon/src/outcome.rs index f84d95b..d1174f8 100644 --- a/crates/ghostlight-dungeon/src/outcome.rs +++ b/crates/ghostlight-dungeon/src/outcome.rs @@ -267,7 +267,7 @@ async fn verify_outcomes( source_receipt_ids: &[String], ) -> Result<(ModelStageOutput, Vec)> { let mut schema = serde_json::to_value(schema_for!(OutcomeVerifierBundle))?; - constrain_verifier_schema(&mut schema, digests); + constrain_verifier_schema(&mut schema, digests)?; let outcome_hash = format!( "sha256:{:x}", Sha256::digest(rmp_serde::to_vec_named(outcomes)?) @@ -1484,14 +1484,44 @@ fn constrain_digest_schema(schema: &mut serde_json::Value, digests: &[String]) { } } -fn constrain_verifier_schema(schema: &mut serde_json::Value, digests: &[String]) { - if let Some(value) = schema.pointer_mut("/properties/verdicts/items/properties/action_digest") { - value["enum"] = serde_json::json!(digests); - } - if let Some(verdicts) = schema.pointer_mut("/properties/verdicts") { - verdicts["minItems"] = serde_json::json!(digests.len()); - verdicts["maxItems"] = serde_json::json!(digests.len()); - } +fn constrain_verifier_schema(schema: &mut serde_json::Value, digests: &[String]) -> Result<()> { + let verdicts = schema + .pointer_mut("/properties/verdicts") + .ok_or_else(|| anyhow!("strategic outcome verifier schema has no verdicts property"))?; + verdicts["minItems"] = serde_json::json!(digests.len()); + verdicts["maxItems"] = serde_json::json!(digests.len()); + let verdict = schema + .pointer_mut("/$defs/OutcomeVerifierVerdict") + .ok_or_else(|| anyhow!("strategic outcome verifier schema has no verdict definition"))?; + *verdict = serde_json::json!({ + "oneOf":[ + { + "type":"object", + "additionalProperties":false, + "required":["action_digest", "result", "repair_guidance"], + "properties":{ + "action_digest":{"enum":digests}, + "result":{"const":"match"}, + "repair_guidance":{"type":"null"} + } + }, + { + "type":"object", + "additionalProperties":false, + "required":["action_digest", "result", "repair_guidance"], + "properties":{ + "action_digest":{"enum":digests}, + "result":{"const":"mismatch"}, + "repair_guidance":{ + "type":"string", + "minLength":1, + "maxLength":240 + } + } + } + ] + }); + Ok(()) } fn required(value: &Option, field: &str) -> Result { @@ -1654,6 +1684,36 @@ mod tests { } } + #[test] + fn outcome_verifier_schema_makes_result_guidance_coherence_structural() { + let digest = format!("sha256:{}", "a".repeat(64)); + let mut schema = serde_json::to_value(schema_for!(OutcomeVerifierBundle)).unwrap(); + constrain_verifier_schema(&mut schema, std::slice::from_ref(&digest)).unwrap(); + let validator = jsonschema::validator_for(&schema).unwrap(); + + assert!(validator.is_valid(&serde_json::json!({ + "verdicts":[{ + "action_digest":digest, + "result":"match", + "repair_guidance":null + }] + }))); + assert!(!validator.is_valid(&serde_json::json!({ + "verdicts":[{ + "action_digest":digest, + "result":"match", + "repair_guidance":"This cannot accompany a match." + }] + }))); + assert!(!validator.is_valid(&serde_json::json!({ + "verdicts":[{ + "action_digest":digest, + "result":"mismatch", + "repair_guidance":null + }] + }))); + } + fn campaign() -> Campaign { let mut value = Campaign { schema: "ghostlight.campaign.v1".into(), diff --git a/crates/ghostlight-dungeon/src/persona.rs b/crates/ghostlight-dungeon/src/persona.rs index 563f20d..f4b74aa 100644 --- a/crates/ghostlight-dungeon/src/persona.rs +++ b/crates/ghostlight-dungeon/src/persona.rs @@ -1158,13 +1158,55 @@ fn cell_effect_verifier_schema(action_count: usize) -> Result .ok_or_else(|| anyhow!("cell effect verifier schema has no verdicts property"))?; verdicts.insert("minItems".into(), serde_json::json!(action_count)); verdicts.insert("maxItems".into(), serde_json::json!(action_count)); - let action_index = schema - .pointer_mut("/$defs/CellActionEffectVerdict/properties/action_index") - .ok_or_else(|| anyhow!("cell effect verifier schema has no action index property"))?; - *action_index = serde_json::json!({ - "type":"integer", - "minimum":0, - "maximum":action_count - 1 + let verdict = schema + .pointer_mut("/$defs/CellActionEffectVerdict") + .ok_or_else(|| anyhow!("cell effect verifier schema has no verdict definition"))?; + *verdict = serde_json::json!({ + "oneOf":[ + { + "type":"object", + "additionalProperties":false, + "required":["action_index", "result", "mismatch_kind", "repair_guidance"], + "properties":{ + "action_index":{ + "type":"integer", + "minimum":0, + "maximum":action_count - 1 + }, + "result":{"const":"match"}, + "mismatch_kind":{"type":"null"}, + "repair_guidance":{"type":"null"} + } + }, + { + "type":"object", + "additionalProperties":false, + "required":["action_index", "result", "mismatch_kind", "repair_guidance"], + "properties":{ + "action_index":{ + "type":"integer", + "minimum":0, + "maximum":action_count - 1 + }, + "result":{"const":"mismatch"}, + "mismatch_kind":{ + "enum":[ + "subject_swap", + "effect_omission", + "effect_reversal", + "target_substitution", + "invented_outcome", + "wrong_effect_kind" + ] + }, + "repair_guidance":{ + "type":"string", + "minLength":1, + "maxLength":240 + } + } + } + ] }); Ok(schema) } @@ -2270,10 +2312,26 @@ mod tests { schema.pointer("/properties/verdicts/maxItems"), Some(&serde_json::json!(4)) ); - assert_eq!( - schema.pointer("/$defs/CellActionEffectVerdict/properties/action_index/maximum"), - Some(&serde_json::json!(3)) - ); + let validator = jsonschema::validator_for(&schema).unwrap(); + let faithful = serde_json::json!({ + "verdicts":[ + {"action_index":0,"result":"match","mismatch_kind":null,"repair_guidance":null}, + {"action_index":1,"result":"mismatch","mismatch_kind":"target_substitution","repair_guidance":"Use the exact addressed subject."}, + {"action_index":2,"result":"match","mismatch_kind":null,"repair_guidance":null}, + {"action_index":3,"result":"match","mismatch_kind":null,"repair_guidance":null} + ] + }); + assert!(validator.is_valid(&faithful)); + + let incoherent = serde_json::json!({ + "verdicts":[ + {"action_index":0,"result":"match","mismatch_kind":"invented_outcome","repair_guidance":null}, + {"action_index":1,"result":"match","mismatch_kind":null,"repair_guidance":null}, + {"action_index":2,"result":"match","mismatch_kind":null,"repair_guidance":null}, + {"action_index":3,"result":"match","mismatch_kind":null,"repair_guidance":null} + ] + }); + assert!(!validator.is_valid(&incoherent)); } #[test]