Skip to content

fix: group a turn whose anchor falls outside the first history page - #941

Merged
agegr merged 1 commit into
agegr:mainfrom
uvforce:pr/paginated-turn-grouping
Sep 30, 2026
Merged

agegr merged 1 commit into
agegr:mainfrom
uvforce:pr/paginated-turn-grouping

Conversation

@uvforce

@uvforce uvforce commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A turn longer than the first history page renders completely flat: every thinking block and tool call is laid out inline, with no "Process details" group to collapse, even though the turn ended with a normal final answer.

app/api/sessions/[id] serves the most recent 50 visible entries by default, and loadSession() does not override tail. When a single turn contains more than 50 entries, the first page starts in the middle of it and the leading user message is simply not in messages.

The render loop then hits this branch for every message in that page:

if (!isMessageGroupAnchor(msg)) {
  rendered.push(renderMessage(idx));
  idx += 1;
  continue;
}

With no anchor anywhere in the page, the turn never reaches the grouping path, so ProcessDetailsGroup is never constructed.

Reproduction

  1. Open a session whose most recent turn has more than 50 entries (a long agentic turn with many tool calls).
  2. The whole turn is rendered inline — every tool call as its own card, every assistant message with its own model label and usage line.
  3. Reloading does not help: each load serves the same 50-entry window.

On a real 5818-entry session here, the latest user message sits 100 entries from the end, and the first page contains 24 assistant + 24 toolResult + 2 custom messages and zero anchors.

Before (current main, a 60-tool-call turn — the page keeps scrolling like this all the way up):

before

After (this branch, same session):

after

Fix

Treat the anchorless leading segment as its own group instead of flattening it.

findFinalAssistantIndex already scans back to userIdx + 1, so passing -1 covers index 0 without touching that helper. groupStartIdx keeps the flat-render fallbacks and the group key pointing at the first loaded message, and the user bubble is only pushed when the anchor is actually present.

Loading is untouched — still 50 entries — so there is no extra transfer or parse cost. The grouped page renders fewer DOM nodes than before, because the process messages now sit inside a collapsed group.

I considered widening the initial window server-side instead, but measured it as clearly worse: extending tail to cover a full turn grew the first-page payload 2.8x–7.5x across local sessions (183 KiB to 556 KiB, 253 KiB to 711 KiB, 113 KiB to 842 KiB), while the client-side fix costs nothing.

Verification

  • components/ChatWindow.process-details.test.mjs — new case for the paginated leading segment.
  • npm test, tsc --noEmit, npm run lint — clean on this branch merged onto current main (v0.9.3).
  • Running in daily use on a production build since 2026-09-22; the previously flat turns now render as a collapsed "Process details" group, and the screenshots above come from a synthetic 60-tool-call session against main and this branch.

The session detail route serves the most recent 50 entries by default, so a
turn longer than that page is delivered without its leading user message. The
render loop treats every message before the first anchor as standalone and
pushes it directly, which skips the process-details grouping for that turn:
the whole turn renders flat, with no collapsible group, even though it has a
final answer.

Group the anchorless leading segment as well. findFinalAssistantIndex already
scans back to userIdx + 1, so passing -1 covers index 0, and groupStartIdx
keeps the render range and the group key pointing at the first loaded message.

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Reviewed. The bug is real: GET /api/sessions/[id] serves the last 50 entries by default, so a turn longer than that page arrives without its user message and the old loop rendered the whole turn flat.

The fix is right as written:

  • findFinalAssistantIndex(messages, -1, endIdx) scans down to index 0, and the process loop and written-files scan start at userIdx + 1 = 0, so the leading segment is grouped whole.
  • A live turn with no anchor on the page still hits isLiveTail (-1 === lastAnchorIdx) and stays flat while streaming.
  • Once older history loads, the anchor shows up and the group re-keys onto it.

Merged locally with current main (clean): tsc --noEmit, eslint, and the full unit suite (1324/1324) pass. LGTM 👍


Generated by Claude Code

@agegr
agegr merged commit 69882b9 into agegr:main Sep 30, 2026
2 checks passed
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…#907 agegr#941 agegr#946 agegr#961 agegr#968 agegr#969 agegr#1007 agegr#1008 agegr#1009 agegr#1010)

上游已把我们提前摘取的 agegr#884 / agegr#871 / agegr#996 / agegr#961 原样合并(agegr#961 逐字节相同),
agegr#967 与 agegr#963 由上游的 agegr#1009 / agegr#1008 接管。

冲突解决:
- demo/ 对齐上游 fe288c0,撤销 agegr#963 对 demo 的两处改动(`@/lib/chat-phase-label`
  在 demo/lib 下不存在,会打断 Pages 构建)。
- AGENTS.md:保留我们的 default-project 条目,open-folder 换成上游的
  open-in-explorer。
- MessageView.tsx / SessionSidebar.tsx / FileExplorer.tsx:以上游为准,
  DismissButton 采用上游的独立文件,保留我们的思考卡片与文件管理改动。
- SessionSidebar 移除引用已删函数的旧 agegr#951 工作区按钮(agegr#951 的其他入口另行收尾)。
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 6, 2026
…gegr#941)

The session detail route serves the most recent 50 entries by default, so a
turn longer than that page is delivered without its leading user message. The
render loop treats every message before the first anchor as standalone and
pushes it directly, which skips the process-details grouping for that turn:
the whole turn renders flat, with no collapsible group, even though it has a
final answer.

Group the anchorless leading segment as well. findFinalAssistantIndex already
scans back to userIdx + 1, so passing -1 covers index 0, and groupStartIdx
keeps the render range and the group key pointing at the first loaded message.

Co-authored-by: wayne.luo <[email protected]>
windli2018 pushed a commit to aiti-ai/aiti-ai that referenced this pull request Nov 8, 2026
…gegr#941)

The session detail route serves the most recent 50 entries by default, so a
turn longer than that page is delivered without its leading user message. The
render loop treats every message before the first anchor as standalone and
pushes it directly, which skips the process-details grouping for that turn:
the whole turn renders flat, with no collapsible group, even though it has a
final answer.

Group the anchorless leading segment as well. findFinalAssistantIndex already
scans back to userIdx + 1, so passing -1 covers index 0, and groupStartIdx
keeps the render range and the group key pointing at the first loaded message.

Co-authored-by: wayne.luo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants