vtcode-core 0.182.7

Core library for VT Code - a Rust-based terminal coding agent
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
use super::*;
use crate::config::constants::tools;
use crate::config::types::CapabilityLevel;
use crate::tools::registry::{ExecutionPolicySnapshot, ToolRegistration};
use crate::tools::traits::Tool;
use anyhow::Result;
use async_trait::async_trait;
use serde_json::json;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tempfile::TempDir;

const PROBE_NAME: &str = "request_attempt_probe";

struct AttemptProbe {
    calls: Arc<AtomicUsize>,
    failures_before_success: usize,
}

#[async_trait]
impl Tool for AttemptProbe {
    async fn execute(&self, args: Value) -> Result<Value> {
        let call = self.calls.fetch_add(1, Ordering::SeqCst) + 1;
        if call <= self.failures_before_success {
            return Ok(json!({"error": {
                "tool_name": PROBE_NAME,
                "error_type": "NetworkError",
                "category": "Network",
                "message": "connection reset by peer",
                "retryable": true,
                "is_recoverable": true,
                "retry_after_ms": 1
            }}));
        }
        Ok(json!({"call": call, "input": args["input"]}))
    }

    fn name(&self) -> &str {
        PROBE_NAME
    }

    fn description(&self) -> &str {
        "Counts request attempts and returns controlled transient failures"
    }
}

async fn register_probe(registry: &ToolRegistry, failures_before_success: usize) -> Result<Arc<AtomicUsize>> {
    let calls = Arc::new(AtomicUsize::new(0));
    registry
        .register_tool(ToolRegistration::from_tool_instance(
            PROBE_NAME,
            CapabilityLevel::CodeSearch,
            AttemptProbe { calls: Arc::clone(&calls), failures_before_success },
        ))
        .await?;
    registry.allow_all_tools().await?;
    Ok(calls)
}

#[tokio::test]
async fn web_search_and_aliases_never_use_generic_automatic_retries() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = Arc::new(AtomicUsize::new(0));
    registry
        .register_tool(
            ToolRegistration::from_tool_instance(
                tools::WEB_SEARCH,
                CapabilityLevel::Basic,
                AttemptProbe {
                    calls: Arc::clone(&calls),
                    failures_before_success: usize::MAX,
                },
            )
            .with_aliases(["search_web", "websearch"]),
        )
        .await?;
    registry.allow_all_tools().await?;
    for (index, name) in [tools::WEB_SEARCH, "search_web", "websearch", "functions.web_search"]
        .iter()
        .enumerate()
    {
        let outcome = registry
            .execute_public_tool_request(
                ToolExecutionRequest::new(*name, json!({"query":"rust"})).with_policy(retry_policy(5)),
            )
            .await;
        assert!(!outcome.is_success());
        assert_eq!(outcome.attempts, 1);
        assert_eq!(calls.load(Ordering::SeqCst), index + 1);
    }
    Ok(())
}

#[tokio::test]
async fn web_fetch_aliases_share_canonical_retry_limit() -> Result<()> {
    for name in [tools::WEB_FETCH, "fetch_url", "web", "functions.web_fetch"] {
        let workspace = TempDir::new()?;
        let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
        let calls = Arc::new(AtomicUsize::new(0));
        registry
            .register_tool(
                ToolRegistration::from_tool_instance(
                    tools::WEB_FETCH,
                    CapabilityLevel::Basic,
                    AttemptProbe {
                        calls: Arc::clone(&calls),
                        failures_before_success: usize::MAX,
                    },
                )
                .with_aliases(["fetch_url", "web"]),
            )
            .await?;
        registry.allow_all_tools().await?;
        let outcome = registry
            .execute_public_tool_request(
                ToolExecutionRequest::new(name, json!({"url": "https://example.com"})).with_policy(retry_policy(5)),
            )
            .await;
        assert!(!outcome.is_success(), "{name}");
        assert_eq!(outcome.attempts, 2, "{name}");
        assert_eq!(calls.load(Ordering::SeqCst), 2, "{name}");
    }
    Ok(())
}

