Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions apps/server/src/project/AgentSessionScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,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;
Expand Down
62 changes: 61 additions & 1 deletion apps/server/src/project/AgentSessionScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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;

Expand Down Expand Up @@ -102,12 +103,20 @@ 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));
const decodeTranscriptValue = Schema.decodeUnknownOption(TranscriptRecord);
const selectTranscriptPath = createTranscriptJsonSelector(TranscriptRecord);
const decodeCodexTurnMetadata = Schema.decodeUnknownOption(CodexTurnMetadata);
const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
Schema.fromJsonString(CodexSessionIndexEntry),
);

type DecodedTranscriptRecord = typeof TranscriptRecord.Type;

Expand Down Expand Up @@ -162,6 +171,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<{
Expand All @@ -174,6 +184,7 @@ interface TranscriptCandidate {
readonly filePath: string;
readonly mtimeMs: number;
readonly providerInstanceId: ProviderInstanceId;
readonly homePath: string;
readonly size: number;
}

Expand Down Expand Up @@ -282,6 +293,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;
Expand Down Expand Up @@ -902,6 +924,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) {
Expand Down Expand Up @@ -958,6 +1003,7 @@ export const make = Effect.gen(function* () {
filePath,
mtimeMs: stats.value.mtime.value.getTime(),
providerInstanceId,
homePath,
size: Number(stats.value.size),
});
}
Expand Down Expand Up @@ -1019,6 +1065,7 @@ export const make = Effect.gen(function* () {
filePath,
mtimeMs: stats.value.mtime.value.getTime(),
providerInstanceId,
homePath,
size: Number(stats.value.size),
});
}
Expand All @@ -1040,6 +1087,7 @@ export const make = Effect.gen(function* () {
{
cwd: string;
providerInstanceId: ProviderInstanceId;
homePath: string;
lastActiveAtMs: number;
transcripts: Array<{ filePath: string; mtimeMs: number }>;
}
Expand All @@ -1057,6 +1105,7 @@ export const make = Effect.gen(function* () {
byOwnerAndCwd.set(key, {
cwd,
providerInstanceId: transcript.providerInstanceId,
homePath: transcript.homePath,
lastActiveAtMs: transcript.mtimeMs,
transcripts: [transcript],
});
Expand All @@ -1067,6 +1116,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,
Expand Down Expand Up @@ -1355,6 +1405,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;
Expand Down Expand Up @@ -1433,6 +1484,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,
Expand All @@ -1447,7 +1506,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)),
Expand Down
Loading