diff --git a/apps/albedo/src/albedo/model_loop/loop.gleam b/apps/albedo/src/albedo/model_loop/loop.gleam index 24717054b..00667de41 100644 --- a/apps/albedo/src/albedo/model_loop/loop.gleam +++ b/apps/albedo/src/albedo/model_loop/loop.gleam @@ -16,6 +16,7 @@ import albedo/observation/stream_trace import albedo/observation/tool_progress import albedo/runtime import albedo/runtime/clip_cost +import albedo/session/turn import albedo/storage/requests import albedo/usage/cache_fade import albedo/usage/usage @@ -40,6 +41,7 @@ import gleam/bool import gleam/dict import gleam/dynamic.{type Dynamic} import gleam/dynamic/decode +import gleam/erlang/process import gleam/int import gleam/io import gleam/json @@ -1924,7 +1926,7 @@ fn retry_stream( False -> Error(types.Cancelled) True -> { reset_progress(attempt) - albedo_clock.sleep(delay) + backoff(delay) case kept { [] -> retry_stream( @@ -1948,6 +1950,18 @@ fn retry_stream( } } +/// Wait out a retry's backoff, or less once the run is stopped. A stop raises +/// the worker's latch and wakes it (`turn.cancel` and `turn.cancel_call`), so +/// the wait ends at once and the next attempt finds the run cancelled, rather +/// than holding an interrupted turn for up to 16 s. +pub fn backoff(delay_ms: Int) -> Nil { + let _ = + process.new_selector() + |> turn.select_stop(Nil) + |> process.selector_receive(albedo_clock.delay_ms(delay_ms)) + Nil +} + fn accepted_timeout(error: types.Error) -> Bool { case error { types.Timeout(phase, _) -> types.accepted(phase) diff --git a/apps/albedo/test/albedo/model_loop/backoff_test.gleam b/apps/albedo/test/albedo/model_loop/backoff_test.gleam new file mode 100644 index 000000000..9fb025254 --- /dev/null +++ b/apps/albedo/test/albedo/model_loop/backoff_test.gleam @@ -0,0 +1,19 @@ +//// A stopped run must not wait out its retry backoff. The waits double to +//// 16 s, and an interrupted turn stays `interrupting` until its worker ends, +//// so a worker that sleeps through the stop holds the session that long. + +import albedo/model_loop/loop +import albedo/session/turn +import gleam/erlang/process + +pub fn a_stop_ends_the_retry_backoff_at_once_test() -> Nil { + let done = process.new_subject() + let latch = turn.latch() + let worker = + process.spawn(fn() { + loop.backoff(60_000) + process.send(done, Nil) + }) + turn.cancel_call(worker, latch) + assert process.receive(done, 2000) == Ok(Nil) +} diff --git a/docs/http-sessions.md b/docs/http-sessions.md index c7cb8c1ca..961be5f5a 100644 --- a/docs/http-sessions.md +++ b/docs/http-sessions.md @@ -405,7 +405,8 @@ interrupt request does not mean all external tools stopped. The result contains `run_id`, `state: "requested"|"already_ended"`, `cancelled_input_ids`, and warnings. Interruption prevents new inputs joining -the captured run. Live status reports `interrupting` until the run ends. +the captured run. Live status reports `interrupting` until the run ends. A run +waiting out a provider retry backoff ends at once instead of finishing the wait. ## Human shell commands