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: no timed OSC 11 probe just after a key in the prompt, nor with the mod off

razorback1626d36efb5db2
diff --git a/hooks/lib/host.ts b/hooks/lib/host.ts
index 4716fda..ad4c95b 100644
--- a/hooks/lib/host.ts
+++ b/hooks/lib/host.ts
@@ -4,7 +4,7 @@
import type { EngineInterface } from 'claude-code'
import type { IuiTheme } from '../../types'
-import { CLIP_WAKE_MS, HERDR_POLL_MS, HERDR_SCAN_MS, HOST_CHECK_MS, HOST_POLL_MS, MODES } from './settings'
+import { CLIP_WAKE_MS, HERDR_POLL_MS, HERDR_SCAN_MS, HOST_CHECK_MS, HOST_POLL_MS, MODES, OSC11_QUIET_MS } from './settings'
import { S } from './state'
import { beginEpoch } from './epoch'
import { keptOsc11, luminanceOf, modeOfColorFgBg, modeOfLuminance, modeOfMacAppearance, parseOsc11, resolveMode, schemeOfPortal, type HostMode } from './mode'
@@ -769,8 +769,11 @@ export async function pollHost($: EngineInterface, modeNow = false) {
const moved = herdr && (await checkHerdrClient($).catch(() => false))
if (moved) await setOuter($)
const due = !herdr || modeNow || moved || ++S.hostTicks >= HOST_POLL_MS / HERDR_POLL_MS
- if (due) S.hostTicks = 0
- if (S.setting === 'auto' && due) S.host = (await readHostMode($).catch(() => null)) ?? S.host
+ // Under herdr a timed read asks the tty (OSC 11): not while the mod is off,
+ // nor just after a key in the prompt (it stays due: a later poll reads).
+ const asks = !S.isHerdr || modeNow || moved || (!S.isOff && (await nowMs($).catch(() => Date.now())) - S.keyAt >= OSC11_QUIET_MS)
+ if (due && asks) S.hostTicks = 0
+ if (S.setting === 'auto' && due && asks) S.host = (await readHostMode($).catch(() => null)) ?? S.host
if (moved) refonted = await refont($).catch(() => false)
} finally {
S.checking = false
diff --git a/hooks/lib/settings.ts b/hooks/lib/settings.ts
index 691e86a..bcdd799 100644
--- a/hooks/lib/settings.ts
+++ b/hooks/lib/settings.ts
@@ -83,6 +83,10 @@ export const HOST_CHECK_MS = 2000
// (the process list: new or gone clients).
export const HERDR_POLL_MS = 1000
export const HERDR_SCAN_MS = 10000
+// Under herdr the mode read asks the pane's tty (OSC 11), which Claude Code
+// reads too: a key typed then can go to the probe. So a poll does not ask
+// within this time of a key in the prompt box.
+export const OSC11_QUIET_MS = 2000
// Copy: how often the watcher reads the clipboard's change count (macOS;
// one call in the watcher, no process), and the word positions of how
// many pages are kept in memory (a copy probes only the pages it reads).
diff --git a/hooks/lib/state.ts b/hooks/lib/state.ts
index 594ffa2..e665081 100644
--- a/hooks/lib/state.ts
+++ b/hooks/lib/state.ts
@@ -222,6 +222,8 @@ export const S = {
herdrDraws: false,
isOff: false,
hostEnded: false,
+ // The clock time of the last key in the prompt box (register.tsx; OSC11_QUIET_MS).
+ keyAt: Number.NEGATIVE_INFINITY,
checking: false,
// Linux: the tool that reads the desktop portal's colour scheme (host.ts;
// null: not tried yet, '': none works, so GNOME's gsettings is read).
diff --git a/hooks/register.tsx b/hooks/register.tsx
index 0114ade..dff2ed3 100644
--- a/hooks/register.tsx
+++ b/hooks/register.tsx
@@ -871,6 +871,7 @@ export const register: Register = on => {
on('prompt.edit', async ($, e, next) => {
const isEdit = !!e.inputText || e.end > e.start
if (picked.now && isEdit) clearSelection($)
+ void Promise.resolve().then(() => nowMs($)).then(t => void (S.keyAt = t), () => undefined)
const box = await next(e)
if (isEdit && e.origin.kind === 'composer') noteDraft($, box.text)
return box
diff --git a/tests/mode.test.ts b/tests/mode.test.ts
index 7ca4689..47f5570 100644
--- a/tests/mode.test.ts
+++ b/tests/mode.test.ts
@@ -208,6 +208,44 @@ test('with the theme on auto, herdr takes the OSC 11 background first, and keeps
expect(probes).toBe(5)
})
+// The OSC 11 probe reads the pane's tty, as Claude Code does: a key typed
+// then can go to the probe. A timed probe waits for 2 s with no key in the
+// prompt, and none runs while the mod is off.
+test('under herdr, no timed OSC 11 probe just after a key in the prompt, nor with the mod off', async ($, on) => {
+ mock.store(on)
+ const clock = mock.clock(on)
+ mock.env(on, { HOME: '/h', TERM_PROGRAM: 'herdr', HERDR_ENV: '1', TERM: 'xterm-256color', GHOSTTY_RESOURCES_DIR: '/R', CLAUDE_CODE_FORCE_TERMINAL_IMAGES: '1' })
+ on('config.list', async () => ({ value: [{ key: 'theme', value: 'auto' }] as any }))
+ const ok = (stdout: string, exitCode = 0) => ({ value: { exitCode, stdout, stderr: '', isStdoutTruncated: false, isStderrTruncated: false } })
+ let probes = 0
+ on('process.run', async ($: any, e: any, next: any) => {
+ const argv: string[] = e.argv
+ const cmd = argv.join(' ')
+ if (argv[0] === 'sh' && /cc-intelligent-ui-/.test(cmd)) return ok('/tmp/mdp\nDarwin\n')
+ if (argv[0] === 'perl' && cmd.includes('11;?')) return (probes++, ok('\x1b]11;rgb:ffff/ffff/ffff\x1b\\'))
+ if (argv[0] === 'defaults' && argv.includes('AppleInterfaceStyle')) return ok('', 1)
+ return next(e)
+ })
+ on('prompt.edit', async ($: any, e: any) => ({ text: e.text + e.inputText, cursor: e.text.length + e.inputText.length }))
+ await $.command.run({ command: 'iui', args: '' } as any)
+ expect(probes).toBe(1)
+ // A key each 500 ms for 6 s: no timed probe.
+ let text = ''
+ for (let i = 0; i < 12; i++) {
+ await ($ as any).prompt.edit({ origin: { kind: 'composer' }, text, cursor: text.length, start: text.length, end: text.length, inputText: 'a' })
+ text += 'a'
+ await clock.advance(500)
+ }
+ expect(probes).toBe(1)
+ // 2 s with no key: the next poll asks.
+ await clock.advance(3000)
+ expect(probes).toBe(2)
+ // The mod off: none.
+ await $.command.run({ command: 'iui', args: 'off' } as any)
+ await clock.advance(9000)
+ expect(probes).toBe(2)
+})
+
// herdr --remote: the pane is over ssh (the mod runs on the remote
// computer), but herdr's OSC 11 answer is still the user's terminal.
test('with the theme on auto, a remote herdr pane takes the OSC 11 background first, then COLORFGBG', async ($, on) => {