Repository navigation
Description
Background
Sessions using AgentScope's EVENT_LOG mode (sessionHistoryMode=EVENT_LOG, JDBC-backed SessionLog) currently rebuild SessionProjection by calling log.scan(0, head.seq()) at the start of every request.
On a real JDBC session with 445 commit segments this means roughly one remote query per segment during recovery, producing about 35 seconds of latency before the first token.
Problem
Recovery has to replay the entire journal even though the only two pieces of state it needs are already reachable from a bounded suffix:
- the latest state snapshot (
state/checkpoint,migration/baseline, orstate/restored) forstateJson; - the latest
run/startso thatactiveRunsand unresolvedtool/dispatchentries stay visible.
Both are monotonic. Keeping their sequence numbers next to the journal head makes the lookup O(1) instead of walking the commit chain backwards.
Proposed change
-
Maintain a small
<session>/anchors.json:{ "lastCheckpointSeq": 48896, "lastRunStartSeq": 51506 }- Updated after a commit has durably written both its segment and
head.json. - Only increases; CAS retries are best-effort because a stale anchor only means replaying more events.
- Missing or corrupt anchors are rebuilt from the committed chain before the next update, never partially overwritten.
- Updated after a commit has durably written both its segment and
-
Start
SessionProjection.readatmin(lastCheckpointSeq, lastRunStartSeq) - 1(or from 0 when no checkpoint exists or an anchor is out of range). -
Start
SessionViews.transcriptfrom the latest checkpoint, falling back to the full journal when a snapshot cannot carry all history (for example an explicit context clear). -
Advance the durable export cursor in batches of 200 events instead of one
advanceExportCursorper event, keepingsink.acceptper event and at-least-once delivery (a crash replays at most one batch).
Acceptance criteria
stateJson,activeRuns, anduncertainToolCallsmatch a full replay for normal sessions, interrupted runs, missing/lagging anchors, and concurrent commits.- Missing or corrupt
anchors.jsonsafely falls back to a full replay, then rebuilds complete anchors. - Anchor lookup is O(1) regardless of segment count.
- Transcript matches the full journal, including after context replacement.
- Export cursor advances every 200 events and the final tail, with safe replay after a mid-batch crash.
- No event format or committed data changes.
Activity
perf(core): anchor session recovery at latest checkpoint and run start