authoravatarclover caruso<git@paperclover.net>2026-11-21 17:36:33-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-22 00:44:03-08:00
logc584c4d8a4cca56a74abd108b2f6595ac50fd5ec
treedcaa6114f35b6a0cdb7e12759c9c1324a1b58b13
parentb952d99332a054941ad27516c5e83e7222f66c85
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

3 files changed, 29 insertions(+), 29 deletions(-)

service/spacebar/build/supervisor.mjs+17-21
...@@ -6,12 +6,13 @@ import { basename } from "node:path";...@@ -6,12 +6,13 @@ import { basename } from "node:path";
6import { setTimeout as sleep } from "node:timers/promises";6import { setTimeout as sleep } from "node:timers/promises";
7import { promisify } from "node:util";7import { promisify } from "node:util";
88
9const { SOURCE_URL, BRANCH, PORT } = process.env;9const { BRANCH, PORT } = process.env;
10const WORK = "/work";10const WORK = "/work";
11const REPO = `${WORK}/repo.git`;11const REPO = "/source.git";
12const POLL_MS = 500;12const POLL_MS = 250;
13const DRAIN_MS = 500;13const DRAIN_MS = 500;
14const RETRY_FAILED_MS = 15000;14const RETRY_FAILED_MS = 15000;
15const GIT = ["-c", "safe.directory=*", "--git-dir", REPO];
1516
16const out = promisify(execFile);17const out = promisify(execFile);
17const log = (...args) => console.log("[deploy]", ...args);18const log = (...args) => console.log("[deploy]", ...args);
...@@ -21,7 +22,7 @@ const run = (cmd, args, opts = {}, ok = [0]) =>...@@ -21,7 +22,7 @@ const run = (cmd, args, opts = {}, ok = [0]) =>
21 .once("error", reject)22 .once("error", reject)
22 .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))),23 .once("exit", (code) => (ok.includes(code) ? resolve() : reject(new Error(`${basename(cmd)} ${args[0]} exited with ${code}`)))),
23 );24 );
24const git = (...args) => out("git", ["--git-dir", REPO, ...args]).then((result) => result.stdout.trim());25const git = (...args) => out("git", [...GIT, ...args]).then((result) => result.stdout.trim());
2526
26let live = null;27let live = null;
27let wanted = null;28let wanted = null;
...@@ -46,11 +47,11 @@ createServer((client) => {...@@ -46,11 +47,11 @@ createServer((client) => {
46 upstream.on("error", end).on("close", end);47 upstream.on("error", end).on("close", end);
47}).listen(Number(PORT));48}).listen(Number(PORT));
4849
49async function remoteHead() {50const branchTip = () =>
50 const response = await fetch(`${SOURCE_URL}/info/refs?service=git-upload-pack`, { signal: AbortSignal.timeout(5000) });51 git("rev-parse", "--verify", "--quiet", `refs/heads/${BRANCH}`).catch((error) => {
51 if (!response.ok) throw new Error(`${SOURCE_URL} answered ${response.status}`);52 if (error.code !== 1) throw error;
52 return (await response.text()).match(new RegExp(`([0-9a-f]{40}) refs/heads/${BRANCH}(?=\\0|\\n)`))?.[1] ?? null;53 return null;
53}54 });
5455
55async function install(dir, modules) {56async function install(dir, modules) {
56 const staging = `${modules}.partial`;57 const staging = `${modules}.partial`;
...@@ -104,9 +105,9 @@ function retire(old) {...@@ -104,9 +105,9 @@ function retire(old) {
104}105}
105106
106async function deploy() {107async function deploy() {
107 const target = wanted;108 const rev = wanted;
108 const current = () => {109 const current = () => {
109 if (wanted !== target) throw new Error("superseded by a newer push");110 if (wanted !== rev) throw new Error("superseded by a newer push");
110 };111 };
111 let release;112 let release;
112 let last = performance.now();113 let last = performance.now();
...@@ -119,18 +120,14 @@ async function deploy() {...@@ -119,18 +120,14 @@ async function deploy() {
119 };120 };
120121
121 try {122 try {
122 await run("git", ["--git-dir", REPO, "fetch", "--quiet", "--depth", "1", "--no-tags", SOURCE_URL, `refs/heads/${BRANCH}`]);
123 const rev = await git("rev-parse", "FETCH_HEAD");
124 if (rev === live?.rev) return;
125 const [committedAt, subject] = (await git("log", "-1", "--format=%ct%n%s", rev)).split("\n");123 const [committedAt, subject] = (await git("log", "-1", "--format=%ct%n%s", rev)).split("\n");
126 log(`deploying ${rev.slice(0, 7)} ${subject}`);124 log(`deploying ${rev.slice(0, 7)} ${subject}`);
127 lap("fetch");
128125
129 release = { rev, dir: `${WORK}/rel/${rev}`, modules: null, port: 0, proc: null, sockets: new Set() };126 release = { rev, dir: `${WORK}/rel/${rev}`, modules: null, port: 0, proc: null, sockets: new Set() };
130 releases.add(release);127 releases.add(release);
131 rmSync(release.dir, { recursive: true, force: true });128 rmSync(release.dir, { recursive: true, force: true });
132 mkdirSync(release.dir, { recursive: true });129 mkdirSync(release.dir, { recursive: true });
133 await run("bash", ["-c", 'set -o pipefail; git --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]);130 await run("bash", ["-c", 'set -o pipefail; git -c safe.directory="*" --git-dir "$0" archive "$1" | tar -x -C "$2"', REPO, rev, release.dir]);
134 lap("extract");131 lap("extract");
135132
136 const lock = createHash("sha256");133 const lock = createHash("sha256");
...@@ -175,8 +172,8 @@ async function deploy() {...@@ -175,8 +172,8 @@ async function deploy() {
175 if (old) retire(old);172 if (old) retire(old);
176 log(`live ${rev.slice(0, 7)} in ${((performance.now() - start) / 1000).toFixed(1)}s: ${laps.join(", ")}`);173 log(`live ${rev.slice(0, 7)} in ${((performance.now() - start) / 1000).toFixed(1)}s: ${laps.join(", ")}`);
177 } catch (error) {174 } catch (error) {
178 log(`${target.slice(0, 7)} failed: ${error.message}`);175 log(`${rev.slice(0, 7)} failed: ${error.message}`);
179 failed = { rev: target, at: Date.now() };176 failed = { rev, at: Date.now() };
180 if (release) releases.delete(release);177 if (release) releases.delete(release);
181 sweep();178 sweep();
182 }179 }
...@@ -184,15 +181,14 @@ async function deploy() {...@@ -184,15 +181,14 @@ async function deploy() {
184181
185mkdirSync(`${WORK}/rel`, { recursive: true });182mkdirSync(`${WORK}/rel`, { recursive: true });
186mkdirSync(`${WORK}/modules`, { recursive: true });183mkdirSync(`${WORK}/modules`, { recursive: true });
187await run("git", ["init", "--quiet", "--bare", REPO]);
188process.on("SIGTERM", () => process.exit(0));184process.on("SIGTERM", () => process.exit(0));
189185
190(async () => {186(async () => {
191 let problem = "";187 let problem = "";
192 for (;; await sleep(POLL_MS)) {188 for (;; await sleep(POLL_MS)) {
193 try {189 try {
194 const head = await remoteHead();190 const head = await branchTip();
195 if (!head) throw new Error(`${SOURCE_URL} has no branch ${BRANCH}`);191 if (!head) throw new Error(`${REPO} has no branch ${BRANCH}`);
196 wanted = head;192 wanted = head;
197 problem = "";193 problem = "";
198 } catch (error) {194 } catch (error) {
service/spacebar/service.pkl+3-2
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1amends "../../config/Service.pkl"1amends "../../config/Service.pkl"
22
3import "../../config/site.pkl" as site
3import "../postgres/service.pkl" as postgres4import "../postgres/service.pkl" as postgres
45
5local host = module.container.http.hostname6local host = module.container.http.hostname
...@@ -24,7 +25,6 @@ container {...@@ -24,7 +25,6 @@ container {
24 build = "build"25 build = "build"
25 cpu = 100026 cpu = 1000
26 memory = 307227 memory = 3072
27 extraHosts { "shale.paperclover.net:host-gateway" }
2828
29 http {29 http {
30 containerPort = 300130 containerPort = 3001
...@@ -35,11 +35,12 @@ container {...@@ -35,11 +35,12 @@ container {
3535
36 volumes {36 volumes {
37 ["/data"] {}37 ["/data"] {}
38 // Shale keeps each repository under its UUID, which renaming and visibility changes leave alone.
39 ["/source.git"] { src = "\(site.root)/prod/shale/repositories_owned/006AH3H3QKH2KN318QXRNFBH4B"; readOnly = true }
38 }40 }
39 tmpfs { "/work:rw,exec,size=3g" }41 tmpfs { "/work:rw,exec,size=3g" }
4042
41 env {43 env {
42 ["SOURCE_URL"] = "https://shale.paperclover.net/discord-server.git"
43 ["BRANCH"] = "main"44 ["BRANCH"] = "main"
44 ["HOME"] = "/work"45 ["HOME"] = "/work"
45 ["CONFIG_PATH"] = "/spacebar/config.json"46 ["CONFIG_PATH"] = "/spacebar/config.json"
tools/spacebar.md+9-6
...@@ -2,17 +2,17 @@...@@ -2,17 +2,17 @@
22
3`https://spacebar.paperclover.net` runs Clover's fork of3`https://spacebar.paperclover.net` runs Clover's fork of
4[spacebarchat/server](https://github.com/spacebarchat/server) from branch `main` of4[spacebarchat/server](https://github.com/spacebarchat/server) from branch `main` of
5`https://shale.paperclover.net/discord-server.git`. Pushing to that branch is the5the private Shale repository `https://shale.paperclover.net/snowcord-server.git`.
6deploy; `tools/deploy.py` is only needed to change the service definition.6Pushing to that branch is the deploy; `tools/deploy.py` is only needed to change the service definition.
77
8```8```
9~/dev/discord-server jj checkout, remotes: origin = Shale, upstream = GitHub9~/dev/snowcord-server jj checkout, remotes: origin = Shale, upstream = GitHub
10 (`master` mirrors upstream, `main` is what runs)10 (`master` mirrors upstream, `main` is what runs)
11jj bookmark set main -r @- && jj git push -b main11jj bookmark set main -r @- && jj git push -b main
12curl https://spacebar.paperclover.net/_spacebar/api/version # commit that is live12curl https://spacebar.paperclover.net/_spacebar/api/version # commit that is live
13```13```
1414
15A push is live about 4 seconds later (3.5 to 4.6 s measured from the start of the push). Dependency changes15A push is live about 4 seconds later (3.4 to 4.6 s measured from the start of the push). Dependency changes
16(`package.json` or the lockfile) add an `npm ci`, about 5 s. Previews run the same16(`package.json` or the lockfile) add an `npm ci`, about 5 s. Previews run the same
17supervisor against the same branch with a fresh database.17supervisor against the same branch with a fresh database.
1818
...@@ -21,8 +21,11 @@ supervisor against the same branch with a fresh database....@@ -21,8 +21,11 @@ supervisor against the same branch with a fresh database.
21[`supervisor.mjs`](../service/spacebar/build/supervisor.mjs) is the container's21[`supervisor.mjs`](../service/spacebar/build/supervisor.mjs) is the container's
22only process, on a stock Node image with its working files in tmpfs `/work`.22only process, on a stock Node image with its working files in tmpfs `/work`.
2323
241. Every 500 ms it reads the branch tip from Shale's `info/refs`.241. Every 250 ms it reads the branch tip from the repository's Shale storage,
252. On a new tip it shallow-fetches, extracts the tree, installs `node_modules`25 mounted read-only at `/source.git`. Shale keeps a repository under its UUID,
26 so renaming it or changing its visibility does not affect the mount; a
27 recreated repository needs the new UUID in `service.pkl`.
282. On a new tip it extracts the tree with `git archive`, installs `node_modules`
26 only when the dependency files changed, and builds with the repo's own29 only when the dependency files changed, and builds with the repo's own
27 `node_modules/.bin/tsc` (TypeScript 7, about 1 s).30 `node_modules/.bin/tsc` (TypeScript 7, about 1 s).
283. It starts the new server on a free port, waits for `/api/ping`, then points its313. It starts the new server on a free port, waits for `/api/ping`, then points its