@calltelemetry/fmix 2.7.27 → 2.7.29

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 +370 -86
  2. package/package.json +3 -3
package/bin/fmix CHANGED
@@ -28,7 +28,7 @@ set -uo pipefail
28
28
  # ---------------------------------------------------------------------------
29
29
  # Constants & config
30
30
  # ---------------------------------------------------------------------------
31
- FMIX_VERSION="2.7.27"
31
+ FMIX_VERSION="2.7.29"
32
32
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
33
33
  FMIX_OS="$(uname -s)"
34
34
  FMIX_ARCH="$(uname -m)"
@@ -157,6 +157,9 @@ FMIX_PENDING_LOCK_KEEPER_STATE=""
157
157
  # was retired: "bootstrap_deadline" (host load; retryable) or "bootstrap_failed"
158
158
  # (genuine; fail fast). Callers branch on this, never on the message text.
159
159
  FMIX_LAST_LOCK_KEEPER_FAILURE=""
160
+ # The typed failure the last keeper start observed, kept even when retirement
161
+ # failed. A permit loop reads it to make a deadline terminal, never retried.
162
+ FMIX_LAST_LOCK_KEEPER_OBSERVED=""
160
163
 
161
164
  # Set by fmix_activate_session before any mutable artifact, stamp, or scratch
162
165
  # database path is resolved.
@@ -557,6 +560,126 @@ fmix_readonly_entrypoint() {
557
560
  return 1
558
561
  }
559
562
 
563
+ fmix_artifact_v2_managed_entrypoint() {
564
+ # Optional arguments judge another launcher: entrypoint, runner, FMIX_HOME.
565
+ local py_bin entrypoint="${1:-$0}" runner="${2:-}" managed_home="${3:-$FMIX_HOME}"
566
+ if [[ "${FMIX_SELF_TEST_MANAGED_REEXEC:-0}" == "1" \
567
+ && " ${FUNCNAME[*]} " == *" cmd_self_test "* \
568
+ && -n "${FMIX_SELF_TEST_RUNNER_HOME:-}" ]]; then
569
+ managed_home="$FMIX_SELF_TEST_RUNNER_HOME"
570
+ fi
571
+ py_bin="$(fmix_python_bin)" || return 1
572
+ if [[ -z "$runner" ]]; then
573
+ runner="$(fmix_realpath "${BASH_SOURCE[0]}")" || return 1
574
+ fi
575
+ "$py_bin" -I -B - "$HOME" "$managed_home" "$entrypoint" "$runner" <<'PY'
576
+ import hashlib,os,stat,sys
577
+ user_home,fmix_home,entrypoint,runner=sys.argv[1:]
578
+ uid=os.getuid(); euid=os.geteuid(); user_home=os.path.abspath(user_home); fmix_home=os.path.abspath(fmix_home)
579
+ def reject(reason):
580
+ print(reason,file=sys.stderr)
581
+ raise SystemExit(1)
582
+ if uid==0 or euid!=uid: reject("privileged_cleaner_forbidden")
583
+ entrypoint=os.path.abspath(entrypoint)
584
+ if os.path.realpath(user_home)!=user_home or os.path.realpath(fmix_home)!=fmix_home:
585
+ reject("noncanonical_managed_home")
586
+ known={user_home+"/.local/bin/fmix",user_home+"/.local/share/fmix/bin/fmix"}
587
+ custom=os.environ.get("FMIX_INSTALL_DIR")
588
+ if custom:
589
+ custom=os.path.abspath(custom)
590
+ if os.path.commonpath((user_home,custom))!=user_home: raise SystemExit(1)
591
+ known.add(os.path.join(custom,"fmix"))
592
+ if entrypoint not in known and entrypoint!=runner: reject("unrecognized_managed_entrypoint")
593
+ paths=[]
594
+ for path in sorted(known):
595
+ if not os.path.lexists(path): continue
596
+ parent=os.path.dirname(path)
597
+ if os.path.commonpath((user_home,parent))!=user_home: raise SystemExit(1)
598
+ rel=os.path.relpath(parent,user_home); current=user_home
599
+ for part in (() if rel=="." else rel.split(os.sep)):
600
+ current=os.path.join(current,part); s=os.lstat(current)
601
+ if not stat.S_ISDIR(s.st_mode) or stat.S_ISLNK(s.st_mode) or s.st_uid!=uid or s.st_mode&0o022:
602
+ reject("unsafe_launcher_parent")
603
+ link=os.lstat(path)
604
+ if not stat.S_ISLNK(link.st_mode) or link.st_uid!=uid or link.st_nlink!=1 or os.path.realpath(path)!=runner:
605
+ reject("managed_launcher_mismatch")
606
+ paths.append(path)
607
+ if entrypoint not in paths and (entrypoint!=runner or not paths):
608
+ reject("managed_launcher_missing")
609
+ fd=os.open(runner,os.O_RDONLY|getattr(os,"O_NOFOLLOW",0))
610
+ try:
611
+ s=os.fstat(fd)
612
+ if not stat.S_ISREG(s.st_mode) or s.st_uid!=uid or s.st_nlink!=1 or s.st_mode&0o222 or not s.st_mode&0o111 or s.st_size>4*1024*1024:
613
+ reject("unsafe_runner_identity")
614
+ parent=os.open(fmix_home+"/runtime",os.O_RDONLY|os.O_DIRECTORY|getattr(os,"O_NOFOLLOW",0))
615
+ try:
616
+ runtime=os.fstat(parent)
617
+ if runtime.st_uid!=uid or runtime.st_dev!=s.st_dev or runtime.st_mode&0o022:
618
+ reject("unsafe_runner_runtime")
619
+ finally: os.close(parent)
620
+ h=hashlib.sha256()
621
+ for block in iter(lambda: os.read(fd,65536),b""): h.update(block)
622
+ if runner!=fmix_home+"/runtime/fmix-"+h.hexdigest():
623
+ reject("managed_launcher_runner_mismatch")
624
+ finally: os.close(fd)
625
+ PY
626
+ }
627
+
628
+ # REL-5471: a package-manager copy of this runner (an npm Node prefix, which is
629
+ # what installers maintain and shells resolve) is never a publisher, because the
630
+ # predicate above accepts only the managed launcher. When a managed launcher
631
+ # holds byte-identical runner bytes and passes that same predicate, run through
632
+ # it. This grants the copy nothing: the process that continues is the managed
633
+ # one, and every publication still runs the predicate. A copy with any other
634
+ # bytes stays where it is and stays refused.
635
+ fmix_managed_reexec_target() {
636
+ local self="" runtime_home="" candidate target
637
+ [[ -n "${HOME:-}" ]] || return 1
638
+ for candidate in ${FMIX_INSTALL_DIR:+"$FMIX_INSTALL_DIR/fmix"} \
639
+ "$HOME/.local/bin/fmix" "$HOME/.local/share/fmix/bin/fmix"; do
640
+ [[ -L "$candidate" ]] || continue
641
+ if [[ -z "$self" ]]; then
642
+ self="$(fmix_realpath "${BASH_SOURCE[0]}" 2>/dev/null)" || return 1
643
+ runtime_home="$(fmix_realpath "$FMIX_HOME" 2>/dev/null)" || return 1
644
+ # Already running a runtime snapshot: nothing to hand over.
645
+ [[ "$self" != "$runtime_home"/runtime/fmix-* ]] || return 1
646
+ # fmix_private_env_file still reads a legacy .env beside the invoked
647
+ # script. Handing over would silently drop it.
648
+ [[ -n "${FMIX_ENV_FILE:-}" || -f "$FMIX_CONFIG_HOME/fmix.env" || ! -f "${self%/*}/.env" ]] \
649
+ || return 1
650
+ fi
651
+ target="$(fmix_realpath "$candidate" 2>/dev/null)" || continue
652
+ [[ "$target" == "$runtime_home"/runtime/fmix-* ]] || continue
653
+ cmp -s "$self" "$target" || continue
654
+ fmix_artifact_v2_managed_entrypoint "$candidate" "$target" "$runtime_home" \
655
+ >/dev/null 2>&1 || continue
656
+ # The snapshot, not the link: a concurrent install may repoint the link,
657
+ # and only the bytes compared above may run.
658
+ printf '%s\n' "$target"
659
+ return 0
660
+ done
661
+ return 1
662
+ }
663
+
664
+ fmix_reexec_through_managed_launcher() {
665
+ # Identity probes, install and self-test describe the invoked copy. hook and
666
+ # runner publish nothing and read files next to the invoked script.
667
+ case "${1:-}" in
668
+ ""|help|--help|-h|version|--version|install|self-test|hook|runner|--fmix-observability-worker) return 0 ;;
669
+ esac
670
+ [[ "${FMIX_OBSERVABILITY_WORKER:-0}" != "1" ]] || return 0
671
+ local launcher
672
+ launcher="$(fmix_managed_reexec_target)" || return 0
673
+ # A package copy never auto-upgraded. Keep it so: a detached upgrade would
674
+ # repoint the launcher away from the bytes the package manager installed.
675
+ export FMIX_NO_AUTO_UPGRADE="${FMIX_NO_AUTO_UPGRADE:-1}"
676
+ exec "$launcher" "$@"
677
+ }
678
+
679
+ if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
680
+ fmix_reexec_through_managed_launcher "$@"
681
+ fi
682
+
560
683
  if ! fmix_readonly_entrypoint "$@" && [[ "${BASH_SOURCE[0]}" == "$0" \
561
684
  && "${FMIX_OBSERVABILITY_WORKER:-0}" != "1" \
562
685
  && "${1:-}" != "--fmix-observability-worker" \
@@ -3897,67 +4020,6 @@ fmix_dependency_lane_fingerprint() {
3897
4020
  return "$rc"
3898
4021
  }
3899
4022
 
3900
- fmix_artifact_v2_managed_entrypoint() {
3901
- local py_bin runner managed_home="$FMIX_HOME"
3902
- if [[ "${FMIX_SELF_TEST_MANAGED_REEXEC:-0}" == "1" \
3903
- && " ${FUNCNAME[*]} " == *" cmd_self_test "* \
3904
- && -n "${FMIX_SELF_TEST_RUNNER_HOME:-}" ]]; then
3905
- managed_home="$FMIX_SELF_TEST_RUNNER_HOME"
3906
- fi
3907
- py_bin="$(fmix_python_bin)" || return 1
3908
- runner="$(fmix_realpath "${BASH_SOURCE[0]}")" || return 1
3909
- "$py_bin" -I -B - "$HOME" "$managed_home" "$0" "$runner" <<'PY'
3910
- import hashlib,os,stat,sys
3911
- user_home,fmix_home,entrypoint,runner=sys.argv[1:]
3912
- uid=os.getuid(); euid=os.geteuid(); user_home=os.path.abspath(user_home); fmix_home=os.path.abspath(fmix_home)
3913
- def reject(reason):
3914
- print(reason,file=sys.stderr)
3915
- raise SystemExit(1)
3916
- if uid==0 or euid!=uid: reject("privileged_cleaner_forbidden")
3917
- entrypoint=os.path.abspath(entrypoint)
3918
- if os.path.realpath(user_home)!=user_home or os.path.realpath(fmix_home)!=fmix_home:
3919
- reject("noncanonical_managed_home")
3920
- known={user_home+"/.local/bin/fmix",user_home+"/.local/share/fmix/bin/fmix"}
3921
- custom=os.environ.get("FMIX_INSTALL_DIR")
3922
- if custom:
3923
- custom=os.path.abspath(custom)
3924
- if os.path.commonpath((user_home,custom))!=user_home: raise SystemExit(1)
3925
- known.add(os.path.join(custom,"fmix"))
3926
- if entrypoint not in known and entrypoint!=runner: reject("unrecognized_managed_entrypoint")
3927
- paths=[]
3928
- for path in sorted(known):
3929
- if not os.path.lexists(path): continue
3930
- parent=os.path.dirname(path)
3931
- if os.path.commonpath((user_home,parent))!=user_home: raise SystemExit(1)
3932
- rel=os.path.relpath(parent,user_home); current=user_home
3933
- for part in (() if rel=="." else rel.split(os.sep)):
3934
- current=os.path.join(current,part); s=os.lstat(current)
3935
- if not stat.S_ISDIR(s.st_mode) or stat.S_ISLNK(s.st_mode) or s.st_uid!=uid or s.st_mode&0o022:
3936
- reject("unsafe_launcher_parent")
3937
- link=os.lstat(path)
3938
- if not stat.S_ISLNK(link.st_mode) or link.st_uid!=uid or link.st_nlink!=1 or os.path.realpath(path)!=runner:
3939
- reject("managed_launcher_mismatch")
3940
- paths.append(path)
3941
- if entrypoint not in paths and (entrypoint!=runner or not paths):
3942
- reject("managed_launcher_missing")
3943
- fd=os.open(runner,os.O_RDONLY|getattr(os,"O_NOFOLLOW",0))
3944
- try:
3945
- s=os.fstat(fd)
3946
- if not stat.S_ISREG(s.st_mode) or s.st_uid!=uid or s.st_nlink!=1 or s.st_mode&0o222 or not s.st_mode&0o111 or s.st_size>4*1024*1024:
3947
- reject("unsafe_runner_identity")
3948
- parent=os.open(fmix_home+"/runtime",os.O_RDONLY|os.O_DIRECTORY|getattr(os,"O_NOFOLLOW",0))
3949
- try:
3950
- runtime=os.fstat(parent)
3951
- if runtime.st_uid!=uid or runtime.st_dev!=s.st_dev or runtime.st_mode&0o022:
3952
- reject("unsafe_runner_runtime")
3953
- finally: os.close(parent)
3954
- h=hashlib.sha256()
3955
- for block in iter(lambda: os.read(fd,65536),b""): h.update(block)
3956
- if runner!=fmix_home+"/runtime/fmix-"+h.hexdigest():
3957
- reject("managed_launcher_runner_mismatch")
3958
- finally: os.close(fd)
3959
- PY
3960
- }
3961
4023
  fmix_managed_publication_gate() {
3962
4024
  # Shared-cache publication requires the managed install (fmix install).
3963
4025
  # An npm/mise-linked runner is refused. Report the validator's reason and
@@ -3965,7 +4027,7 @@ fmix_managed_publication_gate() {
3965
4027
  local reason
3966
4028
  reason="$(fmix_artifact_v2_managed_entrypoint 2>&1)" && return 0
3967
4029
  fmix_warn "Shared ${1:-artifact} publication is held: managed launcher check failed (${reason:-unknown})."
3968
- fmix_warn "Run 'fmix install' and invoke fmix as \$HOME/.local/bin/fmix so shared templates and dependency artifacts can publish."
4030
+ fmix_warn "Run 'fmix install' from this runner so shared templates and dependency artifacts can publish: a byte-identical copy then runs through \$HOME/.local/bin/fmix."
3969
4031
  return 1
3970
4032
  }
3971
4033
 
@@ -7159,6 +7221,7 @@ fmix_lock_keeper_start() {
7159
7221
  local bootstrap_secs failure_kind="bootstrap_failed" retryable=false
7160
7222
  local probe_pid
7161
7223
  FMIX_LAST_LOCK_KEEPER_FAILURE=""
7224
+ FMIX_LAST_LOCK_KEEPER_OBSERVED=""
7162
7225
  # Failed retirement is not contention. Preserve its exact recovery handle
7163
7226
  # rather than overwriting it with a second keeper, even for a different lock.
7164
7227
  if [[ -n "$FMIX_PENDING_LOCK_KEEPER_PID" ]]; then
@@ -7250,7 +7313,20 @@ def io_failure(operation, target, error):
7250
7313
  raise SystemExit(70)
7251
7314
 
7252
7315
 
7316
+ def remove_state():
7317
+ try:
7318
+ state_path.unlink()
7319
+ except OSError:
7320
+ pass
7321
+
7322
+
7253
7323
  def publish_state(value):
7324
+ # A waiter that died can never read this handshake. Removing it here (the
7325
+ # keeper outlives a SIGKILLed waiter until its next liveness poll) means a
7326
+ # killed waiter leaves no keeper state behind.
7327
+ if not owner_alive():
7328
+ remove_state()
7329
+ return
7254
7330
  temporary = state_path.with_name(
7255
7331
  state_path.name + f".tmp-{os.getpid()}"
7256
7332
  )
@@ -7370,8 +7446,10 @@ finally:
7370
7446
  started)
7371
7447
  # Do not unlink an intermediate state: the atomic ready
7372
7448
  # publication could otherwise be removed before we read it.
7449
+ # SECONDS is whole-second: observed at N.99 s, "+1" would
7450
+ # expire ~10 ms later. "+2" always leaves a real second.
7373
7451
  [[ -n "$acquire_deadline" ]] \
7374
- || acquire_deadline=$(( SECONDS + 10#$timeout + 1 ))
7452
+ || acquire_deadline=$(( SECONDS + 10#$timeout + 2 ))
7375
7453
  ;;
7376
7454
  timeout)
7377
7455
  # Only the keeper's actual flock contention is retryable.
@@ -7380,6 +7458,9 @@ finally:
7380
7458
  ;;
7381
7459
  *)
7382
7460
  failure="invalid keeper handshake"
7461
+ if fmix_lock_keeper_contended "$keeper_pid" "$path" "$py_bin"; then
7462
+ failure_kind=contention
7463
+ fi
7383
7464
  break
7384
7465
  ;;
7385
7466
  esac
@@ -7400,21 +7481,97 @@ finally:
7400
7481
  fi
7401
7482
  elif (( SECONDS >= acquire_deadline )); then
7402
7483
  failure="keeper readiness deadline exceeded"
7484
+ if fmix_lock_keeper_contended "$keeper_pid" "$path" "$py_bin"; then
7485
+ failure_kind=contention
7486
+ else
7487
+ # A keeper woken just before the deadline may not have taken
7488
+ # the lock yet. One grace re-probe before calling it fatal.
7489
+ sleep 0.2
7490
+ if fmix_lock_keeper_contended "$keeper_pid" "$path" "$py_bin"; then
7491
+ failure_kind=contention
7492
+ fi
7493
+ fi
7403
7494
  break
7404
7495
  fi
7405
7496
  sleep 0.05
7406
7497
  done
7407
- [[ "$failure_kind" != bootstrap_deadline ]] || retryable=true
7408
- # This is exactly the child launched above, never an inferred lock holder.
7409
- printf '{"error":"lock_keeper_bootstrap_failed","reason":"%s","retryable":%s}\n' \
7410
- "$failure" "$retryable" >&2
7411
- fmix_lock_keeper_retire "$keeper_pid" "$state" || return 70
7498
+ # Live contention (keeper alive, kernel lock held by another process) is
7499
+ # not a bootstrap failure: it is retryable silently, and the caller reports
7500
+ # its holders only when its budget runs out.
7501
+ if [[ "$failure_kind" != contention ]]; then
7502
+ [[ "$failure_kind" != bootstrap_deadline ]] || retryable=true
7503
+ # This is exactly the child launched above, never an inferred lock holder.
7504
+ printf '{"error":"lock_keeper_bootstrap_failed","reason":"%s","retryable":%s}\n' \
7505
+ "$failure" "$retryable" >&2
7506
+ fi
7507
+ FMIX_LAST_LOCK_KEEPER_OBSERVED="$failure_kind"
7508
+ if [[ "$failure_kind" == bootstrap_deadline ]] \
7509
+ && fmix_lock_keeper_kill_unstarted "$keeper_pid" "$state"; then
7510
+ # Exit is proven by wait; no handshake or lock existed to clean up.
7511
+ rm -f "$state" "$state.tmp-$keeper_pid" 2>/dev/null || true
7512
+ [[ "$FMIX_LAST_LOCK_KEEPER_PID" != "$keeper_pid" ]] || FMIX_LAST_LOCK_KEEPER_PID=""
7513
+ else
7514
+ fmix_lock_keeper_retire "$keeper_pid" "$state" || return 70
7515
+ fi
7412
7516
  # Publish the typed kind only after the abandoned keeper is confirmed gone:
7413
7517
  # a retry must never run beside a keeper this attempt failed to retire.
7414
7518
  FMIX_LAST_LOCK_KEEPER_FAILURE="$failure_kind"
7519
+ if [[ "$failure_kind" == contention ]]; then
7520
+ return 1
7521
+ fi
7415
7522
  return 70
7416
7523
  }
7417
7524
 
7525
+ fmix_lock_keeper_contended() {
7526
+ # Evidence for a retryable keeper failure: the keeper is still alive and the
7527
+ # kernel lock it waits on is held by another process. A dead keeper is a
7528
+ # crash; a live keeper on a free lock is not waiting for anyone. Both stay
7529
+ # fatal. The try-lock is shared, so it cannot evict an exclusive holder.
7530
+ local keeper_pid="$1" kernel_path="$2" py_bin="$3"
7531
+ kill -0 "$keeper_pid" 2>/dev/null || return 1
7532
+ "$py_bin" -I -B -c 'import fcntl, sys
7533
+ try:
7534
+ handle = open(sys.argv[1], "a+")
7535
+ except OSError:
7536
+ raise SystemExit(1)
7537
+ try:
7538
+ fcntl.flock(handle, fcntl.LOCK_SH | fcntl.LOCK_NB)
7539
+ except BlockingIOError:
7540
+ raise SystemExit(0)
7541
+ except OSError:
7542
+ raise SystemExit(1)
7543
+ raise SystemExit(1)' "$kernel_path" </dev/null >/dev/null 2>&1
7544
+ }
7545
+
7546
+ fmix_lock_contention_report() {
7547
+ # Names the holder of a generic lock when a caller gives up on contention.
7548
+ local lock_name="$1" diagnostic_name="$2" timeout="$3" metadata_file holder_pid
7549
+ metadata_file="$FMIX_HOME/lock-metadata/${lock_name}.json"
7550
+ holder_pid="$(sed -n 's/.*"pid"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' \
7551
+ "$metadata_file" 2>/dev/null | head -1)"
7552
+ fmix_error "Timed out waiting for kernel lock '$diagnostic_name' after ${timeout}s: still held by another process (holder pid ${holder_pid:-unknown})."
7553
+ fmix_error "Inspect with: fmix status"
7554
+ }
7555
+
7556
+ fmix_compile_permit_holders_json() {
7557
+ # JSON array of live compile-permit holders: pid, worktree and age in seconds.
7558
+ local file pid worktree started epoch now age entries="" sep=""
7559
+ now="$(fmix_epoch)"
7560
+ for file in "$FMIX_HOME"/lock-metadata/compile-permit-*.json; do
7561
+ [[ -f "$file" && ! -L "$file" ]] || continue
7562
+ pid="$(sed -n 's/.*"pid": *\([0-9][0-9]*\).*/\1/p' "$file" | head -1)"
7563
+ [[ -n "$pid" ]] || continue
7564
+ worktree="$(sed -n 's/.*"worktree": *\(".*"\).*/\1/p' "$file" | head -1)"
7565
+ [[ -n "$worktree" ]] || worktree='""'
7566
+ started="$(sed -n 's/.*"start_time": *"\([^"]*\)".*/\1/p' "$file" | head -1)"
7567
+ epoch="$(fmix_iso_epoch "$started" 2>/dev/null)" || epoch=""
7568
+ if [[ "$epoch" =~ ^[0-9]+$ ]]; then age=$(( now - epoch )); else age=-1; fi
7569
+ entries="${entries}${sep}{\"pid\": ${pid}, \"worktree\": ${worktree}, \"age_s\": ${age}}"
7570
+ sep=", "
7571
+ done
7572
+ printf '[%s]' "$entries"
7573
+ }
7574
+
7418
7575
  fmix_lock_keeper_reap_completed() {
7419
7576
  local keeper_pid="$1" job_line
7420
7577
  local terminal_job='^\[[0-9]+\][+-]?[[:space:]]+([1-9][0-9]*)[[:space:]]+(Done|Exit|Killed|Terminated|Hangup|Interrupt)([[:space:]:]|$)'
@@ -7453,6 +7610,23 @@ fmix_lock_keeper_signal_owned() {
7453
7610
  return 70
7454
7611
  }
7455
7612
 
7613
+ fmix_lock_keeper_kill_unstarted() {
7614
+ # Only for a keeper that never published a handshake (bootstrap deadline).
7615
+ # It holds no kernel lock and no FIFO, so its death strands nothing. An
7616
+ # owned running keeper is SIGKILLed and exit is proven by wait. The probe
7617
+ # stop path is bounded by whole-second SECONDS and can miss its budget under
7618
+ # host load, which left retirement failing and the deadline unclassified.
7619
+ local keeper_pid="$1" state="$2"
7620
+ [[ "$keeper_pid" =~ ^[1-9][0-9]*$ ]] || return 1
7621
+ [[ ! -s "$state" ]] || return 1
7622
+ fmix_lock_keeper_signal_owned "$keeper_pid" KILL || return 1
7623
+ wait "$keeper_pid" 2>/dev/null || true
7624
+ if kill -0 "$keeper_pid" 2>/dev/null; then
7625
+ return 1
7626
+ fi
7627
+ return 0
7628
+ }
7629
+
7456
7630
  fmix_lock_keeper_stop() {
7457
7631
  local keeper_pid="${1:-}" child_pid owned=0 py_bin probe_source probe_pid
7458
7632
  local cleanup_deadline kill_at remaining ignored keeper_done=0 probe_done=0
@@ -7502,11 +7676,13 @@ fmix_lock_keeper_stop() {
7502
7676
  # no foreground mkfifo/interpreter startup or blocking FIFO open can run
7503
7677
  # beyond the owning shell deadline. A writer connects nonblockingly only
7504
7678
  # after our read/write open; it then unlinks the private create-only FIFO.
7505
- probe_source='import errno, os, sys, time
7679
+ probe_source='import errno, os, signal, sys, time
7506
7680
  pid = int(sys.argv[1])
7507
7681
  channel = sys.argv[2]
7508
7682
  fd = -1
7509
7683
  created = False
7684
+ # SIGTERM leaves through the finally block below, which removes the FIFO.
7685
+ signal.signal(signal.SIGTERM, lambda _signum, _frame: sys.exit(1))
7510
7686
  try:
7511
7687
  os.mkfifo(channel, 0o600)
7512
7688
  created = True
@@ -7608,6 +7784,12 @@ finally:
7608
7784
  # A refused probe signal stays latched even after a later confirmed exit.
7609
7785
  # Keep its recovery handle; never turn disappearance into a green cleanup.
7610
7786
  (( keeper_refused == 0 && probe_refused == 0 )) || return 70
7787
+ # SIGKILL cannot run the probe's finally block. Once this exact probe has
7788
+ # exited or been signalled, it can no longer unlink its private FIFO, so
7789
+ # the owning shell removes the orphan it created.
7790
+ if [[ -e "$probe_channel" || -L "$probe_channel" ]] && (( probe_done || probe_killed )); then
7791
+ rm -f "$probe_channel" 2>/dev/null || true
7792
+ fi
7611
7793
  if [[ -e "$probe_channel" || -L "$probe_channel" ]]; then
7612
7794
  # An orphaned channel is not a live child or a signal permit. Retain
7613
7795
  # the caller's typed cleanup failure, but never restore an already
@@ -7704,6 +7886,18 @@ fmix_lock_acquire() {
7704
7886
  fmix_lock_keeper_start "$kernel_file" "$mode" "$remaining" \
7705
7887
  "$diagnostic_name" "$lock_name" || keeper_rc=$?
7706
7888
  if (( keeper_rc != 0 )); then
7889
+ case "$FMIX_LAST_LOCK_KEEPER_FAILURE" in
7890
+ contention|bootstrap_deadline)
7891
+ # Retryable keeper failures spend the caller's budget, never
7892
+ # a fixed retry count. Once it is spent, name the holder.
7893
+ if (( $(fmix_epoch) < deadline )); then
7894
+ sleep 0.2
7895
+ continue
7896
+ fi
7897
+ fmix_lock_contention_report "$lock_name" "$diagnostic_name" "$timeout"
7898
+ return 1
7899
+ ;;
7900
+ esac
7707
7901
  (( keeper_rc == 1 )) || return "$keeper_rc"
7708
7902
  fmix_error "Timed out waiting for kernel lock '$diagnostic_name' after ${timeout}s."
7709
7903
  fmix_error "Inspect with: fmix status"
@@ -15068,6 +15262,14 @@ fmix_compile_semaphore_acquire() {
15068
15262
  bootstrap_transient=1
15069
15263
  break
15070
15264
  fi
15265
+ # The deadline was observed, but its abandoned keeper could not be
15266
+ # retired. Retrying beside an unretired keeper is forbidden, so the
15267
+ # deadline is terminal and is reported as exhausted.
15268
+ if [[ "$FMIX_LAST_LOCK_KEEPER_OBSERVED" == bootstrap_deadline ]]; then
15269
+ printf '{"error":"lock_keeper_bootstrap_deadline_exhausted","retries":%d,"retired":false}\n' \
15270
+ "$bootstrap_retries" >&2
15271
+ return 70
15272
+ fi
15071
15273
  # A genuine bootstrap error is not permit contention. Surface
15072
15274
  # its typed failure immediately instead of retrying it.
15073
15275
  return "$keeper_rc"
@@ -15116,8 +15318,8 @@ EOF
15116
15318
  local estimated_wait=$(( (queue_depth / permits) * estimated_compile_s ))
15117
15319
  if (( elapsed + estimated_wait + estimated_compile_s > deadline_secs )); then
15118
15320
  local err_json
15119
- err_json="$(printf '{"error": "host_compile_deadline_exceeded", "queue_depth": %d, "estimated_wait_s": %d, "permit_limit": %d}' \
15120
- "$queue_depth" "$estimated_wait" "$permits")"
15321
+ err_json="$(printf '{"error": "host_compile_deadline_exceeded", "queue_depth": %d, "estimated_wait_s": %d, "permit_limit": %d, "holders": %s}' \
15322
+ "$queue_depth" "$estimated_wait" "$permits" "$(fmix_compile_permit_holders_json)")"
15121
15323
  printf '%s\n' "$err_json" >&2
15122
15324
  return 75
15123
15325
  fi
@@ -16318,7 +16520,7 @@ cmd_setup() {
16318
16520
  # Surface an unmanaged launcher before a long cold compile, not after it.
16319
16521
  # The publication hold itself is non-fatal; this is advisory only.
16320
16522
  if ! fmix_artifact_v2_managed_entrypoint >/dev/null 2>&1; then
16321
- fmix_warn "This fmix launcher is not the managed install. The cold compile will run, but shared template and dependency artifacts will not publish from it (run 'fmix install', then use \$HOME/.local/bin/fmix)."
16523
+ fmix_warn "This fmix launcher is not the managed install. The cold compile will run, but shared template and dependency artifacts will not publish from it (run 'fmix install' from this runner; a byte-identical copy then runs through the managed launcher)."
16322
16524
  fi
16323
16525
  fmix_info "DB name: $db_name"
16324
16526
 
@@ -20384,6 +20586,25 @@ fmix_session_last_from_meta() {
20384
20586
 
20385
20587
  # Keep one warm session per worktree realpath (prefer worktree-default source,
20386
20588
  # then newest last_accessed). Reap the rest when do_reap=1.
20589
+ fmix_lock_metadata_expired_entries() {
20590
+ # Prints "<whole-days><TAB><path>" for each real, non-hidden directory
20591
+ # directly under $1 whose mtime is more than the TTL before $2 (epoch).
20592
+ local dir="$1" now="$2" ttl="$3"
20593
+ if [[ "$FMIX_OS" == Darwin ]]; then
20594
+ find "$dir" -mindepth 1 -maxdepth 1 -type d ! -name '.*' \
20595
+ -exec stat -f '%m %N' {} + 2>/dev/null
20596
+ else
20597
+ find "$dir" -mindepth 1 -maxdepth 1 -type d ! -name '.*' \
20598
+ -exec stat -c '%Y %n' {} + 2>/dev/null
20599
+ fi | awk -v now="$now" -v ttl="$ttl" '
20600
+ {
20601
+ mtime = $1
20602
+ path = substr($0, length($1) + 2)
20603
+ age = now - mtime
20604
+ if (age > ttl) printf "%d\t%s\n", age / 86400, path
20605
+ }'
20606
+ }
20607
+
20387
20608
  fmix_gc_consolidate_sessions() {
20388
20609
  local do_reap="$1" skip_prompts="$2" worktree_filter="${3:-}"
20389
20610
  local real_filter="" py_bin
@@ -22765,23 +22986,86 @@ cmd_gc() {
22765
22986
  lock_metadata_ttl_seconds=$(( lock_metadata_ttl_days * 86400 ))
22766
22987
  echo ""
22767
22988
  echo "--- Retired lock metadata older than ${lock_metadata_ttl_days} days ---"
22768
- local metadata_class metadata_entry metadata_mtime metadata_age
22989
+ local metadata_class metadata_dir metadata_now metadata_expired metadata_count
22990
+ metadata_now="$(fmix_epoch)"
22769
22991
  for metadata_class in released stale; do
22770
- [[ -d "$FMIX_HOME/lock-metadata/$metadata_class" ]] || continue
22771
- for metadata_entry in "$FMIX_HOME/lock-metadata/$metadata_class"/*; do
22772
- [[ -d "$metadata_entry" && ! -L "$metadata_entry" ]] || continue
22773
- metadata_mtime="$(fmix_stat_mtime "$metadata_entry" 2>/dev/null || echo 0)"
22774
- metadata_age=$(( $(fmix_epoch) - metadata_mtime ))
22775
- (( metadata_age > lock_metadata_ttl_seconds )) || continue
22776
- echo " OLD: $metadata_entry (age=$(( metadata_age / 86400 ))d)"
22992
+ metadata_dir="$FMIX_HOME/lock-metadata/$metadata_class"
22993
+ [[ -d "$metadata_dir" ]] || continue
22994
+ # One find+stat pass per class replaces a date, stat and realpath
22995
+ # fork per entry. Eligibility is unchanged: the same real directory
22996
+ # whose mtime is more than the TTL before now.
22997
+ metadata_expired="$(fmix_lock_metadata_expired_entries \
22998
+ "$metadata_dir" "$metadata_now" "$lock_metadata_ttl_seconds")"
22999
+ [[ -n "$metadata_expired" ]] || continue
23000
+ metadata_count="$(printf '%s\n' "$metadata_expired" | wc -l | tr -d ' ')"
23001
+ printf '%s\n' "$metadata_expired" | awk -F '\t' '{ print " OLD: " $2 " (age=" $1 "d)" }'
23002
+ actions=$(( actions + metadata_count ))
23003
+ if (( do_reap == 1 )); then
23004
+ # The class directory is the guarded root: its entries are
23005
+ # non-symlink children, so one check covers every removal.
23006
+ if fmix_guard_path "$metadata_dir"; then
23007
+ if printf '%s\n' "$metadata_expired" | awk -F '\t' '{ print $2 }' \
23008
+ | tr '\n' '\0' | xargs -0 rm -rf --; then
23009
+ echo " Deleted: $metadata_count retired lock-metadata entries from $metadata_dir"
23010
+ else
23011
+ fmix_warn "Some retired lock metadata in $metadata_dir could not be removed."
23012
+ fi
23013
+ else
23014
+ fmix_warn "Keeping retired lock metadata in $metadata_dir: root is not under FMIX_HOME."
23015
+ fi
23016
+ fi
23017
+ done
23018
+
23019
+ # Keeper handshakes and probe FIFOs carry the PID of the shell that made
23020
+ # them. A dead owner can never read or unlink them; a live owner keeps them.
23021
+ echo ""
23022
+ echo "--- Keeper state and probe FIFOs with dead owners ---"
23023
+ local owner_artifact owner_list owner_dead owner_path py_owner_bin
23024
+ py_owner_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
23025
+ owner_list=""
23026
+ for owner_artifact in "$FMIX_HOME/lock-keeper-state"/*.state "$FMIX_HOME"/.lock-stop-probe-*; do
23027
+ [[ -e "$owner_artifact" || -L "$owner_artifact" ]] || continue
23028
+ owner_list="${owner_list}${owner_artifact}"$'\n'
23029
+ done
23030
+ if [[ -n "$owner_list" ]]; then
23031
+ owner_dead="$(printf '%s' "$owner_list" | "$py_owner_bin" -I -B -c '
23032
+ import os, re, stat, sys
23033
+ state = re.compile(r"^(.+)\.([0-9]+)\.[0-9]+\.state$")
23034
+ probe = re.compile(r"^\.lock-stop-probe-([0-9]+)-[0-9]+-[0-9]+$")
23035
+ for path in sys.stdin.read().splitlines():
23036
+ if not path or os.path.islink(path):
23037
+ continue
23038
+ name = os.path.basename(path)
23039
+ if os.path.basename(os.path.dirname(path)) == "lock-keeper-state":
23040
+ match = state.match(name)
23041
+ else:
23042
+ match = probe.match(name)
23043
+ if match and not stat.S_ISFIFO(os.lstat(path).st_mode):
23044
+ continue
23045
+ if not match:
23046
+ continue
23047
+ try:
23048
+ os.kill(int(match.group(2) if match.re is state else match.group(1)), 0)
23049
+ except ProcessLookupError:
23050
+ print(path)
23051
+ except OSError:
23052
+ pass
23053
+ ' 2>/dev/null)"
23054
+ fi
23055
+ if [[ -n "$owner_dead" ]]; then
23056
+ # Every candidate is a direct child of FMIX_HOME: one guard covers all.
23057
+ if (( do_reap == 1 )) && ! fmix_guard_path "$FMIX_HOME"; then
23058
+ fmix_warn "Keeping dead-owner keeper artifacts: FMIX_HOME guard failed."
23059
+ fi
23060
+ while IFS= read -r owner_path; do
23061
+ [[ -n "$owner_path" ]] || continue
23062
+ echo " DEAD OWNER: $owner_path"
22777
23063
  actions=$(( actions + 1 ))
22778
23064
  if (( do_reap == 1 )); then
22779
- fmix_guard_path "$metadata_entry" \
22780
- && rm -rf "$metadata_entry" \
22781
- && echo " Deleted: $metadata_entry"
23065
+ rm -f "$owner_path" 2>/dev/null && echo " Deleted: $owner_path"
22782
23066
  fi
22783
- done
22784
- done
23067
+ done <<<"$owner_dead"
23068
+ fi
22785
23069
 
22786
23070
  # Published artifacts are not released by age or --yes. The former
22787
23071
  # template path lacked reader coordination; the dependency path lacked
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calltelemetry/fmix",
3
- "version": "2.7.27",
3
+ "version": "2.7.29",
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": "79915aeb7ff03adeb3308c73fa5363b4d6fa59ace54813a607c0e43ec500f028",
30
- "sourceTag": "fmix-v2.7.27"
29
+ "runnerSha256": "cb79676edbcf683cd4962938b4bfb2ae1c2e807889da04b0586388c0c5b0513b",
30
+ "sourceTag": "fmix-v2.7.29"
31
31
  }
32
32
  }