2026-11-20 08:16:43 +00:00
//! 任務: everything handed to a bot — the owner's or a teammate's words, a routine's run, a goal,
//! a message from a channel or another bot — as one task, whose state the program works out from
//! what is already kept: the chat's messages and steps, how the turn ended (Bot::last_turn), what
//! runs now and what waits for the person, the follow-ups, the goal and the plan, the steps'
//! evidence (evidence::tally) and each file's 交付確認 (attach.rs). Never from what the bot says.
//!
//! The chat is where a task is read from. A chat cleared, or folded into a summary, would lose
//! the recent ones: so each change of a finished (or waiting) task's state is kept on its bot as a
//! small event (Bot::tasks: appended, never changed, the newest KEEP), and a task the chat no
//! longer has is shown as its last event says.
//!
//! The brain works the list out (app/tasks_ui.rs); the phone, the screens and the bots'
//! `list_tasks` all read that one list.
use serde ::{ Deserialize , Serialize } ;
use serde_json ::{ json , Value } ;
use std ::collections ::{ HashMap , HashSet } ;
use crate ::model ::{ Bot , Message , Role , ToolStatus , TurnOutcome } ;
/// Events kept on each bot (the oldest go first).
pub const KEEP : usize = 40 ;
/// Tasks in the list, every bot together (the newest first).
pub const SHOWN : usize = 60 ;
/// Tasks read from each chat (its newest).
const RECENT : usize = 30 ;
/// A task's opening words, at most.
const TEXT_CHARS : usize = 60 ;
/// A person's message nobody started on yet is still on its way this long (ms); after that it
/// had no reply.
const START_GRACE_MS : i64 = 15_000 ;
2026-11-20 08:20:57 +00:00
/// 規格驗收(人類助理 §可觀測 P0) : 一個進行中的任務超過這麼久沒有任何動靜( 訊息、步驟、
/// 交付全算),卡片標「疑似卡住」。狀態不變,只有標記——宁可漏報,不误報:长命令(下载、
/// 编译)本来就几分钟没消息。
pub const STALL_MS : i64 = 15 * 60_000 ;
2026-11-20 08:16:43 +00:00
/// The read-only tool a bot answers 「現在在跑什麼」 with.
pub const TOOL : & str = " list_tasks " ;
/// The reaction that says the person has it (the 「你已確認」 signal, with a file opened).
pub const CONFIRM_REACTION : & str = " 👍 " ;
/// Where a task is. Each says something the others don't: started, a file there, shown on a
/// device, opened by the person — never one `done` for all of them.
#[ derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Default) ]
#[ serde(rename_all = " snake_case " ) ]
pub enum Status {
/// A turn of it runs now.
Running ,
/// Waiting for the person: a request to allow, a question card, a goal stopped to ask.
Waiting ,
/// 追進度: a follow-up it registered is still being checked.
Following ,
/// 目標: its goal is still on; the second model checks whether it holds.
Verifying ,
/// Over; it handed over a file no device of the person's has shown yet.
Produced ,
/// A device of the person's showed a file it handed over.
Delivered ,
/// The person opened a file it handed over, or put 👍 on its answer.
Confirmed ,
/// Over with steps the system saw go through, nothing to hand over.
Answered ,
/// The goal's checker (a second model) said it holds.
Verified ,
Failed ,
/// The person stopped it.
Stopped ,
/// Over, and nothing the system saw backs it (only words, or only another bot's).
#[ default ]
Unverified ,
}
impl Status {
pub fn label ( self ) -> & 'static str {
match self {
Status ::Running = > " 進行中 " ,
Status ::Waiting = > " 等你 " ,
Status ::Following = > " 追進度中 " ,
Status ::Verifying = > " 驗收中 " ,
Status ::Produced = > " 已產出 " ,
Status ::Delivered = > " 已交付 " ,
Status ::Confirmed = > " 你已確認 " ,
Status ::Answered = > " 已回覆 " ,
Status ::Verified = > " 已驗收 " ,
Status ::Failed = > " 失敗 " ,
Status ::Stopped = > " 已停止 " ,
Status ::Unverified = > " 結束但未驗證 " ,
}
}
/// Still going (a turn, the person, a follow-up or a goal check is pending).
pub fn active ( self ) -> bool {
matches! ( self , Status ::Running | Status ::Waiting | Status ::Following | Status ::Verifying )
}
}
/// Who handed it over.
#[ derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Default) ]
#[ serde(rename_all = " snake_case " ) ]
pub enum Source {
#[ default ]
Owner ,
Member ,
Routine ,
Trigger ,
Goal ,
Plan ,
FollowUp ,
Monitor ,
Channel ,
Bot ,
Delivery ,
}
impl Source {
pub fn label ( self ) -> & 'static str {
match self {
Source ::Owner = > " 你 " ,
Source ::Member = > " 團隊成員 " ,
Source ::Routine = > " 例行任務 " ,
Source ::Trigger = > " 觸發 " ,
Source ::Goal = > " 目標 " ,
Source ::Plan = > " 計畫 " ,
Source ::FollowUp = > " 追進度 " ,
Source ::Monitor = > " 監看 " ,
Source ::Channel = > " 訊息管道 " ,
Source ::Bot = > " 其他 Bot " ,
Source ::Delivery = > " 交付 " ,
}
}
}
/// How good the backing is (規格 §6): A the system saw it itself (a step's result), C another
/// bot said so (ask_bot's answer: not checked here), Nothing none at all (未驗).
#[ derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Default) ]
pub enum Level {
#[ serde(rename = " a " ) ]
A ,
#[ serde(rename = " c " ) ]
C ,
#[ default ]
#[ serde(rename = " none " ) ]
Nothing ,
}
impl Level {
pub fn label ( self ) -> & 'static str {
match self {
Level ::A = > " A 系統看到 " ,
Level ::C = > " C 別的 Bot 轉述 " ,
Level ::Nothing = > " 未驗 " ,
}
}
}
/// How the task's last turn ended.
#[ derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq) ]
#[ serde(rename_all = " snake_case " ) ]
pub enum End {
Success ,
Cancelled ,
Error ,
/// AnyBot stopped while it ran (the 「繼續」 card).
Interrupted ,
/// Nothing started on it.
NoReply ,
}
/// How far a file it handed over got with the person (attach::Delivery, and a failure).
#[ derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Default) ]
#[ serde(rename_all = " snake_case " ) ]
pub enum Dlv {
#[ default ]
InChat ,
Shown ,
Opened ,
/// A device of the person's couldn't show it, and none has.
Failed ,
}
#[ derive(Clone, Serialize, Deserialize, Debug, PartialEq, Default) ]
pub struct Artifact {
pub name : String ,
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub id : String ,
#[ serde(default) ]
pub delivery : Dlv ,
/// The device it got furthest on (or failed on).
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub device : String ,
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub why : String ,
}
impl Artifact {
fn of ( a : & crate ::attach ::Attachment ) -> Artifact {
use crate ::attach ::{ Delivery , SeenKind } ;
let owner = | k : SeenKind | a . seen . iter ( ) . find ( | s | ! s . guest & & s . kind = = k ) ;
let ( delivery , at ) = match a . delivery ( ) {
Delivery ::Opened = > ( Dlv ::Opened , owner ( SeenKind ::Opened ) ) ,
Delivery ::Shown = > ( Dlv ::Shown , owner ( SeenKind ::Shown ) ) ,
Delivery ::InChat = > match a . first_failure_unseen ( ) {
Some ( f ) = > ( Dlv ::Failed , Some ( f ) ) ,
None = > ( Dlv ::InChat , None ) ,
} ,
} ;
Artifact { name : a . name . clone ( ) , id : a . id . clone ( ) , delivery , device : at . map ( | s | s . device . clone ( ) ) . unwrap_or_default ( ) , why : at . filter ( | _ | delivery = = Dlv ::Failed ) . map ( | s | s . why . clone ( ) ) . unwrap_or_default ( ) }
}
/// In words: 「a.png: 已在 iPhone 顯示」.
pub fn line ( & self ) -> String {
let how = match self . delivery {
Dlv ::InChat = > " 已在聊天裡,還沒有裝置回報顯示 " . to_string ( ) ,
Dlv ::Shown = > format! ( " 已在 {} 顯示 " , self . device ) ,
Dlv ::Opened = > format! ( " {} 點開了 " , self . device ) ,
Dlv ::Failed = > format! ( " {} 顯示失敗: {} " , self . device , self . why ) ,
} ;
format! ( " {} : {how} " , self . name )
}
}
fn is_zero ( n : & u32 ) -> bool {
* n = = 0
}
fn is_zero_i64 ( n : & i64 ) -> bool {
* n = = 0
}
/// One task as every screen shows it.
#[ derive(Clone, Serialize, Deserialize, Debug, PartialEq, Default) ]
pub struct Task {
/// Its own name, never changed: the bot and when its opening message was written.
pub id : String ,
pub bot : u64 ,
/// The opening message's time (ms): when it started, and where the chat jumps to.
pub at : i64 ,
/// The opening words, one line.
pub text : String ,
#[ serde(default) ]
pub source : Source ,
#[ serde(default) ]
pub status : Status ,
/// What it does now (running), or the last step it took.
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub doing : String ,
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub computer : String ,
#[ serde(default, skip_serializing_if = " is_zero " ) ]
pub steps : u32 ,
#[ serde(default, skip_serializing_if = " is_zero " ) ]
pub failed : u32 ,
/// Its last sign of life (ms): the newest message or step of it.
#[ serde(default, skip_serializing_if = " is_zero_i64 " ) ]
pub heartbeat : i64 ,
/// When it ended (ms; 0 while it goes on).
#[ serde(default, skip_serializing_if = " is_zero_i64 " ) ]
pub finished : i64 ,
#[ serde(default, skip_serializing_if = " Vec::is_empty " ) ]
pub artifacts : Vec < Artifact > ,
#[ serde(default) ]
pub evidence : Level ,
/// What the system saw, a line each.
#[ serde(default, skip_serializing_if = " Vec::is_empty " ) ]
pub proof : Vec < String > ,
/// Why it is stuck, waits or failed.
#[ serde(default, skip_serializing_if = " String::is_empty " ) ]
pub why : String ,
/// How its last turn ended (a record keeps it once the chat is gone).
#[ serde(default, skip_serializing_if = " Option::is_none " ) ]
pub ended : Option < End > ,
/// Shown from its record: the chat no longer has it.
#[ serde(default, skip_serializing_if = " std::ops::Not::not " ) ]
pub kept : bool ,
}
/// A task's state as the system noted it (Bot::tasks). Appended, never changed.
#[ derive(Clone, Serialize, Deserialize, Debug, PartialEq, Default) ]
pub struct Event {
/// When it was noted (ms).
pub noted : i64 ,
#[ serde(flatten) ]
pub task : Task ,
}
/// What runs and waits now, from the brain (nothing on a copy).
#[ derive(Clone, Debug, Default) ]
pub struct Live {
/// Bots with a turn running: the step it is on, in words (empty while it thinks).
pub running : HashMap < u64 , String > ,
/// Bots waiting for the person: for what, in words.
pub waiting : HashMap < u64 , String > ,
/// Bots whose goal is being checked (or is about to be).
pub checking : HashSet < u64 > ,
}
/// The goal's checker's last word on a task, from its line in the chat.
#[ derive(Clone, Debug, PartialEq) ]
pub enum GoalEnd {
Met ,
Impossible ( String ) ,
}
/// What a task's state is worked out from.
#[ derive(Clone, Debug, Default, PartialEq) ]
pub struct Facts {
pub running : bool ,
pub waiting : bool ,
pub following : bool ,
/// Its goal is on the bot and going.
pub goal_active : bool ,
/// …and stopped to ask the person.
pub goal_paused : bool ,
pub goal : Option < GoalEnd > ,
pub ended : Option < End > ,
pub evidence : Level ,
pub artifacts : usize ,
/// A device of the owner's showed one of its files.
pub shown : bool ,
/// The person opened one.
pub opened : bool ,
/// The person put 👍 on its answer.
pub thumbs : bool ,
}
/// The task's state: the first that holds, in this order. A pure function, tested as a table.
pub fn status ( f : & Facts ) -> Status {
if f . waiting | | f . goal_paused {
return Status ::Waiting ;
}
if f . running {
return Status ::Running ;
}
if f . following {
return Status ::Following ;
}
if f . goal_active {
return Status ::Verifying ;
}
if matches! ( f . goal , Some ( GoalEnd ::Impossible ( _ ) ) ) {
return Status ::Failed ;
}
match f . ended {
Some ( End ::Error | End ::Interrupted | End ::NoReply ) = > return Status ::Failed ,
Some ( End ::Cancelled ) = > return Status ::Stopped ,
Some ( End ::Success ) = > { }
None = > return Status ::Unverified ,
}
if f . opened | | f . thumbs {
Status ::Confirmed
} else if f . shown {
Status ::Delivered
} else if f . artifacts > 0 {
Status ::Produced
} else if f . goal = = Some ( GoalEnd ::Met ) {
Status ::Verified
} else if f . evidence = = Level ::A {
Status ::Answered
} else {
Status ::Unverified
}
}
/// Whether the facts back a state: what each one needs before a task may be in it (no
/// 「已交付」 without a device that showed it, no 「你已確認」 without the person's signal…).
pub fn backed ( s : Status , f : & Facts ) -> bool {
match s {
Status ::Running = > f . running ,
Status ::Waiting = > f . waiting | | f . goal_paused ,
Status ::Following = > f . following ,
Status ::Verifying = > f . goal_active ,
Status ::Produced = > f . artifacts > 0 & & f . ended = = Some ( End ::Success ) ,
Status ::Delivered = > f . shown & & f . ended = = Some ( End ::Success ) ,
Status ::Confirmed = > ( f . opened | | f . thumbs ) & & f . ended = = Some ( End ::Success ) ,
Status ::Answered = > f . evidence = = Level ::A & & f . ended = = Some ( End ::Success ) ,
Status ::Verified = > f . goal = = Some ( GoalEnd ::Met ) ,
Status ::Failed = > matches! ( f . ended , Some ( End ::Error | End ::Interrupted | End ::NoReply ) ) | | matches! ( f . goal , Some ( GoalEnd ::Impossible ( _ ) ) ) ,
Status ::Stopped = > f . ended = = Some ( End ::Cancelled ) ,
Status ::Unverified = > ! f . running & & ! f . waiting ,
}
}
/// Whether a task may go from one state to the next. It can always take up again (a goal's next
/// turn, a follow-up waking it); once over it only goes further along the way to the person
/// (產出 → 交付 → 確認) — a record never slides back (a chat cleared, a mark taken off).
pub fn legal ( from : Status , to : Status ) -> bool {
use Status ::* ;
if from = = to | | to . active ( ) | | from . active ( ) {
return true ;
}
match from {
Produced = > matches! ( to , Delivered | Confirmed ) ,
Delivered | Answered | Verified | Unverified = > to = = Confirmed ,
Confirmed | Failed | Stopped = > false ,
Running | Waiting | Following | Verifying = > true ,
}
}
/// A task's own name: its bot and the time of its opening message.
pub fn task_id ( bot : u64 , m : & Message ) -> String {
if m . at > 0 {
format! ( " {bot} - {} " , m . at )
} else {
use std ::hash ::{ Hash , Hasher } ;
let mut h = std ::collections ::hash_map ::DefaultHasher ::new ( ) ;
m . text . hash ( & mut h ) ;
format! ( " {bot} -h {:x} " , h . finish ( ) )
}
}
/// Whether a message starts a turn of its own (a steer joins the running one; AnyBot's own lines,
/// a summary, a 支線's card or report and the 「繼續」 card don't start one).
fn starts_turn ( m : & Message ) -> bool {
m . role = = Role ::User & & ! m . steer & & m . branch = = 0 & & ! m . note & & ! m . summary & & ! m . interrupted
}
/// What a woken message goes on with: a goal's next turn, a follow-up or a watched command
/// reporting back, a file a device couldn't show.
#[ derive(Clone, Debug, PartialEq, Eq, Hash) ]
enum Key {
Goal ( String ) ,
Follow ( u64 ) ,
Monitor ( u64 ) ,
Attach ( String ) ,
}
fn number ( s : & str ) -> Option < u64 > {
s . chars ( ) . take_while ( char ::is_ascii_digit ) . collect ::< String > ( ) . parse ( ) . ok ( )
}
/// The tag a deliver wake starts with (app/deliver_ui.rs WAKE_TAG; the test ties the two).
const DELIVER_WAKE : & str = " [交付]你傳的附件 " ;
fn continues ( m : & Message ) -> Option < Key > {
if ! m . wake {
return None ;
}
let t = m . text . as_str ( ) ;
if t . starts_with ( crate ::goal ::TAG ) & & ! m . goal . is_empty ( ) {
return Some ( Key ::Goal ( m . goal . clone ( ) ) ) ;
}
if let Some ( r ) = t . strip_prefix ( " [追進度 # " ) {
return number ( r ) . map ( Key ::Follow ) ;
}
if let Some ( r ) = t . strip_prefix ( " [監看 # " ) {
return number ( r ) . map ( Key ::Monitor ) ;
}
if let Some ( r ) = t . strip_prefix ( DELIVER_WAKE ) {
let id : String = r . chars ( ) . take_while ( | c | c . is_ascii_alphanumeric ( ) | | * c = = '-' | | * c = = '_' ) . collect ( ) ;
return ( ! id . is_empty ( ) ) . then_some ( Key ::Attach ( id ) ) ;
}
None
}
fn source_of ( m : & Message ) -> Source {
let t = m . text . as_str ( ) ;
let tag = | a : & str , b : & str | t . starts_with ( a ) | | t . starts_with ( b ) ;
if m . routine ! = 0 | | tag ( " [例行任務] " , " ⏰ " ) {
Source ::Routine
} else if tag ( " [觸發] " , " ⚡ " ) {
Source ::Trigger
} else if tag ( " [訊息管道] " , " 💬 " ) {
Source ::Channel
} else if m . from_bot ! = 0 | | tag ( " [來自 Bot] " , " 📨 " ) {
Source ::Bot
} else if t . starts_with ( " [追進度 " ) {
Source ::FollowUp
} else if t . starts_with ( " [監看 " ) {
Source ::Monitor
} else if t . starts_with ( " [交付] " ) {
Source ::Delivery
} else if ! m . goal . is_empty ( ) | | t . starts_with ( crate ::goal ::TAG ) {
Source ::Goal
} else if m . untrusted_sender {
Source ::Member
} else if t . starts_with ( " /plan " ) {
Source ::Plan
} else {
Source ::Owner
}
}
/// The opening words in one line, without the tag a woken message carries.
fn opening ( m : & Message ) -> String {
let mut t = m . text . trim ( ) ;
if t . starts_with ( '[ ' ) {
if let Some ( ( _ , rest ) ) = t . split_once ( '] ' ) {
t = rest . trim_start ( ) ;
}
}
for tag in [ " ⏰ " , " ⚡ " , " 💬 " , " 📨 " ] {
t = t . strip_prefix ( tag ) . unwrap_or ( t ) ;
}
let line = t . lines ( ) . map ( str ::trim ) . find ( | l | ! l . is_empty ( ) ) . unwrap_or ( " " ) ;
let mut out : String = line . chars ( ) . take ( TEXT_CHARS ) . collect ( ) ;
if line . chars ( ) . count ( ) > TEXT_CHARS {
out . push ( '…' ) ;
}
if out . is_empty ( ) & & ! m . attachments . is_empty ( ) {
out = m . attachments . iter ( ) . map ( | a | a . name . as_str ( ) ) . collect ::< Vec < _ > > ( ) . join ( " 、 " ) ;
}
out
}
/// How a turn ended, from what it left in the chat (a turn ended before AnyBot kept its record).
fn ended_by_chat ( turn : & [ Message ] ) -> End {
match turn . iter ( ) . rev ( ) . find ( | m | m . interrupted | | ( m . role = = Role ::Assistant & & ! m . note ) ) {
None = > End ::NoReply ,
Some ( m ) if m . interrupted = > End ::Interrupted ,
Some ( m ) if m . error = > End ::Error ,
Some ( m ) if m . text . trim ( ) = = " (已停止) " = > End ::Cancelled ,
Some ( _ ) = > End ::Success ,
}
}
fn clip ( s : & str , n : usize ) -> String {
let line = s . lines ( ) . map ( str ::trim ) . find ( | l | ! l . is_empty ( ) ) . unwrap_or ( " " ) ;
let mut out : String = line . chars ( ) . take ( n ) . collect ( ) ;
if line . chars ( ) . count ( ) > n {
out . push ( '…' ) ;
}
out
}
/// The follow-up a step registered (its result: 「已登記追進度 #N…」).
fn registered_follow ( m : & Message ) -> Option < u64 > {
( m . tool_name = = crate ::followup ::TOOL ) . then ( | | m . text . strip_prefix ( " 已登記追進度 # " ) . and_then ( number ) ) . flatten ( )
}
/// The watched command a step started (shell's result: 「已開始監看(背景指令 #N) …」).
fn registered_monitor ( m : & Message ) -> Option < u64 > {
m . text . strip_prefix ( " 已開始監看(背景指令 # " ) . and_then ( number )
}
/// One task, worked out from its turns (each a range of the chat).
struct Read < ' a > {
bot : & ' a Bot ,
head : usize ,
turns : Vec < ( usize , usize ) > ,
/// This task's goal is the bot's goal now.
goal_here : bool ,
}
fn work_out ( r : & Read , newest_turn : Option < usize > , live : & Live , records : & [ Event ] , now : i64 ) -> Task {
let ( bot , msgs ) = ( r . bot , & r . bot . messages [ .. ] ) ;
let m0 = & msgs [ r . head ] ;
let id = task_id ( bot . id , m0 ) ;
let ( mut steps , mut failed , mut done_a , mut done_c ) = ( 0 u32 , 0 u32 , 0 u32 , 0 u32 ) ;
let mut sends : Vec < String > = vec! [ ] ;
let mut computer : Option < String > = None ;
let mut last_step : Option < ( & str , & str ) > = None ;
let mut heartbeat = m0 . at ;
let mut artifacts : Vec < Artifact > = vec! [ ] ;
let mut thumbs = false ;
let mut goal_end : Option < GoalEnd > = None ;
let mut follows : Vec < u64 > = vec! [ ] ;
for & ( s , e ) in & r . turns {
let turn = & msgs [ s .. e ] ;
let t = crate ::evidence ::tally ( turn ) ;
steps + = t . steps ;
failed + = t . failed ;
sends . extend ( t . sends ) ;
for m in turn {
heartbeat = heartbeat . max ( m . at ) ;
match m . role {
Role ::Assistant if m . note = > {
if m . text . starts_with ( " 目標達成 " ) {
goal_end = Some ( GoalEnd ::Met ) ;
} else if let Some ( rest ) = m . text . strip_prefix ( " 目標做不到,已清除: " ) {
goal_end = Some ( GoalEnd ::Impossible ( clip ( rest . lines ( ) . nth ( 1 ) . unwrap_or ( rest ) , 120 ) ) ) ;
}
}
Role ::Assistant = > {
thumbs | = m . reactions . iter ( ) . any ( | x | x = = CONFIRM_REACTION ) ;
for c in & m . tool_calls {
last_step = Some ( ( c . name . as_str ( ) , c . args . as_str ( ) ) ) ;
if c . status = = ToolStatus ::Done {
match c . name . as_str ( ) {
" ask_bot " = > done_c + = 1 ,
" react " | " show_card " = > { }
_ = > done_a + = 1 ,
}
}
if let Some ( n ) = crate ::tools ::args_of ( & c . args ) [ " computer " ] . as_str ( ) . filter ( | n | ! n . trim ( ) . is_empty ( ) ) {
computer = Some ( n . to_string ( ) ) ;
}
}
artifacts . extend ( m . attachments . iter ( ) . map ( Artifact ::of ) ) ;
}
Role ::Tool = > follows . extend ( registered_follow ( m ) ) ,
Role ::User = > { }
}
}
}
let last = r . turns . last ( ) . copied ( ) . unwrap_or ( ( r . head , r . head + 1 ) ) ;
let newest = Some ( last . 0 ) = = newest_turn ;
let record = records . iter ( ) . rev ( ) . find ( | e | e . task . id = = id ) . map ( | e | & e . task ) ;
let mut f = Facts { running : newest & & live . running . contains_key ( & bot . id ) , waiting : newest & & live . waiting . contains_key ( & bot . id ) , .. Default ::default ( ) } ;
let follow = bot . follow_ups . iter ( ) . find ( | x | follows . contains ( & x . id ) ) ;
f . following = follow . is_some ( ) ;
let goal = bot . goal . as_ref ( ) . filter ( | _ | r . goal_here ) ;
f . goal_active = goal . is_some_and ( | g | g . active ( ) ) ;
f . goal_paused = goal . is_some_and ( | g | ! g . active ( ) ) ;
f . goal = goal_end ;
let turn_at = msgs [ last . 0 ] . at ;
let settled = bot . last_turn . as_ref ( ) . filter ( | t | newest & & t . settled_at_ms > = turn_at ) ;
if ! f . running & & ! f . waiting {
f . ended = Some ( match settled {
Some ( t ) = > match t . outcome {
TurnOutcome ::Success = > End ::Success ,
TurnOutcome ::Cancelled = > End ::Cancelled ,
TurnOutcome ::Error if t . turn_id = = " interrupted " = > End ::Interrupted ,
TurnOutcome ::Error = > End ::Error ,
} ,
None = > match record . and_then ( | t | t . ended ) {
Some ( e ) = > e ,
None = > ended_by_chat ( & msgs [ last . 0 .. last . 1 ] ) ,
} ,
} ) ;
// Just sent, not yet started on: on its way, not unanswered.
if f . ended = = Some ( End ::NoReply ) & & newest & & now - turn_at < START_GRACE_MS {
f . ended = None ;
f . running = true ;
}
}
f . evidence = if done_a > 0 {
Level ::A
} else if done_c > 0 {
Level ::C
} else {
Level ::Nothing
} ;
f . artifacts = artifacts . len ( ) ;
f . shown = artifacts . iter ( ) . any ( | a | a . delivery = = Dlv ::Shown ) ;
f . opened = artifacts . iter ( ) . any ( | a | a . delivery = = Dlv ::Opened ) ;
f . thumbs = thumbs ;
let status = status ( & f ) ;
debug_assert! ( backed ( status , & f ) , " {status:?} not backed by {f:?} " ) ;
// What it does, and why it waits, stopped or failed.
let step = last_step . map ( | ( n , a ) | crate ::evidence ::step_line ( n , a ) ) . unwrap_or_default ( ) ;
let plan = bot . plan . as_ref ( ) . filter ( | p | p . since = = m0 . at & & p . stage = = crate ::plan ::Stage ::Doing ) . and_then ( | p | p . current ( ) . map ( | s | format! ( " 計畫第 {} / {} 步: {} " , p . done ( ) + 1 , p . steps . len ( ) , s . title ) ) ) ;
let mut why = String ::new ( ) ;
let doing = match status {
Status ::Running = > plan . unwrap_or_else ( | | live . running . get ( & bot . id ) . filter ( | s | ! s . is_empty ( ) ) . cloned ( ) . unwrap_or_else ( | | if f . running & & ! live . running . contains_key ( & bot . id ) { " 準備開始 " . into ( ) } else { " 思考中 " . into ( ) } ) ) ,
Status ::Waiting = > {
why = match ( live . waiting . get ( & bot . id ) . filter ( | _ | f . waiting ) , goal ) {
( Some ( w ) , _ ) = > w . clone ( ) ,
( None , Some ( g ) ) = > format! ( " 目標暫停: {} ,傳一句話就接著做 " , if g . paused . is_empty ( ) { " 等你回覆 " } else { & g . paused } ) ,
_ = > " 等你 " . into ( ) ,
} ;
why . clone ( )
}
Status ::Following = > {
let x = follow . expect ( " following " ) ;
why = format! ( " 追進度 # {} 「 {} 」: {} " , x . id , x . what , clip ( x . last . lines ( ) . last ( ) . unwrap_or ( " 還沒檢查 " ) , 80 ) ) ;
format! ( " 追進度 # {} 「 {} 」 " , x . id , x . what )
}
Status ::Verifying = > {
if let Some ( g ) = goal . filter ( | g | ! g . reason . is_empty ( ) ) {
why = format! ( " 上次檢查: {} " , clip ( & g . reason , 120 ) ) ;
}
if live . checking . contains ( & bot . id ) { " 另一個模型正在檢查目標達成沒 " . into ( ) } else { " 目標還沒達成,準備下一輪 " . into ( ) }
}
_ = > step ,
} ;
if why . is_empty ( ) {
why = match status {
Status ::Failed = > match ( & f . goal , f . ended ) {
( Some ( GoalEnd ::Impossible ( w ) ) , _ ) = > format! ( " 目標做不到: {w} " ) ,
( _ , Some ( End ::Interrupted ) ) = > " AnyBot 關掉時中斷了(按聊天裡的「繼續」接著做) " . into ( ) ,
( _ , Some ( End ::NoReply ) ) = > " Bot 沒有開始回覆 " . into ( ) ,
_ = > msgs [ last . 0 .. last . 1 ] . iter ( ) . rev ( ) . find ( | m | m . error ) . map ( | m | clip ( & m . text , 120 ) ) . filter ( | s | ! s . is_empty ( ) ) . unwrap_or_else ( | | " 出錯了 " . into ( ) ) ,
} ,
Status ::Stopped = > " 被停止了 " . into ( ) ,
Status ::Produced = > artifacts . iter ( ) . find ( | a | a . delivery = = Dlv ::Failed ) . map ( | a | format! ( " {} 顯示失敗: {} " , a . device , a . why ) ) . unwrap_or_default ( ) ,
Status ::Unverified if f . evidence = = Level ::C = > " 只有別的 Bot 的轉述,系統沒有驗證 " . into ( ) ,
Status ::Unverified = > " 這輪沒有系統看得到的結果 " . into ( ) ,
_ = > String ::new ( ) ,
} ;
}
let mut proof = vec! [ ] ;
if done_a > 0 {
proof . push ( if failed > 0 { format! ( " 系統看到 {done_a} 步做完、 {failed} 步失敗或被拒絕 " ) } else { format! ( " 系統看到 {done_a} 步做完 " ) } ) ;
} else if failed > 0 {
proof . push ( format! ( " {failed} 步失敗或被拒絕 " ) ) ;
}
proof . extend ( sends . iter ( ) . take ( 2 ) . map ( | s | format! ( " 送出: {} " , clip ( s , 80 ) ) ) ) ;
if done_c > 0 {
proof . push ( format! ( " 別的 Bot 回答了 {done_c} 次(轉述,系統沒有驗證) " ) ) ;
}
2026-11-20 08:20:57 +00:00
// 進行中的任務太久沒動靜:標「疑似卡住」(狀態不變,只標記;規格 §可觀測)。
if status = = Status ::Running & & now - heartbeat > STALL_MS {
if ! why . is_empty ( ) {
why . push ( ', ' ) ;
}
why . push_str ( & format! ( " 疑似卡住: {} 分鐘沒有動靜 " , ( now - heartbeat ) / 60_000 ) ) ;
}
2026-11-20 08:16:43 +00:00
if f . goal = = Some ( GoalEnd ::Met ) {
proof . push ( " 目標由另一個模型檢查,判定達成 " . into ( ) ) ;
}
let computer = computer . unwrap_or_else ( | | match bot . default_computer . as_str ( ) {
crate ::model ::BOT_COMPUTER_NAME | " " = > crate ::model ::own_computer_name ( & bot . name ) ,
n = > n . to_string ( ) ,
} ) ;
let finished = if status . active ( ) { 0 } else { settled . map ( | t | t . settled_at_ms ) . unwrap_or ( heartbeat ) . max ( heartbeat ) } ;
Task {
id ,
bot : bot . id ,
at : m0 . at ,
text : opening ( m0 ) ,
source : source_of ( m0 ) ,
status ,
doing ,
computer ,
steps ,
failed ,
heartbeat ,
finished ,
artifacts ,
evidence : f . evidence ,
proof ,
why ,
ended : f . ended ,
kept : false ,
}
}
/// The tasks a bot's chat holds (its newest RECENT), oldest first.
pub fn from_chat ( bot : & Bot , live : & Live , now : i64 ) -> Vec < Task > {
let msgs = & bot . messages [ .. ] ;
let starts : Vec < usize > = ( 0 .. msgs . len ( ) ) . filter ( | & i | starts_turn ( & msgs [ i ] ) ) . collect ( ) ;
let mut reads : Vec < Read > = vec! [ ] ;
let mut owner : HashMap < Key , usize > = HashMap ::new ( ) ;
for ( k , & s ) in starts . iter ( ) . enumerate ( ) {
let e = starts . get ( k + 1 ) . copied ( ) . unwrap_or ( msgs . len ( ) ) ;
let t = match continues ( & msgs [ s ] ) . and_then ( | key | owner . get ( & key ) . copied ( ) ) {
Some ( t ) = > t ,
None = > {
reads . push ( Read { bot , head : s , turns : vec ! [ ] , goal_here : false } ) ;
reads . len ( ) - 1
}
} ;
reads [ t ] . turns . push ( ( s , e ) ) ;
// What this turn set going: later wakes about it go on with this task.
if ! msgs [ s ] . goal . is_empty ( ) {
owner . insert ( Key ::Goal ( msgs [ s ] . goal . clone ( ) ) , t ) ;
}
for m in & msgs [ s .. e ] {
if let Some ( n ) = registered_follow ( m ) {
owner . insert ( Key ::Follow ( n ) , t ) ;
}
if let Some ( n ) = registered_monitor ( m ) . filter ( | _ | m . role = = Role ::Tool ) {
owner . insert ( Key ::Monitor ( n ) , t ) ;
}
if m . role = = Role ::Assistant {
for a in m . attachments . iter ( ) . filter ( | a | ! a . id . is_empty ( ) ) {
owner . insert ( Key ::Attach ( a . id . clone ( ) ) , t ) ;
}
}
}
}
if let Some ( g ) = & bot . goal {
if let Some ( & t ) = owner . get ( & Key ::Goal ( g . condition . clone ( ) ) ) {
reads [ t ] . goal_here = true ;
}
}
let newest = starts . last ( ) . copied ( ) ;
let from = reads . len ( ) . saturating_sub ( RECENT ) ;
reads [ from .. ] . iter ( ) . map ( | r | work_out ( r , newest , live , & bot . tasks , now ) ) . collect ( )
}
fn last_event < ' a > ( events : & ' a [ Event ] , id : & str ) -> Option < & ' a Event > {
events . iter ( ) . rev ( ) . find ( | e | e . task . id = = id )
}
/// Whether a task may be in `t`'s state after its record `e`: a legal move, or it ran again
/// since (a message after the record: it went through 進行中, which isn't noted).
fn may_follow ( e : & Event , t : & Task ) -> bool {
legal ( e . task . status , t . status ) | | t . heartbeat > e . noted
}
/// A bot's tasks: from its chat, and from its record the ones the chat no longer has. A state the
/// record has gone past is kept (a record never slides back: see `legal`).
pub fn of_bot ( bot : & Bot , live : & Live , now : i64 ) -> Vec < Task > {
let mut out = from_chat ( bot , live , now ) ;
for t in out . iter_mut ( ) {
if let Some ( e ) = last_event ( & bot . tasks , & t . id ) {
if ! may_follow ( e , t ) {
t . status = e . task . status ;
t . why = e . task . why . clone ( ) ;
}
}
}
let have : HashSet < String > = out . iter ( ) . map ( | t | t . id . clone ( ) ) . collect ( ) ;
let mut seen : HashSet < & str > = HashSet ::new ( ) ;
for e in bot . tasks . iter ( ) . rev ( ) {
if have . contains ( & e . task . id ) | | ! seen . insert ( e . task . id . as_str ( ) ) {
continue ;
}
let mut t = e . task . clone ( ) ;
t . bot = bot . id ;
t . kept = true ;
// Going when last noted, and the chat that would say how it went is gone.
if t . status . active ( ) {
t . status = Status ::Unverified ;
t . why = " 聊天裡已經沒有它,系統沒看到它後來怎樣 " . into ( ) ;
t . doing . clear ( ) ;
t . finished = t . finished . max ( e . noted ) ;
}
out . push ( t ) ;
}
out
}
/// Every bot's tasks, the newest (last sign of life) first.
pub fn all ( bots : & [ Bot ] , live : & Live , now : i64 ) -> Vec < Task > {
newest ( bots . iter ( ) . flat_map ( | b | of_bot ( b , live , now ) ) . collect ( ) )
}
/// The newest SHOWN, by their last sign of life.
pub fn newest ( mut v : Vec < Task > ) -> Vec < Task > {
v . sort_by_key ( | t | std ::cmp ::Reverse ( ( t . heartbeat . max ( t . at ) , t . at ) ) ) ;
v . truncate ( SHOWN ) ;
v
}
/// Note on `bot` each of its tasks whose state changed since its last event (appended; past KEEP
/// the oldest go). A running task isn't noted: its end is. True when something was added.
pub fn note ( bot : & mut Bot , tasks : & [ Task ] , now : i64 ) -> bool {
let mut added = false ;
for t in tasks . iter ( ) . filter ( | t | t . bot = = bot . id & & ! t . kept & & t . at > 0 & & t . status ! = Status ::Running ) {
let new = match last_event ( & bot . tasks , & t . id ) {
None = > true ,
Some ( e ) = > ( e . task . status ! = t . status & & may_follow ( e , t ) ) | | ( e . task . ended . is_none ( ) & & t . ended . is_some ( ) ) | | e . task . artifacts ! = t . artifacts ,
} ;
if new {
let mut task = t . clone ( ) ;
task . proof . truncate ( 4 ) ;
bot . tasks . push ( Event { noted : now , task } ) ;
added = true ;
}
}
if bot . tasks . len ( ) > KEEP {
let cut = bot . tasks . len ( ) - KEEP ;
bot . tasks . drain ( .. cut ) ;
}
added
}
/// What needs a look: going on, or failed in the last day.
pub fn attention ( tasks : & [ Task ] , now : i64 ) -> usize {
tasks . iter ( ) . filter ( | t | t . status . active ( ) | | ( t . status = = Status ::Failed & & now - t . finished . max ( t . heartbeat ) < 86_400_000 ) ) . count ( )
}
/// A task as the phone takes it: the task, and its words.
pub fn json ( t : & Task ) -> Value {
let mut v = serde_json ::to_value ( t ) . unwrap_or ( Value ::Null ) ;
v [ " label " ] = json! ( t . status . label ( ) ) ;
v [ " from " ] = json! ( t . source . label ( ) ) ;
v [ " level " ] = json! ( t . evidence . label ( ) ) ;
v
}
/// The tool a bot reads the list with: read-only, the system's own states.
pub fn spec ( ) -> Value {
json! ( { " name " : TOOL ,
" description " : " List the tasks handed to you (or, with all=true, to every bot): each one's state as AnyBot itself works it out from the chat's steps, the turn's end, the follow-ups, the goal checks and whether a device showed the files — not from memory. States: 進行中, 等你 (a request or question waits for the person), 追進度中, 驗收中 (a goal being checked), 已產出 (a file there, no device showed it yet), 已交付 (a device showed it), 你已確認 (the person opened it or put 👍), 已回覆, 已驗收, 失敗, 已停止, 結束但未驗證. Use it when the person asks what is running, whether something is done, or where a file is: answer from these states and their evidence, and never call a task done, delivered or seen when its state doesn't say so. " ,
" parameters " : { " type " : " object " , " properties " : {
" all " : { " type " : " boolean " , " description " : " Every bot's tasks, not only yours (only in the owner's own conversations). " } ,
" limit " : { " type " : " integer " , " description " : " How many, newest first (default 10, at most 30). " } } } } )
}
fn when ( ms : i64 , tz : & str ) -> String {
use chrono ::TimeZone ;
crate ::routines ::tz_of ( tz ) . timestamp_millis_opt ( ms ) . single ( ) . map ( | t | t . format ( " %m/%d %H:%M " ) . to_string ( ) ) . unwrap_or_default ( )
}
/// The list in words, for a bot (`name`: each bot's name).
pub fn describe ( tasks : & [ Task ] , name : & dyn Fn ( u64 ) -> String , tz : & str ) -> String {
if tasks . is_empty ( ) {
return " 目前沒有任務紀錄。 " . into ( ) ;
}
let mut out = vec! [ " 任務( AnyBot 從聊天、步驟和交付回報算出的狀態,不是記憶;最新的在上面): " . to_string ( ) ] ;
for ( i , t ) in tasks . iter ( ) . enumerate ( ) {
let steps = if t . steps > 0 { format! ( " · {} 步( {} 步失敗) " , t . steps , t . failed ) } else { String ::new ( ) } ;
let end = if t . finished > 0 { format! ( " · 結束 {} " , when ( t . finished , tz ) ) } else { String ::new ( ) } ;
out . push ( format! (
" {}. 〔 {}〕 {}:「{}」— {}{}開的 · 在「{}」{steps} · 開始 {} · 最後動靜 {}{end}{} " ,
i + 1 ,
t . status . label ( ) ,
name ( t . bot ) ,
t . text ,
t . source . label ( ) ,
if t . source . label ( ) . ends_with ( | c : char | c . is_ascii_alphabetic ( ) ) { " " } else { " " } ,
t . computer ,
when ( t . at , tz ) ,
when ( t . heartbeat . max ( t . at ) , tz ) ,
if t . kept { " (聊天裡已經沒有,照紀錄) " } else { " " }
) ) ;
if ! t . doing . is_empty ( ) {
out . push ( format! ( " {} : {} " , if t . status . active ( ) { " 現在 " } else { " 最後一步 " } , t . doing ) ) ;
}
if ! t . why . is_empty ( ) & & t . why ! = t . doing {
out . push ( format! ( " 原因: {} " , t . why ) ) ;
}
out . push ( format! ( " 證據 {} {} " , t . evidence . label ( ) , if t . proof . is_empty ( ) { String ::new ( ) } else { format! ( " : {} " , t . proof . join ( " ; " ) ) } ) ) ;
for a in & t . artifacts {
out . push ( format! ( " 產物 {} " , a . line ( ) ) ) ;
}
}
out . join ( " \n " )
}
/// Who a task's opening words say sent it, as far as telling outsiders apart goes: a teammate's
/// 「【名字】」, a channel's 「Slack「…」的 amy」.
fn sender ( source : Source , text : & str ) -> String {
match source {
Source ::Member = > text . strip_prefix ( '【' ) . and_then ( | r | r . split_once ( '】' ) ) . map ( | ( n , _ ) | n . to_string ( ) ) . unwrap_or_default ( ) ,
Source ::Channel = > text . split_once ( ': ' ) . map ( | ( h , _ ) | h . to_string ( ) ) . unwrap_or_default ( ) ,
_ = > String ::new ( ) ,
}
}
/// `list_tasks` for bot `asker`: its own tasks, or every bot's with `all` — only in a turn the
/// owner started. A turn someone else started (a teammate, a channel) lists only the tasks that
/// same sender handed this bot: the owner's words are not theirs to read.
pub fn answer ( tasks : & [ Task ] , bots : & [ Bot ] , asker : u64 , args : & Value , tz : & str ) -> String {
let Some ( b ) = bots . iter ( ) . find ( | b | b . id = = asker ) else { return " 找不到這個 Bot " . into ( ) } ;
let start = b . messages . iter ( ) . rev ( ) . find ( | m | starts_turn ( m ) ) ;
let outsider = start . is_some_and ( | m | m . untrusted_sender ) ;
let limit = args [ " limit " ] . as_u64 ( ) . unwrap_or ( 10 ) . clamp ( 1 , 30 ) as usize ;
let every = args [ " all " ] . as_bool ( ) . unwrap_or ( false ) & & ! outsider ;
let mine : Vec < Task > = match start . filter ( | _ | outsider ) {
Some ( m ) = > {
let ( src , who ) = ( source_of ( m ) , sender ( source_of ( m ) , & opening ( m ) ) ) ;
tasks . iter ( ) . filter ( | t | t . bot = = asker & & t . source = = src & & ! who . is_empty ( ) & & sender ( t . source , & t . text ) = = who ) . take ( limit ) . cloned ( ) . collect ( )
}
None = > tasks . iter ( ) . filter ( | t | every | | t . bot = = asker ) . take ( limit ) . cloned ( ) . collect ( ) ,
} ;
let name = | id : u64 | bots . iter ( ) . find ( | b | b . id = = id ) . map ( | b | b . name . clone ( ) ) . unwrap_or_else ( | | " 已刪除的 Bot " . into ( ) ) ;
describe ( & mine , & name , tz )
}
#[ cfg(test) ]
mod tests {
use super ::* ;
use crate ::attach ::{ Attachment , SeenKind } ;
use crate ::model ::{ Message , ToolCall , TurnSettlement } ;
fn f ( ) -> Facts {
Facts { ended : Some ( End ::Success ) , .. Default ::default ( ) }
}
/// The states, as a table: the facts → the state, and that state is backed by them.
#[ test ]
fn the_state_follows_the_facts ( ) {
let a = Level ::A ;
let rows : Vec < ( & str , Facts , Status ) > = vec! [
( " a turn runs " , Facts { running : true , .. Default ::default ( ) } , Status ::Running ) ,
( " a card waits " , Facts { running : true , waiting : true , .. Default ::default ( ) } , Status ::Waiting ) ,
( " a goal stopped to ask " , Facts { goal_paused : true , ended : Some ( End ::Success ) , .. Default ::default ( ) } , Status ::Waiting ) ,
( " a follow-up checks " , Facts { following : true , .. f ( ) } , Status ::Following ) ,
( " a goal is checked " , Facts { goal_active : true , .. f ( ) } , Status ::Verifying ) ,
( " running beats following " , Facts { running : true , following : true , .. Default ::default ( ) } , Status ::Running ) ,
( " a file, nobody showed it " , Facts { artifacts : 1 , evidence : a , .. f ( ) } , Status ::Produced ) ,
( " a device showed it " , Facts { artifacts : 1 , shown : true , evidence : a , .. f ( ) } , Status ::Delivered ) ,
( " the person opened it " , Facts { artifacts : 1 , shown : true , opened : true , evidence : a , .. f ( ) } , Status ::Confirmed ) ,
( " 👍 on the answer " , Facts { thumbs : true , .. f ( ) } , Status ::Confirmed ) ,
( " steps went through " , Facts { evidence : a , .. f ( ) } , Status ::Answered ) ,
( " only another bot's word " , Facts { evidence : Level ::C , .. f ( ) } , Status ::Unverified ) ,
( " only words " , f ( ) , Status ::Unverified ) ,
( " the goal holds " , Facts { goal : Some ( GoalEnd ::Met ) , .. f ( ) } , Status ::Verified ) ,
( " the goal can't be done " , Facts { goal : Some ( GoalEnd ::Impossible ( " x " . into ( ) ) ) , evidence : a , .. f ( ) } , Status ::Failed ) ,
( " the turn errored " , Facts { ended : Some ( End ::Error ) , artifacts : 1 , .. Default ::default ( ) } , Status ::Failed ) ,
( " AnyBot stopped meanwhile " , Facts { ended : Some ( End ::Interrupted ) , .. Default ::default ( ) } , Status ::Failed ) ,
( " nothing started " , Facts { ended : Some ( End ::NoReply ) , .. Default ::default ( ) } , Status ::Failed ) ,
( " the person stopped it " , Facts { ended : Some ( End ::Cancelled ) , evidence : a , .. Default ::default ( ) } , Status ::Stopped ) ,
( " no turn known " , Facts ::default ( ) , Status ::Unverified ) ,
] ;
for ( what , facts , want ) in rows {
let got = status ( & facts ) ;
assert_eq! ( got , want , " {what} " ) ;
assert! ( backed ( got , & facts ) , " {what}: {got:?} isn't backed by its facts " ) ;
}
}
/// What each state needs before a task can be in it: never 「已交付」 without a device that
/// showed it, never 「你已確認」 without the person's signal, never 「已回覆」 on words alone.
#[ test ]
fn a_state_needs_its_evidence ( ) {
let a = Level ::A ;
assert! ( ! backed ( Status ::Delivered , & Facts { artifacts : 1 , evidence : a , .. f ( ) } ) ) ;
assert! ( ! backed ( Status ::Confirmed , & Facts { artifacts : 1 , shown : true , .. f ( ) } ) ) ;
assert! ( ! backed ( Status ::Produced , & Facts { evidence : a , .. f ( ) } ) ) ;
assert! ( ! backed ( Status ::Answered , & Facts { evidence : Level ::C , .. f ( ) } ) ) ;
assert! ( ! backed ( Status ::Verified , & Facts { evidence : a , .. f ( ) } ) ) ;
assert! ( ! backed ( Status ::Answered , & Facts { evidence : a , ended : Some ( End ::Error ) , .. Default ::default ( ) } ) ) ;
assert! ( ! backed ( Status ::Running , & f ( ) ) ) ;
assert! ( ! backed ( Status ::Failed , & f ( ) ) ) ;
}
#[ test ]
fn a_record_only_goes_forward ( ) {
use Status ::* ;
let rows = [
( Produced , Delivered , true ) ,
( Delivered , Confirmed , true ) ,
( Answered , Confirmed , true ) ,
( Unverified , Confirmed , true ) ,
( Running , Answered , true ) ,
( Verifying , Verified , true ) ,
( Failed , Running , true ) ,
( Confirmed , Running , true ) ,
( Delivered , Produced , false ) ,
( Confirmed , Delivered , false ) ,
( Confirmed , Answered , false ) ,
( Answered , Produced , false ) ,
( Failed , Answered , false ) ,
( Stopped , Confirmed , false ) ,
( Unverified , Delivered , false ) ,
] ;
for ( from , to , ok ) in rows {
assert_eq! ( legal ( from , to ) , ok , " {from:?} → {to:?} " ) ;
}
}
fn bot ( msgs : Vec < Message > ) -> Bot {
let mut b : Bot = serde_json ::from_value ( json! ( { " id " : 1 , " name " : " 小幫手 " , " shape " : " " , " color " : " " , " provider " : " Xai " , " model " : " m " , " instructions " : " " , " messages " : [ ] } ) ) . unwrap ( ) ;
b . messages = msgs . into ( ) ;
b
}
fn user ( text : & str , at : i64 ) -> Message {
Message { at , .. Message ::user ( text ) }
}
fn said ( text : & str , at : i64 , calls : & [ ( & str , & str , ToolStatus ) ] ) -> Message {
let tool_calls = calls . iter ( ) . enumerate ( ) . map ( | ( i , ( n , a , s ) ) | ToolCall { id : format ! ( " c{at}{i} " ) , name : n . to_string ( ) , args : a . to_string ( ) , status : * s } ) . collect ( ) ;
Message { at , tool_calls , .. Message ::assistant ( text , " " ) }
}
fn result ( id : & str , name : & str , text : & str , at : i64 ) -> Message {
Message { at , .. Message ::tool_result ( id , name , text ) }
}
fn settled ( b : & mut Bot , outcome : TurnOutcome , at : i64 ) {
b . last_turn = Some ( TurnSettlement { turn_id : " t " . into ( ) , outcome , settled_at_ms : at } ) ;
}
#[ test ]
fn each_message_of_the_person_is_a_task_and_steers_join_it ( ) {
let mut b = bot ( vec! [
user ( " 查一下天氣 " , 1_000 ) ,
said ( " " , 1_100 , & [ ( " web_fetch " , r # "{"url":"https://example.com"}"# , ToolStatus ::Done ) ] ) ,
result ( " c11000 " , " web_fetch " , " 晴 " , 1_200 ) ,
Message { steer : true , .. user ( " 順便看明天 " , 1_250 ) } ,
said ( " 晴天 " , 1_300 , & [ ] ) ,
user ( " 你好 " , 2_000 ) ,
said ( " 你好! " , 2_100 , & [ ] ) ,
] ) ;
settled ( & mut b , TurnOutcome ::Success , 2_200 ) ;
let t = from_chat ( & b , & Live ::default ( ) , 10_000 ) ;
assert_eq! ( t . len ( ) , 2 ) ;
assert_eq! ( ( t [ 0 ] . id . as_str ( ) , t [ 0 ] . text . as_str ( ) , t [ 0 ] . status , t [ 0 ] . evidence , t [ 0 ] . steps ) , ( " 1-1000 " , " 查一下天氣 " , Status ::Answered , Level ::A , 1 ) ) ;
assert_eq! ( t [ 0 ] . heartbeat , 1_300 ) ;
assert_eq! ( t [ 0 ] . computer , " 小幫手 的電腦 " ) ;
// Small talk: over, and nothing backs it.
assert_eq! ( ( t [ 1 ] . status , t [ 1 ] . evidence ) , ( Status ::Unverified , Level ::Nothing ) ) ;
assert_eq! ( t [ 1 ] . finished , 2_200 ) ;
}
#[ test ]
fn running_waiting_and_failed_come_from_the_live_state_and_the_turns_end ( ) {
let mut b = bot ( vec! [ user ( " 整理報表 " , 1_000 ) , said ( " " , 1_100 , & [ ( " shell " , r # "{"command":"ls","computer":"工作室"}"# , ToolStatus ::Running ) ] ) ] ) ;
let mut live = Live ::default ( ) ;
live . running . insert ( 1 , " 執行指令 ls " . into ( ) ) ;
let t = & from_chat ( & b , & live , 5_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . doing . as_str ( ) , t . computer . as_str ( ) ) , ( Status ::Running , " 執行指令 ls " , " 工作室 " ) ) ;
live . waiting . insert ( 1 , " 等你允許:執行指令 " . into ( ) ) ;
let t = & from_chat ( & b , & live , 5_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . why . as_str ( ) ) , ( Status ::Waiting , " 等你允許:執行指令 " ) ) ;
// The model service failed: 失敗, with its words as the reason.
b . messages . push ( Message { error : true , at : 1_200 , .. Message ::assistant ( " 模型服務沒有回應 " , " " ) } ) ;
settled ( & mut b , TurnOutcome ::Error , 1_300 ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 5_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . why . as_str ( ) , t . ended ) , ( Status ::Failed , " 模型服務沒有回應 " , Some ( End ::Error ) ) ) ;
// Stopped.
settled ( & mut b , TurnOutcome ::Cancelled , 1_300 ) ;
assert_eq! ( from_chat ( & b , & Live ::default ( ) , 5_000 ) [ 0 ] . status , Status ::Stopped ) ;
}
#[ test ]
fn a_message_nobody_answered_fails_after_a_moment ( ) {
let b = bot ( vec! [ user ( " 在嗎 " , 1_000 ) ] ) ;
assert_eq! ( from_chat ( & b , & Live ::default ( ) , 2_000 ) [ 0 ] . status , Status ::Running ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 1_000 + START_GRACE_MS + 1 ) [ 0 ] ;
assert_eq! ( ( t . status , t . why . as_str ( ) ) , ( Status ::Failed , " Bot 沒有開始回覆 " ) ) ;
}
2026-11-20 08:20:57 +00:00
#[ test ]
fn a_running_task_that_goes_quiet_is_marked_suspected_stalled ( ) {
let mut b = bot ( vec! [ user ( " 整理報表 " , 1_000 ) , said ( " " , 1_100 , & [ ( " shell " , r # "{"command":"ls"}"# , ToolStatus ::Running ) ] ) ] ) ;
let mut live = Live ::default ( ) ;
live . running . insert ( 1 , " 執行指令 ls " . into ( ) ) ;
// 還活著:不標。
let t = & from_chat ( & b , & live , 1_100 + STALL_MS ) [ 0 ] ;
assert_eq! ( ( t . status , t . doing . as_str ( ) , t . why . as_str ( ) ) , ( Status ::Running , " 執行指令 ls " , " " ) ) ;
// 太久沒動靜:標「疑似卡住」,狀態仍是進行中。
let t = & from_chat ( & b , & live , 1_100 + STALL_MS + 1 ) [ 0 ] ;
assert_eq! ( t . status , Status ::Running ) ;
assert! ( t . why . starts_with ( " 疑似卡住: " ) & & t . why . contains ( " 沒有動靜 " ) , " {} " , t . why ) ;
// 結束的任務不標(失敗就是失敗,不疊卡住)。
settled ( & mut b , TurnOutcome ::Error , 1_200 ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 1_200 + STALL_MS + 1 ) [ 0 ] ;
assert_eq! ( t . status , Status ::Failed ) ;
assert! ( ! t . why . contains ( " 疑似卡住 " ) , " {} " , t . why ) ;
}
2026-11-20 08:16:43 +00:00
fn file ( seen : & [ ( & str , SeenKind , & str ) ] ) -> Attachment {
let mut a = Attachment { name : " a.png " . into ( ) , id : " att1 " . into ( ) , image : true , .. Default ::default ( ) } ;
for ( d , k , why ) in seen {
a . note_seen ( d , * k , why , 5 ) ;
}
a
}
#[ test ]
fn a_file_goes_produced_delivered_confirmed_by_what_the_devices_said ( ) {
let turn = | a : Attachment | {
let mut b = bot ( vec! [ user ( " 畫一張圖 " , 1_000 ) , Message { attachments : vec ! [ a ] , .. said ( " 好了 " , 1_100 , & [ ( " send_file " , " {} " , ToolStatus ::Done ) ] ) } ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_200 ) ;
from_chat ( & b , & Live ::default ( ) , 9_000 ) . remove ( 0 )
} ;
let t = turn ( file ( & [ ] ) ) ;
assert_eq! ( ( t . status , t . artifacts [ 0 ] . delivery ) , ( Status ::Produced , Dlv ::InChat ) ) ;
let t = turn ( file ( & [ ( " iPhone " , SeenKind ::Failed , " 這台裝置讀不出這個檔案 " ) ] ) ) ;
assert_eq! ( ( t . status , t . why . as_str ( ) ) , ( Status ::Produced , " iPhone顯示失敗: 這台裝置讀不出這個檔案 " ) ) ;
let t = turn ( file ( & [ ( " iPhone " , SeenKind ::Shown , " " ) ] ) ) ;
assert_eq! ( ( t . status , t . artifacts [ 0 ] . line ( ) ) , ( Status ::Delivered , " a.png: 已在iPhone顯示 " . to_string ( ) ) ) ;
let t = turn ( file ( & [ ( " iPhone " , SeenKind ::Shown , " " ) , ( " iPhone " , SeenKind ::Opened , " " ) ] ) ) ;
assert_eq! ( t . status , Status ::Confirmed ) ;
// A teammate's device says nothing about whether the owner has it.
let mut a = Attachment { name : " a.png " . into ( ) , id : " att1 " . into ( ) , .. Default ::default ( ) } ;
a . note_seen_by ( " 小明的 iPhone " , SeenKind ::Opened , " " , 5 , true ) ;
assert_eq! ( turn ( a ) . status , Status ::Produced ) ;
}
#[ test ]
fn a_thumbs_up_on_the_answer_confirms ( ) {
let mut b = bot ( vec! [ user ( " 記一下 " , 1_000 ) , Message { reactions : vec ! [ " 👍 " . into ( ) ] , .. said ( " 記好了 " , 1_100 , & [ ] ) } ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_200 ) ;
assert_eq! ( from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] . status , Status ::Confirmed ) ;
// The bot's own reaction sits on the person's message: not the person's signal.
let mut b = bot ( vec! [ Message { reactions : vec ! [ " 👍 " . into ( ) ] , .. user ( " 記一下 " , 1_000 ) } , said ( " 記好了 " , 1_100 , & [ ] ) ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_200 ) ;
assert_eq! ( from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] . status , Status ::Unverified ) ;
}
#[ test ]
fn another_bots_answer_is_hearsay ( ) {
let mut b = bot ( vec! [ user ( " 問小明那份報告好了沒 " , 1_000 ) , said ( " " , 1_100 , & [ ( " ask_bot " , r # "{"bot":"小明","question":"好了沒"}"# , ToolStatus ::Done ) ] ) , said ( " 小明說好了 " , 1_200 , & [ ] ) ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_300 ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . evidence ) , ( Status ::Unverified , Level ::C ) ) ;
assert! ( t . proof . iter ( ) . any ( | p | p . contains ( " 轉述 " ) ) , " {:?} " , t . proof ) ;
}
#[ test ]
fn a_goal_is_one_task_over_its_turns ( ) {
let cond = " 建立 hello.txt " ;
let mut b = bot ( vec! [
Message { goal : cond . into ( ) , .. user ( " /goal 建立 hello.txt " , 1_000 ) } ,
said ( " " , 1_100 , & [ ( " edit_file " , r # "{"path":"x"}"# , ToolStatus ::Failed ) ] ) ,
Message { wake : true , goal : cond . into ( ) , .. user ( " [目標]還沒達成:沒有檔案 " , 2_000 ) } ,
said ( " " , 2_100 , & [ ( " edit_file " , r # "{"path":"hello.txt"}"# , ToolStatus ::Done ) ] ) ,
] ) ;
b . goal = Some ( crate ::goal ::Goal ::new ( cond , 1_000 ) ) ;
settled ( & mut b , TurnOutcome ::Success , 2_200 ) ;
let t = from_chat ( & b , & Live ::default ( ) , 9_000 ) ;
assert_eq! ( t . len ( ) , 1 , " {t:?} " ) ;
assert_eq! ( ( t [ 0 ] . source , t [ 0 ] . status , t [ 0 ] . steps , t [ 0 ] . failed ) , ( Source ::Goal , Status ::Verifying , 2 , 1 ) ) ;
let mut live = Live ::default ( ) ;
live . checking . insert ( 1 ) ;
assert_eq! ( from_chat ( & b , & live , 9_000 ) [ 0 ] . doing , " 另一個模型正在檢查目標達成沒 " ) ;
// Stopped to ask.
b . goal . as_mut ( ) . unwrap ( ) . paused = " 已經做了 20 輪 " . into ( ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . why . as_str ( ) ) , ( Status ::Waiting , " 目標暫停:已經做了 20 輪,傳一句話就接著做 " ) ) ;
// Met: the goal is gone, its line stays.
b . goal = None ;
b . messages . push ( Message { note : true , at : 2_300 , .. Message ::assistant ( " 目標達成:建立 hello.txt \n 有了( 2 輪 · 1 分) " , " " ) } ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] ;
assert_eq! ( t . status , Status ::Verified ) ;
// Impossible.
b . messages . pop ( ) ;
b . messages . push ( Message { note : true , at : 2_300 , .. Message ::assistant ( " 目標做不到,已清除:建立 hello.txt \n 沒有權限( 2 輪 · 1 分) " , " " ) } ) ;
let t = & from_chat ( & b , & Live ::default ( ) , 9_000 ) [ 0 ] ;
assert_eq! ( ( t . status , t . why . as_str ( ) ) , ( Status ::Failed , " 目標做不到: 沒有權限( 2 輪 · 1 分) " ) ) ;
}
#[ test ]
fn a_follow_up_keeps_its_task_and_its_wake_goes_on_with_it ( ) {
let reg = crate ::followup ::FollowUp { id : 3 , what : " 下載模型 " . into ( ) , last : " 5G " . into ( ) , .. Default ::default ( ) } ;
let mut b = bot ( vec! [
user ( " 下載模型 " , 1_000 ) ,
said ( " " , 1_100 , & [ ( crate ::followup ::TOOL , " {} " , ToolStatus ::Done ) ] ) ,
result ( " c11000 " , crate ::followup ::TOOL , & crate ::followup ::started_text ( & reg , " Bot 的電腦 " ) , 1_150 ) ,
said ( " 我在追了 " , 1_200 , & [ ] ) ,
user ( " 你好 " , 1_500 ) ,
said ( " 嗨 " , 1_600 , & [ ] ) ,
] ) ;
b . follow_ups = vec! [ reg . clone ( ) ] ;
settled ( & mut b , TurnOutcome ::Success , 1_700 ) ;
let t = from_chat ( & b , & Live ::default ( ) , 9_000 ) ;
assert_eq! ( ( t [ 0 ] . status , t [ 0 ] . doing . as_str ( ) ) , ( Status ::Following , " 追進度 #3「下載模型」 " ) ) ;
assert! ( t [ 0 ] . why . ends_with ( " 5G " ) , " {} " , t [ 0 ] . why ) ;
// Done: AnyBot wakes the bot, and that turn is the same task.
b . follow_ups . clear ( ) ;
let wake = crate ::followup ::wake_text ( & reg , & crate ::followup ::Verdict ::Done , 3_000 ) . unwrap ( ) ;
b . messages . push ( Message { wake : true , .. user ( & wake , 3_000 ) } ) ;
b . messages . push ( said ( " " , 3_100 , & [ ( " shell " , r # "{"command":"ls"}"# , ToolStatus ::Done ) ] ) ) ;
b . messages . push ( said ( " 下載好了 " , 3_200 , & [ ] ) ) ;
settled ( & mut b , TurnOutcome ::Success , 3_300 ) ;
let t = from_chat ( & b , & Live ::default ( ) , 9_000 ) ;
assert_eq! ( t . len ( ) , 2 , " {t:?} " ) ;
assert_eq! ( ( t [ 0 ] . text . as_str ( ) , t [ 0 ] . status , t [ 0 ] . steps , t [ 0 ] . heartbeat ) , ( " 下載模型 " , Status ::Answered , 2 , 3_200 ) ) ;
}
#[ test ]
fn who_handed_it_over ( ) {
let src = | m : Message | source_of ( & m ) ;
assert_eq! ( src ( user ( " 幫我 " , 1 ) ) , Source ::Owner ) ;
assert_eq! ( src ( Message { untrusted_sender : true , .. user ( " 【小明】我也要 " , 1 ) } ) , Source ::Member ) ;
assert_eq! ( src ( Message { wake : true , routine : 4 , .. user ( " [例行任務]早報 " , 1 ) } ) , Source ::Routine ) ;
assert_eq! ( src ( Message { wake : true , untrusted_sender : true , .. user ( " [ 訊息管道] Slack「x」的 amy: hi" , 1 ) } ) , Source ::Channel ) ;
assert_eq! ( src ( Message { from_bot : 2 , .. user ( " [來自 Bot] 幫我查 " , 1 ) } ) , Source ::Bot ) ;
assert_eq! ( src ( user ( " /plan 搬家 " , 1 ) ) , Source ::Plan ) ;
assert_eq! ( opening ( & Message { wake : true , routine : 4 , .. user ( " [例行任務]整理今天的新聞 \n 細節 " , 1 ) } ) , " 整理今天的新聞 " ) ;
assert_eq! ( sender ( Source ::Member , & opening ( & user ( " 【小明】我也要 " , 1 ) ) ) , " 小明 " ) ;
}
#[ test ]
fn a_cleared_chat_keeps_its_recent_tasks_from_the_record ( ) {
let mut b = bot ( vec! [ user ( " 查一下天氣 " , 1_000 ) , said ( " " , 1_100 , & [ ( " web_fetch " , " {} " , ToolStatus ::Done ) ] ) , said ( " 晴 " , 1_200 , & [ ] ) ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_300 ) ;
let live = Live ::default ( ) ;
let tasks = of_bot ( & b , & live , 9_000 ) ;
assert! ( note ( & mut b , & tasks , 9_000 ) ) ;
// Nothing changed: nothing more is noted.
let again = of_bot ( & b , & live , 9_100 ) ;
assert! ( ! note ( & mut b , & again , 9_100 ) ) ;
assert_eq! ( b . tasks . len ( ) , 1 ) ;
// The chat is cleared: the task is still there, as its record says.
b . messages . clear ( ) ;
let t = of_bot ( & b , & live , 9_200 ) ;
assert_eq! ( t . len ( ) , 1 ) ;
assert_eq! ( ( t [ 0 ] . status , t [ 0 ] . kept , t [ 0 ] . text . as_str ( ) ) , ( Status ::Answered , true , " 查一下天氣 " ) ) ;
// The record keeps at most KEEP events, the newest.
for i in 0 .. ( KEEP as i64 + 5 ) {
b . tasks . push ( Event { noted : i , task : Task { id : format ! ( " 1-{i} " ) , .. Default ::default ( ) } } ) ;
}
note ( & mut b , & [ ] , 9_300 ) ;
assert_eq! ( b . tasks . len ( ) , KEEP ) ;
assert_eq! ( b . tasks . last ( ) . unwrap ( ) . task . id , format! ( " 1- {} " , KEEP as i64 + 4 ) ) ;
}
#[ test ]
fn a_record_never_slides_back ( ) {
let mut b = bot ( vec! [ user ( " 記一下 " , 1_000 ) , Message { reactions : vec ! [ " 👍 " . into ( ) ] , .. said ( " 記好了 " , 1_100 , & [ ] ) } ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_200 ) ;
let live = Live ::default ( ) ;
let t = of_bot ( & b , & live , 9_000 ) ;
note ( & mut b , & t , 9_000 ) ;
// The 👍 taken off: what the person said once stays said.
b . messages [ 1 ] . reactions . clear ( ) ;
let t = of_bot ( & b , & live , 9_100 ) ;
assert_eq! ( t [ 0 ] . status , Status ::Confirmed ) ;
assert! ( ! note ( & mut b , & t , 9_100 ) ) ;
}
/// AnyBot stopped mid-turn (失敗, noted), then 「繼續」 finished it: running again in between
/// isn't noted, and the record doesn't hold it back at 失敗.
#[ test ]
fn a_task_that_ran_again_moves_on_from_its_record ( ) {
let mut b = bot ( vec! [ user ( " 整理報表 " , 1_000 ) , said ( " " , 1_100 , & [ ( " shell " , " {} " , ToolStatus ::Done ) ] ) , Message { interrupted : true , at : 1_200 , .. Message ::assistant ( " 中斷了 " , " " ) } ] ) ;
b . last_turn = Some ( TurnSettlement { turn_id : " interrupted " . into ( ) , outcome : TurnOutcome ::Error , settled_at_ms : 1_300 } ) ;
let live = Live ::default ( ) ;
let t = of_bot ( & b , & live , 2_000 ) ;
assert_eq! ( t [ 0 ] . status , Status ::Failed ) ;
note ( & mut b , & t , 2_000 ) ;
b . messages . push ( said ( " 整理好了 " , 3_000 , & [ ] ) ) ;
settled ( & mut b , TurnOutcome ::Success , 3_100 ) ;
let t = of_bot ( & b , & live , 4_000 ) ;
assert_eq! ( t [ 0 ] . status , Status ::Answered ) ;
assert! ( note ( & mut b , & t , 4_000 ) ) ;
assert_eq! ( b . tasks . last ( ) . unwrap ( ) . task . status , Status ::Answered ) ;
}
#[ test ]
fn records_from_before_read_in_and_old_bots_have_none ( ) {
// A bot saved before tasks were kept.
let b = bot ( vec! [ ] ) ;
assert! ( b . tasks . is_empty ( ) ) ;
let v = serde_json ::to_value ( & b ) . unwrap ( ) ;
assert! ( v . get ( " tasks " ) . is_none ( ) , " an empty record isn't written " ) ;
// An event as written, read back.
let e : Event = serde_json ::from_value ( json! ( { " noted " : 5 , " id " : " 1-1000 " , " bot " : 1 , " at " : 1000 , " text " : " 查 " , " status " : " delivered " , " evidence " : " a " , " artifacts " : [ { " name " : " a.png " , " delivery " : " shown " , " device " : " iPhone " } ] } ) ) . unwrap ( ) ;
assert_eq! ( ( e . task . status , e . task . evidence , e . task . artifacts [ 0 ] . delivery ) , ( Status ::Delivered , Level ::A , Dlv ::Shown ) ) ;
// Small on disk: what is empty isn't written.
let s = serde_json ::to_string ( & Event { noted : 1 , task : Task { id : " 1-1 " . into ( ) , bot : 1 , at : 1 , text : " 查 " . into ( ) , .. Default ::default ( ) } } ) . unwrap ( ) ;
assert! ( s . len ( ) < 110 , " {s} " ) ;
}
#[ test ]
fn the_bot_reads_states_and_evidence_in_words ( ) {
let mut b = bot ( vec! [ user ( " 畫一張圖 " , 1_000 ) , Message { attachments : vec ! [ file ( & [ ( " iPhone " , SeenKind ::Shown , " " ) ] ) ] , .. said ( " 好了 " , 1_100 , & [ ( " send_file " , " {} " , ToolStatus ::Done ) ] ) } ] ) ;
settled ( & mut b , TurnOutcome ::Success , 1_200 ) ;
let tasks = all ( std ::slice ::from_ref ( & b ) , & Live ::default ( ) , 9_000 ) ;
let text = describe ( & tasks , & | _ | " 小幫手 " . into ( ) , " Asia/Taipei " ) ;
assert! ( text . contains ( " 〔已交付〕小幫手:「畫一張圖」— 你開的 " ) , " {text} " ) ;
assert! ( text . contains ( " 證據 A 系統看到:系統看到 1 步做完 " ) , " {text} " ) ;
assert! ( text . contains ( " 產物 a.png: 已在iPhone顯示 " ) , " {text} " ) ;
assert_eq! ( attention ( & tasks , 9_000 ) , 0 ) ;
let v = json ( & tasks [ 0 ] ) ;
assert_eq! ( ( v [ " label " ] . as_str ( ) , v [ " status " ] . as_str ( ) , v [ " from " ] . as_str ( ) ) , ( Some ( " 已交付 " ) , Some ( " delivered " ) , Some ( " 你 " ) ) ) ;
}
#[ test ]
fn an_outsiders_turn_lists_only_what_that_sender_handed_over ( ) {
let mut b = bot ( vec! [
user ( " 我的私事 " , 1_000 ) ,
said ( " 好 " , 1_100 , & [ ( " web_fetch " , " {} " , ToolStatus ::Done ) ] ) ,
Message { untrusted_sender : true , .. user ( " 【小明】幫我查 " , 2_000 ) } ,
said ( " 查好了 " , 2_100 , & [ ] ) ,
Message { untrusted_sender : true , .. user ( " 【阿華】那件事好了沒 " , 3_000 ) } ,
] ) ;
settled ( & mut b , TurnOutcome ::Success , 2_200 ) ;
let mut other = bot ( vec! [ user ( " 另一個 Bot 的事 " , 500 ) , said ( " 好 " , 600 , & [ ] ) ] ) ;
other . id = 2 ;
let bots = vec! [ b , other ] ;
let tasks = all ( & bots , & Live ::default ( ) , 3_100 ) ;
// 阿華's turn: none of the owner's, none of 小明's — and no other bot's, even asked.
let text = answer ( & tasks , & bots , 1 , & json! ( { " all " : true } ) , " Asia/Taipei " ) ;
assert! ( ! text . contains ( " 我的私事 " ) & & ! text . contains ( " 幫我查 " ) & & ! text . contains ( " 另一個 Bot " ) , " {text} " ) ;
assert! ( text . contains ( " 那件事好了沒 " ) , " {text} " ) ;
// The owner's turn: this bot's, or every bot's with all.
let mut bots = bots ;
bots [ 0 ] . messages . push ( user ( " 現在在跑什麼 " , 4_000 ) ) ;
let tasks = all ( & bots , & Live ::default ( ) , 4_100 ) ;
let mine = answer ( & tasks , & bots , 1 , & json! ( { } ) , " Asia/Taipei " ) ;
assert! ( mine . contains ( " 我的私事 " ) & & ! mine . contains ( " 另一個 Bot " ) , " {mine} " ) ;
assert! ( answer ( & tasks , & bots , 1 , & json! ( { " all " : true } ) , " Asia/Taipei " ) . contains ( " 另一個 Bot 的事 " ) ) ;
}
#[ test ]
fn the_wakes_it_follows_are_the_ones_anybot_writes ( ) {
let f = crate ::followup ::FollowUp { id : 12 , what : " x " . into ( ) , .. Default ::default ( ) } ;
let w = crate ::followup ::wake_text ( & f , & crate ::followup ::Verdict ::Stuck , 0 ) . unwrap ( ) ;
assert_eq! ( continues ( & Message { wake : true , .. user ( & w , 1 ) } ) , Some ( Key ::Follow ( 12 ) ) ) ;
assert_eq! ( registered_follow ( & result ( " c " , crate ::followup ::TOOL , & crate ::followup ::started_text ( & f , " x " ) , 1 ) ) , Some ( 12 ) ) ;
assert_eq! ( continues ( & Message { wake : true , goal : " c " . into ( ) , .. user ( & format! ( " {} 還沒達成: x " , crate ::goal ::TAG ) , 1 ) } ) , Some ( Key ::Goal ( " c " . into ( ) ) ) ) ;
}
}