session

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package session — branch.go implements session branching. Branches allow creating divergent conversation paths from a fork point.

Package session provides a file-based session.Service implementation that persists sessions as JSONL files on disk.

Index

Constants

View Source
const UnknownModel = "unknown"

UnknownModel is the placeholder written to meta.Model when the caller did not supply one. meta.json always carries a non-empty "model" field so log consumers (CLI status, /sessions list, the ATIF trajectory) never have to branch on absence.

Variables

This section is empty.

Functions

func GenerateSessionID added in v0.0.23

func GenerateSessionID() string

GenerateSessionID creates a time-sortable session ID. Format: yymmdd-hhmm-xxxxx-xxxxx (23 chars total). The yymmdd-hhmm prefix makes IDs naturally sortable by creation time.

Types

type BranchInfo

type BranchInfo struct {
	Name      string  `json:"name"`
	Head      int     `json:"head"`                // index of the last event in this branch
	Parent    *string `json:"parent"`              // parent branch name, nil for main
	ForkPoint int     `json:"forkPoint,omitempty"` // event index where this branch forked from parent
}

BranchInfo describes a named branch within a session.

type CompactConfig

type CompactConfig struct {
	// MaxTokens is the approximate token threshold that triggers compaction.
	// Default: 100000.
	MaxTokens int

	// KeepRecent is the number of recent events to keep uncompacted.
	// Default: 10.
	KeepRecent int
}

CompactConfig controls when and how compaction runs.

func DefaultCompactConfig

func DefaultCompactConfig() CompactConfig

DefaultCompactConfig returns sensible default compaction settings.

type FileService

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

FileService implements session.Service with file-based JSONL persistence. Sessions are stored in baseDir/<session-id>/ with meta.json and events.jsonl.

func NewFileService

func NewFileService(baseDir string) (*FileService, error)

NewFileService creates a new file-based session service. baseDir is the directory where sessions are stored (e.g., ~/.pi-go/sessions).

func (*FileService) ATIFWriter added in v0.0.14

func (s *FileService) ATIFWriter(sessionID string) *atif.Writer

ATIFWriter returns the ATIF writer for the given session, or nil if not found.

func (*FileService) ActiveBranch

func (s *FileService) ActiveBranch(sessionID string) string

ActiveBranch returns the name of the currently active branch.

func (*FileService) AppendEvent

func (s *FileService) AppendEvent(_ context.Context, curSession session.Session, event *session.Event) error

func (*FileService) Archive added in v0.0.23

func (s *FileService) Archive(_ context.Context, req *session.DeleteRequest) error

Archive moves the session directory under baseDir to archiveDir/yyyy/mm/dd/ using the current time. It removes the session from the in-memory cache.

func (*FileService) Compact

func (s *FileService) Compact(sessionID, appName, userID string, summarizer Summarizer, cfg CompactConfig) error

Compact checks if the session's events exceed the token threshold and, if so, summarizes older events using the provided summarizer function. The older events are replaced with a single summary event while recent events are preserved. The events file on disk is rewritten.

func (*FileService) Create

func (*FileService) CreateBranch

func (s *FileService) CreateBranch(sessionID, appName, userID, branchName string) error

CreateBranch creates a new branch from the current position in the session. The new branch starts with a copy of events up to the current head of the active branch.

func (*FileService) Delete

func (s *FileService) Delete(ctx context.Context, req *session.DeleteRequest) error

Delete archives the session (moves to archive/yyyy/mm/dd/) instead of deleting.

func (*FileService) EstimateTokens

func (s *FileService) EstimateTokens(sessionID, appName, userID string) (int, error)

EstimateTokens returns an approximate token count for a session's events. Uses a simple chars/4 heuristic.

func (*FileService) Get

func (*FileService) GetPlanContext added in v0.0.15

func (s *FileService) GetPlanContext(sessionID string) (*PlanContext, error)

GetPlanContext returns the plan context for the given session, or nil if not found.

func (*FileService) LastSessionID

func (s *FileService) LastSessionID(appName, userID string) string

LastSessionID returns the most recently updated session ID, or "" if none.

func (*FileService) List

func (*FileService) ListBranches

func (s *FileService) ListBranches(sessionID, appName, userID string) ([]BranchInfo, string, error)

ListBranches returns all branches for a session.

func (*FileService) SetSessionModel added in v0.0.23

func (s *FileService) SetSessionModel(sessionID, modelName string) error

SetSessionModel records the model name used by the agent for the given session and persists it to meta.json. Empty model names are normalized to UnknownModel so meta.json's "model" field is never blank.

func (*FileService) SwitchBranch

func (s *FileService) SwitchBranch(sessionID, appName, userID, branchName string) error

SwitchBranch switches the active branch for a session. The session's in-memory events are replaced with the branch's events.

func (*FileService) UpdatePlanContext added in v0.0.15

func (s *FileService) UpdatePlanContext(sessionID string, ctx *PlanContext) error

UpdatePlanContext updates the plan session context in the session metadata. Pass nil to clear the context.

type Meta

type Meta struct {
	ID          string       `json:"id"`
	AppName     string       `json:"appName"`
	UserID      string       `json:"userID"`
	WorkDir     string       `json:"workDir,omitempty"`
	Model       string       `json:"model"`
	CreatedAt   time.Time    `json:"createdAt"`
	UpdatedAt   time.Time    `json:"updatedAt"`
	PlanContext *PlanContext `json:"planContext,omitempty"`
}

Meta holds session metadata persisted in meta.json.

type PlanContext added in v0.0.15

type PlanContext struct {
	TaskName  string `json:"taskName,omitempty"`
	RoughIdea string `json:"roughIdea,omitempty"`
	SpecDir   string `json:"specDir,omitempty"`
	Phase     string `json:"phase,omitempty"`
}

PlanContext holds the /plan session context for resume.

type Summarizer

type Summarizer func(events []*session.Event) (string, error)

Summarizer is a function that takes a slice of events to be summarized and returns a summary text. This is typically backed by an LLM call.

var SimpleSummarizer Summarizer = func(events []*session.Event) (string, error) {
	return fmt.Sprintf("[Compacted %d events]", len(events)), nil
}

SimpleSummarizer is a basic summarizer that returns a placeholder summary. Useful for manual /compact invocations where no LLM is needed.

Jump to

Keyboard shortcuts

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