fault-matrix-scripts.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // ============================================================
  2. // 故障矩阵的并行度轴:八个脚本形状(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Verification」)
  3. // ============================================================
  4. // 所有脚本共用同一个返回类型 `Out { note: string }` 与同一种 persona,只有形状不同。
  5. // 每个 ask 都是**自己的子代理实例**(`agent(\`w-${i}\`)` 写在 map 回调里),否则 fan-out
  6. // 会退化成一个共享会话上的串行 ask(authoring skill §7 的那条纪律)。
  7. // `parallel` 在 dwf 里就是「不 await 直到 Promise.all」;`pipeline` 是两级 Promise.all,
  8. // 第二级以第一级的结果为输入(扇入)。
  9. export type ScriptShape = "W1" | "W2" | "W4" | "W8" | "W16" | "S-pipe" | "S-nest" | "S-phase";
  10. export interface ShapeSpec {
  11. script: string;
  12. /** 同时在飞的子代理数上限(由脚本形状决定)。 */
  13. parallelism: number;
  14. /** 整个 run 创建的子代理数。 */
  15. subagents: number;
  16. /** ask 站点实例总数(= 子代理数:每个子代理恰好一次 ask)。 */
  17. asks: number;
  18. }
  19. const HEADER = ["interface Out { note: string }", 'const PERSONA = "You are a test worker.";'].join(
  20. "\n",
  21. );
  22. /** 平铺 fan-out:宽度 n。 */
  23. function flat(n: number): string {
  24. if (n === 1) {
  25. return [
  26. HEADER,
  27. 'const out = await agent("w-1", PERSONA).ask<Out>("Do task 1.");',
  28. "return out.note;",
  29. ].join("\n");
  30. }
  31. return [
  32. HEADER,
  33. `const items = [${Array.from({ length: n }, (_, i) => i + 1).join(", ")}];`,
  34. "const outs = await Promise.all(",
  35. " items.map((i) => agent(`w-${i}`, PERSONA).ask<Out>(`Do task ${i}.`)),",
  36. ");",
  37. "return outs.map((o) => o.note);",
  38. ].join("\n");
  39. }
  40. const PIPE = [
  41. HEADER,
  42. "const first = await Promise.all(",
  43. " [1, 2, 3, 4].map((i) => agent(`s1-${i}`, PERSONA).ask<Out>(`Stage one, task ${i}.`)),",
  44. ");",
  45. "const second = await Promise.all(",
  46. " first.map((o, i) => agent(`s2-${i + 1}`, PERSONA).ask<Out>(`Stage two on: ${o.note}`)),",
  47. ");",
  48. "return second.map((o) => o.note);",
  49. ].join("\n");
  50. const NEST = [
  51. HEADER,
  52. "const groups = await Promise.all(",
  53. " [1, 2, 3].map((g) =>",
  54. " Promise.all(",
  55. " [1, 2, 3].map((i) => agent(`g${g}-w${i}`, PERSONA).ask<Out>(`Group ${g}, task ${i}.`)),",
  56. " ),",
  57. " ),",
  58. ");",
  59. "return groups.flat().map((o) => o.note);",
  60. ].join("\n");
  61. const PHASE = [
  62. HEADER,
  63. 'phase("Stage A");',
  64. "const a = await Promise.all(",
  65. " [1, 2, 3, 4].map((i) => agent(`a-${i}`, PERSONA).ask<Out>(`Stage A, task ${i}.`)),",
  66. ");",
  67. 'phase("Gate");',
  68. 'const gate = await agent("gate", PERSONA).ask<Out>(`Gate on ${a.length} results.`);',
  69. 'phase("Stage B");',
  70. "const b = await Promise.all(",
  71. " [1, 2, 3, 4].map((i) => agent(`b-${i}`, PERSONA).ask<Out>(`Stage B, task ${i}, after ${gate.note}.`)),",
  72. ");",
  73. "return b.map((o) => o.note);",
  74. ].join("\n");
  75. export const FAULT_MATRIX_SHAPES: Record<ScriptShape, ShapeSpec> = {
  76. W1: { script: flat(1), parallelism: 1, subagents: 1, asks: 1 },
  77. W2: { script: flat(2), parallelism: 2, subagents: 2, asks: 2 },
  78. W4: { script: flat(4), parallelism: 4, subagents: 4, asks: 4 },
  79. W8: { script: flat(8), parallelism: 8, subagents: 8, asks: 8 },
  80. W16: { script: flat(16), parallelism: 16, subagents: 16, asks: 16 },
  81. "S-pipe": { script: PIPE, parallelism: 4, subagents: 8, asks: 8 },
  82. "S-nest": { script: NEST, parallelism: 9, subagents: 9, asks: 9 },
  83. "S-phase": { script: PHASE, parallelism: 4, subagents: 9, asks: 9 },
  84. };
  85. export const FAULT_MATRIX_SHAPE_IDS = Object.keys(FAULT_MATRIX_SHAPES) as ScriptShape[];