diff --git a/deploy/bind-cli.sh b/deploy/bind-cli.sh index a9d0233c2..19bb489cc 100755 --- a/deploy/bind-cli.sh +++ b/deploy/bind-cli.sh @@ -25,7 +25,7 @@ # --uninstall 卸载:通知平台注销机器、停止服务、删除全部文件和配置 # # 默认直接从平台下载对应 OS/架构的二进制(由服务端 /runners/agentflow-runner 分发)。 -# 脚本安装 agentflow-runner 和跨沙箱 Gate MCP helper、写配置并启动 +# 脚本安装 agentflow-runner 和跨沙箱 Git/MCP、Gate MCP helper、写配置并启动 # daemon。Runner 仅向平台发起出站 HTTPS,不开放任何入站端口。 set -euo pipefail @@ -38,6 +38,7 @@ BIN_DIR="${HOME}/.local/bin" LOCAL_BINARY="" DOWNLOAD_URL="" SOURCE_DIR="" +RUNNER_BUILD_SOURCE="" INSTALL_SERVICE=0 NO_START=0 UNINSTALL=0 @@ -339,6 +340,51 @@ case "$ARCH" in esac [[ "$OS" == "darwin" || "$OS" == "linux" ]] || fail "不支持的系统: ${OS}(Windows 请使用 PowerShell 安装方式)" +# Use the Mach-O slices instead of uname. A universal Runner installed under +# Rosetta can later be launched natively by launchd. List the native slice first. +macos_runner_arches() { + local header="" entry_size little=0 arches + if [[ -f "$1" ]]; then + header="$(LC_ALL=C od -An -v -t x1 -N 8 "$1" | tr -d '[:space:]')" + fi + case "$header" in + cffaedfe07000001) printf '%s\n' amd64; return ;; + cffaedfe0c000001) printf '%s\n' arm64; return ;; + cafebabe*) entry_size=20 ;; + cafebabf*) entry_size=32 ;; + bebafeca*) entry_size=20; little=1 ;; + bfbafeca*) entry_size=32; little=1 ;; + *) printf '%s\n' "$ARCH"; return ;; + esac + # A fat header is eight bytes. Bound the read to the architecture table + # and accept only the CPU types for which we distribute Linux companions. + arches=$(LC_ALL=C od -An -v -t u1 -N 4104 "$1" | + awk -v stride="$entry_size" -v little="$little" ' + { for (i = 1; i <= NF; i++) bytes[count++] = $i } + function word(offset, i, value) { + for (i = 0; i < 4; i++) value = 256 * value + bytes[offset + (little ? 3-i : i)] + return value + } + END { + slices = word(4) + if (slices < 1 || 8 + slices * stride > count) exit 1 + for (slice = 0; slice < slices; slice++) { + cpu = word(8 + slice * stride) + if (cpu == 16777223) amd64 = 1 + if (cpu == 16777228) arm64 = 1 + } + if (amd64 && arm64) print "arm64 amd64" + else if (amd64) print "amd64" + else if (arm64) print "arm64" + }') || arches="" + # uname reports x86_64 under Rosetta too. Intel Macs can use only amd64. + if [[ "$arches" == "arm64 amd64" && "$ARCH" == "amd64" && + "$(PATH="$PATH:/usr/sbin" sysctl -n hw.optional.arm64 2>/dev/null || true)" != "1" ]]; then + arches="amd64" + fi + printf '%s\n' "${arches:-$ARCH}" +} + RUNNER_HOME="${HOME}/.agentflow-runner" ENV_FILE="${RUNNER_HOME}/env" STATE_FILE="${RUNNER_HOME}/state.json" @@ -420,6 +466,12 @@ if [[ "$UNINSTALL" == "1" ]]; then done < <(docker images --format '{{.Repository}}' 2>/dev/null | awk '/^agentflow-tmp-rt-/' || true) info "已清理 runner Docker 沙箱和临时镜像" fi + if [[ "$OS" == "darwin" ]]; then + read -r -a runner_arches <<< "$(macos_runner_arches "$BINARY")" + for runner_arch in "${runner_arches[@]}"; do + rm -f "${BIN_DIR}/agentflow-runner-linux-${runner_arch}" + done + fi rm -f "$BINARY" rm -f "${BIN_DIR}"/agentflow-gate-mcp-darwin-amd64 \ "${BIN_DIR}"/agentflow-gate-mcp-darwin-arm64 \ @@ -537,75 +589,343 @@ case "$(printf '%s' "${AGENTFLOW_BROWSER:-}" | tr '[:upper:]' '[:lower:]')" in esac # ---------- 1. 安装二进制 ---------- -install_binary() { +download_runner_binary() { + local target_os="$1" url + if [[ -n "$DOWNLOAD_URL" && ( "$target_os" == "$OS" || "$DOWNLOAD_URL" == *"{os}"* ) ]]; then + url="${DOWNLOAD_URL//\{os\}/$target_os}" + url="${url//\{arch\}/$ARCH}" + else + url="${SERVER}/runners/agentflow-runner?os=${target_os}&arch=${ARCH}" + fi + info "从 ${url} 下载 agentflow-runner(${target_os}/${ARCH})..." + curl -fsSL "$url" -o "$TEMP_BINARY" && [[ -s "$TEMP_BINARY" ]] +} + +build_runner_binary() { + local target_os="$1" source="$2" command="${3:-agentflow-runner}" output="$TEMP_BINARY" + [[ "$output" == /* ]] || output="$PWD/$output" + info "从源码 ${source} 构建 ${command}(${target_os}/${ARCH})..." + (cd "$source" && CGO_ENABLED=0 GOOS="$target_os" GOARCH="$ARCH" go build -o "$output" "./cmd/$command") +} + +publish_runner_binary() { + [[ -s "$TEMP_BINARY" ]] || fail "下载或构建的 runner 二进制为空" + chmod +x "$TEMP_BINARY" + mv -f "$TEMP_BINARY" "$1" + TEMP_BINARY="" +} + +stage_runner_binary() { mkdir -p "$BIN_DIR" TEMP_BINARY=$(mktemp "${BIN_DIR}/.agentflow-runner.XXXXXX") if [[ -n "$LOCAL_BINARY" ]]; then [[ -f "$LOCAL_BINARY" ]] || fail "本地二进制不存在: $LOCAL_BINARY" cp "$LOCAL_BINARY" "$TEMP_BINARY" elif [[ -n "$DOWNLOAD_URL" ]]; then - URL="${DOWNLOAD_URL//\{os\}/$OS}" - URL="${URL//\{arch\}/$ARCH}" - info "从 ${URL} 下载二进制..." - curl -fsSL "$URL" -o "$TEMP_BINARY" + download_runner_binary "$OS" else # 默认:平台直接分发对应 OS/架构的二进制,无需本机 Go 工具链。 - info "从 ${SERVER} 下载 agentflow-runner(${OS}/${ARCH})..." - if curl -fsSL "${SERVER}/runners/agentflow-runner?os=${OS}&arch=${ARCH}" -o "$TEMP_BINARY"; then + if download_runner_binary "$OS"; then : else # 平台未配置分发:回退到本地源码构建(--source 或当前目录为源码 checkout)。 AUTO_UPDATE=0 - SRC="${SOURCE_DIR:-$PWD}" - if [[ ! -d "$SRC/cmd/agentflow-runner" ]]; then + RUNNER_BUILD_SOURCE="${SOURCE_DIR:-$PWD}" + if [[ ! -d "$RUNNER_BUILD_SOURCE/cmd/agentflow-runner" ]]; then fail "平台未提供二进制且未找到源码。解决方式任选其一: 1) 平台管理员设置 AGENTFLOW_SOURCE_ROOT(源码目录)或 AGENTFLOW_RUNNER_BINARY(预编译产物)后重启服务 2) 本命令追加 --source (需要本机安装 Go) 3) 本命令追加 --from <已编译的 agentflow-runner 二进制路径>" fi command -v go >/dev/null 2>&1 || fail "本机未安装 Go,无法从源码构建;请使用 --from 提供二进制" - info "从源码 ${SRC} 构建(${OS}/${ARCH})..." - (cd "$SRC" && CGO_ENABLED=0 GOOS="$OS" GOARCH="$ARCH" go build -o "$TEMP_BINARY" ./cmd/agentflow-runner) \ + build_runner_binary "$OS" "$RUNNER_BUILD_SOURCE" \ || fail "源码构建失败;请检查 Go 版本(需 1.22+)或改用 --from" fi fi [[ -s "$TEMP_BINARY" ]] || fail "下载或构建的 runner 二进制为空" - chmod +x "$TEMP_BINARY" - mv -f "$TEMP_BINARY" "$BINARY" +} + +is_linux_runner_binary() { + local binary="$1" machine size layout offset table_size + case "${2:-$ARCH}" in + amd64) machine=62 ;; + arm64) machine=183 ;; + *) return 1 ;; + esac + size=$(wc -c < "$binary") || return 1 + local decode_bytes=' + { for (i = 1; i <= NF; i++) bytes[count++] = $i } + function little_endian(offset, width, i, value) { + for (i = width - 1; i >= 0; i--) value = 256 * value + bytes[offset + i] + return value + }' + # ELF64 starts with 64 bytes, followed by a table of 56-byte program headers. + layout=$(LC_ALL=C od -An -v -t u1 -N 64 "$binary" | + awk -v machine="$machine" -v size="$size" "$decode_bytes"' + END { + if (count != 64 || bytes[0] != 127 || bytes[1] != 69 || + bytes[2] != 76 || bytes[3] != 70 || bytes[4] != 2 || + bytes[5] != 1 || bytes[6] != 1 || (bytes[7] != 0 && bytes[7] != 3)) exit 1 + type = little_endian(16, 2) + offset = little_endian(32, 8) + num = little_endian(56, 2) + if ((type != 2 && type != 3) || little_endian(18, 2) != machine || + little_endian(54, 2) != 56 || num == 0 || num * 56 > 65536 || + offset < 64 || num * 56 > size - offset) exit 1 + printf "%.0f %d\n", offset, num * 56 + }') || return 1 + read -r offset table_size <<< "$layout" + # The header can be valid even when the server only has part of the file. + LC_ALL=C od -An -v -t u1 -j "$offset" -N "$table_size" "$binary" | + awk -v size="$size" -v table_size="$table_size" "$decode_bytes"' + END { + if (count != table_size) exit 1 + for (header = 0; header < count; header += 56) { + type = little_endian(header, 4) + flags = little_endian(header + 4, 4) + offset = little_endian(header + 8, 8) + file_size = little_endian(header + 32, 8) + if (type != 0 && file_size > 0 && (offset > size || file_size > size - offset)) exit 1 + if (type != 1) continue + if (file_size > little_endian(header + 40, 8)) exit 1 + if (file_size > 0 && flags % 2 == 1) executable = 1 + } + if (!executable) exit 1 + }' +} + +is_usable_docker_runner_helper() { + # The runtime copies the source into the sandbox with executable mode. + [[ -r "$1" && -f "$1" ]] || return 1 + # A custom installer may also provide an executable script wrapper. + [[ "$(LC_ALL=C od -An -v -t x1 -N 2 "$1" | tr -d '[:space:]')" == 2321 ]] || + is_linux_runner_binary "$1" "${2:-$ARCH}" +} + +stage_docker_runner_binary() { + local source="${SOURCE_DIR:-$PWD}" + if [[ -n "$RUNNER_BUILD_SOURCE" ]]; then + build_runner_binary linux "$RUNNER_BUILD_SOURCE" && is_linux_runner_binary "$TEMP_BINARY" + return + fi + if download_runner_binary linux && is_linux_runner_binary "$TEMP_BINARY"; then + return 0 + fi + [[ -d "$source/cmd/agentflow-runner" ]] && command -v go >/dev/null 2>&1 && + build_runner_binary linux "$source" && is_linux_runner_binary "$TEMP_BINARY" +} + +install_docker_runner_binary() { + # A custom host runner may have a matching helper managed by its installer. + # A per-OS download URL can replace the helper when it is the selected source. + if [[ -n "$LOCAL_BINARY" || -n "$RUNNER_BUILD_SOURCE" || + ( "$DOWNLOAD_URL" != *"{os}"* && ( -n "$DOWNLOAD_URL" || -n "$SOURCE_DIR" ) ) ]] && + is_usable_docker_runner_helper "$DOCKER_RUNNER_BINARY"; then + info "保留自定义 Runner 的 Linux Git/MCP helper:${DOCKER_RUNNER_BINARY}" + return + fi + local helper_dir + helper_dir=$(dirname "$DOCKER_RUNNER_BINARY") + if [[ -d "$DOCKER_RUNNER_BINARY" || "$DOCKER_RUNNER_BINARY" -ef "$BINARY" ]] || + ! mkdir -p "$helper_dir" || ! TEMP_BINARY=$(mktemp "${helper_dir}/.agentflow-runner.XXXXXX"); then + warn "无法准备 Linux Git/MCP helper 的安装位置:${DOCKER_RUNNER_BINARY}" + return + fi + if stage_docker_runner_binary; then + publish_runner_binary "$DOCKER_RUNNER_BINARY" + return + fi + rm -f "$TEMP_BINARY" TEMP_BINARY="" + warn "无法获取有效的 Linux/${ARCH} Git/MCP helper,Docker 工作区需要该文件:${DOCKER_RUNNER_BINARY}" } TEMP_BINARY="" -trap '[[ -z "$TEMP_BINARY" ]] || rm -f "$TEMP_BINARY"' EXIT -install_binary +STAGED_RUNNER="" +trap '[[ -z "$TEMP_BINARY" ]] || rm -f "$TEMP_BINARY"; [[ -z "$STAGED_RUNNER" ]] || rm -f "$STAGED_RUNNER"' EXIT +stage_runner_binary +STAGED_RUNNER="$TEMP_BINARY" +TEMP_BINARY="" + +saved_runner_binary_pattern() { + awk 'sub(/^AGENTFLOW_RUNNER_BINARY=/, "") { print; exit }' "$ENV_FILE" +} + +normalize_runner_binary_pattern() { + local pattern="$1" working_dir="$2" + # Keep the template expansion in sync with runtime.DockerRunnerHelperPath. + pattern="${pattern#"${pattern%%[![:space:]]*}"}" + pattern="${pattern%"${pattern##*[![:space:]]}"}" + if [[ "$pattern" == *"{os}"* && "$pattern" != /* ]]; then + pattern="${working_dir%/}/$pattern" + fi + printf '%s\n' "$pattern" +} + +installed_macos_runner_binary_pattern() { + local pattern working_dir + if [[ -f "$PLIST" ]] && + pattern=$(/usr/bin/plutil -extract EnvironmentVariables.AGENTFLOW_RUNNER_BINARY raw "$PLIST" 2>/dev/null); then + working_dir=$(/usr/bin/plutil -extract WorkingDirectory raw "$PLIST" 2>/dev/null) || working_dir="/" + normalize_runner_binary_pattern "$pattern" "${working_dir:-/}" + else + pattern=$(saved_runner_binary_pattern) + normalize_runner_binary_pattern "$pattern" "$PWD" + fi +} + +configure_macos_docker_runner_helper() { + local pattern + pattern=$(normalize_runner_binary_pattern "${AGENTFLOW_RUNNER_BINARY-$1}" "$PWD") + if [[ "${#runner_arches[@]}" -gt 1 && "$pattern" == *"{os}"* && "$pattern" != *"{arch}"* ]]; then + fail "通用 macOS Runner 需要为每种架构安装 Linux Git/MCP helper,请在 AGENTFLOW_RUNNER_BINARY 中添加 {arch}" + fi + export AGENTFLOW_RUNNER_BINARY="$pattern" +} + +macos_docker_runner_helper_path() { + local target_arch="$1" pattern="$2" target="${BIN_DIR}/agentflow-runner-linux-${1}" + if [[ "$pattern" == *"{os}"* ]]; then + target="${pattern//\{os\}/linux}" + target="${target//\{arch\}/$target_arch}" + fi + printf '%s\n' "$target" +} + +canonical_helper_target() { + local target="$1" dir + dir=$(dirname "$target") + (mkdir -p "$dir" && cd -P "$dir" && printf '%s/%s\n' "$PWD" "${target##*/}") +} + +check_macos_helper_paths() { + local host_target target resolved runner_arch previous_target="" previous_resolved="" + host_target=$(canonical_helper_target "$BINARY") + for runner_arch in "${runner_arches[@]}"; do + target=$(macos_docker_runner_helper_path "$runner_arch" "$AGENTFLOW_RUNNER_BINARY") + # Compare physical parents even on fresh installs, where -ef cannot + # compare inodes yet. Unavailable helper destinations are warned about + # by the installer when it tries to stage the companion. + resolved=$(canonical_helper_target "$target" 2>/dev/null) || resolved="" + if [[ "$target" -ef "$BINARY" ]] || [[ -n "$resolved" && "$resolved" == "$host_target" ]]; then + fail "Linux Git/MCP helper 路径与 macOS Runner 相同:${target}" + fi + if [[ -n "$previous_target" && "$target" -ef "$previous_target" ]] || + [[ -n "$resolved" && "$resolved" == "$previous_resolved" ]]; then + fail "两种架构的 Linux Git/MCP helper 路径相同,请使用独立的架构目录" + fi + previous_target="$target" + previous_resolved="$resolved" + done +} + +save_runner_binary_pattern() { + [[ "$(saved_runner_binary_pattern)" != "$AGENTFLOW_RUNNER_BINARY" ]] || return 0 + local tmp + tmp=$(mktemp "${RUNNER_HOME}/.env.XXXXXX") + if ! { sed '/^AGENTFLOW_RUNNER_BINARY=/d' "$ENV_FILE" && + printf 'AGENTFLOW_RUNNER_BINARY=%s\n' "$AGENTFLOW_RUNNER_BINARY"; } > "$tmp" || + ! mv -f "$tmp" "$ENV_FILE"; then + rm -f "$tmp" + fail "无法保存 Git/MCP helper 路径配置" + fi +} + +if [[ "$OS" == "darwin" ]]; then + read -r -a runner_arches <<< "$(macos_runner_arches "$STAGED_RUNNER")" + required_arch="${runner_arches[0]}" + previous_pattern="" + require_replacement_helper=0 + if [[ "$UPGRADE" == "1" ]]; then + previous_pattern=$(installed_macos_runner_binary_pattern) + read -r -a previous_arches <<< "$(macos_runner_arches "$BINARY")" + # The old universal binary may be running its Intel slice under Rosetta. + for previous_arch in "${previous_arches[@]}"; do + previous_helper=$(macos_docker_runner_helper_path "$previous_arch" "$previous_pattern") + if [[ ! "$previous_helper" -ef "$BINARY" ]] && + is_usable_docker_runner_helper "$previous_helper" "$previous_arch"; then + require_replacement_helper=1 + break + fi + done + fi + configure_macos_docker_runner_helper "$previous_pattern" + check_macos_helper_paths +fi install_gate_binary() { - local target_os="$1" target_arch="$2" target + local target_os="$1" target_arch="$ARCH" optional="${2:-0}" target + local src="${SOURCE_DIR:-$PWD}" error target="${GATE_BINARY_PATTERN//\{os\}/$target_os}" target="${target//\{arch\}/$target_arch}" info "从 ${SERVER} 下载 Gate MCP(${target_os}/${target_arch})..." - if curl -fsSL "${SERVER}/runners/agentflow-gate-mcp?os=${target_os}&arch=${target_arch}" -o "$target"; then - chmod +x "$target" + TEMP_BINARY=$(mktemp "${BIN_DIR}/.agentflow-gate-mcp.XXXXXX") + if curl -fsSL "${SERVER}/runners/agentflow-gate-mcp?os=${target_os}&arch=${target_arch}" -o "$TEMP_BINARY" && + [[ -s "$TEMP_BINARY" ]]; then + publish_runner_binary "$target" return fi - local src="${SOURCE_DIR:-$PWD}" - [[ -d "$src/cmd/agentflow-gate-mcp" ]] || fail "平台未提供 Gate MCP ${target_os}/${target_arch} 二进制,且未找到本地源码" - command -v go >/dev/null 2>&1 || fail "本机未安装 Go,无法构建 Gate MCP;请让平台配置 AGENTFLOW_GATE_MCP_BINARY" - info "从源码 ${src} 构建 Gate MCP(${target_os}/${target_arch})..." - (cd "$src" && CGO_ENABLED=0 GOOS="$target_os" GOARCH="$target_arch" go build -o "$target" ./cmd/agentflow-gate-mcp) \ - || fail "Gate MCP 源码构建失败" - chmod +x "$target" + if [[ ! -d "$src/cmd/agentflow-gate-mcp" ]]; then + error="平台未提供 Gate MCP ${target_os}/${target_arch} 二进制,且未找到本地源码" + elif ! command -v go >/dev/null 2>&1; then + error="本机未安装 Go,无法构建 Gate MCP;请让平台配置 AGENTFLOW_GATE_MCP_BINARY" + elif build_runner_binary "$target_os" "$src" agentflow-gate-mcp && [[ -s "$TEMP_BINARY" ]]; then + publish_runner_binary "$target" + return + else + error="Gate MCP ${target_os}/${target_arch} 源码构建失败" + fi + rm -f "$TEMP_BINARY" + TEMP_BINARY="" + if [[ "$optional" == "1" ]]; then + warn "$error" + else + fail "$error" + fi } -# Host sandbox uses the native helper. Docker always uses Linux; on a macOS -# runner both artifacts are therefore required. -install_gate_binary "$OS" "$ARCH" -if [[ "$OS" != "linux" ]]; then - install_gate_binary linux "$ARCH" +# Install the primary slice's companions before touching the optional slice. +if [[ "$OS" == "darwin" ]]; then + for ARCH in "${runner_arches[@]}"; do + DOCKER_RUNNER_BINARY=$(macos_docker_runner_helper_path "$ARCH" "$AGENTFLOW_RUNNER_BINARY") + optional_gate=0 + [[ "$ARCH" == "$required_arch" ]] || optional_gate=1 + install_gate_binary darwin "$optional_gate" + install_gate_binary linux "$optional_gate" + install_docker_runner_binary + if [[ "$ARCH" == "$required_arch" && "$require_replacement_helper" == "1" ]] && + ! is_usable_docker_runner_helper "$DOCKER_RUNNER_BINARY"; then + fail "缺少升级所需的 Linux/${ARCH} Git/MCP helper(${DOCKER_RUNNER_BINARY}),已保留当前 Runner 和服务" + fi + done +else + install_gate_binary "$OS" fi +TEMP_BINARY="$STAGED_RUNNER" +STAGED_RUNNER="" +publish_runner_binary "$BINARY" + if [[ "$UPGRADE" == "1" ]]; then + if [[ "$OS" == "darwin" ]]; then + save_runner_binary_pattern + fi if [[ "$OS" == "darwin" && -f "$PLIST" ]]; then + if [[ "$AUTO_UPDATE" == "0" ]]; then + arg_count=$(/usr/bin/plutil -extract ProgramArguments raw -expect array "$PLIST") + if [[ "$arg_count" -eq 0 ]] || + [[ "$(/usr/bin/plutil -extract "ProgramArguments.$((arg_count - 1))" raw "$PLIST")" != "-auto-update=false" ]]; then + /usr/bin/plutil -insert ProgramArguments -string -auto-update=false -append "$PLIST" + fi + fi + if [[ "$AGENTFLOW_RUNNER_BINARY" == *"{os}"* ]]; then + if ! /usr/bin/plutil -type EnvironmentVariables "$PLIST" >/dev/null 2>&1; then + /usr/bin/plutil -insert EnvironmentVariables -dictionary "$PLIST" + fi + /usr/bin/plutil -replace EnvironmentVariables.AGENTFLOW_RUNNER_BINARY -string "$AGENTFLOW_RUNNER_BINARY" "$PLIST" + elif /usr/bin/plutil -type EnvironmentVariables.AGENTFLOW_RUNNER_BINARY "$PLIST" >/dev/null 2>&1; then + /usr/bin/plutil -remove EnvironmentVariables.AGENTFLOW_RUNNER_BINARY "$PLIST" + fi launchctl unload "$PLIST" launchctl load "$PLIST" info "已原地升级并重启 launchd 服务;机器绑定和工作区保持不变" @@ -630,6 +950,7 @@ AGENTFLOW_RUNNER_BOOTSTRAP_TOKEN=${TOKEN} AGENTFLOW_RUNNER_NAME=${NAME:-$(hostname)} AGENTFLOW_RUNNER_CONCURRENCY=${CONCURRENCY} AGENTFLOW_GATE_MCP_BINARY=${GATE_BINARY_PATTERN} +AGENTFLOW_RUNNER_BINARY=${AGENTFLOW_RUNNER_BINARY:-} EOF chmod 600 "$ENV_FILE" info "配置已写入 ${ENV_FILE}(权限 600)" @@ -640,6 +961,12 @@ RUN_ARGS=(-server "$SERVER" -token "$TOKEN" -name "${NAME:-$(hostname)}" -concur if [[ "$AUTO_UPDATE" == "0" ]]; then RUN_ARGS+=(-auto-update=false) fi +RUNNER_LAUNCH=("$BINARY") +if [[ "$OS" == "darwin" && "${#runner_arches[@]}" -gt 1 ]]; then + # Use the required native slice for foreground and manual starts, even + # when the shell runs under Rosetta and the optional amd64 files are absent. + RUNNER_LAUNCH=(/usr/bin/arch -arm64 "$BINARY") +fi # 用户当前 PATH:launchd/systemd 的服务环境只有 /usr/bin:/bin,不含 # ~/.local/bin、Homebrew、nvm 等 CLI 安装目录,必须显式带入。 @@ -647,7 +974,14 @@ USER_PATH="${PATH}" if [[ "$NO_START" == "1" ]]; then info "已完成安装与配置(--no-start,未启动)。手动启动:" - info " $BINARY ${RUN_ARGS[*]}" + runner_command="" + if [[ "${AGENTFLOW_RUNNER_BINARY:-}" == *"{os}"* ]]; then + runner_command="AGENTFLOW_RUNNER_BINARY=$(shell_quote "$AGENTFLOW_RUNNER_BINARY") " + fi + for arg in "${RUNNER_LAUNCH[@]}" "${RUN_ARGS[@]}"; do + runner_command+="$(shell_quote "$arg") " + done + info " ${runner_command% }" info "常驻方式:追加 --service 重新执行本脚本;卸载:追加 --uninstall(--purge 删除全部配置)" elif [[ "$INSTALL_SERVICE" == "1" ]]; then if [[ "$OS" == "darwin" ]]; then @@ -663,11 +997,13 @@ EOF # plist 值需要 XML 转义(& 等) ESCAPED_PATH=$(printf '%s' "$USER_PATH" | sed 's/&/\&/g; s//\>/g') ESCAPED_GATE_PATTERN=$(printf '%s' "$GATE_BINARY_PATTERN" | sed 's/&/\&/g; s//\>/g') + ESCAPED_RUNNER_PATTERN=$(printf '%s' "$AGENTFLOW_RUNNER_BINARY" | sed 's/&/\&/g; s//\>/g') cat >> "$PLIST" < EnvironmentVariables PATH${ESCAPED_PATH} AGENTFLOW_GATE_MCP_BINARY${ESCAPED_GATE_PATTERN} + AGENTFLOW_RUNNER_BINARY${ESCAPED_RUNNER_PATTERN} RunAtLoad KeepAlive @@ -708,5 +1044,5 @@ EOF info "卸载方式:curl -fsSL ${SERVER}/runners/install.sh | bash -s -- --uninstall" else info "前台启动 agentflow-runner(Ctrl+C 停止;常驻请加 --service)..." - exec "$BINARY" "${RUN_ARGS[@]}" + exec "${RUNNER_LAUNCH[@]}" "${RUN_ARGS[@]}" fi diff --git a/deploy/bind_cli_docker_runner_test.go b/deploy/bind_cli_docker_runner_test.go new file mode 100644 index 000000000..5baf85d3b --- /dev/null +++ b/deploy/bind_cli_docker_runner_test.go @@ -0,0 +1,501 @@ +package deploy + +import ( + "debug/elf" + "encoding/binary" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "runtime" + "slices" + "strings" + "testing" +) + +func TestBindCLIInstallsDockerRunnerHelper(t *testing.T) { + curl, err := exec.LookPath("curl") + if err != nil { + t.Skip("curl is required for the runner installer") + } + script, err := filepath.Abs("bind-cli.sh") + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name, hostOS, arch, unameArch string + upgrade, launchd bool + helperArtifact, source, existing string + preserveHelper bool + templatePath bool + helperAliasesRunner bool + universal, previousUniversal bool + intelHardware, sharedTemplateDir bool + arm64OnlyArtifacts bool + templateMissingArch bool + trimSavedTemplate bool + relativeServiceTemplate bool + missingRequiredGate bool + missingReplacementHelper bool + }{ + {name: "macOS_install", hostOS: "darwin", arch: "arm64", unameArch: "arm64"}, + {name: "macOS_uses_template_in_manual_start_and_upgrade", hostOS: "darwin", arch: "arm64", unameArch: "arm64", templatePath: true, source: "from"}, + {name: "macOS_rejects_template_aliasing_the_runner", hostOS: "darwin", arch: "arm64", unameArch: "arm64", templatePath: true, helperAliasesRunner: true}, + {name: "macOS_upgrade", hostOS: "darwin", arch: "amd64", unameArch: "x86_64", upgrade: true}, + {name: "Linux_install", hostOS: "linux", arch: "arm64", unameArch: "aarch64"}, + {name: "macOS_preserves_helper_on_failed_download", hostOS: "darwin", arch: "arm64", unameArch: "arm64", helperArtifact: "unavailable", preserveHelper: true}, + {name: "macOS_preserves_helper_when_server_serves_macOS_binary_for_Linux", hostOS: "darwin", arch: "arm64", unameArch: "arm64", upgrade: true, helperArtifact: "mach-o", preserveHelper: true}, + {name: "macOS_preserves_helper_when_server_serves_wrong_architecture", hostOS: "darwin", arch: "amd64", unameArch: "x86_64", upgrade: true, helperArtifact: "wrong-arch", preserveHelper: true}, + {name: "macOS_preserves_helper_when_only_ELF_header_is_served", hostOS: "darwin", arch: "arm64", unameArch: "arm64", upgrade: true, helperArtifact: "truncated-header", preserveHelper: true}, + {name: "macOS_preserves_helper_when_ELF_segment_is_truncated", hostOS: "darwin", arch: "arm64", unameArch: "arm64", upgrade: true, helperArtifact: "truncated-segment", preserveHelper: true}, + {name: "macOS_preserves_helper_with_local_runner", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", preserveHelper: true}, + {name: "macOS_preserves_helper_with_implicit_source_build", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "checkout", preserveHelper: true}, + {name: "macOS_upgrades_launchd_with_custom_runner", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", launchd: true, upgrade: true, preserveHelper: true}, + {name: "macOS_upgrades_launchd_on_template_path", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", launchd: true, upgrade: true, templatePath: true}, + {name: "macOS_installs_companion_for_Intel_runner_on_arm64", hostOS: "darwin", arch: "amd64", unameArch: "arm64", source: "from"}, + {name: "macOS_under_Rosetta_installs_both_companions_for_universal_Runner", hostOS: "darwin", arch: "arm64", unameArch: "x86_64", source: "from", universal: true}, + {name: "macOS_Intel_installs_universal_Runner_from_amd64_only_platform", hostOS: "darwin", arch: "amd64", unameArch: "x86_64", source: "from", universal: true, intelHardware: true}, + {name: "macOS_Apple_Silicon_installs_universal_Runner_from_arm64_only_platform", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", universal: true, arm64OnlyArtifacts: true}, + {name: "macOS_rejects_universal_companions_with_a_shared_template_directory", hostOS: "darwin", arch: "arm64", unameArch: "x86_64", source: "from", universal: true, templatePath: true, sharedTemplateDir: true}, + {name: "macOS_rejected_universal_upgrade_preserves_installed_runner", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", universal: true, upgrade: true, launchd: true, templatePath: true, templateMissingArch: true}, + {name: "macOS_preserves_installed_runner_if_required_gate_is_unavailable", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", universal: true, upgrade: true, missingRequiredGate: true}, + {name: "macOS_architecture_upgrade_keeps_working_runner_when_new_helper_is_unavailable", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", universal: true, upgrade: true, launchd: true, missingReplacementHelper: true}, + {name: "macOS_keeps_working_Rosetta_slice_when_native_helper_is_unavailable", hostOS: "darwin", arch: "arm64", unameArch: "x86_64", source: "from", previousUniversal: true, upgrade: true, launchd: true, missingReplacementHelper: true}, + {name: "macOS_preserves_working_helper_with_whitespace_in_saved_template", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", upgrade: true, templatePath: true, trimSavedTemplate: true, missingReplacementHelper: true}, + {name: "macOS_preserves_readable_old_helper_without_execute_permission", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", existing: "non-executable", upgrade: true, launchd: true, missingReplacementHelper: true}, + {name: "macOS_preserves_helper_relative_to_launchd_working_directory", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", upgrade: true, launchd: true, relativeServiceTemplate: true, missingReplacementHelper: true}, + {name: "macOS_repairs_incompatible_local_helper", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", existing: "mach-o", upgrade: true}, + {name: "macOS_preserves_custom_helper_script", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "from", existing: "script", preserveHelper: true}, + {name: "macOS_preserves_helper_with_host_only_URL", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "host-url", preserveHelper: true}, + {name: "macOS_updates_helper_with_explicit_Linux_URL", hostOS: "darwin", arch: "arm64", unameArch: "arm64", source: "template"}, + } { + t.Run(tc.name, func(t *testing.T) { + if tc.launchd && (!tc.missingReplacementHelper || tc.relativeServiceTemplate) && runtime.GOOS != "darwin" { + t.Skip("requires macOS plutil") + } + fixtureHome := t.TempDir() + bundle := filepath.Join(fixtureHome, "artifact bundle") + bin := filepath.Join(fixtureHome, "bin") + if tc.helperAliasesRunner { + bin = filepath.Join(bundle, "linux", tc.arch) + } + if err := os.MkdirAll(bin, 0o755); err != nil { + t.Fatal(err) + } + uname := fmt.Sprintf("#!/bin/sh\ncase \"$1\" in -s) echo %s ;; -m) echo %s ;; esac\n", tc.hostOS, tc.unameArch) + if err := os.WriteFile(filepath.Join(bin, "uname"), []byte(uname), 0o755); err != nil { + t.Fatal(err) + } + linuxRunner := bindFixtureLinuxRunner(tc.arch, "new helper") + oldHelper := bindFixtureLinuxRunner(tc.arch, "old helper") + hostRunner := linuxRunner + if tc.hostOS == "darwin" { + cpu := "\x0c\x00\x00\x01" + if tc.arch == "amd64" { + cpu = "\x07\x00\x00\x01" + } + hostRunner = "\xcf\xfa\xed\xfe" + cpu + " macOS runner" + } + if tc.source == "from" { + hostRunner += " custom" + } + oldHost := "\xcf\xfa\xed\xfe\x07\x00\x00\x01 old Intel Runner" + if tc.universal || tc.previousUniversal { + const x8664, arm64 = 0x01000007, 0x0100000c + header := make([]byte, 8+2*20) + binary.BigEndian.PutUint32(header[:], 0xcafebabe) + binary.BigEndian.PutUint32(header[4:], 2) + binary.BigEndian.PutUint32(header[8:], x8664) + binary.BigEndian.PutUint32(header[28:], arm64) + if tc.previousUniversal { + oldHost = string(header) + "old universal runner" + } + if tc.universal { + hostRunner = string(header) + "universal runner" + } + arm64Hardware := 1 + if tc.intelHardware { + arm64Hardware = 0 + } + sysctl := fmt.Sprintf("#!/bin/sh\nprintf '%%s\\n' %d\n", arm64Hardware) + if err := os.WriteFile(filepath.Join(bin, "sysctl"), []byte(sysctl), 0o755); err != nil { + t.Fatal(err) + } + } + oldHelperMode := os.FileMode(0o755) + switch tc.existing { + case "mach-o": + oldHelper = "\xcf\xfa\xed\xfe wrong OS" + case "script": + oldHelper = "#!/bin/sh\nexec /opt/runner \"$@\"\n" + case "non-executable": + oldHelperMode = 0o644 + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + targetOS, arch := req.URL.Query().Get("os"), req.URL.Query().Get("arch") + if (arch != tc.arch && arch != tc.unameArch && !(tc.universal && arch == "amd64" && !tc.arm64OnlyArtifacts)) || + (targetOS != tc.hostOS && targetOS != "linux") { + http.NotFound(w, req) + return + } + switch req.URL.Path { + case "/runners/agentflow-runner": + if targetOS == tc.hostOS { + if tc.source == "checkout" { + http.NotFound(w, req) + return + } + fmt.Fprint(w, hostRunner) + return + } + if tc.missingReplacementHelper && arch == "arm64" { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + switch tc.helperArtifact { + case "unavailable": + http.NotFound(w, req) + case "mach-o": + fmt.Fprint(w, hostRunner) + case "wrong-arch": + fmt.Fprint(w, bindFixtureLinuxRunner("arm64", "new helper")) + case "truncated-header": + fmt.Fprint(w, linuxRunner[:64]) + case "truncated-segment": + fmt.Fprint(w, linuxRunner[:len(linuxRunner)-1]) + default: + fmt.Fprint(w, bindFixtureLinuxRunner(arch, "new helper")) + } + case "/runners/agentflow-gate-mcp": + if tc.missingRequiredGate && targetOS == "darwin" && arch == "arm64" { + http.NotFound(w, req) + return + } + fmt.Fprintf(w, "gate %s/%s", targetOS, arch) + default: + http.NotFound(w, req) + } + })) + defer server.Close() + args := []string{script, "--bin-dir", bin, "--no-start"} + helper := filepath.Join(bin, "agentflow-runner-linux-"+tc.arch) + patternEnv := "" + if tc.templatePath { + patternEnv = filepath.Join(bundle, "{os}", "{arch}", "agentflow-runner") + helper = filepath.Join(bundle, "linux", tc.arch, "agentflow-runner") + } + if tc.templateMissingArch { + patternEnv = filepath.Join(bundle, "{os}", "agentflow-runner") + helper = filepath.Join(bundle, "linux", "agentflow-runner") + oldHelper = bindFixtureLinuxRunner("amd64", "original Intel helper") + } + if tc.missingReplacementHelper { + oldHelper = bindFixtureLinuxRunner("amd64", "existing Intel helper") + helper = filepath.Join(bin, "agentflow-runner-linux-amd64") + if tc.templatePath { + helper = filepath.Join(bundle, "linux", "amd64", "agentflow-runner") + } + } + workingDir := filepath.Join(fixtureHome, "service directory") + if tc.relativeServiceTemplate { + helper = filepath.Join(workingDir, "relative helpers", "linux", "amd64", "agentflow-runner") + } + if tc.sharedTemplateDir { + amd64 := filepath.Join(bundle, "linux", "amd64") + if err := os.MkdirAll(amd64, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(amd64, filepath.Join(bundle, "linux", "arm64")); err != nil { + t.Fatal(err) + } + } + statePath := filepath.Join(fixtureHome, ".agentflow-runner", "state.json") + const state = `{"runnerId":"fixture"}` + envPath := filepath.Join(filepath.Dir(statePath), "env") + initialEnv := "AGENTFLOW_SERVER_URL=" + server.URL + "\n" + if tc.upgrade { + if err := os.Mkdir(filepath.Dir(statePath), 0o700); err != nil { + t.Fatal(err) + } + if tc.templateMissingArch { + initialEnv += "AGENTFLOW_RUNNER_BINARY=" + patternEnv + "\n" + patternEnv = "" // Use the saved configuration, with no process override. + } + if tc.trimSavedTemplate { + initialEnv += "AGENTFLOW_RUNNER_BINARY= " + patternEnv + " \n" + patternEnv = "" + } + for path, data := range map[string]string{ + statePath: state, + envPath: initialEnv, + filepath.Join(bin, "agentflow-runner"): oldHost, + } { + if err := os.WriteFile(path, []byte(data), 0o700); err != nil { + t.Fatal(err) + } + } + args = append(args, "--upgrade") + } else { + args = append(args, "--server", server.URL, "--token", "fixture") + } + switch tc.source { + case "from": + custom := filepath.Join(fixtureHome, "custom-runner") + if err := os.WriteFile(custom, []byte(hostRunner), 0o755); err != nil { + t.Fatal(err) + } + args = append(args, "--from", custom) + case "host-url": + args = append(args, "--download-url", server.URL+"/runners/agentflow-runner?os=darwin&arch={arch}") + case "template": + args = append(args, "--download-url", server.URL+"/runners/agentflow-runner?os={os}&arch={arch}") + } + plist := filepath.Join(fixtureHome, "Library", "LaunchAgents", "com.agentflow.runner.plist") + oldServiceArgs := []string{filepath.Join(bin, "agentflow-runner"), "-labels", "fixture", "-auto-update=true"} + if tc.missingReplacementHelper { + oldServiceArgs[len(oldServiceArgs)-1] = "-auto-update=false" + } + initialPlist := "" + if tc.launchd { + if err := os.MkdirAll(filepath.Dir(plist), 0o700); err != nil { + t.Fatal(err) + } + serviceConfig := "" + if tc.relativeServiceTemplate { + serviceConfig = fmt.Sprintf("WorkingDirectory%sEnvironmentVariablesAGENTFLOW_RUNNER_BINARYrelative helpers/{os}/{arch}/agentflow-runner", workingDir) + } + initialPlist = fmt.Sprintf("KeepAliveProgramArguments%s%s", strings.Join(oldServiceArgs, ""), serviceConfig) + if err := os.WriteFile(plist, []byte(initialPlist), 0o600); err != nil { + t.Fatal(err) + } + launchctl := "#!/bin/sh\nprintf '%s\\n' \"$1\" >> \"$HOME/launchctl.log\"\nif [ \"$1\" = load ]; then cp \"$2\" \"$HOME/reloaded.plist\"; fi\n" + if err := os.WriteFile(filepath.Join(bin, "launchctl"), []byte(launchctl), 0o755); err != nil { + t.Fatal(err) + } + } + oldGate := filepath.Join(bin, "agentflow-gate-mcp-linux-amd64") + if tc.missingReplacementHelper { + if err := os.WriteFile(oldGate, []byte("original Intel Gate"), 0o755); err != nil { + t.Fatal(err) + } + } + if (tc.upgrade && !tc.templatePath) || tc.source == "template" || tc.preserveHelper || tc.templateMissingArch || tc.missingReplacementHelper { + if err := os.MkdirAll(filepath.Dir(helper), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(helper, []byte(oldHelper), oldHelperMode); err != nil { + t.Fatal(err) + } + } + if tc.source == "checkout" { + if err := os.MkdirAll(filepath.Join(fixtureHome, "cmd", "agentflow-runner"), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(fixtureHome, "source-host"), []byte(hostRunner), 0o755); err != nil { + t.Fatal(err) + } + goBuild := "#!/bin/sh\n[ \"$GOOS\" = darwin ] && cp \"$HOME/source-host\" \"$3\"\n" + if err := os.WriteFile(filepath.Join(bin, "go"), []byte(goBuild), 0o755); err != nil { + t.Fatal(err) + } + } + install := func() string { + t.Helper() + cmd := exec.Command("/bin/bash", args...) + cmd.Dir = fixtureHome + cmd.Env = []string{"HOME=" + fixtureHome, "PATH=" + bin + ":" + filepath.Dir(curl) + ":/usr/bin:/bin", "AGENTFLOW_BROWSER=off"} + if patternEnv != "" { + cmd.Env = append(cmd.Env, "AGENTFLOW_RUNNER_BINARY="+patternEnv) + } + out, err := cmd.CombinedOutput() + if tc.helperAliasesRunner { + if err == nil || !strings.Contains(string(out), "helper 路径与 macOS Runner 相同") { + t.Fatalf("installer did not reject the conflicting path: %v\n%s", err, out) + } + } else if tc.sharedTemplateDir { + if err == nil || !strings.Contains(string(out), "两种架构的 Linux Git/MCP helper 路径相同") { + t.Fatalf("installer did not reject the shared template path: %v\n%s", err, out) + } + } else if tc.templateMissingArch { + if err == nil || !strings.Contains(string(out), "请在 AGENTFLOW_RUNNER_BINARY 中添加 {arch}") { + t.Fatalf("installer did not reject the old single-architecture template: %v\n%s", err, out) + } + } else if tc.missingRequiredGate { + if err == nil || !strings.Contains(string(out), "平台未提供 Gate MCP darwin/arm64") { + t.Fatalf("installer did not report the missing required Gate helper: %v\n%s", err, out) + } + } else if tc.missingReplacementHelper { + if err == nil || !strings.Contains(string(out), "缺少升级所需的 Linux/arm64 Git/MCP helper") { + t.Fatalf("installer did not preserve the working runner: %v\n%s", err, out) + } + } else if err != nil { + t.Fatalf("install: %v\n%s", err, out) + } + return string(out) + } + output := install() + if tc.templateMissingArch || tc.missingRequiredGate || tc.missingReplacementHelper { + preserved := map[string]string{ + filepath.Join(bin, "agentflow-runner"): oldHost, + helper: oldHelper, + envPath: initialEnv, + statePath: state, + } + if tc.launchd { + preserved[plist] = initialPlist + } + if tc.missingReplacementHelper { + preserved[oldGate] = "original Intel Gate" + } + for path, want := range preserved { + if got, err := os.ReadFile(path); err != nil || string(got) != want { + t.Fatalf("rejected upgrade changed %s: %q, %v", filepath.Base(path), got, err) + } + } + if _, err := os.Stat(filepath.Join(fixtureHome, "launchctl.log")); !os.IsNotExist(err) { + t.Fatalf("rejected upgrade restarted the service: %v", err) + } + return + } + if tc.universal && !tc.intelHardware && !tc.sharedTemplateDir && + !strings.Contains(output, "/usr/bin/arch -arm64 ") { + t.Fatalf("the manual launch command can inherit Rosetta: %s", output) + } + if tc.sharedTemplateDir { + if _, err := os.Stat(helper); !os.IsNotExist(err) { + t.Fatalf("the installer wrote a helper before rejecting the conflicting template: %v", err) + } + return + } + if tc.helperAliasesRunner { + if _, err := os.Stat(helper); !os.IsNotExist(err) { + t.Fatalf("installer published the runner before rejecting its conflicting helper path: %v", err) + } + return + } + if tc.source == "checkout" && !strings.Contains(output, "-auto-update=false") { + t.Fatalf("local source fallback did not disable automatic updates: %s", output) + } + if tc.templatePath && !tc.upgrade { + var start string + for line := range strings.SplitSeq(output, "\n") { + if command, ok := strings.CutPrefix(line, "[bind-cli] "); ok && strings.Contains(command, " -state ") { + start = command + } + } + if start == "" { + t.Fatalf("manual launch command missing: %s", output) + } + if err := os.WriteFile(filepath.Join(bin, "agentflow-runner"), []byte("#!/bin/sh\nprintf '%s' \"$AGENTFLOW_RUNNER_BINARY\"\n"), 0o755); err != nil { + t.Fatal(err) + } + cmd := exec.Command("/bin/bash", "-c", start) + cmd.Env = []string{"HOME=" + fixtureHome, "PATH=/usr/bin:/bin"} + if out, err := cmd.CombinedOutput(); err != nil || string(out) != patternEnv { + t.Fatalf("manual launch got helper template %q, %v, want %q", out, err, patternEnv) + } + if err := os.WriteFile(statePath, []byte(state), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Remove(helper); err != nil { + t.Fatal(err) + } + args = append(args, "--upgrade") + patternEnv = "" + install() + } + if tc.launchd { + if tc.templatePath { + // Use the saved service path during another upgrade without a shell override. + if err := os.Remove(helper); err != nil { + t.Fatal(err) + } + patternEnv = "" + } + install() // The same upgrade must keep only one disabling argument. + out, err := exec.Command("/usr/bin/plutil", "-extract", "ProgramArguments", "json", "-o", "-", filepath.Join(fixtureHome, "reloaded.plist")).CombinedOutput() + if err != nil { + t.Fatalf("read reloaded launchd arguments: %v %s", err, out) + } + var got []string + if err := json.Unmarshal(out, &got); err != nil || !slices.Equal(got, append(oldServiceArgs, "-auto-update=false")) { + t.Fatalf("launchd started with %q: %v", got, err) + } + } + assertArtifact := func(path, want string) { + t.Helper() + got, err := os.ReadFile(path) + if err != nil || string(got) != want { + t.Fatalf("%s = %q, %v; want %q", filepath.Base(path), got, err, want) + } + if info, err := os.Stat(path); err != nil || info.Mode().Perm()&0o100 == 0 { + t.Fatalf("%s is not executable: %v", path, err) + } + } + assertArtifact(filepath.Join(bin, "agentflow-runner"), hostRunner) + if tc.universal && !tc.intelHardware && !tc.arm64OnlyArtifacts { + assertArtifact(filepath.Join(bin, "agentflow-runner-linux-amd64"), bindFixtureLinuxRunner("amd64", "new helper")) + assertArtifact(filepath.Join(bin, "agentflow-gate-mcp-darwin-amd64"), "gate darwin/amd64") + assertArtifact(filepath.Join(bin, "agentflow-gate-mcp-darwin-arm64"), "gate darwin/arm64") + } + if tc.intelHardware { + if _, err := os.Stat(filepath.Join(bin, "agentflow-runner-linux-arm64")); !os.IsNotExist(err) { + t.Fatalf("Intel install wrote an arm64 helper: %v", err) + } + } + if tc.arm64OnlyArtifacts { + if _, err := os.Stat(filepath.Join(bin, "agentflow-gate-mcp-linux-amd64")); !os.IsNotExist(err) { + t.Fatalf("missing optional artifact changed the amd64 Gate helper: %v", err) + } + } + switch { + case tc.hostOS == "linux": + if _, err := os.Stat(helper); !os.IsNotExist(err) { + t.Fatalf("Linux install wrote a duplicate runner: %v", err) + } + case tc.preserveHelper: + assertArtifact(helper, oldHelper) + default: + assertArtifact(helper, linuxRunner) + assertArtifact(filepath.Join(bin, "agentflow-gate-mcp-linux-"+tc.arch), "gate linux/"+tc.arch) + } + if tc.templatePath { + if _, err := os.Stat(filepath.Join(bin, "agentflow-runner-linux-"+tc.arch)); !os.IsNotExist(err) { + t.Fatalf("the installer also wrote the fallback helper path: %v", err) + } + } + if tc.upgrade { + if got, err := os.ReadFile(statePath); err != nil || string(got) != state { + t.Fatalf("upgrade changed runner state: %q, %v", got, err) + } + } + }) + } +} + +func bindFixtureLinuxRunner(arch, payload string) string { + const elfHeaderSize, programHeaderSize = 64, 56 + header := make([]byte, elfHeaderSize+programHeaderSize) + copy(header, "\x7fELF\x02\x01\x01\x00") + machine := elf.EM_X86_64 + if arch == "arm64" { + machine = elf.EM_AARCH64 + } + binary.LittleEndian.PutUint16(header[16:], uint16(elf.ET_EXEC)) + binary.LittleEndian.PutUint16(header[18:], uint16(machine)) + binary.LittleEndian.PutUint32(header[20:], uint32(elf.EV_CURRENT)) + binary.LittleEndian.PutUint64(header[32:], elfHeaderSize) + binary.LittleEndian.PutUint16(header[52:], elfHeaderSize) + binary.LittleEndian.PutUint16(header[54:], programHeaderSize) + binary.LittleEndian.PutUint16(header[56:], 1) + program := header[elfHeaderSize:] + binary.LittleEndian.PutUint32(program, uint32(elf.PT_LOAD)) + binary.LittleEndian.PutUint32(program[4:], uint32(elf.PF_R|elf.PF_X)) + size := uint64(len(header) + len(payload)) + binary.LittleEndian.PutUint64(program[32:], size) + binary.LittleEndian.PutUint64(program[40:], size) + return string(header) + payload +} diff --git a/docs/deployment.md b/docs/deployment.md index 0d1c79995..ed1819455 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -133,6 +133,12 @@ The upstream process roles remain `-role api`, `-role worker` and `-role all`. A Personal runners are connected from the execution-machine page with a short-lived binding code. The installer is served at `/runners/install.sh`. `AGENTFLOW_RUNNER_BINARY` can point to binaries with `{os}` and `{arch}` substitutions. Container images bundle Linux/macOS amd64/arm64 runner and gate binaries. Native source installations can build on demand with Go. +Docker workspaces on macOS use a Linux Runner executable for Git credentials and MCP calls. The installer chooses the architecture of the installed macOS Runner, including Intel binaries running under Rosetta. For a universal Runner on Apple Silicon it installs the arm64 helpers and adds the amd64 helpers when available. Foreground starts and the command printed by `--no-start` select the native arm64 slice, including from a Rosetta shell. On Intel Macs the installer installs the amd64 helpers. By default, it places the helpers next to the host binary. A Runner-side `AGENTFLOW_RUNNER_BINARY` template with `{os}` selects the installation and runtime path. To install both helpers, use `{arch}` and give each architecture a distinct destination. The installer saves the template for upgrades and includes it in launchd and in the startup command printed by `--no-start`. Helper paths must resolve to files separate from the macOS Runner. + +The installer checks the helper destinations against the staged macOS binary and installs the required Gate MCP helpers before replacing an installed Runner. When the existing Runner has a working Docker helper, an upgrade requires a compatible helper at the new Runner's configured path before replacing the binary and restarting the service. Relative templates in the installed launchd configuration use the job's working directory. The installer and updater validate the Linux executable's format, architecture, and load segments before replacing it. Custom installs keep a compatible existing helper. A Runner built from a local checkout can build its companion from that checkout. A download URL with an `{os}` placeholder can supply both binaries when used as the Runner's download source. + +When platform updates are enabled, macOS runners synchronize the helper in the background at startup. A failed version check is retried during rescans, including when an older helper remains usable. A readable, compatible local ELF or a locally configured script wrapper can be copied into a Docker workspace with executable permissions while the sync runs. Operations that need a missing helper for Git or MCP can wait up to five seconds for the initial sync. The background download continues within its own transfer timeout. + A platform administrator can share one CodeBuddy API Key under **Platform administration → Shared CodeBuddy Key** (平台共享 CodeBuddy Key): pick the network environment that issued it and the models members may use (the first is the default). Members without a key of their own then chat and run SOPs on it, limited to those models; a role configured with another model runs on the default and the run says so. Members with their own key keep using it with any model. Scheduled tasks, clarification, reports, bots and run evaluation still need the runner's own key. A scheduled task uses the key for its selected CLI and does not borrow the shared CodeBuddy key. The key is stored in the database like personal keys and is never shown again after saving. `AGENTFLOW_ASSISTANT_SPONSOR_LOGIN`, which lent one account's key limited to `deepseek-v4.1-flash`, still applies while no shared key is configured. Configure provider credentials in the platform UI, not the host's global CLI configuration. Credentials are bound to the responsible actor by the upstream runtime. The home composer and SOP roles each choose a built-in CLI independently. Select the executor beside the home model selector; its API key (and optional Codex/Claude base URL) comes from your personal settings. Changing the executor preserves the draft; saved conversations record the executor used. Main Bot writes still pass through the platform tool gateway and its approval checks. Requirement clarification and notifications continue to use CodeBuddy. A scheduled assistant task can select any supported CLI and runs as an ordinary chat turn; Codex and Claude Code still need that account's own credential. Live model tests are opt-in; set `AGENTFLOW_LIVE_TESTS=1` only for the acceptance suites that need to load local credentials. diff --git a/internal/runner/daemon.go b/internal/runner/daemon.go index f3c8c1696..a81144fa9 100644 --- a/internal/runner/daemon.go +++ b/internal/runner/daemon.go @@ -48,6 +48,7 @@ type Daemon struct { updateMu sync.RWMutex updateReady atomic.Bool updateDownloadMu sync.Mutex + dockerHelperSyncDone chan struct{} // closed after the initial macOS helper sync cancelClaimLoop context.CancelFunc heartbeatWake chan struct{} maintenance runnerMaintenanceState @@ -157,7 +158,10 @@ func (d *Daemon) Run(ctx context.Context) error { claimCtx, cancelClaim := context.WithCancel(ctx) defer cancelClaim() d.cancelClaimLoop = cancelClaim - go d.rescanLoop(ctx) + if goruntime.GOOS == "darwin" && !d.cfg.DisableAutoUpdate { + d.dockerHelperSyncDone = make(chan struct{}) + } + go d.rescanLoop(ctx, 5*time.Minute) heartbeatDone := make(chan struct{}) go func() { defer close(heartbeatDone); d.heartbeatLoop(ctx) }() controlDone := make(chan struct{}) @@ -172,6 +176,9 @@ func (d *Daemon) Run(ctx context.Context) error { <-controlDone d.maintenance.jobs.Wait() <-heartbeatDone + if d.dockerHelperSyncDone != nil { + <-d.dockerHelperSyncDone + } closeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) d.closeAllRunSandboxes(closeCtx) cancel() @@ -371,8 +378,13 @@ func (d *Daemon) availableProviders() []string { return providers } -func (d *Daemon) rescanLoop(ctx context.Context) { - ticker := time.NewTicker(5 * time.Minute) +func (d *Daemon) rescanLoop(ctx context.Context, interval time.Duration) { + helperSynced := false + if d.dockerHelperSyncDone != nil { + helperSynced = d.refreshDockerRunnerHelper(ctx, false) + close(d.dockerHelperSyncDone) + } + ticker := time.NewTicker(interval) defer ticker.Stop() for { select { @@ -380,6 +392,9 @@ func (d *Daemon) rescanLoop(ctx context.Context) { return case <-ticker.C: d.refreshRuntimes(ctx) + if d.dockerHelperSyncDone != nil { + helperSynced = d.refreshDockerRunnerHelper(ctx, helperSynced) + } d.reapIdleRunSandboxes(ctx, runSandboxIdleTTL) } } @@ -531,6 +546,7 @@ func (d *Daemon) executeTask(ctx context.Context, task domain.RunnerTask) { // locks, cloning repositories, creating sandboxes and collecting outputs too. ctx, stopLease := d.keepTaskLease(ctx, task, leaseRenewInterval) defer stopLease() + ctx = d.withDockerRunnerHelperWait(ctx) // The read fence ends before the completion report: once the control plane // sees this task terminal (and lets a paused run continue), a follow-up // preparation or snapshot must not still find the gate held. @@ -761,7 +777,7 @@ func (d *Daemon) executeTerminal(ctx context.Context, task domain.RunnerTask) ex if err := runtime.ProvisionMCPAssets(ctx, sandbox, workspace, bound, nil); err != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("provision MCP: %v", err)} } - env, err := runtime.ScriptMCPEnvironment(sandbox, workspace, bound.MCPServers) + env, err := runtime.ScriptMCPEnvironment(ctx, sandbox, workspace, bound.MCPServers) if err != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare mcp call: %v", err)} } @@ -964,7 +980,7 @@ func (d *Daemon) executeInvocation(ctx context.Context, task domain.RunnerTask) gateAssets, _, _ := runtime.PrepareMCPServers(domain.AgentAssetBundle{MCPServers: servers}, d.mcpTarget(false, inv.GitHTTPCredentials)) servers = gateAssets.MCPServers } - mcpEnv, mcpErr := runtime.ScriptMCPEnvironment(sandbox, workspace, servers) + mcpEnv, mcpErr := runtime.ScriptMCPEnvironment(ctx, sandbox, workspace, servers) if mcpErr != nil { return execResult{status: domain.RunnerTaskFailed, errMsg: fmt.Sprintf("prepare mcp call: %v", mcpErr)}, nil } diff --git a/internal/runner/update.go b/internal/runner/update.go index fddfee923..afaf0772a 100644 --- a/internal/runner/update.go +++ b/internal/runner/update.go @@ -3,6 +3,7 @@ package runner import ( "context" "crypto/sha256" + "debug/elf" "encoding/hex" "encoding/json" "errors" @@ -16,6 +17,8 @@ import ( "strings" "sync/atomic" "time" + + flowruntime "agentflow/internal/runtime" ) // ErrUpdateReady tells the entry point to exec the newly installed binary. @@ -51,14 +54,136 @@ func (d *Daemon) checkGateMCPUpdates(ctx context.Context) error { for _, targetOS := range targetOSes { path := strings.ReplaceAll(strings.ReplaceAll(pattern, "{os}", targetOS), "{arch}", runtime.GOARCH) endpoint := fmt.Sprintf("%s/runners/agentflow-gate-mcp?os=%s&arch=%s", d.client.base, targetOS, runtime.GOARCH) - if err := d.checkGateMCPUpdate(ctx, endpoint, path); err != nil { + if err := d.checkCompanionBinaryUpdate(ctx, "gate MCP", endpoint, path, nil, true); err != nil { return fmt.Errorf("%s/%s: %w", targetOS, runtime.GOARCH, err) } } return nil } -func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string) error { +// Until the first successful sync, a rescan must retry the version check even +// when an older helper is usable. Downloads use the normal updater's timeout. +func (d *Daemon) refreshDockerRunnerHelper(ctx context.Context, repairOnly bool) bool { + if err := d.checkDockerRunnerHelperUpdate(ctx, repairOnly); err != nil { + if ctx.Err() == nil { + log.Printf("Docker Git/MCP helper update: %v", err) + } + return false + } + return true +} + +func (d *Daemon) checkDockerRunnerHelperUpdate(ctx context.Context, repairOnly bool) error { + path, err := flowruntime.DockerRunnerHelperPath() + if err != nil { + return err + } + validateInstalled := func(path string) error { + return validateDockerRunnerHelper(path, false) + } + if repairOnly && validateInstalled(path) == nil { + return nil + } + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return fmt.Errorf("create Docker Git/MCP helper directory: %w", err) + } + // Sandboxes copy this executable into the workspace, so an atomic replacement + // can run alongside active tasks and requests waiting for the first download. + return d.checkCompanionBinaryUpdate(ctx, "Docker Git/MCP helper", d.runnerUpdateEndpoint("linux"), path, validateInstalled, false) +} + +func (d *Daemon) withDockerRunnerHelperWait(ctx context.Context) context.Context { + if d.dockerHelperSyncDone == nil { + return ctx + } + return flowruntime.WithDockerRunnerHelperWait(ctx, d.waitForDockerRunnerHelper) +} + +func (d *Daemon) waitForDockerRunnerHelper(ctx context.Context, path string) error { + select { + case <-d.dockerHelperSyncDone: + return nil + default: + } + // A workspace copy gets its own executable permissions. + if validateDockerRunnerHelper(path, true) == nil { + return nil + } + // A workspace request can give up while the download continues. It never + // controls the downloader's deadline. + waitCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + select { + case <-d.dockerHelperSyncDone: + return nil + case <-waitCtx.Done(): + return fmt.Errorf("Docker Git/MCP helper is still syncing: %w", waitCtx.Err()) + } +} + +func validateDockerRunnerHelper(path string, forWorkspace bool) error { + var machine elf.Machine + switch runtime.GOARCH { + case "arm64": + machine = elf.EM_AARCH64 + case "amd64": + machine = elf.EM_X86_64 + default: + return fmt.Errorf("unsupported Linux helper architecture %s", runtime.GOARCH) + } + info, err := os.Stat(path) + if err != nil { + return err + } + if !info.Mode().IsRegular() { + return fmt.Errorf("expected regular Linux/%s helper file", runtime.GOARCH) + } + if !forWorkspace && info.Mode().Perm()&0o111 == 0 { + return fmt.Errorf("expected executable Linux/%s helper", runtime.GOARCH) + } + source, err := os.Open(path) + if err != nil { + return err + } + defer source.Close() + if forWorkspace { + // Match the installer for local custom wrappers. Platform artifacts must be ELF. + var header [2]byte + if _, err := source.ReadAt(header[:], 0); err == nil && header == [2]byte{'#', '!'} { + return nil + } + } + file, err := elf.NewFile(source) + if err != nil { + return fmt.Errorf("expected Linux/%s ELF helper: %w", runtime.GOARCH, err) + } + if file.Class != elf.ELFCLASS64 || file.Data != elf.ELFDATA2LSB || file.Machine != machine || + (file.OSABI != elf.ELFOSABI_NONE && file.OSABI != elf.ELFOSABI_LINUX) || + (file.Type != elf.ET_EXEC && file.Type != elf.ET_DYN) { + return fmt.Errorf("expected Linux/%s executable, got ELF %s %s %s %s %s", runtime.GOARCH, + file.Class, file.Data, file.Machine, file.OSABI, file.Type) + } + size := uint64(info.Size()) + hasExecutableSegment := false + for _, segment := range file.Progs { + if segment.Type != elf.PT_NULL && segment.Filesz > 0 && + (segment.Off > size || segment.Filesz > size-segment.Off) { + return fmt.Errorf("incomplete Linux/%s ELF helper segment", runtime.GOARCH) + } + if segment.Type == elf.PT_LOAD { + if segment.Filesz > segment.Memsz { + return fmt.Errorf("invalid Linux/%s ELF helper load segment", runtime.GOARCH) + } + hasExecutableSegment = hasExecutableSegment || segment.Filesz > 0 && segment.Flags&elf.PF_X != 0 + } + } + if !hasExecutableSegment { + return fmt.Errorf("Linux/%s ELF helper has no executable load segment", runtime.GOARCH) + } + return nil +} + +func (d *Daemon) checkCompanionBinaryUpdate(ctx context.Context, name, endpoint, path string, validate func(string) error, requireIdle bool) error { metadata, err := runnerBinaryMetadata(ctx, endpoint) if err != nil { return err @@ -67,7 +192,7 @@ func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string) if err != nil && !os.IsNotExist(err) { return err } - if localHash == metadata.SHA256 { + if strings.EqualFold(localHash, metadata.SHA256) && (validate == nil || validate(path) == nil) { return nil } tmp, err := downloadRunnerBinary(ctx, endpoint, path, metadata.SHA256) @@ -75,9 +200,21 @@ func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string) return err } defer os.Remove(tmp) - d.updateMu.Lock() - defer d.updateMu.Unlock() - if d.updateReady.Load() || atomic.LoadInt32(&d.running) != 0 || ctx.Err() != nil { + if validate != nil { + if err := validate(tmp); err != nil { + return fmt.Errorf("validate %s: %w", name, err) + } + } + if requireIdle { + if !d.updateMu.TryLock() { + return nil + } + defer d.updateMu.Unlock() + if d.runnerExecutionActive() { + return nil + } + } + if d.updateReady.Load() || ctx.Err() != nil { return nil } current, err := hashFile(path) @@ -85,12 +222,12 @@ func (d *Daemon) checkGateMCPUpdate(ctx context.Context, endpoint, path string) return err } if current != localHash { - return errors.New("gate MCP executable changed during update") + return fmt.Errorf("%s executable changed during update", name) } if err := os.Rename(tmp, path); err != nil { - return fmt.Errorf("replace gate MCP executable: %w", err) + return fmt.Errorf("replace %s executable: %w", name, err) } - log.Printf("gate MCP update installed (%s) at %s", metadata.SHA256[:12], path) + log.Printf("%s update installed (%s) at %s", name, metadata.SHA256[:12], path) return nil } @@ -107,8 +244,8 @@ func currentRunnerExecutable() (string, error) { var errRunnerUpdateBusy = errors.New("runner is busy") -func (d *Daemon) runnerUpdateEndpoint() string { - return fmt.Sprintf("%s/runners/agentflow-runner?os=%s&arch=%s", d.client.base, runtime.GOOS, runtime.GOARCH) +func (d *Daemon) runnerUpdateEndpoint(targetOS string) string { + return fmt.Sprintf("%s/runners/agentflow-runner?os=%s&arch=%s", d.client.base, targetOS, runtime.GOARCH) } func (d *Daemon) runnerUpdateHashes(ctx context.Context, binary string) (local, expected string, err error) { @@ -116,7 +253,7 @@ func (d *Daemon) runnerUpdateHashes(ctx context.Context, binary string) (local, if err != nil { return "", "", err } - remote, err := runnerBinaryMetadata(ctx, d.runnerUpdateEndpoint()) + remote, err := runnerBinaryMetadata(ctx, d.runnerUpdateEndpoint(runtime.GOOS)) return local, remote.SHA256, err } @@ -157,7 +294,7 @@ func (d *Daemon) installRunnerUpdate(ctx context.Context, binary string) (bool, if strings.EqualFold(localHash, expectedHash) { return false, nil } - tmp, err := downloadRunnerBinary(ctx, d.runnerUpdateEndpoint(), binary, expectedHash) + tmp, err := downloadRunnerBinary(ctx, d.runnerUpdateEndpoint(runtime.GOOS), binary, expectedHash) if err != nil { return false, err } diff --git a/internal/runner/update_test.go b/internal/runner/update_test.go index 6fe88879d..6a68307a2 100644 --- a/internal/runner/update_test.go +++ b/internal/runner/update_test.go @@ -3,6 +3,8 @@ package runner import ( "context" "crypto/sha256" + "debug/elf" + "encoding/binary" "encoding/hex" "encoding/json" "errors" @@ -16,6 +18,10 @@ import ( "sync/atomic" "testing" "time" + + flowruntime "agentflow/internal/runtime" + + "github.com/gorilla/websocket" ) func TestRunnerUpdateReplacesBinaryOnlyAfterVerification(t *testing.T) { @@ -163,6 +169,418 @@ func TestGateMCPHelperUpdatesWhileRunnerIdle(t *testing.T) { } } +func TestDockerRunnerHelperUpdateRepairsMissingBinary(t *testing.T) { + payload := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "Docker helper") + var downloads atomic.Int32 + var damaged atomic.Bool + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + query := req.URL.Query() + if req.URL.Path != "/runners/agentflow-runner" || query.Get("os") != "linux" || query.Get("arch") != runtime.GOARCH { + http.NotFound(w, req) + return + } + if query.Get("metadata") == "1" { + expected := payload + if damaged.Load() { + expected = "new companion" + } + hash := sha256.Sum256([]byte(expected)) + fmt.Fprintf(w, `{"sha256":%q}`, hex.EncodeToString(hash[:])) + return + } + downloads.Add(1) + if damaged.Load() { + fmt.Fprint(w, "partial data") + return + } + fmt.Fprint(w, payload) + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture"}) + if err != nil { + t.Fatal(err) + } + bin := t.TempDir() + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(bin, "{os}", "{arch}", "agentflow-runner")) + helper := filepath.Join(bin, "linux", runtime.GOARCH, "agentflow-runner") + ctx := context.Background() + assertHelper := func() { + t.Helper() + if got, err := os.ReadFile(helper); err != nil || string(got) != payload { + t.Fatalf("Docker helper = %q, %v", got, err) + } + if info, err := os.Stat(helper); err != nil || info.Mode().Perm()&0o100 == 0 { + t.Fatalf("helper is not executable: %v", err) + } + if d.updateReady.Load() { + t.Fatal("updating the companion must not restart the runner") + } + } + // A Docker workspace can already be waiting for the helper under the update fence. + d.updateMu.RLock() + atomic.StoreInt32(&d.running, 1) + err = d.checkDockerRunnerHelperUpdate(ctx, true) + atomic.StoreInt32(&d.running, 0) + d.updateMu.RUnlock() + if err != nil { + t.Fatal(err) + } + assertHelper() + if err := d.checkDockerRunnerHelperUpdate(ctx, false); err != nil { + t.Fatal(err) + } + if n := downloads.Load(); n != 1 { + t.Fatalf("already-current helper was re-downloaded, requests = %d", n) + } + damaged.Store(true) + if err := d.checkDockerRunnerHelperUpdate(ctx, false); err == nil || !strings.Contains(err.Error(), "checksum mismatch") { + t.Fatalf("replacement was not rejected: %v", err) + } + assertHelper() + damaged.Store(false) + if err := os.WriteFile(helper, []byte("invalid existing helper"), 0o755); err != nil { + t.Fatal(err) + } + if err := d.checkDockerRunnerHelperUpdate(ctx, true); err != nil { + t.Fatal(err) + } + assertHelper() + if err := os.Chmod(helper, 0o644); err != nil { + t.Fatal(err) + } + if err := d.checkDockerRunnerHelperUpdate(ctx, true); err != nil { + t.Fatal(err) + } + assertHelper() +} + +func TestDockerRunnerHelperUpdateRejectsRunningExecutablePath(t *testing.T) { + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + if err := os.Symlink(filepath.Dir(self), filepath.Join(root, "linux")); err != nil { + t.Fatal(err) + } + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "{os}", filepath.Base(self))) + var requests atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + requests.Add(1) + w.WriteHeader(http.StatusServiceUnavailable) + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture"}) + if err != nil { + t.Fatal(err) + } + if err := d.checkDockerRunnerHelperUpdate(t.Context(), false); err == nil || !strings.Contains(err.Error(), "aliases the host Runner") { + t.Fatalf("conflicting helper path was not rejected: %v", err) + } + if got := requests.Load(); got != 0 { + t.Fatalf("updater requested an artifact for a conflicting path, requests = %d", got) + } +} + +func TestDockerRunnerHelperUpdateRejectsIncompatibleBinary(t *testing.T) { + otherArch := "arm64" + if runtime.GOARCH == otherArch { + otherArch = "amd64" + } + complete := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "full program") + for _, tc := range []struct{ name, payload string }{ + {name: "macOS_binary", payload: "\xcf\xfa\xed\xfemacOS runner"}, + {name: "wrong_Linux_architecture", payload: dockerRunnerFixtureLinuxBinary(otherArch, "wrong architecture")}, + {name: "script_wrapper", payload: "#!/bin/sh\nexit 0\n"}, + {name: "only_ELF_header", payload: complete[:64]}, + {name: "truncated_load_segment", payload: complete[:len(complete)-1]}, + } { + t.Run(tc.name, func(t *testing.T) { + hash := sha256.Sum256([]byte(tc.payload)) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if req.URL.Query().Get("metadata") == "1" { + fmt.Fprintf(w, `{"sha256":%q}`, hex.EncodeToString(hash[:])) + return + } + fmt.Fprint(w, tc.payload) + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture"}) + if err != nil { + t.Fatal(err) + } + bin := t.TempDir() + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(bin, "agentflow-runner-{os}-{arch}")) + helper := filepath.Join(bin, "agentflow-runner-linux-"+runtime.GOARCH) + original := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "working helper") + if err := os.WriteFile(helper, []byte(original), 0o755); err != nil { + t.Fatal(err) + } + updateErr := d.checkDockerRunnerHelperUpdate(t.Context(), false) + got, err := os.ReadFile(helper) + if err != nil || string(got) != original { + t.Fatalf("incompatible download replaced the helper: %q, read error %v, update error %v", got, err, updateErr) + } + if updateErr == nil || !strings.Contains(updateErr.Error(), "Linux/"+runtime.GOARCH) { + t.Fatalf("expected a platform validation error, got %v", updateErr) + } + }) + } +} + +func TestDockerRunnerHelperDownloadDoesNotBlockMacOSStartup(t *testing.T) { + if runtime.GOOS != "darwin" { + t.Skip("macOS Runner downloads the Linux helper at startup") + } + t.Run("readable_ELF", func(t *testing.T) { + testDockerRunnerHelperDuringStartup(t, dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "working helper"), 0o644) + }) + t.Run("local_script_wrapper", func(t *testing.T) { + testDockerRunnerHelperDuringStartup(t, "#!/bin/sh\nexec /usr/local/bin/agentflow-runner \"$@\"\n", 0o755) + }) +} + +func testDockerRunnerHelperDuringStartup(t *testing.T, original string, mode os.FileMode) { + t.Helper() + root := t.TempDir() + t.Setenv("HOME", root) + t.Setenv("PATH", "/usr/bin:/bin") + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "agentflow-runner-{os}-{arch}")) + helper := filepath.Join(root, "agentflow-runner-linux-"+runtime.GOARCH) + if err := os.WriteFile(helper, []byte(original), mode); err != nil { + t.Fatal(err) + } + updated := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "updated helper") + hash := sha256.Sum256([]byte(updated)) + helperRequested := make(chan struct{}, 1) + allowMetadata := make(chan struct{}) + heartbeat := make(chan struct{}, 1) + controlHello := make(chan struct{}, 1) + claim := make(chan struct{}, 1) + signal := func(ch chan struct{}) { + select { + case ch <- struct{}{}: + default: + } + } + upgrader := websocket.Upgrader{} + ctx, cancel := context.WithCancel(t.Context()) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + switch req.URL.Path { + case "/runners/agentflow-runner": + if req.URL.Query().Get("os") != "linux" || req.URL.Query().Get("arch") != runtime.GOARCH { + http.NotFound(w, req) + return + } + if req.URL.Query().Get("metadata") == "1" { + signal(helperRequested) + select { + case <-allowMetadata: + fmt.Fprintf(w, `{"sha256":%q}`, hex.EncodeToString(hash[:])) + case <-req.Context().Done(): + } + return + } + fmt.Fprint(w, updated) + case "/runners/heartbeat": + signal(heartbeat) + fmt.Fprint(w, `{}`) + case "/runners/connect": + conn, err := upgrader.Upgrade(w, req, nil) + if err != nil { + return + } + defer conn.Close() + var hello controlMessage + if conn.ReadJSON(&hello) == nil && hello.Type == "hello" { + signal(controlHello) + _, _, _ = conn.NextReader() + } + case "/runners/tasks/claim": + signal(claim) + <-ctx.Done() + case "/runners/disconnect": + w.WriteHeader(http.StatusNoContent) + default: + http.NotFound(w, req) + } + })) + defer func() { cancel(); server.Close() }() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture", StatePath: filepath.Join(root, "state.json")}) + if err != nil { + t.Fatal(err) + } + if err := d.saveState(daemonState{RunnerID: "runner", SessionToken: "session", ExpiresAt: time.Now().Add(24 * time.Hour)}); err != nil { + t.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- d.Run(ctx) }() + defer func() { + cancel() + if err := <-done; err != nil { + t.Errorf("run: %v", err) + } + }() + wait := func(ch <-chan struct{}, event string) { + t.Helper() + select { + case <-ch: + case <-time.After(5 * time.Second): + t.Fatalf("did not observe %s before the Linux helper download completed", event) + } + } + wait(helperRequested, "Linux helper metadata request") + wait(heartbeat, "heartbeat") + wait(controlHello, "control hello") + wait(claim, "task polling") + workspaceCtx, cancelWorkspace := context.WithTimeout(ctx, time.Second) + defer cancelWorkspace() + workspace := t.TempDir() + if _, err := flowruntime.PrepareMCPCall(d.withDockerRunnerHelperWait(workspaceCtx), dockerHelperFixtureSandbox{}, workspace, nil); err != nil { + t.Fatalf("existing readable helper should be available while metadata is pending: %v", err) + } + workspaceHelper := filepath.Join(workspace, ".agentflow", "runtime", "bin", "agentflow-helper") + if got, err := os.ReadFile(workspaceHelper); err != nil || string(got) != original { + t.Fatalf("workspace helper = %q, %v", got, err) + } + if info, err := os.Stat(workspaceHelper); err != nil || info.Mode().Perm()&0o100 == 0 { + t.Fatalf("workspace helper did not receive executable permissions: %v", err) + } + close(allowMetadata) + waitFor(t, 5*time.Second, "helper update", func() bool { + got, err := os.ReadFile(helper) + return err == nil && string(got) == updated + }) +} + +type dockerHelperFixtureSandbox struct{ flowruntime.Sandbox } + +func (dockerHelperFixtureSandbox) Driver() string { return "docker" } + +func TestDockerRunnerHelperRescanRetriesFailedVersionCheck(t *testing.T) { + root := t.TempDir() + t.Setenv("HOME", root) + t.Setenv("PATH", "/usr/bin:/bin") + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "agentflow-runner-{os}-{arch}")) + helper := filepath.Join(root, "agentflow-runner-linux-"+runtime.GOARCH) + if err := os.WriteFile(helper, []byte(dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "old version")), 0o755); err != nil { + t.Fatal(err) + } + updated := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "updated version") + hash := sha256.Sum256([]byte(updated)) + var metadataRequests atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if req.URL.Query().Get("metadata") == "1" { + if metadataRequests.Add(1) == 1 { + w.WriteHeader(http.StatusServiceUnavailable) + return + } + fmt.Fprintf(w, `{"sha256":%q}`, hex.EncodeToString(hash[:])) + return + } + fmt.Fprint(w, updated) + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture"}) + if err != nil { + t.Fatal(err) + } + d.dockerHelperSyncDone = make(chan struct{}) + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan struct{}) + go func() { + defer close(done) + d.rescanLoop(ctx, 50*time.Millisecond) + }() + defer func() { cancel(); <-done }() + waitFor(t, 5*time.Second, "rescan to replace the old helper after a failed startup sync", func() bool { + data, err := os.ReadFile(helper) + return err == nil && string(data) == updated + }) + if got := metadataRequests.Load(); got != 2 { + t.Fatalf("metadata requests = %d, want a failed check and one retry", got) + } +} + +func TestDockerRunnerHelperSlowDownloadOutlivesWorkspaceWait(t *testing.T) { + root := t.TempDir() + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "agentflow-runner-{os}-{arch}")) + helper := filepath.Join(root, "agentflow-runner-linux-"+runtime.GOARCH) + binary := dockerRunnerFixtureLinuxBinary(runtime.GOARCH, "complete slow download") + hash := sha256.Sum256([]byte(binary)) + downloading := make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if req.URL.Query().Get("metadata") == "1" { + fmt.Fprintf(w, `{"sha256":%q}`, hex.EncodeToString(hash[:])) + return + } + fmt.Fprint(w, binary[:64]) + w.(http.Flusher).Flush() + close(downloading) + // Keep the transfer active beyond the old shared 30-second deadline. + timer := time.NewTimer(31 * time.Second) + defer timer.Stop() + select { + case <-timer.C: + fmt.Fprint(w, binary[64:]) + case <-req.Context().Done(): + } + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture"}) + if err != nil { + t.Fatal(err) + } + d.dockerHelperSyncDone = make(chan struct{}) + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan struct{}) + go func() { + defer close(done) + d.rescanLoop(ctx, time.Hour) + }() + defer func() { cancel(); <-done }() + select { + case <-downloading: + case <-time.After(5 * time.Second): + t.Fatal("helper download did not start") + } + if err := d.waitForDockerRunnerHelper(ctx, helper); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("workspace wait = %v, want its own deadline", err) + } + select { + case <-d.dockerHelperSyncDone: + case <-time.After(40 * time.Second): + t.Fatal("helper sync did not complete") + } + if data, err := os.ReadFile(helper); err != nil || string(data) != binary { + t.Fatalf("slow transfer did not publish the verified helper: %q, %v", data, err) + } +} + +func dockerRunnerFixtureLinuxBinary(arch, payload string) string { + const elfHeaderSize, programHeaderSize = 64, 56 + header := make([]byte, elfHeaderSize+programHeaderSize) + copy(header, "\x7fELF\x02\x01\x01\x00") + machine := elf.EM_X86_64 + if arch == "arm64" { + machine = elf.EM_AARCH64 + } + binary.LittleEndian.PutUint16(header[16:], uint16(elf.ET_EXEC)) + binary.LittleEndian.PutUint16(header[18:], uint16(machine)) + binary.LittleEndian.PutUint32(header[20:], uint32(elf.EV_CURRENT)) + binary.LittleEndian.PutUint64(header[32:], elfHeaderSize) + binary.LittleEndian.PutUint16(header[52:], elfHeaderSize) + binary.LittleEndian.PutUint16(header[54:], programHeaderSize) + binary.LittleEndian.PutUint16(header[56:], 1) + program := header[elfHeaderSize:] + binary.LittleEndian.PutUint32(program, uint32(elf.PT_LOAD)) + binary.LittleEndian.PutUint32(program[4:], uint32(elf.PF_R|elf.PF_X)) + size := uint64(len(header) + len(payload)) + binary.LittleEndian.PutUint64(program[32:], size) + binary.LittleEndian.PutUint64(program[40:], size) + return string(header) + payload +} + func TestRunnerUpdateDefersWhileWorkspaceRequestIsActive(t *testing.T) { const updated = "new runner" hash := sha256.Sum256([]byte(updated)) diff --git a/internal/runner/workspace.go b/internal/runner/workspace.go index 57842f0b2..4889c0fdd 100644 --- a/internal/runner/workspace.go +++ b/internal/runner/workspace.go @@ -120,6 +120,7 @@ func (d *Daemon) readRunnerWorkspace(ctx context.Context, request domain.RunnerW if d.updateReady.Load() { return domain.RunnerWorkspaceResponse{Status: http.StatusConflict, Error: "Runner 即将重启,请稍后重试"} } + ctx = d.withDockerRunnerHelperWait(ctx) if request.Operation == "conversation_process" { return d.conversationProcess(ctx, request) } diff --git a/internal/runner/workspace_browser_test.go b/internal/runner/workspace_browser_test.go index 15b146402..44611c6f6 100644 --- a/internal/runner/workspace_browser_test.go +++ b/internal/runner/workspace_browser_test.go @@ -3,11 +3,17 @@ package runner import ( "context" "encoding/json" + "fmt" "net/http" + "net/http/httptest" + "os" + "path/filepath" "testing" "time" "agentflow/internal/domain" + + "github.com/gorilla/websocket" ) func TestHandleRunnerWorkspaceBrowserRejectsMissingBrowser(t *testing.T) { @@ -52,6 +58,102 @@ func TestHandleRunnerWorkspaceBrowserReportsEnsureFailure(t *testing.T) { } } +func TestDockerBrowserRequestsDoNotWaitForRunnerHelper(t *testing.T) { + root := t.TempDir() + t.Setenv("AGENTFLOW_BROWSER", "always") + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "{os}", "{arch}", "agentflow-runner")) + // Exercise the Docker browser path without launching a real browser. + if err := os.WriteFile(filepath.Join(root, "docker"), []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", root+":/usr/bin:/bin") + + const runID = "browser-helper-sync" + if err := os.MkdirAll(filepath.Join(root, "runs", runID, "workspace"), 0o700); err != nil { + t.Fatal(err) + } + request := domain.RunnerWorkspaceRequest{ + RunID: runID, WorkspaceLayout: domain.RunnerWorkspaceLayoutShared, + Operation: domain.RunnerWorkspaceOperationBrowser, Browser: &domain.BrowserRequest{Ensure: true}, + Sandbox: &domain.RunnerSandboxSpec{ + Profile: domain.SandboxProfile{Driver: domain.SandboxDriverDocker}, + Dockerfile: &domain.DockerfileDefinition{Content: "FROM fixture"}, + }, + Workflow: &domain.WorkflowDefinition{}, + } + upgrader := websocket.Upgrader{} + result := make(chan error, 1) + probe := func(conn *websocket.Conn) error { + if err := conn.SetReadDeadline(time.Now().Add(4 * time.Second)); err != nil { + return err + } + var hello controlMessage + if err := conn.ReadJSON(&hello); err != nil { + return err + } + const slots = 8 + for index := range slots { + if err := conn.WriteJSON(controlMessage{ + Type: "workspace.request", RequestID: fmt.Sprint(index), WorkspaceRequest: &request, + }); err != nil { + return err + } + } + for range slots { + var response controlMessage + if err := conn.ReadJSON(&response); err != nil { + return fmt.Errorf("browser requests waited for the helper: %w", err) + } + if response.WorkspaceResponse == nil || response.WorkspaceResponse.Status != http.StatusOK { + return fmt.Errorf("unexpected browser response: %+v", response.WorkspaceResponse) + } + } + info := domain.RunnerWorkspaceRequest{RunID: runID, WorkspaceLayout: domain.RunnerWorkspaceLayoutShared, Operation: domain.RunnerWorkspaceOperationInfo} + if err := conn.WriteJSON(controlMessage{Type: "workspace.request", RequestID: "info", WorkspaceRequest: &info}); err != nil { + return err + } + var response controlMessage + if err := conn.ReadJSON(&response); err != nil { + return err + } + if response.WorkspaceResponse == nil || response.WorkspaceResponse.Status != http.StatusOK { + return fmt.Errorf("workspace info did not have a free slot: %+v", response.WorkspaceResponse) + } + return nil + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + conn, err := upgrader.Upgrade(w, req, nil) + if err != nil { + result <- err + return + } + defer conn.Close() + result <- probe(conn) + })) + defer server.Close() + d, err := New(Config{ServerURL: server.URL, BootstrapToken: "fixture", DefaultWorkspaceRoot: root}) + if err != nil { + t.Fatal(err) + } + // Model a cold start whose first helper download is still in flight. + d.dockerHelperSyncDone = make(chan struct{}) + ctx, cancel := context.WithCancel(t.Context()) + done := make(chan struct{}) + go func() { + defer close(done) + d.controlLoop(ctx) + }() + defer func() { cancel(); <-done }() + select { + case err := <-result: + if err != nil { + t.Fatal(err) + } + case <-time.After(5 * time.Second): + t.Fatal("control loop did not complete the browser and workspace requests") + } +} + // 只读操作(status/frame)不续期沙箱,写操作续期;HandleBrowser 报错映射为 409。 func TestHandleRunnerWorkspaceBrowserTouchesSandboxOnlyForWrites(t *testing.T) { const runID = "run-browser-touch" diff --git a/internal/runtime/agent_cli_integration_test.go b/internal/runtime/agent_cli_integration_test.go index fb5eb0aff..bd00189c7 100644 --- a/internal/runtime/agent_cli_integration_test.go +++ b/internal/runtime/agent_cli_integration_test.go @@ -23,7 +23,7 @@ type credentialSandbox struct { } func (s credentialSandbox) Exec(ctx context.Context, command CommandSpec, sink EventSink) (string, error) { - bound, err := bindCommand(s.localSandbox, s.workspace, command, s.credentials) + bound, err := bindCommand(ctx, s.localSandbox, s.workspace, command, s.credentials) if err != nil { return "", err } diff --git a/internal/runtime/agent_cli_test.go b/internal/runtime/agent_cli_test.go index c518421da..ff6a1cbb6 100644 --- a/internal/runtime/agent_cli_test.go +++ b/internal/runtime/agent_cli_test.go @@ -396,7 +396,7 @@ func TestBindCommandRejectsSymlinkedParentsBeforeWritingCredentials(t *testing.T credentials := RunCredentials{AgentCLI: map[string]ProviderCredential{ kind: {APIKey: "fake-test-key", BaseURL: "https://example.test"}, }} - if _, err := bindCommand(sb, workspace, command, credentials); err == nil { + if _, err := bindCommand(t.Context(), sb, workspace, command, credentials); err == nil { t.Fatal("symlinked private-home component was accepted") } entries, err := os.ReadDir(outside) @@ -428,7 +428,7 @@ func TestBindAgentCLICommandRejectsSymlinkedPrivateHome(t *testing.T) { if _, _, err := BindAgentCLICommand(sb, workspace, command, ProviderCredential{APIKey: "secret-key", BaseURL: "https://example.test"}, command.Argv); err == nil { t.Fatal("symlinked private home was accepted") } - if _, err := bindCommand(sb, workspace, command, RunCredentials{}); err == nil { + if _, err := bindCommand(t.Context(), sb, workspace, command, RunCredentials{}); err == nil { t.Fatal("bindCommand accepted a symlinked private home") } if entries, _ := os.ReadDir(outside); len(entries) != 0 { diff --git a/internal/runtime/agent_cli_zcode_test.go b/internal/runtime/agent_cli_zcode_test.go index e9c8ace03..49f6e5bea 100644 --- a/internal/runtime/agent_cli_zcode_test.go +++ b/internal/runtime/agent_cli_zcode_test.go @@ -290,7 +290,7 @@ func TestZCodeBindCommandComposition(t *testing.T) { command.CredentialBindings = []CredentialBinding{BindAgentCLI, BindGit, BindMCP} command.PrivateHome = true command.PrivateHomeKey = "zcode-a" - bound, err := bindCommand(sb, workspace, command, RunCredentials{AgentCLI: map[string]ProviderCredential{AgentCLIZCode: {APIKey: "k"}}}) + bound, err := bindCommand(t.Context(), sb, workspace, command, RunCredentials{AgentCLI: map[string]ProviderCredential{AgentCLIZCode: {APIKey: "k"}}}) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/cli_live_test.go b/internal/runtime/cli_live_test.go index b2b6e12c5..c545ab29f 100644 --- a/internal/runtime/cli_live_test.go +++ b/internal/runtime/cli_live_test.go @@ -93,7 +93,7 @@ func runLiveSOP(t *testing.T, root, kind, driver string, item liveCLIConfig) { out = redactLive(out) if err != nil { shown := command.Argv - if bound, berr := bindCommand(sb, workspace, command, creds); berr == nil { + if bound, berr := bindCommand(t.Context(), sb, workspace, command, creds); berr == nil { shown = bound.Argv } t.Fatalf("执行失败: %s\nargv=%q\n%s", redactLive(err.Error()), shown, clipLive(out)) diff --git a/internal/runtime/command_binding.go b/internal/runtime/command_binding.go index fa4f155d3..c67768887 100644 --- a/internal/runtime/command_binding.go +++ b/internal/runtime/command_binding.go @@ -4,6 +4,7 @@ import ( "agentflow/internal/domain" "agentflow/internal/gitwork" "agentflow/internal/observability" + "context" "fmt" "os" "path/filepath" @@ -32,7 +33,7 @@ func upsertEnv(env []string, key, value string) []string { return out } -func bindCommand(sandbox Sandbox, workspace string, command CommandSpec, credentials RunCredentials) (CommandSpec, error) { +func bindCommand(ctx context.Context, sandbox Sandbox, workspace string, command CommandSpec, credentials RunCredentials) (CommandSpec, error) { if command.bindingsPrepared { return command, nil } @@ -114,11 +115,11 @@ func bindCommand(sandbox Sandbox, workspace string, command CommandSpec, credent } bound.environment["CODEBUDDY_INTERNET_ENVIRONMENT"] = strings.TrimSpace(credentials.CodeBuddy.InternetEnvironment) case BindGit: - if err := bindWorkspaceGitEnvironment(sandbox, workspace, bound.environment, credentials); err != nil { + if err := bindWorkspaceGitEnvironment(ctx, sandbox, workspace, bound.environment, credentials); err != nil { return CommandSpec{}, err } case BindGitWorkspace: - if err := bindWorkspaceGitEnvironment(sandbox, workspace, bound.environment, credentials); err != nil { + if err := bindWorkspaceGitEnvironment(ctx, sandbox, workspace, bound.environment, credentials); err != nil { return CommandSpec{}, err } case BindExtra: @@ -130,7 +131,7 @@ func bindCommand(sandbox Sandbox, workspace string, command CommandSpec, credent bound.credentialEnvKeys = append(bound.credentialEnvKeys, envName) } case BindMCP: - if err := bindMCPCallEnvironment(sandbox, workspace, bound.environment, credentials.MCPServers); err != nil { + if err := bindMCPCallEnvironment(ctx, sandbox, workspace, bound.environment, credentials.MCPServers); err != nil { return CommandSpec{}, err } bound.Argv = BindMCPCallArgv(sandbox, bound.Argv) @@ -302,7 +303,7 @@ func privateHomePaths(sandbox Sandbox, workspace string, command CommandSpec) (h // bindWorkspaceGitEnvironment installs the standard host-scoped helper. // It requests credentials through a live relay rather than writing tokens. -func bindWorkspaceGitEnvironment(sandbox Sandbox, workspace string, environment map[string]string, credentials RunCredentials) error { +func bindWorkspaceGitEnvironment(ctx context.Context, sandbox Sandbox, workspace string, environment map[string]string, credentials RunCredentials) error { environment["GIT_TERMINAL_PROMPT"] = "0" if _, err := EnsureControlDirectory(workspace, "runtime"); err != nil { return fmt.Errorf("create git runtime dir: %w", err) @@ -346,7 +347,7 @@ func bindWorkspaceGitEnvironment(sandbox Sandbox, workspace string, environment if _, err := EnsureControlDirectory(workspace, "runtime", "bin"); err != nil { return fmt.Errorf("create git helper dir: %w", err) } - if err := installMCPCallHelperForSandbox(sandbox, workspace, filepath.Join(hostBin, "agentflow-helper")); err != nil { + if err := installMCPCallHelperForSandbox(ctx, sandbox, workspace, filepath.Join(hostBin, "agentflow-helper")); err != nil { return fmt.Errorf("install git credential helper: %w", err) } helper := runtimePath("bin/agentflow-helper") diff --git a/internal/runtime/command_binding_telemetry_test.go b/internal/runtime/command_binding_telemetry_test.go index 48add7aea..bddc9ec27 100644 --- a/internal/runtime/command_binding_telemetry_test.go +++ b/internal/runtime/command_binding_telemetry_test.go @@ -38,14 +38,14 @@ func TestBindTelemetryEnvironmentSetsCollectorAndRunTags(t *testing.T) { func TestBindCommandAppliesTelemetryEnvironment(t *testing.T) { t.Setenv("AGENTFLOW_OTEL_TRACES_ENDPOINT", "http://collector.test/v1/traces") workspace := t.TempDir() - plain, err := bindCommand(&localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}}, RunCredentials{}) + plain, err := bindCommand(t.Context(), &localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}}, RunCredentials{}) if err != nil { t.Fatal(err) } if _, ok := plain.environment["AGENTFLOW_RUN_ID"]; ok { t.Fatalf("command without telemetry got telemetry env: %#v", plain.environment) } - traced, err := bindCommand(&localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}, Telemetry: &InvocationOptions{RunID: "run-2", NodeID: "node-b"}}, RunCredentials{}) + traced, err := bindCommand(t.Context(), &localSandbox{workspace: workspace}, workspace, CommandSpec{Argv: []string{"true"}, Telemetry: &InvocationOptions{RunID: "run-2", NodeID: "node-b"}}, RunCredentials{}) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/command_binding_workspace_git_test.go b/internal/runtime/command_binding_workspace_git_test.go index e1799fd7b..ff62acd35 100644 --- a/internal/runtime/command_binding_workspace_git_test.go +++ b/internal/runtime/command_binding_workspace_git_test.go @@ -129,7 +129,7 @@ func TestBindGitWorkspaceResetsCLIConfig(t *testing.T) { workspace := t.TempDir() sb := &localSandbox{workspace: workspace, driver: "docker"} env := map[string]string{SessionGatewayEnv: "http://relay.invalid/mcp"} - if err := bindWorkspaceGitEnvironment(sb, workspace, env, RunCredentials{GitHTTPHosts: map[string]string{"github.com": "github"}}); err != nil { + if err := bindWorkspaceGitEnvironment(t.Context(), sb, workspace, env, RunCredentials{GitHTTPHosts: map[string]string{"github.com": "github"}}); err != nil { t.Fatal(err) } stale := filepath.Join(workspace, ".agentflow", "runtime", "gh-config", "hosts.yml") @@ -137,7 +137,7 @@ func TestBindGitWorkspaceResetsCLIConfig(t *testing.T) { t.Fatal(err) } env = map[string]string{SessionGatewayEnv: "http://relay.invalid/mcp"} - if err := bindWorkspaceGitEnvironment(sb, workspace, env, RunCredentials{GitHTTPHosts: map[string]string{"github.com": "github"}}); err != nil { + if err := bindWorkspaceGitEnvironment(t.Context(), sb, workspace, env, RunCredentials{GitHTTPHosts: map[string]string{"github.com": "github"}}); err != nil { t.Fatal(err) } if _, err := os.Stat(stale); !os.IsNotExist(err) { diff --git a/internal/runtime/conversation_process.go b/internal/runtime/conversation_process.go index c85c02fdc..53d61ea0f 100644 --- a/internal/runtime/conversation_process.go +++ b/internal/runtime/conversation_process.go @@ -643,7 +643,7 @@ func (p *ConversationProcessPool) start(ctx context.Context, root, workspace str } } nativeArgv := append([]string(nil), command.Argv...) - command, err = bindCommand(bindSandbox, bindWorkspace, command, req.Command.Credentials) + command, err = bindCommand(procCtx, bindSandbox, bindWorkspace, command, req.Command.Credentials) if err != nil { return ConversationProcessResult{}, err } diff --git a/internal/runtime/docker_cases_test.go b/internal/runtime/docker_cases_test.go index fb2ad5643..f390d1a24 100644 --- a/internal/runtime/docker_cases_test.go +++ b/internal/runtime/docker_cases_test.go @@ -44,7 +44,7 @@ func TestDockerSandboxLifecycleWithFakeCLI(t *testing.T) { t.Fatalf("sandbox=%+v", sb) } const extraValue = "test-extra-credential" - command, err := bindCommand(sb, workspace, CommandSpec{ + command, err := bindCommand(t.Context(), sb, workspace, CommandSpec{ Argv: []string{"sh", "-lc", "echo ok"}, CredentialBindings: []CredentialBinding{BindExtra}, ExtraCredentialEnv: map[string]string{"DEPLOYMENT_VALUE": "registry"}, diff --git a/internal/runtime/managed_files_test.go b/internal/runtime/managed_files_test.go index 3a7a63ac0..4ad0ddd9d 100644 --- a/internal/runtime/managed_files_test.go +++ b/internal/runtime/managed_files_test.go @@ -169,7 +169,7 @@ func TestPrepareMCPCallRejectsSymlinks(t *testing.T) { if err := os.Symlink(victim, target); err != nil { t.Fatal(err) } - if _, err := PrepareMCPCall(nil, workspace, nil); err == nil { + if _, err := PrepareMCPCall(t.Context(), nil, workspace, nil); err == nil { t.Fatal("symlink accepted") } if victim != outside { diff --git a/internal/runtime/mcp_call.go b/internal/runtime/mcp_call.go index 788151704..80c86f8dd 100644 --- a/internal/runtime/mcp_call.go +++ b/internal/runtime/mcp_call.go @@ -57,7 +57,7 @@ func mcpCallGuestPath(rel string) string { // PrepareMCPCall writes the script-node helper (`agentflow mcp call`) and bound // MCP server list into the workspace. Tokens stay in a 0600 file; they are not // exported as environment variables. -func PrepareMCPCall(sandbox Sandbox, workspace string, servers []domain.MCPServerDefinition) (map[string]string, error) { +func PrepareMCPCall(ctx context.Context, sandbox Sandbox, workspace string, servers []domain.MCPServerDefinition) (map[string]string, error) { hostRoot := mcpCallHostDir(workspace) hostBin := filepath.Join(hostRoot, "bin") if _, err := EnsureControlDirectory(workspace, "runtime", "bin"); err != nil { @@ -80,7 +80,7 @@ func PrepareMCPCall(sandbox Sandbox, workspace string, servers []domain.MCPServe return nil, fmt.Errorf("write mcp call config: %w", err) } helperHost := filepath.Join(hostBin, "agentflow-helper") - if err := installMCPCallHelperForSandbox(sandbox, workspace, helperHost); err != nil { + if err := installMCPCallHelperForSandbox(ctx, sandbox, workspace, helperHost); err != nil { return nil, err } wrapperHost := filepath.Join(hostBin, "agentflow") @@ -168,25 +168,53 @@ func installMCPCallHelper(workspace, dest string) error { return installMCPCallHelperFrom(workspace, src, dest) } -func installMCPCallHelperForSandbox(sandbox Sandbox, workspace, dest string) error { +type dockerRunnerHelperWaitKey struct{} + +// WithDockerRunnerHelperWait lets a personal Runner wait for its background +// download when a command actually needs the Linux Git/MCP executable. +func WithDockerRunnerHelperWait(ctx context.Context, wait func(context.Context, string) error) context.Context { + return context.WithValue(ctx, dockerRunnerHelperWaitKey{}, wait) +} + +func installMCPCallHelperForSandbox(ctx context.Context, sandbox Sandbox, workspace, dest string) error { if sandboxDriver(sandbox) != "docker" || goruntime.GOOS == "linux" { return installMCPCallHelper(workspace, dest) } - self, err := os.Executable() + src, err := DockerRunnerHelperPath() if err != nil { return err } - pattern := strings.TrimSpace(os.Getenv("AGENTFLOW_RUNNER_BINARY")) - if !strings.Contains(pattern, "{os}") { - pattern = filepath.Join(filepath.Dir(self), "agentflow-runner-{os}-{arch}") + if wait, ok := ctx.Value(dockerRunnerHelperWaitKey{}).(func(context.Context, string) error); ok { + if err := wait(ctx, src); err != nil { + return err + } } - src := strings.NewReplacer("{os}", "linux", "{arch}", goruntime.GOARCH).Replace(pattern) if _, err := os.Stat(src); err != nil { return fmt.Errorf("Docker Git/MCP helper requires a Linux runner binary: set AGENTFLOW_RUNNER_BINARY with {os} and {arch}: %w", err) } return installMCPCallHelperFrom(workspace, src, dest) } +// DockerRunnerHelperPath is the Linux runner artifact used for Git and MCP +// inside Docker on a non-Linux runner. Keep installation and lookup in sync. +func DockerRunnerHelperPath() (string, error) { + self, err := os.Executable() + if err != nil { + return "", err + } + pattern := strings.TrimSpace(os.Getenv("AGENTFLOW_RUNNER_BINARY")) + if !strings.Contains(pattern, "{os}") { + pattern = filepath.Join(filepath.Dir(self), "agentflow-runner-{os}-{arch}") + } + path := strings.NewReplacer("{os}", "linux", "{arch}", goruntime.GOARCH).Replace(pattern) + if helper, err := os.Stat(path); err == nil { + if host, err := os.Stat(self); err == nil && os.SameFile(helper, host) { + return "", fmt.Errorf("Docker Git/MCP helper path aliases the host Runner: %s", path) + } + } + return path, nil +} + func installMCPCallHelperFrom(workspace, src, dest string) error { srcInfo, err := os.Stat(src) if err != nil { @@ -245,8 +273,8 @@ func mcpCallAliases(server domain.MCPServerDefinition) []string { return out } -func bindMCPCallEnvironment(sandbox Sandbox, workspace string, environment map[string]string, servers []domain.MCPServerDefinition) error { - prepared, err := PrepareMCPCall(sandbox, workspace, servers) +func bindMCPCallEnvironment(ctx context.Context, sandbox Sandbox, workspace string, environment map[string]string, servers []domain.MCPServerDefinition) error { + prepared, err := PrepareMCPCall(ctx, sandbox, workspace, servers) if err != nil { return err } @@ -335,9 +363,9 @@ func wrapIsolatedMCPCallExec(argv []string) []string { // ScriptMCPEnvironment materializes agentflow mcp call and returns AGENTFLOW_BIN // plus the config path. It does not rewrite PATH for local/unisolated processes. -func ScriptMCPEnvironment(sandbox Sandbox, workspace string, servers []domain.MCPServerDefinition) (map[string]string, error) { +func ScriptMCPEnvironment(ctx context.Context, sandbox Sandbox, workspace string, servers []domain.MCPServerDefinition) (map[string]string, error) { env := map[string]string{} - if err := bindMCPCallEnvironment(sandbox, workspace, env, servers); err != nil { + if err := bindMCPCallEnvironment(ctx, sandbox, workspace, env, servers); err != nil { return nil, err } return env, nil diff --git a/internal/runtime/mcp_call_test.go b/internal/runtime/mcp_call_test.go index 9676c9e6f..42e9b7818 100644 --- a/internal/runtime/mcp_call_test.go +++ b/internal/runtime/mcp_call_test.go @@ -75,7 +75,7 @@ func TestMCPCallCLIInvokesBoundHTTPServer(t *testing.T) { ID: "mcp-2214-project-dns", Name: "Config Center MCP", MarketID: "2214", Transport: "http", URL: gateway.URL, Headers: map[string]string{"Authorization": "Bearer mcp-token"}, }}} - env, err := ScriptMCPEnvironment(sb, workspace, assets.MCPServers) + env, err := ScriptMCPEnvironment(t.Context(), sb, workspace, assets.MCPServers) if err != nil { t.Fatal(err) } @@ -100,7 +100,7 @@ func TestMCPCallRequiresPersonalCredentialAtCallTime(t *testing.T) { if err != nil { t.Fatal(err) } - env, err := ScriptMCPEnvironment(sb, workspace, assets.MCPServers) + env, err := ScriptMCPEnvironment(t.Context(), sb, workspace, assets.MCPServers) if err != nil { t.Fatal(err) } @@ -117,7 +117,7 @@ func TestBindMCPWritesWorkspaceBinWithoutPATH(t *testing.T) { workspace := t.TempDir() sb := &localSandbox{workspace: workspace, environment: map[string]string{"PATH": "/usr/bin"}} command := CommandSpec{Argv: []string{"sh", "-lc", "agentflow mcp list"}, CredentialBindings: []CredentialBinding{BindMCP}} - bound, err := bindCommand(sb, workspace, command, RunCredentials{MCPServers: []domain.MCPServerDefinition{{ + bound, err := bindCommand(t.Context(), sb, workspace, command, RunCredentials{MCPServers: []domain.MCPServerDefinition{{ ID: "tapd", Name: "TAPD", Transport: "http", URL: "https://example.com/tapd", }}}) if err != nil { @@ -152,7 +152,7 @@ func TestDockerMCPCallPointsAtWorkspaceBin(t *testing.T) { provideDockerHelperFixture(t) workspace := t.TempDir() sb := &localSandbox{workspace: workspace, driver: "docker"} - env, err := ScriptMCPEnvironment(sb, workspace, []domain.MCPServerDefinition{{ + env, err := ScriptMCPEnvironment(t.Context(), sb, workspace, []domain.MCPServerDefinition{{ ID: "tapd", Name: "TAPD", Transport: "http", URL: "https://example.com/tapd", }}) if err != nil { @@ -198,10 +198,45 @@ func provideDockerHelperFixture(t *testing.T) { t.Setenv("AGENTFLOW_RUNNER_BINARY", pattern) } +func TestDockerHelperBindingWaitsForTheRunnerArtifact(t *testing.T) { + if goruntime.GOOS == "linux" { + t.Skip("a Linux Runner uses its own executable") + } + for _, use := range []string{"git", "mcp"} { + t.Run(use, func(t *testing.T) { + root := t.TempDir() + t.Setenv("AGENTFLOW_RUNNER_BINARY", filepath.Join(root, "runner-{os}-{arch}")) + helper := filepath.Join(root, "runner-linux-"+goruntime.GOARCH) + workspace := t.TempDir() + sb := &localSandbox{workspace: workspace, driver: "docker"} + const payload = "#!/bin/sh\nexit 0\n" + ctx := WithDockerRunnerHelperWait(t.Context(), func(ctx context.Context, path string) error { + if path != helper { + t.Fatalf("waited for %q, want %q", path, helper) + } + return os.WriteFile(path, []byte(payload), 0o755) + }) + var err error + if use == "git" { + env := map[string]string{SessionGatewayEnv: "http://fixture.invalid"} + err = bindWorkspaceGitEnvironment(ctx, sb, workspace, env, RunCredentials{GitHTTPHosts: map[string]string{"git.fixture.invalid": "other"}}) + } else { + _, err = PrepareMCPCall(ctx, sb, workspace, nil) + } + if err != nil { + t.Fatal(err) + } + if got, err := os.ReadFile(filepath.Join(mcpCallHostDir(workspace), "bin", "agentflow-helper")); err != nil || string(got) != payload { + t.Fatalf("workspace helper = %q, %v", got, err) + } + }) + } +} + func TestProcessSandboxMCPCallUsesAbsoluteManagedLauncher(t *testing.T) { workspace := t.TempDir() sb := &localSandbox{workspace: workspace, driver: "bwrap"} - env, err := PrepareMCPCall(sb, workspace, []domain.MCPServerDefinition{{ + env, err := PrepareMCPCall(t.Context(), sb, workspace, []domain.MCPServerDefinition{{ ID: "replace-batch", Name: "Replace Batch", Transport: "stdio", Command: "python3", Args: []string{"server.py"}, FileRefs: []domain.MCPFileRef{{Path: "server.py"}}, }}) if err != nil { @@ -263,7 +298,7 @@ func TestHookScriptCanCallMCP(t *testing.T) { } sb := &localSandbox{workspace: workspace} command := CommandSpec{Argv: []string{"sh", "-lc", compiled[0].Command}, CredentialBindings: []CredentialBinding{BindMCP}} - bound, err := bindCommand(sb, workspace, command, RunCredentials{MCPServers: []domain.MCPServerDefinition{{ + bound, err := bindCommand(t.Context(), sb, workspace, command, RunCredentials{MCPServers: []domain.MCPServerDefinition{{ ID: "tapd", Name: "TAPD", MarketID: "70", Transport: "http", URL: "https://example.com/tapd", }}}) if err != nil { diff --git a/internal/runtime/mcp_stdio_test.go b/internal/runtime/mcp_stdio_test.go index b6db2c8ab..cefef34cc 100644 --- a/internal/runtime/mcp_stdio_test.go +++ b/internal/runtime/mcp_stdio_test.go @@ -20,7 +20,7 @@ read initialized read request printf '%s\n' '{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"stdio-ok"}]}}'` servers := []domain.MCPServerDefinition{{ID: "local", Name: "Local", Transport: "stdio", Command: "sh", Args: []string{"-c", serverScript}, TimeoutSec: 5}} - env, err := ScriptMCPEnvironment(sb, workspace, servers) + env, err := ScriptMCPEnvironment(t.Context(), sb, workspace, servers) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/provider_credentials_test.go b/internal/runtime/provider_credentials_test.go index 08de2ce6a..25896c649 100644 --- a/internal/runtime/provider_credentials_test.go +++ b/internal/runtime/provider_credentials_test.go @@ -25,7 +25,7 @@ func TestProviderCredentialBindingIsolation(t *testing.T) { if err != nil { t.Fatal(err) } - bound, err := bindCommand(sb, workspace, command, creds) + bound, err := bindCommand(t.Context(), sb, workspace, command, creds) if err != nil { t.Fatal(err) } @@ -66,13 +66,13 @@ func TestProviderCredentialBindingIsolation(t *testing.T) { t.Fatalf("subprocess environment failed: %v", err) } // Without a responsible user's key, no host credential may be reused. - _, err = bindCommand(sb, workspace, command, RunCredentials{}) + _, err = bindCommand(t.Context(), sb, workspace, command, RunCredentials{}) if err == nil || !strings.Contains(err.Error(), "未配置") { t.Fatal("missing user key must fail before any CLI can reuse host login") } }) } - plain, err := bindCommand(&localSandbox{workspace: t.TempDir()}, t.TempDir(), CommandSpec{Argv: []string{"true"}}, creds) + plain, err := bindCommand(t.Context(), &localSandbox{workspace: t.TempDir()}, t.TempDir(), CommandSpec{Argv: []string{"true"}}, creds) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/provider_gateway_integration_test.go b/internal/runtime/provider_gateway_integration_test.go index 22f579090..6930a5432 100644 --- a/internal/runtime/provider_gateway_integration_test.go +++ b/internal/runtime/provider_gateway_integration_test.go @@ -107,7 +107,7 @@ func TestNativeProviderCustomGateway(t *testing.T) { } creds := RunCredentials{Codex: ProviderCredential{APIKey: "local-gateway-test-key", BaseURL: server.URL + "/v1"}, ClaudeCode: ProviderCredential{APIKey: "local-gateway-test-key", BaseURL: server.URL}} sb := &localSandbox{workspace: workspace} - bound, err := bindCommand(sb, workspace, command, creds) + bound, err := bindCommand(t.Context(), sb, workspace, command, creds) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/run_credentials_extra_test.go b/internal/runtime/run_credentials_extra_test.go index 8c065ed68..31d8f0102 100644 --- a/internal/runtime/run_credentials_extra_test.go +++ b/internal/runtime/run_credentials_extra_test.go @@ -50,7 +50,7 @@ func TestBindExtraCredentialEnv(t *testing.T) { } credentials := RunCredentials{Extra: map[string]string{"gateway": "tok-1"}} - bound, err := bindCommand(sb, sb.workspace, command, credentials) + bound, err := bindCommand(t.Context(), sb, sb.workspace, command, credentials) if err != nil { t.Fatalf("bindCommand: %v", err) } @@ -66,7 +66,7 @@ func TestBindExtraCredentialEnv(t *testing.T) { // Without the binding the mapping is inert. plain := command plain.CredentialBindings = nil - bound, err = bindCommand(sb, sb.workspace, plain, credentials) + bound, err = bindCommand(t.Context(), sb, sb.workspace, plain, credentials) if err != nil { t.Fatalf("bindCommand without binding: %v", err) } @@ -110,7 +110,7 @@ func TestBindExtraMarksCredentialEnvKeysForRedaction(t *testing.T) { CredentialBindings: []CredentialBinding{BindExtra}, ExtraCredentialEnv: map[string]string{"AWS_SECRET_ACCESS_KEY": "gateway"}, } - bound, err := bindCommand(sb, sb.workspace, command, RunCredentials{Extra: map[string]string{"gateway": "tok-1"}}) + bound, err := bindCommand(t.Context(), sb, sb.workspace, command, RunCredentials{Extra: map[string]string{"gateway": "tok-1"}}) if err != nil { t.Fatalf("bindCommand: %v", err) } @@ -152,7 +152,7 @@ func TestConversationPathBindsExtraCredentialEnv(t *testing.T) { CredentialBindings: wire.Bindings, ExtraCredentialEnv: wire.ExtraCredentialEnv, } - bound, err := bindCommand(sb, sb.workspace, rebuilt, wire.Credentials) + bound, err := bindCommand(t.Context(), sb, sb.workspace, rebuilt, wire.Credentials) if err != nil { t.Fatalf("bindCommand: %v", err) } diff --git a/internal/runtime/run_git_credentials.go b/internal/runtime/run_git_credentials.go index 8de21e476..305afa1a0 100644 --- a/internal/runtime/run_git_credentials.go +++ b/internal/runtime/run_git_credentials.go @@ -89,7 +89,7 @@ func RunGitEnvironment(ctx context.Context, sandbox Sandbox, workspace string, c for _, key := range workspaceGitSecretEnvironment { env[key] = "" } - if err := bindWorkspaceGitEnvironment(sandbox, workspace, env, credentials); err != nil { + if err := bindWorkspaceGitEnvironment(ctx, sandbox, workspace, env, credentials); err != nil { closeRelay() return nil, func() {}, err } diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go index c07265979..fa7fe334d 100644 --- a/internal/runtime/runtime_test.go +++ b/internal/runtime/runtime_test.go @@ -819,7 +819,7 @@ func TestLightweightSandboxSmoke(t *testing.T) { t.Fatal(createErr) } allowed := `test "$HOME" != "` + stateHome + `" && test -w "$HOME" && f=$(mktemp) && printf ok > "$f" && printf ok > /dev/null && printf ok > workspace-write.txt` - command, err := bindCommand(sb, workspace, CommandSpec{Argv: []string{fakeCodeBuddy, "-lc", allowed}, Provider: "codebuddy", PrivateHome: true}, RunCredentials{}) + command, err := bindCommand(t.Context(), sb, workspace, CommandSpec{Argv: []string{fakeCodeBuddy, "-lc", allowed}, Provider: "codebuddy", PrivateHome: true}, RunCredentials{}) if err != nil { t.Fatal(err) } diff --git a/internal/runtime/sandbox_local.go b/internal/runtime/sandbox_local.go index aad2bd386..87106831b 100644 --- a/internal/runtime/sandbox_local.go +++ b/internal/runtime/sandbox_local.go @@ -106,7 +106,7 @@ func ExecuteLocalCommand(ctx context.Context, workspace string, command CommandS if cleanup != nil { defer cleanup() } - bound, err := bindCommand(sandbox, workspace, command, credentials) + bound, err := bindCommand(ctx, sandbox, workspace, command, credentials) if err != nil { return "", err } diff --git a/internal/runtime/sandbox_manager.go b/internal/runtime/sandbox_manager.go index b2991d016..65fe2d101 100644 --- a/internal/runtime/sandbox_manager.go +++ b/internal/runtime/sandbox_manager.go @@ -439,7 +439,7 @@ func (s *managedSandbox) Exec(ctx context.Context, command CommandSpec, sink Eve // Binding writes the credential helpers into the workspace; parallel // branches then run their commands concurrently. s.execMu.Lock() - bound, err := bindCommand(s.Sandbox, s.workspace, command, credentials) + bound, err := bindCommand(ctx, s.Sandbox, s.workspace, command, credentials) s.execMu.Unlock() if err != nil { return "", err diff --git a/internal/runtime/workspace_terminal_sandbox.go b/internal/runtime/workspace_terminal_sandbox.go index 1f123fb4d..3c687f821 100644 --- a/internal/runtime/workspace_terminal_sandbox.go +++ b/internal/runtime/workspace_terminal_sandbox.go @@ -117,7 +117,7 @@ func sandboxTerminalCommandBound(ctx context.Context, sandbox Sandbox, command C return nil, err } credentials.GitHTTPHosts = runGitHosts(credentials) - command, err = bindCommand(sandbox, sandbox.Workspace(), prepared, credentials) + command, err = bindCommand(ctx, sandbox, sandbox.Workspace(), prepared, credentials) if err != nil { closeGit() return nil, err