Class SessionTree
The session file is a JSONL file where each line is a JSON-serialized SessionEntry.
Entries form a tree via id/parentId links. A companion .log.jsonl file
stores the full history for grep-ability (dual-file pattern from pi-mono mom).
File layout
agents/{agentId}/sessions/{sessionId}.jsonl — LLM context (compacted)
agents/{agentId}/sessions/{sessionId}.log.jsonl — full history (append-only, never compacted)
Persistence model
The local file is the working copy. Cross-replica durability uses one of:TranscriptStore(preferred) — each flush writes an immutable segment; concurrent writers never overwrite each other.AbstractFilesystemfull-file mirror (legacy) — O(N²) uploads and last-writer-wins across replicas; kept for backward compatibility when noTranscriptStoreis set.
Deferred persistence
Entries are buffered in memory and only flushed to disk on the first call toflush()
(typically after the first assistant message). This avoids partial session files from
failed/short interactions.-
Constructor Summary
ConstructorsConstructorDescriptionSessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem) Creates a SessionTree backed by the given filesystem for remote mirroring.SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index) SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index, String contextRelativePath) Creates a SessionTree with optional best-effort workspace index support. -
Method Summary
Modifier and TypeMethodDescriptionappend(SessionEntry entry) Appends an entry to the in-memory tree.static booleanawaitMirrorQuiescence(long timeout, TimeUnit unit) Blocks until all remote-mirror tasks submitted before this call have finished.Builds the LLM-visible context from the session tree.voidflush()Flushes pending entries to the local context and log files, then schedules asynchronous remote mirrors.Returns all entries in append order (full history).Returns only message entries in append order.booleanReturns whetherflush()has been called at least once.voidload()Loads existing entries from the local context file into the in-memory tree.setRuntimeContext(io.agentscope.core.agent.RuntimeContext rc) Binds aRuntimeContextto this tree so that all subsequent remote filesystem reads and writes (including async mirror operations) propagate the caller's identity into namespace-aware stores.setTranscriptStore(TranscriptStore store, TranscriptRef ref) Binds aTranscriptStorefor segmented remote persistence.intsize()intSyncs entries from the log file that are not yet in the context file.voidPulls remote entries and union-merges any not yet present locally.
-
Constructor Details
-
SessionTree
Creates a SessionTree backed by the given filesystem for remote mirroring.- Parameters:
contextFile- path to the.jsonlcontext file (LLM-facing, compacted)workspaceRoot- root of the agent workspace; used to derive workspace-relative pathsfilesystem-AbstractFilesystemused for remote read/write; may benullto disable remote mirroring (local-only mode)
-
SessionTree
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index) -
SessionTree
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index, String contextRelativePath) Creates a SessionTree with optional best-effort workspace index support.- Parameters:
contextFile- path to the.jsonlcontext fileworkspaceRoot- root of the agent workspacefilesystem- remote filesystem; may benullindex- best-effort workspace index; may benullcontextRelativePath- workspace-relative path for the context file WITHOUT namespace prefix (e.g.agents/X/sessions/Y.jsonl); when non-null, used for filesystem mirror/restore instead of computing viatoWorkspaceRelative(Path)
-
-
Method Details
-
awaitMirrorQuiescence
Blocks until all remote-mirror tasks submitted before this call have finished.The mirror executor is single-threaded and serial, so waiting on a sentinel task guarantees that every previously scheduled mirror upload has completed. Intended for graceful shutdown (
HarnessAgent.close()) so asynchronous transcript/session mirrors do not race with resource cleanup (e.g., temp workspace deletion).- Parameters:
timeout- maximum time to waitunit- time unit oftimeout- Returns:
trueif the mirrors quiesced within the timeout
-
setRuntimeContext
Binds aRuntimeContextto this tree so that all subsequent remote filesystem reads and writes (including async mirror operations) propagate the caller's identity into namespace-aware stores. Defaults toRuntimeContext.empty()when never set.- Parameters:
rc- the runtime context to bind;nullresets to empty- Returns:
- this tree, for fluent chaining
-
setTranscriptStore
Binds aTranscriptStorefor segmented remote persistence. When set,flush()writes immutable segments instead of full-file mirrors, andsyncFromRemote()merges from listed segments. history UI paths stay aligned with the local workspace.nulldisables segments- Parameters:
store- segment store;nulldisables segmented persistenceref- transcript identity; required whenstoreis non-null- Returns:
- this tree, for fluent chaining
-
load
public void load()Loads existing entries from the local context file into the in-memory tree.This is a local-only, zero-network operation. If the local file is absent, the tree starts empty. To additionally pull and union-merge entries from the remote filesystem (e.g., before a write that may follow a cross-machine handoff), call
syncFromRemote()after this method.Safe to call multiple times; only loads once.
-
syncFromRemote
public void syncFromRemote()Pulls remote entries and union-merges any not yet present locally.When a
TranscriptStoreis bound, reads immutable segments (no whole-file overwrite race). Otherwise falls back to the legacy full-file remote mirror.load()must be called before this method. -
append
Appends an entry to the in-memory tree. The entry will be written to disk on the nextflush()call.- Returns:
- the entry (for chaining)
-
flush
public void flush()Flushes pending entries to the local context and log files, then schedules asynchronous remote mirrors. When aTranscriptStoreis bound, both immutable segments and the canonical context/log files are mirrored. The remote mirror is fire-and-forget: failures are logged as warnings and do not affect the return of this method. The local write is always the primary guarantee. -
isFlushed
public boolean isFlushed()Returns whetherflush()has been called at least once. -
buildContext
Builds the LLM-visible context from the session tree.Returns entries that the LLM should see:
- If compaction has occurred, starts with the summary entry, then all entries
from
firstKeptEntryIdonward - If no compaction, returns all message entries in order
- If compaction has occurred, starts with the summary entry, then all entries
from
-
getAllEntries
Returns all entries in append order (full history). -
getMessageEntries
Returns only message entries in append order. -
size
public int size() -
getContextFile
-
getLogFile
-
syncFromLog
public int syncFromLog()Syncs entries from the log file that are not yet in the context file. This handles offline messages that were appended to the log while the agent was inactive.- Returns:
- the number of new entries synced
-