chio-cross-protocol 0.1.2

Shared cross-protocol bridge contracts and orchestrator runtime for Chio
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
use std::collections::BTreeMap;
use std::time::{SystemTime, UNIX_EPOCH};

use chio_core::{canonical_json_bytes, sha256_hex};
use chio_kernel::{ChioKernel, ToolCallResponse, Verdict as KernelVerdict};
use serde_json::{json, Value};

use crate::capability_bridge::{
    attenuate_scope_for_tool, CapabilityBridge, CrossProtocolCapabilityEnvelope,
    CrossProtocolCapabilityRef, CrossProtocolTraceContext, ProtocolHop,
    CROSS_PROTOCOL_AUTHORITY_PATH, CROSS_PROTOCOL_CAPABILITY_ENVELOPE_SCHEMA,
};
use crate::discovery::{DiscoveryProtocol, TargetProtocolRegistry};
use crate::error::BridgeError;
use crate::execution::{
    kernel_tool_call_request, metadata_with_source_receipt_context, CrossProtocolExecutionRequest,
    CrossProtocolTargetExecution, CrossProtocolTargetRequest, TargetExecutionHop,
    TargetProtocolExecutor,
};
use crate::routing::{
    build_route_evidence, plan_authoritative_route, route_hops_from_planning,
    route_selection_metadata, CrossProtocolRouteEvidence, RouteAvailabilityStatus,
    RouteSelectionDecision, RouteSelectionEvidence,
};
use crate::validation::{validate_execution_request_boundary, validate_provided_capability_ref};

/// Result of executing a bridged call through the shared orchestrator.
#[derive(Debug)]
pub struct OrchestratedToolCall {
    pub response: ToolCallResponse,
    pub source_protocol: DiscoveryProtocol,
    pub target_protocol: DiscoveryProtocol,
    pub terminal_protocol: DiscoveryProtocol,
    pub bridge_id: String,
    pub capability_ref: CrossProtocolCapabilityRef,
    pub capability_envelope: CrossProtocolCapabilityEnvelope,
    pub trace: CrossProtocolTraceContext,
    pub route: CrossProtocolRouteEvidence,
    pub projected_request: Value,
    pub protocol_result: Option<Value>,
    pub protocol_notifications: Vec<Value>,
}

impl OrchestratedToolCall {
    #[must_use]
    pub fn metadata(&self) -> Value {
        json!({
            "chio": {
                "receiptId": self.response.receipt.id,
                "receipt": self.response.receipt,
                "receiptRef": {
                    "receiptId": self.response.receipt.id,
                    "capabilityId": self.response.receipt.capability_id,
                    "traceId": self.trace.trace_id,
                    "bridgeId": self.bridge_id,
                    "sourceProtocol": self.source_protocol,
                    "targetProtocol": self.target_protocol,
                },
                "decision": match self.response.verdict {
                    KernelVerdict::Allow => "allow",
                    KernelVerdict::Deny => "deny",
                    KernelVerdict::PendingApproval => "pending_approval",
                },
                "capabilityId": self.response.receipt.capability_id,
                "authorityPath": CROSS_PROTOCOL_AUTHORITY_PATH,
                "authoritative": true,
                "reason": self.response.reason,
                "terminalState": self.response.terminal_state,
                "executionNonce": self.response.execution_nonce,
                "routeSelection": self
                    .response
                    .receipt
                    .metadata
                    .as_ref()
                    .and_then(|metadata| metadata.get("route_selection").cloned()),
                "bridge": {
                    "bridgeId": self.bridge_id,
                    "sourceProtocol": self.source_protocol,
                    "targetProtocol": self.target_protocol,
                    "terminalProtocol": self.terminal_protocol,
                    "capabilityRef": self.capability_ref,
                    "capabilityEnvelope": self.capability_envelope,
                    "route": self.route,
                    "trace": self.trace,
                },
                "targetExecution": {
                    "projectedResult": self.protocol_result.is_some(),
                    "notificationCount": self.protocol_notifications.len(),
                    "routeHopCount": self.route.selected_protocols.len(),
                    "multiHop": self.route.multi_hop,
                    "terminalProtocol": self.terminal_protocol,
                }
            }
        })
    }
}

/// Shared cross-protocol runtime over the existing kernel substrate.
pub struct CrossProtocolOrchestrator<'a> {
    kernel: &'a ChioKernel,
    target_registry: TargetProtocolRegistry<'a>,
    route_availability: BTreeMap<DiscoveryProtocol, RouteAvailabilityStatus>,
}

impl<'a> CrossProtocolOrchestrator<'a> {
    #[must_use]
    pub fn new(kernel: &'a ChioKernel) -> Self {
        Self {
            kernel,
            target_registry: TargetProtocolRegistry::new(DiscoveryProtocol::Native),
            route_availability: BTreeMap::new(),
        }
    }

    #[must_use]
    pub fn with_executor(mut self, executor: &'a dyn TargetProtocolExecutor) -> Self {
        self.target_registry = self.target_registry.with_executor(executor);
        self
    }

    #[must_use]
    pub fn with_registry(mut self, registry: TargetProtocolRegistry<'a>) -> Self {
        self.target_registry = registry;
        self
    }

    #[must_use]
    pub fn with_protocol_availability(
        mut self,
        protocol: DiscoveryProtocol,
        availability: RouteAvailabilityStatus,
    ) -> Self {
        self.route_availability.insert(protocol, availability);
        self
    }

    pub fn execute<B: CapabilityBridge>(
        &self,
        bridge: &B,
        request: CrossProtocolExecutionRequest,
    ) -> Result<OrchestratedToolCall, BridgeError> {
        validate_execution_request_boundary(&request)?;
        let source_protocol = bridge.source_protocol();
        let provided_ref = bridge.extract_capability_ref(&request.source_envelope)?;
        let capability_ref = match provided_ref {
            Some(cap_ref) => {
                validate_provided_capability_ref(&cap_ref, &request.capability, source_protocol)?;
                cap_ref
            }
            None => CrossProtocolCapabilityRef::from_capability(
                &request.capability,
                source_protocol,
                bridge.protocol_context(&request.source_envelope)?,
            )?,
        };

        let mut projected_request = request.source_envelope.clone();
        bridge.inject_capability_ref(&mut projected_request, &capability_ref)?;

        let bridge_id = format!(
            "chio-bridge-{}-{}-{}",
            source_protocol, request.target_protocol, request.origin_request_id
        );
        let bridged_at = current_unix_timestamp();
        let attenuated_scope = attenuate_scope_for_tool(
            &request.capability.scope,
            &request.target_server_id,
            &request.target_tool_name,
        );
        if !attenuated_scope.is_subset_of(&request.capability.scope) {
            return Err(BridgeError::InvalidAttenuation(
                "attenuated scope must remain a strict subset of the parent capability".to_string(),
            ));
        }

        let planning = plan_authoritative_route(
            &request.origin_request_id,
            source_protocol,
            request.target_protocol,
            request.governed_intent.as_ref(),
            &self.target_registry,
            &self.route_availability,
        )?;

        if planning.evidence.decision == RouteSelectionDecision::Deny {
            let deny_reason = planning
                .evidence
                .reason
                .clone()
                .unwrap_or_else(|| "route selection denied".to_string());
            let response = self
                .kernel
                .sign_planned_deny_response(
                    &kernel_tool_call_request(&request),
                    &deny_reason,
                    Some(route_selection_metadata(&planning.evidence)?),
                )
                .map_err(BridgeError::Kernel)?;
            let deny_route_hops = route_hops_from_planning(
                &planning.evidence,
                &request.kernel_request_id,
                &response.receipt.id,
            );
            let route = build_route_evidence(source_protocol, &deny_route_hops)?;
            let trace = build_trace_context(
                &request,
                source_protocol,
                &bridge_id,
                &deny_route_hops,
                bridged_at,
            )?;
            let capability_envelope = CrossProtocolCapabilityEnvelope {
                schema: CROSS_PROTOCOL_CAPABILITY_ENVELOPE_SCHEMA.to_string(),
                capability_ref: capability_ref.clone(),
                target_protocol: request.target_protocol,
                attenuated_scope: attenuated_scope.clone(),
                bridged_at,
                bridge_id: bridge_id.clone(),
            };

            return Ok(OrchestratedToolCall {
                response,
                source_protocol,
                target_protocol: request.target_protocol,
                terminal_protocol: route.terminal_protocol,
                bridge_id,
                capability_ref,
                capability_envelope,
                trace,
                route,
                projected_request,
                protocol_result: None,
                protocol_notifications: Vec::new(),
            });
        }

        let selected_target_protocol = planning.selected_target_protocol.ok_or_else(|| {
            BridgeError::InvalidRequest(
                "route planner returned no selected target protocol".to_string(),
            )
        })?;
        let mut selected_request = request.clone();
        selected_request.target_protocol = selected_target_protocol;
        let capability_envelope = CrossProtocolCapabilityEnvelope {
            schema: CROSS_PROTOCOL_CAPABILITY_ENVELOPE_SCHEMA.to_string(),
            capability_ref: capability_ref.clone(),
            target_protocol: selected_request.target_protocol,
            attenuated_scope,
            bridged_at,
            bridge_id: bridge_id.clone(),
        };

        let target_execution = self.execute_target(
            &selected_request,
            source_protocol,
            &bridge_id,
            &capability_ref,
            &capability_envelope,
            &planning.evidence,
            &projected_request,
        )?;
        let route = build_route_evidence(source_protocol, &target_execution.route_hops)?;

        let trace = build_trace_context(
            &selected_request,
            source_protocol,
            &bridge_id,
            &target_execution.route_hops,
            bridged_at,
        )?;

        Ok(OrchestratedToolCall {
            response: target_execution.response,
            source_protocol,
            target_protocol: selected_request.target_protocol,
            terminal_protocol: route.terminal_protocol,
            bridge_id,
            capability_ref,
            capability_envelope,
            trace,
            route,
            projected_request,
            protocol_result: target_execution.protocol_result,
            protocol_notifications: target_execution.protocol_notifications,
        })
    }

