ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
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
//! Bridge API client implementation.
//!
//! Translated from openclaudecode/src/bridge/bridgeApi.ts

use crate::utils::http::get_user_agent;
use serde::{Deserialize, Serialize};

// =============================================================================
// CONSTANTS
// =============================================================================

const BETA_HEADER: &str = "environments-2025-11-01";
const EMPTY_POLL_LOG_INTERVAL: usize = 100;

// Safe pattern for server-provided IDs in URL paths
const SAFE_ID_PATTERN: &str = r"^[a-zA-Z0-9_-]+$";

// =============================================================================
// TYPES
// =============================================================================

/// Work data from the server
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkData {
    #[serde(rename = "type")]
    pub data_type: String,
    pub id: String,
}

/// Work response from poll endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkResponse {
    pub id: String,
    #[serde(rename = "type")]
    pub response_type: String,
    pub environment_id: String,
    pub state: String,
    pub data: WorkData,
    pub secret: String, // base64url-encoded JSON
    pub created_at: String,
}

/// Permission response event sent to a session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionResponseEvent {
    #[serde(rename = "type")]
    pub event_type: String,
    pub response: PermissionResponseInner,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionResponseInner {
    #[serde(rename = "subtype")]
    pub response_subtype: String,
    pub request_id: String,
    pub response: serde_json::Value,
}

/// Bridge configuration for environment registration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BridgeConfig {
    pub machine_name: String,
    pub dir: String,
    pub branch: String,
    #[serde(rename = "gitRepoUrl")]
    pub git_repo_url: Option<String>,
    #[serde(rename = "maxSessions")]
    pub max_sessions: u32,
    #[serde(rename = "bridgeId")]
    pub bridge_id: String,
    #[serde(rename = "workerType")]
    pub worker_type: String,
    #[serde(rename = "reuseEnvironmentId")]
    pub reuse_environment_id: Option<String>,
    #[serde(rename = "apiBaseUrl")]
    pub api_base_url: String,
}

/// Registration response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistrationResponse {
    #[serde(rename = "environment_id")]
    pub environment_id: String,
    #[serde(rename = "environment_secret")]
    pub environment_secret: String,
}

/// Heartbeat response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeartbeatResponse {
    #[serde(rename = "lease_extended")]
    pub lease_extended: bool,
    pub state: String,
}

// =============================================================================
// ERROR TYPES
// =============================================================================

/// Full error printed when `claude remote-control` is run without auth.
pub const BRIDGE_LOGIN_ERROR: &str = "Error: You must be logged in to use Remote Control.\n\n\
    Remote Control is only available with claude.ai subscriptions. Please use `/login` to \
    sign in with your claude.ai account.";

/// Reusable login guidance appended to bridge auth errors.
pub const BRIDGE_LOGIN_INSTRUCTION: &str = "Remote Control is only available with claude.ai \
    subscriptions. Please use `/login` to sign in with your claude.ai account.";

/// Fatal bridge errors that should not be retried
#[derive(Debug)]
pub struct BridgeFatalError {
    pub message: String,
    pub status: u16,
    pub error_type: Option<String>,
}

impl std::fmt::Display for BridgeFatalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for BridgeFatalError {}

impl BridgeFatalError {
    pub fn new(message: String, status: u16, error_type: Option<String>) -> Self {
        Self {
            message,
            status,
            error_type,
        }
    }
}

// =============================================================================
// API CLIENT
// =============================================================================

/// Bridge API client dependencies
pub struct BridgeApiDeps {
    pub base_url: String,
    pub get_access_token: Box<dyn Fn() -> Option<String> + Send + Sync>,
    pub runner_version: String,
    pub on_debug: Option<Box<dyn Fn(&str) + Send + Sync>>,
    /// Returns the trusted device token
    pub get_trusted_device_token: Option<Box<dyn Fn() -> Option<String> + Send + Sync>>,
}

