agy-family 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agy-family",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Zero-privacy-leak transparent usage dashboard, /family slash command, and fair-use budget guardrail for Google One / Antigravity family groups.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -47,7 +47,7 @@ import {
47
47
  writeHostConfig,
48
48
  } from '../src/host.mjs';
49
49
 
50
- const CLI_VERSION = '0.2.8';
50
+ const CLI_VERSION = '0.2.10';
51
51
  const THIS_CLI_PATH = fileURLToPath(import.meta.url);
52
52
  const PACKAGE_ROOT = resolve(dirname(THIS_CLI_PATH), '..', '..', '..');
53
53
 
@@ -716,7 +716,7 @@ export async function runHeadlessSync({ cfg, fullSync = false, home = homedir(),
716
716
  const { payload, scan, nextSyncedHashes, nextAccountLedger } = await buildSyncPayload({
717
717
  deviceId,
718
718
  clientKind: 'cli',
719
- clientVersion: '0.2.8',
719
+ clientVersion: '0.2.10',
720
720
  home,
721
721
  fileCache: cache,
722
722
  syncedHashes: prevHashes,
@@ -82,7 +82,7 @@ export function diffSyncBuckets(buckets, syncedHashes = null, { fullSync = false
82
82
  export async function buildSyncPayload({
83
83
  deviceId,
84
84
  clientKind = 'cli',
85
- clientVersion = '0.2.8',
85
+ clientVersion = '0.2.10',
86
86
  home,
87
87
  roots = null,
88
88
  sourceFilter = 'all',
@@ -155,7 +155,7 @@ export async function buildSyncPayload({
155
155
  deviceId: String(deviceId || deriveAnonymousDeviceId()).slice(0, 32),
156
156
  client: {
157
157
  kind: clientKind === 'extension' ? 'extension' : 'cli',
158
- version: String(clientVersion || '0.2.8').slice(0, 24),
158
+ version: String(clientVersion || '0.2.10').slice(0, 24),
159
159
  },
160
160
  account: {
161
161
  emailHash: effectiveReportedHash,
@@ -23,7 +23,7 @@ import {
23
23
  import { renderDashboardHtml } from './ui.mjs';
24
24
  import { dispatchWebhook, sendDedupedAlert, validateWebhookUrl } from './webhooks.mjs';
25
25
 
26
- export const SERVER_VERSION = '0.2.8';
26
+ export const SERVER_VERSION = '0.2.10';
27
27
  const MAX_BUCKETS_PER_SYNC = 1000;
28
28
  const MAX_DEVICES_PER_MEMBER = 8;
29
29
  const MAX_TOKENS_PER_BUCKET = 500_000_000;
@@ -336,13 +336,18 @@ export function createApp({
336
336
  const settings = await loadSettings(db, envDefaults);
337
337
  const viewErr = await requireViewKeyIfEnabled(request, url, settings, now);
338
338
  if (viewErr) return viewErr;
339
- const period = ['today', '7d', 'cycle', '30d'].includes(url.searchParams.get('period'))
340
- ? url.searchParams.get('period')
341
- : 'today';
339
+ const from = (url.searchParams.get('from') || '').trim();
340
+ const to = (url.searchParams.get('to') || '').trim();
341
+ const rawPeriod = url.searchParams.get('period');
342
+ const period = ['today', 'yesterday', '7d', 'cycle', '30d', 'custom'].includes(rawPeriod)
343
+ ? rawPeriod
344
+ : from
345
+ ? 'custom'
346
+ : 'today';
342
347
  const source = ['desktop', 'cli'].includes(url.searchParams.get('source'))
343
348
  ? url.searchParams.get('source')
344
349
  : 'all';
345
- const board = await buildBoardPayload(db, { period, source, envDefaults, now });
350
+ const board = await buildBoardPayload(db, { period, from, to, source, envDefaults, now });
346
351
  return jsonRes(board);
347
352
  }
348
353
 
@@ -351,19 +356,25 @@ export function createApp({
351
356
  const settings = await loadSettings(db, envDefaults);
352
357
  const viewErr = await requireViewKeyIfEnabled(request, url, settings, now);
353
358
  if (viewErr) return viewErr;
354
- const period = ['today', '7d', 'cycle', '30d'].includes(url.searchParams.get('period'))
355
- ? url.searchParams.get('period')
356
- : 'cycle';
359
+ const from = (url.searchParams.get('from') || '').trim();
360
+ const to = (url.searchParams.get('to') || '').trim();
361
+ const rawPeriod = url.searchParams.get('period');
362
+ const period = ['today', 'yesterday', '7d', 'cycle', '30d', 'custom'].includes(rawPeriod)
363
+ ? rawPeriod
364
+ : from
365
+ ? 'custom'
366
+ : 'cycle';
357
367
  const source = ['desktop', 'cli'].includes(url.searchParams.get('source'))
358
368
  ? url.searchParams.get('source')
359
369
  : 'all';
360
- const board = await buildBoardPayload(db, { period, source, envDefaults, now });
370
+ const board = await buildBoardPayload(db, { period, from, to, source, envDefaults, now });
361
371
  const csv = buildBoardCsv(board);
372
+ const fileTag = period === 'custom' && board.window ? `${board.window.fromDate}_${board.window.toDate}` : period;
362
373
  return new Response(csv, {
363
374
  status: 200,
364
375
  headers: {
365
376
  'Content-Type': 'text/csv; charset=utf-8',
366
- 'Content-Disposition': `attachment; filename="agy-family-${period}.csv"`,
377
+ 'Content-Disposition': `attachment; filename="agy-family-${fileTag}.csv"`,
367
378
  'Cache-Control': 'no-store',
368
379
  'Referrer-Policy': 'no-referrer',
369
380
  'X-Content-Type-Options': 'nosniff',
@@ -378,10 +389,15 @@ export function createApp({
378
389
  const viewErr = await requireViewKeyIfEnabled(request, url, settings, now);
379
390
  if (viewErr) return viewErr;
380
391
  const memberId = decodeURIComponent(boardMemberMatch[1]);
381
- const period = ['today', '7d', 'cycle', '30d'].includes(url.searchParams.get('period'))
382
- ? url.searchParams.get('period')
383
- : 'today';
384
- const detail = await buildMemberDetailPayload(db, memberId, { period, envDefaults, now });
392
+ const from = (url.searchParams.get('from') || '').trim();
393
+ const to = (url.searchParams.get('to') || '').trim();
394
+ const rawPeriod = url.searchParams.get('period');
395
+ const period = ['today', 'yesterday', '7d', 'cycle', '30d', 'custom'].includes(rawPeriod)
396
+ ? rawPeriod
397
+ : from
398
+ ? 'custom'
399
+ : 'today';
400
+ const detail = await buildMemberDetailPayload(db, memberId, { period, from, to, envDefaults, now });
385
401
  if (!detail) return jsonRes({ error: 'Member not found.' }, 404);
386
402
  return jsonRes(detail);
387
403
  }
@@ -261,7 +261,27 @@ export function computeCycleInfo({ now = Date.now(), timezoneOffsetMin = 480, cy
261
261
  };
262
262
  }
263
263
 
264
- export function getPeriodWindow(period, { now = Date.now(), timezoneOffsetMin = 480, cycleResetDay = 1 } = {}) {
264
+ function parseIsoDateParts(raw) {
265
+ const m = String(raw || '').trim().match(/^(\d{4})-(\d{2})-(\d{2})$/);
266
+ if (!m) return null;
267
+ const y = Number(m[1]);
268
+ const mo = Number(m[2]);
269
+ const d = Number(m[3]);
270
+ if (y < 2020 || y > 2100 || mo < 1 || mo > 12 || d < 1 || d > 31) return null;
271
+ const check = new Date(Date.UTC(y, mo - 1, d));
272
+ if (check.getUTCFullYear() !== y || check.getUTCMonth() !== mo - 1 || check.getUTCDate() !== d) return null;
273
+ return { y, mo, d, key: `${y}-${String(mo).padStart(2, '0')}-${String(d).padStart(2, '0')}` };
274
+ }
275
+
276
+ function formatLocalDateKey(utcMs, offsetMs) {
277
+ const dt = new Date(utcMs + offsetMs);
278
+ return `${dt.getUTCFullYear()}-${String(dt.getUTCMonth() + 1).padStart(2, '0')}-${String(dt.getUTCDate()).padStart(2, '0')}`;
279
+ }
280
+
281
+ export function getPeriodWindow(
282
+ period,
283
+ { now = Date.now(), timezoneOffsetMin = 480, cycleResetDay = 1, from = '', to = '' } = {},
284
+ ) {
265
285
  const offsetMs = (Number(timezoneOffsetMin) || 0) * 60 * 1000;
266
286
  const localNow = new Date(now + offsetMs);
267
287
  const y = localNow.getUTCFullYear();
@@ -271,15 +291,67 @@ export function getPeriodWindow(period, { now = Date.now(), timezoneOffsetMin =
271
291
  const localDayStartUtc = Date.UTC(y, m, d, 0, 0, 0) - offsetMs;
272
292
  const dateKey = `${y}-${String(m + 1).padStart(2, '0')}-${String(d).padStart(2, '0')}`;
273
293
 
294
+ if (period === 'custom' || (from && parseIsoDateParts(from))) {
295
+ const pFrom = parseIsoDateParts(from);
296
+ const pTo = parseIsoDateParts(to) || pFrom;
297
+ if (pFrom && pTo) {
298
+ let startDayUtc = Date.UTC(pFrom.y, pFrom.mo - 1, pFrom.d, 0, 0, 0) - offsetMs;
299
+ let endDayUtc = Date.UTC(pTo.y, pTo.mo - 1, pTo.d, 0, 0, 0) - offsetMs;
300
+ if (startDayUtc > endDayUtc) {
301
+ const tmp = startDayUtc;
302
+ startDayUtc = endDayUtc;
303
+ endDayUtc = tmp;
304
+ }
305
+ // Cap custom window at 366 days
306
+ if (endDayUtc - startDayUtc > 365 * DAY_MS) {
307
+ startDayUtc = endDayUtc - 365 * DAY_MS;
308
+ }
309
+ const endMs = endDayUtc + DAY_MS - 1;
310
+ const fromDate = formatLocalDateKey(startDayUtc, offsetMs);
311
+ const toDate = formatLocalDateKey(endDayUtc, offsetMs);
312
+ const days = Math.max(1, Math.round((endDayUtc - startDayUtc) / DAY_MS) + 1);
313
+ return {
314
+ period: 'custom',
315
+ startMs: startDayUtc,
316
+ endMs,
317
+ fromDate,
318
+ toDate,
319
+ days,
320
+ dateKey,
321
+ windowKey: `custom:${fromDate}..${toDate}`,
322
+ };
323
+ }
324
+ }
325
+
326
+ if (period === 'yesterday') {
327
+ const startMs = localDayStartUtc - DAY_MS;
328
+ const endMs = localDayStartUtc - 1;
329
+ const yKey = formatLocalDateKey(startMs, offsetMs);
330
+ return {
331
+ period: 'yesterday',
332
+ startMs,
333
+ endMs,
334
+ fromDate: yKey,
335
+ toDate: yKey,
336
+ days: 1,
337
+ dateKey,
338
+ windowKey: `yesterday:${yKey}`,
339
+ };
340
+ }
341
+
274
342
  if (period === '7d') {
275
343
  // ISO week key for alert deduplication (Monday-start local week)
276
344
  const dow = (localNow.getUTCDay() + 6) % 7;
277
345
  const weekStartDate = new Date(Date.UTC(y, m, d - dow, 0, 0, 0));
278
346
  const weekKey = `${weekStartDate.getUTCFullYear()}-${String(weekStartDate.getUTCMonth() + 1).padStart(2, '0')}-${String(weekStartDate.getUTCDate()).padStart(2, '0')}`;
347
+ const startMs = localDayStartUtc - 6 * DAY_MS;
279
348
  return {
280
349
  period: '7d',
281
- startMs: localDayStartUtc - 6 * DAY_MS,
350
+ startMs,
282
351
  endMs: now,
352
+ fromDate: formatLocalDateKey(startMs, offsetMs),
353
+ toDate: dateKey,
354
+ days: 7,
283
355
  dateKey,
284
356
  weekKey,
285
357
  windowKey: `7d:${weekKey}`,
@@ -287,10 +359,14 @@ export function getPeriodWindow(period, { now = Date.now(), timezoneOffsetMin =
287
359
  }
288
360
 
289
361
  if (period === '30d') {
362
+ const startMs = localDayStartUtc - 29 * DAY_MS;
290
363
  return {
291
364
  period: '30d',
292
- startMs: localDayStartUtc - 29 * DAY_MS,
365
+ startMs,
293
366
  endMs: now,
367
+ fromDate: formatLocalDateKey(startMs, offsetMs),
368
+ toDate: dateKey,
369
+ days: 30,
294
370
  dateKey,
295
371
  windowKey: `30d:${dateKey}`,
296
372
  };
@@ -309,10 +385,14 @@ export function getPeriodWindow(period, { now = Date.now(), timezoneOffsetMin =
309
385
  }
310
386
  const startMs = Date.UTC(cy, cm, resetDay, 0, 0, 0) - offsetMs;
311
387
  const cycleKey = `${cy}-${String(cm + 1).padStart(2, '0')}-${String(resetDay).padStart(2, '0')}`;
388
+ const days = Math.max(1, Math.floor((localDayStartUtc - startMs) / DAY_MS) + 1);
312
389
  return {
313
390
  period: 'cycle',
314
391
  startMs,
315
392
  endMs: now,
393
+ fromDate: cycleKey,
394
+ toDate: dateKey,
395
+ days,
316
396
  dateKey,
317
397
  windowKey: `cycle:${cycleKey}`,
318
398
  };
@@ -322,6 +402,9 @@ export function getPeriodWindow(period, { now = Date.now(), timezoneOffsetMin =
322
402
  period: 'today',
323
403
  startMs: localDayStartUtc,
324
404
  endMs: now,
405
+ fromDate: dateKey,
406
+ toDate: dateKey,
407
+ days: 1,
325
408
  dateKey,
326
409
  windowKey: `today:${dateKey}`,
327
410
  };
@@ -575,7 +658,10 @@ export async function computeCreditsReconciliation(db, members, settings, cycleI
575
658
  };
576
659
  }
577
660
 
578
- export async function buildBoardPayload(db, { period = 'today', source = 'all', envDefaults = {}, now = Date.now() } = {}) {
661
+ export async function buildBoardPayload(
662
+ db,
663
+ { period = 'today', from = '', to = '', source = 'all', envDefaults = {}, now = Date.now() } = {},
664
+ ) {
579
665
  const settings = await loadSettings(db, envDefaults);
580
666
  const sourceFilter = ['desktop', 'cli'].includes(source) ? source : 'all';
581
667
  const offsetMs = (Number(settings.timezoneOffsetMin) || 0) * 60 * 1000;
@@ -583,6 +669,8 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
583
669
  now,
584
670
  timezoneOffsetMin: settings.timezoneOffsetMin,
585
671
  cycleResetDay: settings.cycleResetDay,
672
+ from,
673
+ to,
586
674
  });
587
675
  const cycleInfo = computeCycleInfo({
588
676
  now,
@@ -600,14 +688,20 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
600
688
  cycleResetDay: settings.cycleResetDay,
601
689
  }).startMs;
602
690
 
603
- // Daily trend spans 7d for today/7d, 30d for 30d, and cycle start -> today (up to 31d) for cycle.
604
- const trendStartMs =
605
- win.period === '30d'
606
- ? win.startMs
607
- : win.period === 'cycle'
608
- ? Math.max(cycleInfo.cycleStartMs, todayStartMs - 30 * DAY_MS)
609
- : sevenDaysStart;
610
- const trendDaysCount = Math.max(1, Math.min(31, Math.floor((todayStartMs - trendStartMs) / DAY_MS) + 1));
691
+ // Daily trend spans selected custom range (up to 45d), 30d for 30d, cycle start->today for cycle, or 7d.
692
+ let trendStartMs = sevenDaysStart;
693
+ let trendEndDayStartMs = todayStartMs;
694
+ if (win.period === '30d') {
695
+ trendStartMs = win.startMs;
696
+ } else if (win.period === 'cycle') {
697
+ trendStartMs = Math.max(cycleInfo.cycleStartMs, todayStartMs - 30 * DAY_MS);
698
+ } else if (win.period === 'custom') {
699
+ const customEndDay = win.endMs + 1 - DAY_MS;
700
+ trendEndDayStartMs = customEndDay;
701
+ trendStartMs = Math.max(win.startMs, customEndDay - 44 * DAY_MS);
702
+ }
703
+ const trendDaysCount = Math.max(1, Math.min(45, Math.floor((trendEndDayStartMs - trendStartMs) / DAY_MS) + 1));
704
+ const trendEndMs = trendStartMs + trendDaysCount * DAY_MS - 1;
611
705
 
612
706
  const membersRaw = await db.all(
613
707
  'SELECT id, name, role, color, quota_pct, expected_email_hash, last_account_status, daily_cap_usd, weekly_cap_usd, cycle_cap_usd, active, created_at, last_seen_at, last_client_kind, last_client_version FROM members WHERE active = 1 ORDER BY created_at ASC',
@@ -641,12 +735,19 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
641
735
  });
642
736
  }
643
737
 
644
- // Build 24h timeline slots
738
+ // Build 24h timeline slots (exact 00:00..23:00 for single-day yesterday/custom, rolling 24h otherwise)
645
739
  const curHour = Math.floor(now / HOUR_MS) * HOUR_MS;
646
740
  const hourlyMap = new Map();
647
- for (let i = 23; i >= 0; i--) {
648
- const h = curHour - i * HOUR_MS;
649
- hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0 });
741
+ if (win.period === 'yesterday' || (win.period === 'custom' && win.days === 1)) {
742
+ for (let i = 0; i < 24; i++) {
743
+ const h = win.startMs + i * HOUR_MS;
744
+ hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0 });
745
+ }
746
+ } else {
747
+ for (let i = 23; i >= 0; i--) {
748
+ const h = curHour - i * HOUR_MS;
749
+ hourlyMap.set(h, { hourMs: h, usd: 0, tokens: 0, steps: 0 });
750
+ }
650
751
  }
651
752
 
652
753
  // Build daily trend slots
@@ -696,7 +797,7 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
696
797
  slot.steps += steps;
697
798
  }
698
799
 
699
- if (b.bucket_hour_ms >= trendStartMs) {
800
+ if (b.bucket_hour_ms >= trendStartMs && b.bucket_hour_ms <= trendEndMs) {
700
801
  const dayIdx = Math.floor((b.bucket_hour_ms - trendStartMs) / DAY_MS);
701
802
  const dayKey = trendStartMs + dayIdx * DAY_MS;
702
803
  const dSlot = dailyTrendMap.get(dayKey);
@@ -713,7 +814,7 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
713
814
  cycleUsedUsd += usd;
714
815
  }
715
816
 
716
- if (b.bucket_hour_ms < win.startMs) continue;
817
+ if (b.bucket_hour_ms < win.startMs || b.bucket_hour_ms > win.endMs) continue;
717
818
 
718
819
  const entry = byMember.get(b.member_id);
719
820
  if (!entry) continue;
@@ -911,6 +1012,14 @@ export async function buildBoardPayload(db, { period = 'today', source = 'all',
911
1012
  period: win.period,
912
1013
  source: sourceFilter,
913
1014
  windowStartMs: win.startMs,
1015
+ windowEndMs: win.endMs,
1016
+ window: {
1017
+ startMs: win.startMs,
1018
+ endMs: win.endMs,
1019
+ fromDate: win.fromDate,
1020
+ toDate: win.toDate,
1021
+ days: win.days,
1022
+ },
914
1023
  generatedAt: now,
915
1024
  rules: {
916
1025
  maxMembers: MAX_FAMILY_MEMBERS,
@@ -1037,7 +1146,11 @@ export async function checkOfflineMembersAndAlert(
1037
1146
  };
1038
1147
  }
1039
1148
 
1040
- export async function buildMemberDetailPayload(db, memberId, { period = 'today', envDefaults = {}, now = Date.now() } = {}) {
1149
+ export async function buildMemberDetailPayload(
1150
+ db,
1151
+ memberId,
1152
+ { period = 'today', from = '', to = '', envDefaults = {}, now = Date.now() } = {},
1153
+ ) {
1041
1154
  const member = await db.get(
1042
1155
  'SELECT id, name, role, color, quota_pct, expected_email_hash, last_account_status, daily_cap_usd, weekly_cap_usd, cycle_cap_usd, active, created_at, last_seen_at, last_client_kind, last_client_version FROM members WHERE id = ? AND active = 1',
1043
1156
  [memberId],
@@ -1057,6 +1170,8 @@ export async function buildMemberDetailPayload(db, memberId, { period = 'today',
1057
1170
  now,
1058
1171
  timezoneOffsetMin: settings.timezoneOffsetMin,
1059
1172
  cycleResetDay: settings.cycleResetDay,
1173
+ from,
1174
+ to,
1060
1175
  });
1061
1176
  const todayWin = getPeriodWindow('today', {
1062
1177
  now,
@@ -1092,7 +1207,7 @@ export async function buildMemberDetailPayload(db, memberId, { period = 'today',
1092
1207
  if (gr.bucket_hour_ms >= todayWin.startMs) groupTodayUsd += u;
1093
1208
  if (gr.bucket_hour_ms >= weeklyWin.startMs) groupWeeklyUsd += u;
1094
1209
  if (gr.bucket_hour_ms >= cycleWin.startMs) groupCycleUsd += u;
1095
- if (gr.bucket_hour_ms >= win.startMs) groupPeriodUsd += u;
1210
+ if (gr.bucket_hour_ms >= win.startMs && gr.bucket_hour_ms <= win.endMs) groupPeriodUsd += u;
1096
1211
  }
1097
1212
  groupTodayUsd = roundUsd(groupTodayUsd);
1098
1213
  groupWeeklyUsd = roundUsd(groupWeeklyUsd);
@@ -1167,7 +1282,7 @@ export async function buildMemberDetailPayload(db, memberId, { period = 'today',
1167
1282
  }
1168
1283
  }
1169
1284
 
1170
- if (b.bucket_hour_ms < win.startMs) continue;
1285
+ if (b.bucket_hour_ms < win.startMs || b.bucket_hour_ms > win.endMs) continue;
1171
1286
  const devId = String(b.device_id || 'unknown');
1172
1287
  const dev = deviceMap.get(devId) || {
1173
1288
  deviceId: devId,