Public: anyone can read this; the crew can change it.
Registry: per-app ownership records for installed items (#6009) #17055
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/17038-registry-per-app-ownership-records-for-i → main 0 commits fork 3 threads · 3 unresolved
25 files
changed +693
−3
- docs/features/app-registry.md
- src/actions/app_registry.ts
- src/contract/openapi/routes/deleteApiV1ReposOwnerNameAppRegistryItem.ts
- src/contract/openapi/routes/getApiV1ReposOwnerNameAppRegistry.ts
- src/contract/openapi/routes/index.ts
- src/contract/openapi/routes/putApiV1ReposOwnerNameAppRegistryItem.ts
- src/core/account_purge.ts
- src/core/app_registry.ts
- src/core/empty_words.ts
- src/core/migration-pins/20261201140000-app-registry-item.pin
- src/core/migrations/20261201140000-app-registry-item.ts
- src/generated/migrations.ts
- src/http/api.ts
- src/http/api/app_registry.ts
- src/http/mcp/tools/app_registry.ts
- src/http/mcp/tools/index.ts
- src/tui/screens/appregistry.ts
- src/tui/screens/workspace.ts
- test/app-registry-doors.test.ts
- test/app-registry.test.ts
- test/conversation-confirm.test.ts
- test/delete-tier.test.ts
- test/mcp-forge-protocol.test.ts
- test/mixed-actions-held.test.ts
- test/route-timing-coverage.test.ts
docs/features/app-registry.md added +20
| @@ -0,0 +1,20 @@ | ||
| 1 | --- | |
| 2 | topic: Your apps | |
| 3 | row: What an app has installed from the component registry (`Registry items` in the terminal) | |
| 4 | --- | |
| 5 | # What an app has installed from the component registry | |
| 6 | ||
| 7 | Installing a registry item copies its source into the app, which then owns it. The forge keeps one | |
| 8 | note per app and item: the version the app holds, who noted it and when, so an update is the | |
| 9 | registry's newer version against that note (#17038, a slice of #6009). | |
| 10 | ||
| 11 | - **API:** `GET /api/v1/repos/:owner/:name/app/registry` lists `{ items: [{ item, version, | |
| 12 | installedBy, installedAt }] }` for anyone who reads the repository. | |
| 13 | `PUT …/app/registry/:item` with `{ "version": "1.2.0" }` notes an install, and | |
| 14 | `DELETE …/app/registry/:item` notes the removal; both need an admin of the repository and an | |
| 15 | admin-scope token, and answer 404 to anyone else. | |
| 16 | - **MCP:** `app_registry` with `{ repo, action: "list" | "install" | "remove", item?, version? }`. | |
| 17 | - **Terminal:** the palette's `<owner>/<name> — Registry items` lists them for a reader; the admin presses `i` to note an install and `x` to forget the item under the cursor. A write from the API or MCP reaches an open screen through the `app.registry_installed` and `app.registry_removed` events. | |
| 18 | ||
| 19 | An item's name is lower case letters, digits and dashes (up to 48); a version is up to 32 letters, | |
| 20 | digits, dots, dashes, plus signs and underscores. An app holds at most 200 items. |
src/actions/app_registry.ts added +51
| @@ -0,0 +1,51 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the component registry (#17038): one gate for the API, the MCP and | |
| 3 | * the terminal. Anyone who reads the repository reads the list; noting an install or its removal | |
| 4 | * is its admin's, and a non-admin is told `not_found` like every settings door. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../app.ts"; | |
| 8 | import { | |
| 9 | forgetInstalled, | |
| 10 | installedItems, | |
| 11 | ITEM_NAME, | |
| 12 | ITEM_VERSION, | |
| 13 | MAX_ITEMS, | |
| 14 | recordInstalled, | |
| 15 | type InstalledItem, | |
| 16 | } from "../core/app_registry.ts"; | |
| 17 | import type { Repo } from "../core/repos.ts"; | |
| 18 | import type { User } from "../core/users.ts"; | |
| 19 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 20 | import { requireReader, requireRepoAdmin } from "./access.ts"; | |
| 21 | ||
| 22 | export type RegistryError = "not_found" | { message: string }; | |
| 23 | ||
| 24 | /** The items a reader of the repository may see installed. */ | |
| 25 | export function registryItems(app: App, actor: User | undefined, repo: Repo): Result<InstalledItem[], "not_found"> { | |
| 26 | const gate = requireReader(app, actor, repo); | |
| 27 | if (!gate.ok) return gate; | |
| 28 | return ok(installedItems(app.db, repo.id)); | |
| 29 | } | |
| 30 | ||
| 31 | /** Note that `item` is installed at `version`; the repository's admin only. */ | |
| 32 | export function noteInstalled(app: App, actor: User | undefined, repo: Repo, item: string, version: string): Result<InstalledItem[], RegistryError> { | |
| 33 | const gate = requireRepoAdmin(app, actor, repo); | |
| 34 | if (!gate.ok) return gate; | |
| 35 | if (!ITEM_NAME.test(item)) return err({ message: "An item's name is lower case letters, digits and dashes." }); | |
| 36 | if (!ITEM_VERSION.test(version)) return err({ message: "A version is letters, digits, dots, dashes, plus signs and underscores, up to 32." }); | |
| 37 | const held = installedItems(app.db, repo.id); | |
| 38 | if (held.length >= MAX_ITEMS && !held.some((h) => h.item === item)) return err({ message: `An app holds at most ${MAX_ITEMS} items.` }); | |
| 39 | recordInstalled(app.db, repo.id, item, version, gate.value.id); | |
| 40 | app.bus.append({ kind: "app.registry_installed", repoId: repo.id, actorId: gate.value.id, payload: { item, version } }); | |
| 41 | return ok(installedItems(app.db, repo.id)); | |
| 42 | } | |
| 43 | ||
| 44 | /** Note that `item` was removed from the app; the repository's admin only. */ | |
| 45 | export function noteRemoved(app: App, actor: User | undefined, repo: Repo, item: string): Result<InstalledItem[], RegistryError> { | |
| 46 | const gate = requireRepoAdmin(app, actor, repo); | |
| 47 | if (!gate.ok) return gate; | |
| 48 | if (!forgetInstalled(app.db, repo.id, item)) return err({ message: `"${item.slice(0, 48)}" is not installed.` }); | |
| 49 | app.bus.append({ kind: "app.registry_removed", repoId: repo.id, actorId: gate.value.id, payload: { item } }); | |
| 50 | return ok(installedItems(app.db, repo.id)); | |
| 51 | } |
src/contract/openapi/routes/deleteApiV1ReposOwnerNameAppRegistryItem.ts added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "delete", | |
| 5 | path: `${REPO}/app/registry/{item}`, | |
| 6 | summary: "Note that an app removed a registry item", | |
| 7 | detail: "Forget that the app holds a registry item (admin scope; admin of the repository); answers { items }", | |
| 8 | auth: true, | |
| 9 | role: "member", | |
| 10 | refuses: [401, 404], | |
| 11 | }; |
src/contract/openapi/routes/getApiV1ReposOwnerNameAppRegistry.ts added +11
| @@ -0,0 +1,11 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "get", | |
| 5 | path: `${REPO}/app/registry`, | |
| 6 | summary: "List the registry items an app has installed", | |
| 7 | detail: "List the component registry items the app has installed with their versions (anyone who reads the repository); answers { items: [{ item, version, installedBy, installedAt }] }", | |
| 8 | auth: false, | |
| 9 | role: "anonymous", | |
| 10 | refuses: [404], | |
| 11 | }; |
src/contract/openapi/routes/index.ts +3
| @@ -17,6 +17,7 @@ export { route as deleteApiV1ReposOwnerName } from "./deleteApiV1ReposOwnerName. | ||
| 17 | 17 | export { route as deleteApiV1ReposOwnerNameApp } from "./deleteApiV1ReposOwnerNameApp.ts"; |
| 18 | 18 | export { route as deleteApiV1ReposOwnerNameAppDomainsHost } from "./deleteApiV1ReposOwnerNameAppDomainsHost.ts"; |
| 19 | 19 | export { route as deleteApiV1ReposOwnerNameAppEnvVar } from "./deleteApiV1ReposOwnerNameAppEnvVar.ts"; |
| 20 | export { route as deleteApiV1ReposOwnerNameAppRegistryItem } from "./deleteApiV1ReposOwnerNameAppRegistryItem.ts"; | |
| 20 | 21 | export { route as deleteApiV1ReposOwnerNameBranchesBranch } from "./deleteApiV1ReposOwnerNameBranchesBranch.ts"; |
| 21 | 22 | export { route as deleteApiV1ReposOwnerNameBuildsLabel } from "./deleteApiV1ReposOwnerNameBuildsLabel.ts"; |
| 22 | 23 | export { route as deleteApiV1ReposOwnerNameBuildsLabelShotsFile } from "./deleteApiV1ReposOwnerNameBuildsLabelShotsFile.ts"; |
| @@ -155,6 +156,7 @@ export { route as getApiV1ReposOwnerNameAppMembers } from "./getApiV1ReposOwnerN | ||
| 155 | 156 | export { route as getApiV1ReposOwnerNameAppNotes } from "./getApiV1ReposOwnerNameAppNotes.ts"; |
| 156 | 157 | export { route as getApiV1ReposOwnerNameAppProductionOpen } from "./getApiV1ReposOwnerNameAppProductionOpen.ts"; |
| 157 | 158 | export { route as getApiV1ReposOwnerNameAppPulls } from "./getApiV1ReposOwnerNameAppPulls.ts"; |
| 159 | export { route as getApiV1ReposOwnerNameAppRegistry } from "./getApiV1ReposOwnerNameAppRegistry.ts"; | |
| 158 | 160 | export { route as getApiV1ReposOwnerNameBranches } from "./getApiV1ReposOwnerNameBranches.ts"; |
| 159 | 161 | export { route as getApiV1ReposOwnerNameBuilds } from "./getApiV1ReposOwnerNameBuilds.ts"; |
| 160 | 162 | export { route as getApiV1ReposOwnerNameBuildsLabelCart } from "./getApiV1ReposOwnerNameBuildsLabelCart.ts"; |
| @@ -510,6 +512,7 @@ export { route as putApiV1PlacesPin } from "./putApiV1PlacesPin.ts"; | ||
| 510 | 512 | export { route as putApiV1ReposOwnerNameAppDomainsHostOldRecords } from "./putApiV1ReposOwnerNameAppDomainsHostOldRecords.ts"; |
| 511 | 513 | export { route as putApiV1ReposOwnerNameAppEnvVar } from "./putApiV1ReposOwnerNameAppEnvVar.ts"; |
| 512 | 514 | export { route as putApiV1ReposOwnerNameAppMembers } from "./putApiV1ReposOwnerNameAppMembers.ts"; |
| 515 | export { route as putApiV1ReposOwnerNameAppRegistryItem } from "./putApiV1ReposOwnerNameAppRegistryItem.ts"; | |
| 513 | 516 | export { route as putApiV1ReposOwnerNameCrewUsername } from "./putApiV1ReposOwnerNameCrewUsername.ts"; |
| 514 | 517 | export { route as putApiV1ReposOwnerNameDemo } from "./putApiV1ReposOwnerNameDemo.ts"; |
| 515 | 518 | export { route as putApiV1ReposOwnerNameDisplayBuilds } from "./putApiV1ReposOwnerNameDisplayBuilds.ts"; |
src/contract/openapi/routes/putApiV1ReposOwnerNameAppRegistryItem.ts added +12
| @@ -0,0 +1,12 @@ | ||
| 1 | import { REPO, type Route } from "../shared.ts"; | |
| 2 | ||
| 3 | export const route: Route = { | |
| 4 | method: "put", | |
| 5 | path: `${REPO}/app/registry/{item}`, | |
| 6 | summary: "Note that an app installed a registry item", | |
| 7 | detail: "Note that the app holds a registry item at a version (admin scope; admin of the repository); answers { items }", | |
| 8 | auth: true, | |
| 9 | role: "member", | |
| 10 | body: { version: "string" }, | |
| 11 | refuses: [401, 404], | |
| 12 | }; |
src/core/account_purge.ts +1
| @@ -70,6 +70,7 @@ export const COVERED: Readonly<Record<string, Coverage>> = { | ||
| 70 | 70 | "app_feature.absorbed_by": FORMER, |
| 71 | 71 | "app_feature_setting.set_by": FORMER, |
| 72 | 72 | "app_flag.updated_by": FORMER, |
| 73 | "app_registry_item.installed_by": FORMER, | |
| 73 | 74 | "app_vital.member": { do: "none", why: "a 0/1 mark that the visit was by a signed-in reader of the repository; it names no one" }, |
| 74 | 75 | "bet.backed_by": FORMER, |
| 75 | 76 | "check_proven.proven_by": FORMER, |
src/core/app_registry.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the component registry (#17038, a slice of #6009): one record per | |
| 3 | * item with the version the app holds, so an update is the registry's newer version against it. | |
| 4 | * Installing copies the source into the app, which then owns it; this is only the forge's note of | |
| 5 | * which item and which version. Pure data: no authz, no events. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | ||
| 10 | export type InstalledItem = { item: string; version: string; installedBy: string; installedAt: string }; | |
| 11 | ||
| 12 | /** An item's name, as shadcn names them: lower case, digits and dashes. It is a URL. */ | |
| 13 | export const ITEM_NAME = /^[a-z][a-z0-9-]{0,47}$/; | |
| 14 | /** A version: a short token such as `1`, `1.2.0` or `2026-12-01`. */ | |
| 15 | export const ITEM_VERSION = /^[0-9A-Za-z][0-9A-Za-z._+-]{0,31}$/; | |
| 16 | export const MAX_ITEMS = 200; | |
| 17 | ||
| 18 | /** Every item a repository holds, by name. */ | |
| 19 | export function installedItems(db: Database, repoId: string): InstalledItem[] { | |
| 20 | return db | |
| 21 | .query<{ item: string; version: string; installed_by: string; installed_at: string }, [string]>( | |
| 22 | "SELECT item, version, installed_by, installed_at FROM app_registry_item WHERE repo_id = ?1 ORDER BY item", | |
| 23 | ) | |
| 24 | .all(repoId) | |
| 25 | .map((r) => ({ item: r.item, version: r.version, installedBy: r.installed_by, installedAt: r.installed_at })); | |
| 26 | } | |
| 27 | ||
| 28 | /** Note that an item is at a version; installing it again at another version replaces the note. */ | |
| 29 | export function recordInstalled(db: Database, repoId: string, item: string, version: string, actorId: string): void { | |
| 30 | db.query( | |
| 31 | `INSERT INTO app_registry_item (repo_id, item, version, installed_by, installed_at) VALUES (?1, ?2, ?3, ?4, ?5) | |
| 32 | ON CONFLICT (repo_id, item) DO UPDATE SET version = excluded.version, | |
| 33 | installed_by = excluded.installed_by, installed_at = excluded.installed_at`, | |
| 34 | ).run(repoId, item, version, actorId, new Date().toISOString()); | |
| 35 | } | |
| 36 | ||
| 37 | /** Forget an item (the app removed it); true when there was a note. */ | |
| 38 | export function forgetInstalled(db: Database, repoId: string, item: string): boolean { | |
| 39 | return db.query("DELETE FROM app_registry_item WHERE repo_id = ?1 AND item = ?2").run(repoId, item).changes > 0; | |
| 40 | } |
src/core/empty_words.ts +1
| @@ -92,6 +92,7 @@ const TABLE = { | ||
| 92 | 92 | // The forge's own flags are declared in code; an app's in its repository. One sentence for both. |
| 93 | 93 | flags: { sentence: NO_FLAG }, |
| 94 | 94 | appFlags: { sentence: NO_FLAG, reason: `Add a ${FLAGS_FILE} to the repository to declare one.` }, |
| 95 | appRegistry: { sentence: "Nothing installed from the registry.", reason: "An item is noted here when it is installed." }, | |
| 95 | 96 | regValues: { sentence: "No values." }, |
| 96 | 97 | gameBoxes: { sentence: "Nothing runs here yet.", reason: "A game box is a machine on your cloud running a compose project; an admin makes one from Infra." }, |
| 97 | 98 | gameServices: { sentence: "Nothing has reported yet.", reason: "The box shows its services once its agent first syncs." }, |
src/core/migration-pins/20261201140000-app-registry-item.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 717890533dbd29b649b8ef504f3376f0bf66aabbf068e70f8afaa51fdafe7e89 |
src/core/migrations/20261201140000-app-registry-item.ts added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | // Which registry item at which version an app has installed (#17038, a slice of #6009): one row per | |
| 2 | // repository and item, so an update can be computed by comparing it with the registry's latest. | |
| 3 | // Additive: one table. | |
| 4 | export default ` | |
| 5 | CREATE TABLE app_registry_item ( | |
| 6 | repo_id TEXT NOT NULL REFERENCES repo(id) ON DELETE CASCADE, | |
| 7 | item TEXT NOT NULL, | |
| 8 | version TEXT NOT NULL, | |
| 9 | installed_by TEXT NOT NULL, | |
| 10 | installed_at TEXT NOT NULL, | |
| 11 | PRIMARY KEY (repo_id, item) | |
| 12 | ) WITHOUT ROWID; | |
| 13 | `; |
src/generated/migrations.ts +4 −2
| @@ -447,7 +447,8 @@ import m442 from "../core/migrations/20261201120000-first-change-forge.ts"; | ||
| 447 | 447 | import m443 from "../core/migrations/20261201121000-repo-check-slots.ts"; |
| 448 | 448 | import m444 from "../core/migrations/20261201130000-finding-touched.ts"; |
| 449 | 449 | import m445 from "../core/migrations/20261201130000-game-box-boot.ts"; |
| 450 | import m446 from "../core/migrations/20261201140000-scan-finding-excerpt.ts"; | |
| 450 | import m446 from "../core/migrations/20261201140000-app-registry-item.ts"; | |
| 451 | import m447 from "../core/migrations/20261201140000-scan-finding-excerpt.ts"; | |
| 451 | 452 | |
| 452 | 453 | /** Each migration file's name and its default export, in name order. */ |
| 453 | 454 | export const MIGRATION_MODULES: readonly (readonly [string, unknown])[] = [ |
| @@ -897,5 +898,6 @@ export const MIGRATION_MODULES: readonly (readonly [string, unknown])[] = [ | ||
| 897 | 898 | ["20261201121000-repo-check-slots.ts", m443], |
| 898 | 899 | ["20261201130000-finding-touched.ts", m444], |
| 899 | 900 | ["20261201130000-game-box-boot.ts", m445], |
| 900 | ["20261201140000-scan-finding-excerpt.ts", m446], | |
| 901 | ["20261201140000-app-registry-item.ts", m446], | |
| 902 | ["20261201140000-scan-finding-excerpt.ts", m447], | |
| 901 | 903 | ]; |
src/http/api.ts +2
| @@ -365,6 +365,7 @@ import { buildCartRoutes } from "./api/build_cart.ts"; | ||
| 365 | 365 | import { iterationRoutes } from "./api/iterations.ts"; |
| 366 | 366 | import { deepsecRoutes } from "./api/deepsec.ts"; |
| 367 | 367 | import { appNoteRoutes } from "./api/app_notes.ts"; |
| 368 | import { appRegistryRoutes } from "./api/app_registry.ts"; | |
| 368 | 369 | import { appChangesRoutes } from "./api/app_changes.ts"; |
| 369 | 370 | import { reportRoutes } from "./api/reports.ts"; |
| 370 | 371 | import { offerHandler, offerStatusHandler, outsiderStatusJson } from "./offer_routes.ts"; |
| @@ -2285,6 +2286,7 @@ export function apiRoutes(router: Router<Handler>, isPage: (path: string) => boo | ||
| 2285 | 2286 | router.add("GET", "/api/v1/status/:token", outsiderStatusJson); |
| 2286 | 2287 | startRoutes(router); |
| 2287 | 2288 | appNoteRoutes(router); |
| 2289 | appRegistryRoutes(router); | |
| 2288 | 2290 | appChangesRoutes(router); |
| 2289 | 2291 | rollbackRoutes(router); |
| 2290 | 2292 | takeBackRoutes(router); |
src/http/api/app_registry.ts added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the component registry (#17038), over the API: | |
| 3 | * `GET …/app/registry` lists each item and its version, `PUT …/app/registry/:item` with | |
| 4 | * `{ version }` notes an install and `DELETE …/app/registry/:item` its removal. The rules live in | |
| 5 | * `actions/app_registry.ts`. | |
| 6 | */ | |
| 7 | ||
| 8 | import { noteInstalled, noteRemoved, registryItems } from "../../actions/app_registry.ts"; | |
| 9 | import { loadRepo } from "../api.ts"; | |
| 10 | import type { Handler } from "../context.ts"; | |
| 11 | import { apiError, body, json, notFound, requireScope } from "../respond.ts"; | |
| 12 | import type { Router } from "../router.ts"; | |
| 13 | ||
| 14 | const view = (items: readonly object[]) => json({ items }); | |
| 15 | ||
| 16 | export function appRegistryRoutes(router: Router<Handler>): void { | |
| 17 | router.add("GET", "/api/v1/repos/:owner/:name/app/registry", (ctx) => { | |
| 18 | const repo = loadRepo(ctx); | |
| 19 | if (!repo) return notFound(); | |
| 20 | const got = registryItems(ctx.app, ctx.auth.user, repo); | |
| 21 | return got.ok ? view(got.value) : notFound(); | |
| 22 | }); | |
| 23 | ||
| 24 | router.add("PUT", "/api/v1/repos/:owner/:name/app/registry/:item", async (ctx) => { | |
| 25 | const denied = requireScope(ctx, "admin"); | |
| 26 | if (denied) return denied; | |
| 27 | const repo = loadRepo(ctx); | |
| 28 | if (!repo) return notFound(); | |
| 29 | const input = await body(ctx.request); | |
| 30 | const version = typeof input["version"] === "string" ? input["version"] : ""; | |
| 31 | const done = noteInstalled(ctx.app, ctx.auth.user, repo, ctx.params.item ?? "", version); | |
| 32 | if (done.ok) return view(done.value); | |
| 33 | return done.error === "not_found" ? notFound() : apiError("invalid", 422, done.error.message); | |
| 34 | }); | |
| 35 | ||
| 36 | router.add("DELETE", "/api/v1/repos/:owner/:name/app/registry/:item", (ctx) => { | |
| 37 | const denied = requireScope(ctx, "admin"); | |
| 38 | if (denied) return denied; | |
| 39 | const repo = loadRepo(ctx); | |
| 40 | if (!repo) return notFound(); | |
| 41 | const done = noteRemoved(ctx.app, ctx.auth.user, repo, ctx.params.item ?? ""); | |
| 42 | if (done.ok) return view(done.value); | |
| 43 | return done.error === "not_found" ? notFound() : apiError("invalid", 422, done.error.message); | |
| 44 | }); | |
| 45 | } |
src/http/mcp/tools/app_registry.ts added +41
| @@ -0,0 +1,41 @@ | ||
| 1 | import { noteInstalled, noteRemoved, registryItems } from "../../../actions/app_registry.ts"; | |
| 2 | import { scopeAllows } from "../../../core/tokens.ts"; | |
| 3 | import { bad, good, isAnswer, REPO_ARG, repoArg, type Tool } from "../shared.ts"; | |
| 4 | ||
| 5 | export const tool: Tool = { | |
| 6 | name: "app_registry", | |
| 7 | doc: "`{ repo, action, item?, version? }`: `list` the registry items an app has installed with their versions, `install` notes an item at a version, `remove` forgets it; noting needs an admin of the repository with an admin-scope token", | |
| 8 | description: "What an app has installed from the component registry: list, note an install or a removal. Admin to write.", | |
| 9 | inputSchema: { | |
| 10 | type: "object", | |
| 11 | properties: { | |
| 12 | repo: REPO_ARG, | |
| 13 | action: { type: "string", enum: ["list", "install", "remove"] }, | |
| 14 | item: { type: "string" }, | |
| 15 | version: { type: "string" }, | |
| 16 | }, | |
| 17 | required: ["repo", "action"], | |
| 18 | }, | |
| 19 | writes: true, | |
| 20 | readsWhen: (args) => args["action"] === "list", | |
| 21 | heldNotWhen: (args) => args["action"] === "list", | |
| 22 | run(ctx, user, args) { | |
| 23 | const action = args["action"]; | |
| 24 | if (action !== "list" && !scopeAllows(ctx.auth.scope ?? "ro", "admin")) return bad("forbidden", "noting an install needs an admin-scope token"); | |
| 25 | const repo = repoArg(ctx, user, args); | |
| 26 | if (isAnswer(repo)) return repo; | |
| 27 | const item = typeof args["item"] === "string" ? args["item"] : ""; | |
| 28 | const version = typeof args["version"] === "string" ? args["version"] : ""; | |
| 29 | const done = | |
| 30 | action === "list" | |
| 31 | ? registryItems(ctx.app, user, repo) | |
| 32 | : action === "install" | |
| 33 | ? noteInstalled(ctx.app, user, repo, item, version) | |
| 34 | : action === "remove" | |
| 35 | ? noteRemoved(ctx.app, user, repo, item) | |
| 36 | : undefined; | |
| 37 | if (done === undefined) return bad("invalid", "`action` is list, install or remove"); | |
| 38 | if (done.ok) return good({ items: done.value }); | |
| 39 | return done.error === "not_found" ? bad("not_found", "no such app or repository (owner/name)") : bad("invalid", done.error.message); | |
| 40 | }, | |
| 41 | }; |
src/http/mcp/tools/index.ts +1
| @@ -17,6 +17,7 @@ export { tool as app_env } from "./app_env.ts"; | ||
| 17 | 17 | export { tool as app_flags } from "./app_flags.ts"; |
| 18 | 18 | export { tool as app_members } from "./app_members.ts"; |
| 19 | 19 | export { tool as app_notes } from "./app_notes.ts"; |
| 20 | export { tool as app_registry } from "./app_registry.ts"; | |
| 20 | 21 | export { tool as apps } from "./apps.ts"; |
| 21 | 22 | export { tool as archive } from "./archive.ts"; |
| 22 | 23 | export { tool as asks } from "./asks.ts"; |
src/tui/screens/appregistry.ts added +122
| @@ -0,0 +1,122 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the component registry in the terminal (#17038): each item and the | |
| 3 | * version the app holds, read through `registryItems`; `i` notes an install and `x` forgets one, | |
| 4 | * through `noteInstalled` and `noteRemoved`, the same gate and words as the API and the MCP. A | |
| 5 | * write from another door reaches an open screen by the `app.registry_*` events. | |
| 6 | */ | |
| 7 | ||
| 8 | import { noteInstalled, noteRemoved, registryItems } from "../../actions/app_registry.ts"; | |
| 9 | import type { InstalledItem } from "../../core/app_registry.ts"; | |
| 10 | import { getAppByRepo } from "../../core/apps.ts"; | |
| 11 | import { emptyText } from "../../core/empty_words.ts"; | |
| 12 | import { canAdmin, type Repo } from "../../core/repos.ts"; | |
| 13 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 14 | import { binding } from "../app/keys.ts"; | |
| 15 | import type { Command } from "../app/palette.ts"; | |
| 16 | import type { Session } from "../session.ts"; | |
| 17 | import { style } from "../term/style.ts"; | |
| 18 | import { wrap } from "../term/width.ts"; | |
| 19 | import { fill, len, split } from "../ui/layout.ts"; | |
| 20 | import { ListState, confirmScreen, drawList, later, live, promptScreen, runAsync, type ListRow } from "./common.ts"; | |
| 21 | ||
| 22 | /** The palette's "Registry items", for a reader of a repository that is an app. */ | |
| 23 | export function appRegistryCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { | |
| 24 | if (repo === undefined || getAppByRepo(f.app.db, repo.id) === undefined) return []; | |
| 25 | return [ | |
| 26 | { | |
| 27 | id: "app-registry", | |
| 28 | title: `${repo.ownerName}/${repo.name} — Registry items`, | |
| 29 | hint: "installed from the registry", | |
| 30 | run: () => ctx.push(appRegistryScreen(f, ctx, repo)), | |
| 31 | }, | |
| 32 | ]; | |
| 33 | } | |
| 34 | ||
| 35 | export function appRegistryScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | |
| 36 | const state = new ListState(); | |
| 37 | let rows: InstalledItem[] = []; | |
| 38 | let readable = false; | |
| 39 | let note = ""; | |
| 40 | const reload = (): void => { | |
| 41 | const got = registryItems(f.app, f.user, repo); | |
| 42 | readable = got.ok; | |
| 43 | rows = got.ok ? got.value : []; | |
| 44 | state.clamp(rows.length); | |
| 45 | ctx.invalidate(); | |
| 46 | }; | |
| 47 | reload(); | |
| 48 | const admin = f.user !== undefined && canAdmin(f.app.db, repo, f.user); | |
| 49 | const say = (text: string): void => { | |
| 50 | note = text; | |
| 51 | reload(); | |
| 52 | }; | |
| 53 | const refused = "Noting an install is the app's admin's."; | |
| 54 | const install = (): Screen => | |
| 55 | promptScreen(ctx, screen, { | |
| 56 | title: "Note an install", | |
| 57 | label: "the item's name, like empty-state", | |
| 58 | placeholder: "empty-state", | |
| 59 | onSubmit: (item) => { | |
| 60 | if (item === "") return "An item needs a name."; | |
| 61 | return later(ctx, () => | |
| 62 | promptScreen(ctx, screen, { | |
| 63 | title: item, | |
| 64 | label: "the version, like 1.2.0", | |
| 65 | placeholder: "1.0.0", | |
| 66 | onSubmit: async (version) => { | |
| 67 | const done = noteInstalled(f.app, f.user, repo, item, version); | |
| 68 | say(done.ok ? `${item} is noted at ${version}.` : done.error === "not_found" ? refused : done.error.message); | |
| 69 | }, | |
| 70 | }), | |
| 71 | ); | |
| 72 | }, | |
| 73 | }); | |
| 74 | const forget = (item: string): Screen => | |
| 75 | confirmScreen(ctx, screen, { | |
| 76 | title: "Forget this item", | |
| 77 | body: [item, "Only the note goes; the app's files stay."], | |
| 78 | actions: [{ label: "Keep", variant: "ghost" }, { label: "Forget", variant: "danger" }], | |
| 79 | onChoose: (i) => { | |
| 80 | if (i !== 1) return; | |
| 81 | runAsync(ctx, async () => { | |
| 82 | const done = noteRemoved(f.app, f.user, repo, item); | |
| 83 | say(done.ok ? `${item} is forgotten.` : done.error === "not_found" ? refused : done.error.message); | |
| 84 | }); | |
| 85 | }, | |
| 86 | }); | |
| 87 | const screen: Screen = { | |
| 88 | title: "Registry items", | |
| 89 | get keys() { | |
| 90 | return admin | |
| 91 | ? [binding(["i"], "note an install"), binding(["x"], "forget the item"), binding(["j", "k"], "move cursor")] | |
| 92 | : [binding(["j", "k"], "move cursor")]; | |
| 93 | }, | |
| 94 | async load(signal) { | |
| 95 | live(f, signal, { repoId: repo.id, where: (e) => e.kind === "app.registry_installed" || e.kind === "app.registry_removed" }, reload); | |
| 96 | }, | |
| 97 | render(area, s) { | |
| 98 | const c = ctx.theme.c; | |
| 99 | const text = note !== "" ? note : !readable ? "No such app." : rows.length === 0 ? emptyText("appRegistry") : `${rows.length} for ${repo.ownerName}/${repo.name}.`; | |
| 100 | const said = wrap(text, Math.max(1, area.w)).slice(0, 3); | |
| 101 | const [head, list] = split(area, "v", [len(said.length), fill(1)]); | |
| 102 | said.forEach((line, i) => s.text(head!.x, head!.y + i, line, style(c.mutedFg, c.bg), head!.x + head!.w)); | |
| 103 | const lines: ListRow[] = rows.map((r) => ({ title: r.item, meta: `version ${r.version} · ${r.installedAt.slice(0, 10)}` })); | |
| 104 | drawList(s, list!, c, state, lines); | |
| 105 | }, | |
| 106 | key(e) { | |
| 107 | if (state.key(e, rows.length)) return true; | |
| 108 | if (e.ctrl || e.alt || !admin || !readable) return false; | |
| 109 | if (e.key === "i") { | |
| 110 | ctx.push(install()); | |
| 111 | return true; | |
| 112 | } | |
| 113 | const row = rows[state.index]; | |
| 114 | if (e.key === "x" && row !== undefined) { | |
| 115 | ctx.push(forget(row.item)); | |
| 116 | return true; | |
| 117 | } | |
| 118 | return false; | |
| 119 | }, | |
| 120 | }; | |
| 121 | return screen; | |
| 122 | } |
src/tui/screens/workspace.ts +2
| @@ -212,6 +212,7 @@ import { labelsCommands } from "./labels.ts"; | ||
| 212 | 212 | import { appEnvCommands } from "./appenv.ts"; |
| 213 | 213 | import { appSecretCommands } from "./appsecrets.ts"; |
| 214 | 214 | import { appFlagCommands } from "./appflags.ts"; |
| 215 | import { appRegistryCommands } from "./appregistry.ts"; | |
| 215 | 216 | import { changesCommands } from "./app_changes.ts"; |
| 216 | 217 | import { appBackupCommands } from "./appbackup.ts"; |
| 217 | 218 | import { variantsCommands } from "./variants.ts"; |
| @@ -2451,6 +2452,7 @@ export function workspaceScreen( | ||
| 2451 | 2452 | ...appEnvCommands(f, ctx, repo), |
| 2452 | 2453 | ...appSecretCommands(f, ctx, repo), |
| 2453 | 2454 | ...appFlagCommands(f, ctx, repo), |
| 2455 | ...appRegistryCommands(f, ctx, repo), | |
| 2454 | 2456 | ...changesCommands(f, ctx, repo), |
| 2455 | 2457 | ...appBackupCommands(f, ctx, repo), |
| 2456 | 2458 | ...variantsCommands(f, ctx, repo), |
test/app-registry-doors.test.ts added +207
| @@ -0,0 +1,207 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the registry (#17038), the doors the first test file leaves out: | |
| 3 | * the MCP tool and its scope gate, the API's scope gate, the 200-item cap, the events, and the | |
| 4 | * terminal screen for an admin, a reader and a stranger. | |
| 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 { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { noteInstalled, noteRemoved } from "../src/actions/app_registry.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { installedItems, MAX_ITEMS, recordInstalled } from "../src/core/app_registry.ts"; | |
| 14 | import type { Repo } from "../src/core/repos.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 19 | import { appRegistryScreen } from "../src/tui/screens/appregistry.ts"; | |
| 20 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 21 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 22 | import { quietLogs } from "./helpers/globals.ts"; | |
| 23 | import { pollUntil } from "./helpers/poll.ts"; | |
| 24 | import { settleApp } from "./helpers/tui.ts"; | |
| 25 | ||
| 26 | quietLogs(); | |
| 27 | ||
| 28 | describe("the registry record's other doors", () => { | |
| 29 | let dir = ""; | |
| 30 | let app: App; | |
| 31 | let servers: Servers; | |
| 32 | let alice: User; | |
| 33 | let bob: User; | |
| 34 | let carol: User; | |
| 35 | let repo: Repo; | |
| 36 | let pub: Repo; | |
| 37 | const tokens: Record<string, string> = {}; | |
| 38 | ||
| 39 | beforeAll(async () => { | |
| 40 | dir = await mkdtemp(join(tmpdir(), "forge-app-registry-doors-")); | |
| 41 | app = createApp({ dataDir: dir, port: 0 }); | |
| 42 | servers = await serve(app); | |
| 43 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 44 | if (!a.ok) throw new Error(a.error); | |
| 45 | alice = a.value; | |
| 46 | const mk = async (username: string): Promise<User> => { | |
| 47 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.test`, password: "a-good-passphrase" }); | |
| 48 | if (!r.ok) throw new Error(r.error); | |
| 49 | return r.value; | |
| 50 | }; | |
| 51 | bob = await mk("bob"); | |
| 52 | carol = await mk("carol"); | |
| 53 | const made = await newApp(app, alice, { name: "ledger" }); | |
| 54 | if (!made.ok) throw new Error(made.error); | |
| 55 | repo = made.value.repo; | |
| 56 | const open = await newApp(app, alice, { name: "gallery", visibility: "public" }); | |
| 57 | if (!open.ok) throw new Error(open.error); | |
| 58 | pub = open.value.repo; | |
| 59 | tokens["admin"] = createToken(app.db, alice.id, "t", "admin").token; | |
| 60 | tokens["rw"] = createToken(app.db, alice.id, "t", "rw").token; | |
| 61 | tokens["ro"] = createToken(app.db, alice.id, "t", "ro").token; | |
| 62 | }); | |
| 63 | afterAll(async () => { | |
| 64 | servers.stop(); | |
| 65 | app.close(); | |
| 66 | await rm(dir, { recursive: true, force: true }); | |
| 67 | }); | |
| 68 | ||
| 69 | const events = (kind: string): { item: string; version?: string }[] => | |
| 70 | app.db | |
| 71 | .query<{ payload: string }, [string]>("SELECT payload FROM event WHERE kind = ?1 ORDER BY seq") | |
| 72 | .all(kind) | |
| 73 | .map((e) => JSON.parse(e.payload)); | |
| 74 | ||
| 75 | const rpc = async (token: string, args: Record<string, unknown>): Promise<{ error: boolean; body: any }> => { | |
| 76 | const res = await fetch(`${servers.url}/mcp`, { | |
| 77 | method: "POST", | |
| 78 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, | |
| 79 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "app_registry", arguments: { repo: "alice/ledger", ...args } } }), | |
| 80 | }); | |
| 81 | const out: any = await res.json(); | |
| 82 | const text = String(out.result.content[0].text); | |
| 83 | let body: unknown = text; | |
| 84 | try { body = JSON.parse(text); } catch { /* a refusal is words */ } | |
| 85 | return { error: out.result.isError === true, body }; | |
| 86 | }; | |
| 87 | ||
| 88 | const api = (token: string, method: string, path: string, body?: object): Promise<Response> => | |
| 89 | fetch(`${servers.url}/api/v1/repos/alice/ledger/app/registry${path}`, { | |
| 90 | method, | |
| 91 | headers: { authorization: `Bearer ${token}`, ...(body ? { "content-type": "application/json" } : {}) }, | |
| 92 | ...(body ? { body: JSON.stringify(body) } : {}), | |
| 93 | }); | |
| 94 | ||
| 95 | test("the MCP tool lists, installs and removes for an admin-scope token; other scopes are refused", async () => { | |
| 96 | const put = await rpc(tokens["admin"]!, { action: "install", item: "empty-state", version: "1.0.0" }); | |
| 97 | expect(put.error).toBe(false); | |
| 98 | expect(put.body.items.map((i: { item: string }) => i.item)).toEqual(["empty-state"]); | |
| 99 | const list = await rpc(tokens["ro"]!, { action: "list" }); | |
| 100 | expect(list.error, JSON.stringify(list.body)).toBe(false); | |
| 101 | expect(list.body.items[0].version).toBe("1.0.0"); | |
| 102 | expect((await rpc(tokens["rw"]!, { action: "install", item: "button", version: "1" })).error).toBe(true); | |
| 103 | expect((await rpc(tokens["ro"]!, { action: "remove", item: "empty-state" })).error).toBe(true); | |
| 104 | expect(installedItems(app.db, repo.id).map((i) => i.item)).toEqual(["empty-state"]); | |
| 105 | expect((await rpc(tokens["admin"]!, { action: "install", item: "Bad Name", version: "1" })).error).toBe(true); | |
| 106 | const gone = await rpc(tokens["admin"]!, { action: "remove", item: "empty-state" }); | |
| 107 | expect(gone.error).toBe(false); | |
| 108 | expect(gone.body.items).toEqual([]); | |
| 109 | expect((await rpc(tokens["admin"]!, { action: "remove", item: "empty-state" })).error).toBe(true); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a read-scope token is refused 403 on the API's PUT and DELETE, and can still list", async () => { | |
| 113 | expect((await api(tokens["ro"]!, "PUT", "/button", { version: "1" })).status).toBe(403); | |
| 114 | expect((await api(tokens["ro"]!, "DELETE", "/button")).status).toBe(403); | |
| 115 | expect((await api(tokens["rw"]!, "PUT", "/button", { version: "1" })).status).toBe(403); | |
| 116 | expect(installedItems(app.db, repo.id)).toEqual([]); | |
| 117 | expect((await api(tokens["ro"]!, "GET", "")).status).toBe(200); | |
| 118 | }); | |
| 119 | ||
| 120 | test("the 201st distinct item is refused, and noting an existing one again still works", () => { | |
| 121 | for (let i = 0; i < MAX_ITEMS; i++) recordInstalled(app.db, repo.id, `item-${i}`, "1", alice.id); | |
| 122 | const over = noteInstalled(app, alice, repo, "one-too-many", "1"); | |
| 123 | expect(over.ok).toBe(false); | |
| 124 | expect(over.ok === false && typeof over.error === "object" ? over.error.message : "").toContain("at most 200"); | |
| 125 | expect(installedItems(app.db, repo.id)).toHaveLength(MAX_ITEMS); | |
| 126 | const again = noteInstalled(app, alice, repo, "item-7", "2.0.0"); | |
| 127 | expect(again.ok).toBe(true); | |
| 128 | expect(installedItems(app.db, repo.id).find((i) => i.item === "item-7")?.version).toBe("2.0.0"); | |
| 129 | app.db.query("DELETE FROM app_registry_item WHERE repo_id = ?1").run(repo.id); | |
| 130 | }); | |
| 131 | ||
| 132 | test("the version error names every character the pattern allows", () => { | |
| 133 | const bad = noteInstalled(app, alice, repo, "button", "no spaces"); | |
| 134 | const said = bad.ok === false && typeof bad.error === "object" ? bad.error.message : ""; | |
| 135 | expect(said).toContain("plus signs"); | |
| 136 | expect(said).toContain("underscores"); | |
| 137 | expect(noteInstalled(app, alice, repo, "button", "1.0.0-rc_1+build").ok).toBe(true); | |
| 138 | noteRemoved(app, alice, repo, "button"); | |
| 139 | }); | |
| 140 | ||
| 141 | test("a note and a forgetting each append their event, and a refusal appends none", () => { | |
| 142 | const before = [events("app.registry_installed").length, events("app.registry_removed").length]; | |
| 143 | noteInstalled(app, alice, repo, "tokens", "3"); | |
| 144 | noteInstalled(app, bob, repo, "tokens", "4"); | |
| 145 | noteRemoved(app, alice, repo, "tokens"); | |
| 146 | noteRemoved(app, alice, repo, "tokens"); | |
| 147 | expect(events("app.registry_installed").slice(before[0]!)).toEqual([{ item: "tokens", version: "3" }]); | |
| 148 | expect(events("app.registry_removed").slice(before[1]!)).toEqual([{ item: "tokens" }]); | |
| 149 | }); | |
| 150 | ||
| 151 | const open = async (user: User, keys: string[], between?: () => void, at: Repo = repo, until?: string): Promise<string> => { | |
| 152 | const terminal = headlessTerminal(120, 30); | |
| 153 | const { app: tui, session } = createForgeApp( | |
| 154 | app, | |
| 155 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 156 | { terminal, exitHooks: false }, | |
| 157 | ); | |
| 158 | tui.start(); | |
| 159 | tui.ctx.push(appRegistryScreen(session, tui.ctx, at)); | |
| 160 | await settleApp(tui); | |
| 161 | between?.(); | |
| 162 | for (const k of keys) { | |
| 163 | terminal.feed(k); | |
| 164 | await settleApp(tui); | |
| 165 | } | |
| 166 | if (until !== undefined) await pollUntil(() => tui.screen.toText().includes(until), `the screen says ${until}`, 5000); | |
| 167 | const text = tui.screen.toText(); | |
| 168 | tui.stop?.(); | |
| 169 | return text; | |
| 170 | }; | |
| 171 | ||
| 172 | test("the terminal shows the items to a reader and 'No such app.' to a stranger", async () => { | |
| 173 | recordInstalled(app.db, repo.id, "empty-state", "1.2.0", alice.id); | |
| 174 | recordInstalled(app.db, pub.id, "empty-state", "1.2.0", alice.id); | |
| 175 | const reader = await open(carol, [], undefined, pub); | |
| 176 | expect(reader).toContain("empty-state"); | |
| 177 | expect(reader).toContain("version 1.2.0"); | |
| 178 | expect(await open(bob, [])).toContain("No such app."); | |
| 179 | expect(await open(bob, [])).not.toContain("empty-state"); | |
| 180 | app.db.query("DELETE FROM app_registry_item WHERE repo_id = ?1").run(repo.id); | |
| 181 | }); | |
| 182 | ||
| 183 | test("the terminal's admin notes an install with i and forgets it with x; a reader's keys do nothing", async () => { | |
| 184 | const noted = await open(alice, ["i", "badge", "\r", "2.1.0", "\r"]); | |
| 185 | expect(noted).toContain("badge is noted at 2.1.0."); | |
| 186 | expect(installedItems(app.db, repo.id).map((i) => [i.item, i.version])).toEqual([["badge", "2.1.0"]]); | |
| 187 | await open(carol, ["i", "chip", "\r", "1", "\r", "x", "l", "\r"], undefined, pub); | |
| 188 | expect(installedItems(app.db, pub.id).map((i) => i.item)).toEqual(["empty-state"]); | |
| 189 | const forgot = await open(alice, ["x", "l", "\r"]); | |
| 190 | expect(forgot).toContain("badge is forgotten."); | |
| 191 | expect(installedItems(app.db, repo.id)).toEqual([]); | |
| 192 | }); | |
| 193 | ||
| 194 | test("a write from another door reaches an open terminal", async () => { | |
| 195 | const text = await open( | |
| 196 | alice, | |
| 197 | [], | |
| 198 | () => { | |
| 199 | noteInstalled(app, alice, repo, "from-elsewhere", "9"); | |
| 200 | }, | |
| 201 | repo, | |
| 202 | "from-elsewhere", | |
| 203 | ); | |
| 204 | expect(text).toContain("from-elsewhere"); | |
| 205 | noteRemoved(app, alice, repo, "from-elsewhere"); | |
| 206 | }); | |
| 207 | }); |
test/app-registry.test.ts added +100
| @@ -0,0 +1,100 @@ | ||
| 1 | /** | |
| 2 | * What an app has installed from the component registry (#17038): the records, and the three | |
| 3 | * doors over one gate: an admin notes an install, a reader lists, a stranger is told nothing. | |
| 4 | */ | |
| 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 { newApp } from "../src/actions/apps.ts"; | |
| 12 | import { noteInstalled, noteRemoved, registryItems } from "../src/actions/app_registry.ts"; | |
| 13 | import { forgetInstalled, installedItems, recordInstalled } from "../src/core/app_registry.ts"; | |
| 14 | import type { Repo } from "../src/core/repos.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | describe("what an app has installed from the registry", () => { | |
| 24 | let dataDir = ""; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let alice: User; | |
| 28 | let bob: User; | |
| 29 | let repo: Repo; | |
| 30 | let aliceToken = ""; | |
| 31 | let bobToken = ""; | |
| 32 | ||
| 33 | beforeAll(async () => { | |
| 34 | dataDir = await mkdtemp(join(tmpdir(), "forge-app-registry-")); | |
| 35 | app = createApp({ dataDir, port: 0 }); | |
| 36 | servers = await serve(app); | |
| 37 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 38 | if (!a.ok) throw new Error(a.error); | |
| 39 | alice = a.value; | |
| 40 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 41 | if (!b.ok) throw new Error(b.error); | |
| 42 | bob = b.value; | |
| 43 | const made = await newApp(app, alice, { name: "budgeting" }); | |
| 44 | if (!made.ok) throw new Error(made.error); | |
| 45 | repo = made.value.repo; | |
| 46 | aliceToken = createToken(app.db, alice.id, "t", "admin").token; | |
| 47 | bobToken = createToken(app.db, bob.id, "t", "admin").token; | |
| 48 | }); | |
| 49 | afterAll(async () => { | |
| 50 | servers.stop(); | |
| 51 | app.close(); | |
| 52 | await rm(dataDir, { recursive: true, force: true }); | |
| 53 | }); | |
| 54 | ||
| 55 | const api = (token: string, method: string, path: string, body?: object): Promise<Response> => | |
| 56 | fetch(`${servers.url}/api/v1/repos/alice/budgeting/app/registry${path}`, { | |
| 57 | method, | |
| 58 | headers: { authorization: `Bearer ${token}`, ...(body ? { "content-type": "application/json" } : {}) }, | |
| 59 | ...(body ? { body: JSON.stringify(body) } : {}), | |
| 60 | }); | |
| 61 | ||
| 62 | test("a record is kept, replaced by a newer version, and forgotten", () => { | |
| 63 | recordInstalled(app.db, repo.id, "button", "1.0.0", alice.id); | |
| 64 | recordInstalled(app.db, repo.id, "tokens", "2", alice.id); | |
| 65 | recordInstalled(app.db, repo.id, "button", "1.1.0", bob.id); | |
| 66 | expect(installedItems(app.db, repo.id).map((h) => [h.item, h.version, h.installedBy])).toEqual([ | |
| 67 | ["button", "1.1.0", bob.id], | |
| 68 | ["tokens", "2", alice.id], | |
| 69 | ]); | |
| 70 | expect(forgetInstalled(app.db, repo.id, "button")).toBe(true); | |
| 71 | expect(forgetInstalled(app.db, repo.id, "button")).toBe(false); | |
| 72 | forgetInstalled(app.db, repo.id, "tokens"); | |
| 73 | }); | |
| 74 | ||
| 75 | test("the admin notes an install over the API and it lists; a bad name or version is refused", async () => { | |
| 76 | const put = await api(aliceToken, "PUT", "/empty-state", { version: "1.2.0" }); | |
| 77 | expect(put.status).toBe(200); | |
| 78 | const listed = (await (await api(aliceToken, "GET", "")).json()) as { items: { item: string; version: string }[] }; | |
| 79 | expect(listed.items.map((i) => [i.item, i.version])).toEqual([["empty-state", "1.2.0"]]); | |
| 80 | expect((await api(aliceToken, "PUT", "/Bad_Name", { version: "1" })).status).toBe(422); | |
| 81 | expect((await api(aliceToken, "PUT", "/button", { version: "../x" })).status).toBe(422); | |
| 82 | expect((await api(aliceToken, "PUT", "/button", {})).status).toBe(422); | |
| 83 | expect(installedItems(app.db, repo.id)).toHaveLength(1); | |
| 84 | }); | |
| 85 | ||
| 86 | test("a stranger notes nothing and is told nothing", async () => { | |
| 87 | expect((await api(bobToken, "PUT", "/button", { version: "1" })).status).toBe(404); | |
| 88 | expect((await api(bobToken, "DELETE", "/empty-state")).status).toBe(404); | |
| 89 | expect(installedItems(app.db, repo.id).map((i) => i.item)).toEqual(["empty-state"]); | |
| 90 | expect(noteInstalled(app, bob, repo, "button", "1")).toEqual({ ok: false, error: "not_found" }); | |
| 91 | }); | |
| 92 | ||
| 93 | test("the action lists for a reader, and removing what is not there says so", async () => { | |
| 94 | expect(registryItems(app, alice, repo).ok).toBe(true); | |
| 95 | const gone = noteRemoved(app, alice, repo, "nothing"); | |
| 96 | expect(gone.ok).toBe(false); | |
| 97 | expect((await api(aliceToken, "DELETE", "/empty-state")).status).toBe(200); | |
| 98 | expect(installedItems(app.db, repo.id)).toEqual([]); | |
| 99 | }); | |
| 100 | }); |
test/conversation-confirm.test.ts +1 −1
| @@ -123,7 +123,7 @@ describe("the one list of what asks first", () => { | ||
| 123 | 123 | // settings and can be put back (`system` previews and does only on its own `confirm:true`). A new |
| 124 | 124 | // `writes: true` tool is on neither list until someone decides. |
| 125 | 125 | const NOT_ASKED = new Set([ |
| 126 | "access", "account_requests", "app_flags", "apps", "account_verify", "app_members", "app_notes", "asks_act", "asks_decide", "asks_proposal", "asks_undecide", "atlas", | |
| 126 | "access", "account_requests", "app_flags", "app_registry", "apps", "account_verify", "app_members", "app_notes", "asks_act", "asks_decide", "asks_proposal", "asks_undecide", "atlas", | |
| 127 | 127 | "asks", "assign", "attach_look", "bet", "board_pins", "board_write", "branches", "capacity", "changes", "valves", "cloud_accounts", "close", "confirm_wait", |
| 128 | 128 | "crew", "decisions_by", "desk", "detach", "economics", "edit", "email_verify", "estimate", "feed_chips", "feed_dismiss", "feed_like", "flag_set", "flag_winner", |
| 129 | 129 | "follow_mode", "forge", "fork", "front_page", "hold", "issue_teams", "keep_plan", "knobs", "labels", "lift", "limb", "maintenance", "make_it_yours", "map", "merge", "new_app", |
test/delete-tier.test.ts +1
| @@ -130,6 +130,7 @@ const SPARED: Readonly<Record<string, string>> = { | ||
| 130 | 130 | egress_grants: "a revoked grant is granted again by the same tool; no data goes with it", |
| 131 | 131 | invites: "a revoked invite is an unused link; a new one is minted in its place", |
| 132 | 132 | invite: "a contributor taken off is let in again with the same tool; their work, pulls and account are untouched", |
| 133 | app_registry: "a removal forgets a note of what was installed; noting the install again puts it back, and the app's files are untouched", | |
| 133 | 134 | sever: "it unlinks and, as it says, deletes nothing: both sides keep all they hold", |
| 134 | 135 | }; |
| 135 | 136 |
test/mcp-forge-protocol.test.ts +1
test/mixed-actions-held.test.ts +1
| @@ -53,6 +53,7 @@ const MIXED: Readonly< | ||
| 53 | 53 | pinned_forge: { key: "verb", reads: [{ verb: "list" }] }, |
| 54 | 54 | proposal_tokens: { key: "verb", reads: [{ verb: "list" }] }, |
| 55 | 55 | app_flags: { key: "action", reads: [{ repo: "o/r", action: "list" }] }, |
| 56 | app_registry: { key: "action", reads: [{ repo: "o/r", action: "list" }] }, | |
| 56 | 57 | app_env: { key: "action", reads: [{ repo: "o/r", action: "list" }] }, |
| 57 | 58 | crew_devices: { key: "action", reads: [{ repo: "o/r", action: "list" }] }, |
| 58 | 59 | drop: { key: "action", reads: [{ repo: "o/r", action: "list" }] }, |
test/route-timing-coverage.test.ts +1
| @@ -27,6 +27,7 @@ const UNTIMED: Record<string, string> = { | ||
| 27 | 27 | "/api/v1/templates": "the marked repositories, one indexed read; no page to phase", |
| 28 | 28 | "/api/v1/repos/:owner/:name/drop": "an app's drop listing, one indexed read and a directory scan; no page to phase", |
| 29 | 29 | "/api/v1/repos/:owner/:name/crew-devices": "a repository admin's device list, one indexed read; no page to phase", |
| 30 | "/api/v1/repos/:owner/:name/app/registry": "an app's installed registry items, one indexed read; no page to phase", | |
| 30 | 31 | "/api/v1/admin/plans": "an admin's plans with the measured median, two indexed reads; no page to phase", |
| 31 | 32 | "/metrics": "Prometheus text exposition, scraped by token; not a page read", |
| 32 | 33 | }; |
new above
offline
The message omits + and _, which the regex and the docs allow. It also needs a test for the cap on line 38 and for the events on line 40.