Skip to content

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

Rust

handoff UX: tighter render + agent guidance for single-use semantics

AkitaOnRailsc34dab0d697b
diff --git a/CLAUDE.md b/CLAUDE.md
index 84ff6b0f..8a8a9474 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -167,8 +167,9 @@ match the intent to the tool. They do not need to name the tool.
| "is ai-memory healthy?" / "how big is the wiki?" | `memory_status` |
| "give me the stats" / structured snapshot for the agent to consume | `memory_briefing` |
| "catch me up" / "I've been away" / "what's important right now?" / open-ended exploration | `memory_explore` |
-| "where did we leave off?" / first turn of a new session | `memory_handoff_accept` |
-| "save context for the next session" / wrapping up | `memory_handoff_begin` |
+| "where did we leave off?" — and you see a `📥 ai-memory: pending handoff` block in your context | already done — answer from that block; do NOT re-call `memory_handoff_accept` |
+| "where did we leave off?" — and no such block is visible | `memory_handoff_accept` (rare; the SessionStart hook usually got there first) |
+| "save context for the next session" / wrapping up | `memory_handoff_begin` (terse summary; put detail in `open_questions` + `next_steps` bullets) |
| "consolidate this session" / "compile what we learned" (usually automatic) | `memory_consolidate` |
| "audit the wiki" / "find contradictions" / "what rules should we add?" | `memory_lint` |
| "prune old pages" / "memory cleanup" | `memory_forget_sweep` |
@@ -198,3 +199,6 @@ It's idempotent: re-runs replace the block bracketed by
`<!-- ai-memory:start -->` / `<!-- ai-memory:end -->` markers
without disturbing the rest of the file.
<!-- ai-memory:end -->
+` markers
+without disturbing the rest of the file.
+<!-- ai-memory:end -->
diff --git a/crates/ai-memory-cli/src/commands/install_instructions.rs b/crates/ai-memory-cli/src/commands/install_instructions.rs
index 8b243088..dd1521db 100644
--- a/crates/ai-memory-cli/src/commands/install_instructions.rs
+++ b/crates/ai-memory-cli/src/commands/install_instructions.rs
@@ -49,8 +49,9 @@ match the intent to the tool. They do not need to name the tool.
| "is ai-memory healthy?" / "how big is the wiki?" | `memory_status` |
| "give me the stats" / structured snapshot for the agent to consume | `memory_briefing` |
| "catch me up" / "I've been away" / "what's important right now?" / open-ended exploration | `memory_explore` |
-| "where did we leave off?" / first turn of a new session | `memory_handoff_accept` |
-| "save context for the next session" / wrapping up | `memory_handoff_begin` |
+| "where did we leave off?" — and you see a `📥 ai-memory: pending handoff` block in your context | already done — answer from that block; do NOT re-call `memory_handoff_accept` |
+| "where did we leave off?" — and no such block is visible | `memory_handoff_accept` (rare; the SessionStart hook usually got there first) |
+| "save context for the next session" / wrapping up | `memory_handoff_begin` (terse summary; put detail in `open_questions` + `next_steps` bullets) |
| "consolidate this session" / "compile what we learned" (usually automatic) | `memory_consolidate` |
| "audit the wiki" / "find contradictions" / "what rules should we add?" | `memory_lint` |
| "prune old pages" / "memory cleanup" | `memory_forget_sweep` |
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index 7a0767d3..c0b5e4f1 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -127,36 +127,63 @@ async fn fetch_and_accept_handoff(
}
fn render_handoff_markdown(h: &Handoff) -> String {
+ // Layout goal: TUI-renderable + agent-friendly. The previous
+ // shape put a paragraph-long `## Summary` first, which made the
+ // hook output look like a wall of text in Codex's "completed"
+ // block AND let the agent miss that this *is* the answer to
+ // "where did we leave off" questions. The new layout leads
+ // with the actionable bullets (open questions, next steps) and
+ // pushes the prose summary to the bottom; the agent-facing
+ // footer explicitly tells the model how to interpret a follow-up
+ // memory_handoff_accept = null.
let mut buf = String::with_capacity(512);
- buf.push_str("> ai-memory — pending handoff from previous session\n");
+ buf.push_str("> 📥 **ai-memory: pending handoff from previous session**\n");
buf.push_str(&format!(
- "> from: {from} | created: {ts}\n",
+ "> from `{from}` · created {ts}\n",
from = h.from_agent.as_str(),
ts = h.created_at,
));
- buf.push_str("\n## Summary\n");
- buf.push_str(&h.summary);
+
if !h.open_questions.is_empty() {
- buf.push_str("\n\n## Open questions\n");
+ buf.push_str("\n**Open questions**\n");
for q in &h.open_questions {
buf.push_str(&format!("- {q}\n"));
}
}
if !h.next_steps.is_empty() {
- buf.push_str("\n## Next steps\n");
+ buf.push_str("\n**Next steps**\n");
for s in &h.next_steps {
buf.push_str(&format!("- {s}\n"));
}
}
if !h.files_touched.is_empty() {
- buf.push_str("\n## Files touched recently\n");
+ buf.push_str("\n**Files touched**\n");
for f in &h.files_touched {
buf.push_str(&format!("- `{f}`\n"));
}
}
+
+ // Summary last, as reference prose. Models reading top-down
+ // see the action items first; the summary is detail.
+ buf.push_str("\n**Summary**\n");
+ buf.push_str(h.summary.trim());
+ buf.push('\n');
+
+ // Agent-facing reading instructions. This block is the
+ // load-bearing UX fix — without it, agents call
+ // memory_handoff_accept again, get `null` (single-use
+ // already consumed by this hook), and conclude "no handoff"
+ // *despite this content being right in their context*.
buf.push_str(
- "\n_Tip: call `memory_query` or `memory_recent` to recover \
- more detail from prior sessions._\n",
+ "\n---\n\
+ _**To the receiving agent:** this content IS the pending \
+ handoff — already consumed by the SessionStart hook. A \
+ subsequent `memory_handoff_accept` call will return \
+ `{ \"handoff\": null }` (single-use). When the user asks \
+ \"where did we leave off?\" or \"any pending handoff?\", \
+ answer from THIS content; do NOT re-call the tool. Call \
+ `memory_query` / `memory_recent` only for additional \
+ context beyond what's listed here._\n",
);
buf
}
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rs
index b659ad79..79175704 100644
--- a/crates/ai-memory-mcp/src/server.rs
+++ b/crates/ai-memory-mcp/src/server.rs
@@ -43,10 +43,15 @@ for them:\n\
arg. Use over memory_briefing when the user asks open-ended \
questions like 'catch me up' or 'what's important right now'.\n\
- `memory_handoff_accept` — when the user asks 'where did we leave \
- off'. The SessionStart hook also auto-fetches this.\n\
+ off'. The SessionStart hook auto-fetches + consumes the handoff \
+ before you see your first prompt; if a block starting with \
+ '📥 ai-memory: pending handoff' is anywhere in your context, \
+ THAT is the handoff — answer from it directly, don't re-call \
+ this tool (it'll return null because handoffs are single-use).\n\
- `memory_handoff_begin` — when the user is wrapping up and you \
want to ensure the next agent has context (the SessionEnd hook \
- also auto-captures this).\n\
+ also auto-captures this). Keep the summary terse (2-3 sentences); \
+ put detail in open_questions + next_steps bullets.\n\
- `memory_consolidate` — when the user asks to compile session \
observations into wiki pages (usually automatic at session end).\n\
- `memory_lint` — when the user asks to audit the wiki for stale \
@@ -453,10 +458,19 @@ impl AiMemoryServer {
}
/// Create a handoff snapshot for the next agent CLI.
- #[tool(description = "Record a cross-agent handoff snapshot. Call this \
- before quitting one CLI so the next one (e.g. Codex picking up \
- after Claude Code) can fetch context via memory_handoff_accept. \
- Use cwd to scope the handoff to a specific working directory.")]
+ #[tool(description = "Record a cross-agent handoff snapshot for the \
+ NEXT agent that opens this project (e.g. Codex picking up after \
+ Claude Code). The next session's SessionStart hook automatically \
+ consumes the handoff and prepends its content to the agent's \
+ context — no manual fetch needed. \
+ \
+ Write style: keep `summary` to 2-3 SHORT sentences (what just \
+ happened + what state the project's in). Put actionable detail \
+ in `open_questions` and `next_steps` as bullet-sized strings — \
+ the next agent reads those first; long prose summaries make the \
+ TUI rendering ugly. `files_touched` is a hint, not exhaustive. \
+ \
+ Use `cwd` to scope the handoff to a specific working directory.")]
async fn memory_handoff_begin(
&self,
Parameters(args): Parameters<HandoffBeginArgs>,
@@ -489,12 +503,23 @@ impl AiMemoryServer {
/// Fetch the latest open handoff for this project (optionally filtered
/// by cwd) and mark it accepted.
- #[tool(description = "Fetch the latest open handoff for this project \
- and mark it accepted. The SessionStart hook already calls this \
- for you and prepends the result, so most sessions never need to \
- invoke it directly. Call it manually only when the user asks \
- 'where did we leave off?' or when you suspect a handoff was \
- missed. Returns summary + open questions + next steps.")]
+ #[tool(description = "Fetch the latest OPEN cross-agent handoff and \
+ mark it accepted. \
+ \
+ IMPORTANT: handoffs are SINGLE-USE. The SessionStart hook \
+ automatically consumes the handoff at session-start and prepends \
+ the content to your context — when you see a block starting with \
+ '📥 ai-memory: pending handoff from previous session' anywhere \
+ in your context, that IS the handoff. \
+ \
+ A subsequent call to this tool will return `{ \"handoff\": null }` \
+ because the hook already consumed it. Do NOT interpret null as \
+ 'no handoff exists' — check your context for the prepended block \
+ first, and answer the user from there. Call this tool only when \
+ you BOTH don't see a prepended block AND the user explicitly asks \
+ for a handoff (e.g. a hook script ran with no stdout capture). \
+ \
+ Returns the same JSON shape memory_handoff_begin accepted.")]
async fn memory_handoff_accept(
&self,
Parameters(args): Parameters<HandoffAcceptArgs>,