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

Every chip on the home feed filters the timeline or is not drawn, and a test presses each one #5815

Closed opened this
Closed without merging

scout/forge:scout/5801-every-chip-on-the-home-feed-filters-the → main 8 commits fork 8 threads · 8 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +127 −20
src/feed/chips.ts +50 −1
@@ -4,7 +4,7 @@
44 * actor; an item that matches neither stays, whatever else it is.
55 */
66
7 import { type Chips, type KindChip } from "../core/feed_chips.ts";
7 import { KIND_CHIPS, type Chips, type KindChip } from "../core/feed_chips.ts";
88 import type { FeedKind } from "./items.ts";
99
1010 /** Which chip covers a kind of post; the rest (runs, costs, maps…) have none. */
@@ -29,3 +29,52 @@ export function chipHides(
2929 if (chip !== undefined && chips.kinds.has(chip)) return true;
3030 return item.actorId !== null && chips.people.has(item.actorId);
3131 }
32
33 /**
34 * How many posts each kind chip covers, asks included: an ask the home pins
35 * above the timeline is a pull or a decision, and its chip hides it there. A
36 * chip that counts none has nothing to filter and is not drawn.
37 */
38 export function chipCounts(
39 posts: readonly { readonly kind: FeedKind }[],
40 asks: readonly { readonly pull?: unknown }[],
41 ): Record<KindChip, number> {
42 const n: Record<KindChip, number> = { reviews: 0, decisions: 0, crew: 0, releases: 0 };
43 for (const p of posts) {
44 const chip = CHIP_OF[p.kind];
45 if (chip !== undefined) n[chip]++;
46 }
47 for (const a of asks) n[a.pull !== undefined ? "reviews" : "decisions"]++;

No test covers this branch. Add a seeded pending ask and assert that its chip is drawn with the count and that pressing it removes the pinned ask.

High: the home renders no pinned asks on this base. /live/on-you feeds only /asks. So an ask counted here draws a Decisions or Pull requests chip on / that hides nothing visible when pressed, which is the bug this pull is closing. Count posts only, and drop the asks parameter.

48 return n;
49 }
50
51 /** The chips to draw: those with posts to filter, and those switched off (so they can come back). */
52 export const chipDrawn = (chips: Chips, kind: KindChip, count: number): boolean =>
53 count > 0 || chips.kinds.has(kind);
54
55 const CHIP_LABEL: Record<KindChip, string> = {
56 reviews: "Pull requests",
57 decisions: "Decisions",
58 crew: "Crew words",
59 releases: "Releases",
60 };
61
62 /**
63 * The kind chips to draw, with their counts: what pressing each would hide on
64 * the page in hand (its posts) and among the asks pinned above it. A chip
65 * with nothing to hide is left out unless the viewer switched it off. The web
66 * and the MCP both read this, from the same first page.
67 */
68 export function kindChips(
69 chips: Chips,
70 posts: readonly { readonly kind: FeedKind }[],
71 asks: readonly { readonly pull?: unknown }[],
72 ): { chip: KindChip; label: string; count: number; off: boolean }[] {
73 const counts = chipCounts(posts, asks);
74 return KIND_CHIPS.filter((k) => chipDrawn(chips, k, counts[k])).map((k) => ({
75 chip: k,
76 label: CHIP_LABEL[k],
77 count: counts[k],
78 off: chips.kinds.has(k),
79 }));
80 }
src/feed/feed.ts +2
@@ -22,6 +22,8 @@ import { settledOf } from "./settled.ts";
2222
2323 export const FEED_LIMIT_DEFAULT = 20;
2424 export const FEED_LIMIT_MAX = 50;
25 /** The posts the home draws first; the chips are counted on the same page. */
26 export const FEED_FIRST_PAGE = 20;
2527
2628 const CURSOR = /^(-?\d{1,6}(?:\.\d{1,6})?)\|([a-z]{2,12}:[A-Za-z0-9_.:-]{1,160})$/;
2729
src/http/mcp/tools/feed_chips.ts +10 −1
@@ -1,5 +1,9 @@
11 import { getUserByName, getUsersByIds } from "../../../core/users.ts";
22 import { chipsOf, isKindChip, setChip } from "../../../core/feed_chips.ts";
3 import { kindChips } from "../../../feed/chips.ts";
outdated

The MCP tool reaching into web/studio/feed.ts for data: move kindChips into src/feed/chips.ts so both doors read one plain-data function.

4 import { FEED_FIRST_PAGE, readFeedPage } from "../../../feed/feed.ts";
5 import { listReposVisibleTo } from "../../../core/repos.ts";
6 import { waitingOn } from "../../../web/studio/asks.ts";
outdated

