reduce-accumulator.ts 1.2 KB

1234567891011121314151617181920
  1. // reduce chaining through the accumulator (fix #3): each iteration asks with the
  2. // running accumulator interpolated into the instructions. The accumulator carries
  3. // the PRIOR iteration's ask result, so the ask's own artifact flows back into its
  4. // own instructions across iterations -> a data SELF-edge ask#1 -> ask#1. Before the
  5. // fix the accumulator param never received the callback's return taint (the per-pass
  6. // callbackReturn was fresh at bind time), so the ask -> ask chain was dropped. The
  7. // reduce body reaches an ask, so the candidate also promotes to a fan-out.
  8. //
  9. // Expected edges:
  10. // source -> world-read#1 (items drive the loop)
  11. // world-read#1 -> fan-out#1 (iterated collection into the fan-out)
  12. // world-read#1 -> ask#1, fan-out#1 -> ask#1 (the item element into the ask)
  13. // ask#1 -> ask#1 (accumulator self-chain)
  14. // world-read#1 -> sink, fan-out#1 -> sink, ask#1 -> sink (summary reaches return)
  15. const items = await files.glob("*.md");
  16. const summary = await items.reduce(
  17. async (acc, item) => agent("summarizer").ask<string>(`${await acc} then ${item}`),
  18. Promise.resolve(""),
  19. );
  20. return summary;