Make verifier verdict coherence structural

This commit is contained in:
Meta Vens 2026-08-23 06:18:19 +02:00
commit 1d158184f5
2 changed files with 138 additions and 20 deletions

View file

@ -267,7 +267,7 @@ async fn verify_outcomes(
source_receipt_ids: &[String],
) -> Result<(ModelStageOutput, Vec<String>)> {
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<String>, field: &str) -> Result<String> {
@ -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(),

View file

@ -1158,13 +1158,55 @@ fn cell_effect_verifier_schema(action_count: usize) -> Result<serde_json::Value>
.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]