vtcode-core 0.100.1

Core library for VT Code - a Rust-based terminal coding agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
use super::*;
use crate::cache::EvictionPolicy;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

// ================================================================
// Test executor provider
// ================================================================

struct EchoExecutor;

#[async_trait]
impl<Ctx: Send + Sync> ExecuteProvider<Ctx> for EchoExecutor {
    async fn execute(_ctx: &Ctx, args: Value) -> Result<Value> {
        Ok(serde_json::json!({
            "tool_name": "echo",
            "echoed": args,
        }))
    }
}

// ================================================================
// Test contexts with different component wiring
// ================================================================

struct TestAutoCtx;

struct EchoMetadata;

impl<Ctx> MetadataProvider<Ctx> for EchoMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "echo"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "Echo tool"
    }
}

delegate_components!(TestAutoCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => EchoExecutor,
    MetadataComponent => EchoMetadata,
    LoggingComponent  => NoLogging,
    CacheComponent    => NoCache,
    RetryComponent    => NoRetry,
});

struct TestDenyCtx;

struct ExecMetadata;

impl<Ctx> MetadataProvider<Ctx> for ExecMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "exec"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "Exec tool"
    }
}

delegate_components!(TestDenyCtx {
    ApprovalComponent => DenyAllApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => EchoExecutor,
    MetadataComponent => ExecMetadata,
    LoggingComponent  => NoLogging,
    CacheComponent    => NoCache,
    RetryComponent    => NoRetry,
});

struct TestTracingCtx;

struct TracedToolMetadata;

impl<Ctx> MetadataProvider<Ctx> for TracedToolMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "traced_tool"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "A traced tool"
    }
}

delegate_components!(TestTracingCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => EchoExecutor,
    MetadataComponent => TracedToolMetadata,
    LoggingComponent  => TracingLogging,
    CacheComponent    => NoCache,
    RetryComponent    => NoRetry,
});

struct NamedToolCtx;

struct NamedToolMetadata;

impl<Ctx> MetadataProvider<Ctx> for NamedToolMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "my_tool"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "My description"
    }
}

delegate_components!(NamedToolCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => EchoExecutor,
    MetadataComponent => NamedToolMetadata,
    LoggingComponent  => NoLogging,
    CacheComponent    => NoCache,
    RetryComponent    => NoRetry,
});

trait HasExecutionCount: Send + Sync {
    fn execution_count(&self) -> &AtomicUsize;
}

struct CountingExecutor;

#[async_trait]
impl<Ctx: HasExecutionCount + Send + Sync> ExecuteProvider<Ctx> for CountingExecutor {
    async fn execute(ctx: &Ctx, args: Value) -> Result<Value> {
        let count = ctx.execution_count().fetch_add(1, Ordering::SeqCst) + 1;
        Ok(serde_json::json!({
            "tool_name": "counting",
            "attempt": count,
            "args": args,
        }))
    }
}

struct TestCachingCtx {
    executions: Arc<AtomicUsize>,
    json_cache: UnifiedCache<ToolExecutionCacheKey, Value>,
    dual_cache: UnifiedCache<ToolExecutionCacheKey, SplitToolResult>,
}

impl TestCachingCtx {
    fn new(executions: Arc<AtomicUsize>) -> Self {
        Self {
            executions,
            json_cache: UnifiedCache::new(8, Duration::from_secs(60), EvictionPolicy::Lru),
            dual_cache: UnifiedCache::new(8, Duration::from_secs(60), EvictionPolicy::Lru),
        }
    }
}

impl HasExecutionCount for TestCachingCtx {
    fn execution_count(&self) -> &AtomicUsize {
        self.executions.as_ref()
    }
}

impl HasExecutionCaches for TestCachingCtx {
    fn json_cache(&self) -> &UnifiedCache<ToolExecutionCacheKey, Value> {
        &self.json_cache
    }

    fn dual_cache(&self) -> &UnifiedCache<ToolExecutionCacheKey, SplitToolResult> {
        &self.dual_cache
    }
}

struct CachedToolMetadata;

impl<Ctx> MetadataProvider<Ctx> for CachedToolMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "cached_tool"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "A cached tool"
    }
}

delegate_components!(TestCachingCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => CountingExecutor,
    MetadataComponent => CachedToolMetadata,
    LoggingComponent  => NoLogging,
    CacheComponent    => CachedResults,
    RetryComponent    => NoRetry,
});

struct FlakyExecutor;

