cf-gears-toolkit 0.15.0

Core ToolKit library
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
//! Consumer-side discovery wiring for eventual readiness.
//!
//! Bridges the service [`DirectoryClient`](crate::DirectoryClient) into the
//! contract layer's [`EndpointResolver`], and carries the
//! [`ConsumerRegistration`]s emitted by `#[toolkit::consumes]` that the
//! runtime's proxy-wiring phase replays at startup.
//!
//! Unconditional: consuming another gear over a discovered REST transport is
//! the normal case, and `#[toolkit::consumes]` emits its registration
//! unconditionally too. Putting this behind a feature only created a way to
//! build a gear whose declared dependencies were silently never wired.

use std::sync::Arc;

use async_trait::async_trait;

use cf_system_sdks::directory::{DirectoryInvalidArgument, DirectoryNotFound};

use crate::DirectoryClient;
use crate::client_hub::ClientHub;

/// Re-export so the `#[toolkit::consumes]`-generated wire fn can name the
/// resolver type as `toolkit::discovery::EndpointResolver` without the consuming
/// gear crate needing a direct `toolkit-contract` dependency.
pub use toolkit_contract::runtime::resolving::{EndpointResolver, ResolveError, ResolveTarget};

/// Adapts the service [`DirectoryClient`] into the contract-layer
/// [`EndpointResolver`] consumed by `DirectoryResolvingClient`.
///
/// Keeps `toolkit-contract` free of a dependency on the directory SDK: the
/// adapter lives here in `toolkit`, which already owns `DirectoryClient`.
///
/// Successful resolutions are memoized for a short TTL ([`Self::TTL`]) so a
/// per-call resolving client does not incur a directory round-trip on every
/// business call (the `OoP` directory is a network hop). Only `Ok(Some(uri))` is
/// cached; `Ok(None)` / `Err` always re-resolve. The TTL is deliberately short
/// so endpoint churn self-heals within ~TTL, and an endpoint failure seen by a
/// client drops the entry at once ([`EndpointResolver::invalidate`]).
///
/// REST endpoints are looked up by gear name, gRPC endpoints by service name
/// (`resolve_grpc_service`); the two are cached apart.
pub struct DirectoryEndpointResolver {
    client: Arc<dyn DirectoryClient>,
    cache: parking_lot::Mutex<std::collections::HashMap<String, (String, std::time::Instant)>>,
}

impl DirectoryEndpointResolver {
    /// Memoization window for successful resolutions.
    ///
    /// Public because it bounds observable behaviour: a provider that moves is
    /// followed within roughly this window, or at the next call after a client
    /// saw the old endpoint fail.
    pub const TTL: std::time::Duration = std::time::Duration::from_millis(1500);

    /// Wrap a [`DirectoryClient`] with a short-TTL resolution cache.
    #[must_use]
    pub fn new(client: Arc<dyn DirectoryClient>) -> Self {
        Self {
            client,
            cache: parking_lot::Mutex::new(std::collections::HashMap::new()),
        }
    }

    fn cached(&self, key: &str) -> Option<String> {
        self.cache
            .lock()
            .get(key)
            .filter(|(_, at)| at.elapsed() < Self::TTL)
            .map(|(uri, _)| uri.clone())
    }

    fn store(&self, key: &str, uri: &str) {
        self.cache
            .lock()
            .insert(key.to_owned(), (uri.to_owned(), std::time::Instant::now()));
    }

    fn cache_key(target: &ResolveTarget) -> String {
        match target {
            ResolveTarget::Grpc { service, .. } => format!("grpc:{service}"),
            // `ResolveTarget` is non-exhaustive; anything else is keyed by gear.
            other => format!("rest:{}", other.gear()),
        }
    }