fn retry_policy(max_retries: usize) -> ExecutionPolicySnapshot {
    ExecutionPolicySnapshot {
        max_retries,
        retry_base_delay: Duration::from_millis(1),
        retry_max_delay: Duration::from_millis(1),
        retry_multiplier: 1.0,
        ..Default::default()
    }
}

#[tokio::test]
async fn successful_first_attempt_preserves_output_and_metadata() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = register_probe(&registry, 0).await?;
    let outcome = registry
        .execute_public_tool_request(ToolExecutionRequest::new(PROBE_NAME, json!({"input": "first"})))
        .await;
    assert!(outcome.is_success());
    assert_eq!(outcome.attempts, 1);
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert_eq!(outcome.output.expect("output")["input"], "first");
    assert_eq!(outcome.last_error_category, None);
    assert_eq!(registry.execution_history.get_recent_records(10).len(), 1);
    Ok(())
}

#[tokio::test]
async fn structured_failure_recovers_on_second_attempt() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = register_probe(&registry, 1).await?;
    let outcome = registry
        .execute_public_tool_request(
            ToolExecutionRequest::new(PROBE_NAME, json!({"input": "recover"})).with_policy(retry_policy(1)),
        )
        .await;
    assert!(outcome.is_success(), "{:?}", outcome.error);
    assert_eq!(outcome.attempts, 2);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    let output = outcome.output.expect("recovered output");
    assert_eq!(output["call"], 2);
    assert_eq!(output["input"], "recover");
    assert_eq!(outcome.last_error_category, Some(ErrorCategory::Network));
    let records = registry.execution_history.get_recent_records(10);
    assert_eq!(records.len(), 2);
    assert_eq!(records.iter().filter(|record| record.success).count(), 1);
    Ok(())
}

#[tokio::test]
async fn exhausted_retry_budget_preserves_last_error_context() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = register_probe(&registry, 2).await?;
    let outcome = registry
        .execute_public_tool_request(
            ToolExecutionRequest::new(PROBE_NAME, json!({"input": "exhaust"})).with_policy(retry_policy(1)),
        )
        .await;
    assert!(!outcome.is_success());
    assert_eq!(outcome.attempts, 2);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    assert!(outcome.output.is_none());
    assert_eq!(outcome.last_error_category, Some(ErrorCategory::Network));
    let error = outcome.error.expect("terminal error");
    assert_eq!(error.tool_name, PROBE_NAME);
    assert_eq!(error.message, "connection reset by peer");
    assert_eq!(error.attempts_made(), Some(2));
    assert_eq!(error.debug_context.expect("context").surface.as_deref(), Some("tool_registry"));
    Ok(())
}

#[tokio::test]
async fn dispatch_failure_remains_terminal_despite_retry_budget() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let outcome = registry
        .execute_public_tool_request(
            ToolExecutionRequest::new("unknown_attempt_tool", json!({})).with_policy(retry_policy(3)),
        )
        .await;
    assert!(!outcome.is_success());
    assert_eq!(outcome.attempts, 1);
    assert!(outcome.output.is_none());
    let error = outcome.error.expect("dispatch error");
    assert_eq!(error.tool_name, "unknown_attempt_tool");
    assert!(!error.retryable);
    assert_eq!(error.debug_context.expect("context").surface.as_deref(), Some("tool_registry"));
    assert!(registry.exec_sessions.list_sessions().await.is_empty());
    Ok(())
}

