Repository navigation
Conversation
|
Overlap note for reviewers: #3673 ( This PR is on the caller side: |
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.
|
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 ( The second commit introduces
What this still does not fix is the 2x per-commit read inside @oyx888 if your 39 s measurement is still reproducible, the per-phase breakdown (timers around |
|
Closing: #4154 removed the session classes this optimization targeted ( |
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:
SessionInteractions.pending(SessionLog)paged the journal withreadAfter(cursor, 256), andJournalSessionLog.readAfter()starts a fresh commit-chainscan()for every page. For a 15k-event journal that is roughly 60 full chain traversals per call.SessionRecorder's constructor replayed three times andSessionExecution.preparetwice 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 insidereadCommit).Change
First commit —
pending()scans the stable prefix once.pending()captureshead.seq()once and traverseslog.scan(0, upper)a single time instead of paging.Second commit — every view shares one replay. New
SessionPrefixperforms 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, andSessionRecorderhands its prefix to the execution that follows, so the constructor andpreparereplay once instead of five times.Behaviour is unchanged: the stable-prefix boundary is still captured once,
interaction/requested/interaction/resolvedsemantics 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.pendingScansTheStablePrefixOnceInsteadOfPagingFromHeaddrives a 600-event log through a countingSessionLogand asserts onehead(), onescan()and zero pagedreadAfter()restarts. On unmodifiedmainit fails withexpected: <1> but was: <0>.SessionPrefixTest.oneReplayFeedsEveryViewAtThePrefixseeds 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-existingDangerousPathBypassTestsymlink cases that need Windows symlink privileges unavailable on this machine (they also fail on unmodifiedmain).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.