Repository navigation
Conversation
7af2f38 to
b1f3fb6
Compare
b1f3fb6 to
12ee8d4
Compare
Code change approver recommendation: A person should decideWhy: the decider thinks the change is probably fine but estimates an 18% chance that this change introduces a defect, above the 12% limit for approving on its own. The fix does what the PR says. It marks an eager-spawn task as "starting" around _spawn_admitted_prefetch (after admission; removed in a finally), and _handle_slot_focused no longer cancels a starting task on focus change, blur or socket close. Socket close now goes through the same handler as a blur. Refocusing a slot whose prefetch is starting returns that same task. A reload's respawn is still checked first, and a cancel during the debounce still lands. compose() binds the eager-spawn functions to chat_runner's globals, so the new set resolves correctly. The cleanup kill now reports "cancelled session setup cleanup" for a CancelledError; nothing in src/ matches on the old string. The deliberate teardown cancels (shutdown, slot delete, agent/project re-arm, environment switch) are unchanged. The tests wait on events, drive the real handlers and pin each new branch. An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit |
12ee8d4 to
47fdca3
Compare
47fdca3 to
f47c009
Compare
f47c009 to
5d166b7
Compare
5d166b7 to
f7583f8
Compare
f7583f8 to
a1344e7
Compare
a1344e7 to
a81c406
Compare
a81c406 to
73f6766
Compare
A focus change, blur or socket close cancelled the slot's resume prefetch even after its runtime had spawned, often after session/load had already run. The cancel reached the start's BaseException cleanup, which killed the runtime as a failed session setup or a failed init handshake. Clicking through resumable chats paid a full process-tree start each time and kept none of them. A prefetch is marked as starting while _spawn_admitted_prefetch runs. The slot_focused handler leaves a starting prefetch running, and the socket close takes the same path as a blur. A later focus of its slot keeps it instead of arming a new one. The finished session falls to the existing unclaimed-prefetch TTL and population cap. A cancel during the debounce still lands. A cancel that still reaches the session setup cleanup kills the runtime with the reason "cancelled session setup cleanup". Other setup exceptions use "failed session setup cleanup". Closes kirodotdev#33711 Co-authored-by: jeeshofone <167437+jeeshofone@users.noreply.github.com>
73f6766 to
eb1e059
Compare
Problem / Motivation
Goal: Clicking through resumable chats no longer throws away resume prefetches that already started their runtime.
Focusing a resumable chat schedules a resume prefetch. The focus handler cancels that prefetch on the next focus change, on blur and when the socket closes. That cancel is meant for the 1.5 s debounce, but it also lands after the debounce, once the runtime has spawned and
session/loadhas run. TheCancelledErrorthen goes through the start'sexcept BaseExceptioncleanup, and the runtime is killed asfailed session setup cleanuporfailed init handshake cleanup. #33711 has logs of runtimes killed 2 to 17 s after spawn, a second aftersession_loadandset_modeloggedoutcome=ready.Why it matters
Every runtime thrown away this way costs a full start: the agent process, its MCP stub processes and the stdio MCP servers. That's about twenty process launches on the host in the report. It hits anyone with
session.eager_spawnon who moves between chats after a gateway restart, and the chat they come back to still cold-starts on its first message.Not a goal
test_acp_runtime_rederive_retry.pymatch on its exact reason string.What changed (motivation → approach → change)
A cancel can't safely interrupt a start that already has a process, so the focus path stops cancelling once the start begins.
_eager_spawnadds its task to_starting_spawns(held inchat_runnernext to_armed_prefetches) for the length of_spawn_admitted_prefetch, andeager_spawn_is_startingreads it._handle_slot_focusedleaves a starting task running, and the socket close in_serve_wsgoes through the same handler as a blur. Refocusing a slot whose prefetch is starting returns that task instead of arming a new one, which would cancel it throughschedule_eager_spawn. A reload's respawn is checked first and stays with the reload, so a focus returnsNonefor it even while it is starting. The finished session goes to the existing TTL and population cap, as the issue suggests. A cancel during the debounce or before admission still lands. An admitted prefetch is kept even while it waits in the start queue, before its process exists. The ws.py entry inlearn-cron-dashboard.mdnow says so. A cancel that still reaches the session setup cleanup inacp_runtime_start.pykills the runtime ascancelled session setup cleanup, and any other setup exception kills it asfailed session setup cleanup. That change and its test come from #33768.Backwards compatibility
Compatible: nothing is rejected or renamed. A focus change, blur or socket close now skips one cancel it used to issue.
Removes nothing: no path, record, key or function is taken away. The predicate and the set are new.
Tests
test/test_eager_spawn_slot_frames.py:test_focus_change_after_start_keeps_the_load[t2]and[None]: a focus change to another chat, or a blur, while the prefetch is insideget_or_createdoesn't cancel it. The load finishes and the TTL is armed.test_refocus_while_starting_keeps_the_load: leaving the chat and coming back while its prefetch is starting keeps the same task and runs one load.test_a_starting_reload_respawn_is_not_adopted_by_a_focus: a focus on a slot whose reload respawn is starting returnsNoneand leaves the respawn running. It fails if the reload check and the starting check swap order.test_socket_close_after_start_keeps_the_load: drivesapi_wswith a socket that sends oneslot_focusedframe and then disconnects whileget_or_createis blocked. The starting prefetch isn't cancelled, the load finishes and the TTL is armed.test_a_failed_start_is_not_kept_by_the_next_focus: a start that raises leaves_starting_spawns, and the next focus of its slot doesn't adopt the finished task._finish_loadalso checks the task left the set after a successful load.test_focus_change_cancels_previous_prefetchandtest_blur_cancels_and_schedules_nothingstill pass, so a cancel during the debounce still lands.test/test_acp_provider_setup_cleanup_reason.py:test_a_setup_cleanup_kill_names_why_the_setup_endedis parametrized over both branches. ACancelledErrorfromcreate_sessionkills the runtime once, ascancelled session setup cleanup. ARuntimeErrorkills it asfailed session setup cleanup. With the change reverted, the cancelled case seesfailed session setup cleanupand fails. The checks live in their own file becausetest/test_acp_provider.pyis already at the 2,000-line cap on main.Proof: PROVEN test/test_acp_provider_setup_cleanup_reason.py::test_a_setup_cleanup_kill_names_why_the_setup_ended[error1-cancelled session setup cleanup], test/test_eager_spawn_slot_frames.py::TestSlotFocusedFrame::test_focus_change_after_start_keeps_the_load[t2], test/test_eager_spawn_slot_frames.py::TestSlotFocusedFrame::test_focus_change_after_start_keeps_the_load[None], test/test_eager_spawn_slot_frames.py::TestSlotFocusedFrame::test_refocus_while_starting_keeps_the_load, test/test_eager_spawn_slot_frames.py::TestSocketCloseKeepsStartingPrefetch::test_socket_close_after_start_keeps_the_load (head a81c406)
The related suites (2,513 tests:
test_acp_provider.py, the new cleanup-reason file,test_eager_spawn_slot_frames.py, the websocket suites and the suites #33875 and #35739 touched) pass on this head. A fullscripts/local-gate.pyrun on an earlier base passed 18,767 tests. Its 13 failures fail the same way on unmodifiedmainon this host (test_cli.py::TestDoctorcannot read a home-directory file, and twotest_mcp_gateway_pool_integ.pytests cannot read a stub's start-time identity).Manual verification
N/A: unit coverage drives the real
_handle_slot_focused,api_wsand_eager_spawnwith a session manager that blocks insideget_or_create. I haven't repeated the issue's click-through against a live gateway.Related Issues
Closes #33711
Sibling sweep
Swept: git grep -nE "(_eager_spawn_task|_focus_task|prev_task|prev).cancel()|task.cancel()" -- src/kiro_crew/dashboard
Sibling: src/kiro_crew/dashboard/ws.py -- changed here
Sibling: src/kiro_crew/dashboard/chat_turn/eager_spawn.py -- not affected -- 503 re-arms on new bindings, so the old runtime is torn down either way, and 1156 is gateway shutdown, which has to stop every start
Sibling: src/kiro_crew/dashboard/chat_api/slot_lifecycle.py:785 -- not affected -- the slot is being deleted
Sibling: src/kiro_crew/dashboard/chat_turn/app_agent_binding.py:56 -- not affected -- an app-owned turn was refused and its session must not serve
Sibling: src/kiro_crew/dashboard/handlers/session_environments.py:725 -- not affected -- the session moves to another environment
Pattern harvest
Not generalizable: the defect is one handler cancelling a task past the point where cancelling is cheap. The other cancels of this task are deliberate teardowns.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)