flue icon indicating copy to clipboard operation
flue copied to clipboard

Fold checkpoints stop advancing when turns are short, so every load re-folds an ever-growing suffix

Open cgaravitoq opened this issue 2 months ago • 0 comments

Summary

On @flue/runtime 2.0.3 (the code is unchanged in 2.1.1 and 2.2.0-next.1), a conversation's fold checkpoint only advances inside SqlAgentExecutionStore's append path. There, batchesSinceFoldCheckpoint is a per-writer counter with a threshold of 64. The writer is recreated per turn, and a typical turn appends about 12 batches, so the threshold is reached only by rare turns that append 64 or more batches.

Nothing else writes a checkpoint. loadReducedConversationState folds the whole suffix after the last checkpoint on every load, in pages of limit: 1e3, and then discards the progress.

Impact we measured (Cloudflare Durable Objects)

  • One production conversation stream: 5712 batches, 90.5 MB. Several batches are 200-670 KB because they carry tool results.
  • Checkpoint head_offset stuck at 3660 while the head was at 5712, so every dispatch re-folded about 2050 batches (about 60 MB of JSON).
  • The isolate's P99 memory reached 208 MB against the 128 MB limit. Every dispatch then failed with Internal error in Durable Object storage caused object to be reset on the first write after the fold (INSERT INTO cf_agents_runs in _runFiberInternal). The object stayed wedged: each retry appended a few more batches and made the suffix longer.
  • We reproduced it end to end by cloning that object's SQLite into another object and sending one turn.

Proposed fix

  1. Checkpoint from the loader: in loadReducedConversationState, count the batches folded since the seed checkpoint. Write a fold checkpoint every N batches during the catch-up and once at the end when the count passes a small threshold. A long suffix is then folded once, not on every load.
  2. Bound the loader's page size: read in pages of about 64 batches, not 1000, so a single page cannot hold tens of MB of batch JSON plus its parsed records.

Optionally, the append-path counter could be seeded from the distance between the head and the last checkpoint instead of starting at 0 per writer.

We run this as a local bun patch of the runtime today and can share the diff. Related: #658 (the stream itself grows without bound).

cgaravitoq avatar Sep 28 '26 16:09 cgaravitoq