-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.sh
More file actions
executable file
·305 lines (233 loc) · 7.91 KB
/
loop.sh
File metadata and controls
executable file
·305 lines (233 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
set -euo pipefail
# Activate mise if available to ensure tools are in PATH
export PATH="$HOME/.local/share/mise/shims:$HOME/.local/bin:$PATH"
if command -v mise &> /dev/null; then
eval "$(mise activate bash)"
fi
# Fallback: explicitly add Go binary path if not found (fixes CI issues)
if ! command -v go &> /dev/null; then
if command -v mise &> /dev/null; then
GO_ROOT=$(mise where go 2>/dev/null || true)
if [[ -n "$GO_ROOT" && -d "$GO_ROOT/bin" ]]; then
export PATH="$GO_ROOT/bin:$PATH"
fi
fi
# Fallback: check standard install location
if [[ -d "/usr/local/go/bin" ]]; then
export PATH="/usr/local/go/bin:$PATH"
fi
fi
# Check for GCC to enable/disable race detector
# Note: scripts/ci.sh handles this logic for CI runs.
if command -v gcc &> /dev/null; then
# Useful if we run go commands directly in this script later
export CGO_ENABLED=1
else
export CGO_ENABLED=0
fi
# -------------------------------------------------------------------
# GridPulse CSMS orchestration loop
# Orchestrates iterations using LOOP_PROMPT.md.
# Engineering rules live in AGENTS.md. This script enforces process only.
# -------------------------------------------------------------------
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <goal>"
exit 1
fi
GOAL="$*"
# Allow environment overrides for easier CI/debugging.
: "${MAX_ITERATIONS:=50}"
: "${TIMEOUT:=300}"
: "${LOG_DIR:=logs}"
: "${PROMPT_FILE:=LOOP_PROMPT.md}"
OPENCODE_BIN=$(command -v opencode || true)
: "${AGENT:=orchestrator}"
PROMPT_OVERRIDE_FILE="$LOG_DIR/LOOP_PROMPT.override.md"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
mkdir -p "$LOG_DIR"
# -------------------------------------------------------------------
# Pre-flight checks (fail fast)
# -------------------------------------------------------------------
required_files=(
"$PROMPT_FILE"
"MANIFEST.md"
"AGENTS.md"
"RUNBOOK.md"
"NEXT_STEPS.md"
)
for f in "${required_files[@]}"; do
[[ -f "$f" ]] || {
echo -e "${RED}Missing required file: $f${NC}"
exit 1
}
done
[[ -n "$OPENCODE_BIN" ]] || {
echo -e "${RED}opencode not found${NC}"
exit 1
}
command -v git >/dev/null 2>&1 || {
echo -e "${RED}git not found${NC}"
exit 1
}
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
echo -e "${RED}Not inside a git work tree${NC}"
exit 1
}
BASELINE_GIT_STATUS=$(git status --porcelain)
if [[ -n "$BASELINE_GIT_STATUS" ]]; then
echo -e "${YELLOW}Warning: repo has uncommitted changes before starting.${NC}"
echo -e "${YELLOW}Loop will require returning to this baseline after each iteration.${NC}"
fi
# -------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------
git_clean_required() {
local current
current=$(git status --porcelain)
if [[ "$current" != "$BASELINE_GIT_STATUS" ]]; then
echo -e "${YELLOW}Warning: repository state changed and was not restored/committed.${NC}"
echo -e "${YELLOW}Expected to match baseline git status after iteration.${NC}"
echo -e "${YELLOW}Current status:${NC}"
git status --short
return 1
fi
return 0
}
run_ci() {
local ci_log="$1"
local ci_latest="$LOG_DIR/ci_failures_latest.txt"
local status=0
echo -e "${BLUE}Running CI checks...${NC}" | tee -a "$ci_log"
echo "--- CI $(date -u +"%Y-%m-%dT%H:%M:%SZ") ---" >>"$ci_log"
if [[ ! -x "./scripts/ci.sh" ]]; then
echo "[ci] Error: ./scripts/ci.sh not found or not executable" | tee -a "$ci_log"
cp -f "$ci_log" "$ci_latest"
return 1
fi
# Delegate to canonical CI script
set +e
./scripts/ci.sh >>"$ci_log" 2>&1
local ci_rc=$?
set -e
if ((ci_rc != 0)); then
echo "[ci] CI script failed (rc=$ci_rc)" | tee -a "$ci_log"
cp -f "$ci_log" "$ci_latest"
return 1
fi
return 0
}
kill_child() {
local pid="${CURRENT_PID:-}"
[[ -z "$pid" ]] && return 0
if [[ "$pid" =~ ^[0-9]+$ ]]; then
# Try to kill the whole process group first (covers pipelines).
kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null || true
sleep 1
kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null || true
fi
}
trap kill_child EXIT INT TERM
echo -e "${BLUE}🔄 GridPulse loop started (max $MAX_ITERATIONS iterations)${NC}"
echo -e "${BLUE}📄 Prompt: $PROMPT_FILE${NC}"
echo -e "${BLUE}📂 Logs: $LOG_DIR/${NC}"
START=$(date +%s)
# -------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------
for ((i = 1; i <= MAX_ITERATIONS; i++)); do
ITER_START=$(date +%s)
TS=$(date +"%Y%m%d_%H%M%S")
LOG="$LOG_DIR/iter_${i}_${TS}.log"
CI_LOG="$LOG_DIR/ci_${i}_${TS}.log"
PROMPT_TO_USE="$PROMPT_FILE"
if [[ -f "$PROMPT_OVERRIDE_FILE" ]]; then
PROMPT_TO_USE="$PROMPT_OVERRIDE_FILE"
fi
# concise, deterministic instruction
OPENCODE_GOAL="Execute one iteration using $PROMPT_TO_USE to progress toward: $GOAL"
OPENCODE_CMD=(run --print-logs --agent "$AGENT" "$OPENCODE_GOAL" -f "$PROMPT_TO_USE")
echo -e "\n${YELLOW}--- Iteration $i ---${NC}" | tee "$LOG"
(
# Avoid brittle failures with `pipefail`: `grep -v` returns 1 when it filters everything.
# `sed` reliably returns 0 while still letting earlier pipeline failures propagate.
"$OPENCODE_BIN" "${OPENCODE_CMD[@]}" 2>&1 | tee -a "$LOG" | sed '/INFO/d'
) &
CURRENT_PID=$!
# inactivity watchdog
while kill -0 "$CURRENT_PID" 2>/dev/null; do
sleep 5
NOW=$(date +%s)
if [[ "$OSTYPE" == "darwin"* ]]; then
LAST=$(stat -f %m "$LOG" 2>/dev/null || echo "$NOW")
else
LAST=$(stat -c %Y "$LOG" 2>/dev/null || echo "$NOW")
fi
if ((NOW - LAST > TIMEOUT)); then
echo -e "${RED}Timeout. Killing iteration.${NC}" | tee -a "$LOG"
kill_child || true
exit 1
fi
done
set +e
wait "$CURRENT_PID"
ITER_RC=$?
set -e
CURRENT_PID=""
if ((ITER_RC != 0)); then
echo -e "${RED}Iteration process exited non-zero (rc=$ITER_RC). Continuing.${NC}" | tee -a "$LOG"
DUR=$(($(date +%s) - ITER_START))
echo -e "${YELLOW}Iteration $i ended early (${DUR}s)${NC}" | tee -a "$LOG"
continue
fi
# -----------------------------------------------------------------
# Post-iteration verification
# -----------------------------------------------------------------
if run_ci "$CI_LOG"; then
rm -f "$PROMPT_OVERRIDE_FILE" 2>/dev/null || true
else
echo -e "${RED}CI failed. See $LOG_DIR/ci_failures_latest.txt${NC}" | tee -a "$LOG" | tee -a "$CI_LOG"
cat >"$PROMPT_OVERRIDE_FILE" <<'EOF'
# LOOP PROMPT OVERRIDE — Fix CI failures
Your ONLY task this iteration is to fix the CI failures.
Read:
- logs/ci_failures_latest.txt
Then:
1) Fix the errors.
2) Re-run the CI commands from RUNBOOK.md (CI section).
3) Commit all changes.
4) Output `<promise>NEXT_TASK</promise>` when CI is green.
Do not work on NEXT_STEPS.md items until CI is green.
EOF
# Do not enforce clean git or completion signals while CI is failing.
DUR=$(($(date +%s) - ITER_START))
echo -e "${YELLOW}Iteration $i ended with CI failures (${DUR}s)${NC}" | tee -a "$LOG"
continue
fi
# Stop immediately when DONE is signaled, even if the repo is dirty.
if grep -q "<promise>DONE</promise>" "$LOG"; then
echo -e "${GREEN}✅ DONE signaled${NC}"
break
fi
# Non-fatal: warn if repo state drifts from the starting baseline.
git_clean_required || true
if grep -q "<promise>NEXT_TASK</promise>" "$LOG"; then
echo -e "${BLUE}➡ NEXT_TASK signaled${NC}"
else
echo -e "${YELLOW}No completion signal detected. Continuing.${NC}"
fi
DUR=$(($(date +%s) - ITER_START))
echo -e "${GREEN}Iteration $i complete (${DUR}s)${NC}"
# Push changes
echo -e "${BLUE}Pushing changes to git...${NC}"
git push
done
TOTAL=$(($(date +%s) - START))
echo -e "\n${BLUE}🏁 Finished in ${TOTAL}s${NC}"
if ((i > MAX_ITERATIONS)); then
echo -e "${YELLOW}Reached iteration limit${NC}"
fi