microclaw-engine 0.29.1

Channel-independent Agent Engine and default services for MicroClaw
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
use base64::Engine;
use serde::Deserialize;
use std::path::{Path, PathBuf};

use microclaw_core::error::MicroClawError;

pub const OPENAI_CODEX_PROVIDER: &str = "openai-codex";
pub const QWEN_PORTAL_PROVIDER: &str = "qwen-portal";

#[derive(Debug, Deserialize)]
struct CodexAuthFile {
    tokens: Option<CodexAuthTokens>,
    #[serde(rename = "OPENAI_API_KEY")]
    openai_api_key: Option<String>,
}

#[derive(Debug, Deserialize)]
struct CodexAuthTokens {
    access_token: Option<String>,
    #[serde(rename = "refresh_token")]
    _refresh_token: Option<String>,
    account_id: Option<String>,
}

#[derive(Debug, Deserialize)]
struct QwenOauthCredsFile {
    access_token: Option<String>,
}

#[derive(Debug, Clone)]
pub struct CodexAuthResolved {
    pub bearer_token: String,
    pub account_id: Option<String>,
}

pub fn provider_allows_empty_api_key(provider: &str) -> bool {
    provider.eq_ignore_ascii_case("ollama")
        || provider.eq_ignore_ascii_case(OPENAI_CODEX_PROVIDER)
        || provider.eq_ignore_ascii_case(QWEN_PORTAL_PROVIDER)
}

pub fn is_openai_codex_provider(provider: &str) -> bool {
    provider.eq_ignore_ascii_case(OPENAI_CODEX_PROVIDER)
}

pub fn is_qwen_portal_provider(provider: &str) -> bool {
    provider.eq_ignore_ascii_case(QWEN_PORTAL_PROVIDER)
}

pub fn default_codex_auth_path() -> PathBuf {
    let base = std::env::var("CODEX_HOME")
        .ok()
        .filter(|v| !v.trim().is_empty())
        .as_deref()
        .map(expand_tilde)
        .unwrap_or_else(|| expand_tilde("~/.codex"));
    Path::new(&base).join("auth.json")
}

pub fn default_codex_config_path() -> PathBuf {
    let base = std::env::var("CODEX_HOME")
        .ok()
        .filter(|v| !v.trim().is_empty())
        .as_deref()
        .map(expand_tilde)
        .unwrap_or_else(|| expand_tilde("~/.codex"));
    Path::new(&base).join("config.toml")
}

pub fn default_qwen_oauth_creds_path() -> PathBuf {
    let base = std::env::var("QWEN_HOME")
        .ok()
        .filter(|v| !v.trim().is_empty())
        .as_deref()
        .map(expand_tilde)
        .unwrap_or_else(|| expand_tilde("~/.qwen"));
    Path::new(&base).join("oauth_creds.json")
}

pub fn codex_auth_file_has_access_token() -> Result<bool, MicroClawError> {
    if let Ok(token) = std::env::var("OPENAI_CODEX_ACCESS_TOKEN") {
        if !token.trim().is_empty() {
            return Ok(true);
        }
    }

    let path = default_codex_auth_path();
    if !path.exists() {
        return Ok(false);
    }
    let content = std::fs::read_to_string(&path).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to read Codex auth file {}: {e}",
            path.display()
        ))
    })?;
    let parsed: CodexAuthFile = serde_json::from_str(&content).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to parse Codex auth file {}: {e}",
            path.display()
        ))
    })?;
    let has_access_token = parsed
        .tokens
        .as_ref()
        .and_then(|tokens| tokens.access_token.as_ref())
        .map(|token| !token.trim().is_empty())
        .unwrap_or(false);
    let has_openai_api_key = parsed
        .openai_api_key
        .as_ref()
        .map(|key| !key.trim().is_empty())
        .unwrap_or(false);
    Ok(has_access_token || has_openai_api_key)
}

