salvage 2026-11-06: uncommitted work from scratch/darkwing-scratch/r137/wt4 (disk cleanup #824)

This commit is contained in:
mos committed 2026-11-06 10:03:56 -06:00
1 parent 63c82f1c47
commit 0bcee35167
31 files changed
+2160 -255

No files matched your search

+130 -10
View File
@@ -1,10 +1,12 @@
// The host's git socket (#1587), `<dataRoot>/bus-host/git.sock` (0600). A
// coder's runner pushes its work to the business's working repository
// through it, and a reviewer's runner fetches a commit it was asked to
// review. The bundle is the only thing that crosses: the host never runs git
// in a session's clone and never opens a path in a session's workspace
// (packages/seat/src/working.mjs says why). The sandbox binds the socket only
// for a role that holds git.push.working or review.verdict.
// through it, a reviewer's runner fetches a commit it was asked to review,
// and a run holding an approved merge decision lands its task on the
// repository's main (row 137, #1631). The bundle is the only thing that
// crosses: the host never runs git in a session's clone and never opens a
// path in a session's workspace (packages/seat/src/working.mjs says why). The
// sandbox binds the socket only for a role that holds git.push.working or
// review.verdict, or a merge decision a landing could use (landHeld).
//
// Every connection starts with one JSON line of at most 4096 bytes.
//
@@ -17,6 +19,13 @@
// The host answers {"ok": true, "result": {project, ref, commit,
// size}} followed by <size> bytes of a bundle whose one ref is
// refs/mosaic/review, or a refusal.
// land {"cap", "op": "land", "task_ref", "decision"}
// The host answers {"ok": true, "result": {landing: "started",
// commit, base, reviewed, patch_id}} once everything that can
// refuse before the gate has passed, or a refusal. `commit` is the
// squash that will land. The gate runs after the answer, and its
// outcome is the broker's: recordLand or landRefused, each on the
// task's trail and told to the role as a RESULT message.
//
// A refusal is {"ok": false, "error": "<code>"}, and the connection ends.
// The host checks, in order: identity (a role of this business), the request
@@ -41,13 +50,23 @@
//
// A bundle file lives in bus-host/ (0700) under a random name, and the host
// deletes it on every path.
//
// A landing runs land-work's steps (scripts/land-work.sh): the broker's
// checkLand, then prepareLand (working.mjs) under the repository's lock, the
// gate check (gate-missing), the gate in the squash's worktree with the
// root's node_modules linked, and finishLand, which moves main and records.
// One landing runs at a time; another refuses land-busy. Its logs stay in
// bus-host/land/<id>/, and its worktree and Docker network go on every path.
// close() stops a running gate, and the landing is refused host-closing.
import { spawn } from "node:child_process";
import { randomBytes } from "node:crypto";
import { closeSync, createReadStream, existsSync, lstatSync, openSync, rmSync, statSync, writeSync, chmodSync } from "node:fs";
import { closeSync, createReadStream, existsSync, lstatSync, mkdirSync, openSync, rmSync, statSync, symlinkSync, writeSync, chmodSync } from "node:fs";
import { createServer } from "node:net";
import { join } from "node:path";
import { gitSocketPath } from "../../seat/src/session.mjs";
import { BUNDLE_MAX, FSCK, HAVE_MAX, push, review, validBranch, workRef, WorkingError } from "../../seat/src/working.mjs";
import { gateEnv, isMosaicStack, rootHits, runGate } from "../../seat/src/landing.mjs";
import { BUNDLE_MAX, dropLand, finishLand, FSCK, HAVE_MAX, prepareLand, push, review, validBranch, workRef, WorkingError } from "../../seat/src/working.mjs";
import { CliError } from "./errors.mjs";
const HEADER_MAX = 4096;
@@ -217,9 +236,43 @@ export async function letGo(sockets, ms = CLOSE_WAIT_MS) {
for (const s of sockets) s.destroy();
}
// Removes the Docker network a gate's Compose project left. The project is
// named after the worktree, so the network is <name>_default.
function dropNetwork(name, env) {
return new Promise((resolve) => {
const child = spawn("docker", ["network", "rm", name], { env, stdio: "ignore" });
child.on("error", () => resolve());
child.on("close", () => resolve());
});
}
export const LAND_GATE = { classify: isMosaicStack, run: runGate, dropNetwork };
// How the decision was resolved, by the resolution's via: the human through the CLI, or the
// arbiter through the broker (row 137, REQ-DEC-2). readLanding takes no other.
const RESOLVED_VIA = { cli: "via the CLI", broker: "as its arbiter, via the broker" };
// The squash's message: land-work's, applied under the git socket.
function landMessage(f, { old, base, patch_id }) {
return `${f.subject}
Squash of ${f.work_ref} (${f.task_ref}) at ${f.commit} onto main ${old},
the reviewed diff ${base}..${f.commit}. Resolved ${f.choice} by ${f.by} ${RESOLVED_VIA[f.via] ?? `via ${f.via}`} at ${f.resolved_at}.
Decision: ${f.decision}
Verdict: ${f.verdict}
Work-ref: ${f.work_ref}
Reviewed-commit: ${f.commit}
Base: main ${old}
Patch-id: ${patch_id}
Gate: full, every package suite and every scripts/test-*.sh
Applied-under: git socket land (bus host)
`;
}
// host: startHost()'s handle; business: loadBusiness() output. bundleMax,
// bodyDeadline, bodyIdle, maxActive and fsck are for the tests.
export function createGitSocket({ host, business: b, dataRoot, env = process.env, log = () => {}, bundleMax = BUNDLE_MAX, bodyDeadline: deadlineOf = bodyDeadline, bodyIdle = BODY_IDLE_MS, maxActive = MAX_ACTIVE, fsck = FSCK }) {
// bodyDeadline, bodyIdle, maxActive, fsck and gate (LAND_GATE's shape) are
// for the tests.
export function createGitSocket({ host, business: b, dataRoot, env = process.env, log = () => {}, bundleMax = BUNDLE_MAX, bodyDeadline: deadlineOf = bodyDeadline, bodyIdle = BODY_IDLE_MS, maxActive = MAX_ACTIVE, fsck = FSCK, gate = LAND_GATE }) {
const path = gitSocketPath(dataRoot);
const dir = join(dataRoot, "bus-host");
let server = null;
@@ -228,6 +281,8 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
const active = new Set();
// Connections still reading their request line (readHeader).
const waiting = new Map();
// The running landing, { abort, done }, or null.
let landing = null;
const op = async (message) => {
try {
@@ -270,6 +325,67 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
await send(socket, file);
}
async function doLand(socket, cap, req) {
const { task_ref, decision } = req;
if (typeof decision !== "string") throw new Refusal("invalid-request");
workRef(task_ref);
if (landing) throw new Refusal("land-busy", "another landing is running");
// Taken before the first await, so two requests can't both pass.
const slot = { abort: new AbortController(), done: null };
landing = slot;
try {
await startLand(socket, cap, task_ref, decision, slot);
} finally {
if (!slot.done) landing = null;
}
}
// From the broker's ticket on, each refusal is the landing's (landRefused):
// the run's cap may be gone by the time the gate ends.
async function startLand(socket, cap, task_ref, decision, slot) {
const facts = await op({ op: "checkLand", cap, args: { task_ref, decision } });
const { ticket } = facts;
const id = `land-${randomBytes(6).toString("hex")}`;
const logs = join(dir, "land", id);
const p = project(facts.project);
const where = { dataRoot, business: b.id, project: facts.project, env };
const refused = async (e, notify) => {
const code = e instanceof Refusal || e instanceof Recorded || e instanceof WorkingError ? e.code : "git-failed";
if (code === "git-failed" || code === "rollback-failed") log(`git socket: ${id}: ${code}: ${e.message}`);
// The host's own faults stay in its log; the role gets the code.
const detail = notify && code !== "git-failed" && e.message !== code ? e.message.slice(0, 1000) : null;
await host.op({ op: "landRefused", ticket, args: { code, detail, notify } }).catch((x) => log(`git socket: ${id}: refusal ${code} not recorded: ${x.code ?? x.message}`));
return code;
};
let prepared = null;
try {
if (!p) throw new Refusal("unknown-project");
mkdirSync(logs, { recursive: true, mode: 0o700 });
prepared = await prepareLand({ ...where, root: p.root, taskRef: task_ref, commit: facts.commit, dir: logs, name: id, rootHits, message: (m) => landMessage(facts, m) });
if (!(await gate.classify(p.root, env))) throw new Refusal("gate-missing", `${p.root} isn't a mosaic-stack checkout with node_modules`);
symlinkSync(join(p.root, "node_modules"), join(prepared.worktree, "node_modules"));
} catch (e) {
if (prepared) await dropLand({ ...where, worktree: prepared.worktree }).catch((x) => log(`git socket: ${id}: ${x.message}`));
throw new Recorded(await refused(e, false));
}
const { commit, base, reviewed, patch_id } = prepared;
socket.end(`${JSON.stringify({ ok: true, result: { landing: "started", commit, base, reviewed, patch_id } })}\n`);
slot.done = (async () => {
try {
const r = await gate.run({ worktree: prepared.worktree, logs, env: gateEnv(env), signal: slot.abort.signal });
if (r.aborted || slot.abort.signal.aborted) throw new Refusal("host-closing", "the host closed during the gate; nothing moved");
if (!r.ok) throw new Refusal("gate-failed", `${r.step} failed, see ${r.log}`);
await finishLand({ ...where, land: prepared, record: (m) => op({ op: "recordLand", ticket, land: { ...m, reviewed, base, patch_id } }) });
} catch (e) {
await refused(e, true);
} finally {
await dropLand({ ...where, worktree: prepared.worktree }).catch((x) => log(`git socket: ${id}: ${x.message}`));
await gate.dropNetwork(`${id}_default`, gateEnv(env));
landing = null;
}
})();
}
async function serve(socket) {
let cap = null;
let subject = null;
@@ -293,7 +409,8 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
file = join(dir, `${randomBytes(8).toString("hex")}.bundle`);
if (req.op === "push") await doPush(socket, cap, req, rest, file);
else if (req.op === "fetch") await doFetch(socket, cap, req, file);
else throw new Refusal("invalid-request", "op is push or fetch");
else if (req.op === "land") await doLand(socket, cap, req);
else throw new Refusal("invalid-request", "op is push, fetch or land");
} catch (e) {
if (e instanceof Recorded) return fail(e.code);
const code = e instanceof Refusal || e instanceof WorkingError ? e.code : "git-failed";
@@ -330,6 +447,7 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
// A push whose bundle already came finishes, under its lock, while the
// broker still runs: the launcher's close() runs before the host stops it.
// A connection still waiting for its request line hears host-closing first.
// A landing's gate is stopped, and its refusal recorded the same way.
async function close() {
closed = true;
if (!server) return;
@@ -337,7 +455,9 @@ export function createGitSocket({ host, business: b, dataRoot, env = process.env
const told = new Set(waiting.keys());
for (const stop of [...waiting.values()]) stop();
for (const socket of sockets) if (!told.has(socket)) socket.destroy();
landing?.abort.abort();
await Promise.allSettled([...active]);
await landing?.done;
// The refusal is a few bytes; once it is flushed, or the peer is gone,
// nothing more is owed, and past CLOSE_WAIT_MS it is dropped unsent.
await letGo([...told]);