#!/usr/bin/env bash # Start a standard Herdr agent with non-interactive tool approvals. set -euo pipefail usage() { cat <<'EOF' 用法: scripts/herdr-agent-start.sh [--dry-run] profile: claude | codex | cursor | antigravity | pi | kimi | cline 标准名称见 .doc/ops/2026-09-13-herdr-agent-roster.md。 EOF } dry_run=0 if [[ "${1:-}" == "--dry-run" ]]; then dry_run=1 shift fi if [[ $# -ne 3 ]]; then usage >&2 exit 2 fi profile="$1" agent_name="$2" pane_id="$3" shift 3 case "${profile}" in claude) kind="claude" expected_name="reserve_claude" native_args=(--dangerously-skip-permissions) ;; codex) kind="codex" expected_name="orchestrator_codex" native_args=(--ask-for-approval never --sandbox workspace-write) ;; cursor) kind="cursor" expected_name="implementer_cursor" native_args=(--force --sandbox enabled --approve-mcps --trust) ;; antigravity) kind="agy" expected_name="verifier_antigravity" native_args=(--dangerously-skip-permissions --sandbox) ;; pi) kind="pi" expected_name="context_pi" native_args=(--approve) ;; kimi) kind="kimi" expected_name="context_kimi" native_args=(--auto) ;; cline) kind="cline" expected_name="implementer_cline" native_args=(--tui --auto-approve true) ;; *) printf '[错误] 未知 profile: %s\n' "${profile}" >&2 usage >&2 exit 2 ;; esac if [[ "${agent_name}" != "${expected_name}" ]]; then printf '[错误] profile %s 的标准名称必须是 %s\n' "${profile}" "${expected_name}" >&2 exit 2 fi if [[ ! "${pane_id}" =~ ^w[[:alnum:]]+:p[[:alnum:]]+$ ]]; then printf '[错误] 非法 pane id: %s\n' "${pane_id}" >&2 exit 2 fi command=(herdr agent start "${agent_name}" --kind "${kind}" --pane "${pane_id}" -- "${native_args[@]}") if [[ ${dry_run} -eq 1 ]]; then printf '%q ' "${command[@]}" printf '\n' exit 0 fi if [[ "${HERDR_ENV:-}" != "1" ]]; then printf '[错误] 当前进程不在 Herdr 管理的 pane 中。\n' >&2 exit 1 fi repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)" if [[ -z "${repo_root}" || ! -f "${repo_root}/AGENTS.md" || ! -f "${repo_root}/PROJECT.md" ]]; then printf '[错误] 必须从 Agent-System 任务 worktree 启动。\n' >&2 exit 1 fi branch="$(git branch --show-current)" if [[ "${branch}" != feature/* && "${branch}" != fix/* ]]; then printf '[错误] 自动审批实例只能在 feature/* 或 fix/* 分支启动,当前为 %s。\n' "${branch}" >&2 exit 1 fi command -v jq >/dev/null 2>&1 || { printf '[错误] 缺少 jq。\n' >&2; exit 1; } workspace_id="${pane_id%%:*}" pane_json="$(herdr pane list --workspace "${workspace_id}")" target_cwd="$(jq -r --arg pane "${pane_id}" '.result.panes[] | select(.pane_id == $pane) | .cwd' <<<"${pane_json}" | head -n 1)" if [[ -z "${target_cwd}" || "$(realpath -m "${target_cwd}")" != "$(realpath -m "${repo_root}")" ]]; then printf '[错误] 目标 pane 不存在或不在当前任务 worktree: %s\n' "${pane_id}" >&2 exit 1 fi exec "${command[@]}"