aicx 0.9.2

Operator CLI + MCP server: canonical corpus first, optional semantic index second (Claude Code, Codex, Gemini)
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
//! Shared HTTP Bearer-token auth for MCP HTTP transport and dashboard server.
//!
//! Single token loaded from CLI override, `AICX_HTTP_AUTH_TOKEN`, `~/.aicx/auth-token`,
//! or generated and persisted on Unix (mode 0600). Compared in constant time.
//! Mismatch and missing produce the same 401 body to defeat oracle probing.
//!
//! Vibecrafted with AI Agents by VetCoders (c)2026 VetCoders

use anyhow::{Context, Result, anyhow};
use axum::{
    Json, Router,
    extract::{Request, State},
    http::{StatusCode, header::AUTHORIZATION},
    middleware::{self, Next},
    response::{IntoResponse, Response},
};
use serde::Serialize;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tower_governor::{GovernorLayer, governor::GovernorConfigBuilder};

const AUTH_RATE_LIMIT_BURST: u32 = 100;
const AUTH_RATE_LIMIT_REPLENISH_MS: u64 = 600;

/// Where the active token came from. Used for the startup log line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthSource {
    /// Token provided via CLI flag (`--auth-token`).
    Cli,
    /// Token read from `AICX_HTTP_AUTH_TOKEN`.
    Env,
    /// Token read from a persistent token file (typically `~/.aicx/auth-token`).
    File(PathBuf),
    /// Token generated on this run and written to a fresh token file.
    Generated(PathBuf),
    /// Auth explicitly disabled by the operator (`--no-require-auth`). No token enforced.
    Disabled,
}

impl AuthSource {
    pub fn describe(&self) -> String {
        match self {
            Self::Cli => "cli".to_string(),
            Self::Env => "env".to_string(),
            Self::File(path) => format!("file:{}", path.display()),
            Self::Generated(path) => format!("generated:{}", path.display()),
            Self::Disabled => "disabled".to_string(),
        }
    }
}

/// Loaded auth state. `token == None` only when the operator explicitly opted out.
#[derive(Debug, Clone)]
pub struct AuthConfig {
    pub token: Option<String>,
    pub source: AuthSource,
}

impl AuthConfig {
    /// Auth is enforced when a token is present.
    pub fn is_enforced(&self) -> bool {
        self.token.is_some()
    }

    /// Disabled auth — no token. Use only when operator passes `--no-require-auth`.
    pub fn disabled() -> Self {
        Self {
            token: None,
            source: AuthSource::Disabled,
        }
    }
}

/// Resolution rule for the canonical persistent token file.
///
/// Lives at `<AICX_HOME>/auth-token` — i.e. honors `$AICX_HOME` when set,
/// falls back to `~/.aicx/auth-token` otherwise. Routes through the
/// canonical resolver so an operator pinning `AICX_HOME` does not end
/// up with an auth token stranded in the default `~/.aicx` while
/// everything else moves.
fn default_token_path() -> Result<PathBuf> {
    Ok(crate::store::resolve_aicx_home()?.join("auth-token"))
}

/// Load auth configuration from (in order): CLI override, env, file, or generate.
///
/// `require_auth = false` skips all of the above and returns [`AuthConfig::disabled`].
pub fn load_auth_config(cli_token: Option<&str>, require_auth: bool) -> Result<AuthConfig> {
    if !require_auth {
        return Ok(AuthConfig::disabled());
    }

    if let Some(token) = cli_token {
        let token = token.trim();
        if token.is_empty() {
            return Err(anyhow!("--auth-token must not be empty"));
        }
        return Ok(AuthConfig {
            token: Some(token.to_string()),
            source: AuthSource::Cli,
        });
    }

    if let Ok(value) = std::env::var("AICX_HTTP_AUTH_TOKEN") {
        let value = value.trim().to_string();
        if !value.is_empty() {
            return Ok(AuthConfig {
                token: Some(value),
                source: AuthSource::Env,
            });
        }
    }

    let path = default_token_path()?;
    if path.exists() {
        let content = std::fs::read_to_string(&path)
            .with_context(|| format!("Read auth token file {}", path.display()))?;
        let token = content.trim().to_string();
        if !token.is_empty() {
            return Ok(AuthConfig {
                token: Some(token),
                source: AuthSource::File(path),
            });
        }
    }

    let token = generate_token().context("Generate HTTP auth token")?;
    match persist_token_file(&path, &token).context("Persist HTTP auth token to file")? {
        TokenPersistOutcome::Created | TokenPersistOutcome::Overwrote => Ok(AuthConfig {
            token: Some(token),
            source: AuthSource::Generated(path),
        }),
        TokenPersistOutcome::AdoptedExisting(existing) => Ok(AuthConfig {
            // Startup race: another process created a usable token file
            // between our existence check and our create_new attempt.
            // Adopt theirs instead of failing the entire auth init.
            token: Some(existing),
            source: AuthSource::File(path),
        }),
    }
}

/// Outcome of [`persist_token_file`]. Distinguishes between the
/// happy-path create, recovering from a startup race against another
/// process, and atomically replacing a truncated / empty existing file.
#[derive(Debug)]
enum TokenPersistOutcome {
    /// We won the create race and wrote our token to disk.
    Created,
    /// The file already existed and held a non-empty token; the caller
    /// should adopt that token instead of the one it just generated.
    AdoptedExisting(String),
    /// The file existed but was empty / whitespace-only (truncated or
    /// manually edited); we atomically replaced it with our token while
    /// preserving mode 0600.
    Overwrote,
}

fn generate_token() -> Result<String> {
    let mut buf = [0u8; 32];
    getrandom::fill(&mut buf)
        .map_err(|err| anyhow!("Generate random bytes for auth token: {err}"))?;
    Ok(hex_encode(&buf))
}

fn hex_encode(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut out = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        out.push(HEX[(*byte >> 4) as usize] as char);
        out.push(HEX[(*byte & 0x0f) as usize] as char);
    }
    out
}

fn persist_token_file(path: &PathBuf, token: &str) -> Result<TokenPersistOutcome> {
    #[cfg(windows)]
    {
        let _ = token;
        return Err(anyhow!(
            "Refusing to persist aicx auth token file {} on Windows because this build does not configure restricted file ACLs. Run aicx auth on Linux/macOS, or pass --auth-token <token> explicitly so the token file is never written.",
            path.display()
        ));
    }

    #[cfg(unix)]
    {
        use std::io::{ErrorKind, Write};
        use std::os::unix::fs::OpenOptionsExt;

        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("Create token directory {}", parent.display()))?;
        }

        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(path)
        {
            Ok(mut file) => {
                file.write_all(format!("{}\n", token).as_bytes())
                    .with_context(|| format!("Write token file {}", path.display()))?;
                file.flush()
                    .with_context(|| format!("Flush token file {}", path.display()))?;
                Ok(TokenPersistOutcome::Created)
            }
            Err(err) if err.kind() == ErrorKind::AlreadyExists => {
                // Two cases collapse here:
                //   1. Startup race — another process won create_new between
                //      load_auth_config's `path.exists()` check and ours.
                //   2. Empty-file recovery — the file exists but was
                //      truncated or hand-edited to whitespace, so
                //      load_auth_config fell through to generate+persist.
                // Reading the file once tells us which case we're in and
                // lets us avoid aborting the entire auth init on either.
                let existing = std::fs::read_to_string(path).with_context(|| {
                    format!(
                        "Re-read existing token file after AlreadyExists: {}",
                        path.display()
                    )
                })?;
                let trimmed = existing.trim();
                if !trimmed.is_empty() {
                    return Ok(TokenPersistOutcome::AdoptedExisting(trimmed.to_string()));
                }
                atomic_replace_token_file(path, token)
                    .with_context(|| format!("Replace empty token file {}", path.display()))?;
                Ok(TokenPersistOutcome::Overwrote)
            }
            Err(err) => Err(err).with_context(|| {
                format!(
                    "Create token file {} atomically with mode 0600",
                    path.display()
                )
            }),
        }
    }

    #[cfg(all(not(unix), not(windows)))]
    {
        let _ = token;
        Err(anyhow!(
            "Refusing to persist aicx auth token file {} because this platform does not expose Unix mode 0600 or Windows restricted ACL handling. Pass --auth-token <token> explicitly so the token file is never written.",
            path.display()
        ))
    }
}

/// Atomically replace an existing (empty / whitespace-only) token file
/// with a fresh token while preserving mode 0600. Implemented as
/// "write tempfile sibling with create_new + 0600, then rename" so a
/// crash mid-write never truncates the destination. Called only from
/// the recovery branch of [`persist_token_file`].
#[cfg(unix)]
fn atomic_replace_token_file(path: &Path, token: &str) -> Result<()> {
    use std::io::Write;
    use std::os::unix::fs::OpenOptionsExt;

    let parent = path
        .parent()
        .ok_or_else(|| anyhow!("Token file path has no parent: {}", path.display()))?;
    let file_name = path
        .file_name()
        .ok_or_else(|| anyhow!("Token file path has no filename: {}", path.display()))?;
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.subsec_nanos())
        .unwrap_or(0);
    let mut rand = [0u8; 8];
    getrandom::fill(&mut rand)
        .map_err(|err| anyhow!("Generate random tmp suffix for token replace: {err}"))?;
    let tmp_path = parent.join(format!(
        ".{}.tmp.{}.{}.{}",
        file_name.to_string_lossy(),
        std::process::id(),
        nanos,
        hex_encode(&rand),
    ));

    let res = (|| -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(&tmp_path)
            .with_context(|| format!("Create tmp token file {}", tmp_path.display()))?;
        file.write_all(format!("{}\n", token).as_bytes())
            .with_context(|| format!("Write tmp token file {}", tmp_path.display()))?;
        file.flush()
            .with_context(|| format!("Flush tmp token file {}", tmp_path.display()))?;
        // Drop the handle so the rename is unambiguous on platforms that
        // care about an open writer crossing a rename boundary.
        drop(file);
        std::fs::rename(&tmp_path, path).with_context(|| {
            format!(
                "Rename tmp token file {} -> {}",
                tmp_path.display(),
                path.display()
            )
        })
    })();

    if res.is_err() {
        let _ = std::fs::remove_file(&tmp_path);
    }
    res
}

/// Hand-rolled constant-time byte slice comparison. Returns true iff the inputs
/// have identical length AND identical bytes. Length-mismatch is short-circuited
/// (already a known timing channel for length, but byte content does not leak).
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut result: u8 = 0;
    for (x, y) in a.iter().zip(b.iter()) {
        result |= x ^ y;
    }
    result == 0
}

#[derive(Serialize)]
struct UnauthorizedBody {
    error: &'static str,
}

fn unauthorized_response() -> Response {
    (
        StatusCode::UNAUTHORIZED,
        Json(UnauthorizedBody {
            error: "unauthorized",
        }),
    )
        .into_response()
}

async fn auth_middleware(
    State(config): State<Arc<AuthConfig>>,
    request: Request,
    next: Next,
) -> Response {
    let Some(expected) = config.token.as_deref() else {
        return next.run(request).await;
    };

    let presented = request
        .headers()
        .get(AUTHORIZATION)
        .and_then(|value| value.to_str().ok())
        .and_then(|raw| raw.strip_prefix("Bearer "));

    let Some(provided) = presented else {
        return unauthorized_response();
    };

    if provided.len() != expected.len() {
        return unauthorized_response();
    }

    if constant_time_eq(provided.as_bytes(), expected.as_bytes()) {
        next.run(request).await
    } else {
        unauthorized_response()
    }
}

