Skip to content

fix(chat): leave a resume prefetch that has started running on a focus change - #33768

Closed
jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/resume-prefetch-cancel-33711
Closed

jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/resume-prefetch-cancel-33711

Conversation

@jeeshofone

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Moving between dashboard chats no longer kills a resume prefetch that has already started loading its session.

_handle_slot_focused cancelled the socket's pending resume prefetch on every focus change and blur, whatever stage it had reached. A cancel landing after the 1.5 s debounce went through the start's except BaseException cleanup, which killed a runtime whose session/load had often already succeeded, and logged it as failed session setup cleanup.

Why it matters

Clicking through a few resumable chats after a gateway restart threw away runtimes 2 to 17 s old, each costing a full start (the agent process plus its MCP servers). The cancel also read in the log as a setup failure.

Not a goal

What changed (motivation → approach → change)

Symptom: focus or blur killed a started prefetch. Root cause: the focus path cancelled at any stage, not only during the debounce it exists to collapse. Change:

  • chat_turn/eager_spawn.py records a speculative start once its debounce ends. eager_spawn_debouncing(task) reports whether it is still debouncing, and chat_runner re-exports it like the other eager-spawn entry points.
  • ws._handle_slot_focused cancels the previous prefetch only while it is still debouncing. A prefetch already starting finishes, and the existing unclaimed-prefetch TTL and population cap reap it if no turn claims it. Rapid tab flipping still leaves at most one debouncing prefetch per socket.
  • providers/acp_runtime_start.py names a cancelled setup's kill cancelled session setup cleanup.

Backwards compatibility

Compatible: no input, key or API is narrowed; a started prefetch is kept instead of cancelled, and one kill reason string gains a cancelled variant.

Removes nothing: no path, record, key or function is removed.

Tests

  • test_eager_spawn_slot_frames.py::test_focus_change_and_blur_leave_a_started_prefetch_running: a prefetch past its debounce survives a focus change and a blur. Fails on main's ws.py (assert not True).
  • test_acp_provider.py::test_a_cancelled_setup_kills_its_runtime_as_cancelled_not_failed: a cancelled setup kills with the cancelled reason.
  • Related and contract suites (eager spawn, ACP provider, runtime death, ws event scoping, error-code contract, chat-runner and dashboard-server composition, symbols manifest): 986 passed, 3 skipped. black, flake8, mypy, Semgrep and the file line cap pass.

Proof: PROVEN test/test_acp_provider.py::TestStartKiroRuntimeResume::test_a_cancelled_setup_kills_its_runtime_as_cancelled_not_failed (head 59c92ce)

Manual verification

N/A — the focus-path decision and the kill reason are both covered by unit tests; no live gateway run.

Related Issues

Fixes #33711

Sibling sweep

Swept: git grep -n "prev_task.cancel|_eager_spawn_task" -- src/
Sibling: src/kiro_crew/dashboard/chat_turn/eager_spawn.py -- not affected -- schedule_eager_spawn cancels on a newer signal for the same slot, whose bindings changed
Sibling: src/kiro_crew/dashboard/chat_turn/eager_spawn.py -- not affected -- stop_eager_spawns cancels at gateway shutdown, where the kill is wanted

Pattern harvest

Not generalizable: the debounce-only cancel is specific to the focus prefetch signal.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

@jeeshofone
jeeshofone requested review from a team and KiroCrewCaptain as code owners November 9, 2026 07:10
@jeeshofone
jeeshofone requested a review from Zedmor November 9, 2026 07:10
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 9, 2026
…s change

A focus change or blur cancelled the socket's pending resume prefetch at any
point, so a cancel landing after the debounce killed a runtime whose
session/load had already succeeded, logged as a failed setup. Only a prefetch
still debouncing is cancelled now; one already starting finishes and is reaped
by the unclaimed-prefetch TTL and population cap. A cancelled setup's kill now
says cancelled session setup cleanup.

Fixes kirodotdev#33711
@jeeshofone
jeeshofone force-pushed the fix/resume-prefetch-cancel-33711 branch from 59c92ce to fc8a912 Compare November 9, 2026 09:07
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision fc8a91234ddab990b835f47bcc3a072010efeadf touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of fc8a91234ddab990b835f47bcc3a072010efeadf via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the fix stops focus changes from cancelling prefetches that have started, and the TTL and population cap that already exist still limit what those prefetches hold.

Notes

  • Several sockets can now each leave a started handshake running after flipping through tabs. Before this, focus-path cancels capped that at one per socket. _cap_armed_prefetches only counts a prefetch once it registers, so it is worth confirming the admission check at eager_spawn.py:843 also covers handshakes still in flight.

[DESIGN-REVIEWED] fc8a912

Description read

[DESCRIPTION-READ] 6bd07575fcfd079fad05ed40e6e24754e14b71f158171b1472190641024567a2

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — 🔴 changes requested (blocking)

Reviewed fc8a91234ddab990b835f47bcc3a072010efeadf via the fork AI-review pipeline; updated in place on each push.

Blocks: prefetches that keep running after the user leaves a slot now hold both places in the eager-spawn limit (the semaphore allows two), so the slot the user actually settles on gets skipped and cold-starts.

BLOCKING — src/kiro_crew/dashboard/ws.py:72
if prev_task is not None and not prev_task.done() and eager_spawn_debouncing(prev_task):
Trigger: The user stays on resumable slot t1 for more than 1.5s, then on t2 for more than 1.5s, then focuses t3 (or creates a new slot). This happens while t1's and t2's spawn plus session/load handshakes, which take several seconds, are still running. Neither earlier task is cancelled any more, so both sit inside async with _eager_spawn_sem (_EAGER_SPAWN_MAX_CONCURRENT = 2, chat_runner.py:644). When t3's task finishes its debounce, _eager_spawn hits if _eager_spawn_sem.locked(): ... return (eager_spawn.py:605) and never retries.
Outcome: The focused slot's resume prefetch is dropped. A FOREGROUND spawn armed by create or agent switch is dropped the same way. The user then waits for a full cold start on the slot they are about to type in, while loads for slots they already left keep running. Before this change, each focus change cancelled the earlier task and freed its place, so t3 got one.
Anchor: defect
Fix: Never let a spared background prefetch block the slot the user is focused on. Either exempt the focused-slot and FOREGROUND spawns from the locked() skip, or make a newer focus cancel spared tasks that have not yet reached _spawn_admitted_prefetch. The second option still keeps the protection this PR adds for loads already in flight.

Notes:

  • The _handle_slot_focused docstring and learn-cron-dashboard.md:2179 still say every focus change and blur cancels the prefetch and that at most one is pending per socket. Neither is true after this change.

[BLOCK-MERGE] fc8a912
[OPUS-REVIEWED] fc8a912

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — 🔴 changes requested (blocking)

Reviewed fc8a91234ddab990b835f47bcc3a072010efeadf via the fork AI-review pipeline; updated in place on each push.

Adjudication upheld 1 of 1 blocking finding(s); those must be resolved before merging.

BLOCKING -- src/kiro_crew/dashboard/ws.py:710 -- Returning focus still cancels the preserved prefetch

if prev_task is not None and not prev_task.done() and eager_spawn_debouncing(prev_task):
Trigger: Hide and reveal the dashboard while its resume prefetch is inside session setup. Blur preserves the task but returns None; reveal therefore bypasses the same-task guard and calls schedule_eager_spawn, which cancels the slot’s unfinished task. has_session remains false until startup registers the session.
Outcome: Cancellation reaches _start_kiro_runtime_impl’s cleanup and kills the starting runtime; the replacement prefetch repeats startup and session loading, defeating preservation and delaying chat readiness.
Anchor: defect
Fix: Reuse the target slot’s unfinished prefetch on focus return, without changing cancellation for binding-change signals.

[BLOCK-MERGE] fc8a912
[GPT-REVIEWED] fc8a912

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

F1 — UPHOLD. Harm rung: MEDIUM. Hiding and revealing a tab is a common path. When it happens during resume setup, the prefetch this PR means to preserve gets killed and restarted. That is recoverable but costs a full restart and session load.

Confirmed conditions: on blur the task is not cancelled, but the handler returns None (ws.py:713-714, unchanged by the patch). On reveal, prev_task is None, so the same-task guard at ws.py:701-710 is skipped. The handler then passes the has_session check at ws.py:721 and calls schedule_eager_spawn, which unconditionally cancels the slot's unfinished _eager_spawn_task (eager_spawn.py:496-498).

Fix cost: small and local. On focus, reuse the slot's own pending task once it is past the debounce. That is one extra check in the guard, with no new state, no change to the binding-change signals, and nothing new for later readers to carry.

[ADJUDICATION] fc8a91234ddab990b835f47bcc3a072010efeadf total=1 uphold=1 downgrade=0
UPHOLD F1 src/kiro_crew/dashboard/ws.py:710 reason=harm-warrants-remedy
[GPT-ADJUDICATED] fc8a91234ddab990b835f47bcc3a072010efeadf

@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

This PR and #33898 both close #33711. Both change eager_spawn.py, chat_runner.py, ws.py and test/test_eager_spawn_slot_frames.py the same way: _handle_slot_focused now cancels the previous prefetch task only when a new predicate says that task has not started its runtime yet. Please keep one of #33768 and #33898 and close the other, carrying over anything only this one has, for example the cancelled session setup cleanup kill reason in acp_runtime_start.py and its test.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

#33898 fixes the same issue and also covers two cases this PR misses:

The only thing #33768 adds is the cancelled session setup cleanup kill reason in acp_runtime_start.py and its test, which could be carried into #33898. Maintainers can keep whichever PR they prefer.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Closing in favour of #33898, which also covers the socket-close cancel and the refocus-while-starting case. The one piece only this PR had, the 'cancelled session setup cleanup' kill reason in acp_runtime_start.py and its test, is free for #33898 to carry over if useful.

@jeeshofone jeeshofone closed this Nov 9, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cancelling a resume prefetch after its runtime is up kills the runtime as a failed setup

2 participants