-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.sh
More file actions
executable file
Β·346 lines (275 loc) Β· 9.31 KB
/
loop.sh
File metadata and controls
executable file
Β·346 lines (275 loc) Β· 9.31 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
set -euo pipefail
# -------------------------------------------------------------------
# GridHammer 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:=100}"
: "${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"
"PROJECT_PROMPT.md"
"PROJECT_SPEC.md"
"MANIFEST.md"
"ARCHITECTURE.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"
# ----------------------------------------------------------------
# Rust Checks
# ----------------------------------------------------------------
if command -v cargo >/dev/null 2>&1; then
local cargo_tomls
cargo_tomls=$(find . -name Cargo.toml \
-not -path "*/.git/*" \
-not -path "*/.opencode/*" \
-not -path "*/target/*" \
-not -path "*/node_modules/*" \
-print 2>/dev/null | sort)
if [[ -n "$cargo_tomls" ]]; then
while IFS= read -r cargo_file; do
[[ -z "$cargo_file" ]] && continue
local crate_dir
crate_dir=$(dirname "$cargo_file")
echo "[ci] Rust Crate: $crate_dir" | tee -a "$ci_log"
set +e
(
cd "$crate_dir" || exit 2
cargo fmt -- --check && cargo clippy -- -D warnings && cargo test
) >>"$ci_log" 2>&1
local rust_rc=$?
set -e
if ((rust_rc != 0)); then
echo "[ci] Rust checks failed for $crate_dir (rc=$rust_rc)" | tee -a "$ci_log"
status=1
fi
done <<<"$cargo_tomls"
else
echo "[ci] No Rust crates found (skipping)" | tee -a "$ci_log"
fi
else
echo "[ci] cargo not found (skipping Rust checks)" | tee -a "$ci_log"
fi
# ----------------------------------------------------------------
# TypeScript/Node Checks
# ----------------------------------------------------------------
if command -v npm >/dev/null 2>&1; then
local package_jsons
package_jsons=$(find . -name package.json \
-not -path "*/.git/*" \
-not -path "*/.opencode/*" \
-not -path "*/target/*" \
-not -path "*/node_modules/*" \
-print 2>/dev/null | sort)
if [[ -n "$package_jsons" ]]; then
while IFS= read -r pkg_file; do
[[ -z "$pkg_file" ]] && continue
local pkg_dir
pkg_dir=$(dirname "$pkg_file")
echo "[ci] Node Project: $pkg_dir" | tee -a "$ci_log"
set +e
(
cd "$pkg_dir" || exit 2
# Only run if scripts exist
if grep -q '"test":' package.json; then
npm test
fi
if grep -q '"lint":' package.json; then
npm run lint
fi
) >>"$ci_log" 2>&1
local node_rc=$?
set -e
if ((node_rc != 0)); then
echo "[ci] Node checks failed for $pkg_dir (rc=$node_rc)" | tee -a "$ci_log"
status=1
fi
done <<<"$package_jsons"
else
echo "[ci] No Node projects found (skipping)" | tee -a "$ci_log"
fi
else
echo "[ci] npm not found (skipping Node checks)" | tee -a "$ci_log"
fi
if ((status != 0)); then
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