@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 1,258 lines 48.7 KB
Raw
1 import {
2 type ProviderInstanceConfig,
3 ProviderInstanceId,
4 USAGE_CONTRACT_VERSION,
5 type ServerSettings as ServerSettingsValue,
6 type UsageProviderKind,
7 type UsageSource,
8 type UsagePricing,
9 type UsageSummary,
10 type UsageSummaryInput,
11 UsageReadError,
12 } from "@t3tools/contracts";
13 import * as HostProcess from "@t3tools/shared/HostProcess";
14 import * as Cause from "effect/Cause";
15 import * as Clock from "effect/Clock";
16 import * as Context from "effect/Context";
17 import * as DateTime from "effect/DateTime";
18 import * as Deferred from "effect/Deferred";
19 import * as Effect from "effect/Effect";
20 import * as Exit from "effect/Exit";
21 import * as Fiber from "effect/Fiber";
22 import * as FileSystem from "effect/FileSystem";
23 import * as Layer from "effect/Layer";
24 import * as Option from "effect/Option";
25 import * as Path from "effect/Path";
26 import * as Schema from "effect/Schema";
27 import * as Semaphore from "effect/Semaphore";
28 import { HttpClient, HttpClientResponse } from "effect/http";
29
30 import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
31 import { formatMalformedUsageWarning } from "@t3tools/shared/usageFormat";
32 import * as ServerConfig from "../config.ts";
33 import * as ServerSettings from "../serverSettings.ts";
34 import { BUILT_IN_USAGE_DRIVERS, type BuiltInUsageReadersEnv } from "../provider/builtInDrivers.ts";
35 import type { ProviderDriver } from "@t3tools/provider-core/server/driver";
36 import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
37 import type {
38 ProviderUsageInstance,
39 TranscriptUsageFormat,
40 UsageRecord,
41 } from "@t3tools/provider-core/server/usage";
42 import { canonicalModelFilter, resolveModelAliases, UsageAggregator } from "./usageAggregation.ts";
43 import {
44 createOverrideRateTable,
45 isUnlistedModel,
46 parseRateTable,
47 type RateTable,
48 } from "./usagePricing.ts";
49 import {
50 DESKTOP_HOST_SCOPE_ENV,
51 listTranscriptFiles,
52 readDirectoryVolumeId,
53 readPhysicalSourceId,
54 readTranscriptRecords,
55 } from "./usageTranscriptReader.ts";
56 import {
57 decodeScanCache,
58 dedupeWithinFile,
59 LEGACY_SCAN_CACHE_FILE_NAMES,
60 makeScanCacheWriter,
61 pruneScanCache,
62 SCAN_CACHE_FILE_NAME,
63 type CachedFile,
64 type ScanCache,
65 } from "./usageScanCache.ts";
66
67 const LITELLM_RATES_URL =
68 "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
69 const LITELLM_RATES_API_URL =
70 "https://api.github.com/repos/BerriAI/litellm/contents/model_prices_and_context_window.json?ref=main";
71
72 const RATES_TTL_MS = 24 * 60 * 60 * 1000;
73
74 const RATES_REFRESH_FLOOR_MS = 60 * 1000;
75
76 // mtime slack includes writes just before local midnight on the window's first day.
77 const MTIME_SLACK_MS = 36 * 60 * 60 * 1000;
78 const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000;
79
80 const STALE_TEMP_DIRECTORY_MS = 60 * 60 * 1000;
81
82 const TRANSCRIPT_READ_CONCURRENCY = 4;
83
84 const transcriptReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
85 driver.usage?.kind === "transcripts" ? [{ driver, reader: driver.usage }] : [],
86 );
87
88 const scanReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
89 driver.usage?.kind === "scan" ? [{ driver, reader: driver.usage }] : [],
90 );
91
92 const providerRateModels = new Map(
93 BUILT_IN_USAGE_DRIVERS.flatMap(({ usage }) =>
94 usage?.rateModel === undefined ? [] : [[usage.provider, usage.rateModel] as const],
95 ),
96 );
97
98 const transcriptFormats = new Map(
99 transcriptReaders.map(({ reader }) => [reader.provider, reader.format] as const),
100 );
101
102 interface TranscriptSource {
103 readonly provider: UsageProviderKind;
104 readonly format: TranscriptUsageFormat<unknown>;
105 readonly dir: string;
106 readonly volumeId: string;
107 readonly physicalSourceId?: string;
108 readonly fileName?: string;
109 }
110
111 const RatesCacheFile = Schema.Struct({
112 fetchedAtMs: Schema.Number,
113 document: Schema.Unknown,
114 });
115 const decodeRatesCache = Schema.decodeUnknownEffect(Schema.fromJsonString(RatesCacheFile));
116 const encodeRatesCache = Schema.encodeEffect(Schema.fromJsonString(RatesCacheFile));
117
118 const ScanCacheJson = Schema.fromJsonString(Schema.Unknown);
119 const decodeScanCacheFile = Schema.decodeUnknownEffect(ScanCacheJson);
120 const encodeUsageRecordKey = Schema.encodeSync(ScanCacheJson);
121 const CachedSource = Schema.Struct({ dir: Schema.String, volumeId: Schema.String });
122
123 function isLaterRead(a: CachedFile, b: CachedFile): boolean {
124 return a.mtimeMs > b.mtimeMs || (a.mtimeMs === b.mtimeMs && a.size > b.size);
125 }
126
127 const sharedSessionProviders: ReadonlySet<UsageProviderKind> = new Set(
128 transcriptReaders.flatMap(({ reader }) =>
129 reader.format.sharedSessionsAcrossFiles ? [reader.provider] : [],
130 ),
131 );
132
133 // Only shared sessions need cross-file dedupe; per-file occurrence counts make other keys unique.
134 function sharedSessions(
135 files: readonly { readonly records: readonly UsageRecord[] }[],
136 ): ReadonlySet<string> {
137 const firstFile = new Map<string, number>();
138 const shared = new Set<string>();
139 for (const [index, file] of files.entries()) {
140 let previous = "";
141 for (const { provider, sessionId } of file.records) {
142 if (!sharedSessionProviders.has(provider) || sessionId === previous || sessionId.length === 0)
143 continue;
144 previous = sessionId;
145 const first = firstFile.get(sessionId);
146 if (first === undefined) firstFile.set(sessionId, index);
147 else if (first !== index) shared.add(sessionId);
148 }
149 }
150 return shared;
151 }
152 const decodeCachedSources = Schema.decodeUnknownOption(
153 Schema.Struct({
154 sources: Schema.Record(Schema.String, CachedSource),
155 coveredSince: Schema.optional(Schema.Record(Schema.String, Schema.Number)),
156 }),
157 );
158
159 export class UsageService extends Context.Service<
160 UsageService,
161 {
162 readonly readSummary: (input: UsageSummaryInput) => Effect.Effect<UsageSummary, UsageReadError>;
163 readonly refreshRates: Effect.Effect<UsagePricing>;
164 }
165 >()("t3/usage/UsageService") {}
166
167 export const make = Effect.gen(function* () {
168 const fileSystem = yield* FileSystem.FileSystem;
169 const path = yield* Path.Path;
170 const config = yield* ServerConfig.ServerConfig;
171 const settingsService = yield* ServerSettings.ServerSettingsService;
172 const httpClient = yield* HttpClient.HttpClient;
173 const hostEnvironment = yield* HostProcess.Environment;
174 const hostPlatform = yield* HostProcess.Platform;
175 const hostScope = hostEnvironment[DESKTOP_HOST_SCOPE_ENV]?.trim();
176 // A mount or a directory's on-disk name does not change under a running server.
177 // A miss is not cached: the directory may not exist yet.
178 const physicalSourceIds = new Map<string, Promise<string | undefined>>();
179 const physicalSourceId = (dir: string) =>
180 Effect.promise(() => {
181 const cached = physicalSourceIds.get(dir);
182 if (cached !== undefined) return cached;
183 const id = readPhysicalSourceId(dir, hostScope, hostPlatform);
184 physicalSourceIds.set(dir, id);
185 void id.then((resolved) => {
186 if (resolved === undefined && physicalSourceIds.get(dir) === id) {
187 physicalSourceIds.delete(dir);
188 }
189 });
190 return id;
191 });
192 const hostname = yield* HostProcess.Hostname;
193 const readerContext = yield* Effect.context<BuiltInUsageReadersEnv>();
194
195 const fileCache: ScanCache = new Map();
196 // Reads from concurrent windows can still hold the original record after it is corrected.
197 const resolvedRecords = new WeakMap<UsageRecord, UsageRecord>();
198 const correctedRecord = (record: UsageRecord) => resolvedRecords.get(record) ?? record;
199 const correctedBatch = (records: readonly UsageRecord[]) =>
200 records.some((record) => resolvedRecords.has(record)) ? records.map(correctedRecord) : records;
201 const sourceCache = new Map<string, typeof CachedSource.Type>();
202 // Earliest window start a complete scan read each transcript directory from.
203 const coveredSince = new Map<string, number>();
204 const coverageKey = (source: TranscriptSource) => `${source.provider}\0${source.dir}`;
205 let cacheDirty = false;
206 const cover = (source: TranscriptSource, windowStartMs: number) => {
207 if ((coveredSince.get(coverageKey(source)) ?? Infinity) <= windowStartMs) return;
208 coveredSince.set(coverageKey(source), windowStartMs);
209 cacheDirty = true;
210 };
211 const isWithinDirectory = (filePath: string, dir: string) => {
212 const relative = path.relative(dir, filePath);
213 return relative !== ".." && !relative.startsWith(".." + path.sep) && !path.isAbsolute(relative);
214 };
215
216 const ratesCachePath = path.join(config.stateDir, "usage-model-rates.json");
217 const scanCachePath = path.join(config.stateDir, SCAN_CACHE_FILE_NAME);
218 const writeCacheFile = (filePath: string, contents: string | Iterable<string>) =>
219 writeFileStringAtomically({ filePath, contents }).pipe(
220 Effect.provideService(FileSystem.FileSystem, fileSystem),
221 Effect.provideService(Path.Path, path),
222 );
223 const hostIdPath = path.join(config.baseDir, "usage-host-id");
224 const explicitHostId = hostEnvironment["T3CODE_HOST_ID"]?.trim();
225 const hostId =
226 explicitHostId ||
227 (yield* fileSystem.readFileString(hostIdPath).pipe(
228 Effect.catchIf(
229 (error) => error.reason._tag === "NotFound",
230 () =>
231 Effect.scoped(
232 Effect.gen(function* () {
233 yield* fileSystem.makeDirectory(config.baseDir, { recursive: true });
234 const tempDirectory = yield* fileSystem.makeTempDirectoryScoped({
235 directory: config.baseDir,
236 prefix: "usage-host-id.",
237 });
238 const tempFile = path.join(tempDirectory, "identity");
239 yield* fileSystem.writeFileString(tempFile, `${hostname}\n`);
240 // Publish complete contents without replacing another initializer's identity.
241 yield* fileSystem.link(tempFile, hostIdPath).pipe(
242 Effect.catchIf(
243 (error) => error.reason._tag === "AlreadyExists",
244 () => Effect.void,
245 ),
246 );
247 return yield* fileSystem.readFileString(hostIdPath);
248 }),
249 ),
250 ),
251 Effect.map((value) => value.trim() || hostname),
252 Effect.orElseSucceed(() => hostname),
253 ));
254 let rates: RateTable = new Map();
255 let ratesFetchedAtMs: number | null = null;
256 let ratesLastFailedAtMs: number | null = null;
257 let ratesStatus: UsagePricing["status"] = "unavailable";
258 const ratesLock = yield* Semaphore.make(1);
259
260 const pricing = (): UsagePricing => ({
261 status: ratesStatus,
262 source: LITELLM_RATES_URL,
263 fetchedAt:
264 ratesFetchedAtMs === null ? null : DateTime.formatIso(DateTime.makeUnsafe(ratesFetchedAtMs)),
265 knownModels: rates.size,
266 });
267
268 const fetchRates = (url: string, accept = "application/json") =>
269 httpClient.get(url, { headers: { accept } }).pipe(
270 Effect.flatMap(HttpClientResponse.filterStatusOk),
271 Effect.flatMap((response) => response.json),
272 Effect.map((document) => {
273 const parsed = parseRateTable(document);
274 return parsed.size > 0 ? { document, parsed } : null;
275 }),
276 Effect.timeout(10_000),
277 Effect.catchCause(() => Effect.succeed(null)),
278 );
279
280 // Outside ratesLock, which a slow fetch holds: a provisional answer prices with these.
281 const loadSavedRates = yield* Effect.cached(
282 fileSystem.readFileString(ratesCachePath).pipe(
283 Effect.flatMap((raw) => decodeRatesCache(raw)),
284 Effect.map((saved) => {
285 const parsed = parseRateTable(saved.document);
286 if (parsed.size === 0) return;
287 rates = parsed;
288 ratesFetchedAtMs = saved.fetchedAtMs;
289 ratesStatus = "cached";
290 }),
291 Effect.ignoreCause,
292 ),
293 );
294
295 const loadRates = Effect.fn("UsageService.loadRates")(function* (force: boolean) {
296 const now = yield* Clock.currentTimeMillis;
297 const maxAgeMs = force ? RATES_REFRESH_FLOOR_MS : RATES_TTL_MS;
298 yield* loadSavedRates;
299 if (ratesFetchedAtMs !== null && now - ratesFetchedAtMs < maxAgeMs) return;
300 if (ratesLastFailedAtMs !== null && now - ratesLastFailedAtMs < RATES_REFRESH_FLOOR_MS) return;
301
302 let fetched = yield* fetchRates(LITELLM_RATES_URL);
303 if (fetched === null) {
304 fetched = yield* fetchRates(LITELLM_RATES_API_URL, "application/vnd.github.raw+json");
305 }
306 if (fetched === null) {
307 ratesLastFailedAtMs = yield* Clock.currentTimeMillis;
308 if (rates.size > 0) ratesStatus = "cached";
309 return;
310 }
311
312 rates = fetched.parsed;
313 ratesFetchedAtMs = now;
314 ratesLastFailedAtMs = null;
315 ratesStatus = "fresh";
316
317 yield* encodeRatesCache({ fetchedAtMs: now, document: fetched.document }).pipe(
318 Effect.flatMap((contents) => writeCacheFile(ratesCachePath, contents)),
319 Effect.ignoreCause,
320 );
321 });
322
323 const ensureRates = (force: boolean) => ratesLock.withPermit(loadRates(force));
324
325 // Rates are kept for RATES_TTL_MS, so a model released after the table was fetched would stay unpriced
326 // that long. A scan that meets a model the table does not list refetches early: once per model against
327 // a fresh table, with loadRates keeping RATES_REFRESH_FLOOR_MS between attempts.
328 const checkedUnlistedModels = new Set<string>();
329 const refreshForUnlistedModels = Effect.fn("UsageService.refreshForUnlistedModels")(function* (
330 models: ReadonlySet<string>,
331 ) {
332 const unlisted = [...models].filter(
333 (model) => !checkedUnlistedModels.has(model) && isUnlistedModel(rates, model),
334 );
335 if (unlisted.length === 0) return;
336 yield* ensureRates(true);
337 const now = yield* Clock.currentTimeMillis;
338 // A failed fetch leaves the models unchecked, so a later scan tries again.
339 if (ratesFetchedAtMs === null || now - ratesFetchedAtMs >= RATES_REFRESH_FLOOR_MS) return;
340 for (const model of unlisted) checkedUnlistedModels.add(model);
341 });
342
343 const refreshRates = ensureRates(true).pipe(
344 Effect.map(pricing),
345 Effect.withSpan("UsageService.refreshRates"),
346 );
347
348 const readSettings = settingsService.getSettings.pipe(
349 Effect.catchCause(
350 (cause) =>
351 new UsageReadError({
352 reason: "scanFailed",
353 detail: "Server settings could not be read.",
354 cause: Cause.squash(cause),
355 }),
356 ),
357 );
358
359 const usageInstances = Effect.fn("UsageService.usageInstances")(function* <Config>(
360 driver: ProviderDriver<Config, unknown, unknown>,
361 settings: ServerSettingsValue,
362 ) {
363 const entries: Array<
364 readonly [ProviderInstanceId, Pick<ProviderInstanceConfig, "config" | "environment">, boolean]
365 > = Object.entries(settings.providerInstances)
366 .filter(([, instance]) => instance.driver === driver.driverKind)
367 .map(([id, instance]) => [ProviderInstanceId.make(id), instance, true] as const);
368 // History in the driver's default home stays counted after every instance moves elsewhere; readers dedupe shared directories.
369 entries.push([ProviderInstanceId.make(driver.driverKind), {}, false]);
370 const decodeConfig = Schema.decodeUnknownOption(driver.configSchema);
371 return yield* Effect.forEach(
372 entries,
373 Effect.fnUntraced(function* ([instanceId, instance, configured]) {
374 const instanceConfig: ProviderUsageInstance<Config> = {
375 instanceId,
376 config: Option.getOrUndefined(decodeConfig(instance.config ?? {})),
377 environment: yield* mergeProviderInstanceEnvironment(
378 instance.environment,
379 hostEnvironment,
380 ),
381 configured,
382 };
383 return instanceConfig;
384 }),
385 );
386 });
387
388 const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* (
389 settings: ServerSettingsValue,
390 retentionCutoffMs: number,
391 awaitRefresh: boolean,
392 ) {
393 const dirs: Array<TranscriptSource> = [];
394 const pending: Array<ScannedDir> = [];
395 const seen = new Set<string>();
396 for (const { driver, reader } of transcriptReaders) {
397 const { provider, format } = reader;
398 const directories = yield* Effect.forEach(
399 yield* usageInstances(driver, settings),
400 (instance) => reader.directories(instance, { awaitRefresh }),
401 );
402 for (const { dir: directory, fileName, refreshing } of directories.flat()) {
403 if (refreshing) {
404 const key = `${provider}\0${directory}`;
405 if (seen.has(key)) continue;
406 seen.add(key);
407 pending.push({ provider, dir: directory, volumeId: "", files: [], refreshing });
408 continue;
409 }
410 const sourceKey = provider + "\0" + directory;
411 const previous = sourceCache.get(sourceKey);
412 const dir = yield* fileSystem
413 .realPath(directory)
414 .pipe(Effect.orElseSucceed(() => previous?.dir ?? directory));
415 const currentVolumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
416 const hasRetainedHistory = fileCache
417 .entries()
418 .some(
419 ([filePath, entry]) =>
420 entry.provider === provider &&
421 entry.mtimeMs >= retentionCutoffMs &&
422 (entry.records.length + entry.tailRecords.length > 0 || entry.malformedRecords > 0) &&
423 isWithinDirectory(filePath, dir),
424 );
425 const volumeId =
426 previous?.dir === dir && (hasRetainedHistory || !currentVolumeId)
427 ? previous.volumeId || currentVolumeId
428 : currentVolumeId;
429 if (previous?.dir !== dir || previous.volumeId !== volumeId) {
430 sourceCache.set(sourceKey, { dir, volumeId });
431 cacheDirty = true;
432 }
433 const key = `${provider}\0${dir}`;
434 if (seen.has(key)) continue;
435 seen.add(key);
436 const sourceId = yield* physicalSourceId(dir);
437 dirs.push({
438 provider,
439 format,
440 dir,
441 volumeId,
442 ...(sourceId === undefined ? {} : { physicalSourceId: sourceId }),
443 ...(fileName === undefined ? {} : { fileName }),
444 });
445 }
446 }
447 return { dirs, pending };
448 });
449
450 // Share first loads so concurrent readers cannot scan against a still-empty cache.
451 const ensureScanCacheLoaded = yield* Effect.cached(
452 Effect.gen(function* () {
453 const readDocument = (filePath: string) =>
454 fileSystem.readFileString(filePath).pipe(
455 Effect.flatMap((raw) => decodeScanCacheFile(raw)),
456 Effect.catchCause(() => Effect.succeed(null)),
457 );
458 let document = yield* readDocument(scanCachePath);
459 const current = document !== null;
460 for (const fileName of LEGACY_SCAN_CACHE_FILE_NAMES) {
461 if (document !== null) break;
462 document = yield* readDocument(path.join(config.stateDir, fileName));
463 cacheDirty = document !== null;
464 }
465 if (document === null) return;
466 const decoded = decodeScanCache(document, transcriptFormats);
467 for (const [path, entry] of decoded) fileCache.set(path, entry);
468 const saved = (document as { readonly files?: unknown }).files;
469 const complete =
470 typeof saved === "object" && saved !== null && Object.keys(saved).length === decoded.size;
471 const sources = decodeCachedSources(document);
472 if (Option.isSome(sources)) {
473 for (const [key, source] of Object.entries(sources.value.sources))
474 sourceCache.set(key, source);
475 // Coverage describes the saved files, so it needs every one, in this version's format.
476 if (current && complete && fileCache.size > 0) {
477 for (const [key, since] of Object.entries(sources.value.coveredSince ?? {}))
478 coveredSince.set(key, since);
479 }
480 }
481 }),
482 );
483
484 const writeScanCache = makeScanCacheWriter();
485 // Serialize writes so concurrently finished window scans cannot overwrite newer snapshots.
486 const persistLock = yield* Semaphore.make(1);
487
488 // The saved cache supersedes older versions. Temp folders an aborted write left behind are
489 // removed once idle for an hour, so a write in progress from another server survives.
490 let supersededFilesRemoved = false;
491 const removeSupersededCacheFiles = Effect.gen(function* () {
492 for (const fileName of LEGACY_SCAN_CACHE_FILE_NAMES) {
493 yield* fileSystem.remove(path.join(config.stateDir, fileName)).pipe(Effect.ignore);
494 }
495 const tempPrefixes = [SCAN_CACHE_FILE_NAME, ...LEGACY_SCAN_CACHE_FILE_NAMES].map(
496 (fileName) => `${fileName}.`,
497 );
498 const staleBeforeMs = (yield* Clock.currentTimeMillis) - STALE_TEMP_DIRECTORY_MS;
499 for (const name of yield* fileSystem.readDirectory(config.stateDir)) {
500 if (!tempPrefixes.some((prefix) => name.startsWith(prefix))) continue;
501 const tempPath = path.join(config.stateDir, name);
502 yield* fileSystem.stat(tempPath).pipe(
503 Effect.flatMap((info) => {
504 const mtimeMs = Option.getOrUndefined(info.mtime)?.getTime();
505 return info.type === "Directory" && mtimeMs !== undefined && mtimeMs <= staleBeforeMs
506 ? fileSystem.remove(tempPath, { recursive: true })
507 : Effect.void;
508 }),
509 Effect.ignore,
510 );
511 }
512 }).pipe(Effect.ignore);
513
514 // Clear dirty before encoding to retain concurrent changes; restore it on failure for retry.
515 const persistCaches = Effect.gen(function* () {
516 if (cacheDirty) {
517 cacheDirty = false;
518 yield* Effect.sync(() =>
519 writeScanCache(fileCache, {
520 sources: Object.fromEntries(sourceCache),
521 coveredSince: Object.fromEntries(coveredSince),
522 }),
523 ).pipe(
524 Effect.flatMap((contents) => writeCacheFile(scanCachePath, contents)),
525 Effect.andThen(
526 Effect.suspend(() => {
527 if (supersededFilesRemoved) return Effect.void;
528 supersededFilesRemoved = true;
529 return removeSupersededCacheFiles;
530 }),
531 ),
532 Effect.catchCause(() =>
533 Effect.sync(() => {
534 cacheDirty = true;
535 }),
536 ),
537 );
538 }
539 }).pipe(persistLock.withPermit, Effect.withSpan("UsageService.persistCaches"));
540
541 const pendingPersists = new Set<Fiber.Fiber<void>>();
542 const schedulePersist = Effect.forkDetach(persistCaches).pipe(
543 Effect.map((fiber) => {
544 pendingPersists.add(fiber);
545 fiber.addObserver(() => pendingPersists.delete(fiber));
546 }),
547 );
548 const awaitPersisted = Effect.suspend(() => Fiber.awaitAll([...pendingPersists])).pipe(
549 Effect.asVoid,
550 );
551 yield* Effect.addFinalizer(() => awaitPersisted);
552
553 /**
554 * Reads run concurrently, and the caller stores updates in walk order rather
555 * than completion order: saved records of deleted transcripts aggregate in
556 * cache order, where the first copy of a duplicate decides its bucket. Untraced: a scan
557 * reads thousands of files, so scanTranscriptDir records the counts instead.
558 */
559 const readFileRecords = Effect.fnUntraced(function* (
560 filePath: string,
561 size: number,
562 mtimeMs: number,
563 provider: UsageProviderKind,
564 format: TranscriptUsageFormat<unknown>,
565 ): Effect.fn.Return<{
566 readonly records: readonly UsageRecord[];
567 readonly malformedRecords: number;
568 readonly failed?: true;
569 readonly update?: { readonly entry: CachedFile; readonly replaces: CachedFile | undefined };
570 }> {
571 const cached = fileCache.get(filePath);
572 // Provider is part of the identity: if both providers were ever pointed
573 // at one directory, a hit parsed by the other parser must not be reused.
574 if (
575 cached &&
576 cached.size === size &&
577 cached.mtimeMs === mtimeMs &&
578 cached.provider === provider
579 ) {
580 return {
581 records:
582 cached.tailRecords.length === 0
583 ? cached.records
584 : [...cached.records, ...cached.tailRecords],
585 malformedRecords: cached.malformedRecords,
586 };
587 }
588
589 // Only a strictly grown file may resume. Same size with a new mtime, or
590 // a shrunken file, means rewritten content; re-parse it whole.
591 const resumeFrom =
592 cached !== undefined && cached.provider === provider && size > cached.size
593 ? cached.position
594 : undefined;
595
596 const parsed = yield* Effect.promise(() => readTranscriptRecords(filePath, format, resumeFrom));
597 // A read failure is not an empty transcript: caching it under this
598 // (size, mtime) would silently drop the file's usage until it changes.
599 if (parsed === null)
600 return {
601 records: cached?.provider === provider ? [...cached.records, ...cached.tailRecords] : [],
602 malformedRecords: cached?.provider === provider ? cached.malformedRecords : 0,
603 failed: true,
604 };
605
606 const identity = (record: UsageRecord) =>
607 encodeUsageRecordKey([
608 record.provider,
609 record.sessionId,
610 record.timestampMs,
611 record.model,
612 record.rateModel,
613 record.totals,
614 record.reportedCostUsd,
615 record.dedupeKey,
616 ]);
617 if (!parsed.resumed && cached !== undefined && cached.provider === provider) {
618 // A whole-file re-parse re-emits saved requests unresolved. Keep their saved tier
619 // corrections, since the history that resolved them may no longer be available.
620 // Equal requests keep their own corrections by matching occurrences in order.
621 const saved = new Map<string, UsageRecord[]>();
622 for (const previous of [...cached.records, ...cached.tailRecords]) {
623 const corrected = correctedRecord(previous);
624 const key = identity(corrected);
625 const occurrences = saved.get(key);
626 if (occurrences === undefined) saved.set(key, [corrected]);
627 else occurrences.push(corrected);
628 }
629 for (const record of [...parsed.records, ...parsed.tailRecords]) {
630 const corrected = saved.get(identity(record))?.shift();
631 if (
632 record.unresolvedServiceTierTurnId !== undefined &&
633 corrected !== undefined &&
634 corrected.unresolvedServiceTierTurnId === undefined
635 ) {
636 resolvedRecords.set(record, corrected);
637 }
638 }
639 }
640 if (parsed.resumed && cached !== undefined && cached.tailRecords.length > 0) {
641 // Completing a tentative line reparses it. Reuse its saved correction only when the
642 // same usage is emitted again, without putting inferred tiers in the parser state.
643 const reparsed = [...parsed.records, ...parsed.tailRecords];
644 for (const [index, previous] of cached.tailRecords.entries()) {
645 const record = reparsed[index];
646 const corrected = correctedRecord(previous);
647 if (
648 record?.unresolvedServiceTierTurnId !== undefined &&
649 corrected.unresolvedServiceTierTurnId === undefined &&
650 identity(record) === identity(corrected)
651 ) {
652 resolvedRecords.set(record, corrected);
653 }
654 }
655 }
656 const base = parsed.resumed && cached !== undefined ? cached.records : [];
657 const records = dedupeWithinFile([...base, ...parsed.records]);
658 const tailRecords = parsed.tailRecords;
659
660 return {
661 records: tailRecords.length === 0 ? records : [...records, ...tailRecords],
662 malformedRecords: parsed.malformedRecords,
663 update: {
664 entry: {
665 size,
666 mtimeMs,
667 provider,
668 records,
669 tailRecords,
670 malformedRecords: parsed.malformedRecords,
671 position: parsed.position,
672 },
673 replaces: cached,
674 },
675 };
676 });
677
678 interface ScannedDir {
679 readonly provider: UsageProviderKind;
680 readonly dir: string;
681 readonly volumeId: string;
682 readonly physicalSourceId?: string;
683 readonly hostId?: string;
684 readonly status?: UsageSource["status"];
685 readonly message?: string;
686 readonly files:
687 | readonly {
688 readonly path: string;
689 readonly records: readonly UsageRecord[];
690 readonly malformedRecords: number;
691 }[]
692 | null;
693 /** Every transcript path the walk listed, in walk order, whatever its mtime. */
694 readonly walked?: readonly string[];
695 readonly refreshing?: true;
696 }
697
698 const scanTranscriptDir = Effect.fn("UsageService.scanTranscriptDir")(function* (
699 source: TranscriptSource,
700 windowStartMs: number,
701 fromMemory: boolean,
702 ) {
703 const { provider, format, dir, volumeId, physicalSourceId, fileName } = source;
704 const identity = {
705 provider,
706 dir,
707 volumeId,
708 ...(physicalSourceId === undefined ? {} : { physicalSourceId }),
709 };
710 const exists = yield* fileSystem
711 .exists(dir)
712 .pipe(Effect.catchCause(() => Effect.succeed(false)));
713 if (!exists) {
714 cover(source, windowStartMs);
715 return { ...identity, files: null } satisfies ScannedDir;
716 }
717 // scanSummary aggregates the saved files, as it does for a deleted directory.
718 if (fromMemory) return { ...identity, files: [] } satisfies ScannedDir;
719 const { files, walked, failedPaths } = yield* Effect.promise(() =>
720 listTranscriptFiles(dir, windowStartMs, fileName === undefined ? undefined : { fileName }),
721 );
722 const read = yield* Effect.forEach(
723 files,
724 (file) =>
725 readFileRecords(file.path, file.size, file.mtimeMs, provider, format).pipe(
726 Effect.map((result) => ({ path: file.path, ...result })),
727 ),
728 { concurrency: TRANSCRIPT_READ_CONCURRENCY },
729 );
730 const parsedFiles = read.map(({ path, records, malformedRecords, update }) => {
731 if (update === undefined) return { path, records, malformedRecords };
732 // A scan of another window may have cached its own read of this file
733 // meanwhile. Then keep whichever read saw the later file, so a slower
734 // scan never replaces newer usage with older.
735 const current = fileCache.get(path);
736 if (
737 current === update.replaces ||
738 current === undefined ||
739 isLaterRead(update.entry, current)
740 ) {
741 fileCache.set(path, {
742 ...update.entry,
743 records: correctedBatch(update.entry.records),
744 tailRecords: correctedBatch(update.entry.tailRecords),
745 });
746 cacheDirty = true;
747 }
748 return { path, records, malformedRecords };
749 });
750 const livePaths = new Set(files.map((file) => file.path));
751 for (const file of read) {
752 if (file.failed || !file.path.endsWith(".jsonl.zst")) continue;
753 const originalPath = file.path.slice(0, -4);
754 // A successfully read archive replaces the original's saved records and diagnostics.
755 if (!livePaths.has(originalPath) && fileCache.delete(originalPath)) cacheDirty = true;
756 }
757 const unread = failedPaths + read.filter((file) => file.failed).length;
758 if (unread === 0) cover(source, windowStartMs);
759 yield* Effect.annotateCurrentSpan({
760 "usage.files": files.length,
761 "usage.files_parsed": read.filter((file) => file.update !== undefined).length,
762 });
763 return {
764 ...identity,
765 files: parsedFiles,
766 walked,
767 ...(unread > 0
768 ? {
769 status: "partial",
770 message: `${unread} transcript path(s) could not be read; usage may be incomplete.`,
771 }
772 : {}),
773 } satisfies ScannedDir;
774 });
775
776 const collectDirs = Effect.fn("UsageService.collectDirs")(function* (
777 { dirs, pending }: Effect.Success<ReturnType<typeof resolveTranscriptDirs>>,
778 windowStartMs: number,
779 settings: ServerSettingsValue,
780 retentionCutoffMs: number,
781 awaitRefresh: boolean,
782 fromMemory: boolean,
783 ) {
784 const scans = Effect.forEach(
785 scanReaders,
786 ({ driver, reader }) =>
787 usageInstances(driver, settings).pipe(
788 Effect.flatMap((instances) =>
789 reader.scan({ instances, settings, windowStartMs, retentionCutoffMs, awaitRefresh }),
790 ),
791 Effect.flatMap((sources) =>
792 Effect.forEach(sources, ({ volumeId, ...source }) =>
793 Effect.map(
794 // A reader's own volumeId names a remote account, not a local directory.
795 volumeId === undefined
796 ? Effect.all([
797 Effect.promise(() => readDirectoryVolumeId(source.dir)),
798 physicalSourceId(source.dir),
799 ])
800 : Effect.succeed<[string, string | undefined]>([volumeId, undefined]),
801 ([resolved, sourceId]): ScannedDir => ({
802 ...source,
803 files: source.files?.map((file) => ({ ...file, malformedRecords: 0 })) ?? null,
804 provider: reader.provider,
805 volumeId: resolved,
806 ...(sourceId === undefined ? {} : { physicalSourceId: sourceId }),
807 }),
808 ),
809 ),
810 ),
811 Effect.provideContext(readerContext),
812 ),
813 { concurrency: "unbounded" },
814 );
815
816 // Scan independent sources together, directories sequentially to bound open files; keep order, since the first copy decides a duplicate's bucket.
817 const [transcripts, scanDirs] = yield* Effect.all(
818 [Effect.forEach(dirs, (dir) => scanTranscriptDir(dir, windowStartMs, fromMemory)), scans],
819 { concurrency: "unbounded" },
820 );
821 const scanned: readonly ScannedDir[] = [...transcripts, ...pending, ...scanDirs.flat()];
822 return scanned;
823 });
824
825 const scanSummary = Effect.fn("UsageService.scanSummary")(function* (
826 input: UsageSummaryInput,
827 settings: ServerSettingsValue,
828 provisional: boolean,
829 ) {
830 if (input.sinceDay > input.untilDay) {
831 return yield* new UsageReadError({
832 reason: "invalidWindow",
833 detail: `sinceDay '${input.sinceDay}' is after untilDay '${input.untilDay}'`,
834 });
835 }
836
837 let hourlyWindow: { readonly sinceTimeMs: number; readonly untilTimeMs: number } | null = null;
838 if (input.resolution === "hour") {
839 const sinceTime =
840 input.sinceTime === undefined ? Option.none() : DateTime.make(input.sinceTime);
841 const untilTime =
842 input.untilTime === undefined ? Option.none() : DateTime.make(input.untilTime);
843 if (Option.isNone(sinceTime) || Option.isNone(untilTime)) {
844 return yield* new UsageReadError({
845 reason: "invalidWindow",
846 detail: "Hourly usage requires valid sinceTime and untilTime instants",
847 });
848 }
849 const sinceTimeMs = DateTime.toEpochMillis(sinceTime.value);
850 const untilTimeMs = DateTime.toEpochMillis(untilTime.value);
851 const durationMs = untilTimeMs - sinceTimeMs;
852 if (durationMs <= 0 || durationMs > MAX_HOURLY_WINDOW_MS) {
853 return yield* new UsageReadError({
854 reason: "invalidWindow",
855 detail: "Hourly usage window must be greater than zero and at most 24 hours",
856 });
857 }
858 hourlyWindow = { sinceTimeMs, untilTimeMs };
859 }
860
861 const startedAtMs = yield* Clock.currentTimeMillis;
862 yield* ensureScanCacheLoaded;
863
864 const windowStart = DateTime.make(`${input.sinceDay}T00:00:00Z`);
865 if (Option.isNone(windowStart)) {
866 return yield* new UsageReadError({
867 reason: "invalidWindow",
868 detail: `sinceDay '${input.sinceDay}' is not a valid date`,
869 });
870 }
871 const windowStartMs =
872 (hourlyWindow?.sinceTimeMs ?? DateTime.toEpochMillis(windowStart.value)) - MTIME_SLACK_MS;
873
874 const retentionCutoffMs =
875 settings.usageHistoryRetentionDays === "unlimited"
876 ? 0
877 : Math.max(0, startedAtMs - settings.usageHistoryRetentionDays * 24 * 60 * 60 * 1000);
878
879 const awaitRefresh = input.awaitRefresh === true;
880 const collect = Effect.gen(function* () {
881 const resolved = yield* resolveTranscriptDirs(settings, retentionCutoffMs, awaitRefresh).pipe(
882 Effect.provideContext(readerContext),
883 );
884 // A window no complete scan has read would show partial totals, so every directory must cover it.
885 // Saved files are keyed by path, so two providers reading one directory keep only one parse.
886 const fromMemory =
887 provisional &&
888 resolved.dirs.length > 0 &&
889 new Set(resolved.dirs.map((source) => source.dir)).size === resolved.dirs.length &&
890 resolved.dirs.every(
891 (source) => (coveredSince.get(coverageKey(source)) ?? Infinity) <= windowStartMs,
892 );
893 const [, scanned] = yield* Effect.all(
894 [
895 fromMemory ? loadSavedRates : provisional ? ensureRates(false) : Effect.void,
896 collectDirs(
897 resolved,
898 windowStartMs,
899 settings,
900 retentionCutoffMs,
901 awaitRefresh,
902 fromMemory,
903 ),
904 ],
905 { concurrency: 2 },
906 );
907 return { scannedDirs: scanned, fromMemory };
908 });
909 const [{ scannedDirs, fromMemory }] = yield* Effect.all(
910 [collect, provisional ? Effect.void : ensureRates(false)],
911 { concurrency: 2 },
912 );
913
914 const priceOverrides = createOverrideRateTable(settings.usagePriceOverrides);
915 const modelAliases = resolveModelAliases(settings.usageModelAliases);
916 const sources: UsageSource[] = [];
917
918 // Deleted transcripts' saved usage still aggregates and deduplicates normally.
919 const retainedSinceMs = Math.max(windowStartMs, retentionCutoffMs);
920 // Saved paths are files below a scanned root, so their folder decides; folders repeat thousands of times.
921 const withinDir = (dir: string) => {
922 const foldersWithin = new Map<string, boolean>();
923 return (filePath: string) => {
924 const folder = path.dirname(filePath);
925 let within = foldersWithin.get(folder);
926 if (within === undefined) {
927 within = isWithinDirectory(folder, dir);
928 foldersWithin.set(folder, within);
929 }
930 return within;
931 };
932 };
933 const filesByDir = scannedDirs.map(({ provider, dir, files }) => {
934 const retainedFiles = [...(files ?? [])];
935 const livePaths = new Set(retainedFiles.map((file) => file.path));
936 const isRetained = withinDir(dir);
937 for (const [filePath, entry] of fileCache) {
938 if (
939 entry.provider !== provider ||
940 entry.mtimeMs < retainedSinceMs ||
941 livePaths.has(filePath) ||
942 !isRetained(filePath)
943 )
944 continue;
945 retainedFiles.push({
946 path: filePath,
947 records:
948 entry.tailRecords.length === 0
949 ? entry.records
950 : [...entry.records, ...entry.tailRecords],
951 malformedRecords: entry.malformedRecords,
952 });
953 }
954 return retainedFiles;
955 });
956 // Usage two transcripts share counts in the first one, so saved files take the order an exact
957 // scan reads them in: walked files in walk order, then the directory's deleted files.
958 for (const { provider, dir, walked } of fromMemory ? [] : scannedDirs) {
959 if (walked === undefined) continue;
960 const within = withinDir(dir);
961 const saved = [...fileCache.keys()];
962 const members = new Set(
963 saved.filter(
964 (filePath) => fileCache.get(filePath)!.provider === provider && within(filePath),
965 ),
966 );
967 if (members.size < 2) continue;
968 const listed = new Set(walked);
969 const target = [
970 ...walked.filter((filePath) => members.has(filePath)),
971 ...saved.filter((filePath) => members.has(filePath) && !listed.has(filePath)),
972 ];
973 let next = 0;
974 const ordered = saved.map((filePath) => (members.has(filePath) ? target[next++]! : filePath));
975 if (ordered.every((filePath, index) => filePath === saved[index])) continue;
976 const entries = new Map(fileCache);
977 fileCache.clear();
978 for (const filePath of ordered) fileCache.set(filePath, entries.get(filePath)!);
979 cacheDirty = true;
980 }
981
982 // The models priced below, as UsageAggregator resolves them: an alias replaces rateModel.
983 const rateModels = new Set<string>();
984 for (const files of filesByDir) {
985 for (const file of files) {
986 for (const record of file.records) {
987 const alias = modelAliases.get(record.model);
988 const model = alias ?? record.model;
989 if (priceOverrides.has(model.trim())) continue;
990 rateModels.add(alias === undefined ? (record.rateModel ?? model) : model);
991 }
992 }
993 }
994 // A saved answer prices with saved rates and the full scan after it refetches.
995 if (!fromMemory) yield* refreshForUnlistedModels(rateModels);
996
997 const summaryPricing = pricing();
998
999 const aggregator = new UsageAggregator({
1000 timeZone: input.timeZone,
1001 sinceDay: input.sinceDay,
1002 untilDay: input.untilDay,
1003 resolution: input.resolution ?? "day",
1004 ...hourlyWindow,
1005 rates,
1006 priceOverrides,
1007 modelAliases,
1008 rateModel: (provider, model) => providerRateModels.get(provider)?.(model),
1009 ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }),
1010 });
1011
1012 const windowEndMs =
1013 hourlyWindow?.untilTimeMs ??
1014 Date.parse(`${input.untilDay}T00:00:00Z`) + 86400000 + MTIME_SLACK_MS;
1015 let resolvedAny = false;
1016 for (const { reader } of transcriptReaders) {
1017 if (reader.resolveServiceTiers === undefined || fromMemory) continue;
1018 const pending = new Set<UsageRecord>();
1019 for (const [index, files] of filesByDir.entries()) {
1020 if (scannedDirs[index]?.provider !== reader.provider) continue;
1021 for (const file of files) {
1022 for (const raw of file.records) {
1023 const record = correctedRecord(raw);
1024 if (
1025 record.provider === reader.provider &&
1026 record.unresolvedServiceTierTurnId !== undefined &&
1027 record.timestampMs >= windowStartMs &&
1028 record.timestampMs < windowEndMs
1029 ) {
1030 pending.add(record);
1031 }
1032 }
1033 }
1034 }
1035 if (pending.size === 0) continue;
1036 const resolved = yield* reader
1037 .resolveServiceTiers([...pending])
1038 .pipe(Effect.provideContext(readerContext));
1039 for (const [record, speed] of resolved) {
1040 const { unresolvedServiceTierTurnId: _turnId, ...rest } = record;
1041 resolvedRecords.set(record, { ...rest, speed });
1042 resolvedAny = true;
1043 }
1044 }
1045 if (resolvedAny) {
1046 for (const [filePath, entry] of fileCache) {
1047 const records = correctedBatch(entry.records);
1048 const tailRecords = correctedBatch(entry.tailRecords);
1049 if (records === entry.records && tailRecords === entry.tailRecords) continue;
1050 // Update the current entry by identity; a slow resolver cannot restore an older file.
1051 fileCache.set(filePath, { ...entry, records, tailRecords });
1052 cacheDirty = true;
1053 }
1054 }
1055 const crossFileSessions = sharedSessions(filesByDir.flat());
1056
1057 for (const [
1058 index,
1059 {
1060 provider,
1061 dir,
1062 volumeId,
1063 physicalSourceId,
1064 files,
1065 status,
1066 message,
1067 refreshing,
1068 hostId: sourceHostId,
1069 },
1070 ] of scannedDirs.entries()) {
1071 let scannedFiles = 0;
1072 let skippedFiles = 0;
1073 let malformedRecords = 0;
1074 // Distinct per directory. Buckets carry per-cell session counts, but a
1075 // session spans days and models, so clients total this figure instead.
1076 const sessionIds = new Set<string>();
1077 let counted = false;
1078
1079 for (const file of filesByDir[index] ?? []) {
1080 malformedRecords += file.malformedRecords;
1081 if (file.records.length === 0) {
1082 skippedFiles += 1;
1083 continue;
1084 }
1085 scannedFiles += 1;
1086 const eventOccurrences = new Map<string, number>();
1087 for (const rawRecord of file.records) {
1088 const record = correctedRecord(rawRecord);
1089 let usageRecord = record;
1090 if (
1091 sharedSessionProviders.has(record.provider) &&
1092 crossFileSessions.has(record.sessionId)
1093 ) {
1094 // Dedupe moved copies but retain repeated equal events within a file, including second-precision timestamps.
1095 const key = encodeUsageRecordKey([
1096 record.provider,
1097 record.sessionId,
1098 record.timestampMs,
1099 record.model,
1100 record.totals,
1101 ]);
1102 const occurrence = (eventOccurrences.get(key) ?? 0) + 1;
1103 eventOccurrences.set(key, occurrence);
1104 usageRecord = { ...record, dedupeKey: key + ":" + occurrence };
1105 }
1106 if (aggregator.add(usageRecord, dir)) {
1107 counted = true;
1108 if (record.sessionId.length > 0) sessionIds.add(record.sessionId);
1109 }
1110 }
1111 }
1112
1113 const sourceMessage =
1114 message ?? (files === null ? "No transcript directory on this environment." : null);
1115 sources.push({
1116 fingerprint: {
1117 hostId: sourceHostId ?? hostId,
1118 provider,
1119 resolvedHomePath: dir,
1120 volumeId,
1121 ...(physicalSourceId === undefined ? {} : { physicalSourceId }),
1122 },
1123 status:
1124 files === null && scannedFiles === 0
1125 ? "missing"
1126 : malformedRecords > 0
1127 ? status === undefined || status === "ok"
1128 ? "partial"
1129 : status
1130 : (status ?? "ok"),
1131 scannedFiles,
1132 skippedFiles,
1133 malformedRecords,
1134 distinctSessions: sessionIds.size,
1135 message:
1136 malformedRecords > 0
1137 ? [sourceMessage, formatMalformedUsageWarning(malformedRecords)]
1138 .filter(Boolean)
1139 .join(" ")
1140 : sourceMessage,
1141 // A saved answer misses what changed since its scan, so the client must ask again. An
1142 // empty source stays unmarked: it would only flash a loading row.
1143 ...(refreshing || (fromMemory && counted && transcriptFormats.has(provider))
1144 ? { refreshing: true }
1145 : {}),
1146 });
1147 }
1148
1149 yield* settingsService
1150 .withSettingsSnapshot((currentSettings) =>
1151 Effect.sync(() => {
1152 if (settings.usageHistoryRetentionDays === currentSettings.usageHistoryRetentionDays) {
1153 const pruned = pruneScanCache(fileCache, retentionCutoffMs);
1154 if (pruned > 0) {
1155 cacheDirty = true;
1156 // Pruned files are no longer saved, so nothing before the cutoff stays covered.
1157 for (const [key, since] of coveredSince) {
1158 if (since < retentionCutoffMs) coveredSince.set(key, retentionCutoffMs);
1159 }
1160 }
1161 }
1162 }),
1163 )
1164 .pipe(
1165 Effect.catchCause((cause) =>
1166 Effect.logWarning("failed to read settings before pruning the usage cache", { cause }),
1167 ),
1168 );
1169
1170 const aggregated = aggregator.finish();
1171 const readAt = yield* DateTime.now;
1172 const finishedAtMs = yield* Clock.currentTimeMillis;
1173
1174 const summary = {
1175 contractVersion: USAGE_CONTRACT_VERSION,
1176 readAt: DateTime.formatIso(readAt),
1177 timeZone: input.timeZone,
1178 sinceDay: input.sinceDay,
1179 untilDay: input.untilDay,
1180 buckets: aggregated.buckets,
1181 sources,
1182 pricing: summaryPricing,
1183 scanDurationMs: Math.max(0, finishedAtMs - startedAtMs),
1184 ...(input.modelFilter === undefined ? {} : { modelFilter: input.modelFilter }),
1185 } satisfies UsageSummary;
1186 return { summary, fromMemory };
1187 });
1188
1189 const readWindow = Effect.fn("UsageService.readWindow")(function* (
1190 input: UsageSummaryInput,
1191 settings: ServerSettingsValue,
1192 provisional: boolean,
1193 ) {
1194 const { summary, fromMemory } = yield* scanSummary(input, settings, provisional);
1195 // With nothing marked refreshing the client would keep a saved answer as final.
1196 return fromMemory && !summary.sources.some((source) => source.refreshing === true)
1197 ? (yield* scanSummary(input, settings, false)).summary
1198 : summary;
1199 });
1200
1201 const inflightScans = new Map<string, Deferred.Deferred<UsageSummary, UsageReadError>>();
1202
1203 const scanKey = (
1204 input: UsageSummaryInput,
1205 settings: ServerSettingsValue,
1206 provisional: boolean,
1207 ): string =>
1208 JSON.stringify([
1209 input.timeZone,
1210 input.sinceDay,
1211 input.untilDay,
1212 input.resolution ?? "day",
1213 input.sinceTime ?? null,
1214 input.untilTime ?? null,
1215 settings,
1216 input.awaitRefresh === true,
1217 provisional,
1218 input.modelFilter ?? null,
1219 ]);
1220
1221 const readSummary = Effect.fn("UsageService.readSummary")(function* (
1222 requested: UsageSummaryInput,
1223 ) {
1224 const { modelFilter: requestedFilter, ...unfiltered } = requested;
1225 const modelFilter = canonicalModelFilter(requestedFilter);
1226 const input = modelFilter === undefined ? unfiltered : { ...unfiltered, modelFilter };
1227 const settings = yield* readSettings;
1228 const provisional = input.provisional === true && input.awaitRefresh !== true;
1229 const key = scanKey(input, settings, provisional);
1230 const deferred = yield* Effect.uninterruptible(
1231 Effect.gen(function* () {
1232 const existing = inflightScans.get(key);
1233 if (existing !== undefined) return existing;
1234
1235 // Register and fork atomically so cancellation cannot leave a Deferred without a scan.
1236 const created = Deferred.makeUnsafe<UsageSummary, UsageReadError>();
1237 inflightScans.set(key, created);
1238 // Detach so caller departure cannot cancel shared scans; finished scans warm the cache.
1239 // Register writes before releasing waiters, but start writing after delivering their summary.
1240 yield* readWindow(input, settings, provisional).pipe(
1241 Effect.onExit((exit) =>
1242 Effect.sync(() => inflightScans.delete(key)).pipe(
1243 Effect.andThen(Exit.isSuccess(exit) ? schedulePersist : Effect.void),
1244 Effect.andThen(Deferred.done(created, exit)),
1245 ),
1246 ),
1247 Effect.forkDetach,
1248 );
1249 return created;
1250 }),
1251 );
1252 return yield* Deferred.await(deferred);
1253 });
1254
1255 return { readSummary, refreshRates, awaitPersisted } as const;
1256 });
1257
1258 export const layer = Layer.effect(UsageService, make);