    /// Look `name` up through `lookup`, memoizing a hit under `key`. `name`
    /// is the gear or service name, for errors and logs.
    async fn resolve_cached<F, Fut>(
        &self,
        key: &str,
        name: &str,
        lookup: F,
    ) -> Result<Option<String>, ResolveError>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = anyhow::Result<crate::ServiceEndpoint>>,
    {
        // Fresh cache hit avoids the directory round-trip.
        if let Some(uri) = self.cached(key) {
            return Ok(Some(uri));
        }
        // Preserve the trait's `Ok(None)` (no live instance / not ready) vs
        // `Err` (directory backend unreachable) distinction so a real directory
        // outage surfaces at `warn` rather than being silently treated as
        // "provider not ready". The directory returns the typed
        // `DirectoryNotFound` sentinel for the not-found case; anything else is
        // a genuine backend/transport failure (relevant for the OoP gRPC
        // directory client).
        match lookup().await {
            Ok(ep) => {
                self.store(key, &ep.uri);
                Ok(Some(ep.uri))
            }
            Err(e) if e.downcast_ref::<DirectoryNotFound>().is_some() => Ok(None),
            Err(e) if e.downcast_ref::<DirectoryInvalidArgument>().is_some() => {
                // A malformed provider name is a permanent configuration error,
                // not a transient outage: re-resolving can never succeed. Log
                // it distinctly from the generic backend-failure path (at
                // `error`, not the caller's transient `warn`) so the actionable
                // "fix your config" message is not mistaken for a directory
                // outage, and still return `Err` so the call fails.
                tracing::error!(
                    name,
                    error = %e,
                    "provider name permanently rejected by the directory (invalid \
                     argument); this is a static configuration error, not a directory \
                     outage, and will never resolve. Check the consumed gear or service name"
                );
                Err(ResolveError::new(name, e))
            }
            Err(e) => Err(ResolveError::new(name, e)),
        }
    }
}

#[async_trait]
impl EndpointResolver for DirectoryEndpointResolver {
    async fn resolve_endpoint(&self, gear: &str) -> Result<Option<String>, ResolveError> {
        let key = Self::cache_key(&ResolveTarget::rest(gear));
        self.resolve_cached(&key, gear, || self.client.resolve_rest_service(gear))
            .await
    }

    async fn resolve_grpc_endpoint(&self, service: &str) -> Result<Option<String>, ResolveError> {
        let key = Self::cache_key(&ResolveTarget::grpc("", service));
        self.resolve_cached(&key, service, || self.client.resolve_grpc_service(service))
            .await
    }

    fn invalidate(&self, target: &ResolveTarget) {
        self.cache.lock().remove(&Self::cache_key(target));
    }
}

/// An [`EndpointResolver`] that never resolves any gear (`Ok(None)` for every
/// lookup). Used by the proxy-wiring phase when no [`DirectoryClient`] is present
/// in the [`ClientHub`]: co-located (local) consumers still short-circuit and
/// wire successfully, while remote consumers register as *unresolved* readiness
/// gates (so `/readyz` correctly stays 503) rather than being silently skipped.
pub struct NullEndpointResolver;

#[async_trait]
impl EndpointResolver for NullEndpointResolver {
    async fn resolve_endpoint(&self, _gear: &str) -> Result<Option<String>, ResolveError> {
        Ok(None)
    }
}

/// An [`EndpointResolver`] that always resolves to a fixed endpoint — the
/// ADR-0004 static-endpoint override. It bypasses the service directory: a
/// consumer configured with `gears.<owner>.clients.consumes.<dep>.endpoint`
/// wires a resolving client whose endpoint is this constant. gRPC lookups get
/// `grpc_endpoint` when one is set, else the same endpoint.
pub struct StaticEndpointResolver {
    endpoint: String,
    grpc_endpoint: Option<String>,
}

impl StaticEndpointResolver {
    /// Wrap a fixed base endpoint URI.
    #[must_use]
    pub fn new(endpoint: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            grpc_endpoint: None,
        }
    }

    /// Resolve gRPC services to `endpoint` instead.
    #[must_use]
    pub fn with_grpc_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.grpc_endpoint = Some(endpoint.into());
        self
    }
}

#[async_trait]
impl EndpointResolver for StaticEndpointResolver {
    async fn resolve_endpoint(&self, _gear: &str) -> Result<Option<String>, ResolveError> {
        Ok(Some(self.endpoint.clone()))
    }

    async fn resolve_grpc_endpoint(&self, _service: &str) -> Result<Option<String>, ResolveError> {
        Ok(Some(
            self.grpc_endpoint
                .clone()
                .unwrap_or_else(|| self.endpoint.clone()),
        ))
    }
}

/// Static overrides for some transports, the directory for the rest. Built by
/// the proxy-wiring phase from `clients.consumes.<dep>.endpoint` /
/// `grpc_endpoint`.
pub(crate) struct OverrideResolver {
    /// Fixed REST endpoint, from `endpoint`.
    pub(crate) rest: Option<String>,
    /// Fixed gRPC endpoint, from `grpc_endpoint`, else `endpoint`.
    pub(crate) grpc: Option<String>,
    /// Resolves a transport that has no override.
    pub(crate) fallback: Arc<dyn EndpointResolver>,
}

#[async_trait]
impl EndpointResolver for OverrideResolver {
    async fn resolve_endpoint(&self, gear: &str) -> Result<Option<String>, ResolveError> {
        match &self.rest {
            Some(uri) => Ok(Some(uri.clone())),
            None => self.fallback.resolve_endpoint(gear).await,
        }
    }

    async fn resolve_grpc_endpoint(&self, service: &str) -> Result<Option<String>, ResolveError> {
        match &self.grpc {
            Some(uri) => Ok(Some(uri.clone())),
            None => self.fallback.resolve_grpc_endpoint(service).await,
        }
    }

    fn invalidate(&self, target: &ResolveTarget) {
        self.fallback.invalidate(target);
    }
}

/// Outcome of a [`ConsumerRegistration::wire`] call: whether the dependency was
/// satisfied by a co-located compile-time impl (no directory dependency) or by
/// a directory-resolving client that still needs endpoint resolution.
///
/// The runtime uses this to gate readiness: `Local` deps are readiness-resolved
/// immediately (no `/readyz` gating, no probe loop), while remote deps gate
/// readiness until the directory resolves what the client will call (ADR-0007).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WireOutcome {
    /// A compile-time (in-process) impl was already registered — it won the
    /// `hub.try_get_local` short-circuit; no remote binding was created.
    Local,
    /// A directory-resolving REST client was registered; readiness waits for
    /// the provider gear's REST endpoint.
    Remote,
    /// A directory-resolving gRPC client was registered; readiness waits for
    /// the directory to resolve `service`.
    RemoteGrpc {
        /// Fully qualified gRPC service name (`package.Service`).
        service: &'static str,
    },
}

/// A consumer→provider contract wiring, registered by `#[toolkit::consumes]`
/// via `inventory::submit!` and replayed by the runtime's proxy-wiring phase.
///
/// `wire` is a non-capturing `fn` (the provider gear name is inlined into the
/// generated body). It must:
///   1. short-circuit returning [`WireOutcome::Local`] if a compile-time (local)
///      impl is already registered (`hub.try_get_local::<dyn Trait>().is_some()`),
///      so in-process providers win;
///   2. otherwise register a directory-resolving client under the contract
///      trait via [`ClientHub::register_remote_proxy`] (using the supplied
///      [`EndpointResolver`]) and return [`WireOutcome::Remote`] for REST or
///      [`WireOutcome::RemoteGrpc`] for gRPC.
///
/// The runtime passes the owner gear's view ([`ClientHub::for_gear`]) when
/// `owner_gear` is a gear in this process, so each consumer gets its own proxy
/// built from its own `clients.consumes.<dep>` settings; otherwise it passes the
/// process-wide hub.
///
/// Step 1 must use `try_get_local`, not `try_get`: `try_get` also returns a
/// process-wide proxy, and reporting that as `Local` would mark the dependency
/// readiness-resolved — flipping `/readyz` to 200 while the provider is still
/// absent.
pub struct ConsumerRegistration {
    /// Gear that declares the dependency (diagnostics).
    pub owner_gear: &'static str,
    /// Provider gear name the contract resolves against (diagnostics).
    pub dep_gear: &'static str,
    /// Wiring action: register the client into the hub; reports whether the
    /// dependency was satisfied locally or bound to a remote client. See
    /// [`WireFn`] for the argument roles.
    pub wire: WireFn,
}

/// The wiring function pointer emitted by `#[toolkit::consumes]`: registers the
/// client into the `ClientHub` from an endpoint resolver and a prepared
/// [`ClientTuning`](toolkit_contract::wiring::ClientTuning) (the `clients.consumes`
/// knobs plus the process's platform-plane credential source). Aliased to keep
/// the fn-pointer type readable.
pub type WireFn = fn(
    &ClientHub,
    Arc<dyn EndpointResolver>,
    toolkit_contract::wiring::ClientTuning,
) -> anyhow::Result<WireOutcome>;

