roder-api 0.1.2

Agentic software development tools and SDKs for Roder.
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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};

use crate::capabilities::{CapabilityDenial, CapabilityGrant, CapabilityRequest, CapabilityStatus};

pub type ExtensionId = String;
pub type ApiVersion = String;
pub type InferenceEngineId = String;
pub type InferenceRouterId = String;
pub type ContextProviderId = String;
pub type ContextPlannerId = String;
pub type ThreadStoreId = String;
pub type CheckpointStoreId = String;
pub type MemoryStoreId = String;
pub type KnowledgeStoreId = String;
pub type EmbeddingProviderId = String;
pub type MediaGeneratorProviderId = String;
pub type ToolProviderId = String;
pub type SubagentDispatcherId = String;
pub type PolicyContributorId = String;
pub type EventSinkId = String;
pub type TaskExecutorId = String;
pub type NotificationSinkId = String;
pub type InteractiveRegionHandlerId = String;
pub type SpeechTranscriberId = String;
pub type SpeechSynthesizerId = String;
pub type VersionControlProviderId = crate::version_control::VcsProviderId;

pub const SUPPORTED_EXTENSION_API_VERSION: &str = "0.1.0";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ProvidedService {
    InferenceEngine(InferenceEngineId),
    InferenceRouter(InferenceRouterId),
    ContextProvider(ContextProviderId),
    ContextPlanner(ContextPlannerId),
    ThreadStore(ThreadStoreId),
    CheckpointStore(CheckpointStoreId),
    MemoryStore(MemoryStoreId),
    KnowledgeStore(KnowledgeStoreId),
    EmbeddingProvider(EmbeddingProviderId),
    MediaGenerator(MediaGeneratorProviderId),
    ToolProvider(ToolProviderId),
    SubagentDispatcher(SubagentDispatcherId),
    PolicyContributor(PolicyContributorId),
    EventSink(EventSinkId),
    ForkProvider(crate::forks::ForkProviderId),
    TaskExecutor(TaskExecutorId),
    NotificationSink(NotificationSinkId),
    InteractiveRegionHandler(InteractiveRegionHandlerId),
    SpeechTranscriber(SpeechTranscriberId),
    SpeechSynthesizer(SpeechSynthesizerId),
    VersionControlProvider(VersionControlProviderId),
    RemoteRunnerProvider(crate::remote_runner::RemoteRunnerProviderId),
    StatusSegment(crate::tui_status::StatusSegmentId),
    PaletteSource(crate::tui_status::PaletteSourceId),
    CodeIndexProvider(crate::code_index::CodeIndexProviderId),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtensionManifest {
    pub id: ExtensionId,
    pub name: String,
    pub version: Version,
    pub api_version: ApiVersion,
    pub description: Option<String>,
    pub provides: Vec<ProvidedService>,
    pub required_capabilities: Vec<CapabilityRequest>,
}

pub trait RoderExtension: Send + Sync + 'static {
    fn manifest(&self) -> ExtensionManifest;

    fn install(&self, registry: &mut ExtensionRegistryBuilder) -> anyhow::Result<()>;
}

/// Lets shared extension handles (e.g. distribution-supplied extension lists)
/// be installed through the same `ExtensionRegistryBuilder::install` path as
/// concrete extension values.
impl<E: RoderExtension + ?Sized> RoderExtension for Arc<E> {
    fn manifest(&self) -> ExtensionManifest {
        (**self).manifest()
    }

    fn install(&self, registry: &mut ExtensionRegistryBuilder) -> anyhow::Result<()> {
        (**self).install(registry)
    }
}

#[derive(Clone)]
pub struct ExtensionRegistry {
    pub manifests: Vec<ExtensionManifest>,
    pub capability_statuses: BTreeMap<ExtensionId, Vec<CapabilityStatus>>,
    pub inference_engines: Vec<Arc<dyn crate::inference::InferenceEngine>>,
    pub inference_routers: Vec<Arc<dyn crate::inference_routing::InferenceRouter>>,
    pub context_providers: Vec<Arc<dyn crate::context::ContextProvider>>,
    pub context_planners: Vec<Arc<dyn crate::context::ContextPlanner>>,
    pub thread_stores: Vec<Arc<dyn crate::thread::ThreadStoreFactory>>,
    pub checkpoint_stores: Vec<Arc<dyn crate::thread::CheckpointStoreFactory>>,
    pub memory_stores: Vec<Arc<dyn crate::memory::MemoryStoreFactory>>,
    pub knowledge_stores: Vec<Arc<dyn crate::knowledge::KnowledgeStoreFactory>>,
    pub embedding_providers: Vec<Arc<dyn crate::embeddings::EmbeddingProvider>>,
    pub media_generator_providers: Vec<Arc<dyn crate::media::MediaGeneratorProvider>>,
    pub tools: Vec<Arc<dyn crate::tools::ToolContributor>>,
    pub subagent_dispatchers: Vec<Arc<dyn crate::subagents::SubagentDispatcher>>,
    pub policy_contributors: Vec<Arc<dyn crate::context::PolicyContributor>>,
    pub event_sinks: Vec<Arc<dyn crate::extension::EventSink>>,
    pub fork_providers: Vec<Arc<dyn crate::forks::ForkProvider>>,
    pub task_executors: Vec<Arc<dyn crate::tasks::TaskExecutor>>,
    pub notification_sinks: Vec<Arc<dyn crate::notifications::NotificationSink>>,
    pub interactive_region_handlers: Vec<Arc<dyn crate::interactive::InteractiveRegionHandler>>,
    pub speech_transcribers: Vec<Arc<dyn crate::speech::SpeechTranscriber>>,
    pub speech_synthesizers: Vec<Arc<dyn crate::speech::SpeechSynthesizer>>,
    pub version_control_providers: Vec<Arc<dyn crate::version_control::VcsProvider>>,
    pub remote_runner_providers: Vec<Arc<dyn crate::remote_runner::RemoteRunnerProvider>>,
    pub status_segments: Vec<crate::tui_status::StatusSegment>,
    pub palette_sources: Vec<crate::tui_status::PaletteSourceDescriptor>,
    pub code_index_providers: Vec<Arc<dyn crate::code_index::CodeIndexProvider>>,
}

