Skip to content

fix(ralph-loop): crash on macOS (bash 3.2) with "mem_limit_prefix[@]: unbound variable"#12

Merged
opensource-SantanderAI merged 1 commit into
mainfrom
fix/macos-bash32-mem-limit-unbound
Jul 1, 2026
Merged

fix(ralph-loop): crash on macOS (bash 3.2) with "mem_limit_prefix[@]: unbound variable"#12
opensource-SantanderAI merged 1 commit into
mainfrom
fix/macos-bash32-mem-limit-unbound

Conversation

@opensource-SantanderAI

Copy link
Copy Markdown
Contributor

ralph-loop crashes on macOS (bash 3.2) with mem_limit_prefix[@]: unbound variable

Environment

  • OS: macOS (arm64)
  • Shell: GNU bash 3.2.57 (macOS default; resolved via #!/usr/bin/env bash when no newer bash is on PATH)
  • Config: RALPH_MEMORY_MAX=8G, RALPH_TOOL=codex

Symptom

On the first iteration the loop aborts with exit code 1:

Warning: RALPH_MEMORY_MAX is set (8G) but systemd-run --user scopes are unavailable; running the agent without a RAM limit.
ralph-loop: line 308: mem_limit_prefix[@]: unbound variable

Root cause

Three factors combine:

  1. set -u is active (line 22).
  2. macOS has no systemd, so build_mem_limit_prefix() leaves mem_limit_prefix=() empty (warning branch).
  3. On bash < 4.4 (macOS ships 3.2), expanding an empty array with "${arr[@]}" under set -u is treated as an unbound variable. On bash >= 4.4 this expands to nothing without error, so it does not reproduce on Linux.

Affected: the four tool invocations that prefix the array — codex (308), claude (322), gemini (346), devin (357).

Isolated repro

/bin/bash -uc \arr=(); echo before; "${arr[@]}"; echo after'
# bash: arr[@]: unbound variable

Fix

Use the safe empty-array idiom, compatible with bash 3.2+: ${arr[@]:+"${arr[@]}"} (expands to nothing when empty; to the quoted elements otherwise). Applied to all four run_tool() invocations.

Verification

  • bash -n ralph-loop.sh OK under bash 3.2.57.
  • RALPH_MEMORY_MAX=8G ralph-loop 1 plan/plan.md no longer crashes; the informative one-shot warning remains and the agent starts without a RAM limit.
  • No Linux regression: when mem_limit_prefix is populated (systemd available), ${arr[@]:+"${arr[@]}"} expands identically to "${arr[@]}".
  • Added a bats regression guard that fails if any bare "${mem_limit_prefix[@]}" expansion is reintroduced.

Notes

The systemd --user RAM limit never applies on macOS; the warning is already a one-shot (mem_limit_warned). Bumping the shebang / requiring bash >= 4.4 was considered but the :+ fix is less intrusive and keeps compatibility with the factory macOS bash.

Under `set -u` on bash < 4.4 (the default macOS bash 3.2), expanding an
empty array with "${arr[@]}" is treated as an unbound variable and aborts
the script. On macOS, systemd-run --user is unavailable, so
build_mem_limit_prefix leaves mem_limit_prefix empty, triggering the crash
on the first tool invocation:

  line 308: mem_limit_prefix[@]: unbound variable

Use the ${arr[@]:+"${arr[@]}"} idiom for all four tool invocations
(codex, claude, gemini, devin). It expands to nothing when empty and
identically to "${arr[@]}" when populated, so Linux behaviour with a
systemd RAM-limit prefix is unchanged.

Add a bats regression guard ensuring no bare mem_limit_prefix expansion is
reintroduced.
@opensource-SantanderAI opensource-SantanderAI requested review from a team as code owners July 1, 2026 09:46
@opensource-SantanderAI opensource-SantanderAI merged commit d991e50 into main Jul 1, 2026
8 checks passed
@opensource-SantanderAI opensource-SantanderAI deleted the fix/macos-bash32-mem-limit-unbound branch July 1, 2026 11:26
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant