// LOCAL ONLY (demo epic D5). THE CLAIM, exactly as enforced (the docs quote // LOCAL_ONLY_CLAIM): // // With ARCHILYZER_LOCAL_ONLY=1, this instance refuses — before any request // is sent or any process started — every call to the Claude API, every // Claude Code answer (a task routed to `code`, or `answers` posted by a // session), the digest's `claude` CLI lane, and every call to an ollama // model that is a cloud model (a `-cloud` tag, or one whose /api/show names // a remote host) or that is served from anywhere but this machine (a // loopback address) or a host named in ARCHILYZER_LOCAL_MODEL_HOSTS. It does // not cover the web pages a local model may read (its web_search and fetch // tools), the published site's in-browser Ask, or remote transcription // workers, which are not language models. // // It FAILS CLOSED: each path asks `assertLocalOnly…` at the last point before // its I/O, so a route, a preset, a URL or a model named by hand cannot get // past it; the dispatchers and the settings writers ask too, earlier, only so // that a job or a write is refused with a sentence. // // "Local" is a HOST: 127.0.0.0/8, ::1, `localhost`, or a name in // ARCHILYZER_LOCAL_MODEL_HOSTS (comma-separated, exact, case-insensitive) — // set by docker-compose.demo.yml to `ollama`, the compose service on the // stack's own network. Nothing else. // // Unset, every function here is a no-op and every caller behaves exactly as // before the flag existed. The flag's values are parsed by `localOnlyFlag` // alone (docker/entrypoint.sh mirrors it): 1/true/yes/on (trimmed, any case) // is on, 0/false/no/off or unset is off, anything else is NOT UNDERSTOOD — // off, and said so at boot and by `gen local-check`. // // Pure, browser-safe: no node:* import (it reads the environment it is given, // process.env by default). type Env = Record; const envOf = (env?: Env): Env => env ?? (typeof process !== "undefined" ? process.env : {}); export const LOCAL_ONLY_CLAIM = "With ARCHILYZER_LOCAL_ONLY=1, this instance refuses — before any request is sent or any process started — every call to the Claude API, every Claude Code answer (a task routed to `code`, or `answers` posted by a session), the digest's `claude` CLI lane, and every call to an ollama model that is a cloud model (a `-cloud` tag, or one whose /api/show names a remote host) or that is served from anywhere but this machine (a loopback address) or a host named in ARCHILYZER_LOCAL_MODEL_HOSTS. It does not cover the web pages a local model may read (its web_search and fetch tools), the published site's in-browser Ask, or remote transcription workers, which are not language models."; /** How ARCHILYZER_LOCAL_ONLY reads: on, off, or a value that is not understood (off). */ export function localOnlyFlag(env?: Env): { on: boolean; raw: string; understood: boolean } { const raw = envOf(env).ARCHILYZER_LOCAL_ONLY ?? ""; const v = raw.trim().toLowerCase(); if (v === "1" || v === "true" || v === "yes" || v === "on") return { on: true, raw, understood: true }; if (v === "" || v === "0" || v === "false" || v === "no" || v === "off") return { on: false, raw, understood: true }; return { on: false, raw, understood: false }; } /** Whether ARCHILYZER_LOCAL_ONLY is on. */ export function isLocalOnly(env?: Env): boolean { return localOnlyFlag(env).on; } export const LOCAL_ONLY_WHY = "this instance is local only (ARCHILYZER_LOCAL_ONLY=1)"; const LOOPBACK_NAMES = new Set(["localhost", "::1", "[::1]"]); /** The hosts besides loopback a local model may be served from (ARCHILYZER_LOCAL_MODEL_HOSTS). */ export function localModelHosts(env?: Env): string[] { return (envOf(env).ARCHILYZER_LOCAL_MODEL_HOSTS ?? "") .split(",") .map((h) => h.trim().toLowerCase()) .filter(Boolean); } /** The host of a model server's URL, lowercased, or null when it does not parse as http(s). */ export function modelHostOf(url: string): string | null { try { const u = new URL(url.trim()); if (u.protocol !== "http:" && u.protocol !== "https:") return null; return u.hostname.toLowerCase(); } catch { return null; } } /** Whether a model server's URL is on this machine: loopback, or a host in ARCHILYZER_LOCAL_MODEL_HOSTS. */ export function isLocalModelUrl(url: string, env?: Env): boolean { const host = modelHostOf(url); if (!host) return false; if (LOOPBACK_NAMES.has(host) || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host)) return true; return localModelHosts(env).includes(host); } /** * The refusal for a model server URL on a local-only instance, or null. `what` * names it ("the local route's ollama", "LLM worker w1"). */ export function localOnlyEndpointRefusal(url: string, what: string, env?: Env): string | null { if (!isLocalOnly(env)) return null; if (isLocalModelUrl(url, env)) return null; const host = modelHostOf(url) ?? "an unreadable URL"; return `${LOCAL_ONLY_WHY}: ${what} at ${host} is not on this machine (a loopback address, or a host in ARCHILYZER_LOCAL_MODEL_HOSTS)`; } /** Throws before a model server that is not on this machine is asked anything. */ export function assertLocalOnlyAllowsEndpoint(url: string, what: string, env?: Env): void { const r = localOnlyEndpointRefusal(url, what, env); if (r) throw new LocalOnlyError(r); } /** * Whether an ollama /api/show answer says the model runs elsewhere: a * `remote_host` or `remote_model` (ollama's cloud models), at the top level or * under `details`. (Field names as ollama documents its cloud models; checked * against 0.40.5 only at the real up.) */ export function showNamesRemote(show: unknown): boolean { const o = show && typeof show === "object" ? (show as Record) : {}; const d = o.details && typeof o.details === "object" ? (o.details as Record) : {}; const set = (v: unknown) => typeof v === "string" && v.trim() !== ""; return set(o.remote_host) || set(o.remote_model) || set(d.remote_host) || set(d.remote_model); } export class LocalOnlyError extends Error { constructor(what: string) { super(what.startsWith(LOCAL_ONLY_WHY) ? what : `${LOCAL_ONLY_WHY}: ${what} is refused`); this.name = "LocalOnlyError"; } } /** * An ollama model name that runs on ollama's hosted service rather than on * this machine: its tag is `cloud` or ends in `-cloud` (`gpt-oss:120b-cloud`, * `qwen3-coder:480b-cloud`, `kimi-k2:1t-cloud`). */ export function isCloudOllamaModel(model: string): boolean { const tag = model.trim().toLowerCase().split(":").slice(1).join(":"); return tag === "cloud" || tag.endsWith("-cloud"); } /** Throws before the Claude API is asked anything. */ export function assertLocalOnlyAllowsApi(env?: Env): void { if (isLocalOnly(env)) throw new LocalOnlyError("a call to the Claude API"); } /** Throws before the `claude` CLI (the digest's metered lane) is started. */ export function assertLocalOnlyAllowsClaudeCli(env?: Env): void { if (isLocalOnly(env)) throw new LocalOnlyError("the Claude Code CLI (the digest's metered lane)"); } /** Throws before an ollama model that is not on this machine is asked. */ export function assertLocalOnlyAllowsOllamaModel(model: string, env?: Env): void { if (isLocalOnly(env) && isCloudOllamaModel(model)) throw new LocalOnlyError(`the ollama cloud model "${model}"`); } /** * The refusal for a generation job, or null: on a local-only instance a job * runs on a local model only and takes no Claude Code session's answers. * `models` are every model the job would run on (its model, an extractor); * `isLocal` says whether one is local (providerOf). */ export function localOnlyJobRefusal( o: { models: readonly (string | null | undefined)[]; answers?: boolean; route?: string | null }, isLocal: (model: string) => boolean, env?: Env, ): string | null { if (!isLocalOnly(env)) return null; if (o.answers) return `${LOCAL_ONLY_WHY}: a Claude Code session's answers are refused`; if (o.route === "code") return `${LOCAL_ONLY_WHY}: the task is routed to Claude Code — route it to local (\`archilyzer gen preset local\`)`; for (const m of o.models) { if (!m) continue; if (!isLocal(m)) return `${LOCAL_ONLY_WHY}: "${m}" is not a local model — run it on the local model`; if (isCloudOllamaModel(m)) return `${LOCAL_ONLY_WHY}: "${m}" is an ollama cloud model`; } return null; } /** * The refusal for a settings write on a local-only instance, or null: a * generation route other than `local`. */ export function localOnlyRoutesRefusal(routes: Record | null | undefined, env?: Env): string | null { if (!isLocalOnly(env) || !routes) return null; const off = Object.entries(routes).filter(([, r]) => r !== "local").map(([t, r]) => `${t} → ${String(r)}`); return off.length ? `${LOCAL_ONLY_WHY}: every generation route stays local — not saved: ${off.join(", ")}` : null; } const isObj = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); /** * The refusal for a settings write on a local-only instance, or null — what * the ops route and the editor's settings writer both ask: a patch that * routes a generation task anywhere but `local`, or names a model server not * on this machine (settings.generation.ollamaUrl, a digest engine's * baseUrl). Only what the patch TOUCHES is judged, against `merged` (the * settings as they would be saved); a blank URL means OLLAMA_URL, judged at * call time. */ export function localOnlySettingsRefusal(patch: Record, merged: Record, env?: Env): string | null { if (!isLocalOnly(env)) return null; const gen = isObj(merged.generation) ? merged.generation : {}; const pg = patch.generation; if (isObj(pg)) { if (Object.hasOwn(pg, "routes")) { const r = localOnlyRoutesRefusal(isObj(gen.routes) ? gen.routes : null, env); if (r) return r; } if (Object.hasOwn(pg, "ollamaUrl") && typeof gen.ollamaUrl === "string" && gen.ollamaUrl.trim()) { const r = localOnlyEndpointRefusal(gen.ollamaUrl, "the local route's model server (generation.ollamaUrl)", env); if (r) return `${r} — not saved`; } } if (isObj(patch.digest)) { const apps = isObj(merged.digest) && isObj(merged.digest.apps) ? merged.digest.apps : {}; for (const [id, cfg] of Object.entries(apps)) { const url = isObj(cfg) && typeof cfg.baseUrl === "string" ? cfg.baseUrl.trim() : ""; if (!url) continue; const r = localOnlyEndpointRefusal(url, `the digest engine ${id} (digest.apps.${id}.baseUrl)`, env); if (r) return `${r} — not saved`; } } return null; }