@calltelemetry/fmix 2.7.3 → 2.7.4

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.
Files changed (2) hide show
  1. package/bin/fmix +1070 -195
  2. package/package.json +3 -3
package/bin/fmix CHANGED
@@ -23,7 +23,7 @@ set -uo pipefail
23
23
  # ---------------------------------------------------------------------------
24
24
  # Constants & config
25
25
  # ---------------------------------------------------------------------------
26
- FMIX_VERSION="2.7.3"
26
+ FMIX_VERSION="2.7.4"
27
27
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
28
28
  FMIX_OS="$(uname -s)"
29
29
  FMIX_ARCH="$(uname -m)"
@@ -100,15 +100,15 @@ FMIX_MIGRATE_FROM_ZERO_TIMEOUT="${FMIX_MIGRATE_FROM_ZERO_TIMEOUT:-1800}"
100
100
  FMIX_STRUCTURE_LOAD_TIMEOUT="${FMIX_STRUCTURE_LOAD_TIMEOUT:-120}"
101
101
  FMIX_STRUCTURE_LOAD_HEARTBEAT="${FMIX_STRUCTURE_LOAD_HEARTBEAT:-30}"
102
102
 
103
- # Kernel locks are held by inherited file descriptors for the full protected
104
- # operation. Indexed arrays keep this compatible with the Bash 3.2 shipped by
105
- # older macOS hosts; associative arrays and `flock(1)` are intentionally not
106
- # required.
107
- FMIX_LOCK_FD_MIN=210
108
- FMIX_LOCK_FD_MAX=239
103
+ # Kernel locks are held by dedicated Python keeper processes. The FMix shell
104
+ # never owns the lock descriptor, so compiler children and detached daemons
105
+ # cannot inherit lock ownership. Indexed arrays keep this compatible with the
106
+ # Bash 3.2 shipped by older macOS hosts; associative arrays and `flock(1)` are
107
+ # intentionally not required.
109
108
  FMIX_HELD_LOCK_NAMES=()
110
- FMIX_HELD_LOCK_FDS=()
109
+ FMIX_HELD_LOCK_KEEPER_PIDS=()
111
110
  FMIX_HELD_LOCK_TOKENS=()
111
+ FMIX_LAST_LOCK_KEEPER_PID=""
112
112
 
113
113
  # Set by fmix_activate_session before any mutable artifact, stamp, or scratch
114
114
  # database path is resolved.
