merge sandbox/dd24a29: chg_0003 dispatch_helpers — A1 prep + A2 setup + A3 wait scripts (3 host tools, 8 cases, gate PASS)

- scripts/prep-sandbox-skills.sh: A1 host-side skill 拷贝 (跳过 symlink/template-boot-jnpf/JNPF_*)
- scripts/setup-xstudio-ai.sh: A2 host-side ai-config writer (python3 merge 不覆盖已有 providers)
- scripts/wait-for-task.sh: A3 host-side completion poller (轮询 /tmp/xstudio-sandbox/*/space/.xstudio-report.md)
- tests/e2e/dispatch_helpers.test.sh: 6 case 自检
- openspec/changes/chg_0003_dispatch_helpers/{5 工件}

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
xstudio agentandClaude Opus 4.8 committed 2026-09-28 11:18:36 +08:00
1 parent 3f5f32fd99
commit a94f40e42e
9 files changed
+917

No files matched your search

+146
View File
@@ -0,0 +1,146 @@
#!/usr/bin/env bash
# prep-sandbox-skills.sh — A1: copy host skills into sandbox space before dispatch.
#
# Usage:
# bash scripts/prep-sandbox-skills.sh <wsHash> [--src <skillSrcDir>]
#
# Why: xstudio 内核 ccWorkDir 挂载时 .xstudio/ 被排除(容器不挂)。
# sandbox agent 启动后需要 skill 资产 → 必须 pre-dispatch 把 host 的
# ~/.claude/skills/ 拷到 sandbox space 的 .claude/skills/。
#
# 跳过:
# - symlink(在容器里 host 路径解析失败)
# - ssw-jnpf-lite-react/template-boot-jnpf(>50MB 模板)
# - ssw-jnpf-lite-react/JNPF_*(>10KB 大型资源)
#
# Exit codes:
# 0 成功(断言 .claude/skills/ai-collab-framework/ 存在)
# 1 入参 / 空间 / source 路径错误
# 2 复制后断言失败(sandbox 端 skill 资产缺失)
set -euo pipefail
# ---- args ----
usage() {
cat <<'EOF'
prep-sandbox-skills.sh — A1 host-side skill prep
Usage:
bash scripts/prep-sandbox-skills.sh <wsHash> [--src <skillSrcDir>]
Arguments:
wsHash -- sandbox hash; /tmp/xstudio-sandbox/<wsHash>/space/ must exist
--src <dir> -- source skills dir (default: $HOME/.claude/skills/)
Example:
bash scripts/prep-sandbox-skills.sh $(ls /tmp/xstudio-sandbox | head -1)
EOF
}
if [[ $# -lt 1 || "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
WS_HASH="$1"
shift
SKILL_SRC="$HOME/.claude/skills/"
while [[ $# -gt 0 ]]; do
case "$1" in
--src)
SKILL_SRC="${2:-}"
shift 2
;;
--src=*)
SKILL_SRC="${1#--src=}"
shift
;;
*)
echo "ERROR: unknown arg: $1" >&2
usage >&2
exit 1
;;
esac
done
# ---- validation ----
if [[ -z "$WS_HASH" ]]; then
echo "ERROR: wsHash is required" >&2
usage >&2
exit 1
fi
SPACE_DIR="/tmp/xstudio-sandbox/${WS_HASH}/space"
if [[ ! -d "$SPACE_DIR" ]]; then
echo "ERROR: sandbox space $SPACE_DIR 不存在;先派一个任务让内核播种" >&2
exit 1
fi
if [[ ! -d "$SKILL_SRC" ]]; then
echo "ERROR: skill source $SKILL_SRC 不存在" >&2
exit 1
fi
# ---- copy ----
DEST="$SPACE_DIR/.claude/skills"
mkdir -p "$DEST"
# Clear stale copies so re-runs don't accumulate (idempotent)
rm -rf "$DEST"/*
copied=()
skipped=()
for skill_dir in "$SKILL_SRC"/*/; do
[[ -d "$skill_dir" ]] || continue
name=$(basename "$skill_dir")
# 跳过 symlink(容器里 host 绝对路径解析失败)
if [[ -L "$skill_dir" ]]; then
skipped+=("$name (symlink)")
continue
fi
# 跳过 ssw-jnpf-lite-react 内的超大资源
case "$name" in
template-boot-jnpf|JNPF_*)
skipped+=("$name (large)")
continue
;;
esac
cp -R "$skill_dir" "$DEST/$name"
copied+=("$name")
done
# 也跳过 ssw-jnpf-lite-react 内的特定子目录
if [[ -d "$DEST/ssw-jnpf-lite-react" ]]; then
for sub in template-boot-jnpf; do
if [[ -e "$DEST/ssw-jnpf-lite-react/$sub" ]]; then
rm -rf "$DEST/ssw-jnpf-lite-react/$sub"
skipped+=("ssw-jnpf-lite-react/$sub (large)")
fi
done
# JNPF_* glob(空匹配走字面量 shopt 关闭 nullglob 后会留 *)
shopt -s nullglob
for sub in "$DEST/ssw-jnpf-lite-react"/JNPF_*; do
if [[ -e "$sub" ]]; then
local_name="${sub##*/}"
rm -rf "$sub"
skipped+=("ssw-jnpf-lite-react/$local_name (large)")
fi
done
shopt -u nullglob
fi
echo "copied: ${copied[*]:-none}"
echo "skipped: ${skipped[*]:-none}"
echo "skills present:"
ls -1 "$DEST" | sed 's/^/ - /'
# ---- assertion ----
if [[ ! -d "$DEST/ai-collab-framework" ]]; then
echo "ERROR: ai-collab-framework not present after copy" >&2
exit 2
fi
exit 0
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
# setup-xstudio-ai.sh — A2: write/merge .xstudio/ai-config.json with claude-process provider.
#
# Usage:
# bash scripts/setup-xstudio-ai.sh <workspaceRoot>
#
# Why: ai-config.json 缺省 {} 导致首次派单 401/403。本脚本把 host 端
# ANTHROPIC_BASE_URL / ANTHROPIC_MODEL / ANTHROPIC_AUTH_TOKEN 写到
# <workspaceRoot>/.xstudio/ai-config.json 的 claude-process provider 段。
#
# 幂等:先用 python3 读已有 providers,merge(已有 provider 不覆盖,只填空
# claude-process 缺失字段),再写回。
#
# 兜底:
# - ANTHROPIC_BASE_URL 缺省 https://lm.mayishangshu.cn:88/minimax/anthropic
# - ANTHROPIC_MODEL 缺省 MiniMax-M3
# - ANTHROPIC_AUTH_TOKEN 先 env,后 ~/ssw.ini 的 [llm-api] key= 行
#
# Exit codes:
# 0 写入成功(断言 .providers.claude-process.baseURL 非空)
# 1 入参 / workspaceRoot 不存在
# 2 token 缺失(env + ~/ssw.ini 都没有)
# 3 写后断言失败
set -euo pipefail
# ---- args ----
usage() {
cat <<'EOF'
setup-xstudio-ai.sh — A2 host-side ai-config writer
Usage:
bash scripts/setup-xstudio-ai.sh <workspaceRoot>
Arguments:
workspaceRoot -- workspace root path (e.g. /Users/xs/Desktop/workspace/dth)
Reads env:
ANTHROPIC_BASE_URL (default: https://lm.mayishangshu.cn:88/minimax/anthropic)
ANTHROPIC_MODEL (default: MiniMax-M3)
ANTHROPIC_AUTH_TOKEN (env OR ~/ssw.ini [llm-api] key=)
EOF
}
if [[ $# -lt 1 || "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
WORKSPACE_ROOT="${1:-}"
if [[ -z "$WORKSPACE_ROOT" ]]; then
echo "ERROR: workspaceRoot is required" >&2
usage >&2
exit 1
fi
if [[ ! -d "$WORKSPACE_ROOT" ]]; then
echo "ERROR: workspaceRoot $WORKSPACE_ROOT 不存在" >&2
exit 1
fi
CONFIG_DIR="$WORKSPACE_ROOT/.xstudio"
CONFIG="$CONFIG_DIR/ai-config.json"
mkdir -p "$CONFIG_DIR"
# ---- read config sources ----
BASE_URL="${ANTHROPIC_BASE_URL:-https://lm.mayishangshu.cn:88/minimax/anthropic}"
MODEL="${ANTHROPIC_MODEL:-MiniMax-M3}"
TOKEN="${ANTHROPIC_AUTH_TOKEN:-}"
if [[ -z "$TOKEN" && -f "$HOME/ssw.ini" ]]; then
# 兼容 [llm-api] 与 [llm—api](破折号)两种 segment 名
TOKEN=$(awk -F'key=' '/^\[llm[-—]api\]/{f=1} f && /key=/{print $2; exit}' "$HOME/ssw.ini" | tr -d ' \r\n')
fi
if [[ -z "$TOKEN" ]]; then
echo "ERROR: ANTHROPIC_AUTH_TOKEN 未设且 ~/ssw.ini 无 [llm-api].key=" >&2
exit 2
fi
# token 仅作 authTokenEnv 引用,env 优先;无需把 secret 写进 config 文件
# (claude-process provider 按 env 名 lookup,避免明文落盘)
# ---- merge + write via python3 ----
# 读已有 config(如有)→ merge → 写回。已有 provider 段保留不覆盖。
python3 - "$CONFIG" "$BASE_URL" "$MODEL" <<'PYEOF'
import json, sys, os
config_path = sys.argv[1]
base_url = sys.argv[2]
model = sys.argv[3]
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
try:
cfg = json.load(f)
except json.JSONDecodeError:
cfg = {}
else:
cfg = {}
cfg.setdefault("providers", {})
providers = cfg["providers"]
# 已有 claude-process 段:保留所有现有字段,只填空缺失字段(merge)
existing = providers.get("claude-process", {}) if isinstance(providers, dict) else {}
claude_process = dict(existing) # 拷贝已有字段
claude_process.setdefault("baseURL", base_url)
claude_process.setdefault("authTokenEnv", "ANTHROPIC_AUTH_TOKEN")
claude_process.setdefault("model", model)
# ccContainer 仅在缺失时填空(避免覆盖用户显式设的 false)
claude_process.setdefault("ccContainer", True)
providers["claude-process"] = claude_process
with open(config_path, "w", encoding="utf-8") as f:
json.dump(cfg, f, indent=2, ensure_ascii=False)
f.write("\n")
# 断言
assert isinstance(providers.get("claude-process"), dict), "claude-process not a dict"
assert providers["claude-process"].get("baseURL"), "baseURL empty after write"
assert providers["claude-process"].get("model"), "model empty after write"
assert providers["claude-process"].get("authTokenEnv") == "ANTHROPIC_AUTH_TOKEN", "authTokenEnv mismatch"
print(f"ai-config written: {config_path}")
print(f" baseURL = {providers['claude-process']['baseURL']}")
print(f" model = {providers['claude-process']['model']}")
print(f" authTokenEnv = {providers['claude-process']['authTokenEnv']}")
print(f" ccContainer = {providers['claude-process']['ccContainer']}")
PYEOF
# 二次断言(python3 抛 AssertionError 已 set -e 捕获)
echo "ok: $CONFIG"
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env bash
# wait-for-task.sh — A3: poll sandbox space for .xstudio-report.md (task completion signal).
#
# Usage:
# bash scripts/wait-for-task.sh <taskId> [timeoutSec] [--poll-interval N]
#
# Why: 任务完成信号原本只走 macOS 通知,host AI agent 容易漏接。本脚本
# 轮询 /tmp/xstudio-sandbox/*/space/.xstudio-report.md,文件出现即视为
# 任务完成 + 报告就位,cat 出来给 host 看。
#
# 轮询所有 sandbox hash(不只当前 taskId 对应的 hash —— taskId 与 hash
# 不一定一一对应,多任务并发时任何一份报告都算信号)。
#
# Exit codes:
# 0 报告出现并打印
# 1 timeout 内报告未出现
# 2 入参错误 / sandbox 根目录不存在
set -euo pipefail
# ---- args ----
usage() {
cat <<'EOF'
wait-for-task.sh — A3 host-side task completion poller
Usage:
bash scripts/wait-for-task.sh <taskId> [timeoutSec] [--poll-interval N]
Arguments:
taskId -- AI task id (currently used only for logging)
timeoutSec -- total timeout seconds (default: 3000)
--poll-interval N -- poll period in seconds (default: 5)
Examples:
bash scripts/wait-for-task.sh ai-1790562657-0EF743
bash scripts/wait-for-task.sh my-task 60 --poll-interval 2
EOF
}
if [[ $# -lt 1 || "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
TASK_ID="$1"
shift
POLL_INTERVAL=5
TIMEOUT=3000
while [[ $# -gt 0 ]]; do
case "$1" in
--poll-interval)
POLL_INTERVAL="${2:-5}"
shift 2
;;
--poll-interval=*)
POLL_INTERVAL="${1#--poll-interval=}"
shift
;;
*)
# 兼容位置参数 timeoutSec
if [[ "$TIMEOUT" == "3000" && "$1" =~ ^[0-9]+$ ]]; then
TIMEOUT="$1"
shift
else
echo "ERROR: unknown arg: $1" >&2
usage >&2
exit 2
fi
;;
esac
done
SANDBOX_ROOT="/tmp/xstudio-sandbox"
if [[ ! -d "$SANDBOX_ROOT" ]]; then
echo "ERROR: $SANDBOX_ROOT 不存在;内核未播种 sandbox 空间" >&2
exit 2
fi
if [[ -z "$TASK_ID" ]]; then
echo "ERROR: taskId required" >&2
usage >&2
exit 2
fi
# ---- poll loop ----
START=$(date +%s)
echo "waiting for .xstudio-report.md (taskId=$TASK_ID, timeout=${TIMEOUT}s, poll=${POLL_INTERVAL}s)..." >&2
while true; do
# 遍历所有 <hash>/space/.xstudio-report.md
for hash_dir in "$SANDBOX_ROOT"/*/; do
[[ -d "$hash_dir" ]] || continue
report="$hash_dir/space/.xstudio-report.md"
if [[ -f "$report" ]]; then
echo "task complete, report: $report" >&2
cat "$report"
exit 0
fi
done
ELAPSED=$(( $(date +%s) - START ))
if [[ $ELAPSED -ge $TIMEOUT ]]; then
echo "TIMEOUT after ${TIMEOUT}s; check mcp__xstudio__xstudio_ai_status manually" >&2
exit 1
fi
sleep "$POLL_INTERVAL"
done