Skip to content

Commit cf0e1a6

Browse files
only21milclaude
andcommitted
fix(server): imported Codex threads keep their saved Codex names
Codex records each rename in session_index.jsonl. Import ignored it and titled threads from the first user message, so a renamed chat could arrive titled with injected markup. Read a bounded tail of the index and use the newest nonempty name, as Codex does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c5332bf commit cf0e1a6

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,56 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
11121112
}),
11131113
);
11141114

1115+
it.effect("titles Codex sessions with their newest saved Codex name", () =>
1116+
Effect.gen(function* () {
1117+
const fileSystem = yield* FileSystem.FileSystem;
1118+
const path = yield* Path.Path;
1119+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
1120+
yield* TestClock.setTime(nowMs);
1121+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1122+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1123+
const workspace = yield* makeTempDir("t3code-workspace-");
1124+
1125+
yield* writeTranscript({
1126+
filePath: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-named.jsonl"),
1127+
contents: [
1128+
encodeTranscriptRecord({
1129+
type: "session_meta",
1130+
payload: { id: "named-session", cwd: workspace },
1131+
}),
1132+
encodeTranscriptRecord({
1133+
type: "event_msg",
1134+
payload: {
1135+
type: "user_message",
1136+
message:
1137+
'<external_codex_apps_open_page>{"page_id":null}</external_codex_apps_open_page>\nHey, what\'s up?',
1138+
},
1139+
}),
1140+
].join("\n"),
1141+
mtimeMs: nowMs,
1142+
});
1143+
yield* fileSystem.writeFileString(
1144+
path.join(codexHomePath, "session_index.jsonl"),
1145+
[
1146+
{ id: "named-session", thread_name: "First name" },
1147+
{ id: "other-session", thread_name: "Other chat" },
1148+
{ id: "named-session", thread_name: "Renamed chat" },
1149+
{ id: "named-session", thread_name: " " },
1150+
]
1151+
.map((entry) => `${encodeTranscriptRecord({ ...entry, updated_at: "unknown" })}\n`)
1152+
.join(""),
1153+
);
1154+
1155+
const threads = yield* runRecentThreads({
1156+
claudeHomePath,
1157+
codexHomePath,
1158+
workspaceRoot: workspace,
1159+
});
1160+
1161+
expect(threads.map((thread) => thread.title)).toEqual(["Renamed chat"]);
1162+
}),
1163+
);
1164+
11151165
it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>
11161166
Effect.gen(function* () {
11171167
const path = yield* Path.Path;

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
const TRANSCRIPT_PREFIX_BYTES = 32 * 1024;
4545
const METADATA_READ_BYTES = 8 * 1024;
4646
const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024;
47+
const MAX_CODEX_SESSION_INDEX_BYTES = 1024 * 1024;
4748

4849
const MAX_TRANSCRIPTS_PER_SOURCE = 5000;
4950

@@ -102,12 +103,20 @@ const TranscriptRecord = Schema.Struct({
102103
),
103104
});
104105

106+
const CodexSessionIndexEntry = Schema.Struct({
107+
id: Schema.String,
108+
thread_name: Schema.String,
109+
});
110+
105111
const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);
106112
const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);
107113
const decodeTranscriptRecord = Schema.decodeUnknownOption(Schema.fromJsonString(TranscriptRecord));
108114
const decodeTranscriptValue = Schema.decodeUnknownOption(TranscriptRecord);
109115
const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);
110116
const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);
117+
const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
118+
Schema.fromJsonString(CodexSessionIndexEntry),
119+
);
111120

112121
type DecodedTranscriptRecord = typeof TranscriptRecord.Type;
113122

