From 26b70cc792ea09bc99709a42d6ff2d2a5e93d643 Mon Sep 17 00:00:00 2001 From: L <6723574+louisgv@users.noreply.github.com> Date: Sat, 7 Feb 2026 16:40:21 -0800 Subject: [PATCH] Fix OAuth server in shared/common.sh for macOS bash 3.x (#29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The autonomous refactoring reverted all our macOS fixes in shared/common.sh: 1. nc_listen: removed spurious -p flag check that misfires on macOS BSD nc (BSD nc's -p means source port, not listen port — wrong syntax) 2. start_oauth_server: replaced echo -e (broken on macOS bash 3.x) with printf-based write_oauth_response_file called before the subshell. Removed local vars from subshell (not function scope). 3. ((elapsed++)) / ((attempt++)) → $((var + 1)) to avoid set -e killing the script when the value is 0 (evaluates falsy). Co-authored-by: Sprite Co-authored-by: Claude Opus 4.6 (1M context) --- shared/common.sh | 50 +++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/shared/common.sh b/shared/common.sh index c270a6a7..f7551e82 100644 --- a/shared/common.sh +++ b/shared/common.sh @@ -83,8 +83,8 @@ safe_read() { nc_listen() { local port=$1 shift - # Detect if nc requires -p flag (busybox nc on Termux) - if nc --help 2>&1 | grep -q "BusyBox\|busybox" || nc --help 2>&1 | grep -q "\-p "; then + # BusyBox nc (Termux) requires -p flag; macOS/Linux do not + if nc --help 2>&1 | grep -q "BusyBox\|busybox"; then nc -l -p "$port" "$@" else nc -l "$port" "$@" @@ -229,37 +229,29 @@ get_openrouter_api_key_manual() { echo "$api_key" } -# Generate OAuth success response HTML -create_oauth_response_html() { - cat << 'HTML_EOF' -HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n

Authentication Successful!

Redirecting back to terminal...

This tab will close automatically

-HTML_EOF +# Write OAuth success response HTTP file (uses printf for macOS bash 3.x compat) +write_oauth_response_file() { + local dest="$1" + printf 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n

Authentication Successful!

You can close this tab

' > "$dest" } # Start OAuth callback server in background, returns server PID +# $1=port $2=code_file $3=response_file (must already exist) start_oauth_server() { local port="$1" local code_file="$2" - local success_response=$(create_oauth_response_html) + local response_file="$3" ( while true; do - local response_file=$(mktemp) - echo -e "$success_response" > "$response_file" + request=$(nc_listen "$port" < "$response_file" 2>/dev/null | head -1) || break - local request=$(nc_listen "$port" < "$response_file" 2>/dev/null | head -1) - local nc_status=$? - rm -f "$response_file" - - if [[ $nc_status -ne 0 ]]; then - break - fi - - if [[ "$request" == *"/callback?code="* ]]; then - local code=$(echo "$request" | sed -n 's/.*code=\([^ &]*\).*/\1/p') - echo "$code" > "$code_file" - break - fi + case "$request" in + *"/callback?code="*) + echo "$request" | sed -n 's/.*code=\([^ &]*\).*/\1/p' > "$code_file" + break + ;; + esac done ) /dev/null; then @@ -648,7 +642,7 @@ generic_cloud_api() { sleep "$jitter" interval="$next_interval" - ((attempt++)) + attempt=$((attempt + 1)) continue fi @@ -678,7 +672,7 @@ generic_cloud_api() { sleep "$jitter" interval="$next_interval" - ((attempt++)) + attempt=$((attempt + 1)) continue fi @@ -769,7 +763,7 @@ generic_ssh_wait() { elapsed_time=$((elapsed_time + jitter)) interval="$next_interval" - ((attempt++)) + attempt=$((attempt + 1)) done log_error "$description failed after $max_attempts attempts (${elapsed_time}s elapsed)"