Skip to content

Commit e846b46

Browse files
fix(server): keep image-only Codex prompts and request-based titles
An image-only Codex prompt has no text, so with response copies suppressed the turn vanished; it now imports as Codex's own [Image #n] placeholders. Response-only prompts whose setup context is followed by the request line are titled after the request again, and a prompt recorded as both a user_message and a completed UserMessage imports once. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ddc7b9e commit e846b46

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,7 @@ describe("parseAgentSessionTranscript", () => {
32673267
"<recommended_plugins>\nGitHub\n</recommended_plugins>",
32683268
"# AGENTS.md instructions for /tmp/project\n\n<INSTRUCTIONS>\nRules\n</INSTRUCTIONS>",
32693269
'<in-app-browser-context source="ambient-ui-state">\nTab\n</in-app-browser-context>',
3270+
"## My request for Codex:",
32703271
"Fix the import titles",
32713272
].join("\n");
32723273
const thread = AgentSessionScanner.parseAgentSessionTranscript({
@@ -3313,6 +3314,23 @@ describe("parseAgentSessionTranscript", () => {
33133314
'<send_user_message_question_reply>\n[{"answer":"Use PostgreSQL","question":"Which database?","questionItemId":"q-1"}]\n</send_user_message_question_reply>',
33143315
),
33153316
assistant("PostgreSQL it is."),
3317+
responseUser("<image name=[Image #1]>\n</image>"),
3318+
encodeTranscriptRecord({
3319+
type: "event_msg",
3320+
payload: { type: "user_message", message: "", local_images: ["/tmp/shot.png"] },
3321+
}),
3322+
assistant("Nice screenshot."),
3323+
encodeTranscriptRecord({
3324+
type: "event_msg",
3325+
payload: { type: "user_message", message: "Ship it" },
3326+
}),
3327+
encodeTranscriptRecord({
3328+
type: "event_msg",
3329+
payload: {
3330+
type: "item_completed",
3331+
item: { type: "UserMessage", content: [{ type: "text", text: "Ship it" }] },
3332+
},
3333+
}),
33163334
].join("\n"),
33173335
source: "codex",
33183336
providerInstanceId: ProviderInstanceId.make("codex"),
@@ -3324,6 +3342,9 @@ describe("parseAgentSessionTranscript", () => {
33243342
"Which database should we use?",
33253343
"Pick one?",
33263344
"PostgreSQL it is.",
3345+
"[Image #1]",
3346+
"Nice screenshot.",
3347+
"Ship it",
33273348
]);
33283349
});
33293350

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ const TranscriptRecord = Schema.Struct({
135135
type: Schema.optional(Schema.String),
136136
role: Schema.optional(Schema.String),
137137
message: Schema.optional(Schema.String),
138+
images: Schema.optional(Schema.Unknown),
139+
local_images: Schema.optional(Schema.Unknown),
138140
model: Schema.optional(Schema.String),
139141
cwd: Schema.optional(Schema.String),
140142
history_base: Schema.optional(Schema.Unknown),
@@ -359,7 +361,7 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord)
359361
// Codex records setup context as user text ahead of the real request. Skip
360362
// these leading blocks when deriving a title; imported messages keep them.
361363
const CODEX_TITLE_PREAMBLE =
362-
/^(?:\s*(?:<(recommended_plugins|environment_context|user_instructions|INSTRUCTIONS|in-app-browser-context)(?:\s[^>]*)?>[\s\S]*?<\/\1>|# AGENTS\.md instructions\b[^\n]*))+/;
364+
/^(?:\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:))+/;
363365

364366
// Codex prompts that open with generated context end it at the request line.
365367
// When that line appears more than once, either side may quote it, so keep the text.
@@ -403,12 +405,28 @@ function codexUserText(text: string): string {
403405
.join("\n\n");
404406
}
405407

408+
// Codex shows a prompt's images as these placeholders; an image-only prompt is only them.
409+
function codexImagePlaceholders(text: string, imageCount: number): string {
410+
if (text.trim().length > 0) return text;
411+
return Array.from({ length: imageCount }, (_, index) => `[Image #${index + 1}]`).join(" ");
412+
}
413+
406414
// Legacy rollouts record user_message events; paginated ones record completed UserMessage items.
407415
function codexUserEventText(record: DecodedTranscriptRecord): string | null {
408416
if (record.type !== "event_msg") return null;
409-
if (record.payload?.type === "user_message") return record.payload.message ?? "";
410-
if (record.payload?.type === "item_completed" && record.payload.item?.type === "UserMessage") {
411-
return extractText(record.payload.item.content);
417+
const payload = record.payload;
418+
if (payload?.type === "user_message") {
419+
const imageCount =
420+
(Array.isArray(payload.images) ? payload.images.length : 0) +
421+
(Array.isArray(payload.local_images) ? payload.local_images.length : 0);
422+
return codexImagePlaceholders(payload.message ?? "", imageCount);
423+
}
424+
if (payload?.type === "item_completed" && payload.item?.type === "UserMessage") {
425+
const content = payload.item.content ?? [];
426+
const imageCount = content.filter(
427+
(block) => block.type === "image" || block.type === "local_image",
428+
).length;
429+
return codexImagePlaceholders(extractText(content), imageCount);
412430
}
413431
return null;
414432
}
@@ -562,6 +580,7 @@ function parseAgentSessionRecords(
562580
// response copies add injected context. Rollouts without them only have responses.
563581
const hasCodexUserEvents =
564582
input.source === "codex" && records.some((record) => codexUserEventText(record) !== null);
583+
let codexTurnUserEvents: Array<{ readonly type: string | undefined; readonly text: string }> = [];
565584

566585
let conversationMessageCount = 0;
567586
const retainMessage = (
@@ -610,11 +629,22 @@ function parseAgentSessionRecords(
610629
}
611630
const userEventText = codexUserEventText(record);
612631
if (userEventText !== null) {
632+
const eventType = record.payload?.type;
633+
// A rollout that records a prompt in both event forms shows it once.
634+
const copy = codexTurnUserEvents.findIndex(
635+
(event) => event.type !== eventType && event.text === userEventText,
636+
);
637+
if (copy !== -1) {
638+
codexTurnUserEvents.splice(copy, 1);
639+
continue;
640+
}
641+
codexTurnUserEvents.push({ type: eventType, text: userEventText });
613642
retainMessage("user", codexUserText(userEventText), record.timestamp);
614643
continue;
615644
}
616645
if (record.type !== "response_item" || record.payload?.type !== "message") continue;
617646
if (record.payload.role === "assistant") {
647+
codexTurnUserEvents = [];
618648
retainMessage("assistant", extractText(record.payload.content), record.timestamp);
619649
} else if (record.payload.role === "user" && !hasCodexUserEvents) {
620650
retainMessage("user", codexUserText(extractText(record.payload.content)), record.timestamp);

0 commit comments

Comments
 (0)