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-serve-http / src / readers / tests.rs

205 lines · 7587 bytes Raw Blame History
1//! Host tests of the readers' split and request building (a word counter stands in for the tokenizer).
2use super::*;
3
4fn words(s: &str) -> usize {
5 s.split_whitespace().count()
6}
7
8fn cfg() -> ReadersConfig {
9 ReadersConfig::new("m")
10}
11
12/// `paras` paragraphs of `lines` lines of `w` words each, every word naming its line.
13fn doc(paras: usize, lines: usize, w: usize) -> String {
14 let mut s = String::new();
15 let mut n = 0;
16 for p in 0..paras {
17 if p > 0 {
18 s.push('\n');
19 }
20 for _ in 0..lines {
21 n += 1;
22 let line: Vec<String> = (0..w).map(|k| format!("l{n}w{k}")).collect();
23 s.push_str(&line.join(" "));
24 s.push('\n');
25 }
26 }
27 s
28}
29
30fn user(text: &str) -> Value {
31 json!({"role": "user", "content": text})
32}
33
34#[test]
35fn short_input_takes_the_plain_path() {
36 let m = vec![user(&doc(3, 10, 10))]; // 300 words
37 assert!(plan(&m, &cfg(), &words).is_none());
38}
39
40#[test]
41fn long_input_is_eight_equal_parts_covering_every_line_with_overlap() {
42 let text = format!("Summarize this design.\n\n{}", doc(40, 6, 20)); // 4800 words + 3
43 let m = vec![
44 json!({"role": "system", "content": "be brief"}),
45 user(&text),
46 ];
47 let p = plan(&m, &cfg(), &words).expect("over the threshold");
48 assert_eq!(p.parts.len(), 8);
49 assert!(p.question_is_excerpt);
50 assert!(p.question.starts_with("Summarize this design."));
51 assert!(p.question.contains("[...]"));
52 assert_eq!(p.long_msgs, vec![1]);
53 // Lines: each part starts where the one before ended, the last ends at the last line.
54 assert_eq!(p.parts[0].first_line, 1);
55 for w in p.parts.windows(2) {
56 assert_eq!(
57 w[1].first_line,
58 w[0].last_line + 1,
59 "{:?}",
60 (w[0].last_line, w[1].first_line)
61 );
62 }
63 assert_eq!(p.parts[7].last_line, p.total_lines);
64 // Equal within a sixth of a part (a part is ~600 words, paragraphs are 120).
65 let each = p.total_tokens / 8;
66 for (k, part) in p.parts.iter().enumerate() {
67 let own = part
68 .tokens
69 .saturating_sub(if k > 0 { cfg().overlap_tokens } else { 0 });
70 assert!(
71 own.abs_diff(each) <= each / 4,
72 "part {k}: {} vs {each}",
73 part.tokens
74 );
75 }
76 // Overlap: a part after the first starts with the end of the part before it.
77 for w in p.parts.windows(2) {
78 let head: String = w[1].text.lines().next().unwrap().to_string();
79 assert!(w[0].text.contains(&head), "overlap line {head:?}");
80 assert!(w[1].tokens > 0);
81 }
82}
83
84#[test]
85fn cuts_prefer_paragraph_boundaries() {
86 let m = vec![user(&doc(16, 10, 30))]; // 16 paragraphs of 300 words: 2 per part
87 let p = plan(&m, &cfg(), &words).unwrap();
88 assert_eq!(p.parts.len(), 8);
89 // Each paragraph is 10 lines + a blank line: a part after the first starts on a paragraph's first line.
90 for part in &p.parts[1..] {
91 assert_eq!(
92 (part.first_line - 1) % 11,
93 0,
94 "part starts at line {}",
95 part.first_line
96 );
97 }
98}
99
100#[test]
101fn one_huge_line_is_still_cut_into_eight() {
102 let line: Vec<String> = (0..5000).map(|k| format!("w{k}")).collect();
103 let m = vec![user(&line.join(" "))];
104 let p = plan(&m, &cfg(), &words).unwrap();
105 assert_eq!(p.parts.len(), 8);
106 let joined: usize = p.parts.iter().map(|x| x.tokens).sum();
107 assert!(joined >= 5000);
108 assert!(
109 p.parts.iter().all(|x| x.tokens >= 400),
110 "{:?}",
111 p.parts.iter().map(|x| x.tokens).collect::<Vec<_>>()
112 );
113}
114
115#[test]
116fn short_question_and_long_tool_result_keep_the_question_and_the_tools() {
117 let raw = json!({
118 "model": "m-readers",
119 "tools": [{"type": "function", "function": {"name": "read_file"}}],
120 "aien_show_notes": true,
121 "messages": [
122 {"role": "system", "content": "sys"},
123 {"role": "user", "content": "What does the log say about the crash?"},
124 {"role": "assistant", "content": "", "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "read_file", "arguments": "{}"}}]},
125 {"role": "tool", "tool_call_id": "c1", "content": doc(20, 5, 40)},
126 ]
127 });
128 let msgs = raw["messages"].as_array().unwrap();
129 let p = plan(msgs, &cfg(), &words).unwrap();
130 assert!(!p.question_is_excerpt);
131 assert_eq!(p.question, "What does the log say about the crash?");
132 assert_eq!(p.long_msgs, vec![3]);
133 let notes: Vec<String> = (1..=8).map(|k| format!("note {k}")).collect();
134 let c = connect_body(&raw, &p, &notes, "m");
135 assert_eq!(c["model"], "m");
136 assert!(c.get("aien_show_notes").is_none());
137 assert_eq!(c["tools"], raw["tools"]);
138 assert_eq!(c["messages"][1], raw["messages"][1]);
139 assert_eq!(c["messages"][2], raw["messages"][2]);
140 let tool = c["messages"][3]["content"].as_str().unwrap();
141 assert_eq!(c["messages"][3]["tool_call_id"], "c1");
142 for k in 1..=8 {
143 assert!(tool.contains(&format!("## Part {k} of 8")), "part {k}");
144 assert!(tool.contains(&format!("note {k}")));
145 }
146 // The notes are in order.
147 assert!(tool.find("note 1").unwrap() < tool.find("note 8").unwrap());
148}
149
150#[test]
151fn long_user_message_connect_starts_with_the_request() {
152 let text = format!("{}\nWhat is the main risk?", doc(30, 5, 30));
153 let raw = json!({"model": "m-readers", "stream": true, "messages": [user(&text)]});
154 let p = plan(raw["messages"].as_array().unwrap(), &cfg(), &words).unwrap();
155 assert!(p.question.ends_with("What is the main risk?"));
156 let notes = vec!["n".to_string(); 8];
157 let c = connect_body(&raw, &p, &notes, "m");
158 let u = c["messages"][0]["content"].as_str().unwrap();
159 assert!(u.starts_with(&p.question));
160 assert!(u.contains("read in 8 parts by 8 readers"));
161 assert_eq!(c["stream"], true);
162 assert!(
163 words(u) < 600,
164 "the connect prompt carries notes, not the input"
165 );
166}
167
168#[test]
169fn map_body_carries_system_question_and_its_part_only() {
170 let text = format!("Find the numbers.\n\n{}", doc(20, 6, 30));
171 let m = vec![
172 json!({"role": "developer", "content": "house rules"}),
173 user(&text),
174 ];
175 let p = plan(&m, &cfg(), &words).unwrap();
176 let b = map_body(&m, &p, 2, "m", &cfg());
177 assert_eq!(b["model"], "m");
178 assert_eq!(b["max_tokens"], 400);
179 assert_eq!(b["temperature"], 0.0);
180 assert_eq!(b["chat_template_kwargs"]["enable_thinking"], false);
181 assert!(b.get("tools").is_none() && b.get("stream").is_none());
182 assert_eq!(b["messages"][0]["content"], "house rules");
183 let u = b["messages"][1]["content"].as_str().unwrap();
184 assert!(u.contains("You are reader 3 of 8"));
185 assert!(u.contains("Find the numbers."));
186 assert!(u.contains(p.parts[2].text.trim_end()));
187 let other = p.parts[5].text.lines().nth(3).unwrap();
188 assert!(!u.contains(other), "another part's line leaked in");
189}
190
191#[test]
192fn notes_are_cleaned_and_the_preface_is_collapsed() {
193 assert_eq!(
194 clean_notes("<think>\nhm\n</think>\n\n Summary: x \n"),
195 "Summary: x"
196 );
197 assert_eq!(clean_notes(" plain "), "plain");
198 let m = vec![user(&doc(30, 5, 30))];
199 let p = plan(&m, &cfg(), &words).unwrap();
200 let notes: Vec<String> = (1..=8).map(|k| format!("n{k}")).collect();
201 let s = notes_preface(&p, &notes);
202 assert!(s.starts_with("<details>\n<summary>Notes from 8 readers"));
203 assert!(s.ends_with("</details>\n\n"));
204 assert!(s.contains("**Part 8 of 8"));
205}