Repository navigation
Expand file tree
/
Copy pathkoder-dev.sh
More file actions
executable file
·561 lines (521 loc) · 14.1 KB
/
Copy pathkoder-dev.sh
File metadata and controls
executable file
·561 lines (521 loc) · 14.1 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_SCRIPT="$ROOT_DIR/scripts/build-koder"
BIN="${KODER_DEV_BIN:-/tmp/koder-dev-${USER:-user}/koder}"
SETTLE_SECONDS="${KODER_DEV_SETTLE_SECONDS:-10}"
POLL_SECONDS="${KODER_DEV_POLL_SECONDS:-1}"
STOP_GRACE_SECONDS="${KODER_DEV_STOP_GRACE_SECONDS:-5}"
STOP_TIMEOUT_SECONDS="${KODER_DEV_STOP_TIMEOUT_SECONDS:-20}"
SHUTDOWN_GRACE_SECONDS="${KODER_DEV_SHUTDOWN_GRACE_SECONDS:-60}"
SHUTDOWN_TIMEOUT_SECONDS="${KODER_DEV_SHUTDOWN_TIMEOUT_SECONDS:-75}"
RESTART_EXIT_CODE="${KODER_DEV_RESTART_EXIT_CODE:-75}"
KODER_DEV_LOG_DIR="${KODER_DEV_LOG_DIR:-/tmp/koder-dev-${USER:-user}}"
KODER_SUPERVISOR_LOG="${KODER_DEV_SUPERVISOR_LOG:-$KODER_DEV_LOG_DIR/supervisor.log}"
KODER_OUTPUT_LOG_REQUESTED="${KODER_DEV_OUTPUT_LOG:-}"
KODER_OUTPUT_LOG=""
KODER_OUTPUT_LATEST_LOG="${KODER_DEV_OUTPUT_LATEST_LOG:-$KODER_DEV_LOG_DIR/output.latest.log}"
KODER_DEV_WEB_BIND="${KODER_DEV_WEB_BIND:-0.0.0.0:7979}"
mkdir -p "$KODER_DEV_LOG_DIR"
child_pid=""
shutting_down=0
restart_failures=0
launch_count=0
log() {
local line
line="[$(date '+%Y-%m-%d %H:%M:%S')] $*"
printf '%s\n' "$line"
printf '%s\n' "$line" >>"$KODER_SUPERVISOR_LOG"
}
now_ms() {
date '+%s%3N'
}
elapsed_ms() {
local start="$1"
printf '%s' "$(( $(now_ms) - start ))"
}
exit_status_text() {
local status="$1"
if (( status == 0 )); then
printf 'status 0'
elif (( status > 128 )); then
printf 'signal %d (status %d)' "$((status - 128))" "$status"
else
printf 'status %d' "$status"
fi
}
cleanup() {
if (( shutting_down )); then
return 0
fi
shutting_down=1
if [[ -n "$child_pid" ]] && kill -0 "$child_pid" 2>/dev/null; then
stop_koder "$child_pid" "shutdown" || true
fi
}
handle_signal() {
local signal="$1"
local status="$2"
if [[ "$signal" == "TERM" ]]; then
log "received TERM from outside dev-restart; shutting down with error"
report_live_koder_status
report_koder_output
else
log "received $signal; shutting down"
fi
cleanup
exit "$status"
}
trap cleanup EXIT
trap 'handle_signal INT 130' INT
trap 'handle_signal TERM 1' TERM
build_koder() {
local started
started="$(now_ms)"
"$BUILD_SCRIPT" "$BIN" >/dev/null
log "build completed in $(elapsed_ms "$started")ms"
}
has_web_bind_arg() {
local arg
for arg in "$@"; do
case "$arg" in
--)
return 1
;;
--web-bind|--web-bind=*)
return 0
;;
esac
done
return 1
}
explicit_koder_subcommand() {
local expect_value=0
local arg
for arg in "$@"; do
if (( expect_value )); then
expect_value=0
continue
fi
case "$arg" in
--)
return 1
;;
--data-dir|--config|--config-file)
expect_value=1
;;
--data-dir=*|--config=*|--config-file=*)
;;
-*)
;;
serve|doctor|version|session|debug|skill|exec|completion|help)
printf '%s\n' "$arg"
return 0
;;
*)
return 1
;;
esac
done
return 1
}
launch_args() {
local subcommand
subcommand="$(explicit_koder_subcommand "$@" || true)"
if [[ "$subcommand" != "" && "$subcommand" != "serve" ]]; then
printf '%s\0' "$@"
return 0
fi
if [[ "$subcommand" == "serve" ]]; then
if has_web_bind_arg "$@"; then
printf '%s\0' "$@"
return 0
fi
printf '%s\0' "$@" "--web-bind=$KODER_DEV_WEB_BIND"
return 0
fi
if has_web_bind_arg "$@"; then
printf '%s\0' "serve" "$@"
return 0
fi
printf '%s\0' "serve" "--web-bind=$KODER_DEV_WEB_BIND" "$@"
}
launch_koder() {
local args=()
local started
local started_label
started="$(now_ms)"
started_label="$(date '+%Y%m%d-%H%M%S')"
launch_count=$((launch_count + 1))
mapfile -d '' -t args < <(launch_args "$@")
if [[ -n "$KODER_OUTPUT_LOG_REQUESTED" ]]; then
KODER_OUTPUT_LOG="$KODER_OUTPUT_LOG_REQUESTED"
else
KODER_OUTPUT_LOG="$KODER_DEV_LOG_DIR/output.${started_label}.${launch_count}.log"
fi
{
printf '\n[%s] launching koder child #%d\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$launch_count"
printf '[%s] command:' "$(date '+%Y-%m-%d %H:%M:%S')"
printf ' %q' "$BIN" "${args[@]}"
printf '\n'
} >>"$KODER_OUTPUT_LOG"
ln -sfn "$KODER_OUTPUT_LOG" "$KODER_OUTPUT_LATEST_LOG" 2>/dev/null || true
"$BIN" "${args[@]}" > >(tee -a "$KODER_OUTPUT_LOG") 2>&1 &
child_pid="$!"
log "launched koder pid=$child_pid in $(elapsed_ms "$started")ms"
log "koder output log: $KODER_OUTPUT_LOG"
log "koder latest output log: $KODER_OUTPUT_LATEST_LOG"
}
stop_koder() {
local pid="$1"
local reason="${2:-restart}"
shift 2 || true
local waited=0
local stat=""
local exit_status=0
local grace_seconds="$STOP_GRACE_SECONDS"
local timeout_seconds="$STOP_TIMEOUT_SECONDS"
if [[ "$reason" == "shutdown" ]]; then
grace_seconds="$SHUTDOWN_GRACE_SECONDS"
timeout_seconds="$SHUTDOWN_TIMEOUT_SECONDS"
fi
if (( timeout_seconds < grace_seconds )); then
timeout_seconds="$grace_seconds"
fi
if [[ -z "$pid" ]] || ! kill -0 "$pid" 2>/dev/null; then
wait "$pid" 2>/dev/null || true
return 0
fi
if [[ "$reason" == "restart" ]]; then
log "stopping koder pid=$pid for restart"
else
log "stopping koder pid=$pid for $reason"
fi
if [[ "$reason" == "restart" ]]; then
request_koder_restart "$@" || kill -USR1 "$pid" 2>/dev/null || true
else
kill -TERM "$pid" 2>/dev/null || true
fi
while true; do
if ! kill -0 "$pid" 2>/dev/null; then
wait "$pid" 2>/dev/null || exit_status=$?
report_stopped_status "$reason" "$exit_status"
return "$exit_status"
fi
stat="$(ps -o stat= -p "$pid" 2>/dev/null)"
stat="${stat//[[:space:]]/}"
if [[ "$stat" == Z* ]]; then
wait "$pid" 2>/dev/null || exit_status=$?
report_stopped_status "$reason" "$exit_status"
return "$exit_status"
fi
if (( waited >= grace_seconds )); then
break
fi
sleep 1
waited=$((waited + 1))
done
log "koder pid=$pid did not stop after ${grace_seconds}s; terminating"
kill -TERM "$pid" 2>/dev/null || true
while true; do
if ! kill -0 "$pid" 2>/dev/null; then
wait "$pid" 2>/dev/null || exit_status=$?
report_stopped_status "$reason" "$exit_status"
return "$exit_status"
fi
stat="$(ps -o stat= -p "$pid" 2>/dev/null)"
stat="${stat//[[:space:]]/}"
if [[ "$stat" == Z* ]]; then
wait "$pid" 2>/dev/null || exit_status=$?
report_stopped_status "$reason" "$exit_status"
return "$exit_status"
fi
if (( waited >= timeout_seconds )); then
log "koder pid=$pid did not terminate after ${timeout_seconds}s; killing"
kill -KILL "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || exit_status=$?
report_stopped_status "$reason" "$exit_status"
return "$exit_status"
fi
sleep 1
waited=$((waited + 1))
done
}
web_bind_arg() {
local expect_value=0
local arg
for arg in "$@"; do
if (( expect_value )); then
printf '%s\n' "$arg"
return 0
fi
case "$arg" in
--)
break
;;
--web-bind=*)
printf '%s\n' "${arg#--web-bind=}"
return 0
;;
--web-bind)
expect_value=1
;;
esac
done
printf '%s\n' "$KODER_DEV_WEB_BIND"
}
restart_needed_url() {
local bind
local addr
local host
local port
bind="$(web_bind_arg "$@")"
addr="${bind#http://}"
addr="${addr#https://}"
host="${addr%:*}"
port="${addr##*:}"
if [[ "$host" == "$addr" ]]; then
host="127.0.0.1"
port="$addr"
fi
case "$host" in
""|"0.0.0.0"|"::"|"[::]")
host="127.0.0.1"
;;
esac
printf 'http://%s:%s/api/restart-needed\n' "$host" "$port"
}
restart_rpc_url() {
local bind
local addr
local host
local port
bind="$(web_bind_arg "$@")"
addr="${bind#http://}"
addr="${addr#https://}"
host="${addr%:*}"
port="${addr##*:}"
if [[ "$host" == "$addr" ]]; then
host="127.0.0.1"
port="$addr"
fi
case "$host" in
""|"0.0.0.0"|"::"|"[::]")
host="127.0.0.1"
;;
esac
printf 'http://%s:%s/api/rpc/restart_process\n' "$host" "$port"
}
request_koder_restart() {
local url
local output
url="$(restart_rpc_url "$@")"
log "requesting koder restart through $url"
if output="$(curl --fail --silent --show-error --max-time 5 -X POST -H 'Content-Type: application/json' --data '{"hard":true}' "$url" 2>&1)"; then
log "restart request acknowledged"
return 0
fi
log "restart request failed: $output"
return 1
}
notify_restart_needed() {
local pid="$1"
shift || true
local url
local output
local payload
if [[ -z "$pid" ]] || ! kill -0 "$pid" 2>/dev/null; then
return 0
fi
url="$(restart_needed_url "$@")"
payload="$(restart_needed_payload)"
log "new koder build is ready; notifying $url with $(restart_needed_log_label "$payload")"
if output="$(curl --fail --silent --show-error --max-time 2 -X POST -H 'Content-Type: application/json' --data "$payload" "$url" 2>&1)"; then
log "restart-needed notification acknowledged"
return 0
fi
log "restart-needed notification failed: $output"
return 1
}
json_escape() {
local value="$1"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
value="${value//$'\n'/ }"
value="${value//$'\r'/ }"
printf '%s' "$value"
}
version_field() {
local text="$1"
local key="$2"
awk -F': ' -v key="$key" '$1 == key {print $2; exit}' <<<"$text"
}
restart_needed_payload() {
local version_text=""
local first_line=""
local version=""
local commit=""
local dirty=""
local build_time=""
local build_id=""
version_text="$("$BIN" version 2>/dev/null || true)"
first_line="$(sed -n '1p' <<<"$version_text")"
version="${first_line##* }"
commit="$(version_field "$version_text" commit)"
dirty="$(version_field "$version_text" dirty)"
build_time="$(version_field "$version_text" build_time)"
build_id="$commit"
if [[ "$dirty" == "true" && -n "$build_id" ]]; then
build_id="${build_id}-dirty"
fi
if [[ -n "$build_time" && -n "$build_id" ]]; then
build_id="${build_id} @ ${build_time}"
fi
printf '{"version":"%s","commit":"%s","dirty":"%s","build_time":"%s","build_id":"%s"}' \
"$(json_escape "$version")" \
"$(json_escape "$commit")" \
"$(json_escape "$dirty")" \
"$(json_escape "$build_time")" \
"$(json_escape "$build_id")"
}
restart_needed_log_label() {
local payload="$1"
local label
label="$(sed -n 's/.*"build_id":"\([^"]*\)".*/\1/p' <<<"$payload")"
if [[ -n "$label" ]]; then
printf 'build %s' "$label"
else
printf 'build metadata'
fi
}
report_stopped_status() {
local reason="$1"
local exit_status="$2"
if [[ "$reason" == "restart" ]]; then
if (( exit_status == RESTART_EXIT_CODE )); then
log "koder exited for restart with $(exit_status_text "$exit_status")"
else
log "koder exited unexpectedly during restart with $(exit_status_text "$exit_status")"
report_koder_output
fi
elif [[ "$reason" == "shutdown" ]]; then
log "koder exited after shutdown with $(exit_status_text "$exit_status")"
report_koder_output
fi
}
report_live_koder_status() {
if [[ -z "$child_pid" ]]; then
log "koder pid: none"
return 0
fi
if kill -0 "$child_pid" 2>/dev/null; then
log "koder pid=$child_pid is still running; no koder exit code is available yet"
return 0
fi
log "koder pid=$child_pid has already exited; collecting exit code"
}
report_koder_output() {
if [[ ! -s "$KODER_OUTPUT_LOG" ]]; then
log "koder output: <empty>"
return 0
fi
log "last koder output from $KODER_OUTPUT_LOG:"
tail -n "${KODER_DEV_OUTPUT_TAIL_LINES:-40}" "$KODER_OUTPUT_LOG" >&2 || true
}
pid_running() {
local pid="$1"
local stat=""
if [[ -z "$pid" ]] || ! kill -0 "$pid" 2>/dev/null; then
return 1
fi
stat="$(ps -o stat= -p "$pid" 2>/dev/null || true)"
stat="${stat//[[:space:]]/}"
[[ "$stat" != Z* ]]
}
source_signature() {
(
cd "$ROOT_DIR"
find \
cmd internal docs scripts \
-type f \
! -path '*/.git/*' \
-printf '%p %T@ %s\n' 2>/dev/null
find . \
-maxdepth 1 \
-type f \
\( -name '*.go' -o -name '*.mod' -o -name '*.sum' -o -name '*.md' -o -name '*.sh' \) \
-printf '%p %T@ %s\n' 2>/dev/null
) | sort
}
log_signature_changes() {
local previous="$1"
local current="$2"
diff -u <(printf '%s\n' "$previous") <(printf '%s\n' "$current") |
awk '
/^[+-][^+-]/ {
line = substr($0, 2)
sub(/ [0-9]+(\.[0-9]+)? [0-9]+$/, "", line)
if (line != "" && !seen[line]++) {
print " " line
}
}
' | tee -a "$KODER_SUPERVISOR_LOG" >&2 || true
}
wait_for_settle() {
local previous
local current
local quiet
previous="$(source_signature)"
quiet=0
while (( quiet < SETTLE_SECONDS )); do
sleep 1
current="$(source_signature)"
if [[ "$current" == "$previous" ]]; then
quiet=$((quiet + 1))
else
previous="$current"
quiet=0
fi
done
}
log "building koder..."
build_koder
launch_koder "$@"
last_signature="$(source_signature)"
while true; do
sleep "$POLL_SECONDS"
if [[ -n "$child_pid" ]] && ! kill -0 "$child_pid" 2>/dev/null; then
exit_status=0
wait "$child_pid" 2>/dev/null || exit_status=$?
child_pid=""
if (( exit_status == RESTART_EXIT_CODE )); then
log "koder exited for restart with $(exit_status_text "$exit_status"); relaunching last successful build"
launch_koder "$@"
continue
fi
log "koder exited unexpectedly with $(exit_status_text "$exit_status")"
report_koder_output
exit 1
fi
current_signature="$(source_signature)"
if [[ "$current_signature" == "$last_signature" ]]; then
continue
fi
log "changes detected; waiting ${SETTLE_SECONDS}s for code to settle..."
log_signature_changes "$last_signature" "$current_signature"
wait_for_settle
last_signature="$(source_signature)"
log "building koder..."
if build_koder; then
if [[ -n "$child_pid" ]] && kill -0 "$child_pid" 2>/dev/null; then
notify_restart_needed "$child_pid" "$@" || true
last_signature="$(source_signature)"
continue
fi
launch_koder "$@"
restart_failures=0
last_signature="$(source_signature)"
else
log "build failed; keeping current koder process"
fi
done