impl Default for BridgeApiDeps {
    fn default() -> Self {
        Self {
            base_url: String::new(),
            get_access_token: Box::new(|| None),
            runner_version: String::new(),
            on_debug: None,
            get_trusted_device_token: None,
        }
    }
}

// Note: BridgeApiClient trait removed in favor of SyncBridgeApiClient struct

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

/// Validate that a server-provided ID is safe to interpolate into a URL path.
pub fn validate_bridge_id(id: &str, label: &str) -> Result<String, String> {
    if id.is_empty() || !Regex::new(SAFE_ID_PATTERN).unwrap().is_match(id) {
        return Err(format!("Invalid {}: contains unsafe characters", label));
    }
    Ok(id.to_string())
}

/// Check whether an error type string indicates a session/environment expiry.
pub fn is_expired_error_type(error_type: Option<&str>) -> bool {
    match error_type {
        Some(etype) => etype.contains("expired") || etype.contains("lifetime"),
        None => false,
    }
}

/// Check whether a BridgeFatalError is a suppressible 403 permission error.
pub fn is_suppressible_403(err: &BridgeFatalError) -> bool {
    if err.status != 403 {
        return false;
    }
    err.message.contains("external_poll_sessions") || err.message.contains("environments:manage")
}

fn extract_error_type_from_data(data: &serde_json::Value) -> Option<String> {
    if let Some(error) = data.get("error").and_then(|v| v.as_object()) {
        if let Some(etype) = error.get("type").and_then(|v| v.as_str()) {
            return Some(etype.to_string());
        }
    }
    None
}

fn extract_error_detail(data: &serde_json::Value) -> Option<String> {
    if let Some(msg) = data.get("message").and_then(|v| v.as_str()) {
        return Some(msg.to_string());
    }
    if let Some(error) = data.get("error").and_then(|v| v.as_object()) {
        if let Some(msg) = error.get("message").and_then(|v| v.as_str()) {
            return Some(msg.to_string());
        }
    }
    None
}

// =============================================================================
// SIMPLE SYNC IMPLEMENTATION (no async)
// =============================================================================

/// Synchronous bridge API client for simple use cases
pub struct SyncBridgeApiClient {
    base_url: String,
    get_access_token: Box<dyn Fn() -> Option<String> + Send + Sync>,
    runner_version: String,
    get_trusted_device_token: Option<Box<dyn Fn() -> Option<String> + Send + Sync>>,
    on_debug: Option<Box<dyn Fn(&str) + Send + Sync>>,
}

impl SyncBridgeApiClient {
    pub fn new(
        base_url: String,
        get_access_token: impl Fn() -> Option<String> + Send + Sync + 'static,
        runner_version: String,
    ) -> Self {
        Self {
            base_url,
            get_access_token: Box::new(get_access_token),
            runner_version,
            get_trusted_device_token: None,
            on_debug: None,
        }
    }

    pub fn with_trusted_device_token(
        mut self,
        getter: impl Fn() -> Option<String> + Send + Sync + 'static,
    ) -> Self {
        self.get_trusted_device_token = Some(Box::new(getter));
        self
    }

    pub fn with_debug(mut self, debug: impl Fn(&str) + Send + Sync + 'static) -> Self {
        self.on_debug = Some(Box::new(debug));
        self
    }

    fn debug(&self, msg: &str) {
        if let Some(ref debug) = self.on_debug {
            debug(msg);
        }
    }

    fn get_headers(&self, access_token: &str) -> reqwest::header::HeaderMap {
        let mut headers = reqwest::header::HeaderMap::new();
        headers.insert(
            reqwest::header::AUTHORIZATION,
            format!("Bearer {}", access_token).parse().unwrap(),
        );
        headers.insert(
            reqwest::header::CONTENT_TYPE,
            "application/json".parse().unwrap(),
        );
        headers.insert("anthropic-version", "2023-06-01".parse().unwrap());
        headers.insert("anthropic-beta", BETA_HEADER.parse().unwrap());
        headers.insert(
            "x-environment-runner-version",
            self.runner_version.parse().unwrap(),
        );
        headers.insert("User-Agent", get_user_agent().parse().unwrap());

        if let Some(ref getter) = self.get_trusted_device_token {
            if let Some(token) = getter() {
                headers.insert("X-Trusted-Device-Token", token.parse().unwrap());
            }
        }

        headers
    }

