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

web/layout: stop loading the server through the Talk count and the page links #17388

opened this

Merged 26h 41m ago, live in production

layout-leaf → main 0 commits

  • Merged. layout-leaf was merged into main as 4bd7794.

Review

Sign in to review this change.
7 files changed +176 −113
src/actions/conversation_confirms.ts +1 −6
@@ -19,7 +19,7 @@ import { lastEventOf } from "../core/events.ts";
1919 import { getUserById } from "../core/users.ts";
2020 import { getIssue, labelsFor } from "../core/issues.ts";
2121 import { getRepo } from "../core/repos.ts";
22 import { answerHeld, countWaiting, HELD_TTL_MS, waitingFor, type YesRefusal } from "../core/fence.ts";
22 import { answerHeld, HELD_TTL_MS, waitingFor, type YesRefusal } from "../core/fence.ts";
2323 import type { User } from "../core/users.ts";
2424 import { argumentsShown, destroysData, destroysSentence, keptOf, undoOf } from "../crew/conversation_confirm.ts";
2525 import { confirmSentence } from "../crew/conversation_ask.ts";
@@ -52,11 +52,6 @@ export function waitingConfirms(ctx: Pick<Ctx, "app">, user: User, now: number =
5252 }));
5353 }
5454
55 /** How many confirms wait on the person's yes, for the header's Talk button. */
56 export function waitingCount(ctx: Pick<Ctx, "app">, user: User, now: number = Date.now()): number {
57 return countWaiting(ctx.app.db, user.id, now);
58 }
59
6055 export const CONFIRM_ID = /^[0-9a-z]{1,40}$/;
6156
6257 /** Why a yes did nothing, in words, with the status the API answers. */
src/web/layout.ts +3 −3
@@ -12,7 +12,7 @@ import { keyOf } from "./keymap.ts";
1212 import { requestHolds } from "../http/conditional.ts";
1313 import { sshOn } from "../settings/ssh.ts";
1414 import { hereAddress, returnTo, withReturn } from "./return_to.ts";
15 import { waitingCount } from "../actions/conversation_confirms.ts";
15 import { countWaiting } from "../core/fence.ts";
1616 import { goLevel, goPaletteRows } from "./go_pages.ts";
1717 import { NEW_ISSUE_KEY } from "./script_keys.ts";
1818 import { newIssueHref } from "./issue_from.ts";
@@ -60,7 +60,7 @@ import {
6060 spriteAsset,
6161 stylesheetAsset,
6262 } from "./asset.ts";
63 import { linkHeader, pageLinks, unindexed, type PageLink } from "./standards.ts";
63 import { linkHeader, pageLinks, unindexed, type PageLink } from "./page_links.ts";
6464 import { DESCRIPTION_MAX } from "./describe.ts";
6565 import { breadcrumbStructuredData } from "./structured_data.ts";
6666 import {
@@ -697,7 +697,7 @@ export function signUpHref(ctx: Ctx): string | undefined {
697697 * button renders.
698698 */
699699 function talkButton(ctx: Ctx, user: User, about?: string): Html {
700 const waiting = waitingCount(ctx, user);
700 const waiting = countWaiting(ctx.app.db, user.id);
701701 const label = waiting > 0 ? `Talk to the forge, ${waiting} waiting` : "Talk to the forge";
702702 return html`<a class="btn btn-icon talk-btn" href="${talkHref(about)}" aria-label="${label}" data-shortcut="${keyOf("talk")}" ${hintAttr(keyOf("talk"), "Talk to the forge")}
703703 >${icon("comment")}${waiting > 0 ? html`<span class="talk-count" aria-hidden="true">${waiting > 9 ? "9+" : String(waiting)}</span>` : null}</a
src/web/standards.ts +1 −98
@@ -29,7 +29,6 @@
2929 * FORGE_SECURITY_CONTACT.
3030 */
3131
32 import { repoOnRequest } from "../http/request_repo.ts";
3332 import { noneMatch } from "../http/conditional.ts";
3433 import { sshOn } from "../settings/ssh.ts";
3534 import { UPSTREAM_REPO } from "../core/upstream.ts";
@@ -37,13 +36,10 @@ import { CRAWL_DELAY_S, DEEP_SEGMENTS } from "../http/crawl.ts";
3736 import { markPng, type MarkShape } from "../design/mark.ts";
3837 import { metaThemeColor } from "../design/themes.ts";
3938 import { BODY_MAX, TITLE_MAX } from "../core/reports.ts";
40 import { reportsClosedFor } from "./reports_closed.ts";
4139 import { pageDay, sitemapPages } from "./sitemap_pages.ts";
4240 import { dayOf } from "../release/days.ts";
4341 import { readLandings } from "../release/landings.ts";
44 import { takesOutside } from "../actions/contribute_token.ts";
4542 import { OFFER_PATCH_MAX, OFFERS_PER_SOURCE } from "../actions/offer.ts";
46 import { REPORT_LINK_TITLE } from "../actions/report_words.ts";
4743 import { sshTellCommand } from "../ssh/command.ts";
4844 import { llmsRepos, newestLanding, sitemapRepos, type LlmsListing } from "../core/listings.ts";
4945 import { canRead, getRepo, type Repo } from "../core/repos.ts";
@@ -69,6 +65,7 @@ import { GUIDE } from "../view/guide.ts";
6965 import { loadMcp } from "../http/mcp/load.ts";
7066 import { timePhase, timePhaseAsync } from "../core/route_timing.ts";
7167 import { shippedReleases, type Shipped } from "../release/shipped.ts";
68 import { UNINDEXED_PAGES } from "./page_links.ts";
7269
7370 const DAY = 24 * 60 * 60 * 1000;
7471
@@ -309,17 +306,6 @@ export function releasesJson(ctx: Ctx): Response {
309306 /** The releases document and its ETag, per origin, made again when the tags read differently. */
310307 const releasesBodies = new Map<string, { list: readonly Shipped[]; body: string; etag: string }>();
311308
312 /**
313 * The pages that are never a search result: robots.txt keeps a crawler out of them, and
314 * each one's head also says `noindex` (`unindexed`), for the crawler that arrives by a link.
315 */
316 export const UNINDEXED_PAGES = ["/signin", "/signup", "/settings", "/search"] as const;
317
318 /** A page under `UNINDEXED_PAGES`: its head says `noindex`. */
319 export function unindexed(pathname: string): boolean {
320 return UNINDEXED_PAGES.some((p) => pathname === p || pathname.startsWith(`${p}/`));
321 }
322
323309 /** GET /robots.txt */
324310 export function robotsTxt(ctx: Ctx): Response {
325311 return textWithEtag(
@@ -708,93 +694,10 @@ function renderLlmsTxt(ctx: Ctx, o: string, tell: string | undefined, running: s
708694 );
709695 }
710696
711 export interface PageLink {
712 readonly rel: string;
713 readonly href: string;
714 readonly title?: string;
715 readonly type?: string;
716 }
717
718 /**
719 * What every page points an agent at, as link relations (RFC 8288): the
720 * forge's own description for agents, and — on a public repository's pages
721 * — where to report a problem without an account. `page()` writes them both
722 * as `<link>` tags and as a `Link` header, so a reader of either finds them.
723 */
724 export function pageLinks(ctx: Ctx): PageLink[] {
725 const links: PageLink[] = [
726 { rel: "service-doc", href: "/llms.txt", type: "text/plain" },
727 // What the reader of this page can do here, as who they are
728 // (`actions/here.ts`), for an agent that would rather not scrape it.
729 {
730 rel: "alternate",
731 href: `/api/v1/here?path=${encodeURIComponent(ctx.url.pathname)}`,
732 type: "application/json",
733 },
734 ];
735 const owner = ctx.params["owner"];
736 const name = ctx.params["name"];
737 if (
738 owner !== undefined &&
739 name !== undefined &&
740 ctx.url.pathname.startsWith(`/${owner}/${name}`)
741 ) {
742 const repo = repoOnRequest(ctx.app.db, ctx.url, ctx.request.method, owner, name.replace(/\.git$/, ""));
743 if (
744 repo !== undefined &&
745 canRead(ctx.app.db, repo, undefined) &&
746 !reportsClosedFor(ctx, repo.id)
747 ) {
748 links.push({
749 rel: "help",
750 href: `/${repo.ownerName}/${repo.name}/report`,
751 title: REPORT_LINK_TITLE,
752 });
753 // A public page unfurls elsewhere through oEmbed; a private one advertises nothing.
754 links.push({
755 rel: "alternate",
756 href: oembedHref(ctx.url.origin, ctx.url.pathname),
757 title: "oEmbed",
758 type: OEMBED_TYPE,
759 });
760 // The same door for an agent: the endpoint that takes the JSON.
761 links.push({
762 rel: "help",
763 href: `/api/v1/repos/${repo.ownerName}/${repo.name}/reports`,
764 title: "Report a problem as JSON: POST {title, body?, page?}",
765 });
766 // The door for a change, only where the owner has said yes to it.
767 if (ctx.url.pathname === `/${repo.ownerName}/${repo.name}` && takesOutside(ctx.app, repo))
768 links.push({
769 rel: "help",
770 href: "/api/v1/contribute",
771 title: "Offer a change: ask for a day's token, push contrib/<name>/..., open a pull",
772 });
773 }
774 }
775 return links;
776 }
777
778 /** The same links as an HTTP `Link` header value. */
779 export function linkHeader(links: readonly PageLink[]): string {
780 return links
781 .map(
782 (l) =>
783 `<${l.href}>; rel="${l.rel}"${l.title === undefined ? "" : `; title="${l.title}"`}${l.type === undefined ? "" : `; type="${l.type}"`}`,
784 )
785 .join(", ");
786 }
787
788697 // ---- /oembed: the oEmbed 1.0 link card of a public repository or app page (never fetches the url)
789698
790 export const OEMBED_TYPE = "application/json+oembed";
791699 const URL_MAX = 2048;
792700
793 /** The `<link>` a public page advertises: where its oEmbed answer is. */
794 export function oembedHref(origin: string, pathname: string): string {
795 return `${origin}/oembed?url=${encodeURIComponent(`${origin}${pathname}`)}&format=json`;
796 }
797
798701 /**
799702 * `[owner, name]` from a url on `origin`; undefined when it is another host or names no repository.
800703 */
test/layout-closure.test.ts added +64
@@ -0,0 +1,64 @@
1 /**
2 * The layout stays a small module. Nearly every test reaches `src/web/layout.ts`, so whatever
3 * the layout imports, those tests load and the import graph picks them for a change there.
4 * It reached the HTTP server and the MCP tools once, through the Talk button's count of waiting
5 * confirms and the page links, which put 4,416 of the tree's files behind every page. Red here
6 * when an import brings that path back, or when the closure grows past {@link CLOSURE_MAX}.
7 *
8 * Reads files; boots nothing.
9 *
10 * @tree-scan: walks the import graph from src/web/layout.ts.
11 */
12
13 import { describe, expect, test } from "bun:test";
14 import { existsSync } from "node:fs";
15 import { importsReader } from "../scripts/tier-input.ts";
16
17 const LAYOUT = "src/web/layout.ts";
18
19 /**
20 * The layout's import closure was 1,656 source files when this guard was written; lower it as cuts
21 * land.
22 */
23 const CLOSURE_MAX = 2000;
24
25 /** Modules the layout must never load: the server and every route it mounts, and the MCP tools. */
26 const KEPT_OUT = (f: string) => f === "src/http/server.ts" || f === "src/http/mcp.ts" || f.startsWith("src/http/mcp/");
27
28 /**
29 * Every file reachable from `start` by static and literal dynamic imports, each with the file that
30 * first imported it.
31 */
32 function closure(start: string): Map<string, string> {
33 const imports = importsReader();
34 const from = new Map<string, string>([[start, ""]]);
35 const stack = [start];
36 while (stack.length > 0) {
37 const f = stack.pop()!;
38 for (const i of imports(f))
39 if (!from.has(i) && existsSync(i)) {
40 from.set(i, f);
41 stack.push(i);
42 }
43 }
44 return from;
45 }
46
47 /** The import chain from the layout to `f`, for the failure message. */
48 function chain(from: Map<string, string>, f: string): string {
49 const path = [f];
50 for (let x = from.get(f); x; x = from.get(x)) path.unshift(x);
51 return path.join(" -> ");
52 }
53
54 describe("the layout's import closure", () => {
55 const from = closure(LAYOUT);
56
57 test("never reaches the server or the MCP tools", () => {
58 expect([...from.keys()].filter(KEPT_OUT).map((f) => chain(from, f))).toEqual([]);
59 });
60
61 test(`stays under ${CLOSURE_MAX} source files`, () => {
62 expect([...from.keys()].filter((f) => f.startsWith("src/")).length).toBeLessThanOrEqual(CLOSURE_MAX);
63 });
64 });
test/public-head.test.ts +1 −1
@@ -12,7 +12,7 @@ import { createSession } from "../src/core/sessions.ts";
1212 import { catalog } from "../src/packs/catalog.ts";
1313 import { seedPacks } from "./helpers/packs.ts";
1414 import { serve, type Servers } from "../src/http/server.ts";
15 import { UNINDEXED_PAGES } from "../src/web/standards.ts";
15 import { UNINDEXED_PAGES } from "../src/web/page_links.ts";
1616
1717 /** The content of the first tag matching, or undefined when there is none. */
1818 function meta(html: string, key: "name" | "property", value: string): string | undefined {
test/worker-database-opens.test.ts −5
@@ -56,21 +56,16 @@ const ONE_JOB: Record<string, string> = {
5656 * that no Worker calls these.
5757 */
5858 const REACHED: Record<string, { opens: number; why: string }> = {
59 "src/apps/changes.ts": { opens: 1, why: "an app's change stream keeps one handle per app database, opened on its first read or append, on the serving thread behind a request; no Worker calls it" },
6059 "src/apps/data.ts": { opens: 1, why: "quiesceSqlite folds each app database's log before a copy: called by the serving thread's clone and backup paths, in reach of Workers only through the page layer's imports" },
6160 "src/apps/dump_import.ts": { opens: 3, why: "a person's import of a dump, on the serving thread behind a request; no Worker calls it" },
62 "src/apps/dump_undo.ts": { opens: 1, why: "an undo folds the live store's log before it is set aside, on the serving thread behind an admin's request; no Worker calls it" },
6361 "src/crew/sentinel.ts": { opens: 1, why: "one scratch database in memory, made once for the process and remembered; the sentinel is a duty of the serving thread" },
64 "src/data/browse.ts": { opens: 1, why: "openReadOnly for a data page, on the serving thread behind a request; no Worker calls it" },
6562 "src/kernel/db.ts": { opens: 2, why: "the forge's own database, opened once by createApp on the thread that boots; the Workers that read it open their own handle and keep it (KEEPS)" },
6663 "src/release/rollback.ts": { opens: 1, why: "liveSchema reads the schema count once for a rollback or a swap, on the serving thread or in the release tool" },
6764 "src/snapshot/drill.ts": { opens: 1, why: "a restore drill opens the restored copy once, as a duty of the serving thread" },
6865 "src/snapshot/drill_hosted.ts": { opens: 1, why: "a restore drill opens the one hosted forge it restored once, for its integrity check, in the drill's own process or as a duty of the serving thread; no Worker calls it" },
6966 "src/snapshot/restore.ts": { opens: 1, why: "a restore reads the sealed names of the restored copy once, in the restore tool or a drill on the serving thread" },
7067 "src/snapshot/snapshot.ts": { opens: 1, why: "a snapshot counts the rows of its own copy once, as a duty of the serving thread (a hosted forge's copy is read in a process of its own, snapshot/hosted_copy.ts); the mirror Worker imports this file for the manifest only" },
71 "src/spawn/move_in.ts": { opens: 1, why: "inspect opens one uploaded copy of a browser forge's database once, read-only, to count its rows before a move, on the serving thread behind an admin's request; no Worker calls it" },
7268 "src/spawn/child_runs.ts": { opens: 2, why: "called by the reader process (spawn/child_db_reader.ts), never by a thread of the forge; cell.ts re-exports the two for tests that read a file directly" },
73 "src/web/render/sqlite.ts": { opens: 1, why: "a committed .sqlite file is deserialized read-only in memory for its page, on the serving thread behind a request; a Worker reaches it only through src/generated/screens.ts, which lists every screen so the bundle holds them, and calls no open" },
7469 "src/system/compact.ts": { opens: 2, why: "compactFile opens the database and its copy once each, called only by the compaction's own process (system/compact_child.ts) at boot when the compact-on-boot marker asks; Workers reach the file through db_size.ts's keptCopies, which opens nothing" },
7570 };
7671

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs