Batching nudges: parallel-calls prompt line, codemode hint, tool-call count

This commit is contained in:
hermes committed 2026-10-12 12:33:52 +00:00
1 parent 8b5d24436f
commit a30b3a3e73
5 files changed
+120 -4

No files matched your search

+1
View File
@@ -8,6 +8,7 @@ Use the tools to read, edit and run code. Read files before editing them. Keep a
- Inspect with the tools, not scripts: `read` for files, `rg` to search text, `fd` or `find` to list files, `ls`/`tree` for directories.\n\
- Never use node, python, perl, sed or awk to look at files or data; they need user approval every time. Read-only commands (ls cat head tail rg fd find grep wc jq sort, git status/log/diff/show) run without a prompt, also piped into each other.\n\
- One simple command per call: no `;`, `&&`, redirects, `$()`. Those need approval too.\n\
- Issue independent tool calls together in one turn; they run in parallel.\n\
- Script only when the task needs it (build, test, edit), not to explore.";
const CONTEXT_FILES: &[&str] = &["AGENTS.md", "CLAUDE.md"];
+1
View File
@@ -9,6 +9,7 @@ pub mod herdr;
pub mod hook;
mod mask;
pub mod memory;
pub mod nudge;
pub mod permit;
pub mod session;
pub mod stats;
+103
View File
@@ -0,0 +1,103 @@
//! Cheap nudge: after `STREAK` consecutive turns that each made one lone read-only call, remind the
//! model once that independent reads can be batched (parallel native calls, or `codemode`).
//! Injected at turn end, so the cached prefix is untouched.
//! ponytail: name/first-word heuristic; swap for per-tool metadata if tools grow a read-only flag.
use crate::hook::{Event, Hook, HookAction};
use async_trait::async_trait;
use std::sync::Mutex;
const STREAK: u32 = 3;
const READ_ONLY: &[&str] = &[
"read", "rg", "fd", "find", "ls", "tree", "cat", "head", "tail", "grep", "wc",
];
#[derive(Default)]
struct S {
calls: Vec<bool>, // per call this turn: read-only?
streak: u32,
}
pub struct BatchNudge {
codemode: bool,
s: Mutex<S>,
}
impl BatchNudge {
pub fn new(codemode: bool) -> Self {
Self {
codemode,
s: Mutex::default(),
}
}
}
fn read_only(name: &str, args: &serde_json::Value) -> bool {
match name {
"read" => true,
"bash" => args["command"]
.as_str()
.and_then(|c| c.split_whitespace().next())
.is_some_and(|w| READ_ONLY.contains(&w)),
_ => false,
}
}
#[async_trait]
impl Hook for BatchNudge {
async fn on(&self, ev: &Event) -> HookAction {
let mut s = self.s.lock().unwrap_or_else(|e| e.into_inner());
match ev {
Event::ToolCall { name, args, .. } => s.calls.push(read_only(name, args)),
Event::TurnEnd => {
let lone_read = s.calls == [true];
s.calls.clear();
s.streak = if lone_read { s.streak + 1 } else { 0 };
if s.streak == STREAK {
s.streak = 0;
return HookAction::Inject(if self.codemode {
"hint: several single reads in a row. Batch independent reads/searches in one turn (parallel tool calls), or use `codemode` with Promise.all and filter the output.".into()
} else {
"hint: several single reads in a row. Emit independent reads/searches as parallel tool calls in one turn.".into()
});
}
}
_ => {}
}
HookAction::Continue
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
async fn turn(h: &BatchNudge, name: &str, args: serde_json::Value) -> HookAction {
h.on(&Event::ToolCall {
id: "i".into(),
name: name.into(),
args,
})
.await;
h.on(&Event::TurnEnd).await
}
#[tokio::test]
async fn nudges_after_streak_then_resets() {
let h = BatchNudge::new(true);
assert_eq!(turn(&h, "read", json!({})).await, HookAction::Continue);
assert_eq!(
turn(&h, "bash", json!({"command": "rg foo"})).await,
HookAction::Continue
);
assert!(
matches!(turn(&h, "read", json!({})).await, HookAction::Inject(m) if m.contains("codemode"))
);
// a write breaks the streak
turn(&h, "read", json!({})).await;
turn(&h, "edit", json!({})).await;
turn(&h, "read", json!({})).await;
assert_eq!(turn(&h, "read", json!({})).await, HookAction::Continue);
}
}