    fn resolve_auth(&self) -> Result<String, BridgeFatalError> {
        match (self.get_access_token)() {
            Some(token) => Ok(token),
            None => Err(BridgeFatalError::new(
                BRIDGE_LOGIN_INSTRUCTION.to_string(),
                401,
                None,
            )),
        }
    }

    /// Register this bridge environment
    pub fn register_bridge_environment(
        &self,
        config: BridgeConfig,
    ) -> Result<RegistrationResponse, String> {
        validate_bridge_id(&config.bridge_id, "bridgeId").map_err(|e| e.to_string())?;

        self.debug(&format!(
            "[bridge:api] POST /v1/environments/bridge bridgeId={}",
            config.bridge_id
        ));

        let client = reqwest::blocking::Client::new();
        let token = self.resolve_auth().map_err(|e| e.to_string())?;

        let mut body = serde_json::json!({
            "machine_name": config.machine_name,
            "directory": config.dir,
            "branch": config.branch,
            "git_repo_url": config.git_repo_url,
            "max_sessions": config.max_sessions,
            "metadata": { "worker_type": config.worker_type },
        });

        if let Some(reuse_id) = config.reuse_environment_id {
            body["environment_id"] = serde_json::json!(reuse_id);
        }

        let response = client
            .post(&format!("{}/v1/environments/bridge", self.base_url))
            .headers(self.get_headers(&token))
            .json(&body)
            .timeout(std::time::Duration::from_secs(15))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 201 {
            return Err(handle_error_status_sync(status, &data, "Registration"));
        }

        let result: RegistrationResponse = serde_json::from_value(data.clone())
            .map_err(|e| format!("Failed to parse response: {}", e))?;

        self.debug(&format!(
            "[bridge:api] POST /v1/environments/bridge -> {} environment_id={}",
            status, result.environment_id
        ));

        Ok(result)
    }

