Public: anyone can read this; the crew can change it.
Home opens with a "Since you were here" digest: at most three lines, one per repository, biggest change first, each linking to its evidence #6542
scout/forge:scout/5797-home-opens-with-a-since-you-were-here-di → main 4 commits fork
5 files
changed +226
−4
src/feed/digest.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * "Since you were here": the last day of the feed's candidates folded to | |
| 3 | * one line per repository — landed, released, failed, decided — the biggest | |
| 4 | * change first, at most three lines, each pointing at its heaviest post. | |
| 5 | * Plain candidates in, plain lines out. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Candidate } from "./items.ts"; | |
| 9 | ||
| 10 | export const DIGEST_WINDOW_MS = 24 * 3_600_000; | |
| 11 | export const DIGEST_LINES = 3; | |
| 12 | ||
| 13 | export interface DigestLine { | |
| 14 | readonly app: string; | |
| 15 | /** `2 landed, 1 released`. */ | |
| 16 | readonly text: string; | |
| 17 | /** The heaviest post: the pull, the release or the run. */ | |
| 18 | readonly link: string; | |
| 19 | readonly weight: number; | |
| 20 | } | |
| 21 | ||
| 22 | type Move = "released" | "landed" | "failed" | "decided"; | |
| 23 | const ORDER: readonly Move[] = ["released", "landed", "failed", "decided"]; | |
| 24 | const WEIGHT: Record<Move, number> = { released: 4, landed: 3, failed: 3, decided: 1 }; | |
| 25 | ||
| 26 | function moveOf(c: Candidate): Move | undefined { | |
| 27 | switch (c.kind) { | |
| 28 | case "release": | |
| 29 | return "released"; | |
| 30 | case "pull": | |
| 31 | return c.what.startsWith("merged") ? "landed" : undefined; | |
| 32 | case "run": | |
| 33 | return c.urgency >= 4 && c.id.startsWith("event:") ? "failed" : undefined; | |
| 34 | case "decision": | |
| 35 | return c.id.startsWith("decision:") ? "decided" : undefined; | |
| 36 | default: | |
| 37 | return undefined; | |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | export function digestOf(candidates: readonly Candidate[], now: number): DigestLine[] { | |
| 42 | const since = now - DIGEST_WINDOW_MS; | |
| 43 | const groups = new Map< | |
| 44 | string, | |
| 45 | { counts: Map<Move, number>; weight: number; best: number; link: string; at: number } | |
| 46 | >(); | |
| 47 | for (const c of candidates) { | |
| 48 | const move = moveOf(c); | |
| 49 | if (move === undefined) continue; | |
| 50 | const at = Date.parse(c.at); | |
| 51 | if (!(at >= since && at <= now)) continue; | |
| 52 | let g = groups.get(c.app); | |
| 53 | if (g === undefined) { | |
| 54 | g = { counts: new Map(), weight: 0, best: -1, link: c.link, at: 0 }; | |
| 55 | groups.set(c.app, g); | |
| 56 | } | |
| 57 | g.counts.set(move, (g.counts.get(move) ?? 0) + 1); | |
| 58 | g.weight += WEIGHT[move]; | |
| 59 | if (WEIGHT[move] > g.best || (WEIGHT[move] === g.best && at > g.at)) { | |
| 60 | g.best = WEIGHT[move]; | |
| 61 | g.link = c.link; | |
| 62 | g.at = at; | |
| 63 | } | |
| 64 | } | |
| 65 | return [...groups] | |
| 66 | .sort(([an, a], [bn, b]) => b.weight - a.weight || b.at - a.at || an.localeCompare(bn)) | |
| 67 | .slice(0, DIGEST_LINES) | |
| 68 | .map(([app, g]) => ({ | |
| 69 | app, | |
| 70 | text: `${app}: ${ORDER.filter((m) => g.counts.has(m)) | |
| 71 | .map((m) => `${g.counts.get(m)} ${m}`) | |
| 72 | .join(", ")}`, | |
| 73 | link: g.link, | |
| 74 | weight: g.weight, | |
| 75 | })); | |
| 76 | } |
src/feed/feed.ts +12
| @@ -244,3 +244,15 @@ function sharedCandidates( | ||
| 244 | 244 | } |
| 245 | 245 | return out; |
| 246 | 246 | } |
| 247 | ||
| 248 | /** | |
| 249 | * What the home's digest reads: the candidates the timeline reads (the same | |
| 250 | * cache entry, so no second scan), less what the viewer muted or filtered. | |
| 251 | */ | |
| 252 | export function digestCandidates(db: Database, viewer: User, now: number): readonly Candidate[] { | |
| 253 | const chips = chipsOf(db, viewer.id); | |
| 254 | const muted = mutedRepos(db, viewer.id); | |
| 255 | return viewerCandidates(db, viewer, now, chips, false).filter( | |
| 256 | (c) => !muted.has(c.repoId) && !chipHides(chips, c), | |
| 257 | ); | |
| 258 | } |
src/web/studio/feed.ts +16 −2
| @@ -15,7 +15,8 @@ import type { Ctx } from "../../http/context.ts"; | ||
| 15 | 15 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 16 | 16 | import { placementsOf, type Placement } from "../../core/placements.ts"; |
| 17 | 17 | import { recentThoughts, type Thought } from "../../core/thoughts.ts"; |
| 18 | import { parseCursor, readFeedPage } from "../../feed/feed.ts"; | |
| 18 | import { digestOf } from "../../feed/digest.ts"; | |
| 19 | import { digestCandidates, parseCursor, readFeedPage } from "../../feed/feed.ts"; | |
| 19 | 20 | import type { FeedItem } from "../../feed/rank.ts"; |
| 20 | 21 | import { getAppByRepo } from "../../core/apps.ts"; |
| 21 | 22 | import { getRepo, getRepoById } from "../../core/repos.ts"; |
| @@ -24,7 +25,7 @@ import { mayFetchLook, pictured } from "../pages/looks.ts"; | ||
| 24 | 25 | import { html, type Html } from "../html.ts"; |
| 25 | 26 | import { fragment } from "../layout.ts"; |
| 26 | 27 | import { outboxJs } from "../outbox.ts"; |
| 27 | import { avatar, btn, card, disclosure, empty, nameMark, stateBadge, time, timeAgo } from "../ui.ts"; | |
| 28 | import { avatar, btn, card, disclosure, empty, listCard, nameMark, row, stateBadge, time, timeAgo } from "../ui.ts"; | |
| 28 | 29 | import { writableAppsOf } from "./asks.ts"; |
| 29 | 30 | |
| 30 | 31 | /** Events that can change what the feed says. */ |
| @@ -539,3 +540,16 @@ ${outboxJs} | ||
| 539 | 540 | flush(); |
| 540 | 541 | })(); |
| 541 | 542 | `; |
| 543 | ||
| 544 | /** | |
| 545 | * "Since you were here": the last day in at most three lines, one per | |
| 546 | * repository, each linking to its heaviest post. Nothing in the window, nothing drawn. | |
| 547 | */ | |
| 548 | export function digestSection(user: User, ctx: Ctx): Html | null { | |
| 549 | const lines = digestOf(digestCandidates(ctx.app.db, user, Date.now()), Date.now()); | |
| 550 | if (lines.length === 0) return null; | |
| 551 | return html`<section id="digest" aria-labelledby="digest-h"> | |
| 552 | <h2 class="small muted" id="digest-h">Since you were here · last 24 h</h2> | |
| 553 | ${listCard(lines.map((l) => row({ href: l.link, title: l.text, nav: true })))} | |
| 554 | </section>`; | |
| 555 | } |
src/web/studio/home.ts +3 −2
| @@ -51,7 +51,7 @@ import { | ||
| 51 | 51 | import { writableAppsOf } from "./asks.ts"; |
| 52 | 52 | import { needsYouSection } from "./asks_page.ts"; |
| 53 | 53 | import { flowCards } from "./flow_card.ts"; |
| 54 | import { composer, feedJs, feedNote, feedRegion } from "./feed.ts"; | |
| 54 | import { composer, digestSection, feedJs, feedNote, feedRegion } from "./feed.ts"; | |
| 55 | 55 | import { alarmLines, cachedAlarms } from "./alarm_lines.ts"; |
| 56 | 56 | import { kpiRow, vitalsRow, waitingLine } from "./kpi_row.ts"; |
| 57 | 57 | import { feedCandidateCache } from "../../feed/feed.ts"; |
| @@ -295,6 +295,7 @@ export async function studioHome( | ||
| 295 | 295 | <a class="small muted" href="/asked">What you asked for</a> |
| 296 | 296 | </div> |
| 297 | 297 | ${flowCards(ctx, user)} |
| 298 | ${timePhase(ctx.request, "digest", () => digestSection(user, ctx), () => feedCandidateCache(ctx.app.db))} | |
| 298 | 299 | ${feedNote()} |
| 299 | 300 | ${timePhase(ctx.request, "feed", () => feedRegion(user, ctx), () => feedCandidateCache(ctx.app.db))} |
| 300 | 301 | </div> |
| @@ -316,7 +317,7 @@ export async function studioHome( | ||
| 316 | 317 | body, |
| 317 | 318 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${feedJs}${ask ? `\n${passkeyJs}` : ""}`, |
| 318 | 319 | }); |
| 319 | notePhaseRest(ctx.request, "render", rendering, ["kpis", "vitals", "waiting", "panels", "feed"]); | |
| 320 | notePhaseRest(ctx.request, "render", rendering, ["kpis", "vitals", "waiting", "panels", "digest", "feed"]); | |
| 320 | 321 | return res; |
| 321 | 322 | } |
| 322 | 323 |
test/home-digest.test.ts added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | /** | |
| 2 | * Home's "Since you were here" digest: the last day of the feed's events, | |
| 3 | * one line per repository, biggest change first, three at most, each line | |
| 4 | * linking to its heaviest post. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createUser, type User } from "../src/core/users.ts"; | |
| 14 | import { digestOf } from "../src/feed/digest.ts"; | |
| 15 | import type { Candidate } from "../src/feed/items.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | import { promote } from "./helpers/members.ts"; | |
| 18 | ||
| 19 | describe("digestOf", () => { | |
| 20 | const now = Date.parse("2026-10-25T12:00:00Z"); | |
| 21 | const cand = (n: number, app: string, kind: Candidate["kind"], what: string, link: string, ageH = 1, urgency = 0): Candidate => ({ | |
| 22 | id: `event:${n}`, kind, repoId: app, app, actorId: null, actor: null, | |
| 23 | at: new Date(now - ageH * 3_600_000).toISOString(), link, what, urgency, | |
| 24 | }); | |
| 25 | ||
| 26 | test("groups by repository, biggest first, capped at three, linking the heaviest post", () => { | |
| 27 | const lines = digestOf([ | |
| 28 | cand(1, "a/one", "pull", "merged a pull request", "/a/one/pulls/1"), | |
| 29 | cand(2, "a/one", "pull", "merged a pull request", "/a/one/pulls/2", 2), | |
| 30 | cand(3, "a/one", "release", "a new version is serving", "/a/one"), | |
| 31 | cand(4, "b/two", "pull", "merged a pull request", "/b/two/pulls/9"), | |
| 32 | cand(5, "c/three", "run", "a deploy run failed", "/c/three/runs/r1", 1, 4), | |
| 33 | cand(6, "d/four", "pull", "merged a pull request", "/d/four/pulls/3", 3), | |
| 34 | cand(7, "d/four", "pull", "opened a pull request", "/d/four/pulls/4"), | |
| 35 | cand(8, "e/old", "release", "a new version is serving", "/e/old", 30), | |
| 36 | ], now); | |
| 37 | expect(lines.map((l) => l.text)).toEqual([ | |
| 38 | "a/one: 1 released, 2 landed", | |
| 39 | "b/two: 1 landed", | |
| 40 | "c/three: 1 failed", | |
| 41 | ]); | |
| 42 | expect(lines.map((l) => l.link)).toEqual(["/a/one", "/b/two/pulls/9", "/c/three/runs/r1"]); | |
| 43 | }); | |
| 44 | ||
| 45 | test("nothing in the window, nothing", () => { | |
| 46 | expect(digestOf([cand(1, "a/one", "release", "x", "/a/one", 25)], now)).toEqual([]); | |
| 47 | expect(digestOf([cand(1, "a/one", "tell", "opened an issue", "/a/one/issues/1")], now)).toEqual([]); | |
| 48 | }); | |
| 49 | ||
| 50 | test("200 repositories fold in under 10 ms warm", () => { | |
| 51 | const all = Array.from({ length: 200 }, (_, i) => | |
| 52 | cand(i, `u/r${i}`, "pull", "merged a pull request", `/u/r${i}/pulls/1`)); | |
| 53 | digestOf(all, now); | |
| 54 | const t = performance.now(); | |
| 55 | digestOf(all, now); | |
| 56 | expect(performance.now() - t).toBeLessThan(10); | |
| 57 | }); | |
| 58 | }); | |
| 59 | ||
| 60 | describe("home shows the digest", () => { | |
| 61 | let dataDir: string; | |
| 62 | let app: App; | |
| 63 | let servers: Servers; | |
| 64 | let cookie: string; | |
| 65 | let alice: User; | |
| 66 | ||
| 67 | beforeAll(async () => { | |
| 68 | dataDir = await mkdtemp(join(tmpdir(), "forge-home-digest-")); | |
| 69 | app = createApp({ dataDir, port: 0 }); | |
| 70 | servers = await serve(app); | |
| 71 | const r = await createUser(app.db, { | |
| 72 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 73 | password: "a-good-passphrase", adminIfFirst: true, | |
| 74 | }); | |
| 75 | if (!r.ok) throw new Error(r.error); | |
| 76 | alice = r.value; | |
| 77 | promote(app.db, alice); | |
| 78 | cookie = `forge_session=${createSession(app.db, alice.id).token}; forge_mode=studio`; | |
| 79 | }); | |
| 80 | afterAll(async () => { | |
| 81 | servers.stop(); | |
| 82 | app.close(); | |
| 83 | await rm(dataDir, { recursive: true, force: true }); | |
| 84 | }); | |
| 85 | ||
| 86 | const home = async () => (await fetch(`${servers.url}/`, { headers: { cookie }, redirect: "manual" })); | |
| 87 | ||
| 88 | test("without events in the window, no digest", async () => { | |
| 89 | const res = await home(); | |
| 90 | expect(res.status).toBe(200); | |
| 91 | expect(await res.text()).not.toContain('id="digest"'); | |
| 92 | }); | |
| 93 | ||
| 94 | test("three repositories: ordered, linked, and its own Server-Timing phase", async () => { | |
| 95 | const mk = async (name: string) => { | |
| 96 | const r = await createRepo(app, alice, { name, visibility: "public" }); | |
| 97 | if (!r.ok) throw new Error("repo"); | |
| 98 | return r.value; | |
| 99 | }; | |
| 100 | const [x, y, z] = [await mk("x"), await mk("y"), await mk("z")]; | |
| 101 | const emit = (repoId: string, kind: string, payload = {}) => | |
| 102 | app.bus.append({ kind, repoId, actorId: alice.id, payload }); | |
| 103 | emit(x.id, "pull.merged", { number: 1 }); | |
| 104 | emit(y.id, "pull.merged", { number: 2 }); | |
| 105 | emit(y.id, "app.serving"); | |
| 106 | emit(z.id, "run.finished", { kind: "deploy", state: "failed", summary: "boom", runId: "r9" }); | |
| 107 | const res = await home(); | |
| 108 | const body = await res.text(); | |
| 109 | expect(body).toContain("Since you were here"); | |
| 110 | expect(body).toContain("last 24 h"); | |
| 111 | const at = (s: string) => body.indexOf(s); | |
| 112 | expect(at("alice/y: 1 released, 1 landed")).toBeGreaterThan(-1); | |
| 113 | expect(at("alice/y: 1 released, 1 landed")).toBeLessThan(at("alice/z: 1 failed")); | |
| 114 | expect(body).toContain('href="/alice/z/runs/r9"'); | |
| 115 | expect(body).toContain('href="/alice/x/pulls/1"'); | |
| 116 | expect(body).toContain('href="/alice/y"'); | |
| 117 | expect(res.headers.get("server-timing") ?? "").toContain("digest"); | |
| 118 | }); | |
| 119 | }); |
new above
offline