Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): flag abrupt endings in the zero-LLM session page (#1221)
AkitaOnRails321b7ceffc2a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 60eed928..6d4be533 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Fixed+- Fixed the zero-LLM session page staying silent about abrupt endings. The+ rule-based synthesis now adds a deterministic terminal-state line reusing+ the automatic handoff's heuristics: "Session ended without a normal stop;+ the final prompt was never answered." when the log ends on an unanswered+ question, the equivalent line when file activity was never followed by a+ stop, and a mid-task-exit line when a stop was recorded but no session+ end arrived. A session with a normal stop and end gets no line. (#1221)- Fixed consolidation dropping the tail of a session from the compiledpage. The single and batch prompts now carry explicit rules: say so whenthe observation log ends on an unanswered prompt or without a normaldiff --git a/crates/ai-memory-hooks/src/synth.rs b/crates/ai-memory-hooks/src/synth.rsindex 1e85c516..d1bee23d 100644--- a/crates/ai-memory-hooks/src/synth.rs+++ b/crates/ai-memory-hooks/src/synth.rs@@ -10,10 +10,11 @@ use std::collections::BTreeMap;use ai_memory_core::{AgentKind, NewPage, Observation, ObservationKind, PagePath, ProjectId, SessionId, Tier,- WorkspaceId, looks_like_scaffolding, truncate_for_title,+ WorkspaceId, looks_like_markup_block, looks_like_scaffolding, truncate_for_title,};use jiff::tz::TimeZone;+use crate::capture_policy::ToolFamily;use crate::payload::is_safe_tool_title;const RAW_OBSERVATION_MAX_LINES: usize = 500;@@ -352,6 +353,10 @@ fn render_body(}buf.push_str(&format!("- **observations:** {}\n\n", observations.len()));+ if let Some(note) = abrupt_ending_note(observations) {+ buf.push_str(&format!("{note}\n\n"));+ }+if !prompts.is_empty() {buf.push_str("## Prompts\n\n");for (i, p) in prompts.iter().enumerate() {@@ -375,6 +380,66 @@ fn render_body(buf}+/// One deterministic line flagging an abrupt ending, or `None` when the+/// session closed normally (a Stop and a SessionEnd are both on record).+///+/// Mirrors the multi-signal heuristics the router's `derive_open_questions`+/// uses for automatic SessionEnd handoffs, first match wins, so the+/// zero-LLM page states the same terminal state a handoff would flag+/// (#1221):+/// 1. The final substantive prompt is an unanswered question and no Stop+/// ever arrived after it.+/// 2. The last completed tool call touched files and no Stop followed —+/// the session ended while working in the tree.+/// 3. A Stop was recorded but no SessionEnd — a mid-task exit (or a+/// checkpoint page rendered while the session is still running; the+/// wording only claims what the log shows).+fn abrupt_ending_note(observations: &[Observation]) -> Option<&'static str> {+ let last_of_kind = |kind: ObservationKind| observations.iter().rev().find(|o| o.kind == kind);+ let last_stop = last_of_kind(ObservationKind::Stop);+ let has_session_end = observations+ .iter()+ .any(|o| o.kind == ObservationKind::SessionEnd);+ // Harness blocks delivered as a user turn are not the user's question.+ let last_prompt = observations.iter().rev().find(|o| {+ let text = if o.body.is_empty() { &o.title } else { &o.body };+ o.kind == ObservationKind::UserPrompt && !looks_like_markup_block(text)+ });++ if last_stop.is_none()+ && let Some(prompt) = last_prompt+ {+ let text = if prompt.body.is_empty() {+ prompt.title.trim()+ } else {+ prompt.body.trim()+ };+ if text.ends_with('?') || text.ends_with('?') {+ return Some(+ "Session ended without a normal stop; the final prompt was never answered.",+ );+ }+ }++ if last_stop.is_none()+ && let Some(tool) = last_of_kind(ObservationKind::PostToolUse)+ && crate::payload::tool_family_from_title(&tool.title) == Some(ToolFamily::File)+ {+ return Some(+ "Session ended without a normal stop while working with files; \+ the working tree may hold uncommitted or unverified changes.",+ );+ }++ if last_stop.is_some() && !has_session_end {+ return Some(+ "A stop was recorded but no session end; the session may have exited mid-task.",+ );+ }++ None+}+fn render_raw_observations(buf: &mut String, observations: &[Observation]) {if observations.len() <= RAW_OBSERVATION_MAX_LINES {for obs in observations {@@ -960,6 +1025,110 @@ mod tests {assert!(page.body.contains("`other [fstech:lead.contact]`"));}+ /// #1221: the zero-LLM page must say when a session ended abruptly,+ /// reusing the `derive_open_questions` heuristics. A final prompt that+ /// is a question, with no Stop after it, means the prompt was never+ /// answered.+ #[test]+ fn an_unanswered_final_prompt_flags_the_abrupt_ending() {+ let observations = vec![+ obs(ObservationKind::SessionStart, "session"),+ obs(ObservationKind::UserPrompt, "why is the cache empty?"),+ obs(ObservationKind::PostToolUse, "tool non-file"),+ ];+ let page = synthesize_session_page(+ WorkspaceId::new(),+ ProjectId::new(),+ SessionId::new(),+ AgentKind::Codex,+ &observations,+ );+ assert!(+ page.body.contains(+ "Session ended without a normal stop; the final prompt was never answered."+ ),+ "the abrupt-ending line must be in the body: {}",+ page.body+ );+ }++ /// File activity with no subsequent Stop: the session ended while+ /// working in the tree, so the page must say the changes may be+ /// unverified.+ #[test]+ fn file_activity_without_a_stop_flags_the_abrupt_ending() {+ let observations = vec![+ obs(ObservationKind::SessionStart, "session"),+ obs(ObservationKind::UserPrompt, "rework the retry loop"),+ obs(ObservationKind::PostToolUse, "tool file"),+ ];+ let page = synthesize_session_page(+ WorkspaceId::new(),+ ProjectId::new(),+ SessionId::new(),+ AgentKind::Codex,+ &observations,+ );+ assert!(+ page.body+ .contains("Session ended without a normal stop while working with files"),+ "the file-activity line must be in the body: {}",+ page.body+ );+ }++ /// A Stop with no SessionEnd is a mid-task exit.+ #[test]+ fn a_stop_without_session_end_flags_the_mid_task_exit() {+ let observations = vec![+ obs(ObservationKind::SessionStart, "session"),+ obs(ObservationKind::UserPrompt, "rework the retry loop"),+ obs(ObservationKind::Stop, "stop"),+ ];+ let page = synthesize_session_page(+ WorkspaceId::new(),+ ProjectId::new(),+ SessionId::new(),+ AgentKind::Codex,+ &observations,+ );+ assert!(+ page.body.contains("may have exited mid-task"),+ "the mid-task line must be in the body: {}",+ page.body+ );+ }++ /// Control: a clean SessionStart → prompt → tool → Stop → SessionEnd+ /// session gets no terminal-state line at all.+ #[test]+ fn a_cleanly_closed_session_has_no_abrupt_ending_line() {+ let observations = vec![+ obs(ObservationKind::SessionStart, "session"),+ obs(ObservationKind::UserPrompt, "rework the retry loop"),+ obs(ObservationKind::PostToolUse, "Edit"),+ obs(ObservationKind::Stop, "stop"),+ obs(ObservationKind::SessionEnd, "session-end"),+ ];+ let page = synthesize_session_page(+ WorkspaceId::new(),+ ProjectId::new(),+ SessionId::new(),+ AgentKind::Codex,+ &observations,+ );+ assert!(+ !page.body.contains("Session ended without a normal stop"),+ "a normal close must not be flagged: {}",+ page.body+ );+ assert!(+ !page.body.contains("may have exited mid-task"),+ "a normal close must not be flagged: {}",+ page.body+ );+ }+#[test]fn raw_observations_small_session_includes_all_entries() {let observations: Vec<Observation> = (0..5)