Escaping Sandbox Popups & Keychain Deadlocks: A Zero-Popup Git File-Queue Architecture on macOS

#macos-sandbox#git-automation#agent-harness#developer-tooling
🌐 阅读中文版 →

This engineering note is written for developers running multi-repository Git syncs, GitHub REST API checks, and external connectivity probes from restricted macOS desktop agent sandboxes. The goal is straightforward: collapse what normally triggers O(N)O(N) manual network approval modals, macOS Keychain deadlocks, and sandbox TLS errors across every remote operation down to at most O(1)O(1) network confirmation per session (00 when launched at login via launchd). If you only push a repository manually once in a while, typing git push in a normal terminal is faster—skip everything below.

1. Failure Anatomy: Why Direct Git Calls Inside a macOS Sandbox Hit Five Walls

When a sandboxed desktop coding agent invokes remote git or gh commands directly, the default execution path collides with five OS and security boundaries back-to-back:

Failure Symptom & Signature Underlying Root Cause Impact on Daily Engineering Flow
Relentless Allow approval popups The sandbox blocks outbound sockets by default; every git fetch, git push, or gh pr list spawns a blocking GUI modal Inspecting multiple repos requires clicking 5–10 popups in a row, shattering deep-work focus
Keychain deadlock (OSStatus -26276) macOS’s default credential.helper=osxkeychain requires an interactive GUI security session unavailable to non-interactive sandboxed subprocesses git push or gh hangs indefinitely waiting on stdin or aborts with authentication errors
Missing SSL root bundle & proxy drops Stripped sandbox environments lose the system CA bundle path; switching between VPN/proxy and direct Wi-Fi breaks hardcoded ports Fails with unable to get local issuer certificate or Connection reset by peer
Hidden dot-directory bridge disconnects Desktop file-bridge enumerators touching hidden home directories (~/.config, ~/.ssh) can trip IPC stream resets Invalidates local session handles and forces a full client window restart
/bin/ps blocked for liveness checks The sandbox profile denies executing /bin/ps (Operation not permitted) Standard ps aux daemon health checks fail immediately

2. Core Architecture: Physically Separating the Offline Control Plane from the Resident Network Plane

The first-principles fix is to decouple high-frequency command dispatch from outbound network privilege ownership across two separate OS process planes (the core prototype behind my open-source macOS harness gengzongyuan/gemini-folderbus):

  1. Resident Network Plane: Runs queue-daemon.sh in the background via a user-level launchd agent (or a single cold-start authorization detached cleanly via start_new_session=True, close_fds=True), holding an authenticated user-space network stack continuously.
  2. Zero-Popup File-Queue Control Plane: For every subsequent git push, ls-remote, or GitHub API query, the sandboxed agent performs pure local filesystem writes—dropping job descriptors into a visible pending/ directory. The resident daemon claims jobs atomically via POSIX mv, executes them outside the sandbox’s per-command prompt loop, and writes stdout plus exit codes back to done/*.out and .rc with zero network permission modals.

Directory Topology & POSIX Permission Contract

All state-machine directories live under a standard visible workspace path (for example, ~/workspace/agent-git-queue/) and are excluded silently via .git/info/exclude if co-located near repositories so git status stays spotless:

File / Directory Mode Core Responsibility
gh_token 600 Owner-only scoped PAT file; never committed to Git and never echoed to stdout/logs
askpass.sh 700 Non-interactive Git credential responder routing username prompts to x-access-token and password prompts to gh_token
git-env.sh 700 Process-scoped environment injector: overrides credential helpers in memory, mounts CA certs, sets 500MB buffer, and handles proxy fallback
queue-daemon.sh 700 Resident consumer loop refreshing daemon.heartbeat every second and atomically claiming pending/*.job
enqueue.sh 700 Offline producer CLI supporting synchronous --wait --timeout <sec> polling and exact exit-code passthrough
pending/ running/ done/ failed/ 755 Four-state POSIX mv atomic file state machine preventing concurrent double-execution while preserving full audit trails

3. Three Production Mechanisms vs. Common Anti-Patterns

1. Process-Scoped Credential Chain Override (Zero ~/.gitconfig Pollution)

  • Common anti-pattern: Running git config --global credential.helper "" (which breaks normal Keychain auth in your interactive terminal) or embedding tokens inside .git/config’s remote.origin.url (which leaks secrets into verbose remote logs).
  • Production pattern: Use Git’s native GIT_CONFIG_COUNT environment variables to shadow credential.helper strictly inside the child process’s memory space, paired with GIT_ASKPASS:
export GIT_TERMINAL_PROMPT=0
export GIT_ASKPASS="$QUEUE_DIR/askpass.sh"
export GIT_CONFIG_COUNT=3
export GIT_CONFIG_KEY_0="credential.helper"
export GIT_CONFIG_VALUE_0=""
export GIT_CONFIG_KEY_1="credential.https://github.com.helper"
export GIT_CONFIG_VALUE_1=""
export GIT_CONFIG_KEY_2="http.postBuffer"
export GIT_CONFIG_VALUE_2="524288000"

2. Explicit CA Bundle Mounting, 500MB Post Buffers, and Dynamic Proxy Fallback

Never paper over TLS errors with GIT_SSL_NO_VERIFY=true. Instead, git-env.sh explicitly binds Homebrew’s CA bundle at /opt/homebrew/etc/ca-certificates/cert.pem (falling back to /etc/ssl/cert.pem), sets a 500MB http.postBuffer to prevent HTTP 400 curl 22 failures on large asset packs, and parses scutil --proxy dynamically—trying the active macOS system proxy first and automatically retrying with proxy environment variables cleared if the local proxy port is stale.

3. Clean Session Detachment and /bin/ps-Free Heartbeat Liveness Probes

A naive nohup cmd & that leaves stdout/stderr file descriptors inherited will hang outer command runners waiting for pipe EOF. Launching via Python’s subprocess.Popen(..., stdin=devnull, stdout=logf, stderr=logf, start_new_session=True, close_fds=True) severs descriptor inheritance cleanly and returns in under 0.5s. For liveness checks inside sandboxes that forbid /bin/ps, combine Bash’s built-in kill -0 "$PID" signal probe with a staleness check (< 20s) against daemon.heartbeat.

4. Engineering Cost & Exit Criteria (TL;DR)

  • Operational overhead: Adds 0.5 ~ 1.0s of file-polling pickup latency compared to raw git, plus ~120 lines of shell state-machine scripts.
  • When to skip or upgrade: If a session only needs a single git push at the very end, run it in your terminal directly. When orchestrating continuous loops across Git worktrees, tmux dev servers, AST/LSP gates, and GitHub PRs, use the full open-source harness generalized from this architecture: gengzongyuan/gemini-folderbus (fbus).