pub fn resolve_openai_codex_auth(
    _fallback_api_key: &str,
) -> Result<CodexAuthResolved, MicroClawError> {
    if let Ok(token) = std::env::var("OPENAI_CODEX_ACCESS_TOKEN") {
        let trimmed = token.trim();
        if !trimmed.is_empty() {
            return Ok(CodexAuthResolved {
                bearer_token: trimmed.to_string(),
                account_id: None,
            });
        }
    }

    let auth_path = default_codex_auth_path();
    if auth_path.exists() {
        let content = std::fs::read_to_string(&auth_path).map_err(|e| {
            MicroClawError::Config(format!(
                "Failed to read Codex auth file {}: {e}",
                auth_path.display()
            ))
        })?;
        let parsed: CodexAuthFile = serde_json::from_str(&content).map_err(|e| {
            MicroClawError::Config(format!(
                "Failed to parse Codex auth file {}: {e}",
                auth_path.display()
            ))
        })?;
        if let Some(token) = parsed
            .tokens
            .as_ref()
            .and_then(|tokens| tokens.access_token.as_ref())
            .map(|token| token.trim())
            .filter(|token| !token.is_empty())
        {
            return Ok(CodexAuthResolved {
                bearer_token: token.to_string(),
                account_id: parsed
                    .tokens
                    .as_ref()
                    .and_then(|tokens| tokens.account_id.clone())
                    .map(|id| id.trim().to_string())
                    .filter(|id| !id.is_empty()),
            });
        }

        if let Some(api_key) = parsed
            .openai_api_key
            .as_deref()
            .map(str::trim)
            .filter(|key| !key.is_empty())
        {
            return Ok(CodexAuthResolved {
                bearer_token: api_key.to_string(),
                account_id: None,
            });
        }
    }

    Err(MicroClawError::Config(format!(
        "OpenAI Codex provider requires ~/.codex/auth.json (access token or OPENAI_API_KEY), or OPENAI_CODEX_ACCESS_TOKEN. Run `codex login` or update Codex config files (expected auth file: {}).",
        auth_path.display()
    )))
}

pub fn qwen_oauth_file_has_access_token() -> Result<bool, MicroClawError> {
    if let Ok(token) = std::env::var("QWEN_PORTAL_ACCESS_TOKEN") {
        if !token.trim().is_empty() {
            return Ok(true);
        }
    }
    if let Ok(token) = std::env::var("QWEN_CODE_ACCESS_TOKEN") {
        if !token.trim().is_empty() {
            return Ok(true);
        }
    }
    let path = default_qwen_oauth_creds_path();
    if !path.exists() {
        return Ok(false);
    }
    let content = std::fs::read_to_string(&path).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to read Qwen oauth creds file {}: {e}",
            path.display()
        ))
    })?;
    let parsed: QwenOauthCredsFile = serde_json::from_str(&content).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to parse Qwen oauth creds file {}: {e}",
            path.display()
        ))
    })?;
    Ok(parsed
        .access_token
        .as_ref()
        .map(|v| !v.trim().is_empty())
        .unwrap_or(false))
}

pub fn resolve_qwen_portal_auth(
    fallback_api_key: &str,
) -> Result<CodexAuthResolved, MicroClawError> {
    if let Ok(token) = std::env::var("QWEN_PORTAL_ACCESS_TOKEN") {
        let trimmed = token.trim();
        if !trimmed.is_empty() {
            return Ok(CodexAuthResolved {
                bearer_token: trimmed.to_string(),
                account_id: None,
            });
        }
    }
    if let Ok(token) = std::env::var("QWEN_CODE_ACCESS_TOKEN") {
        let trimmed = token.trim();
        if !trimmed.is_empty() {
            return Ok(CodexAuthResolved {
                bearer_token: trimmed.to_string(),
                account_id: None,
            });
        }
    }
    let path = default_qwen_oauth_creds_path();
    if path.exists() {
        let content = std::fs::read_to_string(&path).map_err(|e| {
            MicroClawError::Config(format!(
                "Failed to read Qwen oauth creds file {}: {e}",
                path.display()
            ))
        })?;
        let parsed: QwenOauthCredsFile = serde_json::from_str(&content).map_err(|e| {
            MicroClawError::Config(format!(
                "Failed to parse Qwen oauth creds file {}: {e}",
                path.display()
            ))
        })?;
        if let Some(token) = parsed
            .access_token
            .as_deref()
            .map(str::trim)
            .filter(|v| !v.is_empty())
        {
            return Ok(CodexAuthResolved {
                bearer_token: token.to_string(),
                account_id: None,
            });
        }
    }
    let fallback = fallback_api_key.trim();
    if !fallback.is_empty() {
        return Ok(CodexAuthResolved {
            bearer_token: fallback.to_string(),
            account_id: None,
        });
    }
    Err(MicroClawError::Config(
        "qwen-portal requires ~/.qwen/oauth_creds.json (access_token), or QWEN_PORTAL_ACCESS_TOKEN, or api_key.".into(),
    ))
}

