Skip to content

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

Rust

fix(hooks): wrap the PowerShell session-start handoff in its harness envelope

Rudy Mizrahi Celekli25a3d91881d3
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index 503ebb6a..7dce5ed0 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -11425,6 +11425,48 @@ model = "gpt-5"
assert!(inferred.is_none());
}
+ /// A `session-start.sh` that wraps the handoff in Claude Code's
+ /// `hookSpecificOutput` envelope must have a PowerShell peer that asks for
+ /// the same envelope. Bare text does not start with "{", so the harness
+ /// logs "Hook output does not start with {, treating as plain text" and the
+ /// handoff arrives unstructured — the exact failure the POSIX half was
+ /// written to avoid. Enumerated rather than listed, so the next bundle
+ /// cannot ship uncovered.
+ #[test]
+ fn a_posix_session_start_envelope_has_a_powershell_peer() {
+ let hooks_root = Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("..")
+ .join("..")
+ .join("hooks");
+ let mut checked = 0usize;
+ for entry in fs::read_dir(&hooks_root).expect("hooks dir") {
+ let dir = entry.expect("hooks entry").path();
+ if !dir.is_dir() || dir.file_name().and_then(|n| n.to_str()) == Some("lib") {
+ continue;
+ }
+ let posix = dir.join("session-start.sh");
+ let shell = dir.join("session-start.ps1");
+ if !posix.is_file() || !shell.is_file() {
+ continue;
+ }
+ let posix_src = fs::read_to_string(&posix).expect("read session-start.sh");
+ if !posix_src.contains("hookSpecificOutput") {
+ continue;
+ }
+ let shell_src = fs::read_to_string(&shell).expect("read session-start.ps1");
+ assert!(
+ shell_src.contains("-SessionStartEnvelope"),
+ "{} emits a hookSpecificOutput envelope but its PowerShell peer does not request one",
+ posix.display()
+ );
+ checked += 1;
+ }
+ assert!(
+ checked >= 3,
+ "expected at least the claude-code, command-code and devin bundles to emit the envelope, found {checked}"
+ );
+ }
+
#[test]
fn bundled_posix_and_powershell_hooks_stay_in_parity() {
let hooks_root = Path::new(env!("CARGO_MANIFEST_DIR"))
diff --git a/hooks/claude-code/session-start.ps1 b/hooks/claude-code/session-start.ps1
index 309675d5..07c7f65f 100644
--- a/hooks/claude-code/session-start.ps1
+++ b/hooks/claude-code/session-start.ps1
@@ -1,3 +1,3 @@
. "$PSScriptRoot\..\lib\ai-memory-hook.ps1"
-Invoke-AiMemoryHook -Event "session-start" -Agent "claude-code" -FetchHandoff
+Invoke-AiMemoryHook -Event "session-start" -Agent "claude-code" -FetchHandoff -SessionStartEnvelope
exit 0
diff --git a/hooks/command-code/session-start.ps1 b/hooks/command-code/session-start.ps1
index acb47f1d..be050bae 100644
--- a/hooks/command-code/session-start.ps1
+++ b/hooks/command-code/session-start.ps1
@@ -1,3 +1,3 @@
. "$PSScriptRoot\..\lib\ai-memory-hook.ps1"
-Invoke-AiMemoryHook -Event "session-start" -Agent "command-code" -FetchHandoff
+Invoke-AiMemoryHook -Event "session-start" -Agent "command-code" -FetchHandoff -SessionStartEnvelope
exit 0
diff --git a/hooks/devin/session-start.ps1 b/hooks/devin/session-start.ps1
index c60138cb..5bfece6e 100644
--- a/hooks/devin/session-start.ps1
+++ b/hooks/devin/session-start.ps1
@@ -1,3 +1,3 @@
. "$PSScriptRoot\..\lib\ai-memory-hook.ps1"
-Invoke-AiMemoryHook -Event "session-start" -Agent "devin" -FetchHandoff
+Invoke-AiMemoryHook -Event "session-start" -Agent "devin" -FetchHandoff -SessionStartEnvelope
exit 0
diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1
index 788600be..bc7674bb 100644
--- a/hooks/lib/ai-memory-hook.ps1
+++ b/hooks/lib/ai-memory-hook.ps1
@@ -991,6 +991,12 @@ function Invoke-AiMemoryHook {
# `injectSteps` system message. With -AntigravityPreInvocation the
# payload is a PreInvocation, a start boundary only at invocation 0.
[switch] $AntigravityStartOutput,
+ # Claude Code prepends a SessionStart hook's stdout to the resuming
+ # session. Bare text does not start with "{", so it logs every start as
+ # "Hook output does not start with {, treating as plain text" and the
+ # handoff lands unstructured. The POSIX peers of these bundles already
+ # emit the envelope; this is their PowerShell half.
+ [switch] $SessionStartEnvelope,
[switch] $AntigravityPreInvocation,
# Deliver the `[briefing]` compiled project brief on the FIRST
# handoff fetch of a session only (kimi-code's user-prompt path:
@@ -1216,14 +1222,22 @@ function Invoke-AiMemoryHook {
injectSteps = @(@{ systemMessage = @{ systemMessage = $Response.Content } })
}
[Console]::Out.Write(($Payload | ConvertTo-Json -Depth 5 -Compress))
+ } elseif ($SessionStartEnvelope) {
+ $Payload = @{
+ hookSpecificOutput = @{
+ hookEventName = "SessionStart"
+ additionalContext = [string]$Response.Content
+ }
+ }
+ [Console]::Out.Write(($Payload | ConvertTo-Json -Depth 5 -Compress))
} else {
[Console]::Out.Write($Response.Content)
}
- } elseif ($AntigravityStartOutput -or $GrokPostTool -or $CopilotCliSessionStartOutput) {
+ } elseif ($AntigravityStartOutput -or $GrokPostTool -or $CopilotCliSessionStartOutput -or $SessionStartEnvelope) {
[Console]::Out.Write("{}")
}
} catch {
- if ($AntigravityStartOutput -or $GrokPostTool -or $CopilotCliSessionStartOutput) {
+ if ($AntigravityStartOutput -or $GrokPostTool -or $CopilotCliSessionStartOutput -or $SessionStartEnvelope) {
[Console]::Out.Write("{}")
}
}