portail 2.1.0

Unified proxy/gateway: AI Gateway + MCP Gateway + CDN cache
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
//! Drift detect — production traffic replay for regression testing.
//!
//! v0.4 — Captures real request/response pairs, replays them in CI,
//! compares SHA-256 of responses. Posts diff report as PR comment.
//! Advisory only — never fails CI.
//!
//! # Workflow
//!
//! 1. **Capture**: `portail drift-detect capture --url http://localhost:8787`
//!    → records requests to `.drift/session-{timestamp}.json`
//! 2. **Replay**: `portail drift-detect replay`
//!    → sends captured requests to proxy, compares responses
//! 3. **CI mode**: `portail drift-detect --ci`
//!    → replays + writes `drift-report.toml`, always exits 0

use anyhow::Result;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

// ─── data model ───────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftSession {
    pub captured_at: String,
    pub source_url: String,
    pub entries: Vec<DriftEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftEntry {
    pub method: String,
    pub path: String,
    pub request_headers: Vec<(String, String)>,
    pub request_body: String,
    pub response_status: u16,
    pub response_headers: Vec<(String, String)>,
    pub response_body: String,
    pub response_sha256: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftReport {
    pub generated_at: String,
    pub total: usize,
    pub matched: usize,
    pub drifted: usize,
    pub errors: usize,
    pub entries: Vec<DriftDiff>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DriftStatus {
    #[serde(rename = "match")]
    Match,
    #[serde(rename = "drifted")]
    Drifted,
    #[serde(rename = "error")]
    Error,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DriftDiff {
    pub method: String,
    pub path: String,
    pub status: DriftStatus,
    pub original_sha256: String,
    pub replay_sha256: Option<String>,
    pub detail: Option<String>,
}

// ─── defaults ─────────────────────────────────────────────────────

const DRIFT_DIR: &str = ".drift";

// ─── capture ──────────────────────────────────────────────────────

/// Capture requests by sending pre-defined probes to a running proxy.
pub fn capture(url: &str) -> Result<DriftSession> {
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(10))
        .build()?;

    let probes: Vec<DriftEntry> = vec![
        probe(&client, url, "GET", "/healthz", "")?,
        probe(&client, url, "GET", "/readyz", "")?,
        probe(&client, url, "GET", "/metrics", "")?,
        probe(&client, url, "GET", "/.well-known/agent.json", "")?,
        probe(&client, url, "GET", "/stats", "")?,
        probe_json(
            &client,
            url,
            "POST",
            "/v1/chat/completions",
            r#"{"model":"test","messages":[{"role":"user","content":"hi"}]}"#,
        )?,
    ];

    let _now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();

    Ok(DriftSession {
        captured_at: chrono::Utc::now().to_rfc3339(),
        source_url: url.to_string(),
        entries: probes,
    })
}

fn probe(
    client: &reqwest::blocking::Client,
    base: &str,
    method: &str,
    path: &str,
    _body: &str,
) -> Result<DriftEntry> {
    let url = format!("{}{}", base, path);
    let resp = match method {
        "GET" => client.get(&url).send()?,
        _ => client.get(&url).send()?,
    };

    let status = resp.status().as_u16();
    let resp_headers: Vec<(String, String)> = resp
        .headers()
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
        .collect();
    let resp_body = resp.text()?;
    let sha = sha256_hex(&resp_body);

    Ok(DriftEntry {
        method: method.to_string(),
        path: path.to_string(),
        request_headers: vec![],
        request_body: String::new(),
        response_status: status,
        response_headers: resp_headers,
        response_body: resp_body,
        response_sha256: sha,
    })
}

fn probe_json(
    client: &reqwest::blocking::Client,
    base: &str,
    method: &str,
    path: &str,
    body: &str,
) -> Result<DriftEntry> {
    let url = format!("{}{}", base, path);
    let resp = client
        .post(&url)
        .header("content-type", "application/json")
        .body(body.to_string())
        .send()?;

    let status = resp.status().as_u16();
    let resp_headers: Vec<(String, String)> = resp
        .headers()
        .iter()
        .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
        .collect();
    let resp_body = resp.text()?;
    let sha = sha256_hex(&resp_body);

    Ok(DriftEntry {
        method: method.to_string(),
        path: path.to_string(),
        request_headers: vec![],
        request_body: body.to_string(),
        response_status: status,
        response_headers: resp_headers,
        response_body: resp_body,
        response_sha256: sha,
    })
}

// ─── replay ───────────────────────────────────────────────────────

/// Replay a session against a running proxy and produce a diff report.
pub fn replay(url: &str, session: &DriftSession) -> Result<DriftReport> {
    let client = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(10))
        .build()?;

    let mut diffs = Vec::new();
    let mut matched = 0;
    let mut drifted = 0;
    let mut errors = 0;

    for entry in &session.entries {
        let result = replay_entry(&client, url, entry);
        match result {
            Ok(diff) => {
                match diff.status {
                    DriftStatus::Match => matched += 1,
                    DriftStatus::Drifted => drifted += 1,
                    DriftStatus::Error => errors += 1,
                }
                diffs.push(diff);
            }
            Err(e) => {
                errors += 1;
                diffs.push(DriftDiff {
                    method: entry.method.clone(),
                    path: entry.path.clone(),
                    status: DriftStatus::Error,
                    original_sha256: entry.response_sha256.clone(),
                    replay_sha256: None,
                    detail: Some(format!("{}", e)),
                });
            }
        }
    }

    Ok(DriftReport {
        generated_at: chrono::Utc::now().to_rfc3339(),
        total: session.entries.len(),
        matched,
        drifted,
        errors,
        entries: diffs,
    })
}

fn replay_entry(
    client: &reqwest::blocking::Client,
    base: &str,
    entry: &DriftEntry,
) -> Result<DriftDiff> {
    let url = format!("{}{}", base, entry.path);
    let resp = match entry.method.as_str() {
        "GET" => client.get(&url).send()?,
        "POST" => client
            .post(&url)
            .header("content-type", "application/json")
            .body(entry.request_body.clone())
            .send()?,
        _ => client.get(&url).send()?,
    };

    let resp_body = resp.text()?;
    let replay_sha = sha256_hex(&resp_body);

    let status = if replay_sha == entry.response_sha256 {
        DriftStatus::Match
    } else {
        DriftStatus::Drifted
    };

    Ok(DriftDiff {
        method: entry.method.clone(),
        path: entry.path.clone(),
        status,
        original_sha256: entry.response_sha256.clone(),
        replay_sha256: Some(replay_sha),
        detail: None,
    })
}

// ─── persistence ──────────────────────────────────────────────────

/// Save a session to the drift directory.
pub fn save_session(session: &DriftSession) -> Result<PathBuf> {
    let dir = Path::new(DRIFT_DIR);
    std::fs::create_dir_all(dir)?;

    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let path = dir.join(format!("session-{}.json", ts));
    let json = serde_json::to_string_pretty(session)?;
    std::fs::write(&path, json)?;
    eprintln!("drift-detect: saved session to {}", path.display());
    Ok(path)
}

/// Load the most recent session from the drift directory.
pub fn load_latest_session() -> Result<DriftSession> {
    let dir = Path::new(DRIFT_DIR);
    let mut sessions: Vec<_> = std::fs::read_dir(dir)?
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "json"))
        .collect();
    sessions.sort_by_key(|e| e.path());
    let latest = sessions
        .last()
        .ok_or_else(|| anyhow::anyhow!("no drift sessions found in {}", DRIFT_DIR))?;
    let json = std::fs::read_to_string(latest.path())?;
    Ok(serde_json::from_str(&json)?)
}

