A birth that failed is asked for again on a bounded row of waits, and clones again (#11)
jonas, 2026-11-26 17:11 UTC · 8afe58197003bab59ec5de7833337f3a2060ca24 · parent a4a23da
2 files changed
M core/mind-worker/src/mind/processor.test.ts
@@ -174,6 +174,88 @@ ]); }); +test("a birth that failed is asked for again after bounded waits in one incarnation, and its next try clones the repo whose clone failed", async () => { + vi.useFakeTimers(); + try { + const mind = mindOf({ owes: false }); + const unborn: MindState = { + ...mind.state, + creation: { status: "requested", offset: 2 }, + configured: configuredEvent({ + repos: [{ path: "/repos/config", origin: "https://git.example.org/repos/config-template" }], + }).payload as MindState["configured"], + repos: {}, + repoTips: {}, + newestAsking: null, + published: null, + lastPublicationFactOffset: null, + }; + const failed: MindState = { ...unborn, creation: { status: "failed", offset: 3 } }; + const typesOf = () => mind.facts.map((fact) => fact.type.split("/").slice(1).join("/")); + // a new mind's stack is still being deployed at its first request: the repo cannot be made + mind.root.refusals.push( + "create: Alchemy worker is being deployed", + "create: Alchemy worker is being deployed", + ); + mind.deliver(unborn); + await mind.settled(); + expect(mind.facts.at(-1)).toMatchObject({ + type: "events.iterate.com/mind/create-failed", + payload: { error: "Alchemy worker is being deployed" }, + }); + // the first ask comes at once, and it fails the same way + mind.deliver(failed); + await mind.settled(); + mind.deliver(unborn); + await mind.settled(); + expect(typesOf()).toEqual(["mind/create-failed", "mind/create-requested", "mind/create-failed"]); + // the second ask waits 5 s, and a pass in between asks for nothing more + mind.deliver(failed); + mind.deliver(failed); + await vi.advanceTimersByTimeAsync(4_900); + expect(typesOf()).toHaveLength(3); + await vi.advanceTimersByTimeAsync(100); + await mind.settled(); + expect(typesOf().at(-1)).toBe("mind/create-requested"); + // this try makes the repo, and its clone fails: the birth fails, and the try after it clones again + mind.root.refusals.push("pull: the origin did not answer"); + mind.deliver(unborn); + await mind.settled(); + expect(mind.facts.at(-1)).toMatchObject({ payload: { error: "the origin did not answer" } }); + mind.deliver(failed); + await vi.advanceTimersByTimeAsync(15_000); + await mind.settled(); + mind.deliver(unborn); + await mind.settled(); + expect(mind.root.did).toEqual([ + "create /repos/config", + "setOrigin /repos/config https://git.example.org/repos/config-template", + "setOrigin /repos/config https://git.example.org/repos/config-template", + 'pull /repos/config {"remote":"https://git.example.org/repos/config-template"}', + ]); + // THE ROW OF WAITS ENDS: three more asks (30 s, 60 s, 120 s), then none in this incarnation + const asks = () => typesOf().filter((type) => type === "mind/create-requested").length; + expect(asks()).toBe(3); + for (const wait of [30_000, 60_000, 120_000]) { + mind.deliver(failed); + await vi.advanceTimersByTimeAsync(wait); + await mind.settled(); + } + expect(asks()).toBe(6); + mind.deliver(failed); + await vi.advanceTimersByTimeAsync(600_000); + await mind.settled(); + expect(asks()).toBe(6); + // a new incarnation starts the row again + mind.restart(); + mind.deliver(failed); + await mind.settled(); + expect(asks()).toBe(7); + } finally { + vi.useRealTimers(); + } +}); + test("a birth also makes the repos that the entry repo's package.json names, each cloned from beside where the entry repo came from", async () => { const mind = mindOf({ owes: false, @@ -844,7 +926,13 @@ }); // The root's own schedules and rows, as the daily refresh of the grants asks for them: what it // read and what it wrote, in order. - const root = { schedule: there.schedule, row: there.row, did: [] as string[] }; + // `refusals`: what the next call of a repo's `create` or `pull` throws, as "<verb>: <error>" + const root = { + schedule: there.schedule, + row: there.row, + did: [] as string[], + refusals: [] as string[], + }; const ofTheRoot = (expression: ItxExpression): { answer: unknown } | undefined => { const [registry, last] = [expression[2], expression.at(-1)]; const [verb, argument] = Array.isArray(last) ? last : []; @@ -892,6 +980,8 @@ // the repo's own facet (`cd(path).facets.get("repo")`) is one that a real repo refuses. const [verb, ...args] = expression.at(-1) as [string, ...unknown[]]; const at = expression[2]; + if (at === "repos" && root.refusals[0]?.startsWith(`${verb}: `)) + throw new Error(root.refusals.shift()!.slice(verb.length + 2)); if (at === "repos" && verb === "create") { root.did.push(`create ${args[0]}`); return { path: args[0] };
M core/mind-worker/src/mind/processor.ts
@@ -36,6 +36,14 @@ // After the last give-up the commit stays owed to the mind's next incarnation. const PUBLICATION_RERUN_WAITS_MS = [30_000, 30_000] as const; +// A BIRTH THAT FAILED IS ASKED FOR AGAIN after each of these waits, in one incarnation. A new +// mind's first request can come while its stack is still being deployed: a Worker that Alchemy +// has not finished answers "Alchemy worker is being deployed", and two tries failed so within a +// second (blimro.com, 26 Nov 2026), after which the mind had no repo until its next incarnation, +// ten minutes later. After the last wait the failure stands for this incarnation, and the next +// one starts the waits again. Each failed try is a `mind/create-failed` on the log. +const CREATION_RETRY_WAITS_MS = [0, 5_000, 15_000, 30_000, 60_000, 120_000] as const; + const ingressToConfig = () => ({ type: "events.iterate.com/itx/ingress-configured" as const, idempotencyKey: "itx/ingress-configured", @@ -87,10 +95,13 @@ // per incarnation; the durable ground is the state #creating = false; - #retriedFailedCreation = false; - /** The repos this incarnation already made, and those it already tried to clone: the fact of - * a new repo reaches the state a moment after the call, and a clone that keeps failing is - * tried once per incarnation, not at every pass. */ + /** How many times this incarnation asked again for a birth that failed, and whether one such + * ask waits now (`CREATION_RETRY_WAITS_MS`). */ + #creationRetries = 0; + #creationRetryWaits = false; + /** The repos this incarnation already made, and those it already cloned or is cloning: the + * fact of a new repo reaches the state a moment after the call. A clone that failed fails + * the birth, and the birth's next try clones again. */ #repoMade = new Set<string>(); #cloneTried = new Set<string>(); /** Whether this incarnation made the root's own subscription row match the contract, or is @@ -297,13 +308,22 @@ // The persons' grants are refreshed once a day: the root gets what does that, and looks // again at most once an hour (`#armGrantRefresh`). if (state.creation?.status === "created") runInBackground(() => this.#armGrantRefresh()); - // A failed creation is asked for again, once per incarnation: only the root's birth writes the - // request, so a failure that stood would leave the mind with no site for good. - if (state.creation?.status === "failed" && !this.#retriedFailedCreation) { - this.#retriedFailedCreation = true; - runInBackground(() => - append({ type: "events.iterate.com/mind/create-requested", payload: {} }), - ); + // A failed creation is asked for again, after a bounded row of waits in each incarnation + // (`CREATION_RETRY_WAITS_MS`): only the root's birth writes the request, so a failure that + // stood would leave the mind with no site for good. + if (state.creation?.status === "failed") { + const wait = CREATION_RETRY_WAITS_MS[this.#creationRetries]; + if (wait === undefined || this.#creationRetryWaits) return; + this.#creationRetries += 1; + this.#creationRetryWaits = true; + runInBackground(async () => { + try { + if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait)); + await append({ type: "events.iterate.com/mind/create-requested", payload: {} }); + } finally { + this.#creationRetryWaits = false; + } + }); return; } // The config says which repos the mind is born with: nothing is made before it has come. @@ -364,8 +384,14 @@ } if (!origin || state.repoTips[path] || this.#cloneTried.has(path)) return; this.#cloneTried.add(path); - await call(ofRepo(path, "setOrigin", origin)); - await call(ofRepo(path, "pull", { remote: origin })); + try { + await call(ofRepo(path, "setOrigin", origin)); + await call(ofRepo(path, "pull", { remote: origin })); + } catch (error) { + // the birth fails with this error, and its next try clones this repo again + this.#cloneTried.delete(path); + throw error; + } }; const { repos, entry } = state.configured!; for (const { path, origin } of repos) await make(path, origin);