#!/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 [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 [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 # 遍历所有 /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