From 1f692a8be2894a6d2f65afbdf96ff15b1da4a82b Mon Sep 17 00:00:00 2001 From: kshitij Date: Mon, 3 Aug 2026 18:25:01 +0530 Subject: [PATCH] refactor(desktop): hoist the reference-line matcher; drop dead textWithoutImageRefs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #77653: textWithoutReferenceLines built a fresh /g RegExp per call and hand-managed lastIndex — but it runs on both sides of every message comparison in the reconcile loops. An anchored non-global regex has no shared-lastIndex hazard and can be hoisted to module scope. Also removes textWithoutImageRefs, whose last production consumer #77653 replaced (kept IMAGE_REF_LINE_RE for extractImageRefs), and retargets its now-stale comment. --- .../hooks/use-session-actions/utils.test.ts | 2 +- .../assistant-ui/reference-kinds.ts | 15 +++++------- apps/desktop/src/lib/embedded-images.test.ts | 24 +------------------ apps/desktop/src/lib/embedded-images.ts | 19 ++++++--------- 4 files changed, 15 insertions(+), 45 deletions(-) diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts index 1a67321d8e..1e0ce73f55 100644 --- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts +++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.test.ts @@ -557,7 +557,7 @@ describe('preserveLocalPendingTurnMessages', () => { // `attachmentRefs`. A naive text compare (chatMessageText a === b) therefore // always mismatched whenever an image was attached and re-appended the // optimistic row as a distinct, duplicate user bubble. Both sides must now - // reduce to the same visible text via textWithoutImageRefs. + // reduce to the same visible text via textWithoutReferenceLines. it('does not duplicate the optimistic image turn when the persisted turn carries @image refs', () => { const previous = [ msg('1-user', 'user', 'first'), diff --git a/apps/desktop/src/components/assistant-ui/reference-kinds.ts b/apps/desktop/src/components/assistant-ui/reference-kinds.ts index ff7d5913b8..7d6c397584 100644 --- a/apps/desktop/src/components/assistant-ui/reference-kinds.ts +++ b/apps/desktop/src/components/assistant-ui/reference-kinds.ts @@ -159,18 +159,15 @@ export function referenceRe(): RegExp { } /** Remove reference-only lines when comparing visible message text. */ -export function textWithoutReferenceLines(text: string): string { - const matcher = referenceRe() +// Anchored + non-global: no shared `lastIndex` state (the hazard referenceRe() +// exists to avoid), and hoisting skips a RegExp construction per call — this +// runs on both sides of every message comparison in the reconcile loops. +const REFERENCE_LINE_RE = new RegExp(`^(?:${REFERENCE_PATTERN.source})$`) +export function textWithoutReferenceLines(text: string): string { return text .split('\n') - .filter(line => { - const candidate = line.trimEnd() - matcher.lastIndex = 0 - const match = matcher.exec(candidate) - - return !match || match.index !== 0 || match[0] !== candidate - }) + .filter(line => !REFERENCE_LINE_RE.test(line.trimEnd())) .join('\n') .trim() } diff --git a/apps/desktop/src/lib/embedded-images.test.ts b/apps/desktop/src/lib/embedded-images.test.ts index 7d6a0b75e8..c4ff61852e 100644 --- a/apps/desktop/src/lib/embedded-images.test.ts +++ b/apps/desktop/src/lib/embedded-images.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -import { extractEmbeddedImages, extractImageRefs, textWithoutImageRefs } from './embedded-images' +import { extractEmbeddedImages, extractImageRefs } from './embedded-images' const SAMPLE_PNG_DATA_URL = 'data:image/png;base64,' + 'A'.repeat(120) @@ -43,28 +43,6 @@ describe('extractEmbeddedImages', () => { }) }) -describe('textWithoutImageRefs', () => { - it('leaves plain text untouched', () => { - expect(textWithoutImageRefs('just a question')).toBe('just a question') - }) - - it('strips a single leading @image directive line', () => { - expect(textWithoutImageRefs('@image:/tmp/cat.png\nwhat is this?')).toBe('what is this?') - }) - - it('strips multiple @image directive lines and trims', () => { - const input = '@image:/tmp/a.png\n@image:/tmp/b.png\n describe both ' - - expect(textWithoutImageRefs(input)).toBe('describe both') - }) - - it('does not treat an inline @image mention as a directive line', () => { - // Only full-line leading directives are stripped, matching the gateway's - // persist-time rewrite. A bare mention mid-prose is preserved. - expect(textWithoutImageRefs('see @image:/tmp/cat.png here')).toBe('see @image:/tmp/cat.png here') - }) -}) - describe('extractImageRefs', () => { it('returns the text untouched and no refs when there are no directives', () => { expect(extractImageRefs('a normal prompt')).toEqual({ cleanedText: 'a normal prompt', refs: [] }) diff --git a/apps/desktop/src/lib/embedded-images.ts b/apps/desktop/src/lib/embedded-images.ts index 144a602091..57578724df 100644 --- a/apps/desktop/src/lib/embedded-images.ts +++ b/apps/desktop/src/lib/embedded-images.ts @@ -165,20 +165,15 @@ export function textWithoutEmbeddedImages(text: string): string { // (see tui_gateway/server.py's persist-time rewrite), prepended before the // user's own text. The composer's own optimistic/local turn never carries // this prefix — it keeps the attachment as separate `attachmentRefs` -// metadata, not inline text. Comparing raw chatMessageText between the -// optimistic turn and the authoritative (persisted) turn therefore always -// mismatches whenever an image was attached, which defeats the "is this the -// same turn" checks in preserveLocalPendingTurnMessages / appendLiveSessionProjection -// and re-appends the optimistic row as if it were a distinct, unconfirmed -// turn — a duplicated user bubble. Strip the directive line(s) before any -// such equality comparison so both sides reduce to the same visible text. +// metadata, not inline text. The turn-equality comparisons in +// preserveLocalPendingTurnMessages / appendLiveSessionProjection strip ALL +// reference-directive lines (not just images) via +// `textWithoutReferenceLines` in components/assistant-ui/reference-kinds.ts; +// IMAGE_REF_LINE_RE remains here for extractImageRefs below, which moves the +// image directives into attachmentRefs metadata. const IMAGE_REF_LINE_RE = /^@image:[^\n]*\n?/gm -export function textWithoutImageRefs(text: string): string { - return text.replace(IMAGE_REF_LINE_RE, '').trim() -} - -// Same directive lines as textWithoutImageRefs, but keeps them instead of +// Same directive lines as IMAGE_REF_LINE_RE, but keeps them instead of // discarding — used when converting persisted server messages into // ChatMessage/ThreadMessageLike shape, where `@image:` refs need to // move from inline text into the `attachmentRefs` metadata field (mirroring