Files
orcs-code/src/utils/idleTimeout.ts
did:key:z6MkqDnb7Siv3Cwj7pGJq4T5EsUisECqR8KpnDLwcaZq5TPr d2542c9a62 asdf
Squash the current repository state back into one baseline commit while
preserving the README reframing and repository contents.

Constraint: User explicitly requested a single squashed commit with subject "asdf"
Confidence: high
Scope-risk: broad
Reversibility: clean
Directive: This commit intentionally rewrites published history; coordinate before future force-pushes
Tested: git status clean; local history rewritten to one commit; force-pushed main to origin and instructkr
Not-tested: Fresh clone verification after push
2026-03-31 03:34:03 -07:00

54 lines
1.5 KiB
TypeScript

import { logForDebugging } from './debug.js'
import { gracefulShutdownSync } from './gracefulShutdown.js'
/**
* Creates an idle timeout manager for SDK mode.
* Automatically exits the process after the specified idle duration.
*
* @param isIdle Function that returns true if the system is currently idle
* @returns Object with start/stop methods to control the idle timer
*/
export function createIdleTimeoutManager(isIdle: () => boolean): {
start: () => void
stop: () => void
} {
// Parse CLAUDE_CODE_EXIT_AFTER_STOP_DELAY environment variable
const exitAfterStopDelay = process.env.CLAUDE_CODE_EXIT_AFTER_STOP_DELAY
const delayMs = exitAfterStopDelay ? parseInt(exitAfterStopDelay, 10) : null
const isValidDelay = delayMs && !isNaN(delayMs) && delayMs > 0
let timer: NodeJS.Timeout | null = null
let lastIdleTime = 0
return {
start() {
// Clear any existing timer
if (timer) {
clearTimeout(timer)
timer = null
}
// Only start timer if delay is configured and valid
if (isValidDelay) {
lastIdleTime = Date.now()
timer = setTimeout(() => {
// Check if we've been continuously idle for the full duration
const idleDuration = Date.now() - lastIdleTime
if (isIdle() && idleDuration >= delayMs) {
logForDebugging(`Exiting after ${delayMs}ms of idle time`)
gracefulShutdownSync()
}
}, delayMs)
}
},
stop() {
if (timer) {
clearTimeout(timer)
timer = null
}
},
}
}