Skip to content

Commit 52ffe0c

Browse files
t3dotggonly21milclaude
authored
fix(usage): cold usage scans no longer stall the server (#19446)
Co-authored-by: Victor Vogel <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f4df794 commit 52ffe0c

4 files changed

Lines changed: 274 additions & 44 deletions

File tree

‎apps/server/src/provider/Drivers/antigravityUsageReader.test.ts‎

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// @effect-diagnostics-next-line nodeBuiltinImport:off - Effect's File.Info has no ctime or sub-millisecond mtime, which the cache tests compare.
22
import * as NodeFSP from "node:fs/promises";
3+
import * as NodeSqlite from "node:sqlite";
34

45
import * as NodeServices from "@effect/platform-node/NodeServices";
5-
import { assert, describe, it } from "@effect/vitest";
6+
import { assert, describe, it, vi } from "@effect/vitest";
67
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
78
import * as Context from "effect/Context";
89
import * as Effect from "effect/Effect";
910
import * as FileSystem from "effect/FileSystem";
1011
import * as Layer from "effect/Layer";
1112
import * as Path from "effect/Path";
1213
import * as SqlClient from "effect/sql/SqlClient";
14+
import * as Tracer from "effect/Tracer";
1315

1416
import { makeAntigravityUsageCache, readAntigravityUsage } from "./antigravityUsageReader.ts";
1517

@@ -445,4 +447,141 @@ describe("readAntigravityUsage", () => {
445447
assert.strictEqual(records(result)[0]?.totals.uncachedInputTokens, 10);
446448
}).pipe(Effect.provide(NodeServices.layer)),
447449
);
450+
451+
/** Counts the rows each StatementSync.all() call returns, by its SQL. */
452+
const recordReads = Effect.gen(function* () {
453+
const reads: Array<{ sql: string; rows: number }> = [];
454+
const all = NodeSqlite.StatementSync.prototype.all;
455+
yield* Effect.acquireRelease(
456+
Effect.sync(() =>
457+
vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
458+
this: NodeSqlite.StatementSync,
459+
...params
460+
) {
461+
const result = all.apply(this, params);
462+
reads.push({ sql: this.sourceSQL, rows: result.length });
463+
return result;
464+
}),
465+
),
466+
(spy) => Effect.sync(() => spy.mockRestore()),
467+
);
468+
return reads;
469+
});
470+
471+
const stepUsage = (responseId: string) =>
472+
new Uint8Array(protoBytes(9, [...protoNumber(2, 10), ...protoText(11, responseId)]));
473+
474+
it.effect("reads one Antigravity generation or one page of steps per statement", () =>
475+
Effect.gen(function* () {
476+
const path = yield* Path.Path;
477+
const dir = yield* tempDir;
478+
yield* seedDatabase(path.join(dir, "session-1.db"), (db) =>
479+
Effect.gen(function* () {
480+
// Antigravity's keys and indexes: idx is the INTEGER PRIMARY KEY, and steps has indexes.
481+
yield* db.exec(
482+
"CREATE TABLE `gen_metadata` (`idx` integer,`data` blob,`size` integer NOT NULL DEFAULT 0,PRIMARY KEY (`idx`))",
483+
);
484+
yield* db.exec(
485+
"CREATE TABLE `steps` (`idx` integer,`step_type` integer NOT NULL DEFAULT 0,`status` integer NOT NULL DEFAULT 0,`metadata` blob,PRIMARY KEY (`idx`))",
486+
);
487+
yield* db.exec("CREATE INDEX `idx_steps_status` ON `steps`(`status`)");
488+
yield* db.exec("CREATE INDEX `idx_steps_step_type` ON `steps`(`step_type`)");
489+
yield* db.exec("BEGIN");
490+
for (let idx = 0; idx < 3; idx++) {
491+
yield* db.run("INSERT INTO gen_metadata (idx, data) VALUES (?, ?)", [
492+
idx,
493+
antigravityGeneration(`g-${idx}`),
494+
]);
495+
}
496+
// A run of steps without metadata sits between steps with usage.
497+
for (let idx = 0; idx < 250; idx++) {
498+
yield* db.run("INSERT INTO steps (idx, metadata) VALUES (?, ?)", [
499+
idx,
500+
idx >= 75 && idx < 175 ? null : stepUsage(`s-${idx}`),
501+
]);
502+
}
503+
yield* db.exec("COMMIT");
504+
}),
505+
);
506+
const reads = yield* recordReads;
507+
const spans: Tracer.NativeSpan[] = [];
508+
const tracer = Tracer.make({
509+
span: (options) => {
510+
const span = new Tracer.NativeSpan(options);
511+
spans.push(span);
512+
return span;
513+
},
514+
});
515+
const result = yield* readAntigravityUsage(dir, 0).pipe(
516+
Effect.provideService(Tracer.Tracer, tracer),
517+
);
518+
assert.deepStrictEqual(result.errors, []);
519+
assert.deepStrictEqual(
520+
records(result).map((record) => record.dedupeKey),
521+
[
522+
...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx}`),
523+
...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx + 175}`),
524+
...Array.from({ length: 3 }, (_, idx) => `antigravity:11:g-${idx}`),
525+
],
526+
);
527+
// Page statements are untraced, so the trace does not grow with the tables.
528+
assert.isTrue(spans.some((span) => span.name === "sqlite"));
529+
assert.deepStrictEqual(
530+
spans.flatMap((span) => {
531+
const query = span.attributes.get("db.query.text");
532+
return typeof query === "string" && query.includes("rowid > ?") ? [query] : [];
533+
}),
534+
[],
535+
);
536+
// Every row, with metadata or not, comes back through a bounded statement.
537+
for (const [table, total, most] of [
538+
["steps", 250, 64],
539+
["gen_metadata", 3, 1],
540+
] as const) {
541+
const rows = reads
542+
.filter((read) => read.sql.includes(`FROM ${table} `))
543+
.map((read) => read.rows);
544+
assert.strictEqual(
545+
rows.reduce((sum, count) => sum + count, 0),
546+
total,
547+
table,
548+
);
549+
assert.isAtMost(Math.max(...rows), most, table);
550+
}
551+
}).pipe(Effect.provide(NodeServices.layer)),
552+
);
553+
554+
it.effect("returns Antigravity records in idx order however the rows are stored", () =>
555+
Effect.gen(function* () {
556+
const path = yield* Path.Path;
557+
const dir = yield* tempDir;
558+
yield* seedDatabase(path.join(dir, "session-1.db"), (db) =>
559+
Effect.gen(function* () {
560+
yield* createTables(db);
561+
yield* db.exec("BEGIN");
562+
// Stored in reverse idx order, so rowid order is the opposite of idx order.
563+
for (let idx = 99; idx >= 0; idx--) {
564+
yield* db.run("INSERT INTO steps VALUES (?, ?)", [idx, stepUsage(`s-${idx}`)]);
565+
}
566+
for (const idx of [1, 0]) {
567+
yield* db.run("INSERT INTO gen_metadata VALUES (?, ?)", [
568+
idx,
569+
antigravityGeneration(`g-${idx}`),
570+
]);
571+
}
572+
yield* db.exec("COMMIT");
573+
}),
574+
);
575+
const result = yield* readAntigravityUsage(dir, 0);
576+
assert.deepStrictEqual(result.errors, []);
577+
assert.deepStrictEqual(
578+
records(result).map((record) => record.dedupeKey),
579+
[
580+
...Array.from({ length: 100 }, (_, idx) => `antigravity:11:s-${idx}`),
581+
"antigravity:11:g-0",
582+
"antigravity:11:g-1",
583+
],
584+
);
585+
}).pipe(Effect.provide(NodeServices.layer)),
586+
);
448587
});