#[async_trait]
impl<Ctx: HasExecutionCount + Send + Sync> ExecuteProvider<Ctx> for FlakyExecutor {
    async fn execute(ctx: &Ctx, args: Value) -> Result<Value> {
        let attempt = ctx.execution_count().fetch_add(1, Ordering::SeqCst) + 1;
        if attempt == 1 {
            anyhow::bail!("transient failure")
        }

        Ok(serde_json::json!({
            "tool_name": "flaky",
            "attempt": attempt,
            "args": args,
        }))
    }
}

struct TestRetryCtx {
    executions: Arc<AtomicUsize>,
    retry_policy: RetryPolicy,
}

impl TestRetryCtx {
    fn new(executions: Arc<AtomicUsize>, retry_policy: RetryPolicy) -> Self {
        Self {
            executions,
            retry_policy,
        }
    }
}

impl HasExecutionCount for TestRetryCtx {
    fn execution_count(&self) -> &AtomicUsize {
        self.executions.as_ref()
    }
}

impl HasRetryPolicy for TestRetryCtx {
    fn retry_policy(&self) -> RetryPolicy {
        self.retry_policy
    }
}

struct FlakyToolMetadata;

impl<Ctx> MetadataProvider<Ctx> for FlakyToolMetadata {
    fn tool_name(_ctx: &Ctx) -> &'static str {
        "flaky_tool"
    }

    fn tool_description(_ctx: &Ctx) -> &'static str {
        "A flaky tool"
    }
}

delegate_components!(TestRetryCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => NoSandbox,
    ExecuteComponent  => FlakyExecutor,
    MetadataComponent => FlakyToolMetadata,
    LoggingComponent  => NoLogging,
    CacheComponent    => NoCache,
    RetryComponent    => ExponentialBackoffRetry,
});

// ================================================================
// Phase 1 tests: approval + sandbox
// ================================================================

#[tokio::test]
async fn auto_ctx_approves() {
    let ctx = TestAutoCtx;
    let result = ComposableRuntime::run(&ctx, "grep", "search files").await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn consumer_trait_executes_directly_on_context() {
    let ctx = TestAutoCtx;
    let result = ctx
        .execute_tool_json("echo", serde_json::json!({"via": "consumer"}))
        .await
        .expect("context capability should execute");

    assert_eq!(
        result
            .get("echoed")
            .and_then(|value| value.get("via"))
            .and_then(|value| value.as_str()),
        Some("consumer")
    );
}

#[tokio::test]
async fn deny_ctx_rejects() {
    let ctx = TestDenyCtx;
    let result = ComposableRuntime::run(&ctx, "exec", "run command").await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("operation denied"));
}

#[tokio::test]
async fn sandbox_check_returns_policy() {
    let ctx = TestAutoCtx;
    let sandboxed = ComposableRuntime::run_with_sandbox(&ctx, "file_write", "write file")
        .await
        .expect("should succeed");
    assert!(!sandboxed);
}

struct StrictSandbox;

impl<Ctx: Send + Sync> SandboxProvider<Ctx> for StrictSandbox {
    fn sandbox_enabled(_ctx: &Ctx) -> bool {
        true
    }

    fn workspace_root(_ctx: &Ctx) -> Option<&PathBuf> {
        None
    }
}

struct StrictCtx;

delegate_components!(StrictCtx {
    ApprovalComponent => AutoApproval,
    SandboxComponent  => StrictSandbox,
});

#[tokio::test]
async fn strict_ctx_enables_sandbox() {
    let ctx = StrictCtx;
    let sandboxed = ComposableRuntime::run_with_sandbox(&ctx, "exec", "run cmd")
        .await
        .expect("should succeed");
    assert!(sandboxed);
}

// ================================================================
// Phase 2 tests: ToolFacade — same context projected as Tool
// ================================================================

#[tokio::test]
async fn tool_facade_executes_via_cgp() {
    let facade = ToolFacade::new(TestAutoCtx);
    let result = facade
        .execute(serde_json::json!({"msg": "hello"}))
        .await
        .expect("should succeed");

    assert_eq!(
        result
            .get("echoed")
            .and_then(|v| v.get("msg"))
            .and_then(|v| v.as_str()),
        Some("hello")
    );
}

#[tokio::test]
async fn tool_facade_denied_by_ctx() {
    let facade = ToolFacade::new(TestDenyCtx);
    let result = facade.execute(serde_json::json!({})).await;
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("operation denied"));
}

#[tokio::test]
async fn tool_facade_name_and_description() {
    let facade = ToolFacade::new(NamedToolCtx);
    assert_eq!(facade.name(), "my_tool");
    assert_eq!(facade.description(), "My description");
}

