()
| 44 | let generating = false |
| 45 | |
| 46 | function scheduleTick(): void { |
| 47 | const nextTs = Date.now() + TICK_INTERVAL_MS |
| 48 | setNextTickAt(nextTs) |
| 49 | |
| 50 | timer = setTimeout(() => { |
| 51 | timer = null |
| 52 | |
| 53 | // Guard: skip tick if any blocking condition is met |
| 54 | if (!shouldTick()) { |
| 55 | // Reschedule — conditions may clear later |
| 56 | scheduleTick() |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | const { |
| 61 | isLoading, |
| 62 | queuedCommandsLength, |
| 63 | hasActiveLocalJsxUI, |
| 64 | isInPlanMode, |
| 65 | } = optsRef.current |
| 66 | |
| 67 | // Don't fire while a query is in-flight, plan mode is active, |
| 68 | // a local JSX UI is showing, or commands are queued |
| 69 | if ( |
| 70 | isLoading || |
| 71 | isInPlanMode || |
| 72 | hasActiveLocalJsxUI || |
| 73 | queuedCommandsLength > 0 || |
| 74 | generating |
| 75 | ) { |
| 76 | scheduleTick() |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | generating = true |
| 81 | void (async () => { |
| 82 | const commands = await createProactiveAutonomyCommands({ |
| 83 | basePrompt: `<${TICK_TAG}>${new Date().toLocaleTimeString()}</${TICK_TAG}>`, |
| 84 | currentDir: getCwd(), |
| 85 | shouldCreate: () => !disposed, |
| 86 | }) |
| 87 | if (disposed) { |
| 88 | await cancelQueuedAutonomyCommands({ commands }) |
| 89 | return |
| 90 | } |
| 91 | const queuedCommands: QueuedCommand[] = [] |
| 92 | try { |
| 93 | for (const command of commands) { |
| 94 | // Always queue proactive turns. This avoids races where the prompt |
| 95 | // is built asynchronously, a user turn starts meanwhile, and a |
| 96 | // direct-submit path would silently drop the autonomy turn after |
| 97 | // consuming its heartbeat due-state. |
| 98 | optsRef.current.onQueueTick(command) |
| 99 | queuedCommands.push(command) |
| 100 | } |
| 101 | } catch (error) { |
| 102 | await cancelQueuedAutonomyCommands({ |
| 103 | commands: commands.filter( |
no test coverage detected