MCPcopy
hub / github.com/codeaashu/claude-code / runPostCompactCleanup

Function runPostCompactCleanup

src/services/compact/postCompactCleanup.ts:31–77  ·  view source on GitHub ↗
(querySource?: QuerySource)

Source from the content-addressed store, hash-verified

29 * genuinely main-thread-only (/compact, /clear).
30 */
31export function runPostCompactCleanup(querySource?: QuerySource): void {
32 // Subagents (agent:*) run in the same process and share module-level
33 // state with the main thread. Only reset main-thread module-level state
34 // (context-collapse, memory file cache) for main-thread compacts.
35 // Same startsWith pattern as isMainThread (index.ts:188).
36 const isMainThreadCompact =
37 querySource === undefined ||
38 querySource.startsWith('repl_main_thread') ||
39 querySource === 'sdk'
40
41 resetMicrocompactState()
42 if (feature('CONTEXT_COLLAPSE')) {
43 if (isMainThreadCompact) {
44 /* eslint-disable @typescript-eslint/no-require-imports */
45 ;(
46 require('../contextCollapse/index.js') as typeof import('../contextCollapse/index.js')
47 ).resetContextCollapse()
48 /* eslint-enable @typescript-eslint/no-require-imports */
49 }
50 }
51 if (isMainThreadCompact) {
52 // getUserContext is a memoized outer layer wrapping getClaudeMds() →
53 // getMemoryFiles(). If only the inner getMemoryFiles cache is cleared,
54 // the next turn hits the getUserContext cache and never reaches
55 // getMemoryFiles(), so the armed InstructionsLoaded hook never fires.
56 // Manual /compact already clears this explicitly at its call sites;
57 // auto-compact and reactive-compact did not — this centralizes the
58 // clear so all compaction paths behave consistently.
59 getUserContext.cache.clear?.()
60 resetGetMemoryFilesCache('compact')
61 }
62 clearSystemPromptSections()
63 clearClassifierApprovals()
64 clearSpeculativeChecks()
65 // Intentionally NOT calling resetSentSkillNames(): re-injecting the full
66 // skill_listing (~4K tokens) post-compact is pure cache_creation. The
67 // model still has SkillTool in schema, invoked_skills preserves used
68 // skills, and dynamic additions are handled by skillChangeDetector /
69 // cacheUtils resets. See compactConversation() for full rationale.
70 clearBetaTracingState()
71 if (feature('COMMIT_ATTRIBUTION')) {
72 void import('../../utils/attributionHooks.js').then(m =>
73 m.sweepFileContentCache(),
74 )
75 }
76 clearSessionMessagesCache()
77}
78

Callers 5

callFunction · 0.85
compactViaReactiveFunction · 0.85
clearSessionCachesFunction · 0.85
REPLFunction · 0.85
autoCompactIfNeededFunction · 0.85

Calls 9

resetMicrocompactStateFunction · 0.85
featureFunction · 0.85
resetGetMemoryFilesCacheFunction · 0.85
clearClassifierApprovalsFunction · 0.85
clearSpeculativeChecksFunction · 0.85
clearBetaTracingStateFunction · 0.85
clearMethod · 0.45

Tested by

no test coverage detected