import { chmod, readFile, writeFile } from "node:fs/promises";
import { makeState, runPromise } from "./t3.ts";
export async function seedRuntimeOnly(t3: { project(name: string): Promise<string> }, home: string, port?: number) {
  const binary = `${home}/idle-codex-replay.cjs`;
  await writeFile(binary, await readFile(new URL("./idle-codex-replay.cjs", import.meta.url)));
  await chmod(binary, 0o755);
  const settingsPath = `${home}/userdata/settings.json`;
  const settings = JSON.parse(await readFile(settingsPath, "utf8"));
  settings.providerInstances.codex = { driver: "codex", enabled: true, config: { binaryPath: binary, homePath: `${home}/synthetic-codex-home` } };
  await writeFile(settingsPath, JSON.stringify(settings));
  const project = await t3.project("Runtime demo");
  const { cli } = makeState(home, port === undefined ? {} : { T3CODE_PORT: String(port) });
  const result = await runPromise(cli(["thread", "start", project, "Complete this synthetic turn", "--title", "Runtime-only error", "--provider", "codex", "--model", "gpt-5.4", "--wait", "--json"], [0,1,2]));
  if (!result.stdout.trim()) throw new Error(result.stderr);
  const thread = JSON.parse(result.stdout);
  return { project, thread, cli, crash: () => writeFile(`${project}/.crash-idle`, "Synthetic idle transport exit\n") };
}
