@memnexus-ai/mx-agent-cli 0.1.260 → 0.1.262

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.
@@ -26,6 +26,8 @@
26
26
  # 0 = allow (not a push, or all validations pass, or a load-flake was absorbed)
27
27
  # 2 = block (a real validation failure, or the serialization lock timed out)
28
28
 
29
+
30
+
29
31
  # Fail-open: any unexpected error → let the push through. NOTE: the two
30
32
  # deliberately fail-CLOSED paths (lock timeout, confirmed real regression) use
31
33
  # explicit `exit 2` inside conditionals, so the ERR trap never downgrades them.
@@ -73,6 +75,22 @@ mx_validate_dir() {
73
75
  MX_VALIDATE_DIR="$(mx_validate_dir)"
74
76
  LOCK_FILE="${MX_VALIDATE_LOCK_FILE:-$MX_VALIDATE_DIR/validate.lock}"
75
77
  LOCK_TIMEOUT="${MX_VALIDATE_LOCK_TIMEOUT:-900}" # seconds to wait in queue
78
+ # Per-run bound on a validate script. WORST-CASE BUDGET for this hook, which the
79
+ # settings.json harness timeout (3600s) must stay above, because a harness kill
80
+ # is fail-OPEN and would skip the gate entirely:
81
+ # lock wait LOCK_TIMEOUT 900s (already fail-closed)
82
+ # + first pass CV_VALIDATE_TIMEOUT_SECS 600s (fail-closed, below)
83
+ # + isolation retry CV_VALIDATE_TIMEOUT_SECS 600s (fail-closed, below)
84
+ # + devlog check CV_DEVLOG_TIMEOUT_SECS 60s (advisory, warn only)
85
+ # + two git diffs CV_GIT_TIMEOUT_SECS x2 30s (fail-open)
86
+ # ────────────────────────────────────────────────
87
+ # 2190s floor, 1410s of headroom under the 3600s harness cap.
88
+ # 2190 is a LOWER BOUND, not an exact total: the shared push-target resolver adds
89
+ # up to MXPT_GIT_TIMEOUT_SECS (10s) per rev-parse and the segment count scales
90
+ # with the pushed command, so a pathological command adds more. It would take
91
+ # ~141 segments to close the 1410s of headroom, which is why the cap is 3600.
92
+ CV_VALIDATE_TIMEOUT_SECS="${MX_VALIDATE_TIMEOUT:-600}"
93
+ CV_DEVLOG_TIMEOUT_SECS="${MX_VALIDATE_DEVLOG_TIMEOUT:-60}"
76
94
  FLAKE_LOG="${MX_VALIDATE_FLAKE_LOG:-$MX_VALIDATE_DIR/flakes.log}"
77
95
  MAX_ISOLATE_FILES="${MX_VALIDATE_MAX_ISOLATE_FILES:-10}" # >this many failed files ⇒ real breakage
78
96
 
@@ -191,9 +209,28 @@ run_validate() {
191
209
  # run and skip lint/build/the full parallel suite — silently turning the gate
192
210
  # into a no-op. The deliberate retry below sets MX_VALIDATE_SERIAL explicitly,
193
211
  # so nothing legitimate breaks.
194
- env -u MX_VALIDATE_SERIAL -u MX_VALIDATE_ISOLATE_FILES bash "$script_path" >"$out" 2>&1 || rc=$?
212
+ timeout "$CV_VALIDATE_TIMEOUT_SECS" \
213
+ env -u MX_VALIDATE_SERIAL -u MX_VALIDATE_ISOLATE_FILES bash "$script_path" >"$out" 2>&1 || rc=$?
195
214
  cat "$out" >&2
196
215
 
216
+ # A validation that could not finish has not validated anything, so a timeout
217
+ # BLOCKS — it does not fall through to the flake-retry path below and it is
218
+ # not absorbed as machine noise. This is deliberately fail-CLOSED, for the
219
+ # same reason the lock-wait timeout is: the outer harness timeout is fail-OPEN
220
+ # (a killed PreToolUse hook allows the tool call), so an unbounded run that
221
+ # outlived the harness would let the push through exactly when the machine is
222
+ # most degraded — the moment the gate matters most (#4846 review ruling 1).
223
+ if [[ $rc -eq 124 ]]; then
224
+ {
225
+ echo "✗ $label TIMED OUT after ${CV_VALIDATE_TIMEOUT_SECS}s — push BLOCKED (fail-closed)."
226
+ echo " The validation did not finish, so nothing about this push has been verified."
227
+ echo " Re-run it directly to see where it hangs: bash $script_path"
228
+ echo " If this package is genuinely slower than the budget, raise it:"
229
+ echo " MX_VALIDATE_TIMEOUT=<seconds> git push ..."
230
+ } >&2
231
+ FAILED=1; rm -f "$out"; return
232
+ fi
233
+
197
234
  if [[ $rc -eq 0 ]]; then
198
235
  echo "✓ $label passed" >&2
199
236
  rm -f "$out"
@@ -253,9 +290,21 @@ run_validate() {
253
290
 
254
291
  local iso_out; iso_out="$(mktemp)"
255
292
  local iso_rc=0
256
- MX_VALIDATE_SERIAL=1 bash "$script_path" >"$iso_out" 2>&1 || iso_rc=$?
293
+ MX_VALIDATE_SERIAL=1 timeout "$CV_VALIDATE_TIMEOUT_SECS" bash "$script_path" >"$iso_out" 2>&1 || iso_rc=$?
257
294
  cat "$iso_out" >&2
258
295
 
296
+ # Same rule on the retry: a timed-out isolation run cannot absorb an under-load
297
+ # failure. The absorb condition below already requires iso_rc -eq 0, so 124
298
+ # blocks either way; this branch exists to say why rather than reporting the
299
+ # retry as a plain failure.
300
+ if [[ $iso_rc -eq 124 ]]; then
301
+ {
302
+ echo "✗ $label: isolation re-run TIMED OUT after ${CV_VALIDATE_TIMEOUT_SECS}s — push BLOCKED (fail-closed)."
303
+ echo " The under-load failures above could not be shown to be machine noise."
304
+ } >&2
305
+ FAILED=1; rm -f "$out" "$iso_out"; return
306
+ fi
307
+
259
308
  local still_failing
260
309
  still_failing="$(parse_failed_test_files "$iso_out")"
261
310
 
@@ -411,6 +460,12 @@ fi
411
460
  # the wrong answer and the right answer coincide, which is why it went unnoticed.
412
461
  #
413
462
  # >>> SHARED push-target resolver v1 (#4803, #4808) — KEEP BYTE-IDENTICAL ─────
463
+ # Every git call in this resolver is bounded. An unbounded rev-parse on a
464
+ # wedged filesystem would burn the hook's whole harness timeout and then be
465
+ # killed — and a killed PreToolUse hook ALLOWS the tool call, so the push
466
+ # would go through unvalidated. Bounded, a hang instead lands on the existing
467
+ # MXPT_UNRESOLVED path, which already blocks and now says why (#4846 review).
468
+ MXPT_GIT_TIMEOUT_SECS="${MXPT_GIT_TIMEOUT_SECS:-10}"
414
469
  # Duplicated verbatim in mx-guard-hook.sh and ci-validate-hook.sh, exactly the
415
470
  # way PUSH_RE already is, and pinned byte-for-byte by push-target-parity.test.sh.
416
471
  # Deliberately NOT a sourced library: each hook must stay a standalone file (that
@@ -570,7 +625,7 @@ if [ "${#MXPT_PUSH_RAWS[@]}" -eq 0 ]; then
570
625
  MXPT_PUSH_REMOTE=(0)
571
626
  fi
572
627
 
573
- MXPT_BASE_TOP=$(git -C "$MXPT_BASE_DIR" rev-parse --show-toplevel 2>/dev/null) || MXPT_BASE_TOP="$MXPT_BASE_DIR"
628
+ MXPT_BASE_TOP=$(timeout "$MXPT_GIT_TIMEOUT_SECS" git -C "$MXPT_BASE_DIR" rev-parse --show-toplevel 2>/dev/null) || MXPT_BASE_TOP="$MXPT_BASE_DIR"
574
629
 
575
630
  # Resolve every push segment. Remote segments are skipped first and never
576
631
  # resolved (SF-1); the first local segment that cannot be resolved stops
@@ -606,8 +661,14 @@ for mxpt_i in "${!MXPT_PUSH_RAWS[@]}"; do
606
661
  esac
607
662
  if [ ! -d "$MXPT_CAND" ]; then
608
663
  mxpt_why="no such directory on this machine"
609
- elif ! mxpt_dir=$(git -C "$MXPT_CAND" rev-parse --show-toplevel 2>/dev/null); then
610
- mxpt_why="it is not inside a git work tree"
664
+ else
665
+ mxpt_rc=0
666
+ mxpt_dir=$(timeout "$MXPT_GIT_TIMEOUT_SECS" git -C "$MXPT_CAND" rev-parse --show-toplevel 2>/dev/null) || mxpt_rc=$?
667
+ if [ "$mxpt_rc" = "124" ]; then
668
+ mxpt_why="git rev-parse did not answer within ${MXPT_GIT_TIMEOUT_SECS}s, so the tree could not be identified"
669
+ elif [ "$mxpt_rc" != "0" ]; then
670
+ mxpt_why="it is not inside a git work tree"
671
+ fi
611
672
  fi ;;
612
673
  esac
613
674
  fi
@@ -708,8 +769,12 @@ fi
708
769
  # ── Detect changed packages ──────────────────────────────────────────────────
709
770
  # Use three-dot diff against origin/main to find files changed on this branch.
710
771
  # Falls back to HEAD (single-commit diff) if origin/main isn't available.
711
- CHANGED_FILES=$(git -C "$REPO_ROOT" diff --name-only "origin/main...HEAD" 2>/dev/null \
712
- || git -C "$REPO_ROOT" diff --name-only HEAD 2>/dev/null \
772
+ # Both diffs are bounded so a contended index lock or a slow history walk cannot
773
+ # hold the Bash tool call open. On timeout the substitution is empty and we take
774
+ # the existing no-changed-files branch below: fail-open, unchanged.
775
+ CV_GIT_TIMEOUT_SECS="${CV_GIT_TIMEOUT_SECS:-15}"
776
+ CHANGED_FILES=$(timeout "$CV_GIT_TIMEOUT_SECS" git -C "$REPO_ROOT" diff --name-only "origin/main...HEAD" 2>/dev/null \
777
+ || timeout "$CV_GIT_TIMEOUT_SECS" git -C "$REPO_ROOT" diff --name-only HEAD 2>/dev/null \
713
778
  || true)
714
779
 
715
780
  if [[ -z "$CHANGED_FILES" ]]; then
@@ -730,7 +795,10 @@ if [[ -f "$DEVLOG_SCRIPT" ]]; then
730
795
  DEVLOG_PATHS="$DEVLOG_PATHS $REPO_ROOT/$df"
731
796
  done <<< "$DEVLOG_FILES"
732
797
  # shellcheck disable=SC2086
733
- if ! bash "$DEVLOG_SCRIPT" $DEVLOG_PATHS 2>&1; then
798
+ # Bounded, and deliberately NOT fail-closed: this check is advisory, so a
799
+ # timeout warns exactly as a content failure does rather than blocking.
800
+ # shellcheck disable=SC2086
801
+ if ! timeout "$CV_DEVLOG_TIMEOUT_SECS" bash "$DEVLOG_SCRIPT" $DEVLOG_PATHS 2>&1; then
734
802
  echo "ci-validate: NOTE — dev-log section check found issues (advisory, not blocking push)." >&2
735
803
  fi
736
804
  fi
@@ -206,14 +206,27 @@ if [ -n "$STDERR_CAP" ]; then
206
206
  else
207
207
  CAP_REDIR="/dev/null"
208
208
  fi
209
- timeout "${MX_COORD_BRIEF_TIMEOUT_SECS:-30}" \
209
+ timeout "${MX_COORD_BRIEF_TIMEOUT_SECS:-60}" \
210
210
  env \
211
211
  MX_COORD_SERVICE_URL="${MX_COORD_SERVICE_URL:-$COORD_URL_DEFAULT}" \
212
212
  MX_COORD_SERVICE_TOKEN="$TOKEN" \
213
213
  MX_COORD_EXPECT_TEAM="$SLUG" \
214
214
  mx-agent coordination brief 2>"$CAP_REDIR" || status=$?
215
215
  if [ "$status" -eq 124 ]; then
216
- echo "coordination brief timed out — skipping" >&2
216
+ # LOUD BY DESIGN (#4846 SF-25). Everything else in this hook is quietly
217
+ # fail-open, and this branch must not be: a silent timeout is indistinguishable
218
+ # from an empty queue, so the agent reads "no escalations" when the truth is
219
+ # "delivery failed". The drain is destructive server-side, so messages may have
220
+ # been marked delivered without ever being shown. Say so, and say what to run.
221
+ {
222
+ echo "coordination brief: DRAIN DID NOT COMPLETE within ${MX_COORD_BRIEF_TIMEOUT_SECS:-60}s."
223
+ echo " QUEUE STATE UNKNOWN — this is NOT the same as an empty queue."
224
+ echo " Escalations addressed to this team may be undelivered, and because the"
225
+ echo " drain consumes messages server-side, some may already be marked"
226
+ echo " delivered without having been shown to you."
227
+ echo " Before relying on 'no escalations', run: mx-agent coordination history"
228
+ echo " Raise the bound with MX_COORD_BRIEF_TIMEOUT_SECS if the queue is deep."
229
+ } >&2
217
230
  elif [ "$status" -ne 0 ]; then
218
231
  # Non-timeout failure. ONLY the CLI-too-old case is announced; every other
219
232
  # failure stays silently fail-open (a coordination hiccup must not be noise).
@@ -96,6 +96,19 @@ if [ "$STATUS" -ne 0 ]; then
96
96
  # the `--min-priority` option → Commander exits non-zero). Fail-open and do NOT
97
97
  # update the throttle, so the next turn retries. We deliberately do NOT retry
98
98
  # without --min-priority: an unfiltered drain would consume P3s the brief owns.
99
+ #
100
+ # A TIMEOUT IS ANNOUNCED (#4846 SF-25). Failing silently here makes a failed
101
+ # drain look exactly like an empty queue. The retry next turn usually repairs
102
+ # it, which is why this is a note rather than a block — but the agent has to be
103
+ # able to tell the two apart in the meantime. Other non-zero statuses stay
104
+ # quiet: an old CLI would otherwise print this on every single turn.
105
+ if [ "$STATUS" -eq 124 ]; then
106
+ {
107
+ echo "coordination drain: DID NOT COMPLETE within ${TIMEOUT_SECS}s — queue state unknown."
108
+ echo " Not an empty queue. Retrying next turn; if it persists, run:"
109
+ echo " mx-agent coordination history"
110
+ } >&2
111
+ fi
99
112
  exit 0
100
113
  fi
101
114
 
@@ -253,7 +253,11 @@ fi
253
253
  # behaviour change. Only runs on the reminder path, never the hot Bash path.
254
254
  CLI_PACKAGE="${MX_CLI_PACKAGE:-}"
255
255
  if [ -z "$CLI_PACKAGE" ]; then
256
- DVR_ROOT="${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || true)}"
256
+ # Bounded (#4846 review): only reached when CLAUDE_PROJECT_DIR is unset, but a
257
+ # rev-parse on a wedged filesystem would otherwise hold this hook open. On
258
+ # timeout DVR_ROOT is empty and the config lookup below is skipped, which is
259
+ # the same path as "no config file" — the reminder still fires.
260
+ DVR_ROOT="${CLAUDE_PROJECT_DIR:-$(timeout "${DVR_GIT_TIMEOUT_SECS:-5}" git rev-parse --show-toplevel 2>/dev/null || true)}"
257
261
  DVR_CFG="${DVR_ROOT}/mx-agent.config.json"
258
262
  if [ -n "$DVR_ROOT" ] && [ -f "$DVR_CFG" ] && command -v jq >/dev/null 2>&1; then
259
263
  CLI_PACKAGE="$(jq -r '.cliPackage // empty' "$DVR_CFG" 2>/dev/null || true)"
@@ -139,7 +139,14 @@ if [[ "$CURRENT" -ge "$THRESHOLD" ]]; then
139
139
  # a `find` on the hot path would tax all 50 for work that matters once. Bounded
140
140
  # by -maxdepth 1 and a fixed name pattern; best-effort, errors ignored (other
141
141
  # users' files are simply not ours to delete).
142
- find /tmp -maxdepth 1 -name '.mx-checkpoint-counter-*.session-*' -mmin +1440 -delete 2>/dev/null || true
142
+ # Bounded (#4846 review). This is the one call in this hook that touches a
143
+ # directory it does not own the size of: /tmp on a loaded machine can hold
144
+ # enough entries for the sweep to run for seconds, and this hook's harness
145
+ # timeout is the thing standing between a slow sweep and a lost reminder.
146
+ # Best-effort either way — on timeout the stale files are simply swept on a
147
+ # later run, which is what `|| true` already meant for any other failure.
148
+ timeout "${MCR_SWEEP_TIMEOUT_SECS:-5}" \
149
+ find /tmp -maxdepth 1 -name '.mx-checkpoint-counter-*.session-*' -mmin +1440 -delete 2>/dev/null || true
143
150
 
144
151
  # ── 3a. Resolve codeContext for the suggested command (#4652) ──────────────
145
152
  # Deliberately INSIDE the threshold branch. This hook runs on every single
@@ -38,6 +38,8 @@
38
38
  # `git -C /treeA push && git -C /treeB push` pushed unlinted. Every push
39
39
  # segment is now resolved and every distinct toplevel linted.
40
40
 
41
+
42
+
41
43
  # Read hook input from stdin
42
44
  INPUT=$(cat)
43
45
  COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
@@ -92,6 +94,12 @@ fi
92
94
  # #4803 test fails against it.
93
95
 
94
96
  # >>> SHARED push-target resolver v1 (#4803, #4808) — KEEP BYTE-IDENTICAL ─────
97
+ # Every git call in this resolver is bounded. An unbounded rev-parse on a
98
+ # wedged filesystem would burn the hook's whole harness timeout and then be
99
+ # killed — and a killed PreToolUse hook ALLOWS the tool call, so the push
100
+ # would go through unvalidated. Bounded, a hang instead lands on the existing
101
+ # MXPT_UNRESOLVED path, which already blocks and now says why (#4846 review).
102
+ MXPT_GIT_TIMEOUT_SECS="${MXPT_GIT_TIMEOUT_SECS:-10}"
95
103
  # Duplicated verbatim in mx-guard-hook.sh and ci-validate-hook.sh, exactly the
96
104
  # way PUSH_RE already is, and pinned byte-for-byte by push-target-parity.test.sh.
97
105
  # Deliberately NOT a sourced library: each hook must stay a standalone file (that
@@ -251,7 +259,7 @@ if [ "${#MXPT_PUSH_RAWS[@]}" -eq 0 ]; then
251
259
  MXPT_PUSH_REMOTE=(0)
252
260
  fi
253
261
 
254
- MXPT_BASE_TOP=$(git -C "$MXPT_BASE_DIR" rev-parse --show-toplevel 2>/dev/null) || MXPT_BASE_TOP="$MXPT_BASE_DIR"
262
+ MXPT_BASE_TOP=$(timeout "$MXPT_GIT_TIMEOUT_SECS" git -C "$MXPT_BASE_DIR" rev-parse --show-toplevel 2>/dev/null) || MXPT_BASE_TOP="$MXPT_BASE_DIR"
255
263
 
256
264
  # Resolve every push segment. Remote segments are skipped first and never
257
265
  # resolved (SF-1); the first local segment that cannot be resolved stops
@@ -287,8 +295,14 @@ for mxpt_i in "${!MXPT_PUSH_RAWS[@]}"; do
287
295
  esac
288
296
  if [ ! -d "$MXPT_CAND" ]; then
289
297
  mxpt_why="no such directory on this machine"
290
- elif ! mxpt_dir=$(git -C "$MXPT_CAND" rev-parse --show-toplevel 2>/dev/null); then
291
- mxpt_why="it is not inside a git work tree"
298
+ else
299
+ mxpt_rc=0
300
+ mxpt_dir=$(timeout "$MXPT_GIT_TIMEOUT_SECS" git -C "$MXPT_CAND" rev-parse --show-toplevel 2>/dev/null) || mxpt_rc=$?
301
+ if [ "$mxpt_rc" = "124" ]; then
302
+ mxpt_why="git rev-parse did not answer within ${MXPT_GIT_TIMEOUT_SECS}s, so the tree could not be identified"
303
+ elif [ "$mxpt_rc" != "0" ]; then
304
+ mxpt_why="it is not inside a git work tree"
305
+ fi
292
306
  fi ;;
293
307
  esac
294
308
  fi
@@ -381,7 +395,12 @@ if ! cd "$TARGET_DIR"; then
381
395
  fi
382
396
  # <<< #4803 END push-target resolution
383
397
 
384
- mx-guard check >&2
398
+ # Bounded so a wedged lint cannot hold the Bash tool call open. The bound is
399
+ # internal, not the harness `timeout` in settings.json, because a harness
400
+ # timeout kills the hook and ALLOWS the push — the one outcome a guard must not
401
+ # have. At 124 we stay on the existing non-zero path and still block.
402
+ MX_GUARD_CHECK_TIMEOUT_SECS="${MX_GUARD_CHECK_TIMEOUT_SECS:-120}"
403
+ timeout "$MX_GUARD_CHECK_TIMEOUT_SECS" mx-guard check >&2
385
404
  GUARD_EXIT=$?
386
405
  if [ "${MXG_PRIOR_FAIL:-0}" -ne 0 ]; then
387
406
  GUARD_EXIT=1
@@ -390,6 +409,13 @@ fi
390
409
  if [ $GUARD_EXIT -ne 0 ]; then
391
410
  {
392
411
  echo ""
412
+ if [ $GUARD_EXIT -eq 124 ]; then
413
+ echo "mx-guard: push blocked — mx-guard check did not finish within"
414
+ echo "${MX_GUARD_CHECK_TIMEOUT_SECS}s, so this push is unvalidated. Re-run"
415
+ echo "'mx-guard check' directly to see where it hangs; raise the bound with"
416
+ echo "MX_GUARD_CHECK_TIMEOUT_SECS if the tree is genuinely that slow to lint."
417
+ echo ""
418
+ fi
393
419
  echo "mx-guard: push blocked. Fix the errors above before pushing."
394
420
  echo " To check specific rules: mx-guard check --rule <category>"
395
421
  echo " To see all rules: mx-guard rules"
@@ -220,6 +220,11 @@ HEAD_LEN=$(printf '%s' "$HEAD_JSON" | sed -n '/{/,$p' | jq -r '(.content // "")
220
220
  # log the event so we can see how often the head is unreadable.
221
221
  if [[ -z "$HEAD_LEN" || ! "$HEAD_LEN" =~ ^[0-9]+$ || "$HEAD_LEN" -eq 0 ]]; then
222
222
  emit_telemetry "$NAME" "fetch-fail" "$NEW_LEN" "no" "fetch-fail"
223
+ # Announced as well as recorded (#4846 SF-25). The telemetry line is for us
224
+ # later; the agent needs to know NOW that the shrink check did not run, because
225
+ # a fetch failure is indistinguishable from "no existing head" and the write
226
+ # proceeds unguarded either way.
227
+ echo "named-memory-write-guard: could not read the current head of '${NAME_SAFE}' — shrink check SKIPPED, write allowed unguarded." >&2
223
228
  exit 0
224
229
  fi
225
230
 
@@ -3,6 +3,8 @@
3
3
  # Logs sync failures to the local startup JSONL so they are visible at next session start.
4
4
  # FAIL-OPEN: always exits 0 regardless of sync outcome.
5
5
 
6
+
7
+
6
8
  set -euo pipefail
7
9
 
8
10
  WORKTREE_PATH="${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null || echo '')}"
@@ -16,7 +18,11 @@ LOG_FILE="$LOG_DIR/${WORKTREE_NAME}.jsonl"
16
18
  # Run the sync, capture exit code
17
19
  SYNC_OUTPUT=""
18
20
  SYNC_EXIT=0
19
- SYNC_OUTPUT=$(mx-agent sync --api-url "$API_URL" --quiet 2>&1) || SYNC_EXIT=$?
21
+ # Bounded: this is a network call to the observability API. Without a bound a
22
+ # hung or black-holed connection holds the Stop hook open. On timeout `timeout`
23
+ # exits 124, which lands on the existing failure path below — logged to the
24
+ # startup JSONL, still exit 0. Fail-open contract unchanged.
25
+ SYNC_OUTPUT=$(timeout "${OBS_SYNC_TIMEOUT_SECS:-25}" mx-agent sync --api-url "$API_URL" --quiet 2>&1) || SYNC_EXIT=$?
20
26
 
21
27
  if [ "$SYNC_EXIT" -ne 0 ]; then
22
28
  # Log failure to the local startup JSONL (v2.58 infrastructure)
@@ -32,12 +32,22 @@ command -v mx >/dev/null 2>&1 || exit 0
32
32
 
33
33
  # ── 2. Retrieve checkpoint memory ───────────────────────────────────────
34
34
 
35
- CHECKPOINT=$(mx memories get --name "auto-checkpoint-${WORKTREE_NAME}" --format json --detail minimal 2>/dev/null)
36
-
37
- if [[ -z "$CHECKPOINT" || "$CHECKPOINT" == *"not found"* || "$CHECKPOINT" == *"404"* ]]; then
35
+ # Bounded: network read. On timeout the result is empty and the next test exits
36
+ # 0 without injecting a checkpoint — the same path as "no checkpoint found".
37
+ CHECKPOINT=$(timeout "${RELOAD_CHECKPOINT_TIMEOUT_SECS:-15}" mx memories get --name "auto-checkpoint-${WORKTREE_NAME}" --format json --detail minimal 2>/dev/null || true)
38
+
39
+ if [[ -z "$CHECKPOINT" ]]; then
40
+ # Announced (#4846 SF-25): an empty result here is either "no checkpoint was
41
+ # saved" or "the fetch failed", and the agent resumes from compaction either
42
+ # way. Only the second case means context it should have had is missing.
43
+ echo "reload-checkpoint: could not read auto-checkpoint-${WORKTREE_NAME} (fetch failed or timed out) — resuming without it." >&2
38
44
  exit 0
39
45
  fi
40
46
 
47
+ if [[ "$CHECKPOINT" == *"not found"* || "$CHECKPOINT" == *"404"* ]]; then
48
+ exit 0 # Genuinely no checkpoint saved — nothing to announce.
49
+ fi
50
+
41
51
  # Extract just the content field from the JSON response
42
52
  CONTENT=$(echo "$CHECKPOINT" | jq -r '.content // empty' 2>/dev/null)
43
53
 
@@ -21,6 +21,8 @@
21
21
  # 0 = allow
22
22
  # 2 = block (path filters explain why CI didn't trigger — dispatch not needed)
23
23
 
24
+
25
+
24
26
  # Fail-open: any unexpected error → allow the command through
25
27
  trap 'exit 0' ERR
26
28
 
@@ -68,7 +70,11 @@ WORKFLOW_FILE="$REPO_ROOT/.github/workflows/$WORKFLOW_NAME"
68
70
  if [[ ! -f "$WORKFLOW_FILE" ]]; then
69
71
  # Selector may be a display name or numeric ID — try resolving via gh CLI
70
72
  if command -v gh &>/dev/null; then
71
- RESOLVED_PATH=$(gh workflow view "$WORKFLOW_NAME" --json path -q .path 2>/dev/null || true)
73
+ # Network call to the GitHub API — bounded so an unreachable or slow API
74
+ # cannot hold the Bash tool call open. On timeout `timeout` exits 124, the
75
+ # substitution is empty, and we take the existing unresolvable-selector
76
+ # branch below: fail-open, unchanged.
77
+ RESOLVED_PATH=$(timeout "${WDG_GH_TIMEOUT_SECS:-10}" gh workflow view "$WORKFLOW_NAME" --json path -q .path 2>/dev/null || true)
72
78
  if [[ -n "$RESOLVED_PATH" && -f "$REPO_ROOT/$RESOLVED_PATH" ]]; then
73
79
  WORKFLOW_FILE="$REPO_ROOT/$RESOLVED_PATH"
74
80
  else
@@ -130,7 +136,10 @@ if [[ -z "$PATH_FILTERS" ]]; then
130
136
  fi
131
137
 
132
138
  # ── Get changed files on this branch ─────────────────────────────────────────
133
- CHANGED_FILES=$(git -C "$REPO_ROOT" diff --name-only "origin/main...HEAD" 2>/dev/null || true)
139
+ # Bounded: a three-dot diff walks history and can be slow on a large repo or a
140
+ # contended index lock. On timeout the result is empty and we take the existing
141
+ # can't-determine-changed-files branch: fail-open, unchanged.
142
+ CHANGED_FILES=$(timeout "${WDG_GIT_TIMEOUT_SECS:-15}" git -C "$REPO_ROOT" diff --name-only "origin/main...HEAD" 2>/dev/null || true)
134
143
 
135
144
  if [[ -z "$CHANGED_FILES" ]]; then
136
145
  exit 0 # Can't determine changed files — fail-open
@@ -7,7 +7,8 @@
7
7
  "hooks": [
8
8
  {
9
9
  "type": "command",
10
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/memory-checkpoint-reminder.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
10
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/memory-checkpoint-reminder.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
11
+ "timeout": 60
11
12
  }
12
13
  ]
13
14
  },
@@ -16,31 +17,38 @@
16
17
  "hooks": [
17
18
  {
18
19
  "type": "command",
19
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/worktree-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
20
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/worktree-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
21
+ "timeout": 60
20
22
  },
21
23
  {
22
24
  "type": "command",
23
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/mx-guard-hook.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
25
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/mx-guard-hook.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
26
+ "timeout": 300
24
27
  },
25
28
  {
26
29
  "type": "command",
27
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/ci-validate-hook.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
30
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/ci-validate-hook.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
31
+ "timeout": 3600
28
32
  },
29
33
  {
30
34
  "type": "command",
31
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/workflow-dispatch-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
35
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/workflow-dispatch-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
36
+ "timeout": 60
32
37
  },
33
38
  {
34
39
  "type": "command",
35
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/git-mutation-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
40
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/git-mutation-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
41
+ "timeout": 60
36
42
  },
37
43
  {
38
44
  "type": "command",
39
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/named-memory-write-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
45
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/named-memory-write-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
46
+ "timeout": 60
40
47
  },
41
48
  {
42
49
  "type": "command",
43
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/gh-pr-edit-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
50
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/gh-pr-edit-guard.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
51
+ "timeout": 60
44
52
  }
45
53
  ]
46
54
  }
@@ -51,7 +59,8 @@
51
59
  "hooks": [
52
60
  {
53
61
  "type": "command",
54
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/deploy-verification-reminder.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
62
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/deploy-verification-reminder.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
63
+ "timeout": 60
55
64
  }
56
65
  ]
57
66
  }
@@ -62,11 +71,13 @@
62
71
  "hooks": [
63
72
  {
64
73
  "type": "command",
65
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-checkpoint.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
74
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-checkpoint.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
75
+ "timeout": 30
66
76
  },
67
77
  {
68
78
  "type": "command",
69
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-capture-precompact.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
79
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-capture-precompact.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
80
+ "timeout": 30
70
81
  }
71
82
  ]
72
83
  }
@@ -77,7 +88,8 @@
77
88
  "hooks": [
78
89
  {
79
90
  "type": "command",
80
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/reload-checkpoint.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
91
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/reload-checkpoint.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
92
+ "timeout": 20
81
93
  }
82
94
  ]
83
95
  },
@@ -86,11 +98,13 @@
86
98
  "hooks": [
87
99
  {
88
100
  "type": "command",
89
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/set-terminal-appearance.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
101
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/set-terminal-appearance.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
102
+ "timeout": 5
90
103
  },
91
104
  {
92
105
  "type": "command",
93
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/coordination-brief.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
106
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/coordination-brief.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
107
+ "timeout": 180
94
108
  }
95
109
  ]
96
110
  }
