TypeScript
fix(server): imported Codex threads keep their saved Codex names (#24982)
Victor Vogel4e08e9d14165
diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.tsindex 3cbec21d883..039393eb52a 100644--- a/apps/server/src/project/AgentSessionScanner.test.ts+++ b/apps/server/src/project/AgentSessionScanner.test.ts@@ -1245,6 +1245,56 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {}),);+ it.effect("titles Codex sessions with their newest saved Codex name", () =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ 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-");++ yield* writeTranscript({+ filePath: path.join(codexHomePath, "sessions", "2026", "08", "24", "rollout-named.jsonl"),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: "named-session", cwd: workspace },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: {+ type: "user_message",+ message:+ '<external_codex_apps_open_page>{"page_id":null}</external_codex_apps_open_page>\nHey, what\'s up?',+ },+ }),+ ].join("\n"),+ mtimeMs: nowMs,+ });+ yield* fileSystem.writeFileString(+ path.join(codexHomePath, "session_index.jsonl"),+ [+ { id: "named-session", thread_name: "First name" },+ { id: "other-session", thread_name: "Other chat" },+ { id: "named-session", thread_name: "Renamed chat" },+ { id: "named-session", thread_name: " " },+ ]+ .map((entry) => `${encodeTranscriptRecord({ ...entry, updated_at: "unknown" })}\n`)+ .join(""),+ );++ const threads = yield* runRecentThreads({+ claudeHomePath,+ codexHomePath,+ workspaceRoot: workspace,+ });++ expect(threads.map((thread) => thread.title)).toEqual(["Renamed chat"]);+ }),+ );+it.effect("excludes Codex subagents from discovery and the recent-thread budget", () =>Effect.gen(function* () {const path = yield* Path.Path;diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.tsindex bf026d9005c..bc839cf4796 100644--- a/apps/server/src/project/AgentSessionScanner.ts+++ b/apps/server/src/project/AgentSessionScanner.ts@@ -46,6 +46,7 @@ import {const TRANSCRIPT_PREFIX_BYTES = 32 * 1024;const METADATA_READ_BYTES = 8 * 1024;const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024;+const MAX_CODEX_SESSION_INDEX_BYTES = 1024 * 1024;const MAX_TRANSCRIPTS_PER_SOURCE = 5000;@@ -108,6 +109,11 @@ const TranscriptRecord = Schema.Struct({),});+const CodexSessionIndexEntry = Schema.Struct({+ id: Schema.String,+ thread_name: Schema.String,+});+const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);const decodeTranscriptRecord = Schema.decodeUnknownOption(Schema.fromJsonString(TranscriptRecord));@@ -121,6 +127,9 @@ const decodePiEntryLinks = Schema.decodeUnknownOption();const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);+const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(+ Schema.fromJsonString(CodexSessionIndexEntry),+);type DecodedTranscriptRecord = typeof TranscriptRecord.Type;@@ -175,6 +184,7 @@ interface RawCandidate {readonly cwd: string;readonly source: AgentSessionSource;readonly providerInstanceId: ProviderInstanceId;+ readonly homePath: string;readonly threadCount: number;readonly lastActiveAtMs: number | null;readonly transcripts: ReadonlyArray<{@@ -187,6 +197,7 @@ interface TranscriptCandidate {readonly filePath: string;readonly mtimeMs: number;readonly providerInstanceId: ProviderInstanceId;+ readonly homePath: string;readonly size: number;}@@ -295,6 +306,17 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord)const CODEX_TITLE_PREAMBLE =/^(?:\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:))+/;+// Codex appends an entry per rename; like Codex, the newest nonempty name wins.+function parseCodexThreadNames(contents: string): ReadonlyMap<string, string> {+ const names = new Map<string, string>();+ for (const line of contents.split("\n")) {+ const entry = Option.getOrUndefined(decodeCodexSessionIndexEntry(line));+ const name = entry?.thread_name.trim();+ if (entry !== undefined && name) names.set(entry.id, name);+ }+ return names;+}+function deriveTitle(source: AgentSessionSource, text: string): string | undefined {const request = source === "codex" ? text.replace(CODEX_TITLE_PREAMBLE, "") : text;return request.trim().split("\n")[0]?.slice(0, 100).trim() || undefined;@@ -984,6 +1006,29 @@ export const make = Effect.gen(function* () {);});+ const readCodexThreadNames = Effect.fn("AgentSessionScanner.readCodexThreadNames")(function* (+ homePath: string,+ ) {+ const indexPath = path.join(homePath, "session_index.jsonl");+ return yield* Effect.scoped(+ Effect.gen(function* () {+ const file = yield* fileSystem.open(indexPath, { flag: "r" });+ const size = Number((yield* file.stat).size);+ const start = Math.max(0, size - MAX_CODEX_SESSION_INDEX_BYTES);+ yield* file.seek(BigInt(start), "start");+ const bytes = yield* file.readAlloc(size - start);+ const contents = Option.match(bytes, {+ onNone: () => "",+ onSome: (value) => new TextDecoder().decode(value),+ });+ // A tail read can start mid-record.+ return parseCodexThreadNames(+ start > 0 ? contents.slice(contents.indexOf("\n") + 1) : contents,+ );+ }),+ ).pipe(Effect.orElseSucceed((): ReadonlyMap<string, string> => new Map()));+ });+const resolveClaudeConfigDir = (homePath: string, environmentHome?: string): string => {const configured = homePath.trim();if (configured.length > 0) {@@ -1040,6 +1085,7 @@ export const make = Effect.gen(function* () {filePath,mtimeMs: stats.value.mtime.value.getTime(),providerInstanceId,+ homePath,size: Number(stats.value.size),});}@@ -1101,6 +1147,7 @@ export const make = Effect.gen(function* () {filePath,mtimeMs: stats.value.mtime.value.getTime(),providerInstanceId,+ homePath,size: Number(stats.value.size),});}@@ -1175,6 +1222,7 @@ export const make = Effect.gen(function* () {{cwd: string;providerInstanceId: ProviderInstanceId;+ homePath: string;lastActiveAtMs: number;transcripts: Array<{ filePath: string; mtimeMs: number }>;}@@ -1192,6 +1240,7 @@ export const make = Effect.gen(function* () {byOwnerAndCwd.set(key, {cwd,providerInstanceId: transcript.providerInstanceId,+ homePath: transcript.homePath,lastActiveAtMs: transcript.mtimeMs,transcripts: [transcript],});@@ -1202,6 +1251,7 @@ export const make = Effect.gen(function* () {cwd: group.cwd,source,providerInstanceId: group.providerInstanceId,+ homePath: group.homePath,threadCount: group.transcripts.length,lastActiveAtMs: group.lastActiveAtMs,transcripts: group.transcripts,@@ -1504,6 +1554,7 @@ export const make = Effect.gen(function* () {(source) => `${source.providerInstanceId}\0${source.filePath}`,);const importedSessions = new Set<string>();+ const codexThreadNamesByHome = new Map<string, ReadonlyMap<string, string>>();let bytesRemaining = MAX_IMPORT_BYTES;let transcriptsRemaining = MAX_IMPORT_TRANSCRIPTS;let recordsRemaining = MAX_IMPORT_RECORDS;@@ -1582,6 +1633,14 @@ export const make = Effect.gen(function* () {if (parsedThread === null) {return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });}+ let savedTitle: string | undefined;+ if (candidate.source === "codex") {+ const names =+ codexThreadNamesByHome.get(candidate.homePath) ??+ (yield* readCodexThreadNames(candidate.homePath));+ codexThreadNamesByHome.set(candidate.homePath, names);+ savedTitle = names.get(parsedThread.providerSessionId);+ }const source: AgentSessionImportSource = {...identity,@@ -1596,7 +1655,8 @@ export const make = Effect.gen(function* () {importedSessions.add(sessionKey);return Option.some<AgentSessionRecentThread>({_tag: "Importable",- thread: parsedThread,+ thread:+ savedTitle === undefined ? parsedThread : { ...parsedThread, title: savedTitle },source,});}).pipe(importReadLock.withPermits(1)),