Fork Horse repository, listed by its owner. Publisher snapshot.

packages/forkhorse-mcp/__tests__/generate-race-wire-fixture.mjs

Snapshot at 34e3175

Containing directory

4,717 bytes · javascriptView raw
1import { readFileSync, writeFileSync } from "node:fs";
2import { stripTypeScriptTypes } from "node:module";
3import { fileURLToPath } from "node:url";
4
5// Execute only the pure core wire/resume mappers against labeled in-memory storage.
6// No core Worker, provider, network, credential or live storage is involved.
7const coreSource = new URL("../../forkhorse-core/src/", import.meta.url);
8const moduleUrl = (source) =>
9  `data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
10function sourceModule(file, dependencies = {}) {
11  let source = stripTypeScriptTypes(readFileSync(new URL(file, coreSource), "utf8"), {
12    mode: "transform",
13  });
14  for (const [specifier, url] of Object.entries(dependencies))
15    source = source.replace(JSON.stringify(specifier), JSON.stringify(url));
16  return moduleUrl(source);
17}
18// Fixed pure-module graph: definitions only, no provider/Worker entrypoint imports.
19const httpModule = sourceModule("http.ts");
20const usageModule = sourceModule("usage.ts", { "./http": httpModule });
21const raceModule = sourceModule("race-entrants.ts", { "./usage": usageModule });
22const { raceStates } = await import(raceModule);
23const { publicPacket } = await import(
24  sourceModule("packets.ts", { "./race-entrants": raceModule })
25);
26const { resumeWork } = await import(
27  sourceModule("resume-work.ts", { "./race-entrants": raceModule })
28);
29const at = "2026-01-01T00:00:00.000Z";
30const head = "a".repeat(40);
31const canonicalHead = "b".repeat(40);
32const agents = ["builder", "second", "third"];
33const packet = {
34  id: "FH-race",
35  mode: "race",
36  tokenBudget: 1000,
37  title: "Core wire fixture",
38  goal: "Offline wire validation",
39  paths: ["src/"],
40  acceptance: ["Fixture only"],
41};
42const legacy = { ...packet, id: "FH-standard", mode: undefined, tokenBudget: undefined };
43const records = agents.map((agent) => ({
44  packet: packet.id,
45  agent,
46  repoName: `demo--${agent}-1`,
47  paths: packet.paths,
48  createdAt: at,
49  firstPushAt: at,
50  reviewRequestedAt: at,
51  receiptPassedAt: at,
52  approvedAt: at,
53  promotedAt: null,
54  place: null,
55  closedAt: null,
56  closedBy: null,
57  usage: {
58    reported: {
59      inputTokens: [800, 400, 1001][agents.indexOf(agent)],
60      outputTokens: 0,
61      model: "fixture-model",
62      source: "reported",
63      at,
64    },
65    metered: null,
66  },
67}));
68const review = {
69  id: "review-wire-fixture",
70  packet: packet.id,
71  agent: "builder",
72  repoName: "demo--builder-1",
73  sha: head,
74  base: canonicalHead,
75  createdAt: at,
76  stale: false,
77  promoted: false,
78  returned: null,
79  approval: { sha: head, receiptDigest: null },
80  receipt: { status: "passed" },
81};
82async function snapshot(finished) {
83  const entries = new Map([
84    [`packet:${packet.id}`, packet],
85    [`packet:${legacy.id}`, legacy],
86    [`review:${review.id}`, review],
87  ]);
88  for (const [index, record] of records.entries()) {
89    const entrant = finished
90      ? {
91          ...record,
92          place: [2, 1, 3][index],
93          closedAt: at,
94          closedBy: index === 2 ? "release" : "race-finished",
95          promotedAt: index === 1 ? at : null,
96          ...(index === 0 ? { promoteRejected: { at, expected: canonicalHead, found: head } } : {}),
97        }
98      : record;
99    entries.set(`entrant:${packet.id}:${record.agent}`, entrant);
100  }
101  if (finished)
102    entries.set(`race:${packet.id}`, {
103      winner: "second",
104      finishedAt: at,
105      ledgerEntryId: "ledger-wire-fixture-id",
106      places: [
107        { agent: "second", place: 1 },
108        { agent: "builder", place: 2 },
109        { agent: "third", place: 3 },
110      ],
111    });
112  const storage = {
113    async list({ prefix }) {
114      return new Map([...entries].filter(([key]) => key.startsWith(prefix)));
115    },
116  };
117  const packets = [legacy, packet].map(publicPacket);
118  return {
119    fixture: "Offline source-derived core race/resume mapper output; no live acceptance",
120    state: { packets, claims: [], races: await raceStates(storage, packets) },
121    me: {
122      work: await resumeWork(
123        storage,
124        "builder",
125        new Map([["demo--builder-1", head]]),
126        canonicalHead,
127        true,
128      ),
129    },
130  };
131}
132const content =
133  JSON.stringify({ open: await snapshot(false), finished: await snapshot(true) }, null, 2) + "\n";
134const destination = new URL("workerd/race-wire.fixture.json", import.meta.url);
135if (process.argv.includes("--check")) {
136  if (readFileSync(destination, "utf8") !== content)
137    throw new Error("Race wire fixture differs from current core mapper output.");
138  console.log("Offline race wire fixture matches current core mappers.");
139} else {
140  writeFileSync(destination, content);
141  console.log(`Generated ${fileURLToPath(destination)} from offline core mappers.`);
142}
143