#[tokio::test]
async fn tool_facade_dual_output() {
    let facade = ToolFacade::new(TestAutoCtx);
    let result = facade
        .execute_dual(serde_json::json!({"key": "value"}))
        .await
        .expect("should succeed");

    assert!(result.success);
}

// ================================================================
// Phase 2 tests: HandlerFacade — same context projected as ToolHandler
// ================================================================

#[tokio::test]
async fn handler_facade_executes_via_cgp() {
    let facade = HandlerFacade::new(TestAutoCtx);
    let session: Arc<dyn crate::tools::handlers::tool_handler::ToolSession> = Arc::new(
        crate::tools::handlers::adapter::DefaultToolSession::new(PathBuf::from("/tmp")),
    );
    let turn = Arc::new(crate::tools::handlers::tool_handler::TurnContext {
        cwd: PathBuf::from("/tmp"),
        turn_id: "test".to_string(),
        sub_id: None,
        shell_environment_policy:
            crate::tools::handlers::tool_handler::ShellEnvironmentPolicy::default(),
        approval_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            crate::tools::handlers::tool_handler::ApprovalPolicy::default(),
        ),
        codex_linux_sandbox_exe: None,
        sandbox_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            Default::default(),
        ),
    });
    let invocation = ToolInvocation {
        session,
        turn,
        tracker: None,
        call_id: "test-call".to_string(),
        tool_name: "echo".to_string(),
        payload: ToolPayload::Function {
            arguments: r#"{"msg":"handler"}"#.to_string(),
        },
    };

    let output = facade.handle(invocation).await.expect("should succeed");
    assert!(output.is_success());
    let content = output.content().expect("should have content");
    assert!(content.contains("handler"));
}

#[tokio::test]
async fn handler_facade_denied_by_ctx() {
    let facade = HandlerFacade::new(TestDenyCtx);
    let session: Arc<dyn crate::tools::handlers::tool_handler::ToolSession> = Arc::new(
        crate::tools::handlers::adapter::DefaultToolSession::new(PathBuf::from("/tmp")),
    );
    let turn = Arc::new(crate::tools::handlers::tool_handler::TurnContext {
        cwd: PathBuf::from("/tmp"),
        turn_id: "test".to_string(),
        sub_id: None,
        shell_environment_policy:
            crate::tools::handlers::tool_handler::ShellEnvironmentPolicy::default(),
        approval_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            crate::tools::handlers::tool_handler::ApprovalPolicy::default(),
        ),
        codex_linux_sandbox_exe: None,
        sandbox_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            Default::default(),
        ),
    });
    let invocation = ToolInvocation {
        session,
        turn,
        tracker: None,
        call_id: "test-call".to_string(),
        tool_name: "exec".to_string(),
        payload: ToolPayload::Function {
            arguments: "{}".to_string(),
        },
    };

    let result = facade.handle(invocation).await;
    assert!(result.is_err());
}

// ================================================================
// Phase 2 tests: same context, two facades — unification proof
// ================================================================

#[tokio::test]
async fn same_context_both_facades() {
    let tool = ToolFacade::new(TestAutoCtx);
    let tool_result = tool
        .execute(serde_json::json!({"via": "tool"}))
        .await
        .expect("tool facade should succeed");
    assert!(tool_result.get("echoed").is_some());

    let handler = HandlerFacade::new(TestAutoCtx);
    let session: Arc<dyn crate::tools::handlers::tool_handler::ToolSession> = Arc::new(
        crate::tools::handlers::adapter::DefaultToolSession::new(PathBuf::from("/tmp")),
    );
    let turn = Arc::new(crate::tools::handlers::tool_handler::TurnContext {
        cwd: PathBuf::from("/tmp"),
        turn_id: "test".to_string(),
        sub_id: None,
        shell_environment_policy:
            crate::tools::handlers::tool_handler::ShellEnvironmentPolicy::default(),
        approval_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            crate::tools::handlers::tool_handler::ApprovalPolicy::default(),
        ),
        codex_linux_sandbox_exe: None,
        sandbox_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            Default::default(),
        ),
    });
    let invocation = ToolInvocation {
        session,
        turn,
        tracker: None,
        call_id: "test-call".to_string(),
        tool_name: "echo".to_string(),
        payload: ToolPayload::Function {
            arguments: r#"{"via":"handler"}"#.to_string(),
        },
    };

    let handler_output = handler
        .handle(invocation)
        .await
        .expect("handler facade should succeed");
    assert!(handler_output.is_success());
    let content = handler_output.content().expect("should have content");
    assert!(content.contains("handler"));
}