    #[allow(clippy::too_many_arguments)]
    fn execute_target(
        &self,
        request: &CrossProtocolExecutionRequest,
        source_protocol: DiscoveryProtocol,
        bridge_id: &str,
        capability_ref: &CrossProtocolCapabilityRef,
        capability_envelope: &CrossProtocolCapabilityEnvelope,
        route_selection: &RouteSelectionEvidence,
        projected_request: &Value,
    ) -> Result<CrossProtocolTargetExecution, BridgeError> {
        if request.target_protocol == DiscoveryProtocol::Native {
            let route_metadata = metadata_with_source_receipt_context(
                route_selection_metadata(route_selection)?,
                &request.source_envelope,
            )?;
            let response = self
                .kernel
                .evaluate_tool_call_blocking_with_metadata(
                    &kernel_tool_call_request(request),
                    Some(route_metadata),
                )
                .map_err(BridgeError::Kernel)?;
            let receipt_id = response.receipt.id.clone();
            return Ok(CrossProtocolTargetExecution {
                response,
                protocol_result: None,
                protocol_notifications: Vec::new(),
                route_hops: vec![TargetExecutionHop {
                    protocol: DiscoveryProtocol::Native,
                    request_id: request.kernel_request_id.clone(),
                    receipt_id: Some(receipt_id),
                }],
            });
        }

        let executor = self
            .target_registry
            .executor_for_target(request.target_protocol)
            .ok_or(BridgeError::UnsupportedTargetProtocol(
                request.target_protocol,
            ))?;

        executor.execute(CrossProtocolTargetRequest {
            kernel: self.kernel,
            execution: request,
            source_protocol,
            bridge_id,
            capability_ref,
            capability_envelope,
            route_selection,
            projected_request,
        })
    }
}

fn build_trace_context(
    request: &CrossProtocolExecutionRequest,
    source_protocol: DiscoveryProtocol,
    bridge_id: &str,
    route_hops: &[TargetExecutionHop],
    timestamp: u64,
) -> Result<CrossProtocolTraceContext, BridgeError> {
    let route_protocols = route_hops
        .iter()
        .map(|hop| hop.protocol.as_str())
        .collect::<Vec<_>>();
    let trace_id = sha256_hex(
        &canonical_json_bytes(&json!({
            "originRequestId": request.origin_request_id,
            "kernelRequestId": request.kernel_request_id,
            "sourceProtocol": source_protocol,
            "targetProtocol": request.target_protocol,
            "routeProtocols": route_protocols,
            "capabilityId": request.capability.id,
            "bridgeId": bridge_id,
        }))
        .map_err(|error| BridgeError::Canonical(error.to_string()))?,
    );
    let session_fingerprint = sha256_hex(
        &canonical_json_bytes(&json!({
            "agentId": request.agent_id,
            "capabilityId": request.capability.id,
            "sourceProtocol": source_protocol,
            "bridgeId": bridge_id,
        }))
        .map_err(|error| BridgeError::Canonical(error.to_string()))?,
    );

    Ok(CrossProtocolTraceContext {
        trace_id,
        session_fingerprint,
        hops: std::iter::once(ProtocolHop {
            protocol: source_protocol,
            request_id: request.origin_request_id.clone(),
            receipt_id: None,
            bridge_id: bridge_id.to_string(),
            timestamp,
        })
        .chain(route_hops.iter().map(|hop| ProtocolHop {
            protocol: hop.protocol,
            request_id: hop.request_id.clone(),
            receipt_id: hop.receipt_id.clone(),
            bridge_id: bridge_id.to_string(),
            timestamp,
        }))
        .collect(),
    })
}

pub(crate) fn current_unix_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}