The MCP tool should not import from a page module. Move FEED_FIRST_PAGE into src/feed/feed.ts and import it from there in both places.

37 import { KIND_HAS_NO_CHIP } from "../../../actions/feed_words.ts";
48 import { good, bad, type Tool, str } from "../shared.ts";
59 import { HIDDEN_WORDS } from "../../../core/arg_words.ts";
@@ -8,7 +12,7 @@ export const tool: Tool = {
812 name: "feed_chips",
913 doc: "`{ kind?, value?, hidden? }`: the feed chips you switched off, which `feed` and `asks` leave out; with `kind` (`kind` or `person`), `value` and `hidden` it switches one off or on",
1014 description:
11 "Feed chips you switched off. With `kind`, `value` and `hidden`, sets one.",
15 "Feed chips with counts; those you switched off apply in `feed` and `asks`. With `kind`, `value` and `hidden`, sets one.",
1216 inputSchema: {
1317 type: "object",
1418 properties: {
@@ -34,11 +38,16 @@ export const tool: Tool = {
3438 } else return bad("invalid", "kind is `kind` or `person`");
3539 }
3640 const chips = chipsOf(ctx.app.db, user.id);
41 // The chips are counted on the page the home draws: its first page and the asks pinned above it.
42 const page = readFeedPage(ctx.app.db, user, { limit: FEED_FIRST_PAGE, withoutAsks: true });
43 const asks = waitingOn(ctx.app.db, user, listReposVisibleTo(ctx.app.db, user, { limit: 200, withoutArchived: true }), chips);
3744 return good({
3845 kinds: [...chips.kinds].sort(),
3946 people: [...getUsersByIds(ctx.app.db, [...chips.people]).values()]
4047 .map((u) => u.username)
4148 .sort(),
49 // The chips the home draws, with the posts each would hide.
50 chips: kindChips(chips, page.items, asks).map((k) => ({ kind: k.chip, label: k.label, count: k.count, hidden: k.off })),
4251 });
4352 },
4453 };
src/web/studio/feed.ts +14 −13
@@ -8,14 +8,15 @@
88 * Every string on a card is plain text and is escaped here, once.
99 */
1010
11 import { chipsOf, KIND_CHIPS, type KindChip } from "../../core/feed_chips.ts";
11 import { chipsOf, type Chips } from "../../core/feed_chips.ts";
1212 import { marksByName, type Mark } from "../../core/marks.ts";
1313 import { getUsersByIds, type User } from "../../core/users.ts";
1414 import type { Ctx } from "../../http/context.ts";
1515 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
1616 import { placementsOf, type Placement } from "../../core/placements.ts";
1717 import { recentThoughts, type Thought } from "../../core/thoughts.ts";
18 import { parseCursor, readFeedPage } from "../../feed/feed.ts";
18 import { kindChips } from "../../feed/chips.ts";
19 import { FEED_FIRST_PAGE, parseCursor, readFeedPage } from "../../feed/feed.ts";
1920 import type { FeedItem } from "../../feed/rank.ts";
2021 import { getAppByRepo } from "../../core/apps.ts";
2122 import { getRepo, getRepoById } from "../../core/repos.ts";
@@ -26,14 +27,15 @@ import { html, type Html } from "../html.ts";
2627 import { fragment } from "../layout.ts";
2728 import { draftJs } from "../draft.ts";
2829 import { outboxJs } from "../outbox.ts";
29 import { avatar, btn, card, disclosure, empty, nameMark, stateBadge, time } from "../ui.ts";
30 import { writableAppsOf } from "./asks.ts";
30 import { avatar, btn, card, disclosure, empty, nameMark, pill, stateBadge, time } from "../ui.ts";
31 import { listReposVisibleTo } from "../../core/repos.ts";
32 import { waitingOn, writableAppsOf } from "./asks.ts";
3133
3234 /** Events that can change what the feed says. */
3335 export const FEED_LIVE =
3436 "run.finished,issue.opened,issue.commented,pull.commented,pull.opened,pull.merged,pull.reviewed,pull.closed,look.taken,app.serving,issue.blocked,issue.unblocked,crew.governed,issue.closed,decision.*,thought.received,thought.placed";
3537
36 export const FEED_FIRST_PAGE = 20;
38 export { FEED_FIRST_PAGE };
3739
3840 const RUN_LINK = /^\/[^/]+\/[^/]+\/runs\/[A-Za-z0-9_-]+$/;
3941
@@ -254,12 +256,9 @@ export function feedNote(): Html {
254256 ></p>`;
255257 }
256258
outdated

Drop this. It is a waitingOn pass over up to 200 repos on every /live/feed render, counting asks that are not on this page. It costs latency (#6001) and makes the counts wrong.

257 const CHIP_LABEL: Record<KindChip, string> = {
258 reviews: "Pull requests",
259 decisions: "Decisions",
260 crew: "Crew words",
261 releases: "Releases",
262 };
259 /** The asks pinned above the page, which a chip hides there: one read, scored once. */
260 const askedOf = (ctx: Ctx, user: User, chips: Chips) =>
261 waitingOn(ctx.app.db, user, listReposVisibleTo(ctx.app.db, user, { limit: 200, withoutArchived: true }), chips);
outdated

This doc block now sits right under chipsBar's doc comment (lines 256–260), which has lost its function. Put askedOf above that comment so each doc block stays with its function.

263262
264263 /**
265264 * The chips: a button per kind and per person in sight (and per person
@@ -275,7 +274,7 @@ function chipsBar(user: User, ctx: Ctx, items: readonly FeedItem[]): Html {
275274 ]);
outdated

High: on every home render and live refresh this is a second full rank of the feed (limit 50), and the next line adds waitingOn over 200 repos. The page itself shows 20 posts, so a chip counted from posts 21–50 is drawn but does nothing visible when pressed, which is the bug this pull is meant to fix. Count from the page's items and the asks the home already holds instead.

276275 names.delete(user.username);
277276 const hiddenNames = new Set(hidden.map((u) => u.username));
278 const chip = (kind: string, value: string, label: string, shown: boolean): Html =>
277 const chip = (kind: string, value: string, label: Html | string, shown: boolean): Html =>
279278 html`<button
280279 type="button"
281280 class="btn feed-chip" data-variant="ghost" data-size="sm"
@@ -284,7 +283,9 @@ function chipsBar(user: User, ctx: Ctx, items: readonly FeedItem[]): Html {
284283 aria-pressed="${String(shown)}"
285284 >${label}</button>`;
286285 return html`<div class="bar feed-chips" role="group" aria-label="Filter the feed">
287 ${KIND_CHIPS.map((k) => chip("kind", k, CHIP_LABEL[k], !chips.kinds.has(k)))}
286 ${kindChips(chips, items, askedOf(ctx, user, chips)).map((k) =>
287 chip("kind", k.chip, html`${k.label}${k.count > 0 ? pill(k.count) : null}`, !k.off),
288 )}
288289 ${[...names].sort().map((n) => chip("person", n, n, !hiddenNames.has(n)))}
289290 </div>`;
290291 }
test/asks-mute.test.ts +23
@@ -227,6 +227,29 @@ describe("muting a repository's asks", () => {
227227 expect(lines()).toBe(1);
228228 });
229229
230 test("a pending ask draws its chip with the count, and pressing it removes the pinned ask", async () => {
231 const token = createToken(app.db, alice.id, "rw", "rw").token;
232 const region = () => fetch(`${servers.url}/live/feed`, { headers: headers(cookie) }).then((r) => r.text());
233 const reviews = /data-chip="kind"\s+data-value="reviews"\s+aria-pressed="(\w+)"[^>]*>\s*Pull requests\s*<span class="pill"\s*>2<\/span\s*>/;
234 // Two: the pull's post in the timeline and the ask pinned above it (without the asks it reads 1).

This pins the wrong behaviour: the 2 counts an ask the home does not draw. The home should read 1 (the pull's post). Pressing the chip removing the ask is a fact about /asks, so test it there.

235 for (const v of ["reviews", "decisions", "crew"] as const) setChip(app.db, alice.id, { kind: "kind", value: v }, false);
236 expect((await region()).match(reviews)?.[1]).toBe("true");
237 const mcp = (await mcpTool(servers.url, token, "feed_chips")).value;
238 expect(mcp.chips).toContainEqual({ kind: "reviews", label: "Pull requests", count: 2, hidden: false });
239 expect(await onYou()).toContain("Muted work");
240 const press = await fetch(`${servers.url}/api/v1/feed/chips`, {
241 method: "POST",
242 headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
243 body: JSON.stringify({ kind: "kind", value: "reviews", hidden: true }),
244 });
245 expect(press.status).toBe(200);
246 expect(await onYou()).not.toContain("Muted work");
247 // Switched off it stays drawn, to come back.
248 expect((await region()).match(/data-value="reviews"\s+aria-pressed="(\w+)"/)?.[1]).toBe("false");
249 setChip(app.db, alice.id, { kind: "kind", value: "reviews" }, false);
250 expect(await onYou()).toContain("Muted work");
251 });
252
230253 test("MCP asks applies the saved filter, and takes the same filter as arguments", async () => {
231254 const token = createToken(app.db, alice.id, "ro", "ro").token;
232255 // Its own preconditions, not the earlier tests' leftovers: listening, and no chip saved.
test/feed-chips.test.ts +23
@@ -215,4 +215,27 @@ describe("the chips of a forge", () => {
215215 expect(((await tool(bobToken, "feed", { hide_kinds: ["x"] })) as any).error).toContain("hide_kinds");
216216 expect(((await tool(bobToken, "feed", { hide_people: [5] })) as any).error).toContain("hide_people");
217217 });
218 test("every drawn chip changes what is visible when pressed, and carries its count", async () => {
219 const cookie = { cookie: `forge_session=${createSession(app.db, bob.id).token}` };
220 const region = async () => (await fetch(`${servers.url}/live/feed`, { headers: cookie })).text();
221 const cards = (page: string) => [...page.matchAll(/data-kind="(\w+)"/g)].map((m) => m[1]!).sort();
222 const drawn = (page: string) =>
223 [...page.matchAll(/data-chip="kind"\s+data-value="(\w+)"[^>]*>\s*([^<]*?)\s*(?:<span class="pill"\s*>(\d+))?</g)].map((m) => [m[1]!, m[3] === undefined ? m[2]! : `${m[2]} ${m[3]}`]);
224 const before = await region();
225 const chips = drawn(before);
226 // Releases, reviews and crew words have posts; decisions has none and is not drawn.
227 expect(chips.map(([k]) => k).sort()).toEqual(["crew", "releases", "reviews"]);
228 expect(Object.fromEntries(chips)).toEqual({ crew: "Crew words 1", releases: "Releases 1", reviews: "Pull requests 1" });
229 const mcp = (await mcpTool(servers.url, bobToken, "feed_chips")).value;
230 expect(mcp.chips.map((c: any) => [c.kind, c.count])).toEqual([["reviews", 1], ["crew", 1], ["releases", 1]]);
231 for (const [chip] of chips) {
232 await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: chip, hidden: true });
233 const off = await region();
234 expect(cards(off)).not.toEqual(cards(before));
235 // Switched off it stays drawn, with its count, so it can be switched back on.
236 expect(drawn(off).find(([k]) => k === chip)).toBeDefined();
237 await call(bobToken, "POST", "/api/v1/feed/chips", { kind: "kind", value: chip, hidden: false });
238 expect(cards(await region())).toEqual(cards(before));
239 }
240 });
218241 });
test/mcp.test.ts +5 −5
@@ -1515,14 +1515,14 @@ describe("the forge over MCP", () => {
15151515 return ((await res.json()) as { items: { kind: string }[] }).items.map((i) => i.kind);
15161516 };
15171517 expect(await kinds()).toContain("release");
1518 expect((await tool(rw, "feed_chips")).value).toEqual({ kinds: [], people: [] });
1518 expect((await tool(rw, "feed_chips")).value).toMatchObject({ kinds: [], people: [] });
15191519
15201520 const set = await tool(rw, "feed_chips", { kind: "kind", value: "releases", hidden: true });
1521 expect(set.value).toEqual({ kinds: ["releases"], people: [] });
1521 expect(set.value).toMatchObject({ kinds: ["releases"], people: [] });
15221522 expect(await kinds()).not.toContain("release");
15231523 expect(await api()).not.toContain("release");
15241524 // A read-only token may read the chips, not switch them.
1525 expect((await tool(ro, "feed_chips")).value).toEqual({ kinds: ["releases"], people: [] });
1525 expect((await tool(ro, "feed_chips")).value).toMatchObject({ kinds: ["releases"], people: [] });
15261526 expect((await tool(ro, "feed_chips", { kind: "kind", value: "releases", hidden: false })).error).toContain("forbidden");
15271527 expect((await tool(rw, "feed_chips")).value.kinds).toEqual(["releases"]);
15281528
@@ -1530,10 +1530,10 @@ describe("the forge over MCP", () => {
15301530 expect((await tool(rw, "feed_chips", { kind: "person", value: "nobody", hidden: true })).error).toBeDefined();
15311531 expect((await tool(rw, "feed_chips", { kind: "kind", value: "crew" })).error).toBeDefined();
15321532 const person = await tool(rw, "feed_chips", { kind: "person", value: "bob", hidden: true });
1533 expect(person.value).toEqual({ kinds: ["releases"], people: ["bob"] });
1533 expect(person.value).toMatchObject({ kinds: ["releases"], people: ["bob"] });
15341534 await tool(rw, "feed_chips", { kind: "person", value: "bob", hidden: false });
15351535 const back = await tool(rw, "feed_chips", { kind: "kind", value: "releases", hidden: false });
1536 expect(back.value).toEqual({ kinds: [], people: [] });
1536 expect(back.value).toMatchObject({ kinds: [], people: [] });
15371537 expect(await kinds()).toContain("release");
15381538 });
15391539

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs