-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-healthcheck.sh
More file actions
executable file
·279 lines (231 loc) · 7.52 KB
/
Copy pathcodex-healthcheck.sh
File metadata and controls
executable file
·279 lines (231 loc) · 7.52 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
#!/usr/bin/env bash
set -uo pipefail
umask 077
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_FILE="${LOG_FILE:-$SCRIPT_DIR/codex-healthcheck.log}"
PROMPTS_FILE="${PROMPTS_FILE:-$SCRIPT_DIR/prompts.txt}"
MIN_INTERVAL_SEC="${MIN_INTERVAL_SEC:-2700}"
MAX_INTERVAL_SEC="${MAX_INTERVAL_SEC:-3300}"
REQUEST_TIMEOUT_SEC="${REQUEST_TIMEOUT_SEC:-180}"
RUN_ONCE=false
DRY_RUN=false
CURRENT_RUN_DIR=""
CURRENT_RESPONSE_FILE=""
CURRENT_RAW_FILE=""
CURRENT_COMMAND_PID=""
CURRENT_WATCHDOG_PID=""
usage() {
cat <<'EOF'
Usage: codex-healthcheck.sh [--once] [--dry-run]
Options:
--once Send one request, then exit.
--dry-run Create and verify the temporary workspace without calling Codex.
-h, --help Show this help.
Environment variables:
MIN_INTERVAL_SEC Minimum delay between requests (default: 2700).
MAX_INTERVAL_SEC Maximum delay between requests (default: 3300).
REQUEST_TIMEOUT_SEC Per-request timeout (default: 180).
LOG_FILE Status log path.
PROMPTS_FILE Prompt list path (one prompt per line).
CODEX_BIN Codex executable path.
EOF
}
log() {
local message="$1"
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$message" | tee -a "$LOG_FILE"
}
is_positive_integer() {
case "$1" in
''|*[!0-9]*|0) return 1 ;;
*) return 0 ;;
esac
}
cleanup_current_run() {
if [ -n "$CURRENT_RUN_DIR" ] && [ "${PWD:-}" = "$CURRENT_RUN_DIR" ]; then
cd /tmp 2>/dev/null || true
fi
if [ -n "$CURRENT_RESPONSE_FILE" ]; then
rm -f -- "$CURRENT_RESPONSE_FILE"
CURRENT_RESPONSE_FILE=""
fi
if [ -n "$CURRENT_RAW_FILE" ]; then
rm -f -- "$CURRENT_RAW_FILE"
CURRENT_RAW_FILE=""
fi
if [ -n "$CURRENT_RUN_DIR" ] && [ -d "$CURRENT_RUN_DIR" ]; then
if ! rmdir -- "$CURRENT_RUN_DIR" 2>/dev/null; then
log "WARNING: temporary workspace was not empty; left in place: $CURRENT_RUN_DIR"
fi
CURRENT_RUN_DIR=""
fi
}
handle_signal() {
if [ -n "$CURRENT_COMMAND_PID" ]; then
kill -TERM "$CURRENT_COMMAND_PID" 2>/dev/null || true
fi
if [ -n "$CURRENT_WATCHDOG_PID" ]; then
kill "$CURRENT_WATCHDOG_PID" 2>/dev/null || true
fi
log "Stopped by signal."
cleanup_current_run
exit 130
}
trap handle_signal INT TERM
trap cleanup_current_run EXIT
run_with_timeout() {
local timeout_sec="$1"
shift
"$@" &
CURRENT_COMMAND_PID=$!
(
sleep "$timeout_sec"
kill -TERM "$CURRENT_COMMAND_PID" 2>/dev/null || exit 0
sleep 5
kill -KILL "$CURRENT_COMMAND_PID" 2>/dev/null || true
) &
CURRENT_WATCHDOG_PID=$!
local exit_code=0
wait "$CURRENT_COMMAND_PID" || exit_code=$?
kill "$CURRENT_WATCHDOG_PID" 2>/dev/null || true
wait "$CURRENT_WATCHDOG_PID" 2>/dev/null || true
CURRENT_COMMAND_PID=""
CURRENT_WATCHDOG_PID=""
case "$exit_code" in
137|143) return 124 ;;
*) return "$exit_code" ;;
esac
}
random_interval() {
local span=$((MAX_INTERVAL_SEC - MIN_INTERVAL_SEC + 1))
printf '%s\n' "$((MIN_INTERVAL_SEC + RANDOM % span))"
}
pick_prompt() {
local line check
local prompts=()
while IFS= read -r line || [ -n "$line" ]; do
line="${line%$'\r'}"
check="${line#"${line%%[![:space:]]*}"}"
[ -z "$check" ] && continue
case "$check" in
\#*) continue ;;
esac
prompts[${#prompts[@]}]="$line"
done < "$PROMPTS_FILE"
if [ "${#prompts[@]}" -eq 0 ]; then
return 1
fi
printf '%s\n' "${prompts[$((RANDOM % ${#prompts[@]}))]}"
}
run_request() {
local request_id selected_prompt prompt exit_code response_bytes previous_dir
CURRENT_RUN_DIR=$(mktemp -d /tmp/cxrun.XXXXXX) || return 1
CURRENT_RESPONSE_FILE=$(mktemp /tmp/cxresponse.XXXXXX) || return 1
CURRENT_RAW_FILE=$(mktemp /tmp/cxoutput.XXXXXX) || return 1
if [ -n "$(ls -A "$CURRENT_RUN_DIR" 2>/dev/null)" ]; then
log "ERROR: temporary workspace is not empty: $CURRENT_RUN_DIR"
cleanup_current_run
return 1
fi
if ! selected_prompt=$(pick_prompt); then
log "ERROR: no usable prompts found in $PROMPTS_FILE"
cleanup_current_run
return 1
fi
request_id="$(date +%s)-$RANDOM"
prompt="$selected_prompt
Answer concisely in at most 80 words. Do not inspect local files, run commands, browse, or use tools. ID=$request_id"
log "Prompt: ${selected_prompt:0:100}"
if [ "$DRY_RUN" = true ]; then
log "DRY RUN: verified empty workspace $CURRENT_RUN_DIR"
cleanup_current_run
return 0
fi
local codex_args=(
exec
--ephemeral
--skip-git-repo-check
--ignore-rules
--sandbox read-only
--color never
-C "$CURRENT_RUN_DIR"
-c 'model_reasoning_effort="low"'
-c 'project_doc_max_bytes=0'
-c 'project_doc_fallback_filenames=[]'
-c 'history.persistence="none"'
-c 'features.memories=false'
-c 'features.skills=false'
-c 'features.multi_agent=false'
-c 'features.personality=false'
-c 'features.shell_snapshot=false'
-c 'notify=[]'
-c 'disable_response_storage=true'
--output-last-message "$CURRENT_RESPONSE_FILE"
"$prompt"
)
previous_dir="$PWD"
exit_code=0
if cd "$CURRENT_RUN_DIR"; then
run_with_timeout "$REQUEST_TIMEOUT_SEC" "$CODEX_BIN" "${codex_args[@]}" \
>"$CURRENT_RAW_FILE" 2>&1 || exit_code=$?
cd "$previous_dir" 2>/dev/null || cd "$SCRIPT_DIR" || exit 1
else
exit_code=1
fi
if [ "$exit_code" -eq 0 ] && [ -s "$CURRENT_RESPONSE_FILE" ]; then
response_bytes=$(wc -c < "$CURRENT_RESPONSE_FILE" | tr -d '[:space:]')
log "OK (response=${response_bytes}B, workspace=$CURRENT_RUN_DIR)"
else
log "FAILED (exit=$exit_code, workspace=$CURRENT_RUN_DIR)"
{
printf '%s\n' '--- last command output ---'
tail -n 30 "$CURRENT_RAW_FILE" 2>/dev/null || true
printf '%s\n' '--- end command output ---'
} >> "$LOG_FILE"
fi
cleanup_current_run
return "$exit_code"
}
while [ "$#" -gt 0 ]; do
case "$1" in
--once) RUN_ONCE=true ;;
--dry-run) DRY_RUN=true; RUN_ONCE=true ;;
-h|--help) usage; exit 0 ;;
*) printf 'Unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;;
esac
shift
done
if ! is_positive_integer "$MIN_INTERVAL_SEC" ||
! is_positive_integer "$MAX_INTERVAL_SEC" ||
! is_positive_integer "$REQUEST_TIMEOUT_SEC"; then
printf 'Intervals and timeout must be positive integers.\n' >&2
exit 2
fi
if [ "$MIN_INTERVAL_SEC" -gt "$MAX_INTERVAL_SEC" ]; then
printf 'MIN_INTERVAL_SEC must be <= MAX_INTERVAL_SEC.\n' >&2
exit 2
fi
CODEX_BIN="${CODEX_BIN:-$(command -v codex 2>/dev/null || true)}"
if [ -z "$CODEX_BIN" ] || [ ! -x "$CODEX_BIN" ]; then
printf 'Codex executable not found. Set CODEX_BIN or update PATH.\n' >&2
exit 127
fi
if [ ! -r "$PROMPTS_FILE" ]; then
printf 'Prompt file is not readable: %s\n' "$PROMPTS_FILE" >&2
exit 2
fi
if ! pick_prompt >/dev/null; then
printf 'Prompt file has no non-empty, non-comment lines: %s\n' "$PROMPTS_FILE" >&2
exit 2
fi
mkdir -p "$(dirname "$LOG_FILE")"
log "Started (interval=${MIN_INTERVAL_SEC}-${MAX_INTERVAL_SEC}s, timeout=${REQUEST_TIMEOUT_SEC}s)."
while true; do
run_request || true
if [ "$RUN_ONCE" = true ]; then
break
fi
sleep_sec=$(random_interval)
log "Next request in ${sleep_sec}s."
sleep "$sleep_sec"
done
log "Finished."