    /// Poll for work from the environment
    pub fn poll_for_work(
        &self,
        environment_id: &str,
        environment_secret: &str,
        reclaim_older_than_ms: Option<u64>,
    ) -> Result<Option<WorkResponse>, String> {
        validate_bridge_id(environment_id, "environmentId")?;

        let client = reqwest::blocking::Client::new();

        let mut url = format!(
            "{}/v1/environments/{}/work/poll",
            self.base_url, environment_id
        );
        if let Some(ms) = reclaim_older_than_ms {
            url = format!("{}?reclaim_older_than_ms={}", url, ms);
        }

        let response = client
            .get(&url)
            .header("Authorization", format!("Bearer {}", environment_secret))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or(serde_json::Value::Null);

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "Poll"));
        }

        if data.is_null() || data.is_array() {
            return Ok(None);
        }

        let work: WorkResponse = serde_json::from_value(data.clone())
            .map_err(|e| format!("Failed to parse response: {}", e))?;

        self.debug(&format!(
            "[bridge:api] GET .../work/poll -> {} workId={} type={}",
            status, work.id, work.data.data_type
        ));

        Ok(Some(work))
    }

    /// Acknowledge work receipt
    pub fn acknowledge_work(
        &self,
        environment_id: &str,
        work_id: &str,
        session_token: &str,
    ) -> Result<(), String> {
        validate_bridge_id(environment_id, "environmentId")?;
        validate_bridge_id(work_id, "workId")?;

        self.debug(&format!("[bridge:api] POST .../work/{}/ack", work_id));

        let client = reqwest::blocking::Client::new();

        let response = client
            .post(&format!(
                "{}/v1/environments/{}/work/{}/ack",
                self.base_url, environment_id, work_id
            ))
            .headers(self.get_headers(session_token))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "Acknowledge"));
        }

        Ok(())
    }

    /// Stop a work item
    pub fn stop_work(
        &self,
        environment_id: &str,
        work_id: &str,
        force: bool,
    ) -> Result<(), String> {
        validate_bridge_id(environment_id, "environmentId")?;
        validate_bridge_id(work_id, "workId")?;

        self.debug(&format!(
            "[bridge:api] POST .../work/{}/stop force={}",
            work_id, force
        ));

        let client = reqwest::blocking::Client::new();
        let token = self.resolve_auth().map_err(|e| e.to_string())?;

        let response = client
            .post(&format!(
                "{}/v1/environments/{}/work/{}/stop",
                self.base_url, environment_id, work_id
            ))
            .headers(self.get_headers(&token))
            .json(&serde_json::json!({ "force": force }))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "StopWork"));
        }

        Ok(())
    }

    /// Deregister the environment
    pub fn deregister_environment(&self, environment_id: &str) -> Result<(), String> {
        validate_bridge_id(environment_id, "environmentId")?;

        self.debug(&format!(
            "[bridge:api] DELETE /v1/environments/bridge/{}",
            environment_id
        ));

        let client = reqwest::blocking::Client::new();
        let token = self.resolve_auth().map_err(|e| e.to_string())?;

        let response = client
            .delete(&format!(
                "{}/v1/environments/bridge/{}",
                self.base_url, environment_id
            ))
            .headers(self.get_headers(&token))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "Deregister"));
        }

        Ok(())
    }

    /// Archive a session
    pub fn archive_session(&self, session_id: &str) -> Result<(), String> {
        validate_bridge_id(session_id, "sessionId")?;

        self.debug(&format!(
            "[bridge:api] POST /v1/sessions/{}/archive",
            session_id
        ));

        let client = reqwest::blocking::Client::new();
        let token = self.resolve_auth().map_err(|e| e.to_string())?;

        let response = client
            .post(&format!(
                "{}/v1/sessions/{}/archive",
                self.base_url, session_id
            ))
            .headers(self.get_headers(&token))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        // 409 = already archived (idempotent, not an error)
        if status == 409 {
            self.debug(&format!(
                "[bridge:api] POST /v1/sessions/{}/archive -> 409 (already archived)",
                session_id
            ));
            return Ok(());
        }

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "ArchiveSession"));
        }

        Ok(())
    }

    /// Reconnect a session
    pub fn reconnect_session(&self, environment_id: &str, session_id: &str) -> Result<(), String> {
        validate_bridge_id(environment_id, "environmentId")?;
        validate_bridge_id(session_id, "sessionId")?;

        self.debug(&format!(
            "[bridge:api] POST /v1/environments/{}/bridge/reconnect session_id={}",
            environment_id, session_id
        ));

        let client = reqwest::blocking::Client::new();
        let token = self.resolve_auth().map_err(|e| e.to_string())?;

        let response = client
            .post(&format!(
                "{}/v1/environments/{}/bridge/reconnect",
                self.base_url, environment_id
            ))
            .headers(self.get_headers(&token))
            .json(&serde_json::json!({ "session_id": session_id }))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "ReconnectSession"));
        }

        Ok(())
    }

    /// Send heartbeat for a work item
    pub fn heartbeat_work(
        &self,
        environment_id: &str,
        work_id: &str,
        session_token: &str,
    ) -> Result<HeartbeatResponse, String> {
        validate_bridge_id(environment_id, "environmentId")?;
        validate_bridge_id(work_id, "workId")?;

        self.debug(&format!("[bridge:api] POST .../work/{}/heartbeat", work_id));

        let client = reqwest::blocking::Client::new();

        let response = client
            .post(&format!(
                "{}/v1/environments/{}/work/{}/heartbeat",
                self.base_url, environment_id, work_id
            ))
            .headers(self.get_headers(session_token))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(status, &data, "Heartbeat"));
        }

        let result: HeartbeatResponse = serde_json::from_value(data.clone())
            .map_err(|e| format!("Failed to parse response: {}", e))?;

        self.debug(&format!(
            "[bridge:api] POST .../work/{}/heartbeat -> {} lease_extended={} state={}",
            work_id, status, result.lease_extended, result.state
        ));

        Ok(result)
    }

    /// Send permission response event
    pub fn send_permission_response_event(
        &self,
        session_id: &str,
        event: PermissionResponseEvent,
        session_token: &str,
    ) -> Result<(), String> {
        validate_bridge_id(session_id, "sessionId")?;

        self.debug(&format!(
            "[bridge:api] POST /v1/sessions/{}/events type={}",
            session_id, event.event_type
        ));

        let client = reqwest::blocking::Client::new();

        let response = client
            .post(&format!(
                "{}/v1/sessions/{}/events",
                self.base_url, session_id
            ))
            .headers(self.get_headers(session_token))
            .json(&serde_json::json!({ "events": [event] }))
            .timeout(std::time::Duration::from_secs(10))
            .send()
            .map_err(|e| e.to_string())?;

        let status = response.status().as_u16();
        let data: serde_json::Value = response.json().unwrap_or_default();

        if status != 200 && status != 204 {
            return Err(handle_error_status_sync(
                status,
                &data,
                "SendPermissionResponseEvent",
            ));
        }

        Ok(())
    }
}

