From 115355a38bd60322d9452d9f9ac9c58d2709470a Mon Sep 17 00:00:00 2001 From: garrytan Date: Wed, 28 Oct 2026 11:03:05 +0000 Subject: [PATCH] perf(doctor): cut Git Bash process starts in doctor, hook-check and launch probe (GSTA-138) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- bin/gstack-doctor | 17 ++++++++++++----- bin/gstack-doctor-check.sh | 22 +++++++++++----------- bin/gstack-hook-check | 21 +++++++++++++++++---- bin/gstack-launch-probe.sh | 5 ++++- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/bin/gstack-doctor b/bin/gstack-doctor index 9e18a5a38..94e3ce18d 100755 --- a/bin/gstack-doctor +++ b/bin/gstack-doctor @@ -93,7 +93,14 @@ row() { sq() { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"; } # clamp CHARSET MAX — stdin's first line reduced to CHARSET, at most MAX chars. -clamp() { head -1 | tr -cd "$1" | cut -c1-"$2"; } +# Builtins only, bytewise like tr and cut: every caller pipes into it, and on +# Windows a head | tr | cut chain costs three process starts (~60 ms). +clamp() { local LC_ALL=C line pat="[^$1]"; IFS= read -r line; line=${line//$pat/}; printf '%s' "${line:0:$2}"; } + +# cfg KEY DEFAULT — sets _cfg to KEY as `gstack-config get` reads it, or DEFAULT +# (gstack-config's own default) when unset. In-process through +# gstack-state-root.sh, sourced below: a gstack-config start costs ~200 ms on Windows. +cfg() { gstack_config_select "$1" 2>/dev/null; _cfg="${_gstack_cfg_value:-$2}"; } # epoch_of VALUE — epoch seconds for an epoch or an ISO-8601 UTC time; "" otherwise. epoch_of() { @@ -202,7 +209,7 @@ check_hooks() { check_codex() { local reviews probe="$BIN/gstack-codex-probe" sel rc model src CODEX_READY=0 - reviews=$("$BIN/gstack-config" get codex_reviews 2>/dev/null || echo enabled) + cfg codex_reviews enabled; reviews=$_cfg if [ "$reviews" = disabled ]; then row "not configured" codex "codex_reviews disabled; $CODEX_FEATURES skip Codex" "$BIN/gstack-config set codex_reviews enabled" return @@ -269,7 +276,7 @@ check_codex_probe() { check_sync() { local mode file json status held drainable drain push queue=0 now f detail case_names fix="$BIN/gstack-brain-sync --status" - mode=$("$BIN/gstack-config" get artifacts_sync_mode 2>/dev/null || echo off) + cfg artifacts_sync_mode off; mode=$_cfg if [ ! -d "$STATE_ROOT/.git" ]; then row "not configured" "artifacts sync" "off; cross-machine artifacts sync is unavailable" "$BIN/gstack-artifacts-init" return @@ -373,7 +380,7 @@ check_binaries() { check_cso() { local file="$SRC/bin/.gstack-cso-build-result" result stage reason installed diag usable=0 [ -x "$SRC/bin/gstack-cso-launcher$EXE" ] && [ -x "$SRC/bin/gstack-cso-core$EXE" ] && usable=1 - cso_field() { sed -n "s/^$1=//p" "$file" 2>/dev/null | head -1 | clamp ' -~' 200; } + cso_field() { sed -n "s/^$1=//p" "$file" 2>/dev/null | clamp ' -~' 200; } result=$(cso_field result) stage=$(cso_field stage) reason=$(cso_field reason) installed=$(cso_field installed) diag=$(cso_field diagnostic) case "$result" in ok) if [ "$usable" -eq 1 ]; then row ok cso "native helper $installed" @@ -603,7 +610,7 @@ if [ "$CHECK" -eq 1 ]; then exit $? fi -echo "gstack doctor $(head -1 "$ROOT/VERSION" 2>/dev/null | clamp '0-9A-Za-z.+-' 40)" +echo "gstack doctor $(clamp '0-9A-Za-z.+-' 40 2>/dev/null <"$ROOT/VERSION")" check_install check_rescue_refs check_state_root diff --git a/bin/gstack-doctor-check.sh b/bin/gstack-doctor-check.sh index b11f20f71..0cb0d5956 100644 --- a/bin/gstack-doctor-check.sh +++ b/bin/gstack-doctor-check.sh @@ -2,7 +2,7 @@ # gstack-doctor-check.sh — `gstack-doctor --check`: the one-line install verdict. # # Sourced by bin/gstack-doctor after its prelude (ROOT, ROOT_REAL, BIN, SRC, -# DOCS, STATE_ROOT, EXE, clamp, the launch probe, the codex status helpers) +# DOCS, STATE_ROOT, EXE, clamp, cfg, the launch probe, the codex status helpers) # and never executed on its own. Prints `required: `, one # `PASS | FAIL | SKIP ` row per component in bin/gstack-doctor-components.sh # (generated from lib/doctor-components.ts; the same ids feed --for, --require @@ -142,7 +142,7 @@ _dc_check_patch() { fi # A tier 2+ render keeps the patch in the shared sections/preamble.md its SKILL.md points to. for f in "$file" "${file%/SKILL.md}/sections/preamble.md"; do - model=$(grep -F 'Model-Specific Behavioral Patch (' "$f" 2>/dev/null | head -1 | sed 's/.*Model-Specific Behavioral Patch (\([^)]*\)).*/\1/' | clamp 'A-Za-z0-9._:/-' 60) + model=$(grep -F 'Model-Specific Behavioral Patch (' "$f" 2>/dev/null | sed 's/.*Model-Specific Behavioral Patch (\([^)]*\)).*/\1/' | clamp 'A-Za-z0-9._:/-' 60) [ -n "$model" ] && { file="$f"; break; } done if [ -n "$model" ]; then @@ -154,7 +154,7 @@ _dc_check_patch() { _dc_check_codex_cli() { local probe="$BIN/gstack-codex-probe" reviews sel rc model out state optional_note="optional; a Claude subagent serves as the outside voice" - reviews=$("$BIN/gstack-config" get codex_reviews 2>/dev/null || echo enabled) + cfg codex_reviews enabled; reviews=$_cfg if ! command -v codex >/dev/null 2>&1; then _dc_fail_or_skip codex-cli "Codex CLI not installed" "npm install -g @openai/codex, then codex login" COMPONENT_MISSING "$optional_note; Codex CLI not installed" return @@ -228,7 +228,7 @@ _dc_check_browser() { if [ "$rc" -eq 0 ] && printf '%s\n' "$out" | grep -q '^RENDER_OK'; then _dc_row PASS browser "Chromium $(printf '%s\n' "$out" | sed -n 's/^RENDER_OK //p' | clamp '0-9.' 20) launched and rendered ($engine)" else - _dc_row FAIL browser "Chromium did not launch and render via $engine (exit $rc: $(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | head -1 | clamp ' -~' 160))" "$fix" BROWSER_UNAVAILABLE + _dc_row FAIL browser "Chromium did not launch and render via $engine (exit $rc: $(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | clamp ' -~' 160))" "$fix" BROWSER_UNAVAILABLE fi } @@ -238,7 +238,7 @@ _dc_check_cso() { _dc_row PASS cso "native helper built" return fi - [ -f "$file" ] && reason=$(sed -n 's/^reason=//p' "$file" 2>/dev/null | head -1 | clamp ' -~' 100) + [ -f "$file" ] && reason=$(sed -n 's/^reason=//p' "$file" 2>/dev/null | clamp ' -~' 100) _dc_fail_or_skip cso "native helper not built${reason:+ ($reason)}" "cd $SRC && ./setup (it names any missing build prerequisite)" COMPONENT_MISSING "optional; toolchain absent or helper not built${reason:+: $reason}" } @@ -376,7 +376,7 @@ _dc_check_pins() { return fi _dc_row FAIL pins "$failline$note" "$fix" PROJECT_PIN_MISMATCH ;; - *) _dc_row SKIP pins "gstack-runtime-pins could not evaluate $project (exit $rc: $(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | head -1 | clamp ' -~' 120))" ;; + *) _dc_row SKIP pins "gstack-runtime-pins could not evaluate $project (exit $rc: $(printf '%s\n' "$out" | grep -v '^[[:space:]]*$' | clamp ' -~' 120))" ;; esac } @@ -394,7 +394,7 @@ _dc_check_state_root() { } # _dc_cfg KEY DEFAULT — a clamped config value, never the file or a message. -_dc_cfg() { local v; v=$("$BIN/gstack-config" get "$1" 2>/dev/null /dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 0) n=$(printf '%s' "$n" | clamp '0-9' 6) - backend=$("$BIN/gstack-config" get test_backend 2>/dev/null /dev/null | clamp '0-9A-Za-z.+-' 40) + installed=$(clamp '0-9A-Za-z.+-' 40 2>/dev/null <"$ROOT/VERSION") head=$(git -C "$ROOT_REAL" rev-parse --short HEAD 2>/dev/null | clamp '0-9a-f' 12) _dc_revision="${installed:-unknown}" _dc_check_min_revision "${installed:-0}" "$head" || return @@ -500,7 +500,7 @@ _dc_check_floor() { # first occurrence wins (the top-level field in a policy file), and only the # first 64 KiB are read: a real policy is a few KB. flat=$(head -c 65536 "$file" 2>/dev/null | tr '\n\r\t' ' ') - floor=$(printf '%s' "$flat" | grep -o '"'"$GSTACK_PROJECT_FLOOR_FIELD"'" *: *"[0-9][0-9.]*"' | head -1 | sed 's/.*"\([0-9][0-9.]*\)"$/\1/' | clamp '0-9.' 32) + floor=$(printf '%s' "$flat" | grep -o '"'"$GSTACK_PROJECT_FLOOR_FIELD"'" *: *"[0-9][0-9.]*"' | sed 's/.*"\([0-9][0-9.]*\)"$/\1/' | clamp '0-9.' 32) if [ -z "$floor" ]; then if printf '%s' "$flat" | grep -q '"'"$GSTACK_PROJECT_FLOOR_FIELD"'" *: *"'; then _dc_row SKIP floor "$file sets $GSTACK_PROJECT_FLOOR_FIELD to something other than a plain dotted version" @@ -509,7 +509,7 @@ _dc_check_floor() { fi return fi - installed=$(head -1 "$ROOT/VERSION" 2>/dev/null | clamp '0-9A-Za-z.+-' 40) + installed=$(clamp '0-9A-Za-z.+-' 40 2>/dev/null <"$ROOT/VERSION") if _dc_version_below "${installed:-0}" "$floor"; then if [ -n "${GSTACK_CAPY_WRAPPER_PATH:-}" ]; then fix="bash $GSTACK_CAPY_WRAPPER_PATH${CHECK_FOR:+ --for $CHECK_FOR} (fast-forwards a clean checkout to origin and reinstalls), or $SRC/bin/gstack-upgrade" diff --git a/bin/gstack-hook-check b/bin/gstack-hook-check index a399d2ba6..63f5e5eb5 100755 --- a/bin/gstack-hook-check +++ b/bin/gstack-hook-check @@ -155,11 +155,17 @@ ts_entries() { # One bun process bundles every hook's TypeScript entries (a bun start costs # seconds on Windows). Each line is "\tok\t" per bundled input, # or "\tfail\t\t". +# TS_OF[i] keeps hook i's entries for the check loop below, so each shim is +# scanned once (four process starts, ~80 ms on Windows). ENTRIES="" +i=0 for hook in $HOOKS; do shim="$ROOT/$hook" + TS_OF[i]="" + [ -f "$shim" ] && TS_OF[i]=$(ts_entries "$shim") + i=$((i + 1)) [ -f "$shim" ] || continue - for entry in $(ts_entries "$shim"); do + for entry in ${TS_OF[i - 1]}; do [ -f "${shim%/*}/$entry" ] && ENTRIES="$ENTRIES${shim%/*}/$entry$NL" done done @@ -190,11 +196,14 @@ if [ -n "$ENTRIES" ]; then }' $ENTRIES) > "$BUILT" 2>/dev/null || true fi +i=0 for hook in $HOOKS; do shim="$ROOT/$hook" + i=$((i + 1)) if [ ! -f "$shim" ]; then echo "missing $hook"; continue; fi hook_ok=1 - shebang=$(head -1 "$shim") + shebang="" + IFS= read -r shebang < "$shim" case "$shebang" in '#!'*bash*) interp=bash ;; '#!'*/sh|'#!'*/sh' '*|'#!'*' sh'|'#!'*' sh '*) interp=sh ;; @@ -217,7 +226,7 @@ for hook in $HOOKS; do scan="$scan$NL$helper" done dir=${shim%/*} - for entry in $(ts_entries "$shim"); do + for entry in ${TS_OF[i - 1]}; do if [ ! -f "$dir/$entry" ]; then fail "$hook" "$dir/$entry:1" "missing TypeScript entry (the hook runs it)" hook_ok=0 @@ -244,7 +253,11 @@ for hook in $HOOKS; do # and -H prefixes "::" (the line is the last colon field before # the marker text, so paths containing colons still split right). # shellcheck disable=SC2046 # newline-split list (IFS is a newline) - for found in $(grep -HnE -m1 '^(<<<<<<<|=======|>>>>>>>)( |$)' $(printf '%s\n' "$scan" | awk '!seen[$0]++') 2>/dev/null); do + files="" + for f in $scan; do + case "$NL$files" in *"$NL$f$NL"*) ;; *) files="$files$f$NL" ;; esac + done + for found in $(grep -HnE -m1 '^(<<<<<<<|=======|>>>>>>>)( |$)' $files 2>/dev/null); do marker_file=${found%%:[0-9]*:[<=>]*} marker_line=${found#"$marker_file":} marker_line=${marker_line%%:*} diff --git a/bin/gstack-launch-probe.sh b/bin/gstack-launch-probe.sh index 098587025..8fa4a35fe 100644 --- a/bin/gstack-launch-probe.sh +++ b/bin/gstack-launch-probe.sh @@ -30,7 +30,10 @@ gstack_launch_probe() { out=$("$1" --version &1) fi rc=$? - _glp_detail=$(printf '%s\n' "$out" | tr -d '\r' | grep -v '^[[:space:]]*$' | head -1 | cut -c1-200) + # The first non-blank line, CRs dropped, at most 200 bytes: one subshell, not + # a five-process pipeline (each process start is ~20 ms on Windows). + _glp_detail=$(set -f; IFS=' +'; LC_ALL=C; for line in $out; do line=${line//$'\r'/}; case "$line" in *[![:space:]]*) printf '%s' "${line:0:200}"; break ;; esac; done) case "$out" in *[Aa]pplication\ [Cc]ontrol*|*"Smart App Control"*|*"blocked by group policy"*|*"has blocked this file"*) _glp_state=blocked; return 0 ;;