Documentation
¶
Overview ¶
Package session owns agent-session lifecycle.
It is the sole authority over session creation, resumption, kind/channel policy, main-session resolution, review candidate selection, and the conversion from a validated session record to a memory operation scope.
Info is a session-domain type, not an alias to memory.SessionInfo. Every use boundary fails closed: the persistence mapping (Record / InfoFromRecord) and the memory-scope conversion (MemoryScope) validate the session invariant and return an error rather than emit an unchecked value. The memory scope is produced only by MemoryScope; production code must not hand-build memory.Session values.
Index ¶
- Variables
- type Channel
- type Info
- type Kind
- type ListOptions
- type MainRequest
- type Registry
- func (r *Registry) Archive(ctx context.Context, scope Scope, id string) error
- func (r *Registry) Ensure(ctx context.Context, req Request) (Info, error)
- func (r *Registry) Get(ctx context.Context, scope Scope, id string) (Info, error)
- func (r *Registry) List(ctx context.Context, scope Scope, opts ListOptions) ([]Info, error)
- func (r *Registry) ListForReview(ctx context.Context, req ReviewRequest) ([]Info, error)
- func (r *Registry) MemoryScope(info Info) (memory.Session, error)
- func (r *Registry) ResolveMain(ctx context.Context, req MainRequest) (Info, error)
- type Request
- type ReviewPolicy
- type ReviewRequest
- type Scope
Constants ¶
This section is empty.
Variables ¶
var ErrArchived = errors.New("session is archived")
ErrArchived is returned when an attempt is made to write to an archived session.
var ErrForbidden = errors.New("session access forbidden")
ErrForbidden is returned when a caller attempts to access a session they do not own.
var ErrNotFound = errors.New("session not found")
ErrNotFound is returned when a session cannot be located.
var ErrWrongKind = errors.New("session kind mismatch")
ErrWrongKind is returned when a resumed session has a different kind than required.
Functions ¶
This section is empty.
Types ¶
type Info ¶
type Info struct {
ID string
AgentID string
UserID string
GroupID string
Channel string
Kind string
ProjectID string
Title string
CreatedAt time.Time
LastActive time.Time
Archived bool
// LatestSeq is populated by review listings and is not session metadata.
LatestSeq int64
}
Info holds validated metadata about an agent session. It is a session-domain value distinct from the memory-layer persistence type memory.SessionInfo; callers cross the persistence boundary through Record and InfoFromRecord.
GroupID identifies the group a group session belongs to. It is now durable (ctx_conversation.group_id) and load/save round-trip it. The durable invariant is that a group session is owned by the group itself: UserID equals GroupID, and GroupID is the canonical ctx_group_state UUID. That equality is what makes group read, write, and compaction resolve one memory partition; it is enforced by Validate at every use boundary rather than left as an undeclared convention.
func InfoFromRecord ¶ added in v0.60.0
func InfoFromRecord(r memory.SessionInfo) (Info, error)
InfoFromRecord maps a memory-layer persistence record back to a session-domain Info. It is the persistence→domain half of the session store boundary and the only sanctioned way to turn an unvalidated memory.SessionInfo into an Info; it validates the resulting Info and fails closed on a record that violates the session invariant.
func (Info) MemoryScope ¶ added in v0.60.0
MemoryScope is the single canonical conversion from a validated session Info to a memory.Session operation scope. It fails closed: an Info that violates the session invariant yields an error, never a half-formed scope.
For a private session, read (Assemble), write (Append/Bootstrap), and compaction all derive their scope here and land on the same partition. A group session shares one durable canonical scope for read and write (partition keyed on session/user/agent, with GroupID selecting the group assembly/isolation path); group compaction is a separate matter and is unsupported (rejected by agent.Service.CompactSession), because group history is assembled from the group event log rather than the LCM conversation.
func (Info) Record ¶ added in v0.60.0
func (i Info) Record() (memory.SessionInfo, error)
Record maps a validated session-domain Info onto the memory-layer persistence type. It is the domain→persistence half of the session store boundary and fails closed on an invalid Info.
func (Info) Validate ¶ added in v0.60.0
Validate enforces the durable session-scope invariant. Every session needs an ID, an agent, and a user owner. A group session is owned by its group: UserID must equal GroupID and GroupID must be a canonical ctx_group_state UUID. This is the single fail-closed gate the persistence and memory-scope boundaries run.
type ListOptions ¶
type ListOptions struct {
// Kinds filters by session kind; empty = all kinds.
Kinds []Kind
// IncludeArchived includes archived sessions.
IncludeArchived bool
// ExcludeInternal omits task/delegate worker sessions from human lists.
ExcludeInternal bool
// ProjectID filters to one project when non-empty.
ProjectID string
Limit int
Offset int
}
ListOptions controls session listing.
type MainRequest ¶
MainRequest describes a main-session resolution request.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is the sole owner of agent-session lifecycle. It creates, resumes, lists, and archives sessions; it also converts validated session records into memory operation scopes.
Registry has no knowledge of LLM execution, runners, or tools.
func NewRegistry ¶
NewRegistry creates a Registry backed by the given memory.Provider. mem must implement memory.SessionManager; if it does not, an error is returned. agentID may be empty for a cross-agent registry.
func NewRegistryWithStore ¶
NewRegistryWithStore creates a Registry with an explicit store (for testing).
func (*Registry) Ensure ¶
Ensure finds or creates a session according to the request policy.
- If req.ID is set and the session exists: validates kind and user, returns it. - If req.ID is set but missing and AllowExactIDCreate: creates with that ID. - If req.ID is empty and CreateIfMissing: generates a new ID and creates. - Otherwise: returns ErrNotFound.
func (*Registry) Get ¶
Get returns session metadata by ID. The session must belong to the given scope (userID + agentID match).
func (*Registry) ListForReview ¶
ListForReview returns sessions that are candidates for reflect review.
func (*Registry) MemoryScope ¶
MemoryScope converts a validated session Info into a memory.Session scope. This is the ONLY authorised way to produce memory.Session for production agent sessions; it delegates to the canonical Info.MemoryScope conversion. Private read, write, and compaction resolve one partition; group read and write share the durable canonical group scope (group compaction is unsupported, so MemoryScope makes no claim about it). It fails closed on an invalid Info.
func (*Registry) ResolveMain ¶
ResolveMain returns the main session for a user+agent pair. It first looks for an existing main-kind session, then promotes the most recent candidate, and finally creates a new one if CreateIfMissing is set.
type Request ¶
type Request struct {
// ID is the exact session ID to resume. If empty, a new ID is generated.
ID string
// UserID and AgentID are required for user-scoped operations.
UserID string
AgentID string
// GroupID marks this as a group session owned by the group. When set it is
// the canonical ctx_group_state UUID and must equal UserID; it is persisted
// so the group identity survives reload. Empty for private sessions.
GroupID string
// ProjectID scopes the session to a project when non-empty.
ProjectID string
// Kind is the session kind to use when creating a new session.
// If the session already exists, it must match RequireKind when set.
Kind Kind
// Channel is the originating channel (e.g. ChannelWeb, ChannelDelegate).
Channel Channel
// Title is optional; pool_chat auto-derives it from the first message.
Title string
// CreateIfMissing creates a new session if none matches.
CreateIfMissing bool
// AllowExactIDCreate allows creating a new session with the explicit ID
// when that ID does not yet exist. Required for delegate session persistence.
AllowExactIDCreate bool
// RequireKind enforces that a resumed session must have this kind.
// Empty means any kind is acceptable.
RequireKind Kind
}
Request describes what session to find or create.
type ReviewPolicy ¶
type ReviewPolicy struct {
// ExcludeKinds lists session kinds to skip.
ExcludeKinds []Kind
// RequirePrivateMain, when true, includes only sessions on private user channels.
RequirePrivateMain bool
}
ReviewPolicy controls which sessions are included in reflect review.
func DefaultReviewPolicy ¶
func DefaultReviewPolicy() ReviewPolicy
DefaultReviewPolicy returns the policy used by the reflect system. Delegate, task, and scheduler sessions are excluded by default.
func (ReviewPolicy) Includes ¶
func (p ReviewPolicy) Includes(kind Kind) bool
Includes reports whether a session with the given kind passes this policy.
func (ReviewPolicy) IsZero ¶
func (p ReviewPolicy) IsZero() bool
IsZero reports whether no review policy was supplied.
type ReviewRequest ¶
type ReviewRequest struct {
AgentID string
// Policy controls inclusion/exclusion rules.
Policy ReviewPolicy
}
ReviewRequest describes which sessions are candidates for reflect review.