Craft Anything: understand how any software works, and craft your own.
Rust
A question answered outside a build stays in the project's conversation (#180)
BFlat7776b97d2be3
diff --git a/crates/ai/src/rebuild/feed.rs b/crates/ai/src/rebuild/feed.rsindex 9988c809..73ee002f 100644--- a/crates/ai/src/rebuild/feed.rs+++ b/crates/ai/src/rebuild/feed.rs@@ -216,8 +216,8 @@ mod tests {let inbox = Inbox::new(ws.inbox_dir());let id = inbox.post(craft_anything_engine::inbox::RequestKind::Question, "Blue?", "", vec![], true, None).unwrap();let q = inbox.pending().into_iter().find(|r| r.id == id).unwrap();- log.record(&RunEvent::Ask(q));- record_answer(&ws, &id, "Yes").unwrap();+ log.record(&RunEvent::Ask(q.clone()));+ record_answer(&ws, &q, "Yes").unwrap();log.record(&RunEvent::Finished { ok: true, message: "Ready.".into(), paused: false });assert!(load(&ws).iter().any(|i| matches!(i, FeedItem::Ask { answer: Some(a), .. } if a == "Yes")));}diff --git a/crates/engine/src/feed.rs b/crates/engine/src/feed.rsindex c7c605a9..f442c027 100644--- a/crates/engine/src/feed.rs+++ b/crates/engine/src/feed.rs@@ -144,10 +144,15 @@ pub fn save(root: &Path, feed: &[FeedItem]) -> Result<(), ToolError> {crate::project::write_atomic(&root.join(FEED_FILE), &json)}-/// Record an answer given while no run is going (straight into the saved feed).-pub fn record_answer(ws: &Workspace, id: &str, shown: &str) -> Result<(), ToolError> {+/// Record an answer given while no run is going (straight into the saved feed). `request` is the+/// question, taken before it was answered: one the saved conversation doesn't have yet (posted+/// from outside a run) is added with its answer, rather than lost once it stops waiting (#176).+pub fn record_answer(ws: &Workspace, request: &Request, shown: &str) -> Result<(), ToolError> {let mut feed = load(ws);- answered(&mut feed, id, shown);+ if !feed.iter().any(|i| matches!(i, FeedItem::Ask { request: r, .. } if r.id == request.id)) {+ feed.push(FeedItem::Ask { request: request.clone(), answer: None });+ }+ answered(&mut feed, &request.id, shown);save(&ws.root, &feed)}@@ -183,10 +188,25 @@ mod tests {std::fs::write(ws.root.join(FEED_FILE), br#"[{"User":"Old request"}]"#).unwrap();assert!(matches!(load(&ws).as_slice(), [FeedItem::User(r)] if r == "Old request"));// Answers given with no run going are saved too.- Inbox::new(ws.inbox_dir()).post(RequestKind::Question, "Blue or red?", "", vec![], true, None).unwrap();- let id = load(&ws).iter().find_map(|i| if let FeedItem::Ask { request, .. } = i { Some(request.id.clone()) } else { None }).unwrap();- record_answer(&ws, &id, "Blue").unwrap();+ let inbox = Inbox::new(ws.inbox_dir());+ let id = inbox.post(RequestKind::Question, "Blue or red?", "", vec![], true, None).unwrap();+ let question = inbox.pending().into_iter().find(|r| r.id == id).unwrap();+ inbox.answer(&id, &crate::inbox::Answer { choice: Some("Blue".into()), ..Default::default() }).unwrap();+ record_answer(&ws, &question, "Blue").unwrap();assert!(std::fs::read_to_string(ws.root.join(FEED_FILE)).unwrap().contains("\"Blue\""));+ // #176: one the saved conversation never had (posted from outside a run, answered before+ // anything saved it) is kept with its answer, once, and still after the inbox forgets it.+ save(&ws.root, &[FeedItem::User("Clone it".into())]).unwrap();+ let id = inbox.post(RequestKind::Question, "Which scope?", "", vec![], true, None).unwrap();+ let question = inbox.pending().into_iter().find(|r| r.id == id).unwrap();+ inbox.answer(&id, &crate::inbox::Answer { choice: Some("Minimal".into()), ..Default::default() }).unwrap();+ let _ = std::fs::remove_file(inbox.dir().join(format!("{id}.request.json")));+ let _ = std::fs::remove_file(inbox.dir().join(format!("{id}.answer.json")));+ record_answer(&ws, &question, "Minimal").unwrap();+ let asks = |f: &[FeedItem]| f.iter().filter(|i| matches!(i, FeedItem::Ask { request, answer: Some(a) } if request.id == id && a == "Minimal")).count();+ assert_eq!(asks(&load(&ws)), 1, "kept with its answer");+ record_answer(&ws, &question, "Minimal").unwrap();+ assert_eq!(asks(&load(&ws)), 1, "not twice");// One from a newer Craft Anything is shown as far as we can, never written over.let newer = br#"{"version":99,"items":[{"User":"From the future"}],"extra":1}"#;std::fs::write(ws.root.join(FEED_FILE), newer).unwrap();diff --git a/crates/engine/src/tools/project_tools.rs b/crates/engine/src/tools/project_tools.rsindex a388e6c3..438c84b4 100644--- a/crates/engine/src/tools/project_tools.rs+++ b/crates/engine/src/tools/project_tools.rs@@ -436,10 +436,11 @@ fn project_inbox_answer(_ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput,let p: AnswerParams = parse_params(params)?;let ws = open(&p.project)?;let inbox = Inbox::new(ws.inbox_dir());- if !inbox.pending().iter().any(|r| r.id == p.id) {+ // Taken before it's answered: the session that asked may read and remove it right after.+ let Some(question) = inbox.pending().into_iter().find(|r| r.id == p.id) else {return Err(ToolError::new(ErrorCode::NotFound, format!("No question {} is waiting in this project.", p.id)).with_remediation("Use project_show to see the questions still waiting."));- }+ };let answer = Answer { choice: p.choice, text: p.text, allow: p.allow, always: p.always };let shown = match answer.allow {Some(true) if answer.always => "Always allowed".to_string(),@@ -449,7 +450,7 @@ fn project_inbox_answer(_ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput,};inbox.answer(&p.id, &answer)?;// Into the saved conversation; a running build keeps answers it finds there when it saves.- if let Err(e) = feed::record_answer(&ws, &p.id, &shown) {+ if let Err(e) = feed::record_answer(&ws, &question, &shown) {log::warn!("answer not added to the conversation: {}", e.message);}Ok(ToolOutput::Value(json!({"answered": p.id, "shown": shown})))diff --git a/crates/ui-egui/src/project.rs b/crates/ui-egui/src/project.rsindex 8760aedf..e07d9f24 100644--- a/crates/ui-egui/src/project.rs+++ b/crates/ui-egui/src/project.rs@@ -220,7 +220,12 @@ impl CraftApp {pub fn answer_request(&mut self, id: &str, answer: Answer, shown: String) {let r = match (self.open_run(), &self.workspace) {(Some(run), _) => run.answer_as(id, &answer, &shown),- (None, Some(ws)) => Inbox::new(ws.inbox_dir()).answer(id, &answer).and_then(|()| feed::record_answer(ws, id, &shown)),+ (None, Some(ws)) => {+ // The question, taken before it's answered: whoever asked may remove it right after.+ let inbox = Inbox::new(ws.inbox_dir());+ let question = inbox.pending().into_iter().find(|r| r.id == id);+ inbox.answer(id, &answer).and_then(|()| question.map_or(Ok(()), |q| feed::record_answer(ws, &q, &shown)))+ }_ => return,};if let Err(e) = r {