Public: anyone can read this; the crew can change it.

The search warm-up is bounded and reports how long it took, so a slow warm-up shows as a number #15172

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15166-the-search-warm-up-is-bounded-and-report → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/15166-the-search-warm-up-is-bounded-and-report was merged into main as af62c42.

Review

Sign in to review this change.
3 files changed +133 −8
src/core/search.ts +1 −1
@@ -661,7 +661,7 @@ export interface BudgetTimer {
661661 clear(): void;
662662 }
663663
664 function budgetTimer(ms: number): BudgetTimer {
664 export function budgetTimer(ms: number): BudgetTimer {
665665 let id: ReturnType<typeof setTimeout> | undefined;
666666 const done = new Promise<void>((resolve) => {
667667 id = setTimeout(resolve, ms);
src/search/warm.ts +52 −7
@@ -12,15 +12,60 @@
1212
1313 import type { App } from "../app.ts";
1414 import { search } from "../actions/search.ts";
15 import { budgetTimer, type BudgetTimer } from "../core/search.ts";
1516
1617 /** A query long enough to grep (`MIN_CODE_QUERY`), so the warm fills the grep cache. */
1718 export const WARM_QUERY = "an";
1819
19 /** Fill the caches a first search reads. Never throws: a warm that fails is a cold first visit, nothing more. */
20 export async function warmSearch(app: App, q: string = WARM_QUERY): Promise<void> {
21 try {
22 await search(app, undefined, { q });
23 } catch (error) {
24 app.log.error("search warm failed", { error: String(error) });
25 }
20 /**
21 * The most the warm may take before it stops waiting: the first search must never queue behind it.
22 */
23 export const WARM_BUDGET_MS = 10_000;
24
25 /** How a warm ended: `complete` is false when the budget stopped it or it failed. */
26 export interface WarmReport {
27 readonly ms: number;
28 readonly complete: boolean;
29 }
30
31 /**
32 * `search`, `now` and `timer` are for tests to inject; a monotonic clock and a `setTimeout` by
33 * default.
34 */
35 export interface WarmOptions {
36 readonly budgetMs?: number;
37 readonly search?: (app: App, actor: undefined, input: { q: string }) => Promise<unknown>;
38 readonly now?: () => number;
39 readonly timer?: (ms: number) => BudgetTimer;
40 }
41
42 /**
43 * Fill the caches a first search reads, for at most `budgetMs`. Never throws: a warm that fails is
44 * a cold first visit, nothing more. At the budget it stops waiting and reports `complete: false`; a
45 * grep still running settles on its own (each is bounded by its own timeout) and only a finished
46 * grep is ever cached, so a cut warm leaves the rest cold, never a partial answer standing in for a
47 * whole one.
48 */
49 export async function warmSearch(
50 app: App,
51 q: string = WARM_QUERY,
52 options: WarmOptions = {},
53 ): Promise<WarmReport> {
54 const now = options.now ?? (() => performance.now());
55 const started = now();
56 const timer = (options.timer ?? budgetTimer)(options.budgetMs ?? WARM_BUDGET_MS);
57 const running = (options.search ?? search)(app, undefined, { q }).then(
58 () => "done" as const,
59 (error: unknown) => {
60 app.log.error("search warm failed", { error: String(error) });
61 return "failed" as const;
62 },
63 );
64 const raced = await Promise.race([running, timer.done.then(() => "cut" as const)]);
65 timer.clear();
66 const ms = Math.round(now() - started);
67 const report: WarmReport = { ms, complete: raced === "done" };
68 if (raced === "cut") app.log.error("search warm stopped at its budget", { ms });

A rejected search ends up here and logs info search warmed with complete:false. Log the info line only when raced === "done"; a failure is already logged above.

69 else if (raced === "done") app.log.info("search warmed", { ms });
70 return report;
2671 }
test/search-warm-budget.test.ts added +80
@@ -0,0 +1,80 @@
1 /** The search warm stops at its budget and reports how long it took, on a fake clock. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp } from "../src/app.ts";
7 import { WARM_BUDGET_MS, warmSearch } from "../src/search/warm.ts";
8
9 let dir = "";
10 let app: ReturnType<typeof createApp>;
11 beforeAll(async () => {
12 dir = await mkdtemp(join(tmpdir(), "forge-warm-budget-"));
13 app = createApp({ dataDir: dir, port: 0 });
14 });
15 afterAll(() => rm(dir, { recursive: true, force: true }));
16
17 test("the default budget reaches the timer when budgetMs is omitted", async () => {
outdated

Passes with any positive budget, so it proves nothing. Assert instead that the default budget reaches the timer when budgetMs is omitted.

18 let asked = -1;
19 await warmSearch(app, "an", {
20 search: async () => undefined,
21 timer: (ms) => {
22 asked = ms;
23 return { done: new Promise<void>(() => {}), clear() {} };
24 },
25 });
26 expect(asked).toBe(WARM_BUDGET_MS);
27 });
28
29 test("a search that rejects reports incomplete and is not logged as warmed", async () => {
30 const lines: string[] = [];
31 const record = (message: string) => void lines.push(message);
32 const spied = { ...app, log: { ...app.log, info: record, error: record } } as typeof app;
33 const report = await warmSearch(spied, "an", {
34 search: async () => {
35 throw new Error("boom");
36 },
37 timer: () => ({ done: new Promise<void>(() => {}), clear() {} }),
38 });
39 expect(report.complete).toBe(false);
40 expect(lines).toEqual(["search warm failed"]);
41 });
42
43 test("a warm that finishes inside the budget reports its duration and complete", async () => {
44 let t = 0;
45 const report = await warmSearch(app, "an", {
46 search: async () => undefined,
47 now: () => (t += 40),
48 timer: () => ({ done: new Promise<void>(() => {}), clear() {} }),
49 });
50 expect(report).toEqual({ ms: 40, complete: true });
51 });
52
53 test("a warm that outruns the budget stops waiting and reports incomplete", async () => {
54 let fire: () => void = () => {};
55 let cleared = false;
56 let t = 0;
57 // A search that never answers: only the fake clock can end the warm.
58 const slow = warmSearch(app, "an", {
59 budgetMs: 123,
60 search: () => new Promise(() => {}),
61 now: () => t,
62 timer: (ms) => {
63 expect(ms).toBe(123);
64 return {
65 done: new Promise<void>((r) => {
66 fire = r;
67 }),
68 clear: () => {
69 cleared = true;
70 },
71 };
72 },
73 });
74 t = 123;
75 fire();
76 const report = await slow;
77 expect(cleared).toBe(true);
78 expect(report.ms).toBe(123);
79 expect(report.complete).toBe(false);
80 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs