Repository navigation
fix(web): fold work before and after a mid-turn user message separately #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,6 +236,8 @@ export type MessagesTimelineRow = | |
| message: ChatMessage; | ||
| durationStart: string; | ||
| showAssistantMeta: boolean; | ||
| /** Draws the fold's separator above this message when a fold leads into it. */ | ||
| showsTurnFoldSeparator: boolean; | ||
| showAssistantCopyButton: boolean; | ||
| assistantCopyStreaming: boolean; | ||
| assistantTurnDiffSummary?: TurnDiffSummary | undefined; | ||
|
|
@@ -341,6 +343,8 @@ interface TurnFold { | |
| createdAt: string; | ||
| hiddenEntryIds: ReadonlySet<string>; | ||
| label: string; | ||
| /** The terminal assistant message this fold leads into, when it has one. */ | ||
| terminalEntryId: string | null; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】折叠记录里记下它通向哪条最终回复。上面那个布尔字段就是靠这个算出来的。没有最终回复的折叠(steer 之前的那一段)这里是 null,于是不画分隔线,符合"上方折叠只显示 Worked for X"。 |
||
| /** The group's direct-spawn CTA, carrying every direct spawn the fold absorbed. */ | ||
| spawnRow: { entryId: string; entry: WorkLogEntry } | null; | ||
| } | ||
|
|
@@ -395,6 +399,8 @@ function workEntryIsActiveTurnActivity(entry: WorkLogEntry): boolean { | |
| * Settled turns keep only their terminal assistant message visible. | ||
| * Everything before it folds behind a "Worked for ..." row anchored at the | ||
| * first hidden entry, so the duration leads directly into the final response. | ||
| * A user message that lands mid-turn (a steer) splits the turn into segments | ||
| * that fold separately, so each fold stays above the work it covers. | ||
|
Comment on lines
+402
to
+403
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】只是把函数说明补一句:一个回合里出现 steer 时,会按 steer 前后分成几段,各自折叠。 |
||
| */ | ||
| function deriveTurnFolds(input: { | ||
| timelineEntries: ReadonlyArray<TimelineEntry>; | ||
|
|
@@ -405,6 +411,8 @@ function deriveTurnFolds(input: { | |
| interface TurnGroup { | ||
| /** The turn id that opened the group; keeps the fold's identity stable. */ | ||
| turnId: TurnId; | ||
| /** Opened by a steer, so it cannot read the turn's own start time. */ | ||
| steered: boolean; | ||
|
Comment on lines
+414
to
+415
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】分组多一个布尔:这一段是不是由 steer 开出来的。它只在算时长时用:由 steer 开出来的段,起点是 steer 那条消息,不能再用回合本身的开始时间。 |
||
| entries: Array<TimelineEntry>; | ||
| terminalEntry: Extract<TimelineEntry, { kind: "message" }> | null; | ||
| hasStreamingMessage: boolean; | ||
|
|
@@ -416,6 +424,8 @@ function deriveTurnFolds(input: { | |
| */ | ||
| startBoundary: string | null; | ||
| } | ||
| // Every segment in timeline order, plus the one each turn id writes into now. | ||
| const groups: Array<TurnGroup> = []; | ||
|
Comment on lines
+427
to
+428
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】新增一个按时间线顺序排的分组列表。原来只有一张"回合 id → 分组"的表,一个回合拆成两段之后,后一段会把前一段从表里顶掉,前一段就再也折叠不到了。这个列表保证每一段都会被折叠。表本身保留,用来找"这个回合当前正在写入哪一段"。 |
||
| const groupsByTurnId = new Map<TurnId, TurnGroup>(); | ||
|
|
||
| let pendingUserBoundary: string | null = null; | ||
|
|
@@ -435,20 +445,32 @@ function deriveTurnFolds(input: { | |
| // group, even past the next user message, since no new response has | ||
| // started until a turn id shows up. | ||
| let group: TurnGroup | null = turnId ? (groupsByTurnId.get(turnId) ?? null) : lastGroup; | ||
| // The turn read a steer and kept working: close the segment it interrupted | ||
| // so that fold stays above the work it covers, and open a new one below the | ||
| // steer. Trade-off: a group that already holds its response is done talking, | ||
| // so a late row is residue and rides it instead of splitting. | ||
| const steeredGroup: TurnGroup | null = | ||
| group?.terminalEntry === null && turnId && pendingUserBoundary !== null ? group : null; | ||
| if (steeredGroup) group = null; | ||
|
Comment on lines
+448
to
+454
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】这就是拆分本身,整个 PR 的核心判断。条件:这一行属于某个回合、它当前的段还没有出现最终回复、而且中间来过一条用户消息(steer)。满足就把当前段关掉,后面的行落到新段里。 取舍写在注释里:如果当前段已经有了最终回复,说明回合已经说完话,之后再来的行是后台残留(比如迟到的工具完成记录),不拆,跟着原来的段走。Codex 机器人提的第三条(被中断、从没回复过的旧回合的残留行会被当成 steer)就是这个取舍的另一面:不往前看时间线没法区分,作为已知限制写在 PR 正文里。 |
||
| if (!group) { | ||
| if (!turnId) { | ||
| continue; | ||
| } | ||
| // A fresh turn id with no user message since the last group opened is a | ||
| // synthetic mid-response continuation (subagent wake-up, stop-hook | ||
| // retry): it joins that group instead of starting a fold of its own. | ||
| group = (pendingUserBoundary === null ? lastGroup : null) ?? { | ||
| turnId, | ||
| const joinedGroup: TurnGroup | null = pendingUserBoundary === null ? lastGroup : null; | ||
| group = joinedGroup ?? { | ||
| // A split segment keeps the interrupted group's turn id, so both of | ||
| // the turn's folds still expand together. | ||
| turnId: steeredGroup?.turnId ?? turnId, | ||
| steered: steeredGroup !== null, | ||
| entries: [], | ||
| terminalEntry: null, | ||
| hasStreamingMessage: false, | ||
| startBoundary: pendingUserBoundary, | ||
| }; | ||
| if (!joinedGroup) groups.push(group); | ||
|
Comment on lines
+462
to
+473
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】新开一段时的三处改动:(1) |
||
| pendingUserBoundary = null; | ||
| groupsByTurnId.set(turnId, group); | ||
| lastGroup = group; | ||
|
|
@@ -465,7 +487,7 @@ function deriveTurnFolds(input: { | |
| } | ||
|
|
||
| const foldsByAnchorEntryId = new Map<string, TurnFold>(); | ||
| for (const group of new Set(groupsByTurnId.values())) { | ||
| for (const group of groups) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】折叠时改成遍历有序列表,而不是表里的值。原因同上:表里每个回合只剩最后一段。 |
||
| const { turnId } = group; | ||
| if (input.unsettledTurnId !== null && groupsByTurnId.get(input.unsettledTurnId) === group) { | ||
| continue; | ||
|
|
@@ -517,20 +539,20 @@ function deriveTurnFolds(input: { | |
| const isLatestInterruptedTurn = | ||
| input.latestTurn?.state === "interrupted" && | ||
| groupsByTurnId.get(input.latestTurn.turnId) === group; | ||
| // The turn's own clock, when this group opened under the latest turn. | ||
| const ownTurn = input.latestTurn?.turnId === turnId ? input.latestTurn : null; | ||
|
Comment on lines
+542
to
+543
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】把"这个分组属于最新那个回合"的判断提出来,下面两处都用;提出来之后下面的表达式就短了。 |
||
| // A turn cut short by a steer leaves trailing work entries behind its | ||
| // terminal message — take whichever ended last. | ||
| const lastEntryEnd = | ||
| lastEntry.kind === "message" ? lastEntry.message.updatedAt : lastEntry.createdAt; | ||
| const elapsedMs = | ||
| input.latestTurn?.turnId === turnId && | ||
| input.latestTurn.startedAt && | ||
| input.latestTurn.completedAt | ||
| ? computeElapsedMs(input.latestTurn.startedAt, input.latestTurn.completedAt) | ||
| : computeElapsedMs( | ||
| group.startBoundary ?? firstEntry.createdAt, | ||
| maxIsoTimestamp(group.terminalEntry?.message.updatedAt ?? null, lastEntryEnd) ?? | ||
| lastEntryEnd, | ||
| ); | ||
| // Only the segment that opened the turn may read its start, and only the | ||
| // one that closed it may read its completion; a steer bounds the rest. | ||
| const elapsedMs = computeElapsedMs( | ||
| (group.steered ? null : ownTurn?.startedAt) ?? group.startBoundary ?? firstEntry.createdAt, | ||
| (groupsByTurnId.get(turnId) === group ? ownTurn?.completedAt : null) ?? | ||
| maxIsoTimestamp(group.terminalEntry?.message.updatedAt ?? null, lastEntryEnd) ?? | ||
| lastEntryEnd, | ||
| ); | ||
|
Comment on lines
+548
to
+555
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】时长计算改写。原来:如果是最新回合,就直接用回合的开始和结束时间;否则用条目时间。拆成几段之后不能整段照搬:只有开回合的那段( |
||
| const duration = elapsedMs !== null ? formatDuration(elapsedMs) : null; | ||
| const label = isLatestInterruptedTurn | ||
| ? duration | ||
|
|
@@ -546,6 +568,7 @@ function deriveTurnFolds(input: { | |
| createdAt: firstHiddenEntry.createdAt, | ||
| hiddenEntryIds, | ||
| label, | ||
| terminalEntryId: group.terminalEntry?.id ?? null, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】把最终回复的 id 写进折叠记录,见上面 |
||
| spawnRow, | ||
| }); | ||
| } | ||
|
|
@@ -580,7 +603,13 @@ export function deriveMessagesTimelineRows(input: { | |
| }); | ||
| const collapsedEntryIds = new Set<string>(); | ||
| const mergedSpawnEntries = new Map<string, WorkLogEntry>(); | ||
| // A fold's separator rides the response it leads into, so it lands below any | ||
| // spawn row the fold kept visible. | ||
| const foldTerminalEntryIds = new Set<string>(); | ||
| for (const fold of foldsByAnchorEntryId.values()) { | ||
| if (fold.terminalEntryId !== null) { | ||
| foldTerminalEntryIds.add(fold.terminalEntryId); | ||
| } | ||
|
Comment on lines
+606
to
+612
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】收集所有"有折叠通向它"的最终回复 id,下面给消息行打分隔线标记时查一下。放在这个已有的循环里,不另起循环。 |
||
| if (!input.expandedTurnIds?.has(fold.turnId)) { | ||
| for (const entryId of fold.hiddenEntryIds) { | ||
| collapsedEntryIds.add(entryId); | ||
|
|
@@ -704,7 +733,8 @@ export function deriveMessagesTimelineRows(input: { | |
| if (anchoredTurnFold) { | ||
| nextRows.push({ | ||
| kind: "turn-fold", | ||
| id: `turn-fold:${anchoredTurnFold.turnId}`, | ||
| // Keyed by anchor, not by turn: a steered turn folds once per segment. | ||
| id: `turn-fold:${anchoredTurnFold.anchorEntryId}`, | ||
|
Comment on lines
+736
to
+737
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】折叠行的 id 原来是 |
||
| createdAt: anchoredTurnFold.createdAt, | ||
| turnId: anchoredTurnFold.turnId, | ||
| label: anchoredTurnFold.label, | ||
|
|
@@ -858,6 +888,7 @@ export function deriveMessagesTimelineRows(input: { | |
| message: timelineEntry.message, | ||
| durationStart, | ||
| showAssistantMeta, | ||
| showsTurnFoldSeparator: foldTerminalEntryIds.has(timelineEntry.id), | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】给消息行填上分隔线标记。 |
||
| showAssistantCopyButton: showAssistantMeta, | ||
| assistantCopyStreaming: timelineEntry.message.streaming || assistantTurnStillInProgress, | ||
| assistantTurnDiffSummary: | ||
|
|
@@ -962,6 +993,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean | |
| a.message === bm.message && | ||
| a.durationStart === bm.durationStart && | ||
| a.showAssistantMeta === bm.showAssistantMeta && | ||
| a.showsTurnFoldSeparator === bm.showsTurnFoldSeparator && | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【说明】行复用比较里加上新字段,否则标记变了行也不会重画。 |
||
| a.showAssistantCopyButton === bm.showAssistantCopyButton && | ||
| a.assistantCopyStreaming === bm.assistantCopyStreaming && | ||
| a.assistantTurnDiffSummary === bm.assistantTurnDiffSummary && | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
【给审查者的说明】消息行多一个布尔字段:这条最终回复上面要不要画分隔线。原来分隔线画在折叠行底部,会落在"Ran N subagents"行的上面;你要求的顺序是 折叠行、subagents 行、分隔线、最终回复,所以分隔线要挪到最终回复的顶部。渲染层一次只看一行,不知道上面有没有折叠,必须由这里算好告诉它。