Skip to content

Commit f651bd7

Browse files
fix(server): strip the Codex request prefix only after generated context
Codex prompts that open with generated IDE, file or browser context end it at the last request line; unwrapped prompts that quote the marker keep their text. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bda3d22 commit f651bd7

2 files changed

Lines changed: 54 additions & 9 deletions

File tree

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,9 +2338,46 @@ describe("parseAgentSessionTranscript", () => {
23382338
},
23392339
);
23402340

2341-
it("keeps a Codex request that mentions the request marker in prose", () => {
2342-
const prompt =
2343-
"Explain why Codex adds ## My request for Codex: to prompts.\nShow where it is defined.";
2341+
it.each([
2342+
{
2343+
name: "keeps a request that mentions the marker in prose",
2344+
prompt:
2345+
"Explain why Codex adds ## My request for Codex: to prompts.\nShow where it is defined.",
2346+
expected:
2347+
"Explain why Codex adds ## My request for Codex: to prompts.\nShow where it is defined.",
2348+
},
2349+
{
2350+
name: "keeps a request that quotes the marker line in a code block",
2351+
prompt: [
2352+
"Fix this template without dropping its introduction:",
2353+
"```markdown",
2354+
"## My request for Codex:",
2355+
"Write the introduction",
2356+
"```",
2357+
].join("\n"),
2358+
expected: [
2359+
"Fix this template without dropping its introduction:",
2360+
"```markdown",
2361+
"## My request for Codex:",
2362+
"Write the introduction",
2363+
"```",
2364+
].join("\n"),
2365+
},
2366+
{
2367+
name: "takes the request after the last marker when IDE context selects one",
2368+
prompt: [
2369+
"# Context from my IDE setup:",
2370+
"",
2371+
"## Active selection of the file:",
2372+
"## My request for Codex:",
2373+
"Old selected template text",
2374+
"",
2375+
"## My request for Codex:",
2376+
"Please fix this parser",
2377+
].join("\n"),
2378+
expected: "Please fix this parser",
2379+
},
2380+
])("$name", ({ prompt, expected }) => {
23442381
const thread = AgentSessionScanner.parseAgentSessionTranscript({
23452382
contents: [
23462383
encodeTranscriptRecord({ type: "session_meta", payload: { id: "codex-session" } }),
@@ -2355,8 +2392,8 @@ describe("parseAgentSessionTranscript", () => {
23552392
lastActiveAtMs: Date.parse("2026-08-25T08:00:00.000Z"),
23562393
});
23572394

2358-
expect(thread?.title).toBe("Explain why Codex adds ## My request for Codex: to prompts.");
2359-
expect(thread?.messages.map((message) => message.text)).toEqual([prompt]);
2395+
expect(thread?.title).toBe(expected.split("\n")[0]);
2396+
expect(thread?.messages.map((message) => message.text)).toEqual([expected]);
23602397
});
23612398

23622399
it("keeps Claude prose that mentions a command tag", () => {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,20 @@ function foldClaudeFields(fields: ClaudeFields, record: DecodedTranscriptRecord)
281281
const CODEX_TITLE_PREAMBLE =
282282
/^(?:\s*(?:<(recommended_plugins|environment_context|user_instructions|INSTRUCTIONS|in-app-browser-context)(?:\s[^>]*)?>[\s\S]*?<\/\1>|# AGENTS\.md instructions\b[^\n]*))+/;
283283

284-
// Codex's USER_MESSAGE_BEGIN line: attachments and context precede it, the typed request follows.
285-
const CODEX_REQUEST_MARKER = /(?:^|\n)## My request for Codex:(?=\n|$)/;
284+
// Codex prompts that open with generated context end it at the last request line
285+
// (its extract_prompt_request); unwrapped prompts are the user's own text.
286+
const CODEX_CONTEXT_OPENERS = [
287+
"# Context from my IDE setup:",
288+
"# Files mentioned by the user:",
289+
"<in-app-browser-context",
290+
];
291+
const CODEX_REQUEST_MARKER = /\n## My request for Codex:(?=\n|$)/g;
286292

287293
function codexRequestText(text: string): string {
288-
const marker = CODEX_REQUEST_MARKER.exec(text);
289-
return marker === null ? text : text.slice(marker.index + marker[0].length).trim();
294+
const opening = text.trimStart();
295+
if (!CODEX_CONTEXT_OPENERS.some((opener) => opening.startsWith(opener))) return text;
296+
const marker = Array.from(text.matchAll(CODEX_REQUEST_MARKER)).at(-1);
297+
return marker === undefined ? text : text.slice(marker.index + marker[0].length).trim();
290298
}
291299

292300
const CODEX_QUESTION_REPLY_OPEN = "<send_user_message_question_reply>";

0 commit comments

Comments
 (0)