@@ -100,27 +114,33 @@
100
114
  "hooks": [
101
115
  {
102
116
  "type": "command",
103
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/observability-sync.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
117
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/observability-sync.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
118
+ "timeout": 30
104
119
  },
105
120
  {
106
121
  "type": "command",
107
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-capture-session-end.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
122
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/auto-capture-session-end.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
123
+ "timeout": 30
108
124
  },
109
125
  {
110
126
  "type": "command",
111
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/delegation-audit.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
127
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/delegation-audit.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
128
+ "timeout": 15
112
129
  },
113
130
  {
114
131
  "type": "command",
115
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/iteration-log-check.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
132
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/iteration-log-check.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
133
+ "timeout": 20
116
134
  },
117
135
  {
118
136
  "type": "command",
119
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/coordination-midsession-drain.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
137
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/coordination-midsession-drain.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
138
+ "timeout": 30
120
139
  },
121
140
  {
122
141
  "type": "command",
123
- "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/session-idle-gate.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'"
142
+ "command": "bash -c 'H=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}/.claude/hooks/session-idle-gate.sh\"; [ -f \"$H\" ] && exec bash \"$H\" || exit 0'",
143
+ "timeout": 60
124
144
  }
125
145
  ]
126
146
  }
@@ -270,15 +270,49 @@ export declare const CODEX_AGENT_GATE_COMMAND = "mx-agent internal-codex-agent-g
270
270
  * allowlist, and "no team will ever have an apostrophe in its checkout path" is not a
