Skip to content

Claude's answers, rendered: Markdown, math, charts and diagrams drawn in the Claude Code terminal. Independent project, not affiliated with Anthropic.

TypeScript claude-code-mod

herdr in iTerm2: a failed flip tries again only after a back-off

razorback162b07f2b3e648
diff --git a/hooks/lib/pictures.tsx b/hooks/lib/pictures.tsx
index 19b145d..83ce24b 100644
--- a/hooks/lib/pictures.tsx
+++ b/hooks/lib/pictures.tsx
@@ -24,7 +24,7 @@ import { flipFile } from './flip'
import { pagesOf, type Drawing } from './answer'
import { HEADERS_SH, perlFor } from './host'
import { frameFor, pictureColumns, shownFor, type Frame } from './frame'
-import { delay, exists, ls, cat, stat, write, exec, sid, root, hide, redraw, show, uiResolve } from './api'
+import { delay, exists, ls, cat, stat, write, exec, sid, root, hide, redraw, show, uiResolve, nowMs } from './api'
export type Picture = { file: string; stamp: string; bytes: number; width: number; height: number }
export type Shown = { source: ImageSource; columns: number; rows: number; alt: string }
@@ -141,20 +141,35 @@ export async function sourceOf($: EngineInterface, ready: Env, p: Picture, rows?
// Flip mode: a PNG stored for herdr in iTerm2 drawn `rows` rows tall
// (flip.ts), in a key folder of the cache by the file, its stat stamp and
// the rows; made once (one run per key), kept from an earlier load. Null
-// when it fails (not kept: a later draw tries again).
+// when it fails: not kept, but a key that failed runs again only after a
+// back-off (FLIP_RETRY_MS, doubled each failure up to FLIP_RETRY_MAX_MS).
const flipped = new Map<string, Promise<string | null>>()
+const flipFailed = new Map<string, { at: number; wait: number }>()
+const FLIP_RETRY_MS = 5_000
+const FLIP_RETRY_MAX_MS = 60_000
function flippedOf($: EngineInterface, ready: Env, p: Picture, rows: number): Promise<string | null> {
const key = `${p.file}@${p.stamp}@${rows}`
let got = flipped.get(key)
if (!got) {
const out = `${ready.cache}/${hashOf(['flip', p.file, p.stamp, rows])}/flip.png`
got = (async (): Promise<string | null> => {
+ const failed = flipFailed.get(key)
+ const now = () => nowMs($).catch(() => Date.now())
+ if (failed && (await now()) < failed.at) {
+ if (flipped.get(key) === got) flipped.delete(key)
+ return null
+ }
const made = await stat($, out).catch(() => null)
if (made?.kind === 'file' && made.size) return out
const ok = await flipFile($, { src: p.file, out, rows, isPage: false, perl: perlFor(ready.isMac) })
- if (ok) return out
+ if (ok) {
+ flipFailed.delete(key)
+ return out
+ }
+ const wait = failed ? Math.min(FLIP_RETRY_MAX_MS, failed.wait * 2) : FLIP_RETRY_MS
+ keep(flipFailed, key, { at: (await now()) + wait, wait }, PICTURES_KEPT)
if (flipped.get(key) === got) flipped.delete(key)
- await logDebug($, ready.cache, `could not store ${p.file} for herdr in iTerm2`)
+ await logDebug($, ready.cache, `could not store ${p.file} for herdr in iTerm2 (next try in ${wait / 1000} s)`)
return null
})()
keep(flipped, key, got, PICTURES_KEPT)
diff --git a/tests/images.test.ts b/tests/images.test.ts
index ff3623b..fa7136a 100644
--- a/tests/images.test.ts
+++ b/tests/images.test.ts
@@ -603,12 +603,13 @@ test('in herdr in iTerm2, a PNG whose flip takes longer than a slice of real tim
expect(pngs.has(image.props.source.file)).toBe(true)
})
-test('in herdr in iTerm2, a picture whose flipped copy could not be stored is tried again at the next draw', async ($, on) => {
+test('in herdr in iTerm2, a picture whose flipped copy could not be stored is tried again at a draw after the back-off', async ($, on) => {
// The first write fails (as when another session wrote the same file at that time).
- const { pngs, writes } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG, failWrites: 1 })
+ const { clock, pngs, writes } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG, failWrites: 1 })
await ready($)
const ui = await $.ui.mount(sent(attach('/t/icon.png')))
expect(images(await ui.drawn())).toEqual([])
+ await clock.advance(5000)
await ui.redraw(sent(attach('/t/icon.png')).props)
const [image] = images(await ui.drawn())
expect(writes()).toBe(2)
@@ -616,6 +617,29 @@ test('in herdr in iTerm2, a picture whose flipped copy could not be stored is tr
expect([...decodePng(pngs.get(image.props.source.file)!).data]).toEqual([...flipRows(ROWS_IMG, 10).data])
})
+test('in herdr in iTerm2, many draws after a failed flip run no write until the back-off ends, which doubles after each failure', async ($, on) => {
+ const { clock, writes } = world(on, { typst: true, herdr: '/Applications/iTerm.app/Contents/MacOS/iTerm2', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG, failWrites: 2 })
+ await ready($)
+ const ui = await $.ui.mount(sent(attach('/t/icon.png')))
+ const draw = async () => {
+ await ui.redraw(sent(attach('/t/icon.png')).props)
+ return images(await ui.drawn())
+ }
+ expect(images(await ui.drawn())).toEqual([])
+ for (let i = 0; i < 10; i++) expect(await draw()).toEqual([])
+ expect(writes()).toBe(1)
+ // After 5 s: one more try, which fails; the next wait is 10 s.
+ await clock.advance(5000)
+ for (let i = 0; i < 10; i++) expect(await draw()).toEqual([])
+ expect(writes()).toBe(2)
+ await clock.advance(5000)
+ for (let i = 0; i < 10; i++) expect(await draw()).toEqual([])
+ expect(writes()).toBe(2)
+ await clock.advance(5000)
+ expect((await draw()).length).toBe(1)
+ expect(writes()).toBe(3)
+})
+
test('in herdr in Ghostty, a PNG Claude sends draws as it is', async ($, on) => {
const { converts, pngs } = world(on, { typst: true, herdr: '/Applications/Ghostty.app/Contents/MacOS/ghostty', files: { '/t/icon.png': HEAD }, bytes: ROWS_PNG })
await ready($)