Skip to content

perf(core): anchor session recovery at latest checkpoint and run start - #3988

Draft
chcodex wants to merge 1 commit into
agentscope-ai:mainfrom
chcodex:perf/session-log-recovery-anchors
Draft

chcodex wants to merge 1 commit into
agentscope-ai:mainfrom
chcodex:perf/session-log-recovery-anchors

Conversation

@chcodex

@chcodex chcodex commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3987.

Problem

EVENT_LOG recovery called log.scan(0, head.seq()) at the start of every request. On a JDBC-backed session with 445 commit segments this meant roughly one remote read per segment and about 35 seconds before the first token.

Change

  • Add <session>/anchors.json with lastCheckpointSeq and lastRunStartSeq.
    • Written only after the commit segment and head.json are durable.
    • Merge-only and CAS-based; a failed update is safe because recovery just replays more.
    • Missing/corrupt anchors are rebuilt from the committed chain before the next update, so partial anchors never overwrite a valid checkpoint.
  • Start SessionProjection.read at the earlier of the checkpoint and run-start anchors, so stateJson, activeRuns, and uncertainToolCalls stay equivalent to a full replay.
    • No checkpoint: replay from 0.
    • Anchor beyond the current head: ignore the anchor file and replay from 0.
  • SessionViews.transcript merges checkpoint state instead of clearing collected messages, preserving history across context replacement.
  • SessionLogExporter.drain advances the durable cursor every 200 events plus a final tail, keeping per-event sink.accept and at-least-once delivery; a crash replays at most one batch.

No event format or committed journal data changes.

Verification

mvn -o -T1C -pl agentscope-core -am -DskipTests package
# BUILD SUCCESS

mvn -o -T1C -pl agentscope-core -am -Dtest='io.agentscope.core.session.*Test' test
# Tests run: 50, Failures: 0, Errors: 0 — BUILD SUCCESS

mvn -o -T1C -pl agentscope-harness -am \
  -Dtest='AgentSessionTest,SubagentPendingToolRecoveryTest,WorkspaceSessionLogStoreTest' \
  -Dsurefire.failIfNoSpecifiedTests=false test
# Tests run: 34, Failures: 0, Errors: 0 — BUILD SUCCESS

New coverage includes anchor create/read, missing/corrupt rebuild, lagging anchors, CAS-conflict monotonicity, out-of-range anchors, interrupted runs, unresolved tool calls, transcript history, and batched export cursor replay after a simulated crash.

Read-count comparison

Synthetic journal with 153 one-event commit segments:

path commit-segment reads anchor-file reads
full scan 306 0
previous reverse-scan anchor lookup 7 0
this change 4 2

Anchor lookup is now 2 file reads regardless of segment count; remaining commit reads are only the necessary replay from the anchor to head.

Merge order

Recovery previously replayed the whole EVENT_LOG journal, costing one remote read per commit segment (about 35s on a 445-segment session).

Maintain an O(1) anchors.json next to the journal head with the latest checkpoint and run/start sequences. Anchors update only after the commit and head are durable, only increase, and are rebuilt from the committed chain when missing or corrupt.

Start projection at the earlier of the two anchors so stateJson, activeRuns, and uncertainToolCalls match a full replay. Transcript now merges checkpoint state without dropping history, and the export cursor advances in batches of 200 events with at-least-once replay.

Refs agentscope-ai#3987
@jujn jujn added the P2 重要不紧急 label Nov 7, 2026
chcodex added a commit to chcodex/agentscope-java that referenced this pull request Nov 7, 2026
# Conflicts:
#	agentscope-extensions/agentscope-extensions-protocol/agentscope-extensions-agui/src/main/java/io/agentscope/core/agui/processor/AguiRequestProcessor.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/main/java/io/agentscope/extensions/sandbox/e2b/E2bEnvdProcessClient.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/main/java/io/agentscope/extensions/sandbox/e2b/E2bSandbox.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/main/java/io/agentscope/extensions/sandbox/e2b/E2bSandboxClient.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/main/java/io/agentscope/extensions/sandbox/e2b/E2bVolumeMount.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bEnvdProcessClientTest.java
#	agentscope-extensions/agentscope-extensions-sandbox/agentscope-extensions-sandbox-e2b/src/test/java/io/agentscope/extensions/sandbox/e2b/E2bVolumeMountTest.java
#	agentscope-extensions/agentscope-spring-boot-starters/agentscope-agui-spring-boot-starter/src/main/java/io/agentscope/spring/boot/agui/mvc/AguiMvcController.java
#	agentscope-extensions/agentscope-spring-boot-starters/agentscope-agui-spring-boot-starter/src/test/java/io/agentscope/spring/boot/agui/mvc/AguiMvcControllerTest.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/local/LocalFilesystem.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/remote/RemoteFilesystem.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/sandbox/BaseSandboxFilesystem.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/filesystem/util/FilesystemUtils.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/middleware/WorkspaceContextMiddleware.java
#	agentscope-harness/src/main/java/io/agentscope/harness/agent/sandbox/WorkspaceMountSupport.java
#	agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/sandbox/BaseSandboxFilesystemTest.java
#	agentscope-harness/src/test/java/io/agentscope/harness/agent/filesystem/util/FilesystemUtilsTest.java
#	agentscope-harness/src/test/java/io/agentscope/harness/agent/sandbox/WorkspaceMountSupportTest.java
chcodex added a commit to chcodex/agentscope-java that referenced this pull request Nov 7, 2026
…ts tests

The anchors merge (agentscope-ai#3988) kept the local middleware implementation while the
matching prompt tests came from upstream, leaving 27 harness tests failing.
Restore the upstream implementation and align the local-only sandbox/composite
assertions with it; the sandbox branch now describes the execution backend
instead of the old container-id wording.
chcodex added a commit to chcodex/agentscope-java that referenced this pull request Nov 9, 2026
@chcodex
chcodex marked this pull request as draft November 10, 2026 02:27

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 full EVENT_LOG journal replay on every session recovery

2 participants