// ================================================================
// Phase 6 tests: static logging, cache, and retry providers
// ================================================================

#[tokio::test]
async fn tracing_logging_executes() {
    let facade = ToolFacade::new(TestTracingCtx);
    let result = facade
        .execute(serde_json::json!({"test": true}))
        .await
        .expect("should succeed with tracing logging");

    assert!(result.get("echoed").is_some());
}

#[tokio::test]
async fn cached_results_short_circuit_second_execute() {
    let executions = Arc::new(AtomicUsize::new(0));
    let facade = ToolFacade::new(TestCachingCtx::new(executions.clone()));

    let first = facade
        .execute(serde_json::json!({"query": "same"}))
        .await
        .expect("first execution should succeed");
    let second = facade
        .execute(serde_json::json!({"query": "same"}))
        .await
        .expect("second execution should succeed");

    assert_eq!(executions.load(Ordering::SeqCst), 1);
    assert_eq!(first, second);
}

#[tokio::test]
async fn cached_results_short_circuit_dual_output() {
    let executions = Arc::new(AtomicUsize::new(0));
    let facade = ToolFacade::new(TestCachingCtx::new(executions.clone()));

    let first = facade
        .execute_dual(serde_json::json!({"query": "same"}))
        .await
        .expect("first dual execution should succeed");
    let second = facade
        .execute_dual(serde_json::json!({"query": "same"}))
        .await
        .expect("second dual execution should succeed");

    assert_eq!(executions.load(Ordering::SeqCst), 1);
    assert_eq!(first.ui_content, second.ui_content);
    assert_eq!(first.llm_content, second.llm_content);
}

#[tokio::test]
async fn retry_provider_retries_failed_execute() {
    let executions = Arc::new(AtomicUsize::new(0));
    let retry_policy = RetryPolicy {
        max_attempts: 2,
        initial_backoff: Duration::ZERO,
        max_backoff: Duration::ZERO,
    };
    let facade = ToolFacade::new(TestRetryCtx::new(executions.clone(), retry_policy));

    let result = facade
        .execute(serde_json::json!({"retry": true}))
        .await
        .expect("retry should recover the transient failure");

    assert_eq!(executions.load(Ordering::SeqCst), 2);
    assert_eq!(
        result.get("attempt").and_then(|value| value.as_u64()),
        Some(2)
    );
}

// ================================================================
// Phase 3 tests: concrete runtime contexts
// ================================================================

#[tokio::test]
async fn interactive_ctx_enables_sandbox() {
    let ctx = InteractiveCtx::new(PathBuf::from("/workspace"));
    let sandboxed = ComposableRuntime::run_with_sandbox(&ctx, "exec", "run cmd")
        .await
        .expect("should succeed");
    assert!(sandboxed);
}

#[tokio::test]
async fn ci_ctx_auto_approves_with_sandbox() {
    let ctx = CiCtx::new(PathBuf::from("/ci/workspace"));
    let sandboxed = ComposableRuntime::run_with_sandbox(&ctx, "exec", "run cmd")
        .await
        .expect("should succeed");
    assert!(sandboxed);
}

#[tokio::test]
async fn bench_ctx_no_sandbox() {
    let sandboxed = ComposableRuntime::run_with_sandbox(&BenchCtx, "exec", "run cmd")
        .await
        .expect("should succeed");
    assert!(!sandboxed);
}

// ================================================================
// Phase 3 tests: ToolBridgeCtx + PassthroughExecutor
// ================================================================

struct SimpleTool;

#[async_trait]
impl Tool for SimpleTool {
    async fn execute(&self, args: Value) -> Result<Value> {
        Ok(serde_json::json!({
            "tool_name": "simple",
            "input": args,
            "result": "ok"
        }))
    }

    fn name(&self) -> &'static str {
        "simple"
    }

    fn description(&self) -> &'static str {
        "A simple test tool"
    }

    fn parameter_schema(&self) -> Option<Value> {
        Some(serde_json::json!({
            "type": "object",
            "properties": {
                "query": { "type": "string" }
            }
        }))
    }

    fn default_permission(&self) -> ToolPolicy {
        ToolPolicy::Allow
    }

    fn is_mutating(&self) -> bool {
        false
    }

    fn kind(&self) -> &'static str {
        "test"
    }
}