pub fn codex_config_default_openai_base_url() -> Option<String> {
    let path = default_codex_config_path();
    let content = std::fs::read_to_string(path).ok()?;
    parse_codex_config_default_openai_base_url(&content)
}

fn parse_codex_config_default_openai_base_url(content: &str) -> Option<String> {
    let mut model_provider: Option<String> = None;

    for raw in content.lines() {
        let line = raw.split('#').next().unwrap_or("").trim();
        if line.is_empty() {
            continue;
        }
        if line.starts_with("model_provider") {
            model_provider = parse_toml_string_assignment(line, "model_provider");
            if model_provider.is_some() {
                break;
            }
        }
    }

    let provider = model_provider?;
    let target = provider.trim();
    if target.is_empty() {
        return None;
    }

    let mut current_provider: Option<String> = None;
    for raw in content.lines() {
        let line = raw.split('#').next().unwrap_or("").trim();
        if line.is_empty() {
            continue;
        }
        if line.starts_with('[') && line.ends_with(']') {
            let section = &line[1..line.len() - 1];
            current_provider = section
                .strip_prefix("model_providers.")
                .map(|v| v.trim().trim_matches('"').to_string())
                .filter(|v| !v.is_empty());
            continue;
        }

        if current_provider.as_deref() == Some(target) {
            if let Some(base_url) = parse_toml_string_assignment(line, "base_url") {
                let normalized = base_url.trim().trim_end_matches('/').to_string();
                if !normalized.is_empty() {
                    return Some(normalized);
                }
            }
        }
    }

    None
}

fn parse_toml_string_assignment(line: &str, key: &str) -> Option<String> {
    let (lhs, rhs) = line.split_once('=')?;
    if lhs.trim() != key {
        return None;
    }
    let value = rhs.trim();
    if !(value.starts_with('"') && value.ends_with('"') && value.len() >= 2) {
        return None;
    }
    Some(value[1..value.len() - 1].to_string())
}

fn expand_tilde(input: &str) -> String {
    if let Some(rest) = input.strip_prefix("~/") {
        if let Ok(home) = std::env::var("HOME") {
            return format!("{home}/{rest}");
        }
    }
    if input == "~" {
        if let Ok(home) = std::env::var("HOME") {
            return home;
        }
    }
    input.to_string()
}

#[derive(Debug, Deserialize)]
struct CodexRefreshResponse {
    access_token: String,
    refresh_token: Option<String>,
    id_token: Option<String>,
}

/// OpenAI's OAuth token endpoint, used by `codex login`.
pub(crate) const CODEX_TOKEN_URL: &str = "https://auth.openai.com/oauth/token";
/// The OAuth client id of the Codex CLI.
const CODEX_CLIENT_ID: &str = "app_EMoamEEZ73f0CkXaXp7hrann";
/// Refresh the access token this long before it expires, so a request never
/// leaves with a token that runs out on the way.
const CODEX_REFRESH_MARGIN_SECS: i64 = 300;

/// The OAuth tokens in a Codex auth file, if it has both.
fn codex_oauth_tokens(parsed: &serde_json::Value) -> Option<(String, String)> {
    let tokens = parsed.get("tokens")?.as_object()?;
    let field = |name: &str| {
        tokens
            .get(name)
            .and_then(|v| v.as_str())
            .map(str::trim)
            .filter(|v| !v.is_empty())
            .map(str::to_string)
    };
    Some((field("access_token")?, field("refresh_token")?))
}

fn codex_refresh_body(refresh_token: &str) -> serde_json::Value {
    serde_json::json!({
        "grant_type": "refresh_token",
        "refresh_token": refresh_token,
        "client_id": CODEX_CLIENT_ID,
    })
}

/// Write a refresh response into the parsed auth file. Returns false when the
/// response carries no access token.
fn apply_codex_refresh(parsed: &mut serde_json::Value, resp: CodexRefreshResponse) -> bool {
    if resp.access_token.trim().is_empty() {
        return false;
    }
    let Some(tokens) = parsed.get_mut("tokens").and_then(|t| t.as_object_mut()) else {
        return false;
    };
    tokens.insert(
        "access_token".to_string(),
        serde_json::Value::String(resp.access_token),
    );
    for (name, value) in [
        ("refresh_token", resp.refresh_token),
        ("id_token", resp.id_token),
    ] {
        if let Some(value) = value.filter(|v| !v.trim().is_empty()) {
            tokens.insert(name.to_string(), serde_json::Value::String(value));
        }
    }
    parsed["last_refresh"] = serde_json::Value::String(chrono::Utc::now().to_rfc3339());
    true
}

