diff --git a/crates/ghostlight-dungeon/src/assessor.rs b/crates/ghostlight-dungeon/src/assessor.rs index 0a421fa..f125988 100644 --- a/crates/ghostlight-dungeon/src/assessor.rs +++ b/crates/ghostlight-dungeon/src/assessor.rs @@ -1,5 +1,8 @@ use crate::{ - domain::{ActionAssessment, ActionIntent, Campaign, ContextModifier, WorldEffectDelta}, + domain::{ + ActionAssessment, ActionIntent, Campaign, ContextModifier, MAX_POSTURE_CHARS, + WorldEffectDelta, + }, model::{ ModelPort, ModelProviderAttemptReceipt, ModelStageReceipt, ModelStageRequest, ModelTokenUsage, run_validated_stage, @@ -1531,8 +1534,11 @@ fn constrain_effect_schema( .get_mut("institution_postures") .ok_or_else(|| anyhow!("assessment effect schema omitted institution_postures"))?; constrain_map_keys(institution_postures, &institution_ids)?; - institution_postures["additionalProperties"] = - serde_json::json!({"type":"string","minLength":1}); + institution_postures["additionalProperties"] = serde_json::json!({ + "type":"string", + "minLength":1, + "maxLength":MAX_POSTURE_CHARS + }); let mut unavailable_lanes = Vec::new(); if !knowledge_available { unavailable_lanes.push("actor_knowledge_additions"); @@ -1777,7 +1783,7 @@ fn project_effect_schema_to_mutation_entries( "type":"object", "properties":{ "institution_id":typed_string_enum(&institution_ids), - "posture":{"type":"string","minLength":1} + "posture":{"type":"string","minLength":1,"maxLength":MAX_POSTURE_CHARS} }, "required":["institution_id","posture"], "additionalProperties":false @@ -2753,13 +2759,13 @@ pub(crate) fn validate_effect( "one outcome cannot both advance and reduce the same clock: {id}" )); } - if let Some((id, posture)) = effect - .institution_postures - .iter() - .find(|(id, posture)| !campaign.institutions.contains_key(*id) || posture.trim().is_empty()) - { + if let Some((id, posture)) = effect.institution_postures.iter().find(|(id, posture)| { + !campaign.institutions.contains_key(*id) + || posture.trim().is_empty() + || posture.chars().count() > MAX_POSTURE_CHARS + }) { return Err(anyhow!( - "outcome institution posture must name an existing institution and a non-empty posture: {id}={posture:?}" + "outcome institution posture must name an existing institution and contain one to {MAX_POSTURE_CHARS} characters: {id}={posture:?}" )); } Ok(()) @@ -4009,6 +4015,10 @@ mod tests { effect["institution_postures"]["items"]["properties"]["posture"]["minLength"], 1 ); + assert_eq!( + effect["institution_postures"]["items"]["properties"]["posture"]["maxLength"], + MAX_POSTURE_CHARS + ); assert_eq!( effect["actor_relationship_updates"]["items"]["properties"]["relationship"]["minLength"], 1 @@ -4307,6 +4317,23 @@ mod tests { assert!(error.contains("at least one")); } + #[test] + fn assessment_effect_rejects_an_oversized_posture() { + let campaign = crate::resolution::tests::campaign(2, 1); + let acting = campaign.actors["player"].clone(); + let institution_id = campaign.institutions.keys().next().unwrap().clone(); + let effect = WorldEffectDelta { + institution_postures: std::collections::BTreeMap::from([( + institution_id, + "x".repeat(MAX_POSTURE_CHARS + 1), + )]), + ..WorldEffectDelta::default() + }; + + let error = validate_effect(&campaign, &acting, &effect, "nothing changes").unwrap_err(); + assert!(error.to_string().contains("one to 460 characters")); + } + #[test] fn assessment_effect_rejects_invalid_or_conflicting_clock_reduction() { let mut campaign = crate::resolution::tests::campaign(0, 1); diff --git a/crates/ghostlight-dungeon/src/compiler.rs b/crates/ghostlight-dungeon/src/compiler.rs index 1ede6fe..2cd59cb 100644 --- a/crates/ghostlight-dungeon/src/compiler.rs +++ b/crates/ghostlight-dungeon/src/compiler.rs @@ -4,8 +4,8 @@ use crate::{ AgencySubjectKind, BranchOrigin, Campaign, CivicSystemManifest, DestinationCompilationPreview, EvidenceCoverage, EvidenceUseLane, FactScope, GestaltMemberDelta, GestaltPersonaState, InstitutionState, LocalityElaboration, - LocalityElaborationPreview, Location, Route, VaultEvidenceReceipt, WorldClock, - WorldCompilePreview, WorldFact, + LocalityElaborationPreview, Location, MAX_POSTURE_CHARS, Route, VaultEvidenceReceipt, + WorldClock, WorldCompilePreview, WorldFact, }, model::{ ModelPort, ModelStageReceipt, ModelStageRequest, run_validated_stage, @@ -4020,6 +4020,7 @@ pub fn validate_region_expansion( for institution in &expansion.institutions { if institution.name.trim().is_empty() || institution.posture.trim().is_empty() + || institution.posture.chars().count() > MAX_POSTURE_CHARS || institution.goals.is_empty() || institution .resources @@ -5188,6 +5189,15 @@ fn validate_campaign(c: &Campaign, require_dematerialized_members: bool) -> Resu } crate::resolution::validate_policy(&c.resolution_policy)?; crate::resolution::validate_pins(c, &c.resolution_pins)?; + if let Some(institution) = c.institutions.values().find(|institution| { + institution.posture.trim().is_empty() + || institution.posture.chars().count() > MAX_POSTURE_CHARS + }) { + return Err(anyhow!( + "institution {} posture must contain one to {MAX_POSTURE_CHARS} characters", + institution.id + )); + } let canonical_subjects = c .actors .keys() @@ -6757,6 +6767,16 @@ mod tests { assert!(error.contains("faction-0000")); } + #[test] + fn campaign_seed_rejects_an_oversized_institution_posture() { + let mut campaign = crate::resolution::tests::campaign(2, 1); + let institution = campaign.institutions.values_mut().next().unwrap(); + institution.posture = "x".repeat(MAX_POSTURE_CHARS + 1); + + let error = validate_campaign_seed(&campaign).unwrap_err(); + assert!(error.to_string().contains("one to 460 characters")); + } + #[test] fn approved_relationships_resolve_party_members_and_materialize_stable_private_anchors() { let mut sable = CharacterDraft { diff --git a/crates/ghostlight-dungeon/src/domain.rs b/crates/ghostlight-dungeon/src/domain.rs index dae6fe7..75934da 100644 --- a/crates/ghostlight-dungeon/src/domain.rs +++ b/crates/ghostlight-dungeon/src/domain.rs @@ -750,6 +750,8 @@ pub struct GestaltReaction { pub deliberate_silence: bool, } +pub const MAX_POSTURE_CHARS: usize = 460; + #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] pub struct InstitutionState { pub id: String, diff --git a/crates/ghostlight-dungeon/src/kernel.rs b/crates/ghostlight-dungeon/src/kernel.rs index e6c271c..facab06 100644 --- a/crates/ghostlight-dungeon/src/kernel.rs +++ b/crates/ghostlight-dungeon/src/kernel.rs @@ -2713,10 +2713,10 @@ fn apply_strategic_tick_plan( "strategic institution posture is empty".into(), )); } - if action.posture.chars().count() > 240 { - return Err(KernelError::Invalid( - "strategic institution posture exceeds 240 characters".into(), - )); + if action.posture.chars().count() > MAX_POSTURE_CHARS { + return Err(KernelError::Invalid(format!( + "strategic institution posture exceeds {MAX_POSTURE_CHARS} characters" + ))); } if action .location_ids diff --git a/crates/ghostlight-dungeon/src/persona.rs b/crates/ghostlight-dungeon/src/persona.rs index 8f97f4b..5ab2e89 100644 --- a/crates/ghostlight-dungeon/src/persona.rs +++ b/crates/ghostlight-dungeon/src/persona.rs @@ -1347,11 +1347,12 @@ impl CellProjectionEngine { "Each action carries an effects object whose exact subject-specific lane keys are supplied by the schema. Across scalar effects and every expanded activity scope, an action has one to four exact effects total. Scalar lanes contain one typed effect. An activities lane is one object keyed by chosen activity kinds; each non-null kind contains an array of one to four separate exact target-and-location scopes. Preserve repeated uses of one activity kind as separate scopes when their targets or locations differ; never union scopes across distinct locations or audiences. Top-level lane names are stable across subjects: use null for a null-only unavailable lane and for any schema-required optional lane or activity key the subject does not use. A non-null lane or activity key absent from its exact schema is structurally unavailable: do not emit it and do not invent a destination. Pressure and migration lanes each have one slot. Preserve every means of one chosen course in one action. With relocation, activities at the exact snapshot location occur before departure and activities at the exact admitted destination occur after arrival; activity effects inside each location phase are an atomic set. Field and array order is not chronology. Never split one subject's single choice into multiple actions. ", "Use gestalt_activities or member_activities for concrete attempts that do not themselves change pressure. A cohesive Gestalt coordinating its own unnamed internal members uses coordinate with an empty target_subject_ids list; do not invent its containing population, a distant population, or another canonical subject as the target of internal coordination. Cite the smallest exact set of state_references that materially supports each attempt; the permission list is an upper bound, not a checklist to echo. ", "target_subject_ids and location_ids must come from that exact subject's permissions. activity_targets is the exact canonical target map: each key is the authoritative ID and each value supplies the target's name and current canonical locations. Use an ID only when the Persona addresses that named target, never merely because the ID is permitted. If an addressed person or role has no matching activity_targets entry, it is not a canonical target in this slice. reachable_destinations maps exact actor-movement destination IDs to names. migration_destinations maps exact population destination IDs to names and locations. When the Persona chooses to go to a canonical target, compare the target's current locations with the acting subject's current location and exact reachable destinations; never guess a destination from an opaque ID. Every activity has at most four unique target_subject_ids; choose the four most causally relevant when more permitted subjects are involved. A member activity uses exactly the member's source_location_id. Internal work is prepare with no targets. A local investigate may have no target and use the exact current location to seek information from the environment or an unnamed ordinary role; asking an unnamed clerk or dock master for facts maps here and records only the inquiry, never a reply or discovery. A local communicate may likewise have no target at the exact current location when the Persona speaks, sends, offers, asks permission, or notifies an unnamed ordinary role; it records only the source's outgoing attempt, never a listener, reply, acceptance, or outcome. Communication with a canonical subject requires that exact target ID. When one utterance addresses canonical subjects and an unnamed public audience, emit one communicate activity with those exact target IDs and place its admitted public reach in public_channels; do not invent a second communicate lane. Never substitute a containing population, related institution, or merely permitted ID for an unnamed role. ", - "Write intended_effect as the affirmative attempted acts, never the purpose, restraint, condition to preserve, hoped-for outcome, or target response. Keep purpose and restraint in intent. Respecting choice, declining coercion, leaving state unchanged, or waiting for another subject does not add a typed effect unless the Persona separately chooses an observable act. Merely waiting, watching, staying, holding position, or remaining ready is attributed inaction, not prepare. prepare requires concrete work on a bounded arrangement, repair, resource, or capability-backed readiness change. Institution posture must be a specific materially new commitment or withholding of at most 240 characters. already_committed_posture is state already in force: maintaining, continuing, or restating it is inaction and must not emit an institution action. Gestalt pressure_resolutions copy exact current_pressures; additions are new unresolved constraints, never completed actions. Use only permitted state references. public_channels means durable publication of this attempt through exact allowed_persistent_publication_channels; it is not a perception method or ordinary local speech. Use [] when that exact list is empty. ", + "Write intended_effect as the affirmative attempted acts, never the purpose, restraint, condition to preserve, hoped-for outcome, or target response. Keep purpose and restraint in intent. Respecting choice, declining coercion, leaving state unchanged, or waiting for another subject does not add a typed effect unless the Persona separately chooses an observable act. Merely waiting, watching, staying, holding position, or remaining ready is attributed inaction, not prepare. prepare requires concrete work on a bounded arrangement, repair, resource, or capability-backed readiness change. Institution posture must be a specific materially new commitment or withholding of at most {} characters. already_committed_posture is state already in force: maintaining, continuing, or restating it is inaction and must not emit an institution action. Gestalt pressure_resolutions copy exact current_pressures; additions are new unresolved constraints, never completed actions. Use only permitted state references. public_channels means durable publication of this attempt through exact allowed_persistent_publication_channels; it is not a perception method or ordinary local speech. Use [] when that exact list is empty. ", "A population that chooses to board, depart, or relocate together to one supplied migration_destinations key emits gestalt_migration; do not reduce it to prepare. It relocates only that exact population leaf and never implies a named member traveled. A named member who chooses to board, depart, travel, or join a supplied destination emits member_migration; use prepare only while departure remains unchosen. ", "A population or arena cannot migrate a person. Runtime binds identity and effect owner IDs from subject_id. Do not emit institution_id, gestalt_id, actor_id, or member_id inside effect. An inaction means that exact subject takes no strategic action in this horizon. Record only a subject that explicitly holds, waits without making another attempt, or merely continues already_committed_posture in the Persona turn as an inaction, using its exact subject_id and a concrete reason of at most 160 characters. Waiting for the result of an action, withholding a different possible action, or declining one option after choosing another does not make the chosen action an inaction. Never invent an inaction for an unvoiced subject or use absence of a Persona decision as a reason. Inactions share the same count limit stated for actions. A subject cannot appear in both actions and inactions. When nobody acts, actions is empty and inactions must still contain at least one exact attributed decision from the Persona turn." ), - slice.max_actions + slice.max_actions, + crate::domain::MAX_POSTURE_CHARS ); let permission_guidance = format!("{permission_guidance} {CELL_ACTIVITY_CLASSIFICATION_GUIDANCE}"); @@ -2376,11 +2377,12 @@ fn validate_constituent_effect( subject.subject_id )); } - if posture.chars().count() > 240 { + if posture.chars().count() > crate::domain::MAX_POSTURE_CHARS { return Err(anyhow!( - "institution {} proposed a posture of {} characters; the exact maximum is 240", + "institution {} proposed a posture of {} characters; the exact maximum is {}", subject.subject_id, - posture.chars().count() + posture.chars().count(), + crate::domain::MAX_POSTURE_CHARS )); } if current.trim().eq_ignore_ascii_case(posture.trim()) { @@ -2747,7 +2749,7 @@ fn exact_constituent_effect_bundle_schema(subject: &CellConstituentSlice) -> ser "additionalProperties":false, "required":["posture","location_ids"], "properties":{ - "posture":{"type":"string","minLength":1,"maxLength":240}, + "posture":{"type":"string","minLength":1,"maxLength":crate::domain::MAX_POSTURE_CHARS}, "location_ids":exact_string_array_schema(&subject.location_ids, 0, 4) } })), @@ -3739,6 +3741,11 @@ mod tests { assert!(!CELL_APPRAISAL_OUTPUT_CONTRACT.contains("\"gestalt_id\"")); assert!(!CELL_APPRAISAL_OUTPUT_CONTRACT.contains("\"actor_id\"")); assert!(!CELL_APPRAISAL_OUTPUT_CONTRACT.contains("\"member_id\"")); + assert_eq!( + exact_constituent_effect_bundle_schema(&fixture_cell_slice().constituents[0]) + .pointer("/properties/institution/anyOf/0/properties/posture/maxLength"), + Some(&serde_json::json!(crate::domain::MAX_POSTURE_CHARS)) + ); } #[test] @@ -4752,6 +4759,28 @@ mod tests { projector_context["constituents"][0]["pressures"][0], "the vote is near" ); + + validate_constituent_effect( + &slice.constituents[0], + &StrategicCellEffect::Institution { + institution_id: "faction-06".into(), + posture: "x".repeat(crate::domain::MAX_POSTURE_CHARS), + location_ids: vec!["forum".into()], + }, + None, + ) + .unwrap(); + let error = validate_constituent_effect( + &slice.constituents[0], + &StrategicCellEffect::Institution { + institution_id: "faction-06".into(), + posture: "x".repeat(crate::domain::MAX_POSTURE_CHARS + 1), + location_ids: vec!["forum".into()], + }, + None, + ) + .unwrap_err(); + assert!(error.to_string().contains("exact maximum is 460")); } #[tokio::test] diff --git a/crates/ghostlight-dungeon/src/resolution.rs b/crates/ghostlight-dungeon/src/resolution.rs index 5138022..289077d 100644 --- a/crates/ghostlight-dungeon/src/resolution.rs +++ b/crates/ghostlight-dungeon/src/resolution.rs @@ -2298,7 +2298,7 @@ fn validate_cell_proposal( if institution_id != &proposal.subject_id || !campaign.institutions.contains_key(institution_id) || posture.trim().is_empty() - || posture.chars().count() > 240 + || posture.chars().count() > MAX_POSTURE_CHARS || !substantive_text_change( &campaign.institutions[institution_id].posture, posture, diff --git a/crates/ghostlight-dungeon/src/transition.rs b/crates/ghostlight-dungeon/src/transition.rs index 0974c39..028945e 100644 --- a/crates/ghostlight-dungeon/src/transition.rs +++ b/crates/ghostlight-dungeon/src/transition.rs @@ -1,4 +1,6 @@ -use crate::domain::{CivicSystemManifest, FactScope, OutcomeBand, StrategicOutcomeBand}; +use crate::domain::{ + CivicSystemManifest, FactScope, MAX_POSTURE_CHARS, OutcomeBand, StrategicOutcomeBand, +}; use anyhow::{Result, anyhow}; use chrono::{DateTime, Utc}; use schemars::{JsonSchema, schema_for}; @@ -4181,7 +4183,12 @@ impl WorldMutation { _ => {} }, ChangeMemory { memory_id, .. } => nonempty("memory id", memory_id)?, - ChangePosture { posture, .. } => nonempty("posture", posture)?, + ChangePosture { posture, .. } => { + nonempty("posture", posture)?; + if posture.chars().count() > MAX_POSTURE_CHARS { + return Err(anyhow!("posture exceeds {MAX_POSTURE_CHARS} characters")); + } + } ChangePopulationMembership { operation, source_population, @@ -4951,6 +4958,17 @@ mod tests { ); } + #[test] + fn posture_mutation_rejects_an_oversized_canonical_value() { + let mutation = WorldMutation::ChangePosture { + subject: subject(SubjectKind::Institution, "institution:clinic"), + posture: "x".repeat(MAX_POSTURE_CHARS + 1), + }; + + let error = mutation.validate_local_shape().unwrap_err(); + assert!(error.to_string().contains("exceeds 460 characters")); + } + fn permit( id: &str, operation: WorldMutationOperation,