Skip to content

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

Rust

fix(dev): keep the pre-push block's shell state inside its subshell

Éverton Toffanetto8926124f5b80
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index c81eab50..dbbfd124 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -88,7 +88,9 @@ Bypass it on a work-in-progress branch with `git push --no-verify`.
The managed test block clears Git's repository environment and disables global
and system Git configuration for Cargo and its children. Fixture commands can
then use their own repositories without inheriting the checkout being pushed.
-The publishing Git process and other hook code retain their configuration.
+The publishing Git process and other hook code retain their configuration, and
+the block's shell options stay inside it; a failing test run still fails the
+hook even when your own commands follow the block without `set -e`.
Run the installer again to update an existing installation.
Integration tests live in `tests/suite/` per crate and compile into the
diff --git a/crates/ai-memory-cli/tests/suite/packaging.rs b/crates/ai-memory-cli/tests/suite/packaging.rs
index 52902c52..c8d6336c 100644
--- a/crates/ai-memory-cli/tests/suite/packaging.rs
+++ b/crates/ai-memory-cli/tests/suite/packaging.rs
@@ -1170,6 +1170,10 @@ const FIXTURE_TOOLS: &[&str] = &[
"awk", "bash", "cat", "chmod", "env", "git", "grep", "mkdir", "mktemp", "mv", "sort", "uname",
];
+/// The runner's last line, which hands over to the installed hook.
+#[cfg(any(unix, windows))]
+const RUNNER_EXEC: &str = "exec bash \"$AI_MEMORY_FIXTURE_HOOK\"\n";
+
/// Give up on the pre-push tests, or refuse to.
///
/// A contributor whose machine lacks Git Bash or a fixture tool gets a skip,
@@ -1528,7 +1532,10 @@ impl PrePushFixture {
printf '{name}-begin\n'
env | grep '^GIT_' | LC_ALL=C sort
printf '{name}-end\n'
- printf '{name}-ifs:[%s]\n' "$IFS"
+ printf '{name}-ifs:[%s]\n' "${{IFS-<unset>}}"
+ printf '{name}-opts:[%s]\n' "$-"
+ printf '{name}-pipefail:[%s]\n' "$([[ -o pipefail ]] && echo on || echo off)"
+ printf '{name}-ssl:[%s]\n' "${{SSL_CERT_FILE-<unset>}}"
printf '{name}-toplevel:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" rev-parse --show-toplevel 2>&1)"
printf '{name}-injected:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.injected 2>&1)"
printf '{name}-counted:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.counted 2>&1)"
@@ -1558,10 +1565,28 @@ impl PrePushFixture {
if with_nextest {
runner.push_str("cargo-nextest() { return 0; }\nexport -f cargo-nextest\n");
}
- runner.push_str("exec bash \"$AI_MEMORY_FIXTURE_HOOK\"\n");
+ runner.push_str(RUNNER_EXEC);
std::fs::write(&self.runner, runner).unwrap();
}
+ /// Make the hook's `git rev-parse --local-env-vars` fail with `code`. The
+ /// exported function shadows `git` for that one call and defers to the real
+ /// binary for everything else, including the probes.
+ fn fail_local_env_vars(&self, code: i32) {
+ let runner = std::fs::read_to_string(&self.runner).unwrap();
+ let prelude = runner
+ .strip_suffix(RUNNER_EXEC)
+ .expect("write_runner must run first");
+ let git = format!(
+ "git() {{\n\
+ \x20 if [ \"$1\" = rev-parse ] && [ \"$2\" = --local-env-vars ]; then return {code}; fi\n\
+ \x20 command git \"$@\"\n\
+ }}\n\
+ export -f git\n"
+ );
+ std::fs::write(&self.runner, format!("{prelude}{git}{RUNNER_EXEC}")).unwrap();
+ }
+
/// Seed a user hook whose own content precedes the managed block.
fn write_user_hook(&self, prelude: &str) {
std::fs::write(
@@ -1761,6 +1786,16 @@ fn pre_push_hook_leaves_the_user_hook_environment_intact_around_the_block() {
);
}
}
+ // The block's own shell options and exports stay inside it too: the block
+ // is replaced in place, so user commands after it run with whatever it
+ // leaves behind.
+ for key in ["opts", "pipefail", "ssl"] {
+ assert_eq!(
+ log_field(&log, &format!("after-{key}")),
+ log_field(&log, &format!("before-{key}")),
+ "the managed block's {key} leaked into the trailing user content:\n{log}"
+ );
+ }
}
#[test]
@@ -1821,6 +1856,39 @@ fn pre_push_hook_reinstall_keeps_user_content_and_one_managed_block() {
assert!(!String::from_utf8_lossy(&failed.stdout).contains("order-after"));
}
+#[test]
+#[cfg(any(unix, windows))]
+fn pre_push_hook_stops_before_cargo_and_user_content_when_the_scrub_fails() {
+ let Some(fixture) = PrePushFixture::new() else {
+ return;
+ };
+ fixture.write_runner(0, false);
+ fixture.fail_local_env_vars(23);
+ // No `set -e` in the user hook: the block alone has to stop the push.
+ fixture.write_user_hook("");
+ fixture.install_hook();
+ fixture.append_after_managed_block();
+
+ let output = fixture.run_hook();
+ assert_eq!(
+ output.status.code(),
+ Some(23),
+ "a failed scrub must fail the hook: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(
+ !fixture.cargo_log().contains("cargo-argv:"),
+ "Cargo ran without the scrub:\n{}",
+ fixture.cargo_log()
+ );
+ let caller = fixture.caller_log();
+ assert!(
+ caller.contains("before-begin") && !caller.contains("after-begin"),
+ "the user's trailing content ran after the block failed:\n{caller}"
+ );
+}
+
#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_installs_from_a_linked_worktree_subdirectory() {
diff --git a/scripts/install-git-hooks.sh b/scripts/install-git-hooks.sh
index 60722910..43a5d354 100755
--- a/scripts/install-git-hooks.sh
+++ b/scripts/install-git-hooks.sh
@@ -17,6 +17,8 @@ if git config --get core.hooksPath >/dev/null; then
echo 'core.hooksPath is set; this installer only manages the shared repository hook directory' >&2
exit 1
else
+ # `$?` is still the condition's status here: 1 means the key is unset, and
+ # anything else is a Git failure the installer must not guess past.
config_status=$?
if [[ "$config_status" -ne 1 ]]; then
exit "$config_status"
@@ -29,21 +31,24 @@ end="# <<< ai-memory pre-push <<<"
managed_block=$(cat <<'HOOK'
# >>> ai-memory pre-push >>>
# Runs the full test tier before a push. See scripts/install-git-hooks.sh.
-set -euo pipefail
-
-# macOS: stop reqwest re-reading the Keychain in every test process.
-if [ "$(uname -s 2>/dev/null || true)" = "Darwin" ] && [ -z "${SSL_CERT_FILE:-}" ] && [ -f /etc/ssl/cert.pem ]; then
- export SSL_CERT_FILE=/etc/ssl/cert.pem
-fi
# Git's hook environment would redirect fixture commands into this checkout.
-# Isolate Cargo and its children so other hook code keeps its Git context.
+# Isolate Cargo and its children, and keep this block's shell options and
+# exports away from user hook code around it.
(
+ set -euo pipefail
+
+ # macOS: stop reqwest re-reading the Keychain in every test process.
+ if [ "$(uname -s 2>/dev/null || true)" = "Darwin" ] && [ -z "${SSL_CERT_FILE:-}" ] && [ -f /etc/ssl/cert.pem ]; then
+ export SSL_CERT_FILE=/etc/ssl/cert.pem
+ fi
+
# A plain assignment, so `set -e` still aborts if `git rev-parse` fails.
git_local_env_vars=$(git rev-parse --local-env-vars)
# A surrounding user hook may have narrowed IFS; the list below is split on
- # newlines, so restore the default before splitting it.
- IFS=$' \t\n'
+ # newlines, so fall back to the default before splitting it. Unset rather
+ # than assigned: Bash 3.2 expands ANSI-C quoting inside this heredoc.
+ unset IFS
# Unquoted on purpose: the output is one variable name per line.
for git_local_env_var in $git_local_env_vars; do
unset "$git_local_env_var"
@@ -60,6 +65,14 @@ fi
cargo test --workspace --all-targets
fi
)
+# Bash ignores `set -e` inside a subshell tested by `||`, `&&`, `!` or `if`, so
+# the subshell stays a plain command and its status is propagated here. A user
+# hook without `set -e` would otherwise run on and let the push through.
+ai_memory_pre_push_status=$?
+if [ "$ai_memory_pre_push_status" -ne 0 ]; then
+ exit "$ai_memory_pre_push_status"
+fi
+unset ai_memory_pre_push_status
# <<< ai-memory pre-push <<<
HOOK
)
@@ -69,6 +82,7 @@ if [[ -f "$hook" ]]; then
if grep -q '[^[:space:]]' "$hook"; then
has_content=1
else
+ # As above, `$?` is grep's status: 1 is a blank hook, 2 a read error.
read_status=$?
if [[ "$read_status" -ne 1 ]]; then
exit "$read_status"