The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): transcript directives survive markdown, and the guide keeps its reasoning to itself
brooklyn!65b622da0d0d
diff --git a/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx b/apps/desktop/src/components/assistant-ui/thread/message-parts.tsxindex d0351d5721e..944c809c470 100644--- a/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx+++ b/apps/desktop/src/components/assistant-ui/thread/message-parts.tsx@@ -20,6 +20,7 @@ import { formatElapsed, useElapsedSeconds, useMeasuredDuration } from '@/componeimport { ActivityTimerText } from '@/components/chat/activity-timer-text'import { GeneratedImage } from '@/components/chat/generated-image-result'import { SCAFFOLD_LABEL_CLASS, SCAFFOLD_META_CLASS, ScaffoldRow } from '@/components/chat/scaffold-row'+import { useOnboardingChatActive } from '@/components/onboarding-chat/assembly'import { useI18n } from '@/i18n'import { connectorCalls, mcpTargets } from '@/lib/connector-tools'import { generatedImageFromResult } from '@/lib/generated-images'@@ -286,6 +287,10 @@ const ReasoningAccordionGroup: FC<{ children?: ReactNode; endIndex: number; star}) => {const messageId = useAuiState(s => s.message.id)const messageRunning = useAuiState(s => s.message.status?.type === 'running')+ // The guide's reasoning is it reading its own runbook ("Now step 4: offer+ // the tour with ::ask"), and a first-time user reading that alongside the+ // greeting breaks the one conversation the guide is trying to have.+ const guidedChat = useOnboardingChatActive()const pending = useAuiState(s =>@@ -324,7 +329,7 @@ const ReasoningAccordionGroup: FC<{ children?: ReactNode; endIndex: number; star}, undefined))- if (!hasContent) {+ if (!hasContent || guidedChat) {return null}diff --git a/apps/desktop/src/lib/markdown-preprocess.directives.test.ts b/apps/desktop/src/lib/markdown-preprocess.directives.test.tsnew file mode 100644index 00000000000..7e8e02708a3--- /dev/null+++ b/apps/desktop/src/lib/markdown-preprocess.directives.test.ts@@ -0,0 +1,65 @@+import remarkGfm from 'remark-gfm'+import remarkParse from 'remark-parse'+import { unified } from 'unified'+import { describe, expect, it } from 'vitest'++import { preprocessMarkdown } from './markdown-preprocess'++/**+ * A transcript directive (`::onboarding{...}`) is a card only while the parser+ * hands the paragraph over as ONE text node; the moment an attribute value+ * reads as markdown the paragraph splits into element children and the raw+ * directive paints as the user's message. These are the shapes that leaked.+ */++// The paragraph's children as the renderer will see them, after preprocess.+function paragraphChildren(markdown: string) {+ const tree = unified().use(remarkParse).use(remarkGfm).parse(preprocessMarkdown(markdown)) as {+ children: { children?: { type: string; value?: string }[] }[]+ }++ return tree.children[0]?.children ?? []+}++const briefs = [+ 'Track monarch sightings *by week* and plot them',+ 'Track a_b and c_d values across runs',+ 'Sync ~/notes to ~/backup nightly',+ 'Wrap the `run` command with retries',+ 'Compare <before> and <after> snapshots',+ 'Parse [id] tokens and \\n escapes'+]++describe('preprocessMarkdown / directive lines', () => {+ it.each(briefs)('stays one text node with brief: %s', brief => {+ const line = `::onboarding{step="handoff" task="Tracker" brief="${brief}" plan="build"}`+ const children = paragraphChildren(line)++ expect(children.map(child => child.type)).toEqual(['text'])+ // The parser eats the escapes, so the directive arrives as written.+ expect(children[0]?.value).toBe(line)+ })++ it('leaves the surrounding prose to markdown', () => {+ const text = 'Perfect, that is *all* I needed.\n\n::onboarding{step="handoff" task="T" brief="a_b" plan="build"}'++ const tree = unified().use(remarkParse).use(remarkGfm).parse(preprocessMarkdown(text)) as {+ children: { children?: { type: string }[] }[]+ }++ expect(tree.children[0]?.children?.map(child => child.type)).toEqual(['text', 'emphasis', 'text'])+ expect(tree.children[1]?.children?.map(child => child.type)).toEqual(['text'])+ })++ it('does not touch a directive inside a code fence', () => {+ const text = '```\n::onboarding{step="handoff" brief="a_b"}\n```'++ expect(preprocessMarkdown(text)).toBe(text)+ })++ it('does not touch prose that merely contains ::', () => {+ const text = 'Use std::vector<int> for *speed*.'++ expect(preprocessMarkdown(text)).toBe(text)+ })+})diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.tsindex dc5d669ef33..ec95338e63b 100644--- a/apps/desktop/src/lib/markdown-preprocess.ts+++ b/apps/desktop/src/lib/markdown-preprocess.ts@@ -56,6 +56,15 @@ const CITATION_MARKER_RE = /(?<=[\p{L}\p{N})\].,!?:;"'”’])\[(?:\d+(?:\s*,\s*// char class excludes `)`/whitespace, matching how LLMs actually emit these.const FILE_LINK_RE = /(?<!!)\[(?<label>[^\]\n]+)\]\((?<target><?(?:file:\/\/|\/|~\/|[a-z]:[\\/])[^)\s]*>?)\)/gi+// A transcript directive on its own line: `::name{...}`. Attribute values are+// prose the model wrote (a task brief, a question) and read as markdown to the+// parser — `*by week*` becomes <em>, `a_b c_d` becomes <em>, `~/x` opens+// strikethrough. Any of those splits the paragraph into element children, the+// directive stops being text-only, and the raw line paints as the user's+// message. Same shape as lib/transcript-directives.ts DIRECTIVE_RE.+const DIRECTIVE_LINE_RE = /^([ \t]*)(::[a-z][a-z0-9-]{0,63}\{[^{}\n]{0,1024}\})[ \t]*$/gm+const MARKDOWN_INLINE_META_RE = /[\\`*_~[\]<>]/g+/*** Returns true when `body` contains a line that's exactly `marker` (modulo* leading/trailing horizontal whitespace) — i.e. an unambiguous close fence@@ -203,6 +212,22 @@ function rewriteProseSegment(segment: string): string {)}+/**+ * Backslash-escape markdown inline syntax inside directive lines so the parser+ * yields one text node. The escapes are consumed by the parser, so the+ * directive the renderer sees is byte-for-byte what the model wrote.+ */+export function shieldDirectiveLines(text: string): string {+ if (!text.includes('::')) {+ return text+ }++ return text.replace(+ DIRECTIVE_LINE_RE,+ (_match, indent: string, directive: string) => indent + directive.replace(MARKDOWN_INLINE_META_RE, '\\$&')+ )+}+/*** Apply the prose rewrites to visible prose only.*@@ -641,7 +666,11 @@ export function preprocessMarkdown(text: string): string {// blocks stay intact. The HTML-depth clamp belongs here for the same// reason: a fenced block renders as code and never reaches rehype-raw,// so escaping tags inside one would corrupt the listing for nothing.- return clampHtmlNestingDepth(normalizeVisibleProse(stripPreviewTargets(normalizeProseMath(part))))+ // Directive lines are shielded last, after the prose rewrites have had+ // their look, so nothing re-introduces markdown into them.+ return shieldDirectiveLines(+ clampHtmlNestingDepth(normalizeVisibleProse(stripPreviewTargets(normalizeProseMath(part))))+ )}).join('')}