Skip to content

The agent that grows with you

fix(desktop): a saved comment crop shows its own marker, and only its own

Brooklyn Nicholson6ef6691960a2
diff --git a/apps/desktop/src/app/chat/right-rail/preview-annotate-host.test.ts b/apps/desktop/src/app/chat/right-rail/preview-annotate-host.test.ts
index d399bfa9f19..7ab5d185a2c 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-annotate-host.test.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-annotate-host.test.ts
@@ -57,4 +57,56 @@ describe('preview annotate host', () => {
y: 20 - ANNOTATE_CROP_PAD
})
})
+
+ it('brackets the shot so the crop is taken with only this comment marked', async () => {
+ const order: string[] = []
+
+ const executeJavaScript = vi.fn(async (code: string) => {
+ order.push(code.includes('beginCapture') ? 'begin' : 'end')
+
+ return true
+ })
+
+ const capture = vi.fn(async () => {
+ order.push('capture')
+
+ return 'data:image/png;base64,AA=='
+ })
+
+ await captureAnnotateCrop({ capture, executeJavaScript }, { height: 16, width: 40, x: 10, y: 20 })
+
+ expect(order).toEqual(['begin', 'capture', 'end'])
+ })
+
+ it('restores saved pins even when the capture fails', async () => {
+ const executeJavaScript = vi.fn(async (code: string) => {
+ void code
+
+ return true
+ })
+
+ const capture = vi.fn(async () => {
+ throw new Error('capture exploded')
+ })
+
+ await expect(
+ captureAnnotateCrop({ capture, executeJavaScript }, { height: 16, width: 40, x: 10, y: 20 })
+ ).rejects.toThrow('capture exploded')
+
+ expect(executeJavaScript.mock.calls.some(([code]) => String(code).includes('endCapture'))).toBe(true)
+ })
+
+ it('still captures when the overlay cannot be reached', async () => {
+ const capture = vi.fn(async () => 'data:image/png;base64,AA==')
+
+ const executeJavaScript = vi.fn(async (code: string) => {
+ void code
+
+ throw new Error('guest is gone')
+ })
+
+ await expect(
+ captureAnnotateCrop({ capture, executeJavaScript }, { height: 16, width: 40, x: 10, y: 20 })
+ ).resolves.toContain('data:image/png')
+ })
})
diff --git a/apps/desktop/src/app/chat/right-rail/preview-annotate-host.ts b/apps/desktop/src/app/chat/right-rail/preview-annotate-host.ts
index 1ce04b5bcbd..1fdad4eb090 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-annotate-host.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-annotate-host.ts
@@ -95,6 +95,15 @@ export async function hideAnnotateDraft(guest: PreviewAnnotateGuest): Promise<vo
`)
}
+/** Guest calls are best-effort dressing: never let one fail the capture. */
+async function tryGuest(guest: PreviewAnnotateGuest, code: string): Promise<void> {
+ try {
+ await guest.executeJavaScript(code)
+ } catch {
+ // The overlay may be mid-teardown or the guest gone. Shoot anyway.
+ }
+}
+
export async function captureAnnotateCrop(
guest: PreviewAnnotateGuest,
rect: AnnotatePinChrome['rect']
@@ -103,5 +112,15 @@ export async function captureAnnotateCrop(
throw new Error('preview capture is unavailable')
}
- return guest.capture(padRect(rect))
+ // Bracket the shot: the overlay hides saved pins and waits for a paint, so
+ // the crop carries this comment's marker and no neighbour's. `endCapture`
+ // runs even when the capture throws, or one failed crop leaves every saved
+ // pin invisible on the page.
+ await tryGuest(guest, 'window.__hermesAnnotate ? window.__hermesAnnotate.beginCapture() : null')
+
+ try {
+ return await guest.capture(padRect(rect))
+ } finally {
+ await tryGuest(guest, 'window.__hermesAnnotate && window.__hermesAnnotate.endCapture()')
+ }
}
diff --git a/apps/desktop/src/lib/preview-annotate/in-page.test.ts b/apps/desktop/src/lib/preview-annotate/in-page.test.ts
index 203cec37758..b289c70b7db 100644
--- a/apps/desktop/src/lib/preview-annotate/in-page.test.ts
+++ b/apps/desktop/src/lib/preview-annotate/in-page.test.ts
@@ -224,6 +224,56 @@ describe('annotateInPage overlay', () => {
api.teardown()
})
+ it('shows the draft marker before a capture can photograph it', async () => {
+ const api = annotateInPage(document)
+ api.install()
+ api.showDraft({ height: 24, width: 80, x: 10, y: 10 }, 2)
+
+ const ready = await api.beginCapture()
+ const shadow = document.querySelector('hermes-annotate')!.shadowRoot!
+ const draft = shadow.querySelector('[data-annotate-outline="draft"]') as HTMLElement
+ const marker = draft.querySelector('[data-annotate-marker]')
+
+ expect(ready).toBe(true)
+ expect(draft.style.display).toBe('block')
+ expect(marker?.getAttribute('data-annotate-marker')).toBe('2')
+ api.teardown()
+ })
+
+ it('hides saved pins during a capture so a neighbour marker cannot land in the crop', async () => {
+ const api = annotateInPage(document)
+ api.install()
+ api.showPins([
+ { kind: 'element', number: 1, rect: { height: 24, width: 80, x: 10, y: 10 } },
+ { kind: 'element', number: 2, rect: { height: 24, width: 80, x: 10, y: 40 } }
+ ])
+ api.showDraft({ height: 24, width: 80, x: 10, y: 70 }, 3)
+
+ const shadow = document.querySelector('hermes-annotate')!.shadowRoot!
+ const pins = () => shadow.querySelector('[data-annotate-pin="1"]')!.parentElement as HTMLElement
+
+ expect(pins().style.display).not.toBe('none')
+
+ await api.beginCapture()
+
+ expect(pins().style.display).toBe('none')
+ // The draft's own marker must survive — it is the point of the crop.
+ expect(
+ (shadow.querySelector('[data-annotate-outline="draft"]') as HTMLElement).style.display
+ ).toBe('block')
+
+ api.endCapture()
+
+ expect(pins().style.display).toBe('block')
+ api.teardown()
+ })
+
+ it('refuses to dress a capture when the overlay is gone', async () => {
+ const api = annotateInPage(document)
+
+ expect(await api.beginCapture()).toBe(false)
+ })
+
it('owns wheel scrolling instead of also allowing the native wheel action', () => {
const scroller = document.createElement('div')
scroller.style.overflowY = 'auto'
diff --git a/apps/desktop/src/lib/preview-annotate/in-page.ts b/apps/desktop/src/lib/preview-annotate/in-page.ts
index 7aee415d5c7..2de7bf5a0b5 100644
--- a/apps/desktop/src/lib/preview-annotate/in-page.ts
+++ b/apps/desktop/src/lib/preview-annotate/in-page.ts
@@ -40,6 +40,8 @@ export interface AnnotatePinChrome {
}
export interface AnnotateInPage {
+ beginCapture: () => Promise<boolean>
+ endCapture: () => void
getMarkerNumbers: () => number[]
getOutlineColor: () => string
hideDraft: () => void
@@ -732,6 +734,58 @@ export function annotateInPage(doc: Document): AnnotateInPage {
}
}
+ /**
+ * Two frames. `executeJavaScript` resolving only means the style property is
+ * set — the compositor has not drawn it yet, and `capturePage` grabs whatever
+ * is on screen. Capturing straight after `showDraft` therefore photographs
+ * the page one frame before the marker exists, which is why saved crops came
+ * back outlined but unnumbered. One rAF schedules us before the next paint;
+ * the second lands after it.
+ */
+ const afterPaint = (): Promise<void> =>
+ new Promise(resolve => {
+ if (!win) {
+ resolve()
+
+ return
+ }
+
+ win.requestAnimationFrame(() => win.requestAnimationFrame(() => resolve()))
+ })
+
+ /**
+ * Dress the page for one crop: the draft's own marker, nothing else.
+ *
+ * Saved pins live in the page, so a neighbour's marker lands inside this
+ * crop whenever the two elements sit within the crop padding of each other —
+ * a comment on a heading came back carrying the marker of the comment on the
+ * paragraph below it, and "Image 2 marks the target in blue" then pointed at
+ * a 1. Hover chrome is transient but would be captured just the same.
+ */
+ const beginCapture = async (): Promise<boolean> => {
+ if (!host || !host.isConnected) {
+ return false
+ }
+
+ if (pinsLayer) {
+ style(pinsLayer, { display: 'none' })
+ }
+
+ if (hoverBox) {
+ style(hoverBox, { display: 'none' })
+ }
+
+ await afterPaint()
+
+ return true
+ }
+
+ const endCapture = () => {
+ if (pinsLayer) {
+ style(pinsLayer, { display: 'block' })
+ }
+ }
+
const teardown = () => {
unbind()
emit({ type: 'end' })
@@ -750,6 +804,8 @@ export function annotateInPage(doc: Document): AnnotateInPage {
}
return {
+ beginCapture,
+ endCapture,
getMarkerNumbers: () => {
if (!shadow) {
return []