#[tokio::test]
async fn safety_denial_categories_keep_recovery_and_retry_metadata() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    struct DenialCase {
        violation: GatewaySafetyError,
        category: ErrorCategory,
        retryable: bool,
        recoverable: bool,
    }
    let cases = [
        DenialCase {
            violation: GatewaySafetyError::RateLimitExceeded { current: 2, max: 1, window: "1s" },
            category: ErrorCategory::RateLimit,
            retryable: true,
            recoverable: true,
        },
        DenialCase {
            violation: GatewaySafetyError::PlanningPolicyViolation("write blocked".to_string()),
            category: ErrorCategory::PlanningPolicyViolation,
            retryable: false,
            recoverable: true,
        },
    ];
    for case in cases {
        let error = registry.safety_denial_error(
            PROBE_NAME,
            "bounded denial",
            Some(case.violation),
            Some(Duration::from_millis(25)),
        );
        assert_eq!(error.category, case.category);
        assert_eq!(error.retryable, case.retryable);
        assert_eq!(error.is_recoverable, case.recoverable);
        assert_eq!(error.retry_after_ms, Some(25));
        assert_eq!(error.message, "Safety gateway denied execution: bounded denial");
        assert_eq!(error.to_json_value()["error"]["retry_after_ms"], 25);
    }
    Ok(())
}

#[tokio::test]
async fn unsandboxed_request_requires_approval_even_when_prevalidated() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    registry.allow_all_tools().await?;
    for permission in ["require_escalated", "BYPASS_SANDBOX"] {
        let outcome = registry
            .execute_public_tool_request(
                ToolExecutionRequest::new(
                    tools::EXEC_COMMAND,
                    json!({"cmd": "touch must_not_exist", "sandbox_permissions": permission}),
                )
                .with_policy(retry_policy(3).with_prevalidated(true).with_safety_prevalidated(true)),
            )
            .await;
        assert!(!outcome.is_success());
        assert_eq!(outcome.attempts, 1);
        let error = outcome.error.expect("approval error");
        assert_eq!(error.error_type, ToolErrorType::PolicyViolation);
        assert!(error.message.contains("enforced operator approval decision"));
    }
    assert!(!workspace.path().join("must_not_exist").exists());
    assert!(registry.exec_sessions.list_sessions().await.is_empty());
    assert!(registry.execution_history.get_recent_records(10).is_empty());
    Ok(())
}

#[tokio::test]
async fn invocation_approval_survives_retry_but_not_a_separate_call() -> Result<()> {
    use crate::tool_policy::ToolPolicy;
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = register_probe(&registry, 1).await?;
    registry.set_tool_policy(PROBE_NAME, ToolPolicy::Prompt).await?;
    registry.mark_tool_preapproved(PROBE_NAME).await;
    let request = || ToolExecutionRequest::new(PROBE_NAME, json!({"input":"approved"})).with_policy(retry_policy(2));
    let outcome = registry.execute_public_tool_request(request()).await;
    assert!(outcome.is_success(), "{:?}", outcome.error);
    assert_eq!(outcome.attempts, 2);
    for args in [json!({"input":"approved"}), json!({"input":"changed"})] {
        let denied = registry
            .execute_public_tool_request(ToolExecutionRequest::new(PROBE_NAME, args).with_policy(retry_policy(2)))
            .await;
        assert!(!denied.is_success());
        assert_eq!(denied.attempts, 1);
    }
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

struct HttpProbe {
    calls: Arc<AtomicUsize>,
    status: u16,
    retry_after_ms: u64,
    failures: usize,
    policy_change: Option<Arc<super::super::policy::ToolPolicyGateway>>,
}

#[async_trait]
impl Tool for HttpProbe {
    async fn execute(&self, args: Value) -> Result<Value> {
        let url = url::Url::parse(args["url"].as_str().unwrap())?;
        let budget = crate::tools::web_fetch::request_budget::RequestBudget::current();
        let _transport = match &budget {
            Some(budget) => Some(budget.lock_transport().await?),
            None => None,
        };
        if let Some(budget) = &budget
            && let Err(error) = budget.admit(&url)
        {
            return Ok(error.to_json_value());
        }
        let call = self.calls.fetch_add(1, Ordering::SeqCst) + 1;
        if let Some(registry) = &self.policy_change {
            registry
                .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Deny)
                .await?;
        }
        if call <= self.failures {
            let output = json!({"error":format!("HTTP request failed with status: {}", self.status),
                "error_type":"http_error", "http_status":self.status,
                "retry_after_ms":self.retry_after_ms, "next_action":"Disclose unavailable external evidence."});
            match budget.as_ref() {
                Some(budget) => Ok(budget
                    .observe(&url, ToolExecutionError::from_tool_output(&output).unwrap())
                    .to_json_value()),
                None => Ok(output),
            }
        } else {
            Ok(json!({"preview":"remote text", "call":call}))
        }
    }
    fn name(&self) -> &str {
        tools::WEB_FETCH
    }
    fn description(&self) -> &str {
        "Controlled HTTP fetch outcome without external network access"
    }
}