impl ExtensionRegistry {
    pub fn media_generator(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::media::MediaGeneratorProvider>> {
        self.media_generator_providers
            .iter()
            .find(|provider| provider.provider_id() == id)
            .cloned()
    }

    pub fn inference_engine(&self, id: &str) -> Option<Arc<dyn crate::inference::InferenceEngine>> {
        self.inference_engines
            .iter()
            .find(|engine| engine.id() == id)
            .cloned()
    }

    pub fn default_inference_engine(&self) -> Option<Arc<dyn crate::inference::InferenceEngine>> {
        self.inference_engines.first().cloned()
    }

    pub fn inference_router(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::inference_routing::InferenceRouter>> {
        self.inference_routers
            .iter()
            .find(|router| router.id() == id)
            .cloned()
    }

    pub fn speech_transcriber(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::speech::SpeechTranscriber>> {
        self.speech_transcribers
            .iter()
            .find(|transcriber| transcriber.id() == id)
            .cloned()
    }

    pub fn speech_synthesizer(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::speech::SpeechSynthesizer>> {
        self.speech_synthesizers
            .iter()
            .find(|synthesizer| synthesizer.id() == id)
            .cloned()
    }

    pub fn fork_provider(&self, id: &str) -> Option<Arc<dyn crate::forks::ForkProvider>> {
        self.fork_providers
            .iter()
            .find(|provider| provider.descriptor().id == id)
            .cloned()
    }

    pub fn provided_services(&self) -> Vec<ProvidedService> {
        self.manifests
            .iter()
            .flat_map(|manifest| manifest.provides.iter().cloned())
            .collect()
    }

    pub fn capability_statuses(&self, extension_id: &str) -> &[CapabilityStatus] {
        self.capability_statuses
            .get(extension_id)
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    pub fn subagent_dispatcher(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::subagents::SubagentDispatcher>> {
        self.subagent_dispatchers
            .iter()
            .find(|dispatcher| dispatcher.id() == id)
            .cloned()
    }

    pub fn version_control_provider(
        &self,
        id: &str,
    ) -> Option<Arc<dyn crate::version_control::VcsProvider>> {
        self.version_control_providers
            .iter()
            .find(|provider| provider.id() == id)
            .cloned()
    }

    pub fn version_control_resolver(&self) -> crate::version_control::RegistryVcsProviderResolver {
        crate::version_control::RegistryVcsProviderResolver::new(
            self.version_control_providers.clone(),
        )
    }
}

pub struct ExtensionRegistryBuilder {
    manifests: Vec<ExtensionManifest>,
    granted_capabilities: BTreeMap<ExtensionId, BTreeSet<String>>,
    denied_capabilities: BTreeMap<ExtensionId, BTreeMap<String, String>>,
    pub inference_engines: Vec<Arc<dyn crate::inference::InferenceEngine>>,
    pub inference_routers: Vec<Arc<dyn crate::inference_routing::InferenceRouter>>,
    pub context_providers: Vec<Arc<dyn crate::context::ContextProvider>>,
    pub context_planners: Vec<Arc<dyn crate::context::ContextPlanner>>,
    pub thread_stores: Vec<Arc<dyn crate::thread::ThreadStoreFactory>>,
    pub checkpoint_stores: Vec<Arc<dyn crate::thread::CheckpointStoreFactory>>,
    pub memory_stores: Vec<Arc<dyn crate::memory::MemoryStoreFactory>>,
    pub knowledge_stores: Vec<Arc<dyn crate::knowledge::KnowledgeStoreFactory>>,
    pub embedding_providers: Vec<Arc<dyn crate::embeddings::EmbeddingProvider>>,
    pub media_generator_providers: Vec<Arc<dyn crate::media::MediaGeneratorProvider>>,
    pub tools: Vec<Arc<dyn crate::tools::ToolContributor>>,
    pub subagent_dispatchers: Vec<Arc<dyn crate::subagents::SubagentDispatcher>>,
    pub policy_contributors: Vec<Arc<dyn crate::context::PolicyContributor>>,
    pub event_sinks: Vec<Arc<dyn crate::extension::EventSink>>,
    pub fork_providers: Vec<Arc<dyn crate::forks::ForkProvider>>,
    pub task_executors: Vec<Arc<dyn crate::tasks::TaskExecutor>>,
    pub notification_sinks: Vec<Arc<dyn crate::notifications::NotificationSink>>,
    pub interactive_region_handlers: Vec<Arc<dyn crate::interactive::InteractiveRegionHandler>>,
    pub speech_transcribers: Vec<Arc<dyn crate::speech::SpeechTranscriber>>,
    pub speech_synthesizers: Vec<Arc<dyn crate::speech::SpeechSynthesizer>>,
    pub version_control_providers: Vec<Arc<dyn crate::version_control::VcsProvider>>,
    pub remote_runner_providers: Vec<Arc<dyn crate::remote_runner::RemoteRunnerProvider>>,
    pub status_segments: Vec<crate::tui_status::StatusSegment>,
    pub palette_sources: Vec<crate::tui_status::PaletteSourceDescriptor>,
    pub code_index_providers: Vec<Arc<dyn crate::code_index::CodeIndexProvider>>,
}

impl Default for ExtensionRegistryBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ExtensionRegistryBuilder {
    pub fn new() -> Self {
        Self {
            manifests: Vec::new(),
            granted_capabilities: BTreeMap::new(),
            denied_capabilities: BTreeMap::new(),
            inference_engines: Vec::new(),
            inference_routers: Vec::new(),
            context_providers: Vec::new(),
            context_planners: Vec::new(),
            thread_stores: Vec::new(),
            checkpoint_stores: Vec::new(),
            memory_stores: Vec::new(),
            knowledge_stores: Vec::new(),
            embedding_providers: Vec::new(),
            media_generator_providers: Vec::new(),
            tools: Vec::new(),
            subagent_dispatchers: Vec::new(),
            policy_contributors: Vec::new(),
            event_sinks: Vec::new(),
            fork_providers: Vec::new(),
            task_executors: Vec::new(),
            notification_sinks: Vec::new(),
            interactive_region_handlers: Vec::new(),
            speech_transcribers: Vec::new(),
            speech_synthesizers: Vec::new(),
            version_control_providers: Vec::new(),
            remote_runner_providers: Vec::new(),
            status_segments: Vec::new(),
            palette_sources: Vec::new(),
            code_index_providers: Vec::new(),
        }
    }

    /// Installs the extension and checks that every service it registers is
    /// declared in its manifest `provides` list, so a manifest is a complete
    /// inventory of what an installed extension contributes. Host code that
    /// registers services directly on the builder (outside `install`) is not
    /// subject to this check.
    pub fn install<E: RoderExtension>(&mut self, ext: E) -> anyhow::Result<()> {
        let manifest = ext.manifest();
        if self
            .manifests
            .iter()
            .any(|existing| existing.id == manifest.id)
        {
            anyhow::bail!("extension {} is already installed", manifest.id);
        }
        let before = service_counts(self)?;
        ext.install(self)?;
        let declared: BTreeSet<ProvidedService> = manifest.provides.iter().cloned().collect();
        for (service, count) in service_counts(self)? {
            let prior = before.get(&service).copied().unwrap_or(0);
            if count > prior && !declared.contains(&service) {
                anyhow::bail!(
                    "extension {} installed undeclared service {}; declare it in the manifest provides list",
                    manifest.id,
                    service_label(&service)
                );
            }
        }
        self.manifests.push(manifest);
        Ok(())
    }

    pub fn build(self) -> anyhow::Result<ExtensionRegistry> {
        let validation = self.validate()?;
        Ok(ExtensionRegistry {
            manifests: self.manifests,
            capability_statuses: validation.capability_statuses,
            inference_engines: self.inference_engines,
            inference_routers: self.inference_routers,
            context_providers: self.context_providers,
            context_planners: self.context_planners,
            thread_stores: self.thread_stores,
            checkpoint_stores: self.checkpoint_stores,
            memory_stores: self.memory_stores,
            knowledge_stores: self.knowledge_stores,
            embedding_providers: self.embedding_providers,
            media_generator_providers: self.media_generator_providers,
            tools: self.tools,
            subagent_dispatchers: self.subagent_dispatchers,
            policy_contributors: self.policy_contributors,
            event_sinks: self.event_sinks,
            fork_providers: self.fork_providers,
            task_executors: self.task_executors,
            notification_sinks: self.notification_sinks,
            interactive_region_handlers: self.interactive_region_handlers,
            speech_transcribers: self.speech_transcribers,
            speech_synthesizers: self.speech_synthesizers,
            version_control_providers: self.version_control_providers,
            remote_runner_providers: self.remote_runner_providers,
            status_segments: self.status_segments,
            palette_sources: self.palette_sources,
            code_index_providers: self.code_index_providers,
        })
    }

    pub fn manifest(&mut self, manifest: ExtensionManifest) {
        self.manifests.push(manifest);
    }

    /// Capability grants are advisory metadata surfaced through
    /// `extensions/list` (statically linked extensions run in-process);
    /// nothing enforces them at runtime.
    pub fn grant_capability(&mut self, extension_id: impl Into<String>, grant: CapabilityGrant) {
        self.granted_capabilities
            .entry(extension_id.into())
            .or_default()
            .insert(grant.id);
    }

    /// Advisory, like [`Self::grant_capability`]: a denial fails `build()` for
    /// extensions that require the capability but does not constrain runtime
    /// behavior.
    pub fn deny_capability(&mut self, extension_id: impl Into<String>, denial: CapabilityDenial) {
        self.denied_capabilities
            .entry(extension_id.into())
            .or_default()
            .insert(denial.id, denial.reason);
    }

    pub fn inference_engine(&mut self, engine: Arc<dyn crate::inference::InferenceEngine>) {
        self.inference_engines.push(engine);
    }

    pub fn inference_router(&mut self, router: Arc<dyn crate::inference_routing::InferenceRouter>) {
        self.inference_routers.push(router);
    }

    pub fn context_provider(&mut self, provider: Arc<dyn crate::context::ContextProvider>) {
        self.context_providers.push(provider);
    }

    pub fn context_planner(&mut self, planner: Arc<dyn crate::context::ContextPlanner>) {
        self.context_planners.push(planner);
    }

    pub fn thread_store_factory(&mut self, store: Arc<dyn crate::thread::ThreadStoreFactory>) {
        self.thread_stores.push(store);
    }

    pub fn checkpoint_store_factory(
        &mut self,
        store: Arc<dyn crate::thread::CheckpointStoreFactory>,
    ) {
        self.checkpoint_stores.push(store);
    }

    pub fn memory_store_factory(&mut self, store: Arc<dyn crate::memory::MemoryStoreFactory>) {
        self.memory_stores.push(store);
    }

    pub fn knowledge_store_factory(
        &mut self,
        store: Arc<dyn crate::knowledge::KnowledgeStoreFactory>,
    ) {
        self.knowledge_stores.push(store);
    }

    pub fn embedding_provider(&mut self, provider: Arc<dyn crate::embeddings::EmbeddingProvider>) {
        self.embedding_providers.push(provider);
    }

    pub fn media_generator_provider(
        &mut self,
        provider: Arc<dyn crate::media::MediaGeneratorProvider>,
    ) {
        self.media_generator_providers.push(provider);
    }

    pub fn tool_contributor(&mut self, contributor: Arc<dyn crate::tools::ToolContributor>) {
        self.tools.push(contributor);
    }

    pub fn subagent_dispatcher(
        &mut self,
        dispatcher: Arc<dyn crate::subagents::SubagentDispatcher>,
    ) {
        self.subagent_dispatchers.push(dispatcher);
    }

    pub fn policy_contributor(&mut self, contributor: Arc<dyn crate::context::PolicyContributor>) {
        self.policy_contributors.push(contributor);
    }

    pub fn event_sink(&mut self, sink: Arc<dyn crate::extension::EventSink>) {
        self.event_sinks.push(sink);
    }

    pub fn fork_provider(&mut self, provider: Arc<dyn crate::forks::ForkProvider>) {
        self.fork_providers.push(provider);
    }

    pub fn task_executor(&mut self, executor: Arc<dyn crate::tasks::TaskExecutor>) {
        self.task_executors.push(executor);
    }

    pub fn notification_sink(&mut self, sink: Arc<dyn crate::notifications::NotificationSink>) {
        self.notification_sinks.push(sink);
    }

    pub fn interactive_region_handler(
        &mut self,
        handler: Arc<dyn crate::interactive::InteractiveRegionHandler>,
    ) {
        self.interactive_region_handlers.push(handler);
    }

    pub fn speech_transcriber(&mut self, transcriber: Arc<dyn crate::speech::SpeechTranscriber>) {
        self.speech_transcribers.push(transcriber);
    }

    pub fn speech_synthesizer(&mut self, synthesizer: Arc<dyn crate::speech::SpeechSynthesizer>) {
        self.speech_synthesizers.push(synthesizer);
    }

    pub fn version_control_provider(
        &mut self,
        provider: Arc<dyn crate::version_control::VcsProvider>,
    ) {
        self.version_control_providers.push(provider);
    }

    pub fn remote_runner_provider(
        &mut self,
        provider: Arc<dyn crate::remote_runner::RemoteRunnerProvider>,
    ) {
        self.remote_runner_providers.push(provider);
    }

    pub fn status_segment(&mut self, segment: crate::tui_status::StatusSegment) {
        self.status_segments.push(segment);
    }

    pub fn palette_source(&mut self, source: crate::tui_status::PaletteSourceDescriptor) {
        self.palette_sources.push(source);
    }

    pub fn code_index_provider(&mut self, provider: Arc<dyn crate::code_index::CodeIndexProvider>) {
        self.code_index_providers.push(provider);
    }

    fn validate(&self) -> anyhow::Result<RegistryValidation> {
        validate_manifests(&self.manifests)?;
        validate_actual_services(self)?;
        validate_tool_contributors(&self.tools)?;
        let capability_statuses = validate_capabilities(
            &self.manifests,
            &self.granted_capabilities,
            &self.denied_capabilities,
        )?;
        Ok(RegistryValidation {
            capability_statuses,
        })
    }
}

#[async_trait::async_trait]
pub trait EventSink: Send + Sync + 'static {
    fn id(&self) -> EventSinkId;

    async fn handle_event(&self, envelope: &crate::events::EventEnvelope) -> anyhow::Result<()>;
}

struct RegistryValidation {
    capability_statuses: BTreeMap<ExtensionId, Vec<CapabilityStatus>>,
}

fn validate_manifests(manifests: &[ExtensionManifest]) -> anyhow::Result<()> {
    let mut extension_ids = BTreeSet::new();
    let mut services = BTreeMap::<ProvidedService, ExtensionId>::new();
    for manifest in manifests {
        if manifest.id.trim().is_empty() {
            anyhow::bail!("extension manifest has an empty id");
        }
        if !extension_ids.insert(manifest.id.clone()) {
            anyhow::bail!("duplicate extension id {}", manifest.id);
        }
        validate_api_version(manifest)?;
        for service in &manifest.provides {
            if let Some(existing) = services.insert(service.clone(), manifest.id.clone()) {
                anyhow::bail!(
                    "duplicate provided service {} declared by {} and {}",
                    service_label(service),
                    existing,
                    manifest.id
                );
            }
        }
    }
    Ok(())
}

fn validate_api_version(manifest: &ExtensionManifest) -> anyhow::Result<()> {
    let supported = Version::parse(SUPPORTED_EXTENSION_API_VERSION)?;
    let requirement = VersionReq::parse(&manifest.api_version).or_else(|_| {
        Version::parse(&manifest.api_version).map(|version| VersionReq {
            comparators: vec![semver::Comparator {
                op: semver::Op::Exact,
                major: version.major,
                minor: Some(version.minor),
                patch: Some(version.patch),
                pre: version.pre,
            }],
        })
    })?;
    if requirement.matches(&supported) {
        Ok(())
    } else {
        anyhow::bail!(
            "extension {} requires unsupported API version {}; supported {}",
            manifest.id,
            manifest.api_version,
            SUPPORTED_EXTENSION_API_VERSION
        )
    }
}

fn validate_actual_services(builder: &ExtensionRegistryBuilder) -> anyhow::Result<()> {
    let declared = builder
        .manifests
        .iter()
        .flat_map(|manifest| manifest.provides.iter().cloned())
        .collect::<BTreeSet<_>>();
    let actual = actual_services(builder)?;
    for service in &declared {
        if !actual.contains(service) {
            anyhow::bail!(
                "manifest declares provided service {} but no matching service was installed",
                service_label(service)
            );
        }
    }
    validate_duplicate_actual_services(&actual)
}

fn validate_duplicate_actual_services(actual: &[ProvidedService]) -> anyhow::Result<()> {
    let mut seen = BTreeSet::new();
    for service in actual {
        if !seen.insert(service.clone()) {
            anyhow::bail!("duplicate installed service {}", service_label(service));
        }
    }
    Ok(())
}

/// Multiset of the services currently registered on the builder; diffed
/// around each extension install to attribute new services to that extension.
fn service_counts(
    builder: &ExtensionRegistryBuilder,
) -> anyhow::Result<BTreeMap<ProvidedService, usize>> {
    let mut counts = BTreeMap::new();
    for service in actual_services(builder)? {
        *counts.entry(service).or_insert(0) += 1;
    }
    Ok(counts)
}

fn actual_services(builder: &ExtensionRegistryBuilder) -> anyhow::Result<Vec<ProvidedService>> {
    let mut services = Vec::new();
    services.extend(
        builder
            .inference_engines
            .iter()
            .map(|service| ProvidedService::InferenceEngine(service.id())),
    );
    services.extend(
        builder
            .inference_routers
            .iter()
            .map(|service| ProvidedService::InferenceRouter(service.id())),
    );
    services.extend(
        builder
            .context_providers
            .iter()
            .map(|service| ProvidedService::ContextProvider(service.id())),
    );
    services.extend(
        builder
            .context_planners
            .iter()
            .map(|service| ProvidedService::ContextPlanner(service.id())),
    );
    services.extend(
        builder
            .thread_stores
            .iter()
            .map(|service| ProvidedService::ThreadStore(service.id())),
    );
    services.extend(
        builder
            .checkpoint_stores
            .iter()
            .map(|service| ProvidedService::CheckpointStore(service.id())),
    );
    services.extend(
        builder
            .memory_stores
            .iter()
            .map(|service| ProvidedService::MemoryStore(service.id())),
    );
    services.extend(
        builder
            .knowledge_stores
            .iter()
            .map(|service| ProvidedService::KnowledgeStore(service.id())),
    );
    services.extend(
        builder
            .embedding_providers
            .iter()
            .map(|service| ProvidedService::EmbeddingProvider(service.descriptor().id)),
    );
    services.extend(
        builder
            .media_generator_providers
            .iter()
            .map(|service| ProvidedService::MediaGenerator(service.provider_id().to_string())),
    );
    services.extend(
        builder
            .tools
            .iter()
            .map(|service| ProvidedService::ToolProvider(service.id())),
    );
    services.extend(
        builder
            .subagent_dispatchers
            .iter()
            .map(|service| ProvidedService::SubagentDispatcher(service.id())),
    );
    services.extend(
        builder
            .policy_contributors
            .iter()
            .map(|service| ProvidedService::PolicyContributor(service.id())),
    );
    services.extend(
        builder
            .event_sinks
            .iter()
            .map(|service| ProvidedService::EventSink(service.id())),
    );
    services.extend(
        builder
            .fork_providers
            .iter()
            .map(|service| ProvidedService::ForkProvider(service.descriptor().id)),
    );
    services.extend(
        builder
            .task_executors
            .iter()
            .map(|service| ProvidedService::TaskExecutor(service.id())),
    );
    services.extend(
        builder
            .notification_sinks
            .iter()
            .map(|service| ProvidedService::NotificationSink(service.id())),
    );
    services.extend(
        builder
            .interactive_region_handlers
            .iter()
            .map(|service| ProvidedService::InteractiveRegionHandler(service.id())),
    );
    services.extend(
        builder
            .speech_transcribers
            .iter()
            .map(|service| ProvidedService::SpeechTranscriber(service.id())),
    );
    services.extend(
        builder
            .speech_synthesizers
            .iter()
            .map(|service| ProvidedService::SpeechSynthesizer(service.id())),
    );
    services.extend(
        builder
            .version_control_providers
            .iter()
            .map(|service| ProvidedService::VersionControlProvider(service.id())),
    );
    services.extend(
        builder
            .remote_runner_providers
            .iter()
            .map(|service| ProvidedService::RemoteRunnerProvider(service.id())),
    );
    services.extend(
        builder
            .status_segments
            .iter()
            .map(|service| ProvidedService::StatusSegment(service.id.clone())),
    );
    services.extend(
        builder
            .palette_sources
            .iter()
            .map(|service| ProvidedService::PaletteSource(service.id.clone())),
    );
    services.extend(
        builder
            .code_index_providers
            .iter()
            .map(|service| ProvidedService::CodeIndexProvider(service.id())),
    );
    Ok(services)
}

fn validate_tool_contributors(
    contributors: &[Arc<dyn crate::tools::ToolContributor>],
) -> anyhow::Result<()> {
    let mut registry = crate::tools::ToolRegistry::default();
    for contributor in contributors {
        contributor.contribute(&mut registry)?;
    }
    Ok(())
}

fn validate_capabilities(
    manifests: &[ExtensionManifest],
    granted: &BTreeMap<ExtensionId, BTreeSet<String>>,
    denied: &BTreeMap<ExtensionId, BTreeMap<String, String>>,
) -> anyhow::Result<BTreeMap<ExtensionId, Vec<CapabilityStatus>>> {
    let mut statuses = BTreeMap::new();
    for manifest in manifests {
        let mut seen = BTreeSet::new();
        let mut extension_statuses = Vec::new();
        for request in &manifest.required_capabilities {
            if !seen.insert(request.id.clone()) {
                anyhow::bail!(
                    "extension {} declares capability {} more than once",
                    manifest.id,
                    request.id
                );
            }
            if let Some(reason) = denied
                .get(&manifest.id)
                .and_then(|denials| denials.get(&request.id))
            {
                anyhow::bail!(
                    "extension {} requires denied capability {}: {}",
                    manifest.id,
                    request.id,
                    reason
                );
            }
            let decision = if granted
                .get(&manifest.id)
                .is_some_and(|grants| grants.contains(&request.id))
            {
                crate::capabilities::CapabilityDecision::Granted
            } else {
                crate::capabilities::CapabilityDecision::Requested
            };
            extension_statuses.push(CapabilityStatus {
                id: request.id.clone(),
                decision,
                reason: request.reason.clone(),
            });
        }
        statuses.insert(manifest.id.clone(), extension_statuses);
    }
    Ok(statuses)
}

fn service_label(service: &ProvidedService) -> String {
    match service {
        ProvidedService::InferenceEngine(id) => format!("InferenceEngine({id})"),
        ProvidedService::InferenceRouter(id) => format!("InferenceRouter({id})"),
        ProvidedService::ContextProvider(id) => format!("ContextProvider({id})"),
        ProvidedService::ContextPlanner(id) => format!("ContextPlanner({id})"),
        ProvidedService::ThreadStore(id) => format!("ThreadStore({id})"),
        ProvidedService::CheckpointStore(id) => format!("CheckpointStore({id})"),
        ProvidedService::MemoryStore(id) => format!("MemoryStore({id})"),
        ProvidedService::KnowledgeStore(id) => format!("KnowledgeStore({id})"),
        ProvidedService::EmbeddingProvider(id) => format!("EmbeddingProvider({id})"),
        ProvidedService::MediaGenerator(id) => format!("MediaGenerator({id})"),
        ProvidedService::ToolProvider(id) => format!("ToolProvider({id})"),
        ProvidedService::SubagentDispatcher(id) => format!("SubagentDispatcher({id})"),
        ProvidedService::PolicyContributor(id) => format!("PolicyContributor({id})"),
        ProvidedService::EventSink(id) => format!("EventSink({id})"),
        ProvidedService::ForkProvider(id) => format!("ForkProvider({id})"),
        ProvidedService::TaskExecutor(id) => format!("TaskExecutor({id})"),
        ProvidedService::NotificationSink(id) => format!("NotificationSink({id})"),
        ProvidedService::InteractiveRegionHandler(id) => {
            format!("InteractiveRegionHandler({id})")
        }
        ProvidedService::SpeechTranscriber(id) => format!("SpeechTranscriber({id})"),
        ProvidedService::SpeechSynthesizer(id) => format!("SpeechSynthesizer({id})"),
        ProvidedService::VersionControlProvider(id) => {
            format!("VersionControlProvider({id})")
        }
        ProvidedService::RemoteRunnerProvider(id) => format!("RemoteRunnerProvider({id})"),
        ProvidedService::StatusSegment(id) => format!("StatusSegment({id})"),
        ProvidedService::PaletteSource(id) => format!("PaletteSource({id})"),
        ProvidedService::CodeIndexProvider(id) => format!("CodeIndexProvider({id})"),
    }
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};
    use std::sync::Arc;

    use crate::tui_status::{PaletteSourceDescriptor, StatusCell, StatusSegment, StatusStyle};
    use crate::version_control::{
        VcsCapabilities, VcsChangedContentPage, VcsChangedFile, VcsDetectionClaim, VcsError,
        VcsListChangesRequest, VcsProvider, VcsReadChangedContentRequest, VcsStatus,
        VcsStatusRequest, VcsWorkspace,
    };

    use super::*;

    #[test]
    fn provided_service_status_segment_round_trips_json() {
        let service = ProvidedService::StatusSegment("mode".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize status segment service");
        assert_eq!(encoded, serde_json::json!({ "StatusSegment": "mode" }));

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize status segment service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_inference_router_round_trips_json() {
        let service = ProvidedService::InferenceRouter("adaptive".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize inference router service");
        assert_eq!(
            encoded,
            serde_json::json!({ "InferenceRouter": "adaptive" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize inference router service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_palette_source_round_trips_json() {
        let service = ProvidedService::PaletteSource("commands".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize palette source service");
        assert_eq!(encoded, serde_json::json!({ "PaletteSource": "commands" }));

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize palette source service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_media_generator_round_trips_json() {
        let service = ProvidedService::MediaGenerator("openai".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize media generator service");
        assert_eq!(encoded, serde_json::json!({ "MediaGenerator": "openai" }));

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize media generator service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn registering_media_generator_advertises_service_and_resolves_provider() {
        struct FakeImageExtension;

        struct FakeImageProvider;

        #[async_trait::async_trait]
        impl crate::media::MediaGeneratorProvider for FakeImageProvider {
            fn provider_id(&self) -> &str {
                "fake"
            }

            fn descriptor(&self) -> crate::media::MediaProviderDescriptor {
                crate::media::MediaProviderDescriptor {
                    id: "fake".to_string(),
                    display_name: "Fake Image Provider".to_string(),
                    supports_images: true,
                    configured: true,
                    ..crate::media::MediaProviderDescriptor::default()
                }
            }
        }

        impl RoderExtension for FakeImageExtension {
            fn manifest(&self) -> ExtensionManifest {
                ExtensionManifest {
                    id: "fake-image-extension".to_string(),
                    name: "Fake Image".to_string(),
                    version: Version::new(0, 1, 0),
                    api_version: SUPPORTED_EXTENSION_API_VERSION.to_string(),
                    description: None,
                    provides: vec![ProvidedService::MediaGenerator("fake".to_string())],
                    required_capabilities: Vec::new(),
                }
            }

            fn install(&self, registry: &mut ExtensionRegistryBuilder) -> anyhow::Result<()> {
                registry.media_generator_provider(Arc::new(FakeImageProvider));
                Ok(())
            }
        }

        let mut builder = ExtensionRegistryBuilder::new();
        builder
            .install(FakeImageExtension)
            .expect("install image extension");
        let registry = builder.build().expect("build registry");

        assert!(
            registry
                .provided_services()
                .contains(&ProvidedService::MediaGenerator("fake".to_string()))
        );
        let provider = registry.media_generator("fake").expect("resolve provider");
        assert!(provider.descriptor().supports_images);
        assert!(registry.media_generator("missing").is_none());
    }

    #[test]
    fn provided_service_task_executor_round_trips_json() {
        let service = ProvidedService::TaskExecutor("process".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize task executor service");
        assert_eq!(encoded, serde_json::json!({ "TaskExecutor": "process" }));

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize task executor service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_code_index_provider_round_trips_json() {
        let service = ProvidedService::CodeIndexProvider("local-code-index".to_string());
        let encoded =
            serde_json::to_value(&service).expect("serialize code index provider service");
        assert_eq!(
            encoded,
            serde_json::json!({ "CodeIndexProvider": "local-code-index" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize code index provider service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_notification_sink_round_trips_json() {
        let service = ProvidedService::NotificationSink("terminal-bell".to_string());
        let encoded = serde_json::to_value(&service).expect("serialize notification sink service");
        assert_eq!(
            encoded,
            serde_json::json!({ "NotificationSink": "terminal-bell" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize notification sink service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_interactive_region_handler_round_trips_json() {
        let service = ProvidedService::InteractiveRegionHandler("links".to_string());
        let encoded =
            serde_json::to_value(&service).expect("serialize interactive region handler service");
        assert_eq!(
            encoded,
            serde_json::json!({ "InteractiveRegionHandler": "links" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize interactive region handler service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_remote_runner_provider_round_trips_json() {
        let service = ProvidedService::RemoteRunnerProvider("unix-local".to_string());
        let encoded =
            serde_json::to_value(&service).expect("serialize remote runner provider service");
        assert_eq!(
            encoded,
            serde_json::json!({ "RemoteRunnerProvider": "unix-local" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize remote runner provider service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn provided_service_version_control_provider_round_trips_json() {
        let service = ProvidedService::VersionControlProvider("git".to_string());
        let encoded =
            serde_json::to_value(&service).expect("serialize version control provider service");
        assert_eq!(
            encoded,
            serde_json::json!({ "VersionControlProvider": "git" })
        );

        let decoded = serde_json::from_value::<ProvidedService>(encoded)
            .expect("deserialize version control provider service");
        assert_eq!(decoded, service);
    }

    #[test]
    fn registry_builder_records_status_segments() {
        let mut builder = ExtensionRegistryBuilder::new();
        builder.status_segment(StatusSegment::new("custom", 42, 6, |_| StatusCell {
            text: "ready".to_string(),
            style: StatusStyle::Accent,
            tooltip: None,
        }));

        let registry = builder.build().expect("build registry");
        assert_eq!(registry.status_segments.len(), 1);
        assert_eq!(registry.status_segments[0].id, "custom");
        assert_eq!(registry.status_segments[0].priority, 42);
        assert_eq!(registry.status_segments[0].min_width, 6);
    }

    #[test]
    fn registry_builder_records_palette_sources() {
        let mut builder = ExtensionRegistryBuilder::new();
        builder.palette_source(PaletteSourceDescriptor {
            id: "commands".to_string(),
            label: "Commands".to_string(),
            priority: 100,
        });

        let registry = builder.build().expect("build registry");
        assert_eq!(registry.palette_sources.len(), 1);
        assert_eq!(registry.palette_sources[0].id, "commands");
        assert_eq!(registry.palette_sources[0].label, "Commands");
        assert_eq!(registry.palette_sources[0].priority, 100);
    }

    #[test]
    fn registering_vcs_provider_advertises_service_and_builds_registry() {
        let mut builder = ExtensionRegistryBuilder::new();
        builder
            .install(FakeVcsExtension::new("git"))
            .expect("install vcs extension");

        let registry = builder.build().expect("build registry");

        assert!(
            registry
                .provided_services()
                .contains(&ProvidedService::VersionControlProvider("git".to_string()))
        );
        assert!(registry.version_control_provider("git").is_some());
    }

    #[test]
    fn duplicate_vcs_provider_ids_fail_registry_validation() {
        let mut builder = ExtensionRegistryBuilder::new();
        builder.version_control_provider(Arc::new(FakeVcsProvider::new("git")));
        builder.version_control_provider(Arc::new(FakeVcsProvider::new("git")));

        let error = match builder.build() {
            Ok(_) => panic!("duplicate provider should fail"),
            Err(error) => error,
        };

        assert!(
            error
                .to_string()
                .contains("duplicate installed service VersionControlProvider(git)")
        );
    }

    #[test]
    fn installing_an_undeclared_service_fails_install() {
        let mut builder = ExtensionRegistryBuilder::new();

        let error = match builder.install(UndeclaredServiceExtension) {
            Ok(()) => panic!("undeclared service should fail install"),
            Err(error) => error,
        };

        assert!(
            error
                .to_string()
                .contains("installed undeclared service VersionControlProvider(git)"),
            "unexpected error: {error}"
        );
        assert!(builder.manifests.is_empty());
    }

    struct UndeclaredServiceExtension;

    impl RoderExtension for UndeclaredServiceExtension {
        fn manifest(&self) -> ExtensionManifest {
            ExtensionManifest {
                id: "undeclared-service-extension".to_string(),
                name: "Undeclared Service".to_string(),
                version: Version::new(0, 1, 0),
                api_version: SUPPORTED_EXTENSION_API_VERSION.to_string(),
                description: None,
                provides: Vec::new(),
                required_capabilities: Vec::new(),
            }
        }

        fn install(&self, registry: &mut ExtensionRegistryBuilder) -> anyhow::Result<()> {
            registry.version_control_provider(Arc::new(FakeVcsProvider::new("git")));
            Ok(())
        }
    }

    struct FakeVcsExtension {
        id: String,
    }

    impl FakeVcsExtension {
        fn new(id: impl Into<String>) -> Self {
            Self { id: id.into() }
        }
    }

    impl RoderExtension for FakeVcsExtension {
        fn manifest(&self) -> ExtensionManifest {
            ExtensionManifest {
                id: format!("{}-extension", self.id),
                name: "Fake VCS".to_string(),
                version: Version::new(0, 1, 0),
                api_version: SUPPORTED_EXTENSION_API_VERSION.to_string(),
                description: None,
                provides: vec![ProvidedService::VersionControlProvider(self.id.clone())],
                required_capabilities: Vec::new(),
            }
        }

        fn install(&self, registry: &mut ExtensionRegistryBuilder) -> anyhow::Result<()> {
            registry.version_control_provider(Arc::new(FakeVcsProvider::new(self.id.clone())));
            Ok(())
        }
    }

    struct FakeVcsProvider {
        id: String,
    }

    impl FakeVcsProvider {
        fn new(id: impl Into<String>) -> Self {
            Self { id: id.into() }
        }
    }

    #[async_trait::async_trait]
    impl VcsProvider for FakeVcsProvider {
        fn id(&self) -> crate::version_control::VcsProviderId {
            self.id.clone()
        }

        fn display_name(&self) -> String {
            self.id.clone()
        }

        async fn detect(
            &self,
            workspace_root: &Path,
        ) -> Result<Option<VcsDetectionClaim>, VcsError> {
            Ok(Some(VcsDetectionClaim {
                workspace: VcsWorkspace {
                    root: workspace_root.to_path_buf(),
                    id: None,
                },
                priority: 0,
                metadata: serde_json::Value::Null,
            }))
        }

        async fn status(&self, request: VcsStatusRequest) -> Result<VcsStatus, VcsError> {
            Ok(VcsStatus {
                provider: crate::version_control::VcsProviderIdentity {
                    id: self.id.clone(),
                    display_name: self.id.clone(),
                },
                workspace: VcsWorkspace {
                    root: request.workspace_root,
                    id: None,
                },
                active_line: None,
                base: None,
                capabilities: VcsCapabilities::default(),
                changed_file_count: 0,
            })
        }

        async fn list_changes(
            &self,
            _request: VcsListChangesRequest,
        ) -> Result<Vec<VcsChangedFile>, VcsError> {
            Ok(Vec::new())
        }

        async fn read_changed_content(
            &self,
            request: VcsReadChangedContentRequest,
        ) -> Result<VcsChangedContentPage, VcsError> {
            Ok(VcsChangedContentPage {
                path: PathBuf::from(request.path),
                content: Some(String::new()),
                offset: request.offset,
                total_lines: 0,
                next_offset: None,
                binary: false,
            })
        }
    }
}