‎apps/server/src/provider/Drivers/antigravityUsageReader.ts‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ function metadata(bytes: Uint8Array, step: boolean): Metadata {
132132
const data = step ? root : nested(root, 1);
133133
const model = step ? nested(data, 24) : data;
134134
const usage = bytesAt(data, step ? 9 : 4);
135-
const usages = usage === undefined ? [] : [fields(usage)];
135+
// Copy the usage bytes: subarrays kept in usages would hold each row's whole blob alive.
136+
const usages = usage === undefined ? [] : [fields(usage.slice())];
136137
for (const retry of data.get(step ? 28 : 17) ?? []) {
137138
if (!(retry instanceof Uint8Array)) throw new Error("Invalid retry metadata");
138139
const retryUsage = bytesAt(fields(retry), 2);
139-
if (retryUsage !== undefined) usages.push(fields(retryUsage));
140+
if (retryUsage !== undefined) usages.push(fields(retryUsage.slice()));
140141
}
141142
return {
142143
model: modelName(
@@ -171,6 +172,14 @@ class AntigravityDatabaseDecodeError extends Schema.TaggedError<AntigravityDatab
171172
}
172173
}
173174

175+
/**
176+
* Rows per statement. One statement.all() holds the event loop until every row
177+
* it returns is in memory, and a generation row carries the conversation's
178+
* whole context so far, often megabytes, so generations are read one at a time.
179+
*/
180+
const GENERATION_PAGE_ROWS = 1;
181+
const STEP_PAGE_ROWS = 64;
182+
174183
const readDatabase = Effect.fn("readDatabase")(function* (
175184
path: string,
176185
sessionId: string,
@@ -192,24 +201,49 @@ const readDatabase = Effect.fn("readDatabase")(function* (
192201
if (!tables.has("gen_metadata") && !tables.has("steps")) {
193202
return yield* new AntigravityDatabaseDecodeError({ path });
194203
}
204+
// Runs `query` a page at a time, binding the last rowid read and the page
205+
// size. A rowid seek works whatever the table's indexes. Antigravity declares
206+
// idx as the INTEGER PRIMARY KEY, so rowid order is idx order; the sort keeps
207+
// idx order where rows are stored otherwise.
195208
const readMetadata = Effect.fnUntraced(function* (
196209
query: string,
197-
column: string,
210+
pageRows: number,
198211
step: boolean,
199212
) {
200213
const entries: Array<{ idx: number; entry: Metadata }> = [];
201-
for (const row of yield* sql.unsafe<Record<string, unknown>>(query)) {
202-
const idx = row.idx;
203-
if (typeof idx !== "number") {
204-
return yield* new AntigravityDatabaseDecodeError({ path });
214+
// Below every rowid.
215+
let after = -Infinity;
216+
while (true) {
217+
// Untraced: a cold read runs one of these per generation.
218+
const rows = yield* sql
219+
.unsafe<{
220+
readonly row_id: number;
221+
readonly idx: unknown;
222+
readonly data: unknown;
223+
}>(query, [after, pageRows])
224+
.pipe(Effect.withTracerEnabled(false));
225+
for (const row of rows) {
226+
// Skipped here rather than in SQL, so a statement never scans past
227+
// more than one page of steps that have no metadata.
228+
if (step && row.data === null) continue;
229+
const idx = row.idx;
230+
if (typeof idx !== "number") {
231+
return yield* new AntigravityDatabaseDecodeError({ path });
232+
}
233+
entries.push({ idx, entry: yield* decode(() => metadata(blob(row.data), step)) });
205234
}
206-
entries.push({ idx, entry: yield* decode(() => metadata(blob(row[column]), step)) });
207-
if (entries.length % 256 === 0) yield* Effect.yieldNow;
235+
if (rows.length < pageRows) break;
236+
after = rows[rows.length - 1]!.row_id;
237+
yield* Effect.yieldNow;
208238
}
209-
return entries;
239+
return entries.sort((a, b) => a.idx - b.idx);
210240
});
211241
const generations = tables.has("gen_metadata")
212-
? yield* readMetadata("SELECT idx, data FROM gen_metadata ORDER BY idx", "data", false)
242+
? yield* readMetadata(
243+
"SELECT rowid AS row_id, idx, data FROM gen_metadata WHERE rowid > ? ORDER BY rowid LIMIT ?",
244+
GENERATION_PAGE_ROWS,
245+
false,
246+
)
213247
: [];
214248
let trajectoryTimestamp: number | null = null;
215249
if (tables.has("trajectory_metadata_blob")) {
@@ -221,8 +255,8 @@ const readDatabase = Effect.fn("readDatabase")(function* (
221255
}
222256
const steps = tables.has("steps")
223257
? yield* readMetadata(
224-
"SELECT idx, metadata FROM steps WHERE metadata IS NOT NULL ORDER BY idx",
225-
"metadata",
258+
"SELECT rowid AS row_id, idx, metadata AS data FROM steps WHERE rowid > ? ORDER BY rowid LIMIT ?",
259+
STEP_PAGE_ROWS,
226260
true,
227261
)
228262
: [];

‎packages/provider-opencode/src/server/usage.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,62 @@ describe("readOpenCodeUsage", () => {
8787
}).pipe(Effect.provide(NodeServices.layer)),
8888
);
8989

90+
it.effect("reads at most 64 OpenCode rowids per statement however much history is older", () =>
91+
Effect.gen(function* () {
92+
const fileSystem = yield* FileSystem.FileSystem;
93+
const path = yield* Path.Path;
94+
const dir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "usage-reader-test-" });
95+
const db = yield* openDatabase(path.join(dir, "opencode.db"));
96+
yield* db.unsafe(
97+
"CREATE TABLE message (id TEXT, session_id TEXT, data TEXT, time_created INTEGER)",
98+
);
99+
const sinceMs = 1780000000000;
100+
const data = JSON.stringify({
101+
role: "assistant",
102+
modelID: "claude-sonnet-4-5",
103+
tokens: { input: 100, output: 20 },
104+
});
105+
// OpenCode appends, so the history before the window comes first.
106+
yield* db.unsafe("BEGIN");
107+
for (let index = 0; index < 305; index++) {
108+
yield* db.unsafe("INSERT INTO message VALUES (?, ?, ?, ?)", [
109+
`msg-${index}`,
110+
"session-1",
111+
data,
112+
sinceMs + index - 300,
113+
]);
114+
}
115+
yield* db.unsafe("COMMIT");
116+
117+
const spans: number[] = [];
118+
const originalAll = NodeSqlite.StatementSync.prototype.all;
119+
yield* Effect.acquireRelease(
120+
Effect.sync(() =>
121+
vi.spyOn(NodeSqlite.StatementSync.prototype, "all").mockImplementation(function (
122+
this: NodeSqlite.StatementSync,
123+
...params
124+
) {
125+
const rows = originalAll.apply(this, params);
126+
if (this.sourceSQL.includes("FROM message WHERE")) {
127+
const range = /rowid > (-?[\d.]+) AND rowid <= (-?[\d.]+)/.exec(this.expandedSQL);
128+
spans.push(range ? Number(range[2]) - Number(range[1]) : Infinity);
129+
}
130+
return rows;
131+
}),
132+
),
133+
(spy) => Effect.sync(() => spy.mockRestore()),
134+
);
135+
const result = yield* readOpenCodeUsage(dir, sinceMs);
136+
assert.isFalse(result.error);
137+
assert.deepStrictEqual(
138+
result.files.flatMap((file) => file.records).map((record) => record.dedupeKey),
139+
Array.from({ length: 5 }, (_, index) => `opencode:msg-${index + 300}`),
140+
);
141+
assert.isNotEmpty(spans);
142+
assert.isAtMost(Math.max(...spans), 64);
143+
}).pipe(Effect.provide(NodeServices.layer)),
144+
);
145+
90146
it.effect("counts migrated OpenCode messages once and sees subsequent WAL writes", () =>
91147
Effect.gen(function* () {
92148
const fileSystem = yield* FileSystem.FileSystem;

‎packages/provider-opencode/src/server/usage.ts‎

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,34 +188,37 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
188188
const timestamp = columns.has("time_created") ? "time_created" : "NULL";
189189
const predicates = table === "session_message" ? ["type = 'assistant'"] : [];
190190
if (timestamp !== "NULL") predicates.push("time_created >= ?");
191-
const lastRowId = (yield* sql.unsafe<{ readonly last_row_id: number | null }>(
192-
`SELECT MAX(rowid) AS last_row_id FROM ${table}`,
193-
))[0]?.last_row_id;
194-
if (lastRowId === null || lastRowId === undefined) continue;
195-
let afterRowId: number | undefined;
196-
// SqlClient materializes each result with StatementSync.all(). Keep
197-
// message bodies in small pages, and exclude rows appended mid-scan.
198-
while (true) {
199-
const pagePredicates = [
200-
...predicates,
201-
"rowid <= ?",
202-
...(afterRowId === undefined ? [] : ["rowid > ?"]),
203-
];
204-
const rows = yield* sql.unsafe<{
205-
readonly row_id: number;
206-
readonly id: unknown;
207-
readonly session_id: unknown;
208-
readonly data: unknown;
209-
readonly created: unknown;
210-
}>(
211-
`SELECT rowid AS row_id, id, session_id, data, ${timestamp} AS created
212-
FROM ${table} WHERE ${pagePredicates.join(" AND ")} ORDER BY rowid LIMIT ${MESSAGE_PAGE_SIZE}`,
213-
[
214-
...(timestamp === "NULL" ? [] : [sinceMs]),
215-
lastRowId,
216-
...(afterRowId === undefined ? [] : [afterRowId]),
217-
],
218-
);
191+
predicates.push("rowid > ?", "rowid <= ?");
192+
const [range] = yield* sql.unsafe<{
193+
readonly first_row_id: number | null;
194+
readonly last_row_id: number | null;
195+
}>(
196+
`SELECT (SELECT MIN(rowid) FROM ${table}) AS first_row_id, (SELECT MAX(rowid) FROM ${table}) AS last_row_id`,
197+
);
198+
const firstRowId = range?.first_row_id;
199+
const lastRowId = range?.last_row_id;
200+
if (typeof firstRowId !== "number" || typeof lastRowId !== "number") continue;
201+
// SqlClient materializes each result with StatementSync.all(), and the
202+
// window can exclude most rows. Each statement covers MESSAGE_PAGE_SIZE
203+
// rowids, however few match, and rows appended mid-scan are left out.
204+
// Untraced, as a large history runs thousands of them.
205+
for (let after = firstRowId - 1; after < lastRowId; after += MESSAGE_PAGE_SIZE) {
206+
const rows = yield* sql
207+
.unsafe<{
208+
readonly id: unknown;
209+
readonly session_id: unknown;
210+
readonly data: unknown;
211+
readonly created: unknown;
212+
}>(
213+
`SELECT id, session_id, data, ${timestamp} AS created
214+
FROM ${table} WHERE ${predicates.join(" AND ")} ORDER BY rowid`,
215+
[
216+
...(timestamp === "NULL" ? [] : [sinceMs]),
217+
after,
218+
Math.min(after + MESSAGE_PAGE_SIZE, lastRowId),
219+
],
220+
)
221+
.pipe(Effect.withTracerEnabled(false));
219222
for (const row of rows) {
220223
append(
221224
file.records,
@@ -226,8 +229,6 @@ export const readOpenCodeUsage = Effect.fn("readOpenCodeUsage")(function* (
226229
}),
227230
);
228231
}
229-
if (rows.length < MESSAGE_PAGE_SIZE) break;
230-
afterRowId = rows[rows.length - 1]!.row_id;
231232
yield* Effect.yieldNow;
232233
}
233234
}

0 commit comments

Comments
 (0)