MCPcopy Create free account
hub / github.com/codeaashu/claude-code / createIdleTimeoutManager

Function createIdleTimeoutManager

src/utils/idleTimeout.ts:11–53  ·  view source on GitHub ↗
(isIdle: () => boolean)

Source from the content-addressed store, hash-verified

9 * @returns Object with start/stop methods to control the idle timer
10 */
11export function createIdleTimeoutManager(isIdle: () => boolean): {
12 start: () => void
13 stop: () => void
14} {
15 // Parse CLAUDE_CODE_EXIT_AFTER_STOP_DELAY environment variable
16 const exitAfterStopDelay = process.env.CLAUDE_CODE_EXIT_AFTER_STOP_DELAY
17 const delayMs = exitAfterStopDelay ? parseInt(exitAfterStopDelay, 10) : null
18 const isValidDelay = delayMs && !isNaN(delayMs) && delayMs > 0
19
20 let timer: NodeJS.Timeout | null = null
21 let lastIdleTime = 0
22
23 return {
24 start() {
25 // Clear any existing timer
26 if (timer) {
27 clearTimeout(timer)
28 timer = null
29 }
30
31 // Only start timer if delay is configured and valid
32 if (isValidDelay) {
33 lastIdleTime = Date.now()
34
35 timer = setTimeout(() => {
36 // Check if we've been continuously idle for the full duration
37 const idleDuration = Date.now() - lastIdleTime
38 if (isIdle() && idleDuration >= delayMs) {
39 logForDebugging(`Exiting after ${delayMs}ms of idle time`)
40 gracefulShutdownSync()
41 }
42 }, delayMs)
43 }
44 },
45
46 stop() {
47 if (timer) {
48 clearTimeout(timer)
49 timer = null
50 }
51 },
52 }
53}
54

Callers 1

runHeadlessStreamingFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected