fix(tui): isolate turn state from app render

This commit is contained in:
Brooklyn Nicholson 2026-04-26 15:40:38 -05:00
parent 6a3873942f
commit f6846205cc
3 changed files with 61 additions and 8 deletions

View file

@ -5,6 +5,8 @@ import { pick, toolTrailLabel } from '../lib/text.js'
import type { ActiveTool } from '../types.js'
import { turnController } from './turnController.js'
import { useTurnSelector } from './turnStore.js'
import { getUiState } from './uiStore.js'
const DELAY_MS = 8_000
const INTERVAL_MS = 10_000
@ -15,21 +17,28 @@ interface Slot {
lastAt: number
}
export function useLongRunToolCharms(busy: boolean, tools: ActiveTool[]) {
export function useLongRunToolCharms() {
const tools = useTurnSelector(state => state.tools)
const slots = useRef(new Map<string, Slot>())
useEffect(() => {
if (!busy || !tools.length) {
if (!getUiState().busy || !tools.length) {
slots.current.clear()
return
}
const tick = () => {
if (!getUiState().busy) {
slots.current.clear()
return
}
const now = Date.now()
const liveIds = new Set(tools.map(t => t.id))
for (const key of [...slots.current.keys()]) {
for (const key of Array.from(slots.current.keys())) {
if (!liveIds.has(key)) {
slots.current.delete(key)
}
@ -57,5 +66,5 @@ export function useLongRunToolCharms(busy: boolean, tools: ActiveTool[]) {
const id = setInterval(tick, 1000)
return () => clearInterval(id)
}, [busy, tools])
}, [tools])
}

View file

@ -29,7 +29,7 @@ import { type GatewayRpc, type TranscriptRow } from './interfaces.js'
import { $overlayState, patchOverlayState } from './overlayStore.js'
import { scrollWithSelectionBy } from './scroll.js'
import { turnController } from './turnController.js'
import { $turnState, patchTurnState, useTurnSelector } from './turnStore.js'
import { patchTurnState, useTurnSelector } from './turnStore.js'
import { $uiState, getUiState, patchUiState } from './uiStore.js'
import { useComposerState } from './useComposerState.js'
import { useConfigSync } from './useConfigSync.js'
@ -107,8 +107,6 @@ export function useMainApp(gw: GatewayClient) {
const ui = useStore($uiState)
const overlay = useStore($overlayState)
const turn = useStore($turnState)
const turnLiveTailActive = useTurnSelector(state =>
Boolean(
state.streaming ||
@ -503,7 +501,7 @@ export function useMainApp(gw: GatewayClient) {
}
}, [gw, sys])
useLongRunToolCharms(ui.busy, turn.tools)
useLongRunToolCharms()
const slash = useMemo(
() =>