@calltelemetry/fmix 2.7.16 → 2.7.18

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 +636 -13
  2. package/package.json +3 -3
package/bin/fmix CHANGED
@@ -18,6 +18,8 @@
18
18
  # ./fmix gc [--reap] [--yes]
19
19
  # ./fmix gc --artifacts-only [--json] # bounded read-only inventory; no eviction
20
20
  # ./fmix self-test
21
+ # ./fmix verify-push [--worktree <path>] [--cooldown-minutes <N>] [--require-receipt]
22
+ # ./fmix hook install [--worktree <path>]
21
23
  #
22
24
 
23
25
  set -uo pipefail
@@ -25,7 +27,7 @@ set -uo pipefail
25
27
  # ---------------------------------------------------------------------------
26
28
  # Constants & config
27
29
  # ---------------------------------------------------------------------------
28
- FMIX_VERSION="2.7.16"
30
+ FMIX_VERSION="2.7.18"
29
31
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
30
32
  FMIX_OS="$(uname -s)"
31
33
  FMIX_ARCH="$(uname -m)"
@@ -339,6 +341,9 @@ fmix_readonly_entrypoint() {
339
341
  test|coverage|task|format|setup|seed|cache|ensure|doctor|status|timing|gc|install|self-test)
340
342
  [[ "${2:-}" == "--help" || "${2:-}" == "-h" ]] && return 0
341
343
  ;;
344
+ verify-push|hook)
345
+ [[ "${2:-}" == "--help" || "${2:-}" == "-h" ]] && return 0
346
+ ;;
342
347
  esac
343
348
  return 1
344
349
  }
