Compare commits
55
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ecadf294e | ||
|
|
ae8b0c65bc | ||
|
|
1568877f35 | ||
|
|
04eda6130d | ||
|
|
959e8d6d30 | ||
|
|
01543d9121 | ||
|
|
8c8b4367f1 | ||
|
|
c775aab29a | ||
|
|
df79ab8769 | ||
|
|
5b06dd7e43 | ||
|
|
c99ad77633 | ||
|
|
401289afac | ||
|
|
a6a6135f74 | ||
|
|
c91ab3acfa | ||
|
|
ba0330c205 | ||
|
|
15458aeb61 | ||
|
|
815be6bdce | ||
|
|
f85f639afe | ||
|
|
0400d7bbe7 | ||
|
|
3ba309c5e4 | ||
|
|
7f5f285b60 | ||
|
|
f0bf4578a9 | ||
|
|
40dc5fe11d | ||
|
|
82d5688477 | ||
|
|
57984e9b98 | ||
|
|
2f471edc0f | ||
|
|
6cde12bf2f | ||
|
|
e534f667da | ||
|
|
4f10af81b0 | ||
|
|
6d398264db | ||
|
|
e44a2d140d | ||
|
|
f9857ab712 | ||
|
|
0cafb62d0e | ||
|
|
d7dada57aa | ||
|
|
a1bc613702 | ||
|
|
abcbeed115 | ||
|
|
673d1db95a | ||
|
|
c7f54357ad | ||
|
|
a30852939c | ||
|
|
9cfda8d6c7 | ||
|
|
a943b6a7db | ||
|
|
d901c6c19d | ||
|
|
30d1527e6a | ||
|
|
f82338e8ff | ||
|
|
c28fd39d23 | ||
|
|
ac2cd3749c | ||
|
|
4bdcedd0f0 | ||
|
|
f85199f624 | ||
|
|
13d43a7de0 | ||
|
|
5dfb357333 | ||
|
|
83accdbb4e | ||
|
|
7c64c17e65 | ||
|
|
0a446c7572 | ||
|
|
e1521a2ecf | ||
|
|
3a94021766 |
@@ -5,7 +5,7 @@
|
||||
// exempt from that, and said so. Every build module, test step or not, must also hold a test that calls
|
||||
// conformance.LeftRunning with its own start sweep, so a step a previous process left running is ended at its start
|
||||
// (issue 1890), and one that calls conformance.Passing, so a step whose network did not answer is said as a transport fault
|
||||
// (issue 1748). Its manifest asks the capability seat-settle-delay (ADR 0664 §5).
|
||||
// (issue 1748), and one that calls conformance.StepEnds, so its steps can say a pipeline step's end (design 47 G §24). Its manifest asks the capability seat-settle-delay (ADR 0664 §5).
|
||||
package failurekit
|
||||
|
||||
import (
|
||||
@@ -91,6 +91,25 @@ func Judge(root string) ([]Judged, error) {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.LeftRunning "+
|
||||
"(mesh-build-go buildstep/conformance): run the kit's left-running case with the module's own start sweep (issue 1890)", seat))
|
||||
}
|
||||
// A-489b: every build module is asked pipeline steps, so every one runs the case that its steps can say a step's end: without a
|
||||
// SayStep every step ask is refused live ("this build module says no step's end"), as mesh-build-node's was.
|
||||
ends, err := runsTheKit(dir, "conformance.StepEnds(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts, err := runsTheKit(dir, "conformance.RunsCounted(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !counts {
|
||||
// A-489c
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.RunsCounted "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's case that its puller counts the runs that started, not the bus's deliveries (A-489a)", seat))
|
||||
}
|
||||
if !ends {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.StepEnds "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's step-ends case with the module's own steps (design 47 G §24)", seat))
|
||||
}
|
||||
// Every build module meets the network (a fetch, a download, an install, a publish), so every one runs the passing case: its
|
||||
// steps say a network that did not answer as a transport fault the bus gives the ask again for (ADR 0664, issue 1748).
|
||||
says, err := runsTheKit(dir, "conformance.Passing(")
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
const imp = "package main\n\nimport \"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/conformance\"\n\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n"
|
||||
kit := imp + "func TestKit(t *testing.T) { conformance.Run(t, conformance.GoTest, nil) }\n" + sweep
|
||||
for path, body := range map[string]string{
|
||||
"modules/a/module.json": `{"claims":[{"name":"node-build-a"}],"tools":["build_a_fetch","build_a_test"]}`,
|
||||
@@ -30,6 +30,10 @@ func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
"modules/h/cmd/h/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\n",
|
||||
"modules/i/module.json": `{"claims":[{"name":"node-build-i"}],"capabilities":["other"],"tools":["build_i_build"]}`, // the whole kit, and no seat-settle-delay
|
||||
"modules/i/cmd/i/kit_test.go": imp + sweep,
|
||||
"modules/j/module.json": `{"claims":[{"name":"node-build-j"}],"capabilities":["seat-settle-delay"],"tools":["build_j_build"]}`, // the rest of the kit, and no step-ends case
|
||||
"modules/j/cmd/j/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n",
|
||||
"modules/k/module.json": `{"claims":[{"name":"node-build-k"}],"capabilities":["seat-settle-delay"],"tools":["build_k_build"]}`, // no runs-counted case
|
||||
"modules/k/cmd/k/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\n",
|
||||
"modules/e/module.json": `{"claims":[{"name":"node-build-e"}],"tools":["build_e_test"]}`,
|
||||
"modules/e/cmd/e/kit.go": kit, // not a test file
|
||||
} {
|
||||
@@ -45,9 +49,9 @@ func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused", "h": "refused", "i": "refused"}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused", "h": "refused", "i": "refused", "j": "refused", "k": "refused"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f, g, h and i", got)
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f, g, h, i, j and k", got)
|
||||
}
|
||||
for _, j := range got {
|
||||
is := "passes"
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -118,12 +119,21 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
if got := strings.Join(addresses, ","); got != "mesh-module-assigner.assignments,node-service-manager.status,node-service-manager.journal,docker.docker_logs" {
|
||||
t.Errorf("replacements: %s", got)
|
||||
}
|
||||
t.Run("the again tool answers the operator and the healers only", func(t *testing.T) {
|
||||
// An operator's session through the mesh MCP server is proved as its machine's tool runner (issue 627, issue 1966).
|
||||
for caller, refused := range map[string]bool{"person.jochen": false, "shanks.node-tools": false, "shanks.mesh-healers": false,
|
||||
"shanks.mesh-config": true} {
|
||||
if err := seatlist.Refusal("claude_code_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused {
|
||||
t.Errorf("%s: refused=%v, err %v", caller, refused, err)
|
||||
t.Run("the again tool answers every caller", func(t *testing.T) {
|
||||
// H-A-66 (design 47 H §752): no holder keeps a callers list; the seat list's tools answer every caller.
|
||||
files, _ := filepath.Glob("*.go")
|
||||
for _, f := range files {
|
||||
if strings.HasSuffix(f, "_test.go") {
|
||||
continue
|
||||
}
|
||||
raw, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, gone := range []string{"AnyPerson", "seatListAgainCallers", "seatlist.Refusal"} {
|
||||
if strings.Contains(string(raw), gone) {
|
||||
t.Errorf("%s still names %s", f, gone)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -734,17 +734,12 @@ func refusals(n int) string {
|
||||
return fmt.Sprintf(" (after %d refusal(s))", n)
|
||||
}
|
||||
|
||||
// seatListAgainCallers may read the seat list again: every operator's session and the healers (ADR 0636 §7). A call through
|
||||
// the mesh MCP server is proved only as its machine's tool runner (<node>.node-tools, issue 627), so node-tools stands for
|
||||
// an operator's session until a session is proved (issue 1966).
|
||||
var seatListAgainCallers = []string{stdio.AnyPerson, "node-tools", "mesh-healers"}
|
||||
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now. The
|
||||
// reader starts once the runtime serves, after the tools are listed, so each tool finds it when it is called.
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now, each
|
||||
// answering every caller (design 47 H §752, H-A-66). The reader starts once the runtime serves, after the tools are
|
||||
// listed, so each tool finds it when it is called.
|
||||
func seatListTools() []stdio.Tool {
|
||||
names := []string{"claude_code_seat_list", "claude_code_seat_list_again"}
|
||||
callers := seatListAgainCallers
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1], callers)
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1])
|
||||
for i := range shape {
|
||||
run := i
|
||||
shape[i].Run = func(args map[string]any) (any, error) {
|
||||
@@ -754,7 +749,7 @@ func seatListTools() []stdio.Tool {
|
||||
if r == nil {
|
||||
return nil, errors.New("the seat list reader has not started yet: the runtime has not served this module")
|
||||
}
|
||||
return seatlist.Tools(r, names[0], names[1], callers)[run].Run(args)
|
||||
return seatlist.Tools(r, names[0], names[1])[run].Run(args)
|
||||
}
|
||||
}
|
||||
return shape
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/publishconformance"
|
||||
)
|
||||
|
||||
type memObjects map[string][]byte
|
||||
@@ -666,6 +667,22 @@ func TestTheHolderOfTheTwoImageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
// Design 47 G A-490: the publish contract's no-parts form, as every publish holder is held to it.
|
||||
t.Run("the publish conformance cases", func(t *testing.T) {
|
||||
publishconformance.Run(t, func(t *testing.T) publishconformance.Bed {
|
||||
b := newBed(true)
|
||||
return publishconformance.Bed{
|
||||
Hold: func(d string) { b.store.has[d] = true },
|
||||
Publish: func(name, version, digest string) publishconformance.Outcome {
|
||||
got, err := b.h.Publish(ArtifactSeat, builder, PublishRequest{Name: name, Version: version, Kind: KindBundle, Digest: digest, Transfer: "t"})
|
||||
o := publishconformance.Outcome{Ended: got.Ended, Lacks: len(got.Missing), Err: err}
|
||||
if len(b.said) > 0 {
|
||||
o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].body.Reason
|
||||
}
|
||||
return o
|
||||
}}
|
||||
})
|
||||
})
|
||||
t.Run("a publish with a version and no parts tags the held digest at the store's size; one not held is not stored", func(t *testing.T) {
|
||||
// Design 47 G A-417, A-73, A-50: the build stored the bundle untagged; the builder's publish of its version with that
|
||||
// digest and no parts tags it and says published, nothing sent; a digest the store holds none of is refused as not
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): before it merges
|
||||
// anything, the forge module writes the call's key `call.<call>` in its own template state `calls` (the seat git's
|
||||
// definition declares it): `by`, the bus account the tool runner proved, `at`, the members, each with its base `branch`
|
||||
// and `state` judged, merged with its commit, or refused with why, and `said`, whether its `merge-called` was said. Each
|
||||
// member is updated as it merges. A call is kept `call-retention` (a setting, default 30 days), then deleted by the forge
|
||||
// module on each new call's write, oldest first, with no timer (H-A-47).
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import type { State } from "@novox/mesh-sdk/state";
|
||||
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;
|
||||
|
||||
/** call-retention as the module's config file says it, in days; the default when it says none (mainrules.ts' form). */
|
||||
export function callRetentionDays(env: NodeJS.ProcessEnv = process.env): number {
|
||||
const file = env.MESH_GITEA_CONFIG_FILE;
|
||||
if (!file) return CALL_RETENTION_DAYS;
|
||||
try {
|
||||
const v = Number((JSON.parse(readFileSync(file, "utf8")) as Record<string, unknown>)["call-retention"]);
|
||||
return Number.isInteger(v) && v >= 1 ? v : CALL_RETENTION_DAYS;
|
||||
} catch { return CALL_RETENTION_DAYS; }
|
||||
}
|
||||
|
||||
/** One kept call with its id. */
|
||||
export interface KeptCall extends Call {
|
||||
call: string;
|
||||
}
|
||||
|
||||
/** The state `calls`: one key per merge call. */
|
||||
export class Calls {
|
||||
constructor(private readonly st: State<unknown>, private readonly retentionDays: () => number = () => CALL_RETENTION_DAYS,
|
||||
private readonly log: (line: string) => void = () => {}) {}
|
||||
|
||||
/** Every call kept, newest first. */
|
||||
async all(): Promise<KeptCall[]> {
|
||||
const out: KeptCall[] = [];
|
||||
for (const key of await this.st.keys()) {
|
||||
const parsed = parseCallsByCallCallKey(key);
|
||||
if (!parsed) continue;
|
||||
const e = await this.st.get(key);
|
||||
if (e?.value) out.push({ ...(e.value as Call), call: parsed.call });
|
||||
}
|
||||
return out.sort((a, b) => (a.at < b.at ? 1 : a.at > b.at ? -1 : a.call < b.call ? 1 : -1));
|
||||
}
|
||||
|
||||
/** One call, or undefined. */
|
||||
async get(call: string): Promise<KeptCall | undefined> {
|
||||
const e = await this.st.get(callsByCallCallKey(call));
|
||||
return e?.value ? { ...(e.value as Call), call } : undefined;
|
||||
}
|
||||
|
||||
/** Writes a call. A new call (none kept under its key) first deletes the calls past call-retention, oldest first. */
|
||||
async put(call: string, value: Call, now: Date = new Date()): Promise<void> {
|
||||
if (!(await this.st.get(callsByCallCallKey(call)))?.value) await this.expire(now);
|
||||
await this.st.put(callsByCallCallKey(call), value);
|
||||
}
|
||||
|
||||
/** Deletes the calls whose `at` is past call-retention before now, oldest first; answers the calls deleted. */
|
||||
async expire(now: Date): Promise<string[]> {
|
||||
const before = now.getTime() - this.retentionDays() * 86_400_000;
|
||||
const old = (await this.all()).filter((c) => (Date.parse(c.at) || 0) < before).reverse();
|
||||
for (const c of old) await this.st.delete(callsByCallCallKey(c.call));
|
||||
if (old.length) this.log(`${old.length} merge calls past ${this.retentionDays()} days deleted: ${old.map((c) => c.call).join(", ")}`);
|
||||
return old.map((c) => c.call);
|
||||
}
|
||||
}
|
||||
|
||||
/** A kept member of a call. */
|
||||
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<void>;
|
||||
|
||||
/** 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<boolean> {
|
||||
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<string[]> {
|
||||
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;
|
||||
}
|
||||
@@ -291,7 +291,7 @@ func verbTools[PI, PA, PPI, PPA, VI, VA, RI, RA any](seat string, holder func()
|
||||
}
|
||||
return []stdio.Tool{
|
||||
{Name: seat + "." + packageseats.VerbPublish, Input: obj,
|
||||
Description: "Open a transfer of a package's one file (ADR 0611): its name, the builder's version, kind package, its sha256 digest and the file as its one part; answers the part the forge lacks. A version held with that digest answers held and says published.",
|
||||
Description: "Open a transfer of a package's one file (ADR 0611): its name, the builder's version, kind package, its sha256 digest and the file as its one part; answers the part the forge lacks. A version held with that digest answers held and says published; with no part at all (the builder's publish again) nothing moves: held says published, else refused as not stored, naming the digest (design 47 G A-490).",
|
||||
Run: run(func(h *packageseats.Holder, args map[string]any) (any, error) {
|
||||
in, err := as[PI](args)
|
||||
if err != nil {
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
failedStepsByModuleFingerprintStepKey as failedKey, type FailedStep, type PullClosedEvent, type PullMergedEvent, type PullUpdatedEvent,
|
||||
} from "@novox/mesh-delivery-contracts-git";
|
||||
import type { StepEndedEvent } from "@novox/mesh-delivery-contracts-mesh-pipeline-runner";
|
||||
import { findingKey } from "./catalog.js";
|
||||
|
||||
/** The key prefix of the commits' fingerprints (§487). */
|
||||
export const SEEN = "seen.";
|
||||
@@ -58,6 +59,22 @@ const safe = (s: string): string => s.replace(/[^A-Za-z0-9_=/-]/g, "_");
|
||||
export const seenKey = (commit: string, module: string): string => `${SEEN}${commit}.${safe(module)}`;
|
||||
export const failedStepKey = (module: string, fingerprint: string, step: string): string => failedKey(safe(module), safe(fingerprint), safe(step));
|
||||
|
||||
/** The finding of a step-ended the forge module could not record (G §585). */
|
||||
export const STEP_UNREAD = "step-unread";
|
||||
|
||||
/** step-unread of one step of one run (H-A-93): its key in the state findings and its value. */
|
||||
export function stepUnreadFinding(run: string, module: string, step: string, at: string): { key: string; value: Record<string, any> } {
|
||||
const key = findingKey("module", `${run}.${module}.${step}`, STEP_UNREAD);
|
||||
const summary = `the step-ended of ${step} of ${module} in run ${run} names no build-input fingerprint, so a failure of it cannot be recorded`;
|
||||
return { key, value: {
|
||||
v: 1, key, kind: STEP_UNREAD, subject: { scope: "module", id: module }, severity: "warning", resolver: "operator", summary,
|
||||
headline: `A step the forge module could not read: ${step} of ${module}`,
|
||||
explanation: `merge-pull judges a head by the failed required steps at its fingerprint; this step-ended of run ${run} carries no fingerprint, ` +
|
||||
"so the forge module could not keep it. A later step-ended of the step with its fingerprint clears this.",
|
||||
resolved: "", evidence: [{ at, said: summary }], source: `gitea.${STEP_UNREAD}`, observer: "gitea", at,
|
||||
} };
|
||||
}
|
||||
|
||||
export class FailedSteps {
|
||||
/** Every act runs after the one before it, so a step-ended and a head's loss never interleave their reads and writes. */
|
||||
private chain: Promise<unknown> = Promise.resolve();
|
||||
@@ -66,7 +83,9 @@ export class FailedSteps {
|
||||
/** Whether a commit is now a watched branch's head or an open pull request's head. */
|
||||
private readonly current: (commit: string) => boolean = () => false,
|
||||
/** The state's fill against its caps: the bundle's state call (§623). */
|
||||
private readonly fillOf: () => Promise<StateFill> = askFill) {}
|
||||
private readonly fillOf: () => Promise<StateFill> = askFill,
|
||||
/** The module's state findings, where step-unread is written (G §585, H-A-93); none writes no finding. */
|
||||
private readonly findings?: State<unknown>) {}
|
||||
|
||||
private turn<T>(act: () => Promise<T>): Promise<T> {
|
||||
const r = this.chain.then(act, act);
|
||||
@@ -98,9 +117,13 @@ export class FailedSteps {
|
||||
|
||||
private record(ev: StepEndedEvent): Promise<void> {
|
||||
const r = ev?.result;
|
||||
const fingerprint = r?.fingerprint || ev?.fingerprint || "";
|
||||
if (!r || !SHA.test(String(ev.commit ?? "")) || !fingerprint || !ev.module) return Promise.resolve();
|
||||
// The fingerprint is the body's, the build-input fingerprint the runner fills in every step-ended; no field of the step's
|
||||
// result is read for it (G §585, H-A-93).
|
||||
const fingerprint = ev?.fingerprint || "";
|
||||
if (!r || !SHA.test(String(ev.commit ?? "")) || !ev.module) return Promise.resolve();
|
||||
if (!fingerprint) return this.turn(() => this.unread(ev, true));
|
||||
return this.turn(async () => {
|
||||
await this.unread(ev, false);
|
||||
const at = ev.at ?? new Date().toISOString();
|
||||
const sk = seenKey(ev.commit, ev.module);
|
||||
const held = (await this.st.get(sk))?.value as Seen | undefined;
|
||||
@@ -137,6 +160,26 @@ export class FailedSteps {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* step-unread (G §585, H-A-93): a step-ended without its fingerprint writes the finding, scope module, its subject the step's
|
||||
* module, keyed `<run>.<module>.<step>`, naming the run, the module and the step, severity warning, resolver operator; a
|
||||
* later step-ended of that step with its fingerprint clears it, so a failure that could not be recorded is seen.
|
||||
*/
|
||||
private async unread(ev: StepEndedEvent, raise: boolean): Promise<void> {
|
||||
if (!this.findings) return;
|
||||
const step = String(ev.result?.step ?? ""), run = String(ev.pipeline ?? "");
|
||||
const { key, value } = stepUnreadFinding(run, ev.module, step, String(ev.at ?? new Date().toISOString()));
|
||||
if (!raise) {
|
||||
if ((await this.findings.get(key))?.value) {
|
||||
await this.findings.delete(key);
|
||||
this.log(`${ev.module} ${step} of ${run} is read with its fingerprint: step-unread cleared`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
await this.findings.put(key, value);
|
||||
this.log(String(value.summary));
|
||||
}
|
||||
|
||||
/** The failed `required` step that refuses this head, or null: a head with a `seen` key is judged by it, one with none merges (§487). */
|
||||
judge(head: string): Promise<Judged | null> {
|
||||
return this.turn(async () => {
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
// **One writer of a head's mesh/merge-gate** (novox/hq issue 446).
|
||||
//
|
||||
// A pending check says where it is: "waiting in the build queue since …" when the head is announced. The forge keeps only
|
||||
// the newest status of a context, so two writers cannot order their writes; this module is the one writer of
|
||||
// mesh/merge-gate, and every write of one head's gate goes through one chain, in the order it was asked. A recheck's
|
||||
// pending (novox/hq issue 456) and a trunk's move (ADR 0323 §4) take their turns on the same chain. No verdict is written
|
||||
// here any more: the controller's `checked` is no event (ADR 0571 clause 13), and main requires the checker's mesh/check.
|
||||
|
||||
import { CHECK_CONTEXT, pendingWords } from "./pulls.js";
|
||||
import type { CommitStatus, HeadCheck } from "./client.js";
|
||||
|
||||
/** What the gate writer needs of the forge: a status set, and a head's statuses as the forge holds them. */
|
||||
export interface GateForge {
|
||||
setCommitStatus(owner: string, repo: string, sha: string, status: CommitStatus): Promise<void>;
|
||||
/** The head's statuses by context, the newest of each, with their words (novox/hq ADR 0323). */
|
||||
headChecks(owner: string, repo: string, sha: string): Promise<Record<string, HeadCheck>>;
|
||||
}
|
||||
|
||||
/** Whether a repository as the mesh clones it (`http://forge/novox/hq.git`) is owner/repo. */
|
||||
export function clonedAs(url: string | undefined, owner: string, repo: string): boolean {
|
||||
const u = (url ?? "").trim().toLowerCase().replace(/\.git$/, "");
|
||||
const full = `${owner}/${repo}`.toLowerCase();
|
||||
return u === full || u.endsWith("/" + full);
|
||||
}
|
||||
|
||||
interface Head {
|
||||
/** The chain the head's writes take their turns on. */
|
||||
chain: Promise<void>;
|
||||
}
|
||||
|
||||
export class GateWriter {
|
||||
private heads = new Map<string, Head>();
|
||||
private forge: GateForge;
|
||||
private now: () => Date;
|
||||
|
||||
constructor(forge: GateForge, now: () => Date = () => new Date()) {
|
||||
this.forge = forge;
|
||||
this.now = now;
|
||||
}
|
||||
|
||||
private head(owner: string, repo: string, sha: string): Head {
|
||||
const key = `${owner}/${repo}@${sha}`.toLowerCase();
|
||||
let h = this.heads.get(key);
|
||||
if (!h) {
|
||||
h = { chain: Promise.resolve() };
|
||||
this.heads.set(key, h);
|
||||
// Kept for the heads still in play; the oldest go first, past what any forge holds open at once.
|
||||
if (this.heads.size > 2000) this.heads.delete(this.heads.keys().next().value as string);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
/** One turn on the head's chain; a turn that fails never stops the next. Its failure is the caller's. */
|
||||
private turn(h: Head, fn: () => Promise<void>): Promise<void> {
|
||||
const run = h.chain.then(fn);
|
||||
h.chain = run.catch(() => {});
|
||||
return run;
|
||||
}
|
||||
|
||||
/** A turn whose failure is said here: a pending word not written is not worth the event again. */
|
||||
private said(h: Head, what: string, fn: () => Promise<void>): Promise<void> {
|
||||
return this.turn(h, fn).catch((err) => {
|
||||
console.error(`[gitea] ${what} — ${err instanceof Error ? err.message : err}`);
|
||||
});
|
||||
}
|
||||
|
||||
/** The head announced: it waits in the build queue from now. A new check of the head, so no verdict yet. */
|
||||
pending(owner: string, repo: string, sha: string): Promise<void> {
|
||||
const h = this.head(owner, repo, sha);
|
||||
const at = this.now();
|
||||
return this.said(h, `${owner}/${repo}@${sha.slice(0, 8)}: could not say a check is pending`, () =>
|
||||
this.forge.setCommitStatus(owner, repo, sha, { state: "pending", context: CHECK_CONTEXT, description: pendingWords(at) }));
|
||||
}
|
||||
|
||||
/**
|
||||
* A status the seat git's `status` verb sets (seat.ts), a recheck's pending among them (novox/hq issue 456): it takes
|
||||
* its turn after every write of the head already asked for, so it lands over them, and a later write lands over it. A
|
||||
* write that fails fails the caller.
|
||||
*/
|
||||
status(owner: string, repo: string, sha: string, status: CommitStatus): Promise<void> {
|
||||
return this.turn(this.head(owner, repo, sha), () => this.forge.setCommitStatus(owner, repo, sha, status));
|
||||
}
|
||||
|
||||
/**
|
||||
* **A trunk's move puts the head's verdict out of date** (novox/hq ADR 0323 §4): its merge check statuses are put back
|
||||
* to pending, with the reason, in their turn after every write of the head already asked for — a verdict in flight
|
||||
* lands first. What is written is decided in the turn, from the statuses the forge holds then (`decide`, trunk.ts's
|
||||
* outOfDateStatuses): a check on its way, or a verdict already judged against the trunk's head, is left as it is. It
|
||||
* opens no check — nothing is asked — so a verdict of a check asked before it keeps its weight. Answers what it wrote;
|
||||
* a write that fails fails the caller, and the move is weighed again on the next pass.
|
||||
*/
|
||||
outOfDate(owner: string, repo: string, sha: string,
|
||||
decide: (checks: Record<string, HeadCheck>) => CommitStatus[] | Promise<CommitStatus[]>, target?: string): Promise<CommitStatus[]> {
|
||||
const h = this.head(owner, repo, sha);
|
||||
const written: CommitStatus[] = [];
|
||||
return this.turn(h, async () => {
|
||||
for (const s of await decide(await this.forge.headChecks(owner, repo, sha))) {
|
||||
await this.forge.setCommitStatus(owner, repo, sha, target ? { ...s, target_url: target } : s);
|
||||
written.push(s);
|
||||
}
|
||||
}).then(() => written);
|
||||
}
|
||||
}
|
||||
+35
-148
@@ -4,8 +4,7 @@
|
||||
// Emits (novox/hq ADR 0041/0042):
|
||||
// module.gitea.repo.created — a repository appeared, however it was made (push, web UI, or tool)
|
||||
// module.gitea.pull.merged — a pull request was merged, however it was merged (web UI, API, or tool)
|
||||
// module.gitea.pull.updated — an open pull request's head moved, opened or pushed to: the mesh checks it
|
||||
// before it merges (novox/hq to-be 45 §9)
|
||||
// module.gitea.pull.updated — an open pull request's head moved, opened or pushed to, said from the forge's event alone
|
||||
//
|
||||
// module.gitea.pull.closed — a pull request closed unmerged: the delivery it was is stopped (novox/hq ADR 0239)
|
||||
//
|
||||
@@ -16,13 +15,8 @@
|
||||
// It reads no other module's state: mesh/check, and every act of a delivery part on the forge, arrive as verbs of the seat
|
||||
// git (seat.ts), asked by the forge bridge (novox/hq design 47 *The seat git*).
|
||||
//
|
||||
// This module is the one writer of mesh/merge-gate. It hears no build seat's event: since R4g a check's steps are asks
|
||||
// on the build seats' streams, whose backlog the delivery view shows (novox/hq ADR 0525 §9; issue 1887).
|
||||
//
|
||||
// **A trunk's move puts every open verdict into it out of date** (novox/hq ADR 0323 §4, trunk.ts): each pass reads the
|
||||
// head of every trunk an open pull request merges into, and when one moved, the merge check statuses of every open pull
|
||||
// request into it go back to pending with the reason. Nothing is run for it. A verdict that lands judged against a trunk
|
||||
// commit that is no longer the trunk's head is said out of date in place of its pass or failure.
|
||||
// No merge check is asked, weighed or written here (design 47 H §764; H-A-28, H-A-100): a merge is one merge call of the
|
||||
// seat git (mergecall.ts), its members judged by their heads and failed steps alone.
|
||||
//
|
||||
// Every pull request the forge holds is announced, whatever its repository: the controller holds the
|
||||
// module graph and decides what is checked — a repository is never asked to opt in.
|
||||
@@ -50,18 +44,18 @@ 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, sayUnsaid } from "./calls.js";
|
||||
import { Partials } from "./partials.js";
|
||||
import { callTools } from "./tools/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";
|
||||
import { GiteaAdmin, GiteaClient, movedSince } from "./client.js";
|
||||
import { keepMainRules, mainRulesStep } from "./mainrules.js";
|
||||
import { CHECK_CONTEXT, CHECK_STATUS, REPO_CHECK_CONTEXT, headKey, requiredOf, writesGate, type Announced } from "./pulls.js";
|
||||
import { actOn, announceNewHeads, startPass } from "./watch.js";
|
||||
import { GateWriter } from "./gate.js";
|
||||
import { HOOK_PATH, HOOK_PORT, Pace, claimHead, ensureSystemHook, eventsTool, keptSecret, receiver, type ForgeEvent } from "./webhook.js";
|
||||
import { keepSiblings, readSiblings, siblingsFile, weighSiblings } from "./siblings.js";
|
||||
import { actOn } from "./watch.js";
|
||||
import { HOOK_PATH, HOOK_PORT, Pace, ensureSystemHook, eventsTool, keptSecret, receiver, type ForgeEvent } from "./webhook.js";
|
||||
import { bindInBackground, bindUntilHeld } from "./bind.js";
|
||||
import { announcementOf, weighTrunks } from "./trunk.js";
|
||||
import { announcementOf } from "./updated.js";
|
||||
import { ReadAll, readAllTools } from "./readall.js";
|
||||
import { DeliveredWrites, writeTools } from "./writes.js";
|
||||
import { TOOLS_ACCOUNT, TOOLS_ORG, meshAccounts } from "./tools-account.js";
|
||||
@@ -107,7 +101,7 @@ async function pollRepos(client: GiteaClient): Promise<void> {
|
||||
// What has been announced is kept beside the module's state, so a restart does not announce the
|
||||
// whole history again — and the first tick on a machine with no record announces nothing, because
|
||||
// everything it sees then predates the watching.
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
||||
|
||||
import { join } from "node:path";
|
||||
// What has been announced is no longer kept in a file (novox/hq ADR 0406 §6, §7, issue 976): an announcement carries a
|
||||
// message id (announce.ts), so the same merge announced by the poll and by the forge's event, or again after a restart,
|
||||
@@ -179,7 +173,6 @@ async function announceClosed(client: GiteaClient, repo: RepoOf, pull: PullOf):
|
||||
throw err;
|
||||
}
|
||||
console.log(`[gitea] announced ${repo.full_name}#${pull.number} closed unmerged`);
|
||||
await siblingMoves(client, repo.full_name, pull.number);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -239,8 +232,6 @@ async function announceMerge(client: GiteaClient, repo: RepoOf, pull: PullOf): P
|
||||
// Said, because a trigger that fires silently is indistinguishable from one that did not
|
||||
// fire (novox/hq 04-ISSUES/131) — this line is how an operator knows the mesh was told.
|
||||
console.log(`[gitea] announced merge ${repo.full_name}#${pull.number} (${pull.merge_commit_sha.slice(0, 8)}) into ${pull.base}`);
|
||||
// Its own merge of the head that was read is no sibling's move; any other is (ADR 0382 §5).
|
||||
await siblingMoves(client, repo.full_name, pull.number);
|
||||
} catch (err) {
|
||||
announced.delete(pull.merge_commit_sha);
|
||||
throw err;
|
||||
@@ -248,123 +239,12 @@ async function announceMerge(client: GiteaClient, repo: RepoOf, pull: PullOf): P
|
||||
return true;
|
||||
}
|
||||
|
||||
// **Every new head of an open pull request is announced** (novox/hq to-be 45 §9): the mesh checks it against every
|
||||
// machine of its facts before it merges. What this process announced is remembered in memory only (novox/hq ADR 0406
|
||||
// §6, §7, issue 976): the announcement carries a message id (announce.ts), so the poll's, the forge event's and the
|
||||
// merge tool's are one to the bus. After a start, the first look announces a head only if the forge shows no merge check
|
||||
// of it yet — a head with one was announced — and only what moved in the last day, so a forge's whole backlog is not
|
||||
// checked at once and a restart asks no check again against a trunk that moved.
|
||||
let heads: Announced = {};
|
||||
let primedPulls = false;
|
||||
/** **A head, announced once, whichever path saw it first** (novox/hq issue 729): the poll or the forge's event. */
|
||||
async function announceHead(client: GiteaClient, repo: RepoOf, pull: OpenOf): Promise<boolean> {
|
||||
const key = headKey(repo.full_name, pull.number, pull.base);
|
||||
const sha = String(pull.head_sha ?? "");
|
||||
const before = heads[key];
|
||||
// Claimed before the first wait, so the other path finds it taken while this one is still announcing.
|
||||
if (!claimHead(heads, key, sha)) return false;
|
||||
try {
|
||||
// The announcement the merge tool makes too when it asks a check (trunk.ts, novox/hq ADR 0323 §5).
|
||||
await announce("pull.updated", await announcementOf(client, repo.owner, repo.name, repo.clone_url ?? "", pull));
|
||||
} catch (err) {
|
||||
if (before === undefined) delete heads[key];
|
||||
else heads[key] = before;
|
||||
throw err;
|
||||
}
|
||||
// A sibling's new head puts out of date every verdict that read the sibling at another head (ADR 0382 §5).
|
||||
await siblingMoves(client, repo.full_name, pull.number);
|
||||
// Said, so an operator knows the mesh was asked to check it.
|
||||
console.log(`[gitea] announced ${key} at ${sha.slice(0, 8)} to be checked before it merges`);
|
||||
// Pending until the verdict comes, so the pull request says a check is running rather than nothing — and
|
||||
// where it is: in the build queue from now, until a build machine takes it (novox/hq issue 446). Only where the base
|
||||
// still requires mesh/merge-gate (issue 1301): a main that requires mesh/check is the checker's;
|
||||
// a rule that cannot be read is said pending, as before.
|
||||
const rule = pull.base ? await client.appliedProtection(repo.owner, repo.name, String(pull.base)).catch(() => undefined) : undefined;
|
||||
if (writesGate(requiredOf(rule))) await gates.pending(repo.owner, repo.name, sha);
|
||||
return true;
|
||||
/** **A pull request's new head, said as pull.updated from the forge's event alone** (design 47 H §764; H-A-100): no
|
||||
* poll, no pending status and no sibling's weighing. */
|
||||
async function announceUpdated(client: GiteaClient, repo: RepoOf, pull: PullOf): Promise<void> {
|
||||
await announce("pull.updated", await announcementOf(client, repo.owner, repo.name, repo.clone_url ?? "", pull));
|
||||
console.log(`[gitea] said ${repo.full_name}#${pull.number} at ${String(pull.head_sha ?? "").slice(0, 8)} updated`);
|
||||
}
|
||||
type OpenOf = Awaited<ReturnType<GiteaClient["listOpenPullsSince"]>>[number];
|
||||
|
||||
/** Whether the forge shows a merge check of the head, in any state: only an announced head has one. */
|
||||
async function alreadyChecked(client: GiteaClient, repo: RepoOf, pull: OpenOf): Promise<boolean> {
|
||||
try {
|
||||
const checks = await client.headChecks(repo.owner, repo.name, String(pull.head_sha));
|
||||
return CHECK_CONTEXT in checks || REPO_CHECK_CONTEXT in checks || CHECK_STATUS in checks;
|
||||
} catch {
|
||||
return false; // the forge cannot say: announced, and the bus's message id collapses it if it was
|
||||
}
|
||||
}
|
||||
|
||||
let lastPullLook = "";
|
||||
/** The pass over every open pull request: run at the start and when the receiver listens, never on a timer (issue 1422). */
|
||||
async function passPulls(client: GiteaClient): Promise<void> {
|
||||
const began = new Date().toISOString();
|
||||
const dayAgo = new Date(Date.now() - 24 * 3600_000).toISOString();
|
||||
for (const repo of await client.listAllRepos()) {
|
||||
// Every open pull request, paged (novox/hq issue 579).
|
||||
const open = await client.listOpenPullsSince(repo.owner, repo.name, "");
|
||||
await announceNewHeads(repo.full_name, open, heads, primedPulls, dayAgo, {
|
||||
alreadyChecked: (pull) => alreadyChecked(client, repo, pull),
|
||||
announceHead: (pull) => announceHead(client, repo, pull),
|
||||
missed: (what) => pace.miss(what),
|
||||
});
|
||||
if (await trunksMoved(client, repo.owner, repo.name)) changedTrunks = true;
|
||||
}
|
||||
if (changedTrunks) keepTrunks();
|
||||
changedTrunks = false;
|
||||
primedPulls = true;
|
||||
lastPullLook = began;
|
||||
}
|
||||
|
||||
// **The trunks' heads, as this module last read them** (novox/hq ADR 0323 §4), by `owner/repo:branch`: kept beside the
|
||||
// other records, so a move made while this module was away is weighed on its first pass back. With no record — the
|
||||
// first pass after this rolls out — a trunk's head is taken as it is and nothing is put out of date: the verdicts
|
||||
// then standing are weighed on the trunk's next move, and the merge tool compares commits whatever this holds.
|
||||
const trunksRecord = process.env.MESH_GITEA_STATE_DIR ? join(process.env.MESH_GITEA_STATE_DIR, "trunks-seen.json") : null;
|
||||
let trunks: Record<string, string> = {};
|
||||
let changedTrunks = false;
|
||||
if (trunksRecord && existsSync(trunksRecord)) {
|
||||
try {
|
||||
trunks = (JSON.parse(readFileSync(trunksRecord, "utf8")) as { trunks: Record<string, string> }).trunks ?? {};
|
||||
} catch {
|
||||
// An unreadable record is no record: the next pass takes each trunk's head as it is.
|
||||
}
|
||||
}
|
||||
function keepTrunks(): void {
|
||||
if (!trunksRecord) return;
|
||||
mkdirSync(join(trunksRecord, ".."), { recursive: true });
|
||||
const tmp = trunksRecord + ".tmp";
|
||||
writeFileSync(tmp, JSON.stringify({ trunks }));
|
||||
renameSync(tmp, trunksRecord);
|
||||
}
|
||||
|
||||
/** Whether a trunk of one repository moved, weighed with this module's forge and its one writer (trunk.ts). */
|
||||
function trunksMoved(client: GiteaClient, owner: string, name: string): Promise<boolean> {
|
||||
return weighTrunks(client, gates, trunks, owner, name, () => siblingMoves(client, `${owner}/${name}`));
|
||||
}
|
||||
|
||||
// **The siblings each open pull request's latest verdict names** (novox/hq ADR 0382 §5, siblings.ts), by member, kept beside
|
||||
// the other records. A sibling's new head, close unmerged, merge, or a move of its base puts the merge check of every
|
||||
// member whose verdict names it back to pending, with the reason; nothing is run for it.
|
||||
const siblingsRecord = siblingsFile(process.env.MESH_GITEA_STATE_DIR);
|
||||
const siblingNamed = readSiblings(siblingsRecord);
|
||||
|
||||
/** A sibling moved, weighed (ADR 0382 §5): never fatal to what heard it. */
|
||||
async function siblingMoves(client: GiteaClient, repository: string, number?: number): Promise<void> {
|
||||
try {
|
||||
if (await weighSiblings(client, gates, siblingNamed, repository, number)) keepSiblings(siblingsRecord, siblingNamed);
|
||||
} catch (err) {
|
||||
console.error(`[gitea] whether a sibling of ${repository}${number ? "#" + number : ""} moved could not be weighed — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
}
|
||||
|
||||
// **This module is the one writer of a head's mesh/merge-gate** (novox/hq issue 446): the announcement's pending words,
|
||||
// where the check is when a build machine takes it, and the verdict — each head's writes in one chain, the verdict last
|
||||
// (gate.ts). A second writer elsewhere raced the verdict and could leave a pass reading pending.
|
||||
const gates = new GateWriter({
|
||||
setCommitStatus: (owner, repo, sha, status) => (gitea as GiteaClient).setCommitStatus(owner, repo, sha, status),
|
||||
headChecks: (owner, repo, sha) => (gitea as GiteaClient).headChecks(owner, repo, sha),
|
||||
});
|
||||
|
||||
/** How fast the polls look: 30 s until the forge's events are heard, five minutes while they are; and what
|
||||
* gitea_events says of them (webhook.ts). */
|
||||
@@ -400,27 +280,34 @@ const registrations = new Registrations((verb, input) => broker().request(verb,
|
||||
// The failed steps the runner's step-ended said, and the heads that keep them (failedsteps.ts; design 47 §39, §475, §487, §135).
|
||||
const heldHeads = new Heads();
|
||||
let reloadHeads: () => Promise<void> = async () => {};
|
||||
const failedSteps = new FailedSteps(state<unknown>("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit));
|
||||
// The merge calls, each written before its first merge (calls.ts; design 47 H §766).
|
||||
const calls = new Calls(state<unknown>("calls"), () => callRetentionDays(), (line) => console.log(`[gitea] ${line}`));
|
||||
// At the start, every call whose merge-called is not said says it for what landed (§766).
|
||||
const partials = new Partials(state<unknown>("findings"), calls, (line) => console.log(`[gitea] ${line}`));
|
||||
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<unknown>("failed-steps"), (line) => console.log(`[gitea] ${line}`), (commit) => heldHeads.has(commit),
|
||||
undefined, state<unknown>("findings"));
|
||||
|
||||
// The `seen` keys past their 30 days are deleted at the start, as on each step-ended (design 47 §581).
|
||||
void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys past 30 days could not be deleted at the start: ${err instanceof Error ? err.message : String(err)}`));
|
||||
|
||||
// gitea_events is served whether or not the forge is watched: it says so (novox/hq issue 729). The seat git's verbs are
|
||||
// served here, beside the one writer of a head's statuses: the seat's status takes its turn on the head's chain, after the
|
||||
// announcement's pending and a trunk's move, so a recheck's pending never lands over a later verdict (issue 456).
|
||||
// served here.
|
||||
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) }) : []),
|
||||
...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, registered: () => registrations.written, judge: (head) => failedSteps.judge(head),
|
||||
watched: () => registrations.watched, calls, say: seatPublish, partials }) : []),
|
||||
...callTools(calls, partials, seatPublish, async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }),
|
||||
...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. */
|
||||
const actOnEvent = (client: GiteaClient, event: ForgeEvent): Promise<void> => actOn<RepoOf, PullOf>(event, {
|
||||
getRepo: (o, r) => client.getRepo(o, r),
|
||||
getPull: (o, r, n) => client.getPullRequest(o, r, n) as Promise<PullOf & { state?: string; merged?: boolean }>,
|
||||
announceHead: (repo, pull) => announceHead(client, repo, pull as OpenOf),
|
||||
announceUpdated: (repo, pull) => announceUpdated(client, repo, pull),
|
||||
announceMerge: (repo, pull) => announceMerge(client, repo, pull),
|
||||
announceClosed: (repo, pull) => announceClosed(client, repo, pull),
|
||||
siblingMoves: (full, n) => siblingMoves(client, full, n),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -566,7 +453,9 @@ if (gitea) {
|
||||
const read = await readHeads(forge, registrations.watched, (line) => console.log(`[gitea] ${line}`));
|
||||
await failedSteps.lost(heldHeads.replace(read.names, read.branches, read.pulls));
|
||||
};
|
||||
void bindUntilHeld(`${RUNNER}.${RUNNER_EVENTS.stepEnded}`, () => on<StepEndedEvent>(`${RUNNER}.${RUNNER_EVENTS.stepEnded}`, (event) => failedSteps.stepEnded(event.body)));
|
||||
// The runner's step-ended is said on step-ended.<module>.<branch>.<commit> (the seat's definition).
|
||||
const stepEnded = `${RUNNER}.${RUNNER_EVENTS.stepEnded}.*.*.*`;
|
||||
void bindUntilHeld(stepEnded, () => on<StepEndedEvent>(stepEnded, (event) => failedSteps.stepEnded(event.body)));
|
||||
// A branch's head is read from the forge, never taken from the event (the seat's definition of branch-moved).
|
||||
void bindUntilHeld(`${GIT}.${GIT_EVENTS.branchMoved}`, () => on<BranchMovedEvent>(`${GIT}.${GIT_EVENTS.branchMoved}`, async (event) => {
|
||||
const { repository, branch } = event.body;
|
||||
@@ -580,9 +469,8 @@ if (gitea) {
|
||||
}
|
||||
void registrations.start().then(() => reloadHeads()).catch(say);
|
||||
}
|
||||
// Main takes no push and only the tools' account merges, in every repository, kept and not only when a token is
|
||||
// minted: at the start and every ten minutes after, so a repository made later gets it too (issue 948, ADR 0398 §2).
|
||||
// Only while the setting keep-main-rules is true (mainrules.ts); false, the default, leaves every repository's rule as set.
|
||||
// Main's rule is put right only when the tools' account's token is minted, and only while the setting keep-main-rules is
|
||||
// true (mainrules.ts); no timer keeps it (design 47 H §764, H-A-100).
|
||||
// The package seats' write token (packages-account.ts): minted at the start; after that renewed only when the Go
|
||||
// bundle that writes with it says the forge refused it (the finding PACKAGES_TOKEN_REFUSED), never on a timer.
|
||||
try {
|
||||
@@ -595,14 +483,13 @@ if (gitea) {
|
||||
} catch (err) {
|
||||
console.error(`[gitea] the package seats cannot write — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
tick(mainRulesStep(keepMainRules(), () => GiteaAdmin.fromEnv().ensureMainMerges("novox"), (line) => console.log(line)), 600_000);
|
||||
tick(() => pollRepos(client), 60_000);
|
||||
// **No timer asks the forge for pull requests or merges** (issue 1422): the forge's events announce them. One pass over
|
||||
// every repository runs at the start and again once the receiver listens, so what happened while the module was away or
|
||||
// not yet listening is announced; passes run one at a time, a failed one is asked again a minute later.
|
||||
let passes: Promise<void> = Promise.resolve();
|
||||
const pass = (): Promise<void> => (passes = passes.catch(() => {}).then(() =>
|
||||
startPass(async () => { await pollMerged(client); await passPulls(client); }, { retryMs: 60_000 })));
|
||||
pollMerged(client).catch((err) => console.error(`[gitea] the pass over every repository's merges failed — ${err instanceof Error ? err.message : err}`))));
|
||||
void pass();
|
||||
let catchUpAsked = false;
|
||||
catchUp = () => {
|
||||
|
||||
@@ -213,7 +213,9 @@ func (h *Holder) answer(transfer string, r parts.Received) Answer {
|
||||
return Answer{Transfer: transfer, Lacks: r.Lacks, Ended: r.Ended}
|
||||
}
|
||||
|
||||
// Publish is `publish` on seat for the proved caller: a package is one file, its digest the file's sha256.
|
||||
// Publish is `publish` on seat for the proved caller: a package is one file, its digest the file's sha256. With its one
|
||||
// part the file travels (design 51 *Publishing*); with none (the builder's publish again, design 47 G A-73, A-417) the
|
||||
// version is said published when the forge holds it under the digest, and refused as not stored otherwise.
|
||||
func (h *Holder) Publish(seat, caller string, p Publish) (Answer, error) {
|
||||
if err := checkPublish(seat, p); err != nil {
|
||||
return Answer{}, err
|
||||
@@ -225,12 +227,25 @@ func (h *Holder) Publish(seat, caller string, p Publish) (Answer, error) {
|
||||
} else if ok {
|
||||
if d != p.Digest {
|
||||
reason := fmt.Sprintf("%s@%s is published with %s, not %s", p.Name, p.Version, d, p.Digest)
|
||||
if len(p.Parts) == 0 {
|
||||
// The builder's publish again names nothing the forge holds under that digest: refused as not stored (A-490,
|
||||
// A-490a), the words its republish recognises.
|
||||
reason = "not stored: " + reason
|
||||
return Answer{Transfer: p.Transfer, Ended: true}, errors.Join(errors.New("publish: "+reason), h.refused(Transfer{Shape: Opening{Publish: p, Seat: seat}}, reason))
|
||||
}
|
||||
// A one-part publish of a taken version is refused with an event only, as the registry would (the builder's "version taken").
|
||||
return Answer{Transfer: p.Transfer, Ended: true}, h.refused(Transfer{Shape: Opening{Publish: p, Seat: seat}}, reason)
|
||||
}
|
||||
o := outcomeOf(p, "")
|
||||
o.Reference = Reference(p.Name, p.Version)
|
||||
return Answer{Transfer: p.Transfer, Ended: true}, h.Say(seat, EventPublished, o)
|
||||
}
|
||||
// A publish with a version, a digest and no parts (design 47 G A-73, A-417): what the forge holds under the version has
|
||||
// been said published above; nothing else travels, so the publish is refused as not stored, naming the digest.
|
||||
if len(p.Parts) == 0 {
|
||||
reason := fmt.Sprintf("not stored: the forge holds no %s of %s, so version %s names nothing", p.Digest, p.Name, p.Version)
|
||||
return Answer{Transfer: p.Transfer, Ended: true}, errors.Join(errors.New("publish: "+reason), h.refused(Transfer{Shape: Opening{Publish: p, Seat: seat}}, reason))
|
||||
}
|
||||
// The transfer's name is the caller's own (the seat's contract): none would open the seat's one nameless transfer,
|
||||
// which key() still makes a valid id.
|
||||
if strings.TrimSpace(p.Transfer) == "" {
|
||||
@@ -377,8 +392,8 @@ func checkPublish(seat string, p Publish) error {
|
||||
return fmt.Errorf("publish: %s holds packages, not a %s", seat, p.Kind)
|
||||
case !strings.HasPrefix(p.Digest, "sha256:"):
|
||||
return fmt.Errorf("publish: digest %q is no sha256 digest", p.Digest)
|
||||
case len(p.Parts) != 1 || p.Parts[0].Digest != p.Digest:
|
||||
return errors.New("publish: a package is one file, its one part the package's digest")
|
||||
case len(p.Parts) > 1 || (len(p.Parts) == 1 && p.Parts[0].Digest != p.Digest):
|
||||
return errors.New("publish: a package is one file, its one part the package's digest, or no part at all")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package packageseats
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/parts"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/publishconformance"
|
||||
)
|
||||
|
||||
type memObjects map[string][]byte
|
||||
@@ -50,10 +52,21 @@ func (b builder) Recorded(_, _, _ string) (bool, string, error) { return b.ok, "
|
||||
// forge is the forge's package registry: seat -> name@version -> file.
|
||||
type forge struct {
|
||||
files map[string]map[string][]byte
|
||||
digests map[string]string // seat+name@version -> a digest held with no bytes kept
|
||||
removed []string
|
||||
}
|
||||
|
||||
func (f *forge) holdDigest(seat, name, version, d string) {
|
||||
if f.digests == nil {
|
||||
f.digests = map[string]string{}
|
||||
}
|
||||
f.digests[seat+" "+name+"@"+version] = d
|
||||
}
|
||||
|
||||
func (f *forge) Held(seat, name, version string) (string, bool, error) {
|
||||
if d, ok := f.digests[seat+" "+name+"@"+version]; ok {
|
||||
return d, true, nil
|
||||
}
|
||||
b, ok := f.files[seat][name+"@"+version]
|
||||
if !ok {
|
||||
return "", false, nil
|
||||
@@ -150,6 +163,85 @@ func TestTheForgeHoldsBothPackageSeats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
})
|
||||
// Design 47 G A-490, A-490a: the publish contract's no-parts form, as every publish holder is held to it, per seat.
|
||||
for _, seat := range []string{GoSeat, NpmSeat} {
|
||||
t.Run("the publish conformance cases on "+seat, func(t *testing.T) {
|
||||
publishconformance.Run(t, func(t *testing.T) publishconformance.Bed {
|
||||
b := newBed(true)
|
||||
return publishconformance.Bed{
|
||||
Hold: func(d string) {
|
||||
b.forge.holdDigest(seat, publishconformance.Name, publishconformance.Version, d)
|
||||
},
|
||||
Publish: func(name, version, digest string) publishconformance.Outcome {
|
||||
a, err := b.h.Publish(seat, goBuild, Publish{Transfer: "t", Name: name, Version: version, Kind: KindPackage, Digest: digest})
|
||||
o := publishconformance.Outcome{Ended: a.Ended, Lacks: len(a.Lacks), Err: err}
|
||||
if len(b.said) > 0 {
|
||||
o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].o.Reason
|
||||
}
|
||||
return o
|
||||
}}
|
||||
})
|
||||
})
|
||||
}
|
||||
// Design 47 G A-490j: the one-part publish a build module makes while it builds, as every publish holder is held to it, per
|
||||
// seat, checked through the real Reserved against the builder's answer.
|
||||
for _, seat := range []string{GoSeat, NpmSeat} {
|
||||
t.Run("the publish while the build runs on "+seat, func(t *testing.T) {
|
||||
publishconformance.RunReserved(t, func(t *testing.T) publishconformance.ReservedBed {
|
||||
b := newBed(true)
|
||||
whole := []byte("a package built now")
|
||||
answer := ""
|
||||
b.h.Builder = Reserved{Ask: func(string, any) (json.RawMessage, error) {
|
||||
if answer == "" {
|
||||
return nil, errors.New("no reserved version of mesh-sdk")
|
||||
}
|
||||
return json.RawMessage(answer), nil
|
||||
}}
|
||||
return publishconformance.ReservedBed{
|
||||
Reserve: func(r publishconformance.Reservation) {
|
||||
d := map[publishconformance.Reservation]string{publishconformance.NoDigestYet: "", publishconformance.SameDigest: digest(whole),
|
||||
publishconformance.OtherDigest: digest([]byte("another"))}[r]
|
||||
answer = `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:` + seat + `","digest":"` + d + `"}]}`
|
||||
},
|
||||
Hold: func() { b.forge.holdDigest(seat, "mesh-sdk", "0.1.19", digest([]byte("first"))) },
|
||||
PublishWhole: func() publishconformance.Outcome {
|
||||
a, err := b.h.Publish(seat, goBuild, pub("mesh-sdk", "0.1.19", whole))
|
||||
if err == nil && !a.Ended {
|
||||
_, err = b.h.PublishPart(seat, goBuild, "t-mesh-sdk", digest(whole), 0, true, whole)
|
||||
}
|
||||
o := publishconformance.Outcome{Err: err}
|
||||
if len(b.said) > 0 {
|
||||
o.Event, o.Reason = b.said[len(b.said)-1].event, b.said[len(b.said)-1].o.Reason
|
||||
}
|
||||
return o
|
||||
}}
|
||||
})
|
||||
})
|
||||
}
|
||||
// Design 47 G A-73, A-417: the builder's publish again names the version and the digest and no parts.
|
||||
t.Run("a publish with a digest and no parts is published when held, refused as not stored when not", func(t *testing.T) {
|
||||
for _, seat := range []string{NpmSeat, GoSeat} {
|
||||
b := newBed(true)
|
||||
none := pub("mesh-sdk", "0.1.0", file)
|
||||
none.Parts = nil
|
||||
a, err := b.h.Publish(seat, goBuild, none)
|
||||
if err == nil || !strings.Contains(err.Error(), "not stored") || !strings.Contains(err.Error(), digest(file)) || !a.Ended {
|
||||
t.Fatalf("%s: a publish of a digest the forge lacks answered %+v, %v; want refused as not stored, naming the digest", seat, a, err)
|
||||
}
|
||||
if len(b.said) != 1 || b.said[0].event != EventRefused || !strings.Contains(b.said[0].o.Reason, digest(file)) || !b.findings["publish-refused mesh-sdk@0.1.0"] {
|
||||
t.Errorf("%s: said %+v, findings %v; want refused with the digest and the finding raised", seat, b.said, b.findings)
|
||||
}
|
||||
b = newBed(true)
|
||||
if _, ok := b.forge.files[seat]; !ok {
|
||||
b.forge.files[seat] = map[string][]byte{}
|
||||
}
|
||||
b.forge.files[seat]["mesh-sdk@0.1.0"] = file
|
||||
a, err = b.h.Publish(seat, goBuild, none)
|
||||
if err != nil || !a.Ended || len(a.Lacks) != 0 || len(b.said) != 1 || b.said[0].event != EventPublished || b.said[0].o.Reference != "mesh-sdk@0.1.0" {
|
||||
t.Errorf("%s: a held digest answered %+v, %v, said %+v; want published", seat, a, err, b.said)
|
||||
}
|
||||
}
|
||||
})
|
||||
// Tools review F4 and running review F2's kind: a seat's incoming is read per seat with the cap its binding
|
||||
// carries; an open transfer is armed with its idle key, re-armed for the rest when it took a chunk since, and
|
||||
// dropped with its chunks when it had none for 24 hours; a cap from the binding refuses a transfer past it.
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
// Reserved is the holder's check of a publish against the builder's reservation (novox/hq design 47 §381, §478, §504): it asks the
|
||||
// seat mesh-builds' verb reserved with the publish's name and version, a package's name in its ecosystem, and accepts the publish
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest. It reads no catalog and a publish gains
|
||||
// only when that version is the one reserved and an artifact of it has the publish's digest or none yet (A-490j). It reads no catalog and a publish gains
|
||||
// no field. It is the Builder the holder's Check asks.
|
||||
type Reserved struct {
|
||||
// Ask is the runtime's request and reply over the bus (mesh-sdk stdio.Ask).
|
||||
@@ -34,7 +34,10 @@ func (r Reserved) Recorded(name, version, digest string) (bool, string, error) {
|
||||
return false, fmt.Sprintf("%s reserved %s for %s, not %s", a.Module, a.Version, name, version), nil
|
||||
}
|
||||
for _, art := range a.Artifacts {
|
||||
if art.Digest == digest {
|
||||
// An artifact whose build has not recorded its digest yet takes the publish: the builder records a build's digests only when
|
||||
// the build ends, while the build module publishes during it (design 47 G §369, A-415, A-490j). Its record then meets what
|
||||
// the holder holds: the published kept on the reservation marks the artifact, and a digest that differs fails the build.
|
||||
if art.Digest == digest || art.Digest == "" {
|
||||
return true, "", nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ func reservedChecksAPublishAgainstTheReservation(t *testing.T) {
|
||||
}{
|
||||
{name: "the reserved version and digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:ts","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "another artifact of it carries the digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:go"},{"artifact":"mesh-sdk:ts","digest":"sha256:aa"}]}`, ok: true},
|
||||
{name: "the artifact is not built yet", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:ts"}]}`, ok: true},
|
||||
{name: "another digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:ts","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"},
|
||||
{name: "another version reserved", answer: `{"module":"mesh-sdk","version":"0.1.20","artifacts":[]}`, why: "reserved 0.1.20 for @novox/mesh-sdk, not 0.1.19"},
|
||||
{name: "the builder refused the name", err: errors.New("no entry carries @novox/mesh-sdk"), why: "no entry carries @novox/mesh-sdk"},
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// Package seatgit holds the whole test of the seat git as the forge module serves it (novox/hq design 47 H §774, H-A-87):
|
||||
// the module's TypeScript bundle run against a bus of the test alone and a fixture forge. It has no code of its own.
|
||||
package seatgit
|
||||
@@ -0,0 +1,483 @@
|
||||
//go:build integration
|
||||
|
||||
package seatgit
|
||||
|
||||
// The seat git whole, as the forge module serves it (novox/hq design 47 H §734 to §736, §766, §767, §774; acceptance tests
|
||||
// 111, 114 and 115; H-A-61, H-A-87): the module's TypeScript bundle, built here and played over its real stdin and stdout
|
||||
// as the node's tool runner plays it, its states key-value buckets of a nats-server of this test alone, its seat events
|
||||
// published on that bus and read back from its stream, and the forge an httptest server.
|
||||
//
|
||||
// - At its start a call kept unsaid, stopped after the second of three merges, says one merge-called naming the two
|
||||
// that landed, and is kept said (§766, test 111's stop case).
|
||||
// - A runner's step-ended of a failed required step at a pull request's head and fingerprint is kept, and a merge call
|
||||
// naming that head merges nothing and names the step (§735, test 114).
|
||||
// - A call naming two pull requests in two repositories merges both, in order, and says one merge-called (§734, §736).
|
||||
// - A forge that refuses the second merge gives one merge-called with the first member and `refused`, and merge-partial
|
||||
// raised; a later call landing the refused member clears it (§767, test 115).
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
)
|
||||
|
||||
const within = 30 * time.Second
|
||||
|
||||
// pull is one pull request of the fixture forge.
|
||||
type pull struct {
|
||||
repo, base, head string
|
||||
number int
|
||||
merged, refuse bool
|
||||
merge string
|
||||
}
|
||||
|
||||
// forge is the fixture forge: the pull requests by `<owner>/<name>#<number>` and every merge asked.
|
||||
type forge struct {
|
||||
mu sync.Mutex
|
||||
pulls map[string]*pull
|
||||
merges []string
|
||||
paths []string
|
||||
}
|
||||
|
||||
func (f *forge) serve(w http.ResponseWriter, r *http.Request) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.paths = append(f.paths, r.Method+" "+r.URL.Path)
|
||||
m := regexp.MustCompile(`^/api/v1/repos/([^/]+/[^/]+)/pulls/(\d+)(/merge)?$`).FindStringSubmatch(r.URL.Path)
|
||||
if m != nil {
|
||||
p := f.pulls[m[1]+"#"+m[2]]
|
||||
if p == nil {
|
||||
http.Error(w, `{"message":"not found"}`, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if m[3] != "" && r.Method == http.MethodPost {
|
||||
f.merges = append(f.merges, m[1]+"#"+m[2])
|
||||
if p.refuse {
|
||||
http.Error(w, `{"message":"merge conflict"}`, http.StatusConflict)
|
||||
return
|
||||
}
|
||||
p.merged, p.merge = true, strings.Repeat(fmt.Sprint(p.number%10), 40)
|
||||
return
|
||||
}
|
||||
body := map[string]any{"number": p.number, "title": "t", "state": "open", "merged": p.merged,
|
||||
"base": map[string]any{"ref": p.base}, "head": map[string]any{"ref": "feat", "sha": p.head}}
|
||||
if p.merged {
|
||||
body["state"], body["merge_commit_sha"] = "closed", p.merge
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
return
|
||||
}
|
||||
if c := regexp.MustCompile(`^/api/v1/repos/[^/]+/[^/]+/git/commits/([0-9a-f]+)$`).FindStringSubmatch(r.URL.Path); c != nil {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"sha": c[1], "parents": []any{map[string]any{"sha": c[1]}}})
|
||||
return
|
||||
}
|
||||
http.Error(w, `{"message":"the fixture forge serves no `+r.URL.Path+`"}`, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func (f *forge) merged() []string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return append([]string(nil), f.merges...)
|
||||
}
|
||||
|
||||
func (f *forge) asked(prefix string) bool {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for _, p := range f.paths {
|
||||
if strings.HasPrefix(p, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// runtime is the half of the conversation the tool runner plays for the bundle.
|
||||
type runtime struct {
|
||||
t *testing.T
|
||||
in io.WriteCloser
|
||||
js jetstream.JetStream
|
||||
kvs map[string]jetstream.KeyValue
|
||||
mu sync.Mutex
|
||||
next int
|
||||
wait map[string]chan json.RawMessage
|
||||
// regs are what the seat mesh-catalog's registrations answers; subs the patterns the bundle subscribed.
|
||||
regs []map[string]any
|
||||
subs []string
|
||||
}
|
||||
|
||||
func (r *runtime) subscribed(pattern string) bool {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for _, s := range r.subs {
|
||||
if s == pattern {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *runtime) say(m map[string]any) {
|
||||
m["jsonrpc"] = "2.0"
|
||||
raw, _ := json.Marshal(m)
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
_, _ = r.in.Write(append(raw, '\n'))
|
||||
}
|
||||
|
||||
func (r *runtime) read(out io.Reader) {
|
||||
sc := bufio.NewScanner(out)
|
||||
sc.Buffer(make([]byte, 1<<20), 1<<24)
|
||||
for sc.Scan() {
|
||||
var m struct {
|
||||
ID json.RawMessage `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params json.RawMessage `json:"params"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
}
|
||||
if json.Unmarshal(sc.Bytes(), &m) != nil {
|
||||
continue
|
||||
}
|
||||
if m.Method == "" {
|
||||
var id string
|
||||
_ = json.Unmarshal(m.ID, &id)
|
||||
r.mu.Lock()
|
||||
ch := r.wait[id]
|
||||
delete(r.wait, id)
|
||||
r.mu.Unlock()
|
||||
if ch != nil {
|
||||
ch <- m.Result
|
||||
}
|
||||
continue
|
||||
}
|
||||
go r.answer(m.ID, m.Method, m.Params)
|
||||
}
|
||||
}
|
||||
|
||||
// answer is the tool runner's answer to one method the bundle asks.
|
||||
func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessage) {
|
||||
var p struct {
|
||||
State string `json:"state"`
|
||||
Key string `json:"key"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
Subject string `json:"subject"`
|
||||
Pattern string `json:"pattern"`
|
||||
Body json.RawMessage `json:"body"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
_ = json.Unmarshal(params, &p)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
result, err := func() (any, error) {
|
||||
kv := r.kvs[p.State]
|
||||
switch method {
|
||||
case "mesh/subscribe":
|
||||
r.mu.Lock()
|
||||
r.subs = append(r.subs, p.Pattern)
|
||||
r.mu.Unlock()
|
||||
return map[string]any{}, nil
|
||||
case "mesh/publish", "mesh/state.watch", "mesh/seat.record":
|
||||
return map[string]any{}, nil
|
||||
case "mesh/capabilities":
|
||||
return map[string]any{"words": []string{}}, nil
|
||||
case "mesh/seat.publish":
|
||||
// The seat's event, published on the bus under its id, as the runtime does.
|
||||
_, err := r.js.Publish(ctx, p.Subject, p.Body, jetstream.WithMsgID(p.ID))
|
||||
return map[string]any{}, err
|
||||
case "mesh/ask":
|
||||
var a struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
_ = json.Unmarshal(params, &a)
|
||||
switch a.Key {
|
||||
case "seat:mesh-catalog.registrations":
|
||||
return map[string]any{"registrations": r.regs}, nil
|
||||
case "seat:mesh-hand-acts.hand-act":
|
||||
return map[string]any{}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("the runtime answers no ask %s in this test", a.Key)
|
||||
case "mesh/state.get":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
}
|
||||
e, err := kv.Get(ctx, p.Key)
|
||||
if errors.Is(err, jetstream.ErrKeyNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"key": p.Key, "value": json.RawMessage(e.Value()), "revision": e.Revision()}, nil
|
||||
case "mesh/state.put":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
}
|
||||
rev, err := kv.Put(ctx, p.Key, p.Value)
|
||||
return map[string]any{"revision": rev}, err
|
||||
case "mesh/state.delete":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
}
|
||||
return map[string]any{}, kv.Delete(ctx, p.Key)
|
||||
case "mesh/state.keys":
|
||||
if kv == nil {
|
||||
return nil, fmt.Errorf("the state %q is not declared", p.State)
|
||||
}
|
||||
keys, err := kv.Keys(ctx)
|
||||
if errors.Is(err, jetstream.ErrNoKeysFound) {
|
||||
return []string{}, nil
|
||||
}
|
||||
return keys, err
|
||||
case "mesh/state.info":
|
||||
return map[string]any{"bytes": 0, "keys": 0, "max_bytes": 0}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("the runtime offers no %s to this bundle", method)
|
||||
}()
|
||||
if id == nil {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
r.say(map[string]any{"id": id, "error": map[string]any{"code": -32000, "message": err.Error()}})
|
||||
return
|
||||
}
|
||||
r.say(map[string]any{"id": id, "result": result})
|
||||
}
|
||||
|
||||
// ask sends the bundle one request and answers its result.
|
||||
func (r *runtime) ask(method string, params map[string]any) json.RawMessage {
|
||||
r.t.Helper()
|
||||
r.mu.Lock()
|
||||
r.next++
|
||||
id := fmt.Sprintf("rt-%d", r.next)
|
||||
ch := make(chan json.RawMessage, 1)
|
||||
r.wait[id] = ch
|
||||
r.mu.Unlock()
|
||||
r.say(map[string]any{"id": id, "method": method, "params": params})
|
||||
select {
|
||||
case raw := <-ch:
|
||||
return raw
|
||||
case <-time.After(within):
|
||||
r.t.Fatalf("%s was not answered in %s", method, within)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mergePull calls the seat's verb as a proved caller and answers its result.
|
||||
func (r *runtime) mergePull(pulls ...map[string]any) map[string]any {
|
||||
r.t.Helper()
|
||||
raw := r.ask("tools/call", map[string]any{"name": "git.merge-pull", "_meta": map[string]any{"mesh/caller": "shanks.claude-code"},
|
||||
"arguments": map[string]any{"by": "lead@shanks", "pulls": pulls}})
|
||||
var res struct {
|
||||
Content []struct {
|
||||
Text string `json:"text"`
|
||||
} `json:"content"`
|
||||
}
|
||||
var out map[string]any
|
||||
if json.Unmarshal(raw, &res) != nil || len(res.Content) == 0 || json.Unmarshal([]byte(res.Content[0].Text), &out) != nil {
|
||||
r.t.Fatalf("merge-pull answered %s", raw)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func until(t *testing.T, what string, ok func() bool) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(within)
|
||||
for !ok() {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("within %s: %s did not happen", within, what)
|
||||
}
|
||||
select {
|
||||
case <-time.After(20 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// said is every merge-called on the bus so far, by call.
|
||||
func said(t *testing.T, js jetstream.JetStream) map[string]map[string]any {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
out := map[string]map[string]any{}
|
||||
s, err := js.Stream(ctx, "SEAT_git_merge-called")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := s.Info(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for seq := info.State.FirstSeq; seq <= info.State.LastSeq && info.State.Msgs > 0; seq++ {
|
||||
m, err := s.GetMsg(ctx, seq)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(m.Data, &body)
|
||||
out[strings.TrimPrefix(m.Subject, "mesh.seat.git.event.merge-called.")] = body
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func numbers(v any) []float64 {
|
||||
var out []float64
|
||||
list, _ := v.([]any)
|
||||
for _, m := range list {
|
||||
n, _ := m.(map[string]any)["number"].(float64)
|
||||
out = append(out, n)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestTheSeatGitWholeOverARealBus(t *testing.T) {
|
||||
dir, err := filepath.Abs(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "node_modules", "@novox", "mesh-sdk")); err != nil {
|
||||
t.Fatalf("the forge module's packages are not installed in %s (npm install first): %v", dir, err)
|
||||
}
|
||||
build := exec.Command("npm", "run", "build")
|
||||
build.Dir = dir
|
||||
if out, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("the bundle does not build: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
nc, err := nats.Connect(testbus.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(nc.Close)
|
||||
js, _ := jetstream.New(nc)
|
||||
ctx := context.Background()
|
||||
// The seat's stream merge-called is kept on (the definition's keep); the template states as buckets.
|
||||
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "SEAT_git_merge-called", Subjects: []string{"mesh.seat.git.event.merge-called.*"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kvs := map[string]jetstream.KeyValue{}
|
||||
for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls"} {
|
||||
kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "gitea_" + name})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kvs[name] = kv
|
||||
}
|
||||
A, B, X := strings.Repeat("a", 40), strings.Repeat("b", 40), strings.Repeat("e", 40)
|
||||
// A call stopped after the second of three merges, kept and not said (§766).
|
||||
stopped, _ := json.Marshal(map[string]any{"by": "lead@shanks", "account": "shanks.claude-code", "at": "2026-11-20T03:00:00Z", "said": false,
|
||||
"members": []any{
|
||||
map[string]any{"repository": "novox/app", "number": 1, "branch": "main", "head": A, "state": "merged", "commit": strings.Repeat("1", 40)},
|
||||
map[string]any{"repository": "novox/app", "number": 2, "branch": "main", "head": A, "state": "merged", "commit": strings.Repeat("2", 40)},
|
||||
map[string]any{"repository": "novox/app", "number": 3, "branch": "main", "head": A, "state": "judged"}}})
|
||||
if _, err := kvs["calls"].Put(ctx, "call.00000000000000cc", stopped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
f := &forge{pulls: map[string]*pull{
|
||||
"novox/app#5": {repo: "novox/app", base: "main", head: A, number: 5},
|
||||
"novox/app#6": {repo: "novox/app", base: "main", head: X, number: 6},
|
||||
"novox/app#7": {repo: "novox/app", base: "main", head: A, number: 7},
|
||||
"novox/lib#9": {repo: "novox/lib", base: "main", head: B, number: 9, refuse: true},
|
||||
"novox/lib#10": {repo: "novox/lib", base: "main", head: B, number: 10},
|
||||
}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
state := t.TempDir()
|
||||
launch := `const sdk = await import("@novox/mesh-sdk/stdio"); await import("./dist/index.js"); await import("./dist/tools/index.js"); await sdk.serveRegisteredOverStdio("gitea");`
|
||||
cmd := exec.Command("node", "--input-type=module", "-e", launch)
|
||||
cmd.Dir = dir
|
||||
cmd.Env = append(os.Environ(), "MESH_SERVED_MODULE=gitea", "MESH_MODULE=gitea", "MESH_NODE=test", "MESH_GITEA_URL="+srv.URL,
|
||||
"GITEA_TOKEN=t", "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json"))
|
||||
in, _ := cmd.StdinPipe()
|
||||
out, _ := cmd.StdoutPipe()
|
||||
cmd.Stderr = testLog{t}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = in.Close(); _ = cmd.Process.Kill(); _ = cmd.Wait() })
|
||||
rt := &runtime{t: t, in: in, js: js, kvs: kvs, wait: map[string]chan json.RawMessage{}, regs: []map[string]any{
|
||||
{"module": "app", "origin": "novox/app", "branches": []string{"main"}},
|
||||
{"module": "lib", "origin": "novox/lib", "branches": []string{"main"}}}}
|
||||
go rt.read(out)
|
||||
|
||||
// At the start the stopped call says one merge-called naming the two that landed (§766).
|
||||
until(t, "the stopped call's merge-called at the start", func() bool { return said(t, js)["00000000000000cc"] != nil })
|
||||
if got := numbers(said(t, js)["00000000000000cc"]["members"]); fmt.Sprint(got) != "[1 2]" {
|
||||
t.Fatalf("the stopped call names %v, not the two that landed", got)
|
||||
}
|
||||
until(t, "the stopped call kept said", func() bool {
|
||||
e, err := kvs["calls"].Get(ctx, "call.00000000000000cc")
|
||||
return err == nil && strings.Contains(string(e.Value()), `"said":true`)
|
||||
})
|
||||
// The registrations are read, and the heads with them, before a merge is judged against them.
|
||||
until(t, "the watched heads read after the registrations", func() bool { return f.asked("GET /api/v1/repos/novox/app/branches") })
|
||||
|
||||
// A failed required step at a head's fingerprint refuses that head (§735, test 114), heard on the module's consumer.
|
||||
until(t, "the runner's step-ended followed", func() bool { return rt.subscribed("mesh-pipeline-runner.step-ended.*.*.*") })
|
||||
rt.ask("mesh/event", map[string]any{"envelope": map[string]any{"key": "mesh-pipeline-runner.step-ended.app.main." + X, "node": "test",
|
||||
"body": map[string]any{"v": 1, "pipeline": "main.4", "module": "app", "branch": "main", "commit": X, "fingerprint": "fp-x",
|
||||
"at": "2026-11-20T03:00:00Z", "result": map[string]any{"step": "unit", "status": "required", "outcome": "failed"}}}})
|
||||
refused := rt.mergePull(map[string]any{"repository": "novox/app", "number": 6, "head": X})
|
||||
members, _ := refused["members"].([]any)
|
||||
if len(members) != 1 || members[0].(map[string]any)["state"] != "refused" ||
|
||||
!strings.Contains(fmt.Sprint(members[0].(map[string]any)["why"]), "the unit step of app failed in run main.4") {
|
||||
t.Fatalf("a head at a failed required step's fingerprint answered %v", refused)
|
||||
}
|
||||
if got := f.merged(); len(got) != 0 {
|
||||
t.Fatalf("merged %v", got)
|
||||
}
|
||||
|
||||
// Two pull requests in two repositories merge, in order, and say one merge-called (§734, §736).
|
||||
both := rt.mergePull(map[string]any{"repository": "novox/app", "number": 7, "head": A}, map[string]any{"repository": "novox/lib", "number": 10, "head": B})
|
||||
call, _ := both["call"].(string)
|
||||
until(t, "the merge-called of the two", func() bool { return said(t, js)[call] != nil })
|
||||
if got := numbers(said(t, js)[call]["members"]); fmt.Sprint(got) != "[7 10]" || fmt.Sprint(f.merged()) != "[novox/app#7 novox/lib#10]" {
|
||||
t.Fatalf("the call of two said %v, merged %v", got, f.merged())
|
||||
}
|
||||
|
||||
// The forge refuses the second merge: merge-called with the first and refused, merge-partial raised (§767, test 115).
|
||||
partial := rt.mergePull(map[string]any{"repository": "novox/app", "number": 5, "head": A}, map[string]any{"repository": "novox/lib", "number": 9, "head": B})
|
||||
pc, _ := partial["call"].(string)
|
||||
until(t, "the partial call's merge-called", func() bool { return said(t, js)[pc] != nil })
|
||||
body := said(t, js)[pc]
|
||||
if fmt.Sprint(numbers(body["members"])) != "[5]" || fmt.Sprint(numbers(body["refused"])) != "[9]" {
|
||||
t.Fatalf("the partial call said %v", body)
|
||||
}
|
||||
key := "delivery." + pc + ".merge-partial"
|
||||
if e, err := kvs["findings"].Get(ctx, key); err != nil || !strings.Contains(string(e.Value()), `"severity":"urgent"`) {
|
||||
t.Fatalf("merge-partial %s is not raised: %v", key, err)
|
||||
}
|
||||
// A later call landing the refused member clears it.
|
||||
f.mu.Lock()
|
||||
f.pulls["novox/lib#9"].refuse = false
|
||||
f.mu.Unlock()
|
||||
rt.mergePull(map[string]any{"repository": "novox/lib", "number": 9, "head": B})
|
||||
until(t, "merge-partial cleared by the later call", func() bool {
|
||||
_, err := kvs["findings"].Get(ctx, key)
|
||||
return errors.Is(err, jetstream.ErrKeyNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
type testLog struct{ t *testing.T }
|
||||
|
||||
func (w testLog) Write(p []byte) (int, error) {
|
||||
defer func() { _ = recover() }()
|
||||
w.t.Logf("bundle: %s", strings.TrimRight(string(p), "\n"))
|
||||
return len(p), nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
// checks/readable every state another manifest reads to a readable announcement.
|
||||
export const KEPT: readonly { readonly name: string; readonly readable?: boolean }[] = [
|
||||
{ name: "announced" },
|
||||
{ name: "calls" },
|
||||
{ name: "bound", readable: true },
|
||||
{ name: "findings", readable: true },
|
||||
{ name: "failed-steps" },
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
// False, the default, leaves every repository's branch protection as people set it at the forge.
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
export const LEFT_AS_SET = "[gitea] main's rules are left as set at the forge (keep-main-rules is false)";
|
||||
|
||||
/** The setting from the settings-merged config file the mesh delivers; absent, unreadable or not true is false. */
|
||||
export function keepMainRules(env: NodeJS.ProcessEnv = process.env): boolean {
|
||||
const file = env.MESH_GITEA_CONFIG_FILE;
|
||||
@@ -14,19 +12,3 @@ export function keepMainRules(env: NodeJS.ProcessEnv = process.env): boolean {
|
||||
return v === true || v === "true";
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
/** The 10-minute step: puts the rules right only when kept; else says once that they are left and writes nothing. */
|
||||
export function mainRulesStep(keep: boolean, put: () => Promise<string[]>, say: (line: string) => void): () => Promise<void> {
|
||||
let said = false;
|
||||
let touched = -1;
|
||||
return async () => {
|
||||
if (!keep) {
|
||||
if (!said) say(LEFT_AS_SET);
|
||||
said = true;
|
||||
return;
|
||||
}
|
||||
const repos = await put();
|
||||
if (repos.length !== touched) say(`[gitea] main's rule is right in ${repos.length} repositories`);
|
||||
touched = repos.length;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
// **One merge call is a delivery's start and its group** (novox/hq design 47 H §734, §735; ADR 0682 §1, §2, §13; H-A-21,
|
||||
// H-A-22, H-A-47): the seat git's `merge-pull` takes `pulls`, one or more members `{repository, number, head}`, and `by`,
|
||||
// the caller as who@node, recorded beside the bus account the tool runner proved for the call. Every member is judged
|
||||
// before any is merged: still the pull request's head, its base a watched branch, not merged already (refused
|
||||
// `already-merged` with its commit), and no run at the head's build-input fingerprint failed a `required` step (the
|
||||
// module's own `failed-steps`). No branch protection is judged (ADR 0682 §13). When one member is refused, none is merged
|
||||
// and the answer names every refused member and why; else the members are merged in the order the call names them. The
|
||||
// verb answers once, at the call's end: `call` and each member's outcome.
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { UNPROVEN, callerOf } from "@novox/mesh-sdk/callers";
|
||||
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, sayCall, type Calls, type Say } from "./calls.js";
|
||||
import type { Partials } from "./partials.js";
|
||||
|
||||
/** What a merge call reaches beside the forge's API. */
|
||||
export interface CallDeps {
|
||||
/** Whether the first registrations were written: until then no merge is made (design 47 §95). */
|
||||
registered: () => boolean;
|
||||
/** The watched branches the registrations name, by their origin repository `<owner>/<name>`. */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>>;
|
||||
/** The failed `required` step that refuses a head, or null (failedsteps.ts). */
|
||||
judge: (head: string) => Promise<Judged | null>;
|
||||
/** A new call's id: 16 lowercase hex characters (H-A-24). */
|
||||
newCall?: () => string;
|
||||
/** 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 findings merge-partial (partials.ts; §767). */
|
||||
partials?: Partials;
|
||||
}
|
||||
|
||||
/** The forge's API a merge call uses. */
|
||||
export type CallForge = Pick<GiteaClient, "getPullRequest" | "mergePullRequest" | "commitParents">;
|
||||
|
||||
/** One member as the call names it, and as it was judged. */
|
||||
export interface Member {
|
||||
repository: string;
|
||||
owner: string;
|
||||
repo: string;
|
||||
number: number;
|
||||
head: string;
|
||||
/** The pull request's base, read at judging. */
|
||||
branch: string;
|
||||
state: MemberOutcome["state"];
|
||||
commit?: string;
|
||||
why?: string;
|
||||
}
|
||||
|
||||
const SHA = /^[0-9a-f]{40}$/;
|
||||
export const CALL = /^[0-9a-f]{16}$/;
|
||||
|
||||
/** A new call's id. */
|
||||
export const newCallId = (): string => randomBytes(8).toString("hex");
|
||||
|
||||
/** The members a call names, each checked, or the reason the call is refused whole. */
|
||||
export function membersOf(raw: unknown): Member[] {
|
||||
if (!Array.isArray(raw) || raw.length === 0) throw new Refused("pulls names one or more members {repository, number, head}");
|
||||
const seen = new Set<string>();
|
||||
return raw.map((p: any) => {
|
||||
const parts = String(p?.repository ?? "").split("/");
|
||||
if (parts.length !== 2) throw new Refused(`repository ${JSON.stringify(p?.repository)} is not <owner>/<name>`);
|
||||
const owner = checkName("owner", parts[0]), repo = checkName("name", parts[1]);
|
||||
const number = Number(p?.number);
|
||||
if (!Number.isInteger(number) || number < 1) throw new Refused(`${JSON.stringify(p?.number)} is not a pull request's number`);
|
||||
const head = String(p?.head ?? "").trim();
|
||||
if (!SHA.test(head)) throw new Refused("head is a commit's full 40-hex sha");
|
||||
const repository = `${owner}/${repo}`;
|
||||
if (seen.has(`${repository}#${number}`)) throw new Refused(`${repository}#${number} is named twice`);
|
||||
seen.add(`${repository}#${number}`);
|
||||
return { repository, owner, repo, number, head, branch: "", state: "judged" as const };
|
||||
});
|
||||
}
|
||||
|
||||
/** Why a member is refused at judging, or "" when it is judged. */
|
||||
async function judgeMember(forge: CallForge, deps: CallDeps, m: Member): Promise<string> {
|
||||
const pull = await forge.getPullRequest(m.owner, m.repo, m.number);
|
||||
m.branch = String(pull.base ?? "");
|
||||
if (pull.merged) {
|
||||
if (pull.merge_commit_sha) m.commit = pull.merge_commit_sha;
|
||||
return `already-merged: ${m.repository}#${m.number} was merged${pull.merge_commit_sha ? ` as ${pull.merge_commit_sha}` : ""}`;
|
||||
}
|
||||
if (pull.state !== "open") return `${m.repository}#${m.number} is closed`;
|
||||
if (pull.head_sha !== m.head) {
|
||||
return `head-moved: its head is ${String(pull.head_sha).slice(0, 12)}, not the ${m.head.slice(0, 12)} the caller names`;
|
||||
}
|
||||
if (!deps.watched().get(m.repository)?.has(m.branch)) return `its base ${m.branch} is no watched branch`;
|
||||
const failed = await deps.judge(m.head);
|
||||
if (failed) {
|
||||
return `the ${failed.step} step of ${failed.module} failed in run ${failed.run} at its build-input fingerprint ${failed.fingerprint}`;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/** One member merged at its head, its merge commit read back; throws when the forge refused or did not name it. */
|
||||
async function mergeMember(forge: CallForge, m: Member): Promise<string> {
|
||||
await forge.mergePullRequest(m.owner, m.repo, m.number, "merge", true, m.head);
|
||||
const merged = await forge.getPullRequest(m.owner, m.repo, m.number);
|
||||
if (!merged.merge_commit_sha) throw new Error(`${m.repository}#${m.number} WAS merged, but the forge named no merge commit`);
|
||||
// The merge is done once the forge named its commit: a read-back of its parents that fails is said, never a failed act.
|
||||
try {
|
||||
const parents = await forge.commitParents(m.owner, m.repo, merged.merge_commit_sha);
|
||||
if (parents.length === 2 && parents[1] !== m.head) {
|
||||
console.error(`[gitea] ${m.repository}#${m.number} WAS merged (${merged.merge_commit_sha}), but the forge merged ` +
|
||||
`${parents[1].slice(0, 12)}, not the ${m.head.slice(0, 12)} the caller names (novox/hq issue 381)`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${m.repository}#${m.number} was merged (${merged.merge_commit_sha}); which head it joined could not be read back — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
return merged.merge_commit_sha;
|
||||
}
|
||||
|
||||
/** A member's outcome as the answer says it. */
|
||||
export const outcomeOf = (m: Member): MemberOutcome => ({ repository: m.repository, number: m.number, state: m.state,
|
||||
...(m.commit ? { commit: m.commit } : {}), ...(m.why ? { why: m.why } : {}) });
|
||||
|
||||
/** merge-pull: one merge call (design 47 H §734, §735). */
|
||||
export async function mergeCall(forge: CallForge, args: Record<string, unknown>, deps: CallDeps): Promise<MergePullAnswer> {
|
||||
const by = String(args.by ?? "").trim();
|
||||
if (!/^[^\s@]+@[^\s@]+$/.test(by)) throw new Refused("by is the caller as who@node");
|
||||
const account = callerOf(args);
|
||||
if (account === UNPROVEN) throw new Refused("the tool runner proved no caller account for this call, so nothing is merged");
|
||||
const members = membersOf(args.pulls);
|
||||
// Until the first registrations are written no merge is made (design 47 §95).
|
||||
if (!deps.registered()) throw new Refused("no module is registered yet: the catalog has said no registration-changed, so no merge is made until the first registrations are written");
|
||||
const call = (deps.newCall ?? newCallId)();
|
||||
if (!CALL.test(call)) throw new Error(`the call's id ${JSON.stringify(call)} is not 16 lowercase hex characters`);
|
||||
// Every member is judged before any is merged (§735).
|
||||
let refused = false;
|
||||
for (const m of members) {
|
||||
const why = await judgeMember(forge, deps, m);
|
||||
if (why) {
|
||||
m.state = "refused";
|
||||
m.why = why;
|
||||
refused = true;
|
||||
}
|
||||
}
|
||||
const now = deps.now ?? (() => new Date());
|
||||
// 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) };
|
||||
}
|
||||
// Written before the first merge, and again as each member merges.
|
||||
await keep(false);
|
||||
// Then merged in the order the call names them; a member the forge refuses stops the call (§767).
|
||||
for (const m of members) {
|
||||
try {
|
||||
m.commit = await mergeMember(forge, m);
|
||||
m.state = "merged";
|
||||
at = now().toISOString();
|
||||
await keep(false);
|
||||
} catch (err) {
|
||||
m.state = "refused";
|
||||
m.why = `the forge refused the merge: ${err instanceof Error ? err.message : String(err)}`;
|
||||
for (const rest of members.slice(members.indexOf(m) + 1)) {
|
||||
rest.state = "refused";
|
||||
rest.why = `not merged: ${m.repository}#${m.number} before it was refused by the forge`;
|
||||
}
|
||||
await keep(false);
|
||||
// Raised for the refused members, the landed ones delivered as any call's (§767).
|
||||
await deps.partials?.raise({ ...value(false), call });
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
// A call that landed members clears every merge-partial whose refused members are now all landed (§767).
|
||||
if (members.some((m) => m.state === "merged")) {
|
||||
await deps.partials?.landed().catch((err) => console.error(`[gitea] the merge-partial findings could not be judged after the call ${call} — ${err instanceof Error ? err.message : err}`));
|
||||
}
|
||||
return { call, members: members.map(outcomeOf) };
|
||||
}
|
||||
@@ -211,9 +211,16 @@
|
||||
"trusted": true,
|
||||
"default": {}
|
||||
},
|
||||
"call-retention": {
|
||||
"type": "number",
|
||||
"description": "How many days the forge module keeps a merge call in its state calls (design 47 H §766): a call older than this is deleted when a new call is written, oldest first.",
|
||||
"trusted": false,
|
||||
"minimum": 1,
|
||||
"default": 30
|
||||
},
|
||||
"keep-main-rules": {
|
||||
"type": "boolean",
|
||||
"description": "Whether the module keeps main's branch protection as the mesh wants it. True: every ten minutes, and at the start, main's rule in every repository of the organisation novox is put to no push and the tools' account alone merging, and each apply of the `repositories` value narrows the main rule of the repositories it lists, requiring no status. False (the default): the module changes no existing repository's branch protection, whoever set it at the forge, but takes away a required status it finds on a rule covering main, once (ADR 0534 §1); a repository listed in `repositories` that does not exist is still made, with its first protection.",
|
||||
"description": "Whether the module keeps main's branch protection as the mesh wants it. True: when the tools' account's token is minted, main's rule in every repository of the organisation novox is put to no push and the tools' account alone merging, and each apply of the `repositories` value narrows the main rule of the repositories it lists, requiring no status. False (the default): the module changes no existing repository's branch protection, whoever set it at the forge, but takes away a required status it finds on a rule covering main, once (ADR 0534 §1); a repository listed in `repositories` that does not exist is still made, with its first protection.",
|
||||
"trusted": true,
|
||||
"default": false
|
||||
},
|
||||
@@ -358,7 +365,8 @@
|
||||
"takes-config": [
|
||||
"url",
|
||||
"keep-main-rules",
|
||||
"main-mergers"
|
||||
"main-mergers",
|
||||
"call-retention"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -623,6 +631,10 @@
|
||||
{
|
||||
"name": "failed-steps",
|
||||
"history": 1
|
||||
},
|
||||
{
|
||||
"name": "calls",
|
||||
"history": 1
|
||||
}
|
||||
],
|
||||
"reads": [
|
||||
@@ -642,7 +654,7 @@
|
||||
"consumes": [
|
||||
"mesh-catalog.catalog-revision",
|
||||
"mesh-catalog.registration-changed",
|
||||
"mesh-pipeline-runner.step-ended",
|
||||
"mesh-pipeline-runner.step-ended.*.*.*",
|
||||
"git.branch-moved",
|
||||
"git.pull-updated",
|
||||
"git.pull-closed",
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// **A member refused after every member was judged is said and raised** (novox/hq design 47 H §767; H-A-1, H-A-18,
|
||||
// H-A-81): when the forge refuses a member after all were judged, the call stops merging and the forge module raises the
|
||||
// finding `merge-partial`, scope `delivery`, id the call, naming each refused member, severity urgent, resolver operator,
|
||||
// in its own state `findings`, which the condition keeper reads. It clears it when a later call lands every refused member
|
||||
// (read from its own `calls`), or by its tool `forge_partial_clear` with `call` and `why`, written in the hand-act log.
|
||||
|
||||
import type { State } from "@novox/mesh-sdk/state";
|
||||
import { findingKey } from "./catalog.js";
|
||||
import type { Calls, KeptCall } from "./calls.js";
|
||||
|
||||
export const MERGE_PARTIAL = "merge-partial";
|
||||
const SCOPE = "delivery";
|
||||
|
||||
/** The key of a call's merge-partial in the state findings. */
|
||||
export const partialKey = (call: string): string => findingKey(SCOPE, call, MERGE_PARTIAL);
|
||||
|
||||
/** A member's name in a finding, `<repository>#<number>`. */
|
||||
const named = (m: { repository: string; number: number }) => `${m.repository}#${m.number}`;
|
||||
|
||||
/** The merge-partial of a call the forge refused a member of after judging, as the condition keeper reads it. */
|
||||
export function partialValue(c: KeptCall, observer = "gitea"): { key: string; value: Record<string, unknown> } {
|
||||
const key = partialKey(c.call);
|
||||
const landed = c.members.filter((m) => m.state === "merged").map(named);
|
||||
const refused = c.members.filter((m) => m.state === "refused");
|
||||
const summary = `the merge call ${c.call} by ${c.by} landed ${landed.length ? landed.join(", ") : "none"} and not ` +
|
||||
refused.map((m) => `${named(m)} (${m.why ?? "refused"})`).join(", ");
|
||||
return { key, value: {
|
||||
v: 1, key, kind: MERGE_PARTIAL, subject: { scope: SCOPE, id: c.call }, severity: "urgent", resolver: "operator", summary,
|
||||
headline: `A merge call landed in part: ${refused.map(named).join(", ")} not merged`,
|
||||
explanation: `The forge refused ${refused.map(named).join(", ")} after every member of the merge call ${c.call} was judged, so ` +
|
||||
`the repositories stand half-merged. A later merge call that lands them clears this, or forge_partial_clear with why.`,
|
||||
resolved: "", evidence: [{ at: c.at, said: summary }], source: `${observer}.${MERGE_PARTIAL}`, observer, at: c.at,
|
||||
} };
|
||||
}
|
||||
|
||||
export class Partials {
|
||||
constructor(private readonly findings: State<unknown>, private readonly calls: Calls,
|
||||
private readonly log: (line: string) => void = () => {}) {}
|
||||
|
||||
/** Raises merge-partial for a call the forge refused a member of after judging. */
|
||||
async raise(c: KeptCall): Promise<void> {
|
||||
const f = partialValue(c);
|
||||
await this.findings.put(f.key, f.value);
|
||||
this.log(`merge-partial raised: ${String(f.value.summary)}`);
|
||||
}
|
||||
|
||||
/** The calls whose merge-partial is held, newest first. */
|
||||
async held(): Promise<KeptCall[]> {
|
||||
const out: KeptCall[] = [];
|
||||
for (const c of await this.calls.all()) if ((await this.findings.get(partialKey(c.call)))?.value) out.push(c);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** After a call landed members: every held merge-partial whose refused members a later call landed, each, is cleared.
|
||||
* Answers the calls cleared. */
|
||||
async landed(): Promise<string[]> {
|
||||
const all = await this.calls.all(); // newest first
|
||||
const cleared: string[] = [];
|
||||
for (const [i, c] of all.entries()) {
|
||||
if (!(await this.findings.get(partialKey(c.call)))?.value) continue;
|
||||
const later = new Set(all.slice(0, i).flatMap((x) => x.members.filter((m) => m.state === "merged").map(named)));
|
||||
const refused = c.members.filter((m) => m.state === "refused").map(named);
|
||||
if (refused.every((r) => later.has(r))) {
|
||||
await this.findings.delete(partialKey(c.call));
|
||||
cleared.push(c.call);
|
||||
this.log(`merge-partial of ${c.call} cleared: a later call landed ${refused.join(", ")}`);
|
||||
}
|
||||
}
|
||||
return cleared;
|
||||
}
|
||||
|
||||
/** Clears one call's merge-partial by hand (forge_partial_clear); answers whether one was held. */
|
||||
async clear(call: string): Promise<boolean> {
|
||||
const held = !!(await this.findings.get(partialKey(call)))?.value;
|
||||
if (held) await this.findings.delete(partialKey(call));
|
||||
return held;
|
||||
}
|
||||
}
|
||||
+2
-92
@@ -1,15 +1,5 @@
|
||||
// A pull request's merge check (novox/hq to-be 45 §9): what the forge's announcer says when a pull
|
||||
// request's head moves, and the status contexts the check is kept under.
|
||||
//
|
||||
// **Before merge, never after.** Each new head of an open pull request is announced as `pull.updated`, and the mesh
|
||||
// checks it against every machine of its facts before it merges. The verdict is the checker's `mesh/check`, which this
|
||||
// module writes from the checker's owed state (check.ts); the controller's `checked`, which once set the two older
|
||||
// statuses, is no event any more (ADR 0571 clause 13).
|
||||
//
|
||||
// Pure functions here, so they are tested without a forge.
|
||||
|
||||
import type { GiteaPull, GiteaRepo } from "./client.js";
|
||||
import { movedSince } from "./client.js";
|
||||
// The status contexts the forge's protections named for a merge check, read by the account's writes (writes.ts) to take
|
||||
// them off a protection: no merge check is asked, weighed or written any more (design 47 H §764; H-A-28, H-A-100).
|
||||
|
||||
/** The status context a merge check's gate is kept under: one per commit, the newest replacing the last. */
|
||||
export const CHECK_CONTEXT = "mesh/merge-gate";
|
||||
@@ -20,83 +10,3 @@ export const REPO_CHECK_CONTEXT = "mesh/repo-check";
|
||||
/** The one status main requires (novox/hq ADR 0447 §1, issue 1301): the checker's verdict, written by this module from the
|
||||
* checker's owed state (check.ts, ADR 0521). Its line names the head and the trunk commit it judged: "pass <head12> on <trunk12> …". */
|
||||
export const CHECK_STATUS = "mesh/check";
|
||||
|
||||
/** The statuses a branch's rule requires, from the rule the forge applies to it: none when it is unprotected or checks
|
||||
* no status; undefined when the rule could not be read. */
|
||||
export function requiredOf(rule: { enable_status_check?: boolean; status_check_contexts?: string[] | null } | null | undefined): string[] | undefined {
|
||||
if (rule === undefined) return undefined;
|
||||
if (!rule || !rule.enable_status_check) return [];
|
||||
return (rule.status_check_contexts ?? []).filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* **The controller's statuses are written only where a base still requires them** (issue 1301; ADR 0503 §3, §5): of
|
||||
* mesh/merge-gate and mesh/repo-check, those the base requires — hq's mesh/repo-check alone once the forge requires
|
||||
* mesh/check everywhere else, so no mesh/merge-gate stands on a head that nothing will set. A base whose rule could not
|
||||
* be read keeps both, as before: a status nothing requires blocks nothing, and one that is required is never left out.
|
||||
*/
|
||||
export function maintained(required: string[] | undefined): string[] {
|
||||
const both = [CHECK_CONTEXT, REPO_CHECK_CONTEXT];
|
||||
return required === undefined ? both : both.filter((c) => required.includes(c));
|
||||
}
|
||||
|
||||
/** Whether mesh/merge-gate is written on a head of this base (maintained). */
|
||||
export function writesGate(required: string[] | undefined): boolean {
|
||||
return maintained(required).includes(CHECK_CONTEXT);
|
||||
}
|
||||
|
||||
/**
|
||||
* The pending words a head's merge check is announced with (novox/hq issue 446): where the check is, and since when,
|
||||
* in the time the operator reads. They stood as "the mesh is mapping this head onto its module graph" for the whole
|
||||
* check — the few seconds the controller maps the head and then the queue wait and the build — and readers took the
|
||||
* forge for lagging (issue 423). The controller maps and queues the check in seconds, so from the announcement it waits
|
||||
* in the build queue; mesh-delivery says "building on <machine> since …" when a build machine takes it.
|
||||
*/
|
||||
export function pendingWords(announced: Date): string {
|
||||
const two = (n: number) => String(n).padStart(2, "0");
|
||||
return `waiting in the build queue since ${two(announced.getHours())}:${two(announced.getMinutes())}`;
|
||||
}
|
||||
|
||||
/** The heads already announced, keyed by `headKey`, so a restart announces nothing twice. */
|
||||
export type Announced = Record<string, string>;
|
||||
|
||||
/** The key a pull request's announced head is kept under: `owner/repo#number@base`. The base is in it because a pull
|
||||
* request retargeted keeps its head, and a head judged into one base is not judged into another (issue 1422, de-spiegel #6). */
|
||||
export function headKey(full: string, number: number, base?: string): string {
|
||||
return `${full}#${number}@${base ?? ""}`;
|
||||
}
|
||||
|
||||
/** Which open pull requests have a head not yet announced. A pull request merged or closed is not
|
||||
* open and is never asked about. */
|
||||
export function headsToAnnounce(full: string, pulls: GiteaPull[], announced: Announced): GiteaPull[] {
|
||||
return pulls.filter((p) => p.state === "open" && !!p.head_sha && announced[headKey(full, p.number, p.base)] !== p.head_sha);
|
||||
}
|
||||
|
||||
/** A pull request the forge found updated, by its repository: what `searchOpenPulls` answers. */
|
||||
export interface PullSeen {
|
||||
owner: string;
|
||||
name: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
/** **The repositories a pass looks at** (novox/hq issue 347): every one that moved since the floor, every one
|
||||
* holding a pull request updated since the floor — opening, reopening or retargeting a pull request moves its own
|
||||
* update time and not its repository's — and every one on a sweep or the first pass. */
|
||||
export function reposToLook(repos: GiteaRepo[], floor: string, pulls: PullSeen[], sweep: boolean): GiteaRepo[] {
|
||||
if (sweep || !floor) return repos;
|
||||
const moved = new Set(movedSince(repos, floor).map((r) => r.full_name));
|
||||
const at = Date.parse(floor);
|
||||
for (const p of pulls) {
|
||||
if (!p.updated_at || !(Date.parse(p.updated_at) < at)) moved.add(`${p.owner}/${p.name}`);
|
||||
}
|
||||
return repos.filter((r) => moved.has(r.full_name));
|
||||
}
|
||||
|
||||
export function clipped(d: string): string {
|
||||
d = d.replace(/\s+/g, " ").trim();
|
||||
return d.length > 140 ? d.slice(0, 139) + "…" : d;
|
||||
}
|
||||
|
||||
/** What a group member's gate says while the group's composed check is the one that judges it: the member's own check can say
|
||||
* nothing more of it, so asking that check again brings no verdict that names a trunk (novox/hq issue 1106). */
|
||||
export const WAITS_FOR_COMPOSED = "waits for its group's composed check";
|
||||
|
||||
+22
-80
@@ -9,9 +9,9 @@
|
||||
// 4xx, or a refusal of this holder) the kind `refused`, a forge that failed or did not answer the kind `failed`; an empty list or an absent file is answered
|
||||
// only when the forge said so.
|
||||
//
|
||||
// **The acts carry the caller's id** (status, comment, view, note, merge-pull): an id the pull request or the note already
|
||||
// holds is answered `held` and nothing is done again; `merge-pull` merges the head the caller names, only into a base
|
||||
// whose protection refuses a push, and refuses a head at whose fingerprint a run failed a `required` step (design 47 §39, §314).
|
||||
// **The acts carry the caller's id** (status, comment, view, note): an id the pull request or the note already holds is
|
||||
// answered `held` and nothing is done again. `merge-pull` is one merge call of one or more pull requests, every member
|
||||
// judged before any is merged, no branch protection judged (mergecall.ts; design 47 H §734, §735; ADR 0682 §13).
|
||||
|
||||
import type { ToolDefinition } from "@novox/mesh-sdk/tools";
|
||||
import {
|
||||
@@ -26,6 +26,9 @@ import { containedFrom, newestByContext } from "./checks.js";
|
||||
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, Say } from "./calls.js";
|
||||
import type { Partials } from "./partials.js";
|
||||
|
||||
/** The verbs of the seat, in its definition's order. */
|
||||
export const VERBS: readonly string[] = Object.values(GIT_VERBS);
|
||||
@@ -37,22 +40,23 @@ export const TAGS_PAGE = 50;
|
||||
* is the manifest's container's; no variable names it (a MESH_* word nothing sets is refused, checks/environment). */
|
||||
export const FORGE_CONTAINER = "gitea";
|
||||
|
||||
/** The writer every status of a head takes its turn on (gate.ts): the seat's status lands after every write of the head
|
||||
* already asked for, so a recheck's pending never lands over a later verdict (novox/hq issue 456). */
|
||||
export interface HeadStatuses {
|
||||
status(owner: string, repo: string, sha: string, status: CommitStatus): Promise<void>;
|
||||
}
|
||||
|
||||
/** What the holder reaches beside the forge's API: git in the forge's container (the note), the head's one writer of its
|
||||
* statuses, and whether the first registrations are written. */
|
||||
/** What the holder reaches beside the forge's API: git in the forge's container (the note), and whether the first
|
||||
* registrations are written. */
|
||||
export interface SeatDeps {
|
||||
run: Runner;
|
||||
container: string;
|
||||
statuses: HeadStatuses;
|
||||
/** Whether the first registrations were written (registrations.ts): until then merge-pull refuses every merge (design 47 §95). */
|
||||
registered: () => boolean;
|
||||
/** The failed `required` step that refuses a head, judged from the module's own state `failed-steps` (failedsteps.ts), or null. */
|
||||
judge: (head: string) => Promise<Judged | null>;
|
||||
/** The watched branches the registrations name, by their origin repository (registrations.ts): a member's base is one. */
|
||||
watched: () => ReadonlyMap<string, ReadonlySet<string>>;
|
||||
/** 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;
|
||||
/** The findings merge-partial (partials.ts; design 47 H §767). */
|
||||
partials?: Partials;
|
||||
}
|
||||
|
||||
/** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */
|
||||
@@ -149,7 +153,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
const pull = pullOf(full, p);
|
||||
return pull.state === "open" ? { pull, merging: await mergingOf(gitea, owner, repo, p) } : { pull };
|
||||
}),
|
||||
verb(GIT_VERBS.branch, "The seat git's branch: a branch's head, and whether a protection rule refuses a direct push and admits only merges.",
|
||||
verb(GIT_VERBS.branch, "The seat git's branch: a branch's head.",
|
||||
async (args): Promise<BranchAnswer> => {
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
// No branch named is the repository's default branch, read from the forge, as the verb file reads it (A-399).
|
||||
@@ -158,7 +162,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (branch === "") throw new Error(`the forge names no default branch of ${full}`);
|
||||
// The head first: a branch the forge does not hold is its 404, the kind not-found, never an unprotected branch.
|
||||
const head = await gitea.branchHead(owner, repo, branch);
|
||||
return { repository: full, branch, head, protected: pushRefused(await gitea.appliedProtection(owner, repo, branch)) };
|
||||
return { repository: full, branch, head };
|
||||
}),
|
||||
verb(GIT_VERBS.openPulls, "The seat git's open-pulls: every open pull request of every repository, with the statuses its base requires and those its head has.",
|
||||
async (): Promise<OpenPullsAnswer> => {
|
||||
@@ -218,14 +222,14 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
const combined = await gitea.combinedStatus(owner, repo, commit);
|
||||
return { commit, combined: combined.state, statuses: newestByContext(combined.statuses).map(statusOf) };
|
||||
}),
|
||||
verb(GIT_VERBS.status, "The seat git's status: an act of the forge bridge, a commit status set in its turn after every write of the head already asked for; the same id said again sets it again to the same.",
|
||||
verb(GIT_VERBS.status, "The seat git's status: an act of the forge bridge, a commit status set on a commit; the same id said again sets it again to the same.",
|
||||
async (args): Promise<StatusAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const state = String(args.state ?? "");
|
||||
if (!STATES.has(state)) throw new Refused(`state ${JSON.stringify(state)} is none of ${[...STATES].join(", ")}`);
|
||||
await deps.statuses.status(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""),
|
||||
await gitea.setCommitStatus(owner, repo, commit, { state: state as CommitStatus["state"], context: String(args.context ?? ""),
|
||||
description: String(args.description ?? ""), ...(args.target ? { target_url: String(args.target) } : {}) });
|
||||
return { id, performed: true };
|
||||
}),
|
||||
@@ -258,8 +262,8 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
const said = await appendNote(deps.run, deps.container, owner, repo, commit, "mesh-plan", `${String(args.line ?? "")} [${id}]`, id);
|
||||
return said.added ? { id, performed: true } : { id, held: true, performed: true };
|
||||
}),
|
||||
verb(GIT_VERBS.mergePull, "The seat git's merge-pull: an act of the forge bridge, a pull request merged at the head the caller names; it refuses a head at whose fingerprint a required step failed.",
|
||||
async (args): Promise<MergePullAnswer> => mergePull(gitea, args, deps.registered, deps.judge)),
|
||||
verb(GIT_VERBS.mergePull, "The seat git's merge-pull: one merge call, a delivery's start and its group: the pull requests named (pulls, each {repository, number, head}) and by (who@node); every member is judged before any is merged (still its head, its base a watched branch, no required step failed at its fingerprint), none merged when one is refused, then each merged in the order named; it answers the call and each member's outcome.",
|
||||
async (args): Promise<MergePullAnswer> => mergeCall(gitea, args, deps)),
|
||||
verb(GIT_VERBS.tag, "The seat git's tag: the git tag go/vMAJOR.MINOR.PATCH of a released version, written at the commit its build was made from; a tag that is at that commit is performed.",
|
||||
async (args): Promise<TagAnswer> => {
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
@@ -275,68 +279,6 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
];
|
||||
}
|
||||
|
||||
/** Whether a protection rule refuses a direct push and admits only merges: the rule exists and takes no push. */
|
||||
export function pushRefused(rule: { enable_push?: boolean } | null): boolean {
|
||||
return !!rule && rule.enable_push === false;
|
||||
}
|
||||
|
||||
function holds(comments: GiteaComment[], id: string): boolean {
|
||||
return comments.some((c) => String(c.body ?? "").includes(actMarker(id)));
|
||||
}
|
||||
|
||||
/** merge-pull (design 47 §314): only the head the caller names, only into a base that takes no push, no check judged. */
|
||||
export async function mergePull(gitea: GiteaClient, args: Record<string, unknown>, registered: () => boolean,
|
||||
judge: (head: string) => Promise<Judged | null>): Promise<MergePullAnswer> {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const number = numberOf(args.number);
|
||||
const head = commitOf("head", args.head);
|
||||
const no = (why: string, kind: NonNullable<MergePullAnswer["refused-kind"]>): MergePullAnswer =>
|
||||
({ id, refused: `${full}#${number} is not merged: ${why}`, "refused-kind": kind });
|
||||
// Until the first registrations are written no merge is made: the old assigner would write a want from the trunk with no
|
||||
// pipeline (design 47 §95). It names what is missing.
|
||||
if (!registered()) return no("no module is registered yet: the catalog has said no registration-changed, so no merge is made until the first registrations are written", "no-registrations");
|
||||
const pull = await gitea.getPullRequest(owner, repo, number);
|
||||
if (pull.merged) {
|
||||
// Already merged at that head: performed (the act said again after its answer was lost); at another, refused.
|
||||
return pull.head_sha === head ? { id, performed: true, held: true, ...(pull.merge_commit_sha ? { "merge-commit": pull.merge_commit_sha } : {}) }
|
||||
: no(`it was merged at ${String(pull.head_sha).slice(0, 12)}, not the ${head.slice(0, 12)} the caller names`, "head-moved");
|
||||
}
|
||||
if (pull.state !== "open") return no("it is closed", "git-refused");
|
||||
if (pull.head_sha !== head) {
|
||||
return no(`its head is ${String(pull.head_sha).slice(0, 12)}, not the ${head.slice(0, 12)} the caller names`, "head-moved");
|
||||
}
|
||||
const rule = await gitea.appliedProtection(owner, repo, String(pull.base ?? ""));
|
||||
if (!pushRefused(rule)) {
|
||||
return no(`its base ${pull.base} is guarded by no protection rule that refuses a push`, "unprotected");
|
||||
}
|
||||
// The head's runs, judged from the module's own state (design 47 §39): a step that failed at its fingerprint in any run refuses it,
|
||||
// naming the run, the module and the step; a head no run named merges, and the runner being down changes nothing.
|
||||
const failed = await judge(head);
|
||||
if (failed) {
|
||||
return { id, refused: `${full}#${number} is not merged: the ${failed.step} step of ${failed.module} failed in run ${failed.run} at its build-input fingerprint ${failed.fingerprint}`,
|
||||
"refused-kind": "policy-failed", run: failed.run, module: failed.module, step: failed.step };
|
||||
}
|
||||
try {
|
||||
await gitea.mergePullRequest(owner, repo, number, "merge", true, head);
|
||||
} catch (err) {
|
||||
const f = failure(err);
|
||||
if (f["error-kind"] === "refused") return no(`the forge refused the merge: ${f.error}`, "git-refused");
|
||||
throw err;
|
||||
}
|
||||
const merged = await gitea.getPullRequest(owner, repo, number);
|
||||
if (!merged.merge_commit_sha) throw new Error(`${full}#${number} WAS merged, but the forge named no merge commit`);
|
||||
// The merge is done once the forge named its commit: a read-back that fails is said, never answered as a failed act.
|
||||
let parents: string[];
|
||||
try {
|
||||
parents = await gitea.commitParents(owner, repo, merged.merge_commit_sha);
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${full}#${number} was merged (${merged.merge_commit_sha}); which head it joined could not be read back — ${err instanceof Error ? err.message : err}`);
|
||||
return { id, performed: true, "merge-commit": merged.merge_commit_sha };
|
||||
}
|
||||
if (parents.length === 2 && parents[1] !== head) {
|
||||
throw new Error(`${full}#${number} WAS merged (${merged.merge_commit_sha}), but the forge merged ${parents[1].slice(0, 12)}, ` +
|
||||
`not the ${head.slice(0, 12)} the caller names (novox/hq issue 381)`);
|
||||
}
|
||||
return { id, performed: true, "merge-commit": merged.merge_commit_sha };
|
||||
}
|
||||
|
||||
@@ -1,298 +0,0 @@
|
||||
// **A delivery group's member is checked beside the other members' heads, and a sibling's move puts that verdict out of
|
||||
// date** (novox/hq ADR 0382 §5, §6; issue 867).
|
||||
//
|
||||
// The controller's verdict (`checked`) names the siblings its check read: each as the sibling's repository, pull
|
||||
// request, head, base branch and the base commit the sibling's head was merged onto. This module, the forge's holder,
|
||||
// keeps them per open pull request, beside its other records, and:
|
||||
//
|
||||
// - on its own hearing of a sibling's new head, of its close unmerged, or of a move of its base, sets
|
||||
// mesh/merge-gate and mesh/repo-check of every member whose verdict names that sibling back to pending, with the
|
||||
// reason. Nothing is run for it. The one move that does not count is the sibling's own merge of the head that was
|
||||
// read onto the base commit that was read (in either order of the two merges);
|
||||
// - lets the merge tool refuse a verdict that read siblings unless mesh/delivery-group passed at the very heads it
|
||||
// read, and every sibling head it read carries an approving review at that head (merge.ts).
|
||||
//
|
||||
// A verdict without siblings (a controller from before ADR 0382) is none of this: nothing is kept and nothing changes.
|
||||
// Pure functions and one small file of the module's own state, so this is tested without a forge.
|
||||
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import type { CommitStatus, GiteaPull, HeadCheck } from "./client.js";
|
||||
import { Refused } from "./guard.js";
|
||||
import { CHECK_CONTEXT, REPO_CHECK_CONTEXT, clipped } from "./pulls.js";
|
||||
import { sameCommit, twelve, type OutOfDateWriter } from "./trunk.js";
|
||||
|
||||
/** The status the delivery group's owner sets on each member's head (novox/hq ADR 0239). */
|
||||
export const GROUP_CONTEXT = "mesh/delivery-group";
|
||||
|
||||
/** One sibling a verdict read, as the controller names it (contract of ADR 0382 §4). */
|
||||
export interface SiblingNamed {
|
||||
repository: string;
|
||||
number: number;
|
||||
head: string;
|
||||
base: string;
|
||||
base_commit: string;
|
||||
/** Set here, never by the controller: the sibling merged its head onto its base commit read, so it is no sibling's
|
||||
* move any more. */
|
||||
merged?: boolean;
|
||||
}
|
||||
|
||||
/** What is kept of one member's latest verdict that read siblings: the head it was of and the siblings it named. */
|
||||
export interface SiblingEntry {
|
||||
head: string;
|
||||
siblings: SiblingNamed[];
|
||||
}
|
||||
|
||||
/** The records, by `owner/repo#number` of the member. */
|
||||
export type SiblingRecord = Record<string, SiblingEntry>;
|
||||
|
||||
/** The member's key. */
|
||||
export function memberKey(owner: string, repo: string, number: number): string {
|
||||
return `${owner}/${repo}#${number}`.toLowerCase();
|
||||
}
|
||||
|
||||
/** The siblings a verdict names, trimmed to what is kept; none when it names none or names one badly. */
|
||||
export function siblingsOf(named: unknown): SiblingNamed[] {
|
||||
if (!Array.isArray(named)) return [];
|
||||
const out: SiblingNamed[] = [];
|
||||
for (const s of named) {
|
||||
const r = s as Record<string, unknown>;
|
||||
const repository = String(r?.repository ?? ""), head = String(r?.head ?? ""), base = String(r?.base ?? "");
|
||||
const number = Number(r?.number);
|
||||
if (!repository || !head || !base || !Number.isInteger(number) || number <= 0) continue;
|
||||
out.push({ repository, number, head, base, base_commit: String(r?.base_commit ?? "") });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The siblings as a status names them: "mesh-host#114@abcdef123456, …". */
|
||||
export function siblingWords(siblings: SiblingNamed[]): string {
|
||||
return siblings.map((s) => `${s.repository.split("/").pop()}#${s.number}@${twelve(s.head)}`).join(", ");
|
||||
}
|
||||
|
||||
/** What a status of a verdict that read siblings carries, after the commits it judged. */
|
||||
export function besideWords(siblings: SiblingNamed[]): string {
|
||||
// Short, so that the verdict word and the start of its reason survive the forge's 140 characters: the first sibling
|
||||
// by its pull request and 8 characters of its head, and how many more. The whole list is the holder's record and the comment's.
|
||||
const first = siblings[0];
|
||||
const more = siblings.length > 1 ? ` +${siblings.length - 1} more` : "";
|
||||
return `beside ${first.repository.split("/").pop()}#${first.number}@${twelve(first.head).slice(0, 8)}${more}`;
|
||||
}
|
||||
|
||||
/** The mark a status carries when the siblings could not be learned (ADR 0382 §1): said, never silent. */
|
||||
export const UNKNOWN_MARK = "siblings unknown";
|
||||
|
||||
/** Whether a status names that its verdict read siblings (so that a record lost does not read as none). */
|
||||
export function namesSiblings(description: string | undefined): boolean {
|
||||
return / beside [\w.-]+#\d+@[0-9a-f]{8}\b/.test(description ?? "");
|
||||
}
|
||||
|
||||
/** Whether a status is a verdict put out of date by a sibling's move. */
|
||||
export function siblingOutOfDate(description: string | undefined): boolean {
|
||||
return /^out of date: judged beside /.test(description ?? "");
|
||||
}
|
||||
|
||||
// --- the record on disk ----------------------------------------------------------------------------------------
|
||||
|
||||
/** The file the holder keeps the siblings its verdicts name in, beside its other records. */
|
||||
export function siblingsFile(stateDir: string | undefined): string | null {
|
||||
return stateDir ? `${stateDir.replace(/\/+$/, "")}/siblings-named.json` : null;
|
||||
}
|
||||
|
||||
export function readSiblings(file: string | null): SiblingRecord {
|
||||
if (!file || !existsSync(file)) return {};
|
||||
try {
|
||||
return (JSON.parse(readFileSync(file, "utf8")) as { members?: SiblingRecord }).members ?? {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export function keepSiblings(file: string | null, record: SiblingRecord): void {
|
||||
if (!file) return;
|
||||
try {
|
||||
mkdirSync(dirname(file), { recursive: true });
|
||||
writeFileSync(file + ".tmp", JSON.stringify({ members: record }));
|
||||
renameSync(file + ".tmp", file);
|
||||
} catch (err) {
|
||||
console.error(`[gitea] the siblings a verdict named could not be kept — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
}
|
||||
|
||||
// --- what a sibling's move is ---------------------------------------------------------------------------------
|
||||
|
||||
/** What weighing a sibling reads of the forge. */
|
||||
export interface SiblingForge {
|
||||
getPullRequest(owner: string, repo: string, index: number): Promise<GiteaPull>;
|
||||
branchHead(owner: string, repo: string, branch: string): Promise<string>;
|
||||
commitParents(owner: string, repo: string, sha: string): Promise<string[]>;
|
||||
}
|
||||
|
||||
/** The reason a verdict that named this sibling is out of date, from what the forge holds now, or nothing; and whether
|
||||
* the sibling has merged its head read onto the base commit read, which ends it as a sibling. */
|
||||
export async function siblingMoved(forge: SiblingForge, s: SiblingNamed): Promise<{ reason?: string; merged?: boolean }> {
|
||||
const [owner, repo] = s.repository.split("/");
|
||||
const who = `${repo}#${s.number}`;
|
||||
const pull = await forge.getPullRequest(owner, repo, s.number);
|
||||
if (pull.merged) {
|
||||
// Its own merge of the head that was read onto the base commit that was read: the head the merge joined is the head
|
||||
// read, and the commit it was made onto is the base commit read. Any other merge is a move of the base.
|
||||
const parents = pull.merge_commit_sha ? await forge.commitParents(owner, repo, pull.merge_commit_sha).catch(() => []) : [];
|
||||
const own = sameCommit(pull.head_sha, s.head) && parents.length >= 1 && sameCommit(parents[0], s.base_commit) &&
|
||||
(parents.length === 1 || sameCommit(parents[1], s.head));
|
||||
if (own) return { merged: true };
|
||||
return { reason: `${who} was merged otherwise than its head ${twelve(s.head)} onto ${twelve(s.base_commit)}` };
|
||||
}
|
||||
if (pull.state !== "open") return { reason: `${who} was closed unmerged` };
|
||||
if (!sameCommit(pull.head_sha, s.head)) {
|
||||
return { reason: `${who} at ${twelve(s.head)}, it is now at ${twelve(pull.head_sha)}` };
|
||||
}
|
||||
const base = await forge.branchHead(owner, repo, s.base);
|
||||
if (!sameCommit(base, s.base_commit)) {
|
||||
return { reason: `${who} merged onto ${twelve(s.base_commit)}, its ${s.base} is now ${twelve(base)}` };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/** A verdict out of date because a sibling moved, as its statuses say it (ADR 0382 §5; the wording of ADR 0323 §4). */
|
||||
export function siblingOutOfDateWords(reason: string): string {
|
||||
return clipped(`out of date: judged beside ${reason}; judged again on a recheck or when it is merged`);
|
||||
}
|
||||
|
||||
/** The statuses a sibling's move puts back to pending on one member's head, from what the forge holds on it now: none
|
||||
* when it has no merge check, or its gate is pending (a check on its way, or already out of date). */
|
||||
export function siblingStatuses(checks: Record<string, HeadCheck>, reason: string): CommitStatus[] {
|
||||
const gate = checks[CHECK_CONTEXT];
|
||||
if (!gate || gate.state === "pending") return [];
|
||||
const description = siblingOutOfDateWords(reason);
|
||||
const out: CommitStatus[] = [{ state: "pending", context: CHECK_CONTEXT, description }];
|
||||
if (checks[REPO_CHECK_CONTEXT]) out.push({ state: "pending", context: REPO_CHECK_CONTEXT, description });
|
||||
return out;
|
||||
}
|
||||
|
||||
/** What weighing a sibling's move writes with, beside the forge's reads. */
|
||||
export interface SiblingWeighing extends SiblingForge {}
|
||||
|
||||
/**
|
||||
* A sibling moved (a new head, a close, a base move, a merge): every member whose latest verdict names it has its
|
||||
* merge check put back to pending with the reason. `repository` and, when the event names one, `number` say which
|
||||
* sibling. A member whose head is no longer the one its verdict was of is dropped from the record: its new head is
|
||||
* checked afresh. Nothing is run. Answers whether the record changed. A read that fails leaves that member for the next
|
||||
* move to weigh, and is said.
|
||||
*/
|
||||
export async function weighSiblings(forge: SiblingWeighing, gates: OutOfDateWriter, record: SiblingRecord,
|
||||
repository: string, number?: number): Promise<boolean> {
|
||||
let changed = false;
|
||||
const repo = repository.toLowerCase();
|
||||
for (const [key, entry] of Object.entries(record)) {
|
||||
const [path] = key.split("#");
|
||||
const [owner, name] = path.split("/");
|
||||
const memberNumber = Number(key.split("#")[1]);
|
||||
// A member that is merged or no longer open has no verdict to put out of date: its entry goes (the record must not
|
||||
// grow without bound, and a merged head is never re-pended by a sibling's later push). Weighed when the event is the
|
||||
// member's own, or names one of its siblings.
|
||||
const own = path === repo && (number === undefined || number === memberNumber);
|
||||
if (own || entry.siblings.some((s) => !s.merged && s.repository.toLowerCase() === repo && (number === undefined || s.number === number))) {
|
||||
try {
|
||||
const member = await forge.getPullRequest(owner, name, memberNumber);
|
||||
if (member.merged || member.state !== "open") {
|
||||
delete record[key];
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${key}: whether it is still open could not be read — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
}
|
||||
for (const s of entry.siblings) {
|
||||
if (s.merged || s.repository.toLowerCase() !== repo || (number !== undefined && s.number !== number)) continue;
|
||||
try {
|
||||
const moved = await siblingMoved(forge, s);
|
||||
if (moved.merged) {
|
||||
s.merged = true;
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if (!moved.reason) continue;
|
||||
const member = await forge.getPullRequest(owner, name, memberNumber);
|
||||
if (!sameCommit(member.head_sha, entry.head)) {
|
||||
delete record[key];
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
const written = await gates.outOfDate(owner, name, entry.head, (checks) => siblingStatuses(checks, moved.reason!),
|
||||
member.html_url || undefined);
|
||||
if (written.length) {
|
||||
console.log(`[gitea] ${key}: its verdict was judged beside ${moved.reason}: out of date, nothing run (novox/hq ADR 0382)`);
|
||||
}
|
||||
break;
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${key}: whether ${s.repository}#${s.number} moved could not be weighed, so it is weighed again ` +
|
||||
`on its next move — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
// --- the merge tool's rule (ADR 0382 §6) ----------------------------------------------------------------------
|
||||
|
||||
/** What the rule reads beside the pull request being merged. */
|
||||
export interface GroupForge {
|
||||
headChecks(owner: string, repo: string, sha: string): Promise<Record<string, HeadCheck>>;
|
||||
}
|
||||
|
||||
/** The heads the operator's Approve covers (novox/hq ADR 0421 §9, issue 1022), as mesh-delivery hands them to the merge
|
||||
* tool: `owner/repo@<full sha>`, one per member, by repository. A malformed entry is refused, never skipped. */
|
||||
export function approvedHeadsOf(given: unknown): Map<string, string> | undefined {
|
||||
if (given === undefined || given === null) return undefined;
|
||||
if (!Array.isArray(given)) throw new Refused("approved_heads must be a list of owner/repo@<full sha>. Nothing was merged");
|
||||
const out = new Map<string, string>();
|
||||
for (const entry of given) {
|
||||
const m = /^([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)@([0-9a-f]{40}|[0-9a-f]{64})$/i.exec(String(entry));
|
||||
if (!m) throw new Refused(`approved_heads holds ${JSON.stringify(entry)}, which is not owner/repo@<full sha>. Nothing was merged`);
|
||||
out.set(m[1].toLowerCase(), m[2].toLowerCase());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Why a pull request's head is not one the Approve covers, in words, or nothing: its repository is not named, or names
|
||||
* another head. */
|
||||
export function notApproved(approved: Map<string, string>, repository: string, number: number, head: string): string | undefined {
|
||||
const covered = approved.get(repository.toLowerCase());
|
||||
const at = `${repository.split("/")[1]}#${number}@${twelve(head)}`;
|
||||
if (covered === undefined) return `${at} is not among the heads the Approve covers`;
|
||||
if (covered !== head.toLowerCase()) return `${at} is not the head the Approve covers (${twelve(covered)})`;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* What stops a pull request whose verdict read siblings from merging, in words, or nothing: mesh/delivery-group must be
|
||||
* a success at its own head and at every sibling head the verdict read, and every head read must be one the operator's
|
||||
* Approve covers (ADR 0421 §9: the Approve is the review; the merge tool takes the approved heads from mesh-delivery).
|
||||
* Every missing thing is named.
|
||||
*/
|
||||
export async function groupRefusal(forge: GroupForge, self: { owner: string; repo: string; number: number; head: string },
|
||||
siblings: SiblingNamed[], approved: Map<string, string> | undefined): Promise<string | undefined> {
|
||||
const missing: string[] = [];
|
||||
const members = [{ repository: `${self.owner}/${self.repo}`, number: self.number, head: self.head },
|
||||
...siblings.map((s) => ({ repository: s.repository, number: s.number, head: s.head }))];
|
||||
for (const m of members) {
|
||||
const [owner, repo] = m.repository.split("/");
|
||||
const at = `${repo}#${m.number}@${twelve(m.head)}`;
|
||||
let group: HeadCheck | undefined;
|
||||
try {
|
||||
group = (await forge.headChecks(owner, repo, m.head))[GROUP_CONTEXT];
|
||||
} catch (err) {
|
||||
missing.push(`${GROUP_CONTEXT} at ${at} could not be read (${err instanceof Error ? err.message : err})`);
|
||||
continue;
|
||||
}
|
||||
if (group?.state !== "success") missing.push(`${GROUP_CONTEXT} is ${group?.state ?? "missing"} at ${at}`);
|
||||
if (!approved) missing.push(`${at} is covered by no Approve: the merge was handed no approved heads`);
|
||||
else {
|
||||
const why = notApproved(approved, m.repository, m.number, m.head);
|
||||
if (why) missing.push(why);
|
||||
}
|
||||
}
|
||||
return missing.length ? missing.join("; ") : undefined;
|
||||
}
|
||||
@@ -12,22 +12,6 @@ const repo = (full: string, updated: string) => {
|
||||
return { full_name: full, owner, name, private: false, html_url: "", updated_at: updated };
|
||||
};
|
||||
|
||||
test("a pull request opened after the margin is looked at, by its own update time", async () => {
|
||||
const { reposToLook } = await import("../pulls.ts");
|
||||
const pushed = "2026-10-09T10:00:00Z"; // the branch pushed: the repository's update time
|
||||
// The pull request opened two minutes later; the last pass was at 10:02:30, so the floor is 10:01:30.
|
||||
const floor = "2026-10-09T10:01:30Z";
|
||||
const repos = [repo("novox/mesh-sdk", pushed), repo("novox/hq", "2026-10-09T09:00:00Z")];
|
||||
const opened = [{ owner: "novox", name: "mesh-sdk", updated_at: "2026-10-09T10:02:00Z" }];
|
||||
assert.deepEqual(reposToLook(repos, floor, opened, false).map((r) => r.full_name), ["novox/mesh-sdk"],
|
||||
"the repository of a pull request updated since the floor, though the repository itself did not move");
|
||||
assert.deepEqual(reposToLook(repos, floor, [], false).map((r) => r.full_name), [],
|
||||
"without the pull request's own time nothing is looked at — the fault");
|
||||
assert.deepEqual(reposToLook(repos, floor, [], true).map((r) => r.full_name).sort(), ["novox/hq", "novox/mesh-sdk"],
|
||||
"a sweep looks at every repository");
|
||||
assert.deepEqual(reposToLook(repos, "", [], false).length, 2, "the first pass looks at every repository");
|
||||
});
|
||||
|
||||
test("every open pull request is read with what its base requires and what its head has (gitea_open_pulls)", async () => {
|
||||
const { GiteaClient } = await import("../client.ts");
|
||||
const asked: string[] = [];
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { memory } from "./memstate.ts";
|
||||
|
||||
// **The call is written before its first merge** (novox/hq design 47 H §766; H-A-18, H-A-47, H-A-71): the key
|
||||
// call.<call> of the state calls holds by, the proved account, at, each member with its base and its state (judged before
|
||||
// the first merge, merged with its commit as it merges) and said; a call past call-retention is deleted on a new call's
|
||||
// write, oldest first.
|
||||
|
||||
const A = "a".repeat(40), B = "b".repeat(40);
|
||||
|
||||
test("a merge call is kept before its first merge, each member judged, then merged with its commit", async () => {
|
||||
const { mergeCall } = await import("../dist/mergecall.js");
|
||||
const { Calls } = await import("../dist/calls.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const { kv, st } = memory();
|
||||
const calls = new Calls(st as any);
|
||||
const seenAtMerge: any[] = [];
|
||||
const pulls: Record<string, any> = { 5: { head: A, merged: false }, 9: { head: B, merged: false } };
|
||||
const forge = {
|
||||
getPullRequest: async (_o: string, r: string, n: number) => ({ number: n, state: "open", 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) => {
|
||||
seenAtMerge.push(structuredClone(kv.get("call.0123456789abcdef")));
|
||||
pulls[n].merged = true;
|
||||
},
|
||||
commitParents: async () => [],
|
||||
};
|
||||
const args = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/app", number: 9, head: B }] };
|
||||
const at = new Date("2026-11-20T03:00:00Z");
|
||||
await runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, { registered: () => true,
|
||||
watched: () => new Map([["novox/app", new Set(["main"])]]), judge: async () => null, newCall: () => "0123456789abcdef",
|
||||
calls, now: () => at } as any));
|
||||
const first = seenAtMerge[0];
|
||||
assert.deepEqual([first.by, first.account, first.at, first.said], ["lead@shanks", "shanks.claude-code", at.toISOString(), false]);
|
||||
assert.deepEqual(first.members.map((m: any) => [m.number, m.branch, m.head, m.state]), [[5, "main", A, "judged"], [9, "main", B, "judged"]]);
|
||||
assert.deepEqual(seenAtMerge[1].members.map((m: any) => [m.state, m.commit]), [["merged", "5".repeat(40)], ["judged", undefined]]);
|
||||
const kept = await calls.get("0123456789abcdef");
|
||||
assert.deepEqual(kept?.members.map((m: any) => m.state), ["merged", "merged"]);
|
||||
});
|
||||
|
||||
test("a call past call-retention is deleted on a new call's write, oldest first; a younger one is kept", async () => {
|
||||
const { Calls } = await import("../dist/calls.js");
|
||||
const { kv, st } = memory();
|
||||
const calls = new Calls(st as any, () => 30);
|
||||
const call = (at: string) => ({ by: "a@b", account: "x", at, members: [], said: true });
|
||||
await calls.put("1111111111111111", call("2026-09-01T00:00:00Z"), new Date("2026-09-01T00:00:00Z"));
|
||||
await calls.put("2222222222222222", call("2026-10-25T00:00:00Z"), new Date("2026-10-25T00:00:00Z"));
|
||||
await calls.put("3333333333333333", call("2026-11-20T00:00:00Z"), new Date("2026-11-20T00:00:00Z"));
|
||||
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.<call> 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<number, any> = { 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");
|
||||
});
|
||||
|
||||
// The merge calls' tools (design 47 H §766): forge_calls answers the calls newest first with each member's state, and
|
||||
// forge_call_say_again says a call's merge-called again once the hand-act log took it.
|
||||
test("forge_calls reads the calls newest first; forge_call_say_again says one again after the hand-act log", async () => {
|
||||
const { Calls } = await import("../dist/calls.js");
|
||||
const { Partials } = await import("../dist/partials.js");
|
||||
const { callTools } = await import("../dist/tools/calls.js");
|
||||
const { st } = memory();
|
||||
const calls = new Calls(st as any);
|
||||
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("00000000000000aa", { by: "a@b", account: "x", at: "2026-11-20T01:00:00Z", members: [m(1, "merged")], said: true } as any);
|
||||
await calls.put("00000000000000bb", { by: "a@b", account: "x", at: "2026-11-20T02:00:00Z", members: [m(2, "refused")], said: true } as any);
|
||||
const said: any[] = [], acts: any[] = [];
|
||||
const tools = callTools(calls, new Partials(memory().st as any, calls), async (subject: string, body: unknown) => { said.push([subject, body]); },
|
||||
async (a) => { acts.push(a); });
|
||||
const tool = (name: string) => tools.find((t) => t.name === name)!;
|
||||
const read: any = await tool("forge_calls").run({});
|
||||
assert.deepEqual(read.calls.map((c: any) => [c.call, c.members[0].state]), [["00000000000000bb", "refused"], ["00000000000000aa", "merged"]]);
|
||||
await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000aa" }), /names the call .* and why/);
|
||||
await assert.rejects(tool("forge_call_say_again").run({ call: "00000000000000ff", why: "x" }), /keeps no merge call/);
|
||||
const again: any = await tool("forge_call_say_again").run({ call: "00000000000000aa", why: "the gate missed it" });
|
||||
assert.deepEqual([again.said, acts[0].verb, said[0][0]], [true, "forge_call_say_again", "mesh.seat.git.event.merge-called.00000000000000aa"]);
|
||||
const none: any = await tool("forge_call_say_again").run({ call: "00000000000000bb", why: "x" });
|
||||
assert.equal(none.said, false, "a call that landed none says none");
|
||||
});
|
||||
@@ -20,7 +20,7 @@ function memory() {
|
||||
}
|
||||
const ended = (commit: string, outcome: string, extra: Record<string, unknown> = {}, over: Record<string, unknown> = {}) => ({
|
||||
v: 1, pipeline: `main.7.nats-${commit.slice(0, 2)}`, module: "nats", branch: "main", commit, at: "2026-11-11T12:00:00Z",
|
||||
result: { step: "unit", status: "required", outcome, fingerprint: "fp-1", ...extra }, ...over }) as any;
|
||||
fingerprint: "fp-1", result: { step: "unit", status: "required", outcome, ...extra }, ...over }) as any;
|
||||
|
||||
test("a head is refused by a required step that failed at its fingerprint in any run, until a rerun passes it", async () => {
|
||||
const { kv, st } = memory();
|
||||
@@ -45,12 +45,13 @@ test("a head is refused by a required step that failed at its fingerprint in any
|
||||
// A later commit at the fingerprint is refused too, and a failed optional step refuses nothing.
|
||||
await f.stepEnded(ended(C3, "passed"));
|
||||
await judged("a later commit of the same content", C3, "run-on-request nats unit");
|
||||
await f.stepEnded(ended(C3, "failed", { step: "lint", status: "optional", fingerprint: "fp-2" }));
|
||||
await f.stepEnded(ended(C3, "failed", { step: "lint", status: "optional" }, { fingerprint: "fp-2" }));
|
||||
await judged("a failed optional step", C3, "run-on-request nats unit");
|
||||
assert.equal(kv.has(failedStepKey("nats", "fp-2", "lint")), false);
|
||||
// A result that names no fingerprint is kept at the one the seat's step event carries (`fingerprint`).
|
||||
await f.stepEnded(ended(C3, "failed", { step: "lint", fingerprint: undefined }, { fingerprint: "fp-3" }));
|
||||
// The fingerprint is the body's alone, never the result's (G §585, H-A-93).
|
||||
await f.stepEnded(ended(C3, "failed", { step: "lint", fingerprint: "fp-x" }, { fingerprint: "fp-3" }));
|
||||
assert.ok(kv.has(failedStepKey("nats", "fp-3", "lint")), "the event's fingerprint is kept");
|
||||
assert.equal(kv.has(failedStepKey("nats", "fp-x", "lint")), false, "the result's is never read");
|
||||
// A plain pass clears nothing; a rerun that passes clears it.
|
||||
await f.stepEnded(ended(C2, "passed"));
|
||||
await judged("a plain pass", C2, "run-on-request nats unit");
|
||||
@@ -109,7 +110,8 @@ test("a seen key past 30 days is deleted at the next step-ended, a younger one k
|
||||
const now = new Date("2026-11-11T12:00:00Z");
|
||||
const old = (n: number) => new Date(Date.parse("2026-10-01T00:00:00Z") - n * 60000).toISOString();
|
||||
await st.put(seenKey(C1, "old"), { v: 1, commit: C1, module: "old", fingerprints: ["x"], at: old(0) });
|
||||
await st.put(seenKey(C2, "young"), { v: 1, commit: C2, module: "young", fingerprints: ["x"], at: "2026-10-20T00:00:00Z" });
|
||||
// Younger than 30 days both before the step-ended and before the tool's own clock, today's: kept by both.
|
||||
await st.put(seenKey(C2, "young"), { v: 1, commit: C2, module: "young", fingerprints: ["x"], at: new Date(Date.now() - 86_400_000).toISOString() });
|
||||
await f.stepEnded(ended(C3, "passed"));
|
||||
assert.equal(kv.has(seenKey(C1, "old")), false, "past 30 days: deleted at the next step-ended");
|
||||
assert.equal(kv.has(seenKey(C2, "young")), true, "younger: kept");
|
||||
@@ -121,3 +123,21 @@ test("a seen key past 30 days is deleted at the next step-ended, a younger one k
|
||||
const done: any = await runAs({ "mesh/caller": "operator" }, {}, () => tool.run({ why: "stale" }));
|
||||
assert.deepEqual([done.deleted.length, acts[0].verb], [1, "git_seen_expire"]);
|
||||
});
|
||||
|
||||
// step-unread (G §585, H-A-93): a step-ended of a pipeline's commit without the body's fingerprint records nothing and writes
|
||||
// the finding, scope module, its subject the step's module, keyed <run>.<module>.<step>, warning, resolver operator; a later
|
||||
// step-ended of that step with its fingerprint clears it.
|
||||
test("a step-ended without its fingerprint writes step-unread, cleared by the step's later step-ended with it", async () => {
|
||||
const { stepUnreadFinding } = await import("../failedsteps.js");
|
||||
const { kv, st } = memory();
|
||||
const findings = memory();
|
||||
const f = new FailedSteps(st as any, () => {}, () => true, async () => ({ bytes: 0, keys: 0, max_bytes: 0 }), findings.st as any);
|
||||
await f.stepEnded(ended(C1, "failed", { fingerprint: "fp-in-result" }, { fingerprint: undefined }));
|
||||
const { key } = stepUnreadFinding("main.7.nats-" + C1.slice(0, 2), "nats", "unit", "");
|
||||
const raised: any = findings.kv.get(key);
|
||||
assert.deepEqual([raised?.kind, raised?.subject, raised?.severity, raised?.resolver], ["step-unread", { scope: "module", id: "nats" }, "warning", "operator"]);
|
||||
assert.match(raised.summary, /unit of nats in run main\.7\.nats-/);
|
||||
assert.equal([...kv.keys()].some((k) => !k.startsWith("seen.")), false, "no failure recorded");
|
||||
await f.stepEnded(ended(C1, "failed"));
|
||||
assert.equal(findings.kv.has(key), false, "cleared by the step's step-ended with its fingerprint");
|
||||
});
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
// **One writer of a head's mesh/merge-gate** (novox/hq issue 446): the announcement's pending words, a recheck's pending and
|
||||
// a trunk's move, each write of a head in the order it was asked.
|
||||
|
||||
const SHA = "a6b08de8507c1703528277e5cefc80548dbc9bb3";
|
||||
|
||||
/** A forge that keeps the newest status per context, as gitea does, and whose writes take as long as a test says. */
|
||||
function aForge() {
|
||||
const kept: Record<string, { state: string; description: string; updated_at: string }> = {};
|
||||
const written: string[] = [];
|
||||
let failNext = 0;
|
||||
let hold: Promise<void> | null = null;
|
||||
let release = () => {};
|
||||
return {
|
||||
kept, written,
|
||||
/** The next writes fail: a request the forge did not take. */
|
||||
failWrites(n = 1) {
|
||||
failNext = n;
|
||||
},
|
||||
/** The next writes wait until released: a request in flight. */
|
||||
holdWrites() {
|
||||
hold = new Promise<void>((r) => (release = r));
|
||||
},
|
||||
release() {
|
||||
hold = null;
|
||||
release();
|
||||
},
|
||||
async headChecks() {
|
||||
return {};
|
||||
},
|
||||
async setCommitStatus(_o: string, _r: string, _sha: string, s: { state: string; context: string; description: string }) {
|
||||
if (hold) await hold;
|
||||
if (failNext > 0) {
|
||||
failNext--;
|
||||
throw new Error("the forge answered 502");
|
||||
}
|
||||
kept[s.context] = { state: s.state, description: s.description, updated_at: new Date().toISOString() };
|
||||
written.push(`${s.state} ${s.description}`);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// **A recheck's pending goes through the one writer** (novox/hq issue 456).
|
||||
|
||||
const recheckAt = new Date("2026-10-11T01:30:00Z");
|
||||
const checkingAgain = { state: "pending" as const, context: "mesh/merge-gate", description: "checking again: main moved on" };
|
||||
const gateOf = (forge: ReturnType<typeof aForge>) =>
|
||||
`${forge.kept["mesh/merge-gate"].state} ${forge.kept["mesh/merge-gate"].description}`;
|
||||
|
||||
test("a repository as the mesh clones it is told from another", async () => {
|
||||
const { clonedAs } = await import("../gate.ts");
|
||||
assert.equal(clonedAs("http://127.0.0.1:20000/novox/hq.git", "novox", "hq"), true);
|
||||
assert.equal(clonedAs("http://127.0.0.1:20000/novox/mesh-hq.git", "novox", "hq"), false);
|
||||
assert.equal(clonedAs("http://127.0.0.1:20000/novox/mesh-controller.git", "novox", "hq"), false);
|
||||
});
|
||||
|
||||
test("a recheck the forge does not take fails its caller, and the next write still takes its turn", async () => {
|
||||
const { GateWriter } = await import("../gate.ts");
|
||||
const forge = aForge();
|
||||
const gates = new GateWriter(forge, () => recheckAt);
|
||||
await gates.pending("novox", "hq", SHA);
|
||||
forge.failWrites();
|
||||
await assert.rejects(gates.status("novox", "hq", SHA, checkingAgain));
|
||||
await gates.status("novox", "hq", SHA, checkingAgain);
|
||||
assert.equal(gateOf(forge), "pending checking again: main moved on");
|
||||
});
|
||||
|
||||
@@ -431,70 +431,20 @@ test("the two new tools are named for what mesh_search is asked, and gitea_api s
|
||||
assert.ok("refused" in judgeApiCall("PATCH", "/repos/a/b/pulls/7", { base: "x" }));
|
||||
});
|
||||
|
||||
// **merge-pull judges the head it is asked at and no check** (design 47 *The seat git*, §314): merge-pull merges the
|
||||
// head it is named into a base that takes no push, whatever its statuses say, and an id said again after the merge is
|
||||
// answered performed.
|
||||
test("merge-pull merges the head the caller names, and refuses one at whose fingerprint a required step failed", async () => {
|
||||
// The seat git's pull and status (design 47 *The seat git*): an open pull request answers what the forge would merge now,
|
||||
// and two writes of one head's status land in order. merge-pull is the merge call's (merge-call.test.ts).
|
||||
test("the seat's pull answers what the forge would merge now, and its status is set on the commit", async () => {
|
||||
const { seatTools } = await import("../dist/seat.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const bridge = "ace.mesh-delivery-forge-bridge";
|
||||
let registered = true;
|
||||
let failedStep: { run: string; module: string; step: string; fingerprint: string } | null = null;
|
||||
// The head's one writer of its statuses (gate.ts), over a forge whose first write is still in flight when the next is asked.
|
||||
const { GateWriter } = await import("../dist/gate.js");
|
||||
// The seat's status is set through the forge's client directly (design 47 H §764; H-A-100): no chain of a head's writes.
|
||||
const landed: string[] = [];
|
||||
let inFlight: Promise<void> | null = null, land = () => {};
|
||||
const slow = {
|
||||
headChecks: async () => ({}),
|
||||
setCommitStatus: async (_o: string, _r: string, _sha: string, s: { state: string; description: string }) => {
|
||||
if (inFlight) { const w = inFlight; inFlight = null; await w; }
|
||||
landed.push(`${s.state} ${s.description}`);
|
||||
},
|
||||
};
|
||||
const gates = new GateWriter(slow);
|
||||
const seat = seatTools(Object.assign(Object.create(client), { setCommitStatus: slow.setCommitStatus }),
|
||||
{ run: async () => ({ stdout: "", code: 0 }), container: "gitea", statuses: gates, registered: () => registered, judge: async () => failedStep });
|
||||
const seat = seatTools(Object.assign(Object.create(client), {
|
||||
setCommitStatus: async (_o: string, _r: string, _sha: string, s: { state: string; description: string }) => { landed.push(`${s.state} ${s.description}`); },
|
||||
}), { run: async () => ({ stdout: "", code: 0 }), container: "gitea", registered: () => true, judge: async () => null,
|
||||
watched: () => new Map() });
|
||||
const call = (verb: string, args: Record<string, unknown>, caller = bridge) =>
|
||||
runAs({ "mesh/caller": caller }, args, () => seat.find((t: any) => t.name === `git.${verb}`)!.run(args)) as Promise<any>;
|
||||
const merge = (extra: Record<string, unknown> = {}, caller?: string) =>
|
||||
call("merge-pull", { id: "act-1", repository: "novox/mesh-catalog", number: 7, head: REVIEWED, ...extra }, caller);
|
||||
const rows: { name: string; set: () => void; caller?: string; extra?: Record<string, unknown>; want: RegExp | "merged" | "held"; loud?: boolean; kind?: string }[] = [
|
||||
{ name: "no registration written yet", set: () => { registered = false; }, want: /no module is registered yet/, kind: "no-registrations" },
|
||||
{ name: "a head that moved", set: () => { state.head = MOVED; }, want: /its head is 3c4d5e6f7a8b, not the abc1234def56/, kind: "head-moved" },
|
||||
{ name: "a base no rule guards", set: () => { state.rules = []; }, want: /guarded by no protection rule/, kind: "unprotected" },
|
||||
{ name: "a base whose protection was removed after the last branch-moved", set: () => { state.rules = [protectedMain()]; state.rules = []; }, want: /guarded by no protection rule/, kind: "unprotected" },
|
||||
{ name: "a base that takes a push", set: () => { state.rules = [{ rule_name: "main", enable_push: true }]; }, want: /guarded by no protection rule/, kind: "unprotected" },
|
||||
{ name: "a required step failed at the head's fingerprint", set: () => { failedStep = { run: "main.7.nats", module: "nats", step: "unit", fingerprint: "fp-1" }; },
|
||||
want: /the unit step of nats failed in run main\.7\.nats/, kind: "policy-failed" },
|
||||
{ name: "a failed check", set: () => { state.statuses = [{ context: "mesh/check", state: "failure", description: "fail" }]; }, want: "merged" },
|
||||
{ name: "no status at all", set: () => { state.statuses = []; }, want: "merged" },
|
||||
{ name: "a check of another trunk", set: () => { state.statuses = judgedAt(MOVED); }, want: "merged" },
|
||||
{ name: "the id said again after the merge", set: () => { state.merged = true; }, want: "held" },
|
||||
{ name: "a pull request merged at another head", set: () => { state.merged = true; state.head = MOVED; }, want: /merged at 3c4d5e6f7a8b, not the abc1234def56/ },
|
||||
{ name: "a closed pull request", set: () => { state.prState = "closed"; }, want: /it is closed/, kind: "git-refused" },
|
||||
{ name: "a fast-forward", set: () => { state.parents = [REVIEWED]; }, want: "merged" },
|
||||
{ name: "the merge read back fails", set: () => { state.commitsFail = 502; }, want: "merged" },
|
||||
{ name: "the forge merged another head", set: () => { state.landed = MOVED; }, want: /WAS merged .*the forge merged 3c4d5e6f7a8b, not the abc1234def56/, loud: true },
|
||||
];
|
||||
for (const r of rows) {
|
||||
registered = true; failedStep = null; state.merged = false; state.head = REVIEWED; state.statuses = judgedAt(TRUNK); state.sent = [];
|
||||
state.rules = [protectedMain()]; state.prState = "open"; state.parents = null; state.commitsFail = 0; state.landed = "";
|
||||
r.set();
|
||||
const got = await merge(r.extra, r.caller);
|
||||
if (r.want === "merged") {
|
||||
assert.equal(got.performed, true, `${r.name}: ${JSON.stringify(got)}`);
|
||||
assert.equal(got["merge-commit"], state.mergeCommit, r.name);
|
||||
assert.equal(state.sent.find((s) => s.path.endsWith("/pulls/7/merge"))?.body.head_commit_id, REVIEWED, r.name);
|
||||
} else if (r.want === "held") {
|
||||
assert.deepEqual([got.performed, got.held], [true, true], r.name);
|
||||
assert.deepEqual(writes(), [], `${r.name}: nothing merged again`);
|
||||
} else {
|
||||
assert.match(String(got.refused ?? got.error), r.want, r.name);
|
||||
if (r.kind) assert.equal(got["refused-kind"], r.kind, r.name);
|
||||
if (r.kind === "policy-failed") assert.deepEqual([got.run, got.module, got.step], ["main.7.nats", "nats", "unit"], r.name);
|
||||
assert.deepEqual(writes(), r.loud ? ["POST /repos/novox/mesh-catalog/pulls/7/merge"] : [], `${r.name}: what was merged`);
|
||||
}
|
||||
}
|
||||
// A read: an open pull request answers what the forge would merge now; a forge that fails is the kind failed.
|
||||
state.merged = false; state.head = REVIEWED; state.rules = [protectedMain()];
|
||||
const pull = await call("pull", { repository: "novox/mesh-catalog", number: 7 });
|
||||
@@ -507,17 +457,9 @@ test("merge-pull merges the head the caller names, and refuses one at whose fing
|
||||
assert.deepEqual([open.pull?.head, open.pull?.state, open.merging?.protected, open.merging?.required],
|
||||
[REVIEWED, "open", true, ["mesh/merge-gate", "mesh/repo-check"]], JSON.stringify(open));
|
||||
assert.equal("body" in (open.pull ?? {}), false, "never its description");
|
||||
// Two writes of one head out of order (issue 456): a recheck's pending still in flight when the verdict is set through the
|
||||
// seat; the verdict takes its turn after it and is what the head carries.
|
||||
inFlight = new Promise<void>((r) => (land = r));
|
||||
const recheck = gates.pending("novox", "mesh-catalog", REVIEWED);
|
||||
const verdict = call("status", { id: "act-s", repository: "novox/mesh-catalog", commit: REVIEWED, state: "success",
|
||||
context: "mesh/merge-gate", description: "passed" });
|
||||
await new Promise((r) => setImmediate(r));
|
||||
land();
|
||||
await recheck;
|
||||
assert.equal((await verdict).performed, true);
|
||||
assert.equal(landed.at(-1), "success passed", `the verdict lands last: ${JSON.stringify(landed)}`);
|
||||
const verdict = await call("status", { id: "act-s", repository: "novox/mesh-catalog", commit: REVIEWED, state: "success",
|
||||
context: "mesh/check", description: "passed" });
|
||||
assert.deepEqual([verdict.performed, landed], [true, ["success passed"]]);
|
||||
// A pull request the forge does not have is the kind not-found, so a caller never reads it out of the words.
|
||||
const gone = await call("pull", { repository: "novox/mesh-catalog", number: 404 });
|
||||
assert.deepEqual([gone.pull, gone["error-kind"]], [undefined, "not-found"], JSON.stringify(gone));
|
||||
@@ -552,10 +494,10 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
};
|
||||
const run = async (_f: string, args: string[]) => args.includes("show") || args.includes("--ref") && !args.includes("append")
|
||||
? { stdout: noteShown, code: noteShown ? 0 : 1 } : (noteWritten++, { stdout: "", code: 0 });
|
||||
const seat = seatTools(forge as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null });
|
||||
const seat = seatTools(forge as any, { run: run as any, container: "gitea", registered: () => true, judge: async () => null, watched: () => new Map() });
|
||||
const call = (verb: string, args: Record<string, unknown>, over: Record<string, unknown> = {}) => {
|
||||
const t = seat.find((x: any) => x.name === `git.${verb}`)!;
|
||||
const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", statuses: { status: async () => {} }, registered: () => true, judge: async () => null });
|
||||
const wrapped = seatTools(Object.assign(Object.create(forge), over) as any, { run: run as any, container: "gitea", registered: () => true, judge: async () => null, watched: () => new Map() });
|
||||
return runAs({ "mesh/caller": "ace.x" }, args, () => wrapped.find((x: any) => x.name === t.name)!.run(args)) as Promise<any>;
|
||||
};
|
||||
const rows: [string, string, Record<string, unknown>, Record<string, unknown>, (a: any) => unknown, unknown][] = [
|
||||
@@ -563,9 +505,7 @@ test("every read and act of the seat git answers its contract, and an error is a
|
||||
["pull", "by merge commit", { repository: "novox/app", "merge-commit": OTHER }, {}, (a) => [a.pull.state, a.pull["merge-commit"], a.merging], ["merged", OTHER, undefined]],
|
||||
["pull", "unknown", { repository: "novox/app", number: 404 }, {}, (a) => [a.pull, a["error-kind"]], [undefined, "not-found"]],
|
||||
["pull", "forge down", { repository: "novox/app", number: 9 }, { getPullRequest: down }, (a) => [a.pull, a["error-kind"]], [undefined, "failed"]],
|
||||
["branch", "a branch a rule guards", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => ({ rule_name: "main", enable_push: false }) }, (a) => [a.head, a.protected], [SHA, true]],
|
||||
["branch", "a branch no rule guards", { repository: "novox/app", branch: "scratch" }, { branchHead: async () => SHA, appliedProtection: async () => null }, (a) => [a.head, a.protected], [SHA, false]],
|
||||
["branch", "a rule that takes a push", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => ({ rule_name: "main", enable_push: true }) }, (a) => [a.protected], [false]],
|
||||
["branch", "a branch's head, no protection read (ADR 0682 §13)", { repository: "novox/app", branch: "main" }, { branchHead: async () => SHA, appliedProtection: async () => { throw new Error("read"); } }, (a) => [a.head, a.protected, a.error], [SHA, undefined, undefined]],
|
||||
["branch", "no such branch", { repository: "novox/app", branch: "nope" }, { branchHead: gone }, (a) => [a.head, a["error-kind"]], [undefined, "not-found"]],
|
||||
["open-pulls", "one", {}, {}, (a) => [a.pulls.length, a.pulls[0].required], [1, ["mesh/check"]]],
|
||||
["open-pulls", "forge down", {}, { openPulls: down }, (a) => [a.pulls, a["error-kind"]], [undefined, "failed"]],
|
||||
|
||||
@@ -16,8 +16,8 @@ test("the states the module announces are the states its manifest declares", asy
|
||||
|
||||
test("the announcement has the layout mesh-sdk's go/announce reads: version 1, the module, the node, the states sorted by name", async () => {
|
||||
const { statesAnnouncement } = await import("../kept.ts");
|
||||
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", "ace"), { v: 1, module: "gitea", node: "ace", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
assert.deepEqual(statesAnnouncement("gitea", undefined), { v: 1, module: "gitea", states: [{ name: "announced" }, { name: "bound", readable: true }, { name: "calls" }, { name: "failed-steps" }, { name: "findings", readable: true }, { name: "first-views" }, { name: "idle" }, { name: "transfers" }] });
|
||||
});
|
||||
|
||||
test("index.ts says the announcement as the event `states` before the tools are served", () => {
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
// writes nothing at the forge, true puts main's rule right as before.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { keepMainRules, mainRulesStep, LEFT_AS_SET } from "../dist/mainrules.js";
|
||||
import { keepMainRules } from "../dist/mainrules.js";
|
||||
|
||||
function env(body?: string): NodeJS.ProcessEnv {
|
||||
if (body === undefined) return {};
|
||||
@@ -15,6 +15,14 @@ function env(body?: string): NodeJS.ProcessEnv {
|
||||
return { MESH_GITEA_CONFIG_FILE: f };
|
||||
}
|
||||
|
||||
// No timer keeps main's rules (design 47 H §764, H-A-100): the setting's description says when it acts, and names none.
|
||||
test("the setting's description names no timer", () => {
|
||||
const manifest = JSON.parse(readFileSync(new URL("../module.json", import.meta.url), "utf8"));
|
||||
const said: string = manifest.config["keep-main-rules"].description;
|
||||
assert.match(said, /token is minted/);
|
||||
assert.doesNotMatch(said, /every ten minutes|every \w+ minutes/);
|
||||
});
|
||||
|
||||
test("the setting is false unless the config says true", () => {
|
||||
assert.equal(keepMainRules(env()), false);
|
||||
assert.equal(keepMainRules(env("{}")), false);
|
||||
@@ -23,25 +31,6 @@ test("the setting is false unless the config says true", () => {
|
||||
assert.equal(keepMainRules(env('{"keep-main-rules":true}')), true);
|
||||
});
|
||||
|
||||
test("false: the step never writes and says once that the rules are left", async () => {
|
||||
let writes = 0;
|
||||
const said: string[] = [];
|
||||
const step = mainRulesStep(false, async () => { writes++; return ["a"]; }, (l) => said.push(l));
|
||||
await step(); await step(); await step();
|
||||
assert.equal(writes, 0);
|
||||
assert.deepEqual(said, [LEFT_AS_SET]);
|
||||
assert.match(LEFT_AS_SET, /main's rules are left as set at the forge \(keep-main-rules is false\)/);
|
||||
});
|
||||
|
||||
test("true: every step puts the rules right", async () => {
|
||||
let writes = 0;
|
||||
const said: string[] = [];
|
||||
const step = mainRulesStep(true, async () => { writes++; return ["a", "b"]; }, (l) => said.push(l));
|
||||
await step(); await step();
|
||||
assert.equal(writes, 2);
|
||||
assert.deepEqual(said, ["[gitea] main's rule is right in 2 repositories"]);
|
||||
});
|
||||
|
||||
test("a token mint's account setup writes no protection while keep-main-rules is false, and does when true", async () => {
|
||||
const { setUpToolsAccount } = await import("../dist/tools-account.js");
|
||||
for (const [keep, want] of [[false, 0], [true, 1]] as const) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// A state of the runtime in memory, for the tests of the merge calls (calls.ts, partials.ts).
|
||||
export function memory() {
|
||||
const kv = new Map<string, unknown>();
|
||||
return { kv, st: {
|
||||
get: async (k: string) => (kv.has(k) ? { key: k, value: structuredClone(kv.get(k)), revision: 1 } : null),
|
||||
put: async (k: string, v: unknown) => { kv.set(k, structuredClone(v)); return 1; },
|
||||
delete: async (k: string) => { kv.delete(k); },
|
||||
keys: async () => [...kv.keys()].sort(),
|
||||
watch: async () => {},
|
||||
} };
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
// **One merge call, every member judged before any is merged** (novox/hq design 47 H §734, §735; ADR 0682 §13; H-A-21,
|
||||
// H-A-22, H-A-47): the seat git's merge-pull takes pulls and by, judges each member (its head, its base a watched branch,
|
||||
// not merged already, no required step failed at its fingerprint) and no branch protection, merges none when one is
|
||||
// refused, else merges them in the order named, and answers the call and each member's outcome once.
|
||||
|
||||
const A = "a".repeat(40), B = "b".repeat(40), MOVED = "c".repeat(40);
|
||||
const CALL = "0123456789abcdef";
|
||||
|
||||
interface FakePull { base: string; head: string; state: string; merged: boolean; merge?: string; refuse?: string }
|
||||
|
||||
/** A forge of pull requests by `<repository>#<number>`, recording each merge asked. It has no protection API: a call that
|
||||
* read a branch's protection would throw. */
|
||||
function forge(pulls: Record<string, FakePull>) {
|
||||
const merges: string[] = [];
|
||||
const key = (o: string, r: string, n: number) => `${o}/${r}#${n}`;
|
||||
return {
|
||||
merges,
|
||||
getPullRequest: async (o: string, r: string, n: number) => {
|
||||
const p = pulls[key(o, r, n)];
|
||||
if (!p) throw new Error(`Gitea API GET /repos/${o}/${r}/pulls/${n}: 404 {}`);
|
||||
return { number: n, title: "", state: p.state, merged: p.merged, base: p.base, head: "b", head_sha: p.head,
|
||||
...(p.merged ? { merge_commit_sha: p.merge ?? "m".repeat(40) } : {}) } as any;
|
||||
},
|
||||
mergePullRequest: async (o: string, r: string, n: number, _m: string, _d: boolean, head: string) => {
|
||||
const p = pulls[key(o, r, n)];
|
||||
merges.push(key(o, r, n));
|
||||
if (p.refuse) throw new Error(`Gitea API POST /repos/${o}/${r}/pulls/${n}/merge: 409 ${p.refuse}`);
|
||||
assert.equal(head, p.head);
|
||||
p.merged = true;
|
||||
p.merge = `${n}`.repeat(40).slice(0, 40);
|
||||
},
|
||||
commitParents: async (_o: string, _r: string, sha: string) => [sha, "x"],
|
||||
};
|
||||
}
|
||||
|
||||
const watched = new Map([["novox/app", new Set(["main"])], ["novox/lib", new Set(["main"])]]);
|
||||
|
||||
async function call(f: ReturnType<typeof forge>, args: Record<string, unknown>, over: Record<string, unknown> = {}, caller = "person.jochen") {
|
||||
const { mergeCall } = await import("../dist/mergecall.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const deps = { registered: () => true, watched: () => watched, judge: async () => null, newCall: () => CALL, ...over };
|
||||
return runAs({ "mesh/caller": caller }, args, () => mergeCall(f as any, args, deps as any)) as Promise<any>;
|
||||
}
|
||||
|
||||
const two = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 5, head: A }, { repository: "novox/lib", number: 9, head: B }] };
|
||||
const open = (): Record<string, FakePull> => ({ "novox/app#5": { base: "main", head: A, state: "open", merged: false },
|
||||
"novox/lib#9": { base: "main", head: B, state: "open", merged: false } });
|
||||
|
||||
test("a merge call merges its members in the order named and answers each with its merge commit; one pull request is a list of one", async () => {
|
||||
const f = forge(open());
|
||||
const got = await call(f, two);
|
||||
assert.equal(got.call, CALL);
|
||||
assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"]);
|
||||
assert.deepEqual(got.members.map((m: any) => [m.repository, m.number, m.state, m.commit]),
|
||||
[["novox/app", 5, "merged", "5".repeat(40)], ["novox/lib", 9, "merged", "9".repeat(40)]]);
|
||||
const one = forge(open());
|
||||
const single = await call(one, { by: "lead@shanks", pulls: [two.pulls[0]] });
|
||||
assert.deepEqual([one.merges, single.members.length, single.members[0].state], [["novox/app#5"], 1, "merged"]);
|
||||
});
|
||||
|
||||
test("a member refused at judging merges none and names every refused member and why", async () => {
|
||||
const rows: { name: string; set: (p: Record<string, FakePull>) => void; over?: Record<string, unknown>; why: RegExp }[] = [
|
||||
{ name: "the second member's head moved", set: (p) => { p["novox/lib#9"].head = MOVED; }, why: /head-moved: its head is cccccccccccc, not the bbbbbbbbbbbb/ },
|
||||
{ name: "already merged", set: (p) => { Object.assign(p["novox/lib#9"], { merged: true, merge: "e".repeat(40) }); }, why: /already-merged: .* as e{40}/ },
|
||||
{ name: "closed", set: (p) => { p["novox/lib#9"].state = "closed"; }, why: /is closed/ },
|
||||
{ name: "a base no registration watches", set: (p) => { p["novox/lib#9"].base = "feat/x"; }, why: /its base feat\/x is no watched branch/ },
|
||||
{ name: "a required step failed at its head's fingerprint",
|
||||
set: () => {}, over: { judge: async (h: string) => (h === B ? { run: "main.7", module: "lib", step: "unit", fingerprint: "fp" } : null) },
|
||||
why: /the unit step of lib failed in run main\.7 at its build-input fingerprint fp/ },
|
||||
];
|
||||
for (const r of rows) {
|
||||
const pulls = open();
|
||||
r.set(pulls);
|
||||
const f = forge(pulls);
|
||||
const got = await call(f, two, r.over);
|
||||
assert.deepEqual(f.merges, [], `${r.name}: nothing merged`);
|
||||
assert.equal(got.members[0].state, "judged", r.name);
|
||||
assert.equal(got.members[1].state, "refused", r.name);
|
||||
assert.match(got.members[1].why, r.why, r.name);
|
||||
}
|
||||
});
|
||||
|
||||
test("a call is refused whole with no proved caller, no by, no registration or no pull request", async () => {
|
||||
const f = forge(open());
|
||||
await assert.rejects(call(f, two, {}, ""), /proved no caller account/);
|
||||
await assert.rejects(call(f, { pulls: two.pulls }), /by is the caller as who@node/);
|
||||
await assert.rejects(call(f, two, { registered: () => false }), /no module is registered yet/);
|
||||
await assert.rejects(call(f, { by: "lead@shanks", pulls: [] }), /one or more members/);
|
||||
await assert.rejects(call(f, { by: "lead@shanks", pulls: [two.pulls[0], two.pulls[0]] }), /named twice/);
|
||||
assert.deepEqual(f.merges, []);
|
||||
});
|
||||
|
||||
test("the forge refusing a member after every member was judged stops the call: the landed member merged, the rest refused", async () => {
|
||||
const pulls = open();
|
||||
pulls["novox/lib#9"].refuse = "merge conflict";
|
||||
const three = { by: "lead@shanks", pulls: [...two.pulls, { repository: "novox/app", number: 6, head: A }] };
|
||||
pulls["novox/app#6"] = { base: "main", head: A, state: "open", merged: false };
|
||||
const f = forge(pulls);
|
||||
const got = await call(f, three);
|
||||
assert.deepEqual(f.merges, ["novox/app#5", "novox/lib#9"], "the third is never asked");
|
||||
assert.deepEqual(got.members.map((m: any) => m.state), ["merged", "refused", "refused"]);
|
||||
assert.match(got.members[1].why, /the forge refused the merge: .*merge conflict/);
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { memory } from "./memstate.ts";
|
||||
|
||||
// **A member refused after every member was judged is said and raised** (novox/hq design 47 H §767; acceptance test 115;
|
||||
// H-A-81): a forge that refuses the second merge gives one merge-called with the first member and `refused`, and
|
||||
// merge-partial (scope delivery, id the call, urgent, resolver operator) raised; a later call landing the refused member
|
||||
// clears it, and forge_partial_clear clears one by hand after the hand-act log took it.
|
||||
|
||||
const A = "a".repeat(40), B = "b".repeat(40);
|
||||
|
||||
test("the forge refusing the second merge says what landed, raises merge-partial, and a later call landing it clears it", async () => {
|
||||
const { mergeCall } = await import("../dist/mergecall.js");
|
||||
const { Calls } = await import("../dist/calls.js");
|
||||
const { Partials, partialKey } = await import("../dist/partials.js");
|
||||
const { callTools } = await import("../dist/tools/calls.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const { st } = memory();
|
||||
const findings = memory();
|
||||
const calls = new Calls(st as any);
|
||||
const partials = new Partials(findings.st as any, calls);
|
||||
const said: any[] = [];
|
||||
const pulls: Record<number, any> = { 5: { head: A, merged: false, refuse: false }, 9: { head: B, merged: false, refuse: true } };
|
||||
const forge = {
|
||||
getPullRequest: async (_o: string, _r: string, n: number) => ({ number: n, state: "open", 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) => {
|
||||
if (pulls[n].refuse) throw new Error(`Gitea API POST /repos/novox/${r}/pulls/${n}/merge: 409 merge conflict`);
|
||||
pulls[n].merged = true;
|
||||
},
|
||||
commitParents: async () => [],
|
||||
};
|
||||
let clock = Date.parse("2026-11-20T03:00:00Z");
|
||||
const run = (call: string, numbers: number[]) => {
|
||||
const args = { by: "lead@shanks", pulls: numbers.map((n) => ({ repository: n === 5 ? "novox/app" : "novox/lib", number: n, head: pulls[n].head })) };
|
||||
return runAs({ "mesh/caller": "shanks.claude-code" }, args, () => mergeCall(forge as any, args, { registered: () => true,
|
||||
watched: () => new Map([["novox/app", new Set(["main"])], ["novox/lib", new Set(["main"])]]), judge: async () => null,
|
||||
newCall: () => call, calls, partials, say: async (_s: string, body: unknown) => { said.push(body); },
|
||||
now: () => new Date((clock += 1000)) } as any)) as Promise<any>;
|
||||
};
|
||||
const got = await run("00000000000000aa", [5, 9]);
|
||||
assert.deepEqual(got.members.map((m: any) => m.state), ["merged", "refused"]);
|
||||
assert.equal(said.length, 1);
|
||||
assert.deepEqual(said[0].members.map((m: any) => m.number), [5]);
|
||||
assert.deepEqual(said[0].refused.map((m: any) => [m.repository, m.number]), [["novox/lib", 9]]);
|
||||
assert.match(said[0].refused[0].why, /merge conflict/);
|
||||
const raised: any = findings.kv.get(partialKey("00000000000000aa"));
|
||||
assert.deepEqual([raised?.kind, raised?.subject, raised?.severity, raised?.resolver], ["merge-partial", { scope: "delivery", id: "00000000000000aa" }, "urgent", "operator"]);
|
||||
assert.match(raised.summary, /landed novox\/app#5 and not novox\/lib#9/);
|
||||
// A later call landing the refused member clears it.
|
||||
pulls[9].refuse = false;
|
||||
await run("00000000000000bb", [9]);
|
||||
assert.equal(findings.kv.has(partialKey("00000000000000aa")), false, "cleared by the later call");
|
||||
// By hand: written in the hand-act log first, then cleared.
|
||||
pulls[9] = { head: B, merged: false, refuse: true };
|
||||
pulls[5] = { head: A, merged: false, refuse: false };
|
||||
await run("00000000000000cc", [5, 9]);
|
||||
assert.equal(findings.kv.has(partialKey("00000000000000cc")), true);
|
||||
const acts: any[] = [];
|
||||
const tool = callTools(calls, partials, async () => {}, async (a) => { acts.push(a); }).find((t) => t.name === "forge_partial_clear")!;
|
||||
await assert.rejects(tool.run({ call: "00000000000000cc" }), /names the call .* and why/);
|
||||
const failing = callTools(calls, partials, async () => {}, async () => { throw new Error("down"); }).find((t) => t.name === "forge_partial_clear")!;
|
||||
await assert.rejects(failing.run({ call: "00000000000000cc", why: "dropped" }), /not recorded, so nothing was cleared/);
|
||||
assert.equal(findings.kv.has(partialKey("00000000000000cc")), true);
|
||||
const done: any = await runAs({ "mesh/caller": "person.jochen" }, {}, () => tool.run({ call: "00000000000000cc", why: "the lib change is dropped" }));
|
||||
assert.deepEqual([done.cleared, acts[0].verb, findings.kv.has(partialKey("00000000000000cc"))], [true, "forge_partial_clear", false]);
|
||||
});
|
||||
@@ -41,33 +41,6 @@ async function serve(count: number, heads: Record<number, string>) {
|
||||
return { client, ...f };
|
||||
}
|
||||
|
||||
test("a new head beyond the newest 20 open pull requests is announced, exactly once", async () => {
|
||||
const { headsToAnnounce } = await import("../pulls.ts");
|
||||
// 25 open, every head announced before; #3 — the 23rd most recently updated — got a new head.
|
||||
const announced: Record<string, string> = {};
|
||||
for (let n = 1; n <= 25; n++) announced[`novox/hq#${n}@main`] = sha(n, "a");
|
||||
const { client, server, asked } = await serve(25, { 3: sha(3, "b") });
|
||||
try {
|
||||
// The fault: the newest 20 alone do not hold it.
|
||||
const newest = await client.listPullRequests("novox", "hq", { state: "open", sort: "recentupdate", limit: "20" });
|
||||
assert.deepEqual(headsToAnnounce("novox/hq", newest, announced), [], "the newest 20 miss #3's new head");
|
||||
asked.length = 0;
|
||||
const said: string[] = [];
|
||||
for (let pass = 0; pass < 2; pass++) {
|
||||
// The sweep, as index.ts's pollPulls asks: no floor, every page.
|
||||
const open = await client.listOpenPullsSince("novox", "hq", "");
|
||||
for (const p of headsToAnnounce("novox/hq", open, announced)) {
|
||||
said.push(`#${p.number}@${p.head_sha}`);
|
||||
announced[`novox/hq#${p.number}@${p.base}`] = String(p.head_sha);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(said, [`#3@${sha(3, "b")}`], "the new head past the newest 20, announced once over two passes");
|
||||
assert.ok(asked.every((q) => q.get("state") === "open" && q.get("sort") === "recentupdate"), "asked newest first");
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("a pass pages until it reaches back past its floor, and no further", async () => {
|
||||
// 130 open pull requests, #n updated n hours after T0: page 1 holds #130–#81, page 2 #80–#31, page 3 #30–#1.
|
||||
const { client, server, asked } = await serve(130, {});
|
||||
|
||||
@@ -2,32 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { createServer } from "node:http";
|
||||
|
||||
// A pull request's merge check (novox/hq to-be 45 §9): each new head announced once, and the verdict set
|
||||
// where the pull request shows it — an error never as a success.
|
||||
|
||||
test("each open pull request's new head is announced once, and nothing closed or merged", async () => {
|
||||
const { headsToAnnounce } = await import("../pulls.ts");
|
||||
const pulls = [
|
||||
{ number: 1, title: "a", state: "open", merged: false, head_sha: "aaa", base: "main", html_url: "" },
|
||||
{ number: 2, title: "b", state: "open", merged: false, head_sha: "bbb", html_url: "" },
|
||||
{ number: 3, title: "c", state: "closed", merged: true, head_sha: "ccc", html_url: "" },
|
||||
{ number: 4, title: "d", state: "open", merged: false, html_url: "" },
|
||||
];
|
||||
const announced = { "novox/mesh-catalog#1@main": "aaa", "novox/mesh-catalog#2@": "old" };
|
||||
assert.deepEqual(headsToAnnounce("novox/mesh-catalog", pulls, announced).map((p) => p.number), [2],
|
||||
"only a head not announced, of a pull request that is open");
|
||||
});
|
||||
|
||||
test("a pull request retargeted keeps its head and is announced again, into its new base (de-spiegel #6)", async () => {
|
||||
const { headsToAnnounce, headKey } = await import("../pulls.ts");
|
||||
const was = { number: 6, title: "a", state: "open", merged: false, head_sha: "aaa", base: "old-trunk", html_url: "" };
|
||||
const announced: Record<string, string> = { [headKey("novox/de-spiegel", 6, "old-trunk")]: "aaa" };
|
||||
assert.deepEqual(headsToAnnounce("novox/de-spiegel", [was], announced), [], "the same head into the same base is announced once");
|
||||
const retargeted = { ...was, base: "main" };
|
||||
assert.deepEqual(headsToAnnounce("novox/de-spiegel", [retargeted], announced).map((p) => p.number), [6],
|
||||
"the same head into a new base is judged afresh");
|
||||
assert.notEqual(headKey("novox/de-spiegel", 6, "main"), headKey("novox/de-spiegel", 6, "old-trunk"));
|
||||
});
|
||||
// A commit status, set where the pull request shows it (the seat git's status).
|
||||
|
||||
test("a commit status is set on the commit, under the merge check's context", async () => {
|
||||
const { GiteaClient } = await import("../client.ts");
|
||||
@@ -53,35 +28,3 @@ test("a commit status is set on the commit, under the merge check's context", as
|
||||
assert.equal(seen!.body.state, "failure");
|
||||
assert.equal(seen!.body.context, "mesh/merge-gate");
|
||||
});
|
||||
|
||||
// **A pending check says where it is** (novox/hq issue 446): announced, it waits in the build queue, since the time the
|
||||
// operator reads — not "mapping this head onto its module graph" for the ten minutes of a controller check.
|
||||
test("a head announced is pending in the build queue, since the local time it was announced", async () => {
|
||||
const { pendingWords } = await import("../pulls.ts");
|
||||
assert.equal(pendingWords(new Date(2026, 9, 11, 3, 8, 50)), "waiting in the build queue since 03:08");
|
||||
assert.equal(pendingWords(new Date(2026, 9, 11, 14, 0, 0)), "waiting in the build queue since 14:00");
|
||||
});
|
||||
|
||||
// Issue 1301 (ADR 0503 §3, §5): gitea writes the controller's statuses only where the base still requires them. hq's main
|
||||
// requires mesh/repo-check alone, so no mesh/merge-gate is written on its heads.
|
||||
test("a base's required statuses decide which of the controller's statuses are written", async () => {
|
||||
const { maintained, writesGate, CHECK_STATUS } = await import("../pulls.ts");
|
||||
assert.equal(CHECK_STATUS, "mesh/check");
|
||||
assert.deepEqual(maintained(undefined), ["mesh/merge-gate", "mesh/repo-check"], "a rule not read: as before");
|
||||
assert.deepEqual(maintained(["mesh/repo-check"]), ["mesh/repo-check"]);
|
||||
assert.deepEqual(maintained(["mesh/check", "other/x"]), []);
|
||||
assert.deepEqual(maintained([]), []);
|
||||
assert.equal(writesGate(undefined), true);
|
||||
assert.equal(writesGate(["mesh/repo-check"]), false);
|
||||
assert.equal(writesGate(["mesh/check"]), false);
|
||||
assert.equal(writesGate(["mesh/merge-gate", "mesh/repo-check"]), true);
|
||||
});
|
||||
|
||||
test("a rule the forge gives with no contexts (null) requires nothing, and an unread rule is unknown", async () => {
|
||||
const { requiredOf } = await import("../pulls.ts");
|
||||
// The forge's BranchProtection says status_check_contexts may be null (protection.ts); requiredOf takes it as it comes.
|
||||
assert.deepEqual(requiredOf({ enable_status_check: true, status_check_contexts: null }), []);
|
||||
assert.deepEqual(requiredOf({ enable_status_check: true, status_check_contexts: ["mesh/check", ""] }), ["mesh/check"]);
|
||||
assert.deepEqual(requiredOf(null), []);
|
||||
assert.equal(requiredOf(undefined), undefined);
|
||||
});
|
||||
|
||||
@@ -1,273 +0,0 @@
|
||||
// **A delivery group's member is judged beside its siblings, and a sibling's move puts that verdict out of date**
|
||||
// (novox/hq ADR 0382 §4-§6, issue 867): the holder keeps the siblings each verdict names; a sibling's new head, close
|
||||
// unmerged or base move (other than its own merge of the head that was read) sets both statuses of every member whose
|
||||
// verdict names it back to pending, and runs nothing; a member merges only with its group passed and every sibling head
|
||||
// read approved.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { approvedHeadsOf, besideWords, groupRefusal, keepSiblings, namesSiblings, readSiblings, siblingOutOfDate, siblingsFile,
|
||||
siblingsOf, siblingStatuses, weighSiblings, type SiblingRecord } from "../dist/siblings.js";
|
||||
import { weighTrunks } from "../dist/trunk.js";
|
||||
|
||||
const h = (c: string) => c.repeat(40);
|
||||
const MEMBER_HEAD = h("a"), HOST_HEAD = h("b"), HOST_BASE = h("c"), HOST_NEW = h("d"), HOST_MOVED = h("e"), MERGE = h("f");
|
||||
const sibling = { repository: "novox/mesh-host", number: 114, head: HOST_HEAD, base: "main", base_commit: HOST_BASE };
|
||||
const verdict = { owner: "novox", repo: "mesh-controller", number: 346, commit: MEMBER_HEAD, verdict: "pass", summary: "x", id: "build-1",
|
||||
trunk: h("1").slice(0, 12), siblings: [sibling], judge_from: "sibling novox/mesh-controller#346 merge 123456789abc",
|
||||
gate: { verdict: "pass", summary: "every machine composes ".repeat(10) }, "repo-check": { verdict: "pass", summary: "ok" } };
|
||||
|
||||
test("the siblings a verdict names are kept per member, and read back from where the holder keeps its state", () => {
|
||||
const file = siblingsFile(mkdtempSync(join(tmpdir(), "gitea-siblings-")));
|
||||
const record: SiblingRecord = { "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } };
|
||||
keepSiblings(file, record);
|
||||
assert.deepEqual(readSiblings(file), record, "kept where the holder keeps its state");
|
||||
assert.deepEqual(readSiblings(null), {});
|
||||
});
|
||||
|
||||
// --- a sibling's move -----------------------------------------------------------------------------------------
|
||||
|
||||
interface World {
|
||||
host: { merged: boolean; state: string; head_sha: string; merge_commit_sha?: string };
|
||||
hostBase: string;
|
||||
parents: string[];
|
||||
member: { head_sha: string; state: string; merged: boolean };
|
||||
checks: Record<string, { state: string; description?: string }>;
|
||||
written: { sha: string; context: string; state: string; description: string }[];
|
||||
reads: string[];
|
||||
}
|
||||
const world = (): World => ({ host: { merged: false, state: "open", head_sha: HOST_HEAD }, hostBase: HOST_BASE, parents: [],
|
||||
member: { head_sha: MEMBER_HEAD, state: "open", merged: false },
|
||||
checks: { "mesh/merge-gate": { state: "success", description: "head aaaaaaaaaaaa merged into trunk 111111111111 beside mesh-host#114@bbbbbbbb — pass: x" },
|
||||
"mesh/repo-check": { state: "success", description: "pass: y" } },
|
||||
written: [], reads: [] });
|
||||
const forgeOf = (w: World) => ({
|
||||
async getPullRequest(owner: string, repo: string, n: number) {
|
||||
w.reads.push(`${owner}/${repo}#${n}`);
|
||||
return repo === "mesh-host" ? { number: n, title: "", html_url: "", ...w.host } : { number: n, title: "", html_url: "", merged: false, ...w.member };
|
||||
},
|
||||
async branchHead(_o: string, _r: string, _b: string) { return w.hostBase; },
|
||||
async commitParents() { return w.parents; },
|
||||
});
|
||||
const gatesOf = (w: World) => ({
|
||||
async outOfDate(_o: string, _r: string, sha: string, decide: (c: any) => any[] | Promise<any[]>) {
|
||||
const put = await decide(w.checks);
|
||||
for (const s of put) { w.written.push({ sha, ...s }); w.checks[s.context] = { state: s.state, description: s.description }; }
|
||||
return put;
|
||||
},
|
||||
});
|
||||
const recordOf = (): SiblingRecord => ({ "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } });
|
||||
const weigh = (w: World, record: SiblingRecord, repository = "novox/mesh-host", number?: number) =>
|
||||
weighSiblings(forgeOf(w) as any, gatesOf(w) as any, record, repository, number);
|
||||
|
||||
test("a sibling's new head puts both statuses of the member back to pending, with the reason, and runs nothing", async () => {
|
||||
const w = world();
|
||||
w.host.head_sha = HOST_NEW;
|
||||
await weigh(w, recordOf(), "novox/mesh-host", 114);
|
||||
assert.deepEqual(w.written.map((s) => [s.sha, s.context, s.state]),
|
||||
[[MEMBER_HEAD, "mesh/merge-gate", "pending"], [MEMBER_HEAD, "mesh/repo-check", "pending"]]);
|
||||
assert.equal(w.written[0].description,
|
||||
"out of date: judged beside mesh-host#114 at bbbbbbbbbbbb, it is now at dddddddddddd; judged again on a recheck or when it is merged");
|
||||
assert.ok(siblingOutOfDate(w.written[0].description));
|
||||
// Already pending (out of date, or a check on its way): not written again.
|
||||
w.written.length = 0;
|
||||
await weigh(w, recordOf(), "novox/mesh-host", 114);
|
||||
assert.deepEqual(w.written, []);
|
||||
});
|
||||
|
||||
test("a sibling closed unmerged puts the member's verdict out of date", async () => {
|
||||
const w = world();
|
||||
w.host.state = "closed";
|
||||
await weigh(w, recordOf(), "novox/mesh-host", 114);
|
||||
assert.equal(w.written.length, 2);
|
||||
assert.match(w.written[0].description, /^out of date: judged beside mesh-host#114 was closed unmerged/);
|
||||
});
|
||||
|
||||
test("a move of the sibling's base puts it out of date, naming both commits", async () => {
|
||||
const w = world();
|
||||
w.hostBase = HOST_MOVED;
|
||||
await weigh(w, recordOf());
|
||||
assert.equal(w.written.length, 2);
|
||||
assert.match(w.written[0].description, /mesh-host#114 merged onto cccccccccccc, its main is now eeeeeeeeeeee/);
|
||||
});
|
||||
|
||||
test("the sibling's own merge of the head read onto the base read does not, in either order of the two merges", async () => {
|
||||
// The sibling merges first (a merge commit of the base commit read and the head read).
|
||||
const w = world();
|
||||
Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE });
|
||||
w.hostBase = MERGE;
|
||||
w.parents = [HOST_BASE, HOST_HEAD];
|
||||
const record = recordOf();
|
||||
assert.equal(await weigh(w, record), true, "the record changes: the sibling is marked merged");
|
||||
assert.deepEqual(w.written, []);
|
||||
assert.equal(record["novox/mesh-controller#346"].siblings[0].merged, true);
|
||||
// A move of that base afterwards is no sibling's move: the sibling is merged.
|
||||
w.hostBase = HOST_MOVED;
|
||||
await weigh(w, record);
|
||||
assert.deepEqual(w.written, []);
|
||||
|
||||
// The member merges first: its own repository moves, which no member's verdict names as a sibling.
|
||||
const m = world();
|
||||
m.member.merged = true;
|
||||
await weigh(m, recordOf(), "novox/mesh-controller");
|
||||
assert.deepEqual(m.written, []);
|
||||
// And the sibling then merges: still not a move.
|
||||
Object.assign(m.host, { merged: true, state: "closed", merge_commit_sha: MERGE });
|
||||
m.parents = [HOST_BASE, HOST_HEAD];
|
||||
await weigh(m, recordOf());
|
||||
assert.deepEqual(m.written, []);
|
||||
|
||||
// A squash is one parent: the base commit read.
|
||||
const q = world();
|
||||
Object.assign(q.host, { merged: true, state: "closed", merge_commit_sha: MERGE });
|
||||
q.parents = [HOST_BASE];
|
||||
await weigh(q, recordOf());
|
||||
assert.deepEqual(q.written, []);
|
||||
});
|
||||
|
||||
test("a merge of another head, or onto another base, is a move", async () => {
|
||||
const other = world();
|
||||
Object.assign(other.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW });
|
||||
other.parents = [HOST_BASE, HOST_NEW];
|
||||
await weigh(other, recordOf());
|
||||
assert.equal(other.written.length, 2);
|
||||
const onto = world();
|
||||
Object.assign(onto.host, { merged: true, state: "closed", merge_commit_sha: MERGE });
|
||||
onto.parents = [HOST_MOVED, HOST_HEAD];
|
||||
await weigh(onto, recordOf());
|
||||
assert.equal(onto.written.length, 2);
|
||||
});
|
||||
|
||||
test("only the members whose verdict names the sibling are touched, and a member that moved on is dropped", async () => {
|
||||
// Another sibling's move, or another repository's: nothing read, nothing written.
|
||||
const w = world();
|
||||
w.host.head_sha = HOST_NEW;
|
||||
await weigh(w, recordOf(), "novox/mesh-lab");
|
||||
await weigh(w, recordOf(), "novox/mesh-host", 999);
|
||||
assert.deepEqual(w.written, []);
|
||||
// A verdict that is none (a gate not yet judged) is left alone.
|
||||
const none = world();
|
||||
none.host.head_sha = HOST_NEW;
|
||||
none.checks = {};
|
||||
await weigh(none, recordOf());
|
||||
assert.deepEqual(none.written, []);
|
||||
// The member has a new head: its old verdict is no verdict; the record is dropped, nothing written to the old head.
|
||||
const moved = world();
|
||||
moved.host.head_sha = HOST_NEW;
|
||||
moved.member.head_sha = h("9");
|
||||
const record = recordOf();
|
||||
assert.equal(await weigh(moved, record), true);
|
||||
assert.deepEqual(moved.written, []);
|
||||
assert.deepEqual(record, {});
|
||||
// A repository without a repo-check gets no pending one a verdict would never set.
|
||||
assert.deepEqual(siblingStatuses({ "mesh/merge-gate": { state: "success" } }, "x").map((s) => s.context), ["mesh/merge-gate"]);
|
||||
});
|
||||
|
||||
test("a trunk that moved tells the siblings' weighing; one first seen does not", async () => {
|
||||
const told: string[] = [];
|
||||
const forge = { listOpenPulls: async () => [{ number: 1, title: "", state: "open", merged: false, head_sha: h("2"), base: "main", html_url: "" }],
|
||||
branchHead: async () => h("3") };
|
||||
const gates = { outOfDate: async () => [] };
|
||||
const trunks: Record<string, string> = {};
|
||||
await weighTrunks(forge as any, gates as any, trunks, "novox", "mesh-host", async (b) => { told.push(b); });
|
||||
assert.deepEqual(told, [], "first seen");
|
||||
trunks["novox/mesh-host:main"] = h("4");
|
||||
await weighTrunks(forge as any, gates as any, trunks, "novox", "mesh-host", async (b) => { told.push(b); });
|
||||
assert.deepEqual(told, ["main"]);
|
||||
});
|
||||
|
||||
// --- the merge rule -------------------------------------------------------------------------------------------
|
||||
|
||||
test("the group rule names every missing thing: the group status at each head read, and each head the Approve does not cover", async () => {
|
||||
const self = { owner: "novox", repo: "mesh-controller", number: 346, head: MEMBER_HEAD };
|
||||
const statuses: Record<string, string> = {};
|
||||
const forge = {
|
||||
headChecks: async (_o: string, r: string, sha: string) => (statuses[`${r}@${sha}`] ? { "mesh/delivery-group": { state: statuses[`${r}@${sha}`] } } : {}),
|
||||
};
|
||||
const siblings = siblingsOf([sibling]);
|
||||
const none = await groupRefusal(forge, self, siblings, undefined);
|
||||
assert.match(none!, /mesh\/delivery-group is missing at mesh-controller#346@aaaaaaaaaaaa; mesh-controller#346@aaaaaaaaaaaa is covered by no Approve: the merge was handed no approved heads; mesh\/delivery-group is missing at mesh-host#114@bbbbbbbbbbbb; mesh-host#114@bbbbbbbbbbbb is covered by no Approve/);
|
||||
statuses[`mesh-controller@${MEMBER_HEAD}`] = "success";
|
||||
statuses[`mesh-host@${HOST_HEAD}`] = "pending";
|
||||
const approved = approvedHeadsOf([`novox/mesh-controller@${MEMBER_HEAD}`])!;
|
||||
assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh\/delivery-group is pending at mesh-host#114@bbbbbbbbbbbb; mesh-host#114@bbbbbbbbbbbb is not among the heads the Approve covers$/);
|
||||
statuses[`mesh-host@${HOST_HEAD}`] = "success";
|
||||
assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh-host#114@bbbbbbbbbbbb is not among the heads the Approve covers$/);
|
||||
approved.set("novox/mesh-host", "c".repeat(40));
|
||||
assert.match((await groupRefusal(forge, self, siblings, approved))!, /^mesh-host#114@bbbbbbbbbbbb is not the head the Approve covers \(cccccccccccc\)$/);
|
||||
approved.set("novox/mesh-host", HOST_HEAD.toLowerCase());
|
||||
assert.equal(await groupRefusal(forge, self, siblings, approved), undefined);
|
||||
// A read that fails is named, never read as a pass.
|
||||
const broken = { headChecks: async () => { throw new Error("the forge is away"); } };
|
||||
assert.match((await groupRefusal(broken, self, siblings, approved))!, /could not be read \(the forge is away\)/);
|
||||
assert.equal(besideWords(siblings), "beside mesh-host#114@bbbbbbbb");
|
||||
});
|
||||
|
||||
test("a merged or closed member's entry is dropped, so a sibling's later push never re-pends its merged head", async () => {
|
||||
// mesh-host#114 judged beside mesh-controller#346; #114 then merged; a push to #346 follows.
|
||||
const w = world();
|
||||
w.host.merged = true; // the member (mesh-host#114) is merged
|
||||
w.member.head_sha = h("9"); // the sibling (mesh-controller#346) has a new head
|
||||
const record: SiblingRecord = { "novox/mesh-host#114": { head: HOST_HEAD, siblings: siblingsOf([
|
||||
{ repository: "novox/mesh-controller", number: 346, head: MEMBER_HEAD, base: "main", base_commit: h("2") }]) } };
|
||||
assert.equal(await weigh(w, record, "novox/mesh-controller", 346), true);
|
||||
assert.deepEqual(w.written, [], "the merged head is not put back to pending");
|
||||
assert.deepEqual(record, {}, "and its entry is gone");
|
||||
// On its own close or merge the member's entry goes too, whatever the siblings do.
|
||||
const own = world();
|
||||
own.host.state = "closed";
|
||||
const kept = recordOf();
|
||||
const mine: SiblingRecord = { "novox/mesh-host#114": { head: HOST_HEAD, siblings: kept["novox/mesh-controller#346"].siblings } };
|
||||
assert.equal(await weigh(own, mine, "novox/mesh-host", 114), true);
|
||||
assert.deepEqual(mine, {});
|
||||
assert.deepEqual(own.written, []);
|
||||
});
|
||||
|
||||
// --- the composed verdict names the siblings of each member (issue 1049) --------------------------------------
|
||||
|
||||
// Issue 1049: the 2026-10-17 23:48 group (mesh-controller #375 beside mesh-catalog #353). #375's own verdict named #353
|
||||
// at its old head; #353 then got a new head, and the group's composed verdict passed for both new heads. The record still
|
||||
// held the old head, so #353's own merge of the NEW head read as "merged otherwise than its head" and put #375's gate back
|
||||
// to pending, and the group merged in two passes.
|
||||
const composedMembers = () => [
|
||||
{ owner: "novox", repo: "mesh-controller", number: 346, commit: MEMBER_HEAD, trunk: "111111111111", base: "main" },
|
||||
{ owner: "novox", repo: "mesh-host", number: 114, commit: HOST_NEW, trunk: HOST_BASE.slice(0, 12), base: "main" },
|
||||
];
|
||||
/** The record a group's composed verdict once left: for each member, the other members at the heads and base commits it composed. */
|
||||
const composedRecord = (members: ReturnType<typeof composedMembers>): SiblingRecord => Object.fromEntries(members.map((m) => [
|
||||
`${m.owner}/${m.repo}#${m.number}`.toLowerCase(), { head: m.commit, siblings: members.filter((o) => o !== m).map((o) => ({
|
||||
repository: `${o.owner}/${o.repo}`, number: o.number, head: o.commit, base: o.base, base_commit: o.trunk })) }]));
|
||||
const staleRecord = (): SiblingRecord => ({ "novox/mesh-controller#346": { head: MEMBER_HEAD, siblings: siblingsOf([sibling]) } });
|
||||
|
||||
test("without the composed verdict's siblings, a sibling's own merge of its new head is a move (the pin of issue 1049)", async () => {
|
||||
const w = world();
|
||||
Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW });
|
||||
w.parents = [HOST_BASE, HOST_NEW];
|
||||
await weigh(w, staleRecord());
|
||||
assert.equal(w.written.length, 2, "the record names the old head: the merge reads as another head's");
|
||||
});
|
||||
|
||||
test("a composed verdict names the siblings of each member at the heads and base commits it composed, so a sibling's own merge of its new head is no move (issue 1049)", async () => {
|
||||
const record = composedRecord(composedMembers());
|
||||
assert.deepEqual(record["novox/mesh-controller#346"], { head: MEMBER_HEAD,
|
||||
siblings: [{ repository: "novox/mesh-host", number: 114, head: HOST_NEW, base: "main", base_commit: HOST_BASE.slice(0, 12) }] });
|
||||
assert.deepEqual(record["novox/mesh-host#114"], { head: HOST_NEW,
|
||||
siblings: [{ repository: "novox/mesh-controller", number: 346, head: MEMBER_HEAD, base: "main", base_commit: "111111111111" }] });
|
||||
const w = world();
|
||||
Object.assign(w.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW });
|
||||
w.parents = [HOST_BASE, HOST_NEW];
|
||||
await weigh(w, record);
|
||||
assert.deepEqual(w.written, [], "its own merge of the head the composed check judged leaves the member's gate alone");
|
||||
assert.equal(record["novox/mesh-controller#346"].siblings[0].merged, true);
|
||||
// A merge of any other head, or onto any other base, is still a move.
|
||||
const other = world();
|
||||
const again = composedRecord(composedMembers());
|
||||
Object.assign(other.host, { merged: true, state: "closed", merge_commit_sha: MERGE, head_sha: HOST_NEW });
|
||||
other.parents = [HOST_MOVED, HOST_NEW];
|
||||
await weigh(other, again);
|
||||
assert.equal(other.written.length, 2);
|
||||
});
|
||||
@@ -487,9 +487,9 @@ test("a glob rule covering main gives its approvals and override, not its status
|
||||
assert.deepEqual(rule.merge_whitelist_usernames, [TOOLS_ACCOUNT]);
|
||||
});
|
||||
|
||||
test("a start with the token kept still puts main's rules right: the module's start asks for it each time (issue 948, review round 1)", async () => {
|
||||
const src = await readFile(new URL("../index.ts", import.meta.url), "utf8");
|
||||
assert.match(src, /ensureMainMerges\("novox"\)/, "index.ts calls it outside the token's mint");
|
||||
// The module's start no longer puts main's rules right every ten minutes (design 47 H §764; H-A-100): ensureMainMerges is
|
||||
// the mint's alone.
|
||||
test("ensureMainMerges makes a main rule gone again with no token minted", async () => {
|
||||
const { GiteaAdmin } = await import("../dist/client.js");
|
||||
const env = await environment();
|
||||
forge.mainRules.delete("hq");
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
// **A merge check's verdict holds for the trunk commit it judged** (novox/hq ADR 0323, issue 341): both statuses name
|
||||
// the head and the trunk commit; a trunk's move puts the merge check of every open pull request into that trunk — and
|
||||
// of none into another — back to pending with the reason, and runs nothing; a verdict that lands judged against a trunk
|
||||
// that has moved since is said out of date.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { checkOnItsWay, outOfDateStatuses, outOfDateWords, trunkNamed, verdictAtTrunk, weighTrunks } from "../dist/trunk.js";
|
||||
import { GateWriter } from "../dist/gate.js";
|
||||
|
||||
const HEAD = "dfb1415aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const TRUNK = "a2c2c35bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const MOVED = "d28ca95ccccccccccccccccccccccccccccccccc";
|
||||
|
||||
test("a trunk's move puts an open head's merge check out of date, naming both commits, unless a check is on its way", () => {
|
||||
const verdict = (trunk: string, state = "success") => ({
|
||||
"mesh/merge-gate": { state, description: `head ${HEAD.slice(0, 12)} merged into trunk ${trunk.slice(0, 12)} — pass: x` },
|
||||
"mesh/repo-check": { state, description: `head ${HEAD.slice(0, 12)} merged into trunk ${trunk.slice(0, 12)} — pass: y` } });
|
||||
const put = outOfDateStatuses(verdict(TRUNK), MOVED);
|
||||
assert.deepEqual(put.map((s) => [s.context, s.state]), [["mesh/merge-gate", "pending"], ["mesh/repo-check", "pending"]]);
|
||||
assert.equal(put[0].description, "out of date: judged against trunk a2c2c35bbbbb, the trunk is now d28ca95ccccc; " +
|
||||
"judged again on a recheck or when it is merged");
|
||||
assert.equal(put[0].description, outOfDateWords(TRUNK, MOVED));
|
||||
assert.equal(trunkNamed(put[0].description), "a2c2c35bbbbb", "an out-of-date status still names what it judged");
|
||||
// A failure is out of date too: it judged another merge.
|
||||
assert.equal(outOfDateStatuses(verdict(TRUNK, "failure"), MOVED).length, 2);
|
||||
// Already judged against the trunk's head; a check on its way or already out of date; no check yet: left alone.
|
||||
assert.deepEqual(outOfDateStatuses(verdict(MOVED), MOVED), []);
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": { state: "pending", description: "building on ace since 14:02" } }, MOVED), []);
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": { state: "pending", description: put[0].description } }, MOVED), []);
|
||||
assert.deepEqual(outOfDateStatuses({}, MOVED), []);
|
||||
// A head with no repository check of its own gets no pending one a verdict would never set.
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": verdict(TRUNK)["mesh/merge-gate"] }, MOVED).map((s) => s.context),
|
||||
["mesh/merge-gate"]);
|
||||
// A verdict naming no trunk, of a build seat from before the rule, judged an earlier trunk.
|
||||
assert.match(outOfDateStatuses({ "mesh/merge-gate": { state: "success", description: "pass: x" } }, MOVED)[0].description,
|
||||
/judged against a trunk it does not name, the trunk is now d28ca95ccccc/);
|
||||
|
||||
assert.ok(checkOnItsWay({ state: "pending", description: "waiting in the build queue since 14:00" }));
|
||||
assert.ok(checkOnItsWay({ state: "pending", description: "checking again: a person asked" }));
|
||||
assert.ok(!checkOnItsWay({ state: "pending", description: put[0].description }));
|
||||
assert.deepEqual(verdictAtTrunk(verdict(MOVED), MOVED), { current: true, onItsWay: false, judged: "d28ca95ccccc" });
|
||||
assert.equal(verdictAtTrunk(verdict(TRUNK), MOVED).current, false);
|
||||
});
|
||||
|
||||
/** A forge holding open pull requests into two branches, each head's statuses, and what was written. */
|
||||
function aForge(pulls: { number: number; base: string; head: string }[], heads: Record<string, string>) {
|
||||
const statuses = new Map<string, Record<string, { state: string; description?: string }>>();
|
||||
const writes: string[] = [];
|
||||
const verdict = (trunk: string) => ({
|
||||
"mesh/merge-gate": { state: "success", description: `head x merged into trunk ${trunk.slice(0, 12)} — pass` },
|
||||
"mesh/repo-check": { state: "success", description: `head x merged into trunk ${trunk.slice(0, 12)} — pass` } });
|
||||
for (const p of pulls) statuses.set(p.head, verdict(heads[p.base]));
|
||||
const forge = {
|
||||
listOpenPulls: async () => pulls.map((p) => ({ number: p.number, title: "t", state: "open", merged: false, base: p.base,
|
||||
head: "feat", head_sha: p.head, html_url: `http://forge/pulls/${p.number}` })),
|
||||
branchHead: async (_o: string, _r: string, branch: string) => heads[branch],
|
||||
setCommitStatus: async (_o: string, _r: string, sha: string, s: { context: string; state: string; description: string }) => {
|
||||
writes.push(`${sha} ${s.context} ${s.state}`);
|
||||
statuses.set(sha, { ...statuses.get(sha), [s.context]: { state: s.state, description: s.description } });
|
||||
},
|
||||
headChecks: async (_o: string, _r: string, sha: string) => statuses.get(sha) ?? {},
|
||||
};
|
||||
return { forge, writes, statuses };
|
||||
}
|
||||
|
||||
test("a push to a trunk puts every open pull request into it out of date — twenty-five of them — and none into another", async () => {
|
||||
const heads: Record<string, string> = { main: TRUNK, release: "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" };
|
||||
const pulls = Array.from({ length: 25 }, (_, i) => ({ number: i + 1, base: "main", head: `${String(i).padStart(2, "0")}`.repeat(20) }));
|
||||
pulls.push({ number: 99, base: "release", head: "9".repeat(40) });
|
||||
const { forge, writes, statuses } = aForge(pulls, heads);
|
||||
const gates = new GateWriter(forge);
|
||||
const trunks: Record<string, string> = {};
|
||||
|
||||
// First sight: each trunk is taken as it is, and nothing is put out of date.
|
||||
assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), true);
|
||||
assert.deepEqual(writes, []);
|
||||
assert.equal(trunks["novox/mesh-host:main"], TRUNK);
|
||||
// Nothing moved: nothing read past the trunks, nothing written.
|
||||
assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), false);
|
||||
|
||||
heads.main = MOVED;
|
||||
assert.equal(await weighTrunks(forge, gates, trunks, "novox", "mesh-host"), true);
|
||||
assert.equal(writes.length, 50, "both statuses of each of the 25 pull requests into main");
|
||||
for (const p of pulls.filter((p) => p.base === "main")) {
|
||||
const s = statuses.get(p.head)!;
|
||||
assert.equal(s["mesh/merge-gate"].state, "pending");
|
||||
assert.match(s["mesh/merge-gate"].description!, /judged against trunk a2c2c35bbbbb, the trunk is now d28ca95ccccc/);
|
||||
assert.equal(s["mesh/repo-check"].state, "pending");
|
||||
}
|
||||
assert.equal(statuses.get("9".repeat(40))!["mesh/merge-gate"].state, "success", "a pull request into another branch is left");
|
||||
assert.equal(trunks["novox/mesh-host:main"], MOVED);
|
||||
|
||||
// Weighed again, nothing more: the move was taken once, and the out-of-date statuses are left as they are.
|
||||
writes.length = 0;
|
||||
heads.main = "f".repeat(40);
|
||||
await weighTrunks(forge, gates, trunks, "novox", "mesh-host");
|
||||
assert.deepEqual(writes, [], "a check already out of date is not written again");
|
||||
});
|
||||
|
||||
test("a trunk that cannot be read is weighed again on the next pass, never recorded half", async () => {
|
||||
const heads: Record<string, string> = { main: TRUNK };
|
||||
const { forge } = aForge([{ number: 1, base: "main", head: "1".repeat(40) }], heads);
|
||||
const trunks: Record<string, string> = { "novox/mesh-host:main": TRUNK };
|
||||
heads.main = MOVED;
|
||||
const failing = { ...forge, setCommitStatus: async () => { throw new Error("the forge is away"); } };
|
||||
assert.equal(await weighTrunks(failing, new GateWriter(failing), trunks, "novox", "mesh-host"), false);
|
||||
assert.equal(trunks["novox/mesh-host:main"], TRUNK, "not recorded: the next pass weighs it again");
|
||||
});
|
||||
|
||||
test("a verdict judged against a trunk newer than the pass's first read is not put out of date", async () => {
|
||||
const NEWER = "ffffffffffffffffffffffffffffffffffffffff";
|
||||
const heads: Record<string, string> = { main: TRUNK };
|
||||
const { forge, statuses } = aForge([{ number: 1, base: "main", head: HEAD }], heads);
|
||||
const trunks: Record<string, string> = { "novox/mesh-host:main": TRUNK };
|
||||
// The pass reads main at MOVED; by the head's turn the trunk moved again, and the head's check answered for it.
|
||||
let reads = 0;
|
||||
const moving = { ...forge, branchHead: async () => (reads++ === 0 ? MOVED : NEWER) };
|
||||
statuses.set(HEAD, { "mesh/merge-gate": { state: "success", description: `head x merged into trunk ${NEWER.slice(0, 12)} — pass` } });
|
||||
await weighTrunks(moving, new GateWriter(moving), trunks, "novox", "mesh-host");
|
||||
assert.equal(statuses.get(HEAD)!["mesh/merge-gate"].state, "success", "a fresh verdict was put out of date");
|
||||
});
|
||||
|
||||
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<string, string> = { 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<string, string> = {};
|
||||
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);
|
||||
});
|
||||
|
||||
// Issue 1301: a trunk's move puts out of date only the controller's statuses the base still requires; on hq that is
|
||||
// mesh/repo-check alone, judged by its own words; on a main requiring mesh/check, none (the checker's, ADR 0447 §4).
|
||||
test("a trunk's move writes only the statuses the base still requires", async () => {
|
||||
const { outOfDateStatuses, trunkNamed } = await import("../trunk.ts");
|
||||
const old = "1".repeat(40), now = "2".repeat(40);
|
||||
const repo = { state: "success", description: `head ${"a".repeat(12)} merged into trunk ${old.slice(0, 12)} — pass: y` };
|
||||
const gate = { state: "success", description: `head ${"a".repeat(12)} merged into trunk ${old.slice(0, 12)} — pass: x` };
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/repo-check": repo }, now, ["mesh/repo-check"]).map((s: any) => s.context), ["mesh/repo-check"]);
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now, ["mesh/repo-check"]).map((s: any) => s.context),
|
||||
["mesh/repo-check"]);
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now, ["mesh/check"]), []);
|
||||
assert.deepEqual(outOfDateStatuses({ "mesh/merge-gate": gate, "mesh/repo-check": repo }, now).map((s: any) => s.context),
|
||||
["mesh/merge-gate", "mesh/repo-check"], "a rule not read: as before");
|
||||
// The checker's line names its trunk as "<head12> on <trunk12>".
|
||||
assert.equal(trunkNamed(`pass ${"a".repeat(12)} on ${old.slice(0, 12)} | unit 3 run`), old.slice(0, 12));
|
||||
assert.equal(trunkNamed("building on shanks since 10:00"), undefined);
|
||||
});
|
||||
@@ -1,9 +1,9 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mock, test } from "node:test";
|
||||
import { test } from "node:test";
|
||||
import type { GiteaPull } from "../client.ts";
|
||||
|
||||
// **No poll of the forge** (issue 1422, notes 10 and 11): the forge's events announce heads and merges; a pass runs once at
|
||||
// the start and when the receiver listens.
|
||||
// **No poll of the forge for heads** (issue 1422; design 47 H §764, H-A-100): the forge's events say a pull request's new
|
||||
// head as pull.updated, a merge and a close; nothing weighs a sibling or asks a merge check.
|
||||
|
||||
const edited = (base: string) => ({ name: "forge.pull-request.edited", body: { owner: "novox", repo: "hq", number: 7, base, merged: false } });
|
||||
|
||||
@@ -12,10 +12,9 @@ function deps() {
|
||||
return { calls, d: {
|
||||
getRepo: async () => ({ r: 1 }),
|
||||
getPull: async () => ({ state: "open", base: "trunk" }) as any,
|
||||
announceHead: async () => { calls.push("head"); },
|
||||
announceUpdated: async () => { calls.push("updated"); },
|
||||
announceMerge: async () => { calls.push("merge"); },
|
||||
announceClosed: async () => { calls.push("closed"); },
|
||||
siblingMoves: async () => { calls.push("siblings"); },
|
||||
} };
|
||||
}
|
||||
|
||||
@@ -23,51 +22,15 @@ test("an edited event announces the head into the base it now has, without a pol
|
||||
const { actOn } = await import("../watch.ts");
|
||||
const { calls, d } = deps();
|
||||
await actOn(edited("trunk"), d);
|
||||
assert.deepEqual(calls, ["head"]);
|
||||
assert.deepEqual(calls, ["updated"]);
|
||||
});
|
||||
|
||||
test("a close without a merge is announced by its event, a merged close is left to the merge event", async () => {
|
||||
const { actOn } = await import("../watch.ts");
|
||||
const { calls, d } = deps();
|
||||
await actOn({ name: "forge.pull-request.closed", body: { owner: "novox", repo: "hq", number: 7, merged: false } }, d);
|
||||
assert.deepEqual(calls, ["closed", "siblings"]);
|
||||
assert.deepEqual(calls, ["closed"]);
|
||||
calls.length = 0;
|
||||
await actOn({ name: "forge.pull-request.closed", body: { owner: "novox", repo: "hq", number: 7, merged: true } }, d);
|
||||
assert.deepEqual(calls, []);
|
||||
});
|
||||
|
||||
test("no timer polls after the start: one pass ends and nothing is scheduled; a failed one is asked again", async () => {
|
||||
const { startPass } = await import("../watch.ts");
|
||||
mock.timers.enable({ apis: ["setTimeout", "setInterval"] });
|
||||
try {
|
||||
let runs = 0;
|
||||
await startPass(async () => { runs++; }, { retryMs: 60_000, log: () => {} });
|
||||
mock.timers.tick(24 * 3600_000);
|
||||
assert.equal(runs, 1, "a day later the forge was not asked again");
|
||||
let tries = 0;
|
||||
const retried = startPass(async () => { if (++tries < 2) throw new Error("forge down"); }, { retryMs: 60_000, log: () => {} });
|
||||
await Promise.resolve(); await Promise.resolve();
|
||||
mock.timers.tick(60_000);
|
||||
await retried;
|
||||
assert.equal(tries, 2);
|
||||
} finally {
|
||||
mock.timers.reset();
|
||||
}
|
||||
});
|
||||
|
||||
test("a start pass announces a head nobody announced, and only remembers one the forge shows a check of", async () => {
|
||||
const { announceNewHeads } = await import("../watch.ts");
|
||||
const pull = (n: number, sha: string): GiteaPull => ({ number: n, state: "open", head_sha: sha, base: "main",
|
||||
updated_at: new Date().toISOString() } as unknown as GiteaPull);
|
||||
const announced: number[] = [], said: string[] = [];
|
||||
const heads: Record<string, string> = {};
|
||||
await announceNewHeads("novox/hq", [pull(1, "a".repeat(40)), pull(2, "b".repeat(40))], heads, false,
|
||||
new Date(Date.now() - 24 * 3600_000).toISOString(), {
|
||||
alreadyChecked: async (p) => p.number === 2,
|
||||
announceHead: async (p) => { announced.push(p.number); return true; },
|
||||
missed: (w) => said.push(w),
|
||||
});
|
||||
assert.deepEqual(announced, [1]);
|
||||
assert.equal(heads["novox/hq#2@main"], "b".repeat(40), "the checked head is remembered, not announced");
|
||||
assert.equal(said.length, 1);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// The tools of the forge module's merge calls (novox/hq design 47 H §766, §767): forge_calls reads the calls, newest first,
|
||||
// with each member's state; forge_call_say_again says a call's merge-called again; forge_partial_clear clears a call's
|
||||
// merge-partial by hand. Each act is written in the hand-act log before it is done.
|
||||
|
||||
import type { ToolDefinition } from "@novox/mesh-sdk/tools";
|
||||
import { callerOf } from "@novox/mesh-sdk/callers";
|
||||
import type { HandAct } from "@novox/mesh-sdk/views";
|
||||
import type { Partials } from "../partials.js";
|
||||
import { sayCall, type Calls, type Say } from "../calls.js";
|
||||
|
||||
const word = (v: unknown): string => (typeof v === "string" ? v.trim() : "");
|
||||
|
||||
export function callTools(calls: Calls, partials: Partials, say: Say, acts: (act: HandAct) => Promise<void>): ToolDefinition[] {
|
||||
return [
|
||||
{
|
||||
name: "forge_calls",
|
||||
description: "The merge calls the forge module keeps (its state calls), newest first: each call's by, account, when, whether its merge-called " +
|
||||
"was said, and each member with its base, head and state (judged, merged with its commit, or refused with why) (design 47 H §766).",
|
||||
input: { type: "object", properties: { call: { type: "string", description: "only this merge call" } } },
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
const only = word(args.call);
|
||||
const all = await calls.all();
|
||||
return { calls: only ? all.filter((c) => c.call === only) : all };
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "forge_call_say_again",
|
||||
description: "Says one merge call's merge-called again, for what it landed, with why: a gate that missed it takes the call in. " +
|
||||
"Written in the hand-act log first (design 47 H §766).",
|
||||
input: { type: "object", required: ["call", "why"], properties: {
|
||||
call: { type: "string", description: "the merge call, 16 lowercase hex characters" },
|
||||
why: { type: "string", description: "why it is said again, in plain words" } } },
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
const call = word(args.call), why = word(args.why);
|
||||
if (!/^[0-9a-f]{16}$/.test(call) || !why) throw new Error("forge_call_say_again names the call (16 lowercase hex characters) and why");
|
||||
const kept = await calls.get(call);
|
||||
if (!kept) throw new Error(`the forge module keeps no merge call ${call}`);
|
||||
const by = callerOf(args);
|
||||
try {
|
||||
await acts({ what: `said the merge-called of the merge call ${call} again`, why: `${why} (${by})`, cause: "merge-called-said-again", verb: "forge_call_say_again" });
|
||||
} catch (err) {
|
||||
throw new Error(`the hand act was not recorded, so nothing was said: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
const said = await sayCall(kept, say);
|
||||
if (said && !kept.said) {
|
||||
const { call: _c, ...value } = kept;
|
||||
await calls.put(call, { ...value, said: true });
|
||||
}
|
||||
return { call, said, why, by, ...(said ? {} : { "said-none": "the call landed no member, so it says no merge-called" }) };
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "forge_partial_clear",
|
||||
description: "Clears the finding merge-partial of one merge call (a call the forge refused a member of after every member was judged), " +
|
||||
"with why, when its refused members are not to be merged. Written in the hand-act log first (design 47 H §767).",
|
||||
input: { type: "object", required: ["call", "why"], properties: {
|
||||
call: { type: "string", description: "the merge call, 16 lowercase hex characters" },
|
||||
why: { type: "string", description: "why it is cleared, in plain words" } } },
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
const call = word(args.call), why = word(args.why);
|
||||
if (!/^[0-9a-f]{16}$/.test(call) || !why) throw new Error("forge_partial_clear names the call (16 lowercase hex characters) and why");
|
||||
const by = callerOf(args);
|
||||
try {
|
||||
await acts({ what: `cleared the merge-partial of the merge call ${call}`, why: `${why} (${by})`, cause: "merge-partial-cleared", verb: "forge_partial_clear" });
|
||||
} catch (err) {
|
||||
throw new Error(`the hand act was not recorded, so nothing was cleared: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
return { call, cleared: await partials.clear(call), why, by };
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
// **A merge check's verdict holds for the trunk commit it judged** (novox/hq ADR 0323, issue 341).
|
||||
//
|
||||
// The build seat checks a pull request's head merged into its trunk's head at that moment — the verdict's trunk
|
||||
// commit — and the verdict names both: "head 1a2b3c4d5e6f merged into trunk 7a8b9c0d1e2f", in both statuses'
|
||||
// descriptions. When the trunk moves, every open pull request's verdict into it is out of date:
|
||||
//
|
||||
// - **the forge's holder** (index.ts) hears the move on its own — it reads each trunk's head on every pass that
|
||||
// looks at the repository, never through mesh-delivery (ADR 0239 §36) — and sets mesh/merge-gate and
|
||||
// mesh/repo-check on the head of every open pull request into that trunk back to pending, with the reason.
|
||||
// Nothing is run for it: a recheck, a new head, or the merge tool asks the check again;
|
||||
// - **the merge tool** (tools/index.ts) compares the trunk commit the head's verdict names with the trunk's head at
|
||||
// the moment it is asked, merges nothing when they differ, asks the head's check once — the same announcement a
|
||||
// new head makes — and never merges by itself after.
|
||||
//
|
||||
// Since issue 1301 (ADR 0447 §4) a main that requires mesh/check is the checker's: its line names the trunk commit it judged
|
||||
// ("pass <head12> on <trunk12>"), the checker sets it out of date when the trunk moves, and the merge tool refuses a verdict
|
||||
// of another trunk and asks nothing. The two statuses above are kept only where a base still requires them (a revert,
|
||||
// pulls.ts maintained).
|
||||
//
|
||||
// Pure functions and two small files of the module's own state, so this is tested without a forge.
|
||||
|
||||
import type { CommitStatus, GiteaClient, GiteaPull, HeadCheck } from "./client.js";
|
||||
import { CHECK_CONTEXT, WAITS_FOR_COMPOSED, maintained, requiredOf } from "./pulls.js";
|
||||
|
||||
/** A commit as a verdict names it: its first twelve characters. */
|
||||
export function twelve(sha: string | undefined): string {
|
||||
return String(sha ?? "").slice(0, 12).toLowerCase();
|
||||
}
|
||||
|
||||
/** Whether two commits are one, however either is abbreviated (seven characters at least). */
|
||||
export function sameCommit(a: string | undefined, b: string | undefined): boolean {
|
||||
const x = String(a ?? "").toLowerCase(), y = String(b ?? "").toLowerCase();
|
||||
if (x.length < 7 || y.length < 7) return false;
|
||||
return x.length <= y.length ? y.startsWith(x) : x.startsWith(y);
|
||||
}
|
||||
|
||||
/** What a verdict judged, as both statuses' descriptions begin (ADR 0323 §3). */
|
||||
export function judgedWords(head: string, trunk: string): string {
|
||||
return `head ${twelve(head)} merged into trunk ${twelve(trunk)}`;
|
||||
}
|
||||
|
||||
/** The trunk commit a merge check's status names: the one its verdict judged ("merged into trunk …"), or, for a
|
||||
* verdict put out of date, the one it had judged ("judged against trunk …"). Undefined when it names none, as a
|
||||
* verdict of a build seat from before ADR 0323 does. */
|
||||
export function trunkNamed(description: string | undefined): string | undefined {
|
||||
const m = /(?:merged into|judged against) trunk ([0-9a-f]{7,64})\b/i.exec(description ?? "")
|
||||
// The checker's mesh/check line (ADR 0447 §3): "<head12> on <trunk12>".
|
||||
?? /\b[0-9a-f]{12} on ([0-9a-f]{12})\b/i.exec(description ?? "");
|
||||
return m ? m[1].toLowerCase() : undefined;
|
||||
}
|
||||
|
||||
/** A verdict out of date, as its statuses say it (ADR 0323 §4). */
|
||||
export function outOfDateWords(judged: string | undefined, now: string): string {
|
||||
const was = judged ? `judged against trunk ${twelve(judged)}` : "judged against a trunk it does not name";
|
||||
return `out of date: ${was}, the trunk is now ${twelve(now)}; judged again on a recheck or when it is merged`;
|
||||
}
|
||||
|
||||
/** Whether a pending gate says a check of the head is asked and not yet answered: waiting in the build queue,
|
||||
* building on a machine (gate.ts), or asked again by a recheck (mesh-delivery). Such a check is not asked again. */
|
||||
export function checkOnItsWay(gate: HeadCheck | undefined): boolean {
|
||||
if (!gate || gate.state !== "pending") return false;
|
||||
const d = gate.description ?? "";
|
||||
return d.startsWith("waiting in the build queue") || d.startsWith("building on ") || d.startsWith("checking again");
|
||||
}
|
||||
|
||||
/** Whether a gate says it waits for its group's composed check. */
|
||||
export function waitsForComposed(gate: HeadCheck | undefined): boolean {
|
||||
return !!gate && gate.state === "pending" && (gate.description ?? "").startsWith(WAITS_FOR_COMPOSED);
|
||||
}
|
||||
|
||||
/**
|
||||
* The statuses a trunk's move puts back to pending on one open pull request's head, from what the forge holds on it
|
||||
* now: none when it has no merge check yet, when a check is pending (on its way, or already out of date — its
|
||||
* verdict is weighed against the trunk when it lands), or when its verdict judged the trunk's head. Otherwise the
|
||||
* gate, and the repository's own check when the head has one — a pending repo-check a verdict will not set would
|
||||
* stand for ever (novox/hq issue 305's rule).
|
||||
*/
|
||||
export function outOfDateStatuses(checks: Record<string, HeadCheck>, trunkHead: string, required?: string[]): CommitStatus[] {
|
||||
// Only the statuses the base still requires (issue 1301): the first of them the head holds judges.
|
||||
const kept = maintained(required).filter((c) => checks[c]);
|
||||
const judge = kept.length ? checks[kept[0]] : undefined;
|
||||
if (!judge || judge.state === "pending") return [];
|
||||
const judged = trunkNamed(judge.description);
|
||||
if (judged && sameCommit(judged, trunkHead)) return [];
|
||||
const description = outOfDateWords(judged, trunkHead);
|
||||
return kept.map((context) => ({ state: "pending" as const, context, description }));
|
||||
}
|
||||
|
||||
/** Where the merge tool stands with a head's verdict and the trunk's head (ADR 0323 §5). */
|
||||
export interface VerdictAtTrunk {
|
||||
/** The verdict judged the trunk's head: the required statuses decide, as before. */
|
||||
current: boolean;
|
||||
/** A check of the head is asked and not yet answered: nothing is asked again. */
|
||||
onItsWay: boolean;
|
||||
/** The trunk commit the verdict names, when it names one. */
|
||||
judged?: string;
|
||||
}
|
||||
|
||||
export function verdictAtTrunk(checks: Record<string, HeadCheck>, trunkHead: string, context = CHECK_CONTEXT): VerdictAtTrunk {
|
||||
const gate = checks[context];
|
||||
const judged = trunkNamed(gate?.description);
|
||||
if (checkOnItsWay(gate)) return { current: false, onItsWay: true, judged };
|
||||
return { current: !!gate && gate.state !== "pending" && !!judged && sameCommit(judged, trunkHead), onItsWay: false, judged };
|
||||
}
|
||||
|
||||
/** The pending words the merge tool's ask of a check is said with: in the build queue from now, as an announced head's. */
|
||||
export function askedWords(at: Date, trunkHead: string): string {
|
||||
const two = (n: number) => String(n).padStart(2, "0");
|
||||
return `waiting in the build queue since ${two(at.getHours())}:${two(at.getMinutes())}: asked by the merge tool ` +
|
||||
`against trunk ${twelve(trunkHead)}`;
|
||||
}
|
||||
|
||||
// --- a trunk's move, weighed --------------------------------------------------------------------------------------
|
||||
|
||||
/** What weighing a trunk's move reads of the forge. */
|
||||
export interface TrunkForge {
|
||||
listOpenPulls(owner: string, repo: string): Promise<GiteaPull[]>;
|
||||
branchHead(owner: string, repo: string, branch: string): Promise<string>;
|
||||
/** The rule the forge applies to a branch: which of the controller's statuses are kept there (issue 1301). Not given,
|
||||
* or not read: both, as before. */
|
||||
appliedProtection?(owner: string, repo: string, branch: string): Promise<{ enable_status_check?: boolean; status_check_contexts?: string[] | null } | null>;
|
||||
}
|
||||
|
||||
/** What it writes with: the merge check's one writer (gate.ts), which decides in the head's own turn. */
|
||||
export interface OutOfDateWriter {
|
||||
outOfDate(owner: string, repo: string, sha: string,
|
||||
decide: (checks: Record<string, HeadCheck>) => CommitStatus[] | Promise<CommitStatus[]>, target?: string): Promise<CommitStatus[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every trunk of one repository that an open pull request merges into, read; for one that moved since it was last read
|
||||
* (`trunks`, by `owner/repo:branch`, which this keeps current), the merge check of every open pull request into it put
|
||||
* out of date (ADR 0323 §4). A trunk first seen is recorded as it is and nothing is put out of date. Bounded however
|
||||
* many are open: one listing of the open pull requests (paged), one read of each trunk, then per open pull request into
|
||||
* a trunk that moved one read and at most two writes, one at a time. A trunk that could not be weighed whole is not
|
||||
* recorded, so the next pass weighs it again. Answers whether the record changed. `moved` is told of each base that moved, for the siblings whose base it is.
|
||||
*/
|
||||
export async function weighTrunks(forge: TrunkForge, gates: OutOfDateWriter, trunks: Record<string, string>, owner: string,
|
||||
name: string, moved?: (base: string) => Promise<void>): Promise<boolean> {
|
||||
let changed = false;
|
||||
try {
|
||||
const open = (await forge.listOpenPulls(owner, name)).filter((p) => p.state === "open" && !!p.head_sha && !!p.base);
|
||||
for (const base of new Set(open.map((p) => String(p.base)))) {
|
||||
const key = `${owner}/${name}:${base}`.toLowerCase();
|
||||
const head = await forge.branchHead(owner, name, base);
|
||||
const before = trunks[key];
|
||||
if (before && sameCommit(before, head)) continue;
|
||||
if (before) {
|
||||
let put = 0;
|
||||
const required = forge.appliedProtection
|
||||
? requiredOf(await forge.appliedProtection(owner, name, base).catch(() => undefined)) : undefined;
|
||||
for (const pull of open.filter((p) => p.base === base)) {
|
||||
// The trunk is read again in the head's own turn: a verdict judged against a head newer than the pass's first
|
||||
// read — the trunk moved again meanwhile, and a check answered — is not put out of date.
|
||||
const written = await gates.outOfDate(owner, name, String(pull.head_sha),
|
||||
async (checks) => outOfDateStatuses(checks, await forge.branchHead(owner, name, base), required), pull.html_url || undefined);
|
||||
if (written.length) put++;
|
||||
}
|
||||
console.log(`[gitea] ${owner}/${name}'s ${base} moved from ${twelve(before)} to ${twelve(head)}: ${put} open pull ` +
|
||||
`request(s) into it put out of date, nothing run (novox/hq ADR 0323)`);
|
||||
// A verdict that read a pull request into this trunk as a sibling is out of date by its move too (ADR 0382 §5).
|
||||
if (moved) await moved(base);
|
||||
}
|
||||
trunks[key] = head;
|
||||
changed = true;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${owner}/${name}: whether a trunk moved could not be weighed, so it is weighed again on the next ` +
|
||||
`pass — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
// --- the announcement of a head, shared by the watcher and the merge tool ---------------------------------------
|
||||
|
||||
/** `pull.updated` for one open pull request's head: what the controller asks the build seat to check (novox/hq to-be
|
||||
* 45 §9). The merge tool's ask is this same announcement (ADR 0323 §5). */
|
||||
export async function announcementOf(client: GiteaClient, owner: string, name: string, cloneURL: string | undefined,
|
||||
pull: GiteaPull): Promise<Record<string, unknown>> {
|
||||
const files = await client.listPullFiles(owner, name, pull.number);
|
||||
const head = String(pull.head_sha);
|
||||
// What the controller maps the change onto the mesh's module graph with (novox/hq ADR 0237 as amended): which
|
||||
// changed directories hold a module at the head — the merge's rule (issue 278). Not said when it could not be read; the controller then reads the change as
|
||||
// touching everything built from the repository.
|
||||
let moduleDirs: string[] | null = null;
|
||||
try {
|
||||
if (!files.truncated) moduleDirs = await client.moduleDirsAt(owner, name, head, files.paths);
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${owner}/${name}#${pull.number}: what the head holds could not be read — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
// The base branch's head as the head is announced: the trunk commit a verdict no build judges is said against
|
||||
// (novox/hq ADR 0323 §3). Not said when it cannot be read; that verdict then names none.
|
||||
const baseSHA = pull.base ? await client.branchHead(owner, name, String(pull.base)).catch(() => undefined) : undefined;
|
||||
return {
|
||||
owner,
|
||||
repo: name,
|
||||
number: pull.number,
|
||||
...(baseSHA ? { base_sha: baseSHA } : {}),
|
||||
title: pull.title,
|
||||
body: pull.body,
|
||||
base: pull.base,
|
||||
head: pull.head,
|
||||
head_sha: pull.head_sha,
|
||||
clone_url: cloneURL,
|
||||
html_url: pull.html_url,
|
||||
paths: files.paths,
|
||||
paths_truncated: files.truncated,
|
||||
removed: files.removed,
|
||||
...(moduleDirs ? { module_dirs: moduleDirs, module_dirs_said: true } : {}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// **A pull request's new head is said as the seat git's pull.updated** (novox/hq design 47 H §764; H-A-100): from the forge's
|
||||
// event of an opened, pushed, reopened or retargeted pull request alone; nothing polls for heads and no merge check is
|
||||
// asked or written (the merge-check machinery went with milestone H).
|
||||
|
||||
import type { GiteaClient, GiteaPull } from "./client.js";
|
||||
|
||||
/** `pull.updated` for one open pull request's head: what the controller asks the build seat to check (novox/hq to-be
|
||||
* 45 §9). The merge tool's ask is this same announcement (ADR 0323 §5). */
|
||||
export async function announcementOf(client: GiteaClient, owner: string, name: string, cloneURL: string | undefined,
|
||||
pull: GiteaPull): Promise<Record<string, unknown>> {
|
||||
const files = await client.listPullFiles(owner, name, pull.number);
|
||||
const head = String(pull.head_sha);
|
||||
// What the controller maps the change onto the mesh's module graph with (novox/hq ADR 0237 as amended): which
|
||||
// changed directories hold a module at the head — the merge's rule (issue 278). Not said when it could not be read; the controller then reads the change as
|
||||
// touching everything built from the repository.
|
||||
let moduleDirs: string[] | null = null;
|
||||
try {
|
||||
if (!files.truncated) moduleDirs = await client.moduleDirsAt(owner, name, head, files.paths);
|
||||
} catch (err) {
|
||||
console.error(`[gitea] ${owner}/${name}#${pull.number}: what the head holds could not be read — ${err instanceof Error ? err.message : err}`);
|
||||
}
|
||||
// The base branch's head as the head is announced: the trunk commit a verdict no build judges is said against
|
||||
// (novox/hq ADR 0323 §3). Not said when it cannot be read; that verdict then names none.
|
||||
const baseSHA = pull.base ? await client.branchHead(owner, name, String(pull.base)).catch(() => undefined) : undefined;
|
||||
return {
|
||||
owner,
|
||||
repo: name,
|
||||
number: pull.number,
|
||||
...(baseSHA ? { base_sha: baseSHA } : {}),
|
||||
title: pull.title,
|
||||
body: pull.body,
|
||||
base: pull.base,
|
||||
head: pull.head,
|
||||
head_sha: pull.head_sha,
|
||||
clone_url: cloneURL,
|
||||
html_url: pull.html_url,
|
||||
paths: files.paths,
|
||||
paths_truncated: files.truncated,
|
||||
removed: files.removed,
|
||||
...(moduleDirs ? { module_dirs: moduleDirs, module_dirs_said: true } : {}),
|
||||
};
|
||||
}
|
||||
+6
-54
@@ -1,29 +1,24 @@
|
||||
// What the forge's events ask of the announcer, and the one pass at a start (issue 1422, notes 10 and 11). Nothing polls the
|
||||
// forge: a head or merge is announced when its event is heard, and a pass over every repository runs once when the module
|
||||
// starts and again when the receiver is listening, so what happened while it was down is announced too.
|
||||
// forge for heads: a pull request's new head is said as pull.updated when its event is heard, a merge or a close by its
|
||||
// event and by the pass over every repository's merges at the start (design 47 H §764; H-A-100).
|
||||
//
|
||||
// Apart from index.ts, which has side effects at import, so these are tested with the announcers injected.
|
||||
|
||||
import type { GiteaPull, GiteaRepo } from "./client.js";
|
||||
import type { Announced } from "./pulls.js";
|
||||
import { headKey, headsToAnnounce } from "./pulls.js";
|
||||
import type { ForgeEvent } from "./webhook.js";
|
||||
|
||||
export interface ActDeps<R, P> {
|
||||
getRepo(owner: string, repo: string): Promise<R>;
|
||||
getPull(owner: string, repo: string, number: number): Promise<P & { state?: string; merged?: boolean }>;
|
||||
announceHead(repo: R, pull: P): Promise<unknown>;
|
||||
announceUpdated(repo: R, pull: P): Promise<unknown>;
|
||||
announceMerge(repo: R, pull: P): Promise<unknown>;
|
||||
announceClosed(repo: R, pull: P): Promise<unknown>;
|
||||
siblingMoves(full: string, number: number): Promise<unknown>;
|
||||
}
|
||||
|
||||
const HEAD_EVENTS = ["forge.pull-request.opened", "forge.pull-request.synchronized", "forge.pull-request.reopened"];
|
||||
|
||||
/**
|
||||
* What the forge's event asks of the announcer at once: a new head is announced, a merge, a close without a merge, and a
|
||||
* pull request retargeted (an edit: the head is announced into the base it now has — the base is in the head's key, so an
|
||||
* edit that changed nothing about the base announces nothing).
|
||||
* What the forge's event asks of the announcer at once: a new head is said, a merge, a close without a merge, and a pull
|
||||
* request retargeted (an edit: the head is said into the base it now has).
|
||||
*/
|
||||
export async function actOn<R, P>(event: ForgeEvent, d: ActDeps<R, P>): Promise<void> {
|
||||
const b = event.body as { owner?: string; repo?: string; number?: number; merged?: boolean };
|
||||
@@ -37,51 +32,8 @@ export async function actOn<R, P>(event: ForgeEvent, d: ActDeps<R, P>): Promise<
|
||||
const [repo, pull] = await Promise.all([d.getRepo(b.owner, b.repo), d.getPull(b.owner, b.repo, b.number)]);
|
||||
if (closed) {
|
||||
await d.announceClosed(repo, pull);
|
||||
await d.siblingMoves(`${b.owner}/${b.repo}`, b.number);
|
||||
return;
|
||||
}
|
||||
if (head && pull.state === "open") await d.announceHead(repo, pull);
|
||||
if (head && pull.state === "open") await d.announceUpdated(repo, pull);
|
||||
if (merged) await d.announceMerge(repo, pull);
|
||||
}
|
||||
|
||||
/**
|
||||
* The one pass at a start, and its retry: run now; if it fails, say so and run again after `retryMs`, until one pass has
|
||||
* ended. A retry of a failed pass, never a poll: after a pass has ended nothing is scheduled.
|
||||
*/
|
||||
export function startPass(run: () => Promise<void>, opts: { retryMs: number; log?: (l: string) => void }): Promise<void> {
|
||||
const log = opts.log ?? ((l) => console.error(`[gitea] ${l}`));
|
||||
return run().catch((err) => {
|
||||
log(`the pass over every repository failed, asked again in ${opts.retryMs / 1000} s — ${err instanceof Error ? err.message : err}`);
|
||||
return new Promise<void>((resolve) => setTimeout(() => resolve(startPass(run, opts)), opts.retryMs));
|
||||
});
|
||||
}
|
||||
|
||||
export interface HeadDeps {
|
||||
/** Whether the forge shows a merge check of the head: only an announced head has one. */
|
||||
alreadyChecked(pull: GiteaPull): Promise<boolean>;
|
||||
/** Announce it; true when it was this call that announced it (not an event before it). */
|
||||
announceHead(pull: GiteaPull): Promise<boolean>;
|
||||
/** Told of a head this pass announced that no event had. */
|
||||
missed(what: string): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The heads of one repository's open pull requests a pass announces. A head is announced when it is new and fresh (moved in
|
||||
* the last day); at the first pass after a start (`primed` false) one the forge shows a merge check of was announced
|
||||
* already, and is only remembered.
|
||||
*/
|
||||
export async function announceNewHeads(full: string, open: GiteaPull[], heads: Announced, primed: boolean, dayAgo: string, d: HeadDeps): Promise<void> {
|
||||
for (const pull of headsToAnnounce(full, open, heads)) {
|
||||
const key = headKey(full, pull.number, pull.base);
|
||||
const fresh = primed || (!!pull.updated_at && pull.updated_at > dayAgo);
|
||||
if (fresh && !primed && await d.alreadyChecked(pull)) {
|
||||
heads[key] = String(pull.head_sha);
|
||||
continue;
|
||||
}
|
||||
if (fresh) {
|
||||
if (await d.announceHead(pull)) d.missed(`${key} at ${String(pull.head_sha).slice(0, 8)}`);
|
||||
} else heads[key] = String(pull.head_sha);
|
||||
}
|
||||
}
|
||||
|
||||
export type { GiteaRepo };
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
// What this module keeps on the bus, announced from its code (novox/hq ADR 0495 §2 §5; issue 1790): every state it
|
||||
// keeps, each readable by others, since another module reads each today (the builder, the checker and the indexer read toolchains; the builder and the checker read steps; gitea reads keys). A state of its own that it
|
||||
// keeps, each readable by others, since another module reads each today (the builder and the indexer read toolchains; the builder reads steps; gitea reads keys). A state of its own that it
|
||||
// did not name here is refused at the call (stdio.Keeps).
|
||||
|
||||
import (
|
||||
|
||||
@@ -57,9 +57,6 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps {
|
||||
func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller {
|
||||
if source == nil {
|
||||
source = &steprun.SeatAsks{Language: sdkstep.AskGo, Logf: m.Say}
|
||||
if m.Callers == nil {
|
||||
m.Callers = steprun.SeatListCallers(sdkstep.AskGo, m.Say)
|
||||
}
|
||||
}
|
||||
capacity := m.Capacity
|
||||
// The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config
|
||||
@@ -91,7 +88,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul
|
||||
if m.SayEvent != nil {
|
||||
say = steprun.SayOn(sdkstep.AskGo, m.Node, m.SayEvent)
|
||||
}
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskGo, Say: say, Callers: m.Callers, Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskGo, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Logf: m.Say, Has: m.Has,
|
||||
Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) {
|
||||
if err := m.Check(ctx); err != nil {
|
||||
|
||||
@@ -55,7 +55,7 @@ func TestTheModuleTakesADeliveryAskBeforeAnEarlierCheck(t *testing.T) {
|
||||
// A build rides main's subject, any other kind a named asker's (design 47 G §543).
|
||||
subject, _ := sdkstep.AskSubject(a.Language, a.Kind)
|
||||
if a.Kind != sdkstep.AskDelivery {
|
||||
subject, _ = sdkstep.AskSubjectBy(a.Language, a.Kind, "mesh-delivery-checker")
|
||||
subject, _ = sdkstep.AskSubjectBy(a.Language, a.Kind, "mesh-delivery-runner")
|
||||
}
|
||||
if _, err := js.Publish(ctx, subject, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -87,7 +87,7 @@ func TestTheModuleTakesADeliveryAskBeforeAnEarlierCheck(t *testing.T) {
|
||||
ended := 0
|
||||
states.mu.Lock()
|
||||
for _, a := range []sdkstep.StepAsk{first, check} {
|
||||
asker := "mesh-delivery-checker" // a named ask's step is keyed under its asker, main's under its node (§481)
|
||||
asker := "mesh-delivery-runner" // a named ask's step is keyed under its asker, main's under its node (§481)
|
||||
if a.Kind == sdkstep.AskDelivery {
|
||||
asker = ""
|
||||
}
|
||||
|
||||
@@ -11,15 +11,11 @@ import (
|
||||
func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) {
|
||||
var subjects []string
|
||||
m := &Module{Node: "ace", Capacity: func() int { return 0 },
|
||||
Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" },
|
||||
SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }}
|
||||
p := m.Puller(nil, steprun.NewFakeAsks())
|
||||
if p.Say == nil {
|
||||
t.Fatal("the puller says no event")
|
||||
}
|
||||
if got, ok := p.Callers("step"); !ok || len(got) != 1 {
|
||||
t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok)
|
||||
}
|
||||
seat, _ := sdkstep.BuildSeat(sdkstep.AskGo)
|
||||
for _, event := range []string{"ask-taken", "step-progress", "step-ended"} {
|
||||
if err := p.Say(event, nil, "id"); err != nil {
|
||||
|
||||
@@ -198,7 +198,7 @@ func TestACheckAsksDigestIsTheToolsAnswerInTheCall(t *testing.T) {
|
||||
}
|
||||
}
|
||||
ask := m.askSteps(steps)
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker",
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner",
|
||||
At: time.Now(), Check: &sdkstep.CheckJob{Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: askCommit}, Levels: []string{sdkstep.LevelUnit}}}
|
||||
out, err := steprun.RunAsk(context.Background(), ask, a)
|
||||
if err != nil {
|
||||
|
||||
@@ -166,14 +166,15 @@ func TestAComposeAskFailsLoudly(t *testing.T) {
|
||||
}
|
||||
|
||||
// Design 47 G §615: the module reads the manifests a compose ask names from the catalog graph's entry, by the seat's verb
|
||||
// entry, and invokes no tool of the delivery indexer. 1921 (B0): a run's input is asked of the checker seat's verb input, as
|
||||
// the manifest invokes it, and no state of the checker's inputs is read (ADR 0596 §2).
|
||||
// entry, and invokes no tool of the delivery indexer. 1921 (B0): a run's input is asked of the seat mesh-wants' verb input, as
|
||||
// the manifest invokes it, and no state of the checker's inputs is read (ADR 0596 §2); the seat mesh-delivery-checker is no
|
||||
// longer invoked (design 47 H §764, H-A-57).
|
||||
func TestTheModuleReadsTheCatalogEntries(t *testing.T) {
|
||||
m := readManifest(t)
|
||||
if !slices.Contains(m.Invokes, catalogclient.EntryVerb) || slices.ContainsFunc(m.Invokes, func(i string) bool { return strings.HasPrefix(i, "mesh-delivery-indexer") }) {
|
||||
t.Errorf("invokes = %v: want %s and no tool of the indexer", m.Invokes, catalogclient.EntryVerb)
|
||||
}
|
||||
if !slices.Contains(m.Invokes, runInputVerb) || slices.Contains(m.Reads, "mesh-delivery-checker.inputs") {
|
||||
if !slices.Contains(m.Invokes, runInputVerb) || slices.ContainsFunc(m.Invokes, func(i string) bool { return strings.HasPrefix(i, "seat:mesh-delivery-checker") }) || slices.Contains(m.Reads, "mesh-delivery-checker.inputs") {
|
||||
t.Errorf("invokes = %v, reads = %v: want %s invoked and no inputs state read", m.Invokes, m.Reads, runInputVerb)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,3 +56,19 @@ func TestTheStepsSayAFailedNetworkAsATransportFault(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A-489b, design 47 G §24: the module's own steps can say a pipeline step's end, or
|
||||
// every step ask the runner sends it is refused ("this build module says no step's end"), as the first live pipeline met.
|
||||
func TestTheStepsCanSayAStepsEnd(t *testing.T) {
|
||||
conformance.StepEnds(t, (&Module{Node: "kitnode"}).askSteps(nil))
|
||||
}
|
||||
|
||||
// A-489c (A-489a): the module's puller counts the runs its steps state says started, not the bus's deliveries (an ask given back before
|
||||
// it ran is no run), so the poison rule never ends an ask that never ran.
|
||||
func TestThePullerCountsRunsThatStarted(t *testing.T) {
|
||||
conformance.RunsCounted(t, func(read func(string) (json.RawMessage, error)) *steprun.Puller {
|
||||
m := &Module{Node: "kitnode", Capacity: func() int { return 1 }}
|
||||
m.ReadSteps = StateRead{Get: read}
|
||||
return m.Puller(nil, steprun.NewFakeAsks())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -25,9 +25,6 @@ const CheckRound = 10 * time.Minute
|
||||
// Module is the Go build module's one running state: the node it runs on, its recipe, the toolchain guard and the
|
||||
// states it writes (toolchains, steps).
|
||||
type Module struct {
|
||||
// Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when
|
||||
// the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163.
|
||||
Callers func(kind string) ([]string, bool)
|
||||
// SayEvent publishes an event of the go build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the
|
||||
// puller's ask-taken, step-progress and step-ended (design 47 G §496, §602).
|
||||
SayEvent func(subject string, body any, id string) error
|
||||
@@ -119,7 +116,7 @@ type Module struct {
|
||||
// request names each manifest it gives by digest with (compose.go, design 47 G §615); GoModCache, when set, is the Go module cache in place of Cache's (a test).
|
||||
ReadEntry func(fingerprint string) (meshcat.CatalogEntry, error)
|
||||
// AskInput asks a seat verb the module invokes (module.json) as the runtime calls it (stdio.Ask): the verb a run ask's
|
||||
// input names, answering the input by its digest (meshdeliverycheckerclient.ReadInput).
|
||||
// input names, answering the input by its digest (the seat mesh-wants' verb input).
|
||||
AskInput func(key string, body any) (json.RawMessage, error)
|
||||
// PlaceHead, when set, lays a run's head at a directory instead of fetching it (a test).
|
||||
PlaceHead func(ctx context.Context, h sdkstep.RunHead, dir string) error
|
||||
|
||||
@@ -63,7 +63,7 @@ func TestACheckAskReachesTheTestStepPastItsPackages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker",
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner",
|
||||
At: time.Now(), Check: &sdkstep.CheckJob{Fetch: sdkstep.Fetch{Repository: "novox/x", Commit: askCommit}, Levels: []string{sdkstep.LevelUnit}}}
|
||||
out, err := steprun.RunAsk(context.Background(), m.askSteps(steps), a)
|
||||
got := ""
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
)
|
||||
@@ -50,22 +50,21 @@ func (m *Module) run(ctx context.Context, fetch steprun.StepFunc, a sdkstep.Step
|
||||
}
|
||||
|
||||
// inputOf is the input a run names, asked of the seat verb the ask names (module.json invokes it) and checked against
|
||||
// its digest and size: bytes that are not the input named refuse the ask. A verb marked chunks (the seat mesh-catalog's
|
||||
// its digest and size: bytes that are not the input named refuse the ask. A verb marked chunks (the seats mesh-catalog's and mesh-wants'
|
||||
// input, of any size) is asked by the plain seat call: the tool runner receives the chunks and hands this module the
|
||||
// whole file in the member `file` (design 47 G A-237, A-249); another seat's input answers its bytes in `input`.
|
||||
// whole file in the member `file` (design 47 G A-237, A-249).
|
||||
func (m *Module) inputOf(in sdkstep.RunInput) ([]byte, error) {
|
||||
if m.AskInput == nil {
|
||||
return nil, errors.New("this module asks no seat verb for a run's input")
|
||||
}
|
||||
// A verb that does not answer fails the step; an answer that is not the input named refuses the ask.
|
||||
raw, err := m.AskInput(in.Verb, checker.InputInput{Digest: in.Digest})
|
||||
raw, err := m.AskInput(in.Verb, meshwants.InputInput{Digest: in.Digest})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("asking %s for the input %s: %w", in.Verb, in.Digest, err)
|
||||
}
|
||||
var a struct {
|
||||
Digest string `json:"digest"`
|
||||
File string `json:"file"`
|
||||
Input string `json:"input"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, steprun.Refuse("%s answered no input for %s: %v", in.Verb, in.Digest, err)
|
||||
@@ -73,11 +72,7 @@ func (m *Module) inputOf(in sdkstep.RunInput) ([]byte, error) {
|
||||
if a.Digest != in.Digest {
|
||||
return nil, steprun.Refuse("%s answered the input %q, asked %s", in.Verb, a.Digest, in.Digest)
|
||||
}
|
||||
text := a.File
|
||||
if text == "" {
|
||||
text = a.Input
|
||||
}
|
||||
body, err := base64.StdEncoding.DecodeString(text)
|
||||
body, err := base64.StdEncoding.DecodeString(a.File)
|
||||
if err != nil {
|
||||
return nil, steprun.Refuse("%s answered the input %s in no base64: %v", in.Verb, in.Digest, err)
|
||||
}
|
||||
|
||||
@@ -16,8 +16,7 @@ import (
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
checkerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient"
|
||||
checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
@@ -25,7 +24,7 @@ import (
|
||||
|
||||
// 1921/C3: a run ask put on the Go build seat's check stream is pulled by the module started whole on the real bus and
|
||||
// answered on its step key <node>.<ask id>: the program, run shut off over its heads and its input (asked by digest of the
|
||||
// seat verb the ask names, which a fake checker answers on the bus as the checker's verb input does), answers its last
|
||||
// seat verb the ask names, which a fake assigner answers on the bus as the seat's verb input does), answers its last
|
||||
// line and exit; a program cannot write under its heads, and one
|
||||
// that dials the machine's network is shut off, and its key ends failed, the answer kept (design 47 G A-290).
|
||||
func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) {
|
||||
@@ -63,13 +62,13 @@ func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) {
|
||||
}
|
||||
m.PlaceHead = func(_ context.Context, h sdkstep.RunHead, dir string) error { return os.MkdirAll(dir, 0o700) }
|
||||
body := `{"v":1}`
|
||||
// The checker's verb input on the bus, and the module asking it as the runtime calls a seat verb it invokes.
|
||||
// The wants seat's verb input on the bus, and the module asking it as the runtime calls a seat verb it invokes.
|
||||
asked := make(chan string, 4)
|
||||
sub, err := nc.Subscribe("mesh.seat.mesh-delivery-checker.tool.input", func(msg *nats.Msg) {
|
||||
var ask checker.InputInput
|
||||
sub, err := nc.Subscribe("mesh.seat.mesh-wants.tool.input", func(msg *nats.Msg) {
|
||||
var ask meshwants.InputInput
|
||||
_ = json.Unmarshal(msg.Data, &ask)
|
||||
asked <- ask.Digest
|
||||
reply, _ := json.Marshal(map[string]any{"result": checkerclient.InputAnswerOf([]byte(body))})
|
||||
reply, _ := json.Marshal(map[string]any{"result": inputAnswerOf([]byte(body))})
|
||||
_ = msg.Respond(reply)
|
||||
})
|
||||
if err != nil {
|
||||
@@ -81,7 +80,7 @@ func TestARunAskIsAnsweredOnTheBusAndItsProgramIsShutOff(t *testing.T) {
|
||||
return nil, errors.New("asked " + key)
|
||||
}
|
||||
data, _ := json.Marshal(b)
|
||||
msg, err := nc.Request("mesh.seat.mesh-delivery-checker.tool.input", data, 5*time.Second)
|
||||
msg, err := nc.Request("mesh.seat.mesh-wants.tool.input", data, 5*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
checkerclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient"
|
||||
checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
@@ -25,17 +24,24 @@ import (
|
||||
|
||||
const runCommit = "5555555555555555555555555555555555555555"
|
||||
|
||||
// runInputVerb is the seat verb a run ask's input is served on, as the checker names it in the ask.
|
||||
const runInputVerb = checkerclient.InputVerb
|
||||
// runInputVerb is the seat verb a run ask's input is served on, as the ask names it.
|
||||
const runInputVerb = "seat:mesh-wants.input"
|
||||
|
||||
// answersInput is a seat verb serving body by its digest, as the checker's verb input answers (sdkstep.RunInputAnswer).
|
||||
// inputAnswerOf is the seat mesh-wants's answer of the input body as the tool runner hands it over: the digest and
|
||||
// the whole file.
|
||||
func inputAnswerOf(body []byte) map[string]any {
|
||||
sum := sha256.Sum256(body)
|
||||
return map[string]any{"digest": hex.EncodeToString(sum[:]), "chunks": 1, "file": base64.StdEncoding.EncodeToString(body)}
|
||||
}
|
||||
|
||||
// answersInput is a seat verb serving body by its digest, as the seat mesh-wants' verb input answers.
|
||||
func answersInput(body string) func(key string, b any) (json.RawMessage, error) {
|
||||
return func(key string, b any) (json.RawMessage, error) {
|
||||
ask, _ := b.(checker.InputInput)
|
||||
ask, _ := b.(meshwants.InputInput)
|
||||
if sum := sha256.Sum256([]byte(body)); key != runInputVerb || ask.Digest != hex.EncodeToString(sum[:]) {
|
||||
return nil, fmt.Errorf("the checker keeps no input %s", ask.Digest)
|
||||
return nil, fmt.Errorf("the assigner keeps no input %s", ask.Digest)
|
||||
}
|
||||
return json.Marshal(checkerclient.InputAnswerOf([]byte(body)))
|
||||
return json.Marshal(inputAnswerOf([]byte(body)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +51,7 @@ func runAskOf(t *testing.T, body string) sdkstep.StepAsk {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-checker", At: time.Now(),
|
||||
return sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: sdkstep.AskCheck, Language: sdkstep.AskGo, Asker: "mesh-delivery-runner", At: time.Now(),
|
||||
Run: &sdkstep.RunJob{Program: sdkstep.RunProgram{Repository: "novox/mesh-delivery", Commit: runCommit, Dir: ".", Package: "./cmd/prog"},
|
||||
Heads: []sdkstep.RunHead{{Repository: "novox/mesh-sdk", Commit: runCommit}, {Repository: "novox/mesh-host", Commit: runCommit}},
|
||||
Input: in, Args: []string{"-seats"}}}
|
||||
@@ -112,7 +118,7 @@ func TestAKeptProgramIsRunOverItsHeadsAndInput(t *testing.T) {
|
||||
m.Inside, m.Run = true, StepRunner(t.TempDir())
|
||||
// An input other than the one named is refused, naming it; one the verb does not answer fails the step, saying why.
|
||||
m.AskInput = func(_ string, b any) (json.RawMessage, error) {
|
||||
return json.Marshal(checker.InputAnswer{Digest: b.(checker.InputInput).Digest, Input: base64.StdEncoding.EncodeToString([]byte(`{"other":1}`))})
|
||||
return json.Marshal(map[string]any{"digest": b.(meshwants.InputInput).Digest, "chunks": 1, "file": base64.StdEncoding.EncodeToString([]byte(`{"other":1}`))})
|
||||
}
|
||||
if _, err := m.run(context.Background(), fetch, a); err == nil || !strings.Contains(err.Error(), "is not the one the ask names") || !errors.As(err, new(*steprun.Refusal)) {
|
||||
t.Errorf("a wrong input: %v", err)
|
||||
@@ -182,22 +188,22 @@ func TestARunsInputIsAskedOfTheVerbTheAskNames(t *testing.T) {
|
||||
// answersInputOn is answersInput for the seat verb wanted.
|
||||
func answersInputOn(wanted, body string) func(key string, b any) (json.RawMessage, error) {
|
||||
return func(key string, b any) (json.RawMessage, error) {
|
||||
ask, _ := b.(checker.InputInput)
|
||||
ask, _ := b.(meshwants.InputInput)
|
||||
if sum := sha256.Sum256([]byte(body)); key != wanted || ask.Digest != hex.EncodeToString(sum[:]) {
|
||||
return nil, fmt.Errorf("%s keeps no input %s", key, ask.Digest)
|
||||
}
|
||||
return json.Marshal(checkerclient.InputAnswerOf([]byte(body)))
|
||||
return json.Marshal(inputAnswerOf([]byte(body)))
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-237, A-249: the input of a run is of any size: the mesh-catalog seat's verb input, marked chunks, is asked by the
|
||||
// Design 47 G A-237, A-249: the input of a run is of any size: the mesh-wants seat's verb input, marked chunks, is asked by the
|
||||
// plain seat call and answers {digest, chunks, file}, the file whole as the tool runner received it; the bytes are checked.
|
||||
func TestARunsInputOfAnySizeIsTakenFromTheChunkedVerb(t *testing.T) {
|
||||
body := strings.Repeat("0123456789", 300000) // 3 MB
|
||||
sum := sha256.Sum256([]byte(body))
|
||||
in := sdkstep.RunInput{Verb: "seat:mesh-catalog.input", Digest: hex.EncodeToString(sum[:]), Size: len(body)}
|
||||
m := &Module{AskInput: func(key string, b any) (json.RawMessage, error) {
|
||||
if ask, _ := b.(checker.InputInput); key != in.Verb || ask.Digest != in.Digest {
|
||||
if ask, _ := b.(meshwants.InputInput); key != in.Verb || ask.Digest != in.Digest {
|
||||
return nil, fmt.Errorf("%s keeps no input %v", key, b)
|
||||
}
|
||||
return json.Marshal(map[string]any{"digest": in.Digest, "chunks": 6, "file": base64.StdEncoding.EncodeToString([]byte(body))})
|
||||
|
||||
@@ -9,10 +9,9 @@ require (
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
|
||||
@@ -43,7 +43,6 @@
|
||||
"seat:oci-artifact-registry.publish-part",
|
||||
"seat:go-package-registry.publish",
|
||||
"seat:go-package-registry.publish-part",
|
||||
"seat:mesh-delivery-checker.input",
|
||||
"seat:mesh-catalog.input",
|
||||
"seat:mesh-wants.input",
|
||||
"seat:mesh-module-assigner.assignments",
|
||||
|
||||
@@ -31,16 +31,24 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps {
|
||||
Fetch: byName["build_image_fetch"], Build: byName["build_image_build"], Copy: byName["build_image_copy"],
|
||||
Release: func(ws string) error { return m.spaces().Release(ws) },
|
||||
Logf: m.Say,
|
||||
Node: m.Node,
|
||||
SayStep: m.sayStep(),
|
||||
}
|
||||
}
|
||||
|
||||
// sayStep says a step ask's end on this node's subject of the seat (design 47 G §24): an image has no lint, unit or integration
|
||||
// step (§328), so none is asked of this seat, but the steps of every build module can say their ends, which the kit holds.
|
||||
func (m *Module) sayStep() func(sdkstep.StepEnded) error {
|
||||
if m.SayStep != nil {
|
||||
return m.SayStep
|
||||
}
|
||||
return steprun.SayStepOn(sdkstep.AskImage, m.Node, stdio.SeatPublish)
|
||||
}
|
||||
|
||||
// Puller is the module's pull of its seat's asks, on source (the runtime's seat pull when nil).
|
||||
func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller {
|
||||
if source == nil {
|
||||
source = &steprun.SeatAsks{Language: sdkstep.AskImage, Logf: m.Say}
|
||||
if m.Callers == nil {
|
||||
m.Callers = steprun.SeatListCallers(sdkstep.AskImage, m.Say)
|
||||
}
|
||||
}
|
||||
capacity := m.Capacity
|
||||
// The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config
|
||||
@@ -69,7 +77,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul
|
||||
if m.SayEvent != nil {
|
||||
say = steprun.SayOn(sdkstep.AskImage, m.Node, m.SayEvent)
|
||||
}
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskImage, Say: say, Callers: m.Callers, Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskImage, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Eligible: m.Eligible, Logf: m.Say,
|
||||
Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) {
|
||||
if err := m.Check(ctx); err != nil {
|
||||
|
||||
@@ -11,15 +11,11 @@ import (
|
||||
func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) {
|
||||
var subjects []string
|
||||
m := &Module{Node: "ace", Capacity: func() int { return 0 },
|
||||
Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" },
|
||||
SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }}
|
||||
p := m.Puller(nil, steprun.NewFakeAsks())
|
||||
if p.Say == nil {
|
||||
t.Fatal("the puller says no event")
|
||||
}
|
||||
if got, ok := p.Callers("step"); !ok || len(got) != 1 {
|
||||
t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok)
|
||||
}
|
||||
seat, _ := sdkstep.BuildSeat(sdkstep.AskImage)
|
||||
for _, event := range []string{"ask-taken", "step-progress", "step-ended"} {
|
||||
if err := p.Say(event, nil, "id"); err != nil {
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
@@ -44,3 +45,19 @@ func TestTheStepsSayAFailedNetworkAsATransportFault(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A-489b, design 47 G §24: the module's own steps can say a pipeline step's end, or
|
||||
// every step ask the runner sends it is refused ("this build module says no step's end"), as the first live pipeline met.
|
||||
func TestTheStepsCanSayAStepsEnd(t *testing.T) {
|
||||
conformance.StepEnds(t, (&Module{Node: "kitnode"}).askSteps(nil))
|
||||
}
|
||||
|
||||
// A-489c (A-489a): the module's puller counts the runs its steps state says started, not the bus's deliveries (an ask given back before
|
||||
// it ran is no run), so the poison rule never ends an ask that never ran.
|
||||
func TestThePullerCountsRunsThatStarted(t *testing.T) {
|
||||
conformance.RunsCounted(t, func(read func(string) (json.RawMessage, error)) *steprun.Puller {
|
||||
m := &Module{Node: "kitnode", Capacity: func() int { return 1 }}
|
||||
m.ReadSteps = StateRead{Get: read}
|
||||
return m.Puller(nil, steprun.NewFakeAsks())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -20,14 +20,13 @@ const CheckRound = 10 * time.Minute
|
||||
|
||||
// Module is the image build module's one running state.
|
||||
type Module struct {
|
||||
// Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when
|
||||
// the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163.
|
||||
Callers func(kind string) ([]string, bool)
|
||||
// Outputs is the object store of a run's answers over what step-ended carries inline (steprun.OutputsStore); nil keeps none.
|
||||
Outputs steprun.OutputObjects
|
||||
// SayEvent publishes an event of the image build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the
|
||||
// puller's ask-taken, step-progress and step-ended (design 47 G §496, §602).
|
||||
SayEvent func(subject string, body any, id string) error
|
||||
// SayStep says a step ask's end (steprun.AskSteps.SayStep); the seat's publish on this node when nil (a test's own).
|
||||
SayStep func(sdkstep.StepEnded) error
|
||||
|
||||
Node string
|
||||
// RecordAct writes an act in the hand-act log of the seat mesh-hand-acts (§463, §480): a drop writes it first.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
// What this module keeps on the bus, announced from its code (novox/hq ADR 0495 §2 §5; issue 1790): every state it
|
||||
// keeps, each readable by others, since another module reads each today (the builder, the checker and the indexer read toolchains; the builder and the checker read steps; gitea reads keys). A state of its own that it
|
||||
// keeps, each readable by others, since another module reads each today (the builder and the indexer read toolchains; the builder reads steps; gitea reads keys). A state of its own that it
|
||||
// did not name here is refused at the call (stdio.Keeps).
|
||||
|
||||
import (
|
||||
|
||||
@@ -41,16 +41,26 @@ func (m *Module) askSteps(steps []StepTool) steprun.AskSteps {
|
||||
},
|
||||
Release: func(ws string) error { return m.spaces().Release(ws) },
|
||||
Logf: m.Say,
|
||||
Has: m.Has,
|
||||
Lint: m.lintStep,
|
||||
Node: m.Node,
|
||||
SayStep: m.sayStep(),
|
||||
}
|
||||
}
|
||||
|
||||
// sayStep says a pipeline step's end on this node's subject of the seat (design 47 G §24, *Asked on a work queue, ended by an
|
||||
// event*): SayStep when a test sets it, else the runtime's seat publish.
|
||||
func (m *Module) sayStep() func(sdkstep.StepEnded) error {
|
||||
if m.SayStep != nil {
|
||||
return m.SayStep
|
||||
}
|
||||
return steprun.SayStepOn(sdkstep.AskNode, m.Node, stdio.SeatPublish)
|
||||
}
|
||||
|
||||
// Puller is the module's pull of its seat's asks, on source (the runtime's seat pull when nil).
|
||||
func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Puller {
|
||||
if source == nil {
|
||||
source = &steprun.SeatAsks{Language: sdkstep.AskNode, Logf: m.Say}
|
||||
if m.Callers == nil {
|
||||
m.Callers = steprun.SeatListCallers(sdkstep.AskNode, m.Say)
|
||||
}
|
||||
}
|
||||
capacity := m.Capacity
|
||||
// The capacity's own change wakes the puller (issue 1887): the runtime's watch of the config value as mesh-config
|
||||
@@ -79,7 +89,7 @@ func (m *Module) Puller(steps []StepTool, source steprun.AskSource) *steprun.Pul
|
||||
if m.SayEvent != nil {
|
||||
say = steprun.SayOn(sdkstep.AskNode, m.Node, m.SayEvent)
|
||||
}
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskNode, Say: say, Callers: m.Callers, Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
return &steprun.Puller{KeepOutput: steprun.KeepOutputIn(m.Outputs), Source: source, Language: sdkstep.AskNode, Say: say, Prior: steprun.PriorRuns(m.ReadSteps.Get), Node: m.Node, Put: m.Steps, PausePut: m.Pauses, Load: load, ForgetPause: m.ForgetPause, WatchPauses: m.WatchPauses, PauseKeys: m.ReadPauses.Keys,
|
||||
Capacity: capacity, SettingsFile: settings, WatchConfig: watch, CoresPerStep: cores, BuildHours: hours, Logf: m.Say,
|
||||
Run: func(ctx context.Context, a sdkstep.StepAsk) (any, error) {
|
||||
if err := m.Prepare(ctx); err != nil {
|
||||
|
||||
@@ -11,15 +11,11 @@ import (
|
||||
func TestPullerSaysTheSeatsEventsOnItsNodesSubject(t *testing.T) {
|
||||
var subjects []string
|
||||
m := &Module{Node: "ace", Capacity: func() int { return 0 },
|
||||
Callers: func(kind string) ([]string, bool) { return []string{"mesh-pipeline-runner"}, kind == "step" },
|
||||
SayEvent: func(subject string, _ any, _ string) error { subjects = append(subjects, subject); return nil }}
|
||||
p := m.Puller(nil, steprun.NewFakeAsks())
|
||||
if p.Say == nil {
|
||||
t.Fatal("the puller says no event")
|
||||
}
|
||||
if got, ok := p.Callers("step"); !ok || len(got) != 1 {
|
||||
t.Fatalf("the puller checks the callers of %v, %v, not the module's", got, ok)
|
||||
}
|
||||
seat, _ := sdkstep.BuildSeat(sdkstep.AskNode)
|
||||
for _, event := range []string{"ask-taken", "step-progress", "step-ended"} {
|
||||
if err := p.Say(event, nil, "id"); err != nil {
|
||||
|
||||
@@ -32,7 +32,7 @@ func fakeSteps(calls *[]string) []StepTool {
|
||||
}
|
||||
|
||||
func nodeAsk(kind string) sdkstep.StepAsk {
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: kind, Language: sdkstep.AskNode, Asker: "mesh-delivery-checker", At: time.Now()}
|
||||
a := sdkstep.StepAsk{ID: sdkstep.NewID(), Kind: kind, Language: sdkstep.AskNode, Asker: "mesh-delivery-runner", At: time.Now()}
|
||||
fetch := sdkstep.Fetch{Repository: "novox/mesh-catalog", Commit: askCommit}
|
||||
if kind == sdkstep.AskDelivery {
|
||||
a.Asker = "mesh-delivery-builder"
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
||||
)
|
||||
@@ -226,3 +227,54 @@ func TestTheTestScriptFindsTscAndEsbuildOnItsPath(t *testing.T) {
|
||||
t.Errorf("npm ran with %v, want NODE_OPTIONS naming the TAP reporter and its file", npm.env)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G §328 (*Lint per language*: TypeScript's lint is the type-check) and §24: the pipeline step lint of a TypeScript
|
||||
// artifact runs tsc without emitting over the module's directory; a type error fails it naming the file, a clean package passes,
|
||||
// a tree with no .ts file has nothing to lint, and a tsc that cannot start is an error, not a verdict.
|
||||
func TestTheLintStepIsTheTypeCheck(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
files map[string]string
|
||||
ran forgekey.Ran
|
||||
failed int
|
||||
nothing bool
|
||||
err bool
|
||||
}{
|
||||
{"a type error", map[string]string{"tools/bad.ts": `export const n: number = "text";`}, forgekey.Ran{Status: 2, Stdout: tscTypeError}, 1, false, false},
|
||||
{"a clean package", map[string]string{"index.ts": `export const a = 1;`}, forgekey.Ran{}, 0, false, false},
|
||||
{"no TypeScript", map[string]string{"package.json": `{"name":"x"}`}, forgekey.Ran{}, 0, true, false},
|
||||
{"tsc cannot start", map[string]string{"index.ts": `export const a = 1;`}, forgekey.Ran{Status: 127, Err: "no such file"}, 0, false, true},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
m, f, work := newMachine(t)
|
||||
workspaceWith(t, work, c.files)
|
||||
f.answer["tsc"] = c.ran
|
||||
out, err := m.lintStep(context.Background(), map[string]any{"workspace": "w1", "dir": "."})
|
||||
if (err != nil) != c.err {
|
||||
t.Fatalf("error = %v, want error %v", err, c.err)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
la := out.(steprun.LintAnswer)
|
||||
if len(la.Failed) != c.failed || la.Nothing != c.nothing {
|
||||
t.Fatalf("answer = %+v, want %d failed, nothing %v", la, c.failed, c.nothing)
|
||||
}
|
||||
if c.failed > 0 && !strings.Contains(la.Failed[0].Cause+la.Failed[0].Output, "tools/bad.ts") {
|
||||
t.Errorf("the failure %+v does not name tools/bad.ts", la.Failed[0])
|
||||
}
|
||||
if !c.nothing && !strings.Contains(joined(f.calls[0]), "--noEmit") {
|
||||
t.Errorf("the lint ran %s, want tsc --noEmit", joined(f.calls[0]))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G §24: the Node build module's steps carry what a step ask needs (its node, the lint body, the end's publisher and
|
||||
// its own need), so the ask the first live pipeline sent is no longer refused as "says no step's end".
|
||||
func TestTheNodeStepsCarryWhatAStepAskNeeds(t *testing.T) {
|
||||
s := (&Module{Node: "shanks"}).askSteps(nil)
|
||||
if s.Lint == nil || s.Node != "shanks" || s.SayStep == nil || s.Has == nil || !s.Has(sdkstep.NeedNodeBuildNode) || s.Has("other") {
|
||||
t.Errorf("steps = %+v, want Lint, Node, SayStep and Has set", s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -49,3 +50,19 @@ func TestTheStepsSayAFailedNetworkAsATransportFault(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A-489b, design 47 G §24: the module's own steps can say a pipeline step's end, or
|
||||
// every step ask the runner sends it is refused ("this build module says no step's end"), as the first live pipeline met.
|
||||
func TestTheStepsCanSayAStepsEnd(t *testing.T) {
|
||||
conformance.StepEnds(t, (&Module{Node: "kitnode"}).askSteps(nil))
|
||||
}
|
||||
|
||||
// A-489c (A-489a): the module's puller counts the runs its steps state says started, not the bus's deliveries (an ask given back before
|
||||
// it ran is no run), so the poison rule never ends an ask that never ran.
|
||||
func TestThePullerCountsRunsThatStarted(t *testing.T) {
|
||||
conformance.RunsCounted(t, func(read func(string) (json.RawMessage, error)) *steprun.Puller {
|
||||
m := &Module{Node: "kitnode", Capacity: func() int { return 1 }}
|
||||
m.ReadSteps = StateRead{Get: read}
|
||||
return m.Puller(nil, steprun.NewFakeAsks())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
||||
)
|
||||
|
||||
// **The type-check is a TypeScript artifact's pipeline step lint** (design 47 G §328 *Lint per language*: Go's lint is the Go
|
||||
// build module's linter, TypeScript's the type-check; §22: lint is a step the build seats serve; §24: asked on a work queue,
|
||||
// its end said as an event). It takes {"workspace", "dir"} as the lint step of mesh-build-go does and runs tsc over each
|
||||
// TypeScript package of the module's directory (testTargets, the way the unit level finds them), without emitting: a package
|
||||
// with no .ts file has nothing to lint, and nothing at all ends the step `nothing to run`. A failure is the step's, naming
|
||||
// the tsc output as the unit level names it; a tsc that could not start is an error, never a verdict.
|
||||
func (m *Module) lintStep(ctx context.Context, args map[string]any) (any, error) {
|
||||
workspace, _ := args["workspace"].(string)
|
||||
dir, _ := args["dir"].(string)
|
||||
root, mdir, err := m.moduleDirOf(workspace, dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
targets, err := testTargets(mdir, filepath.ToSlash(filepath.Clean(dir)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var failed []Failure
|
||||
linted := false
|
||||
for _, tg := range targets {
|
||||
sources, err := typeScriptFiles(tg.dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
continue
|
||||
}
|
||||
linted = true
|
||||
if err := m.provide(root); err != nil {
|
||||
return nil, fmt.Errorf("providing the toolchain's packages to the workspace: %w", err)
|
||||
}
|
||||
r := m.runIn(ctx, root, false, tg.dir, nil, "node", m.typeCheck(tg.dir, sources)...)
|
||||
if r.Err != "" {
|
||||
return nil, fmt.Errorf("type-check could not be run: status %d %s: %s", r.Status, r.Err, firstLine(r.Stderr))
|
||||
}
|
||||
if r.Status != 0 {
|
||||
failed = append(failed, testFailures(tg.where, "type-check", tg.dir, r, "")...)
|
||||
}
|
||||
}
|
||||
return steprun.LintAnswer{Failed: failed, Nothing: !linted}, nil
|
||||
}
|
||||
|
||||
// A-489b: Has says whether this node has a need a package's tests name (sdkstep.AskNeeds): the need node-build-node is this module's own
|
||||
// (a TypeScript package's tests run through it); any other it lacks.
|
||||
func (m *Module) Has(need string) bool { return need == sdkstep.NeedNodeBuildNode }
|
||||
@@ -25,15 +25,15 @@ const CheckRound = 10 * time.Minute
|
||||
// Module is the Node build module's one running state: the node it runs on, its recipe, the toolchain guard, the
|
||||
// toolchain directory and the states it writes (toolchains, steps).
|
||||
type Module struct {
|
||||
// Callers says which seats the build seat's definition lists as callers of an accepted kind (steprun.SeatListCallers when
|
||||
// the module pulls through its runtime; nil in a test checks no named asker): design 47 G §543, A-163.
|
||||
Callers func(kind string) ([]string, bool)
|
||||
// Outputs is the object store of a run's answers over what step-ended carries inline (steprun.OutputsStore); nil keeps none.
|
||||
Outputs steprun.OutputObjects
|
||||
// SayEvent publishes an event of the node build seat on a seat subject (stdio.SeatPublish; nil says none, a test's): the
|
||||
// puller's ask-taken, step-progress and step-ended (design 47 G §496, §602).
|
||||
SayEvent func(subject string, body any, id string) error
|
||||
|
||||
// SayStep says a pipeline step's end (steprun.AskSteps.SayStep); the seat's publish on this node when nil (a test's own).
|
||||
SayStep func(sdkstep.StepEnded) error
|
||||
|
||||
Node string
|
||||
// Recipe is the recipe in force. Reload, when set, reads it again at every check (the lock file the mesh
|
||||
// writes can change under a running module); a failure to read it is a refusal.
|
||||
|
||||
@@ -166,28 +166,7 @@ var commands = []command{
|
||||
return call{"mesh-delivery.deliveries", pick(l, "state", "repository", "group", "all")}, nil
|
||||
}},
|
||||
{words: []string{"delivery"}, usage: "<id>", does: "one delivery or group whole", build: needArg("mesh-delivery.show", "id")},
|
||||
{words: []string{"groups"}, usage: "[--all]", does: "every delivery group", allowed: []string{"all"},
|
||||
build: func(l line) (call, error) { return call{"mesh-delivery.groups", pick(l, "all")}, nil }},
|
||||
{words: []string{"stalled"}, does: "every delivery held past its state's bound", build: fixed("mesh-delivery.stalled")},
|
||||
{words: []string{"checks"}, usage: "<owner/repo> [--number n | --commit c]", does: "what the mesh's checks said of a head",
|
||||
allowed: []string{"number", "commit"}, build: func(l line) (call, error) {
|
||||
if len(l.pos) != 1 {
|
||||
return call{}, usageErr("give the repository, owner/repo")
|
||||
}
|
||||
a := pick(l, "number", "commit")
|
||||
a["repository"] = l.pos[0]
|
||||
return call{"mesh-delivery.checks", a}, nil
|
||||
}},
|
||||
{words: []string{"try"}, usage: "<owner/repo> <head> [--level l] [--trunk t] [--tests t]",
|
||||
does: "try a pushed head before its pull request is opened", allowed: []string{"level", "trunk", "tests"},
|
||||
build: func(l line) (call, error) {
|
||||
if len(l.pos) != 2 {
|
||||
return call{}, usageErr("give the repository and the head")
|
||||
}
|
||||
a := pick(l, "level", "trunk", "tests")
|
||||
a["repository"], a["head"] = l.pos[0], l.pos[1]
|
||||
return call{"mesh-delivery-checker.try", a}, nil
|
||||
}},
|
||||
{words: []string{"builds"}, usage: "[--since <RFC 3339 time>]", does: "every build that ended since (the last day by default)",
|
||||
allowed: []string{"since"}, build: func(l line) (call, error) {
|
||||
since := l.flags["since"]
|
||||
@@ -289,12 +268,12 @@ var commands = []command{
|
||||
does: "clear one of a module's config values on one node", allowed: []string{"node", "why"},
|
||||
build: func(l line) (call, error) { return configChange(l, "clear") }},
|
||||
{words: []string{"config"}, usage: "[list|set|clear|history|propose|proposals] [--module m] [--node n] [--key k] [--value v] [--why w]",
|
||||
does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "pull", "clear"},
|
||||
does: "the mesh's config values, at any level", allowed: []string{"module", "node", "key", "value", "why", "limit", "proposal", "clear"},
|
||||
build: func(l line) (call, error) {
|
||||
if len(l.pos) > 1 {
|
||||
return call{}, usageErr("give at most one action")
|
||||
}
|
||||
a := pick(l, "module", "node", "key", "why", "proposal", "pull", "clear", "limit")
|
||||
a := pick(l, "module", "node", "key", "why", "proposal", "clear", "limit")
|
||||
if len(l.pos) == 1 {
|
||||
a["action"] = l.pos[0]
|
||||
}
|
||||
@@ -387,15 +366,16 @@ var commands = []command{
|
||||
{words: []string{"licence"}, instead: "anthropic-licence-manager.adopt takes an API key from a file on the manager's node; nox licence list|use|refresh|key"},
|
||||
{words: []string{"module", "list"}, does: "every module the indexer holds", build: fixed("mesh-delivery-view.indexer_modules")},
|
||||
{words: []string{"module", "show"}, usage: "<module>", does: "what the indexer holds of one module", build: needArg("mesh-delivery-view.indexer_module", "module")},
|
||||
{words: []string{"module", "moved"}, usage: "<module> --why <text>", does: "take a registered module in now", allowed: []string{"why"},
|
||||
{words: []string{"head", "take"}, usage: "<owner/repository> <branch> --why <text>", does: "take a held head in by hand (design 47 H §742, H-A-105)", allowed: []string{"why"},
|
||||
build: func(l line) (call, error) {
|
||||
if len(l.pos) < 1 {
|
||||
return call{}, usageErr("give the module")
|
||||
if len(l.pos) != 2 {
|
||||
return call{}, usageErr("give the repository and the branch")
|
||||
}
|
||||
return call{"mesh-delivery-indexer.indexer_take_in", map[string]any{"module": l.pos[0], "why": l.flags["why"]}}, nil
|
||||
return call{"mesh-delivery-gate.gate_head_take", map[string]any{"repository": l.pos[0], "branch": l.pos[1], "why": l.flags["why"]}}, nil
|
||||
}},
|
||||
{words: []string{"module"}, instead: "the indexer takes modules in from their repositories; nox module list|show|moved, and nothing forgets one by hand yet"},
|
||||
{words: []string{"register"}, instead: "no seat registers a module from another repository yet (novox/hq ADR 0501 §4); nox module moved <module> takes a registered one in"},
|
||||
{words: []string{"head", "list"}, does: "every untaken head the gate holds", build: fixed("mesh-delivery-gate.gate_heads")},
|
||||
{words: []string{"module"}, instead: "the indexer takes modules in from their repositories; nox module list|show, nox head take for a held head, and nothing forgets one by hand yet"},
|
||||
{words: []string{"register"}, instead: "no seat registers a module from another repository yet (novox/hq ADR 0501 §4); nox head take <repository> <branch> takes a held head in"},
|
||||
{words: []string{"account", "issue"}, usage: "<user>", does: "mint one bus account the grants part wants, now", build: needArg("mesh-vault.issue-account", "user")},
|
||||
|
||||
// Issues and records.
|
||||
|
||||
@@ -88,6 +88,8 @@ func TestEachLineCallsItsOwner(t *testing.T) {
|
||||
args string
|
||||
}{
|
||||
{"node show ace", "mesh-module-assigner.assignments", `{"node":"ace"}`},
|
||||
{"head take novox/x main --why moved", "mesh-delivery-gate.gate_head_take", `{"branch":"main","repository":"novox/x","why":"moved"}`},
|
||||
{"head list", "mesh-delivery-gate.gate_heads", `{}`},
|
||||
{"node public-domain ace", "mesh-module-assigner.assignments", `{"node":"ace"}`},
|
||||
{"node public-domain ace example.org --why moved", "mesh-module-assigner.propose", `{"changes":[{"field":"public-domain","node":"ace","op":"decide","value":"example.org"}],"why":"moved"}`},
|
||||
{"plan ace", "mesh-delivery-view.assigner_wants", `{"node":"ace"}`},
|
||||
@@ -100,8 +102,6 @@ func TestEachLineCallsItsOwner(t *testing.T) {
|
||||
{"unpin ace postgres-database --why back", "mesh-module-assigner.propose", `{"changes":[{"node":"ace","op":"unpin","provision":"postgres-database"}],"why":"back"}`},
|
||||
{"deliveries --state merged --json", "mesh-delivery.deliveries", `{"state":"merged"}`},
|
||||
{"delivery novox/mesh-sdk@dff0ce2abb21", "mesh-delivery.show", `{"id":"novox/mesh-sdk@dff0ce2abb21"}`},
|
||||
{"checks novox/mesh-catalog --number 591", "mesh-delivery.checks", `{"number":"591","repository":"novox/mesh-catalog"}`},
|
||||
{"try novox/mesh-catalog abc123 --level unit", "mesh-delivery-checker.try", `{"head":"abc123","level":"unit","repository":"novox/mesh-catalog"}`},
|
||||
{"builds", "mesh-delivery-builder.builder_builds_since", `{"since":"2026-11-01T10:00:00Z"}`},
|
||||
{"upgrade nats --policy record --why bus", "mesh-wants.upgrade", `{"module":"nats","policy":"record","why":"bus"}`},
|
||||
{"bus", "mesh-wants.bus", `{}`},
|
||||
@@ -173,7 +173,12 @@ func TestALineWithNoOwnerOrNotReadCallsNothingAndSaysWhy(t *testing.T) {
|
||||
{"conditions --bogus x", 2, "--bogus is not a flag"},
|
||||
{"settings set zsh {\"execute\":\"withhold\"} --node g14 --why x", 2, "set each key"},
|
||||
{"healers --days soon", 2, "takes a number"},
|
||||
{"config propose --pull novox/x#1", 2, "--pull is not a flag"},
|
||||
{"frobnicate", 2, "is not a command"},
|
||||
// The checker and the groups go whole (design 47 H §764): nox calls neither.
|
||||
{"try novox/mesh-catalog abc123 --level unit", 2, "is not a command"},
|
||||
{"groups", 2, "is not a command"},
|
||||
{"checks novox/mesh-catalog --number 591", 2, "is not a command"},
|
||||
} {
|
||||
got := console(t, nil)
|
||||
args := strings.Fields(c.line)
|
||||
|
||||
@@ -321,5 +321,5 @@ func decodeWant(value []byte) (motion.Want, error) {
|
||||
if err := json.Unmarshal(value, &w); err != nil {
|
||||
return motion.Want{}, err
|
||||
}
|
||||
return motion.Want{FP: w.Fp, Composed: w.Composed, Removed: w.Removed}, nil
|
||||
return motion.Want{FP: w.Fp, Removed: w.Removed}, nil
|
||||
}
|
||||
|
||||
@@ -34,8 +34,9 @@ const (
|
||||
// failed delivery raises what stands at once; another module's and the machine's own are raised at once; compare lists a
|
||||
// held key as held.
|
||||
func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) {
|
||||
want := func(fp, composed string) []byte {
|
||||
raw, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp, Composed: composed})
|
||||
// A want names the fingerprint alone (design 47 H, H-A-78): a have at it is the want applied.
|
||||
want := func(fp string) []byte {
|
||||
raw, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp})
|
||||
return raw
|
||||
}
|
||||
have := func(fp, composed string, phase, h string) haveSpec { return haveSpec{fp, composed, phase, h} }
|
||||
@@ -55,10 +56,10 @@ func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) {
|
||||
said: "raised module.mesh-controller.novox.unhealthy", line: "as the delivery of version 0d1e2f3a4b5c"},
|
||||
} {
|
||||
r := newRig()
|
||||
_ = r.k.Want("want.novox.mesh-controller", want(ctrlRunning, ctrlComposed), false)
|
||||
_ = r.k.Want("want.novox.mesh-controller", want(ctrlRunning), false)
|
||||
r.engineHave("novox.mesh-controller", have(ctrlRunning, ctrlComposed, motion.PhaseRunning, engineclient.HealthOK))
|
||||
// The delivery starts: the assigner wants the next fingerprint; the node applies it.
|
||||
_ = r.k.Want("want.novox.mesh-controller", want(ctrlNext, ctrlNextComp), false)
|
||||
_ = r.k.Want("want.novox.mesh-controller", want(ctrlNext), false)
|
||||
r.engineHave("novox.mesh-controller", have(ctrlNext, ctrlNextComp, motion.PhaseApplying, engineclient.HealthUnknown))
|
||||
own, ownRaw := finding(t, watchman, cc.ScopeModule, "mesh-controller.novox", "unhealthy", "health", cc.Warning, r.now)
|
||||
other, otherRaw := finding(t, watchman, cc.ScopeModule, "postgres.novox", "unhealthy", "health", cc.Warning, r.now)
|
||||
@@ -109,7 +110,7 @@ func TestADeliveryHoldsItsOwnFindingsUntilItEnds(t *testing.T) {
|
||||
}
|
||||
r.k.Assignments("ace.mesh-delivery-assigner", placed["ace.mesh-delivery-assigner"], false)
|
||||
r.k.Assignments("ace.node", placed["ace.node"], false)
|
||||
_ = r.k.Want("want.ace.mesh-delivery-assigner", want(ctrlNext, ctrlNextComp), false)
|
||||
_ = r.k.Want("want.ace.mesh-delivery-assigner", want(ctrlNext), false)
|
||||
r.engineHave("ace.mesh-delivery-assigner", have(ctrlRunning, ctrlComposed, motion.PhaseRunning, engineclient.HealthOK))
|
||||
seatKey, seatRaw := finding(t, selfCheck, cc.ScopeSeat, "mesh-module-assigner.ace", "holder-silent", "D3", cc.Warning, r.now)
|
||||
_ = r.k.Finding(selfCheck, seatKey, seatRaw, false)
|
||||
|
||||
@@ -30,7 +30,7 @@ module none of whose manifests can be read is listed under `unresolved` with why
|
||||
values last resolved for it. Then: the manifest's default; mesh config for the predefined key the value falls back
|
||||
to; node config for that key on the node; the module's own value on that node, the most specific winning. A value
|
||||
naming no predefined key resolves from its default and the module's own value only (ADR 0348 §2). It resolves at
|
||||
start, whenever an assignment, a want or a trunk module moves, and every five minutes, writing only the values that moved.
|
||||
start, whenever an assignment, a want or a trunk module moves, writing only the values that moved.
|
||||
|
||||
## The verb
|
||||
|
||||
@@ -87,8 +87,9 @@ Each directory or archive this module declares says how it treats a file it did
|
||||
- `mesh-config_declared`: each placed module's declarations at the trunk, with the manifest and commit they come
|
||||
from, written only when they differ.
|
||||
- Its own sources hold always (novox/hq ADR 0590): declarations from the modules' own manifests, the list from the
|
||||
indexer's `predefined`, pull requests' config from the indexer's `config-candidates`. The controller's
|
||||
`pull-declares`, the take-over, compare and hand-back went with the controller. `config_declared` and
|
||||
indexer's `predefined`. The controller's `pull-declares`, the take-over, compare and hand-back went with the
|
||||
controller, and so did every pull request path (`pulls`, `kept`, `held`, a proposal's `pull`): the mesh knows no pull
|
||||
request (design 47 G, ADR 0502 ended by the operator's word, record pending). `config_declared` and
|
||||
`config_resync` read and write again what it keeps.
|
||||
- A trusted value is set only through propose and the operator's Approve: no controller relay is trusted (issue 1126).
|
||||
- Its sets, clears and warrants are recorded in the hand-act log of the seat `mesh-hand-acts` alone.
|
||||
|
||||
@@ -73,10 +73,6 @@ type Change struct {
|
||||
Approved string `json:"approved,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
Moved []Moved `json:"moved"`
|
||||
// Pull and Head name the pull request a value is held for, dropped from, or applied from at its merge (novox/hq
|
||||
// ADR 0502): the actions hold and drop are always about one, and a set with a pull is its merge.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
Head string `json:"head,omitempty"`
|
||||
}
|
||||
|
||||
// Config is the module: its store, its clock, and one lock, so a set and a resolution never interleave.
|
||||
@@ -94,8 +90,6 @@ type Config struct {
|
||||
|
||||
// Asks is the operator channel as this module asks on it (proposals.go); nil, nothing can be proposed.
|
||||
Asks AskBus
|
||||
// Forge is the forge as pulls are asked of it (pulls.go); nil, a merge or a close is heard from its events alone.
|
||||
Forge PullForge
|
||||
// Delivery is what the delivery parts answer (renames.go, retiring.go); nil, no window closes and nothing retires.
|
||||
Delivery Delivery
|
||||
// Catalog is the catalog half: which modules exist and the manifest each entry carries, and the entry of a build's
|
||||
@@ -130,13 +124,6 @@ type Call struct {
|
||||
// Clear asks a proposal to clear the value instead of setting it; Proposal names one proposal to read.
|
||||
Clear bool
|
||||
Proposal string
|
||||
// Pull names the open pull request that introduces the key, for propose (a held value) and pull-declares; Head is
|
||||
// the commit its declarations are read at; Declares the config declarations the head adds or changes, by module;
|
||||
// Pulls the pull requests held asks about, each at a head.
|
||||
Pull string
|
||||
Head string
|
||||
Declares map[string]any
|
||||
Pulls []PullAt
|
||||
|
||||
// approval is the operator's warrant a set or a clear is made on: given by a decided proposal alone, never by
|
||||
// a call's arguments.
|
||||
@@ -177,13 +164,8 @@ func (c *Config) Do(call Call) (any, error) {
|
||||
return c.Propose(call)
|
||||
case "proposals":
|
||||
return c.Proposals(call)
|
||||
case "pull-declares":
|
||||
return c.PullDeclares(call)
|
||||
case "held":
|
||||
return c.Held(call)
|
||||
}
|
||||
return nil, fmt.Errorf("config does set, clear, list, history, propose, proposals, pull-declares or held; %q is "+
|
||||
"none of them", call.Action)
|
||||
return nil, fmt.Errorf("config does set, clear, list, history, propose or proposals; %q is none of them", call.Action)
|
||||
}
|
||||
|
||||
// level is the level a call names, refused as ADR 0348 §3 says: a module's own value is for one node.
|
||||
@@ -211,10 +193,6 @@ func (c *Config) Change(call Call) (*SetAnswer, error) {
|
||||
if call.Clear || call.Proposal != "" {
|
||||
return nil, fmt.Errorf("clear and proposal are a proposal's: %s takes neither", call.Action)
|
||||
}
|
||||
if call.Pull != "" {
|
||||
return nil, fmt.Errorf("pull names the pull request a value is held for, which only a proposal does: %s takes "+
|
||||
"none", call.Action)
|
||||
}
|
||||
call.approval = nil // only a decided proposal gives one
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
@@ -131,3 +131,37 @@ func (v toolVault) Retire(r RetireRequest) error {
|
||||
_, err = v.ask(VaultRetireAsk, map[string]any{"node": r.Node, "module": r.Module, "name": r.Name, "warrant": rec.Act.ID, "ask": r.Ask})
|
||||
return err
|
||||
}
|
||||
|
||||
// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}.
|
||||
func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return unwrapAnswer(json.RawMessage(s))
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &m) != nil {
|
||||
return raw, string(raw), true
|
||||
}
|
||||
if content, has := m["content"]; has {
|
||||
var items []struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
var e struct {
|
||||
IsError bool `json:"isError"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &e)
|
||||
if json.Unmarshal(content, &items) == nil && len(items) > 0 {
|
||||
inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text))
|
||||
return inner, out, ok && !e.IsError
|
||||
}
|
||||
}
|
||||
var v struct {
|
||||
Output string `json:"output"`
|
||||
OK *bool `json:"ok"`
|
||||
Answer json.RawMessage `json:"answer"`
|
||||
}
|
||||
if json.Unmarshal(raw, &v) == nil && v.OK != nil {
|
||||
return v.Answer, v.Output, *v.OK
|
||||
}
|
||||
return raw, string(raw), true
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ type Listed struct {
|
||||
Withheld string `json:"withheld,omitempty"`
|
||||
// Unresolved is each placed module the call narrows to whose declarations cannot be read, with why (issue 1953).
|
||||
Unresolved []Unresolved `json:"unresolved,omitempty"`
|
||||
// Held is the approved values held for open pull requests, not yet set (novox/hq ADR 0502).
|
||||
Held []HeldValue `json:"held,omitempty"`
|
||||
}
|
||||
|
||||
// SetValue is one value set, at its level.
|
||||
@@ -95,10 +93,6 @@ func (c *Config) List(call Call) (*Listed, error) {
|
||||
if call.Module == "" {
|
||||
out.Predefined = s.Predefined
|
||||
}
|
||||
out.Held = heldListed(s, call)
|
||||
if len(out.Held) == 0 {
|
||||
out.Held = nil
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -38,15 +38,19 @@ func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
|
||||
var theViews *ownerViews
|
||||
|
||||
// runtimeStore is the bus, through the runtime that launched this bundle. A state of the delivery owners that the
|
||||
// owners' tools answer is read from the picture those keep (ownerviews.go), the rest from the bus.
|
||||
type runtimeStore struct{}
|
||||
// owners' tools answer is read from the picture those keep (ownerviews.go), this module's own states from the copy their
|
||||
// watches keep (ownstates.go), the rest from the bus.
|
||||
type runtimeStore struct{ own *ownCopy }
|
||||
|
||||
func (runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
func (r runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
if theViews != nil {
|
||||
if e, followed, err := theViews.get(state, key); followed {
|
||||
return e, err
|
||||
}
|
||||
}
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.get(state, key)
|
||||
}
|
||||
e, err := stdio.State(state).Get(key)
|
||||
if err != nil || e == nil {
|
||||
return nil, err
|
||||
@@ -54,19 +58,30 @@ func (runtimeStore) Get(state, key string) (*Entry, error) {
|
||||
return &Entry{Key: e.Key, Value: e.Value, Revision: e.Revision}, nil
|
||||
}
|
||||
|
||||
func (runtimeStore) Put(state, key string, value any) error {
|
||||
func (r runtimeStore) Put(state, key string, value any) error {
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.write(state, key, value)
|
||||
}
|
||||
_, err := stdio.State(state).Put(key, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (runtimeStore) Delete(state, key string) error { return stdio.State(state).Delete(key) }
|
||||
func (r runtimeStore) Delete(state, key string) error {
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.remove(state, key)
|
||||
}
|
||||
return stdio.State(state).Delete(key)
|
||||
}
|
||||
|
||||
func (runtimeStore) Keys(state string) ([]string, error) {
|
||||
func (r runtimeStore) Keys(state string) ([]string, error) {
|
||||
if theViews != nil {
|
||||
if keys, followed, err := theViews.keys(state); followed {
|
||||
return keys, err
|
||||
}
|
||||
}
|
||||
if r.own != nil && ownStates[state] {
|
||||
return r.own.keys(state)
|
||||
}
|
||||
return stdio.State(state).Keys()
|
||||
}
|
||||
|
||||
@@ -119,8 +134,22 @@ func (runtimeAsks) Warranted(ask string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// armsDeadline says a write to this state can move the deadline timer: a proposal, a retirement or a rename window (A-490l).
|
||||
func armsDeadline(state string) bool {
|
||||
return state == ProposalsState || state == RetiringState || state == RenamesState
|
||||
}
|
||||
|
||||
func main() {
|
||||
c := &Config{Store: runtimeStore{}, Logf: logf, Asks: runtimeAsks{}, Forge: toolForge{ask: stdio.Ask},
|
||||
own, rearm := newOwnCopy(), make(chan struct{}, 1)
|
||||
own.changed = func(state string) {
|
||||
if armsDeadline(state) {
|
||||
select {
|
||||
case rearm <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
c := &Config{Store: runtimeStore{own: own}, Logf: logf, Asks: runtimeAsks{},
|
||||
Delivery: toolDelivery{ask: stdio.Ask}, Vault: toolVault{ask: stdio.Ask, recorder: recorder}}
|
||||
// The module's one held writer (design 47 G §689, A-16, A-29, A-32): the renames' and retirements' findings and the views reader's go through it.
|
||||
w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
@@ -181,6 +210,7 @@ func main() {
|
||||
theViews.Start(context.Background())
|
||||
go resolving(c, moved)
|
||||
go hearing(c)
|
||||
go deciding(c, rearm)
|
||||
go hearingDelivery(c)
|
||||
if err := stdio.Serve("", append(Tools(c), branches.EntryAgainTool("config", c.Store.HandAct))); err != nil {
|
||||
logf("%v", err)
|
||||
@@ -188,23 +218,12 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// resolving resolves at start and again whenever a source moves, and every five minutes besides, so a change
|
||||
// heard nowhere is still resolved. The runtime answers only once Serve runs, so the watches are asked again
|
||||
// until they are taken.
|
||||
// resolving resolves at start and again whenever a source moves: an assignment, a want, a catalog revision, an entry
|
||||
// answered or the predefined list (each reaches `moved` through its view, ownerviews.go and main), and never on a timer
|
||||
// (novox/hq principle 1, issue 1982). The runtime answers only once Serve runs.
|
||||
func resolving(c *Config, moved chan struct{}) {
|
||||
// The owners' states reach `moved` through their views (ownerviews.go); the builder's builds, which has no tool yet,
|
||||
// is read when a want moves, and nothing else is watched.
|
||||
tick := time.NewTicker(5 * time.Minute)
|
||||
defer tick.Stop()
|
||||
time.Sleep(time.Second)
|
||||
for sweep := true; ; {
|
||||
if sweep {
|
||||
// A merge or a close of a pull request a value is held for, heard nowhere, is found here (ADR 0502).
|
||||
for _, s := range c.SweepPulls() {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
}
|
||||
sweep = false
|
||||
<-stdio.Served()
|
||||
for {
|
||||
r, err := c.Reconcile()
|
||||
switch {
|
||||
case err != nil:
|
||||
@@ -215,21 +234,22 @@ func resolving(c *Config, moved chan struct{}) {
|
||||
if r.Published != "" {
|
||||
logf("[mesh-config] published %s", r.Published)
|
||||
}
|
||||
// A window or a retirement judged while the entry of the build it waits on was still asked stays open until the
|
||||
// entry answers: the answer moves this pass, and the open ones are judged again here (design 32 *A config value
|
||||
// renamed*). It reads nothing from the bus when none is open.
|
||||
for _, s := range c.Windows("") {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
<-moved
|
||||
time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution
|
||||
select {
|
||||
case <-moved:
|
||||
time.Sleep(500 * time.Millisecond) // a burst of writes is one resolution
|
||||
select {
|
||||
case <-moved:
|
||||
default:
|
||||
}
|
||||
case <-tick.C:
|
||||
sweep = true
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription, and
|
||||
// reads the router's record of every waiting ask once a minute besides, so an answer heard nowhere is still taken.
|
||||
// hearing takes the router's word on this module's asks, asked again until the runtime takes the subscription.
|
||||
func hearing(c *Config) {
|
||||
go func() {
|
||||
<-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard
|
||||
@@ -251,32 +271,32 @@ func hearing(c *Config) {
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}()
|
||||
for _, pattern := range []string{"git.pull-merged", "git.pull-closed"} {
|
||||
go func(pattern string) {
|
||||
<-stdio.Served() // subscribed as soon as the runtime serves, never after a guessed wait: an event sent meanwhile is heard
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
err := stdio.Subscribe(pattern, func(e stdio.Envelope) error {
|
||||
p, ok := pullEventOf(e.Body)
|
||||
if !ok {
|
||||
logf("[mesh-config] %s is not readable", pattern)
|
||||
return nil
|
||||
}
|
||||
for _, s := range c.HeardPull(p, pattern == "git.pull-merged") {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
logf("[mesh-config] does not hear %s yet (%v); asking again in %s", pattern, err, wait)
|
||||
time.Sleep(wait)
|
||||
}
|
||||
}(pattern)
|
||||
}
|
||||
|
||||
// DeadlineFloorKey is this module's setting for the least wait of its one deadline timer, in seconds (module.json `config`).
|
||||
const DeadlineFloorKey = "deadline-floor-seconds"
|
||||
|
||||
// DefaultDeadlineFloor is the floor when the setting cannot be read.
|
||||
const DefaultDeadlineFloor = time.Minute
|
||||
|
||||
// floorOf is the floor a setting's value names: its seconds, or the default for a value that is not a positive number.
|
||||
func floorOf(raw json.RawMessage, err error) time.Duration {
|
||||
var seconds float64
|
||||
if err != nil || json.Unmarshal(raw, &seconds) != nil || seconds < 1 {
|
||||
return DefaultDeadlineFloor
|
||||
}
|
||||
tick := time.NewTicker(time.Minute)
|
||||
defer tick.Stop()
|
||||
for range tick.C {
|
||||
return time.Duration(seconds * float64(time.Second))
|
||||
}
|
||||
|
||||
func deadlineFloor() time.Duration { return floorOf(stdio.Config(DeadlineFloorKey)) }
|
||||
|
||||
// deciding reads the router's record of every waiting ask once at start, which finds an answer said while this module was
|
||||
// down, and then when the router says an ask ended (hearing's `decided` subscription calls Decide itself) or the next
|
||||
// deadline of a waiting proposal or retirement passes: one timer for the earliest, set again whenever a proposal or a
|
||||
// retirement is written (issue 1982; novox/hq ADR 0426 §3: a timer is a deadline, never a poll).
|
||||
func deciding(c *Config, rearm chan struct{}) {
|
||||
<-stdio.Served()
|
||||
decide := func() {
|
||||
said, err := c.Decide()
|
||||
if err != nil {
|
||||
logf("[mesh-config] reading the operator's answers: %v", err)
|
||||
@@ -284,12 +304,31 @@ func hearing(c *Config) {
|
||||
for _, s := range said {
|
||||
logf("[mesh-config] %s", s)
|
||||
}
|
||||
if err := c.JudgeHeldWindows(); err != nil {
|
||||
logf("[mesh-config] the held windows cannot be judged: %v", err)
|
||||
}
|
||||
}
|
||||
decide()
|
||||
for {
|
||||
at, ok := c.NextDeadline()
|
||||
if !ok {
|
||||
<-rearm
|
||||
continue
|
||||
}
|
||||
// Not sooner than the floor: a deadline that passed and was not settled (an ask the router refused) is tried again, not spun on.
|
||||
timer := time.NewTimer(max(time.Until(at), deadlineFloor()))
|
||||
select {
|
||||
case <-timer.C:
|
||||
decide()
|
||||
case <-rearm:
|
||||
timer.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hearingDelivery takes the delivery parts' words that close a rename window or retire a value, each asked again until
|
||||
// the runtime takes the subscription: the status view's `delivered`, the node-engine seat's `manifest-reads-changed`
|
||||
// (a returning node-engine; design 32) and the seat mesh-catalog's `manifest-accepted`. At its start it judges the windows once,
|
||||
// (a returning node-engine; design 32) and the seat mesh-catalog's `catalog-revision` (design 47 H §764, H-A-49). At its start it judges the windows once,
|
||||
// since an event said while this module was down is not said again.
|
||||
func hearingDelivery(c *Config) {
|
||||
<-stdio.Served()
|
||||
@@ -309,15 +348,7 @@ func hearingDelivery(c *Config) {
|
||||
}
|
||||
return c.Windows(b.Node)
|
||||
}},
|
||||
{"mesh-catalog.manifest-accepted", func(e stdio.Envelope) []string {
|
||||
var b struct {
|
||||
Module string `json:"module"`
|
||||
}
|
||||
if json.Unmarshal(e.Body, &b) != nil {
|
||||
return nil
|
||||
}
|
||||
return c.Retirements(b.Module)
|
||||
}},
|
||||
{"mesh-catalog.catalog-revision.>", func(e stdio.Envelope) []string { return catalogRevisionRetirements(c, e.Body) }},
|
||||
} {
|
||||
go func(pattern string, handle func(stdio.Envelope) []string) {
|
||||
for wait := 2 * time.Second; ; wait = min(wait*2, time.Minute) {
|
||||
@@ -340,6 +371,22 @@ func hearingDelivery(c *Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// catalogRevisionRetirements judges the retirements of every module a catalog revision names changed: the seat
|
||||
// mesh-catalog's `catalog-revision` replaces `manifest-accepted`, which goes in H1 (design 47 H §764, H-A-49).
|
||||
func catalogRevisionRetirements(c *Config, body []byte) []string {
|
||||
var b struct {
|
||||
Changed []string `json:"changed"`
|
||||
}
|
||||
if json.Unmarshal(body, &b) != nil {
|
||||
return nil
|
||||
}
|
||||
var said []string
|
||||
for _, m := range b.Changed {
|
||||
said = append(said, c.Retirements(m)...)
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// seatRename is one open rename as the seat's definition answers it: module, from, to, at and the nodes holding it open.
|
||||
type seatRename struct {
|
||||
Module string `json:"module"`
|
||||
@@ -379,13 +426,10 @@ func Tools(c *Config) []stdio.Tool {
|
||||
"who and why. A trusted value is set only on the operator's Approve: " +
|
||||
"propose asks the operator, under mesh-config's own name on a channel that proves who answered, and sets " +
|
||||
"the value on Approve if what it was shown against still stands; proposals lists them, or one with " +
|
||||
"proposal. A proposal naming the open pull request that introduces its key (pull) is judged against what " +
|
||||
"that pull request declares and asked of the operator, and on Approve is held until the pull request " +
|
||||
"merges, then set; list shows the held values. pull-declares and held are the controller's. list and " +
|
||||
"history are narrowed by module, node and key.",
|
||||
"proposal. list and history are narrowed by module, node and key.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"action": map[string]any{"type": "string",
|
||||
"enum": []string{"list", "set", "clear", "history", "propose", "proposals", "pull-declares", "held"},
|
||||
"enum": []string{"list", "set", "clear", "history", "propose", "proposals"},
|
||||
"description": "list when not given"},
|
||||
"key": map[string]any{"type": "string", "description": "the config key"},
|
||||
"value": map[string]any{"description": "for set: the value, of the key's declared type"},
|
||||
@@ -398,14 +442,6 @@ func Tools(c *Config) []stdio.Tool {
|
||||
"clear": map[string]any{"type": "boolean",
|
||||
"description": "for propose: propose clearing the value at its level instead of setting one"},
|
||||
"proposal": map[string]any{"type": "string", "description": "for proposals: one proposal's id, read whole"},
|
||||
"pull": map[string]any{"type": "string", "description": "owner/repository#number: for propose, the open " +
|
||||
"pull request that introduces the key, so the value is judged against what it declares and held until " +
|
||||
"it merges (a module's own value on one node); for pull-declares, the pull request declared for"},
|
||||
"head": map[string]any{"type": "string", "description": "for pull-declares: the commit the declarations are read at"},
|
||||
"declares": map[string]any{"type": "object", "description": "for pull-declares (the controller's): the " +
|
||||
"config declarations the head adds or changes against its base, by module and key"},
|
||||
"pulls": map[string]any{"type": "array", "description": "for held (the controller's): the pull requests " +
|
||||
"asked about, each {pull, head}", "items": map[string]any{"type": "object"}},
|
||||
}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
call, err := callOf(args)
|
||||
@@ -421,8 +457,7 @@ func Tools(c *Config) []stdio.Tool {
|
||||
// callOf reads a call's arguments strictly: an argument it does not know is refused, not ignored.
|
||||
func callOf(args map[string]any) (Call, error) {
|
||||
known := map[string]bool{"action": true, "key": true, "value": true, "module": true, "node": true, "why": true,
|
||||
"by": true, "limit": true, "clear": true, "proposal": true, "pull": true, "head": true, "declares": true,
|
||||
"pulls": true}
|
||||
"by": true, "limit": true, "clear": true, "proposal": true}
|
||||
var unknown []string
|
||||
for k := range args {
|
||||
if !known[k] {
|
||||
@@ -431,7 +466,7 @@ func callOf(args map[string]any) (Call, error) {
|
||||
}
|
||||
if len(unknown) > 0 {
|
||||
return Call{}, fmt.Errorf("config takes no %s; it takes action, key, value, module, node, why, by, limit, "+
|
||||
"clear, proposal, pull, head, declares and pulls",
|
||||
"clear and proposal",
|
||||
strings.Join(unknown, ", "))
|
||||
}
|
||||
str := func(k string) (string, error) {
|
||||
@@ -448,8 +483,7 @@ func callOf(args map[string]any) (Call, error) {
|
||||
var call Call
|
||||
var problems []error
|
||||
for k, into := range map[string]*string{"action": &call.Action, "key": &call.Key, "module": &call.Module,
|
||||
"node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal, "pull": &call.Pull,
|
||||
"head": &call.Head} {
|
||||
"node": &call.Node, "why": &call.Why, "by": &call.By, "proposal": &call.Proposal} {
|
||||
s, err := str(k)
|
||||
problems = append(problems, err)
|
||||
*into = s
|
||||
@@ -468,19 +502,6 @@ func callOf(args map[string]any) (Call, error) {
|
||||
}
|
||||
call.Clear = b
|
||||
}
|
||||
if v, ok := args["declares"]; ok && v != nil {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
problems = append(problems, errors.New("declares is an object: a module to its keys' declarations"))
|
||||
}
|
||||
call.Declares = m
|
||||
}
|
||||
if v, ok := args["pulls"]; ok && v != nil {
|
||||
raw, _ := json.Marshal(v)
|
||||
if err := json.Unmarshal(raw, &call.Pulls); err != nil {
|
||||
problems = append(problems, errors.New("pulls is a list of {pull, head}"))
|
||||
}
|
||||
}
|
||||
if v, ok := args["value"]; ok {
|
||||
// The value as given, a JSON value: a number for a number, never its text; the validation judges it.
|
||||
call.Value = v
|
||||
|
||||
@@ -50,10 +50,10 @@ func TestEveryStateAndActHasItsTool(t *testing.T) {
|
||||
}
|
||||
via := map[string]string{
|
||||
ValuesState: "config list", ResolvedState: "config list", PredefinedState: "config list",
|
||||
ProposalsState: "config proposals", PullsState: "config list", KeptState: "config list", HistoryLog: "config history",
|
||||
ProposalsState: "config proposals", HistoryLog: "config history",
|
||||
DeclaredState: DeclaredTool,
|
||||
AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list", CandidatesRead: ResyncTool,
|
||||
"a resolution": ResyncTool, "the indexer's candidates taken": ResyncTool,
|
||||
AssignmentsRead: "config list", WantsRead: "config list", IndexerPredefinedRead: "config list",
|
||||
"a resolution": ResyncTool,
|
||||
// design 32: each new state with the tool that reads it, each act with the tool that starts it.
|
||||
RenamesState: RenamesTool, RetiringState: RetiringTool, FindingsState: "config_renames and config_retiring",
|
||||
"a move refused": RenameAgainTool, "a window dropped": RenameCloseTool, "an ask again": RetiringTool,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
// What mesh-config reads of the delivery owners' state, through their tools and views families (novox/hq ADR 0615 §1,
|
||||
// §4 to §6, ADR 0596): never a bucket of theirs. For each of the four states it follows (the indexer's predefined and
|
||||
// config-candidates, the assigner's assignments and wants) it asks the owner's read tool for the whole
|
||||
// §4 to §6, ADR 0596): never a bucket of theirs. For each of the three states it follows (the indexer's predefined, the
|
||||
// assigner's assignments and wants) it asks the owner's read tool for the whole
|
||||
// state at its start, keeps the picture from the owner's `views.<state>` events, and asks again only on a gap. Started
|
||||
// while an owner is down it keeps asking, and past two minutes writes a finding naming the owner and the tool.
|
||||
//
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
// The tools of the owners that mesh-config asks, by the state they read.
|
||||
const (
|
||||
IndexerPredefTool = "indexer_read_predefined"
|
||||
IndexerCandidatesTool = "indexer_read_config_candidates"
|
||||
AssignerAssignmentsTool = "assigner_read_assignments"
|
||||
AssignerWantsTool = "assigner_read_wants"
|
||||
|
||||
@@ -40,7 +39,6 @@ const (
|
||||
// state and its tool.
|
||||
var followed = []struct{ Read, Owner, State, Tool string }{
|
||||
{IndexerPredefinedRead, indexerModule, catalog.StatePredefined, IndexerPredefTool},
|
||||
{CandidatesRead, indexerModule, catalog.StateConfigCandidates, IndexerCandidatesTool},
|
||||
{AssignmentsRead, assignerModule, "assignments", AssignerAssignmentsTool},
|
||||
{WantsRead, assignerModule, "wants", AssignerWantsTool},
|
||||
}
|
||||
@@ -189,7 +187,7 @@ func viewTools(c *Config, o func() *ownerViews) []stdio.Tool {
|
||||
return []stdio.Tool{
|
||||
{Name: FirstViewsTool,
|
||||
Description: "mesh-config's first views of the delivery owners' state: for each of the indexer's " +
|
||||
"predefined and config-candidates, the assigner's assignments and wants and the catalog half's registrations and " +
|
||||
"predefined, the assigner's assignments and wants and the catalog half's registrations and " +
|
||||
"each watched branch's revision, the tool it asks, the revision its " +
|
||||
"picture holds, when it asked and was answered, how many asks (and by hand), and whether its condition is raised.",
|
||||
Run: func(map[string]any) (any, error) {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// The module's own states are read from a copy each one's watch keeps, never key by key (novox/hq ADR 0580 §1, issue 1982):
|
||||
// a resolution reads every key of values, resolved, declared and the rest, and a get per key for each of them, once per
|
||||
// pass and once per delivery event, was about 4,000 requests to the bus every time. A state is watched on its first read, the
|
||||
// watch hands every current value over before the read is answered, and this process's own writes are taken into the copy
|
||||
// as they are made, so a read right after a write sees it.
|
||||
|
||||
// ownStates are the states this module keeps and reads through its store (its manifest's `state`, but the findings, which its
|
||||
// held writer alone reads and writes): the ones read from the copy.
|
||||
var ownStates = map[string]bool{
|
||||
ValuesState: true, ResolvedState: true, PredefinedState: true, ProposalsState: true, DeclaredState: true, RenamesState: true, RetiringState: true,
|
||||
}
|
||||
|
||||
// ownCopy is the copy of the module's own states over a bus: watch hands every current value of a state and then each change,
|
||||
// get reads one key, put and del write one.
|
||||
type ownCopy struct {
|
||||
copy *stdio.WatchedStates
|
||||
put func(state, key string, value any) (uint64, error)
|
||||
del func(state, key string) error
|
||||
// changed, when not nil, is told of each write or removal after it is in the copy: what waits on a deadline is judged again.
|
||||
changed func(state string)
|
||||
}
|
||||
|
||||
// newOwnCopy is the copy over this bundle's runtime.
|
||||
func newOwnCopy() *ownCopy {
|
||||
return &ownCopy{copy: stdio.NewWatchedStates(),
|
||||
put: func(state, key string, value any) (uint64, error) { return stdio.State(state).Put(key, value) },
|
||||
del: func(state, key string) error { return stdio.State(state).Delete(key) }}
|
||||
}
|
||||
|
||||
func (o *ownCopy) get(state, key string) (*Entry, error) {
|
||||
raw, ok, err := o.copy.Get(state, key)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
return &Entry{Key: key, Value: raw}, nil
|
||||
}
|
||||
|
||||
func (o *ownCopy) keys(state string) ([]string, error) { return o.copy.Keys(state) }
|
||||
|
||||
// write puts a key and takes it into the copy at the revision the bus answered.
|
||||
func (o *ownCopy) write(state, key string, value any) error {
|
||||
rev, err := o.put(state, key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = o.copy.Wrote(state, key, raw, rev)
|
||||
o.told(state)
|
||||
return err
|
||||
}
|
||||
|
||||
func (o *ownCopy) told(state string) {
|
||||
if o.changed != nil {
|
||||
o.changed(state)
|
||||
}
|
||||
}
|
||||
|
||||
// remove deletes a key and takes that into the copy.
|
||||
func (o *ownCopy) remove(state, key string) error {
|
||||
if err := o.del(state, key); err != nil {
|
||||
return err
|
||||
}
|
||||
err := o.copy.Removed(state, key)
|
||||
o.told(state)
|
||||
return err
|
||||
}
|
||||
@@ -98,11 +98,9 @@ type Proposal struct {
|
||||
HadValue bool `json:"had-value"`
|
||||
WasDigest string `json:"was-digest"`
|
||||
Trusted bool `json:"trusted,omitempty"`
|
||||
// Pull is the open pull request the value is held for, when it is (novox/hq ADR 0502): the value is then not set
|
||||
// on Approve but kept apart until the pull request merges, and DeclDigest is the digest of the pull request's
|
||||
// declaration of the key it was judged and shown against.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
DeclDigest string `json:"declaration,omitempty"`
|
||||
// Pull is read only: a proposal an earlier mesh-config kept for a pull request (ADR 0502, superseded by ADR 0681 §5).
|
||||
// It is never set on an Approve now, and judged not to be one this module makes (problem).
|
||||
Pull string `json:"pull,omitempty"`
|
||||
// Reaches is each module on each node whose value it moves, as it was judged when proposed.
|
||||
Reaches []string `json:"reaches"`
|
||||
// From is who proposed it, as the bus proved the caller; Why is theirs; At is when.
|
||||
@@ -175,9 +173,6 @@ func (p Proposal) Fingerprint() string {
|
||||
// about is what its ask is about: the value's place, so a newer proposal for it replaces an older one still open
|
||||
// (ADR 0277 §10, at a level, a key).
|
||||
func (p Proposal) about() string {
|
||||
if p.Pull != "" {
|
||||
return "config.held." + pullKey(p.Pull) + "." + p.Level.ValueKey(p.Key)
|
||||
}
|
||||
return "config." + p.Level.ValueKey(p.Key)
|
||||
}
|
||||
|
||||
@@ -188,9 +183,6 @@ func (p Proposal) Act(option string) occ.Act {
|
||||
"level": p.Level.Kind, "node": p.Level.Node, "module": p.Level.Module, "key": p.Key, "value": p.Digest,
|
||||
"was": p.WasDigest, "had-value": strconv.FormatBool(p.HadValue), "from": p.From,
|
||||
"at": p.At.UTC().Format(time.RFC3339Nano)}
|
||||
if p.Pull != "" {
|
||||
act["pull"], act["declaration"] = p.Pull, p.DeclDigest
|
||||
}
|
||||
return act
|
||||
}
|
||||
|
||||
@@ -270,16 +262,6 @@ func (p Proposal) summary() string {
|
||||
if p.Trusted {
|
||||
trusted = ", a trusted value,"
|
||||
}
|
||||
if p.Pull != "" {
|
||||
s := fmt.Sprintf("Holds %s for %s on %s for %s; approved, it takes effect when that pull request merges, "+
|
||||
"and is dropped if it closes unmerged. Nothing reaches a machine before the merge.", p.Key, p.Level.Module,
|
||||
p.Level.Node, p.Pull)
|
||||
if len([]rune(s)) > occ.SummaryLength {
|
||||
s = fmt.Sprintf("Holds %s for %s on %s for %s until it merges; dropped if it closes unmerged.", p.Key,
|
||||
p.Level.Module, p.Level.Node, p.Pull)
|
||||
}
|
||||
return s
|
||||
}
|
||||
reach := p.reaches()
|
||||
s := fmt.Sprintf("%s%s at %s; approved, it takes effect at once on every node it reaches: %s.", verb, trusted,
|
||||
p.Level, reach)
|
||||
@@ -305,9 +287,6 @@ func (p Proposal) headline() string {
|
||||
if p.Action == "clear" {
|
||||
verb = "Clear"
|
||||
}
|
||||
if p.Pull != "" {
|
||||
verb = "Hold"
|
||||
}
|
||||
where := "for the whole mesh"
|
||||
switch p.Level.Kind {
|
||||
case LevelNode:
|
||||
@@ -359,9 +338,6 @@ func (p Proposal) Ask() oc.Ask {
|
||||
a.WholeDetails = w
|
||||
}
|
||||
does := "the value is set at once, and every node it reaches takes it"
|
||||
if p.Pull != "" {
|
||||
does = "the value is held for " + p.Pull + ": it is set when that pull request merges, and dropped if it closes unmerged"
|
||||
}
|
||||
if p.Action == "clear" {
|
||||
does = "the value is cleared at once, and every node it reaches takes what is below it"
|
||||
}
|
||||
@@ -392,8 +368,8 @@ func (p Proposal) problem() string {
|
||||
return "its value is not the value its digest names: it changed after it was asked"
|
||||
case p.Action == "clear" && (len(p.Value) != 0 || p.Digest != valueDigest(nil, false)):
|
||||
return "a clear carries no value"
|
||||
case p.Pull != "" && (p.Action != "set" || p.Level.Kind != LevelModule || p.DeclDigest == ""):
|
||||
return "a value held for a pull request is a set of a module's own value, judged against its declaration"
|
||||
case p.Pull != "":
|
||||
return "it was held for a pull request, which the mesh knows no more: nothing is set on it"
|
||||
case valueDigest(p.Was, p.HadValue) != p.WasDigest:
|
||||
return "the value it was shown against is not the one its digest names: it changed after it was asked"
|
||||
}
|
||||
@@ -559,39 +535,16 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string,
|
||||
// **Judged as a set judges it, before anyone is asked** (ADR 0277 §4): no ask for a value the verb would refuse.
|
||||
var r rule
|
||||
var trusted bool
|
||||
var declDigest string
|
||||
vk := level.ValueKey(call.Key)
|
||||
was, had := s.Values[vk]
|
||||
if call.Pull != "" {
|
||||
// **A value held for the open pull request that introduces its key** (novox/hq ADR 0502): judged against what
|
||||
// the pull request declares at its head, as the controller told it, and asked of the operator whatever the
|
||||
// key's trust.
|
||||
if action != "set" {
|
||||
return fail("a clear is not held for a pull request: only a value to set is")
|
||||
}
|
||||
if _, _, _, err := parsePull(call.Pull); err != nil {
|
||||
return fail("%v", err)
|
||||
}
|
||||
call.Pull = strings.TrimSpace(call.Pull)
|
||||
var herr error
|
||||
r, _, declDigest, herr = heldRule(s, call, level)
|
||||
if herr != nil {
|
||||
return fail("%v", herr)
|
||||
}
|
||||
trusted = false
|
||||
k, kept := s.Kept[keptKey(call.Pull, level.Node, level.Module, call.Key)]
|
||||
was, had = Value{Value: heldValue(k)}, kept
|
||||
} else {
|
||||
var derr error
|
||||
r, trusted, derr = c.declared(s, level, call.Key)
|
||||
if derr != nil {
|
||||
return fail("%v", derr)
|
||||
}
|
||||
r, trusted, derr := c.declared(s, level, call.Key)
|
||||
if derr != nil {
|
||||
return fail("%v", derr)
|
||||
}
|
||||
now := c.now()
|
||||
prop := Proposal{ID: newProposalID(), Action: action, Level: level, Key: call.Key, HadValue: had,
|
||||
WasDigest: valueDigest(was.Value, had), Trusted: trusted, From: call.Caller, Why: why, At: now,
|
||||
State: ProposalWaiting, Digest: valueDigest(nil, false), Pull: call.Pull, DeclDigest: declDigest}
|
||||
State: ProposalWaiting, Digest: valueDigest(nil, false)}
|
||||
if had {
|
||||
prop.Was = compact(was.Value)
|
||||
}
|
||||
@@ -618,19 +571,15 @@ func (c *Config) propose(call Call, action string) (Proposal, oc.Ask, []string,
|
||||
if had && string(compact(was.Value)) == string(prop.Value) {
|
||||
return fail("%s at %s already holds exactly %s", call.Key, level, prop.Value)
|
||||
}
|
||||
if call.Pull == "" {
|
||||
after.Values[vk] = Value{Value: prop.Value}
|
||||
}
|
||||
after.Values[vk] = Value{Value: prop.Value}
|
||||
case "clear":
|
||||
if !had {
|
||||
return fail("nothing is set for %s at %s, so there is nothing to clear", call.Key, level)
|
||||
}
|
||||
delete(after.Values, vk)
|
||||
}
|
||||
if call.Pull == "" {
|
||||
for _, m := range diff(Resolve(s), Resolve(after)) {
|
||||
prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node)
|
||||
}
|
||||
for _, m := range diff(Resolve(s), Resolve(after)) {
|
||||
prop.Reaches = append(prop.Reaches, m.Module+" on "+m.Node)
|
||||
}
|
||||
if prop.Reaches == nil {
|
||||
prop.Reaches = []string{}
|
||||
@@ -830,14 +779,6 @@ func (c *Config) decide(p Proposal, w oc.Warrant) string {
|
||||
return fmt.Sprintf("proposal %s: approved by the operator, and not set: the decision could not be kept (%v); "+
|
||||
"it is read again on the next look", p.ID, err)
|
||||
}
|
||||
if p.Pull != "" {
|
||||
// **A held value is kept apart, never set on Approve** (ADR 0502 clause 3).
|
||||
said, err := c.keep(p)
|
||||
if err != nil {
|
||||
return keep(ProposalRefused, "approved by the operator, and refused on Approve, so nothing is held: "+err.Error())
|
||||
}
|
||||
return keep(ProposalApproved, "approved by the operator and held: "+said)
|
||||
}
|
||||
call := Call{Action: p.Action, Key: p.Key, Module: p.Level.Module, Node: p.Level.Node, Why: p.Why, Caller: p.From,
|
||||
approval: &approval{Proposal: p.ID, By: "the operator", Says: p.Approved, Was: p.Was, HadValue: p.HadValue,
|
||||
WasDigest: p.WasDigest}}
|
||||
@@ -910,19 +851,63 @@ func (c *Config) HeardEnd(w oc.Warrant) []string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
defer c.tellEnded(w.Ask, endedWord{Outcome: w.Outcome, Words: w.Words})
|
||||
// A retirement's ask ended with no choice is taken from the router's record the same way (retiring.go): the router's word
|
||||
// is the only thing that moves it, and no timer looks.
|
||||
said := c.decideRetirements()
|
||||
e, err := c.Store.Get(ProposalsState, w.Ask)
|
||||
if err != nil || e == nil {
|
||||
return nil
|
||||
return said
|
||||
}
|
||||
var p Proposal
|
||||
if json.Unmarshal(e.Value, &p) != nil || p.ID != w.Ask || p.State != ProposalWaiting {
|
||||
return nil
|
||||
return said
|
||||
}
|
||||
why := "the ask " + string(w.Outcome)
|
||||
if w.Words != "" {
|
||||
why += " (" + w.Words + ")"
|
||||
}
|
||||
return []string{c.end(p, why+"; nothing was set")}
|
||||
return append(said, c.end(p, why+"; nothing was set"))
|
||||
}
|
||||
|
||||
// NextDeadline is when a waiting proposal or retirement is next judged for want of an answer: a proposal with no record
|
||||
// at the router ends an hour after its ask lapses (Decide), and a retirement left unanswered is asked once more after
|
||||
// HeldFinding, and an open rename window's held finding comes after HeldFinding too (lead answer A-490l). The earliest of
|
||||
// them; false when nothing waits on a time.
|
||||
func (c *Config) NextDeadline() (time.Time, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var at time.Time
|
||||
earlier := func(t time.Time) {
|
||||
if at.IsZero() || t.Before(at) {
|
||||
at = t
|
||||
}
|
||||
}
|
||||
if all, err := c.proposals(); err == nil {
|
||||
for _, p := range all {
|
||||
if p.State == ProposalWaiting {
|
||||
earlier(p.At.Add(ProposalLasts + time.Hour))
|
||||
}
|
||||
}
|
||||
}
|
||||
if held, err := c.readRetiring(); err == nil {
|
||||
for _, e := range held {
|
||||
if e.State == RetireUnanswered && !e.AskedAgain {
|
||||
earlier(e.Asked.Add(HeldFinding))
|
||||
}
|
||||
}
|
||||
}
|
||||
// An open rename window reaches its held finding after HeldFinding (renames.go): its deadline, while it lies ahead.
|
||||
if renames, err := c.readRenames(); err == nil {
|
||||
now := c.now()
|
||||
for _, r := range renames {
|
||||
for _, node := range r.Open() {
|
||||
if d := r.Nodes[node].Since.Add(HeldFinding); d.After(now) {
|
||||
earlier(d)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return at, !at.IsZero()
|
||||
}
|
||||
|
||||
// ---- the list ----------------------------------------------------------------------------------------------
|
||||
@@ -940,8 +925,6 @@ type ProposalRow struct {
|
||||
Expires time.Time `json:"expires"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
Approved string `json:"approved,omitempty"`
|
||||
// Pull is the pull request a value is held for, when it is.
|
||||
Pull string `json:"pull,omitempty"`
|
||||
// What one proposal read whole says besides.
|
||||
Value json.RawMessage `json:"value,omitempty"`
|
||||
Was json.RawMessage `json:"was,omitempty"`
|
||||
@@ -970,8 +953,7 @@ func (c *Config) Proposals(call Call) ([]ProposalRow, error) {
|
||||
continue
|
||||
}
|
||||
row := ProposalRow{ID: p.ID, Action: p.Action, Level: p.Level, Key: p.Key, State: p.State, Outcome: p.Outcome,
|
||||
From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved,
|
||||
Pull: p.Pull}
|
||||
From: p.From, At: p.At, Expires: p.At.Add(ProposalLasts), Fingerprint: p.Fingerprint(), Approved: p.Approved}
|
||||
if p.State == ProposalWaiting && c.now().After(row.Expires) {
|
||||
row.State = "expired, not yet heard"
|
||||
}
|
||||
|
||||
@@ -590,3 +590,42 @@ func TestAnOwnersListOfNamesWithDigitsIsProposed(t *testing.T) {
|
||||
t.Fatalf("the owners were not put to the operator: %+v, asked on %v", a, r.subjects)
|
||||
}
|
||||
}
|
||||
|
||||
// **An approved proposal records a hand act that names no pull request** (ADR 0681 clause 1: nothing it records as a hand act is a
|
||||
// held value).
|
||||
func TestAnApprovedProposalsHandActNamesNoPullRequest(t *testing.T) {
|
||||
c, m, r := aProposingMesh(t)
|
||||
a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"})
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
m.handActs = nil
|
||||
if said, err := c.Decide(); err != nil || len(said) == 0 {
|
||||
t.Fatalf("decide: %v, %v", said, err)
|
||||
}
|
||||
if len(m.handActs) == 0 {
|
||||
t.Fatal("the approval recorded no hand act")
|
||||
}
|
||||
for _, act := range m.handActs {
|
||||
if strings.Contains(strings.ToLower(act), "pull") || strings.Contains(act, "#") {
|
||||
t.Errorf("a hand act names a pull request: %q", act)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A stored proposal that names a pull request is refused on its approval, saying why, and nothing is set (lead answer A-490l).
|
||||
func TestAStoredProposalNamingAPullRequestIsRefusedOnApproval(t *testing.T) {
|
||||
c, m, r := aProposingMesh(t)
|
||||
a := propose(t, c, Call{Key: "font-size", Value: 12.0, Node: "g14"})
|
||||
p := storedProposal(t, m, a.Proposal)
|
||||
p.Pull = "novox/mesh-catalog#7"
|
||||
if err := m.Put(ProposalsState, p.ID, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if got := storedProposal(t, m, a.Proposal); got.State == ProposalApproved || len(said) == 0 || !strings.Contains(strings.Join(said, " "), "pull request") {
|
||||
t.Errorf("said %v, proposal %+v", said, got)
|
||||
}
|
||||
if _, ok := storedValue(t, m, "node.g14.font-size"); ok {
|
||||
t.Error("a value was set on a proposal that names a pull request")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// toolForge.Pull reads the seat git's answer of the verb pull (design 47 G §654), bare or in the wrappings the runtime puts
|
||||
// around an answer; pullEventOf reads the seat's pull-merged and pull-closed bodies.
|
||||
func TestToolForgePullReadsTheSeatGitAnswersAndEventsAreRead(t *testing.T) {
|
||||
var asked struct {
|
||||
key string
|
||||
body map[string]any
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
answer string
|
||||
open, merged bool
|
||||
head string
|
||||
}{
|
||||
{`{"pull":{"repository":"novox/mesh-catalog","number":435,"state":"merged","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2"}}`, false, true, "7b5f7f84acb73aaf8189200548ff69fd9aac04b2"},
|
||||
{`{"pull":{"repository":"novox/mesh-catalog","number":434,"state":"open","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, true, false, "c948f3a61c74257e6e3147effb396d9d6434f491"},
|
||||
{`{"pull":{"state":"closed","head":"c948f3a61c74257e6e3147effb396d9d6434f491"}}`, false, false, "c948f3a61c74257e6e3147effb396d9d6434f491"},
|
||||
} {
|
||||
raw := json.RawMessage(tc.answer)
|
||||
content, _ := json.Marshal(map[string]any{"content": []map[string]any{{"type": "text", "text": tc.answer}}})
|
||||
ok, _ := json.Marshal(map[string]any{"ok": true, "output": "", "answer": raw})
|
||||
for name, answer := range map[string]json.RawMessage{"bare": raw, "content list": content, "ok answer": ok} {
|
||||
f := toolForge{ask: func(key string, body any) (json.RawMessage, error) {
|
||||
asked.key, asked.body = key, body.(map[string]any)
|
||||
return answer, nil
|
||||
}}
|
||||
open, merged, head, err := f.Pull("novox", "mesh-catalog", 434)
|
||||
if err != nil || open != tc.open || merged != tc.merged || head != tc.head {
|
||||
t.Errorf("%s (%s): open %v merged %v head %q, err %v", tc.answer, name, open, merged, head, err)
|
||||
}
|
||||
if asked.key != "seat:git.pull" || asked.body["repository"] != "novox/mesh-catalog" || asked.body["number"] != 434 {
|
||||
t.Errorf("asked %+v", asked)
|
||||
}
|
||||
}
|
||||
}
|
||||
failed := toolForge{ask: func(string, any) (json.RawMessage, error) {
|
||||
return json.RawMessage(`{"error":"no such pull request","error-kind":"not-found"}`), nil
|
||||
}}
|
||||
if _, _, _, err := failed.Pull("novox", "mesh-catalog", 9); err == nil {
|
||||
t.Error("an error answer was read as a pull request")
|
||||
}
|
||||
p, ok := pullEventOf([]byte(`{"repository":"novox/mesh-catalog","number":435,"branch":"fix/a","head":"7b5f7f84acb73aaf8189200548ff69fd9aac04b2","base":"main"}`))
|
||||
if !ok || p.Owner != "novox" || p.Repo != "mesh-catalog" || p.Number != 435 || p.HeadSHA != "7b5f7f84acb73aaf8189200548ff69fd9aac04b2" {
|
||||
t.Errorf("%+v %v", p, ok)
|
||||
}
|
||||
if _, ok := pullEventOf([]byte(`{"repository":"norepo","number":1}`)); ok {
|
||||
t.Error("a repository with no owner was read")
|
||||
}
|
||||
}
|
||||
@@ -1,644 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient"
|
||||
)
|
||||
|
||||
// A config value held for the open pull request that introduces its key (novox/hq ADR 0502, issue 1826).
|
||||
//
|
||||
// A key a pull request adds is declared by no registered manifest until it merges, so a value for it, which the
|
||||
// pull request's composed check needs, was refused. The controller reads each pull request's manifests from the
|
||||
// forge at its head and tells this module the config declarations the head adds or changes (PullDeclares); mesh-config
|
||||
// reads no manifest itself (ADR 0495 §1). A proposal that names the pull request is judged against those, asked of the
|
||||
// operator whatever the key's trust, and on Approve kept apart (KeptState) from the values it resolves. The
|
||||
// controller asks for the approved held values of a check's pull requests (Held) and hands them to the composed
|
||||
// check. At the merge the value is written as the module's own value on its node (PullMerged), where it resolves
|
||||
// once the trunk declares the key; a pull request closed unmerged drops it (PullClosed). Nothing reaches a machine
|
||||
// before the merge.
|
||||
|
||||
// The states of held values, and the actions their changes carry in the history and in `config-changed`.
|
||||
const (
|
||||
PullsState = "pulls"
|
||||
KeptState = "kept"
|
||||
|
||||
ActionHold = "hold"
|
||||
ActionDrop = "drop"
|
||||
)
|
||||
|
||||
// pullName is a pull request as it is named: owner/repository#number.
|
||||
var pullName = regexp.MustCompile(`^([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)#([0-9]+)$`)
|
||||
|
||||
func parsePull(s string) (owner, repo string, number int, err error) {
|
||||
m := pullName.FindStringSubmatch(strings.TrimSpace(s))
|
||||
if m == nil {
|
||||
return "", "", 0, fmt.Errorf("%q is not a pull request: name it owner/repository#number", s)
|
||||
}
|
||||
n, _ := strconv.Atoi(m[3])
|
||||
if n <= 0 {
|
||||
return "", "", 0, fmt.Errorf("%q is not a pull request: its number is above zero", s)
|
||||
}
|
||||
return m[1], m[2], n, nil
|
||||
}
|
||||
|
||||
// pullKey is a pull request's key in the states.
|
||||
func pullKey(pull string) string {
|
||||
return strings.NewReplacer("/", ".", "#", ".").Replace(strings.TrimSpace(pull))
|
||||
}
|
||||
|
||||
// PullDecl is what the controller told: the config declarations a pull request's head adds or changes, by module.
|
||||
type PullDecl struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Declares map[string]map[string]Declaration `json:"declares"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// KeptValue is an approved held value, kept apart from the values mesh-config resolves, in the layout of the seat's state
|
||||
// `kept`, which the checker and the assigner's composition check read (novox/hq ADR 0565 §5, ADR 0634 §4).
|
||||
type KeptValue = meshconfigclient.Kept
|
||||
|
||||
// keptKey is a held value's key in `kept`: the pull request's key form (pullKey), the node, the module and the config
|
||||
// key, joined by dots.
|
||||
func keptKey(pull, node, module, key string) string {
|
||||
k, _ := meshconfigclient.HeldValueKey(pull, node, module, key) // a held value of no pull request has no key, and is found by none
|
||||
return k
|
||||
}
|
||||
|
||||
// heldValue is the value held, as the operator approved it, in its JSON form.
|
||||
func heldValue(k KeptValue) json.RawMessage {
|
||||
raw, _ := meshconfigclient.ValueOf(k) // a value that was read from JSON writes as JSON
|
||||
return raw
|
||||
}
|
||||
|
||||
func heldAt(t time.Time) *time.Time { return &t }
|
||||
|
||||
func keptStateKey(k KeptValue) string { return keptKey(k.Pull, k.Node, k.Module, k.Key) }
|
||||
|
||||
func keptLevel(k KeptValue) Level { return Level{Kind: LevelModule, Node: k.Node, Module: k.Module} }
|
||||
|
||||
// declDigest is the digest of what a declaration holds a value to: a held value stays only while it is the same.
|
||||
func declDigest(d Declaration) string {
|
||||
d.Description, d.Why, d.Kind, d.Runtime = "", "", "", false
|
||||
if !d.HasDefault() {
|
||||
d.Default = nil
|
||||
} else {
|
||||
d.Default = compact(d.Default)
|
||||
}
|
||||
raw, _ := json.Marshal(d)
|
||||
sum := sha256.Sum256(raw)
|
||||
return "sha256:" + hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// PullForge is the forge as this module asks it of a pull request: its state, and whether it merged.
|
||||
type PullForge interface {
|
||||
Pull(owner, repo string, number int) (open, merged bool, head string, err error)
|
||||
}
|
||||
|
||||
// pulls reads what is kept for pull requests, and the held values.
|
||||
func (c *Config) readPulls(s *Sources) error {
|
||||
s.Pulls, s.Kept = map[string]PullDecl{}, map[string]KeptValue{}
|
||||
if err := c.each(PullsState, func(key string, raw []byte) error {
|
||||
var p PullDecl
|
||||
if err := json.Unmarshal(raw, &p); err != nil || p.Pull == "" {
|
||||
return nil // not one this module wrote
|
||||
}
|
||||
s.Pulls[pullKey(p.Pull)] = p
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("the pull requests' declarations cannot be read: %w", err)
|
||||
}
|
||||
if err := c.each(KeptState, func(key string, raw []byte) error {
|
||||
k, err := meshconfigclient.DecodeKept(key, raw)
|
||||
if err != nil {
|
||||
return nil // not a held value of the seat's layout under its own key
|
||||
}
|
||||
s.Kept[key] = k
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("the held values cannot be read: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- pull-declares ---------------------------------------------------------------------------------------
|
||||
|
||||
// PullDeclaresAnswer is what pull-declares answers.
|
||||
type PullDeclaresAnswer struct {
|
||||
Said string `json:"said"`
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Modules []string `json:"modules"`
|
||||
Dropped []string `json:"dropped,omitempty"`
|
||||
Unsaid []string `json:"unsaid,omitempty"`
|
||||
}
|
||||
|
||||
// PullDeclares is gone: a pull request's config declarations are read from the indexer's config-candidates
|
||||
// (takeCandidates), and the controller that told them is not placed (novox/hq ADR 0565 §4, ADR 0590).
|
||||
func (c *Config) PullDeclares(Call) (*PullDeclaresAnswer, error) {
|
||||
return nil, errors.New("pull-declares is gone: mesh-config reads a pull request's config declarations from the " +
|
||||
"indexer's config-candidates (novox/hq ADR 0565 §4, ADR 0590)")
|
||||
}
|
||||
|
||||
// declarePull keeps what a pull request's head declares and judges again the values held for it, from the indexer's
|
||||
// config candidates. The caller holds the lock.
|
||||
func (c *Config) declarePull(pull, head string, declares map[string]map[string]Declaration) (*PullDeclaresAnswer, error) {
|
||||
if err := c.Store.Put(PullsState, pullKey(pull), PullDecl{Pull: pull, Head: head, Declares: declares,
|
||||
At: c.now()}); err != nil {
|
||||
return nil, fmt.Errorf("the declarations were not kept: %v", err)
|
||||
}
|
||||
a := &PullDeclaresAnswer{Pull: pull, Head: head, Modules: sortedKeys(declares)}
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if pullKey(kv.Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
why := ""
|
||||
d, declared := declares[kv.Module][kv.Key]
|
||||
switch {
|
||||
case !declared:
|
||||
why = "the head no longer adds or changes its declaration"
|
||||
case declDigest(d) != kv.Declaration:
|
||||
why = "the head declares the key otherwise than the value was approved against"
|
||||
default:
|
||||
var v any
|
||||
_ = json.Unmarshal(heldValue(kv), &v)
|
||||
if p := d.rule().check(v); len(p) > 0 {
|
||||
why = "the value no longer passes: " + strings.Join(p, "; ")
|
||||
}
|
||||
}
|
||||
if why != "" {
|
||||
a.Unsaid = append(a.Unsaid, c.dropKept(kv, "the new head "+short(head)+": "+why)...)
|
||||
a.Dropped = append(a.Dropped, fmt.Sprintf("%s for %s on %s: %s", kv.Key, kv.Module, kv.Node, why))
|
||||
continue
|
||||
}
|
||||
if kv.Head != head {
|
||||
kv.Head = head
|
||||
if err := c.Store.Put(KeptState, k, kv); err != nil {
|
||||
a.Unsaid = append(a.Unsaid, "the held value "+k+" was not moved to the new head: "+err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
a.Said = fmt.Sprintf("keeps what %s declares at %s for %d module(s)", pull, short(head), len(declares))
|
||||
if len(a.Dropped) > 0 {
|
||||
a.Said += fmt.Sprintf("; %d held value(s) dropped", len(a.Dropped))
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// heldChange records a hold, a drop or a merge's set of a held value in the history, the hand-act log and
|
||||
// config-changed; what could not be done is answered, not dropped.
|
||||
func (c *Config) heldChange(kv KeptValue, action, why, approved string) []string {
|
||||
level := keptLevel(kv)
|
||||
ch := Change{Action: action, Level: &level, Key: kv.Key, Value: heldValue(kv), By: kv.By, Via: kv.Via, Why: why,
|
||||
Approved: approved, Pull: kv.Pull, Head: kv.Head, At: c.now(), Moved: []Moved{}}
|
||||
unsaid := c.record(level.ValueKey(kv.Key), ch)
|
||||
cause := "a value held for " + kv.Pull
|
||||
what := fmt.Sprintf("config %s %s of %s for %s, held for %s", action, kv.Key, kv.Module, kv.Node, kv.Pull)
|
||||
if action != ActionDrop {
|
||||
what += " (" + string(heldValue(kv)) + ")"
|
||||
}
|
||||
if err := c.Store.HandAct(what, why, cause); err != nil {
|
||||
unsaid = append(unsaid, "the hand-act log does not hold it: "+err.Error())
|
||||
}
|
||||
return unsaid
|
||||
}
|
||||
|
||||
// dropKept removes a held value, saying why.
|
||||
func (c *Config) dropKept(kv KeptValue, why string) []string {
|
||||
if err := c.Store.Delete(KeptState, keptStateKey(kv)); err != nil {
|
||||
return []string{"the held value " + keptStateKey(kv) + " was not dropped: " + err.Error()}
|
||||
}
|
||||
c.logf("[mesh-config] dropped %s of %s on %s held for %s: %s", kv.Key, kv.Module, kv.Node, kv.Pull, why)
|
||||
return c.heldChange(kv, ActionDrop, "dropped, nothing was set: "+why, "")
|
||||
}
|
||||
|
||||
// ---- held ------------------------------------------------------------------------------------------------
|
||||
|
||||
// HeldAnswer is what held answers: the approved held values of the pull requests asked, at the heads asked.
|
||||
type HeldAnswer struct {
|
||||
Held []HeldValue `json:"held"`
|
||||
Skipped []HeldSkipped `json:"skipped"`
|
||||
}
|
||||
|
||||
// HeldValue is one approved held value as it is handed to a composed check and listed.
|
||||
type HeldValue struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
Node string `json:"node"`
|
||||
Module string `json:"module"`
|
||||
Key string `json:"key"`
|
||||
Value json.RawMessage `json:"value"`
|
||||
Approved string `json:"approved"`
|
||||
By string `json:"by,omitempty"`
|
||||
Why string `json:"why,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
// HeldSkipped is a pull request asked for whose held values are not answered, and why.
|
||||
type HeldSkipped struct {
|
||||
Pull string `json:"pull"`
|
||||
Why string `json:"why"`
|
||||
}
|
||||
|
||||
func heldValueOf(k KeptValue) HeldValue {
|
||||
return HeldValue{Pull: k.Pull, Head: k.Head, Node: k.Node, Module: k.Module, Key: k.Key, Value: compact(heldValue(k)),
|
||||
Approved: k.Approved, By: k.By, Why: k.Why, At: timeOf(k.At)}
|
||||
}
|
||||
|
||||
// Held answers the approved held values for the pull requests asked, at the heads asked, to the controller alone: a
|
||||
// pull request at another head than the one kept is skipped and said (ADR 0502 clause 4).
|
||||
func (c *Config) Held(call Call) (*HeldAnswer, error) {
|
||||
if call.Caller != ControllerCaller {
|
||||
return nil, fmt.Errorf("held is the controller's: it hands the held values to a composed check; %s is not "+
|
||||
"the controller", call.Caller)
|
||||
}
|
||||
asked := map[string]string{}
|
||||
var order []string
|
||||
for _, p := range call.Pulls {
|
||||
if _, _, _, err := parsePull(p.Pull); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(p.Head) == "" {
|
||||
return nil, fmt.Errorf("name the head of %s", p.Pull)
|
||||
}
|
||||
if _, dup := asked[pullKey(p.Pull)]; !dup {
|
||||
order = append(order, strings.TrimSpace(p.Pull))
|
||||
}
|
||||
asked[pullKey(p.Pull)] = strings.TrimSpace(p.Head)
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := &HeldAnswer{Held: []HeldValue{}, Skipped: []HeldSkipped{}}
|
||||
for _, p := range order {
|
||||
head := asked[pullKey(p)]
|
||||
var mine []KeptValue
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
if pullKey(s.Kept[k].Pull) == pullKey(p) {
|
||||
mine = append(mine, s.Kept[k])
|
||||
}
|
||||
}
|
||||
for _, k := range mine {
|
||||
if k.Head != head {
|
||||
out.Skipped = append(out.Skipped, HeldSkipped{Pull: p, Why: fmt.Sprintf("%s for %s on %s is held "+
|
||||
"at head %s, not %s", k.Key, k.Module, k.Node, short(k.Head), short(head))})
|
||||
continue
|
||||
}
|
||||
out.Held = append(out.Held, heldValueOf(k))
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// HeldListed is the held values a listing shows, narrowed by module, node and key.
|
||||
func heldListed(s Sources, call Call) []HeldValue {
|
||||
out := []HeldValue{}
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if (call.Module == "" || kv.Module == call.Module) && (call.Node == "" || kv.Node == call.Node) &&
|
||||
(call.Key == "" || kv.Key == call.Key) {
|
||||
out = append(out, heldValueOf(kv))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ---- the proposal's rule -------------------------------------------------------------------------------
|
||||
|
||||
// heldRule is the rule a value held for a pull request is judged by: the pull request's declaration of the key for
|
||||
// the module at its head, and the module's assignment to the node. Refused, saying what the pull request does
|
||||
// declare, when it declares none.
|
||||
func heldRule(s Sources, call Call, level Level) (rule, PullDecl, string, error) {
|
||||
if level.Kind != LevelModule {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("a value held for a pull request is a module's own value on one "+
|
||||
"node, and %s is not one: a predefined key a pull request adds is set after its merge (novox/hq ADR 0502)",
|
||||
level)
|
||||
}
|
||||
pd, ok := s.Pulls[pullKey(call.Pull)]
|
||||
if !ok {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("mesh-config holds no declaration for %s: the controller tells it "+
|
||||
"what a pull request declares when its head is announced, and none has been", strings.TrimSpace(call.Pull))
|
||||
}
|
||||
d, ok := pd.Declares[level.Module][call.Key]
|
||||
if !ok {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s does not add or change a value %q for %s at %s; it declares "+
|
||||
"for %s: %s", pd.Pull, call.Key, level.Module, short(pd.Head), level.Module, joinKeys(pd.Declares[level.Module]))
|
||||
}
|
||||
if _, reg := s.Declared[level.Module]; !reg {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s is not a registered module, so a value cannot be held for it", level.Module)
|
||||
}
|
||||
if !assigned(s, level.Node, level.Module) {
|
||||
return rule{}, PullDecl{}, "", fmt.Errorf("%s is not placed on %s, and a module's own value is set for a node "+
|
||||
"it is placed on (novox/hq ADR 0348 §3); it is placed on: %s", level.Module, level.Node,
|
||||
strings.Join(nodesOf(s, level.Module), ", "))
|
||||
}
|
||||
return d.rule(), pd, declDigest(d), nil
|
||||
}
|
||||
|
||||
// keep holds an approved proposal's value apart. The caller holds the lock.
|
||||
func (c *Config) keep(p Proposal) (string, error) {
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return "", err
|
||||
}
|
||||
pd, ok := s.Pulls[pullKey(p.Pull)]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("mesh-config holds no declaration for %s any more", p.Pull)
|
||||
}
|
||||
d, ok := pd.Declares[p.Level.Module][p.Key]
|
||||
if !ok || declDigest(d) != p.DeclDigest {
|
||||
return "", fmt.Errorf("%s declares %s otherwise than the operator was shown, or no longer declares it: "+
|
||||
"propose it again", p.Pull, p.Key)
|
||||
}
|
||||
was, had := s.Kept[keptKey(p.Pull, p.Level.Node, p.Level.Module, p.Key)]
|
||||
if had != p.HadValue || valueDigest(heldValue(was), had) != p.WasDigest {
|
||||
return "", fmt.Errorf("the value held for %s changed since the operator was shown the change: propose it "+
|
||||
"again", p.Pull)
|
||||
}
|
||||
kv := KeptValue{Pull: p.Pull, Head: pd.Head, Declaration: p.DeclDigest, Node: p.Level.Node, Module: p.Level.Module,
|
||||
Key: p.Key, Value: p.Value, By: "the operator", Via: p.From, Why: p.Why, Approved: p.Approved, Proposal: p.ID,
|
||||
At: heldAt(c.now())}
|
||||
if err := c.Store.Put(KeptState, keptStateKey(kv), kv); err != nil {
|
||||
return "", fmt.Errorf("the value was not kept: %v", err)
|
||||
}
|
||||
unsaid := c.heldChange(kv, ActionHold, "held for "+p.Pull+" until it merges: "+p.Why, p.Approved)
|
||||
said := fmt.Sprintf("%s of %s on %s is held at %s for %s until it merges", p.Key, p.Level.Module, p.Level.Node,
|
||||
short(pd.Head), p.Pull)
|
||||
if len(unsaid) > 0 {
|
||||
said += " (" + strings.Join(unsaid, "; ") + ")"
|
||||
}
|
||||
return said, nil
|
||||
}
|
||||
|
||||
// ---- the merge and the close ---------------------------------------------------------------------------
|
||||
|
||||
// PullMerged writes each value held for a pull request as the module's own value on its node, with the approval kept
|
||||
// beside it; it resolves once the trunk declares the key. It answers a line for each.
|
||||
func (c *Config) PullMerged(pull, head string) []string {
|
||||
return c.settlePull(pull, true, head)
|
||||
}
|
||||
|
||||
// PullClosed drops the values held for a pull request closed unmerged, and says that nothing was set.
|
||||
func (c *Config) PullClosed(pull string) []string {
|
||||
return c.settlePull(pull, false, "")
|
||||
}
|
||||
|
||||
func (c *Config) settlePull(pull string, merged bool, head string) []string {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
var said []string
|
||||
wrote := false
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
kv := s.Kept[k]
|
||||
if pullKey(kv.Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
if !merged {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which closed unmerged: dropped, "+
|
||||
"nothing was set", kv.Key, kv.Module, kv.Node, kv.Pull))
|
||||
said = append(said, c.dropKept(kv, kv.Pull+" closed unmerged")...)
|
||||
continue
|
||||
}
|
||||
// **Only a value held at the merged head, whose key that head declares as it was approved** (ADR 0502 clause 7).
|
||||
why := ""
|
||||
pd, known := s.Pulls[pullKey(pull)]
|
||||
d, declared := pd.Declares[kv.Module][kv.Key]
|
||||
switch {
|
||||
case head == "":
|
||||
why = "the merged head is not known, so the value cannot be held to it"
|
||||
case !strings.EqualFold(head, kv.Head):
|
||||
why = fmt.Sprintf("it is held at head %s and the pull request merged at %s", short(kv.Head), short(head))
|
||||
case !known || !declared || declDigest(d) != kv.Declaration:
|
||||
why = "the merged head does not declare the key as the operator was shown"
|
||||
}
|
||||
if why != "" {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s was held for %s, which merged: dropped, nothing was set (%s)",
|
||||
kv.Key, kv.Module, kv.Node, kv.Pull, why))
|
||||
said = append(said, c.dropKept(kv, kv.Pull+" merged, but "+why)...)
|
||||
continue
|
||||
}
|
||||
vk := keptLevel(kv).ValueKey(kv.Key)
|
||||
v := Value{Value: compact(heldValue(kv)), By: kv.By, Via: kv.Via, Why: kv.Why + " (held for " + kv.Pull +
|
||||
", merged)", Approved: kv.Approved, At: c.now()}
|
||||
if err := c.Store.Put(ValuesState, vk, v); err != nil {
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s held for %s is not written yet: %v; it is tried "+
|
||||
"again", kv.Key, kv.Module, kv.Node, kv.Pull, err))
|
||||
continue
|
||||
}
|
||||
if err := c.Store.Delete(KeptState, k); err != nil {
|
||||
said = append(said, "the held value "+k+" is written but not removed: "+err.Error())
|
||||
}
|
||||
wrote = true
|
||||
said = append(said, c.heldChange(kv, "set", kv.Pull+" merged: the held value is the module's own value now",
|
||||
kv.Approved)...)
|
||||
said = append(said, fmt.Sprintf("%s of %s on %s held for %s is set: %s", kv.Key, kv.Module, kv.Node,
|
||||
kv.Pull, kv.Value))
|
||||
}
|
||||
if wrote {
|
||||
if s2, err := c.sources(); err == nil {
|
||||
if _, err := c.writeResolved(s2); err != nil {
|
||||
said = append(said, "the resolved values are not all written yet ("+err.Error()+"); the next "+
|
||||
"resolution writes them")
|
||||
}
|
||||
}
|
||||
}
|
||||
left := 0
|
||||
for _, k := range sortedKeys(s.Kept) {
|
||||
if pullKey(s.Kept[k].Pull) != pullKey(pull) {
|
||||
continue
|
||||
}
|
||||
if e, err := c.Store.Get(KeptState, k); err != nil || e != nil {
|
||||
left++
|
||||
}
|
||||
}
|
||||
if left == 0 {
|
||||
if err := c.Store.Delete(PullsState, pullKey(pull)); err != nil {
|
||||
said = append(said, "the declarations of "+pull+" were not removed: "+err.Error())
|
||||
}
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// PullEvent is a pull request merged or closed, as far as this module reads it: the seat `git`'s `pull-merged` or `pull-closed`
|
||||
// (design 47 G §654), read by pullEventOf.
|
||||
type PullEvent struct {
|
||||
Owner string
|
||||
Repo string
|
||||
Number int
|
||||
Merged bool
|
||||
// HeadSHA is the head the pull request merged at.
|
||||
HeadSHA string
|
||||
}
|
||||
|
||||
// pullEventOf reads the seat `git`'s `pull-merged` or `pull-closed` body: the repository `<owner>/<name>`, the number and the head.
|
||||
func pullEventOf(body []byte) (PullEvent, bool) {
|
||||
var e struct {
|
||||
Repository string `json:"repository"`
|
||||
Number int `json:"number"`
|
||||
Head string `json:"head"`
|
||||
}
|
||||
if json.Unmarshal(body, &e) != nil {
|
||||
return PullEvent{}, false
|
||||
}
|
||||
owner, repo, ok := strings.Cut(e.Repository, "/")
|
||||
return PullEvent{Owner: owner, Repo: repo, Number: e.Number, HeadSHA: e.Head}, ok
|
||||
}
|
||||
|
||||
func (p PullEvent) name() string { return fmt.Sprintf("%s/%s#%d", p.Owner, p.Repo, p.Number) }
|
||||
|
||||
// HeardPull takes the forge's word that a pull request merged or closed. A close is asked of the forge when it can
|
||||
// be, because a merged pull request may be announced as closed too.
|
||||
func (c *Config) HeardPull(p PullEvent, merged bool) []string {
|
||||
if p.Owner == "" || p.Repo == "" || p.Number <= 0 {
|
||||
return nil
|
||||
}
|
||||
head := p.HeadSHA
|
||||
if !merged && !p.Merged && c.Forge != nil {
|
||||
// A close is told from a merge by the forge; when it cannot be asked nothing is dropped, and the five-minute
|
||||
// look finds it.
|
||||
open, m, h, err := c.Forge.Pull(p.Owner, p.Repo, p.Number)
|
||||
if err != nil {
|
||||
c.logf("[mesh-config] %s closed, and the forge does not say whether it merged (%v); the next look decides", p.name(), err)
|
||||
return nil
|
||||
}
|
||||
if open {
|
||||
return nil
|
||||
}
|
||||
merged = m
|
||||
if head == "" {
|
||||
head = h
|
||||
}
|
||||
}
|
||||
return c.settlePull(p.name(), merged || p.Merged, head)
|
||||
}
|
||||
|
||||
// SweepPulls looks at the forge for every pull request it keeps something for, so a merge or a close it did not hear
|
||||
// is found: run with each five-minute resolution.
|
||||
func (c *Config) SweepPulls() []string {
|
||||
if c.Forge == nil {
|
||||
return nil
|
||||
}
|
||||
c.mu.Lock()
|
||||
var s Sources
|
||||
err := c.readPulls(&s)
|
||||
c.mu.Unlock()
|
||||
if err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
seen := map[string]string{}
|
||||
for _, p := range s.Pulls {
|
||||
seen[pullKey(p.Pull)] = p.Pull
|
||||
}
|
||||
for _, k := range s.Kept {
|
||||
seen[pullKey(k.Pull)] = k.Pull
|
||||
}
|
||||
var said []string
|
||||
for _, key := range sortedKeys(seen) {
|
||||
pull := seen[key]
|
||||
owner, repo, n, err := parsePull(pull)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
open, merged, head, err := c.Forge.Pull(owner, repo, n)
|
||||
if err != nil {
|
||||
said = append(said, fmt.Sprintf("the forge does not say whether %s is open: %v", pull, err))
|
||||
continue
|
||||
}
|
||||
if !open {
|
||||
said = append(said, c.settlePull(pull, merged, head)...)
|
||||
}
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// PullAt is a pull request at a head, as held asks about it.
|
||||
type PullAt struct {
|
||||
Pull string `json:"pull"`
|
||||
Head string `json:"head"`
|
||||
}
|
||||
|
||||
// toolForge asks the forge's holder through its tools.
|
||||
type toolForge struct {
|
||||
ask func(key string, body any) (json.RawMessage, error)
|
||||
}
|
||||
|
||||
// Pull asks the seat `git`'s verb `pull` whether a pull request is open, and whether it merged.
|
||||
func (f toolForge) Pull(owner, repo string, number int) (bool, bool, string, error) {
|
||||
raw, err := f.ask(GitPullAsk, map[string]any{"repository": owner + "/" + repo, "number": number})
|
||||
if err != nil {
|
||||
return false, false, "", err
|
||||
}
|
||||
answer, output, ok := unwrapAnswer(raw)
|
||||
if !ok {
|
||||
return false, false, "", fmt.Errorf("the seat git refused pull: %s", output)
|
||||
}
|
||||
var a struct {
|
||||
Pull struct {
|
||||
State string `json:"state"`
|
||||
Head string `json:"head"`
|
||||
} `json:"pull"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(answer, &a); err != nil || a.Error != "" || a.Pull.State == "" {
|
||||
return false, false, "", fmt.Errorf("the seat git's pull is not readable: %s", output)
|
||||
}
|
||||
return a.Pull.State == "open", a.Pull.State == "merged", a.Pull.Head, nil
|
||||
}
|
||||
|
||||
// GitPullAsk is the seat `git`'s verb that answers one pull request.
|
||||
const GitPullAsk = "seat:git.pull"
|
||||
|
||||
// unwrapAnswer reads a tool's answer whatever wraps it: the protocol's content list, or {output, ok, answer}.
|
||||
func unwrapAnswer(raw json.RawMessage) (json.RawMessage, string, bool) {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return unwrapAnswer(json.RawMessage(s))
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &m) != nil {
|
||||
return raw, string(raw), true
|
||||
}
|
||||
if content, has := m["content"]; has {
|
||||
var items []struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
var e struct {
|
||||
IsError bool `json:"isError"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &e)
|
||||
if json.Unmarshal(content, &items) == nil && len(items) > 0 {
|
||||
inner, out, ok := unwrapAnswer(json.RawMessage(items[0].Text))
|
||||
return inner, out, ok && !e.IsError
|
||||
}
|
||||
}
|
||||
var v struct {
|
||||
Output string `json:"output"`
|
||||
OK *bool `json:"ok"`
|
||||
Answer json.RawMessage `json:"answer"`
|
||||
}
|
||||
if json.Unmarshal(raw, &v) == nil && v.OK != nil {
|
||||
return v.Answer, v.Output, *v.OK
|
||||
}
|
||||
return raw, string(raw), true
|
||||
}
|
||||
@@ -1,504 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
)
|
||||
|
||||
// Issue 1826 (novox/hq ADR 0502): a value held for the open pull request that introduces its key.
|
||||
|
||||
const thePull = "novox/mesh-catalog#434"
|
||||
|
||||
// n8nPullDeclares is what the controller tells of the pull request: n8n gets a required number media-gid.
|
||||
func n8nPullDeclares() map[string]any {
|
||||
return map[string]any{"n8n": map[string]any{"media-gid": map[string]any{"type": "number",
|
||||
"description": "the media group", "required": true, "minimum": 1, "maximum": 65535}}}
|
||||
}
|
||||
|
||||
// aHoldingMesh is a proposing mesh with n8n assigned to ace and declaring nothing on the trunk (the case of the
|
||||
// issue), and a stand-in forge.
|
||||
func aHoldingMesh(t *testing.T) (*Config, *memStore, *standIn, *standForge) {
|
||||
t.Helper()
|
||||
c, m, r := aProposingMesh(t)
|
||||
m.declare(t, "n8n", `{"module":"n8n","settings":{}}`)
|
||||
m.assign(t, "ace", "n8n")
|
||||
f := &standForge{state: map[string][2]bool{}}
|
||||
c.Forge = f
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.events, m.handActs = nil, nil
|
||||
return c, m, r, f
|
||||
}
|
||||
|
||||
type standForge struct {
|
||||
state map[string][2]bool // pull -> open, merged
|
||||
head string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *standForge) Pull(owner, repo string, n int) (bool, bool, string, error) {
|
||||
if f.err != nil {
|
||||
return false, false, "", f.err
|
||||
}
|
||||
st, ok := f.state[(PullEvent{Owner: owner, Repo: repo, Number: n}).name()]
|
||||
if !ok {
|
||||
return true, false, f.head, nil
|
||||
}
|
||||
return st[0], st[1], f.head, nil
|
||||
}
|
||||
|
||||
// tell keeps what a pull request's head declares as the indexer's candidate is taken (takeCandidates).
|
||||
func tell(t *testing.T, c *Config, pull, head string, declares map[string]any) *PullDeclaresAnswer {
|
||||
t.Helper()
|
||||
raw, _ := json.Marshal(declares)
|
||||
d := map[string]map[string]Declaration{}
|
||||
if err := json.Unmarshal(raw, &d); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
a, err := c.declarePull(pull, head, d)
|
||||
if err != nil {
|
||||
t.Fatalf("declare: %v", err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// holdIt proposes media-gid=2000 for n8n on ace held for the pull request and has the operator approve it.
|
||||
func holdIt(t *testing.T, c *Config, m *memStore, r *standIn) string {
|
||||
t.Helper()
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull,
|
||||
Why: "n8n's container joins the media group, held for #434"})
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, err := c.Decide()
|
||||
if err != nil || len(said) != 1 || !strings.Contains(said[0], "approved by the operator and held") {
|
||||
t.Fatalf("the Approve decided %v (%v)", said, err)
|
||||
}
|
||||
return a.Proposal
|
||||
}
|
||||
|
||||
func keptCount(m *memStore) int { k, _ := m.Keys(KeptState); return len(k) }
|
||||
|
||||
// 1826/K1: mesh-config keeps what a pull request declares per pull request; pull-declares is gone (ADR 0590).
|
||||
func TestAPullRequestsDeclarationsAreKeptPerPullRequest(t *testing.T) {
|
||||
c, m, _, _ := aHoldingMesh(t)
|
||||
for _, caller := range []string{"g14.node-tools", Unproven, "", ControllerCaller} {
|
||||
if _, err := c.Do(Call{Action: "pull-declares", Pull: thePull, Head: "abc1234", Declares: n8nPullDeclares(),
|
||||
Caller: caller}); err == nil || !strings.Contains(err.Error(), "config-candidates") {
|
||||
t.Errorf("pull-declares from %q: %v", caller, err)
|
||||
}
|
||||
}
|
||||
a := tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
if len(a.Modules) != 1 || a.Modules[0] != "n8n" {
|
||||
t.Errorf("answered %+v", a)
|
||||
}
|
||||
e, _ := m.Get(PullsState, "novox.mesh-catalog.434")
|
||||
var pd PullDecl
|
||||
if e == nil || json.Unmarshal(e.Value, &pd) != nil || pd.Head != "abc1234def" ||
|
||||
pd.Declares["n8n"]["media-gid"].Type != "number" || !pd.Declares["n8n"]["media-gid"].Required {
|
||||
t.Errorf("kept %s", func() string {
|
||||
if e == nil {
|
||||
return "nothing"
|
||||
}
|
||||
return string(e.Value)
|
||||
}())
|
||||
}
|
||||
// Nothing is resolved from a pull request's declaration.
|
||||
if r, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); r != nil {
|
||||
t.Errorf("a pull request's declaration was resolved: %s", r.Value)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: a proposal for a key only the pull request declares is refused without its declaration, for a mesh or
|
||||
// node level, for a clear, for an undeclared key, and for a module not placed on the node; and trunk-only
|
||||
// proposals are judged as before.
|
||||
func TestAProposalNamingAPullRequestIsJudgedAgainstItsDeclaration(t *testing.T) {
|
||||
c, m, _, _ := aHoldingMesh(t)
|
||||
try := func(call Call) error {
|
||||
call.Action, call.Caller, call.Why = "propose", "g14.node-tools", "held for 434"
|
||||
_, err := c.Propose(call)
|
||||
return err
|
||||
}
|
||||
base := Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull}
|
||||
if err := try(base); err == nil || !strings.Contains(err.Error(), "holds no declaration for "+thePull) {
|
||||
t.Errorf("without the pull request's declaration: %v", err)
|
||||
}
|
||||
// And without the pull argument it is the trunk's refusal, as before.
|
||||
plain := base
|
||||
plain.Pull = ""
|
||||
if err := try(plain); err == nil || !strings.Contains(err.Error(), `n8n declares no value "media-gid"`) {
|
||||
t.Errorf("the trunk's refusal changed: %v", err)
|
||||
}
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
for name, tc := range map[string]struct {
|
||||
mutate func(*Call)
|
||||
want string
|
||||
}{
|
||||
"a mesh level": {func(c *Call) { c.Module, c.Node = "", "" }, "a module's own value on one node"},
|
||||
"a node level": {func(c *Call) { c.Module = "" }, "a module's own value on one node"},
|
||||
"a clear": {func(c *Call) { c.Clear, c.Value = true, nil }, "a clear is not held"},
|
||||
"a key not added": {func(c *Call) { c.Key = "other" }, "does not add or change a value"},
|
||||
"a bad value": {func(c *Call) { c.Value = 0.0 }, "below the minimum"},
|
||||
"a wrong type": {func(c *Call) { c.Value = "2000" }, "is not a number"},
|
||||
"another node": {func(c *Call) { c.Node = "g14" }, "is not placed on g14"},
|
||||
"another pull": {func(c *Call) { c.Pull = "novox/mesh-catalog#435" }, "holds no declaration for"},
|
||||
"a malformed pull": {func(c *Call) { c.Pull = "434" }, "owner/repository#number"},
|
||||
} {
|
||||
call := base
|
||||
tc.mutate(&call)
|
||||
if err := try(call); err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("%s: %v, want %q", name, err, tc.want)
|
||||
}
|
||||
}
|
||||
// A set or a clear through the verb takes no pull request.
|
||||
if _, err := c.Change(Call{Action: "set", Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull,
|
||||
Caller: "g14.node-tools", Why: "w"}); err == nil || !strings.Contains(err.Error(), "only a proposal") {
|
||||
t.Errorf("a set naming a pull request: %v", err)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("something was held")
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: every held value is asked whatever the key's trust; the ask says it is held and dropped on a close; on
|
||||
// Approve the value is kept apart, never set or resolved; history, event and hand-act say it; held answers it.
|
||||
func TestAHeldValueIsAskedAndKeptApartOnApprove(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "held for #434"})
|
||||
if len(a.Reaches) != 0 {
|
||||
t.Errorf("a held value reaches %v", a.Reaches)
|
||||
}
|
||||
ask := r.asks[a.Proposal]
|
||||
if err := occ.Check(ask, c.now()); err != nil {
|
||||
t.Fatalf("the ask: %v", err)
|
||||
}
|
||||
for _, want := range []string{thePull, "merges", "dropped if it closes unmerged", "media-gid", "n8n", "ace"} {
|
||||
if !strings.Contains(ask.Explanation, want) {
|
||||
t.Errorf("the ask does not say %q:\n%s", want, ask.Explanation)
|
||||
}
|
||||
}
|
||||
if strings.Contains(ask.Explanation, "takes effect at once") || !strings.Contains(ask.Headline, "Hold") {
|
||||
t.Errorf("the ask: %q\n%s", ask.Headline, ask.Explanation)
|
||||
}
|
||||
if ap := ask.Options[0]; !strings.Contains(ap.Does, thePull) || ap.Level == "" {
|
||||
t.Errorf("the approve option does %q", ap.Does)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Fatal("a proposal held something")
|
||||
}
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if len(said) != 1 || !strings.Contains(said[0], "held at abc1234d for "+thePull) {
|
||||
t.Fatalf("decided %v", said)
|
||||
}
|
||||
// Kept apart: not a value, not resolved.
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set {
|
||||
t.Error("an Approve set the value")
|
||||
}
|
||||
if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil {
|
||||
t.Errorf("an Approve resolved the value: %s", e.Value)
|
||||
}
|
||||
if keptCount(m) != 1 {
|
||||
t.Fatalf("%d values held", keptCount(m))
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionHold || m.events[0].Pull != thePull ||
|
||||
m.events[0].Key != "media-gid" || m.events[0].Head != "abc1234def" || len(m.events[0].Moved) != 0 ||
|
||||
!strings.Contains(m.events[0].Approved, "approved by the operator") {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
if len(m.handActs) != 1 || !strings.Contains(m.handActs[0], thePull) {
|
||||
t.Errorf("hand-acts %q", m.handActs)
|
||||
}
|
||||
h, _ := c.History(Call{Module: "n8n", Node: "ace"})
|
||||
if len(h) != 1 || h[0].Action != ActionHold || h[0].Pull != thePull {
|
||||
t.Errorf("history %+v", h)
|
||||
}
|
||||
// held answers it, at the head it is kept at, to the controller alone.
|
||||
if _, err := c.Do(Call{Action: "held", Caller: "g14.node-tools", Pulls: []PullAt{{thePull, "abc1234def"}}}); err == nil {
|
||||
t.Error("held answered a caller that is not the controller")
|
||||
}
|
||||
got, err := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "abc1234def"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ha := got.(*HeldAnswer)
|
||||
if len(ha.Held) != 1 || ha.Held[0].Module != "n8n" || ha.Held[0].Node != "ace" || ha.Held[0].Key != "media-gid" ||
|
||||
string(ha.Held[0].Value) != "2000" || !strings.Contains(ha.Held[0].Approved, "approved by the operator") ||
|
||||
ha.Held[0].Pull != thePull || len(ha.Skipped) != 0 {
|
||||
t.Errorf("held answered %+v", ha)
|
||||
}
|
||||
// Another head, and another pull request, are not answered.
|
||||
got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller,
|
||||
Pulls: []PullAt{{thePull, "ffff0000"}, {"novox/mesh-catalog#435", "abc"}}})
|
||||
if ha = got.(*HeldAnswer); len(ha.Held) != 0 || len(ha.Skipped) != 1 || !strings.Contains(ha.Skipped[0].Why, "held at") {
|
||||
t.Errorf("held at another head answered %+v", ha)
|
||||
}
|
||||
// The listing shows it as held, and the proposal as held for the pull request.
|
||||
l, _ := c.List(Call{Module: "n8n"})
|
||||
if len(l.Held) != 1 || len(l.Set) != 0 {
|
||||
t.Errorf("the listing: held %+v set %+v", l.Held, l.Set)
|
||||
}
|
||||
rows, _ := c.Proposals(Call{})
|
||||
if len(rows) != 1 || rows[0].Pull != thePull || rows[0].State != ProposalApproved {
|
||||
t.Errorf("proposals %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K2: an Approve is refused when the pull request's declaration changed after the operator was shown it.
|
||||
func TestAnApproveOfAHeldValueIsRefusedWhenTheDeclarationChanged(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
a := propose(t, c, Call{Module: "n8n", Node: "ace", Key: "media-gid", Value: 2000.0, Pull: thePull, Why: "w"})
|
||||
changed := n8nPullDeclares()
|
||||
changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0
|
||||
tell(t, c, thePull, "def5678abc", changed)
|
||||
r.choose(t, a.Proposal, OptionApprove, nil)
|
||||
said, _ := c.Decide()
|
||||
if len(said) != 1 || !strings.Contains(said[0], "nothing is held") || keptCount(m) != 0 {
|
||||
t.Errorf("decided %v; %d held", said, keptCount(m))
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K1: at a new head a held value is kept if its key is declared the same and it still passes; otherwise it is
|
||||
// dropped, recorded, raised and said.
|
||||
func TestANewHeadKeepsAHeldValueOnlyWhileItsDeclarationIsUnchanged(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
m.events = nil
|
||||
|
||||
// The same declaration at a new head (the description may differ): kept, moved to the new head.
|
||||
same := n8nPullDeclares()
|
||||
same["n8n"].(map[string]any)["media-gid"].(map[string]any)["description"] = "reworded"
|
||||
if a := tell(t, c, thePull, "1111111111", same); len(a.Dropped) != 0 {
|
||||
t.Errorf("an unchanged declaration dropped %v", a.Dropped)
|
||||
}
|
||||
got, _ := c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "1111111111"}}})
|
||||
if len(got.(*HeldAnswer).Held) != 1 || len(m.events) != 0 {
|
||||
t.Errorf("after an unchanged head: %+v, events %+v", got, m.events)
|
||||
}
|
||||
|
||||
// A changed declaration: dropped.
|
||||
changed := n8nPullDeclares()
|
||||
changed["n8n"].(map[string]any)["media-gid"].(map[string]any)["maximum"] = 1000.0
|
||||
a := tell(t, c, thePull, "2222222222", changed)
|
||||
if len(a.Dropped) != 1 || !strings.Contains(a.Dropped[0], "declares the key otherwise") || keptCount(m) != 0 {
|
||||
t.Fatalf("a changed declaration: %+v, %d held", a, keptCount(m))
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
got, _ = c.Do(Call{Action: "held", Caller: ControllerCaller, Pulls: []PullAt{{thePull, "2222222222"}}})
|
||||
if len(got.(*HeldAnswer).Held) != 0 {
|
||||
t.Error("a dropped value was answered")
|
||||
}
|
||||
|
||||
// A head that no longer adds the key drops it too.
|
||||
tell(t, c, thePull, "3333333333", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
if a = tell(t, c, thePull, "4444444444", map[string]any{}); len(a.Dropped) != 1 ||
|
||||
!strings.Contains(a.Dropped[0], "no longer adds or changes") {
|
||||
t.Errorf("a head without the key: %+v", a)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: the merge writes the held value as the module's own value with its approval kept, before the trunk
|
||||
// declares the key; it resolves right after the declared-config mirror moves.
|
||||
func TestTheMergeAppliesAHeldValueAndItResolvesOnceTheTrunkDeclaresTheKey(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
approved := func() string {
|
||||
raw, _ := m.Get(KeptState, "novox.mesh-catalog.434.ace.n8n.media-gid")
|
||||
// the state kept is the seat's layout, under the seat's key: the checker and the assigner read it with this
|
||||
k, err := meshconfigclient.DecodeKept("novox.mesh-catalog.434.ace.n8n.media-gid", raw.Value)
|
||||
if err != nil || string(heldValue(k)) != "2000" || k.Declaration == "" || k.At == nil {
|
||||
t.Fatalf("the held value is not the seat's: %+v, %v", k, err)
|
||||
}
|
||||
return k.Approved
|
||||
}()
|
||||
m.events, m.handActs = nil, nil
|
||||
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true)
|
||||
if len(said) == 0 {
|
||||
t.Fatal("the merge said nothing")
|
||||
}
|
||||
v, set := storedValue(t, m, "module.ace.n8n.media-gid")
|
||||
if !set || string(v.Value) != "2000" || v.Approved != approved || v.Approved == "" || v.By != "the operator" ||
|
||||
!strings.Contains(v.Why, thePull) {
|
||||
t.Fatalf("the merge wrote %+v", v)
|
||||
}
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("the value is still held after the merge")
|
||||
}
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("the pull request's declarations are still kept: %v", p)
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != "set" || m.events[0].Pull != thePull ||
|
||||
m.events[0].Approved != approved || len(m.handActs) != 1 {
|
||||
t.Errorf("raised %+v, recorded %q", m.events, m.handActs)
|
||||
}
|
||||
// Not resolved before the trunk declares it.
|
||||
if e, _ := m.Get(ResolvedState, ResolvedKey("ace", "n8n", "media-gid")); e != nil {
|
||||
t.Errorf("resolved before the trunk declares the key: %s", e.Value)
|
||||
}
|
||||
m.declare(t, "n8n", `{"module":"n8n","settings":{"media-gid":{"kind":"","default":null,"why":"",`+
|
||||
`"type":"number","required":true,"minimum":1,"maximum":65535}}}`)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res := m.resolved(t, "ace", "n8n", "media-gid"); string(res.Value) != "2000" || res.Source != SourceModule {
|
||||
t.Errorf("media-gid resolves as %+v", res)
|
||||
}
|
||||
l, _ := c.List(Call{Module: "n8n", Node: "ace"})
|
||||
if len(l.Set) != 1 || l.Set[0].Approved != approved || len(l.Held) != 0 {
|
||||
t.Errorf("the listing: set %+v held %+v", l.Set, l.Held)
|
||||
}
|
||||
// A merge heard twice sets nothing twice.
|
||||
m.events = nil
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "abc1234def"}, true)
|
||||
if len(m.events) != 0 {
|
||||
t.Errorf("a second merge raised %+v", m.events)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: a pull request closed unmerged drops its held values and says so; nothing is set or resolved.
|
||||
func TestAClosedPullRequestDropsItsHeldValuesAndSaysSo(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
m.events = nil
|
||||
f.state[thePull] = [2]bool{false, false}
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if len(said) == 0 || !strings.Contains(strings.Join(said, "\n"), "closed unmerged: dropped, nothing was set") {
|
||||
t.Errorf("said %v", said)
|
||||
}
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 {
|
||||
t.Error("a closed pull request left a value")
|
||||
}
|
||||
if len(m.events) != 1 || m.events[0].Action != ActionDrop || m.events[0].Pull != thePull ||
|
||||
!strings.Contains(m.events[0].Why, "closed unmerged") {
|
||||
t.Errorf("raised %+v", m.events)
|
||||
}
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("declarations of a closed pull request kept: %v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: a close event for a pull request the forge says merged is the merge.
|
||||
func TestACloseEventForAMergedPullRequestIsItsMerge(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
f.state[thePull] = [2]bool{false, true}
|
||||
f.head = "abc1234def"
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" {
|
||||
t.Errorf("the merge of a pull request announced closed set %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K3: the five-minute look finds a merge, a close, and an open pull request, and says a forge that does not
|
||||
// answer.
|
||||
func TestTheLookAtTheForgeFindsAMissedMergeOrCloseAndLeavesAnOpenPullRequest(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
if said := c.SweepPulls(); len(said) != 0 || keptCount(m) != 1 {
|
||||
t.Errorf("an open pull request: said %v, %d held", said, keptCount(m))
|
||||
}
|
||||
f.err = errors.New("the forge is down")
|
||||
if said := c.SweepPulls(); len(said) != 1 || !strings.Contains(said[0], "does not say whether") || keptCount(m) != 1 {
|
||||
t.Errorf("a silent forge: said %v", said)
|
||||
}
|
||||
f.err = nil
|
||||
f.head = "abc1234def"
|
||||
f.state[thePull] = [2]bool{false, true}
|
||||
c.SweepPulls()
|
||||
if v, set := storedValue(t, m, "module.ace.n8n.media-gid"); !set || string(v.Value) != "2000" || keptCount(m) != 0 {
|
||||
t.Errorf("a missed merge: %+v", v)
|
||||
}
|
||||
// A missed close.
|
||||
tell(t, c, "novox/mesh-catalog#440", "abc", n8nPullDeclares())
|
||||
f.state["novox/mesh-catalog#440"] = [2]bool{false, false}
|
||||
c.SweepPulls()
|
||||
if p, _ := m.Keys(PullsState); len(p) != 0 {
|
||||
t.Errorf("a missed close left %v", p)
|
||||
}
|
||||
}
|
||||
|
||||
// 1826/K1: the new arguments are read strictly and the new actions are the verb's.
|
||||
func TestThePullArgumentsAreRead(t *testing.T) {
|
||||
call, err := callOf(map[string]any{"action": "pull-declares", "pull": " " + thePull + " ", "head": "abc",
|
||||
"declares": map[string]any{"n8n": map[string]any{}}, "pulls": []any{map[string]any{"pull": thePull, "head": "h"}}})
|
||||
if err != nil || call.Pull != thePull || call.Head != "abc" || len(call.Declares) != 1 || len(call.Pulls) != 1 ||
|
||||
call.Pulls[0].Head != "h" {
|
||||
t.Errorf("read %+v (%v)", call, err)
|
||||
}
|
||||
if _, err := callOf(map[string]any{"declares": "x"}); err == nil {
|
||||
t.Error("declares that is not an object was read")
|
||||
}
|
||||
if _, err := callOf(map[string]any{"pulls": "x"}); err == nil {
|
||||
t.Error("pulls that is not a list was read")
|
||||
}
|
||||
if _, err := callOf(map[string]any{"bogus": 1}); err == nil || !strings.Contains(err.Error(), "pull") {
|
||||
t.Errorf("an unknown argument: %v", err)
|
||||
}
|
||||
_ = time.Second
|
||||
}
|
||||
|
||||
// 1826 review r1: the merge applies a value only if it is held at the merged head; otherwise it is dropped and said.
|
||||
func TestTheMergeAppliesOnlyAValueHeldAtTheMergedHead(t *testing.T) {
|
||||
for name, head := range map[string]string{"another head": "9999999999", "no head told": ""} {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
said := c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: head}, true)
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set || keptCount(m) != 0 ||
|
||||
!strings.Contains(strings.Join(said, "\n"), "dropped, nothing was set") {
|
||||
t.Errorf("%s: set=%v held=%d said %v", name, set, keptCount(m), said)
|
||||
}
|
||||
if len(m.events) == 0 || m.events[len(m.events)-1].Action != ActionDrop {
|
||||
t.Errorf("%s: raised %+v", name, m.events)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1826 review r1: a later head without the declaration drops the value, so the merge has nothing to apply.
|
||||
func TestALaterHeadWithoutTheDeclarationLeavesNothingToApplyAtTheMerge(t *testing.T) {
|
||||
c, m, r, _ := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
tell(t, c, thePull, "bbbbbbbbbb", map[string]any{})
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434, Merged: true, HeadSHA: "bbbbbbbbbb"}, true)
|
||||
if _, set := storedValue(t, m, "module.ace.n8n.media-gid"); set {
|
||||
t.Error("a value held for a declaration the merged head lacks was set")
|
||||
}
|
||||
}
|
||||
|
||||
// 1826 review r1: a close the forge cannot be asked about drops nothing; the five-minute look decides.
|
||||
func TestACloseThatTheForgeCannotBeAskedAboutDropsNothing(t *testing.T) {
|
||||
c, m, r, f := aHoldingMesh(t)
|
||||
tell(t, c, thePull, "abc1234def", n8nPullDeclares())
|
||||
holdIt(t, c, m, r)
|
||||
f.err = errors.New("the forge is down")
|
||||
c.HeardPull(PullEvent{Owner: "novox", Repo: "mesh-catalog", Number: 434}, false)
|
||||
if keptCount(m) != 1 {
|
||||
t.Fatal("a close the forge could not confirm dropped the value")
|
||||
}
|
||||
f.err = nil
|
||||
f.state[thePull] = [2]bool{false, false}
|
||||
c.SweepPulls()
|
||||
if keptCount(m) != 0 {
|
||||
t.Error("the look did not drop it")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
// Issue 1982, against a real bus: mesh-config whole asks the bus for nothing while no input of it changes, and a changed input
|
||||
// resolves only what it touches. The binary runs as the node's tool runtime plays it (whole_integration_test.go), and every
|
||||
// request it makes of the runtime is counted: what a state read, a watch and a write ask of the bus.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/testbus"
|
||||
)
|
||||
|
||||
func (p *player) snapshot() (calls map[string]int, puts []string) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
calls = map[string]int{}
|
||||
for k, v := range p.calls {
|
||||
calls[k] = v
|
||||
}
|
||||
return calls, slices.Clone(p.puts)
|
||||
}
|
||||
|
||||
// asked is how many requests of these methods the module made between two snapshots.
|
||||
func asked(before, after map[string]int, methods ...string) int {
|
||||
n := 0
|
||||
for _, m := range methods {
|
||||
n += after[m] - before[m]
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func TestConfigWholeAsksTheBusForNothingWhileNoInputChanges(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
bin := filepath.Join(t.TempDir(), Module)
|
||||
build := exec.Command("go", "build", "-o", bin, ".")
|
||||
build.Stderr = os.Stderr
|
||||
if err := build.Run(); err != nil {
|
||||
t.Fatalf("mesh-config does not build: %v", err)
|
||||
}
|
||||
nc, err := nats.Connect(testbus.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(nc.Close)
|
||||
js, err := jetstream.New(nc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants", "mesh-delivery-indexer_predefined"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState,
|
||||
RenamesState, RetiringState, FindingsState} {
|
||||
buckets = append(buckets, Module+"_"+s)
|
||||
}
|
||||
for _, b := range buckets {
|
||||
if _, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: b, History: 2}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
haves := map[string]string{"g14.player": fpOf("player", fp1), "novox.player": fpOf("player", fp1), "g14.radio": fpOf("radio", fp1)}
|
||||
p := start(t, bin, js, newStandIn(), haves, nil)
|
||||
p.viewsReady()
|
||||
|
||||
const radio = `{"module":"radio","config":{"band":{"type":"number","default":1}}}`
|
||||
p.manifestIn("player", oldPlayer)
|
||||
p.manifestIn("radio", radio)
|
||||
p.buildIn("g14", "player", fpOf("player", fp1), oldPlayer)
|
||||
p.buildIn("novox", "player", fpOf("player", fp1), oldPlayer)
|
||||
p.buildIn("g14", "radio", fpOf("radio", fp1), radio)
|
||||
p.placed("g14", "player", "radio")
|
||||
p.placed("novox", "player")
|
||||
p.hearsTheViews()
|
||||
eventually(t, "every value is resolved", func() bool {
|
||||
a, okA := resolvedValue(p, "g14", "player", "volume")
|
||||
b, okB := resolvedValue(p, "novox", "player", "volume")
|
||||
c, okC := resolvedValue(p, "g14", "radio", "band")
|
||||
return okA && okB && okC && a == "5" && b == "5" && c == "1"
|
||||
})
|
||||
// The passes the setup moved have ended when the module answers a tool: it takes its lock for none.
|
||||
if _, err := p.call(ResyncTool, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// No input changes: the delivery says it delivered and a node-engine says what it reads, with no rename window open and no retirement
|
||||
// waiting. Each is heard and none is a pass: nothing is asked of the bus. (The module has no polling timer; its one deadline
|
||||
// timer is set for a real deadline, and none is near.)
|
||||
before, putsBefore := p.snapshot()
|
||||
for i := 0; i < 20; i++ {
|
||||
p.event("mesh-delivery-view.delivered", map[string]any{"revision": "r"})
|
||||
p.event("node-engine.manifest-reads-changed", map[string]any{"node": "g14"})
|
||||
}
|
||||
after, putsAfter := p.snapshot()
|
||||
p.noGitAsk()
|
||||
if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch", "mesh/state.put", "mesh/state.create",
|
||||
"mesh/state.update", "mesh/state.delete", "mesh/ask"); n != 0 || len(putsAfter) != len(putsBefore) {
|
||||
t.Errorf("40 events that move nothing asked %d requests of the bus (%v); wrote %v", n, after, putsAfter[len(putsBefore):])
|
||||
}
|
||||
|
||||
// One input changes: player's manifest declares a second value. Only player's values on its two nodes are written, and `declared`
|
||||
// for player; radio's resolved values are not written again, and nothing is read key by key.
|
||||
before, putsBefore = p.snapshot()
|
||||
const gain = `{"module":"player","config":{"volume":{"type":"number","default":5},"gain":{"type":"number","default":2}}}`
|
||||
p.manifestIn("player", gain)
|
||||
eventually(t, "the new value is resolved on both nodes", func() bool {
|
||||
a, okA := resolvedValue(p, "g14", "player", "gain")
|
||||
b, okB := resolvedValue(p, "novox", "player", "gain")
|
||||
return okA && okB && a == "2" && b == "2"
|
||||
})
|
||||
after, putsAfter = p.snapshot()
|
||||
var wrote []string
|
||||
for _, w := range putsAfter[len(putsBefore):] {
|
||||
wrote = append(wrote, strings.TrimPrefix(w, "mesh/state."))
|
||||
}
|
||||
for _, w := range wrote {
|
||||
if !strings.Contains(w, ResolvedState+"/g14.player.") && !strings.Contains(w, ResolvedState+"/novox.player.") &&
|
||||
!strings.Contains(w, DeclaredState+"/player") {
|
||||
t.Errorf("a changed manifest of player wrote %q, which is not player's", w)
|
||||
}
|
||||
}
|
||||
if n := asked(before, after, "mesh/state.get", "mesh/state.keys", "mesh/state.watch"); n != 0 {
|
||||
t.Errorf("a changed manifest read %d keys of the bus's states one by one (%v)", n, after)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
package main
|
||||
|
||||
// What mesh-config asks of the bus per input (issue 1982): its own states are read from the copy their watches keep, an event
|
||||
// that moves nothing reads nothing, and the one timer left is a deadline.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// countingStore counts the reads of the states by state: what the bus would be asked.
|
||||
type countingStore struct {
|
||||
*memStore
|
||||
mu sync.Mutex
|
||||
reads map[string]int
|
||||
}
|
||||
|
||||
func (s *countingStore) Get(state, key string) (*Entry, error) {
|
||||
s.mu.Lock()
|
||||
s.reads[state]++
|
||||
s.mu.Unlock()
|
||||
return s.memStore.Get(state, key)
|
||||
}
|
||||
|
||||
func (s *countingStore) Keys(state string) ([]string, error) {
|
||||
s.mu.Lock()
|
||||
s.reads[state]++
|
||||
s.mu.Unlock()
|
||||
return s.memStore.Keys(state)
|
||||
}
|
||||
|
||||
// readsOf is the reads of every state but those named.
|
||||
func (s *countingStore) readsExcept(states ...string) map[string]int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := map[string]int{}
|
||||
for k, n := range s.reads {
|
||||
out[k] = n
|
||||
}
|
||||
for _, k := range states {
|
||||
delete(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// **An event that moves nothing reads nothing but what it judges** (design 32 *A config value renamed*, *A value no longer
|
||||
// read*): with no rename window open, `delivered` and `manifest-reads-changed` read no source, and a `manifest-accepted`
|
||||
// whose rows are all marked or read again reads none either. Each was a read of every key of every state of the module
|
||||
// (about 4,000 requests) under a delivery's burst of events.
|
||||
func TestAnEventThatMovesNothingReadsNoSource(t *testing.T) {
|
||||
c, m, d, _, _, _ := retiringMesh(t)
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
d.rows = []ReaderRow{{Module: "player", Key: "volume", Kind: KindValue, Readers: []string{"g14"}}} // read: nothing to retire
|
||||
for name, event := range map[string]func(){
|
||||
"delivered": func() { c.Windows("") },
|
||||
"manifest-reads-changed": func() { c.Windows("g14") },
|
||||
"manifest-accepted": func() { c.Retirements("player") },
|
||||
} {
|
||||
cs.reads = map[string]int{}
|
||||
event()
|
||||
if got := cs.readsExcept(RenamesState, RetiringState); len(got) != 0 {
|
||||
t.Errorf("%s read %v", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An open window is judged, and reads what judging it needs: the same event with a window open reads the sources.
|
||||
func TestAnEventWithAWindowOpenJudgesIt(t *testing.T) {
|
||||
c, m, _, _ := aRenamingMesh(t)
|
||||
m.declare(t, "player", newPlayer)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
c.Windows("")
|
||||
if cs.reads[ValuesState] == 0 {
|
||||
t.Errorf("an open window was judged on no source: %v", cs.reads)
|
||||
}
|
||||
}
|
||||
|
||||
// **An ask of a retirement that ends with no choice is taken when the router says so** (design 32 *A value no longer read*):
|
||||
// with no minute's look at the router's record, the `decided` event of an ask that expired moves the retirement.
|
||||
func TestARetirementWhoseAskExpiredIsTakenOnTheRoutersWord(t *testing.T) {
|
||||
c, m, d, r, _, _ := retiringMesh(t)
|
||||
for _, node := range []string{"g14", "novox"} {
|
||||
m.want(t, node, "player", fp3, donePlayer)
|
||||
d.haves[node+".player"] = fp3
|
||||
}
|
||||
c.Windows("")
|
||||
c.Retirements("player")
|
||||
rows := retiringOf(t, c)
|
||||
if len(rows) != 1 || rows[0].State != RetireAsked {
|
||||
t.Fatalf("retiring %+v, want asked", rows)
|
||||
}
|
||||
r.choose(t, rows[0].Ask, OptionApprove, func(w *oc.Warrant) { w.Outcome = occ.OutcomeExpired })
|
||||
c.HeardEnd(oc.Warrant{Ask: rows[0].Ask, Asker: Module, Outcome: occ.OutcomeExpired})
|
||||
if rows = retiringOf(t, c); rows[0].State != RetireUnanswered {
|
||||
t.Errorf("after the router said the ask expired: %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// The next deadline is the earliest of a waiting proposal's (an hour past its ask lapsing, with no record at the router)
|
||||
// and an unanswered retirement's (asked once more after HeldFinding); none when nothing waits on a time.
|
||||
func TestTheNextDeadlineIsTheEarliestOfWhatWaitsOnATime(t *testing.T) {
|
||||
m := newStore()
|
||||
c := &Config{Store: m, Catalog: m, Logf: t.Logf}
|
||||
if _, ok := c.NextDeadline(); ok {
|
||||
t.Fatal("a deadline with nothing waiting")
|
||||
}
|
||||
t0 := time.Date(2026, 11, 19, 10, 0, 0, 0, time.UTC)
|
||||
put := func(state, key string, v any) {
|
||||
if err := m.Put(state, key, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
put(ProposalsState, "p1", Proposal{ID: "p1", State: ProposalWaiting, At: t0.Add(48 * time.Hour)})
|
||||
put(ProposalsState, "p2", Proposal{ID: "p2", State: ProposalEnded, At: t0})
|
||||
want := t0.Add(48*time.Hour + ProposalLasts + time.Hour)
|
||||
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
||||
t.Errorf("deadline %v, %v; want %v", at, ok, want)
|
||||
}
|
||||
put(RetiringState, "player.volume", Retiring{Module: "player", Key: "volume", State: RetireUnanswered, Asked: t0.Add(-6 * 24 * time.Hour)})
|
||||
want = t0.Add(HeldFinding - 6*24*time.Hour)
|
||||
if at, ok := c.NextDeadline(); !ok || !at.Equal(want) {
|
||||
t.Errorf("deadline %v, %v; want the retirement's %v", at, ok, want)
|
||||
}
|
||||
}
|
||||
|
||||
// busOf is a bus of states for the copy's tests: what each state holds, the watches handed out and the requests asked.
|
||||
type busOf struct {
|
||||
mu sync.Mutex
|
||||
held map[string]map[string][]byte
|
||||
handles map[string]func(stdio.StateChange) error
|
||||
watches int
|
||||
gets int
|
||||
writes int
|
||||
revision uint64
|
||||
}
|
||||
|
||||
func (b *busOf) ownCopy() *ownCopy {
|
||||
return &ownCopy{
|
||||
copy: stdio.WatchedStatesOver(func(state string, handle func(stdio.StateChange) error) error {
|
||||
b.mu.Lock()
|
||||
b.watches++
|
||||
b.handles[state] = handle
|
||||
current := map[string][]byte{}
|
||||
for k, v := range b.held[state] {
|
||||
current[k] = v
|
||||
}
|
||||
b.mu.Unlock()
|
||||
for k, v := range current {
|
||||
if err := handle(stdio.StateChange{State: state, Key: k, Op: stdio.OpPut, Value: v, Revision: 1, Current: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}, func(state, key string) ([]byte, bool, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.gets++
|
||||
v, ok := b.held[state][key]
|
||||
return v, ok, nil
|
||||
}),
|
||||
put: func(state, key string, value any) (uint64, error) {
|
||||
raw, _ := json.Marshal(value)
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.writes++
|
||||
b.revision++
|
||||
if b.held[state] == nil {
|
||||
b.held[state] = map[string][]byte{}
|
||||
}
|
||||
b.held[state][key] = raw
|
||||
return b.revision + 1, nil
|
||||
},
|
||||
del: func(state, key string) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.writes++
|
||||
delete(b.held[state], key)
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
// **The module's own states are read from a copy, never key by key** (ADR 0580 §1, issue 1982): a whole state read of 1,176
|
||||
// keys is one watch and no get, again and again; this process's write is in the copy at once, another's arrives by the watch.
|
||||
func TestAnOwnStateIsReadFromItsWatchedCopyNotKeyByKey(t *testing.T) {
|
||||
b := &busOf{held: map[string]map[string][]byte{ValuesState: {}}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
for i := 0; i < 1176; i++ {
|
||||
b.held[ValuesState]["mesh.key-"+time.Duration(i).String()] = []byte(`{"value":1}`)
|
||||
}
|
||||
n := len(b.held[ValuesState])
|
||||
own := b.ownCopy()
|
||||
for pass := 0; pass < 3; pass++ {
|
||||
keys, err := own.keys(ValuesState)
|
||||
if err != nil || len(keys) != n {
|
||||
t.Fatalf("keys %d, %v; want %d", len(keys), err, n)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if e, err := own.get(ValuesState, k); err != nil || e == nil {
|
||||
t.Fatalf("get %s: %v, %v", k, e, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if b.watches != 1 || b.gets != 0 {
|
||||
t.Errorf("3 passes over %d keys asked the bus for %d watches and %d gets; want 1 and 0", n, b.watches, b.gets)
|
||||
}
|
||||
// Its own write and removal are in the copy at once.
|
||||
if err := own.write(ValuesState, "mine", map[string]string{"x": "mine"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "mine"); e == nil {
|
||||
t.Error("a key just written is not read")
|
||||
}
|
||||
if err := own.remove(ValuesState, "mine"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "mine"); e != nil {
|
||||
t.Error("a key just removed is still read")
|
||||
}
|
||||
// Another writer's change arrives by the watch.
|
||||
if err := b.handles[ValuesState](stdio.StateChange{State: ValuesState, Key: "theirs", Op: stdio.OpPut, Value: []byte(`{"value":2}`), Revision: 99}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e, _ := own.get(ValuesState, "theirs"); e == nil {
|
||||
t.Error("a change another wrote is not read")
|
||||
}
|
||||
if b.gets != 0 {
|
||||
t.Errorf("%d gets", b.gets)
|
||||
}
|
||||
}
|
||||
|
||||
// A write and a removal tell what waits on a deadline, once each.
|
||||
func TestAWriteTellsWhatWaitsOnADeadline(t *testing.T) {
|
||||
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
own := b.ownCopy()
|
||||
var told []string
|
||||
own.changed = func(state string) { told = append(told, state) }
|
||||
if err := own.write(ProposalsState, "p", Proposal{ID: "p"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := own.remove(ProposalsState, "p"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(told) != 2 || told[0] != ProposalsState {
|
||||
t.Errorf("told %v", told)
|
||||
}
|
||||
}
|
||||
|
||||
// The mesh knows no pull request (design 47 G; ADR 0502 ended by the operator's word, record pending): the verb takes no `pull`,
|
||||
// `head`, `declares` or `pulls`, and does neither `pull-declares` nor `held`.
|
||||
func TestTheVerbKnowsNoPullRequest(t *testing.T) {
|
||||
for _, arg := range []string{"pull", "head", "declares", "pulls"} {
|
||||
if _, err := callOf(map[string]any{"action": "propose", arg: "x"}); err == nil || !strings.Contains(err.Error(), arg) {
|
||||
t.Errorf("an argument %s was taken: %v", arg, err)
|
||||
}
|
||||
}
|
||||
c, _ := aMesh(t)
|
||||
for _, action := range []string{"pull-declares", "held"} {
|
||||
if _, err := c.Do(Call{Action: action}); err == nil || !strings.Contains(err.Error(), action) {
|
||||
t.Errorf("the action %s was done: %v", action, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The deadline timer's floor is the setting deadline-floor-seconds, the default for a value that is not a positive number.
|
||||
func TestTheDeadlineFloorIsTheSettingOrItsDefault(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
raw string
|
||||
err error
|
||||
want time.Duration
|
||||
}{
|
||||
{`5`, nil, 5 * time.Second}, {`0`, nil, DefaultDeadlineFloor}, {`"x"`, nil, DefaultDeadlineFloor},
|
||||
{``, errors.New("not declared"), DefaultDeadlineFloor}, {`90`, nil, 90 * time.Second},
|
||||
} {
|
||||
if got := floorOf(json.RawMessage(c.raw), c.err); got != c.want {
|
||||
t.Errorf("floor of %q (%v) = %v, want %v", c.raw, c.err, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **A rename window held seven days comes as a finding in a quiet mesh** (lead answer A-490l): the deadline is the window's
|
||||
// seventh day, the finding is written at it with no resolution pass, and a window that is not yet seven days old writes none.
|
||||
func TestAHeldWindowIsAFindingAtItsDeadlineWithNoPass(t *testing.T) {
|
||||
c, m, _, clock := aRenamingMesh(t)
|
||||
m.declare(t, "player", newPlayer)
|
||||
if _, err := c.Reconcile(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
at, ok := c.NextDeadline()
|
||||
if !ok || !at.Equal(clock.Add(HeldFinding)) {
|
||||
t.Fatalf("deadline %v, %v; want the window's seventh day %v", at, ok, clock.Add(HeldFinding))
|
||||
}
|
||||
if err := c.JudgeHeldWindows(); err != nil || len(findingsOf(t, m, FindingWindowHeld)) != 0 {
|
||||
t.Fatalf("a young window wrote a finding (%v)", err)
|
||||
}
|
||||
*clock = at
|
||||
cs := &countingStore{memStore: m, reads: map[string]int{}}
|
||||
c.Store = cs
|
||||
if err := c.JudgeHeldWindows(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fs := findingsOf(t, m, FindingWindowHeld); len(fs) != 2 {
|
||||
t.Errorf("findings %+v, want one for each node holding the window", fs)
|
||||
}
|
||||
if got := cs.readsExcept(RenamesState); len(got) != 0 {
|
||||
t.Errorf("judging the held windows read %v", got)
|
||||
}
|
||||
if _, ok := c.NextDeadline(); ok {
|
||||
t.Error("a deadline is still set once the finding is written")
|
||||
}
|
||||
}
|
||||
|
||||
// A write of a proposal, a retirement or a rename window sets the deadline timer again, and no other state does: a window opened
|
||||
// after start, with nothing else waiting on a time, must not leave the timer blocked (A-490l).
|
||||
func TestAWriteOfARenameWindowRearmsTheDeadlineTimer(t *testing.T) {
|
||||
for state, want := range map[string]bool{ProposalsState: true, RetiringState: true, RenamesState: true,
|
||||
ValuesState: false, ResolvedState: false, DeclaredState: false} {
|
||||
if got := armsDeadline(state); got != want {
|
||||
t.Errorf("a write of %s arms the timer: %v, want %v", state, got, want)
|
||||
}
|
||||
}
|
||||
// Wired as main wires it: the copy's write of a window tells the channel the timer waits on.
|
||||
b := &busOf{held: map[string]map[string][]byte{}, handles: map[string]func(stdio.StateChange) error{}}
|
||||
own := b.ownCopy()
|
||||
rearm := make(chan struct{}, 1)
|
||||
own.changed = func(state string) {
|
||||
if armsDeadline(state) {
|
||||
select {
|
||||
case rearm <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := own.write(RenamesState, "player.level", Rename{Module: "player"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-rearm:
|
||||
default:
|
||||
t.Error("a rename window written after start did not rearm the timer")
|
||||
}
|
||||
}
|
||||
|
||||
// CONFIG-1/AC1 (design 47 H §763, §764; H-A-49): mesh-config consumes no pull request event and no `manifest-accepted`,
|
||||
// calls no `git.pull`, and judges the retirements of each module a `catalog-revision` names changed.
|
||||
func TestARetirementIsJudgedOnACatalogRevisionAndNoPullIsHeard(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, gone := range []string{`"git.pull-merged"`, `"git.pull-closed"`, `"seat:git.pull"`, `"mesh-catalog.manifest-accepted"`} {
|
||||
if strings.Contains(string(raw), gone) {
|
||||
t.Errorf("the manifest still names %s", gone)
|
||||
}
|
||||
}
|
||||
c, _, d, _, _, _ := retiringMesh(t)
|
||||
d.known = false
|
||||
said := catalogRevisionRetirements(c, []byte(`{"v":1,"revision":4,"kind":"trunk","branch":"main","changed":["player","radio"]}`))
|
||||
if len(said) != 2 || !strings.Contains(said[0], "player") || !strings.Contains(said[1], "radio") {
|
||||
t.Fatalf("a catalog revision naming player and radio judged %q", said)
|
||||
}
|
||||
if got := catalogRevisionRetirements(c, []byte(`{"v":1,"revision":5,"kind":"trunk","branch":"main"}`)); len(got) != 0 {
|
||||
t.Fatalf("a revision naming no module judged %q", got)
|
||||
}
|
||||
}
|
||||
@@ -293,14 +293,26 @@ func (c *Config) Windows(node string) []string {
|
||||
return said
|
||||
}
|
||||
|
||||
// windowOpen says whether any rename holds a window open on the node named, or on any node when none is named.
|
||||
func windowOpen(renames map[string]Rename, only string) bool {
|
||||
for _, r := range renames {
|
||||
for _, node := range r.Open() {
|
||||
if only == "" || only == node {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// closeWindows is Windows' rename half; the caller holds the lock.
|
||||
func (c *Config) closeWindows(only string) []string {
|
||||
renames, err := c.readRenames()
|
||||
if err != nil {
|
||||
return []string{"the renames cannot be read: " + err.Error()}
|
||||
}
|
||||
if c.Delivery == nil {
|
||||
return nil
|
||||
if c.Delivery == nil || !windowOpen(renames, only) {
|
||||
return nil // no window to judge: the delivery's word moves nothing, and no source is read for it (issue 1982)
|
||||
}
|
||||
s, err := c.sources()
|
||||
if err != nil {
|
||||
@@ -475,6 +487,20 @@ func (c *Config) RenameClose(module, to, node, why string) (any, error) {
|
||||
return map[string]any{"closed": node, "open": r.Open()}, nil
|
||||
}
|
||||
|
||||
// JudgeHeldWindows writes the finding of each window held open past HeldFinding, and clears one that is not, from the renames
|
||||
// kept: run at start and at the deadline the next window reaches (NextDeadline), so the finding comes in a quiet mesh too (lead
|
||||
// answer A-490l); it reads no source and asks nothing.
|
||||
func (c *Config) JudgeHeldWindows() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
renames, err := c.readRenames()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.sweepRenameFindings(&Sources{Renames: renames}, c.now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// sweepRenameFindings writes a finding for each window held open past HeldFinding, naming the node, and clears one
|
||||
// whose window closed.
|
||||
func (c *Config) sweepRenameFindings(s *Sources, now time.Time) {
|
||||
|
||||
@@ -140,10 +140,6 @@ type Sources struct {
|
||||
Unresolved []Unresolved
|
||||
Predefined map[string]Published
|
||||
Values map[string]Value
|
||||
// Pulls is what the controller told of each open pull request's declarations, and Kept the approved values held
|
||||
// for them (pulls.go); neither is resolved.
|
||||
Pulls map[string]PullDecl
|
||||
Kept map[string]KeptValue
|
||||
// Renames are the renames kept, by `<module>.<to>`, whose open windows answer the old name (renames.go).
|
||||
Renames map[string]Rename
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package main
|
||||
// A value no longer read (novox/hq ADR 0609, design 32 *A value no longer read*).
|
||||
//
|
||||
// A config value or secret that no manifest on any trunk reads any more is a **retiring value**. mesh-config learns it
|
||||
// from the indexer's tool `indexer_config_readers`, asked on each of the indexer's `manifest-accepted` (Retirements);
|
||||
// from the indexer's tool `indexer_config_readers`, asked for each module a `catalog-revision` names changed (Retirements);
|
||||
// a tool the indexer does not serve yet answers "not known" and nothing retires. A value is marked retiring only once no
|
||||
// node-engine reports applied a build that reads it (State draining until then, judged again on each `delivered`);
|
||||
// mesh-config then says `retiring` and asks the operator once, through the operator channel, whether to remove it. It is
|
||||
@@ -148,9 +148,15 @@ func (c *Config) Retirements(module string) []string {
|
||||
if err != nil {
|
||||
return []string{"the retirements cannot be read: " + err.Error()}
|
||||
}
|
||||
s, err := c.sources()
|
||||
if err != nil {
|
||||
return []string{"the retirements cannot be judged: " + err.Error()}
|
||||
// The sources are read only when a row is one to judge: a row already marked, or read again, needs none (issue 1982).
|
||||
var s Sources
|
||||
for _, row := range rows {
|
||||
if _, has := held[retiringKey(module, row.Key)]; (row.Module == "" || row.Module == module) && (len(row.Readers) == 0 && !has) {
|
||||
if s, err = c.sources(); err != nil {
|
||||
return []string{"the retirements cannot be judged: " + err.Error()}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
var said []string
|
||||
for _, row := range rows {
|
||||
|
||||
@@ -51,9 +51,6 @@ func (c *Config) sources() (Sources, error) {
|
||||
}); err != nil {
|
||||
return s, fmt.Errorf("the values cannot be read: %w", err)
|
||||
}
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return s, err
|
||||
}
|
||||
if renames, err := c.readRenames(); err != nil {
|
||||
return s, fmt.Errorf("the renames cannot be read: %w", err)
|
||||
} else {
|
||||
@@ -339,10 +336,8 @@ type Resolution struct {
|
||||
Moved []Moved `json:"moved"`
|
||||
Published string `json:"published,omitempty"`
|
||||
Withheld string `json:"withheld,omitempty"`
|
||||
// Declared are the modules whose `declared` key was written, and Candidates what the indexer's config candidates
|
||||
// did after the take-over.
|
||||
Declared []string `json:"declared,omitempty"`
|
||||
Candidates []string `json:"candidates,omitempty"`
|
||||
// Declared are the modules whose `declared` key was written.
|
||||
Declared []string `json:"declared,omitempty"`
|
||||
// Renamed is what the renames did in this pass.
|
||||
Renamed []string `json:"renamed,omitempty"`
|
||||
// SourceGone is said while the stored switch still names the controller, whose records are no longer read.
|
||||
@@ -383,9 +378,6 @@ func (c *Config) reconcileLocked() (Resolution, error) {
|
||||
} else if len(written) > 0 {
|
||||
r.Declared = written
|
||||
}
|
||||
if sw, serr := c.source(); serr == nil && sw.Source == SourceIndexer {
|
||||
r.Candidates = c.takeCandidates()
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ func ownSwitchTools(c *Config) []stdio.Tool {
|
||||
}},
|
||||
{Name: ResyncTool,
|
||||
Description: "Read every source again and write what differs: the published list of predefined keys, the " +
|
||||
"resolved values, declared, and the indexer's config candidates.",
|
||||
"resolved values and declared.",
|
||||
Run: func(map[string]any) (any, error) { return c.Resync() }},
|
||||
{Name: RenamesTool,
|
||||
Description: "Each config value renamed (renamed-from): from which name to which, when, the nodes its value " +
|
||||
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
// Config's records out of the controller (novox/hq ADR 0565 §3, §15, and the progressive insight of 2026-10-29):
|
||||
// mesh-config keeps each placed module's declarations in its own state `declared`, reads the list of predefined keys
|
||||
// from the indexer's `predefined` and a pull request's config from the indexer's `config-candidates`.
|
||||
// from the indexer's `predefined`. A pull request's config is not read: the mesh knows no pull request (design 47 G).
|
||||
//
|
||||
// **Its own sources hold always** (ADR 0590, issue 1126): the controller ended by its unassignment, so its pull-declares,
|
||||
// the take-over that switched away from them, compare and hand-back are gone, and the switch is the indexer's whatever
|
||||
@@ -23,12 +23,11 @@ const (
|
||||
// DeclaredState is each placed module's declarations at the trunk, one key per module (ADR 0565 §3).
|
||||
DeclaredState = "declared"
|
||||
|
||||
// IndexerPredefinedRead and CandidatesRead are the indexer's states (ADR 0565 §2).
|
||||
// IndexerPredefinedRead is the indexer's state of the predefined list (ADR 0565 §2).
|
||||
IndexerPredefinedRead = indexerModule + "." + catalog.StatePredefined
|
||||
CandidatesRead = indexerModule + "." + catalog.StateConfigCandidates
|
||||
)
|
||||
|
||||
// SourceIndexer is the one source of a pull request's config and of the predefined list: the indexer's.
|
||||
// SourceIndexer is the one source of the predefined list: the indexer's.
|
||||
const SourceIndexer = "indexer"
|
||||
|
||||
// Switch is which source holds: the indexer's, always, since the controller ended (ADR 0590).
|
||||
@@ -105,7 +104,7 @@ func (c *Config) writeDeclared(s Sources) ([]string, error) {
|
||||
return written, errors.Join(problems...)
|
||||
}
|
||||
|
||||
// ---- the predefined list and the pull requests from the indexer ------------------------------------------
|
||||
// ---- the predefined list from the indexer -----------------------------------------------------------------------------------------
|
||||
|
||||
// indexerList is the indexer's predefined list as the mirror's list holds one: the keys, and the commit.
|
||||
func (c *Config) indexerList() (PredefinedList, bool, error) {
|
||||
@@ -128,75 +127,6 @@ func (c *Config) indexerList() (PredefinedList, bool, error) {
|
||||
return list, true, nil
|
||||
}
|
||||
|
||||
// candidates is every open pull request's config declarations as the indexer keeps them, by pull key.
|
||||
func (c *Config) candidates() (map[string]PullDecl, error) {
|
||||
out := map[string]PullDecl{}
|
||||
err := c.each(CandidatesRead, func(key string, raw []byte) error {
|
||||
var cand catalog.ConfigCandidatesByPullValue
|
||||
if err := json.Unmarshal(raw, &cand); err != nil || cand.Pull == "" {
|
||||
return fmt.Errorf("the indexer's config candidate %s is not one", key)
|
||||
}
|
||||
p := PullDecl{Pull: cand.Pull, Head: cand.Head, Declares: map[string]map[string]Declaration{}, At: timeOf(cand.At)}
|
||||
for module, keys := range cand.Declares {
|
||||
raw, _ := json.Marshal(keys)
|
||||
var d map[string]Declaration
|
||||
if err := json.Unmarshal(raw, &d); err != nil {
|
||||
return fmt.Errorf("the indexer's config candidate %s declares %s unreadably", key, module)
|
||||
}
|
||||
for k, decl := range d {
|
||||
if !decl.HasDefault() {
|
||||
decl.Default = nil
|
||||
}
|
||||
d[k] = decl
|
||||
}
|
||||
p.Declares[module] = d
|
||||
}
|
||||
out[pullKey(cand.Pull)] = p
|
||||
return nil
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// takeCandidates is the indexer's config candidates taken in place of pull-declares, after the take-over: each one
|
||||
// whose head or declarations differ from what `pulls` holds is declared again, its held values judged again (ADR 0502
|
||||
// §6). The caller holds the lock.
|
||||
func (c *Config) takeCandidates() []string {
|
||||
cands, err := c.candidates()
|
||||
if err != nil {
|
||||
return []string{"the indexer's config candidates cannot be read: " + err.Error()}
|
||||
}
|
||||
var s Sources
|
||||
if err := c.readPulls(&s); err != nil {
|
||||
return []string{err.Error()}
|
||||
}
|
||||
var said []string
|
||||
for _, k := range sortedKeys(cands) {
|
||||
p := cands[k]
|
||||
if have, ok := s.Pulls[k]; ok && have.Head == p.Head && declaresDigest(have.Declares) == declaresDigest(p.Declares) {
|
||||
continue
|
||||
}
|
||||
a, err := c.declarePull(p.Pull, p.Head, p.Declares)
|
||||
if err != nil {
|
||||
said = append(said, fmt.Sprintf("%s from the indexer: %v", p.Pull, err))
|
||||
continue
|
||||
}
|
||||
said = append(said, a.Said+" (from the indexer)")
|
||||
}
|
||||
return said
|
||||
}
|
||||
|
||||
// declaresDigest is what a pull request's declarations hold a value to, in one encoding.
|
||||
func declaresDigest(d map[string]map[string]Declaration) string {
|
||||
flat := map[string]string{}
|
||||
for m, keys := range d {
|
||||
for k, decl := range keys {
|
||||
flat[m+"."+k] = declDigest(decl)
|
||||
}
|
||||
}
|
||||
raw, _ := json.Marshal(flat)
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
// Declared is the read of `declared`: every module's key, or one whole.
|
||||
func (c *Config) Declared(module string) (any, error) {
|
||||
if module != "" {
|
||||
@@ -221,20 +151,14 @@ func (c *Config) Declared(module string) (any, error) {
|
||||
return map[string]any{"declared": rows, "count": len(rows), "source": sw}, err
|
||||
}
|
||||
|
||||
// Resync reads every source again and writes what differs: the published list, the resolved values, `declared`, and
|
||||
// the indexer's config candidates (ADR 0565 §15).
|
||||
// Resync reads every source again and writes what differs: the published list, the resolved values and `declared`
|
||||
// (ADR 0565 §15).
|
||||
func (c *Config) Resync() (any, error) {
|
||||
r, err := c.Reconcile()
|
||||
if err != nil {
|
||||
return r, err
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
out := map[string]any{"resolution": r}
|
||||
if sw, err := c.source(); err == nil && sw.Source == SourceIndexer {
|
||||
out["candidates"] = c.takeCandidates()
|
||||
}
|
||||
return out, nil
|
||||
return map[string]any{"resolution": r}, nil
|
||||
}
|
||||
|
||||
// timeOf is an optional time of a seat's value as a time, the zero time when it is absent.
|
||||
|
||||
@@ -15,7 +15,7 @@ const (
|
||||
oldDeclared = `{"module":"old","settings":{"workers":{"kind":"preference","default":4,"why":"two per core"}}}`
|
||||
)
|
||||
|
||||
// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place and no open pull request.
|
||||
// anAgreeingMesh is aProposingMesh with the indexer's predefined list in place.
|
||||
func anAgreeingMesh(t *testing.T) (*Config, *memStore, *standIn) {
|
||||
t.Helper()
|
||||
c, m, r := aProposingMesh(t)
|
||||
@@ -34,19 +34,9 @@ func indexerList(t *testing.T, m *memStore, commit, list string) {
|
||||
m.mirror(t, IndexerPredefinedRead, catalog.PredefinedByListKey(), string(raw))
|
||||
}
|
||||
|
||||
// candidate is the indexer's config candidate of a pull request.
|
||||
func candidate(t *testing.T, m *memStore, pull, head, declares string) {
|
||||
t.Helper()
|
||||
key, err := catalog.ConfigCandidatesByPullKey(pullKey(pull)), error(nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.mirror(t, CandidatesRead, key, `{"v":1,"pull":"`+pull+`","head":"`+head+`","declares":`+declares+`}`)
|
||||
}
|
||||
|
||||
// ADR 0590 (issue 1126): config reads its own sources always, with no take-over: declared is written once per
|
||||
// manifest, pull-declares is refused naming the indexer's candidates, and the list and a pull request's config come from
|
||||
// the indexer. compare, take-over and hand-back went with the controller.
|
||||
// manifest, and the list comes from the indexer. compare, take-over, hand-back and a pull request's config went (the
|
||||
// mesh knows no pull request, design 47 G).
|
||||
func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
c, m, _ := anAgreeingMesh(t)
|
||||
steps := []struct {
|
||||
@@ -60,11 +50,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
again, _ := c.Reconcile()
|
||||
return strings.Join(first.Declared, ",") + "|" + strings.Join(again.Declared, ","), err
|
||||
}, "dunst|"},
|
||||
{"pull-declares refused, naming the indexer", func() (string, error) {
|
||||
_, err := c.PullDeclares(Call{Caller: ControllerCaller, Pull: "novox/mesh-catalog#8", Head: "h1",
|
||||
Declares: map[string]any{}})
|
||||
return "", err
|
||||
}, "config-candidates"},
|
||||
{"the list comes from the indexer", func() (string, error) {
|
||||
indexerList(t, m, "cccccccccccc", `{"font-size":{"type":"number","description":"text size","minimum":6,"maximum":72},
|
||||
"agent-managed-settings":{"type":"object","description":"managed settings","trusted":true},
|
||||
@@ -72,11 +57,6 @@ func TestConfigReadsItsOwnSourcesAlways(t *testing.T) {
|
||||
res, err := c.Reconcile()
|
||||
return res.Published, err
|
||||
}, "theme"},
|
||||
{"a pull request's config comes from the indexer", func() (string, error) {
|
||||
candidate(t, m, "novox/mesh-catalog#9", "h9", `{"dunst":{"gap":{"kind":"","type":"number","default":2}}}`)
|
||||
res, err := c.Reconcile()
|
||||
return strings.Join(res.Candidates, "; "), err
|
||||
}, "from the indexer"},
|
||||
}
|
||||
for _, s := range steps {
|
||||
got, err := s.do()
|
||||
|
||||
@@ -81,6 +81,12 @@ type player struct {
|
||||
catalog map[string]catalog.CatalogEntry
|
||||
byFp map[string]catalog.CatalogEntry
|
||||
catRev int64
|
||||
// calls counts what the module asked of the runtime, by method (and by key for an ask); puts are the keys it wrote, "<state>/<key>".
|
||||
calls map[string]int
|
||||
asks map[string]int // the asks of tools and seat verbs, by key
|
||||
puts []string
|
||||
// stop ends the module this player runs: the restart of a module is the old process gone and a new one started.
|
||||
stop func()
|
||||
}
|
||||
|
||||
// ownedState is one state of an owner (the indexer's modules, the assigner's wants...), kept from its bucket.
|
||||
@@ -232,6 +238,12 @@ func (p *player) handle(m wire) {
|
||||
Bucket string `json:"bucket"`
|
||||
}
|
||||
_ = json.Unmarshal(m.Params, &a)
|
||||
p.mu.Lock()
|
||||
p.calls[m.Method]++
|
||||
if strings.HasPrefix(m.Method, "mesh/state.") && m.Method != "mesh/state.get" && m.Method != "mesh/state.keys" && m.Method != "mesh/state.watch" {
|
||||
p.puts = append(p.puts, m.Method+" "+a.State+"/"+a.Key)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
kv, kvErr := p.kv(a.State)
|
||||
switch m.Method {
|
||||
case "mesh/state.get":
|
||||
@@ -357,6 +369,9 @@ func (p *player) handle(m wire) {
|
||||
Body json.RawMessage `json:"body"`
|
||||
}
|
||||
_ = json.Unmarshal(m.Params, &q)
|
||||
p.mu.Lock()
|
||||
p.asks[q.Key]++
|
||||
p.mu.Unlock()
|
||||
p.answer(m.ID, p.asked(q.Key, q.Body), nil)
|
||||
default:
|
||||
p.answer(m.ID, nil, errors.New("nats: no responders available for request"))
|
||||
@@ -675,14 +690,16 @@ func start(t *testing.T, bin string, js jetstream.JetStream, router *standIn, ha
|
||||
stdout, _ := cmd.StdoutPipe()
|
||||
cmd.Stderr = os.Stderr
|
||||
p := &player{t: t, ctx: ctx, js: js, router: router, in: stdin, pending: map[string]chan wire{}, haves: haves, readers: readers,
|
||||
owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{}}
|
||||
owned: map[string]*ownedState{}, catalog: map[string]catalog.CatalogEntry{}, byFp: map[string]catalog.CatalogEntry{},
|
||||
calls: map[string]int{}, asks: map[string]int{}}
|
||||
for _, f := range followed {
|
||||
p.own(f.Read)
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() })
|
||||
p.stop = func() { _ = stdin.Close(); cancel(); _ = cmd.Wait() }
|
||||
t.Cleanup(p.stop)
|
||||
go p.serve(stdout)
|
||||
t.Cleanup(cancel)
|
||||
return p
|
||||
@@ -708,9 +725,8 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buckets := []string{"mesh-delivery-assigner_assignments", "mesh-delivery-assigner_wants",
|
||||
"mesh-delivery-indexer_predefined",
|
||||
"mesh-delivery-indexer_config-candidates"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, PullsState, KeptState, DeclaredState,
|
||||
"mesh-delivery-indexer_predefined"}
|
||||
for _, s := range []string{ValuesState, ResolvedState, PredefinedState, ProposalsState, DeclaredState,
|
||||
RenamesState, RetiringState, FindingsState} {
|
||||
buckets = append(buckets, Module+"_"+s)
|
||||
}
|
||||
@@ -785,6 +801,7 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
|
||||
// The module restarted inside the window keeps it, and says renamed no more.
|
||||
said := p.publishedCount(EventRenamed)
|
||||
p.stop()
|
||||
p2 := start(t, bin, js, router, haves, nil)
|
||||
p2.viewsReady()
|
||||
if _, err := p2.call(ResyncTool, nil); err != nil {
|
||||
@@ -833,7 +850,7 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
p2.mu.Lock()
|
||||
p2.readers = []ReaderRow{{Module: "radio", Key: "band", Kind: KindValue, Readers: []string{}}}
|
||||
p2.mu.Unlock()
|
||||
p2.event("mesh-catalog.manifest-accepted", map[string]any{"module": "radio"})
|
||||
p2.event(catalog.Seat+"."+catalog.EventCatalogRevision+".main", map[string]any{"v": 1, "revision": p2.catRev, "kind": "trunk", "branch": "main", "changed": []string{"radio"}})
|
||||
eventually(t, "the retirement waits on g14", func() bool {
|
||||
raw, ok := p2.get(RetiringState, "radio.band")
|
||||
return ok && strings.Contains(string(raw), RetireDraining)
|
||||
@@ -869,6 +886,21 @@ func TestConfigWholeRenamesAndRetiresAgainstTheBus(t *testing.T) {
|
||||
p2.conforms(EventRetiring, &meshconfig.RetiringHolderEvent{}, meshconfig.HolderSubjectRetiring)
|
||||
p2.conforms(EventRetired, &meshconfig.RetiredHolderEvent{}, meshconfig.HolderSubjectRetired)
|
||||
eventually(t, "the retirement is dropped", func() bool { _, ok := p2.get(RetiringState, "radio.band"); return !ok })
|
||||
p.noGitAsk()
|
||||
p2.noGitAsk()
|
||||
}
|
||||
|
||||
// noGitAsk fails when the module asked the seat git for anything over the whole run (ADR 0681 clause 1: it calls no
|
||||
// `seat:git.pull`, and no other verb of the seat git is its).
|
||||
func (p *player) noGitAsk() {
|
||||
p.t.Helper()
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
for key, n := range p.asks {
|
||||
if strings.HasPrefix(key, "seat:git.") {
|
||||
p.t.Errorf("the module asked %s %d times: it holds no value for a pull request", key, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// streamCarries says whether the module's manifest names event among its own streams' events.
|
||||
|
||||
@@ -3,7 +3,6 @@ module mesh-config
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/go/clients/meshconfigclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/seats/meshconfig v0.0.0-00010101000000-000000000000
|
||||
|
||||
@@ -19,13 +19,9 @@
|
||||
"operator-channel"
|
||||
],
|
||||
"consumes": [
|
||||
"git.pull-merged",
|
||||
"git.pull-closed",
|
||||
"mesh-delivery-view.delivered",
|
||||
"node-engine.manifest-reads-changed",
|
||||
"mesh-catalog.manifest-accepted",
|
||||
"mesh-delivery-indexer.views.predefined",
|
||||
"mesh-delivery-indexer.views.config-candidates",
|
||||
"mesh-delivery-assigner.views.assignments",
|
||||
"mesh-delivery-assigner.views.wants",
|
||||
"mesh-catalog.catalog-revision",
|
||||
@@ -36,13 +32,11 @@
|
||||
"invokes": [
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
"seat:mesh-hand-acts.warranted",
|
||||
"seat:git.pull",
|
||||
"mesh-delivery-view.node_haves",
|
||||
"seat:node-engine.manifest-reads",
|
||||
"mesh-delivery-indexer.indexer_config_readers",
|
||||
"seat:mesh-vault.retire",
|
||||
"mesh-delivery-indexer.indexer_read_predefined",
|
||||
"mesh-delivery-indexer.indexer_read_config_candidates",
|
||||
"mesh-delivery-assigner.assigner_read_assignments",
|
||||
"mesh-delivery-assigner.assigner_read_wants",
|
||||
"seat:mesh-catalog.revision",
|
||||
@@ -66,8 +60,6 @@
|
||||
"resolved",
|
||||
"predefined",
|
||||
"proposals",
|
||||
"pulls",
|
||||
"kept",
|
||||
"declared",
|
||||
"renames",
|
||||
"retiring",
|
||||
@@ -132,5 +124,16 @@
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
}
|
||||
]
|
||||
],
|
||||
"config": {
|
||||
"deadline-floor-seconds": {
|
||||
"type": "number",
|
||||
"description": "the least wait, in seconds, of mesh-config's one deadline timer: it is set for the earliest real deadline it holds (a waiting proposal's end, an unanswered retirement's second ask, a rename window's seven-day held finding) and never sooner than this after a look, so a deadline passed and not settled is tried again, not spun on (lead answer A-490l, ADR 0681).",
|
||||
"minimum": 1,
|
||||
"maximum": 3600,
|
||||
"trusted": false,
|
||||
"default": 60,
|
||||
"runtime": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -54,6 +55,10 @@ type testBus struct {
|
||||
answers map[string][]byte
|
||||
// the gate's ledger `records`, as the tool runner keeps it: it outlives one run of the gate, as the bus's does.
|
||||
ledger *ledgerFake
|
||||
// registrations are what the seat mesh-catalog's verb registrations answers, and heads what the seat git's verb branch
|
||||
// answers, by `<repository>@<branch>` (the start pass and the seed read them, design 47 H §768).
|
||||
registrations []meshcatalog.Registration
|
||||
heads map[string]string
|
||||
}
|
||||
|
||||
func newTestBus(t *testing.T) *testBus {
|
||||
@@ -355,6 +360,22 @@ func (r *runtime) answer(m rpcMessage) {
|
||||
switch a.Key {
|
||||
case "":
|
||||
return nil, errors.New("an ask with no key")
|
||||
case "seat:" + meshcatalog.Seat + "." + meshcatalog.VerbRegistrations:
|
||||
r.b.mu.Lock()
|
||||
defer r.b.mu.Unlock()
|
||||
return toolAnswer(meshcatalog.RegistrationsAnswer{Registrations: append([]meshcatalog.Registration{}, r.b.registrations...)}), nil
|
||||
case "seat:git.branch":
|
||||
repository, _ := a.Body["repository"].(string)
|
||||
branch, _ := a.Body["branch"].(string)
|
||||
r.b.mu.Lock()
|
||||
defer r.b.mu.Unlock()
|
||||
head, ok := r.b.heads[repository+"@"+branch]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("the forge of this test holds no %s@%s", repository, branch)
|
||||
}
|
||||
return toolAnswer(map[string]any{"repository": repository, "branch": branch, "head": head}), nil
|
||||
case "seat:mesh-hand-acts.hand-act":
|
||||
return toolAnswer(map[string]any{"id": "hand-act-1"}), nil
|
||||
case "gitea.gitea_note_append", "gitea.gitea_delivery_view", "gitea.gitea_commit_status", "gitea.gitea_merge_pull_request",
|
||||
"gitea.gitea_add_comment":
|
||||
return toolAnswer(map[string]any{}), nil
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user