pub struct BackgroundManager { /* private fields */ }Expand description
后台任务管理器(Send + Sync,可跨线程共享)
Implementations§
Source§impl BackgroundManager
impl BackgroundManager
Sourcepub fn spawn_command(
&self,
command: &str,
_cwd: Option<String>,
_timeout_secs: u64,
is_thread_running: Option<Arc<AtomicBool>>,
) -> (String, Arc<Mutex<String>>)
pub fn spawn_command( &self, command: &str, _cwd: Option<String>, _timeout_secs: u64, is_thread_running: Option<Arc<AtomicBool>>, ) -> (String, Arc<Mutex<String>>)
注册后台命令为 running 状态,返回 task_id(实际 spawn 在调用方完成)
返回 task_id 和共享输出缓冲区的 Arc,调用方将 buffer 传给 reader 线程实现实时写入
is_thread_running:线程类任务(如 SubAgent)可传入 Arc
Sourcepub fn adopt_process(
&self,
command: &str,
pid: u32,
started_at: Instant,
) -> (String, Arc<Mutex<String>>)
pub fn adopt_process( &self, command: &str, pid: u32, started_at: Instant, ) -> (String, Arc<Mutex<String>>)
接管已运行中的子进程,注册为 running 后台任务 返回 task_id 和共享输出缓冲区;调用方负责在独立线程中继续读取 child 输出并写入 buffer, 进程结束后调用 complete_task() 完成任务
Sourcepub fn update_child_pid(&self, task_id: &str, pid: u32)
pub fn update_child_pid(&self, task_id: &str, pid: u32)
更新子进程 PID(执行线程在 spawn 成功后调用)
Sourcepub fn set_pty_writer(&self, task_id: &str, writer: Box<dyn Write + Send>)
pub fn set_pty_writer(&self, task_id: &str, writer: Box<dyn Write + Send>)
设置 PTY writer 句柄(交互式会话使用)
Sourcepub fn session_stdout(
&self,
task_id: &str,
timeout_ms: u64,
) -> Result<String, String>
pub fn session_stdout( &self, task_id: &str, timeout_ms: u64, ) -> Result<String, String>
读取交互式会话的当前输出(从 output_buffer 读取,可选等待)
Sourcepub fn session_quit(&self, task_id: &str) -> Result<(), String>
pub fn session_quit(&self, task_id: &str) -> Result<(), String>
终止交互式会话(drop PTY 句柄,进程收到 SIGHUP 自然退出)
Sourcepub fn complete_task(&self, task_id: &str, status: &str, result: String)
pub fn complete_task(&self, task_id: &str, status: &str, result: String)
内部方法:标记任务完成并添加通知
Sourcepub fn drain_notifications(&self) -> Vec<BgNotification>
pub fn drain_notifications(&self) -> Vec<BgNotification>
Drain 所有待处理的通知(agent loop 每轮调用)
Sourcepub fn get_task_status(&self, task_id: &str) -> Option<Value>
pub fn get_task_status(&self, task_id: &str) -> Option<Value>
查询单个后台任务状态(包括中间输出)
Sourcepub fn is_running(&self, task_id: &str) -> bool
pub fn is_running(&self, task_id: &str) -> bool
检查任务是否仍在运行
Sourcepub fn list_running(&self) -> Vec<(String, String, u64, bool)>
pub fn list_running(&self) -> Vec<(String, String, u64, bool)>
列出当前所有 status == “running” 的任务,用于注入 LLM 上下文 返回 (task_id, command 摘要, 已运行秒数) 的列表,按 task_id 排序 返回 (task_id, command_summary, elapsed_secs, is_interactive)。 is_interactive = true 表示该任务持有 PTY writer,是交互式会话(sid), 应当使用 Session 工具 stdin/stdout 操作,而不是 TaskOutput。
Sourcepub fn cleanup_dead_tasks(&self)
pub fn cleanup_dead_tasks(&self)
清理已死进程:双重验证(PID 存在 + command 匹配) 在每次 LLM request 前调用,确保 system prompt 中的后台任务状态准确
Trait Implementations§
Source§impl Debug for BackgroundManager
impl Debug for BackgroundManager
Auto Trait Implementations§
impl !Freeze for BackgroundManager
impl RefUnwindSafe for BackgroundManager
impl Send for BackgroundManager
impl Sync for BackgroundManager
impl Unpin for BackgroundManager
impl UnsafeUnpin for BackgroundManager
impl UnwindSafe for BackgroundManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.