fn read_codex_auth_value(path: &Path) -> Result<serde_json::Value, MicroClawError> {
    let content = std::fs::read_to_string(path).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to read Codex auth file {}: {e}",
            path.display()
        ))
    })?;
    serde_json::from_str(&content).map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to parse Codex auth file {}: {e}",
            path.display()
        ))
    })
}

/// `std::fs::write` keeps the existing file's mode (Codex creates it 0600).
fn write_codex_auth_value(path: &Path, parsed: &serde_json::Value) -> Result<(), MicroClawError> {
    let text = serde_json::to_string_pretty(parsed).map_err(|e| {
        MicroClawError::Config(format!("Failed to serialize refreshed Codex auth: {e}"))
    })?;
    std::fs::write(path, text)?;
    Ok(())
}

/// Refreshes an expired Codex access token in `~/.codex/auth.json`.
///
/// Blocking: it uses `reqwest::blocking`, so never call it from async code
/// (the client panics when dropped inside a Tokio runtime). Providers use
/// [`openai_codex_auth_for_request`], which refreshes before every request.
pub fn refresh_openai_codex_auth_if_needed() -> Result<(), MicroClawError> {
    let auth_path = default_codex_auth_path();
    if !auth_path.exists() {
        return Ok(());
    }
    let mut parsed = read_codex_auth_value(&auth_path)?;
    let Some((access, refresh)) = codex_oauth_tokens(&parsed) else {
        return Ok(());
    };
    if !jwt_expires_within(&access, 0) {
        return Ok(());
    }

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()?;
    let resp = client
        .post(CODEX_TOKEN_URL)
        .header("content-type", "application/json")
        .body(codex_refresh_body(&refresh).to_string())
        .send()?;
    if !resp.status().is_success() {
        return Ok(());
    }
    let parsed_resp: CodexRefreshResponse = resp.json().map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to parse OpenAI Codex refresh response: {e}"
        ))
    })?;
    if apply_codex_refresh(&mut parsed, parsed_resp) {
        write_codex_auth_value(&auth_path, &parsed)?;
    }
    Ok(())
}

/// One refresh at a time: OpenAI rotates refresh tokens, so two concurrent
/// refreshes with the same token would make the second one fail.
fn codex_refresh_lock() -> &'static tokio::sync::Mutex<()> {
    static LOCK: std::sync::OnceLock<tokio::sync::Mutex<()>> = std::sync::OnceLock::new();
    LOCK.get_or_init(|| tokio::sync::Mutex::new(()))
}

/// Refreshes the OAuth access token in the Codex auth file at `path` when it
/// expires within [`CODEX_REFRESH_MARGIN_SECS`], or when the server rejected
/// it (`rejected` is the token that was sent). If the file already holds a
/// different token than the rejected one, someone else refreshed it and
/// nothing is done. Returns whether the file was refreshed.
pub(crate) async fn refresh_codex_auth_file(
    http: &reqwest::Client,
    token_url: &str,
    path: &Path,
    rejected: Option<&str>,
) -> Result<bool, MicroClawError> {
    let _guard = codex_refresh_lock().lock().await;
    if !path.exists() {
        return Ok(false);
    }
    let mut parsed = read_codex_auth_value(path)?;
    let Some((access, refresh)) = codex_oauth_tokens(&parsed) else {
        return Ok(false);
    };
    let needed = match rejected.map(str::trim) {
        Some(rejected) => rejected == access,
        None => jwt_expires_within(&access, CODEX_REFRESH_MARGIN_SECS),
    };
    if !needed {
        return Ok(false);
    }

    let resp = http
        .post(token_url)
        .header("content-type", "application/json")
        .timeout(std::time::Duration::from_secs(15))
        .body(codex_refresh_body(&refresh).to_string())
        .send()
        .await?;
    let status = resp.status();
    if !status.is_success() {
        let text = resp.text().await.unwrap_or_default();
        return Err(MicroClawError::Config(format!(
            "OpenAI Codex token refresh failed (HTTP {status}): {}; run `codex login` again",
            text.chars().take(300).collect::<String>()
        )));
    }
    let parsed_resp: CodexRefreshResponse = resp.json().await.map_err(|e| {
        MicroClawError::Config(format!(
            "Failed to parse OpenAI Codex refresh response: {e}"
        ))
    })?;
    if !apply_codex_refresh(&mut parsed, parsed_resp) {
        return Ok(false);
    }
    write_codex_auth_value(path, &parsed)?;
    Ok(true)
}

