Skip to content

Commit 7a498ad

Browse files
committed
fix(routing): incremental history index identity, filter validation, cursor safety (RI-02)
1 parent 85d3b24 commit 7a498ad

3 files changed

Lines changed: 103 additions & 29 deletions

File tree

‎src/routing/history/indexer.ts‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import {
2929
} from "../../usage/log";
3030
import {
3131
HISTORY_DDL,
32-
HISTORY_DB_FILENAME,
3332
HISTORY_META_KEYS,
3433
HISTORY_SCHEMA_VERSION,
34+
historyIndexPath,
3535
} from "./schema";
3636
import {
3737
decodeHistoryCursor,
@@ -83,8 +83,9 @@ let openPromise: Promise<RequestHistoryIndexMeta> | null = null;
8383

8484
function indexDbPath(): string {
8585
const dir = getConfigDir();
86-
recordOwnedConfigPath(dir, `${dir}/${HISTORY_DB_FILENAME}`);
87-
return `${dir}/${HISTORY_DB_FILENAME}`;
86+
const path = historyIndexPath(dir);
87+
recordOwnedConfigPath(dir, path);
88+
return path;
8889
}
8990

9091
function metaValue(dbHandle: Database, key: string): string | null {
@@ -125,11 +126,22 @@ function sourceIdentity(): UsageLogRevision | null {
125126
return currentUsageLogRevision();
126127
}
127128

129+
function readStoredMetaField(dbHandle: Database, key: string): string {
130+
return metaValue(dbHandle, key) ?? "";
131+
}
132+
128133
function sourceIdentityMatches(dbHandle: Database, revision: UsageLogRevision | null): boolean {
129134
const stored = readIndexedMeta(dbHandle);
130135
if (revision === null) return stored.sourceSize === 0;
131-
return stored.sourceSize === Number(revision.size)
132-
&& stored.sourceMtimeMs === Number(revision.mtimeMs);
136+
const storedPath = readStoredMetaField(dbHandle, HISTORY_META_KEYS.sourcePath);
137+
if (!storedPath) return false;
138+
const storedDev = Number(readStoredMetaField(dbHandle, HISTORY_META_KEYS.sourceDev));
139+
const storedIno = Number(readStoredMetaField(dbHandle, HISTORY_META_KEYS.sourceIno));
140+
const storedBirthtimeMs = Number(readStoredMetaField(dbHandle, HISTORY_META_KEYS.sourceBirthtimeMs));
141+
return storedPath === revision.path
142+
&& storedDev === Number(revision.dev)
143+
&& storedIno === Number(revision.ino)
144+
&& storedBirthtimeMs === Number(revision.birthtimeMs);
133145
}
134146

135147
/** Extract the `requests` row columns from a canonical persisted entry. */
@@ -301,6 +313,9 @@ function destroyAndRecreate(path: string, reason: string): Database {
301313
// the file briefly after the throw. Retry the unlink before recreating.
302314
for (let attempt = 0; attempt < 5; attempt++) {
303315
try {
316+
for (const suffix of ["-wal", "-shm"] as const) {
317+
try { unlinkSync(`${path}${suffix}`); } catch { /* sidecar may not exist */ }
318+
}
304319
unlinkSync(path);
305320
break;
306321
} catch {
@@ -449,12 +464,18 @@ export async function rebuildRequestHistoryIndex(): Promise<RequestHistoryIndexM
449464
return metaFor(handle);
450465
}
451466

467+
type HistoryQueryRow = {
468+
row_json: string;
469+
timestamp?: number;
470+
request_id?: string;
471+
};
472+
452473
function queryRows(
453474
handle: Database,
454475
filters: RequestHistoryFilters,
455476
cursor: HistoryCursor | null,
456477
limit: number,
457-
): { rows: Array<{ row_json: string }>; total: number } {
478+
): { rows: Array<HistoryQueryRow>; total: number } {
458479
const where: string[] = [];
459480
const values: Array<string | number> = [];
460481
const add = (clause: string, value: string | number) => {
@@ -479,12 +500,18 @@ function queryRows(
479500
}
480501
const whereSql = where.length > 0 ? ` WHERE ${where.join(" AND ")}` : "";
481502
const rows = handle.query(
482-
`SELECT row_json FROM requests${whereSql} ORDER BY timestamp DESC, request_id DESC LIMIT ?`,
483-
).all(...values, limit + 1) as Array<{ row_json: string }>;
503+
`SELECT row_json, timestamp, request_id FROM requests${whereSql} ORDER BY timestamp DESC, request_id DESC LIMIT ?`,
504+
).all(...values, limit + 1) as Array<HistoryQueryRow>;
484505
return { rows, total: rows.length };
485506
}
486507

487-
function hydrateRow(row: { row_json: string } | undefined): PersistedUsageEntry | null {
508+
function requireDb(): Database {
509+
const handle = db;
510+
if (!handle) throw new Error("request-history index is not open");
511+
return handle;
512+
}
513+
514+
function hydrateRow(row: HistoryQueryRow | undefined): PersistedUsageEntry | null {
488515
if (!row) return null;
489516
try {
490517
const parsed = JSON.parse(row.row_json) as PersistedUsageEntry;
@@ -511,7 +538,7 @@ export async function queryRequestHistory(
511538
Math.max(1, Math.trunc(pageSize ?? REQUEST_HISTORY_DEFAULT_PAGE_SIZE)),
512539
REQUEST_HISTORY_MAX_PAGE_SIZE,
513540
);
514-
const handle = db!;
541+
const handle = requireDb();
515542
const { rows, total } = queryRows(handle, filters, cursor, limit);
516543
const hasMore = total > limit;
517544
const pageRows = rows.slice(0, limit);
@@ -523,15 +550,14 @@ export async function queryRequestHistory(
523550
let nextCursor: string | undefined;
524551
if (hasMore && pageRows.length > 0) {
525552
const last = pageRows[pageRows.length - 1]!;
526-
const parsed = JSON.parse(last.row_json) as PersistedUsageEntry;
527-
nextCursor = encodeHistoryCursor({ t: parsed.timestamp, i: parsed.requestId });
553+
nextCursor = encodeHistoryCursor({ t: last.timestamp!, i: last.request_id! });
528554
}
529555
return { rows: entries, ...(nextCursor ? { nextCursor } : {}), hasMore, meta };
530556
}
531557

532558
export async function requestHistoryRowById(requestId: string): Promise<PersistedUsageEntry | null> {
533559
await openRequestHistoryIndex();
534-
const handle = db!;
560+
const handle = requireDb();
535561
const row = handle.query("SELECT row_json FROM requests WHERE request_id = ?").get(requestId) as
536562
| { row_json: string }
537563
| undefined;

‎src/server/management/request-history-routes.ts‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,51 @@ import { requestLogDto } from "./shared";
2020
import { jsonResponse } from "../auth-cors";
2121
import type { ManagementContext } from "./context";
2222

23-
function parseOptionalInt(raw: string | null): number | undefined {
23+
function parseQueryInt(raw: string | null): number | undefined | "invalid" {
2424
if (raw === null) return undefined;
25-
const value = Number(raw.trim());
26-
return Number.isInteger(value) ? value : undefined;
25+
const trimmed = raw.trim();
26+
if (trimmed.length === 0) return "invalid";
27+
const value = Number(trimmed);
28+
return Number.isInteger(value) ? value : "invalid";
2729
}
2830

2931
export async function handleRequestHistoryRoutes(ctx: ManagementContext): Promise<Response | null> {
3032
const { url, req, config } = ctx;
3133
if (!url.pathname.startsWith("/api/request-history")) return null;
3234

3335
if (url.pathname === "/api/request-history" && req.method === "GET") {
34-
const status = parseOptionalInt(url.searchParams.get("status"));
36+
const statusRaw = parseQueryInt(url.searchParams.get("status"));
37+
if (statusRaw === "invalid") {
38+
return jsonResponse({ error: { code: "invalid_status", message: "status must be an integer from 100 to 599" } }, 400, req, config);
39+
}
40+
const status = statusRaw;
3541
if (status !== undefined && (status < 100 || status > 599)) {
3642
return jsonResponse({ error: { code: "invalid_status", message: "status must be an integer from 100 to 599" } }, 400, req, config);
3743
}
38-
const from = parseOptionalInt(url.searchParams.get("from"));
39-
const to = parseOptionalInt(url.searchParams.get("to"));
44+
const fromRaw = parseQueryInt(url.searchParams.get("from"));
45+
if (fromRaw === "invalid") {
46+
return jsonResponse({ error: { code: "invalid_from", message: "from must be an integer timestamp" } }, 400, req, config);
47+
}
48+
const toRaw = parseQueryInt(url.searchParams.get("to"));
49+
if (toRaw === "invalid") {
50+
return jsonResponse({ error: { code: "invalid_to", message: "to must be an integer timestamp" } }, 400, req, config);
51+
}
52+
const from = fromRaw;
53+
const to = toRaw;
4054
if (from !== undefined && to !== undefined && from > to) {
4155
return jsonResponse({ error: { code: "invalid_range", message: "from must not be after to" } }, 400, req, config);
4256
}
4357
const limitRaw = url.searchParams.get("limit");
44-
const limit = limitRaw === null ? undefined : parseOptionalInt(limitRaw);
58+
const limitParsed = limitRaw === null ? undefined : parseQueryInt(limitRaw);
59+
if (limitParsed === "invalid") {
60+
return jsonResponse(
61+
{ error: { code: "invalid_limit", message: `limit must be an integer from 1 to ${REQUEST_HISTORY_MAX_PAGE_SIZE}` } },
62+
400,
63+
req,
64+
config,
65+
);
66+
}
67+
const limit = limitParsed;
4568
if (limit !== undefined && (limit < 1 || limit > REQUEST_HISTORY_MAX_PAGE_SIZE)) {
4669
return jsonResponse(
4770
{ error: { code: "invalid_limit", message: `limit must be an integer from 1 to ${REQUEST_HISTORY_MAX_PAGE_SIZE}` } },
@@ -90,7 +113,12 @@ export async function handleRequestHistoryRoutes(ctx: ManagementContext): Promis
90113
}
91114

92115
if (url.pathname.startsWith("/api/request-history/") && req.method === "GET") {
93-
const requestId = decodeURIComponent(url.pathname.slice("/api/request-history/".length));
116+
let requestId: string;
117+
try {
118+
requestId = decodeURIComponent(url.pathname.slice("/api/request-history/".length));
119+
} catch {
120+
return jsonResponse({ error: { code: "not_found", message: "unknown request" } }, 404, req, config);
121+
}
94122
if (!requestId || requestId.includes("/")) {
95123
return jsonResponse({ error: { code: "not_found", message: "unknown request" } }, 404, req, config);
96124
}

‎tests/request-history-index.test.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ describe("request-history index (RI-02)", () => {
9898
const page = await queryRequestHistory({}, undefined, 10);
9999
expect(page.rows.length).toBe(5);
100100
expect(page.meta.indexedRows).toBe(5);
101+
const offsetAfterInitial = page.meta.indexedOffset;
102+
expect(page.meta.lastError).not.toMatch(/identity changed/i);
101103
// New appends are picked up without a rebuild.
102104
appendUsageEntry(entry("req-late", 9000));
103105
const after = await queryRequestHistory({}, undefined, 10);
104106
expect(after.rows.length).toBe(6);
105107
expect(after.meta.indexedRows).toBe(6);
108+
expect(after.meta.indexedOffset).toBeGreaterThan(offsetAfterInitial);
109+
expect(after.meta.lastError).not.toMatch(/identity changed/i);
106110
});
107111

108112
test("large history indexes fully and paginates without duplicates or misses", async () => {
@@ -225,6 +229,9 @@ describe("request-history index (RI-02)", () => {
225229

226230
const missing = await apiGet("/api/request-history/missing");
227231
expect(missing.status).toBe(404);
232+
233+
const malformed = await apiGet("/api/request-history/%");
234+
expect(malformed.status).toBe(404);
228235
});
229236

230237
test("API list endpoint returns entries, cursor, hasMore and index status", async () => {
@@ -253,9 +260,23 @@ describe("request-history index (RI-02)", () => {
253260

254261
const badLimit = await apiGet("/api/request-history?limit=9999");
255262
expect(badLimit.status).toBe(400);
263+
const badLimitBody = await badLimit.json() as { error?: { code?: string } };
264+
expect(badLimitBody.error?.code).toBe("invalid_limit");
256265

257266
const badStatus = await apiGet("/api/request-history?status=42");
258267
expect(badStatus.status).toBe(400);
268+
const badStatusText = await apiGet("/api/request-history?status=abc");
269+
expect(badStatusText.status).toBe(400);
270+
const badStatusTextBody = await badStatusText.json() as { error?: { code?: string } };
271+
expect(badStatusTextBody.error?.code).toBe("invalid_status");
272+
273+
const badFrom = await apiGet("/api/request-history?from=abc");
274+
expect(badFrom.status).toBe(400);
275+
276+
const badRange = await apiGet("/api/request-history?from=2000&to=1000");
277+
expect(badRange.status).toBe(400);
278+
const badRangeBody = await badRange.json() as { error?: { code?: string } };
279+
expect(badRangeBody.error?.code).toBe("invalid_range");
259280

260281
await expect(queryRequestHistory({}, "garbage-cursor", 10)).rejects.toBeInstanceOf(InvalidCursorError);
261282
});
@@ -268,15 +289,14 @@ describe("request-history index (RI-02)", () => {
268289
});
269290

270291
test("index rebuild equivalence: rebuilt rows match the ledger exactly", async () => {
271-
for (const row of seedRows(25, 42)) appendUsageEntry(row);
292+
const rows = seedRows(25, 42);
293+
for (const row of rows) appendUsageEntry(row);
272294
await queryRequestHistory({}, undefined, 10);
273-
const first = await rebuildRequestHistoryIndex();
274-
const second = await rebuildRequestHistoryIndex();
275-
const firstPage = await queryRequestHistory({}, undefined, 100);
276-
const secondPage = await queryRequestHistory({}, undefined, 100);
277-
expect(first.indexedRows).toBe(25);
278-
expect(second.indexedRows).toBe(25);
279-
expect(firstPage.rows.map(row => row.requestId)).toEqual(secondPage.rows.map(row => row.requestId));
295+
const canonicalIds = rows.map(row => row.requestId).sort();
296+
const rebuilt = await rebuildRequestHistoryIndex();
297+
expect(rebuilt.indexedRows).toBe(25);
298+
const page = await queryRequestHistory({}, undefined, 100);
299+
expect(page.rows.map(row => row.requestId).sort()).toEqual(canonicalIds);
280300
});
281301

282302
test("cursor stays stable while appends arrive between pages", async () => {

0 commit comments

Comments
 (0)