From b8fb8809845ce2bf6bb06220ddfeaa905cf47c18 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 20 Nov 2026 05:33:18 -0500 Subject: [PATCH] fix(session): report a pending turn start as preparing, not idle When the actor decided a queued input should start, it sent itself StartQueued and answered every read handled before that message from a state with no run, no boot and no reset: idle. After a workspace move the reset handler clears the reset and queues the start, so a status read that reached the actor during the reset was answered idle although the input admitted during the move had not run. A client that waits for idle took the input for done; under load a_move_waits_for_teardown_without_blocking_admission failed this way (3 of 15 runs of 12 parallel copies on four CPUs; a trace of the actor showed the idle state between the reset and the start). The actor now records the start it sent itself (start_pending) through queue_start, clears it when it handles StartQueued, and the status counts it as preparing, like a boot or a reset. Every immediate StartQueued send goes through queue_start: admission commits, workspace application and reset, kernel upgrades and editor overlay reloads. The delayed retry sends keep their own status (blocking reason, held failure). a_read_between_a_reset_and_the_next_start_is_not_idle holds the runtime so the reset lands while the actor is suspended, queues a summary read behind it, and fails on the old code (phase idle). The move test passed 8 of 8 loaded runs afterwards. --- apps/albedo/src/albedo/session.gleam | 33 ++++++++----- .../src/albedo/session/session_state.gleam | 7 ++- .../bedrock_replay_preflight_test.gleam | 1 + .../albedo/session/albedo_session_stall.erl | 17 ++++++- .../albedo/session/session_actor_test.gleam | 48 +++++++++++++++++++ robot-docs/sessions.md | 11 +++++ 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/apps/albedo/src/albedo/session.gleam b/apps/albedo/src/albedo/session.gleam index 75b4fe92a..d6a7540a3 100644 --- a/apps/albedo/src/albedo/session.gleam +++ b/apps/albedo/src/albedo/session.gleam @@ -744,7 +744,8 @@ pub fn start( )) let unheld = list.take_while(waiting, fn(row) { !dict.has_key(held_map, row.id) }) - case turn.starts_turn(unheld) { + let start_pending = turn.starts_turn(unheld) + case start_pending { False -> Nil True -> process.send(self, StartQueued) } @@ -805,6 +806,7 @@ pub fn start( boot_token: 0, workspace_reset: None, workspace_probe: None, + start_pending:, releasing: None, checkpointing: None, closing: False, @@ -1645,8 +1647,7 @@ fn upgraded_kernel( "/sessions/" <> state.info.id, ) list.each(parked, process.send(state.self, _)) - process.send(state.self, StartQueued) - answer(state, reply, refusal.storage(outcome)) + answer(queue_start(state), reply, refusal.storage(outcome)) } /// Replace the session's kernel with an empty one, stopping what runs first. @@ -2186,7 +2187,10 @@ fn handle( } WorkspaceReady(token, purpose, result) -> actor.continue(workspace_ready(state, token, purpose, result)) - StartQueued -> actor.continue(start_queued(state)) + StartQueued -> + actor.continue(start_queued( + session_state.State(..state, start_pending: False), + )) AdmitOperation(operation, submission, committed, reply) -> admit_operation(state, operation, submission, committed, reply) AdmitACP(operation, submission, digest, committed, reply) -> @@ -2232,9 +2236,8 @@ fn handle( case outcome { Ok(_) -> { acp_attachments.reloaded(state.info.id) - process.send(state.self, StartQueued) answer( - session_state.State(..state, blocked_until: 0), + session_state.State(..queue_start(state), blocked_until: 0), reply, Ok(Nil), ) @@ -6006,6 +6009,13 @@ fn children_working(host: runtime.Runtime, id: String) -> Bool { } } +/// Ask the actor to start what waits in the inbox once it has handled the +/// messages already queued; until then the session reads as preparing. +fn queue_start(state: State) -> State { + process.send(state.self, StartQueued) + session_state.State(..state, start_pending: True) +} + fn start_queued(state: State) -> State { use <- bool.lazy_guard( state.pending_agent_close != None @@ -6335,8 +6345,7 @@ fn workspace_reset( case applied { Ok(state) -> { list.each(parked, process.send(state.self, _)) - process.send(state.self, StartQueued) - state |> follow_up |> session_state.announce + state |> queue_start |> follow_up |> session_state.announce } Error(#(state, error)) -> list.fold(parked, state, fn(state, message) { @@ -6380,9 +6389,9 @@ fn finish_workspace( True -> { // Inputs a missing folder blocked try the new one now, not when // their 15 s retry comes round. - case queue_starts(state) { - False -> Nil - True -> process.send(state.self, StartQueued) + let state = case queue_starts(state) { + False -> state + True -> queue_start(state) } let state = session_state.State( @@ -6651,8 +6660,8 @@ fn admission_committed( outcome.acceptance_order, TitleFinished, ) - process.send(state.self, StartQueued) state + |> queue_start |> session_state.emit(view.Input( outcome, Some(session_submission.display(submission)), diff --git a/apps/albedo/src/albedo/session/session_state.gleam b/apps/albedo/src/albedo/session/session_state.gleam index ae91df688..67dc79f66 100644 --- a/apps/albedo/src/albedo/session/session_state.gleam +++ b/apps/albedo/src/albedo/session/session_state.gleam @@ -128,6 +128,10 @@ pub type State(message) { workspace_reset: Option(List(message)), /// A remote workspace readiness probe running outside the actor. workspace_probe: Option(String), + /// A `StartQueued` the actor sent itself and has not handled yet. Reads + /// answered before it report the session preparing, not idle, so a + /// client never hears that an input waiting to start is done. + start_pending: Bool, releasing: Option(List(message)), checkpointing: Option(List(message)), closing: Bool, @@ -496,7 +500,8 @@ pub fn current_status(state: State(message)) -> session_activity.Status { state.activity, state.booting != None || state.workspace_reset != None - || state.holding_failure, + || state.holding_failure + || state.start_pending, blocking, ) } diff --git a/apps/albedo/test/albedo/history/bedrock_replay_preflight_test.gleam b/apps/albedo/test/albedo/history/bedrock_replay_preflight_test.gleam index 3cb29bb7d..aeb86325b 100644 --- a/apps/albedo/test/albedo/history/bedrock_replay_preflight_test.gleam +++ b/apps/albedo/test/albedo/history/bedrock_replay_preflight_test.gleam @@ -201,6 +201,7 @@ fn state(home: String, host: runtime.Runtime) -> session_state.State(Nil) { boot_token: 0, workspace_reset: None, workspace_probe: None, + start_pending: False, releasing: None, checkpointing: None, closing: False, diff --git a/apps/albedo/test/albedo/session/albedo_session_stall.erl b/apps/albedo/test/albedo/session/albedo_session_stall.erl index cfa67953f..0d0f8c299 100644 --- a/apps/albedo/test/albedo/session/albedo_session_stall.erl +++ b/apps/albedo/test/albedo/session/albedo_session_stall.erl @@ -13,7 +13,8 @@ hold_readers/1, release_readers/1, wait_capture/0, - capture_lock_order/2 + capture_lock_order/2, + summary_behind/2 ]). suspend(Pid) -> @@ -50,6 +51,20 @@ start(Session, Kind, Reply) -> end), nil. +%% With the actor suspended, wait for a message tagged `Tag` in its mailbox, +%% queue a summary read behind it, resume the actor, and return that read. +summary_behind(Session, Tag) -> + {ok, Owner} = 'gleam@erlang@process':subject_owner(Session), + <<"queued">> = wait_queued(Owner, Tag), + Parent = self(), + spawn(fun() -> Parent ! {summary_behind, 'albedo@session':summary(Session)} end), + <<"queued">> = wait_queued(Owner, read_summary), + ok = sys:resume(Owner), + receive + {summary_behind, Summary} -> Summary + after 30000 -> error(summary_not_answered) + end. + %% Whether the actor answers a summary read within a second. responsive(Session) -> case diff --git a/apps/albedo/test/albedo/session/session_actor_test.gleam b/apps/albedo/test/albedo/session/session_actor_test.gleam index 8d4b9d63c..df57f37e3 100644 --- a/apps/albedo/test/albedo/session/session_actor_test.gleam +++ b/apps/albedo/test/albedo/session/session_actor_test.gleam @@ -15,6 +15,7 @@ import albedo/history/conversation import albedo/runtime import albedo/session +import albedo/session/session_activity import albedo/session/session_configuration import albedo/session/session_workspace import albedo/support/turn_harness as h @@ -65,10 +66,17 @@ fn wait_capture() -> String @external(erlang, "albedo_session_stall", "capture_lock_order") fn capture_lock_order(session: session.Session, home: String) -> String +@external(erlang, "albedo_session_stall", "summary_behind") +fn summary_behind( + session: session.Session, + tag: Tag, +) -> Result(session.Summary, String) + type Tag { ObserveLoaded Reset Delete + WorkspaceReset } fn runtime_pid(harness: h.Harness) -> Pid { @@ -133,6 +141,46 @@ pub fn unload_checks_children_without_blocking_the_parent_test() -> Nil { h.stop(harness) } +/// A move resets the session's kernel; an input admitted meanwhile waits for +/// the reset, and once it lands the actor sends itself the start of that +/// input's turn. A read answered in between must not call the session idle: +/// a client waiting for idle would take the input for done. The runtime is +/// held so the reset lands while the actor is suspended, with the read +/// queued right behind it. +pub fn a_read_between_a_reset_and_the_next_start_is_not_idle_test_() -> albedo_property.Timed { + use <- albedo_property.within(120) + let harness = + h.start([h.python("c1", "x = 1"), h.text("one"), h.text("two")], []) + let _ = h.say(harness, "before move") + h.idle(harness, 30_000) + let destination = harness.workspace <> "/destination" + let _ = make_dir(destination) + let assert Ok(observed) = + conversation.capture(runtime.ledger(harness.host), harness.id) + let version = + session_configuration.Version( + observed.configuration.revision, + observed.family.revision, + ) + let runtime_pid = runtime_pid(harness) + let assert Ok(session_pid) = process.subject_owner(harness.session) + suspend(runtime_pid) + process.spawn(fn() { + session.change_workspace( + harness.session, + session_workspace.Request(harness.id, version, destination), + ) + }) + assert wait_queued(runtime_pid, Delete) == "queued" + let _ = h.say(harness, "after move") + suspend(session_pid) + resume(runtime_pid) + let assert Ok(summary) = summary_behind(harness.session, WorkspaceReset) + assert summary.status.phase != session_activity.Idle + h.idle(harness, 30_000) + h.stop(harness) +} + pub fn capture_loans_cannot_exhaust_the_actors_reader_pool_test_() -> albedo_property.Timed { use <- albedo_property.within(90) let harness = h.start([], []) diff --git a/robot-docs/sessions.md b/robot-docs/sessions.md index 952ea85db..b7e6d5780 100644 --- a/robot-docs/sessions.md +++ b/robot-docs/sessions.md @@ -110,6 +110,17 @@ unfinished assistant row within the 15-minute `lease_ms` retention window. Persistence and recovery can fail, so callers must handle the returned results rather than infer success from a requested transition. +A session's status phase +([`session_activity.status`](../apps/albedo/src/albedo/session/session_activity.gleam)) +is `idle` only when nothing it already accepted is about to run. While the actor +boots a kernel, resets a moved workspace, holds a failure, or has sent itself +the start of a queued turn that it has not handled yet +(`session_state.start_pending`), the phase is `preparing`. Clients and tests may +wait for `idle` to mean that every admitted input has run. Start a queued turn +through `queue_start` in +[`session.gleam`](../apps/albedo/src/albedo/session.gleam), not with a bare +`StartQueued` send. + Session deletion coordinates closure with the runtime and durable state. [`session_deletion.gleam`](../apps/albedo/src/albedo/session/session_deletion.gleam) owns the daemon deletion path. Agent code cannot invoke deletion through -- knotmirror 0.0.1