Skip to main content

glass_dev/
agents.rs

1//! Glass-owned scheduling for independent resident Pi agent sessions.
2
3use crate::development::{DevelopmentError, DevelopmentResult};
4use crate::pi_runtime::{GlassPiRuntime, PiRuntimeOptions, PiSessionRequest, PiToolExecutor};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::collections::{BTreeMap, BTreeSet, VecDeque};
8use std::path::{Path, PathBuf};
9use std::sync::Arc;
10use std::sync::atomic::{AtomicU64, Ordering};
11use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError, TrySendError};
12use std::thread;
13use std::time::{Duration, SystemTime, UNIX_EPOCH};
14
15const COMMAND_CAPACITY: usize = 32;
16const EVENT_CAPACITY: usize = 256;
17const HISTORY_CAPACITY: usize = 512;
18const AGENT_EVIDENCE_CAPACITY: usize = 64;
19const MAX_AGENTS: usize = 32;
20const MAX_PROMPT_BYTES: usize = 64 * 1024;
21
22/// Connection metadata for an optional resident-agent broker.
23#[derive(Debug, Clone)]
24pub struct ResidentAgentBroker {
25    /// Unix socket used to reach the broker.
26    pub socket: PathBuf,
27    /// Authentication token sent to the broker.
28    pub token: String,
29    /// Workspace identity asserted by the broker.
30    pub workspace_id: String,
31}
32
33/// Stable identifier for one resident agent; values start with `agent-`.
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
35pub struct AgentId(String);
36
37impl AgentId {
38    /// Borrow the stable identifier as a string slice.
39    pub fn as_str(&self) -> &str {
40        &self.0
41    }
42
43    /// Parse an identifier, requiring the `agent-` prefix and a 64-byte bound.
44    pub fn parse(value: impl Into<String>) -> DevelopmentResult<Self> {
45        let value = value.into();
46        if !value.starts_with("agent-")
47            || value.len() > 64
48            || value.chars().any(|character| character.is_control())
49        {
50            return Err(DevelopmentError::InvalidInput("invalid agent id".into()));
51        }
52        Ok(Self(value))
53    }
54}
55
56/// Lifecycle state of a resident agent.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
58#[serde(rename_all = "camelCase")]
59pub enum AgentStatus {
60    /// Accepted but waiting for dependencies or capacity.
61    Queued,
62    /// Worker process is being started.
63    Starting,
64    /// Worker is ready for a request.
65    Idle,
66    /// Worker is processing a request.
67    Working,
68    /// Worker is waiting on external/user input.
69    Waiting,
70    /// Agent completed successfully.
71    Completed,
72    /// Agent terminated with an error.
73    Failed,
74    /// Agent was explicitly cancelled.
75    Cancelled,
76}
77
78impl AgentStatus {
79    fn terminal(self) -> bool {
80        matches!(self, Self::Completed | Self::Failed | Self::Cancelled)
81    }
82
83    /// Human-readable label for terminal and TUI surfaces.
84    pub fn label(self) -> &'static str {
85        match self {
86            Self::Queued => "queued",
87            Self::Starting => "starting",
88            Self::Idle => "idle",
89            Self::Working => "working",
90            Self::Waiting => "waiting",
91            Self::Completed => "completed",
92            Self::Failed => "failed",
93            Self::Cancelled => "cancelled",
94        }
95    }
96}
97
98/// Declarative request for one resident agent session.
99#[derive(Debug, Clone, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101pub struct AgentSpec {
102    /// Human-readable role assigned to the agent.
103    pub role: String,
104    /// Initial task prompt.
105    pub task: String,
106    /// Agents that must complete before this agent starts.
107    #[serde(default)]
108    pub dependencies: Vec<AgentId>,
109    /// Optional Pi model override.
110    pub model: Option<String>,
111    /// Optional Pi thinking-level override.
112    pub thinking: Option<String>,
113    /// Optional existing worktree path.
114    pub worktree: Option<PathBuf>,
115    /// Whether governed tool restrictions are disabled.
116    #[serde(default)]
117    pub unrestricted: bool,
118    /// Optional worker runtime limit in seconds.
119    pub max_runtime_seconds: Option<u64>,
120    /// Optional retained-event limit for this agent.
121    pub max_events: Option<u64>,
122}
123
124impl AgentSpec {
125    /// Build a spec with governed defaults of 3,600 seconds and 10,000 events.
126    pub fn new(role: impl Into<String>, task: impl Into<String>) -> Self {
127        Self {
128            role: role.into(),
129            task: task.into(),
130            dependencies: Vec::new(),
131            model: None,
132            thinking: None,
133            worktree: None,
134            unrestricted: false,
135            max_runtime_seconds: Some(3_600),
136            max_events: Some(10_000),
137        }
138    }
139}
140
141/// Current serialized projection of an agent and its bounded evidence.
142#[derive(Debug, Clone, Serialize, Deserialize)]
143#[serde(rename_all = "camelCase")]
144pub struct AgentSnapshot {
145    /// Stable agent identifier.
146    pub id: AgentId,
147    /// Configured role.
148    pub role: String,
149    /// Configured task prompt.
150    pub task: String,
151    /// Current lifecycle status.
152    pub status: AgentStatus,
153    /// Dependency identifiers.
154    pub dependencies: Vec<AgentId>,
155    /// Effective model, if configured.
156    pub model: Option<String>,
157    /// Effective thinking level, if configured.
158    pub thinking: Option<String>,
159    /// Canonical worktree used by the worker.
160    pub worktree: PathBuf,
161    /// Whether unrestricted tools were enabled.
162    pub unrestricted: bool,
163    /// Creation timestamp in Unix milliseconds.
164    pub created_at_ms: u128,
165    /// Start timestamp in Unix milliseconds.
166    pub started_at_ms: Option<u128>,
167    /// Last projection update timestamp in Unix milliseconds.
168    pub updated_at_ms: u128,
169    /// Number of events received for this agent.
170    pub event_count: u64,
171    /// Number of lossy events dropped by the bounded queue.
172    pub dropped_event_count: u64,
173    /// Last worker error, if any.
174    pub last_error: Option<String>,
175    /// Last Pi response identifier, if any.
176    pub last_response_id: Option<String>,
177    /// Bounded tool/runtime evidence values.
178    pub evidence: Vec<Value>,
179}
180
181/// One ordered event emitted by an agent worker.
182#[derive(Debug, Clone, Serialize, Deserialize)]
183#[serde(rename_all = "camelCase")]
184pub struct AgentEvent {
185    /// Monotonic event sequence number.
186    pub sequence: u64,
187    /// Agent that emitted the event.
188    pub agent_id: AgentId,
189    /// Event timestamp in Unix milliseconds.
190    pub timestamp_ms: u128,
191    /// Event kind label.
192    pub kind: String,
193    /// Structured event payload.
194    pub payload: Value,
195}
196
197#[derive(Debug)]
198enum WorkerCommand {
199    Request(PiSessionRequest),
200    ApproveTool { frame_id: String, approved: bool },
201    Shutdown,
202}
203
204#[derive(Debug)]
205enum WorkerEvent {
206    Ready(AgentId),
207    RequestStarted(AgentId, String, bool),
208    Pi(AgentId, Value),
209    Failed(AgentId, String),
210    Stopped(AgentId),
211}
212
213struct AgentRecord {
214    snapshot: AgentSnapshot,
215    spec: AgentSpec,
216    command: Option<SyncSender<WorkerCommand>>,
217    worker: Option<thread::JoinHandle<()>>,
218    awaiting_agent_settle: bool,
219    dropped_events: Arc<AtomicU64>,
220}
221
222struct WorkerRuntime {
223    worktree: PathBuf,
224    local_tool_executor: Option<PiToolExecutor>,
225    sessions_dir: PathBuf,
226    broker: Option<ResidentAgentBroker>,
227    additional_system_prompt: Option<String>,
228}
229
230/// Registry and scheduler for independent Pi subprocesses.
231///
232/// Each running agent owns a distinct Pi process and persistent session. The
233/// registry only starts dependency-ready work, bounds all queues and retained
234/// evidence, and never silently promotes a failed dependency to success.
235pub struct AgentRegistry {
236    root: PathBuf,
237    sessions_dir: PathBuf,
238    records: BTreeMap<AgentId, AgentRecord>,
239    events_tx: SyncSender<WorkerEvent>,
240    events_rx: Receiver<WorkerEvent>,
241    history: VecDeque<AgentEvent>,
242    local_tool_executor: Option<PiToolExecutor>,
243    next_agent: u64,
244    next_event: u64,
245    broker: Option<ResidentAgentBroker>,
246    additional_system_prompt: Option<String>,
247    default_model: Option<String>,
248    default_thinking: Option<String>,
249    default_unrestricted: bool,
250}
251
252impl AgentRegistry {
253    /// Create a registry rooted at the canonical project directory.
254    pub fn new(root: impl AsRef<Path>) -> DevelopmentResult<Self> {
255        let root = std::fs::canonicalize(root)?;
256        let sessions_dir = root.join(".glass").join("pi-sessions");
257        let (events_tx, events_rx) = mpsc::sync_channel(EVENT_CAPACITY);
258        Ok(Self {
259            root,
260            sessions_dir,
261            records: BTreeMap::new(),
262            events_tx,
263            events_rx,
264            history: VecDeque::new(),
265            local_tool_executor: None,
266            next_agent: 1,
267            next_event: 1,
268            broker: None,
269            additional_system_prompt: None,
270            default_model: None,
271            default_thinking: None,
272            default_unrestricted: false,
273        })
274    }
275
276    /// Configure the authenticated broker used for resident-agent requests.
277    pub fn set_resident_broker(&mut self, broker: ResidentAgentBroker) -> DevelopmentResult<()> {
278        if !broker.socket.is_absolute()
279            || broker.socket == Path::new("/")
280            || broker.token.is_empty()
281            || broker.token.len() > 256
282            || broker.workspace_id.is_empty()
283            || broker.workspace_id.len() > 128
284            || broker
285                .workspace_id
286                .chars()
287                .any(|character| character.is_control())
288        {
289            return Err(DevelopmentError::InvalidInput(
290                "invalid resident agent broker context".into(),
291            ));
292        }
293        self.broker = Some(broker);
294        Ok(())
295    }
296    pub(crate) fn set_local_tool_executor(&mut self, executor: PiToolExecutor) {
297        self.local_tool_executor = Some(executor);
298    }
299
300    pub fn set_additional_system_prompt(
301        &mut self,
302        prompt: Option<String>,
303    ) -> DevelopmentResult<()> {
304        if prompt
305            .as_ref()
306            .is_some_and(|prompt| prompt.len() > 128 * 1024 || prompt.contains('\0'))
307        {
308            return Err(DevelopmentError::InvalidInput(
309                "agent project instructions exceed 128 KiB or contain NUL".into(),
310            ));
311        }
312        self.additional_system_prompt = prompt;
313        Ok(())
314    }
315
316    /// Active user-global and trusted-project instructions supplied to Pi.
317    pub fn additional_system_prompt(&self) -> Option<&str> {
318        self.additional_system_prompt.as_deref()
319    }
320
321    pub fn set_defaults(
322        &mut self,
323        model: Option<String>,
324        thinking: Option<String>,
325    ) -> DevelopmentResult<()> {
326        for (label, value) in [("model", &model), ("thinking", &thinking)] {
327            if value
328                .as_ref()
329                .is_some_and(|value| value.is_empty() || value.len() > 256 || value.contains('\0'))
330            {
331                return Err(DevelopmentError::InvalidInput(format!(
332                    "default agent {label} must contain 1..=256 bytes without NUL"
333                )));
334            }
335        }
336        self.default_model = model;
337        self.default_thinking = thinking;
338        Ok(())
339    }
340    /// Make newly created resident agents unrestricted for this process.
341    pub fn set_default_unrestricted(&mut self, unrestricted: bool) {
342        self.default_unrestricted = unrestricted;
343    }
344
345    /// Validate and enqueue an agent; dependencies must be known and successful.
346    pub fn create(&mut self, mut spec: AgentSpec) -> DevelopmentResult<AgentId> {
347        self.refresh()?;
348        if self.default_unrestricted {
349            spec.unrestricted = true;
350        }
351        if spec.model.is_none() {
352            spec.model.clone_from(&self.default_model);
353        }
354        if spec.thinking.is_none() {
355            spec.thinking.clone_from(&self.default_thinking);
356        }
357        validate_spec(&self.root, &self.records, &spec)?;
358        if self.records.len() >= MAX_AGENTS {
359            return Err(DevelopmentError::Conflict(format!(
360                "agent registry reached its {MAX_AGENTS} session limit"
361            )));
362        }
363        let id = AgentId(format!("agent-{:04}", self.next_agent));
364        self.next_agent = self
365            .next_agent
366            .checked_add(1)
367            .ok_or_else(|| DevelopmentError::Conflict("agent identifier space exhausted".into()))?;
368        let now = now_ms();
369        let worktree = spec.worktree.clone().unwrap_or_else(|| self.root.clone());
370        let status = if spec.dependencies.is_empty() {
371            AgentStatus::Starting
372        } else {
373            AgentStatus::Queued
374        };
375        self.records.insert(
376            id.clone(),
377            AgentRecord {
378                snapshot: AgentSnapshot {
379                    id: id.clone(),
380                    role: spec.role.clone(),
381                    task: spec.task.clone(),
382                    status,
383                    dependencies: spec.dependencies.clone(),
384                    model: spec.model.clone(),
385                    thinking: spec.thinking.clone(),
386                    worktree,
387                    unrestricted: spec.unrestricted,
388                    created_at_ms: now,
389                    started_at_ms: None,
390                    updated_at_ms: now,
391                    event_count: 0,
392                    dropped_event_count: 0,
393                    last_error: None,
394                    last_response_id: None,
395                    evidence: Vec::new(),
396                },
397                spec,
398                command: None,
399                worker: None,
400                awaiting_agent_settle: false,
401                dropped_events: Arc::new(AtomicU64::new(0)),
402            },
403        );
404        if status == AgentStatus::Starting {
405            self.spawn(&id)?;
406        }
407        Ok(id)
408    }
409
410    /// Drain worker events, update snapshots, and start newly ready agents.
411    pub fn refresh(&mut self) -> DevelopmentResult<()> {
412        loop {
413            match self.events_rx.try_recv() {
414                Ok(event) => self.apply_worker_event(event),
415                Err(TryRecvError::Empty) => break,
416                Err(TryRecvError::Disconnected) => {
417                    return Err(DevelopmentError::Process(
418                        "agent scheduler event channel closed".into(),
419                    ));
420                }
421            }
422        }
423        for record in self.records.values_mut() {
424            record.snapshot.dropped_event_count = record.dropped_events.load(Ordering::Relaxed);
425        }
426        self.reap_finished_workers();
427        self.enforce_budgets();
428        self.fail_blocked_dependents();
429        self.start_ready_agents()?;
430        Ok(())
431    }
432
433    /// Return one refreshed agent snapshot or a not-found error.
434    pub fn snapshot(&mut self, id: &AgentId) -> DevelopmentResult<AgentSnapshot> {
435        self.refresh()?;
436        self.records
437            .get(id)
438            .map(|record| record.snapshot.clone())
439            .ok_or_else(|| DevelopmentError::NotFound(format!("agent {}", id.as_str())))
440    }
441
442    /// Return refreshed snapshots for all registered agents.
443    pub fn list(&mut self) -> DevelopmentResult<Vec<AgentSnapshot>> {
444        self.refresh()?;
445        Ok(self
446            .records
447            .values()
448            .map(|record| record.snapshot.clone())
449            .collect())
450    }
451
452    /// Return retained events at or after the supplied sequence number.
453    pub fn history(&mut self, since: u64) -> DevelopmentResult<Vec<AgentEvent>> {
454        self.refresh()?;
455        Ok(self
456            .history
457            .iter()
458            .filter(|event| event.sequence > since)
459            .cloned()
460            .collect())
461    }
462
463    /// Send an initial prompt to an agent, subject to prompt-size bounds.
464    pub fn prompt(&mut self, id: &AgentId, text: impl Into<String>) -> DevelopmentResult<()> {
465        self.prompt_with_context(id, text, None)
466    }
467
468    pub fn prompt_with_context(
469        &mut self,
470        id: &AgentId,
471        text: impl Into<String>,
472        context: Option<Value>,
473    ) -> DevelopmentResult<()> {
474        let text = text.into();
475        validate_text("agent prompt", &text, MAX_PROMPT_BYTES)?;
476        validate_context_attachment(context.as_ref())?;
477        self.send(id, PiSessionRequest::Prompt { text, context })
478    }
479
480    pub fn steer(&mut self, id: &AgentId, text: impl Into<String>) -> DevelopmentResult<()> {
481        self.steer_with_context(id, text, None)
482    }
483
484    pub fn steer_with_context(
485        &mut self,
486        id: &AgentId,
487        text: impl Into<String>,
488        context: Option<Value>,
489    ) -> DevelopmentResult<()> {
490        let text = text.into();
491        validate_text("agent steering", &text, MAX_PROMPT_BYTES)?;
492        validate_context_attachment(context.as_ref())?;
493        self.send(id, PiSessionRequest::Steer { text, context })
494    }
495
496    pub fn follow_up(&mut self, id: &AgentId, text: impl Into<String>) -> DevelopmentResult<()> {
497        self.follow_up_with_context(id, text, None)
498    }
499
500    pub fn follow_up_with_context(
501        &mut self,
502        id: &AgentId,
503        text: impl Into<String>,
504        context: Option<Value>,
505    ) -> DevelopmentResult<()> {
506        let text = text.into();
507        validate_text("agent follow-up", &text, MAX_PROMPT_BYTES)?;
508        validate_context_attachment(context.as_ref())?;
509        self.send(id, PiSessionRequest::FollowUp { text, context })
510    }
511
512    pub fn approve_tool(
513        &mut self,
514        id: &AgentId,
515        frame_id: impl Into<String>,
516        approved: bool,
517    ) -> DevelopmentResult<()> {
518        self.refresh()?;
519        let frame_id = frame_id.into();
520        validate_text("Pi approval frame", &frame_id, 256)?;
521        let record = self.record_mut(id)?;
522        if record.snapshot.status.terminal() {
523            return Err(DevelopmentError::Conflict(format!(
524                "agent {} is already terminal",
525                id.as_str()
526            )));
527        }
528        let sender = record.command.as_ref().ok_or_else(|| {
529            DevelopmentError::Conflict(format!("agent {} is still starting", id.as_str()))
530        })?;
531        sender
532            .try_send(WorkerCommand::ApproveTool { frame_id, approved })
533            .map_err(|error| match error {
534                TrySendError::Full(_) => DevelopmentError::Conflict(format!(
535                    "agent {} command queue is full",
536                    id.as_str()
537                )),
538                TrySendError::Disconnected(_) => DevelopmentError::Process(format!(
539                    "agent {} command channel closed",
540                    id.as_str()
541                )),
542            })
543    }
544
545    pub fn request(&mut self, id: &AgentId, request: PiSessionRequest) -> DevelopmentResult<()> {
546        self.send(id, request)
547    }
548
549    /// Mark a non-terminal agent complete after its worker settles.
550    pub fn complete(&mut self, id: &AgentId) -> DevelopmentResult<()> {
551        self.refresh()?;
552        if self.record_mut(id)?.snapshot.status.terminal() {
553            return Err(DevelopmentError::Conflict(format!(
554                "agent {} is already terminal",
555                id.as_str()
556            )));
557        }
558        if let Some(sender) = self.record_mut(id)?.command.take() {
559            let _ = sender.try_send(WorkerCommand::Shutdown);
560        }
561        self.join_worker(id, AgentStatus::Completed)?;
562        self.record_event(id, "completed", Value::Null);
563        self.start_ready_agents()?;
564        Ok(())
565    }
566
567    /// Cancel a non-terminal agent and stop its worker.
568    pub fn cancel(&mut self, id: &AgentId) -> DevelopmentResult<()> {
569        self.refresh()?;
570        if self.record_mut(id)?.snapshot.status.terminal() {
571            return Ok(());
572        }
573        if let Some(sender) = &self.record_mut(id)?.command {
574            let _ = sender.try_send(WorkerCommand::Request(PiSessionRequest::Abort));
575            let _ = sender.try_send(WorkerCommand::Shutdown);
576        }
577        self.record_mut(id)?.command = None;
578        self.join_worker(id, AgentStatus::Cancelled)?;
579        self.record_event(id, "cancelled", Value::Null);
580        self.fail_blocked_dependents();
581        Ok(())
582    }
583    /// Restart a failed or cancelled session under the same agent identity.
584    ///
585    /// Interactive callers keep the selected agent id, so retrying after an
586    /// aborted or crashed turn should not strand the composer on a terminal
587    /// worker. Completed sessions remain terminal by design; callers should
588    /// create a new session when they explicitly finished one.
589    pub fn restart(&mut self, id: &AgentId) -> DevelopmentResult<()> {
590        self.refresh()?;
591        let previous_status = {
592            let record = self.record_mut(id)?;
593            if !matches!(
594                record.snapshot.status,
595                AgentStatus::Failed | AgentStatus::Cancelled
596            ) {
597                return Err(DevelopmentError::Conflict(format!(
598                    "agent {} can only restart from failed or cancelled, not {:?}",
599                    id.as_str(),
600                    record.snapshot.status
601                )));
602            }
603            record.snapshot.status
604        };
605        // A failure event can be observed just before the worker thread
606        // exits. Join it here instead of making the user retry a second time.
607        self.join_worker(id, previous_status)?;
608        {
609            let record = self.record_mut(id)?;
610            record.snapshot.last_error = None;
611            record.snapshot.last_response_id = None;
612            record.awaiting_agent_settle = false;
613        }
614        self.spawn(id)?;
615        self.record_event(
616            id,
617            "restarted",
618            serde_json::json!({"previousStatus": previous_status.label()}),
619        );
620        Ok(())
621    }
622
623    fn send(&mut self, id: &AgentId, request: PiSessionRequest) -> DevelopmentResult<()> {
624        self.refresh()?;
625        let user_event = match &request {
626            PiSessionRequest::Prompt { text, context } => Some(
627                serde_json::json!({"text": text, "mode": "prompt", "contextAttached": context.is_some()}),
628            ),
629            PiSessionRequest::Steer { text, context } => Some(
630                serde_json::json!({"text": text, "mode": "steer", "contextAttached": context.is_some()}),
631            ),
632            PiSessionRequest::FollowUp { text, context } => Some(
633                serde_json::json!({"text": text, "mode": "follow-up", "contextAttached": context.is_some()}),
634            ),
635            _ => None,
636        };
637        {
638            let record = self.record_mut(id)?;
639            if record.snapshot.status.terminal() || record.snapshot.status == AgentStatus::Queued {
640                return Err(DevelopmentError::Conflict(format!(
641                    "agent {} cannot accept requests while {:?}",
642                    id.as_str(),
643                    record.snapshot.status
644                )));
645            }
646            let sender = record.command.as_ref().ok_or_else(|| {
647                DevelopmentError::Conflict(format!("agent {} is still starting", id.as_str()))
648            })?;
649            sender
650                .try_send(WorkerCommand::Request(request))
651                .map_err(|error| match error {
652                    TrySendError::Full(_) => DevelopmentError::Conflict(format!(
653                        "agent {} command queue is full",
654                        id.as_str()
655                    )),
656                    TrySendError::Disconnected(_) => DevelopmentError::Process(format!(
657                        "agent {} command channel closed",
658                        id.as_str()
659                    )),
660                })?;
661        }
662        if let Some(payload) = user_event {
663            self.record_event(id, "user", payload);
664        }
665        Ok(())
666    }
667
668    fn spawn(&mut self, id: &AgentId) -> DevelopmentResult<()> {
669        let sessions_dir = self.sessions_dir.clone();
670        let events = self.events_tx.clone();
671        let broker = self.broker.clone();
672        let additional_system_prompt = self.additional_system_prompt.clone();
673        let local_tool_executor = self.local_tool_executor.clone();
674        let record = self.record_mut(id)?;
675        let dropped_events = Arc::clone(&record.dropped_events);
676        let spec = record.spec.clone();
677        let worktree = record.snapshot.worktree.clone();
678        let worker_id = id.clone();
679        let (commands_tx, commands_rx) = mpsc::sync_channel(COMMAND_CAPACITY);
680        record.command = Some(commands_tx);
681        record.snapshot.status = AgentStatus::Starting;
682        record.snapshot.started_at_ms = Some(now_ms());
683        record.snapshot.updated_at_ms = now_ms();
684        let worker = thread::Builder::new()
685            .name(format!("glass-{}", id.as_str()))
686            .spawn(move || {
687                run_worker(
688                    worker_id,
689                    spec,
690                    WorkerRuntime {
691                        local_tool_executor,
692                        worktree,
693                        sessions_dir,
694                        broker,
695                        additional_system_prompt,
696                    },
697                    commands_rx,
698                    events,
699                    dropped_events,
700                )
701            })
702            .map_err(DevelopmentError::Io)?;
703        record.worker = Some(worker);
704        self.record_event(id, "starting", Value::Null);
705        Ok(())
706    }
707
708    fn start_ready_agents(&mut self) -> DevelopmentResult<()> {
709        let ready: Vec<_> = self
710            .records
711            .iter()
712            .filter(|(_, record)| record.snapshot.status == AgentStatus::Queued)
713            .filter(|(_, record)| {
714                record.spec.dependencies.iter().all(|dependency| {
715                    self.records.get(dependency).is_some_and(|dependency| {
716                        dependency.snapshot.status == AgentStatus::Completed
717                    })
718                })
719            })
720            .map(|(id, _)| id.clone())
721            .collect();
722        for id in ready {
723            self.spawn(&id)?;
724        }
725        Ok(())
726    }
727
728    fn fail_blocked_dependents(&mut self) {
729        let terminal_failures: BTreeSet<_> = self
730            .records
731            .iter()
732            .filter(|(_, record)| {
733                matches!(
734                    record.snapshot.status,
735                    AgentStatus::Failed | AgentStatus::Cancelled
736                )
737            })
738            .map(|(id, _)| id.clone())
739            .collect();
740        let blocked: Vec<_> = self
741            .records
742            .iter()
743            .filter(|(_, record)| record.snapshot.status == AgentStatus::Queued)
744            .filter(|(_, record)| {
745                record
746                    .spec
747                    .dependencies
748                    .iter()
749                    .any(|dependency| terminal_failures.contains(dependency))
750            })
751            .map(|(id, _)| id.clone())
752            .collect();
753        for id in blocked {
754            if let Some(record) = self.records.get_mut(&id) {
755                record.snapshot.status = AgentStatus::Failed;
756                record.snapshot.last_error = Some("a required agent did not complete".into());
757                record.snapshot.updated_at_ms = now_ms();
758            }
759            self.record_event(
760                &id,
761                "dependencyFailed",
762                Value::String("a required agent did not complete".into()),
763            );
764        }
765    }
766
767    fn enforce_budgets(&mut self) {
768        let now = now_ms();
769        let exceeded: Vec<_> = self
770            .records
771            .iter()
772            .filter(|(_, record)| !record.snapshot.status.terminal())
773            .filter(|(_, record)| {
774                record
775                    .spec
776                    .max_runtime_seconds
777                    .zip(record.snapshot.started_at_ms)
778                    .is_some_and(|(seconds, started)| {
779                        now.saturating_sub(started) > u128::from(seconds) * 1_000
780                    })
781                    || record
782                        .spec
783                        .max_events
784                        .is_some_and(|limit| record.snapshot.event_count >= limit)
785            })
786            .map(|(id, _)| id.clone())
787            .collect();
788        for id in exceeded {
789            if let Some(record) = self.records.get_mut(&id) {
790                if let Some(sender) = record.command.take() {
791                    let _ = sender.try_send(WorkerCommand::Request(PiSessionRequest::Abort));
792                    let _ = sender.try_send(WorkerCommand::Shutdown);
793                }
794                record.snapshot.status = AgentStatus::Failed;
795                record.snapshot.last_error = Some("agent budget exceeded".into());
796                record.snapshot.updated_at_ms = now;
797            }
798            self.record_event(&id, "budgetExceeded", Value::Null);
799        }
800    }
801
802    fn apply_worker_event(&mut self, event: WorkerEvent) {
803        match event {
804            WorkerEvent::Ready(id) => {
805                if let Some(record) = self.records.get_mut(&id)
806                    && !record.snapshot.status.terminal()
807                {
808                    record.snapshot.status = AgentStatus::Idle;
809                    record.snapshot.updated_at_ms = now_ms();
810                }
811                self.record_event(&id, "ready", Value::Null);
812            }
813            WorkerEvent::RequestStarted(id, request_id, waits_for_agent) => {
814                if let Some(record) = self.records.get_mut(&id)
815                    && !record.snapshot.status.terminal()
816                {
817                    record.snapshot.last_response_id = Some(request_id.clone());
818                    record.snapshot.status = AgentStatus::Working;
819                    record.awaiting_agent_settle |= waits_for_agent;
820                    record.snapshot.updated_at_ms = now_ms();
821                }
822                self.record_event(&id, "requestStarted", Value::String(request_id));
823            }
824            WorkerEvent::Pi(id, value) => {
825                if let Some(record) = self.records.get_mut(&id) {
826                    let event_type = value.get("type").and_then(Value::as_str);
827                    if event_type == Some("agent_settled") && !record.snapshot.status.terminal() {
828                        record.awaiting_agent_settle = false;
829                        record.snapshot.status = AgentStatus::Idle;
830                    }
831                    if event_type == Some("response")
832                        && !record.awaiting_agent_settle
833                        && !record.snapshot.status.terminal()
834                    {
835                        record.snapshot.status = AgentStatus::Idle;
836                    }
837                    record.snapshot.event_count = record.snapshot.event_count.saturating_add(1);
838                    record.snapshot.updated_at_ms = now_ms();
839                    if record.snapshot.evidence.len() == AGENT_EVIDENCE_CAPACITY {
840                        record.snapshot.evidence.remove(0);
841                    }
842                    record.snapshot.evidence.push(value.clone());
843                }
844                let kind = value
845                    .get("type")
846                    .and_then(Value::as_str)
847                    .unwrap_or("piEvent")
848                    .to_string();
849                self.record_event(&id, &kind, value);
850            }
851            WorkerEvent::Failed(id, error) => {
852                if let Some(record) = self.records.get_mut(&id)
853                    && !record.snapshot.status.terminal()
854                {
855                    record.snapshot.status = AgentStatus::Failed;
856                    record.snapshot.last_error = Some(error.clone());
857                    record.snapshot.updated_at_ms = now_ms();
858                    record.command = None;
859                }
860                self.record_event(&id, "failed", Value::String(error));
861            }
862            WorkerEvent::Stopped(id) => {
863                if let Some(record) = self.records.get_mut(&id) {
864                    record.command = None;
865                }
866                self.record_event(&id, "stopped", Value::Null);
867            }
868        }
869    }
870
871    fn join_worker(&mut self, id: &AgentId, terminal: AgentStatus) -> DevelopmentResult<()> {
872        let worker = self.record_mut(id)?.worker.take();
873        if let Some(worker) = worker
874            && worker.join().is_err()
875        {
876            let error = format!("agent {} worker panicked", id.as_str());
877            let record = self.record_mut(id)?;
878            record.command = None;
879            record.snapshot.status = AgentStatus::Failed;
880            record.snapshot.last_error = Some(error.clone());
881            record.snapshot.updated_at_ms = now_ms();
882            self.record_event(
883                id,
884                "workerPanicked",
885                serde_json::json!({"agentId":id.as_str(),"error":error}),
886            );
887            return Err(DevelopmentError::Process(error));
888        }
889        let record = self.record_mut(id)?;
890        record.command = None;
891        record.snapshot.status = terminal;
892        record.snapshot.updated_at_ms = now_ms();
893        Ok(())
894    }
895
896    fn reap_finished_workers(&mut self) {
897        let finished = self
898            .records
899            .iter()
900            .filter(|(_, record)| {
901                record
902                    .worker
903                    .as_ref()
904                    .is_some_and(|worker| worker.is_finished())
905            })
906            .map(|(id, _)| id.clone())
907            .collect::<Vec<_>>();
908        for id in finished {
909            let worker = self
910                .records
911                .get_mut(&id)
912                .and_then(|record| record.worker.take());
913            let panicked = worker.is_some_and(|worker| worker.join().is_err());
914            let mut event = None;
915            if let Some(record) = self.records.get_mut(&id) {
916                record.command = None;
917                if panicked {
918                    let error = format!("agent {} worker panicked", id.as_str());
919                    record.snapshot.status = AgentStatus::Failed;
920                    record.snapshot.last_error = Some(error.clone());
921                    record.snapshot.updated_at_ms = now_ms();
922                    event = Some((
923                        "workerPanicked",
924                        serde_json::json!({
925                            "agentId":id.as_str(),
926                            "role":record.snapshot.role.clone(),
927                            "task":record.snapshot.task.clone(),
928                            "error":error,
929                        }),
930                    ));
931                } else if !record.snapshot.status.terminal() {
932                    let error = format!(
933                        "agent {} worker exited before a terminal outcome",
934                        id.as_str()
935                    );
936                    record.snapshot.status = AgentStatus::Failed;
937                    record.snapshot.last_error = Some(error.clone());
938                    record.snapshot.updated_at_ms = now_ms();
939                    event = Some(("workerExited", Value::String(error)));
940                }
941            }
942            if let Some((kind, payload)) = event {
943                self.record_event(&id, kind, payload);
944            }
945        }
946    }
947
948    fn record_event(&mut self, id: &AgentId, kind: &str, payload: Value) {
949        if self.history.len() == HISTORY_CAPACITY {
950            self.history.pop_front();
951        }
952        self.history.push_back(AgentEvent {
953            sequence: self.next_event,
954            agent_id: id.clone(),
955            timestamp_ms: now_ms(),
956            kind: kind.into(),
957            payload,
958        });
959        self.next_event = self.next_event.saturating_add(1);
960    }
961
962    fn record_mut(&mut self, id: &AgentId) -> DevelopmentResult<&mut AgentRecord> {
963        self.records
964            .get_mut(id)
965            .ok_or_else(|| DevelopmentError::NotFound(format!("agent {}", id.as_str())))
966    }
967}
968
969impl Drop for AgentRegistry {
970    fn drop(&mut self) {
971        let mut workers = Vec::new();
972        for (id, record) in &mut self.records {
973            if let Some(sender) = record.command.take() {
974                let _ = sender.try_send(WorkerCommand::Shutdown);
975            }
976            if let Some(worker) = record.worker.take() {
977                workers.push((id.clone(), worker));
978            }
979        }
980        for (id, worker) in workers {
981            if worker.join().is_err() {
982                tracing::error!(agent_id = %id.as_str(), "agent worker panicked during registry drop");
983            }
984        }
985    }
986}
987
988fn run_worker(
989    id: AgentId,
990    spec: AgentSpec,
991    runtime: WorkerRuntime,
992    commands: Receiver<WorkerCommand>,
993    events: SyncSender<WorkerEvent>,
994    dropped_events: Arc<AtomicU64>,
995) {
996    let options = PiRuntimeOptions {
997        unrestricted: spec.unrestricted,
998        session_dir: runtime.sessions_dir,
999        name: Some(format!("{}: {}", spec.role, id.as_str())),
1000        model: spec.model,
1001        thinking: spec.thinking,
1002        broker: runtime.broker,
1003        local_tool_executor: runtime.local_tool_executor,
1004        additional_system_prompt: runtime.additional_system_prompt,
1005        resume: false,
1006    };
1007    let mut harness = match GlassPiRuntime::spawn(&runtime.worktree, options) {
1008        Ok(harness) => harness,
1009        Err(error) => {
1010            send_critical_worker_event(&events, WorkerEvent::Failed(id, error.to_string()));
1011            return;
1012        }
1013    };
1014    send_critical_worker_event(&events, WorkerEvent::Ready(id.clone()));
1015    loop {
1016        loop {
1017            match commands.try_recv() {
1018                Ok(WorkerCommand::Request(request)) => {
1019                    let waits_for_agent = matches!(
1020                        request,
1021                        PiSessionRequest::Prompt { .. } | PiSessionRequest::FollowUp { .. }
1022                    );
1023                    match harness.start_request(request) {
1024                        Ok(request_id) => send_critical_worker_event(
1025                            &events,
1026                            WorkerEvent::RequestStarted(id.clone(), request_id, waits_for_agent),
1027                        ),
1028                        Err(error) => {
1029                            send_critical_worker_event(
1030                                &events,
1031                                WorkerEvent::Failed(id.clone(), error.to_string()),
1032                            );
1033                            return;
1034                        }
1035                    }
1036                }
1037                Ok(WorkerCommand::ApproveTool { frame_id, approved }) => {
1038                    if let Err(error) = harness.resolve_tool_approval(&frame_id, approved) {
1039                        send_critical_worker_event(
1040                            &events,
1041                            WorkerEvent::Pi(
1042                                id.clone(),
1043                                serde_json::json!({
1044                                    "type":"glass_tool_approval_resolved",
1045                                    "frameId":frame_id,
1046                                    "approved":approved,
1047                                    "ok":false,
1048                                    "error":error.to_string(),
1049                                }),
1050                            ),
1051                        );
1052                    }
1053                }
1054                Ok(WorkerCommand::Shutdown) | Err(TryRecvError::Disconnected) => {
1055                    send_critical_worker_event(&events, WorkerEvent::Stopped(id));
1056                    return;
1057                }
1058                Err(TryRecvError::Empty) => break,
1059            }
1060        }
1061        match harness.recv_event_timeout(Duration::from_millis(25)) {
1062            Ok(Some(value)) => send_lossy_worker_event(
1063                &events,
1064                WorkerEvent::Pi(id.clone(), value),
1065                &dropped_events,
1066            ),
1067            Ok(None) => {}
1068            Err(error) => {
1069                send_critical_worker_event(&events, WorkerEvent::Failed(id, error.to_string()));
1070                return;
1071            }
1072        }
1073    }
1074}
1075
1076fn send_lossy_worker_event(
1077    events: &SyncSender<WorkerEvent>,
1078    event: WorkerEvent,
1079    dropped_events: &AtomicU64,
1080) {
1081    if matches!(events.try_send(event), Err(TrySendError::Full(_))) {
1082        dropped_events.fetch_add(1, Ordering::Relaxed);
1083    }
1084}
1085
1086fn send_critical_worker_event(events: &SyncSender<WorkerEvent>, event: WorkerEvent) {
1087    let _ = events.send(event);
1088}
1089
1090fn validate_spec(
1091    root: &Path,
1092    records: &BTreeMap<AgentId, AgentRecord>,
1093    spec: &AgentSpec,
1094) -> DevelopmentResult<()> {
1095    validate_text("agent role", &spec.role, 128)?;
1096    validate_text("agent task", &spec.task, MAX_PROMPT_BYTES)?;
1097    if spec.dependencies.len() > MAX_AGENTS {
1098        return Err(DevelopmentError::InvalidInput(
1099            "agent dependency list is too large".into(),
1100        ));
1101    }
1102    let mut unique = BTreeSet::new();
1103    for dependency in &spec.dependencies {
1104        if !unique.insert(dependency) {
1105            return Err(DevelopmentError::InvalidInput(format!(
1106                "duplicate dependency {}",
1107                dependency.as_str()
1108            )));
1109        }
1110        if !records.contains_key(dependency) {
1111            return Err(DevelopmentError::NotFound(format!(
1112                "dependency agent {}",
1113                dependency.as_str()
1114            )));
1115        }
1116    }
1117    if let Some(worktree) = &spec.worktree {
1118        let worktree = std::fs::canonicalize(worktree)?;
1119        if worktree == Path::new("/")
1120            || (!worktree.starts_with(root) && !worktree.join(".git").exists())
1121        {
1122            return Err(DevelopmentError::PathOutsideWorkspace(worktree));
1123        }
1124    }
1125    if spec.max_runtime_seconds == Some(0) || spec.max_events == Some(0) {
1126        return Err(DevelopmentError::InvalidInput(
1127            "agent budgets must be greater than zero".into(),
1128        ));
1129    }
1130    Ok(())
1131}
1132
1133fn validate_text(description: &str, text: &str, limit: usize) -> DevelopmentResult<()> {
1134    if text.trim().is_empty() || text.len() > limit || text.contains('\0') {
1135        return Err(DevelopmentError::InvalidInput(format!(
1136            "{description} must contain 1..={limit} bytes without NUL"
1137        )));
1138    }
1139    Ok(())
1140}
1141
1142fn validate_context_attachment(context: Option<&Value>) -> DevelopmentResult<()> {
1143    let Some(context) = context else {
1144        return Ok(());
1145    };
1146    if !context.is_object() {
1147        return Err(DevelopmentError::InvalidInput(
1148            "agent context attachment must be a JSON object".into(),
1149        ));
1150    }
1151    let size = serde_json::to_vec(context)
1152        .map_err(|error| DevelopmentError::Serialization(error.to_string()))?
1153        .len();
1154    if size > 64 * 1024 {
1155        return Err(DevelopmentError::InvalidInput(
1156            "agent context attachment exceeds 64 KiB".into(),
1157        ));
1158    }
1159    Ok(())
1160}
1161
1162fn now_ms() -> u128 {
1163    SystemTime::now()
1164        .duration_since(UNIX_EPOCH)
1165        .unwrap_or_default()
1166        .as_millis()
1167}
1168
1169#[cfg(test)]
1170mod tests {
1171    use super::*;
1172    use std::sync::atomic::{AtomicU64, Ordering};
1173
1174    static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);
1175
1176    fn test_root() -> PathBuf {
1177        let sequence = NEXT_ROOT.fetch_add(1, Ordering::Relaxed);
1178        let root = std::env::temp_dir().join(format!(
1179            "glass-agent-registry-{}-{sequence}",
1180            std::process::id()
1181        ));
1182        std::fs::create_dir_all(&root).unwrap();
1183        root
1184    }
1185
1186    #[test]
1187    fn lossy_worker_queue_reports_dropped_events() {
1188        let (sender, _receiver) = mpsc::sync_channel(1);
1189        let id = AgentId::parse("agent-overflow").unwrap();
1190        sender.send(WorkerEvent::Ready(id.clone())).unwrap();
1191        let dropped = AtomicU64::new(0);
1192        send_lossy_worker_event(
1193            &sender,
1194            WorkerEvent::Pi(id, serde_json::json!({"type":"delta"})),
1195            &dropped,
1196        );
1197        assert_eq!(dropped.load(Ordering::Relaxed), 1);
1198    }
1199
1200    fn insert_test_worker(
1201        registry: &mut AgentRegistry,
1202        id: AgentId,
1203        command: SyncSender<WorkerCommand>,
1204        worker: thread::JoinHandle<()>,
1205    ) {
1206        let spec = AgentSpec::new("lifecycle-test", "prove worker ownership");
1207        let now = now_ms();
1208        registry.records.insert(
1209            id.clone(),
1210            AgentRecord {
1211                snapshot: AgentSnapshot {
1212                    id,
1213                    role: spec.role.clone(),
1214                    task: spec.task.clone(),
1215                    status: AgentStatus::Working,
1216                    dependencies: Vec::new(),
1217                    model: None,
1218                    thinking: None,
1219                    worktree: registry.root.clone(),
1220                    unrestricted: false,
1221                    created_at_ms: now,
1222                    started_at_ms: Some(now),
1223                    updated_at_ms: now,
1224                    event_count: 0,
1225                    dropped_event_count: 0,
1226                    last_error: None,
1227                    last_response_id: None,
1228                    evidence: Vec::new(),
1229                },
1230                spec,
1231                command: Some(command),
1232                worker: Some(worker),
1233                awaiting_agent_settle: false,
1234                dropped_events: Arc::new(AtomicU64::new(0)),
1235            },
1236        );
1237    }
1238
1239    #[test]
1240    fn repeated_cancel_joins_every_owned_worker() {
1241        let root = test_root();
1242        let mut registry = AgentRegistry::new(&root).unwrap();
1243        let exited = Arc::new(AtomicU64::new(0));
1244        for sequence in 0..128 {
1245            let id = AgentId::parse(format!("agent-test-{sequence}")).unwrap();
1246            let (command, commands) = mpsc::sync_channel(COMMAND_CAPACITY);
1247            let exits = Arc::clone(&exited);
1248            let worker = thread::spawn(move || {
1249                while let Ok(command) = commands.recv() {
1250                    if matches!(command, WorkerCommand::Shutdown) {
1251                        break;
1252                    }
1253                }
1254                exits.fetch_add(1, Ordering::SeqCst);
1255            });
1256            insert_test_worker(&mut registry, id.clone(), command, worker);
1257            registry.cancel(&id).unwrap();
1258            let record = registry.records.get(&id).unwrap();
1259            assert_eq!(record.snapshot.status, AgentStatus::Cancelled);
1260            assert!(record.worker.is_none());
1261        }
1262        assert_eq!(exited.load(Ordering::SeqCst), 128);
1263        drop(registry);
1264        std::fs::remove_dir_all(root).unwrap();
1265    }
1266
1267    #[test]
1268    fn worker_panic_becomes_attributed_agent_failure() {
1269        let root = test_root();
1270        let mut registry = AgentRegistry::new(&root).unwrap();
1271        let id = AgentId::parse("agent-panic-test").unwrap();
1272        let (command, _commands) = mpsc::sync_channel(COMMAND_CAPACITY);
1273        let worker = thread::spawn(|| panic!("intentional worker panic"));
1274        worker.thread().unpark();
1275        insert_test_worker(&mut registry, id.clone(), command, worker);
1276        for _ in 0..100 {
1277            registry.refresh().unwrap();
1278            if registry.records[&id].snapshot.status == AgentStatus::Failed {
1279                break;
1280            }
1281            thread::sleep(Duration::from_millis(5));
1282        }
1283        let snapshot = registry.snapshot(&id).unwrap();
1284        assert_eq!(snapshot.status, AgentStatus::Failed);
1285        assert!(snapshot.last_error.unwrap().contains(id.as_str()));
1286        assert!(registry.history(0).unwrap().iter().any(|event| {
1287            event.agent_id == id
1288                && event.kind == "workerPanicked"
1289                && event.payload["role"] == "lifecycle-test"
1290        }));
1291        drop(registry);
1292        std::fs::remove_dir_all(root).unwrap();
1293    }
1294
1295    fn wait_for_status(
1296        registry: &mut AgentRegistry,
1297        id: &AgentId,
1298        expected: AgentStatus,
1299    ) -> AgentSnapshot {
1300        for _ in 0..800 {
1301            let snapshot = registry.snapshot(id).unwrap();
1302            if snapshot.status == expected {
1303                return snapshot;
1304            }
1305            if snapshot.status == AgentStatus::Failed {
1306                panic!("agent failed: {:?}", snapshot.last_error);
1307            }
1308            thread::sleep(Duration::from_millis(25));
1309        }
1310        panic!("agent did not reach {expected:?}");
1311    }
1312
1313    #[test]
1314    fn independent_pi_sessions_schedule_dependencies_and_stream_state() {
1315        if std::process::Command::new("pi")
1316            .arg("--version")
1317            .output()
1318            .is_err()
1319        {
1320            return;
1321        }
1322        let root = test_root();
1323        let mut registry = AgentRegistry::new(&root).unwrap();
1324        let first = registry
1325            .create(AgentSpec::new("implementer", "inspect state"))
1326            .unwrap();
1327        let peer = registry
1328            .create(AgentSpec::new("researcher", "inspect independent state"))
1329            .unwrap();
1330        wait_for_status(&mut registry, &first, AgentStatus::Idle);
1331        wait_for_status(&mut registry, &peer, AgentStatus::Idle);
1332        registry.request(&first, PiSessionRequest::State).unwrap();
1333        registry.request(&peer, PiSessionRequest::State).unwrap();
1334        for _ in 0..200 {
1335            let snapshot = registry.snapshot(&first).unwrap();
1336            if snapshot
1337                .evidence
1338                .iter()
1339                .any(|event| event.get("type").and_then(Value::as_str) == Some("response"))
1340            {
1341                break;
1342            }
1343            thread::sleep(Duration::from_millis(25));
1344        }
1345        assert!(registry.snapshot(&first).unwrap().event_count > 0);
1346        for _ in 0..200 {
1347            if registry.snapshot(&peer).unwrap().event_count > 0 {
1348                break;
1349            }
1350            thread::sleep(Duration::from_millis(25));
1351        }
1352        assert!(registry.snapshot(&peer).unwrap().event_count > 0);
1353
1354        let mut dependent = AgentSpec::new("reviewer", "review evidence");
1355        dependent.dependencies.push(first.clone());
1356        let second = registry.create(dependent).unwrap();
1357        assert_eq!(
1358            registry.snapshot(&second).unwrap().status,
1359            AgentStatus::Queued
1360        );
1361        registry.complete(&first).unwrap();
1362        wait_for_status(&mut registry, &second, AgentStatus::Idle);
1363        assert_ne!(first, second);
1364        registry.cancel(&second).unwrap();
1365        assert_eq!(
1366            registry.snapshot(&second).unwrap().status,
1367            AgentStatus::Cancelled
1368        );
1369        registry.restart(&second).unwrap();
1370        wait_for_status(&mut registry, &second, AgentStatus::Idle);
1371        registry.cancel(&second).unwrap();
1372        registry.cancel(&peer).unwrap();
1373        drop(registry);
1374        std::fs::remove_dir_all(root).unwrap();
1375    }
1376
1377    #[test]
1378    fn invalid_agent_specs_and_failed_dependencies_fail_closed() {
1379        let root = test_root();
1380        let mut registry = AgentRegistry::new(&root).unwrap();
1381        assert!(registry.create(AgentSpec::new("", "task")).is_err());
1382        let first = registry.create(AgentSpec::new("lead", "task")).unwrap();
1383        let mut dependent = AgentSpec::new("review", "task");
1384        dependent.dependencies.push(first.clone());
1385        let second = registry.create(dependent).unwrap();
1386        registry.cancel(&first).unwrap();
1387        assert_eq!(
1388            registry.snapshot(&second).unwrap().status,
1389            AgentStatus::Failed
1390        );
1391        drop(registry);
1392        std::fs::remove_dir_all(root).unwrap();
1393    }
1394
1395    #[test]
1396    fn context_attachments_are_bounded_json_objects() {
1397        assert!(validate_context_attachment(None).is_ok());
1398        assert!(validate_context_attachment(Some(&serde_json::json!("text"))).is_err());
1399        let oversized = serde_json::json!({"text": "x".repeat(64 * 1024)});
1400        assert!(validate_context_attachment(Some(&oversized)).is_err());
1401        let bounded = serde_json::json!({
1402            "schemaVersion": "glass.agent-context.v1",
1403            "browser": {"browserRevision": 12}
1404        });
1405        assert!(validate_context_attachment(Some(&bounded)).is_ok());
1406    }
1407}