forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackgroundHousekeeping.ts
More file actions
94 lines (81 loc) · 3.14 KB
/
backgroundHousekeeping.ts
File metadata and controls
94 lines (81 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { 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'))
: null
const 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 milliseconds
const 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()
}
}