Sync Manager (@xtnd/vantage-sync-manager)
Package:
@xtnd/vantage-sync-manager
Source Directory:xtnd-vantage/packages/sync-manager/
1. Overview
@xtnd/vantage-sync-manager is the operational nerve center of XTND Vantage. It orchestrates:
- Non-blocking writes into local storage adapters.
- Dynamic urgency evaluation via the
VantageUrgencyEngine. - Immediate out-of-band dispatch of P0 Critical events.
- Autonomous background batch syncing of P1–P3 events with exponential backoff and network retry logic.
2. Storage Manager (VantageStorageManager)
export interface StorageManagerOptions {
storageAdapter?: IStorageAdapter; // Defaults to MemoryStorageAdapter
urgencyEvaluator?: IUrgencyEvaluator; // Defaults to VantageUrgencyEngine
transport?: ISyncTransport; // E.g. VantageHttpTransport
batchSize?: number; // Defaults to 50
syncIntervalMs?: number; // Defaults to 10,000ms (10s)
autoStartSync?: boolean; // Defaults to true
}
2.1 The Write Cycle (writeEvent)
When an application logs an event:
- Urgency is evaluated:
urgencyEvaluator.evaluate(type, payload). - A
VantageEventobject is stamped with a UUID, timestamp, priority, andsynced: false. - The event is written into the local
IStorageAdapter. - If priority is
P0_CRITICAL,dispatchImmediateEvent(event)is triggered out-of-band:- Dispatches via
transport.sendImmediate(event). - On confirmation, calls
storage.markSynced([event.id]).
- Dispatches via
- The method resolves immediately, ensuring non-blocking execution for callers.
2.2 The Batch Sync Loop (startSyncLoop)
- Initiates an interval timer (default 10s) with
syncTimer.unref()to avoid keeping Node.js process loops open. - In browsers, coordinates with
window.requestIdleCallback()to execute batch flushes only when the browser main thread is idle. - Concurrency lock:
isSyncingprevents overlapping flush executions.
3. Urgency Evaluation Engine (VantageUrgencyEngine)
The Urgency Engine assigns one of four operational priorities:
| Priority | Evaluation Triggers | Dispatch Behavior |
|---|---|---|
P0_CRITICAL |
Quota exhaustion (ai:quota:exhausted), circuit breaker trips (circuit:trip), incident declared (outage:declared), HTTP status >= 500, or explicit isCritical: true. |
Dispatched immediately via HTTP transport without waiting for the batch loop. |
P1_HIGH |
AI stream completion (ai:stream:completed), AI generation finished, user conversion, payment success, quota warning (< 20%). |
Queued for high-priority batch flush. |
P2_NORMAL |
Page initialization (page:init), token stream chunks (ai:chunk), Core Web Vitals (web_vitals), edge request metrics. |
Standard batch queue (flushed every 10 seconds). |
P3_DEFERRED |
Heartbeats (status:heartbeat), cache hits, debug traces. |
Enqueued with low priority and flushed during idle cycles. |
4. HTTP Edge Transport (VantageHttpTransport)
The HTTP transport layer handles resilient communication with Cloudflare Workers:
Features
- Exponential Backoff: Up to 3 retries for HTTP 5xx errors or network drops with exponential backoff delay (
1s,2s,4s, capped at5s). - Timeout Protection: Employs
AbortControllerwith a configurable timeout (default 5000ms). - Tab Close Delivery (
keepalive: true): Uses the browser fetchkeepaliveflag so events are successfully delivered even if a user navigates away or closes their tab. - Client Identity: Sends
X-Vantage-Client: xtnd-vantage-sdk/1.0and optionalAuthorization: Bearer <token>.
5. Usage Example
import { MemoryStorageAdapter } from '@xtnd/vantage-storage-local';
import { VantageStorageManager, VantageHttpTransport } from '@xtnd/vantage-sync-manager';
const storageAdapter = new MemoryStorageAdapter();
const transport = new VantageHttpTransport({
endpoint: 'https://api.vantage.xgi.io',
apiKey: 'secret_vantage_key'
});
const storageManager = new VantageStorageManager({
storageAdapter,
transport,
batchSize: 50,
syncIntervalMs: 5000,
autoStartSync: true
});
// Log normal event (batched)
await storageManager.writeEvent('page:init', { route: '/dashboard' });
// Log critical event (immediately sent to api.vantage.xgi.io/v1/events/urgent)
await storageManager.writeEvent('ai:quota:exhausted', {
provider: 'openai',
remainingCreditsUsd: 0,
isCritical: true
});