Skip to content

perf(core): avoid full EVENT_LOG journal replay on every session recovery #3987

Open

Description

@chcodex

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, or state/restored) for stateJson;
  • the latest run/start so that activeRuns and unresolved tool/dispatch entries 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

  1. 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.
  2. Start SessionProjection.read at min(lastCheckpointSeq, lastRunStartSeq) - 1 (or from 0 when no checkpoint exists or an anchor is out of range).

  3. Start SessionViews.transcript from the latest checkpoint, falling back to the full journal when a snapshot cannot carry all history (for example an explicit context clear).

  4. Advance the durable export cursor in batches of 200 events instead of one advanceExportCursor per event, keeping sink.accept per event and at-least-once delivery (a crash replays at most one batch).

Acceptance criteria

  • stateJson, activeRuns, and uncertainToolCalls match a full replay for normal sessions, interrupted runs, missing/lagging anchors, and concurrent commits.
  • Missing or corrupt anchors.json safely 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions