strand-fanout-two-phases-join.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // The reference shape of the strands refactor. Two phases each fan out `async` work and
  2. // neither awaits it, so B's marker is reached while A's subagents are still running; one
  3. // barrier then joins both fan-outs, and C fans out over their answers. A ∥ B → C.
  4. //
  5. // Under the old single settled set the `await` inside A's callback settled ask#1 for the
  6. // WHOLE script, so `ask#1 -> ask#2 seq` appeared and the control flow threaded A → B → C on
  7. // one line — byte-identical to strand-fanout-awaited-per-phase, which really is sequential.
  8. // A fan-out with an `async` literal callback is a strand: its awaits suspend the element's
  9. // activation, not the spawner.
  10. //
  11. // Expected:
  12. // core — `region fanout#1 … strand` and `region fanout#2 … strand`; each callback's own
  13. // `settle ask#N in=seq#1>fanout#N`; and ONE certain barrier on the main line,
  14. // `settle ask#1,ask#2 in=seq#1 joins=fanout#1,fanout#2`.
  15. // causality — NO edge of any kind between ask#1 and ask#2 (concurrency is
  16. // incomparability in the partial order, so it needs no ink), both `-> ask#3 data`, and
  17. // the forward phase edges are `phase#1 -> phase#3` and `phase#2 -> phase#3` only (the
  18. // `carry` self-edges every may-repeat fan-out raises are not forward edges).
  19. // cfg — `phase#1@1 -> phase#2@1 next` (the spawner walked marker to marker),
  20. // `ask#1@1 -> phase#3@1 join via=return`, `ask#2@1 -> phase#3@1 join via=return`, and
  21. // `phase phase#2 "B" alongside=phase#1`: B was entered beside A, which is a node fact,
  22. // not a transfer of control.
  23. const ids = [1, 2, 3];
  24. phase("A");
  25. const aWork = ids.map(async (i) => {
  26. const r = await agent(`a-${i}`).ask<string>(`survey ${i}`);
  27. return r;
  28. });
  29. phase("B");
  30. const bWork = ids.map(async (i) => {
  31. const r = await agent(`b-${i}`).ask<string>(`probe ${i}`);
  32. return r;
  33. });
  34. const [a, b] = await Promise.all([Promise.all(aWork), Promise.all(bWork)]);
  35. phase("C");
  36. const merged = await Promise.all(
  37. ids.map(async (i) => {
  38. const r = await agent(`c-${i}`).ask<string>(`${a.join(",")} / ${b.join(",")} #${i}`);
  39. return r;
  40. }),
  41. );
  42. return merged;