1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! Job scheduler for parallel execution.
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{RwLock, mpsc, oneshot};
use tokio::task::JoinHandle;
use uuid::Uuid;
use crate::agent::task::{Task, TaskContext, TaskOutput};
use crate::agent::worker::{Worker, WorkerDeps};
use crate::config::AgentConfig;
use crate::context::{ContextManager, JobContext, JobState};
use crate::db::Database;
use crate::error::{Error, JobError};
use crate::hooks::HookRegistry;
use crate::llm::LlmProvider;
use crate::safety::SafetyLayer;
use crate::tools::ToolRegistry;
/// Message to send to a worker.
#[derive(Debug)]
pub enum WorkerMessage {
/// Start working on the job.
Start,
/// Stop the job.
Stop,
/// Check health.
Ping,
}
/// Status of a scheduled job.
#[derive(Debug)]
pub struct ScheduledJob {
pub handle: JoinHandle<()>,
pub tx: mpsc::Sender<WorkerMessage>,
}
/// Status of a scheduled sub-task.
struct ScheduledSubtask {
handle: JoinHandle<Result<TaskOutput, Error>>,
}
/// Schedules and manages parallel job execution.
pub struct Scheduler {
config: AgentConfig,
context_manager: Arc<ContextManager>,
llm: Arc<dyn LlmProvider>,
safety: Arc<SafetyLayer>,
tools: Arc<ToolRegistry>,
store: Option<Arc<dyn Database>>,
hooks: Arc<HookRegistry>,
/// Running jobs (main LLM-driven jobs).
jobs: Arc<RwLock<HashMap<Uuid, ScheduledJob>>>,
/// Running sub-tasks (tool executions, background tasks).
subtasks: Arc<RwLock<HashMap<Uuid, ScheduledSubtask>>>,
}
impl Scheduler {
/// Create a new scheduler.
pub fn new(
config: AgentConfig,
context_manager: Arc<ContextManager>,
llm: Arc<dyn LlmProvider>,
safety: Arc<SafetyLayer>,
tools: Arc<ToolRegistry>,
store: Option<Arc<dyn Database>>,
hooks: Arc<HookRegistry>,
) -> Self {
Self {
config,
context_manager,
llm,
safety,
tools,
store,
hooks,
jobs: Arc::new(RwLock::new(HashMap::new())),
subtasks: Arc::new(RwLock::new(HashMap::new())),
}
}
/// Schedule a job for execution.
pub async fn schedule(&self, job_id: Uuid) -> Result<(), JobError> {
// Hold write lock for the entire check-insert sequence to prevent
// TOCTOU races where two concurrent calls both pass the checks.
{
let mut jobs = self.jobs.write().await;
if jobs.contains_key(&job_id) {
return Ok(());
}
if jobs.len() >= self.config.max_parallel_jobs {
return Err(JobError::MaxJobsExceeded {
max: self.config.max_parallel_jobs,
});
}
// Transition job to in_progress
self.context_manager
.update_context(job_id, |ctx| {
ctx.transition_to(
JobState::InProgress,
Some("Scheduled for execution".to_string()),
)
})
.await?
.map_err(|s| JobError::ContextError {
id: job_id,
reason: s,
})?;
// Create worker channel
let (tx, rx) = mpsc::channel(16);
// Create worker with shared dependencies
let deps = WorkerDeps {
context_manager: self.context_manager.clone(),
llm: self.llm.clone(),
safety: self.safety.clone(),
tools: self.tools.clone(),
store: self.store.clone(),
hooks: self.hooks.clone(),
timeout: self.config.job_timeout,
use_planning: self.config.use_planning,
};
let worker = Worker::new(job_id, deps);
// Spawn worker task
let handle = tokio::spawn(async move {
if let Err(e) = worker.run(rx).await {
tracing::error!("Worker for job {} failed: {}", job_id, e);
}
});
// Start the worker
let _ = tx.send(WorkerMessage::Start).await;
// Insert while still holding the write lock
jobs.insert(job_id, ScheduledJob { handle, tx });
}
// Cleanup task for this job to avoid capacity leaks
let jobs = Arc::clone(&self.jobs);
tokio::spawn(async move {
loop {
let finished = {
let jobs_read = jobs.read().await;
match jobs_read.get(&job_id) {
Some(scheduled) => scheduled.handle.is_finished(),
None => true,
}
};
if finished {
jobs.write().await.remove(&job_id);
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
});
tracing::info!("Scheduled job {} for execution", job_id);
Ok(())
}
/// Schedule a sub-task from within a worker.
///
/// Sub-tasks are lightweight tasks that don't go through the full job lifecycle.
/// They're used for parallel tool execution and background computations.
///
/// Returns a oneshot receiver to get the result.
pub async fn spawn_subtask(
&self,
parent_id: Uuid,
task: Task,
) -> Result<oneshot::Receiver<Result<TaskOutput, Error>>, JobError> {
let task_id = Uuid::new_v4();
let (result_tx, result_rx) = oneshot::channel();
let handle = match task {
Task::Job { .. } => {
// Jobs should go through schedule(), not spawn_subtask
return Err(JobError::ContextError {
id: parent_id,
reason: "Use schedule() for Job tasks, not spawn_subtask()".to_string(),
});
}
Task::ToolExec {
parent_id: tool_parent_id,
tool_name,
params,
} => {
let tools = self.tools.clone();
let context_manager = self.context_manager.clone();
let safety = self.safety.clone();
tokio::spawn(async move {
let result = Self::execute_tool_task(
tools,
context_manager,
safety,
tool_parent_id,
&tool_name,
params,
)
.await;
// Send result (ignore if receiver dropped)
let _ = result_tx.send(result);
})
}
Task::Background { id: _, handler } => {
let ctx = TaskContext::new(task_id).with_parent(parent_id);
tokio::spawn(async move {
let result = handler.run(ctx).await;
let _ = result_tx.send(result);
})
}
};
// Track the subtask
self.subtasks.write().await.insert(
task_id,
ScheduledSubtask {
handle: tokio::spawn(async move {
// Wrap the handle to get its result
match handle.await {
Ok(()) => Err(Error::Job(JobError::ContextError {
id: task_id,
reason: "Subtask completed but result not captured".to_string(),
})),
Err(e) => Err(Error::Job(JobError::ContextError {
id: task_id,
reason: format!("Subtask panicked: {}", e),
})),
}
}),
},
);
// Cleanup task for subtask tracking
let subtasks = Arc::clone(&self.subtasks);
tokio::spawn(async move {
loop {
let finished = {
let subtasks_read = subtasks.read().await;
match subtasks_read.get(&task_id) {
Some(scheduled) => scheduled.handle.is_finished(),
None => true,
}
};
if finished {
subtasks.write().await.remove(&task_id);
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
});
tracing::debug!(
parent_id = %parent_id,
task_id = %task_id,
"Spawned subtask"
);
Ok(result_rx)
}
/// Schedule multiple tasks in parallel and wait for all to complete.
///
/// Returns results in the same order as the input tasks.
pub async fn spawn_batch(
&self,
parent_id: Uuid,
tasks: Vec<Task>,
) -> Vec<Result<TaskOutput, Error>> {
if tasks.is_empty() {
return Vec::new();
}
let mut receivers = Vec::with_capacity(tasks.len());
// Spawn all tasks
for task in tasks {
match self.spawn_subtask(parent_id, task).await {
Ok(rx) => receivers.push(Some(rx)),
Err(e) => {
// Store the error directly
receivers.push(None);
tracing::warn!(
parent_id = %parent_id,
error = %e,
"Failed to spawn subtask in batch"
);
}
}
}
// Collect results
let mut results = Vec::with_capacity(receivers.len());
for rx in receivers {
let result = match rx {
Some(receiver) => match receiver.await {
Ok(task_result) => task_result,
Err(_) => Err(Error::Job(JobError::ContextError {
id: parent_id,
reason: "Subtask channel closed unexpectedly".to_string(),
})),
},
None => Err(Error::Job(JobError::ContextError {
id: parent_id,
reason: "Subtask failed to spawn".to_string(),
})),
};
results.push(result);
}
results
}
/// Execute a single tool as a subtask.
async fn execute_tool_task(
tools: Arc<ToolRegistry>,
context_manager: Arc<ContextManager>,
safety: Arc<SafetyLayer>,
job_id: Uuid,
tool_name: &str,
params: serde_json::Value,
) -> Result<TaskOutput, Error> {
let start = std::time::Instant::now();
// Get the tool
let tool = tools.get(tool_name).await.ok_or_else(|| {
Error::Tool(crate::error::ToolError::NotFound {
name: tool_name.to_string(),
})
})?;
// Get job context
let job_ctx: JobContext = context_manager.get_context(job_id).await?;
if job_ctx.state == JobState::Cancelled {
return Err(crate::error::ToolError::ExecutionFailed {
name: tool_name.to_string(),
reason: "Job is cancelled".to_string(),
}
.into());
}
if tool.requires_approval() {
return Err(crate::error::ToolError::AuthRequired {
name: tool_name.to_string(),
}
.into());
}
// Validate tool parameters
let validation = safety.validator().validate_tool_params(¶ms);
if !validation.is_valid {
let details = validation
.errors
.iter()
.map(|e| format!("{}: {}", e.field, e.message))
.collect::<Vec<_>>()
.join("; ");
return Err(crate::error::ToolError::InvalidParameters {
name: tool_name.to_string(),
reason: format!("Invalid tool parameters: {}", details),
}
.into());
}
// Execute with per-tool timeout
let tool_timeout = tool.execution_timeout();
let result =
tokio::time::timeout(tool_timeout, async { tool.execute(params, &job_ctx).await })
.await
.map_err(|_| {
Error::Tool(crate::error::ToolError::Timeout {
name: tool_name.to_string(),
timeout: tool_timeout,
})
})?
.map_err(|e| {
Error::Tool(crate::error::ToolError::ExecutionFailed {
name: tool_name.to_string(),
reason: e.to_string(),
})
})?;
Ok(TaskOutput::new(result.result, start.elapsed()))
}
/// Stop a running job.
pub async fn stop(&self, job_id: Uuid) -> Result<(), JobError> {
let mut jobs = self.jobs.write().await;
if let Some(scheduled) = jobs.remove(&job_id) {
// Send stop signal
let _ = scheduled.tx.send(WorkerMessage::Stop).await;
// Give it a moment to clean up
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
// Abort if still running
if !scheduled.handle.is_finished() {
scheduled.handle.abort();
}
// Update job state
self.context_manager
.update_context(job_id, |ctx| {
let _ = ctx.transition_to(
JobState::Cancelled,
Some("Stopped by scheduler".to_string()),
);
})
.await?;
// Persist cancellation (fire-and-forget)
if let Some(ref store) = self.store {
let store = store.clone();
tokio::spawn(async move {
if let Err(e) = store
.update_job_status(
job_id,
JobState::Cancelled,
Some("Stopped by scheduler"),
)
.await
{
tracing::warn!("Failed to persist cancellation for job {}: {}", job_id, e);
}
});
}
tracing::info!("Stopped job {}", job_id);
}
Ok(())
}
/// Check if a job is running.
pub async fn is_running(&self, job_id: Uuid) -> bool {
self.jobs.read().await.contains_key(&job_id)
}
/// Get count of running jobs.
pub async fn running_count(&self) -> usize {
self.jobs.read().await.len()
}
/// Get count of running subtasks.
pub async fn subtask_count(&self) -> usize {
self.subtasks.read().await.len()
}
/// Get all running job IDs.
pub async fn running_jobs(&self) -> Vec<Uuid> {
self.jobs.read().await.keys().cloned().collect()
}
/// Clean up finished jobs and subtasks.
pub async fn cleanup_finished(&self) {
// Clean up jobs
{
let mut jobs = self.jobs.write().await;
let mut finished = Vec::new();
for (id, scheduled) in jobs.iter() {
if scheduled.handle.is_finished() {
finished.push(*id);
}
}
for id in finished {
jobs.remove(&id);
tracing::debug!("Cleaned up finished job {}", id);
}
}
// Clean up subtasks
{
let mut subtasks = self.subtasks.write().await;
let mut finished = Vec::new();
for (id, scheduled) in subtasks.iter() {
if scheduled.handle.is_finished() {
finished.push(*id);
}
}
for id in finished {
subtasks.remove(&id);
tracing::trace!("Cleaned up finished subtask {}", id);
}
}
}
/// Stop all jobs.
pub async fn stop_all(&self) {
let job_ids: Vec<Uuid> = self.jobs.read().await.keys().cloned().collect();
for job_id in job_ids {
let _ = self.stop(job_id).await;
}
// Abort all subtasks
let mut subtasks = self.subtasks.write().await;
for (_, scheduled) in subtasks.drain() {
scheduled.handle.abort();
}
}
/// Get access to the tools registry.
pub fn tools(&self) -> &Arc<ToolRegistry> {
&self.tools
}
/// Get access to the context manager.
pub fn context_manager(&self) -> &Arc<ContextManager> {
&self.context_manager
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_scheduler_creation() {
// Would need to mock dependencies for proper testing
}
#[tokio::test]
async fn test_spawn_batch_empty() {
// This test would need mock dependencies.
// For now just verify the empty case doesn't panic.
}
}