| 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!(engine.refresh_pinned_header_for_turn(&context).is_none()); |
| 18 | let pinned = |
| 19 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()); |
| 20 | let sections: Vec<_> = (0..3) |
| 21 | .map(|i| crate::extension_host::prompt::PromptSection { |
| 22 | plugin_id: "user/fixture/repo-style".to_string(), |
| 23 | plugin_name: "repo-style".to_string(), |
| 24 | generation: 1, |
| 25 | content_hash: "fixture-reviewed-hash".to_string(), |
| 26 | id: format!("repo-style-{i}"), |
| 27 | text: format!("{}\nLast instruction {i}.", "x".repeat(4000)), |
| 28 | scope: None, |
| 29 | interpolate: false, |
| 30 | }) |
| 31 | .collect(); |
| 32 | let block = crate::extension_host::prompt::render_prompt_sections(§ions) |
| 33 | .expect("render") |
| 34 | .expect("sections"); |
| 35 | assert!(block.len() > codewhale_core::prefix_cache::CONTEXT_UPDATE_MAX_BYTES); |
| 36 | engine |
| 37 | .record_extension_prompt_contributions(Some(&block)) |
| 38 | .await; |
| 39 | let initial_count = engine.session.messages.len(); |
| 40 | engine |
| 41 | .record_extension_prompt_contributions(Some(&block)) |
| 42 | .await; |
| 43 | assert_eq!( |
| 44 | engine.session.messages.len(), |
| 45 | initial_count, |
| 46 | "unchanged instructions do not repeat" |
| 47 | ); |
| 48 | let recorded = engine.session.messages.last().expect("recorded snapshot"); |
| 49 | let display = crate::runtime_handoff::extension_prompt_contributions_display(recorded) |
| 50 | .expect("runtime event"); |
| 51 | assert!( |
| 52 | display.contains(&block), |
| 53 | "the entire admitted snapshot reaches the model" |
| 54 | ); |
| 55 | assert!(crate::runtime_handoff::is_runtime_owned_user_message( |
| 56 | recorded |
| 57 | )); |
| 58 | assert!(engine.refresh_pinned_header_for_turn(&context).is_none()); |
| 59 | assert_eq!( |
| 60 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()), |
| 61 | pinned |
| 62 | ); |
| 63 | |
| 64 | engine.record_extension_prompt_contributions(None).await; |
| 65 | let retired_count = engine.session.messages.len(); |
| 66 | engine.record_extension_prompt_contributions(None).await; |
| 67 | assert_eq!(engine.session.messages.len(), retired_count); |
| 68 | assert!( |
| 69 | crate::runtime_handoff::extension_prompt_contributions_display( |
| 70 | engine.session.messages.last().expect("withdrawal") |
| 71 | ) |
| 72 | .is_some_and(|text| text.contains("withdrawn")) |
| 73 | ); |
| 74 | assert_eq!( |
| 75 | codewhale_core::prefix_cache::system_prompt_text(engine.session.system_prompt.as_ref()), |
| 76 | pinned |
| 77 | ); |
| 78 | |
| 79 | // Restore/compaction cannot make a process-local baseline swallow the text. |
| 80 | engine.extension_prompt_block = Some(block.clone()); |
| 81 | engine.session.messages.clear(); |
| 82 | engine.record_current_extension_prompt_contributions().await; |
| 83 | assert_eq!(engine.session.messages.len(), 1); |
| 84 | assert!( |
| 85 | crate::runtime_handoff::extension_prompt_contributions_display(&engine.session.messages[0]) |
| 86 | .is_some_and(|text| text.contains(&block)) |
| 87 | ); |
| 88 | } |
| 89 |