Public: anyone can read this; the crew can change it.
Move in: bring all of GitHub and Vercel into the forge through one trusted flow (inventory → plan → paced copy → verify), read-only toward the sources; clean-up is a separate, later step #18000
Merged 15h 5m ago, live in production
scout/forge:scout/6007-move-in-bring-all-of-github-and-vercel-i → main 0 commits fork 4 threads · 4 unresolved
9 files
changed +444
−16
docs/features/bring.md +20 −1
| @@ -53,6 +53,25 @@ never mistaken for an empty account. At most 40 repositories are read per visit, | ||
| 53 | 53 | most recently pushed first; the rest say "Not read yet" and are read on a later |
| 54 | 54 | visit. What was read is kept until the repository is pushed. |
| 55 | 55 | |
| 56 | - Terminal: the palette's "What is left to bring over"; `b` brings the row in, | |
| 56 | Move in, first step (#6007), read-only toward the source: each row also shows | |
| 57 | its **size** and **last push** as the origin states them (history included), | |
| 58 | whether it is a fork, and a **suggested** default (in the row's line) the owner may override: forks | |
| 59 | *link only*, repositories untouched over a year *bring in archived*, the rest | |
| 60 | *bring in*. The filter row keeps forks, stale (over a year), large (over | |
| 61 | 100 MiB) or apps; rows not read yet are not apps to it, and it says how many. A line above the list adds up what the default plan would | |
| 62 | copy (forks are linked, not copied) against this forge's free disk and says | |
| 63 | **Refused** when it would leave less than the 15% the forge keeps for itself | |
| 64 | (`src/spawn/disk_floor.ts`); a disk it cannot read says "Disk size unknown, | |
| 65 | nothing checked", and an origin that did not answer is named as not counted. **Export** is the same table as CSV or JSON | |
| 66 | (`?format=csv|json`, with the filter applied): secret names only, never values; a `scanned` | |
| 67 | column is false for a row not read yet, whose app, domains and secrets are | |
| 68 | unknown, not empty. Code: | |
| 69 | `src/origins/move_plan.ts`; test: `test/move-plan.test.ts`. Not yet: the Vercel | |
| 70 | side, the owner's per-row choice, the paced copy and the verify. | |
| 71 | ||
| 72 | - Terminal: the palette's "What is left to bring over"; each row's line carries | |
| 73 | the size, last push and suggested default, and the header line the room check; | |
| 74 | `f` cycles the filter (all, forks, stale, large, apps), `c` copies the shown | |
| 75 | rows as the CSV to your clipboard; `b` brings the row in, | |
| 57 | 76 | `n` starts a new forge, `R` reads again. |
| 58 | 77 | - MCP: the `origins` tool, `action: inventory`, `id` an origin: the same rows. |
src/actions/inventory.ts +13
| @@ -11,6 +11,8 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 11 | 11 | import { providerForOrigin } from "../origins/credential.ts"; |
| 12 | 12 | import { inventoryRows, type InventoryRow } from "../origins/inventory.ts"; |
| 13 | 13 | import { slugFor } from "../core/apps.ts"; |
| 14 | import { readDisk } from "../kernel/disk_reading.ts"; | |
| 15 | import { copyBytes, roomFor, type Room } from "../origins/move_plan.ts"; | |
| 14 | 16 | import { listRemoteRepos } from "./origins.ts"; |
| 15 | 17 | import { requireUser } from "./access.ts"; |
| 16 | 18 | |
| @@ -70,3 +72,14 @@ export async function inventoryOfOrigins( | ||
| 70 | 72 | unreachable: origins.filter((_, i) => !read[i]!.ok).map((o) => o.login), |
| 71 | 73 | }; |
| 72 | 74 | } |
| 75 | ||
| 76 | /** Whether the default plan for these rows fits this forge's disk and leaves its floor (#6007). */ | |
| 77 | export function moveRoom(app: App, items: readonly InventoryItem[], now: number): Room { | |
| 78 | let disk; | |
| 79 | try { | |
| 80 | disk = (app.config.disk ?? readDisk)(app.config.dataDir); | |
| 81 | } catch { | |
| 82 | disk = undefined; | |
| 83 | } | |
| 84 | return roomFor(copyBytes(items, now), disk); | |
| 85 | } |
src/origins/inventory.ts +7
| @@ -46,6 +46,10 @@ export interface InventoryRow extends Detected { | ||
| 46 | 46 | readonly fullName: string; |
| 47 | 47 | readonly htmlUrl: string; |
| 48 | 48 | readonly private: boolean; |
| 49 | readonly fork: boolean; | |
| 50 | /** As the origin states them: last push (ISO) and size in KiB, history included. */ | |
| 51 | readonly pushedAt: string | null; | |
| 52 | readonly sizeKb: number; | |
| 49 | 53 | /** False when the cap on reads per request left this one for the next. */ |
| 50 | 54 | readonly scanned: boolean; |
| 51 | 55 | readonly state: MigrationState; |
| @@ -157,6 +161,9 @@ export async function inventoryRows( | ||
| 157 | 161 | fullName: r.fullName, |
| 158 | 162 | htmlUrl: r.htmlUrl, |
| 159 | 163 | private: r.private, |
| 164 | fork: r.fork, | |
| 165 | pushedAt: r.pushedAt, | |
| 166 | sizeKb: r.sizeKb, | |
| 160 | 167 | scanned: seen !== undefined, |
| 161 | 168 | ...stateOf(db, user, r), |
| 162 | 169 | }; |
src/origins/move_plan.ts added +154
| @@ -0,0 +1,154 @@ | ||
| 1 | /** | |
| 2 | * The first look at a move in (#6007): what each inventory row would be by | |
| 3 | * default, which rows a filter keeps, whether the copy fits the disk, and | |
| 4 | * the table as a file. Plain rows in, plain data out; reads nothing and | |
| 5 | * writes nothing, toward the source or here. | |
| 6 | * | |
| 7 | * bring full history, every branch and tag | |
| 8 | * archived stale (untouched over a year): brought in, kept archived | |
| 9 | * link a fork: a pointer to its upstream, no copy | |
| 10 | * | |
| 11 | * The room check is the forge's own floor (`spawn/disk_floor.ts`): a plan | |
| 12 | * whose size would leave less than `DISK_FLOOR` of the volume free is refused. | |
| 13 | */ | |
| 14 | ||
| 15 | import type { InventoryRow } from "./inventory.ts"; | |
| 16 | import type { DiskReading } from "../kernel/disk_reading.ts"; | |
| 17 | import { DISK_FLOOR } from "../spawn/disk_floor.ts"; | |
| 18 | import { toCsv } from "../text/csv_write.ts"; | |
| 19 | ||
| 20 | export const SHOWS = ["all", "forks", "stale", "large", "app"] as const; | |
| 21 | type Show = (typeof SHOWS)[number]; | |
| 22 | ||
| 23 | export const SHOW_WORDS: Readonly<Record<Show, string>> = { | |
| 24 | all: "All", | |
| 25 | forks: "Forks", | |
| 26 | stale: "Stale", | |
| 27 | large: "Large", | |
| 28 | app: "Apps", | |
| 29 | }; | |
| 30 | ||
| 31 | export const FORMATS = ["json", "csv"] as const; | |
| 32 | export type Format = (typeof FORMATS)[number]; | |
| 33 | ||
| 34 | export type SuggestedAction = "bring" | "archived" | "link"; | |
| 35 | ||
| 36 | export const ACTION_WORDS: Readonly<Record<SuggestedAction, string>> = { | |
| 37 | bring: "Bring in", | |
| 38 | archived: "Bring in archived", | |
| 39 | link: "Link only", | |
| 40 | }; | |
| 41 | ||
| 42 | const STALE_MS = 365 * 24 * 3600 * 1000; | |
| 43 | const LARGE_KIB = 100 * 1024; | |
| 44 | ||
| 45 | /** A query value that is one of `list`, else the first. */ | |
| 46 | export const pick = <T extends string>(list: readonly T[], raw: string | null | undefined): T => | |
| 47 | list.find((x) => x === raw) ?? list[0]!; | |
| 48 | ||
| 49 | const isStale = (row: Pick<InventoryRow, "pushedAt">, now: number): boolean => { | |
| 50 | const at = row.pushedAt === null ? Number.NaN : Date.parse(row.pushedAt); | |
| 51 | return Number.isFinite(at) && now - at > STALE_MS; | |
| 52 | }; | |
| 53 | ||
| 54 | const isLarge = (row: Pick<InventoryRow, "sizeKb">): boolean => row.sizeKb > LARGE_KIB; | |
| 55 | ||
| 56 | /** The default for a row, which the owner may override: forks are linked, stale ones archived. */ | |
| 57 | export function suggest(row: Pick<InventoryRow, "fork" | "pushedAt">, now: number): SuggestedAction { | |
| 58 | return row.fork ? "link" : isStale(row, now) ? "archived" : "bring"; | |
| 59 | } | |
| 60 | ||
| 61 | export function filterRows<T extends InventoryRow>(rows: readonly T[], show: Show, now: number): T[] { | |
| 62 | switch (show) { | |
| 63 | case "forks": | |
| 64 | return rows.filter((r) => r.fork); | |
| 65 | case "stale": | |
| 66 | return rows.filter((r) => isStale(r, now)); | |
| 67 | case "large": | |
| 68 | return rows.filter(isLarge); | |
| 69 | case "app": | |
| 70 | return rows.filter((r) => r.app !== null); | |
| 71 | default: | |
| 72 | return [...rows]; | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | export interface Room { | |
| 77 | /** What the copies would take, in bytes (the origin's own size, which counts the history). */ | |
| 78 | readonly needBytes: number; | |
| 79 | readonly freeBytes: number; | |
| 80 | /** Free share of the volume after the copy; null when the volume cannot be read. */ | |
| 81 | readonly afterShare: number | null; | |
| 82 | /** Whether the volume could be read; when not, nothing was checked. */ | |
| 83 | readonly known: boolean; | |
| 84 | /** One sentence when the plan would take the disk past its floor, else undefined. */ | |
| 85 | readonly refusal: string | undefined; | |
| 86 | } | |
| 87 | ||
| 88 | const gib = (bytes: number): string => (bytes / 1024 ** 3).toFixed(1); | |
| 89 | ||
| 90 | /** The bytes a default plan copies: everything except forks, which are linked. */ | |
| 91 | export const copyBytes = (rows: readonly InventoryRow[], now: number): number => | |
| 92 | rows.reduce((sum, r) => sum + (suggest(r, now) === "link" ? 0 : Math.max(0, r.sizeKb) * 1024), 0); | |
| 93 | ||
| 94 | /** | |
| 95 | * Whether `needBytes` fits on `disk` and leaves the floor free; a disk that cannot be read refuses | |
| 96 | * nothing. | |
| 97 | */ | |
| 98 | export function roomFor(needBytes: number, disk: DiskReading | undefined): Room { | |
| 99 | if (disk === undefined || !(disk.totalBytes > 0)) | |
| 100 | return { needBytes, freeBytes: 0, afterShare: null, known: false, refusal: undefined }; | |
| 101 | const after = disk.freeBytes - needBytes; | |
| 102 | const afterShare = after / disk.totalBytes; | |
| 103 | const refusal = | |
| 104 | afterShare >= DISK_FLOOR | |
| 105 | ? undefined | |
| 106 | : `Refused: this plan takes ${gib(needBytes)} GiB of the ${gib(disk.freeBytes)} GiB free, which would leave ${Math.max(0, afterShare * 100).toFixed(1)}% of the disk; the forge keeps ${DISK_FLOOR * 100}% for itself.`; | |
| 107 | return { needBytes, freeBytes: disk.freeBytes, afterShare, known: true, refusal }; | |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * The room check in one sentence, the page's and the terminal's; `partial` when an origin did not | |
| 112 | * answer. | |
| 113 | */ | |
| 114 | export function roomLine(room: Room, partial: boolean, size: (kib: number) => string): string { | |
| 115 | if (room.refusal !== undefined) return room.refusal; | |
| 116 | const need = size(room.needBytes / 1024) || "nothing"; | |
| 117 | const part = partial ? " (an origin that did not answer is not counted)" : ""; | |
| 118 | return room.known | |
| 119 | ? `The default plan copies ${need} and fits the disk (${size(room.freeBytes / 1024)} free)${part}.` | |
| 120 | : `The default plan copies ${need}. Disk size unknown, nothing checked${part}.`; | |
| 121 | } | |
| 122 | ||
| 123 | const HEADER = ["repository", "private", "fork", "last_push", "size_kib", "scanned", "stale", "large", "state", "local", "suggested", "app", "domains", "secret_names"]; | |
| 124 | ||
| 125 | /** | |
| 126 | * Secret NAMES only: an inventory row never holds a value. A row not read yet has `scanned` false: | |
| 127 | * its app, domains and secrets are unknown, not empty. | |
| 128 | */ | |
| 129 | export function exportRows(rows: readonly InventoryRow[], format: Format, now: number): { type: string; body: string } { | |
| 130 | const flat = rows.map((r) => ({ | |
| 131 | repository: r.fullName, | |
| 132 | private: r.private, | |
| 133 | fork: r.fork, | |
| 134 | last_push: r.pushedAt, | |
| 135 | size_kib: r.sizeKb, | |
| 136 | scanned: r.scanned, | |
| 137 | stale: isStale(r, now), | |
| 138 | large: isLarge(r), | |
| 139 | state: r.state, | |
| 140 | local: r.local, | |
| 141 | suggested: suggest(r, now), | |
| 142 | app: r.app, | |
| 143 | domains: [...r.domains], | |
| 144 | secret_names: [...r.secrets], | |
| 145 | })); | |
| 146 | if (format === "json") return { type: "application/json", body: JSON.stringify(flat) }; | |
| 147 | return { | |
| 148 | type: "text/csv; charset=utf-8", | |
| 149 | body: toCsv( | |
| 150 | HEADER, | |
| 151 | flat.map((f) => [f.repository, `${f.private}`, `${f.fork}`, f.last_push, f.size_kib, `${f.scanned}`, `${f.stale}`, `${f.large}`, f.state, f.local, f.suggested, f.app, f.domains.join(" "), f.secret_names.join(" ")]), | |
| 152 | ), | |
| 153 | }; | |
| 154 | } |
src/tui/screens/inventory.ts +33 −9
| @@ -7,9 +7,11 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { keyOf } from "../../web/keymap.ts"; |
| 10 | import { holds, inventoryOfOrigins, type InventoryItem } from "../../actions/inventory.ts"; | |
| 10 | import { holds, inventoryOfOrigins, moveRoom, type InventoryItem } from "../../actions/inventory.ts"; | |
| 11 | 11 | import { listUserOrigins } from "../../actions/origins.ts"; |
| 12 | 12 | import { STATE_WORDS } from "../../origins/inventory.ts"; |
| 13 | import { formatKib } from "../../origins/url-preview.ts"; | |
| 14 | import { ACTION_WORDS, SHOWS, SHOW_WORDS, exportRows, filterRows, roomLine, suggest } from "../../origins/move_plan.ts"; | |
| 13 | 15 | import type { Ctx, Screen } from "../app/app.ts"; |
| 14 | 16 | import { binding } from "../app/keys.ts"; |
| 15 | 17 | import { style } from "../term/style.ts"; |
| @@ -28,7 +30,9 @@ export function inventoryScreen(f: Session, ctx: Ctx): Screen { | ||
| 28 | 30 | let items: InventoryItem[] = []; |
| 29 | 31 | let unreachable: string[] = []; |
| 30 | 32 | let loading = true; |
| 31 | const selected = (): InventoryItem | undefined => items[state.index]; | |
| 33 | let show = 0; | |
| 34 | const shown = (): InventoryItem[] => filterRows(items, SHOWS[show]!, Date.now()); | |
| 35 | const selected = (): InventoryItem | undefined => shown()[state.index]; | |
| 32 | 36 | |
| 33 | 37 | const load = (): void => { |
| 34 | 38 | if (user === undefined) return; |
| @@ -38,7 +42,7 @@ export function inventoryScreen(f: Session, ctx: Ctx): Screen { | ||
| 38 | 42 | items = got.items; |
| 39 | 43 | unreachable = got.unreachable; |
| 40 | 44 | loading = false; |
| 41 | state.clamp(items.length); | |
| 45 | state.clamp(shown().length); | |
| 42 | 46 | ctx.invalidate(); |
| 43 | 47 | }); |
| 44 | 48 | }; |
| @@ -46,31 +50,51 @@ export function inventoryScreen(f: Session, ctx: Ctx): Screen { | ||
| 46 | 50 | |
| 47 | 51 | return { |
| 48 | 52 | title: "What is left to bring over", |
| 49 | keys: [binding(["j", "k"], "move cursor"), binding(["b"], "bring in"), binding(["n"], "new forge"), binding([keyOf("reread")], "read again")], | |
| 53 | keys: [binding(["j", "k"], "move cursor"), binding(["b"], "bring in"), binding(["n"], "new forge"), binding(["f"], "filter"), binding(["c"], "copy as CSV"), binding([keyOf("reread")], "read again")], | |
| 50 | 54 | render(area, s) { |
| 51 | 55 | const c = ctx.theme.c; |
| 52 | 56 | const [head, body] = split(area, "v", [len(1), fill(1)]); |
| 53 | const said = | |
| 54 | unreachable.length === 0 ? "b brings it in · n starts a new forge" : `Could not reach ${unreachable.join(", ")}`; | |
| 57 | const room = moveRoom(f.app, items, Date.now()); | |
| 58 | const kind = SHOWS[show]!; | |
| 59 | const unread = items.filter((i) => !i.scanned).length; | |
| 60 | const said = [ | |
| 61 | unreachable.length > 0 ? `Could not reach ${unreachable.join(", ")}` : "", | |
| 62 | roomLine(room, unreachable.length > 0, formatKib), | |
| 63 | kind === "all" ? "" : `Showing: ${SHOW_WORDS[kind]}${kind === "app" && unread > 0 ? `, ${unread} not read yet are not counted` : ""}`, | |
| 64 | ] | |
| 65 | .filter((x) => x !== "") | |
| 66 | .join(" · "); | |
| 55 | 67 | s.text(head!.x, head!.y, truncate(said, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 56 | 68 | if (items.length === 0) { |
| 57 | 69 | emptyState(s, body!, c, { title: loading ? "Reading…" : unreachable.length > 0 ? "Nothing could be read." : "Nothing listed." }); |
| 58 | 70 | return; |
| 59 | 71 | } |
| 60 | const rows: ListRow[] = items.map((i) => ({ | |
| 72 | const rows: ListRow[] = shown().map((i) => ({ | |
| 61 | 73 | title: i.fullName, |
| 62 | 74 | secondary: STATE_WORDS[i.state], |
| 63 | meta: holds(i), | |
| 75 | meta: `${holds(i)} · ${formatKib(i.sizeKb) || "empty"} · ${i.pushedAt?.slice(0, 10) ?? "never pushed"}${i.fork ? " · fork" : ""} · suggested: ${ACTION_WORDS[suggest(i, Date.now())]}`, | |
| 64 | 76 | })); |
| 65 | 77 | drawList(s, body!, c, state, rows); |
| 66 | 78 | }, |
| 67 | 79 | key(e) { |
| 68 | if (state.key(e, items.length)) return true; | |
| 80 | if (state.key(e, shown().length)) return true; | |
| 69 | 81 | if (e.ctrl || e.alt) return false; |
| 70 | 82 | if (e.key === keyOf("reread")) { |
| 71 | 83 | load(); |
| 72 | 84 | return true; |
| 73 | 85 | } |
| 86 | if (e.key === "f") { | |
| 87 | show = (show + 1) % SHOWS.length; | |
| 88 | state.index = 0; | |
| 89 | state.clamp(shown().length); | |
| 90 | return true; | |
| 91 | } | |
| 92 | if (e.key === "c") { | |
| 93 | const rows = filterRows(items, SHOWS[show]!, Date.now()); | |
| 94 | ctx.copy(exportRows(rows, "csv", Date.now()).body); | |
| 95 | ctx.toast(`${rows.length} rows sent to your clipboard as CSV.`); | |
| 96 | return true; | |
| 97 | } | |
| 74 | 98 | const item = selected(); |
| 75 | 99 | if (item === undefined) return false; |
| 76 | 100 | if (e.key === "b" && item.state === "not_started") { |
src/web/pages/inventory.ts +44 −6
| @@ -6,15 +6,18 @@ | ||
| 6 | 6 | * The rows are `actions/inventory.ts`'s, the same the MCP answers. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { holds, inventoryOfOrigins, type InventoryItem } from "../../actions/inventory.ts"; | |
| 9 | import { holds, inventoryOfOrigins, moveRoom, type InventoryItem } from "../../actions/inventory.ts"; | |
| 10 | 10 | import { listUserOrigins } from "../../actions/origins.ts"; |
| 11 | 11 | import { MIGRATION_STATES, STATE_WORDS, type MigrationState } from "../../origins/inventory.ts"; |
| 12 | import { formatKib } from "../../origins/url-preview.ts"; | |
| 13 | import { ACTION_WORDS, FORMATS, SHOWS, SHOW_WORDS, exportRows, filterRows, pick, roomLine, suggest } from "../../origins/move_plan.ts"; | |
| 12 | 14 | import { notePhase, takeSessionTime, timePhaseAsync } from "../../core/route_timing.ts"; |
| 13 | 15 | import type { Ctx } from "../../http/context.ts"; |
| 14 | 16 | import { notFoundPage, pageHead, signInFirst } from "../chrome.ts"; |
| 15 | 17 | import { html, type Html } from "../html.ts"; |
| 16 | 18 | import { page } from "../layout.ts"; |
| 17 | import { badge, btn, empty, row, type Tone } from "../ui.ts"; | |
| 19 | import { badge, btn, empty, row, seg, type Tone } from "../ui.ts"; | |
| 20 | import { securityFloor } from "../../http/respond.ts"; | |
| 18 | 21 | |
| 19 | 22 | const TONE: Record<MigrationState, Tone> = { |
| 20 | 23 | not_started: "default", |
| @@ -24,7 +27,7 @@ const TONE: Record<MigrationState, Tone> = { | ||
| 24 | 27 | cut_over: "success", |
| 25 | 28 | }; |
| 26 | 29 | |
| 27 | function inventoryRow(item: InventoryItem): Html { | |
| 30 | function inventoryRow(item: InventoryItem, now: number): Html { | |
| 28 | 31 | const started = item.state !== "not_started" && item.state !== "migrating"; |
| 29 | 32 | const actions = |
| 30 | 33 | item.local !== null && started |
| @@ -37,8 +40,10 @@ function inventoryRow(item: InventoryItem): Html { | ||
| 37 | 40 | ]; |
| 38 | 41 | return row({ |
| 39 | 42 | title: item.fullName, |
| 40 | meta: html`${holds(item)}${item.private ? " · private" : ""}`, | |
| 43 | meta: html`${holds(item)}${item.private ? " · private" : " · public"}${item.fork ? " · fork" : ""} · ${ACTION_WORDS[suggest(item, now)]}`, | |
| 41 | 44 | facts: [ |
| 45 | { label: "Size", value: formatKib(item.sizeKb) || "empty" }, | |
| 46 | { label: "Last push", value: item.pushedAt === null ? "never" : item.pushedAt.slice(0, 10) }, | |
| 42 | 47 | ...(item.domains.length > 0 ? [{ label: "Domains", value: item.domains.join(", ") }] : []), |
| 43 | 48 | ...(item.secrets.length > 0 ? [{ label: "Secret names", value: item.secrets.join(", ") }] : []), |
| 44 | 49 | ], |
| @@ -63,9 +68,32 @@ export async function inventoryPage(ctx: Ctx): Promise<Response> { | ||
| 63 | 68 | const asked = ctx.url.searchParams.get("origin") ?? ""; |
| 64 | 69 | const chosen = asked === "" ? origins : origins.filter((o) => o.id === asked); |
| 65 | 70 | if (asked !== "" && chosen.length === 0) return notFoundPage(ctx); |
| 66 | const { items, unreachable } = await timePhaseAsync(ctx.request, "inventory", () => | |
| 71 | const show = pick(SHOWS, ctx.url.searchParams.get("show")); | |
| 72 | const format = ctx.url.searchParams.get("format"); | |
| 73 | const now = Date.now(); | |
| 74 | const { items: all, unreachable } = await timePhaseAsync(ctx.request, "inventory", () => | |
| 67 | 75 | inventoryOfOrigins(ctx.app, user, chosen), |
| 68 | 76 | ); |
| 77 | if (format !== null && (FORMATS as readonly string[]).includes(format)) { | |
| 78 | const out = exportRows(filterRows(all, show, now), pick(FORMATS, format), now); | |
| 79 | return securityFloor( | |
| 80 | new Response(out.body, { | |
| 81 | headers: { "content-type": out.type, "content-disposition": `attachment; filename="inventory.${format}"`, "cache-control": "no-store" }, | |
| 82 | }), | |
| 83 | ); | |
| 84 | } | |
| 85 | const items = filterRows(all, show, now); | |
| 86 | const room = moveRoom(ctx.app, all, now); | |
| 87 | // Every link on the page, the filters and the export, is built here. | |
| 88 | const query = (over: { show?: string; format?: string }): string => { | |
| 89 | const q = new URLSearchParams(); | |
| 90 | if (asked !== "") q.set("origin", asked); | |
| 91 | const shown = over.show ?? show; | |
| 92 | if (shown !== "all") q.set("show", shown); | |
| 93 | if (over.format !== undefined) q.set("format", over.format); | |
| 94 | return `/migrate/inventory${q.size === 0 ? "" : `?${q}`}`; | |
| 95 | }; | |
| 96 | const unread = all.filter((i) => !i.scanned).length; | |
| 69 | 97 | const next = items.find((i) => i.app !== null && i.state === "not_started"); |
| 70 | 98 | const rendering = performance.now(); |
| 71 | 99 | const body = |
| @@ -88,7 +116,17 @@ export async function inventoryPage(ctx: Ctx): Promise<Response> { | ||
| 88 | 116 | : html`<p class="muted small mb" id="inventory-unreachable"> |
| 89 | 117 | Could not reach ${unreachable.join(", ")}. |
| 90 | 118 | </p>`} |
| 91 | <ul class="list setting-list" id="inventory">${items.map(inventoryRow)}</ul>`; | |
| 119 | <p class="small mb" id="inventory-room" data-refused="${room.refusal === undefined ? "no" : "yes"}"> | |
| 120 | ${roomLine(room, unreachable.length > 0, formatKib)} | |
| 121 | </p> | |
| 122 | ${seg(SHOWS.map((id) => ({ label: SHOW_WORDS[id], href: query({ show: id }), current: id === show })), true)} | |
| 123 | ${show === "app" && unread > 0 | |
| 124 | ? html`<p class="muted small mb" id="inventory-unread">${unread} not read yet, so not counted here.</p>` | |
| 125 | : null} | |
| 126 | <p class="muted small mb"> | |
| 127 | Export: <a href="${query({ format: "csv" })}">CSV</a> · <a href="${query({ format: "json" })}">JSON</a> | |
| 128 | </p> | |
| 129 | <ul class="list setting-list" id="inventory">${items.map((i) => inventoryRow(i, now))}</ul>`; | |
| 92 | 130 | const response = page({ ctx, title: "Inventory", body }); |
| 93 | 131 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 94 | 132 | return response; |
test/move-plan.test.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** The first look at a move in (#6007): defaults, filters, the disk floor, the export. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { copyBytes, exportRows, filterRows, roomFor, roomLine, suggest } from "../src/origins/move_plan.ts"; | |
| 4 | import type { InventoryRow } from "../src/origins/inventory.ts"; | |
| 5 | import { NOTHING } from "../src/origins/inventory_detect.ts"; | |
| 6 | ||
| 7 | const NOW = Date.parse("2026-12-04T00:00:00Z"); | |
| 8 | const GIB = 1024 ** 3; | |
| 9 | const row = (over: Partial<InventoryRow>): InventoryRow => ({ | |
| 10 | ...NOTHING, | |
| 11 | originId: "o", | |
| 12 | fullName: "me/x", | |
| 13 | htmlUrl: "https://github.com/me/x", | |
| 14 | private: false, | |
| 15 | fork: false, | |
| 16 | pushedAt: "2026-11-01T00:00:00Z", | |
| 17 | sizeKb: 1024, | |
| 18 | scanned: true, | |
| 19 | state: "not_started", | |
| 20 | local: null, | |
| 21 | ...over, | |
| 22 | }); | |
| 23 | ||
| 24 | test("defaults: forks are linked, stale ones archived, the rest brought in", () => { | |
| 25 | expect(suggest(row({ fork: true }), NOW)).toBe("link"); | |
| 26 | expect(suggest(row({ pushedAt: "2025-01-01T00:00:00Z" }), NOW)).toBe("archived"); | |
| 27 | expect(suggest(row({ pushedAt: null }), NOW)).toBe("bring"); | |
| 28 | expect(suggest(row({}), NOW)).toBe("bring"); | |
| 29 | }); | |
| 30 | ||
| 31 | test("filters keep forks, stale, large and apps", () => { | |
| 32 | const rows = [ | |
| 33 | row({ fullName: "a", fork: true }), | |
| 34 | row({ fullName: "b", pushedAt: "2024-01-01T00:00:00Z" }), | |
| 35 | row({ fullName: "c", sizeKb: 100 * 1024 + 1 }), | |
| 36 | row({ fullName: "d", app: "forge" }), | |
| 37 | ]; | |
| 38 | const names = (s: Parameters<typeof filterRows>[1]) => filterRows(rows, s, NOW).map((r) => r.fullName); | |
| 39 | expect(names("forks")).toEqual(["a"]); | |
| 40 | expect(names("stale")).toEqual(["b"]); | |
| 41 | expect(names("large")).toEqual(["c"]); | |
| 42 | expect(names("app")).toEqual(["d"]); | |
| 43 | expect(names("all")).toEqual(["a", "b", "c", "d"]); | |
| 44 | }); | |
| 45 | ||
| 46 | test("the plan copies everything but forks", () => { | |
| 47 | expect(copyBytes([row({ sizeKb: 1024 }), row({ fork: true, sizeKb: 4096 })], NOW)).toBe(1024 * 1024); | |
| 48 | }); | |
| 49 | ||
| 50 | test("a plan that would take the disk past its floor is refused", () => { | |
| 51 | const disk = { freeBytes: 30 * GIB, totalBytes: 100 * GIB }; | |
| 52 | expect(roomFor(10 * GIB, disk).refusal).toBeUndefined(); | |
| 53 | const refused = roomFor(20 * GIB, disk); | |
| 54 | expect(refused.refusal).toContain("Refused"); | |
| 55 | expect(refused.refusal).toContain("15%"); | |
| 56 | expect(roomFor(100 * GIB, disk).refusal).toContain("0.0%"); | |
| 57 | expect(roomFor(100 * GIB, undefined).refusal).toBeUndefined(); | |
| 58 | }); | |
| 59 | ||
| 60 | test("the export carries secret names, not values, and writes a formula as text", () => { | |
| 61 | const r = row({ fullName: "=cmd/x", secrets: ["STRIPE_KEY"], domains: ["a.example.com"] }); | |
| 62 | const csv = exportRows([r], "csv", NOW).body; | |
| 63 | expect(csv.split("\r\n")[0]).toContain("size_kib"); | |
| 64 | expect(csv).toContain("'=cmd/x"); | |
| 65 | expect(csv).toContain("STRIPE_KEY"); | |
| 66 | const json = JSON.parse(exportRows([r], "json", NOW).body); | |
| 67 | expect(json[0]).toMatchObject({ size_kib: 1024, suggested: "bring", secret_names: ["STRIPE_KEY"] }); | |
| 68 | }); | |
| 69 | ||
| 70 | test("a disk that cannot be read says nothing was checked, never that it fits", () => { | |
| 71 | const size = (k: number) => `${k}K`; | |
| 72 | const unknown = roomFor(GIB, undefined); | |
| 73 | expect(unknown.known).toBe(false); | |
| 74 | expect(roomLine(unknown, false, size)).toContain("Disk size unknown, nothing checked"); | |
| 75 | expect(roomLine(unknown, false, size)).not.toContain("fits"); | |
| 76 | expect(roomLine(roomFor(GIB, { freeBytes: 50 * GIB, totalBytes: 100 * GIB }), true, size)).toContain("fits the disk"); | |
| 77 | expect(roomLine(roomFor(GIB, { freeBytes: 50 * GIB, totalBytes: 100 * GIB }), true, size)).toContain("did not answer"); | |
| 78 | expect(roomLine(roomFor(60 * GIB, { freeBytes: 50 * GIB, totalBytes: 100 * GIB }), false, size)).toContain("Refused"); | |
| 79 | }); | |
| 80 | ||
| 81 | test("a row not read yet is marked in the export, not shown as holding nothing", () => { | |
| 82 | const unread = row({ scanned: false }); | |
| 83 | expect(JSON.parse(exportRows([unread], "json", NOW).body)[0].scanned).toBe(false); | |
| 84 | const lines = exportRows([unread], "csv", NOW).body.split("\r\n"); | |
| 85 | expect(lines[0]).toContain("scanned"); | |
| 86 | expect(lines[1]).toContain("false"); | |
| 87 | }); |
test/origin-inventory.test.ts +59
| @@ -174,3 +174,62 @@ test("states come from the forge's own records", async () => { | ||
| 174 | 174 | setCutOver(app.db, "shop.example.com", new Date().toISOString()); |
| 175 | 175 | expect(stateOf(app.db, alice, row).state).toBe("cut_over"); |
| 176 | 176 | }); |
| 177 | ||
| 178 | test("the page shows size, the room check, a filter and a CSV and JSON export (#6007)", async () => { | |
| 179 | const get = (q: string) => fetch(`${servers.url}/migrate/inventory${q}`, { headers: { authorization: auth } }); | |
| 180 | const body = await (await get("")).text(); | |
| 181 | expect(body).toContain('id="inventory-room"'); | |
| 182 | expect(body).toContain("Last push"); | |
| 183 | expect(body).toContain("Bring in"); | |
| 184 | expect(body).toContain("show=forks"); | |
| 185 | const forks = await (await get("?show=forks")).text(); | |
| 186 | expect(forks).not.toContain("octo/shop"); | |
| 187 | const csv = await get("?format=csv"); | |
| 188 | expect(csv.headers.get("content-type")).toContain("text/csv"); | |
| 189 | expect(csv.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 190 | const text = await csv.text(); | |
| 191 | expect(text).toContain("octo/shop"); | |
| 192 | expect(text).not.toContain(VALUE); | |
| 193 | const json = (await (await get("?format=json")).json()) as { repository: string }[]; | |
| 194 | expect(json.map((j) => j.repository).sort()).toEqual(["octo/blog", "octo/notes", "octo/shop"]); | |
| 195 | const signedOut = await fetch(`${servers.url}/migrate/inventory?format=json`, { redirect: "manual" }); | |
| 196 | expect(await signedOut.text()).not.toContain("octo/shop"); | |
| 197 | }); | |
| 198 | ||
| 199 | test("the room line says so when the disk cannot be read, and Refused when the plan is too big (#6007)", async () => { | |
| 200 | const get = () => fetch(`${servers.url}/migrate/inventory`, { headers: { authorization: auth } }).then((r) => r.text()); | |
| 201 | const config = app.config as { disk?: unknown }; | |
| 202 | const before = config.disk; | |
| 203 | try { | |
| 204 | config.disk = () => { | |
| 205 | throw new Error("no disk"); | |
| 206 | }; | |
| 207 | const unknown = await get(); | |
| 208 | expect(unknown).toContain("Disk size unknown, nothing checked"); | |
| 209 | expect(unknown).not.toContain("fits the disk"); | |
| 210 | expect(unknown).toContain('data-refused="no"'); | |
| 211 | config.disk = () => ({ freeBytes: 1, totalBytes: 1024 ** 4 }); | |
| 212 | const refused = await get(); | |
| 213 | expect(refused).toContain('data-refused="yes"'); | |
| 214 | expect(refused).toContain("Refused:"); | |
| 215 | } finally { | |
| 216 | config.disk = before; | |
| 217 | } | |
| 218 | }); | |
| 219 | ||
| 220 | test("an unknown ?format= is the page, not a file; the filter and export links share one builder (#6007)", async () => { | |
| 221 | const res = await fetch(`${servers.url}/migrate/inventory?format=xml&show=app`, { headers: { authorization: auth } }); | |
| 222 | expect(res.headers.get("content-type")).toContain("text/html"); | |
| 223 | const body = await res.text(); | |
| 224 | expect(body).toContain('id="inventory"'); | |
| 225 | expect(body).toContain('href="/migrate/inventory?show=app&format=csv"'); | |
| 226 | expect(body).toContain('href="/migrate/inventory?show=forks"'); | |
| 227 | expect(body).toContain('href="/migrate/inventory"'); | |
| 228 | }); | |
| 229 | ||
| 230 | test("the export marks a row not read yet, and the Apps filter says unread rows are not counted (#6007)", async () => { | |
| 231 | const json = (await (await fetch(`${servers.url}/migrate/inventory?format=json`, { headers: { authorization: auth } })).json()) as { scanned: boolean }[]; | |
| 232 | expect(json.every((j) => typeof j.scanned === "boolean")).toBe(true); | |
| 233 | const csv = await (await fetch(`${servers.url}/migrate/inventory?format=csv`, { headers: { authorization: auth } })).text(); | |
| 234 | expect(csv.split("\r\n")[0]).toContain("scanned"); | |
| 235 | }); |
test/tui-bring.test.ts +27
| @@ -141,3 +141,30 @@ describe("the bring screen", () => { | ||
| 141 | 141 | } |
| 142 | 142 | }); |
| 143 | 143 | }); |
| 144 | ||
| 145 | describe("the inventory screen (#6007)", () => { | |
| 146 | test("f filters the list and the header says what is shown; the room line never says it fits an unread disk", async () => { | |
| 147 | const { inventoryScreen } = await import("../src/tui/screens/inventory.ts"); | |
| 148 | const terminal = headlessTerminal(140, 30); | |
| 149 | const { app: tui, session } = createForgeApp(app, identity(alice), { terminal, exitHooks: false }); | |
| 150 | tui.start(); | |
| 151 | try { | |
| 152 | await settleApp(tui); | |
| 153 | (app.config as { disk?: unknown }).disk = () => { | |
| 154 | throw new Error("no disk"); | |
| 155 | }; | |
| 156 | tui.ctx.push(inventoryScreen(session, tui.ctx)); | |
| 157 | await settleApp(tui); | |
| 158 | expect(tui.screen.toText()).toContain("Disk size unknown, nothing checked"); | |
| 159 | terminal.feed("f"); | |
| 160 | await settleApp(tui); | |
| 161 | expect(tui.screen.toText()).toContain("Showing: Forks"); | |
| 162 | for (let i = 0; i < 4; i++) terminal.feed("f"); | |
| 163 | await settleApp(tui); | |
| 164 | expect(tui.screen.toText()).not.toContain("Showing:"); | |
| 165 | } finally { | |
| 166 | tui.stop(); | |
| 167 | delete (app.config as { disk?: unknown }).disk; | |
| 168 | } | |
| 169 | }); | |
| 170 | }); |
new above
offline
Fixes 1 and 2: says 'fits the disk' even when the disk could not be read. The terminal also has no filter and no export; the page does.