Compare commits

...
Author SHA1 Message Date
abeatrix c313d7af98 Merge branch 'main' into bee/temp-in-memory-cache 2025-08-15 10:27:13 -07:00
abeatrix caf0cd45d8 revert wrong git stash 2025-08-15 10:19:04 -07:00
abeatrix 45b5c1ab60 Add in-memory mode support to CacheService for testing
- Add inMemoryMode flag that activates when TEMP_PROFILE and IS_DEV env vars are set
- Skip disk persistence operations when in in-memory mode
- Add methods to control and query in-memory mode state
- Reorganize imports using type-only imports where appropriate
2025-08-15 10:10:08 -07:00
+55 -14
View File
@@ -1,10 +1,10 @@
import { ApiConfiguration } from "@shared/api"
import { SecretKey, GlobalStateKey, LocalStateKey, GlobalState, Secrets, LocalState } from "./state-keys"
import { CACHE_SERVICE_NOT_INITIALIZED } from "./error-messages"
import type { ExtensionContext } from "vscode"
import { readStateFromDisk } from "./utils/state-helpers"
import { DEFAULT_AUTO_APPROVAL_SETTINGS } from "@/shared/AutoApprovalSettings"
import type { ApiConfiguration } from "@shared/api"
import { DEFAULT_FOCUS_CHAIN_SETTINGS } from "@shared/FocusChainSettings"
import type { ExtensionContext } from "vscode"
import { DEFAULT_AUTO_APPROVAL_SETTINGS } from "@/shared/AutoApprovalSettings"
import { CACHE_SERVICE_NOT_INITIALIZED } from "./error-messages"
import type { GlobalState, GlobalStateKey, LocalState, LocalStateKey, SecretKey, Secrets } from "./state-keys"
import { readStateFromDisk } from "./utils/state-helpers"
/**
* Interface for persistence error event data
@@ -34,8 +34,15 @@ export class CacheService {
// Callback for persistence errors
onPersistenceError?: (event: PersistenceErrorEvent) => void
/**
* Check if the cache service is in in-memory only mode
* Only available in testing mode with temporary profile enabled
*/
private readonly isInMemoryOnlyMode: boolean
constructor(context: ExtensionContext) {
this.context = context
this.isInMemoryOnlyMode = this.setInMemoryStoreMode()
this.context = this.isInMemoryOnlyMode ? ({} as ExtensionContext) : context
}
/**
@@ -43,13 +50,16 @@ export class CacheService {
*/
async initialize(): Promise<void> {
try {
// Load all extension state from disk
const state = await readStateFromDisk(this.context)
// In special testing mode, we initialize with empty caches
if (!this.isInMemoryOnlyMode) {
// Load all extension state from disk
const state = await readStateFromDisk(this.context)
if (state) {
// Populate the caches with all extension state fields
// Use populate method to avoid triggering persistence during initialization
this.populateCache(state)
if (state) {
// Populate the caches with all extension state fields
// Use populate method to avoid triggering persistence during initialization
this.populateCache(state)
}
}
this.isInitialized = true
@@ -480,10 +490,32 @@ export class CacheService {
// Clear all cached data and pending state
this.dispose()
// Reinitialize from disk
// Reinitialize from disk (or empty if in-memory mode)
await this.initialize()
}
/**
* Enable or disable in-memory only mode
* When enabled, no data is persisted to disk
*/
private setInMemoryStoreMode(): boolean {
if (!(process?.env?.TEMP_PROFILE && process?.env?.IS_DEV)) {
return false
}
this.globalStateCache = {} as GlobalState
this.secretsCache = {} as Secrets
this.workspaceStateCache = {} as LocalState
// Clear any pending persistence operations
if (this.persistenceTimeout) {
clearTimeout(this.persistenceTimeout)
this.persistenceTimeout = null
}
this.pendingGlobalState.clear()
this.pendingSecrets.clear()
this.pendingWorkspaceState.clear()
return true
}
/**
* Dispose of the cache service
*/
@@ -508,6 +540,15 @@ export class CacheService {
* Schedule debounced persistence - simple timeout-based persistence
*/
private scheduleDebouncedPersistence(): void {
// Skip persistence entirely in in-memory only mode
if (this.isInMemoryOnlyMode) {
// Clear pending sets immediately since we're not persisting
this.pendingGlobalState.clear()
this.pendingSecrets.clear()
this.pendingWorkspaceState.clear()
return
}
// Clear existing timeout if one is pending
if (this.persistenceTimeout) {
clearTimeout(this.persistenceTimeout)