prizmkit 1.1.83 → 1.1.84
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/bundled/VERSION.json +3 -3
- package/bundled/dev-pipeline/lib/common.sh +81 -18
- package/bundled/dev-pipeline/run-bugfix.sh +6 -0
- package/bundled/dev-pipeline/run-feature.sh +6 -0
- package/bundled/dev-pipeline/run-refactor.sh +6 -0
- package/bundled/skills/_metadata.json +1 -1
- package/package.json +1 -1
- package/bundled/dev-pipeline-windows/lib/heartbeat.sh +0 -471
package/bundled/VERSION.json
CHANGED
|
@@ -403,12 +403,29 @@ prizm_start_ai_session() {
|
|
|
403
403
|
|
|
404
404
|
unset CLAUDECODE 2>/dev/null || true
|
|
405
405
|
|
|
406
|
-
#
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
406
|
+
# Create backup outside project tree. Claude Code subagents can unlink
|
|
407
|
+
# session.log inside the project, but cannot reach $HOME/.prizmkit/.
|
|
408
|
+
# tee writes to the backup file while >> redirects to the main log.
|
|
409
|
+
# If the main log gets severed (unlinked+recreated by subagent),
|
|
410
|
+
# prizm_recover_session_log copies the backup back when the session ends.
|
|
411
|
+
local backup_dir="$HOME/.prizmkit/session-backups"
|
|
412
|
+
mkdir -p "$backup_dir"
|
|
413
|
+
local session_dir
|
|
414
|
+
session_dir="$(dirname "$(dirname "$log_path")")"
|
|
415
|
+
local session_id
|
|
416
|
+
session_id="$(basename "$session_dir")"
|
|
417
|
+
local backup_log="$backup_dir/${session_id}.log"
|
|
418
|
+
PRIZM_BACKUP_LOG="$backup_log"
|
|
419
|
+
|
|
420
|
+
# Write preamble to both files
|
|
421
|
+
{
|
|
422
|
+
echo "=== PRIZMKIT SESSION START ==="
|
|
423
|
+
echo "Prompt: $prompt_path"
|
|
424
|
+
echo "Model: ${model:-default}"
|
|
425
|
+
echo "Started at: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
426
|
+
echo "--- OUTPUT BELOW ---"
|
|
427
|
+
} > "$log_path"
|
|
428
|
+
cp "$log_path" "$backup_log"
|
|
412
429
|
|
|
413
430
|
local session_platform
|
|
414
431
|
session_platform="$(_prizm_session_platform)"
|
|
@@ -428,7 +445,7 @@ prizm_start_ai_session() {
|
|
|
428
445
|
if [[ -n "$model" ]]; then
|
|
429
446
|
claude_args+=(--model "$model")
|
|
430
447
|
fi
|
|
431
|
-
"$CLI_CMD" "${claude_args[@]}" >> "$log_path"
|
|
448
|
+
"$CLI_CMD" "${claude_args[@]}" 2>&1 | tee -a "$backup_log" >> "$log_path" &
|
|
432
449
|
;;
|
|
433
450
|
codex)
|
|
434
451
|
local codex_args=(--ask-for-approval never --sandbox danger-full-access)
|
|
@@ -447,7 +464,7 @@ prizm_start_ai_session() {
|
|
|
447
464
|
if [[ -n "${PRIZMKIT_EFFORT:-}" ]]; then
|
|
448
465
|
codex_args+=(--config "model_reasoning_effort=$PRIZMKIT_EFFORT")
|
|
449
466
|
fi
|
|
450
|
-
"$CLI_CMD" "${codex_args[@]}" - < "$prompt_path" >> "$log_path"
|
|
467
|
+
"$CLI_CMD" "${codex_args[@]}" - < "$prompt_path" 2>&1 | tee -a "$backup_log" >> "$log_path" &
|
|
451
468
|
;;
|
|
452
469
|
*)
|
|
453
470
|
local cb_args=(--print -y)
|
|
@@ -463,7 +480,7 @@ prizm_start_ai_session() {
|
|
|
463
480
|
if [[ -n "$model" ]]; then
|
|
464
481
|
cb_args+=(--model "$model")
|
|
465
482
|
fi
|
|
466
|
-
"$CLI_CMD" "${cb_args[@]}" < "$prompt_path" >> "$log_path"
|
|
483
|
+
"$CLI_CMD" "${cb_args[@]}" < "$prompt_path" 2>&1 | tee -a "$backup_log" >> "$log_path" &
|
|
467
484
|
;;
|
|
468
485
|
esac
|
|
469
486
|
PRIZM_AI_PID=$!
|
|
@@ -945,12 +962,25 @@ prizm_run_ai_session() {
|
|
|
945
962
|
|
|
946
963
|
unset CLAUDECODE 2>/dev/null || true
|
|
947
964
|
|
|
948
|
-
#
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
965
|
+
# Create backup outside project tree (same rationale as prizm_start_ai_session)
|
|
966
|
+
local backup_dir="$HOME/.prizmkit/session-backups"
|
|
967
|
+
mkdir -p "$backup_dir"
|
|
968
|
+
local session_dir
|
|
969
|
+
session_dir="$(dirname "$(dirname "$log_path")")"
|
|
970
|
+
local session_id
|
|
971
|
+
session_id="$(basename "$session_dir")"
|
|
972
|
+
local backup_log="$backup_dir/${session_id}.log"
|
|
973
|
+
PRIZM_BACKUP_LOG="$backup_log"
|
|
974
|
+
|
|
975
|
+
# Write preamble to both files
|
|
976
|
+
{
|
|
977
|
+
echo "=== PRIZMKIT SESSION START ==="
|
|
978
|
+
echo "Prompt: $prompt_path"
|
|
979
|
+
echo "Model: ${model:-default}"
|
|
980
|
+
echo "Started at: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
981
|
+
echo "--- OUTPUT BELOW ---"
|
|
982
|
+
} > "$log_path"
|
|
983
|
+
cp "$log_path" "$backup_log"
|
|
954
984
|
|
|
955
985
|
local session_platform
|
|
956
986
|
session_platform="$(_prizm_session_platform)"
|
|
@@ -964,7 +994,7 @@ prizm_run_ai_session() {
|
|
|
964
994
|
if [[ -n "$model" ]]; then
|
|
965
995
|
claude_args+=(--model "$model")
|
|
966
996
|
fi
|
|
967
|
-
"$CLI_CMD" "${claude_args[@]}" >> "$log_path"
|
|
997
|
+
"$CLI_CMD" "${claude_args[@]}" 2>&1 | tee -a "$backup_log" >> "$log_path"
|
|
968
998
|
;;
|
|
969
999
|
codex)
|
|
970
1000
|
local codex_args=(--ask-for-approval never --sandbox danger-full-access)
|
|
@@ -983,7 +1013,7 @@ prizm_run_ai_session() {
|
|
|
983
1013
|
if [[ -n "${PRIZMKIT_EFFORT:-}" ]]; then
|
|
984
1014
|
codex_args+=(--config "model_reasoning_effort=$PRIZMKIT_EFFORT")
|
|
985
1015
|
fi
|
|
986
|
-
"$CLI_CMD" "${codex_args[@]}" - < "$prompt_path" >> "$log_path"
|
|
1016
|
+
"$CLI_CMD" "${codex_args[@]}" - < "$prompt_path" 2>&1 | tee -a "$backup_log" >> "$log_path"
|
|
987
1017
|
;;
|
|
988
1018
|
*)
|
|
989
1019
|
local cb_args=(--print -y)
|
|
@@ -993,11 +1023,44 @@ prizm_run_ai_session() {
|
|
|
993
1023
|
if [[ -n "$model" ]]; then
|
|
994
1024
|
cb_args+=(--model "$model")
|
|
995
1025
|
fi
|
|
996
|
-
"$CLI_CMD" "${cb_args[@]}" < "$prompt_path" >> "$log_path"
|
|
1026
|
+
"$CLI_CMD" "${cb_args[@]}" < "$prompt_path" 2>&1 | tee -a "$backup_log" >> "$log_path"
|
|
997
1027
|
;;
|
|
998
1028
|
esac
|
|
999
1029
|
}
|
|
1000
1030
|
|
|
1031
|
+
# Recover session log from backup if the main log was truncated during execution.
|
|
1032
|
+
# Claude Code subagents can unlink session.log at the filesystem level. The backup
|
|
1033
|
+
# lives in $HOME/.prizmkit/ — outside the project tree — so it survives.
|
|
1034
|
+
#
|
|
1035
|
+
# Must be called AFTER the AI CLI process exits and BEFORE the session summary
|
|
1036
|
+
# reads session.log. Uses PRIZM_BACKUP_LOG set by prizm_start_ai_session /
|
|
1037
|
+
# prizm_run_ai_session.
|
|
1038
|
+
prizm_recover_session_log() {
|
|
1039
|
+
local session_log="${1:-}"
|
|
1040
|
+
local backup_log="${PRIZM_BACKUP_LOG:-}"
|
|
1041
|
+
|
|
1042
|
+
if [[ -z "$backup_log" || ! -f "$backup_log" ]]; then
|
|
1043
|
+
return 0
|
|
1044
|
+
fi
|
|
1045
|
+
|
|
1046
|
+
local main_size=0
|
|
1047
|
+
if [[ -f "$session_log" ]]; then
|
|
1048
|
+
main_size=$(wc -c < "$session_log" 2>/dev/null | tr -d ' ')
|
|
1049
|
+
fi
|
|
1050
|
+
|
|
1051
|
+
local backup_size=0
|
|
1052
|
+
backup_size=$(wc -c < "$backup_log" 2>/dev/null | tr -d ' ')
|
|
1053
|
+
|
|
1054
|
+
if [[ "$main_size" -lt 1024 && "$backup_size" -gt "$main_size" ]]; then
|
|
1055
|
+
cp "$backup_log" "$session_log"
|
|
1056
|
+
log_info "Session log recovered from backup (${backup_size}B → $session_log)"
|
|
1057
|
+
fi
|
|
1058
|
+
|
|
1059
|
+
# Clean up backup either way
|
|
1060
|
+
rm -f "$backup_log"
|
|
1061
|
+
return 0
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1001
1064
|
# prizm_detect_subagents <session_log>
|
|
1002
1065
|
#
|
|
1003
1066
|
# Scan session log for subagent spawns, count them, and log the result.
|
|
@@ -139,6 +139,9 @@ spawn_and_wait_session() {
|
|
|
139
139
|
stop_progress_parser "$parser_pid"
|
|
140
140
|
[[ -n "$watcher_pid" ]] && wait "$watcher_pid" 2>/dev/null || true
|
|
141
141
|
|
|
142
|
+
# Recover session log from backup if truncated by subagent stdout redirection
|
|
143
|
+
prizm_recover_session_log "$session_log"
|
|
144
|
+
|
|
142
145
|
[[ $exit_code -eq 143 ]] && exit_code=124
|
|
143
146
|
|
|
144
147
|
# Check for stale-kill marker (heartbeat killed the process due to no progress)
|
|
@@ -971,6 +974,9 @@ DEPLOY_PROMPT_EOF
|
|
|
971
974
|
prizm_run_ai_session "$deploy_prompt" "$deploy_session_dir/logs/session.log"
|
|
972
975
|
local deploy_exit=$?
|
|
973
976
|
|
|
977
|
+
# Recover log from backup if truncated
|
|
978
|
+
prizm_recover_session_log "$deploy_session_dir/logs/session.log"
|
|
979
|
+
|
|
974
980
|
if [[ $deploy_exit -eq 0 ]]; then
|
|
975
981
|
log_success "Deploy session completed (exit 0)"
|
|
976
982
|
else
|
|
@@ -150,6 +150,9 @@ spawn_and_wait_session() {
|
|
|
150
150
|
stop_progress_parser "$parser_pid"
|
|
151
151
|
[[ -n "$watcher_pid" ]] && wait "$watcher_pid" 2>/dev/null || true
|
|
152
152
|
|
|
153
|
+
# Recover session log from backup if truncated by subagent stdout redirection
|
|
154
|
+
prizm_recover_session_log "$session_log"
|
|
155
|
+
|
|
153
156
|
# Map SIGTERM (143) to timeout code 124
|
|
154
157
|
if [[ $exit_code -eq 143 ]]; then
|
|
155
158
|
exit_code=124
|
|
@@ -1264,6 +1267,9 @@ DEPLOY_PROMPT_EOF
|
|
|
1264
1267
|
prizm_run_ai_session "$deploy_prompt" "$deploy_session_dir/logs/session.log"
|
|
1265
1268
|
local deploy_exit=$?
|
|
1266
1269
|
|
|
1270
|
+
# Recover log from backup if truncated
|
|
1271
|
+
prizm_recover_session_log "$deploy_session_dir/logs/session.log"
|
|
1272
|
+
|
|
1267
1273
|
if [[ $deploy_exit -eq 0 ]]; then
|
|
1268
1274
|
log_success "Deploy session completed (exit 0)"
|
|
1269
1275
|
else
|
|
@@ -141,6 +141,9 @@ spawn_and_wait_session() {
|
|
|
141
141
|
stop_progress_parser "$parser_pid"
|
|
142
142
|
[[ -n "$watcher_pid" ]] && wait "$watcher_pid" 2>/dev/null || true
|
|
143
143
|
|
|
144
|
+
# Recover session log from backup if truncated by subagent stdout redirection
|
|
145
|
+
prizm_recover_session_log "$session_log"
|
|
146
|
+
|
|
144
147
|
[[ $exit_code -eq 143 ]] && exit_code=124
|
|
145
148
|
|
|
146
149
|
# Check for stale-kill marker (heartbeat killed the process due to no progress)
|
|
@@ -1006,6 +1009,9 @@ DEPLOY_PROMPT_EOF
|
|
|
1006
1009
|
prizm_run_ai_session "$deploy_prompt" "$deploy_session_dir/logs/session.log"
|
|
1007
1010
|
local deploy_exit=$?
|
|
1008
1011
|
|
|
1012
|
+
# Recover log from backup if truncated
|
|
1013
|
+
prizm_recover_session_log "$deploy_session_dir/logs/session.log"
|
|
1014
|
+
|
|
1009
1015
|
if [[ $deploy_exit -eq 0 ]]; then
|
|
1010
1016
|
log_success "Deploy session completed (exit 0)"
|
|
1011
1017
|
else
|
package/package.json
CHANGED
|
@@ -1,471 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# ============================================================
|
|
3
|
-
# dev-pipeline/lib/heartbeat.sh - Shared heartbeat monitoring
|
|
4
|
-
#
|
|
5
|
-
# Provides start_heartbeat / stop_heartbeat functions that read
|
|
6
|
-
# structured progress from progress.json (written by
|
|
7
|
-
# parse-stream-progress.py) and fall back to tail-based monitoring.
|
|
8
|
-
#
|
|
9
|
-
# When stale_kill_threshold is set (>0), the heartbeat monitor will
|
|
10
|
-
# automatically kill the AI CLI process if it shows no progress for
|
|
11
|
-
# the specified duration. This prevents sessions from hanging forever
|
|
12
|
-
# when the AI CLI process doesn't exit after completing its work.
|
|
13
|
-
#
|
|
14
|
-
# Usage:
|
|
15
|
-
# source "$SCRIPT_DIR/lib/heartbeat.sh"
|
|
16
|
-
# start_heartbeat "$cli_pid" "$session_log" "$progress_json" "$interval" ["$stale_kill_threshold"]
|
|
17
|
-
# # ... wait for CLI to finish ...
|
|
18
|
-
# stop_heartbeat "$_HEARTBEAT_PID"
|
|
19
|
-
#
|
|
20
|
-
# Requires: colors (GREEN, YELLOW, BLUE, NC) and log functions
|
|
21
|
-
# to be defined before sourcing.
|
|
22
|
-
# ============================================================
|
|
23
|
-
|
|
24
|
-
# Start a heartbeat monitor in the background.
|
|
25
|
-
# Sets _HEARTBEAT_PID to the background process PID.
|
|
26
|
-
#
|
|
27
|
-
# Arguments:
|
|
28
|
-
# $1 - cli_pid PID of the AI CLI process to monitor
|
|
29
|
-
# $2 - session_log Path to session.log
|
|
30
|
-
# $3 - progress_json Path to progress.json (may not exist if stream-json disabled)
|
|
31
|
-
# $4 - interval Heartbeat interval in seconds
|
|
32
|
-
# $5 - stale_kill_threshold (optional) Seconds of no progress before auto-killing the process.
|
|
33
|
-
# 0 = disabled (default). Recommended: 900 (15 minutes).
|
|
34
|
-
start_heartbeat() {
|
|
35
|
-
local cli_pid="$1"
|
|
36
|
-
local session_log="$2"
|
|
37
|
-
local progress_json="$3"
|
|
38
|
-
local heartbeat_interval="$4"
|
|
39
|
-
local stale_kill_threshold="${5:-0}"
|
|
40
|
-
|
|
41
|
-
(
|
|
42
|
-
local elapsed=0
|
|
43
|
-
local prev_size=0
|
|
44
|
-
local prev_max_size=0
|
|
45
|
-
local prev_child_activity_signature=""
|
|
46
|
-
local stale_seconds=0
|
|
47
|
-
while kill -0 "$cli_pid" 2>/dev/null; do
|
|
48
|
-
sleep "$heartbeat_interval"
|
|
49
|
-
elapsed=$((elapsed + heartbeat_interval))
|
|
50
|
-
kill -0 "$cli_pid" 2>/dev/null || break
|
|
51
|
-
|
|
52
|
-
# Get log file size. If session_log was truncated (e.g. by subagent
|
|
53
|
-
# stdout redirection severing the file descriptor), detect and flag it.
|
|
54
|
-
local cur_size=0
|
|
55
|
-
if [[ -f "$session_log" ]]; then
|
|
56
|
-
cur_size=$(wc -c < "$session_log" 2>/dev/null || echo 0)
|
|
57
|
-
cur_size=$(echo "$cur_size" | tr -d ' ')
|
|
58
|
-
fi
|
|
59
|
-
|
|
60
|
-
# Detect log truncation: size dropped to 0 or near-zero after previously
|
|
61
|
-
# accumulating significant content (>100KB). This indicates the file was
|
|
62
|
-
# unlinked/truncated by something outside our control while the AI CLI
|
|
63
|
-
# process is still running.
|
|
64
|
-
local log_truncated=false
|
|
65
|
-
if [[ $cur_size -lt 1024 && $prev_max_size -gt 102400 ]]; then
|
|
66
|
-
log_truncated=true
|
|
67
|
-
fi
|
|
68
|
-
# Track the maximum size we've seen for truncation detection
|
|
69
|
-
local prev_max_size=$prev_max_size
|
|
70
|
-
if [[ $cur_size -gt $prev_max_size ]]; then
|
|
71
|
-
prev_max_size=$cur_size
|
|
72
|
-
fi
|
|
73
|
-
|
|
74
|
-
local growth=$((cur_size - prev_size))
|
|
75
|
-
prev_size=$cur_size
|
|
76
|
-
|
|
77
|
-
local child_activity_signature=""
|
|
78
|
-
local child_total_bytes=0
|
|
79
|
-
local child_session_count=0
|
|
80
|
-
if [[ -f "$progress_json" ]]; then
|
|
81
|
-
local child_activity_data
|
|
82
|
-
child_activity_data=$(python3 - "$progress_json" <<'PY' 2>/dev/null || true
|
|
83
|
-
import json
|
|
84
|
-
import sys
|
|
85
|
-
|
|
86
|
-
try:
|
|
87
|
-
with open(sys.argv[1], "r", encoding="utf-8") as fh:
|
|
88
|
-
progress = json.load(fh)
|
|
89
|
-
except Exception:
|
|
90
|
-
sys.exit(0)
|
|
91
|
-
|
|
92
|
-
signature = str(progress.get("child_activity_signature") or "")
|
|
93
|
-
total_bytes = int(progress.get("child_total_bytes") or 0)
|
|
94
|
-
session_count = len(progress.get("child_session_files") or [])
|
|
95
|
-
print(f"{signature}\t{total_bytes}\t{session_count}")
|
|
96
|
-
PY
|
|
97
|
-
)
|
|
98
|
-
if [[ -n "$child_activity_data" ]]; then
|
|
99
|
-
IFS=$'\t' read -r child_activity_signature child_total_bytes child_session_count <<< "$child_activity_data"
|
|
100
|
-
fi
|
|
101
|
-
fi
|
|
102
|
-
|
|
103
|
-
local child_growth=0
|
|
104
|
-
if [[ -n "$child_activity_signature" && "$child_activity_signature" != "$prev_child_activity_signature" ]]; then
|
|
105
|
-
child_growth=1
|
|
106
|
-
fi
|
|
107
|
-
prev_child_activity_signature="$child_activity_signature"
|
|
108
|
-
|
|
109
|
-
local effective_stale_kill_threshold="$stale_kill_threshold"
|
|
110
|
-
if [[ $stale_kill_threshold -gt 0 && -f "$progress_json" ]]; then
|
|
111
|
-
local extended_threshold
|
|
112
|
-
extended_threshold=$(python3 - "$progress_json" "$stale_kill_threshold" <<'PY' 2>/dev/null || true
|
|
113
|
-
import json
|
|
114
|
-
import os
|
|
115
|
-
import sys
|
|
116
|
-
|
|
117
|
-
progress_path = sys.argv[1]
|
|
118
|
-
base_threshold = int(sys.argv[2])
|
|
119
|
-
|
|
120
|
-
with open(progress_path, "r", encoding="utf-8") as fh:
|
|
121
|
-
progress = json.load(fh)
|
|
122
|
-
|
|
123
|
-
spawn_count = 0
|
|
124
|
-
for tool in progress.get("tool_calls", []):
|
|
125
|
-
if isinstance(tool, dict) and tool.get("name") in ("spawn_agent", "Agent", "TaskCreate"):
|
|
126
|
-
try:
|
|
127
|
-
spawn_count += int(tool.get("count", 0))
|
|
128
|
-
except (TypeError, ValueError):
|
|
129
|
-
pass
|
|
130
|
-
|
|
131
|
-
# Also check the subagent_spawn_count field (set by _record_cb_agent_tool_call)
|
|
132
|
-
if not spawn_count:
|
|
133
|
-
spawn_count = int(progress.get("subagent_spawn_count", 0))
|
|
134
|
-
|
|
135
|
-
fmt = progress.get("event_format", "")
|
|
136
|
-
|
|
137
|
-
# Codex: current_tool == "wait" means parent is blocked on spawn_agent completion
|
|
138
|
-
if (
|
|
139
|
-
fmt == "codex-json"
|
|
140
|
-
and progress.get("current_tool") == "wait"
|
|
141
|
-
and spawn_count > 0
|
|
142
|
-
):
|
|
143
|
-
configured = os.environ.get("CODEX_WAIT_STALE_KILL_THRESHOLD", "")
|
|
144
|
-
try:
|
|
145
|
-
wait_threshold = int(configured)
|
|
146
|
-
except ValueError:
|
|
147
|
-
wait_threshold = max(base_threshold * 4, 3600)
|
|
148
|
-
if wait_threshold > base_threshold:
|
|
149
|
-
print(wait_threshold)
|
|
150
|
-
|
|
151
|
-
# CodeBuddy: Agent tool blocks synchronously; Task* tools imply bg agents.
|
|
152
|
-
# Extend the stale window when sub-agents have been spawned so the heartbeat
|
|
153
|
-
# doesn't kill the parent while children are still running.
|
|
154
|
-
if (
|
|
155
|
-
fmt == "stream-json"
|
|
156
|
-
and spawn_count > 0
|
|
157
|
-
and progress.get("cb_session_id", "")
|
|
158
|
-
):
|
|
159
|
-
configured = os.environ.get("CB_SUBAGENT_STALE_KILL_THRESHOLD", "")
|
|
160
|
-
try:
|
|
161
|
-
cb_threshold = int(configured)
|
|
162
|
-
except ValueError:
|
|
163
|
-
cb_threshold = max(base_threshold * 4, 3600)
|
|
164
|
-
if cb_threshold > base_threshold:
|
|
165
|
-
print(cb_threshold)
|
|
166
|
-
PY
|
|
167
|
-
)
|
|
168
|
-
if [[ "$extended_threshold" =~ ^[0-9]+$ && "$extended_threshold" -gt "$stale_kill_threshold" ]]; then
|
|
169
|
-
effective_stale_kill_threshold="$extended_threshold"
|
|
170
|
-
fi
|
|
171
|
-
fi
|
|
172
|
-
|
|
173
|
-
# Check for error-loop: agent is actively producing output but results are
|
|
174
|
-
# all read-offset errors or wasted calls. This is a stuck agent that appears
|
|
175
|
-
# "active" by log growth but is accomplishing nothing.
|
|
176
|
-
local error_loop_detected=false
|
|
177
|
-
if [[ $effective_stale_kill_threshold -gt 0 && $growth -gt 0 && -f "$progress_json" ]]; then
|
|
178
|
-
local error_loop_flag
|
|
179
|
-
error_loop_flag=$(python3 - "$progress_json" <<'PY' 2>/dev/null || true
|
|
180
|
-
import json, sys
|
|
181
|
-
try:
|
|
182
|
-
with open(sys.argv[1], encoding="utf-8") as fh:
|
|
183
|
-
progress = json.load(fh)
|
|
184
|
-
except Exception:
|
|
185
|
-
raise SystemExit(0)
|
|
186
|
-
errors = progress.get("errors", [])
|
|
187
|
-
if isinstance(errors, list) and len(errors) >= 5:
|
|
188
|
-
recent = errors[-5:]
|
|
189
|
-
if all(isinstance(e, dict) and e.get("type") in ("read_offset_overflow", "wasted_call") for e in recent):
|
|
190
|
-
print("error_loop")
|
|
191
|
-
PY
|
|
192
|
-
)
|
|
193
|
-
if [[ "$error_loop_flag" == "error_loop" ]]; then
|
|
194
|
-
error_loop_detected=true
|
|
195
|
-
fi
|
|
196
|
-
fi
|
|
197
|
-
|
|
198
|
-
# Track progress staleness. Parent sessions can sit in a wait/polling
|
|
199
|
-
# tool while child transcripts keep growing, so child activity counts.
|
|
200
|
-
# Error loops bypass normal growth-as-progress because the log is only
|
|
201
|
-
# growing with repeated failed reads or wasted calls.
|
|
202
|
-
#
|
|
203
|
-
# When the main session log has been truncated (e.g. by subagent stdout
|
|
204
|
-
# redirection severing the file descriptor), use child activity as the
|
|
205
|
-
# primary progress signal — the parent process is still running and children
|
|
206
|
-
# are still producing output, so this is not a true stall.
|
|
207
|
-
if [[ "$error_loop_detected" == "true" ]]; then
|
|
208
|
-
stale_seconds=$effective_stale_kill_threshold
|
|
209
|
-
elif [[ "$log_truncated" == "true" ]]; then
|
|
210
|
-
# Log truncated: rely on child growth and CLI process liveness.
|
|
211
|
-
# Don't increment staleness — the process is still working.
|
|
212
|
-
if [[ $child_growth -gt 0 ]]; then
|
|
213
|
-
stale_seconds=0
|
|
214
|
-
fi
|
|
215
|
-
# If neither log nor children grow, this is a real stall
|
|
216
|
-
elif [[ $growth -le 0 && $child_growth -eq 0 ]]; then
|
|
217
|
-
stale_seconds=$((stale_seconds + heartbeat_interval))
|
|
218
|
-
else
|
|
219
|
-
stale_seconds=0
|
|
220
|
-
fi
|
|
221
|
-
|
|
222
|
-
local size_display
|
|
223
|
-
if $log_truncated; then
|
|
224
|
-
size_display="${cur_size}B/TRUNCATED(from ${prev_max_size}B)"
|
|
225
|
-
elif [[ $cur_size -gt 1048576 ]]; then
|
|
226
|
-
size_display="$((cur_size / 1048576))MB"
|
|
227
|
-
elif [[ $cur_size -gt 1024 ]]; then
|
|
228
|
-
size_display="$((cur_size / 1024))KB"
|
|
229
|
-
else
|
|
230
|
-
size_display="${cur_size}B"
|
|
231
|
-
fi
|
|
232
|
-
local child_display=""
|
|
233
|
-
if [[ ${child_total_bytes:-0} -gt 0 ]]; then
|
|
234
|
-
local child_size_display
|
|
235
|
-
if [[ $child_total_bytes -gt 1048576 ]]; then
|
|
236
|
-
child_size_display="$((child_total_bytes / 1048576))MB"
|
|
237
|
-
elif [[ $child_total_bytes -gt 1024 ]]; then
|
|
238
|
-
child_size_display="$((child_total_bytes / 1024))KB"
|
|
239
|
-
else
|
|
240
|
-
child_size_display="${child_total_bytes}B"
|
|
241
|
-
fi
|
|
242
|
-
child_display=" | child: ${child_size_display}"
|
|
243
|
-
if [[ ${child_session_count:-0} -gt 1 ]]; then
|
|
244
|
-
child_display="${child_display}/${child_session_count}"
|
|
245
|
-
fi
|
|
246
|
-
fi
|
|
247
|
-
|
|
248
|
-
local mins=$((elapsed / 60))
|
|
249
|
-
local secs=$((elapsed % 60))
|
|
250
|
-
|
|
251
|
-
local status_icon
|
|
252
|
-
if $log_truncated; then
|
|
253
|
-
status_icon="${RED}⚠${NC}"
|
|
254
|
-
elif [[ $growth -gt 0 || $child_growth -gt 0 ]]; then
|
|
255
|
-
status_icon="${GREEN}▶${NC}"
|
|
256
|
-
else
|
|
257
|
-
status_icon="${YELLOW}⏸${NC}"
|
|
258
|
-
fi
|
|
259
|
-
|
|
260
|
-
# Fatal provider/runtime errors are terminal; do not wait for the
|
|
261
|
-
# stale window when progress.json already proves the model cannot
|
|
262
|
-
# continue (for example context_too_large).
|
|
263
|
-
if [[ -f "$progress_json" ]]; then
|
|
264
|
-
local fatal_error_code=""
|
|
265
|
-
fatal_error_code=$(python3 - "$progress_json" <<'PY' 2>/dev/null || true
|
|
266
|
-
import json
|
|
267
|
-
import sys
|
|
268
|
-
try:
|
|
269
|
-
with open(sys.argv[1], encoding="utf-8") as fh:
|
|
270
|
-
progress = json.load(fh)
|
|
271
|
-
except Exception:
|
|
272
|
-
raise SystemExit(0)
|
|
273
|
-
code = progress.get("fatal_error_code") or ""
|
|
274
|
-
if code:
|
|
275
|
-
print(code)
|
|
276
|
-
PY
|
|
277
|
-
)
|
|
278
|
-
if [[ -n "$fatal_error_code" ]]; then
|
|
279
|
-
echo -e " ${RED}[HEARTBEAT]${NC} ${mins}m${secs}s | log: ${size_display} | ${RED}FATAL: ${fatal_error_code}${NC}"
|
|
280
|
-
local _marker_dir
|
|
281
|
-
_marker_dir="$(dirname "$session_log")"
|
|
282
|
-
echo "{\"killed_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"reason\": \"${fatal_error_code}\", \"fatal_error_code\": \"${fatal_error_code}\", \"stale_seconds\": $stale_seconds, \"threshold\": $effective_stale_kill_threshold}" > "$_marker_dir/fatal-error.json" 2>/dev/null || true
|
|
283
|
-
echo "{\"killed_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"reason\": \"${fatal_error_code}\", \"fatal_error_code\": \"${fatal_error_code}\", \"stale_seconds\": $stale_seconds, \"threshold\": $effective_stale_kill_threshold}" > "$_marker_dir/stale-kill.json" 2>/dev/null || true
|
|
284
|
-
kill -TERM "$cli_pid" 2>/dev/null || true
|
|
285
|
-
local fatal_kill_grace_seconds="${STALE_KILL_GRACE_SECONDS:-10}"
|
|
286
|
-
if [[ $fatal_kill_grace_seconds -gt 0 ]]; then
|
|
287
|
-
sleep "$fatal_kill_grace_seconds"
|
|
288
|
-
fi
|
|
289
|
-
if kill -0 "$cli_pid" 2>/dev/null; then
|
|
290
|
-
kill -9 "$cli_pid" 2>/dev/null || true
|
|
291
|
-
fi
|
|
292
|
-
break
|
|
293
|
-
fi
|
|
294
|
-
fi
|
|
295
|
-
|
|
296
|
-
# Stale-kill: auto-terminate process if no progress for too long.
|
|
297
|
-
# Parent sessions can wait on spawned work; child transcript growth
|
|
298
|
-
# counts as progress above, while silent waits still use the active
|
|
299
|
-
# stale window to surface stuck agents promptly.
|
|
300
|
-
if [[ $effective_stale_kill_threshold -gt 0 && $stale_seconds -ge $effective_stale_kill_threshold ]]; then
|
|
301
|
-
local stale_mins=$((stale_seconds / 60))
|
|
302
|
-
echo -e " ${RED}[HEARTBEAT]${NC} ${mins}m${secs}s | log: ${size_display} | ${RED}STALE-KILL: no progress for ${stale_mins}m (threshold: ${effective_stale_kill_threshold}s)${NC}"
|
|
303
|
-
echo -e " ${RED}[HEARTBEAT]${NC} Killing AI CLI process $cli_pid (stale session)..."
|
|
304
|
-
# Write the marker before killing. Some CLIs exit quickly, and the
|
|
305
|
-
# parent runner may stop this heartbeat process immediately after
|
|
306
|
-
# wait(1) returns.
|
|
307
|
-
local _marker_dir
|
|
308
|
-
_marker_dir="$(dirname "$session_log")"
|
|
309
|
-
echo "{\"killed_at\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\", \"reason\": \"stale_session\", \"stale_seconds\": $stale_seconds, \"threshold\": $effective_stale_kill_threshold}" > "$_marker_dir/stale-kill.json" 2>/dev/null || true
|
|
310
|
-
kill -TERM "$cli_pid" 2>/dev/null || true
|
|
311
|
-
# Give process 10s to exit gracefully, then force kill
|
|
312
|
-
local stale_kill_grace_seconds="${STALE_KILL_GRACE_SECONDS:-10}"
|
|
313
|
-
if [[ $stale_kill_grace_seconds -gt 0 ]]; then
|
|
314
|
-
sleep "$stale_kill_grace_seconds"
|
|
315
|
-
fi
|
|
316
|
-
if kill -0 "$cli_pid" 2>/dev/null; then
|
|
317
|
-
echo -e " ${RED}[HEARTBEAT]${NC} Process still alive after SIGTERM, sending SIGKILL..."
|
|
318
|
-
kill -9 "$cli_pid" 2>/dev/null || true
|
|
319
|
-
fi
|
|
320
|
-
break
|
|
321
|
-
fi
|
|
322
|
-
|
|
323
|
-
# Build staleness hint for display
|
|
324
|
-
local stale_hint=""
|
|
325
|
-
if [[ $effective_stale_kill_threshold -gt 0 && $stale_seconds -gt 0 ]]; then
|
|
326
|
-
local stale_mins=$((stale_seconds / 60))
|
|
327
|
-
local threshold_mins=$((effective_stale_kill_threshold / 60))
|
|
328
|
-
stale_hint=" | stale: ${stale_mins}m/${threshold_mins}m"
|
|
329
|
-
fi
|
|
330
|
-
|
|
331
|
-
# Try structured progress from progress.json
|
|
332
|
-
if [[ -f "$progress_json" ]]; then
|
|
333
|
-
local phase tool msgs tools_total
|
|
334
|
-
phase=$(python3 -c "
|
|
335
|
-
import json, sys
|
|
336
|
-
try:
|
|
337
|
-
with open(sys.argv[1]) as f:
|
|
338
|
-
d = json.load(f)
|
|
339
|
-
parts = []
|
|
340
|
-
if d.get('current_phase'):
|
|
341
|
-
parts.append('phase: ' + d['current_phase'])
|
|
342
|
-
if d.get('current_tool'):
|
|
343
|
-
parts.append('tool: ' + d['current_tool'])
|
|
344
|
-
parts.append('msgs: ' + str(d.get('message_count', 0)))
|
|
345
|
-
parts.append(str(d.get('total_tool_calls', 0)) + ' tool calls')
|
|
346
|
-
print(' | '.join(parts))
|
|
347
|
-
except Exception:
|
|
348
|
-
sys.exit(1)
|
|
349
|
-
" "$progress_json" 2>/dev/null) && {
|
|
350
|
-
echo -e " ${status_icon} ${BLUE}[HEARTBEAT]${NC} ${mins}m${secs}s | log: ${size_display}${child_display} | ${phase}${stale_hint}"
|
|
351
|
-
continue
|
|
352
|
-
}
|
|
353
|
-
fi
|
|
354
|
-
|
|
355
|
-
# Fallback: tail-based activity detection
|
|
356
|
-
local last_activity=""
|
|
357
|
-
if [[ -f "$session_log" ]]; then
|
|
358
|
-
last_activity=$(tail -20 "$session_log" 2>/dev/null | grep -v '^$' | tail -1 | cut -c1-80 || echo "")
|
|
359
|
-
fi
|
|
360
|
-
|
|
361
|
-
echo -e " ${status_icon} ${BLUE}[HEARTBEAT]${NC} ${mins}m${secs}s elapsed | log: ${size_display}${child_display} (+${growth}B) | ${last_activity}${stale_hint}"
|
|
362
|
-
done
|
|
363
|
-
) &
|
|
364
|
-
_HEARTBEAT_PID=$!
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
# Stop a heartbeat monitor process.
|
|
368
|
-
#
|
|
369
|
-
# Arguments:
|
|
370
|
-
# $1 - heartbeat_pid PID returned by start_heartbeat
|
|
371
|
-
stop_heartbeat() {
|
|
372
|
-
local heartbeat_pid="$1"
|
|
373
|
-
if [[ -n "$heartbeat_pid" ]]; then
|
|
374
|
-
kill "$heartbeat_pid" 2>/dev/null || true
|
|
375
|
-
wait "$heartbeat_pid" 2>/dev/null || true
|
|
376
|
-
fi
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
# Start the stream-json progress parser as a background process.
|
|
380
|
-
# Sets _PARSER_PID to the background process PID.
|
|
381
|
-
# No-op if USE_STREAM_JSON is not "true".
|
|
382
|
-
#
|
|
383
|
-
# Arguments:
|
|
384
|
-
# $1 - session_log Path to session.log
|
|
385
|
-
# $2 - progress_json Path to write progress.json
|
|
386
|
-
# $3 - scripts_dir Path to scripts/ directory
|
|
387
|
-
start_progress_parser() {
|
|
388
|
-
local session_log="$1"
|
|
389
|
-
local progress_json="$2"
|
|
390
|
-
local scripts_dir="$3"
|
|
391
|
-
|
|
392
|
-
_PARSER_PID=""
|
|
393
|
-
|
|
394
|
-
if [[ "${USE_STREAM_JSON:-}" != "true" ]]; then
|
|
395
|
-
return 0
|
|
396
|
-
fi
|
|
397
|
-
|
|
398
|
-
local parser_script="$scripts_dir/parse-stream-progress.py"
|
|
399
|
-
if [[ ! -f "$parser_script" ]]; then
|
|
400
|
-
return 0
|
|
401
|
-
fi
|
|
402
|
-
|
|
403
|
-
python3 "$parser_script" \
|
|
404
|
-
--session-log "$session_log" \
|
|
405
|
-
--progress-file "$progress_json" &
|
|
406
|
-
_PARSER_PID=$!
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
# Stop the progress parser process.
|
|
410
|
-
#
|
|
411
|
-
# Arguments:
|
|
412
|
-
# $1 - parser_pid PID returned by start_progress_parser
|
|
413
|
-
stop_progress_parser() {
|
|
414
|
-
local parser_pid="$1"
|
|
415
|
-
if [[ -n "$parser_pid" ]]; then
|
|
416
|
-
kill "$parser_pid" 2>/dev/null || true
|
|
417
|
-
wait "$parser_pid" 2>/dev/null || true
|
|
418
|
-
fi
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
# Detect whether the AI CLI supports structured JSON progress.
|
|
422
|
-
# Sets USE_STREAM_JSON to "true" or "false".
|
|
423
|
-
#
|
|
424
|
-
# Arguments:
|
|
425
|
-
# $1 - cli_cmd The AI CLI command
|
|
426
|
-
detect_stream_json_support() {
|
|
427
|
-
local cli_cmd="$1"
|
|
428
|
-
USE_STREAM_JSON="false"
|
|
429
|
-
STREAM_JSON_FORMAT=""
|
|
430
|
-
|
|
431
|
-
local session_platform=""
|
|
432
|
-
session_platform="$(_prizm_known_platform_from_cli "$cli_cmd" 2>/dev/null || true)"
|
|
433
|
-
if [[ -z "$session_platform" ]]; then
|
|
434
|
-
case "$(_prizm_normalize_platform "${PRIZMKIT_PLATFORM:-${PLATFORM:-}}")" in
|
|
435
|
-
codex|claude|codebuddy)
|
|
436
|
-
session_platform="$(_prizm_normalize_platform "${PRIZMKIT_PLATFORM:-${PLATFORM:-}}")"
|
|
437
|
-
;;
|
|
438
|
-
esac
|
|
439
|
-
fi
|
|
440
|
-
|
|
441
|
-
# CodeBuddy (cbc) always supports stream-json
|
|
442
|
-
if [[ "$session_platform" == "codebuddy" || "$cli_cmd" == "cbc" ]]; then
|
|
443
|
-
USE_STREAM_JSON="true"
|
|
444
|
-
STREAM_JSON_FORMAT="stream-json"
|
|
445
|
-
export USE_STREAM_JSON STREAM_JSON_FORMAT
|
|
446
|
-
return 0
|
|
447
|
-
fi
|
|
448
|
-
|
|
449
|
-
# Codex uses `codex exec --json` JSONL, not `--output-format stream-json`.
|
|
450
|
-
if [[ "$session_platform" == "codex" ]]; then
|
|
451
|
-
local codex_help
|
|
452
|
-
codex_help=$("$cli_cmd" exec --help 2>&1) || true
|
|
453
|
-
if echo "$codex_help" | grep -q -- "--json"; then
|
|
454
|
-
USE_STREAM_JSON="true"
|
|
455
|
-
STREAM_JSON_FORMAT="codex-json"
|
|
456
|
-
fi
|
|
457
|
-
export USE_STREAM_JSON STREAM_JSON_FORMAT
|
|
458
|
-
return 0
|
|
459
|
-
fi
|
|
460
|
-
|
|
461
|
-
# For other CLIs, try to detect support via --help output
|
|
462
|
-
# Use explicit file descriptor to avoid issues in background processes
|
|
463
|
-
local help_output
|
|
464
|
-
help_output=$("$cli_cmd" --help 2>&1) || true
|
|
465
|
-
|
|
466
|
-
if echo "$help_output" | grep -q "stream-json"; then
|
|
467
|
-
USE_STREAM_JSON="true"
|
|
468
|
-
STREAM_JSON_FORMAT="stream-json"
|
|
469
|
-
fi
|
|
470
|
-
export USE_STREAM_JSON STREAM_JSON_FORMAT
|
|
471
|
-
}
|