Skip to content

perf(core): locate journal segments by sequence and load them in bounded batches - #4085

Draft
chcodex wants to merge 2 commits into
agentscope-ai:mainfrom
chcodex:fix/session-log-bulk-load
Draft

chcodex wants to merge 2 commits into
agentscope-ai:mainfrom
chcodex:fix/session-log-bulk-load

Conversation

@chcodex

@chcodex chcodex commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem

JournalSessionLog.scan(after, through) always walked the parent chain from head down to after, and fetched commit segments one by one. The cost grew with session length: on a 445-segment journal, an AG-UI replay from an arbitrary afterSequence spent 1.6–10 s just locating the range, and memory use grew with the number of segments touched.

Change

  • Sequence-named segments. Commit segment names are now commits/<20-digit zero-padded firstSeq>.json, so lexicographic key order is sequence order.
  • Key-based locating. scan(after, through) lists the journal's commit keys once, binary-searches the segment covering after, and reads only the first, last, and one anchor segment to validate the boundaries. Segment contents are not bulk-read just to find the range.
  • Bounded streaming. The iterator loads commit segments in readAll batches of at most 256, validates firstSeq, lastSeq, and parent per segment as it streams, and keeps memory bounded to one batch instead of the whole journal.
  • Full replay uses locating too. after == 0 also goes through key locating instead of bulk-loading every segment. The old bulk load remains only as a fallback when locating is impossible.
  • Legacy compatibility. Journals containing legacy <sha256(batchId)>.json segments fall back to the previous parent-chain walk. Any key that cannot be parsed as a sequence name triggers the same fallback.

Measured on a 445-segment session: after=0 1.78 s, after=1000 1.65 s, after=5000 0.68 s; locating takes ~30 ms and peak memory is bounded to 256 segments.

Tests

JournalSessionLogTest covers sequence-named locating, mid-segment cursors, boundaries (0, head.seq(), > head.seq()), mixed legacy/sequence journals, unparseable keys, and the bounded batch path.

mvn -o -pl agentscope-core -Dtest=JournalSessionLogTest test
# Tests run: 16, Failures: 0, Errors: 0, Skipped: 0

Spotless: mvn -o -pl agentscope-core spotless:check — clean.

Fixes #4083

Merge order

JournalSessionLog.scan unconditionally called loadCommitSegments(), so every incremental read (after > 0, e.g. the AG-UI connect poll every 200ms) listed and read every commit segment in the journal even though only the head -> after suffix is needed.

Bulk pre-read is now limited to a full replay (after == 0); incremental reads fall back to per-segment reads via committedSegment(id, null). The commit walk and event filtering are unchanged.

Tests: add a counting AtomicSessionStorage and assert incremental scans never call listPaths/readAll, full replays still do, and both paths return the same event sequence.
@chcodex
chcodex force-pushed the fix/session-log-bulk-load branch from 9eddef6 to 194ae14 Compare November 9, 2026 11:47
@chcodex
chcodex marked this pull request as draft November 10, 2026 02:26
@jujn jujn added the P2 重要不紧急 label Nov 10, 2026
@chcodex chcodex changed the title perf(core): skip journal bulk-load on incremental session-log reads perf(core): locate journal segments by sequence and load them in bounded batches Nov 10, 2026
…ded batches

Commit segments are named <20-digit firstSeq>.json, so the lexicographic key order of commits/ equals the sequence order. scan(after, through) lists the session's commit keys once, binary-searches the segment covering after, and validates only the boundary segments; the iterator then reads 256 segments at a time and checks firstSeq/lastSeq/parent while streaming, so heap use no longer grows with the journal size. after == 0 takes the same path instead of bulk-loading every segment, and journals with legacy <sha256>.json keys still fall back to the parent-chain walk.
@chcodex
chcodex force-pushed the fix/session-log-bulk-load branch from 2a32525 to b1ce0d5 Compare November 10, 2026 10:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants