interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-cli / src / turn_journal / crash_tests.rs

176 lines · 5777 bytes Raw Blame History
1//! Process-death checks for #665. A real durable append is the disposable tool.
2//! These exercise the production journal and settlement functions without a model.
3
4use super::*;
5use std::os::unix::process::ExitStatusExt;
6use std::process::{Child, Command, Stdio};
7use std::time::{Duration, Instant};
8
9const CHILD_TEST: &str = "turn_journal::crash_tests::crash_child";
10
11// Reap the subprocess even if a parent assertion or readiness timeout fails.
12struct ChildGuard(Child);
13
14impl Drop for ChildGuard {
15 fn drop(&mut self) {
16 let _ = self.0.kill();
17 let _ = self.0.wait();
18 }
19}
20
21fn append_effect(dir: &Path) {
22 let mut file = OpenOptions::new()
23 .create(true)
24 .append(true)
25 .open(dir.join("effects"))
26 .unwrap();
27 file.write_all(b"committed\n").unwrap();
28 file.sync_all().unwrap();
29}
30
31fn hold(dir: &Path) -> ! {
32 std::fs::write(dir.join("ready"), b"ready").unwrap();
33 loop {
34 std::thread::park();
35 }
36}
37
38#[test]
39#[ignore = "subprocess entry point, invoked by crash boundary tests"]
40fn crash_child() {
41 let dir = PathBuf::from(std::env::var_os("AIEN_CRASH_TEST_DIR").unwrap());
42 let phase = std::env::var("AIEN_CRASH_TEST_PHASE").unwrap();
43 assert!(matches!(
44 phase.as_str(),
45 "before_effect" | "after_effect" | "after_outcome"
46 ));
47 // Pre-create and sync the effect ledger so the test measures process death,
48 // not persistence of a newly created directory entry across power failure.
49 File::create(dir.join("effects"))
50 .unwrap()
51 .sync_all()
52 .unwrap();
53 let mut journal = Journal::create(&dir.join("session.jsonl")).unwrap();
54 let mut messages = Vec::new();
55 run_tool_calls(
56 &mut journal,
57 &mut messages,
58 vec![("write_to_file".into(), json!({"path": "effects"}))],
59 |_, _| {
60 if phase == "before_effect" {
61 hold(&dir);
62 }
63 append_effect(&dir);
64 if phase == "after_effect" {
65 // The effect is durable, but dispatch has not returned, so the
66 // journal cannot yet contain its successful outcome.
67 hold(&dir);
68 }
69 json!({"status": "committed"})
70 },
71 )
72 .unwrap();
73 hold(&dir);
74}
75
76fn crash_and_resume(phase: &str, expected_effect: &str, expected_pending: usize) {
77 let dir = tempfile::tempdir().unwrap();
78 let mut child = ChildGuard(
79 Command::new(std::env::current_exe().unwrap())
80 .args(["--exact", CHILD_TEST, "--ignored", "--test-threads=1"])
81 .env("AIEN_CRASH_TEST_DIR", dir.path())
82 .env("AIEN_CRASH_TEST_PHASE", phase)
83 .stdout(Stdio::null())
84 .stderr(Stdio::inherit())
85 .spawn()
86 .unwrap(),
87 );
88 let deadline = Instant::now() + Duration::from_secs(20);
89 while !dir.path().join("ready").exists() {
90 assert!(
91 child.0.try_wait().unwrap().is_none(),
92 "child exited before {phase}"
93 );
94 assert!(Instant::now() < deadline, "child never reached {phase}");
95 std::thread::sleep(Duration::from_millis(5));
96 }
97 assert_eq!(
98 std::fs::read_to_string(dir.path().join("effects")).unwrap(),
99 expected_effect
100 );
101 child.0.kill().unwrap();
102 assert_eq!(
103 child.0.wait().unwrap().signal(),
104 Some(9),
105 "expected SIGKILL"
106 );
107
108 let path = dir.path().join("session.jsonl");
109 let (mut journal, replay) = Journal::resume(&path).unwrap();
110 assert_eq!(replay.in_doubt.len(), expected_pending);
111 assert!(!replay.torn_tail);
112 let mut messages = replay.messages;
113 let steps = settle_in_doubt(&mut journal, &mut messages, &replay.in_doubt, |_, _| {
114 // A regression that replays a write produces an observable extra effect.
115 append_effect(dir.path());
116 json!({"status": "committed"})
117 })
118 .unwrap();
119 assert_eq!(
120 std::fs::read_to_string(dir.path().join("effects")).unwrap(),
121 expected_effect,
122 "recovery must not execute an effectful tool"
123 );
124 assert_eq!(
125 messages.len(),
126 1,
127 "exactly one outcome reaches the conversation"
128 );
129 let content = messages[0]["content"].as_str().unwrap();
130 if expected_pending == 1 {
131 assert!(matches!(
132 steps.as_slice(),
133 [ResumeStep::ReportUnknown { .. }]
134 ));
135 assert!(content.contains("\"status\":\"outcome_unknown\""));
136 assert!(
137 !content.contains("\"status\":\"committed\""),
138 "lost response is not success"
139 );
140 } else {
141 assert!(steps.is_empty());
142 assert!(content.contains("\"status\":\"committed\""));
143 }
144 drop(journal);
145
146 // A second journal reopen must neither replay the effect nor add a second result.
147 let (mut journal, replay) = Journal::resume(&path).unwrap();
148 assert!(replay.in_doubt.is_empty());
149 assert_eq!(replay.messages, messages);
150 let mut again = replay.messages;
151 settle_in_doubt(&mut journal, &mut again, &replay.in_doubt, |_, _| {
152 append_effect(dir.path());
153 json!({"status": "committed"})
154 })
155 .unwrap();
156 assert_eq!(again, messages);
157 assert_eq!(
158 std::fs::read_to_string(dir.path().join("effects")).unwrap(),
159 expected_effect
160 );
161}
162
163#[test]
164fn killed_before_effect_does_not_invent_success_or_execute_on_resume() {
165 crash_and_resume("before_effect", "", 1);
166}
167
168#[test]
169fn killed_after_effect_before_outcome_does_not_repeat_the_committed_effect() {
170 crash_and_resume("after_effect", "committed\n", 1);
171}
172
173#[test]
174fn killed_after_outcome_preserves_success_without_repeating_the_effect() {
175 crash_and_resume("after_outcome", "committed\n", 0);
176}