SessionTree
public class SessionTree
io/agentscope/harness/agent/memory/session/SessionTree.java:79–804会话树,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.harness.agent.memory.session 包。核心成员包括 awaitMirrorQuiescence、setRuntimeContext、setTranscriptStore、load、syncFromRemote、append。
Manages an append-only JSONL session tree (pi-mono-inspired).
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 to #flush()
(typically after the first assistant message). This avoids partial session files from
failed/short interactions.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem) | 初始化 `SessionTree` 实例。 Creates a SessionTree backed by the given filesystem for remote mirroring. |
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index) | 初始化 `SessionTree` 实例。 Creates a new instance. |
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index, String contextRelativePath) | 初始化 `SessionTree` 实例。 Creates a SessionTree with optional best-effort workspace index support. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public static boolean awaitMirrorQuiescence(long timeout, TimeUnit unit)static | 执行 `awaitMirrorQuiescence` 方法。 Blocks until all remote-mirror tasks submitted before this call have finished. |
public SessionTree setRuntimeContext(RuntimeContext rc) | 设置运行时上下文。 Binds a `RuntimeContext` to this tree so that all subsequent remote filesystem reads
and writes (including async mirror operations) propagate the caller's identity into
namespace-aware stores. |
public SessionTree setTranscriptStore(TranscriptStore store, TranscriptRef ref) | 设置 `setTranscriptStore`。 Binds a `TranscriptStore` for segmented remote persistence. |
public void load() | 从持久化存储恢复数据。 Loads existing entries from the local context file into the in-memory tree. |
public void syncFromRemote() | 执行同步来自远程相关操作。 Pulls remote entries and union-merges any not yet present locally. |
public SessionEntry append(SessionEntry entry) | 设置追加。 Appends an entry to the in-memory tree. |
public void flush() | 执行刷新相关操作。 Flushes pending entries to the local context and log files, then schedules asynchronous
remote mirrors. |
public boolean isFlushed() | 判断是否 `isFlushed`。 Returns whether `#flush()` has been called at least once. |
public List<SessionEntry> buildContext() | 构建模型可见的上下文来自会话树。 Builds the LLM-visible context from the session tree. |
public List<SessionEntry> getAllEntries() | 获取所有条目。 Returns all entries in append order (full history). |
public List<SessionEntry.MessageEntry> getMessageEntries() | 获取消息条目。 Returns only message entries in append order. |
public int size() | 返回大小。 Returns the size. |
public Path getContextFile() | 获取上下文文件。 Returns the context file. |
public Path getLogFile() | 获取日志文件。 Returns the log file. |
public int syncFromLog() | 返回同步来自日志。 Syncs entries from the log file that are not yet in the context file. |
构造器详情Constructor Detail
SessionTree
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem)
初始化 SessionTree 实例。
Creates a SessionTree backed by the given filesystem for remote mirroring.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
contextFile | Path | 上下文文件(类型 `Path`)。path to the `.jsonl` context file (LLM-facing, compacted) |
workspaceRoot | Path | 工作区根(类型 `Path`)。root of the agent workspace; used to derive workspace-relative paths |
filesystem | AbstractFilesystem | 文件系统(类型 `AbstractFilesystem`)。`AbstractFilesystem` used for remote read/write; may be `null` to disable remote mirroring (local-only mode) |
io/agentscope/harness/agent/memory/session/SessionTree.java:156–158SessionTree
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index)
初始化 SessionTree 实例。
Creates a new instance.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
contextFile | Path | 上下文文件(类型 `Path`)。The context file. |
workspaceRoot | Path | 工作区根(类型 `Path`)。The workspace root. |
filesystem | AbstractFilesystem | 文件系统(类型 `AbstractFilesystem`)。The filesystem. |
index | WorkspaceIndex | 索引(类型 `WorkspaceIndex`)。The index. |
io/agentscope/harness/agent/memory/session/SessionTree.java:160–166SessionTree
public SessionTree(Path contextFile, Path workspaceRoot, AbstractFilesystem filesystem, WorkspaceIndex index, String contextRelativePath)
初始化 SessionTree 实例。
Creates a SessionTree with optional best-effort workspace index support.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
contextFile | Path | 上下文文件(类型 `Path`)。path to the `.jsonl` context file |
workspaceRoot | Path | 智能体工作区的根。root of the agent workspace |
filesystem | AbstractFilesystem | 文件系统(类型 `AbstractFilesystem`)。remote filesystem; may be `null` |
index | WorkspaceIndex | 索引(类型 `WorkspaceIndex`)。best-effort workspace index; may be `null` |
contextRelativePath | String | 上下文 Relative 路径(类型 `String`)。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 via `#toWorkspaceRelative(Path)` |
io/agentscope/harness/agent/memory/session/SessionTree.java:180–204方法详情Method Detail
awaitMirrorQuiescence
public static boolean awaitMirrorQuiescence(long timeout, TimeUnit unit)
执行 awaitMirrorQuiescence 方法。
Blocks until all remote-mirror tasks submitted before this call have finished.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
timeout | long | 超时时间(类型 `long`)。maximum time to wait |
unit | TimeUnit | unit(类型 `TimeUnit`)。time unit of `timeout` |
booleanio/agentscope/harness/agent/memory/session/SessionTree.java:114–125setRuntimeContext
public SessionTree setRuntimeContext(RuntimeContext rc)
设置运行时上下文。
Binds a RuntimeContext to this tree so that all subsequent remote filesystem reads and writes (including async mirror operations) propagate the caller's identity into namespace-aware stores.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rc | RuntimeContext | rc(类型 `RuntimeContext`)。the runtime context to bind; `null` resets to empty |
SessionTreeio/agentscope/harness/agent/memory/session/SessionTree.java:214–217setTranscriptStore
public SessionTree setTranscriptStore(TranscriptStore store, TranscriptRef ref)
设置 setTranscriptStore。
Binds a TranscriptStore for segmented remote persistence.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
store | TranscriptStore | 存储实例(类型 `TranscriptStore`)。segment store; `null` disables segmented persistence |
ref | TranscriptRef | 引用(类型 `TranscriptRef`)。transcript identity; required when `store` is non-null |
SessionTreeio/agentscope/harness/agent/memory/session/SessionTree.java:229–233load
public void load()
从持久化存储恢复数据。
Loads existing entries from the local context file into the in-memory tree.
io/agentscope/harness/agent/memory/session/SessionTree.java:245–266syncFromRemote
public void syncFromRemote()
执行同步来自远程相关操作。
Pulls remote entries and union-merges any not yet present locally.
io/agentscope/harness/agent/memory/session/SessionTree.java:276–298append
public SessionEntry append(SessionEntry entry)
设置追加。
Appends an entry to the in-memory tree.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
entry | SessionEntry | 条目(类型 `SessionEntry`)。The entry. |
SessionEntryio/agentscope/harness/agent/memory/session/SessionTree.java:306–317flush
public void flush()
执行刷新相关操作。
Flushes pending entries to the local context and log files, then schedules asynchronous remote mirrors.
io/agentscope/harness/agent/memory/session/SessionTree.java:326–347isFlushed
public boolean isFlushed()
判断是否 isFlushed。
Returns whether #flush() has been called at least once.
booleanio/agentscope/harness/agent/memory/session/SessionTree.java:352–354buildContext
public List<SessionEntry> buildContext()
构建模型可见的上下文来自会话树。
Builds the LLM-visible context from the session tree.
List<SessionEntry>io/agentscope/harness/agent/memory/session/SessionTree.java:366–395getAllEntries
public List<SessionEntry> getAllEntries()
获取所有条目。
Returns all entries in append order (full history).
List<SessionEntry>io/agentscope/harness/agent/memory/session/SessionTree.java:400–402getMessageEntries
public List<SessionEntry.MessageEntry> getMessageEntries()
获取消息条目。
Returns only message entries in append order.
List<SessionEntry.MessageEntry>io/agentscope/harness/agent/memory/session/SessionTree.java:407–412size
public int size()
返回大小。
Returns the size.
intio/agentscope/harness/agent/memory/session/SessionTree.java:414–416getContextFile
public Path getContextFile()
获取上下文文件。
Returns the context file.
Pathio/agentscope/harness/agent/memory/session/SessionTree.java:418–420getLogFile
public Path getLogFile()
获取日志文件。
Returns the log file.
Pathio/agentscope/harness/agent/memory/session/SessionTree.java:422–424syncFromLog
public int syncFromLog()
返回同步来自日志。
Syncs entries from the log file that are not yet in the context file.
intio/agentscope/harness/agent/memory/session/SessionTree.java:433–473