fn handle_error_status_sync(status: u16, data: &serde_json::Value, context: &str) -> String {
    let detail = extract_error_detail(data);
    let error_type = extract_error_type_from_data(data);

    match status {
        401 => format!(
            "{}: Authentication failed (401){}. {}",
            context,
            detail.map(|d| format!(": {}", d)).unwrap_or_default(),
            BRIDGE_LOGIN_INSTRUCTION
        ),
        403 => {
            if is_expired_error_type(error_type.as_deref()) {
                "Remote Control session has expired. Please restart with `claude remote-control` or /remote-control.".to_string()
            } else {
                format!(
                    "{}: Access denied (403){}. Check your organization permissions.",
                    context,
                    detail.map(|d| format!(": {}", d)).unwrap_or_default()
                )
            }
        }
        404 => detail.unwrap_or_else(|| {
            format!(
                "{}: Not found (404). Remote Control may not be available for this organization.",
                context
            )
        }),
        410 => detail.unwrap_or_else(|| {
            "Remote Control session has expired. Please restart with `claude remote-control` or /remote-control.".to_string()
        }),
        429 => format!("{}: Rate limited (429). Polling too frequently.", context),
        _ => format!(
            "{}: Failed with status {}{}",
            context,
            status,
            detail.map(|d| format!(": {}", d)).unwrap_or_default()
        ),
    }
}

// =============================================================================
// REGEX
// =============================================================================

use regex::Regex;

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

    #[test]
    fn test_validate_bridge_id() {
        assert!(validate_bridge_id("abc123", "test").is_ok());
        assert!(validate_bridge_id("abc-def_123", "test").is_ok());
        assert!(validate_bridge_id("", "test").is_err());
        assert!(validate_bridge_id("../admin", "test").is_err());
        assert!(validate_bridge_id("abc/def", "test").is_err());
    }

    #[test]
    fn test_is_expired_error_type() {
        assert!(is_expired_error_type(Some("session_expired")));
        assert!(is_expired_error_type(Some("environment_lifetime")));
        assert!(!is_expired_error_type(Some("other_error")));
        assert!(!is_expired_error_type(None));
    }

    #[test]
    fn test_is_suppressible_403() {
        let err = BridgeFatalError::new(
            "403: external_poll_sessions not allowed".to_string(),
            403,
            None,
        );
        assert!(is_suppressible_403(&err));

        let err2 = BridgeFatalError::new("403: Some other error".to_string(), 403, None);
        assert!(!is_suppressible_403(&err2));

        let err3 = BridgeFatalError::new("401: Unauthorized".to_string(), 401, None);
        assert!(!is_suppressible_403(&err3));
    }
}