phase-jsonl-db.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // The jsonl-db optimization loop, condensed: the motivating example for phase markers
  2. // (docs/dynamic-workflow/presentation.md「The display contract」). Six markers turn a
  3. // dense step graph into preflight → attempt → gate → checkpoint/recover → wrap-up.
  4. //
  5. // Three things this fixture exists to pin: the shared `runBench` helper is claimed by
  6. // BOTH preflight and gate, so its site is copied once per phase; the gate's `continue`
  7. // exits mean the phases cannot be expressed as scope callbacks (which is why markers are
  8. // statements); and the loop closes with cross-phase carries rather than a container.
  9. interface Attempt {
  10. /** One sentence: the optimization increment this attempt implements. */
  11. approach: string;
  12. }
  13. interface Advice {
  14. /** 2-4 sentences: a different, concrete strategy for the next iteration. */
  15. strategy: string;
  16. }
  17. /** One bench run. Called from preflight (baseline) and from the gate (candidate). */
  18. async function runBench(): Promise<number> {
  19. const bench = await world.run("./target/release/bench", []);
  20. return bench.exitCode === 0 ? Number(bench.stdout.trim()) : Number.MAX_SAFE_INTEGER;
  21. }
  22. phase("preflight");
  23. const status = await world.run("git", ["status", "--porcelain"]);
  24. if (status.stdout.trim() !== "") throw new Error("working tree is not clean");
  25. const build = await world.run("cargo", ["build", "--release"]);
  26. if (build.exitCode !== 0) throw new Error(`baseline build failed:\n${build.stderr}`);
  27. let bestMs = await runBench();
  28. const optimizer = agent("optimizer", { system: "You make the engine faster." });
  29. const consultant = agent("consultant", { system: "You propose another angle." });
  30. let feedback = "Fresh run: take the biggest structural lever.";
  31. for (let iter = 0; iter < 4; iter++) {
  32. let accepted = false;
  33. for (let attempt = 0; attempt < 3 && !accepted; attempt++) {
  34. phase("attempt");
  35. const plan = await optimizer.ask<Attempt>(`Attempt ${attempt}. Feedback:\n${feedback}`);
  36. phase("gate");
  37. const tests = await world.run("cargo", ["test"]);
  38. if (tests.exitCode !== 0) {
  39. feedback = `"${plan.approach}" broke the tests:\n${tests.stderr}`;
  40. continue;
  41. }
  42. const candMs = await runBench();
  43. if (candMs < bestMs) {
  44. phase("checkpoint");
  45. const commit = await world.run("git", ["commit", "-am", `perf: ${plan.approach}`]);
  46. if (commit.exitCode !== 0) throw new Error(`checkpoint failed:\n${commit.stderr}`);
  47. bestMs = candMs;
  48. feedback = `Accepted "${plan.approach}" at ${bestMs} ms.`;
  49. accepted = true;
  50. } else {
  51. feedback = `"${plan.approach}" was not faster: ${candMs} vs ${bestMs} ms.`;
  52. }
  53. }
  54. if (accepted) {
  55. report({ bestMs, iter });
  56. } else {
  57. phase("recover");
  58. const rollback = await world.run("git", ["reset", "--hard", "HEAD"]);
  59. if (rollback.exitCode !== 0) throw new Error("rollback failed");
  60. const advice = await consultant.ask<Advice>(`Iteration ${iter} was rejected:\n${feedback}`);
  61. feedback = `Take a different angle: ${advice.strategy}`;
  62. }
  63. }
  64. phase("wrap-up");
  65. const head = await world.run("git", ["rev-parse", "HEAD"]);
  66. return { bestMs, head: head.stdout.trim() };