#[tokio::test]
async fn bridge_interactive_passthrough() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let facade = wrap_tool_interactive(tool, PathBuf::from("/workspace"));

    assert_eq!(facade.name(), "simple");
    assert_eq!(facade.description(), "A simple test tool");

    let result = facade
        .execute(serde_json::json!({"query": "test"}))
        .await
        .expect("should succeed");

    assert_eq!(result.get("result").and_then(|v| v.as_str()), Some("ok"));
    assert_eq!(
        result
            .get("input")
            .and_then(|v| v.get("query"))
            .and_then(|v| v.as_str()),
        Some("test")
    );
}

#[tokio::test]
async fn bridge_ci_passthrough() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let facade = wrap_tool_ci(tool, PathBuf::from("/ci"));

    let result = facade
        .execute(serde_json::json!({"key": "value"}))
        .await
        .expect("should succeed");

    assert_eq!(result.get("result").and_then(|v| v.as_str()), Some("ok"));
}

#[tokio::test]
async fn bridge_passthrough_metadata_is_preserved() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let facade = wrap_tool_interactive(tool, PathBuf::from("/workspace"));

    assert!(facade.parameter_schema().is_some());
    assert_eq!(facade.default_permission(), ToolPolicy::Allow);
    assert!(!facade.is_mutating());
    assert_eq!(facade.kind(), "test");
}

#[tokio::test]
async fn native_typed_tool_preserves_metadata_and_execution() {
    let facade = wrap_native_tool_interactive(SimpleTool, PathBuf::from("/workspace"));

    assert_eq!(facade.name(), "simple");
    assert_eq!(facade.description(), "A simple test tool");
    assert!(facade.parameter_schema().is_some());
    assert_eq!(facade.default_permission(), ToolPolicy::Allow);
    assert!(!facade.is_mutating());
    assert_eq!(facade.kind(), "test");

    let result = facade
        .execute(serde_json::json!({"query": "native"}))
        .await
        .expect("should succeed");
    assert_eq!(
        result
            .get("input")
            .and_then(|v| v.get("query"))
            .and_then(|v| v.as_str()),
        Some("native")
    );
}

#[tokio::test]
async fn bridge_dual_output() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let facade = wrap_tool_interactive(tool, PathBuf::from("/workspace"));

    let result = facade
        .execute_dual(serde_json::json!({"x": 1}))
        .await
        .expect("should succeed");

    assert!(result.success);
    assert_eq!(result.tool_name, "simple");
}

#[tokio::test]
async fn bridge_handler_facade() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let bridge_ctx = ToolBridgeCtx {
        inner: tool,
        runtime: InteractiveCtx::new(PathBuf::from("/workspace")),
    };
    let handler = HandlerFacade::new(bridge_ctx);

    let session: Arc<dyn crate::tools::handlers::tool_handler::ToolSession> = Arc::new(
        crate::tools::handlers::adapter::DefaultToolSession::new(PathBuf::from("/tmp")),
    );
    let turn = Arc::new(crate::tools::handlers::tool_handler::TurnContext {
        cwd: PathBuf::from("/tmp"),
        turn_id: "test".to_string(),
        sub_id: None,
        shell_environment_policy:
            crate::tools::handlers::tool_handler::ShellEnvironmentPolicy::default(),
        approval_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            crate::tools::handlers::tool_handler::ApprovalPolicy::default(),
        ),
        codex_linux_sandbox_exe: None,
        sandbox_policy: crate::tools::handlers::tool_handler::Constrained::allow_any(
            Default::default(),
        ),
    });
    let invocation = ToolInvocation {
        session,
        turn,
        tracker: None,
        call_id: "bridge-test".to_string(),
        tool_name: "simple".to_string(),
        payload: ToolPayload::Function {
            arguments: r#"{"via":"bridge"}"#.to_string(),
        },
    };

    let output = handler.handle(invocation).await.expect("should succeed");
    assert!(output.is_success());
    let content = output.content().expect("should have content");
    assert!(content.contains("bridge"));
}

// ================================================================
// Phase 3 tests: HasComponent delegation through ToolBridgeCtx
// ================================================================

#[tokio::test]
async fn bridge_ctx_delegates_components() {
    let tool: Arc<dyn Tool> = Arc::new(SimpleTool);
    let bridge = ToolBridgeCtx {
        inner: tool,
        runtime: InteractiveCtx::new(PathBuf::from("/workspace")),
    };

    let sandboxed = ComposableRuntime::run_with_sandbox(&bridge, "exec", "test")
        .await
        .expect("should succeed");
    assert!(sandboxed);
}