@@ -53,6 +53,19 @@ pub fn save_project(p: &Project) {
5353 fs::write(dir.join("project.json"), serde_json::to_string_pretty(p).unwrap()).unwrap();
5454 }
5555
56+/// Parts a ticket's words from what conveyor adds below them: what was clicked, and how to see it.
57+const RULE: &str = "\n\n---\n\n";
58+
59+/// A ticket's words, as the user wrote them, without the context below them.
60+pub fn words(body: &str) -> &str {
61+ body.split(RULE).next().unwrap_or("").trim()
62+}
63+
64+/// A body of `words` with `context` below them, if there is any.
65+pub fn with_context(words: &str, context: &str) -> String {
66+ if context.is_empty() { words.into() } else { format!("{}{RULE}{context}", words.trim_end()) }
67+}
68+
5669 pub fn dir(project: &str, id: u32) -> PathBuf {
5770 paths::projects().join(project).join("tickets").join(id.to_string())
5871 }
@@ -100,12 +113,13 @@ pub fn set_state(project: &str, id: u32, state: &str) {
100113 event(project, id, json!({"kind": "state", "state": state}));
101114 }
102115
103-/// Rewrites a ticket's title and body. A running ticket's text is the agent's, so it's refused.
116+/// Rewrites a ticket's title and words, keeping its context: the app only ever sees the words.
117+/// A running ticket's text is the agent's, so it's refused.
104118 pub fn edit(project: &str, id: u32, title: &str, body: &str) -> Result<(), String> {
105119 let _w = WRITE.lock().unwrap();
106120 let mut t = ticket(project, id).ok_or("no such ticket")?;
107121 if t.state == "running" { return Err("an agent is working this ticket: edit it once it settles".into()); }
108- (t.title, t.body) = (title.replace('\n', " "), body.into());
122+ (t.title, t.body) = (title.replace('\n', " "), with_context(body, t.body.split_once(RULE).map_or("", |(_, c)| c)));
109123 write(&t);
110124 drop(_w);
111125 event(project, id, json!({"kind": "conveyor", "msg": "edited"}));
@@ -167,3 +181,17 @@ pub fn load_activity() {
167181 evs.sort_by_key(|(_, _, e)| e["at"].as_u64().unwrap_or(0));
168182 for (p, id, e) in evs.iter().rev().take(KEEP).rev() { remember(p, *id, e); }
169183 }
184+
185+#[cfg(test)]
186+mod tests {
187+ use super::*;
188+
189+ #[test]
190+ fn context_survives_rewording() {
191+ let body = with_context("Make it blue", "- Element: `#save`");
192+ assert_eq!(words(&body), "Make it blue");
193+ let edited = with_context("Make it red", body.split_once(RULE).map_or("", |(_, c)| c));
194+ assert_eq!(edited, "Make it red\n\n---\n\n- Element: `#save`");
195+ assert_eq!(with_context("Plain", ""), "Plain");
196+ }
197+}