Skip to main content

BackgroundManager

Struct BackgroundManager 

Source
pub struct BackgroundManager { /* private fields */ }
Expand description

后台任务管理器(Send + Sync,可跨线程共享)

Implementations§

Source§

impl BackgroundManager

Source

pub fn new() -> Self

创建新的后台任务管理器实例

Source

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 用于存活检测; shell 后台任务传 None(通过 child_pid + pgrep 检测)

Source

pub fn adopt_process( &self, command: &str, pid: u32, started_at: Instant, ) -> (String, Arc<Mutex<String>>)

接管已运行中的子进程,注册为 running 后台任务 返回 task_id 和共享输出缓冲区;调用方负责在独立线程中继续读取 child 输出并写入 buffer, 进程结束后调用 complete_task() 完成任务

Source

pub fn update_child_pid(&self, task_id: &str, pid: u32)

更新子进程 PID(执行线程在 spawn 成功后调用)

Source

pub fn set_pty_writer(&self, task_id: &str, writer: Box<dyn Write + Send>)

设置 PTY writer 句柄(交互式会话使用)

Source

pub fn session_stdin(&self, task_id: &str, text: &str) -> Result<(), String>

向交互式会话写入 stdin

Source

pub fn session_stdout( &self, task_id: &str, timeout_ms: u64, ) -> Result<String, String>

读取交互式会话的当前输出(从 output_buffer 读取,可选等待)

Source

pub fn session_quit(&self, task_id: &str) -> Result<(), String>

终止交互式会话(drop PTY 句柄,进程收到 SIGHUP 自然退出)

Source

pub fn complete_task(&self, task_id: &str, status: &str, result: String)

内部方法:标记任务完成并添加通知

Source

pub fn drain_notifications(&self) -> Vec<BgNotification>

Drain 所有待处理的通知(agent loop 每轮调用)

Source

pub fn get_task_status(&self, task_id: &str) -> Option<Value>

查询单个后台任务状态(包括中间输出)

Source

pub fn is_running(&self, task_id: &str) -> bool

检查任务是否仍在运行

Source

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。

Source

pub fn cleanup_dead_tasks(&self)

清理已死进程:双重验证(PID 存在 + command 匹配) 在每次 LLM request 前调用,确保 system prompt 中的后台任务状态准确

Trait Implementations§

Source§

impl Debug for BackgroundManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BackgroundManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more