@@ -4366,34 +4366,52 @@ fmix_prepare_dependency_artifacts() {
4366
4366
  # ---------------------------------------------------------------------------
4367
4367
 
4368
4368
  fmix_hash_source_paths() {
4369
- local root="$1"
4369
+ local root="$1" tmp_tracked="" tmp_diff="" tmp_staged=""
4370
+ local unstaged_hash="" staged_hash="" rc
4370
4371
  shift
4371
4372
  local path
4372
4373
 
4373
- if git -C "$root" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
4374
- local tmp_tracked tmp_diff tmp_staged
4375
- tmp_tracked="$(mktemp)"
4376
- tmp_diff="$(mktemp)"
4377
- tmp_staged="$(mktemp)"
4374
+ if [[ -d "$root/.git" || -f "$root/.git" ]]; then
4375
+ git -C "$root" rev-parse --is-inside-work-tree >/dev/null 2>&1 \
4376
+ || return 1
4377
+ tmp_tracked="$(mktemp)" || return 1
4378
+ tmp_diff="$(mktemp)" || { rm -f "$tmp_tracked"; return 1; }
4379
+ tmp_staged="$(mktemp)" \
4380
+ || { rm -f "$tmp_tracked" "$tmp_diff"; return 1; }
4378
4381
 
4379
- git -C "$root" ls-files -s -- "$@" \
4382
+ if ! git -C "$root" ls-files -s -- "$@" \
4380
4383
  | awk '$1 != "160000" { print $0 }' \
4381
- | LC_ALL=C sort >"$tmp_tracked"
4382
-
4383
- git -C "$root" diff --binary -- "$@" >"$tmp_diff" 2>/dev/null
4384
- git -C "$root" diff --cached --binary -- "$@" >"$tmp_staged" 2>/dev/null
4385
-
4384
+ | LC_ALL=C sort >"$tmp_tracked"; then
4385
+ rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"
4386
+ return 1
4387
+ fi
4388
+ if ! git -C "$root" diff --binary -- "$@" >"$tmp_diff" 2>/dev/null; then
4389
+ rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"
4390
+ return 1
4391
+ fi
4392
+ if ! git -C "$root" diff --cached --binary -- "$@" \
4393
+ >"$tmp_staged" 2>/dev/null; then
4394
+ rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"
4395
+ return 1
4396
+ fi
4397
+ if [[ -s "$tmp_diff" ]]; then
4398
+ unstaged_hash="$(fmix_hash_files "$tmp_diff" | awk '{print $1}')" \
4399
+ || { rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"; return 1; }
4400
+ fi
4401
+ if [[ -s "$tmp_staged" ]]; then
4402
+ staged_hash="$(fmix_hash_files "$tmp_staged" | awk '{print $1}')" \
4403
+ || { rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"; return 1; }
4404
+ fi
4386
4405
  {
4387
4406
  cat "$tmp_tracked"
4388
- if [[ -s "$tmp_diff" ]]; then
4389
- printf 'unstaged-diff:%s\n' "$(fmix_hash_files "$tmp_diff" | awk '{print $1}')"
4390
- fi
4391
- if [[ -s "$tmp_staged" ]]; then
4392
- printf 'staged-diff:%s\n' "$(fmix_hash_files "$tmp_staged" | awk '{print $1}')"
4393
- fi
4407
+ [[ -z "$unstaged_hash" ]] \
4408
+ || printf 'unstaged-diff:%s\n' "$unstaged_hash"
4409
+ [[ -z "$staged_hash" ]] \
4410
+ || printf 'staged-diff:%s\n' "$staged_hash"
4394
4411
  }
4395
-
4412
+ rc=$?
4396
4413
  rm -f "$tmp_tracked" "$tmp_diff" "$tmp_staged"
4414
+ return "$rc"
4397
4415
  else
4398
4416
  for path in "$@"; do
4399
4417
  if [[ -f "$root/$path" || -L "$root/$path" ]]; then
@@ -4642,28 +4660,6 @@ fmix_lock_find_index() {
4642
4660
  return 1
4643
4661
  }
4644
4662
 
4645
- fmix_lock_allocate_fd() {
4646
- local fd
4647
- for (( fd = FMIX_LOCK_FD_MIN; fd <= FMIX_LOCK_FD_MAX; fd++ )); do
4648
- if ! eval "true >&${fd}" 2>/dev/null; then
4649
- printf '%s\n' "$fd"
4650
- return 0
4651
- fi
4652
- done
4653
- fmix_error "No FMix lock file descriptor is available (${FMIX_LOCK_FD_MIN}-${FMIX_LOCK_FD_MAX})."
4654
- return 1
4655
- }
4656
-
4657
- fmix_lock_close_fd() {
4658
- local fd="$1"
4659
- eval "exec ${fd}>&-"
4660
- }
4661
-
4662
- fmix_lock_open_fd() {
4663
- local fd="$1" path="$2"
4664
- eval "exec ${fd}>>\"\$path\""
4665
- }
4666
-
4667
4663
  fmix_lock_quarantine_metadata() {
4668
4664
  local lock_name="$1" lock_dir="$2" reason="$3" destination
4669
4665
  [[ "$lock_name" =~ ^[A-Za-z0-9._-]+$ ]] || return 1
@@ -4677,26 +4673,77 @@ fmix_lock_quarantine_metadata() {
4677
4673
  return 1
4678
4674
  }
4679
4675
 
4680
- fmix_lock_kernel_try() {
4681
- local fd="$1" timeout="$2" diagnostic_name="${3:-${lock_name:-lock}}" lock_name="${4:-${diagnostic_name:-lock}}" py_bin
4676
+ fmix_lock_keeper_start() {
4677
+ local path="$1" mode="$2" timeout="$3"
4678
+ local diagnostic_name="${4:-lock}"
4679
+ local lock_name="${5:-${diagnostic_name:-lock}}"
4680
+ local py_bin owner_pid owner_file state keeper_pid status
4681
+ FMIX_LAST_LOCK_KEEPER_PID=""
4682
4682
  py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
4683
- "$py_bin" - "$fd" "$timeout" "$diagnostic_name" "$lock_name" "$FMIX_HOME" <<'PY'
4683
+ # Bash 3.2 does not expose BASHPID, and $$ intentionally remains the
4684
+ # top-level shell PID inside a subshell. Ask a short-lived child for its
4685
+ # real parent so the keeper monitors the process that actually owns it.
4686
+ owner_pid="${BASHPID:-}"
4687
+ if [[ ! "$owner_pid" =~ ^[0-9]+$ ]]; then
4688
+ owner_file="$(mktemp)" || return 1
4689
+ if ! "$py_bin" -c 'import os; print(os.getppid())' >"$owner_file" \
4690
+ || ! IFS= read -r owner_pid <"$owner_file"; then
4691
+ rm -f "$owner_file"
4692
+ return 1
4693
+ fi
4694
+ rm -f "$owner_file"
4695
+ [[ "$owner_pid" =~ ^[0-9]+$ ]] || return 1
4696
+ fi
4697
+ mkdir -p "$FMIX_HOME/lock-keeper-state" || return 1
4698
+ state="$FMIX_HOME/lock-keeper-state/${lock_name}.${owner_pid}.${RANDOM}.state"
4699
+ (
4700
+ exec "$py_bin" - "$path" "$mode" "$timeout" "$diagnostic_name" \
4701
+ "$lock_name" "$FMIX_HOME" "$owner_pid" "$state" <<'PY'
4684
4702
  import fcntl
4685
4703
  import json
4686
4704
  import os
4705
+ import signal
4687
4706
  import sys
4688
4707
  import time
4689
4708
  from pathlib import Path
4690
4709
 
4691
- fd = int(sys.argv[1])
4692
- timeout = int(sys.argv[2])
4693
- diag_name = sys.argv[3] if len(sys.argv) > 3 and sys.argv[3] else "lock"
4694
- lock_name = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else diag_name
4695
- fmix_home = sys.argv[5] if len(sys.argv) > 5 and sys.argv[5] else os.environ.get("FMIX_HOME", "")
4710
+ path = sys.argv[1]
4711
+ mode = sys.argv[2]
4712
+ timeout = int(sys.argv[3])
4713
+ diag_name = sys.argv[4] if sys.argv[4] else "lock"
4714
+ lock_name = sys.argv[5] if sys.argv[5] else diag_name
4715
+ fmix_home = sys.argv[6]
4716
+ owner_pid = int(sys.argv[7])
4717
+ state_path = Path(sys.argv[8])
4696
4718
 
4697
4719
  start_time = time.monotonic()
4698
4720
  deadline = start_time + timeout
4699
4721
  last_heartbeat = start_time
4722
+ stop_requested = False
4723
+
4724
+
4725
+ def request_stop(_signum, _frame):
4726
+ global stop_requested
4727
+ stop_requested = True
4728
+
4729
+
4730
+ def publish_state(value):
4731
+ temporary = state_path.with_name(
4732
+ state_path.name + f".tmp-{os.getpid()}"
4733
+ )
4734
+ temporary.write_text(value + "\n", encoding="utf-8")
4735
+ os.replace(temporary, state_path)
4736
+
4737
+
4738
+ def owner_alive():
4739
+ if os.getppid() != owner_pid:
4740
+ return False
4741
+ try:
4742
+ os.kill(owner_pid, 0)
4743
+ except OSError:
4744
+ return False
4745
+ return True
4746
+
4700
4747
 
4701
4748
  def get_holder_info(lock_name, fmix_home):
4702
4749
  if not lock_name or not fmix_home:
@@ -4718,28 +4765,80 @@ def get_holder_info(lock_name, fmix_home):
4718
4765
  pass
4719
4766
  return "unknown", diag_name
4720
4767
 
4721
- while True:
4722
- try:
4723
- fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
4724
- raise SystemExit(0)
4725
- except BlockingIOError:
4726
- now = time.monotonic()
4727
- if now >= deadline:
4728
- raise SystemExit(1)
4729
- if now - last_heartbeat >= 15.0:
4730
- elapsed = int(now - start_time)
4731
- holder_pid, purpose = get_holder_info(lock_name, fmix_home)
4732
- sys.stderr.write(f"[fmix] Waiting for lock '{lock_name}' ({elapsed}s elapsed, timeout {timeout}s, holder PID: {holder_pid}, purpose: \"{purpose}\") ...\n")
4733
- sys.stderr.flush()
4734
- last_heartbeat = now
4768
+ signal.signal(signal.SIGTERM, request_stop)
4769
+ signal.signal(signal.SIGINT, request_stop)
4770
+ fd = os.open(path, os.O_RDWR | os.O_CREAT, 0o600)
4771
+ os.set_inheritable(fd, False)
4772
+ operation = fcntl.LOCK_SH if mode == "shared" else fcntl.LOCK_EX
4773
+ try:
4774
+ while not stop_requested and owner_alive():
4775
+ try:
4776
+ fcntl.flock(fd, operation | fcntl.LOCK_NB)
4777
+ break
4778
+ except BlockingIOError:
4779
+ now = time.monotonic()
4780
+ if now >= deadline:
4781
+ publish_state("timeout")
4782
+ raise SystemExit(1)
4783
+ if now - last_heartbeat >= 15.0:
4784
+ elapsed = int(now - start_time)
4785
+ holder_pid, purpose = get_holder_info(lock_name, fmix_home)
4786
+ sys.stderr.write(
4787
+ f"[fmix] Waiting for lock '{lock_name}' ({elapsed}s elapsed, "
4788
+ f"timeout {timeout}s, holder PID: {holder_pid}, "
4789
+ f"purpose: \"{purpose}\") ...\n"
4790
+ )
4791
+ sys.stderr.flush()
4792
+ last_heartbeat = now
4793
+ time.sleep(0.05)
4794
+ else:
4795
+ publish_state("stopped")
4796
+ raise SystemExit(1)
4797
+
4798
+ publish_state("ready")
4799
+ while not stop_requested and owner_alive():
4735
4800
  time.sleep(0.05)
4801
+ finally:
4802
+ os.close(fd)
4736
4803
  PY
4804
+ ) >/dev/null &
4805
+ keeper_pid=$!
4806
+ while true; do
4807
+ if [[ -s "$state" ]]; then
4808
+ IFS= read -r status <"$state"
4809
+ rm -f "$state" 2>/dev/null || true
4810
+ case "$status" in
4811
+ ready)
4812
+ if kill -0 "$keeper_pid" 2>/dev/null; then
4813
+ FMIX_LAST_LOCK_KEEPER_PID="$keeper_pid"
4814
+ return 0
4815
+ fi
4816
+ ;;
4817
+ esac
4818
+ wait "$keeper_pid" 2>/dev/null || true
4819
+ return 1
4820
+ fi
4821
+ if ! kill -0 "$keeper_pid" 2>/dev/null; then
4822
+ wait "$keeper_pid" 2>/dev/null || true
4823
+ rm -f "$state" 2>/dev/null || true
4824
+ return 1
4825
+ fi
4826
+ sleep 0.05
4827
+ done
4828
+ }
4829
+
4830
+ fmix_lock_keeper_stop() {
4831
+ local keeper_pid="${1:-}"
4832
+ [[ "$keeper_pid" =~ ^[0-9]+$ ]] || return 1
4833
+ kill -TERM "$keeper_pid" 2>/dev/null || true
4834
+ wait "$keeper_pid" 2>/dev/null || true
4835
+ return 0
4737
4836
  }
4738
4837
 
4739
4838
  fmix_lock_acquire() {
4740
4839
  local lock_name="${1:-}" timeout="${2:-$FMIX_LOCK_TIMEOUT}"
4741
4840
  local diagnostic_name="${3:-${1:-}}" lock_dir kernel_file deadline
4742
- local fd token owner_start holder_pid holder_start recorded_start
4841
+ local keeper_pid token owner_start holder_pid holder_start recorded_start
4743
4842
  local lock_mtime lock_age grace printed_wait=0 index py_bin
4744
4843
 
4745
4844
  [[ "$lock_name" =~ ^[A-Za-z0-9._-]+$ ]] || {
@@ -4747,7 +4846,7 @@ fmix_lock_acquire() {
4747
4846
  return 1
4748
4847
  }
4749
4848
  if index="$(fmix_lock_find_index "$lock_name")"; then
4750
- fmix_error "Nested acquisition of FMix lock '$diagnostic_name' is not supported (fd=${FMIX_HELD_LOCK_FDS[$index]})."
4849
+ fmix_error "Nested acquisition of FMix lock '$diagnostic_name' is not supported (keeper pid=${FMIX_HELD_LOCK_KEEPER_PIDS[$index]})."
4751
4850
  return 1
4752
4851
  fi
4753
4852
  py_bin="$(fmix_python_bin)"
@@ -4764,16 +4863,15 @@ fmix_lock_acquire() {
4764
4863
  mkdir -p "$FMIX_HOME/locks" "$FMIX_HOME/lock-kernel" "$FMIX_HOME/lock-metadata" || return 1
4765
4864
 
4766
4865
  while true; do
4767
- fd="$(fmix_lock_allocate_fd)" || return 1
4768
- fmix_lock_open_fd "$fd" "$kernel_file" || return 1
4769
4866
  local remaining=$(( deadline - $(fmix_epoch) ))
4770
4867
  (( remaining >= 0 )) || remaining=0
4771
- if ! fmix_lock_kernel_try "$fd" "$remaining" "$diagnostic_name" "$lock_name"; then
4772
- fmix_lock_close_fd "$fd"
4868
+ if ! fmix_lock_keeper_start "$kernel_file" exclusive "$remaining" \
4869
+ "$diagnostic_name" "$lock_name"; then
4773
4870
  fmix_error "Timed out waiting for kernel lock '$diagnostic_name' after ${timeout}s."
4774
4871
  fmix_error "Inspect with: fmix status"
4775
4872
  return 1
4776
4873
  fi
4874
+ keeper_pid="$FMIX_LAST_LOCK_KEEPER_PID"
4777
4875
 
4778
4876
  # Keep a directory-shaped record for older FMix binaries. mkdir remains
4779
4877
  # the mixed-version arbitration point, while the persistent inode above
@@ -4790,11 +4888,11 @@ fmix_lock_acquire() {
4790
4888
  if (( lock_age >= grace )); then
4791
4889
  fmix_lock_quarantine_metadata "$lock_name" "$lock_dir" \
4792
4890
  "owner publication never completed" || {
4793
- fmix_lock_close_fd "$fd"
4891
+ fmix_lock_keeper_stop "$keeper_pid"
4794
4892
  return 1
4795
4893
  }
4796
4894
  else
4797
- fmix_lock_close_fd "$fd"
4895
+ fmix_lock_keeper_stop "$keeper_pid"
4798
4896
  sleep 0.05
4799
4897
  continue
4800
4898
  fi
@@ -4802,7 +4900,7 @@ fmix_lock_acquire() {
4802
4900
  || { [[ -n "$recorded_start" ]] && [[ "$recorded_start" != "$holder_start" ]]; }; then
4803
4901
  fmix_lock_quarantine_metadata "$lock_name" "$lock_dir" \
4804
4902
  "dead or reused pid $holder_pid" || {
4805
- fmix_lock_close_fd "$fd"
4903
+ fmix_lock_keeper_stop "$keeper_pid"
4806
4904
  return 1
4807
4905
  }
4808
4906
  else
@@ -4810,7 +4908,7 @@ fmix_lock_acquire() {
4810
4908
  fmix_info "Waiting for mixed-version lock '$diagnostic_name' (pid=$holder_pid) …"
4811
4909
  printed_wait=1
4812
4910
  fi
4813
- fmix_lock_close_fd "$fd"
4911
+ fmix_lock_keeper_stop "$keeper_pid"
4814
4912
  if (( $(fmix_epoch) >= deadline )); then
4815
4913
  fmix_error "Timed out waiting for mixed-version lock '$diagnostic_name' after ${timeout}s."
4816
4914
  return 1
@@ -4819,13 +4917,13 @@ fmix_lock_acquire() {
4819
4917
  continue
4820
4918
  fi
4821
4919
  elif [[ -e "$lock_dir" || -L "$lock_dir" ]]; then
4822
- fmix_lock_close_fd "$fd"
4920
+ fmix_lock_keeper_stop "$keeper_pid"
4823
4921
  fmix_error "Lock metadata path is not a directory: $lock_dir"
4824
4922
  return 1
4825
4923
  fi
4826
4924
 
4827
4925
  if ! mkdir "$lock_dir" 2>/dev/null; then
4828
- fmix_lock_close_fd "$fd"
4926
+ fmix_lock_keeper_stop "$keeper_pid"
4829
4927
  if (( $(fmix_epoch) >= deadline )); then
4830
4928
  fmix_error "Timed out publishing lock metadata for '$diagnostic_name'."
4831
4929
  return 1
@@ -4850,7 +4948,7 @@ EOF
4850
4948
  then
4851
4949
  fmix_lock_quarantine_metadata "$lock_name" "$lock_dir" \
4852
4950
  "owner publication failed" >/dev/null 2>&1 || true
4853
- fmix_lock_close_fd "$fd"
4951
+ fmix_lock_keeper_stop "$keeper_pid"
4854
4952
  fmix_error "Could not publish ownership for lock '$diagnostic_name'."
4855
4953
  return 1
4856
4954
  fi
@@ -4868,14 +4966,14 @@ EOF
4868
4966
  true
4869
4967
 
4870
4968
  FMIX_HELD_LOCK_NAMES+=("$lock_name")
4871
- FMIX_HELD_LOCK_FDS+=("$fd")
4969
+ FMIX_HELD_LOCK_KEEPER_PIDS+=("$keeper_pid")
4872
4970
  FMIX_HELD_LOCK_TOKENS+=("$token")
4873
4971
  return 0
4874
4972
  done
4875
4973
  }
4876
4974
 
4877
4975
  fmix_lock_release() {
4878
- local lock_name="${1:-}" index fd token lock_dir recorded_token destination
4976
+ local lock_name="${1:-}" index keeper_pid token lock_dir recorded_token destination
4879
4977
  [[ -n "$lock_name" ]] || {
4880
4978
  fmix_error "fmix_lock_release called with empty lock name — refusing."
4881
4979
  return 1
@@ -4888,15 +4986,15 @@ fmix_lock_release() {
4888
4986
  fmix_error "Refusing to release unowned FMix lock '$lock_name'."
4889
4987
  return 1
4890
4988
  }
4891
- fd="${FMIX_HELD_LOCK_FDS[$index]}"
4989
+ keeper_pid="${FMIX_HELD_LOCK_KEEPER_PIDS[$index]}"
4892
4990
  token="${FMIX_HELD_LOCK_TOKENS[$index]}"
4893
4991
  lock_dir="$FMIX_HOME/locks/$lock_name"
4894
4992
  recorded_token="$(sed -n 's/^token=//p' "$lock_dir/owner" 2>/dev/null | head -1)"
4895
4993
  if [[ "$recorded_token" != "$token" ]] \
4896
4994
  || [[ "$(cat "$lock_dir/pid" 2>/dev/null || true)" != "$$" ]]; then
4897
- fmix_lock_close_fd "$fd"
4995
+ fmix_lock_keeper_stop "$keeper_pid"
4898
4996
  FMIX_HELD_LOCK_NAMES[$index]=""
4899
- FMIX_HELD_LOCK_FDS[$index]=""
4997
+ FMIX_HELD_LOCK_KEEPER_PIDS[$index]=""
4900
4998
  FMIX_HELD_LOCK_TOKENS[$index]=""
4901
4999
  fmix_error "Refusing to remove lock metadata for '$lock_name': ownership changed."
4902
5000
  return 1
@@ -4907,17 +5005,17 @@ fmix_lock_release() {
4907
5005
  destination="$FMIX_HOME/lock-metadata/released/${lock_name}.$(fmix_epoch).$$.${RANDOM}"
4908
5006
  if ! mkdir -p "$(dirname "$destination")" \
4909
5007
  || ! mv "$lock_dir" "$destination"; then
4910
- fmix_lock_close_fd "$fd"
5008
+ fmix_lock_keeper_stop "$keeper_pid"
4911
5009
  FMIX_HELD_LOCK_NAMES[$index]=""
4912
- FMIX_HELD_LOCK_FDS[$index]=""
5010
+ FMIX_HELD_LOCK_KEEPER_PIDS[$index]=""
4913
5011
  FMIX_HELD_LOCK_TOKENS[$index]=""
4914
5012
  fmix_error "Could not retire lock metadata for '$lock_name'."
4915
5013
  fmix_error "The kernel lock was released; stale metadata will be quarantined by the next owner."
4916
5014
  return 1
4917
5015
  fi
4918
- fmix_lock_close_fd "$fd"
5016
+ fmix_lock_keeper_stop "$keeper_pid"
4919
5017
  FMIX_HELD_LOCK_NAMES[$index]=""
4920
- FMIX_HELD_LOCK_FDS[$index]=""
5018
+ FMIX_HELD_LOCK_KEEPER_PIDS[$index]=""
4921
5019
  FMIX_HELD_LOCK_TOKENS[$index]=""
4922
5020
  return 0
4923
5021
  }
@@ -5937,34 +6035,20 @@ fmix_clone_donor_to_session() {
5937
6035
 
5938
6036
  py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
5939
6037
 
5940
- # Lock-Then-Revalidate (LOCK_SH): acquire shared lock on donor session
5941
- local donor_fd
5942
- donor_fd="$(fmix_lock_allocate_fd)" || return 1
5943
- if [[ -f "$donor/meta.json" ]]; then
5944
- eval "exec ${donor_fd}<\"$donor/meta.json\""
5945
- else
5946
- fmix_lock_close_fd "$donor_fd"
5947
- return 1
5948
- fi
5949
-
5950
- if ! "$py_bin" - "$donor_fd" <<'PY' 2>/dev/null
5951
- import fcntl, sys
5952
- fd = int(sys.argv[1])
5953
- try:
5954
- fcntl.flock(fd, fcntl.LOCK_SH | fcntl.LOCK_NB)
5955
- raise SystemExit(0)
5956
- except Exception:
5957
- raise SystemExit(1)
5958
- PY
5959
- then
5960
- fmix_lock_close_fd "$donor_fd"
6038
+ # Lock-Then-Revalidate (LOCK_SH): a dedicated keeper owns the descriptor,
6039
+ # so copy helpers cannot inherit and extend donor-lock lifetime.
6040
+ local donor_keeper_pid
6041
+ [[ -f "$donor/meta.json" ]] || return 1
6042
+ if ! fmix_lock_keeper_start "$donor/meta.json" shared 0 \
6043
+ "donor session clone" "donor-$(fmix_hash_text "$donor" | cut -c1-16)"; then
5961
6044
  fmix_warn "Could not acquire shared lock on donor session $donor."
5962
6045
  return 1
5963
6046
  fi
6047
+ donor_keeper_pid="$FMIX_LAST_LOCK_KEEPER_PID"
5964
6048
 
5965
6049
  # Revalidate donor artifacts under active LOCK_SH
5966
6050
  if [[ ! -d "$donor/deps" || ! -d "$donor/_build" || ! -f "$donor/compile-stamp.json" ]]; then
5967
- fmix_lock_close_fd "$donor_fd"
6051
+ fmix_lock_keeper_stop "$donor_keeper_pid"
5968
6052
  fmix_warn "Donor session $donor missing required artifacts during revalidation."
5969
6053
  return 1
5970
6054
  fi
@@ -5972,25 +6056,25 @@ PY
5972
6056
  retired="$FMIX_ACTIVE_SESSION_ROOT/retired/$(date +%Y%m%dT%H%M%S)-$$-${RANDOM}"
5973
6057
  if [[ -e "$deps_root" || -L "$deps_root" \
5974
6058
  || -e "$build_root" || -L "$build_root" ]]; then
5975
- mkdir -p "$retired" || { fmix_lock_close_fd "$donor_fd"; return 1; }
6059
+ mkdir -p "$retired" || { fmix_lock_keeper_stop "$donor_keeper_pid"; return 1; }
5976
6060
  retired_used=1
5977
6061
  [[ -e "$deps_root" || -L "$deps_root" ]] && mv "$deps_root" "$retired/deps"
5978
6062
  [[ -e "$build_root" || -L "$build_root" ]] && mv "$build_root" "$retired/_build"
5979
6063
  fi
5980
6064
 
5981
6065
  if ! fmix_cow_clone "$donor/deps" "$deps_root"; then
5982
- fmix_lock_close_fd "$donor_fd"
6066
+ fmix_lock_keeper_stop "$donor_keeper_pid"
5983
6067
  return 1
5984
6068
  fi
5985
6069
  if ! fmix_cow_clone "$donor/_build" "$build_root"; then
5986
6070
  mkdir -p "$retired" || true
5987
6071
  mv "$deps_root" "$retired/failed-deps" 2>/dev/null || true
5988
- fmix_lock_close_fd "$donor_fd"
6072
+ fmix_lock_keeper_stop "$donor_keeper_pid"
5989
6073
  return 1
5990
6074
  fi
5991
6075
  chmod -R u+w "$deps_root" "$build_root" || {
5992
6076
  fmix_error "Could not make donor clone writable."
5993
- fmix_lock_close_fd "$donor_fd"
6077
+ fmix_lock_keeper_stop "$donor_keeper_pid"
5994
6078
  return 1
5995
6079
  }
5996
6080
 
@@ -6022,8 +6106,8 @@ for name in ("compile-stamp.json", "dependency-stamp.json"):
6022
6106
  PY
6023
6107
  true
6024
6108
 
6025
- # Release LOCK_SH on donor
6026
- fmix_lock_close_fd "$donor_fd"
6109
+ # Release LOCK_SH on donor.
6110
+ fmix_lock_keeper_stop "$donor_keeper_pid"
6027
6111
 
6028
6112
  # Post-clone manifest synchronization: touch compile manifests to prevent full DAG recompiles
6029
6113
  find "$build_root" -type f -name 'compile.*' -exec touch {} + 2>/dev/null || true
@@ -7785,29 +7869,150 @@ fmix_compile_build_root() {
7785
7869
  fi
7786
7870
  }
7787
7871
 
7872
+ fmix_reject_compile_input_symlink_components() {
7873
+ # Compiler-input pathspecs are always relative to the canonical worktree.
7874
+ # Reject a symlink at any component below that root before asking Git to
7875
+ # enumerate the path: Git returns no entry for a pathspec beneath a tracked
7876
+ # directory symlink, which would otherwise make an escaped input invisible.
7877
+ # Path dependencies deliberately use fmix_hash_path_dependency instead; it
7878
+ # canonicalizes the explicitly configured dependency root first, then
7879
+ # rejects nested links beneath that allowed top-level alias.
7880
+ local root="$1" relative="$2" current remaining component
7881
+ [[ -n "$root" && -n "$relative" ]] || return 1
7882
+ case "$relative" in
7883
+ /*)
7884
+ fmix_error "Compiler input path must be relative to the worktree: $relative"
7885
+ return 1
7886
+ ;;
7887
+ esac
7888
+
7889
+ current="${root%/}"
7890
+ remaining="$relative"
7891
+ while [[ -n "$remaining" ]]; do
7892
+ if [[ "$remaining" == */* ]]; then
7893
+ component="${remaining%%/*}"
7894
+ remaining="${remaining#*/}"
7895
+ else
7896
+ component="$remaining"
7897
+ remaining=""
7898
+ fi
7899
+ case "$component" in
7900
+ ""|.) continue ;;
7901
+ ..)
7902
+ fmix_error "Compiler input path escapes the worktree: $relative"
7903
+ return 1
7904
+ ;;
7905
+ esac
7906
+ current="$current/$component"
7907
+ if [[ -L "$current" ]]; then
7908
+ fmix_error "Symlinked compiler input is unsupported: $current"
7909
+ return 1
7910
+ fi
7911
+ done
7912
+ return 0
7913
+ }
7914
+
7915
+ fmix_reject_repo_elixir_input_symlinks() {
7916
+ # priv/repo compiler inputs are discovered with find before they are passed
7917
+ # to fmix_hash_compile_paths. Validate the discovery root explicitly so a
7918
+ # root or nested directory alias cannot disappear from that candidate list.
7919
+ # This is a worktree-owned input root, not the deliberate top-level alias
7920
+ # exception for explicitly configured path dependencies.
7921
+ local root="$1" repo_root="${1%/}/priv/repo" probe symlink_path
7922
+ fmix_reject_compile_input_symlink_components "$root" "priv/repo" \
7923
+ || return 1
7924
+ [[ -d "$repo_root" ]] || return 0
7925
+ probe="$(mktemp)" || return 1
7926
+ if ! find "$repo_root" -type l -print -quit >"$probe" 2>/dev/null; then
7927
+ rm -f "$probe"
7928
+ return 1
7929
+ fi
7930
+ if [[ -s "$probe" ]]; then
7931
+ IFS= read -r symlink_path <"$probe" || symlink_path="$repo_root"
7932
+ rm -f "$probe"
7933
+ fmix_error "Symlinked compiler input is unsupported: $symlink_path"
7934
+ return 1
7935
+ fi
7936
+ rm -f "$probe"
7937
+ return 0
7938
+ }
7939
+
7788
7940
  fmix_hash_compile_paths() {
7789
- local root="$1"
7941
+ local root="$1" tracked_file="" untracked_file="" entry metadata mode path file
7790
7942
  shift
7791
- if git -C "$root" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
7792
- fmix_hash_source_paths "$root" "$@"
7793
- local untracked
7794
- untracked="$(git -C "$root" ls-files --others --exclude-standard -- "$@" 2>/dev/null)"
7795
- if [[ -n "$untracked" ]]; then
7796
- while IFS= read -r path; do
7797
- [[ -f "$root/$path" ]] || continue
7798
- printf 'untracked:%s:' "$path"
7799
- fmix_hash_files "$root/$path"
7800
- done <<<"$untracked"
7943
+ for path in "$@"; do
7944
+ fmix_reject_compile_input_symlink_components "$root" "$path" || return 1
7945
+ done
7946
+ if [[ -d "$root/.git" || -f "$root/.git" ]]; then
7947
+ git -C "$root" rev-parse --is-inside-work-tree >/dev/null 2>&1 \
7948
+ || return 1
7949
+ tracked_file="$(mktemp)" || return 1
7950
+ if ! git -C "$root" ls-files -s -z -- "$@" \
7951
+ >"$tracked_file" 2>/dev/null; then
7952
+ rm -f "$tracked_file"
7953
+ return 1
7954
+ fi
7955
+ while IFS= read -r -d '' entry; do
7956
+ metadata="${entry%%$'\t'*}"
7957
+ mode="${metadata%% *}"
7958
+ path="${entry#*$'\t'}"
7959
+ [[ "$mode" != "160000" ]] || continue
7960
+ printf 'tracked:%s:%s:' "$metadata" "$path"
7961
+ if [[ -L "$root/$path" ]]; then
7962
+ fmix_error "Symlinked compiler input is unsupported: $root/$path"
7963
+ rm -f "$tracked_file"
7964
+ return 1
7965
+ elif [[ -f "$root/$path" ]]; then
7966
+ fmix_hash_stdin <"$root/$path" \
7967
+ || { rm -f "$tracked_file"; return 1; }
7968
+ else
7969
+ printf 'missing\n'
7970
+ fi
7971
+ done <"$tracked_file"
7972
+ rm -f "$tracked_file"
7973
+ untracked_file="$(mktemp)" || return 1
7974
+ if ! (
7975
+ git -C "$root" ls-files --others --exclude-standard -z -- "$@" \
7976
+ || exit 1
7977
+ # Git ignores do not change what Mix can compile or execute. Keep
7978
+ # ignored source files in the exact run identity, but only beneath
7979
+ # the caller's bounded source/test pathspecs. Never widen an empty
7980
+ # pathspec into a scan of ignored cache artifacts.
7981
+ if (( $# > 0 )); then
7982
+ git -C "$root" ls-files --others --ignored \
7983
+ --exclude-standard -z -- "$@" || exit 1
7984
+ fi
7985
+ ) >"$untracked_file" 2>/dev/null; then
7986
+ rm -f "$untracked_file"
7987
+ return 1
7801
7988
  fi
7989
+ while IFS= read -r -d '' path; do
7990
+ case "$path" in
7991
+ _build|_build/*|deps|deps/*) continue ;;
7992
+ esac
7993
+ if [[ -L "$root/$path" ]]; then
7994
+ fmix_error "Symlinked compiler input is unsupported: $root/$path"
7995
+ rm -f "$untracked_file"
7996
+ return 1
7997
+ fi
7998
+ [[ -f "$root/$path" ]] || continue
7999
+ printf 'untracked:%s:' "$path"
8000
+ fmix_hash_files "$root/$path" \
8001
+ || { rm -f "$untracked_file"; return 1; }
8002
+ done <"$untracked_file"
8003
+ rm -f "$untracked_file"
7802
8004
  return 0
7803
8005
  fi
7804
8006
 
7805
- local path file
7806
8007
  for path in "$@"; do
7807
- if [[ -f "$root/$path" ]]; then
8008
+ if [[ -L "$root/$path" ]]; then
8009
+ fmix_error "Symlinked compiler input is unsupported: $root/$path"
8010
+ return 1
8011
+ elif [[ -f "$root/$path" ]]; then
7808
8012
  printf 'file:%s:' "$path"
7809
8013
  fmix_hash_files "$root/$path"
7810
8014
  elif [[ -d "$root/$path" ]]; then
8015
+ fmix_reject_nested_source_symlinks "$root/$path" || return 1
7811
8016
  while IFS= read -r file; do
7812
8017
  printf 'file:%s:' "${file#"$root/"}"
7813
8018
  fmix_hash_files "$file"
@@ -7818,6 +8023,62 @@ fmix_hash_compile_paths() {
7818
8023
  done
7819
8024
  }
7820
8025
 
8026
+ fmix_ignored_compile_inputs_hash() {
8027
+ # Git's ordinary dirty hash intentionally omits ignored files, but ignored
8028
+ # files under Mix compiler roots are still executable inputs. Hash only
8029
+ # that bounded set; never walk dependency, build, node, or artifact caches.
8030
+ local worktree="$1" real ignored_file manifest hash rc path
8031
+ local -a compile_paths=(
8032
+ .tool-versions mix.exs mix.lock
8033
+ config/config.exs config/config_sync.exs config/test.exs
8034
+ lib test/support test/cdrcisco/policies/reputation_signals/shared
8035
+ vendor dev_tools priv/repo
8036
+ )
8037
+ real="$(fmix_realpath "$worktree" 2>/dev/null)" || return 1
8038
+ if [[ ! -d "$real/.git" && ! -f "$real/.git" ]]; then
8039
+ printf 'non-git\n'
8040
+ return 0
8041
+ fi
8042
+ command -v git >/dev/null 2>&1 || return 1
8043
+ git -C "$real" rev-parse --is-inside-work-tree >/dev/null 2>&1 \
8044
+ || return 1
8045
+ ignored_file="$(mktemp)" || return 1
8046
+ manifest="$(mktemp)" || { rm -f "$ignored_file"; return 1; }
8047
+ if ! git -C "$real" ls-files --others --ignored --exclude-standard -z \
8048
+ -- "${compile_paths[@]}" >"$ignored_file" 2>/dev/null; then
8049
+ rm -f "$ignored_file" "$manifest"
8050
+ return 1
8051
+ fi
8052
+ rc=0
8053
+ {
8054
+ printf 'fmix-ignored-compile-inputs-v1\n'
8055
+ while IFS= read -r -d '' path; do
8056
+ case "/$path/" in
8057
+ */.git/*|*/_build/*|*/deps/*|*/node_modules/*|*/artifacts/*)
8058
+ continue
8059
+ ;;
8060
+ esac
8061
+ [[ -f "$real/$path" ]] || continue
8062
+ printf 'ignored:%s:' "$path"
8063
+ if ! fmix_hash_files "$real/$path"; then
8064
+ rc=1
8065
+ break
8066
+ fi
8067
+ done <"$ignored_file"
8068
+ } >"$manifest"
8069
+ rm -f "$ignored_file"
8070
+ if (( rc != 0 )); then
8071
+ rm -f "$manifest"
8072
+ return "$rc"
8073
+ fi
8074
+ hash="$(fmix_hash_stdin <"$manifest")"
8075
+ rc=$?
8076
+ rm -f "$manifest"
8077
+ (( rc == 0 )) || return "$rc"
8078
+ [[ "$hash" =~ ^[0-9a-f]{64}$ ]] || return 1
8079
+ printf '%s\n' "$hash"
8080
+ }
8081
+
7821
8082
  fmix_compute_compile_fingerprint() {
7822
8083
  # Content-only and deliberately conservative. Ordinary test files are not
7823
8084
  # compiler inputs, but all test elixirc paths, config, dependency inputs,
@@ -7836,9 +8097,11 @@ fmix_compute_compile_fingerprint() {
7836
8097
  vendor dev_tools
7837
8098
  )
7838
8099
  local -a repo_elixir_paths=()
8100
+ fmix_reject_repo_elixir_input_symlinks "$real" || return 1
7839
8101
  while IFS= read -r file; do
7840
8102
  repo_elixir_paths+=("${file#"$real/"}")
7841
- done < <(find "$real/priv/repo" -type f -name '*.ex' -print 2>/dev/null | LC_ALL=C sort)
8103
+ done < <(find "$real/priv/repo" \( -type f -o -type l \) \
8104
+ -name '*.ex' -print 2>/dev/null | LC_ALL=C sort)
7842
8105
  manifest="$(mktemp)" || return 1
7843
8106
  (
7844
8107
  printf 'fmix-compile-fingerprint-v4\n'
@@ -7862,6 +8125,184 @@ fmix_compute_compile_fingerprint() {
7862
8125
  printf '%s\n' "$fingerprint"
7863
8126
  }
7864
8127
 
8128
+ fmix_source_stability_compile_identity() {
8129
+ local worktree="${1:-}" real head manifest identity rc file
8130
+ local -a compile_paths=(
8131
+ .tool-versions mix.exs mix.lock
8132
+ config/config.exs config/config_sync.exs config/test.exs
8133
+ lib test/support test/cdrcisco/policies/reputation_signals/shared
8134
+ vendor dev_tools
8135
+ )
8136
+ local -a repo_elixir_paths=()
8137
+ (( $# >= 1 )) || return 1
8138
+ real="$(fmix_realpath "$worktree")" || return 1
8139
+ if [[ -d "$real/.git" || -f "$real/.git" ]]; then
8140
+ git -C "$real" rev-parse --is-inside-work-tree >/dev/null 2>&1 \
8141
+ || return 1
8142
+ head="$(git -C "$real" rev-parse HEAD 2>/dev/null)" || return 1
8143
+ [[ "$head" =~ ^[0-9a-f]{40}$ ]] || return 1
8144
+ else
8145
+ head="non-git"
8146
+ fi
8147
+ fmix_reject_repo_elixir_input_symlinks "$real" || return 1
8148
+ while IFS= read -r file; do
8149
+ repo_elixir_paths+=("${file#"$real/"}")
8150
+ done < <(find "$real/priv/repo" \( -type f -o -type l \) \
8151
+ -name '*.ex' -print 2>/dev/null | LC_ALL=C sort)
8152
+ manifest="$(mktemp)" || return 1
8153
+ {
8154
+ printf 'fmix-source-stability-compile-v1\n'
8155
+ printf 'worktree=%s\n' "$real"
8156
+ printf 'head=%s\n' "$head"
8157
+ printf 'compile-inputs\n'
8158
+ fmix_hash_compile_paths \
8159
+ "$real" \
8160
+ "${compile_paths[@]}" \
8161
+ "${repo_elixir_paths[@]+"${repo_elixir_paths[@]}"}" || return 1
8162
+ fmix_declared_path_dependency_guard "$real" || return 1
8163
+ fmix_path_dependency_source_guard "$real" || return 1
8164
+ } >"$manifest"
8165
+ rc=$?
8166
+ if (( rc != 0 )); then
8167
+ rm -f "$manifest"
8168
+ return "$rc"
8169
+ fi
8170
+ identity="$(fmix_hash_stdin <"$manifest")"
8171
+ rc=$?
8172
+ rm -f "$manifest"
8173
+ (( rc == 0 )) || return "$rc"
8174
+ [[ "$identity" =~ ^[0-9a-f]{64}$ ]] || return 1
8175
+ printf '%s\n' "$identity"
8176
+ }
8177
+
8178
+ # Exact identity for an immutable test/coverage run. Bind the compiler inputs
8179
+ # above to the selected tests and every shared helper file, including helper
8180
+ # dependencies such as test_helper_nats.exs.
8181
+ fmix_source_stability_identity() {
8182
+ local worktree="${1:-}" real manifest identity compile_identity rc arg candidate relative
8183
+ local selected_path_count=0 file
8184
+ local -a helper_paths=()
8185
+ (( $# >= 1 )) || return 1
8186
+ shift
8187
+ real="$(fmix_realpath "$worktree")" || return 1
8188
+ compile_identity="$(fmix_source_stability_compile_identity "$real")" || return 1
8189
+ while IFS= read -r file; do
8190
+ helper_paths+=("${file#"$real/"}")
8191
+ done < <(find "$real/test" \( -type f -o -type l \) \
8192
+ \( -name '*helper*.ex' -o -name '*helper*.exs' \) \
8193
+ -print 2>/dev/null | LC_ALL=C sort)
8194
+
8195
+ manifest="$(mktemp)" || return 1
8196
+ {
8197
+ printf 'fmix-source-stability-v2\n'
8198
+ printf 'compile-identity=%s\n' "$compile_identity"
8199
+ printf 'shared-test-helpers\n'
8200
+ fmix_hash_compile_paths \
8201
+ "$real" \
8202
+ "${helper_paths[@]+"${helper_paths[@]}"}" || return 1
8203
+ printf 'selected-count=%s\n' "$#"
8204
+ for arg in "$@"; do
8205
+ printf 'selected-arg:'
8206
+ printf '%s' "$arg" | fmix_hash_stdin || return 1
8207
+ candidate="$arg"
8208
+ if [[ "$candidate" =~ ^(.+):[0-9]+$ ]]; then
8209
+ candidate="${BASH_REMATCH[1]}"
8210
+ fi
8211
+ [[ "$candidate" != -* ]] || continue
8212
+ if [[ "$candidate" == /* ]]; then
8213
+ case "$candidate" in
8214
+ "$real"/*) relative="${candidate#"$real/"}" ;;
8215
+ *) continue ;;
8216
+ esac
8217
+ else
8218
+ relative="${candidate#./}"
8219
+ case "$relative" in ../*|*/../*|..|*/..) continue ;; esac
8220
+ fi
8221
+ [[ -e "$real/$relative" || -L "$real/$relative" ]] || continue
8222
+ printf 'selected-path=%s\n' "$relative"
8223
+ fmix_hash_compile_paths "$real" "$relative" || return 1
8224
+ selected_path_count=$((selected_path_count + 1))
8225
+ done
8226
+ if (( selected_path_count == 0 )); then
8227
+ printf 'selected-default=test\n'
8228
+ fmix_hash_compile_paths "$real" test || return 1
8229
+ fi
8230
+ } >"$manifest"
8231
+ rc=$?
8232
+ if (( rc != 0 )); then
8233
+ rm -f "$manifest"
8234
+ return "$rc"
8235
+ fi
8236
+ identity="$(fmix_hash_stdin <"$manifest")"
8237
+ rc=$?
8238
+ rm -f "$manifest"
8239
+ (( rc == 0 )) || return "$rc"
8240
+ [[ "$identity" =~ ^[0-9a-f]{64}$ ]] || return 1
8241
+ printf '%s\n' "$identity"
8242
+ }
8243
+
8244
+ fmix_invalidate_compile_stamp() {
8245
+ local worktree="${1:-}" stamp
8246
+ [[ -n "$worktree" ]] || return 1
8247
+ stamp="$(fmix_compile_stamp_file "$worktree" 2>/dev/null)" || return 1
8248
+ [[ -n "$stamp" ]] || return 1
8249
+ if [[ -e "$stamp" || -L "$stamp" ]]; then
8250
+ rm -f "$stamp" || {
8251
+ fmix_error "Could not invalidate rejected compile stamp: $stamp"
8252
+ return 1
8253
+ }
8254
+ fi
8255
+ return 0
8256
+ }
8257
+
8258
+ fmix_invalidate_dependency_stamp() {
8259
+ local worktree="${1:-}" stamp
8260
+ [[ -n "$worktree" ]] || return 1
8261
+ stamp="$(fmix_dependency_stamp_file "$worktree" 2>/dev/null)" || return 1
8262
+ [[ -n "$stamp" ]] || return 1
8263
+ if [[ -e "$stamp" || -L "$stamp" ]]; then
8264
+ rm -f "$stamp" || {
8265
+ fmix_error "Could not invalidate rejected dependency stamp: $stamp"
8266
+ return 1
8267
+ }
8268
+ fi
8269
+ return 0
8270
+ }
8271
+
8272
+ fmix_invalidate_rejected_stamps() {
8273
+ local worktree="${1:-}" rc=0
8274
+ fmix_invalidate_compile_stamp "$worktree" || rc=1
8275
+ fmix_invalidate_dependency_stamp "$worktree" || rc=1
8276
+ return "$rc"
8277
+ }
8278
+
8279
+ fmix_require_stable_source() {
8280
+ local worktree="${1:-}" phase="${2:-execution}" invalidate_stamps="${3:-0}" current
8281
+ [[ "${FMIX_SOURCE_STABILITY_REQUIRED:-0}" == "1" ]] || return 0
8282
+ [[ "${FMIX_SOURCE_STABILITY_IDENTITY:-}" =~ ^[0-9a-f]{64}$ ]] || {
8283
+ fmix_error "Source stability identity is missing; refusing $phase."
8284
+ [[ "$invalidate_stamps" != "1" ]] \
8285
+ || fmix_invalidate_rejected_stamps "$worktree"
8286
+ return 1
8287
+ }
8288
+ current="$(fmix_source_stability_identity \
8289
+ "$worktree" \
8290
+ "${FMIX_SOURCE_STABILITY_SELECTED_ARGS[@]+"${FMIX_SOURCE_STABILITY_SELECTED_ARGS[@]}"}")" \
8291
+ || {
8292
+ fmix_error "Source stability could not be revalidated; refusing $phase."
8293
+ [[ "$invalidate_stamps" != "1" ]] \
8294
+ || fmix_invalidate_rejected_stamps "$worktree"
8295
+ return 1
8296
+ }
8297
+ if [[ "$current" != "$FMIX_SOURCE_STABILITY_IDENTITY" ]]; then
8298
+ fmix_error "Source/test inputs changed during $phase; refusing to use mixed-revision results."
8299
+ [[ "$invalidate_stamps" != "1" ]] \
8300
+ || fmix_invalidate_rejected_stamps "$worktree"
8301
+ return 1
8302
+ fi
8303
+ return 0
8304
+ }
8305
+
7865
8306
  fmix_compute_dependency_fingerprint() {
7866
8307
  # Inputs that can change dependency resolution or native dependency builds.
7867
8308
  # Production/test source and ordinary config changes deliberately stay out:
@@ -7909,9 +8350,11 @@ fmix_compute_legacy_compile_fingerprint_v2() {
7909
8350
  vendor dev_tools
7910
8351
  )
7911
8352
  local -a repo_elixir_paths=()
8353
+ fmix_reject_repo_elixir_input_symlinks "$real" || return 1
7912
8354
  while IFS= read -r file; do
7913
8355
  repo_elixir_paths+=("${file#"$real/"}")
7914
- done < <(find "$real/priv/repo" -type f -name '*.ex' -print 2>/dev/null | LC_ALL=C sort)
8356
+ done < <(find "$real/priv/repo" \( -type f -o -type l \) \
8357
+ -name '*.ex' -print 2>/dev/null | LC_ALL=C sort)
7915
8358
  {
7916
8359
  printf 'fmix-compile-fingerprint-v2\n'
7917
8360
  printf 'worktree=%s\n' "$real"
@@ -8080,6 +8523,35 @@ fmix_dependency_source_root() {
8080
8523
  fi
8081
8524
  }
8082
8525
 
8526
+ fmix_reject_nested_source_symlinks() {
8527
+ # Local dependency roots may themselves be symlinks (Mix commonly creates
8528
+ # those for path deps), so canonicalize the explicit root first. Beneath
8529
+ # that root, never follow or accept another symlink: its target could escape
8530
+ # the bounded compiler-input tree, form a cycle, or change without changing
8531
+ # the link identity recorded by Git.
8532
+ local root="$1" real probe symlink_path
8533
+ real="$(fmix_realpath "$root")" || return 1
8534
+ [[ -d "$real" ]] || return 1
8535
+ probe="$(mktemp)" || return 1
8536
+ if ! find "$real" \
8537
+ \( \( -type d -o -type l \) \
8538
+ \( -name .git -o -name _build -o -name deps -o \
8539
+ -name target -o -name c_build -o -name node_modules -o \
8540
+ -name .elixir_ls \) -prune \) -o \
8541
+ -type l -print -quit >"$probe" 2>/dev/null; then
8542
+ rm -f "$probe"
8543
+ return 1
8544
+ fi
8545
+ if [[ -s "$probe" ]]; then
8546
+ IFS= read -r symlink_path <"$probe" || symlink_path="$real"
8547
+ rm -f "$probe"
8548
+ fmix_error "Symlinked compiler input is unsupported: $symlink_path"
8549
+ return 1
8550
+ fi
8551
+ rm -f "$probe"
8552
+ return 0
8553
+ }
8554
+
8083
8555
  fmix_dependency_source_guard() {
8084
8556
  local worktree="$1" deps_root
8085
8557
  deps_root="$(fmix_dependency_source_root "$worktree")"
@@ -8095,6 +8567,7 @@ fmix_dependency_source_guard() {
8095
8567
  dependency_count=$(( dependency_count + 1 ))
8096
8568
  printf '%s\n' "$(basename "$dependency")"
8097
8569
  if [[ -f "$dependency/.hex" ]]; then
8570
+ fmix_reject_nested_source_symlinks "$dependency" || return 1
8098
8571
  hex_markers+=("$dependency/.hex")
8099
8572
  elif [[ -e "$dependency/.git" ]]; then
8100
8573
  fmix_hash_git_dependency "$dependency" || return 1
@@ -8150,6 +8623,7 @@ fmix_hash_path_dependency() {
8150
8623
  local dependency="$1" path_real file
8151
8624
  local -a files=()
8152
8625
  path_real="$(fmix_realpath "$dependency")" || return 1
8626
+ fmix_reject_nested_source_symlinks "$path_real" || return 1
8153
8627
 
8154
8628
  while IFS= read -r file; do
8155
8629
  files+=("$file")
@@ -8247,8 +8721,9 @@ fmix_declared_path_dependency_resolution_guard() {
8247
8721
  }
8248
8722
 
8249
8723
  fmix_hash_git_dependency() {
8250
- local dependency="$1" commit diff_hash file
8724
+ local dependency="$1" commit diff_hash file untracked_file
8251
8725
  local -a untracked=()
8726
+ fmix_reject_nested_source_symlinks "$dependency" || return 1
8252
8727
  commit="$(git -C "$dependency" rev-parse HEAD 2>/dev/null)" || return 1
8253
8728
  [[ -n "$commit" ]] || return 1
8254
8729
  printf '.git-commit:%s\n' "$commit"
@@ -8257,9 +8732,16 @@ fmix_hash_git_dependency() {
8257
8732
  | fmix_hash_stdin)" || return 1
8258
8733
  printf '.git-worktree-diff:%s\n' "$diff_hash"
8259
8734
 
8735
+ untracked_file="$(mktemp)" || return 1
8736
+ if ! git -C "$dependency" ls-files --others --exclude-standard -z \
8737
+ >"$untracked_file" 2>/dev/null; then
8738
+ rm -f "$untracked_file"
8739
+ return 1
8740
+ fi
8260
8741
  while IFS= read -r -d '' file; do
8261
8742
  untracked+=("$dependency/$file")
8262
- done < <(git -C "$dependency" ls-files --others --exclude-standard -z 2>/dev/null)
8743
+ done <"$untracked_file"
8744
+ rm -f "$untracked_file"
8263
8745
  if (( ${#untracked[@]} > 0 )); then
8264
8746
  printf '.git-untracked-count:%s\n' "${#untracked[@]}"
8265
8747
  fmix_hash_file_batches "${untracked[@]}" || return 1
@@ -8424,13 +8906,14 @@ PY
8424
8906
 
8425
8907
  fmix_get_git_tree_hash() {
8426
8908
  local worktree="$1" real tree_hash
8427
- real="$(fmix_realpath "$worktree" 2>/dev/null || echo "$worktree")"
8428
- if command -v git >/dev/null 2>&1 && { [[ -d "$real/.git" ]] || [[ -f "$real/.git" ]]; }; then
8429
- tree_hash="$(git -C "$real" rev-parse HEAD^{tree} 2>/dev/null || true)"
8430
- if [[ "$tree_hash" =~ ^[0-9a-f]{40}$ ]]; then
8431
- printf '%s\n' "$tree_hash"
8432
- return 0
8433
- fi
8909
+ real="$(fmix_realpath "$worktree" 2>/dev/null)" || return 1
8910
+ if [[ -d "$real/.git" || -f "$real/.git" ]]; then
8911
+ command -v git >/dev/null 2>&1 || return 1
8912
+ tree_hash="$(git -C "$real" rev-parse HEAD^{tree} 2>/dev/null)" \
8913
+ || return 1
8914
+ [[ "$tree_hash" =~ ^[0-9a-f]{40}$ ]] || return 1
8915
+ printf '%s\n' "$tree_hash"
8916
+ return 0
8434
8917
  fi
8435
8918
  printf 'unknown\n'
8436
8919
  }
@@ -8453,37 +8936,84 @@ fmix_compute_compile_env_hash() {
8453
8936
  }
8454
8937
 
8455
8938
  fmix_compute_dirty_hash() {
8456
- local worktree="$1" real status
8457
- real="$(fmix_realpath "$worktree" 2>/dev/null || echo "$worktree")"
8458
- if ! command -v git >/dev/null 2>&1 || { [[ ! -d "$real/.git" ]] && [[ ! -f "$real/.git" ]]; }; then
8939
+ local worktree="$1" real status manifest hash rc file
8940
+ local untracked_file diff_file staged_file
8941
+ local -a untracked=()
8942
+ real="$(fmix_realpath "$worktree" 2>/dev/null)" || return 1
8943
+ if [[ ! -d "$real/.git" && ! -f "$real/.git" ]]; then
8459
8944
  printf 'clean\n'
8460
8945
  return 0
8461
8946
  fi
8462
- status="$(git -C "$real" status --porcelain=v1 2>/dev/null || true)"
8947
+ command -v git >/dev/null 2>&1 || return 1
8948
+ git -C "$real" rev-parse --is-inside-work-tree >/dev/null 2>&1 \
8949
+ || return 1
8950
+ status="$(git -C "$real" status --porcelain=v1 2>/dev/null)" || return 1
8463
8951
  if [[ -z "$status" ]]; then
8464
8952
  printf 'clean\n'
8465
8953
  return 0
8466
8954
  fi
8955
+ manifest="$(mktemp)" || return 1
8956
+ untracked_file="$(mktemp)" || { rm -f "$manifest"; return 1; }
8957
+ diff_file="$(mktemp)" \
8958
+ || { rm -f "$manifest" "$untracked_file"; return 1; }
8959
+ staged_file="$(mktemp)" \
8960
+ || { rm -f "$manifest" "$untracked_file" "$diff_file"; return 1; }
8961
+ if ! git -C "$real" ls-files --others --exclude-standard -z \
8962
+ >"$untracked_file" 2>/dev/null; then
8963
+ rm -f "$manifest" "$untracked_file" "$diff_file" "$staged_file"
8964
+ return 1
8965
+ fi
8966
+ if ! git -C "$real" diff --no-ext-diff --binary -- . \
8967
+ >"$diff_file" 2>/dev/null; then
8968
+ rm -f "$manifest" "$untracked_file" "$diff_file" "$staged_file"
8969
+ return 1
8970
+ fi
8971
+ if ! git -C "$real" diff --cached --no-ext-diff --binary -- . \
8972
+ >"$staged_file" 2>/dev/null; then
8973
+ rm -f "$manifest" "$untracked_file" "$diff_file" "$staged_file"
8974
+ return 1
8975
+ fi
8976
+ while IFS= read -r -d '' file; do
8977
+ untracked+=("$file")
8978
+ done <"$untracked_file"
8467
8979
  (
8468
8980
  printf '%s\n' "$status"
8469
- git -C "$real" ls-files --others --modified --exclude-standard 2>/dev/null | head -50 | while IFS= read -r f; do
8470
- fmix_stat_mtime "$real/$f" 2>/dev/null || echo 0
8981
+ cat "$diff_file" || exit 1
8982
+ cat "$staged_file" || exit 1
8983
+ for file in "${untracked[@]+"${untracked[@]}"}"; do
8984
+ printf 'untracked:%s:' "$file"
8985
+ fmix_hash_files "$real/$file" || exit 1
8471
8986
  done
8472
- ) | fmix_hash_stdin
8987
+ ) >"$manifest"
8988
+ rc=$?
8989
+ rm -f "$untracked_file" "$diff_file" "$staged_file"
8990
+ if (( rc != 0 )); then
8991
+ rm -f "$manifest"
8992
+ return "$rc"
8993
+ fi
8994
+ hash="$(fmix_hash_stdin <"$manifest")"
8995
+ rc=$?
8996
+ rm -f "$manifest"
8997
+ (( rc == 0 )) || return "$rc"
8998
+ [[ "$hash" =~ ^[0-9a-f]{64}$ ]] || return 1
8999
+ printf '%s\n' "$hash"
8473
9000
  }
8474
9001
 
8475
9002
  fmix_write_compile_stamp() {
8476
9003
  local worktree="$1" fingerprint="$2" real build_root guard dependency_guard stamp tmp quick_guard
8477
- local template_key tree_hash env_hash dirty_hash
9004
+ local template_key tree_hash env_hash dirty_hash ignored_compile_hash source_stability_identity
8478
9005
  real="$(fmix_realpath "$worktree")"
8479
9006
  build_root="$(fmix_compile_build_root "$real")"
8480
9007
  guard="$(fmix_compile_manifest_guard "$real")" || return 1
8481
9008
  dependency_guard="$(fmix_dependency_source_guard "$real")" || return 1
8482
- quick_guard="$(fmix_compile_quick_guard "$real")" || quick_guard=""
9009
+ quick_guard="$(fmix_compile_quick_guard "$real")" || return 1
8483
9010
  template_key="$(fmix_compute_template_key "$real" 2>/dev/null || echo "")"
8484
- tree_hash="$(fmix_get_git_tree_hash "$real")"
9011
+ tree_hash="$(fmix_get_git_tree_hash "$real")" || return 1
8485
9012
  env_hash="$(fmix_compute_compile_env_hash "$real" 2>/dev/null || echo "")"
8486
- dirty_hash="$(fmix_compute_dirty_hash "$real" 2>/dev/null || echo "clean")"
9013
+ dirty_hash="$(fmix_compute_dirty_hash "$real" 2>/dev/null)" || return 1
9014
+ ignored_compile_hash="$(fmix_ignored_compile_inputs_hash "$real" 2>/dev/null)" \
9015
+ || return 1
9016
+ source_stability_identity="${FMIX_SOURCE_STABILITY_COMPILE_IDENTITY:-}"
8487
9017
  stamp="$(fmix_compile_stamp_file "$real")"
8488
9018
  mkdir -p "$(dirname "$stamp")"
8489
9019
  tmp="$(mktemp "${stamp}.tmp.XXXXXX")" || return 1
@@ -8496,10 +9026,12 @@ fmix_write_compile_stamp() {
8496
9026
  "git_tree_hash": $(fmix_json_escape "$tree_hash"),
8497
9027
  "compile_env_hash": $(fmix_json_escape "$env_hash"),
8498
9028
  "dirty_hash": $(fmix_json_escape "$dirty_hash"),
9029
+ "ignored_compile_hash": $(fmix_json_escape "$ignored_compile_hash"),
8499
9030
  "fingerprint": $(fmix_json_escape "$fingerprint"),
8500
9031
  "manifest_guard": $(fmix_json_escape "$guard"),
8501
9032
  "dependency_source_guard": $(fmix_json_escape "$dependency_guard"),
8502
9033
  "quick_guard": $(fmix_json_escape "$quick_guard"),
9034
+ "source_stability_identity": $(fmix_json_escape "$source_stability_identity"),
8503
9035
  "compiled_at": $(fmix_json_escape "$(fmix_now)")
8504
9036
  }
8505
9037
  EOF
@@ -8510,11 +9042,16 @@ EOF
8510
9042
  mv "$tmp" "$stamp" || { rm -f "$tmp"; return 1; }
8511
9043
  }
8512
9044
 
8513
- # Warm-path short-circuit: accept an existing stamp when the cheap invalidator
8514
- # matches and beams still exist. Skips full content fingerprint + deps walk.
9045
+ # Warm-path staged admission: cheap invalidators reject obvious drift before
9046
+ # bounded live-content and dependency-source guards authorize artifact reuse.
8515
9047
  fmix_try_compile_stamp_quick_hit() {
8516
- local worktree="$1" real build_root stamp dependency_stamp dependency_fingerprint dependency_guard
9048
+ local worktree="$1" real build_root stamp dependency_stamp dependency_fingerprint
8517
9049
  local recorded_path recorded_build recorded_quick current_quick
9050
+ local recorded_tree_hash current_tree_hash recorded_dirty_hash current_dirty_hash
9051
+ local recorded_ignored_compile_hash current_ignored_compile_hash
9052
+ local recorded_fingerprint current_fingerprint
9053
+ local recorded_dependency_guard current_dependency_guard dependency_stamp_guard
9054
+ local recorded_source_stability_identity
8518
9055
  local app_ebin
8519
9056
  real="$(fmix_realpath "$worktree")" || return 1
8520
9057
  build_root="$(fmix_compile_build_root "$real")" || return 1
@@ -8523,21 +9060,53 @@ fmix_try_compile_stamp_quick_hit() {
8523
9060
  recorded_path="$(fmix_read_state "$stamp" path)"
8524
9061
  recorded_build="$(fmix_read_state "$stamp" build_root)"
8525
9062
  recorded_quick="$(fmix_read_state "$stamp" quick_guard)"
9063
+ recorded_tree_hash="$(fmix_read_state "$stamp" git_tree_hash)"
9064
+ recorded_dirty_hash="$(fmix_read_state "$stamp" dirty_hash)"
9065
+ recorded_ignored_compile_hash="$(fmix_read_state "$stamp" ignored_compile_hash)"
9066
+ recorded_fingerprint="$(fmix_read_state "$stamp" fingerprint)"
9067
+ recorded_dependency_guard="$(fmix_read_state "$stamp" dependency_source_guard)"
9068
+ recorded_source_stability_identity="$(fmix_read_state "$stamp" source_stability_identity)"
9069
+ [[ "$recorded_path" == "$real" ]] || return 1
9070
+ [[ "$recorded_build" == "$build_root" ]] || return 1
8526
9071
  [[ -n "$recorded_quick" ]] || return 1
9072
+ [[ -n "$recorded_tree_hash" && "$recorded_tree_hash" != "unknown" ]] || return 1
9073
+ [[ -n "$recorded_dirty_hash" ]] || return 1
9074
+ [[ "$recorded_fingerprint" =~ ^[0-9a-f]{64}$ ]] || return 1
9075
+ [[ "$recorded_dependency_guard" =~ ^[0-9a-f]{64}$ ]] || return 1
9076
+ [[ "$recorded_ignored_compile_hash" =~ ^[0-9a-f]{64}$ \
9077
+ || "$recorded_ignored_compile_hash" == "non-git" ]] || return 1
9078
+ if [[ "${FMIX_SOURCE_STABILITY_REQUIRED:-0}" == "1" ]]; then
9079
+ [[ "${FMIX_SOURCE_STABILITY_COMPILE_IDENTITY:-}" =~ ^[0-9a-f]{64}$ ]] || return 1
9080
+ [[ "$recorded_source_stability_identity" \
9081
+ == "$FMIX_SOURCE_STABILITY_COMPILE_IDENTITY" ]] || return 1
9082
+ fi
8527
9083
  app_ebin="$build_root/test/lib/cdrcisco/ebin"
8528
9084
  [[ -d "$app_ebin" ]] || return 1
8529
9085
  [[ -n "$(find "$app_ebin" -maxdepth 1 -type f -name '*.beam' -print -quit 2>/dev/null)" ]] || return 1
8530
9086
  current_quick="$(fmix_compile_quick_guard "$real")" || return 1
8531
9087
  [[ "$recorded_quick" == "$current_quick" ]] || return 1
9088
+ current_tree_hash="$(fmix_get_git_tree_hash "$real")" || return 1
9089
+ [[ "$current_tree_hash" != "unknown" \
9090
+ && "$recorded_tree_hash" == "$current_tree_hash" ]] || return 1
9091
+ current_dirty_hash="$(fmix_compute_dirty_hash "$real")" || return 1
9092
+ [[ "$recorded_dirty_hash" == "$current_dirty_hash" ]] || return 1
9093
+ current_ignored_compile_hash="$(fmix_ignored_compile_inputs_hash "$real")" \
9094
+ || return 1
9095
+ [[ "$recorded_ignored_compile_hash" == "$current_ignored_compile_hash" ]] \
9096
+ || return 1
9097
+ # Git's status/index stat cache and the mtime quick guard are only early
9098
+ # rejectors. Admission is content-exact for the bounded compiler inputs.
9099
+ current_fingerprint="$(fmix_compute_compile_fingerprint "$real")" || return 1
9100
+ [[ "$recorded_fingerprint" == "$current_fingerprint" ]] || return 1
9101
+ current_dependency_guard="$(fmix_dependency_source_guard "$real")" || return 1
9102
+ [[ "$recorded_dependency_guard" == "$current_dependency_guard" ]] || return 1
8532
9103
  # A quick hit must not accept a stale or partial dependency checkpoint.
8533
- # Validate the cheap structural fields here; the full dependency/source
8534
- # guard remains the responsibility of fmix_ensure_compile on a miss.
8535
9104
  dependency_stamp="$(fmix_dependency_stamp_file "$real")"
8536
9105
  [[ -f "$dependency_stamp" ]] || return 1
8537
9106
  dependency_fingerprint="$(fmix_read_state "$dependency_stamp" dependency_fingerprint)"
8538
- dependency_guard="$(fmix_read_state "$dependency_stamp" source_guard)"
9107
+ dependency_stamp_guard="$(fmix_read_state "$dependency_stamp" source_guard)"
8539
9108
  [[ "$dependency_fingerprint" =~ ^[0-9a-f]{64}$ ]] || return 1
8540
- [[ -n "$dependency_guard" ]] || return 1
9109
+ [[ "$dependency_stamp_guard" == "$current_dependency_guard" ]] || return 1
8541
9110
  return 0
8542
9111
  }
8543
9112
 
@@ -8565,6 +9134,8 @@ fmix_compile_stamp_matches() {
8565
9134
  local recorded_schema recorded_path recorded_build recorded_fingerprint recorded_guard current_guard
8566
9135
  local recorded_dependency_guard current_dependency_guard
8567
9136
  local recorded_template_key recorded_tree_hash recorded_env_hash recorded_dirty_hash
9137
+ local recorded_ignored_compile_hash current_ignored_compile_hash
9138
+ local recorded_source_stability_identity
8568
9139
  real="$(fmix_realpath "$worktree")"
8569
9140
  build_root="$(fmix_compile_build_root "$real")"
8570
9141
  stamp="$(fmix_compile_stamp_file "$real")"
@@ -8576,6 +9147,12 @@ fmix_compile_stamp_matches() {
8576
9147
  recorded_fingerprint="$(fmix_read_state "$stamp" fingerprint)"
8577
9148
  recorded_guard="$(fmix_read_state "$stamp" manifest_guard)"
8578
9149
  recorded_dependency_guard="$(fmix_read_state "$stamp" dependency_source_guard)"
9150
+ recorded_source_stability_identity="$(fmix_read_state "$stamp" source_stability_identity)"
9151
+ if [[ "${FMIX_SOURCE_STABILITY_REQUIRED:-0}" == "1" ]]; then
9152
+ [[ "${FMIX_SOURCE_STABILITY_COMPILE_IDENTITY:-}" =~ ^[0-9a-f]{64}$ ]] || return 1
9153
+ [[ "$recorded_source_stability_identity" \
9154
+ == "$FMIX_SOURCE_STABILITY_COMPILE_IDENTITY" ]] || return 1
9155
+ fi
8579
9156
 
8580
9157
  # Schema v2 content-identity matching (Patch 3)
8581
9158
  if [[ "$recorded_schema" == "2" ]]; then
@@ -8583,12 +9160,18 @@ fmix_compile_stamp_matches() {
8583
9160
  recorded_tree_hash="$(fmix_read_state "$stamp" git_tree_hash)"
8584
9161
  recorded_env_hash="$(fmix_read_state "$stamp" compile_env_hash)"
8585
9162
  recorded_dirty_hash="$(fmix_read_state "$stamp" dirty_hash)"
9163
+ recorded_ignored_compile_hash="$(fmix_read_state "$stamp" ignored_compile_hash)"
8586
9164
 
8587
9165
  local current_template_key current_tree_hash current_env_hash current_dirty_hash
8588
9166
  current_template_key="$(fmix_compute_template_key "$real" 2>/dev/null || echo "")"
8589
- current_tree_hash="$(fmix_get_git_tree_hash "$real")"
9167
+ current_tree_hash="$(fmix_get_git_tree_hash "$real")" || return 1
8590
9168
  current_env_hash="$(fmix_compute_compile_env_hash "$real" 2>/dev/null || echo "")"
8591
- current_dirty_hash="$(fmix_compute_dirty_hash "$real" 2>/dev/null || echo "clean")"
9169
+ current_dirty_hash="$(fmix_compute_dirty_hash "$real" 2>/dev/null)" \
9170
+ || return 1
9171
+ current_ignored_compile_hash="$(fmix_ignored_compile_inputs_hash "$real" 2>/dev/null)" \
9172
+ || return 1
9173
+ current_dependency_guard="$(fmix_dependency_source_guard "$real")" \
9174
+ || return 1
8592
9175
 
8593
9176
  if [[ "$recorded_tree_hash" != "unknown" && "$current_tree_hash" != "unknown" ]]; then
8594
9177
  # template_key is compared for equality rather than required to be
@@ -8601,7 +9184,11 @@ fmix_compile_stamp_matches() {
8601
9184
  if [[ "$recorded_template_key" == "$current_template_key" \
8602
9185
  && -n "$recorded_tree_hash" && "$recorded_tree_hash" == "$current_tree_hash" \
8603
9186
  && -n "$recorded_env_hash" && "$recorded_env_hash" == "$current_env_hash" \
8604
- && "$recorded_dirty_hash" == "$current_dirty_hash" ]]; then
9187
+ && "$recorded_dirty_hash" == "$current_dirty_hash" \
9188
+ && "$recorded_ignored_compile_hash" == "$current_ignored_compile_hash" \
9189
+ && "$recorded_fingerprint" == "$fingerprint" \
9190
+ && -n "$recorded_dependency_guard" \
9191
+ && "$recorded_dependency_guard" == "$current_dependency_guard" ]]; then
8605
9192
  local app_ebin="$build_root/test/lib/cdrcisco/ebin"
8606
9193
  if [[ -d "$app_ebin" ]] && [[ -n "$(find "$app_ebin" -maxdepth 1 -type f -name '*.beam' -print -quit 2>/dev/null)" ]]; then
8607
9194
  return 0
@@ -8767,7 +9354,7 @@ fmix_adopt_compatible_worktree_artifacts() {
8767
9354
  }
8768
9355
 
8769
9356
  FMIX_CURRENT_SEMAPHORE_SLOT=""
8770
- FMIX_CURRENT_SEMAPHORE_FD=""
9357
+ FMIX_CURRENT_SEMAPHORE_KEEPER_PID=""
8771
9358
 
8772
9359
  fmix_compile_semaphore_permits() {
8773
9360
  if [[ -n "${FMIX_COMPILE_PERMITS:-}" ]]; then
@@ -8791,8 +9378,7 @@ fmix_compile_semaphore_acquire() {
8791
9378
  permits="$(fmix_compile_semaphore_permits)"
8792
9379
  deadline_secs="${FMIX_CALLER_DEADLINE_SECS:-}"
8793
9380
 
8794
- local py_bin slot fd acquired_slot=-1 acquired_fd=""
8795
- py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
9381
+ local slot acquired_slot=-1 acquired_keeper_pid=""
8796
9382
  mkdir -p "$FMIX_HOME/lock-kernel" "$FMIX_HOME/lock-metadata" || return 1
8797
9383
 
8798
9384
  local t0=$(fmix_epoch)
@@ -8802,28 +9388,17 @@ fmix_compile_semaphore_acquire() {
8802
9388
  while true; do
8803
9389
  for (( slot = 0; slot < permits; slot++ )); do
8804
9390
  local kfile="$FMIX_HOME/lock-kernel/compile-permit-${slot}"
8805
- fd="$(fmix_lock_allocate_fd)" || return 1
8806
- fmix_lock_open_fd "$fd" "$kfile" || { fmix_lock_close_fd "$fd"; continue; }
8807
- if "$py_bin" - "$fd" 0 <<'PY' 2>/dev/null
8808
- import fcntl, sys
8809
- fd = int(sys.argv[1])
8810
- try:
8811
- fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
8812
- raise SystemExit(0)
8813
- except Exception:
8814
- raise SystemExit(1)
8815
- PY
8816
- then
9391
+ if fmix_lock_keeper_start "$kfile" exclusive 0 \
9392
+ "compile permit ${slot}" "compile-permit-${slot}" 2>/dev/null; then
8817
9393
  acquired_slot="$slot"
8818
- acquired_fd="$fd"
9394
+ acquired_keeper_pid="$FMIX_LAST_LOCK_KEEPER_PID"
8819
9395
  break
8820
9396
  fi
8821
- fmix_lock_close_fd "$fd"
8822
9397
  done
8823
9398
 
8824
9399
  if (( acquired_slot >= 0 )); then
8825
9400
  FMIX_CURRENT_SEMAPHORE_SLOT="$acquired_slot"
8826
- FMIX_CURRENT_SEMAPHORE_FD="$acquired_fd"
9401
+ FMIX_CURRENT_SEMAPHORE_KEEPER_PID="$acquired_keeper_pid"
8827
9402
  cat >"$FMIX_HOME/lock-metadata/compile-permit-${acquired_slot}.json" 2>/dev/null <<EOF
8828
9403
  {
8829
9404
  "slot": $acquired_slot,
@@ -8861,11 +9436,12 @@ EOF
8861
9436
  }
8862
9437
 
8863
9438
  fmix_compile_semaphore_release() {
8864
- if [[ -n "$FMIX_CURRENT_SEMAPHORE_SLOT" && -n "$FMIX_CURRENT_SEMAPHORE_FD" ]]; then
9439
+ if [[ -n "$FMIX_CURRENT_SEMAPHORE_SLOT" \
9440
+ && -n "$FMIX_CURRENT_SEMAPHORE_KEEPER_PID" ]]; then
8865
9441
  rm -f "$FMIX_HOME/lock-metadata/compile-permit-${FMIX_CURRENT_SEMAPHORE_SLOT}.json" 2>/dev/null || true
8866
- fmix_lock_close_fd "$FMIX_CURRENT_SEMAPHORE_FD" 2>/dev/null || true
9442
+ fmix_lock_keeper_stop "$FMIX_CURRENT_SEMAPHORE_KEEPER_PID" 2>/dev/null || true
8867
9443
  FMIX_CURRENT_SEMAPHORE_SLOT=""
8868
- FMIX_CURRENT_SEMAPHORE_FD=""
9444
+ FMIX_CURRENT_SEMAPHORE_KEEPER_PID=""
8869
9445
  fi
8870
9446
  }
8871
9447
 
@@ -9245,8 +9821,24 @@ fmix_ensure_compile() {
9245
9821
  FMIX_LAST_COMPILE_SECONDS="0"
9246
9822
  FMIX_LAST_COMPILE_LOG=""
9247
9823
 
9248
- # Warm-path short-circuit: cheap mtime/manifest guard before full
9249
- # content fingerprint (which walked deps + lib and cost ~8–10s).
9824
+ if [[ "${FMIX_SOURCE_STABILITY_REQUIRED:-0}" == "1" ]]; then
9825
+ if ! fmix_require_stable_source "$worktree" "compile preparation" 1; then
9826
+ FMIX_LAST_COMPILE_OUTCOME="error"
9827
+ return 1
9828
+ fi
9829
+ if [[ ! "${FMIX_SOURCE_STABILITY_COMPILE_IDENTITY:-}" =~ ^[0-9a-f]{64}$ ]]; then
9830
+ FMIX_SOURCE_STABILITY_COMPILE_IDENTITY="$(
9831
+ fmix_source_stability_compile_identity "$worktree"
9832
+ )" || {
9833
+ fmix_invalidate_rejected_stamps "$worktree"
9834
+ FMIX_LAST_COMPILE_OUTCOME="error"
9835
+ return 1
9836
+ }
9837
+ fi
9838
+ fi
9839
+
9840
+ # Warm-path staged admission: cheap stat/manifest checks reject obvious
9841
+ # drift before bounded live-content and dependency-source verification.
9250
9842
  if fmix_try_compile_stamp_quick_hit "$worktree"; then
9251
9843
  fmix_info "Compile stamp quick-hit — skipping deps.get and mix compile."
9252
9844
  FMIX_LAST_COMPILE_OUTCOME="stamp_hit_quick"
@@ -9340,6 +9932,10 @@ fmix_ensure_compile() {
9340
9932
  compile_timeout="$FMIX_SETUP_COMPILE_TIMEOUT"
9341
9933
  fmix_info "Explicit setup compile budget: ${compile_timeout}s."
9342
9934
  fi
9935
+ if ! fmix_require_stable_source "$worktree" "compile start" 1; then
9936
+ FMIX_LAST_COMPILE_OUTCOME="error"
9937
+ return 1
9938
+ fi
9343
9939
  fmix_run_compile_streamed "$worktree" "$db_name" "" "$log_file" "$compile_timeout"
9344
9940
  rc=$?
9345
9941
  t1=$(fmix_epoch)
@@ -9360,6 +9956,10 @@ fmix_ensure_compile() {
9360
9956
  fmix_error "Fix the source error or explicitly repair the worktree build before retrying."
9361
9957
  return "$rc"
9362
9958
  fi
9959
+ if ! fmix_require_stable_source "$worktree" "compile" 1; then
9960
+ FMIX_LAST_COMPILE_OUTCOME="error"
9961
+ return 1
9962
+ fi
9363
9963
  local compiled_files=0
9364
9964
  compiled_files="$(sed -nE 's/.*Compiling ([0-9]+) files?.*/\1/p' "$log_file" 2>/dev/null \
9365
9965
  | awk '{ total += $1 } END { print total + 0 }')" || compiled_files=0
@@ -9385,18 +9985,29 @@ fmix_ensure_compile() {
9385
9985
  || dependency_fingerprint=""
9386
9986
  if [[ -z "$dependency_fingerprint" ]] \
9387
9987
  || ! fmix_write_dependency_stamp "$worktree" "$dependency_fingerprint"; then
9988
+ fmix_invalidate_rejected_stamps "$worktree"
9388
9989
  fmix_error "Compile succeeded, but final dependency provenance could not be recorded."
9389
9990
  FMIX_LAST_COMPILE_OUTCOME="error"
9390
9991
  return 1
9391
9992
  fi
9993
+ if ! fmix_require_stable_source "$worktree" "final dependency provenance" 1; then
9994
+ FMIX_LAST_COMPILE_OUTCOME="error"
9995
+ return 1
9996
+ fi
9392
9997
 
9393
9998
  fingerprint="$(fmix_compute_compile_fingerprint "$worktree")" || fingerprint=""
9394
9999
  if [[ -z "$fingerprint" ]] \
10000
+ || ! fmix_require_stable_source "$worktree" "compile stamp construction" 1 \
9395
10001
  || ! fmix_write_compile_stamp "$worktree" "$fingerprint"; then
10002
+ fmix_invalidate_rejected_stamps "$worktree"
9396
10003
  fmix_error "Compile succeeded, but final content/config provenance could not be recorded."
9397
10004
  FMIX_LAST_COMPILE_OUTCOME="error"
9398
10005
  return 1
9399
10006
  fi
10007
+ if ! fmix_require_stable_source "$worktree" "compile stamp publication" 1; then
10008
+ FMIX_LAST_COMPILE_OUTCOME="error"
10009
+ return 1
10010
+ fi
9400
10011
  return 0
9401
10012
  }
9402
10013
 
@@ -9578,6 +10189,18 @@ cmd_cache() {
9578
10189
  # ---------------------------------------------------------------------------
9579
10190
 
9580
10191
  cmd_setup() {
10192
+ # Command substitutions inside one setup inherit the parent's `bash fmix
10193
+ # setup ...` argv and can look recursive in a process snapshot, but they do
10194
+ # not call cmd_setup again. A descendant wrapper that actually launches
10195
+ # setup does re-enter this function, so carry a function-scoped sentinel
10196
+ # through the environment and reject that child before any setup work.
10197
+ if [[ "${FMIX_SETUP_ACTIVE:-0}" == "1" ]]; then
10198
+ fmix_error "Recursive fmix setup invocation refused; a setup descendant attempted to re-enter setup."
10199
+ return 2
10200
+ fi
10201
+ local FMIX_SETUP_ACTIVE=1
10202
+ export FMIX_SETUP_ACTIVE
10203
+
9581
10204
  local worktree_arg="auto"
9582
10205
  local session_arg=""
9583
10206
  local rebuild_existing=0
@@ -10023,6 +10646,39 @@ cmd_test() {
10023
10646
  fmix_run_telemetry_phase "lock"
10024
10647
  fmix_emit_phase_change "lock" "completed"
10025
10648
 
10649
+ # Test and coverage runs are immutable snapshots. Capture exact compiler
10650
+ # inputs, HEAD, and selected test content only after the canonical
10651
+ # worktree lock is held. `fmix task` deliberately does not enable this
10652
+ # guard because generators are expected to mutate repository inputs.
10653
+ local FMIX_SOURCE_STABILITY_REQUIRED=1
10654
+ local FMIX_SOURCE_STABILITY_IDENTITY=""
10655
+ local FMIX_SOURCE_STABILITY_COMPILE_IDENTITY=""
10656
+ local -a FMIX_SOURCE_STABILITY_SELECTED_ARGS=("${mix_args[@]+"${mix_args[@]}"}")
10657
+ FMIX_SOURCE_STABILITY_IDENTITY="$(fmix_source_stability_identity \
10658
+ "$real_worktree" \
10659
+ "${FMIX_SOURCE_STABILITY_SELECTED_ARGS[@]+"${FMIX_SOURCE_STABILITY_SELECTED_ARGS[@]}"}")" \
10660
+ || {
10661
+ fmix_error "Could not capture a source-stability identity after acquiring the worktree lock."
10662
+ fmix_invalidate_rejected_stamps "$real_worktree"
10663
+ fmix_lock_release "$lock_name"
10664
+ fmix_run_telemetry_finish 1
10665
+ return 1
10666
+ }
10667
+ FMIX_SOURCE_STABILITY_COMPILE_IDENTITY="$(
10668
+ fmix_source_stability_compile_identity "$real_worktree"
10669
+ )" || {
10670
+ fmix_error "Could not capture compile-source identity after acquiring the worktree lock."
10671
+ fmix_invalidate_rejected_stamps "$real_worktree"
10672
+ fmix_lock_release "$lock_name"
10673
+ fmix_run_telemetry_finish 1
10674
+ return 1
10675
+ }
10676
+ if ! fmix_require_stable_source "$real_worktree" "source identity capture" 1; then
10677
+ fmix_lock_release "$lock_name"
10678
+ fmix_run_telemetry_finish 1
10679
+ return 1
10680
+ fi
10681
+
10026
10682
  # An explicitly selected stable base may seed dependency artifacts. A
10027
10683
  # selector or restore miss is intentionally cold: the unchanged V2
10028
10684
  # artifact path below remains authoritative.
@@ -10070,6 +10726,12 @@ cmd_test() {
10070
10726
  fmix_run_telemetry_finish "$compile_rc"
10071
10727
  return "$compile_rc"
10072
10728
  fi
10729
+ if ! fmix_require_stable_source "$real_worktree" "compilation" 1; then
10730
+ fmix_lock_release "$lock_name"
10731
+ fmix_emit_phase_change "compile" "failed"
10732
+ fmix_run_telemetry_finish 1
10733
+ return 1
10734
+ fi
10073
10735
  fmix_run_telemetry_phase "compile"
10074
10736
  fmix_emit_phase_change "compile" "completed"
10075
10737
 
@@ -10103,8 +10765,6 @@ cmd_test() {
10103
10765
  fmix_run_telemetry_finish 1
10104
10766
  return 1
10105
10767
  }
10106
- fmix_update_template_last_used "$template_key"
10107
-
10108
10768
  # Keep the worktree lock through ExUnit: callers for the same realpath share
10109
10769
  # one scratch DB. Different worktrees remain fully independent.
10110
10770
  fmix_prepend_toolchain_paths
@@ -10174,6 +10834,13 @@ cmd_test() {
10174
10834
  [[ -n "${MIX_BUILD_PATH:-}" ]] && build_env+=("MIX_BUILD_PATH=$MIX_BUILD_PATH")
10175
10835
  [[ -n "${MIX_DEPS_PATH:-}" ]] && build_env+=("MIX_DEPS_PATH=$MIX_DEPS_PATH")
10176
10836
 
10837
+ if ! fmix_require_stable_source "$real_worktree" "Mix startup"; then
10838
+ fmix_lock_release "$lock_name"
10839
+ fmix_emit_phase_change "mix_test" "failed"
10840
+ fmix_run_telemetry_finish 1
10841
+ return 1
10842
+ fi
10843
+
10177
10844
  fmix_emit_phase_change "mix_test" "started"
10178
10845
  local test_rc=0
10179
10846
  (
@@ -10274,6 +10941,12 @@ end
10274
10941
  )
10275
10942
  test_rc=$?
10276
10943
 
10944
+ local source_stable_after_mix=1
10945
+ if ! fmix_require_stable_source "$real_worktree" "Mix execution"; then
10946
+ source_stable_after_mix=0
10947
+ test_rc=1
10948
+ fi
10949
+
10277
10950
  t1=$(fmix_epoch)
10278
10951
  local test_duration_ms=$(( (t1 - t0) * 1000 ))
10279
10952
  (( test_duration_ms < 0 )) && test_duration_ms=0
@@ -10284,9 +10957,13 @@ end
10284
10957
  fmix_emit_phase_change "mix_test" "failed" "$test_duration_ms"
10285
10958
  fi
10286
10959
 
10287
- # Update last_used after test run
10288
- fmix_update_last_used "$state_file"
10289
- fmix_update_template_last_used "$template_key"
10960
+ # A drifted run must not refresh successful-use provenance. Ordinary test
10961
+ # failures retain the historical last-used behavior when source stayed
10962
+ # fixed for the entire run.
10963
+ if (( source_stable_after_mix == 1 )); then
10964
+ fmix_update_last_used "$state_file"
10965
+ fmix_update_template_last_used "$template_key"
10966
+ fi
10290
10967
  fmix_lock_release "$lock_name"
10291
10968
 
10292
10969
  if [[ "$run_mode" == "coverage" ]]; then
@@ -16155,6 +16832,189 @@ cmd_install() {
16155
16832
  # Main dispatch
16156
16833
  # ---------------------------------------------------------------------------
16157
16834
 
16835
+ # Per-verb help: `fmix help <verb>` (or fmix_error hints) prints the focused
16836
+ # usage for one verb plus its examples, without the full --help wall.
16837
+ fmix_verb_help() {
16838
+ local verb="$1"
16839
+ case "$verb" in
16840
+ test)
16841
+ cat <<'VERB'
16842
+ fmix test — run ExUnit targets inside a provisioned session DB.
16843
+
16844
+ Usage:
16845
+ fmix test [--worktree <path>] [--session <id>] [--isolate]
16846
+ [--cache-base <N.N.N-stable>] [--event-fd <fd>] [--unit]
16847
+ -- <mix test args>
16848
+
16849
+ Notes:
16850
+ - One worktree → one warm overlay (SUPER-HOT): pass --session only with a
16851
+ stable name plus --isolate for true parallel Mix on one checkout.
16852
+ - Slow DB startup = local snapshot recovery (see fmix status / fmix timing).
16853
+ - Slow compiles ("Compiling 47xx files") = session fragmentation; inspect
16854
+ with fmix timing --worktree <path>.
16855
+
16856
+ Examples:
16857
+ fmix test -- test/cdrcisco/sip/replaces_test.exs
16858
+ fmix test --worktree ./cisco-cdr -- test/cdrcisco/sip/replaces_test.exs
16859
+ fmix test --worktree ./cisco-cdr --cache-base 0.8.7-stable -- test/cdrcisco/sip/replaces_test.exs
16860
+ VERB
16861
+ ;;
16862
+ coverage)
16863
+ cat <<'VERB'
16864
+ fmix coverage — targeted diff-coverage gate (ADR 0054).
16865
+
16866
+ Usage:
16867
+ fmix coverage --worktree <path> [--session <id>] --base <git-ref>
16868
+ [--source <lib/**/*.ex>]... [--threshold 80]
16869
+ -- <test paths>
16870
+
16871
+ Notes:
16872
+ --base is REQUIRED: it defines the changed-module set. --source scopes
16873
+ cover-compilation to explicit modules (default: every changed lib module).
16874
+ Cannot gate staged-but-uncommitted changes — the bootstrap diffs the
16875
+ committed base...HEAD range. For staged gates use:
16876
+ fmix task -- cdrcisco.diff_coverage --staged --staged-diff <patch>
16877
+
16878
+ Examples:
16879
+ fmix coverage --worktree ./cisco-cdr --base origin/0.8.7-stable -- test/cdrcisco/sip/replaces_test.exs
16880
+ fmix coverage --worktree . --base HEAD --threshold 80 -- test/cdrcisco/call_health/poller_test.exs
16881
+ VERB
16882
+ ;;
16883
+ task)
16884
+ cat <<'VERB'
16885
+ fmix task — run one mix task in a provisioned session DB.
16886
+
16887
+ Usage:
16888
+ fmix task --worktree <path> [--session <id>] [--database <name>]
16889
+ [--mix-env <env>] [--allow-run] -- <mix task> [args]
16890
+
16891
+ Notes:
16892
+ ExUnit tasks are rejected — use fmix test so database and test-lane
16893
+ isolation stay enforced. --allow-run is required for tasks that shell out
16894
+ to git or external processes (e.g. cdrcisco.diff_coverage --staged).
16895
+
16896
+ Examples:
16897
+ fmix task --worktree ./cisco-cdr -- openapi.gen --format all
16898
+ fmix task --worktree ./cisco-cdr --database ct_dev --mix-env dev -- ecto.migrate
16899
+ fmix task --worktree . --mix-env test --allow-run -- cdrcisco.diff_coverage --staged \
16900
+ --staged-diff test-logs/coverage/diff/staged.patch --threshold 80
16901
+ VERB
16902
+ ;;
16903
+ setup)
16904
+ cat <<'VERB'
16905
+ fmix setup — provision the session DB and build artifacts.
16906
+
16907
+ Usage:
16908
+ fmix setup [--worktree <path>] [--session <id>]
16909
+ [--rebuild-db|--cold-db|--from-zero]
16910
+
16911
+ Notes:
16912
+ --rebuild-db reuse snapshots only; never replays structure.sql
16913
+ --cold-db explicit one-time schema cache build
16914
+ --from-zero full migration chain from an empty DB (structure.sql stale)
16915
+
16916
+ Example:
16917
+ fmix setup --worktree ./cisco-cdr
16918
+ VERB
16919
+ ;;
16920
+ seed)
16921
+ cat <<'VERB'
16922
+ fmix seed publish — explicit clean-stable-only maintenance.
16923
+
16924
+ Usage:
16925
+ fmix seed publish --worktree <path> --cache-base <N.N.N-stable>
16926
+
16927
+ Example:
16928
+ fmix seed publish --worktree ./cisco-cdr --cache-base 0.8.7-stable
16929
+ VERB
16930
+ ;;
16931
+ cache)
16932
+ cat <<'VERB'
16933
+ fmix cache — explain or verify the template cache.
16934
+
16935
+ Usage:
16936
+ fmix cache explain --worktree <path> --cache-base <N.N.N-stable> # read-only
16937
+ fmix cache verify [--key <16-hex-template-key>]
16938
+
16939
+ Example:
16940
+ fmix cache explain --worktree ./cisco-cdr --cache-base 0.8.7-stable
16941
+ VERB
16942
+ ;;
16943
+ ensure)
16944
+ cat <<'VERB'
16945
+ fmix ensure — idempotent toolchain + artifact readiness check (no network
16946
+ when stamps are current).
16947
+
16948
+ Example:
16949
+ fmix ensure
16950
+ VERB
16951
+ ;;
16952
+ doctor)
16953
+ cat <<'VERB'
16954
+ fmix doctor — read-only host diagnostic (toolchain, DB, NATS, FMix state).
16955
+
16956
+ Example:
16957
+ fmix doctor
16958
+ VERB
16959
+ ;;
16960
+ status)
16961
+ cat <<'VERB'
16962
+ fmix status — read-only session/artifact/lock diagnostics. Exits 1 when
16963
+ session metadata discovery is incomplete, still printing remaining sections.
16964
+
16965
+ Example:
16966
+ fmix status
16967
+ VERB
16968
+ ;;
16969
+ timing)
16970
+ cat <<'VERB'
16971
+ fmix timing — phase timings + session fragmentation report.
16972
+
16973
+ Usage:
16974
+ fmix timing [--limit N] [--worktree <path>]
16975
+
16976
+ Example:
16977
+ fmix timing --worktree ./cisco-cdr
16978
+ VERB
16979
+ ;;
16980
+ gc)
16981
+ cat <<'VERB'
16982
+ fmix gc — reclaim stale sessions and consolidated templates.
16983
+
16984
+ Usage:
16985
+ fmix gc [--reap|--consolidate] [--worktree <path>] [--yes]
16986
+
16987
+ Notes:
16988
+ Review the fmix status report before --reap --yes.
16989
+
16990
+ Examples:
16991
+ fmix gc
16992
+ fmix gc --consolidate --worktree ./cisco-cdr --yes
16993
+ VERB
16994
+ ;;
16995
+ install)
16996
+ cat <<'VERB'
16997
+ fmix install — immutable runtime snapshot + atomic PATH link (~/.local/bin).
16998
+
16999
+ Example:
17000
+ fmix install
17001
+ VERB
17002
+ ;;
17003
+ self-test)
17004
+ cat <<'VERB'
17005
+ fmix self-test — run the runner's internal correctness suite.
17006
+
17007
+ Example:
17008
+ fmix self-test
17009
+ VERB
17010
+ ;;
17011
+ *)
17012
+ fmix_error "No focused help for '$verb'. Valid commands: test, coverage, task, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, version, help"
17013
+ return 1
17014
+ ;;
17015
+ esac
17016
+ }
17017
+
16158
17018
  fmix_usage() {
16159
17019
  cat <<'USAGE'
16160
17020
  fmix — fast native Elixir test runner for cisco-cdr
@@ -18421,6 +19281,11 @@ main() {
18421
19281
  shift
18422
19282
  case "$cmd" in
18423
19283
  help|--help|-h)
19284
+ # focused help: fmix help <verb> | fmix <verb> --help
19285
+ if [[ $# -ge 1 && "$1" != "--json" ]]; then
19286
+ fmix_verb_help "$1"
19287
+ return 0
19288
+ fi
18424
19289
  fmix_usage
18425
19290
  return 0
18426
19291
  ;;
@@ -18454,6 +19319,15 @@ main() {
18454
19319
  chmod 0700 "$FMIX_HOME" 2>/dev/null || true
18455
19320
  fi
18456
19321
 
19322
+ case "$cmd" in
19323
+ test|coverage|task|setup|seed|cache|ensure|doctor|status|timing|gc|install|self-test)
19324
+ if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
19325
+ fmix_verb_help "$cmd"
19326
+ return 0
19327
+ fi
19328
+ ;;
19329
+ esac
19330
+
18457
19331
  case "$cmd" in
18458
19332
  test) cmd_test "$@" ;;
18459
19333
  coverage) cmd_coverage "$@" ;;
@@ -18470,7 +19344,8 @@ main() {
18470
19344
  self-test) cmd_self_test "$@" ;;
18471
19345
  *)
18472
19346
  fmix_error "Unknown command: $cmd"
18473
- fmix_error "Valid commands: test, coverage, task, setup, seed publish, cache explain, cache verify, doctor, status, timing, gc, install, self-test, version, help"
19347
+ fmix_error "Valid commands: test, coverage, task, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, version, help"
19348
+ fmix_error "Run 'fmix --help' for the full manual or 'fmix help <command>' for focused usage."
18474
19349
  return 1
18475
19350
  ;;
18476
19351
  esac