From 07ea70ab4657cf32b9815243b5075d55a4de32cf Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Tue, 21 Jul 2026 19:07:24 -0700 Subject: [PATCH] perf(rate-limits): keep throttled statusline ticks process-free Use Claude's monotonic session duration for the POSIX throttle so skipped ticks do not replace curl churn with date churn, while retaining a fail-open date fallback. Key temp files by the stable leaf UUID so path-like or long host tab IDs cannot disable the throttle. --- src/main/claude/statusline-script.test.ts | 107 +++++++++++++++------- src/main/claude/statusline-script.ts | 51 ++++++++--- 2 files changed, 109 insertions(+), 49 deletions(-) diff --git a/src/main/claude/statusline-script.test.ts b/src/main/claude/statusline-script.test.ts index 41980c66aac..f5c255b8081 100644 --- a/src/main/claude/statusline-script.test.ts +++ b/src/main/claude/statusline-script.test.ts @@ -58,10 +58,10 @@ describe('getManagedStatusLineScript (win32 local)', () => { it('posts the buffered payload file and deletes it afterwards', () => { stubPlatform('win32') const script = getManagedStatusLineScript('local') - // Why: the temp file is per-pane (pane key with ":" mapped to "_") because %RANDOM% - // collides across cmd instances spawned in the same second. + // Why: the stable leaf UUID stays filename-safe even when a host-supplied tab id does not. + expect(script).toContain('set "ORCA_STATUSLINE_PANE_ID=%ORCA_PANE_KEY:~-36%"') expect(script).toContain( - 'set "ORCA_STATUSLINE_PAYLOAD_FILE=%TEMP%\\orca-claude-statusline-%ORCA_PANE_KEY::=_%.tmp"' + 'set "ORCA_STATUSLINE_PAYLOAD_FILE=%TEMP%\\orca-claude-statusline-%ORCA_STATUSLINE_PANE_ID%.tmp"' ) expect(script).toContain('--data-urlencode "payload@%ORCA_STATUSLINE_PAYLOAD_FILE%"') expect(script).not.toContain('payload@-') @@ -100,7 +100,7 @@ describe('getManagedStatusLineScript (win32 local)', () => { const script = getManagedStatusLineScript('local') const captureIndex = script.indexOf('more.com') const stampIndex = script.indexOf( - 'set "ORCA_STATUSLINE_STAMP_FILE=%TEMP%\\orca-claude-statusline-last-%ORCA_PANE_KEY::=_%.tmp"' + 'set "ORCA_STATUSLINE_STAMP_FILE=%TEMP%\\orca-claude-statusline-last-%ORCA_STATUSLINE_PANE_ID%.tmp"' ) const throttleIndex = script.indexOf( `if %ORCA_STATUSLINE_ELAPSED% GEQ 0 if %ORCA_STATUSLINE_ELAPSED% LSS ${CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS} goto :orca_statusline_cleanup` @@ -120,6 +120,9 @@ describe('getManagedStatusLineScript (win32 local)', () => { expect(stampWriteIndex).toBeLessThan(curlIndex) // Fail-open shape: undefined elapsed (unparseable time/stamp) proceeds to the probe. expect(script).toContain('if not defined ORCA_STATUSLINE_ELAPSED goto :orca_statusline_probe') + expect(script).toContain( + 'for /f "delims=0123456789" %%d in ("%ORCA_STATUSLINE_LAST%") do set "ORCA_STATUSLINE_LAST="' + ) // cmd parses leading-zero numbers as octal; 1%%x %% 100 defuses 08/09. expect(script).toContain('(1%%a %% 100)*3600+(1%%b %% 100)*60+(1%%c %% 100)') expect(script).toContain('set "ORCA_STATUSLINE_TIME=%TIME: =0%"') @@ -131,23 +134,23 @@ describe('statusline curl throttle (posix)', () => { stubPlatform('darwin') const script = getManagedStatusLineScript('local') const envGuardIndex = script.indexOf('-z "$ORCA_AGENT_HOOK_PORT"') - const stampIndex = script.indexOf('orca-claude-statusline-last-${ORCA_PANE_KEY}') + const durationIndex = script.indexOf('"total_duration_ms"') + const stampIndex = script.indexOf('orca-claude-statusline-last-${orca_statusline_pane_id}') const intervalIndex = script.indexOf(`-lt ${CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS}`) const curlIndex = script.indexOf('curl -sS') expect(envGuardIndex).toBeLessThan(stampIndex) - expect(stampIndex).toBeLessThan(intervalIndex) + expect(stampIndex).toBeLessThan(durationIndex) + expect(durationIndex).toBeLessThan(intervalIndex) expect(intervalIndex).toBeLessThan(curlIndex) // Fail-open shape: non-numeric date output or stamp content must never suppress the post. expect(script).toContain('case "$orca_statusline_now" in \'\'|*[!0-9]*) orca_statusline_now=') - expect(script).toContain( - 'case "$orca_statusline_last" in \'\'|*[!0-9]*) orca_statusline_last=0' - ) + expect(script).toContain('case "$orca_statusline_last" in \'\'|*[!0-9]*) orca_statusline_last=') }) }) describe.skipIf(process.platform === 'win32')('statusline curl throttle (posix behavioral)', () => { + const LEAF_ID = '00000000-0000-4000-8000-000000000000' const PANE_KEY = 'tab-1:00000000-0000-4000-8000-000000000000' - const RATE_LIMIT_PAYLOAD = '{"rate_limits":{"five_hour":{"used_percentage":12}}}' const dirs: string[] = [] afterEach(() => { @@ -156,10 +159,18 @@ describe.skipIf(process.platform === 'win32')('statusline curl throttle (posix b } }) - function makeHarness(): { scriptPath: string; dir: string; curlLog: string } { + function rateLimitPayload(durationMs: number): string { + return JSON.stringify({ + cost: { total_duration_ms: durationMs }, + rate_limits: { five_hour: { used_percentage: 12 } } + }) + } + + function makeHarness(): { scriptPath: string; dir: string; curlLog: string; dateLog: string } { const dir = mkdtempSync(join(tmpdir(), 'orca-statusline-throttle-')) dirs.push(dir) const curlLog = join(dir, 'curl.log') + const dateLog = join(dir, 'date.log') const scriptPath = join(dir, 'statusline.sh') writeFileSync(scriptPath, getManagedStatusLineScript('posix')) const binDir = join(dir, 'stub-bin') @@ -167,10 +178,20 @@ describe.skipIf(process.platform === 'win32')('statusline curl throttle (posix b writeFileSync(join(binDir, 'curl'), `#!/bin/sh\nprintf 'x\\n' >> "${curlLog}"\nexit 0\n`, { mode: 0o755 }) - return { scriptPath, dir, curlLog } + writeFileSync( + join(binDir, 'date'), + `#!/bin/sh\nprintf 'x\\n' >> "${dateLog}"\nprintf '2000000000\\n'\n`, + { mode: 0o755 } + ) + return { scriptPath, dir, curlLog, dateLog } } - function runScript(scriptPath: string, dir: string, payload: string): Promise { + function runScript( + scriptPath: string, + dir: string, + payload: string, + paneKey = PANE_KEY + ): Promise { return new Promise((resolve, reject) => { const child = spawn('sh', [scriptPath], { env: { @@ -178,7 +199,7 @@ describe.skipIf(process.platform === 'win32')('statusline curl throttle (posix b TMPDIR: dir, ORCA_AGENT_HOOK_PORT: '65535', ORCA_AGENT_HOOK_TOKEN: 'test-token', - ORCA_PANE_KEY: PANE_KEY + ORCA_PANE_KEY: paneKey }, stdio: ['pipe', 'ignore', 'pipe'] }) @@ -199,48 +220,66 @@ describe.skipIf(process.platform === 'win32')('statusline curl throttle (posix b }) } - function curlCount(curlLog: string): number { + function lineCount(logPath: string): number { try { - return readFileSync(curlLog, 'utf8').split('\n').filter(Boolean).length + return readFileSync(logPath, 'utf8').split('\n').filter(Boolean).length } catch { return 0 } } - function stampPathFor(dir: string): string { - return join(dir, `orca-claude-statusline-last-${PANE_KEY}`) + function stampPathFor(dir: string, leafId = LEAF_ID): string { + return join(dir, `orca-claude-statusline-last-${leafId}`) } - it('spawns curl once across rapid ticks, not once per tick', async () => { - const { scriptPath, dir, curlLog } = makeHarness() - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - expect(curlCount(curlLog)).toBe(1) + it('spawns one curl and no clock subprocesses across 30 rapid ticks', async () => { + const { scriptPath, dir, curlLog, dateLog } = makeHarness() + for (let index = 0; index < 30; index += 1) { + await runScript(scriptPath, dir, rateLimitPayload(1_000 + index * 100)) + } + expect(lineCount(curlLog)).toBe(1) + expect(lineCount(dateLog)).toBe(0) expect(readFileSync(stampPathFor(dir), 'utf8')).toMatch(/^[0-9]+$/) }) it('posts again once the interval has elapsed', async () => { const { scriptPath, dir, curlLog } = makeHarness() - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - const expired = Math.floor(Date.now() / 1000) - CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS - 1 - writeFileSync(stampPathFor(dir), String(expired)) - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - expect(curlCount(curlLog)).toBe(2) + await runScript(scriptPath, dir, rateLimitPayload(1_000)) + await runScript(scriptPath, dir, rateLimitPayload(16_000)) + expect(lineCount(curlLog)).toBe(2) }) - it('fails open and posts when the stamp file holds garbage', async () => { + it('fails open on a garbage stamp even early in a session', async () => { const { scriptPath, dir, curlLog } = makeHarness() writeFileSync(stampPathFor(dir), 'not-a-number') - await runScript(scriptPath, dir, RATE_LIMIT_PAYLOAD) - expect(curlCount(curlLog)).toBe(1) + await runScript(scriptPath, dir, rateLimitPayload(1_000)) + expect(lineCount(curlLog)).toBe(1) expect(readFileSync(stampPathFor(dir), 'utf8')).toMatch(/^[0-9]+$/) }) - it('never touches curl or the stamp for payloads without rate_limits', async () => { + it('uses the clock fallback when the payload omits session duration', async () => { + const { scriptPath, dir, curlLog, dateLog } = makeHarness() + const payload = '{"rate_limits":{"five_hour":{"used_percentage":12}}}' + await runScript(scriptPath, dir, payload) + await runScript(scriptPath, dir, payload) + expect(lineCount(curlLog)).toBe(1) + expect(lineCount(dateLog)).toBe(2) + }) + + it('uses only the stable leaf id for temp files', async () => { const { scriptPath, dir, curlLog } = makeHarness() + const paneKey = `${'path/segment/'.repeat(30)}tab:${LEAF_ID}` + await runScript(scriptPath, dir, rateLimitPayload(1_000), paneKey) + await runScript(scriptPath, dir, rateLimitPayload(2_000), paneKey) + expect(lineCount(curlLog)).toBe(1) + expect(readFileSync(stampPathFor(dir), 'utf8')).toBe('1') + }) + + it('never touches curl or the stamp for payloads without rate_limits', async () => { + const { scriptPath, dir, curlLog, dateLog } = makeHarness() await runScript(scriptPath, dir, '{"model":{"id":"claude-fable-5"}}') - expect(curlCount(curlLog)).toBe(0) + expect(lineCount(curlLog)).toBe(0) + expect(lineCount(dateLog)).toBe(0) expect(() => readFileSync(stampPathFor(dir), 'utf8')).toThrow() }) }) diff --git a/src/main/claude/statusline-script.ts b/src/main/claude/statusline-script.ts index 8a683a7526d..f13f2a1c2b7 100644 --- a/src/main/claude/statusline-script.ts +++ b/src/main/claude/statusline-script.ts @@ -22,20 +22,20 @@ export function getManagedStatusLineScript(target: 'local' | 'posix' = 'local'): 'setlocal', // Why: pane key is static PTY env (the endpoint file never sets it), so it can gate before stdin is consumed. `if "%ORCA_PANE_KEY%"=="" goto :${WINDOWS_HOOK_STDIN_DRAIN_LABEL}`, + // Why: tab ids may contain path characters or exceed filename limits; the stable leaf UUID is safe and globally unique. + 'set "ORCA_STATUSLINE_PANE_ID=%ORCA_PANE_KEY:~-36%"', // Why: cmd has no builtin stdin capture, so buffer the payload in a per-pane temp file // (%RANDOM% collides across same-second cmd spawns) to guard before any curl spawn. - 'set "ORCA_STATUSLINE_PAYLOAD_FILE=%TEMP%\\orca-claude-statusline-%ORCA_PANE_KEY::=_%.tmp"', + 'set "ORCA_STATUSLINE_PAYLOAD_FILE=%TEMP%\\orca-claude-statusline-%ORCA_STATUSLINE_PANE_ID%.tmp"', `${WINDOWS_HOOK_STDIN_READER} >"%ORCA_STATUSLINE_PAYLOAD_FILE%" 2>nul`, - // Why: all-builtin seconds-of-day throttle — ~3 ticks/sec while streaming would otherwise spawn - // findstr+curl each tick. `1%%x %% 100` defuses cmd's leading-zero-is-octal parse; any parse/read - // oddity (locale separators, garbage stamp, midnight wrap) leaves ELAPSED undefined or negative - // and fails open to posting, so a broken throttle can never dark the live usage feed. - 'set "ORCA_STATUSLINE_STAMP_FILE=%TEMP%\\orca-claude-statusline-last-%ORCA_PANE_KEY::=_%.tmp"', + // Why: an all-builtin seconds-of-day throttle avoids spawning findstr+curl on every streaming tick. + 'set "ORCA_STATUSLINE_STAMP_FILE=%TEMP%\\orca-claude-statusline-last-%ORCA_STATUSLINE_PANE_ID%.tmp"', 'set "ORCA_STATUSLINE_TIME=%TIME: =0%"', 'for /f "tokens=1-3 delims=:.," %%a in ("%ORCA_STATUSLINE_TIME%") do set /a "ORCA_STATUSLINE_NOW=(1%%a %% 100)*3600+(1%%b %% 100)*60+(1%%c %% 100)" 2>nul', 'set "ORCA_STATUSLINE_LAST="', 'set "ORCA_STATUSLINE_ELAPSED="', 'if exist "%ORCA_STATUSLINE_STAMP_FILE%" set /p ORCA_STATUSLINE_LAST=<"%ORCA_STATUSLINE_STAMP_FILE%"', + 'if defined ORCA_STATUSLINE_LAST for /f "delims=0123456789" %%d in ("%ORCA_STATUSLINE_LAST%") do set "ORCA_STATUSLINE_LAST="', 'if defined ORCA_STATUSLINE_LAST set /a "ORCA_STATUSLINE_ELAPSED=ORCA_STATUSLINE_NOW-ORCA_STATUSLINE_LAST" 2>nul', `if not defined ORCA_STATUSLINE_ELAPSED goto :${STATUSLINE_PROBE_LABEL}`, `if %ORCA_STATUSLINE_ELAPSED% GEQ 0 if %ORCA_STATUSLINE_ELAPSED% LSS ${CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS} goto :${STATUSLINE_CLEANUP_LABEL}`, @@ -90,19 +90,40 @@ export function getManagedStatusLineScript(target: 'local' | 'posix' = 'local'): 'if [ -z "$ORCA_AGENT_HOOK_PORT" ] || [ -z "$ORCA_AGENT_HOOK_TOKEN" ] || [ -z "$ORCA_PANE_KEY" ]; then', ' exit 0', 'fi', - // Why: rate_limits ticks arrive ~3x/sec while streaming; a per-pane stamp file bounds curl - // spawns to one per interval. Every parse/read oddity fails open to posting — a lost throttle - // costs spawns, a wrong throttle would silently dark the live usage feed. - 'orca_statusline_stamp="${TMPDIR:-/tmp}/orca-claude-statusline-last-${ORCA_PANE_KEY}"', - 'orca_statusline_now=$(date +%s 2>/dev/null) || orca_statusline_now=', + // Why: the stable leaf UUID avoids path-unsafe and overlong user-supplied tab ids. + 'orca_statusline_pane_id=${ORCA_PANE_KEY##*:}', + 'orca_statusline_stamp="${TMPDIR:-/tmp}/orca-claude-statusline-last-${orca_statusline_pane_id}"', + // Why: the payload clock keeps throttled ticks free of subprocesses; date is only a schema-drift fallback. + 'orca_statusline_now=', + 'case "$payload" in', + ' *\'"total_duration_ms"\'*)', + ' orca_statusline_duration=${payload#*\'"total_duration_ms"\'}', + ' orca_statusline_duration=${orca_statusline_duration#*:}', + ' orca_statusline_duration=${orca_statusline_duration#"${orca_statusline_duration%%[![:space:]]*}"}', + ' orca_statusline_duration=${orca_statusline_duration%%[!0-9]*}', + ' case "$orca_statusline_duration" in', + ' 0|[1-9]|[1-9][0-9]*)', + ' if [ "${#orca_statusline_duration}" -le 15 ]; then', + ' orca_statusline_now=$((orca_statusline_duration / 1000))', + ' fi', + ' ;;', + ' esac', + ' ;;', + 'esac', + 'if [ -z "$orca_statusline_now" ]; then', + ' orca_statusline_now=$(date +%s 2>/dev/null) || orca_statusline_now=', + 'fi', 'case "$orca_statusline_now" in \'\'|*[!0-9]*) orca_statusline_now= ;; esac', 'if [ -n "$orca_statusline_now" ] && [ -f "$orca_statusline_stamp" ]; then', ' orca_statusline_last=', ' IFS= read -r orca_statusline_last <"$orca_statusline_stamp" 2>/dev/null || :', - ' case "$orca_statusline_last" in \'\'|*[!0-9]*) orca_statusline_last=0 ;; esac', - ' orca_statusline_elapsed=$((orca_statusline_now - orca_statusline_last))', - ` if [ "$orca_statusline_elapsed" -ge 0 ] && [ "$orca_statusline_elapsed" -lt ${CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS} ]; then`, - ' exit 0', + ' case "$orca_statusline_last" in \'\'|*[!0-9]*) orca_statusline_last= ;; esac', + ' if [ "${#orca_statusline_last}" -gt 15 ]; then orca_statusline_last=; fi', + ' if [ -n "$orca_statusline_last" ]; then', + ' orca_statusline_elapsed=$((orca_statusline_now - orca_statusline_last))', + ` if [ "$orca_statusline_elapsed" -ge 0 ] && [ "$orca_statusline_elapsed" -lt ${CLAUDE_STATUSLINE_MIN_POST_INTERVAL_SECONDS} ]; then`, + ' exit 0', + ' fi', ' fi', 'fi', 'if [ -n "$orca_statusline_now" ]; then',