1use std::path::Path;
9
10use super::task::{
11 TASK_ACTION_CANCEL, TASK_ACTION_GET, Task, TaskAuthorityConfigV1, TaskAuthorityError,
12 TaskAuthorityExpectationV1, TaskControlDescriptorV1, TaskState, TaskStatusV1, TaskStore,
13};
14
15#[derive(Debug)]
16pub enum TaskControlError {
17 Authority(TaskAuthorityError),
18 Storage(std::io::Error),
19 NotFound,
21 InvalidTransition,
23}
24
25impl From<std::io::Error> for TaskControlError {
26 fn from(error: std::io::Error) -> Self {
27 Self::Storage(error)
28 }
29}
30
31pub fn get_status(
35 path: &Path,
36 request: &TaskControlDescriptorV1,
37 policy: &TaskAuthorityConfigV1,
38 expected: &TaskAuthorityExpectationV1<'_>,
39) -> Result<TaskStatusV1, TaskControlError> {
40 if request.action != TASK_ACTION_GET {
41 return Err(TaskControlError::Authority(
42 TaskAuthorityError::UnsupportedAction,
43 ));
44 }
45 request
46 .verify_authority(policy, expected)
47 .map_err(TaskControlError::Authority)?;
48 TaskStore::read_locked(path, |store| {
49 store
50 .get_task(&request.task_id)
51 .filter(|task| matches_target(task, request))
52 .map(Task::status_v1)
53 .ok_or(TaskControlError::NotFound)
54 })
55 .map_err(TaskControlError::Storage)?
56}
57
58pub fn cancel(
63 path: &Path,
64 request: &TaskControlDescriptorV1,
65 policy: &TaskAuthorityConfigV1,
66 expected: &TaskAuthorityExpectationV1<'_>,
67) -> Result<TaskStatusV1, TaskControlError> {
68 if request.action != TASK_ACTION_CANCEL {
69 return Err(TaskControlError::Authority(
70 TaskAuthorityError::UnsupportedAction,
71 ));
72 }
73 request
74 .verify_authority(policy, expected)
75 .map_err(TaskControlError::Authority)?;
76 TaskStore::mutate_locked(path, |store| {
77 let task = store
78 .get_task_mut(&request.task_id)
79 .filter(|task| matches_target(task, request))
80 .ok_or(TaskControlError::NotFound)?;
81 if task.state != TaskState::Canceled {
82 task.transition(TaskState::Canceled, request.reason.as_deref())
83 .map_err(|_| TaskControlError::InvalidTransition)?;
84 }
85 Ok(task.status_v1())
86 })
87}
88
89fn matches_target(task: &Task, request: &TaskControlDescriptorV1) -> bool {
90 task.authority_key_id.is_some()
93 && task.id == request.task_id
94 && task.from_agent == request.sender
95 && task.to_agent == request.recipient
96 && task.tenant_id.as_deref() == Some(request.tenant_id.as_str())
97 && task.project_id.as_deref() == Some(request.project_id.as_str())
98 && task.descriptor_digest.as_deref() == Some(request.task_descriptor_digest.as_str())
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104 use chrono::{Duration, Utc};
105
106 fn fixture() -> (Task, TaskControlDescriptorV1) {
107 let mut task = Task::new("owner", "receiver", "private payload");
108 task.tenant_id = Some("tenant".into());
109 task.project_id = Some("project".into());
110 task.authority_key_id = Some("original-key".into());
111 task.descriptor_digest = Some(format!("sha256:{}", "a".repeat(64)));
112 let now = Utc::now();
113 let request = TaskControlDescriptorV1::new(
114 "owner",
115 "receiver",
116 "tenant",
117 "project",
118 TASK_ACTION_GET,
119 &task.id,
120 task.descriptor_digest.as_deref().expect("fixture digest"),
121 now,
122 now + Duration::minutes(1),
123 "nonce",
124 None,
125 "grant",
126 "rotated-key",
127 );
128 (task, request)
129 }
130
131 #[test]
132 fn target_binding_requires_all_persisted_authority_dimensions() {
133 let (task, request) = fixture();
134 assert!(matches_target(&task, &request));
135 for field in 0..7 {
136 let mut changed = task.clone();
137 match field {
138 0 => changed.id.push('x'),
139 1 => changed.from_agent.push('x'),
140 2 => changed.to_agent.push('x'),
141 3 => changed.tenant_id = None,
142 4 => changed.project_id = None,
143 5 => changed.descriptor_digest = None,
144 6 => changed.authority_key_id = None,
145 _ => unreachable!(),
146 }
147 assert!(!matches_target(&changed, &request), "dimension {field}");
148 }
149 }
150
151 #[test]
152 fn unauthenticated_status_request_never_touches_storage() {
153 let (_, request) = fixture();
154 let dir = tempfile::tempdir().expect("temporary directory");
155 let path = dir.path().join("absent-parent").join("tasks.json");
156 let policy = TaskAuthorityConfigV1::default();
157 let expected = TaskAuthorityExpectationV1 {
158 sender: "owner",
159 recipient: "receiver",
160 tenant_id: "tenant",
161 project_id: "project",
162 now: Utc::now(),
163 };
164 assert!(matches!(
165 get_status(&path, &request, &policy, &expected),
166 Err(TaskControlError::Authority(_))
167 ));
168 assert!(!path.parent().expect("parent").exists());
169 }
170
171 fn authorized_cancel() -> (Task, TaskControlDescriptorV1, TaskAuthorityConfigV1) {
172 use super::super::task::{TaskCapabilityGrantV1, TaskPeerTrustV1, TaskScopeV1};
173 let (task, mut request) = fixture();
174 let key = ed25519_dalek::SigningKey::from_bytes(&[7; 32]);
175 request.action = TASK_ACTION_CANCEL.into();
176 request.reason = Some("owner requested cancellation".into());
177 request.sign(&key);
178 let policy = TaskAuthorityConfigV1 {
179 schema_version: 1,
180 peers: vec![TaskPeerTrustV1 {
181 schema_version: 1,
182 key_id: request.key_id.clone(),
183 agent_id: request.sender.clone(),
184 public_key: crate::core::agent_identity::hex_encode(key.verifying_key().as_bytes()),
185 allowed_actions: vec![TASK_ACTION_CANCEL.into()],
186 allowed_scopes: vec![TaskScopeV1 {
187 schema_version: 1,
188 tenant_id: request.tenant_id.clone(),
189 project_id: request.project_id.clone(),
190 }],
191 not_before: request.issued_at - Duration::minutes(1),
192 expires_at: request.expires_at + Duration::minutes(1),
193 revoked: false,
194 }],
195 grants: vec![TaskCapabilityGrantV1 {
196 schema_version: 1,
197 grant_id: request.grant_ref.grant_id.clone(),
198 key_id: request.key_id.clone(),
199 action: TASK_ACTION_CANCEL.into(),
200 tenant_id: request.tenant_id.clone(),
201 project_id: request.project_id.clone(),
202 not_before: request.issued_at - Duration::minutes(1),
203 expires_at: request.expires_at + Duration::minutes(1),
204 revoked: false,
205 }],
206 };
207 (task, request, policy)
208 }
209
210 fn expected(request: &TaskControlDescriptorV1) -> TaskAuthorityExpectationV1<'_> {
211 TaskAuthorityExpectationV1 {
212 sender: "owner",
213 recipient: "receiver",
214 tenant_id: "tenant",
215 project_id: "project",
216 now: request.issued_at,
217 }
218 }
219
220 fn persist(path: &Path, task: Task) -> Vec<u8> {
221 TaskStore::mutate_locked(path, |store| {
222 store.tasks = vec![task];
223 Ok::<(), std::io::Error>(())
224 })
225 .expect("persist fixture");
226 std::fs::read(path).expect("fixture bytes")
227 }
228
229 #[test]
230 fn signed_cancel_is_durable_concurrent_and_hidden_from_legacy() {
231 let (task, request, policy) = authorized_cancel();
232 let dir = tempfile::tempdir().unwrap();
233 let path = dir.path().join("tasks.json");
234 persist(&path, task);
235 let operation = || cancel(&path, &request, &policy, &expected(&request)).unwrap();
236 let (left, right) = std::thread::scope(|scope| {
237 let first = scope.spawn(operation);
238 let second = scope.spawn(operation);
239 (first.join().unwrap(), second.join().unwrap())
240 });
241 assert_eq!(left, right);
242 assert_eq!(left.state, TaskState::Canceled);
243 TaskStore::read_locked(&path, |store| {
244 let task = store.get_task(&request.task_id).unwrap();
245 assert_eq!(task.status_v1(), left);
246 assert_eq!(task.history.len(), 2);
247 assert_eq!(task.history[1].reason, request.reason);
248 })
249 .unwrap();
250 let before = std::fs::read(&path).unwrap();
251 for method in [TASK_ACTION_GET, TASK_ACTION_CANCEL] {
252 let legacy = super::super::a2a_compat::JsonRpcRequest {
253 jsonrpc: "2.0".into(),
254 id: 1.into(),
255 method: method.into(),
256 params: serde_json::json!({"id": request.task_id}),
257 };
258 let response = super::super::a2a_compat::handle_a2a_jsonrpc_at_path(&legacy, &path);
259 assert_eq!(response.error.unwrap().message, "task not found");
260 assert_eq!(std::fs::read(&path).unwrap(), before);
261 }
262 }
263
264 #[test]
265 fn signed_cancel_denies_every_target_mismatch_without_writing() {
266 let (task, request, policy) = authorized_cancel();
267 let dir = tempfile::tempdir().unwrap();
268 let path = dir.path().join("tasks.json");
269 for field in 0..7 {
270 let mut changed = task.clone();
271 match field {
272 0 => changed.id.push('x'),
273 1 => changed.from_agent.push('x'),
274 2 => changed.to_agent.push('x'),
275 3 => changed.tenant_id = None,
276 4 => changed.project_id = None,
277 5 => changed.descriptor_digest = None,
278 6 => changed.authority_key_id = None,
279 _ => unreachable!(),
280 }
281 let before = persist(&path, changed);
282 assert!(
283 matches!(
284 cancel(&path, &request, &policy, &expected(&request)),
285 Err(TaskControlError::NotFound)
286 ),
287 "dimension {field}"
288 );
289 assert_eq!(std::fs::read(&path).unwrap(), before);
290 }
291 }
292
293 #[test]
294 fn signed_cancel_rejects_invalid_authority_before_storage() {
295 let (_, request, policy) = authorized_cancel();
296 let dir = tempfile::tempdir().unwrap();
297 let path = dir.path().join("absent-parent/tasks.json");
298 for field in 0..5 {
299 let mut request = request.clone();
300 let mut policy = policy.clone();
301 match field {
302 0 => request.action = TASK_ACTION_GET.into(),
303 1 => request.signature = "00".repeat(64),
304 2 => policy.peers[0].revoked = true,
305 3 => policy.grants[0].revoked = true,
306 4 => policy.grants.clear(),
307 _ => unreachable!(),
308 }
309 assert!(matches!(
310 cancel(&path, &request, &policy, &expected(&request)),
311 Err(TaskControlError::Authority(_))
312 ));
313 assert!(!path.parent().unwrap().exists());
314 }
315 }
316
317 #[test]
318 fn signed_cancel_preserves_other_terminal_states_and_corrupt_storage() {
319 let (task, request, policy) = authorized_cancel();
320 let dir = tempfile::tempdir().unwrap();
321 let path = dir.path().join("tasks.json");
322 for state in [TaskState::Completed, TaskState::Failed] {
323 let mut task = task.clone();
324 task.transition(TaskState::Working, None).unwrap();
325 task.transition(state, None).unwrap();
326 let before = persist(&path, task);
327 assert!(matches!(
328 cancel(&path, &request, &policy, &expected(&request)),
329 Err(TaskControlError::InvalidTransition)
330 ));
331 assert_eq!(std::fs::read(&path).unwrap(), before);
332 }
333 std::fs::write(&path, "invalid fixture").unwrap();
334 assert!(matches!(
335 cancel(&path, &request, &policy, &expected(&request)),
336 Err(TaskControlError::Storage(_))
337 ));
338 assert_eq!(std::fs::read(&path).unwrap(), b"invalid fixture");
339 }
340
341 #[test]
342 fn signed_cancel_never_persists_an_unreadable_history_overflow() {
343 let (mut task, request, policy) = authorized_cancel();
344 for index in 0..2046 {
345 task.transition(
346 if index % 2 == 0 {
347 TaskState::Working
348 } else {
349 TaskState::InputRequired
350 },
351 None,
352 )
353 .unwrap();
354 }
355 task.history.push(super::super::task::TaskTransition {
358 from: task.state.clone(),
359 to: TaskState::Working,
360 timestamp: task.updated_at,
361 reason: None,
362 });
363 task.state = TaskState::Working;
364 assert_eq!(task.history.len(), 2048);
365 let dir = tempfile::tempdir().unwrap();
366 let path = dir.path().join("tasks.json");
367 let before = persist(&path, task);
368 assert!(matches!(
369 cancel(&path, &request, &policy, &expected(&request)),
370 Err(TaskControlError::InvalidTransition)
371 ));
372 assert_eq!(std::fs::read(&path).unwrap(), before);
373 assert!(TaskStore::read_locked(&path, |_| ()).is_ok());
374 }
375
376 #[test]
377 fn signed_cancel_uses_reserved_capacity_and_retries_without_growth() {
378 let (mut task, request, policy) = authorized_cancel();
379 while task.history.len() < 2047 {
380 let next = if task.state == TaskState::Working {
381 TaskState::InputRequired
382 } else {
383 TaskState::Working
384 };
385 task.transition(next, None).unwrap();
386 }
387 let dir = tempfile::tempdir().unwrap();
388 let path = dir.path().join("tasks.json");
389 persist(&path, task);
390 let first = cancel(&path, &request, &policy, &expected(&request)).unwrap();
391 assert_eq!(first.state, TaskState::Canceled);
392 let stored = TaskStore::read_locked(&path, |store| store.tasks[0].clone()).unwrap();
393 assert_eq!(stored.history.len(), 2048);
394 assert_eq!(stored.history.last().unwrap().timestamp, stored.updated_at);
395 assert_eq!(
396 cancel(&path, &request, &policy, &expected(&request)).unwrap(),
397 first
398 );
399 let retry = TaskStore::read_locked(&path, |store| store.tasks[0].clone()).unwrap();
400 assert_eq!(
401 serde_json::to_vec(&retry).unwrap(),
402 serde_json::to_vec(&stored).unwrap()
403 );
404 }
405
406 #[test]
407 fn signed_cancel_rejects_backward_clock_without_touching_storage() {
408 let (mut task, request, policy) = authorized_cancel();
409 task.updated_at = Utc::now() + Duration::hours(1);
410 let dir = tempfile::tempdir().unwrap();
411 let path = dir.path().join("tasks.json");
412 let before = persist(&path, task);
413 assert!(matches!(
414 cancel(&path, &request, &policy, &expected(&request)),
415 Err(TaskControlError::InvalidTransition)
416 ));
417 assert_eq!(std::fs::read(&path).unwrap(), before);
418 assert!(TaskStore::read_locked(&path, |_| ()).is_ok());
419 }
420}