Skip to content

Commit 09a0912

Browse files
feat: fold an answered compact-on-return question into its compaction row
When Claude asks to compact a resumed session and the answer is to compact, the question card is folded into the compaction row, which notes how long the session had been idle. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2813df8 commit 09a0912

7 files changed

Lines changed: 162 additions & 47 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
type ScheduledTaskId,
5050
} from "@t3tools/contracts";
5151
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
52+
import { claudeResumeCompactionAge } from "@t3tools/shared/claudeCompaction";
5253
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
5354
import { parseThreadLinkHref, resolveThreadLinkRef } from "@t3tools/shared/threadLinks";
5455
import { PROMPT_LINK_MAX_PROMPT_LENGTH } from "@t3tools/shared/promptLinks";
@@ -1189,11 +1190,9 @@ function ThreadAutomationMessageRow(props: {
11891190
);
11901191
}
11911192

1192-
function AutomaticCompactionNote() {
1193-
return (
1194-
<Text className="font-t3-medium text-xs text-foreground-muted opacity-70">
1195-
· Automatic before cache expiry
1196-
</Text>
1193+
function CompactionNote(props: { readonly note: string | undefined }) {
1194+
return props.note === undefined ? null : (
1195+
<Text className="font-t3-medium text-xs text-foreground-muted opacity-70">· {props.note}</Text>
11971196
);
11981197
}
11991198

@@ -1215,7 +1214,7 @@ function renderFeedEntry(
12151214
readonly workGroupScrollPositions: Map<string, ThreadWorkGroupScrollPosition>;
12161215
readonly terminalAssistantMessageIds: ReadonlySet<string>;
12171216
readonly unsettledTurnId: RunId | null;
1218-
readonly automaticCompactionRunIds: ReadonlySet<RunId>;
1217+
readonly compactionNoteByRunId: ReadonlyMap<RunId, string>;
12191218
readonly failedRunIds: ReadonlySet<RunId>;
12201219
readonly onCopyWorkRow: (rowId: string, value: string) => void;
12211220
readonly onToggleWorkGroup: (groupId: string, anchorKey?: string) => void;
@@ -1378,9 +1377,9 @@ function renderFeedEntry(
13781377
iconColor={iconSubtleColor}
13791378
active={active}
13801379
>
1381-
{entry.runId !== null && props.automaticCompactionRunIds.has(entry.runId) ? (
1382-
<AutomaticCompactionNote />
1383-
) : null}
1380+
<CompactionNote
1381+
note={entry.runId === null ? undefined : props.compactionNoteByRunId.get(entry.runId)}
1382+
/>
13841383
</ThreadContextDivider>
13851384
);
13861385
}
@@ -1429,7 +1428,9 @@ function renderFeedEntry(
14291428
iconColor={iconSubtleColor}
14301429
active={active}
14311430
>
1432-
{isIdleCompactionMessage(message) ? <AutomaticCompactionNote /> : null}
1431+
<CompactionNote
1432+
note={isIdleCompactionMessage(message) ? "Automatic before cache expiry" : undefined}
1433+
/>
14331434
</ThreadContextDivider>
14341435
);
14351436
}
@@ -2527,19 +2528,26 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
25272528
);
25282529
const reviewCommentColors = useReviewCommentColors();
25292530
const unsettledTurnId = threadFeedRunIsUnsettled(props.latestRun) ? props.latestRun.runId : null;
2530-
const automaticCompactionRunIds = useMemo(
2531-
() =>
2532-
new Set(
2533-
props.feed.flatMap((entry) =>
2534-
entry.type === "message" &&
2535-
entry.message.runId !== null &&
2536-
isIdleCompactionMessage(entry.message)
2537-
? [entry.message.runId]
2538-
: [],
2539-
),
2540-
),
2541-
[props.feed],
2542-
);
2531+
const compactionNoteByRunId = useMemo(() => {
2532+
const notes = new Map<RunId, string>();
2533+
for (const entry of props.feed) {
2534+
if (
2535+
entry.type === "message" &&
2536+
entry.message.runId !== null &&
2537+
isIdleCompactionMessage(entry.message)
2538+
) {
2539+
notes.set(entry.message.runId, "Automatic before cache expiry");
2540+
} else if (entry.type === "activity-group" && entry.runId !== null) {
2541+
for (const activity of entry.activities) {
2542+
const age = activity.workEntry.questionAnswer
2543+
? claudeResumeCompactionAge(activity.workEntry.questionAnswer)
2544+
: null;
2545+
if (age !== null) notes.set(entry.runId, `On return after ${age} idle`);
2546+
}
2547+
}
2548+
}
2549+
return notes;
2550+
}, [props.feed]);
25432551
// LegendList does not invalidate visible rows when only the renderItem closure changes.
25442552
// Include turn completion so unchanged message rows reveal their footer and spacing
25452553
// even when the final message update arrives before the turn settles.
@@ -2983,7 +2991,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29832991
workGroupScrollPositions,
29842992
terminalAssistantMessageIds,
29852993
unsettledTurnId,
2986-
automaticCompactionRunIds,
2994+
compactionNoteByRunId,
29872995
failedRunIds,
29882996
onCopyWorkRow,
29892997
onToggleWorkGroup,

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
formatReadToolLabel,
6363
formatSearchToolLabel,
6464
} from "@t3tools/shared/toolActivity";
65+
import { claudeResumeCompactionAge } from "@t3tools/shared/claudeCompaction";
6566
import { formatDuration } from "@t3tools/shared/orchestrationTiming";
6667
import { truncate } from "@t3tools/shared/String";
6768
import type { HtmlRenderReference } from "@t3tools/shared/htmlRender";
@@ -352,6 +353,13 @@ export function isContextCompactionActivityGroup(entry: ThreadFeedActivityGroup)
352353
);
353354
}
354355

