Bypass npx by installing tsx locally and calling its binary directly

This commit is contained in:
Rebase Agent
2026-05-14 16:53:28 +00:00
committed by Forgejo
parent 6f2c4de113
commit 8f631969ff
+26 -9
View File
@@ -44,12 +44,29 @@ IDLE_THRESHOLD_MS=180000 # 3 minutes — session considered idle if last_active
# Script paths
SCRIPT_DIR="/app/.opencode/skills/auto-agents-system/scripts"
SESSION_LIST="npx --yes tsx ${SCRIPT_DIR}/session_list.ts"
SESSION_FIND_PREFIX="npx --yes tsx ${SCRIPT_DIR}/session_find_by_prefix.ts"
SESSION_START="npx --yes tsx ${SCRIPT_DIR}/session_start.ts"
SESSION_MESSAGES="npx --yes tsx ${SCRIPT_DIR}/session_messages.ts"
SESSION_DELETE="npx --yes tsx ${SCRIPT_DIR}/session_delete.ts"
SESSION_STOP="npx --yes tsx ${SCRIPT_DIR}/session_stop.ts"
# Resolve a stable tsx binary path. Using `npx --yes tsx` is unsafe under
# concurrency: when many invocations start simultaneously with a cold (or
# even partially-warm) npm cache, they race on writes to ~/.npm/_npx/.../
# and corrupt each other with `ENOTEMPTY: rename '.../esbuild' ...` errors,
# crashing all but one and leaving the cache in a broken state for some time
# afterwards. We install tsx once into ~/.local/node_modules and call that
# binary directly, bypassing npx entirely.
TSX_PREFIX="${HOME}/.local"
TSX_BIN="${TSX_PREFIX}/node_modules/.bin/tsx"
if ! [[ -x "$TSX_BIN" ]]; then
printf "[%s] Installing tsx into %s …\n" "$(date +%H:%M:%S)" "$TSX_PREFIX" >&2
mkdir -p "$TSX_PREFIX"
npm install --prefix "$TSX_PREFIX" --silent tsx >/dev/null 2>&1 \
|| { printf "ERROR: failed to install tsx into %s\n" "$TSX_PREFIX" >&2; exit 1; }
fi
SESSION_LIST="$TSX_BIN ${SCRIPT_DIR}/session_list.ts"
SESSION_FIND_PREFIX="$TSX_BIN ${SCRIPT_DIR}/session_find_by_prefix.ts"
SESSION_START="$TSX_BIN ${SCRIPT_DIR}/session_start.ts"
SESSION_MESSAGES="$TSX_BIN ${SCRIPT_DIR}/session_messages.ts"
SESSION_DELETE="$TSX_BIN ${SCRIPT_DIR}/session_delete.ts"
SESSION_STOP="$TSX_BIN ${SCRIPT_DIR}/session_stop.ts"
# ── Internal state ───────────────────────────────────────────────────────
SERVER_PID=""
@@ -408,7 +425,7 @@ fetch_list_script() {
fi
local result
result=$(timeout "$SCRIPT_TIMEOUT_SEC" npx --yes tsx "$script_path" \
result=$(timeout "$SCRIPT_TIMEOUT_SEC" "$TSX_BIN" "$script_path" \
--url "$forgejo_url" \
--pat "$forgejo_pat" \
--owner "$forgejo_owner" \
@@ -1059,7 +1076,7 @@ main_loop() {
fi
cache_keys+=("$cache_key")
log "[pool:${prefix}] launching $(basename "$script_path") (cache key: ${cache_key})..."
timeout "$SCRIPT_TIMEOUT_SEC" npx --yes tsx "$script_path" \
timeout "$SCRIPT_TIMEOUT_SEC" "$TSX_BIN" "$script_path" \
--url "$FORGEJO_URL" \
--pat "$FORGEJO_PAT" \
--owner "$FORGEJO_OWNER" \
@@ -1085,7 +1102,7 @@ main_loop() {
elif [[ $ec -ne 0 ]]; then
log_warn "[pool:${prefix}] ${ck}: FAILED (exit ${ec}) — treating as empty"
local snippet
snippet=$(head -n 3 "${out_file}.err" 2>/dev/null | tr '\n' '|' | sed 's/|$//')
snippet=$(head -n 20 "${out_file}.err" 2>/dev/null | tr '\n' '|' | sed 's/|$//')
if [[ -n "$snippet" ]]; then
log_warn " stderr: ${snippet}"
fi