1use super::{
5 AgentTool, HumanTool, LogicalPath, MAX_DECLARED_ITEMS, ToolFqn, ToolSession, schemas,
6 validation::{path, paths, script, timeout},
7};
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10use std::collections::{BTreeMap, BTreeSet};
11
12#[derive(Debug, Clone, Default, Serialize, Deserialize)]
14#[serde(
15 rename_all(serialize = "camelCase", deserialize = "snake_case"),
16 deny_unknown_fields
17)]
18pub struct Views {
19 #[serde(default)]
20 pub agent_sessions: BTreeMap<ToolFqn, ToolSession>,
21 #[serde(default)]
22 pub agent_tools: BTreeMap<ToolFqn, AgentTool>,
23 #[serde(default)]
24 pub human_tools: BTreeMap<ToolFqn, HumanTool>,
25}
26
27impl Views {
28 pub(super) fn check(
31 &self,
32 schemas: &mut schemas::Schemas,
33 ) -> Result<(), (&'static str, ToolFqn, String)> {
34 for (name, session) in &self.agent_sessions {
35 session
36 .start
37 .validate()
38 .and_then(|()| session.stop.validate())
39 .map_err(|message| ("agent_sessions", name.clone(), message))?;
40 }
41 for (name, tool) in &self.agent_tools {
42 if let AgentTool::Command(command) = tool
43 && let Some(session) = &command.session
44 && !self.agent_sessions.contains_key(session)
45 {
46 return Err((
47 "agent_tools",
48 name.clone(),
49 format!("Agent tool {name}: unknown tool session {session}."),
50 ));
51 }
52 tool.validate(schemas).map_err(|message| {
53 (
54 "agent_tools",
55 name.clone(),
56 format!("Tool {name}: {message}"),
57 )
58 })?;
59 }
60 for (name, tool) in &self.human_tools {
61 tool.validate().map_err(|message| {
62 (
63 "human_tools",
64 name.clone(),
65 format!("Tool {name}: {message}"),
66 )
67 })?;
68 }
69 Ok(())
70 }
71
72 pub(super) fn share(&mut self, schemas: &mut schemas::Schemas) {
74 for tool in self.agent_tools.values_mut() {
75 if let AgentTool::Command(tool) = tool {
76 tool.input_schema = schemas.tool(&tool.input_schema);
77 }
78 }
79 }
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(try_from = "Map<String, Value>", into = "Value")]
87pub enum Fingerprint {
88 Graph,
90 Script {
91 command: String,
92 args: Vec<crate::scope::Argument>,
93 files: Vec<LogicalPath>,
95 timeout_ms: Option<std::time::Duration>,
96 },
97 Artifactsum {
98 files: Vec<LogicalPath>,
99 ignore: crate::cache::IgnorePatterns,
100 },
101}
102
103#[derive(Deserialize)]
104#[serde(deny_unknown_fields)]
105struct ScriptForm {
106 script: ScriptFields,
107}
108
109#[derive(Deserialize)]
110#[serde(rename_all = "snake_case", deny_unknown_fields)]
111struct ScriptFields {
112 command: String,
113 args: Vec<crate::scope::Argument>,
114 #[serde(default)]
115 files: Vec<LogicalPath>,
116 #[serde(
117 default,
118 deserialize_with = "timeout",
119 serialize_with = "validation::milliseconds::serialize"
120 )]
121 timeout_ms: Option<std::time::Duration>,
122}
123
124#[derive(Deserialize)]
125#[serde(deny_unknown_fields)]
126struct ArtifactsumForm {
127 #[serde(default = "owner_root")]
128 files: Vec<LogicalPath>,
129 #[serde(default)]
130 ignore: crate::cache::IgnorePatterns,
131}
132
133fn owner_root() -> Vec<LogicalPath> {
134 vec![".".parse().expect("owner root logical path")]
135}
136
137impl Default for Fingerprint {
138 fn default() -> Self {
139 Self::Artifactsum {
140 files: owner_root(),
141 ignore: Default::default(),
142 }
143 }
144}
145
146const REMOVED_DEPENDENCIES: &str = "fingerprint.dependencies was removed in 0.5: a fingerprint covers only its own Artifact, and an eval's reuse key adds the fingerprints of its children and referenced Artifacts. Remove the field.";
147
148impl From<ScriptForm> for Fingerprint {
149 fn from(form: ScriptForm) -> Self {
150 let ScriptFields {
151 command,
152 args,
153 files,
154 timeout_ms,
155 } = form.script;
156 Self::Script {
157 command,
158 args,
159 files,
160 timeout_ms,
161 }
162 }
163}
164
165impl From<ArtifactsumForm> for Fingerprint {
166 fn from(form: ArtifactsumForm) -> Self {
167 let ArtifactsumForm { files, ignore } = form;
168 Self::Artifactsum { files, ignore }
169 }
170}
171
172impl TryFrom<Map<String, Value>> for Fingerprint {
173 type Error = serde_json::Error;
174
175 fn try_from(object: Map<String, Value>) -> Result<Self, Self::Error> {
176 Ok(if object.contains_key("script") {
177 serde_json::from_value::<ScriptForm>(Value::Object(object))?.into()
178 } else {
179 if object.contains_key("dependencies") {
180 return Err(serde::de::Error::custom(REMOVED_DEPENDENCIES));
181 }
182 serde_json::from_value::<ArtifactsumForm>(Value::Object(object))?.into()
183 })
184 }
185}
186
187impl From<Fingerprint> for Value {
188 fn from(fingerprint: Fingerprint) -> Self {
189 match fingerprint {
190 Fingerprint::Graph => serde_json::json!({"graph": true}),
191 Fingerprint::Script {
192 command,
193 args,
194 files,
195 timeout_ms,
196 } => serde_json::json!({
197 "script": {
198 "command": crate::platform::program_text(&command),
199 "args": args,
200 "files": files,
201 "timeoutMs": timeout_ms.map(|value| value.as_millis()),
202 },
203 }),
204 Fingerprint::Artifactsum { files, ignore } => {
205 serde_json::json!({"files": files, "ignore": ignore})
206 }
207 }
208 }
209}
210
211impl Fingerprint {
212 pub fn kind(&self) -> crate::types::FingerprintKind {
213 match self {
214 Self::Graph => crate::types::FingerprintKind::Graph,
215 Self::Artifactsum { .. } => crate::types::FingerprintKind::Artifactsum,
216 Self::Script { .. } => crate::types::FingerprintKind::Script,
217 }
218 }
219
220 pub(super) fn check(&self) -> Result<(), String> {
222 match self {
223 Self::Graph => Ok(()),
224 Self::Script {
225 command,
226 args,
227 files,
228 ..
229 } => {
230 script(command, args)?;
231 paths(files, "fingerprint.script.files")
232 }
233 Self::Artifactsum { files, .. } => {
234 if files.is_empty()
235 || files.len() > MAX_DECLARED_ITEMS
236 || files.iter().collect::<BTreeSet<_>>().len() != files.len()
237 {
238 return Err(
239 "fingerprint.files must contain 1–64 unique owner-relative paths.".into(),
240 );
241 }
242 for file in files.iter().filter(|file| *file != ".") {
243 path(file).map_err(|message| format!("fingerprint.files: {message}"))?;
244 }
245 Ok(())
246 }
247 }
248 }
249}