Skip to content
138 changes: 138 additions & 0 deletions apps/server/src/project/AgentSessionScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,144 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
}),
);

it.effect("ignores sessions without visible messages without spending the import budget", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
yield* TestClock.setTime(nowMs);
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
const workspace = yield* makeTempDir("t3code-workspace-");
const codexRollout = (name: string) =>
path.join(codexHomePath, "sessions", "2026", "08", "24", `rollout-${name}.jsonl`);
// Newer empty sessions would exhaust the 100-transcript import budget before any real one.
const emptySessions = Array.from({ length: 100 }, (_, index) =>
index % 2 === 0
? {
filePath: codexRollout(`empty-${index}`),
records: [
{
type: "session_meta",
payload: { id: `codex-empty-${index}`, cwd: workspace, source: "vscode" },
},
{
type: "event_msg",
payload: {
type: "thread_settings_applied",
thread_settings: { cwd: workspace },
},
},
],
}
: {
filePath: path.join(
claudeHomePath,
"projects",
"-slug",
`claude-empty-${index}.jsonl`,
),
records: [
{ type: "system", subtype: "informational", content: "Status", cwd: workspace },
{
type: "user",
isMeta: true,
message: {
role: "user",
content: "<local-command-caveat>Caveat</local-command-caveat>",
},
cwd: workspace,
},
],
},
);
const transcripts = [
...emptySessions,
{
// Without session_meta there is no resumable session ID.
filePath: codexRollout("unusable"),
records: [
{ type: "turn_context", payload: { cwd: workspace } },
{
type: "event_msg",
payload: { type: "user_message", message: "Import this prompt" },
},
],
},
{
filePath: codexRollout("real"),
records: [
{ type: "session_meta", payload: { id: "real-session", cwd: workspace } },
{
type: "event_msg",
payload: { type: "user_message", message: "Import this prompt" },
},
],
},
];
for (const [index, { filePath, records }] of transcripts.entries()) {
yield* writeTranscript({
filePath,
contents: records.map((record) => encodeTranscriptRecord(record)).join("\n"),
mtimeMs: nowMs - index * 60_000,
});
}

const outcomes = yield* runRecentThreadOutcomes({
claudeHomePath,
codexHomePath,
workspaceRoot: workspace,
});

expect(
outcomes.map((outcome) =>
outcome._tag === "Importable" ? outcome.thread.providerSessionId : outcome._tag,
),
).toEqual(["Skipped", "real-session"]);
}),
);

it.effect("skips a rewound Codex rollout whose inherited history is unavailable", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
yield* TestClock.setTime(nowMs);
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
const workspace = yield* makeTempDir("t3code-workspace-");
const threadId = "019a0000-0000-7000-8000-000000000001";
const rolloutId = "019a0000-0000-7000-8000-000000000002";
// The ancestor rollout is missing, and this rollout added no messages of its own.
yield* writeTranscript({
filePath: path.join(
codexHomePath,
"sessions",
"2026",
"08",
"24",
`rollout-2026-08-24T11-00-00-${threadId}_${rolloutId}.jsonl`,
),
contents: encodeTranscriptRecord({
type: "session_meta",
payload: {
id: threadId,
cwd: workspace,
history_mode: "paginated",
history_base: { thread_id: threadId, end_ordinal_exclusive: 3, end_byte_offset: 512 },
},
}),
mtimeMs: nowMs,
});

const outcomes = yield* runRecentThreadOutcomes({
claudeHomePath,
codexHomePath,
workspaceRoot: workspace,
});

expect(outcomes).toEqual([{ _tag: "Skipped" }]);
}),
);

it.effect("skips excessive records without blocking an older valid transcript", () =>
Effect.gen(function* () {
const path = yield* Path.Path;
Expand Down
15 changes: 13 additions & 2 deletions apps/server/src/project/AgentSessionScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,16 @@ export function parseAgentSessionTranscript(
input.source === "claudeAgent"
? records.reduce(foldClaudeFields, emptyClaudeFields)
: emptyClaudeFields;
return parseAgentSessionRecords(input, records, claudeFields, 0);
const thread = parseAgentSessionRecords(input, records, claudeFields, 0);
return thread === "empty" ? null : thread;
}

function parseAgentSessionRecords(
input: AgentSessionTranscriptMetadata,
records: ReadonlyArray<DecodedTranscriptRecord>,
claudeFields: ClaudeFields,
droppedMessageCount: number,
): AgentSessionThread | null {
): AgentSessionThread | "empty" | null {
const fallbackTimestamp = DateTime.formatIso(DateTime.makeUnsafe(input.lastActiveAtMs));
// Claude filenames are session IDs. Codex rollout filenames include extra
// timestamp text, so only transcript metadata can provide a resumable ID.
Expand Down Expand Up @@ -648,6 +649,7 @@ function parseAgentSessionRecords(
});
}

if (messages.length === 0 && droppedMessageCount === 0) return "empty";
const visibleMessages = messages.map(
({ codexResponseUser: _codexResponseUser, ...message }) => message,
);
Expand Down Expand Up @@ -2139,6 +2141,15 @@ export const make = Effect.gen(function* () {
snapshot.claudeFields,
snapshot.droppedMessageCount,
);
if (parsedThread === "empty") {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// null means an unreadable history base, which may hold the conversation.
if (candidate.source === "codex" && inheritedRecords === null) {
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
}
// Never recorded as imported, so charging them would starve real sessions every run.
transcriptsRemaining += 1;
return Option.none<AgentSessionRecentThread>();
}
if (parsedThread === null) {
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
}
Expand Down
6 changes: 4 additions & 2 deletions docs/user/welcome-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ a time and skips files larger than 4 GiB. It also skips a conversation when the
message text and metadata it keeps need more than 32 MiB of memory. Tool output
does not count toward that limit. It skips a conversation with more than 100,000
records every time, so opening the page again does not import it. It ignores
malformed records and skips unreadable or unparseable conversations.
malformed records and conversations with no messages, such as a session opened
in an editor but never used. It skips unreadable or unparseable conversations.

Each import attempt reads up to 100 conversation files and 4 GiB per project. To
Each import attempt reads up to 100 conversations and 4 GiB per project.
Conversations with no messages count toward the 4 GiB but not the 100. To
continue a large batch, open the Welcome page again and pick the project in
**Review all**. Completed conversations are not imported again. You can continue
without the remaining history.
Expand Down
Loading