@calltelemetry/fmix 2.7.25 → 2.7.27

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 +354 -8
  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.25"
31
+ FMIX_VERSION="2.7.27"
32
32
  FMIX_HOME="${FMIX_HOME:-${HOME}/.ct-fmix}"
33
33
  FMIX_OS="$(uname -s)"
34
34
  FMIX_ARCH="$(uname -m)"
@@ -62,6 +62,14 @@ FMIX_TELEMETRY_TTL_HOURS="${FMIX_TELEMETRY_TTL_HOURS:-4}"
62
62
  FMIX_TELEMETRY_TTL_MAX_HOURS="8760"
63
63
  FMIX_LOCK_INIT_GRACE="${FMIX_LOCK_INIT_GRACE:-5}"
64
64
  FMIX_LOCK_METADATA_TTL_DAYS="${FMIX_LOCK_METADATA_TTL_DAYS:-7}"
65
+ # REL-5395: content-addressed template DBs (ct_fmix_tmpl_*/ct_fmix_snap_*) are
66
+ # superseded by every migration or structure.sql change. Retire the ones unused
67
+ # for this many days, beyond the newest FMIX_TEMPLATE_KEEP per class.
68
+ FMIX_TEMPLATE_PRUNE="${FMIX_TEMPLATE_PRUNE:-1}"
69
+ FMIX_TEMPLATE_RETENTION_DAYS="${FMIX_TEMPLATE_RETENTION_DAYS:-7}"
70
+ FMIX_TEMPLATE_KEEP="${FMIX_TEMPLATE_KEEP:-3}"
71
+ FMIX_TEMPLATE_PRUNE_BATCH="${FMIX_TEMPLATE_PRUNE_BATCH:-8}"
72
+ FMIX_TEMPLATE_RETIRED_RC=201 # internal: a selected template was retired before its clone
65
73
  FMIX_WORKTREE_LOCK_TIMEOUT="${FMIX_WORKTREE_LOCK_TIMEOUT:-360}"
66
74
  # Session overlays are large (roughly 0.5 GiB each), so four hours is the
67
75
  # retention default for both automatic housekeeping and explicit GC. Preserve
@@ -7146,7 +7154,7 @@ fmix_lock_keeper_start() {
7146
7154
  local path="$1" mode="$2" timeout="$3"
7147
7155
  local diagnostic_name="${4:-lock}"
7148
7156
  local lock_name="${5:-${diagnostic_name:-lock}}"
7149
- local py_bin owner_pid owner_file state keeper_pid status keeper_source
7157
+ local py_bin owner_pid owner_file state keeper_pid status keeper_source keeper_alive keeper_rc
7150
7158
  local bootstrap_deadline acquire_deadline="" failure="keeper exited before readiness"
7151
7159
  local bootstrap_secs failure_kind="bootstrap_failed" retryable=false
7152
7160
  local probe_pid
@@ -7341,6 +7349,13 @@ finally:
7341
7349
  bootstrap_secs="$(fmix_positive_uint_or_default "${FMIX_LOCK_KEEPER_BOOTSTRAP_SECS:-30}" 30 FMIX_LOCK_KEEPER_BOOTSTRAP_SECS)"
7342
7350
  bootstrap_deadline=$(( SECONDS + bootstrap_secs ))
7343
7351
  while true; do
7352
+ # Probe liveness BEFORE reading the handshake. A keeper publishes its
7353
+ # terminal state and then exits, so a keeper that finishes between a
7354
+ # state read and a liveness probe must be classified from its final
7355
+ # state. Probing afterwards mistook ordinary flock contention ("timeout")
7356
+ # for a dead bootstrap under load.
7357
+ keeper_alive=1
7358
+ kill -0 "$keeper_pid" 2>/dev/null || keeper_alive=0
7344
7359
  if [[ -s "$state" ]]; then
7345
7360
  IFS= read -r status <"$state"
7346
7361
  case "$status" in
@@ -7369,7 +7384,12 @@ finally:
7369
7384
  ;;
7370
7385
  esac
7371
7386
  fi
7372
- if ! kill -0 "$keeper_pid" 2>/dev/null; then
7387
+ if (( keeper_alive == 0 )); then
7388
+ # Nothing terminal was published: the keeper died during bootstrap.
7389
+ # Its exit status names the real cause (crash, signal, or io failure).
7390
+ keeper_rc=0
7391
+ wait "$keeper_pid" 2>/dev/null || keeper_rc=$?
7392
+ failure="keeper exited before readiness (status $keeper_rc)"
7373
7393
  break
7374
7394
  fi
7375
7395
  if [[ -z "$acquire_deadline" ]]; then
@@ -11198,7 +11218,13 @@ fmix_create_database() {
11198
11218
  fmix_guard_db_name "$db_name" || return 1
11199
11219
  if [[ -n "$template_name" ]]; then
11200
11220
  case "$source_kind" in
11201
- published) fmix_guard_template_db_name "$template_name" || return 1 ;;
11221
+ published)
11222
+ fmix_guard_template_db_name "$template_name" || return 1
11223
+ # Stamp use BEFORE the host DDL lock: retention re-reads this
11224
+ # stamp under the same lock, so a template a clone is about to
11225
+ # use is never retired out from under it (REL-5395).
11226
+ fmix_record_template_use "$template_name"
11227
+ ;;
11202
11228
  scratch-publish) fmix_guard_publish_source_db_name "$template_name" || return 1 ;;
