Skip to content

fix: keep rows pushed while a chat's open page loads - #47879

Open
winnythedev wants to merge 2 commits into
mainfrom
fix/47857-switch-keeps-live-rows
Open

winnythedev wants to merge 2 commits into
mainfrom
fix/47857-switch-keeps-live-rows

Conversation

@winnythedev

@winnythedev winnythedev commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A chat opened, or switched back to, while its turn is running shows every message segment and tool card the gateway sent while the chat was loading.

When a person opens a chat whose turn is running, the text written just before a tool call and that call's card can go missing. They stay missing until the turn ends or the person switches away and back. Sometimes the text from before and after the call runs together in one bubble with no tool card between. It happens whenever a tool call starts while the chat's first page is loading (about 57 ms on a local gateway, longer over a slow link).

Why it matters

Anyone who opens or switches into a busy chat can lose the record of what the agent did: the tool card that says what ran and the text that led up to it. The view looks complete, so nothing tells the person a part is missing, and on a slow or remote link the window is wide enough to hit often.

Not a goal

  • refreshSlot, which already has its own rescue for rows that race its read.
  • The split and background panes (warmSlotCache, hydrateSlotMessages), which merge with their own rules.
  • Permission and elicitation rows, which have their own handling in this reducer.
  • Any server change.
  • A queued bubble that lands below the live reply during the read. The reply is then not the trailing row, so the chunks it took during the read are not kept, as on main. The segment and tool rows of that window are kept.

What changed (motivation → approach → change)

  • Symptom. Opening a chat mid-turn drops the finalized segment and tool row that arrived while its page was loading.
  • Root cause. switchSlot reads one page, then switchSlot.fulfilled replaces the view with that page. A page is read at one instant. The rows the gateway pushed during the read reach the view through live frames but are not on the page. The reducer kept only the person's own sends (liveSends) and the trailing reply, so every other server row from that window was dropped. The page also still folds the flushed segment's text into its streaming row, which is how the text before and after the tool call ended up in one bubble.
  • Change (website/src/store/chat/slotSwitch.ts):
    • The thunk records the row identities the view held when the switch began (idsAtDispatch), as refreshSlot already does.
    • rowsLandedDuringFetch picks the server rows (meta.mid) that a live frame added after that and that the page does not carry (tailNotInPage). Like liveSendsSince, it looks only below the last row that was on screen at dispatch, skipping queued bubbles, so older rows another read wrote into the view stay above the page.
    • The reducer puts those rows with the live sends, in view order, ahead of the trailing reply. When a live segment replaces the text the page still folds, the page's streaming row is dropped.
    • An idle page that lacks such a row was read before the turn that wrote it, so the chat keeps the run state that turn set, as it already did for a live send.
    • A row the view already held when the switch began is left to the page, so a row the server has since dropped is not carried back.
    • The page rebuilds the meta of a row with a JSON cls from that cls alone, so its copy has no meta.mid (inject rows keep theirs). A stop card of that kind is matched by its meta.id; any other JSON-cls row is left to the page, as on main.
  • Spec. docs/system-specs/modules/history.md, "Bounded reads", gains the rule.

Backwards compatibility

Compatible: only rows that live frames already put on screen are kept; a page that carries them, or a switch with no live frames during the read, lands as before.

Removes nothing: the change keeps rows the reducer used to drop and takes no path, record, key or function away.

Tests

website/src/store/chatSlice.switchLiveRows.test.ts drives the real chat slice reducers with the frames the server writes: the pre-tool segment flush as an assistant chat_message with meta.mid, the tool chat_message after it, chunks with seq and gen, and a page whose open segment is folded into one streaming row.

  • Keeps a finalized segment and its tool row beside the live reply, and the next chunk appends to the live row.
  • Keeps the segment and the tool row when the tool row is the newest row.
  • Keeps them on a first open with no cached rows.
  • Draws no row twice when the page already carries it.
  • Does not carry back a cached row the page no longer holds.
  • Keeps the live rows when a queued send was on screen before the switch.
  • Leaves older rows another read wrote meanwhile above the page.
  • Keeps the run state a live row shows when the page reads the chat idle.
  • Draws a stop card the page also holds once, and a later settlement frame updates that one card.
  • Keeps a stop card pressed after the page was read.
  • Leaves another JSON-cls row to the page, which holds it without its mid.

Proof: not provable -- frontend test (prove.py runs Python only)

