| 1 | //! Mod instructions use complete recorded history snapshots after the pinned prefix. |
| 2 | use super::*; |
| 3 | |
| 4 | #[tokio::test] |
| 5 | async fn extension_prompt_changes_and_retirement_are_recorded_without_rewriting_the_prefix() { |
| 6 | let _home = crate::test_support::SealedHome::new(); |
| 7 | let workspace = tempfile::tempdir().expect("workspace"); |
| 8 | let (mut engine, _) = Engine::new( |
| 9 | EngineConfig { |
| 10 | workspace: workspace.path().to_path_buf(), |
| 11 | terminal_chrome_enabled: false, |
| 12 | ..EngineConfig::default() |
| 13 | }, |
| 14 | &Config::default(), |
| 15 | ); |
| 16 | let context = engine.installed_next_turn_prompt_context(); |
| 17 | assert!( |
| 18 | engine |
| 19 | .refresh_pinned_header_for_turn(&context) |
| 20 | .unwrap() |
| 21 | .is_none() |
| 22 | ); |
| 23 | let pinned = |
| 24 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()); |
| 25 | let sections: Vec<_> = (0..3) |
| 26 | .map(|i| crate::extension_host::prompt::PromptSection { |
| 27 | plugin_id: "user/fixture/repo-style".to_string(), |
| 28 | plugin_name: "repo-style".to_string(), |
| 29 | generation: 1, |
| 30 | content_hash: "fixture-reviewed-hash".to_string(), |
| 31 | id: format!("repo-style-{i}"), |
| 32 | text: format!("{}\nLast instruction {i}.", "x".repeat(4000)), |
| 33 | scope: None, |
| 34 | interpolate: false, |
| 35 | }) |
| 36 | .collect(); |
| 37 | let block = crate::extension_host::prompt::render_prompt_sections(§ions) |
| 38 | .expect("render") |
| 39 | .expect("sections"); |
| 40 | assert!(block.len() > codewhale_core::prefix_cache::CONTEXT_UPDATE_MAX_BYTES); |
| 41 | engine |
| 42 | .record_extension_prompt_contributions(Some(&block)) |
| 43 | .await; |
| 44 | let initial_count = engine.session.messages.len(); |
| 45 | engine |
| 46 | .record_extension_prompt_contributions(Some(&block)) |
| 47 | .await; |
| 48 | assert_eq!( |
| 49 | engine.session.messages.len(), |
| 50 | initial_count, |
| 51 | "unchanged instructions do not repeat" |
| 52 | ); |
| 53 | let recorded = engine.session.messages.last().expect("recorded snapshot"); |
| 54 | let display = crate::runtime_handoff::extension_prompt_contributions_display(recorded) |
| 55 | .expect("runtime event"); |
| 56 | assert!( |
| 57 | display.contains(&block), |
| 58 | "the entire admitted snapshot reaches the model" |
| 59 | ); |
| 60 | assert!(codewhale_models::turn_classify::is_runtime_owned_user_message(recorded)); |
| 61 | assert!( |
| 62 | engine |
| 63 | .refresh_pinned_header_for_turn(&context) |
| 64 | .unwrap() |
| 65 | .is_none() |
| 66 | ); |
| 67 | assert_eq!( |
| 68 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()), |
| 69 | pinned |
| 70 | ); |
| 71 | |
| 72 | engine.record_extension_prompt_contributions(None).await; |
| 73 | let retired_count = engine.session.messages.len(); |
| 74 | engine.record_extension_prompt_contributions(None).await; |
| 75 | assert_eq!(engine.session.messages.len(), retired_count); |
| 76 | assert!( |
| 77 | crate::runtime_handoff::extension_prompt_contributions_display( |
| 78 | engine.session.messages.last().expect("withdrawal") |
| 79 | ) |
| 80 | .is_some_and(|text| text.contains("withdrawn")) |
| 81 | ); |
| 82 | assert_eq!( |
| 83 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()), |
| 84 | pinned |
| 85 | ); |
| 86 | |
| 87 | // Restore/compaction cannot make a process-local baseline swallow the text. |
| 88 | engine.extension_prompt_block = Some(block.clone()); |
| 89 | engine.session.messages.clear(); |
| 90 | engine.record_current_extension_prompt_contributions().await; |
| 91 | assert_eq!(engine.session.messages.len(), 1); |
| 92 | assert!( |
| 93 | crate::runtime_handoff::extension_prompt_contributions_display(&engine.session.messages[0]) |
| 94 | .is_some_and(|text| text.contains(&block)) |
| 95 | ); |
| 96 | } |
| 97 |