11203
11229
  *)
11204
11230
  fmix_error "SAFETY: unknown CREATE DATABASE source kind '$source_kind'."
@@ -11241,13 +11267,22 @@ fmix_create_database() {
11241
11267
  -v ON_ERROR_STOP=1 -c "$sql" 2>&1)"
11242
11268
  rc=$?
11243
11269
  if (( rc != 0 )); then
11244
- printf '%s\n' "$out" | tail -8 >&2
11245
- fmix_error "CREATE DATABASE $db_name failed or exceeded ${timeout}s."
11270
+ local create_fail_rc=1
11271
+ # REL-5395: a published source retired by template retention between
11272
+ # selection and this clone is a reselect, not a failed run.
11273
+ if [[ "$source_kind" == "published" && -n "$template_name" \
11274
+ && "$out" == *"template database \"${template_name}\" does not exist"* ]]; then
11275
+ fmix_warn "Template $template_name was retired before this clone; reselecting a base."
11276
+ create_fail_rc=$FMIX_TEMPLATE_RETIRED_RC
11277
+ else
11278
+ printf '%s\n' "$out" | tail -8 >&2
11279
+ fmix_error "CREATE DATABASE $db_name failed or exceeded ${timeout}s."
11280
+ fi
11246
11281
  if [[ -n "$ddl_lock" ]]; then
11247
- fmix_command_lock_finish "$ddl_lock" 1
11282
+ fmix_command_lock_finish "$ddl_lock" "$create_fail_rc"
11248
11283
  return $?
11249
11284
  fi
11250
- return 1
11285
+ return "$create_fail_rc"
11251
11286
  fi
11252
11287
  if [[ -n "$ddl_lock" ]]; then
11253
11288
  fmix_command_lock_finish "$ddl_lock" 0 || return $?
@@ -11977,10 +12012,232 @@ fmix_publish_hot_base() {
11977
12012
  fmix_command_lock_finish "$lock" 1
11978
12013
  return $?
11979
12014
  }
12015
+ fmix_record_template_use "$target"
11980
12016
  fmix_command_lock_finish "$lock" 0 || return $?
11981
12017
  printf '%s\n' "$target"
11982
12018
  }
11983
12019
 