By hand instead: on main 4fe9089 the first three cases fail (expected [ 'user:q', 'streaming:f' ] to deeply equal [ 'user:q', 'assistant:abcde', 'tool:ls', 'streaming:f' ], and twice expected [ 'user:q', 'streaming:abcde' ] to deeply equal [ 'user:q', 'assistant:abcde', 'tool:ls' ]). The queued-send, older-rows and idle-page cases each fail when their own part of the fix is taken out (the queued skip in the walk, the walk's bound, the run-state guard). On head fd93488 the stop-card-on-page and other-JSON-row cases fail (expected [ 'stopping', 'stopping' ] to deeply equal [ 'stopping' ]); leaving every JSON-cls row out without the meta.id match fails the pressed-after-read case.

Determinism: the file passes 20 of 20 runs with --sequence.shuffle and seeds 1 to 20. The tests use no timers, sleeps or Date; the page race is injected synchronously inside the mocked read, and beforeEach resets the module state.

Other runs on this head: 274 related frontend files (5,142 tests: the 163 files naming switch, refresh, warm or streaming, all of src/store and src/lib), CarriedMockAnswerRatchet and the chatSlice.ownership guard, test/test_flake_pattern_ratchet.py (81 passed), eslint on the two changed files, tsc -p tsconfig.app.json --noEmit, scripts/docs-lint.sh, check_comment_history.py, check_brand_name.py, check_file_line_cap.py, and semgrep scan --config semgrep/ --baseline-commit <base> --error (1.78.0, 0 findings).

scripts/local-gate.py --tree-scans was started but had not finished when this was pushed: the docs change pulls in about 1,600 cross-surface backend test files. Its backend half is left to CI.

Review (multi-model interrogate)

Independent reviewers read the full diff against CI's lane contracts.

  • Found: a queued bubble landing below the live reply during the read lost the streaming row (Medium); the scan for live rows was not bounded below the last row on screen at dispatch (Medium); an idle page read before a turn started elsewhere could idle the chat under a kept tool row (Low).
  • Acted on: the unbounded scan and the run state are fixed, each with its own test.
  • The queued case was narrowed out (see Not a goal) after two attempts each drew new Medium findings in re-checks: finding the reply above the queue re-attached a stale partial from an earlier turn once the queue had drained, and keeping the row the read's chunks went to could pick that same partial or draw two streaming rows. On that input this PR keeps the segment and tool rows, and the raced chunks are lost as on main. A shortcut: comment marks the limit.
  • Dismissed: a note that a user row sent from another surface (no sendId) during the read is still dropped. That is outside this PR's goal and unchanged from main.
  • CI's GPT lane then found that a stop card pressed during the load was drawn twice, because the page's copy carries no meta.mid. Fixed in a second commit: stop cards are matched by meta.id, other JSON-cls rows are left to the page, with three tests.
  • Deferred: the First Principles note that idsAtDispatch is a second record, beside _switchLiveFrom, of what was on screen when the switch began. Folding the two is a refactor of liveSends outside this fix.
  • Two reviews were not counted because their reviewer misreported the length of AGENTS.md.
  • The last re-check ran on the head just before the narrowing. The pushed head only removes the hunk it flagged and the test for it, and adds the shortcut: comment.

Manual verification

N/A -- unit coverage sufficient: the defect and the fix live in one reducer, and the tests drive it with the frames the server sends. No live dashboard run was done, and no gateway was started.

Real-machine verification

Machine: Linux x86_64 developer host, Node with the repository's pinned website dependencies (npm ci).
Before (main 4fe9089): cd website && NO_COLOR=1 npx vitest run src/store/chatSlice.switchLiveRows.test.ts --coverage.enabled=false -> the first three cases fail as quoted under Tests.
After (head): the same command -> Tests 8 passed (8).
Cleanup: own computer; vitest only, on the real chat slice reducers. No browser run, no gateway and no dashboard token were used.

Screenshots / video

Why no screenshot: The change only stops a chat from dropping rows during its first page load; no layout, style or component changes, and the rows look as they already do.

Related Issues

Fixes #47857

Sibling sweep

Swept: git grep -n "tailNotInPage\|idsAtDispatch" -- website/src/store/chat/
Sibling: website/src/store/chat/slotRefresh.ts -- not affected -- refreshSlot already keeps raced sends and the raced streaming reply through liveSendsSince and racedStreamingRows
Sibling: website/src/store/chat/slotSwitch.ts -- changed here -- JSON-cls rows whose page copy has no mid: stop cards matched by meta.id, the rest left to the page (swept with git grep -n "slot.append(" -- src/kiro_crew/dashboard/)
Sibling: website/src/store/chat/messages.ts -- not affected -- hydrateSlotMessages and warmSlotCache (slotRefresh.ts) merge a background pane by their own rules, out of this PR's scope

Pattern harvest

Not generalizable: the defect is one reducer replacing the view with a page read before frames it already applied; the sibling reads already guard the same race.

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • 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

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

Opening a chat reads one page of its transcript, then replaces the
view with that page. A page is read at one instant, so the rows the
gateway pushed during the read (a finalized segment, a tool row, a
card) reached the view through live frames but not the page. The
switch kept only the person's own sends and the trailing reply, so the
text before a tool call and that call's card vanished until the turn
ended, or the text before and after the call ran into one bubble.

switchSlot now records the row identities the view held when the
switch began, and its fulfilled reducer keeps the server rows a live
frame added after that which the page does not carry, in view order,
ahead of the trailing reply. A row the view already held is left to
the page, so a row the server since dropped is not carried back.
An idle page that lacks such a live row was read before the turn that
wrote it, so the chat keeps the run state that turn set.

Fixes #47857
@winnythedev
winnythedev requested review from a team and KiroCrewCaptain as code owners December 5, 2026 19:02
@winnythedev
winnythedev requested a review from buluoray December 5, 2026 19:02
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of 4cafa8c653dec1e695ce45efcbd5f0cb51994953 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

UX-Verdict: PASS

Nothing to check. The diff changes only a store reducer, a spec and tests. It adds or changes no control, label or string, so there are no evidence gaps despite the blind read not being performed.

[UX-REVIEWED] 4cafa8c

Description read

[DESCRIPTION-READ] b84f52cc48d1f2dbcdbbb2bc0e7d7583ac7872d7e65c9562094fd07d24c52d6e

That sha256 names everything this verdict read: the PR description, plus the 3 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 Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 4cafa8c653dec1e695ce45efcbd5f0cb51994953 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks. The fix keeps only rows a live frame added after dispatch, using the same rules refreshSlot and liveSendsSince already use. It removes nothing and adds no new surface, so no screenshot is required.

Notes

  • The stop-card and JSON-cls special cases in carriesOverPage exist because the page rebuilds meta from cls and drops meta.mid. Keeping mid on every page row server-side would remove that matching code and the "left to the page" gap. That belongs in a follow-up, not this PR.
  • rowsLandedDuringFetch now repeats the bounded below-last-on-screen walk already in liveSendsSince. Sharing one helper would stop the two walks from drifting apart when the next queued-bubble edge case is fixed in only one of them.

[DESIGN-REVIEWED] 4cafa8c

Description read

[DESCRIPTION-READ] 88d96a7faddd5a102ae589fce96df0ecda31173ac9b7b34799245b76ef48f265

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 Dec 5, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of fd934884d5ecfd7812805ef59ee354dd270ac032 — 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 does not.

First-Principles-Verdict: PASS

This fixes a defect a new test proves: three of its cases fail on base. The note below is about a second record of "what was on screen when the switch began" kept in the same reducer. It is not a duplicate that blocks.

Notes

  • Item 2 — one question answered twice: switchSlot already records where live frames start as _switchLiveFrom, an index set in pending (slotSwitch.ts:592) and used for liveSends. This PR adds a second record of the same moment, idsAtDispatch, and walks back to it the way liveSendsSince does (slotRefresh.ts:92). The same reducer now answers one question two ways. Subtraction: once the ID-based walk is in, have liveSends use it too (it also survives older rows being added above the page) and delete _switchLiveFrom and the code in messages.ts:246,276 that keeps it in step. Or defer that.

What this change ships

Inventory (5 items) — 4 justified

Intent: someone who opens or returns to a chat while a turn is running sees every segment and tool card the gateway sent while the page was loading. FIX.

  1. A finalized segment and tool card that arrive while the open page loads stay on screen. — justified
  2. The open records which rows were on screen when it began (idsAtDispatch in the thunk payload). — duplicate of _switchLiveFrom in slotSwitch.ts
  3. When a live segment arrived, the page's folded streaming bubble is dropped, so text from before and after a tool call no longer merges into one bubble. — justified
  4. A chat whose page reads idle stays running when such a live row landed during the load. — justified
  5. The "Bounded reads" section of history.md gains the rule. — justified

[FIRST-PRINCIPLES-REVIEWED] fd93488

Description read

[DESCRIPTION-READ] e31331ace4bc65aa09bf25bbe740ee749bf59e7444ef444b5e52e4f20b617525

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 Dec 5, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed fd934884d5ecfd7812805ef59ee354dd270ac032 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] fd93488

