Skip to content

Commit 28cca82

Browse files
dominikzabcikclaude
andcommitted
fix(server): imported conversations keep their full visible history
Importing a Claude, Codex or Pi session kept only the first prompt and the newest 199 visible messages, then added a notice that earlier messages were not imported. Long sessions lost most of their conversation in T3 Code even though it is still in the transcript. Imports now keep every visible user and assistant message. Reading is still bounded by the 32 MiB selected-history budget and the 100,000-record limit, so memory stays capped. The truncation notice and the first-prompt bookkeeping that only existed for the cap are gone. Claude Opus 5.5 in Claude Code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b8c2cfa commit 28cca82

5 files changed

Lines changed: 47 additions & 178 deletions

File tree

‎apps/server/src/project/AgentSessionImporter.test.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ function candidate(
7777
model: modelSelection.model,
7878
createdAt: timestamp,
7979
updatedAt: timestamp,
80-
omittedMessageCount: 0,
8180
messages: [
8281
{ role: "user" as const, text: "Fix it", createdAt: timestamp },
8382
{ role: "assistant" as const, text: "Fixed", createdAt: timestamp },
@@ -281,7 +280,6 @@ it.effect(
281280
"claudeAgent",
282281
);
283282
const unowned = candidate(CODEX_MODEL_SELECTION, "unowned");
284-
unowned.thread.omittedMessageCount = 1;
285283
const unownedPi = candidate(PI_MODEL_SELECTION, "unowned-pi", "pi");
286284
const importer = yield* makeImporter([
287285
active,
@@ -316,23 +314,9 @@ it.effect(
316314
const threadId = importId(source);
317315
const records = yield* projections.getThreadRecords(threadId, [
318316
"messages",
319-
"turnItems",
320317
"providerThreads",
321318
]);
322319
expect(records.messages.map((message) => message.text)).toEqual(["Fix it", "Fixed"]);
323-
const notices = records.turnItems.filter((item) => item.type === "system_notice");
324-
if (source === unowned) {
325-
expect(records.turnItems[0]).toBe(notices[0]);
326-
expect(notices).toMatchObject([
327-
{
328-
ordinal: 1,
329-
runId: null,
330-
message: expect.stringContaining("1 earlier message was not imported"),
331-
},
332-
]);
333-
} else {
334-
expect(notices).toEqual([]);
335-
}
336320
expect(records.thread.historyOrigin).toBe("v1_import");
337321
expect(records.thread.activeProviderThreadId).toBe(records.providerThreads[0]?.id);
338322
expect(records.providerThreads[0]?.nativeThreadRef?.nativeId).toBe(nativeId(source));

‎apps/server/src/project/AgentSessionImporter.ts‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -385,34 +385,6 @@ const make = Effect.gen(function* () {
385385
occurredAt: createdAt,
386386
payload: appThread,
387387
},
388-
...(thread.omittedMessageCount > 0
389-
? [
390-
{
391-
id: EventId.make(`${IMPORT_EVENT_PREFIX}:history-truncated:${threadId}`),
392-
type: "turn-item.updated" as const,
393-
threadId,
394-
occurredAt: createdAt,
395-
payload: {
396-
id: TurnItemId.make(`${IMPORT_EVENT_PREFIX}:history-truncated:${threadId}`),
397-
threadId,
398-
runId: null,
399-
nodeId: null,
400-
providerThreadId: null,
401-
providerTurnId: null,
402-
nativeItemRef: null,
403-
parentItemId: null,
404-
ordinal: 0,
405-
status: "completed" as const,
406-
title: null,
407-
startedAt: createdAt,
408-
completedAt: createdAt,
409-
updatedAt: createdAt,
410-
type: "system_notice" as const,
411-
message: `${thread.omittedMessageCount} earlier ${thread.omittedMessageCount === 1 ? "message was" : "messages were"} not imported. This thread keeps the first user message and the most recent history. The original session transcript is unchanged.`,
412-
},
413-
},
414-
]
415-
: []),
416388
...thread.messages.flatMap((message, index) =>
417389
messageEvents({ threadId, index, message }),
418390
),

‎apps/server/src/project/AgentSessionScanner.test.ts‎

Lines changed: 38 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,47 +2277,44 @@ describe("parseAgentSessionTranscript", () => {
22772277
},
22782278
);
22792279

2280-
it.each([199, 200, 201, 205])(
2281-
"counts only omitted conversation messages for a %s-message Claude transcript",
2282-
(count) => {
2283-
const thread = AgentSessionScanner.parseAgentSessionTranscript({
2284-
contents: [
2285-
...Array.from({ length: count }, (_, index) =>
2286-
encodeTranscriptRecord({
2287-
type: index === 0 ? "user" : "assistant",
2288-
sessionId: "claude-session",
2289-
message: { content: `Message ${index}` },
2290-
}),
2291-
),
2292-
encodeTranscriptRecord({ type: "user", isMeta: true, message: { content: "Setup" } }),
2293-
encodeTranscriptRecord({
2294-
type: "user",
2295-
isCompactSummary: true,
2296-
message: { content: "Summary" },
2297-
}),
2298-
encodeTranscriptRecord({
2299-
type: "assistant",
2300-
isSidechain: true,
2301-
message: { content: "Sidechain" },
2302-
}),
2280+
it("keeps every visible message of a long Claude transcript", () => {
2281+
const count = 205;
2282+
const thread = AgentSessionScanner.parseAgentSessionTranscript({
2283+
contents: [
2284+
...Array.from({ length: count }, (_, index) =>
23032285
encodeTranscriptRecord({
2304-
type: "assistant",
2305-
message: { content: [{ type: "tool_use" }] },
2286+
type: index === 0 ? "user" : "assistant",
2287+
sessionId: "claude-session",
2288+
message: { content: `Message ${index}` },
23062289
}),
2307-
"malformed",
2308-
].join("\n"),
2309-
source: "claudeAgent",
2310-
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
2311-
fallbackSessionId: "fallback",
2312-
lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),
2313-
});
2290+
),
2291+
encodeTranscriptRecord({ type: "user", isMeta: true, message: { content: "Setup" } }),
2292+
encodeTranscriptRecord({
2293+
type: "user",
2294+
isCompactSummary: true,
2295+
message: { content: "Summary" },
2296+
}),
2297+
encodeTranscriptRecord({
2298+
type: "assistant",
2299+
isSidechain: true,
2300+
message: { content: "Sidechain" },
2301+
}),
2302+
encodeTranscriptRecord({
2303+
type: "assistant",
2304+
message: { content: [{ type: "tool_use" }] },
2305+
}),
2306+
"malformed",
2307+
].join("\n"),
2308+
source: "claudeAgent",
2309+
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
2310+
fallbackSessionId: "fallback",
2311+
lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),
2312+
});
23142313

2315-
expect(thread?.omittedMessageCount).toBe(Math.max(0, count - 200));
2316-
expect(thread?.messages).toHaveLength(Math.min(200, count));
2317-
expect(thread?.messages[0]?.text).toBe("Message 0");
2318-
expect(thread?.messages.at(-1)?.text).toBe(`Message ${count - 1}`);
2319-
},
2320-
);
2314+
expect(thread?.messages).toHaveLength(count);
2315+
expect(thread?.messages[0]?.text).toBe("Message 0");
2316+
expect(thread?.messages.at(-1)?.text).toBe(`Message ${count - 1}`);
2317+
});
23212318

23222319
it("keeps Claude text and titles while dropping malformed and tool records", () => {
23232320
const thread = AgentSessionScanner.parseAgentSessionTranscript({
@@ -2470,7 +2467,8 @@ describe("parseAgentSessionTranscript", () => {
24702467
]);
24712468
});
24722469

2473-
it.each([199, 200])("keeps the canonical first prompt with %s later Codex messages", (count) => {
2470+
it("keeps the canonical first prompt and every later Codex message", () => {
2471+
const count = 200;
24742472
const canonicalPrompt = "\n Keep the canonical prompt \n";
24752473
const canonicalTimestamp = "2026-08-24T10:01:00.000Z";
24762474
const laterAssistantMessages = Array.from({ length: count }, (_, index) =>
@@ -2509,83 +2507,12 @@ describe("parseAgentSessionTranscript", () => {
25092507
lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),
25102508
});
25112509

2512-
expect(thread?.omittedMessageCount).toBe(count - 199);
2513-
expect(thread?.messages).toHaveLength(200);
2514-
expect(thread?.messages[0]).toMatchObject({
2515-
role: "user",
2516-
text: canonicalPrompt,
2517-
createdAt: canonicalTimestamp,
2518-
});
2519-
});
2520-
2521-
it("restores the canonical first prompt when a later user message remains", () => {
2522-
const canonicalPrompt = "\n Keep the canonical prompt \n";
2523-
const canonicalTimestamp = "2026-08-24T10:01:00.000Z";
2524-
const assistantMessages = Array.from({ length: 198 }, (_, index) =>
2525-
encodeTranscriptRecord({
2526-
type: "response_item",
2527-
timestamp: `2026-08-24T11:${String(index % 60).padStart(2, "0")}:00.000Z`,
2528-
payload: {
2529-
type: "message",
2530-
role: "assistant",
2531-
content: [{ type: "output_text", text: `Assistant message ${index}` }],
2532-
},
2533-
}),
2534-
);
2535-
const thread = AgentSessionScanner.parseAgentSessionTranscript({
2536-
contents: [
2537-
encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),
2538-
encodeTranscriptRecord({
2539-
type: "response_item",
2540-
timestamp: "2026-08-24T10:00:00.000Z",
2541-
payload: {
2542-
type: "message",
2543-
role: "user",
2544-
internal_chat_message_metadata_passthrough: { turn_id: "turn-1" },
2545-
content: [{ type: "input_text", text: "Keep the canonical prompt" }],
2546-
},
2547-
}),
2548-
encodeTranscriptRecord({
2549-
type: "event_msg",
2550-
timestamp: canonicalTimestamp,
2551-
payload: { type: "user_message", message: canonicalPrompt },
2552-
}),
2553-
...assistantMessages,
2554-
encodeTranscriptRecord({
2555-
type: "event_msg",
2556-
timestamp: "2026-08-24T11:58:30.000Z",
2557-
payload: { type: "user_message", message: "Keep this later prompt" },
2558-
}),
2559-
encodeTranscriptRecord({
2560-
type: "response_item",
2561-
timestamp: "2026-08-24T11:59:00.000Z",
2562-
payload: {
2563-
type: "message",
2564-
role: "assistant",
2565-
content: [{ type: "output_text", text: "Keep this latest response" }],
2566-
},
2567-
}),
2568-
].join("\n"),
2569-
source: "codex",
2570-
providerInstanceId: ProviderInstanceId.make("codex"),
2571-
fallbackSessionId: "fallback",
2572-
lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"),
2573-
});
2574-
2575-
expect(thread?.omittedMessageCount).toBe(1);
2576-
expect(thread?.messages).toHaveLength(200);
2510+
expect(thread?.messages).toHaveLength(count + 1);
25772511
expect(thread?.messages[0]).toMatchObject({
25782512
role: "user",
25792513
text: canonicalPrompt,
25802514
createdAt: canonicalTimestamp,
25812515
});
2582-
expect(
2583-
thread?.messages.filter((message) => message.text.trim() === canonicalPrompt.trim()),
2584-
).toHaveLength(1);
2585-
expect(thread?.messages.some((message) => message.text === "Keep this later prompt")).toBe(
2586-
true,
2587-
);
2588-
expect(thread?.messages.at(-1)?.text).toBe("Keep this latest response");
25892516
});
25902517

25912518
it("keeps mixed-format response users when turn IDs repeat after an assistant", () => {

‎apps/server/src/project/AgentSessionScanner.ts‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const MAX_METADATA_RECORDS_PER_TRANSCRIPT = 1_000;
6969
const RECENT_THREAD_WINDOW_MS = 30 * 24 * 60 * 60 * 1000;
7070
// Screenshot-heavy transcripts can reach GiB; select fields while streaming to avoid allocating payloads.
7171
const MAX_IMPORTED_TRANSCRIPT_BYTES = 4 * 1024 * 1024 * 1024;
72-
const MAX_IMPORTED_MESSAGES = 200;
7372
const MAX_IMPORT_HISTORY_BYTES = 32 * 1024 * 1024;
7473
const MAX_IMPORT_BYTES = 4 * 1024 * 1024 * 1024;
7574
const MAX_IMPORT_TRANSCRIPTS = 100;
@@ -188,7 +187,6 @@ export interface AgentSessionThread {
188187
readonly createdAt: string;
189188
readonly updatedAt: string;
190189
readonly messages: ReadonlyArray<AgentSessionThreadMessage>;
191-
readonly omittedMessageCount: number;
192190
}
193191

194192
export type AgentSessionRecentThread =
@@ -442,9 +440,6 @@ function parseAgentSessionRecords(
442440
let derivedTitle: string | undefined;
443441
let hasCodexSessionId = false;
444442
const messages: Array<AgentSessionThreadMessage & { readonly codexResponseUser: boolean }> = [];
445-
let firstUserMessage:
446-
| (AgentSessionThreadMessage & { readonly codexResponseUser: boolean })
447-
| undefined;
448443
const canonicalCodexResponseUserIndices = new Set<number>();
449444
let canonicalUserTextsInTurn = new Set<string>();
450445
let responseUsersInTurn: Array<{
@@ -498,19 +493,13 @@ function parseAgentSessionRecords(
498493
finishCodexTurn();
499494
}
500495

501-
let conversationMessageCount = 0;
502496
const retainMessage = (
503497
message: AgentSessionThreadMessage & { readonly codexResponseUser: boolean },
504498
) => {
505-
if (firstUserMessage === undefined && message.role === "user") {
506-
firstUserMessage = message;
507-
}
508499
if (derivedTitle === undefined && message.role === "user") {
509500
derivedTitle = deriveTitle(input.source, message.text);
510501
}
511-
conversationMessageCount += 1;
512502
messages.push(message);
513-
if (messages.length > MAX_IMPORTED_MESSAGES) messages.shift();
514503
};
515504

516505
const hasMatchingCodexEventInTurn = (text: string) => {
@@ -578,9 +567,7 @@ function parseAgentSessionRecords(
578567
const message = messages[index];
579568
if (message?.role === "assistant") break;
580569
if (message?.codexResponseUser === true && message.text.trim() === text.trim()) {
581-
if (firstUserMessage === message) firstUserMessage = undefined;
582570
messages.splice(index, 1);
583-
conversationMessageCount -= 1;
584571
break;
585572
}
586573
}
@@ -616,15 +603,15 @@ function parseAgentSessionRecords(
616603
});
617604
}
618605

619-
const visibleMessages = messages.map(
606+
if (
607+
providerSessionId.trim().length === 0 ||
608+
!messages.some((message) => message.role === "user")
609+
) {
610+
return null;
611+
}
612+
const retainedMessages = messages.map(
620613
({ codexResponseUser: _codexResponseUser, ...message }) => message,
621614
);
622-
if (providerSessionId.trim().length === 0 || firstUserMessage === undefined) return null;
623-
const firstUserMessageRetained = messages.includes(firstUserMessage);
624-
const { codexResponseUser: _codexResponseUser, ...visibleFirstUserMessage } = firstUserMessage;
625-
const retainedMessages = firstUserMessageRetained
626-
? visibleMessages
627-
: [visibleFirstUserMessage, ...visibleMessages.slice(-(MAX_IMPORTED_MESSAGES - 1))];
628615

629616
return {
630617
source: input.source,
@@ -635,7 +622,6 @@ function parseAgentSessionRecords(
635622
createdAt: retainedMessages[0]?.createdAt ?? fallbackTimestamp,
636623
updatedAt: fallbackTimestamp,
637624
messages: retainedMessages,
638-
omittedMessageCount: conversationMessageCount - retainedMessages.length,
639625
};
640626
}
641627

‎docs/user/welcome-page.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ are read from each enabled Pi provider's session directory, which follows
8989
`PI_CODING_AGENT_SESSION_DIR` and `PI_CODING_AGENT_DIR` in its environment. A
9090
Pi conversation imports its active branch, the one Pi reopens.
9191

92-
Conversation import is best effort. T3 Code keeps the first user prompt and the
93-
newest remaining visible user and assistant messages, with 200 messages total.
92+
Conversation import is best effort. T3 Code keeps every visible user and
93+
assistant message.
9494
It omits tool activity and attachments. For Codex, it omits generated setup
9595
context only when a canonical user event and a valid shared turn ID identify the
9696
same user turn. Ambiguous legacy or response-only context stays in the imported

0 commit comments

Comments
 (0)