Public: anyone can read this; the crew can change it.

From outside: Freshly spawned child forge: the forge fork is empty and cannot fetch its upstream (DNS resolution fails inside the child) #13722

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13716-from-outside-freshly-spawned-child-forge → main 3 commits fork 1 thread · 1 unresolved

  • Merged. scout/13716-from-outside-freshly-spawned-child-forge was merged into main as eba7ded.

Review

Sign in to review this change.
2 files changed +137 −8
src/spawn/upstream.ts +45 −7
@@ -20,6 +20,7 @@ import { getUserById, getUserByName } from "../core/users.ts";
2020 import { runGit } from "../git/env.ts";
2121 import { repoDir } from "../git/repos.ts";
2222 import { basicHeader, gitAuthEnv, redact, secretsIn } from "../origins/credentials.ts";
23 import { vettedEnv } from "../origins/git-mirror.ts";
2324 import { forgeLinkOf, originOf } from "./chain.ts";
2425 import { parentCredential } from "./parent_credential.ts";
2526 import { newestCommits, takeFromParent, type Taken } from "./take.ts";
@@ -136,17 +137,30 @@ async function run(app: App, options: { remote?: string; now?: number }): Promis
136137 const scope = URL.parse(remote)?.origin; // a local path has none, and is sent nothing
137138 const auth = cred === undefined || scope === undefined ? {} : gitAuthEnv(scope, basicHeader(cred.child, cred.token));
138139 const secrets = cred === undefined ? [] : [...secretsIn(auth), cred.token];
139 const fetched = await runGit(
140 ["fetch", "--quiet", "--no-tags", "--prune", "--", remote, "+refs/heads/*:refs/upstream/*", "+HEAD:refs/upstream/HEAD"],
141 { cwd: dir, timeoutMs: FETCH_TIMEOUT_MS, env: auth },
142 );
140 // The parent's name is resolved here, as a proposal push resolves it, and git is pinned to the
141 // answer (or sent through the cell's gate): a child whose git cannot look the name up still
142 // reaches it.
143 let env: Record<string, string> = auth;
144 let refusal = "";
145 if (scope !== undefined) {
146 const vetted = await vettedEnv(remote, auth, undefined, [new URL(scope).hostname]);
147 if (vetted.ok) env = vetted.value;
148 else refusal = vetted.error.message;
149 }
150 const fetched = refusal !== ""
151 ? undefined
152 : await runGit(
153 ["fetch", "--quiet", "--no-tags", "--prune", "--", remote, "+refs/heads/*:refs/upstream/*", "+HEAD:refs/upstream/HEAD"],
154 { cwd: dir, timeoutMs: FETCH_TIMEOUT_MS, env },
155 );
143156 let state: UpstreamState;
144 if (!fetched.ok) {
145 const line = fetched.error.stderr.split("\n").find((l) => l.trim() !== "")?.trim();
146 const error = redact(line ?? "", ...secrets) || (fetched.error.code === 137 ? "the fetch timed out after 60 s" : "the fetch failed");
157 if (fetched === undefined || !fetched.ok) {
158 const line = fetched === undefined ? refusal : fetched.error.stderr.split("\n").find((l) => l.trim() !== "")?.trim();
159 const error = redact(line ?? "", ...secrets) || (fetched?.error.code === 137 ? "the fetch timed out after 60 s" : "the fetch failed");
147160 const before = await upstreamState(app);
148161 state = { behind: before?.behind ?? null, parentTip: before?.parentTip ?? "", checkedAt, error: error.slice(0, 200), toldTip: before?.toldTip ?? "" };
149162 } else {
163 if (link.mode === "take-updates") await populateEmpty(app, o, n, dir, link.upstream);
150164 const tip = await runGit(["rev-parse", "--verify", "-q", "refs/upstream/HEAD"], { cwd: dir });
151165 const count = tip.ok
152166 ? await runGit(["rev-list", "--count", "HEAD..refs/upstream/HEAD"], { cwd: dir })
@@ -189,6 +203,30 @@ async function run(app: App, options: { remote?: string; now?: number }): Promis
189203 }
190204 }
191205
206 /**
207 * A fork that arrived with no code (its branch unborn) takes the parent's
208 * tip as its default branch the first time the parent is fetched, so the
209 * follow loop has something to compare and to propose from. A fork that has
210 * a branch is never moved here: taking updates stays `takeFromParent`'s.
211 */
212 export async function populateEmpty(app: App, o: string, n: string, dir: string, upstream: string): Promise<boolean> {
213 const repo = getRepo(app.db, o, n);
214 if (repo === undefined) return false;
215 const ref = `refs/heads/${repo.defaultBranch}`;
216 const have = await runGit(["rev-parse", "--verify", "-q", ref], { cwd: dir });
217 if (have.ok) return false;
218 const up = await runGit(["rev-parse", "--verify", "-q", "refs/upstream/HEAD"], { cwd: dir });
219 const tip = up.ok ? up.value.trim() : "";
220 if (!TIP.test(tip)) return false;
221 if (!(await runGit(["update-ref", ref, tip, "0".repeat(40)], { cwd: dir })).ok) return false;
222 app.bus.append({
223 kind: "push",
224 repoId: repo.id,
225 payload: { updates: [{ ref, oldOid: "0".repeat(40), newOid: tip }], followed: upstream },
226 });
227 return true;
228 }
229
192230 /**
193231 * One inbox card for the owner: an issue on the fork assigned to them, which
194232 * the inbox shows as "assigned to you", and which links the chain page. The
test/spawn-upstream.test.ts +92 −1
@@ -11,7 +11,7 @@ import { join } from "node:path";
1111 import { createApp, type App } from "../src/app.ts";
1212 import { runGit } from "../src/git/env.ts";
1313 import { repoDir } from "../src/git/repos.ts";
14 import { checkUpstream, upstreamRemote, upstreamState, upstreamWords } from "../src/spawn/upstream.ts";
14 import { checkUpstream, populateEmpty, upstreamRemote, upstreamState, upstreamWords } from "../src/spawn/upstream.ts";
1515 import { quietLogs } from "./helpers/globals.ts";
1616
1717 quietLogs();
@@ -79,6 +79,97 @@ describe("looking upstream", () => {
7979 expect((await upstreamState(app))?.error).toBe(s!.error);
8080 });
8181
82 // A second app whose fork `kid2/forge` is born empty or with a branch, following in `mode`.
83 const kidFork = async (name: string, mode: string, branch: boolean) => {
84 const data = join(dir, `${name}-data`);
85 const kid = createApp({ dataDir: data, url: `https://${name}.test`, port: 0 });
86 const { createUser } = await import("../src/core/users.ts");
87 const { createRepo } = await import("../src/core/repos.ts");
88 const owner = await createUser(kid.db, { origin: "edge", username: name, email: `${name}@forge.example` });
89 expect(owner.ok).toBe(true);
90 if (!owner.ok) throw new Error("no owner");
91 const made = await createRepo(kid.db, kid.reposRoot, { ownerId: owner.value.id, name: "forge", visibility: "public" });
92 expect(made.ok).toBe(true);
93 await Bun.write(
94 join(data, "parent.json"),
95 JSON.stringify({
96 upstream: "https://parent.test",
97 forgeCode: { repo: `${name}/forge`, upstream: "root/forge", mode, tip: "a".repeat(40), chain: [] },
98 }),
99 );
100 const dest = repoDir(kid.reposRoot, name, "forge");
101 if (branch) {
102 await git(dest, "fetch", "-q", parent, "+refs/heads/main:refs/heads/main");
103 await git(dest, "symbolic-ref", "HEAD", "refs/heads/main");
104 }
105 return { kid, dest };
106 };
107
108 test("an empty take-updates fork takes the parent's tip as its branch and says so on the bus", async () => {
109 const { kid, dest } = await kidFork("kidempty", "take-updates", false);
110 const pushes: { payload: unknown }[] = [];
111 kid.bus.subscribe((e) => {
112 if (e.kind === "push") pushes.push(e);
113 }, "local");
114 const s = await checkUpstream(kid, { remote: parent, now: 5000 });
115 expect(s).toMatchObject({ behind: 0, error: "" });
116 expect(await git(dest, "rev-parse", "refs/heads/main")).toBe(s!.parentTip);
117 expect(pushes.length).toBe(1);
118 expect(JSON.stringify(pushes[0]!.payload)).toContain('"followed":"root/forge"');
119 });
120
121 test("a tell-me fork with no branch stays unborn", async () => {
122 const { kid, dest } = await kidFork("kidtell", "tell-me", false);
123 await checkUpstream(kid, { remote: parent, now: 5000 });
124 const r = await runGit(["rev-parse", "--verify", "-q", "refs/heads/main"], { cwd: dest });
125 expect(r.ok).toBe(false);
126 });
127
128 test("a fork that already has a branch is not populated and emits nothing", async () => {
129 const { kid, dest } = await kidFork("kidhas", "take-updates", true);
130 await git(dest, "fetch", "-q", parent, "+refs/heads/main:refs/upstream/HEAD");
131 const before = await git(dest, "rev-parse", "refs/heads/main");
132 const pushes: unknown[] = [];
133 kid.bus.subscribe((e) => {
134 if (e.kind === "push") pushes.push(e);
135 }, "local");
136 expect(await populateEmpty(kid, "kidhas", "forge", dest, "root/forge")).toBe(false);
137 expect(await git(dest, "rev-parse", "refs/heads/main")).toBe(before);
138 expect(pushes.length).toBe(0);
139 });
140
141 test("a parent name that does not resolve is said so, not thrown", async () => {
142 const file = join(app.config.dataDir, "parent.json");
143 const doc = await Bun.file(file).json();
144 try {

Add a sibling test for a refused host. Assert the error contains "refused" and that the prior state is kept.

145 await Bun.write(file, JSON.stringify({ ...doc, upstream: "https://nowhere.invalid" }));
146 const s = await checkUpstream(app, { now: 6000 });
147 expect(s?.error).toContain("could not be resolved");
148 } finally {
149 await Bun.write(file, JSON.stringify(doc));
150 }
151 });
152
153 test("a parent host that is refused is said so, and the earlier state is kept", async () => {
154 const file = join(app.config.dataDir, "parent.json");
155 const doc = await Bun.file(file).json();
156 const seeded = await checkUpstream(app, { remote: parent, now: 6500 });
157 expect(seeded?.error).toBe("");
158 expect(seeded?.parentTip).toMatch(/^[0-9a-f]{40}$/);
159 try {
160 // An address literal is never trusted by name, so a private one (not the loopback the preload
161 // exempts) is refused before git runs.
162 await Bun.write(file, JSON.stringify({ ...doc, upstream: "https://10.0.0.1" }));
163 const s = await checkUpstream(app, { now: 7000 });
164 expect(s?.error).toContain("refused");
165 expect(s?.error).not.toContain("could not be resolved");
166 expect(s?.behind).toBe(seeded!.behind);
167 expect(s?.parentTip).toBe(seeded!.parentTip);
168 } finally {
169 await Bun.write(file, JSON.stringify(doc));
170 }
171 });
172
82173 test("words", () => {
83174 const base = { behind: 3, parentTip: "", checkedAt: 0, error: "", toldTip: "" };
84175 const at = 12 * 60_000;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs