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

packages/forkhorse-gatekeeper/test/local/model-fixture.ts

Snapshot at 34e3175

Containing directory

2,904 bytes · typescriptView raw
1// Deterministic protocol fixture, not an AI model. It emits only the local S7 tool sequence.
2import type { ServerResponse } from "node:http";
3let step = 0;
4export function modelFixture(body: Buffer, res: ServerResponse, head: string): void {
5  const request = JSON.parse(body.toString());
6  const agent = request.tools?.some((tool: { name: string }) => tool.name === "createWorktree");
7  let name: string | undefined, input: Record<string, unknown> = {};
8  if (agent) {
9    const transcript = JSON.stringify(request.messages);
10    const committed = [...transcript.matchAll(/LOCAL_COMMIT=([a-f0-9]{40})/g)].at(-1)?.[1];
11    const code = (value: string) => { name = "executeCode"; input = { code: `export default async function(self, env, ctx) { ${value} }` }; };
12    switch (step++) {
13      case 0: code('console.log(JSON.stringify(await env.FORK.getHead()));'); break;
14      case 1: name = "createWorktree"; input = { title: "Synthetic S7 worktree", bindingName: "REPO", commitId: head }; break;
15      case 2: code('console.log("BEFORE=" + await env.REPO.readFile("README.md")); await env.REPO.writeFile("README.md", "After full run-local approval\\n"); console.log("LOCAL_COMMIT=" + await env.REPO.commit("Synthetic full OS acceptance"));'); break;
16      case 3:
17        if (!committed) throw new Error("Synthetic model fixture did not receive a worktree commit");
18        code(`await env.FORK.push("main", "${committed}"); console.log("LOCAL_APPROVAL_QUEUED");`); break;
19    }
20    console.log(`LOCAL MODEL FIXTURE step=${step} tool=${name ?? "complete"}`);
21  }
22  const content = name ? { type: "tool_use", id: `fixture_tool_${step}`, name, input } : { type: "text", text: "Synthetic local acceptance complete. No inference was performed." };
23  const message = { id: `fixture_${step}`, type: "message", role: "assistant", model: request.model,
24    content: [content], stop_reason: name ? "tool_use" : "end_turn", stop_sequence: null, usage: { input_tokens: 100, output_tokens: 50 } };
25  if (!request.stream) { res.setHeader("content-type", "application/json"); res.end(JSON.stringify(message)); return; }
26  res.setHeader("content-type", "text/event-stream");
27  const event = (type: string, data: object) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`);
28  event("message_start", { message: { ...message, content: [], stop_reason: null } });
29  event("content_block_start", { index: 0, content_block: name ? { ...content, input: {} } : { type: "text", text: "" } });
30  event("content_block_delta", { index: 0, delta: name ? { type: "input_json_delta", partial_json: JSON.stringify(input) } : { type: "text_delta", text: (content as { text: string }).text } });
31  event("content_block_stop", { index: 0 });
32  event("message_delta", { delta: { stop_reason: message.stop_reason, stop_sequence: null }, usage: { output_tokens: 50 } });
33  event("message_stop", {}); res.end();
34}
35