Files
t3code/apps/server/src/usage/UsageService.ts
T

937 lines
36 KiB
TypeScript

/**
* UsageService - reads every driver's usage history and returns priced usage
* buckets.
*
* Each built-in driver with a `usage` reader contributes its sources, including
* work driven outside T3 Code. A `transcripts` reader names JSONL directories
* that this service streams itself; a `scan` reader reads its own sources.
*
* JSONL transcripts are append-only, so parsed records are memoised per file by
* `(size, mtime)`. A cold 30-day scan of ~1.4 GB lands around 2-3 seconds; warm
* scans only reparse files that changed, and a file that merely grew resumes
* from its cached parse position so only the appended bytes are read.
*
* A scan reader with a slow source (an account API) may answer from its own
* cache and mark itself `refreshing` while a background refresh runs;
* `awaitRefresh` waits for that refresh instead.
*
* @module UsageService
*/
import * as NodeOS from "node:os";
import {
type ProviderInstanceConfig,
ProviderInstanceId,
USAGE_CONTRACT_VERSION,
type ServerSettings as ServerSettingsValue,
type UsageProviderKind,
type UsageSource,
type UsagePricing,
type UsageSummary,
type UsageSummaryInput,
UsageReadError,
} from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Cause from "effect/Cause";
import * as Clock from "effect/Clock";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as Semaphore from "effect/Semaphore";
import { HttpClient, HttpClientResponse } from "effect/http";
import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite";
import * as ServerConfig from "../config.ts";
import * as ServerSettings from "../serverSettings.ts";
import { BUILT_IN_USAGE_DRIVERS, type BuiltInUsageReadersEnv } from "../provider/builtInDrivers.ts";
import type { ProviderDriver } from "@t3tools/provider-core/server/driver";
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
import type {
ProviderUsageInstance,
TranscriptUsageFormat,
UsageRecord,
} from "@t3tools/provider-core/server/usage";
import { resolveModelAliases, UsageAggregator } from "./usageAggregation.ts";
import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts";
import {
listTranscriptFiles,
readDirectoryVolumeId,
readTranscriptRecords,
} from "./usageTranscriptReader.ts";
import {
decodeScanCache,
dedupeWithinFile,
LEGACY_SCAN_CACHE_FILE_NAME,
makeScanCacheWriter,
pruneScanCache,
SCAN_CACHE_FILE_NAME,
type CachedFile,
type ScanCache,
} from "./usageScanCache.ts";
const LITELLM_RATES_URL =
"https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
/** Rates move rarely; a day-old table keeps the page working offline. */
const RATES_TTL_MS = 24 * 60 * 60 * 1000;
/** An explicit refresh ignores the TTL, but not a table fetched this recently. */
const RATES_REFRESH_FLOOR_MS = 60 * 1000;
/**
* Files are filtered by mtime before opening. The slack covers a session whose
* last write lands just before local midnight on the window's first day.
*/
const MTIME_SLACK_MS = 36 * 60 * 60 * 1000;
const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000;
/**
* The longest window the UI offers, 90 days, plus its `MTIME_SLACK_MS`, rounded
* up. Older entries are pruned.
*/
const CACHE_RETENTION_DAYS = 92;
/** Transcripts parsed at once. More gains little once the disk stays busy. */
const TRANSCRIPT_READ_CONCURRENCY = 4;
/** The transcript readers, in driver order. */
const transcriptReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
driver.usage?.kind === "transcripts" ? [{ driver, reader: driver.usage }] : [],
);
/** The scan readers, in driver order. */
const scanReaders = BUILT_IN_USAGE_DRIVERS.flatMap((driver) =>
driver.usage?.kind === "scan" ? [{ driver, reader: driver.usage }] : [],
);
/** Transcript formats by provider, for decoding the persisted scan cache. */
const transcriptFormats = new Map(
transcriptReaders.map(({ reader }) => [reader.provider, reader.format] as const),
);
/** One transcript directory to scan. */
interface TranscriptSource {
readonly provider: UsageProviderKind;
readonly format: TranscriptUsageFormat<unknown>;
readonly dir: string;
readonly volumeId: string;
readonly fileName?: string;
}
/** On-disk shape of the rate snapshot. */
const RatesCacheFile = Schema.Struct({
fetchedAtMs: Schema.Number,
document: Schema.Unknown,
});
const decodeRatesCache = Schema.decodeUnknownEffect(
Schema.fromJsonString(RatesCacheFile as unknown as Schema.Codec<typeof RatesCacheFile.Type>),
);
const encodeRatesCache = Schema.encodeEffect(
Schema.fromJsonString(RatesCacheFile as unknown as Schema.Codec<typeof RatesCacheFile.Type>),
);
/** The scan cache is narrowed by hand in `usageScanCache`, so JSON is enough here. */
const ScanCacheJson = Schema.fromJsonString(Schema.Unknown as unknown as Schema.Codec<unknown>);
const decodeScanCacheFile = Schema.decodeUnknownEffect(ScanCacheJson);
const encodeUsageRecordKey = Schema.encodeSync(ScanCacheJson);
const CachedSource = Schema.Struct({ dir: Schema.String, volumeId: Schema.String });
/** Whether `a` read a later state of its file than `b`. Transcripts only grow. */
function isLaterRead(a: CachedFile, b: CachedFile): boolean {
return a.mtimeMs > b.mtimeMs || (a.mtimeMs === b.mtimeMs && a.size > b.size);
}
/** Providers whose format lets one session's records appear in several files. */
const sharedSessionProviders: ReadonlySet<UsageProviderKind> = new Set(
transcriptReaders.flatMap(({ reader }) =>
reader.format.sharedSessionsAcrossFiles ? [reader.provider] : [],
),
);
/**
* Sessions of `sharedSessionProviders` with records in more than one file,
* such as a rollout that moved after it was read. Only these need cross-file
* dedupe keys: within one file the occurrence count already keeps every key
* unique, so keying the rest would only build and hash a string for each of
* their records.
*/
function sharedSessions(
files: readonly { readonly records: readonly UsageRecord[] }[],
): ReadonlySet<string> {
const firstFile = new Map<string, number>();
const shared = new Set<string>();
for (const [index, file] of files.entries()) {
let previous = "";
for (const { provider, sessionId } of file.records) {
if (!sharedSessionProviders.has(provider) || sessionId === previous || sessionId.length === 0)
continue;
previous = sessionId;
const first = firstFile.get(sessionId);
if (first === undefined) firstFile.set(sessionId, index);
else if (first !== index) shared.add(sessionId);
}
}
return shared;
}
const decodeCachedSources = Schema.decodeUnknownOption(
Schema.Struct({ sources: Schema.Record(Schema.String, CachedSource) }),
);
export class UsageService extends Context.Service<
UsageService,
{
readonly readSummary: (input: UsageSummaryInput) => Effect.Effect<UsageSummary, UsageReadError>;
/** Refetches the rate table ahead of its TTL. See `ensureRates`. */
readonly refreshRates: Effect.Effect<UsagePricing>;
}
>()("t3/usage/UsageService") {}
const EMPTY_PRICING: UsagePricing = {
status: "unavailable",
source: LITELLM_RATES_URL,
fetchedAt: null,
knownModels: 0,
};
/** Empty summary, for suites that only need the RPC surface to resolve. */
const layerTest = Layer.succeed(
UsageService,
UsageService.of({
readSummary: (input) =>
Effect.succeed({
contractVersion: USAGE_CONTRACT_VERSION,
readAt: "1970-01-01T00:00:00.000Z",
timeZone: input.timeZone,
sinceDay: input.sinceDay,
untilDay: input.untilDay,
buckets: [],
sources: [],
pricing: EMPTY_PRICING,
scanDurationMs: 0,
}),
refreshRates: Effect.succeed(EMPTY_PRICING),
}),
);
export const make = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const config = yield* ServerConfig.ServerConfig;
const settingsService = yield* ServerSettings.ServerSettingsService;
const httpClient = yield* HttpClient.HttpClient;
const hostEnvironment = yield* HostProcess.Environment;
// The readers yield their own services; scans run them against this context.
const readerContext = yield* Effect.context<BuiltInUsageReadersEnv>();
const fileCache: ScanCache = new Map();
const sourceCache = new Map<string, typeof CachedSource.Type>();
let cacheDirty = false;
const isWithinDirectory = (filePath: string, dir: string) => {
const relative = path.relative(dir, filePath);
return relative !== ".." && !relative.startsWith(".." + path.sep) && !path.isAbsolute(relative);
};
const ratesCachePath = path.join(config.stateDir, "usage-model-rates.json");
const scanCachePath = path.join(config.stateDir, SCAN_CACHE_FILE_NAME);
const legacyScanCachePath = path.join(config.stateDir, LEGACY_SCAN_CACHE_FILE_NAME);
const writeCacheFile = (filePath: string, contents: string) =>
writeFileStringAtomically({ filePath, contents }).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
);
let rates: RateTable = new Map();
let ratesFetchedAtMs: number | null = null;
let ratesStatus: UsagePricing["status"] = "unavailable";
// One fetch at a time. A burst of refreshes from several clients waits on
// the first fetch and then sees a table young enough to skip its own.
const ratesLock = yield* Semaphore.make(1);
const pricing = (): UsagePricing => ({
status: ratesStatus,
source: LITELLM_RATES_URL,
fetchedAt:
ratesFetchedAtMs === null ? null : DateTime.formatIso(DateTime.makeUnsafe(ratesFetchedAtMs)),
knownModels: rates.size,
});
/**
* Loads the LiteLLM rate table, preferring a fresh copy and falling back to
* the on-disk snapshot. With neither, every model reports as unpriced rather
* than the page failing. `force` refetches inside the TTL so a model that
* LiteLLM added since the last fetch gets priced now.
*/
const loadRates = Effect.fn("UsageService.loadRates")(function* (force: boolean) {
const now = yield* Clock.currentTimeMillis;
const maxAgeMs = force ? RATES_REFRESH_FLOOR_MS : RATES_TTL_MS;
if (ratesFetchedAtMs !== null && now - ratesFetchedAtMs < maxAgeMs) return;
if (ratesFetchedAtMs === null) {
const fromDisk = yield* fileSystem.readFileString(ratesCachePath).pipe(
Effect.flatMap((raw) => decodeRatesCache(raw)),
Effect.catchCause(() => Effect.succeed(null)),
);
if (fromDisk !== null) {
const parsed = parseRateTable(fromDisk.document);
if (parsed.size > 0) {
rates = parsed;
ratesFetchedAtMs = fromDisk.fetchedAtMs;
ratesStatus = "cached";
if (now - fromDisk.fetchedAtMs < maxAgeMs) return;
}
}
}
const fetched = yield* httpClient.get(LITELLM_RATES_URL).pipe(
Effect.flatMap(HttpClientResponse.filterStatusOk),
Effect.flatMap((response) => response.json),
Effect.timeout(10_000),
Effect.catchCause(() => Effect.succeed(null)),
);
if (fetched === null) {
// The refresh failed; whatever we are serving is now past its TTL and
// must not keep claiming to be fresh.
if (rates.size > 0) ratesStatus = "cached";
return;
}
const parsed = parseRateTable(fetched);
if (parsed.size === 0) return;
rates = parsed;
ratesFetchedAtMs = now;
ratesStatus = "fresh";
yield* encodeRatesCache({ fetchedAtMs: now, document: fetched }).pipe(
Effect.flatMap((contents) => writeCacheFile(ratesCachePath, contents)),
Effect.ignoreCause,
);
});
const ensureRates = (force: boolean) => ratesLock.withPermit(loadRates(force));
const refreshRates = ensureRates(true).pipe(
Effect.map(pricing),
Effect.withSpan("UsageService.refreshRates"),
);
// A settings failure must not silently discard custom rates or transcript homes.
const readSettings = settingsService.getSettings.pipe(
Effect.catchCause(
(cause) =>
new UsageReadError({
reason: "scanFailed",
detail: "Server settings could not be read.",
cause: Cause.squash(cause),
}),
),
);
/**
* Every instance of `driver` with its decoded config, or none when it does
* not decode. Disabled accounts still have history. An unconfigured default
* slot runs with default config, just as it does in the provider registry.
*/
const usageInstances = Effect.fn("UsageService.usageInstances")(function* <Config>(
driver: ProviderDriver<Config, unknown, unknown>,
settings: ServerSettingsValue,
) {
const entries: Array<
readonly [ProviderInstanceId, Pick<ProviderInstanceConfig, "config" | "environment">, boolean]
> = Object.entries(settings.providerInstances)
.filter(([, instance]) => instance.driver === driver.driverKind)
.map(([id, instance]) => [ProviderInstanceId.make(id), instance, true] as const);
if (!Object.hasOwn(settings.providerInstances, driver.driverKind)) {
entries.push([ProviderInstanceId.make(driver.driverKind), {}, false]);
}
const decodeConfig = Schema.decodeUnknownOption(driver.configSchema);
return yield* Effect.forEach(
entries,
Effect.fnUntraced(function* ([instanceId, instance, configured]) {
const instanceConfig: ProviderUsageInstance<Config> = {
instanceId,
config: Option.getOrUndefined(decodeConfig(instance.config ?? {})),
environment: yield* mergeProviderInstanceEnvironment(
instance.environment,
hostEnvironment,
),
configured,
};
return instanceConfig;
}),
);
});
/** Resolves every transcript directory the usage readers point at. */
const resolveTranscriptDirs = Effect.fn("UsageService.resolveTranscriptDirs")(function* (
settings: ServerSettingsValue,
retentionCutoffMs: number,
) {
const dirs: Array<TranscriptSource> = [];
const seen = new Set<string>();
for (const { driver, reader } of transcriptReaders) {
const { provider, format } = reader;
const directories = yield* Effect.forEach(
yield* usageInstances(driver, settings),
(instance) => reader.directories(instance),
);
for (const { dir: directory, fileName } of directories.flat()) {
const sourceKey = provider + "\0" + directory;
const previous = sourceCache.get(sourceKey);
// Keep canonical paths and source fingerprints stable after root cleanup,
// including aliases and clients merging pre-cleanup environment summaries.
const dir = yield* fileSystem
.realPath(directory)
.pipe(Effect.orElseSucceed(() => previous?.dir ?? directory));
const currentVolumeId = yield* Effect.promise(() => readDirectoryVolumeId(dir));
const hasRetainedHistory = fileCache
.entries()
.some(
([filePath, entry]) =>
entry.provider === provider &&
entry.mtimeMs >= retentionCutoffMs &&
entry.records.length + entry.tailRecords.length > 0 &&
isWithinDirectory(filePath, dir),
);
// A recreated directory still reports the retained history under its old identity.
const volumeId =
previous?.dir === dir && (hasRetainedHistory || !currentVolumeId)
? previous.volumeId || currentVolumeId
: currentVolumeId;
if (previous?.dir !== dir || previous.volumeId !== volumeId) {
sourceCache.set(sourceKey, { dir, volumeId });
cacheDirty = true;
}
const key = `${provider}\0${dir}`;
if (seen.has(key)) continue;
seen.add(key);
dirs.push({
provider,
format,
dir,
volumeId,
...(fileName === undefined ? {} : { fileName }),
});
}
}
return dirs;
});
/**
* Loads the persisted scan cache exactly once per process.
*
* `Effect.cached` makes concurrent first readers await the same load rather
* than each seeing a "loaded" flag set before the read finished and cold
* scanning against an empty cache.
*/
const ensureScanCacheLoaded = yield* Effect.cached(
Effect.gen(function* () {
const readDocument = (filePath: string) =>
fileSystem.readFileString(filePath).pipe(
Effect.flatMap((raw) => decodeScanCacheFile(raw)),
Effect.catchCause(() => Effect.succeed(null)),
);
let document = yield* readDocument(scanCachePath);
if (document === null) {
document = yield* readDocument(legacyScanCachePath);
// Write the migrated cache to its own file on the next scan.
cacheDirty = document !== null;
}
if (document === null) return;
for (const [path, entry] of decodeScanCache(document, transcriptFormats)) {
fileCache.set(path, entry);
}
const sources = decodeCachedSources(document);
if (Option.isSome(sources)) {
for (const [key, source] of Object.entries(sources.value.sources))
sourceCache.set(key, source);
}
}),
);
const writeScanCache = makeScanCacheWriter();
// Scans with different windows can finish together; serializing the writes
// keeps an older snapshot from landing after a newer one.
const persistLock = yield* Semaphore.make(1);
// The dirty flag is cleared before encoding, so a change while the write is
// in flight marks it dirty again. A failed write restores the flag, so the
// next persist retries instead of leaving disk stale. A cache we cannot
// write is a slower next start, not a failed read.
const persistCaches = Effect.gen(function* () {
if (cacheDirty) {
cacheDirty = false;
yield* Effect.sync(() =>
writeScanCache(fileCache, { sources: Object.fromEntries(sourceCache) }),
).pipe(
Effect.flatMap((contents) => writeCacheFile(scanCachePath, contents)),
Effect.catchCause(() =>
Effect.sync(() => {
cacheDirty = true;
}),
),
);
}
}).pipe(persistLock.withPermit, Effect.withSpan("UsageService.persistCaches"));
const pendingPersists = new Set<Fiber.Fiber<void>>();
/** Writes dirty caches in the background, after the summary that dirtied them answers. */
const schedulePersist = Effect.forkDetach(persistCaches).pipe(
Effect.map((fiber) => {
pendingPersists.add(fiber);
fiber.addObserver(() => pendingPersists.delete(fiber));
}),
);
/** Waits for every write scheduled so far, as a restart would need. */
const awaitPersisted = Effect.suspend(() => Fiber.awaitAll([...pendingPersists])).pipe(
Effect.asVoid,
);
// A write still running when the service shuts down finishes first, so the
// next start does not lose the last scan.
yield* Effect.addFinalizer(() => awaitPersisted);
/**
* Parses one transcript, reusing the cached result when it is unchanged.
*
* A file that only grew re-parses from the cached position, so an actively
* written multi-hundred-megabyte rollout costs its appended bytes per scan
* rather than a full re-read. The reader verifies the position's guard bytes
* and silently restarts from byte 0 when they no longer match.
*
* A fresh parse comes back as `update` for the caller to cache, with the
* entry it was built from. Reads run concurrently, and the caller stores
* updates in walk order rather than completion order: saved records of
* deleted transcripts aggregate in cache order, where the first copy of a
* duplicate wins.
*/
const readFileRecords = (
filePath: string,
size: number,
mtimeMs: number,
provider: UsageProviderKind,
format: TranscriptUsageFormat<unknown>,
): Effect.Effect<{
readonly records: readonly UsageRecord[];
readonly failed?: true;
readonly update?: { readonly entry: CachedFile; readonly replaces: CachedFile | undefined };
}> =>
Effect.gen(function* () {
const cached = fileCache.get(filePath);
// Provider is part of the identity: if both providers were ever pointed
// at one directory, a hit parsed by the other parser must not be reused.
if (
cached &&
cached.size === size &&
cached.mtimeMs === mtimeMs &&
cached.provider === provider
) {
return {
records:
cached.tailRecords.length === 0
? cached.records
: [...cached.records, ...cached.tailRecords],
};
}
// Only a strictly grown file may resume. Same size with a new mtime, or
// a shrunken file, means rewritten content; re-parse it whole.
const resumeFrom =
cached !== undefined && cached.provider === provider && size > cached.size
? cached.position
: undefined;
const parsed = yield* Effect.promise(() =>
readTranscriptRecords(filePath, format, resumeFrom),
);
// A read failure is not an empty transcript: caching it under this
// (size, mtime) would silently drop the file's usage until it changes.
if (parsed === null)
return {
records: cached?.provider === provider ? [...cached.records, ...cached.tailRecords] : [],
failed: true,
};
// Stored already de-duplicated within the file, which is 99% of all
// duplicates. The aggregator still runs the cross-file dedupe pass. One
// seen set spans the cached base, the new lines, and the tail so a
// resumed parse dedupes exactly like a full one.
const base = parsed.resumed && cached !== undefined ? cached.records : [];
const seen = new Set<string>();
const records = dedupeWithinFile([...base, ...parsed.records], seen);
const tailRecords = dedupeWithinFile(parsed.tailRecords, seen);
return {
records: tailRecords.length === 0 ? records : [...records, ...tailRecords],
update: {
entry: { size, mtimeMs, provider, records, tailRecords, position: parsed.position },
replaces: cached,
},
};
});
/** One provider directory's walk and parse, before rates are involved. */
interface ScannedDir {
readonly provider: UsageProviderKind;
readonly dir: string;
readonly volumeId: string;
readonly hostId?: string;
readonly status?: UsageSource["status"];
readonly message?: string;
readonly action?: UsageSource["action"];
/** Parsed records per file, or `null` when the directory does not exist. */
readonly files:
| readonly { readonly path: string; readonly records: readonly UsageRecord[] }[]
| null;
/** Answered from a cache while a refresh runs. */
readonly refreshing?: true;
}
const scanTranscriptDir = Effect.fn("UsageService.scanTranscriptDir")(function* (
source: TranscriptSource,
windowStartMs: number,
) {
const { provider, format, dir, volumeId, fileName } = source;
const exists = yield* fileSystem
.exists(dir)
.pipe(Effect.catchCause(() => Effect.succeed(false)));
if (!exists) return { provider, dir, volumeId, files: null } satisfies ScannedDir;
const { files, failedPaths } = yield* Effect.promise(() =>
listTranscriptFiles(dir, windowStartMs, fileName === undefined ? undefined : { fileName }),
);
// A cold parse waits on disk reads, so a few files in flight read
// close to twice as fast. Results keep walk order.
const read = yield* Effect.forEach(
files,
(file) =>
readFileRecords(file.path, file.size, file.mtimeMs, provider, format).pipe(
Effect.map((result) => ({ path: file.path, ...result })),
),
{ concurrency: TRANSCRIPT_READ_CONCURRENCY },
);
const parsedFiles = read.map(({ path, records, update }) => {
if (update === undefined) return { path, records };
// A scan of another window may have cached its own read of this file
// meanwhile. Then keep whichever read saw the later file, so a slower
// scan never replaces newer usage with older.
const current = fileCache.get(path);
if (
current === update.replaces ||
current === undefined ||
!isLaterRead(current, update.entry)
) {
fileCache.set(path, update.entry);
cacheDirty = true;
}
return { path, records };
});
// Unread files keep their cached usage, but the total may be short.
const unread = failedPaths + read.filter((file) => file.failed).length;
return {
provider,
dir,
volumeId,
files: parsedFiles,
...(unread > 0
? {
status: "partial",
message: `${unread} transcript path(s) could not be read; usage may be incomplete.`,
}
: {}),
} satisfies ScannedDir;
});
const collectDirs = Effect.fn("UsageService.collectDirs")(function* (
windowStartMs: number,
settings: ServerSettingsValue,
retentionCutoffMs: number,
awaitRefresh: boolean,
) {
const dirs = yield* resolveTranscriptDirs(settings, retentionCutoffMs).pipe(
Effect.provideContext(readerContext),
);
const scans = Effect.forEach(
scanReaders,
({ driver, reader }) =>
usageInstances(driver, settings)
.pipe(
Effect.flatMap((instances) =>
reader.scan({ instances, settings, windowStartMs, retentionCutoffMs, awaitRefresh }),
),
)
.pipe(
Effect.flatMap((sources) =>
Effect.forEach(sources, ({ volumeId, ...source }) =>
Effect.map(
volumeId === undefined
? Effect.promise(() => readDirectoryVolumeId(source.dir))
: Effect.succeed(volumeId),
(resolved): ScannedDir => ({
...source,
provider: reader.provider,
volumeId: resolved,
}),
),
),
),
Effect.provideContext(readerContext),
),
{ concurrency: "unbounded" },
);
// Independent sources scan together. Transcript directories go one at a
// time, so open files stay at `TRANSCRIPT_READ_CONCURRENCY`. The result
// keeps this order, since aggregation keeps the first copy of a duplicate.
const [transcripts, scanDirs] = yield* Effect.all(
[Effect.forEach(dirs, (dir) => scanTranscriptDir(dir, windowStartMs)), scans],
{ concurrency: "unbounded" },
);
const scanned: readonly ScannedDir[] = [...transcripts, ...scanDirs.flat()];
return scanned;
});
const scanSummary = Effect.fn("UsageService.scanSummary")(function* (
input: UsageSummaryInput,
settings: ServerSettingsValue,
) {
if (input.sinceDay > input.untilDay) {
return yield* new UsageReadError({
reason: "invalidWindow",
detail: `sinceDay '${input.sinceDay}' is after untilDay '${input.untilDay}'`,
});
}
let hourlyWindow: { readonly sinceTimeMs: number; readonly untilTimeMs: number } | null = null;
if (input.resolution === "hour") {
const sinceTime =
input.sinceTime === undefined ? Option.none() : DateTime.make(input.sinceTime);
const untilTime =
input.untilTime === undefined ? Option.none() : DateTime.make(input.untilTime);
if (Option.isNone(sinceTime) || Option.isNone(untilTime)) {
return yield* new UsageReadError({
reason: "invalidWindow",
detail: "Hourly usage requires valid sinceTime and untilTime instants",
});
}
const sinceTimeMs = DateTime.toEpochMillis(sinceTime.value);
const untilTimeMs = DateTime.toEpochMillis(untilTime.value);
const durationMs = untilTimeMs - sinceTimeMs;
if (durationMs <= 0 || durationMs > MAX_HOURLY_WINDOW_MS) {
return yield* new UsageReadError({
reason: "invalidWindow",
detail: "Hourly usage window must be greater than zero and at most 24 hours",
});
}
hourlyWindow = { sinceTimeMs, untilTimeMs };
}
const startedAtMs = yield* Clock.currentTimeMillis;
yield* ensureScanCacheLoaded;
const hostId = NodeOS.hostname();
const windowStart = DateTime.make(`${input.sinceDay}T00:00:00Z`);
if (Option.isNone(windowStart)) {
return yield* new UsageReadError({
reason: "invalidWindow",
detail: `sinceDay '${input.sinceDay}' is not a valid date`,
});
}
const windowStartMs =
(hourlyWindow?.sinceTimeMs ?? DateTime.toEpochMillis(windowStart.value)) - MTIME_SLACK_MS;
const retentionCutoffMs = startedAtMs - CACHE_RETENTION_DAYS * 24 * 60 * 60 * 1000;
// Pricing only matters once records are aggregated, so the rate table
// loads while transcripts stream instead of gating them: a cold rates
// fetch on a slow network no longer delays the scan by its own timeout.
const [, scannedDirs] = yield* Effect.all(
[
ensureRates(false),
collectDirs(windowStartMs, settings, retentionCutoffMs, input.awaitRefresh === true),
],
{ concurrency: 2 },
);
const aggregator = new UsageAggregator({
timeZone: input.timeZone,
sinceDay: input.sinceDay,
untilDay: input.untilDay,
resolution: input.resolution ?? "day",
...hourlyWindow,
rates,
priceOverrides: createOverrideRateTable(settings.usagePriceOverrides),
modelAliases: resolveModelAliases(settings.usageModelAliases),
});
const sources: UsageSource[] = [];
// Cleanup may remove transcripts, but the usage we already saved still
// contributes to its source through the normal aggregation and dedupe
// path. Like the walk, skip files last written before the window: they
// cannot hold records inside it.
const retainedSinceMs = Math.max(windowStartMs, retentionCutoffMs);
const filesByDir = scannedDirs.map(({ provider, dir, files }) => {
const retainedFiles = [...(files ?? [])];
const livePaths = new Set(retainedFiles.map((file) => file.path));
for (const [filePath, entry] of fileCache) {
if (
entry.provider !== provider ||
entry.mtimeMs < retainedSinceMs ||
livePaths.has(filePath) ||
!isWithinDirectory(filePath, dir)
)
continue;
retainedFiles.push({ path: filePath, records: [...entry.records, ...entry.tailRecords] });
}
return retainedFiles;
});
const crossFileSessions = sharedSessions(filesByDir.flat());
for (const [
index,
{ provider, dir, volumeId, files, status, message, action, refreshing, hostId: sourceHostId },
] of scannedDirs.entries()) {
let scannedFiles = 0;
let skippedFiles = 0;
// Distinct per directory. Buckets carry per-cell session counts, but a
// session spans days and models, so clients total this figure instead.
const sessionIds = new Set<string>();
for (const file of filesByDir[index] ?? []) {
if (file.records.length === 0) {
skippedFiles += 1;
continue;
}
scannedFiles += 1;
const eventOccurrences = new Map<string, number>();
for (const record of file.records) {
let usageRecord = record;
if (
sharedSessionProviders.has(record.provider) &&
crossFileSessions.has(record.sessionId)
) {
// Match moved copies without collapsing repeated equal events
// within one file (timestamps can have only second precision).
// Only sessions seen in several files can have a copy to match.
const key = encodeUsageRecordKey([
record.provider,
record.sessionId,
record.timestampMs,
record.model,
record.totals,
]);
const occurrence = (eventOccurrences.get(key) ?? 0) + 1;
eventOccurrences.set(key, occurrence);
usageRecord = { ...record, dedupeKey: key + ":" + occurrence };
}
// Only sessions contributing in-window count; the mtime slack can
// admit boundary files whose records fall outside the range.
if (aggregator.add(usageRecord, dir) && record.sessionId.length > 0) {
sessionIds.add(record.sessionId);
}
}
}
sources.push({
fingerprint: { hostId: sourceHostId ?? hostId, provider, resolvedHomePath: dir, volumeId },
// Clients exclude missing sources, so saved records remain an available source.
status: files === null && scannedFiles === 0 ? "missing" : (status ?? "ok"),
scannedFiles,
skippedFiles,
malformedRecords: 0,
distinctSessions: sessionIds.size,
message:
message ?? (files === null ? "No transcript directory on this environment." : null),
...(action ? { action } : {}),
...(refreshing ? { refreshing } : {}),
});
}
const pruned = pruneScanCache(fileCache, retentionCutoffMs);
if (pruned > 0) cacheDirty = true;
const aggregated = aggregator.finish();
const readAt = yield* DateTime.now;
const finishedAtMs = yield* Clock.currentTimeMillis;
return {
contractVersion: USAGE_CONTRACT_VERSION,
readAt: DateTime.formatIso(readAt),
timeZone: input.timeZone,
sinceDay: input.sinceDay,
untilDay: input.untilDay,
buckets: aggregated.buckets,
sources,
pricing: pricing(),
scanDurationMs: Math.max(0, finishedAtMs - startedAtMs),
} satisfies UsageSummary;
});
/**
* In-flight scans by window and settings, so concurrent identical requests (the usage
* page open on two clients at once) share one scan instead of racing over
* the same corpus twice. Readers read settings of their own, so a scan is
* shared only under the same settings snapshot.
*/
const inflightScans = new Map<string, Deferred.Deferred<UsageSummary, UsageReadError>>();
const scanKey = (input: UsageSummaryInput, settings: ServerSettingsValue): string =>
JSON.stringify([
input.timeZone,
input.sinceDay,
input.untilDay,
input.resolution ?? "day",
input.sinceTime ?? null,
input.untilTime ?? null,
settings,
// A waiting read must never share a scan that answers with `refreshing`.
input.awaitRefresh === true,
]);
const readSummary = Effect.fn("UsageService.readSummary")(function* (input: UsageSummaryInput) {
const settings = yield* readSettings;
const key = scanKey(input, settings);
const deferred = yield* Effect.uninterruptible(
Effect.gen(function* () {
const existing = inflightScans.get(key);
if (existing !== undefined) return existing;
// Enrollment and detached-fiber creation must be atomic. Otherwise a
// canceled first caller can leave a Deferred with no scan to finish it.
const created = Deferred.makeUnsafe<UsageSummary, UsageReadError>();
inflightScans.set(key, created);
// Detached so one departing client cannot tear the scan out from under
// the fibers awaiting it; a finished scan warms the cache either way.
// The cache write is registered before the waiters resume, so they
// can await it, but its fiber starts after they have the summary.
yield* scanSummary(input, settings).pipe(
Effect.onExit((exit) =>
Effect.sync(() => inflightScans.delete(key)).pipe(
Effect.andThen(Exit.isSuccess(exit) ? schedulePersist : Effect.void),
Effect.andThen(Deferred.done(created, exit)),
),
),
Effect.forkDetach,
);
return created;
}),
);
// Waiting stays interruptible. The detached scan continues for other
// callers and still warms the cache if this caller leaves.
return yield* Deferred.await(deferred);
});
// `awaitPersisted` is outside the service interface: tests use it to restart
// against what a previous instance wrote.
return { readSummary, refreshRates, awaitPersisted } as const;
});
export const layer = Layer.effect(UsageService, make);