Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ each. This copy makes no version tags yet, so the list is one section.
- `bin/run.sh` and `herdr-projects update` look for releases in this repo only: upstream's program is never downloaded,
and a build older than its source runs with a line saying so.
([#21](https://github.com/jbutton/herdr-projects-legacy/pull/21))
- The board never types into a pane whose agent's program has ended: a task's text no longer goes into the shell left
behind (where it ran as commands). Under `usage.extra defer`, a start whose usage window ends it waits for that window
and carries on by itself at the reset, instead of "Not started", and a window known to be used up holds the start back.
([#30](https://github.com/jbutton/herdr-projects-legacy/pull/30))
8 changes: 8 additions & 0 deletions crates/core/src/herdr/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ impl Herdr {
/// command is at work, in the program's group (on the terminal, a file or `/dev/null`) or in a group of its own
/// (what, in words: busy.rs).
pub fn pane_childless(&self, id: &str, agent: &str) -> bool { super::busy::beside_agent(self, id, agent, false).is_empty() }
/// Is the pane's shell back in its foreground, with nothing else there: the program that ran in it has ended? herdr
/// may still name the agent for a moment after its program exited. Unknown (herdr does not say): no.
pub fn shell_in_front(&self, id: &str) -> bool {
let Some(i) = self.process_info(id) else { return false };
if !crate::gate::is_int(i.at("shell_pid")) { return false; }
let sp = i.i("shell_pid");
i.i("foreground_process_group_id") == sp && i.arr("foreground_processes").iter().all(|p| p.i("pid") == sp)
}
/// A pane, with whatever runs in it, into a new tab of another space. Never focused.
pub fn pane_move(&self, id: &str, workspace: &str, label: &str) -> HRes<Value> {
let mut a = args(&["pane", "move", id, "--new-tab", "--workspace", workspace]);
Expand Down
2 changes: 1 addition & 1 deletion crates/flow/src/actions/talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn tell_as(id: &str, text: &str, keep_intent: bool, chat: bool) -> Res<Said>
flow::sidebar::tell_sidebar(&t);
let how = flow::send::deliver(&t, &words, None, false)?;
let goes_on = flow::chat::during(&t) && ["reviewing", "reviewneeds"].contains(&t.s("stage"));
let msg = if how == "queued" { "queued: open the agent and answer its dialog first" } else if back { "The plan went back to its writer with your words" } else if goes_on { "Sent to the agent: the review goes on unless the code changes" } else { "Sent to the agent" };
let msg = if how == "gone" { "Not sent: the agent's program had ended. Retry starts it again with your words" } else if how == "queued" { "queued: open the agent and answer its dialog first" } else if back { "The plan went back to its writer with your words" } else if goes_on { "Sent to the agent: the review goes on unless the code changes" } else { "Sent to the agent" };
Ok(say(get(&cid).unwrap_or(t), msg))
}
/// A task that just ended is being cleaned up: a reply waits for that, or its work would go with the worktree.
Expand Down
2 changes: 1 addition & 1 deletion crates/flow/src/attempts/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn compare(id: &str, g: &mut Guard, t: &Task) -> Res<Obj> {
let at_rest = a.as_ref().map(|a| a.s("agent_status") == "idle" || a.s("agent_status") == "done").unwrap_or(false);
if !sent && at_rest {
comp_at_send = a.as_ref().map(|a| a.at("completion_seq").clone()).unwrap_or(Value::Null);
match crate::submit::hand(&api, &pane, &brief) { Ok(_) => sent = true, Err(e) => if e.code != "agent_blocked" && e.code != "agent_not_ready" { sent = true; } }
match crate::submit::hand(&api, &pane, &brief) { Ok(_) => sent = true, Err(e) => if !["agent_blocked", "agent_not_ready", crate::submit::gone::GONE].contains(&e.code.as_str()) { sent = true; } }
} else if sent && !reads {
// A program whose conversation the board does not read: herdr saw it finish a turn since the brief, and
// still no answer 15 seconds later.
Expand Down
2 changes: 1 addition & 1 deletion crates/flow/src/finish/review_once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn review_once(id: &str, g: &mut Guard, text: &str, change: Option<&Value>,
else if !blocked && cur.s("stage") == "reviewneeds" { g.set(|t| { t.put("stage", "reviewing"); t.put("msg", "Under review"); })?; g.allow(&["reviewneeds"]); }
let at_rest = a.as_ref().map(|a| a.s("agent_status") == "idle" || a.s("agent_status") == "done").unwrap_or(false);
if !sent && at_rest {
match crate::submit::hand(&api, &pane, &brief) { Ok(m) => { crate::store::said_by::note_board(id, &brief); comp_at_send = a.as_ref().map(|a| a.at("completion_seq").clone()).unwrap_or(Value::Null); *mark_cell.borrow_mut() = m; sent = true; sent_at = now(); } Err(e) => { if e.code != "agent_blocked" && e.code != "agent_not_ready" { sent = true; sent_at = now(); } } }
match crate::submit::hand(&api, &pane, &brief) { Ok(m) => { crate::store::said_by::note_board(id, &brief); comp_at_send = a.as_ref().map(|a| a.at("completion_seq").clone()).unwrap_or(Value::Null); *mark_cell.borrow_mut() = m; sent = true; sent_at = now(); } Err(e) => { if !["agent_blocked", "agent_not_ready", crate::submit::gone::GONE].contains(&e.code.as_str()) { sent = true; sent_at = now(); } } }
} else if mark_cell.borrow().is_some() && a.is_some() {
// The brief was typed: did the reviewer start on it? If not, enter is pressed again; never typed twice.
let mut m = mark_cell.borrow().clone().unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/flow/src/flow/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use crate::submit::{hand, note_sent};
use crate::util::shq;
use std::path::PathBuf;

/// "sent": typed, with enter pressed (whether the agent then started on it is looked at again: submit.rs); "queued".
/// "sent": typed, with enter pressed (whether the agent then started on it is looked at again: submit.rs); "queued";
/// "gone": its program had ended, nothing was typed, and the task waits for its usage window or failed (submit/gone.rs).
/// `first`: the text is all a freshly started program is given.
pub fn deliver(t: &Task, text: &str, wake: Option<Value>, first: bool) -> Res<&'static str> {
match hand(&h(t), t.at("link").s("pane"), text) {
Expand All @@ -27,6 +28,7 @@ pub fn deliver(t: &Task, text: &str, wake: Option<Value>, first: bool) -> Res<&'
queue_text(t.s("id"), text, extra)?;
Ok("queued")
}
Err(e) if e.code == crate::submit::gone::GONE => { crate::submit::gone::before_typed(t, text, first, &h(t)); Ok("gone") }
Err(e) => Err(e.into()),
}
}
Expand Down
12 changes: 11 additions & 1 deletion crates/flow/src/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! foreground process, which on a loaded machine is seconds before it has drawn anything; `agent prompt` pastes the
//! text and presses enter 300 ms later, and a program still starting keeps the text and drops the enter.
pub mod give_up;
pub mod gone;
pub mod unconfirmed;
use crate::agents::hand_limits::hand_over;
use crate::agents::installed::said_to;
Expand Down Expand Up @@ -42,6 +43,7 @@ pub fn settle(api: &Herdr, pane: &str, launch: &str) -> bool {
let until = now() + max;
let (mut prev, mut since) = (None::<String>, 0);
loop {
if api.shell_in_front(pane) { return false; } // its program has ended: nothing is to be typed into a shell
let Ok(screen) = api.pane_read(pane, "visible", None) else { return false };
if drawn(&screen, launch) { if since == 0 { since = now(); } if prev.as_deref() == Some(screen.as_str()) || now() - since >= QUIET_MAX { return true; } } else { since = 0; }
prev = Some(screen);
Expand Down Expand Up @@ -87,9 +89,13 @@ pub fn pending_sent(t: &Task, a: Option<&Value>) -> bool {
/// Type a prompt into an agent. Returns the mark to look again with (None when herdr said nothing of the agent before).
/// Fails with what herdr answers when nothing was typed (agent_blocked, agent_not_ready, anything unexpected).
/// A text too long to hand over in one go is written to a file, and a line naming it is typed instead (hand_limits.rs).
/// Fails with `agent_gone` and types nothing when the agent's program is not running in the pane: herdr names no agent
/// there, or the pane's shell is back in front. Text typed into a shell is run as commands.
pub fn hand(api: &Herdr, pane: &str, text: &str) -> Result<Option<Value>, HerdrError> {
let got = api.agent_get(pane);
if matches!(got, Ok(None)) || api.shell_in_front(pane) { return Err(gone::error()); }
let typed = hand_over(text).map_err(|e| HerdrError { code: "failed".into(), message: e.msg })?;
let a = api.agent_get(pane).ok().flatten();
let a = got.ok().flatten();
if let Err(e) = api.agent_prompt(pane, &typed) { if !["agent_prompt_stalled", "timeout"].contains(&e.code.as_str()) { return Err(e); } } // these do not prove nothing was sent; never send twice
Ok(mark_of(a.as_ref()))
}
Expand All @@ -112,6 +118,7 @@ pub fn look(api: &Herdr, pane: &str, mark: &mut Value, said: Option<&dyn Fn() ->
let proof = said.and_then(|f| f());
if took(mark, a.as_ref(), proof) == Some(true) { return "took"; }
if mark.i("tries") >= TRIES { return "lost"; }
if api.shell_in_front(pane) { return "lost"; } // its program has ended: no enter into a shell
if let Some(m) = mark.as_object_mut() { let n = m.i("tries") + 1; m.put("tries", n); m.put("pressedAt", now()); }
let _ = api.agent_keys(pane, &["enter"]);
"pressed"
Expand Down Expand Up @@ -165,8 +172,11 @@ pub fn check_sent(id: &str, a: Option<&Value>, api: &Herdr) {
let lost = |detail: String, next: String| give_up::fail(id, "sent", &m, &detail, &next, &give_up::screen(api, t.at("link").s("pane"), a.map(|a| a.s("agent_status"))));
let secs = (((now() - m.i("at")) as f64 / 1000.0).round() as i64).max(1);
if a.is_none() && marker_alive(t.at("runner")) { return; } // a job of the board's own is starting its program again
// herdr may name the agent for a moment after its program exited: the pane's shell in front says it is gone.
let a = a.filter(|_| !api.shell_in_front(t.at("link").s("pane")));
let Some(a) = a else {
if said() == Some(true) { return clear(); }
if let Some(x) = gone::window_used(id) { gone::keep_typed(id, &m); if gone::wait(id, x) { return; } } // under `defer`: it waits for the reset
return lost(format!("The board typed the text into the agent's box {secs} seconds ago. The agent's program ended before it started on it: nothing was submitted."), format!("Retry ({}) starts the agent's program again and hands it the text once more.", key_name("yes")));
};
if m.b("busy") { // typed into a running turn: looked at from the moment the agent is at rest
Expand Down
72 changes: 72 additions & 0 deletions crates/flow/src/submit/gone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//! A task's agent whose program is not running where the board was about to type, or had just typed: nothing more goes
//! into its pane (a shell runs what is typed into it). Under `usage.extra defer`, with its program's window used up or
//! on extra usage, the task waits for that window as one stopped mid-run does (usage/stop.rs) and carries on by itself
//! at the reset; else it fails as not started. Either way its text is kept, for the start that follows.
use super::give_up::screen;
use crate::agents::read_window::codex_newest;
use crate::board::keys::key_name;
use crate::clock::now;
use crate::finish::shared::last_lines;
use crate::herdr::{cli::HerdrError, Herdr};
use crate::json::*;
use crate::limits::reached::{stop_at, StopHow};
use crate::store::failures::failure_patch;
use crate::store::tasks::{get, update_wait, Change, Task};
use crate::store::usage::{fold, now_of, READ};
use crate::usage::words::setting_of;

/// herdr's answer for it, as `hand` gives it: nothing was typed.
pub const GONE: &str = "agent_gone";
pub const ENDED: &str = "Not started: its program ended before it was given its instructions";
pub fn error() -> HerdrError { HerdrError { code: GONE.into(), message: "the agent's program is not running in its pane: nothing was typed".into() } }

/// Under `defer`, is its program's window used up or on extra usage, as read afresh now? The stop's own marks, if so.
pub fn window_used(id: &str) -> Option<Obj> {
let t = get(id)?;
let kind = t.t("agent");
if setting_of(&t) != "defer" || crate::chat_only::is_chat(&t) || !READ.contains(&kind.as_str()) { return None; }
let at = now();
let fresh = if kind == "codex" { codex_newest() } else { crate::usage::tick::reading_of(&t, &kind, "agent") };
if let Some(r) = fresh { fold(&kind, &r, at); }
let n = now_of(&kind, at);
if !(n.extra || n.hard) || n.resets <= at { return None; }
Some(obj(json!({ "how": "defer", "resets": n.resets, "window": n.since, "hard": n.hard })))
}
/// The task stopped to wait for its program's window, as one stopped mid-run is: it carries on by itself at the reset.
pub fn wait(id: &str, extra: Obj) -> bool {
let Some(kind) = get(id).map(|t| t.t("agent")) else { return false };
stop_at(id, "usage", &kind, now() as f64, StopHow { role: "agent".into(), extra, ..Default::default() }).is_some()
}
/// Text typed into the agent's box that it never started on (`m`, its mark): kept for the start that follows.
pub fn keep_typed(id: &str, m: &Value) {
let _ = update_wait(id, 10000, |c| {
if c.at("sent").at("at") != m.at("at") { return Ok(Change::Keep); }
c.put("unsent", json!({ "text": m.at("text"), "first": m.b("first"), "at": if m.b("typedAt") { m.at("typedAt").clone() } else { m.at("at").clone() }, "typed": true }));
c.put("sent", Value::Null);
Ok(Change::Write)
});
}

/// Nothing was typed: its program had ended. The text is kept as not typed, before what waited unsent already, and the
/// task waits for its usage window or fails as not started.
pub fn before_typed(t: &Task, text: &str, first: bool, api: &Herdr) {
let id = t.t("id");
let shown = screen(api, t.at("link").s("pane"), None);
let _ = update_wait(&id, 10000, |c| {
let old = c.at("unsent").clone();
let all = if old.is_object() && !old.s("text").is_empty() { format!("{}\n\n{text}", old.s("text")) } else { text.to_string() };
c.put("unsent", json!({ "text": all, "first": first || old.b("first"), "at": now() }));
c.put("sent", Value::Null);
Ok(Change::Write)
});
if let Some(x) = window_used(&id) { if wait(&id, x) { return; } }
let tail = last_lines(&shown, 8);
let detail = format!("The board was about to type the text into the agent's box, but its program had already ended: nothing was typed into its pane.{}", if tail.is_empty() { String::new() } else { format!("\n\nThe last lines of its terminal:\n{tail}") });
let next = format!("Retry ({}) starts the agent's program again and hands it the text.", key_name("yes"));
let _ = update_wait(&id, 10000, |c| {
if c.s("stage") != "working" { return Ok(Change::Keep); }
c.put("stage", "failed"); c.put("msg", ENDED); c.put("turnOpen", false); c.put("watch", Value::Null);
for (k, v) in failure_patch(&obj(json!({ "stage": "send", "title": "The agent's program ended as it started", "auth": false, "agentCanFix": false, "detail": detail, "next": next }))) { c.insert(k, v); }
Ok(Change::Write)
});
}
10 changes: 8 additions & 2 deletions crates/flow/src/usage/gate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Before a start (actions/start.rs: start_queued): a queued task whose agent's program is known to be on extra usage
//! does not start under `ask` or `defer`. Under `defer` the first agent on `worker.fallback` that is not on extra usage
//! starts instead (a hand-over before it began); else it stays in the queue, holding no slot, as "waiting for the usage
//! window: resets 17:00", and starts by itself once that has passed. Under `ask`, or with its reset not known, it waits
//! window: resets 17:00", and starts by itself once that has passed. Under `defer` a window used up (`hard`) holds it
//! back the same way. Under `ask`, or with its reset not known, it waits
//! for you (`usageWait.you`), and a retry starts it anyway, with leave for this episode (`extraOk`).
use super::stop::{fallback_for, why};
use super::words::{setting_of, start_line};
Expand Down Expand Up @@ -30,8 +31,13 @@ pub fn held(w: &Obj) -> bool {
let (id, at) = (w.t("id"), now());
let kind = kind_of(w);
let s = setting_of(w);
// Codex's window read afresh before a start, not only once a minute (usage/tick.rs): a start must not be the first to find it used up.
if kind == "codex" && s == "defer" && crate::store::marks::due("usage-codex-start", 5_000) { if let Some(r) = crate::agents::read_window::codex_newest() { crate::store::usage::fold("codex", &r, at); } }
let n = now_of(&kind, at);
let hold = s != "allow" && READ.contains(&kind.as_str()) && n.extra && !extra_ok(w, &kind, &n, at) && !crate::chat_only::is_chat(w);
// Under `defer` a window used up with nothing to pay on (`hard`) holds it back as extra usage does: started, its
// program would end at once.
let over = (n.extra && !extra_ok(w, &kind, &n, at)) || (s == "defer" && n.hard && n.resets > at);
let hold = s != "allow" && READ.contains(&kind.as_str()) && over && !crate::chat_only::is_chat(w);
if !hold {
if w.b("usageWait") { let _ = update(&id, |t| { t.del("usageWait"); t.put("msg", ""); Ok(Change::Write) }); }
return false;
Expand Down
9 changes: 6 additions & 3 deletions docs/extra-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ Today's behaviour: the agent works on, and the board only says so (above).
- **Nobody to take it: the agent is stopped at once**, as under `ask`, its work kept. The task waits as `waiting for
the usage window: resets 17:00`, with the queued tasks, not under "needs you". **At the reset it carries on by
itself**: the agent is told it was stopped for extra usage and that the window has reset.
- **A task does not start** while its program is on extra usage: started with an agent on the list that is not, else it
stays in the queue as `waiting for the usage window: resets 17:00`, holding no slot, and starts by itself once the
window has reset.
- **A task does not start** while its program is on extra usage, or its window is used up with nothing to pay on:
started with an agent on the list that is not, else it stays in the queue as `waiting for the usage window: resets
17:00`, holding no slot, and starts by itself once the window has reset. Codex's window is read afresh before a start.
- **A start its window ends** (the program comes up, finds its window used up and ends, before the board knew): it waits
for that window as a task stopped at work does, `waiting for the usage window: resets 17:00`, and carries on by
itself at the reset with its own text. Nothing is typed into the pane ([never into a shell](handing-over.md#never-into-a-shell)).
- **A turn that ends out of allowance** (extra usage off or used up), with nobody on the list to take it: it waits for
that window too, instead of "Agent stopped: out of allowance until 17:00", and carries on by itself at the reset.
- **When the reset is not known**, it waits for you instead, under "needs you", with the reason.
Expand Down
Loading
Loading