handoff-earth-v5.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Hand-off graph acceptance sample (docs/dynamic-workflow/presentation.md「The display contract」):
  2. // the earth-v5 orchestration with prompts shortened. Five phases:
  3. // P1 architect alone; P2 harness + a five-member builder family (literal ROLES) and no
  4. // hand-offs; P3 workspace ⇄ fixer (back edge); P4 workspace → {fixer, jury-a/b/c},
  5. // juries → polisher (types JuryVerdict), polisher →(back) workspace, fixer →(back)
  6. // workspace; P5 a lone workspace card.
  7. interface Contract {
  8. namespace: string;
  9. signatureFeatures: string[];
  10. }
  11. interface BuildReport {
  12. builder: string;
  13. status: "done" | "blocked";
  14. }
  15. interface SceneScore {
  16. scene: string;
  17. score: number;
  18. }
  19. interface JuryVerdict {
  20. sceneScores: SceneScore[];
  21. }
  22. interface FixReport {
  23. summary: string;
  24. gatesGreenNow: boolean;
  25. }
  26. interface WorkflowReport {
  27. gatesGreen: boolean;
  28. visualGreen: boolean;
  29. signatureFeatures: string[];
  30. }
  31. const INTEG_ROUNDS = 4;
  32. const VISUAL_ROUNDS = 3;
  33. const VISUAL_PASS = 8.6;
  34. function consolidate(verdicts: JuryVerdict[]): { scene: string; median: number }[] {
  35. const rows: { scene: string; median: number }[] = [];
  36. for (const v of verdicts) for (const ss of v.sceneScores) rows.push({ scene: ss.scene, median: ss.score });
  37. return rows.sort((a, b) => a.median - b.median);
  38. }
  39. phase("总设计");
  40. const architect = agent("architect", { system: "总设计师" });
  41. const contract = await architect.ask<Contract>("冻结规格与契约");
  42. report({ stage: "contract", namespace: contract.namespace });
  43. phase("并行构建");
  44. const harness = agent("harness", { system: "测试基建工程师" });
  45. const ROLES = ["data", "astro", "gl", "ui", "app"];
  46. const parallelResults = await Promise.all([
  47. harness.ask<BuildReport>("写出三个工具"),
  48. ...ROLES.map((r) => agent(`builder-${r}`).ask<BuildReport>(`你是 builder-${r}`)),
  49. ]);
  50. report({ stage: "build", reports: parallelResults.map((b) => b.status) });
  51. phase("集成与门禁");
  52. const fixer = agent("fixer", { system: "集成与修复专家" });
  53. let gatesGreen = false;
  54. for (let round = 1; round <= INTEG_ROUNDS && !gatesGreen; round++) {
  55. const asm = await world.run("node", ["tools/assemble.js"]);
  56. if (asm.exitCode !== 0) {
  57. const fix = await fixer.ask<FixReport>(`assemble.js 失败\n${asm.stderr}`);
  58. report({ stage: `integ-${round}`, fixed: fix.gatesGreenNow });
  59. continue;
  60. }
  61. const acc = await world.run("node", ["tools/accept.js"]);
  62. gatesGreen = acc.exitCode === 0;
  63. if (!gatesGreen) {
  64. await fixer.ask<FixReport>(`accept.js 未过\n${acc.stdout}`);
  65. }
  66. }
  67. if (!gatesGreen) {
  68. const failed: WorkflowReport = { gatesGreen: false, visualGreen: false, signatureFeatures: contract.signatureFeatures };
  69. return failed;
  70. }
  71. phase("视觉评审");
  72. const juryPersona = { system: "视觉评审" } as const;
  73. const juryA = agent("jury-a", juryPersona);
  74. const juryB = agent("jury-b", juryPersona);
  75. const juryC = agent("jury-c", juryPersona);
  76. const polisher = agent("polisher", { system: "视觉精修师" });
  77. let rows: { scene: string; median: number }[] = [];
  78. let visualGreen = false;
  79. let scoredDirty = false;
  80. for (let round = 1; round <= VISUAL_ROUNDS; round++) {
  81. const shoot = await world.run("node", ["tools/shoot.js"]);
  82. if (shoot.exitCode !== 0) {
  83. await fixer.ask<FixReport>(`shoot.js 失败\n${shoot.stderr}`);
  84. continue;
  85. }
  86. const verdicts = await Promise.all([juryA, juryB, juryC].map((j) => j.ask<JuryVerdict>(`第 ${round} 轮视觉评审`)));
  87. rows = consolidate(verdicts);
  88. visualGreen = rows.length > 0 && rows.every((r) => r.median >= VISUAL_PASS);
  89. if (visualGreen) break;
  90. const below = rows.filter((r) => r.median < VISUAL_PASS);
  91. await polisher.ask<FixReport>(`视觉打磨 ${JSON.stringify(below)}`);
  92. scoredDirty = true;
  93. }
  94. if (scoredDirty) {
  95. const shoot = await world.run("node", ["tools/shoot.js"]);
  96. if (shoot.exitCode === 0) {
  97. const confirm = await juryA.ask<JuryVerdict>("终验确认");
  98. rows = consolidate([confirm]);
  99. visualGreen = rows.length > 0 && rows.every((r) => r.median >= VISUAL_PASS);
  100. }
  101. }
  102. phase("终验与交付");
  103. const finalAsm = await world.run("node", ["tools/assemble.js"]);
  104. const finalAcc = await world.run("node", ["tools/accept.js"]);
  105. const finalReport: WorkflowReport = {
  106. gatesGreen: finalAsm.exitCode === 0 && finalAcc.exitCode === 0,
  107. visualGreen,
  108. signatureFeatures: contract.signatureFeatures,
  109. };
  110. return finalReport;