From 7f5f285b60571323467405194849840fc3db7495 Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 03:45:27 +0100 Subject: [PATCH] FORGE-3: a merge call says one merge-called, and the start says an unsaid one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Once its members are merged or refused the call says merge-called on merge-called. with its landed members (base and merge commit) and its refused ones, and is kept said; a call that landed none says none. At the start every call not said says it for what landed. Builds: design 47 H §736, §766 (said again at the start); H-A-18, H-A-47 (said), H-A-71 (2) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy --- modules/gitea/calls.ts | 41 ++++++++++++++++++++++++++++++- modules/gitea/index.ts | 8 ++++-- modules/gitea/mergecall.ts | 24 ++++++++++++++---- modules/gitea/seat.ts | 4 ++- modules/gitea/test/calls.test.ts | 42 ++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 9 deletions(-) diff --git a/modules/gitea/calls.ts b/modules/gitea/calls.ts index 4f22c968f..106f156d0 100644 --- a/modules/gitea/calls.ts +++ b/modules/gitea/calls.ts @@ -7,7 +7,9 @@ import { readFileSync } from "node:fs"; import type { State } from "@novox/mesh-sdk/state"; -import { callsByCallCallKey, parseCallsByCallCallKey, type Call, type CallMember } from "@novox/mesh-delivery-contracts-git"; +import { + callsByCallCallKey, mergeCalledSubject, parseCallsByCallCallKey, type Call, type CallMember, type MergeCalledEvent, +} from "@novox/mesh-delivery-contracts-git"; /** call-retention's default, in days (§766). */ export const CALL_RETENTION_DAYS = 30; @@ -70,3 +72,40 @@ export class Calls { export const keptMember = (m: { repository: string; number: number; branch: string; head: string; state: CallMember["state"]; commit?: string; why?: string }): CallMember => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, state: m.state, ...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) }); + +/** Says a seat event on its subject, under an id the bus keeps once. */ +export type Say = (subject: string, body: unknown, id?: string) => Promise; + +/** The merge-called of a kept call (design 47 H §736, §767): its landed members, each with its base and merge commit, and its + * refused ones; undefined for a call that landed no member, which says none. */ +export function mergeCalledOf(c: KeptCall): MergeCalledEvent | undefined { + const landed = c.members.filter((m) => m.state === "merged" && m.commit); + if (landed.length === 0) return undefined; + // A member judged and not merged (a stop mid-call) is in neither: its head is an untaken head (§740). + const refused = c.members.filter((m) => m.state === "refused"); + return { call: c.call, by: c.by, account: c.account, at: c.at, + members: landed.map((m) => ({ repository: m.repository, number: m.number, branch: m.branch, head: m.head, commit: m.commit! })), + ...(refused.length ? { refused: refused.map((m) => ({ repository: m.repository, number: m.number, why: m.why ?? "not merged" })) } : {}) }; +} + +/** Says a call's merge-called, when it landed a member; answers whether it said one. */ +export async function sayCall(c: KeptCall, say: Say): Promise { + const body = mergeCalledOf(c); + if (!body) return false; + await say(mergeCalledSubject(c.call), body, `merge-called.${c.call}`); + return true; +} + +/** At the start (§766): every call whose event is not said says merge-called for what landed, and is kept said, so a stop + * between the last merge and the event loses nothing it recorded. Answers the calls said. */ +export async function sayUnsaid(calls: Calls, say: Say, log: (line: string) => void = () => {}): Promise { + const said: string[] = []; + for (const c of await calls.all()) { + if (c.said) continue; + if (await sayCall(c, say)) said.push(c.call); + const { call, ...value } = c; + await calls.put(call, { ...value, said: true }); + } + if (said.length) log(`merge-called said again at the start for ${said.join(", ")}`); + return said; +} diff --git a/modules/gitea/index.ts b/modules/gitea/index.ts index db90a36a5..6096a9720 100644 --- a/modules/gitea/index.ts +++ b/modules/gitea/index.ts @@ -50,7 +50,8 @@ import { FORGE_CONTAINER, seatTools } from "./seat.js"; import { REGISTRATION_CHANGED_EVENT, Registrations } from "./registrations.js"; import { FailedSteps, Heads, readHeads } from "./failedsteps.js"; import { failedStepsTools } from "./tools/failedsteps.js"; -import { Calls, callRetentionDays } from "./calls.js"; +import { Calls, callRetentionDays, sayUnsaid } from "./calls.js"; +import { seatPublish } from "@novox/mesh-sdk/runtime"; import { seat as GIT, events as GIT_EVENTS, type BranchMovedEvent, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent } from "@novox/mesh-delivery-contracts-git"; import { seat as RUNNER, events as RUNNER_EVENTS, type StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner"; import { run } from "./delivery.js"; @@ -403,6 +404,9 @@ const heldHeads = new Heads(); let reloadHeads: () => Promise = async () => {}; // The merge calls, each written before its first merge (calls.ts; design 47 H §766). const calls = new Calls(state("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`)); +// At the start, every call whose merge-called is not said says it for what landed (§766). +void sayUnsaid(calls, seatPublish, (line) => console.log(`[gitea] ${line}`)).catch((err) => + console.error(`[gitea] the merge calls not said could not be said at the start — ${err instanceof Error ? err.message : err}`)); const failedSteps = new FailedSteps(state("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit)); // The `seen` keys past their 30 days are deleted at the start, as on each step-ended (design 47 §581). @@ -414,7 +418,7 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }), ...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, statuses: gates, registered: () => registrations.written, judge: (head) => failedSteps.judge(head), - watched: () => registrations.watched, calls }) : []), + watched: () => registrations.watched, calls, say: seatPublish }) : []), ...failedStepsTools(failedSteps, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); })]); /** What the forge's event asks of the announcer at once (issue 729, 1422): see watch.ts. */ diff --git a/modules/gitea/mergecall.ts b/modules/gitea/mergecall.ts index 29fc96005..11c772c29 100644 --- a/modules/gitea/mergecall.ts +++ b/modules/gitea/mergecall.ts @@ -9,11 +9,11 @@ import { randomBytes } from "node:crypto"; import { UNPROVEN, callerOf } from "@novox/mesh-sdk/callers"; -import type { MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contracts-git"; +import type { Call, MemberOutcome, MergePullAnswer } from "@novox/mesh-delivery-contracts-git"; import type { GiteaClient } from "./client.js"; import { Refused, checkName } from "./guard.js"; import type { Judged } from "./failedsteps.js"; -import { keptMember, type Calls } from "./calls.js"; +import { keptMember, sayCall, type Calls, type Say } from "./calls.js"; /** What a merge call reaches beside the forge's API. */ export interface CallDeps { @@ -28,6 +28,8 @@ export interface CallDeps { /** The state calls (calls.ts): the call is written before its first merge and as each member merges (§766). */ calls?: Calls; now?: () => Date; + /** Says a seat event on its subject under an id the bus keeps once (mesh-sdk runtime.seatPublish): merge-called (§736). */ + say?: Say; } /** The forge's API a merge call uses. */ @@ -136,9 +138,11 @@ export async function mergeCall(forge: CallForge, args: Record, } } const now = deps.now ?? (() => new Date()); - const at = now().toISOString(); - // The call as kept (§766): a call refused at judging merged nothing and has nothing to say. - const keep = (said: boolean) => deps.calls?.put(call, { by, account, at, members: members.map(keptMember), said }, now()); + // The call as kept (§766), its `at` the time of its write, then of its last merge (the event's `at`, §736). + let at = now().toISOString(); + const value = (said: boolean): Call => ({ by, account, at, members: members.map(keptMember), said }); + const keep = async (said: boolean) => { await deps.calls?.put(call, value(said), now()); }; + // A call refused at judging merged nothing and has nothing to say. if (refused) { await keep(true); return { call, members: members.map(outcomeOf) }; @@ -150,6 +154,7 @@ export async function mergeCall(forge: CallForge, args: Record, try { m.commit = await mergeMember(forge, m); m.state = "merged"; + at = now().toISOString(); await keep(false); } catch (err) { m.state = "refused"; @@ -162,5 +167,14 @@ export async function mergeCall(forge: CallForge, args: Record, break; } } + // Once its members are merged or refused, the call says merge-called, then is kept said (§736, §766). + if (deps.say) { + try { + await sayCall({ ...value(false), call }, deps.say); + await keep(true); + } catch (err) { + console.error(`[gitea] the merge call ${call} landed and its merge-called is not said yet; it is said at the next start — ${err instanceof Error ? err.message : err}`); + } + } return { call, members: members.map(outcomeOf) }; } diff --git a/modules/gitea/seat.ts b/modules/gitea/seat.ts index 969bfff0f..baa7e5bb1 100644 --- a/modules/gitea/seat.ts +++ b/modules/gitea/seat.ts @@ -27,7 +27,7 @@ import { Refused, checkName } from "./guard.js"; import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js"; import type { Judged } from "./failedsteps.js"; import { mergeCall } from "./mergecall.js"; -import type { Calls } from "./calls.js"; +import type { Calls, Say } from "./calls.js"; /** The verbs of the seat, in its definition's order. */ export const VERBS: readonly string[] = Object.values(GIT_VERBS); @@ -59,6 +59,8 @@ export interface SeatDeps { watched: () => ReadonlyMap>; /** The state calls (calls.ts): each merge call written before its first merge (design 47 H §766). */ calls?: Calls; + /** Says merge-called on the seat (mesh-sdk runtime.seatPublish; design 47 H §736). */ + say?: Say; } /** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */ diff --git a/modules/gitea/test/calls.test.ts b/modules/gitea/test/calls.test.ts index 27a0c8f21..d45afdb06 100644 --- a/modules/gitea/test/calls.test.ts +++ b/modules/gitea/test/calls.test.ts @@ -60,3 +60,45 @@ test("a call past call-retention is deleted on a new call's write, oldest first; assert.deepEqual([...kv.keys()].sort(), ["call.2222222222222222", "call.3333333333333333"]); assert.deepEqual((await calls.all()).map((c) => c.call), ["3333333333333333", "2222222222222222"], "newest first"); }); + +// **The merge call says one event** (design 47 H §736, §766; test 111's stop case): merge-called on merge-called. with +// the landed members, each with its base and merge commit; a call that landed none says none; at the start a call whose +// event is not said (a stop after the second of three merges) says one naming the two that landed, and is kept said. +test("a call says one merge-called of what landed; none for a call that landed none; the start says an unsaid one", async () => { + const { mergeCall } = await import("../dist/mergecall.js"); + const { Calls, sayUnsaid } = await import("../dist/calls.js"); + const { runAs } = await import("@novox/mesh-sdk/callers"); + const said: { subject: string; body: any; id?: string }[] = []; + const say = async (subject: string, body: unknown, id?: string) => { said.push({ subject, body, id }); }; + const { st } = memory(); + const calls = new Calls(st as any); + const pulls: Record = { 5: { head: A, merged: false, state: "open" }, 9: { head: B, merged: false, state: "closed" } }; + const forge = { + getPullRequest: async (_o: string, _r: string, n: number) => ({ number: n, state: pulls[n].state, merged: pulls[n].merged, base: "main", + head_sha: pulls[n].head, ...(pulls[n].merged ? { merge_commit_sha: String(n).repeat(40) } : {}) }), + mergePullRequest: async (_o: string, _r: string, n: number) => { pulls[n].merged = true; }, + commitParents: async () => [], + }; + const deps = (call: string) => ({ registered: () => true, watched: () => new Map([["novox/app", new Set(["main"])]]), + judge: async () => null, newCall: () => call, calls, say, now: () => new Date("2026-11-20T03:00:00Z") }); + const run = (call: string, numbers: number[]) => { + const args = { by: "lead@shanks", pulls: numbers.map((n) => ({ repository: "novox/app", number: n, head: pulls[n].head })) }; + return runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, deps(call) as any)); + }; + await run("00000000000000aa", [5, 9]); // refused at judging: no event + assert.deepEqual(said, []); + await run("00000000000000bb", [5]); + assert.equal(said.length, 1); + assert.deepEqual([said[0].subject, said[0].id], ["mesh.seat.git.event.merge-called.00000000000000bb", "merge-called.00000000000000bb"]); + assert.deepEqual(said[0].body, { call: "00000000000000bb", by: "lead@shanks", account: "shanks.claude-code", at: "2026-11-20T03:00:00.000Z", + members: [{ repository: "novox/app", number: 5, branch: "main", head: A, commit: "5".repeat(40) }] }); + assert.equal((await calls.get("00000000000000bb"))?.said, true); + // A stop after the second of three merges: kept, not said. + const m = (n: number, state: string) => ({ repository: "novox/app", number: n, branch: "main", head: A, state, ...(state === "merged" ? { commit: String(n).repeat(40) } : {}) }); + await calls.put("00000000000000cc", { by: "lead@shanks", account: "x", at: "2026-11-20T03:01:00Z", members: [m(1, "merged"), m(2, "merged"), m(3, "judged")], said: false } as any); + said.length = 0; + assert.deepEqual(await sayUnsaid(calls, say), ["00000000000000cc"]); + assert.deepEqual(said.map((s) => s.body.members.map((x: any) => x.number)), [[1, 2]]); + assert.equal(said[0].body.refused, undefined, "a member not merged after a stop is no refused member"); + assert.deepEqual(await sayUnsaid(calls, say), [], "said once"); +});