Documentation
¶
Overview ¶
Package session implements the SessionDB — persistent per-project session tracking.
Stores raw events captured by hooks during a Claude Code session, session metadata, and resume snapshots.
Index ¶
- Constants
- func BuildResumeSnapshot(events []model.StoredEvent, opts *BuildSnapshotOpts) string
- func BuildSessionDirective(source string, events []model.StoredEvent) string
- func ExtractEvents(input model.HookInput) []model.SessionEvent
- func ExtractUserEvents(message string) []model.SessionEvent
- type BuildSnapshotOpts
- type GetEventsOpts
- type SessionDB
- func (sdb *SessionDB) CleanupOldSessions(maxAgeDays int) (int, error)
- func (sdb *SessionDB) Close()
- func (sdb *SessionDB) DBPath() string
- func (sdb *SessionDB) DeleteSession(sessionID string) error
- func (sdb *SessionDB) EnsureSession(sessionID, projectDir string) error
- func (sdb *SessionDB) EvictStaleSessions(maxAgeDays int) (int, error)
- func (sdb *SessionDB) GetActivity(sessionID string) (*model.SessionActivity, error)
- func (sdb *SessionDB) GetEventCount(sessionID string) (int, error)
- func (sdb *SessionDB) GetEvents(sessionID string, opts *GetEventsOpts) ([]model.StoredEvent, error)
- func (sdb *SessionDB) GetResume(sessionID string) (*model.ResumeRow, error)
- func (sdb *SessionDB) GetSessionStats(sessionID string) (*model.SessionMeta, error)
- func (sdb *SessionDB) IncrementCompactCount(sessionID string) error
- func (sdb *SessionDB) InsertEvent(sessionID string, event model.SessionEvent, sourceHook string) error
- func (sdb *SessionDB) MarkResumeConsumed(sessionID string) error
- func (sdb *SessionDB) TouchActivity(sessionID, appName, projectDir string, isQuery bool) error
- func (sdb *SessionDB) UpsertResume(sessionID, snapshot string, eventCount int) error
- type WorkspaceSessionMap
Constants ¶
const ( // MaxEventsPerSession before FIFO eviction kicks in. MaxEventsPerSession = 1000 // DedupWindow is the number of recent events to check for deduplication. DedupWindow = 5 )
Constants matching the TypeScript implementation.
const ( DefaultMaxBytes = 2048 MaxActiveFiles = 10 )
Budget constants for snapshot assembly.
Variables ¶
This section is empty.
Functions ¶
func BuildResumeSnapshot ¶
func BuildResumeSnapshot(events []model.StoredEvent, opts *BuildSnapshotOpts) string
BuildResumeSnapshot builds a resume snapshot XML string from stored session events.
Algorithm: 1. Group events by category 2. Render each section 3. Assemble by priority tier with budget trimming 4. If over maxBytes, drop lowest priority sections first
func BuildSessionDirective ¶ added in v0.9.0
func BuildSessionDirective(source string, events []model.StoredEvent) string
BuildSessionDirective builds an XML directive for compact/resume context injection. The source parameter indicates whether this is "compact", "resume", "startup", or "clear".
func ExtractEvents ¶
func ExtractEvents(input model.HookInput) []model.SessionEvent
ExtractEvents extracts session events from a PostToolUse hook input. Returns zero or more SessionEvents. Never panics.
func ExtractUserEvents ¶
func ExtractUserEvents(message string) []model.SessionEvent
ExtractUserEvents extracts session events from a user message. Handles: decision, role, intent, data categories.
Types ¶
type BuildSnapshotOpts ¶
BuildSnapshotOpts configures snapshot generation.
type GetEventsOpts ¶
GetEventsOpts configures event retrieval.
type SessionDB ¶
type SessionDB struct {
// contains filtered or unexported fields
}
SessionDB manages session events, metadata, and resume snapshots in SQLite.
func (*SessionDB) CleanupOldSessions ¶
CleanupOldSessions removes sessions older than maxAgeDays. Returns the count deleted.
func (*SessionDB) DeleteSession ¶
DeleteSession removes all data for a session (events, meta, resume).
func (*SessionDB) EnsureSession ¶
EnsureSession creates a session metadata entry if it doesn't exist (idempotent).
func (*SessionDB) EvictStaleSessions ¶ added in v0.6.0
EvictStaleSessions removes sessions (events, meta, resume, activity) that haven't been used in maxAgeDays. Returns the count evicted.
func (*SessionDB) GetActivity ¶ added in v0.6.0
func (sdb *SessionDB) GetActivity(sessionID string) (*model.SessionActivity, error)
GetActivity returns the activity row for a session.
func (*SessionDB) GetEventCount ¶
GetEventCount returns the total event count for a session.
func (*SessionDB) GetEvents ¶
func (sdb *SessionDB) GetEvents(sessionID string, opts *GetEventsOpts) ([]model.StoredEvent, error)
GetEvents retrieves events for a session with optional filtering.
func (*SessionDB) GetSessionStats ¶
func (sdb *SessionDB) GetSessionStats(sessionID string) (*model.SessionMeta, error)
GetSessionStats returns session metadata.
func (*SessionDB) IncrementCompactCount ¶
IncrementCompactCount increments the compact_count for a session.
func (*SessionDB) InsertEvent ¶
func (sdb *SessionDB) InsertEvent(sessionID string, event model.SessionEvent, sourceHook string) error
InsertEvent inserts a session event with deduplication and FIFO eviction.
Deduplication: skips if the same type + data_hash appears in the last DedupWindow events for this session.
Eviction: if session exceeds MaxEventsPerSession, evicts the lowest-priority (then oldest) event.
func (*SessionDB) MarkResumeConsumed ¶
MarkResumeConsumed marks the resume snapshot as consumed.
func (*SessionDB) TouchActivity ¶ added in v0.6.0
TouchActivity records a tool call or query for the given session, updating last_used_at.
type WorkspaceSessionMap ¶ added in v0.9.0
type WorkspaceSessionMap struct {
// contains filtered or unexported fields
}
WorkspaceSessionMap manages OpenClaw workspace-to-session mappings. It provides stable session IDs for workspace keys so that sessions persist across gateway restarts.
func NewWorkspaceSessionMap ¶ added in v0.9.0
func NewWorkspaceSessionMap(db *SessionDB) *WorkspaceSessionMap
NewWorkspaceSessionMap creates a WorkspaceSessionMap backed by the given SessionDB.
func (*WorkspaceSessionMap) RenameSession ¶ added in v0.9.0
func (w *WorkspaceSessionMap) RenameSession(oldKey, newKey string) error
RenameSession updates the workspace key for an existing mapping. This is used when a workspace is re-keyed (e.g., after gateway restart) but should continue using the same session.
func (*WorkspaceSessionMap) ResolveSession ¶ added in v0.9.0
func (w *WorkspaceSessionMap) ResolveSession(workspaceKey, projectDir string) (string, error)
ResolveSession returns a stable session ID for a workspace key. If no mapping exists, a new session ID is generated from the workspace key and project directory, and the mapping is persisted.