diff --git a/modules/build-agent/manifest_test.go b/modules/build-agent/manifest_test.go index 163d2d77..06e22fb1 100644 --- a/modules/build-agent/manifest_test.go +++ b/modules/build-agent/manifest_test.go @@ -115,3 +115,57 @@ func TestTheGenesisDocumentIsDeclaredAndIssuedOutside(t *testing.T) { } t.Fatal("the build agent has no environment file") } + +// **The judge cache is the agent's own, beside its workspace and never in it** (novox/hq ADR 0358 §6, issue 704): a +// directory resource of its own, kept as the agent's data, mounted into the agent at the same path as on the host — +// the container runtime resolves a judge build's mounts on the host — and handed to the agent as MESH_JUDGE_CACHE. +func TestTheJudgeCacheIsADirectoryOfItsOwnMountedAtTheSamePath(t *testing.T) { + raw, err := os.ReadFile("module.json") + if err != nil { + t.Fatal(err) + } + var m struct { + Data struct { + Own []struct { + ID string `json:"id"` + Path string `json:"path"` + Class string `json:"class"` + } `json:"own"` + } `json:"data"` + Resources []struct { + ID string `json:"id"` + Type string `json:"type"` + Content string `json:"content"` + Volumes []string `json:"volumes"` + } `json:"resources"` + } + if err := json.Unmarshal(raw, &m); err != nil { + t.Fatal(err) + } + dir, data, env, mounted := false, false, false, false + for _, d := range m.Data.Own { + if d.ID == "judge-cache" && d.Path == "${dir:judge-cache}" && d.Class == "cache" { + data = true + } + } + for _, r := range m.Resources { + switch { + case r.ID == "judge-cache" && r.Type == "directory": + dir = true + case r.ID == "agent-env": + env = strings.Contains(r.Content, "MESH_JUDGE_CACHE=${dir:judge-cache}\n") + case r.ID == "server": + for _, v := range r.Volumes { + if v == "${dir:judge-cache}:${dir:judge-cache}" { + mounted = true + } + if strings.HasPrefix(v, "${dir:workspace}") && strings.Contains(v, "judge-cache") { + t.Errorf("the judge cache is mounted inside the workspace: %s", v) + } + } + } + } + if !dir || !data || !env || !mounted { + t.Fatalf("the judge cache: directory %v, data %v, env %v, mounted at the same path %v", dir, data, env, mounted) + } +} diff --git a/modules/build-agent/module.json b/modules/build-agent/module.json index 9e2a5b22..7cae885c 100644 --- a/modules/build-agent/module.json +++ b/modules/build-agent/module.json @@ -39,6 +39,12 @@ "path": "${dir:workspace}", "class": "cache", "why": "a build's working copy, cloned again for every build" + }, + { + "id": "judge-cache", + "path": "${dir:judge-cache}", + "class": "cache", + "why": "the Go cache every merge check's judge is built with and the release-branch judges kept per head; mounted into judge builds only, never into a check's containers (novox/hq ADR 0358 §6)" } ] }, @@ -54,12 +60,17 @@ "type": "directory", "mode": "0700" }, + { + "id": "judge-cache", + "type": "directory", + "mode": "0700" + }, { "id": "agent-env", "type": "file", "path": "${dir:mesh-state}/build-agent.env", "mode": "0600", - "content": "MESH_BROKER_FILE=/run/mesh/broker\nMESH_NODE=${machine:name}\nMESH_REGISTRY=${bound:artifact-store:at}:${bound:artifact-store:port}\nMESH_PACKAGE_BINDING=/run/mesh/package-registry.json\nMESH_NPM_TOKEN_FILE=/run/mesh/package-registry.secret\nMESH_GENESIS_PACKAGE_REGISTRY=/run/mesh/genesis-package-registry.json\nMESH_WORKSPACE=${dir:workspace}\n" + "content": "MESH_BROKER_FILE=/run/mesh/broker\nMESH_NODE=${machine:name}\nMESH_REGISTRY=${bound:artifact-store:at}:${bound:artifact-store:port}\nMESH_PACKAGE_BINDING=/run/mesh/package-registry.json\nMESH_NPM_TOKEN_FILE=/run/mesh/package-registry.secret\nMESH_GENESIS_PACKAGE_REGISTRY=/run/mesh/genesis-package-registry.json\nMESH_WORKSPACE=${dir:workspace}\nMESH_JUDGE_CACHE=${dir:judge-cache}\n" }, { "id": "server", @@ -72,6 +83,7 @@ "volumes": [ "${dir:mesh-state}:/run/mesh:ro", "${dir:workspace}:${dir:workspace}", + "${dir:judge-cache}:${dir:judge-cache}", "/var/run/docker.sock:/var/run/docker.sock" ], "restart-on": [ diff --git a/modules/gitea/client.ts b/modules/gitea/client.ts index 4ac40fb9..55bba9dc 100644 --- a/modules/gitea/client.ts +++ b/modules/gitea/client.ts @@ -101,6 +101,92 @@ export interface OpenPull { /** The head's statuses by context, the newest of each, with its state (novox/hq issue 438): what tells a check * that started and never answered — pending — from one that answered. */ checks: Record; + /** The pull request's labels, by name. */ + labels?: string[]; + /** Its ready mark (novox/hq issue 722): the label `mesh/ready` and the newest comment "ready at ", the + * head a reviewer approved. Absent without the label; `head` empty when the label names no head in a comment. */ + ready?: ReadyMark; +} + +/** The label that marks a pull request ready to merge at the head a comment names (novox/hq issue 722). */ +export const READY_LABEL = "mesh/ready"; + +/** A comment that names the head a pull request is marked ready at: "ready at ", on a line of its own. */ +const READY_AT = /^\s*ready at ([0-9a-f]{40}|[0-9a-f]{64})\b/im; + +/** A pull request's ready mark: the head it names, who said it and when, and the label's id, to take it off. */ +export interface ReadyMark { + head: string; + by?: string; + at?: string; + label_id: number; +} + +/** **The accounts whose "ready at" comment counts** (novox/hq issue 722): the trusted setting ready-authors, which + * the mesh writes to the file MESH_GITEA_READY_AUTHORS_FILE names as a line `authors= …`. A file + * that cannot be read leaves the mesh's own forge account alone, said; never an empty list read as everyone. */ +export function readyAuthors(file = process.env.MESH_GITEA_READY_AUTHORS_FILE, own = process.env.MESH_GITEA_ADMIN_USER, + read: (f: string) => string = (f) => readFileSync(f, "utf8")): string[] { + const fallback = own ? [own] : []; + if (!file) return fallback; + try { + const line = read(file).split("\n").find((l) => l.startsWith("authors=")); + const authors = (line ?? "").slice("authors=".length).split(/[\s,]+/).filter(Boolean); + if (authors.length) return authors; + console.error(`[gitea] ${file} names no account that may mark a pull request ready; only ${fallback.join(", ") || "none"} may`); + } catch (err) { + console.error(`[gitea] ${file} cannot be read (${err instanceof Error ? err.message : err}); only ` + + `${fallback.join(", ") || "none"} may mark a pull request ready`); + } + return fallback; +} + +/** One event of an issue's or pull request's timeline, as the forge keeps it, oldest first: a comment, a push to a + * pull request's branch (`pull_push`), a label added (body "1") or taken off (body ""), and the rest. */ +export interface TimelineEvent { + id: number; + type: string; + body: string; + created_at?: string; + user?: string; + label?: { id: number; name: string }; +} + +/** The comments that wrote "ready at" without counting, by id: each said once in the log (novox/hq issue 722). */ +const ignoredSaid = new Set(); + +/** **The ready mark a pull request's timeline makes** (novox/hq issue 722), read in the forge's order — every event is + * one row of one table, numbered as it happened, so the order is exact where times share a second. A "ready at" + * comment counts only when its author is one of the accounts given, and only when it was written after the label + * mesh/ready was last added and after the last push to the pull request's branch: a push or the label taken off + * undoes every mark before it, so a mark older than the latest push never counts, even with the label still on. A + * comment by any other account is ignored, and said once. */ +export function readyMarkOf(labelID: number, events: TimelineEvent[], authors: string[], + say: (line: string) => void = (l) => console.error(l)): ReadyMark { + const listed = new Set(authors.map((a) => a.toLowerCase())); + let labelled = false; + let mark: ReadyMark | undefined; + for (const e of [...events].sort((x, y) => x.id - y.id)) { + if (e.type === "pull_push") { + mark = undefined; + } else if (e.type === "label" && e.label?.name === READY_LABEL) { + labelled = e.body === "1"; + mark = undefined; + } else if (e.type === "comment") { + const m = READY_AT.exec(e.body); + if (!m) continue; + if (!listed.has(String(e.user ?? "").toLowerCase())) { + if (!ignoredSaid.has(e.id)) { + ignoredSaid.add(e.id); + say(`[gitea] a "ready at ${m[1].slice(0, 12)}" comment (${e.id}) by ${e.user ?? "an unknown account"} is ignored: ` + + `only ${authors.join(", ") || "no account"} may mark a pull request ready (novox/hq issue 722)`); + } + continue; + } + if (labelled) mark = { head: m[1].toLowerCase(), by: e.user, at: e.created_at, label_id: labelID }; + } + } + return mark ?? { head: "", label_id: labelID }; } /** One status on a head, the newest of its context: its state, what it said, and when it was set. */ @@ -332,8 +418,9 @@ export class GiteaClient { /** The open pull requests of every repository the mesh's account sees, updated since a moment when one is given, * page by page (novox/hq issue 347): each with its repository and its own update time. */ - async searchOpenPulls(since?: string): Promise<{ owner: string; name: string; number: number; updated_at?: string }[]> { - const out: { owner: string; name: string; number: number; updated_at?: string }[] = []; + async searchOpenPulls(since?: string): Promise<{ owner: string; name: string; number: number; updated_at?: string; + labels?: { id: number; name: string }[] }[]> { + const out: { owner: string; name: string; number: number; updated_at?: string; labels?: { id: number; name: string }[] }[] = []; for (let page = 1; page <= 20; page++) { const qs = new URLSearchParams({ type: "pulls", state: "open", limit: "50", page: String(page) }); if (since) qs.set("since", since); @@ -343,7 +430,8 @@ export class GiteaClient { const [fo, fn] = String(r.full_name ?? "").split("/"); const owner = String(r.owner ?? fo ?? ""); const name = String(r.name ?? fn ?? ""); - if (owner && name && i?.number) out.push({ owner, name, number: Number(i.number), updated_at: i.updated_at }); + const labels = (Array.isArray(i?.labels) ? i.labels : []).map((l: any) => ({ id: Number(l?.id ?? 0), name: String(l?.name ?? "") })); + if (owner && name && i?.number) out.push({ owner, name, number: Number(i.number), updated_at: i.updated_at, labels }); } if (!found || found.length < 50) break; } @@ -354,7 +442,7 @@ export class GiteaClient { * requires and the contexts its head has a status of, in any state. What mesh-delivery reads to say a pull * request the mesh was never asked to check; asked of the forge, never of what the announcer remembers, so a * fault in the announcer cannot hide itself. Reads only. */ - async openPulls(): Promise { + async openPulls(readyAuthors: string[] = []): Promise { const out: OpenPull[] = []; const rules = new Map(); for (const found of await this.searchOpenPulls()) { @@ -364,9 +452,14 @@ export class GiteaClient { if (!rules.has(full)) rules.set(full, await this.branchProtections(found.owner, found.name).catch(() => undefined)); const rule = rules.get(full)?.find((p) => (p.rule_name ?? p.branch_name) === (pull.base || "main")); const checks = await this.headChecks(found.owner, found.name, pull.head_sha); + const labels = found.labels ?? []; + // The ready mark (novox/hq issue 722): its timeline is read, whole, only for a pull request that carries the label. + const label = labels.find((l) => l.name === READY_LABEL); + const ready = label ? readyMarkOf(label.id, await this.timeline(found.owner, found.name, pull.number), readyAuthors) + : undefined; out.push({ owner: found.owner, repo: found.name, number: pull.number, base: pull.base ?? "", head_sha: pull.head_sha, html_url: pull.html_url, required: rule?.enable_status_check ? (rule.status_check_contexts ?? []) : [], - statuses: Object.keys(checks), checks }); + statuses: Object.keys(checks), checks, labels: labels.map((l) => l.name), ...(ready ? { ready } : {}) }); } return out; } @@ -639,6 +732,24 @@ export class GiteaClient { return this.requestText(`${repoPath(owner, repo)}/pulls/${index}.diff`); } + /** **Every event of an issue's or pull request's timeline**, oldest first, page by page (novox/hq issue 722): a long + * thread hides nothing. Bounded at 100 pages of 50; a longer one is said, and its first 5000 events read. */ + async timeline(owner: string, repo: string, index: number): Promise { + indexOf(index); + const out: TimelineEvent[] = []; + for (let page = 1; page <= 100; page++) { + const raw = await this.request(`${repoPath(owner, repo)}/issues/${index}/timeline?page=${page}&limit=50`); + for (const e of raw ?? []) { + out.push({ id: Number(e?.id ?? 0), type: String(e?.type ?? ""), body: String(e?.body ?? ""), + created_at: e?.created_at, user: e?.user?.login, + ...(e?.label ? { label: { id: Number(e.label.id ?? 0), name: String(e.label.name ?? "") } } : {}) }); + } + if (!raw || raw.length < 50) return out; + } + console.error(`[gitea] ${owner}/${repo}#${index} has more than 5000 timeline events; only the first 5000 are read`); + return out; + } + /** Every comment on an issue or pull request, oldest first. */ async listComments(owner: string, repo: string, index: number): Promise { indexOf(index); diff --git a/modules/gitea/module.json b/modules/gitea/module.json index c97134a5..069d7163 100644 --- a/modules/gitea/module.json +++ b/modules/gitea/module.json @@ -109,6 +109,13 @@ } } }, + "settings": { + "ready-authors": { + "kind": "preference", + "default": "mesh-admin jschoubben", + "why": "the forge accounts whose comment \"ready at \" marks a pull request ready for mesh-delivery to merge, separated by spaces: the mesh's own forge account and the operator's (novox/hq issue 722). A comment by any other account is ignored and said in the forge's log. Trusted: who may have a change merged is the operator's word" + } + }, "resources": [ { "id": "mesh-state", @@ -122,6 +129,14 @@ "path": "${dir:mesh-state}/state", "mode": "0700" }, + { + "id": "ready-authors", + "type": "file", + "path": "${dir:mesh-state}/ready-authors", + "mode": "0600", + "trusted": true, + "content": "# The accounts whose \"ready at \" comment marks a pull request ready (novox/hq issue 722), from the setting\n# ready-authors, written by the mesh. Replaced on every push; change the setting, never this file.\nauthors=${setting:ready-authors}\n" + }, { "id": "state", "type": "directory", @@ -207,7 +222,8 @@ "path": "${dir:mesh-state}/config.json", "mode": "0600", "content": "{}\n", - "merge": "json" + "merge": "json", + "takes-settings": [] } ], "provides": [ @@ -246,7 +262,8 @@ "MESH_GITEA_ADMIN_USER": "mesh-admin", "MESH_GITEA_ADMIN_PASSWORD_FILE": "${dir:state}/admin.secret", "MESH_GITEA_STATE_DIR": "${dir:runtime-state}", - "MESH_RECEIVES": "${dir:grants}/npm.json" + "MESH_RECEIVES": "${dir:grants}/npm.json", + "MESH_GITEA_READY_AUTHORS_FILE": "${dir:mesh-state}/ready-authors" } }, { diff --git a/modules/gitea/test/announcing.test.ts b/modules/gitea/test/announcing.test.ts index b24c7fef..01ce7a05 100644 --- a/modules/gitea/test/announcing.test.ts +++ b/modules/gitea/test/announcing.test.ts @@ -137,3 +137,120 @@ test("a merged pull request whose branch was deleted is announced with its branc assert.equal(headBranchOf({ ref: "refs/pull/7/head" }), "refs/pull/7/head"); assert.equal(headBranchOf(undefined), undefined); }); + +// **A pull request marked ready says the head it is marked at** (novox/hq issue 722): the label mesh/ready and the +// newest comment "ready at " by a listed account, written after the label was added and after the last +// push. Read from the pull request's whole timeline, page by page; without the label nothing is read. +const head = "c".repeat(40), older = "d".repeat(40); +const ev = (id: number, type: string, body: string, user = "mesh-admin", label?: { id: number; name: string }) => + ({ id, type, body, user: { login: user }, created_at: `2026-10-14T10:${String(id % 60).padStart(2, "0")}:00Z`, + ...(label ? { label } : {}) }); +const readyLabel = { id: 31, name: "mesh/ready" }; + +function forgeWith(timeline: unknown[]) { + const asked: string[] = []; + const server = createServer((req, res) => { + const url = req.url ?? ""; + asked.push(url); + const send = (v: unknown) => res.end(JSON.stringify(v)); + if (url.startsWith("/api/v1/repos/issues/search")) { + return send(url.includes("page=1") ? [ + { number: 7, repository: { owner: "novox", name: "hq", full_name: "novox/hq" }, + labels: [{ id: 31, name: "mesh/ready" }, { id: 2, name: "bug" }] }, + { number: 8, repository: { owner: "novox", name: "hq", full_name: "novox/hq" }, labels: [] }, + ] : []); + } + if (url.startsWith("/api/v1/repos/novox/hq/pulls/7")) { + return send({ number: 7, state: "open", base: { ref: "main" }, head: { ref: "x", sha: head }, html_url: "h" }); + } + if (url.startsWith("/api/v1/repos/novox/hq/pulls/8")) { + return send({ number: 8, state: "open", base: { ref: "main" }, head: { ref: "y", sha: older }, html_url: "h" }); + } + const t = /\/issues\/7\/timeline\?page=(\d+)&limit=50/.exec(url); + if (t) { + const page = Number(t[1]); + return send(timeline.slice((page - 1) * 50, page * 50)); + } + if (url.includes("/branch_protections")) return send([]); + if (url.includes("/status")) return send({ statuses: [] }); + res.statusCode = 404; + res.end("{}"); + }); + return { server, asked }; +} + +async function readOpen(timeline: unknown[], authors = ["mesh-admin", "jschoubben"]) { + const { GiteaClient } = await import("../client.ts"); + const { server, asked } = forgeWith(timeline); + await new Promise((r) => server.listen(0, r)); + try { + const client = new GiteaClient(`http://127.0.0.1:${(server.address() as any).port}`, "t"); + return { open: await client.openPulls(authors), asked }; + } finally { + server.close(); + } +} + +test("a pull request's ready mark is read: the label and the head the newest listed ready comment names", async () => { + const { open, asked } = await readOpen([ + ev(1, "pull_push", JSON.stringify({ commit_ids: [head] })), + ev(2, "label", "1", "mesh-admin", readyLabel), + ev(3, "comment", `ready at ${older}`), + ev(4, "comment", `Reviewed: approve.\nready at ${head}\n`), + ev(5, "comment", "thanks", "jschoubben"), + ]); + assert.deepEqual(open[0].labels, ["mesh/ready", "bug"]); + assert.deepEqual(open[0].ready, { head, by: "mesh-admin", at: "2026-10-14T10:04:00Z", label_id: 31 }); + assert.equal(open[1].ready, undefined, "no label, no mark"); + assert.ok(!asked.some((u) => u.includes("/issues/8/")), "no timeline read without the label"); +}); + +test("a ready comment by an account not listed is ignored, and said", async () => { + const { readyMarkOf } = await import("../client.ts"); + const said: string[] = []; + const mark = readyMarkOf(31, [ + ev(1, "pull_push", "{}") as any, ev(2, "label", "1", "mesh-admin", readyLabel) as any, + { ...ev(3, "comment", `ready at ${head}`, "outsider"), user: "outsider" }, + ].map((e: any) => ({ ...e, user: typeof e.user === "string" ? e.user : e.user.login })), ["mesh-admin"], (l) => said.push(l)); + assert.equal(mark.head, "", "an outsider's mark counts"); + assert.equal(said.length, 1); + assert.match(said[0], /by outsider is ignored/); +}); + +test("a ready comment older than the latest push, or than the label, never counts", async () => { + const pushedAfter = await readOpen([ + ev(1, "label", "1", "mesh-admin", readyLabel), + ev(2, "comment", `ready at ${head}`), + ev(3, "pull_push", JSON.stringify({ commit_ids: [head] })), + ]); + assert.equal(pushedAfter.open[0].ready?.head, "", "a mark written before the latest push counts, the label still on"); + const beforeLabel = await readOpen([ + ev(1, "pull_push", "{}"), + ev(2, "comment", `ready at ${head}`), + ev(3, "label", "1", "mesh-admin", readyLabel), + ]); + assert.equal(beforeLabel.open[0].ready?.head, "", "a mark written before the label was added counts"); + const takenOff = await readOpen([ + ev(1, "label", "1", "mesh-admin", readyLabel), + ev(2, "comment", `ready at ${head}`), + ev(3, "label", "", "mesh-admin", readyLabel), + ]); + assert.equal(takenOff.open[0].ready?.head, "", "a mark whose label was taken off since counts"); +}); + +test("a long thread hides nothing: every page of the timeline is read", async () => { + const events: unknown[] = [ev(1, "pull_push", "{}"), ev(2, "label", "1", "mesh-admin", readyLabel), + ev(3, "comment", `ready at ${older}`)]; + for (let id = 4; id < 130; id++) events.push(ev(id, "comment", "a long discussion", "jschoubben")); + events.push(ev(130, "comment", `ready at ${head}`, "jschoubben")); + const { open, asked } = await readOpen(events); + assert.equal(open[0].ready?.head, head, "the newest mark, on the third page, is not read"); + assert.equal(asked.filter((u) => u.includes("/issues/7/timeline")).length, 3); +}); + +test("the accounts that may mark are the setting's, and only the mesh's own when it cannot be read", async () => { + const { readyAuthors } = await import("../client.ts"); + assert.deepEqual(readyAuthors("f", "mesh-admin", () => "# x\nauthors=mesh-admin jschoubben\n"), ["mesh-admin", "jschoubben"]); + assert.deepEqual(readyAuthors("f", "mesh-admin", () => { throw new Error("gone"); }), ["mesh-admin"]); + assert.deepEqual(readyAuthors("f", "mesh-admin", () => "authors=\n"), ["mesh-admin"]); +}); diff --git a/modules/gitea/test/trunk.test.ts b/modules/gitea/test/trunk.test.ts index 7e7fdd4b..3eb373a6 100644 --- a/modules/gitea/test/trunk.test.ts +++ b/modules/gitea/test/trunk.test.ts @@ -173,3 +173,29 @@ test("a verdict that landed out of date keeps its account on the pull request", const bare = lateVerdictComment({ commit: HEAD, trunk: TRUNK.slice(0, 12), verdict: "pass" }, null, words); assert.match(bare, /head dfb1415aaaaa merged into trunk a2c2c35bbbbb, pass/); }); + +test("a release branch's move puts every open pull request into it out of date, as a trunk's does (ADR 0358 §5)", async () => { + // novox/hq ADR 0358 §5: for a pull request into a release branch, the release branch takes the trunk's place in ADR + // 0323 — the check merges the head onto the release branch's head and names it as the trunk commit, so the release + // branch's move puts its open verdicts out of date, and leaves those into main alone. + const release = "feat/57-config-and-provisioning"; + const heads: Record = { main: TRUNK, [release]: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" }; + const pulls = [{ number: 315, base: release, head: "3".repeat(40) }, { number: 1, base: "main", head: "1".repeat(40) }]; + const { forge, statuses } = aForge(pulls, heads); + const gates = new GateWriter(forge); + const trunks: Record = {}; + await weighTrunks(forge, gates, trunks, "novox", "mesh-controller"); + + heads[release] = MOVED; + assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-controller"), true); + const s = statuses.get("3".repeat(40))!; + assert.equal(s["mesh/merge-gate"].state, "pending"); + assert.match(s["mesh/merge-gate"].description!, /judged against trunk eeeeeeeeeeee, the trunk is now d28ca95ccccc/); + assert.equal(s["mesh/repo-check"].state, "pending"); + assert.equal(statuses.get("1".repeat(40))!["mesh/merge-gate"].state, "success", "a pull request into main is left"); + assert.equal(trunks[`novox/mesh-controller:${release}`], MOVED); + + // The merge tool's rule reads the same: a verdict judged against the release branch's old head is not current. + assert.equal(verdictAtTrunk({ "mesh/merge-gate": { state: "success", + description: "head x merged into trunk eeeeeeeeeeee — pass" } }, MOVED).current, false); +}); diff --git a/modules/gitea/tools/index.ts b/modules/gitea/tools/index.ts index 201b08d0..b8c46151 100644 --- a/modules/gitea/tools/index.ts +++ b/modules/gitea/tools/index.ts @@ -10,7 +10,7 @@ import { registerModuleTools, type ToolDefinition } from "@novox/mesh-sdk/tools"; import { emit } from "@novox/mesh-sdk/events"; -import { GiteaClient, type BranchProtection } from "../client.js"; +import { GiteaClient, readyAuthors, type BranchProtection } from "../client.js"; import { appendNote, run, viewBody, viewComment } from "../delivery.js"; import { containedFrom, isCommit, mergeableUnder, newestByContext } from "../checks.js"; import { @@ -514,9 +514,9 @@ function allGiteaTools(gitea: GiteaClient): ToolDefinition[] { }, { name: "gitea_open_pulls", - description: "Every open pull request of every repository, from the forge itself: its head, its base, the statuses the base's protection requires and the ones its head has, each with its state, description and when it was set. What mesh-delivery reads to say a pull request the mesh was never asked to check (novox/hq issue 347), or whose check started and never answered (issue 438). Reads only.", + description: "Every open pull request of every repository, from the forge itself: its head, its base, the statuses the base's protection requires and the ones its head has, each with its state, description and when it was set. What mesh-delivery reads to say a pull request the mesh was never asked to check (novox/hq issue 347), or whose check started and never answered (issue 438), and each pull request's labels and ready mark — the label mesh/ready and the head named by the newest comment \"ready at \" that a listed account (the setting ready-authors) wrote after the label was added and after the last push — which mesh-delivery merges on (issue 722). Reads only.", input: {}, - run: async () => ({ pulls: await gitea.openPulls() }), + run: async () => ({ pulls: await gitea.openPulls(readyAuthors()) }), }, { name: "gitea_contains", diff --git a/modules/mesh-catalog/heard.ts b/modules/mesh-catalog/heard.ts new file mode 100644 index 00000000..4dab234f --- /dev/null +++ b/modules/mesh-catalog/heard.ts @@ -0,0 +1,96 @@ +// What the catalogue makes of a build outcome it hears (novox/hq issue 712). +// +// Kept apart from index.ts, which subscribes at its top level, so the decision can be tested without a bus. + +import type { Made } from "./store.js"; + +/** What the build seat says when it has built something, and what the control plane replays. */ +export interface Built { + module?: string; + commit?: string; + repository?: string; + path?: string; + ref?: string; + manifest?: unknown; + /** What this build published, each pinned as anything else would name it. */ + made?: Made[]; + /** + * Every artifact this was built on top of, so the edge is derived rather than declared. + * + * References, not module names: the builder sees a pinned image and cannot see which module + * produced it. Turning that into an edge between module-versions is this module's job. + */ + against?: string[]; + /** + * This is history, not news — the mesh re-announcing a build this catalogue was not there for. + * + * Registered exactly as any other, and announced as nothing. A module that moved months ago is + * not something anything should act on now: emitting `upgraded` would have the control plane + * decide about a rollout, and `rebuild-needed` would ask for builds of things that are already + * current. + */ + replay?: boolean; + /** Why the build failed; a failed build made no module-version. */ + failed?: string; + /** A dry run is looked at, never taken in (novox/hq issue 240). */ + "dry-run"?: boolean; + /** A pull request's merge check or a branch tried before one (to-be 45 §9, issue 686): nothing merged. */ + "check-outcome"?: unknown; + check?: unknown; + /** The repository's default branch as the build seat read the forge at the build (ADR 0238); empty from a seat older than the rule. */ + trunk?: string; + /** Whether the commit is on `trunk`. */ + "on-trunk"?: boolean; + /** The branches the commit is on. */ + branches?: string[]; +} + +/** The events a build outcome reaches this module on: the build seat's, as it happens, and the control plane's replay. */ +export const BUILD_EVENTS = [ + // **The build seat that builds now** (novox/hq ADR 0190). Until issue 712 the catalogue heard only the + // retired seat below, so every build after the handover reached the graph only through the replay this + // module asks for when it starts: a merge built and pushed stayed out of the catalogue until its next start. + "node-build-agent.built", + // The retired build role, while a machine still serves it (ADR 0190's handover). Goes with the seat row. + "mesh-build-machine.built", + // What the mesh already held, when this module asks what it missed (novox/hq ADR 0134). + "mesh-controller.built-before", +] as const; + +/** + * Whether a build outcome is a module-version to place in the graph, and why not when it is not. + * + * It mirrors two of the rules the controller keeps before it registers a build (mesh-controller + * cmd/mesh-controller/main.go `builds.Built`, build.go `publishable`): a merge check, a try and a dry run are + * never published, and **only a commit on its repository's trunk is** (ADR 0238) — a branch built through + * the `build` verb is checked, never a module's version. Where the seat names no trunk (one older than the + * rule) it is placed, as the controller lets it through. A failure made nothing and is passed over too. + * + * Not mirrored, because the catalogue cannot see what they read: the branch a module follows when it is not + * the default one (the controller's `follows`; such a module's builds off the default branch are passed over + * here and reach the graph by the replay), ADR 0266's rule that a module registers only from the repository + * it is built from, a superseded request, a build in the scratch namespace, one that failed its gate, and a + * build whose source is unchanged. Each needs the controller's records. This is safe for what the mesh + * sends, which is chosen from the controller's own module table and never from this graph; that the graph + * can still hold a build the controller refused under those rules is a follow-up (hq issue 712). + */ +export function placeable(body: Built): + | { placed: "yes"; module: string; commit: string } + | { placed: "no"; why: string; fault: boolean } { + if (body["check-outcome"] != null || body.check != null) { + return { placed: "no", why: "a merge check or a try: nothing of it merged", fault: false }; + } + if (body["dry-run"]) { + return { placed: "no", why: "a dry run is looked at, never taken in", fault: false }; + } + if (body.failed) { + return { placed: "no", why: "a failed build made no module-version", fault: false }; + } + if (body.trunk && !body["on-trunk"] && !(body.branches ?? []).includes(body.trunk)) { + return { placed: "no", why: `the commit is not on its repository's trunk ${body.trunk}: checked, never published (ADR 0238)`, fault: false }; + } + if (!body.module || !body.commit) { + return { placed: "no", why: "it named no module or no commit", fault: true }; + } + return { placed: "yes", module: body.module, commit: body.commit }; +} diff --git a/modules/mesh-catalog/index.ts b/modules/mesh-catalog/index.ts index 74f218e8..13a98d2d 100644 --- a/modules/mesh-catalog/index.ts +++ b/modules/mesh-catalog/index.ts @@ -10,7 +10,8 @@ // diamond need no special handling, and nothing holds a plan. import { on, emit } from "@novox/mesh-sdk/events"; -import { Graph, type Made } from "./store.js"; +import { Graph } from "./store.js"; +import { BUILD_EVENTS, placeable, type Built } from "./heard.js"; const graph = Graph.fromEnv(); @@ -21,54 +22,29 @@ const graph = Graph.fromEnv(); // would block the very apply that brings the overlay up — stopped being true when a step's failure // became this module's business and not the machine's (ADR 0136). -/** What the builder says when it has built something. */ -interface Built { - module?: string; - commit?: string; - repository?: string; - path?: string; - ref?: string; - manifest?: unknown; - /** What this build published, each pinned as anything else would name it. */ - made?: Made[]; - /** - * Every artifact this was built on top of, so the edge is derived rather than declared. - * - * References, not module names: the builder sees a pinned image and cannot see which module - * produced it. Turning that into an edge between module-versions is this module's job. - */ - against?: string[]; - /** - * This is history, not news — the mesh re-announcing a build this catalogue was not there for. - * - * Registered exactly as any other, and announced as nothing. A module that moved months ago is - * not something anything should act on now: emitting `upgraded` would have the control plane - * decide about a rollout, and `rebuild-needed` would ask for builds of things that are already - * current. - */ - replay?: boolean; -} - /** * What a build means for the graph, wherever it came from. * * Two emitters say the same thing and neither is a mistake: the build machine says it as it happens, * and the control plane says what it already held when this module asks what it missed * (novox/hq ADR 0134). A replay is marked as one in its body, so nothing acts on a module that moved - * months ago — see `replay` above. + * months ago — see `replay` in heard.ts. */ const placeTheBuild = async (event: { body: unknown }): Promise => { const body = (event.body ?? {}) as Built; - if (!body.module || !body.commit) { - // Said rather than dropped: a build that announced itself without saying what it built is a - // fault in the builder, and a silent discard here would make it look like a missing event. - console.error("mesh-catalog: a build event named no module or no commit; ignored", body); + const placing = placeable(body); + if (placing.placed === "no") { + // A check, a try, a dry run and a failure are the build seat's ordinary outcomes: passed over. One + // that names no module or commit is a fault in the builder, said rather than dropped, so it does + // not look like a missing event. + if (placing.fault) console.error(`mesh-catalog: a build event was not placed: ${placing.why}`, body); return; } + const { module, commit } = placing; const { upgraded, previous } = await graph.register({ - module: body.module, - commit: body.commit, + module, + commit, repository: body.repository ?? "", path: body.path ?? "", ref: body.ref ?? "", @@ -80,7 +56,7 @@ const placeTheBuild = async (event: { body: unknown }): Promise => { if (body.replay) return; await emit("registered", { - module: body.module, commit: body.commit, upgraded, + module, commit, upgraded, }); // **A rebuild that changed nothing is not an upgrade.** Announcing it would ripple outward @@ -88,7 +64,7 @@ const placeTheBuild = async (event: { body: unknown }): Promise => { if (!upgraded) return; await emit("upgraded", { - module: body.module, commit: body.commit, previous, + module, commit, previous, }); // What can be built now — stale, and waiting on nothing that is itself stale. @@ -101,9 +77,9 @@ const placeTheBuild = async (event: { body: unknown }): Promise => { } }; -// As it happens, and what the mesh already held when this module asked what it missed. -await on("mesh-build-machine.built", placeTheBuild); -await on("mesh-controller.built-before", placeTheBuild); +// As it happens — from the build seat that builds now, and the retired one while it still answers — and +// what the mesh already held when this module asked what it missed (novox/hq issue 712). +for (const heard of BUILD_EVENTS) await on(heard, placeTheBuild); // **And ask for what was built before this catalogue existed** (novox/hq 04-ISSUES/050). // diff --git a/modules/mesh-catalog/module.json b/modules/mesh-catalog/module.json index b5f69958..75f0a6d0 100644 --- a/modules/mesh-catalog/module.json +++ b/modules/mesh-catalog/module.json @@ -26,6 +26,7 @@ "postgres-database": "${dir:state}/database.secret" }, "consumes": [ + "node-build-agent.built", "mesh-build-machine.built", "mesh-controller.built-before" ], diff --git a/modules/mesh-catalog/test/heard.test.ts b/modules/mesh-catalog/test/heard.test.ts new file mode 100644 index 00000000..e00daa1b --- /dev/null +++ b/modules/mesh-catalog/test/heard.test.ts @@ -0,0 +1,72 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; + +import { BUILD_EVENTS, placeable } from "../heard.js"; + +// A walk's build as the build seat says it (mesh-controller's link.BuildResult), here mailu's of novox/hq issue 712. +const walkBuild = { + id: "build-1791997333800041716", + repository: "http://novox.internal:20000/novox/mesh-catalog.git", + path: "modules/mailu", + ref: "main", + on: "shanks", + trunk: "main", + "on-trunk": true, + branches: ["main"], + module: "mailu", + commit: "e1c8965ddbf1cf9af9cf1eed7a72ae9ffa6d5a87", + manifest: { module: "mailu", unhealthy: { severity: "urgent", why: "mail stops" } }, + made: [{ name: "code", kind: "bundle", reference: "artifact-store://mailu/code/blobs/sha256:d041" }], +}; + +test("the catalogue hears the build seat that builds now, not only the retired one (issue 712)", async () => { + assert.ok(BUILD_EVENTS.includes("node-build-agent.built")); + assert.ok(BUILD_EVENTS.includes("mesh-controller.built-before")); + // Subscribing is not enough: the mesh grants and routes only what the manifest consumes. + const manifest = JSON.parse(await readFile(new URL("../module.json", import.meta.url), "utf8")); + for (const heard of BUILD_EVENTS) assert.ok(manifest.consumes.includes(heard), `${heard} is not consumed`); +}); + +test("a walk's build is placed, under its module and commit", () => { + assert.deepEqual(placeable(walkBuild), { placed: "yes", module: "mailu", commit: walkBuild.commit }); + // A walk that took the check's build of the same key says so, and is placed all the same (ADR 0350 §2). + assert.equal(placeable({ ...walkBuild, reused: "the check's build" } as never).placed, "yes"); +}); + +test("a merge check, a try, a dry run and a failure are passed over without a fault", () => { + for (const [body, why] of [ + [{ ...walkBuild, "check-outcome": { verdict: "passed" } }, "merge check"], + [{ ...walkBuild, check: { try: true } }, "merge check or a try"], + [{ ...walkBuild, "dry-run": true }, "dry run"], + [{ ...walkBuild, failed: "exit status 1" }, "failed"], + ] as const) { + const placing = placeable(body); + assert.equal(placing.placed, "no"); + if (placing.placed === "no") { + assert.match(placing.why, new RegExp(why)); + assert.equal(placing.fault, false); + } + } +}); + +test("an outcome that names no module or commit is said as the builder's fault", () => { + const placing = placeable({ repository: walkBuild.repository, commit: walkBuild.commit }); + assert.equal(placing.placed, "no"); + if (placing.placed === "no") assert.equal(placing.fault, true); +}); + +test("a branch built through the build verb is passed over; a commit on the trunk, or from a seat that names none, is placed (ADR 0238)", () => { + const branch = { ...walkBuild, ref: "feature", "on-trunk": false, branches: ["feature"] }; + const placing = placeable(branch); + assert.equal(placing.placed, "no"); + if (placing.placed === "no") { + assert.match(placing.why, /not on its repository's trunk main/); + assert.equal(placing.fault, false); + } + // On the trunk by its branches although the seat did not set on-trunk. + assert.equal(placeable({ ...branch, branches: ["feature", "main"] }).placed, "yes"); + // A seat older than the rule says no trunk: placed, as the controller lets it through. + const { trunk, "on-trunk": _on, branches, ...older } = walkBuild; + assert.equal(placeable(older).placed, "yes"); +}); diff --git a/modules/mesh-catalog/tsconfig.json b/modules/mesh-catalog/tsconfig.json index 4e95d4fa..c1eaae10 100644 --- a/modules/mesh-catalog/tsconfig.json +++ b/modules/mesh-catalog/tsconfig.json @@ -11,6 +11,7 @@ "include": [ "pg.d.ts", "store.ts", + "heard.ts", "index.ts", "tools/index.ts", "prepare/index.ts" diff --git a/modules/mesh-delivery/cmd/mesh-delivery/fakes_test.go b/modules/mesh-delivery/cmd/mesh-delivery/fakes_test.go index a9f0768a..d6572f79 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/fakes_test.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/fakes_test.go @@ -396,6 +396,9 @@ type fakeForge struct { // open is what OpenPulls answers; openDown a forge that cannot say. open []OpenPull openDown bool + // merges, comments and unlabels are what was asked of the forge for ready marks (novox/hq issue 722). + merges, comments, unlabels []string + refuse error } func (f *fakeForge) OpenPulls() ([]OpenPull, error) { @@ -649,3 +652,25 @@ func aWalk(id, repo, commit string, waits bool) Walk { } return w } + +// Merge, Comment and Unlabel record what was asked (novox/hq issue 722); Merge answers refuse when set. +func (f *fakeForge) Merge(owner, repo string, number int, head string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.merges = append(f.merges, fmt.Sprintf("%s/%s#%d@%s", owner, repo, number, head)) + return f.refuse +} + +func (f *fakeForge) Comment(owner, repo string, number int, body string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.comments = append(f.comments, fmt.Sprintf("%s/%s#%d: %s", owner, repo, number, body)) + return nil +} + +func (f *fakeForge) Unlabel(owner, repo string, number, label int) error { + f.mu.Lock() + defer f.mu.Unlock() + f.unlabels = append(f.unlabels, fmt.Sprintf("%s/%s#%d-%d", owner, repo, number, label)) + return nil +} diff --git a/modules/mesh-delivery/cmd/mesh-delivery/holder.go b/modules/mesh-delivery/cmd/mesh-delivery/holder.go index 45dcaae3..da430dc2 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/holder.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/holder.go @@ -69,6 +69,8 @@ type Holder struct { leaveRefused map[string]string // tried counts the owed effects Flush tried since this owner started, by how each ended (novox/hq issue 407). tried owedTries + // marks are the open pull requests read with a ready mark, by owner/repo#number (novox/hq issue 722, ready.go). + marks map[string]*marked } // owedTries is how many owed effects were tried since this owner started, and when the last was: three numbers @@ -120,6 +122,7 @@ func (h *Holder) init() { h.askTried = map[string]time.Time{} h.walkAsked = map[string]time.Time{} h.unchecked = map[string]unchecked{} + h.marks = map[string]*marked{} h.walks = map[string]seenWalk{} h.orphans = map[string]time.Time{} h.asking = map[string]bool{} @@ -996,6 +999,12 @@ func (h *Holder) Tick() { for _, ask := range h.leaveStopped() { ask() } + h.mu.Lock() + asks := h.readyAsks(h.Now()) + h.mu.Unlock() + for _, ask := range asks { + ask() + } } // plan works out, under the lock, what to ask; each ask applies its own answer. diff --git a/modules/mesh-delivery/cmd/mesh-delivery/main.go b/modules/mesh-delivery/cmd/mesh-delivery/main.go index be753778..cd24e09b 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/main.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/main.go @@ -76,8 +76,9 @@ func run(h *Holder, l *listening) { logf("[mesh-delivery] the controller's walks cannot be read: %v", err) } } - // Every open pull request, from the forge itself, every five minutes (novox/hq issue 347). - if now.Sub(looked) >= 5*time.Minute { + // Every open pull request, from the forge itself, every minute (novox/hq issues 347, 722): what says a + // head nobody checks, and the ready marks a merge is asked on. + if now.Sub(looked) >= time.Minute { looked = now h.SeeOpenPulls() } diff --git a/modules/mesh-delivery/cmd/mesh-delivery/manifest_test.go b/modules/mesh-delivery/cmd/mesh-delivery/manifest_test.go index 6b4800f7..ade98c14 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/manifest_test.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/manifest_test.go @@ -65,7 +65,9 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) { want = append(want, "seat:"+ControllerSeat+"."+v) } want = append(want, "gitea.gitea_note_append", "gitea.gitea_delivery_view", "gitea.gitea_commit_status", - "gitea.gitea_commit_statuses", "gitea.gitea_contains", "gitea.gitea_open_pulls") + "gitea.gitea_commit_statuses", "gitea.gitea_contains", "gitea.gitea_open_pulls", + // The merge on a ready mark (novox/hq issue 722): the merge tool, a comment, a label taken off. + "gitea.gitea_merge_pull_request", "gitea.gitea_add_comment", "gitea.gitea_api") if !reflect.DeepEqual(m.Invokes, want) { t.Fatalf("invokes %v, wanted %v", m.Invokes, want) } diff --git a/modules/mesh-delivery/cmd/mesh-delivery/model.go b/modules/mesh-delivery/cmd/mesh-delivery/model.go index 1f25263b..ec3e2e1d 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/model.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/model.go @@ -110,6 +110,9 @@ type Delivery struct { LetGo time.Time `json:"let_go"` LetGoWhy string `json:"let_go_why,omitempty"` + // Ready is what this owner did with its pull request's ready mark (novox/hq issue 722, ready.go). + Ready *ReadyRun `json:"ready,omitempty"` + Transitions []Transition `json:"transitions,omitempty"` // Owed are the forge's and the bus's effects of its transitions not yet done: said only after the // delivery is kept, retried until done. diff --git a/modules/mesh-delivery/cmd/mesh-delivery/ports.go b/modules/mesh-delivery/cmd/mesh-delivery/ports.go index a4727928..63fb608f 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/ports.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/ports.go @@ -91,6 +91,13 @@ type Forge interface { // OpenPulls is every open pull request, with what its base requires and what its head has (novox/hq issue // 347). Reads only. OpenPulls() ([]OpenPull, error) + // Merge asks the forge's merge tool to merge a pull request at the head named (gitea_merge_pull_request, novox/hq + // issue 722): the one path every merge takes. Its refusal is the error, in its words. + Merge(owner, repo string, number int, head string) error + // Comment adds a comment to a pull request. + Comment(owner, repo string, number int, body string) error + // Unlabel takes a label off a pull request, by the label's id. + Unlabel(owner, repo string, number, label int) error } // ForgeChecks is what the forge says of a commit's checks (the gitea module's gitea_commit_statuses). @@ -392,6 +399,19 @@ func (f toolForge) Statuses(owner, repo string, number int, commit, base string) return &c, nil } +func (f toolForge) Merge(owner, repo string, number int, head string) error { + return f.tool("gitea_merge_pull_request", map[string]any{"owner": owner, "repo": repo, "number": number, "head": head}) +} + +func (f toolForge) Comment(owner, repo string, number int, body string) error { + return f.tool("gitea_add_comment", map[string]any{"owner": owner, "repo": repo, "number": number, "body": body}) +} + +func (f toolForge) Unlabel(owner, repo string, number, label int) error { + return f.tool("gitea_api", map[string]any{"method": "DELETE", + "path": fmt.Sprintf("/repos/%s/%s/issues/%d/labels/%d", owner, repo, number, label)}) +} + func (f toolForge) OpenPulls() ([]OpenPull, error) { raw, err := f.ask("gitea.gitea_open_pulls", map[string]any{}) if err != nil { diff --git a/modules/mesh-delivery/cmd/mesh-delivery/ready.go b/modules/mesh-delivery/cmd/mesh-delivery/ready.go new file mode 100644 index 00000000..f5488f25 --- /dev/null +++ b/modules/mesh-delivery/cmd/mesh-delivery/ready.go @@ -0,0 +1,323 @@ +package main + +import ( + "fmt" + "strings" + "time" +) + +// Merge on ready (novox/hq issue 722). +// +// The operator, 2026-10-14: "the reviewer task should merge the request or mark the MR as ready to merge in any case". +// Before, a reviewed pull request with green checks waited until a session called the forge's merge tool, and again +// after every check the tool asked because the trunk had moved. Now the independent reviewer marks the pull request +// ready at the head it approved — the forge label `mesh/ready` and a comment "ready at " (approving +// through the forge is the operator's) — and this owner merges it, through the forge's own merge tool +// (gitea_merge_pull_request), the one path every merge takes: +// +// - only at the head the mark names, and only while that is the pull request's head: a push after the mark clears +// it — the label taken off, and said on the pull request — and the new head needs a mark of its own; +// - only when its delivery is ready here: its check passed against its trunk's head. A verdict out of date is judged +// again by the merge tool, which asks the check against the trunk's head itself (novox/hq ADR 0323 §5); the merge +// is asked again once a minute, and lands on the passing verdict; +// - a group member only at its turn in the group's order, and only once the group's composed check passed for its +// members' heads as they are; +// - a check that failed merges nothing: it is said once on the pull request, and as a stalled line, which the +// controller raises as the condition `delivery..stalled`. So is a mark the merge tool refused for an hour. +// +// What this owner does with a mark is kept on the delivery (Ready), said in its log, and the merge's transitions name +// it as their author. + +// ReadyMark is a pull request's ready mark as the forge reads it (the gitea module's gitea_open_pulls): the head the +// newest "ready at" comment names, who wrote it and when, and the label's id. Head is empty when the label is on and +// no comment names a head. +type ReadyMark struct { + Head string `json:"head"` + By string `json:"by,omitempty"` + At string `json:"at,omitempty"` + LabelID int `json:"label_id"` +} + +// ReadyLabel is the label that marks a pull request ready. +const ReadyLabel = "mesh/ready" + +// readyTryEvery is how often a marked head's merge is asked while the merge tool refuses it: a check it asked takes +// minutes, and a refusal costs the forge a few reads. +const readyTryEvery = time.Minute + +// readyRefusedFor is how long the merge tool may refuse a marked head before it is said as stalled: longer than a +// check takes (the controller's checkTimeout is 45 minutes), so a check on its way is never said. +const readyRefusedFor = time.Hour + +// ReadyRun is what this owner did with a delivery's ready mark: kept on the delivery and shown whole by `show`. +type ReadyRun struct { + // Head, By and MarkedAt are the mark: the head it names, who marked it, when. + Head string `json:"head"` + By string `json:"by,omitempty"` + MarkedAt string `json:"marked_at,omitempty"` + // Seen is when this owner first read the mark. + Seen time.Time `json:"seen"` + // Waits is what the merge waits on, when it waits: a verdict, the group's turn or its composed check. + Waits string `json:"waits,omitempty"` + // Tries is how many times the merge was asked; LastTry when, and Answer what the merge tool last answered. + Tries int `json:"tries,omitempty"` + LastTry time.Time `json:"last_try"` + Answer string `json:"answer,omitempty"` + // RefusedSince is when the merge tool began refusing it without a break. + RefusedSince *time.Time `json:"refused_since,omitempty"` + // Merged is when the merge tool merged it. + Merged *time.Time `json:"merged,omitempty"` + // Failed is why it is not merged and will not be on this head: its check failed. Said once on the pull request. + Failed string `json:"failed,omitempty"` +} + +// mergedBy is who a merge this owner made on a ready mark is said to be by, in its transitions. +func (r *ReadyRun) mergedBy() string { + if r == nil || r.Merged == nil { + return "" + } + by := byOwner + ", on the ready mark at " + shortOf(r.Head, 12) + if r.By != "" { + by += " by " + r.By + } + return by +} + +// marked is a pull request read with a ready mark. +type marked struct { + pull OpenPull + // cleared is when the mark was cleared, the label taken off: not cleared again within readyTryEvery. + cleared time.Time +} + +// pullKey is a pull request as a key: owner/repo#number. +func pullKey(p OpenPull) string { + return strings.ToLower(p.Owner+"/"+p.Repo) + fmt.Sprintf("#%d", p.Number) +} + +// seeMarks keeps the open pull requests that carry a ready mark; under the lock. A pull request no longer open or +// no longer marked drops out. +func (h *Holder) seeMarks(open []OpenPull) { + seen := map[string]*marked{} + for _, p := range open { + if p.Ready == nil || p.HeadSHA == "" { + continue + } + k := pullKey(p) + m := &marked{pull: p} + if was := h.marks[k]; was != nil && was.pull.Ready != nil && was.pull.Ready.Head == p.Ready.Head && + was.pull.HeadSHA == p.HeadSHA { + m.cleared = was.cleared + } + seen[k] = m + } + h.marks = seen +} + +// sameFull is whether a mark names a head: the whole sha, or — read from an older forge module — at least twelve +// characters of it. +func sameFull(mark, head string) bool { + mark, head = strings.ToLower(mark), strings.ToLower(head) + return len(mark) >= 12 && strings.HasPrefix(head, mark) +} + +// readyAsks works out, under the lock, what each ready mark needs: a mark cleared, a merge asked, a failure said. +// The asking happens outside the lock. +func (h *Holder) readyAsks(now time.Time) []func() { + var asks []func() + for k, m := range h.marks { + p, mark := m.pull, *m.pull.Ready + repository := strings.ToLower(p.Owner + "/" + p.Repo) + if !sameFull(mark.Head, p.HeadSHA) { + if !m.cleared.IsZero() && now.Sub(m.cleared) < readyTryEvery { + continue + } + m.cleared = now + asks = append(asks, h.clearMark(k, p, mark)) + continue + } + d := h.deliveries[IDOf(repository, p.HeadSHA)] + if d == nil || d.State.Final() || d.MergedAs != "" { + continue // not announced yet, or past its merge + } + if d.Ready == nil || d.Ready.Head != mark.Head { + d.Ready = &ReadyRun{Head: mark.Head, By: mark.By, MarkedAt: mark.At, Seen: now} + h.Logf("[mesh-delivery] %s is marked ready at %s by %s: merged once its check passes against its trunk's head", + d.ID, shortOf(mark.Head, 12), orNone(mark.By)) + h.keep(d) + } + r := d.Ready + if r.Merged != nil { + continue + } + if d.State == Rejected { + if r.Failed == "" { + r.Failed = "its check failed" + if d.Check != nil { + r.Failed += ": " + oneLine(d.Check.Summary) + if j := d.Check.Judged(); j != "" { + r.Failed += " (" + j + ")" + } + } + r.Waits = "" + h.Logf("[mesh-delivery] %s is marked ready and not merged: %s", d.ID, r.Failed) + h.keep(d) + asks = append(asks, h.sayOn(d, fmt.Sprintf("mesh-delivery: **not merged.** This pull request is marked "+ + "ready at %s, and %s. A new head, marked ready once reviewed, or a recheck that passes, merges it.", + mark.Head, r.Failed))) + } + continue + } + r.Failed = "" + waits := h.readyWaits(d) + if waits != r.Waits { + r.Waits = waits + if waits != "" { + h.Logf("[mesh-delivery] %s is marked ready and waits: %s", d.ID, waits) + } + h.keep(d) + } + if waits != "" || (!r.LastTry.IsZero() && now.Sub(r.LastTry) < readyTryEvery) { + continue + } + r.Tries++ + r.LastTry = now + h.keep(d) + asks = append(asks, h.askMerge(d.ID, p, mark.Head)) + } + return asks +} + +// readyWaits is what a marked delivery's merge waits on; empty when it is asked now. A ready delivery is asked; so is +// a proposed one whose verdict went out of date and whose check nobody asked again — the merge tool asks it. +func (h *Holder) readyWaits(d *Delivery) string { + switch { + case d.State == Ready: + case d.State == Proposed && d.outOfDate() != "" && d.AskedAgain == nil: + case d.State == Proposed && d.AskedAgain != nil: + return "its check against the trunk's head is asked and has not answered" + default: + return "its check has not passed yet (" + string(d.State) + ")" + } + if d.Group == "" { + return "" + } + return h.groupTurnToMerge(d) +} + +// groupTurnToMerge is why a group member may not merge yet; empty at its turn: the group's composed check passed for +// its members' heads as they are and is not out of date, and every member before it in the group's order merged. +func (h *Holder) groupTurnToMerge(d *Delivery) string { + g := h.groups[d.Group] + if g == nil { + return "" + } + members := h.membersOf(g) + if state, why := GroupState(g, members); state != "ready" && state != "delivering" { + return fmt.Sprintf("its group %s is %s: %s", g.ID, state, why) + } + if c := g.Check; c == nil || (c.Verdict != "pass" && c.Verdict != "warning") || c.OutOfDate != "" || + !sameMembers(c.Commits, commitsOf(members)) { + return "its group " + g.ID + "'s composed check has not passed for its members' heads" + } + if !containsString(g.Order, d.ID) { + return "its group " + g.ID + "'s order is not worked out for it yet" + } + for _, id := range g.Order { + if id == d.ID { + return "" + } + if o := h.deliveries[id]; o != nil && o.MergedAs == "" && !o.State.Final() { + return "its group " + g.ID + "'s order merges " + id + " first" + } + } + return "" +} + +// askMerge asks the forge's merge tool to merge a marked head, and keeps its answer. +func (h *Holder) askMerge(id string, p OpenPull, head string) func() { + return func() { + err := h.Forge.Merge(p.Owner, p.Repo, p.Number, head) + h.mu.Lock() + defer h.mu.Unlock() + d := h.deliveries[id] + if d == nil || d.Ready == nil { + return + } + r, now := d.Ready, h.Now() + if err == nil { + r.Merged, r.Answer, r.RefusedSince = &now, "merged", nil + h.Logf("[mesh-delivery] %s merged on its ready mark at %s, its check passed against its trunk's head", id, + shortOf(head, 12)) + h.keep(d) + return + } + answer := oneLine(err.Error()) + if r.RefusedSince == nil { + r.RefusedSince = &now + } + if answer != r.Answer { + h.Logf("[mesh-delivery] %s is marked ready, and the merge tool did not merge it (asked again in %s): %s", id, + readyTryEvery, answer) + } + r.Answer = answer + h.keep(d) + } +} + +// clearMark takes a mark off a pull request whose head moved after it, and says so on it. +func (h *Holder) clearMark(k string, p OpenPull, mark ReadyMark) func() { + return func() { + err := h.Forge.Unlabel(p.Owner, p.Repo, p.Number, mark.LabelID) + if err == nil { + err = h.Forge.Comment(p.Owner, p.Repo, p.Number, fmt.Sprintf("mesh-delivery: the ready mark at %s is cleared: "+ + "the head is now %s, pushed after it. Mark the new head ready once it is reviewed.", mark.Head, p.HeadSHA)) + } + if err != nil { + h.Logf("[mesh-delivery] %s's ready mark at %s names an older head than %s, and could not be cleared yet: %v", + k, shortOf(mark.Head, 12), shortOf(p.HeadSHA, 12), err) + return + } + h.Logf("[mesh-delivery] %s's ready mark at %s cleared: its head is now %s", k, shortOf(mark.Head, 12), + shortOf(p.HeadSHA, 12)) + h.mu.Lock() + delete(h.marks, k) + h.mu.Unlock() + } +} + +// sayOn comments on a delivery's pull request. +func (h *Holder) sayOn(d *Delivery, body string) func() { + owner, repo, number, id := d.Owner(), d.Repo(), d.Number, d.ID + return func() { + if err := h.Forge.Comment(owner, repo, number, body); err != nil { + h.Logf("[mesh-delivery] %s: could not say on its pull request: %v", id, err) + } + } +} + +// readyLines are the marked heads that will not merge: a check that failed at once, a merge refused past its bound; +// as stalled lines, which the controller raises as conditions. Under the lock. +func (h *Holder) readyLines(now time.Time) []StalledLine { + var out []StalledLine + for _, d := range h.deliveries { + r := d.Ready + if r == nil || r.Merged != nil || d.MergedAs != "" || d.State.Final() { + continue + } + switch { + case r.Failed != "" && d.State == Rejected: + out = append(out, StalledLine{ID: d.ID, Number: d.Number, State: d.State, For: now.Sub(r.Seen).Round(time.Second).String(), + Bound: "0s", H2: "none: a new head marked ready, or a recheck that passes — the operator's", + Says: fmt.Sprintf("%s#%d is marked ready at %s and not merged: %s", d.Repository, d.Number, + shortOf(r.Head, 12), r.Failed)}) + case r.RefusedSince != nil && now.Sub(*r.RefusedSince) >= readyRefusedFor: + out = append(out, StalledLine{ID: d.ID, Number: d.Number, State: d.State, + For: now.Sub(*r.RefusedSince).Round(time.Second).String(), Bound: readyRefusedFor.String(), + H2: "none: the merge is asked again each minute — the operator's", + Says: fmt.Sprintf("%s#%d is marked ready at %s, and the merge tool has refused it since %s: %s", d.Repository, + d.Number, shortOf(r.Head, 12), r.RefusedSince.UTC().Format(time.RFC3339), r.Answer)}) + } + } + return out +} diff --git a/modules/mesh-delivery/cmd/mesh-delivery/ready_test.go b/modules/mesh-delivery/cmd/mesh-delivery/ready_test.go new file mode 100644 index 00000000..e818e9b1 --- /dev/null +++ b/modules/mesh-delivery/cmd/mesh-delivery/ready_test.go @@ -0,0 +1,199 @@ +package main + +import ( + "errors" + "strings" + "testing" + "time" +) + +// **A pull request marked ready is merged by this owner** (novox/hq issue 722): before, a reviewed pull request with +// green checks waited until a session called the merge tool, and again after every check the tool asked because the +// trunk had moved. Each test below fails on a holder without the ready mark: nothing merged it. + +const ( + readyHead = "1111111111111111111111111111111111111111" + pushed = "2222222222222222222222222222222222222222" +) + +// markedPull is an open pull request as gitea_open_pulls reads it, labelled mesh/ready at the head named. +func markedPull(repo string, number int, head, markedAt string) OpenPull { + owner, name, _ := strings.Cut(repo, "/") + return OpenPull{Owner: owner, Repo: name, Number: number, Base: "main", HeadSHA: head, Labels: []string{ReadyLabel}, + Ready: &ReadyMark{Head: markedAt, By: "reviewer", At: "2026-10-14T19:00:00Z", LabelID: 31}} +} + +func judgedAt(repo string, number int, sha, gate, trunk string) CheckedEvent { + c := verdict(repo, number, sha, gate) + c.Trunk = trunk + return c +} + +func TestAReadyPullRequestMarkedAtItsHeadIsMergedThroughTheMergeTool(t *testing.T) { + w := newWorld(t) + id := IDOf("novox/app", readyHead) + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.h.Checked(judgedAt("novox/app", 7, readyHead, "pass", "t1t1t1t1t1t1")) + w.settleAll() + if len(w.forge.merges) != 0 { + t.Fatalf("an unmarked pull request was merged: %v", w.forge.merges) + } + w.forge.open = []OpenPull{markedPull("novox/app", 7, readyHead, readyHead)} + w.h.SeeOpenPulls() + w.settleAll() + if len(w.forge.merges) != 1 || w.forge.merges[0] != "novox/app#7@"+readyHead { + t.Fatalf("a ready, marked pull request was merged %v; wanted once, at the marked head", w.forge.merges) + } + d := w.delivery(id) + if d.Ready == nil || d.Ready.Merged == nil || d.Ready.By != "reviewer" || d.Ready.Tries != 1 { + t.Fatalf("what was done with the mark is not kept: %+v", d.Ready) + } + // Its merge's transition names who merged it. + w.h.PullMerged(merged("novox/app", 7, readyHead, "m7m7m7m7m7m7")) + w.h.WalkMoved(w.ctl.put(aWalk("plan-7", "novox/app", "m7m7m7m7m7m7", true))) + w.settleAll() + d = w.delivery(id) + var last Transition + for _, tr := range d.Transitions { + if tr.Event == EvMerged { + last = tr + } + } + if !strings.Contains(last.By, "ready mark at "+readyHead[:12]+" by reviewer") { + t.Fatalf("the merge's transition does not name the mark: %+v", last) + } + if len(w.forge.merges) != 1 { + t.Fatalf("merged again: %v", w.forge.merges) + } +} + +func TestAPushAfterTheMarkClearsItAndNothingIsMerged(t *testing.T) { + w := newWorld(t) + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.h.PullUpdated(pr("novox/app", 7, pushed, "feat/x", "src/a.go")) + w.h.Checked(judgedAt("novox/app", 7, pushed, "pass", "t1t1t1t1t1t1")) + w.forge.open = []OpenPull{markedPull("novox/app", 7, pushed, readyHead)} + w.h.SeeOpenPulls() + w.settleAll() + if len(w.forge.merges) != 0 { + t.Fatalf("a head pushed after the mark was merged: %v", w.forge.merges) + } + if len(w.forge.unlabels) != 1 || w.forge.unlabels[0] != "novox/app#7-31" { + t.Fatalf("the stale mark's label was not taken off: %v", w.forge.unlabels) + } + if len(w.forge.comments) != 1 || !strings.Contains(w.forge.comments[0], "cleared") || + !strings.Contains(w.forge.comments[0], pushed) { + t.Fatalf("the cleared mark is not said on the pull request: %v", w.forge.comments) + } +} + +func TestAMovedTrunkIsJudgedAgainByTheMergeToolAndMergedOnItsVerdict(t *testing.T) { + w := newWorld(t) + id := IDOf("novox/app", readyHead) + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.h.Checked(judgedAt("novox/app", 7, readyHead, "pass", "t1t1t1t1t1t1")) + // Another merge moves the trunk: its verdict is out of date, and it goes back to proposed. + w.h.PullMerged(merged("novox/app", 9, "9999999999999999", "t2t2t2t2t2t2")) + if w.state(id) != Proposed { + t.Fatalf("out of date, it is %s", w.state(id)) + } + // The merge tool asks the check against the trunk's head and refuses: nothing merged. + w.forge.refuse = errors.New("the forge refused gitea_merge_pull_request: The check of head merged into trunk was asked") + w.forge.open = []OpenPull{markedPull("novox/app", 7, readyHead, readyHead)} + w.h.SeeOpenPulls() + w.settleAll() + if len(w.forge.merges) != 1 { + t.Fatalf("the merge tool was not asked once to judge it again: %v", w.forge.merges) + } + // The tool's ask is the head announced again; it waits for that verdict, asking nothing. + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.later(5 * time.Minute) + w.settleAll() + if len(w.forge.merges) != 1 || !strings.Contains(w.delivery(id).Ready.Waits, "asked") { + t.Fatalf("asked again while its check was on its way: %v (%+v)", w.forge.merges, w.delivery(id).Ready) + } + // It passes against the trunk's head: merged, without anyone asking. + w.forge.refuse = nil + w.h.Checked(judgedAt("novox/app", 7, readyHead, "pass", "t2t2t2t2t2t2")) + w.settleAll() + if w.state(id) != Ready || len(w.forge.merges) != 2 || w.delivery(id).Ready.Merged == nil { + t.Fatalf("on its passing verdict it was not merged: %s %v %+v", w.state(id), w.forge.merges, w.delivery(id).Ready) + } +} + +func TestAMarkedHeadWhoseCheckFailedIsNotMergedAndIsSaid(t *testing.T) { + w := newWorld(t) + id := IDOf("novox/app", readyHead) + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.h.Checked(judgedAt("novox/app", 7, readyHead, "fail", "t1t1t1t1t1t1")) + w.forge.open = []OpenPull{markedPull("novox/app", 7, readyHead, readyHead)} + w.h.SeeOpenPulls() + w.settleAll() + w.later(10 * time.Minute) + w.h.SeeOpenPulls() + w.settleAll() + if len(w.forge.merges) != 0 { + t.Fatalf("a head whose check failed was merged: %v", w.forge.merges) + } + if len(w.forge.comments) != 1 || !strings.Contains(w.forge.comments[0], "not merged") { + t.Fatalf("the failure is not said once on the pull request: %v", w.forge.comments) + } + lines := w.h.Stalled() + if len(lines) != 1 || lines[0].ID != id || lines[0].State != Rejected || !strings.Contains(lines[0].Says, "marked ready") { + t.Fatalf("the failure is not a stalled line, which the controller raises as a condition: %+v", lines) + } +} + +func TestAMarkRefusedForAnHourIsSaid(t *testing.T) { + w := newWorld(t) + w.h.PullUpdated(pr("novox/app", 7, readyHead, "feat/x", "src/a.go")) + w.h.Checked(judgedAt("novox/app", 7, readyHead, "pass", "t1t1t1t1t1t1")) + w.forge.refuse = errors.New("the forge refused gitea_merge_pull_request: mesh/repo-check is pending") + w.forge.open = []OpenPull{markedPull("novox/app", 7, readyHead, readyHead)} + w.h.SeeOpenPulls() + w.settleAll() + w.later(30 * time.Second) + w.settleAll() + if len(w.forge.merges) != 1 { + t.Fatalf("asked again within the minute: %v", w.forge.merges) + } + w.later(readyRefusedFor) + w.settleAll() + if len(w.forge.merges) != 2 { + t.Fatalf("not asked again after a minute: %v", w.forge.merges) + } + if lines := w.h.Stalled(); len(lines) != 1 || !strings.Contains(lines[0].Says, "refused it since") { + t.Fatalf("a mark refused for an hour is not said: %+v", lines) + } +} + +func TestAGroupMergesItsMarkedMembersInOrderOnlyOnceComposed(t *testing.T) { + w := newWorld(t) + const ctlHead, catHead = "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", "d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2" + ctl, cat := IDOf("novox/mesh-controller", ctlHead), IDOf("novox/mesh-catalog", catHead) + w.ctl.order = func(ms []Member) Order { return Order{Order: []string{ctl, cat}} } + w.h.PullUpdated(pr("novox/mesh-controller", 1, ctlHead, "feat/x", "internal/a.go")) + w.h.PullUpdated(pr("novox/mesh-catalog", 2, catHead, "feat/x", "modules/app/module.json")) + w.h.Checked(judgedAt("novox/mesh-controller", 1, ctlHead, "pass", "t1t1t1t1t1t1")) + w.h.Checked(judgedAt("novox/mesh-catalog", 2, catHead, "pass", "t3t3t3t3t3t3")) + w.forge.open = []OpenPull{markedPull("novox/mesh-controller", 1, ctlHead, ctlHead), + markedPull("novox/mesh-catalog", 2, catHead, catHead)} + w.h.SeeOpenPulls() + w.settleAll() + if len(w.forge.merges) != 0 { + t.Fatalf("a member merged before its group's composed check passed: %v", w.forge.merges) + } + w.h.Checked(CheckedEvent{Group: "feat/x", ID: "check-1", Verdict: "pass", Summary: "both compose"}) + w.settleAll() + if len(w.forge.merges) != 1 || w.forge.merges[0] != "novox/mesh-controller#1@"+ctlHead { + t.Fatalf("composed, the first member in the group's order was not merged alone: %v", w.forge.merges) + } + if waits := w.delivery(cat).Ready.Waits; !strings.Contains(waits, "merges "+ctl+" first") { + t.Fatalf("the second member does not say it waits its turn: %q", waits) + } + w.h.PullMerged(merged("novox/mesh-controller", 1, ctlHead, "m1m1m1m1m1m1")) + w.settleAll() + if len(w.forge.merges) != 2 || w.forge.merges[1] != "novox/mesh-catalog#2@"+catHead { + t.Fatalf("after the first member merged, the next was not: %v", w.forge.merges) + } +} diff --git a/modules/mesh-delivery/cmd/mesh-delivery/table.go b/modules/mesh-delivery/cmd/mesh-delivery/table.go index 687f1b2c..9f9a6749 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/table.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/table.go @@ -498,7 +498,12 @@ func Apply(d *Delivery, ev Event, act bool, f Facts) (Transition, error) { if why == "" { why = r.Guard } - t := Transition{At: f.Now, From: d.State, To: r.To, Event: ev, Why: why, By: f.By} + by := f.By + if by == "" && (ev == EvMerged || ev == EvDone) && d.MergedAs != "" { + // A merge this owner made on a ready mark names it as its author (novox/hq issue 722). + by = d.Ready.mergedBy() + } + t := Transition{At: f.Now, From: d.State, To: r.To, Event: ev, Why: why, By: by} d.State, d.Since = r.To, f.Now d.Transitions = append(d.Transitions, t) if len(d.Transitions) > keptTransitions { diff --git a/modules/mesh-delivery/cmd/mesh-delivery/unchecked.go b/modules/mesh-delivery/cmd/mesh-delivery/unchecked.go index 2947b823..14cc5d20 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/unchecked.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/unchecked.go @@ -72,6 +72,9 @@ type OpenPull struct { // than that says none: the head is then read by Statuses alone, as before, and a pending check is not told // from an answered one. Checks map[string]HeadCheck `json:"checks,omitempty"` + // Labels are its labels by name, and Ready its ready mark, when it carries one (novox/hq issue 722). + Labels []string `json:"labels,omitempty"` + Ready *ReadyMark `json:"ready,omitempty"` } // HeadCheck is one status on a head as the forge keeps it: pending, success, failure, error or warning. @@ -201,6 +204,7 @@ func (h *Holder) SeeOpenPulls() { seen[id] = u } h.unchecked = seen + h.seeMarks(open) } // started is whether the head's check started, rather than was never asked: it has any merge check, or a delivery diff --git a/modules/mesh-delivery/cmd/mesh-delivery/verbs.go b/modules/mesh-delivery/cmd/mesh-delivery/verbs.go index bcaa8e57..b95aacd2 100644 --- a/modules/mesh-delivery/cmd/mesh-delivery/verbs.go +++ b/modules/mesh-delivery/cmd/mesh-delivery/verbs.go @@ -249,6 +249,7 @@ func (h *Holder) Stalled() []StalledLine { } out = append(out, h.withChecks(h.uncheckedLines(now))...) out = append(out, h.orphanLines(now)...) + out = append(out, h.readyLines(now)...) sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID }) return out } diff --git a/modules/mesh-delivery/module.json b/modules/mesh-delivery/module.json index 681165fe..4c486145 100644 --- a/modules/mesh-delivery/module.json +++ b/modules/mesh-delivery/module.json @@ -47,7 +47,10 @@ "gitea.gitea_commit_status", "gitea.gitea_commit_statuses", "gitea.gitea_contains", - "gitea.gitea_open_pulls" + "gitea.gitea_open_pulls", + "gitea.gitea_merge_pull_request", + "gitea.gitea_add_comment", + "gitea.gitea_api" ], "state": [ "deliveries", diff --git a/modules/messenger/README.md b/modules/messenger/README.md index 7a70a347..10b86afd 100644 --- a/modules/messenger/README.md +++ b/modules/messenger/README.md @@ -49,8 +49,9 @@ channel's client and no channel's secret. warrant published to the asker alone. A warrant the bus did not take is published again every minute. - **What the operator sees when they answer** (issue 372): first, in reply to the answer where it was given, that it was recorded and whether its asker was told ("You chose Approve: recorded, and claude-code was - told.") — said only once the ask's end is recorded — or, refused, why; then the ask's message edited to how - it ended, as its title ("You chose Approve at 13:30 on Telegram · "), with no answers left on it. + told.") — said only once the ask's end is recorded — or, refused, why; then the ask's message edited with no + answers left on it: its question whole (headline, summary, points, link, Details), and below it how it ended + ("You chose Approve at 13:30 on Telegram.") and who acts on it (issue 714). What the asker then did is not said: the asks protocol carries no result back to the router, so the message says only that the asker was told and acts on it itself. - **An ask never defaults**: at its expiry it ends `expired` and its asker does what it said it would. @@ -138,8 +139,9 @@ and said in the log). Everything else is the message's note (`asks.Message.Note` smaller: who asked and when, with what happens if nobody answers; what each answer does; where else it is answered; and the asker's Details (who proposed it, a fingerprint, how to read it whole) — on a kind that proves who answers and was given the ask's answers, its Details whole (`asks.Ask.WholeDetails`): every value an approval sets, held to -the secret rule alone, as the whole words are. Answered, the message is one line — -"You chose Approve at 16:06 on Telegram · " — and the router's word on the asker is its note. +the secret rule alone, as the whole words are. Answered, the message keeps its headline, body and Details, so the +operator can read back what they answered (issue 714); its note becomes "You chose Approve at 16:06 on Telegram." +with the router's word on the asker. **An ask's values are shown whole where the sender is proven** (hq issue 383). An ask may carry its explanation twice: as it is held to the whole content rule, and whole (`asks.Ask.Whole`), its paths and addresses as they diff --git a/modules/messenger/cmd/messenger/answers_test.go b/modules/messenger/cmd/messenger/answers_test.go index 828629cc..3410abb8 100644 --- a/modules/messenger/cmd/messenger/answers_test.go +++ b/modules/messenger/cmd/messenger/answers_test.go @@ -365,10 +365,12 @@ func TestAnApproveFromTheLinkedAccountIsAWarrantForTheAskerAlone(t *testing.T) { for _, m := range ch.edits { edited = m } - // The question's title is how it ended, read at a glance; then what the router knows of the asker (issue 372). - if len(edited.Actions) != 0 || edited.Title != "You chose Release at 12:00 on Telegram · Delivery of hq held for 36 hours" || - edited.Body != "" || edited.Note != "Told to mesh-delivery, which acts on it itself." { - t.Errorf("%s still offers %q, or does not say how it ended in one line (issue 639): %q / %q / %q", ch.name, labelsOf(edited), edited.Title, edited.Body, edited.Note) + // The question stays whole (issue 714); below it, how it ended and what the router knows of the asker + // (issue 372). + if len(edited.Actions) != 0 || edited.Title != "Delivery of hq held for 36 hours" || + edited.Body != heldAsk(c, "d-1").Explanation || + edited.Note != "You chose Release at 12:00 on Telegram.\nTold to mesh-delivery, which acts on it itself." { + t.Errorf("%s still offers %q, or does not keep the question and say how it ended (issue 714): %q / %q / %q", ch.name, labelsOf(edited), edited.Title, edited.Body, edited.Note) } } // The tap is confirmed where it was made, in reply to it, once the warrant stood — and before the question @@ -624,8 +626,8 @@ func TestAnAskExpiresAndNeverDefaults(t *testing.T) { for _, m := range tg.edits { edited = m } - if len(edited.Actions) != 0 || !strings.HasPrefix(edited.Title, "No longer answerable: nobody answered in time · Delivery of hq") || - edited.Body != "" || edited.Note != "The delivery stays held." { + if len(edited.Actions) != 0 || !strings.HasPrefix(edited.Title, "Delivery of hq") || + edited.Body == "" || edited.Note != "No longer answerable: nobody answered in time.\nThe delivery stays held." { t.Errorf("the copy still offers answers, or does not say how it ended: %+v", edited) } } diff --git a/modules/messenger/cmd/messenger/feedback_test.go b/modules/messenger/cmd/messenger/feedback_test.go index fc7c8859..e3657c77 100644 --- a/modules/messenger/cmd/messenger/feedback_test.go +++ b/modules/messenger/cmd/messenger/feedback_test.go @@ -51,7 +51,7 @@ func TestAnApprovalIsConfirmedWhereItWasGivenAndThenTheQuestionSaysHowItEnded(t } want := []string{ "send You chose Release: recorded, and mesh-delivery was told.", - "edit You chose Release at 12:00 on Telegram · Delivery of hq held for 36 hours", + "edit Delivery of hq held for 36 hours", } if strings.Join(log, "\n") != strings.Join(want, "\n") { t.Fatalf("the operator saw, in order:\n%s\nwant:\n%s", strings.Join(log, "\n"), strings.Join(want, "\n")) @@ -122,7 +122,7 @@ func TestTheConfirmationNeverClaimsWhatDidNotHappen(t *testing.T) { for _, m := range tg.edits { edited = m } - if !strings.HasPrefix(edited.Note, "Told to mesh-delivery as soon as the bus takes it") { + if !strings.Contains(edited.Note, "\nTold to mesh-delivery as soon as the bus takes it") { t.Errorf("the question claims its asker was told: %q", edited.Note) } }) diff --git a/modules/messenger/cmd/messenger/glance_test.go b/modules/messenger/cmd/messenger/glance_test.go index 669f38a3..223caeb9 100644 --- a/modules/messenger/cmd/messenger/glance_test.go +++ b/modules/messenger/cmd/messenger/glance_test.go @@ -9,8 +9,8 @@ import ( ) // novox/hq issue 639: an ask's message reads at a glance on the phone. Its body is the asker's summary and points -// alone; who asked and when, what each answer does, the expiry and the asker's Details are its note; once answered -// it is one line with the headline. +// alone; who asked and when, what each answer does, the expiry and the asker's Details are its note. Once answered +// it keeps the question whole, so the operator can read back what they answered (issue 714). // decisionAsk is the ask for ADR 0343 as the knowledge base composes it now (its glance form). func decisionAsk(c *clock) asks.Ask { @@ -59,7 +59,8 @@ func TestAnAsksMessageIsItsSummaryWithTheRestInItsNote(t *testing.T) { if n := len(strings.Split(m.Text(), "\n")); n > 9 || len(m.Actions) != 2 { t.Errorf("the message is %d lines offering %s:\n%s", n, labelsOf(m), m.Text()) } - // Answered, it is one line with the headline; the body is not said again. + // Answered, it keeps its headline, summary, points and Details; below them, how it ended and who acts on it; + // its buttons are gone (issue 714: the one line issue 639 made left nothing to read back). if out, err := h.Chosen("telegram", choiceOf(ticketFor(m, "Approve"), "42", c.now())); err != nil || out != "chosen" { t.Fatalf("Approve: %q %v", out, err) } @@ -67,8 +68,9 @@ func TestAnAsksMessageIsItsSummaryWithTheRestInItsNote(t *testing.T) { for _, e := range tg.edits { edited = e } - if edited.Title != "You chose Approve at "+asked+" on Telegram · "+a.Headline || edited.Body != "" || - strings.Contains(edited.Note, "What an agent learns") || len(edited.Actions) != 0 { + if edited.Title != a.Headline || edited.Body != a.Explanation+"\n"+want[2]+"\n"+want[3] || + edited.Note != "You chose Approve at "+asked+" on Telegram.\nTold to mesh-knowledge, which acts on it itself." || + len(edited.Actions) != 0 { t.Errorf("the answered copy: %q\n%q\n%q", edited.Title, edited.Body, edited.Note) } } diff --git a/modules/messenger/cmd/messenger/holder.go b/modules/messenger/cmd/messenger/holder.go index 67310e9a..2c237bb4 100644 --- a/modules/messenger/cmd/messenger/holder.go +++ b/modules/messenger/cmd/messenger/holder.go @@ -856,7 +856,7 @@ func (h *Holder) sayOn(r *Record, what, kind string) Message { } } if r.Answered != "" { - m = h.answeredWords(r, m) + m = h.answeredWords(r, m, kind) } if kind == "" { return m @@ -910,18 +910,24 @@ func (h *Holder) sayOn(r *Record, what, kind string) Message { return m } -// answeredWords is a message that says what came of its question. An ask's own message is the question -// itself, so it becomes how it ended and what it was about — "You chose Approve at 13:30 on Telegram · -// " — one line, read at a glance on the phone, where the edit rings nothing (issue 372); the body -// is not said again (issue 639), and what the router knows of the asker after it is the note. A condition's -// message keeps its own title, and says it first. -func (h *Holder) answeredWords(r *Record, m Message) Message { +// answeredWords is a message that says what came of its question. An ask's own message keeps the question +// whole — its headline as the title, the asker's summary, points and link as the body, its Details after them — +// so the operator can read back what they answered (issue 714, which undid issue 639's one line). How it ended +// ("You chose Approve at 13:30 on Telegram.") and what the router knows of the asker after it are the note, +// below the question, and its answers are gone (issue 372). A channel that must shorten it cuts the body from +// its end, the Details first; never the headline or the note. A condition's message keeps its own title, and +// says how it ended first. +func (h *Holder) answeredWords(r *Record, m Message, kind string) Message { if r.Origin == "ask" { - first, rest, _ := strings.Cut(r.Answered, "\n") headline, _ := wordsOf(r) - out := Message{Title: strings.TrimSuffix(first, ".") + " · " + headline, Note: strings.TrimSpace(rest), + body := []string{} + if b := strings.TrimSpace(m.Body); b != "" { + body = append(body, b) + } + body = append(body, h.answeredDetails(r, kind)...) + out := Message{Title: headline, Body: strings.Join(body, "\n"), Note: strings.TrimSpace(r.Answered), Urgent: m.Urgent, Quiet: m.Quiet} - if _, ok := Check(out.Text()); ok { + if _, ok := Check(out.Title + "\n" + out.Note); ok { return out } // Its headline refused (it is the asker's): the outcome is still said, before what may leave. @@ -930,6 +936,33 @@ func (h *Holder) answeredWords(r *Record, m Message) Message { return m } +// answeredDetails are an answered ask's Details, line by line, as its open message noted them (issue 714): whole +// on a kind that proves who answers, a line that may not leave withheld and said so. +func (h *Holder) answeredDetails(r *Record, kind string) []string { + if r.Ask == "" { + return nil + } + e := h.askOf(r.Ask) + if e == nil { + return nil + } + details, passes := e.rec.Ask.Details, Check + if wd := h.wholeDetailsOf(r, kind); wd != "" { + details, passes = wd, CheckSecret + } + var lines []string + for _, line := range strings.Split(strings.TrimSpace(details), "\n") { + if line = strings.TrimSpace(line); line == "" { + continue + } + if refusal, ok := passes(line); !ok { + line = withheldLine(line, refusal, passes) + } + lines = append(lines, line) + } + return lines +} + // wholeWords is a record's message with the ask's whole words in place of its explanation, when they carry no // secret's shape once composed; else false, and the message under the whole content rule stands. func (h *Holder) wholeWords(r *Record, what, whole string) (Message, bool) { diff --git a/modules/messenger/cmd/messenger/preview.go b/modules/messenger/cmd/messenger/preview.go index caa40e94..dc73f910 100644 --- a/modules/messenger/cmd/messenger/preview.go +++ b/modules/messenger/cmd/messenger/preview.go @@ -91,7 +91,7 @@ func (h *Holder) Preview(key, what string) (map[string]any, error) { asking := h.openAsk(&r) != nil // askOf takes the same lock, so this is asked outside it m, line, refusal, ownWords := h.allowedWords(&r, what, now) if r.Answered != "" { - m = h.answeredWords(&r, m) + m = h.answeredWords(&r, m, "") } out := map[string]any{ "key": key, diff --git a/modules/messenger/cmd/messenger/replaced_test.go b/modules/messenger/cmd/messenger/replaced_test.go index 100e0b80..467a9c0c 100644 --- a/modules/messenger/cmd/messenger/replaced_test.go +++ b/modules/messenger/cmd/messenger/replaced_test.go @@ -50,7 +50,7 @@ func TestAReplacedAsksMessageEndsWithIt(t *testing.T) { m, edited = r.m, true } } - if !edited || len(m.Actions) != 0 || !strings.Contains(m.Title, "Replaced by a newer question") { + if !edited || len(m.Actions) != 0 || !strings.HasPrefix(m.Note, "Replaced by a newer question") { t.Errorf("%s: the replaced ask's message still stands: edited %v, %q %v", ch.name, edited, m.Text(), labelsOf(m)) } } @@ -86,7 +86,7 @@ func TestAnAsksMessageLeftOpenAfterItsAskEndedIsEndedOnTheNextTick(t *testing.T) if got := openAskRecords(h); len(got) != 0 { t.Fatalf("the message of an ended ask is still open: %v", got) } - if m, ok := tg.edits["1"]; !ok || len(m.Actions) != 0 || !strings.Contains(m.Title, "Replaced by a newer question") { + if m, ok := tg.edits["1"]; !ok || len(m.Actions) != 0 || !strings.HasPrefix(m.Note, "Replaced by a newer question") { t.Errorf("the leftover's copy was not ended: %v %q %v", ok, m.Text(), labelsOf(m)) } } diff --git a/modules/messenger/cmd/messenger/whole_test.go b/modules/messenger/cmd/messenger/whole_test.go index 93b3e4d3..e696f832 100644 --- a/modules/messenger/cmd/messenger/whole_test.go +++ b/modules/messenger/cmd/messenger/whole_test.go @@ -61,8 +61,7 @@ func TestAnAsksWholeWordsAreShownWhereTheSenderIsProvenAndMaskedElsewhere(t *tes if got := w.of("mesh-controller"); len(got) != 0 || store.asks["mesh-controller.s-1"].State != StateOpen { t.Errorf("the ask was answered: %+v / %s", got, store.asks["mesh-controller.s-1"].State) } - // Approved, the message is edited to one line that says so, with the headline; the body is not said again - // (issue 639). + // Approved, the message keeps its headline and words, and says below them how it ended (issue 714). if out, err := h.Chosen("telegram", choiceOf(ticketFor(phone, "Release"), "42", c.now())); err != nil || out != "chosen" { t.Fatalf("Release: %q %v", out, err) } @@ -70,8 +69,8 @@ func TestAnAsksWholeWordsAreShownWhereTheSenderIsProvenAndMaskedElsewhere(t *tes if !ok { t.Fatalf("the phone's message was not edited: %+v", tg.edits) } - if !strings.HasPrefix(edited.Title, "You chose Release at ") || !strings.HasSuffix(edited.Title, " · Set mounts on shanks?") || - edited.Body != "" || len(edited.Actions) != 0 { + if edited.Title != "Set mounts on shanks?" || edited.Body == "" || + !strings.HasPrefix(edited.Note, "You chose Release at ") || len(edited.Actions) != 0 { t.Errorf("the edited message: %q\n%s\nactions %+v", edited.Title, edited.Body, edited.Actions) } } diff --git a/modules/telegram/cmd/telegram/render.go b/modules/telegram/cmd/telegram/render.go index 9bdd257f..81c22782 100644 --- a/modules/telegram/cmd/telegram/render.go +++ b/modules/telegram/cmd/telegram/render.go @@ -4,10 +4,11 @@ package main // formatting", "too much text, no formatting, no summary"). The router's words are shown as given — // `exact-render` — and formatted, never reworded: // -// - the title in bold: the headline, or how an answered ask ended; +// - the title in bold: the headline, an answered ask's too (issue 714); // - the body as it is, a line that is a key with its value (as a settings proposal lists them, "+ key: value") // in monospace; -// - the note — who asked, what each answer does, the expiry, the asker's details — apart, after a blank line, +// - the note — who asked, what each answer does, the expiry, the asker's details; once answered, how it ended +// and who acts on it — apart, after a blank line, // in a blockquote Telegram shows collapsed to its first lines, expanded on a tap. // // Every character Telegram's HTML would read as markup is escaped first, so a message carrying '<' neither diff --git a/modules/telegram/cmd/telegram/render_test.go b/modules/telegram/cmd/telegram/render_test.go index a2658313..963136d8 100644 --- a/modules/telegram/cmd/telegram/render_test.go +++ b/modules/telegram/cmd/telegram/render_test.go @@ -84,3 +84,20 @@ func TestTheNoteIsEscaped(t *testing.T) { t.Errorf("%q", got) } } + +// novox/hq issue 714: an answered ask keeps its question whole, with how it ended below it. Over Telegram's bound, +// the end of the body (the asker's Details) is cut; the headline and the answer are never. +func TestAnAnsweredAskOverTheBoundKeepsItsHeadlineAndItsAnswer(t *testing.T) { + answer := "You chose Approve at 20:03 on Telegram.\nTold to mesh-knowledge, which acts on it itself." + m := asks.Message{Handle: "h-714", To: "42", Title: "Fixes reach every machine at once, unchecked", + Body: "A fix goes to every machine in one step.\n• No machine goes first\n" + strings.Repeat("Details line. ", 400), + Note: answer} + s := partsOf(m) + if n := len([]rune(s.plain())); n > MaxLength { + t.Fatalf("the answered ask is %d characters, over %d", n, MaxLength) + } + if s.title != m.Title || s.note != answer || !strings.HasPrefix(s.body, "A fix goes to every machine in one step.\n• No machine goes first") || + !strings.HasSuffix(s.body, cutNote) { + t.Errorf("the headline, the summary or the answer was cut:\n%s", s.plain()) + } +}