-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsyncService.ts
More file actions
713 lines (642 loc) · 26.4 KB
/
syncService.ts
File metadata and controls
713 lines (642 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/**
* Sync service for backend facade.
* Handles background sync, timer management, and daily rollup computation.
*/
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as os from 'os';
import { DefaultAzureCredential } from '@azure/identity';
import { safeStringifyError } from '../../utils/errors';
import type { DailyRollupKey } from '../rollups';
import { upsertDailyRollup } from '../rollups';
import type { BackendSettings } from '../settings';
import { BACKEND_SYNC_MIN_INTERVAL_MS } from '../constants';
import type { DailyRollupValue, ChatRequest, SessionFileCache } from '../types';
import { resolveUserIdentityForSync, type BackendUserIdentityMode } from '../identity';
import { computeBackendSharingPolicy, hashMachineIdForTeam, hashWorkspaceIdForTeam } from '../sharingProfile';
import { createDailyAggEntity } from '../storageTables';
import { CredentialService } from './credentialService';
import { DataPlaneService } from './dataPlaneService';
import { BackendUtility } from './utilityService';
/**
* Validate and normalize consent timestamp.
* Returns ISO string if valid, undefined if invalid or in the future.
*/
function validateConsentTimestamp(ts: string | undefined, logger?: (msg: string) => void): string | undefined {
if (!ts) {
return undefined;
}
try {
const parsed = new Date(ts);
if (isNaN(parsed.getTime())) {
if (logger) {
logger(`Invalid consent timestamp (not a valid date): "${ts}"`);
}
return undefined;
}
if (parsed.getTime() > Date.now()) {
if (logger) {
logger(`Invalid consent timestamp (future date): "${ts}" (parsed: ${parsed.toISOString()})`);
}
return undefined;
}
return parsed.toISOString();
} catch (e) {
if (logger) {
logger(`Failed to parse consent timestamp: "${ts}", error: ${e}`);
}
return undefined;
}
}
export interface SyncServiceDeps {
context: vscode.ExtensionContext | undefined;
log: (message: string) => void;
warn: (message: string) => void;
getCopilotSessionFiles: () => Promise<string[]>;
estimateTokensFromText: (text: string, model: string) => number;
getModelFromRequest: (request: ChatRequest) => string;
// Cache integration for performance
getSessionFileDataCached?: (sessionFilePath: string, mtime: number) => Promise<SessionFileCache>;
// UI refresh callback after successful sync
updateTokenStats?: () => Promise<void>;
}
/**
* SyncService manages background synchronization of local session data to the backend.
*/
export class SyncService {
private backendSyncInProgress = false;
private syncQueue = Promise.resolve();
private backendSyncInterval: NodeJS.Timeout | undefined;
private consecutiveFailures = 0;
private readonly MAX_CONSECUTIVE_FAILURES = 5;
constructor(
private readonly deps: SyncServiceDeps,
private readonly credentialService: CredentialService,
private readonly dataPlaneService: DataPlaneService,
private readonly utility: typeof BackendUtility
) {}
/**
* Start the background sync timer if backend is enabled.
* @param settings - Backend settings to check if sync should be enabled
* @param isConfigured - Whether the backend is fully configured
*/
startTimerIfEnabled(settings: BackendSettings, isConfigured: boolean): void {
try {
this.stopTimer();
const sharingPolicy = computeBackendSharingPolicy({
enabled: settings.enabled,
profile: settings.sharingProfile,
shareWorkspaceMachineNames: settings.shareWorkspaceMachineNames
});
if (!sharingPolicy.allowCloudSync || !isConfigured) {
return;
}
const intervalMs = Math.max(BACKEND_SYNC_MIN_INTERVAL_MS, settings.lookbackDays * 60 * 1000);
this.deps.log(`Backend sync: starting timer with interval ${intervalMs}ms`);
this.backendSyncInterval = setInterval(() => {
this.syncToBackendStore(false, settings, isConfigured).catch((e) => {
this.deps.warn(`Backend sync timer failed: ${e?.message ?? e}`);
this.consecutiveFailures++;
// Show user-facing warning after first few failures
if (this.consecutiveFailures === 3) {
vscode.window.showWarningMessage(
'Backend sync is experiencing issues. Check the output panel for details.',
'Show Output'
).then(choice => {
if (choice === 'Show Output') {
// User can manually open output panel via command palette
}
});
}
if (this.consecutiveFailures >= this.MAX_CONSECUTIVE_FAILURES) {
this.deps.warn(`Backend sync: stopping timer after ${this.MAX_CONSECUTIVE_FAILURES} consecutive failures`);
vscode.window.showErrorMessage(
'Backend sync stopped after repeated failures. Check your Azure configuration.',
'Configure Backend'
).then(choice => {
if (choice === 'Configure Backend') {
vscode.commands.executeCommand('copilotTokenTracker.configureBackend');
}
});
this.stopTimer();
}
});
}, intervalMs);
// Immediate initial sync (forced to ensure settings changes take effect)
this.syncToBackendStore(true, settings, isConfigured).catch((e) => {
this.deps.warn(`Backend sync initial sync failed: ${e?.message ?? e}`);
});
} catch (e) {
this.deps.warn(`Backend sync timer setup failed: ${e}`);
}
}
/**
* Stop the background sync timer.
*/
stopTimer(): void {
if (this.backendSyncInterval) {
clearInterval(this.backendSyncInterval);
this.backendSyncInterval = undefined;
this.consecutiveFailures = 0;
}
}
/**
* Dispose the sync service.
*/
dispose(): void {
this.stopTimer();
}
/**
* Get the current sync queue promise (for testing).
*/
getSyncQueue(): Promise<void> {
return this.syncQueue;
}
/**
* Process a session file using cached data for token counts but extracting accurate timestamps.
* Returns true if successful, false if cache miss (caller should parse file).
* Validates all cached data at runtime to prevent injection/corruption.
*
* CRITICAL: We parse the file to extract actual interaction timestamps and create per-day
* rollups, but use cached token counts for performance. This ensures accurate day assignment
* while still benefiting from cached calculations.
*/
private async processCachedSessionFile(
sessionFile: string,
fileMtimeMs: number,
workspaceId: string,
machineId: string,
userId: string | undefined,
rollups: Map<string, { key: DailyRollupKey; value: DailyRollupValue }>,
startMs: number,
now: Date
): Promise<boolean> {
try {
const cachedData = await this.deps.getSessionFileDataCached!(sessionFile, fileMtimeMs);
// Validate cached data structure to prevent injection/corruption
if (!cachedData || typeof cachedData !== 'object') {
this.deps.warn(`Backend sync: invalid cached data structure for ${sessionFile}`);
return false;
}
if (typeof cachedData.modelUsage !== 'object' || cachedData.modelUsage === null) {
this.deps.warn(`Backend sync: invalid modelUsage in cached data for ${sessionFile}`);
return false;
}
if (!Number.isFinite(cachedData.interactions) || cachedData.interactions < 0) {
this.deps.warn(`Backend sync: invalid interactions count in cached data for ${sessionFile}`);
return false;
}
// Parse the session file to get actual request timestamps and create per-day rollups
// This ensures accurate day assignment while using cached token counts
const content = await fs.promises.readFile(sessionFile, 'utf8');
// Map to track per-day per-model interactions for proper distribution
const dayModelInteractions = new Map<string, Map<string, number>>();
// Handle JSONL format (Copilot CLI)
if (sessionFile.endsWith('.jsonl')) {
const lines = content.trim().split('\n');
const todayKey = this.utility.toUtcDayKey(now);
let lineCount = 0;
let processedLines = 0;
for (const line of lines) {
lineCount++;
if (!line.trim()) { continue; }
try {
const event = JSON.parse(line);
if (!event || typeof event !== 'object') { continue; }
const normalizedTs = this.utility.normalizeTimestampToMs(event.timestamp);
const eventMs = Number.isFinite(normalizedTs) ? normalizedTs : fileMtimeMs;
if (!eventMs || eventMs < startMs) { continue; }
const dayKey = this.utility.toUtcDayKey(new Date(eventMs));
const model = (event.model || 'gpt-4o').toString();
const isFileFromToday = dayKey === todayKey;
if (isFileFromToday && processedLines < 3) {
this.deps.log(`Backend sync: file ${sessionFile.split(/[/\\]/).pop()} line ${lineCount}: eventMs=${new Date(eventMs).toISOString()}, dayKey=${dayKey}, type=${event.type}`);
processedLines++;
}
// Track interaction for this day+model (count all events, not just user.message)
if (!dayModelInteractions.has(dayKey)) {
dayModelInteractions.set(dayKey, new Map());
}
const dayMap = dayModelInteractions.get(dayKey)!;
dayMap.set(model, (dayMap.get(model) || 0) + 1);
} catch {
// skip malformed line
}
}
} else {
// Handle JSON format (VS Code Copilot Chat)
try {
const sessionJson = JSON.parse(content);
if (!sessionJson || typeof sessionJson !== 'object') {
return false;
}
const sessionObj = sessionJson as Record<string, unknown>;
const requests = Array.isArray(sessionObj.requests) ? (sessionObj.requests as unknown[]) : [];
for (const request of requests) {
const req = request as ChatRequest;
const normalizedTs = this.utility.normalizeTimestampToMs(
typeof req.timestamp !== 'undefined' ? req.timestamp : (sessionObj.lastMessageDate as unknown)
);
const eventMs = Number.isFinite(normalizedTs) ? normalizedTs : fileMtimeMs;
if (!eventMs || eventMs < startMs) { continue; }
const dayKey = this.utility.toUtcDayKey(new Date(eventMs));
const model = this.deps.getModelFromRequest(req);
// Track interaction for this day+model
if (!dayModelInteractions.has(dayKey)) {
dayModelInteractions.set(dayKey, new Map());
}
const dayMap = dayModelInteractions.get(dayKey)!;
dayMap.set(model, (dayMap.get(model) || 0) + 1);
}
} catch (e) {
this.deps.warn(`Backend sync: failed to parse JSON for ${sessionFile}: ${e}`);
return false;
}
}
// Now distribute cached token counts proportionally across day+model combinations
// based on the actual interaction distribution we just calculated
for (const [dayKey, modelMap] of dayModelInteractions) {
for (const [model, interactions] of modelMap) {
const cachedUsage = cachedData.modelUsage[model];
if (!cachedUsage) { continue; }
// Validate usage object structure
if (!Number.isFinite(cachedUsage.inputTokens) || cachedUsage.inputTokens < 0) {
this.deps.warn(`Backend sync: invalid inputTokens for model ${model}`);
continue;
}
if (!Number.isFinite(cachedUsage.outputTokens) || cachedUsage.outputTokens < 0) {
this.deps.warn(`Backend sync: invalid outputTokens for model ${model}`);
continue;
}
const key: DailyRollupKey = { day: dayKey, model, workspaceId, machineId, userId };
// For simplicity, if a file spans multiple days, distribute tokens proportionally
// In practice, most session files are from a single day, so this is accurate
const totalInteractionsForModel = Array.from(dayModelInteractions.values())
.reduce((sum, m) => sum + (m.get(model) || 0), 0);
const tokenRatio = totalInteractionsForModel > 0 ? interactions / totalInteractionsForModel : 1;
upsertDailyRollup(rollups, key, {
inputTokens: Math.round(cachedUsage.inputTokens * tokenRatio),
outputTokens: Math.round(cachedUsage.outputTokens * tokenRatio),
interactions: interactions
});
}
}
// Log if this file had data for multiple days (for debugging)
if (dayModelInteractions.size > 1) {
const days = Array.from(dayModelInteractions.keys()).sort();
this.deps.log(`Backend sync: file ${sessionFile.split(/[/\\]/).pop()} spans ${days.length} days: ${days.join(', ')}`);
}
return true;
} catch (e) {
// Differentiate between cache miss (expected) and errors (unexpected)
const errorMessage = e instanceof Error ? e.message : String(e);
if (errorMessage.includes('ENOENT') || errorMessage.includes('not found')) {
// Expected cache miss - file doesn't exist or not cached yet
return false;
} else {
// Unexpected error - log as warning
this.deps.warn(`Backend sync: cache error for ${sessionFile}: ${errorMessage}`);
return false;
}
}
}
/**
* Resolve workspace name from session path if not already resolved.
*/
private async ensureWorkspaceNameResolved(
workspaceId: string,
sessionFile: string,
workspaceNamesById: Record<string, string>
): Promise<void> {
if (!workspaceNamesById[workspaceId]) {
const resolved = await this.utility.tryResolveWorkspaceNameFromSessionPath(sessionFile);
if (resolved) {
workspaceNamesById[workspaceId] = resolved;
}
}
}
/**
* Log cache performance statistics.
*/
private logCachePerformance(cacheHits: number, cacheMisses: number): void {
const totalFiles = cacheHits + cacheMisses;
if (totalFiles === 0) {return;}
const hitRate = ((cacheHits / totalFiles) * 100).toFixed(1);
this.deps.log(`Backend sync: Cache performance - Hits: ${cacheHits}, Misses: ${cacheMisses}, Hit Rate: ${hitRate}%`);
}
/**
* Resolve the effective user identity for sync.
*/
private async resolveEffectiveUserIdentityForSync(settings: BackendSettings, includeUserDimension: boolean): Promise<{ userId?: string; userKeyType?: BackendUserIdentityMode }> {
let accessTokenForClaims: string | undefined;
if (includeUserDimension && settings.userIdentityMode === 'pseudonymous' && settings.authMode === 'entraId') {
try {
const token = await new DefaultAzureCredential().getToken('https://storage.azure.com/.default');
accessTokenForClaims = token?.token;
} catch {
// Best-effort only: fall back to omitting user dimension.
}
}
const resolved = resolveUserIdentityForSync({
shareWithTeam: includeUserDimension,
userIdentityMode: settings.userIdentityMode,
configuredUserId: settings.userId,
datasetId: settings.datasetId,
accessTokenForClaims
});
return resolved;
}
/**
* Compute daily rollups from local session files.
* Uses cached session data when available to avoid re-parsing files.
*/
private async computeDailyRollupsFromLocalSessions(args: { lookbackDays: number; userId?: string }): Promise<{
rollups: Map<string, { key: DailyRollupKey; value: DailyRollupValue }>;
workspaceNamesById: Record<string, string>;
machineNamesById: Record<string, string>;
}> {
const lookbackDays = args.lookbackDays;
const userId = (args.userId ?? '').trim() || undefined;
const now = new Date();
// Include all events from the start of the first day in the range (UTC).
const start = new Date(now.getTime());
start.setUTCHours(0, 0, 0, 0);
start.setUTCDate(start.getUTCDate() - (lookbackDays - 1));
const startMs = start.getTime();
// Log the date range being processed
const todayKey = this.utility.toUtcDayKey(now);
const startKey = this.utility.toUtcDayKey(start);
this.deps.log(`Backend sync: processing sessions from ${startKey} to ${todayKey} (lookback ${lookbackDays} days)`);
const machineId = vscode.env.machineId;
const rollups = new Map<string, { key: DailyRollupKey; value: DailyRollupValue }>();
const workspaceNamesById: Record<string, string> = {};
const machineNamesById: Record<string, string> = {};
const machineName = this.utility.normalizeNameForStorage(this.utility.stripHostnameDomain(os.hostname()));
if (machineName) {
machineNamesById[machineId] = machineName;
}
const sessionFiles = await this.deps.getCopilotSessionFiles();
const useCachedData = !!this.deps.getSessionFileDataCached;
let cacheHits = 0;
let cacheMisses = 0;
let filesSkipped = 0;
let filesProcessed = 0;
this.deps.log(`Backend sync: analyzing ${sessionFiles.length} session files`);
for (const sessionFile of sessionFiles) {
let fileMtimeMs: number | undefined;
try {
const fileStat = await fs.promises.stat(sessionFile);
fileMtimeMs = fileStat.mtimeMs;
// Skip files older than lookback period
if (fileMtimeMs < startMs) {
filesSkipped++;
continue;
}
filesProcessed++;
} catch (e) {
this.deps.warn(`Backend sync: failed to stat session file ${sessionFile}: ${e}`);
continue;
}
const workspaceId = this.utility.extractWorkspaceIdFromSessionPath(sessionFile);
await this.ensureWorkspaceNameResolved(workspaceId, sessionFile, workspaceNamesById);
// Try to use cached data first (faster than full recomputation)
// Note: We still parse the file to get accurate day keys from timestamps,
// but use cached token counts for performance
if (useCachedData) {
const cacheSuccess = await this.processCachedSessionFile(
sessionFile,
fileMtimeMs,
workspaceId,
machineId,
userId,
rollups,
startMs,
now
);
if (cacheSuccess) {
cacheHits++;
continue;
} else {
cacheMisses++;
}
}
// Fallback: parse file directly (legacy path or cache unavailable)
let content: string;
try {
content = await fs.promises.readFile(sessionFile, 'utf8');
} catch (e) {
this.deps.warn(`Backend sync: failed to read session file ${sessionFile}: ${e}`);
continue;
}
// JSONL (Copilot CLI)
if (sessionFile.endsWith('.jsonl')) {
const lines = content.trim().split('\n');
for (const line of lines) {
if (!line.trim()) {
continue;
}
try {
const event = JSON.parse(line);
if (!event || typeof event !== 'object') {
continue;
}
const normalizedTs = this.utility.normalizeTimestampToMs(event.timestamp);
const eventMs = Number.isFinite(normalizedTs) ? normalizedTs : fileMtimeMs;
if (!eventMs || eventMs < startMs) {
continue;
}
const dayKey = this.utility.toUtcDayKey(new Date(eventMs));
const model = (event.model || 'gpt-4o').toString();
let inputTokens = 0;
let outputTokens = 0;
let interactions = 0;
if (event.type === 'user.message' && event.data?.content) {
inputTokens = this.deps.estimateTokensFromText(event.data.content, model);
interactions = 1;
} else if (event.type === 'assistant.message' && event.data?.content) {
outputTokens = this.deps.estimateTokensFromText(event.data.content, model);
} else if (event.type === 'tool.result' && event.data?.output) {
inputTokens = this.deps.estimateTokensFromText(event.data.output, model);
}
if (inputTokens === 0 && outputTokens === 0 && interactions === 0) {
continue;
}
const key: DailyRollupKey = { day: dayKey, model, workspaceId, machineId, userId };
upsertDailyRollup(rollups as any, key, { inputTokens, outputTokens, interactions });
} catch {
// skip malformed line
}
}
continue;
}
// JSON (VS Code Copilot Chat)
let sessionJson: unknown;
try {
sessionJson = JSON.parse(content);
if (!sessionJson || typeof sessionJson !== 'object') {
this.deps.warn(`Backend sync: session file has invalid JSON structure: ${sessionFile}`);
continue;
}
} catch (e) {
this.deps.warn(`Backend sync: failed to parse JSON session file ${sessionFile}: ${e}`);
continue;
}
const sessionObj = sessionJson as Record<string, unknown>; // Safe due to check above
const requests = Array.isArray(sessionObj.requests) ? (sessionObj.requests as unknown[]) : [];
for (const request of requests) {
try {
// Cast to ChatRequest since it comes from validated JSON object
const req = request as ChatRequest;
const normalizedTs = this.utility.normalizeTimestampToMs(
typeof req.timestamp !== 'undefined' ? req.timestamp : (sessionObj.lastMessageDate as unknown)
);
const eventMs = Number.isFinite(normalizedTs) ? normalizedTs : fileMtimeMs;
if (!eventMs || eventMs < startMs) {
continue;
}
const dayKey = this.utility.toUtcDayKey(new Date(eventMs));
const model = this.deps.getModelFromRequest(req);
let inputTokens = 0;
let outputTokens = 0;
if (req.message && req.message.parts) {
for (const part of req.message.parts) {
if (part?.text) {
inputTokens += this.deps.estimateTokensFromText(part.text, model);
}
}
}
if (req.response && Array.isArray(req.response)) {
for (const responseItem of req.response) {
if (typeof responseItem?.value === 'string') {
outputTokens += this.deps.estimateTokensFromText(responseItem.value, model);
}
}
}
if (inputTokens === 0 && outputTokens === 0) {
continue;
}
const key: DailyRollupKey = { day: dayKey, model, workspaceId, machineId, userId };
upsertDailyRollup(rollups as any, key, { inputTokens, outputTokens, interactions: 1 });
} catch (e) {
this.deps.warn(`Backend sync: failed to process request in ${sessionFile}: ${e}`);
}
}
}
// Log cache performance statistics
if (useCachedData) {
this.logCachePerformance(cacheHits, cacheMisses);
}
this.deps.log(`Backend sync: processed ${filesProcessed} files, skipped ${filesSkipped} files outside lookback period`);
return { rollups, workspaceNamesById, machineNamesById };
}
/**
* Sync local session data to the backend store.
* @param force - If true, forces sync even if recently synced
* @param settings - Backend settings for sync configuration
* @param isConfigured - Whether the backend is fully configured
* @throws Error if sync fails due to network or auth issues
*/
async syncToBackendStore(force: boolean, settings: BackendSettings, isConfigured: boolean): Promise<void> {
this.syncQueue = this.syncQueue.then(async () => {
if (this.backendSyncInProgress) {
return;
}
const sharingPolicy = computeBackendSharingPolicy({
enabled: settings.enabled,
profile: settings.sharingProfile,
shareWorkspaceMachineNames: settings.shareWorkspaceMachineNames
});
if (!sharingPolicy.allowCloudSync || !isConfigured) {
return;
}
// Avoid excessive syncing when UI refreshes frequently.
const lastSyncAt = this.deps.context?.globalState.get<number>('backend.lastSyncAt');
if (!force && lastSyncAt && Date.now() - lastSyncAt < BACKEND_SYNC_MIN_INTERVAL_MS) {
const secondsSinceLastSync = Math.round((Date.now() - lastSyncAt) / 1000);
this.deps.log(`Backend sync: skipping (last sync was ${secondsSinceLastSync}s ago, minimum interval is ${BACKEND_SYNC_MIN_INTERVAL_MS / 1000}s)`);
return;
}
this.backendSyncInProgress = true;
try {
this.deps.log('Backend sync: starting rollup sync');
const creds = await this.credentialService.getBackendDataPlaneCredentials(settings);
if (!creds) {
// Shared Key mode selected but key not available (or user canceled). Keep local mode functional.
return;
}
await this.dataPlaneService.ensureTableExists(settings, creds.tableCredential);
await this.dataPlaneService.validateAccess(settings, creds.tableCredential);
const resolvedIdentity = await this.resolveEffectiveUserIdentityForSync(settings, sharingPolicy.includeUserDimension);
const { rollups, workspaceNamesById, machineNamesById } = await this.computeDailyRollupsFromLocalSessions({ lookbackDays: settings.lookbackDays, userId: resolvedIdentity.userId });
// Log day keys being synced for better visibility
const dayKeys = new Set<string>();
for (const { key } of rollups.values()) {
dayKeys.add(key.day);
}
const sortedDays = Array.from(dayKeys).sort();
if (sortedDays.length > 0) {
this.deps.log(`Backend sync: processing data for ${sortedDays.length} days: ${sortedDays.join(', ')}`);
}
this.deps.log(`Backend sync: upserting ${rollups.size} rollup entities (lookback ${settings.lookbackDays} days)`);
const tableClient = this.dataPlaneService.createTableClient(settings, creds.tableCredential);
const entities = [];
for (const { key, value } of rollups.values()) {
const effectiveUserId = (key.userId ?? '').trim() || undefined;
const includeConsent = sharingPolicy.includeUserDimension && !!effectiveUserId;
const includeNames = sharingPolicy.includeNames;
const workspaceIdToStore = sharingPolicy.workspaceIdStrategy === 'hashed'
? hashWorkspaceIdForTeam({ datasetId: settings.datasetId, workspaceId: key.workspaceId })
: key.workspaceId;
const machineIdToStore = sharingPolicy.machineIdStrategy === 'hashed'
? hashMachineIdForTeam({ datasetId: settings.datasetId, machineId: key.machineId })
: key.machineId;
const workspaceName = includeNames ? workspaceNamesById[key.workspaceId] : undefined;
const machineName = includeNames ? machineNamesById[key.machineId] : undefined;
const entity = createDailyAggEntity({
datasetId: settings.datasetId,
day: key.day,
model: key.model,
workspaceId: workspaceIdToStore,
workspaceName,
machineId: machineIdToStore,
machineName,
userId: effectiveUserId,
userKeyType: resolvedIdentity.userKeyType,
shareWithTeam: includeConsent ? true : undefined,
consentAt: validateConsentTimestamp(settings.shareConsentAt, this.deps.log),
inputTokens: value.inputTokens,
outputTokens: value.outputTokens,
interactions: value.interactions
});
entities.push(entity);
}
const { successCount, errors } = await this.dataPlaneService.upsertEntitiesBatch(tableClient, entities);
if (errors.length > 0) {
this.deps.warn(`Backend sync: ${successCount}/${entities.length} entities synced successfully, ${errors.length} failed`);
} else {
this.deps.log(`Backend sync: ${successCount} entities synced successfully`);
}
this.consecutiveFailures = 0;
try {
await this.deps.context?.globalState.update('backend.lastSyncAt', Date.now());
} catch (e) {
this.deps.warn(`Backend sync: failed to update lastSyncAt: ${e}`);
}
this.deps.log('Backend sync: completed');
// Trigger UI refresh to update status bar and panels with latest sync data
try {
await this.deps.updateTokenStats?.();
} catch (e) {
this.deps.warn(`Backend sync: failed to update UI: ${e}`);
}
} catch (e: any) {
// Keep local mode functional.
const secretsToRedact = await this.credentialService.getBackendSecretsToRedactForError(settings);
this.deps.warn(`Backend sync: ${safeStringifyError(e, secretsToRedact)}`);
} finally {
this.backendSyncInProgress = false;
}
});
return this.syncQueue;
}
}