Public: anyone can read this; the crew can change it.
Tab forge: the whole tab, the whole forge, and this machine put to work #18290
Merged 5h 29m ago, live in production
scout/forge:scout/18259-tab-forge-the-whole-tab-the-whole-forge → main 0 commits fork 10 threads · 10 unresolved
19 files
changed +454
−92
- docs/features/tab.md
- src/generated/screens.ts
- src/tab/boot.ts
- src/tab/coverage.ts
- src/tab/forge.ts
- src/tab/restore.ts
- src/tab/shell.ts
- src/tab/whole.ts
- src/tab/words.ts
- src/tab/worker.ts
- src/tui/screens/common.ts
- src/tui/screens/maintainruns.ts
- src/tui/screens/maintenanceweek.ts
- src/tui/screens/tabworks.ts
- src/web/style/content.ts
- src/web/tab-asset.ts
- test/palette-roster.test.ts
- test/support/tab-db.ts
- test/tab-whole.test.ts
docs/features/tab.md +12
| @@ -65,3 +65,15 @@ Tests: `test/tab-fill.test.ts` runs a stand-in model through a good answer, inva | ||
| 65 | 65 | ### What was chosen, and what was not measured |
| 66 | 66 | |
| 67 | 67 | The runtime is the browser's own Prompt API: it adds no dependency, no weights hosted or proxied by this forge, takes a JSON schema as a constraint, and keeps the prompt on the device. WebLLM and wllama were not chosen: they need their weights and runtime from a third-party host, which this page's policy (`connect-src 'self'`) does not allow and which would be a request leaving the device. **Not measured:** download size, time to first app and memory on a phone-class device. The run that built this had no device, no GPU and no route to a model, and a figure written without one would be invented. The "about 2 GB" on the page is the browser's own figure for its model, not ours. What a person with a phone gets today: most phones lack the Prompt API, so the tier is not offered and the compiler writes the app, with no download and no model memory. Where the API exists, the numbers have to come from a real device before this tier is relied on. |
| 68 | ||
| 69 | ## The whole tab (#18259) | |
| 70 | ||
| 71 | Once started, the forge is the tab: its frame covers the viewport (`html[data-running]`, `src/web/style/content.ts`), with no page around it. Its shell carries one quiet line, *This forge lives in this browser… Export*, and the pages below (`src/tab/whole.ts`, `src/tab/coverage.ts`, words in `TAB_WHOLE`, said again by the terminal's entry, `src/tui/screens/tab.ts`): | |
| 72 | ||
| 73 | - **What works** (`/tab/f/pages`): every page of the hosted forge (`GO_PAGES`), as works, works differently, or cannot work in a tab (with why and the way to get it: "use your hosted forge"); there is no fourth outcome. The terminal lists the same in *What works in a forge in a tab* (`src/tui/screens/tabworks.ts`), with the runner words. Search, Waiting and Archive are cannot; Board and Notes work differently (an app made from a sentence). `test/tab-whole.test.ts` fails if the hosted list gains a page this one does not name, and if a page that claims to work does not answer. | |
| 74 | - **Runners** (`/tab/f/runners`, in the menu; the What works list does not link it, because no *cannot* page is linked there): says what a tab can run (JavaScript and WebAssembly in a worker) and cannot (git, a shell, native programs, listening, surviving a closed tab). It has **no switch**: the tab runner is #18257's spike, and a switch that did nothing would be dead. Until it lands this tab is not a runner. | |
| 75 | - **Where it is kept** (`/tab/f/storage`): OPFS, this device. The first Start, a press, asks the browser to keep the storage (`navigator.storage.persist()`, which some browsers answer with a prompt); a start by itself on a later load only reads `persisted()`. The page relays the answer, so the line says "promised" or "may be cleared". | |
| 76 | ||
| 77 | It survives a reload and a reopened tab: the page remembers (`localStorage`) that the forge was started and the last page of it that was read, and starts it again on load. Every decision a reload makes is in `src/tab/restore.ts` and tested in `test/tab-whole.test.ts`: `startsItself` (starts only when this browser started it before), `openOn` (only a `/tab/f/` path is ever opened), `keepStorage` (asks only on the first Start). When the browser cleared the store but kept `localStorage`, the worker reports the store empty (`unmade`, no tables before the migration): every page of it carries *The browser cleared this forge since it last ran: Import an export to bring it back.* When the browser cleared both, the page is a first visit again: Start. A second tab cannot open the store while the first holds it; it says so and names the way out. If the browser clears the site's data the forge is gone: export first. | |
| 78 | ||
| 79 | Not done: a topbar identical to the hosted forge's, an export of the whole database file (the export is the app and its issues), a hosted forge reading the file, and the runner switch. The forge is still shown in a frame over /tab, not served as the tab's own page in the hosted shell: the database needs a dedicated worker (OPFS sync handles), which a page under `/tab/f/` loses on every navigation. The cold read by a no-context agent was not run. |
src/generated/screens.ts +14 −12
| @@ -126,18 +126,19 @@ import * as s121 from "../tui/screens/stalepulls.ts"; | ||
| 126 | 126 | import * as s122 from "../tui/screens/subscriptions.ts"; |
| 127 | 127 | import * as s123 from "../tui/screens/system.ts"; |
| 128 | 128 | import * as s124 from "../tui/screens/tab.ts"; |
| 129 | import * as s125 from "../tui/screens/teams.ts"; | |
| 130 | import * as s126 from "../tui/screens/telemetry.ts"; | |
| 131 | import * as s127 from "../tui/screens/tell_undo.ts"; | |
| 132 | import * as s128 from "../tui/screens/traces.ts"; | |
| 133 | import * as s129 from "../tui/screens/traffic.ts"; | |
| 134 | import * as s130 from "../tui/screens/trafficview.ts"; | |
| 135 | import * as s131 from "../tui/screens/valves.ts"; | |
| 136 | import * as s132 from "../tui/screens/waiting.ts"; | |
| 137 | import * as s133 from "../tui/screens/waiting_for_me.ts"; | |
| 138 | import * as s134 from "../tui/screens/wayback.ts"; | |
| 139 | import * as s135 from "../tui/screens/weekcard.ts"; | |
| 140 | import * as s136 from "../tui/screens/whocan.ts"; | |
| 129 | import * as s125 from "../tui/screens/tabworks.ts"; | |
| 130 | import * as s126 from "../tui/screens/teams.ts"; | |
| 131 | import * as s127 from "../tui/screens/telemetry.ts"; | |
| 132 | import * as s128 from "../tui/screens/tell_undo.ts"; | |
| 133 | import * as s129 from "../tui/screens/traces.ts"; | |
| 134 | import * as s130 from "../tui/screens/traffic.ts"; | |
| 135 | import * as s131 from "../tui/screens/trafficview.ts"; | |
| 136 | import * as s132 from "../tui/screens/valves.ts"; | |
| 137 | import * as s133 from "../tui/screens/waiting.ts"; | |
| 138 | import * as s134 from "../tui/screens/waiting_for_me.ts"; | |
| 139 | import * as s135 from "../tui/screens/wayback.ts"; | |
| 140 | import * as s136 from "../tui/screens/weekcard.ts"; | |
| 141 | import * as s137 from "../tui/screens/whocan.ts"; | |
| 141 | 142 | |
| 142 | 143 | /** Each screen file that declares palette entries: its module and the export names. */ |
| 143 | 144 | export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readonly string[]])[] = [ |
| @@ -278,4 +279,5 @@ export const SCREEN_MODULES: readonly (readonly [Record<string, unknown>, readon | ||
| 278 | 279 | [s134, ["entry"]], |
| 279 | 280 | [s135, ["entry"]], |
| 280 | 281 | [s136, ["entry"]], |
| 282 | [s137, ["entry"]], | |
| 281 | 283 | ]; |
src/tab/boot.ts +50 −21
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | import { activated, type Registration } from "./active.ts"; |
| 7 | 7 | import type { Runtime } from "./fill.ts"; |
| 8 | 8 | import type { TabRequest } from "./forge.ts"; |
| 9 | import { keepStorage, openOn, startsItself } from "./restore.ts"; | |
| 9 | 10 | |
| 10 | 11 | // The few browser globals this file touches, named here rather than pulling in the DOM library. |
| 11 | 12 | interface Box { |
| @@ -16,7 +17,7 @@ interface Box { | ||
| 16 | 17 | src: string; |
| 17 | 18 | value: string; |
| 18 | 19 | srcdoc: string; |
| 19 | addEventListener(type: "click" | "input", listener: () => void): void; | |
| 20 | addEventListener(type: "click" | "input" | "load", listener: () => void): void; | |
| 20 | 21 | addEventListener(type: "submit", listener: (e: { preventDefault(): void }) => void): void; |
| 21 | 22 | } |
| 22 | 23 | interface Registrations { |
| @@ -25,7 +26,13 @@ interface Registrations { | ||
| 25 | 26 | register(script: string, options: { scope: string }): Promise<Registration>; |
| 26 | 27 | } |
| 27 | 28 | declare const document: { getElementById(id: string): Box | null; readonly documentElement: { readonly dataset: Record<string, string> } }; |
| 28 | declare const navigator: { readonly serviceWorker?: Registrations; readonly storage?: { getDirectory?: unknown } }; | |
| 29 | declare const navigator: { readonly serviceWorker?: Registrations; readonly storage?: { getDirectory?: unknown; persist?(): Promise<boolean>; persisted?(): Promise<boolean> } }; | |
| 30 | interface Frame extends Box { | |
| 31 | readonly contentWindow: { readonly location: { readonly pathname: string } } | null; | |
| 32 | } | |
| 33 | declare const localStorage: { getItem(key: string): string | null; setItem(key: string, value: string): void }; | |
| 34 | const ON = "tf:on"; | |
| 35 | const WHERE = "tf:at"; | |
| 29 | 36 | |
| 30 | 37 | // Named apart so a scan for server-side Workers does not read this browser file as one. |
| 31 | 38 | const Spawn = Worker; |
| @@ -37,9 +44,13 @@ const say = (text: string): void => { | ||
| 37 | 44 | // The worker of the forge this page started; null on a page that never did, which then stays |
| 38 | 45 | // silent. |
| 39 | 46 | let holder: Worker | null = null; |
| 47 | // Whether the browser promised to keep the storage, asked once at start; the forge's pages say it. | |
| 48 | let kept: "persistent" | "best-effort" | null = null; | |
| 49 | // Whether a forge that ran here before opened on an empty store; its pages say so. | |
| 50 | let cleared = false; | |
| 40 | 51 | navigator.serviceWorker?.addEventListener("message", (e: MessageEvent<{ forge?: TabRequest }>) => { |
| 41 | 52 | const port = e.ports[0]; |
| 42 | if (holder !== null && e.data.forge !== undefined && port !== undefined) holder.postMessage({ req: e.data.forge }, [port]); | |
| 53 | if (holder !== null && e.data.forge !== undefined && port !== undefined) holder.postMessage({ req: { ...e.data.forge, kept, cleared } }, [port]); | |
| 43 | 54 | }); |
| 44 | 55 | navigator.serviceWorker?.startMessages(); |
| 45 | 56 | |
| @@ -47,21 +58,24 @@ async function start(button: Box): Promise<boolean> { | ||
| 47 | 58 | button.disabled = true; |
| 48 | 59 | const workers = navigator.serviceWorker; |
| 49 | 60 | if (workers === undefined || typeof Worker === "undefined" || navigator.storage?.getDirectory === undefined) { |
| 50 | say("This browser cannot run a forge in a tab: it lacks service workers or private file storage."); | |
| 61 | say("This browser lacks service workers or private storage for a forge."); | |
| 51 | 62 | return false; |
| 52 | 63 | } |
| 53 | 64 | say("Starting…"); |
| 54 | 65 | const worker = new Spawn(button.dataset.worker!, { type: "module" }); |
| 66 | let empty = false; | |
| 55 | 67 | const ready = new Promise<string | null>((resolve) => { |
| 56 | worker.onmessage = (e: MessageEvent<{ ready?: boolean; failed?: string }>) => resolve(e.data.ready ? null : (e.data.failed ?? "failed")); | |
| 68 | worker.onmessage = (e: MessageEvent<{ ready?: boolean; empty?: boolean; failed?: string }>) => { | |
| 69 | empty = !!e.data.empty; | |
| 70 | resolve(e.data.ready ? null : (e.data.failed ?? "failed")); | |
| 71 | }; | |
| 57 | 72 | }); |
| 58 | 73 | worker.postMessage({ start: button.dataset.sqlite }); |
| 59 | 74 | const failed = await ready; |
| 60 | 75 | if (failed !== null) { |
| 61 | // The raw error stays in the console; the person gets what they can do about it. | |
| 62 | console.warn("tab forge: database", failed); | |
| 76 | console.warn("tab: database", failed); | |
| 63 | 77 | worker.terminate(); |
| 64 | say("The forge's storage could not open. Another tab of this browser may already hold the forge: use that one, or close it and press Start again."); | |
| 78 | say("Another tab may already hold the forge: use it, or close it."); | |
| 65 | 79 | button.disabled = false; |
| 66 | 80 | return false; |
| 67 | 81 | } |
| @@ -71,18 +85,30 @@ async function start(button: Box): Promise<boolean> { | ||
| 71 | 85 | // is /tab/f/, so it never settles. |
| 72 | 86 | await activated(await workers.register(button.dataset.sw!, { scope: "/tab/f/" })); |
| 73 | 87 | } catch (error) { |
| 74 | console.warn("tab forge: service worker", error); | |
| 88 | console.warn("tab: service worker", error); | |
| 75 | 89 | holder = null; |
| 76 | 90 | worker.terminate(); |
| 77 | say("The browser would not start the forge's server in this tab. Reload the page and press Start again; a private window may not allow it."); | |
| 91 | say("The browser would not start the forge's server: reload and press Start."); | |
| 78 | 92 | button.disabled = false; |
| 79 | 93 | return false; |
| 80 | 94 | } |
| 81 | const frame = document.getElementById("tab-frame")!; | |
| 82 | frame.src = "/tab/f/"; | |
| 95 | const ranBefore = startsItself(localStorage.getItem(ON)); | |
| 96 | // Asked to keep the storage on the first Start only; the forge's pages say whether it agreed. | |
| 97 | kept = await keepStorage(navigator.storage, !ranBefore); | |
| 98 | const frame = document.getElementById("tab-frame") as unknown as Frame; | |
| 99 | // Back where the person was. | |
| 100 | frame.src = openOn(localStorage.getItem(WHERE)); | |
| 101 | frame.addEventListener("load", () => { | |
| 102 | const at = frame.contentWindow?.location.pathname; | |
| 103 | if (at !== undefined && openOn(at) === at) localStorage.setItem(WHERE, at); | |
| 104 | }); | |
| 83 | 105 | frame.hidden = false; |
| 106 | localStorage.setItem(ON, "1"); | |
| 107 | // The forge is the whole tab: the page's own chrome steps back (CSS on `data-running`). | |
| 84 | 108 | document.documentElement.dataset.running = "1"; |
| 85 | say("Running in this tab."); | |
| 109 | // Its pages say so: the page's own status is hidden while the forge fills the tab. | |
| 110 | cleared = ranBefore && empty; | |
| 111 | say("Running."); | |
| 86 | 112 | return true; |
| 87 | 113 | } |
| 88 | 114 | |
| @@ -121,21 +147,21 @@ async function offerModel(): Promise<void> { | ||
| 121 | 147 | const state = await (await modelCode(from)).offer(); |
| 122 | 148 | if (state === null) return; |
| 123 | 149 | section.hidden = false; |
| 124 | if (state === "available") get.textContent = "Turn on the model (it is already on this device)"; | |
| 150 | if (state === "available") get.textContent = "Turn on the model (on this device)"; | |
| 125 | 151 | get.addEventListener("click", () => { |
| 126 | 152 | get.disabled = true; |
| 127 | status.textContent = "Starting the model…"; | |
| 153 | status.textContent = "Starting…"; | |
| 128 | 154 | void modelCode(from).then((m) => m.turnOn((fraction) => { |
| 129 | 155 | status.textContent = `Downloading the model: ${Math.round(fraction * 100)}%`; |
| 130 | 156 | }).then( |
| 131 | 157 | (r) => { |
| 132 | 158 | runtime = r; |
| 133 | status.textContent = "The model is on. It runs on this device, and \"Make it\" now asks it first."; | |
| 159 | status.textContent = "The model is on: Make it asks it first."; | |
| 134 | 160 | get.hidden = true; |
| 135 | 161 | }, |
| 136 | 162 | (error) => { |
| 137 | console.warn("tab forge: model", error); | |
| 138 | status.textContent = "The model could not start here. The compiler still writes apps."; | |
| 163 | console.warn("tab: model", error); | |
| 164 | status.textContent = "The model could not start; the compiler writes apps."; | |
| 139 | 165 | get.disabled = false; |
| 140 | 166 | }, |
| 141 | 167 | )); |
| @@ -150,6 +176,9 @@ if (button !== null && go !== null) { | ||
| 150 | 176 | say("Not started."); |
| 151 | 177 | button.addEventListener("click", () => void start(button)); |
| 152 | 178 | void offerModel(); |
| 179 | // A reload, or a tab opened again, starts the forge it had: the person never lands on a start | |
| 180 | // button. | |
| 181 | if (startsItself(localStorage.getItem(ON))) void start(button); | |
| 153 | 182 | } |
| 154 | 183 | |
| 155 | 184 | // "Make an app": the compiler is a separate file, fetched the first time the box is used, so the |
| @@ -176,7 +205,7 @@ if (make !== null && sayBox !== null && previewFrame !== null && button !== null | ||
| 176 | 205 | if (holder === null && !(await start(button))) return; |
| 177 | 206 | let answer: string | null = null; |
| 178 | 207 | if (runtime !== null && code !== null) { |
| 179 | say("Writing the app with the model…"); | |
| 208 | say("Writing with the model…"); | |
| 180 | 209 | answer = await (await code).askModel(runtime, sentence, MODEL_WAIT_MS); |
| 181 | 210 | } |
| 182 | 211 | const done = await ask({ method: "POST", path: "/apps", form: answer === null ? { say: sentence } : { say: sentence, answer } }); |
| @@ -184,8 +213,8 @@ if (make !== null && sayBox !== null && previewFrame !== null && button !== null | ||
| 184 | 213 | document.getElementById("tab-frame")!.src = done.location; |
| 185 | 214 | previewFrame.hidden = true; |
| 186 | 215 | sayBox.value = ""; |
| 187 | say("Made. It is kept on this device, in this browser."); | |
| 188 | } else say("That could not be saved. Try a shorter sentence."); | |
| 216 | say("Made, kept in this browser."); | |
| 217 | } else say("Not saved. Try a shorter sentence."); | |
| 189 | 218 | })(); |
| 190 | 219 | }); |
| 191 | 220 | } |
src/tab/coverage.ts added +59
| @@ -0,0 +1,59 @@ | ||
| 1 | /** | |
| 2 | * The tab forge's honest map of the hosted forge (#18259): every top-level page the hosted forge | |
| 3 | * has, and whether this tab has it. Plain data out; `test/tab-whole.test.ts` holds it equal to the | |
| 4 | * hosted forge's own list (`web/go_pages.ts`), so a page added there is named here or the check | |
| 5 | * fails. | |
| 6 | */ | |
| 7 | ||
| 8 | export type Way = "works" | "differs" | "cannot"; | |
| 9 | ||
| 10 | export interface Coverage { | |
| 11 | /** The hosted forge's name for the page (`GO_PAGES`). */ | |
| 12 | readonly name: string; | |
| 13 | readonly way: Way; | |
| 14 | /** How it differs, why it cannot, or when it will. Always said. */ | |
| 15 | readonly words: string; | |
| 16 | /** Where in this forge it lives, for the pages that work. */ | |
| 17 | readonly at?: string; | |
| 18 | } | |
| 19 | ||
| 20 | const HOSTED = "Use your hosted forge for it."; | |
| 21 | const NEEDS_SERVER = "needs a machine that stays on and answers other people"; | |
| 22 | const NEEDS_CREW = "needs the crew, and the crew runs on a server with git and a shell"; | |
| 23 | ||
| 24 | export const COVERAGE: readonly Coverage[] = [ | |
| 25 | { name: "Home", way: "differs", at: "/", words: "Counts this forge's issues and shows its apps; there is no feed of other people's posts." }, | |
| 26 | { name: "Apps", way: "differs", words: "Apps here are made from a sentence and kept as data, drawn by fixed pages. They are not deployed and have no address; Get a link shares a read-only copy." }, | |
| 27 | { name: "Issues", way: "works", at: "/issues", words: "Open an issue, read the list and each one." }, | |
| 28 | { name: "Games", way: "cannot", words: `A game server ${NEEDS_SERVER}; a tab sleeps and has no address. ${HOSTED}` }, | |
| 29 | { name: "Infrastructure", way: "cannot", words: `Cloud machines and their cost are a server's business, and a tab holds none. ${HOSTED}` }, | |
| 30 | { name: "Explore", way: "cannot", words: `Ranking other people's apps and forges needs the network between forges, which this tab keeps shut. ${HOSTED}` }, | |
| 31 | { name: "Packs", way: "cannot", words: `Packs are installed on a server. ${HOSTED}` }, | |
| 32 | { name: "Front page", way: "cannot", words: `It is the public forge's own front page, which this tab does not have. ${HOSTED}` }, | |
| 33 | { name: "Search", way: "cannot", words: `Search runs over an index a server keeps of every repository and conversation; a tab holds one small forge and no index. Read the issue list here, or search in the hosted forge.` }, | |
| 34 | { name: "Feed", way: "cannot", words: `The feed is the crew's and other people's posts; with no crew and no one else here it would be empty. ${HOSTED}` }, | |
| 35 | { name: "Board", way: "differs", at: "/", words: "There is no board of the crew's work; an app you make here can be a board of cards (say \"a board for my bugs\")." }, | |
| 36 | { name: "Boards", way: "cannot", words: `Boards for a team ${NEEDS_SERVER}. ${HOSTED}` }, | |
| 37 | { name: "Flow", way: "cannot", words: `Flow follows the crew's work through git, which a tab does not have. ${HOSTED}` }, | |
| 38 | { name: "Waiting", way: "cannot", words: `Waiting lists what the crew and other people wait on you for; this forge has neither. ${HOSTED}` }, | |
| 39 | { name: "Notes", way: "differs", at: "/", words: "Notes here are an app you make from a sentence (say \"my notes\"), kept in this browser." }, | |
| 40 | { name: "Talk", way: "cannot", words: `Talking to the forge ${NEEDS_CREW}. The optional model on the home page only fills an app's spec. ${HOSTED}` }, | |
| 41 | { name: "Next up", way: "cannot", words: `Next up ranks the crew's work, which ${NEEDS_CREW}. ${HOSTED}` }, | |
| 42 | { name: "Map", way: "cannot", words: `The map of what waits for what ${NEEDS_CREW}. ${HOSTED}` }, | |
| 43 | { name: "Archive", way: "cannot", words: `Nothing is archived here: removing an app asks for its title and is final, so export it first. Archives of finished work are kept by a server. ${HOSTED}` }, | |
| 44 | { name: "Displays", way: "cannot", words: `Displays are shown on other screens, so they ${NEEDS_SERVER}. ${HOSTED}` }, | |
| 45 | { name: "Settings", way: "differs", at: "/storage", words: "There is no account to set up; what can be set is how this tab keeps its data, on this page." }, | |
| 46 | { name: "System", way: "cannot", words: `It reports a server's health; a tab is not one. ${HOSTED}` }, | |
| 47 | { name: "Releases", way: "cannot", words: `Releases are of a server's own software. ${HOSTED}` }, | |
| 48 | { name: "Forges", way: "cannot", words: `Making forges for other people ${NEEDS_SERVER}. ${HOSTED}` }, | |
| 49 | { name: "Family", way: "cannot", words: `A family of forges ${NEEDS_SERVER}. ${HOSTED}` }, | |
| 50 | { name: "Audit", way: "cannot", words: `The audit trail is kept by a server for every account on it; this forge has one person. ${HOSTED}` }, | |
| 51 | { name: "Runners", way: "cannot", words: "This tab is not a runner yet: the switch arrives with the tab runner of #18257. Its Runners page, in the menu, says what a tab will and will not run; until then add runners in the hosted forge." }, | |
| 52 | ]; | |
| 53 | ||
| 54 | /** What a tab can and cannot run as a runner, said on the page and in the terminal. */ | |
| 55 | export const RUNNER_CAN = "A tab can run JavaScript and WebAssembly in a worker, with the browser's memory and a CPU it shares with you."; | |
| 56 | export const RUNNER_CANNOT = "A tab cannot run git, a shell, native programs or installs, cannot listen for connections, and stops when the tab is closed or the device sleeps."; | |
| 57 | export const RUNNER_STATE = "This tab is not a runner yet: the switch arrives with the tab runner of #18257, and a switch that did nothing would be worse than none."; | |
| 58 | ||
| 59 | export const WAY_LABEL: Readonly<Record<Way, string>> = { works: "Works", differs: "Works differently", cannot: "Cannot work in a tab" }; |
src/tab/forge.ts +15
| @@ -8,6 +8,7 @@ import { appsHome, handleApps, migrateApps } from "./apps.ts"; | ||
| 8 | 8 | import { esc } from "./escape.ts"; |
| 9 | 9 | import { ROOT } from "./render.ts"; |
| 10 | 10 | import { shell } from "./shell.ts"; |
| 11 | import { pagesPage, runnersPage, storagePage, withKept } from "./whole.ts"; | |
| 11 | 12 | import { BODY_MAX, makeSpec, NAME_MAX, parseSpec, RECORDS_CAP, SPEC_MAX, TITLE_MAX } from "./spec.ts"; |
| 12 | 13 | |
| 13 | 14 | /** The two things it asks of a database. */ |
| @@ -23,6 +24,10 @@ export interface TabRequest { | ||
| 23 | 24 | /** The query string without its `?`. */ |
| 24 | 25 | readonly query?: string; |
| 25 | 26 | readonly form: Readonly<Record<string, string>>; |
| 27 | /** Whether the browser promised to keep the storage, as the page found out (`whole.ts`). */ | |
| 28 | readonly kept?: string; | |
| 29 | /** Set by the page when a forge that ran here before opened on an empty store. */ | |
| 30 | readonly cleared?: boolean; | |
| 26 | 31 | } |
| 27 | 32 | |
| 28 | 33 | export interface TabReply { |
| @@ -34,6 +39,9 @@ export interface TabReply { | ||
| 34 | 39 | readonly location?: string; |
| 35 | 40 | } |
| 36 | 41 | |
| 42 | /** Whether the store holds no forge yet: a first start, or a store the browser cleared. */ | |
| 43 | export const unmade = (db: TabDb): boolean => db.all("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'app'").length === 0; | |
| 44 | ||
| 37 | 45 | export function migrate(db: TabDb): void { |
| 38 | 46 | db.run( |
| 39 | 47 | "CREATE TABLE IF NOT EXISTS issue (id INTEGER PRIMARY KEY, title TEXT NOT NULL, body TEXT NOT NULL, created INTEGER NOT NULL)", |
| @@ -67,6 +75,10 @@ const move = (issues: number): string => { | ||
| 67 | 75 | const FORM = `<form method="post" action="${ROOT}/issues"><label>Title <input name="title" maxlength="${TITLE_MAX}" required></label><label>Description <textarea name="body" maxlength="${BODY_MAX}"></textarea></label><button>Open issue</button></form>`; |
| 68 | 76 | |
| 69 | 77 | export function handle(db: TabDb, req: TabRequest, now = Date.now()): TabReply { |
| 78 | return withKept(route(db, req, now), req.kept, req.cleared === true); | |
| 79 | } | |
| 80 | ||
| 81 | function route(db: TabDb, req: TabRequest, now: number): TabReply { | |
| 70 | 82 | const path = req.path.replace(/\/+$/, "") || "/"; |
| 71 | 83 | const app = handleApps(db, req, path, now); |
| 72 | 84 | if (app !== null) return app; |
| @@ -81,6 +93,9 @@ export function handle(db: TabDb, req: TabRequest, now = Date.now()): TabReply { | ||
| 81 | 93 | const records = withRecords ? all.map((r) => ({ title: String(r.title), body: String(r.body) })) : undefined; |
| 82 | 94 | return { status: 200, html: makeSpec(appName(db), records), file: { type: "application/json", name: "app.json" } }; |
| 83 | 95 | } |
| 96 | if (path === "/pages") return pagesPage(); | |
| 97 | if (path === "/runners") return runnersPage(); | |
| 98 | if (path === "/storage") return storagePage(req.kept); | |
| 84 | 99 | if (path === "/issues") return shell("Issues", `${list(rows(db, 100))}${FORM}`); |
| 85 | 100 | const one = /^\/issues\/(\d{1,9})$/.exec(path); |
| 86 | 101 | if (one !== null) { |
src/tab/restore.ts added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | /** | |
| 2 | * What a reload, a reopened tab or a second tab finds (#18259): whether the forge starts by itself, | |
| 3 | * which page it opens on, and whether the browser promised to keep the storage. Plain data in, | |
| 4 | * plain data out; `boot.ts` holds the browser's `localStorage` and only asks these. | |
| 5 | */ | |
| 6 | ||
| 7 | /** A page of this forge that may be remembered: never another path, whatever storage holds. */ | |
| 8 | const FORGE_PATH = /^\/tab\/f\/(?!.*\.\.)[\w/.-]{0,120}$/; | |
| 9 | ||
| 10 | /** The page to open on: the last one read if it is one of this forge's, else its home. */ | |
| 11 | export const openOn = (last: string | null): string => (last !== null && FORGE_PATH.test(last) ? last : "/tab/f/"); | |
| 12 | ||
| 13 | /** Whether a load starts the forge by itself: only when this browser started it before. */ | |
| 14 | export const startsItself = (on: string | null): boolean => on === "1"; | |
| 15 | ||
| 16 | interface Storage { | |
| 17 | persisted?(): Promise<boolean>; | |
| 18 | persist?(): Promise<boolean>; | |
| 19 | } | |
| 20 | ||
| 21 | /** | |
| 22 | * Whether the browser keeps the storage. It is asked to (`persist`, which some browsers answer | |
| 23 | * with a prompt) only on a first Start, which is a press; a start by itself only reads the answer. | |
| 24 | */ | |
| 25 | export async function keepStorage(storage: Storage | undefined, ask: boolean): Promise<"persistent" | "best-effort"> { | |
| 26 | const no = (): boolean => false; | |
| 27 | if (await storage?.persisted?.().catch(no)) return "persistent"; | |
| 28 | return ask && (await storage?.persist?.().catch(no)) ? "persistent" : "best-effort"; | |
| 29 | } |
src/tab/shell.ts +5 −2
| @@ -3,7 +3,10 @@ import type { TabReply } from "./forge.ts"; | ||
| 3 | 3 | import { esc } from "./escape.ts"; |
| 4 | 4 | import { pageOf, ROOT } from "./render.ts"; |
| 5 | 5 | |
| 6 | /** Where the shell leaves its one quiet line; `whole.ts` fills it. */ | |
| 7 | export const KEPT_MARK = "<!--kept-->"; | |
| 8 | ||
| 6 | 9 | export function shell(title: string, body: string, status = 200, heading = true): TabReply { |
| 7 | const nav = `<nav><a href="${ROOT}/">Home</a> <a href="${ROOT}/issues">Issues</a></nav>`; | |
| 8 | return { status, html: pageOf(title, `${heading ? `<h1>${esc(title)}</h1>` : ""}${body}`, nav) }; | |
| 10 | const nav = `<nav><a href="${ROOT}/">Home</a> <a href="${ROOT}/issues">Issues</a> <a href="${ROOT}/runners">Runners</a> <a href="${ROOT}/pages">What works</a></nav>`; | |
| 11 | return { status, html: pageOf(title, `${heading ? `<h1>${esc(title)}</h1>` : ""}${body}<footer class="kept muted">${KEPT_MARK}</footer>`, nav) }; | |
| 9 | 12 | } |
src/tab/whole.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** | |
| 2 | * The tab forge's pages about itself (#18259): what works here, the runner, and where its data | |
| 3 | * lives. Plain data in, a reply out; the shell's one quiet line is `keptLine`. | |
| 4 | */ | |
| 5 | import type { TabReply } from "./forge.ts"; | |
| 6 | import { COVERAGE, RUNNER_CAN, RUNNER_CANNOT, RUNNER_STATE, WAY_LABEL } from "./coverage.ts"; | |
| 7 | import { esc } from "./escape.ts"; | |
| 8 | import { ROOT } from "./render.ts"; | |
| 9 | import { KEPT_MARK, shell } from "./shell.ts"; | |
| 10 | ||
| 11 | /** Whether the browser promised to keep this forge's storage; unknown until the page asks. */ | |
| 12 | export type Kept = "persistent" | "best-effort"; | |
| 13 | ||
| 14 | const MOVE = `<a href="${ROOT}/app.json?records=1">Export</a> · <a href="${ROOT}/storage">Where it is kept</a> · <a href="/" target="_top">The hosted forge</a>`; | |
| 15 | ||
| 16 | /** The one quiet line: where the data is, how to keep it. */ | |
| 17 | function keptLine(kept: Kept | undefined, cleared: boolean): string { | |
| 18 | const how = kept === "persistent" ? "Kept: the browser promised." : kept === "best-effort" ? "The browser may clear it." : ""; | |
| 19 | const lost = cleared ? `<strong class="cleared">${CLEARED}</strong> ` : ""; | |
| 20 | return `${lost}This forge lives in this browser, on this device. ${how} ${MOVE}`; | |
| 21 | } | |
| 22 | ||
| 23 | /** Said on every page when a forge that ran here before came back empty. */ | |
| 24 | const CLEARED = "The browser cleared this forge since it last ran: Import an export to bring it back."; | |
| 25 | ||
| 26 | /** The reply with its footer line filled in; a reply that is a file or a redirect has none. */ | |
| 27 | export function withKept(reply: TabReply, kept: unknown, cleared: boolean): TabReply { | |
| 28 | const known = kept === "persistent" || kept === "best-effort" ? kept : undefined; | |
| 29 | return reply.html.includes(KEPT_MARK) ? { ...reply, html: reply.html.replace(KEPT_MARK, keptLine(known, cleared)) } : reply; | |
| 30 | } | |
| 31 | ||
| 32 | export function pagesPage(): TabReply { | |
| 33 | const rows = COVERAGE.map((c) => { | |
| 34 | const name = c.at === undefined ? esc(c.name) : `<a href="${ROOT}${c.at}">${esc(c.name)}</a>`; | |
| 35 | return `<li class="way-${c.way}"><span class="way">${esc(WAY_LABEL[c.way])}</span> <strong>${name}</strong><span class="why muted">${esc(c.words)}</span></li>`; | |
| 36 | }); | |
| 37 | return shell("What works in this tab", `<p>Each page of the hosted forge, and what it is here.</p><ul class="items pages">${rows.join("")}</ul>`); | |
| 38 | } | |
| 39 | ||
| 40 | export function runnersPage(): TabReply { | |
| 41 | return shell( | |
| 42 | "Runners", | |
| 43 | `<p><strong>${esc(RUNNER_STATE)}</strong></p><h2>What a tab can run</h2><p>${esc(RUNNER_CAN)}</p><h2>What it cannot</h2><p>${esc(RUNNER_CANNOT)}</p><p class="muted">Open this forge in a browser on a spare machine, sign in on your hosted forge and switch it on: that is where this is going. Until then the hosted forge's own Runners page is where runners are added.</p>`, | |
| 44 | ); | |
| 45 | } | |
| 46 | ||
| 47 | export function storagePage(kept: unknown): TabReply { | |
| 48 | const state = kept === "persistent" ? "The browser has promised to keep it until you clear this site's data." : kept === "best-effort" ? "The browser has not promised: it may clear this forge when the device is short of space." : "This page could not ask the browser whether it promised to keep it."; | |
| 49 | return shell( | |
| 50 | "Where this forge is kept", | |
| 51 | `<p>In this browser's private file storage (OPFS, a directory called forge-tab), on this device only. Nothing is kept by the forge that served the page.</p><h2>Will it survive?</h2><p>Reloading, closing the tab and opening it again, and a second tab, all find it where you left it; a second tab on the same forge cannot open the store while the first holds it, and says so. ${esc(state)} Clearing this site's data in the browser deletes it.</p><p>When this page starts the forge it asks the browser to keep its storage; that is the only protection a page can ask for.</p><h2>Keep a copy, or move</h2><p><a href="${ROOT}/app.json?records=1">Download the app and its issues</a>. One file that any forge in a tab opens (Import on the home page). A hosted forge does not read it yet; each app's <em>Get a link</em> sends a read-only copy to the hosted forge.</p>`, | |
| 52 | ); | |
| 53 | } |
src/tab/words.ts +7 −1
| @@ -35,5 +35,11 @@ export const TAB_MODEL: readonly string[] = [ | ||
| 35 | 35 | "Your sentence and its answer never leave this device, and this forge's own models are not used.", |
| 36 | 36 | ]; |
| 37 | 37 | |
| 38 | /** | |
| 39 | * What the forge in the tab is once it runs: the whole tab, honest about its edges (#18259). The | |
| 40 | * pages it has, the ones it cannot have and the runner are listed in `coverage.ts`. | |
| 41 | */ | |
| 42 | export const TAB_WHOLE = "Once started it fills the tab. Its What works page names every page of the hosted forge and says whether it works here, works differently or cannot work in a tab (and why); its Runners page says what a tab can and cannot run. It reopens where you were after a reload or a new tab, and asks the browser to keep its storage."; | |
| 43 | ||
| 38 | 44 | /** The terminal's one sentence: the facts and where the page is. */ |
| 39 | export const tabLine = (): string => `${TAB_FACTS.join(" ")} ${TAB_LEFT_OUT} ${TAB_MOVE} ${TAB_LINK} ${TAB_MODEL.join(" ")} Open /tab in a browser to start it and to make an app from a sentence.`; | |
| 45 | export const tabLine = (): string => `${TAB_FACTS.join(" ")} ${TAB_LEFT_OUT} ${TAB_MOVE} ${TAB_LINK} ${TAB_MODEL.join(" ")} ${TAB_WHOLE} Open /tab in a browser to start it and to make an app from a sentence.`; |
src/tab/worker.ts +5 −4
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | * `opfs-sahpool` VFS, which needs a dedicated worker and no cross-origin isolation). The page |
| 4 | 4 | * starts it with the URL of the vendored module; it answers each request on the port it came with. |
| 5 | 5 | */ |
| 6 | import { handle, migrate, type TabDb, type TabReply, type TabRequest } from "./forge.ts"; | |
| 6 | import { handle, migrate, unmade, type TabDb, type TabReply, type TabRequest } from "./forge.ts"; | |
| 7 | 7 | |
| 8 | 8 | interface Sqlite { |
| 9 | 9 | installOpfsSAHPoolVfs(options: { name: string; directory: string }): Promise<{ OpfsSAHPoolDb: new (path: string) => Oo1 }>; |
| @@ -16,7 +16,7 @@ declare const self: { onmessage: ((event: MessageEvent<{ start?: string; req?: T | ||
| 16 | 16 | |
| 17 | 17 | let db: TabDb | undefined; |
| 18 | 18 | |
| 19 | async function open(moduleUrl: string): Promise<void> { | |
| 19 | async function open(moduleUrl: string): Promise<boolean> { | |
| 20 | 20 | const init = (await import(/* @vite-ignore */ moduleUrl)).default as () => Promise<Sqlite>; |
| 21 | 21 | const pool = await (await init()).installOpfsSAHPoolVfs({ name: "forge-tab", directory: "/forge-tab" }); |
| 22 | 22 | const file = new pool.OpfsSAHPoolDb("/forge.db"); |
| @@ -25,7 +25,9 @@ async function open(moduleUrl: string): Promise<void> { | ||
| 25 | 25 | all: (sql, bind) => |
| 26 | 26 | file.exec({ sql, ...(bind === undefined ? {} : { bind }), rowMode: "object", returnValue: "resultRows" }) as Record<string, string | number>[], |
| 27 | 27 | }; |
| 28 | const empty = unmade(db); | |
| 28 | 29 | migrate(db); |
| 30 | return empty; | |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | self.onmessage = async (event) => { |
| @@ -33,8 +35,7 @@ self.onmessage = async (event) => { | ||
| 33 | 35 | const port = event.ports[0]; |
| 34 | 36 | try { |
| 35 | 37 | if (start !== undefined) { |
| 36 | await open(start); | |
| 37 | self.postMessage({ ready: true }); | |
| 38 | self.postMessage({ ready: true, empty: await open(start) }); | |
| 38 | 39 | } else if (req !== undefined && port !== undefined) { |
| 39 | 40 | const reply: TabReply = db === undefined ? { status: 503, html: "The database is not open." } : handle(db, req); |
| 40 | 41 | port.postMessage(reply); |
src/tui/screens/common.ts +28
| @@ -398,6 +398,34 @@ export function drawLines( | ||
| 398 | 398 | }); |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | /** | |
| 402 | * A scrolling card of plain text lines for a width: the first is the heading, the rest muted; esc | |
| 403 | * goes back. | |
| 404 | */ | |
| 405 | export function textScreen(ctx: Ctx, title: string, linesFor: (width: number) => string[]): Screen { | |
| 406 | const scroll = new Scroll(); | |
| 407 | let total = 0; | |
| 408 | return { | |
| 409 | title, | |
| 410 | keys: [binding(["j", "k"], "scroll"), binding(["esc", "q"], "back")], | |
| 411 | render(area, s) { | |
| 412 | const c = ctx.theme.c; | |
| 413 | const lines = linesFor(area.w - 4); | |
| 414 | total = lines.length; | |
| 415 | drawLines(s, area, c, scroll, lines.map((text, i) => [{ text, style: style(i === 0 ? c.fg : c.mutedFg, c.bg) }])); | |
| 416 | hitScroll(ctx, area, scroll, total); | |
| 417 | }, | |
| 418 | key(e) { | |
| 419 | if (scroll.key(e, total)) return true; | |
| 420 | if (e.key === "esc" || e.key === "q") { | |
| 421 | ctx.pop(); | |
| 422 | return true; | |
| 423 | } | |
| 424 | return false; | |
| 425 | }, | |
| 426 | }; | |
| 427 | } | |
| 428 | ||
| 401 | 429 | /** |
| 402 | 430 | * A label/value column — the "about" blocks every detail screen carries. |
| 403 | 431 | * |
src/tui/screens/maintainruns.ts +2 −25
| @@ -7,11 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | import { latest } from "../../maintain/runs.ts"; |
| 9 | 9 | import type { Ctx, Screen } from "../app/app.ts"; |
| 10 | import { binding } from "../app/keys.ts"; | |
| 11 | 10 | import type { Session } from "../session.ts"; |
| 12 | import { style } from "../term/style.ts"; | |
| 13 | 11 | import { pad } from "../term/width.ts"; |
| 14 | import { Scroll, drawLines, hitScroll } from "./common.ts"; | |
| 12 | import { textScreen } from "./common.ts"; | |
| 15 | 13 | import type { PaletteEntry } from "./entries.ts"; |
| 16 | 14 | |
| 17 | 15 | const mayRead = (session: Session): boolean => session.user?.isAdmin === true; |
| @@ -27,28 +25,7 @@ export function runLines(session: Session, limit = 50): string[] { | ||
| 27 | 25 | return [head, ...rows]; |
| 28 | 26 | } |
| 29 | 27 | |
| 30 | export function maintainRunsScreen(session: Session, ctx: Ctx): Screen { | |
| 31 | const scroll = new Scroll(); | |
| 32 | return { | |
| 33 | title: "Maintenance duty runs", | |
| 34 | keys: [binding(["j", "k"], "scroll"), binding(["esc", "q"], "back")], | |
| 35 | render(area, s) { | |
| 36 | const c = ctx.theme.c; | |
| 37 | const lines = runLines(session); | |
| 38 | const spans = lines.map((text, i) => [{ text, style: style(i === 0 ? c.fg : c.mutedFg, c.bg) }]); | |
| 39 | drawLines(s, area, c, scroll, spans); | |
| 40 | hitScroll(ctx, area, scroll, lines.length); | |
| 41 | }, | |
| 42 | key(e) { | |
| 43 | if (scroll.key(e, runLines(session).length)) return true; | |
| 44 | if (e.key === "esc" || e.key === "q") { | |
| 45 | ctx.pop(); | |
| 46 | return true; | |
| 47 | } | |
| 48 | return false; | |
| 49 | }, | |
| 50 | }; | |
| 51 | } | |
| 28 | export const maintainRunsScreen = (session: Session, ctx: Ctx): Screen => textScreen(ctx, "Maintenance duty runs", () => runLines(session)); | |
| 52 | 29 | |
| 53 | 30 | export const entry: PaletteEntry = { |
| 54 | 31 | rank: 290, |
src/tui/screens/maintenanceweek.ts +2 −24
| @@ -9,11 +9,9 @@ import { telemetryReader } from "../../actions/telemetry.ts"; | ||
| 9 | 9 | import { duties } from "../../maintain/registry.ts"; |
| 10 | 10 | import { resultWords, weekRows, weekWords } from "../../maintain/week_line.ts"; |
| 11 | 11 | import type { Ctx, Screen } from "../app/app.ts"; |
| 12 | import { binding } from "../app/keys.ts"; | |
| 13 | 12 | import type { Session } from "../session.ts"; |
| 14 | import { style } from "../term/style.ts"; | |
| 15 | 13 | import { pad } from "../term/width.ts"; |
| 16 | import { Scroll, drawLines, hitScroll } from "./common.ts"; | |
| 14 | import { textScreen } from "./common.ts"; | |
| 17 | 15 | import type { PaletteEntry } from "./entries.ts"; |
| 18 | 16 | |
| 19 | 17 | const mayRead = (session: Session): boolean => telemetryReader(session.app, session.user) !== undefined; |
| @@ -33,27 +31,7 @@ export function weekLines(session: Session, now = new Date()): string[] { | ||
| 33 | 31 | ]; |
| 34 | 32 | } |
| 35 | 33 | |
| 36 | export function weekScreen(session: Session, ctx: Ctx): Screen { | |
| 37 | const scroll = new Scroll(); | |
| 38 | return { | |
| 39 | title: "Maintenance, last 7 days", | |
| 40 | keys: [binding(["j", "k"], "scroll"), binding(["esc", "q"], "back")], | |
| 41 | render(area, s) { | |
| 42 | const c = ctx.theme.c; | |
| 43 | const lines = weekLines(session); | |
| 44 | drawLines(s, area, c, scroll, lines.map((text, i) => [{ text, style: style(i === 0 ? c.fg : c.mutedFg, c.bg) }])); | |
| 45 | hitScroll(ctx, area, scroll, lines.length); | |
| 46 | }, | |
| 47 | key(e) { | |
| 48 | if (scroll.key(e, weekLines(session).length)) return true; | |
| 49 | if (e.key === "esc" || e.key === "q") { | |
| 50 | ctx.pop(); | |
| 51 | return true; | |
| 52 | } | |
| 53 | return false; | |
| 54 | }, | |
| 55 | }; | |
| 56 | } | |
| 34 | export const weekScreen = (session: Session, ctx: Ctx): Screen => textScreen(ctx, "Maintenance, last 7 days", () => weekLines(session)); | |
| 57 | 35 | |
| 58 | 36 | export const entry: PaletteEntry = { |
| 59 | 37 | rank: 150, |
src/tui/screens/tabworks.ts added +33
| @@ -0,0 +1,33 @@ | ||
| 1 | /** | |
| 2 | * What works in a forge in a browser tab, in the terminal (#18259): every hosted page with its | |
| 3 | * outcome and why, then what a tab can run. The words are `src/tab/coverage.ts`, the same the | |
| 4 | * tab's own pages print. | |
| 5 | */ | |
| 6 | ||
| 7 | import { wrap } from "../term/width.ts"; | |
| 8 | import { COVERAGE, RUNNER_CAN, RUNNER_CANNOT, RUNNER_STATE, WAY_LABEL } from "../../tab/coverage.ts"; | |
| 9 | import type { PaletteEntry } from "./entries.ts"; | |
| 10 | import { textScreen } from "./common.ts"; | |
| 11 | ||
| 12 | /** The list as text lines: label first, the page's name, its reason beneath. */ | |
| 13 | export function worksLines(width = 80): string[] { | |
| 14 | const w = Math.max(20, width); | |
| 15 | return [ | |
| 16 | "What works in a forge in a browser tab", | |
| 17 | "", | |
| 18 | ...COVERAGE.flatMap((c) => [`${WAY_LABEL[c.way]}: ${c.name}`, ...wrap(c.words, w - 2).map((l) => ` ${l}`), ""]), | |
| 19 | "Runners", | |
| 20 | ...wrap(RUNNER_STATE, w), | |
| 21 | ...wrap(RUNNER_CAN, w), | |
| 22 | ...wrap(RUNNER_CANNOT, w), | |
| 23 | ]; | |
| 24 | } | |
| 25 | ||
| 26 | export const entry: PaletteEntry = { | |
| 27 | rank: 911, | |
| 28 | id: "tab-forge-works", | |
| 29 | title: "What works in a forge in a tab", | |
| 30 | hint: "each hosted page: works, works differently, or cannot (and why); what a tab can run", | |
| 31 | offered: () => true, | |
| 32 | run: (_session, ctx) => ctx.push(textScreen(ctx, "What works in a tab", worksLines)), | |
| 33 | }; |
src/web/style/content.ts +5
| @@ -28,6 +28,11 @@ a[data-waves-link] { display: inline-flex; align-items: center; min-height: 44px | ||
| 28 | 28 | .clamp { margin: 0; } |
| 29 | 29 | .tab-frame { display: block; width: 100%; height: 60vh; min-height: 320px; margin-top: 24px; } |
| 30 | 30 | .tab-frame[hidden] { display: none; } |
| 31 | /* A forge running in the tab is the whole tab: the page's chrome steps aside and the frame takes the width and the height (#18259). */ | |
| 32 | html[data-running="1"] .skip, html[data-running="1"] .topbar, html[data-running="1"] .foot, html[data-running="1"] body > a[data-palette], | |
| 33 | html[data-running="1"] #main > :not(#tab-frame) { display: none; } | |
| 34 | html[data-running="1"] #main { max-width: none; padding: 0; margin: 0; } | |
| 35 | html[data-running="1"] #tab-frame { height: max(100dvh, 320px); margin: 0; border: 0; } | |
| 31 | 36 | .clamp-l { display: block; cursor: pointer; } |
| 32 | 37 | .clamp-t { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; } |
| 33 | 38 | .clamp-x:checked ~ .clamp-t { display: block; } |
src/web/tab-asset.ts +1
| @@ -37,6 +37,7 @@ form{display:grid;gap:8px;margin-top:16px}label{display:grid;gap:4px}input,texta | ||
| 37 | 37 | .items form,.pair form{display:block;margin:0}.pair{display:flex;gap:8px}.add{grid-template-columns:1fr auto}.quiet{background:none;border-color:transparent;color:var(--muted-fg)}.tick{margin:0} |
| 38 | 38 | .big{font-size:20px;font-weight:700;margin:4px 0}.note{white-space:pre-wrap;margin:12px 0}.field,.act{display:inline-block;color:var(--muted-fg)} |
| 39 | 39 | .lanes{display:grid;gap:16px;margin-top:12px}.lane h3{font-size:14px;margin:0}.spec{overflow:auto;font-size:13px} |
| 40 | .kept{margin-top:32px;font-size:13px}.cleared{font-weight:600;color:var(--danger)}.pages{display:block}.pages li{display:block;padding:8px 0}.way{font-size:13px;font-weight:600;margin-right:6px}.why{display:block} | |
| 40 | 41 | .remove{margin-top:32px}button.danger{background:var(--danger);border-color:var(--danger);color:var(--bg)}`; |
| 41 | 42 | |
| 42 | 43 | /** The worker runs wasm, fetches the module and its binary from this origin, and nothing else. */ |
test/palette-roster.test.ts +4
| @@ -260,6 +260,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 260 | 260 | ["domains", "Failure domains", "failure domains and their last drill"], |
| 261 | 261 | ["feed", "Feed", "what happened, ranked; r replies on a post about an issue or pull, l likes, h who liked, p its permalink, o a run's log"], |
| 262 | 262 | ["tab-forge", "A forge in a browser tab", "where it lives, when it stops, who can reach it, make an app from a sentence"], |
| 263 | ["tab-forge-works", "What works in a forge in a tab", "each hosted page: works, works differently, or cannot (and why); what a tab can run"], | |
| 263 | 264 | ["feature-review", "Menu entries to review", "absorbed apps whose declared name or page changed: accept them"], |
| 264 | 265 | ["feature-feed", "Feature feeds", "the feed items about an absorbed app, as Settings › Features shows them"], |
| 265 | 266 | ["features", "Features", "apps that declare a feature: absorb one, change its settings"], |
| @@ -389,6 +390,7 @@ test("a site-admin agent is offered the roster without the person-only entries", | ||
| 389 | 390 | ["design-gallery", "Component gallery", "every component the design names"], |
| 390 | 391 | ["feed", "Feed", "what happened, ranked; r replies on a post about an issue or pull, l likes, h who liked, p its permalink, o a run's log"], |
| 391 | 392 | ["tab-forge", "A forge in a browser tab", "where it lives, when it stops, who can reach it, make an app from a sentence"], |
| 393 | ["tab-forge-works", "What works in a forge in a tab", "each hosted page: works, works differently, or cannot (and why); what a tab can run"], | |
| 392 | 394 | ["feature-feed", "Feature feeds", "the feed items about an absorbed app, as Settings › Features shows them"], |
| 393 | 395 | ["front", "Front page", "the apps a first visitor sees, ranked; an admin pins or hides"], |
| 394 | 396 | ["forges", "Forges you sent", "each forge sent, claimed or not, and its apps"], |
| @@ -521,6 +523,7 @@ test("a member person is offered the signed-in roster without the admin entries" | ||
| 521 | 523 | ["design-gallery", "Component gallery", "every component the design names"], |
| 522 | 524 | ["feed", "Feed", "what happened, ranked; r replies on a post about an issue or pull, l likes, h who liked, p its permalink, o a run's log"], |
| 523 | 525 | ["tab-forge", "A forge in a browser tab", "where it lives, when it stops, who can reach it, make an app from a sentence"], |
| 526 | ["tab-forge-works", "What works in a forge in a tab", "each hosted page: works, works differently, or cannot (and why); what a tab can run"], | |
| 524 | 527 | ["feature-feed", "Feature feeds", "the feed items about an absorbed app, as Settings › Features shows them"], |
| 525 | 528 | ["front", "Front page", "the apps a first visitor sees, ranked; an admin pins or hides"], |
| 526 | 529 | ["origin-ahead", "New commits upstream", "mirrors whose origin has moved on: take them"], |
| @@ -590,6 +593,7 @@ test("signed out, the palette offers sign-in and the public entries", () => { | ||
| 590 | 593 | ["apps-crew", "What the crew is doing", "one plain sentence per app: who is on what"], |
| 591 | 594 | ["design-gallery", "Component gallery", "every component the design names"], |
| 592 | 595 | ["tab-forge", "A forge in a browser tab", "where it lives, when it stops, who can reach it, make an app from a sentence"], |
| 596 | ["tab-forge-works", "What works in a forge in a tab", "each hosted page: works, works differently, or cannot (and why); what a tab can run"], | |
| 593 | 597 | ["front", "Front page", "the apps a first visitor sees, ranked; an admin pins or hides"], |
| 594 | 598 | ["origin-ahead", "New commits upstream", "mirrors whose origin has moved on: take them"], |
| 595 | 599 | ["security", "Security", "what the forge enforces for everyone, and how to report a problem"], |
test/support/tab-db.ts +3 −3
| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | import { Database } from "bun:sqlite"; |
| 2 | 2 | import { migrate, type TabDb } from "../../src/tab/forge.ts"; |
| 3 | 3 | |
| 4 | /** A fresh in-memory tab forge database, migrated. */ | |
| 5 | export const open = (): TabDb => { | |
| 4 | /** A fresh in-memory tab forge database, migrated unless asked not to. */ | |
| 5 | export const open = (migrated = true): TabDb => { | |
| 6 | 6 | const db = new Database(":memory:"); |
| 7 | 7 | const tab: TabDb = { |
| 8 | 8 | run: (sql, bind) => void db.query(sql).run(...(bind ?? [])), |
| 9 | 9 | all: (sql, bind) => db.query(sql).all(...(bind ?? [])) as Record<string, string | number>[], |
| 10 | 10 | }; |
| 11 | migrate(tab); | |
| 11 | if (migrated) migrate(tab); | |
| 12 | 12 | return tab; |
| 13 | 13 | }; |
test/tab-whole.test.ts added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | /** | |
| 2 | * The forge in a tab as the whole tab (#18259): every hosted page is named, nothing is dead, it | |
| 3 | * says where it is kept. | |
| 4 | */ | |
| 5 | import { expect, test } from "bun:test"; | |
| 6 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp } from "../src/app.ts"; | |
| 10 | import type { Screen } from "../src/tui/app/app.ts"; | |
| 11 | import { Session } from "../src/tui/session.ts"; | |
| 12 | import { quietCtx, screenText } from "./helpers/tui-text.ts"; | |
| 13 | import { COVERAGE, RUNNER_STATE } from "../src/tab/coverage.ts"; | |
| 14 | import { entry as tabWorks, worksLines } from "../src/tui/screens/tabworks.ts"; | |
| 15 | import { handle, migrate, unmade, type TabDb, type TabRequest } from "../src/tab/forge.ts"; | |
| 16 | import { keepStorage, openOn, startsItself } from "../src/tab/restore.ts"; | |
| 17 | import { tabLine } from "../src/tab/words.ts"; | |
| 18 | import { GO_PAGES } from "../src/web/go_pages.ts"; | |
| 19 | import { open as db } from "./support/tab-db.ts"; | |
| 20 | ||
| 21 | const get = (d: TabDb, path: string, kept?: string, cleared?: boolean) => | |
| 22 | handle(d, { method: "GET", path, form: {}, ...(kept === undefined ? {} : { kept }), ...(cleared === undefined ? {} : { cleared }) } satisfies TabRequest); | |
| 23 | ||
| 24 | test("every top-level page of the hosted forge is named, with a reason unless it works", () => { | |
| 25 | for (const p of GO_PAGES) { | |
| 26 | const c = COVERAGE.find((x) => x.name === p.name); | |
| 27 | expect(c, `${p.name} is not named in src/tab/coverage.ts`).toBeDefined(); | |
| 28 | expect(c!.words.length).toBeGreaterThan(20); | |
| 29 | } | |
| 30 | for (const c of COVERAGE) if (c.way === "cannot") expect(c.words).toContain("hosted forge"); | |
| 31 | }); | |
| 32 | ||
| 33 | test("a page that has a place in the tab answers, and the pages that do not are not linked", () => { | |
| 34 | const d = db(); | |
| 35 | for (const c of COVERAGE) if (c.at !== undefined) expect(get(d, c.at).status).toBe(200); | |
| 36 | const html = get(d, "/pages").html; | |
| 37 | expect(html.match(/<li class="[^"]*"><span class="way">[^<]*<\/span> <strong><a /g)?.length).toBe(COVERAGE.filter((c) => c.at !== undefined).length); | |
| 38 | expect(html).toContain("Cannot work in a tab"); | |
| 39 | }); | |
| 40 | ||
| 41 | test("the runners page has no switch and says what a tab can and cannot run", () => { | |
| 42 | const html = get(db(), "/runners").html; | |
| 43 | expect(html).toContain("#18257"); | |
| 44 | expect(html).toContain("cannot run git"); | |
| 45 | expect(html).not.toMatch(/<button|<input/); | |
| 46 | }); | |
| 47 | ||
| 48 | test("every page carries the quiet line, filled from what the browser promised", () => { | |
| 49 | const d = db(); | |
| 50 | expect(get(d, "/").html).toContain("lives in this browser"); | |
| 51 | expect(get(d, "/", "persistent").html).toContain("the browser promised"); | |
| 52 | expect(get(d, "/", "best-effort").html).toContain("may clear it"); | |
| 53 | expect(get(d, "/", "<script>").html).not.toContain("<script>"); | |
| 54 | expect(get(d, "/storage", "best-effort").html).toContain("has not promised"); | |
| 55 | expect(get(d, "/issues").html).not.toContain("<!--kept-->"); | |
| 56 | // The way back to the hosted forge leaves the frame. | |
| 57 | expect(get(d, "/").html).toContain('<a href="/" target="_top">'); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a load starts the forge by itself only when this browser started it, and a cleared store is said", () => { | |
| 61 | expect(startsItself("1")).toBe(true); | |
| 62 | for (const no of [null, "", "0", "true"]) expect(startsItself(no)).toBe(false); | |
| 63 | const d = db(); | |
| 64 | expect(get(d, "/").html).not.toContain("cleared this forge"); | |
| 65 | expect(get(d, "/", undefined, true).html).toContain("cleared this forge"); | |
| 66 | expect(get(d, "/issues", undefined, true).html).toContain("cleared this forge"); | |
| 67 | }); | |
| 68 | ||
| 69 | test("an empty store reads as unmade until the forge is migrated into it", () => { | |
| 70 | const d = db(false); | |
| 71 | expect(unmade(d)).toBe(true); | |
| 72 | migrate(d); | |
| 73 | expect(unmade(d)).toBe(false); | |
| 74 | }); | |
| 75 | ||
| 76 | test("the terminal says the same", () => { | |
| 77 | expect(tabLine()).toContain("fills the tab"); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a reload opens the forge on the last forge page, and never on a path storage was made to hold", async () => { | |
| 81 | expect(openOn("/tab/f/issues/3")).toBe("/tab/f/issues/3"); | |
| 82 | expect(openOn(null)).toBe("/tab/f/"); | |
| 83 | // Storage a page can write is not trusted: another path, another origin, a climb out of the | |
| 84 | // forge. | |
| 85 | for (const bad of ["/tab", "https://evil.example/tab/f/x", "//evil.example", "/tab/f/../../x", "/tab/f/a b", "javascript:alert(1)"]) expect(openOn(bad)).toBe("/tab/f/"); | |
| 86 | // The frame remembers a page only when opening on it would give the same page back. | |
| 87 | expect(openOn("/tab/f/pages") === "/tab/f/pages").toBe(true); | |
| 88 | expect(openOn("/elsewhere") === "/elsewhere").toBe(false); | |
| 89 | }); | |
| 90 | ||
| 91 | test("the browser's promise to keep storage is asked on the first Start only and read as persistent or best-effort", async () => { | |
| 92 | const calls: string[] = []; | |
| 93 | const store = (persisted: boolean, persist: boolean | "throws") => ({ | |
| 94 | persisted: async () => (calls.push("persisted"), persisted), | |
| 95 | persist: async () => (calls.push("persist"), persist === "throws" ? Promise.reject(new Error("no")) : persist), | |
| 96 | }); | |
| 97 | expect(await keepStorage(store(true, false), true)).toBe("persistent"); | |
| 98 | expect(calls).toEqual(["persisted"]); | |
| 99 | expect(await keepStorage(store(false, true), true)).toBe("persistent"); | |
| 100 | expect(await keepStorage(store(false, false), true)).toBe("best-effort"); | |
| 101 | expect(await keepStorage(store(false, "throws"), true)).toBe("best-effort"); | |
| 102 | expect(await keepStorage(undefined, true)).toBe("best-effort"); | |
| 103 | // A start by itself (no press) reads the answer and never asks: asking may prompt. | |
| 104 | calls.length = 0; | |
| 105 | expect(await keepStorage(store(false, true), false)).toBe("best-effort"); | |
| 106 | expect(calls).toEqual(["persisted"]); | |
| 107 | }); | |
| 108 | ||
| 109 | test("the terminal lists every hosted page with its outcome and the runner words", () => { | |
| 110 | const text = worksLines(60).join("\n"); | |
| 111 | for (const p of GO_PAGES) expect(text).toContain(p.name); | |
| 112 | expect(text).toContain("Cannot work in a tab: Games"); | |
| 113 | expect(text).toContain(RUNNER_STATE.slice(0, 40)); | |
| 114 | expect(text).toContain("cannot run git"); | |
| 115 | expect(worksLines(60).every((l) => l.length <= 62)).toBe(true); | |
| 116 | const dir = mkdtempSync(join(tmpdir(), "forge-tabworks-")); | |
| 117 | const app = createApp({ dataDir: dir, port: 0 }); | |
| 118 | try { | |
| 119 | const pushed: Screen[] = []; | |
| 120 | tabWorks.run(new Session(app, { sshUsername: "guest", remoteAddress: "127.0.0.1" }), { ...quietCtx(), push: (screen) => void pushed.push(screen) }); | |
| 121 | expect(pushed.map((x) => x.title)).toEqual(["What works in a tab"]); | |
| 122 | expect(screenText(pushed[0]!, 80, 12)).toContain("What works in a forge in a browser tab"); | |
| 123 | } finally { | |
| 124 | app.close(); | |
| 125 | rmSync(dir, { recursive: true, force: true }); | |
| 126 | } | |
| 127 | }); |
new above
offline
Box 1: still
#tab-frame, an iframe over /tab. The forge must be the tab's own page with the hosted shell, no frame.