justintsus-experiment Public
// Judge's differential probes: both builds' api.ts in one process, same inputs.import { pathToFileURL } from "node:url";import { DatabaseSync } from "node:sqlite";// Set WORKTREE_A and WORKTREE_B to checkouts of the two builds (Rebasely at 40d9e6fd and a69ca436).process.env.SEARCH_WORKTREE ??= process.env.WORKTREE_A;const H = await import(new URL("../../hidden-tests/helpers.mjs", import.meta.url).href);const WT = { A: process.env.WORKTREE_A, B: process.env.WORKTREE_B,};const api = {};for (const k of Object.keys(WT)) api[k] = await import(pathToFileURL(`${WT[k]}/apps/web/src/search/api.ts`).href);const db = H.fixture();const { CTX, ANON } = H;const OTHER = { ...CTX, orgId: "org-2" };const out = { diffs: [], crashes: { A: [], B: [] }, props: { A: [], B: [] }, notes: [] };function run(k, input, target, ctx = CTX) { const a = api[k]; let r; try { r = a.parseQuery(input, target); } catch (e) { out.crashes[k].push({ where: "parse", input, target, e: String(e) }); return { crash: true }; } if (!r.ok) return { ok: false, errs: r.errors.map((e) => `${e.code}@${e.start}-${e.end}`), msgs: r.errors.map((e) => e.message) }; let c, s, rows; try { c = a.serializeQuery(r.query); } catch (e) { out.crashes[k].push({ where: "serialize", input, target, e: String(e) }); return { crash: true }; } try { s = a.toSql(r.query, ctx); } catch (e) { out.crashes[k].push({ where: "toSql", input, target, e: String(e) }); return { crash: true }; } const ph = H.placeholders(s.where); if (ph !== s.params.length) out.props[k].push({ input, target, prop: "placeholders", ph, n: s.params.length }); try { rows = db.prepare(`SELECT number, repo_id, org_id FROM ${H.tableOf(target)} WHERE ${s.where} AND number > 0 ORDER BY ${s.orderBy}`).all(...s.params); } catch (e) { out.crashes[k].push({ where: "sqlite", input, target, e: String(e), sql: s.where }); return { crash: true }; } for (const row of rows) if (row.repo_id !== ctx.repoId || row.org_id !== ctx.orgId) out.props[k].push({ input, target, prop: "scope-leak", row }); // round trip const r2 = a.parseQuery(c, target); if (!r2.ok) out.props[k].push({ input, target, prop: "canon-not-parse", c, errs: r2.errors }); else { const c2 = a.serializeQuery(r2.query); if (c2 !== c) out.props[k].push({ input, target, prop: "canon-not-fixed", c, c2 }); const s2 = a.toSql(r2.query, ctx); if (s2.where !== s.where || JSON.stringify(s2.params) !== JSON.stringify(s.params) || s2.orderBy !== s.orderBy) out.props[k].push({ input, target, prop: "canon-sql-differs", c }); } return { ok: true, canon: c, rows: rows.map((x) => x.number).join(","), nparams: s.params.length, where: s.where, params: s.params };}function both(input, target, ctx = CTX, label = "") { const a = run("A", input, target, ctx); const b = run("B", input, target, ctx); const ka = a.ok ? `ok canon=${JSON.stringify(a.canon)} rows=[${a.rows}]` : a.crash ? "CRASH" : `err ${a.errs.join(" ")}`; const kb = b.ok ? `ok canon=${JSON.stringify(b.canon)} rows=[${b.rows}]` : b.crash ? "CRASH" : `err ${b.errs.join(" ")}`; if (ka !== kb) out.diffs.push({ label, input, target, ctx: ctx === CTX ? "CTX" : ctx === ANON ? "ANON" : "other", A: ka, B: kb }); return { a, b };}// ---------- 1. targeted probesconst T = (q, t = "issues", ctx = CTX) => both(q, t, ctx, "targeted");const INJ = [ `'; DROP TABLE issues; --`, `") OR 1=1 --`, `label:"x') OR 1=1 --"`, `author:"' OR '1'='1"`, `"%' OR 1=1 --"`, `#1;DELETE`, `created:2026-10-01'--`, `sort:created-desc;DROP`, `sort:"created_at DESC; --"`, `review-requested:"x' OR 1"`, `?1 ?2 :name @name $x`, `label:"bug\\") OR (\\"1"`, `"\u0000"`, `author:fixer'--`, `a
b`, `"\\\\\\""`,];for (const q of INJ) { T(q, "issues"); T(q, "pulls"); }// ctx carrying SQLfor (const k of ["A", "B"]) { const evil = { repoId: "repo-1' OR 1=1 --", orgId: "x\") OR (1=1", viewerId: "' OR 1=1 --", now: H.NOW }; const r = api[k].parseQuery("author:@me crash label:bug", "issues"); const s = api[k].toSql(r.query, evil); const leaks = [evil.repoId, evil.orgId, evil.viewerId].filter((v) => s.where.includes(v)); out.notes.push(`${k}: evil ctx values in SQL text: ${leaks.length}`);}// cross-org agent slugs, author/review on pullsfor (const q of ["author:fixer", "-author:fixer", "author:FIXER", "review-requested:fixer", "-review-requested:fixer", "author:nobody", "-author:nobody"]) { T(q, "issues"); T(q, "pulls"); T(q, "pulls", ANON);}// what an org-2 viewer sees for author:fixer (the fixer of org-2 is FIXER2)T("author:fixer", "issues", OTHER); T("author:fixer", "pulls", OTHER);// NULL semantics under negationfor (const q of ["-author:@me", "-author:" + H.ME, "-author:" + H.BOB, "-review-requested:@me", "-is:draft", "-is:closed", "-is:merged", "-label:bug", "-crash", "-\"needs review\"", "no:label", "-label:docs -label:bug"]) { T(q, q.includes("label") ? "issues" : "pulls"); T(q, q.includes("label") ? "issues" : "pulls", ANON);}T("-author:@me", "issues", ANON); T("author:@me", "issues", ANON);// LIKE escapingfor (const q of ["100%", "a_c", "a_b", "%", "_", "\\", "a\\b", "\"a\\\\b\"", "\"100% \"", "%%", "x%y", "Ü", "ü", "ünïcode", "ÜNÏCODE", "🐛", "CRASH", "crash on", "\"crash on\""]) { T(q, "issues"); T(q, "pulls"); }// unicode / surrogate spansconst UNI = ["🐛:x", "label:🐛", "foo:🐛 bar", "\"🐛", "🐛\"x", "-\"🐛\"x", "a 🐛\" b", "é́:x", "is:🐛", "label:\"🐛 bug\"", "\uD83D", "\uDC1B:", "a\uD83D\"", " is:open label:bug ", "is:open", "is:open", "\u0085is:open", "is:openlabel:bug"];for (const q of UNI) { T(q, "issues"); }// span edge casesfor (const q of ["\"abc", "-\"abc", " \"abc def", "label:\"x", "x\"y z", "-x\"y", "\"a\"b c", "label:\"a\"b", "label:\"\"", "\"\"", "\" \"", "-\"\"", "-", "- -", "--x", "-#5", "#0", "#1234567890", "#123456789", "sort:created-asc sort:updated-asc", "sort:created-asc sort:CREATED-ASC", "sort:x sort:y", "is:open is:open", "IS:OPEN is:open", "-is:open is:open"]) { T(q, "issues"); T(q, "pulls"); }// datesconst DATES = ["2026-10-01", ">2026-10-01", ">=2026-10-01", "<2026-10-01", "<=2026-10-01", "2026-10-01..2026-10-05", "2026-10-05..2026-10-01", "2026-10-01..*", "*..2026-10-01", "*..*", "2026-02-29", "2024-02-29", "1969-12-31", "1970-01-01", "9999-12-31", "10000-01-01", "<7d", "<=7d", ">7d", ">=7d", "<0007D", "<0d", "<10000d", "<9999w", "7d", "=2026-10-01", "<1H", "<1W", ">2026-10-01..2026-10-02", "2026-10-01...2026-10-02", "2026-1-01", "2026-10-01..", "..2026-10-01", "<", "<=", "2026-10-01..*..*", "<1e3d", "<+7d", "< 7d", "<7.5d", "<٧d", "2026-10-01", "2026-13-01", "2026-04-31", "<2026-10-01T00:00", "*", "<*"];for (const v of DATES) for (const f of ["created", "updated"]) { T(`${f}:${v}`, "issues"); T(`${f}:${v}`, "pulls"); }// labelsfor (const q of ["label:BUG", "label:\" Needs Review \"", "label:\"needs\treview\"", "label:a" + "b".repeat(31), "label:a" + "b".repeat(32), "label:-x", "label:.x", "label:x!", "label:\"needs review\" label:\"needs review\"", "label:ünï", "-label:\"needs review\"", "no:LABEL", "no:labels", "-no:label"]) T(q, "issues");// usersfor (const q of ["author:@ME", "author:@you", "author:@", "author:" + H.ME.toUpperCase(), "author:11111111-1111-4111-8111-11111111111", "author:-x", "author:a".padEnd(47, "b"), "author:a".padEnd(48, "b"), "author:x_y", "review-requested:@me", "review-requested:" + H.ALICE]) { T(q, "issues"); T(q, "pulls"); }// unknown/unsupported orderingfor (const q of ["foo:", "-foo:", "label:", "-label:", "no:", "-no:", "sort:", "-sort:x", "-sort:", "-created:x", "created:", "Review-Requested:@me", "review-requested:", "http://x", "12:30", "a1:b", ":x", "a-:x", "a--b:x", "a-b-c:x", "\"label:bug\"", "label:bug:x", "is:open:x"]) { T(q, "issues"); T(q, "pulls"); }// ---------- 2. limitsconst long = "a ".repeat(512); // 1024T(long, "issues"); T(long + "b", "issues"); T("x".repeat(1025), "issues");const t32 = Array.from({ length: 32 }, (_, i) => `w${i}`).join(" ");T(t32); T(t32 + " w32"); T(t32 + " \"unterminated"); T(t32 + " foo:bar w33");T(Array.from({ length: 33 }, () => "\"\"").join(" "));T(Array.from({ length: 31 }, () => "x").join(" ") + " \"a b c d e f\" y z");T("\"" + "a ".repeat(100)); // unterminated with many spaces: one term// worst-case params: 32 heavy termsconst heavy = { issues: [...Array.from({ length: 16 }, (_, i) => `t${i}`), ...Array.from({ length: 8 }, (_, i) => `created:2026-10-${String(i + 1).padStart(2, "0")}`), ...Array.from({ length: 8 }, (_, i) => `updated:2026-0${i + 1}-01..2026-12-01`)], pulls: [...Array.from({ length: 10 }, (_, i) => `author:agent-${i}`), ...Array.from({ length: 10 }, (_, i) => `-author:${"0000000" + i}-1111-4111-8111-111111111111`), ...Array.from({ length: 6 }, (_, i) => `created:2026-10-${String(i + 1).padStart(2, "0")}`), ...Array.from({ length: 6 }, (_, i) => `review-requested:bot-${i}`)],};for (const t of ["issues", "pulls"]) { const q = heavy[t].join(" "); for (const k of ["A", "B"]) { const r = api[k].parseQuery(q, t); if (!r.ok) { out.notes.push(`${k} heavy ${t}: did not parse ${JSON.stringify(r.errors.slice(0, 2))} len=${q.length}`); continue; } const s = api[k].toSql(r.query, CTX); out.notes.push(`${k} heavy ${t}: ${r.query.terms.length} terms, ${s.params.length} params (+2 for LIMIT/OFFSET on the page = ${s.params.length + 2}); where length ${s.where.length}`); }}// brute-force worst case: maximize params per term per buildfor (const t of ["issues", "pulls"]) { const cands = t === "issues" ? ["crash", "author:fixer", "author:@me", "label:bug", "created:2026-10-01", "created:2026-10-01..2026-10-02", "-author:fixer", "-crash", "#1", "updated:<7d"] : ["crash", "author:fixer", "author:@me", "-author:@me", "review-requested:fixer", "review-requested:@me", "created:2026-10-01", "-author:fixer", "#1"]; for (const k of ["A", "B"]) { let best = { c: "", n: 0 }; for (const c of cands) { const s = api[k].toSql(api[k].parseQuery(c, t).query, CTX); if (s.params.length - 2 > best.n) best = { c, n: s.params.length - 2 }; } out.notes.push(`${k} ${t}: most params per term ${best.n} (${best.c}) -> 32 distinct such terms = ${2 + 32 * best.n} params + 2 (LIMIT, OFFSET) = ${4 + 32 * best.n}`); }}// ---------- 3. random differential fuzz + timingconst parts = ["is:", "open", "closed", "merged", "draft", "label:", "bug", "\"", "\\", "-", " ", " ", " ", " ", " ", "#", "1", "0", "9", "a", "ü", "🐛", "\uD83D", ":", "author:", "@me", "fixer", "review-requested:", "created:", "updated:", ">", "<", "=", "2026-10-01", "2024-02-29", "..", "*", "7d", "007D", "sort:", "updated-asc", "comments-desc", "%", "_", "'", ";", "no:", "crash", "OR", "1=1", "--", "needs review", "\t", "\n", "Ü", "A-b:", "x"];const rand = H.prng(424242);const corpus = [];for (let i = 0; i < 10000; i++) { let s = ""; const n = 1 + Math.floor(rand() * 14); for (let j = 0; j < n; j++) s += parts[Math.floor(rand() * parts.length)]; corpus.push(s);}// pathological long inputsconst rlong = H.prng(7);for (let i = 0; i < 200; i++) { let s = ""; while (s.length < 1024) s += parts[Math.floor(rlong() * parts.length)]; corpus.push(s.slice(0, 1000 + Math.floor(rlong() * 40))); }let diffCount = 0;const diffSample = [];for (const q of corpus) for (const t of ["issues", "pulls"]) { const before = out.diffs.length; both(q, t, rand() < 0.3 ? ANON : CTX, "fuzz"); if (out.diffs.length > before) { diffCount++; }}out.notes.push(`fuzz: ${corpus.length * 2} (input, target) pairs, ${diffCount} differ`);// timing: parse + serialize + toSql only, 10k random queries, 5 rounds, medianfor (const k of ["A", "B"]) { const a = api[k]; const times = []; for (let round = 0; round < 5; round++) { const t0 = performance.now(); for (let i = 0; i < 10000; i++) { const r = a.parseQuery(corpus[i], i % 2 ? "issues" : "pulls"); if (r.ok) { a.serializeQuery(r.query); a.toSql(r.query, CTX); } } times.push(performance.now() - t0); } times.sort((x, y) => x - y); // worst-case single input: 1024 chars const worst = ["\"" + "\\\"".repeat(511) + "x", "a".repeat(1024), ("-x ").repeat(341) + "y", ("label:\"" + "a b".repeat(3) + "\" ").repeat(60).slice(0, 1024), " ".repeat(1024), "\"".repeat(1024), ("\\").repeat(1023) + "\""]; const wt = worst.map((w) => { const t0 = performance.now(); for (let i = 0; i < 1000; i++) a.parseQuery(w, "issues"); return ((performance.now() - t0) / 1000 * 1000).toFixed(1); }); out.notes.push(`${k} timing: 10k random queries median ${times[2].toFixed(1)} ms (min ${times[0].toFixed(1)}, max ${times[4].toFixed(1)}); worst-case 1024-char inputs, us per parse: ${wt.join(", ")}`);}// ---------- 4. never throws on odd args (beyond the contract)for (const k of ["A", "B"]) { const r = []; for (const [label, f] of [["parse(undefined)", () => api[k].parseQuery(undefined, "issues")], ["parse(null)", () => api[k].parseQuery(null, "issues")], ["parse(123)", () => api[k].parseQuery(123, "issues")], ["parse(x, 'bogus')", () => api[k].parseQuery("is:open", "bogus")], ["toSql(viewerId undefined)", () => api[k].toSql(api[k].parseQuery("author:@me", "issues").query, { ...CTX, viewerId: undefined })], ["toSql(now NaN, <7d)", () => api[k].toSql(api[k].parseQuery("updated:<7d", "issues").query, { ...CTX, now: NaN })], ["toSql(issues query, pulls-looking ctx)", () => api[k].toSql(api[k].parseQuery("label:bug", "issues").query, CTX)]]) { try { const v = f(); r.push(`${label}: ${v && typeof v === "object" ? ("ok" in v ? `ok=${v.ok}${v.ok ? "" : " " + v.errors.map((e) => e.code)}` : `params=${JSON.stringify(v.params)}`) : String(v)}`); } catch (e) { r.push(`${label}: THROWS ${String(e).slice(0, 80)}`); } } out.notes.push(`${k} odd args: ${r.join(" | ")}`);}// ---------- 5. invalid JSON in labels column (robustness)db.exec(`INSERT INTO issues (repo_id, number, org_id, author_id, title, body, state, labels, comments, created_at, updated_at) VALUES ('repo-1', 77, 'org-1', '${H.ME}', 'bad labels row', '', 'open', 'not json', 0, 0, 0)`);for (const q of ["label:bug", "-label:bug", "no:label", "crash"]) both(q, "issues", CTX, "bad-json-labels");// reportconst byLabel = {};for (const d of out.diffs) (byLabel[d.label] ??= []).push(d);console.log(JSON.stringify({ notes: out.notes, crashes: out.crashes, props: { A: out.props.A.slice(0, 30), B: out.props.B.slice(0, 30), nA: out.props.A.length, nB: out.props.B.length }, diffs: Object.fromEntries(Object.entries(byLabel).map(([k, v]) => [k, { n: v.length, items: v.slice(0, 80) }])) }, null, 1));