/// Wrap a router with the Bearer auth middleware. Pass-through when
/// `config.token` is `None` (operator opted out of auth).
///
/// **Rate-limit contract (local-first, NOT proxy-aware).** The governor
/// uses `tower_governor`'s default key extractor, which buckets requests
/// by the peer socket address. AICX is local-first: in the loopback /
/// Tailscale / direct-bind case that maps one bucket per actual client
/// and behaves as intended.
///
/// Behind a reverse proxy (nginx, Caddy, Cloudflare), every request
/// arrives from a small number of proxy IPs, so all proxied users share
/// a single bucket — one noisy client can starve the rest with `429`s.
/// The bind path emits an operator warning when auth is enforced and the
/// server is bound to a non-loopback address; resolving that into a
/// trusted-header proxy mode is tracked as a follow-up (Option B in the
/// PR #6 review). Do not market this layer as multi-user / proxy-safe
/// until that work lands.
pub fn require_auth_layer<S>(router: Router<S>, config: AuthConfig) -> Router<S>
where
    S: Clone + Send + Sync + 'static,
{
    let auth_enforced = config.is_enforced();
    let state = Arc::new(config);
    let router = router.layer(middleware::from_fn_with_state(state, auth_middleware));

    if !auth_enforced {
        return router;
    }

    // Peer-IP / local-first bucket. See the function doc-comment for the
    // proxy contract this intentionally does not provide.
    let governor_config = GovernorConfigBuilder::default()
        .per_millisecond(AUTH_RATE_LIMIT_REPLENISH_MS)
        .burst_size(AUTH_RATE_LIMIT_BURST)
        .finish()
        .expect("auth rate limit config is non-zero");

    router.layer(GovernorLayer::new(governor_config))
}

