Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
240 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

API: notifications over REST and MCP, with notifications scopes1#!/usr/bin/env node
2// Where should g1t's Workers run? A throwaway experiment that answers it
3// with numbers, without touching any production Worker.
4//
5// It deploys a handful of tiny Workers named g1t-probe-*, one per
6// placement setting, each bound read-only in practice (it runs only
7// `SELECT 1`) to the g1t-repos D1 database, plus g1t-probe-edge, which runs
8// near the caller and calls each of the others through a service binding,
9// the way the site calls the services. Then it measures them from this
10// machine, prints a table, and deletes them.
11//
12// node scripts/perf/placement-probe.mjs deploy # make the probes (needs `wrangler login`)
13// node scripts/perf/placement-probe.mjs measure # 1 warm-up + 7 requests each, a table
14// node scripts/perf/placement-probe.mjs delete # remove them all
15// node scripts/perf/placement-probe.mjs apply '{"region":"aws:us-west-1"}'
16// # write a placement into every config that should have it
17// node scripts/perf/placement-probe.mjs apply '{"mode":"smart"}' # back to Smart Placement
18//
The docs folder is gone, and what it held lives where people read it: how a self-hosted g1t runs and how to deploy g1t to Cloudflare are pages on docs.g1t.sh under Run g1t yourself, and speed, rate limits and operating g1t.sh are sections of CONTRIBUTING.md; code that cited a file in docs/ now points to the page or section that covers it, or says what it means itself, and applied migrations and the runner images are left as they were.19// Nothing here runs in CI.
API: notifications over REST and MCP, with notifications scopes20
21import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
22import { tmpdir } from "node:os";
23import { join } from "node:path";
24
25import { exec, wranglerEnv } from "../deploy/cloudflare.mjs";
26import { ROOT } from "../deploy/stack.mjs";
27
28const WRANGLER = join(ROOT, "node_modules/wrangler/bin/wrangler.js");
29const DATABASE = { name: "g1t-repos", id: "f9544c51-c3bf-4621-a96f-8a6d5cf24a97" };
30
31/** Each placement tried. Regions near D1's WNAM, one far one as a control. */
32export const VARIANTS = [
33 { name: "off", placement: { mode: "off" } },
34 { name: "smart", placement: { mode: "smart" } },
35 { name: "aws-us-west-1", placement: { region: "aws:us-west-1" } },
36 { name: "aws-us-west-2", placement: { region: "aws:us-west-2" } },
37 { name: "gcp-us-west1", placement: { region: "gcp:us-west1" } },
38 { name: "gcp-us-west2", placement: { region: "gcp:us-west2" } },
39 { name: "aws-us-east-1", placement: { region: "aws:us-east-1" } },
40];
41
42/**
43 * The Workers that read D1 or call services that do, by config: what
Merge branch 'worktree-agent-ac950b9c891220ad3'44 * `apply` sets the placement of. The rest (pages, og, runner,
API: notifications over REST and MCP, with notifications scopes45 * status, docs) serve from the edge or have no data of their own.
46 */
47export const PLACED = [
48 "apps/web/wrangler.jsonc",
49 "apps/api/wrangler.jsonc",
50 "apps/sudo/wrangler.jsonc",
51 "services/actions/wrangler.jsonc",
52 "services/billing/wrangler.jsonc",
53 "services/deployments/wrangler.jsonc",
54 "services/events/wrangler.jsonc",
55 "services/identity/wrangler.jsonc",
56 "services/integrations/wrangler.jsonc",
57 "services/projects/wrangler.jsonc",
58 "services/repos/wrangler.jsonc",
59 "services/search/wrangler.jsonc",
60 "services/security/wrangler.jsonc",
61 "services/webhooks/wrangler.jsonc",
62 "services/work/wrangler.jsonc",
63];
64
65const PROBE = `
66const QUERIES = 5;
67async function ranIn() {
68 const trace = await (await fetch("https://cloudflare.com/cdn-cgi/trace")).text();
69 return /colo=(\\w+)/.exec(trace)?.[1] ?? null;
70}
71async function timedQueries(db) {
72 const ms = [];
73 let meta = null;
74 for (let i = 0; i < QUERIES; i++) {
75 const started = Date.now();
76 const result = await db.prepare("SELECT 1 AS one").run();
77 ms.push(Date.now() - started);
78 meta = result.meta;
79 }
80 return { ms, region: meta?.served_by_region ?? null, primary: meta?.served_by_primary ?? null, sql_ms: meta?.timings?.sql_duration_ms ?? null };
81}
82export default {
83 async fetch(request, env) {
84 const { pathname } = new URL(request.url);
85 if (pathname === "/chain") {
86 const names = Object.keys(env).filter((key) => key.startsWith("TO_"));
87 const out = {};
88 await Promise.all(names.map(async (name) => {
89 const started = Date.now();
90 try {
91 const answer = await (await env[name].fetch("https://probe/")).json();
92 out[name.slice(3).toLowerCase().replaceAll("_", "-")] = { rpc_ms: Date.now() - started, ran_in: answer.ran_in, primary_ms: answer.primary.ms };
93 } catch (error) {
94 out[name] = { error: String(error) };
95 }
96 }));
97 return Response.json({ worker: env.NAME, ingress: request.cf?.colo ?? null, ran_in: await ranIn(), chain: out });
98 }
99 const where = await ranIn();
100 const primary = await timedQueries(env.DB);
101 const session = await timedQueries(env.DB.withSession("first-unconstrained"));
102 return Response.json({ worker: env.NAME, ingress: request.cf?.colo ?? null, ran_in: where, primary, session });
103 },
104};
105`;
106
107function configFor(variant, bindings = []) {
108 return {
109 name: `g1t-probe-${variant.name}`,
110 main: "worker.js",
111 account_id: "1e6f2cffa3f445920836e8ebe446bb58",
112 compatibility_date: "2026-09-26",
113 workers_dev: true,
114 preview_urls: false,
115 placement: variant.placement,
116 vars: { NAME: variant.name },
117 d1_databases: [{ binding: "DB", database_name: DATABASE.name, database_id: DATABASE.id }],
118 services: bindings,
119 };
120}
121
122async function wrangler(args, cwd) {
123 const { code, out } = await exec(process.execPath, [WRANGLER, ...args], { cwd, env: wranglerEnv() });
124 return { code, out };
125}
126
127const URLS = join(tmpdir(), "g1t-placement-probe", "urls.json");
128
129async function deploy() {
130 const urls = {};
131 const edge = { name: "edge", placement: { mode: "off" } };
132 const bindings = VARIANTS.map((v) => ({ binding: `TO_${v.name.toUpperCase().replaceAll("-", "_")}`, service: `g1t-probe-${v.name}` }));
133 for (const [variant, extra] of [...VARIANTS.map((v) => [v, []]), [edge, bindings]]) {
134 const dir = join(tmpdir(), "g1t-placement-probe", variant.name);
135 rmSync(dir, { recursive: true, force: true });
136 mkdirSync(dir, { recursive: true });
137 writeFileSync(join(dir, "worker.js"), PROBE);
138 writeFileSync(join(dir, "wrangler.json"), JSON.stringify(configFor(variant, extra), null, 2));
139 process.stdout.write(`deploying g1t-probe-${variant.name} ${JSON.stringify(variant.placement)} … `);
140 const { code, out } = await wrangler(["deploy", "--config", "wrangler.json"], dir);
141 const url = /https:\/\/g1t-probe-[\w-]+\.[\w-]+\.workers\.dev/.exec(out)?.[0];
142 console.log(code === 0 ? url ?? "deployed (no URL printed)" : "FAILED");
143 if (code !== 0) console.log(out.split("\n").slice(-15).join("\n"));
144 if (url) urls[variant.name] = url;
145 }
146 writeFileSync(URLS, JSON.stringify(urls, null, 2));
147 console.log("\nSmart Placement needs traffic from several places and up to 15 minutes before it moves anything;");
148 console.log("the region variants are placed at once. Wait a minute, then: node scripts/perf/placement-probe.mjs measure");
149}
150
151const median = (values) => {
152 const sorted = [...values].filter((v) => typeof v === "number").sort((a, b) => a - b);
153 return sorted.length ? sorted[Math.floor(sorted.length / 2)] : null;
154};
155
156async function timedFetch(url) {
157 const started = performance.now();
158 const response = await fetch(url, { headers: { "cache-control": "no-cache" } });
159 const ttfb = performance.now() - started;
160 const body = await response.json();
161 return { ttfb: Math.round(ttfb), placement: response.headers.get("cf-placement"), body };
162}
163
164async function measure() {
165 const urls = JSON.parse(readFileSync(URLS, "utf8"));
166 const rows = [];
167 for (const variant of VARIANTS) {
168 const url = urls[variant.name];
169 if (!url) continue;
170 await timedFetch(url).catch(() => null);
171 const runs = [];
172 for (let i = 0; i < 7; i++) runs.push(await timedFetch(url));
173 const last = runs.at(-1);
174 rows.push({
175 variant: variant.name,
176 "cf-placement": last.placement ?? "",
177 "ran in": last.body.ran_in,
178 "ttfb ms (p50)": median(runs.map((r) => r.ttfb)),
179 "D1 primary ms/query (p50)": median(runs.flatMap((r) => r.body.primary.ms)),
180 "D1 session ms/query (p50)": median(runs.flatMap((r) => r.body.session.ms)),
181 "session served by": `${last.body.session.region ?? "?"}${last.body.session.primary ? " (primary)" : ""}`,
182 });
183 }
184 console.table(rows);
185 if (urls.edge) {
186 await timedFetch(`${urls.edge}/chain`).catch(() => null);
187 const chains = [];
188 for (let i = 0; i < 5; i++) chains.push(await timedFetch(`${urls.edge}/chain`));
189 const names = Object.keys(chains[0].body.chain);
190 console.log(`\nFrom g1t-probe-edge (runs in ${chains[0].body.ran_in}) through a service binding, as the site calls a service:`);
191 console.table(
192 names.map((name) => ({
193 target: name,
194 "target ran in": chains.at(-1).body.chain[name].ran_in,
195 "rpc ms (p50)": median(chains.map((c) => c.body.chain[name].rpc_ms)),
196 "its D1 ms/query (p50)": median(chains.flatMap((c) => c.body.chain[name].primary_ms ?? [])),
197 })),
198 );
199 }
200 console.log("\nPick the region with the lowest D1 primary ms/query (the database's neighbour), then:");
201 console.log(` node scripts/perf/placement-probe.mjs apply '{"region":"<it>"}'`);
202}
203
204async function remove() {
205 for (const name of [...VARIANTS.map((v) => v.name), "edge"]) {
206 const { code } = await wrangler(["delete", "--name", `g1t-probe-${name}`, "--force"], ROOT);
207 console.log(`g1t-probe-${name}: ${code === 0 ? "deleted" : "not deleted (already gone?)"}`);
208 }
209}
210
211/** Sets `"placement"` in every config in PLACED, keeping the line's place. */
212export function withPlacement(text, placement) {
213 const fields = Object.entries(placement).map(([key, value]) => `"${key}": ${JSON.stringify(value)}`);
214 const line = `"placement": { ${fields.join(", ")} },`;
215 if (/"placement":\s*\{[^}]*\},?/.test(text)) return text.replace(/"placement":\s*\{[^}]*\},?/, line);
216 return text.replace(/(\r?\n)([ \t]*)("compatibility_date":[^\r\n]*)/, (_, nl, indent, date) => `${nl}${indent}${date}${nl}${indent}${line}`);
217}
218
219function apply(json) {
220 const placement = JSON.parse(json);
221 for (const file of PLACED) {
222 const path = join(ROOT, file);
223 const before = readFileSync(path, "utf8");
224 const after = withPlacement(before, placement);
225 if (after !== before) writeFileSync(path, after);
226 console.log(`${file}: ${after === before ? "unchanged" : JSON.stringify(placement)}`);
227 }
228}
229
230const [command, argument] = process.argv.slice(2);
231if (process.argv[1]?.replaceAll("\\", "/").endsWith("scripts/perf/placement-probe.mjs")) {
232 if (command === "deploy") await deploy();
233 else if (command === "measure") await measure();
234 else if (command === "delete") await remove();
235 else if (command === "apply" && argument) apply(argument);
236 else {
237 console.log("usage: node scripts/perf/placement-probe.mjs deploy | measure | delete | apply '<placement json>'");
238 process.exitCode = 1;
239 }
240}

This file's history is long; its oldest lines are credited to the oldest commit read.