@@ -162,6 +171,7 @@ interface RawCandidate {
162171
readonly cwd: string;
163172
readonly source: AgentSessionSource;
164173
readonly providerInstanceId: ProviderInstanceId;
174+
readonly homePath: string;
165175
readonly threadCount: number;
166176
readonly lastActiveAtMs: number | null;
167177
readonly transcripts: ReadonlyArray<{
@@ -174,6 +184,7 @@ interface TranscriptCandidate {
174184
readonly filePath: string;
175185
readonly mtimeMs: number;
176186
readonly providerInstanceId: ProviderInstanceId;
187+
readonly homePath: string;
177188
readonly size: number;
178189
}
179190

@@ -282,6 +293,17 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord)
282293
const CODEX_TITLE_PREAMBLE =
283294
/^(?:\s*(?:<(recommended_plugins|environment_context|user_instructions|INSTRUCTIONS|in-app-browser-context)(?:\s[^>]*)?>[\s\S]*?<\/\1>|# AGENTS\.md instructions\b[^\n]*|## My request for Codex:))+/;
284295

296+
// Codex appends an entry per rename; like Codex, the newest nonempty name wins.
297+
function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {
298+
const names = new Map<string, string>();
299+
for (const line of contents.split("\n")) {
300+
const entry = Option.getOrUndefined(decodeCodexSessionIndexEntry(line));
301+
const name = entry?.thread_name.trim();
302+
if (entry !== undefined && name) names.set(entry.id, name);
303+
}
304+
return names;
305+
}
306+
285307
function deriveTitle(source: AgentSessionSource, text: string): string | undefined {
286308
const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text;
287309
return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined;
@@ -902,6 +924,29 @@ export const make = Effect.gen(function* () {
902924
);
903925
});
904926

927+
const readCodexThreadNames = Effect.fn("AgentSessionScanner.readCodexThreadNames")(function* (
928+
homePath: string,
929+
) {
930+
const indexPath = path.join(homePath, "session_index.jsonl");
931+
return yield* Effect.scoped(
932+
Effect.gen(function* () {
933+
const file = yield* fileSystem.open(indexPath, { flag: "r" });
934+
const size = Number((yield* file.stat).size);
935+
const start = Math.max(0, size - MAX_CODEX_SESSION_INDEX_BYTES);
936+
yield* file.seek(BigInt(start), "start");
937+
const bytes = yield* file.readAlloc(size - start);
938+
const contents = Option.match(bytes, {
939+
onNone: () => "",
940+
onSome: (value) => new TextDecoder().decode(value),
941+
});
942+
// A tail read can start mid-record.
943+
return parseCodexThreadNames(
944+
start > 0 ? contents.slice(contents.indexOf("\n") + 1) : contents,
945+
);
946+
}),
947+
).pipe(Effect.orElseSucceed((): ReadonlyMap<string, string> => new Map()));
948+
});
949+
905950
const resolveClaudeConfigDir = (homePath: string, environmentHome?: string): string => {
906951
const configured = homePath.trim();
907952
if (configured.length > 0) {
@@ -958,6 +1003,7 @@ export const make = Effect.gen(function* () {
9581003
filePath,
9591004
mtimeMs: stats.value.mtime.value.getTime(),
9601005
providerInstanceId,
1006+
homePath,
9611007
size: Number(stats.value.size),
9621008
});
9631009
}
@@ -1019,6 +1065,7 @@ export const make = Effect.gen(function* () {
10191065
filePath,
10201066
mtimeMs: stats.value.mtime.value.getTime(),
10211067
providerInstanceId,
1068+
homePath,
10221069
size: Number(stats.value.size),
10231070
});
10241071
}
@@ -1040,6 +1087,7 @@ export const make = Effect.gen(function* () {
10401087
{
10411088
cwd: string;
10421089
providerInstanceId: ProviderInstanceId;
1090+
homePath: string;
10431091
lastActiveAtMs: number;
10441092
transcripts: Array<{ filePath: string; mtimeMs: number }>;
10451093
}
@@ -1057,6 +1105,7 @@ export const make = Effect.gen(function* () {
10571105
byOwnerAndCwd.set(key, {
10581106
cwd,
10591107
providerInstanceId: transcript.providerInstanceId,
1108+
homePath: transcript.homePath,
10601109
lastActiveAtMs: transcript.mtimeMs,
10611110
transcripts: [transcript],
10621111
});
@@ -1067,6 +1116,7 @@ export const make = Effect.gen(function* () {
10671116
cwd: group.cwd,
10681117
source,
10691118
providerInstanceId: group.providerInstanceId,
1119+
homePath: group.homePath,
10701120
threadCount: group.transcripts.length,
10711121
lastActiveAtMs: group.lastActiveAtMs,
10721122
transcripts: group.transcripts,
@@ -1353,6 +1403,7 @@ export const make = Effect.gen(function* () {
13531403
(source) => `${source.providerInstanceId}\0${source.filePath}`,
13541404
);
13551405
const importedSessions = new Set<string>();
1406+
const codexThreadNamesByHome = new Map<string, ReadonlyMap<string, string>>();
13561407
let bytesRemaining = MAX_IMPORT_BYTES;
13571408
let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS;
13581409
let recordsRemaining = MAX_IMPORT_RECORDS;
@@ -1431,6 +1482,14 @@ export const make = Effect.gen(function* () {
14311482
if (parsedThread === null) {
14321483
return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
14331484
}
1485+
let savedTitle: string | undefined;
1486+
if (candidate.source === "codex") {
1487+
const names =
1488+
codexThreadNamesByHome.get(candidate.homePath) ??
1489+
(yield* readCodexThreadNames(candidate.homePath));
1490+
codexThreadNamesByHome.set(candidate.homePath, names);
1491+
savedTitle = names.get(parsedThread.providerSessionId);
1492+
}
14341493

14351494
const source: AgentSessionImportSource = {
14361495
...identity,
@@ -1445,7 +1504,8 @@ export const make = Effect.gen(function* () {
14451504
importedSessions.add(sessionKey);
14461505
return Option.some<AgentSessionRecentThread>({
14471506
_tag: "Importable",
1448-
thread: parsedThread,
1507+
thread:
1508+
savedTitle === undefined ? parsedThread : { ...parsedThread, title: savedTitle },
14491509
source,
14501510
});
14511511
}).pipe(importReadLock.withPermits(1)),

0 commit comments

Comments
 (0)