271
271
  * property this code can assert.
272
272
  *
273
- * The command now reads `CLAUDE_PROJECT_DIR` from the environment, which
274
- * `CodexAdapter.launch` sets to the worktree path. The only interpolated value is the
275
- * script filename, constrained by SAFE_SCRIPT_NAME.
276
- *
277
- * Fail-OPEN when the script is absent (`|| exit 0`) mirrors the wrapper
278
- * `.claude/settings.json` uses for the same script: the Codex port reproduces the
279
- * Claude semantics rather than inventing a different failure model on a security
280
- * path. `deployCodexConfig` warns by name for each guard script that is missing, so
281
- * the silent case is at least announced (security review S9).
273
+ * The only interpolated value is the script filename, constrained by SAFE_SCRIPT_NAME.
274
+ *
275
+ * ─── RESOLUTION AND FAIL DIRECTION (#4227 G-7 defect, 2026-08-01) ───
276
+ * The previous command was `H="$CLAUDE_PROJECT_DIR/.claude/hooks/<s>"; [ -f "$H" ] &&
277
+ * exec bash "$H" || exit 0`. Two defects, proven live in paired Codex sessions:
278
+ *
279
+ * a. CLAUDE_PROJECT_DIR is not part of Codex. Codex never sets it (0 occurrences of
280
+ * the string in the 0.145.0 binary; printed as UNSET from inside a session), so
281
+ * unless the session was launched by `CodexAdapter.launch` — which does set it —
282
+ * `H` resolved to `/.claude/hooks/<s>`.
283
+ * b. The `|| exit 0` then took the ALLOW branch. Session 2 (no CLAUDE_PROJECT_DIR):
284
+ * `git reset --hard HEAD` exited 0 and succeeded. Session 3 (same prompt,
285
+ * CLAUDE_PROJECT_DIR exported): the same command was refused by the guard, and the
286
+ * permitted control still succeeded. The guards were trusted, enabled, discovered
287
+ * — and inert.
288
+ *
289
+ * (b) is the root cause. (a) merely triggered it: any reason the script cannot be
290
+ * found — unset variable, wrong project root, a not-yet-deployed guard — produced a
291
+ * silent allow on a containment path. This is the same class of bug as the guards
292
+ * fixed in #4802/#4803/#4804: a control that resolves its subject from ambient
293
+ * context, and treats a failed resolution as consent.
294
+ *
295
+ * The fix, in order:
296
+ * 1. Resolve from CLAUDE_PROJECT_DIR, then CLAUDE_WORKTREE_PATH, then by walking up
297
+ * from the working directory looking for `.claude/hooks/<script>`. The walk is
298
+ * what makes the hook work in a Codex session that no mx-agent code launched.
299
+ * 2. If nothing resolves, print why and `exit 2` — Codex's block contract (finding 3
300
+ * in the file header). A guard that cannot locate itself refuses the command.
301
+ *
302
+ * NO ABSOLUTE PATH IS BAKED IN, deliberately. The generated `.codex/hooks.json` is
303
+ * byte-identical in every worktree (verified: worktree and /workspace copies share
304
+ * sha256 7a65ab4f…dbf468), and Codex hook trust is keyed on that hash with a human
305
+ * granting it once (PO ruling on gate G-7, 2026-07-31; guards shared fleet-wide,
306
+ * 2026-08-01). Interpolating one worktree path would give every worktree a distinct
307
+ * file, a distinct hash, and a separate human trust grant — and would reintroduce the
308
+ * S4 quote-injection hole the path-free form closed.
309
+ *
310
+ * DIVERGENCE FROM `.claude/settings.json` IS INTENTIONAL. That wrapper still ends in
311
+ * `|| exit 0`; this one does not. Claude sets CLAUDE_PROJECT_DIR itself, so there the
312
+ * missing-script branch means the script is genuinely absent, while under Codex it was
313
+ * the normal case. Bringing the Claude wrapper to fail-closed is a separate change with
314
+ * its own blast radius (it also wraps advisory, non-guard hooks) and is out of scope
315
+ * here; this change alters Codex behaviour only.
282
316
  */
283
317
  export declare function codexHookCommand(script: string): string;
284
318
  /**