/// Operator-facing description of the rate-limit / proxy contract.
///
/// Returned as `Some(message)` when the operator binds to a non-loopback
/// address with auth enabled — exactly the configuration where a
/// reverse proxy is plausible and where the peer-IP bucket could be
/// silently shared across many real users. Returned as `None` when the
/// bind is loopback (rate-limit semantics match operator expectations).
pub fn proxy_rate_limit_warning(host: std::net::IpAddr) -> Option<&'static str> {
    if host.is_loopback() {
        None
    } else {
        Some(
            "Rate limit on /api/* is peer-IP / local-first and NOT proxy-aware. \
             Behind a reverse proxy every user shares the proxy's bucket, so a single \
             noisy client can starve others with 429. Proxy-aware key extraction \
             (trusted-header opt-in) is tracked as a follow-up.",
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    // Serialise env-var manipulation to avoid cross-test interference.
    static ENV_MUTEX: Mutex<()> = Mutex::new(());

    fn clear_env() {
        // Safety: the mutex guards concurrent access to process env across tests.
        unsafe {
            std::env::remove_var("AICX_HTTP_AUTH_TOKEN");
        }
    }

    #[test]
    fn test_proxy_rate_limit_warning_is_silent_on_loopback() {
        // PR #6 follow-up regression for the rate-limit/proxy contract:
        // local-first binds (the AICX default) must NOT emit the
        // proxy-shared-bucket warning — that would be noise and would
        // train operators to ignore it.
        let v4 = std::net::IpAddr::from([127u8, 0, 0, 1]);
        assert!(super::proxy_rate_limit_warning(v4).is_none());
        let v6 = std::net::IpAddr::from([0u16, 0, 0, 0, 0, 0, 0, 1]);
        assert!(super::proxy_rate_limit_warning(v6).is_none());
    }

    #[test]
    fn test_proxy_rate_limit_warning_fires_for_non_loopback_bind() {
        // The same path that gates non-loopback bind (must have auth +
        // explicit CORS) must surface the peer-IP / proxy limitation so
        // the operator does not assume multi-user safety behind a
        // reverse proxy.
        let v4 = std::net::IpAddr::from([0u8, 0, 0, 0]);
        let msg = super::proxy_rate_limit_warning(v4)
            .expect("non-loopback bind must emit proxy rate-limit warning");
        assert!(
            msg.contains("peer-IP") && msg.contains("proxy"),
            "warning must reference peer-IP / proxy contract: {msg}"
        );

        let tailscale = std::net::IpAddr::from([100u8, 75, 30, 90]);
        assert!(super::proxy_rate_limit_warning(tailscale).is_some());
    }

    #[test]
    fn test_generate_token_shape_and_uniqueness_sanity() {
        let first = generate_token().expect("generate first token");
        let second = generate_token().expect("generate second token");

        assert_eq!(first.len(), 64, "32 bytes hex-encoded = 64 chars");
        assert_eq!(second.len(), 64, "32 bytes hex-encoded = 64 chars");
        assert!(first.bytes().all(|byte| byte.is_ascii_hexdigit()));
        assert!(second.bytes().all(|byte| byte.is_ascii_hexdigit()));
        assert_ne!(first, second, "two CSPRNG tokens should differ");
    }

    #[test]
    fn test_load_auth_token_from_env() {
        let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
        clear_env();
        // Safety: guarded by ENV_MUTEX for the duration of this test.
        unsafe {
            std::env::set_var("AICX_HTTP_AUTH_TOKEN", "from-env-token");
        }
        let cfg = load_auth_config(None, true).expect("load env token");
        assert_eq!(cfg.token.as_deref(), Some("from-env-token"));
        assert_eq!(cfg.source, AuthSource::Env);
        clear_env();
    }

    #[test]
    fn test_load_auth_token_from_file_with_mode_0600() {
        let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
        clear_env();
        let tmp = std::env::temp_dir().join(format!(
            "aicx-auth-test-{}-{}.token",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
        ));
        std::fs::write(&tmp, "file-token-value\n").expect("write tmp token");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = std::fs::metadata(&tmp).expect("stat tmp").permissions();
            perms.set_mode(0o600);
            std::fs::set_permissions(&tmp, perms).expect("chmod tmp");
        }

        let token = std::fs::read_to_string(&tmp).expect("read tmp");
        assert_eq!(token.trim(), "file-token-value");
        assert!(constant_time_eq(
            token.trim().as_bytes(),
            b"file-token-value"
        ));

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&tmp)
                .expect("stat tmp")
                .permissions()
                .mode()
                & 0o777;
            assert_eq!(mode, 0o600, "file should be mode 0600");
        }
        let _ = std::fs::remove_file(&tmp);
    }

    #[test]
    fn test_load_auth_token_generates_when_missing() {
        let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
        clear_env();
        let tmp_dir = std::env::temp_dir().join(format!(
            "aicx-auth-gen-{}-{}",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
        ));
        let tmp_path = tmp_dir.join("auth-token");
        let token = generate_token().expect("generate token");
        assert_eq!(token.len(), 64, "32 bytes hex-encoded = 64 chars");
        persist_token_file(&tmp_path, &token).expect("persist token");
        assert!(tmp_path.exists());

        let on_disk = std::fs::read_to_string(&tmp_path).expect("read persisted");
        assert_eq!(on_disk.trim(), token);

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = std::fs::metadata(&tmp_path)
                .expect("stat persisted")
                .permissions()
                .mode()
                & 0o777;
            assert_eq!(mode, 0o600, "persisted token must be 0600");
        }
        let _ = std::fs::remove_file(&tmp_path);
        let _ = std::fs::remove_dir(&tmp_dir);
    }

    #[cfg(unix)]
    #[test]
    fn test_persist_token_file_adopts_existing_valid_token() {
        // PR #6 follow-up regression: simulate the startup race where
        // another process wrote a valid token between our existence
        // check and our `create_new(true)` attempt. The recovery path
        // MUST re-read the file and return `AdoptedExisting` instead of
        // aborting auth initialisation with `AlreadyExists`.
        let tmp_dir = std::env::temp_dir().join(format!(
            "aicx-auth-existing-{}-{}",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
        ));
        let tmp_path = tmp_dir.join("auth-token");
        std::fs::create_dir_all(&tmp_dir).expect("create tmp dir");
        std::fs::write(&tmp_path, "existing-token\n").expect("write existing token");

        let outcome = persist_token_file(&tmp_path, "replacement-token")
            .expect("AlreadyExists with valid content must recover via adoption");
        match outcome {
            TokenPersistOutcome::AdoptedExisting(token) => {
                assert_eq!(token, "existing-token");
            }
            other => panic!("expected AdoptedExisting, got {other:?}"),
        }
        // Existing content must not be clobbered when we adopt it.
        assert_eq!(
            std::fs::read_to_string(&tmp_path).expect("read existing token"),
            "existing-token\n"
        );

        let _ = std::fs::remove_file(&tmp_path);
        let _ = std::fs::remove_dir(&tmp_dir);
    }

    #[cfg(unix)]
    #[test]
    fn test_persist_token_file_overwrites_empty_existing_file() {
        // PR #6 follow-up regression: an empty / whitespace-only token
        // file is treated as unusable by load_auth_config, so it falls
        // through to generate + persist. The recovery path MUST
        // atomically replace the empty file with the fresh token (mode
        // 0600 preserved) and signal `Overwrote` so the caller surfaces
        // `AuthSource::Generated`.
        use std::os::unix::fs::PermissionsExt;

        let tmp_dir = std::env::temp_dir().join(format!(
            "aicx-auth-empty-{}-{}",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
        ));
        let tmp_path = tmp_dir.join("auth-token");
        std::fs::create_dir_all(&tmp_dir).expect("create tmp dir");
        // Whitespace-only is exactly the shape load_auth_config rejects.
        std::fs::write(&tmp_path, "   \n").expect("write empty token");

        let fresh = generate_token().expect("generate replacement token");
        let outcome = persist_token_file(&tmp_path, &fresh)
            .expect("empty token file must be atomically replaced");
        match outcome {
            TokenPersistOutcome::Overwrote => {}
            other => panic!("expected Overwrote, got {other:?}"),
        }

        let on_disk = std::fs::read_to_string(&tmp_path).expect("read replaced token");
        assert_eq!(on_disk.trim(), fresh);
        let mode = std::fs::metadata(&tmp_path)
            .expect("stat replaced token")
            .permissions()
            .mode()
            & 0o777;
        assert_eq!(
            mode, 0o600,
            "atomic replace must preserve mode 0600 on the destination"
        );

        // Sibling tempfiles must be cleaned up (no `.auth-token.tmp.*` left).
        let leftovers: Vec<_> = std::fs::read_dir(&tmp_dir)
            .expect("read tmp dir")
            .filter_map(|e| e.ok())
            .filter(|e| {
                e.file_name()
                    .to_string_lossy()
                    .starts_with(".auth-token.tmp.")
            })
            .collect();
        assert!(
            leftovers.is_empty(),
            "atomic replace left tempfiles behind: {leftovers:?}"
        );

        let _ = std::fs::remove_file(&tmp_path);
        let _ = std::fs::remove_dir(&tmp_dir);
    }

    #[cfg(unix)]
    #[test]
    fn test_persist_token_file_first_writer_returns_created() {
        // Happy path: target does not exist, we win create_new, outcome
        // is `Created`. Reaffirms that the recovery branch did not
        // silently take over the normal create path.
        let tmp_dir = std::env::temp_dir().join(format!(
            "aicx-auth-firstwriter-{}-{}",
            std::process::id(),
            chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
        ));
        let tmp_path = tmp_dir.join("auth-token");
        let fresh = generate_token().expect("generate token");
        let outcome = persist_token_file(&tmp_path, &fresh).expect("persist new token");
        match outcome {
            TokenPersistOutcome::Created => {}
            other => panic!("expected Created, got {other:?}"),
        }
        assert_eq!(
            std::fs::read_to_string(&tmp_path)
                .expect("read persisted")
                .trim(),
            fresh
        );
        let _ = std::fs::remove_file(&tmp_path);
        let _ = std::fs::remove_dir(&tmp_dir);
    }

    #[test]
    fn test_constant_time_compare_rejects_short_mismatch() {
        assert!(constant_time_eq(b"abc", b"abc"));
        assert!(!constant_time_eq(b"abc", b"abd"));
        assert!(!constant_time_eq(b"abc", b"ab"));
        assert!(!constant_time_eq(b"abc", b"abcd"));
        assert!(!constant_time_eq(b"", b"x"));
        assert!(constant_time_eq(b"", b""));
    }

    #[test]
    fn test_disabled_config_passes_through_in_middleware() {
        assert!(!AuthConfig::disabled().is_enforced());
    }

    #[test]
    fn test_cli_override_wins() {
        let _guard = ENV_MUTEX.lock().expect("env mutex poisoned");
        clear_env();
        // Safety: env access guarded by ENV_MUTEX.
        unsafe {
            std::env::set_var("AICX_HTTP_AUTH_TOKEN", "env-loser");
        }
        let cfg = load_auth_config(Some("cli-winner"), true).expect("cli override");
        assert_eq!(cfg.token.as_deref(), Some("cli-winner"));
        assert_eq!(cfg.source, AuthSource::Cli);
        clear_env();
    }
}