-
-
Notifications
You must be signed in to change notification settings - Fork 237
376 lines (331 loc) · 14.6 KB
/
Copy pathchain-runner.yml
File metadata and controls
376 lines (331 loc) · 14.6 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
name: Aeon · Chain Runner
run-name: "chain: ${{ inputs.chain }}"
on:
workflow_dispatch:
inputs:
chain:
description: 'Chain name from aeon.yml chains section'
required: true
type: string
permissions:
contents: write
actions: write
issues: write # §3 issues-as-state: the chain's append (state_store.sh ensure/comment)
# calls the Issues API; without this it 403s and only the file path records
# chain events (and in `issues` mode they'd be lost from the ledger entirely).
concurrency:
group: aeon-chain-${{ inputs.chain }}
cancel-in-progress: false
jobs:
run:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- name: Checkout repo
uses: actions/checkout@v7
with:
token: ${{ secrets.GH_GLOBAL || secrets.GITHUB_TOKEN }}
- name: Configure git identity
run: |
git config user.name "aeonframework"
git config user.email "aeonframework@proton.me"
- name: Run chain
env:
GH_TOKEN: ${{ secrets.GH_GLOBAL || secrets.GITHUB_TOKEN }}
_INPUT_CHAIN: ${{ inputs.chain }}
run: |
set -euo pipefail
# env-bound (passed via env:, not expanded into this run: block) +
# allowlisted: a chain name is an
# aeon.yml chains: key, so it must match ^[a-zA-Z0-9_-]+$. Interpolating
# it into the shell source let a crafted value break out and run
# arbitrary commands on the runner. (GHSA-h9v2-7m42-33m3)
CHAIN="$_INPUT_CHAIN"
if ! printf '%s' "$CHAIN" | grep -qE '^[a-zA-Z0-9_-]+$'; then
echo "::error::Invalid chain name: must match ^[a-zA-Z0-9_-]+$"
exit 1
fi
NOW_ISO=$(date -u +%FT%TZ)
echo "Running chain: $CHAIN"
# --- Helper: dispatch a skill and return its run ID ---
dispatch_skill() {
local skill="$1" var="${2:-}" ctx_file="${3:-}"
local before_ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)
local args=(-f skill="$skill")
[ -n "$var" ] && args+=(-f var="$var")
[ -n "$ctx_file" ] && args+=(-f chain_context_file="$ctx_file")
echo " Dispatching: $skill"
gh workflow run aeon.yml "${args[@]}"
# Poll for the run to appear (up to 60s)
local run_id=""
for i in $(seq 1 12); do
sleep 5
run_id=$(gh run list --workflow=aeon.yml -L 10 \
--json databaseId,displayTitle,createdAt \
-q "[.[] | select(.displayTitle | test(\"skill: ${skill}( |\$)\"; \"i\")) | select(.createdAt >= \"${before_ts}\")] | .[0].databaseId" \
2>/dev/null || true)
[ -n "$run_id" ] && [ "$run_id" != "null" ] && break
run_id=""
done
if [ -z "$run_id" ]; then
echo "::error::Failed to discover run ID for skill: $skill"
return 1
fi
echo " Run ID: $run_id"
echo "$run_id"
}
# --- Helper: wait for a set of run IDs to complete ---
wait_for_runs() {
local timeout=1800 # 30 min
local start=$(date +%s)
local run_ids=("$@")
echo " Waiting for ${#run_ids[@]} run(s)..."
while true; do
local all_done=true
for id in "${run_ids[@]}"; do
local status=$(gh run view "$id" --json status -q '.status' 2>/dev/null || echo "unknown")
if [ "$status" != "completed" ]; then
all_done=false
break
fi
done
$all_done && break
local elapsed=$(( $(date +%s) - start ))
if [ "$elapsed" -ge "$timeout" ]; then
echo "::error::Chain timed out waiting for runs: ${run_ids[*]}"
return 1
fi
sleep 30
done
# Check conclusions
local failed=()
for id in "${run_ids[@]}"; do
local conclusion=$(gh run view "$id" --json conclusion -q '.conclusion' 2>/dev/null || echo "failure")
if [ "$conclusion" != "success" ]; then
local name=$(gh run view "$id" --json displayTitle -q '.displayTitle' 2>/dev/null || echo "$id")
failed+=("$name")
echo "::warning::Run failed: $name (ID: $id, conclusion: $conclusion)"
fi
done
if [ ${#failed[@]} -gt 0 ]; then
return 1
fi
return 0
}
# --- Helper: build chain context file from consumed outputs ---
build_context() {
local target_skill="$1"
shift
local consume_skills=("$@")
local ctx_file="output/.chains/.chain-context-${target_skill}.md"
mkdir -p output/.chains
echo "" > "$ctx_file"
for dep in "${consume_skills[@]}"; do
local output_file="output/.chains/${dep}.md"
if [ -f "$output_file" ] && [ -s "$output_file" ]; then
echo "### ${dep}" >> "$ctx_file"
echo "" >> "$ctx_file"
cat "$output_file" >> "$ctx_file"
echo "" >> "$ctx_file"
echo "---" >> "$ctx_file"
echo "" >> "$ctx_file"
else
echo "### ${dep}" >> "$ctx_file"
echo "" >> "$ctx_file"
echo "_Output not available._" >> "$ctx_file"
echo "" >> "$ctx_file"
fi
done
echo "$ctx_file"
}
# --- Parse chain definition from aeon.yml ---
# Extract the chain block using awk
CHAIN_BLOCK=$(awk -v chain="$CHAIN" '
/^chains:/ { in_chains=1; next }
in_chains && /^[^ ]/ { in_chains=0 }
in_chains && $0 ~ "^ " chain ":" { in_chain=1; next }
in_chain && /^ [^ ]/ && !($0 ~ "^ ") { in_chain=0 }
in_chain { print }
' aeon.yml)
if [ -z "$CHAIN_BLOCK" ]; then
echo "::error::Chain '$CHAIN' not found in aeon.yml"
exit 1
fi
# Extract on_error mode (default: fail-fast)
ON_ERROR=$(echo "$CHAIN_BLOCK" | grep -oP 'on_error:\s*\K[a-z-]+' || echo "fail-fast")
echo "Error mode: $ON_ERROR"
# Parse steps into an ordered array
# Each step is either:
# - parallel: [a, b, c]
# - skill: name [, var: "val"] [, consume: [a, b]]
STEP_INDEX=0
declare -A STEP_TYPE STEP_SKILLS STEP_VAR STEP_CONSUME
while IFS= read -r line; do
# Skip empty lines and non-step lines
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
[[ "$line" =~ ^[[:space:]]*# ]] && continue
if [[ "$line" =~ parallel:\ *\[([^\]]+)\] ]]; then
STEP_TYPE[$STEP_INDEX]="parallel"
STEP_SKILLS[$STEP_INDEX]=$(echo "${BASH_REMATCH[1]}" | tr -d ' ')
STEP_INDEX=$((STEP_INDEX + 1))
elif [[ "$line" =~ skill:\ *([a-zA-Z0-9_-]+) ]]; then
STEP_TYPE[$STEP_INDEX]="single"
STEP_SKILLS[$STEP_INDEX]="${BASH_REMATCH[1]}"
# Extract optional var
if [[ "$line" =~ var:\ *\"([^\"]+)\" ]]; then
STEP_VAR[$STEP_INDEX]="${BASH_REMATCH[1]}"
fi
# Extract optional consume
if [[ "$line" =~ consume:\ *\[([^\]]+)\] ]]; then
STEP_CONSUME[$STEP_INDEX]=$(echo "${BASH_REMATCH[1]}" | tr -d ' ')
fi
STEP_INDEX=$((STEP_INDEX + 1))
fi
done <<< "$CHAIN_BLOCK"
TOTAL_STEPS=$STEP_INDEX
echo "Chain has $TOTAL_STEPS step(s)"
CHAIN_FAILED=false
# --- Execute steps sequentially ---
for ((i=0; i<TOTAL_STEPS; i++)); do
TYPE="${STEP_TYPE[$i]}"
SKILLS="${STEP_SKILLS[$i]}"
VAR="${STEP_VAR[$i]:-}"
CONSUME="${STEP_CONSUME[$i]:-}"
echo ""
echo "=== Step $((i+1))/$TOTAL_STEPS: $TYPE [$SKILLS] ==="
# Check if chain should abort
if [ "$CHAIN_FAILED" = "true" ] && [ "$ON_ERROR" = "fail-fast" ]; then
echo "Skipping step (chain failed, on_error=fail-fast)"
continue
fi
# Build list of skills to dispatch
IFS=',' read -ra SKILL_LIST <<< "$SKILLS"
RUN_IDS=()
for skill in "${SKILL_LIST[@]}"; do
# Build context file if this step consumes outputs
CTX_FILE=""
if [ -n "$CONSUME" ]; then
IFS=',' read -ra CONSUME_LIST <<< "$CONSUME"
CTX_FILE=$(build_context "$skill" "${CONSUME_LIST[@]}")
# Commit and push context file so the skill run can access it
git add output/.chains/ 2>/dev/null || true
if ! git diff --staged --quiet 2>/dev/null; then
git commit -m "chore(chain): context for $skill"
for attempt in 1 2 3; do
git pull --rebase origin main 2>/dev/null || true
git push -u origin HEAD 2>/dev/null && break
sleep "$attempt"
done
fi
fi
RUN_ID=$(dispatch_skill "$skill" "$VAR" "$CTX_FILE")
DISPATCH_EXIT=$?
if [ $DISPATCH_EXIT -ne 0 ] || [ -z "$RUN_ID" ]; then
echo "::error::Failed to dispatch skill: $skill"
CHAIN_FAILED=true
[ "$ON_ERROR" = "fail-fast" ] && break
continue
fi
# run_id is the last line of dispatch_skill output
RUN_ID=$(echo "$RUN_ID" | tail -1)
RUN_IDS+=("$RUN_ID")
sleep 2 # Stagger dispatches
done
# Wait for all runs in this step to complete
if [ ${#RUN_IDS[@]} -gt 0 ]; then
if ! wait_for_runs "${RUN_IDS[@]}"; then
echo "::warning::Step $((i+1)) had failures"
CHAIN_FAILED=true
if [ "$ON_ERROR" = "fail-fast" ]; then
echo "Aborting chain (on_error=fail-fast)"
continue
fi
fi
# Pull latest to get committed outputs from completed skills
git pull --rebase origin main 2>/dev/null || true
echo " Pulled latest outputs"
fi
done
# --- Final status ---
echo ""
if [ "$CHAIN_FAILED" = "true" ]; then
echo "::error::Chain '$CHAIN' completed with failures"
echo "CHAIN_STATUS=failed" >> "$GITHUB_ENV"
else
echo "Chain '$CHAIN' completed successfully"
echo "CHAIN_STATUS=success" >> "$GITHUB_ENV"
fi
- name: Update cron state
if: always()
env:
GH_TOKEN: ${{ secrets.GH_GLOBAL || secrets.GITHUB_TOKEN }}
STATE_BACKEND: ${{ vars.STATE_BACKEND }}
_INPUT_CHAIN: ${{ inputs.chain }}
run: |
# env-bound + allowlisted, same as the Run chain step. (GHSA-h9v2-7m42-33m3)
CHAIN="$_INPUT_CHAIN"
if ! printf '%s' "$CHAIN" | grep -qE '^[a-zA-Z0-9_-]+$'; then
echo "::error::Invalid chain name: must match ^[a-zA-Z0-9_-]+$"
exit 1
fi
NOW_ISO=$(date -u +%FT%TZ)
STATUS="${CHAIN_STATUS:-failed}"
STATE_FILE="memory/cron-state.json"
# Issues backend (hardening §3) — STATE_BACKEND tri-state, matching aeon.yml:
# file (DEFAULT) commits the file only, no Issue writes (best for
# fork-and-forget); dual also appends the chain event to the shared state
# Issue (opt-in transition); issues appends + skips the file/rebase path.
# Append failure falls through to the file either way.
BACKEND="${STATE_BACKEND:-file}"
if [ "$BACKEND" != "file" ]; then
EVENT=$(jq -cn --arg s "chain:$CHAIN" --arg st "$STATUS" --arg ts "$NOW_ISO" \
'{skill:$s,status:$st,ts:$ts}')
ISSUE=$(GH_REPO="$GITHUB_REPOSITORY" bash scripts/state_store.sh ensure "aeon:cron-state" 2>/dev/null || true)
if [ -n "$ISSUE" ] && GH_REPO="$GITHUB_REPOSITORY" bash scripts/state_store.sh append "$ISSUE" "$EVENT"; then
echo "chain state appended to issue #$ISSUE (backend=$BACKEND)"
if [ "$BACKEND" = "issues" ]; then echo "(issues mode: skipping file commit)"; exit 0; fi
else
echo "::warning::Issues-as-state append failed (backend=$BACKEND); file path records this run"
fi
fi
git checkout main 2>/dev/null || true
git pull --rebase origin main 2>/dev/null || true
if [ -f "$STATE_FILE" ] && jq empty "$STATE_FILE" 2>/dev/null; then
STATE=$(cat "$STATE_FILE")
else
STATE='{}'
fi
# Store chain state with "chain:" prefix to distinguish from skills
STATE=$(echo "$STATE" | jq --arg s "chain:$CHAIN" --arg st "$STATUS" --arg ts "$NOW_ISO" \
'.[$s].last_status = $st | .[$s]["last_" + $st] = $ts')
mkdir -p memory
echo "$STATE" | jq '.' > "$STATE_FILE"
git add "$STATE_FILE"
if git diff --staged --quiet; then
echo "No state changes"
exit 0
fi
git commit -m "chore(chain): $CHAIN $STATUS"
for i in 1 2 3 4 5; do
if git pull --rebase origin main 2>/dev/null && git push -u origin HEAD 2>/dev/null; then
echo "Chain state updated: $CHAIN=$STATUS"
exit 0
fi
git rebase --abort 2>/dev/null || true
git reset --soft HEAD~1 2>/dev/null || true
git checkout -- "$STATE_FILE" 2>/dev/null || true
git pull origin main 2>/dev/null || true
if [ -f "$STATE_FILE" ] && jq empty "$STATE_FILE" 2>/dev/null; then
STATE=$(cat "$STATE_FILE")
else
STATE='{}'
fi
STATE=$(echo "$STATE" | jq --arg s "chain:$CHAIN" --arg st "$STATUS" --arg ts "$NOW_ISO" \
'.[$s].last_status = $st | .[$s]["last_" + $st] = $ts')
echo "$STATE" | jq '.' > "$STATE_FILE"
git add "$STATE_FILE"
git diff --staged --quiet && { echo "State already up to date"; exit 0; }
git commit -m "chore(chain): $CHAIN $STATUS"
sleep "$i"
done
echo "::warning::Failed to commit chain state (non-fatal)"