forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.rs
More file actions
837 lines (767 loc) · 27.1 KB
/
dispatch.rs
File metadata and controls
837 lines (767 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
#![cfg_attr(test, allow(clippy::await_holding_lock))]
use anyhow::Result;
use std::process::{Command as ProcessCommand, Stdio};
use std::time::Instant;
use super::args::{
AmbientCommand, Args, AuthCommand, Command, MemoryCommand, ModelCommand, ProviderCommand,
RestartCommand, SessionCommand, TranscriptModeArg,
};
use crate::{
agent, auth, build, provider, provider_catalog, server, session, setup_hints, startup_profile,
tui,
};
use super::{
commands, debug, hot_exec, login, output, provider_init, selfdev, terminal, tui_launch,
};
use provider_init::ProviderChoice;
pub(crate) async fn run_main(mut args: Args) -> Result<()> {
resolve_resume_arg(&mut args)?;
if let Some(profile_name) = args
.provider_profile
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
{
provider_catalog::apply_named_provider_profile_env(profile_name)?;
crate::env::set_var("JCODE_PROVIDER_PROFILE_NAME", profile_name);
crate::env::set_var("JCODE_PROVIDER_PROFILE_ACTIVE", "1");
args.provider = ProviderChoice::OpenaiCompatible;
}
match args.command {
Some(Command::Serve {
temporary_server,
owner_pid,
temp_idle_timeout_secs,
}) => {
let serve_start = Instant::now();
crate::env::set_var("JCODE_NON_INTERACTIVE", "1");
if temporary_server {
server::configure_temporary_server(owner_pid, temp_idle_timeout_secs);
}
let provider_start = Instant::now();
let provider =
provider_init::init_provider(&args.provider, args.model.as_deref()).await?;
let provider_ms = provider_start.elapsed().as_millis();
let server_new_start = Instant::now();
let server = server::Server::new(provider);
let server_new_ms = server_new_start.elapsed().as_millis();
crate::logging::info(&format!(
"[TIMING] serve bootstrap: provider_init={}ms, server_new={}ms, before_run={}ms",
provider_ms,
server_new_ms,
serve_start.elapsed().as_millis()
));
server.run().await?;
}
Some(Command::Connect) => {
tui_launch::run_client().await?;
}
Some(Command::Run {
message,
json,
ndjson,
}) => {
commands::run_single_message_command(
&args.provider,
args.model.as_deref(),
args.resume.as_deref(),
&message,
json,
ndjson,
)
.await?;
}
Some(Command::Login {
account,
no_browser,
print_auth_url,
callback_url,
auth_code,
json,
complete,
no_validate,
google_access_tier,
api_base,
api_key,
api_key_env,
}) => {
login::run_login(
&args.provider,
account.as_deref(),
login::LoginOptions {
no_browser,
print_auth_url,
callback_url,
auth_code,
json,
complete,
no_validate,
google_access_tier: google_access_tier.map(|tier| match tier {
super::args::GoogleAccessTierArg::Full => {
auth::google::GmailAccessTier::Full
}
super::args::GoogleAccessTierArg::Readonly => {
auth::google::GmailAccessTier::ReadOnly
}
}),
openai_compatible_api_base: api_base,
openai_compatible_api_key: api_key,
openai_compatible_api_key_env: api_key_env,
openai_compatible_default_model: args.model.clone(),
},
)
.await?;
}
Some(Command::Repl) => {
let (provider, registry) =
provider_init::init_provider_and_registry(&args.provider, args.model.as_deref())
.await?;
let mut agent = agent::Agent::new(provider, registry);
agent.repl().await?;
}
Some(Command::Update) => {
hot_exec::run_update()?;
}
Some(Command::Version { json }) => {
commands::run_version_command(json)?;
}
Some(Command::Usage { json }) => {
commands::run_usage_command(json).await?;
}
Some(Command::SelfDev { build }) => {
selfdev::run_self_dev(build, args.resume).await?;
}
Some(Command::Debug {
command,
arg,
session,
socket,
wait,
}) => {
debug::run_debug_command(&command, &arg, session, socket, wait).await?;
}
Some(Command::Auth(subcmd)) => match subcmd {
AuthCommand::Status { json } => commands::run_auth_status_command(json)?,
AuthCommand::Doctor {
provider,
validate,
json,
} => {
let provider_arg = auth_doctor_provider_arg(provider.as_deref(), &args.provider);
commands::run_auth_doctor_command(provider_arg, validate, json).await?
}
},
Some(Command::Provider(subcmd)) => match subcmd {
ProviderCommand::List { json } => {
commands::run_provider_list_command(json)?;
}
ProviderCommand::Current { json } => {
commands::run_provider_current_command(&args.provider, args.model.as_deref(), json)
.await?;
}
ProviderCommand::Add {
name,
base_url,
model,
context_window,
api_key_env,
api_key,
api_key_stdin,
no_api_key,
auth,
auth_header,
env_file,
set_default,
overwrite,
provider_routing,
model_catalog,
json,
} => {
commands::run_provider_add_command(commands::ProviderAddOptions {
name,
base_url,
model,
context_window,
api_key_env,
api_key,
api_key_stdin,
no_api_key,
auth,
auth_header,
env_file,
set_default,
overwrite,
provider_routing,
model_catalog,
json,
})?;
}
},
Some(Command::Memory(subcmd)) => {
commands::run_memory_command(map_memory_subcommand(subcmd))?;
}
Some(Command::Session(subcmd)) => match subcmd {
SessionCommand::Rename {
session,
name,
clear,
json,
} => commands::run_session_rename_command(&session, name.as_deref(), clear, json)?,
},
Some(Command::Ambient(subcmd)) => {
commands::run_ambient_command(map_ambient_subcommand(subcmd)).await?;
}
Some(Command::Pair { list, revoke }) => {
commands::run_pair_command(list, revoke)?;
}
Some(Command::Permissions) => {
tui::permissions::run_permissions()?;
}
Some(Command::Transcript {
text,
mode,
session,
}) => {
commands::run_transcript_command(text, map_transcript_mode(mode), session).await?;
}
Some(Command::Dictate { r#type }) => {
commands::run_dictate_command(r#type).await?;
}
Some(Command::SetupHotkey {
listen_macos_hotkey,
}) => {
setup_hints::run_setup_hotkey(listen_macos_hotkey)?;
}
Some(Command::SetupLauncher) => {
setup_hints::run_setup_launcher()?;
}
Some(Command::Browser { action }) => {
commands::run_browser(&action).await?;
}
Some(Command::Replay {
session,
swarm,
export,
speed,
timeline,
auto_edit,
video,
cols,
rows,
fps,
centered,
no_centered,
}) => {
let centered_override = if centered {
Some(true)
} else if no_centered {
Some(false)
} else {
None
};
tui_launch::run_replay_command(
&session,
swarm,
export,
auto_edit,
speed,
timeline.as_deref(),
video.as_deref(),
cols,
rows,
fps,
centered_override,
)
.await?;
}
Some(Command::Model(subcmd)) => match subcmd {
ModelCommand::List { json, verbose } => {
commands::run_model_command(&args.provider, args.model.as_deref(), json, verbose)
.await?;
}
},
Some(Command::AuthTest {
login,
all_configured,
no_smoke,
no_tool_smoke,
prompt,
json,
output,
coverage,
coverage_file,
coverage_limit,
}) => {
if coverage {
commands::run_auth_test_coverage_command(
json,
output.as_deref(),
coverage_file.as_deref(),
coverage_limit,
)?;
} else {
commands::run_auth_test_command(
&args.provider,
args.model.as_deref(),
login,
all_configured,
no_smoke,
no_tool_smoke,
prompt.as_deref(),
json,
output.as_deref(),
)
.await?;
}
}
Some(Command::Restart { action }) => match action {
RestartCommand::Save { auto_restore } => {
commands::run_restart_save_command(auto_restore).await?
}
RestartCommand::Restore => commands::run_restart_restore_command()?,
RestartCommand::Status => commands::run_restart_status_command()?,
RestartCommand::Clear => commands::run_restart_clear_command()?,
},
None => run_default_command(args).await?,
}
Ok(())
}
fn auth_doctor_provider_arg<'a>(
positional_provider: Option<&'a str>,
global_provider: &'a ProviderChoice,
) -> Option<&'a str> {
positional_provider.or_else(|| {
if *global_provider == ProviderChoice::Auto {
None
} else {
Some(global_provider.as_arg_value())
}
})
}
fn resolve_resume_arg(args: &mut Args) -> Result<()> {
if let Some(ref resume_id) = args.resume {
if resume_id.is_empty() {
return tui_launch::list_sessions();
}
match resolve_resume_id(resume_id) {
Ok(full_id) => {
args.resume = Some(full_id);
}
Err(e) => {
eprintln!("Error: {}", e);
if !output::quiet_enabled() {
eprintln!("\nUse `jcode --resume` to list available sessions.");
}
std::process::exit(1);
}
}
}
Ok(())
}
fn resolve_resume_id(resume_id: &str) -> Result<String> {
match session::find_session_by_name_or_id(resume_id) {
Ok(full_id) => Ok(full_id),
Err(native_err) => match crate::import::import_external_resume_id(resume_id)? {
Some(imported_id) => Ok(imported_id),
None => Err(native_err),
},
}
}
fn map_memory_subcommand(subcmd: MemoryCommand) -> commands::MemorySubcommand {
match subcmd {
MemoryCommand::List { scope, tag } => commands::MemorySubcommand::List { scope, tag },
MemoryCommand::Search { query, semantic } => {
commands::MemorySubcommand::Search { query, semantic }
}
MemoryCommand::Export { output, scope } => {
commands::MemorySubcommand::Export { output, scope }
}
MemoryCommand::Import {
input,
scope,
overwrite,
} => commands::MemorySubcommand::Import {
input,
scope,
overwrite,
},
MemoryCommand::Stats => commands::MemorySubcommand::Stats,
MemoryCommand::ClearTest => commands::MemorySubcommand::ClearTest,
}
}
fn map_ambient_subcommand(subcmd: AmbientCommand) -> commands::AmbientSubcommand {
match subcmd {
AmbientCommand::Status => commands::AmbientSubcommand::Status,
AmbientCommand::Log => commands::AmbientSubcommand::Log,
AmbientCommand::Trigger => commands::AmbientSubcommand::Trigger,
AmbientCommand::Stop => commands::AmbientSubcommand::Stop,
AmbientCommand::RunVisible => commands::AmbientSubcommand::RunVisible,
}
}
fn map_transcript_mode(mode: TranscriptModeArg) -> crate::protocol::TranscriptMode {
match mode {
TranscriptModeArg::Insert => crate::protocol::TranscriptMode::Insert,
TranscriptModeArg::Append => crate::protocol::TranscriptMode::Append,
TranscriptModeArg::Replace => crate::protocol::TranscriptMode::Replace,
TranscriptModeArg::Send => crate::protocol::TranscriptMode::Send,
}
}
async fn run_default_command(args: Args) -> Result<()> {
startup_profile::mark("run_main_none_branch");
let explicit_provider_or_model = args.provider != ProviderChoice::Auto
|| args.model.is_some()
|| args.provider_profile.is_some();
if args.resume.is_none()
&& !explicit_provider_or_model
&& commands::maybe_run_pending_restart_restore_on_startup().await?
{
return Ok(());
}
let startup_hints = if args.fresh_spawn {
None
} else {
setup_hints::maybe_show_setup_hints()
};
startup_profile::mark("setup_hints");
if args.resume.is_none() {
terminal::show_crash_resume_hint();
}
startup_profile::mark("crash_resume_hint");
let cwd = std::env::current_dir()?;
let in_jcode_repo = build::is_jcode_repo(&cwd);
startup_profile::mark("is_jcode_repo");
let already_in_selfdev = crate::cli::selfdev::client_selfdev_requested();
if in_jcode_repo && !already_in_selfdev && !args.no_selfdev {
output::stderr_info("📍 Detected jcode repository - enabling self-dev mode");
output::stderr_info(" Using shared server with self-dev session mode");
output::stderr_info(" (use --no-selfdev to disable auto-detection)");
output::stderr_blank_line();
crate::env::set_var(selfdev::CLIENT_SELFDEV_ENV, "1");
crate::process_title::set_initial_title(&args);
}
startup_profile::mark("client_mode_start");
let mut server_running = if args.fresh_spawn {
true
} else {
server_is_running().await
};
startup_profile::mark("server_check");
if !server_running {
server_running = wait_for_existing_reload_server("client startup").await;
}
if !server_running && std::env::var("JCODE_RESUMING").is_ok() {
server_running = wait_for_resuming_server(
"client startup without reload marker",
std::time::Duration::from_secs(5),
)
.await;
}
if server_running && explicit_provider_or_model {
output::stderr_info(
"Server already running; provider/model flags only apply when starting a new server.",
);
output::stderr_info(format!(
"Current server settings control `/model`. Restart server to apply: --provider {}{}",
args.provider.as_arg_value(),
args.model
.as_ref()
.map(|m| format!(" --model {}", m))
.unwrap_or_default()
));
}
if !server_running {
maybe_prompt_server_bootstrap_login(&args.provider).await?;
spawn_server(
&args.provider,
args.model.as_deref(),
args.provider_profile.as_deref(),
)
.await?;
}
startup_profile::mark("pre_tui_client");
if std::env::var("JCODE_RESUMING").is_err() && server_running {
output::stderr_info("Connecting to server...");
}
tui_launch::run_tui_client(
args.resume,
startup_hints,
!server_running,
args.fresh_spawn,
)
.await?;
Ok(())
}
pub(crate) async fn server_is_running() -> bool {
server_is_running_at(&server::socket_path()).await
}
async fn wait_for_existing_reload_server(context: &str) -> bool {
if let Some(state) = server::recent_reload_state(std::time::Duration::from_secs(30)) {
match state.phase {
server::ReloadPhase::Starting => {
crate::logging::info(&format!(
"Reload state=starting during {}; waiting for existing server to return",
context
));
return wait_for_reloading_server().await;
}
server::ReloadPhase::Failed => {
crate::logging::warn(&format!(
"Reload state=failed during {} on {}: {}; recent_state={}",
context,
server::socket_path().display(),
state
.detail
.unwrap_or_else(|| "unknown reload failure".to_string()),
server::reload_state_summary(std::time::Duration::from_secs(60))
));
}
server::ReloadPhase::SocketReady => {}
}
}
false
}
pub(crate) async fn wait_for_resuming_server(context: &str, timeout: std::time::Duration) -> bool {
let socket_path = server::socket_path();
let start = std::time::Instant::now();
let mut announced = false;
while start.elapsed() < timeout {
if server_is_running_at(&socket_path).await {
crate::logging::info(&format!(
"Server became available during resume wait for {} after {}ms",
context,
start.elapsed().as_millis()
));
return true;
}
if !announced {
crate::logging::info(&format!(
"Server not ready during {}; waiting up to {}ms for a resumed/reloading server before spawning a replacement",
context,
timeout.as_millis()
));
announced = true;
}
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
false
}
pub(crate) async fn wait_for_reloading_server() -> bool {
match server::await_reload_handoff(&server::socket_path(), std::time::Duration::from_secs(30))
.await
{
server::ReloadWaitStatus::Ready => true,
server::ReloadWaitStatus::Failed(detail) => {
crate::logging::warn(&format!(
"Reload handoff failed while waiting for server on {}: {}; recent_state={}",
server::socket_path().display(),
detail.unwrap_or_else(|| "unknown reload failure".to_string()),
server::reload_state_summary(std::time::Duration::from_secs(60))
));
false
}
server::ReloadWaitStatus::Idle => false,
server::ReloadWaitStatus::Waiting { .. } => false,
}
}
async fn server_is_running_at(path: &std::path::Path) -> bool {
server::is_server_ready(path).await || server::has_live_listener(path).await
}
#[cfg(unix)]
fn spawn_lock_path(socket_path: &std::path::Path) -> std::path::PathBuf {
std::path::PathBuf::from(format!("{}.spawning", socket_path.display()))
}
#[cfg(unix)]
struct SpawnLockGuard {
_file: std::fs::File,
path: std::path::PathBuf,
}
#[cfg(unix)]
impl Drop for SpawnLockGuard {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
#[cfg(unix)]
fn try_acquire_spawn_lock(path: &std::path::Path) -> Result<Option<SpawnLockGuard>> {
use std::fs::OpenOptions;
use std::os::fd::AsRawFd;
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(path)?;
let fd = file.as_raw_fd();
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
if ret == 0 {
Ok(Some(SpawnLockGuard {
_file: file,
path: path.to_path_buf(),
}))
} else {
Ok(None)
}
}
#[cfg(unix)]
async fn acquire_spawn_lock_or_wait(
socket_path: &std::path::Path,
) -> Result<Option<SpawnLockGuard>> {
let lock_path = spawn_lock_path(socket_path);
let wait_start = std::time::Instant::now();
let wait_timeout = std::time::Duration::from_secs(10);
let mut announced_wait = false;
loop {
if let Some(lock) = try_acquire_spawn_lock(&lock_path)? {
return Ok(Some(lock));
}
if server_is_running_at(socket_path).await {
return Ok(None);
}
if !announced_wait {
output::stderr_info("Another client is starting the server, waiting...");
announced_wait = true;
}
if wait_start.elapsed() >= wait_timeout {
anyhow::bail!(
"Timed out waiting for another client to start server at {}",
socket_path.display()
);
}
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
pub(crate) async fn maybe_prompt_server_bootstrap_login(
provider_choice: &ProviderChoice,
) -> Result<()> {
startup_profile::mark("cred_check_start");
let mut cred_state = detect_bootstrap_credentials().await;
startup_profile::mark("cred_check_done");
if !cred_state.has_any
&& auth::AuthStatus::has_any_untrusted_external_auth()
&& *provider_choice == ProviderChoice::Auto
{
let _ = provider_init::maybe_run_external_auth_auto_import_flow().await?;
cred_state = detect_bootstrap_credentials().await;
}
if !cred_state.has_any && *provider_choice == ProviderChoice::Auto {
let provider = provider_init::prompt_login_provider_selection(
&provider_catalog::server_bootstrap_login_providers(),
"No credentials found. Let's log in!\n\nChoose a provider:",
)?;
login::run_login_provider(provider, None, login::LoginOptions::default()).await?;
provider_init::apply_login_provider_profile_env(provider);
output::stderr_blank_line();
}
Ok(())
}
struct BootstrapCredentialState {
has_any: bool,
}
async fn detect_bootstrap_credentials() -> BootstrapCredentialState {
let (has_claude, has_openai) = tokio::join!(
tokio::task::spawn_blocking(|| auth::claude::load_credentials().is_ok()),
tokio::task::spawn_blocking(|| auth::codex::load_credentials().is_ok()),
);
let has_claude = has_claude.unwrap_or(false);
let has_openai = has_openai.unwrap_or(false);
let has_openrouter = provider::openrouter::OpenRouterProvider::has_credentials();
let has_copilot = auth::copilot::has_copilot_credentials();
let has_api_key = std::env::var("ANTHROPIC_API_KEY").is_ok();
BootstrapCredentialState {
has_any: has_claude || has_openai || has_openrouter || has_copilot || has_api_key,
}
}
pub(crate) async fn spawn_server(
provider_choice: &ProviderChoice,
model: Option<&str>,
provider_profile: Option<&str>,
) -> Result<()> {
let socket_path = server::socket_path();
if server_is_running_at(&socket_path).await {
startup_profile::mark("server_ready");
return Ok(());
}
if wait_for_existing_reload_server("server spawn").await {
startup_profile::mark("server_ready");
return Ok(());
}
#[cfg(unix)]
let _spawn_lock = acquire_spawn_lock_or_wait(&socket_path).await?;
if server_is_running_at(&socket_path).await {
startup_profile::mark("server_ready");
return Ok(());
}
if wait_for_existing_reload_server("server spawn after lock").await {
startup_profile::mark("server_ready");
return Ok(());
}
startup_profile::mark("server_spawn_start");
output::stderr_info("Starting server...");
let client_requested_selfdev = selfdev::client_selfdev_requested();
let exe = build::shared_server_update_candidate(client_requested_selfdev)
.map(|(path, _)| path)
.or_else(|| std::env::current_exe().ok())
.ok_or_else(|| anyhow::anyhow!("Could not determine executable path for server spawn"))?;
let mut cmd = ProcessCommand::new(&exe);
cmd.env_remove(selfdev::CLIENT_SELFDEV_ENV);
if client_requested_selfdev {
cmd.env("JCODE_DEBUG_CONTROL", "1");
}
cmd.arg("--provider").arg(provider_choice.as_arg_value());
if let Some(provider_profile) = provider_profile {
cmd.arg("--provider-profile").arg(provider_profile);
}
if let Some(model) = model {
cmd.arg("--model").arg(model);
}
cmd.arg("serve")
.stdout(Stdio::null())
.stderr(Stdio::piped());
#[cfg(unix)]
{
let _child = server::spawn_server_notify(&mut cmd).await?;
startup_profile::mark("server_ready");
}
#[cfg(not(unix))]
{
use std::io::Read;
let mut child = cmd.spawn()?;
let start = std::time::Instant::now();
let timeout = std::time::Duration::from_secs(5);
while start.elapsed() < timeout {
if crate::transport::is_socket_path(&server::socket_path()) {
if crate::transport::Stream::connect(server::socket_path())
.await
.is_ok()
{
startup_profile::mark("server_ready");
return Ok(());
}
}
if let Some(status) = child.try_wait()? {
let mut stderr = String::new();
if let Some(mut pipe) = child.stderr.take() {
let _ = pipe.read_to_string(&mut stderr);
}
let detail = stderr.trim();
if detail.is_empty() {
anyhow::bail!("Server exited before becoming ready (status: {})", status);
}
anyhow::bail!(
"Server exited before becoming ready (status: {}). {}",
status,
detail
);
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
anyhow::bail!(
"Timed out waiting for server to become ready at {} after {}ms",
server::socket_path().display(),
timeout.as_millis()
);
}
Ok(())
}
#[cfg(test)]
#[path = "dispatch_tests.rs"]
mod dispatch_tests;