From cd4f712b1f57b3c6780a71cafa82a43f8c87028f Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 24 Aug 2026 06:30:07 +0200 Subject: [PATCH] Bind NPC initiative to one reaction wave --- crates/ghostlight-dungeon/src/kernel.rs | 129 ++++++++++++++++++++ docs/architecture/ghostlight-dungeon-mvp.md | 6 + notes/fresh-workspace-handoff.md | 5 + notes/ghostlight-current-system-map.md | 4 + 4 files changed, 144 insertions(+) diff --git a/crates/ghostlight-dungeon/src/kernel.rs b/crates/ghostlight-dungeon/src/kernel.rs index 86d805b..b68b13f 100644 --- a/crates/ghostlight-dungeon/src/kernel.rs +++ b/crates/ghostlight-dungeon/src/kernel.rs @@ -1311,6 +1311,7 @@ fn execute( Utc::now(), ) .map_err(|error| KernelError::Invalid(error.to_string()))?; + campaign.pending_world_proposals.clear(); for reaction in reactions { if let Some(speech) = reaction.speech { campaign.transcript.push(NarrativeTurn { @@ -1352,6 +1353,16 @@ fn execute( assessment, } => { require_revision(&campaign, expected_revision)?; + let current_window_id = format!("reaction-wave:{}", campaign.revision); + if campaign + .events + .last() + .is_none_or(|event| event.kind != "reaction_wave" || event.id != current_window_id) + { + return Err(KernelError::Invalid( + "pending NPC initiative does not belong to the current reaction wave".into(), + )); + } let selected = crate::initiative::winner(&campaign.pending_world_proposals) .ok_or_else(|| KernelError::Invalid("there is no pending NPC action".into()))?; if proposal != selected { @@ -6116,6 +6127,124 @@ mod tests { assert_eq!(campaign.pending_world_proposals.len(), 2); } + #[tokio::test] + async fn npc_initiative_cannot_rebase_across_a_new_foreground_revision() { + let dir = tempfile::tempdir().unwrap(); + let store = CampaignStore::open(dir.path().join("campaign.cc")).unwrap(); + let kernel = WorldKernel::start(store.clone()); + let mut seed = campaign(); + seed.transcript.push(NarrativeTurn { + revision: 0, + at: seed.world_time, + speaker: "world".into(), + text: "a disturbance".into(), + }); + seed.actors.insert( + "anna".into(), + ActorState { + id: "anna".into(), + name: "Anna".into(), + location_id: "room".into(), + capabilities: BTreeSet::from(["intervene".into()]), + knowledge: BTreeSet::new(), + equipment: BTreeSet::new(), + conditions: BTreeSet::new(), + obligations: BTreeSet::new(), + relationships: BTreeMap::new(), + goals: vec![], + memories: vec![], + }, + ); + let campaign_id = seed.id; + kernel + .command(WorldCommand::CreateCampaign { + campaign: seed, + evidence_receipts: vec![], + model_stage_receipts: vec![], + }) + .await + .unwrap(); + let proposal = WorldActionProposal { + actor_id: "anna".into(), + intent: "intervene".into(), + intended_effect: "take control of the immediate situation".into(), + priority: 9, + state_references: vec!["capability:intervene".into(), "location:room".into()], + }; + kernel + .command(WorldCommand::ResolveReactionWave { + expected_revision: 0, + event_summary: "a disturbance".into(), + reactions: vec![ActorReaction { + actor_id: "anna".into(), + speech: None, + private_delta: ActorStateDelta::default(), + action_proposals: vec![proposal.clone()], + }], + }) + .await + .unwrap(); + kernel + .command(WorldCommand::Speak { + expected_revision: 1, + actor_id: "player".into(), + text: "I move on before Anna acts.".into(), + intended_effect: None, + }) + .await + .unwrap(); + let intent = ActionIntent { + actor_id: "anna".into(), + description: proposal.intent.clone(), + intended_effect: proposal.intended_effect.clone(), + }; + let CommandResult::Assessed { assessment } = kernel + .command(WorldCommand::Assess { + expected_revision: 2, + intent, + proposal: None, + }) + .await + .unwrap() + else { + panic!() + }; + let error = kernel + .command(WorldCommand::ResolveNpcAction { + expected_revision: 2, + proposal, + assessment, + }) + .await + .unwrap_err(); + assert!(error.to_string().contains("current reaction wave")); + + let refreshed = kernel + .command(WorldCommand::ResolveReactionWave { + expected_revision: 2, + event_summary: "player says: I move on before Anna acts.".into(), + reactions: vec![ActorReaction { + actor_id: "anna".into(), + speech: None, + private_delta: ActorStateDelta::default(), + action_proposals: vec![], + }], + }) + .await + .unwrap(); + let CommandResult::Committed { campaign, .. } = refreshed else { + panic!() + }; + assert_eq!(campaign.revision, 3); + assert!(campaign.pending_world_proposals.is_empty()); + let persisted = store + .load::("campaign.v1", &campaign_id.to_string()) + .unwrap() + .unwrap() + .1; + assert!(persisted.pending_world_proposals.is_empty()); + } + #[tokio::test] async fn roll_commits_only_the_prevalidated_typed_outcome_delta() { let dir = tempfile::tempdir().unwrap(); diff --git a/docs/architecture/ghostlight-dungeon-mvp.md b/docs/architecture/ghostlight-dungeon-mvp.md index 70f7feb..3b07c56 100644 --- a/docs/architecture/ghostlight-dungeon-mvp.md +++ b/docs/architecture/ghostlight-dungeon-mvp.md @@ -703,6 +703,12 @@ as a rejected-proposal receipt and returned as an accepted committed result with a visible warning. It cannot be reported as rejection of the already persisted player action, and a browser retry cannot duplicate that action. +Pending NPC initiative belongs to exactly one committed reaction-wave revision. +A fresh reaction wave replaces the previous proposal set rather than appending +to it, and `WorldKernel` rejects resolution unless the current revision's last +event is that exact wave. A malformed same-snapshot assessment may be retried; +a later player action, tick, or reaction wave cannot rebase the old proposal. + ### Away-time agency The service pulses every five minutes. After fifteen minutes without player diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 0da739a..1f26bac 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -217,6 +217,11 @@ messages. knowledge, movement, clock, and institution lanes entirely, close the effect object to undeclared properties, and default omitted maps to no mutation. The focused regression and full 285-test library pass cover the cut. +- That failed assessment left a pending NPC choice which the next live reaction + wave appended to and later resolved at revision 60. Pending initiative now + belongs to one exact reaction-wave revision: a new wave replaces the old set, + and the kernel rejects resolution after any intervening revision. Same-wave + retry remains valid without allowing stale Persona output to rebase. - Live refresh pressure reproduced and removed two contract faults: reuse of a private-command idempotency key across separately sealed attempts, and validation of a refresh receipt as if it carried the initial-login account diff --git a/notes/ghostlight-current-system-map.md b/notes/ghostlight-current-system-map.md index e3cb070..ccb11dd 100644 --- a/notes/ghostlight-current-system-map.md +++ b/notes/ghostlight-current-system-map.md @@ -947,6 +947,10 @@ snapshot and removes unavailable knowledge, movement, clock, and institution lanes before inference. Missing maps lower to no mutation. This keeps both the prompt and correction surface smaller and prevents provider `null` placeholders from stalling an NPC initiative on an operation the actor could not perform. +Pending NPC actions are revision-scoped reaction-window state. The next +reaction wave atomically replaces the set, and the kernel resolves an action +only when the current last event is the exact current-revision reaction wave. +Same-wave assessment correction remains possible; cross-turn rebasing is not. Population fission now composes admitted child entities, stable child identities, one lineage split, exact resource custody transfers, and exact