Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderDeliveryCursor = "session_delivery_cursor"

HeaderDeliveryCursor carries a confirmed delivery cursor in an envelope.

Variables

View Source
var (
	ErrSessionClosed   = errdefs.NotAvailablef("runtime session: session is closed")
	ErrPromptUnknown   = errdefs.NotFoundf("runtime session: prompt is unknown")
	ErrPromptDuplicate = errdefs.Conflictf("runtime session: prompt was already replied")
	ErrPromptClosed    = errdefs.NotAvailablef("runtime session: prompt is closed")
	ErrSinkQueueFull   = errdefs.BudgetExceededf("runtime session: sink queue is full")
)
View Source
var ErrManagerClosed = errdefs.NotAvailablef("runtime session: manager is closed")

Functions

func SubjectPromptRequested

func SubjectPromptRequested(runID string) event.Subject

SubjectPromptRequested returns the run-scoped prompt request subject.

Types

type AckMode

type AckMode string
const (
	// AckOnDelivery acknowledges a confirmed delivery only after Sink.OnDelta
	// returns nil. If the turn is interrupted while the callback is still
	// running, that delivery is outside the frozen acknowledged prefix.
	AckOnDelivery AckMode = ""
	// AckExplicit requires the authoritative sink to call Turn.Ack. The sink
	// may acknowledge the cursor currently offered to its OnDelta callback
	// before that callback returns.
	AckExplicit AckMode = "explicit"
)

type AskUserFunc

type AskUserFunc func(context.Context, agent.UserPrompt) (agent.UserReply, error)

AskUserFunc is the turn-scoped user-prompt callback supplied to a Host.

type Authority

type Authority string
const (
	AuthorityObserver      Authority = ""
	AuthorityAuthoritative Authority = "authoritative"
)

type BaseSessionObserver

type BaseSessionObserver struct{}

BaseSessionObserver provides no-op default implementations of every SessionObserver method.

func (BaseSessionObserver) OnSessionClosed

func (BaseSessionObserver) OnSessionClosed(*Session, error)

OnSessionClosed is a no-op.

func (BaseSessionObserver) OnSessionClosing

func (BaseSessionObserver) OnSessionClosing(*Session)

OnSessionClosing is a no-op.

func (BaseSessionObserver) OnSessionStarted

func (BaseSessionObserver) OnSessionStarted(*Session, *Turn)

OnSessionStarted is a no-op.

func (BaseSessionObserver) OnTurnFinished

func (BaseSessionObserver) OnTurnFinished(*Session, *Turn, *agent.Result, error)

OnTurnFinished is a no-op.

type CatalogProvider

type CatalogProvider interface {
	NewCatalog(ctx context.Context, instance *agent.Agent) (tool.Session, error)
}

CatalogProvider constructs the per-session tool catalog. The provider runs once per Session (on its first Start) and its result is attached to every turn's execution context, where session-scoped tools (tool_search) and the inference node's all_tools mode pick it up. The Session borrows the catalog.

The provider returns the session-scoped tool.Session (hosts typically use tool.Assembly.NewSession), so the dynamic injection view and the plain catalog share one contract.

type CatalogProviderFunc

type CatalogProviderFunc func(ctx context.Context, instance *agent.Agent) (tool.Session, error)

CatalogProviderFunc adapts a plain function to CatalogProvider.

func (CatalogProviderFunc) NewCatalog

func (f CatalogProviderFunc) NewCatalog(ctx context.Context, instance *agent.Agent) (tool.Session, error)

NewCatalog implements CatalogProvider.

type DeliveryCursor

type DeliveryCursor uint64

DeliveryCursor is a turn-global, contiguous confirmed-delivery position.

func DeliveryCursorFromEnvelope

func DeliveryCursorFromEnvelope(env event.Envelope) (DeliveryCursor, error)

DeliveryCursorFromEnvelope reads the confirmed delivery cursor.

type HostFactory

type HostFactory interface {
	NewHost(context.Context, HostRequest) (agent.Host, error)
}

HostFactory constructs a fresh Host for one turn.

type HostFactoryFunc

type HostFactoryFunc func(context.Context, HostRequest) (agent.Host, error)

HostFactoryFunc adapts a function to HostFactory.

func (HostFactoryFunc) NewHost

func (f HostFactoryFunc) NewHost(ctx context.Context, request HostRequest) (agent.Host, error)

NewHost implements HostFactory.

type HostRequest

type HostRequest struct {
	Key        Key
	RunID      string
	Interrupts <-chan agent.Interrupt
	AskUser    AskUserFunc
}

HostRequest contains the per-turn capabilities needed to construct a Host.

func (HostRequest) Validate

func (r HostRequest) Validate() error

Validate checks the required host-construction contract.

type InstanceResolver

type InstanceResolver interface {
	Instance(id string) (*agent.Agent, bool)
}

