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

Every search door takes the same longest query, and no page echoes more of it than fits on a line #13862

Merged opened this

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

Merged

mason/forge:mason/13846-every-search-door-takes-the-same-longest → main 2 commits fork

  • Merged. mason/13846-every-search-door-takes-the-same-longest was merged into main as 24d2d90.

Review

Sign in to review this change.
10 files changed +146 −25
src/actions/explore.ts +3 −3
@@ -16,6 +16,7 @@
1616 * - Forges are the children this forge spawned, for a site admin.
1717 */
1818
19 import { cappedQuery, MAX_QUERY_CHARS } from "../core/query_limit.ts";
1920 import type { Database } from "bun:sqlite";
2021 import type { App } from "../app.ts";
2122 import { appsForRepos } from "../core/apps.ts";
@@ -138,8 +139,7 @@ export function exploreLineFrom(get: (name: string) => string | null | undefined
138139 if (value !== "") given.set(key, value);
139140 }
140141 // A parameter wins: the same key typed in `q` is dropped, not left to the reader's order.
141 const typed = (get("q") ?? "")
142 .trim()
142 const typed = cappedQuery(get("q"))
143143 .split(/\s+/)
144144 .filter((w) => w !== "" && !(w.includes(":") && given.has(w.slice(0, w.indexOf(":")).toLowerCase())));
145145 return [...typed, ...[...given].map(([k, v]) => `${k}:${v}`)].join(" ");
@@ -244,7 +244,7 @@ export function readExploreLine(line: string): ExploreRead {
244244 query: {
245245 kind,
246246 sort,
247 text: words.join(" ").slice(0, 100),
247 text: words.join(" ").slice(0, MAX_QUERY_CHARS),
248248 archived,
249249 updated,
250250 ...(owner === undefined ? {} : { owner }),
src/core/query_limit.ts added +32
@@ -0,0 +1,32 @@
1 /**
2 * The longest search query, for every door that takes one: /explore, /search and /docs, as pages
3 * and as `GET /api/v1/explore` and `GET /api/v1/search`. Longer is a paste, not a search.
4 */
5
6 /** The longest query, in characters. */
7 export const MAX_QUERY_CHARS = 200;
8
9 /** The most of a query an empty-state title or chip repeats back. */
10 export const ECHO_CHARS = 60;
11
12 /** Whether a raw `q` is longer than the limit (after trimming). */
13 export function queryTooLong(raw: string | null | undefined): boolean {
14 return [...(raw ?? "").trim()].length > MAX_QUERY_CHARS;
15 }
16
17 /** The API's 422 message for a `q` that is too long. */
18 export const QUERY_TOO_LONG = `q is at most ${MAX_QUERY_CHARS} characters`;
19
20 /** The one line a page says when it searched only the first characters of a longer `q`. */
21 export const QUERY_TRIMMED_NOTE = `That search was longer than ${MAX_QUERY_CHARS} characters, so this is the first ${MAX_QUERY_CHARS}.`;
22
23 /** The first `MAX_QUERY_CHARS` characters of a trimmed `q`. */
24 export function cappedQuery(raw: string | null | undefined): string {
25 return [...(raw ?? "").trim()].slice(0, MAX_QUERY_CHARS).join("");
26 }
27
28 /** `text` for a title or chip: at most `max` characters, then an ellipsis. */
29 export function echoed(text: string, max: number = ECHO_CHARS): string {
30 const chars = [...text];
31 return chars.length <= max ? text : `${chars.slice(0, max).join("").trimEnd()}…`;
32 }
src/core/search.ts +16 −6
@@ -30,13 +30,14 @@ import { runGitCapture } from "../git/env.ts";
3030 import { isSafeRef } from "../git/guard.ts";
3131 import { repoDir } from "../git/repos.ts";
3232 import { GREP_LIMITS, grepSlots, type GrepSlots } from "../search/grep_slots.ts";
33 import { MAX_QUERY_CHARS } from "./query_limit.ts";
3334 import { appsForRepos } from "./apps.ts";
3435 import { likeEscape } from "./issues.ts";
3536 import { enterableApps, listReposVisibleTo, type Repo } from "./repos.ts";
3637 import type { User } from "./users.ts";
3738
38 /** The longest query we will run. Longer is a paste, not a search. */
39 export const MAX_QUERY = 200;
39 /** The longest query we will run: the one limit every search door shares. */
40 export const MAX_QUERY = MAX_QUERY_CHARS;
4041 /** Repos considered for one query (and, for code, grepped). */
4142 export const MAX_SEARCH_REPOS = 50;
4243 /** Total code hits returned, across every repo. */
@@ -47,7 +48,10 @@ export const MAX_HITS_PER_REPO = 20;
4748 export const GREP_TIMEOUT_MS = 3_000;
4849 /** A first code search starts no new grep after this long; `?code=all` runs the rest. */
4950 export const CODE_BUDGET_MS = 30;
50 /** A grep in a budgeted search is given up after this long and reported as `more`: a cold clone must not hold the page (#7198). */
51 /**
52 * A grep in a budgeted search is given up after this long and reported as `more`: a cold clone must
53 * not hold the page (#7198).
54 */
5155 export const BUDGETED_GREP_MS = 400;
5256
5357 /**
@@ -76,7 +80,8 @@ export function searchKind(raw: string | null): SearchKind {
7680
7781 /**
7882 * What a caller who asked wrongly is told, in one sentence, or undefined when
79 * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text: the caller escapes it where it draws it.
83 * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text:
84 * the caller escapes it where it draws it.
8085 */
8186 export function searchNote(rawQ: string | null, kind: SearchKind = "all"): string | undefined {
8287 if (cleanQuery(rawQ ?? "") === "")
@@ -248,7 +253,9 @@ function rankByName(
248253 .map((x) => x.r);
249254 }
250255
251 /** The one repository a scoped search is about, when its owner, name or description holds the words. */
256 /**
257 * The one repository a scoped search is about, when its owner, name or description holds the words.
258 */
252259 export function matchOneRepo(repo: Repo, query: string, app: boolean): RepoHit[] {
253260 const needle = query.trim().toLowerCase();
254261 const hay = `${repo.ownerName}/${repo.name} ${repo.description}`.toLowerCase();
@@ -611,7 +618,10 @@ export interface GrepWho {
611618 readonly now?: () => number;
612619 /** Resolves after `ms`; `clear` stops it. A `setTimeout` by default. */
613620 readonly timer?: (ms: number) => BudgetTimer;
614 /** The first batch's ceiling (`BUDGETED_GREP_MS`), kept apart from `timer` so tests fire each on its own. */
621 /**
622 * The first batch's ceiling (`BUDGETED_GREP_MS`), kept apart from `timer` so tests fire each on
623 * its own.
624 */
615625 readonly firstTimer?: (ms: number) => BudgetTimer;
616626 }
617627
src/http/api.ts +4
@@ -8,6 +8,7 @@
88 * back to continue); errors are `{ error, message }`.
99 */
1010
11 import { queryTooLong, QUERY_TOO_LONG } from "../core/query_limit.ts";
1112 import { parseAttestation } from "../core/attestation.ts";
1213 import { listAppReleases } from "../core/app_releases.ts";
1314 import { openapiHandler } from "../contract/served.ts";
@@ -1591,6 +1592,7 @@ export function apiRoutes(router: Router<Handler>): void {
15911592 const q = ctx.url.searchParams;
15921593 const refusal = searchRefusal(q.get("kind"), q.get("limit"));
15931594 if (refusal) return apiError(refusal.code, 422, refusal.message);
1595 if (queryTooLong(q.get("q"))) return json({ error: "invalid", field: "q", message: QUERY_TOO_LONG }, 422);
15941596 const kind = searchKind(q.get("kind"));
15951597 const query = q.get("q") ?? "";
15961598 const greps =
@@ -3617,6 +3619,8 @@ export function apiRoutes(router: Router<Handler>): void {
36173619 if (limitRaw !== null && !(/^\d{1,3}$/.test(limitRaw) && Number(limitRaw) >= 1 && Number(limitRaw) <= 100))
36183620 return json({ error: "invalid", field: "limit", message: "limit is an integer from 1 to 100" }, 422);
36193621 const limit = limitRaw === null ? 30 : Number(limitRaw);
3622 if (queryTooLong(ctx.url.searchParams.get("q")))
3623 return json({ error: "invalid", field: "q", message: QUERY_TOO_LONG }, 422);
36203624 const line = exploreLineFrom((name) => ctx.url.searchParams.get(name));
36213625 const { query, unknown } = readExploreLine(line);
36223626 for (const field of ["sort", "kind"] as const) {
src/web/pages/api.ts +2 −2
@@ -12,6 +12,7 @@ import { narrow, reference, type Area } from "../../contract/reference.ts";
1212 import { openapiText, roleOf } from "../../contract/served.ts";
1313 import type { Role } from "../../contract/openapi/shared.ts";
1414 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
15 import { cappedQuery } from "../../core/query_limit.ts";
1516 import { canEnterApp } from "../../core/repos.ts";
1617 import type { Ctx } from "../../http/context.ts";
1718 import { noRoute, prefersJson } from "../../http/respond.ts";
@@ -21,8 +22,7 @@ import { loadRepo, notFoundPage, repoBase, repoHeader } from "../chrome.ts";
2122 import { html } from "../html.ts";
2223 import { page } from "../layout.ts";
2324
24 const MAX_QUERY = 100;
25 const asked = (ctx: Ctx): string => (ctx.url.searchParams.get("q") ?? "").trim().slice(0, MAX_QUERY);
25 const asked = (ctx: Ctx): string => cappedQuery(ctx.url.searchParams.get("q"));
2626 const count = (areas: readonly Area[]): number => areas.reduce((n, a) => n + a.operations.length, 0);
2727
2828 /**
src/web/pages/search.ts +10 −5
@@ -10,6 +10,7 @@
1010 * that was never meant to be confirmed.
1111 */
1212
13 import { cappedQuery, echoed, queryTooLong, QUERY_TRIMMED_NOTE } from "../../core/query_limit.ts";
1314 import { returnTo, withReturn } from "../return_to.ts";
1415 import { DETACH_AGAIN, DETACH_STAYS, DETACH_STOPS } from "../../actions/detach_words.ts";
1516 import { DISCONNECT_DETAIL } from "../../actions/origin_words.ts";
@@ -387,7 +388,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> {
387388 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
388389 const params = ctx.url.searchParams;
389390 const a: Asked = {
390 query: cleanQuery(params.get("q") ?? ""),
391 query: cleanQuery(cappedQuery(params.get("q"))),
391392 kind: searchKind(params.get("kind")),
392393 live: params.get("live") === "1",
393394 codeAll: params.get("code") === "all",
@@ -419,13 +420,17 @@ export async function searchPage(ctx: Ctx): Promise<Response> {
419420 </button>
420421 </form>`;
421422
423 const trimmed = queryTooLong(params.get("q"))
424 ? html`<p class="muted small" role="status" data-query-trimmed>${QUERY_TRIMMED_NOTE}</p>`
425 : html``;
426
422427 if (a.query === "") {
423428 const rendering = performance.now();
424429 const blank = page({
425430 ctx,
426431 title: "Search",
427432 styleCss: SEARCH_CSS,
428 body: html`<div data-search data-palette-auto>${pageHead("Search")} ${form}
433 body: html`<div data-search data-palette-auto>${pageHead("Search")} ${form}${trimmed}
429434 </div>`,
430435 });
431436 notePhase(ctx.request, "render", performance.now() - rendering);
@@ -509,7 +514,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> {
509514 const nothing =
510515 a.kind === "all" &&
511516 found.repos.length + found.issues.length + found.code.length === 0;
512 const body = html`<div data-search>${pageHead(html`Results for “${a.query}”`)} ${form}
517 const body = html`<div data-search>${pageHead(html`Results for “${echoed(a.query)}”`)} ${form}${trimmed}
513518 <div class="bar mb">${chips}</div>
514519 ${liveToggle}
515520 ${when(
@@ -535,7 +540,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> {
535540 )}
536541 ${nothing
537542 ? html`<p class="muted next-step" data-next-step>
538 Nothing matches “${a.query}”. Search reads names, descriptions, issues,
543 Nothing matches “${echoed(a.query)}”. Search reads names, descriptions, issues,
539544 pulls and the code you can see. <a href="/explore">Explore</a>
540545 ${when(
541546 a.query !== "",
@@ -552,7 +557,7 @@ export async function searchPage(ctx: Ctx): Promise<Response> {
552557 .map((w) =>
553558 section(a, w, ...rows[w[0]](), w[0] === "code" ? moreCode : null),
554559 )}</div>`;
555 const response = page({ ctx, title: `Search · ${a.query}`, body, styleCss: SEARCH_CSS });
560 const response = page({ ctx, title: `Search · ${echoed(a.query)}`, body, styleCss: SEARCH_CSS });
556561 notePhase(ctx.request, "render", performance.now() - rendering);
557562 return response;
558563 }
src/web/studio/explore.ts +4 −2
@@ -6,6 +6,7 @@
66 * `actions/explore.ts`; this page only draws it.
77 */
88
9 import { echoed, queryTooLong, QUERY_TRIMMED_NOTE } from "../../core/query_limit.ts";
910 import {
1011 explorePageAt,
1112 explorePageFrom,
@@ -115,7 +116,7 @@ function emptyState(ctx: Ctx, query: ExploreQuery): Html {
115116 if (line !== "")
116117 return empty({
117118 icon: "search",
118 title: `Nothing matches ${line}`,
119 title: `Nothing matches ${echoed(line)}`,
119120 note: "The chips above take one filter off; Clear filters above takes them all.",
120121 });
121122 return empty({
@@ -137,7 +138,7 @@ function chipRow(query: ExploreQuery): Html {
137138 const chips = [...removable(query), ...terms(query).filter((t) => t.term.startsWith("sort:"))];
138139 if (chips.length === 0) return html``;
139140 return html`<div class="bar mb small" data-filter-chips role="group" aria-label="Active filters">
140 ${chips.map((t) => btn(html`${shorten(t.term)} <span aria-hidden="true">×</span>`, { href: href(t.without), size: "sm", title: `Remove ${t.term}`, label: `Remove filter ${t.term}` }))}
141 ${chips.map((t) => btn(html`${shorten(t.term)} <span aria-hidden="true">×</span>`, { href: href(t.without), size: "sm", title: `Remove ${echoed(t.term)}`, label: `Remove filter ${echoed(t.term)}` }))}
141142 </div>`;
142143 }
143144
@@ -252,6 +253,7 @@ export async function explorePage(ctx: Ctx): Promise<Response> {
252253 },
253254 ])}
254255 </div>
256 ${queryTooLong(ctx.url.searchParams.get("q")) ? html`<p class="muted mb" role="status" data-query-trimmed>${QUERY_TRIMMED_NOTE}</p>` : html``}
255257 ${chipRow(query)}
256258 ${oldLink ? html`<p class="muted mb" role="status" data-old-cursor>That page link is out of date, so this is the first page.</p>` : html``}
257259 ${note === "" ? html`` : html`<p class="muted mb" role="status">${note}${
test/query-limit.test.ts added +65
@@ -0,0 +1,65 @@
1 /** One longest query for every search door (#13846). */
2 import { afterAll, beforeAll, describe, 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, type App } from "../src/app.ts";
7 import { echoed, ECHO_CHARS, MAX_QUERY_CHARS } from "../src/core/query_limit.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9
10 let dataDir: string;
11 let app: App;
12 let servers: Servers;
13 const long = "a".repeat(MAX_QUERY_CHARS + 1);
14 const at = (path: string) => fetch(`${servers.url}${path}`);
15
16 beforeAll(async () => {
17 dataDir = await mkdtemp(join(tmpdir(), "forge-query-limit-"));
18 app = createApp({ dataDir, port: 0 });
19 servers = await serve(app);
20 });
21 afterAll(async () => {
22 servers.stop();
23 await rm(dataDir, { recursive: true, force: true });
24 });
25
26 describe("the API", () => {
27 for (const path of ["/api/v1/explore", "/api/v1/search"]) {
28 test(`${path} answers 422 naming q and the limit`, async () => {
29 const res = await at(`${path}?q=${long}`);
30 expect(res.status).toBe(422);
31 const body = (await res.json()) as { field: string; message: string };
32 expect(body.field).toBe("q");
33 expect(body.message).toContain(String(MAX_QUERY_CHARS));
34 });
35 test(`${path} takes a query at the limit`, async () => {
36 expect((await at(`${path}?q=${"a".repeat(MAX_QUERY_CHARS)}`)).status).toBe(200);
37 });
38 }
39 });
40
41 describe("the pages", () => {
42 for (const path of ["/explore", "/search", "/docs"]) {
43 test(`${path} searches the first characters and says so`, async () => {
44 const res = await at(`${path}?q=${long}`);
45 expect(res.status).toBe(200);
46 const text = await res.text();
47 expect(text).toContain("data-query-trimmed");
48 // The box holds the capped query, never the whole paste.
49 const value = /name="q"[^>]*value="([^"]*)"|value="([^"]*)"[^>]*name="q"/.exec(text);
50 expect((value?.[1] ?? value?.[2] ?? "").length).toBe(MAX_QUERY_CHARS);
51 expect(text.length).toBeLessThan(60_000);
52 });
53 }
54 test("the explorer's empty title repeats at most 60 characters", async () => {
55 const text = await (await at(`/explore?q=${"b".repeat(150)}`)).text();
56 expect(text).toContain(`Nothing matches ${"b".repeat(ECHO_CHARS)}…`);
57 const title = /empty-title">([^<]*)</.exec(text)?.[1] ?? "";
58 expect(title).toBe(`Nothing matches ${"b".repeat(ECHO_CHARS)}…`);
59 });
60 });
61
62 test("echoed shortens with an ellipsis and leaves short text alone", () => {
63 expect(echoed("short")).toBe("short");
64 expect(echoed("x".repeat(100))).toBe(`${"x".repeat(ECHO_CHARS)}…`);
65 });
test/search.test.ts +2 −2
@@ -510,8 +510,8 @@ describe("search: one query, four sources", () => {
510510 "--open-files-in-pager=sh",
511511 "-O sh",
512512 " ",
513 // Longer than this and the transport answers 431 before we do.
514 "a".repeat(4_000),
513 // The longest query there is (MAX_QUERY_CHARS); longer is a 422.
514 "a".repeat(200),
515515 "(flux|.*)+$",
516516 "[",
517517 ];

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs