; Fieldtest series-step1.5 (LEAK REPRO) — owned Series threaded through a
; tail-recursive stream loop is not dropped, leaking the RawBuf slab.
;
; This is the minimal form of a leak that surfaces on the CANONICAL
; streaming pattern the whitepaper itself uses (design/models/0007 §Series,
; the SMA worked example, and examples/series_sma.ail): a `run`-style fn
; takes (own (con Series ...)) plus a worklist, and on each element does
; (Series.push s v) then tail-recurses; the base case returns Unit.
;
; Under AILANG_RC_STATS the program reports `live > 0` (here live=4):
; the intermediate Series values produced by push, and/or the final Series
; alive at the base-case arm, are never freed. The committed reference
; example examples/series_sma.ail exhibits the same leak (live=8), as do
; series-step1_1_rolling_max.ail (live=8) and
; series-step1_3_warmup_rolling_sum.ail (live=8).
;
; The leak is NOT present when the owned Series is RETURNED from the
; recursion (ret own Series) and dropped at the final caller's scope close
; (see series-step1_2_last_n_events.ail, which is live=0): the trigger is
; an owned Series reaching a base case whose return type is not Series and
; that does not explicitly consume it.
;
; This fixture is the smallest reproduction: push two Ints into a size-3
; Series via tail recursion, base case prints "done".
;
; Expected stdout: "done".
; Observed under AILANG_RC_STATS: `ailang_rc_stats: allocs=7 frees=3 live=4`.

(module series-step1_5_drop_leak_repro

  (data IntList
    (ctor INil)
    (ctor ICons (con Int) (con IntList)))

  (fn run
    (type (fn-type
      (params (own (con Series (con Int))) (own (con IntList)))
      (ret (own (con Unit))) (effects IO)))
    (params s input)
    (body
      (match input
        (case (pat-ctor INil) (do io/print_str "done\n"))
        (case (pat-ctor ICons v rest)
          (let s_new (app Series.push s v)
            (tail-app run s_new rest))))))

  (fn main
    (type (fn-type (params) (ret (own (con Unit))) (effects IO)))
    (params)
    (body
      (let s (new Series (con Int) 3)
        (app run s
          (term-ctor IntList ICons 1
          (term-ctor IntList ICons 2
          (term-ctor IntList INil))))))))
