Files
notescraft/scripts/pi-wave.sh
T
Marcos Paulo 200987e019 scripts: drop message_update events from agent logs
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 19:08:31 -03:00

41 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run one pi agent per task file in a wave directory, each in its own git worktree.
# usage: scripts/pi-wave.sh scripts/waves/wave1 [parallel=4]
set -euo pipefail
root=$(git rev-parse --show-toplevel)
dir=$(cd "$1" && pwd)
par=${2:-4}
mkdir -p "$root/.worktrees"
run() {
task=$(basename "$1" .md)
wt="$root/.worktrees/$task"
[ -d "$wt" ] || git -C "$root" worktree add -q "$wt" -b "feat/$task"
(
cd "$wt"
# message_update events repeat the whole partial message (quadratic log size): drop them
PATH="$HOME/.cargo/bin:$PATH" CARGO_TARGET_DIR="$root/target/agent-$task" \
timeout 3000 pi -p --mode json --no-session --no-extensions --no-skills --no-themes -np --thinking low \
"$(cat "$root/scripts/waves/_common.md" "$1")" 2>&1 | grep --line-buffered -v '"type":"message_update"' >"$root/.worktrees/$task.jsonl"
)
rc=$?
# final assistant text -> <task>.log; empty means the agent never answered
python3 - "$root/.worktrees/$task.jsonl" >"$root/.worktrees/$task.log" <<'PY'
import json,sys
last=""
for l in open(sys.argv[1],errors="replace"):
try: e=json.loads(l)
except ValueError: continue
if e.get("type")=="message_end" and e["message"].get("role")=="assistant":
t="".join(c.get("text","") for c in e["message"]["content"] if c.get("type")=="text")
if t.strip(): last=t
print(last)
PY
[ -s "$root/.worktrees/$task.log" ] && [ "$(wc -c <"$root/.worktrees/$task.log")" -gt 2 ] || rc="$rc-EMPTY-LOG"
echo "$task exit $rc" >>"$root/.worktrees/summary.log"
}
export -f run
export root
ls "$dir"/*.md | xargs -P "$par" -I{} bash -c 'run {}'