-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathserve_background.go
More file actions
1048 lines (990 loc) · 28.2 KB
/
Copy pathserve_background.go
File metadata and controls
1048 lines (990 loc) · 28.2 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"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/gofrs/flock"
"github.com/spf13/cobra"
"go.kenn.io/agentsview/internal/config"
"go.kenn.io/agentsview/internal/db"
)
const (
backgroundServeReadyTimeout = 5 * time.Second
backgroundAutoStartReadyTimeout = 90 * time.Second
)
var errServeStartupInProgress = errors.New(
"agentsview serve startup is already in progress",
)
var waitForDaemonStartupForEnsure = WaitForDaemonStartupContext
var startServeBackgroundProcessForEnsure = startServeBackgroundProcess
var startServeBackgroundProcessForRun = startServeBackgroundProcess
type backgroundLaunchPolicy struct {
// ConfigOnly starts exclusively from persistent configuration. In
// particular, NoSync is a CLI/runtime option rather than a config key.
ConfigOnly bool
Operation string
Context context.Context
Attached bool
OnLaunch func(pid int, logPath string)
OnProgress func(*startupState, time.Duration)
}
type backgroundServeReadyWaitPolicy struct {
Attached bool
Observe func(*startupState, time.Duration)
}
type backgroundLaunchResult struct {
Runtime *DaemonRuntime
Started bool
LogPath string
childPID int
}
func (p backgroundLaunchPolicy) operation() string {
if p.Operation != "" {
return p.Operation
}
return "serve background"
}
// backgroundChildEnvVar marks the re-exec'd serve process as the child of a
// background launch. The child reads it to keep the auth token out of
// serve.log; the parent prints the token to the invoking terminal instead.
const backgroundChildEnvVar = "AGENTSVIEW_BACKGROUND_CHILD"
// runningAsBackgroundChild reports whether this process was spawned by
// runServeBackground.
func runningAsBackgroundChild() bool {
return os.Getenv(backgroundChildEnvVar) == "1"
}
// backgroundLaunchLockPath is the advisory lock that serializes concurrent
// `serve --background` launches for a data dir.
func backgroundLaunchLockPath(dataDir string) string {
return filepath.Join(dataDir, "serve.background.lock")
}
// acquireBackgroundLaunchLock takes the background launch lock without
// blocking. ok is false when another launch already holds it.
func acquireBackgroundLaunchLock(dataDir string) (*flock.Flock, bool) {
lock, acquired, _ := acquireBackgroundLaunchLockWithError(dataDir)
return lock, acquired
}
// acquireBackgroundLaunchLockWithError distinguishes a lock held by another
// lifecycle operation from an I/O failure opening or acquiring the lock.
func acquireBackgroundLaunchLockWithError(
dataDir string,
) (*flock.Flock, bool, error) {
lock := flock.New(backgroundLaunchLockPath(dataDir))
locked, err := lock.TryLock()
locked, err = classifyBackgroundLaunchLockResult(locked, err)
if err != nil {
return nil, false, err
}
if !locked {
return nil, false, nil
}
return lock, true, nil
}
func isBackgroundLaunchActive(dataDir string) bool {
lock, ok := acquireBackgroundLaunchLock(dataDir)
if ok {
_ = lock.Unlock()
return false
}
return true
}
// reportBackgroundLaunchInProgress waits for an in-flight startup to publish
// its runtime record and reports the running server, or notes that a launch
// is still in progress when no record appears in time. authToken may be empty
// for a contender that has not loaded config; a require_auth daemon then
// reports as in-progress rather than by URL.
func reportBackgroundLaunchInProgress(dataDir, authToken string) {
waitForBackgroundLaunchOwner(
context.Background(), dataDir, authToken, backgroundServeReadyTimeout,
)
if rt := FindDaemonRuntime(dataDir, authToken); rt != nil &&
!rt.ReadOnly && !shouldUpgradeDaemonRuntime(rt, version) {
fmt.Printf(
"agentsview already running at %s (pid %d)\n",
urlFromDaemonRuntime(rt),
rt.Record.PID,
)
return
}
fmt.Println("agentsview serve --background is already in progress.")
}
// runServeBackgroundCommand serializes the launch before loading config.
// Config loading writes config.toml (the cursor secret, and the auth token via
// EnsureAuthToken), so two concurrent launches that loaded config outside the
// lock could clobber each other's writes -- leaving the spawned server using a
// token the parent never printed. Holding the launch lock across both config
// load and token generation makes those writes single-writer.
func runServeBackgroundCommand(
cmd *cobra.Command, opts serveReplacementOptions,
) {
dataDir, err := config.ResolveDataDir()
if err != nil {
fatal("serve background: resolving data dir: %v", err)
}
// The launch lock lives under the data dir, which may not exist on first
// run.
if err := os.MkdirAll(dataDir, 0o700); err != nil {
fatal("serve background: creating data dir: %v", err)
}
launchLock, ok := acquireBackgroundLaunchLock(dataDir)
if !ok {
// Another launch holds the lock and owns the config writes. Report
// without loading config so this process never touches config.toml.
reportBackgroundLaunchInProgress(dataDir, "")
return
}
defer func() { _ = launchLock.Unlock() }()
runServeBackground(mustLoadConfig(cmd), os.Args[1:], opts)
}
// runServeBackground preserves the serve --background CLI's fatal and output
// behavior around the reusable, error-returning background launch path.
func runServeBackground(
cfg config.Config, args []string, opts serveReplacementOptions,
) {
result, err := startServeBackground(
cfg, args, opts, backgroundLaunchPolicy{},
)
if err != nil {
fatal("%v", err)
}
if result.Runtime != nil && !result.Started {
fmt.Printf(
"agentsview already running at %s (pid %d)\n",
urlFromDaemonRuntime(result.Runtime),
result.Runtime.Record.PID,
)
return
}
if !result.Started {
return
}
if result.Runtime != nil {
fmt.Printf(
"agentsview running at %s (pid %d)\n",
urlFromDaemonRuntime(result.Runtime),
result.childPID,
)
fmt.Printf("Logs: %s\n", result.LogPath)
return
}
fmt.Printf(
"agentsview starting in background (pid %d)\n",
result.childPID,
)
fmt.Printf("Logs: %s\n", result.LogPath)
}
// startServeBackground generates the daemon auth token, checks for an existing
// writable daemon, and starts a detached child when needed. The caller must
// already hold the background launch lock. Unlike runServeBackground, this
// lower-level entry point returns launch failures for non-CLI callers.
func startServeBackground(
cfg config.Config,
args []string,
opts serveReplacementOptions,
policy backgroundLaunchPolicy,
) (backgroundLaunchResult, error) {
var result backgroundLaunchResult
operation := policy.operation()
if policy.ConfigOnly {
cfg.NoSync = false
}
replacementCheckStarted := time.Now()
if err := ensureServeAuthToken(&cfg); err != nil {
return result, fmt.Errorf(
"%s: generating auth token: %w", operation, err,
)
}
if cfg.RequireAuth {
if cfg.AuthToken != "" {
fmt.Println("Auth enabled. Token is configured.")
}
}
if err := validateUniqueWritableDaemonSet(cfg.DataDir, cfg.AuthToken); err != nil {
return result, fmt.Errorf("%s: %w", operation, err)
}
decision := decideServeDaemonReplacement(cfg, opts)
switch decision.Action {
case serveReplacementNone:
case serveReplacementUseExisting:
if rt := decision.Runtime; rt != nil {
result.Runtime = rt
result.childPID = rt.Record.PID
}
return result, nil
case serveReplacementAuto, serveReplacementExplicit:
waitedForExternalStartup := false
if waited, err := waitForExternalServeStartupBeforeReplacement(
context.Background(),
cfg.DataDir,
cfg.AuthToken,
backgroundServeReadyTimeout,
); waited {
waitedForExternalStartup = true
if err != nil {
if errors.Is(err, errServeStartupInProgress) {
fmt.Println(errServeStartupInProgress.Error() + ".")
return result, nil
}
return result, fmt.Errorf("%s: %w", operation, err)
}
}
decision = refreshServeDaemonReplacementDecision(
cfg, opts, decision, waitedForExternalStartup,
replacementCheckStarted,
)
switch decision.Action {
case serveReplacementNone:
case serveReplacementUseExisting:
if rt := decision.Runtime; rt != nil {
result.Runtime = rt
result.childPID = rt.Record.PID
}
return result, nil
case serveReplacementAuto, serveReplacementExplicit:
// runServeBackgroundCommand holds the background launch lock across
// this stop/start sequence, so another CLI launcher cannot race into
// the replacement gap.
if err := prepareBackgroundReplacement(
&cfg, decision.Runtime, !policy.ConfigOnly,
); err != nil {
return result, fmt.Errorf("%s: %w", operation, err)
}
fmt.Println("Replacing agentsview daemon")
for _, line := range serveDaemonReplacementLines(decision) {
fmt.Println(line)
}
if err := stopDaemonRuntimeForUpgrade(cfg, decision.Runtime); err != nil {
return result, fmt.Errorf(
"%s: stopping daemon before restart: %w",
operation, err,
)
}
case serveReplacementRefuse:
return result, fmt.Errorf(
"%s: %s", operation,
strings.Join(serveDaemonConflictLines(decision), "\n"),
)
default:
return result, fmt.Errorf(
"%s: unknown serve replacement action %d",
operation, decision.Action,
)
}
case serveReplacementRefuse:
return result, fmt.Errorf(
"%s: %s", operation,
strings.Join(serveDaemonConflictLines(decision), "\n"),
)
default:
return result, fmt.Errorf(
"%s: unknown serve replacement action %d",
operation, decision.Action,
)
}
// A writable daemon (a foreground `serve` or a prior background launch)
// is mid-startup and holds the start lock but has not yet published a
// runtime record. Wait for it instead of racing a second server.
if IsLocalDaemonActive(cfg.DataDir, cfg.AuthToken) {
reportBackgroundLaunchInProgress(cfg.DataDir, cfg.AuthToken)
return result, nil
}
if policy.ConfigOnly {
args = []string{"serve"}
} else {
args = serveBackgroundChildArgs(args)
}
args = serveBackgroundArgsWithNoSync(args, cfg.NoSync)
child, logPath, err := startServeBackgroundProcessForRun(cfg, args)
result.LogPath = logPath
if err != nil {
return result, fmt.Errorf("%s: %w", operation, err)
}
result.Started = true
result.childPID = child.Process.Pid
if policy.OnLaunch != nil {
policy.OnLaunch(result.childPID, logPath)
}
waitCh := make(chan error, 1)
go func() {
waitCh <- child.Wait()
}()
waitContext := policy.Context
if waitContext == nil {
waitContext = context.Background()
}
rt, err := waitForBackgroundServeReadyWithPolicy(
waitContext,
cfg.DataDir,
cfg.AuthToken,
waitCh,
backgroundServeReadyTimeout,
backgroundServeReadyWaitPolicy{
Attached: policy.Attached,
Observe: policy.OnProgress,
},
)
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return result, fmt.Errorf(
"%s: waiting for server readiness: %w", operation, err,
)
}
return result, fmt.Errorf(
"%s: server exited before becoming ready: %w\nLogs: %s",
operation, err, logPath,
)
}
result.Runtime = rt
return result, nil
}
func validateUniqueWritableDaemonSet(dataDir, authToken string) error {
records, err := writableDaemonRecords(dataDir, authToken)
if err != nil {
return fmt.Errorf("inspecting writable daemon runtimes: %w", err)
}
if len(records) <= 1 {
return nil
}
return fmt.Errorf(
"multiple writable agentsview daemons are running (pids %s); refusing startup or replacement; run `agentsview daemon status`, then `agentsview daemon stop` before retrying",
formatRecordPIDList(records),
)
}
func prepareBackgroundReplacement(
cfg *config.Config, rt *DaemonRuntime, adoptRuntimeOptions bool,
) error {
if cfg == nil {
return errors.New("nil replacement config")
}
if err := validateUniqueWritableDaemonSet(cfg.DataDir, cfg.AuthToken); err != nil {
return err
}
if err := checkBackgroundReplacementDataVersion(cfg); err != nil {
return err
}
if adoptRuntimeOptions {
adoptDaemonRuntimeLaunchOptions(cfg, rt)
}
validationCfg := *cfg
if validationCfg.Host == "" {
// Config loading supplies the loopback default. Keep direct callers
// and tests with a zero-value host aligned with that final child
// configuration rather than treating an omitted host as non-loopback.
validationCfg.Host = "127.0.0.1"
}
if err := validateServeConfig(validationCfg); err != nil {
return fmt.Errorf("invalid serve configuration: %w", err)
}
return nil
}
func ensureBackgroundServe(
ctx context.Context,
cfg *config.Config,
waitTimeout time.Duration,
) (*DaemonRuntime, error) {
if cfg == nil {
return nil, fmt.Errorf("nil config")
}
if err := ctx.Err(); err != nil {
return nil, err
}
if waitTimeout <= 0 {
waitTimeout = backgroundAutoStartReadyTimeout
}
if err := os.MkdirAll(cfg.DataDir, 0o700); err != nil {
return nil, fmt.Errorf("creating data dir: %w", err)
}
var launchLock *flock.Flock
for {
var ok bool
launchLock, ok = acquireBackgroundLaunchLock(cfg.DataDir)
if ok {
break
}
waitForBackgroundLaunchOwner(
ctx, cfg.DataDir, cfg.AuthToken, waitTimeout,
)
if err := ctx.Err(); err != nil {
return nil, err
}
if cfg.AuthToken == "" {
adoptBackgroundLaunchConfig(cfg)
}
if retryLock, retryOK := acquireBackgroundLaunchLock(
cfg.DataDir,
); retryOK {
_ = retryLock.Unlock()
continue
}
if rt := FindDaemonRuntime(cfg.DataDir, cfg.AuthToken); rt != nil &&
!rt.ReadOnly {
if shouldUpgradeDaemonRuntime(rt, version) {
return nil, fmt.Errorf(
"agentsview serve --background is already in progress",
)
}
return rt, nil
}
if _, err := findIncompatibleWritableDaemonRuntime(
cfg.DataDir, cfg.AuthToken,
); err != nil {
return nil, fmt.Errorf(
"incompatible daemon is already running: %w; run "+
"`agentsview daemon stop` before starting this version",
err,
)
}
if IsLocalDaemonActive(cfg.DataDir, cfg.AuthToken) {
return nil, fmt.Errorf(
"agentsview serve --background is already in progress",
)
}
return nil, fmt.Errorf(
"agentsview serve --background did not publish a runtime record",
)
}
defer func() { _ = launchLock.Unlock() }()
if err := ensureServeAuthToken(cfg); err != nil {
return nil, fmt.Errorf("generating auth token: %w", err)
}
if err := validateUniqueWritableDaemonSet(cfg.DataDir, cfg.AuthToken); err != nil {
return nil, err
}
probeDaemon:
if rt := FindDaemonRuntime(cfg.DataDir, cfg.AuthToken); rt != nil &&
!rt.ReadOnly {
if shouldUpgradeDaemonRuntime(rt, version) {
if waited, err := waitForExternalServeStartupBeforeReplacement(
ctx, cfg.DataDir, cfg.AuthToken, waitTimeout,
); waited {
if err != nil {
return nil, err
}
goto probeDaemon
}
if serveReplacementTargetChanged(*cfg, rt) {
goto probeDaemon
}
if err := prepareBackgroundReplacement(cfg, rt, true); err != nil {
return nil, err
}
if err := stopDaemonRuntimeForUpgrade(*cfg, rt); err != nil {
return nil, fmt.Errorf(
"stopping older daemon before restart: %w",
err,
)
}
} else {
return rt, nil
}
}
if rt := FindDaemonRuntime(cfg.DataDir, cfg.AuthToken); rt != nil &&
!rt.ReadOnly {
return rt, nil
}
if rt, err := findIncompatibleWritableDaemonRuntime(
cfg.DataDir, cfg.AuthToken,
); err != nil {
if rt != nil && shouldUpgradeIncompatibleDaemonRuntime(rt, version) {
if waited, err := waitForExternalServeStartupBeforeReplacement(
ctx, cfg.DataDir, cfg.AuthToken, waitTimeout,
); waited {
if err != nil {
return nil, err
}
goto probeDaemon
}
if serveReplacementTargetChanged(*cfg, rt) {
goto probeDaemon
}
if err := prepareBackgroundReplacement(cfg, rt, true); err != nil {
return nil, err
}
if stopErr := stopDaemonRuntimeForUpgrade(*cfg, rt); stopErr != nil {
return nil, fmt.Errorf(
"stopping older daemon before restart: %w",
stopErr,
)
}
} else {
return nil, fmt.Errorf(
"incompatible daemon is already running: %w; run "+
"`agentsview daemon stop` before starting this version",
err,
)
}
}
if IsLocalDaemonActive(cfg.DataDir, cfg.AuthToken) {
waitForDaemonStartupForEnsure(
ctx, cfg.DataDir, waitTimeout, cfg.AuthToken,
)
if err := ctx.Err(); err != nil {
return nil, err
}
if rt := FindDaemonRuntime(cfg.DataDir, cfg.AuthToken); rt != nil &&
!rt.ReadOnly {
return rt, nil
}
stoppedUpgradeable := false
if rt, err := findIncompatibleWritableDaemonRuntime(
cfg.DataDir, cfg.AuthToken,
); err != nil {
if rt != nil && shouldUpgradeIncompatibleDaemonRuntime(rt, version) {
if waited, err := waitForExternalServeStartupBeforeReplacement(
ctx, cfg.DataDir, cfg.AuthToken, waitTimeout,
); waited {
if err != nil {
return nil, err
}
goto probeDaemon
}
if serveReplacementTargetChanged(*cfg, rt) {
goto probeDaemon
}
if err := prepareBackgroundReplacement(cfg, rt, true); err != nil {
return nil, err
}
if stopErr := stopDaemonRuntimeForUpgrade(*cfg, rt); stopErr != nil {
return nil, fmt.Errorf(
"stopping older daemon before restart: %w",
stopErr,
)
}
stoppedUpgradeable = true
} else {
return nil, fmt.Errorf(
"incompatible daemon is already running: %w; run "+
"`agentsview daemon stop` before starting this version",
err,
)
}
}
if !stoppedUpgradeable {
return nil, errLocalDaemonUnreachable
}
}
args := []string{"serve"}
args = serveBackgroundArgsWithNoSync(args, cfg.NoSync)
args = serveBackgroundArgsWithSkipInitialSync(args, cfg.SkipInitialSync)
child, logPath, err := startServeBackgroundProcessForEnsure(*cfg, args)
if err != nil {
return nil, err
}
waitCh := make(chan error, 1)
go func() {
waitCh <- child.Wait()
}()
rt, err := waitForBackgroundServeReady(
ctx, cfg.DataDir, cfg.AuthToken, waitCh, waitTimeout,
)
if err != nil {
return nil, fmt.Errorf(
"server exited before becoming ready: %w; logs: %s",
err, logPath,
)
}
if rt == nil {
return nil, fmt.Errorf(
"server did not become ready within %s; logs: %s",
waitTimeout, logPath,
)
}
return rt, nil
}
func waitForExternalServeStartup(
ctx context.Context,
dataDir string,
authToken string,
waitTimeout time.Duration,
) (*DaemonRuntime, bool, error) {
if !isExternalDaemonStarting(dataDir) {
return nil, false, nil
}
if waitTimeout <= 0 {
waitTimeout = backgroundServeReadyTimeout
}
deadline := time.Now().Add(waitTimeout)
for isExternalDaemonStarting(dataDir) {
if err := ctx.Err(); err != nil {
return nil, true, err
}
if rt := FindDaemonRuntime(dataDir, authToken); rt != nil &&
!rt.ReadOnly && rt.RuntimeFallback {
return rt, true, nil
}
remaining := time.Until(deadline)
if remaining <= 0 {
return nil, true, errServeStartupInProgress
}
wait := min(remaining, startProbeTick())
timer := time.NewTimer(wait)
select {
case <-ctx.Done():
timer.Stop()
return nil, true, ctx.Err()
case <-timer.C:
}
}
if err := ctx.Err(); err != nil {
return nil, true, err
}
if rt := FindDaemonRuntime(dataDir, authToken); rt != nil && !rt.ReadOnly {
return rt, true, nil
}
if rt, err := findIncompatibleWritableDaemonRuntime(
dataDir, authToken,
); rt != nil && err != nil {
return nil, true, fmt.Errorf(
"incompatible daemon is already running: %w; run "+
"`agentsview daemon stop` before starting this version",
err,
)
}
return nil, true, fmt.Errorf(
"agentsview serve startup finished without publishing a writable " +
"runtime record",
)
}
func waitForExternalServeStartupBeforeReplacement(
ctx context.Context,
dataDir string,
authToken string,
waitTimeout time.Duration,
) (bool, error) {
_, waited, err := waitForExternalServeStartup(
ctx, dataDir, authToken, waitTimeout,
)
if !waited {
return false, nil
}
if err != nil && (errors.Is(err, errServeStartupInProgress) ||
errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded)) {
return true, err
}
if err := ctx.Err(); err != nil {
return true, err
}
return true, nil
}
func refreshServeDaemonReplacementDecision(
cfg config.Config,
opts serveReplacementOptions,
original serveReplacementDecision,
waitedForExternalStartup bool,
replacementCheckStarted time.Time,
) serveReplacementDecision {
if !opts.Replace {
decision := decideServeDaemonReplacement(cfg, opts)
if decision.Runtime == nil &&
replacementTargetStillStopConfirmed(cfg, original.Runtime) {
return original
}
return decision
}
decision := decideServeDaemonReplacement(
cfg, serveReplacementOptions{},
)
if decision.Action == serveReplacementUseExisting &&
!sameDaemonReplacementTarget(original.Runtime, decision.Runtime) {
return decision
}
// A foreground startup may publish its runtime while still holding the
// start lock. If that startup wins, reuse the daemon it just published
// instead of treating --replace as permission to stop it.
if waitedForExternalStartup &&
decision.Action == serveReplacementUseExisting &&
daemonRuntimeStartedAfter(decision.Runtime, replacementCheckStarted) {
return decision
}
if decision.Runtime == nil &&
replacementTargetStillStopConfirmed(cfg, original.Runtime) {
return original
}
return decideServeDaemonReplacement(cfg, opts)
}
func daemonRuntimeStartedAfter(rt *DaemonRuntime, started time.Time) bool {
return rt != nil &&
!rt.Record.StartedAt.IsZero() &&
rt.Record.StartedAt.After(started)
}
func serveReplacementTargetChanged(
cfg config.Config, original *DaemonRuntime,
) bool {
decision := decideServeDaemonReplacement(cfg, serveReplacementOptions{})
if decision.Runtime == nil {
return !replacementTargetStillStopConfirmed(cfg, original)
}
return !sameDaemonReplacementTarget(original, decision.Runtime)
}
func replacementTargetStillStopConfirmed(
cfg config.Config, original *DaemonRuntime,
) bool {
return original != nil && stopTargetConfirmed(original.Record, cfg.AuthToken)
}
func sameDaemonReplacementTarget(a, b *DaemonRuntime) bool {
if a == nil || b == nil {
return false
}
return a.Record.PID == b.Record.PID &&
a.Record.Address == b.Record.Address
}
func checkBackgroundReplacementDataVersion(cfg *config.Config) error {
if cfg == nil || cfg.DBPath == "" {
return nil
}
return db.CheckDataVersion(cfg.DBPath)
}
func waitForBackgroundLaunchOwner(
ctx context.Context,
dataDir string,
authToken string,
waitTimeout time.Duration,
) {
deadline := time.Now().Add(waitTimeout)
for time.Now().Before(deadline) {
if isExternalDaemonStarting(dataDir) {
_, _, _ = waitForExternalServeStartup(
ctx, dataDir, authToken, time.Until(deadline),
)
return
}
if rt := FindDaemonRuntime(dataDir, authToken); rt != nil &&
!rt.ReadOnly {
return
}
if IsLocalDaemonActive(dataDir, authToken) {
if WaitForDaemonStartupContext(
ctx, dataDir, time.Until(deadline), authToken,
) {
if rt := FindDaemonRuntime(dataDir, authToken); rt != nil &&
!rt.ReadOnly {
return
}
}
if err := ctx.Err(); err != nil {
return
}
launchLock, ok := acquireBackgroundLaunchLock(dataDir)
if ok {
_ = launchLock.Unlock()
// The parent launch lock can clear before the child has
// published its writable runtime. Keep waiting through that
// handoff instead of treating a read-only mirror as success.
if !IsDaemonStarting(dataDir) {
return
}
}
} else {
launchLock, ok := acquireBackgroundLaunchLock(dataDir)
if ok {
_ = launchLock.Unlock()
return
}
}
wait := min(time.Until(deadline), startProbeTick())
timer := time.NewTimer(wait)
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
}
}
}
func adoptBackgroundLaunchConfig(cfg *config.Config) {
reloaded, err := config.LoadMinimal()
if err != nil {
return
}
if reloaded.DataDir != cfg.DataDir {
return
}
cfg.RequireAuth = reloaded.RequireAuth
cfg.AuthToken = reloaded.AuthToken
}
func startServeBackgroundProcess(
cfg config.Config,
args []string,
) (*exec.Cmd, string, error) {
logPath := serveLogPath(cfg.DataDir)
exe, err := os.Executable()
if err != nil {
return nil, logPath, fmt.Errorf("finding executable: %w", err)
}
// 0o600: the child writes its startup output here, which can include
// auth details, so keep the log readable only by the owner.
logFile, err := os.OpenFile(
logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600,
)
if err != nil {
return nil, logPath, fmt.Errorf("opening log file: %w", err)
}
defer logFile.Close()
if _, err := fmt.Fprintf(
logFile,
"\n--- agentsview serve background start %s ---\n",
time.Now().Format(time.RFC3339),
); err != nil {
return nil, logPath, fmt.Errorf("writing log header: %w", err)
}
devNull, err := os.Open(os.DevNull)
if err != nil {
return nil, logPath, fmt.Errorf("opening null device: %w", err)
}
defer devNull.Close()
childArgs := serveBackgroundChildArgs(args)
cmd := exec.Command(exe, childArgs...)
cmd.Env = append(os.Environ(), backgroundChildEnvVar+"=1")
if cfg.DataDir != "" {
cmd.Env = append(cmd.Env, "AGENTSVIEW_DATA_DIR="+cfg.DataDir)
}
cmd.Stdin = devNull
cmd.Stdout = logFile
cmd.Stderr = logFile
configureServeBackgroundCommand(cmd)
if err := cmd.Start(); err != nil {
return nil, logPath, fmt.Errorf("starting server: %w", err)
}
return cmd, logPath, nil
}
func serveBackgroundChildArgs(args []string) []string {
out := make([]string, 0, len(args))
for _, arg := range args {
if isBackgroundChildStrippedFlagArg(arg) {
continue
}
out = append(out, arg)
}
return out
}
func adoptDaemonRuntimeLaunchOptions(cfg *config.Config, rt *DaemonRuntime) {
if cfg == nil || rt == nil {
return
}
if rt.NoSync {
cfg.NoSync = true
}
}
func serveBackgroundArgsWithNoSync(args []string, noSync bool) []string {
if !noSync {
return args
}
for _, arg := range args {
for _, name := range []string{"--no-sync", "-no-sync"} {
if arg == name || strings.HasPrefix(arg, name+"=") {
return args
}
}
}
out := append([]string(nil), args...)
return append(out, "--no-sync")
}
func serveBackgroundArgsWithSkipInitialSync(
args []string, skipInitialSync bool,
) []string {
if !skipInitialSync {
return args
}
for _, arg := range args {
if arg == "--skip-initial-sync" ||
strings.HasPrefix(arg, "--skip-initial-sync=") {
return args
}
}
out := append([]string(nil), args...)
return append(out, "--skip-initial-sync")
}
// isBackgroundChildStrippedFlagArg reports whether arg is a serve flag that
// belongs only to the launching parent. The legacy flag normalizer rewrites
// single-dash forms before Cobra parses, so raw child args still need both
// spellings stripped.
func isBackgroundChildStrippedFlagArg(arg string) bool {
for _, name := range []string{
"--background",
"-background",
"--replace",
"-replace",
} {
if arg == name || strings.HasPrefix(arg, name+"=") {
return true
}
}
return false
}
// waitForBackgroundServeReady polls for the spawned child to publish a
// writable runtime record. It returns the runtime once ready, nil on timeout
// (the child is still starting), or an error if the child exits first.
func waitForBackgroundServeReady(
ctx context.Context,
dataDir string,
authToken string,
waitCh <-chan error,
timeout time.Duration,
) (*DaemonRuntime, error) {
return waitForBackgroundServeReadyWithPolicy(
ctx, dataDir, authToken, waitCh, timeout,
backgroundServeReadyWaitPolicy{},
)
}
func waitForBackgroundServeReadyWithPolicy(
ctx context.Context,
dataDir string,
authToken string,
waitCh <-chan error,
timeout time.Duration,
policy backgroundServeReadyWaitPolicy,
) (*DaemonRuntime, error) {
startedAt := time.Now()
var timeoutC <-chan time.Time
if !policy.Attached {