Skip to content

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

Rust

fix(hooks): preserve BOM-prefixed marker routing

AkitaOnRails1a257aeb479e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index da1fca12..7e338f70 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -114,6 +114,9 @@ version in [`docs/changelog/`](docs/changelog/):
page, aligning belief input aggregation with the lint's resolved/stale split.
([#1386](https://github.com/akitaonrails/ai-memory/pull/1386))
- Kept the previous backup or OKF export intact when an archive download is interrupted, and publish completed downloads atomically. Successful downloads replace destination symlinks with private files rather than changing their targets or inheriting their permissions. ([#1381](https://github.com/akitaonrails/ai-memory/pull/1381))
+- Read a first-line `.ai-memory.toml` scope key, flag, or `aliases`
+ declaration when the marker starts with a UTF-8 BOM, in both native and
+ legacy POSIX hook routing. ([#1457](https://github.com/akitaonrails/ai-memory/pull/1457))
## [3.1.1] - 2026-10-17
diff --git a/crates/ai-memory-cli/src/marker.rs b/crates/ai-memory-cli/src/marker.rs
index cdeedfb5..94c25e94 100644
--- a/crates/ai-memory-cli/src/marker.rs
+++ b/crates/ai-memory-cli/src/marker.rs
@@ -1242,7 +1242,8 @@ fn declares_more_than_capture(text: &str) -> bool {
.any(|key| parse_flag_in(text, key).is_some())
|| server_selection_in(text).is_some()
|| text.lines().any(|line| {
- line.trim_start()
+ line.trim_start_matches('\u{feff}')
+ .trim_start()
.strip_prefix("aliases")
.is_some_and(|rest| rest.trim_start().starts_with('='))
})
diff --git a/crates/ai-memory-core/fixtures/remote_identity_cases.json b/crates/ai-memory-core/fixtures/remote_identity_cases.json
index 7417e9cb..dd5e75d9 100644
--- a/crates/ai-memory-core/fixtures/remote_identity_cases.json
+++ b/crates/ai-memory-core/fixtures/remote_identity_cases.json
@@ -59,6 +59,7 @@
{ "value": "", "style": null }
],
"marker_aliases": [
+ { "toml": "\ufeffaliases = [\"former-name\"]", "status": "valid", "aliases": ["former-name"] },
{ "toml": "aliases = [\" former-name \", \"legacy_name\", \"former-name\", \"V2.name\"]", "status": "valid", "aliases": ["former-name", "legacy_name", "V2.name"] },
{ "toml": "aliases = []", "status": "valid", "aliases": [] },
{ "toml": "aliases = [ ]", "status": "valid", "aliases": [] },
diff --git a/crates/ai-memory-core/src/repository_identity.rs b/crates/ai-memory-core/src/repository_identity.rs
index 82a80657..9de525ac 100644
--- a/crates/ai-memory-core/src/repository_identity.rs
+++ b/crates/ai-memory-core/src/repository_identity.rs
@@ -198,7 +198,9 @@ pub fn parse_marker_aliases(text: &str) -> Result<Option<MarkerAliases>, MarkerA
let mut found = None;
let mut in_table = false;
for line in text.lines() {
- let trimmed = line.trim();
+ // A UTF-8 BOM is not whitespace to `trim`, but a marker saved as
+ // "UTF-8 with BOM" still declares its first-line aliases.
+ let trimmed = line.trim_start_matches('\u{feff}').trim();
if trimmed.starts_with('[') {
in_table = true;
}
diff --git a/hooks/_lib.sh b/hooks/_lib.sh
index 137ed9a7..0e28e5a8 100644
--- a/hooks/_lib.sh
+++ b/hooks/_lib.sh
@@ -61,11 +61,18 @@ ai_memory_find_marker() {
# tables). Returns the first match or nothing. Ignores comments and
# blank lines by construction (the regex only matches the `key = "..."`
# shape).
+ai_memory_strip_utf8_bom() {
+ # Byte spelling keeps this portable across GNU/BSD sed and the awk images
+ # used in CI; LC_ALL=C makes the three bytes ordinary pattern characters.
+ LC_ALL=C sed "1s/^$(printf '\357\273\277')//"
+}
+
ai_memory_parse_toml_key() {
file="$1"; key="$2"
[ -f "$file" ] || return 0
- sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\1/p" \
- "$file" | head -n 1
+ ai_memory_strip_utf8_bom <"$file" \
+ | sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\1/p" \
+ | head -n 1
}
# Like ai_memory_parse_toml_key but also accepts a BARE value
@@ -76,8 +83,9 @@ ai_memory_parse_toml_key() {
ai_memory_parse_toml_flag() {
file="$1"; key="$2"
[ -f "$file" ] || return 0
- sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"?([^\"#]*)\"?.*/\1/p" \
- "$file" | head -n 1 | sed 's/[[:space:]]*$//'
+ ai_memory_strip_utf8_bom <"$file" \
+ | sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"?([^\"#]*)\"?.*/\1/p" \
+ | head -n 1 | sed 's/[[:space:]]*$//'
}
# A resolved marker's `[profile]` flag as the explicit value the hook sends:
@@ -104,7 +112,8 @@ ai_memory_marker_declares_settings() {
for key in workspace project project_strategy drop_subagent_captures identity identity_style; do
[ -n "$(ai_memory_parse_toml_key "$file" "$key")" ] && return 0
done
- LC_ALL=C grep -Eq '^[[:space:]]*aliases[[:space:]]*=' "$file" && return 0
+ ai_memory_strip_utf8_bom <"$file" \
+ | LC_ALL=C grep -Eq '^[[:space:]]*aliases[[:space:]]*=' && return 0
ai_memory_marker_declares_server "$file" && return 0
for key in default_global inject_on_session_start max_chars contribute consume; do
[ -n "$(ai_memory_parse_toml_flag "$file" "$key")" ] && return 0
diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.sh
index 0ca3689e..4b45365b 100755
--- a/tests/hooks/test_lib.sh
+++ b/tests/hooks/test_lib.sh
@@ -64,6 +64,16 @@ assert_eq "parse project_strategy" "repo-root" "$(ai_memory_parse_toml_key "
assert_eq "absent key returns empty" "" "$(ai_memory_parse_toml_key "$TMP/sample.toml" missing)"
assert_eq "absent file returns empty" "" "$(ai_memory_parse_toml_key "$TMP/no-such-file.toml" workspace)"
+printf '\357\273\277workspace = "bom-workspace"\ndefault_global = true\n' >"$TMP/bom.toml"
+assert_eq "BOM does not hide first-line key" "bom-workspace" \
+ "$(ai_memory_parse_toml_key "$TMP/bom.toml" workspace)"
+assert_eq "BOM does not hide first-line flag" "true" \
+ "$(ai_memory_parse_toml_flag "$TMP/bom.toml" default_global)"
+printf '\357\273\277aliases = ["former-name"]\n' >"$TMP/bom-aliases.toml"
+bom_aliases_declared=no
+ai_memory_marker_declares_settings "$TMP/bom-aliases.toml" && bom_aliases_declared=yes
+assert_eq "BOM does not hide first-line aliases boundary" "yes" "$bom_aliases_declared"
+
# --- find_marker ------------------------------------------------------
mkdir -p "$TMP/a/b/c/d"
printf 'workspace = "deep"\n' >"$TMP/a/.ai-memory.toml"