Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): read a spool entry's fields in linear time (#727)
Everton Muniz043703d1c07a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f82367a3..e63d656e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed+- The POSIX shell hook bundle's spool reader no longer costs time quadratic in+ the entry size. `ai_memory_json_field` rebuilt the decoded value one character+ at a time, so reading a multi-megabyte entry — one large tool result is enough+ — took minutes of CPU, and a drain pass reads every entry three times (`url`,+ `body`, `token`) while one detached pass starts behind every delivery that+ succeeds. Once an outage had filled the spool, the passes accumulated faster+ than they retired and saturated the machine. The value is now bounded by a+ single regex pass over the JSON string grammar and unescaped with `gsub` over+ whole segments, for identical output and exit codes: a 2.2 MB entry goes from+ 529 s to 0.29 s, and a 400 KB entry from 15.0 s to 0.07 s (#727).+## [2.2.1] - 2026-09-12### Fixeddiff --git a/hooks/_lib.sh b/hooks/_lib.shindex d4f2fa23..0c473060 100644--- a/hooks/_lib.sh+++ b/hooks/_lib.sh@@ -621,10 +621,21 @@ ai_memory_spool_event() {}# Read one top-level string field out of a spool entry, undoing the escapes-# `ai_memory_json_string` produces. Scans left to right, which is the only-# correct way to find the closing quote. An entry carrying a `\uXXXX` escape-# was written by a richer serializer (the native binary); this prints nothing-# for it so the caller leaves it to `ai-memory hook-drain`.+# `ai_memory_json_string` produces. The regex is the JSON string grammar, so+# the match ends at the first quote that is not escaped, which is the only+# correct way to find the end. An entry carrying a `\uXXXX` escape was written+# by a richer serializer (the native binary); this prints nothing for it so the+# caller leaves it to `ai-memory hook-drain`.+#+# The value is unescaped with `gsub` over whole segments rather than one+# character at a time: appending per character makes the cost grow with the+# square of the value length, and a drain pass reads every spooled entry three+# times while one pass starts behind every delivery that succeeds, so one+# multi-megabyte entry used to cost minutes of CPU per pass. Splitting on the+# escaped-backslash pairs first is what makes the rest safe: no segment can+# contain one, so inside a segment every backslash starts a real escape and no+# placeholder byte is needed, which keeps a raw control byte in the value+# (`ai_memory_json_string` does not escape those) round-tripping untouched.ai_memory_json_field() {awk -v key="$1" '{ text = text (NR > 1 ? "\n" : "") $0 }@@ -632,27 +643,21 @@ ai_memory_json_field() {needle = "\"" key "\":\""start = index(text, needle)if (start == 0) exit 1- i = start + length(needle)- out = ""- while (i <= length(text)) {- c = substr(text, i, 1)- if (c == "\\") {- e = substr(text, i + 1, 1)- if (e == "n") out = out "\n"- else if (e == "t") out = out "\t"- else if (e == "r") out = out "\r"- else if (e == "\"") out = out "\""- else if (e == "\\") out = out "\\"- else if (e == "/") out = out "/"- else exit 1- i += 2- continue- }- if (c == "\"") { printf "%s", out; exit 0 }- out = out c- i += 1+ rest = substr(text, start + length(needle))+ if (!match(rest, /^(\\.|[^"\\])*/)) exit 1+ if (substr(rest, RSTART + RLENGTH, 1) != "\"") exit 1+ parts = split(substr(rest, RSTART, RLENGTH), seg, /\\\\/)+ for (i = 1; i <= parts; i++) {+ if (seg[i] ~ /\\[^ntr"\/]/) exit 1+ gsub(/\\n/, "\n", seg[i])+ gsub(/\\t/, "\t", seg[i])+ gsub(/\\r/, "\r", seg[i])+ gsub(/\\"/, "\"", seg[i])+ gsub(/\\\//, "/", seg[i])+ out = (i == 1 ? seg[i] : out "\\" seg[i])}- exit 1+ printf "%s", out+ exit 0}' "$2"}