/// The Codex credentials for one request. Reads the auth file every time, so
/// a token refreshed by the Codex CLI is picked up too, and refreshes the
/// OAuth token first when it is about to expire or `rejected` (the token the
/// server just turned down) is still the current one. Refresh errors are
/// logged, not returned: the current token is still worth a try.
/// `OPENAI_CODEX_ACCESS_TOKEN` is used as is and never refreshed.
pub async fn openai_codex_auth_for_request(
    http: &reqwest::Client,
    rejected: Option<&str>,
) -> Result<CodexAuthResolved, MicroClawError> {
    let env_token = std::env::var("OPENAI_CODEX_ACCESS_TOKEN")
        .map(|t| !t.trim().is_empty())
        .unwrap_or(false);
    if !env_token {
        let path = default_codex_auth_path();
        match refresh_codex_auth_file(http, &codex_token_url(), &path, rejected).await {
            Ok(true) => tracing::info!("Refreshed the OpenAI Codex access token"),
            Ok(false) => {}
            Err(e) => tracing::warn!("{e}"),
        }
    }
    resolve_openai_codex_auth("")
}

#[cfg(not(test))]
fn codex_token_url() -> String {
    CODEX_TOKEN_URL.to_string()
}

#[cfg(test)]
pub(crate) static TEST_CODEX_TOKEN_URL: std::sync::Mutex<Option<String>> =
    std::sync::Mutex::new(None);

#[cfg(test)]
fn codex_token_url() -> String {
    TEST_CODEX_TOKEN_URL
        .lock()
        .unwrap()
        .clone()
        .unwrap_or_else(|| "http://127.0.0.1:9/never-called".to_string())
}

