A local-first multi-agent app development platform, built on PenguinHarness
/**
* A Project's Benchmarks, read off every machine this server holds.
*
* A Benchmark belongs to the Project, and the Project is the same on each machine it is lent
* to. Its `benchmarks/` directory, though, is written where the Benchmark was created or
* evaluated — so one Benchmark's cases and scoreboard can be spread over as many disks as it
* has run on, while the Evaluation Center read only the disk it is served from.
*
* So the merge is BY IDENTITY, never by machine: one entry per benchmark id, one history of
* evaluations under it. The machine rides along as attribution — which disk an evaluation was
* read from, and where a Case's files have to be fetched from — and is not a grouping key.
*
* The same holds one level up, for the Agents themselves: an Agent that has only ever run on a
* machine has its state directory only over there, so a tree built from this server's Agent
* list has no row to hang its Benchmarks off (mergeAgents, below).
*
* Two rules are load-bearing:
*
* - **Order.** A scoreboard's append order IS its evaluation sequence, and the page preserves
* it rather than trusting timestamps. Two scoreboards on two disks share no append order at
* all, so a merged history can only be ordered by time — and a benchmark that came from a
* single machine is therefore left exactly as that file had it.
* - **First source wins the description.** The caller lists this server first, so a title, a
* `runs` count or a status read here is the one displayed. They describe the same Benchmark;
* where two disks disagree, the one the person is looking at is the honest choice.
*/
import type {
AgentSummary,
BenchmarkCaseSummary,
BenchmarkEvaluation,
BenchmarkSummary,
} from "@adelie/server/api";
/** One server's answer about a Project's Agents, with the machine that gave it. */
export interface AgentSource {
machineId: string | null;
agents: readonly AgentSummary[];
}
/** One Agent of a Project, and the machines that actually hold its state directory. */
export interface MergedAgent {
agent: AgentSummary;
/** Where this Agent exists, in the order the sources were asked; this server is null. */
machineIds: (string | null)[];
}
/**
* The Agents of a Project as every machine together has them.
*
* An Agent belongs to the Project, but its state directory is created on whichever machine it
* has run on — so an Agent that has only ever run on a machine exists only over there, and a
* page that lists this server's Agents cannot show it at all.
*
* The first source to name an Agent describes it (this server, when it has it): a name and a
* description read here are the ones the person is looking at. `machineIds` is what the caller
* asks about that Agent — asking a machine that has never heard of it earns a 404 per call.
*/
export function mergeAgents(sources: readonly AgentSource[]): MergedAgent[] {
const byId = new Map<string, MergedAgent>();
for (const source of sources) {
for (const agent of source.agents) {
const existing = byId.get(agent.agentId);
if (existing === undefined)
byId.set(agent.agentId, { agent, machineIds: [source.machineId] });
else existing.machineIds.push(source.machineId);
}
}
return [...byId.values()];
}
/** One server's answer about a Project's Benchmarks, with the machine that gave it. */
export interface BenchmarkSource {
/** The machine's own id; null for the server serving this page. */
machineId: string | null;
benchmarks: readonly BenchmarkSummary[];
}
/** An evaluation, and the machine whose scoreboard recorded it. */
export type MergedEvaluation = BenchmarkEvaluation & { machineId: string | null };
/** One Benchmark, as every machine together holds it. */
export interface MergedBenchmark extends Omit<BenchmarkSummary, "evaluations"> {
evaluations: MergedEvaluation[];
/**
* The machines holding a copy, in the order they were asked (this server first). More than
* one means the same benchmark was evaluated in more than one place — which is what makes
* an evaluation row's machine worth showing.
*/
machineIds: (string | null)[];
}
/** Newest last, by the timestamp the scoreboard recorded; entries without one keep their place. */
function byTime(a: MergedEvaluation, b: MergedEvaluation): number {
return (a.time ?? "").localeCompare(b.time ?? "");
}
/**
* Folds every machine's answer into one list of Benchmarks, in the order the sources were
* asked (so this server's benchmarks lead, and a machine-only one follows).
*/
export function mergeBenchmarks(sources: readonly BenchmarkSource[]): MergedBenchmark[] {
const byId = new Map<string, MergedBenchmark>();
for (const source of sources) {
for (const benchmark of source.benchmarks) {
const evaluations = benchmark.evaluations.map((evaluation) => ({
...evaluation,
machineId: source.machineId,
}));
const existing = byId.get(benchmark.id);
if (existing === undefined) {
byId.set(benchmark.id, {
...benchmark,
evaluations,
agentIds: [...benchmark.agentIds],
machineIds: [source.machineId],
});
continue;
}
existing.machineIds.push(source.machineId);
existing.evaluations.push(...evaluations);
// The Agents it has tested are those of every scoreboard, first-seen order kept.
for (const agentId of benchmark.agentIds) {
if (!existing.agentIds.includes(agentId)) existing.agentIds.push(agentId);
}
// The count of a directory nobody merged: the machines hold overlapping case sets, so
// the largest is the closest true statement available from the list endpoint alone.
existing.caseCount = Math.max(existing.caseCount, benchmark.caseCount);
}
}
const merged = [...byId.values()];
for (const benchmark of merged) {
// Only where two files were joined: a single scoreboard keeps the order it was written in.
if (benchmark.machineIds.length > 1) benchmark.evaluations.sort(byTime);
}
return merged;
}
/** One server's answer about a Benchmark's Cases. */
export interface CaseSource {
machineId: string | null;
cases: readonly BenchmarkCaseSummary[];
}
/** A Case, and the machine whose disk its files have to be read from. */
export type MergedCase = BenchmarkCaseSummary & { machineId: string | null };
/**
* The union of the Cases every machine holds for one Benchmark, first source winning a
* shared id. A Case's files are then fetched from the machine it is recorded against: they
* are two copies of one Case, and reading one of them is reading the Case.
*/
export function mergeBenchmarkCases(sources: readonly CaseSource[]): MergedCase[] {
const byId = new Map<string, MergedCase>();
for (const source of sources) {
for (const item of source.cases) {
if (byId.has(item.id)) continue;
byId.set(item.id, { ...item, machineId: source.machineId });
}
}
return [...byId.values()];
}