12020
+ # ---------------------------------------------------------------------------
12021
+ # REL-5395: retired template retention
12022
+ #
12023
+ # Every migration-set change publishes a new ct_fmix_tmpl_<key> and every
12024
+ # structure.sql change builds a new ct_fmix_snap_<hash>. The orphan sweep skips
12025
+ # both prefixes by design (they are shared, not worktree-owned), so before this
12026
+ # nothing ever dropped a superseded template: 343 of them (~40 GiB) accumulated
12027
+ # on one host. Retention retires a published template only when ALL hold:
12028
+ # - its name is exactly ct_fmix_(tmpl|snap)_<16 hex> (nothing else is listed);
12029
+ # - it is not the template this run just published or cloned from;
12030
+ # - it is not among the newest FMIX_TEMPLATE_KEEP of its class by last use;
12031
+ # - it has not been used for FMIX_TEMPLATE_RETENTION_DAYS (rechecked under
12032
+ # the host DDL lock that every fmix CREATE DATABASE also takes);
12033
+ # - it has no live connection in pg_stat_activity, no lock held on it by an
12034
+ # in-flight CREATE DATABASE ... TEMPLATE, and no active CREATE DATABASE
12035
+ # statement naming it.
12036
+ # Retention is best effort: every failure is logged and skipped, and the
12037
+ # callers ignore its status, so a stuck template never fails a run.
12038
+ # ---------------------------------------------------------------------------
12039
+
12040
+ fmix_template_use_dir() {
12041
+ printf '%s/template-use\n' "$FMIX_HOME"
12042
+ }
12043
+
12044
+ fmix_is_retirable_template_name() {
12045
+ [[ "${1:-}" =~ ^ct_fmix_(tmpl|snap)_[0-9a-f]{16}$ ]]
12046
+ }
12047
+
12048
+ fmix_record_template_use() {
12049
+ local name="${1:-}" dir
12050
+ fmix_is_retirable_template_name "$name" || return 0
12051
+ dir="$(fmix_template_use_dir)"
12052
+ mkdir -p "$dir" 2>/dev/null && touch "$dir/$name" 2>/dev/null
12053
+ return 0
12054
+ }
12055
+
12056
+ fmix_template_last_used() {
12057
+ # Latest evidence of use: the use stamp, or for a hot base the publication
12058
+ # time of its db-index record. Prints 0 when neither exists.
12059
+ local name="$1" last=0 file mtime files
12060
+ files="$(fmix_template_use_dir)/$name"
12061
+ if [[ "$name" == ct_fmix_tmpl_* ]]; then
12062
+ files+=$'\n'"$(fmix_db_index_dir "${name#ct_fmix_tmpl_}")/meta.json"
12063
+ fi
12064
+ while IFS= read -r file; do
12065
+ [[ -f "$file" ]] || continue
12066
+ mtime="$(fmix_stat_mtime "$file" 2>/dev/null)"
12067
+ [[ "$mtime" =~ ^[0-9]+$ ]] || continue
12068
+ (( mtime > last )) && last="$mtime"
12069
+ done <<<"$files"
12070
+ printf '%s\n' "$last"
12071
+ }
12072
+
12073
+ fmix_select_retired_templates() {
12074
+ # Reads candidate datnames on stdin; prints the retired ones oldest first.
12075
+ # A template with no use evidence (published before retention existed, or
12076
+ # by another FMIX_HOME) starts its retention clock now instead of being
12077
+ # dropped on first sight.
12078
+ local now="$1" retention_seconds="$2" keep="$3"
12079
+ shift 3
12080
+ local name last protected p rows=""
12081
+ while IFS= read -r name; do
12082
+ name="$(printf '%s' "$name" | tr -d '[:space:]')"
12083
+ fmix_is_retirable_template_name "$name" || continue
12084
+ protected=0
12085
+ for p in "$@"; do
12086
+ [[ "$p" == "$name" ]] && protected=1
12087
+ done
12088
+ last="$(fmix_template_last_used "$name")"
12089
+ if [[ ! "$last" =~ ^[1-9][0-9]*$ ]]; then
12090
+ [[ "${FMIX_TEMPLATE_SELECT_REPORT_ONLY:-0}" == "1" ]] || fmix_record_template_use "$name"
12091
+ last="$now"
12092
+ fi
12093
+ rows+="${name%_*} ${last} ${protected} ${name}"$'\n'
12094
+ done
12095
+ [[ -n "$rows" ]] || return 0
12096
+ printf '%s' "$rows" \
12097
+ | LC_ALL=C sort -k1,1 -k2,2nr -k4,4 \
12098
+ | awk -v now="$now" -v ret="$retention_seconds" -v keep="$keep" '
12099
+ $1 != class { class = $1; rank = 0 }
12100
+ { rank++ }
12101
+ rank <= keep { next }
12102
+ $3 == 1 { next }
12103
+ now - $2 <= ret { next }
12104
+ { print $2 " " $4 }' \
12105
+ | LC_ALL=C sort -k1,1n -k2,2 \
12106
+ | cut -d' ' -f2
12107
+ }
12108
+
12109
+ fmix_drop_retired_template() {
12110
+ # Returns 0 dropped, 1 failed (logged), 3 skipped because it is in use or
12111
+ # no longer a published template. Never terminates a backend.
12112
+ local name="$1" state conns locks creates out
12113
+ fmix_is_retirable_template_name "$name" || {
12114
+ fmix_warn "SAFETY: refusing to retire '$name' — not an fmix tmpl/snap template name."
12115
+ return 1
12116
+ }
12117
+ fmix_guard_template_db_name "$name" || return 1
12118
+ state="$(fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -tAc "
12119
+ SELECT (SELECT count(*) FROM pg_stat_activity a
12120
+ WHERE a.datname = d.datname AND a.pid <> pg_backend_pid())
12121
+ || '|' ||
12122
+ (SELECT count(*) FROM pg_locks l
12123
+ WHERE l.locktype = 'object' AND l.classid = 'pg_database'::regclass
12124
+ AND l.objid = d.oid AND l.pid <> pg_backend_pid())
12125
+ || '|' ||
12126
+ (SELECT count(*) FROM pg_stat_activity a
12127
+ WHERE a.pid <> pg_backend_pid() AND a.state IS DISTINCT FROM 'idle'
12128
+ AND a.query ~* 'create[[:space:]]+database'
12129
+ AND strpos(a.query, d.datname) > 0)
12130
+ FROM pg_database d
12131
+ WHERE d.datname = '${name}' AND d.datistemplate AND NOT d.datallowconn" \
12132
+ 2>/dev/null | tr -d '[:space:]')"
12133
+ if [[ -z "$state" ]]; then
12134
+ return 3
12135
+ fi
12136
+ if [[ ! "$state" =~ ^([0-9]+)\|([0-9]+)\|([0-9]+)$ ]]; then
12137
+ fmix_warn "Template retention could not read activity for $name; keeping it."
12138
+ return 1
12139
+ fi
12140
+ conns="${BASH_REMATCH[1]}" locks="${BASH_REMATCH[2]}" creates="${BASH_REMATCH[3]}"
12141
+ if (( conns > 0 )); then
12142
+ fmix_warn "Keeping retired template $name: $conns live connection(s)."
12143
+ return 3
12144
+ fi
12145
+ if (( locks > 0 || creates > 0 )); then
12146
+ fmix_warn "Keeping retired template $name: an in-flight CREATE DATABASE is using it."
12147
+ return 3
12148
+ fi
12149
+ # PostgreSQL refuses to drop a template database; clear the flag first.
12150
+ # ALLOW_CONNECTIONS stays false, so nobody can connect in between.
12151
+ if ! fmix_control_psql 10 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
12152
+ -v ON_ERROR_STOP=1 -c "ALTER DATABASE \"${name}\" IS_TEMPLATE false" >/dev/null 2>&1; then
12153
+ fmix_warn "Could not unmark retired template $name; keeping it."
12154
+ return 1
12155
+ fi
12156
+ if ! out="$(fmix_control_psql 10 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
12157
+ -v ON_ERROR_STOP=1 -c "DROP DATABASE IF EXISTS \"${name}\"" 2>&1)"; then
12158
+ # Restore the published state so readiness checks keep trusting it.
12159
+ fmix_control_psql 10 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
12160
+ -c "ALTER DATABASE \"${name}\" IS_TEMPLATE true" >/dev/null 2>&1 \
12161
+ || fmix_warn "Could not restore IS_TEMPLATE on $name after a failed drop."
12162
+ fmix_warn "Could not drop retired template $name; keeping it: $(printf '%s' "$out" | tail -1)"
12163
+ return 1
12164
+ fi
12165
+ rm -f "$(fmix_template_use_dir)/$name" 2>/dev/null
12166
+ if [[ "$name" == ct_fmix_tmpl_* ]]; then
12167
+ local key="${name#ct_fmix_tmpl_}" dir
12168
+ dir="$(fmix_db_index_dir "$key")"
12169
+ if [[ -d "$dir" && "$(fmix_read_db_index_field "$key" base_id)" == "$name" ]] \
12170
+ && fmix_guard_path "$dir" 2>/dev/null; then
12171
+ rm -rf "$dir" 2>/dev/null \
12172
+ || fmix_warn "Dropped $name but could not remove its db-index record."
12173
+ fi
12174
+ fi
12175
+ return 0
12176
+ }
12177
+
12178
+ fmix_list_published_templates() {
12179
+ fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -tAc \
12180
+ "SELECT datname FROM pg_database WHERE datistemplate AND NOT datallowconn AND datname ~ '^ct_fmix_(tmpl|snap)_[0-9a-f]{16}\$' ORDER BY datname" \
12181
+ 2>/dev/null
12182
+ }
12183
+
12184
+ fmix_prune_retired_template_dbs() {
12185
+ # Usage: fmix_prune_retired_template_dbs <context> <batch|0=all> [protected...]
12186
+ # Best effort by contract: always returns 0.
12187
+ local context="$1" batch="$2"
12188
+ shift 2
12189
+ case "$FMIX_TEMPLATE_PRUNE" in
12190
+ 0|false|FALSE|no|NO|off|OFF) return 0 ;;
12191
+ esac
12192
+ local days keep retention_seconds now listing candidates name
12193
+ local dropped=0 kept=0 failed=0 attempted=0 rc ddl_lock="" last
12194
+ days="$(fmix_uint_or_default "$FMIX_TEMPLATE_RETENTION_DAYS" 7 FMIX_TEMPLATE_RETENTION_DAYS)"
12195
+ keep="$(fmix_uint_or_default "$FMIX_TEMPLATE_KEEP" 3 FMIX_TEMPLATE_KEEP)"
12196
+ retention_seconds=$(( days * 86400 ))
12197
+ now="$(fmix_epoch)"
12198
+ listing="$(fmix_list_published_templates)" || {
12199
+ fmix_warn "Template retention ($context): could not list template databases; skipping."
12200
+ return 0
12201
+ }
12202
+ candidates="$(printf '%s\n' "$listing" \
12203
+ | fmix_select_retired_templates "$now" "$retention_seconds" "$keep" "$@")"
12204
+ [[ -n "$candidates" ]] || return 0
12205
+ fmix_host_test_lock_enabled && ddl_lock="$(fmix_host_test_lock_name)"
12206
+ while IFS= read -r name; do
12207
+ [[ -n "$name" ]] || continue
12208
+ if [[ "$batch" =~ ^[1-9][0-9]*$ ]] && (( attempted >= batch )); then
12209
+ break
12210
+ fi
12211
+ attempted=$(( attempted + 1 ))
12212
+ if [[ -n "$ddl_lock" ]] && ! fmix_lock_acquire "$ddl_lock" 5 >/dev/null 2>&1; then
12213
+ fmix_warn "Template retention ($context): host DDL lock is busy; deferring the rest."
12214
+ break
12215
+ fi
12216
+ # A clone stamps use before it waits for this lock; recheck under it.
12217
+ last="$(fmix_template_last_used "$name")"
12218
+ if [[ "$last" =~ ^[0-9]+$ ]] && (( now - last <= retention_seconds )); then
12219
+ rc=3
12220
+ else
12221
+ rc=0
12222
+ fmix_drop_retired_template "$name" || rc=$?
12223
+ fi
12224
+ if [[ -n "$ddl_lock" ]] && ! fmix_lock_release "$ddl_lock" >/dev/null 2>&1; then
12225
+ fmix_warn "Template retention ($context): could not release the host DDL lock; stopping."
12226
+ case "$rc" in 0) dropped=$(( dropped + 1 )) ;; 3) kept=$(( kept + 1 )) ;; *) failed=$(( failed + 1 )) ;; esac
12227
+ break
12228
+ fi
12229
+ case "$rc" in
12230
+ 0) dropped=$(( dropped + 1 )); fmix_info "Template retention ($context): dropped retired template $name." ;;
12231
+ 3) kept=$(( kept + 1 )) ;;
12232
+ *) failed=$(( failed + 1 )) ;;
12233
+ esac
12234
+ done <<<"$candidates"
12235
+ if (( dropped + kept + failed > 0 )); then
12236
+ fmix_info "Template retention ($context): dropped=$dropped kept_in_use=$kept failed=$failed."
12237
+ fi
12238
+ return 0
12239
+ }
12240
+
11984
12241
  fmix_migrate_from_zero() {
11985
12242
  # API-2846: bootstrap fallback for when structure.sql is unusable for
11986
12243
  # cold bootstrap (missing, or fails fmix_structure_matches_migration_
@@ -12288,6 +12545,13 @@ fmix_ensure_db() {
12288
12545
  rc=0
12289
12546
  _fmix_ensure_db_impl "$@" || rc=$?
12290
12547
  fi
12548
+ if (( rc == FMIX_TEMPLATE_RETIRED_RC )); then
12549
+ # REL-5395: the selected template was retired concurrently and its
12550
+ # db-index record went with it. Nothing was created; select once more.
12551
+ rc=0
12552
+ _fmix_ensure_db_impl "$@" || rc=$?
12553
+ (( rc != FMIX_TEMPLATE_RETIRED_RC )) || rc=1
12554
+ fi
12291
12555
  return "$rc"
12292
12556
  }
12293
12557
 
@@ -12397,6 +12661,7 @@ _fmix_ensure_db_impl() {
12397
12661
  if [[ -n "$selection" ]]; then
12398
12662
  IFS=$'\t' read -r base base_key delta <<<"$selection"
12399
12663
  fmix_guard_template_db_name "$base" || return 1
12664
+ fmix_record_template_use "$base"
12400
12665
  fmix_info "Selected $base from metadata (schema=$base_key, delta=$delta); performing one clone."
12401
12666
  fmix_create_database "$db_name" "$base" || return $?
12402
12667
  FMIX_ACTIVE_DB_CLEAN_BOOTSTRAP=1
@@ -12407,6 +12672,7 @@ _fmix_ensure_db_impl() {
12407
12672
  local snapshot_rc=1
12408
12673
  if [[ "$force_from_zero" != "1" ]]; then
12409
12674
  snap="$(fmix_ensure_snapshot_template "$worktree")" && snapshot_rc=0 || snapshot_rc=$?
12675
+ (( snapshot_rc != 0 )) || fmix_record_template_use "$snap"
12410
12676
  (( snapshot_rc == 0 || snapshot_rc == 1 )) || return "$snapshot_rc"
12411
12677
  fi
12412
12678
  if (( snapshot_rc == 0 )); then
@@ -16219,6 +16485,14 @@ cmd_setup() {
16219
16485
  fmix_error "Setup completion failed (template access rc=$setup_access_rc, final rc=$setup_finish_rc)."
16220
16486
  return "$setup_finish_rc"
16221
16487
  fi
16488
+ # REL-5395: a publish supersedes older templates. Retire a bounded batch
16489
+ # now, after the session lock is released; best effort, never fatal.
16490
+ if [[ "${FMIX_ACTIVE_DB_CLEAN_BOOTSTRAP:-0}" == "1" ]]; then
16491
+ # Subshell: lock-keeper state from a failed acquire cannot leak into
16492
+ # this run's exit status.
16493
+ ( fmix_prune_retired_template_dbs setup "$FMIX_TEMPLATE_PRUNE_BATCH" \
16494
+ "$published_base" "${FMIX_ACTIVE_DB_BASE_ID:-}" ) || true
16495
+ fi
16222
16496
  if [[ "${FMIX_ACTIVE_DB_CLEAN_BOOTSTRAP:-0}" == "1" ]]; then
16223
16497
  fmix_info "=== setup complete; hot DB base: $published_base ==="
16224
16498
  else
@@ -18439,6 +18713,39 @@ cmd_runner() {
18439
18713
  # cmd: fmix task
18440
18714
  # ---------------------------------------------------------------------------
18441
18715
 
18716
+ fmix_task_provision_test_db() {
18717
+ # Artifacts and the scratch DB are separate lifecycles. A session created by
18718
+ # `fmix task` clones artifacts and records db_name in worktree state without
18719
+ # creating ct_fmix_s_<session>, and every later task then matched the warm
18720
+ # artifacts and ran Mix against a missing database. Provision the session DB
18721
+ # through the same fmix_ensure_db path `fmix test` uses, before Mix starts.
18722
+ # An explicit --database name is verified and never created here.
18723
+ local worktree="$1" db_name="$2" task_db_name="$3"
18724
+ if ! fmix_control_psql 5 -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
18725
+ -c "SELECT 1" >/dev/null 2>&1; then
18726
+ fmix_error "Postgres at ${DB_HOST}:${DB_PORT} is unreachable; fmix task cannot verify session DB ${task_db_name}."
18727
+ fmix_error "Start the Postgres container (ct-infra-postgres) and retry. Mix was not started."
18728
+ return 1
18729
+ fi
18730
+ if [[ "$task_db_name" != "$db_name" ]]; then
18731
+ if fmix_db_exists "$task_db_name"; then
18732
+ return 0
18733
+ fi
18734
+ fmix_error "Named database ${task_db_name} does not exist. fmix task does not create an explicit --database. Mix was not started."
18735
+ return 1
18736
+ fi
18737
+ if ! fmix_db_exists "$db_name"; then
18738
+ fmix_warn "Session DB ${db_name} is missing; provisioning it as fmix test does."
18739
+ fi
18740
+ fmix_ensure_db "$worktree" "$db_name" 0 0 0 || {
18741
+ local ensure_rc=$?
18742
+ fmix_error "Session DB ${db_name} is missing and could not be provisioned (rc=${ensure_rc}). Mix was not started."
18743
+ fmix_error "Reprovision it with: fmix test --worktree ${worktree} -- <test file>"
18744
+ return "$ensure_rc"
18745
+ }
18746
+ return 0
18747
+ }
18748
+
18442
18749
  cmd_task() {
18443
18750
  # Run a non-test Mix task against the same exact pinned runtime and
18444
18751
  # session-local incremental deps/_build state as fmix setup/test. This is
@@ -18549,6 +18856,13 @@ cmd_task() {
18549
18856
  fmix_command_lock_finish "$lock_name" "$task_rc" 1
18550
18857
  return $?
18551
18858
  }
18859
+ if [[ "$mix_env_arg" == "test" ]]; then
18860
+ fmix_task_provision_test_db "$real_worktree" "$db_name" "$task_db_name" || {
18861
+ task_rc=$?
18862
+ fmix_command_lock_finish "$lock_name" "$task_rc" 1
18863
+ return $?
18864
+ }
18865
+ fi
18552
18866
 
18553
18867
  local -a build_env=()
18554
18868
  [[ -n "${MIX_BUILD_PATH:-}" ]] && build_env+=("MIX_BUILD_PATH=$MIX_BUILD_PATH")
@@ -22408,6 +22722,34 @@ cmd_gc() {
22408
22722
  done <<<"$all_fmix_dbs"
22409
22723
  fi
22410
22724
 
22725
+ # REL-5395: superseded content-addressed templates. Report the retired set;
22726
+ # --reap drops it through the guarded, best-effort retention path.
22727
+ echo ""
22728
+ echo "--- Retired template DBs (unused > ${FMIX_TEMPLATE_RETENTION_DAYS} days, beyond newest ${FMIX_TEMPLATE_KEEP} per class) ---"
22729
+ local retired_templates retired_days retired_keep
22730
+ retired_days="$(fmix_uint_or_default "$FMIX_TEMPLATE_RETENTION_DAYS" 7 FMIX_TEMPLATE_RETENTION_DAYS)"
22731
+ retired_keep="$(fmix_uint_or_default "$FMIX_TEMPLATE_KEEP" 3 FMIX_TEMPLATE_KEEP)"
22732
+ retired_templates="$(fmix_list_published_templates \
22733
+ | FMIX_TEMPLATE_SELECT_REPORT_ONLY=$(( 1 - do_reap )) fmix_select_retired_templates \
22734
+ "$(fmix_epoch)" "$(( retired_days * 86400 ))" "$retired_keep")"
22735
+ if [[ -n "$retired_templates" ]]; then
22736
+ while IFS= read -r pgdb; do
22737
+ [[ -n "$pgdb" ]] || continue
22738
+ echo " RETIRED-TEMPLATE: $pgdb"
22739
+ actions=$(( actions + 1 ))
22740
+ done <<<"$retired_templates"
22741
+ if (( do_reap == 1 )); then
22742
+ local confirm_rt="y"
22743
+ if (( skip_prompts == 0 )); then
22744
+ printf ' Drop retired templates that are not in use? [y/N] '
22745
+ read -r confirm_rt
22746
+ fi
22747
+ if [[ "$confirm_rt" =~ ^[Yy]$ ]]; then
22748
+ ( fmix_prune_retired_template_dbs gc 0 ) || true
22749
+ fi
22750
+ fi
22751
+ fi
22752
+
22411
22753
  # Incomplete producer staging can still be a recovery/verifier input.
22412
22754
  # Age alone never releases it, including through the legacy mixed GC.
22413
22755
  echo ""
@@ -27726,6 +28068,10 @@ Environment overrides:
27726
28068
  FMIX_DB_PRESSURE_MIN_AGE Sustained wait age in seconds (default: 10)
27727
28069
  FMIX_DB_CREATE_TIMEOUT CREATE DATABASE statement cap in seconds (default: 60)
27728
28070
  FMIX_DB_DROP_TIMEOUT DROP DATABASE statement cap in seconds (default: 30)
28071
+ FMIX_TEMPLATE_PRUNE Set to 0 to disable retired template retention (default: 1)
28072
+ FMIX_TEMPLATE_RETENTION_DAYS Retire tmpl/snap templates unused this long (default: 7)
28073
+ FMIX_TEMPLATE_KEEP Newest tmpl/snap templates always kept per class (default: 3)
28074
+ FMIX_TEMPLATE_PRUNE_BATCH Retired templates dropped per setup (default: 8; gc --reap drops all)
27729
28075
  FMIX_MIGRATION_DELTA_LIMIT Maximum migrations applied from a selected base (default: 50)
27730
28076
  FMIX_COMPILE_HEARTBEAT Silent compile heartbeat seconds (default: 30; 0 disables)
27731
28077
  FMIX_COMPILE_TIMEOUT Per-attempt compile cap in seconds (default: 900)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calltelemetry/fmix",
3
- "version": "2.7.25",
3
+ "version": "2.7.27",
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": "0a78d4b1ae469d91558f9d881df1d56802bc4fb9840f2d8603657ff004b336ae",
30
- "sourceTag": "fmix-v2.7.25"
29
+ "runnerSha256": "79915aeb7ff03adeb3308c73fa5363b4d6fa59ace54813a607c0e43ec500f028",
30
+ "sourceTag": "fmix-v2.7.27"
31
31
  }
32
32
  }