aidaemon_tools/
background_deliverable.rs1use std::collections::HashSet;
9use std::path::{Path, PathBuf};
10use std::time::{Duration, SystemTime};
11
12use crate::backend::SharedExecutionBackend;
13
14#[derive(Debug)]
15pub struct BackgroundDeliverableContext {
16 pub produced_candidates: Vec<PathBuf>,
18 pub unconfirmed_candidates: Vec<PathBuf>,
20}
21
22#[derive(Debug)]
23pub enum AutoSendDecision {
24 One(PathBuf),
25 None,
26 Ambiguous(Vec<PathBuf>),
27}
28
29fn is_non_deliverable_sink(path: &Path) -> bool {
30 path.starts_with("/dev") || path.starts_with("/proc") || path.starts_with("/sys")
31}
32
33pub async fn attribute_declared_deliverables_backend(
37 backend: SharedExecutionBackend,
38 declared_write_paths: &[String],
39 command_start: SystemTime,
40 command_end: SystemTime,
41) -> BackgroundDeliverableContext {
42 let window_end = command_end + Duration::from_secs(2);
43 let mut produced_candidates = Vec::new();
44 let mut unconfirmed_candidates = Vec::new();
45 let mut seen = HashSet::new();
46
47 for raw in declared_write_paths {
48 let raw = raw.trim();
49 if !crate::BackendPath::is_absolute(raw) {
50 continue;
51 }
52 let candidate = PathBuf::from(raw);
53 if is_non_deliverable_sink(&candidate) || !seen.insert(candidate.clone()) {
54 continue;
55 }
56 let Ok(path) = backend.resolve_path(raw).await else {
57 unconfirmed_candidates.push(candidate);
58 continue;
59 };
60 match backend.metadata(&path).await {
61 Ok(metadata) if metadata.is_file() => {
62 let resolved = PathBuf::from(path.as_str());
63 if metadata
64 .modified
65 .is_some_and(|modified| modified >= command_start && modified <= window_end)
66 {
67 produced_candidates.push(resolved);
68 } else {
69 unconfirmed_candidates.push(resolved);
70 }
71 }
72 Ok(_) => {
73 }
76 Err(_) => unconfirmed_candidates.push(PathBuf::from(path.as_str())),
77 }
78 }
79
80 produced_candidates.sort();
81 unconfirmed_candidates.sort();
82 BackgroundDeliverableContext {
83 produced_candidates,
84 unconfirmed_candidates,
85 }
86}
87
88pub fn auto_send_decision(ctx: &BackgroundDeliverableContext) -> AutoSendDecision {
89 match ctx.produced_candidates.as_slice() {
90 [] => AutoSendDecision::None,
91 [path] => AutoSendDecision::One(path.clone()),
92 paths => AutoSendDecision::Ambiguous(paths.to_vec()),
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 #[allow(unused_imports)]
100 use std::path::{Path, PathBuf};
101 #[allow(unused_imports)]
102 use std::time::{Duration, SystemTime};
103
104 #[tokio::test]
105 async fn only_exact_declared_fresh_files_are_candidates() {
106 let backend = crate::testing::active_execution_backend();
107 let root = tempfile::tempdir().expect("root");
108 let declared = root.path().join("declared.txt");
109 let undeclared = root.path().join("undeclared.txt");
110 let start = SystemTime::now() - Duration::from_secs(2);
111 std::fs::write(&declared, "declared").expect("declared fixture");
112 std::fs::write(&undeclared, "undeclared").expect("undeclared fixture");
113 let end = SystemTime::now();
114
115 let context = attribute_declared_deliverables_backend(
116 backend,
117 &[declared.to_string_lossy().to_string()],
118 start,
119 end,
120 )
121 .await;
122
123 assert_eq!(context.produced_candidates, [declared]);
124 assert!(context.unconfirmed_candidates.is_empty());
125 assert!(matches!(
126 auto_send_decision(&context),
127 AutoSendDecision::One(_)
128 ));
129 }
130
131 #[tokio::test]
132 async fn directories_relative_paths_and_sinks_are_not_artifacts() {
133 let backend = crate::testing::active_execution_backend();
134 let root = tempfile::tempdir().expect("root");
135 let now = SystemTime::now();
136 let context = attribute_declared_deliverables_backend(
137 backend,
138 &[
139 root.path().to_string_lossy().to_string(),
140 "relative.txt".to_string(),
141 "/dev/null".to_string(),
142 ],
143 now - Duration::from_secs(1),
144 now,
145 )
146 .await;
147
148 assert!(context.produced_candidates.is_empty());
149 assert!(context.unconfirmed_candidates.is_empty());
150 assert!(matches!(
151 auto_send_decision(&context),
152 AutoSendDecision::None
153 ));
154 }
155
156 #[tokio::test]
157 async fn exact_missing_target_is_reported_without_searching() {
158 let backend = crate::testing::active_execution_backend();
159 let root = tempfile::tempdir().expect("root");
160 let missing = root.path().join("missing.txt");
161 let now = SystemTime::now();
162 let context = attribute_declared_deliverables_backend(
163 backend,
164 &[missing.to_string_lossy().to_string()],
165 now - Duration::from_secs(1),
166 now,
167 )
168 .await;
169
170 assert!(context.produced_candidates.is_empty());
171 assert_eq!(context.unconfirmed_candidates, [missing]);
172 }
173
174 #[tokio::test]
175 async fn two_declared_fresh_files_are_ambiguous() {
176 let backend = crate::testing::active_execution_backend();
177 let root = tempfile::tempdir().expect("root");
178 let first = root.path().join("a.txt");
179 let second = root.path().join("b.txt");
180 let start = SystemTime::now() - Duration::from_secs(2);
181 std::fs::write(&first, "a").expect("first fixture");
182 std::fs::write(&second, "b").expect("second fixture");
183 let end = SystemTime::now();
184 let context = attribute_declared_deliverables_backend(
185 backend,
186 &[
187 second.to_string_lossy().to_string(),
188 first.to_string_lossy().to_string(),
189 ],
190 start,
191 end,
192 )
193 .await;
194
195 assert!(matches!(
196 auto_send_decision(&context),
197 AutoSendDecision::Ambiguous(paths) if paths == vec![first, second]
198 ));
199 }
200}