State & Memory Management
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
MarketSage utilizes a multi-layered approach to state and memory management, designed to support real-time financial analytics and intelligent shopping assistance. The system integrates Redux for client-side UI state, Chrome's local storage for persistent data, and Service Worker caching for high-performance resource management.
The architecture ensures that sensitive market data, such as price history and seller ratings, is persisted across browser sessions while maintaining a lean memory footprint through automated cache eviction and session-based conversation contexts.
Persistent State Management
The project relies on a centralized Storage utility to manage persistent data. This service handles critical entities including product caches, price histories, and seller trust metrics.
Storage Architecture
The system uses chrome.storage.local to persist complex objects. A key feature is the product cache, which implements a hard limit of 100 items to prevent excessive memory usage.
The diagram above shows the data categories managed by the Storage utility and the specific eviction policy applied to the product cache to optimize memory.
Sources: src/common/utils/storage.ts, tests/unit/utils/storage.test.ts:47-65
Key Data Structures
The persistent state is structured around several core interfaces that define the shape of market data.
| Component | Type | Description |
|---|---|---|
AppState | Interface | The root state structure including products, analysis, and UI settings. |
Product | Interface | Detailed product metadata including platform, price, and seller info. |
PriceAnalysis | Interface | Historical data and market comparison metrics. |
SellerAnalysis | Interface | Metrics including account age, trust scores, and risk levels. |
Sources: src/common/types/index.ts:98-120
Memory Management & Caching
MarketSage manages volatile memory and large assets (like AI models) through specialized service worker behaviors and transient class instances.
Service Worker Caching
The service-worker.ts file manages two distinct cache layers to optimize load times and reduce network overhead:
- Static Resource Cache: Stores UI assets like
popup.htmlandpopup.js. - Model Cache: Specifically stores large AI model files such as the
scam-detectorandseller-dna.
The flowchart illustrates the dual-cache strategy implemented in the Service Worker to manage resource loading and memory efficiency.
Sources: src/service-worker.ts:1-35
Session & Conversation Contexts
For AI-driven interactions, memory is managed via session-based maps. The SmartShoppingAssistant maintains a conversationContexts Map where each entry is subject to a 30-minute timeout. This ensures that user-specific entity extractions and message histories do not persist indefinitely in memory.
Key features include:
- Unique Identifiers: Generation of unique
sessionIdandmessageIdfor tracking state. - Automatic Context Creation: Lazy initialization of user profiles from storage only when needed.
Sources: src/core/ai/SmartShoppingAssistant.ts:89-91, src/core/ai/SmartShoppingAssistant.ts:404-420
Monitoring State & Event Streams
Real-time monitoring state is managed using Reactive Extensions (RxJS). This allows the dashboard to handle high-frequency data updates without manual state synchronization.
Dashboard State Streams
The MarketplaceMonitoringDashboard uses BehaviorSubject to maintain the "current" state of various metrics, providing observables for UI components to consume.
| Stream | Data Type | Purpose |
|---|---|---|
metricsSubject | DashboardMetrics | Real-time overview, security, and performance data. |
alertsSubject | Alert[] | Currently active system and security alerts. |
eventsSubject | MonitoringEvent[] | A rolling buffer of the last 1000 system events. |
Sources: src/core/monitoring/MarketplaceMonitoringDashboard.ts:145-148, src/core/monitoring/MarketplaceMonitoringDashboard.ts:466-471
Event Processing Flow
The sequence diagram details how system events are captured, buffered in memory (with a 1000-event limit), and processed for alert generation.
Sources: src/core/monitoring/MarketplaceMonitoringDashboard.ts:464-477, src/core/monitoring/MarketplaceMonitoringDashboard.ts:427-445
Resource Cleanup and Lifecycle
The system implements explicit cleanup mechanisms to maintain performance:
- Storage Clearing: The
Storageclass providesclearAllandclearPriceHistorymethods for manual or automated maintenance. - Cache Activation: The Service Worker
activateevent includes logic to delete outdated cache versions (CACHE_NAMEandMODEL_CACHEexclusions). - Cleanup Scripts: The project includes
cleanup_docs.ps1to remove redundant documentation and maintain a clean project structure during development.
Sources: src/common/utils/storage.ts, src/service-worker.ts:47-55, cleanup_docs.ps1:1-26
Summary
State and memory management in MarketSage is a hybrid system combining persistent storage for market intelligence and reactive, time-bound memory structures for real-time analysis. By utilizing Chrome's local storage for persistence and RxJS/Service Workers for performance, the platform maintains a responsive user experience while handling complex data processing tasks.