async fn http_probe(
    registry: &ToolRegistry,
    status: u16,
    delay: u64,
    failures: usize,
    change_policy: bool,
) -> Result<Arc<AtomicUsize>> {
    let calls = Arc::new(AtomicUsize::new(0));
    registry
        .register_tool(ToolRegistration::from_tool_instance(
            tools::WEB_FETCH,
            CapabilityLevel::CodeSearch,
            HttpProbe {
                calls: calls.clone(),
                status,
                retry_after_ms: delay,
                failures,
                policy_change: change_policy.then(|| registry.policy_gateway.clone()),
            },
        ))
        .await?;
    registry.allow_all_tools().await?;
    Ok(calls)
}

fn fetch_request(url: &str) -> ToolExecutionRequest {
    ToolExecutionRequest::new(tools::WEB_FETCH, json!({"url":url})).with_policy(retry_policy(5))
}

#[tokio::test]
async fn approved_fetch_transient_failure_recovers_and_policy_changes_revoke_retry() -> Result<()> {
    for change_policy in [false, true] {
        let workspace = TempDir::new()?;
        let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
        let calls = http_probe(&registry, 503, 1, 1, change_policy).await?;
        registry
            .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Prompt)
            .await?;
        registry.mark_tool_preapproved(tools::WEB_FETCH).await;
        let outcome = registry
            .execute_public_tool_request(fetch_request("https://example.com/page"))
            .await;
        assert_eq!(outcome.is_success(), !change_policy, "{:?}", outcome.error);
        assert_eq!(calls.load(Ordering::SeqCst), if change_policy { 1 } else { 2 });
        if change_policy {
            assert_eq!(outcome.error.unwrap().error_type, ToolErrorType::PolicyViolation);
        }
    }
    Ok(())
}

#[tokio::test]
async fn rate_limited_origin_stays_bounded_across_paths_and_internal_continuations() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 429, 1, usize::MAX, false).await?;
    let outcome = registry
        .execute_public_tool_request(fetch_request("https://example.com/one"))
        .await;
    assert_eq!(outcome.attempts, 2);
    let error = outcome.error.expect("HTTP limitation");
    let wire = error.to_json_value();
    assert_eq!(wire["error"]["http_status"], 429);
    assert_eq!(wire["error"]["retry_after_ms"], 1);
    assert_eq!(wire["error"]["recovery_suggestions"][0], "Disclose unavailable external evidence.");
    // No genuine-request reset, just another invocation as an internal turn would issue.
    for path in ["two", "three"] {
        let outcome = registry
            .execute_public_tool_request(fetch_request(&format!("https://example.com/{path}")))
            .await;
        assert!(!outcome.is_success());
    }
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    registry
        .execute_public_tool_request(fetch_request("https://other.example.org/one"))
        .await;
    // Other existing safety/loop limits may stop the second attempt; the
    // origin budget must admit this different origin without reopening the first.
    assert!((3..=4).contains(&calls.load(Ordering::SeqCst)));
    registry.begin_tracker_request(false);
    assert!(registry.harness_context.fetch_origin_attempts.is_empty());
    Ok(())
}

