1mod canonical;
15mod profile;
16mod transport;
17mod vscode;
18
19use chrono::Utc;
20use mcp_twill::{CliMcpServer, EffectSpec, InvocationPlan, RuntimeIdentity};
21
22pub use profile::*;
23pub use transport::*;
24pub use vscode::*;
25
26#[derive(Debug, Clone)]
31pub struct RuntimeHost {
32 identity: RuntimeIdentity,
33}
34
35impl RuntimeHost {
36 pub fn new(server: &CliMcpServer) -> Self {
39 let identity = attach_process_facts(server.runtime_identity());
40 Self { identity }
41 }
42
43 pub fn identity(&self) -> &RuntimeIdentity {
46 &self.identity
47 }
48
49 pub fn retry_decision(&self, plan: &InvocationPlan) -> RetryDecision {
54 retry_decision(plan)
55 }
56}
57
58fn attach_process_facts(identity: RuntimeIdentity) -> RuntimeIdentity {
59 let mut identity = identity;
60 identity.process_id = Some(std::process::id());
61 identity.started_at_unix_ms = Some(Utc::now().timestamp_millis());
62 identity
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum RetryDecision {
69 Retry,
71 RetryAsIdempotent,
75 NoRetry {
77 effect: EffectSpec,
79 },
80}
81
82impl RetryDecision {
83 pub fn is_retryable(&self) -> bool {
84 !matches!(self, RetryDecision::NoRetry { .. })
85 }
86}
87
88pub fn retry_decision(plan: &InvocationPlan) -> RetryDecision {
97 effect_retry(&plan.effect, plan.idempotent)
98}
99
100fn effect_retry(effect: &EffectSpec, idempotent: bool) -> RetryDecision {
101 match effect {
102 EffectSpec::Pure | EffectSpec::Read => RetryDecision::Retry,
103 EffectSpec::Composite(effects) => {
104 let mut decision = RetryDecision::Retry;
105 for part in effects {
106 match effect_retry(part, idempotent) {
107 blocked @ RetryDecision::NoRetry { .. } => return blocked,
108 RetryDecision::RetryAsIdempotent => {
109 decision = RetryDecision::RetryAsIdempotent;
110 }
111 RetryDecision::Retry => {}
112 }
113 }
114 decision
115 }
116 effect => {
120 if idempotent {
121 RetryDecision::RetryAsIdempotent
122 } else {
123 RetryDecision::NoRetry {
124 effect: effect.clone(),
125 }
126 }
127 }
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
136 fn pure_and_read_effects_retry_without_a_declaration() {
137 assert_eq!(effect_retry(&EffectSpec::Pure, false), RetryDecision::Retry);
138 assert_eq!(effect_retry(&EffectSpec::Read, false), RetryDecision::Retry);
139 }
140
141 #[test]
142 fn side_effects_do_not_retry_without_a_declaration() {
143 for effect in [
144 EffectSpec::Write,
145 EffectSpec::Delete,
146 EffectSpec::Exec,
147 EffectSpec::Network,
148 EffectSpec::Custom("sync".to_string()),
149 ] {
150 let decision = effect_retry(&effect, false);
151 assert!(!decision.is_retryable(), "{effect:?} must not retry");
152 assert_eq!(decision, RetryDecision::NoRetry { effect });
153 }
154 }
155
156 #[test]
157 fn side_effects_retry_only_when_declared_idempotent() {
158 for effect in [
159 EffectSpec::Write,
160 EffectSpec::Delete,
161 EffectSpec::Exec,
162 EffectSpec::Network,
163 EffectSpec::Custom("sync".to_string()),
164 ] {
165 assert_eq!(
166 effect_retry(&effect, true),
167 RetryDecision::RetryAsIdempotent,
168 "{effect:?} must retry when declared idempotent"
169 );
170 }
171 }
172
173 #[test]
174 fn composites_take_the_most_restrictive_answer() {
175 let read_write = EffectSpec::Composite(vec![EffectSpec::Read, EffectSpec::Write]);
176 assert_eq!(
177 effect_retry(&read_write, false),
178 RetryDecision::NoRetry {
179 effect: EffectSpec::Write
180 }
181 );
182 assert_eq!(
183 effect_retry(&read_write, true),
184 RetryDecision::RetryAsIdempotent
185 );
186
187 let read_only = EffectSpec::Composite(vec![EffectSpec::Read, EffectSpec::Pure]);
188 assert_eq!(effect_retry(&read_only, false), RetryDecision::Retry);
189 }
190}