@@ -8847,9 +8852,14 @@ _fmix_ensure_artifacts_impl() {
8847
8852
  # Need to clone from template. A normal test lane may use an existing cache
8848
8853
  # but must never launch a cold dependency/app build.
8849
8854
  if ! fmix_template_is_complete "$template_key" && [[ "$allow_template_build" != "1" ]]; then
8855
+ if [[ "${FMIX_AUTO_SETUP:-0}" == "1" ]]; then
8856
+ FMIX_LAST_ARTIFACTS_OUTCOME="missing_template"
8857
+ return 1
8858
+ fi
8850
8859
  fmix_error "Artifact template $template_key is missing."
8851
8860
  fmix_error "Refusing a cold dependency/app build in a test lane."
8852
8861
  fmix_error "Build it explicitly with: ./fmix setup --worktree $worktree"
8862
+ fmix_error "DO NOT push untested code to remote CI; run 'fmix setup --worktree $worktree' locally (takes ~30s)."
8853
8863
  FMIX_LAST_ARTIFACTS_OUTCOME="missing_template"
8854
8864
  return 1
8855
8865
  fi
@@ -13787,9 +13797,17 @@ cmd_test() {
13787
13797
  # that don't `use` a DB-capable case).
13788
13798
  local unit_mode=0
13789
13799
  local isolate_mode=0
13800
+ local auto_setup=1
13801
+ if [[ "${FMIX_NO_AUTO_SETUP:-0}" == "1" ]]; then
13802
+ auto_setup=0
13803
+ fi
13790
13804
 
13791
13805
  while (( $# > 0 )); do
13792
13806
  case "$1" in
13807
+ --no-auto-setup)
13808
+ auto_setup=0
13809
+ shift
13810
+ ;;
13793
13811
  --worktree) [[ $# -ge 2 ]] || { fmix_error "--worktree requires a path argument."; return 2; }; worktree_arg="$2"; shift 2 ;;
13794
13812
  --worktree=*) worktree_arg="${1#--worktree=}"; shift ;;
13795
13813
  --cache-base) [[ $# -ge 2 ]] || { fmix_error "--cache-base requires a stable branch."; return 2; }; cache_base="$2"; shift 2 ;;
@@ -14085,14 +14103,50 @@ cmd_test() {
14085
14103
  fi
14086
14104
  fi
14087
14105
  if (( stable_seed_restored == 0 )); then
14088
- fmix_ensure_artifacts "$real_worktree" "$template_key" 0 || {
14089
- local operation_rc=$?
14090
- fmix_command_lock_finish "$lock_name" "$operation_rc" || operation_rc=$?
14091
- FMIX_LAST_ARTIFACTS_OUTCOME="${FMIX_LAST_ARTIFACTS_OUTCOME:-error}"
14092
- fmix_emit_phase_change "artifacts" "failed"
14093
- fmix_run_telemetry_finish "$operation_rc"
14094
- return "$operation_rc"
14095
- }
14106
+ local artifacts_rc=0
14107
+ FMIX_AUTO_SETUP="$auto_setup" fmix_ensure_artifacts "$real_worktree" "$template_key" 0 || artifacts_rc=$?
14108
+ if (( artifacts_rc != 0 )); then
14109
+ if [[ "$FMIX_LAST_ARTIFACTS_OUTCOME" == "missing_template" && "$auto_setup" == "1" ]]; then
14110
+ fmix_info "Artifact template $template_key is missing. Auto-bootstrapping setup (--worktree $real_worktree)..."
14111
+ fmix_command_lock_finish "$lock_name" 0 || true
14112
+ local setup_rc=0
14113
+ local -a auto_setup_cmd=(cmd_setup --worktree "$real_worktree")
14114
+ [[ -n "$session_arg" ]] && auto_setup_cmd+=(--session "$session_arg")
14115
+ "${auto_setup_cmd[@]}" || setup_rc=$?
14116
+ if (( setup_rc != 0 )); then
14117
+ fmix_error "Auto-setup failed (exit $setup_rc)."
14118
+ fmix_error "DO NOT push untested code to remote CI; run 'fmix setup --worktree $real_worktree' locally (takes ~30s)."
14119
+ fmix_emit_phase_change "artifacts" "failed"
14120
+ fmix_run_telemetry_finish "$setup_rc"
14121
+ fmix_write_test_receipt "$real_worktree" "$setup_rc" "$session_arg" "${mix_args[*]:-}"
14122
+ return "$setup_rc"
14123
+ fi
14124
+ fmix_lock_acquire "$lock_name" "$FMIX_WORKTREE_LOCK_TIMEOUT" || {
14125
+ local re_lock_rc=$?
14126
+ fmix_emit_phase_change "artifacts" "failed"
14127
+ fmix_run_telemetry_finish "$re_lock_rc"
14128
+ fmix_write_test_receipt "$real_worktree" "$re_lock_rc" "$session_arg" "${mix_args[*]:-}"
14129
+ return "$re_lock_rc"
14130
+ }
14131
+ FMIX_AUTO_SETUP=0 fmix_ensure_artifacts "$real_worktree" "$template_key" 0 || {
14132
+ local operation_rc=$?
14133
+ fmix_command_lock_finish "$lock_name" "$operation_rc" || operation_rc=$?
14134
+ FMIX_LAST_ARTIFACTS_OUTCOME="${FMIX_LAST_ARTIFACTS_OUTCOME:-error}"
14135
+ fmix_emit_phase_change "artifacts" "failed"
14136
+ fmix_run_telemetry_finish "$operation_rc"
14137
+ fmix_write_test_receipt "$real_worktree" "$operation_rc" "$session_arg" "${mix_args[*]:-}"
14138
+ return "$operation_rc"
14139
+ }
14140
+ else
14141
+ local operation_rc="$artifacts_rc"
14142
+ fmix_command_lock_finish "$lock_name" "$operation_rc" || operation_rc=$?
14143
+ FMIX_LAST_ARTIFACTS_OUTCOME="${FMIX_LAST_ARTIFACTS_OUTCOME:-error}"
14144
+ fmix_emit_phase_change "artifacts" "failed"
14145
+ fmix_run_telemetry_finish "$operation_rc"
14146
+ fmix_write_test_receipt "$real_worktree" "$operation_rc" "$session_arg" "${mix_args[*]:-}"
14147
+ return "$operation_rc"
14148
+ fi
14149
+ fi
14096
14150
  FMIX_LAST_ARTIFACTS_OUTCOME="${FMIX_LAST_ARTIFACTS_OUTCOME:-ready}"
14097
14151
  fi
14098
14152
  fmix_run_telemetry_phase "artifacts"
@@ -14106,6 +14160,7 @@ cmd_test() {
14106
14160
  fmix_command_lock_finish "$lock_name" "$compile_rc" 1 || compile_rc=$?
14107
14161
  fmix_emit_phase_change "compile" "failed"
14108
14162
  fmix_run_telemetry_finish "$compile_rc"
14163
+ fmix_write_test_receipt "$real_worktree" "$compile_rc" "$session_arg" "${mix_args[*]:-}"
14109
14164
  return "$compile_rc"
14110
14165
  fi
14111
14166
  if ! fmix_require_stable_source "$real_worktree" "compilation" 1; then
@@ -14113,6 +14168,7 @@ cmd_test() {
14113
14168
  fmix_command_lock_finish "$lock_name" "$source_rc" || source_rc=$?
14114
14169
  fmix_emit_phase_change "compile" "failed"
14115
14170
  fmix_run_telemetry_finish "$source_rc"
14171
+ fmix_write_test_receipt "$real_worktree" "$source_rc" "$session_arg" "${mix_args[*]:-}"
14116
14172
  return "$source_rc"
14117
14173
  fi
14118
14174
  fmix_run_telemetry_phase "compile"
@@ -14360,6 +14416,7 @@ end
14360
14416
  else
14361
14417
  fmix_info "Test run completed in $(( t1 - t0 ))s (rc=$test_rc)."
14362
14418
  fi
14419
+ fmix_write_test_receipt "$real_worktree" "$test_rc" "$session_arg" "${mix_args[*]:-}"
14363
14420
  fmix_run_telemetry_finish "$test_rc"
14364
14421
  return $test_rc
14365
14422
  }
@@ -14368,6 +14425,414 @@ cmd_coverage() {
14368
14425
  FMIX_INTERNAL_RUN_MODE=coverage cmd_test "$@"
14369
14426
  }
14370
14427
 
14428
+ # ---------------------------------------------------------------------------
14429
+ # Pre-Push Circuit Breaker & Proof-of-Test Receipt
14430
+ # ---------------------------------------------------------------------------
14431
+
14432
+ fmix_git_dir() {
14433
+ local wt="$1"
14434
+ local gd
14435
+ gd="$(git -C "$wt" rev-parse --path-format=absolute --git-dir 2>/dev/null)" || {
14436
+ gd="$(git -C "$wt" rev-parse --git-dir 2>/dev/null)" || return 1
14437
+ gd="$(cd "$wt" && cd "$gd" && pwd -P)" || return 1
14438
+ }
14439
+ printf '%s\n' "$gd"
14440
+ }
14441
+
14442
+ fmix_compute_code_tree_hash() {
14443
+ local wt="$1"
14444
+ local py_bin
14445
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
14446
+ "$py_bin" - "$wt" <<'PY'
14447
+ import hashlib, os, subprocess, sys
14448
+ from pathlib import Path
14449
+
14450
+ root = Path(sys.argv[1]).resolve()
14451
+ tree = {}
14452
+ prefixes = ("lib", "test", "priv", "config", "mix.exs", "mix.lock")
14453
+
14454
+ # 1. Committed blobs from HEAD
14455
+ res = subprocess.run(
14456
+ ["git", "-C", str(root), "-c", "core.quotepath=false", "ls-tree", "-r", "HEAD", "--", *prefixes],
14457
+ capture_output=True, text=True, check=False
14458
+ )
14459
+ if res.returncode == 0:
14460
+ for line in res.stdout.splitlines():
14461
+ if not line:
14462
+ continue
14463
+ parts = line.split(None, 3)
14464
+ if len(parts) >= 4 and parts[1] == "blob":
14465
+ tree[parts[3]] = parts[2]
14466
+
14467
+ # 2. Status changes (modified, added, untracked, deleted)
14468
+ res = subprocess.run(
14469
+ ["git", "-C", str(root), "-c", "core.quotepath=false", "status", "--porcelain", "-uall", "--", *prefixes],
14470
+ capture_output=True, text=True, check=False
14471
+ )
14472
+ if res.returncode == 0:
14473
+ for line in res.stdout.splitlines():
14474
+ if not line:
14475
+ continue
14476
+ rpath = line[3:].strip()
14477
+ if " -> " in rpath:
14478
+ old_p, new_p = rpath.split(" -> ", 1)
14479
+ tree.pop(old_p.strip(), None)
14480
+ rpath = new_p.strip()
14481
+ fpath = root / rpath
14482
+ if fpath.is_file() and not fpath.is_symlink():
14483
+ try:
14484
+ data = fpath.read_bytes()
14485
+ sha = hashlib.sha1(f"blob {len(data)}\0".encode("utf-8") + data).hexdigest()
14486
+ tree[rpath] = sha
14487
+ except OSError:
14488
+ tree.pop(rpath, None)
14489
+ else:
14490
+ tree.pop(rpath, None)
14491
+
14492
+ manifest = "\n".join(f"{p}\t{tree[p]}" for p in sorted(tree.keys())) + "\n"
14493
+ print(hashlib.sha256(manifest.encode("utf-8")).hexdigest())
14494
+ PY
14495
+ }
14496
+
14497
+ fmix_write_test_receipt() {
14498
+ local wt="$1" rc="${2:-0}" session="${3:-}" test_args="${4:-}"
14499
+ local git_dir
14500
+ git_dir="$(fmix_git_dir "$wt" 2>/dev/null)" || return 0
14501
+ [[ -d "$git_dir" ]] || return 0
14502
+
14503
+ local code_tree_hash
14504
+ code_tree_hash="$(fmix_compute_code_tree_hash "$wt" 2>/dev/null)" || code_tree_hash=""
14505
+ [[ -n "$code_tree_hash" ]] || return 0
14506
+
14507
+ local head_sha
14508
+ head_sha="$(git -C "$wt" rev-parse HEAD 2>/dev/null || echo "0000000000000000000000000000000000000000")"
14509
+ local now
14510
+ now="$(fmix_epoch)"
14511
+ local passed="false"
14512
+ (( rc == 0 )) && passed="true"
14513
+
14514
+ local receipt_file="$git_dir/ct-fmix-receipt.json"
14515
+ local tmp_file
14516
+ tmp_file="$(mktemp "${TMPDIR:-/tmp}/ct-fmix-receipt.XXXXXX")" || return 0
14517
+
14518
+ local py_bin
14519
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
14520
+ "$py_bin" - "$tmp_file" "$passed" "$rc" "$now" "$head_sha" "$code_tree_hash" "$session" "$test_args" <<'PY'
14521
+ import json, sys
14522
+ tmp_file, passed, rc, ts, head, code_hash, session, args = sys.argv[1:9]
14523
+ doc = {
14524
+ "schema_version": 1,
14525
+ "passed": passed == "true",
14526
+ "rc": int(rc),
14527
+ "ts_epoch": int(ts),
14528
+ "head_sha": head,
14529
+ "code_tree_hash": code_hash,
14530
+ "session": session,
14531
+ "test_args": args
14532
+ }
14533
+ with open(tmp_file, "w", encoding="utf-8") as f:
14534
+ json.dump(doc, f, indent=2)
14535
+ f.write("\n")
14536
+ PY
14537
+ mv -f "$tmp_file" "$receipt_file" 2>/dev/null || rm -f "$tmp_file"
14538
+ fmix_info "Proof-of-test receipt recorded at $receipt_file (passed=$passed, code_tree_hash=$code_tree_hash)"
14539
+ }
14540
+
14541
+ fmix_record_push_ledger() {
14542
+ local wt="$1" git_dir="$2"
14543
+ local ledger_file="$git_dir/ct-push-ledger.tsv"
14544
+ local branch
14545
+ branch="$(git -C "$wt" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
14546
+ local now
14547
+ now="$(fmix_epoch)"
14548
+ local receipt_file="$git_dir/ct-fmix-receipt.json"
14549
+ local receipt_epoch=0
14550
+ if [[ -f "$receipt_file" ]]; then
14551
+ receipt_epoch="$(grep -o '"ts_epoch":[[:space:]]*[0-9]*' "$receipt_file" 2>/dev/null | awk -F: '{print $2}' | tr -d ' ')"
14552
+ fi
14553
+ receipt_epoch="${receipt_epoch:-0}"
14554
+ printf '%s\t%s\t%s\t%s\n' "$now" "$branch" "pass" "$receipt_epoch" >> "$ledger_file"
14555
+ }
14556
+
14557
+ cmd_verify_push() {
14558
+ local worktree_arg="auto"
14559
+ local cooldown_minutes=30
14560
+ local require_receipt=0
14561
+
14562
+ while (( $# > 0 )); do
14563
+ case "$1" in
14564
+ --worktree)
14565
+ [[ $# -ge 2 ]] || { fmix_error "--worktree requires a path argument."; return 2; }
14566
+ worktree_arg="$2"
14567
+ shift 2
14568
+ ;;
14569
+ --worktree=*)
14570
+ worktree_arg="${1#--worktree=}"
14571
+ shift
14572
+ ;;
14573
+ --cooldown-minutes)
14574
+ [[ $# -ge 2 ]] || { fmix_error "--cooldown-minutes requires an integer."; return 2; }
14575
+ cooldown_minutes="$2"
14576
+ shift 2
14577
+ ;;
14578
+ --cooldown-minutes=*)
14579
+ cooldown_minutes="${1#--cooldown-minutes=}"
14580
+ shift
14581
+ ;;
14582
+ --require-receipt)
14583
+ require_receipt=1
14584
+ shift
14585
+ ;;
14586
+ --help|-h)
14587
+ fmix_verb_help "verify-push"
14588
+ return 0
14589
+ ;;
14590
+ *)
14591
+ # Git pre-push hook passes remote name and url as positional arguments; ignore them
14592
+ shift
14593
+ ;;
14594
+ esac
14595
+ done
14596
+
14597
+ # 1. Human Escape Hatch
14598
+ if [[ "${CT_HUMAN:-0}" == "1" || "${FMIX_BYPASS_PUSH_GATE:-0}" == "1" ]]; then
14599
+ fmix_info "[fmix verify-push] Push allowed: human bypass active (CT_HUMAN=1 or FMIX_BYPASS_PUSH_GATE=1)."
14600
+ return 0
14601
+ fi
14602
+
14603
+ local worktree=""
14604
+ if [[ "$worktree_arg" != "auto" ]]; then
14605
+ worktree="$(fmix_find_worktree "$worktree_arg" 2>/dev/null || fmix_realpath "$worktree_arg")"
14606
+ else
14607
+ worktree="$(fmix_find_worktree auto 2>/dev/null || git rev-parse --show-toplevel 2>/dev/null || pwd -P)"
14608
+ fi
14609
+ local real_worktree
14610
+ real_worktree="$(fmix_realpath "$worktree")"
14611
+
14612
+ if ! git -C "$real_worktree" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
14613
+ fmix_error "Path is not inside a git worktree: $real_worktree"
14614
+ return 1
14615
+ fi
14616
+
14617
+ local git_dir
14618
+ git_dir="$(fmix_git_dir "$real_worktree")" || {
14619
+ fmix_error "Could not resolve git directory for $real_worktree"
14620
+ return 1
14621
+ }
14622
+
14623
+ # 2. Docs / Workflow-Only Fast Pass
14624
+ local base_ref=""
14625
+ if git -C "$real_worktree" rev-parse --verify "@{upstream}" >/dev/null 2>&1; then
14626
+ base_ref="@{upstream}"
14627
+ else
14628
+ local candidate
14629
+ for candidate in origin/0.8.8-stable origin/0.8.7-stable origin/0.8.6-stable origin/main main; do
14630
+ if git -C "$real_worktree" rev-parse --verify "$candidate" >/dev/null 2>&1; then
14631
+ base_ref="$candidate"
14632
+ break
14633
+ fi
14634
+ done
14635
+ fi
14636
+
14637
+ if [[ -n "$base_ref" ]]; then
14638
+ local diff_files
14639
+ diff_files="$(git -C "$real_worktree" diff --name-only "$base_ref"...HEAD 2>/dev/null || true)"
14640
+ if [[ -n "$diff_files" ]]; then
14641
+ local elixir_code_touched=0
14642
+ local f
14643
+ while IFS= read -r f; do
14644
+ [[ -z "$f" ]] && continue
14645
+ case "$f" in
14646
+ lib/*|test/*|priv/*|mix.exs|mix.lock|config/*)
14647
+ elixir_code_touched=1
14648
+ break
14649
+ ;;
14650
+ esac
14651
+ done <<< "$diff_files"
14652
+ if (( elixir_code_touched == 0 )); then
14653
+ fmix_info "[fmix verify-push] Push allowed: only non-Elixir files modified (docs/workflows)."
14654
+ return 0
14655
+ fi
14656
+ fi
14657
+ fi
14658
+
14659
+ # 3. Draft PR Fast Pass (unless --require-receipt is explicitly specified)
14660
+ local pr_status=""
14661
+ if (( require_receipt == 0 )); then
14662
+ local py_bin
14663
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
14664
+ pr_status="$("$py_bin" - "$real_worktree" <<'PY'
14665
+ import json, subprocess, sys
14666
+
14667
+ wt = sys.argv[1]
14668
+ try:
14669
+ res = subprocess.run(
14670
+ ["gh", "pr", "view", "--json", "isDraft,state"],
14671
+ cwd=wt,
14672
+ capture_output=True,
14673
+ text=True,
14674
+ timeout=4.0
14675
+ )
14676
+ if res.returncode != 0:
14677
+ # No open PR found or gh error
14678
+ print("no_open_pr")
14679
+ sys.exit(0)
14680
+ data = json.loads(res.stdout)
14681
+ if data.get("state") != "OPEN":
14682
+ print("no_open_pr")
14683
+ elif data.get("isDraft") is True:
14684
+ print("draft_pr")
14685
+ else:
14686
+ print("non_draft_pr")
14687
+ except subprocess.TimeoutExpired:
14688
+ print("timeout")
14689
+ except Exception:
14690
+ print("error")
14691
+ PY
14692
+ )"
14693
+ case "$pr_status" in
14694
+ no_open_pr|draft_pr|timeout|error)
14695
+ fmix_info "[fmix verify-push] Push allowed: no open ready PR ($pr_status). Encouraging Draft-First PR workflow."
14696
+ fmix_record_push_ledger "$real_worktree" "$git_dir"
14697
+ return 0
14698
+ ;;
14699
+ esac
14700
+ fi
14701
+
14702
+ # 4. Proof-of-Local-Test Gate (Open Non-Draft PR or --require-receipt)
14703
+ local receipt_file="$git_dir/ct-fmix-receipt.json"
14704
+ local current_code_tree_hash
14705
+ current_code_tree_hash="$(fmix_compute_code_tree_hash "$real_worktree" 2>/dev/null)" || current_code_tree_hash=""
14706
+
14707
+ local receipt_valid=0
14708
+ local receipt_passed=0
14709
+ local receipt_hash=""
14710
+ local receipt_epoch=0
14711
+
14712
+ if [[ -f "$receipt_file" ]]; then
14713
+ local py_bin
14714
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
14715
+ local receipt_info
14716
+ receipt_info="$("$py_bin" - "$receipt_file" <<'PY'
14717
+ import json, sys
14718
+ try:
14719
+ with open(sys.argv[1], "r", encoding="utf-8") as f:
14720
+ d = json.load(f)
14721
+ passed = "1" if d.get("passed") is True else "0"
14722
+ h = str(d.get("code_tree_hash", ""))
14723
+ ts = str(d.get("ts_epoch", 0))
14724
+ print(f"{passed}\t{h}\t{ts}")
14725
+ except Exception:
14726
+ print("0\t\t0")
14727
+ PY
14728
+ )"
14729
+ IFS=$'\t' read -r receipt_passed receipt_hash receipt_epoch <<< "$receipt_info"
14730
+ if [[ "$receipt_passed" == "1" && "$receipt_hash" == "$current_code_tree_hash" && -n "$current_code_tree_hash" ]]; then
14731
+ receipt_valid=1
14732
+ fi
14733
+ fi
14734
+
14735
+ if (( receipt_valid == 0 )); then
14736
+ fmix_error "[fmix CIRCUIT BREAKER] Refusing push to open non-draft PR without a passing local fmix test receipt for the current code state. Run 'fmix test -- <test_files>' locally first, OR convert the PR to draft ('gh pr ready --undo') before pushing WIP commits."
14737
+ return 1
14738
+ fi
14739
+
14740
+ # 5. 30-Minute Push Rate-Limiter (<git-dir>/ct-push-ledger.tsv)
14741
+ local ledger_file="$git_dir/ct-push-ledger.tsv"
14742
+ local branch
14743
+ branch="$(git -C "$real_worktree" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
14744
+ local now
14745
+ now="$(fmix_epoch)"
14746
+ local window_seconds=$(( cooldown_minutes * 60 ))
14747
+ local cutoff=$(( now - window_seconds ))
14748
+
14749
+ local recent_pushes=0
14750
+ local last_push_epoch=0
14751
+
14752
+ if [[ -f "$ledger_file" ]]; then
14753
+ while IFS=$'\t' read -r p_ts p_branch p_hash p_rec_ts; do
14754
+ [[ -z "$p_ts" || "$p_branch" != "$branch" ]] && continue
14755
+ if (( p_ts >= cutoff )); then
14756
+ recent_pushes=$(( recent_pushes + 1 ))
14757
+ fi
14758
+ if (( p_ts > last_push_epoch )); then
14759
+ last_push_epoch="$p_ts"
14760
+ fi
14761
+ done < "$ledger_file"
14762
+ fi
14763
+
14764
+ if (( recent_pushes >= 2 && receipt_epoch <= last_push_epoch )); then
14765
+ fmix_error "[fmix CIRCUIT BREAKER] Push rate limit exceeded: $recent_pushes pushes in the last ${cooldown_minutes}m without a new passing test receipt recorded since the last push ($last_push_epoch). Run 'fmix test' locally before pushing again."
14766
+ return 1
14767
+ fi
14768
+
14769
+ printf '%s\t%s\t%s\t%s\n' "$now" "$branch" "$current_code_tree_hash" "$receipt_epoch" >> "$ledger_file"
14770
+ fmix_info "[fmix verify-push] Push verified and allowed."
14771
+ return 0
14772
+ }
14773
+
14774
+ cmd_hook_install() {
14775
+ local worktree_arg="auto"
14776
+ while (( $# > 0 )); do
14777
+ case "$1" in
14778
+ --worktree) [[ $# -ge 2 ]] || { fmix_error "--worktree requires a path argument."; return 2; }; worktree_arg="$2"; shift 2 ;;
14779
+ --worktree=*) worktree_arg="${1#--worktree=}"; shift ;;
14780
+ *) fmix_error "Unknown option: $1. Usage: fmix hook install [--worktree <path>]"; return 1 ;;
14781
+ esac
14782
+ done
14783
+ local worktree=""
14784
+ if [[ "$worktree_arg" != "auto" ]]; then
14785
+ worktree="$(fmix_find_worktree "$worktree_arg" 2>/dev/null || fmix_realpath "$worktree_arg")"
14786
+ else
14787
+ worktree="$(fmix_find_worktree auto 2>/dev/null || git rev-parse --show-toplevel 2>/dev/null || pwd -P)"
14788
+ fi
14789
+ local real_worktree
14790
+ real_worktree="$(fmix_realpath "$worktree")"
14791
+ if ! git -C "$real_worktree" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
14792
+ fmix_error "Path is not inside a git worktree: $real_worktree"
14793
+ return 1
14794
+ fi
14795
+ local git_dir
14796
+ git_dir="$(fmix_git_dir "$real_worktree")" || {
14797
+ fmix_error "Could not resolve git directory for $real_worktree"
14798
+ return 1
14799
+ }
14800
+ local hooks_dir="$git_dir/hooks"
14801
+ mkdir -p "$hooks_dir" || return 1
14802
+ local hook_file="$hooks_dir/pre-push"
14803
+ if [[ -f "$hook_file" ]]; then
14804
+ if grep -q "fmix verify-push" "$hook_file"; then
14805
+ fmix_info "fmix pre-push hook already installed in $hook_file"
14806
+ return 0
14807
+ fi
14808
+ printf '\n# fmix pre-push circuit breaker\nfmix verify-push --worktree "$PWD" "$@"\n' >> "$hook_file"
14809
+ else
14810
+ cat > "$hook_file" <<'EOF'
14811
+ #!/usr/bin/env bash
14812
+ # Installed by fmix hook install
14813
+ set -e
14814
+ fmix verify-push --worktree "$PWD" "$@"
14815
+ EOF
14816
+ chmod +x "$hook_file"
14817
+ fi
14818
+ fmix_info "Installed pre-push hook in $hook_file"
14819
+ return 0
14820
+ }
14821
+
14822
+ cmd_hook() {
14823
+ local subcmd="${1:-}"
14824
+ shift || true
14825
+ case "$subcmd" in
14826
+ install)
14827
+ cmd_hook_install "$@"
14828
+ ;;
14829
+ *)
14830
+ fmix_error "Unknown hook subcommand: '${subcmd:-empty}'. Usage: fmix hook install [--worktree <path>]"
14831
+ return 1
14832
+ ;;
14833
+ esac
14834
+ }
14835
+
14371
14836
  # ---------------------------------------------------------------------------
14372
14837
  # cmd: fmix task
14373
14838
  # ---------------------------------------------------------------------------
@@ -14822,6 +15287,102 @@ fmix_wait_infra() {
14822
15287
  return 1
14823
15288
  }
14824
15289
 
15290
+ fmix_infra_capacity_bytes() {
15291
+ local py_bin
15292
+ py_bin="$(fmix_python_bin)" || return 1
15293
+ "$py_bin" - <<'PY'
15294
+ import json
15295
+ import os
15296
+ import platform
15297
+ import subprocess
15298
+ import sys
15299
+
15300
+ def probe(*args):
15301
+ result = subprocess.run(args, capture_output=True, text=True, timeout=5)
15302
+ if result.returncode or len(result.stdout) > 1024 * 1024:
15303
+ raise ValueError("incomplete native observation")
15304
+ return result.stdout.strip()
15305
+
15306
+ try:
15307
+ # DOCKER_CONTEXT takes precedence over DOCKER_HOST. Never use a local
15308
+ # capacity reading to admit allocation on an unobserved remote daemon.
15309
+ context = os.environ.get("DOCKER_CONTEXT", "")
15310
+ host = os.environ.get("DOCKER_HOST", "")
15311
+ if context:
15312
+ if context.startswith("-"):
15313
+ raise ValueError("invalid context")
15314
+ host = json.loads(probe("docker", "context", "inspect", "--format",
15315
+ "{{json .Endpoints.docker.Host}}", context))
15316
+ elif not host:
15317
+ host = json.loads(probe("docker", "context", "inspect", "--format",
15318
+ "{{json .Endpoints.docker.Host}}"))
15319
+ if not isinstance(host, str) or not host.startswith("unix:///"):
15320
+ raise ValueError("unobserved remote storage")
15321
+
15322
+ if platform.system() == "Darwin":
15323
+ if not host.endswith("/.orbstack/run/docker.sock"):
15324
+ raise ValueError("unobserved macOS Docker storage")
15325
+ # Discover the *open* backing image rather than assuming its default
15326
+ # path: OrbStack can place data on a different filesystem. Only read
15327
+ # native descriptor metadata; do not traverse protected app storage.
15328
+ raw = probe("lsof", "-nP", "-a", "-u", str(os.getuid()),
15329
+ "-c", "OrbStack", "-Fn")
15330
+ paths = {line[1:] for line in raw.splitlines()
15331
+ if line.startswith("n/") and line.endswith("/data.img.raw")}
15332
+ if len(paths) != 1:
15333
+ raise ValueError("ambiguous or unavailable Docker backing storage")
15334
+ anchor = paths.pop()
15335
+ elif platform.system() == "Linux":
15336
+ anchor = json.loads(probe("docker", "info", "--format", "{{json .DockerRootDir}}"))
15337
+ if not isinstance(anchor, str) or not os.path.isabs(anchor):
15338
+ raise ValueError("unavailable Docker storage root")
15339
+ else:
15340
+ raise ValueError("unsupported native storage observation")
15341
+ before = os.stat(anchor)
15342
+ capacity = os.statvfs(anchor)
15343
+ after = os.stat(anchor)
15344
+ if (before.st_dev, before.st_ino) != (after.st_dev, after.st_ino):
15345
+ raise ValueError("storage identity changed")
15346
+ free = capacity.f_bavail * capacity.f_frsize
15347
+ total = capacity.f_blocks * capacity.f_frsize
15348
+ if not 0 <= free <= total < 10**18 or total == 0:
15349
+ raise ValueError("invalid native capacity")
15350
+ print(f"{free} {total}")
15351
+ except Exception:
15352
+ print("[fmix] Docker storage capacity unavailable; refusing cold infrastructure allocation.",
15353
+ file=sys.stderr)
15354
+ sys.exit(1)
15355
+ PY
15356
+ }
15357
+
15358
+ fmix_infra_has_headroom() {
15359
+ # An 8 decimal GB cold-start allowance covers the observed post-reset
15360
+ # image/cache refill. This is admission, not an OS quota or a reservation
15361
+ # against nonparticipating Docker clients. No public bypass/override.
15362
+ local capacity free total floor reserve=8000000000
15363
+ capacity="$(fmix_infra_capacity_bytes)" || {
15364
+ fmix_error "Docker disk capacity is unknown; holding infrastructure startup."
15365
+ return 75
15366
+ }
15367
+ if [[ ! "$capacity" =~ ^[0-9]{1,18}[[:blank:]][0-9]{1,18}$ ]]; then
15368
+ fmix_error "Invalid Docker disk capacity; holding infrastructure startup."
15369
+ return 75
15370
+ fi
15371
+ read -r free total <<<"$capacity"
15372
+ free=$((10#$free))
15373
+ total=$((10#$total))
15374
+ if (( total <= 0 || free > total )); then
15375
+ fmix_error "Invalid Docker disk capacity; holding infrastructure startup."
15376
+ return 75
15377
+ fi
15378
+ floor=$(( total / 20 ))
15379
+ (( floor <= 100000000000 )) || floor=100000000000
15380
+ if (( free < reserve || free - reserve < floor )); then
15381
+ fmix_error "Docker disk pressure: free=${free}B, startup allowance=${reserve}B, retained floor=${floor}B. Holding startup; reuse ready infrastructure or reclaim through its owner."
15382
+ return 75
15383
+ fi
15384
+ }
15385
+
14825
15386
  fmix_ensure_infra() {
14826
15387
  if fmix_infra_ready; then
14827
15388
  echo " [OK] infra ready postgres $DB_HOST:$DB_PORT nats $NATS_HOST:$NATS_PORT"
@@ -14848,12 +15409,32 @@ fmix_ensure_infra() {
14848
15409
  return 1
14849
15410
  fi
14850
15411
 
15412
+ # Coalesce concurrent callers after an engine reset. The native lease is
15413
+ # held through readiness and admission; never force another owner's lock.
15414
+ local lock_name="infra-ensure" startup_rc=0
15415
+ fmix_lock_acquire "$lock_name" "$FMIX_ENSURE_TIMEOUT" "shared infrastructure startup" || return 1
15416
+ fmix_ensure_infra_locked || startup_rc=$?
15417
+ if ! fmix_lock_release "$lock_name"; then
15418
+ fmix_error "Infrastructure startup lease release failed."
15419
+ return 1
15420
+ fi
15421
+ return "$startup_rc"
15422
+ }
15423
+
15424
+ fmix_ensure_infra_locked() {
15425
+ if fmix_infra_ready; then
15426
+ echo " [OK] infra ready postgres $DB_HOST:$DB_PORT nats $NATS_HOST:$NATS_PORT"
15427
+ return 0
15428
+ fi
15429
+ fmix_infra_has_headroom || return 75
14851
15430
  echo " starting shared $FMIX_INFRA_PROJECT (postgres + nats)..."
14852
15431
  fmix_start_existing_ct_infra || true
14853
15432
  if fmix_wait_infra; then
14854
15433
  echo " [OK] infra ready postgres $DB_HOST:$DB_PORT nats $NATS_HOST:$NATS_PORT"
14855
15434
  return 0
14856
15435
  fi
15436
+ # Another producer may allocate while existing services are warming.
15437
+ fmix_infra_has_headroom || return 75
14857
15438
  if ! fmix_compose_up_ensure; then
14858
15439
  fmix_error "docker compose could not start $FMIX_INFRA_PROJECT postgres/nats"
14859
15440
  return 1
@@ -22787,10 +23368,36 @@ fmix self-test — run the runner's internal correctness suite.
22787
23368
 
22788
23369
  Example:
22789
23370
  fmix self-test
23371
+ VERB
23372
+ ;;
23373
+ verify-push)
23374
+ cat <<'VERB'
23375
+ fmix verify-push — pre-push circuit breaker enforcing local test proof and push rate-limiting.
23376
+
23377
+ Usage:
23378
+ fmix verify-push [--worktree <path>] [--cooldown-minutes <N>] [--require-receipt]
23379
+
23380
+ Notes:
23381
+ - Bypassed if CT_HUMAN=1 or FMIX_BYPASS_PUSH_GATE=1.
23382
+ - Bypassed for docs/workflow-only diffs (no Elixir files modified).
23383
+ - Bypassed for draft PRs or branches without an open PR.
23384
+ - Enforces ct-fmix-receipt.json match for open non-draft PRs.
23385
+ - Limits push frequency to max 2 pushes per cooldown window without a new passing test receipt.
23386
+ VERB
23387
+ ;;
23388
+ hook)
23389
+ cat <<'VERB'
23390
+ fmix hook — manage git hooks for fmix.
23391
+
23392
+ Usage:
23393
+ fmix hook install [--worktree <path>]
23394
+
23395
+ Notes:
23396
+ Installs a git pre-push hook invoking fmix verify-push.
22790
23397
  VERB
22791
23398
  ;;
22792
23399
  *)
22793
- fmix_error "No focused help for '$verb'. Valid commands: test, coverage, task, format, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, version, help"
23400
+ fmix_error "No focused help for '$verb'. Valid commands: test, coverage, task, format, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, verify-push, hook, version, help"
22794
23401
  return 1
22795
23402
  ;;
22796
23403
  esac
@@ -22801,7 +23408,7 @@ fmix_usage() {
22801
23408
  fmix — fast native Elixir test runner for cisco-cdr
22802
23409
 
22803
23410
  Usage:
22804
- ./fmix test [--worktree <path>] [--session <id>] [--cache-base <N.N.N-stable>] [--event-fd <fd>] [--unit] -- <mix test args>
23411
+ ./fmix test [--worktree <path>] [--session <id>] [--no-auto-setup] [--cache-base <N.N.N-stable>] [--event-fd <fd>] [--unit] -- <mix test args>
22805
23412
  ./fmix coverage [--worktree <path>] [--session <id>] --base <git-ref> [--source <lib/**/*.ex>] [--threshold 80] -- <test paths>
22806
23413
  ./fmix task [--worktree <path>] [--session <id>] [--database <name>] [--mix-env <env>] [--allow-run] -- <mix task> [args]
22807
23414
  ./fmix format [--worktree <path>] [--session <id>] [--mix-env <env>] [--dot-formatter <path>] [--check] -- <files...>
@@ -22809,6 +23416,8 @@ Usage:
22809
23416
  ./fmix seed publish --worktree <path> [--session <id>] --cache-base <N.N.N-stable>
22810
23417
  ./fmix cache explain --worktree <path> --cache-base <N.N.N-stable>
22811
23418
  ./fmix cache verify [--key <16-hex-template-key>]
23419
+ ./fmix verify-push [--worktree <path>] [--cooldown-minutes <N>] [--require-receipt]
23420
+ ./fmix hook install [--worktree <path>]
22812
23421
  ./fmix ensure
22813
23422
  ./fmix doctor
22814
23423
  ./fmix status
@@ -25104,6 +25713,12 @@ main() {
25104
25713
  return 0
25105
25714
  fi
25106
25715
  ;;
25716
+ verify-push|hook)
25717
+ if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
25718
+ fmix_verb_help "$cmd"
25719
+ return 0
25720
+ fi
25721
+ ;;
25107
25722
  esac
25108
25723
  if [[ "$cmd" == "gc" ]] && fmix_readonly_entrypoint gc "$@"; then
25109
25724
  if fmix_gc_artifact_retirement_requested "$@"; then
@@ -25116,7 +25731,7 @@ main() {
25116
25731
 
25117
25732
  if [[ "$cmd" == "cache" ]]; then
25118
25733
  fmix_prepare_home_readonly || return 1
25119
- elif [[ "$cmd" != "test" && "$cmd" != "coverage" ]]; then
25734
+ elif [[ "$cmd" != "test" && "$cmd" != "coverage" && "$cmd" != "verify-push" && "$cmd" != "hook" ]]; then
25120
25735
  fmix_prepare_command_home || return 1
25121
25736
  fi
25122
25737
 
@@ -25127,6 +25742,12 @@ main() {
25127
25742
  return 0
25128
25743
  fi
25129
25744
  ;;
25745
+ verify-push|hook)
25746
+ if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
25747
+ fmix_verb_help "$cmd"
25748
+ return 0
25749
+ fi
25750
+ ;;
25130
25751
  esac
25131
25752
 
25132
25753
  case "$cmd" in
@@ -25144,9 +25765,11 @@ main() {
25144
25765
  gc) cmd_gc "$@" ;;
25145
25766
  install) cmd_install "$@" ;;
25146
25767
  self-test) cmd_self_test "$@" ;;
25768
+ verify-push) cmd_verify_push "$@" ;;
25769
+ hook) cmd_hook "$@" ;;
25147
25770
  *)
25148
25771
  fmix_error "Unknown command: $cmd"
25149
- fmix_error "Valid commands: test, coverage, task, format, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, version, help"
25772
+ fmix_error "Valid commands: test, coverage, task, format, setup, seed publish, cache explain, cache verify, ensure, doctor, status, timing, gc, install, self-test, verify-push, hook, version, help"
25150
25773
  fmix_error "Run 'fmix --help' for the full manual or 'fmix help <command>' for focused usage."
25151
25774
  return 1
25152
25775
  ;;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calltelemetry/fmix",
3
- "version": "2.7.16",
3
+ "version": "2.7.18",
4
4
  "description": "Verified native FMix runner for CallTelemetry workspaces",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -26,7 +26,7 @@
26
26
  "directory": "packages/npm"
27
27
  },
28
28
  "fmix": {
29
- "runnerSha256": "3c4fbbb7a154db21a68c62906a7e852a98faea0956259feee4b4329fd6f60402",
30
- "sourceTag": "fmix-v2.7.16"
29
+ "runnerSha256": "e7f083173a92e06695c0b3c17e7feec773ccd70eca0000e1d445a6f29af06d57",
30
+ "sourceTag": "fmix-v2.7.18"
31
31
  }
32
32
  }