#[tokio::test]
async fn oversized_delays_and_exhausted_deadlines_do_not_retry() -> Result<()> {
    for (delay, timeout) in [(5001, 30), (1001, 1), (u64::MAX, 30)] {
        let workspace = TempDir::new()?;
        let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
        let calls = http_probe(&registry, 429, delay, usize::MAX, false).await?;
        let mut request = fetch_request("https://example.com/slow");
        request.args["timeout_secs"] = json!(timeout);
        let outcome =
            tokio::time::timeout(Duration::from_secs(1), registry.execute_public_tool_request(request)).await?;
        assert_eq!(outcome.attempts, 1);
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        let error = outcome.error.unwrap();
        assert!(!error.retryable);
        assert_eq!(error.retry_after_ms, Some(delay));
        let next = registry
            .execute_public_tool_request(fetch_request("https://example.com/another-path"))
            .await;
        assert!(!next.is_success());
        assert_eq!(next.error.unwrap().retry_after_ms, Some(delay));
        assert_eq!(calls.load(Ordering::SeqCst), 1, "terminal retry rejection must survive another invocation");
    }
    Ok(())
}

#[tokio::test]
async fn denied_fetch_does_not_consume_the_origins_network_attempts() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 429, 1, 1, false).await?;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Deny)
        .await?;
    let denied = registry
        .execute_public_tool_request(fetch_request("https://example.com/denied"))
        .await;
    assert_eq!(denied.error.unwrap().error_type, ToolErrorType::PolicyViolation);
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Allow)
        .await?;
    let approved = registry
        .execute_public_tool_request(fetch_request("https://example.com/approved"))
        .await;
    assert!(approved.is_success(), "{:?}", approved.error);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

#[tokio::test]
async fn policy_evaluation_does_not_leave_a_stale_approval_after_execution() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 200, 0, 0, false).await?;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Allow)
        .await?;

    let approved = registry
        .execute_public_tool_request(fetch_request("https://example.com/approved"))
        .await;
    assert!(approved.is_success(), "initial allowed fetch failed: {:?}", approved.error);

    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Prompt)
        .await?;
    let pending = registry
        .execute_public_tool_request(fetch_request("https://example.com/requires-approval"))
        .await;
    assert!(!pending.is_success(), "a policy prompt must not consume a stale approval");
    assert_eq!(pending.error.unwrap().error_type, ToolErrorType::PolicyViolation);
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    Ok(())
}

#[tokio::test]
async fn restrictive_policy_change_revokes_explicit_pending_approval() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 200, 0, 0, false).await?;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Prompt)
        .await?;
    registry.mark_tool_preapproved(tools::WEB_FETCH).await;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Deny)
        .await?;

    let denied = registry
        .execute_public_tool_request(fetch_request("https://example.com/revoked"))
        .await;
    assert!(!denied.is_success(), "a denied policy must revoke a pending approval");
    assert_eq!(denied.error.unwrap().error_type, ToolErrorType::PolicyViolation);
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    Ok(())
}

#[tokio::test]
async fn concurrent_rate_limited_calls_share_the_origin_budget() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 429, 1, usize::MAX, false).await?;
    let (first, second) = tokio::join!(
        registry.execute_public_tool_request(fetch_request("https://example.com/a")),
        registry.execute_public_tool_request(fetch_request("https://example.com/b")),
    );
    assert!(!first.is_success());
    assert!(!second.is_success());
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

#[tokio::test]
async fn cancellation_drops_retry_wait_without_another_network_attempt() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 429, 5000, usize::MAX, false).await?;
    let result = tokio::time::timeout(
        Duration::from_millis(100),
        registry.execute_public_tool_request(fetch_request("https://example.com/wait")),
    )
    .await;
    assert!(result.is_err());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    // Cancellation releases the transport permit, but cannot erase Retry-After.
    let resumed = registry
        .execute_public_tool_request_with_deadline(
            fetch_request("https://example.com/after-cancel"),
            Instant::now() + Duration::from_millis(200),
        )
        .await;
    assert!(!resumed.is_success());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    registry.begin_tracker_request(false);
    let mut fresh = fetch_request("https://example.com/fresh-request");
    fresh.policy.max_retries = 0;
    registry.execute_public_tool_request(fresh).await;
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

#[tokio::test]
async fn retry_wait_respects_the_callers_execution_deadline() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 429, 1000, usize::MAX, false).await?;
    let outcome = registry
        .execute_public_tool_request_with_deadline(
            fetch_request("https://example.com/deadline"),
            Instant::now() + Duration::from_millis(200),
        )
        .await;
    assert_eq!(outcome.attempts, 1);
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert_eq!(outcome.error.unwrap().retry_after_ms, Some(1000));
    Ok(())
}

#[tokio::test]
async fn concurrent_fetch_invocations_cannot_share_one_shot_approval() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 503, 1, 1, false).await?;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Prompt)
        .await?;
    registry.mark_tool_preapproved(tools::WEB_FETCH).await;
    let (first, second) = tokio::join!(
        registry.execute_public_tool_request(fetch_request("https://example.com/a")),
        registry.execute_public_tool_request(fetch_request("https://example.com/b")),
    );
    assert!(first.is_success());
    assert!(!second.is_success());
    assert_eq!(second.attempts, 1);
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

#[tokio::test]
async fn concurrent_fetch_invocations_consume_independent_approvals() -> Result<()> {
    let workspace = TempDir::new()?;
    let registry = ToolRegistry::new(workspace.path().to_path_buf()).await;
    let calls = http_probe(&registry, 200, 0, 0, false).await?;
    registry
        .set_tool_policy(tools::WEB_FETCH, crate::tool_policy::ToolPolicy::Prompt)
        .await?;
    registry.mark_tool_preapproved(tools::WEB_FETCH).await;
    registry.mark_tool_preapproved(tools::WEB_FETCH).await;

    let (first, second) = tokio::join!(
        registry.execute_public_tool_request(fetch_request("https://example.com/approved-a")),
        registry.execute_public_tool_request(fetch_request("https://example.com/approved-b")),
    );
    assert!(first.is_success(), "first approved fetch failed: {:?}", first.error);
    assert!(second.is_success(), "second approved fetch failed: {:?}", second.error);
    let denied = registry
        .execute_public_tool_request(fetch_request("https://example.com/unapproved"))
        .await;
    assert!(!denied.is_success(), "a third fetch must still require its own approval");
    assert_eq!(calls.load(Ordering::SeqCst), 2);
    Ok(())
}

#[tokio::test]
async fn retry_approval_cannot_cross_workspace_registry_scope() -> Result<()> {
    let first_workspace = TempDir::new()?;
    let second_workspace = TempDir::new()?;
    let first = ToolRegistry::new(first_workspace.path().to_path_buf()).await;
    let second = ToolRegistry::new(second_workspace.path().to_path_buf()).await;
    let first_calls = register_probe(&first, 0).await?;
    let second_calls = register_probe(&second, 0).await?;
    for registry in [&first, &second] {
        registry
            .set_tool_policy(PROBE_NAME, crate::tool_policy::ToolPolicy::Prompt)
            .await?;
    }
    assert_eq!(first.policy_gateway.revision(), second.policy_gateway.revision());
    let args = json!({"input":"same arguments"});
    let approval = RetryApproval {
        tool_name: PROBE_NAME.into(),
        args: args.clone(),
        policy_revision: first.policy_gateway.revision(),
        policy_gateway: first.policy_gateway.clone(),
        policy: crate::tool_policy::ToolPolicy::Prompt,
    };
    RETRY_APPROVAL
        .scope(std::cell::RefCell::new(Some(approval)), async {
            let denied = second.execute_tool(PROBE_NAME, args.clone()).await;
            assert!(denied.is_err(), "a different workspace must retain admission checks");
            assert_eq!(second_calls.load(Ordering::SeqCst), 0);
            let admitted = first.execute_tool(PROBE_NAME, args).await?;
            assert_eq!(admitted["call"], 1);
            assert_eq!(first_calls.load(Ordering::SeqCst), 1);
            Ok::<(), anyhow::Error>(())
        })
        .await?;
    Ok(())
}