From c584c4d8a4cca56a74abd108b2f6595ac50fd5ec Mon Sep 17 00:00:00 2001 From: clover caruso Date: Sat, 21 Nov 2026 17:36:33 -0800 Subject: [PATCH] Spacebar: read the fork straight from Shale storage The fork is now the private repository snowcord-server. Mount its Shale directory read-only and read the branch tip and tree with git instead of polling and fetching over HTTP, so no token is needed and a push is seen within 250 ms. Assisted-by: claude-sonnet-5.5 --- service/spacebar/build/supervisor.mjs | 38 ++++++++++++--------------- service/spacebar/service.pkl | 5 ++-- tools/spacebar.md | 15 ++++++----- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/service/spacebar/build/supervisor.mjs b/service/spacebar/build/supervisor.mjs index ff15a229d385f9beadcfd8e60067b429903444f5..53d5b3338c85dc21d1a922d92500fbc8424de56b 100644 --- a/service/spacebar/build/supervisor.mjs +++ b/service/spacebar/build/supervisor.mjs @@ -6,12 +6,13 @@ import { basename } from "node:path"; import { setTimeout as sleep } from "node:timers/promises"; import { promisify } from "node:util"; -const { SOURCE_URL, BRANCH, PORT } = process.env; +const { BRANCH, PORT } = process.env; const WORK = "/work"; -const REPO = `${WORK}/repo.git`; -const POLL_MS = 500; +const REPO = "/source.git"; +const POLL_MS = 250; const DRAIN_MS = 500; const RETRY_FAILED_MS = 15000; +const GIT = ["-c", "safe.directory=*", "--git-dir", REPO]; const out = promisify(execFile); const log = (...args) => console.log("[deploy]", ...args); @@ -21,7 +22,7 @@ const run = (cmd, args, opts = {}, ok = [0]) => .once("error", reject) .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))), ); -const git = (...args) => out("git", ["--git-dir", REPO, ...args]).then((result) => result.stdout.trim()); +const git = (...args) => out("git", [...GIT, ...args]).then((result) => result.stdout.trim()); let live = null; let wanted = null; @@ -46,11 +47,11 @@ createServer((client) => { upstream.on("error", end).on("close", end); }).listen(Number(PORT)); -async function remoteHead() { - const response = await fetch(`${SOURCE_URL}/info/refs?service=git-upload-pack`, { signal: AbortSignal.timeout(5000) }); - if (!response.ok) throw new Error(`${SOURCE_URL} answered ${response.status}`); - return (await response.text()).match(new RegExp(`([0-9a-f]{40}) refs/heads/${BRANCH}(?=\\0|\\n)`))?.[1] ?? null; -} +const branchTip = () => + git("rev-parse", "--verify", "--quiet", `refs/heads/${BRANCH}`).catch((error) => { + if (error.code !== 1) throw error; + return null; + }); async function install(dir, modules) { const staging = `${modules}.partial`; @@ -104,9 +105,9 @@ function retire(old) { } async function deploy() { - const target = wanted; + const rev = wanted; const current = () => { - if (wanted !== target) throw new Error("superseded by a newer push"); + if (wanted !== rev) throw new Error("superseded by a newer push"); }; let release; let last = performance.now(); @@ -119,18 +120,14 @@ async function deploy() { }; try { - await run("git", ["--git-dir", REPO, "fetch", "--quiet", "--depth", "1", "--no-tags", SOURCE_URL, `refs/heads/${BRANCH}`]); - const rev = await git("rev-parse", "FETCH_HEAD"); - if (rev === live?.rev) return; const [committedAt, subject] = (await git("log", "-1", "--format=%ct%n%s", rev)).split("\n"); log(`deploying ${rev.slice(0, 7)} ${subject}`); - lap("fetch"); release = { rev, dir: `${WORK}/rel/${rev}`, modules: null, port: 0, proc: null, sockets: new Set() }; releases.add(release); rmSync(release.dir, { recursive: true, force: true }); mkdirSync(release.dir, { recursive: true }); - await run("bash", ["-c", 'set -o pipefail; git --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]); + await run("bash", ["-c", 'set -o pipefail; git -c safe.directory="*" --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]); lap("extract"); const lock = createHash("sha256"); @@ -175,8 +172,8 @@ async function deploy() { if (old) retire(old); log(`live ${rev.slice(0, 7)} in ${((performance.now() - start) / 1000).toFixed(1)}s: ${laps.join(", ")}`); } catch (error) { - log(`${target.slice(0, 7)} failed: ${error.message}`); - failed = { rev: target, at: Date.now() }; + log(`${rev.slice(0, 7)} failed: ${error.message}`); + failed = { rev, at: Date.now() }; if (release) releases.delete(release); sweep(); } @@ -184,15 +181,14 @@ async function deploy() { mkdirSync(`${WORK}/rel`, { recursive: true }); mkdirSync(`${WORK}/modules`, { recursive: true }); -await run("git", ["init", "--quiet", "--bare", REPO]); process.on("SIGTERM", () => process.exit(0)); (async () => { let problem = ""; for (;; await sleep(POLL_MS)) { try { - const head = await remoteHead(); - if (!head) throw new Error(`${SOURCE_URL} has no branch ${BRANCH}`); + const head = await branchTip(); + if (!head) throw new Error(`${REPO} has no branch ${BRANCH}`); wanted = head; problem = ""; } catch (error) { diff --git a/service/spacebar/service.pkl b/service/spacebar/service.pkl index 0f828580464af6466847e873534680887fda1e58..6e2112e62395e74097b45f36a6ad13644cb3f8b0 100644 --- a/service/spacebar/service.pkl +++ b/service/spacebar/service.pkl @@ -1,5 +1,6 @@ amends "../../config/Service.pkl" +import "../../config/site.pkl" as site import "../postgres/service.pkl" as postgres local host = module.container.http.hostname @@ -24,7 +25,6 @@ container { build = "build" cpu = 1000 memory = 3072 - extraHosts { "shale.paperclover.net:host-gateway" } http { containerPort = 3001 @@ -35,11 +35,12 @@ container { volumes { ["/data"] {} + // Shale keeps each repository under its UUID, which renaming and visibility changes leave alone. + ["/source.git"] { src = "\(site.root)/prod/shale/repositories_owned/006AH3H3QKH2KN318QXRNFBH4B"; readOnly = true } } tmpfs { "/work:rw,exec,size=3g" } env { - ["SOURCE_URL"] = "https://shale.paperclover.net/discord-server.git" ["BRANCH"] = "main" ["HOME"] = "/work" ["CONFIG_PATH"] = "/spacebar/config.json" diff --git a/tools/spacebar.md b/tools/spacebar.md index 963c1fbe0043167f75b39b4645982e837e729ccb..0dc17ca62a736bdb9a08ce4bbccf46412636ed66 100644 --- a/tools/spacebar.md +++ b/tools/spacebar.md @@ -2,17 +2,17 @@ `https://spacebar.paperclover.net` runs Clover's fork of [spacebarchat/server](https://github.com/spacebarchat/server) from branch `main` of -`https://shale.paperclover.net/discord-server.git`. Pushing to that branch is the -deploy; `tools/deploy.py` is only needed to change the service definition. +the private Shale repository `https://shale.paperclover.net/snowcord-server.git`. +Pushing to that branch is the deploy; `tools/deploy.py` is only needed to change the service definition. ``` -~/dev/discord-server jj checkout, remotes: origin = Shale, upstream = GitHub +~/dev/snowcord-server jj checkout, remotes: origin = Shale, upstream = GitHub (`master` mirrors upstream, `main` is what runs) jj bookmark set main -r @- && jj git push -b main curl https://spacebar.paperclover.net/_spacebar/api/version # commit that is live ``` -A push is live about 4 seconds later (3.5 to 4.6 s measured from the start of the push). Dependency changes +A push is live about 4 seconds later (3.4 to 4.6 s measured from the start of the push). Dependency changes (`package.json` or the lockfile) add an `npm ci`, about 5 s. Previews run the same supervisor against the same branch with a fresh database. @@ -21,8 +21,11 @@ supervisor against the same branch with a fresh database. [`supervisor.mjs`](../service/spacebar/build/supervisor.mjs) is the container's only process, on a stock Node image with its working files in tmpfs `/work`. -1. Every 500 ms it reads the branch tip from Shale's `info/refs`. -2. On a new tip it shallow-fetches, extracts the tree, installs `node_modules` +1. Every 250 ms it reads the branch tip from the repository's Shale storage, + mounted read-only at `/source.git`. Shale keeps a repository under its UUID, + so renaming it or changing its visibility does not affect the mount; a + recreated repository needs the new UUID in `service.pkl`. +2. On a new tip it extracts the tree with `git archive`, installs `node_modules` only when the dependency files changed, and builds with the repo's own `node_modules/.bin/tsc` (TypeScript 7, about 1 s). 3. It starts the new server on a free port, waits for `/api/ping`, then points its -- 2.54.1