-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlauncher.go
More file actions
1307 lines (1233 loc) · 47.5 KB
/
Copy pathlauncher.go
File metadata and controls
1307 lines (1233 loc) · 47.5 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
package main
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
func shouldRunLauncher(args []string) bool {
if binaryRole == "launcher" {
return true
}
if len(args) > 0 {
base := strings.ToLower(filepath.Base(args[0]))
if strings.Contains(base, "launcher") {
return true
}
}
for _, arg := range args[1:] {
if arg == "--launcher" {
return true
}
}
return false
}
func runLauncher(args []string) error {
settings := loadSettings()
options := parseLaunchRequest(args)
debugPort := options.debugPort
if debugPort == 0 {
debugPort = 9229
}
helperPort := options.helperPort
if helperPort == 0 {
helperPort = 57321
}
appPath := resolveLaunchableCodexApp(options.appPath)
if appPath == "" {
appPath = resolveLaunchableCodexApp(settings.CodexAppPath)
}
if appPath == "" {
message := "未找到可直接启动的 ChatGPT 桌面应用;请安装 ChatGPT 桌面应用,或在管理工具中选择 ChatGPT.app / ChatGPT.exe"
if runtime.GOOS == "windows" {
message = "未找到 Codex 与 ChatGPT 合并后的 Windows 新版应用;请从 Microsoft Store 更新/安装 ChatGPT,或选择 WindowsApps 外的 ChatGPT.exe / Codex.exe"
}
err := errors.New(message)
writeLaunchFailureStatus("启动 ChatGPT Codex 失败:"+err.Error(), debugPort, helperPort, nil)
appendDiagnosticLog("launcher.codex_app_missing", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "error": err.Error()})
return err
}
var restartLock fileLockGuard
releaseRestartLock := func() {
if restartLock != nil {
_ = restartLock.release()
restartLock = nil
}
}
if options.restart && currentRuntimeGOOS() == "windows" {
lock, acquired, lockErr := acquireLauncherRestartLock()
if lockErr != nil {
return fmt.Errorf("获取 Windows 重启互斥锁失败:%w", lockErr)
}
if !acquired {
appendDiagnosticLog("launcher.restart_coalesced", map[string]any{"debug_port": debugPort, "helper_port": helperPort})
return nil
}
restartLock = lock
defer releaseRestartLock()
}
restartPrepared := false
instanceLock, acquired, err := acquireLauncherSingleInstanceLock(debugPort)
if err != nil {
writeLaunchFailureStatus("启动 ChatGPT Codex 单实例锁失败:"+err.Error(), debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.guard_failed", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort, "error": err.Error()})
return err
}
if !acquired {
if options.restart {
appendDiagnosticLog("launcher.restart_existing_launcher", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort})
if err := prepareFullRestartBeforeLaunch(appPath, debugPort, helperPort, true); err != nil {
message := "重启 ChatGPT Codex 失败:" + err.Error()
writeLaunchFailureStatus(message, debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.restart_existing_launcher_failed", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort, "error": err.Error()})
return errors.New(message)
}
restartPrepared = true
instanceLock, acquired, err = waitForLauncherSingleInstanceLock(debugPort, 15*time.Second)
if err != nil {
message := "重启 ChatGPT Codex 失败:重新获取单实例锁失败:" + err.Error()
writeLaunchFailureStatus(message, debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.restart_guard_reacquire_failed", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort, "error": err.Error()})
return errors.New(message)
}
if !acquired {
message := fmt.Sprintf("重启 ChatGPT Codex 失败:旧后端未退出,端口 %d 仍被占用", launcherGuardPort)
writeLaunchFailureStatus(message, debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.restart_guard_still_busy", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort})
return errors.New(message)
}
} else {
appendDiagnosticLog("launcher.already_running", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort})
return nil
}
}
if instanceLock == nil {
err := errors.New("ChatGPT Codex 单实例锁不可用")
writeLaunchFailureStatus("启动 ChatGPT Codex 失败:"+err.Error(), debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.guard_missing", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort, "error": err.Error()})
return err
}
defer instanceLock.release()
if fallbackLockPath := instanceLock.fallbackPath(); fallbackLockPath != "" {
appendDiagnosticLog("launcher.guard_fallback", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "requested_guard_port": launcherGuardPort, "fallback_lock_path": fallbackLockPath})
}
if options.restart && !restartPrepared {
if err := prepareFullRestartBeforeLaunch(appPath, debugPort, helperPort, false); err != nil {
message := "重启 ChatGPT Codex 失败:" + err.Error()
writeLaunchFailureStatus(message, debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.restart_prepare_failed", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "guard_port": launcherGuardPort, "error": err.Error()})
return errors.New(message)
}
}
runtimeState := &launcherRuntime{settings: settings, debugPort: debugPort, codexAppPath: appPath}
if shouldQuitRunningCodexBeforeLaunch(appPath, debugPort, options.restart) {
appendDiagnosticLog("launcher.quit_existing_codex", map[string]any{"codex_app": appPath, "debug_port": debugPort, "restart": options.restart})
if err := quitMacOSApp(appPath); err != nil {
appendDiagnosticLog("launcher.quit_existing_codex_failed", map[string]any{"codex_app": appPath, "error": err.Error()})
}
if !waitForMacOSAppExit(appPath, 8*time.Second) {
appendDiagnosticLog("launcher.force_kill_existing_codex", map[string]any{"codex_app": appPath})
_ = forceKillMacOSApp(appPath)
_ = waitForMacOSAppExit(appPath, 4*time.Second)
}
if activeRelayNeedsLocalProxy(settings) {
waitForTCPPortFree(localRelayProxyPort, 5*time.Second)
}
}
if settings.ProviderSync {
result := runProviderSyncWithHeldLauncherGuard(codexHomeDir())
repairResult := repairCodexConfig(codexHomeDir(), codexConfigRepairOptions{Plugins: true})
appendDiagnosticLog("provider_sync."+result.Status, map[string]any{
"targetProvider": result.TargetProvider,
"changedSessionFiles": result.ChangedSessionFiles,
"sqliteRowsUpdated": result.SQLiteRowsUpdated,
"message": result.Message,
})
appendDiagnosticLog("codex_plugin_repair."+repairResult.Status, map[string]any{
"pluginCount": repairResult.PluginCount,
"marketplaceCount": repairResult.MarketplaceCount,
"changed": repairResult.PluginConfigChanged,
"message": repairResult.Message,
})
}
if helperNeeded(settings) {
if err := runtimeState.startHelper(helperPort); err != nil {
failure := launchStatus{
Status: "failed",
Message: "启动 ChatGPT Codex helper 失败:" + err.Error(),
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: &appPath,
}
_ = atomicWriteJSON(latestStatusPath(), failure)
appendDiagnosticLog("launcher.helper_failed", map[string]any{"helper_port": helperPort, "error": err.Error()})
return err
}
defer runtimeState.shutdownHelper()
}
if activeRelayNeedsLocalProxy(settings) {
if err := runtimeState.startRelayProxy(localRelayProxyPort); err != nil {
failure := launchStatus{
Status: "failed",
Message: "启动 ChatGPT Codex 本地中转代理失败:" + err.Error(),
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: &appPath,
}
_ = atomicWriteJSON(latestStatusPath(), failure)
appendDiagnosticLog("launcher.relay_proxy_failed", map[string]any{"port": localRelayProxyPort, "error": err.Error()})
return err
}
defer runtimeState.shutdownRelayProxy()
}
if settings.ComputerUseGuardEnabled {
result, guardErr := ensureComputerUseGuardConfig(codexHomeDir())
if guardErr != nil {
appendDiagnosticLog("computer_use_guard.pre_launch_failed", map[string]any{"error": guardErr.Error()})
} else {
appendDiagnosticLog("computer_use_guard.pre_launch_ok", map[string]any{"changed": result.Changed, "notify_exe": result.NotifyExe})
}
}
status := launchStatus{
Status: "starting",
Message: "ChatGPT Codex launcher starting ChatGPT and waiting for injection.",
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: &appPath,
}
_ = atomicWriteJSON(latestStatusPath(), status)
appendDiagnosticLog("launcher.starting", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "codex_app": appPath, "enhancements": settings.Enhancements})
launch, err := startCodexApp(appPath, debugPort, settings)
if err != nil {
detail := launchFailureDetail(appPath, debugPort, helperPort, err)
failure := launchStatus{
Status: "failed",
Message: "启动 ChatGPT Codex 失败:" + err.Error(),
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: &appPath,
Detail: detail,
}
_ = atomicWriteJSON(latestStatusPath(), failure)
appendDiagnosticLog("launcher.codex_start_failed", detail)
return err
}
ready := launchStatus{
Status: "running",
Message: "ChatGPT Codex launcher ready.",
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: &appPath,
}
if settings.Enhancements {
if err := runtimeState.retryInjection(helperPort); err != nil {
ready.Status = "degraded"
ready.Message = "ChatGPT 已启动,但 ChatGPT Codex 增强菜单暂时注入失败;中转代理会继续运行,并在后台重试注入:" + err.Error()
appendDiagnosticLog("launcher.inject_degraded", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "error": err.Error()})
}
go runtimeState.bridgeWatchdog(helperPort)
}
if settings.ComputerUseGuardEnabled {
go startComputerUseGuardWatchdog(codexHomeDir())
}
_ = atomicWriteJSON(latestStatusPath(), ready)
appendDiagnosticLog("launcher.ready", map[string]any{"debug_port": debugPort, "helper_port": helperPort, "codex_app": appPath, "launch": launch.logPayload()})
releaseRestartLock()
return reapLauncherChild(launch, appPath, debugPort, helperPort)
}
func writeLaunchFailureStatus(message string, debugPort, helperPort uint16, appPath *string) {
failure := launchStatus{
Status: "failed",
Message: message,
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: appPath,
}
if appPath != nil {
failure.Detail = launchFailureDetail(*appPath, debugPort, helperPort, errors.New(message))
}
_ = atomicWriteJSON(latestStatusPath(), failure)
}
func writeLaunchRestartingStatus(message string, debugPort, helperPort uint16, appPath *string) {
status := launchStatus{
Status: "restarting",
Message: message,
StartedAtMS: uint64(time.Now().UnixMilli()),
DebugPort: &debugPort,
HelperPort: &helperPort,
CodexApp: appPath,
}
_ = atomicWriteJSON(latestStatusPath(), status)
}
func prepareFullRestartBeforeLaunch(appPath string, debugPort, helperPort uint16, waitForGuard bool) error {
writeLaunchRestartingStatus("正在关闭旧 ChatGPT Codex 后端并释放端口。", debugPort, helperPort, &appPath)
appendDiagnosticLog("launcher.restart_prepare", map[string]any{
"codex_app": appPath,
"debug_port": debugPort,
"helper_port": helperPort,
"relay_port": localRelayProxyPort,
"guard_port": launcherGuardPort,
"wait_for_guard": waitForGuard,
})
windowsRestart := currentRuntimeGOOS() == "windows"
closeViaCDP := cdpTargetsAvailable(debugPort, 800*time.Millisecond)
if windowsRestart {
chatGPTTargetAvailable := chatGPTCDPTargetAvailable(debugPort, 800*time.Millisecond)
ownedByTargetApp := windowsCDPPortOwnedByTargetApp(debugPort, appPath)
closeViaCDP = chatGPTTargetAvailable && ownedByTargetApp
if chatGPTTargetAvailable && !ownedByTargetApp {
appendDiagnosticLog("launcher.restart_skip_unowned_cdp", map[string]any{"debug_port": debugPort})
}
}
if closeViaCDP {
appendDiagnosticLog("launcher.restart_close_cdp", map[string]any{"debug_port": debugPort, "codex_app": appPath})
shutdownTimeout := 10 * time.Second
if windowsRestart {
shutdownTimeout = 4 * time.Second
}
if err := requestCodexShutdownViaCDP(debugPort, shutdownTimeout); err != nil {
if !windowsRestart {
return fmt.Errorf("关闭旧 ChatGPT 调试端口失败:%w", err)
}
appendDiagnosticLog("launcher.restart_close_cdp_failed", map[string]any{
"debug_port": debugPort,
"codex_app": appPath,
"error": err.Error(),
})
}
}
if runtime.GOOS == "darwin" && macOSAppRunning(appPath) {
appendDiagnosticLog("launcher.restart_quit_macos_app", map[string]any{"codex_app": appPath})
if err := quitMacOSApp(appPath); err != nil {
appendDiagnosticLog("launcher.restart_quit_macos_app_failed", map[string]any{"codex_app": appPath, "error": err.Error()})
}
if !waitForMacOSAppExit(appPath, 8*time.Second) {
appendDiagnosticLog("launcher.restart_force_kill_macos_app", map[string]any{"codex_app": appPath})
_ = forceKillMacOSApp(appPath)
if !waitForMacOSAppExit(appPath, 4*time.Second) {
return errors.New("旧 ChatGPT 应用未退出")
}
}
}
if windowsRestart {
if err := stopWindowsTargetsBeforeRestart(appPath, debugPort, 8*time.Second); err != nil {
return err
}
}
if err := waitForRequiredDebugPortFree(debugPort, 12*time.Second); err != nil {
return err
}
if err := waitForRequiredTCPPortFree(helperPort, "Helper", 12*time.Second); err != nil {
return err
}
if err := waitForRequiredTCPPortFree(localRelayProxyPort, "本地中转代理", 12*time.Second); err != nil {
return err
}
if waitForGuard && !windowsRestart {
if err := waitForRequiredTCPPortFree(launcherGuardPort, "ChatGPT Codex 后端单实例", 12*time.Second); err != nil {
return err
}
}
writeLaunchRestartingStatus("旧 ChatGPT Codex 后端已退出,正在启动新实例。", debugPort, helperPort, &appPath)
return nil
}
func stopWindowsTargetsBeforeRestart(appPath string, debugPort uint16, timeout time.Duration) error {
processIDs, err := terminateWindowsRestartTargets(appPath, debugPort, timeout)
if len(processIDs) > 0 {
appendDiagnosticLog("launcher.restart_windows_targets_stopped", map[string]any{
"process_ids": processIDs,
"codex_app": appPath,
})
}
if err != nil {
return fmt.Errorf("关闭旧 Windows ChatGPT/Codex 进程失败:%w", err)
}
return nil
}
func waitForRequiredTCPPortFree(port uint16, label string, timeout time.Duration) error {
if port == 0 {
return nil
}
if waitForTCPPortFree(port, timeout) {
return nil
}
return fmt.Errorf("%s端口 %d 未释放,可能仍被旧后端占用", label, port)
}
type debugPortReleaseState struct {
BindError error
Accepting bool
ListenerPIDs []uint32
ListenerPIDKnown bool
BoundPIDs []uint32
BoundPIDKnown bool
}
func waitForRequiredDebugPortFree(port uint16, timeout time.Duration) error {
if port == 0 {
return nil
}
if currentRuntimeGOOS() != "windows" {
return waitForRequiredTCPPortFree(port, "调试", timeout)
}
free, state := waitForDebugPortFree(port, timeout)
if free {
return nil
}
detail := map[string]any{
"port": port,
"accepting": state.Accepting,
"listener_pid_known": state.ListenerPIDKnown,
"listener_pids": state.ListenerPIDs,
"bound_pid_known": state.BoundPIDKnown,
"bound_pids": state.BoundPIDs,
}
if state.BindError != nil {
detail["bind_error"] = state.BindError.Error()
}
appendDiagnosticLog("launcher.restart_debug_port_unavailable", detail)
if len(state.ListenerPIDs) > 0 {
return fmt.Errorf("调试端口 %d 仍有 Windows LISTENING 进程 %v,请退出占用该端口的应用后重试", port, state.ListenerPIDs)
}
if state.Accepting {
return fmt.Errorf("调试端口 %d 仍在接受连接,但 Windows 未能取得监听进程 PID,请退出占用该端口的应用后重试", port)
}
if len(state.BoundPIDs) > 0 {
return fmt.Errorf("调试端口 %d 仍被 Windows BOUND 进程 %v 占用(尚未 LISTENING),请退出占用该端口的应用后重试", port, state.BoundPIDs)
}
if state.BindError != nil {
return fmt.Errorf("调试端口 %d 没有监听进程,但 Windows 无法复用该端口:%v;该端口可能被系统保留或权限策略限制,请改用其他调试端口", port, state.BindError)
}
return fmt.Errorf("调试端口 %d 状态未知,Windows 暂时无法确认该端口可复用", port)
}
func waitForDebugPortFree(port uint16, timeout time.Duration) (bool, debugPortReleaseState) {
deadline := time.Now().Add(timeout)
state := debugPortReleaseState{}
for {
state.BindError = probeTCPPortBind(port)
state.Accepting = tcpPortAccepting(port)
state.ListenerPIDs, state.BoundPIDs, state.ListenerPIDKnown, state.BoundPIDKnown = windowsTCPPortStatus(port)
if debugPortConsideredFree(currentRuntimeGOOS(), state) {
return true, state
}
if time.Now().After(deadline) {
return false, state
}
time.Sleep(150 * time.Millisecond)
}
}
func debugPortConsideredFree(goos string, state debugPortReleaseState) bool {
if state.BindError != nil {
return false
}
if goos != "windows" {
return true
}
return !state.Accepting && len(state.ListenerPIDs) == 0 && len(state.BoundPIDs) == 0
}
func probeTCPPortBind(port uint16) error {
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return err
}
_ = listener.Close()
return nil
}
func waitForLauncherSingleInstanceLock(debugPort uint16, timeout time.Duration) (launcherSingleInstanceLock, bool, error) {
deadline := time.Now().Add(timeout)
for {
lock, acquired, err := acquireLauncherSingleInstanceLock(debugPort)
if err != nil || acquired || time.Now().After(deadline) {
return lock, acquired, err
}
time.Sleep(200 * time.Millisecond)
}
}
func parseLaunchRequest(args []string) launchRequest {
var request launchRequest
for i := 0; i < len(args); i++ {
switch args[i] {
case "--app-path":
if i+1 < len(args) {
request.appPath = strings.TrimSpace(args[i+1])
i++
}
case "--debug-port":
if i+1 < len(args) {
if value, err := strconv.ParseUint(args[i+1], 10, 16); err == nil {
request.debugPort = uint16(value)
}
i++
}
case "--helper-port":
if i+1 < len(args) {
if value, err := strconv.ParseUint(args[i+1], 10, 16); err == nil {
request.helperPort = uint16(value)
}
i++
}
case "--restart":
request.restart = true
}
}
return request
}
func buildCodexLaunchCommand(appPath string, debugPort uint16, extraArgs []string) []string {
return buildCodexLaunchCommandForSettings(appPath, debugPort, extraArgs, backendSettings{})
}
func buildCodexLaunchCommandForSettings(appPath string, debugPort uint16, extraArgs []string, settings backendSettings) []string {
args := buildCodexArgumentsForSettings(debugPort, extraArgs, settings)
if runtime.GOOS == "darwin" && strings.EqualFold(filepath.Ext(appPath), ".app") {
command := []string{"open", "-W", "-a", appPath, "--args"}
return append(command, args...)
}
executable := buildCodexExecutable(appPath)
return append([]string{executable}, args...)
}
func buildCodexArguments(debugPort uint16, extraArgs []string) []string {
return buildCodexArgumentsForSettings(debugPort, extraArgs, backendSettings{})
}
func buildCodexArgumentsForSettings(debugPort uint16, extraArgs []string, settings backendSettings) []string {
args := []string{
fmt.Sprintf("--remote-debugging-port=%d", debugPort),
fmt.Sprintf("--remote-allow-origins=http://127.0.0.1:%d", debugPort),
}
if settings.CodexAppFastStartup {
args = append(args, "--disable-features=CalculateNativeWinOcclusion")
}
if settings.CodexAppForceChineseLocale {
args = append(args, "--lang=zh-CN")
}
return append(args, normalizeExtraArgs(extraArgs)...)
}
func buildCodexExecutable(appPath string) string {
if runtime.GOOS == "windows" {
if isWindowsPackagedAppReference(appPath) {
return ""
}
if isWindowsTargetAppExecutableName(filepath.Base(appPath)) {
return appPath
}
if packageRoot := windowsPackageRootFromPath(appPath); packageRoot != "" {
if metadata, ok := readWindowsPackageManifestMetadata(packageRoot); ok && metadata.Executable != "" {
candidate := joinPathLike(packageRoot, metadata.Executable)
if fileExists(candidate) {
return candidate
}
}
}
var candidates []string
for _, subdir := range []string{
"",
"app",
filepath.Join("VFS", "ProgramFilesX64", "ChatGPT"),
filepath.Join("VFS", "ProgramFilesX64", "OpenAI", "ChatGPT"),
filepath.Join("VFS", "ProgramFilesX64", "Codex"),
filepath.Join("VFS", "ProgramFilesX64", "OpenAI", "Codex"),
} {
for _, name := range windowsTargetAppExecutableNames() {
candidates = append(candidates, filepath.Join(appPath, subdir, name))
}
}
for _, candidate := range candidates {
if fileExists(candidate) {
return candidate
}
}
if packagedWindowsAppUserModelID(appPath) != "" {
return ""
}
return ""
}
if strings.EqualFold(filepath.Ext(appPath), ".app") {
name := strings.TrimSuffix(filepath.Base(appPath), ".app")
candidates := []string{
filepath.Join(appPath, "Contents", "MacOS", name),
filepath.Join(appPath, "Contents", "MacOS", "ChatGPT"),
}
for _, candidate := range candidates {
if fileExists(candidate) {
return candidate
}
}
}
return appPath
}
func startCodexApp(appPath string, debugPort uint16, settings backendSettings) (codexLaunchHandle, error) {
command := buildCodexLaunchCommandForSettings(appPath, debugPort, settings.CodexExtraArgs, settings)
if runtime.GOOS == "windows" {
directLaunchBlocked := len(command) > 0 && isWindowsProtectedCodexDirectLaunchPath(appPath, command[0])
activation := buildWindowsPackagedActivationForSettings(appPath, debugPort, settings.CodexExtraArgs, settings)
if len(command) > 0 && strings.TrimSpace(command[0]) != "" && fileExists(command[0]) && !directLaunchBlocked {
handle, err := startCodexProcess(command, settings)
if err == nil {
return handle, nil
}
if activation == nil {
return nil, err
}
appendDiagnosticLog("launcher.windows_direct_start_failed", map[string]any{"command": safeCommandForLog(command), "error": err.Error()})
}
if activation != nil {
if tcpPortAccepting(debugPort) {
return nil, fmt.Errorf("调试端口 %d 已被其他进程占用;请完全退出现有 ChatGPT/Codex 和其他使用该端口的 Chromium 应用后重试", debugPort)
}
if processIDs := windowsTargetAppProcessIDs(); len(processIDs) > 0 {
return nil, fmt.Errorf("检测到未启用调试端口的 ChatGPT/Codex 进程 %v;请完全退出应用后重试", processIDs)
}
var attemptErrors []string
var attempts []windowsPackagedLaunchAttempt
if alias := windowsPackagedExecutionAlias(activation.appUserModelID); alias != "" {
aliasCommand := append([]string{alias}, buildCodexArgumentsForSettings(debugPort, settings.CodexExtraArgs, settings)...)
aliasHandle, aliasErr := startVerifiedWindowsPackagedProcess(aliasCommand, activation.appUserModelID, debugPort, true, "App Execution Alias", "launcher.windows_execution_alias", 20*time.Second, settings)
if aliasErr == nil {
return aliasHandle, nil
}
attemptErrors = append(attemptErrors, "App Execution Alias: "+aliasErr.Error())
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "app_execution_alias", Outcome: windowsPackagedAttemptOutcome(aliasErr)})
} else {
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "app_execution_alias", Outcome: "not_available"})
}
if executable := windowsPackagedDirectExecutable(activation.appUserModelID); executable != "" {
executableCommand := append([]string{executable}, buildCodexArgumentsForSettings(debugPort, settings.CodexExtraArgs, settings)...)
executableHandle, executableErr := startVerifiedWindowsPackagedProcess(executableCommand, activation.appUserModelID, debugPort, false, "已注册 MSIX 桌面可执行文件", "launcher.windows_packaged_executable", 20*time.Second, settings)
if executableErr == nil {
return executableHandle, nil
}
attemptErrors = append(attemptErrors, "MSIX 桌面可执行文件: "+executableErr.Error())
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "msix_full_trust_executable", Outcome: windowsPackagedAttemptOutcome(executableErr)})
} else {
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "msix_full_trust_executable", Outcome: "not_available"})
}
processID, activationErr := activateWindowsPackagedAppWithEnvironment(activation.appUserModelID, activation.arguments, codexLaunchEnvironment(settings))
if activationErr == nil {
activation.processID = processID
if waitForWindowsPackagedCDPPortAvailable(debugPort, processID, activation.appUserModelID, true, 15*time.Second) {
return activation, nil
}
cleanupFailedWindowsPackagedLaunch(processID, debugPort)
err := packagedCodexDebugPortError(activation.appUserModelID, debugPort, "ApplicationActivationManager")
appendDiagnosticLog("launcher.windows_packaged_activation_no_cdp", map[string]any{
"appUserModelId": activation.appUserModelID,
"debug_port": debugPort,
"processId": processID,
"error": err.Error(),
})
attemptErrors = append(attemptErrors, "ApplicationActivationManager: "+err.Error())
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "application_activation_manager", Outcome: "debug_port_unavailable"})
} else {
attemptErrors = append(attemptErrors, "ApplicationActivationManager: "+activationErr.Error())
attempts = append(attempts, windowsPackagedLaunchAttempt{Method: "application_activation_manager", Outcome: windowsPackagedAttemptOutcome(activationErr)})
}
if len(attemptErrors) == 0 {
attemptErrors = append(attemptErrors, "没有发现清单声明的 App Execution Alias、FullTrust 可执行文件或可用兼容激活方式")
}
return nil, &windowsPackagedLaunchError{
appUserModelID: activation.appUserModelID,
attempts: attempts,
message: fmt.Sprintf("无法启动 Windows 新版 ChatGPT %s;%s", activation.appUserModelID, strings.Join(attemptErrors, ";")),
}
}
if directLaunchBlocked {
return nil, windowsProtectedMSIXLaunchError(appPath, command[0])
}
}
if len(command) == 0 || strings.TrimSpace(command[0]) == "" {
return nil, fmt.Errorf("未找到 ChatGPT.exe 或 Codex.exe:%s", appPath)
}
if runtime.GOOS == "windows" && !isWindowsAppsExecutionAlias(command[0]) && !fileExists(command[0]) {
return nil, fmt.Errorf("未找到 ChatGPT.exe 或 Codex.exe:%s", appPath)
}
handle, err := startCodexProcess(command, settings)
if err != nil {
return nil, err
}
return handle, nil
}
type windowsPackagedLaunchAttempt struct {
Method string `json:"method"`
Outcome string `json:"outcome"`
}
type windowsPackagedLaunchError struct {
appUserModelID string
attempts []windowsPackagedLaunchAttempt
message string
}
func (err *windowsPackagedLaunchError) Error() string {
return err.message
}
func windowsPackagedAttemptOutcome(err error) string {
if err == nil {
return "success"
}
text := strings.ToLower(err.Error())
switch {
case strings.Contains(text, "incorrect function"):
return "activation_incorrect_function"
case strings.Contains(text, "调试端口") || strings.Contains(text, "remote-debugging-port") || strings.Contains(text, "cdp"):
return "debug_port_unavailable"
case strings.Contains(text, "无法启动") || strings.Contains(text, "executable file not found") || strings.Contains(text, "file not found") || strings.Contains(text, "cannot find the file") || strings.Contains(text, "cannot find the path"):
return "process_start_failed"
default:
return "activation_failed"
}
}
func startVerifiedWindowsPackagedProcess(command []string, appUserModelID string, debugPort uint16, requirePackageIdentity bool, method, eventPrefix string, timeout time.Duration, settings backendSettings) (codexLaunchHandle, error) {
handle, err := startCodexProcess(command, settings)
if err != nil {
appendDiagnosticLog(eventPrefix+"_failed", map[string]any{
"appUserModelId": appUserModelID,
"executable": filepath.Base(command[0]),
"error": err.Error(),
})
return nil, err
}
processLaunch, ok := handle.(*codexProcessLaunch)
if !ok || processLaunch.cmd == nil || processLaunch.cmd.Process == nil {
_ = handle.terminate()
return nil, errors.New("Windows 打包应用启动后没有可验证的进程")
}
processID := uint32(processLaunch.cmd.Process.Pid)
if waitForWindowsPackagedCDPPortAvailable(debugPort, processID, appUserModelID, requirePackageIdentity, timeout) {
processLaunch.windowsPackageFamily = windowsPackageFamilyFromAppUserModelID(appUserModelID)
processLaunch.windowsRootProcessID = processID
processLaunch.windowsRequirePackageIdentity = requirePackageIdentity
processLaunch.debugPort = debugPort
return handle, nil
}
cleanupFailedWindowsPackagedLaunch(processID, debugPort)
err = packagedCodexDebugPortError(appUserModelID, debugPort, method)
appendDiagnosticLog(eventPrefix+"_no_cdp", map[string]any{
"appUserModelId": appUserModelID,
"debug_port": debugPort,
"executable": filepath.Base(command[0]),
"processId": processID,
"error": err.Error(),
})
return nil, err
}
func cleanupFailedWindowsPackagedLaunch(processID uint32, debugPort uint16) {
_ = terminateWindowsProcessTree(processID)
terminateWindowsTargetAppProcesses()
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
if len(windowsTargetAppProcessIDs()) == 0 && !tcpPortAccepting(debugPort) {
return
}
time.Sleep(100 * time.Millisecond)
}
}
func windowsProtectedMSIXLaunchError(appPath, executable string) error {
target := strings.TrimSpace(executable)
if target == "" {
target = strings.TrimSpace(appPath)
}
appUserModelID := packagedWindowsAppUserModelID(appPath)
if appUserModelID != "" {
return fmt.Errorf("当前选择的是 Windows Store/MSIX 版新版 ChatGPT(%s),不能直接执行受保护的 WindowsApps 文件,且未能通过应用标识启动。已跳过:%s", appUserModelID, target)
}
return fmt.Errorf("当前选择的是 Windows Store/MSIX 版新版 ChatGPT,不能直接执行受保护的 WindowsApps 文件,且未解析到可用的应用启动标识。已跳过:%s", target)
}
func buildWindowsPackagedActivation(appPath string, debugPort uint16, extraArgs []string) *windowsPackagedActivation {
return buildWindowsPackagedActivationForSettings(appPath, debugPort, extraArgs, backendSettings{})
}
func buildWindowsPackagedActivationForSettings(appPath string, debugPort uint16, extraArgs []string, settings backendSettings) *windowsPackagedActivation {
if runtime.GOOS != "windows" {
return nil
}
appUserModelID := packagedWindowsAppUserModelID(appPath)
return newWindowsPackagedActivation(appUserModelID, debugPort, extraArgs, settings)
}
func newWindowsPackagedActivation(appUserModelID string, debugPort uint16, extraArgs []string, settings backendSettings) *windowsPackagedActivation {
reference := normalizeWindowsPackagedAppReference("aumid:" + strings.TrimSpace(appUserModelID))
if reference == "" {
return nil
}
appUserModelID = strings.TrimPrefix(reference, "aumid:")
if appUserModelID == "" {
return nil
}
return &windowsPackagedActivation{
appUserModelID: appUserModelID,
packageFamilyName: windowsPackageFamilyFromAppUserModelID(appUserModelID),
arguments: commandLineArguments(buildCodexArgumentsForSettings(debugPort, extraArgs, settings)),
debugPort: debugPort,
}
}
func windowsPackagedExplorerCommand(appUserModelID string, args []string) []string {
return []string{"explorer.exe", `shell:AppsFolder\` + appUserModelID}
}
func packagedCodexDebugPortError(appUserModelID string, debugPort uint16, method string) error {
return fmt.Errorf("%s 已启动 Windows Store/MSIX 新版 ChatGPT %s,但未检测到调试端口 %d;应用已安装,请完全退出已有 ChatGPT 进程后重试;若仍无端口,则当前版本可能不接受 --remote-debugging-port,增强注入暂不可用", method, appUserModelID, debugPort)
}
func launchFailureDetail(appPath string, debugPort, helperPort uint16, err error) map[string]any {
detail := map[string]any{
"codex_app": appPath,
"debug_port": debugPort,
"helper_port": helperPort,
"error": err.Error(),
"cdp_port_available": cdpTargetsAvailable(debugPort, 800*time.Millisecond),
"recommended_action": "确认新版 ChatGPT 可从开始菜单启动,并完全退出已有 ChatGPT 进程后重试;若 MSIX 激活后仍没有调试端口,请使用支持该新版应用的工具版本,或选择 WindowsApps 外可直接执行的 ChatGPT.exe / Codex.exe。",
}
var packagedError *windowsPackagedLaunchError
if errors.As(err, &packagedError) {
detail["windows_launch_attempts"] = packagedError.attempts
}
if runtime.GOOS == "windows" {
if activation := buildWindowsPackagedActivation(appPath, debugPort, nil); activation != nil {
detail["appUserModelId"] = activation.appUserModelID
detail["activation_method"] = "packaged_activation"
if alias := windowsPackagedExecutionAlias(activation.appUserModelID); alias != "" {
detail["activation_method"] = "app_execution_alias"
detail["executionAlias"] = alias
} else if windowsPackagedDirectExecutable(activation.appUserModelID) != "" {
detail["activation_method"] = "msix_full_trust_executable"
detail["packagedExecutableAvailable"] = true
}
} else if executable := buildCodexExecutable(appPath); executable != "" {
detail["executable"] = executable
detail["activation_method"] = "executable"
}
}
return detail
}
func commandLineArguments(args []string) string {
quoted := make([]string, 0, len(args))
for _, arg := range args {
quoted = append(quoted, quoteWindowsArgument(arg))
}
return strings.Join(quoted, " ")
}
func quoteWindowsArgument(arg string) string {
if arg != "" && !strings.ContainsAny(arg, " \t\"") {
return arg
}
var output strings.Builder
output.WriteByte('"')
backslashes := 0
for _, ch := range arg {
switch ch {
case '\\':
backslashes++
case '"':
output.WriteString(strings.Repeat("\\", backslashes*2+1))
output.WriteRune(ch)
backslashes = 0
default:
output.WriteString(strings.Repeat("\\", backslashes))
output.WriteRune(ch)
backslashes = 0
}
}
output.WriteString(strings.Repeat("\\", backslashes*2))
output.WriteByte('"')
return output.String()
}
func codexLaunchEnvironment(settingsValues ...backendSettings) []string {
var environment []string
switch runtime.GOOS {
case "darwin":
environment = append(environment, "PATH="+defaultGUIPath)
}
if len(settingsValues) > 0 {
environment = append(environment, imageRelayCLIEnvironment(settingsValues[0])...)
}
return environment
}
func imageRelayCLIEnvironment(settings backendSettings) []string {
profile := activeRelayProfile(normalizeSettings(settings))
if profile.Protocol != "responses" || !usesSeparateImageGenerationAPI(profile) {
return nil
}
return []string{
"OPENAI_API_KEY=" + imageRelayCLIAPIKey,
fmt.Sprintf("OPENAI_BASE_URL=http://127.0.0.1:%d/v1", localRelayProxyPort),
}
}
func mergeLaunchEnvironment(base, overrides []string) []string {
if len(overrides) == 0 {
return append([]string(nil), base...)
}
keys := map[string]struct{}{}
for _, entry := range overrides {
key, _, ok := strings.Cut(entry, "=")
if ok && strings.TrimSpace(key) != "" {
keys[strings.ToUpper(key)] = struct{}{}
}
}
merged := make([]string, 0, len(base)+len(overrides))
for _, entry := range base {
key, _, ok := strings.Cut(entry, "=")
if ok {
if _, replaced := keys[strings.ToUpper(key)]; replaced {
continue
}
}
merged = append(merged, entry)
}
return append(merged, overrides...)
}
func shouldQuitRunningCodexBeforeLaunch(appPath string, debugPort uint16, restart bool) bool {
if runtime.GOOS != "darwin" || !strings.EqualFold(filepath.Ext(appPath), ".app") {
return false
}
if !macOSAppRunning(appPath) {
return false
}
if restart {
return true
}
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Millisecond)
defer cancel()
if _, err := listCDPTargets(ctx, debugPort); err == nil {
return false
}
return true
}
func cdpTargetsAvailable(debugPort uint16, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
targets, err := listCDPTargets(ctx, debugPort)
return err == nil && len(targets) > 0
}
func chatGPTCDPTargetAvailable(debugPort uint16, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
targets, err := listCDPTargets(ctx, debugPort)
if err != nil {
return false
}
for _, target := range targets {
if isCodexCDPPageTarget(target) {
return true
}
}
return false
}
func requestCodexShutdownViaCDP(debugPort uint16, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
targets, err := listCDPTargets(ctx, debugPort)
if err != nil {
return fmt.Errorf("无法连接现有 Codex 调试端口 %d:%w", debugPort, err)
}
target, err := pickCDPPageTarget(targets)
if err != nil {
return err
}
conn, _, err := websocket.DefaultDialer.DialContext(ctx, target.WebSocketDebuggerURL, nil)
if err != nil {
return err
}
defer conn.Close()
if err := conn.WriteJSON(map[string]any{"id": 1, "method": "Browser.close", "params": map[string]any{}}); err != nil {
return err
}
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if !tcpPortAccepting(debugPort) {
return nil
}
time.Sleep(200 * time.Millisecond)
}
return fmt.Errorf("等待 Codex 调试端口 %d 关闭超时", debugPort)
}
func waitForTCPPortFree(port uint16, timeout time.Duration) bool {
deadline := time.Now().Add(timeout)
address := fmt.Sprintf("127.0.0.1:%d", port)
for {
listener, err := net.Listen("tcp", address)
if err == nil {
_ = listener.Close()