Verdict parsed from the review's SHA-scoped output markers for commit fd934884d5ecfd7812805ef59ee354dd270ac032.

False positive or not applicable? A repository writer can comment:
/ai-review override fable fd934884d5ecfd7812805ef59ee354dd270ac032 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — 🔴 changes requested (blocking)

GPT 6.1 found at least one blocking issue that must be resolved before merging fd934884d5ecfd7812805ef59ee354dd270ac032. Adjudication upheld 1 of 1 blocking finding(s); those must be resolved before merging.

This comment is updated in place on each push.

BLOCKING -- website/src/store/chat/slotSwitch.ts:226 -- Overlapping live and page reads duplicate stop cards
return tailNotInPage(
Trigger: Stop is pressed while switchSlot loads, before its page is captured. _open_stop_event_card → slot.append → chat_message_note delivers a live card with meta.mid, but _prepare_messages_scoped replaces its history metadata with the JSON cls metadata, omitting mid. tailNotInPage therefore retains the card even though the page contains it.
Outcome: The merge displays two stop cards. A subsequent settlement updates only the first matching meta.id in applyActiveFrame, leaving the duplicate showing “Stopping” until another refresh.
Anchor: defect
Fix: Recognize stop-event meta.id when excluding live rows already represented on the page.

[BLOCK-MERGE] fd93488
[GPT-REVIEWED] fd93488

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

I'm upholding F1, so it keeps blocking the merge. The harm is LOW to MEDIUM, but the fix is cheap.

F1 — what goes wrong is visible but recoverable: the pane shows two stop cards, and one stays stuck at "Stopping" until the next refresh. I confirmed the conditions in code:

  • The live card carries a row id. src/kiro_crew/dashboard/chat_api/turn_stop.py:246 opens it with slot.append, which stamps mid.
  • The page copy loses that id. src/kiro_crew/dashboard/chat_utils_display.py:1404-1419 replaces stored meta with the cls meta and keeps mid only for permission, elicitation and inject rows, so a system stop card arrives without it.
  • The live copy is kept anyway. website/src/store/chat/slotSwitch.ts:226-230 keeps non-user rows that have a mid and are new since dispatch. tailNotInPage and rowIdentities (website/src/store/chat/transcript.ts:766-785) match rows only by mid or sendId, so they never recognise the page's copy.
  • Recovery: none until the next refresh.

The real fix is small: also match stop cards by meta.id inside rowsLandedDuringFetch. It needs no new state and no coordination between modules, so it costs far less than the harm it prevents. The defect is also in a row this PR adds.

[ADJUDICATION] fd934884d5ecfd7812805ef59ee354dd270ac032 total=1 uphold=1 downgrade=0
UPHOLD F1 website/src/store/chat/slotSwitch.ts:226 reason=harm-warrants-remedy
[GPT-ADJUDICATED] fd934884d5ecfd7812805ef59ee354dd270ac032

False positive or not applicable? A repository writer can comment:
/ai-review override gpt fd934884d5ecfd7812805ef59ee354dd270ac032 false-positive: <reason, 20 to 500 characters>

The page rebuilds the meta of a row with a JSON cls from that cls
alone, so its copy of a stop card carries no meta.mid. A stop card
pressed while the chat's page loaded was therefore kept beside the
page's own copy: two cards, one stuck at Stopping. Such a row is now
matched by its meta.id, and any other JSON-cls row is left to the page.
@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 Dec 5, 2026
@winnythedev

Copy link
Copy Markdown
Contributor Author

self-added: yes
mechanism: rowsLandedDuringFetch carried live rows the page could not match by mid

  • Overlapping live and page reads duplicate stop cards span=35334f559888

fixed in 4cafa8c: a live row with a JSON cls (its page copy has no meta.mid) is kept over the page only when it is a stop card the page does not hold by meta.id; any other JSON-cls row is left to the page. Inject rows keep their mid on the page and stay matched by it.
Tests: one stop card when the page holds it, and a later settlement frame updates that card; a stop card pressed after the page was read is kept; another JSON-cls row is drawn once.

@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 Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: checking Automated validation is still running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat: a chat opened mid-turn can lose the text and tool card pushed while its page loads, until the turn ends

1 participant