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

Live triage's comment lookups use an issue index: 3.8 s → 11.5 ms per query #16713

Merged opened this

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

Merged

triage-index → main 0 commits

  • Merged. triage-index was merged into main as 53352fb.

Review

Sign in to review this change.
3 files changed +97 −0
src/core/migration-pins/20261130040757-comment-issue-author.pin added +1
@@ -0,0 +1,1 @@
1 be34c2b89dcdb2be73a0bbbdeb688a62fed988f281e600235e8fcf737de403a1
src/core/migrations/20261130040757-comment-issue-author.ts added +8
@@ -0,0 +1,8 @@
1 // Live triage asks, per open issue, whether the triage user left a note there (`crew/livetriage.ts`
2 // `questions`, `duplicates`). Between `comment_issue_at (issue_id, …)` and `comment_author
3 // (author_id)` SQLite chose the author's, so each issue walked that user's whole history: on
4 // production 249 open issues × 13,717 comments, 3.8 s a query, twice a minute, on the main
5 // thread. `(issue_id, author_id)` answers from the issue's own comments (11.5 ms). Additive.
6 export default `
7 CREATE INDEX IF NOT EXISTS comment_issue_author ON comment(issue_id, author_id);
8 `;
test/livetriage-plan.test.ts added +88
@@ -0,0 +1,88 @@
1 /**
2 * Live triage asks, for every open issue, whether the triage user left a note on it. With only
3 * `comment_issue_at (issue_id, …)` and `comment_author (author_id)` to choose from and no
4 * statistics, SQLite walked the triage user's comments for each issue: on production 249 open
5 * issues × 13,717 of that user's comments, 3.8 s a query and two queries a minute, every one on
6 * the main thread. `comment_issue_author (issue_id, author_id)` answers each from the issue's
7 * own few comments (11.5 ms). This holds every comment lookup the pass runs to an index that
8 * leads with the issue.
9 */
10 import { afterAll, beforeAll, expect, test } from "bun:test";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { createApp, type App } from "../src/app.ts";
15 import { addComment, openIssue } from "../src/actions/issues.ts";
16 import { createRepo, updateRepoSettings } from "../src/actions/repos.ts";
17 import { getRepoById, type Repo } from "../src/core/repos.ts";
18 import { createUser, type User } from "../src/core/users.ts";
19 import { ensureDirectionIssue } from "../src/crew/autonomy.ts";
20 import { liveTriage } from "../src/crew/livetriage.ts";
21
22 let dir = "";
23 let app: App;
24 let alice: User;
25 let repo: Repo;
26 const fresh = () => getRepoById(app.db, repo.id)!;
27
28 beforeAll(async () => {
29 dir = await mkdtemp(join(tmpdir(), "forge-livetriage-plan-"));
30 app = createApp({ dataDir: dir, port: 0 });
31 const a = await createUser(app.db, {
32 origin: "seed",
33 trusted: true,
34 username: "alice",
35 email: "[email protected]",
36 password: "a-good-passphrase",
37 adminIfFirst: true,
38 });
39 if (!a.ok) throw new Error(a.error);
40 alice = a.value;
41 const made = await createRepo(app, alice, { name: "engine", visibility: "private" });
42 if (!made.ok) throw new Error(String(made.error));
43 repo = made.value;
44 const set = await updateRepoSettings(app, alice, repo, {
45 autonomy: "autonomous",
46 autonomyDirection: "Keep it small.",
47 autonomyBy: alice.id,
48 });
49 if (!set.ok) throw new Error(JSON.stringify(set.error));
50 ensureDirectionIssue(app, alice, fresh());
51 for (const title of ["Make the engine start", "Make the engine stop"]) {
52 const opened = openIssue(app, alice, fresh(), { title });
53 if (!opened.ok) throw new Error(String(opened.error));
54 addComment(app, alice, fresh(), opened.value, "On it.");
55 }
56 });
57
58 afterAll(async () => {
59 app.close();
60 await rm(dir, { recursive: true, force: true });
61 });
62
63 test("every comment lookup of a triage pass leads with the issue, never the author's whole history", async () => {
64 const seen = new Set<string>();
65 const real = app.db.query.bind(app.db);
66 app.db.query = ((sql: string) => {
67 seen.add(sql);
68 return real(sql);
69 }) as typeof app.db.query;
70 try {
71 await liveTriage(app, fresh());
72 } finally {
73 app.db.query = real;
74 }
75 const lookups = [...seen].filter((sql) => /FROM comment c WHERE c\.issue_id = i\.id/.test(sql.replace(/\s+/g, " ")));
76 // The two per-issue questions: open questions not yet folded, and duplicates already noted.
77 expect(lookups.length).toBeGreaterThanOrEqual(2);
78 for (const sql of lookups) {
79 const params = Array.from({ length: app.db.prepare(sql).paramsCount }, () => "x");
80 const plan = app.db
81 .query<{ detail: string }, string[]>(`EXPLAIN QUERY PLAN ${sql}`)
82 .all(...params)
83 .map((r) => r.detail)
84 .join("\n");
85 expect(plan).toContain("USING INDEX comment_issue_author (issue_id=? AND author_id=?)");
86 expect(plan).not.toContain("comment_author");
87 }
88 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs