@memnexus-ai/mx-agent-cli 0.1.238 → 0.1.239
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -20,7 +20,12 @@ set -u
|
|
|
20
20
|
exec 3>&2 || true
|
|
21
21
|
|
|
22
22
|
INPUT=$(cat)
|
|
23
|
-
TOOL_NAME
|
|
23
|
+
# `|| TOOL_NAME=""` is load-bearing: a bare assignment from a pipeline carries
|
|
24
|
+
# the pipeline's status, so a missing or broken jq (127) would reach the ERR trap
|
|
25
|
+
# above and take the hook out silently — no reminder, no error, for the rest of
|
|
26
|
+
# the session. Degrading to an empty tool name instead means the call is treated
|
|
27
|
+
# as "not a memory save", which over-reminds rather than never reminding.
|
|
28
|
+
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty' 2>/dev/null) || TOOL_NAME=""
|
|
24
29
|
|
|
25
30
|
WORKTREE_NAME="${CLAUDE_WORKTREE_NAME:-default}"
|
|
26
31
|
# Sanitize: alphanumeric, hyphens, underscores only — prevent path traversal
|
|
@@ -52,7 +57,7 @@ if [[ "$TOOL_NAME" == *"create_memory"* || "$TOOL_NAME" == *"manage_memory"* ]];
|
|
|
52
57
|
fi
|
|
53
58
|
|
|
54
59
|
if [[ "$TOOL_NAME" == "Bash" ]]; then
|
|
55
|
-
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
60
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null) || COMMAND=""
|
|
56
61
|
if [[ "$COMMAND" == *"mx memories create"* || "$COMMAND" == *"mx memories update"* ]]; then
|
|
57
62
|
IS_MEMORY_SAVE=1
|
|
58
63
|
fi
|
|
@@ -86,6 +91,97 @@ if [[ "$CURRENT" -ge "$THRESHOLD" ]]; then
|
|
|
86
91
|
# Reset counter — after save + retry, next 50 calls proceed unblocked
|
|
87
92
|
echo "0" > "$COUNTER_FILE" 2>/dev/null || true
|
|
88
93
|
|
|
94
|
+
# ── 3a. Resolve codeContext for the suggested command (#4652) ──────────────
|
|
95
|
+
# Deliberately INSIDE the threshold branch. This hook runs on every single
|
|
96
|
+
# tool call; the resolved slug is used once per 50. Resolving it on the hot
|
|
97
|
+
# path taxed every call for a result thrown away 49 times out of 50, and a
|
|
98
|
+
# pathological marker file made that tax unbounded.
|
|
99
|
+
#
|
|
100
|
+
# Without --code-context every hook-forced checkpoint lands with codeContext
|
|
101
|
+
# null, so it does not scope to a team and other sessions' searches surface it
|
|
102
|
+
# by accident. The command we print is the command agents run verbatim, so the
|
|
103
|
+
# flag has to be IN the printed command, not in prose next to it.
|
|
104
|
+
#
|
|
105
|
+
# Slug resolution copies coordination-brief.sh's MF-2 rule: the
|
|
106
|
+
# .mx-coordination-team marker is the ONLY source, because it is an explicit
|
|
107
|
+
# assignment written by an onboarding step. We do NOT derive from
|
|
108
|
+
# CLAUDE_WORKTREE_NAME or CLAUDE_TEAM_NAME — those are the unreliable
|
|
109
|
+
# heuristics that design rejects, and a confidently WRONG team is worse than a
|
|
110
|
+
# placeholder the agent has to fill in consciously.
|
|
111
|
+
MARKER_FILE="${CLAUDE_WORKTREE_PATH:-}/.mx-coordination-team"
|
|
112
|
+
TEAM_SLUG=""
|
|
113
|
+
if [[ -n "${CLAUDE_WORKTREE_PATH:-}" && -f "$MARKER_FILE" ]]; then
|
|
114
|
+
# Read a BOUNDED prefix. A slug is at most 50 bytes; 512 is generous and
|
|
115
|
+
# bounds the work regardless of what the file actually contains. Without the
|
|
116
|
+
# cap, a large no-newline file is read in full before the first line ends.
|
|
117
|
+
#
|
|
118
|
+
# Every capture handles its own failure (`|| VAR=""`) rather than leaving a
|
|
119
|
+
# non-zero status in reach of this script's ERR trap: if `head` is missing
|
|
120
|
+
# the resolution degrades to the placeholder instead of taking the hook down.
|
|
121
|
+
TEAM_SLUG=$(head -c 512 "$MARKER_FILE" 2>/dev/null | head -n1) || TEAM_SLUG=""
|
|
122
|
+
|
|
123
|
+
# Reject a marker containing a NUL byte. Bash strips NUL from command
|
|
124
|
+
# substitution output, so "plat\0form" would otherwise arrive as the
|
|
125
|
+
# plausible-looking slug "platform" and pass validation — the same
|
|
126
|
+
# silently-mangled-into-a-different-team failure the whitespace handling
|
|
127
|
+
# below exists to prevent. `read -d ''` returns 0 only when it actually
|
|
128
|
+
# found a NUL delimiter, which makes it a NUL detector; the same 512-byte
|
|
129
|
+
# cap applies. Pure bash, no new dependency.
|
|
130
|
+
if head -c 512 "$MARKER_FILE" 2>/dev/null | IFS= read -r -d '' _nul_probe; then
|
|
131
|
+
TEAM_SLUG=""
|
|
132
|
+
fi
|
|
133
|
+
|
|
134
|
+
# TRIM leading/trailing whitespace in pure bash — no `sed`, no `tr`.
|
|
135
|
+
# Not `tr -d '[:space:]'`: that DELETES interior whitespace too, turning a
|
|
136
|
+
# marker reading "has space" into the valid-looking slug "hasspace".
|
|
137
|
+
# Interior whitespace must survive to the regex so it fails and yields the
|
|
138
|
+
# placeholder. Pure-bash form also keeps a missing external binary from
|
|
139
|
+
# being able to influence this at all.
|
|
140
|
+
TEAM_SLUG="${TEAM_SLUG#"${TEAM_SLUG%%[![:space:]]*}"}"
|
|
141
|
+
TEAM_SLUG="${TEAM_SLUG%"${TEAM_SLUG##*[![:space:]]}"}"
|
|
142
|
+
fi
|
|
143
|
+
|
|
144
|
+
# Validate before interpolating: the marker is a file on disk and its contents
|
|
145
|
+
# land inside a shell command we are telling an agent to run. Same whole-string
|
|
146
|
+
# regex coordination-brief.sh validates with (bash [[ =~ ]] anchors ^/$ to the
|
|
147
|
+
# string ends, so an embedded newline fails), plus its two extra exclusions —
|
|
148
|
+
# no '--' and not the reserved 'all'. Keeping the validators identical matters:
|
|
149
|
+
# a slug coordination-brief rejects is not a usable team anywhere in the fleet,
|
|
150
|
+
# so scoping a memory to it would be as wrong as guessing. Invalid → placeholder.
|
|
151
|
+
is_valid_team_slug() {
|
|
152
|
+
local s="$1"
|
|
153
|
+
case "$s" in
|
|
154
|
+
*--*) return 1 ;;
|
|
155
|
+
all) return 1 ;;
|
|
156
|
+
esac
|
|
157
|
+
[[ "$s" =~ ^[a-z][a-z0-9-]{0,48}[a-z0-9]$ ]]
|
|
158
|
+
}
|
|
159
|
+
if ! is_valid_team_slug "$TEAM_SLUG"; then
|
|
160
|
+
TEAM_SLUG=""
|
|
161
|
+
fi
|
|
162
|
+
|
|
163
|
+
# Project: MX_PRODUCT_SLUG → mx-agent.config.json projectSlug → memnexus.
|
|
164
|
+
# Same precedence as coordination-brief.sh. jq is a pre-existing dependency of
|
|
165
|
+
# this hook (section 1 parses the tool payload with it); the trim is pure bash.
|
|
166
|
+
PROJECT_SLUG="${MX_PRODUCT_SLUG:-}"
|
|
167
|
+
if [[ -z "$PROJECT_SLUG" && -n "${CLAUDE_WORKTREE_PATH:-}" && -f "${CLAUDE_WORKTREE_PATH}/mx-agent.config.json" ]]; then
|
|
168
|
+
PROJECT_SLUG=$(jq -r '.projectSlug // .project.slug // empty' "${CLAUDE_WORKTREE_PATH}/mx-agent.config.json" 2>/dev/null) || PROJECT_SLUG=""
|
|
169
|
+
PROJECT_SLUG="${PROJECT_SLUG%%$'\n'*}"
|
|
170
|
+
PROJECT_SLUG="${PROJECT_SLUG#"${PROJECT_SLUG%%[![:space:]]*}"}"
|
|
171
|
+
PROJECT_SLUG="${PROJECT_SLUG%"${PROJECT_SLUG##*[![:space:]]}"}"
|
|
172
|
+
fi
|
|
173
|
+
if ! is_valid_team_slug "$PROJECT_SLUG"; then
|
|
174
|
+
PROJECT_SLUG="memnexus"
|
|
175
|
+
fi
|
|
176
|
+
|
|
177
|
+
if [[ -n "$TEAM_SLUG" ]]; then
|
|
178
|
+
CODE_CONTEXT="{\"product\":\"${PROJECT_SLUG}\",\"team\":\"${TEAM_SLUG}\"}"
|
|
179
|
+
CODE_CONTEXT_NOTE=""
|
|
180
|
+
else
|
|
181
|
+
CODE_CONTEXT="{\"product\":\"${PROJECT_SLUG}\",\"team\":\"<YOUR-TEAM-SLUG>\"}"
|
|
182
|
+
CODE_CONTEXT_NOTE=" Replace <YOUR-TEAM-SLUG> with the 'team' value from your roleguide's codeContext — the team marker could not be read, and a guessed team scopes the memory to the wrong search."
|
|
183
|
+
fi
|
|
184
|
+
|
|
89
185
|
# Route to the preserved real stderr (fd 3) — exit-2 PreToolUse messages
|
|
90
186
|
# surface via stderr, not stdout (KI-HOOKS-STDERR-SYSTEMIC). fd 2 may have been
|
|
91
187
|
# redirected to /dev/null by the flock exec above, so fd 3 is used here.
|
|
@@ -93,7 +189,14 @@ if [[ "$CURRENT" -ge "$THRESHOLD" ]]; then
|
|
|
93
189
|
echo ""
|
|
94
190
|
echo "⚠️ Memory checkpoint required: ${CURRENT} tool calls since last memory save."
|
|
95
191
|
echo "1. Save a checkpoint now (required before continuing):"
|
|
96
|
-
echo " mx memories create --conversation-id \"<conv_id>\" --content \"[what you've done]. Next: [next step].\" --topics \"<component>,in-progress\""
|
|
192
|
+
echo " mx memories create --conversation-id \"<conv_id>\" --content \"[what you've done]. Next: [next step].\" --topics \"<component>,in-progress\" --code-context '${CODE_CONTEXT}'"
|
|
193
|
+
# `if`, not `[[ ... ]] && echo`. Policy, not a claim about this line: never
|
|
194
|
+
# leave a failing test within reach of an ERR trap. Whether a given position
|
|
195
|
+
# is exempt depends on where the list sits and how it is nested — too fragile
|
|
196
|
+
# to reason about per-site or to preserve across edits (FL-13).
|
|
197
|
+
if [[ -n "$CODE_CONTEXT_NOTE" ]]; then
|
|
198
|
+
echo "$CODE_CONTEXT_NOTE"
|
|
199
|
+
fi
|
|
97
200
|
echo "2. After saving, retry your original tool call."
|
|
98
201
|
echo ""
|
|
99
202
|
} >&3
|