// ─── CI mode ──────────────────────────────────────────────────────

/// Run capture + replay in CI mode. Always exits 0.
pub fn ci_run(url: &str) -> Result<String> {
    let session = match capture(url) {
        Ok(s) => {
            save_session(&s)?;
            s
        }
        Err(e) => {
            let report = DriftReport {
                generated_at: chrono::Utc::now().to_rfc3339(),
                total: 0,
                matched: 0,
                drifted: 0,
                errors: 1,
                entries: vec![DriftDiff {
                    method: "N/A".into(),
                    path: "N/A".into(),
                    status: DriftStatus::Error,
                    original_sha256: String::new(),
                    replay_sha256: None,
                    detail: Some(format!("capture failed: {}", e)),
                }],
            };
            let toml = toml::to_string_pretty(&report).unwrap_or_default();
            std::fs::write("drift-report.toml", &toml)?;
            return Ok(toml);
        }
    };

    let report = replay(url, &session).unwrap_or_else(|e| DriftReport {
        generated_at: chrono::Utc::now().to_rfc3339(),
        total: 0,
        matched: 0,
        drifted: 0,
        errors: 1,
        entries: vec![DriftDiff {
            method: "N/A".into(),
            path: "N/A".into(),
            status: DriftStatus::Error,
            original_sha256: String::new(),
            replay_sha256: None,
            detail: Some(format!("replay failed: {}", e)),
        }],
    });

    let toml = toml::to_string_pretty(&report).unwrap_or_default();
    std::fs::write("drift-report.toml", &toml)?;

    // Print summary for CI log
    println!(
        "drift-detect: {} total | {} matched | {} drifted | {} errors",
        report.total, report.matched, report.drifted, report.errors
    );

    Ok(toml)
}

