tree

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package tree implements the tree-structured session JSONL format per registration spec §2.6. A session is a single append-only `.jsonl` file plus a tiny sidecar pointer file recording the current active head. Every entry carries an `id` and a `parentId` (empty for the root). Branching is cheap (the JSONL grows, the sidecar moves); rewind / fork / branch-summary operations are implemented by moving the head pointer without truncating history.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppendHook

type AppendHook func(Entry)

Tree is a handle on a session's JSONL + sidecar pair. Safe for concurrent use within a single process — all mutating operations take the tree mutex. Cross-process concurrency is NOT supported (session files are owned by a single agent runtime). AppendHook is invoked (synchronously, after the tree mutex is released) for each successful Append. Used by the agent runtime to forward entries upward as session.entry.appended frames for Nexus- side observability. Implementations must not call back into the tree; they should hand the entry off to a channel / queue.

type Entry

type Entry struct {
	ID       string         `json:"id"`
	ParentID string         `json:"parentId,omitempty"`
	Kind     EntryKind      `json:"kind"`
	TS       time.Time      `json:"ts"`
	Payload  map[string]any `json:"payload,omitempty"`
}

Entry is a single session-tree record as stored on disk.

type EntryKind

type EntryKind string

EntryKind mirrors the spec §2.6 kinds. These are the canonical session-tree kinds; providers.EntryKind is a superset (adds things like custom.*) but the tree owns the ones listed here.

const (
	KindTurnUser       EntryKind = "turn.user"
	KindTurnAssistant  EntryKind = "turn.assistant"
	KindTurnToolResult EntryKind = "turn.tool_result"
	KindSystemPrompt   EntryKind = "system.prompt"
	KindCompaction     EntryKind = "compaction"
	KindBranchSummary  EntryKind = "branch_summary"
)

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

func Open

func Open(dir, name string) (*Tree, error)

Open opens (or creates) the session files at dir/<name>.jsonl and dir/<name>.head.json. Creates the directory if missing. Works on both a brand-new session and an existing one.

func (*Tree) All

func (t *Tree) All(ctx context.Context) ([]Entry, error)

All returns every entry in the log, regardless of which branch they belong to. Useful for export / backup / audit. Ordered by file position, not timestamp.

func (*Tree) Append

func (t *Tree) Append(ctx context.Context, e Entry) (Entry, error)

Append writes a new entry to the JSONL, records it as the new active head, and returns it with ID and TS populated. ParentID defaults to the current head if the caller leaves it empty; pass a specific ParentID to branch off an earlier entry.

If an AppendHook is installed, it fires after the mutex is released with the written entry — the hook gets the fully populated Entry (id + ts + parent) and can forward it.

func (*Tree) Fork

func (t *Tree) Fork(ctx context.Context, id string) error

Fork sets the head to an earlier entry. A subsequent Append will link its ParentID to this entry, creating a new branch in the tree. Shorthand for SetHead; exists so callers can self-document intent.

func (*Tree) Head

func (t *Tree) Head() (string, error)

Head returns the current active-head entry ID. Empty string on a brand-new session.

func (*Tree) JSONLPath

func (t *Tree) JSONLPath() string

JSONLPath returns the session log path.

func (*Tree) Replay

func (t *Tree) Replay(ctx context.Context) ([]Entry, error)

Replay returns the entries on the active branch, oldest first. Walks from the current head back along parentId links, collecting matching entries, then reverses. Callers get a chronological view of the session regardless of where the JSONL physically stored branches.

func (*Tree) SetAppendHook

func (t *Tree) SetAppendHook(h AppendHook)

SetAppendHook installs a callback that fires after each successful Append. The hook runs on the Append caller's goroutine, after the tree mutex has been released, so it must not reach back into the tree or do anything slow. Pass nil to clear.

func (*Tree) SetHead

func (t *Tree) SetHead(ctx context.Context, id string) error

SetHead moves the active head pointer to the given entry ID. Intended for rewind / fork operations — the entry must already exist in the JSONL (otherwise the next Replay would fail).

func (*Tree) SidecarPath

func (t *Tree) SidecarPath() string

SidecarPath returns the head-pointer file path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL