Skip to content
Merged
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
36 changes: 28 additions & 8 deletions server/src/proposals/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const MAX_STEPS: usize = 30;
const TIMEOUT: Duration = Duration::from_secs(10 * 60);
/// One command in the sandbox.
const COMMAND_SECS: u32 = 60;
/// Answers sent back for another try (too long, empty) before the
/// planner fails: a plan cut down once can still be a bit too long.
const MAX_CORRECTIONS: usize = 2;
/// What the planner's answer starts with when it writes no plan.
const NO_PLAN: &str = "NO PLAN:";
/// Bytes of stdout and of stderr read from one command, the tool output
Expand Down Expand Up @@ -163,7 +166,7 @@ impl Planner {
reasoning_effort: None,
};

let mut corrected = false;
let mut corrections = 0;
for _ in 0..MAX_STEPS {
let output = self.ask(&req, &model).await?;
let mut text = String::new();
Expand Down Expand Up @@ -196,20 +199,17 @@ impl Planner {
};
match plans::clean(text, refused) {
Ok(new) => return Ok(new),
// once: shorter, or with a plan at all
Err(e) if !corrected => {
corrected = true;
// shorter, or with a plan at all
Err(e) if corrections < MAX_CORRECTIONS => {
corrections += 1;
req.input.extend([
Input::Text {
role: Role::Assistant,
content: text.into(),
},
Input::Text {
role: Role::User,
content: format!(
"[genco] Not accepted: {e}. Answer \
with the whole plan again."
),
content: correction(&e),
},
]);
continue;
Expand Down Expand Up @@ -311,6 +311,17 @@ async fn read_at_most(r: impl AsyncRead + Unpin) -> Vec<u8> {
buf
}

/// Asks the planner to answer again; a plan over the limit is to be
/// shortened well below it, a cut to just under the limit often misses.
fn correction(e: &str) -> String {
format!(
"[genco] Not accepted: {e}. Answer with the whole plan again, at \
most {} characters: keep what to do and why with the exact names, \
drop explanations and commands genco's agent finds itself.",
plans::MAX_PLAN * 9 / 10
)
}

/// The admin sees why there is no plan and can ask for another try.
fn failed(reason: &str) -> plans::New {
plans::New {
Expand Down Expand Up @@ -577,4 +588,13 @@ mod tests {
assert_eq!(texts[4].1, "[Ann (admin)] Change the plan: try again");
assert_eq!(texts[1].0, "Assistant");
}

#[test]
fn a_too_long_plan_is_sent_back_with_a_lower_target() {
let long = "x".repeat(plans::MAX_PLAN + 58);
let e = plans::clean(&long, false).unwrap_err();
let c = correction(&e);
assert!(c.contains("1558 characters, at most 1500 are allowed"));
assert!(c.contains("again, at most 1350 characters"));
}
}