Skip to content
pleach

Time travel

Event-granular session forking and state reconstruction — the read/write API over the checkpointer's history.

TimeTravelAPI is the navigable view over the checkpoint history a Checkpointer writes. It reconstructs StateSnapshot records from raw checkpoint rows, walks history newest-first, applies synthetic supersteps for replay, and forks a checkpoint into a new session.

import {
  TimeTravelAPI,
  type StateSnapshot,
  type StateSnapshotMetadata,
  type StateSnapshotConfig,
  type GetStateHistoryOptions,
} from "@pleach/core/time-travel";

This is the substrate-level fork surface. runtime.checkpoints.rollback is the higher-level path that rebuilds session state in place and bumps the version vector;TimeTravelAPI is what you reach for when you want to branch into a new session id or inspect snapshot internals without touching the live session.

The canonical runtime entry point is runtime.timeTravel.api — the facet returns the lazily-constructed TimeTravelAPI instance (or undefined when the runtime has no checkpointer). All four methods — getState, getStateHistory, bulkUpdateState, fork — live on that instance.

const api = runtime.timeTravel.api;
if (api) {
  const fork = await api.fork(sourceSessionId, checkpointId, newSessionId);
}

How it differs from runtime.checkpoints.rollback

runtime.checkpoints.rollbackTimeTravelAPI.fork
Target sessionSame session idNew session id
Live sessionMutated in placeUntouched
Version vectorBumps; writes a new source: "rollback" checkpointForked checkpoint has source: "fork", parent_id: source.id (cross-session edge)
Lineage edgeparentCheckpointId on the new rollback checkpointmetadata.forkedFrom: { sessionId, checkpointId } + cross-session parent_id
Event-log cursorInherited from the rollback targetInherited from the source checkpoint's metadata.lastEventSequence
Chained event rowcheckpoint.branched (reason: "rollback")checkpoint.forked
Use caseRecover from a stuck turn, retry from earlier stateExplore an alternative branch without losing the original

Both write to the same checkpointer. Forks land in the new session's namespace; the source session keeps its own history intact.

Constructor

new TimeTravelAPI(
  checkpointer:      CheckpointerLike,
  createChannels:    () => Record<string, ChannelLike>,
  nodeSubscriptions: Record<string, string[]> = {},
);

createChannels is a factory that returns a fresh channel map keyed by channel name — the API uses it to replay reducer logic when applying synthetic supersteps. nodeSubscriptions maps node name to the set of channels each node subscribes to; it drives the next array on every returned StateSnapshot.

If you only need read methods (getState, getStateHistory, fork), an empty nodeSubscriptions is fine — next will simply be empty.

Methods

MethodReturnsNotes
getState(sessionId, checkpointId?)Promise<StateSnapshot | null>Latest when checkpointId is omitted.
getStateHistory(sessionId, options?)AsyncGenerator<StateSnapshot>Newest-first; limit caps iteration, before cursors, source filters by node.
bulkUpdateState(sessionId, supersteps)Promise<StateSnapshot>Replays writes through createChannels(), persists with source: "bulk_update".
fork(sourceSessionId, checkpointId, newSessionId)Promise<StateSnapshot>Copies channel state into a new session; sets parent_id: source.id (cross-session lineage edge).

StateSnapshot shape

interface StateSnapshot<S = Record<string, unknown>> {
  values:          S;                                  // reconstructed channel values
  pendingTasks:    Array<{ id; name; input }>;          // in-flight at snapshot time
  pendingWrites:   Array<{ taskId; channel; value }>;   // buffered but not committed
  metadata:        StateSnapshotMetadata;
  config:          StateSnapshotConfig;
  next:            string[];                            // nodes eligible to run next
  channelVersions: Record<string, number>;
  manifest?:       ChatManifestSnapshot;                // chat-manifest ledger AS-OF this checkpoint (inspection-only)
}

interface StateSnapshotMetadata {
  step:               number;
  timestamp:          string;        // ISO-8601
  source:             string;        // node or trigger that produced this state
  writtenBy:          string[];
  parents:            string[];      // parent checkpoint ids; cross-session for forks
  lastEventSequence?: number;        // event-log cursor at snapshot time
}

next is computed from channelVersions + versions_seen against the constructor's nodeSubscriptions — a node appears if any channel it subscribes to has advanced past the version it last saw. That's what makes a snapshot replayable: you can pick up exactly where the graph left off without re-running already-applied work.

metadata.parents is the parent-link chain — a single-element array for ordinary checkpoints, multi-element for merge scenarios. Forked snapshots populate parents with the source checkpoint id (a cross-session edge), so traversals walking metadata.parents no longer hide the fork's origin.

manifest is the chat-manifest ledger (tool/job invocations, notices, provider switches) AS-OF the checkpoint — surfaced for inspection (getState/getStateHistory/fork/bulkUpdate). It is OPTIONAL (undefined for an empty ledger or a legacy checkpoint). Inspection-only: it does NOT feed the next generation — that functional rehydration runs through rollbackToCheckpoint/resumeSession into the live ChatManifestProvider. The snapshot rides both the checkpoint metadata (this field) and state.extensions (the functional path) for the two distinct purposes.

Event-log cursor on the snapshot

metadata.lastEventSequence carries the event-log sequence number the snapshot was taken at. A host hydrating from this snapshot can fast-forward intermediate state without re-folding the event log from genesis: pass the cursor straight to runtime.events.iterate({ chatId, fromSequenceNumber: snapshot.metadata.lastEventSequence }) and replay events forward from there. The same value flows into hydrateFromEvents() as fromSequenceNumber.

The field is undefined on checkpoints written before the cursor field landed — consumers must back-compat to "no skip" when it's missing.

Fork semantics

Which channels survive a fork is decided by the underlying channel_values blob: every channel in the source checkpoint copies verbatim into the new session. There is no per-channel survival policy at this layer — that lives in the reducer definition for each channel, and the fork respects whatever the snapshot already has.

const api = new TimeTravelAPI(checkpointer, createDefaultChannels);

const snapshot = await api.fork(
  sourceSessionId,
  "cp_018f...",
  crypto.randomUUID(),
);

snapshot.config.sessionId;              // new session id
snapshot.metadata.source;               // "fork"
snapshot.metadata.parents;              // [source.id] — cross-session edge
snapshot.metadata.lastEventSequence;    // inherited from the source checkpoint
// the lineage edge on the raw checkpoint metadata:
// metadata.forkedFrom = { sessionId: sourceSessionId, checkpointId: "cp_018f..." }

Fork lineage

A forked checkpoint records its origin in two places:

  • metadata.forkedFrom: { sessionId, checkpointId } — the semantic lineage edge. Audit-ledger queries join against this to walk forked sessions back to their origin. The forked session keeps its own audit rows under the new sessionId; forkedFrom on the first checkpoint is the one place the cross-session relationship is recorded.
  • parent_id: source.id on the raw checkpoint row (surfaced as metadata.parents: [source.id] on the snapshot). This is a cross-session parent edge — distinct from forkedFrom only by shape (a parent id vs. a {sessionId, checkpointId} pair). Same-session supersteps already set parent_id to the previous checkpoint; fork now sets it to the source checkpoint so traversals walking metadata.parents see the origin instead of treating forked snapshots as orphan roots.

Event-log cursor carry

The fork inherits metadata.lastEventSequence from the source checkpoint. A consumer hydrating the forked session passes that cursor to runtime.events.iterate({ chatId, fromSequenceNumber }) exactly as it would for the source — the cursor is the join point between snapshot state and the event log.

Materializing the forked session row

TimeTravelAPI accepts an optional createForkedSession hook on its constructor options. After the forked checkpoint lands, the API invokes the hook so the host can materialize a Session record at newSessionId — typically by cloning the source session's SessionConfig. Without the hook, a subsequent runtime.sessions.resume(newSessionId) will throw on a missing session row; legacy callers must materialize the row themselves before resume.

The hook is best-effort: a thrown error is caught and logged via console.warn, and the fork's checkpoint write is not rolled back. The checkpoint is the durable artifact; the session row is a derived convenience.

Who owns the fork

fork() takes an optional fourth argument, TimeTravelForkOptions (exported from the package root). Its ownerUserId names the user the forked session belongs to, and the hook receives it as source.ownerUserId:

await api.fork(sourceSessionId, "cp_018f...", crypto.randomUUID(), {
  ownerUserId: callerId,
});

The route family passes the gated caller here, so under an owner gate a fork belongs to the user who forked it, not to the source session's owner. With no ownerUserId (an ungated server), the source row's owner stamp is copied, which is only correct when the caller owns the source.

What a fork inherits: the chat manifest

A fork writes to two places, and both carry the parent's chat manifest — the tool/job/notice ledger — as of the fork point:

  • The forked checkpoint carries the full session state, spread from the source, including the manifest. On the happy path, resume() reads the manifest from the checkpoint overlay; the session row is not consulted for it.
  • The forked session row (written by createForkedSession) also carries the manifest. This copy matters when the checkpoint cannot answer: the source checkpoint was pruned, the saver is non-durable, or a cold process holds durable rows but no checkpoints.

The contract is stated because it was once violated: the forked session row used to skip the manifest stamp every non-forked row received, so a branch whose source checkpoint was later pruned lost the parent's ledger while every ordinary session survived the same event. Both writes now carry it.

Two boundaries to keep in mind:

  • The inherited manifest is the ledger as of the fork checkpoint, not the parent's live ledger. Rewind the conversation, rewind the ledger — work the parent does after the fork point does not appear in the branch.
  • If you supply your own createForkedSession hook, mirror the behavior: seed the manifest from the source checkpoint's metadata, and seed nothing when the source carried no manifest. An empty ledger stamped as if it were real is worse than an absent one.

Branch point on the forked chain

