Skip to content

perf(core): replay each session prefix once for every session view - #3823

Closed
wahllllll wants to merge 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/issue-3816-single-pass-pending
Closed

wahllllll wants to merge 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/issue-3816-single-pass-pending

Conversation

@wahllllll

@wahllllll wahllllll commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

#3816 reports that a settled session becomes slow to continue. Two independent sources of read amplification sit on that path, both replaying the same committed prefix:

  1. SessionInteractions.pending(SessionLog) paged the journal with readAfter(cursor, 256), and JournalSessionLog.readAfter() starts a fresh commit-chain scan() for every page. For a 15k-event journal that is roughly 60 full chain traversals per call.
  2. A turn start needed the turn view, pending interactions, the recovery projection and the transcript at the same prefix, and each built itself from its own full replay. SessionRecorder's constructor replayed three times and SessionExecution.prepare twice more, so a turn start cost seven to nine independent passes.

Measured against a counting storage layer on a 500-commit journal, one replay costs 1002 storage reads (2.004 per commit: scan() reads each commit once to walk the parent chain and once inside readCommit).

Change

First commit — pending() scans the stable prefix once. pending() captures head.seq() once and traverses log.scan(0, upper) a single time instead of paging.

Second commit — every view shares one replay. New SessionPrefix performs one pass over the committed prefix and feeds one accumulator per view (turns, pending interactions, projection, transcript, plus the run/turn guards the recorder needs). The four read paths build from that pass, and SessionRecorder hands its prefix to the execution that follows, so the constructor and prepare replay once instead of five times.

Behaviour is unchanged: the stable-prefix boundary is still captured once, interaction/requested/interaction/resolved semantics are untouched, and the recorder still rejects a reused run id and an already-completed turn from the same event set. No storage SPI, journal format, chunk persistence, checkpoint payload, or retention behaviour is modified.

The rest of #3816 is deliberately out of scope: the 2× per-commit read inside scan() (see #3673, which touches the same method) and per-read SQLite connection cost are separate changes.

Regression

  • SessionInteractionsTest.pendingScansTheStablePrefixOnceInsteadOfPagingFromHead drives a 600-event log through a counting SessionLog and asserts one head(), one scan() and zero paged readAfter() restarts. On unmodified main it fails with expected: <1> but was: <0>.
  • SessionPrefixTest.oneReplayFeedsEveryViewAtThePrefix seeds a 200-commit journal on a counting storage layer, asserts the shared prefix costs no more than a single view (four views cost 1616 reads separately, the shared prefix 402), and asserts every view reports the same content as the individual reads.

Verification

  • mvn -B -pl agentscope-core -Dtest=io.agentscope.core.session.*Test,ToolCallInterceptorTest test: 36 tests, 0 failures.
  • mvn -B -pl agentscope-harness -am -Dtest=io.agentscope.harness.agent.session.*Test test: 17 tests, 0 failures.
  • mvn -B -pl agentscope-core test: 2832 tests, 0 failures, 2 errors — only the pre-existing DangerousPathBypassTest symlink cases that need Windows symlink privileges unavailable on this machine (they also fail on unmodified main).
  • mvn -B -pl agentscope-core spotless:check: BUILD SUCCESS.

A shared prefix replay cannot by itself bring a large journal to interactive latency: scan() still reads and hashes each commit twice, and Windows backends pay per read. That is the next step discussed in #3816, and it should land near #3673 rather than on top of these two commits.

@wahllllll

Copy link
Copy Markdown
Contributor Author

Overlap note for reviewers: #3673 (perf(session): reuse scoped reads within journal scans, @chengqiangrd) optimizes the callee side — it reuses a scoped SQLite read session inside a single JournalSessionLog.scan(). It does not touch SessionInteractions.java.

This PR is on the caller side: pending() previously started a fresh scan() per 256-event page, so it paid full chain traversal N times. The two changes are complementary: #3673 makes each scan cheaper, this one stops issuing N of them. No shared file, no semantic conflict; whichever lands second should be rebased only for context.

A turn start needed the turn view, pending interactions, the recovery
projection and the transcript at the same committed prefix, and each built
itself from its own full replay: the recorder constructor replayed three
times and SessionExecution.prepare twice more, so a turn start cost seven
to nine independent passes over the journal.

Add SessionPrefix: one pass over the committed prefix feeding an
accumulator per view. The four read paths now share that single pass when
they are asked for the same prefix, and the recorder hands its prefix to
the execution that follows, so the constructor and prepare replay once
instead of five times.

On a 500-commit journal one replay costs 1002 storage reads, so this
removes roughly 5000 reads per turn start. The views and the recorder
guards are unchanged: run id reuse, completed turn detection and the
uncertain tool call check still read the same event set.
@wahllllll wahllllll changed the title perf(core): scan the interaction prefix once in SessionInteractions.pending() perf(core): replay each session prefix once for every session view Oct 28, 2026
@wahllllll

Copy link
Copy Markdown
Contributor Author

Added a second commit to this PR, because the first one alone cannot account for the numbers reported in #3816.

I instrumented the storage layer with a read counter on a 500-commit journal: one replay (scan(0, head)) costs 1002 storage reads, i.e. 2.004 per commit. With pending() fixed, a turn start still replayed that prefix 7-9 times — AgentSession.receipt(), the immediate dispatch(), SessionExecution.prepare(), and three separate replays inside the SessionRecorder constructor (projection, the run/turn guard loop, and the transcript), plus two more in the step paths.

The second commit introduces SessionPrefix: one pass over the committed prefix, feeding an accumulator per view, with SessionRecorder handing its prefix to SessionExecution.prepare. The constructor and prepare now replay once instead of five times.

SessionPrefixTest measures it on a 200-commit journal: the four views cost 1616 reads when built separately and 402 when shared, and it asserts each view reports identical content.

What this still does not fix is the 2x per-commit read inside scan() itself: the chain walk reads and parses each commit, then readCommit reads, parses and hashes it again. That is where a "thanks" on a large journal keeps paying, and it belongs near #3673 since that PR touches the same method. I would rather land it there than pile a third rewrite of scan() into this one.

@oyx888 if your 39 s measurement is still reproducible, the per-phase breakdown (timers around receipt(), the immediate dispatch(), SessionExecution.prepare() and the SessionRecorder constructor) would tell us whether these replays are the bulk of it or whether something else dominates. Happy to adjust the approach based on what you see.

@wahllllll

Copy link
Copy Markdown
Contributor Author

Closing: #4154 removed the session classes this optimization targeted (pending() and the prefix-replay path), so the branch no longer merges against main. The new journal architecture may deserve a fresh look for similar patterns, but that would be new work against a different shape.

@wahllllll wahllllll closed this Nov 15, 2026
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.

2 participants