Skip to content

fix(dashboard): record a turn a chat close cut off as interrupted - #18990

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/chat-close-records-cut-off-turn
Oct 11, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
javenciu:fix/chat-close-records-cut-off-turn

Conversation

@javenciu

@javenciu javenciu commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Closing a tab while its turn is running cancels the turn. The runner's cancel branch (_run_chat in src/kiro_crew/dashboard/chat_runner.py, through _persist_partial_reply) keeps the streamed part of the reply as an ordinary assistant row, and nothing follows it. The chat reopened from history therefore shows the cut-off reply as a finished answer: no notice and no Resume control (state.is_turn_interrupted returns False for that tail). Reproduced with the real _run_chat, the real DELETE /api/chat/slots/{slot} route and a fresh ConversationLog: the archived tail is [user, assistant "Part one of the plan is to "].

Goal: a turn that closing its chat cut off reads as interrupted when the chat is reopened, with the same notice and Resume control a restart gives.

Why it matters

The reopened chat presents half an answer as the whole answer, and the person has no prompt to resume it. #15461 asked for a notice when a chat with a running turn is archived; #18009 added only the confirmation before the close.

Not a goal

  • New UI. This is a choice reviewers can change: the cut-off turn is recorded exactly as a restart-interrupted turn is recorded today, so the existing notice and Resume control appear and nothing new is drawn.
  • Turns ended by the Stop button (a deliberate ending, unchanged), the turn ceiling, or a shutdown (the shutdown path is fix(dashboard): keep the streamed part of a reply when a shutdown cuts it off #18789, which merges cleanly with this one).

What changed

  • turn_marker.record_close_interruption (new, src/kiro_crew/dashboard/slot_persistence/turn_marker.py) lands one error row, msg msg-err with meta.kind = "chat_close_interruption" and the text "This turn was interrupted when its chat was closed. Review the partial output above, then resume to continue." It is the row the restart reconcile (_reconcile_local_turn_marker) lands, with its own wording, and it lands nothing when the transcript already reads as interrupted or the newest turn ends in the user's Stop card.
  • In _run_chat's CancelledError branch, after the partial reply is saved: when the cancel was not a Stop press and the slot is closing (slot.is_closing), the row is landed. Every close that cancels a running turn fences the slot with begin_close before it cancels: the single-tab close (_close_slot), the bulk archive of inactive tabs (api_chat_slots_cleanup, both cancel sites), and session_control.close_target, which reuses close_slot. So all three are covered by the one check.

Backwards compatibility

Compatible: a finished turn, a Stop press and a reset are unchanged. A chat closed mid-turn now ends with one more row, the interruption notice.

Removes nothing.

Tests

test/test_chat_close_interrupts_turn.py (new), adapted from an earlier graceful-shutdown reproducer. Its fake background session is now awaitable (get_bg_session is an AsyncMock), which removes the TypeError: object MagicMock can't be used in 'await' expression that auto-title logged.

  • test_a_turn_cut_by_closing_its_tab_reads_as_interrupted: the real turn, the real close route; the archived chat reads as interrupted, with exactly one chat_close_interruption row.
  • test_closing_a_tab_after_a_finished_turn_archives_it_unchanged: the control; no error row.
  • test_a_restart_interrupted_turn_is_recorded_as_before: the control; the restart reconcile still lands its own row.
  • test_no_close_row_when_the_turn_already_reads_as_interrupted_or_stopped: an unanswered opener and a Stop card get no row.

The busy-slot reset refusal (409) is covered by the existing test/test_chat_slot_reset_conversation.py, which passes unchanged.

Red: only the new test file copied onto main 843abe3, three runs, the same result each time:

FAILED test_a_turn_cut_by_closing_its_tab_reads_as_interrupted - AssertionError: a turn cut off by closing its tab is archived as a finished turn: [('user', 'explain the plan'), ('assistant', 'Part one of the plan is to ')]
FAILED test_no_close_row_when_the_turn_already_reads_as_interrupted_or_stopped - ImportError: cannot import name 'record_close_interruption' from 'kiro_crew.dashboard.slot_persistence.turn_marker'
2 failed, 2 passed

The first failure is the defect: the archived tail ends in a plain assistant row. The second is structural, since the helper does not exist on main; what it pins (no row after an unanswered opener or a Stop card) is checked on this branch. The two that pass on main are the finished-turn and restart controls.

Green: the new file, 4 passed, three runs; the 46 test files that name the close routes (api_chat_slot_delete, api_chat_slots_cleanup, close_slot), close_target, the turn marker, is_turn_interrupted or _persist_partial_reply, less two that resolve paths under the home directory: 3465 passed. mypy, flake8, isort, black, scripts/check_black_formatting.py, scripts/docs-lint.sh and scripts/check_comment_history.py (base origin/main) all pass.

Manual verification

None beyond the tests.

Related Issues

#15461 (asked for an archive notice), #18009 (added the close confirmation only).

Pattern harvest

Rule candidate: every path that cancels a running turn records why it ended, in the same row shape the transcript classifier already reads; a cancel that leaves only the partial text is indistinguishable from a finished turn.

Checklist

  • Red on main, green with the fix
  • Neighbour test files (test/test_service.py left out)
  • mypy, flake8, isort, black, black baseline gate, docs-lint, comment-history
  • No new I/O on the event loop (one in-memory row append)

@javenciu
javenciu requested review from a team and KiroCrewCaptain as code owners October 11, 2026 00:38
@javenciu
javenciu requested a review from CrysisDeu October 11, 2026 00:38
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 11, 2026
@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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

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

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of d63737e1ff060bddb0b7bd574faede2d46c1ca32 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

The new helper repeats _reconcile_local_turn_marker's tail guard and append (turn_marker.py:239-248). Check the two copies can't drift apart.

What this change ships

Inventory (3 items) — 3 justified

Intent: a chat that was closed mid-turn should reopen showing the turn as interrupted, with a Resume control, instead of showing half an answer as finished. This is a FIX.

  1. A chat closed while a turn is running now ends with an "interrupted when its chat was closed" notice and offers Resume when reopened. — justified
  2. A close after an unanswered opener, or after the user's own Stop card, adds no notice. — justified
  3. The new archived row has its own kind, chat_close_interruption, separate from the restart kind. — justified

[FIRST-PRINCIPLES-REVIEWED] d63737e

Description read

[DESCRIPTION-READ] 95825df04134247461d7888d556c4a41616a3d062629a0624fb38b216df6b5f0

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of d63737e1ff060bddb0b7bd574faede2d46c1ca32 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check whether docs/system-specs/modules/history.md should document the new persisted chat_close_interruption error row. Today it names only gateway_restart_interruption.

[DESIGN-REVIEWED] d63737e

Description read

[DESCRIPTION-READ] c330405804001a55444955e803013cf6f0d33911878bb3cb70ab7a258301ab33

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 Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

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

Review details

No findings.
[GPT-REVIEWED] d63737e

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] d63737e

