-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathe2e_dashboard_high_risk_actions_real.sh
More file actions
executable file
·1493 lines (1378 loc) · 58.6 KB
/
e2e_dashboard_high_risk_actions_real.sh
File metadata and controls
executable file
·1493 lines (1378 loc) · 58.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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"
export NEXT_PUBLIC_CORTEXPILOT_OPERATOR_ROLE="${NEXT_PUBLIC_CORTEXPILOT_OPERATOR_ROLE:-TECH_LEAD}"
TMP_RUNTIME_DIR="$ROOT_DIR/.runtime-cache/cortexpilot/temp"
mkdir -p "$TMP_RUNTIME_DIR"
export TMPDIR="$TMP_RUNTIME_DIR"
source "$ROOT_DIR/scripts/lib/env.sh"
source "$ROOT_DIR/scripts/lib/e2e_common.sh"
source "$ROOT_DIR/scripts/lib/test_heartbeat.sh"
PYTHON_BIN="${CORTEXPILOT_PYTHON:-}"
is_truthy_flag() {
case "${1:-}" in
1|true|TRUE|yes|YES|on|ON) return 0 ;;
*) return 1 ;;
esac
}
TIMEOUT_PROFILE_INPUT="${CORTEXPILOT_E2E_TIMEOUT_PROFILE:-auto}"
TIMEOUT_PROFILE_SOURCE="auto"
if [[ "$TIMEOUT_PROFILE_INPUT" == "auto" ]]; then
if is_truthy_flag "${CORTEXPILOT_CI_NIGHTLY_FULL:-0}"; then
TIMEOUT_PROFILE="nightly-full"
else
TIMEOUT_PROFILE="pr"
fi
else
TIMEOUT_PROFILE="$TIMEOUT_PROFILE_INPUT"
TIMEOUT_PROFILE_SOURCE="env"
fi
case "$TIMEOUT_PROFILE" in
nightly|full|nightly-full)
FAST_GATE_TIMEOUT_DEFAULT="1200"
LIVE_PREFLIGHT_TIMEOUT_DEFAULT="300"
WARMUP_TIMEOUT_DEFAULT="240"
PLAYWRIGHT_TIMEOUT_DEFAULT="4200"
;;
pr)
FAST_GATE_TIMEOUT_DEFAULT="900"
LIVE_PREFLIGHT_TIMEOUT_DEFAULT="180"
WARMUP_TIMEOUT_DEFAULT="120"
PLAYWRIGHT_TIMEOUT_DEFAULT="3000"
;;
*)
echo "❌ [dashboard-high-risk] invalid timeout profile: $TIMEOUT_PROFILE (supported: pr|nightly-full|auto)"
exit 1
;;
esac
HOST="$(cortexpilot_env_get CORTEXPILOT_E2E_HOST "127.0.0.1")"
API_PORT="$(cortexpilot_env_get CORTEXPILOT_E2E_API_PORT "19400")"
DASHBOARD_PORT="$(cortexpilot_env_get CORTEXPILOT_E2E_DASHBOARD_PORT "19500")"
API_TOKEN="$(cortexpilot_env_get CORTEXPILOT_E2E_API_TOKEN "cortexpilot-e2e-token")"
HEARTBEAT_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_HEARTBEAT_INTERVAL_SEC "20")"
FAST_GATE_TIMEOUT_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_FAST_GATE_TIMEOUT_SEC "$FAST_GATE_TIMEOUT_DEFAULT")"
LIVE_PREFLIGHT_TIMEOUT_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_LIVE_PREFLIGHT_TIMEOUT_SEC "$LIVE_PREFLIGHT_TIMEOUT_DEFAULT")"
WARMUP_TIMEOUT_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_WARMUP_TIMEOUT_SEC "$WARMUP_TIMEOUT_DEFAULT")"
PLAYWRIGHT_TIMEOUT_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_PLAYWRIGHT_TIMEOUT_SEC "$PLAYWRIGHT_TIMEOUT_DEFAULT")"
PLAYWRIGHT_ATTEMPTS="$(cortexpilot_env_get CORTEXPILOT_E2E_PLAYWRIGHT_ATTEMPTS "2")"
PLAYWRIGHT_RETRY_BACKOFF_SEC="$(cortexpilot_env_get CORTEXPILOT_E2E_PLAYWRIGHT_RETRY_BACKOFF_SEC "2")"
ARTIFACT_SUFFIX_ENV="${CORTEXPILOT_E2E_ARTIFACT_SUFFIX:-}"
DASHBOARD_DIST_DIR=".next"
if [[ -n "$ARTIFACT_SUFFIX_ENV" && "$ARTIFACT_SUFFIX_ENV" =~ iter_([0-9]+)$ ]]; then
ITERATION_OFFSET_RAW="${BASH_REMATCH[1]}"
ITERATION_OFFSET="$((10#$ITERATION_OFFSET_RAW))"
if [[ "$ITERATION_OFFSET" -gt 0 ]]; then
API_PORT="$((API_PORT + ITERATION_OFFSET))"
DASHBOARD_PORT="$((DASHBOARD_PORT + ITERATION_OFFSET))"
echo "ℹ️ [dashboard-high-risk] flake iteration port offset applied: +${ITERATION_OFFSET} (api=${API_PORT}, dashboard=${DASHBOARD_PORT})"
fi
DASHBOARD_DIST_DIR=".next-${ARTIFACT_SUFFIX_ENV}"
fi
port_in_use() {
local port="$1"
local probe_status=0
if e2e_port_in_use "$port"; then
probe_status=0
else
probe_status=$?
fi
if [[ "$probe_status" -eq 0 ]]; then
return 0
fi
if [[ "$probe_status" -eq 1 ]]; then
return 1
fi
echo "❌ [dashboard-high-risk] unable to detect port occupancy for port=$port" >&2
return 2
}
resolve_port() {
local requested_port="$1"
local service_name="$2"
local env_name="$3"
local avoid_port="${4:-}"
local resolved_port="$requested_port"
while true; do
local port_status=0
if [[ -n "$avoid_port" && "$resolved_port" == "$avoid_port" ]]; then
resolved_port="$((resolved_port + 1))"
continue
fi
port_in_use "$resolved_port"
port_status=$?
if [[ "$port_status" -eq 1 ]]; then
break
fi
if [[ "$port_status" -ne 0 ]]; then
return "$port_status"
fi
resolved_port="$((resolved_port + 1))"
done
if [[ "$resolved_port" != "$requested_port" ]]; then
echo "⚠️ [dashboard-high-risk] port occupied, auto shift: ${service_name} ${requested_port} -> ${resolved_port} (or set ${env_name})" >&2
fi
echo "$resolved_port"
}
API_PORT="$(resolve_port "$API_PORT" "API_PORT" "CORTEXPILOT_E2E_API_PORT")"
DASHBOARD_PORT="$(resolve_port "$DASHBOARD_PORT" "DASHBOARD_PORT" "CORTEXPILOT_E2E_DASHBOARD_PORT" "$API_PORT")"
OUT_DIR="$ROOT_DIR/.runtime-cache/test_output/ui_regression"
UI_LOG_EVENT_DIR="$ROOT_DIR/.runtime-cache/test_output/ui_log_events"
LOG_DIR="$ROOT_DIR/.runtime-cache/logs/runtime"
mkdir -p "$OUT_DIR" "$UI_LOG_EVENT_DIR" "$LOG_DIR"
TS="$(date +%Y%m%d_%H%M%S)"
START_EPOCH="$(date +%s)"
API_LOG="$LOG_DIR/e2e_dashboard_high_risk_api_${TS}.log"
UI_LOG="$LOG_DIR/e2e_dashboard_high_risk_dashboard_${TS}.log"
ARTIFACT_SUFFIX="$ARTIFACT_SUFFIX_ENV"
if [[ -n "$ARTIFACT_SUFFIX" ]]; then
REPORT_JSON="$OUT_DIR/dashboard_high_risk_actions_real.${ARTIFACT_SUFFIX}.json"
NETWORK_JSON="$OUT_DIR/dashboard_high_risk_actions_real.network.${ARTIFACT_SUFFIX}.json"
UI_LOG_EVENTS_JSONL="$UI_LOG_EVENT_DIR/dashboard.${ARTIFACT_SUFFIX}.jsonl"
GODMODE_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.god_mode.${ARTIFACT_SUFFIX}.png"
DIFFGATE_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.diff_gate.${ARTIFACT_SUFFIX}.png"
RUNDETAIL_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.run_detail.${ARTIFACT_SUFFIX}.png"
PM_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.pm.${ARTIFACT_SUFFIX}.png"
SEARCH_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.search.${ARTIFACT_SUFFIX}.png"
else
REPORT_JSON="$OUT_DIR/dashboard_high_risk_actions_real.json"
NETWORK_JSON="$OUT_DIR/dashboard_high_risk_actions_real.network.json"
UI_LOG_EVENTS_JSONL="$UI_LOG_EVENT_DIR/dashboard.jsonl"
GODMODE_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.god_mode.png"
DIFFGATE_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.diff_gate.png"
RUNDETAIL_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.run_detail.png"
PM_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.pm.png"
SEARCH_SCREENSHOT="$OUT_DIR/dashboard_high_risk_actions_real.search.png"
fi
API_PID=""
UI_PID=""
WARMUP_SEC="0"
PLAYWRIGHT_SEC="0"
PLAYWRIGHT_TOTAL_ATTEMPTS="0"
PLAYWRIGHT_FAILED_ATTEMPTS="0"
PLAYWRIGHT_FIRST_FAILURE_ATTEMPT="0"
PLAYWRIGHT_FIRST_ATTEMPT_FAILED="0"
PLAYWRIGHT_RETRY_SUCCESS_COUNT="0"
preflight_status="0"
write_report_metadata() {
local category="$1"
local error_message="${2:-}"
local total_sec
total_sec="$(( $(date +%s) - START_EPOCH ))"
PYTHONPATH=apps/orchestrator/src "$PYTHON_BIN" - <<'PY' \
"$REPORT_JSON" "$NETWORK_JSON" "$UI_LOG_EVENTS_JSONL" "$GODMODE_SCREENSHOT" "$DIFFGATE_SCREENSHOT" "$RUNDETAIL_SCREENSHOT" "$PM_SCREENSHOT" "$SEARCH_SCREENSHOT" \
"$TIMEOUT_PROFILE" "$TIMEOUT_PROFILE_SOURCE" "$category" "$error_message" "$WARMUP_SEC" "$PLAYWRIGHT_SEC" "$total_sec" \
"$PLAYWRIGHT_TOTAL_ATTEMPTS" "$PLAYWRIGHT_FAILED_ATTEMPTS" "$PLAYWRIGHT_FIRST_FAILURE_ATTEMPT" "$PLAYWRIGHT_FIRST_ATTEMPT_FAILED" "$PLAYWRIGHT_RETRY_SUCCESS_COUNT"
from __future__ import annotations
import json
import time
from pathlib import Path
import sys
(
report_path,
network_path,
ui_log_events_path,
godmode_shot,
diffgate_shot,
rundetail_shot,
pm_shot,
search_shot,
timeout_profile,
timeout_profile_source,
failure_category,
error_message,
warmup_sec,
playwright_sec,
total_sec,
playwright_total_attempts,
playwright_failed_attempts,
playwright_first_failure_attempt,
playwright_first_attempt_failed,
playwright_retry_success_count,
) = sys.argv[1:20]
path = Path(report_path)
if path.exists():
try:
report = json.loads(path.read_text(encoding="utf-8"))
except Exception:
report = {}
else:
report = {}
report.setdefault("scenario", "dashboard high risk actions real e2e")
report.setdefault("started_at", time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
report.setdefault(
"artifacts",
{
"report_json": report_path,
"network_json": network_path,
"ui_log_events_jsonl": ui_log_events_path,
"god_mode_screenshot": godmode_shot,
"diff_gate_screenshot": diffgate_shot,
"run_detail_screenshot": rundetail_shot,
"pm_screenshot": pm_shot,
"search_screenshot": search_shot,
},
)
report["timeout_profile"] = timeout_profile
report["timeout_profile_source"] = timeout_profile_source
report["failure_category"] = failure_category
report["warmup_sec"] = int(warmup_sec)
report["playwright_sec"] = int(playwright_sec)
report["total_sec"] = int(total_sec)
report["playwright_total_attempts"] = int(playwright_total_attempts)
report["playwright_failed_attempts"] = int(playwright_failed_attempts)
report["playwright_first_failure_attempt"] = int(playwright_first_failure_attempt)
report["playwright_first_attempt_failed"] = bool(int(playwright_first_attempt_failed))
report["playwright_retry_success_count"] = int(playwright_retry_success_count)
if failure_category:
report["status"] = "failed"
if error_message and not str(report.get("error") or "").strip():
report["error"] = error_message
path.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
PY
}
infer_playwright_failure_category() {
local last_status="$1"
if [[ "$last_status" -eq 124 ]]; then
echo "playwright_timeout"
return 0
fi
PYTHONPATH=apps/orchestrator/src "$PYTHON_BIN" - <<'PY' "$REPORT_JSON"
from __future__ import annotations
import json
import re
from pathlib import Path
import sys
path = Path(sys.argv[1])
if not path.exists():
print("assertion_failure")
raise SystemExit(0)
try:
report = json.loads(path.read_text(encoding="utf-8"))
except Exception:
print("assertion_failure")
raise SystemExit(0)
failed_checks = report.get("failed_checks") or []
error_text = str(report.get("error") or "")
if failed_checks:
print("assertion_failure")
elif re.search(r"(timeout|timed out|TimeoutError)", error_text, re.IGNORECASE):
print("playwright_timeout")
else:
print("assertion_failure")
PY
}
classify_failure_category_by_status() {
local status="$1"
local timeout_category="$2"
local failure_category="$3"
if [[ "$status" -eq 124 ]]; then
echo "$timeout_category"
else
echo "$failure_category"
fi
}
wait_http_ok() {
local url="$1"
local timeout_sec="$2"
local auth_token="${3:-}"
PYTHONPATH=apps/orchestrator/src "$PYTHON_BIN" - <<'PY' "$url" "$timeout_sec" "$auth_token"
from __future__ import annotations
import sys
import time
import urllib.request
url, timeout_sec_raw, auth_token = sys.argv[1:4]
timeout_sec = int(timeout_sec_raw)
deadline = time.time() + timeout_sec
headers = {}
if auth_token:
headers["Authorization"] = f"Bearer {auth_token}"
while time.time() < deadline:
try:
req = urllib.request.Request(url, headers=headers, method="GET")
with urllib.request.urlopen(req, timeout=4) as resp:
if int(resp.status) == 200:
raise SystemExit(0)
except Exception:
pass
time.sleep(1)
raise SystemExit(124)
PY
}
wait_for_retry_readiness() {
local timeout_sec="$1"
local check_api="${2:-1}"
local check_ui="${3:-1}"
local started_epoch
started_epoch="$(date +%s)"
while (( $(date +%s) - started_epoch < timeout_sec )); do
local ready=1
if [[ "$check_api" == "1" ]]; then
wait_http_ok "http://$HOST:$API_PORT/health" 2 "" || ready=0
fi
if [[ "$check_ui" == "1" ]]; then
wait_http_ok "http://$HOST:$DASHBOARD_PORT/pm" 2 "$API_TOKEN" || ready=0
fi
if [[ "$ready" -eq 1 ]]; then
return 0
fi
sleep 1
done
return 1
}
wait_pids_exit() {
local timeout_sec="$1"
shift
local started_epoch
started_epoch="$(date +%s)"
while (( $(date +%s) - started_epoch < timeout_sec )); do
local alive=0
for pid in "$@"; do
[[ -z "$pid" ]] && continue
if kill -0 "$pid" >/dev/null 2>&1; then
alive=1
break
fi
done
if [[ "$alive" -eq 0 ]]; then
return 0
fi
sleep 1
done
return 1
}
kill_process_tree() {
local pid="$1"
local children
children="$(pgrep -P "$pid" 2>/dev/null || true)"
if [[ -n "$children" ]]; then
while IFS= read -r child_pid; do
[[ -z "$child_pid" ]] && continue
kill_process_tree "$child_pid"
done <<<"$children"
fi
kill "$pid" >/dev/null 2>&1 || true
}
cleanup() {
if [[ -n "$UI_PID" ]] && kill -0 "$UI_PID" >/dev/null 2>&1; then
kill_process_tree "$UI_PID"
wait "$UI_PID" >/dev/null 2>&1 || true
fi
if [[ -n "$API_PID" ]] && kill -0 "$API_PID" >/dev/null 2>&1; then
kill_process_tree "$API_PID"
wait "$API_PID" >/dev/null 2>&1 || true
fi
e2e_restore_dashboard_generated_files "$ROOT_DIR"
}
trap cleanup EXIT INT TERM
e2e_require_python_venv "$ROOT_DIR"
e2e_prepare_dashboard_generated_file_restore "$ROOT_DIR" "$DASHBOARD_DIST_DIR"
e2e_wait_for_port_free "$API_PORT" "API_PORT" 25
e2e_wait_for_port_free "$DASHBOARD_PORT" "DASHBOARD_PORT" 25
e2e_ensure_dashboard_node_modules "$ROOT_DIR"
if [[ -n "${CORTEXPILOT_E2E_ARTIFACT_SUFFIX:-}" ]]; then
echo "ℹ️ [dashboard-high-risk] skip fast gate under flake iteration context (artifact_suffix=${CORTEXPILOT_E2E_ARTIFACT_SUFFIX})"
else
echo "🚀 [dashboard-high-risk] preflight: fast gate (test:quick)"
run_with_heartbeat_and_timeout "dashboard-high-risk-fast-gate-test-quick" "$FAST_GATE_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
env VITEST_POOL=threads DASHBOARD_VITEST_POOL=threads DESKTOP_VITEST_POOL=threads \
bash scripts/test_quick.sh --no-related
fi
echo "🚀 [dashboard-high-risk] preflight: live external probe (real browser + real provider key/api)"
run_live_preflight_probe() {
run_with_heartbeat_and_timeout "dashboard-high-risk-live-preflight" "$LIVE_PREFLIGHT_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
"$PYTHON_BIN" scripts/e2e_external_web_probe.py \
--url "${CORTEXPILOT_E2E_LIVE_PREFLIGHT_URL:-https://example.com}" \
--timeout-ms "${CORTEXPILOT_E2E_LIVE_PREFLIGHT_NAV_TIMEOUT_MS:-15000}" \
--provider-api-mode "${CORTEXPILOT_E2E_LIVE_PREFLIGHT_PROVIDER_API_MODE:-require}" \
--provider-api-timeout-sec "${CORTEXPILOT_E2E_LIVE_PREFLIGHT_PROVIDER_TIMEOUT_SEC:-12}" \
--hard-timeout-sec "${CORTEXPILOT_E2E_LIVE_PREFLIGHT_HARD_TIMEOUT_SEC:-120}"
}
if ! run_live_preflight_probe; then
preflight_status="$?"
echo "⚠️ [dashboard-high-risk] live preflight first attempt failed, retry once after backoff"
wait_for_retry_readiness 5 0 0 || true
if ! run_live_preflight_probe; then
preflight_status="$?"
echo "❌ [dashboard-high-risk] live preflight failed twice"
preflight_category="$(classify_failure_category_by_status "$preflight_status" "preflight_timeout" "network_probe_failure")"
write_report_metadata "$preflight_category" "live preflight failed twice"
exit 1
fi
fi
DASH_LOCK="$ROOT_DIR/apps/dashboard/${DASHBOARD_DIST_DIR}/dev/lock"
force_clear_dashboard_lock() {
local lock_file="$1"
local holder_pids=""
local holder_status=0
if [[ -f "$lock_file" ]]; then
holder_pids="$(e2e_list_lock_holder_pids "$lock_file")" || holder_status=$?
fi
if (( holder_status != 0 )); then
echo "❌ [dashboard-high-risk] unable to inspect dashboard lock holders: $lock_file" >&2
return 1
fi
if [[ -n "${holder_pids// }" ]]; then
echo "⚠️ [dashboard-high-risk] force clearing dashboard lock holders: ${holder_pids}"
for pid in $holder_pids; do
if [[ "$pid" == "$$" || "$pid" == "$PPID" ]]; then
continue
fi
kill_process_tree "$pid"
done
wait_pids_exit 10 $holder_pids || true
for pid in $holder_pids; do
if [[ "$pid" == "$$" || "$pid" == "$PPID" ]]; then
continue
fi
if kill -0 "$pid" >/dev/null 2>&1; then
kill -KILL "$pid" >/dev/null 2>&1 || true
fi
done
wait_pids_exit 5 $holder_pids || true
fi
if [[ -f "$lock_file" ]]; then
rm -f "$lock_file"
fi
}
if ! e2e_ensure_dashboard_lock_clear "$DASH_LOCK" 20; then
force_clear_dashboard_lock "$DASH_LOCK"
e2e_ensure_dashboard_lock_clear "$DASH_LOCK" 20
fi
echo "🚀 [dashboard-high-risk] starting api: http://$HOST:$API_PORT"
PYTHONPATH=apps/orchestrator/src \
CORTEXPILOT_API_AUTH_REQUIRED=true \
CORTEXPILOT_API_TOKEN="$API_TOKEN" \
CORTEXPILOT_DASHBOARD_PORT="$DASHBOARD_PORT" \
"$PYTHON_BIN" -m cortexpilot_orch.cli serve --host "$HOST" --port "$API_PORT" \
>"$API_LOG" 2>&1 &
API_PID=$!
if ! run_with_heartbeat_and_timeout "dashboard-high-risk-api-startup-wait" "$WARMUP_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
wait_http_ok "http://$HOST:$API_PORT/health" "$WARMUP_TIMEOUT_SEC" ""; then
api_wait_status="$?"
api_category="$(classify_failure_category_by_status "$api_wait_status" "api_startup_timeout" "api_startup_failure")"
write_report_metadata "$api_category" "api startup wait failed"
exit 1
fi
echo "🚀 [dashboard-high-risk] starting dashboard: http://$HOST:$DASHBOARD_PORT"
start_dashboard_dev() {
NEXT_DIST_DIR="$DASHBOARD_DIST_DIR" \
NEXT_PUBLIC_CORTEXPILOT_API_BASE="http://$HOST:$API_PORT" \
NEXT_PUBLIC_CORTEXPILOT_API_TOKEN="$API_TOKEN" \
CORTEXPILOT_API_TOKEN="$API_TOKEN" \
CORTEXPILOT_E2E_API_TOKEN="$API_TOKEN" \
PORT="$DASHBOARD_PORT" \
bash scripts/run_workspace_app.sh dashboard dev -- --hostname "$HOST" --port "$DASHBOARD_PORT" \
>"$UI_LOG" 2>&1 &
UI_PID=$!
}
restart_runtime_stack_for_retry() {
echo "⚠️ [dashboard-high-risk] retry readiness failed, restarting api/dashboard stack"
cleanup
API_PID=""
UI_PID=""
echo "🚀 [dashboard-high-risk] restarting api: http://$HOST:$API_PORT"
PYTHONPATH=apps/orchestrator/src \
CORTEXPILOT_API_AUTH_REQUIRED=true \
CORTEXPILOT_API_TOKEN="$API_TOKEN" \
CORTEXPILOT_DASHBOARD_PORT="$DASHBOARD_PORT" \
"$PYTHON_BIN" -m cortexpilot_orch.cli serve --host "$HOST" --port "$API_PORT" \
>"$API_LOG" 2>&1 &
API_PID=$!
run_with_heartbeat_and_timeout "dashboard-high-risk-api-restart-wait" "$WARMUP_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
wait_http_ok "http://$HOST:$API_PORT/health" "$WARMUP_TIMEOUT_SEC" ""
if ! e2e_ensure_dashboard_lock_clear "$DASH_LOCK" 20; then
force_clear_dashboard_lock "$DASH_LOCK"
e2e_ensure_dashboard_lock_clear "$DASH_LOCK" 20
fi
if ! e2e_start_dashboard_dev_with_retry "$DASH_LOCK" "$UI_LOG" start_dashboard_dev 2 "$DASHBOARD_PORT"; then
echo "❌ [dashboard-high-risk] dashboard dev failed to restart before retry"
tail -n 80 "$UI_LOG" || true
return 1
fi
run_with_heartbeat_and_timeout "dashboard-high-risk-ui-restart-wait" "$WARMUP_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
wait_http_ok "http://$HOST:$DASHBOARD_PORT/pm" "$WARMUP_TIMEOUT_SEC" "$API_TOKEN"
}
if ! e2e_start_dashboard_dev_with_retry "$DASH_LOCK" "$UI_LOG" start_dashboard_dev 2 "$DASHBOARD_PORT"; then
echo "❌ dashboard dev failed to start before playwright checks"
tail -n 80 "$UI_LOG" || true
write_report_metadata "ui_startup_failure" "dashboard dev failed to start before playwright checks"
exit 1
fi
if ! run_with_heartbeat_and_timeout "dashboard-high-risk-ui-startup-wait" "$WARMUP_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
wait_http_ok "http://$HOST:$DASHBOARD_PORT/pm" "$WARMUP_TIMEOUT_SEC" "$API_TOKEN"; then
ui_wait_status="$?"
ui_category="$(classify_failure_category_by_status "$ui_wait_status" "ui_startup_timeout" "ui_startup_failure")"
write_report_metadata "$ui_category" "dashboard ui startup wait failed"
exit 1
fi
echo "ℹ️ [dashboard-high-risk] timeout profile=${TIMEOUT_PROFILE} (source=${TIMEOUT_PROFILE_SOURCE})"
echo "ℹ️ [dashboard-high-risk] timeouts(sec): fast_gate=${FAST_GATE_TIMEOUT_SEC}, live_preflight=${LIVE_PREFLIGHT_TIMEOUT_SEC}, warmup=${WARMUP_TIMEOUT_SEC}, playwright=${PLAYWRIGHT_TIMEOUT_SEC}; attempts=${PLAYWRIGHT_ATTEMPTS}; retry_backoff=${PLAYWRIGHT_RETRY_BACKOFF_SEC}s"
run_dashboard_light_warmup() {
PYTHONPATH=apps/orchestrator/src "$PYTHON_BIN" - <<'PY' \
"$HOST" "$API_PORT" "$DASHBOARD_PORT" "$API_TOKEN"
from __future__ import annotations
import re
import subprocess
import sys
import time
from urllib.parse import urljoin
host, api_port, dashboard_port, api_token = sys.argv[1:5]
api_base = f"http://{host}:{api_port}"
dash_base = f"http://localhost:{dashboard_port}"
def fetch(url: str, headers: dict[str, str] | None = None, timeout: int = 6) -> tuple[int, str]:
cmd = ["curl", "-sS", "-L", "--max-time", str(timeout), "-w", "\n%{http_code}"]
for key, value in (headers or {}).items():
cmd.extend(["-H", f"{key}: {value}"])
cmd.append(url)
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
payload = result.stdout.rsplit("\n", 1)
if len(payload) != 2:
raise RuntimeError(f"unexpected curl payload for {url}")
body, status = payload
return int(status.strip()), body
def fetch_with_retry(url: str, headers: dict[str, str] | None = None, timeout: int = 6, attempts: int = 4) -> tuple[int, str]:
last_error: Exception | None = None
for attempt in range(1, attempts + 1):
try:
return fetch(url, headers=headers, timeout=timeout)
except Exception as exc:
last_error = exc
if attempt >= attempts:
break
time.sleep(attempt)
raise last_error or RuntimeError(f"fetch failed for {url}")
def wait_ok(url: str, timeout_sec: int, headers: dict[str, str] | None = None) -> str:
deadline = time.time() + timeout_sec
while time.time() < deadline:
try:
status, body = fetch_with_retry(url, headers=headers)
if status == 200:
return body
except Exception:
pass
time.sleep(1)
raise RuntimeError(f"warmup timeout waiting for {url}")
api_headers = {"Authorization": f"Bearer {api_token}"}
wait_ok(f"{api_base}/health", timeout_sec=90)
pm_html = wait_ok(f"{dash_base}/pm", timeout_sec=120, headers=api_headers)
wait_ok(f"{dash_base}/god-mode", timeout_sec=120, headers=api_headers)
asset_paths = re.findall(r'["\'](/_next/static/[^"\']+)["\']', pm_html)
unique_assets: list[str] = []
for path in asset_paths:
if path not in unique_assets:
unique_assets.append(path)
if not unique_assets:
raise RuntimeError("warmup could not discover /_next/static assets from /pm")
for path in unique_assets[:3]:
asset_url = urljoin(dash_base, path)
asset_deadline = time.time() + 45
status = 0
while time.time() < asset_deadline:
try:
status, _ = fetch_with_retry(asset_url, headers=api_headers, timeout=8)
if status < 400:
break
except Exception:
pass
time.sleep(2)
if status >= 400:
raise RuntimeError(f"warmup asset not ready: {path} status={status}")
PY
}
echo "🔥 [dashboard-high-risk] warmup: API + dashboard key resources"
WARMUP_STARTED_AT="$(date +%s)"
if ! run_with_heartbeat_and_timeout "dashboard-high-risk-warmup" "$WARMUP_TIMEOUT_SEC" "$HEARTBEAT_SEC" -- \
run_dashboard_light_warmup; then
warmup_status="$?"
WARMUP_SEC="$(( $(date +%s) - WARMUP_STARTED_AT ))"
warmup_category="$(classify_failure_category_by_status "$warmup_status" "warmup_timeout" "warmup_dependency_failure")"
write_report_metadata "$warmup_category" "warmup stage failed"
exit 1
fi
WARMUP_SEC="$(( $(date +%s) - WARMUP_STARTED_AT ))"
run_playwright_scenario() {
PYTHONPATH=apps/orchestrator/src "$PYTHON_BIN" - <<'PY' \
"$HOST" "$API_PORT" "$DASHBOARD_PORT" "$API_TOKEN" "$REPORT_JSON" "$NETWORK_JSON" "$GODMODE_SCREENSHOT" "$DIFFGATE_SCREENSHOT" "$RUNDETAIL_SCREENSHOT" "$PM_SCREENSHOT" "$SEARCH_SCREENSHOT"
from __future__ import annotations
import json
import re
import subprocess
import sys
import time
import traceback
from pathlib import Path
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
from playwright.sync_api import sync_playwright
from cortexpilot_orch.store import run_store
host, api_port, dashboard_port, api_token, report_path, network_path, godmode_shot, diffgate_shot, rundetail_shot, pm_shot, search_shot = sys.argv[1:12]
api_base = f"http://{host}:{api_port}"
dash_base = f"http://{host}:{dashboard_port}"
browser_dash_base = f"http://127.0.0.1:{dashboard_port}"
def wait_http_ok(url: str, timeout_sec: int = 120) -> None:
started = time.time()
while time.time() - started < timeout_sec:
try:
result = subprocess.run(
["curl", "-sS", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "4", url],
capture_output=True,
text=True,
check=False,
)
if result.returncode == 0 and result.stdout.strip() == "200":
return
except Exception:
pass
time.sleep(1)
raise RuntimeError(f"timeout waiting for {url}")
def fetch_json(url: str, headers: dict[str, str] | None = None, timeout: int = 6):
cmd = ["curl", "-sS", "--max-time", str(timeout)]
for key, value in (headers or {}).items():
cmd.extend(["-H", f"{key}: {value}"])
cmd.append(url)
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
def wait_pending_approval(url: str, target_run_id: str, auth_token: str, timeout_sec: int = 90) -> None:
started = time.time()
headers = {"Authorization": f"Bearer {auth_token}", "x-cortexpilot-role": "OWNER"}
while time.time() - started < timeout_sec:
try:
payload = fetch_json(url, headers=headers, timeout=6)
if isinstance(payload, list) and any(
isinstance(item, dict) and str(item.get("run_id") or "").strip() == target_run_id
for item in payload
):
return
except Exception:
pass
time.sleep(1)
raise RuntimeError(f"pending approval not visible via api for run_id={target_run_id}")
run_id = f"e2e_dashboard_risk_{int(time.time())}"
manual_run_id = f"e2e_dashboard_manual_{int(time.time())}"
search_run_id = f"e2e_dashboard_search_{int(time.time())}"
live_run_id = f"e2e_dashboard_live_{int(time.time())}"
empty_diff_run_id = f"e2e_dashboard_empty_diff_{int(time.time())}"
invalid_diff_run_id = f"e2e_dashboard_invalid_diff_{int(time.time())}"
task_id = f"task_{run_id}"
now_iso = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
diff_text = """diff --git a/apps/dashboard/README.md b/apps/dashboard/README.md
index 1111111..2222222 100644
--- a/apps/dashboard/README.md
+++ b/apps/dashboard/README.md
@@ -1,3 +1,4 @@
# Dashboard
+- e2e high risk seed
"""
def seed_run(
seed_run_id: str,
seed_task_id: str,
seed_diff: str | None = None,
*,
status: str = "FAILURE",
failure_reason: str = "diff gate violated",
) -> None:
run_store.create_run_dir(seed_run_id)
run_store.write_manifest(
seed_run_id,
{
"run_id": seed_run_id,
"task_id": seed_task_id,
"status": status,
"failure_reason": failure_reason,
"created_at": now_iso,
"start_ts": now_iso,
"end_ts": now_iso,
},
)
run_store.write_contract(
seed_run_id,
{
"task_id": seed_task_id,
"objective": "dashboard high risk actions real e2e seed",
"allowed_paths": ["apps/dashboard"],
"search_queries": ["dashboard p0 real e2e"],
"rollback": {"strategy": "git_reset_hard", "baseline_ref": "HEAD"},
},
)
if seed_diff is not None:
run_store.write_diff(seed_run_id, seed_diff)
seed_run(run_id, task_id, diff_text, status="RUNNING", failure_reason="")
seed_run(search_run_id, f"task_{search_run_id}")
seed_run(live_run_id, f"task_{live_run_id}", diff_text, status="RUNNING", failure_reason="")
seed_run(empty_diff_run_id, f"task_{empty_diff_run_id}", "")
seed_run(invalid_diff_run_id, f"task_{invalid_diff_run_id}", "### invalid-diff-payload ###")
run_store.append_event(
run_id,
{
"ts": now_iso,
"level": "WARN",
"event": "DIFF_GATE_RESULT",
"run_id": run_id,
"context": {
"ok": False,
"violations": ["apps/dashboard/README.md"],
"changed_files": ["apps/dashboard/README.md"],
"allowed_paths": ["apps/dashboard"],
},
},
)
run_store.append_event(
manual_run_id,
{
"ts": now_iso,
"level": "WARN",
"event": "HUMAN_APPROVAL_REQUIRED",
"run_id": manual_run_id,
"context": {
"reason": ["manual approval path seed"],
"actions": ["approve by run id input"],
"verify_steps": ["god-mode manual approve"],
"resume_step": "execute",
},
},
)
run_store.append_event(
run_id,
{
"ts": now_iso,
"level": "WARN",
"event": "HUMAN_APPROVAL_REQUIRED",
"run_id": run_id,
"context": {
"reason": ["manual e2e verification pending"],
"actions": ["confirm dashboard high-risk controls"],
"verify_steps": ["god-mode approve", "diff-gate rollback/reject", "run replay"],
"resume_step": "execute",
},
},
)
wait_http_ok(f"{api_base}/health", 90)
wait_http_ok(f"{dash_base}/god-mode", 180)
wait_pending_approval(f"{api_base}/api/god-mode/pending", run_id, api_token, 90)
wait_pending_approval(f"{api_base}/api/god-mode/pending", manual_run_id, api_token, 90)
report = {
"scenario": "dashboard high risk actions real e2e",
"run_id": run_id,
"manual_run_id": manual_run_id,
"started_at": now_iso,
"api_base_url": api_base,
"dashboard_base_url": dash_base,
"checks": [],
"failed_checks": [],
"artifacts": {
"report_json": report_path,
"network_json": network_path,
"god_mode_screenshot": godmode_shot,
"diff_gate_screenshot": diffgate_shot,
"run_detail_screenshot": rundetail_shot,
"pm_screenshot": pm_shot,
"search_screenshot": search_shot,
},
"status": "failed",
"error": "",
"stage": "",
}
network_events: list[dict] = []
console_events: list[str] = []
screenshot_warnings: list[str] = []
current_stage = "bootstrap"
def set_stage(name: str) -> None:
global current_stage
current_stage = name
report["stage"] = name
def safe_screenshot(page, path: str, label: str) -> None:
try:
page.screenshot(path=path, full_page=True)
except Exception as exc:
screenshot_warnings.append(f"{label}: {exc}")
def goto_with_retry(page, url: str, wait_until: str = "domcontentloaded", timeout: int = 30000, attempts: int = 3):
last_error: Exception | None = None
current_page = page
for attempt in range(1, attempts + 1):
try:
current_page.goto("about:blank", wait_until="load", timeout=5000)
except Exception:
pass
try:
current_page.goto(url, wait_until=wait_until, timeout=timeout)
return current_page
except Exception as exc: # noqa: BLE001
last_error = exc
if attempt >= attempts:
break
try:
current_page.close()
except Exception:
pass
current_page = page.context.new_page()
current_page.on("response", on_response)
current_page.on("console", lambda msg: console_events.append(msg.text or ""))
time.sleep(attempt * 2)
raise last_error or RuntimeError(f"page.goto failed: {url}")
try:
with sync_playwright() as p:
set_stage("launch_browser")
browser = p.chromium.launch(args=["--no-proxy-server"])
context = browser.new_context(
viewport={"width": 1440, "height": 900},
extra_http_headers={"x-cortexpilot-role": "OWNER"},
)
page = context.new_page()
def on_response(resp):
if resp.url.startswith(f"{api_base}/api/"):
post_data = ""
try:
post_data = resp.request.post_data or ""
except Exception:
post_data = ""
network_events.append(
{
"ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"method": resp.request.method,
"url": resp.url,
"status": resp.status,
"page_url": page.url,
"post_data": post_data[:4000],
}
)
page.on("response", on_response)
page.on("console", lambda msg: console_events.append(msg.text or ""))
set_stage("pm_open")
page = goto_with_retry(page, f"{browser_dash_base}/pm", wait_until="domcontentloaded")
cta_btn = page.locator('[data-testid="pm-context-primary-action"]').first
cta_btn.wait_for(timeout=30000)
pm_input = page.locator('textarea[aria-label="PM 对话输入框"]').first
pm_input.fill("")
cta_btn.click()
pm_input.wait_for(timeout=30000)
pm_value = (pm_input.input_value() or "").strip()
cta_label = (cta_btn.text_content() or "").strip()
pm_cta_prefilled = bool(pm_value) or bool(cta_label)
safe_screenshot(page, pm_shot, "pm")
set_stage("search_promote")
page = goto_with_retry(page, f"{browser_dash_base}/search", wait_until="networkidle")
run_id_input = page.get_by_label("运行 ID")
run_id_input.fill(search_run_id)
promote_btn = page.get_by_test_id("search-promote-button")
promote_btn.wait_for(timeout=15000)
page.wait_for_function(
"""() => {
const btn = document.querySelector('[data-testid="search-promote-button"]');
return !!btn && !btn.disabled;
}""",
timeout=15000,
)
run_id_input.press("Tab")
promote_btn.click(timeout=10000)
try:
page.wait_for_function(
"""() => {
const status = document.querySelector('[data-testid="search-promote-status-message"]');
const evidence = document.querySelector('[data-testid="search-evidence-bundle-card"]');
const statusText = (status?.textContent || "").trim();
return statusText.includes("已提升为 EvidenceBundle") || !!evidence;
}""",
timeout=5000,
)
except PlaywrightTimeoutError:
# Promote success is ultimately asserted from captured API network events.
pass
safe_screenshot(page, search_shot, "search")
set_stage("god_mode")
page = goto_with_retry(page, f"{browser_dash_base}/god-mode", wait_until="domcontentloaded")
queue_item_visible = False
continue_to_confirm_visible = False
for attempt in range(1, 9):
queue_item = page.locator(".god-mode-item", has_text=run_id).first
try:
queue_item.wait_for(timeout=8000)
queue_item_visible = True
break
except PlaywrightTimeoutError:
try:
page.get_by_role("button", name=re.compile(r"刷新待审批|刷新中", re.IGNORECASE)).first.click(timeout=5000)
except Exception:
pass
page.wait_for_timeout(1000)
if attempt % 2 == 0:
page = goto_with_retry(page, f"{browser_dash_base}/god-mode", wait_until="domcontentloaded")
if queue_item_visible:
queue_item.get_by_role("button", name="我已完成,继续执行").click()
confirm_btn = page.get_by_role("button", name="确认批准")
confirm_btn.wait_for(timeout=15000)
continue_to_confirm_visible = confirm_btn.is_visible()