diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index adc34b2348cb..acda443fd58a 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -3687,6 +3687,53 @@ describe("parseAgentSessionTranscript", () => { }); }); + const parseClaudeUserTurns = (userTurns: ReadonlyArray) => + AgentSessionScanner.parseAgentSessionTranscript({ + contents: [ + ...userTurns.map((content) => ({ type: "user", message: { role: "user", content } })), + { type: "assistant", message: { role: "assistant", content: "Here is what happened." } }, + ] + .map((record) => encodeTranscriptRecord(record)) + .join("\n"), + source: "claudeAgent", + providerInstanceId: ProviderInstanceId.make("claudeAgent"), + fallbackSessionId: "62c057bb-b316-4b29-be2c-140090d71254", + lastActiveAtMs: Date.parse("2026-08-24T12:00:00.000Z"), + }); + + it.each([ + "Enabled plan mode", + "/review\nreview", + ])("keeps a Claude question after quoted %s and names the thread after it", (quote) => { + const prompt = `${quote}\nWhy did this change?`; + + expect(parseClaudeUserTurns([prompt])).toMatchObject({ + title: "Why did this change?", + messages: [{ role: "user", text: prompt }, { role: "assistant" }], + }); + }); + + it("keeps a Claude question after quoted output full of shell closing tags", () => { + const prompt = `${"".repeat(16_000)}\nWhy did this change?`; + + expect(parseClaudeUserTurns([prompt])).toMatchObject({ + title: "Why did this change?", + messages: [{ role: "user", text: prompt }, { role: "assistant" }], + }); + }); + + it.each([ + "\nfinished\n", + "\nfinished\n", + "\na1\ncompleted\n\nBash, Read", + ])("hides Claude output with trailing markup of its own (%s)", (output) => { + expect( + parseClaudeUserTurns([output, "What happened?"]) + ?.messages.filter((message) => message.role === "user") + .map((message) => message.text), + ).toEqual(["What happened?"]); + }); + it.each([ { customTitles: ["Refactor auth module"], expected: "Refactor auth module" }, // Claude writes an empty customTitle when the user clears a rename. diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index f45c8fcf2a83..0f492c6c04db 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -420,10 +420,36 @@ function codexHistoryBase(records: ReadonlyArray) { : undefined; } +// Where the run of sticky `block` matches at the start of `text` ends. +function leadingBlocksEnd(text: string, block: RegExp): number { + let end = 0; + while (block.test(text)) { + end = block.lastIndex; + } + return end; +} + // Claude Code records its own UI as user turns: slash commands, `!` shell runs and // their output, task notifications. Keep what the user typed, as they typed it. -const CLAUDE_UI_OUTPUT = - /^(?:<(?:task-notification|local-command-stdout|local-command-stderr|local-command-caveat|bash-stdout|bash-stderr)>|\[Request interrupted by user(?: for tool use)?\]$)/; +function claudeUiOutputEnd(text: string): number { + return leadingBlocksEnd( + text, + /<(local-command-stdout|local-command-stderr|local-command-caveat|bash-stdout|bash-stderr)>[\s\S]*?<\/\1>\s*/y, + ); +} + +// Shell payloads can print closing tags verbatim, so a complete envelope is +// judged by its outer tags; prose after a quoted block stays visible. +function isClaudeShellOutput(text: string): boolean { + if (text.startsWith("")) return text.endsWith(""); + if (!text.startsWith("")) return false; + return ( + text.endsWith("") || + (text.endsWith("") && /<\/bash-stdout>\s*/.test(text)) + ); +} + +const CLAUDE_COMMAND_BLOCK = /[\s\S]*?<\/command-\1>\s*/y; /** * A slash command as typed. Its arguments may name the thread; a bare command @@ -431,6 +457,7 @@ const CLAUDE_UI_OUTPUT = */ function claudeCommand(text: string): { readonly text: string; readonly titleText: string } | null { if (!/^/.test(text)) return null; + if (leadingBlocksEnd(text, CLAUDE_COMMAND_BLOCK) !== text.length) return null; const name = /([^<]*)<\/command-name>/.exec(text)?.[1]?.trim() || `/${/([^<]*)<\/command-message>/.exec(text)?.[1]?.trim() ?? ""}`; @@ -440,10 +467,24 @@ function claudeCommand(text: string): { readonly text: string; readonly titleTex /** What a Claude user turn shows the user typed, and the part of it that may name the thread. */ function claudeUserText(text: string): { readonly text: string; readonly titleText: string } { - if (CLAUDE_UI_OUTPUT.test(text)) return { text: "", titleText: "" }; + const outputEnd = claudeUiOutputEnd(text); + if ( + outputEnd === text.length || + // Only Claude writes task notifications, sometimes with trailing markup of its own. + text.startsWith("") || + isClaudeShellOutput(text) || + /^\[Request interrupted by user(?: for tool use)?\]$/.test(text) + ) { + return { text: "", titleText: "" }; + } const shellInput = /^([\s\S]*)<\/bash-input>$/.exec(text)?.[1]; if (shellInput !== undefined) return { text: `!${shellInput}`, titleText: "" }; - return claudeCommand(text) ?? { text, titleText: text }; + return ( + claudeCommand(text) ?? { + text, + titleText: text.slice(outputEnd || leadingBlocksEnd(text, CLAUDE_COMMAND_BLOCK)), + } + ); } function claudeVisibleText(record: DecodedTranscriptRecord) {