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

small: Each explorer row says in one short line why it ranks where it does #11445

Merged opened this

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

Merged

mason/forge:mason/11422-small-each-explorer-row-says-in-one-shor → main 2 commits fork

  • Merged. mason/11422-small-each-explorer-row-says-in-one-shor was merged into main as 114667f.

Review

Sign in to review this change.
4 files changed +86 −4
src/actions/explore.ts +10
@@ -402,6 +402,16 @@ export async function explore(
402402 return (await everything(app, viewer, query, now)).slice(0, ROWS);
403403 }
404404
405 /** The rows as the API and the MCP send them: each with its `why` line when the ranking has one to say (omitted otherwise). */
406 export function rowsWithWhy(app: App, query: ExploreQuery, items: readonly ExploreRow[]): (ExploreRow & { why?: string })[] {
407 const weights = frontWeights(app.db);
408 const now = Date.now();
409 return items.map((row) => {
410 const why = whyOf(row, query.sort, now, weights);
411 return why === undefined ? row : { ...row, why: why.text };
412 });
413 }
414
405415 export interface ExplorePage {
406416 readonly items: readonly ExploreRow[];
407417 /** Pass back as `cursor` for the page after; null on the last. */
src/http/api.ts +2 −1
@@ -334,6 +334,7 @@ import {
334334 } from "./respond.ts";
335335 import {
336336 explorePage,
337 rowsWithWhy,
337338 cursorFits,
338339 type ExploreRow,
339340 exploreLine,
@@ -3470,7 +3471,7 @@ export function apiRoutes(router: Router<Handler>): void {
34703471 );
34713472 if (page === null)
34723473 return apiError("invalid_cursor", 400, "cursor is the next_cursor of a page before");
3473 const items = page.items;
3474 const items = rowsWithWhy(ctx.app, query, page.items);
34743475 return timePhase(ctx.request, "render", () => json({ query: exploreLine(query), ...(unknown.length === 0 ? {} : { note: exploreNote(unknown) }), ...(suggest === undefined ? {} : { suggest: suggest.line }), items, next_cursor: page.nextCursor }, 200, headers));
34753476 });
34763477
src/http/mcp/tools/explore.ts +3 −3
@@ -1,10 +1,10 @@
1 import { explorePage, exploreLine, exploreLineFrom, EXPLORE_PARAMS, exploreNote, exploreSuggestion, readExploreLine } from "../../../actions/explore.ts";
1 import { explorePage, exploreLine, exploreLineFrom, EXPLORE_PARAMS, exploreNote, exploreSuggestion, readExploreLine, rowsWithWhy } from "../../../actions/explore.ts";
22 import { forkTreeRows } from "../../../actions/fork_tree_rows.ts";
33 import { bad, good, type Tool, str, repoArg, isAnswer } from "../shared.ts";
44
55 export const tool: Tool = {
66 name: "explore",
7 doc: "`{ kind: tree, app }`: `{ app, upstream, forks, truncated }`, rows `{ repo, follows, standing, updatedAt }` for the app, the chain above it (nearest first) and its direct forks, in the words of the forks page, only what you may read; else `{ q?, cursor? }` (`query` is the same, `q` wins): the apps and people you can see, ranked, with their numbers; `kind:` `sort:` `updated:` `owner:` `archived:` and words in one line; each also as its own argument (`since` = `updated`); page on with `cursor`",
7 doc: "`{ kind: tree, app }`: `{ app, upstream, forks, truncated }`, rows `{ repo, follows, standing, updatedAt }` for the app, the chain above it (nearest first) and its direct forks, in the words of the forks page, only what you may read; else `{ q?, cursor? }` (`query` is the same, `q` wins): the apps and people you can see, ranked, with their numbers and `why` (a short line on the ranking, absent when none); `kind:` `sort:` `updated:` `owner:` `archived:` and words in one line; each also as its own argument (`since` = `updated`); page on with `cursor`",
88 description:
99 "The apps, people (and, for an admin, forges) you can see, ranked.",
1010 inputSchema: {
@@ -44,7 +44,7 @@ export const tool: Tool = {
4444 if (page === null) return bad("invalid_cursor", "cursor is not the next_cursor of a page before");
4545 return good({
4646 query: exploreLine(query),
47 items: page.items,
47 items: rowsWithWhy(ctx.app, query, page.items),
4848 next_cursor: page.nextCursor,
4949 });
5050 },
test/explore-why-surfaces.test.ts added +71
@@ -0,0 +1,71 @@
1 /** The API and MCP explore rows carry the same `why` line the page shows (#11422); absent when the ranking has nothing to say. */
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, type App } from "../src/app.ts";
7 import { createRepo } from "../src/actions/repos.ts";
8 import { createApp as insertApp } from "../src/core/apps.ts";
9 import { createToken } from "../src/core/tokens.ts";
10 import { createUser, getUserByName } from "../src/core/users.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { promote } from "./helpers/members.ts";
13 import { quietLogs } from "./helpers/globals.ts";
14
15 quietLogs();
16
17 let dataDir = "";
18 let app: App;
19 let servers: Servers;
20 let token = "";
21 type Item = { kind: string; name: string; why?: string };
22
23 beforeAll(async () => {
24 dataDir = await mkdtemp(join(tmpdir(), "forge-explore-why-"));
25 app = createApp({ dataDir, port: 0 });
26 servers = await serve(app);
27 const founder = await createUser(app.db, { origin: "seed", trusted: true, username: "founder", email: "[email protected]", password: "a-long-words-passphrase", adminIfFirst: true });
28 if (!founder.ok) throw new Error("founder");
29 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-words-passphrase" });
30 if (!made.ok) throw new Error("user");
31 promote(app.db, made.value);
32 const alice = getUserByName(app.db, "alice")!;
33 for (const name of ["loved", "quiet"]) {
34 const repo = await createRepo(app, alice, { name, visibility: "public" });
35 if (!repo.ok) throw new Error("repo");
36 const rec = insertApp(app.db, { repoId: repo.value.id, slug: `alice-${name}`, access: "public" });
37 if (!rec.ok) throw new Error("app");
38 if (name === "loved")
39 for (let i = 0; i < 2; i++)
40 app.db.run("INSERT INTO feed_like (viewer_id, item_id, repo_id) VALUES (?1, ?2, ?3)", [alice.id, `p-${i}`, repo.value.id]);
41 }
42 token = createToken(app.db, alice.id, "t", "rw").token;
43 }, 30_000);
44
45 afterAll(async () => {
46 servers.stop();
47 app.close();
48 await rm(dataDir, { recursive: true, force: true });
49 });
50
51 const headers = () => ({ authorization: `Bearer ${token}` });
52
53 test("the MCP explore row says why, and a row with nothing to say has no why", async () => {
54 const res = await fetch(`${servers.url}/mcp`, {
55 method: "POST",
56 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", ...headers() },
57 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "explore", arguments: { q: "kind:app sort:likes" } } }),
58 });
59 const items = (JSON.parse(((await res.json()) as any).result.content[0].text) as { items: Item[] }).items;
60 expect(items.find((i) => i.name.endsWith("loved"))?.why).toBe("2 likes");
61 const quiet = items.find((i) => i.name.endsWith("quiet"));
62 expect(quiet).toBeDefined();
63 expect("why" in quiet!).toBe(false);
64 });
65
66 test("the API explore row says the same", async () => {
67 const res = await fetch(`${servers.url}/api/v1/explore?q=${encodeURIComponent("kind:app sort:likes")}`, { headers: headers() });
68 const items = ((await res.json()) as { items: Item[] }).items;
69 expect(items.find((i) => i.name.endsWith("loved"))?.why).toBe("2 likes");
70 expect("why" in items.find((i) => i.name.endsWith("quiet"))!).toBe(false);
71 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs