@soleur/opencode 3.409.1 → 3.410.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hooks/stop-hook.sh
CHANGED
|
@@ -37,6 +37,12 @@ NOW_EPOCH=$(date +%s)
|
|
|
37
37
|
for state_file in "${PROJECT_ROOT}/.claude"/ralph-loop.*.local.md; do
|
|
38
38
|
[[ -f "$state_file" ]] || continue
|
|
39
39
|
|
|
40
|
+
# The current session file is read for the one grant after this sweep.
|
|
41
|
+
# Dead-owner cleanup of other sessions stays.
|
|
42
|
+
if [[ "$(basename "$state_file")" == "$(basename "$RALPH_STATE_FILE")" ]]; then
|
|
43
|
+
continue
|
|
44
|
+
fi
|
|
45
|
+
|
|
40
46
|
# Extract owner PID from filename: ralph-loop.PID.local.md
|
|
41
47
|
OWNER_PID=$(basename "$state_file" | sed 's/^ralph-loop\.\([0-9]*\)\.local\.md$/\1/')
|
|
42
48
|
|
|
@@ -272,23 +278,114 @@ if [[ -z "$PROMPT_TEXT" ]]; then
|
|
|
272
278
|
exit 0
|
|
273
279
|
fi
|
|
274
280
|
|
|
281
|
+
# Read tally show before the awk rewrite. The grant line is still the value the
|
|
282
|
+
# parent wrote; this pass is what consumes it. Path resolution lives in
|
|
283
|
+
# pipeline-tally.sh — this hook reads `show` output, never the counter file.
|
|
284
|
+
TALLY_SCRIPT="$SCRIPT_DIR/../scripts/pipeline-tally.sh"
|
|
285
|
+
TALLY_OUT=""
|
|
286
|
+
TALLY_CAPPED=""
|
|
287
|
+
TALLY_CAP=""
|
|
288
|
+
downgrade=0
|
|
289
|
+
if [[ -f "$TALLY_SCRIPT" ]]; then
|
|
290
|
+
TALLY_OUT=$(bash "$TALLY_SCRIPT" show 2>/dev/null || true)
|
|
291
|
+
if [[ "$TALLY_OUT" == *"tally:"* && "$TALLY_OUT" != "UNKNOWN"* ]]; then
|
|
292
|
+
_TD="" _TC="" _TN=""
|
|
293
|
+
for _TD in $(printf '%s' "$TALLY_OUT" | grep -o 'capped:[a-z_]*=[0-9]*' | cut -d: -f2 | cut -d= -f1) \
|
|
294
|
+
$(printf '%s' "$TALLY_OUT" | grep -o 'cap:[a-z_]*=[0-9]*' | cut -d: -f2 | cut -d= -f1); do
|
|
295
|
+
[[ -n "$_TD" ]] || continue
|
|
296
|
+
if [[ "$TALLY_OUT" == *"capped:${_TD}="* ]]; then
|
|
297
|
+
TALLY_CAPPED="$_TD"
|
|
298
|
+
TALLY_CAP=$(printf '%s' "$TALLY_OUT" | grep -o "capped:${_TD}=[0-9]*" | cut -d= -f2 || true)
|
|
299
|
+
break
|
|
300
|
+
fi
|
|
301
|
+
# `|| true` on every extraction: a grep no-match under `set -e`/`pipefail`
|
|
302
|
+
# would exit the hook mid-loop — the opposite of this block's fail-open
|
|
303
|
+
# contract.
|
|
304
|
+
_TC=$(printf '%s' "$TALLY_OUT" | grep -o "cap:${_TD}=[0-9]*" | cut -d= -f2 || true)
|
|
305
|
+
_TN=$(printf '%s' "$TALLY_OUT" | grep -o "^tally:.*" | grep -o "${_TD}=[0-9]*" | cut -d= -f2 || true)
|
|
306
|
+
if [[ "$_TC" =~ ^[0-9]+$ ]] && [[ "$_TN" =~ ^[0-9]+$ ]] && (( 10#$_TC > 0 )) && (( 10#$_TN >= 10#$_TC )); then
|
|
307
|
+
TALLY_CAPPED="$_TD"; TALLY_CAP="$_TC"; break
|
|
308
|
+
fi
|
|
309
|
+
done
|
|
310
|
+
fi
|
|
311
|
+
fi
|
|
312
|
+
grant_exact=0
|
|
313
|
+
consumed_set=0
|
|
314
|
+
FRONT_NOW=$(awk '/^---$/{c++; if (c>=2) exit; next} c==1' "$RALPH_STATE_FILE" 2>/dev/null || true)
|
|
315
|
+
if grep -qx 'retry_budget_grant: 1' <<<"$FRONT_NOW"; then
|
|
316
|
+
grant_exact=1
|
|
317
|
+
fi
|
|
318
|
+
if grep -qx 'retry_budget_consumed: 1' <<<"$FRONT_NOW"; then
|
|
319
|
+
consumed_set=1
|
|
320
|
+
fi
|
|
321
|
+
# One consumed grant, and only while the tally is capped. A later 1 does not arm again.
|
|
322
|
+
if [[ -n "$TALLY_CAPPED" && "$grant_exact" -eq 1 && "$consumed_set" -eq 0 ]]; then
|
|
323
|
+
downgrade=1
|
|
324
|
+
fi
|
|
325
|
+
|
|
326
|
+
# TTL of the current file runs after the grant read. A file that is about to
|
|
327
|
+
# spend the grant stays. Any other stale current file is removed, which is the
|
|
328
|
+
# pre-grant behavior for a session that has no grant.
|
|
329
|
+
if [[ "$downgrade" -eq 0 && -f "$RALPH_STATE_FILE" ]]; then
|
|
330
|
+
STARTED_AT=$(printf '%s\n' "$FRONT_NOW" | grep '^started_at:' | sed 's/started_at: *//' | sed 's/^"\(.*\)"$/\1/' || true)
|
|
331
|
+
if [[ -n "$STARTED_AT" ]]; then
|
|
332
|
+
STARTED_EPOCH=$(date -d "$STARTED_AT" +%s 2>/dev/null || true)
|
|
333
|
+
if [[ -n "$STARTED_EPOCH" ]] && [[ $((NOW_EPOCH - STARTED_EPOCH)) -gt $((TTL_HOURS * 3600)) ]]; then
|
|
334
|
+
AGE_MINS=$(( (NOW_EPOCH - STARTED_EPOCH) / 60 ))
|
|
335
|
+
echo "Ralph loop: stale state file detected ($(basename "$RALPH_STATE_FILE"), started ${AGE_MINS}m ago, TTL=${TTL_HOURS}h). Auto-removing." >&2
|
|
336
|
+
rm -f "$RALPH_STATE_FILE"
|
|
337
|
+
exit 0
|
|
338
|
+
fi
|
|
339
|
+
fi
|
|
340
|
+
fi
|
|
341
|
+
|
|
275
342
|
# Update iteration and stuck_count in first frontmatter block only (awk scoped to c==1)
|
|
276
343
|
# Note: on legacy state files without stuck_count field, the awk match is a no-op for that line.
|
|
277
344
|
# The counter will not persist across iterations. Acceptable because legacy files are only
|
|
278
345
|
# created by pre-stuck-detection versions of setup-ralph-loop.sh; all new loops include the field.
|
|
346
|
+
# The same pass rewrites an exact unquoted retry_budget_grant: 1 to 0 and sets
|
|
347
|
+
# retry_budget_consumed: 1. It never clears the consumed line.
|
|
279
348
|
TEMP_FILE="${RALPH_STATE_FILE}.tmp.$$"
|
|
280
|
-
|
|
281
|
-
|
|
349
|
+
awk_rc=0
|
|
350
|
+
awk -v iter="$NEXT_ITERATION" -v sc="$STUCK_COUNT" -v lrh="$LAST_RESPONSE_HASH" -v rc="$REPEAT_COUNT" -v simc="$SIMILARITY_COUNT" -v lrw="$LAST_RESPONSE_WORDS" -v downgrade="$downgrade" '
|
|
351
|
+
BEGIN { seen_consumed = 0 }
|
|
352
|
+
/^---$/ {
|
|
353
|
+
c++
|
|
354
|
+
if (c == 2 && downgrade == 1 && seen_consumed == 0) {
|
|
355
|
+
print "retry_budget_consumed: 1"
|
|
356
|
+
}
|
|
357
|
+
print
|
|
358
|
+
next
|
|
359
|
+
}
|
|
282
360
|
c==1 && /^iteration:/ { print "iteration: " iter; next }
|
|
283
361
|
c==1 && /^stuck_count:/ { print "stuck_count: " sc; next }
|
|
284
362
|
c==1 && /^last_response_hash:/ { print "last_response_hash: " lrh; next }
|
|
285
363
|
c==1 && /^repeat_count:/ { print "repeat_count: " rc; next }
|
|
286
364
|
c==1 && /^similarity_count:/ { print "similarity_count: " simc; next }
|
|
287
365
|
c==1 && /^last_response_words:/ { print "last_response_words: " lrw; next }
|
|
366
|
+
c==1 && /^retry_budget_consumed: 1$/ { seen_consumed = 1; print; next }
|
|
367
|
+
c==1 && downgrade == 1 && /^retry_budget_grant: 1$/ { print "retry_budget_grant: 0"; next }
|
|
288
368
|
{ print }
|
|
289
|
-
' "$RALPH_STATE_FILE" > "$TEMP_FILE" 2>/dev/null ||
|
|
369
|
+
' "$RALPH_STATE_FILE" > "$TEMP_FILE" 2>/dev/null || awk_rc=$?
|
|
370
|
+
awk_rc="${awk_rc:-0}"
|
|
371
|
+
if [[ "$downgrade" -eq 1 ]]; then
|
|
372
|
+
front_closes=0
|
|
373
|
+
consumed_gate=1
|
|
374
|
+
if [[ -f "$TEMP_FILE" ]]; then
|
|
375
|
+
front_closes=$(grep -c '^---$' "$TEMP_FILE" || true)
|
|
376
|
+
consumed_gate=0
|
|
377
|
+
grep -qx 'retry_budget_consumed: 1' "$TEMP_FILE" || consumed_gate=$?
|
|
378
|
+
fi
|
|
379
|
+
if [[ "$awk_rc" -ne 0 || ! -s "$TEMP_FILE" || "$consumed_gate" -ne 0 || "${front_closes:-0}" -lt 2 ]]; then
|
|
380
|
+
if [[ "$consumed_gate" -ge 2 ]]; then
|
|
381
|
+
printf 'SOLEUR_TALLY_GRANT_READ grep-rc=%s\n' "$consumed_gate" >&2
|
|
382
|
+
fi
|
|
383
|
+
rm -f "$TEMP_FILE"
|
|
384
|
+
exit 0
|
|
385
|
+
fi
|
|
386
|
+
fi
|
|
290
387
|
if [[ -s "$TEMP_FILE" ]]; then
|
|
291
|
-
mv "$TEMP_FILE" "$RALPH_STATE_FILE"
|
|
388
|
+
mv "$TEMP_FILE" "$RALPH_STATE_FILE" || { rm -f "$TEMP_FILE"; exit 0; }
|
|
292
389
|
else
|
|
293
390
|
# awk produced empty output (file vanished) -- clean up and exit
|
|
294
391
|
rm -f "$TEMP_FILE"
|
|
@@ -307,50 +404,46 @@ fi
|
|
|
307
404
|
# `capped` latch OR any count >= its cap_<dim> (catches a latch that never got
|
|
308
405
|
# set: a skipped gate, a gate that returned UNKNOWN, a dim nobody gates) — the
|
|
309
406
|
# pipeline is budget-capped and this hook declines to keep the loop alive: it
|
|
310
|
-
# removes the fuel rather than denying a tool.
|
|
311
|
-
#
|
|
312
|
-
# directions: absent/corrupt/UNKNOWN
|
|
313
|
-
# blocks FOR the tally.
|
|
407
|
+
# removes the fuel rather than denying a tool. `show` already ran before the
|
|
408
|
+
# awk rewrite. Placed immediately before the block emit so every terminating
|
|
409
|
+
# path above still wins. Fail-open in BOTH directions: absent/corrupt/UNKNOWN
|
|
410
|
+
# output changes nothing — the hook never blocks FOR the tally.
|
|
314
411
|
#
|
|
315
|
-
#
|
|
316
|
-
#
|
|
317
|
-
#
|
|
318
|
-
|
|
319
|
-
if [[ -
|
|
320
|
-
|
|
321
|
-
TALLY_CAPPED
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
# `|| true` on every extraction: a grep no-match under `set -e`/`pipefail`
|
|
334
|
-
# would exit the hook mid-loop — the opposite of this block's fail-open
|
|
335
|
-
# contract.
|
|
336
|
-
_TC=$(printf '%s' "$TALLY_OUT" | grep -o "cap:${_TD}=[0-9]*" | cut -d= -f2 || true)
|
|
337
|
-
_TN=$(printf '%s' "$TALLY_OUT" | grep -o "^tally:.*" | grep -o "${_TD}=[0-9]*" | cut -d= -f2 || true)
|
|
338
|
-
if [[ "$_TC" =~ ^[0-9]+$ ]] && [[ "$_TN" =~ ^[0-9]+$ ]] && (( 10#$_TC > 0 )) && (( 10#$_TN >= 10#$_TC )); then
|
|
339
|
-
TALLY_CAPPED="$_TD"; TALLY_CAP="$_TC"; break
|
|
340
|
-
fi
|
|
341
|
-
done
|
|
342
|
-
if [[ -n "$TALLY_CAPPED" ]]; then
|
|
343
|
-
_TN=$(printf '%s' "$TALLY_OUT" | grep -o "^tally:.*" | grep -o "${TALLY_CAPPED}=[0-9]*" | cut -d= -f2 || true)
|
|
344
|
-
printf 'SOLEUR_TALLY_CAPPED dim=%s cap=%s\n' "$TALLY_CAPPED" "${TALLY_CAP:-0}" >&2
|
|
345
|
-
# `--reset` deliberately NOT suggested: bare `init --max-<dim> N` takes
|
|
346
|
-
# `capped-reset`, which unlatches but PRESERVES the tally the feature
|
|
347
|
-
# exists to keep.
|
|
348
|
-
printf 'Ralph loop: pipeline tally is budget-capped; resume via pipeline-tally.sh init --max-%s N (raised caps).\n' "$(printf '%s' "$TALLY_CAPPED" | tr '_' '-')" >&2
|
|
349
|
-
# Classified-stop artifact: the state file is the ralph loop's own
|
|
350
|
-
# record — a resume that only reads stderr would never see the stop.
|
|
351
|
-
printf 'budget-capped: %s=%s/%s\n' "$TALLY_CAPPED" "${_TN:-0}" "${TALLY_CAP:-0}" >> "$RALPH_STATE_FILE" 2>/dev/null || true
|
|
412
|
+
# One recorded grant may fall through to the keep-alive block below. That
|
|
413
|
+
# happens only when the read-back after the move is the exact line
|
|
414
|
+
# retry_budget_grant: 0. Any other read-back exits 0 with no block. The hook
|
|
415
|
+
# never clears capped=.
|
|
416
|
+
if [[ -n "$TALLY_CAPPED" ]]; then
|
|
417
|
+
_TN=$(printf '%s' "$TALLY_OUT" | grep -o "^tally:.*" | grep -o "${TALLY_CAPPED}=[0-9]*" | cut -d= -f2 || true)
|
|
418
|
+
printf 'SOLEUR_TALLY_CAPPED dim=%s cap=%s\n' "$TALLY_CAPPED" "${TALLY_CAP:-0}" >&2
|
|
419
|
+
# Classified-stop artifact: the state file is the ralph loop's own
|
|
420
|
+
# record — a resume that only reads stderr would never see the stop.
|
|
421
|
+
printf 'budget-capped: %s=%s/%s\n' "$TALLY_CAPPED" "${_TN:-0}" "${TALLY_CAP:-0}" >> "$RALPH_STATE_FILE" 2>/dev/null || true
|
|
422
|
+
if [[ "$downgrade" -eq 1 ]]; then
|
|
423
|
+
readback=$(awk '/^---$/{c++; if (c>=2) exit; next} c==1' "$RALPH_STATE_FILE" 2>/dev/null || true)
|
|
424
|
+
grant_rc=0
|
|
425
|
+
grep -qx 'retry_budget_grant: 0' <<<"$readback" || grant_rc=$?
|
|
426
|
+
if [[ "$grant_rc" -ge 2 ]]; then
|
|
427
|
+
printf 'SOLEUR_TALLY_GRANT_READ grep-rc=%s\n' "$grant_rc" >&2
|
|
428
|
+
fi
|
|
429
|
+
if [[ "$grant_rc" -ne 0 ]]; then
|
|
352
430
|
exit 0
|
|
353
431
|
fi
|
|
432
|
+
consumed_rc=0
|
|
433
|
+
grep -qx 'retry_budget_consumed: 1' <<<"$readback" || consumed_rc=$?
|
|
434
|
+
if [[ "$consumed_rc" -ge 2 ]]; then
|
|
435
|
+
printf 'SOLEUR_TALLY_GRANT_READ grep-rc=%s\n' "$consumed_rc" >&2
|
|
436
|
+
fi
|
|
437
|
+
if [[ "$consumed_rc" -ne 0 ]]; then
|
|
438
|
+
exit 0
|
|
439
|
+
fi
|
|
440
|
+
else
|
|
441
|
+
# `--reset` deliberately NOT suggested: bare `init --max-<dim> N` takes
|
|
442
|
+
# `capped-reset`, which unlatches but PRESERVES the tally the feature
|
|
443
|
+
# exists to keep. The resume line is the real halt. A grant path falls
|
|
444
|
+
# through to the keep-alive block and does not print it.
|
|
445
|
+
printf 'Ralph loop: pipeline tally is budget-capped; resume via pipeline-tally.sh init --max-%s N (raised caps).\n' "$(printf '%s' "$TALLY_CAPPED" | tr '_' '-')" >&2
|
|
446
|
+
exit 0
|
|
354
447
|
fi
|
|
355
448
|
fi
|
|
356
449
|
|