Skip to content

Commit cfd1c95

Browse files
committed
fix(clients): collapsed turns show one context-compacted divider
A finished turn that compacted more than once kept every compaction divider visible when folded, stacked under the Worked for row and reading as one duplicated line. A folded turn now keeps only its latest divider; earlier ones fold with the turn's other work and reappear in place when expanded. Single-compaction turns are unchanged. Fixes #33825
1 parent ed52af3 commit cfd1c95

4 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,43 @@ it.each(["/compact", "Continue"])(
248248
},
249249
);
250250

251+
it("keeps only the latest compaction divider visible when a turn that compacted twice folds", () => {
252+
const compaction = (id: string, ordinal: number, beforeTokenCount: number) => ({
253+
...base(id, "2026-06-20T00:00:03.000Z", ordinal),
254+
type: "compaction" as const,
255+
driver: ProviderDriverKind.make("claude"),
256+
status: "completed" as const,
257+
beforeTokenCount,
258+
afterTokenCount: 20_000,
259+
});
260+
const feed = buildThreadFeed([
261+
projected(userMessage(), 0),
262+
projected(compaction("first-compaction", 1, 100_000), 1),
263+
projected(command(), 2),
264+
projected(compaction("second-compaction", 3, 90_000), 3),
265+
]);
266+
const latestRun = {
267+
runId,
268+
status: "completed" as const,
269+
startedAt: "2026-06-20T00:00:01.000Z",
270+
completedAt: "2026-06-20T00:00:03.000Z",
271+
};
272+
const compactionsIn = (rows: ReadonlyArray<ThreadFeedEntry>) =>
273+
rows.flatMap((entry) =>
274+
entry.type === "activity-group"
275+
? entry.activities.flatMap(({ projectedItem: { item } }) =>
276+
item.type === "compaction" ? [item.beforeTokenCount] : [],
277+
)
278+
: [],
279+
);
280+
const collapsed = deriveThreadFeedPresentation(feed, latestRun, new Set());
281+
expect(collapsed.filter((entry) => entry.type === "run-fold")).toHaveLength(1);
282+
expect(compactionsIn(collapsed)).toEqual([90_000]);
283+
expect(compactionsIn(deriveThreadFeedPresentation(feed, latestRun, new Set([runId])))).toEqual([
284+
100_000, 90_000,
285+
]);
286+
});
287+
251288
it("binds a persisted resume answer only to the next compaction in its run", () => {
252289
const requestId = RuntimeRequestId.make("resume-compaction");
253290
const question = projected(

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,14 +1152,18 @@ function deriveThreadFeedRunFolds(
11521152
continue;
11531153
}
11541154
const terminalAssistantId = terminalAssistantMessageIdByRun.get(runId);
1155+
// Folded dividers lose their place in the turn, so only the latest stays visible.
1156+
const visibleCompactionId = group.entries.findLast(
1157+
(entry) => entry.type === "activity-group" && isContextCompactionActivityGroup(entry),
1158+
)?.id;
11551159
const hiddenEntryIds = new Set(
11561160
group.entries
11571161
.filter(
11581162
(entry) =>
11591163
entry.id !== terminalAssistantId &&
11601164
entry.type !== "html-render" &&
11611165
entry.type !== "mcp-app" &&
1162-
!(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) &&
1166+
entry.id !== visibleCompactionId &&
11631167
!(
11641168
entry.type === "activity-group" &&
11651169
entry.activities.some(

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,63 @@ describe("deriveMessagesTimelineRows", () => {
700700
},
701701
);
702702

703+
it("keeps only the latest compaction divider visible when a turn that compacted twice folds", () => {
704+
const fixture = makeStreamingTimelineFixture();
705+
const [, , , liveUser, liveWork, liveAssistant] = fixture.visibleTurnItems;
706+
if (liveUser?.item.type !== "user_message" || liveAssistant?.item.type !== "assistant_message")
707+
throw new Error("Expected live fixture items");
708+
if (!liveWork) throw new Error("Expected live work");
709+
const assistantItem = liveAssistant.item;
710+
const compaction = (id: string, position: number, before: number, after: number) => ({
711+
...liveAssistant,
712+
position,
713+
sourceItemId: TurnItemId.make(id),
714+
item: {
715+
...assistantItem,
716+
id: TurnItemId.make(id),
717+
type: "compaction" as const,
718+
driver: null,
719+
status: "completed" as const,
720+
beforeTokenCount: before,
721+
afterTokenCount: after,
722+
},
723+
});
724+
const timelineEntries = deriveTimelineEntriesFromVisibleTurnItems({
725+
visibleTurnItems: [
726+
liveUser,
727+
compaction("first-compaction", 4, 100_000, 20_000),
728+
{ ...liveWork, position: 5 },
729+
compaction("second-compaction", 6, 90_000, 30_000),
730+
],
731+
optimisticMessages: [],
732+
});
733+
const rowsFor = (expandedRunIds?: ReadonlySet<RunId>) =>
734+
deriveMessagesTimelineRows({
735+
timelineEntries,
736+
isWorking: false,
737+
latestRun: {
738+
runId: fixture.runId,
739+
status: "completed",
740+
startedAt: fixture.time(5),
741+
completedAt: fixture.time(8),
742+
},
743+
turnDiffSummaries: [],
744+
supportsConversationRollback: false,
745+
...(expandedRunIds ? { expandedRunIds } : {}),
746+
});
747+
const collapsed = rowsFor();
748+
expect(collapsed.filter((row) => row.kind === "turn-fold")).toHaveLength(1);
749+
expect(collapsed.some((row) => row.kind === "work")).toBe(false);
750+
expect(collapsed.filter((row) => row.kind === "context-compaction")).toMatchObject([
751+
{ tokenCounts: { before: "90K", after: "30K" } },
752+
]);
753+
const expanded = rowsFor(new Set([fixture.runId]));
754+
expect(expanded.filter((row) => row.kind === "context-compaction")).toMatchObject([
755+
{ tokenCounts: { before: "100K", after: "20K" } },
756+
{ tokenCounts: { before: "90K", after: "30K" } },
757+
]);
758+
});
759+
703760
it("folds an answered compact-on-return question into its compaction row", () => {
704761
const fixture = makeStreamingTimelineFixture();
705762
const [history0, history1, history2, liveUser, liveWork, liveAssistant] =

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,8 @@ function deriveTurnFolds(input: {
11231123
const terminalEntryIndex = group.terminalEntry
11241124
? group.entries.findIndex((entry) => entry.id === group.terminalEntry?.id)
11251125
: group.entries.length;
1126+
// Folded dividers lose their place in the turn, so only the latest stays visible.
1127+
const visibleCompactionId = group.entries.findLast(timelineEntryIsCompaction)?.id;
11261128
for (const [index, entry] of group.entries.entries()) {
11271129
if (entry.id === group.terminalEntry?.id) {
11281130
continue;
@@ -1136,7 +1138,7 @@ function deriveTurnFolds(input: {
11361138
continue;
11371139
}
11381140
if (
1139-
isCompaction ||
1141+
entry.id === visibleCompactionId ||
11401142
timelineEntryIsPersistentResourceCard(entry) ||
11411143
input.liveSubagentEntryIds.has(entry.id) ||
11421144
input.completeSupersededReplyEntryIds.has(entry.id)

0 commit comments

Comments
 (0)