Skip to content

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

Rust

feat(core): recognize a "reload context" prompt

Giovanni Coutinho1ff18c4c6d51
diff --git a/crates/ai-memory-core/src/compaction_recap.rs b/crates/ai-memory-core/src/compaction_recap.rs
new file mode 100644
index 00000000..74d40521
--- /dev/null
+++ b/crates/ai-memory-core/src/compaction_recap.rs
@@ -0,0 +1,61 @@
+//! The "reload context" request: a prompt asking for a recap of the session
+//! stretch the last compaction dropped. Pure; the hook client checks it
+//! before fetching `GET /recap`, and the OpenCode plugins get the same rule
+//! as a regex.
+
+const PHRASE: &str = "reload context";
+
+/// [`is_reload_request`] as a JavaScript regex literal, for the generated
+/// OpenCode plugins.
+pub const RELOAD_TRIGGER_JS_REGEX: &str = r"/^\s*reload context(?![\p{L}\p{N}_])/iu";
+
+/// `true` when the prompt opens with `reload context` (ASCII
+/// case-insensitive, after leading whitespace) as whole words. Anchored at
+/// the start so a sentence that only mentions the phrase does not fire.
+#[must_use]
+pub fn is_reload_request(prompt: &str) -> bool {
+ let rest = prompt.trim_start();
+ let Some(head) = rest.get(..PHRASE.len()) else {
+ return false;
+ };
+ if !head.eq_ignore_ascii_case(PHRASE) {
+ return false;
+ }
+ rest[PHRASE.len()..]
+ .chars()
+ .next()
+ .is_none_or(|c| !(c.is_alphanumeric() || c == '_'))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reload_trigger_table() {
+ for (prompt, want) in [
+ ("reload context", true),
+ ("reload context after last compaction, then continue", true),
+ (" Reload Context: then fix the build", true),
+ ("RELOAD CONTEXT\nand go", true),
+ ("reload context.", true),
+ ("reload contexts", false),
+ ("reload context_x", false),
+ ("reload contexté", false),
+ ("please reload context", false),
+ ("can you reload context later?", false),
+ ("reload", false),
+ ("", false),
+ ] {
+ assert_eq!(is_reload_request(prompt), want, "{prompt:?}");
+ }
+ }
+
+ #[test]
+ fn js_regex_names_the_same_phrase() {
+ assert_eq!(
+ RELOAD_TRIGGER_JS_REGEX,
+ r"/^\s*reload context(?![\p{L}\p{N}_])/iu"
+ );
+ }
+}
diff --git a/crates/ai-memory-core/src/ids.rs b/crates/ai-memory-core/src/ids.rs
index eb841be2..455bc24a 100644
--- a/crates/ai-memory-core/src/ids.rs
+++ b/crates/ai-memory-core/src/ids.rs
@@ -468,6 +468,19 @@ impl AgentKind {
matches!(self, Self::KimiCode)
}
+ /// Whether this agent adds a `UserPromptSubmit` hook's
+ /// `hookSpecificOutput.additionalContext` to the turn it belongs to.
+ ///
+ /// Claude Code places it alongside the submitted prompt and Codex adds it
+ /// as developer context (both hook references). The compaction recap uses
+ /// this channel. Grok discards an allowing `UserPromptSubmit`'s output,
+ /// and Kimi Code injects raw stdout rather than this envelope, so neither
+ /// is listed.
+ #[must_use]
+ pub fn user_prompt_injects_context(self) -> bool {
+ matches!(self, Self::ClaudeCode | Self::Codex)
+ }
+
/// Whether `PostToolUse` stdout is model-visible context.
///
/// Grok Build reads that stdout and delivers `hookSpecificOutput.additionalContext`
@@ -807,6 +820,18 @@ mod tests {
}
}
+ #[test]
+ fn user_prompt_context_injection_is_claude_code_and_codex() {
+ for agent in AgentKind::ALL {
+ assert_eq!(
+ agent.user_prompt_injects_context(),
+ matches!(agent, AgentKind::ClaudeCode | AgentKind::Codex),
+ "{}",
+ agent.as_str()
+ );
+ }
+ }
+
#[test]
fn page_path_rejects_backslashes_and_windows_prefixes() {
assert!(PagePath::new(r"notes\secret.md").is_err());
diff --git a/crates/ai-memory-core/src/lib.rs b/crates/ai-memory-core/src/lib.rs
index 10f2e966..0661a3cf 100644
--- a/crates/ai-memory-core/src/lib.rs
+++ b/crates/ai-memory-core/src/lib.rs
@@ -8,6 +8,7 @@
pub mod active_project;
pub mod actor;
pub mod agent_backup;
+pub mod compaction_recap;
pub mod error;
pub mod fix_lines;
pub mod handoff;