cloud / scripts / cloud-rehearsal / ask-agent.tsmain at aacbc74
// ask-agent.ts — THE FAKE'S AUTHOR: a coding agent, run headless in the rehearsal's folder. It
// sets a scenario's world up at the start, and writes the rule for a call nobody has a rule for.
// It never answers a call itself: what it leaves behind is world.json and rules.mjs, which the
// server answers from. The folder holds what it needs to read (RULES.md, reference/).
import { spawn } from "node:child_process";
import { closeSync, openSync } from "node:fs";
import type { Miss } from "./fake-cloudflare.ts";
/** The agents that can be the fake's author, each headless with the prompt on its stdin and
* edits allowed in the folder it runs in. start.ts takes one by name, or a whole command line. */
export const AGENTS: Record<string, string[]> = {
claude: [
"claude",
"-p",
"--model",
"sonnet",
"--permission-mode",
"acceptEdits",
"--allowedTools",
"Read,Edit,Write,Glob,Grep,WebFetch,WebSearch",
],
codex: ["codex", "exec", "--skip-git-repo-check", "--sandbox", "workspace-write", "-"],
};
/** Runs the agent's command in `runDir` with `prompt` on its stdin, until it exits. */
export function runAgent(input: {
command: string[];
runDir: string;
prompt: string;
logFile: string;
timeoutMs: number;
}) {
return new Promise<void>((resolve, reject) => {
const [program = "", ...args] = input.command;
const log = openSync(input.logFile, "a");
const child = spawn(program, args, { cwd: input.runDir, stdio: ["pipe", log, log] });
const timer = setTimeout(() => child.kill("SIGTERM"), input.timeoutMs);
child.on("error", reject);
child.on("close", (code) => {
clearTimeout(timer);
closeSync(log);
if (code === 0) resolve();
else
reject(new Error(`the agent's command ended with ${String(code)}: see ${input.logFile}`));
});
if (!child.stdin) throw new Error("the agent's command has no stdin to take the prompt");
child.stdin.end(input.prompt);
});
}
const SHARED = `You are the author of a fake Cloudflare, for one rehearsal of iterate's sign-up chat. An AI agent walks a person through installing an "iterate mind" in their Cloudflare account, and in the rehearsal every call it makes to Cloudflare, and every page of Cloudflare's dashboard that the person opens, is answered by a small server from two files in this folder:
- world.json: the state. Every wait that has begun is the instant it ends (ISO 8601), so an answer is a comparison with the clock.
- rules.mjs: this run's own rules, tried before the shared ones.
Read RULES.md first: it says how a rule is written, with examples. reference/rules.ts has the world's schema, and reference/base-rules.ts the shared rules (read them, do not edit them). run.jsonl is every call so far and its answer.
Rules for you:
- Never answer a call yourself. Leave a rule in rules.mjs, and state in world.json, so that the server answers this call and the same call later.
- Be Cloudflare: its envelope, its field names, its error codes and messages. When the scenario does not say, choose the dullest realistic answer.
- Stay consistent with world.json and with what run.jsonl shows was already answered.
- Change nothing outside this folder, and do not edit reference/ or run.jsonl.
- End with one line that says what you added.`;
/** The prompt that turns a scenario into the world a rehearsal starts from. */
export function scenarioPrompt(input: { scenario: string; now: string }) {
return `${SHARED}
YOUR TASK NOW: set up this rehearsal's start. world.json holds the default start (reference/rules.ts \`startingWorld\`). Change it so that it is the start of this scenario:
"""
${input.scenario}
"""
It is ${input.now} now, and the rehearsal starts in about a minute. Change what the scenario speaks of: the accounts, their cards and plans, the domains, the waits (in seconds, under \`waits\`), the registrar. Leave the rest as it is. When the scenario asks for behaviour that no field of the world expresses, add a rule to rules.mjs and the state it needs to world.json. Append one line to \`notes\` in world.json that says what you set up.`;
}
/** The prompt for a call that nobody has a rule for. */
export function missPrompt(input: { miss: Miss; scenario: string; now: string }) {
const { miss } = input;
const what = miss.path.startsWith("/client/v4/")
? `a call to Cloudflare's API. What Cloudflare's own API spec says about its route:\n\n${miss.aboutTheRoute}`
: "a page of Cloudflare's dashboard (or another address that is not its API). Answer it as a small page with `call.page`, with a button for what a person does there, as the shared rules' pages do.";
return `${SHARED}
The scenario of this rehearsal:
"""
${input.scenario}
"""
YOUR TASK NOW: the server got a call that no rule answers. It is ${input.now}.
${miss.method} ${miss.path}${miss.query}
body: ${JSON.stringify(miss.body)}
It is ${what}
Add the rule for it to rules.mjs. The caller is waiting, so be quick: one rule, and no more of the world than it needs.`;
}