//! A goal run's side: the briefing and continuation prompts, `goal_reached`, //! `need_help` and the report file. use super::{PathBuf, State, StopReason}; /// Append the stop reason to the session's own report file, so the artifact /// a parent already reads is where the run's ending is recorded too — rather /// than the parent having to correlate a todo against a file. /// /// Best-effort and appended, never rewritten: the subagent wrote that file, /// and this adds a line under what it wrote instead of taking a position on /// the rest of it. A path this daemon can't write to is logged and dropped — /// the todo still carries the same sentence, so nothing is only here. pub(super) async fn write_stop_to_report(path: Option, name: &str, stop: &StopReason) { let Some(path) = path else { return }; let line = format!("\n**Subagent `{name}` stopped:** {}\n", stop.sentence()); let appended = tokio::fs::OpenOptions::new() .create(true) .append(true) .open(&path) .await; let result = match appended { Ok(mut file) => { use tokio::io::AsyncWriteExt as _; // `flush`, not just `write_all`: a `tokio::fs::File` buffers, and // dropping one discards whatever hasn't been handed to the // blocking pool — so without this the line is written to nothing // and the failure is silent. A unit test caught exactly that. match file.write_all(line.as_bytes()).await { Ok(()) => file.flush().await, Err(e) => Err(e), } } Err(e) => Err(e), }; if let Err(e) = result { tracing::warn!( name, path = %path.display(), error = ?e, "subagent: could not record the stop reason in the session's report file", ); } } /// The goal contract appended to a `start`'s first prompt when a goal was /// given. Spelled out to the subagent rather than left implicit: it is about /// to be re-prompted by something it can't see, and the two tools that stop /// that are the only way it has to say "done" or "stuck". pub(super) fn goal_briefing(name: &str, goal: &str, max_turns: u32) -> String { format!( "\n\nYou are the subagent session `{name}`.\n\nYour goal for this session: {goal}\n\nYou \ have up to {max_turns} turns to reach it. When a turn of yours ends and you haven't \ reported the goal reached, the harness starts another turn re-prompting you toward it. \ Call the `goal_reached` tool once you've genuinely reached it, or `need_help` with what \ is blocking you if you can't proceed — either one stops the re-prompting. Neither takes \ a session name: the endpoint you call them on is yours alone, so they always apply to \ this session and can't be aimed at another one. Running out of turns stops it too, with \ the work left wherever it had got to." ) } /// The prompt a continuation turn opens with. It states the one fact the /// subagent can't observe for itself — that its last turn ended without the /// goal being reported reached — and says plainly that claiming the goal /// isn't the same as reaching it, since being re-prompted is precisely the /// pressure to claim it. pub(super) fn continuation_prompt(name: &str, goal: &str, turn: u32, max_turns: u32) -> String { format!( "Your previous turn ended and you have not reported the goal reached.\n\nYou are the \ subagent session `{name}`.\n\nGoal: {goal}\n\nThis is turn {turn} of {max_turns}. Carry \ on toward the goal. If you have in fact reached it, call `goal_reached`; if you are \ blocked, call `need_help` with what is blocking you. Neither is a substitute for \ the work: whoever spawned you reads what you actually changed, not what you claim about \ it." ) } /// Record the subagent's own "I have reached the goal" signal and stop its /// turn continuation. Called by the subagent, from inside its own turn, over /// the signal route this daemon hands it (see `build_config`). /// /// **`name` is not a parameter of the tool.** It is whatever the route's /// token resolved to (`State::session_for_signal_token`), so a subagent /// records a stop against its own session because that is the only session /// its URL can reach — not because it addressed the right one. /// /// **This verifies nothing**, and the answer it returns says so to the /// subagent's face. It stops the loop and extends the done message; whether /// the goal was actually reached is a question about the diff and the gate /// output, which the parent reads for itself. /// /// `report_file` is the subagent saying where it wrote its report, which is /// the only reason this daemon ever knows that path — see /// `State::set_report_file`. pub fn goal_reached( state: &State, name: &str, msg: Option, report_file: Option<&str>, ) -> String { signal_stop(state, name, StopReason::GoalReached(msg), report_file); format!( "noted — `{name}`'s goal is recorded as reported reached, so this turn finishes and no \ further goal turn is started. It is recorded as your claim, not as verification: whoever \ spawned you still reads what you changed." ) } /// Record that the subagent can't proceed, and stop its turn continuation. /// The blocking signal the run has otherwise no way to raise: without it a /// stuck subagent would be re-prompted toward a goal it has already told /// nobody it can't reach, until the turn cap. /// /// `msg` is required, unlike `goal_reached`'s — "I'm stuck" with no reason /// gives the parent nothing to act on, and acting on it is the entire point. /// /// `name` comes from the route's token, exactly as in [`goal_reached`]. pub fn need_help(state: &State, name: &str, msg: String, report_file: Option<&str>) -> String { signal_stop(state, name, StopReason::NeedHelp(msg), report_file); format!( "noted — `{name}` is recorded as blocked, so this turn finishes and no further goal turn \ is started. Write down what you have done so far where your brief told you to; whoever \ spawned you sees the block in `status` and in this run's todo." ) } /// The half [`goal_reached`] and [`need_help`] share: remember where the /// subagent says it wrote, and record the stop. /// /// It checks nothing, and has nothing left to check. `name` reached it by /// being resolved from the route's own token, so "is this signal about the /// session it claims" is answered before the request is dispatched at all — /// a token that names no live session never reaches this function, it gets a /// 404. What used to stand here was an `occupancy()` liveness check standing /// in for identity, which two concurrently-running siblings could each /// satisfy for the other's name. fn signal_stop(state: &State, name: &str, stop: StopReason, report_file: Option<&str>) { state.set_report_file(name, report_file); state.record_stop(name, stop); }