@memnexus-ai/mx-agent-cli 0.1.244 → 0.1.246
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.
|
@@ -22,7 +22,37 @@ if [ -n "${CLAUDE_WORKTREE_PATH:-}" ] && [ -f "${CLAUDE_WORKTREE_PATH}/.mx-leade
|
|
|
22
22
|
fi
|
|
23
23
|
|
|
24
24
|
INPUT=$(cat)
|
|
25
|
-
COMMAND
|
|
25
|
+
# `|| COMMAND=""` is load-bearing, and was missing before #4734. A bare assignment
|
|
26
|
+
# from a pipeline carries the pipeline's status, so a missing or broken jq (127)
|
|
27
|
+
# reached this script's ERR trap and exited 0 — silently disabling deploy
|
|
28
|
+
# verification for the whole session: no reminder, no error, on every call. This
|
|
29
|
+
# is the identical bug memory-checkpoint-reminder.sh:28 already documents and
|
|
30
|
+
# guards. Degrading to an empty command instead means the merge/verify signals
|
|
31
|
+
# are simply not detected, while an ALREADY-pending reminder still surfaces
|
|
32
|
+
# (with unknown attribution, since the session id parse degrades the same way).
|
|
33
|
+
# Over-reminding is the safe direction; going silent is not.
|
|
34
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null) || COMMAND=""
|
|
35
|
+
|
|
36
|
+
# ── Session attribution (#4734) ──────────────────────────────────────────────
|
|
37
|
+
# The harness supplies .session_id on every hook invocation. STATE_FILE below is
|
|
38
|
+
# keyed by WORKTREE ALONE, so without this a merge recorded by one session was
|
|
39
|
+
# reported to every OTHER session in the same worktree as that session's own
|
|
40
|
+
# unverified deploy. A git-expert's `gh pr merge` derailed an unrelated
|
|
41
|
+
# memory-creator's next Bash call, which correctly refused it as suspected prompt
|
|
42
|
+
# injection. Concurrent-session awareness already existed in the locking and
|
|
43
|
+
# guard hooks (ci-validate-hook.sh, git-mutation-guard.sh); it was never extended
|
|
44
|
+
# to this advisory one.
|
|
45
|
+
#
|
|
46
|
+
# `|| SESSION_ID=""` mirrors memory-checkpoint-reminder.sh:28: a missing or broken
|
|
47
|
+
# jq must degrade to "provenance unknown" — which still emits a reminder — rather
|
|
48
|
+
# than returning non-zero into this script's ERR trap and taking the hook out
|
|
49
|
+
# silently for the rest of the session.
|
|
50
|
+
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // empty' 2>/dev/null) || SESSION_ID=""
|
|
51
|
+
# Sanitize to the UUID alphabet. This value is written as a KEY=VALUE line in the
|
|
52
|
+
# state file, so an embedded newline would corrupt the record and could forge a
|
|
53
|
+
# second field. It is only ever COMPARED, never printed into the agent-visible
|
|
54
|
+
# message, so no harness-supplied text reaches agent context through this hook.
|
|
55
|
+
SESSION_ID="${SESSION_ID//[^a-zA-Z0-9_-]/}"
|
|
26
56
|
|
|
27
57
|
WORKTREE_NAME="${CLAUDE_WORKTREE_NAME:-default}"
|
|
28
58
|
# Sanitize: alphanumeric, hyphens, underscores only — prevent path traversal
|
|
@@ -74,7 +104,11 @@ if printf '%s\n' "$COMMAND" | grep -qE '(^|[;&|]\s*)gh\s+run\s+(list|view|watch)
|
|
|
74
104
|
touch "$VERIFY_SENT" 2>/dev/null || true
|
|
75
105
|
fi
|
|
76
106
|
# Clear state and merge/reminder sentinels so the next merge re-arms them.
|
|
77
|
-
|
|
107
|
+
# The per-session `other` notices go too (#4734): the pending merge is resolved,
|
|
108
|
+
# so the next one must be able to notify every session again. The glob is
|
|
109
|
+
# deliberately unquoted so it expands; it is bounded by STATE_FILE, whose
|
|
110
|
+
# worktree component is sanitized at the top of this script.
|
|
111
|
+
rm -f "$STATE_FILE" "$MERGE_SENT" "$REMIND_SENT" "${STATE_FILE}".notice-* 2>/dev/null || true
|
|
78
112
|
exit 0
|
|
79
113
|
fi
|
|
80
114
|
|
|
@@ -100,9 +134,15 @@ if [[ "$IS_MERGE" -eq 1 ]]; then
|
|
|
100
134
|
flock -x 200 2>/dev/null || true
|
|
101
135
|
fi
|
|
102
136
|
|
|
103
|
-
|
|
137
|
+
# SESSION_ID records WHICH session merged, so the reminder below can tell that
|
|
138
|
+
# session apart from the others sharing this worktree-keyed state file. An empty
|
|
139
|
+
# value (no session_id on stdin, or jq unavailable) is written as an empty field
|
|
140
|
+
# and read back as "unknown" — the same branch legacy state files take.
|
|
141
|
+
printf 'PR_NUMBER=%s\nTIMESTAMP=%s\nSESSION_ID=%s\n' "$PR_NUMBER" "$TIMESTAMP" "$SESSION_ID" > "$STATE_FILE" 2>/dev/null || true
|
|
104
142
|
# A new merge starts a new cycle — clear verify sentinel so the next verify re-arms.
|
|
105
|
-
|
|
143
|
+
# Per-session `other` notices clear too (#4734), so sessions already notified
|
|
144
|
+
# about the PREVIOUS merge are notified once about this new one.
|
|
145
|
+
rm -f "$VERIFY_SENT" "${STATE_FILE}".notice-* 2>/dev/null || true
|
|
106
146
|
# Don't fire reminder on the merge call itself — agent hasn't had a chance to verify yet.
|
|
107
147
|
# Gate to one-shot per cycle: emit only if merge sentinel doesn't exist yet.
|
|
108
148
|
if [ ! -f "$MERGE_SENT" ]; then
|
|
@@ -124,17 +164,88 @@ AGE=$((NOW - MTIME))
|
|
|
124
164
|
# Stale cleanup: older than 30 minutes → delete and move on.
|
|
125
165
|
# Also clear telemetry sentinels so the next merge-verify cycle re-arms them.
|
|
126
166
|
if [[ "$AGE" -gt 1800 ]]; then
|
|
127
|
-
rm -f "$STATE_FILE" "$MERGE_SENT" "$VERIFY_SENT" "$REMIND_SENT" 2>/dev/null || true
|
|
167
|
+
rm -f "$STATE_FILE" "$MERGE_SENT" "$VERIFY_SENT" "$REMIND_SENT" "${STATE_FILE}".notice-* 2>/dev/null || true
|
|
128
168
|
exit 0
|
|
129
169
|
fi
|
|
130
170
|
|
|
131
|
-
# Read stored PR number for message
|
|
171
|
+
# Read stored PR number for message.
|
|
172
|
+
#
|
|
173
|
+
# `-f2-` matches the SESSION_ID read below: take the whole value, then constrain
|
|
174
|
+
# it by CONTENT rather than relying on the field split to do it. Splitting at the
|
|
175
|
+
# first '=' silently discards anything after it, which hides malformed input
|
|
176
|
+
# instead of rejecting it.
|
|
177
|
+
#
|
|
178
|
+
# SANITIZE (security review, #4734): PR_REF is the ONLY state-file field that
|
|
179
|
+
# reaches the agent-visible message, and this change moved it from inside a
|
|
180
|
+
# `gh pr view ${PR_REF}` command argument — where stray text reads as noise — to
|
|
181
|
+
# the OPENING OF A SENTENCE in two of the three branches, where injected prose
|
|
182
|
+
# reads as narrative. A crafted /tmp state file could therefore put arbitrary
|
|
183
|
+
# text, including a fake "SYSTEM DIRECTIVE", into another agent's context. The
|
|
184
|
+
# hook only ever writes digits here (BASH_REMATCH from a [0-9]+ capture on the
|
|
185
|
+
# merge path), so restricting to digits is behaviour-preserving on every
|
|
186
|
+
# legitimate path; anything else empties the value and falls through to the
|
|
187
|
+
# `<PR>` placeholder below. Same treatment SESSION_ID and CLI_PACKAGE already get.
|
|
132
188
|
STORED_PR=""
|
|
133
189
|
if [[ -f "$STATE_FILE" ]]; then
|
|
134
|
-
STORED_PR=$(grep '^PR_NUMBER=' "$STATE_FILE" 2>/dev/null | cut -d= -f2 || true)
|
|
190
|
+
STORED_PR=$(grep '^PR_NUMBER=' "$STATE_FILE" 2>/dev/null | cut -d= -f2- || true)
|
|
135
191
|
fi
|
|
192
|
+
STORED_PR="${STORED_PR//[^0-9]/}"
|
|
136
193
|
PR_REF="${STORED_PR:-<PR>}"
|
|
137
194
|
|
|
195
|
+
# Read back the session that recorded the merge (#4734).
|
|
196
|
+
# `cut -d= -f2-` (not -f2) so a value containing '=' is not silently truncated
|
|
197
|
+
# into a DIFFERENT id — a truncated id that happened to still match would
|
|
198
|
+
# misattribute the merge, which is the bug this block exists to prevent.
|
|
199
|
+
# Re-sanitized on read: the file lives in world-writable /tmp, so its contents are
|
|
200
|
+
# not trusted to be well-formed even though this hook wrote them.
|
|
201
|
+
STORED_SESSION=""
|
|
202
|
+
if [[ -f "$STATE_FILE" ]]; then
|
|
203
|
+
STORED_SESSION=$(grep '^SESSION_ID=' "$STATE_FILE" 2>/dev/null | cut -d= -f2- || true)
|
|
204
|
+
fi
|
|
205
|
+
STORED_SESSION="${STORED_SESSION//[^a-zA-Z0-9_-]/}"
|
|
206
|
+
|
|
207
|
+
# Three-way attribution. Every branch still emits a message and still exits 2 —
|
|
208
|
+
# failing to attribute must never SWALLOW the reminder, only soften its claim.
|
|
209
|
+
# self → this session merged it; the original directive wording applies.
|
|
210
|
+
# other → a different concurrent session merged it; non-actionable notice.
|
|
211
|
+
# unknown → no stored id (state file written by the pre-#4734 hook, or the
|
|
212
|
+
# harness supplied no session_id, or jq was unavailable on either
|
|
213
|
+
# side). Assert neither; say so.
|
|
214
|
+
if [[ -z "$STORED_SESSION" || -z "$SESSION_ID" ]]; then
|
|
215
|
+
ATTRIBUTION="unknown"
|
|
216
|
+
elif [[ "$STORED_SESSION" == "$SESSION_ID" ]]; then
|
|
217
|
+
ATTRIBUTION="self"
|
|
218
|
+
else
|
|
219
|
+
ATTRIBUTION="other"
|
|
220
|
+
fi
|
|
221
|
+
|
|
222
|
+
# Per-session one-shot for the `other` branch (security review, #4734).
|
|
223
|
+
#
|
|
224
|
+
# The reminder path fires on EVERY Bash PostToolUse until the state clears, up to
|
|
225
|
+
# 30 minutes. For `self` that repetition is the intended nag and test 21 pins it.
|
|
226
|
+
# For `other` it is a defect: the message says no action is required, and the
|
|
227
|
+
# session cannot clear the state even if it wanted to, because clearing requires
|
|
228
|
+
# the `gh run list` call the message just told it was somebody else's job. A
|
|
229
|
+
# non-actionable notice repeated dozens of times would itself get escalated as an
|
|
230
|
+
# anomaly — a softer version of the failure this hook is being fixed for.
|
|
231
|
+
#
|
|
232
|
+
# Keyed by session so each concurrent session is told once. SESSION_ID is already
|
|
233
|
+
# constrained to [a-zA-Z0-9_-] at parse time, so it is safe in a filename; on this
|
|
234
|
+
# branch it is always non-empty (empty would have routed to `unknown`), and the
|
|
235
|
+
# `:-unknown` default is belt-and-braces.
|
|
236
|
+
#
|
|
237
|
+
# Suppressed repeats exit 0 with NO output, not exit 2. A non-zero exit with an
|
|
238
|
+
# empty stderr is what produced the "No stderr output" reports in
|
|
239
|
+
# KI-HOOKS-STDERR-SYSTEMIC; emitting nothing while claiming a warning is the bug,
|
|
240
|
+
# not the fix. Every branch that actually PRINTS still exits 2.
|
|
241
|
+
NOTICE_SENT="${STATE_FILE}.notice-${SESSION_ID:-unknown}"
|
|
242
|
+
if [[ "$ATTRIBUTION" == "other" ]]; then
|
|
243
|
+
if [[ -f "$NOTICE_SENT" ]]; then
|
|
244
|
+
exit 0
|
|
245
|
+
fi
|
|
246
|
+
touch "$NOTICE_SENT" 2>/dev/null || true
|
|
247
|
+
fi
|
|
248
|
+
|
|
138
249
|
# Resolve the published CLI package name shown in the "confirm artifact live"
|
|
139
250
|
# hint. Precedence: MX_CLI_PACKAGE env → cliPackage in mx-agent.config.json (jq)
|
|
140
251
|
# → '@memnexus-ai/mx-agent-cli' fallback. For THIS repo none of the first two are
|
|
@@ -161,11 +272,31 @@ CLI_PACKAGE="${CLI_PACKAGE:-@memnexus-ai/mx-agent-cli}"
|
|
|
161
272
|
# KI-HOOKS-STDERR-SYSTEMIC). The exit-2-per-call contract is unchanged.
|
|
162
273
|
{
|
|
163
274
|
echo ""
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
275
|
+
if [[ "$ATTRIBUTION" == "other" ]]; then
|
|
276
|
+
# Non-actionable by design: this session did not run the merge, so telling it
|
|
277
|
+
# to "verify the deploy and retry your command" would be a false directive —
|
|
278
|
+
# exactly the misdirection that got read as prompt injection. No numbered
|
|
279
|
+
# steps here; steps are what made the previous message look actionable.
|
|
280
|
+
echo "ℹ️ Notice: PR ${PR_REF} was merged by a different agent session running concurrently in this worktree."
|
|
281
|
+
echo "No action is required from you. Verifying that deploy is the responsibility of the session that ran the merge."
|
|
282
|
+
echo "You are seeing this because deploy-pending state is shared per worktree, not per session."
|
|
283
|
+
elif [[ "$ATTRIBUTION" == "unknown" ]]; then
|
|
284
|
+
# Provenance genuinely undetermined — state the uncertainty instead of
|
|
285
|
+
# asserting either ownership. Steps are offered CONDITIONALLY.
|
|
286
|
+
echo "⚠️ Deploy verification may be required: PR ${PR_REF} was merged in this worktree, but which session merged it could not be determined."
|
|
287
|
+
echo "If you ran that merge, verify it:"
|
|
288
|
+
echo "1. Get merge commit SHA: gh pr view ${PR_REF} --json mergeCommit --jq '.mergeCommit.oid'"
|
|
289
|
+
echo "2. Poll pipeline: gh run list --commit <sha> --json status,conclusion,name --jq '.[]'"
|
|
290
|
+
echo "3. Confirm artifact live: npm view ${CLI_PACKAGE} version"
|
|
291
|
+
echo "If you did not run it, no action is required from you."
|
|
292
|
+
else
|
|
293
|
+
# self — wording unchanged from before #4734.
|
|
294
|
+
echo "⚠️ Deploy verification required: PR was merged but pipeline not verified."
|
|
295
|
+
echo "1. Get merge commit SHA: gh pr view ${PR_REF} --json mergeCommit --jq '.mergeCommit.oid'"
|
|
296
|
+
echo "2. Poll pipeline: gh run list --commit <sha> --json status,conclusion,name --jq '.[]'"
|
|
297
|
+
echo "3. Confirm artifact live: npm view ${CLI_PACKAGE} version"
|
|
298
|
+
echo "After verifying, retry your command."
|
|
299
|
+
fi
|
|
169
300
|
echo ""
|
|
170
301
|
} >&2
|
|
171
302
|
# Flood prevention: the reminder path fires on every Bash PostToolUse until the
|
|
@@ -5,8 +5,19 @@
|
|
|
5
5
|
#
|
|
6
6
|
# Fires on: PreToolUse (empty matcher — all tool calls)
|
|
7
7
|
# Input: JSON on stdin with tool_name and tool_input
|
|
8
|
-
# Output: reminder message
|
|
9
|
-
# Exit:
|
|
8
|
+
# Output: reminder message on stderr (fd 3) when counter >= 50
|
|
9
|
+
# Exit: 2 when the reminder is emitted, 0 otherwise.
|
|
10
|
+
# The previous header here read "always 0 (never blocks tool calls)". That has
|
|
11
|
+
# not matched the code since the reminder moved to exit 2 (see the exit at the
|
|
12
|
+
# end of section 3), which is what makes a PreToolUse message visible to the
|
|
13
|
+
# agent at all. Corrected rather than "fixed" back to 0: exit 2 is the intended
|
|
14
|
+
# behaviour, and a header asserting a safety property the code does not have is
|
|
15
|
+
# the same defect class as the misattributed reminder #4734 addresses — in a
|
|
16
|
+
# file that runs on EVERY tool call for every agent.
|
|
17
|
+
#
|
|
18
|
+
# NOTE (#4734): the counter below is keyed by WORKTREE, not by session, so it
|
|
19
|
+
# aggregates tool calls across every agent session active in the worktree. That
|
|
20
|
+
# is deliberate — see the caveat emitted with the reminder in section 3.
|
|
10
21
|
|
|
11
22
|
# Fail-open: any error → exit 0 silently
|
|
12
23
|
trap 'exit 0' ERR
|
|
@@ -188,6 +199,13 @@ if [[ "$CURRENT" -ge "$THRESHOLD" ]]; then
|
|
|
188
199
|
{
|
|
189
200
|
echo ""
|
|
190
201
|
echo "⚠️ Memory checkpoint required: ${CURRENT} tool calls since last memory save."
|
|
202
|
+
# Attribution caveat (#4734). COUNTER_FILE is keyed by worktree alone, so this
|
|
203
|
+
# total spans every agent session active in this worktree. Without this line an
|
|
204
|
+
# agent that had made two calls itself read "50 tool calls" as its OWN count
|
|
205
|
+
# and reported the reminder as inexplicable. The counter's worktree-wide
|
|
206
|
+
# semantics are intentional (it tracks team checkpoint cadence) and are NOT
|
|
207
|
+
# changed here — only the claim the message makes about whose calls these are.
|
|
208
|
+
echo " Note: this count includes tool calls from all agent sessions active in this worktree, not only your own."
|
|
191
209
|
echo "1. Save a checkpoint now (required before continuing):"
|
|
192
210
|
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
211
|
# `if`, not `[[ ... ]] && echo`. Policy, not a claim about this line: never
|