agentmux_srv_core/backend/
continuity_sections.rs1const SHRINKABLE: [&str; 3] = ["done (unverified)", "done (verified)", "decisions and why"];
18const PACKET_ORDER: [&str; 9] = [
21 "current goal",
22 "last user request",
23 "in flight",
24 "waiting on the user",
25 "exact identifiers",
26 "decisions and why",
27 "user preferences",
28 "done (unverified)",
29 "done (verified)",
30];
31
32struct Section {
33 heading: String,
35 body: Vec<String>,
37}
38
39impl Section {
40 fn name(&self) -> String {
41 self.heading.trim_start_matches('#').trim().to_ascii_lowercase()
42 }
43 fn is(&self, name: &str) -> bool {
44 self.name().starts_with(name)
45 }
46 fn chars(&self) -> usize {
47 self.render().chars().count()
48 }
49 fn render(&self) -> String {
50 let mut out = String::new();
51 if !self.heading.is_empty() {
52 out.push_str(&self.heading);
53 out.push('\n');
54 }
55 for line in &self.body {
56 out.push_str(line);
57 out.push('\n');
58 }
59 out
60 }
61}
62
63fn parse(text: &str) -> Vec<Section> {
64 let mut sections: Vec<Section> = Vec::new();
65 for line in text.lines() {
66 if line.starts_with("## ") {
67 sections.push(Section { heading: line.to_string(), body: Vec::new() });
68 } else {
69 if sections.is_empty() {
70 sections.push(Section { heading: String::new(), body: Vec::new() });
71 }
72 sections.last_mut().expect("a section was just pushed").body.push(line.to_string());
73 }
74 }
75 sections
76}
77
78fn render(sections: &[Section]) -> String {
79 sections.iter().map(Section::render).collect::<String>().trim_end().to_string()
80}
81
82fn omitted_line(n: usize) -> String {
84 format!("({n} older items omitted)")
85}
86
87fn omitted_count(line: &str) -> Option<usize> {
89 line.trim().strip_prefix('(')?.strip_suffix(" older items omitted)")?.parse().ok()
90}
91
92fn starts_item(line: &str) -> bool {
94 let t = line.trim_start();
95 if line.starts_with([' ', '\t']) {
96 return false;
97 }
98 t.starts_with("- ")
99 || t.starts_with("* ")
100 || t.starts_with("+ ")
101 || t.split_once(". ").is_some_and(|(n, _)| !n.is_empty() && n.chars().all(|c| c.is_ascii_digit()))
102}
103
104fn items(body: &[String]) -> (Vec<String>, Vec<Vec<String>>) {
107 let mut lead: Vec<String> = Vec::new();
108 let mut items: Vec<Vec<String>> = Vec::new();
109 for line in body {
110 if starts_item(line) {
111 items.push(vec![line.clone()]);
112 } else if let Some(last) = items.last_mut() {
113 last.push(line.clone());
114 } else {
115 lead.push(line.clone());
116 }
117 }
118 (lead, items)
119}
120
121fn shorten_section(section: &mut Section, text_chars: usize, target: usize) -> usize {
125 let (mut lead, mut items) = items(§ion.body);
126 let earlier = lead.first().and_then(|l| omitted_count(l)).unwrap_or(0);
127 if earlier > 0 {
128 lead.remove(0);
129 }
130 let mut over = text_chars.saturating_sub(target);
131 let mut dropped = 0;
132 over += omitted_line(earlier + items.len()).chars().count() + 1;
134 while over > 0 && !items.is_empty() {
135 let gone = items.remove(0);
136 over = over.saturating_sub(gone.iter().map(|l| l.chars().count() + 1).sum());
137 dropped += 1;
138 }
139 let total = earlier + dropped;
140 section.body = Vec::new();
141 if total > 0 {
142 section.body.push(omitted_line(total));
143 }
144 section.body.extend(lead);
145 section.body.extend(items.into_iter().flatten());
146 dropped
147}
148
149pub(crate) fn shorten(text: &str, cap: usize) -> (String, usize) {
154 if text.chars().count() <= cap {
155 return (text.to_string(), 0);
156 }
157 let mut sections = parse(text);
158 let mut dropped = 0;
159 for name in SHRINKABLE {
160 let total: usize = sections.iter().map(Section::chars).sum();
161 if total <= cap {
162 break;
163 }
164 for section in sections.iter_mut().filter(|s| s.is(name)) {
165 dropped += shorten_section(section, total, cap);
166 }
167 }
168 (render(§ions), dropped)
169}
170
171pub(crate) fn in_packet_order(text: &str) -> String {
175 let sections = parse(text);
176 if sections.iter().all(|s| s.heading.is_empty()) {
177 return text.to_string();
178 }
179 let rank = |s: &Section| PACKET_ORDER.iter().position(|n| s.is(n)).unwrap_or(PACKET_ORDER.len());
180 let mut ordered: Vec<&Section> = sections.iter().collect();
181 ordered.sort_by_key(|s| if s.heading.is_empty() { 0 } else { rank(s) + 1 });
183 ordered.into_iter().map(Section::render).collect::<String>().trim_end().to_string()
184}
185
186#[cfg(test)]
187#[path = "continuity_sections_tests.rs"]
188mod tests;