Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(hooks): keep the SessionEnd reason in the session-end observation

AkitaOnRails6eae5f0efd6d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 344fd15c..7c672ab4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
+- Added the end reason to session-end observations. When a harness's
+ `SessionEnd` payload carries a `reason` (Claude Code sends `clear`,
+ `logout`, `prompt_input_exit` or `other`), its first line is stored,
+ sanitized, as the observation body, so consolidation can tell a normal
+ quit from anything else, and the zero-LLM session page closes its summary
+ with `ended (<reason>)`. Payloads without a reason are stored as before.
+ (#1225)
+
- The web UI shows pending cross-project mail. Project cards carry an amber
`N inbox` badge when messages are waiting for an agent in that project, the
home page summarises the total and links the projects holding mail, and the
diff --git a/crates/ai-memory-hooks/src/payload.rs b/crates/ai-memory-hooks/src/payload.rs
index 38b4e3ec..680689a6 100644
--- a/crates/ai-memory-hooks/src/payload.rs
+++ b/crates/ai-memory-hooks/src/payload.rs
@@ -1166,6 +1166,12 @@ fn best_body_excerpt(event: HookEvent, raw: &serde_json::Value) -> Option<String
}
HookEvent::Notification => extract_content(raw, &["message", "text"]),
HookEvent::PostCompaction => extract_content(raw, &["summary"]),
+ // Why the session ended (#1225): a string only, first line only, so
+ // consolidation and the zero-LLM page can tell a normal quit from
+ // anything else.
+ HookEvent::SessionEnd => extract_string(raw, &["reason"])
+ .and_then(first_line)
+ .map(|reason| format!("reason: {reason}")),
_ => None,
}
}
@@ -2503,6 +2509,31 @@ mod tests {
assert_eq!(env.agent, AgentKind::Other);
}
+ /// #1225: a SessionEnd's `reason` becomes the observation body (first
+ /// line, string only), so why a session ended survives capture. An
+ /// absent, empty or non-string reason leaves the body empty as before.
+ #[test]
+ fn session_end_reason_becomes_the_observation_body() {
+ let q = HookQuery {
+ event: "session-end".into(),
+ agent: Some("claude-code".into()),
+ ..Default::default()
+ };
+ let body = |raw| HookEnvelope::from_query_and_body(q.clone(), raw).body_excerpt;
+ assert_eq!(
+ body(serde_json::json!({ "reason": "prompt_input_exit" })).as_deref(),
+ Some("reason: prompt_input_exit")
+ );
+ assert_eq!(
+ body(serde_json::json!({ "reason": "logout\ninjected second line" })).as_deref(),
+ Some("reason: logout")
+ );
+ assert_eq!(body(serde_json::json!({})), None);
+ assert_eq!(body(serde_json::json!({ "reason": "" })), None);
+ assert_eq!(body(serde_json::json!({ "reason": 7 })), None);
+ assert_eq!(body(serde_json::json!({ "reason": { "kind": "x" } })), None);
+ }
+
/// Title-hint extraction must truncate at the first newline (the
/// "first line" rule used everywhere in the wiki log + handoff
/// surfaces), but must NOT cap length here any more: the 80-char display
diff --git a/crates/ai-memory-hooks/src/synth.rs b/crates/ai-memory-hooks/src/synth.rs
index d1bee23d..cac8fe98 100644
--- a/crates/ai-memory-hooks/src/synth.rs
+++ b/crates/ai-memory-hooks/src/synth.rs
@@ -286,6 +286,19 @@ fn session_summary(tally: &SessionTally<'_>) -> String {
parts.push(format!("over {spent}"));
}
+ // The harness's own word for why the session ended (#1225), when the
+ // SessionEnd observation carried one. Display-bounded: a summary line
+ // is no place for a long free-text field.
+ if let Some(reason) = tally
+ .end
+ .and_then(|end| end.body.strip_prefix("reason: "))
+ .map(str::trim)
+ .filter(|reason| !reason.is_empty())
+ {
+ let shown: String = reason.chars().take(60).collect();
+ parts.push(format!("ended ({shown})"));
+ }
+
if parts.is_empty() {
return String::new();
}
@@ -548,6 +561,45 @@ mod tests {
);
}
+ /// #1225: the harness's end reason, when captured, closes the summary;
+ /// a long one is display-bounded.
+ #[test]
+ fn the_summary_states_why_the_session_ended() {
+ let end = |body: &str| {
+ let mut o = obs_at(
+ ObservationKind::SessionEnd,
+ "session",
+ "2026-08-24T10:05:00Z",
+ );
+ o.body = body.into();
+ o
+ };
+ let start = obs_at(
+ ObservationKind::SessionStart,
+ "session",
+ "2026-08-24T10:00:00Z",
+ );
+ let prompt = obs(ObservationKind::UserPrompt, "ship it");
+ assert_eq!(
+ session_summary(&tally_session(&[
+ start.clone(),
+ prompt.clone(),
+ end("reason: logout")
+ ])),
+ "1 prompt, over 5m, ended (logout)."
+ );
+ assert_eq!(
+ session_summary(&tally_session(&[start.clone(), prompt.clone(), end("")])),
+ "1 prompt, over 5m."
+ );
+ let long = format!("reason: {}", "x".repeat(200));
+ let summary = session_summary(&tally_session(&[start, prompt, end(&long)]));
+ assert!(
+ summary.ends_with(&format!("ended ({}).", "x".repeat(60))),
+ "{summary}"
+ );
+ }
+
#[test]
fn the_summary_is_shaped_so_the_reader_does_not_drop_it() {
// The store prefers `summary` over the body and then runs it through