Skip to content

Commit ddc3cef

Browse files
authored
fix(clients): collapsed turns show one context-compacted divider (pingdotgg#34177)
1 parent 3c7424e commit ddc3cef

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
@@ -1161,14 +1161,18 @@ function deriveThreadFeedRunFolds(
11611161
continue;
11621162
}
11631163
const terminalAssistantId = terminalAssistantMessageIdByRun.get(runId);
1164+
// Folded dividers lose their place in the turn, so only the latest stays visible.
1165+
const visibleCompactionId = group.entries.findLast(
1166+
(entry) => entry.type === "activity-group" && isContextCompactionActivityGroup(entry),
1167+
)?.id;
11641168
const hiddenEntryIds = new Set(
11651169
group.entries
11661170
.filter(
11671171
(entry) =>
11681172
entry.id !== terminalAssistantId &&
11691173
entry.type !== "html-render" &&
11701174
entry.type !== "mcp-app" &&
1171-
!(entry.type === "activity-group" && isContextCompactionActivityGroup(entry)) &&
1175+
entry.id !== visibleCompactionId &&
11721176
!(
11731177
entry.type === "activity-group" &&
11741178
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
@@ -768,6 +768,63 @@ describe("deriveMessagesTimelineRows", () => {
768768
},
769769
);
770770

771+
it("keeps only the latest compaction divider visible when a turn that compacted twice folds", () => {
772+
const fixture = makeStreamingTimelineFixture();
773+
const [, , , liveUser, liveWork, liveAssistant] = fixture.visibleTurnItems;
774+
if (liveUser?.item.type !== "user_message" || liveAssistant?.item.type !== "assistant_message")
775+
throw new Error("Expected live fixture items");
776+
if (!liveWork) throw new Error("Expected live work");
777+
const assistantItem = liveAssistant.item;
778+
const compaction = (id: string, position: number, before: number, after: number) => ({
779+
...liveAssistant,
780+
position,
781+
sourceItemId: TurnItemId.make(id),
782+
item: {
783+
...assistantItem,
784+
id: TurnItemId.make(id),
785+
type: "compaction" as const,
786+
driver: null,
787+
status: "completed" as const,
788+
beforeTokenCount: before,
789+
afterTokenCount: after,
790+
},
791+
});
792+
const timelineEntries = deriveTimelineEntriesFromVisibleTurnItems({
793+
visibleTurnItems: [
794+
liveUser,
795+
compaction("first-compaction", 4, 100_000, 20_000),
796+
{ ...liveWork, position: 5 },
797+
compaction("second-compaction", 6, 90_000, 30_000),
798+
],
799+
optimisticMessages: [],
800+
});
801+
const rowsFor = (expandedRunIds?: ReadonlySet<RunId>) =>
802+
deriveMessagesTimelineRows({
803+
timelineEntries,
804+
isWorking: false,
805+
latestRun: {
806+
runId: fixture.runId,
807+
status: "completed",
808+
startedAt: fixture.time(5),
809+
completedAt: fixture.time(8),
810+
},
811+
turnDiffSummaries: [],
812+
supportsConversationRollback: false,
813+
...(expandedRunIds ? { expandedRunIds } : {}),
814+
});
815+
const collapsed = rowsFor();
816+
expect(collapsed.filter((row) => row.kind === "turn-fold")).toHaveLength(1);
817+
expect(collapsed.some((row) => row.kind === "work")).toBe(false);
818+
expect(collapsed.filter((row) => row.kind === "context-compaction")).toMatchObject([
819+
{ tokenCounts: { before: "90K", after: "30K" } },
820+
]);
821+
const expanded = rowsFor(new Set([fixture.runId]));
822+
expect(expanded.filter((row) => row.kind === "context-compaction")).toMatchObject([
823+
{ tokenCounts: { before: "100K", after: "20K" } },
824+
{ tokenCounts: { before: "90K", after: "30K" } },
825+
]);
826+
});
827+
771828
it("folds an answered compact-on-return question into its compaction row", () => {
772829
const fixture = makeStreamingTimelineFixture();
773830
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
@@ -1137,6 +1137,8 @@ function deriveTurnFolds(input: {
11371137
const terminalEntryIndex = group.terminalEntry
11381138
? group.entries.findIndex((entry) => entry.id === group.terminalEntry?.id)
11391139
: group.entries.length;
1140+
// Folded dividers lose their place in the turn, so only the latest stays visible.
1141+
const visibleCompactionId = group.entries.findLast(timelineEntryIsCompaction)?.id;
11401142
for (const [index, entry] of group.entries.entries()) {
11411143
if (entry.id === group.terminalEntry?.id) {
11421144
continue;
@@ -1150,7 +1152,7 @@ function deriveTurnFolds(input: {
11501152
continue;
11511153
}
11521154
if (
1153-
isCompaction ||
1155+
entry.id === visibleCompactionId ||
11541156
timelineEntryIsPersistentResourceCard(entry) ||
11551157
input.liveSubagentEntryIds.has(entry.id) ||
11561158
input.completeSupersededReplyEntryIds.has(entry.id)

0 commit comments

Comments
 (0)