( preservedAgentIds: ReadonlySet<string> = new Set(), )
| 45 | * intact since it cannot be safely scoped to the main session. |
| 46 | */ |
| 47 | export function clearSessionCaches( |
| 48 | preservedAgentIds: ReadonlySet<string> = new Set(), |
| 49 | ): void { |
| 50 | const hasPreserved = preservedAgentIds.size > 0 |
| 51 | // Clear context caches |
| 52 | getUserContext.cache.clear?.() |
| 53 | getSystemContext.cache.clear?.() |
| 54 | getGitStatus.cache.clear?.() |
| 55 | getSessionStartDate.cache.clear?.() |
| 56 | // Clear file suggestion caches (for @ mentions) |
| 57 | clearFileSuggestionCaches() |
| 58 | |
| 59 | // Clear commands/skills cache |
| 60 | clearCommandsCache() |
| 61 | |
| 62 | // Clear prompt cache break detection state |
| 63 | if (!hasPreserved) resetPromptCacheBreakDetection() |
| 64 | |
| 65 | // Clear system prompt injection (cache breaker) |
| 66 | setSystemPromptInjection(null) |
| 67 | |
| 68 | // Clear last emitted date so it's re-detected on next turn |
| 69 | setLastEmittedDate(null) |
| 70 | |
| 71 | // Run post-compaction cleanup (clears system prompt sections, microcompact tracking, |
| 72 | // classifier approvals, speculative checks, and — for main-thread compacts — memory |
| 73 | // files cache with load_reason 'compact'). |
| 74 | runPostCompactCleanup() |
| 75 | // Reset sent skill names so the skill listing is re-sent after /clear. |
| 76 | // runPostCompactCleanup intentionally does NOT reset this (post-compact |
| 77 | // re-injection costs ~4K tokens), but /clear wipes messages entirely so |
| 78 | // the model needs the full listing again. |
| 79 | resetSentSkillNames() |
| 80 | // Override the memory cache reset with 'session_start': clearSessionCaches is called |
| 81 | // from /clear and --resume/--continue, which are NOT compaction events. Without this, |
| 82 | // the InstructionsLoaded hook would fire with load_reason 'compact' instead of |
| 83 | // 'session_start' on the next getMemoryFiles() call. |
| 84 | resetGetMemoryFilesCache('session_start') |
| 85 | |
| 86 | // Clear stored image paths cache |
| 87 | clearStoredImagePaths() |
| 88 | |
| 89 | // Clear all session ingress caches (lastUuidMap, sequentialAppendBySession) |
| 90 | clearAllSessions() |
| 91 | // Clear swarm permission pending callbacks |
| 92 | if (!hasPreserved) clearAllPendingCallbacks() |
| 93 | |
| 94 | // Clear tungsten session usage tracking |
| 95 | if (process.env.USER_TYPE === 'ant') { |
| 96 | void import('../../tools/TungstenTool/TungstenTool.js').then( |
| 97 | ({ clearSessionsWithTungstenUsage, resetInitializationState }) => { |
| 98 | clearSessionsWithTungstenUsage() |
| 99 | resetInitializationState() |
| 100 | }, |
| 101 | ) |
| 102 | } |
| 103 | // Clear attribution caches (file content cache, pending bash states) |
| 104 | // Dynamic import to preserve dead code elimination for COMMIT_ATTRIBUTION feature flag |
no test coverage detected