agentmux_srv/server/app_api/agent_spawn/import/
moved.rs1use std::path::Path;
13
14use super::{apply, load, make_plan, Mode};
15use crate::backend::agent_archive::SignatureState;
16use super::AppState;
17
18pub(crate) struct Expect<'a> {
20 pub manifest_hash: &'a str,
21 pub source_public: [u8; 32],
23 pub uid: &'a str,
24 pub name: &'a str,
25 pub route: Option<&'a crate::backend::agent_move::signed::RouteRef>,
27 pub route_shown: &'a str,
30}
31
32#[derive(Debug, PartialEq, Eq)]
34pub(crate) enum RouteCall {
35 None,
37 Keep(String),
39 Drop(String),
41}
42
43impl RouteCall {
44 pub(crate) fn shown(&self) -> String {
46 match self {
47 RouteCall::None => String::new(),
48 RouteCall::Keep(text) | RouteCall::Drop(text) => text.clone(),
49 }
50 }
51}
52
53pub(crate) fn missing_key(state: &AppState, named: Option<&crate::backend::agent_move::signed::RouteRef>) -> Option<&'static str> {
56 let r = named?;
57 let (route, _) = crate::backend::model_gateway::catalog::validate(&r.harness, &r.route, &r.model).ok()?;
58 (!crate::backend::vault::route_key::present(&state.identity_store, route.key_label)).then_some(route.key_label)
59}
60
61pub(crate) fn route_call(state: &AppState, named: Option<&crate::backend::agent_move::signed::RouteRef>) -> RouteCall {
63 use crate::backend::model_gateway::{ask, catalog};
64 let Some(r) = named else { return RouteCall::None };
65 match catalog::validate(&r.harness, &r.route, &r.model) {
66 Err(e) => RouteCall::Drop(format!("its route {}/{} is not available here ({e}): it uses its harness's own sign-in until a route is chosen", r.route, r.model)),
67 Ok((route, model)) => {
68 if !crate::backend::vault::route_key::present(&state.identity_store, route.key_label) {
69 return RouteCall::Drop(format!(
70 "its route {} needs the \"{}\" key in the Vault, which this computer does not have: it uses its harness's own sign-in until the key is added and a route is chosen",
71 route.label, route.key_label
72 ));
73 }
74 RouteCall::Keep(ask::consent_line(&ask::Picked { route, model }).trim().to_string())
75 }
76 }
77}
78
79#[derive(Debug, PartialEq, Eq)]
81pub(crate) enum Why {
82 NotWhatWasOffered(String),
84 Blocked(String),
86}
87
88pub(crate) struct Verified {
90 loaded: super::Loaded,
91 plan: super::Plan,
92 needs_key: Option<crate::backend::model_gateway::pending_route::Pending>,
95}
96
97pub(crate) struct Placed {
99 pub uid: String,
100 pub name: String,
101 pub slug: String,
102 pub warnings: Vec<String>,
103 pub needs_key: Option<String>,
105}
106
107pub(crate) fn verify(state: &AppState, path: &Path, expect: &Expect<'_>) -> Result<Verified, Why> {
109 let bad = |m: String| Why::NotWhatWasOffered(m);
110 let loaded = load(path).map_err(|e| bad(e.message))?;
111 if crate::backend::memory::record::sha256_hex(&loaded.manifest.signed_bytes()) != expect.manifest_hash {
112 return Err(bad("the archive is not the one that was offered: its manifest does not hash to the offered hash".to_string()));
113 }
114 match &loaded.signature {
115 SignatureState::Valid { public_key } if *public_key == expect.source_public => {}
116 _ => return Err(bad("the archive is not signed by the computer that sent it".to_string())),
117 }
118 if loaded.manifest.agent.uid != expect.uid || loaded.manifest.agent.name != expect.name {
119 return Err(bad("the archive is of another agent than the one offered".to_string()));
120 }
121 let plan = make_plan(state, &loaded, Mode::Move, None).map_err(|e| bad(e.message))?;
122 if plan.uid != expect.uid || plan.name != expect.name {
123 return Err(bad("the definition in the archive is of another agent than the one offered".to_string()));
124 }
125 if !plan.blockers.is_empty() {
126 return Err(Why::Blocked(plan.blockers.join("; ")));
127 }
128 let mut plan = plan;
132 let mut needs_key = None;
133 if let Some(r) = plan.definition.route.take() {
134 let named_by_offer = expect.route.is_some_and(|n| n.harness == plan.definition.harness && n.route == r.route && n.model == r.model);
135 match (named_by_offer, route_call(state, expect.route)) {
136 (true, RouteCall::Keep(now)) if now == expect.route_shown => plan.definition.route = Some(r),
137 (true, RouteCall::Keep(_)) => {
138 plan.warnings.push(format!("its route {}/{} was not set: what it would bill was not what the person was shown when they decided, so the agent uses its harness's own sign-in until a route is chosen here", r.route, r.model));
139 plan.route_line.clear();
140 }
141 (true, RouteCall::Drop(why)) => {
142 needs_key = missing_key(state, expect.route).zip(expect.route).map(|(key_label, n)| crate::backend::model_gateway::pending_route::Pending {
143 harness: n.harness.clone(),
144 route: n.route.clone(),
145 model: n.model.clone(),
146 key_label: key_label.to_string(),
147 });
148 plan.warnings.push(why);
149 plan.route_line.clear();
150 }
151 _ => {
152 plan.warnings.push(format!("its route {}/{} was not set: the offer the person decided on did not name it, so the agent uses its harness's own sign-in until a route is chosen here", r.route, r.model));
153 plan.route_line.clear();
154 }
155 }
156 }
157 Ok(Verified { loaded, plan, needs_key })
158}
159
160pub(crate) fn place(state: &AppState, path: &Path, verified: Verified) -> Result<Placed, String> {
162 let waiting = verified.needs_key;
163 let (_, plan, mut extra) = apply(state, path, &verified.loaded, verified.plan).map_err(|e| e.message)?;
164 let mut needs_key = None;
166 if let Some(pending) = waiting {
167 match crate::backend::model_gateway::pending_route::set_pending(&state.mstore, &plan.uid, &pending) {
168 Ok(()) => needs_key = Some(pending.key_label),
169 Err(e) => extra.warnings.push(format!("its route could not be kept to take once its key is added: {e}")),
170 }
171 }
172 let mut warnings = plan.warnings.clone();
174 warnings.extend(extra.memory_skipped.drain(..).map(|m| format!("memory not placed: {m}")));
175 warnings.append(&mut extra.warnings);
176 Ok(Placed { uid: plan.uid, name: plan.name, slug: extra.slug, warnings, needs_key })
177}
178
179pub(crate) fn undo_partial(state: &AppState, path: &Path) {
183 let (Ok(loaded), Some(gfs)) = (load(path), state.global_transcript_store.as_deref()) else { return };
184 for part in &loaded.manifest.parts {
185 if let Some(zone) = crate::backend::agent_archive::place::session_zone(&loaded.manifest.agent.uid, &part.path) {
186 crate::backend::agent_archive::place::remove_session(gfs, &zone);
187 }
188 }
189}