The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(install-metrics): one receipt per install run, interrupted runs counted, latch after save
Tekniumfaf3a1e5c094
diff --git a/hermes_cli/observability/shared_metrics_install_run.py b/hermes_cli/observability/shared_metrics_install_run.pyindex e82047e40d5..4fce3375312 100644--- a/hermes_cli/observability/shared_metrics_install_run.py+++ b/hermes_cli/observability/shared_metrics_install_run.py@@ -12,7 +12,6 @@ from __future__ import annotationsimport jsonimport logging-import osimport reimport shutilfrom pathlib import Path@@ -68,21 +67,20 @@ def install_run_fields(receipt: Any) -> dict[str, str] | None:return fields-def _claim_receipt_id(home: Path, receipt_id: str) -> Path | None:- """The new latch for ``receipt_id``, or None when this profile already counted it."""- directory = home / "telemetry" / "shared_metrics" / RECORDED_DIRNAME- directory.mkdir(parents=True, exist_ok=True)- latch = directory / receipt_id- try:- os.close(os.open(latch, os.O_CREAT | os.O_EXCL | os.O_WRONLY))- except FileExistsError:- return None+def _recorded_latch(home: Path, receipt_id: str) -> Path:+ return home / "telemetry" / "shared_metrics" / RECORDED_DIRNAME / receipt_id+++def _mark_recorded(latch: Path) -> None:+ """Latch a receipt id AFTER its row is saved: a process that dies between the latch and the save+ (a short-lived start whose reporter thread is cut off at exit) must not drop the install."""try:- for stale in sorted(directory.iterdir(), key=lambda p: p.stat().st_mtime, reverse=True)[_RECORDED_KEEP:]:+ latch.parent.mkdir(parents=True, exist_ok=True)+ latch.touch()+ for stale in sorted(latch.parent.iterdir(), key=lambda p: p.stat().st_mtime, reverse=True)[_RECORDED_KEEP:]:stale.unlink(missing_ok=True)- except OSError: # a concurrent prune won; the next claim prunes again+ except OSError: # a concurrent prune won; the next latch prunes againpass- return latchdef report_pending_installs(home: Path) -> None:@@ -109,13 +107,14 @@ def report_pending_installs(home: Path) -> None:if fields is None or not isinstance(receipt, dict): # out of contract or unreadablesettle_claim(claimed, path, True)continue- latch = _claim_receipt_id(home, receipt["id"])- if latch is None: # counted before; only the delete had failed+ latch = _recorded_latch(home, receipt["id"])+ if latch.exists(): # counted before; only the delete had failedsettle_claim(claimed, path, True)continue+ # The claim rename already keeps concurrent starts apart; a busy store keeps the receipt.saved = emit_saved([(contract.INSTALL_RUN_MARK, fields)]) == 1- if not saved:- latch.unlink(missing_ok=True) # nothing landed: the next start counts it+ if saved:+ _mark_recorded(latch)settle_claim(claimed, path, saved)except Exception:logger.debug("Pending install receipts not reported", exc_info=True)diff --git a/scripts/install.ps1 b/scripts/install.ps1index 31e04987614..06b3bd94d02 100644--- a/scripts/install.ps1+++ b/scripts/install.ps1@@ -742,6 +742,8 @@ function Fail([string]$msg, [string]$Class = "other") {# a later start only while shared metrics collection is on, and deletes it# unreported when collection is off. Best effort: never fails the install.function Write-InstallReceipt([string]$Outcome, [string]$FailedStage, [string]$Class) {+ if ($script:InstallReceiptWritten) { return }+ $script:InstallReceiptWritten = $truetry {$dir = Join-Path $HermesHome "telemetry\shared_metrics\pending_installs"New-Item -ItemType Directory -Force -Path $dir | Out-Null@@ -1385,6 +1387,7 @@ if ($Stage) {# manifest prints, so -IncludeDesktop inserts desktop here too.$script:InstallStarted = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()$script:InstallStage = "prerequisites"+$script:InstallReceiptWritten = $falsetry {Write-Bannerforeach ($s in $Stages) {@@ -1400,4 +1403,7 @@ try {if ($script:RunAsFile) { exit 1 }# Under iex: report failure without closing the user's window.$global:LASTEXITCODE = 1+} finally {+ # Ctrl-C stops the run without reaching catch; finally still runs (no-op after a receipt).+ Write-InstallReceipt "failed" $script:InstallStage "interrupted"}diff --git a/scripts/install.sh b/scripts/install.shindex b76300be230..74455f3c587 100755--- a/scripts/install.sh+++ b/scripts/install.sh@@ -35,6 +35,7 @@ SKIP_BROWSER=falseSKIP_COMPUTER_USE=falseINSTALL_LADDER=falseINSTALL_STARTED=0+INSTALL_RUN_ID=""FAILURE_CLASS=""while [ $# -gt 0 ]; do@@ -107,13 +108,12 @@ fail() { STAGE_REASON="$1"; FAILURE_CLASS="${2:-other}"; log_error "$1"; exit 1;# later start only while shared metrics collection is on, and deletes it# unreported when collection is off. Best effort: never fails the install.write_install_receipt() {- # $1 outcome, $2 failed stage, $3 failure class+ # $1 outcome, $2 failed stage, $3 failure class. One file per run: the first writer wins.{- local dir="$HERMES_HOME/telemetry/shared_metrics/pending_installs" id- id="$(od -An -N16 -tx1 /dev/urandom 2>/dev/null | tr -d ' \n')"- [ "${#id}" -eq 32 ] || id="$(printf '%08x%08x%08x' "$RANDOM$RANDOM" "$$" "$(date +%s)")"- mkdir -p "$dir" && printf '{"id":"%s","installer":"install_sh","outcome":"%s","failed_stage":"%s","failure_class":"%s","started_at":%s,"finished_at":%s}\n' \- "$id" "$1" "$2" "$3" "${INSTALL_STARTED:-0}" "$(date +%s)" > "$dir/.$id.tmp" \+ local dir="$HERMES_HOME/telemetry/shared_metrics/pending_installs" id="$INSTALL_RUN_ID"+ [ -n "$id" ] && [ ! -e "$dir/$id.json" ] && mkdir -p "$dir" \+ && printf '{"id":"%s","installer":"install_sh","outcome":"%s","failed_stage":"%s","failure_class":"%s","started_at":%s,"finished_at":%s}\n' \+ "$id" "$1" "$2" "$3" "$INSTALL_STARTED" "$(date +%s)" > "$dir/.$id.tmp" \&& mv -f "$dir/.$id.tmp" "$dir/$id.json"} >/dev/null 2>&1 || :}@@ -399,9 +399,8 @@ stage_result() {if [ "$JSON" = true ]; thenjson_frame "$ok" "$STAGE" "${STAGE_SKIPPED:-false}" "$reason"fi- if [ "$ok" = false ] && [ "${INSTALL_LADDER:-false}" = true ]; then- # 130/143: Ctrl-C or SIGTERM while a stage ran, not a stage defect.- case "$code" in 130|143) [ -n "${FAILURE_CLASS:-}" ] || FAILURE_CLASS=interrupted ;; esac+ # 130/143 (Ctrl-C, SIGTERM) are recorded by the ladder as `interrupted`.+ if [ "$ok" = false ] && [ "${INSTALL_LADDER:-false}" = true ] && [ "$code" -ne 130 ] && [ "$code" -ne 143 ]; thenwrite_install_receipt failed "${STAGE//-/_}" "${FAILURE_CLASS:-other}"fi}@@ -926,6 +925,8 @@ if [ "${BASH_SOURCE[0]:-$0}" = "$0" ]; thenelif [ -z "$STAGE" ]; thenINSTALL_LADDER=trueINSTALL_STARTED="$(date +%s)"+ INSTALL_RUN_ID="$(od -An -N16 -tx1 /dev/urandom 2>/dev/null | tr -d ' \n')" || INSTALL_RUN_ID=""+ [ "${#INSTALL_RUN_ID}" -eq 32 ] || INSTALL_RUN_ID="$(printf '%08x%08x%08x%08x' "$RANDOM$RANDOM" "$$" "$INSTALL_STARTED" "$RANDOM")"trap '[ "$?" -eq 0 ] || write_install_receipt failed prerequisites "${FAILURE_CLASS:-other}"' EXITficheck_platform@@ -939,11 +940,15 @@ if [ "${BASH_SOURCE[0]:-$0}" = "$0" ]; then# No --stage: run the whole ladder — the same authoritative list the# manifest prints, so --include-desktop inserts desktop here too.print_banner+ # Ctrl-C / SIGTERM (a closed terminal) end the install mid-stage: one `interrupted` receipt.+ trap 'write_install_receipt failed "${s//-/_}" interrupted; exit 130' INT+ trap 'write_install_receipt failed "${s//-/_}" interrupted; exit 143' TERMfor s in $(stage_names); dorun_stage "$s"rc=$?- [ "$rc" -eq 0 ] || exit "$rc"+ case "$rc" in 0) ;; 130|143) write_install_receipt failed "${s//-/_}" interrupted; exit "$rc" ;; *) exit "$rc" ;; esacdone+ trap - INT TERMwrite_install_receipt success none noneprint_path_reload_hintfi