From 2427f24010c898c6379807a5d60820c5333cfebb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 27 Aug 2026 19:05:56 +0200 Subject: [PATCH] Use balanced civic reconciliation for rejected destinations --- .../src/bin/ghostlight-strategic-smoke.rs | 47 +- crates/ghostlight-dungeon/src/compiler.rs | 1199 +++++++++++++++-- crates/ghostlight-dungeon/src/main.rs | 24 +- crates/ghostlight-dungeon/src/mesh.rs | 1 + crates/ghostlight-dungeon/src/model.rs | 16 +- .../ghostlight-dungeon/src/model_connector.rs | 30 +- .../ghostlight-dungeon/src/model_runtime.rs | 38 +- docs/architecture/ghostlight-dungeon-mvp.md | 30 +- notes/ghostlight-current-system-map.md | 28 +- 9 files changed, 1236 insertions(+), 177 deletions(-) diff --git a/crates/ghostlight-dungeon/src/bin/ghostlight-strategic-smoke.rs b/crates/ghostlight-dungeon/src/bin/ghostlight-strategic-smoke.rs index b68e7c0..d3f4231 100644 --- a/crates/ghostlight-dungeon/src/bin/ghostlight-strategic-smoke.rs +++ b/crates/ghostlight-dungeon/src/bin/ghostlight-strategic-smoke.rs @@ -13,6 +13,7 @@ fn final_wave_field( async fn main() -> anyhow::Result<()> { use chrono::Utc; use ghostlight_dungeon::{ + compiler::DestinationCompilationFailure, domain::{TickSource, WorldCommand}, kernel::{CommandResult, WorldKernel}, model_runtime::ModelRuntimeSelection, @@ -170,9 +171,51 @@ async fn main() -> anyhow::Result<()> { }))?, )?; let request = strategic_locality_request(&location_name, location_id, &pressure); - let (preview, receipts) = compiler + let compilation = compiler .compile_destination(&campaign, location_id, &request) - .await?; + .await; + let (preview, receipts) = match compilation { + Ok(compilation) => compilation, + Err(error) => { + let failure_receipts = error + .downcast_ref::() + .map(|failure| failure.model_receipts.clone()) + .unwrap_or_default(); + let receipt_hashes = failure_receipts + .iter() + .map(|receipt| receipt.storage_key().to_owned()) + .collect::>(); + let persistence_error = if failure_receipts.is_empty() { + None + } else { + store + .persist_model_stage_receipts(&failure_receipts) + .err() + .map(|error| error.to_string()) + }; + std::fs::write( + root.join(format!( + "elaboration-{:02}-terminal-failure.json", + index + 1 + )), + serde_json::to_vec_pretty(&serde_json::json!({ + "schema":"ghostlight.strategic_elaboration_failure.v1", + "location_id":location_id, + "location_name":location_name, + "request":request, + "error":error.to_string(), + "model_receipt_hashes":receipt_hashes, + "receipt_persistence_error":&persistence_error, + }))?, + )?; + if let Some(persistence_error) = persistence_error { + return Err(anyhow::anyhow!( + "{error}; model-receipt persistence failed: {persistence_error}" + )); + } + return Err(error); + } + }; let command = match &preview { ghostlight_dungeon::domain::DestinationCompilationPreview::LocalityElaboration( preview, diff --git a/crates/ghostlight-dungeon/src/compiler.rs b/crates/ghostlight-dungeon/src/compiler.rs index 6fa75e8..c1b1c4e 100644 --- a/crates/ghostlight-dungeon/src/compiler.rs +++ b/crates/ghostlight-dungeon/src/compiler.rs @@ -7,7 +7,7 @@ use crate::{ LocalityElaborationPreview, Location, MAX_POSTURE_CHARS, Route, VaultEvidenceReceipt, WorldClock, WorldCompilePreview, WorldFact, }, - model::{ModelPort, ModelStageReceipt, ModelStageRequest, run_validated_stage}, + model::{MODEL_BALANCED, ModelPort, ModelStageReceipt, ModelStageRequest, run_validated_stage}, session_zero::{ ApprovedCampaignBrief, CampaignContract, MAX_SESSION_ZERO_MEMBERS, actor_from_character, }, @@ -107,6 +107,20 @@ pub struct SuggestedRoles { pub retrieval_receipt: ModelStageReceipt, } +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct DestinationCompilationFailure { + pub message: String, + pub model_receipts: Vec, +} + +impl std::fmt::Display for DestinationCompilationFailure { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str(&self.message) + } +} + +impl std::error::Error for DestinationCompilationFailure {} + #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] struct OpeningSet { openings: Vec, @@ -639,6 +653,124 @@ struct CivicSystemVerification { rationale: String, } +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] +struct CompiledResidentCivicKnowledge { + population_id: String, + civic_fact_ids: BTreeSet, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] +struct CompiledCivicSystemReconciliation { + schema: String, + jurisdiction_location_id: String, + governing_institution_ids: BTreeSet, + resident_population_ids: BTreeSet, + public_authority_fact_ids: BTreeSet, + public_selection_fact_ids: BTreeSet, + public_resource_fact_ids: BTreeSet, + public_redress_fact_ids: BTreeSet, + political_relation_ids: BTreeSet, +} + +impl From for CivicSystemManifest { + fn from(value: CompiledCivicSystemReconciliation) -> Self { + Self { + schema: value.schema, + version: 0, + jurisdiction_location_id: value.jurisdiction_location_id, + governing_institution_ids: value.governing_institution_ids, + resident_population_ids: value.resident_population_ids, + public_authority_fact_ids: value.public_authority_fact_ids, + public_selection_fact_ids: value.public_selection_fact_ids, + public_resource_fact_ids: value.public_resource_fact_ids, + public_redress_fact_ids: value.public_redress_fact_ids, + political_relation_ids: value.political_relation_ids, + semantic_verification_receipt_id: String::new(), + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] +struct CompiledCivicReconciliation { + #[schemars(length(max = 32))] + civic_facts: Vec, + #[schemars(length(max = 8))] + resident_civic_knowledge: Vec, + #[schemars(length(max = 12))] + institutions: Vec, + #[schemars(length(max = 48))] + local_relations: Vec, + civic_system: CompiledCivicSystemReconciliation, +} + +enum CivicSystemVerificationOutcome { + Accepted { + receipts: Vec, + }, + Rejected { + verdict: CivicSystemVerification, + receipts: Vec, + }, + Failed { + message: String, + receipts: Vec, + }, +} + +enum CivicReconciliationOutcome { + Corrected { + seed: CompiledExpansionSeed, + receipt: ModelStageReceipt, + }, + Failed { + message: String, + receipts: Vec, + }, +} + +fn destination_compilation_failure( + message: impl std::fmt::Display, + model_receipts: Vec, +) -> anyhow::Error { + anyhow::Error::new(DestinationCompilationFailure { + message: message.to_string(), + model_receipts, + }) +} + +fn destination_model_receipts( + identity_receipts: &[ModelStageReceipt], + retrieval_receipt: Option<&ModelStageReceipt>, + compiler_receipts: &[ModelStageReceipt], +) -> Vec { + identity_receipts + .iter() + .chain(retrieval_receipt) + .chain(compiler_receipts) + .cloned() + .collect() +} + +fn destination_candidate_failure( + message: impl std::fmt::Display, + identity_receipts: &[ModelStageReceipt], + retrieval_receipt: &ModelStageReceipt, + compiler_receipts: &mut [ModelStageReceipt], +) -> anyhow::Error { + let message = message.to_string(); + if let Some(receipt) = compiler_receipts.last_mut() { + mark_semantic_invalid(receipt, &message); + } + destination_compilation_failure( + message, + destination_model_receipts( + identity_receipts, + Some(retrieval_receipt), + compiler_receipts, + ), + ) +} + #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] struct CompiledDestinationMigrationRelation { id: String, @@ -1598,7 +1730,7 @@ impl WorldCompiler { let mut correction = String::new(); let mut receipts = Vec::new(); for attempt in 0..2 { - let output = run_validated_stage( + let output = match run_validated_stage( self.model.as_ref(), &ModelStageRequest { stage: "destination_identity_resolution".into(), @@ -1611,7 +1743,11 @@ impl WorldCompiler { max_output_tokens: Some(384), }, ) - .await?; + .await + { + Ok(output) => output, + Err(error) => return Err(destination_compilation_failure(error, receipts)), + }; let mut receipt = output.receipt; let resolution = output .structured @@ -1658,8 +1794,12 @@ impl WorldCompiler { } Err(error) => { mark_semantic_invalid(&mut receipt, &error); - return Err(anyhow!( - "destination identity resolution failed local validation after one correction: {error}" + receipts.push(receipt); + return Err(destination_compilation_failure( + format!( + "destination identity resolution failed local validation after one correction: {error}" + ), + receipts, )); } } @@ -1690,10 +1830,12 @@ impl WorldCompiler { let Some((_path, _travel_minutes)) = shortest_location_path(campaign, origin_location_id, existing_destination_id) else { - return Err(anyhow!( - "{} already exists in canonical campaign topology, but no committed route currently reaches it from {}. No substitute location or route preview was created; request a genuinely new intermediary place or resolve the missing topology explicitly.", - destination.name, - origin.name, + return Err(destination_compilation_failure( + format!( + "{} already exists in canonical campaign topology, but no committed route currently reaches it from {}. No substitute location or route preview was created; request a genuinely new intermediary place or resolve the missing topology explicitly.", + destination.name, origin.name, + ), + identity_receipts.clone(), )); }; } @@ -1704,23 +1846,32 @@ impl WorldCompiler { .locations .get(expansion_origin_location_id) .expect("expansion origin is either the validated origin or destination"); + let retrieval_subject = serde_json::to_string(&serde_json::json!({ + "origin": origin, + "elaboration_anchor": expansion_origin, + "existing_destination_id": existing_destination_id, + "destination": destination_request, + "canon_cutoff": campaign.branch_origin.canon_cutoff, + })) + .map_err(|error| destination_compilation_failure(error, identity_receipts.clone()))?; let (queries, retrieval_receipt) = self .plan_queries( "destination_retrieval_plan", &format!("campaign:{}:revision:{}", campaign.id, campaign.revision), - &serde_json::to_string(&serde_json::json!({ - "origin": origin, - "elaboration_anchor": expansion_origin, - "existing_destination_id": existing_destination_id, - "destination": destination_request, - "canon_cutoff": campaign.branch_origin.canon_cutoff, - }))?, + &retrieval_subject, 2, ) - .await?; + .await + .map_err(|error| destination_compilation_failure(error, identity_receipts.clone()))?; let receipts = self .retrieve_all(&queries, &campaign.branch_origin.canon_cutoff, 10) - .await?; + .await + .map_err(|error| { + destination_compilation_failure( + error, + destination_model_receipts(&identity_receipts, Some(&retrieval_receipt), &[]), + ) + })?; let source_population_ids = campaign .gestalts .values() @@ -1784,32 +1935,79 @@ impl WorldCompiler { } else { "The primary destination is genuinely new. Admit it and its bounded child detail from the supplied origin without rewriting any existing place.".into() }; + let completed_model_receipts = |compiler_receipts: &[ModelStageReceipt]| { + destination_model_receipts( + &identity_receipts, + Some(&retrieval_receipt), + compiler_receipts, + ) + }; + let campaign_locations = serde_json::to_string(&campaign.locations).map_err(|error| { + destination_compilation_failure(error, completed_model_receipts(&[])) + })?; + let current_civic_apparatus = + serde_json::to_string(¤t_civic_context).map_err(|error| { + destination_compilation_failure(error, completed_model_receipts(&[])) + })?; + let source_population_context = + serde_json::to_string(&origin_population_context).map_err(|error| { + destination_compilation_failure(error, completed_model_receipts(&[])) + })?; let base_prompt = format!( "Compile only the requested bounded destination region. {scope_instruction} Every new location id must be new. Return explicit origin_routes records owned by expansion anchor id {} into the new region or child locality, and give every such destination a reciprocal route record back to the anchor with the same positive travel time. Every route record needs a stable route_id local to its exact origin, an exact destination_id, a distance, and positive travel_minutes; the same local route_id may exist under another origin without naming the same route. Do not rewrite existing geography. Every place has a non-empty name, valid container, and concrete persistent features. Any locally observable clue must already exist as a fact and list exact discoverable_at_location_ids from the combined existing and new topology; later action assessment can reveal facts but cannot invent them.\n\nUse evidence as canon constraints, not as an exhaustive game map. Missing game-scale routes, geometry, people, ordinary procedures, supplies, local responsibilities, capacity choices, and operating doctrine require the smallest coherent playable elaboration. Mark the resulting facts branch_local or provisional_local and disclose consequential inventions in branch_assumptions. Variation between campaigns is permitted; if a detail must not vary, it belongs in the Vault. Never put a compatible elaboration in gaps merely because the Vault is silent.\n\nWhen CURRENT CIVIC APPARATUS is null, a playable inhabited destination needs one to eight non-overlapping population leaves and two to twelve distinct institutions. When it is present, preserve it and add only genuinely new detail needed by the request; population and institution arrays may be empty. New local relations may join new subjects to exact subjects in the current apparatus. Never duplicate a resident body, office, fact, or relation under a fresh name. Each new population home_location_id must be one new location. Each new institution location_ids set may name the jurisdiction or its new children. shared_fact_ids may contain exact fact IDs from the current apparatus or this candidate, never free-text knowledge. collective_authority_id may be null or the exact ID of one new population and denotes real shared authority.\n\nReturn one complete civic_system manifest for every inhabited candidate. On an existing apparatus it is the next version and must retain every existing governing institution, resident population, public fact, and political relation while adding any new IDs. It must identify the exact jurisdiction, its governing institutions and resident populations, at least one committed public fact in each of four domains—current authority, selection or succession, public resources or revenue, and redress or appeal—and the political relations that make implementation, hierarchy, or contestation legible. Every named resident population must share those public civic facts. If REQUEST presupposes a mayor, election, throne, council, or other office that this locality does not use, commit facts that let a resident correct the premise; never manufacture the requested institution merely to agree with the question. The question selects the missing domain, not its answer.\n\nThe gaps array is legal only when no compatible elaboration can preserve an exact clause of REQUEST without choosing between contradictory canon baselines, inventing an unanchored canon baseline explicitly required by the request, or exceeding an approved capability. Every gap must name that exact premise clause and the exact table choice blocking compilation. `The Vault does not specify X` is never sufficient. Use an empty gaps array when branch-local invention preserves the request.\n\nA migration relation is a directed available path for a later voluntary strategic choice; it does not move anyone, establish that admission occurred, or erase destination-community agency. It may originate only from one exact co-located active population ID supplied below and may target only one new population ID. Emit a relation only when the request and supplied source population/member goals support that migration possibility. Never invent a source population or named member. The approval preview must make all branch-local assumptions explicit without misclassifying them as canon gaps.\n\nCAMPAIGN LOCATIONS:\n{}\nCURRENT CIVIC APPARATUS:\n{}\nCO-LOCATED SOURCE POPULATIONS AND NAMED MEMBER DELTAS:\n{}\nREQUEST:\n{}\nEVIDENCE:\n{}", expansion_origin_location_id, - serde_json::to_string(&campaign.locations)?, - serde_json::to_string(¤t_civic_context)?, - serde_json::to_string(&origin_population_context)?, + campaign_locations, + current_civic_apparatus, + source_population_context, destination_request, evidence_text(&receipts) ); - let mut schema = serde_json::to_value(schema_for!(CompiledExpansionSeed))?; - constrain_destination_expansion_schema(&mut schema, &source_population_ids)?; + let mut schema = + serde_json::to_value(schema_for!(CompiledExpansionSeed)).map_err(|error| { + destination_compilation_failure(error, completed_model_receipts(&[])) + })?; + constrain_destination_expansion_schema(&mut schema, &source_population_ids).map_err( + |error| destination_compilation_failure(error, completed_model_receipts(&[])), + )?; let sources = receipt_ids(&receipts); let mut compiler_receipts = Vec::new(); let mut correction = String::new(); - let (seed, mut expansion) = loop { - let output = self - .structured( - "destination_compile", - &snapshot, - &format!("{base_prompt}{correction}"), - schema.clone(), - sources.clone(), - ) - .await?; - compiler_receipts.push(output.1); - let seed: CompiledExpansionSeed = serde_json::from_value(output.0)?; + let mut local_corrections = 0; + let mut civic_corrections = 0; + let mut reconciled_candidate: Option = None; + let (seed, expansion) = loop { + let candidate_was_reconciled = reconciled_candidate.is_some(); + let seed = if let Some(seed) = reconciled_candidate.take() { + seed + } else { + let output = self + .structured( + "destination_compile", + &snapshot, + &format!("{base_prompt}{correction}"), + schema.clone(), + sources.clone(), + ) + .await + .map_err(|error| { + destination_compilation_failure( + error, + completed_model_receipts(&compiler_receipts), + ) + })?; + compiler_receipts.push(output.1); + match serde_json::from_value(output.0) { + Ok(seed) => seed, + Err(error) => { + return Err(destination_candidate_failure( + error, + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, + )); + } + } + }; if let Err(error) = validate_compiled_material_gaps(&seed.gaps, &receipts) .and_then(|_| validate_branch_assumptions(&seed.branch_assumptions)) { @@ -1819,14 +2017,26 @@ impl WorldCompiler { .expect("receipt was just stored"), &error, ); - if compiler_receipts.len() == 1 { - correction = format!( - "\n\nLOCAL VALIDATOR REJECTED THE PREVIOUS CANDIDATE: {error}\nReturn a corrected complete candidate against the same CAMPAIGN, REQUEST, and EVIDENCE. Compatible game-scale invention belongs in branch_assumptions, not gaps." - ); + if candidate_was_reconciled { + return Err(destination_compilation_failure( + format!( + "destination civic reconciliation failed local validation: {error}" + ), + completed_model_receipts(&compiler_receipts), + )); + } + if local_corrections == 0 { + local_corrections += 1; + correction.push_str(&format!( + "\n\nLOCAL VALIDATOR REJECTED THE PREVIOUS CANDIDATE: {error}\nReturn a corrected complete candidate against the same CAMPAIGN, REQUEST, and EVIDENCE. Compatible game-scale invention belongs in branch_assumptions, not gaps." + )); continue; } - return Err(anyhow!( - "destination compiler failed local validation after one correction: {error}" + return Err(destination_compilation_failure( + format!( + "destination compiler failed local validation after one correction: {error}" + ), + completed_model_receipts(&compiler_receipts), )); } let fact_statements = campaign @@ -1835,7 +2045,7 @@ impl WorldCompiler { .chain(seed.facts.iter()) .map(|fact| (fact.id.as_str(), fact.statement.as_str())) .collect::>(); - let populations = seed + let populations = match seed .populations .iter() .map(|population| { @@ -1860,7 +2070,18 @@ impl WorldCompiler { pressures: population.pressures.clone(), }) }) - .collect::>>()?; + .collect::>>() + { + Ok(populations) => populations, + Err(error) => { + return Err(destination_candidate_failure( + error, + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, + )); + } + }; let population_profiles = seed .populations .iter() @@ -1951,18 +2172,41 @@ impl WorldCompiler { .unwrap_or(0); system.semantic_verification_receipt_id.clear(); } - let expansion = crate::domain::RegionExpansion { + let origin_routes = match compiled_route_map( + seed.origin_routes.clone(), + expansion_origin_location_id, + ) { + Ok(routes) => routes, + Err(error) => { + return Err(destination_candidate_failure( + error, + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, + )); + } + }; + let locations = match seed + .locations + .clone() + .into_iter() + .map(CompiledLocation::into_location) + .collect::>>() + { + Ok(locations) => locations, + Err(error) => { + return Err(destination_candidate_failure( + error, + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, + )); + } + }; + let mut expansion = crate::domain::RegionExpansion { origin_location_id: expansion_origin_location_id.into(), - origin_routes: compiled_route_map( - seed.origin_routes.clone(), - expansion_origin_location_id, - )?, - locations: seed - .locations - .clone() - .into_iter() - .map(CompiledLocation::into_location) - .collect::>>()?, + origin_routes, + locations, facts: seed.facts.clone(), populations, population_profiles, @@ -1989,94 +2233,130 @@ impl WorldCompiler { validate_new_destination_expansion(campaign, &expansion) }; match validation { - Ok(()) => break (seed, expansion), - Err(error) if compiler_receipts.len() == 1 => { + Ok(()) if expansion.civic_system.is_none() => break (seed, expansion), + Ok(()) => { + let verification_sources = sources + .iter() + .cloned() + .chain( + compiler_receipts + .iter() + .map(|receipt| receipt.storage_key().to_owned()), + ) + .collect::>() + .into_iter() + .collect::>(); + match self + .verify_destination_civic_system( + campaign, + destination_request, + &evidence_text(&receipts), + ¤t_civic_context, + &seed, + &mut expansion, + &snapshot, + &verification_sources, + ) + .await + { + CivicSystemVerificationOutcome::Accepted { receipts } => { + compiler_receipts.extend(receipts); + break (seed, expansion); + } + CivicSystemVerificationOutcome::Rejected { verdict, receipts } => { + compiler_receipts.extend(receipts); + let error = anyhow!( + "destination civic verifier rejected the candidate: {}", + verdict.rationale.trim() + ); + if civic_corrections == 0 { + civic_corrections += 1; + let reconciliation_sources = sources + .iter() + .cloned() + .chain( + compiler_receipts + .iter() + .map(|receipt| receipt.storage_key().to_owned()), + ) + .collect::>() + .into_iter() + .collect::>(); + match self + .reconcile_destination_civic_system( + campaign, + destination_request, + &seed, + &verdict, + &snapshot, + &reconciliation_sources, + ) + .await + { + CivicReconciliationOutcome::Corrected { seed, receipt } => { + compiler_receipts.push(receipt); + reconciled_candidate = Some(seed); + continue; + } + CivicReconciliationOutcome::Failed { message, receipts } => { + compiler_receipts.extend(receipts); + return Err(destination_compilation_failure( + message, + completed_model_receipts(&compiler_receipts), + )); + } + } + } + return Err(destination_compilation_failure( + format!( + "destination civic verifier rejected the candidate after one balanced reconciliation: {error}" + ), + completed_model_receipts(&compiler_receipts), + )); + } + CivicSystemVerificationOutcome::Failed { message, receipts } => { + compiler_receipts.extend(receipts); + return Err(destination_compilation_failure( + message, + completed_model_receipts(&compiler_receipts), + )); + } + } + } + Err(error) if candidate_was_reconciled => { + return Err(destination_candidate_failure( + format!( + "destination civic reconciliation failed local validation: {error}" + ), + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, + )); + } + Err(error) if local_corrections == 0 => { mark_semantic_invalid( compiler_receipts .last_mut() .expect("receipt was just stored"), &error, ); - correction = format!( - "\n\nLOCAL VALIDATOR REJECTED THE PREVIOUS CANDIDATE: {error}\nReturn a corrected complete candidate against the same CAMPAIGN, REQUEST, and EVIDENCE." - ); + local_corrections += 1; + correction.push_str(&format!( + "\n\nLOCAL VALIDATOR REJECTED THE PREVIOUS CANDIDATE: {error}\nReturn a corrected complete candidate against the same CAMPAIGN, REQUEST, and EVIDENCE." + )); } Err(error) => { - return Err(anyhow!( - "destination compiler failed local validation after one correction: {error}" + return Err(destination_candidate_failure( + format!( + "destination compiler failed local validation after one correction: {error}" + ), + &identity_receipts, + &retrieval_receipt, + &mut compiler_receipts, )); } } }; - if let Some(civic_system) = &expansion.civic_system { - let base_verification_prompt = format!( - "Independently verify the admitted civic apparatus. Judge meaning, not JSON shape. The public facts must actually explain current authority, selection or succession, public resources or revenue, and redress or appeal. The institutions and political relations must form a coherent local apparatus, and every resident population's exact shared_fact_ids must ground an ordinary answer about local government. A question may select a civic domain but must not have forced its presupposed office, election, or answer into the candidate. Do not rewrite or complete the candidate; return verdicts only.\n\nREQUEST:\n{}\nEVIDENCE:\n{}\nCANDIDATE:\n{}", - destination_request, - evidence_text(&receipts), - serde_json::to_string(&serde_json::json!({ - "previous_civic_apparatus":¤t_civic_context, - "civic_system":civic_system, - "new_facts":&seed.facts, - "new_institutions":&seed.institutions, - "new_local_relations":&seed.local_relations, - "new_resident_populations":seed.populations.iter().map(|population| serde_json::json!({ - "id":population.id, - "shared_fact_ids":population.shared_fact_ids, - })).collect::>(), - }))?, - ); - let verification_schema = serde_json::to_value(schema_for!(CivicSystemVerification))?; - let mut correction = String::new(); - for attempt in 0..2 { - let (value, mut verification_receipt) = self - .structured( - "destination_civic_verification", - &snapshot, - &format!("{base_verification_prompt}{correction}"), - verification_schema.clone(), - sources.clone(), - ) - .await?; - let verdict = serde_json::from_value::(value)?; - let rationale_chars = verdict.rationale.trim().chars().count(); - let accepted = rationale_chars > 0 - && rationale_chars <= 1_000 - && verdict.authority_legible - && verdict.selection_or_succession_legible - && verdict.public_resources_legible - && verdict.redress_legible - && verdict.institutional_relations_coherent - && verdict.resident_answer_grounded; - if accepted { - let candidate_digest = civic_candidate_digest(&expansion)?; - verification_receipt - .rebind_snapshot(civic_verifier_binding(campaign, &candidate_digest)); - expansion - .civic_system - .as_mut() - .expect("verified civic system remains present") - .semantic_verification_receipt_id = - verification_receipt.storage_key().to_owned(); - compiler_receipts.push(verification_receipt); - break; - } - - let error = anyhow!( - "destination civic verifier rejected the candidate: {}", - verdict.rationale.trim() - ); - mark_semantic_invalid(&mut verification_receipt, &error); - compiler_receipts.push(verification_receipt); - if attempt == 0 { - correction = format!( - "\n\nTHE PREVIOUS VERDICT FAILED LOCAL ADMISSION BECAUSE ITS RATIONALE WAS EMPTY OR OVERSIZED, OR ONE OR MORE CIVIC VERDICTS WERE FALSE. Independently re-evaluate the same frozen candidate against every named invariant. Preserve every false verdict that identifies a real defect; do not turn a verdict true merely to satisfy admission. Correct only an accidental, unsupported, or internally inconsistent verdict. Return one complete verdict and no rewritten candidate.\nPREVIOUS_VERDICT:\n{}", - serde_json::to_string(&verdict)? - ); - continue; - } - return Err(error); - } - } let evidence_ids = receipt_ids(&receipts); let affected_sources: Vec = receipts .iter() @@ -2133,14 +2413,7 @@ impl WorldCompiler { requires_approval: true, }) }; - Ok(( - preview, - identity_receipts - .into_iter() - .chain(std::iter::once(retrieval_receipt)) - .chain(compiler_receipts) - .collect(), - )) + Ok((preview, completed_model_receipts(&compiler_receipts))) } async fn retrieve_all( @@ -2661,6 +2934,289 @@ impl WorldCompiler { unreachable!() } + async fn reconcile_destination_civic_system( + &self, + campaign: &Campaign, + destination_request: &str, + seed: &CompiledExpansionSeed, + verdict: &CivicSystemVerification, + snapshot: &str, + sources: &[String], + ) -> CivicReconciliationOutcome { + let Some(civic_system) = seed.civic_system.as_ref() else { + return CivicReconciliationOutcome::Failed { + message: "civic reconciliation requires a frozen civic candidate".into(), + receipts: Vec::new(), + }; + }; + let public_fact_ids = civic_public_fact_ids(civic_system); + let candidate_civic_facts = seed + .facts + .iter() + .filter(|fact| public_fact_ids.contains(&fact.id)) + .collect::>(); + let canonical_civic_facts = public_fact_ids + .iter() + .filter_map(|fact_id| campaign.facts.get(fact_id)) + .collect::>(); + let frozen_projection = serde_json::json!({ + "civic_system":civic_system, + "candidate_owned_civic_facts":candidate_civic_facts, + "canonical_civic_facts":canonical_civic_facts, + "resident_civic_knowledge":seed.populations.iter().map(|population| serde_json::json!({ + "population_id":population.id, + "name":population.name, + "civic_fact_ids":population.shared_fact_ids.intersection(&public_fact_ids).collect::>(), + })).collect::>(), + "institutions":&seed.institutions, + "local_relations":&seed.local_relations, + }); + let frozen_projection = match serde_json::to_string(&frozen_projection) { + Ok(projection) => projection, + Err(error) => { + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let schema = match serde_json::to_value(schema_for!(CompiledCivicReconciliation)) { + Ok(schema) => schema, + Err(error) => { + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let seed_bytes = match serde_json::to_vec(seed) { + Ok(bytes) => bytes, + Err(error) => { + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let binding = format!( + "{snapshot}:civic-reconciliation:sha256:{:x}", + Sha256::digest(seed_bytes) + ); + let prompt = format!( + "OUTPUT JSON SCHEMA (follow exactly):\n{}\n\nRepair only the civic projection of one frozen destination candidate. The expensive world invention is complete and immutable. The output is a complete replacement for candidate-owned civic facts, each resident population's civic fact IDs, candidate institutions, candidate political relations, and the civic manifest. It has no authority to change populations, names, goals, geography, routes, migration, non-civic facts, branch assumptions, or gaps. Preserve exact population IDs and the jurisdiction. Preserve canonical facts and every previously committed apparatus item; reference them from the manifest and resident civic knowledge but do not copy or rewrite them in civic_facts. Candidate-owned civic facts may be replaced or added with stable non-colliding IDs. Resolve the verifier's substantive contradiction across all affected civic fields. Do not merely rephrase the verdict or create an unlisted office. Return the smallest coherent correction that will survive fresh structural validation and independent verification.\n\nREQUEST:\n{}\n\nVERIFIER REJECTION:\n{}\n\nFROZEN CIVIC PROJECTION:\n{}", + match serde_json::to_string(&schema) { + Ok(schema) => schema, + Err(error) => { + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }, + destination_request, + verdict.rationale.trim(), + frozen_projection, + ); + let output = match run_validated_stage( + self.model.as_ref(), + &ModelStageRequest { + stage: "destination_civic_reconciliation".into(), + model: MODEL_BALANCED.into(), + snapshot_binding: binding, + lived_stream: prompt, + output_schema: Some(schema), + source_receipt_ids: sources.to_vec(), + temperature: Some(0.0), + max_output_tokens: Some(3_000), + }, + ) + .await + { + Ok(output) => output, + Err(error) => { + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let mut receipt = output.receipt; + let reconciliation = match output + .structured + .ok_or_else(|| anyhow!("civic reconciler returned no structured output")) + .and_then(|value| { + serde_json::from_value::(value).map_err(Into::into) + }) { + Ok(reconciliation) => reconciliation, + Err(error) => { + mark_semantic_invalid(&mut receipt, &error); + return CivicReconciliationOutcome::Failed { + message: error.to_string(), + receipts: vec![receipt], + }; + } + }; + match apply_civic_reconciliation(campaign, seed, reconciliation) { + Ok(seed) => CivicReconciliationOutcome::Corrected { seed, receipt }, + Err(error) => { + mark_semantic_invalid(&mut receipt, &error); + CivicReconciliationOutcome::Failed { + message: format!("destination civic reconciliation was invalid: {error}"), + receipts: vec![receipt], + } + } + } + } + + #[allow(clippy::too_many_arguments)] + async fn verify_destination_civic_system( + &self, + campaign: &Campaign, + destination_request: &str, + evidence: &str, + current_civic_context: &Option, + seed: &CompiledExpansionSeed, + expansion: &mut crate::domain::RegionExpansion, + snapshot: &str, + sources: &[String], + ) -> CivicSystemVerificationOutcome { + let civic_system = expansion + .civic_system + .as_ref() + .expect("civic verification is called only for an inhabited apparatus"); + let candidate = match serde_json::to_string(&serde_json::json!({ + "previous_civic_apparatus":current_civic_context, + "civic_system":civic_system, + "new_facts":&seed.facts, + "new_institutions":&seed.institutions, + "new_local_relations":&seed.local_relations, + "new_resident_populations":seed.populations.iter().map(|population| serde_json::json!({ + "id":population.id, + "shared_fact_ids":population.shared_fact_ids, + })).collect::>(), + })) { + Ok(candidate) => candidate, + Err(error) => { + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let base_prompt = format!( + "Independently verify the admitted civic apparatus. Judge meaning, not JSON shape. The public facts must actually explain current authority, selection or succession, public resources or revenue, and redress or appeal. The institutions and political relations must form a coherent local apparatus, and every resident population's exact shared_fact_ids must ground an ordinary answer about local government. A question may select a civic domain but must not have forced its presupposed office, election, or answer into the candidate. Do not rewrite or complete the candidate; return verdicts only.\n\nREQUEST:\n{}\nEVIDENCE:\n{}\nCANDIDATE:\n{}", + destination_request, evidence, candidate, + ); + let schema = match serde_json::to_value(schema_for!(CivicSystemVerification)) { + Ok(schema) => schema, + Err(error) => { + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts: Vec::new(), + }; + } + }; + let mut receipts = Vec::new(); + let mut correction = String::new(); + for attempt in 0..2 { + let attempt_sources = sources + .iter() + .cloned() + .chain( + receipts + .iter() + .map(|receipt: &ModelStageReceipt| receipt.storage_key().to_owned()), + ) + .collect::>() + .into_iter() + .collect::>(); + let (value, mut receipt) = match self + .structured( + "destination_civic_verification", + snapshot, + &format!("{base_prompt}{correction}"), + schema.clone(), + attempt_sources, + ) + .await + { + Ok(output) => output, + Err(error) => { + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts, + }; + } + }; + let verdict = match serde_json::from_value::(value) { + Ok(verdict) => verdict, + Err(error) => { + mark_semantic_invalid(&mut receipt, &error); + receipts.push(receipt); + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts, + }; + } + }; + let rationale_chars = verdict.rationale.trim().chars().count(); + let accepted = rationale_chars > 0 + && rationale_chars <= 1_000 + && verdict.authority_legible + && verdict.selection_or_succession_legible + && verdict.public_resources_legible + && verdict.redress_legible + && verdict.institutional_relations_coherent + && verdict.resident_answer_grounded; + if accepted { + let candidate_digest = match civic_candidate_digest(expansion) { + Ok(digest) => digest, + Err(error) => { + mark_semantic_invalid(&mut receipt, &error); + receipts.push(receipt); + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts, + }; + } + }; + receipt.rebind_snapshot(civic_verifier_binding(campaign, &candidate_digest)); + expansion + .civic_system + .as_mut() + .expect("verified civic system remains present") + .semantic_verification_receipt_id = receipt.storage_key().to_owned(); + receipts.push(receipt); + return CivicSystemVerificationOutcome::Accepted { receipts }; + } + + let error = anyhow!( + "destination civic verifier rejected the candidate: {}", + verdict.rationale.trim() + ); + mark_semantic_invalid(&mut receipt, &error); + receipts.push(receipt); + if attempt == 0 { + let previous_verdict = match serde_json::to_string(&verdict) { + Ok(verdict) => verdict, + Err(error) => { + return CivicSystemVerificationOutcome::Failed { + message: error.to_string(), + receipts, + }; + } + }; + correction = format!( + "\n\nTHE PREVIOUS VERDICT FAILED LOCAL ADMISSION BECAUSE ITS RATIONALE WAS EMPTY OR OVERSIZED, OR ONE OR MORE CIVIC VERDICTS WERE FALSE. Independently re-evaluate the same frozen candidate against every named invariant. Preserve every false verdict that identifies a real defect; do not turn a verdict true merely to satisfy admission. Correct only an accidental, unsupported, or internally inconsistent verdict. Return one complete verdict and no rewritten candidate.\nPREVIOUS_VERDICT:\n{}", + previous_verdict + ); + continue; + } + return CivicSystemVerificationOutcome::Rejected { verdict, receipts }; + } + unreachable!() + } + async fn structured( &self, stage: &str, @@ -2701,6 +3257,159 @@ impl WorldCompiler { } } +fn civic_public_fact_ids(civic_system: &CivicSystemManifest) -> BTreeSet { + civic_system + .public_authority_fact_ids + .iter() + .chain(civic_system.public_selection_fact_ids.iter()) + .chain(civic_system.public_resource_fact_ids.iter()) + .chain(civic_system.public_redress_fact_ids.iter()) + .cloned() + .collect() +} + +fn apply_civic_reconciliation( + campaign: &Campaign, + seed: &CompiledExpansionSeed, + reconciliation: CompiledCivicReconciliation, +) -> Result { + let original_civic_system = seed + .civic_system + .as_ref() + .ok_or_else(|| anyhow!("civic reconciliation requires an existing civic manifest"))?; + if reconciliation.civic_system.jurisdiction_location_id + != original_civic_system.jurisdiction_location_id + { + return Err(anyhow!( + "civic reconciliation cannot change the frozen jurisdiction" + )); + } + + let population_ids = seed + .populations + .iter() + .map(|population| population.id.clone()) + .collect::>(); + let reconciled_population_ids = reconciliation + .resident_civic_knowledge + .iter() + .map(|knowledge| knowledge.population_id.clone()) + .collect::>(); + if reconciliation.resident_civic_knowledge.len() != reconciled_population_ids.len() + || reconciled_population_ids != population_ids + { + return Err(anyhow!( + "civic reconciliation must name every frozen candidate population exactly once" + )); + } + + let original_public_fact_ids = civic_public_fact_ids(original_civic_system); + let protected_fact_ids = seed + .facts + .iter() + .filter(|fact| !original_public_fact_ids.contains(&fact.id)) + .map(|fact| fact.id.clone()) + .collect::>(); + let reconciled_fact_ids = reconciliation + .civic_facts + .iter() + .map(|fact| fact.id.clone()) + .collect::>(); + if reconciliation.civic_facts.len() != reconciled_fact_ids.len() { + return Err(anyhow!( + "civic reconciliation returned duplicate candidate fact IDs" + )); + } + if let Some(colliding_id) = reconciled_fact_ids + .iter() + .find(|fact_id| protected_fact_ids.contains(*fact_id)) + { + return Err(anyhow!( + "civic reconciliation cannot overwrite protected non-civic fact {colliding_id}" + )); + } + if let Some(canonical_id) = reconciled_fact_ids + .iter() + .find(|fact_id| campaign.facts.contains_key(*fact_id)) + { + return Err(anyhow!( + "civic reconciliation cannot copy or rewrite canonical fact {canonical_id}" + )); + } + let committed_civic_fact_ids = campaign + .civic_systems + .get(&original_civic_system.jurisdiction_location_id) + .map(civic_public_fact_ids) + .unwrap_or_default(); + let allowed_civic_fact_ids = reconciled_fact_ids + .iter() + .chain(committed_civic_fact_ids.iter()) + .cloned() + .collect::>(); + let reconciled_public_fact_ids = reconciliation + .civic_system + .public_authority_fact_ids + .iter() + .chain(reconciliation.civic_system.public_selection_fact_ids.iter()) + .chain(reconciliation.civic_system.public_resource_fact_ids.iter()) + .chain(reconciliation.civic_system.public_redress_fact_ids.iter()) + .cloned() + .collect::>(); + let unauthorized_public_fact_ids = reconciled_public_fact_ids + .difference(&allowed_civic_fact_ids) + .cloned() + .collect::>(); + if !unauthorized_public_fact_ids.is_empty() { + return Err(anyhow!( + "civic reconciliation referenced facts outside returned or committed civic authority: {unauthorized_public_fact_ids:?}" + )); + } + for knowledge in &reconciliation.resident_civic_knowledge { + let unauthorized_knowledge = knowledge + .civic_fact_ids + .difference(&allowed_civic_fact_ids) + .cloned() + .collect::>(); + if !unauthorized_knowledge.is_empty() { + return Err(anyhow!( + "civic reconciliation gave population {} facts outside returned or committed civic authority: {unauthorized_knowledge:?}", + knowledge.population_id + )); + } + if !reconciled_public_fact_ids.is_subset(&knowledge.civic_fact_ids) { + return Err(anyhow!( + "civic reconciliation must give population {} every reconciled public civic fact", + knowledge.population_id + )); + } + } + + let mut corrected = seed.clone(); + corrected + .facts + .retain(|fact| !original_public_fact_ids.contains(&fact.id)); + corrected.facts.extend(reconciliation.civic_facts); + let mut resident_civic_knowledge = reconciliation + .resident_civic_knowledge + .into_iter() + .map(|knowledge| (knowledge.population_id, knowledge.civic_fact_ids)) + .collect::>(); + for population in &mut corrected.populations { + population + .shared_fact_ids + .retain(|fact_id| !original_public_fact_ids.contains(fact_id)); + population.shared_fact_ids.extend( + resident_civic_knowledge + .remove(&population.id) + .expect("reconciled population set was proven exact"), + ); + } + corrected.institutions = reconciliation.institutions; + corrected.local_relations = reconciliation.local_relations; + corrected.civic_system = Some(reconciliation.civic_system.into()); + Ok(corrected) +} + fn shortest_location_path( campaign: &Campaign, origin_location_id: &str, @@ -5833,8 +6542,10 @@ mod tests { struct DestinationElaborationModel { saw_branch_assumption_boundary: AtomicBool, + saw_balanced_civic_reconciliation: AtomicBool, civic_verification_calls: AtomicUsize, civic_verification_rejections: usize, + reconciliation_steals_non_civic_fact: bool, } struct OversizedQueryModel; @@ -6317,6 +7028,13 @@ mod tests { "scope":"branch_local", "evidence_receipt_ids":[], "discoverable_at_location_ids":["refuge"] + }, + { + "id":"fact:refuge_private_route", + "statement":"A concealed maintenance crawl reaches the closure seal from below.", + "scope":"branch_local", + "evidence_receipt_ids":[], + "discoverable_at_location_ids":["refuge"] } ], "populations":[{ @@ -6401,6 +7119,55 @@ mod tests { }) .to_string()) } + "destination_civic_reconciliation" => { + self.saw_balanced_civic_reconciliation.store( + request.model == MODEL_BALANCED + && request + .lived_stream + .contains("expensive world invention is complete and immutable") + && request.lived_stream.contains("refuge-duty-council") + && request.lived_stream.contains("VERIFIER REJECTION"), + Ordering::SeqCst, + ); + let frozen_projection: serde_json::Value = serde_json::from_str( + request + .lived_stream + .split_once("FROZEN CIVIC PROJECTION:\n") + .map(|(_, projection)| projection) + .ok_or_else(|| { + anyhow!("civic reconciliation lost its frozen projection") + })?, + )?; + let mut resident_civic_knowledge = + frozen_projection["resident_civic_knowledge"] + .as_array() + .ok_or_else(|| anyhow!("frozen projection lost resident knowledge"))? + .iter() + .map(|knowledge| { + serde_json::json!({ + "population_id":knowledge["population_id"], + "civic_fact_ids":knowledge["civic_fact_ids"], + }) + }) + .collect::>(); + let mut civic_system = frozen_projection["civic_system"].clone(); + civic_system["governing_institution_ids"] = + serde_json::json!(["refuge-duty-council", "refuge-stores-office"]); + if self.reconciliation_steals_non_civic_fact { + civic_system["public_resource_fact_ids"] = + serde_json::json!(["fact:refuge_private_route"]); + resident_civic_knowledge[0]["civic_fact_ids"] = + serde_json::json!(["fact:refuge_private_route"]); + } + Ok(serde_json::json!({ + "civic_facts":frozen_projection["candidate_owned_civic_facts"], + "resident_civic_knowledge":resident_civic_knowledge, + "institutions":frozen_projection["institutions"], + "local_relations":frozen_projection["local_relations"], + "civic_system":civic_system, + }) + .to_string()) + } "destination_civic_verification" => { let call = self.civic_verification_calls.fetch_add(1, Ordering::SeqCst); let accepted = call >= self.civic_verification_rejections; @@ -7771,8 +8538,10 @@ mod tests { { let model = Arc::new(DestinationElaborationModel { saw_branch_assumption_boundary: AtomicBool::new(false), + saw_balanced_civic_reconciliation: AtomicBool::new(false), civic_verification_calls: AtomicUsize::new(0), civic_verification_rejections: 0, + reconciliation_steals_non_civic_fact: false, }); let compiler = WorldCompiler::new(vault(), model.clone(), "flash", "pro"); let mut seed = private_actor_test_seed(); @@ -7820,8 +8589,10 @@ mod tests { { let model = Arc::new(DestinationElaborationModel { saw_branch_assumption_boundary: AtomicBool::new(false), + saw_balanced_civic_reconciliation: AtomicBool::new(false), civic_verification_calls: AtomicUsize::new(0), civic_verification_rejections: 1, + reconciliation_steals_non_civic_fact: false, }); let compiler = WorldCompiler::new(vault(), model.clone(), "flash", "pro"); let mut seed = private_actor_test_seed(); @@ -7852,6 +8623,12 @@ mod tests { "semantic_invalid" ); assert_eq!(verification_receipts[1].validation_result, "valid"); + assert!( + verification_receipts[1] + .source_receipt_ids + .iter() + .any(|source| source == verification_receipts[0].storage_key()) + ); assert_eq!( preview .expansion @@ -7863,12 +8640,125 @@ mod tests { ); } + #[tokio::test] + async fn destination_civic_rejection_uses_one_balanced_reconciliation_pass() { + let model = Arc::new(DestinationElaborationModel { + saw_branch_assumption_boundary: AtomicBool::new(false), + saw_balanced_civic_reconciliation: AtomicBool::new(false), + civic_verification_calls: AtomicUsize::new(0), + civic_verification_rejections: 2, + reconciliation_steals_non_civic_fact: false, + }); + let compiler = WorldCompiler::new(vault(), model.clone(), "flash", "pro"); + let mut seed = private_actor_test_seed(); + seed.player.location_id = "convoy-staging".into(); + seed.opening_narration = "The convoy waits in the rain.".into(); + let campaign = seed_to_campaign(seed, &[]).unwrap(); + + let (_, receipts) = compiler + .compile_destination( + &campaign, + "convoy-staging", + "a playable storm refuge with ordinary repair and admission procedure", + ) + .await + .unwrap(); + let civic_receipts = receipts + .iter() + .filter(|receipt| receipt.stage == "destination_civic_verification") + .collect::>(); + let compile_receipts = receipts + .iter() + .filter(|receipt| receipt.stage == "destination_compile") + .collect::>(); + let reconciliation_receipts = receipts + .iter() + .filter(|receipt| receipt.stage == "destination_civic_reconciliation") + .collect::>(); + + assert!( + model + .saw_balanced_civic_reconciliation + .load(Ordering::SeqCst) + ); + assert_eq!(compile_receipts.len(), 1); + assert_eq!(reconciliation_receipts.len(), 1); + assert_eq!(reconciliation_receipts[0].model, MODEL_BALANCED); + assert_eq!(civic_receipts.len(), 3); + assert!( + reconciliation_receipts[0] + .source_receipt_ids + .iter() + .any(|source| source == compile_receipts[0].storage_key()) + ); + assert!(civic_receipts[..2].iter().all(|rejected| { + reconciliation_receipts[0] + .source_receipt_ids + .iter() + .any(|source| source == rejected.storage_key()) + })); + assert!( + civic_receipts[2] + .source_receipt_ids + .iter() + .any(|source| source == reconciliation_receipts[0].storage_key()) + ); + assert_eq!(civic_receipts[0].validation_result, "semantic_invalid"); + assert_eq!(civic_receipts[1].validation_result, "semantic_invalid"); + assert_eq!(civic_receipts[2].validation_result, "valid"); + } + + #[tokio::test] + async fn civic_reconciliation_cannot_promote_a_protected_non_civic_fact_by_reference() { + let model = Arc::new(DestinationElaborationModel { + saw_branch_assumption_boundary: AtomicBool::new(false), + saw_balanced_civic_reconciliation: AtomicBool::new(false), + civic_verification_calls: AtomicUsize::new(0), + civic_verification_rejections: 2, + reconciliation_steals_non_civic_fact: true, + }); + let compiler = WorldCompiler::new(vault(), model.clone(), "flash", "pro"); + let mut seed = private_actor_test_seed(); + seed.player.location_id = "convoy-staging".into(); + seed.opening_narration = "The convoy waits in the rain.".into(); + let campaign = seed_to_campaign(seed, &[]).unwrap(); + + let error = compiler + .compile_destination( + &campaign, + "convoy-staging", + "a playable storm refuge with ordinary repair and admission procedure", + ) + .await + .unwrap_err(); + let failure = error + .downcast_ref::() + .expect("reconciliation failure must preserve its receipt"); + let reconciliation_receipts = failure + .model_receipts + .iter() + .filter(|receipt| receipt.stage == "destination_civic_reconciliation") + .collect::>(); + + assert!(error.to_string().contains( + "facts outside returned or committed civic authority: [\"fact:refuge_private_route\"]" + )); + assert_eq!(reconciliation_receipts.len(), 1); + assert_eq!( + reconciliation_receipts[0].validation_result, + "semantic_invalid" + ); + assert_eq!(model.civic_verification_calls.load(Ordering::SeqCst), 2); + } + #[tokio::test] async fn destination_compiler_rejects_semantically_empty_civic_machinery() { let model = Arc::new(DestinationElaborationModel { saw_branch_assumption_boundary: AtomicBool::new(false), + saw_balanced_civic_reconciliation: AtomicBool::new(false), civic_verification_calls: AtomicUsize::new(0), civic_verification_rejections: usize::MAX, + reconciliation_steals_non_civic_fact: false, }); let compiler = WorldCompiler::new(vault(), model.clone(), "flash", "pro"); let mut seed = private_actor_test_seed(); @@ -7890,7 +8780,38 @@ mod tests { .to_string() .contains("destination civic verifier rejected the candidate") ); - assert_eq!(model.civic_verification_calls.load(Ordering::SeqCst), 2); + let failure = error + .downcast_ref::() + .expect("terminal destination failure must preserve completed model receipts"); + let compile_receipts = failure + .model_receipts + .iter() + .filter(|receipt| receipt.stage == "destination_compile") + .collect::>(); + let civic_receipts = failure + .model_receipts + .iter() + .filter(|receipt| receipt.stage == "destination_civic_verification") + .collect::>(); + let reconciliation_receipts = failure + .model_receipts + .iter() + .filter(|receipt| receipt.stage == "destination_civic_reconciliation") + .collect::>(); + assert_eq!(compile_receipts.len(), 1); + assert_eq!(reconciliation_receipts.len(), 1); + assert_eq!(civic_receipts.len(), 4); + assert!( + civic_receipts + .iter() + .all(|receipt| receipt.validation_result == "semantic_invalid") + ); + assert_eq!(model.civic_verification_calls.load(Ordering::SeqCst), 4); + assert!( + model + .saw_balanced_civic_reconciliation + .load(Ordering::SeqCst) + ); } fn harrow_campaign() -> Campaign { diff --git a/crates/ghostlight-dungeon/src/main.rs b/crates/ghostlight-dungeon/src/main.rs index d6f1c3c..cc40e1d 100644 --- a/crates/ghostlight-dungeon/src/main.rs +++ b/crates/ghostlight-dungeon/src/main.rs @@ -15,7 +15,10 @@ use chrono::{DateTime, Utc}; use ghostlight_dungeon::domain::WorldCompilePreview; use ghostlight_dungeon::{ assessor::ActionAssessor, - compiler::{GestaltFissionRequest, OpeningRequest, OpeningSuggestion, WorldCompiler}, + compiler::{ + DestinationCompilationFailure, GestaltFissionRequest, OpeningRequest, OpeningSuggestion, + WorldCompiler, + }, domain::{ ActionIntent, Campaign, DestinationCompilationPreview, GestaltFissionPreview, RejectedProposalReceipt, WorldCommand, @@ -4023,7 +4026,23 @@ async fn compile_destination( })) .into_response() } - Err(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response(), + Err(error) => { + if let Some(failure) = error.downcast_ref::() + && let Err(persistence_error) = runtime + .store + .persist_model_stage_receipts(&failure.model_receipts) + { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + format!( + "{}; model-receipt persistence failed: {persistence_error}", + failure.message + ), + ) + .into_response(); + } + (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response() + } } } @@ -6527,6 +6546,7 @@ mod tests { model_status: ModelRuntimeStatus { provider: "fixture".into(), fast_model: "fixture".into(), + balanced_model: "fixture".into(), capable_model: "fixture".into(), readiness: "ready".into(), }, diff --git a/crates/ghostlight-dungeon/src/mesh.rs b/crates/ghostlight-dungeon/src/mesh.rs index 1d03017..5ef6858 100644 --- a/crates/ghostlight-dungeon/src/mesh.rs +++ b/crates/ghostlight-dungeon/src/mesh.rs @@ -686,6 +686,7 @@ mod tests { ModelRuntimeStatus { provider: "fixture".into(), fast_model: "fixture-fast".into(), + balanced_model: "fixture-balanced".into(), capable_model: "fixture-capable".into(), readiness: "ready".into(), } diff --git a/crates/ghostlight-dungeon/src/model.rs b/crates/ghostlight-dungeon/src/model.rs index 7707c88..5c2c620 100644 --- a/crates/ghostlight-dungeon/src/model.rs +++ b/crates/ghostlight-dungeon/src/model.rs @@ -7,6 +7,7 @@ use std::time::Instant; use zeroize::Zeroizing; pub const MODEL_FAST: &str = "ghostlight.fast.v1"; +pub const MODEL_BALANCED: &str = "ghostlight.balanced.v1"; pub const MODEL_CAPABLE: &str = "ghostlight.capable.v1"; #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] @@ -14,6 +15,7 @@ pub const MODEL_CAPABLE: &str = "ghostlight.capable.v1"; pub struct ModelRuntimeStatus { pub provider: String, pub fast_model: String, + pub balanced_model: String, pub capable_model: String, pub readiness: String, } @@ -1063,6 +1065,17 @@ mod tests { openrouter_request_body(&capable, "stealth/ox-alpha")["reasoning"]["effort"], "medium" ); + + let mut balanced = capable; + balanced.model = MODEL_BALANCED.into(); + assert_eq!( + openrouter_request_body(&balanced, "stealth/ox-alpha")["reasoning"]["effort"], + "medium" + ); + assert_eq!( + resolve_model(&balanced, "fixture-fast", "fixture-capable"), + "fixture-capable" + ); } #[test] @@ -1431,7 +1444,7 @@ fn openrouter_request_body(request: &ModelStageRequest, resolved_model: &str) -> "messages": [{"role": "user", "content": request.lived_stream}], "stream": false, "reasoning": { - "effort": if request.model == MODEL_CAPABLE { "medium" } else { "low" }, + "effort": if matches!(request.model.as_str(), MODEL_BALANCED | MODEL_CAPABLE) { "medium" } else { "low" }, "exclude": true } }); @@ -1470,6 +1483,7 @@ impl ModelPort for OpenRouterPort { fn resolve_model(request: &ModelStageRequest, fast_model: &str, capable_model: &str) -> String { match request.model.as_str() { MODEL_FAST => fast_model.to_owned(), + MODEL_BALANCED => capable_model.to_owned(), MODEL_CAPABLE => capable_model.to_owned(), explicit => explicit.to_owned(), } diff --git a/crates/ghostlight-dungeon/src/model_connector.rs b/crates/ghostlight-dungeon/src/model_connector.rs index 0b1f9b1..77464c7 100644 --- a/crates/ghostlight-dungeon/src/model_connector.rs +++ b/crates/ghostlight-dungeon/src/model_connector.rs @@ -15,12 +15,14 @@ use tokio::sync::Semaphore; use zeroize::Zeroizing; use crate::model::{ - MODEL_CAPABLE, MODEL_FAST, ModelPort, ModelProviderOutput, ModelStageRequest, ModelTokenUsage, + MODEL_BALANCED, MODEL_CAPABLE, MODEL_FAST, ModelPort, ModelProviderOutput, ModelStageRequest, + ModelTokenUsage, }; const MAX_FRAME_BYTES: usize = 1_052_672; const REQUEST_EXPIRY: Duration = Duration::from_secs(150); const FAST_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(120); +const BALANCED_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(180); const CAPABLE_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(300); #[derive(Clone)] @@ -29,6 +31,7 @@ pub struct CodexConnectorModelPort { request_gate: std::sync::Arc, caller_runtime_id: String, fast_model: String, + balanced_model: String, capable_model: String, } @@ -38,6 +41,7 @@ impl CodexConnectorModelPort { connection_key: String, caller_runtime_id: impl Into, fast_model: impl Into, + balanced_model: impl Into, capable_model: impl Into, max_concurrent_requests: usize, ) -> Result { @@ -54,6 +58,7 @@ impl CodexConnectorModelPort { request_gate: std::sync::Arc::new(Semaphore::new(max_concurrent_requests)), caller_runtime_id, fast_model: fast_model.into(), + balanced_model: balanced_model.into(), capable_model: capable_model.into(), }) } @@ -63,6 +68,7 @@ impl CodexConnectorModelPort { path: impl AsRef, caller_runtime_id: impl Into, fast_model: impl Into, + balanced_model: impl Into, capable_model: impl Into, max_concurrent_requests: usize, ) -> Result { @@ -77,6 +83,7 @@ impl CodexConnectorModelPort { connection_key.to_owned(), caller_runtime_id, fast_model, + balanced_model, capable_model, max_concurrent_requests, ) @@ -86,6 +93,7 @@ impl CodexConnectorModelPort { let request_id = format!("ghostlight-model-{}", uuid::Uuid::new_v4()); let resolved_model = match request.model.as_str() { MODEL_FAST => self.fast_model.clone(), + MODEL_BALANCED => self.balanced_model.clone(), MODEL_CAPABLE => self.capable_model.clone(), explicit => explicit.to_string(), }; @@ -99,7 +107,7 @@ impl CodexConnectorModelPort { text: request.lived_stream.clone(), }]; provider_request.reasoning_effort = Some( - if request.model == MODEL_CAPABLE { + if matches!(request.model.as_str(), MODEL_BALANCED | MODEL_CAPABLE) { "medium" } else { "low" @@ -156,10 +164,10 @@ impl ModelPort for CodexConnectorModelPort { } fn attempt_timeout(&self, request: &ModelStageRequest) -> Duration { - if request.model == MODEL_CAPABLE { - CAPABLE_ATTEMPT_TIMEOUT - } else { - FAST_ATTEMPT_TIMEOUT + match request.model.as_str() { + MODEL_BALANCED => BALANCED_ATTEMPT_TIMEOUT, + MODEL_CAPABLE => CAPABLE_ATTEMPT_TIMEOUT, + _ => FAST_ATTEMPT_TIMEOUT, } } } @@ -618,6 +626,7 @@ mod tests { "connector-test-key".to_string(), "ghostlight-dungeon-yggdrasil", "gpt-5.6-luna", + "gpt-5.6-terra", "gpt-5.6-luna", 1, )?; @@ -625,7 +634,7 @@ mod tests { let output = port .run_observed(&ModelStageRequest { stage: "test_interpreter".to_string(), - model: MODEL_CAPABLE.to_string(), + model: MODEL_BALANCED.to_string(), snapshot_binding: "revision:7".to_string(), lived_stream: "Return the typed answer.".to_string(), output_schema: Some(serde_json::json!({ @@ -640,7 +649,7 @@ mod tests { }) .await?; let invocation = server.join().expect("server thread")?; - assert_eq!(invocation.request.model, "gpt-5.6-luna"); + assert_eq!(invocation.request.model, "gpt-5.6-terra"); assert_eq!(invocation.request.max_output_tokens, Some(512)); assert_eq!( invocation.request.output_format_name.as_deref(), @@ -708,6 +717,7 @@ mod tests { "bounded-test-key".to_string(), "ghostlight-test", "gpt-5.6-luna", + "gpt-5.6-terra", "gpt-5.6-sol", 1, )?; @@ -723,6 +733,10 @@ mod tests { }; assert_eq!(port.attempt_timeout(&request), CAPABLE_ATTEMPT_TIMEOUT); + request.stage = "destination_civic_reconciliation".to_string(); + request.model = MODEL_BALANCED.to_string(); + assert_eq!(port.attempt_timeout(&request), BALANCED_ATTEMPT_TIMEOUT); + request.stage = "cell_interpreter".to_string(); request.model = MODEL_FAST.to_string(); assert_eq!(port.attempt_timeout(&request), FAST_ATTEMPT_TIMEOUT); diff --git a/crates/ghostlight-dungeon/src/model_runtime.rs b/crates/ghostlight-dungeon/src/model_runtime.rs index 2d502ec..6c55183 100644 --- a/crates/ghostlight-dungeon/src/model_runtime.rs +++ b/crates/ghostlight-dungeon/src/model_runtime.rs @@ -13,6 +13,7 @@ use std::{ pub struct ModelRuntimeSelection { pub provider: String, pub fast_model: String, + pub balanced_model: String, pub capable_model: String, pub credential_path: PathBuf, pub connector_endpoint: SocketAddr, @@ -25,17 +26,38 @@ impl ModelRuntimeSelection { let provider = std::env::var("GHOSTLIGHT_MODEL_PROVIDER") .unwrap_or_else(|_| "deepseek".into()) .to_ascii_lowercase(); - let (default_fast_model, default_capable_model, default_secret_name) = - match provider.as_str() { - "deepseek" => ("deepseek-v4-flash", "deepseek-v4-pro", "deepseek.dpapi"), - "openrouter" => ("stealth/ox-alpha", "stealth/ox-alpha", "openrouter.key"), - "codex-connector" => ("gpt-5.6-luna", "gpt-5.6-luna", "codex-connector.key"), - unsupported => bail!("unsupported model provider {unsupported}"), - }; + let ( + default_fast_model, + default_balanced_model, + default_capable_model, + default_secret_name, + ) = match provider.as_str() { + "deepseek" => ( + "deepseek-v4-flash", + "deepseek-v4-pro", + "deepseek-v4-pro", + "deepseek.dpapi", + ), + "openrouter" => ( + "stealth/ox-alpha", + "stealth/ox-alpha", + "stealth/ox-alpha", + "openrouter.key", + ), + "codex-connector" => ( + "gpt-5.6-luna", + "gpt-5.6-terra", + "gpt-5.6-luna", + "codex-connector.key", + ), + unsupported => bail!("unsupported model provider {unsupported}"), + }; Ok(Self { provider, fast_model: std::env::var("GHOSTLIGHT_MODEL_FAST") .unwrap_or_else(|_| default_fast_model.into()), + balanced_model: std::env::var("GHOSTLIGHT_MODEL_BALANCED") + .unwrap_or_else(|_| default_balanced_model.into()), capable_model: std::env::var("GHOSTLIGHT_MODEL_CAPABLE") .unwrap_or_else(|_| default_capable_model.into()), credential_path: std::env::var_os("GHOSTLIGHT_MODEL_CREDENTIAL") @@ -65,6 +87,7 @@ impl ModelRuntimeSelection { ModelRuntimeStatus { provider: self.provider.clone(), fast_model: self.fast_model.clone(), + balanced_model: self.balanced_model.clone(), capable_model: self.capable_model.clone(), readiness: readiness.into(), } @@ -90,6 +113,7 @@ impl ModelRuntimeSelection { &self.credential_path, self.runtime_id.clone(), self.fast_model.clone(), + self.balanced_model.clone(), self.capable_model.clone(), self.connector_max_concurrent_requests, )?), diff --git a/docs/architecture/ghostlight-dungeon-mvp.md b/docs/architecture/ghostlight-dungeon-mvp.md index b2987eb..fb70d73 100644 --- a/docs/architecture/ghostlight-dungeon-mvp.md +++ b/docs/architecture/ghostlight-dungeon-mvp.md @@ -426,7 +426,8 @@ performs her own exact CAS. - **Model transport:** Ghostlight defines an async stage-runner port. Epiphany implements it with `EpiphanyModelRequest`, Codex/OpenAI-compatible auth, runtime jobs, and private model-event recovery. GhostlightDungeon implements - it with the selected provider port and logical fast/capable model classes. + it with the selected provider port and logical fast/balanced/capable model + classes. - **Receipts:** Ghostlight owns portable stage and terminal fields. Epiphany's adapter augments them with `EpiphanyReasoningBasis`, sealed decision contexts, brake evidence, and runtime-spine identities. @@ -926,12 +927,15 @@ acceptance can reject stages that spend heavily while producing no meaningful appraisal, state proposal, or player-facing consequence. Model allocation is provider-neutral inside Ghostlight. Stages request the -logical classes `ghostlight.fast.v1` or `ghostlight.capable.v1`; the selected -provider port alone maps those classes to physical model IDs. Receipts record -the resolved provider and physical model so a routing change cannot masquerade -as the same inference. +logical classes `ghostlight.fast.v1`, `ghostlight.balanced.v1`, or +`ghostlight.capable.v1`; the selected provider port alone maps those classes to +physical model IDs. The fast class owns high-volume work, the balanced class +owns bounded reconciliation and routine elaboration, and the capable class owns +frontier invention and editorial judgment. Receipts record the resolved +provider and physical model so a routing change cannot masquerade as the same +inference. -The `epiphany-codex` profile maps both logical classes to an operator-admitted +The `epiphany-codex` profile maps all three logical classes to operator-admitted Codex model through Epiphany's independently supervised model connector. The Ghostlight port constructs the native model request, exact output schema, reasoning-effort hint, maximum output budget, and a stable prompt-cache key; @@ -944,15 +948,15 @@ receipts. It does not see campaign stores, run its Mind or swarm, interpret results, or admit world state. Ghostlight cannot read the Codex credential and the connector cannot commit a campaign mutation. -OpenRouter remains a supported test profile. It can map both classes to -`stealth/ox-alpha`, using low reasoning for fast stages and high reasoning for -capable stages. OpenRouter is instructed to exclude reasoning from its -response, and the decoder never reads or retains that field. +OpenRouter remains a supported test profile. It maps the balanced class to its +configured capable model, using low reasoning for fast stages and medium +reasoning for balanced or capable stages. OpenRouter is instructed to exclude +reasoning from its response, and the decoder never reads or retains that field. DeepSeek remains a supported provider profile: its flash model serves the fast -class and its pro model serves the capable class with thinking disabled. A -provider switch changes configuration and credentials, not compiler, Persona, -Interpreter, scheduler, or kernel ownership. +class and its pro model serves the balanced and capable classes with thinking +disabled. A provider switch changes configuration and credentials, not +compiler, Persona, Interpreter, scheduler, or kernel ownership. ## Hosting and security diff --git a/notes/ghostlight-current-system-map.md b/notes/ghostlight-current-system-map.md index 7ce7085..a32c8a7 100644 --- a/notes/ghostlight-current-system-map.md +++ b/notes/ghostlight-current-system-map.md @@ -249,6 +249,13 @@ neither side. Yggdrasil runs CodexConnector as the independent systemd ordering edge to that daemon and cannot build, configure, restart, select, or roll it back. +Ghostlight exposes three provider-neutral logical classes. `ghostlight.fast.v1` +owns high-volume cells and retrieval, `ghostlight.balanced.v1` owns bounded +reconciliation and routine elaboration, and `ghostlight.capable.v1` owns +frontier invention and editorial judgment. Only the selected `ModelPort` maps +those classes to physical models or assigns their attempt deadlines; compilers +and harnesses never pass physical model IDs as logical stage authority. + Vault availability is typed at the provider boundary. When the configured Vault cannot return evidence, SessionZeroKernel returns the exact draft to `drafting`, records one stable resolved material blocker, and publishes no @@ -910,11 +917,22 @@ the office, procedure, or answer. Inhabited proposals must expose current authority, selection or succession, public resources, and redress through a typed civic manifest whose exact fact references are shared by each resident population. A separate model stage verifies their semantic legibility; it may -reject but cannot rewrite or commit the proposal. The kernel rebinds that -verdict to the exact candidate before mutation admission. Accepted civic -systems persist as versioned component state, so a later pass receives the -current institutions, populations, facts, and relations and must extend that -apparatus instead of inventing a parallel government. +reject but cannot rewrite or commit the proposal. One false verdict is +independently rechecked against the same frozen candidate. A sustained rejection +invokes one balanced reconciliation over a civic-only replacement schema: +candidate-owned civic facts, resident civic fact references, candidate +institutions, local political relations, and the civic manifest. Geography, +routes, migration, population identity, non-civic facts, branch assumptions, +gaps, civic version, and semantic receipt binding have no writable field in +that pass. The merged candidate must survive the ordinary destination validators +and a fresh independent verdict; the expensive `destination_compile` stage is +not rerun. The kernel rebinds the accepted verdict to the exact candidate before +mutation admission. Completed compiler, reconciliation, and verifier receipts +survive terminal failure and are persisted by both the strategic harness and +the Dungeon API. Accepted civic systems persist as versioned component state, +so a later pass receives the current institutions, populations, facts, and +relations and must extend that apparatus instead of inventing a parallel +government. Both destination paths synthesize the smallest compatible branch-local routes, geometry, people, supplies, procedures, capacity, responsibility, and doctrine