InstanceResolver is the minimum borrowed deployment view needed by Manager.

type Key

type Key struct {
	AgentID   string
	ContextID string
}

Key identifies one conversational session. AgentID is part of the identity: two agents using the same ContextID do not share session state.

func (Key) Validate

func (k Key) Validate() error

Validate rejects empty, whitespace-only, and padded identifiers.

type Lease

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

Lease keeps a shared Session retained by its Manager.

func (*Lease) Close

func (l *Lease) Close() error

Close releases this Lease. It is safe to call repeatedly or concurrently.

func (*Lease) Session

func (l *Lease) Session() *Session

Session returns the Session retained by this Lease.

type Manager

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

Manager shares Sessions by Key and reclaims unleased idle Sessions. It borrows its resolver, HostFactory, and event router and never closes them.

func NewManager

func NewManager(
	resolver InstanceResolver,
	hostFactory HostFactory,
	router *event.Router,
	options ...ManagerOption,
) (*Manager, error)

NewManager constructs a Session manager over borrowed runtime dependencies.

func (*Manager) Close

func (m *Manager) Close() error

Close stops reclamation, refuses new leases, and closes every Session. Borrowed deployment, router, and host dependencies remain owned by Runtime.

func (*Manager) GetOrCreate

func (m *Manager) GetOrCreate(ctx context.Context, key Key) (*Lease, error)

GetOrCreate lazily creates a Session and returns an independent Lease.

func (*Manager) Open

func (m *Manager) Open(ctx context.Context, key Key) (*Lease, error)

Open returns an independent Lease over the Session identified by key.

type ManagerOption

type ManagerOption func(*managerOptions) error

ManagerOption configures a Manager.

func WithCatalogProvider

func WithCatalogProvider(provider CatalogProvider) ManagerOption

WithCatalogProvider wires a per-session tool catalog provider. When set, every Session created by this Manager builds one catalog on its first Start, attaches it to each turn's execution context, and closes it when the Session closes.

func WithCheckpointStore

func WithCheckpointStore(store agent.CheckpointStore) ManagerOption

WithCheckpointStore wires the store used for end-to-end resume. It is required when WithResume is enabled.

func WithDeliveryConcurrency

func WithDeliveryConcurrency(limit int) ManagerOption

WithDeliveryConcurrency bounds how many sink callbacks may be in flight concurrently for one sink.

func WithIdleTimeout

func WithIdleTimeout(timeout time.Duration) ManagerOption

WithIdleTimeout sets how long an unleased, idle Session is retained.

func WithMaxSessions

func WithMaxSessions(limit int) ManagerOption

WithMaxSessions bounds the number of distinct live sessions the manager retains. Values <= 0 leave the manager default (1024).

func WithResume

func WithResume(enable bool) ManagerOption

WithResume enables session-level resume: each session key maps to a stable run id, checkpoints are loaded before Start, and committed checkpoints are deleted after completion.

func WithSessionObserver

func WithSessionObserver(observer SessionObserver) ManagerOption

WithSessionObserver attaches a session-level lifecycle observer to every Session created by this Manager. See SessionObserver for the callback contract.

func WithSinkBufferSize

func WithSinkBufferSize(size int) ManagerOption

WithSinkBufferSize sets the queue size used when SinkSpec.QueueSize is zero.

func WithSpeculativeBufferLimits

func WithSpeculativeBufferLimits(events, bytes int) ManagerOption

WithSpeculativeBufferLimits bounds aggregate pending confirmed branch data per turn.

type PromptRequested

type PromptRequested struct {
	RunID    string           `json:"run_id"`
	TurnID   string           `json:"turn_id"`
	PromptID string           `json:"prompt_id"`
	Prompt   agent.UserPrompt `json:"prompt"`
}

PromptRequested is published when a turn begins waiting for user input.

type Session

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

Session owns conversational activity for one Key while borrowing all deployment and event-routing dependencies from the runtime.

func (*Session) Key

func (s *Session) Key() Key

Key returns this Session's immutable identity.

func (*Session) Start

func (s *Session) Start(ctx context.Context, request agent.Request, sinks ...SinkSpec) (*Turn, error)

Start installs and asynchronously executes a new turn. A previous turn is interrupted and fully finalized before the replacement is constructed.

type SessionObserver

type SessionObserver interface {
	// OnSessionStarted fires after Start successfully scheduled a new turn
	// and before Start returns to its caller. turn is the running turn.
	OnSessionStarted(session *Session, turn *Turn)

	// OnSessionClosing fires exactly once when the Session begins closing,
	// covering explicit close, idle timeout reclamation, and Manager
	// shutdown. Active turns may still be finalizing at this point.
	OnSessionClosing(session *Session)

	// OnSessionClosed fires exactly once after closing completed and any
	// active turn has been shut down and awaited. err is the close error,
	// or nil on a clean close.
	OnSessionClosed(session *Session, err error)

	// OnTurnFinished fires when a turn reaches its terminal state.
	// result and err are the same values Turn.Wait returns.
	// It is called before Turn.Wait unblocks, so observers MUST NOT call
	// Turn.Wait from this callback.
	OnTurnFinished(session *Session, turn *Turn, result *agent.Result, err error)
}

