// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The page's configuration. The kernel is the page's own (sdk.ts): the base // bundle unkernel.wasm, with the distro image ?distro= picks // (virtual:unkernel-kernel's list) attached read-only. // // The settings below are kept in localStorage (SETTINGS_KEY: {name: value}, // only those that differ from the defaults). Each one except ?simulate, // ?showcase, ?routes and ?autohide has a control in Settings (Options.tsx); Reboot saves // them there (ui.ts nextSettings()) and loads the page's bare URL. A URL // parameter of the same name overrides the saved one for that load (links, // the browser tests); the page never writes the URL. ?simulate, ?showcase, // ?routes and ?autohide are read from the URL only. // // ?mem=MiB kernel memory (default: auto, from the SDK's autoMemoryMiB(): // 512 on phones, 2048 otherwise, less on low-memory devices) // ?append= kernel command line // ?net=0 no network device (else: the Wisp server(s), default: // the project's) // ?wisp=[,...] | ?wisp=0 Wisp servers to race (0 = no network device) // ?tailscale=1 join a tailnet too (default off; with the network only): the // guest reaches the tailnet's 100.x addresses (the page leaves // the SDK's acceptRoutes as it is) through a Tailscale node on this page, its // WireGuard relayed over DERP WebSockets, and the peers reach // the guest; everything else still goes through Wisp (the SDK's // NetSpec.tailscale, tailscale.ts). Its code is a chunk of its // own, fetched only then. Without an auth key or a saved node, // the login is interactive (Settings, Network shows the link). // The auth key and the node's keys are never settings: they // stay in localStorage TAILSCALE_KEY (tailscale.ts), never in // the URL, each kept for the login server it belongs to and // used only with that one. Not with a ?tsserver= this page // cannot reach (tailscaleBlocker(): not http(s), or plain http // from an https page, which browsers block as mixed content). // Unlike other settings, the URL alone does not turn it on or // change its server: a ?tailscale=, ?tsserver= or // ?tsserverkey= that would differ from the saved settings // keeps Tailscale off this load (Config.tailscaleAsk; the options // show the saved ones), and the page asks whether to save the // URL's and reboot. // ?tsserver= its login (coordination) server, default Tailscale's // (headscale: its server_url) // ?tsserverkey=mkey: that server's public key, which the page cannot fetch from // headscale (its /key answers without CORS) // ?tshost= the node's host name (default the SDK's, unkernel) // ?routes=0 URL only: plain HTTP to the hosts of DEFAULT_ROUTES goes // through Wisp too (default: answered with fetch() from // their CORS mirrors, the SDK's fetchRoutes(); net.ts) // ?jit=0 kwemu interprets only (default: its JIT tier compiles hot // guest code to WebAssembly; counters in KW.jitStats()) // ?verbose=1 write every kernel message to the terminal, in gray // (default: kernel messages go to the Kernel log tab, and // during boot the newest one shows on the transient line) // ?data=0 no data disk. By default (when IndexedDB exists) the data // disk, an ext4 fs labelled kwdata, is attached as /dev/wda // and kept in OPFS in worker mode, else in IndexedDB // (?datastore=; data.ts); the guest's root is an // overlay whose changes live on it. Its template is the // kernel bundle's section unkernel.datadisk. One tab at a // time: another tab gets an unsaved in-memory copy (IndexedDB) // or no data disk (OPFS). // KW.exportData(), KW.importData(file), KW.resetData() (also // buttons in Settings). Hiding or leaving the page and // Reboot ask the guest to sync first (KW.sync(), the SDK's // sync()). // ?datastore=idb|opfs this load only: where the data disk is kept. Default: opfs // (the SDK's OpfsDisk, extent files in the origin private // file system, written by the kernel worker with synchronous // handles) in worker mode where OPFS works (a secure context, // navigator.storage.getDirectory), else idb (IndexedDB, the // only store on the page's thread, ?worker=0, and on plain // http). A saved IndexedDB disk is offered for migration // once (boot.ts, Data disk migration). // ?datasize= the data disk's size, 4 (the default, the template's) to // 1024: a new disk is made this large, a saved one grows to // it at the next boot without a suspended session to resume // (it never shrinks), and the guest's /init grows /data to // fill it. The browser's storage is used only as /data fills; // the size is clamped to what the disk can fill: the browser's // quota for the page less the site's other data (Settings, // Storage: Disk size, any whole GiB up to that; dataSizeCap()). // ?display=0 no display. By default the guest gets /dev/fb0 (kwfb, // 640x480), and a canvas over the terminal shows it while a // program has it open (try `glimmer`). Nothing is // allocated until then. // ?sound=0 no sound card. By default the guest gets ALSA hw:0,0 and // /dev/dsp (kwsnd, playback), played with Web Audio // (js/web/audio.ts; try `glimmer`). Nothing runs until a // program plays; what ended before the first tap or key // press (the last 10 s) plays then. // ?mic=0 no capture on the sound card. By default (with the sound // card) hw:0,0 also records (/dev/snd/pcmC0D0c, /dev/dsp // reads) from the microphone (js/web/audio-in.ts; try // `arecord` from alsa-utils): the browser asks for it only // when a guest program starts recording, and lets it go // when the program closes the device; silence until then // and when refused. // ?cam=0 no camera. By default the guest has /dev/video0 (kwcam, // V4L2: YUYV and NV12 up to 1280x720 at 30 fps, and H.264 // that the browser encodes where its VideoEncoder can) showing // the user's camera (js/web/camera.ts; try `v4l2-ctl` from // v4l-utils, ffmpeg, or a browser in the guest): the browser // asks for it only when a guest program starts streaming, // and lets it go when the program stops; black frames until // then and when refused. A resumed session keeps the camera // it was saved with. // ?distro= | ?distro=none the distro image to boot (default debian, else the first; // an unknown name boots the default and says so; none: no // image, the base shell). Its changes to the root // live in /data/distros// on the data disk. // ?gl=0 | ?gl=1 guest GL (Debian only; default on where the display is, // ?display=0 turns it off unless ?gl=1): the guest's Mesa // renders through this browser's WebGL2 (virglrenderer // compiled to wasm, on the page's thread; the SDK's // createKernel({ gl }), .agents/gl.md). It also gives the // guest a virtio-gpu device (createKernel({ gpu }): /dev/dri, // where stock Mesa finds its virgl driver with no variables, // and KMS clients show on the display), and adds kw.gl=1 to // the command line, for which the guest's shells set // GALLIUM_DRIVER=virpipe and LIBGL_ALWAYS_SOFTWARE=1 (the // vtest link) only where there is no /dev/dri render node // (kw-gl.sh). Mesa is not in the image: apt-get install it. // virgl.mjs and virgl.wasm are fetched only on the guest's // first GL use (a program asking the device for its // capsets, or a vtest client), not at boot; before the boot // the SDK only tries a WebGL2 context (a few ms) and offers // 3D on the device where it gets one. Off with another // distro, without WebGL2 on the page's thread, or in a build // without the renderer (glBlocker()). // ?vk=0 | ?vk=1 guest Vulkan (Debian only; default on where the page's // thread has WebGPU, navigator.gpu): the guest's Vulkan // loader finds the base's ICD (libvulkan_kw.so), which // talks over vsock port 1018 to a renderer on this // browser's WebGPU (the SDK's createKernel({ vk }), // .agents/vk.md). Nothing of it (the renderer's modules, // kwvk-naga.wasm) is fetched before the guest's first // Vulkan instance. Off with another distro, without // navigator.gpu, or in a build without kwvk-naga.wasm // (vkBlocker()). // ?gpu=0 with guest GL, this load only: no virtio-gpu device, so the // guest's shells use the vtest link (guest GL as it was // before the device, for comparison). Also none with // ?display=0. // ?shm=0 this load only: no kwshm device, so no Wayland desktop // (wayland.ts; the launcher's Wayland apps are disabled). // A resumed session keeps the device it was saved with. // ?va=0 this load only: no guest VA-API (default on, Debian only, // where the page's thread has WebCodecs' VideoDecoder): the // base's libva driver sends pictures over vsock port 1024 to // a VideoDecoder here and reads them back (the SDK's // createKernel({ va }), .agents/va.md). It finds libva's DRM // display on the virtio-gpu device, so it needs guest GL's // device (not with ?gpu=0). Nothing is asked of WebCodecs // before the guest's first connection. // ?va=cpu this load only: VA-API on, but video processing outputs // (mpv's --vo=dmabuf-wayland) are converted on the CPU from a // copy of each frame instead of drawn from the VideoFrame on // the GPU (VaOptions.upload "cpu"; compare vm.va.stats). // ?wlgpu=0 this load only: the Wayland desktop takes no GPU buffers // (no gpu source for wland: GL apps render in software, // llvmpipe over wl_shm, instead of by resource on the // page's renderer; for comparison). // ?glload=eager with guest GL, this load only: load the renderer before // the boot (the SDK's gl.eager), as the page did before it // loaded on first use. // ?glwait=strict with guest GL: the renderer answers the guest's busy waits // as vtest does, once WebGL tells that the fences have // retired (the SDK's gl.strictWaits), for comparing frame // times; the Guest GL row shows them. Default: at once where // safe (.agents/gl.md, Busy waits). // ?root=initramfs safe mode for this boot (kw.root=initramfs): the plain // image root instead of the overlay root; /data stays // mounted (Settings' Safe mode button; .agents/data-disk.md) // ?worker=1 run the kernel in a Web Worker (the SDK's // createKernel({ worker: true })), so it never blocks the // page. The data disk and the Wisp network are built in the // worker from specs; the display, input devices and sound // work as on the page's thread. Console and evdev input and // frames go through shared memory when the page is // cross-origin isolated (https or localhost; a plain-http // LAN page has no SharedArrayBuffer), else as messages, // which wait up to 20 ms while the guest is busy. Default: // on where the page is cross-origin isolated (a worker that // fails to start then reloads once on the page's thread, // boot.ts bootUpOnce()), else off; ?worker=0: the page's // thread, one CPU unless ?cpus= asks for more. // ?shared=0 in worker mode: messages even where shared memory works // (to compare the two) // ?cpus=N boot the SMP kernel on N CPUs (2 to 16, at most // navigator.hardwareConcurrency), each CPU in a Web Worker // of its own on one shared memory; implies ?worker=1 (CPU 0 // blocks while it waits for another CPU, which a page's main // thread may not). Needs the page's SMP kernel (sdk.ts // kernelSmpUrl: a base bundle with an SMP patch) and // cross-origin isolation (https or localhost); otherwise, or // when the SMP kernel fails to load or start, the page boots // on one CPU as usual (smpBlocker(), boot.ts). A session // suspended on N CPUs resumes on N (a load that boots // another count offers to reload on N: CPUS_ONCE_KEY). // Default in worker mode: the suggested count (half the // threads, at most SUGGESTED_CPUS: 4 on desktops, 2 in // Firefox and on phones; one on fewer than 4 threads), // else one. // ?cpus=1: one CPU, the UP kernel. // ?autosuspend=now||0 save the machine and pause it by itself once the tab has been // hidden that long (now: at once; seconds; 0: never); it goes // on (or, after the page was dropped, resumes) when the tab is // shown again (autosuspend.ts, boot.ts); only // a machine the user has used since it booted. Default: now // on phones and tablets (a background tab may be frozen or // discarded at any time), off elsewhere. // ?askleave=0 no question when the page is closed or left while such a // machine runs (beforeunload; staying suspends) // ?showcase[=warm,,...] URL only: a fast scripted tour of the apps with captions; // warm pre-installs them, step ids pick a subset // (showcase.ts); read in launcher.ts // ?autohide=0|1 URL only: 0: the console never minimizes itself after // boot; 1: it does even where Settings > Desktop turned it // off or automation drives the browser (navigator.webdriver, // off by default there); read in autohide.ts // ?simulate=nojspi,oom,lowmem URL only: fake a failure (for tests) import { DEFAULT_MEMORY_FALLBACK_MIB, autoMemoryMiB, glUrl, kernelSmpUrl, kernelUrl, opfsBlocker, vkShaderUrl, webCpuBlocker } from "./sdk.ts"; import { distros as DISTROS } from "virtual:unkernel-kernel"; import { autoSuspendDefault, parseAutoSuspend } from "./autosuspend.ts"; /** A distro image the page offers (virtual:unkernel-kernel). */ export interface DistroEntry { name: string; /** On the page's origin, read with Range requests (in a build, a name with its sha256: immutable). */ url: string; size: number; /** Its /usr/lib/kw-image where the list knows it (else boot.ts reads it from the guest). */ imageId?: string; } /** The images offered, debian first. */ export const DISTROS_OFFERED: readonly DistroEntry[] = DISTROS; /** The ?distro= default. */ export const DEFAULT_DISTRO = DISTROS_OFFERED.find((d) => d.name === "debian")?.name ?? DISTROS_OFFERED[0]?.name ?? "none"; /** * Plain-HTTP hosts the guest's network answers with fetch() (net.ts), each * from a CORS-enabled mirror with the same paths under the base: Debian's * apt sources. ?routes=0 turns them off. */ export const DEFAULT_ROUTES: Readonly> = Object.freeze({ "deb.debian.org": "https://mirror.unkernel.dev/deb", // The mirror's other paths, under its own name: /github/OWNER/REPO/releases/download/TAG/ASSET // (llama.cpp's and Ollama's) and /ollama/v2/library/NAME/{manifests/TAG,blobs/sha256:HEX} // (Ollama's registry), which the llama.cpp and Ollama apps' scripts ask first (apps/agents), and // /claude, /nodejs, /opencode, /astral and /github/astral-sh/uv, which the install shim asks // first where the name resolves to a routed address (guest/base/rootfs/usr/lib/kw/shim). // And /ppa/OWNER/PPA/ubuntu and /ports/ubuntu-ports (Launchpad's and ports.ubuntu.com's dists // and pool, arm64 and all), which VLC's script asks first likewise (apps/media/vlc.ts). "mirror.unkernel.dev": "https://mirror.unkernel.dev", // Ladybird's apt repository (apps/browsers/ladybird.ts), as Debian's. "apt.ladybird.org": "https://mirror.unkernel.dev/ladybird", }); /** The project's source, linked from About and the boot notes. */ export const REPO = "https://github.com/pithings/unkernel"; /** The data disk's default size in GiB (the template's), also the smallest. */ export const DATA_SIZE_DEFAULT = 4; /** The largest size Settings offers where the browser reports no quota, in GiB. */ export const DATA_SIZE_UNCAPPED = 64; /** * The largest data disk size in GiB the site's storage can hold, from * navigator.storage.estimate() (`est`; undefined, or no quota: none known, * Infinity) and the bytes the data disk itself keeps there (`stored`, part * of `usage`; 0 where unknown): the quota less the site's other data * (usage - stored), rounded down to whole GiB, but at least the default and * the saved disk's size (`have`, GiB), since a disk never shrinks. A larger * disk would fail its writes with EIO once the quota is used up. */ export function dataSizeCap(est: { quota?: number; usage?: number } | undefined, stored = 0, have = 0): number { if (est?.quota === undefined) return Infinity; const other = Math.max(0, (est.usage ?? 0) - stored); return Math.max(DATA_SIZE_DEFAULT, have, Math.floor(Math.max(0, est.quota - other) / 1073741824)); } /** ?datasize=: whole GiB from 4 to 1024, else the default. */ /** * A kernel's URL as the page keeps it (cfg, suspended sessions): its path on * the page's origin, the whole URL on another (the release mirror). */ const pageRef = (u: URL) => (u.origin === location.origin ? u.pathname : u.href); function dataSizeParam(v: string | null): number { const n = v !== null && /^\d+$/.test(v) ? Number(v) : NaN; return n >= DATA_SIZE_DEFAULT && n <= 1024 ? n : DATA_SIZE_DEFAULT; } /** The command line without ?append=. */ export const DEFAULT_CMDLINE = "console=hvc0 rdinit=/init"; /** localStorage: the saved settings (the header above), a JSON object of strings. */ export const SETTINGS_KEY = "kw.settings"; /** The settings' names. ?net=0 is read as ?wisp=0 and never saved. */ export const SETTING_NAMES: readonly string[] = ["mem", "append", "net", "wisp", "jit", "verbose", "display", "sound", "mic", "cam", "data", "datasize", "distro", "root", "worker", "shared", "cpus", "autosuspend", "askleave", "gl", "vk", "tailscale", "tsserver", "tsserverkey", "tshost"]; /** The saved settings ({} where there are none or localStorage refuses). */ export function loadSettings(): Record { try { const v: unknown = JSON.parse(localStorage.getItem(SETTINGS_KEY) ?? "{}"); if (v && typeof v === "object") return Object.fromEntries(Object.entries(v).filter(([k, x]) => SETTING_NAMES.includes(k) && typeof x === "string")); } catch {} return {}; } /** Saves the settings for the next load (none: removes the key); false where localStorage refuses. */ export function saveSettings(s: Record): boolean { const keep = Object.entries(s).filter(([k]) => SETTING_NAMES.includes(k) && k !== "net"); try { if (keep.length) localStorage.setItem(SETTINGS_KEY, JSON.stringify(Object.fromEntries(keep))); else localStorage.removeItem(SETTINGS_KEY); return true; } catch { return false; } } export interface Config { /** The settings this load read: the saved ones with the URL's on top (?net=0 as wisp=0). */ settings: Record; /** The kernel this page boots on one CPU: the base bundle unkernel.wasm (its UP kernel); a path, or a URL on the release mirror. */ kernelUrl: string; /** The image this load boots, or null (?distro=none). */ distro: DistroEntry | null; /** The Distro option: an image's name or "none". */ distroParam: string; /** Why this load does not boot what ?distro= asked for, or null. */ distroWhy: string | null; /** The user space this load runs: the image's name ("alpine", "debian", ...), or "none" (the base shell). */ guest: string; /** ?append= as given, or null (the kernel bundle's own command line then, boot.ts). */ append: string | null; memMiB: number; memWhy: string; /** What auto would pick on this device, even when ?mem= is given. */ memAutoMiB: number; /** ?mem= as given, or null for auto. */ memParam: string | null; memFallbackMiB: number[]; cmdline: string; /** The command line as ?append= sets it (cmdline without kw.root=). */ baseCmdline: string; /** ?append= or ?root= was given (the command line is shown only then). */ cmdlineGiven: boolean; /** The guest network device (?net=0 or ?wisp=0 removes it). */ net: boolean; /** ?wisp= servers, or null for net.ts DEFAULT_WISP_SERVERS. */ wisp: string[] | null; /** Plain-HTTP hosts answered with fetch(): DEFAULT_ROUTES, or null with ?routes=0. */ routes: Record | null; /** The Tailscale option (?tailscale=1; off by default). */ tailscaleParam: boolean; /** This load joins a tailnet (the option, with the network, where tailscaleBlocker() allows): the spec's parts that are settings, else null. */ tailscale: TailscaleSettings | null; /** Why this load does not join although the option asks for it (no network, a login server it cannot reach), or null. */ tailscaleWhy: string | null; /** * The URL's Tailscale settings where they would turn it on or change its * login server or key against the saved settings: not applied, and * Tailscale stays off this load (tailscale.ts asks the user; the * options show the saved settings), else null. */ tailscaleAsk: { server: string; settings: Record } | null; /** ?tsserver=, ?tsserverkey=, ?tshost= as given ("" for none). */ tsServer: string; tsServerKey: string; tsHost: string; /** kwemu's JIT tier (?jit=0 turns it off). */ jit: boolean; /** A display, /dev/fb0 (on unless ?display=0). */ display: boolean; /** A sound card, played with Web Audio (on unless ?sound=0). */ sound: boolean; /** Capture on the sound card from the microphone (on unless ?mic=0; only with the sound card). */ mic: boolean; /** A camera (kwcam, /dev/video0) showing the user's camera (on unless ?cam=0). */ cam: boolean; /** Attach the data disk (?data=0 turns it off). */ data: boolean; /** The data disk's size in GiB (?datasize=; DATA_SIZE_DEFAULT): openDataDisk()'s `size`. */ dataSizeGiB: number; /** Where this load keeps the data disk (?datastore=, datastoreFor()): OPFS in worker mode where it works, else IndexedDB. boot.ts may still fall back to IndexedDB (OPFS refused, a migration that did not fit). */ datastore: "idb" | "opfs"; /** Why this load keeps it in IndexedDB although OPFS would be the default or ?datastore=opfs asks for it, or null. */ datastoreWhy: string | null; /** ?root=initramfs: safe mode (kw.root=initramfs) for this boot, else null. */ root: "initramfs" | null; /** The kernel runs in a Web Worker (with the data disk, network, display, input and sound): ?worker=1, or more than one CPU. */ worker: boolean; /** The Worker thread option: ?worker=1 or ?worker=0, else workerDefault. */ workerParam: boolean; /** Worker mode by default: where shared memory works (cross-origin isolated), unless this load falls back to the page's thread (INLINE_ONCE_KEY). */ workerDefault: boolean; /** ?worker= was given. */ workerAsked: boolean; /** The SMP kernel's URL (sdk.ts kernelSmpUrl), or null when the page has none. */ kernelSmpUrl: string | null; /** The CPUs option: ?cpus= as given (1 to 16), else cpuDefault. */ smpParam: number; /** The CPUs this device boots with by default: cpuSuggested where the page can boot the SMP kernel, else 1. */ cpuDefault: number; /** CPUs this load boots with: resumeCpus, else smpParam where possible (capped at cpuMax), else 1. */ cpus: number; /** This load boots on the suspended session's CPU count to resume it (CPUS_ONCE_KEY, boot.ts), else 0. */ resumeCpus: number; /** Why this page cannot boot more than one CPU (smpBlocker()), or null. */ smpBlocked: string | null; /** Why this load boots on one CPU although ?cpus= asks for more, or after a fallback to one (boot.ts bootUpOnce()), or null. */ smpWhy: string | null; /** The most CPUs offered: navigator.hardwareConcurrency, at most 16 (the kernel's limit). */ cpuMax: number; /** The count the CPUs option suggests (half the threads, at most SUGGESTED_CPUS), or 0 when this device has too few threads to suggest one. */ cpuSuggested: number; /** Worker mode: shared memory for input and frames where it works (?shared=0: messages). */ shared: boolean; verbose: boolean; simulate: string[]; /** Seconds the tab is hidden before the page suspends the machine by itself (0: at once), or null: never (?autosuspend=, autosuspend.ts). */ autosuspend: number | null; /** The Auto-suspend option: "0", "now" or seconds. */ autosuspendParam: string; /** Its default on this device: "now" on phones and tablets, else "0". */ autosuspendDefault: string; /** Guest GL this load (the GL option, on a distro of GL_DISTROS, where glBlocker() allows). */ gl: boolean; /** The GL option: ?gl=1, or by default where the display is (?gl=0 or ?display=0: off). */ glParam: boolean; /** The GL option was set (?gl= in the URL or the saved settings), not the default. */ glAsked: boolean; /** ?glload=eager: the SDK's gl.eager (the renderer loads before the boot). */ glEager: boolean; /** This load's URL names ?gl= or ?gpu=: a suspended session must match them (else its own GL devices are kept for its resume, boot.ts). */ glFromUrl: boolean; /** Why this page cannot offer guest GL (glBlocker()), or null. */ glBlocked: string | null; /** Why this load runs without GL although ?gl=1 asks for it, or null. */ glWhy: string | null; /** ?glwait=strict: the SDK's gl.strictWaits. */ glStrictWaits: boolean; /** With guest GL, the virtio-gpu device (createKernel({ gpu })): unless ?gpu=0 or ?display=0. */ gpu: boolean; /** Guest Vulkan this load (the Vulkan option, on a distro of VK_DISTROS, where vkBlocker() allows). */ vk: boolean; /** The Vulkan option: ?vk=1, or by default (?vk=0: off). */ vkParam: boolean; /** The Vulkan option was set (?vk= in the URL or the saved settings), not the default. */ vkAsked: boolean; /** Why this page cannot offer guest Vulkan (vkBlocker()), or null. */ vkBlocked: string | null; /** Why this load runs without Vulkan although the option asks for it, or null. */ vkWhy: string | null; /** kwshm, for the Wayland desktop (wayland.ts): unless ?shm=0 (this load's URL only). */ shm: boolean; /** The Wayland desktop shows GPU apps' buffers by resource and Vulkan apps' external buffers (wayland.ts): unless ?wlgpu=0 (this load's URL only). */ wlGpu: boolean; /** Guest VA-API (the SDK's createKernel({ va })): on Debian where the page's thread has WebCodecs' VideoDecoder, unless ?va=0 (this load's URL only); ?va=cpu: video processing on the CPU. */ va: boolean | { upload: "cpu" }; /** Ask before the page is closed or left while a used machine runs (?askleave=0: never). */ askLeave: boolean; /** A touch device or a mobile user agent. */ mobile: boolean; /** A coarse primary pointer (touch): the page focuses nothing by itself (a terminal, a field), which would raise the on-screen keyboard. */ coarse: boolean; mobileUA: boolean; fontSize: number; } /** The Tailscale settings (never the auth key or the node's keys: tailscale.ts keeps those). */ export interface TailscaleSettings { /** The login server, or undefined for Tailscale's. */ controlURL?: string; /** Its public key ("mkey:"), or undefined to fetch it (GET /key). */ controlPublicKey?: string; /** The node's host name, or undefined for the SDK's default. */ hostname?: string; } /** Tailscale's coordination server, the SDK's default (the login server field's placeholder). */ export const TAILSCALE_DEFAULT_SERVER = "https://controlplane.tailscale.com"; /** The SDK's default host name for the node. */ export const TAILSCALE_DEFAULT_HOST = "unkernel"; /** The login server a ?tsserver= value names, as the page compares and keys them: its origin ("" is Tailscale's). */ export function tailscaleServerOrigin(server: string): string { try { const u = new URL(server.trim() || TAILSCALE_DEFAULT_SERVER); return u.protocol === "http:" || u.protocol === "https:" ? u.origin : server.trim(); } catch { return server.trim(); } } /** The Tailscale settings that the URL alone may not change (Config.tailscaleAsk). */ const TS_GUARDED = ["tailscale", "tsserver", "tsserverkey"]; /** Hosts a browser lets an https page reach over plain http and ws (potentially trustworthy origins). */ const isLoopback = (h: string) => /^(localhost|.+\.localhost|127(\.\d{1,3}){3}|\[::1\])$/i.test(h); /** * Why this page cannot join a tailnet through `server` (the login server, * "" for Tailscale's) with that server key ("" for none), or null: not an * http(s) URL, plain http from an https page (the browser blocks the page's * request for the server's key and its WebSockets to the server and to a * DERP server it embeds as mixed content; loopback hosts excepted), or a * key that is not "mkey:" and 64 hex digits. */ export function tailscaleBlocker(server: string, key: string, pageProtocol: string = location.protocol): string | null { if (server) { let u: URL; try { u = new URL(server); } catch { return `the login server ${server} is not a URL`; } if (u.protocol !== "http:" && u.protocol !== "https:") return `the login server ${server} is not an http(s) URL`; if (pageProtocol === "https:" && u.protocol === "http:" && !isLoopback(u.hostname)) return `this page is https and the login server ${server} plain http, which browsers block (mixed content): give its https URL`; } if (key && !/^mkey:[0-9a-f]{64}$/i.test(key)) return "the login server key is not mkey: and 64 hex digits"; return null; } /** The distros whose Mesa has the virgl driver (Alpine's has none). */ export const GL_DISTROS: readonly string[] = ["debian"]; /** The GL option's default: on where the display is (?display=0: off). */ export function glDefault(display: boolean): boolean { return display; } /** * Why this page cannot offer guest GL, or null: no GL renderer in the * page's build (sdk.ts glUrl; web/scripts/kernel.ts), or no WebGL2 on the * page's thread, where the renderer runs in both modes (OffscreenCanvas; * whether the browser then gives a WebGL2 context shows at boot). */ export function glBlocker(): string | null { if (!glUrl) return "this page's build has no GL renderer (virgl.mjs: gl/wasm/build.sh, or a release that carries it)"; if (typeof OffscreenCanvas !== "function" || typeof WebGL2RenderingContext !== "function") return "this browser has no WebGL2 on the page's thread (OffscreenCanvas)"; return null; } /** The distros the page offers guest Vulkan on (Debian's loader and vulkan-tools; Alpine is untested). */ export const VK_DISTROS: readonly string[] = ["debian"]; /** * Why this page cannot offer guest Vulkan, or null: no translator in the * page's build (sdk.ts vkShaderUrl; web/scripts/kernel.ts), or no WebGPU on * the page's thread, where the renderer runs in both modes (navigator.gpu; * whether the browser then gives an adapter shows at the guest's first * Vulkan instance). */ export function vkBlocker(): string | null { if (!vkShaderUrl) return "this page's build has no Vulkan translator (kwvk-naga.wasm: vk/naga/build.sh, or a release that carries it)"; if (!(navigator as { gpu?: unknown }).gpu) return "this browser has no WebGPU on the page's thread (navigator.gpu)"; return null; } /** * Why this browser's WebGPU cannot serve guest Vulkan although navigator.gpu * is there, or null: requestAdapter() gives no adapter (Chrome on Linux * without its WebGPU flags, a blocklisted GPU or driver, no GPU process). * Asked once at boot (boot.ts), which then runs without guest Vulkan and * says why (the Environment row, the option's note, the launcher's Vulkan * apps); the request makes no device. No answer in 3 s counts as an adapter. */ export async function vkAdapterBlocker(): Promise { const gpu = (navigator as { gpu?: { requestAdapter(): Promise } }).gpu; if (!gpu) return null; // vkBlocker() said so try { const adapter = await Promise.race([gpu.requestAdapter(), new Promise((r) => setTimeout(() => r(true), 3000))]); if (adapter) return null; } catch { // As no adapter. } return "this browser's WebGPU has no adapter (navigator.gpu.requestAdapter() gives none: see chrome://gpu; Chrome on Linux needs chrome://flags/#enable-unsafe-webgpu and #enable-vulkan)"; } /** The kernel's limit (NR_CPUS in kw_smp.config). */ export const MAX_CPUS = 16; /** * Why this page cannot boot the SMP kernel, or null: no SMP kernel in the * page's bundle, no cross-origin isolation (shared memory), no Web * Workers or no Atomics.waitAsync (webCpuBlocker()), or one CPU thread. * JSPI and nested workers in the kernel worker are checked there: a worker * that cannot start CPUs boots the SMP kernel on one CPU (boot.ts says so). */ export function smpBlocker(): string | null { if (!kernelSmpUrl) return "this page's kernel has no SMP build (its base bundle carries no SMP patch: kernel/build.sh smp, then base)"; const why = webCpuBlocker({ worker: true }); if (why) return why; if ((navigator.hardwareConcurrency || 1) < 2) return "this device reports a single CPU thread"; return null; } /** * The most CPUs suggested (and booted by default): half the threads up to * this. Profiled with /profile.html (2026-11-19, a 14-thread desktop, * Chrome): 4 CPUs ran 4-wide work 2.5x and 8-wide 3.5x as fast as one; 8 * CPUs only gained on 8-wide work (4-5x), 14 no more, for 3-7% on * single-threaded work, 40 ms more boot per CPU past 4 and 9 MiB each. * Firefox is not measured yet: two. Desktop Safari 27.2 (same machine, * 2026-11-23, /smp-probe.html): 3.3-3.5x on 4 CPUs and 5.2-5.8x on 8, * against Chrome's 3.4-3.6x and 5.4-6.9x. Phones: two (an iPhone 15 Pro, * two fast cores, 4 threads reported: 1.3-1.6x on 2 CPUs, 1.8-2.0x on 4, * which would leave the browser no thread of its own). */ export const SUGGESTED_CPUS = { desktop: 4, firefox: 2, mobile: 2 }; /** JavaScriptCore: Safari, and every browser on iOS (CriOS and FxiOS are WebKit too, without "Chrome/"). */ export function webkitEngine(ua = navigator.userAgent): boolean { return /AppleWebKit\//.test(ua) && !/Chrome\/|Chromium\//.test(ua); } /** sessionStorage: boot this tab's next load on one CPU (boot.ts bootUpOnce()), with why. */ export const UP_ONCE_KEY = "kw-up-once"; /** sessionStorage: boot this tab's next load on the page's thread, one CPU (boot.ts bootUpOnce(..., inline)), with why. */ export const INLINE_ONCE_KEY = "kw-inline-once"; /** sessionStorage: boot this tab's next load on this many CPUs, to resume the session suspended on them (boot.ts offerResumeCpus()). */ export const CPUS_ONCE_KEY = "kw-cpus-once"; function takeOnce(key: string): string | null { try { const v = sessionStorage.getItem(key); sessionStorage.removeItem(key); return v; } catch { return null; } } /** * Where a load keeps the data disk: ?datastore=idb, else OPFS in worker mode * where opfsBlocker() lets it (an OPFS disk needs the kernel worker's * synchronous file handles), else IndexedDB; with why when it is not OPFS * for a reason other than the URL's. */ export function datastoreFor(param: string | null, worker: boolean, blocker: string | null = opfsBlocker()): { datastore: "idb" | "opfs"; why: string | null } { if (param === "idb") return { datastore: "idb", why: null }; if (!worker) return { datastore: "idb", why: param === "opfs" ? "OPFS needs worker mode (this load runs the kernel on the page's thread)" : null }; if (blocker) return { datastore: "idb", why: blocker }; return { datastore: "opfs", why: null }; } const isCoarse = (): boolean => typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches; const isMobileUA = (): boolean => typeof navigator !== "undefined" && /Android|iPhone|iPad|Mobi/i.test(navigator.userAgent); /** A touch device or a mobile user agent (Config.mobile). */ export const isMobile = (): boolean => isCoarse() || isMobileUA(); export function readConfig(search: string = location.search, saved: Record = loadSettings()): Config { const url = new URLSearchParams(search); const params = new URLSearchParams(saved); for (const k of SETTING_NAMES) if (url.has(k)) params.set(k, url.get(k)!); if (params.get("net") === "0") params.set("wisp", "0"); params.delete("net"); const coarse = isCoarse(); const mobileUA = isMobileUA(); const mobile = coarse || mobileUA; const memParam = params.get("mem"); const auto = autoMemoryMiB({ mobile }); const fixed = memParam !== null && Number(memParam) > 0; const append = params.get("append"); const root = params.get("root") === "initramfs" ? "initramfs" : null; const wispParam = params.get("wisp"); const wisp = wispParam && wispParam !== "0" ? wispParam.split(",").map((s) => s.trim()).filter(Boolean) : null; // Worker mode by default where shared memory works (input and frames then // skip the worker's message queue); a default worker that failed to start // falls back to the page's thread for one load (boot.ts bootUpOnce()). const inlineOnce = takeOnce(INLINE_ONCE_KEY); const workerAsked = params.has("worker"); const workerDefault = !inlineOnce && typeof SharedArrayBuffer === "function" && crossOriginIsolated === true && typeof Worker === "function"; const workerParam = workerAsked ? params.get("worker") === "1" : workerDefault; // CPUs: as many as asked where the page can, never more than the host's // threads; by default the suggestion, in worker mode where the page can // boot the SMP kernel. The suggestion leaves half the threads to the host // (the page, the browser, other tabs): a guest CPU whose thread the host // deschedules stalls the others when they wait for it (.agents/status.md, SMP). const hc = navigator.hardwareConcurrency || 1; const cpuMax = Math.max(1, Math.min(MAX_CPUS, hc)); const cap = mobile ? SUGGESTED_CPUS.mobile : /Firefox\//.test(navigator.userAgent) ? SUGGESTED_CPUS.firefox : SUGGESTED_CPUS.desktop; const half = Math.min(cap, Math.floor(hc / 2)); const cpuSuggested = half >= 2 ? Math.min(half, cpuMax) : 0; const smpBlocked = smpBlocker(); const cpuDefault = !smpBlocked && workerParam && cpuSuggested ? cpuSuggested : 1; const smpParam = params.has("cpus") ? Math.max(1, Math.min(MAX_CPUS, Math.floor(Number(params.get("cpus"))) || 1)) : cpuDefault; // Once, to resume a session suspended on that many CPUs (it needs exactly its count), where the page can. const cpusOnce = Math.floor(Number(takeOnce(CPUS_ONCE_KEY))) || 0; const resumeCpus = cpusOnce > 1 && !smpBlocked && cpusOnce <= cpuMax ? cpusOnce : 0; // A fallback to one CPU (boot.ts bootUpOnce()), also after a failed resume on several with ?cpus=1. const upOnce = resumeCpus ? null : takeOnce(UP_ONCE_KEY) ?? (smpParam > 1 ? inlineOnce : null); const smpWhy = resumeCpus || (smpParam < 2 && !upOnce) ? null : smpBlocked ?? upOnce; const cpus = resumeCpus || (smpWhy ? 1 : Math.min(smpParam, cpuMax)); // The distro: one of the images ("none": the base shell). const distroAsked = params.get("distro"); let distroParam = DEFAULT_DISTRO, distroWhy: string | null = null; const names = DISTROS_OFFERED.map((d) => d.name); if (distroAsked === "none") distroParam = "none"; else if (distroAsked && names.includes(distroAsked)) distroParam = distroAsked; else if (distroAsked) distroWhy = `distro ${distroAsked} is not an image of this page (${[...names, "none"].join(", ")}): booting ${DEFAULT_DISTRO}`; const distro = DISTROS_OFFERED.find((d) => d.name === distroParam) ?? null; const autosuspend = parseAutoSuspend(params.get("autosuspend"), mobile); // Guest GL: on by default where the display is (the renderer loads on the guest's first GL use). const glAsked = params.has("gl"); const glParam = glAsked ? params.get("gl") === "1" : glDefault(params.get("display") !== "0"); const glBlocked = glBlocker(); const glWhy = !glParam ? null : glBlocked ?? (GL_DISTROS.includes(distro?.name ?? "none") ? null : `guest GL needs ${GL_DISTROS.join(" or ")} (its Mesa has the virgl driver), not ${distro?.name ?? "the base shell"}`); // Guest Vulkan: on by default where WebGPU is (the renderer loads on the guest's first Vulkan instance). const vkAsked = params.has("vk"); const vkParam = vkAsked ? params.get("vk") === "1" : true; const vkBlocked = vkBlocker(); const vkWhy = !vkParam ? null : vkBlocked ?? (VK_DISTROS.includes(distro?.name ?? "none") ? null : `guest Vulkan needs ${VK_DISTROS.join(" or ")}, not ${distro?.name ?? "the base shell"}`); // Tailscale: off by default, and only with the network. The URL may turn it off, but turning it // on or changing its login server or key needs the user (tailscaleAsk): such a load starts no node. const tsOn = (p: { get(k: string): string | null }) => p.get("tailscale") === "1"; const tsTarget = (p: { get(k: string): string | null }) => `${tailscaleServerOrigin(p.get("tsserver") ?? "")} ${(p.get("tsserverkey") ?? "").trim()}`; const savedParams = new URLSearchParams(saved); let tailscaleAsk: Config["tailscaleAsk"] = null; if (tsOn(params) && TS_GUARDED.some((k) => url.has(k)) && (!tsOn(savedParams) || tsTarget(params) !== tsTarget(savedParams))) { tailscaleAsk = { server: (params.get("tsserver") ?? "").trim(), settings: Object.fromEntries([...TS_GUARDED, "tshost"].flatMap((k) => (params.has(k) ? [[k, params.get(k)!]] : []))) }; for (const k of TS_GUARDED) { if (savedParams.has(k)) params.set(k, savedParams.get(k)!); else params.delete(k); } } const tailscaleParam = params.get("tailscale") === "1"; const tsServer = (params.get("tsserver") ?? "").trim().replace(/\/+$/, ""); const tsServerKey = (params.get("tsserverkey") ?? "").trim(); const tsHost = (params.get("tshost") ?? "").trim(); const tailscaleWhy = !tailscaleParam ? null : wispParam === "0" ? "the network is off" : tailscaleBlocker(tsServer, tsServerKey); // The data disk's store: this load's URL only (not a setting). const store = datastoreFor(url.get("datastore"), workerParam || cpus > 1); return { settings: Object.fromEntries(params), kernelUrl: pageRef(kernelUrl), distro, distroParam, distroWhy, guest: distro?.name ?? "none", append: append || null, memMiB: fixed ? Number(memParam) : auto.mib, memWhy: fixed ? `set to ${memParam} MiB` : auto.why, memAutoMiB: auto.mib, memParam: fixed ? memParam : null, memFallbackMiB: [...DEFAULT_MEMORY_FALLBACK_MIB], cmdline: root ? `${append || DEFAULT_CMDLINE} kw.root=${root}` : append || DEFAULT_CMDLINE, baseCmdline: append || DEFAULT_CMDLINE, cmdlineGiven: !!append || !!root, net: wispParam !== "0", wisp: wisp?.length ? wisp : null, routes: url.get("routes") === "0" ? null : { ...DEFAULT_ROUTES }, tailscaleParam, // Not started at all while the URL's settings wait for the user (not even on the saved ones). tailscale: tailscaleParam && !tailscaleWhy && !tailscaleAsk ? { ...(tsServer ? { controlURL: tsServer } : {}), ...(tsServerKey ? { controlPublicKey: tsServerKey } : {}), ...(tsHost ? { hostname: tsHost } : {}), } : null, tailscaleWhy, tailscaleAsk, tsServer, tsServerKey, tsHost, jit: params.get("jit") !== "0", display: params.get("display") !== "0", sound: params.get("sound") !== "0", mic: params.get("mic") !== "0" && params.get("sound") !== "0", cam: params.get("cam") !== "0", data: params.get("data") !== "0", dataSizeGiB: dataSizeParam(params.get("datasize")), datastore: store.datastore, datastoreWhy: store.why, root, worker: workerParam || cpus > 1, workerParam, workerDefault, workerAsked, kernelSmpUrl: kernelSmpUrl ? pageRef(kernelSmpUrl) : null, smpParam, cpuDefault, cpus, resumeCpus, smpBlocked, smpWhy, cpuMax, cpuSuggested, shared: params.get("shared") !== "0", verbose: params.get("verbose") === "1", simulate: (url.get("simulate") || "").split(",").filter(Boolean), autosuspend: autosuspend.delayS, autosuspendParam: autosuspend.value, autosuspendDefault: autoSuspendDefault(mobile), askLeave: params.get("askleave") !== "0", gl: glParam && !glWhy, glParam, glAsked, glEager: url.get("glload") === "eager", glFromUrl: url.has("gl") || url.has("gpu"), glBlocked, glWhy, // Not settings (SETTING_NAMES): this load's URL only. glStrictWaits: url.get("glwait") === "strict", gpu: glParam && !glWhy && url.get("gpu") !== "0" && params.get("display") !== "0", vk: vkParam && !vkWhy, vkParam, vkAsked, vkBlocked, vkWhy, shm: url.get("shm") !== "0", wlGpu: url.get("wlgpu") !== "0", va: url.get("va") !== "0" && distro?.name === "debian" && typeof (globalThis as { VideoDecoder?: unknown }).VideoDecoder === "function" ? (url.get("va") === "cpu" ? { upload: "cpu" } : true) : false, mobile, coarse, mobileUA, fontSize: 14, }; }