tokens: mask old large tool results in requests; compact inside a single long tool loop
This commit is contained in:
1 parent
3943243024
commit
80c0f83a68
3 files changed
+167
-11
No files matched your search
+50
-11
@@ -150,12 +150,14 @@ impl Agent {
|
||||
|
||||
/// Summarize everything before the most recent user prompts, keeping at least `keep` messages verbatim.
|
||||
/// The session file keeps full history; the head moves to a new root. Returns false if nothing to cut.
|
||||
/// ponytail: cuts only at user prompts, so one very long tool loop from a single prompt can't be compacted.
|
||||
/// Cuts at the latest user prompt; inside one long tool loop (no prompt to cut at) it cuts before an
|
||||
/// assistant message and the summary becomes its own leading user message.
|
||||
pub async fn compact(&mut self, keep: usize) -> Result<bool, Error> {
|
||||
let msgs = self.session.messages();
|
||||
let Some(cut) = (1..=msgs.len().saturating_sub(keep))
|
||||
.rev()
|
||||
.find(|&i| is_prompt(&msgs[i]))
|
||||
let range = 1..=msgs.len().saturating_sub(keep);
|
||||
let prompt_cut = range.clone().rev().find(|&i| is_prompt(&msgs[i]));
|
||||
let Some(cut) =
|
||||
prompt_cut.or_else(|| range.rev().find(|&i| msgs[i].role == Role::Assistant))
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
@@ -175,13 +177,13 @@ impl Agent {
|
||||
}
|
||||
}
|
||||
let summary = summary.ok_or(Error::Truncated)?;
|
||||
let note = format!("[Summary of earlier conversation]\n{summary}\n[End of summary]");
|
||||
let mut kept = msgs[cut..].to_vec();
|
||||
kept[0].content.insert(
|
||||
0,
|
||||
ContentBlock::Text {
|
||||
text: format!("[Summary of earlier conversation]\n{summary}\n[End of summary]"),
|
||||
},
|
||||
);
|
||||
if prompt_cut.is_some() {
|
||||
kept[0].content.insert(0, ContentBlock::Text { text: note });
|
||||
} else {
|
||||
kept.insert(0, Message::user(note));
|
||||
}
|
||||
self.session.rebase(kept)?;
|
||||
self.last_ctx = None;
|
||||
Ok(true)
|
||||
@@ -229,7 +231,7 @@ impl Agent {
|
||||
let req = Request {
|
||||
model: self.model.clone(),
|
||||
system: self.system.clone(),
|
||||
messages: self.session.messages(),
|
||||
messages: crate::mask::mask_old_results(self.session.messages()),
|
||||
tools: self.tools.specs(),
|
||||
max_tokens: self.max_tokens,
|
||||
};
|
||||
@@ -548,6 +550,43 @@ mod tests {
|
||||
assert_eq!(context_tokens(Some((1000, 5)), &m), estimate_tokens(&m));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn compaction_inside_tool_loop() {
|
||||
let text = |t: &str| asst(vec![ContentBlock::Text { text: t.into() }]);
|
||||
let script = Arc::new(Script(
|
||||
Mutex::new(vec![
|
||||
asst(vec![tu("1", "a")]),
|
||||
asst(vec![tu("2", "b")]),
|
||||
text("fin"),
|
||||
text("SUMMARY"),
|
||||
text("next"),
|
||||
]),
|
||||
Mutex::new(vec![]),
|
||||
));
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut tools = Registry::default();
|
||||
tools.add(Echo);
|
||||
let cx = ToolCtx {
|
||||
cwd: dir.path().into(),
|
||||
state: Default::default(),
|
||||
};
|
||||
let (mut a, _h) = Agent::new(
|
||||
script.clone(),
|
||||
tools,
|
||||
Session::open(dir.path().join("s.jsonl")).unwrap(),
|
||||
cx,
|
||||
);
|
||||
a.run("go").await.unwrap(); // p, a1, r1, a2, r2, fin
|
||||
assert!(a.compact(3).await.unwrap());
|
||||
let m = a.session.messages();
|
||||
// summary(user), a2, r2, fin -> alternates, tool_use still has its result
|
||||
assert_eq!(m.len(), 4);
|
||||
assert_eq!(m[0].role, Role::User);
|
||||
assert!(m[0].text().contains("SUMMARY"));
|
||||
assert_eq!(m[1].role, Role::Assistant);
|
||||
assert!(matches!(m[2].content[0], ContentBlock::ToolResult { .. }));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn compaction() {
|
||||
let text = |t: &str| asst(vec![ContentBlock::Text { text: t.into() }]);
|
||||
|
||||
Reference in new issue
Block a user