C2: the forge module takes an act of the seat git once by its ask id
Each act (status, comment, view, note, merge-pull, tag) carrying an ask id is taken in the state taken, key git.<ask>, for the setting ask-taken-bound (10m, read from MESH_CONFIG_ASK_TAKEN_BOUND): a repeat while the act runs joins it; after it ended, the repeat answers what the forge holds of the act, read afresh (a merge call's pull requests merged with their merge commits), and the act is never done twice. An act that performed nothing drops its key, so the kept call's repeat acts. Builds: design 47 H2 §775, §820; H-A-381, H-A-400, H-A-359a
This commit is contained in:
@@ -68,6 +68,15 @@ export async function appendNote(runner: Runner, container: string, owner: strin
|
||||
return { added: true, lines: lines.length + 1 };
|
||||
}
|
||||
|
||||
/** Whether a commit's note holds a line carrying the act's id, read without writing (design 47 H2 §775: a repeat of a
|
||||
* taken ask answers what its act did). */
|
||||
export async function noteHolds(runner: Runner, container: string, owner: string, repo: string, commit: string,
|
||||
notesRef: string, id: string): Promise<boolean> {
|
||||
const shown = await runner("docker", noteArgs(container, owner, repo, commit, notesRef));
|
||||
if (shown.code !== 0 && shown.code !== 1) throw new Error(`the note on ${commit.slice(0, 8)} could not be read (git exited ${shown.code})`);
|
||||
return shown.code === 0 && shown.stdout.split("\n").some((l) => l.includes(`[${id}]`));
|
||||
}
|
||||
|
||||
/** The marker that makes one comment of a pull request the delivery's view. */
|
||||
export const VIEW_MARKER = "<!-- mesh-delivery:view -->";
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
import { emit } from "@novox/mesh-sdk/events";
|
||||
import { registerModuleTools } from "@novox/mesh-sdk/tools";
|
||||
import { statesAnnouncement } from "./kept.js";
|
||||
import { state } from "@novox/mesh-sdk/state";
|
||||
import { state, TAKEN_STATE } from "@novox/mesh-sdk/state";
|
||||
import { on } from "@novox/mesh-sdk/events";
|
||||
import { broker } from "@novox/mesh-sdk/messaging";
|
||||
import { STATUS_STATE, keepStatus, statusTools, viewWaitsFinding, type Follower, type Status } from "@novox/mesh-sdk/views";
|
||||
@@ -45,6 +45,7 @@ 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 { Once, askTakenBoundMs, takenState } from "./taken.js";
|
||||
import { Partials } from "./partials.js";
|
||||
import { callTools } from "./tools/calls.js";
|
||||
import { seatPublish } from "@novox/mesh-sdk/runtime";
|
||||
@@ -282,6 +283,10 @@ const heldHeads = new Heads();
|
||||
let reloadHeads: () => Promise<void> = async () => {};
|
||||
// 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}`));
|
||||
// Each act of the seat git is taken once by its ask id, the ids kept in the state taken for ask-taken-bound (taken.ts;
|
||||
// design 47 H2 §775).
|
||||
const once = new Once(takenState(state<unknown>(TAKEN_STATE), askTakenBoundMs(process.env, (line) => console.log(`[gitea] ${line}`))),
|
||||
(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) =>
|
||||
@@ -297,7 +302,7 @@ void failedSteps.expireSeen().catch((err) => console.log(`[gitea] the seen keys
|
||||
registerModuleTools("gitea", () => [eventsTool(pace), ...readAllTools(readAll), ...writeTools(writes), ...statusTools("gitea", "gitea_first_views", "gitea_first_view_again", followers, firstViews,
|
||||
async (act) => { await broker().request("seat:mesh-hand-acts.hand-act", act); }),
|
||||
...(gitea ? seatTools(gitea, { run, container: FORGE_CONTAINER, registered: () => registrations.written, judge: (head) => failedSteps.judge(head),
|
||||
watched: () => registrations.watched, calls, say: seatPublish, partials }) : []),
|
||||
watched: () => registrations.watched, calls, say: seatPublish, partials, once }) : []),
|
||||
...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); })]);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ export const KEPT: readonly { readonly name: string; readonly readable?: boolean
|
||||
{ name: "failed-steps" },
|
||||
{ name: "first-views" },
|
||||
{ name: "idle" },
|
||||
{ name: "taken" },
|
||||
{ name: "transfers" },
|
||||
];
|
||||
|
||||
|
||||
@@ -237,6 +237,13 @@
|
||||
"default": [
|
||||
"jschoubben"
|
||||
]
|
||||
},
|
||||
"ask-taken-bound": {
|
||||
"type": "string",
|
||||
"description": "How long the forge module keeps the id of an act of the seat git it took, as a duration such as 10m (design 47 H2 §775): an act given again with that id within it acts not again but answers what the forge holds of it. The largest caller's bound, the pipeline runner's ask-unanswered-bound, by default.",
|
||||
"trusted": false,
|
||||
"runtime": true,
|
||||
"default": "10m"
|
||||
}
|
||||
},
|
||||
"facts": {
|
||||
@@ -635,6 +642,11 @@
|
||||
{
|
||||
"name": "calls",
|
||||
"history": 1
|
||||
},
|
||||
{
|
||||
"name": "taken",
|
||||
"key-ttl": true,
|
||||
"max-mib": 8
|
||||
}
|
||||
],
|
||||
"reads": [
|
||||
|
||||
+94
-16
@@ -27,11 +27,13 @@ import { containedFrom, newestByContext } from "./checks.js";
|
||||
import { Refused, checkName } from "./guard.js";
|
||||
import { AdminRefused } from "./token.js";
|
||||
import { awayAnswer } from "@novox/mesh-sdk/callers";
|
||||
import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js";
|
||||
import { appendNote, noteHolds, viewBody, viewComment, type Runner } from "./delivery.js";
|
||||
import type { Judged } from "./failedsteps.js";
|
||||
import { mergeCall } from "./mergecall.js";
|
||||
import { mergeCall, membersOf } from "./mergecall.js";
|
||||
import type { Calls, Say } from "./calls.js";
|
||||
import type { Partials } from "./partials.js";
|
||||
import type { Once } from "./taken.js";
|
||||
import { askIdOf } from "@novox/mesh-sdk/callers";
|
||||
|
||||
/** The verbs of the seat, in its definition's order. */
|
||||
export const VERBS: readonly string[] = Object.values(GIT_VERBS);
|
||||
@@ -60,6 +62,8 @@ export interface SeatDeps {
|
||||
say?: Say;
|
||||
/** The findings merge-partial (partials.ts; design 47 H §767). */
|
||||
partials?: Partials;
|
||||
/** Takes each act once by its ask id (taken.ts; design 47 H2 §775): absent, an act acts at every call. */
|
||||
once?: Once;
|
||||
}
|
||||
|
||||
/** A failure as the contract says it: the forge's or this holder's refusal, or a forge that failed. */
|
||||
@@ -171,18 +175,24 @@ async function mergingOf(gitea: GiteaClient, owner: string, repo: string, p: Git
|
||||
|
||||
/** The seat's verbs as tools `git.<verb>`, each answering in the seat's contract. */
|
||||
export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[] {
|
||||
const verb = (name: string, description: string, act: (args: Record<string, unknown>) => Promise<object>): ToolDefinition => ({
|
||||
const answered = async (args: Record<string, unknown>, act: (args: Record<string, unknown>) => Promise<object>): Promise<object> => {
|
||||
try {
|
||||
return await act(args);
|
||||
} catch (err) {
|
||||
return failureOf(gitea, err);
|
||||
}
|
||||
};
|
||||
// An act given `standing`, what it did read afresh, is taken once by its ask id (§775, H-A-381, H-A-400).
|
||||
const verb = (name: string, description: string, act: (args: Record<string, unknown>) => Promise<object>,
|
||||
standing?: (args: Record<string, unknown>) => Promise<object>): ToolDefinition => ({
|
||||
name: `${GIT}.${name}`,
|
||||
description,
|
||||
input: {},
|
||||
run: async (args: Record<string, unknown>) => {
|
||||
try {
|
||||
return await act(args);
|
||||
} catch (err) {
|
||||
return failureOf(gitea, err);
|
||||
}
|
||||
},
|
||||
run: async (args: Record<string, unknown>) => standing && deps.once
|
||||
? deps.once.run(askIdOf(args), () => answered(args, act), () => answered(args, standing))
|
||||
: answered(args, act),
|
||||
});
|
||||
const s = standingActs(gitea, deps);
|
||||
|
||||
return [
|
||||
verb(GIT_VERBS.pull, "The seat git's pull: one pull request by its repository and number or by its merge commit, and for an open one what the forge would merge now.",
|
||||
@@ -273,7 +283,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
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 };
|
||||
}),
|
||||
}, s.status),
|
||||
verb(GIT_VERBS.comment, "The seat git's comment: an act of the forge bridge, a comment on a pull request carrying the act's id; an id it already holds is held.",
|
||||
async (args): Promise<CommentAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -282,7 +292,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (holds(await gitea.listComments(owner, repo, number), id)) return { id, held: true, performed: true };
|
||||
await gitea.addComment(owner, repo, number, `${String(args.text ?? "")}\n\n${actMarker(id)}`);
|
||||
return { id, performed: true };
|
||||
}),
|
||||
}, s.comment),
|
||||
verb(GIT_VERBS.view, "The seat git's view: an act of the forge bridge, the delivery's one view comment kept on a pull request; an id it already holds is held.",
|
||||
async (args): Promise<ViewAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -294,7 +304,7 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (existing) await gitea.editComment(owner, repo, existing.id, body);
|
||||
else await gitea.addComment(owner, repo, number, body);
|
||||
return { id, performed: true };
|
||||
}),
|
||||
}, s.view),
|
||||
verb(GIT_VERBS.note, "The seat git's note: an act of the forge bridge, one line on a commit's note under refs/notes/mesh-plan carrying the act's id; an id it already holds is held.",
|
||||
async (args): Promise<NoteAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
@@ -302,9 +312,9 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
const commit = commitOf("commit", args.commit);
|
||||
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 };
|
||||
}),
|
||||
}, s.note),
|
||||
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)),
|
||||
async (args): Promise<MergePullAnswer> => mergeCall(gitea, args, deps), s.mergePull),
|
||||
verb(GIT_VERBS.tag, "The seat git's tag: the git tag <directory>/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);
|
||||
@@ -316,10 +326,78 @@ export function seatTools(gitea: GiteaClient, deps: SeatDeps): ToolDefinition[]
|
||||
if (at !== null) throw new Refused(`${tag} is at ${at.slice(0, 12)} already, not at ${commit.slice(0, 12)}; a tag is never moved`);
|
||||
await gitea.createTag(owner, repo, tag, commit);
|
||||
return { tag, performed: true };
|
||||
}),
|
||||
}, s.tag),
|
||||
];
|
||||
}
|
||||
|
||||
function holds(comments: GiteaComment[], id: string): boolean {
|
||||
return comments.some((c) => String(c.body ?? "").includes(actMarker(id)));
|
||||
}
|
||||
|
||||
/**
|
||||
* What each act did, read afresh from the forge, for an ask given again after its act ended (design 47 H2 §775; H-A-381
|
||||
* (1)): the act is never performed twice, and what the forge holds is what the act answers when done. A repeat for which
|
||||
* the forge holds nothing of the act is a refusal naming it.
|
||||
*/
|
||||
function standingActs(gitea: GiteaClient, deps: SeatDeps) {
|
||||
const never = (ask: string, what: string) => new Refused(`the ask ${ask} was taken and its act acted, but ${what}: answered as it stands, never acted again`);
|
||||
const askOf = (args: Record<string, unknown>) => askIdOf(args);
|
||||
return {
|
||||
status: async (args: Record<string, unknown>): Promise<StatusAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const context = String(args.context ?? ""), state = String(args.state ?? "");
|
||||
const now = newestByContext((await gitea.combinedStatus(owner, repo, commit)).statuses).find((x) => x.context === context);
|
||||
if (!now || now.state !== state) throw never(askOf(args), `${commit.slice(0, 12)} carries no ${context} ${state}`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
comment: async (args: Record<string, unknown>): Promise<CommentAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const number = numberOf(args.number);
|
||||
if (!holds(await gitea.listComments(owner, repo, number), id)) throw never(askOf(args), `${full}#${number} holds no comment of the act ${id}`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
view: async (args: Record<string, unknown>): Promise<ViewAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo, full } = repositoryOf(args.repository);
|
||||
const number = numberOf(args.number);
|
||||
const existing = viewComment(await gitea.listComments(owner, repo, number));
|
||||
if (!existing?.body.includes(actMarker(id))) throw never(askOf(args), `${full}#${number}'s view is not the act ${id}'s`);
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
note: async (args: Record<string, unknown>): Promise<NoteAnswer> => {
|
||||
const id = idOf(args.id);
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const commit = commitOf("commit", args.commit);
|
||||
if (!(await noteHolds(deps.run, deps.container, owner, repo, commit, "mesh-plan", id))) {
|
||||
throw never(askOf(args), `the note on ${commit.slice(0, 12)} holds no line of the act ${id}`);
|
||||
}
|
||||
return { id, held: true, performed: true };
|
||||
},
|
||||
mergePull: async (args: Record<string, unknown>): Promise<MergePullAnswer> => {
|
||||
// Each member as the forge holds it: merged, with its merge commit; the call that merged them, when one is kept.
|
||||
const members = membersOf(args.pulls);
|
||||
const out = await Promise.all(members.map(async (m) => {
|
||||
const p = await gitea.getPullRequest(m.owner, m.repo, m.number);
|
||||
return p.merged && p.merge_commit_sha
|
||||
? { repository: m.repository, number: m.number, state: "merged" as const, commit: p.merge_commit_sha }
|
||||
: { repository: m.repository, number: m.number, state: "refused" as const, why: `not merged: the forge holds it ${p.state === "open" ? "open" : "closed"}` };
|
||||
}));
|
||||
if (!out.some((m) => m.state === "merged")) throw never(askOf(args), "the forge merged none of its pull requests");
|
||||
const named = (c: { members: { repository: string; number: number; state: string }[] }) =>
|
||||
out.every((m) => m.state !== "merged" || c.members.some((k) => k.repository === m.repository && k.number === m.number && k.state === "merged"));
|
||||
const kept = (await deps.calls?.all())?.find(named);
|
||||
return { ...(kept ? { call: kept.call } : {}), members: out };
|
||||
},
|
||||
tag: async (args: Record<string, unknown>): Promise<TagAnswer> => {
|
||||
const { owner, repo } = repositoryOf(args.repository);
|
||||
const tag = String(args.tag ?? "");
|
||||
const commit = commitOf("commit", args.commit);
|
||||
const at = await gitea.tagCommit(owner, repo, tag);
|
||||
if (at !== commit) throw never(askOf(args), at === null ? `the forge holds no tag ${tag}` : `${tag} is at ${at.slice(0, 12)}, not ${commit.slice(0, 12)}`);
|
||||
return { tag, performed: true };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
// **The forge module takes an act of the seat git once by its ask id** (novox/hq design 47 H2 §775; H-A-381, H-A-400): a
|
||||
// kept call carries its ask id (the runtime names it in the call's `_meta`, mesh-sdk askIdOf), and the forge module keeps
|
||||
// each id it took in its state `taken`, key `git.<ask>`, for `ask-taken-bound` (a setting, default 10 minutes, the
|
||||
// largest caller's bound). An ask given again while its act runs joins that act and gets its answer; given again after
|
||||
// the act ended, it answers the act's state as it stands, read afresh from the forge (a merge call answers each pull
|
||||
// request as it is: merged, with its merge commit), and the act is never performed twice. No answer is kept in `taken`.
|
||||
// The key is kept only for an act that acted: an answer that performed nothing (away, a transport fault, a refusal
|
||||
// before any act) drops it, so the kept call's repeat under the same id acts (H-A-400 (1)). A repeat for which the
|
||||
// forge's state says the act that acted never happened is a refusal naming it.
|
||||
|
||||
import { takeOnce, takenKey, TAKEN_STATE, type State } from "@novox/mesh-sdk/state";
|
||||
|
||||
/** The seat the forge module takes asks of. */
|
||||
export const SEAT = "git";
|
||||
|
||||
/** ask-taken-bound's default (H-A-381 (2)). */
|
||||
export const ASK_TAKEN_BOUND_DEFAULT = "10m";
|
||||
|
||||
/** A duration as the mesh writes one ("10m", "1h30m", "45s", "500ms"), in milliseconds; null when it is none. */
|
||||
export function durationMs(text: string): number | null {
|
||||
const s = text.trim();
|
||||
if (!/^(\d+(\.\d+)?(ms|h|m|s))+$/.test(s)) return null;
|
||||
let ms = 0;
|
||||
for (const [, n, , unit] of s.matchAll(/(\d+(\.\d+)?)(ms|h|m|s)/g)) {
|
||||
ms += Number(n) * ({ ms: 1, s: 1_000, m: 60_000, h: 3_600_000 } as Record<string, number>)[unit];
|
||||
}
|
||||
return ms > 0 ? ms : null;
|
||||
}
|
||||
|
||||
/** ask-taken-bound in milliseconds, from the word the node-engine composes for it (H-A-370, H-A-400 (2)); the default when
|
||||
* it is unset. A value that is no duration is said and the default taken. */
|
||||
export function askTakenBoundMs(env: NodeJS.ProcessEnv = process.env, log: (line: string) => void = () => {}): number {
|
||||
const raw = env.MESH_CONFIG_ASK_TAKEN_BOUND;
|
||||
const fallback = durationMs(ASK_TAKEN_BOUND_DEFAULT)!;
|
||||
if (raw === undefined || raw.trim() === "") return fallback;
|
||||
const ms = durationMs(raw);
|
||||
if (ms === null) {
|
||||
log(`the setting ask-taken-bound is ${JSON.stringify(raw)}, no duration: the default ${ASK_TAKEN_BOUND_DEFAULT} holds`);
|
||||
return fallback;
|
||||
}
|
||||
return ms;
|
||||
}
|
||||
|
||||
/** The state taken, as the forge module reaches it: take an ask once, drop a key whose act performed nothing. */
|
||||
export interface Taken {
|
||||
/** Whether this holder takes the ask now (mesh-sdk takeOnce: created, refused when the key exists). */
|
||||
take(ask: string): Promise<boolean>;
|
||||
/** Drops the key of an ask whose act performed nothing, so its repeat acts. */
|
||||
drop(ask: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** The module's state taken over the runtime, its keys living ask-taken-bound. */
|
||||
export function takenState(st: Pick<State<unknown>, "delete">, boundMs: number): Taken {
|
||||
return {
|
||||
take: (ask) => takeOnce(SEAT, ask, boundMs, TAKEN_STATE),
|
||||
drop: (ask) => st.delete(takenKey(SEAT, ask)),
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether an act's answer performed nothing: an error answer, or a merge call that merged no member. */
|
||||
export function performedNothing(answer: object): boolean {
|
||||
const a = answer as Record<string, unknown>;
|
||||
if (a["error-kind"] !== undefined) return true;
|
||||
if (Array.isArray(a.members)) return !a.members.some((m: any) => m?.state === "merged");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes an act once by its ask id: `act` runs for an ask this holder takes now; an ask given while its act runs joins it;
|
||||
* an ask taken before whose act ended answers `standing`, the act's state read afresh. An act with no ask id acts as
|
||||
* before. The taken key is dropped when the act performed nothing.
|
||||
*/
|
||||
export class Once {
|
||||
private readonly running = new Map<string, Promise<object>>();
|
||||
|
||||
constructor(private readonly taken: Taken, private readonly log: (line: string) => void = () => {}) {}
|
||||
|
||||
async run(ask: string, act: () => Promise<object>, standing: () => Promise<object>): Promise<object> {
|
||||
if (!ask) return act();
|
||||
const joined = this.running.get(ask);
|
||||
if (joined) return joined;
|
||||
if (!(await this.taken.take(ask))) {
|
||||
// Taken before, and its act ended (or another process runs it): what it did, read afresh, never done again.
|
||||
return this.running.get(ask) ?? standing();
|
||||
}
|
||||
const acting = (async () => {
|
||||
let answer: object;
|
||||
try {
|
||||
answer = await act();
|
||||
} catch (err) {
|
||||
await this.taken.drop(ask).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
if (performedNothing(answer)) {
|
||||
await this.taken.drop(ask).catch((err) =>
|
||||
this.log(`the ask ${ask} performed nothing and its taken key could not be dropped, so its repeat is answered as it stands — ${err instanceof Error ? err.message : err}`));
|
||||
}
|
||||
return answer;
|
||||
})();
|
||||
this.running.set(ask, acting);
|
||||
try {
|
||||
return await acting;
|
||||
} finally {
|
||||
this.running.delete(ask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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: "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" }] });
|
||||
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: "taken" }, { 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: "taken" }, { name: "transfers" }] });
|
||||
});
|
||||
|
||||
test("index.ts says the announcement as the event `states` before the tools are served", () => {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
// 47 H2 §775, H-A-381, H-A-400: the forge module takes an act of the seat git once by its ask id. Given the same ask id
|
||||
// twice it acts once: a repeat while the act runs joins it, a repeat after it ended answers what the forge holds of the
|
||||
// act; an act that performed nothing (away, a refusal before any merge) drops its key, so the repeat acts.
|
||||
test("an act of the seat git given the same ask id twice acts once", async () => {
|
||||
const { seatTools } = await import("../dist/seat.js");
|
||||
const { Once, durationMs, askTakenBoundMs } = await import("../dist/taken.js");
|
||||
const { runAs } = await import("@novox/mesh-sdk/callers");
|
||||
const SHA = "a".repeat(40), MERGE = "c".repeat(40);
|
||||
const keys = new Set<string>();
|
||||
const taken = { take: async (ask: string) => (keys.has(ask) ? false : (keys.add(ask), true)), drop: async (ask: string) => { keys.delete(ask); } };
|
||||
let merges = 0, comments: { id: number; body: string }[] = [], added = 0, away = false;
|
||||
const pr = () => ({ number: 9, state: merges ? "closed" : "open", merged: merges > 0, head: "feat/x", head_sha: SHA, base: "main", ...(merges ? { merge_commit_sha: MERGE } : {}) });
|
||||
let release: () => void = () => {}, entered: () => void = () => {};
|
||||
const merging = new Promise<void>((r) => { entered = r; });
|
||||
const forge: Record<string, any> = {
|
||||
forgeVersion: async () => (away ? { ok: false, words: "no answer", store: true } : { ok: true, version: "1.27.3" }),
|
||||
getPullRequest: async () => { if (away) throw new Error("Gitea API /repos/novox/app/pulls/9: 503 Service Unavailable"); return pr(); },
|
||||
mergePullRequest: async () => { const held = new Promise<void>((r) => { release = r; }); entered(); await held; merges++; },
|
||||
commitParents: async () => [SHA, SHA],
|
||||
listComments: async () => comments,
|
||||
addComment: async (_o: string, _r: string, _n: number, body: string) => { added++; comments.push({ id: added, body }); },
|
||||
};
|
||||
const seat = seatTools(forge as any, { run: (async () => ({ stdout: "", code: 1 })) as any, container: "gitea", registered: () => true,
|
||||
judge: async () => null, watched: () => new Map([["novox/app", new Set(["main"])]]), once: new Once(taken) });
|
||||
const call = (verb: string, ask: string, args: Record<string, unknown>) =>
|
||||
runAs({ "mesh/caller": "ace.x", "mesh/ask": ask }, args, () => seat.find((x: any) => x.name === `git.${verb}`)!.run(args)) as Promise<any>;
|
||||
const merge = { by: "lead@shanks", pulls: [{ repository: "novox/app", number: 9, head: SHA }] };
|
||||
|
||||
// Away first: nothing performed, the key dropped, so the kept call's repeat under the same id acts (H-A-400 (1)).
|
||||
away = true;
|
||||
assert.equal((await call("merge-pull", "ask1", merge))["error-kind"], "away");
|
||||
assert.equal(keys.has("ask1"), false, "an act that performed nothing keeps no taken key");
|
||||
away = false;
|
||||
|
||||
// The repeat while the first runs joins it: one merge, both answered the same.
|
||||
const first = call("merge-pull", "ask1", merge);
|
||||
await merging;
|
||||
const joined = call("merge-pull", "ask1", merge);
|
||||
release();
|
||||
const [a, b] = await Promise.all([first, joined]);
|
||||
assert.equal(merges, 1, "a repeat while the act runs joins it");
|
||||
assert.deepEqual(b, a);
|
||||
assert.equal(a.members[0].state, "merged");
|
||||
|
||||
// After it ended, the repeat answers the pull request as the forge holds it, merged with its merge commit, and merges nothing.
|
||||
const again = await call("merge-pull", "ask1", merge);
|
||||
assert.deepEqual([merges, again.members], [1, [{ repository: "novox/app", number: 9, state: "merged", commit: MERGE }]]);
|
||||
|
||||
// A comment given its ask again is not written twice; a repeat whose comment the forge does not hold is refused, naming it.
|
||||
const comment = { id: "c1", repository: "novox/app", number: 9, text: "hi" };
|
||||
assert.equal((await call("comment", "ask2", comment)).performed, true);
|
||||
assert.deepEqual([(await call("comment", "ask2", comment)).held, added], [true, 1]);
|
||||
comments = [];
|
||||
const gone = await call("comment", "ask2", comment);
|
||||
assert.deepEqual([gone["error-kind"], /ask2 was taken/.test(gone.error), added], ["refused", true, 1]);
|
||||
|
||||
// ask-taken-bound is read from the word the node-engine composes (H-A-400 (2)), 10 minutes by default.
|
||||
assert.deepEqual([askTakenBoundMs({}), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "90s" }), askTakenBoundMs({ MESH_CONFIG_ASK_TAKEN_BOUND: "soon" }), durationMs("1h30m")],
|
||||
[600_000, 90_000, 600_000, 5_400_000]);
|
||||
});
|
||||
Reference in New Issue
Block a user