Closing a tab while its turn runs cancels the turn, and the runner's
cancel branch keeps the streamed part of the reply as an ordinary
assistant row. Nothing followed it, so the chat reopened from history
read the cut-off reply as a finished answer, with no notice and no
Resume control.

When a close cancels the turn (the slot is fenced by begin_close: one
tab, the bulk archive, and close_target through close_slot), the
cancel branch now lands the same error row a restart lands, through
turn_marker.record_close_interruption, with its own wording and kind.
A Stop press, a turn that already reads as interrupted, and a finished
turn get no row.
@javenciu
javenciu force-pushed the fix/chat-close-records-cut-off-turn branch from a57d28c to d63737e Compare October 11, 2026 10:42
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 11, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a chat is closed mid-turn, the reopened chat currently shows the streamed partial reply as a finished answer, with no notice and no Resume. This PR lands a chat_close_interruption error row after the partial reply so is_turn_interrupted reads True, the same mechanism the restart reconcile already uses. That fixes the root cause without adding any UI. The new branch in _run_chat's CancelledError path fires only when _cancel_reason is not the Stop press (Stop yields STOP_REASON_CANCELLED, while an involuntary cancel yields "error: cancelled") and slot.is_closing is set. Every close path calls begin_close before it cancels: the single-tab close, both bulk-archive sites and close_target. I tried a Stop followed by a close, an unanswered opener, and a trailing error. record_close_interruption returns early on is_turn_interrupted or _latest_turn_was_deliberately_stopped, so none of them gets a duplicate row. The kind is only metadata, and no consumer special-cases the restart kind's value, so Resume works the same. The test drives the real _run_chat and the real DELETE route and reads the transcript back from a fresh ConversationLog. It fails on main. Optional: name the new kind next to gateway_restart_interruption in docs/system-specs/modules/history.md, as the Design lane suggested. — crew-pr-reviewer (deep re-check)

@iamwhatever
iamwhatever enabled auto-merge (squash) October 11, 2026 18:06
@iamwhatever
iamwhatever merged commit d647877 into kirodotdev:main Oct 11, 2026
104 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 11, 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.

2 participants