356+
export function isResumeCompactionAnswerGroup(entry: ThreadFeedActivityGroup): boolean {
357+
return entry.activities.every((activity) => {
358+
const answer = activity.workEntry.questionAnswer;
359+
return answer !== undefined && claudeResumeCompactionAge(answer) !== null;
360+
});
361+
}
362+
355363
export function isContextHandoffActivityGroup(entry: ThreadFeedActivityGroup): boolean {
356364
return (
357365
entry.activities.length === 1 && entry.activities[0]?.projectedItem.item.type === "handoff"
@@ -1375,7 +1383,8 @@ export function deriveThreadFeedPresentation(
13751383
const activeTailGroup = sourceFeed.at(-1);
13761384
const activeRunId = unsettledRunId(latestRun);
13771385
// A bare `/compact` turn is the compaction itself: its compaction row stands in for the prompt
1378-
// and the live activity row.
1386+
// and the live activity row. An answered "compact before continuing?" question folds into the
1387+
// compaction it asked for.
13791388
const compactionCommandRunIds = new Set<RunId>();
13801389
const compactionItemRunIds = new Set<RunId>();
13811390
for (const entry of feed) {
@@ -1448,10 +1457,14 @@ export function deriveThreadFeedPresentation(
14481457
result.push(row);
14491458
}
14501459
if (
1451-
entry.type === "message" &&
1452-
entry.message.runId !== null &&
1453-
compactionItemRunIds.has(entry.message.runId) &&
1454-
isCompactionCommandMessage(entry.message)
1460+
(entry.type === "message" &&
1461+
entry.message.runId !== null &&
1462+
compactionItemRunIds.has(entry.message.runId) &&
1463+
isCompactionCommandMessage(entry.message)) ||
1464+
(entry.type === "activity-group" &&
1465+
entry.runId !== null &&
1466+
compactionItemRunIds.has(entry.runId) &&
1467+
isResumeCompactionAnswerGroup(entry))
14551468
) {
14561469
continue;
14571470
}

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { AgentScope } from "@t3tools/shared/AgentScope";
5858
import * as HostProcess from "@t3tools/shared/HostProcess";
5959
import { applyClaudePromptEffortPrefix } from "@t3tools/shared/model";
6060
import {
61+
CLAUDE_RESUME_COMPACTION_COMPACT_ANSWER,
6162
CLAUDE_RESUME_COMPACTION_NEVER_ANSWER,
6263
formatClaudeResumeCompactionQuestion,
6364
} from "@t3tools/shared/claudeCompaction";
@@ -9108,7 +9109,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91089109
question,
91099110
options: [
91109111
{
9111-
label: "Compact and continue",
9112+
label: CLAUDE_RESUME_COMPACTION_COMPACT_ANSWER,
91129113
description: "Resume with a summary and use fewer tokens.",
91139114
},
91149115
{
@@ -9142,7 +9143,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91429143
return {
91439144
behavior: "completed" as const,
91449145
result:
9145-
selection === "Compact and continue"
9146+
selection === CLAUDE_RESUME_COMPACTION_COMPACT_ANSWER
91469147
? ("compact" as const)
91479148
: selection === CLAUDE_RESUME_COMPACTION_NEVER_ANSWER
91489149
? ("never" as const)

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,61 @@ describe("deriveMessagesTimelineRows", () => {
562562
id: `compaction:${fixture.runId}`,
563563
label: "Compacting context",
564564
active: true,
565-
automatic: true,
565+
note: "Automatic before cache expiry",
566566
},
567567
]);
568568
}
569569
});
570570

571+
it("folds an answered compact-on-return question into its compaction row", () => {
572+
const fixture = makeStreamingTimelineFixture();
573+
const [history0, history1, history2, liveUser, liveWork, liveAssistant] =
574+
fixture.visibleTurnItems;
575+
if (liveWork?.item.type !== "dynamic_tool" || liveAssistant?.item.type !== "assistant_message")
576+
throw new Error("Expected live fixture items");
577+
const { toolName: _toolName, input: _input, output: _output, ...workBase } = liveWork.item;
578+
const question =
579+
"This session is 1h 5m old and uses 412,000 tokens. Compact it before continuing?";
580+
const requestId = RuntimeRequestId.make("resume-compaction");
581+
const answered = {
582+
...liveWork,
583+
item: {
584+
...workBase,
585+
type: "user_input_request" as const,
586+
requestId,
587+
questions: [],
588+
questionAnswer: {
589+
requestId,
590+
questionTextById: { resume: question },
591+
answers: { resume: "Compact and continue" },
592+
attachmentsByQuestionId: {},
593+
},
594+
},
595+
};
596+
const compaction = {
597+
...liveAssistant,
598+
item: {
599+
...liveAssistant.item,
600+
type: "compaction" as const,
601+
status: "completed" as const,
602+
driver: null,
603+
},
604+
};
605+
const rows = deriveMessagesTimelineRows({
606+
timelineEntries: deriveTimelineEntriesFromVisibleTurnItems({
607+
visibleTurnItems: [history0!, history1!, history2!, liveUser!, answered, compaction],
608+
optimisticMessages: [],
609+
}),
610+
isWorking: false,
611+
turnDiffSummaries: [],
612+
supportsConversationRollback: false,
613+
});
614+
const liveRows = rows.slice(rows.findIndex((row) => row.id.includes("live-user")) + 1);
615+
expect(liveRows).toMatchObject([
616+
{ kind: "context-compaction", note: "On return after 1h 5m idle" },
617+
]);
618+
});
619+
571620
it("only enables assistant copy for the terminal assistant message in a turn", () => {
572621
const rows = deriveMessagesTimelineRows({
573622
timelineEntries: [

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
collectAssistantCitations,
99
} from "@t3tools/shared/assistantCitations";
1010
import { shallow } from "zustand/vanilla/shallow";
11+
import { claudeResumeCompactionAge } from "@t3tools/shared/claudeCompaction";
1112
import type { HandoffTimelineRun } from "@t3tools/client-runtime/handoff";
1213
import { renderCodexDirectivesForCopy } from "@t3tools/shared/codexMarkdownDirectives";
1314
import {
@@ -589,8 +590,8 @@ type MessagesTimelineRowContent =
589590
label: string;
590591
tokenCounts: ReturnType<typeof contextCompactionTokenCounts>;
591592
active: boolean;
592-
/** T3 Code compacted an idle thread on its own; absent for mid-turn and typed compactions. */
593-
automatic?: boolean;
593+
/** Why T3 Code compacted without being asked; absent for typed and provider compactions. */
594+
note?: string;
594595
}
595596
| {
596597
kind: "message";
@@ -1577,23 +1578,38 @@ export function deriveMessagesTimelineRows(input: {
15771578
activeWorkRow !== null || latestToolFailed ? activeToolEntries.map((entry) => entry.id) : [],
15781579
);
15791580
// A bare `/compact` turn is the compaction itself: one compaction row stands in for its prompt
1580-
// and working indicator, from the moment the turn starts.
1581-
const compactionRuns = new Map<RunId, { automatic: boolean; hasItem: boolean }>();
1581+
// and working indicator, from the moment the turn starts. Likewise an answered "compact before
1582+
// continuing?" question folds into the compaction it asked for.
1583+
const compactionRuns = new Map<RunId, { note: string | undefined; hasItem: boolean }>();
1584+
const compactedRunIds = new Set<RunId>();
1585+
const resumeCompactionAgeByRunId = new Map<RunId, string>();
15821586
for (const entry of timelineEntries) {
15831587
if (
15841588
entry.kind === "message" &&
15851589
entry.message.runId !== null &&
15861590
isCompactionCommandMessage(entry.message)
15871591
) {
15881592
compactionRuns.set(entry.message.runId, {
1589-
automatic: isIdleCompactionMessage(entry.message),
1593+
note: isIdleCompactionMessage(entry.message) ? "Automatic before cache expiry" : undefined,
15901594
hasItem: false,
15911595
});
1592-
} else if (timelineEntryIsCompaction(entry) && entry.kind === "work" && entry.entry.runId) {
1593-
const run = compactionRuns.get(entry.entry.runId);
1594-
if (run) run.hasItem = true;
1596+
} else if (entry.kind === "work" && entry.entry.runId) {
1597+
if (timelineEntryIsCompaction(entry)) {
1598+
compactedRunIds.add(entry.entry.runId);
1599+
const run = compactionRuns.get(entry.entry.runId);
1600+
if (run) run.hasItem = true;
1601+
}
1602+
const age = entry.entry.questionAnswer
1603+
? claudeResumeCompactionAge(entry.entry.questionAnswer)
1604+
: null;
1605+
if (age !== null) resumeCompactionAgeByRunId.set(entry.entry.runId, age);
15951606
}
15961607
}
1608+
const compactionNote = (runId: RunId | null | undefined) => {
1609+
if (runId == null) return undefined;
1610+
const age = resumeCompactionAgeByRunId.get(runId);
1611+
return compactionRuns.get(runId)?.note ?? (age ? `On return after ${age} idle` : undefined);
1612+
};
15971613
const activeRunIsCompaction =
15981614
input.isWorking && unsettledRunId !== null && compactionRuns.has(unsettledRunId);
15991615
const appendWorkingRow = () => {
@@ -1688,10 +1704,11 @@ export function deriveMessagesTimelineRows(input: {
16881704
const compactionRunId = timelineEntry.entry.runId;
16891705
const compactionRun =
16901706
compactionRunId == null ? undefined : compactionRuns.get(compactionRunId);
1707+
const note = compactionNote(compactionRunId);
16911708
nextRows.push({
16921709
kind: "context-compaction",
16931710
id: compactionRun ? `compaction:${compactionRunId}` : timelineEntry.id,
1694-
...(compactionRun?.automatic ? { automatic: true } : {}),
1711+
...(note ? { note } : {}),
16951712
createdAt: timelineEntry.createdAt,
16961713
label: timelineEntry.entry.label,
16971714
tokenCounts:
@@ -1701,6 +1718,16 @@ export function deriveMessagesTimelineRows(input: {
17011718
continue;
17021719
}
17031720

1721+
if (
1722+
timelineEntry.kind === "work" &&
1723+
timelineEntry.entry.runId != null &&
1724+
compactedRunIds.has(timelineEntry.entry.runId) &&
1725+
timelineEntry.entry.questionAnswer !== undefined &&
1726+
claudeResumeCompactionAge(timelineEntry.entry.questionAnswer) !== null
1727+
) {
1728+
continue;
1729+
}
1730+
17041731
if (timelineEntry.kind === "work") {
17051732
if (
17061733
(timelineEntry.entry.runId != null && failedRunIds.has(timelineEntry.entry.runId)) ||
@@ -1926,7 +1953,7 @@ export function deriveMessagesTimelineRows(input: {
19261953
label: active ? "Compacting context" : "Context compaction",
19271954
tokenCounts: null,
19281955
active,
1929-
...(compactionRun.automatic ? { automatic: true } : {}),
1956+
...(compactionRun.note ? { note: compactionRun.note } : {}),
19301957
});
19311958
}
19321959
continue;
@@ -2322,7 +2349,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
23222349
a.createdAt === bc.createdAt &&
23232350
a.label === bc.label &&
23242351
a.active === bc.active &&
2325-
a.automatic === bc.automatic
2352+
a.note === bc.note
23262353
);
23272354
}
23282355

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,7 @@ function ContextCompactionTimelineRow({
13481348
) : (
13491349
row.label
13501350
)}
1351-
{row.automatic ? (
1352-
<span className="opacity-70">&middot; Automatic before cache expiry</span>
1353-
) : null}
1351+
{row.note ? <span className="opacity-70">&middot; {row.note}</span> : null}
13541352
</span>
13551353
);
13561354
return (

0 commit comments

Comments
 (0)