Skip to content

Commit c4fec1f

Browse files
fix(server): keep Codex prompts whose request line appears more than once
When the request marker repeats, either the generated context or the user's request may quote it, so taking the last one could drop the user's text. AI-assisted; verified with the tests and tools listed above.
1 parent f9c9214 commit c4fec1f

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,18 +3015,33 @@ describe("parseAgentSessionTranscript", () => {
30153015
].join("\n"),
30163016
},
30173017
{
3018-
name: "takes the request after the last marker when IDE context selects one",
3018+
name: "keeps IDE context whose request quotes the marker line",
30193019
prompt: [
30203020
"# Context from my IDE setup:",
30213021
"",
3022-
"## Active selection of the file:",
3022+
"## Open tabs:",
3023+
"- template.md",
3024+
"",
3025+
"## My request for Codex:",
3026+
"Fix this template without dropping its introduction:",
3027+
"```markdown",
30233028
"## My request for Codex:",
3024-
"Old selected template text",
3029+
"Write the introduction",
3030+
"```",
3031+
].join("\n"),
3032+
expected: [
3033+
"# Context from my IDE setup:",
3034+
"",
3035+
"## Open tabs:",
3036+
"- template.md",
30253037
"",
30263038
"## My request for Codex:",
3027-
"Please fix this parser",
3039+
"Fix this template without dropping its introduction:",
3040+
"```markdown",
3041+
"## My request for Codex:",
3042+
"Write the introduction",
3043+
"```",
30283044
].join("\n"),
3029-
expected: "Please fix this parser",
30303045
},
30313046
{
30323047
name: "takes the request after the files-mentioned prefix",

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

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

342-
// Codex prompts that open with generated context end it at the last request line
343-
// (its extract_prompt_request); unwrapped prompts are the user's own text.
342+
// Codex prompts that open with generated context end it at the request line.
343+
// When that line appears more than once, either side may quote it, so keep the text.
344344
const CODEX_CONTEXT_OPENERS = [
345345
"# Context from my IDE setup:",
346346
"# Files mentioned by the user:",
@@ -352,7 +352,8 @@ const CODEX_REQUEST_MARKER = /\n## My request for Codex:(?=\n|$)/g;
352352
function codexRequestText(text: string): string {
353353
const opening = text.trimStart();
354354
if (!CODEX_CONTEXT_OPENERS.some((opener) => opening.startsWith(opener))) return text;
355-
const marker = Array.from(text.matchAll(CODEX_REQUEST_MARKER)).at(-1);
355+
const markers = Array.from(text.matchAll(CODEX_REQUEST_MARKER));
356+
const marker = markers.length === 1 ? markers[0] : undefined;
356357
const request = marker === undefined ? "" : text.slice(marker.index + marker[0].length).trim();
357358
// An attachment sent without text has an empty request; keep the turn as recorded.
358359
return request || text;

0 commit comments

Comments
 (0)