agentmux_srv_core/backend/supervisor/
judge.rs1use crate::ambient::call::{self, CliTarget};
9use crate::ambient::purpose;
10use crate::ambient::reply::{judge_line, FORMAT_RULES};
11use crate::backend::obj::Block;
12use crate::backend::storage::store::Store;
13
14use super::{SupervisorMode, Verdict};
15
16pub const TEXT_MAX_CHARS: usize = 300;
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct Judgement {
22 pub verdict: Verdict,
23 pub reason: String,
24 pub next: String,
26}
27
28const ROLE: &str = "You supervise an AI coding agent that has just ended its turn. Read the end of its \
29conversation and decide whether it should keep working on its own or stop for its user.";
30
31const STEADY_RULES: &str = "Mode: steady. Say continue only when the agent stopped to check in (\"shall I \
32continue?\", \"let me know if you want me to…\") or stopped short of work it had already planned or been asked \
33to do, and nothing in what is left needs the user. Say needs_user when it asks a real question, offers a choice \
34only the user can make, or faces an open question it cannot settle from the conversation.";
35
36const DRIVE_RULES: &str = "Mode: drive. The agent should push forward on its own best judgement. Say continue \
37when it stopped to check in, stopped short of its task, or stopped on an open question it can research or decide \
38itself; give the best next step. Say needs_user only for something only the user can provide: a credential, a \
39sign-in, a payment, or a product decision with no sensible default.";
40
41const COMMON_RULES: &str = "In every mode:\n\
42- done: the task is complete AND the conversation shows evidence (tests passing, a pull request opened or merged, \
43the result shown). A bare claim of being done is not evidence: in drive say continue and ask it to show the \
44evidence; in steady say needs_user.\n\
45- A turn that ends by asking the user to approve, choose or decide something is never done, even when the user \
46told the agent to ask: something is still waiting on an answer.\n\
47- needs_user: the next step is risky, irreversible or outward-facing (deleting data, force-pushing, deploying, \
48spending money, publishing, messaging people outside the team), even if the agent proposes it.\n\
49- blocked: the agent cannot proceed (a failing tool it cannot fix, missing access) and says so.\n\
50- When unsure between continue and needs_user, say needs_user.";
51
52pub fn build_prompt(mode: SupervisorMode, digest: &str, continuations: u32, limit: u32) -> String {
54 let rules = match mode {
55 SupervisorMode::Drive => DRIVE_RULES,
56 _ => STEADY_RULES,
57 };
58 let next = match mode {
59 SupervisorMode::Drive => "<next> is one sentence: the next step the agent should take, or - when the verdict is not continue.",
60 _ => "<next> is a single -.",
61 };
62 let instruction = format!(
63 "{ROLE}\n\n{rules}\n\n{COMMON_RULES}\n\nIt has been continued {continuations} times in a row (limit {limit}).\n\n\
64 The answer text is: <verdict> | <reason> | <next>\n\
65 <verdict> is one of continue, done, needs_user, blocked. <reason> is one sentence the user will read, \
66 about this agent's work. {next}\n{FORMAT_RULES}"
67 );
68 crate::ambient::prompt::with_material(&instruction, "agent_conversation", digest)
69}
70
71pub fn parse_answer(answer: &str, mode: SupervisorMode) -> Option<Judgement> {
74 let mut parts = answer.splitn(3, '|').map(str::trim);
75 let verdict = Verdict::parse(parts.next()?)?;
76 let reason = clip(parts.next().unwrap_or(""));
77 if reason.is_empty() {
78 return None;
79 }
80 let next = clip(parts.next().unwrap_or(""));
81 let next = if mode == SupervisorMode::Drive && verdict == Verdict::Continue && next != "-" { next } else { String::new() };
82 Some(Judgement { verdict, reason, next })
83}
84
85fn clip(text: &str) -> String {
87 let flat = text.split_whitespace().collect::<Vec<_>>().join(" ");
88 if flat.chars().count() <= TEXT_MAX_CHARS {
89 return flat;
90 }
91 let mut out: String = flat.chars().take(TEXT_MAX_CHARS - 1).collect();
92 out.push('…');
93 out
94}
95
96#[derive(Debug)]
98pub enum Outcome {
99 Judged(Judgement),
100 Failed,
102 NoVerdict,
104 Superseded,
106}
107
108pub async fn judge(
110 mstore: &Store,
111 block_id: &str,
112 generation: u64,
113 mode: SupervisorMode,
114 digest: &str,
115 continuations: u32,
116) -> Outcome {
117 let Some(mut slot) = call::admit(&purpose::SUPERVISOR_VERDICT, block_id, generation).await else {
118 return Outcome::Superseded;
119 };
120 if !super::settings::enabled() {
123 slot.abandon(crate::ambient::outcome::Outcome::Gated);
124 return Outcome::NoVerdict;
125 }
126 let Some(target) = mstore.get::<Block>(block_id).ok().flatten().and_then(|b| CliTarget::from_meta(&b.meta)) else {
127 slot.abandon(crate::ambient::outcome::Outcome::Gated);
128 return Outcome::NoVerdict;
129 };
130 let prompt = build_prompt(mode, digest, continuations, mode.limits().continuations);
131 let reply = slot
132 .run_held(&target, &prompt, |raw| {
133 judge_line(raw, |answer| parse_answer(answer, mode).map(|_| answer.to_string()))
134 })
135 .await;
136 if slot.is_superseded() {
137 return Outcome::Superseded;
138 }
139 if reply.error.is_some() {
140 return Outcome::Failed;
141 }
142 match parse_answer(&reply.text, mode) {
143 Some(j) => Outcome::Judged(j),
144 None => Outcome::NoVerdict,
145 }
146}