From f0dd0a33ab8d5d4c160125b50ea7c0ed6db56b77 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 13 Aug 2026 22:14:56 +0200 Subject: [PATCH] Make worker output contracts strict --- epiphany-core/src/agent_launch.rs | 365 +++++++++++++++-------- epiphany-openai-codex-spine/src/lib.rs | 148 +++++++++- epiphany-openai-runtime/src/lib.rs | 381 +++++++++++++++++++------ 3 files changed, 675 insertions(+), 219 deletions(-) diff --git a/epiphany-core/src/agent_launch.rs b/epiphany-core/src/agent_launch.rs index 07b3274d..179a8e02 100644 --- a/epiphany-core/src/agent_launch.rs +++ b/epiphany-core/src/agent_launch.rs @@ -1,7 +1,5 @@ use std::sync::OnceLock; -use crate::build_reorient_job_launch_request; -use crate::default_launch_organ_contract; use crate::EpiphanyCoordinatorAction as CoreEpiphanyCoordinatorAction; use crate::EpiphanyCoordinatorRoleResultStatus as CoreEpiphanyCoordinatorRoleResultStatus; use crate::EpiphanyCrrcAction as CoreEpiphanyCrrcAction; @@ -15,6 +13,8 @@ use crate::EpiphanyReorientLaunchRequestInput; use crate::EpiphanyRoleResultRoleId; use crate::EpiphanyRoleWorkerLaunchDocument; use crate::EpiphanyWorkerLaunchDocument; +use crate::build_reorient_job_launch_request; +use crate::default_launch_organ_contract; use epiphany_state_model::EpiphanyInvestigationCheckpoint; use epiphany_state_model::EpiphanyJobKind as CoreEpiphanyJobKind; use epiphany_state_model::EpiphanyThreadState; @@ -116,6 +116,55 @@ fn modeling_imagination_frontier_output_schema() -> serde_json::Value { schema } +fn adopted_frontier_plan_output_schema() -> serde_json::Value { + serde_json::json!({ + "type": "object", + "required": [ + "planning_request_id", "result_id", "job_id", "candidate_id", "candidate_sha256", + "safe_paths", "action", "command", "checks", "stop_conditions", "rollback_steps", + "commit_message" + ], + "properties": { + "planning_request_id": {"type": "string", "minLength": 1}, + "result_id": {"type": "string", "minLength": 1}, + "job_id": {"type": "string", "minLength": 1}, + "candidate_id": {"type": "string", "minLength": 1}, + "candidate_sha256": {"type": "string", "minLength": 1}, + "safe_paths": {"type": "array", "items": {"type": "string", "minLength": 1}}, + "action": {"type": "string", "minLength": 1}, + "command": {"type": "string", "minLength": 1}, + "checks": {"type": "array", "items": {"type": "string", "minLength": 1}}, + "stop_conditions": {"type": "array", "items": {"type": "string", "minLength": 1}}, + "rollback_steps": {"type": "array", "items": {"type": "string", "minLength": 1}}, + "commit_message": {"type": "string", "minLength": 1}, + "execution_amendment": { + "type": "object", + "required": [ + "amendment_id", "replaces_route_id", "source_actor_id", "command_id", + "admission_id", "packet_sha256", "previous_action_sha256", + "previous_command_sha256", "action", "command", "rationale", "amended_at" + ], + "properties": { + "amendment_id": {"type": "string", "minLength": 1}, + "replaces_route_id": {"type": "string", "minLength": 1}, + "source_actor_id": {"type": "string", "minLength": 1}, + "command_id": {"type": "string", "minLength": 1}, + "admission_id": {"type": "string", "minLength": 1}, + "packet_sha256": {"type": "string", "minLength": 1}, + "previous_action_sha256": {"type": "string", "minLength": 1}, + "previous_command_sha256": {"type": "string", "minLength": 1}, + "action": {"type": "string", "minLength": 1}, + "command": {"type": "string", "minLength": 1}, + "rationale": {"type": "string", "minLength": 1}, + "amended_at": {"type": "string", "minLength": 1} + }, + "additionalProperties": false + } + }, + "additionalProperties": false + }) +} + fn repo_model_node_output_schema() -> serde_json::Value { serde_json::json!({ "type": "object", @@ -227,6 +276,81 @@ fn evidence_output_schema() -> serde_json::Value { }) } +fn scratch_output_schema() -> serde_json::Value { + serde_json::json!({ + "type": "object", + "required": ["summary"], + "properties": { + "summary": {"type": "string", "minLength": 1}, + "hypothesis": {"type": "string"}, + "next_probe": {"type": "string"}, + "notes": {"type": "array", "items": {"type": "string"}} + }, + "additionalProperties": false + }) +} + +fn investigation_checkpoint_output_schema() -> serde_json::Value { + serde_json::json!({ + "type": "object", + "description": "Outer statePatch uses camelCase; this typed checkpoint payload uses its canonical snake_case field names.", + "required": ["checkpoint_id", "kind", "disposition", "focus"], + "properties": { + "checkpoint_id": {"type": "string", "minLength": 1}, + "kind": {"type": "string", "minLength": 1}, + "disposition": {"type": "string", "enum": ["resume_ready", "regather_required"]}, + "focus": {"type": "string", "minLength": 1}, + "summary": {"type": "string"}, + "next_action": {"type": "string"}, + "captured_at_turn_id": {"type": "string"}, + "open_questions": {"type": "array", "items": {"type": "string"}}, + "code_refs": {"type": "array", "items": code_ref_output_schema()}, + "evidence_ids": {"type": "array", "items": {"type": "string"}} + }, + "additionalProperties": false + }) +} + +fn objective_draft_output_schema() -> serde_json::Value { + serde_json::json!({ + "type": "object", + "required": ["id", "title", "summary", "scope", "acceptance_criteria", "lane_plan", "status"], + "properties": { + "id": {"type": "string", "minLength": 1}, + "title": {"type": "string", "minLength": 1}, + "summary": {"type": "string", "minLength": 1}, + "source_item_ids": {"type": "array", "items": {"type": "string"}}, + "scope": { + "type": "object", + "properties": { + "includes": {"type": "array", "items": {"type": "string"}}, + "excludes": {"type": "array", "items": {"type": "string"}} + }, + "additionalProperties": false + }, + "acceptance_criteria": {"type": "array", "minItems": 1, "items": {"type": "string", "minLength": 1}}, + "evidence_required": {"type": "array", "items": {"type": "string"}}, + "lane_plan": { + "type": "object", + "properties": { + "imagination": {"type": "string"}, + "eyes": {"type": "string"}, + "body": {"type": "string"}, + "hands": {"type": "string"}, + "soul": {"type": "string"}, + "life": {"type": "string"} + }, + "additionalProperties": false + }, + "dependencies": {"type": "array", "items": {"type": "string"}}, + "risks": {"type": "array", "items": {"type": "string"}}, + "review_gates": {"type": "array", "items": {"type": "string"}}, + "status": {"type": "string", "enum": ["draft"]} + }, + "additionalProperties": false + }) +} + fn self_patch_memory_output_schema() -> serde_json::Value { serde_json::json!({ "type": "object", @@ -416,23 +540,13 @@ pub fn epiphany_role_launch_output_schema(role_id: EpiphanyRoleResultRoleId) -> "objective_drafts": { "type": "array", "minItems": 1, - "items": { - "type": "object", - "required": ["id", "title", "summary", "acceptance_criteria", "status"], - "properties": { - "status": { - "type": "string", - "enum": ["draft"] - } - }, - "additionalProperties": true - } + "items": objective_draft_output_schema() } }, - "additionalProperties": true + "additionalProperties": false } }, - "additionalProperties": true + "additionalProperties": false }), ); } @@ -449,53 +563,21 @@ pub fn epiphany_role_launch_output_schema(role_id: EpiphanyRoleResultRoleId) -> "observations": { "type": "array", "minItems": 1, - "items": { - "type": "object", - "required": ["id", "summary", "source_kind", "status", "evidence_ids"], - "additionalProperties": true - } + "items": observation_output_schema() }, "evidence": { "type": "array", "minItems": 1, - "items": { - "type": "object", - "required": ["id", "kind", "status", "summary"], - "additionalProperties": true - } + "items": evidence_output_schema() }, - "scratch": { - "type": "object", - "required": ["summary"], - "properties": { - "summary": {"type": "string", "minLength": 1} - }, - "additionalProperties": true - }, - "investigationCheckpoint": { - "type": "object", - "description": "Outer statePatch uses camelCase; this typed checkpoint payload uses its canonical snake_case field names.", - "required": ["checkpoint_id", "kind", "disposition", "focus"], - "properties": { - "checkpoint_id": {"type": "string", "minLength": 1}, - "kind": {"type": "string", "minLength": 1}, - "disposition": {"type": "string", "enum": ["resume_ready", "regather_required"]}, - "focus": {"type": "string", "minLength": 1}, - "summary": {"type": "string"}, - "next_action": {"type": "string"}, - "captured_at_turn_id": {"type": "string"}, - "open_questions": {"type": "array", "items": {"type": "string"}}, - "code_refs": {"type": "array", "items": {"type": "object"}}, - "evidence_ids": {"type": "array", "items": {"type": "string"}} - }, - "additionalProperties": false - } + "scratch": scratch_output_schema(), + "investigationCheckpoint": investigation_checkpoint_output_schema() }, "anyOf": [ {"required": ["scratch"]}, {"required": ["investigationCheckpoint"]} ], - "additionalProperties": true + "additionalProperties": false }), ); } @@ -591,7 +673,7 @@ pub fn epiphany_role_launch_output_schema(role_id: EpiphanyRoleResultRoleId) -> {"type": "object", "required": ["operation", "item"], "properties": {"operation": {"const": "upsert_frontier"}, "item": repo_frontier_item_output_schema()}}, {"type": "object", "required": ["operation", "item"], "properties": {"operation": {"const": "revise_frontier"}, "item": repo_frontier_item_output_schema()}}, {"type": "object", "required": ["operation", "item_id"], "properties": {"operation": {"const": "retire_frontier"}, "item_id": {"type": "string", "minLength": 1}, "retired_at": {"type": "string"}, "superseded_by": {"type": "string"}}}, - {"type": "object", "required": ["operation", "frontier_item_id", "expected_frontier_item_hash", "adopted_plan"], "properties": {"operation": {"const": "adopt_frontier_plan"}, "frontier_item_id": {"type": "string", "minLength": 1}, "expected_frontier_item_hash": {"type": "string", "minLength": 1}, "adopted_plan": {"type": "object"}}} + {"type": "object", "required": ["operation", "frontier_item_id", "expected_frontier_item_hash", "adopted_plan"], "properties": {"operation": {"const": "adopt_frontier_plan"}, "frontier_item_id": {"type": "string", "minLength": 1}, "expected_frontier_item_hash": {"type": "string", "minLength": 1}, "adopted_plan": adopted_frontier_plan_output_schema()}} ] } } @@ -1699,8 +1781,8 @@ mod tests { fn admitted_direction_schema_bounds_autonomous_proposal_fanout() { let schema = epiphany_admitted_model_direction_consideration_output_schema(); assert_eq!( - schema["properties"]["admittedModelDirectionConsiderationResult"]["properties"] - ["option_drafts"]["maxItems"], + schema["properties"]["admittedModelDirectionConsiderationResult"]["properties"]["option_drafts"] + ["maxItems"], crate::admitted_model_direction_consideration::MAX_OPTION_DRAFTS ); } @@ -1786,9 +1868,11 @@ mod tests { .expect("Mind launch request"); assert_eq!(request.owner_role, EPIPHANY_MIND_OWNER_ROLE); assert_eq!(request.binding_id, EPIPHANY_MIND_ROLE_BINDING_ID); - assert!(request - .instruction - .contains("admission-review procedure serving Epiphany Mind")); + assert!( + request + .instruction + .contains("admission-review procedure serving Epiphany Mind") + ); assert_eq!( request.frontier_plan_mind_request_id.as_deref(), Some("mind-request-1") @@ -1811,22 +1895,26 @@ mod tests { #[test] fn modeling_schema_exposes_only_typed_authority_purposes() { let schema = epiphany_role_launch_output_schema(EpiphanyRoleResultRoleId::Modeling); - assert!(schema["required"] - .as_array() - .unwrap() - .iter() - .any(|value| value == "repositoryBodyObservationBasis")); + assert!( + schema["required"] + .as_array() + .unwrap() + .iter() + .any(|value| value == "repositoryBodyObservationBasis") + ); assert_eq!( schema["properties"]["repositoryBodyObservationBasis"]["additionalProperties"], false ); - let frontier_item = &schema["properties"]["repoModelPatch"]["properties"]["operations"] - ["items"]["anyOf"][6]["properties"]["item"]; - assert!(frontier_item["required"] - .as_array() - .unwrap() - .iter() - .any(|value| value == "target_claim_ids")); + let frontier_item = &schema["properties"]["repoModelPatch"]["properties"]["operations"]["items"] + ["anyOf"][6]["properties"]["item"]; + assert!( + frontier_item["required"] + .as_array() + .unwrap() + .iter() + .any(|value| value == "target_claim_ids") + ); assert_eq!( frontier_item["properties"]["target_claim_ids"]["minItems"], 1 @@ -1840,11 +1928,13 @@ mod tests { purposes[1]["properties"]["kind"]["const"], "incorporate_frontier_verdict" ); - assert!(purposes[1]["required"] - .as_array() - .unwrap() - .iter() - .any(|value| value == "route_id")); + assert!( + purposes[1]["required"] + .as_array() + .unwrap() + .iter() + .any(|value| value == "route_id") + ); assert_eq!( schema["allOf"][0]["then"]["required"][0], "repoFrontierModelingRequestId" @@ -1863,10 +1953,11 @@ mod tests { ["maxContains"], 1 ); - let future_gap_operations = schema["allOf"][3]["then"]["properties"]["repoModelPatch"] - ["properties"]["operations"]["items"]["anyOf"] - .as_array() - .expect("future-gap Modeling operations"); + let future_gap_operations = + schema["allOf"][3]["then"]["properties"]["repoModelPatch"]["properties"]["operations"] + ["items"]["anyOf"] + .as_array() + .expect("future-gap Modeling operations"); assert_eq!(future_gap_operations.len(), 7); let future_gap_frontier = &future_gap_operations[6]["properties"]["item"]; assert_eq!( @@ -1891,20 +1982,21 @@ mod tests { ["maxContains"], 1 ); - let no_future_gap_operations = schema["allOf"][4]["then"]["properties"]["repoModelPatch"] - ["properties"]["operations"]["items"]["anyOf"] - .as_array() - .expect("ordinary Modeling operations without future-gap authority"); + let no_future_gap_operations = + schema["allOf"][4]["then"]["properties"]["repoModelPatch"]["properties"]["operations"] + ["items"]["anyOf"] + .as_array() + .expect("ordinary Modeling operations without future-gap authority"); assert_eq!(no_future_gap_operations.len(), 6); assert!(no_future_gap_operations.iter().all(|operation| { !operation["properties"]["operation"]["const"] .as_str() .is_some_and(|kind| kind.contains("frontier")) })); - let operations = schema["properties"]["repoModelPatch"]["properties"]["operations"] - ["items"]["anyOf"] - .as_array() - .expect("typed Modeling operations"); + let operations = + schema["properties"]["repoModelPatch"]["properties"]["operations"]["items"]["anyOf"] + .as_array() + .expect("typed Modeling operations"); let node_kinds = operations[0]["properties"]["node"]["properties"]["kind"]["enum"] .as_array() .expect("typed RepoModel node kinds"); @@ -1958,14 +2050,15 @@ mod tests { schema["properties"]["repoFrontierModelingRequestId"]["const"], "modeling-request-exact" ); - assert!(schema["required"] - .as_array() - .unwrap() - .iter() - .any(|value| value == "repoFrontierModelingRequestId")); + assert!( + schema["required"] + .as_array() + .unwrap() + .iter() + .any(|value| value == "repoFrontierModelingRequestId") + ); assert_eq!( - schema["properties"]["repoModelPatch"]["properties"]["purpose"]["properties"]["kind"] - ["const"], + schema["properties"]["repoModelPatch"]["properties"]["purpose"]["properties"]["kind"]["const"], "incorporate_frontier_verdict" ); let operations = &schema["properties"]["repoModelPatch"]["properties"]["operations"]; @@ -1976,19 +2069,21 @@ mod tests { "revise_frontier" ); assert_eq!(schema["properties"]["evidenceIds"]["minItems"], 1); - let item = &schema["properties"]["repoModelPatch"]["properties"]["operations"]["items"] - ["properties"]["item"]; + let item = &schema["properties"]["repoModelPatch"]["properties"]["operations"]["items"]["properties"] + ["item"]; assert_eq!(item["properties"]["id"]["const"], "frontier-exact"); assert_eq!(item["properties"]["status"]["const"], "resolved"); assert_eq!( item["properties"]["adopted_plan"]["const"]["command"], "cargo test" ); - assert!(item["required"] - .as_array() - .unwrap() - .iter() - .any(|field| field == "adopted_plan")); + assert!( + item["required"] + .as_array() + .unwrap() + .iter() + .any(|field| field == "adopted_plan") + ); } #[test] @@ -2031,12 +2126,29 @@ mod tests { ); } + #[test] + fn research_schema_reuses_complete_typed_evidence_shapes() { + let schema = epiphany_role_launch_output_schema(EpiphanyRoleResultRoleId::Research); + let patch = &schema["properties"]["statePatch"]; + let observation = &patch["properties"]["observations"]["items"]; + let evidence = &patch["properties"]["evidence"]["items"]; + + assert_eq!(patch["additionalProperties"], false); + assert_eq!(observation, &observation_output_schema()); + assert_eq!(evidence, &evidence_output_schema()); + assert!(observation["required"] + .as_array() + .is_some_and(|required| required.iter().any(|field| field == "source_kind"))); + } + #[test] fn frontier_planning_schema_exposes_candidate_without_generic_patch_mouths() { let schema = epiphany_frontier_planning_output_schema(); - assert!(schema["properties"] - .get("frontierPlanningRequestId") - .is_some()); + assert!( + schema["properties"] + .get("frontierPlanningRequestId") + .is_some() + ); assert!(schema["properties"].get("frontierPlanCandidate").is_some()); assert!(schema["properties"].get("statePatch").is_none()); assert!(schema["properties"].get("selfPatch").is_none()); @@ -2053,12 +2165,16 @@ mod tests { #[test] fn consideration_schema_has_only_proposal_candidate_cargo() { let schema = epiphany_imagination_consideration_output_schema(); - assert!(schema["properties"] - .get("imaginationConsiderationRequestId") - .is_none()); - assert!(schema["properties"] - .get("imaginationConsiderationCandidate") - .is_some()); + assert!( + schema["properties"] + .get("imaginationConsiderationRequestId") + .is_none() + ); + assert!( + schema["properties"] + .get("imaginationConsiderationCandidate") + .is_some() + ); for runtime_owned in [ "request_id", "feedback_id", @@ -2072,9 +2188,11 @@ mod tests { "proposed_at", "contract", ] { - assert!(schema["properties"]["imaginationConsiderationCandidate"]["properties"] - .get(runtime_owned) - .is_none()); + assert!( + schema["properties"]["imaginationConsiderationCandidate"]["properties"] + .get(runtime_owned) + .is_none() + ); } for forbidden in [ "statePatch", @@ -2089,9 +2207,8 @@ mod tests { #[test] fn proposal_modeling_schema_exposes_only_strict_semantic_frontier_draft() { - let schema = epiphany_proposal_modeling_output_schema( - crate::RepoFrontierProposalSourceKind::User, - ); + let schema = + epiphany_proposal_modeling_output_schema(crate::RepoFrontierProposalSourceKind::User); assert_eq!(schema["additionalProperties"], false); assert!(schema["properties"].get("proposalFrontierDraft").is_some()); for runtime_owned in [ @@ -2105,15 +2222,19 @@ mod tests { } let properties = schema["properties"].as_object().expect("root properties"); let required = schema["required"].as_array().expect("root required"); - assert!(properties - .keys() - .all(|key| required.iter().any(|item| item.as_str() == Some(key)))); + assert!( + properties + .keys() + .all(|key| required.iter().any(|item| item.as_str() == Some(key))) + ); let draft = &schema["properties"]["proposalFrontierDraft"]; let draft_properties = draft["properties"].as_object().expect("draft properties"); let draft_required = draft["required"].as_array().expect("draft required"); - assert!(draft_properties - .keys() - .all(|key| draft_required.iter().any(|item| item.as_str() == Some(key)))); + assert!( + draft_properties + .keys() + .all(|key| draft_required.iter().any(|item| item.as_str() == Some(key))) + ); fn every_const_has_a_type(value: &serde_json::Value) -> bool { match value { serde_json::Value::Object(map) => { diff --git a/epiphany-openai-codex-spine/src/lib.rs b/epiphany-openai-codex-spine/src/lib.rs index 34c7ff0e..d6c44ac6 100644 --- a/epiphany-openai-codex-spine/src/lib.rs +++ b/epiphany-openai-codex-spine/src/lib.rs @@ -264,8 +264,7 @@ fn responses_text_format( }; let mut schema: serde_json::Value = serde_json::from_str(schema_json).context("output_schema_json is not valid JSON Schema")?; - lower_schema_for_responses_format(&mut schema); - let strict = responses_schema_is_strict(&schema); + project_strict_responses_schema(&mut schema)?; let raw_name = output_contract_id.unwrap_or(request_id); let mut name = raw_name .chars() @@ -285,7 +284,7 @@ fn responses_text_format( "format": { "type": "json_schema", "name": name, - "strict": strict, + "strict": true, "schema": schema } }))) @@ -298,15 +297,11 @@ fn responses_schema_is_strict(schema: &serde_json::Value) -> bool { if map.get("additionalProperties") != Some(&serde_json::Value::Bool(false)) { return false; } - let Some(properties) = map - .get("properties") - .and_then(serde_json::Value::as_object) + let Some(properties) = map.get("properties").and_then(serde_json::Value::as_object) else { return false; }; - let Some(required) = map - .get("required") - .and_then(serde_json::Value::as_array) + let Some(required) = map.get("required").and_then(serde_json::Value::as_array) else { return false; }; @@ -324,13 +319,131 @@ fn responses_schema_is_strict(schema: &serde_json::Value) -> bool { } } +fn project_strict_responses_schema(schema: &mut serde_json::Value) -> Result<()> { + lower_schema_for_responses_format(schema); + require_closed_responses_objects(schema, "$")?; + if !responses_schema_is_strict(schema) { + return Err(anyhow::anyhow!( + "projected Responses output schema is not strict" + )); + } + Ok(()) +} + +fn require_closed_responses_objects(schema: &mut serde_json::Value, path: &str) -> Result<()> { + match schema { + serde_json::Value::Object(map) => { + let describes_object = map.get("type").and_then(serde_json::Value::as_str) + == Some("object") + || map.contains_key("properties"); + if describes_object { + map.insert( + "type".to_string(), + serde_json::Value::String("object".to_string()), + ); + let properties = map + .get("properties") + .and_then(serde_json::Value::as_object) + .cloned() + .unwrap_or_default(); + let canonical_required = map + .get("required") + .and_then(serde_json::Value::as_array) + .cloned() + .unwrap_or_default(); + for required in &canonical_required { + let required = required.as_str().ok_or_else(|| { + anyhow::anyhow!("Responses schema {path} has a non-string required key") + })?; + if !properties.contains_key(required) { + return Err(anyhow::anyhow!( + "Responses schema {path} requires undeclared property {required:?}" + )); + } + } + + let canonical_required = canonical_required + .into_iter() + .filter_map(|value| value.as_str().map(ToString::to_string)) + .collect::>(); + let mut projected = serde_json::Map::new(); + for (name, mut property) in properties { + let property_path = format!("{path}.properties.{name}"); + require_closed_responses_objects(&mut property, &property_path)?; + if !canonical_required.contains(&name) { + property = nullable_responses_property(property); + } + projected.insert(name, property); + } + let required = projected + .keys() + .cloned() + .map(serde_json::Value::String) + .collect(); + map.insert( + "properties".to_string(), + serde_json::Value::Object(projected), + ); + map.insert("required".to_string(), serde_json::Value::Array(required)); + map.insert( + "additionalProperties".to_string(), + serde_json::Value::Bool(false), + ); + } + + for (name, value) in map.iter_mut() { + if name == "properties" && describes_object { + continue; + } + require_closed_responses_objects(value, &format!("{path}.{name}"))?; + } + Ok(()) + } + serde_json::Value::Array(values) => { + for (index, value) in values.iter_mut().enumerate() { + require_closed_responses_objects(value, &format!("{path}[{index}]"))?; + } + Ok(()) + } + _ => Ok(()), + } +} + +fn nullable_responses_property(property: serde_json::Value) -> serde_json::Value { + if property + .get("anyOf") + .and_then(serde_json::Value::as_array) + .is_some_and(|variants| { + variants.iter().any(|variant| { + variant.get("type").and_then(serde_json::Value::as_str) == Some("null") + }) + }) + { + return property; + } + serde_json::json!({ + "anyOf": [property, {"type": "null"}] + }) +} + fn lower_schema_for_responses_format(schema: &mut serde_json::Value) { match schema { serde_json::Value::Object(map) => { // Responses structured output accepts only a JSON-Schema subset. // Canonical conditional authority remains enforced by Epiphany ingress // and Mind admission; this projection owns formatting only. - for unsupported in ["allOf", "if", "then", "else"] { + for unsupported in [ + "allOf", + "not", + "dependentRequired", + "dependentSchemas", + "if", + "then", + "else", + "contains", + "minContains", + "maxContains", + ] { map.remove(unsupported); } if let Some(one_of) = map.remove("oneOf") { @@ -352,14 +465,16 @@ fn lower_schema_for_responses_format(schema: &mut serde_json::Value) { fn openai_tool_from_epiphany_tool( tool: epiphany_openai_adapter::EpiphanyOpenAiToolDefinition, ) -> Result { - let parameters: serde_json::Value = serde_json::from_str(&tool.parameters_json) + let mut parameters: serde_json::Value = serde_json::from_str(&tool.parameters_json) .with_context(|| format!("tool {} parameters_json is not valid JSON", tool.name))?; + project_strict_responses_schema(&mut parameters) + .with_context(|| format!("tool {} parameters cannot be projected strictly", tool.name))?; Ok(serde_json::json!({ "type": "function", "name": tool.name, "description": tool.description, "parameters": parameters, - "strict": false + "strict": true })) } @@ -953,6 +1068,11 @@ mod tests { let responses = responses_body_from_epiphany(request).expect("request should map"); assert_eq!(responses["tool_choice"], "required"); + assert_eq!(responses["tools"][0]["strict"], true); + assert_eq!( + responses["tools"][0]["parameters"]["additionalProperties"], + false + ); } #[test] @@ -1015,10 +1135,12 @@ mod tests { let responses = responses_body_from_epiphany(request).expect("request should map"); let schema = &responses["text"]["format"]["schema"]; - assert_eq!(responses["text"]["format"]["strict"], false); + assert_eq!(responses["text"]["format"]["strict"], true); assert!(schema.get("allOf").is_none()); assert!(schema["properties"]["purpose"].get("oneOf").is_none()); assert!(schema["properties"]["purpose"]["anyOf"].is_array()); + assert_eq!(schema["additionalProperties"], false); + assert_eq!(schema["required"], serde_json::json!(["purpose"])); } #[test] diff --git a/epiphany-openai-runtime/src/lib.rs b/epiphany-openai-runtime/src/lib.rs index fcbc960f..91f0859f 100644 --- a/epiphany-openai-runtime/src/lib.rs +++ b/epiphany-openai-runtime/src/lib.rs @@ -1,20 +1,10 @@ use std::path::Path; use std::path::PathBuf; -use anyhow::anyhow; use anyhow::Context; use anyhow::Result; +use anyhow::anyhow; use chrono::SecondsFormat; -use epiphany_core::append_runtime_event; -use epiphany_core::complete_runtime_job; -use epiphany_core::close_runtime_session; -use epiphany_core::initialize_runtime_spine; -use epiphany_core::put_runtime_reorient_worker_result; -use epiphany_core::put_runtime_role_worker_result; -use epiphany_core::put_runtime_tool_execution_intent; -use epiphany_core::open_runtime_model_execution; -use epiphany_core::runtime_spine_cache; -use epiphany_core::runtime_spine_status; use epiphany_core::EpiphanyRuntimeReorientWorkerResult; use epiphany_core::EpiphanyRuntimeRoleWorkerResult; use epiphany_core::EpiphanyRuntimeWorkerLaunchRequest; @@ -23,8 +13,18 @@ use epiphany_core::RuntimeSpineEventOptions; use epiphany_core::RuntimeSpineInitOptions; use epiphany_core::RuntimeSpineJobOptions; use epiphany_core::RuntimeSpineJobResultOptions; -use epiphany_core::RuntimeSpineSessionOptions; use epiphany_core::RuntimeSpineSessionClosureOptions; +use epiphany_core::RuntimeSpineSessionOptions; +use epiphany_core::append_runtime_event; +use epiphany_core::close_runtime_session; +use epiphany_core::complete_runtime_job; +use epiphany_core::initialize_runtime_spine; +use epiphany_core::open_runtime_model_execution; +use epiphany_core::put_runtime_reorient_worker_result; +use epiphany_core::put_runtime_role_worker_result; +use epiphany_core::put_runtime_tool_execution_intent; +use epiphany_core::runtime_spine_cache; +use epiphany_core::runtime_spine_status; use epiphany_model_adapter::EpiphanyModelInputItem; use epiphany_model_adapter::EpiphanyModelReceipt; use epiphany_model_adapter::EpiphanyModelRequest; @@ -37,16 +37,20 @@ use epiphany_openai_adapter::EpiphanyOpenAiModelRequest; use epiphany_openai_adapter::EpiphanyOpenAiStreamEvent; use epiphany_openai_adapter::EpiphanyOpenAiStreamPayload; use epiphany_openai_adapter::EpiphanyOpenAiToolDefinition; +use epiphany_openai_codex_spine::EpiphanyCodexOpenAiTransport; +use epiphany_openai_codex_spine::EpiphanyResponsesFrameObservation; use epiphany_openai_codex_spine::auth_manager; pub use epiphany_openai_codex_spine::default_codex_home; use epiphany_openai_codex_spine::status_from_auth_manager; -use epiphany_openai_codex_spine::EpiphanyCodexOpenAiTransport; -use epiphany_openai_codex_spine::EpiphanyResponsesFrameObservation; -use epiphany_tool_adapter::tool_invocation_receipt_key; +use epiphany_tool_adapter::EPIPHANY_TOOL_RUNTIME_ADAPTER_ID; use epiphany_tool_adapter::EpiphanyToolInvocationIntent; use epiphany_tool_adapter::EpiphanyToolInvocationReceipt; -use epiphany_tool_adapter::EPIPHANY_TOOL_RUNTIME_ADAPTER_ID; +use epiphany_tool_adapter::tool_invocation_receipt_key; +use serde::Deserialize; +use serde::de::MapAccess; +use serde::de::SeqAccess; use serde::de::DeserializeOwned; +use serde::de::Visitor; use sha2::Digest; mod persona_executor; @@ -1691,7 +1695,9 @@ fn role_worker_result_from_ingress( launch_request: &EpiphanyRuntimeWorkerLaunchRequest, role_id: &str, repository_body_observation_basis: Option<&epiphany_core::RepositoryBodyObservationBasis>, - proposal_modeling_context: Option<&epiphany_core::RepoFrontierProposalModelingContextProjection>, + proposal_modeling_context: Option< + &epiphany_core::RepoFrontierProposalModelingContextProjection, + >, frontier_plan_mind_context: Option<&epiphany_core::RepoFrontierPlanMindContextProjection>, imagination_consideration_context: Option< &epiphany_core::ImaginationConsiderationContextProjection, @@ -1916,7 +1922,8 @@ fn role_worker_result_from_ingress( uncertainties: clean_string_vec(&ingress.uncertainties), evidence_refs: clean_string_vec(&ingress.evidence_refs), proposed_at: completed_at.into(), - contract: epiphany_core::ADMITTED_MODEL_DIRECTION_CONSIDERATION_RESULT_CONTRACT.into(), + contract: epiphany_core::ADMITTED_MODEL_DIRECTION_CONSIDERATION_RESULT_CONTRACT + .into(), proposal_only: true, terminal: true, }), @@ -2144,7 +2151,113 @@ where }) .unwrap_or(trimmed) .trim(); - serde_json::from_str(candidate).context("assistant text was not typed worker-result JSON") + let mut value = serde_json::from_str::(candidate) + .context("assistant text was not typed worker-result JSON")? + .0; + remove_provider_optional_nulls(&mut value); + serde_json::from_value(value).context("assistant text was not typed worker-result JSON") +} + +struct UniqueJsonValue(serde_json::Value); + +impl<'de> Deserialize<'de> for UniqueJsonValue { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + deserializer.deserialize_any(UniqueJsonValueVisitor) + } +} + +struct UniqueJsonValueVisitor; + +impl<'de> Visitor<'de> for UniqueJsonValueVisitor { + type Value = UniqueJsonValue; + + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("JSON without duplicate object keys") + } + + fn visit_bool(self, value: bool) -> Result { + Ok(UniqueJsonValue(serde_json::Value::Bool(value))) + } + + fn visit_i64(self, value: i64) -> Result { + Ok(UniqueJsonValue(serde_json::Value::Number(value.into()))) + } + + fn visit_u64(self, value: u64) -> Result { + Ok(UniqueJsonValue(serde_json::Value::Number(value.into()))) + } + + fn visit_f64(self, value: f64) -> Result + where + E: serde::de::Error, + { + serde_json::Number::from_f64(value) + .map(serde_json::Value::Number) + .map(UniqueJsonValue) + .ok_or_else(|| E::custom("JSON number is not finite")) + } + + fn visit_str(self, value: &str) -> Result { + Ok(UniqueJsonValue(serde_json::Value::String(value.to_string()))) + } + + fn visit_string(self, value: String) -> Result { + Ok(UniqueJsonValue(serde_json::Value::String(value))) + } + + fn visit_none(self) -> Result { + Ok(UniqueJsonValue(serde_json::Value::Null)) + } + + fn visit_unit(self) -> Result { + Ok(UniqueJsonValue(serde_json::Value::Null)) + } + + fn visit_seq(self, mut sequence: A) -> Result + where + A: SeqAccess<'de>, + { + let mut values = Vec::new(); + while let Some(value) = sequence.next_element::()? { + values.push(value.0); + } + Ok(UniqueJsonValue(serde_json::Value::Array(values))) + } + + fn visit_map(self, mut object: A) -> Result + where + A: MapAccess<'de>, + { + let mut values = serde_json::Map::new(); + while let Some((key, value)) = object.next_entry::()? { + if values.insert(key.clone(), value.0).is_some() { + return Err(serde::de::Error::custom(format!( + "duplicate field `{key}`" + ))); + } + } + Ok(UniqueJsonValue(serde_json::Value::Object(values))) + } +} + +fn remove_provider_optional_nulls(value: &mut serde_json::Value) { + match value { + serde_json::Value::Object(map) => { + map.retain(|_, value| !value.is_null()); + for value in map.values_mut() { + remove_provider_optional_nulls(value); + } + } + serde_json::Value::Array(values) => { + for value in values { + remove_provider_optional_nulls(value); + } + } + _ => {} + } } fn clean_optional_string(value: Option<&str>) -> Option { @@ -2183,10 +2296,10 @@ fn now() -> String { #[cfg(test)] mod tests { use super::*; - use epiphany_core::open_runtime_spine_heartbeat_job; - use epiphany_core::runtime_job_snapshot; use epiphany_core::EpiphanyWorkerLaunchDocument; use epiphany_core::RuntimeSpineHeartbeatJobOptions; + use epiphany_core::open_runtime_spine_heartbeat_job; + use epiphany_core::runtime_job_snapshot; use epiphany_openai_adapter::EpiphanyOpenAiModelReceipt; use tempfile::tempdir; @@ -2214,6 +2327,73 @@ mod tests { Ok(()) } + #[test] + fn provider_null_optionals_are_inverse_projected_to_canonical_omission() -> Result<()> { + let parsed = parse_assistant_json::( + r#"{ + "roleId":"research","verdict":"source-gap","summary":"bounded", + "nextSafeMove":"review","filesInspected":[],"frontierNodeIds":null, + "evidenceIds":[],"artifactRefs":null,"openQuestions":null, + "evidenceGaps":["missing"],"risks":null,"statePatch":null, + "repoModelPatch":null,"selfPatch":null + }"#, + )?; + + assert_eq!(parsed.role_id.as_deref(), Some("research")); + assert!(parsed.frontier_node_ids.is_empty()); + assert!(parsed.artifact_refs.is_empty()); + assert!(parsed.state_patch.is_none()); + assert_eq!(parsed.evidence_gaps, vec!["missing"]); + Ok(()) + } + + #[test] + fn every_static_worker_contract_projects_to_one_strict_provider_shape() -> Result<()> { + let mut schemas = vec![ + epiphany_core::epiphany_role_launch_output_schema( + epiphany_core::EpiphanyRoleResultRoleId::Imagination, + ), + epiphany_core::epiphany_role_launch_output_schema( + epiphany_core::EpiphanyRoleResultRoleId::Research, + ), + epiphany_core::epiphany_role_launch_output_schema( + epiphany_core::EpiphanyRoleResultRoleId::Modeling, + ), + epiphany_core::epiphany_role_launch_output_schema( + epiphany_core::EpiphanyRoleResultRoleId::Verification, + ), + epiphany_core::epiphany_frontier_planning_output_schema(), + epiphany_core::epiphany_frontier_plan_mind_output_schema(), + epiphany_core::epiphany_imagination_consideration_output_schema(), + epiphany_core::epiphany_admitted_model_direction_consideration_output_schema(), + epiphany_core::epiphany_reorient_launch_output_schema(), + ]; + for source_kind in [ + epiphany_core::RepoFrontierProposalSourceKind::User, + epiphany_core::RepoFrontierProposalSourceKind::Persona, + epiphany_core::RepoFrontierProposalSourceKind::Bifrost, + epiphany_core::RepoFrontierProposalSourceKind::Imagination, + ] { + schemas.push(epiphany_core::epiphany_proposal_modeling_output_schema( + source_kind, + )); + } + + for (index, schema) in schemas.into_iter().enumerate() { + let mut request = EpiphanyOpenAiModelRequest::new( + format!("strict-worker-schema-{index}"), + "strict-worker-schema", + "gpt-5.4", + "Return the typed result.", + ); + request.output_contract_id = Some(format!("worker-schema-{index}")); + request.output_schema_json = Some(serde_json::to_string(&schema)?); + let body = epiphany_openai_codex_spine::responses_body_from_epiphany(request)?; + assert_eq!(body["text"]["format"]["strict"], true); + } + Ok(()) + } + #[test] fn role_ingress_rejects_duplicate_top_level_fields() { let error = parse_assistant_json::( @@ -2337,9 +2517,11 @@ mod tests { )?; assert_eq!(patch.base_revision, 41); assert_eq!(patch.base_hash, "model-hash-41"); - assert_eq!(patch.purpose, epiphany_core::RepoModelPatchPurpose::Evolution); - let epiphany_core::RepoModelPatchOperation::UpsertFrontier { item } = - &patch.operations[0] + assert_eq!( + patch.purpose, + epiphany_core::RepoModelPatchPurpose::Evolution + ); + let epiphany_core::RepoModelPatchOperation::UpsertFrontier { item } = &patch.operations[0] else { panic!("proposal draft must compose one frontier upsert"); }; @@ -2375,8 +2557,8 @@ mod tests { model_revision: 41, model_hash: "model-hash-41".into(), }; - let document = EpiphanyWorkerLaunchDocument::Role( - epiphany_core::EpiphanyRoleWorkerLaunchDocument { + let document = + EpiphanyWorkerLaunchDocument::Role(epiphany_core::EpiphanyRoleWorkerLaunchDocument { thread_id: "thread-1".into(), role_id: "modeling".into(), state_revision: 1, @@ -2403,8 +2585,7 @@ mod tests { graph_checkpoint: None, planning: None, churn: None, - }, - ); + }); let launch = EpiphanyRuntimeWorkerLaunchRequest { schema_version: epiphany_core::RUNTIME_WORKER_LAUNCH_REQUEST_SCHEMA_VERSION.into(), job_id: "proposal-job-1".into(), @@ -2443,7 +2624,11 @@ mod tests { assert!(schema["properties"].get("proposalFrontierDraft").is_some()); assert!(schema["properties"].get("repoModelPatch").is_none()); assert!(!request.instructions.contains("Output schema JSON")); - assert!(request.instructions.contains("Emit only the semantic frontier draft")); + assert!( + request + .instructions + .contains("Emit only the semantic frontier draft") + ); Ok(()) } @@ -2715,7 +2900,10 @@ mod tests { .expect("runtime-composed candidate"); assert_eq!(candidate.request_id, request.request_id); assert_eq!(candidate.feedback_id, request.feedback_id); - assert_eq!(candidate.feedback_packet_sha256, request.feedback_packet_sha256); + assert_eq!( + candidate.feedback_packet_sha256, + request.feedback_packet_sha256 + ); assert_eq!(candidate.source_room_id, request.source_room_id); assert_eq!(candidate.source_visibility, request.source_visibility); assert_eq!(candidate.data_classification, request.data_classification); @@ -3040,14 +3228,18 @@ mod tests { let mut cache = runtime_spine_cache(&store)?; cache.pull_all_backing_stores()?; assert!(cache.get::("req-1")?.is_some()); - assert!(cache - .get::("req-1:00000000")? - .is_some()); + assert!( + cache + .get::("req-1:00000000")? + .is_some() + ); assert!(cache.get::("req-1")?.is_some()); assert!(cache.get::("req-1")?.is_some()); - assert!(cache - .get::("req-1:00000000")? - .is_some()); + assert!( + cache + .get::("req-1:00000000")? + .is_some() + ); assert!(cache.get::("req-1")?.is_some()); assert_eq!( runtime_job_snapshot(&store, &options.job_id)? @@ -3200,31 +3392,43 @@ mod tests { assert!(!output_schema.contains("\"const\": \"evolution\"")); assert!(!model_request.instructions.contains("Output schema JSON")); assert!(!model_request.instructions.contains("\"repoModelPatch\"")); - assert!(model_request - .instructions - .contains("Emit every object key at most once")); + assert!( + model_request + .instructions + .contains("Emit every object key at most once") + ); assert_eq!(model_request.reasoning_effort.as_deref(), Some("low")); assert_eq!(model_request.reasoning_summary.as_deref(), Some("concise")); - assert!(model_request - .instructions - .contains("")); + assert!( + model_request + .instructions + .contains("") + ); assert!(model_request.instructions.contains("local Verse: bounded")); - assert!(model_request - .tools - .iter() - .any(|tool| tool.name == "mcp__epiphany_source__read_file")); - assert!(model_request - .tools - .iter() - .any(|tool| tool.name == "mcp__epiphany_source__directory_inventory")); - assert!(model_request - .tools - .iter() - .any(|tool| tool.name == "mcp__epiphany_state__resident_grant_lifecycle")); - assert!(!model_request - .tools - .iter() - .any(|tool| tool.name == "mcp__epiphany_public__github_file")); + assert!( + model_request + .tools + .iter() + .any(|tool| tool.name == "mcp__epiphany_source__read_file") + ); + assert!( + model_request + .tools + .iter() + .any(|tool| tool.name == "mcp__epiphany_source__directory_inventory") + ); + assert!( + model_request + .tools + .iter() + .any(|tool| tool.name == "mcp__epiphany_state__resident_grant_lifecycle") + ); + assert!( + !model_request + .tools + .iter() + .any(|tool| tool.name == "mcp__epiphany_public__github_file") + ); assert!(model_request.instructions.contains("Modeling must inspect")); let openai_summary = EpiphanyOpenAiRuntimeRunSummary { store: store.display().to_string(), @@ -3290,10 +3494,12 @@ mod tests { typed_result.self_patch()?.expect("self patch").reason, Some("typed nested document".to_string()) ); - assert!(runtime_job_snapshot(&store, "worker-job-1")? - .expect("snapshot") - .result - .is_some()); + assert!( + runtime_job_snapshot(&store, "worker-job-1")? + .expect("snapshot") + .result + .is_some() + ); Ok(()) } @@ -3380,30 +3586,39 @@ mod tests { assert!(tool_names.contains(&"mcp__epiphany_source__read_hands_receipt")); assert!(tool_names.contains(&"mcp__epiphany_state__resident_grant_lifecycle")); assert!(!tool_names.contains(&"mcp__epiphany_public__github_file")); - assert!(model_request - .instructions - .contains("mcp__epiphany_source__read_file")); - assert!(model_request - .instructions - .contains("mcp__epiphany_source__directory_inventory")); - assert!(model_request - .instructions - .contains("mcp__epiphany_state__resident_grant_lifecycle")); + assert!( + model_request + .instructions + .contains("mcp__epiphany_source__read_file") + ); + assert!( + model_request + .instructions + .contains("mcp__epiphany_source__directory_inventory") + ); + assert!( + model_request + .instructions + .contains("mcp__epiphany_state__resident_grant_lifecycle") + ); let mut research_launch = launch_request; - research_launch.binding_id = - epiphany_core::EPIPHANY_RESEARCH_ROLE_BINDING_ID.to_string(); + research_launch.binding_id = epiphany_core::EPIPHANY_RESEARCH_ROLE_BINDING_ID.to_string(); research_launch.role = epiphany_core::EPIPHANY_RESEARCH_OWNER_ROLE.to_string(); research_launch.authority_scope = "epiphany.role.research".to_string(); let research_request = build_worker_model_request(&research_launch, DEFAULT_MODEL_PROVIDER, "gpt-5.4")?; - assert!(research_request - .tools - .iter() - .any(|tool| tool.name == "mcp__epiphany_public__github_file")); - assert!(research_request - .instructions - .contains("exact 40-hex commit")); + assert!( + research_request + .tools + .iter() + .any(|tool| tool.name == "mcp__epiphany_public__github_file") + ); + assert!( + research_request + .instructions + .contains("exact 40-hex commit") + ); Ok(()) } @@ -3499,10 +3714,8 @@ mod tests { .clone(); let mut cache = runtime_spine_cache(&store)?; cache.pull_all_backing_stores()?; - let tool_binding = epiphany_core::require_runtime_tool_execution_binding( - &store, - &intent_id, - )?; + let tool_binding = + epiphany_core::require_runtime_tool_execution_binding(&store, &intent_id)?; assert_eq!(tool_binding.session_id, options.session_id); assert_eq!(tool_binding.job_id, options.job_id); assert_eq!(tool_binding.model_request_id.as_deref(), Some("req-tools"));