SessionObserver observes session-level lifecycle transitions without affecting their outcome. It is the public hook for metrics, tracing, and accounting that wants the session view (Key, RunID, terminal outcome) rather than per-run agent events.

Concurrency contract:

  • Every method is called synchronously from the goroutine performing the transition, outside Session and Manager locks.
  • Implementations must return promptly and MUST NOT call back into Session or Manager methods; long-running side effects must be dispatched asynchronously by the observer itself.
  • Implementations must be safe for concurrent use: SessionStarted and TurnFinished can be called from a turn goroutine while Closing and Closed run on a close/reclaim goroutine.

Embed BaseSessionObserver to satisfy the interface with no-op defaults when only a subset of the lifecycle is interesting.

type SinkSpec

type SinkSpec struct {
	ID        string
	Sink      agent.StreamSink
	QueueSize int
	// DeliveryTimeout bounds each Sink.OnDelta call. Zero uses the 30-second
	// runtime default; a sink that exceeds the deadline is detached.
	DeliveryTimeout time.Duration
	OnDetach        func(error)
	Visibility      Visibility
	Authority       Authority
	// AckMode controls when confirmed authoritative deliveries become part of
	// the committable prefix. See AckOnDelivery and AckExplicit.
	AckMode    AckMode
	MaxUnacked int
}

SinkSpec describes one independently buffered stream attachment.

func (SinkSpec) Validate

func (s SinkSpec) Validate() error

Validate checks a sink before it is attached to a turn.

type Turn

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

Turn is one asynchronous execution owned by a Session.

func (*Turn) Ack

func (t *Turn) Ack(sinkID string, cursor DeliveryCursor) error

Ack cumulatively acknowledges confirmed deliveries from the authoritative explicit sink. It may be called from Sink.OnDelta for the cursor currently being offered to that callback.

func (*Turn) After

func (t *Turn) After(_ context.Context, _ agent.Identity, _ *agent.Request, res *agent.Result) (agent.Decision, error)

After implements the turn-scoped interruption referee.

func (*Turn) CommitView

func (t *Turn) CommitView(_ context.Context, _ agent.Identity, _ *agent.Request, res *agent.Result) (agent.CommitView, error)

CommitView implements the turn-scoped Committer projection.

func (*Turn) Interrupt

func (t *Turn) Interrupt(interrupt agent.Interrupt) error

Interrupt cooperatively asks the engine to stop. The first cause wins.

func (*Turn) OnInterrupt

func (t *Turn) OnInterrupt(context.Context, agent.Identity, agent.Interrupt)

OnInterrupt implements agent.Observer. Turn.finish owns logical completion.

func (*Turn) OnRunEnd

func (t *Turn) OnRunEnd(context.Context, agent.Identity, *agent.Result)

OnRunEnd implements agent.Observer. Turn.finish owns logical completion.

func (*Turn) OnRunRevise

func (t *Turn) OnRunRevise(
	_ context.Context,
	_ agent.Identity,
	_ *agent.Result,
	nextAttempt int,
)

OnRunRevise moves commit authority to the next attempt while preserving turn-global delivery and acknowledgement cursors.

func (*Turn) OnRunStart

func (t *Turn) OnRunStart(context.Context, agent.Identity, *agent.Request)

OnRunStart implements agent.Observer.

func (*Turn) Reply

func (t *Turn) Reply(ctx context.Context, promptID string, reply agent.UserReply) error

Reply supplies a response to one prompt without affecting other prompts.

func (*Turn) RunID

func (t *Turn) RunID() string

RunID returns the immutable root execution identifier.

func (*Turn) State

func (t *Turn) State() TurnState

State returns the current lifecycle state.

func (*Turn) Wait

func (t *Turn) Wait(ctx context.Context) (*agent.Result, error)

Wait waits for terminal completion without affecting execution.

type TurnState

type TurnState string

TurnState is the externally observable lifecycle state of a Turn.

const (
	TurnStarting     TurnState = "starting"
	TurnRunning      TurnState = "running"
	TurnInterrupting TurnState = "interrupting"
	TurnCompleted    TurnState = "completed"
	TurnInterrupted  TurnState = "interrupted"
	TurnCanceled     TurnState = "canceled"
	TurnFailed       TurnState = "failed"
	TurnAborted      TurnState = "aborted"
)

type Visibility

type Visibility string
const (
	VisibilityRaw       Visibility = ""
	VisibilityConfirmed Visibility = "confirmed"
)

Jump to

Keyboard shortcuts

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