Public: anyone can read this; the crew can change it.
One definition of a public app: home, the shelf, /explore, /api/v1/explore and /api/v1/apps count the same ones #13890
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13863-one-definition-of-a-public-app-home-the → main 4 commits fork 4 threads · 4 unresolved
src/actions/explore.ts +3 −3
| @@ -717,6 +717,7 @@ function rank( | ||
| 717 | 717 | limit: READ, |
| 718 | 718 | withoutArchived: !query.archived, |
| 719 | 719 | withoutUnclaimed: true, |
| 720 | withoutAgents: true, | |
| 720 | 721 | }); |
| 721 | 722 | const records = appsForRepos( |
| 722 | 723 | db, |
| @@ -726,12 +727,11 @@ function rank( | ||
| 726 | 727 | const appRepos = repos.filter( |
| 727 | 728 | (r) => { |
| 728 | 729 | const owner = owners.get(r.ownerId); |
| 729 | // An agent's working fork is not a find; a suspended or removed | |
| 730 | // account's apps are not offered to anyone. | |
| 730 | // A suspended or removed account's apps are not offered to anyone; | |
| 731 | // an agent's working fork is left out by the query (`withoutAgents`). | |
| 731 | 732 | return ( |
| 732 | 733 | records.has(r.id) && |
| 733 | 734 | owner !== undefined && |
| 734 | owner.kind !== "agent" && | |
| 735 | 735 | isActive(owner) |
| 736 | 736 | ); |
| 737 | 737 | }, |
src/core/apps.ts +5 −6
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | * no events. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { PUBLIC_VISIBLE } from "./visibility.ts"; | |
| 10 | import { OFFERED } from "./offered.ts"; | |
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | 12 | import { id } from "../kernel/id.ts"; |
| 13 | 13 | import { clearFires } from "./feature_fires.ts"; |
| @@ -154,16 +154,15 @@ export function listApps(db: Database): AppRecord[] { | ||
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
| 157 | * The apps whose repository is public — what any reader, signed in or not, | |
| 158 | * may see — newest first, at most `limit`. One query over the apps | |
| 159 | * themselves, so a forge with many plain repositories never crowds an app | |
| 160 | * out of the window. | |
| 157 | * The apps the forge offers a stranger (`OFFERED`), newest first, at most `limit`. One query over | |
| 158 | * the apps themselves, so a forge with many plain repositories never crowds an app out of the | |
| 159 | * window. | |
| 161 | 160 | */ |
| 162 | 161 | export function listPublicApps(db: Database, limit: number): AppRecord[] { |
| 163 | 162 | return db |
| 164 | 163 | .query<AppRow, [number]>( |
| 165 | 164 | `SELECT app.* FROM app JOIN repo ON repo.id = app.repo_id |
| 166 | WHERE ${PUBLIC_VISIBLE} ORDER BY app.id DESC LIMIT ?1`, | |
| 165 | WHERE ${OFFERED} ORDER BY app.id DESC LIMIT ?1`, | |
| 167 | 166 | ) |
| 168 | 167 | .all(limit) |
| 169 | 168 | .map(toApp); |
src/core/offered.ts added +62
| @@ -0,0 +1,62 @@ | ||
| 1 | /** | |
| 2 | * What the forge offers a stranger: one definition of "a public app", read by | |
| 3 | * the front page's count, /explore, /api/v1/explore and /api/v1/apps, so a | |
| 4 | * visitor is never told a number the lists beside it contradict (#13863). | |
| 5 | * | |
| 6 | * An app is offered when its repository is public with an owner in good | |
| 7 | * standing, is not archived, belongs to an account a person has claimed, and | |
| 8 | * is not an agent's working fork. Whether it is running is not part of the | |
| 9 | * definition: a sleeping app is offered, and says it sleeps. | |
| 10 | * | |
| 11 | * Two things the issue names are deliberately not rules. "Template": a | |
| 12 | * template is a repository people start from, and when it runs an app it is | |
| 13 | * as much a find as any other, so nothing is dropped for it. "Has a commit": | |
| 14 | * a repository with an app has been pushed to, and an empty one has no app, | |
| 15 | * so the count (apps only) already excludes it. | |
| 16 | * | |
| 17 | * Explore, which also shows a signed-in viewer their own private repositories, | |
| 18 | * builds its clause from the same parts (`NOT_ARCHIVED`, `NOT_UNCLAIMED`, | |
| 19 | * `NOT_AGENT`) rather than from `OFFERED` whole. The front page's shelf is a | |
| 20 | * narrower thing on purpose: it draws from `OFFERED` and then keeps only the | |
| 21 | * forge's own makers' running apps that have a picture; its count is not | |
| 22 | * narrowed. | |
| 23 | */ | |
| 24 | ||
| 25 | import type { Database } from "bun:sqlite"; | |
| 26 | import { PUBLIC_VISIBLE } from "./visibility.ts"; | |
| 27 | ||
| 28 | /** The repository's owner is a start account no person has claimed yet. */ | |
| 29 | export const UNCLAIMED_OWNER = `EXISTS (SELECT 1 FROM start_account | |
| 30 | WHERE start_account.user_id = repo.owner_id AND start_account.claimed_at IS NULL)`; | |
| 31 | ||
| 32 | /** The repository's owner is a crew agent: its forks are working copies, not finds. */ | |
| 33 | export const AGENT_OWNER = `EXISTS (SELECT 1 FROM user AS maker WHERE maker.id = repo.owner_id AND maker.is_agent = 1)`; | |
| 34 | ||
| 35 | /** | |
| 36 | * The three exclusions a find is subject to, whoever is looking (explore builds its clause from | |
| 37 | * these too). | |
| 38 | */ | |
| 39 | export const NOT_ARCHIVED = "repo.archived_at IS NULL"; | |
| 40 | export const NOT_UNCLAIMED = `NOT ${UNCLAIMED_OWNER}`; | |
| 41 | export const NOT_AGENT = `NOT ${AGENT_OWNER}`; | |
| 42 | ||
| 43 | /** A `repo` row a stranger is offered: public, not archived, claimed, not an agent's. */ | |
| 44 | export const OFFERED = `(${PUBLIC_VISIBLE} AND ${NOT_ARCHIVED} AND ${NOT_UNCLAIMED} AND ${NOT_AGENT})`; | |
| 45 | ||
| 46 | /** Which of these repositories are offered to a stranger, one query per chunk. */ | |
| 47 | export function offeredRepoIds(db: Database, repoIds: readonly string[]): Set<string> { | |
| 48 | const out = new Set<string>(); | |
| 49 | const unique = [...new Set(repoIds)]; | |
| 50 | const CHUNK = 400; | |
| 51 | for (let i = 0; i < unique.length; i += CHUNK) { | |
| 52 | const batch = unique.slice(i, i + CHUNK); | |
| 53 | const rows = db | |
| 54 | .query<{ id: string }, string[]>( | |
| 55 | `SELECT repo.id FROM repo WHERE ${OFFERED} | |
| 56 | AND repo.id IN (${batch.map((_, n) => `?${n + 1}`).join(",")})`, | |
| 57 | ) | |
| 58 | .all(...batch); | |
| 59 | for (const row of rows) out.add(row.id); | |
| 60 | } | |
| 61 | return out; | |
| 62 | } |
src/core/repos.ts +7 −4
| @@ -27,6 +27,7 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 27 | 27 | import { appsNaming, isAppMember } from "./app_members.ts"; |
| 28 | 28 | import { forgetListings } from "./listings.ts"; |
| 29 | 29 | import { forgetRepoCursors } from "./origins.ts"; |
| 30 | import { NOT_AGENT, NOT_ARCHIVED, NOT_UNCLAIMED } from "./offered.ts"; | |
| 30 | 31 | import { likeEscape, NOT_STANDING, pageLimit } from "./issues.ts"; |
| 31 | 32 | import { |
| 32 | 33 | OWNER_STANDING, |
| @@ -871,6 +872,8 @@ export interface ListReposOptions { | ||
| 871 | 872 | * a person has claimed it. Its own URL still answers. @default false |
| 872 | 873 | */ |
| 873 | 874 | withoutUnclaimed?: boolean; |
| 875 | /** Leave out a crew agent's repositories: its forks are working copies. @default false */ | |
| 876 | withoutAgents?: boolean; | |
| 874 | 877 | } |
| 875 | 878 | |
| 876 | 879 | // Visibility in SQL: public, owned, or owned by an org the viewer is on a |
| @@ -905,11 +908,11 @@ function visibleClause( | ||
| 905 | 908 | params.push(options.cursor); |
| 906 | 909 | where += ` AND repo.id < ?${params.length}`; |
| 907 | 910 | } |
| 908 | if (options.withoutArchived === true) where += " AND repo.archived_at IS NULL"; | |
| 911 | if (options.withoutArchived === true) where += ` AND ${NOT_ARCHIVED}`; | |
| 909 | 912 | if (options.archivedOnly === true) where += " AND repo.archived_at IS NOT NULL"; |
| 910 | 913 | if (options.templatesOnly === true) where += " AND repo.is_template = 1"; |
| 911 | if (options.withoutUnclaimed === true) | |
| 912 | where += " AND NOT EXISTS (SELECT 1 FROM start_account WHERE start_account.user_id = repo.owner_id AND start_account.claimed_at IS NULL)"; | |
| 914 | if (options.withoutUnclaimed === true) where += ` AND ${NOT_UNCLAIMED}`; | |
| 915 | if (options.withoutAgents === true) where += ` AND ${NOT_AGENT}`; | |
| 913 | 916 | params.push(pageLimit(options.limit)); |
| 914 | 917 | return { where, params }; |
| 915 | 918 | } |
| @@ -1029,7 +1032,7 @@ export function listReposWritableTo( | ||
| 1029 | 1032 | params.push(options.cursor); |
| 1030 | 1033 | where += ` AND repo.id < ?${params.length}`; |
| 1031 | 1034 | } |
| 1032 | if (options.withoutArchived === true) where += " AND repo.archived_at IS NULL"; | |
| 1035 | if (options.withoutArchived === true) where += ` AND ${NOT_ARCHIVED}`; | |
| 1033 | 1036 | if (options.onlyArchived === true) where += " AND repo.archived_at IS NOT NULL"; |
| 1034 | 1037 | params.push(pageLimit(options.limit)); |
| 1035 | 1038 | const rows = db |
src/http/api.ts +5
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * back to continue); errors are `{ error, message }`. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { offeredRepoIds } from "../core/offered.ts"; | |
| 11 | 12 | import { parseAttestation } from "../core/attestation.ts"; |
| 12 | 13 | import { listAppReleases } from "../core/app_releases.ts"; |
| 13 | 14 | import { openapiHandler } from "../contract/served.ts"; |
| @@ -3812,10 +3813,14 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3812 | 3813 | records.map((a) => a.repoId), |
| 3813 | 3814 | ); |
| 3814 | 3815 | const runtime = appsRuntime(ctx.app); |
| 3816 | // Another person's public app is listed only as /explore offers it; | |
| 3817 | // what the viewer owns or reaches by a team is theirs to list as it is. | |
| 3818 | const offered = offeredRepoIds(ctx.app.db, records.map((a) => a.repoId)); | |
| 3815 | 3819 | const out = []; |
| 3816 | 3820 | for (const record of records) { |
| 3817 | 3821 | const repo = repos.get(record.repoId); |
| 3818 | 3822 | if (!repo || !canRead(ctx.app.db, repo, ctx.auth.user)) continue; |
| 3823 | if (repo.visibility === "public" && repo.ownerId !== ctx.auth.user?.id && !offered.has(repo.id)) continue; | |
A second, hand-written filter beside the SQL rule, with an owner exception that /explore does not have. Keep it, but cover it with a test (an owned archived app is listed; another person's archived app is not). |
||
| 3819 | 3824 | out.push(appJson(ctx, record, repo, runtime)); |
| 3820 | 3825 | } |
| 3821 | 3826 | return out; |
src/web/studio/home.ts +4 −2
| @@ -772,8 +772,10 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 772 | 772 | : html`<div class="mt"> |
| 773 | 773 | ${empty({ |
| 774 | 774 | icon: "rocket", |
| 775 | title: "Nothing public is up yet", | |
| 776 | note: "Apps made public here show up on this shelf once they run.", | |
| 775 | title: front.total > 0 ? "Nothing on this shelf yet" : "Nothing public is up yet", | |
outdated
Principle 1 and 8: the title and the long note say the same thing twice, and 'pictures' is jargon. One plain sentence with a link to Explore. The real fix is for the shelf to read OFFERED, not to reword the empty state. |
||
| 776 | ...(front.total > 0 | |
| 777 | ? { action: btn(`See all ${front.total} public ${front.total === 1 ? "app" : "apps"}`, { href: "/explore?q=kind%3Aapp", variant: "link" }) } | |
| 778 | : { note: "Apps made public here show up on this shelf once they run." }), | |
| 777 | 779 | })} |
| 778 | 780 | </div>`} |
| 779 | 781 | </section> |
test/front.test.ts +3 −2
| @@ -863,7 +863,7 @@ describe("the front page", () => { | ||
| 863 | 863 | expect((await put(adminToken, { pinned: [], hidden: [] })).status).toBe(200); |
| 864 | 864 | }); |
| 865 | 865 | |
| 866 | test("with nothing owned by an admin or pinned the door says nothing is up yet, and shows no stranger's app (#11902)", async () => { | |
| 866 | test("with nothing owned by an admin or pinned the door says its shelf is bare, points to Explore, and shows no stranger's app (#11902)", async () => { | |
| 867 | 867 | await make(member, "door-lonely"); |
| 868 | 868 | forgetFrontScores(app); |
| 869 | 869 | // Hide what the owner has made (the shelf is capped, so until none is left). |
| @@ -877,7 +877,8 @@ describe("the front page", () => { | ||
| 877 | 877 | forgetFrontScores(app); |
| 878 | 878 | } |
| 879 | 879 | const body = await door(); |
| 880 | expect(body).toContain("Nothing public is up yet"); | |
| 880 | expect(body).toContain("Nothing on this shelf yet"); | |
| 881 | expect(body).toContain("/explore?q=kind%3Aapp"); | |
| 881 | 882 | expect(body).not.toContain("mia/door-lonely"); |
| 882 | 883 | expect((await put(adminToken, { pinned: [], hidden: [] })).status).toBe(200); |
| 883 | 884 | }); |
test/one-public-app.test.ts added +144
| @@ -0,0 +1,144 @@ | ||
| 1 | /** | |
The test never covers the shelf, an unclaimed owner, or an agent-owned fork, so it would not fail on the cases that motivated the change. Add them and confirm it fails on the old code. |
||
| 2 | * One definition of a public app (#13863): the front page's count, /explore, | |
| 3 | * /api/v1/explore, /api/v1/apps and the shelf are asked about the same apps — | |
| 4 | * public running and asleep, private, archived, an unclaimed start's, an | |
| 5 | * agent's fork, another person's — and name the same ones. A visitor is never | |
| 6 | * told a number the lists contradict. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { doorRepos, frontPage } from "../src/actions/front.ts"; | |
| 14 | import { createRepo, setArchived } from "../src/actions/repos.ts"; | |
| 15 | import { AppsRuntime, type EnvState, type EnvStatus } from "../src/apps/runtime.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { recordLook } from "../src/core/looks.ts"; | |
| 18 | import { encodePng } from "../src/design/image/png.ts"; | |
| 19 | import { mkdirSync, writeFileSync } from "node:fs"; | |
| 20 | import { createApp as insertApp, type AppRecord } from "../src/core/apps.ts"; | |
| 21 | import { getRepo } from "../src/core/repos.ts"; | |
| 22 | import { createUser, type User } from "../src/core/users.ts"; | |
| 23 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 24 | import { quietLogs } from "./helpers/globals.ts"; | |
| 25 | import { promote } from "./helpers/members.ts"; | |
| 26 | ||
| 27 | quietLogs(); | |
| 28 | ||
| 29 | let dataDir: string; | |
| 30 | let app: App; | |
| 31 | let servers: Servers; | |
| 32 | let restore: () => void; | |
| 33 | let runtime: AppsRuntime; | |
| 34 | let stranger: User; | |
| 35 | let token: string; | |
| 36 | const states = new Map<string, EnvState>(); | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | dataDir = await mkdtemp(join(tmpdir(), "forge-offered-")); | |
| 40 | app = createApp({ dataDir, port: 0 }); | |
| 41 | servers = await serve(app); | |
| 42 | const made = await createUser(app.db, { | |
| 43 | origin: "seed", trusted: true, username: "owner", allowReserved: true, | |
| 44 | email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true, | |
| 45 | }); | |
| 46 | if (!made.ok) throw new Error(made.error); | |
| 47 | promote(app.db, made.value); | |
| 48 | const owner: User = made.value; | |
| 49 | const person = async (username: string): Promise<User> => { | |
| 50 | const u = await createUser(app.db, { | |
| 51 | origin: "seed", trusted: true, username, allowReserved: true, | |
| 52 | email: `${username}@forge.local`, password: "a-good-passphrase", | |
| 53 | }); | |
| 54 | if (!u.ok) throw new Error(u.error); | |
| 55 | promote(app.db, u.value); | |
| 56 | return u.value; | |
| 57 | }; | |
| 58 | const png = Buffer.from(encodePng(1, 1, new Uint8Array([255, 255, 255, 255]))); | |
| 59 | const seed = async (who: User, name: string, visibility: "public" | "private", state: EnvState, archive = false) => { | |
| 60 | const repo = await createRepo(app, who, { name, visibility, description: name }); | |
| 61 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 62 | const record = insertApp(app.db, { repoId: repo.value.id, slug: name, access: "public" }); | |
| 63 | if (!record.ok) throw new Error(record.error); | |
| 64 | states.set(record.value.id, state); | |
| 65 | // Every app has a picture, so the shelf's only filters are the rules under test. | |
| 66 | mkdirSync(join(dataDir, "looks", record.value.id), { recursive: true }); | |
| 67 | writeFileSync(join(dataDir, "looks", record.value.id, "thumb.png"), png); | |
| 68 | recordLook(app.db, { | |
| 69 | appId: record.value.id, env: "production", commitOid: "a".repeat(40), | |
| 70 | kind: "thumb", path: `looks/${record.value.id}/thumb.png`, | |
| 71 | }); | |
| 72 | if (archive) { | |
| 73 | const done = await setArchived(app, who, getRepo(app.db, who.username, name)!, true, false); | |
| 74 | if (!done.ok) throw new Error("archive failed"); | |
| 75 | } | |
| 76 | }; | |
| 77 | stranger = await person("stranger"); | |
| 78 | const unclaimed = await person("unclaimed"); | |
| 79 | const agent = await person("crewbot"); | |
| 80 | app.db.query("INSERT INTO start_account (user_id, claim_hash, source, created_at) VALUES (?1, 'h', 'test', ?2)") | |
| 81 | .run(unclaimed.id, new Date().toISOString()); | |
| 82 | app.db.query("UPDATE user SET is_agent = 1 WHERE id = ?1").run(agent.id); | |
| 83 | await seed(owner, "running-one", "public", "running"); | |
| 84 | await seed(owner, "secret-one", "private", "running"); | |
| 85 | await seed(owner, "asleep-one", "public", "asleep"); | |
| 86 | await seed(owner, "archived-one", "public", "running", true); | |
| 87 | await seed(stranger, "theirs-open", "public", "running"); | |
| 88 | await seed(stranger, "theirs-archived", "public", "running", true); | |
| 89 | await seed(unclaimed, "unclaimed-one", "public", "running"); | |
| 90 | await seed(agent, "agent-fork", "public", "running"); | |
| 91 | token = createToken(app.db, stranger.id, "t", "ro").token; | |
| 92 | runtime = new AppsRuntime(app, { | |
| 93 | domain: "offered.test", scheme: "http", publicPort: servers.port, forgeUrl: servers.url, | |
| 94 | }); | |
| 95 | const status = spyOn(AppsRuntime.prototype, "status").mockImplementation( | |
| 96 | (record: AppRecord): EnvStatus[] => | |
| 97 | [{ env: "production", state: states.get(record.id) ?? "stopped", commitOid: "a".repeat(40) } as EnvStatus], | |
| 98 | ); | |
| 99 | restore = () => status.mockRestore(); | |
| 100 | }, 30_000); | |
| 101 | ||
| 102 | afterAll(async () => { | |
| 103 | restore(); | |
| 104 | await runtime.stop(); | |
| 105 | servers.stop(); | |
| 106 | app.close(); | |
| 107 | await rm(dataDir, { recursive: true, force: true }); | |
| 108 | }); | |
| 109 | ||
| 110 | const OFFERED = ["asleep-one", "running-one", "theirs-open"]; | |
| 111 | const NOT_OFFERED = ["secret-one", "archived-one", "theirs-archived", "unclaimed-one", "agent-fork"]; | |
| 112 | ||
| 113 | test("every door a stranger uses names the same public apps", async () => { | |
| 114 | const get = (path: string) => fetch(`${servers.url}${path}`); | |
| 115 | const explored = ((await (await get("/api/v1/explore?q=kind%3Aapp")).json()) as any).items.map( | |
| 116 | (i: any) => i.name.split("/")[1], | |
| 117 | ).sort(); | |
| 118 | const listed = ((await (await get("/api/v1/apps")).json()) as any).items | |
| 119 | .map((i: any) => i.repo.name).sort(); | |
| 120 | const page = await (await get("/explore?q=kind%3Aapp")).text(); | |
| 121 | const front = frontPage(app, Date.now()); | |
| 122 | ||
| 123 | expect(explored).toEqual(OFFERED); | |
| 124 | expect(listed).toEqual(OFFERED); | |
| 125 | for (const name of OFFERED) expect(page).toContain(`/${name}`); | |
| 126 | for (const name of NOT_OFFERED) expect(page).not.toContain(`/${name}`); | |
| 127 | expect(front.total).toBe(OFFERED.length); | |
| 128 | expect(front.pulse.running).toBe(2); | |
| 129 | }); | |
| 130 | ||
| 131 | test("the shelf shows only apps the others offer", () => { | |
| 132 | const shown = doorRepos(app).map((r) => r.name).sort(); | |
| 133 | // A narrower thing on purpose (the forge's own makers, with a picture), never a wider one. | |
| 134 | expect(shown).toEqual(["asleep-one", "running-one"]); | |
| 135 | for (const name of shown) expect(OFFERED).toContain(name); | |
| 136 | }); | |
| 137 | ||
| 138 | test("a signed-in person's list adds their own archived app, and no one else's", async () => { | |
| 139 | const res = await fetch(`${servers.url}/api/v1/apps`, { headers: { authorization: `Bearer ${token}` } }); | |
| 140 | const names = ((await res.json()) as any).items.map((i: any) => i.repo.name); | |
| 141 | expect(names).toContain("theirs-open"); | |
| 142 | expect(names).toContain("theirs-archived"); | |
| 143 | for (const name of ["archived-one", "unclaimed-one", "agent-fork", "secret-one"]) expect(names).not.toContain(name); | |
| 144 | }); |
This is not yet the one definition: explore's visibleClause in repos.ts still writes the public/archived/agent rules itself. Build that clause from OFFERED so the five doors cannot drift.