(exitOnCtrlC: boolean)
| 299 | } |
| 300 | |
| 301 | export function getRenderContext(exitOnCtrlC: boolean): { |
| 302 | renderOptions: RenderOptions; |
| 303 | getFpsMetrics: () => FpsMetrics | undefined; |
| 304 | stats: StatsStore; |
| 305 | } { |
| 306 | let lastFlickerTime = 0; |
| 307 | const baseOptions = getBaseRenderOptions(exitOnCtrlC); |
| 308 | |
| 309 | // Log analytics event when stdin override is active |
| 310 | if (baseOptions.stdin) { |
| 311 | logEvent('tengu_stdin_interactive', {}); |
| 312 | } |
| 313 | |
| 314 | const fpsTracker = new FpsTracker(); |
| 315 | const stats = createStatsStore(); |
| 316 | setStatsStore(stats); |
| 317 | |
| 318 | // Bench mode: when set, append per-frame phase timings as JSONL for |
| 319 | // offline analysis by bench/repl-scroll.ts. Captures the full TUI |
| 320 | // render pipeline (yoga → screen buffer → diff → optimize → stdout) |
| 321 | // so perf work on any phase can be validated against real user flows. |
| 322 | const frameTimingLogPath = process.env.CLAUDE_CODE_FRAME_TIMING_LOG; |
| 323 | return { |
| 324 | getFpsMetrics: () => fpsTracker.getMetrics(), |
| 325 | stats, |
| 326 | renderOptions: { |
| 327 | ...baseOptions, |
| 328 | onFrame: event => { |
| 329 | fpsTracker.record(event.durationMs); |
| 330 | stats.observe('frame_duration_ms', event.durationMs); |
| 331 | if (frameTimingLogPath && event.phases) { |
| 332 | // Bench-only env-var-gated path: sync write so no frames dropped |
| 333 | // on abrupt exit. ~100 bytes at ≤60fps is negligible. rss/cpu are |
| 334 | // single syscalls; cpu is cumulative — bench side computes delta. |
| 335 | const line = |
| 336 | // eslint-disable-next-line custom-rules/no-direct-json-operations -- tiny object, hot bench path |
| 337 | JSON.stringify({ |
| 338 | total: event.durationMs, |
| 339 | ...event.phases, |
| 340 | rss: process.memoryUsage.rss(), |
| 341 | cpu: process.cpuUsage(), |
| 342 | }) + '\n'; |
| 343 | // eslint-disable-next-line custom-rules/no-sync-fs -- bench-only, sync so no frames dropped on exit |
| 344 | appendFileSync(frameTimingLogPath, line); |
| 345 | } |
| 346 | // Skip flicker reporting for terminals with synchronized output — |
| 347 | // DEC 2026 buffers between BSU/ESU so clear+redraw is atomic. |
| 348 | if (isSynchronizedOutputSupported()) { |
| 349 | return; |
| 350 | } |
| 351 | for (const flicker of event.flickers) { |
| 352 | if (flicker.reason === 'resize') { |
| 353 | continue; |
| 354 | } |
| 355 | const now = Date.now(); |
| 356 | if (now - lastFlickerTime < 1000) { |
| 357 | logEvent('tengu_flicker', { |
| 358 | desiredHeight: flicker.desiredHeight, |
no test coverage detected