A fork records where it left the parent chat chain in two chained rows:

  • checkpoint.forked on the parent chat chain names the source session id, source checkpoint id, new session id and new checkpoint id. It also carries the forked-at sequence number, the source checkpoint's event cursor, when the checkpoint has one. It is written only when the API has an event-log writer and a chat id.
  • The forked session chain's genesis (session.created) names the parent row. It is written only by an EventLogWriter constructed with c9ChainIdentity: true; any other writer writes no genesis.

The genesis payload fields:

FieldValue
Parent chain kindAlways "chat"
Parent chain idThe parent chat id
Parent sequence numberThe source checkpoint's event cursor. Absent with status no-cursor
Parent row hashThe parent row's row hash, lowercase 64-hex
Parent row hash statusPresent instead of the parent row hash when the hash could not be read

The status says why no hash was recorded. A hash is never guessed.

StatusMeaning
no-cursorThe source checkpoint carried no event cursor, so only the parent chain is named
no-readerThe fork had no parent-row reader: none was available, or you passed forkParentRowReader: null
not-foundThe reader has no row at that ordinal
unchainedThe row exists but has no row hash
read-failedThe reader threw, or returned something that is not a 32-byte digest

A runtime wires a parent-row reader by default when its eventLogWriter was constructed with c9ChainIdentity: true, the only writer that writes a fork genesis. It reads through that writer's own store, scoped to the writer's chain tenant. forkParentRowReader: null opts out.

To read somewhere else, pass your own as SessionRuntimeConfig.forkParentRowReader (experimental). TimeTravelAPIOptions.readParentChainRow has no default, so a TimeTravelAPI you construct yourself needs one passed in. forkParentRowReaderFromChainReader builds a reader from a chain reader that implements iterateChainEvents, scoped to one tenant:

import {
  forkParentRowReaderFromChainReader,
  verifyBranchPoint,
} from "@pleach/core/eventLog";

const forkParentRowReader = forkParentRowReaderFromChainReader(chainReader, tenantId);

// Later: prove the forked chain's genesis against the parent chain.
const result = await verifyBranchPoint({ child: genesisRow, parentReader: chainReader, key });
if (!result.ok) console.error(result.reason);

verifyBranchPoint refuses, with a BRANCH_POINT_*_REASON constant, when the child row is not a forked genesis, the genesis does not recompute to its row hash, it names no cursor or no hash, the reader is not chain-addressed, or the parent row is missing, belongs to another chain, has a different hash, or does not recompute. On success it returns the parent chain, ordinal and row hash, plus parentPayload, which is "checked" only when the parent row's body was covered.

What a passing result does not prove:

  • On an unkeyed chain it proves only that the parent row existed, with those bytes, before the genesis was hashed. A keyed chain (key) also proves a holder of the chain secret wrote the genesis.
  • Cross-check fork lineage against the parent chain's checkpoint.forked row.
  • It does not verify either chain end to end. Run verifyChainById on the child chain and verifyChainForChat on the parent for that.

Rollback leaves a chained record

runtime.checkpoints.rollback writes a checkpoint.branched row after the rollback checkpoint lands. The rows written after the target checkpoint stay on the chat chain, which is append-only, so this row is what says the session left them behind. Its payload:

FieldValue
Source session id, branch session idThe rolled-back session (the same id)
Source checkpoint idThe rollback target
Branch checkpoint idThe new rollback checkpoint. Omitted when the saver returned no id
reason"rollback"
Branched-at sequence numberThe target checkpoint's event cursor, when it has one

The row goes on the checkpoint's own chat, else the runtime's bound chat. With no event-log writer or no chat, nothing is written. A write failure is reported and does not undo the rollback.

Walking history

for await (const snapshot of api.getStateHistory(sessionId, { limit: 50 })) {
  console.log(snapshot.metadata.step, snapshot.metadata.source, snapshot.config.checkpointId);
}

Pass source: "tool-loop" to filter to checkpoints written by the tool-loop stage; pass before: "cp_..." to page backwards from a known checkpoint id.

Replaying synthetic supersteps

bulkUpdateState is how a test harness or migration script writes a sequence of channel updates without running the graph. Each superstep is an array of writes; the API replays them through createChannels() so reducers fire normally, then persists a single source: "bulk_update" checkpoint at the end.

const snapshot = await api.bulkUpdateState(sessionId, [
  // Superstep 1 — seed the corpus channel.
  [{ channel: "search_results", value: { docs: [{ id: "doc_001" }] } }],
  // Superstep 2 — append a summary.
  [{ channel: "summary", value: "Initial pass complete." }],
]);

snapshot.metadata.source;            // "bulk_update"
snapshot.channelVersions.summary;    // bumped once per superstep that touched it

bulkUpdateState throws if the target session has no prior checkpoint — it needs an anchor to write parent_id against. Use fork for the first checkpoint in a new session id.

Side effects don't roll back

A snapshot captures channel state, not external writes. If a tool called between the target snapshot and now wrote to a database, sent an email, or charged a card, rolling back or forking from the earlier snapshot does not unwind those writes. Tools that need true rollback have to implement compensating actions themselves — Pleach doesn't have a transactional surface across third-party systems.

Where to go next