tokenusage-cli 0.3.0 → 0.4.0

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/README.md +52 -11
  3. package/bin/tokenusage.js +14 -2
  4. package/package.json +1 -2
  5. package/src/cli.js +325 -78
  6. package/src/commands/device-login.js +3 -3
  7. package/src/commands/init.js +343 -153
  8. package/src/commands/pause.js +11 -23
  9. package/src/commands/reconcile.js +485 -0
  10. package/src/commands/sessions.js +57 -14
  11. package/src/commands/sync.js +69 -15
  12. package/src/commands/uninstall.js +6 -9
  13. package/src/commands/upload.js +147 -0
  14. package/src/lib/cli-errors.js +33 -0
  15. package/src/lib/cli-ui.js +93 -0
  16. package/src/lib/queue-row-normalize.js +55 -0
  17. package/src/lib/scheduled-task.js +597 -75
  18. package/src/lib/antigravity-paths.js +0 -48
  19. package/src/lib/async-queue-reader.js +0 -138
  20. package/src/lib/atomic-json.js +0 -27
  21. package/src/lib/bearer-token.js +0 -11
  22. package/src/lib/cloud-account.js +0 -541
  23. package/src/lib/codex-context-breakdown.js +0 -806
  24. package/src/lib/context-health.js +0 -124
  25. package/src/lib/grok-context-breakdown.js +0 -890
  26. package/src/lib/heatmap-compact.js +0 -114
  27. package/src/lib/local-api.js +0 -3574
  28. package/src/lib/outcomes-engine.js +0 -253
  29. package/src/lib/pet-packages.js +0 -722
  30. package/src/lib/skill-usage.js +0 -267
  31. package/src/lib/skills-manager.js +0 -1591
  32. package/src/lib/source-metadata.js +0 -48
  33. package/src/lib/subscription-manager.js +0 -293
  34. package/src/lib/wrapped-aggregator.js +0 -236
  35. package/test/audit-fixes.test.js +0 -249
  36. package/test/branding.test.js +0 -24
  37. package/test/cli-options.test.js +0 -43
  38. package/test/cli-smoke.test.js +0 -53
  39. package/test/cli-ui.test.js +0 -47
  40. package/test/collection-controls.test.js +0 -255
  41. package/test/ingest-sanitize.test.js +0 -162
  42. package/test/packaging.test.js +0 -107
  43. package/test/quiet-warnings.test.js +0 -53
  44. package/test/runtime-copy.test.js +0 -111
  45. package/test/session-upload.test.js +0 -427
  46. package/test/trae-db.test.js +0 -67
  47. package/test/zstd-native.test.js +0 -36