/// Whether the JWT's `exp` claim is less than `secs` seconds away (or past).
/// A token without a readable `exp` never counts as expiring.
fn jwt_expires_within(token: &str, secs: i64) -> bool {
    let parts: Vec<&str> = token.split('.').collect();
    if parts.len() < 2 {
        return false;
    }
    let payload = parts[1].trim_end_matches('=');
    let decoded = base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(payload.as_bytes())
        .ok()
        .and_then(|bytes| serde_json::from_slice::<serde_json::Value>(&bytes).ok());
    let exp = decoded
        .as_ref()
        .and_then(|v| v.get("exp"))
        .and_then(|v| v.as_i64());
    match exp {
        Some(ts) => chrono::Utc::now().timestamp() + secs >= ts,
        None => false,
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    fn env_lock() -> std::sync::MutexGuard<'static, ()> {
        crate::test_support::env_lock()
    }

    #[test]
    fn test_provider_allows_empty_api_key() {
        assert!(provider_allows_empty_api_key("ollama"));
        assert!(provider_allows_empty_api_key("openai-codex"));
        assert!(provider_allows_empty_api_key("qwen-portal"));
        assert!(!provider_allows_empty_api_key("openai"));
    }

    #[test]
    fn test_is_openai_codex_provider() {
        assert!(is_openai_codex_provider("openai-codex"));
        assert!(is_openai_codex_provider("OPENAI-CODEX"));
        assert!(!is_openai_codex_provider("openai"));
    }

    #[test]
    fn test_is_qwen_portal_provider() {
        assert!(is_qwen_portal_provider("qwen-portal"));
        assert!(is_qwen_portal_provider("QWEN-PORTAL"));
        assert!(!is_qwen_portal_provider("qwen"));
    }

    #[test]
    fn test_codex_auth_file_has_access_token_accepts_env_var() {
        let _guard = env_lock();
        let prev_access = std::env::var("OPENAI_CODEX_ACCESS_TOKEN").ok();
        std::env::set_var("OPENAI_CODEX_ACCESS_TOKEN", "env-token");

        let has = codex_auth_file_has_access_token().unwrap();

        if let Some(prev) = prev_access {
            std::env::set_var("OPENAI_CODEX_ACCESS_TOKEN", prev);
        } else {
            std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");
        }
        assert!(has);
    }

    #[test]
    fn test_codex_auth_file_has_access_token_accepts_openai_api_key_only() {
        let _guard = env_lock();
        let prev_codex_home = std::env::var("CODEX_HOME").ok();
        let prev_access = std::env::var("OPENAI_CODEX_ACCESS_TOKEN").ok();
        std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");

        let auth_dir = std::env::temp_dir().join(format!(
            "microclaw-codex-auth-openai-key-only-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        ));
        std::fs::create_dir_all(&auth_dir).unwrap();
        std::fs::write(
            auth_dir.join("auth.json"),
            r#"{"OPENAI_API_KEY":"sk-user-stale"}"#,
        )
        .unwrap();
        std::env::set_var("CODEX_HOME", &auth_dir);

        let has = codex_auth_file_has_access_token().unwrap();

        if let Some(prev) = prev_codex_home {
            std::env::set_var("CODEX_HOME", prev);
        } else {
            std::env::remove_var("CODEX_HOME");
        }
        if let Some(prev) = prev_access {
            std::env::set_var("OPENAI_CODEX_ACCESS_TOKEN", prev);
        } else {
            std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");
        }
        let _ = std::fs::remove_file(auth_dir.join("auth.json"));
        let _ = std::fs::remove_dir(auth_dir);

        assert!(has);
    }

    #[test]
    fn test_resolve_openai_codex_auth_accepts_openai_api_key_only() {
        let _guard = env_lock();
        let prev_codex_home = std::env::var("CODEX_HOME").ok();
        let prev_access = std::env::var("OPENAI_CODEX_ACCESS_TOKEN").ok();
        std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");
        std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");

        let auth_dir = std::env::temp_dir().join(format!(
            "microclaw-codex-auth-openai-key-reject-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        ));
        std::fs::create_dir_all(&auth_dir).unwrap();
        std::fs::write(
            auth_dir.join("auth.json"),
            r#"{"OPENAI_API_KEY":"sk-user-stale"}"#,
        )
        .unwrap();
        std::env::set_var("CODEX_HOME", &auth_dir);

        let auth = resolve_openai_codex_auth("").unwrap();

        if let Some(prev) = prev_codex_home {
            std::env::set_var("CODEX_HOME", prev);
        } else {
            std::env::remove_var("CODEX_HOME");
        }
        if let Some(prev) = prev_access {
            std::env::set_var("OPENAI_CODEX_ACCESS_TOKEN", prev);
        } else {
            std::env::remove_var("OPENAI_CODEX_ACCESS_TOKEN");
        }
        let _ = std::fs::remove_file(auth_dir.join("auth.json"));
        let _ = std::fs::remove_dir(auth_dir);

        assert_eq!(auth.bearer_token, "sk-user-stale");
        assert!(auth.account_id.is_none());
    }

    #[test]
    fn test_parse_codex_config_default_openai_base_url() {
        let content = r#"
model_provider = "tabcode"

[model_providers.tabcode]
name = "openai"
base_url = "https://api.tabcode.cc/openai"
wire_api = "responses"
"#;
        let base = parse_codex_config_default_openai_base_url(content).unwrap();
        assert_eq!(base, "https://api.tabcode.cc/openai");
    }

    #[test]
    fn test_qwen_oauth_file_has_access_token() {
        let _guard = env_lock();
        let prev_qwen_home = std::env::var("QWEN_HOME").ok();
        let prev_qwen_access = std::env::var("QWEN_PORTAL_ACCESS_TOKEN").ok();
        std::env::remove_var("QWEN_PORTAL_ACCESS_TOKEN");
        let qwen_dir = std::env::temp_dir().join(format!(
            "microclaw-qwen-oauth-creds-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        ));
        std::fs::create_dir_all(&qwen_dir).unwrap();
        std::fs::write(
            qwen_dir.join("oauth_creds.json"),
            r#"{"access_token":"qwen-oauth-token"}"#,
        )
        .unwrap();
        std::env::set_var("QWEN_HOME", &qwen_dir);

        let has = qwen_oauth_file_has_access_token().unwrap();

        if let Some(prev) = prev_qwen_home {
            std::env::set_var("QWEN_HOME", prev);
        } else {
            std::env::remove_var("QWEN_HOME");
        }
        if let Some(prev) = prev_qwen_access {
            std::env::set_var("QWEN_PORTAL_ACCESS_TOKEN", prev);
        } else {
            std::env::remove_var("QWEN_PORTAL_ACCESS_TOKEN");
        }
        let _ = std::fs::remove_file(qwen_dir.join("oauth_creds.json"));
        let _ = std::fs::remove_dir(qwen_dir);

        assert!(has);
    }

    #[test]
    fn test_resolve_qwen_portal_auth_reads_oauth_creds_file() {
        let _guard = env_lock();
        let prev_qwen_home = std::env::var("QWEN_HOME").ok();
        let prev_qwen_access = std::env::var("QWEN_PORTAL_ACCESS_TOKEN").ok();
        std::env::remove_var("QWEN_PORTAL_ACCESS_TOKEN");

        let qwen_dir = std::env::temp_dir().join(format!(
            "microclaw-qwen-oauth-resolve-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos()
        ));
        std::fs::create_dir_all(&qwen_dir).unwrap();
        std::fs::write(
            qwen_dir.join("oauth_creds.json"),
            r#"{"access_token":"qwen-oauth-token"}"#,
        )
        .unwrap();
        std::env::set_var("QWEN_HOME", &qwen_dir);

        let auth = resolve_qwen_portal_auth("").unwrap();

        if let Some(prev) = prev_qwen_home {
            std::env::set_var("QWEN_HOME", prev);
        } else {
            std::env::remove_var("QWEN_HOME");
        }
        if let Some(prev) = prev_qwen_access {
            std::env::set_var("QWEN_PORTAL_ACCESS_TOKEN", prev);
        } else {
            std::env::remove_var("QWEN_PORTAL_ACCESS_TOKEN");
        }
        let _ = std::fs::remove_file(qwen_dir.join("oauth_creds.json"));
        let _ = std::fs::remove_dir(qwen_dir);

        assert_eq!(auth.bearer_token, "qwen-oauth-token");
        assert!(auth.account_id.is_none());
    }

    pub(crate) fn fake_jwt(exp: i64) -> String {
        let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD
            .encode(serde_json::json!({ "exp": exp }).to_string());
        format!("eyJhbGciOiJub25lIn0.{payload}.sig")
    }

    /// Serves one canned HTTP response per connection, in order, and sends
    /// back each request's text.
    pub(crate) fn serve_responses(
        responses: Vec<(u16, String)>,
    ) -> (String, std::sync::mpsc::Receiver<String>) {
        use std::io::{Read, Write};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            for (status, body) in responses {
                let Ok((mut stream, _)) = listener.accept() else {
                    return;
                };
                stream
                    .set_read_timeout(Some(std::time::Duration::from_secs(2)))
                    .unwrap();
                let mut req = Vec::new();
                let mut buf = [0u8; 8192];
                loop {
                    let n = stream.read(&mut buf).unwrap_or(0);
                    if n == 0 {
                        break;
                    }
                    req.extend_from_slice(&buf[..n]);
                    let text = String::from_utf8_lossy(&req);
                    if let Some(head_end) = text.find("\r\n\r\n") {
                        let len = text[..head_end]
                            .lines()
                            .find_map(|l| {
                                let lower = l.to_ascii_lowercase();
                                lower
                                    .strip_prefix("content-length:")
                                    .map(|v| v.trim().parse::<usize>().unwrap_or(0))
                            })
                            .unwrap_or(0);
                        if req.len() >= head_end + 4 + len {
                            break;
                        }
                    }
                }
                let _ = tx.send(String::from_utf8_lossy(&req).to_string());
                let reason = if status == 200 { "OK" } else { "Error" };
                let response = format!(
                    "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
                    body.len()
                );
                let _ = stream.write_all(response.as_bytes());
                let _ = stream.flush();
            }
        });
        (format!("http://{addr}"), rx)
    }

    fn write_auth(dir: &Path, access: &str) -> PathBuf {
        let path = dir.join("auth.json");
        std::fs::write(
            &path,
            serde_json::json!({
                "tokens": {
                    "access_token": access,
                    "refresh_token": "refresh-1",
                    "account_id": "acct-1",
                },
                "OPENAI_API_KEY": null,
            })
            .to_string(),
        )
        .unwrap();
        path
    }

    fn read_tokens(path: &Path) -> serde_json::Value {
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
        v["tokens"].clone()
    }

    #[test]
    fn test_jwt_expires_within() {
        let now = chrono::Utc::now().timestamp();
        assert!(jwt_expires_within(&fake_jwt(now - 10), 0));
        assert!(jwt_expires_within(&fake_jwt(now + 60), 300));
        assert!(!jwt_expires_within(&fake_jwt(now + 3600), 300));
        assert!(!jwt_expires_within("not-a-jwt", 300));
        // Padded payloads (as some encoders emit) still parse.
        let padded = base64::engine::general_purpose::URL_SAFE
            .encode(serde_json::json!({ "exp": now - 1 }).to_string());
        assert!(jwt_expires_within(&format!("h.{padded}.s"), 0));
    }

    #[tokio::test]
    async fn test_refresh_codex_auth_file_refreshes_expiring_token() {
        let dir = tempfile::tempdir().unwrap();
        let old = fake_jwt(chrono::Utc::now().timestamp() + 60);
        let path = write_auth(dir.path(), &old);
        let (url, requests) = serve_responses(vec![(
            200,
            r#"{"access_token":"new-access","refresh_token":"refresh-2","id_token":"id-2"}"#.into(),
        )]);
        let http = reqwest::Client::new();
        let refreshed = refresh_codex_auth_file(&http, &format!("{url}/oauth/token"), &path, None)
            .await
            .unwrap();
        assert!(refreshed);
        let req = requests.recv().unwrap();
        assert!(req.starts_with("POST /oauth/token"));
        assert!(req.contains(r#""refresh_token":"refresh-1""#));
        assert!(req.contains(CODEX_CLIENT_ID));
        let tokens = read_tokens(&path);
        assert_eq!(tokens["access_token"], "new-access");
        assert_eq!(tokens["refresh_token"], "refresh-2");
        assert_eq!(tokens["id_token"], "id-2");
        assert_eq!(tokens["account_id"], "acct-1");
    }

    #[tokio::test]
    async fn test_refresh_codex_auth_file_keeps_fresh_token() {
        let dir = tempfile::tempdir().unwrap();
        let fresh = fake_jwt(chrono::Utc::now().timestamp() + 3600);
        let path = write_auth(dir.path(), &fresh);
        let http = reqwest::Client::new();
        let refreshed =
            refresh_codex_auth_file(&http, "http://127.0.0.1:9/never-called", &path, None)
                .await
                .unwrap();
        assert!(!refreshed);
        assert_eq!(read_tokens(&path)["access_token"], fresh.as_str());
    }

    #[tokio::test]
    async fn test_refresh_codex_auth_file_refreshes_rejected_token() {
        let dir = tempfile::tempdir().unwrap();
        let fresh = fake_jwt(chrono::Utc::now().timestamp() + 3600);
        let path = write_auth(dir.path(), &fresh);
        let (url, _requests) =
            serve_responses(vec![(200, r#"{"access_token":"new-access"}"#.into())]);
        let http = reqwest::Client::new();
        let refreshed = refresh_codex_auth_file(&http, &url, &path, Some(&fresh))
            .await
            .unwrap();
        assert!(refreshed);
        let tokens = read_tokens(&path);
        assert_eq!(tokens["access_token"], "new-access");
        assert_eq!(tokens["refresh_token"], "refresh-1");
    }

    #[tokio::test]
    async fn test_refresh_codex_auth_file_skips_when_already_replaced() {
        let dir = tempfile::tempdir().unwrap();
        let current = fake_jwt(chrono::Utc::now().timestamp() + 3600);
        let path = write_auth(dir.path(), &current);
        let http = reqwest::Client::new();
        let refreshed = refresh_codex_auth_file(
            &http,
            "http://127.0.0.1:9/never-called",
            &path,
            Some("an-older-token"),
        )
        .await
        .unwrap();
        assert!(!refreshed);
        assert_eq!(read_tokens(&path)["access_token"], current.as_str());
    }

    #[tokio::test]
    async fn test_refresh_codex_auth_file_reports_failed_refresh() {
        let dir = tempfile::tempdir().unwrap();
        let old = fake_jwt(chrono::Utc::now().timestamp() - 10);
        let path = write_auth(dir.path(), &old);
        let (url, _requests) = serve_responses(vec![(400, r#"{"error":"invalid_grant"}"#.into())]);
        let http = reqwest::Client::new();
        let err = refresh_codex_auth_file(&http, &url, &path, None)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("codex login"));
        assert_eq!(read_tokens(&path)["access_token"], old.as_str());
    }
}