inventory::collect!(ConsumerRegistration);

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use cf_system_sdks::directory::{RegisterInstanceInfo, ServiceEndpoint, ServiceInstanceInfo};
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Counts `resolve_rest_service` / `resolve_grpc_service` calls; all other
    /// methods are unused here.
    struct CountingDirectory {
        calls: AtomicUsize,
    }

    #[async_trait]
    impl DirectoryClient for CountingDirectory {
        async fn resolve_grpc_service(&self, service: &str) -> anyhow::Result<ServiceEndpoint> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(ServiceEndpoint::new(format!("http://grpc.local/{service}")))
        }
        async fn resolve_rest_service(&self, gear: &str) -> anyhow::Result<ServiceEndpoint> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(ServiceEndpoint::new(format!("http://{gear}.local")))
        }
        async fn get_openapi_spec(&self, _: &str) -> anyhow::Result<String> {
            unimplemented!()
        }
        async fn list_instances(&self, _: &str) -> anyhow::Result<Vec<ServiceInstanceInfo>> {
            unimplemented!()
        }
        async fn list_all_instances(&self) -> anyhow::Result<Vec<ServiceInstanceInfo>> {
            unimplemented!()
        }
        async fn register_instance(&self, _: RegisterInstanceInfo) -> anyhow::Result<()> {
            unimplemented!()
        }
        async fn deregister_instance(&self, _: &str, _: &str) -> anyhow::Result<()> {
            unimplemented!()
        }
        async fn send_heartbeat(&self, _: &str, _: &str) -> anyhow::Result<()> {
            unimplemented!()
        }
    }

    #[tokio::test]
    async fn memoizes_successful_resolution_within_ttl() {
        let dir = Arc::new(CountingDirectory {
            calls: AtomicUsize::new(0),
        });
        let resolver = DirectoryEndpointResolver::new(dir.clone());

        for _ in 0..3 {
            let ep = resolver.resolve_endpoint("billing").await.unwrap();
            assert_eq!(ep.as_deref(), Some("http://billing.local"));
        }

        assert_eq!(
            dir.calls.load(Ordering::SeqCst),
            1,
            "successful resolution must be memoized within the TTL (one directory lookup)"
        );
    }

    #[tokio::test]
    async fn grpc_lookups_go_by_service_name_and_are_cached_apart() {
        let dir = Arc::new(CountingDirectory {
            calls: AtomicUsize::new(0),
        });
        let resolver = DirectoryEndpointResolver::new(dir.clone());

        let grpc = resolver
            .resolve_grpc_endpoint("billing.v1.BillingApi")
            .await
            .unwrap();
        assert_eq!(
            grpc.as_deref(),
            Some("http://grpc.local/billing.v1.BillingApi")
        );
        let rest = resolver.resolve_endpoint("billing").await.unwrap();
        assert_eq!(rest.as_deref(), Some("http://billing.local"));
        resolver
            .resolve_grpc_endpoint("billing.v1.BillingApi")
            .await
            .unwrap();

        assert_eq!(dir.calls.load(Ordering::SeqCst), 2, "one lookup per target");
    }

    #[tokio::test]
    async fn invalidate_drops_only_that_targets_answer() {
        let dir = Arc::new(CountingDirectory {
            calls: AtomicUsize::new(0),
        });
        let resolver = DirectoryEndpointResolver::new(dir.clone());
        resolver.resolve_endpoint("billing").await.unwrap();
        resolver
            .resolve_grpc_endpoint("billing.v1.BillingApi")
            .await
            .unwrap();

        resolver.invalidate(&ResolveTarget::grpc("billing", "billing.v1.BillingApi"));
        resolver.resolve_endpoint("billing").await.unwrap();
        resolver
            .resolve_grpc_endpoint("billing.v1.BillingApi")
            .await
            .unwrap();

        assert_eq!(
            dir.calls.load(Ordering::SeqCst),
            3,
            "only the gRPC answer was looked up again"
        );
    }

    #[tokio::test]
    async fn the_static_resolver_can_pin_grpc_apart() {
        let both = StaticEndpointResolver::new("http://svc:8080");
        let split =
            StaticEndpointResolver::new("http://svc:8080").with_grpc_endpoint("http://svc:50051");

        assert_eq!(
            both.resolve_grpc_endpoint("x.v1.X")
                .await
                .unwrap()
                .as_deref(),
            Some("http://svc:8080")
        );
        assert_eq!(
            split
                .resolve_grpc_endpoint("x.v1.X")
                .await
                .unwrap()
                .as_deref(),
            Some("http://svc:50051")
        );
        assert_eq!(
            split.resolve_endpoint("svc").await.unwrap().as_deref(),
            Some("http://svc:8080")
        );
    }

    #[tokio::test]
    async fn static_resolver_always_returns_fixed_endpoint() {
        let resolver = StaticEndpointResolver::new("http://localhost:8081");
        assert_eq!(
            resolver
                .resolve_endpoint("anything")
                .await
                .unwrap()
                .as_deref(),
            Some("http://localhost:8081")
        );
        assert_eq!(
            resolver.resolve_endpoint("other").await.unwrap().as_deref(),
            Some("http://localhost:8081")
        );
    }

    #[tokio::test]
    async fn null_resolver_never_resolves() {
        assert_eq!(
            NullEndpointResolver
                .resolve_endpoint("billing")
                .await
                .unwrap(),
            None
        );
    }

    /// Answers every lookup with a caller-chosen failure, so the resolver's
    /// two error branches can be told apart.
    struct FailingDirectory {
        make_error: fn(&str) -> anyhow::Error,
    }

    #[async_trait]
    impl DirectoryClient for FailingDirectory {
        async fn resolve_grpc_service(&self, _: &str) -> anyhow::Result<ServiceEndpoint> {
            unimplemented!()
        }
        async fn resolve_rest_service(&self, gear: &str) -> anyhow::Result<ServiceEndpoint> {
            Err((self.make_error)(gear))
        }
        async fn get_openapi_spec(&self, _: &str) -> anyhow::Result<String> {
            unimplemented!()
        }
        async fn list_instances(&self, _: &str) -> anyhow::Result<Vec<ServiceInstanceInfo>> {
            unimplemented!()
        }
        async fn list_all_instances(&self) -> anyhow::Result<Vec<ServiceInstanceInfo>> {
            unimplemented!()
        }
        async fn register_instance(&self, _: RegisterInstanceInfo) -> anyhow::Result<()> {
            unimplemented!()
        }
        async fn deregister_instance(&self, _: &str, _: &str) -> anyhow::Result<()> {
            unimplemented!()
        }
        async fn send_heartbeat(&self, _: &str, _: &str) -> anyhow::Result<()> {
            unimplemented!()
        }
    }

    #[tokio::test]
    async fn not_found_sentinel_resolves_to_ok_none() {
        // "The provider has not registered yet" is a routine startup condition,
        // not a failure: it must come back as `Ok(None)` so the readiness probe
        // logs it at `debug` and the resolving client reports `Unresolved`
        // rather than warning on every call.
        let dir = Arc::new(FailingDirectory {
            make_error: |gear| DirectoryNotFound::new(format!("gear {gear}")).into(),
        });
        let resolver = DirectoryEndpointResolver::new(dir);

        assert_eq!(resolver.resolve_endpoint("billing").await.unwrap(), None);
    }

    #[tokio::test]
    async fn backend_failure_resolves_to_err() {
        // Anything that is not the sentinel is a genuine directory outage and
        // must stay an error, so a stuck 503 has a diagnostic trail.
        let dir = Arc::new(FailingDirectory {
            make_error: |_| anyhow::anyhow!("connection refused"),
        });
        let resolver = DirectoryEndpointResolver::new(dir);

        assert!(resolver.resolve_endpoint("billing").await.is_err());
    }

    #[tokio::test]
    #[tracing_test::traced_test]
    async fn invalid_argument_resolves_to_err_and_logs_config_error() {
        // A malformed provider name is a permanent configuration error, handled
        // by the dedicated arm. Unlike the not-found sentinel (`Ok(None)`), it
        // must stay an `Err` so the call fails rather than being treated as a
        // provider that is merely not ready yet. It must also be logged
        // distinctly at `error` (a static configuration error) so it is not
        // folded into the generic backend-failure path, which emits no log of
        // its own at the resolver.
        let dir = Arc::new(FailingDirectory {
            make_error: |gear| DirectoryInvalidArgument::new(format!("bad name {gear}")).into(),
        });
        let resolver = DirectoryEndpointResolver::new(dir);

        assert!(resolver.resolve_endpoint("Bad Name").await.is_err());

        logs_assert(|lines: &[&str]| {
            match lines
                .iter()
                .find(|line| line.contains("permanently rejected by the directory"))
            {
                Some(line) if line.contains("ERROR") => Ok(()),
                Some(_) => Err("invalid-argument arm logged at the wrong level".to_owned()),
                None => {
                    Err("invalid-argument arm must emit a distinct config-error log".to_owned())
                }
            }
        });
    }

    /// Mirrors the body `#[toolkit::consumes]` generates, so the wiring
    /// contract is exercised without going through `inventory` (which is
    /// link-time global and cannot hold two registrations built by a test).
    fn generated_wire_body<C>(hub: &ClientHub, proxy: Arc<C>) -> anyhow::Result<WireOutcome>
    where
        C: ?Sized + Send + Sync + 'static,
    {
        if hub.try_get_local::<C>().is_some() {
            return Ok(WireOutcome::Local);
        }
        if hub.has_remote_proxy::<C>() {
            if let Some(owner) = hub.owner() {
                anyhow::bail!("gear `{owner}` declares #[toolkit::consumes] more than once");
            }
            return Ok(WireOutcome::Remote);
        }
        hub.register_remote_proxy::<C>(proxy);
        Ok(WireOutcome::Remote)
    }

    trait Payments: Send + Sync {}
    struct PaymentsProxy;
    impl Payments for PaymentsProxy {}
    struct PaymentsLocal;
    impl Payments for PaymentsLocal {}

    #[test]
    fn two_consumers_of_one_contract_get_their_own_proxies() {
        // Two gears in one process consuming the same contract from the same
        // provider. Each must get its own proxy (built from its own wiring
        // settings), and neither may short-circuit to `Local`, which would mark
        // the dependency readiness-resolved with no provider up.
        let hub = ClientHub::new();
        let a = hub.for_gear("a");
        let b = hub.for_gear("b");

        let first = generated_wire_body::<dyn Payments>(&a, Arc::new(PaymentsProxy)).unwrap();
        let second = generated_wire_body::<dyn Payments>(&b, Arc::new(PaymentsProxy)).unwrap();

        assert_eq!(first, WireOutcome::Remote);
        assert_eq!(
            second,
            WireOutcome::Remote,
            "the second consumer must still gate readiness, not short-circuit to Local"
        );
        assert!(!Arc::ptr_eq(
            &a.get::<dyn Payments>().unwrap(),
            &b.get::<dyn Payments>().unwrap()
        ));
    }

    #[test]
    fn a_gear_consuming_one_contract_twice_is_an_error() {
        let hub = ClientHub::new();
        let a = hub.for_gear("a");

        generated_wire_body::<dyn Payments>(&a, Arc::new(PaymentsProxy)).unwrap();
        let err = generated_wire_body::<dyn Payments>(&a, Arc::new(PaymentsProxy)).unwrap_err();

        assert!(err.to_string().contains("more than once"), "{err}");
    }

    #[test]
    fn the_process_wide_hub_reuses_its_proxy() {
        // Registrations whose owner is not a gear here (cluster-sdk's) wire into
        // the plain hub and share one proxy.
        let hub = ClientHub::new();

        let first = generated_wire_body::<dyn Payments>(&hub, Arc::new(PaymentsProxy)).unwrap();
        let second = generated_wire_body::<dyn Payments>(&hub, Arc::new(PaymentsProxy)).unwrap();

        assert_eq!((first, second), (WireOutcome::Remote, WireOutcome::Remote));
    }

    #[test]
    fn a_genuine_local_impl_still_wins() {
        // The short-circuit must keep working for its actual purpose: a
        // co-located provider registered during init means no HTTP hop and no
        // readiness gate.
        let hub = ClientHub::new();
        hub.register::<dyn Payments>(Arc::new(PaymentsLocal));

        let outcome = generated_wire_body::<dyn Payments>(&hub, Arc::new(PaymentsProxy)).unwrap();

        assert_eq!(outcome, WireOutcome::Local);
    }

    #[tokio::test]
    async fn local_directory_client_not_found_reaches_the_resolver_as_ok_none() {
        // End-to-end over the real in-process client rather than a stub: this
        // is the pairing that was broken (the client returned a bare `anyhow`,
        // so the resolver could never produce `Ok(None)`).
        let dir = Arc::new(crate::directory::LocalDirectoryClient::new(Arc::new(
            crate::runtime::GearManager::new(),
        )));
        let resolver = DirectoryEndpointResolver::new(dir);

        assert_eq!(
            resolver.resolve_endpoint("never-registered").await.unwrap(),
            None
        );
    }
}