strand-deferred-recursive.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // A `.then` continuation that re-enters its own function: `refine` registers a continuation
  2. // which, when the answer is still too long, calls `refine` again.
  3. //
  4. // PINS A RESIDUAL, NOT A DESIRED RESULT. `inlineBody` cuts recursion by returning as soon as
  5. // `fnStack` already holds the declaration, and that return happens BEFORE the region is
  6. // opened and before the prologue would run. A deferred callback's prologue IS its receiver
  7. // barrier, so round 2's ask is never ordered before round 2's continuation: the whole second
  8. // round is one `jump recur`. The goldens below are what the analyzer does today; the fixture
  9. // exists so that a change to the recursion cut arrives as a reviewed diff, not as silence.
  10. //
  11. // What the core trace shows, in order (locations elided):
  12. //
  13. // region loop#1 loop parent=seq#1 entered recursive label="refine"
  14. // region call#1 call parent=loop#1 entered label="refine" <- NOT a strand: `refine`
  15. // is an ordinary function
  16. // that returns a promise
  17. // region choice#1 / branch#1 <- `.then` may not run
  18. // region loop#2 loop parent=branch#1 entered recursive label="then"
  19. // region call#2 call parent=loop#2 entered label="then" strand <- the continuation IS one,
  20. // and is itself in the
  21. // recursion SCC
  22. // issue ask#1 in=seq#1>loop#1>call#1
  23. // settle ask#1 in=seq#1>loop#1>call#1>choice#1>branch#1>loop#2>call#2 <- the prologue,
  24. // certain, ONCE
  25. // jump recur loop#1 in=…>call#2>choice#2>branch#2 <- round 2, and nothing else
  26. // settle ask#1 in=seq#1 joins=call#2
  27. //
  28. // Read the residual off the third-from-last line: there is exactly one prologue settle for a
  29. // recursion the script may run any number of times. The outer `await` joins `call#2` (the
  30. // strand spawned while its operand was evaluated), not `call#1`, and picks up the strand's
  31. // summary — which is why ask#1 settles on the main line at all.
  32. //
  33. // causality — one step with a `carry` self-edge (`ask#1 -> ask#1 carry always`) and the
  34. // `serial` cue: the rounds are the same site, one after another.
  35. function refine(t: string): PromiseLike<string> {
  36. return agent("r")
  37. .ask<string>(t)
  38. .then((r) => (r.length > 40 ? refine(r) : r));
  39. }
  40. return await refine("start");