fn sha256_hex(data: &str) -> String {
    use sha2::{Digest, Sha256};
    let mut hasher = Sha256::new();
    hasher.update(data.as_bytes());
    hex::encode(hasher.finalize())
}

// ─── CLI command ──────────────────────────────────────────────────

#[derive(clap::Subcommand, Debug, Clone)]
pub enum DriftCommand {
    /// Capture a drift session from a running proxy
    Capture {
        #[arg(long, default_value = "http://localhost:8787")]
        url: String,
    },
    /// Replay the latest captured session
    Replay {
        #[arg(long, default_value = "http://localhost:8787")]
        url: String,
    },
}

pub fn run(command: &DriftCommand, ci: bool) -> Result<()> {
    match command {
        DriftCommand::Capture { url } => {
            let session = capture(url)?;
            save_session(&session)?;
            if ci {
                std::fs::write(
                    "drift-session.json",
                    &serde_json::to_string_pretty(&session)?,
                )?;
            }
            println!(
                "drift-detect: captured {} probes from {}",
                session.entries.len(),
                url
            );
            Ok(())
        }
        DriftCommand::Replay { url } => {
            let session = load_latest_session()?;
            let report = replay(url, &session)?;
            if ci {
                std::fs::write("drift-report.toml", &toml::to_string_pretty(&report)?)?;
            }
            println!(
                "drift-detect: {} total | {} matched | {} drifted | {} errors",
                report.total, report.matched, report.drifted, report.errors
            );
            Ok(())
        }
    }
}

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

    #[test]
    fn sha256_is_stable() {
        let a = sha256_hex("hello");
        let b = sha256_hex("hello");
        assert_eq!(a, b);
        assert_eq!(a.len(), 64);
    }

    #[test]
    fn drift_entry_roundtrip_json() {
        let entry = DriftEntry {
            method: "GET".into(),
            path: "/healthz".into(),
            request_headers: vec![],
            request_body: String::new(),
            response_status: 200,
            response_headers: vec![],
            response_body: "ok".into(),
            response_sha256: sha256_hex("ok"),
        };
        let json = serde_json::to_string(&entry).unwrap();
        let back: DriftEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(back.response_sha256, sha256_hex("ok"));
    }

    #[test]
    fn drift_report_serializes() {
        let report = DriftReport {
            generated_at: "2026-01-01".into(),
            total: 1,
            matched: 1,
            drifted: 0,
            errors: 0,
            entries: vec![DriftDiff {
                method: "GET".into(),
                path: "/healthz".into(),
                status: DriftStatus::Match,
                original_sha256: "abc".into(),
                replay_sha256: Some("abc".into()),
                detail: None,
            }],
        };
        let toml = toml::to_string_pretty(&report).unwrap();
        assert!(toml.contains("match"));
    }
}