Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(retention): sweep the decision-model attempt log like every other log (#1543)

tashdroid9e9c3b503004
diff --git a/.env.example b/.env.example
index a631fae5..aa5895a9 100644
--- a/.env.example
+++ b/.env.example
@@ -286,6 +286,13 @@ REQUEST_ANALYTICS_MAX_ROWS=100000
# SEARCH_LOGS_RETENTION_DAYS=90
# SEARCH_LOGS_MAX_ROWS=100000
+# Decision-model attempt log behind the dashboard's Models → Decision page
+# (decision_requests). Every /v1/decisions attempt appends a row, so the log
+# is bounded by age and count on the same pass as the analytics prune.
+# 0 disables that half.
+# DECISION_LOGS_RETENTION_DAYS=90
+# DECISION_LOGS_MAX_ROWS=100000
+
# Per-request caller identity (client IP + User-Agent) recorded into request
# analytics and shown in the dashboard's "Recent calls" table. Set to false
# to store nulls instead (aggregate analytics are unaffected).
diff --git a/docs/en/env/01-variables.md b/docs/en/env/01-variables.md
index 9325f388..ee2a73b6 100644
--- a/docs/en/env/01-variables.md
+++ b/docs/en/env/01-variables.md
@@ -121,6 +121,8 @@ In Docker, `127.0.0.1` is the container, not your machine — see [03-outbound-p
| `QUOTA_OBSERVATIONS_MAX_ROWS` | `200000` | Count bound for the same audit trail (`QUOTA_OBSERVATIONS_MAX_ROWS=200000`, `.env.example:230-233`). Keeps the newest N rows, pruning the excess daily via `ORDER BY created_at DESC, rowid DESC LIMIT … OFFSET maxRows`. `0` disables count pruning. Together the two knobs are `getQuotaObservationRetentionConfig()`; pruned on the daily gate (24h interval, or next 60s tick while backlog remains) in budgeted chunks of `5k` rows / `250ms` (`QUOTA_OBSERVATIONS_PRUNE_CHUNK` / `QUOTA_OBSERVATIONS_PRUNE_BUDGET_MS`, `server/src/services/request-retention.ts:89-90`, refined in `4a8f095` from 20k/870ms). The count bound is far above live pool count, so keeping newest row per pool is guaranteed. |
| `SEARCH_LOGS_RETENTION_DAYS` | `90` | Age bound for the web-search attempt log (`search_requests` behind the dashboard's Search page). Every search attempt appends a row; the widest analytics window is 90d, so older rows are pure disk growth. Pruned on the same 60s pass as the analytics prune. `0` disables age pruning. |
| `SEARCH_LOGS_MAX_ROWS` | `100000` | Count bound for the same log: keeps the newest N rows. `0` disables count pruning. Together the two knobs are `getSearchLogRetentionConfig()`. |
+| `DECISION_LOGS_RETENTION_DAYS` | `90` | Age bound for the decision-model attempt log (`decision_requests` behind the dashboard's Models → Decision page). Every `/v1/decisions` attempt appends a row; its only reader is a month-scoped count, so older rows are pure disk growth. Pruned on the same 60s pass as the analytics prune. `0` disables age pruning. |
+| `DECISION_LOGS_MAX_ROWS` | `100000` | Count bound for the same log: keeps the newest N rows. `0` disables count pruning. Together the two knobs are `getDecisionLogRetentionConfig()`. |
| `FREEAPI_DB_PATH` | Default location next to the server build | Optional SQLite location override. Useful on hosts where only one directory is mounted persistently, or to keep the DB outside `server/data`. Example: `/app/server/data/freellmapi.db`. |
| `FREEAPI_DB_BACKUP_PATH` | Unset | Optional encrypted SQLite backup target (file path). On startup, FreeLLMAPI restores this backup if the configured DB file is missing; while running, it uploads a fresh backup periodically. |
| `FREEAPI_DB_BACKUP_URL` | Unset | HTTP(S) backup target, alternative to the path above. |
diff --git a/server/src/__tests__/services/request-retention.test.ts b/server/src/__tests__/services/request-retention.test.ts
index f960c9ca..4bfc62f1 100644
--- a/server/src/__tests__/services/request-retention.test.ts
+++ b/server/src/__tests__/services/request-retention.test.ts
@@ -6,8 +6,22 @@ const ORIGINAL_RETENTION_DAYS = process.env.REQUEST_ANALYTICS_RETENTION_DAYS;
const ORIGINAL_MAX_ROWS = process.env.REQUEST_ANALYTICS_MAX_ROWS;
const ORIGINAL_SEARCH_LOGS_RETENTION_DAYS = process.env.SEARCH_LOGS_RETENTION_DAYS;
const ORIGINAL_SEARCH_LOGS_MAX_ROWS = process.env.SEARCH_LOGS_MAX_ROWS;
+const ORIGINAL_DECISION_LOGS_RETENTION_DAYS = process.env.DECISION_LOGS_RETENTION_DAYS;
+const ORIGINAL_DECISION_LOGS_MAX_ROWS = process.env.DECISION_LOGS_MAX_ROWS;
function restoreEnv() {
+ if (ORIGINAL_DECISION_LOGS_RETENTION_DAYS === undefined) {
+ delete process.env.DECISION_LOGS_RETENTION_DAYS;
+ } else {
+ process.env.DECISION_LOGS_RETENTION_DAYS = ORIGINAL_DECISION_LOGS_RETENTION_DAYS;
+ }
+
+ if (ORIGINAL_DECISION_LOGS_MAX_ROWS === undefined) {
+ delete process.env.DECISION_LOGS_MAX_ROWS;
+ } else {
+ process.env.DECISION_LOGS_MAX_ROWS = ORIGINAL_DECISION_LOGS_MAX_ROWS;
+ }
+
if (ORIGINAL_SEARCH_LOGS_RETENTION_DAYS === undefined) {
delete process.env.SEARCH_LOGS_RETENTION_DAYS;
} else {
@@ -220,4 +234,39 @@ describe('request analytics retention', () => {
pruneRequestAnalytics({ db, force: true, now: new Date('2026-05-08T12:00:00Z') });
expect((db.prepare('SELECT COUNT(*) AS n FROM search_requests').get() as { n: number }).n).toBe(1);
});
+
+ it('bounds the decision-model attempt log by age and count', () => {
+ process.env.DECISION_LOGS_RETENTION_DAYS = '7';
+ process.env.DECISION_LOGS_MAX_ROWS = '2';
+ const db = getDb();
+ const insert = db.prepare(`
+ INSERT INTO decision_requests (model_id, platform, status, latency_ms, created_at)
+ VALUES ('clef-flash', 'cloudflare', 'ok', 10, ?)
+ `);
+ insert.run('2026-05-01 00:00:00'); // older than 7d from May 8
+ insert.run('2026-05-07 00:00:00');
+ insert.run('2026-05-07 23:00:00');
+ insert.run('2026-05-08 00:00:00');
+
+ const result = pruneRequestAnalytics({
+ db,
+ force: true,
+ now: new Date('2026-05-08T12:00:00Z'),
+ });
+ // Age prune drops May 1; the count cap then trims the rest to the newest 2.
+ expect(result.deleted).toBe(2);
+ const remaining = db.prepare(`SELECT created_at FROM decision_requests ORDER BY created_at`).all() as Array<{ created_at: string }>;
+ expect(remaining.map(r => r.created_at)).toEqual(['2026-05-07 23:00:00', '2026-05-08 00:00:00']);
+ });
+
+ it('keeps decision rows when both knobs are 0', () => {
+ process.env.DECISION_LOGS_RETENTION_DAYS = '0';
+ process.env.DECISION_LOGS_MAX_ROWS = '0';
+ const db = getDb();
+ db.prepare(`INSERT INTO decision_requests (model_id, platform, status, latency_ms, created_at)
+ VALUES ('clef-flash', 'cloudflare', 'ok', 10, '2020-01-01 00:00:00')`).run();
+
+ pruneRequestAnalytics({ db, force: true, now: new Date('2026-05-08T12:00:00Z') });
+ expect((db.prepare('SELECT COUNT(*) AS n FROM decision_requests').get() as { n: number }).n).toBe(1);
+ });
});
diff --git a/server/src/services/request-retention.ts b/server/src/services/request-retention.ts
index 8ed4dd3a..254c2d26 100644
--- a/server/src/services/request-retention.ts
+++ b/server/src/services/request-retention.ts
@@ -27,6 +27,12 @@ const DEFAULT_QUOTA_OBSERVATIONS_MAX_ROWS = 200_000;
// window is 90d, so the default age bound matches the `requests` table.
const DEFAULT_SEARCH_RETENTION_DAYS = 90;
const DEFAULT_SEARCH_MAX_ROWS = 100_000;
+// decision_requests (#1537) is the per-attempt decision-model log behind the
+// Models → Decision page. Same shape as search_requests: an append-only
+// attempt log whose only reader is a month-scoped count, so older rows are
+// pure disk growth. Bounded like the search log.
+const DEFAULT_DECISION_RETENTION_DAYS = 90;
+const DEFAULT_DECISION_MAX_ROWS = 100_000;
type RetentionDb = ReturnType<typeof getDb>;
@@ -99,6 +105,19 @@ export function getSearchLogRetentionConfig(): SearchLogRetentionConfig {
};
}
+export interface DecisionLogRetentionConfig {
+ retentionDays: number;
+ maxRows: number;
+}
+
+/** Same env convention as the pairs above; 0 on either knob disables that half. */
+export function getDecisionLogRetentionConfig(): DecisionLogRetentionConfig {
+ return {
+ retentionDays: readNonNegativeInt('DECISION_LOGS_RETENTION_DAYS', DEFAULT_DECISION_RETENTION_DAYS),
+ maxRows: readNonNegativeInt('DECISION_LOGS_MAX_ROWS', DEFAULT_DECISION_MAX_ROWS),
+ };
+}
+
// The first sweep on an install that predates the prune may face hundreds of
// thousands of rows; deleting them in one statement held the loop for ~4.5s on
// a 470k-row log. Work is chunked under a wall-clock budget instead, and a
@@ -293,6 +312,44 @@ export function pruneSearchLogs(db: RetentionDb, nowMs: number): number {
return deleted;
}
+/**
+ * Age- and count-bound the decision-model attempt log (#1537).
+ *
+ * decision_requests grew on every POST /v1/decisions attempt (and every failed
+ * one — a cooling-down key still logs) with no prune of any kind. Its only
+ * reader is the month-scoped count on the Decision page, so the 90d default
+ * matches the other attempt logs. Guarded against the table being absent like
+ * every other prune here.
+ */
+export function pruneDecisionLogs(db: RetentionDb, nowMs: number): number {
+ const hasTable = !!db
+ .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='decision_requests'")
+ .get();
+ if (!hasTable) return 0;
+
+ const { retentionDays, maxRows } = getDecisionLogRetentionConfig();
+ let deleted = 0;
+
+ if (retentionDays > 0) {
+ const cutoff = toSqliteTimestamp(new Date(nowMs - retentionDays * DAY_MS));
+ deleted += db.prepare('DELETE FROM decision_requests WHERE created_at < ?').run(cutoff).changes;
+ }
+
+ if (maxRows > 0) {
+ deleted += db.prepare(`
+ DELETE FROM decision_requests
+ WHERE id IN (
+ SELECT id
+ FROM decision_requests
+ ORDER BY created_at DESC, id DESC
+ LIMIT -1 OFFSET ?
+ )
+ `).run(maxRows).changes;
+ }
+
+ return deleted;
+}
+
export function pruneRequestAnalytics(options: {
db?: RetentionDb;
force?: boolean;
@@ -336,6 +393,8 @@ export function pruneRequestAnalytics(options: {
deleted += pruneExpiredClaims(db, nowMs);
// Web-search attempt log, same gate (see pruneSearchLogs).
deleted += pruneSearchLogs(db, nowMs);
+ // Decision-model attempt log, same gate (see pruneDecisionLogs).
+ deleted += pruneDecisionLogs(db, nowMs);
// Hourly aggregate prune (gated once per day). The UI's widest window is
// 30d, so we keep at most 30 days of hourly buckets (~720 rows). The table