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: 2 additions & 2 deletions crates/board/src/board/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Data {
pub coords: Vec<String>,
/// This machine's coordinators' autopilot as each shows it, by level ("" the main one, "machine", "project:<repo>").
pub pilots: Value,
pub keys: Keys, pub repos: Vec<String>, pub roots: BTreeMap<String, String>, pub session: String, pub notify: String,
pub keys: Keys, pub repos: Vec<String>, pub roots: BTreeMap<String, String>, pub session: String, pub notify: String, pub usage: String,
/// The known repos that keep their heading by repo with no task in view (`used_lately`).
pub lately: Vec<String>,
/// This machine's archived projects (store/archived_repos.rs): off the board, each a heading in the archive.
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn load(wants: &Wants) -> Data {
let skills = crate::board::skills::read(&wants.skills, &socket, &coord, sub.as_ref());
Data {
items, autos, jobs, coord, coord_screen, sub, looks, coords, can_dispatch: crate::coordinator::who::ready(&socket), keys: repo_keys(), repos: roots.keys().cloned().collect(), roots, lately, archived, pins: crate::store::pins::pins(),
session: session_name(&socket), notify: setting("board.notify", None).as_str().unwrap_or("").to_string(),
session: session_name(&socket), notify: setting("board.notify", None).as_str().unwrap_or("").to_string(), usage: crate::usage::words::header(now),
repo: here.0, repo_root: here.1, host: crate::remote::check::hostname(),
// herdr's approve action and a line left by the board key are for a board in a terminal, never the web board.
approve: if wants.web { String::new() } else { crate::actions::finish_line::take_approve(&socket, &wants.own_pane) }, heard: if wants.web { None } else { crate::board::run::say::heard(&wants.own_pane, &socket) }, call: crate::voice::call::get(), call_ended: crate::voice::call::last().filter(|c| c.s("state") == "ended").map(|c| (c.t("id"), c.t("why"))),
Expand Down
2 changes: 1 addition & 1 deletion crates/board/src/board/update/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub fn yes(s: &mut State, r: &Obj, fx: &mut Vec<Effect>) {
pub fn send_back(s: &mut State, r: &Obj, fx: &mut Vec<Effect>) {
if r.b("archivedAt") { return s.flash(read_only(), Ink::Yellow); }
let id = r.t("id");
if r.s("stage") == "stopped" && r.b("limitStop") { return act(s, r, "retry", json!({}), Job::CarryOn { id }, fx); }
if crate::limits::run::carries_on(r) { return act(s, r, "retry", json!({}), Job::CarryOn { id }, fx); }
let failure = failure_for(r);
if let Some(f) = &failure { if !f.b("agentCanFix") && !(r.s("stage") == "failed" && r.at("failure").b("auth")) { return s.flash(format!("This needs you first. {}", f.s("next")), Ink::Yellow); } }
let review = r.at("review");
Expand Down
1 change: 1 addition & 0 deletions crates/board/src/board/view/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub fn hints(s: &State, r: Option<&Obj>, who: Option<&Who>) -> (Vec<Hint>, Optio
else if st("approval") && r.b("syncBlocked") { vec![Some(reply.clone()), Some(open.clone())] }
else if st("approval") && r.s("state") == "blocked" { vec![opt(failure.as_ref().map(|f| f.b("agentCanFix") && !f.b("sentAt")).unwrap_or(false), fix.clone()), opt(!r.b("codeReview"), hk(K::Yes, "retry the merge")), Some(reply.clone())] }
else if st("stopped") && r.b("limitStop") { let rev = r.at("limitStop").s("role") == "reviewer"; vec![Some(hk(K::SendBack, if rev { "carry on with this task" } else { "tell this task's agent to continue" })), opt(!rev, hk(K::Yes, "use its work as it is")), Some(reply.clone())] }
else if crate::usage::gate::start_waits(r) { vec![Some(hk(K::SendBack, "start it now, on extra usage")), Some(reply.clone()), Some(stop.clone())] }
else if st("nosummary") { vec![Some(hk(K::SendBack, "tell this task's agent to continue")), Some(hk(K::Yes, "use its work as it is")), Some(open.clone())] }
else if failure.as_ref().map(|f| f.b("agentCanFix")).unwrap_or(false) { if failure.as_ref().unwrap().b("sentAt") && r.s("state") == "working" { vec![Some(open.clone()), Some(reply.clone()), Some(stop.clone())] } else { vec![Some(fix.clone()), opt(st("failed"), as_is.clone()), Some(reply.clone())] } }
else if failure.is_some() { vec![opt(st("failed") || r.s("kind") == "auth", retry.clone()), Some(reply.clone()), Some(open.clone())] }
Expand Down
1 change: 1 addition & 0 deletions crates/board/src/board/view/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn head_lines(s: &mut State, t: &Theme, w: usize, h: usize) -> Vec<Inked> {
if s.scope.is_empty() || s.scope == "*" || s.archive_view { rest.push(where_); }
rest.extend(tally);
if !s.data.session.is_empty() && s.data.session != "default" { rest.push(format!("session {}", s.data.session)); }
if !s.data.usage.is_empty() && !s.archive_view { rest.push(s.data.usage.clone()); } // each program's usage window (usage/words.rs)
let rest = rest.join(" · ");
// The board's name gives way before whose tasks these are; names too long for the line go on to the next, whole.
let name = Inked::plain(" ").add(Inked::plain("herdr-projects").bold()).add(Inked::plain(" "));
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/agents/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ pub fn hook_args(kind: &str, h: &Hook) -> Res<Option<Vec<String>>> {
let command = hook_command(h).iter().map(|s| shq(s)).collect::<Vec<_>>().join(" ");
let file = h.dir.join(format!("hook-{}.json", h.role));
let one = json!([{ "hooks": [{ "type": "command", "command": command, "async": true }] }]);
write_private(&file, &json!({ "hooks": { "Stop": one, "StopFailure": one }, "idleCompaction": false }).to_string())?;
// Its status line is the board's, which keeps where the agent stands in its usage window and then shows
// yours (statusline.rs).
write_private(&file, &json!({ "hooks": { "Stop": one, "StopFailure": one }, "idleCompaction": false, "statusLine": super::statusline::setting(h) }).to_string())?;
Ok(Some(vec!["--settings".into(), file.display().to_string()]))
}
// Codex runs one `notify` program at each turn end. Given at launch it would replace one the user configured,
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/agents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ pub mod provider_error;
pub mod read_kept;
pub mod read_tokens;
pub mod read_turn;
pub mod read_window;
pub mod roles;
pub mod scratch;
pub mod screen_text;
pub mod sign_in;
pub mod skills;
pub mod start;
pub mod start_failed;
pub mod statusline;
pub mod talk;
pub mod talk_commands;
12 changes: 7 additions & 5 deletions crates/core/src/agents/read_turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ pub struct Turn {
pub finished: bool, pub pending: bool, pub interrupted: bool, pub usage: Option<Usage>, pub model: String,
pub auth: bool, pub passing: bool, pub reason: String,
#[serde(skip_serializing_if = "Option::is_none")] pub file: Option<PathBuf>,
/// Its program's usage window, where its lines say it (read_window.rs).
#[serde(skip_serializing_if = "Option::is_none")] pub window: Option<super::read_window::Reading>,
}
/// The provider's side of an error, decided once the turn is read.
fn judge(mut t: Turn, code: &str, status: i64, auth_code: bool) -> Turn {
Expand Down Expand Up @@ -234,13 +236,13 @@ pub fn pi_turn(rows: &[Value]) -> Option<Turn> {
_ => { let done = stop == "stop" || stop == "length"; Turn { said: text, stop: if stop == "length" { "length".into() } else if done { "stop".into() } else { String::new() }, finished: done, pending: !done, ..base } }
})
}
/// The end of a conversation, by this program's reader.
pub fn read_turn(kind: &str, file: &Path, bytes: u64) -> Option<Turn> {
match kind { "claude" => read_claude(file, bytes), "codex" => read_codex(file, bytes), "pi" => read_pi(file, bytes), _ => None }
}
/// The end of a conversation, by this program's reader, with its usage window where its lines say it.
pub fn read_turn(kind: &str, file: &Path, bytes: u64) -> Option<Turn> { turn_in(kind, &tail_lines(file, bytes).ok()?) }
/// The same, of its lines as read (`tail_lines`): what a reader of the file's end kept between reads judges.
pub fn turn_in(kind: &str, rows: &[Value]) -> Option<Turn> {
match kind { "claude" => claude_turn(rows), "codex" => codex_turn(rows), "pi" => pi_turn(rows), _ => None }
let mut t = match kind { "claude" => claude_turn(rows), "codex" => codex_turn(rows), "pi" => pi_turn(rows), _ => None }?;
t.window = super::read_window::window_in(kind, rows);
Some(t)
}
thread_local! { static LISTED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; }
/// A folder's entries, looking for a saved conversation. Each one is counted: what a search costs is how many folders
Expand Down
124 changes: 124 additions & 0 deletions crates/core/src/agents/read_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//! A reading of an agent program's usage window, from what it saved (docs/extra-usage.md): Codex's last `token_count`
//! (its `rate_limits`: the 5-hour and weekly windows and its credits), Claude Code's status line input as the board's
//! own status line kept it (agents/statusline.rs), and a request Claude Code saw refused (`quotaLimits`, its hard stop).
//! On extra usage: a window at 100% or more while answers are still served. Read from files only: no model call.
use super::read_turn::{tail_lines, when};
use crate::json::*;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// One window: its name (`5h`, `week`), how much of it is used (percent) and when it resets (ms; 0: not known).
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Window { pub name: String, pub pct: f64, pub resets: i64 }
/// `at`: when it was read (ms). `extra`: on extra usage. `hard`: refused, the window used up with nothing to pay on.
/// `resets`: when the full windows reset (ms; the later one when both are full; 0: not known).
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Reading { pub at: i64, pub windows: Vec<Window>, pub extra: bool, pub hard: bool, pub resets: i64 }
/// A window used up: at or past this percent.
pub const FULL: f64 = 100.0;

impl Reading {
/// Any window used up.
pub fn full(&self) -> bool { self.windows.iter().any(|w| w.pct >= FULL) }
/// When the full windows reset: the later one; 0 when one of them does not say.
fn full_resets(&self) -> i64 {
let full: Vec<&Window> = self.windows.iter().filter(|w| w.pct >= FULL).collect();
if full.is_empty() || full.iter().any(|w| w.resets <= 0) { 0 } else { full.iter().map(|w| w.resets).max().unwrap_or(0) }
}
/// As it stands at `now`: a window whose reset has passed is a fresh one and goes, and so does what it said.
pub fn at_now(&self, now: i64) -> Reading {
let mut r = self.clone();
r.windows.retain(|w| w.resets <= 0 || now < w.resets);
let over = self.resets > 0 && now >= self.resets;
r.extra = self.extra && r.full() && !over;
r.hard = self.hard && !over;
if over { r.resets = 0; }
r
}
}
fn name_of(minutes: i64) -> String {
match minutes { 300 => "5h".into(), 10080 => "week".into(), m if m > 0 && m % 1440 == 0 => format!("{}d", m / 1440), m if m > 0 && m % 60 == 0 => format!("{}h", m / 60), m => format!("{m}m") }
}
/// Seconds since 1970, as all three sources say a reset, in milliseconds (0: none).
fn ms_of(v: &Value) -> i64 { (number(v) * 1000.0) as i64 }
/// Do these words say the allowance is used up? (The board's own reading of a provider's words: store/handovers.rs.)
fn used_up(words: &str) -> bool { !words.is_empty() && crate::store::handovers::why_of(words, false, false, "", 0, 0).starts_with("out of allowance") }

/// Codex: its last `token_count` with `rate_limits`, and an error after it that says the allowance is used up.
pub fn codex_reading(rows: &[Value]) -> Option<Reading> {
let mut last: Option<Reading> = None;
for e in rows.iter().filter(|e| e.s("type") == "event_msg") {
let p = e.at("payload");
if p.s("type") == "token_count" && p.at("rate_limits").is_object() {
let r = p.at("rate_limits");
let windows = ["primary", "secondary"].iter().map(|k| r.at(k)).filter(|w| w.is_object())
.map(|w| Window { name: name_of(w.i("window_minutes")), pct: w.f("used_percent"), resets: ms_of(w.at("resets_at")) }).collect();
let pays = r.at("credits").b("has_credits") || r.at("credits").b("unlimited");
let mut x = Reading { at: when(e.at("timestamp")), windows, hard: r.b("rate_limit_reached_type"), ..Default::default() };
x.resets = x.full_resets();
x.extra = x.full() && pays && !x.hard;
last = Some(x);
continue;
}
let err = match p.s("type") {
"task_complete" if p.b("error") => { let x = p.at("error"); if x.is_object() { x.t("message") } else { text(x) } }
"error" | "stream_error" => if p.b("message") { p.t("message") } else { p.t("error") },
_ => continue,
};
if !used_up(&err) { continue; }
let mut x = last.take().unwrap_or_default();
x.at = when(e.at("timestamp")).max(x.at);
x.hard = true; x.extra = false;
last = Some(x);
}
last
}
/// Claude Code: a request it saw refused for the allowance (`quotaLimits`), when no answer was served after it.
pub fn claude_reading(rows: &[Value]) -> Option<Reading> {
let mut last: Option<Reading> = None;
for e in rows.iter().filter(|e| e.s("type") == "assistant") {
let q = if e.at("quotaLimits").is_object() { e.at("quotaLimits") } else { e.at("apiErrorParams").at("rate_limit_info") };
let refused = e.b("isApiErrorMessage") || e.at("message").s("model") == "<synthetic>";
if refused && q.is_object() && (e.s("apiError") == "usage_limit_reached" || q.s("status") == "rejected") {
let resets = ms_of(q.at("resetsAt"));
let name = match q.s("rateLimitType") { "five_hour" => "5h".to_string(), "seven_day" => "week".into(), "" => "limit".into(), o => o.into() };
last = Some(Reading { at: when(e.at("timestamp")), windows: vec![Window { name, pct: FULL, resets }], hard: true, resets, ..Default::default() });
} else if !refused { last = None; } // an answer was served after it: that stop is over
}
last
}
/// Claude Code's status line input as the board kept it (`{at, rate_limits}`): its 5-hour and weekly windows. It comes
/// after an answer was served, so a window at 100% here is extra usage.
pub fn status_reading(kept: &Value) -> Option<Reading> {
let r = kept.at("rate_limits");
if !r.is_object() { return None; }
let windows: Vec<Window> = [("five_hour", "5h"), ("seven_day", "week")].iter().filter(|(k, _)| r.at(k).is_object())
.map(|(k, n)| Window { name: n.to_string(), pct: r.at(k).f("used_percentage"), resets: ms_of(r.at(k).at("resets_at")) }).collect();
let mut x = Reading { at: kept.i("at"), windows, ..Default::default() };
x.resets = x.full_resets();
x.extra = x.full();
Some(x)
}
/// A program's reading from a conversation's last lines.
pub fn window_in(kind: &str, rows: &[Value]) -> Option<Reading> {
match kind { "codex" => codex_reading(rows), "claude" => claude_reading(rows), _ => None }
}
/// The newer of two readings.
pub fn newest(a: Option<Reading>, b: Option<Reading>) -> Option<Reading> {
match (a, b) { (Some(a), Some(b)) => Some(if b.at > a.at { b } else { a }), (a, b) => a.or(b) }
}
/// Where the board keeps a Claude agent's status line input: beside the task, one file per role.
pub fn status_file(dir: &Path, role: &str) -> PathBuf { dir.join(format!("window-{role}.json")) }
/// The newest Codex conversation of this machine (today's or yesterday's), yours or a task's: Codex's window before any
/// task of the board ran it.
pub fn codex_newest() -> Option<Reading> {
let root = super::homes::codex_home().join("sessions");
use chrono::TimeZone;
let today = chrono::Local.timestamp_millis_opt(crate::clock::now()).single()?.date_naive();
let days = [Some(today), today.pred_opt()];
let files = days.iter().flatten().flat_map(|d| super::read_turn::find_file(&root.join(d.format("%Y/%m/%d").to_string()), &|n| n.ends_with(".jsonl"), 0));
let newest = files.max_by_key(|f| std::fs::metadata(f).and_then(|m| m.modified()).ok())?;
codex_reading(&tail_lines(&newest, 256 * 1024).ok()?)
}
Loading
Loading