agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

perf(core): avoid bulk-loading every journal segment on incremental session-log reads

Open chcodex opened this issue 3 weeks ago • 1 comments

Problem

JournalSessionLog.scan(after, through) always calls loadCommitSegments(), which lists every commits/* object and bulk-reads all of their values. This happens for every readAfter(...) call.

AG-UI /agui/connect follows the session log and calls readAfter(cursor, 256) every 200 ms. When the cursor is near the head only a few events are new, but the current code still reads the whole journal's commit payloads from the store (on the JDBC store: SELECT item_key, value_json, version ... WHERE item_key IN (...)).

On long conversations this turns a small tail read into repeated full-journal reads.

Expected

Bulk loading is only worth it when replaying from the beginning. For incremental reads (after > 0), only the segments between head and after are needed; committedSegment(id, null) already falls back to a per-segment read.

Proposed change

// Only a full replay (after == 0) is worth a bulk load; incremental reads
// should read segments on demand so polling does not re-read the whole journal.
Map<String, AtomicSessionStorage.Value> loadedSegments =
        after == 0 ? loadCommitSegments() : null;

Acceptance

  • scan(after > 0, through) does not call listPaths / readAll.
  • scan(0, through) keeps the bulk path.
  • The emitted event sequence is identical on both paths.
  • Covered by JournalSessionLogTest.

chcodex avatar Nov 09 '26 09:11 chcodex

Implementation proposed in #4085: bulk pre-read is now limited to after == 0, and incremental reads use the existing per-segment read fallback. Tests cover the no-list/no-readAll assertion, the bulk path, and identical event sequences.

chcodex avatar Nov 09 '26 10:11 chcodex