|
| 1 | +use super::*; |
| 2 | +use crate::openhuman::agent::harness::session::transcript::{ |
| 3 | + read_transcript, write_transcript, TranscriptMeta, |
| 4 | +}; |
| 5 | +use crate::openhuman::providers::ChatMessage; |
| 6 | +use std::fs; |
| 7 | +use std::path::Path; |
| 8 | +use tempfile::TempDir; |
| 9 | + |
| 10 | +fn tainted_prompt() -> String { |
| 11 | + "## Identity\n\nYou are an assistant.\n\n\ |
| 12 | + ### PROFILE.md\n\n\ |
| 13 | + style/calm tooling/rust\n\n\ |
| 14 | + ### Tools\n\n- shell\n" |
| 15 | + .to_string() |
| 16 | +} |
| 17 | + |
| 18 | +fn meta() -> TranscriptMeta { |
| 19 | + TranscriptMeta { |
| 20 | + agent_name: "main".into(), |
| 21 | + dispatcher: "native".into(), |
| 22 | + created: "2026-05-01T00:00:00Z".into(), |
| 23 | + updated: "2026-05-01T00:00:00Z".into(), |
| 24 | + turn_count: 1, |
| 25 | + input_tokens: 0, |
| 26 | + output_tokens: 0, |
| 27 | + cached_input_tokens: 0, |
| 28 | + charged_amount_usd: 0.0, |
| 29 | + thread_id: None, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +fn config_in(tmp: &TempDir) -> Config { |
| 34 | + Config { |
| 35 | + config_path: tmp.path().join("config.toml"), |
| 36 | + workspace_dir: tmp.path().join("workspace"), |
| 37 | + ..Default::default() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +fn seed_tainted_transcript(workspace_dir: &Path) -> std::path::PathBuf { |
| 42 | + let raw_dir = workspace_dir.join("session_raw"); |
| 43 | + fs::create_dir_all(&raw_dir).unwrap(); |
| 44 | + let path = raw_dir.join("1700000000_main.jsonl"); |
| 45 | + let messages = vec![ |
| 46 | + ChatMessage::system(tainted_prompt()), |
| 47 | + ChatMessage::user("hello"), |
| 48 | + ]; |
| 49 | + write_transcript(&path, &messages, &meta(), None).unwrap(); |
| 50 | + path |
| 51 | +} |
| 52 | + |
| 53 | +#[tokio::test] |
| 54 | +async fn run_pending_skips_when_version_current() { |
| 55 | + let tmp = TempDir::new().unwrap(); |
| 56 | + let path = seed_tainted_transcript(&tmp.path().join("workspace")); |
| 57 | + let before = fs::read(&path).unwrap(); |
| 58 | + |
| 59 | + let mut config = config_in(&tmp); |
| 60 | + config.schema_version = CURRENT_SCHEMA_VERSION; |
| 61 | + run_pending(&mut config).await; |
| 62 | + |
| 63 | + assert_eq!(config.schema_version, CURRENT_SCHEMA_VERSION); |
| 64 | + let after = fs::read(&path).unwrap(); |
| 65 | + assert_eq!(before, after, "transcript must be untouched"); |
| 66 | +} |
| 67 | + |
| 68 | +#[tokio::test] |
| 69 | +async fn run_pending_runs_phase_out_when_version_zero() { |
| 70 | + let tmp = TempDir::new().unwrap(); |
| 71 | + let path = seed_tainted_transcript(&tmp.path().join("workspace")); |
| 72 | + |
| 73 | + let mut config = config_in(&tmp); |
| 74 | + assert_eq!(config.schema_version, 0); |
| 75 | + run_pending(&mut config).await; |
| 76 | + |
| 77 | + assert_eq!(config.schema_version, 1); |
| 78 | + let session = read_transcript(&path).unwrap(); |
| 79 | + assert!( |
| 80 | + !session.messages[0].content.contains("### PROFILE.md"), |
| 81 | + "PROFILE.md block must be stripped, got:\n{}", |
| 82 | + session.messages[0].content |
| 83 | + ); |
| 84 | + |
| 85 | + let on_disk = std::fs::read_to_string(&config.config_path).unwrap(); |
| 86 | + assert!( |
| 87 | + on_disk.contains("schema_version = 1"), |
| 88 | + "saved config.toml must record schema_version=1, got:\n{on_disk}" |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +#[tokio::test] |
| 93 | +async fn run_pending_bumps_version_on_fresh_install() { |
| 94 | + let tmp = TempDir::new().unwrap(); |
| 95 | + // No session_raw/ at all — pure fresh install. |
| 96 | + fs::create_dir_all(tmp.path().join("workspace")).unwrap(); |
| 97 | + |
| 98 | + let mut config = config_in(&tmp); |
| 99 | + run_pending(&mut config).await; |
| 100 | + |
| 101 | + assert_eq!(config.schema_version, 1); |
| 102 | + let on_disk = std::fs::read_to_string(&config.config_path).unwrap(); |
| 103 | + assert!(on_disk.contains("schema_version = 1")); |
| 104 | +} |
| 105 | + |
| 106 | +#[tokio::test] |
| 107 | +async fn run_pending_rolls_back_schema_version_when_save_fails() { |
| 108 | + let tmp = TempDir::new().unwrap(); |
| 109 | + seed_tainted_transcript(&tmp.path().join("workspace")); |
| 110 | + |
| 111 | + let mut config = config_in(&tmp); |
| 112 | + // Point config.save() at a path whose parent directory cannot be |
| 113 | + // created (a regular file occupies that name), forcing save() to |
| 114 | + // error after the migration body has succeeded. |
| 115 | + let blocker = tmp.path().join("blocker"); |
| 116 | + fs::write(&blocker, "not a directory").unwrap(); |
| 117 | + config.config_path = blocker.join("nested").join("config.toml"); |
| 118 | + |
| 119 | + assert_eq!(config.schema_version, 0); |
| 120 | + run_pending(&mut config).await; |
| 121 | + |
| 122 | + assert_eq!( |
| 123 | + config.schema_version, 0, |
| 124 | + "save failed → in-memory schema_version must be rolled back to 0" |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +#[tokio::test] |
| 129 | +async fn run_pending_is_a_no_op_on_second_invocation() { |
| 130 | + let tmp = TempDir::new().unwrap(); |
| 131 | + seed_tainted_transcript(&tmp.path().join("workspace")); |
| 132 | + |
| 133 | + let mut config = config_in(&tmp); |
| 134 | + run_pending(&mut config).await; |
| 135 | + assert_eq!(config.schema_version, 1); |
| 136 | + |
| 137 | + // Mutate the config file timestamp marker by reading + comparing |
| 138 | + // before vs after the second invocation. |
| 139 | + let before = fs::metadata(&config.config_path).unwrap().modified().ok(); |
| 140 | + std::thread::sleep(std::time::Duration::from_millis(20)); |
| 141 | + run_pending(&mut config).await; |
| 142 | + let after = fs::metadata(&config.config_path).unwrap().modified().ok(); |
| 143 | + |
| 144 | + assert_eq!(config.schema_version, 1); |
| 145 | + assert_eq!( |
| 146 | + before, after, |
| 147 | + "config.toml must not be re-saved on second run" |
| 148 | + ); |
| 149 | +} |
0 commit comments