@@ -1,3574 +0,0 @@
1
- const fs = require("node:fs");
2
- const os = require("node:os");
3
- const path = require("node:path");
4
- const { spawn } = require("node:child_process");
5
- const crypto = require("node:crypto");
6
- const { setImmediate: yieldToRequests } = require("node:timers/promises");
7
- const { createAsyncQueueReader } = require("./async-queue-reader");
8
- const { DEFAULT_BASE_URL, resolveRuntimeConfig, assertInsforgeRuntime, normalizeInstanceBaseUrl, clearDeviceIdentity, resetInstanceState } = require("./runtime-config");
9
- const {
10
- filterRowsByUsageScope,
11
- getSourceScope,
12
- listExcludedSources,
13
- normalizeUsageScope,
14
- } = require("./source-metadata");
15
- const { accountSlugFor, fetchAccountUsage, mintAccessToken, invalidateCloudAccountPayloadCache, getCloudAccountSessionGeneration } = require("./cloud-account");
16
- const { resetCloudDeviceUsage, readCloudUsageReset } = require("./cloud-usage-reset");
17
- const { getOrCreateMachineId, computeStableMachineId } = require("./machine-id");
18
- const { functionUrlFor, fetchFunctionResponse } = require("./function-url");
19
- const { createCloudDeviceTokenStore } = require("./cloud-device-token");
20
- const { parseBearerToken } = require("./bearer-token");
21
- const { writeJsonAtomicSync } = require("./atomic-json");
22
-
23
- const SYNC_TIMEOUT_MS = 120_000;
24
- // A failed account-view request should not stall every dashboard refresh
25
- // while an expired session or unreachable backend keeps timing out. The first
26
- // request still falls back normally; subsequent fan-out requests reuse that
27
- // decision for a short cooldown and return local data immediately.
28
- const ACCOUNT_VIEW_FAILURE_BACKOFF_MS = 2 * 60_000;
29
- const TRACKER_BIN = path.resolve(__dirname, "../../bin/tokenusage.js");
30
- const MAX_DEVICE_NAME_LENGTH = 128;
31
-
32
- function getSystemDeviceName() {
33
- try {
34
- const hostname = os.hostname()
35
- .replace(/[\u0000-\u001f\u007f]/g, "")
36
- .trim()
37
- .slice(0, MAX_DEVICE_NAME_LENGTH);
38
- return hostname || null;
39
- } catch {
40
- return null;
41
- }
42
- }
43
-
44
- // Avatar proxy (see /api/avatar-proxy below). In-memory LRU; survives the
45
- // CLI server lifetime, which is good enough — the dashboard reloads cheaply.
46
- const AVATAR_PROXY_TTL_MS = 60 * 60 * 1000; // 1h
47
- const AVATAR_PROXY_MAX_BYTES = 512 * 1024; // 512 KiB per image
48
- const AVATAR_PROXY_MAX_ENTRIES = 64;
49
- const avatarProxyCache = new Map();
50
-
51
- // ---------------------------------------------------------------------------
52
- // Per-model pricing — delegated to src/lib/pricing/
53
- // - CURATED overrides (kiro-*, hy3-*, composer-*, kimi-for-coding, etc.)
54
- // - LiteLLM live data (mainstream claude / gpt-5 / gemini), 24h disk-cached
55
- // - Bundled seed snapshot for first-install / offline fallback
56
- // ---------------------------------------------------------------------------
57
-
58
- const {
59
- MODEL_PRICING,
60
- getModelPricing,
61
- computeRowCost,
62
- ensurePricingLoaded,
63
- getPricingRevision,
64
- getModelPricingInfo,
65
- } = require("./pricing");
66
-
67
- const {
68
- computeClaudeCategoryBreakdown,
69
- unsupportedSourcePayload: unsupportedCategoryPayload,
70
- } = require("./claude-categorizer");
71
-
72
- const { computeCodexContextBreakdown } = require("./codex-context-breakdown");
73
- const { computeGrokContextBreakdown } = require("./grok-context-breakdown");
74
-
75
- const {
76
- deriveProjectKeyFromRef,
77
- CLAUDE_MEM_OBSERVER_PROJECT_REF,
78
- } = require("./rollout");
79
-
80
- // ---------------------------------------------------------------------------
81
- // Queue data helpers
82
- // ---------------------------------------------------------------------------
83
-
84
- function resolveQueuePath() {
85
- const home = os.homedir();
86
- return path.join(home, ".tokenusage", "tracker", "queue.jsonl");
87
- }
88
-
89
- // Pseudo-project written by the pre-fix claude-mem observer attribution;
90
- // mirrored Claude Code sessions, not a real repository. Excluded at read
91
- // time so historical rows never surface on the Project Usage panel. Derived
92
- // from rollout.js's ref so a rename there cannot silently desync the filter.
93
- const CLAUDE_MEM_OBSERVER_PROJECT_KEY = deriveProjectKeyFromRef(
94
- CLAUDE_MEM_OBSERVER_PROJECT_REF,
95
- );
96
- const PROJECT_USAGE_MAX_ENTRIES = 10;
97
- const MAX_TIME_ZONE_CACHE_ENTRIES = 16;
98
- const AGGREGATION_BATCH_ROWS = 256;
99
-
100
- // The native dashboard fans one refresh out across several local endpoints.
101
- // Keep one immutable, deduped view of the current queue so those endpoints do
102
- // not all read and JSON.parse the same append-only file. A file identity +
103
- // nanosecond timestamp signature invalidates the cache immediately on append,
104
- // in-place rewrite, or atomic replacement.
105
- const dailyAggregationCache = new WeakMap();
106
- const zonedPartsFormatters = new Map();
107
-
108
- function boundedCacheSet(cache, key, value, maxEntries) {
109
- if (cache.has(key)) cache.delete(key);
110
- cache.set(key, value);
111
- while (cache.size > maxEntries) {
112
- cache.delete(cache.keys().next().value);
113
- }
114
- }
115
-
116
- function timeZoneContextKey({ timeZone, offsetMinutes } = {}) {
117
- return `${timeZone || ""}|${Number.isFinite(offsetMinutes) ? offsetMinutes : ""}|p${getPricingRevision()}`;
118
- }
119
-
120
- // Shared by project-usage-summary and project-usage-detail: reads the
121
- // deduped project bucket log, drops the claude-mem pseudo project, and
122
- // builds the from/to day-range predicate. Callers compute each row's day
123
- // key ONCE (rowDayKey constructs an Intl.DateTimeFormat per call — the
124
- // dominant per-row cost) and reuse it for both the range check and any
125
- // day bucketing.
126
- async function readProjectUsageContext(qp, url) {
127
- const from = url.searchParams.get("from") || "";
128
- const to = url.searchParams.get("to") || "";
129
- const timeZoneContext = getTimeZoneContext(url);
130
- const hasRange = Boolean(from || to);
131
- const dayInRange = (day) => {
132
- if (!hasRange) return true;
133
- if (!day) return false;
134
- return (!from || day >= from) && (!to || day <= to);
135
- };
136
- const projectQueuePath = path.join(path.dirname(qp), "project.queue.jsonl");
137
- const projectRows = (await readProjectQueueData(projectQueuePath)).filter(
138
- (row) => row.project_key !== CLAUDE_MEM_OBSERVER_PROJECT_KEY,
139
- );
140
- return { from, to, timeZoneContext, hasRange, dayInRange, projectRows };
141
- }
142
-
143
- // Same signature-based caching as readQueueData: the project-usage endpoints
144
- // all fan out from one dashboard refresh, so without this every request
145
- // re-read and re-parsed the whole append-only project queue.
146
- const readProjectQueueData = createAsyncQueueReader({
147
- label: "readProjectQueueData",
148
- parseRow(row) {
149
- return {
150
- key: `${row.project_key || ""}|${row.source || ""}|${row.hour_start || ""}`,
151
- row: normalizeQueueRow(row),
152
- };
153
- },
154
- });
155
-
156
- function isLegacyInclusiveCodexRow(row) {
157
- if (!row || (row.source !== "codex" && row.source !== "every-code")) return false;
158
- const inputTokens = Number(row.input_tokens || 0);
159
- const cachedInputTokens = Number(row.cached_input_tokens || 0);
160
- const outputTokens = Number(row.output_tokens || 0);
161
- const totalTokens = Number(row.total_tokens || 0);
162
- if (!Number.isFinite(inputTokens) || !Number.isFinite(cachedInputTokens)) return false;
163
- if (cachedInputTokens <= 0 || inputTokens < cachedInputTokens) return false;
164
- // Legacy Codex queue rows stored input inclusive of cache reads, while
165
- // total_tokens remained input + output. Canonical rows keep input as pure
166
- // non-cached input, so cache-heavy legacy rows can be identified by this
167
- // exact invariant.
168
- return totalTokens === inputTokens + outputTokens;
169
- }
170
-
171
- function normalizeQueueRow(row) {
172
- let normalized = row;
173
- if (isLegacyInclusiveCodexRow(normalized)) {
174
- normalized = {
175
- ...normalized,
176
- input_tokens:
177
- Number(normalized.input_tokens || 0) - Number(normalized.cached_input_tokens || 0),
178
- };
179
- }
180
- // Legacy Cursor rows from versions ≤ 0.26.5 wrote billable_total_tokens = 0
181
- // for "Included in Pro" / "Enterprise" / "no charge" records (kind-based
182
- // gating in cursor-config.js#normalizeCursorUsage). The dashboard headline
183
- // sums billable_total_tokens across sources, so those rows silently
184
- // disappeared from the displayed total once any other source contributed
185
- // non-zero billable usage (GitHub issue #106). Treat billing and usage as
186
- // orthogonal: bump billable up to total_tokens at read time so historical
187
- // queue.jsonl entries render correctly without requiring a file rewrite.
188
- // Antigravity had the same symptom from e69e2746a (billable deleted during
189
- // the O(N) refactoring, leaving 167 local rows with billable=0 until the
190
- // parser fix). Apply the same read-time healing so upgraded installs show
191
- // correct heatmap / rolling numbers without a queue rewrite.
192
- const sourceName = String(normalized.source || "").toLowerCase();
193
- if (sourceName === "cursor" || sourceName === "antigravity") {
194
- const totalTokens = Number(normalized.total_tokens || 0);
195
- const billable = Number(normalized.billable_total_tokens || 0);
196
- if (totalTokens > 0 && billable < totalTokens) {
197
- normalized = { ...normalized, billable_total_tokens: totalTokens };
198
- }
199
- }
200
- return normalized;
201
- }
202
-
203
- const readQueueData = createAsyncQueueReader({
204
- label: "readQueueData",
205
- parseRow(row) {
206
- if (row?.kind === "account_session_state" || row?.kind === "account_sync_watermark") return null;
207
- return {
208
- key: `${row.source || ""}|${row.model || ""}|${row.hour_start || ""}`,
209
- row: normalizeQueueRow(row),
210
- };
211
- },
212
- });
213
-
214
- function rowDayKey(row, timeZoneContext) {
215
- const hs = row.hour_start;
216
- if (!hs) return "";
217
- if (
218
- timeZoneContext &&
219
- (timeZoneContext.timeZone || Number.isFinite(timeZoneContext.offsetMinutes))
220
- ) {
221
- const parts = getZonedParts(new Date(hs), timeZoneContext);
222
- const key = formatPartsDayKey(parts);
223
- if (key) return key;
224
- }
225
- return hs.slice(0, 10);
226
- }
227
-
228
- function aggregateByDay(rows, timeZoneContext = null) {
229
- const normalizedRows = Array.isArray(rows) ? rows : [];
230
- const cacheKey = timeZoneContextKey(timeZoneContext || {});
231
- let cachedByTimeZone = dailyAggregationCache.get(normalizedRows);
232
- if (cachedByTimeZone?.has(cacheKey)) {
233
- const cached = cachedByTimeZone.get(cacheKey);
234
- // Touch the entry so the small per-queue map behaves as an LRU.
235
- cachedByTimeZone.delete(cacheKey);
236
- cachedByTimeZone.set(cacheKey, cached);
237
- return cached;
238
- }
239
-
240
- if (!cachedByTimeZone) {
241
- cachedByTimeZone = new Map();
242
- dailyAggregationCache.set(normalizedRows, cachedByTimeZone);
243
- }
244
- const pending = buildDailyAggregates(normalizedRows, timeZoneContext).then((daily) => {
245
- // Pricing can refresh while a large aggregation yields. Rebuild consistently
246
- // with the current catalog instead of publishing a mix of two revisions.
247
- if (cacheKey !== timeZoneContextKey(timeZoneContext || {})) {
248
- cachedByTimeZone.delete(cacheKey);
249
- return aggregateByDay(normalizedRows, timeZoneContext);
250
- }
251
- return daily;
252
- }).catch((error) => {
253
- if (cachedByTimeZone.get(cacheKey) === pending) cachedByTimeZone.delete(cacheKey);
254
- throw error;
255
- });
256
- // Share both completed and in-flight aggregates across summary/daily/heatmap.
257
- boundedCacheSet(cachedByTimeZone, cacheKey, pending, MAX_TIME_ZONE_CACHE_ENTRIES);
258
- return pending;
259
- }
260
-
261
- async function buildDailyAggregates(normalizedRows, timeZoneContext) {
262
- const byDay = new Map();
263
- let processed = 0;
264
- for (const row of normalizedRows) {
265
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
266
- if (!row.hour_start) continue;
267
- const day = rowDayKey(row, timeZoneContext);
268
- if (!day) continue;
269
- if (!byDay.has(day)) {
270
- byDay.set(day, {
271
- day,
272
- total_tokens: 0,
273
- billable_total_tokens: 0,
274
- total_cost_usd: 0,
275
- input_tokens: 0,
276
- output_tokens: 0,
277
- cached_input_tokens: 0,
278
- cache_creation_input_tokens: 0,
279
- reasoning_output_tokens: 0,
280
- conversation_count: 0,
281
- });
282
- }
283
- const a = byDay.get(day);
284
- a.total_tokens += row.total_tokens || 0;
285
- a.billable_total_tokens += row.billable_total_tokens ?? row.total_tokens ?? 0;
286
- a.total_cost_usd += computeRowCost(row);
287
- a.input_tokens += row.input_tokens || 0;
288
- a.output_tokens += row.output_tokens || 0;
289
- a.cached_input_tokens += row.cached_input_tokens || 0;
290
- a.cache_creation_input_tokens += row.cache_creation_input_tokens || 0;
291
- a.reasoning_output_tokens += row.reasoning_output_tokens || 0;
292
- a.conversation_count += row.conversation_count || 0;
293
-
294
- if (!a.models) {
295
- a.models = {};
296
- }
297
- const model = row.model || "unknown";
298
- a.models[model] = (a.models[model] || 0) + (row.total_tokens || 0);
299
- }
300
- const daily = Array.from(byDay.values()).sort((a, b) => a.day.localeCompare(b.day));
301
- return daily;
302
- }
303
-
304
- function buildCodexCategoryFallbackFromQueue(queueRows, { from, to, timeZoneContext }) {
305
- const totals = {
306
- input_tokens: 0,
307
- cached_input_tokens: 0,
308
- cache_creation_input_tokens: 0,
309
- output_tokens: 0,
310
- reasoning_output_tokens: 0,
311
- total_tokens: 0,
312
- };
313
- let conversationCount = 0;
314
-
315
- for (const row of queueRows || []) {
316
- if ((row?.source || "") !== "codex") continue;
317
- if (!row.hour_start) continue;
318
- const day = rowDayKey(row, timeZoneContext);
319
- if (from && day < from) continue;
320
- if (to && day > to) continue;
321
- totals.input_tokens += Number(row.input_tokens || 0);
322
- totals.cached_input_tokens += Number(row.cached_input_tokens || 0);
323
- totals.cache_creation_input_tokens += Number(row.cache_creation_input_tokens || 0);
324
- totals.output_tokens += Number(row.output_tokens || 0);
325
- totals.reasoning_output_tokens += Number(row.reasoning_output_tokens || 0);
326
- totals.total_tokens += Number(row.total_tokens || 0);
327
- conversationCount += Number(row.conversation_count || 0);
328
- }
329
-
330
- return {
331
- source: "codex",
332
- scope: "supported",
333
- breakdown_status: "queue_fallback",
334
- totals,
335
- session_count: 0,
336
- message_count: conversationCount,
337
- fallback: "queue_totals",
338
- message_breakdown: {
339
- categories: [
340
- {
341
- key: "user_input",
342
- name: "User input",
343
- totals: {
344
- input_tokens: totals.input_tokens,
345
- cached_input_tokens: 0,
346
- cache_creation_input_tokens: 0,
347
- output_tokens: 0,
348
- reasoning_output_tokens: 0,
349
- total_tokens: totals.input_tokens,
350
- },
351
- },
352
- {
353
- key: "conversation_history",
354
- name: "Conversation history",
355
- totals: {
356
- input_tokens: 0,
357
- cached_input_tokens: totals.cached_input_tokens,
358
- cache_creation_input_tokens: totals.cache_creation_input_tokens,
359
- output_tokens: 0,
360
- reasoning_output_tokens: 0,
361
- total_tokens: totals.cached_input_tokens + totals.cache_creation_input_tokens,
362
- },
363
- },
364
- {
365
- key: "assistant_response",
366
- name: "Assistant response",
367
- totals: {
368
- input_tokens: 0,
369
- cached_input_tokens: 0,
370
- cache_creation_input_tokens: 0,
371
- output_tokens: Math.max(0, totals.output_tokens - totals.reasoning_output_tokens),
372
- reasoning_output_tokens: 0,
373
- total_tokens: Math.max(0, totals.output_tokens - totals.reasoning_output_tokens),
374
- },
375
- },
376
- ].sort((a, b) => Number(b.totals.total_tokens || 0) - Number(a.totals.total_tokens || 0)),
377
- privacy: {
378
- includes_content: false,
379
- note: "Queue fallback includes aggregated token categories only; message text is never returned.",
380
- },
381
- },
382
- tool_calls_breakdown: {
383
- total_calls: 0,
384
- tools: [],
385
- categories: [],
386
- tools_total: 0,
387
- privacy: {
388
- includes_inputs: false,
389
- note: "Codex rollout sessions were unavailable; totals come from Token Usage queue rows.",
390
- },
391
- },
392
- exec_command_breakdown: {
393
- by_type: [],
394
- by_exit: [],
395
- },
396
- };
397
- }
398
-
399
- function getRequestedUsageScope(url) {
400
- if (url.searchParams.get("include_account_level") === "1") return "all";
401
- return normalizeUsageScope(url.searchParams.get("scope"));
402
- }
403
-
404
- async function scopedQueueRows(queuePath, url) {
405
- const scope = getRequestedUsageScope(url);
406
- const allRows = await readQueueData(queuePath);
407
- return {
408
- scope,
409
- allRows,
410
- rows: filterRowsByUsageScope(allRows, scope),
411
- excludedSources: listExcludedSources(allRows, scope),
412
- };
413
- }
414
-
415
- // ── Local achievements ───────────────────────────────────────────────────────
416
- // Local-only badges (the cloud nine live in scripts/ops/user-badges.sql).
417
- // Thresholds are ordered bronze → silver → gold → diamond. This module is the
418
- // single server-side home for LOCAL thresholds; the dashboard renders whatever
419
- // the payload says and embeds none of these numbers.
420
- const LOCAL_BADGE_THRESHOLDS = {
421
- project_hopper: [3, 5, 10, 20], // distinct projects
422
- project_devotion: [1000000, 10000000, 100000000, 1000000000], // max tokens in one project
423
- night_owl: [5, 20, 60, 150], // active hour buckets between 00:00–05:59 local
424
- };
425
-
426
- const LOCAL_TIER_KEYS = ["bronze", "silver", "gold", "diamond"];
427
-
428
- /**
429
- * Compute the local badge set from deduped queue rows.
430
- * Rows are replayed in hour_start order so each tier's `achieved` timestamp is
431
- * the hour at which the running metric first crossed that threshold. Local
432
- * time (night_owl) follows the caller's tz query params like every other
433
- * usage endpoint.
434
- */
435
- function computeLocalAchievements(queueRows, projectRows, { timeZoneContext } = {}) {
436
- const sortByHour = (rows) =>
437
- rows
438
- .filter((row) => row && row.hour_start)
439
- .slice()
440
- .sort((a, b) => String(a.hour_start).localeCompare(String(b.hour_start)));
441
-
442
- const trackers = {
443
- project_hopper: { value: 0, achieved: {}, meta: {} },
444
- project_devotion: { value: 0, achieved: {}, meta: {} },
445
- night_owl: { value: 0, achieved: {}, meta: {} },
446
- };
447
-
448
- const bump = (badgeId, newValue, atIso, meta) => {
449
- const tracker = trackers[badgeId];
450
- if (newValue <= tracker.value) return;
451
- tracker.value = newValue;
452
- if (meta) tracker.meta = meta;
453
- const thresholds = LOCAL_BADGE_THRESHOLDS[badgeId];
454
- for (let i = 0; i < thresholds.length; i += 1) {
455
- const tierKey = LOCAL_TIER_KEYS[i];
456
- if (newValue >= thresholds[i] && !tracker.achieved[tierKey]) {
457
- tracker.achieved[tierKey] = atIso;
458
- }
459
- }
460
- };
461
-
462
- const seenProjects = new Set();
463
- const perProjectTokens = new Map();
464
- for (const row of sortByHour(projectRows || [])) {
465
- const projectKey = row.project_key;
466
- const tokens = Number(row.total_tokens || 0);
467
- if (!projectKey || tokens <= 0) continue;
468
- if (!seenProjects.has(projectKey)) {
469
- seenProjects.add(projectKey);
470
- bump("project_hopper", seenProjects.size, row.hour_start);
471
- }
472
- const running = (perProjectTokens.get(projectKey) || 0) + tokens;
473
- perProjectTokens.set(projectKey, running);
474
- if (running > trackers.project_devotion.value) {
475
- bump("project_devotion", running, row.hour_start, { project_key: projectKey });
476
- }
477
- }
478
-
479
- const nightHours = new Set();
480
- for (const row of sortByHour(queueRows || [])) {
481
- if (Number(row.total_tokens || 0) <= 0) continue;
482
- if (nightHours.has(row.hour_start)) continue;
483
- const parts = getZonedParts(new Date(row.hour_start), timeZoneContext || {});
484
- if (!parts || parts.hour >= 6) continue;
485
- nightHours.add(row.hour_start);
486
- bump("night_owl", nightHours.size, row.hour_start);
487
- }
488
-
489
- return Object.entries(LOCAL_BADGE_THRESHOLDS).map(([badgeId, thresholds]) => {
490
- const tracker = trackers[badgeId];
491
- let tier = 0;
492
- for (let i = 0; i < thresholds.length; i += 1) {
493
- if (tracker.value >= thresholds[i]) tier = i + 1;
494
- }
495
- return {
496
- id: badgeId,
497
- tier,
498
- metric_value: tracker.value,
499
- thresholds: thresholds.slice(),
500
- lower_is_better: false,
501
- next_threshold: tier >= 4 ? null : thresholds[tier],
502
- achieved: {
503
- bronze: tracker.achieved.bronze || null,
504
- silver: tracker.achieved.silver || null,
505
- gold: tracker.achieved.gold || null,
506
- diamond: tracker.achieved.diamond || null,
507
- },
508
- meta: tracker.meta,
509
- };
510
- });
511
- }
512
-
513
- function getTimeZoneContext(url) {
514
- const tz = String(url.searchParams.get("tz") || "").trim();
515
- const rawOffset = Number(url.searchParams.get("tz_offset_minutes"));
516
- return {
517
- timeZone: tz || null,
518
- offsetMinutes: Number.isFinite(rawOffset) ? Math.trunc(rawOffset) : null,
519
- };
520
- }
521
-
522
- function getZonedParts(date, { timeZone, offsetMinutes } = {}) {
523
- const dt = date instanceof Date ? date : new Date(date);
524
- if (!Number.isFinite(dt.getTime())) return null;
525
-
526
- if (timeZone && typeof Intl !== "undefined" && Intl.DateTimeFormat) {
527
- try {
528
- let formatter;
529
- if (zonedPartsFormatters.has(timeZone)) {
530
- formatter = zonedPartsFormatters.get(timeZone);
531
- } else {
532
- try {
533
- formatter = new Intl.DateTimeFormat("en-CA", {
534
- timeZone,
535
- year: "numeric",
536
- month: "2-digit",
537
- day: "2-digit",
538
- hour: "2-digit",
539
- minute: "2-digit",
540
- second: "2-digit",
541
- hourCycle: "h23",
542
- });
543
- } catch {
544
- // Cache invalid zone ids too; otherwise a bad local query would
545
- // throw once per queue row before falling back to its fixed offset.
546
- formatter = null;
547
- }
548
- boundedCacheSet(
549
- zonedPartsFormatters,
550
- timeZone,
551
- formatter,
552
- MAX_TIME_ZONE_CACHE_ENTRIES,
553
- );
554
- }
555
- const parts = formatter?.formatToParts(dt) || [];
556
- const values = parts.reduce((acc, part) => {
557
- if (part.type && part.value) acc[part.type] = part.value;
558
- return acc;
559
- }, {});
560
- const year = Number(values.year);
561
- const month = Number(values.month);
562
- const day = Number(values.day);
563
- const hour = Number(values.hour);
564
- const minute = Number(values.minute);
565
- const second = Number(values.second);
566
- if ([year, month, day, hour, minute, second].every(Number.isFinite)) {
567
- return { year, month, day, hour, minute, second };
568
- }
569
- } catch (_e) {
570
- // fall through
571
- }
572
- }
573
-
574
- if (Number.isFinite(offsetMinutes)) {
575
- const shifted = new Date(dt.getTime() + offsetMinutes * 60 * 1000);
576
- return {
577
- year: shifted.getUTCFullYear(),
578
- month: shifted.getUTCMonth() + 1,
579
- day: shifted.getUTCDate(),
580
- hour: shifted.getUTCHours(),
581
- minute: shifted.getUTCMinutes(),
582
- second: shifted.getUTCSeconds(),
583
- };
584
- }
585
-
586
- return {
587
- year: dt.getFullYear(),
588
- month: dt.getMonth() + 1,
589
- day: dt.getDate(),
590
- hour: dt.getHours(),
591
- minute: dt.getMinutes(),
592
- second: dt.getSeconds(),
593
- };
594
- }
595
-
596
- function formatPartsDayKey(parts) {
597
- if (!parts) return "";
598
- return `${parts.year}-${String(parts.month).padStart(2, "0")}-${String(parts.day).padStart(2, "0")}`;
599
- }
600
-
601
- async function aggregateHourlyByDay(rows, dayKey, timeZoneContext) {
602
- const byHour = new Map();
603
- let processed = 0;
604
- for (const row of rows) {
605
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
606
- if (!row.hour_start) continue;
607
- const parts = getZonedParts(new Date(row.hour_start), timeZoneContext);
608
- if (!parts) continue;
609
- if (formatPartsDayKey(parts) !== dayKey) continue;
610
- const hourKey = `${dayKey}T${String(parts.hour).padStart(2, "0")}:00:00`;
611
- if (!byHour.has(hourKey)) {
612
- byHour.set(hourKey, {
613
- hour: hourKey,
614
- total_tokens: 0,
615
- billable_total_tokens: 0,
616
- input_tokens: 0,
617
- output_tokens: 0,
618
- cached_input_tokens: 0,
619
- cache_creation_input_tokens: 0,
620
- reasoning_output_tokens: 0,
621
- conversation_count: 0,
622
- });
623
- }
624
- const bucket = byHour.get(hourKey);
625
- bucket.total_tokens += row.total_tokens || 0;
626
- bucket.billable_total_tokens += row.billable_total_tokens ?? row.total_tokens ?? 0;
627
- bucket.input_tokens += row.input_tokens || 0;
628
- bucket.output_tokens += row.output_tokens || 0;
629
- bucket.cached_input_tokens += row.cached_input_tokens || 0;
630
- bucket.cache_creation_input_tokens += row.cache_creation_input_tokens || 0;
631
- bucket.reasoning_output_tokens += row.reasoning_output_tokens || 0;
632
- bucket.conversation_count += row.conversation_count || 0;
633
-
634
- if (!bucket.models) {
635
- bucket.models = {};
636
- }
637
- const model = row.model || "unknown";
638
- bucket.models[model] = (bucket.models[model] || 0) + (row.total_tokens || 0);
639
- }
640
- return Array.from(byHour.values()).sort((a, b) => a.hour.localeCompare(b.hour));
641
- }
642
-
643
- // ---------------------------------------------------------------------------
644
- // Sync helper
645
- // ---------------------------------------------------------------------------
646
-
647
- function trimOutput(value, max = 4000) {
648
- const t = String(value || "");
649
- return t.length <= max ? t : t.slice(t.length - max);
650
- }
651
-
652
- function normalizeRemoteHttpBaseUrl(value) {
653
- if (typeof value !== "string") return null;
654
- const trimmed = value.trim();
655
- if (!trimmed) return null;
656
- try {
657
- const url = new URL(trimmed);
658
- if (url.protocol !== "http:" && url.protocol !== "https:") return null;
659
- url.username = "";
660
- url.password = "";
661
- url.hash = "";
662
- return url.toString().replace(/\/$/, "");
663
- } catch (_e) {
664
- return null;
665
- }
666
- }
667
-
668
- function resolveAllowedInsforgeBaseUrl(value, runtime = resolveRuntimeConfig()) {
669
- const requested = normalizeRemoteHttpBaseUrl(value);
670
- if (!requested) return null;
671
-
672
- return requested === normalizeRemoteHttpBaseUrl(runtime.baseUrl) ? requested : null;
673
- }
674
-
675
- function parseCookieHeader(value) {
676
- const out = new Map();
677
- if (typeof value !== "string" || !value.trim()) return out;
678
- for (const part of value.split(";")) {
679
- const idx = part.indexOf("=");
680
- if (idx < 1) continue;
681
- const key = part.slice(0, idx).trim();
682
- const rawValue = part.slice(idx + 1).trim();
683
- if (key) out.set(key, rawValue);
684
- }
685
- return out;
686
- }
687
-
688
- function isLoopbackHostname(hostname) {
689
- return hostname === "127.0.0.1" || hostname === "localhost" || hostname === "::1" || hostname === "[::1]";
690
- }
691
-
692
- function hasAllowedLoopbackOrigin(headers = {}) {
693
- const candidates = [headers.origin, headers.referer];
694
- for (const raw of candidates) {
695
- if (raw == null || raw === "") continue;
696
- try {
697
- const url = new URL(String(raw));
698
- if (url.protocol !== "http:" || !isLoopbackHostname(url.hostname)) return false;
699
- } catch (_e) {
700
- return false;
701
- }
702
- }
703
- return true;
704
- }
705
-
706
- function readJsonBody(req) {
707
- return new Promise((resolve, reject) => {
708
- const chunks = [];
709
- req.on("data", (c) => chunks.push(c));
710
- req.on("end", () => {
711
- try {
712
- const raw = Buffer.concat(chunks).toString("utf8");
713
- if (!raw.trim()) return resolve({});
714
- resolve(JSON.parse(raw));
715
- } catch (e) {
716
- reject(e);
717
- }
718
- });
719
- req.on("error", reject);
720
- });
721
- }
722
-
723
- function readBodyLimited(req, maxBytes) {
724
- return new Promise((resolve, reject) => {
725
- const chunks = [];
726
- let total = 0;
727
- let failed = false;
728
- req.on("data", (chunk) => {
729
- if (failed) return;
730
- total += chunk.length;
731
- if (total > maxBytes) {
732
- failed = true;
733
- reject(new Error(`Request body exceeds ${Math.ceil(maxBytes / 1024 / 1024)} MB`));
734
- return;
735
- }
736
- chunks.push(chunk);
737
- });
738
- req.on("end", () => { if (!failed) resolve(Buffer.concat(chunks)); });
739
- req.on("error", (error) => { if (!failed) reject(error); });
740
- });
741
- }
742
-
743
- function runSyncCommand(extraEnv = {}, opts = {}) {
744
- return new Promise((resolve, reject) => {
745
- const args = opts.trackerDataDir
746
- ? ["-e", `require(${JSON.stringify(path.resolve(__dirname, "../commands/sync.js"))}).cmdSync(process.argv.slice(1), ${JSON.stringify({ trackerDataDir: opts.trackerDataDir, home: path.dirname(opts.trackerDataDir), ...(opts.syncContext || {}) })}).catch(e => { console.error(e.message); process.exitCode = 1; })`, "--"]
747
- : [TRACKER_BIN, "sync"];
748
- if (opts.auto === true) args.push("--auto");
749
- if (opts.background === true) args.push("--background");
750
- if (opts.publishAccount === true) args.push("--publish-account");
751
- if (opts.allLocalSources === true) args.push("--all-local-sources");
752
- if (opts.drain === true) args.push("--drain");
753
- if (opts.waitForLock === true) args.push("--wait-for-lock");
754
- if (opts.signal?.aborted) { reject(Object.assign(new Error("Account session changed"), { code: "auth_session_changed", status: 409 })); return; }
755
- const child = spawn(process.execPath, args, {
756
- env: {
757
- ...process.env, ...extraEnv,
758
- ...(extraEnv.TOKENUSAGE_DEVICE_TOKEN ? {
759
- TOKENUSAGE_INSFORGE_BASE_URL: extraEnv.TOKENUSAGE_INSFORGE_BASE_URL,
760
- TOKENUSAGE_INSFORGE_ANON_KEY: extraEnv.TOKENUSAGE_INSFORGE_ANON_KEY || "",
761
- } : {}),
762
- TOKENUSAGE_LOCAL_SYNC_DEVICE_TOKEN: extraEnv.TOKENUSAGE_DEVICE_TOKEN || "",
763
- },
764
- // The API can be hosted by a GUI process (desktop shell): keep the piped
765
- // sync child console-less on Windows.
766
- windowsHide: true,
767
- stdio: ["ignore", "pipe", "pipe"],
768
- });
769
- const cancel = () => child.kill("SIGTERM");
770
- opts.signal?.addEventListener("abort", cancel, { once: true });
771
- let stdout = "";
772
- let stderr = "";
773
- let settled = false;
774
- const finish = (fn, v) => {
775
- if (settled) return;
776
- settled = true;
777
- clearTimeout(tid);
778
- opts.signal?.removeEventListener("abort", cancel);
779
- fn(v);
780
- };
781
- let timedOut = false;
782
- const tid = setTimeout(() => {
783
- timedOut = true;
784
- child.kill("SIGTERM");
785
- }, SYNC_TIMEOUT_MS);
786
- child.stdout?.on("data", (c) => {
787
- stdout += c;
788
- });
789
- child.stderr?.on("data", (c) => {
790
- stderr += c;
791
- });
792
- child.on("error", (e) => {
793
- finish(reject, Object.assign(e, { stdout: trimOutput(stdout), stderr: trimOutput(stderr) }));
794
- });
795
- child.on("close", (code) => {
796
- if (timedOut) { finish(reject, Object.assign(new Error("Sync timed out"), { code: "SYNC_TIMEOUT", stdout: trimOutput(stdout), stderr: trimOutput(stderr) })); return; }
797
- if (opts.signal?.aborted) { finish(reject, Object.assign(new Error("Account session changed"), { code: "auth_session_changed", status: 409 })); return; }
798
- const r = { code: code ?? 1, stdout: trimOutput(stdout), stderr: trimOutput(stderr) };
799
- if (code === 0) {
800
- finish(resolve, r);
801
- return;
802
- }
803
- const error = Object.assign(
804
- new Error(r.stderr || r.stdout || `exit ${r.code}`),
805
- r,
806
- );
807
- if (/\bSYNC_BUSY\b/.test(`${r.stderr}\n${r.stdout}`)) {
808
- error.code = "SYNC_BUSY";
809
- }
810
- finish(reject, error);
811
- });
812
- });
813
- }
814
-
815
- // ---------------------------------------------------------------------------
816
- // Project detection helpers
817
- // ---------------------------------------------------------------------------
818
-
819
- function parseGitUrl(url) {
820
- if (!url) return null;
821
- const ssh = url.match(/git@[^:]+:([^/]+)\/(.+?)(?:\.git)?$/);
822
- if (ssh) return { owner: ssh[1], repo: ssh[2] };
823
- const http = url.match(/https?:\/\/[^/]+\/([^/]+)\/(.+?)(?:\.git)?$/);
824
- if (http) return { owner: http[1], repo: http[2] };
825
- return null;
826
- }
827
-
828
- function extractProjectFromCwd(cwd) {
829
- const home = os.homedir();
830
- if (!cwd || cwd === home) return null;
831
- const rel = cwd.replace(home + "/", "");
832
- const parts = rel.split("/").filter((p) => p && !p.startsWith(".") && p !== "ext-global");
833
- return parts.length > 0 ? parts[0] : null;
834
- }
835
-
836
- function scanCodexProjects(projectMap) {
837
- const dir = path.join(os.homedir(), ".codex", "sessions");
838
- try {
839
- for (const year of fs.readdirSync(dir)) {
840
- const yp = path.join(dir, year);
841
- if (!fs.statSync(yp).isDirectory()) continue;
842
- for (const month of fs.readdirSync(yp)) {
843
- const mp = path.join(yp, month);
844
- if (!fs.statSync(mp).isDirectory()) continue;
845
- for (const day of fs.readdirSync(mp)) {
846
- const dp = path.join(mp, day);
847
- if (!fs.statSync(dp).isDirectory()) continue;
848
- const files = fs.readdirSync(dp).filter((f) => f.endsWith(".jsonl"));
849
- for (const file of files.slice(0, 200)) {
850
- try {
851
- const first = fs.readFileSync(path.join(dp, file), "utf8").split("\n")[0];
852
- const d = JSON.parse(first);
853
- if (d.git?.repository_url) {
854
- const p = parseGitUrl(d.git.repository_url);
855
- if (p) {
856
- const key = `${p.owner}/${p.repo}`;
857
- if (!projectMap.has(key))
858
- projectMap.set(key, {
859
- project_key: key,
860
- project_ref: d.git.repository_url,
861
- count: 0,
862
- });
863
- projectMap.get(key).count++;
864
- }
865
- }
866
- } catch (_e) {}
867
- }
868
- }
869
- }
870
- }
871
- } catch (_e) {}
872
- }
873
-
874
- function findSubagentsDirs(dir, depth) {
875
- const out = [];
876
- if (depth > 3) return out;
877
- try {
878
- for (const item of fs.readdirSync(dir)) {
879
- const fp = path.join(dir, item);
880
- if (!fs.statSync(fp).isDirectory()) continue;
881
- if (item === "subagents") out.push(fp);
882
- else out.push(...findSubagentsDirs(fp, depth + 1));
883
- }
884
- } catch (_e) {}
885
- return out;
886
- }
887
-
888
- function scanClaudeProjects(projectMap) {
889
- const dir = path.join(os.homedir(), ".claude", "projects");
890
- try {
891
- for (const subDir of findSubagentsDirs(dir, 0)) {
892
- const files = fs.readdirSync(subDir).filter((f) => f.endsWith(".jsonl"));
893
- for (const file of files.slice(0, 100)) {
894
- try {
895
- const first = fs.readFileSync(path.join(subDir, file), "utf8").split("\n")[0];
896
- if (!first) continue;
897
- const d = JSON.parse(first);
898
- const name = extractProjectFromCwd(d.cwd);
899
- if (name) {
900
- if (!projectMap.has(name))
901
- projectMap.set(name, {
902
- project_key: name,
903
- project_ref: `file://${d.cwd}`,
904
- count: 0,
905
- });
906
- projectMap.get(name).count++;
907
- }
908
- } catch (_e) {}
909
- }
910
- }
911
- } catch (_e) {}
912
- }
913
-
914
- // ---------------------------------------------------------------------------
915
- // JSON response helper
916
- // ---------------------------------------------------------------------------
917
-
918
- function json(res, data, status) {
919
- // Usage and cost payloads change on every sync, and cost is re-priced on
920
- // every read (pricing is applied per request, never stored in the queue) —
921
- // so a cached response can pin stale costs on screen indefinitely. WebView2
922
- // caches responses that carry no freshness directive, and the dashboard's
923
- // local fetch path omits `cache: "no-store"` (only the cloud path sets it).
924
- // Endpoints that care already set this header by hand; do it once here so
925
- // every JSON response is covered.
926
- res.writeHead(status || 200, {
927
- "Content-Type": "application/json",
928
- "Cache-Control": "no-store",
929
- });
930
- res.end(JSON.stringify(data));
931
- }
932
-
933
- // ---------------------------------------------------------------------------
934
- // IP check API proxy: dashboard/src/pages/IpCheckPage.jsx is a native React
935
- // page that calls ip.net.coffee's data endpoints (/api/iprisk, /api/geoip,
936
- // /api/dns/result, /favicons, /claude/status.json). Browser-side fetch can't
937
- // hit them cross-origin from the dashboard, so we reverse-proxy /proxy/ipcheck/*
938
- // to https://ip.net.coffee/* and strip embedding-hostile headers.
939
- // (Previously this proxy also served the upstream HTML page for an iframe;
940
- // the iframe and its HTML-rewrite path have been removed.)
941
- // ---------------------------------------------------------------------------
942
-
943
- const IP_CHECK_PROXY_PREFIX = "/proxy/ipcheck";
944
- const IP_CHECK_TARGET = "https://ip.net.coffee";
945
-
946
- // HTTP hop-by-hop headers (RFC 7230 §6.1) plus headers undici/fetch manages
947
- // internally. Forwarding any of these to `fetch(...)` either silently breaks
948
- // the request (host being wrong) or, on stricter undici versions like the
949
- // 6.24.1 shipped with Node 22.22.2, throws UND_ERR_INVALID_ARG and turns
950
- // every proxied POST into a 502. Keep this set authoritative for every
951
- // reverse-proxy site in this module.
952
- const HOP_BY_HOP_HEADERS = new Set([
953
- "host",
954
- "connection",
955
- "keep-alive",
956
- "content-length",
957
- "transfer-encoding",
958
- "upgrade",
959
- "proxy-authorization",
960
- "proxy-authenticate",
961
- "proxy-connection",
962
- "te",
963
- "trailer",
964
- "trailers",
965
- ]);
966
-
967
- // Strip forbidden + hop-by-hop headers when forwarding an inbound request to
968
- // fetch(). Honours the Connection header's named-headers list (RFC 7230 §6.1)
969
- // so values like `Connection: keep-alive, x-custom` also drop x-custom.
970
- function buildProxyHeaders(headers) {
971
- const entries =
972
- headers && typeof headers.entries === "function"
973
- ? Array.from(headers.entries())
974
- : Object.entries(headers || {});
975
-
976
- const connectionNamed = new Set();
977
- const normalized = [];
978
- for (const [rawKey, rawValue] of entries) {
979
- if (rawValue == null) continue;
980
- const key = String(rawKey).toLowerCase();
981
- normalized.push([key, rawValue]);
982
- if (key === "connection") {
983
- const values = Array.isArray(rawValue) ? rawValue : [rawValue];
984
- for (const v of values) {
985
- String(v)
986
- .split(",")
987
- .map((part) => part.trim().toLowerCase())
988
- .filter(Boolean)
989
- .forEach((part) => connectionNamed.add(part));
990
- }
991
- }
992
- }
993
-
994
- const out = {};
995
- for (const [key, rawValue] of normalized) {
996
- if (HOP_BY_HOP_HEADERS.has(key) || connectionNamed.has(key)) continue;
997
- if (Array.isArray(rawValue)) {
998
- const joined = rawValue.filter((e) => e != null).map(String).join(", ");
999
- if (joined) out[key] = joined;
1000
- continue;
1001
- }
1002
- out[key] = String(rawValue);
1003
- }
1004
- return out;
1005
- }
1006
-
1007
- // ---------------------------------------------------------------------------
1008
- // Main handler factory
1009
- // ---------------------------------------------------------------------------
1010
-
1011
- function createLocalApiHandler({ queuePath, serverVersion = null, trackerDataDir: isolatedTrackerDataDir = null, syncContext = null }) {
1012
- const qp = queuePath || resolveQueuePath();
1013
- const normalizedServerVersion = typeof serverVersion === "string" ? serverVersion.trim() : "";
1014
-
1015
- // Server-side cookie relay: captures auth cookies from InsForge cloud responses
1016
- // so that both browser and WKWebView share the same login session via the proxy.
1017
- // Persisted to disk so cookies survive server restarts.
1018
- const csrfRelayCookieName = "insforge_csrf_token";
1019
- let relayCookies = new Map();
1020
- const localAuthToken = crypto.randomBytes(24).toString("hex");
1021
- const trackerDataDir = isolatedTrackerDataDir || path.dirname(qp);
1022
- const cookiePath = path.join(trackerDataDir, "relay-cookies.json");
1023
- const localSyncDeviceTokenStore = createCloudDeviceTokenStore(path.join(trackerDataDir, "cloud-device-token.json"));
1024
- const localSyncDeviceTokenCache = new Map();
1025
- const localSyncDeviceTokenInflight = new Map();
1026
- let localSyncDeviceTokenUserId = "";
1027
- let localCloudSession = null;
1028
- let localCloudGeneration = 0;
1029
- const activeCloudSyncs = new Set();
1030
- const localSyncDeviceMetadata = new Map();
1031
- function invalidateLocalCloudSession() {
1032
- localCloudGeneration += 1;
1033
- localCloudSession = null;
1034
- for (const controller of activeCloudSyncs) controller.abort();
1035
- }
1036
- function clearLocalSyncDeviceTokenCache() {
1037
- invalidateLocalCloudSession();
1038
- localSyncDeviceMetadata.clear();
1039
- localSyncDeviceTokenCache.clear();
1040
- localSyncDeviceTokenInflight.clear();
1041
- localSyncDeviceTokenStore.clear();
1042
- localSyncDeviceTokenUserId = "";
1043
- }
1044
- function invalidateLocalSyncDeviceToken(token) {
1045
- if (!token) return;
1046
- for (const [key, value] of localSyncDeviceTokenCache) {
1047
- if (value === token) localSyncDeviceTokenCache.delete(key);
1048
- }
1049
- localSyncDeviceTokenStore.invalidate(token);
1050
- }
1051
- function currentCloudUploadFailure(syncStartedAt, { backoff = false, deviceToken, attemptId } = {}) {
1052
- try {
1053
- const state = JSON.parse(fs.readFileSync(path.join(path.dirname(qp), "upload.throttle.json"), "utf8"));
1054
- if (attemptId && state.lastErrorAttemptId === attemptId &&
1055
- Date.parse(state.lastErrorAt || "") >= syncStartedAt && Number(state.lastErrorStatus) > 0) {
1056
- if (state.lastErrorCode === "CLOUD_DEVICE_TOKEN_REJECTED") invalidateLocalSyncDeviceToken(deviceToken);
1057
- return { code: state.lastErrorCode || "CLOUD_UPLOAD_FAILED", status: Number(state.lastErrorStatus), retryAfterMs: Math.max(0, Math.max(Number(state.backoffUntilMs || 0), Number(state.nextAllowedAtMs || 0)) - Date.now()), nextAllowedAt: state.nextAllowedAtMs ? new Date(state.nextAllowedAtMs).toISOString() : null };
1058
- }
1059
- if (backoff && Math.max(Number(state.backoffUntilMs || 0), Number(state.nextAllowedAtMs || 0)) > syncStartedAt) {
1060
- return { code: "SYNC_UPLOAD_BACKOFF", status: 429, retryAfterMs: Math.max(0, Math.max(Number(state.backoffUntilMs || 0), Number(state.nextAllowedAtMs || 0)) - Date.now()), nextAllowedAt: state.nextAllowedAtMs ? new Date(state.nextAllowedAtMs).toISOString() : null };
1061
- }
1062
- } catch { /* no upload failure was recorded */ }
1063
- return null;
1064
- }
1065
- // A dashboard refresh fans out to several account-view endpoints. Keep the
1066
- // circuit-breaker state per session instead of one global slot (a successful
1067
- // request for one account must not clear another account's failure), and
1068
- // suppress the duplicate fan-out while the first cloud probe is pending.
1069
- const accountViewFailures = new Map();
1070
- const accountViewInFlight = new Map();
1071
- let accountUploadState = readAccountUploadState();
1072
- const runtimeConfigPath = path.join(path.dirname(qp), "config.json");
1073
- const instancePath = path.join(trackerDataDir, "runtime-instance.json");
1074
- const issuedAccessTokenInstances = new Map();
1075
- let instanceFingerprint = "";
1076
- try { instanceFingerprint = JSON.parse(fs.readFileSync(instancePath, "utf8")).fingerprint || ""; } catch { /* first start */ }
1077
-
1078
- function fingerprintFor(runtime) {
1079
- return crypto.createHash("sha256").update(`${runtime.baseUrl}\0${runtime.anonKey || ""}`).digest("hex");
1080
- }
1081
-
1082
- function getRuntime() {
1083
- let config = {};
1084
- let invalid = false;
1085
- try { config = JSON.parse(fs.readFileSync(runtimeConfigPath, "utf8")); }
1086
- catch (error) { invalid = error.code !== "ENOENT"; }
1087
- if (!config || typeof config !== "object" || Array.isArray(config)) { config = {}; invalid = true; }
1088
- const runtime = resolveRuntimeConfig({ config: config || {}, env: process.env });
1089
- if (invalid) runtime.configurationError = "invalid_local_config";
1090
- if (runtime.configurationError || (!fs.existsSync(runtimeConfigPath) && !fs.existsSync(qp))) return runtime;
1091
- const fingerprint = fingerprintFor(runtime);
1092
- if (fingerprint !== instanceFingerprint) {
1093
- if (instanceFingerprint || runtime.baseUrl !== DEFAULT_BASE_URL) {
1094
- clearRelayCookies("backend instance changed");
1095
- _nativeAuthPending = false; _nativeAuthExpiry = 0;
1096
- resetInstanceState(path.dirname(qp));
1097
- if (trackerDataDir !== path.dirname(qp)) resetInstanceState(trackerDataDir);
1098
- accountViewFailures.clear(); accountViewInFlight.clear();
1099
- if (["deviceToken", "deviceId", "user_id"].some(key => config?.[key]) &&
1100
- normalizeInstanceBaseUrl(config.deviceTokenBaseUrl) !== runtime.baseUrl) {
1101
- config = clearDeviceIdentity(config);
1102
- writeJsonAtomicSync(runtimeConfigPath, config);
1103
- runtime.deviceToken = null;
1104
- }
1105
- }
1106
- fs.mkdirSync(trackerDataDir, { recursive: true });
1107
- fs.writeFileSync(instancePath, JSON.stringify({ fingerprint }), { mode: 0o600 });
1108
- instanceFingerprint = fingerprint;
1109
- }
1110
- return runtime;
1111
- }
1112
-
1113
- function readAccountUploadState() {
1114
- try {
1115
- const state = JSON.parse(fs.readFileSync(path.join(path.dirname(qp), "queue.state.json"), "utf8"));
1116
- return JSON.stringify([Number(state.offset) || 0, state.updatedAt || ""]);
1117
- } catch {
1118
- return "";
1119
- }
1120
- }
1121
-
1122
- function invalidateAfterAccountUpload() {
1123
- const state = readAccountUploadState();
1124
- if (state !== accountUploadState) {
1125
- accountUploadState = state;
1126
- invalidateCloudAccountPayloadCache();
1127
- }
1128
- }
1129
-
1130
- function accountViewFailureUntil(failureKey) {
1131
- const until = accountViewFailures.get(failureKey) || 0;
1132
- if (until > Date.now()) return until;
1133
- if (until) accountViewFailures.delete(failureKey);
1134
- return 0;
1135
- }
1136
-
1137
- function rememberAccountViewFailure(failureKey) {
1138
- accountViewFailures.set(failureKey, Date.now() + ACCOUNT_VIEW_FAILURE_BACKOFF_MS);
1139
- // Refresh-token rotation or account switching can produce many keys over a
1140
- // long-lived tray session. Bound the map without affecting the active key.
1141
- while (accountViewFailures.size > 16) {
1142
- const oldest = accountViewFailures.keys().next().value;
1143
- if (!oldest || oldest === failureKey) break;
1144
- accountViewFailures.delete(oldest);
1145
- }
1146
- }
1147
-
1148
- // Load persisted cookies on startup
1149
- try {
1150
- if (!fs.existsSync(trackerDataDir)) fs.mkdirSync(trackerDataDir, { recursive: true });
1151
- if (fs.existsSync(cookiePath)) {
1152
- const content = fs.readFileSync(cookiePath, "utf8");
1153
- const saved = JSON.parse(content);
1154
- if (saved && typeof saved === "object") {
1155
- let count = 0;
1156
- for (const [k, v] of Object.entries(saved)) {
1157
- relayCookies.set(k, v);
1158
- count++;
1159
- }
1160
- if (count > 0) console.log(`[LocalAPI] Loaded ${count} relay cookies from ${cookiePath}`);
1161
- }
1162
- }
1163
- } catch (e) {
1164
- console.error("[LocalAPI] Failed to load relay cookies:", e.message);
1165
- }
1166
-
1167
- function persistRelayCookies() {
1168
- try {
1169
- // Sticky semantics: never replace an existing on-disk session with an empty cookie map.
1170
- if (relayCookies.size === 0) return;
1171
-
1172
- const json = JSON.stringify(Object.fromEntries(relayCookies));
1173
- fs.writeFileSync(cookiePath, json, { encoding: "utf8", mode: 0o600 });
1174
- } catch (e) {
1175
- console.error("[LocalAPI] Failed to persist relay cookies:", e.message);
1176
- }
1177
- }
1178
-
1179
- function clearRelayCookies(reason) {
1180
- clearLocalSyncDeviceTokenCache();
1181
- invalidateCloudAccountPayloadCache({ sessionChanged: true });
1182
- if (relayCookies.size === 0) return;
1183
- relayCookies.clear();
1184
- try {
1185
- if (fs.existsSync(cookiePath)) fs.unlinkSync(cookiePath);
1186
- } catch (e) {
1187
- console.error("[LocalAPI] Failed to clear relay cookies:", e.message);
1188
- return;
1189
- }
1190
- if (reason) console.warn(`[LocalAPI] Cleared relay cookies: ${reason}`);
1191
- }
1192
-
1193
- function captureSetCookies(headerValue) {
1194
- if (!headerValue) return;
1195
- const parts = headerValue.split(/,(?=\s*\w+=)/);
1196
- let changed = false;
1197
- for (const raw of parts) {
1198
- const eqIdx = raw.indexOf("=");
1199
- if (eqIdx < 1) continue;
1200
- const name = raw.substring(0, eqIdx).trim();
1201
- if (!name) continue;
1202
-
1203
- // Basic sticky logic: if it's a deletion cookie (Max-Age=0 or past date),
1204
- // we only remove it if we have it.
1205
- const lower = raw.toLowerCase();
1206
- const isDeletion = lower.includes("max-age=0") || lower.includes("expires=thu, 01 jan 1970");
1207
-
1208
- if (isDeletion) {
1209
- if (relayCookies.has(name)) {
1210
- if (name === "insforge_refresh_token") invalidateCloudAccountPayloadCache({ sessionChanged: true });
1211
- relayCookies.delete(name);
1212
- changed = true;
1213
- console.log(`[LocalAPI] Cookie deleted: ${name}`);
1214
- }
1215
- } else {
1216
- const oldVal = relayCookies.get(name);
1217
- if (oldVal !== raw.trim()) {
1218
- if (name === "insforge_refresh_token") invalidateCloudAccountPayloadCache({ sessionChanged: true });
1219
- relayCookies.set(name, raw.trim());
1220
- changed = true;
1221
- console.log(`[LocalAPI] Cookie captured: ${name}`);
1222
- }
1223
- }
1224
- }
1225
- if (changed) persistRelayCookies();
1226
- }
1227
-
1228
- function getRelayCookieValue(name, { decode = false } = {}) {
1229
- const raw = relayCookies.get(name);
1230
- if (!raw || typeof raw !== "string") return "";
1231
- const pair = raw.split(";")[0] || "";
1232
- const eqIdx = pair.indexOf("=");
1233
- if (eqIdx < 1) return "";
1234
- const value = pair.substring(eqIdx + 1).trim();
1235
- if (!decode) return value;
1236
- try {
1237
- return decodeURIComponent(value);
1238
- } catch {
1239
- return value;
1240
- }
1241
- }
1242
-
1243
- function captureAuthTokensFromBody(bodyBuffer, contentType, { newLogin = false, sourceFingerprint } = {}) {
1244
- if (!bodyBuffer || !String(contentType || "").toLowerCase().includes("application/json")) return;
1245
- let parsed = null;
1246
- try {
1247
- parsed = JSON.parse(bodyBuffer.toString("utf8"));
1248
- } catch {
1249
- return;
1250
- }
1251
- if (sourceFingerprint && typeof parsed?.accessToken === "string" && parsed.accessToken.length <= 8192) {
1252
- const hash = crypto.createHash("sha256").update(parsed.accessToken).digest("hex");
1253
- if (!issuedAccessTokenInstances.has(hash)) issuedAccessTokenInstances.set(hash, sourceFingerprint);
1254
- while (issuedAccessTokenInstances.size > 64) issuedAccessTokenInstances.delete(issuedAccessTokenInstances.keys().next().value);
1255
- }
1256
- let changed = false;
1257
- const token = typeof parsed?.csrfToken === "string" ? parsed.csrfToken.trim() : "";
1258
- if (token) {
1259
- const cookie = `${csrfRelayCookieName}=${encodeURIComponent(token)}; Path=/; SameSite=Lax`;
1260
- if (relayCookies.get(csrfRelayCookieName) !== cookie) {
1261
- relayCookies.set(csrfRelayCookieName, cookie);
1262
- changed = true;
1263
- }
1264
- }
1265
- const refreshToken = typeof parsed?.refreshToken === "string" ? parsed.refreshToken.trim() : "";
1266
- if (refreshToken) {
1267
- if (newLogin) clearLocalSyncDeviceTokenCache();
1268
- const cookie = `insforge_refresh_token=${encodeURIComponent(refreshToken)}; Path=/; HttpOnly; SameSite=Lax`;
1269
- if (relayCookies.get("insforge_refresh_token") !== cookie) {
1270
- invalidateCloudAccountPayloadCache({ sessionChanged: true });
1271
- relayCookies.set("insforge_refresh_token", cookie);
1272
- changed = true;
1273
- }
1274
- }
1275
- if (changed) persistRelayCookies();
1276
- }
1277
-
1278
- // --- Account (cross-device) view for the native popover ---------------
1279
- // The popover follows the dashboard: it shows aggregated cross-device data
1280
- // only when the user is signed in (a relayed refresh token exists) AND cloud
1281
- // sync is on. Cloud sync is a dashboard (WebView) preference persisted in
1282
- // localStorage; the dashboard mirrors it here via POST
1283
- // /api/functions/token-usage-cloud-sync-pref so the auth-unaware popover can key
1284
- // off the same flag. Defaults OFF, exactly like the dashboard toggle;
1285
- // existing saved choices are preserved.
1286
- const cloudSyncPrefPath = path.join(trackerDataDir, "cloud-sync-pref.json");
1287
- let cloudSyncPrefCache;
1288
- let cloudSyncPrefGeneration = 0;
1289
- function getCloudSyncPref() {
1290
- if (cloudSyncPrefCache === undefined) {
1291
- cloudSyncPrefCache = require("./cloud-sync-prefs").readCloudSyncEnabled(trackerDataDir);
1292
- }
1293
- return cloudSyncPrefCache;
1294
- }
1295
- function setCloudSyncPref(enabled, changedAtMs = Date.now()) {
1296
- // Multiple dashboard windows can deliver mirrors out of order. Keep the
1297
- // newest saved choice; old files without an ordering marker remain valid.
1298
- try {
1299
- const saved = JSON.parse(fs.readFileSync(cloudSyncPrefPath, "utf8"));
1300
- if (Number(saved?.changedAtMs || 0) > changedAtMs ||
1301
- (changedAtMs > 0 && saved?.changedAtMs === changedAtMs && saved?.enabled === false && enabled)) return;
1302
- } catch { /* first choice or an unreadable preference */ }
1303
- if (getCloudSyncPref() !== Boolean(enabled)) {
1304
- cloudSyncPrefGeneration += 1;
1305
- clearLocalSyncDeviceTokenCache();
1306
- invalidateCloudAccountPayloadCache();
1307
- }
1308
- cloudSyncPrefCache = Boolean(enabled);
1309
- try {
1310
- if (!fs.existsSync(trackerDataDir)) fs.mkdirSync(trackerDataDir, { recursive: true });
1311
- fs.writeFileSync(
1312
- cloudSyncPrefPath,
1313
- JSON.stringify({ enabled: cloudSyncPrefCache, changedAtMs, updatedAt: new Date().toISOString() }),
1314
- { encoding: "utf8", mode: 0o600 },
1315
- );
1316
- } catch (e) {
1317
- console.error("[LocalAPI] Failed to persist cloud sync pref:", e.message);
1318
- }
1319
- }
1320
-
1321
- function getRefreshTokenForCloud() {
1322
- return getRelayCookieValue("insforge_refresh_token", { decode: true });
1323
- }
1324
- function setRelayRefreshToken(token) {
1325
- if (!token || typeof token !== "string") return;
1326
- const cookie = `insforge_refresh_token=${encodeURIComponent(token)}; Path=/; HttpOnly; SameSite=Lax`;
1327
- if (relayCookies.get("insforge_refresh_token") !== cookie) {
1328
- relayCookies.set("insforge_refresh_token", cookie);
1329
- persistRelayCookies();
1330
- }
1331
- }
1332
- function setRelayCsrfToken(token) {
1333
- if (!token || typeof token !== "string") return;
1334
- const cookie = `${csrfRelayCookieName}=${encodeURIComponent(token)}; Path=/; SameSite=Lax`;
1335
- if (relayCookies.get(csrfRelayCookieName) !== cookie) {
1336
- relayCookies.set(csrfRelayCookieName, cookie);
1337
- persistRelayCookies();
1338
- }
1339
- }
1340
-
1341
- function localSyncDeviceTokenCacheKey(userId, machineId, baseUrl) {
1342
- return `${baseUrl}\0${userId}\0${machineId}`;
1343
- }
1344
-
1345
- async function issueDeviceTokenForLocalSync(queuePathForMachineId, options = {}) {
1346
- if (!getCloudSyncPref()) return null;
1347
- const refreshToken = getRefreshTokenForCloud();
1348
- if (!options.accessToken && !refreshToken) return null;
1349
- const machineId = getOrCreateMachineId(queuePathForMachineId);
1350
- if (!machineId) return null;
1351
- const runtime = assertInsforgeRuntime(getRuntime());
1352
- const baseUrl = normalizeRemoteHttpBaseUrl(options.baseUrl) ||
1353
- normalizeRemoteHttpBaseUrl(runtime.baseUrl);
1354
- const sessionAtStart = getCloudAccountSessionGeneration();
1355
- const cloudPrefAtStart = cloudSyncPrefGeneration;
1356
- const generationAtStart = localCloudGeneration;
1357
- const minted = options.accessToken ? { accessToken: options.accessToken } : await mintAccessToken({
1358
- baseUrl, anonKey: runtime.anonKey, refreshToken, timeoutMs: runtime.httpTimeoutMs, throwOnFailure: true,
1359
- }).catch((error) => {
1360
- if (error.status === 401 && sessionAtStart === getCloudAccountSessionGeneration()) clearLocalSyncDeviceTokenCache();
1361
- throw error;
1362
- });
1363
- if (!minted?.accessToken || sessionAtStart !== getCloudAccountSessionGeneration()) return null;
1364
- const rotatedRefreshToken =
1365
- typeof minted.refreshToken === "string" && minted.refreshToken.trim()
1366
- ? minted.refreshToken.trim()
1367
- : "";
1368
- if (rotatedRefreshToken) setRelayRefreshToken(rotatedRefreshToken);
1369
- if (minted.csrfToken) setRelayCsrfToken(minted.csrfToken);
1370
- if (!getCloudSyncPref() || cloudPrefAtStart !== cloudSyncPrefGeneration) return null;
1371
- // Refresh validates native ownership. A browser JWT must additionally pass
1372
- // token issuance upstream before the mirror receives an upload binding.
1373
- let userId = "";
1374
- try {
1375
- const payload = JSON.parse(Buffer.from(minted.accessToken.split(".")[1], "base64url").toString("utf8"));
1376
- userId = typeof payload.sub === "string" ? payload.sub : "";
1377
- } catch { /* legacy opaque access tokens retain their isolated refresh key */ }
1378
- if (options.expectedOwnerId && userId !== options.expectedOwnerId) {
1379
- throw Object.assign(new Error("Account owner mismatch"), { status: 409, code: "auth_owner_mismatch" });
1380
- }
1381
- if (userId && localSyncDeviceTokenUserId && userId !== localSyncDeviceTokenUserId) {
1382
- clearLocalSyncDeviceTokenCache();
1383
- }
1384
- if (userId) localSyncDeviceTokenUserId = userId;
1385
- const identity = userId || `refresh:${rotatedRefreshToken || refreshToken}`;
1386
- const cacheKey = localSyncDeviceTokenCacheKey(identity, machineId, baseUrl);
1387
- const binding = { userId, baseUrl, machineId };
1388
- const cachedToken = !options.accessToken && (localSyncDeviceTokenCache.get(cacheKey) ||
1389
- (userId ? localSyncDeviceTokenStore.get(binding) : null));
1390
- if (cachedToken) {
1391
- localSyncDeviceTokenCache.set(cacheKey, cachedToken);
1392
- if (userId) localSyncDeviceTokenStore.set(binding, cachedToken);
1393
- return options.returnSession ? localSyncDeviceMetadata.get(cachedToken) || null : cachedToken;
1394
- }
1395
- const inflightToken = localSyncDeviceTokenInflight.get(cacheKey);
1396
- if (inflightToken?.sessionGeneration === sessionAtStart && inflightToken.generation === generationAtStart) {
1397
- const token = await inflightToken.promise;
1398
- return options.returnSession ? localSyncDeviceMetadata.get(token) || null : token;
1399
- }
1400
-
1401
- const issuePromise = (async () => {
1402
- const root = String(baseUrl || DEFAULT_BASE_URL).replace(/\/$/, "");
1403
- const headers = {
1404
- "Content-Type": "application/json",
1405
- Accept: "application/json",
1406
- Authorization: `Bearer ${minted.accessToken}`,
1407
- };
1408
- if (runtime.anonKey) headers.apikey = runtime.anonKey;
1409
-
1410
- let timeoutId;
1411
- const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
1412
- if (controller && runtime.httpTimeoutMs > 0) {
1413
- timeoutId = setTimeout(() => controller.abort(), runtime.httpTimeoutMs);
1414
- }
1415
-
1416
- const dashboardPlatform =
1417
- process.platform === "darwin" ? "MacIntel" :
1418
- process.platform === "win32" ? "Win32" :
1419
- process.platform === "linux" ? "Linux x86_64" :
1420
- "web";
1421
-
1422
- const res = await fetchFunctionResponse(functionUrlFor(root, "token-usage-device-token-issue"), {
1423
- method: "POST",
1424
- headers,
1425
- signal: controller ? controller.signal : undefined,
1426
- body: JSON.stringify({
1427
- // 必须和 dashboard/src/lib/cloud-sync.ts 使用同一个设备身份。
1428
- // 旧云端设备按 (platform, device_name) 认领;如果这里发明
1429
- // local-sync 身份,会多出一个 active device,账户视图会把历史求和两次。
1430
- device_name: getSystemDeviceName() || `Token Usage (dashboard) #${machineId.slice(0, 8)}`,
1431
- platform: dashboardPlatform,
1432
- machine_id: machineId,
1433
- }),
1434
- }).finally(() => {
1435
- if (timeoutId) clearTimeout(timeoutId);
1436
- });
1437
- getRuntime();
1438
- if (sessionAtStart !== getCloudAccountSessionGeneration() || !getCloudSyncPref() || cloudPrefAtStart !== cloudSyncPrefGeneration || generationAtStart !== localCloudGeneration) return null;
1439
- if (!res.ok) {
1440
- if (res.status === 401 && sessionAtStart === getCloudAccountSessionGeneration()) {
1441
- localSyncDeviceTokenCache.delete(cacheKey);
1442
- localSyncDeviceTokenStore.clear(binding);
1443
- }
1444
- const denied = await res.json().catch(() => ({}));
1445
- if (typeof denied?.code === "string" && denied.code.startsWith("cloud_")) {
1446
- throw Object.assign(new Error(denied.code), { code: denied.code, status: res.status,
1447
- recoveryUrl: denied.recovery_url });
1448
- }
1449
- return null;
1450
- }
1451
- const data = await res.json().catch(() => null);
1452
- const token = typeof data?.token === "string" ? data.token.trim() : "";
1453
- if (sessionAtStart !== getCloudAccountSessionGeneration() || !getCloudSyncPref() || cloudPrefAtStart !== cloudSyncPrefGeneration || generationAtStart !== localCloudGeneration) return null;
1454
- if (token) {
1455
- localSyncDeviceMetadata.set(token, { token, deviceId: data.device_id, issuedAt: data.created_at || new Date().toISOString() });
1456
- localSyncDeviceTokenCache.set(cacheKey, token);
1457
- if (userId) localSyncDeviceTokenStore.set(binding, token);
1458
- }
1459
- return token || null;
1460
- })();
1461
- localSyncDeviceTokenInflight.set(cacheKey, { promise: issuePromise, sessionGeneration: sessionAtStart, generation: generationAtStart });
1462
- try {
1463
- const token = await issuePromise;
1464
- return options.returnSession ? localSyncDeviceMetadata.get(token) || null : token;
1465
- } finally {
1466
- if (localSyncDeviceTokenInflight.get(cacheKey)?.promise === issuePromise) localSyncDeviceTokenInflight.delete(cacheKey);
1467
- }
1468
- }
1469
-
1470
- // Why a *classified* fallback: the native popover keeps the last successful
1471
- // account (cross-device) snapshot on screen when a cloud read fails
1472
- // transiently, but must switch to this-machine data the moment the user signs
1473
- // out or turns cloud sync off. Both used to look identical from the outside
1474
- // (HTTP 200 + local payload + `X-TokenUsage-Account-View: 0`), so a single
1475
- // timed-out read silently shrank Activity to one device until the next
1476
- // manual sync. See ACCOUNT_FALLBACK_* below for the vocabulary.
1477
- const ACCOUNT_FALLBACK_CLOUD_SYNC_OFF = "cloud-sync-off";
1478
- const ACCOUNT_FALLBACK_SIGNED_OUT = "signed-out";
1479
-
1480
- // Every transient reason is prefixed "transient-"; clients only need the
1481
- // prefix, so new reasons can be added without a client change.
1482
- function classifyAccountFallback(err) {
1483
- if (!err) return "transient-error";
1484
- if (err.name === "AbortError" || err.name === "TimeoutError" || err.code === "auth_timeout") {
1485
- return "transient-timeout";
1486
- }
1487
- if (typeof err.code === "string" && err.code.startsWith("cloud_")) return err.code.replaceAll("_", "-");
1488
- if (err.code === "auth_rejected" || err.code === "auth_invalid" || err.status === 401 || err.status === 403) {
1489
- return "transient-auth";
1490
- }
1491
- if (err.code === "auth_network") return "transient-network";
1492
- if (Number.isFinite(err.status) && err.status > 0) return "transient-upstream";
1493
- return "transient-network";
1494
- }
1495
-
1496
- // Returns "served" when the cross-device aggregate was written to `res`,
1497
- // otherwise a fallback reason (see above) telling the caller — and, through
1498
- // the `X-TokenUsage-Account-Fallback` header, the popover — why the local
1499
- // single-machine data is being served instead.
1500
- async function tryServeAccountView(usageSlug, url, res) {
1501
- if (!getCloudSyncPref()) return ACCOUNT_FALLBACK_CLOUD_SYNC_OFF;
1502
- const refreshToken = getRefreshTokenForCloud();
1503
- if (!refreshToken) return ACCOUNT_FALLBACK_SIGNED_OUT;
1504
- invalidateAfterAccountUpload();
1505
- const runtime = getRuntime();
1506
- if (runtime.configurationError) return ACCOUNT_FALLBACK_SIGNED_OUT;
1507
- const failureKey = `${runtime.baseUrl}\0${refreshToken}`;
1508
- const sessionAtStart = getCloudAccountSessionGeneration();
1509
- const cloudPrefAtStart = cloudSyncPrefGeneration;
1510
- const params = new URLSearchParams(url.searchParams);
1511
- params.sort();
1512
- const requestKey = `${failureKey}\0${sessionAtStart}\0${cloudPrefAtStart}\0${accountUploadState}\0${usageSlug}\0${params}`;
1513
- if (url.searchParams.get("refresh") !== "1" && accountViewFailureUntil(failureKey) > Date.now()) {
1514
- return "transient-backoff";
1515
- }
1516
- const envTimeout = process.env.TOKENUSAGE_HTTP_TIMEOUT_MS
1517
- ? parseInt(process.env.TOKENUSAGE_HTTP_TIMEOUT_MS, 10)
1518
- : 0;
1519
- const timeoutMs = envTimeout > 0 ? envTimeout : 4000;
1520
- try {
1521
- // An identical overlapping read shares the entire probe, including its
1522
- // token mint. A healthy pending read must not look like a cloud outage
1523
- // and trigger the native client's account recovery loop.
1524
- let pending = accountViewInFlight.get(requestKey);
1525
- if (!pending) {
1526
- pending = fetchAccountUsage({
1527
- usageSlug,
1528
- searchParams: url.searchParams,
1529
- baseUrl: runtime.baseUrl || DEFAULT_BASE_URL,
1530
- anonKey: runtime.anonKey,
1531
- refreshToken,
1532
- timeoutMs,
1533
- onSessionRefreshed: (session) => {
1534
- getRuntime();
1535
- if (sessionAtStart !== getCloudAccountSessionGeneration()) {
1536
- throw Object.assign(new Error("Account session changed"), { code: "auth_session_changed" });
1537
- }
1538
- if (session.refreshToken) setRelayRefreshToken(session.refreshToken);
1539
- if (session.csrfToken) setRelayCsrfToken(session.csrfToken);
1540
- },
1541
- });
1542
- accountViewInFlight.set(requestKey, pending);
1543
- const cleanup = () => {
1544
- if (accountViewInFlight.get(requestKey) === pending) accountViewInFlight.delete(requestKey);
1545
- };
1546
- pending.then(cleanup, cleanup);
1547
- }
1548
- const out = await pending;
1549
- getRuntime();
1550
- // `null` here means the slug has no cloud equivalent, or the session
1551
- // vanished mid-request — a routing/session fact, not an outage.
1552
- if (!out) return ACCOUNT_FALLBACK_SIGNED_OUT;
1553
- if (sessionAtStart !== getCloudAccountSessionGeneration()) {
1554
- throw Object.assign(new Error("Account session changed"), { code: "auth_session_changed" });
1555
- }
1556
- if (out.data == null) {
1557
- rememberAccountViewFailure(failureKey);
1558
- return "transient-upstream";
1559
- }
1560
- if (!getCloudSyncPref()) return ACCOUNT_FALLBACK_CLOUD_SYNC_OFF;
1561
- if (cloudPrefAtStart !== cloudSyncPrefGeneration) {
1562
- json(res, { error: "Cloud sync preference changed", code: "CLOUD_SYNC_CHANGED" }, 409);
1563
- return "served";
1564
- }
1565
- accountViewFailures.delete(failureKey);
1566
- res.writeHead(200, {
1567
- "Content-Type": "application/json",
1568
- "Cache-Control": "no-store",
1569
- "X-TokenUsage-Account-View": "1",
1570
- });
1571
- res.end(JSON.stringify(out.data));
1572
- return "served";
1573
- } catch (e) {
1574
- const currentSession = getCloudAccountSessionGeneration();
1575
- if (e.code === "auth_session_changed" ||
1576
- (sessionAtStart !== currentSession && e.invalidatedSessionGeneration !== currentSession)) {
1577
- json(res, { error: "Account session changed", code: "auth_session_changed" }, 409);
1578
- return "served";
1579
- }
1580
- if (!getCloudSyncPref()) return ACCOUNT_FALLBACK_CLOUD_SYNC_OFF;
1581
- if (cloudPrefAtStart !== cloudSyncPrefGeneration) {
1582
- json(res, { error: "Cloud sync preference changed", code: "CLOUD_SYNC_CHANGED" }, 409);
1583
- return "served";
1584
- }
1585
- // Signed in + cloud sync on, but the cloud read failed (offline, token
1586
- // rejected, edge error, or timeout). Serve local data rather than
1587
- // erroring, but say so: this is a *temporary* downgrade and the client
1588
- // must not treat it as the user's real data scope.
1589
- const reason = classifyAccountFallback(e);
1590
- if (getRuntime().debug) {
1591
- console.warn(`[LocalAPI] account view fallback (${reason}) for ${usageSlug}:`, e?.message || e);
1592
- }
1593
- rememberAccountViewFailure(failureKey);
1594
- return reason;
1595
- }
1596
- }
1597
-
1598
- function normalizeCookieHeader(value) {
1599
- if (Array.isArray(value)) return value.filter(Boolean).join("; ");
1600
- return typeof value === "string" ? value : "";
1601
- }
1602
-
1603
- function buildRelayCookieHeader(clientCookieHeader, { relayPrecedenceNames = [] } = {}) {
1604
- const normalizedClientCookieHeader = normalizeCookieHeader(clientCookieHeader);
1605
- if (relayCookies.size === 0) return normalizedClientCookieHeader;
1606
- const relayPrecedence = new Set(relayPrecedenceNames);
1607
- const clientPairs = new Map();
1608
- if (normalizedClientCookieHeader) {
1609
- for (const part of normalizedClientCookieHeader.split(";")) {
1610
- const eqIdx = part.indexOf("=");
1611
- if (eqIdx < 1) continue;
1612
- const n = part.substring(0, eqIdx).trim();
1613
- if (n) clientPairs.set(n, part.trim());
1614
- }
1615
- }
1616
- // Merge relay cookies. Normal requests keep client precedence; refresh
1617
- // recovery can opt relay cookies into precedence over stale WebView cookies.
1618
- for (const [name, raw] of relayCookies) {
1619
- if (clientPairs.has(name) && !relayPrecedence.has(name)) continue;
1620
- const scIdx = raw.indexOf(";");
1621
- const pair = scIdx > 0 ? raw.substring(0, scIdx).trim() : raw;
1622
- clientPairs.set(name, pair);
1623
- }
1624
- return [...clientPairs.values()].join("; ");
1625
- }
1626
-
1627
- // Ephemeral auth bridge: WebView sets a "native" flag before opening system browser
1628
- // for OAuth. The callback page (in browser) checks this flag to decide whether to
1629
- // relay the code back to the app or handle it as a normal web flow.
1630
- let _nativeAuthPending = false;
1631
- let _nativeAuthExpiry = 0;
1632
-
1633
- function isAuthorizedLocalMutation(req) {
1634
- const headerToken = req?.headers?.["x-tokenusage-local-auth"];
1635
- const cookieToken = parseCookieHeader(req?.headers?.cookie).get("tokenusage_local_auth");
1636
- const token = typeof headerToken === "string" && headerToken.trim()
1637
- ? headerToken.trim()
1638
- : cookieToken || "";
1639
- if (!token || token !== localAuthToken) return false;
1640
- return hasAllowedLoopbackOrigin(req?.headers || {});
1641
- }
1642
-
1643
- return async function handleLocalApi(req, res, url) {
1644
- const p = url.pathname;
1645
- // Session recovery and the one-time native callback marker belong to local
1646
- // clients. Reject foreign requests before consuming either, including preflights.
1647
- const nativeMarkerRead = p === "/api/auth-bridge/verifier" && String(req.method).toUpperCase() === "GET";
1648
- if (p.startsWith("/api/auth/") || nativeMarkerRead) {
1649
- if (!hasAllowedLoopbackOrigin(req.headers || {}) || req.headers?.["sec-fetch-site"] === "cross-site") {
1650
- json(res, { code: "untrusted_auth_origin", error: "Authentication requests must come from the local dashboard." }, 403);
1651
- return true;
1652
- }
1653
- if (String(req.method).toUpperCase() === "OPTIONS") {
1654
- res.writeHead(204, {
1655
- "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
1656
- "Access-Control-Allow-Headers": "Content-Type, Authorization",
1657
- });
1658
- res.end();
1659
- return true;
1660
- }
1661
- }
1662
- const selectedRuntime = getRuntime();
1663
- const callerInstance = req.headers?.["x-tokenusage-instance"];
1664
- if (callerInstance && normalizeInstanceBaseUrl(callerInstance) !== selectedRuntime.baseUrl) {
1665
- json(res, { code: "instance_changed", error: "The configured backend instance changed. Reload and sign in again." }, 409);
1666
- return true;
1667
- }
1668
- const bearer = parseBearerToken(req.headers?.authorization);
1669
- const issuedFor = bearer && issuedAccessTokenInstances.get(crypto.createHash("sha256").update(bearer).digest("hex"));
1670
- if (issuedFor && issuedFor !== fingerprintFor(selectedRuntime)) {
1671
- json(res, { code: "instance_changed", error: "This session belongs to another backend instance. Sign in again." }, 409);
1672
- return true;
1673
- }
1674
- if (p === "/api/runtime-config.js") {
1675
- if (String(req.method).toUpperCase() !== "GET") { json(res, { error: "Method Not Allowed" }, 405); return true; }
1676
- const descriptor = { baseUrl: normalizeInstanceBaseUrl(selectedRuntime.baseUrl) || "",
1677
- anonKey: selectedRuntime.configurationError ? null : selectedRuntime.anonKey,
1678
- dashboardUrl: normalizeInstanceBaseUrl(selectedRuntime.dashboardUrl) || "", configurationError: selectedRuntime.configurationError };
1679
- const value = JSON.stringify(descriptor).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
1680
- res.writeHead(200, { "Content-Type": "application/javascript; charset=utf-8", "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" });
1681
- res.end(`window.__TOKENUSAGE_RUNTIME_CONFIG__=${value};`);
1682
- return true;
1683
- }
1684
-
1685
- if (p === "/api/local-auth") {
1686
- if (String(req.method || "GET").toUpperCase() !== "GET") {
1687
- json(res, { error: "Method Not Allowed" }, 405);
1688
- return true;
1689
- }
1690
- res.writeHead(200, {
1691
- "Content-Type": "application/json",
1692
- "Cache-Control": "no-store",
1693
- });
1694
- res.end(JSON.stringify({
1695
- token: localAuthToken,
1696
- ...(normalizedServerVersion ? { serverVersion: normalizedServerVersion } : {}),
1697
- }));
1698
- return true;
1699
- }
1700
-
1701
- // --- local Codex-compatible pet assets and package import ---
1702
- const localPetAssetMatch = p.match(/^\/api\/pets\/local\/([a-z0-9-]+)\/spritesheet\.webp$/);
1703
- if (localPetAssetMatch) {
1704
- const method = String(req.method || "GET").toUpperCase();
1705
- if (method !== "GET" && method !== "HEAD") {
1706
- json(res, { error: "Method Not Allowed" }, 405);
1707
- return true;
1708
- }
1709
- const pet = require("./pet-packages").resolvePetAsset(localPetAssetMatch[1]);
1710
- if (!pet) {
1711
- json(res, { error: "Pet not found" }, 404);
1712
- return true;
1713
- }
1714
- const stat = fs.statSync(pet.spritesheetPath);
1715
- res.writeHead(200, {
1716
- "Content-Type": "image/webp",
1717
- "Content-Length": stat.size,
1718
- "Cache-Control": "no-cache",
1719
- "X-Content-Type-Options": "nosniff",
1720
- });
1721
- if (method === "HEAD") res.end();
1722
- else fs.createReadStream(pet.spritesheetPath).pipe(res);
1723
- return true;
1724
- }
1725
-
1726
- // Preview asset for a Codex pet that hasn't been imported yet (served from
1727
- // ~/.codex/pets or straight out of the Codex.app bundle).
1728
- const codexPetAssetMatch = p.match(/^\/api\/pets\/codex\/([a-z0-9-]+)\/spritesheet\.webp$/);
1729
- if (codexPetAssetMatch) {
1730
- const method = String(req.method || "GET").toUpperCase();
1731
- if (method !== "GET" && method !== "HEAD") {
1732
- json(res, { error: "Method Not Allowed" }, 405);
1733
- return true;
1734
- }
1735
- const asset = require("./pet-packages").readCodexImportableAsset(codexPetAssetMatch[1]);
1736
- if (!asset) {
1737
- json(res, { error: "Pet not found" }, 404);
1738
- return true;
1739
- }
1740
- res.writeHead(200, {
1741
- "Content-Type": "image/webp",
1742
- "Content-Length": asset.buffer.length,
1743
- "Cache-Control": "no-cache",
1744
- "X-Content-Type-Options": "nosniff",
1745
- });
1746
- if (method === "HEAD") res.end();
1747
- else res.end(asset.buffer);
1748
- return true;
1749
- }
1750
-
1751
- if (p === "/api/pets/import") {
1752
- if (String(req.method || "GET").toUpperCase() !== "POST") {
1753
- json(res, { ok: false, error: "Method Not Allowed" }, 405);
1754
- return true;
1755
- }
1756
- if (!isAuthorizedLocalMutation(req)) {
1757
- json(res, { ok: false, error: "Unauthorized" }, 401);
1758
- return true;
1759
- }
1760
- const pets = require("./pet-packages");
1761
- try {
1762
- const body = await readBodyLimited(req, pets.MAX_PACKAGE_BYTES);
1763
- json(res, { ok: true, pet: await pets.importPetZip(body) });
1764
- } catch (error) {
1765
- if (!res.headersSent) json(res, { ok: false, error: error?.message || "Pet import failed" }, 400);
1766
- }
1767
- return true;
1768
- }
1769
-
1770
- // --- Auth bridge: native OAuth flag (WebView ↔ system browser) ---
1771
- if (p === "/api/auth-bridge/verifier") {
1772
- const method = String(req.method || "GET").toUpperCase();
1773
- if (method === "PUT" || method === "POST") {
1774
- if (!isAuthorizedLocalMutation(req)) {
1775
- json(res, { error: "Unauthorized" }, 401);
1776
- return true;
1777
- }
1778
- const body = await readJsonBody(req);
1779
- _nativeAuthPending = Boolean(body?.native);
1780
- _nativeAuthExpiry = Date.now() + 5 * 60 * 1000; // 5 min TTL
1781
- json(res, { ok: true });
1782
- return true;
1783
- }
1784
- if (method === "GET") {
1785
- const isNative = _nativeAuthPending && Date.now() < _nativeAuthExpiry;
1786
- _nativeAuthPending = false; // one-time read
1787
- _nativeAuthExpiry = 0;
1788
- json(res, { native: isNative });
1789
- return true;
1790
- }
1791
- json(res, { error: "Method Not Allowed" }, 405);
1792
- return true;
1793
- }
1794
-
1795
- // --- auth proxy: forward /api/auth/* to InsForge cloud ---
1796
- if (p.startsWith("/api/auth/")) {
1797
- if (String(req.method).toUpperCase() === "POST" && /^\/api\/auth\/(?:logout|sessions|sign-in|oauth\/exchange)$/.test(p)) {
1798
- clearLocalSyncDeviceTokenCache();
1799
- invalidateCloudAccountPayloadCache({ sessionChanged: true });
1800
- }
1801
- const authSessionAtStart = getCloudAccountSessionGeneration();
1802
- const runtime = getRuntime();
1803
- try { assertInsforgeRuntime(runtime); } catch (error) { json(res, { code: error.code, error: error.message }, 503); return true; }
1804
- const insforgeBase = runtime.baseUrl || DEFAULT_BASE_URL;
1805
- if (req.headers.apikey && req.headers.apikey !== runtime.anonKey) {
1806
- json(res, { code: "instance_changed", error: "The public client key belongs to another backend instance." }, 409);
1807
- return true;
1808
- }
1809
- try {
1810
- const targetUrl = `${insforgeBase.replace(/\/$/, "")}${p}${url.search || ""}`;
1811
- const proxyHeaders = buildProxyHeaders(req.headers);
1812
- // Browser cookies survive changes to the loopback server's backend.
1813
- // Only this instance's captured cookies can be relayed, including when
1814
- // switching from a custom instance back to the official service.
1815
- const hadBrowserCookies = Boolean(normalizeCookieHeader(proxyHeaders.cookie).trim());
1816
- proxyHeaders.cookie = hadBrowserCookies ? buildRelayCookieHeader("") : "";
1817
- if (proxyHeaders["x-csrf-token"] !== getRelayCookieValue(csrfRelayCookieName)) delete proxyHeaders["x-csrf-token"];
1818
- if (insforgeBase !== DEFAULT_BASE_URL && proxyHeaders.authorization && !callerInstance) {
1819
- json(res, { code: "instance_changed", error: "Reload the local dashboard before using a custom backend session." }, 409);
1820
- return true;
1821
- }
1822
- proxyHeaders.apikey = runtime.anonKey;
1823
- const hasClientCookie = normalizeCookieHeader(proxyHeaders["cookie"]).trim().length > 0;
1824
- const hasCsrfHeader = typeof proxyHeaders["x-csrf-token"] === "string" && proxyHeaders["x-csrf-token"].trim().length > 0;
1825
- const relayCsrfToken = getRelayCookieValue(csrfRelayCookieName);
1826
- const relayRefreshToken = getRelayCookieValue("insforge_refresh_token", { decode: true });
1827
- // A cookie-less client (fresh WebView after an app update/restart) has no
1828
- // browser session to pair a CSRF token with — the persisted refresh token
1829
- // replayed through the mobile flow is the only viable recovery. The relay
1830
- // csrf token must NOT force the cookie/csrf path here: background mobile
1831
- // rotations (cloud-account.js) can leave it stale, and a stale csrf turns
1832
- // recovery into 403 Invalid CSRF and signs the user out.
1833
- const shouldUseRelayRefreshFallback =
1834
- p === "/api/auth/refresh" && !hasClientCookie && relayRefreshToken;
1835
- if (p === "/api/auth/refresh" && relayCsrfToken && !shouldUseRelayRefreshFallback) {
1836
- proxyHeaders["x-csrf-token"] = relayCsrfToken;
1837
- }
1838
- const hasEffectiveCsrfHeader =
1839
- hasCsrfHeader ||
1840
- (typeof proxyHeaders["x-csrf-token"] === "string" && proxyHeaders["x-csrf-token"].trim().length > 0);
1841
- let shouldInjectRelayCookies =
1842
- p !== "/api/auth/refresh" || hasClientCookie || hasEffectiveCsrfHeader;
1843
- if (shouldUseRelayRefreshFallback) {
1844
- shouldInjectRelayCookies = false;
1845
- }
1846
-
1847
- // Inject relay cookies so WebView benefits from browser's login session.
1848
- // Refresh requests need either a browser cookie or an explicit CSRF token;
1849
- // otherwise replaying a stale persisted refresh cookie just manufactures
1850
- // Invalid CSRF errors on startup.
1851
- const originalCookieHeader = normalizeCookieHeader(proxyHeaders["cookie"]);
1852
- const mergedCookie = shouldInjectRelayCookies
1853
- ? buildRelayCookieHeader(originalCookieHeader, {
1854
- relayPrecedenceNames: p === "/api/auth/refresh"
1855
- ? [csrfRelayCookieName, "insforge_refresh_token"]
1856
- : [],
1857
- })
1858
- : originalCookieHeader;
1859
- const injectedRelayCookies =
1860
- shouldInjectRelayCookies && relayCookies.size > 0 && mergedCookie !== originalCookieHeader;
1861
- if (mergedCookie) proxyHeaders["cookie"] = mergedCookie;
1862
- else delete proxyHeaders["cookie"];
1863
-
1864
- const bodyChunks = [];
1865
- for await (const chunk of req) bodyChunks.push(chunk);
1866
- let proxyBody = bodyChunks.length > 0 ? Buffer.concat(bodyChunks) : undefined;
1867
- let effectiveTargetUrl = targetUrl;
1868
- if (shouldUseRelayRefreshFallback) {
1869
- effectiveTargetUrl = `${insforgeBase.replace(/\/$/, "")}/api/auth/refresh?client_type=mobile`;
1870
- proxyHeaders["content-type"] = "application/json";
1871
- delete proxyHeaders["content-length"];
1872
- proxyBody = Buffer.from(JSON.stringify({ refresh_token: relayRefreshToken }), "utf8");
1873
- }
1874
- let proxyRes = await fetch(effectiveTargetUrl, {
1875
- method: req.method || "GET",
1876
- headers: proxyHeaders,
1877
- body: proxyBody,
1878
- credentials: "include",
1879
- redirect: "manual",
1880
- });
1881
- let resBody = Buffer.from(await proxyRes.arrayBuffer());
1882
-
1883
- // Stale-CSRF rescue: 403 Invalid CSRF on refresh does NOT mean the
1884
- // session is dead — background mobile rotations (cloud-account.js) can
1885
- // desync the relayed csrf from a still-valid refresh token. Replay the
1886
- // persisted refresh token through the csrf-free mobile flow before
1887
- // letting the client sign out.
1888
- const isStaleCsrf403 =
1889
- p === "/api/auth/refresh"
1890
- && proxyRes.status === 403
1891
- && /invalid csrf token/i.test(resBody.toString("utf8"));
1892
- if (isStaleCsrf403 && relayRefreshToken && !shouldUseRelayRefreshFallback) {
1893
- const rescueHeaders = { ...proxyHeaders, "content-type": "application/json" };
1894
- delete rescueHeaders["cookie"];
1895
- delete rescueHeaders["x-csrf-token"];
1896
- delete rescueHeaders["content-length"];
1897
- const rescueRes = await fetch(
1898
- `${insforgeBase.replace(/\/$/, "")}/api/auth/refresh?client_type=mobile`,
1899
- {
1900
- method: "POST",
1901
- headers: rescueHeaders,
1902
- body: JSON.stringify({ refresh_token: relayRefreshToken }),
1903
- credentials: "include",
1904
- redirect: "manual",
1905
- },
1906
- );
1907
- if (rescueRes.ok) {
1908
- proxyRes = rescueRes;
1909
- resBody = Buffer.from(await rescueRes.arrayBuffer());
1910
- }
1911
- }
1912
-
1913
- const latestRuntime = getRuntime();
1914
- if (latestRuntime.configurationError || latestRuntime.baseUrl !== runtime.baseUrl || latestRuntime.anonKey !== runtime.anonKey) {
1915
- json(res, { code: "instance_changed", error: "The configured backend changed during authentication. Reload and sign in again." }, 409);
1916
- return true;
1917
- }
1918
- // Error responses must not mutate relay state: a 403's deletion
1919
- // set-cookie (insforge_refresh_token=; Expires=1970) would otherwise
1920
- // destroy a still-valid persisted session.
1921
- if ((p === "/api/auth/refresh" || p === "/api/auth/logout") &&
1922
- authSessionAtStart !== getCloudAccountSessionGeneration()) {
1923
- json(res, { error: "Account session changed", code: "auth_session_changed" }, 409);
1924
- return true;
1925
- }
1926
- const allowRelayCapture = proxyRes.status < 400;
1927
- const responseHeaders = [...proxyRes.headers.entries()]
1928
- .filter(([k]) => !["transfer-encoding", "connection"].includes(k.toLowerCase()))
1929
- .map(([k, v]) => {
1930
- if (k.toLowerCase() === "set-cookie") {
1931
- const rewritten = v.replace(/;\s*[Dd]omain=[^;]*/g, "; Domain=localhost");
1932
- if (allowRelayCapture) captureSetCookies(rewritten);
1933
- return [k, rewritten];
1934
- }
1935
- return [k, v];
1936
- });
1937
- res.writeHead(proxyRes.status, Object.fromEntries(responseHeaders));
1938
- if (proxyRes.status >= 200 && proxyRes.status < 300) {
1939
- if (p === "/api/auth/logout") {
1940
- clearRelayCookies("sign out");
1941
- } else {
1942
- captureAuthTokensFromBody(resBody, proxyRes.headers.get("content-type"), {
1943
- newLogin: p !== "/api/auth/refresh", sourceFingerprint: fingerprintFor(runtime),
1944
- });
1945
- }
1946
- }
1947
- if (
1948
- isStaleCsrf403
1949
- && proxyRes.status === 403
1950
- && injectedRelayCookies
1951
- && !hasClientCookie
1952
- ) {
1953
- clearRelayCookies("stale refresh cookie without local CSRF context");
1954
- }
1955
- res.end(resBody);
1956
- } catch (e) {
1957
- json(res, { error: `Auth proxy error: ${e?.message || e}` }, 502);
1958
- }
1959
- return true;
1960
- }
1961
-
1962
- // --- ip-check proxy: reverse-proxy ip.net.coffee (issue #81) ---
1963
- // Lock-down: GET/HEAD only, restricted path prefixes, do not forward
1964
- // browser credentials or fingerprintable headers. Without these limits
1965
- // /proxy/ipcheck is an open reverse-proxy any local process can abuse
1966
- // (exfiltrate dashboard cookies, anonymously POST through user IP).
1967
- if (p.startsWith(`${IP_CHECK_PROXY_PREFIX}/`) || p === IP_CHECK_PROXY_PREFIX) {
1968
- const method = String(req.method || "GET").toUpperCase();
1969
- if (method !== "GET" && method !== "HEAD") {
1970
- json(res, { error: "Method Not Allowed" }, 405);
1971
- return true;
1972
- }
1973
- const targetPath = p === IP_CHECK_PROXY_PREFIX
1974
- ? "/"
1975
- : p.slice(IP_CHECK_PROXY_PREFIX.length) || "/";
1976
- const ALLOWED_PREFIXES = [
1977
- "/api/geoip/",
1978
- "/api/geoip-batch",
1979
- "/api/iprisk/",
1980
- "/api/dns/result/",
1981
- "/claude/status.json",
1982
- "/favicons/",
1983
- "/ip/",
1984
- ];
1985
- if (!ALLOWED_PREFIXES.some((prefix) => targetPath.startsWith(prefix))) {
1986
- json(res, { error: "Path not allowed" }, 403);
1987
- return true;
1988
- }
1989
- const targetUrl = `${IP_CHECK_TARGET}${targetPath}${url.search || ""}`;
1990
- try {
1991
- // Whitelist forwarded headers — no cookies, no auth, no fingerprintable
1992
- // identity. Only what the upstream needs to negotiate content. Do not
1993
- // set `host` explicitly: undici derives it from the URL, and some
1994
- // versions reject a manual host header on fetch() (same forbidden-
1995
- // header family that broke /api/auth/* in 5/13).
1996
- const proxyHeaders = {
1997
- accept: req.headers["accept"] || "*/*",
1998
- "accept-language": req.headers["accept-language"] || "en",
1999
- "accept-encoding": req.headers["accept-encoding"] || "gzip",
2000
- "user-agent": "tokenusage-cli/IPCheck (https://www.tokenusage.cc)",
2001
- referer: `${IP_CHECK_TARGET}${targetPath}`,
2002
- };
2003
-
2004
- const proxyRes = await fetch(targetUrl, {
2005
- method,
2006
- headers: proxyHeaders,
2007
- redirect: "manual",
2008
- });
2009
-
2010
- const stripped = new Set([
2011
- "transfer-encoding",
2012
- "connection",
2013
- "content-length",
2014
- "content-encoding",
2015
- "x-frame-options",
2016
- "content-security-policy",
2017
- "cross-origin-opener-policy",
2018
- "cross-origin-embedder-policy",
2019
- "cross-origin-resource-policy",
2020
- ]);
2021
- const responseHeaders = [...proxyRes.headers.entries()].filter(
2022
- ([k]) => !stripped.has(k.toLowerCase()),
2023
- );
2024
-
2025
- const resBody = Buffer.from(await proxyRes.arrayBuffer());
2026
- res.writeHead(proxyRes.status, Object.fromEntries(responseHeaders));
2027
- res.end(resBody);
2028
- } catch (e) {
2029
- json(res, { error: `IP check proxy error: ${e?.message || e}` }, 502);
2030
- }
2031
- return true;
2032
- }
2033
-
2034
- // --- avatar proxy: fetch third-party avatars server-side ---
2035
- // Why: WKWebView in Token UsageBar fails to load some users' Google /
2036
- // GitHub avatars directly (network-stack / proxy / TLS quirks vary by
2037
- // environment), even when the same URL renders fine in Safari. Proxying
2038
- // through Node's fetch — which honors system proxy + cookies-of-none —
2039
- // produces a same-origin <img> the WKWebView always accepts.
2040
- // Lock-down: GET only; host allowlist of well-known avatar CDNs (no open
2041
- // proxy); strip cookies/auth; small in-memory cache.
2042
- if (p === "/api/avatar-proxy") {
2043
- const method = String(req.method || "GET").toUpperCase();
2044
- if (method !== "GET" && method !== "HEAD") {
2045
- json(res, { error: "Method Not Allowed" }, 405);
2046
- return true;
2047
- }
2048
- const target = url.searchParams.get("url");
2049
- if (!target) {
2050
- json(res, { error: "Missing url" }, 400);
2051
- return true;
2052
- }
2053
- let parsed;
2054
- try {
2055
- parsed = new URL(target);
2056
- } catch {
2057
- json(res, { error: "Invalid url" }, 400);
2058
- return true;
2059
- }
2060
- if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
2061
- json(res, { error: "Only http(s) allowed" }, 400);
2062
- return true;
2063
- }
2064
- const AVATAR_HOST_ALLOWLIST = [
2065
- "lh3.googleusercontent.com",
2066
- "lh4.googleusercontent.com",
2067
- "lh5.googleusercontent.com",
2068
- "lh6.googleusercontent.com",
2069
- "avatars.githubusercontent.com",
2070
- "secure.gravatar.com",
2071
- "www.gravatar.com",
2072
- "gravatar.com",
2073
- "cdn.discordapp.com",
2074
- "pbs.twimg.com",
2075
- "abs.twimg.com",
2076
- "api.dicebear.com",
2077
- ];
2078
- const hostOk = AVATAR_HOST_ALLOWLIST.some(
2079
- (h) => parsed.hostname === h || parsed.hostname.endsWith(`.${h}`),
2080
- );
2081
- if (!hostOk) {
2082
- json(res, { error: "Host not allowed" }, 403);
2083
- return true;
2084
- }
2085
-
2086
- const cacheKey = parsed.toString();
2087
- const now = Date.now();
2088
- const cached = avatarProxyCache.get(cacheKey);
2089
- if (cached && now - cached.fetchedAt < AVATAR_PROXY_TTL_MS) {
2090
- res.writeHead(200, {
2091
- "Content-Type": cached.contentType,
2092
- "Cache-Control": "public, max-age=3600",
2093
- "X-Avatar-Cache": "HIT",
2094
- });
2095
- res.end(method === "HEAD" ? undefined : cached.body);
2096
- return true;
2097
- }
2098
-
2099
- try {
2100
- const upstream = await fetch(cacheKey, {
2101
- method,
2102
- redirect: "follow",
2103
- headers: {
2104
- accept: req.headers["accept"] || "image/*",
2105
- "accept-language": req.headers["accept-language"] || "en",
2106
- "user-agent": "tokenusage-cli/AvatarProxy (https://www.tokenusage.cc)",
2107
- },
2108
- });
2109
- if (!upstream.ok) {
2110
- json(res, { error: `Upstream ${upstream.status}` }, upstream.status);
2111
- return true;
2112
- }
2113
- const contentType = upstream.headers.get("content-type") || "image/png";
2114
- if (!contentType.toLowerCase().startsWith("image/")) {
2115
- json(res, { error: "Not an image" }, 415);
2116
- return true;
2117
- }
2118
- const body = Buffer.from(await upstream.arrayBuffer());
2119
- if (body.length <= AVATAR_PROXY_MAX_BYTES) {
2120
- // Simple LRU: drop oldest if over capacity.
2121
- if (avatarProxyCache.size >= AVATAR_PROXY_MAX_ENTRIES) {
2122
- const oldestKey = avatarProxyCache.keys().next().value;
2123
- if (oldestKey) avatarProxyCache.delete(oldestKey);
2124
- }
2125
- avatarProxyCache.set(cacheKey, { body, contentType, fetchedAt: now });
2126
- }
2127
- res.writeHead(200, {
2128
- "Content-Type": contentType,
2129
- "Cache-Control": "public, max-age=3600",
2130
- "X-Avatar-Cache": "MISS",
2131
- });
2132
- res.end(method === "HEAD" ? undefined : body);
2133
- } catch (e) {
2134
- json(res, { error: `Avatar proxy error: ${e?.message || e}` }, 502);
2135
- }
2136
- return true;
2137
- }
2138
-
2139
- // The browser supplies its current JWT only to prove ownership upstream.
2140
- // Uploads use this server-held binding, never a caller's opaque device token.
2141
- if (p === "/api/functions/tokenusage-cloud-session") {
2142
- if (String(req.method).toUpperCase() !== "POST") { json(res, { error: "Method Not Allowed" }, 405); return true; }
2143
- if (!isAuthorizedLocalMutation(req)) { json(res, { error: "Unauthorized" }, 401); return true; }
2144
- try {
2145
- const body = await readJsonBody(req);
2146
- assertInsforgeRuntime(getRuntime());
2147
- if (body.clear === true) {
2148
- if (body.session_id && body.session_id === localCloudSession?.id) clearLocalSyncDeviceTokenCache();
2149
- json(res, { ok: true }); return true;
2150
- }
2151
- if (!getCloudSyncPref()) { json(res, { code: "CLOUD_SYNC_DISABLED" }, 409); return true; }
2152
- const accessToken = String(req.headers.authorization || "").replace(/^Bearer /i, "").trim();
2153
- const ownerId = typeof body.expectedOwnerId === "string" ? body.expectedOwnerId.trim() : "";
2154
- if (!accessToken || !ownerId) { json(res, { code: "auth_required" }, 401); return true; }
2155
- const baseUrl = resolveAllowedInsforgeBaseUrl(body.insforgeBaseUrl || getRuntime().baseUrl, getRuntime());
2156
- if (!baseUrl) { json(res, { error: "Unsupported insforgeBaseUrl override" }, 400); return true; }
2157
- clearLocalSyncDeviceTokenCache();
2158
- const generation = localCloudGeneration;
2159
- await Promise.all([...activeCloudSyncs].map(controller => controller.done).filter(Boolean));
2160
- const issued = await issueDeviceTokenForLocalSync(qp, { baseUrl, accessToken, expectedOwnerId: ownerId, returnSession: true });
2161
- if (generation !== localCloudGeneration) { json(res, { code: "auth_session_changed" }, 409); return true; }
2162
- if (!issued?.token || !issued.deviceId) { json(res, { code: "auth_rejected" }, 401); return true; }
2163
- localCloudSession = { id: crypto.randomUUID(), ownerId, baseUrl, generation, token: issued.token };
2164
- json(res, { session_id: localCloudSession.id, device_id: issued.deviceId, created_at: issued.issuedAt });
2165
- } catch (error) { json(res, { error: error.message, code: error.code || null, recovery_url: error.recoveryUrl || null }, error.status || 502); }
2166
- return true;
2167
- }
2168
-
2169
- if (p === "/api/functions/tokenusage-cloud-usage-reset") {
2170
- if (!isAuthorizedLocalMutation(req) || req.headers["sec-fetch-site"] === "cross-site") { json(res, { error: "Unauthorized" }, 401); return true; }
2171
- try {
2172
- if (String(req.method).toUpperCase() === "GET") {
2173
- json(res, { pending: Boolean(readCloudUsageReset(trackerDataDir)) }); return true;
2174
- }
2175
- if (String(req.method).toUpperCase() !== "POST") { json(res, { error: "Method Not Allowed" }, 405); return true; }
2176
- const body = await readJsonBody(req);
2177
- const session = localCloudSession;
2178
- if (!getCloudSyncPref() || !session || body.cloudSessionId !== session.id || body.expectedOwnerId !== session.ownerId) {
2179
- json(res, { code: "auth_session_changed" }, 409); return true;
2180
- }
2181
- const metadata = localSyncDeviceMetadata.get(session.token);
2182
- const accessToken = String(req.headers.authorization || "").replace(/^Bearer /i, "").trim();
2183
- const runtime = assertInsforgeRuntime(getRuntime());
2184
- let claims;
2185
- try { claims = JSON.parse(Buffer.from(accessToken.split(".")[1], "base64url").toString("utf8")); } catch { /* rejected below */ }
2186
- if (claims?.sub !== session.ownerId || !Number.isFinite(claims?.exp) || claims.exp * 1000 <= Date.now()) {
2187
- json(res, { code: "auth_required" }, 401); return true;
2188
- }
2189
- const isCurrent = () => getCloudSyncPref() && localCloudSession === session && session.generation === localCloudGeneration &&
2190
- claims.exp * 1000 > Date.now() && getRuntime().baseUrl === session.baseUrl;
2191
- const result = await resetCloudDeviceUsage({ trackerDir: trackerDataDir, ownerId: session.ownerId,
2192
- deviceId: metadata?.deviceId, baseUrl: session.baseUrl, anonKey: runtime.anonKey, accessToken, isCurrent });
2193
- if (!isCurrent()) { json(res, { code: "auth_session_changed" }, 409); return true; }
2194
- clearLocalSyncDeviceTokenCache();
2195
- invalidateAfterAccountUpload();
2196
- invalidateCloudAccountPayloadCache();
2197
- json(res, { ...result, pending: true });
2198
- } catch (error) { json(res, { error: error.code || "cloud_usage_reset_failed", code: error.code || null }, error.status || 502); }
2199
- return true;
2200
- }
2201
-
2202
- // --- local-sync (POST) ---
2203
- if (p === "/api/functions/tokenusage-local-sync") {
2204
- if (String(req.method || "GET").toUpperCase() !== "POST") {
2205
- json(res, { ok: false, error: "Method Not Allowed" }, 405);
2206
- return true;
2207
- }
2208
- if (!isAuthorizedLocalMutation(req)) {
2209
- json(res, { ok: false, error: "Unauthorized" }, 401);
2210
- return true;
2211
- }
2212
- try {
2213
- let body = {};
2214
- try {
2215
- body = await readJsonBody(req);
2216
- } catch {
2217
- body = {};
2218
- }
2219
- const currentRuntime = getRuntime();
2220
- if (body.drain === true || body.cloudSessionId || body.insforgeBaseUrl || body.expectedOwnerId) assertInsforgeRuntime(currentRuntime);
2221
- const extraEnv = { TOKENUSAGE_CLOUD_RECHECK: "0", TOKENUSAGE_INSFORGE_BASE_URL: currentRuntime.baseUrl, TOKENUSAGE_INSFORGE_ANON_KEY: currentRuntime.anonKey || "" };
2222
- const drain = body.drain === true;
2223
- // A user action after membership/resume may probe the server again.
2224
- // The server still enforces membership, paused slots and its deadline.
2225
- if (drain && body.recheckCloudAccess === true && body.auto !== true) {
2226
- extraEnv.TOKENUSAGE_CLOUD_RECHECK = "1";
2227
- }
2228
- // Native Sync Now combines an incremental background scan with a full
2229
- // cloud drain. A plain {drain:true} request remains an exhaustive sync.
2230
- const auto = body.auto === true;
2231
- const background =
2232
- auto && (body.background === true || body.lightweight === true);
2233
- // The local server is the trust boundary for cloud-sync preferences.
2234
- // A persisted device token must not let a native background request
2235
- // upload after the user has explicitly disabled cloud sync.
2236
- const publishAccount =
2237
- background && body.publishAccount === true && getCloudSyncPref();
2238
- const allLocalSources = background && body.allLocalSources === true;
2239
- if (background && body.nativeOnlyWsl === true) {
2240
- extraEnv.TOKENUSAGE_WSL_MODE = "native-only";
2241
- }
2242
- if (body.deviceToken != null && !background) {
2243
- json(res, { ok: false, error: "Use the current cloud session", code: getCloudSyncPref() ? "auth_session_required" : "CLOUD_SYNC_DISABLED" }, 409);
2244
- return true;
2245
- }
2246
- const browserSession = body.cloudSessionId ? localCloudSession : null;
2247
- if (body.cloudSessionId === "" || (body.expectedOwnerId && !body.cloudSessionId)) { json(res, { ok: false, code: "auth_session_required" }, 409); return true; }
2248
- if (body.cloudSessionId && (!browserSession || body.cloudSessionId !== browserSession.id ||
2249
- body.expectedOwnerId !== browserSession.ownerId || browserSession.generation !== localCloudGeneration || !getCloudSyncPref())) {
2250
- json(res, { ok: false, code: "auth_session_changed" }, 409); return true;
2251
- }
2252
- if (browserSession) extraEnv.TOKENUSAGE_DEVICE_TOKEN = browserSession.token;
2253
- let localSyncBaseUrl = null;
2254
- if (body.insforgeBaseUrl != null) {
2255
- const allowedBaseUrl = resolveAllowedInsforgeBaseUrl(body.insforgeBaseUrl, getRuntime());
2256
- if (!allowedBaseUrl) {
2257
- json(res, { ok: false, error: "Unsupported insforgeBaseUrl override" }, 400);
2258
- return true;
2259
- }
2260
- extraEnv.TOKENUSAGE_INSFORGE_BASE_URL = allowedBaseUrl;
2261
- localSyncBaseUrl = allowedBaseUrl;
2262
- }
2263
- if (browserSession) {
2264
- extraEnv.TOKENUSAGE_INSFORGE_BASE_URL = browserSession.baseUrl;
2265
- localSyncBaseUrl = browserSession.baseUrl;
2266
- }
2267
- const uploadGeneration = localCloudGeneration;
2268
- if ((!background || publishAccount) &&
2269
- !extraEnv.TOKENUSAGE_DEVICE_TOKEN &&
2270
- getCloudSyncPref() &&
2271
- getRefreshTokenForCloud()) {
2272
- let issuedToken = null;
2273
- const issuerSessionAtStart = getCloudAccountSessionGeneration();
2274
- const issuerCloudPrefAtStart = cloudSyncPrefGeneration;
2275
- try {
2276
- issuedToken = await issueDeviceTokenForLocalSync(qp, { baseUrl: localSyncBaseUrl, expectedOwnerId: localCloudSession?.ownerId });
2277
- } catch (e) {
2278
- if (e?.code?.startsWith("cloud_") || e?.code === "auth_owner_mismatch") throw e;
2279
- if (getRuntime().debug) {
2280
- console.warn("[LocalAPI] local sync device token issue failed:", e?.message || e);
2281
- }
2282
- }
2283
- if (issuerSessionAtStart !== getCloudAccountSessionGeneration()) {
2284
- json(res, { ok: false, error: "Account session changed", code: "auth_session_changed" }, 409);
2285
- return true;
2286
- }
2287
- if (!getCloudSyncPref() || issuerCloudPrefAtStart !== cloudSyncPrefGeneration) {
2288
- json(res, { ok: false, error: "Cloud sync preference changed", code: "CLOUD_SYNC_CHANGED" }, 409);
2289
- return true;
2290
- }
2291
- if (!issuedToken) {
2292
- if (drain) {
2293
- json(res, { ok: false, error: "Unable to issue cloud device token for local sync" }, 502);
2294
- return true;
2295
- }
2296
- } else {
2297
- extraEnv.TOKENUSAGE_DEVICE_TOKEN = issuedToken;
2298
- }
2299
- }
2300
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN && uploadGeneration !== localCloudGeneration) {
2301
- json(res, { ok: false, code: "auth_session_changed" }, 409); return true;
2302
- }
2303
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN) {
2304
- const metadata = localSyncDeviceMetadata.get(extraEnv.TOKENUSAGE_DEVICE_TOKEN);
2305
- if (metadata?.deviceId) extraEnv.TOKENUSAGE_CLOUD_DEVICE_ID = metadata.deviceId;
2306
- }
2307
- await Promise.all([...activeCloudSyncs].filter(controller => controller.signal.aborted).map(controller => controller.done).filter(Boolean));
2308
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN && uploadGeneration !== localCloudGeneration) throw Object.assign(new Error("Account session changed"), { code: "auth_session_changed", status: 409 });
2309
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN && localSyncDeviceTokenUserId) {
2310
- const uploadBaseUrl = localSyncBaseUrl || getRuntime().baseUrl;
2311
- const reset = readCloudUsageReset(trackerDataDir);
2312
- if (reset && (reset.ownerId !== localSyncDeviceTokenUserId || reset.baseUrl.replace(/\/$/, "") !== uploadBaseUrl.replace(/\/$/, "") ||
2313
- (extraEnv.TOKENUSAGE_CLOUD_DEVICE_ID && reset.deviceId !== extraEnv.TOKENUSAGE_CLOUD_DEVICE_ID))) {
2314
- json(res, { ok: false, code: "cloud_usage_reset_owner_changed" }, 409); return true;
2315
- }
2316
- const binding = JSON.stringify({ ownerId: localSyncDeviceTokenUserId, baseUrl: uploadBaseUrl });
2317
- const ownerPath = path.join(path.dirname(qp), "cloud-upload-owner.json");
2318
- let previous = "";
2319
- try { previous = fs.readFileSync(ownerPath, "utf8"); } catch { /* an unbound legacy cursor cannot prove its previous owner */ }
2320
- if (previous !== binding) {
2321
- fs.writeFileSync(path.join(path.dirname(qp), "queue.state.json"), JSON.stringify({ offset: 0, updatedAt: new Date().toISOString() }), { mode: 0o600 });
2322
- try { fs.unlinkSync(path.join(path.dirname(qp), "upload.throttle.json")); } catch { /* no cooldown yet */ }
2323
- }
2324
- fs.writeFileSync(ownerPath, binding, { mode: 0o600 });
2325
- }
2326
- const controller = new AbortController();
2327
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN) activeCloudSyncs.add(controller);
2328
- const syncStartedAt = Date.now();
2329
- if (localSyncDeviceTokenUserId) extraEnv.TOKENUSAGE_CLOUD_OWNER_ID = localSyncDeviceTokenUserId;
2330
- extraEnv.TOKENUSAGE_LOCAL_SYNC_ATTEMPT_ID = crypto.randomUUID();
2331
- const syncPromise = runSyncCommand(extraEnv, {
2332
- signal: controller.signal,
2333
- trackerDataDir: isolatedTrackerDataDir,
2334
- syncContext,
2335
- drain,
2336
- auto,
2337
- background,
2338
- publishAccount,
2339
- allLocalSources,
2340
- waitForLock:
2341
- !drain && !background && Boolean(extraEnv.TOKENUSAGE_DEVICE_TOKEN),
2342
- });
2343
- controller.done = syncPromise.catch(() => {});
2344
- const result = await syncPromise.catch((error) => {
2345
- if (uploadGeneration !== localCloudGeneration) throw Object.assign(new Error("Account session changed"), { code: "auth_session_changed", status: 409 });
2346
- const failure = currentCloudUploadFailure(syncStartedAt, { backoff: auto && drain, deviceToken: extraEnv.TOKENUSAGE_DEVICE_TOKEN, attemptId: extraEnv.TOKENUSAGE_LOCAL_SYNC_ATTEMPT_ID });
2347
- if (failure) Object.assign(error, failure);
2348
- throw error;
2349
- }).finally(() => activeCloudSyncs.delete(controller));
2350
- if (extraEnv.TOKENUSAGE_DEVICE_TOKEN && uploadGeneration !== localCloudGeneration) throw Object.assign(new Error("Account session changed"), { code: "auth_session_changed", status: 409 });
2351
- const failure = currentCloudUploadFailure(syncStartedAt, { deviceToken: extraEnv.TOKENUSAGE_DEVICE_TOKEN, attemptId: extraEnv.TOKENUSAGE_LOCAL_SYNC_ATTEMPT_ID });
2352
- if (failure) throw Object.assign(new Error("Cloud upload failed"), failure);
2353
- invalidateAfterAccountUpload();
2354
- if (drain) invalidateCloudAccountPayloadCache();
2355
- try {
2356
- const { resetUsageLimitsCache } = require("./usage-limits");
2357
- resetUsageLimitsCache();
2358
- } catch (_e) {
2359
- // ignore if module load fails
2360
- }
2361
- let cloudAccess = { uploaded: false, pending: true };
2362
- try {
2363
- const state = JSON.parse(fs.readFileSync(path.join(path.dirname(qp), "upload.throttle.json"), "utf8"));
2364
- if (state.nextAllowedAtMs) cloudAccess.next_allowed_at = new Date(state.nextAllowedAtMs).toISOString();
2365
- cloudAccess.uploaded = state.lastSuccessAttemptId === extraEnv.TOKENUSAGE_LOCAL_SYNC_ATTEMPT_ID &&
2366
- Number(state.lastSuccessMs || 0) >= syncStartedAt;
2367
- const queueState = JSON.parse(fs.readFileSync(path.join(path.dirname(qp), "queue.state.json"), "utf8"));
2368
- cloudAccess.pending = fs.statSync(qp).size > Number(queueState.offset || 0);
2369
- } catch { /* no upload has been attempted yet */ }
2370
- json(res, { ok: true, ...result, ...cloudAccess });
2371
- } catch (e) {
2372
- json(res, { ok: false, error: e?.message, code: e?.code ?? null, recovery_url: e?.recoveryUrl ?? null, next_allowed_at: e?.nextAllowedAt ?? null, retry_after_seconds: e?.retryAfterMs ? Math.ceil(e.retryAfterMs / 1000) : null, stdout: e?.stdout || "", stderr: e?.stderr || "" }, e?.status || 500);
2373
- }
2374
- return true;
2375
- }
2376
-
2377
- // --- wrapped (year-end summary, à la Spotify Wrapped) ---
2378
- if (p === "/api/functions/tokenusage-wrapped") {
2379
- const yearParam = url.searchParams.get("year");
2380
- const year = yearParam ? Number(yearParam) : null;
2381
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
2382
- const { aggregateWrapped } = require("./wrapped-aggregator");
2383
- const summary = aggregateWrapped(rows, year ? { year } : {});
2384
- json(res, { scope, excluded_sources: excludedSources, ...summary });
2385
- return true;
2386
- }
2387
-
2388
- // --- account (cross-device) view proxy for the native popover ---
2389
- // When ?account=1 and the user is signed in with cloud sync on, serve the
2390
- // same cross-device aggregate the dashboard shows; otherwise tag the
2391
- // response (X-TokenUsage-Account-View: 0 plus an
2392
- // X-TokenUsage-Account-Fallback reason) so the popover knows it got local
2393
- // single-machine data and whether that is permanent (signed out / cloud
2394
- // sync off) or transient, and fall through to the local handler below.
2395
- if (url.searchParams.get("account") === "1") {
2396
- const usageSlug = p.startsWith("/api/functions/") ? p.slice("/api/functions/".length) : "";
2397
- if (accountSlugFor(usageSlug)) {
2398
- const result = await tryServeAccountView(usageSlug, url, res);
2399
- if (result === "served") return true;
2400
- res.setHeader("X-TokenUsage-Account-View", "0");
2401
- res.setHeader("X-TokenUsage-Account-Fallback", result);
2402
- }
2403
- }
2404
-
2405
- // --- usage-summary ---
2406
- if (p === "/api/functions/token-usage-usage-summary") {
2407
- const from = url.searchParams.get("from") || "";
2408
- const to = url.searchParams.get("to") || "";
2409
- const timeZoneContext = getTimeZoneContext(url);
2410
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
2411
- const allDaily = await aggregateByDay(rows, timeZoneContext);
2412
- const daily = allDaily.filter((d) => d.day >= from && d.day <= to);
2413
- const totals = daily.reduce(
2414
- (acc, r) => {
2415
- acc.total_tokens += r.total_tokens;
2416
- acc.billable_total_tokens += r.billable_total_tokens;
2417
- acc.total_cost_usd += r.total_cost_usd || 0;
2418
- acc.input_tokens += r.input_tokens;
2419
- acc.output_tokens += r.output_tokens;
2420
- acc.cached_input_tokens += r.cached_input_tokens;
2421
- acc.cache_creation_input_tokens += r.cache_creation_input_tokens;
2422
- acc.reasoning_output_tokens += r.reasoning_output_tokens;
2423
- acc.conversation_count += r.conversation_count;
2424
- return acc;
2425
- },
2426
- { total_tokens: 0, billable_total_tokens: 0, total_cost_usd: 0, input_tokens: 0, output_tokens: 0, cached_input_tokens: 0, cache_creation_input_tokens: 0, reasoning_output_tokens: 0, conversation_count: 0 },
2427
- );
2428
- const totalCost = totals.total_cost_usd;
2429
-
2430
- const todayParts = getZonedParts(new Date(), timeZoneContext);
2431
- const todayStr = formatPartsDayKey(todayParts) || new Date().toISOString().slice(0, 10);
2432
-
2433
- const shiftDay = (dayStr, delta) => {
2434
- const d = new Date(`${dayStr}T00:00:00Z`);
2435
- d.setUTCDate(d.getUTCDate() + delta);
2436
- return d.toISOString().slice(0, 10);
2437
- };
2438
- const collectDays = (n) => {
2439
- const out = [];
2440
- for (let i = n - 1; i >= 0; i--) {
2441
- const ds = shiftDay(todayStr, -i);
2442
- const dd = allDaily.find((x) => x.day === ds);
2443
- if (dd) out.push(dd);
2444
- }
2445
- return out;
2446
- };
2447
- const sumDays = (days) =>
2448
- days.reduce((a, r) => {
2449
- a.billable_total_tokens += r.billable_total_tokens;
2450
- a.conversation_count += r.conversation_count;
2451
- return a;
2452
- }, { billable_total_tokens: 0, conversation_count: 0 });
2453
-
2454
- const l7 = collectDays(7);
2455
- const l30 = collectDays(30);
2456
- const l7t = sumDays(l7);
2457
- const l30t = sumDays(l30);
2458
- const l7fromStr = shiftDay(todayStr, -6);
2459
- const l30fromStr = shiftDay(todayStr, -29);
2460
-
2461
- json(res, {
2462
- from, to, days: daily.length, scope, excluded_sources: excludedSources,
2463
- totals: { ...totals, total_cost_usd: totalCost.toFixed(6) },
2464
- rolling: {
2465
- last_7d: { from: l7fromStr, to: todayStr, active_days: l7.length, totals: l7t },
2466
- last_30d: { from: l30fromStr, to: todayStr, active_days: l30.length, totals: l30t, avg_per_active_day: l30.length > 0 ? Math.round(l30t.billable_total_tokens / l30.length) : 0 },
2467
- },
2468
- });
2469
- return true;
2470
- }
2471
-
2472
- // --- usage-daily ---
2473
- if (p === "/api/functions/token-usage-usage-daily") {
2474
- const from = url.searchParams.get("from") || "";
2475
- const to = url.searchParams.get("to") || "";
2476
- const timeZoneContext = getTimeZoneContext(url);
2477
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
2478
- const daily = (await aggregateByDay(rows, timeZoneContext)).filter((d) => d.day >= from && d.day <= to);
2479
- json(res, { from, to, scope, excluded_sources: excludedSources, data: daily });
2480
- return true;
2481
- }
2482
-
2483
- // --- outcomes (opt-in quality-per-dollar / Effective Tokens; sidecar) ---
2484
- // Reads the optional ~/.tokenusage/tracker/outcomes.jsonl sidecar and
2485
- // joins it to the existing token/$ rows at READ time. Returns
2486
- // available:false with empty arrays when the user hasn't opted in, so the
2487
- // dashboard renders exactly as before. This path NEVER reads or writes
2488
- // queue.jsonl's schema or the sync path (see GitHub #229).
2489
- if (p === "/api/functions/tokenusage-outcomes") {
2490
- const from = url.searchParams.get("from") || "";
2491
- const to = url.searchParams.get("to") || "";
2492
- const {
2493
- readAllOutcomesData,
2494
- computeQualityPerDollar,
2495
- } = require("./outcomes-engine");
2496
- // Refresh the independent metadata-only sidecars before joining. Any
2497
- // scanner failure degrades to the existing manual outcomes file.
2498
- try {
2499
- const { buildSessionAnalytics } = require("./session-analytics");
2500
- const { buildGitOutcomes } = require("./git-outcomes");
2501
- const sessions = await buildSessionAnalytics();
2502
- await buildGitOutcomes(sessions);
2503
- } catch (error) {
2504
- console.error("[outcomes] automatic Git attribution failed:", error?.message || error);
2505
- }
2506
- const outcomes = readAllOutcomesData();
2507
- if (!outcomes.length) {
2508
- json(res, { available: false, from, to, by_model: [], by_tool: [], totals: null });
2509
- return true;
2510
- }
2511
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
2512
- const result = computeQualityPerDollar(rows, outcomes, { from, to });
2513
- json(res, { from, to, scope, excluded_sources: excludedSources, ...result });
2514
- return true;
2515
- }
2516
-
2517
- // --- metadata-only Claude/Codex session efficiency ---
2518
- if (p === "/api/functions/tokenusage-session-insights") {
2519
- const from = url.searchParams.get("from") || "";
2520
- const to = url.searchParams.get("to") || "";
2521
- const refresh = ["1", "true"].includes(url.searchParams.get("refresh"));
2522
- try {
2523
- const { buildSessionAnalytics, summarizeSessions, sessionsToCsv } = require("./session-analytics");
2524
- const sessions = await buildSessionAnalytics({ force: refresh });
2525
- const wantsCsv = url.searchParams.get("format") === "csv";
2526
- const includeSessions = wantsCsv || ["1", "true"].includes(url.searchParams.get("include_sessions"));
2527
- const result = summarizeSessions(sessions, { from, to, includeSessions, ...getTimeZoneContext(url) });
2528
- if (wantsCsv) {
2529
- const content = sessionsToCsv(result.sessions);
2530
- res.statusCode = 200;
2531
- res.setHeader("Content-Type", "text/csv; charset=utf-8");
2532
- res.setHeader("Content-Disposition", "attachment; filename=tokenusage-sessions.csv");
2533
- res.end(content);
2534
- return true;
2535
- }
2536
- json(res, { from, to, ...result });
2537
- } catch (error) {
2538
- json(res, { available: false, error: error?.message || "Session analytics failed" }, 500);
2539
- }
2540
- return true;
2541
- }
2542
-
2543
- // --- metadata-only session browser (LOCAL ONLY) ---
2544
- // Unlike session-insights this returns per-session rows that retain the
2545
- // raw session_id + local project path so the dashboard can offer one-click
2546
- // resume. It is intentionally served only from the local API and never
2547
- // proxied to the cloud account view.
2548
- if (p === "/api/functions/tokenusage-sessions") {
2549
- const from = url.searchParams.get("from") || "";
2550
- const to = url.searchParams.get("to") || "";
2551
- const refresh = ["1", "true"].includes(url.searchParams.get("refresh"));
2552
- const limitParam = parseInt(url.searchParams.get("limit") || "0", 10);
2553
- const limit = Number.isFinite(limitParam) && limitParam > 0 ? Math.min(limitParam, 2000) : 0;
2554
- // This payload contains local paths and resumable session identifiers.
2555
- // The server is loopback-only; additionally prevent browser/proxy caches
2556
- // from retaining the response after the page is closed.
2557
- res.setHeader("Cache-Control", "no-store");
2558
- res.setHeader("X-Content-Type-Options", "nosniff");
2559
- try {
2560
- const { buildSessionAnalytics, listSessionsForBrowser } = require("./session-analytics");
2561
- const sessions = await buildSessionAnalytics({ force: refresh });
2562
- const result = listSessionsForBrowser(sessions, { from, to, limit, ...getTimeZoneContext(url) });
2563
- json(res, { from, to, ...result });
2564
- } catch (error) {
2565
- // Node fs errors embed the absolute path ("EACCES ... open '/Users/…'").
2566
- // Keep that out of the HTTP body and log it locally instead.
2567
- console.warn("[local-api] session browser failed:", error?.message || error);
2568
- json(res, { available: false, error: "Session browser failed" }, 500);
2569
- }
2570
- return true;
2571
- }
2572
-
2573
- // --- fixed context overhead audit (counts and estimates only) ---
2574
- if (p === "/api/functions/tokenusage-context-health") {
2575
- const { computeContextHealth } = require("./context-health");
2576
- json(res, computeContextHealth({ home: os.homedir(), cwd: process.cwd(), env: process.env }));
2577
- return true;
2578
- }
2579
-
2580
- // --- usage-heatmap ---
2581
- if (p === "/api/functions/token-usage-usage-heatmap") {
2582
- const weeks = parseInt(url.searchParams.get("weeks") || "52", 10);
2583
- const timeZoneContext = getTimeZoneContext(url);
2584
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
2585
- const daily = await aggregateByDay(rows, timeZoneContext);
2586
- const todayParts = getZonedParts(new Date(), timeZoneContext);
2587
- const todayStr = formatPartsDayKey(todayParts) || new Date().toISOString().slice(0, 10);
2588
- const end = new Date(`${todayStr}T00:00:00Z`);
2589
- const start = new Date(end);
2590
- start.setUTCDate(start.getUTCDate() - weeks * 7 + 1);
2591
- const from = start.toISOString().slice(0, 10);
2592
- const to = end.toISOString().slice(0, 10);
2593
- const byDay = new Map(daily.map((d) => [d.day, d]));
2594
-
2595
- const allValues = daily.map((d) => d.billable_total_tokens).filter((v) => v > 0);
2596
- const maxValue = allValues.length > 0 ? Math.max(...allValues) : 0;
2597
- const calcLevel = (v) => {
2598
- if (v <= 0) return 0;
2599
- if (maxValue === 0) return 1;
2600
- const r = v / maxValue;
2601
- if (r <= 0.25) return 1;
2602
- if (r <= 0.5) return 2;
2603
- if (r <= 0.75) return 3;
2604
- return 4;
2605
- };
2606
-
2607
- // Build cells and group into weeks (array of 7-cell arrays) for the dashboard
2608
- const cells = [];
2609
- const cursor = new Date(start);
2610
- while (cursor <= end) {
2611
- const day = cursor.toISOString().slice(0, 10);
2612
- const data = byDay.get(day);
2613
- const billable = data?.billable_total_tokens || 0;
2614
- cells.push({ day, total_tokens: data?.total_tokens || 0, billable_total_tokens: billable, level: calcLevel(billable), models: data?.models || null });
2615
- cursor.setUTCDate(cursor.getUTCDate() + 1);
2616
- }
2617
- const weeksArr = [];
2618
- for (let i = 0; i < cells.length; i += 7) {
2619
- weeksArr.push(cells.slice(i, i + 7));
2620
- }
2621
-
2622
- let totalCostUsd = 0;
2623
- for (const d of daily) {
2624
- if (d.day >= from && d.day <= to) {
2625
- totalCostUsd += d.total_cost_usd || 0;
2626
- }
2627
- }
2628
-
2629
- json(res, {
2630
- from,
2631
- to,
2632
- scope,
2633
- excluded_sources: excludedSources,
2634
- week_starts_on: "sun",
2635
- active_days: cells.filter((c) => c.billable_total_tokens > 0).length,
2636
- streak_days: 0,
2637
- weeks: weeksArr,
2638
- total_cost_usd: totalCostUsd
2639
- });
2640
- return true;
2641
- }
2642
-
2643
- // --- usage-model-breakdown ---
2644
- if (p === "/api/functions/token-usage-usage-model-breakdown") {
2645
- const from = url.searchParams.get("from") || "";
2646
- const to = url.searchParams.get("to") || "";
2647
- const timeZoneContext = getTimeZoneContext(url);
2648
- const { rows: scopedRows, scope, excludedSources } = await scopedQueueRows(qp, url);
2649
- const pricingRevision = getPricingRevision();
2650
- const bySource = new Map();
2651
- let processed = 0;
2652
- for (const row of scopedRows) {
2653
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
2654
- if (!row.hour_start) continue;
2655
- const day = rowDayKey(row, timeZoneContext);
2656
- if (day < from || day > to) continue;
2657
- const src = row.source || "unknown";
2658
- const mdl = row.model || "unknown";
2659
- if (!bySource.has(src))
2660
- bySource.set(src, { source: src, source_scope: getSourceScope(src), totals: { total_tokens: 0, billable_total_tokens: 0, input_tokens: 0, output_tokens: 0, cached_input_tokens: 0, cache_creation_input_tokens: 0, reasoning_output_tokens: 0, total_cost_usd: "0" }, models: new Map() });
2661
- const sa = bySource.get(src);
2662
- sa.totals.total_tokens += row.total_tokens || 0;
2663
- sa.totals.billable_total_tokens += row.billable_total_tokens ?? row.total_tokens ?? 0;
2664
- sa.totals.input_tokens += row.input_tokens || 0;
2665
- sa.totals.output_tokens += row.output_tokens || 0;
2666
- sa.totals.cached_input_tokens += row.cached_input_tokens || 0;
2667
- sa.totals.cache_creation_input_tokens += row.cache_creation_input_tokens || 0;
2668
- sa.totals.reasoning_output_tokens += row.reasoning_output_tokens || 0;
2669
- if (!sa.models.has(mdl))
2670
- sa.models.set(mdl, { model: mdl, model_id: mdl, pricing: getModelPricingInfo(mdl, { source: src }), totals: { total_tokens: 0, billable_total_tokens: 0, input_tokens: 0, output_tokens: 0, cached_input_tokens: 0, cache_creation_input_tokens: 0, reasoning_output_tokens: 0, total_cost_usd: "0" } });
2671
- const ma = sa.models.get(mdl);
2672
- const reportedCost = ["grok", "cline"].includes(src) && Number(row.total_cost_usd) > 0;
2673
- const costSource = reportedCost ? "provider_reported" : "model_pricing";
2674
- ma.cost_source = ma.cost_source && ma.cost_source !== costSource ? "mixed" : costSource;
2675
- ma.totals.total_tokens += row.total_tokens || 0;
2676
- ma.totals.billable_total_tokens += row.billable_total_tokens ?? row.total_tokens ?? 0;
2677
- ma.totals.input_tokens += row.input_tokens || 0;
2678
- ma.totals.output_tokens += row.output_tokens || 0;
2679
- ma.totals.cached_input_tokens += row.cached_input_tokens || 0;
2680
- ma.totals.cache_creation_input_tokens += row.cache_creation_input_tokens || 0;
2681
- ma.totals.reasoning_output_tokens += row.reasoning_output_tokens || 0;
2682
- // Price each row, exactly as aggregateByDay does for the hero (#653).
2683
- // computeRowCost is not linear for models with a Fast tier or
2684
- // long-context rates: priority_* / long_context_* mark a subset of the
2685
- // base columns rather than adding volume, so they survive only while the
2686
- // row is intact. Summing the base columns first and pricing once silently
2687
- // dropped every premium — same tokens, fewer dollars than the hero.
2688
- ma.totals.total_cost_usd = Number(ma.totals.total_cost_usd || 0) + computeRowCost(row);
2689
- }
2690
- if (pricingRevision !== getPricingRevision()) return handleLocalApi(req, res, url);
2691
-
2692
- const sources = Array.from(bySource.values()).map((s) => {
2693
- s.models = Array.from(s.models.values())
2694
- .map((m) => ({
2695
- ...m,
2696
- totals: { ...m.totals, total_cost_usd: Number(m.totals.total_cost_usd || 0).toFixed(6) },
2697
- }))
2698
- .sort((a, b) => b.totals.total_tokens - a.totals.total_tokens);
2699
- const sourceCost = s.models.reduce((sum, m) => sum + Number(m.totals.total_cost_usd), 0);
2700
- s.totals.total_cost_usd = sourceCost.toFixed(6);
2701
- return s;
2702
- });
2703
-
2704
- json(res, {
2705
- from, to, days: 0, scope, excluded_sources: excludedSources, sources,
2706
- pricing: { model: "per-model", pricing_mode: "per_token_type", source: "litellm", effective_from: new Date().toISOString().slice(0, 10) },
2707
- });
2708
- return true;
2709
- }
2710
-
2711
- // --- usage-category-breakdown (Claude + Codex + Grok) ---
2712
- // Claude: splits historical Claude usage into seven semantic categories
2713
- // mirroring Claude Code's /context view (approx).
2714
- // Codex/Grok: tool-oriented breakdown, attributing per-turn token
2715
- // totals to observed tool calls (heuristic).
2716
- if (p === "/api/functions/tokenusage-usage-category-breakdown") {
2717
- const from = url.searchParams.get("from") || "";
2718
- const to = url.searchParams.get("to") || "";
2719
- const requestedSource = (url.searchParams.get("source") || "claude").trim().toLowerCase();
2720
- if (requestedSource === "claude") {
2721
- try {
2722
- const result = await computeClaudeCategoryBreakdown({ from, to, projectDir: process.cwd() });
2723
- json(res, { from, to, ...result });
2724
- } catch (e) {
2725
- console.error("[LocalAPI] usage-category-breakdown:", e?.message || e);
2726
- json(res, { from, to, ...unsupportedCategoryPayload("claude"), error: "compute_failed" }, 500);
2727
- }
2728
- return true;
2729
- }
2730
-
2731
- if (requestedSource === "codex") {
2732
- try {
2733
- const timeZoneContext = getTimeZoneContext(url);
2734
- const result = await computeCodexContextBreakdown({
2735
- from,
2736
- to,
2737
- top: 50,
2738
- timeZoneContext,
2739
- });
2740
- if (!Number(result?.totals?.total_tokens || 0)) {
2741
- const fallback = buildCodexCategoryFallbackFromQueue(await readQueueData(qp), {
2742
- from,
2743
- to,
2744
- timeZoneContext,
2745
- });
2746
- json(res, { from, to, ...fallback });
2747
- return true;
2748
- }
2749
- json(res, { from, to, ...result });
2750
- } catch (e) {
2751
- console.error("[LocalAPI] usage-category-breakdown(codex):", e?.message || e);
2752
- json(res, { from, to, ...unsupportedCategoryPayload("codex"), error: "compute_failed" }, 500);
2753
- }
2754
- return true;
2755
- }
2756
-
2757
- if (requestedSource === "grok") {
2758
- try {
2759
- const timeZoneContext = getTimeZoneContext(url);
2760
- const result = await computeGrokContextBreakdown({
2761
- from,
2762
- to,
2763
- top: 50,
2764
- timeZoneContext,
2765
- });
2766
- json(res, { from, to, ...result });
2767
- } catch (e) {
2768
- console.error("[LocalAPI] usage-category-breakdown(grok):", e?.message || e);
2769
- json(res, { from, to, ...unsupportedCategoryPayload("grok"), error: "compute_failed" }, 500);
2770
- }
2771
- return true;
2772
- }
2773
-
2774
- json(res, { from, to, ...unsupportedCategoryPayload(requestedSource) });
2775
- return true;
2776
- }
2777
-
2778
- // --- project-usage-summary ---
2779
- if (p === "/api/functions/tokenusage-project-usage-summary") {
2780
- // Use the per-project bucket log that rollout.js emits — it already
2781
- // carries the actual tokens attributed to each (project_key, source,
2782
- // hour_start). Falling back to "session-file count × total tokens"
2783
- // (the old behavior) produced pure fiction: every short-and-hot
2784
- // project got the same weight as every long-and-cold one.
2785
- const limitParam = Number(url.searchParams.get("limit"));
2786
- const limit = Number.isFinite(limitParam) && limitParam > 0
2787
- ? Math.min(Math.floor(limitParam), PROJECT_USAGE_MAX_ENTRIES)
2788
- : PROJECT_USAGE_MAX_ENTRIES;
2789
- const { timeZoneContext, hasRange, dayInRange, projectRows } =
2790
- await readProjectUsageContext(qp, url);
2791
-
2792
- const aggregateEntries = async (rows, keyOf, refOf) => {
2793
- const byKey = new Map();
2794
- let processed = 0;
2795
- for (const row of rows) {
2796
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
2797
- if (hasRange && !dayInRange(rowDayKey(row, timeZoneContext))) continue;
2798
- const key = keyOf(row);
2799
- if (!byKey.has(key)) {
2800
- byKey.set(key, {
2801
- project_key: key,
2802
- project_ref: refOf(row),
2803
- total_tokens: 0,
2804
- billable_total_tokens: 0,
2805
- input_tokens: 0,
2806
- output_tokens: 0,
2807
- cached_input_tokens: 0,
2808
- cache_creation_input_tokens: 0,
2809
- reasoning_output_tokens: 0,
2810
- conversation_count: 0,
2811
- sourceTotals: new Map(),
2812
- });
2813
- }
2814
- const agg = byKey.get(key);
2815
- agg.total_tokens += Number(row.total_tokens || 0);
2816
- agg.billable_total_tokens += Number(
2817
- row.billable_total_tokens ?? row.total_tokens ?? 0,
2818
- );
2819
- agg.input_tokens += Number(row.input_tokens || 0);
2820
- agg.output_tokens += Number(row.output_tokens || 0);
2821
- agg.cached_input_tokens += Number(row.cached_input_tokens || 0);
2822
- agg.cache_creation_input_tokens += Number(row.cache_creation_input_tokens || 0);
2823
- agg.reasoning_output_tokens += Number(row.reasoning_output_tokens || 0);
2824
- agg.conversation_count += Number(row.conversation_count || 0);
2825
- if (!agg.project_ref) agg.project_ref = refOf(row);
2826
- const src = row.source || "unknown";
2827
- agg.sourceTotals.set(
2828
- src,
2829
- (agg.sourceTotals.get(src) || 0) + Number(row.total_tokens || 0),
2830
- );
2831
- }
2832
- return Array.from(byKey.values())
2833
- .sort((a, b) => b.billable_total_tokens - a.billable_total_tokens)
2834
- .slice(0, limit)
2835
- .map(({ sourceTotals, ...entry }) => ({
2836
- ...entry,
2837
- total_tokens: String(entry.total_tokens),
2838
- billable_total_tokens: String(entry.billable_total_tokens),
2839
- sources: Array.from(sourceTotals.entries())
2840
- .sort((a, b) => b[1] - a[1])
2841
- .map(([source, totalTokens]) => ({ source, total_tokens: totalTokens })),
2842
- }));
2843
- };
2844
-
2845
- let entries = await aggregateEntries(
2846
- projectRows,
2847
- (row) => row.project_key || "unknown",
2848
- (row) => row.project_ref || "",
2849
- );
2850
-
2851
- // If no project-attributed rows exist yet (user hasn't synced project
2852
- // attribution, or never used a project-capable CLI), fall back to
2853
- // per-source aggregation over the main queue so the panel isn't
2854
- // totally empty. This path used to also exist for the non-empty case
2855
- // and produce wrong numbers; keep it only as the empty fallback.
2856
- if (entries.length === 0 && projectRows.length === 0) {
2857
- entries = await aggregateEntries(
2858
- await readQueueData(qp),
2859
- (row) => row.source || "unknown",
2860
- // Synthetic source-only row: leave project_ref empty rather than
2861
- // fabricating `https://${src}.ai`, which resolves to unrelated
2862
- // domains (e.g. codex.ai, cursor.ai) and was sent to the
2863
- // dashboard as a clickable href before v0.11.1 / this commit.
2864
- () => "",
2865
- );
2866
- }
2867
-
2868
- json(res, { generated_at: new Date().toISOString(), entries });
2869
- return true;
2870
- }
2871
-
2872
- // --- project-usage-detail ---
2873
- // Per-project drill-down for the Project Usage modal: daily series,
2874
- // per-source breakdown, and aggregate stats. Everything comes from the
2875
- // local project bucket log — no external requests, no git access.
2876
- if (p === "/api/functions/tokenusage-project-usage-detail") {
2877
- const projectKey = url.searchParams.get("project_key") || "";
2878
- if (!projectKey) {
2879
- json(res, { error: "missing_project_key" }, 400);
2880
- return true;
2881
- }
2882
- const { from, to, timeZoneContext, dayInRange, projectRows } =
2883
- await readProjectUsageContext(qp, url);
2884
-
2885
- const emptyMeasures = () => ({
2886
- total_tokens: 0,
2887
- billable_total_tokens: 0,
2888
- input_tokens: 0,
2889
- output_tokens: 0,
2890
- cached_input_tokens: 0,
2891
- cache_creation_input_tokens: 0,
2892
- reasoning_output_tokens: 0,
2893
- conversation_count: 0,
2894
- });
2895
- const addMeasures = (acc, row) => {
2896
- acc.total_tokens += Number(row.total_tokens || 0);
2897
- acc.billable_total_tokens += Number(
2898
- row.billable_total_tokens ?? row.total_tokens ?? 0,
2899
- );
2900
- acc.input_tokens += Number(row.input_tokens || 0);
2901
- acc.output_tokens += Number(row.output_tokens || 0);
2902
- acc.cached_input_tokens += Number(row.cached_input_tokens || 0);
2903
- acc.cache_creation_input_tokens += Number(row.cache_creation_input_tokens || 0);
2904
- acc.reasoning_output_tokens += Number(row.reasoning_output_tokens || 0);
2905
- acc.conversation_count += Number(row.conversation_count || 0);
2906
- };
2907
-
2908
- let projectRef = "";
2909
- const totals = emptyMeasures();
2910
- const byDay = new Map();
2911
- const bySource = new Map();
2912
- let rangeTotalTokens = 0;
2913
- let processed = 0;
2914
- for (const row of projectRows) {
2915
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
2916
- const day = rowDayKey(row, timeZoneContext);
2917
- if (!dayInRange(day)) continue;
2918
- rangeTotalTokens += Number(row.total_tokens || 0);
2919
- if (row.project_key !== projectKey) continue;
2920
- if (!projectRef && row.project_ref) projectRef = row.project_ref;
2921
- addMeasures(totals, row);
2922
-
2923
- const src = row.source || "unknown";
2924
- if (day) {
2925
- // `models` keyed by source (project rows carry no model column) —
2926
- // it feeds TrendMonitor's stacked-segment slot so the modal chart
2927
- // colors by provider exactly like the dashboard's Usage Trend.
2928
- if (!byDay.has(day)) byDay.set(day, { day, ...emptyMeasures(), models: {} });
2929
- const dayAgg = byDay.get(day);
2930
- addMeasures(dayAgg, row);
2931
- dayAgg.models[src] = (dayAgg.models[src] || 0) + Number(row.total_tokens || 0);
2932
- }
2933
-
2934
- if (!bySource.has(src)) {
2935
- bySource.set(src, {
2936
- source: src,
2937
- total_tokens: 0,
2938
- conversation_count: 0,
2939
- dayKeys: new Set(),
2940
- });
2941
- }
2942
- const srcAgg = bySource.get(src);
2943
- srcAgg.total_tokens += Number(row.total_tokens || 0);
2944
- srcAgg.conversation_count += Number(row.conversation_count || 0);
2945
- if (day) srcAgg.dayKeys.add(day);
2946
- }
2947
-
2948
- json(res, {
2949
- generated_at: new Date().toISOString(),
2950
- from,
2951
- to,
2952
- project_key: projectKey,
2953
- project_ref: projectRef,
2954
- totals,
2955
- days_active: byDay.size,
2956
- // All-project total over the same range so the client can render
2957
- // "share of everything you tracked" without a second request.
2958
- range_total_tokens: rangeTotalTokens,
2959
- daily: Array.from(byDay.values()).sort((a, b) => a.day.localeCompare(b.day)),
2960
- sources: Array.from(bySource.values())
2961
- .sort((a, b) => b.total_tokens - a.total_tokens)
2962
- .map(({ dayKeys, ...src }) => ({ ...src, days_active: dayKeys.size })),
2963
- });
2964
- return true;
2965
- }
2966
-
2967
- // --- user-status (stub) ---
2968
- if (p === "/api/functions/tokenusage-user-status") {
2969
- json(res, {
2970
- user_id: "local-user", email: "local@localhost", name: "Local User", is_public: false,
2971
- created_at: new Date().toISOString(),
2972
- pro: { active: true, sources: ["local"], expires_at: null, partial: false, as_of: new Date().toISOString() },
2973
- // Cross-device popover state: whether account aggregation can be served
2974
- // (signed in) and whether the dashboard's cloud-sync toggle is on.
2975
- account: {
2976
- available: Boolean(getRefreshTokenForCloud()),
2977
- cloud_sync_enabled: getCloudSyncPref(),
2978
- account_view: Boolean(getRefreshTokenForCloud()) && getCloudSyncPref(),
2979
- },
2980
- });
2981
- return true;
2982
- }
2983
-
2984
- // --- cloud-sync preference mirror ---
2985
- // The dashboard's Settings → Account → Cloud sync toggle (a WebView
2986
- // localStorage flag) is mirrored here so the auth-unaware native popover can
2987
- // gate its account (cross-device) view on the same preference.
2988
- if (p === "/api/functions/token-usage-cloud-sync-pref") {
2989
- const method = String(req.method || "GET").toUpperCase();
2990
- if (method === "GET") {
2991
- json(res, {
2992
- enabled: getCloudSyncPref(),
2993
- account_available: Boolean(getRefreshTokenForCloud()),
2994
- });
2995
- return true;
2996
- }
2997
- if (method === "POST" || method === "PUT") {
2998
- if (!isAuthorizedLocalMutation(req)) {
2999
- json(res, { ok: false, error: "Unauthorized" }, 401);
3000
- return true;
3001
- }
3002
- let body = {};
3003
- try {
3004
- body = await readJsonBody(req);
3005
- } catch {
3006
- body = {};
3007
- }
3008
- // Reject malformed payloads rather than silently coercing them to a
3009
- // persisted `false` — this file is the shared cloud-sync preference, so
3010
- // a bad write would desync the popover from the dashboard.
3011
- if (typeof body?.enabled !== "boolean") {
3012
- json(res, { ok: false, error: "enabled must be a boolean" }, 400);
3013
- return true;
3014
- }
3015
- if (body.changedAtMs != null && (!Number.isSafeInteger(body.changedAtMs) || body.changedAtMs < 0)) {
3016
- json(res, { ok: false, error: "changedAtMs must be a nonnegative safe integer" }, 400);
3017
- return true;
3018
- }
3019
- setCloudSyncPref(body.enabled, body.changedAtMs ?? Date.now());
3020
- json(res, { ok: true, enabled: getCloudSyncPref() });
3021
- return true;
3022
- }
3023
- json(res, { error: "Method Not Allowed" }, 405);
3024
- return true;
3025
- }
3026
-
3027
- // --- outbound proxy preference (manual / system / off) ---
3028
- // Persisted on config.json next to the queue. GET is unauthenticated so the
3029
- // settings page can probe availability; POST requires local-auth.
3030
- if (p === "/api/functions/tokenusage-proxy-config") {
3031
- const {
3032
- normalizeProxyConfig,
3033
- parseProxyPayload,
3034
- } = require("./proxy-settings");
3035
- const {
3036
- applyUndiciProxyIfNeeded,
3037
- getLastProxyApplyError,
3038
- resolveEffectiveProxySource,
3039
- invalidateSystemProxyCache,
3040
- } = require("./proxy-env");
3041
- const { writeFileAtomic, chmod600IfPossible } = require("./fs");
3042
- const configPath = path.join(path.dirname(qp), "config.json");
3043
- const readConfig = () => {
3044
- try {
3045
- const parsed = JSON.parse(fs.readFileSync(configPath, "utf8"));
3046
- return parsed && typeof parsed === "object" ? parsed : {};
3047
- } catch {
3048
- return {};
3049
- }
3050
- };
3051
- const writeConfig = async (next) => {
3052
- await writeFileAtomic(configPath, JSON.stringify(next, null, 2));
3053
- await chmod600IfPossible(configPath);
3054
- };
3055
- const toResponse = (configObj) => {
3056
- const normalized = normalizeProxyConfig(configObj?.proxy);
3057
- const effective = resolveEffectiveProxySource({
3058
- env: process.env,
3059
- proxyConfig: configObj?.proxy,
3060
- });
3061
- return {
3062
- mode: normalized.mode,
3063
- protocol: normalized.protocol,
3064
- host: normalized.host,
3065
- port: normalized.port,
3066
- effective: effective.source,
3067
- applyError: getLastProxyApplyError(),
3068
- };
3069
- };
3070
- const method = String(req.method || "GET").toUpperCase();
3071
- if (method === "GET") {
3072
- json(res, toResponse(readConfig()));
3073
- return true;
3074
- }
3075
- if (method === "POST" || method === "PUT") {
3076
- if (!isAuthorizedLocalMutation(req)) {
3077
- json(res, { ok: false, error: "Unauthorized" }, 401);
3078
- return true;
3079
- }
3080
- let body = {};
3081
- try {
3082
- body = await readJsonBody(req);
3083
- } catch {
3084
- json(res, { ok: false, error: "invalid JSON" }, 400);
3085
- return true;
3086
- }
3087
- const parsed = parseProxyPayload(body);
3088
- if (!parsed.ok) {
3089
- json(res, { ok: false, error: parsed.error }, 400);
3090
- return true;
3091
- }
3092
- const current = readConfig();
3093
- const prevProxy = current.proxy && typeof current.proxy === "object" && !Array.isArray(current.proxy)
3094
- ? current.proxy
3095
- : {};
3096
- current.proxy = {
3097
- ...prevProxy,
3098
- mode: parsed.value.mode,
3099
- protocol: parsed.value.protocol,
3100
- host: parsed.value.host,
3101
- port: parsed.value.port,
3102
- };
3103
- try {
3104
- await writeConfig(current);
3105
- } catch (error) {
3106
- json(res, { ok: false, error: error?.message || "failed to save proxy config" }, 500);
3107
- return true;
3108
- }
3109
- invalidateSystemProxyCache();
3110
- const applyResult = applyUndiciProxyIfNeeded({ proxyConfig: current.proxy });
3111
- json(res, {
3112
- ok: applyResult?.ok !== false,
3113
- unprotected: applyResult?.unprotected === true,
3114
- ...toResponse(current),
3115
- });
3116
- return true;
3117
- }
3118
- json(res, { error: "Method Not Allowed" }, 405);
3119
- return true;
3120
- }
3121
-
3122
- if (p === "/api/functions/tokenusage-proxy-test") {
3123
- const { parseProxyPayload, buildProxyUrl } = require("./proxy-settings");
3124
- const { runProxyConnectivityTest } = require("./proxy-env");
3125
- if (String(req.method || "GET").toUpperCase() !== "POST") {
3126
- json(res, { error: "Method Not Allowed" }, 405);
3127
- return true;
3128
- }
3129
- if (!isAuthorizedLocalMutation(req)) {
3130
- json(res, { ok: false, error: "Unauthorized" }, 401);
3131
- return true;
3132
- }
3133
- let body = {};
3134
- try {
3135
- body = await readJsonBody(req);
3136
- } catch {
3137
- json(res, { ok: false, error: "invalid JSON" }, 400);
3138
- return true;
3139
- }
3140
- const parsed = parseProxyPayload({ ...body, mode: "manual" });
3141
- if (!parsed.ok) {
3142
- json(res, { ok: false, error: parsed.error }, 400);
3143
- return true;
3144
- }
3145
- const proxyUrl = buildProxyUrl({ ...parsed.value, mode: "manual" });
3146
- const configPath = path.join(path.dirname(qp), "config.json");
3147
- let config = {};
3148
- try {
3149
- config = JSON.parse(fs.readFileSync(configPath, "utf8")) || {};
3150
- } catch {
3151
- config = {};
3152
- }
3153
- const runtime = resolveRuntimeConfig({ config, env: process.env });
3154
- const targetUrl = runtime.dashboardUrl || runtime.baseUrl;
3155
- const result = await runProxyConnectivityTest({
3156
- proxyUrl,
3157
- targetUrl,
3158
- timeoutMs: 5000,
3159
- });
3160
- json(res, result);
3161
- return true;
3162
- }
3163
-
3164
- // --- machine-id (stable per-machine device identity for cloud sync) ---
3165
- // The dashboard reads this before issuing a cloud device token so the
3166
- // device_name keys on the MACHINE, not the browser — see
3167
- // getOrCreateMachineId above and dashboard/src/lib/cloud-sync.ts.
3168
- if (p === "/api/functions/tokenusage-machine-id") {
3169
- json(res, {
3170
- machineId: getOrCreateMachineId(qp),
3171
- deviceName: getSystemDeviceName(),
3172
- });
3173
- return true;
3174
- }
3175
-
3176
- // --- usage-hourly (stub for day-view) ---
3177
- if (p === "/api/functions/token-usage-usage-hourly") {
3178
- const day = url.searchParams.get("day") || new Date().toISOString().slice(0, 10);
3179
- const timeZoneContext = getTimeZoneContext(url);
3180
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
3181
- const data = await aggregateHourlyByDay(rows, day, timeZoneContext);
3182
- json(res, { day, scope, excluded_sources: excludedSources, data });
3183
- return true;
3184
- }
3185
-
3186
- // --- usage-monthly (stub for trend view) ---
3187
- if (p === "/api/functions/token-usage-usage-monthly") {
3188
- const from = url.searchParams.get("from") || "";
3189
- const to = url.searchParams.get("to") || "";
3190
- const timeZoneContext = getTimeZoneContext(url);
3191
- const { rows, scope, excludedSources } = await scopedQueueRows(qp, url);
3192
- const byMonth = new Map();
3193
- let processed = 0;
3194
- for (const row of rows) {
3195
- if (++processed % AGGREGATION_BATCH_ROWS === 0) await yieldToRequests();
3196
- if (!row.hour_start) continue;
3197
- const day = rowDayKey(row, timeZoneContext);
3198
- if (!day || day < from || day > to) continue;
3199
- const month = day.slice(0, 7);
3200
- if (!byMonth.has(month))
3201
- byMonth.set(month, { month, total_tokens: 0, billable_total_tokens: 0, input_tokens: 0, output_tokens: 0, cached_input_tokens: 0, cache_creation_input_tokens: 0, reasoning_output_tokens: 0, conversation_count: 0 });
3202
- const a = byMonth.get(month);
3203
- a.total_tokens += row.total_tokens || 0;
3204
- a.billable_total_tokens += row.billable_total_tokens ?? row.total_tokens ?? 0;
3205
- a.input_tokens += row.input_tokens || 0;
3206
- a.output_tokens += row.output_tokens || 0;
3207
- a.cached_input_tokens += row.cached_input_tokens || 0;
3208
- a.cache_creation_input_tokens += row.cache_creation_input_tokens || 0;
3209
- a.reasoning_output_tokens += row.reasoning_output_tokens || 0;
3210
- a.conversation_count += row.conversation_count || 0;
3211
-
3212
- if (!a.models) {
3213
- a.models = {};
3214
- }
3215
- const model = row.model || "unknown";
3216
- a.models[model] = (a.models[model] || 0) + (row.total_tokens || 0);
3217
- }
3218
- json(res, { from, to, scope, excluded_sources: excludedSources, data: Array.from(byMonth.values()).sort((a, b) => a.month.localeCompare(b.month)) });
3219
- return true;
3220
- }
3221
-
3222
- // --- Codex-compatible pet manager ---
3223
- if (p === "/api/functions/tokenusage-pets") {
3224
- const method = String(req.method || "GET").toUpperCase();
3225
- const pets = require("./pet-packages");
3226
- try {
3227
- if (method === "GET") {
3228
- if (url.searchParams.get("scope") === "codex") {
3229
- const importable = pets.listCodexImportablePets();
3230
- json(res, { importable, codexDetected: importable.length > 0 });
3231
- return true;
3232
- }
3233
- json(res, {
3234
- pets: pets.listInstalledPets(),
3235
- hiddenBuiltinIds: pets.readHiddenBuiltinIds(),
3236
- });
3237
- return true;
3238
- }
3239
- if (method === "POST") {
3240
- if (!isAuthorizedLocalMutation(req)) {
3241
- json(res, { ok: false, error: "Unauthorized" }, 401);
3242
- return true;
3243
- }
3244
- const body = await readJsonBody(req);
3245
- if (body?.action === "install_url") {
3246
- json(res, { ok: true, pet: await pets.installFromCodexPets(body.url || body.id) });
3247
- return true;
3248
- }
3249
- if (body?.action === "remove") {
3250
- json(res, { ok: true, ...pets.removeInstalledPet(body.id) });
3251
- return true;
3252
- }
3253
- if (body?.action === "import_codex") {
3254
- json(res, { ok: true, ...pets.importFromCodex(body.ids) });
3255
- return true;
3256
- }
3257
- json(res, { ok: false, error: "Unknown pets action" }, 400);
3258
- return true;
3259
- }
3260
- json(res, { ok: false, error: "Method Not Allowed" }, 405);
3261
- } catch (error) {
3262
- json(res, { ok: false, error: error?.message || "Pet operation failed" }, 400);
3263
- }
3264
- return true;
3265
- }
3266
-
3267
- // --- subscription manager (manual billing plans, issue #460) ---
3268
- // User-entered renewal/expiry dates. Local-only store that lives next to
3269
- // queue.jsonl; distinct from usage-limits window resets and the
3270
- // auto-detected subscriptions in subscriptions.js.
3271
- if (p === "/api/functions/tokenusage-subscription-manager") {
3272
- const method = String(req.method || "GET").toUpperCase();
3273
- const manager = require("./subscription-manager");
3274
- const trackerDir = path.dirname(qp);
3275
- try {
3276
- if (method === "GET") {
3277
- json(res, { subscriptions: await manager.listSubscriptions({ trackerDir }) });
3278
- return true;
3279
- }
3280
- if (method === "POST") {
3281
- if (!isAuthorizedLocalMutation(req)) {
3282
- json(res, { ok: false, error: "Unauthorized" }, 401);
3283
- return true;
3284
- }
3285
- const body = await readJsonBody(req);
3286
- if (body?.action === "create") {
3287
- json(res, {
3288
- ok: true,
3289
- subscription: await manager.createSubscription({
3290
- trackerDir,
3291
- fields: body.subscription || body,
3292
- }),
3293
- });
3294
- return true;
3295
- }
3296
- if (body?.action === "update") {
3297
- json(res, {
3298
- ok: true,
3299
- subscription: await manager.updateSubscription({
3300
- trackerDir,
3301
- id: body.id,
3302
- fields: body.subscription || body,
3303
- }),
3304
- });
3305
- return true;
3306
- }
3307
- if (body?.action === "delete") {
3308
- json(res, {
3309
- ok: true,
3310
- ...(await manager.deleteSubscription({ trackerDir, id: body.id })),
3311
- });
3312
- return true;
3313
- }
3314
- json(res, { ok: false, error: "Unknown subscription-manager action" }, 400);
3315
- return true;
3316
- }
3317
- json(res, { ok: false, error: "Method Not Allowed" }, 405);
3318
- } catch (error) {
3319
- json(res, { ok: false, error: error?.message || "Subscription operation failed" }, 400);
3320
- }
3321
- return true;
3322
- }
3323
-
3324
- // --- skills manager ---
3325
- if (p === "/api/functions/tokenusage-skills") {
3326
- const method = String(req.method || "GET").toUpperCase();
3327
- const skills = require("./skills-manager");
3328
- try {
3329
- if (method === "GET") {
3330
- const mode = url.searchParams.get("mode") || "installed";
3331
- if (mode === "installed") {
3332
- json(res, { targets: skills.targetList(), skills: skills.listInstalledSkills() });
3333
- return true;
3334
- }
3335
- if (mode === "repos") {
3336
- json(res, { repos: skills.listRepos() });
3337
- return true;
3338
- }
3339
- if (mode === "discover") {
3340
- const force = url.searchParams.get("force") === "1";
3341
- json(res, await skills.discoverSkills({ force }));
3342
- return true;
3343
- }
3344
- if (mode === "search") {
3345
- const data = await skills.searchSkillsSh(
3346
- url.searchParams.get("q") || "",
3347
- Number(url.searchParams.get("limit") || 20),
3348
- Number(url.searchParams.get("offset") || 0),
3349
- );
3350
- json(res, data);
3351
- return true;
3352
- }
3353
- if (mode === "popular") {
3354
- const force = url.searchParams.get("force") === "1";
3355
- json(res, await skills.fetchPopularSkillsSh({ force }));
3356
- return true;
3357
- }
3358
- if (mode === "updates") {
3359
- const force = url.searchParams.get("force") === "1";
3360
- json(res, await skills.checkUpdates({ force }));
3361
- return true;
3362
- }
3363
- if (mode === "activity") {
3364
- const limit = Number(url.searchParams.get("limit") || 50);
3365
- json(res, { activity: skills.readActivity(limit) });
3366
- return true;
3367
- }
3368
- if (mode === "skill_usage") {
3369
- const force = url.searchParams.get("force") === "1";
3370
- const usage = await require("./skill-usage").scanSkillUsage({ force });
3371
- await ensurePricingLoaded();
3372
- // Join raw per-skill aggregates against installed skills so we can
3373
- // separate user-installed skills (where "dead weight" = unused) from
3374
- // Claude Code built-in tools (bash/agent/…), which dominate the logs
3375
- // and are NOT uninstallable. Cost is priced per-model (source=claude).
3376
- const installed = skills.listInstalledSkills();
3377
- const skillDirectoryLeaf = (value) =>
3378
- String(value || "").replace(/\\/g, "/").split("/").filter(Boolean).pop()?.trim().toLowerCase() || "";
3379
- const leafCounts = new Map();
3380
- for (const s of installed) {
3381
- const leaf = skillDirectoryLeaf(s.directory);
3382
- if (leaf) leafCounts.set(leaf, (leafCounts.get(leaf) || 0) + 1);
3383
- }
3384
- const installedByDirectory = new Map();
3385
- const installedByLeaf = new Map();
3386
- const nameCounts = new Map();
3387
- const installedByName = new Map();
3388
- for (const s of installed) {
3389
- const dir = String(s.directory || "").trim().toLowerCase();
3390
- if (dir) installedByDirectory.set(dir, s);
3391
- const leaf = skillDirectoryLeaf(s.directory);
3392
- if (leaf && leafCounts.get(leaf) === 1) installedByLeaf.set(leaf, s);
3393
- const name = String(s.name || "").trim().toLowerCase();
3394
- if (name) {
3395
- nameCounts.set(name, (nameCounts.get(name) || 0) + 1);
3396
- if (!installedByName.has(name)) installedByName.set(name, s);
3397
- }
3398
- }
3399
- const findInstalledSkill = (value) => {
3400
- const norm = String(value || "").trim().toLowerCase();
3401
- if (!norm) return null;
3402
- if (installedByDirectory.has(norm)) return installedByDirectory.get(norm);
3403
- if (installedByLeaf.has(norm)) return installedByLeaf.get(norm);
3404
- if (nameCounts.get(norm) === 1) return installedByName.get(norm);
3405
- if (leafCounts.get(norm) > 1) return null;
3406
- return null;
3407
- };
3408
- const usedSkillIds = new Set();
3409
- const priced = usage.skills.map((entry) => {
3410
- let cost = 0;
3411
- for (const [model, tokens] of Object.entries(entry.models || {})) {
3412
- cost += computeRowCost({ ...tokens, model, source: "claude" });
3413
- }
3414
- const match = findInstalledSkill(entry.skill);
3415
- if (match?.id) usedSkillIds.add(match.id);
3416
- return {
3417
- skill: entry.skill,
3418
- invocations: entry.invocations,
3419
- lastUsedAt: entry.lastUsedAt,
3420
- tokens: entry.tokens,
3421
- cost,
3422
- installed: Boolean(match),
3423
- skillId: match?.id || null,
3424
- directory: match?.directory || null,
3425
- };
3426
- });
3427
- // Installed skills with zero invocations = dead-weight candidates.
3428
- const unusedInstalled = installed
3429
- .filter((s) => !s.readOnly && !usedSkillIds.has(s.id))
3430
- .map((s) => ({ skillId: s.id, directory: s.directory, name: s.name }));
3431
- json(res, {
3432
- generatedAt: usage.generatedAt,
3433
- scannedFiles: usage.scannedFiles,
3434
- totalInvocations: usage.totalInvocations,
3435
- cached: usage.cached,
3436
- skills: priced,
3437
- unusedInstalled,
3438
- });
3439
- return true;
3440
- }
3441
- json(res, { error: "Unknown skills mode" }, 400);
3442
- return true;
3443
- }
3444
-
3445
- if (method === "POST") {
3446
- if (!isAuthorizedLocalMutation(req)) {
3447
- json(res, { ok: false, error: "Unauthorized" }, 401);
3448
- return true;
3449
- }
3450
- const body = await readJsonBody(req);
3451
- const action = String(body?.action || "");
3452
- if (action === "install") {
3453
- json(res, { ok: true, skill: await skills.installSkill(body.skill, body.targets || ["claude", "codex"]) });
3454
- return true;
3455
- }
3456
- if (action === "update_all") {
3457
- // Partial success is normal, so this reports per-skill rather than failing.
3458
- json(res, { ok: true, ...(await skills.updateSkills(Array.isArray(body.ids) ? body.ids : [])) });
3459
- return true;
3460
- }
3461
- if (action === "uninstall") {
3462
- json(res, { ok: true, ...(skills.uninstallSkill(body.id) || {}) });
3463
- return true;
3464
- }
3465
- if (action === "restore") {
3466
- json(res, { ok: true, skill: skills.restoreSkill(body.id) });
3467
- return true;
3468
- }
3469
- if (action === "set_targets") {
3470
- json(res, { ok: true, skill: skills.setSkillTargets(body.id, body.targets || []) });
3471
- return true;
3472
- }
3473
- if (action === "import_local") {
3474
- json(res, { ok: true, skill: skills.importLocalSkill(body.directory, body.targets || []) });
3475
- return true;
3476
- }
3477
- if (action === "delete_local") {
3478
- json(res, { ok: true, ...(skills.deleteLocalSkill(body.directory, body.targets || []) || {}) });
3479
- return true;
3480
- }
3481
- if (action === "add_repo") {
3482
- json(res, { ok: true, repo: skills.addRepo(body.repo) });
3483
- return true;
3484
- }
3485
- if (action === "remove_repo") {
3486
- json(res, { ok: true, ...(skills.removeRepo(body.owner, body.name) || {}) });
3487
- return true;
3488
- }
3489
- json(res, { ok: false, error: "Unknown skills action" }, 400);
3490
- return true;
3491
- }
3492
-
3493
- json(res, { ok: false, error: "Method Not Allowed" }, 405);
3494
- } catch (e) {
3495
- json(res, { ok: false, error: e?.message || "Unknown skills error" }, 500);
3496
- }
3497
- return true;
3498
- }
3499
-
3500
- // --- achievements (local badges) ---
3501
- if (p === "/api/functions/tokenusage-achievements") {
3502
- const timeZoneContext = getTimeZoneContext(url);
3503
- const queueRows = await readQueueData(qp);
3504
- const { projectRows } = await readProjectUsageContext(qp, url);
3505
- json(res, {
3506
- generated_at: new Date().toISOString(),
3507
- achievements: computeLocalAchievements(queueRows, projectRows, { timeZoneContext }),
3508
- });
3509
- return true;
3510
- }
3511
-
3512
- // --- usage-limits ---
3513
- if (p === "/api/functions/token-usage-usage-limits") {
3514
- const { getUsageLimits, resetUsageLimitsCache } = require("./usage-limits");
3515
- try {
3516
- // Devin quota is opt-in (Settings > Usage & Limits > Providers). An
3517
- // explicit devin=1 without local authentication is rejected before any
3518
- // cache reset or provider work — silently downgrading it to a disabled
3519
- // response would misreport an enabled client as a disabled provider.
3520
- // Authorization is evaluated unconditionally so the check never depends
3521
- // on the user-controlled opt-in flag itself.
3522
- const localAuthorized = isAuthorizedLocalMutation(req);
3523
- const devinParam = url.searchParams.get("devin");
3524
- const devinEnabled = devinParam === "1" || devinParam === "true";
3525
- if (devinEnabled && !localAuthorized) {
3526
- json(res, { error: "Unauthorized" }, 401);
3527
- return true;
3528
- }
3529
- const refreshParam = url.searchParams.get("refresh");
3530
- const forceRefresh = refreshParam === "1" || refreshParam === "true";
3531
- if (forceRefresh) {
3532
- resetUsageLimitsCache();
3533
- }
3534
- const data = await getUsageLimits({
3535
- home: os.homedir(),
3536
- env: process.env,
3537
- platform: process.platform,
3538
- // Punches through the Claude disk fresh-cache (but not the 429
3539
- // cooldown) — an explicit user refresh should hit upstream.
3540
- forceRefresh,
3541
- devinEnabled,
3542
- });
3543
- json(res, data);
3544
- } catch (e) {
3545
- json(res, { error: e?.message || "Unknown error" }, 500);
3546
- }
3547
- return true;
3548
- }
3549
-
3550
- return false;
3551
- };
3552
- }
3553
-
3554
- module.exports = {
3555
- createLocalApiHandler,
3556
- resolveAllowedInsforgeBaseUrl,
3557
- resolveQueuePath,
3558
- // Exported for cross-consumer tests (pricing + native contract lock).
3559
- MODEL_PRICING,
3560
- getModelPricing,
3561
- computeRowCost,
3562
- ensurePricingLoaded,
3563
- // Shared legacy-row correction so every queue reader (main queue, project
3564
- // queue, wrapped aggregator) reports the same numbers for the same data.
3565
- normalizeQueueRow,
3566
- // Machine-stable identity (config.json machineId) — shared with
3567
- // `tracker device-login`; the hostname is only a human-readable label.
3568
- getOrCreateMachineId,
3569
- computeStableMachineId,
3570
- getSystemDeviceName,
3571
- // Local achievement compute — exported for test/local-achievements.test.js.
3572
- computeLocalAchievements,
3573
- LOCAL_BADGE_THRESHOLDS,
3574
- };