Something went wrong. Try again.
source dump of claude code forked from oppi.li/claude-code
Something went wrong. Try again.
3.2 kB · 94 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import { feature } from 'bun:bundle'import { initAutoDream } from '../services/autoDream/autoDream.js'import { initMagicDocs } from '../services/MagicDocs/magicDocs.js'import { initSkillImprovement } from './hooks/skillImprovement.js'
/* eslint-disable @typescript-eslint/no-require-imports */const extractMemoriesModule = feature('EXTRACT_MEMORIES') ? (require('../services/extractMemories/extractMemories.js') as typeof import('../services/extractMemories/extractMemories.js')) : nullconst registerProtocolModule = feature('LODESTONE') ? (require('./deepLink/registerProtocol.js') as typeof import('./deepLink/registerProtocol.js')) : null
/* eslint-enable @typescript-eslint/no-require-imports */
import { getIsInteractive, getLastInteractionTime } from '../bootstrap/state.js'import { cleanupNpmCacheForAnthropicPackages, cleanupOldMessageFilesInBackground, cleanupOldVersionsThrottled,} from './cleanup.js'import { cleanupOldVersions } from './nativeInstaller/index.js'import { autoUpdateMarketplacesAndPluginsInBackground } from './plugins/pluginAutoupdate.js'
// 24 hours in millisecondsconst RECURRING_CLEANUP_INTERVAL_MS = 24 * 60 * 60 * 1000
// 10 minutes after start.const DELAY_VERY_SLOW_OPERATIONS_THAT_HAPPEN_EVERY_SESSION = 10 * 60 * 1000
export function startBackgroundHousekeeping(): void { void initMagicDocs() void initSkillImprovement() if (feature('EXTRACT_MEMORIES')) { extractMemoriesModule!.initExtractMemories() } initAutoDream() void autoUpdateMarketplacesAndPluginsInBackground() if (feature('LODESTONE') && getIsInteractive()) { void registerProtocolModule!.ensureDeepLinkProtocolRegistered() }
let needsCleanup = true async function runVerySlowOps(): Promise<void> { // If the user did something in the last minute, don't make them wait for these slow operations to run. if ( getIsInteractive() && getLastInteractionTime() > Date.now() - 1000 * 60 ) { setTimeout( runVerySlowOps, DELAY_VERY_SLOW_OPERATIONS_THAT_HAPPEN_EVERY_SESSION, ).unref() return }
if (needsCleanup) { needsCleanup = false await cleanupOldMessageFilesInBackground() }
// If the user did something in the last minute, don't make them wait for these slow operations to run. if ( getIsInteractive() && getLastInteractionTime() > Date.now() - 1000 * 60 ) { setTimeout( runVerySlowOps, DELAY_VERY_SLOW_OPERATIONS_THAT_HAPPEN_EVERY_SESSION, ).unref() return }
await cleanupOldVersions() }
setTimeout( runVerySlowOps, DELAY_VERY_SLOW_OPERATIONS_THAT_HAPPEN_EVERY_SESSION, ).unref()
// For long-running sessions, schedule recurring cleanup every 24 hours. // Both cleanup functions use marker files and locks to throttle to once per day // and skip immediately if another process holds the lock. if (process.env.USER_TYPE === 'ant') { const interval = setInterval(() => { void cleanupNpmCacheForAnthropicPackages() void cleanupOldVersionsThrottled() }, RECURRING_CLEANUP_INTERVAL_MS)
// Don't let this interval keep the process alive interval.unref() }}