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

Search understands repo:owner/name in its words (#16463) #16474

Merged opened this

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

Merged

voice/search-repo → main 0 commits 1 thread · 1 unresolved

  • Merged. voice/search-repo was merged into main as bb6e3b3.

Review

Sign in to review this change.
4 files changed +37 −5
src/core/search.ts +14
@@ -79,6 +79,20 @@ export function searchKind(raw: string | null): SearchKind {
7979 return raw === "repos" || raw === "issues" || raw === "code" ? raw : "all";
8080 }
8181
82 /**
83 * A `repo:owner/name` (or `repo:name`) written into a query, as a person or a voice client
84 * writes it: taken out of the words and named apart, so the search keeps to that repository
85 * instead of looking for the text "repo:". Undefined `repo` when the query names none.
86 */
87 export function repoQualifier(q: string): { readonly q: string; readonly repo?: string } {
outdated

The new function sits between searchNote's doc comment (above, ending at line 85) and searchNote itself, so that comment now documents repoQualifier. Move repoQualifier and its comment above searchNote's comment.

88 let repo: string | undefined;
89 const rest = q.replace(/(^|\s)repo:(\S+)/gi, (_match, lead: string, named: string) => {
90 repo ??= named;
91 return lead;
92 });
93 return repo === undefined ? { q } : { q: rest.replace(/\s+/g, " ").trim(), repo };
94 }
95
8296 /**
8397 * What a caller who asked wrongly is told, in one sentence, or undefined when
8498 * the request was fine (an unknown kind or limit is `searchRefusal`'s 422, not a note). Plain text:
src/http/mcp/reads.ts +10 −4
@@ -9,7 +9,7 @@ import { getIssue, type Issue } from "../../core/issues.ts";
99 import { markReaders } from "../../crew/direct.ts";
1010 import { SEARCH_ROWS as CARD_HITS } from "../../web/studio/cards.ts";
1111 import { search, searchRefusal } from "../../actions/search.ts";
12 import { CODE_BUDGET_MS, searchKind, searchNote, type SearchResults } from "../../core/search.ts";
12 import { CODE_BUDGET_MS, repoQualifier, searchKind, searchNote, type SearchResults } from "../../core/search.ts";
1313 import { explorePage, exploreLine, exploreCount, exploreFiltered, exploreLineFrom, exploreNote, exploreSuggestion, readExploreLine, rowsWithWhy } from "../../actions/explore.ts";
1414 import { forkTreeRows } from "../../actions/fork_tree_rows.ts";
1515 import type { User } from "../../core/users.ts";
@@ -45,9 +45,15 @@ export async function exploreRun(ctx: Ctx, user: User | undefined, args: Args):
4545 });
4646 }
4747
48 export async function searchRun(ctx: Ctx, user: User | undefined, args: Args): Promise<Answer> {
49 const q = args["q"];
50 if (typeof q !== "string" || queryTooLong(q)) return bad("invalid", `\`q\` is a string of at most ${MAX_QUERY_CHARS} characters`);
48 export async function searchRun(ctx: Ctx, user: User | undefined, given: Args): Promise<Answer> {
49 const raw = given["q"];
50 if (typeof raw !== "string" || queryTooLong(raw)) return bad("invalid", `\`q\` is a string of at most ${MAX_QUERY_CHARS} characters`);
51 // `repo:owner/name` in the words is the `repo` filter, as a voice client writes it.
52 const qualified = repoQualifier(raw);
53 const q = qualified.q;
54 if (qualified.repo !== undefined && given["repo"] !== undefined && given["repo"] !== qualified.repo)
55 return bad("invalid", "`q` names one repository with repo: and `repo` another: give one");
56 const args: Args = qualified.repo === undefined ? given : { ...given, repo: qualified.repo };
5157 const kind = args["kind"];
5258 if (kind !== undefined && typeof kind !== "string") return bad("invalid_kind", "kind is all, repos, issues or code");
5359 const refusal = searchRefusal(kind, args["limit"] === undefined || typeof args["limit"] === "number" ? (args["limit"] as number | undefined) : NaN);
src/http/mcp/tools/search.ts +1 −1
@@ -4,7 +4,7 @@ import { MAX_QUERY_CHARS } from "../../../core/query_limit.ts";
44
55 export const tool: Tool = {
66 name: "search",
7 doc: "`{ q, kind?, limit?, repo?, all? }`: guides (`docs`, kind all), issues, pulls, repositories and code you can read, the same results as /search, drawn as the `ui://forge/search` card; `repo` keeps to one; `moreCode` says the code grep stopped at its budget, `all` greps the rest",
7 doc: "`{ q, kind?, limit?, repo?, all? }`: guides (`docs`, kind all), issues, pulls, repositories and code you can read, the same results as /search, drawn as the `ui://forge/search` card; `repo` (or `repo:owner/name` in `q`) keeps to one; `moreCode` says the code grep stopped at its budget, `all` greps the rest",
88 description:
99 "Find issues, pulls, repositories and code you can read; `kind`, `repo` narrow.",
1010 inputSchema: {
test/mcp-search.test.ts +12
@@ -110,6 +110,18 @@ describe("MCP search", () => {
110110 expect(scoped.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]);
111111 });
112112
113 test("repo: written into q is the same scope (#16463), out of the words; a second, different repo is refused", async () => {
114 const said = await tool(aliceRo, { q: "glow repo:alice/pebble", kind: "issues" });
115 expect(said.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]);
116 // First or last in the words, any case of the qualifier.
117 const first = await tool(aliceRo, { q: "REPO:alice/pebble glow", kind: "issues" });
118 expect(first.value.issues.map((i: any) => i.repo.name)).toEqual(["pebble"]);
119 expect((await tool(aliceRo, { q: "glow repo:alice/pebble", repo: "alice/pebble", kind: "issues" })).value.issues.length).toBe(1);
120 expect((await tool(aliceRo, { q: "glow repo:alice/pebble", repo: "alice/noisy" })).error).toContain("give one");
121 // A private one the caller cannot read is not found, as through `repo`.
122 expect((await tool(bobRo, { q: "glow repo:alice/secretpebble" })).error).toContain("no such app or repository");
123 });
124
113125 test("an unknown kind or a bad limit is refused in the API's words; every kind answers", async () => {
114126 expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('argument "kind" is "bogus"');
115127 expect((await tool(aliceRo, { q: "a", kind: "bogus" })).error).toContain('one of "all", "issues", "repos", "code"');

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs