Skip to content

Commit 0fa4d19

Browse files
committed
tell() fails fast when the budget is exhausted
No message is sent to the LLM; the error points at add_budget(). Previously the agent received the message, hit tool refusals, and spent tokens explaining it could not work.
1 parent 6a96f20 commit 0fa4d19

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

R/session.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ atlas_session <- R6::R6Class("atlas_session",
249249
#' @return The agent's reply (invisibly).
250250
tell = function(text, verbose = TRUE) {
251251
stopifnot(is.character(text), length(text) == 1)
252+
reason <- private$budget_reason()
253+
if (!is.null(reason)) {
254+
stop("session budget exhausted (", reason, ") - nothing was sent ",
255+
"to the LLM. Grant more with $add_budget(steps = , seconds = ) ",
256+
"and retry.", call. = FALSE)
257+
}
252258
private$verbose <- verbose
253259
if (private$context_tokens() >= private$compact_at) {
254260
self$compact()

tests/testthat/test-session.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ test_that("add_budget() unblocks an exhausted session", {
232232
expect_equal(readRDS(file.path(dir, "meta.rds"))$max_steps, 3)
233233
})
234234

235+
test_that("tell() on an exhausted session fails fast, without an LLM call", {
236+
chat <- FakeChat$new() # empty script: any message sent would error
237+
s <- atlas_session$new(mtcars, "mpg", chat = chat, dir = temp_dir(),
238+
max_steps = 1)
239+
s$chat$get_tools()$run_r_code("1 + 1") # spend the budget
240+
241+
expect_error(s$tell("remove the worst predictor"), "budget exhausted")
242+
expect_error(s$tell("remove the worst predictor"), "add_budget")
243+
expect_length(chat$log, 0) # nothing reached the model
244+
245+
s$add_budget(steps = 5)
246+
# now the message goes through (FakeChat script exhausted = it was sent)
247+
expect_error(s$tell("try again", verbose = FALSE), "script exhausted")
248+
expect_length(chat$log, 1)
249+
})
250+
235251
test_that("max_runtime blocks execution after the deadline", {
236252
s <- new_session(max_runtime = 0.01) # expires almost immediately
237253
Sys.sleep(0.05)

0 commit comments

Comments
 (0)