@calltelemetry/fmix 2.7.13 → 2.7.14

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 +690 -92
  2. package/package.json +3 -3
package/bin/fmix CHANGED
@@ -25,7 +25,7 @@ set -uo pipefail
25
25
  # ---------------------------------------------------------------------------
26
26
  # Constants & config
27
27
  # ---------------------------------------------------------------------------
28
- FMIX_VERSION="2.7.13"
28
+ FMIX_VERSION="2.7.14"
29
29
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
30
30
  FMIX_OS="$(uname -s)"
31
31
  FMIX_ARCH="$(uname -m)"
@@ -7743,6 +7743,7 @@ fmix_ensure_template() {
7743
7743
  fmix_find_warm_donor_session() {
7744
7744
  local worktree="$1" template_key="$2"
7745
7745
  local real py_bin donor
7746
+ shift 2
7746
7747
  real="$(fmix_realpath "$worktree")" || return 1
7747
7748
  [[ -n "$FMIX_ACTIVE_SESSION_ROOT" ]] || return 1
7748
7749
  py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
@@ -7751,21 +7752,22 @@ fmix_find_warm_donor_session() {
7751
7752
  DONOR_WORKTREE="$real" \
7752
7753
  DONOR_TEMPLATE_KEY="$template_key" \
7753
7754
  DONOR_EXCLUDE="$FMIX_ACTIVE_SESSION_ROOT" \
7754
- "$py_bin" - <<'PY'
7755
- import json, os
7755
+ "$py_bin" - "$@" <<'PY'
7756
+ import json, os, sys
7756
7757
  from pathlib import Path
7757
7758
 
7758
7759
  home = Path(os.environ["FMIX_HOME_FOR_DONOR"])
7759
7760
  wt = os.environ["DONOR_WORKTREE"]
7760
7761
  key = os.environ["DONOR_TEMPLATE_KEY"]
7761
- exclude = os.environ["DONOR_EXCLUDE"]
7762
+ excluded = {os.environ["DONOR_EXCLUDE"]}
7763
+ excluded.update(sys.argv[1:])
7762
7764
  root = home / "sessions" / "v1"
7763
7765
  best = None # (is_same_wt, last, path)
7764
7766
  if not root.is_dir():
7765
7767
  raise SystemExit(0)
7766
7768
  for meta in root.glob("*/*/*/meta.json"):
7767
7769
  session_root = meta.parent
7768
- if str(session_root) == exclude:
7770
+ if str(session_root) in excluded:
7769
7771
  continue
7770
7772
  try:
7771
7773
  d = json.loads(meta.read_text(encoding="utf-8"))
@@ -7808,15 +7810,327 @@ PY
7808
7810
  printf '%s\n' "$donor"
7809
7811
  }
7810
7812
 
7813
+ fmix_donor_session_roots_are_owned() {
7814
+ local donor="$1" py_bin
7815
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
7816
+ FMIX_HOME_FOR_DONOR="$FMIX_HOME" \
7817
+ DONOR_ROOT="$donor" \
7818
+ "$py_bin" - <<'PY'
7819
+ import os
7820
+ import re
7821
+ import stat
7822
+ import sys
7823
+
7824
+ configured_home = os.environ["FMIX_HOME_FOR_DONOR"].rstrip(os.sep)
7825
+ home = os.path.realpath(configured_home)
7826
+ donor = os.path.abspath(os.environ["DONOR_ROOT"])
7827
+ fd = None
7828
+ try:
7829
+ if not os.path.isabs(configured_home) or not os.path.isdir(home):
7830
+ raise ValueError("home path")
7831
+ prefix = os.path.join(configured_home, "sessions", "v1") + os.sep
7832
+ if not donor.startswith(prefix) or os.path.normpath(donor) != donor:
7833
+ raise ValueError("session path")
7834
+ components = donor[len(prefix):].split(os.sep)
7835
+ if (len(components) != 3
7836
+ or any(part in ("", ".", "..") for part in components)
7837
+ or not re.fullmatch(r"[0-9a-f]{16}", components[1])):
7838
+ raise ValueError("session components")
7839
+
7840
+ if not hasattr(os, "O_NOFOLLOW") or not hasattr(os, "O_DIRECTORY"):
7841
+ raise ValueError("no-follow directory opens unavailable")
7842
+ flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
7843
+ fd = os.open(home, flags)
7844
+ home_stat = os.fstat(fd)
7845
+ device = home_stat.st_dev
7846
+ owner = os.getuid()
7847
+ if home_stat.st_uid != owner or not stat.S_ISDIR(home_stat.st_mode):
7848
+ raise ValueError("home ownership")
7849
+ for component in ("sessions", "v1", *components):
7850
+ child = os.open(component, flags, dir_fd=fd)
7851
+ os.close(fd)
7852
+ fd = child
7853
+ current = os.fstat(fd)
7854
+ if (current.st_uid != owner or current.st_dev != device
7855
+ or not stat.S_ISDIR(current.st_mode)):
7856
+ raise ValueError("session root ownership")
7857
+ for component in ("deps", "_build"):
7858
+ child = os.open(component, flags, dir_fd=fd)
7859
+ current = os.fstat(child)
7860
+ os.close(child)
7861
+ if (current.st_uid != owner or current.st_dev != device
7862
+ or not stat.S_ISDIR(current.st_mode)):
7863
+ raise ValueError("artifact root ownership")
7864
+ except (OSError, ValueError):
7865
+ raise SystemExit(1)
7866
+ finally:
7867
+ if fd is not None:
7868
+ os.close(fd)
7869
+ PY
7870
+ }
7871
+
7872
+ fmix_donor_session_revalidate() {
7873
+ local donor="$1" target_worktree="$2" template_key="$3" expected_session_hash="$4"
7874
+ local donor_meta="$donor/meta.json" current_session_hash py_bin
7875
+ fmix_donor_session_roots_are_owned "$donor" || return $?
7876
+ [[ -d "$donor/deps" && ! -L "$donor/deps" \
7877
+ && -d "$donor/_build" && ! -L "$donor/_build" \
7878
+ && -f "$donor/artifact-state.json" && -f "$donor/compile-stamp.json" ]] || return 1
7879
+ current_session_hash="$(fmix_session_hash_from_metadata_path "$donor_meta")" || return $?
7880
+ [[ "$current_session_hash" == "$expected_session_hash" ]] || return 1
7881
+ fmix_session_metadata_identity_matches_path "$donor_meta" || return $?
7882
+ py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
7883
+ DONOR_ROOT="$donor" \
7884
+ DONOR_TARGET_WORKTREE="$target_worktree" \
7885
+ DONOR_TEMPLATE_KEY="$template_key" \
7886
+ DONOR_SESSION_HASH="$expected_session_hash" \
7887
+ "$py_bin" - <<'PY'
7888
+ import json
7889
+ import os
7890
+ from pathlib import Path
7891
+
7892
+ root = Path(os.path.abspath(os.environ["DONOR_ROOT"]))
7893
+ target_worktree = os.path.realpath(os.environ["DONOR_TARGET_WORKTREE"])
7894
+ template_key = os.environ["DONOR_TEMPLATE_KEY"]
7895
+ session_hash = os.environ["DONOR_SESSION_HASH"]
7896
+
7897
+ def read_object(name):
7898
+ path = root / name
7899
+ if path.is_symlink() or not path.is_file():
7900
+ raise ValueError(name)
7901
+ value = json.loads(path.read_text(encoding="utf-8"))
7902
+ if not isinstance(value, dict):
7903
+ raise ValueError(name)
7904
+ return value
7905
+
7906
+ def canonical_path(value):
7907
+ if not isinstance(value, str) or not value or not os.path.isabs(value):
7908
+ raise ValueError("path")
7909
+ return os.path.realpath(value)
7910
+
7911
+ try:
7912
+ meta = read_object("meta.json")
7913
+ if meta.get("session_hash") not in (None, session_hash):
7914
+ raise ValueError("session hash")
7915
+ if meta.get("session_id") not in (None, session_hash):
7916
+ raise ValueError("session id")
7917
+ source_worktree = canonical_path(meta.get("worktree"))
7918
+ if ("deps_path" in meta
7919
+ and canonical_path(meta["deps_path"]) != os.path.realpath(root / "deps")):
7920
+ raise ValueError("metadata deps path")
7921
+ if "build_path" in meta:
7922
+ recorded_build_path = canonical_path(meta["build_path"])
7923
+ allowed_build_paths = {
7924
+ os.path.realpath(root / "_build"),
7925
+ os.path.realpath(root / "_build" / "test"),
7926
+ }
7927
+ if recorded_build_path not in allowed_build_paths:
7928
+ raise ValueError("metadata build path")
7929
+
7930
+ artifact_state = read_object("artifact-state.json")
7931
+ if artifact_state.get("template_key") != template_key:
7932
+ raise ValueError("template key")
7933
+
7934
+ compile_stamp = read_object("compile-stamp.json")
7935
+ if ("template_key" in compile_stamp
7936
+ and compile_stamp.get("template_key") != template_key):
7937
+ raise ValueError("compile template key")
7938
+ if ("path" in compile_stamp
7939
+ and canonical_path(compile_stamp["path"]) != source_worktree):
7940
+ raise ValueError("compile path")
7941
+ if ("build_root" in compile_stamp
7942
+ and canonical_path(compile_stamp["build_root"]) != os.path.realpath(root / "_build")):
7943
+ raise ValueError("compile build root")
7944
+
7945
+ dependency_stamp_path = root / "dependency-stamp.json"
7946
+ if dependency_stamp_path.exists() or dependency_stamp_path.is_symlink():
7947
+ dependency_stamp = read_object("dependency-stamp.json")
7948
+ if ("path" in dependency_stamp
7949
+ and canonical_path(dependency_stamp["path"]) != source_worktree):
7950
+ raise ValueError("dependency path")
7951
+ if ("build_root" in dependency_stamp
7952
+ and canonical_path(dependency_stamp["build_root"]) != os.path.realpath(root / "_build")):
7953
+ raise ValueError("dependency build root")
7954
+
7955
+ if source_worktree != target_worktree and compile_stamp.get("schema_version") == 2:
7956
+ if compile_stamp.get("dirty_hash", "clean") != "clean":
7957
+ raise ValueError("dirty cross-worktree donor")
7958
+ except (OSError, TypeError, ValueError, json.JSONDecodeError):
7959
+ raise SystemExit(1)
7960
+ PY
7961
+ }
7962
+
7963
+ fmix_donor_make_stage_writable() {
7964
+ (( $# == 2 )) || return 1
7965
+ chmod -R u+w "$1" "$2"
7966
+ }
7967
+
7968
+ fmix_donor_copy_stamps() {
7969
+ (( $# == 2 )) || return 1
7970
+ local donor="$1" stage_root="$2"
7971
+ cp -f "$donor/compile-stamp.json" "$stage_root/compile-stamp.json" || return 1
7972
+ if [[ -f "$donor/dependency-stamp.json" ]]; then
7973
+ cp -f "$donor/dependency-stamp.json" "$stage_root/dependency-stamp.json" || return 1
7974
+ fi
7975
+ }
7976
+
7977
+ fmix_donor_rebind_staged_stamps() {
7978
+ local stage_root="$1" real="$2" build_root="$3" py_bin="$4"
7979
+ "$py_bin" - "$stage_root" "$real" "$build_root" <<'PY'
7980
+ import json
7981
+ import os
7982
+ import sys
7983
+ from pathlib import Path
7984
+
7985
+ root = Path(sys.argv[1])
7986
+ real = sys.argv[2]
7987
+ build_root = sys.argv[3]
7988
+ for name in ("compile-stamp.json", "dependency-stamp.json"):
7989
+ path = root / name
7990
+ if not path.exists() and not path.is_symlink():
7991
+ if name == "compile-stamp.json":
7992
+ raise SystemExit(1)
7993
+ continue
7994
+ if path.is_symlink() or not path.is_file():
7995
+ raise SystemExit(1)
7996
+ try:
7997
+ value = json.loads(path.read_text(encoding="utf-8"))
7998
+ if not isinstance(value, dict):
7999
+ raise ValueError(name)
8000
+ value["path"] = real
8001
+ value["build_root"] = build_root
8002
+ temporary = path.with_name(path.name + f".tmp-{os.getpid()}")
8003
+ temporary.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
8004
+ os.replace(temporary, path)
8005
+ except (OSError, TypeError, ValueError, json.JSONDecodeError):
8006
+ raise SystemExit(1)
8007
+ PY
8008
+ }
8009
+
8010
+ fmix_donor_stage_artifact_state() {
8011
+ (( $# == 3 )) || return 1
8012
+ local staged_root="$1" worktree="$2" template_key="$3"
8013
+ local original_session_root="$FMIX_ACTIVE_SESSION_ROOT" rc=0
8014
+ FMIX_ACTIVE_SESSION_ROOT="$staged_root"
8015
+ fmix_write_artifact_state "$worktree" "$template_key" || rc=$?
8016
+ FMIX_ACTIVE_SESSION_ROOT="$original_session_root"
8017
+ return "$rc"
8018
+ }
8019
+
8020
+ fmix_donor_transaction_move() {
8021
+ (( $# == 3 )) || return 1
8022
+ mv "$1" "$2"
8023
+ }
8024
+
8025
+ fmix_donor_transaction_stage_cleanup() {
8026
+ (( $# == 1 )) || return 1
8027
+ local stage="$1"
8028
+ fmix_guard_path "$stage" || return 1
8029
+ rm -rf "$stage" || return 1
8030
+ [[ ! -e "$stage" && ! -L "$stage" ]]
8031
+ }
8032
+
8033
+ _fmix_donor_install_interrupt() {
8034
+ if (( ${donor_install_interrupted:-0} == 0 )); then
8035
+ donor_install_interrupted=1
8036
+ donor_install_interrupt_rc="${1:-130}"
8037
+ fi
8038
+ }
8039
+
8040
+ fmix_donor_restore_interrupt_traps() {
8041
+ (( $# == 3 )) || return 1
8042
+ local saved_int="$1" saved_term="$2" saved_hup="$3"
8043
+ if [[ -n "$saved_int" ]]; then eval "$saved_int"; else trap - INT; fi
8044
+ if [[ -n "$saved_term" ]]; then eval "$saved_term"; else trap - TERM; fi
8045
+ if [[ -n "$saved_hup" ]]; then eval "$saved_hup"; else trap - HUP; fi
8046
+ }
8047
+
8048
+ fmix_donor_session_lease_release() {
8049
+ local lock_name="${1:-}" operation_rc="${2:-0}" release_rc=0
8050
+ fmix_lock_release "$lock_name" || release_rc=$?
8051
+ if [[ -n "$FMIX_PENDING_LOCK_KEEPER_PID" ]] && (( release_rc == 0 )); then
8052
+ release_rc=70
8053
+ fi
8054
+ if (( release_rc != 0 )); then
8055
+ FMIX_ARTIFACT_LEASE_RELEASE_FAILED=1
8056
+ printf '{"error":"donor_session_lock_cleanup_failed","operation_exit_code":%d,"cleanup_exit_code":%d}\n' \
8057
+ "$operation_rc" "$release_rc" >&2
8058
+ fmix_error "Donor session lease cleanup failed (operation rc=$operation_rc, release rc=$release_rc)."
8059
+ return 70
8060
+ fi
8061
+ return 0
8062
+ }
8063
+
8064
+ _fmix_donor_abort_staging() {
8065
+ (( $# == 4 )) || return 70
8066
+ local lock_name="$1" stage="${2:-}" operation_rc="$3"
8067
+ local lease_held="$4" release_rc=0 cleanup_rc=0 restore_rc=0
8068
+
8069
+ if (( donor_install_interrupted != 0 )); then
8070
+ operation_rc="$donor_install_interrupt_rc"
8071
+ fi
8072
+ # The scoped signal handler only records cancellation while cleanup runs;
8073
+ # keep it installed through lease retirement and stage removal.
8074
+ if (( lease_held != 0 )); then
8075
+ fmix_donor_session_lease_release "$lock_name" "$operation_rc" \
8076
+ || release_rc=$?
8077
+ fi
8078
+ # Keep staging inputs when lease release is not confirmed. Cleanup failure
8079
+ # also remains fatal, with whatever recovery data is left kept in place.
8080
+ if (( release_rc == 0 )) && [[ -n "$stage" ]]; then
8081
+ fmix_donor_transaction_stage_cleanup "$stage" || cleanup_rc=$?
8082
+ fi
8083
+ if (( cleanup_rc != 0 )); then
8084
+ fmix_error "Could not clean failed donor staging; preserving recovery inputs: $stage"
8085
+ fi
8086
+ fmix_donor_restore_interrupt_traps \
8087
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" || restore_rc=$?
8088
+ # A signal delivered while the previous handlers are being restored must
8089
+ # still become the caller-visible result instead of an ordinary cache miss.
8090
+ if (( donor_install_interrupted != 0 )); then
8091
+ operation_rc="$donor_install_interrupt_rc"
8092
+ fi
8093
+ if (( release_rc != 0 )); then
8094
+ return 70
8095
+ fi
8096
+ if (( cleanup_rc != 0 )); then
8097
+ return 70
8098
+ fi
8099
+ if (( restore_rc != 0 )); then
8100
+ fmix_error "Could not restore caller signal traps after donor staging cleanup."
8101
+ return 70
8102
+ fi
8103
+ return "$operation_rc"
8104
+ }
8105
+
7811
8106
  fmix_clone_donor_to_session() {
7812
- local worktree="$1" donor="$2" deps_root build_root retired retired_used=0 real py_bin
8107
+ local worktree="$1" donor="$2" template_key="${3:-}"
8108
+ local deps_root build_root real py_bin
8109
+ local donor_meta donor_session_hash donor_lock_name
8110
+ local target_session_root dependency_stamp compile_stamp artifact_state
8111
+ local donor_lease_rc=0 donor_lease_held=0 donor_restore_rc=0
8112
+ local donor_signal_after_restore=0
8113
+ local stage="" stage_new stage_old stage_rc=0 stage_cleanup_rc=0
8114
+ local transaction_rc=0 rollback_failed=0 index item_name item_source item_target move_rc
8115
+ local revalidate_rc=0
8116
+ local donor_install_interrupted=0 donor_install_interrupt_rc=0
8117
+ local saved_trap_int saved_trap_term saved_trap_hup
8118
+ (( $# == 3 )) && [[ "$template_key" =~ ^[0-9a-f]{16}$ ]] || return 1
7813
8119
  real="$(fmix_realpath "$worktree")" || return 1
7814
8120
  deps_root="$(fmix_session_deps_root "$worktree")" || return 1
7815
8121
  build_root="$(fmix_compile_build_root "$worktree")" || return 1
7816
8122
 
7817
8123
  [[ -n "$FMIX_ACTIVE_SESSION_ROOT" ]] || return 1
8124
+ target_session_root="$FMIX_ACTIVE_SESSION_ROOT"
8125
+ [[ -d "$target_session_root" && ! -L "$target_session_root" ]] || return 1
7818
8126
  [[ "$deps_root" == "$FMIX_ACTIVE_SESSION_ROOT/deps" \
7819
8127
  && "$build_root" == "$FMIX_ACTIVE_SESSION_ROOT/_build" ]] || return 1
8128
+ dependency_stamp="$(fmix_dependency_stamp_file "$worktree")" || return 1
8129
+ compile_stamp="$(fmix_compile_stamp_file "$worktree")" || return 1
8130
+ artifact_state="$(fmix_artifact_state_file "$worktree")" || return 1
8131
+ [[ "$dependency_stamp" == "$target_session_root/dependency-stamp.json" \
8132
+ && "$compile_stamp" == "$target_session_root/compile-stamp.json" \
8133
+ && "$artifact_state" == "$target_session_root/artifact-state.json" ]] || return 1
7820
8134
  fmix_guard_path "$donor" || {
7821
8135
  fmix_error "Donor path is outside FMIX_HOME: $donor"
7822
8136
  return 1
@@ -7827,99 +8141,357 @@ fmix_clone_donor_to_session() {
7827
8141
 
7828
8142
  py_bin="$(fmix_python_bin 2>/dev/null || echo python3)"
7829
8143
 
7830
- # Lock-Then-Revalidate (LOCK_SH): a dedicated keeper owns the descriptor,
7831
- # so copy helpers cannot inherit and extend donor-lock lifetime.
7832
- local donor_keeper_pid donor_keeper_rc=0
7833
- [[ -f "$donor/meta.json" ]] || return 1
7834
- fmix_lock_keeper_start "$donor/meta.json" shared 0 \
7835
- "donor session clone" "donor-$(fmix_hash_string "$donor" | cut -c1-16)" || donor_keeper_rc=$?
7836
- if (( donor_keeper_rc != 0 )); then
7837
- fmix_warn "Could not acquire shared lock on donor session $donor."
7838
- return "$donor_keeper_rc"
7839
- fi
7840
- donor_keeper_pid="$FMIX_LAST_LOCK_KEEPER_PID"
7841
-
7842
- # Revalidate donor artifacts under active LOCK_SH
7843
- if [[ ! -d "$donor/deps" || ! -d "$donor/_build" || ! -f "$donor/compile-stamp.json" ]]; then
7844
- fmix_lock_keeper_retire "$donor_keeper_pid" || return 70
7845
- fmix_warn "Donor session $donor missing required artifacts during revalidation."
7846
- return 1
8144
+ # Donor sessions share the same persistent session kernel lock used by
8145
+ # execution and GC. Never derive this lock from a metadata field: metadata
8146
+ # is replaceable, so derive and validate the identity from its canonical
8147
+ # session path before acquiring the lease.
8148
+ donor_meta="$donor/meta.json"
8149
+ [[ -f "$donor_meta" && ! -L "$donor_meta" ]] || return 1
8150
+ donor_session_hash="$(fmix_session_hash_from_metadata_path "$donor_meta")" || return 1
8151
+ fmix_session_metadata_identity_matches_path "$donor_meta" || return 1
8152
+ donor_lock_name="session-$donor_session_hash"
8153
+ # Guard the entire leased read, including revalidation and staging copies.
8154
+ # The saved caller handlers are restored on every exit after admission.
8155
+ saved_trap_int="$(trap -p INT)"
8156
+ saved_trap_term="$(trap -p TERM)"
8157
+ saved_trap_hup="$(trap -p HUP)"
8158
+ trap '_fmix_donor_install_interrupt 130' INT
8159
+ trap '_fmix_donor_install_interrupt 143' TERM
8160
+ trap '_fmix_donor_install_interrupt 129' HUP
8161
+ # Use the canonical session lease so new and legacy FMix binaries arbitrate
8162
+ # on the same mkdir record as execution and GC. This intentionally takes an
8163
+ # exclusive, zero-wait lease only for the duration of copying one donor.
8164
+ # Passive mode refuses preexisting legacy metadata without stale cleanup.
8165
+ fmix_lock_acquire "$donor_lock_name" 0 "donor session clone" 1 || donor_lease_rc=$?
8166
+ if (( donor_lease_rc != 0 )); then
8167
+ fmix_donor_restore_interrupt_traps \
8168
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" \
8169
+ || donor_restore_rc=$?
8170
+ if (( donor_install_interrupted != 0 )); then
8171
+ donor_signal_after_restore=1
8172
+ fi
8173
+ if (( donor_restore_rc != 0 )); then return 70; fi
8174
+ fmix_lock_failure_is_fatal "$donor_lease_rc" && return 70
8175
+ if (( donor_signal_after_restore != 0 )); then
8176
+ donor_lease_rc="$donor_install_interrupt_rc"
8177
+ fi
8178
+ fmix_warn "Could not acquire exclusive donor session lease for $donor."
8179
+ return "$donor_lease_rc"
8180
+ fi
8181
+ donor_lease_held=1
8182
+ if (( donor_install_interrupted != 0 )); then
8183
+ _fmix_donor_abort_staging \
8184
+ "$donor_lock_name" "" "$donor_install_interrupt_rc" "$donor_lease_held"
8185
+ return $?
7847
8186
  fi
7848
8187
 
7849
- retired="$FMIX_ACTIVE_SESSION_ROOT/retired/$(date +%Y%m%dT%H%M%S)-$$-${RANDOM}"
7850
- if [[ -e "$deps_root" || -L "$deps_root" \
7851
- || -e "$build_root" || -L "$build_root" ]]; then
7852
- mkdir -p "$retired" || { fmix_lock_keeper_retire "$donor_keeper_pid" || return 70; return 1; }
7853
- retired_used=1
7854
- [[ -e "$deps_root" || -L "$deps_root" ]] && mv "$deps_root" "$retired/deps"
7855
- [[ -e "$build_root" || -L "$build_root" ]] && mv "$build_root" "$retired/_build"
8188
+ # Atomic metadata replacement does not release this path-derived canonical
8189
+ # session lease. Re-read identity, paths, key, and existing stamps while held.
8190
+ fmix_donor_session_revalidate \
8191
+ "$donor" "$real" "$template_key" "$donor_session_hash" || revalidate_rc=$?
8192
+ if (( donor_install_interrupted != 0 )); then
8193
+ revalidate_rc="$donor_install_interrupt_rc"
8194
+ fi
8195
+ if (( revalidate_rc != 0 )); then
8196
+ if (( donor_install_interrupted == 0 )); then
8197
+ fmix_warn "Donor session $donor failed identity or artifact revalidation."
8198
+ fi
8199
+ _fmix_donor_abort_staging \
8200
+ "$donor_lock_name" "" "$revalidate_rc" "$donor_lease_held"
8201
+ return $?
7856
8202
  fi
7857
8203
 
7858
- if ! fmix_cow_clone "$donor/deps" "$deps_root"; then
7859
- fmix_lock_keeper_retire "$donor_keeper_pid" || return 70
7860
- return 1
8204
+ stage="$(mktemp -d "$target_session_root/.donor-install.XXXXXX")" || stage_rc=$?
8205
+ if (( donor_install_interrupted != 0 )); then
8206
+ stage_rc="$donor_install_interrupt_rc"
7861
8207
  fi
7862
- if ! fmix_cow_clone "$donor/_build" "$build_root"; then
7863
- mkdir -p "$retired" || true
7864
- mv "$deps_root" "$retired/failed-deps" 2>/dev/null || true
7865
- fmix_lock_keeper_retire "$donor_keeper_pid" || return 70
7866
- return 1
8208
+ if (( stage_rc != 0 )); then
8209
+ _fmix_donor_abort_staging \
8210
+ "$donor_lock_name" "$stage" "$stage_rc" "$donor_lease_held"
8211
+ return $?
7867
8212
  fi
7868
- chmod -R u+w "$deps_root" "$build_root" || {
7869
- fmix_error "Could not make donor clone writable."
7870
- fmix_lock_keeper_retire "$donor_keeper_pid" || return 70
7871
- return 1
7872
- }
7873
-
7874
- # Import proven stamps so fmix_ensure_compile can stamp-hit when content matches.
7875
- local f
7876
- for f in dependency-stamp.json compile-stamp.json; do
7877
- if [[ -f "$donor/$f" ]]; then
7878
- cp -f "$donor/$f" "$FMIX_ACTIVE_SESSION_ROOT/$f" || true
8213
+ stage_new="$stage/new"
8214
+ stage_old="$stage/old"
8215
+ mkdir -p "$stage_new" "$stage_old" || stage_rc=$?
8216
+ if (( donor_install_interrupted != 0 )); then
8217
+ stage_rc="$donor_install_interrupt_rc"
8218
+ fi
8219
+ if (( stage_rc == 0 )); then
8220
+ fmix_cow_clone "$donor/deps" "$stage_new/deps" || stage_rc=$?
8221
+ if (( donor_install_interrupted != 0 )); then
8222
+ stage_rc="$donor_install_interrupt_rc"
7879
8223
  fi
7880
- done
8224
+ fi
8225
+ if (( stage_rc == 0 )); then
8226
+ fmix_cow_clone "$donor/_build" "$stage_new/_build" || stage_rc=$?
8227
+ if (( donor_install_interrupted != 0 )); then
8228
+ stage_rc="$donor_install_interrupt_rc"
8229
+ fi
8230
+ fi
8231
+ if (( stage_rc == 0 )); then
8232
+ fmix_donor_make_stage_writable \
8233
+ "$stage_new/deps" "$stage_new/_build" || stage_rc=$?
8234
+ if (( donor_install_interrupted != 0 )); then
8235
+ stage_rc="$donor_install_interrupt_rc"
8236
+ fi
8237
+ fi
8238
+ if (( stage_rc == 0 )); then
8239
+ fmix_donor_copy_stamps "$donor" "$stage_new" || stage_rc=$?
8240
+ if (( donor_install_interrupted != 0 )); then
8241
+ stage_rc="$donor_install_interrupt_rc"
8242
+ fi
8243
+ fi
8244
+ if (( stage_rc == 0 )); then
8245
+ fmix_donor_rebind_staged_stamps \
8246
+ "$stage_new" "$real" "$build_root" "$py_bin" || stage_rc=$?
8247
+ if (( donor_install_interrupted != 0 )); then
8248
+ stage_rc="$donor_install_interrupt_rc"
8249
+ fi
8250
+ fi
8251
+ if (( stage_rc == 0 )); then
8252
+ find "$stage_new/_build" -type f -name 'compile.*' -exec touch {} + \
8253
+ 2>/dev/null || stage_rc=$?
8254
+ if (( donor_install_interrupted != 0 )); then
8255
+ stage_rc="$donor_install_interrupt_rc"
8256
+ fi
8257
+ if (( stage_rc == 0 )); then
8258
+ find "$stage_new/deps" -type f -name 'CMakeCache.txt' -delete \
8259
+ 2>/dev/null || stage_rc=$?
8260
+ fi
8261
+ if (( donor_install_interrupted != 0 )); then
8262
+ stage_rc="$donor_install_interrupt_rc"
8263
+ fi
8264
+ if (( stage_rc == 0 )); then
8265
+ find "$stage_new/deps" -type d -name 'c_build' -exec rm -rf {} + \
8266
+ 2>/dev/null || stage_rc=$?
8267
+ fi
8268
+ if (( donor_install_interrupted != 0 )); then
8269
+ stage_rc="$donor_install_interrupt_rc"
8270
+ fi
8271
+ if (( stage_rc == 0 )); then
8272
+ find "$stage_new/deps" -type d -name 'CMakeFiles' -exec rm -rf {} + \
8273
+ 2>/dev/null || stage_rc=$?
8274
+ fi
8275
+ if (( donor_install_interrupted != 0 )); then
8276
+ stage_rc="$donor_install_interrupt_rc"
8277
+ fi
8278
+ fi
8279
+ if (( stage_rc != 0 )); then
8280
+ _fmix_donor_abort_staging \
8281
+ "$donor_lock_name" "$stage" "$stage_rc" "$donor_lease_held"
8282
+ return $?
8283
+ fi
8284
+ # No donor bytes or stamps are read after this point; release the source
8285
+ # session lease before publishing the target's artifact-state marker.
8286
+ if ! fmix_donor_session_lease_release "$donor_lock_name" 0; then
8287
+ donor_lease_held=0
8288
+ donor_restore_rc=0
8289
+ fmix_donor_restore_interrupt_traps \
8290
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" \
8291
+ || donor_restore_rc=$?
8292
+ if (( donor_install_interrupted != 0 )); then
8293
+ donor_lease_rc="$donor_install_interrupt_rc"
8294
+ fi
8295
+ (( donor_restore_rc == 0 )) || return 70
8296
+ return 70
8297
+ fi
8298
+ donor_lease_held=0
8299
+ if (( donor_install_interrupted != 0 )); then
8300
+ _fmix_donor_abort_staging \
8301
+ "$donor_lock_name" "$stage" "$donor_install_interrupt_rc" "$donor_lease_held"
8302
+ return $?
8303
+ fi
7881
8304
 
7882
- # Rebind path and build_root in cloned stamps to target session's bound realpath
7883
- "$py_bin" - "$FMIX_ACTIVE_SESSION_ROOT" "$real" "$build_root" <<'PY' 2>/dev/null
7884
- import json, sys
7885
- from pathlib import Path
7886
- sroot = Path(sys.argv[1])
7887
- r_path = sys.argv[2]
7888
- b_root = sys.argv[3]
7889
- for name in ("compile-stamp.json", "dependency-stamp.json"):
7890
- p = sroot / name
7891
- if p.is_file():
7892
- try:
7893
- d = json.loads(p.read_text(encoding="utf-8"))
7894
- d["path"] = r_path
7895
- d["build_root"] = b_root
7896
- p.write_text(json.dumps(d, indent=2) + "\n", encoding="utf-8")
7897
- except Exception:
7898
- pass
7899
- PY
7900
- true
8305
+ stage_rc=0
8306
+ fmix_donor_stage_artifact_state \
8307
+ "$stage_new" "$worktree" "$template_key" || stage_rc=$?
8308
+ if (( donor_install_interrupted != 0 )); then
8309
+ stage_rc="$donor_install_interrupt_rc"
8310
+ fi
8311
+ if (( stage_rc != 0 )); then
8312
+ _fmix_donor_abort_staging \
8313
+ "$donor_lock_name" "$stage" "$stage_rc" "$donor_lease_held"
8314
+ return $?
8315
+ fi
7901
8316
 
7902
- # Release LOCK_SH on donor.
7903
- fmix_lock_keeper_retire "$donor_keeper_pid" || return 70
8317
+ local -a donor_item_names=(deps _build dependency-stamp.json compile-stamp.json artifact-state.json)
8318
+ local -a donor_item_targets=("$deps_root" "$build_root" "$dependency_stamp" "$compile_stamp" "$artifact_state")
8319
+ local -a donor_item_backed=()
8320
+ local -a donor_item_installed=()
8321
+ for (( index = 0; index < ${#donor_item_names[@]}; index++ )); do
8322
+ donor_item_backed[$index]=0
8323
+ donor_item_installed[$index]=0
8324
+ done
7904
8325
 
7905
- # Post-clone manifest synchronization: touch compile manifests to prevent full DAG recompiles
7906
- find "$build_root" -type f -name 'compile.*' -exec touch {} + 2>/dev/null || true
8326
+ # Stage every donor byte and provenance file before moving any active path.
8327
+ # Keep old state beside the transaction until artifact-state is installed last.
8328
+ for (( index = 0; index < ${#donor_item_names[@]}; index++ )); do
8329
+ if (( donor_install_interrupted != 0 )); then
8330
+ transaction_rc="$donor_install_interrupt_rc"
8331
+ break
8332
+ fi
8333
+ item_name="${donor_item_names[$index]}"
8334
+ item_target="${donor_item_targets[$index]}"
8335
+ if [[ -e "$item_target" || -L "$item_target" ]]; then
8336
+ move_rc=0
8337
+ fmix_donor_transaction_move \
8338
+ "$item_target" "$stage_old/$item_name" "backup:$item_name" \
8339
+ || move_rc=$?
8340
+ if [[ ! -e "$item_target" && ! -L "$item_target" \
8341
+ && ( -e "$stage_old/$item_name" || -L "$stage_old/$item_name" ) ]]; then
8342
+ donor_item_backed[$index]=1
8343
+ fi
8344
+ if (( move_rc != 0 || donor_item_backed[$index] == 0 )); then
8345
+ transaction_rc="$move_rc"
8346
+ (( transaction_rc != 0 )) || transaction_rc=1
8347
+ break
8348
+ fi
8349
+ fi
8350
+ done
8351
+ if (( donor_install_interrupted != 0 )); then
8352
+ transaction_rc="$donor_install_interrupt_rc"
8353
+ fi
8354
+ if (( transaction_rc == 0 )); then
8355
+ for (( index = 0; index < ${#donor_item_names[@]}; index++ )); do
8356
+ if (( donor_install_interrupted != 0 )); then
8357
+ transaction_rc="$donor_install_interrupt_rc"
8358
+ break
8359
+ fi
8360
+ item_name="${donor_item_names[$index]}"
8361
+ item_source="$stage_new/$item_name"
8362
+ item_target="${donor_item_targets[$index]}"
8363
+ if [[ ! -e "$item_source" && ! -L "$item_source" ]]; then
8364
+ [[ "$item_name" == "dependency-stamp.json" ]] && continue
8365
+ transaction_rc=1
8366
+ break
8367
+ fi
8368
+ move_rc=0
8369
+ fmix_donor_transaction_move \
8370
+ "$item_source" "$item_target" "install:$item_name" \
8371
+ || move_rc=$?
8372
+ if [[ ! -e "$item_source" && ! -L "$item_source" \
8373
+ && ( -e "$item_target" || -L "$item_target" ) ]]; then
8374
+ donor_item_installed[$index]=1
8375
+ fi
8376
+ if (( move_rc != 0 || donor_item_installed[$index] == 0 )); then
8377
+ transaction_rc="$move_rc"
8378
+ (( transaction_rc != 0 )) || transaction_rc=1
8379
+ break
8380
+ fi
8381
+ if (( donor_install_interrupted != 0 )); then
8382
+ transaction_rc="$donor_install_interrupt_rc"
8383
+ break
8384
+ fi
8385
+ done
8386
+ fi
8387
+ if (( donor_install_interrupted != 0 )); then
8388
+ transaction_rc="$donor_install_interrupt_rc"
8389
+ fi
8390
+
8391
+ if (( transaction_rc == 0 && donor_install_interrupted != 0 )); then
8392
+ transaction_rc="$donor_install_interrupt_rc"
8393
+ fi
8394
+ if (( transaction_rc != 0 )); then
8395
+ for (( index = ${#donor_item_names[@]} - 1; index >= 0; index-- )); do
8396
+ if (( donor_item_installed[$index] == 1 )); then
8397
+ item_name="${donor_item_names[$index]}"
8398
+ item_source="${donor_item_targets[$index]}"
8399
+ item_target="$stage_new/$item_name"
8400
+ if [[ ( -e "$item_source" || -L "$item_source" ) \
8401
+ && ! -e "$item_target" && ! -L "$item_target" ]]; then
8402
+ move_rc=0
8403
+ fmix_donor_transaction_move \
8404
+ "$item_source" "$item_target" "rollback-new:$item_name" \
8405
+ || move_rc=$?
8406
+ if [[ -e "$item_source" || -L "$item_source" \
8407
+ || ! -e "$item_target" && ! -L "$item_target" ]]; then
8408
+ rollback_failed=1
8409
+ fi
8410
+ else
8411
+ rollback_failed=1
8412
+ fi
8413
+ fi
8414
+ done
8415
+ for (( index = ${#donor_item_names[@]} - 1; index >= 0; index-- )); do
8416
+ if (( donor_item_backed[$index] == 1 )); then
8417
+ item_name="${donor_item_names[$index]}"
8418
+ item_source="$stage_old/$item_name"
8419
+ item_target="${donor_item_targets[$index]}"
8420
+ if [[ ( -e "$item_source" || -L "$item_source" ) \
8421
+ && ! -e "$item_target" && ! -L "$item_target" ]]; then
8422
+ move_rc=0
8423
+ fmix_donor_transaction_move \
8424
+ "$item_source" "$item_target" "rollback-old:$item_name" \
8425
+ || move_rc=$?
8426
+ if [[ -e "$item_source" || -L "$item_source" \
8427
+ || ! -e "$item_target" && ! -L "$item_target" ]]; then
8428
+ rollback_failed=1
8429
+ fi
8430
+ else
8431
+ rollback_failed=1
8432
+ fi
8433
+ fi
8434
+ done
8435
+ if (( donor_install_interrupted != 0 )); then
8436
+ transaction_rc="$donor_install_interrupt_rc"
8437
+ fi
8438
+ if (( rollback_failed != 0 )); then
8439
+ fmix_error "Donor clone rollback incomplete; preserving recovery inputs: $stage"
8440
+ donor_restore_rc=0
8441
+ fmix_donor_restore_interrupt_traps \
8442
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" \
8443
+ || donor_restore_rc=$?
8444
+ if (( donor_install_interrupted != 0 )); then
8445
+ transaction_rc="$donor_install_interrupt_rc"
8446
+ fi
8447
+ (( donor_restore_rc == 0 )) || return 70
8448
+ return 2
8449
+ fi
8450
+ stage_cleanup_rc=0
8451
+ fmix_donor_transaction_stage_cleanup "$stage" || stage_cleanup_rc=$?
8452
+ if (( donor_install_interrupted != 0 )); then
8453
+ transaction_rc="$donor_install_interrupt_rc"
8454
+ fi
8455
+ if (( stage_cleanup_rc != 0 )); then
8456
+ fmix_error "Could not clean rolled-back donor staging: $stage"
8457
+ fi
8458
+ donor_restore_rc=0
8459
+ fmix_donor_restore_interrupt_traps \
8460
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" \
8461
+ || donor_restore_rc=$?
8462
+ if (( donor_install_interrupted != 0 )); then
8463
+ transaction_rc="$donor_install_interrupt_rc"
8464
+ fi
8465
+ (( donor_restore_rc == 0 )) || return 70
8466
+ if (( stage_cleanup_rc != 0 )); then
8467
+ return 70
8468
+ fi
8469
+ return "$transaction_rc"
8470
+ fi
7907
8471
 
7908
- # Sanitize CMake / NIF artifacts
7909
- find "$deps_root" -type f -name 'CMakeCache.txt' -delete 2>/dev/null || true
7910
- find "$deps_root" -type d -name 'c_build' -exec rm -rf {} + 2>/dev/null || true
7911
- find "$deps_root" -type d -name 'CMakeFiles' -exec rm -rf {} + 2>/dev/null || true
8472
+ # The source lease has been released after copy and stamp rebinding. Old
8473
+ # target state remains in the transaction until final staging cleanup.
8474
+ stage_cleanup_rc=0
8475
+ fmix_donor_transaction_stage_cleanup "$stage" || stage_cleanup_rc=$?
8476
+ if (( stage_cleanup_rc != 0 )); then
8477
+ fmix_error "Donor clone committed but old transaction data remains: $stage"
8478
+ fi
8479
+ donor_restore_rc=0
8480
+ fmix_donor_restore_interrupt_traps \
8481
+ "$saved_trap_int" "$saved_trap_term" "$saved_trap_hup" \
8482
+ || donor_restore_rc=$?
8483
+ if (( donor_install_interrupted != 0 )); then
8484
+ transaction_rc="$donor_install_interrupt_rc"
8485
+ fi
8486
+ (( donor_restore_rc == 0 )) || return 70
8487
+ if (( stage_cleanup_rc != 0 )); then
8488
+ return 70
8489
+ fi
8490
+ if (( donor_install_interrupted != 0 )); then
8491
+ return "$donor_install_interrupt_rc"
8492
+ fi
7912
8493
 
7913
- # NIF path-drift heal after CoW into a new session path.
7914
8494
  fmix_check_known_nif_caches "$worktree" 2>/dev/null || true
7915
-
7916
- if (( retired_used == 1 )); then
7917
- if fmix_guard_path "$retired" && rm -rf "$retired"; then
7918
- rmdir "$(dirname "$retired")" 2>/dev/null || true
7919
- else
7920
- fmix_warn "Donor clone succeeded, but retired artifacts remain: $retired"
7921
- fi
7922
- fi
7923
8495
  fmix_info "Warm donor cloned into session."
7924
8496
  return 0
7925
8497
  }
@@ -8233,10 +8805,32 @@ _fmix_ensure_artifacts_impl() {
8233
8805
  fi
8234
8806
  # Prefer a warm peer session for the same worktree+template over a cold
8235
8807
  # template clone (avoids ~4700-file recompile when isolate/new session).
8236
- local donor=""
8237
- if donor="$(fmix_find_warm_donor_session "$worktree" "$template_key")"; then
8238
- if fmix_clone_donor_to_session "$worktree" "$donor"; then
8239
- fmix_write_artifact_state "$worktree" "$template_key" || return 1
8808
+ # A contended donor can be skipped, but the retry window is deliberately
8809
+ # small so donor discovery never becomes an unbounded session scan/clone.
8810
+ local donor="" donor_attempt donor_seen tried_donor
8811
+ local donor_attempt_limit=3 donor_attempted=0
8812
+ local -a attempted_donors=()
8813
+ for (( donor_attempt = 0; donor_attempt < donor_attempt_limit; donor_attempt++ )); do
8814
+ donor=""
8815
+ if ! donor="$(fmix_find_warm_donor_session \
8816
+ "$worktree" "$template_key" ${attempted_donors[@]+"${attempted_donors[@]}"})"; then
8817
+ break
8818
+ fi
8819
+ [[ -n "$donor" ]] || break
8820
+ donor_seen=0
8821
+ for tried_donor in ${attempted_donors[@]+"${attempted_donors[@]}"}; do
8822
+ if [[ "$tried_donor" == "$donor" ]]; then
8823
+ donor_seen=1
8824
+ break
8825
+ fi
8826
+ done
8827
+ # Be defensive against older wrappers or test doubles that do not
8828
+ # implement optional exclusions: never try the same donor twice.
8829
+ (( donor_seen == 0 )) || break
8830
+ attempted_donors+=("$donor")
8831
+ donor_attempted=$(( donor_attempted + 1 ))
8832
+
8833
+ if fmix_clone_donor_to_session "$worktree" "$donor" "$template_key"; then
8240
8834
  FMIX_LAST_ARTIFACTS_OUTCOME="donor_clone"
8241
8835
  return 0
8242
8836
  else
@@ -8246,9 +8840,13 @@ _fmix_ensure_artifacts_impl() {
8246
8840
  fmix_lock_failure_is_fatal "$donor_rc" && return 70
8247
8841
  fi
8248
8842
  # Only an ordinary donor miss permits fallback. A keeper bootstrap or
8249
- # retirement failure must not admit artifacts while recovery is pending.
8843
+ # retirement failure must not admit artifacts or attempt another donor
8844
+ # while recovery is pending.
8250
8845
  (( donor_rc == 1 )) || return "$donor_rc"
8251
- fmix_warn "Warm donor clone failed; falling back to immutable template."
8846
+ fmix_warn "Warm donor clone failed; trying another eligible donor if available."
8847
+ done
8848
+ if (( donor_attempted > 0 )); then
8849
+ fmix_warn "Warm donor attempts exhausted or unavailable; falling back to immutable template."
8252
8850
  fi
8253
8851
 
8254
8852
  FMIX_TEMPLATE_BUILT_FROM_ACTIVE_SESSION=0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calltelemetry/fmix",
3
- "version": "2.7.13",
3
+ "version": "2.7.14",
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": "2438b033c93d8b1b99e4d484edad68fd74a511048316fc30d57fda780c846222",
30
- "sourceTag": "fmix-v2.7.13"
29
+ "runnerSha256": "8ee775d32021237d69b3e1d04442f146af595d3c4538e78162389b5da76d9316",
30
+ "sourceTag": "fmix-v2.7.14"
31
31
  }
32
32
  }