Documentation
¶
Overview ¶
Package postgres keeps sessions durable in Postgres: the session Store (checkpoints and tool-call bodies), SecretStorage (the users' delegated tokens, sealed before they are written) and the EventLog.
The host owns the connection pool and passes it in; the three share it, and can share it with brain/postgres. Every table is qualified by the schema, so the pool needs no search_path. Run DB.Setup on each deploy.
Index ¶
- Constants
- func Configure(cfg *pgxpool.Config)
- type DB
- type EventLog
- func (l *EventLog) Append(ctx context.Context, sessionID session.ID, topic string, ev session.Event) error
- func (l *EventLog) Close()
- func (l *EventLog) CloseSession(ctx context.Context, sessionID session.ID) error
- func (l *EventLog) Head(ctx context.Context, sessionID session.ID) (session.Seq, error)
- func (l *EventLog) Subscribe(ctx context.Context, sessionID session.ID, after session.Seq) (<-chan session.Event, error)
- func (l *EventLog) Trim(ctx context.Context, sessionID session.ID, through session.Seq) error
- type Options
- type Secrets
- type Store
- func (s *Store) Calls(ctx context.Context, sessionID session.ID, callKeys []string) (map[string][]byte, error)
- func (s *Store) ClearCalls(ctx context.Context, sessionID session.ID) error
- func (s *Store) Delete(ctx context.Context, sessionID session.ID) error
- func (s *Store) Load(ctx context.Context, sessionID session.ID) ([]session.CheckpointEntry, session.Revision, error)
- func (s *Store) PutCall(ctx context.Context, sessionID session.ID, callKey string, body []byte) error
- func (s *Store) Revision(ctx context.Context, sessionID session.ID) (session.Revision, error)
- func (s *Store) Save(ctx context.Context, sessionID session.ID, changes []session.CheckpointChange, ...) (session.Revision, error)
Constants ¶
const DefaultSchema = "tacklr"
DefaultSchema holds the tables when Options.Schema is empty. It is the schema brain/postgres uses too.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the session tables in one schema of the host's pool.
type EventLog ¶
type EventLog struct {
// contains filtered or unexported fields
}
EventLog is session.EventLog on Postgres. Each session's events are numbered by a counter row that an Append bumps and writes from in one statement, so sequences have no gaps. An Append also NOTIFYs the session's id (never the event: a NOTIFY payload is at most 8000 bytes), and one listening connection per EventLog wakes the subscribers, which then read what they have not seen. Close the EventLog to stop its listener.
func (*EventLog) Append ¶
func (l *EventLog) Append(ctx context.Context, sessionID session.ID, topic string, ev session.Event) error
Append implements session.EventLog.
func (*EventLog) Close ¶
func (l *EventLog) Close()
Close stops the listener and ends every tail of this EventLog.
func (*EventLog) CloseSession ¶
CloseSession implements session.EventLog. It deletes the session's log, so a later session can reuse the id, and ends its tails in every process.
func (*EventLog) Subscribe ¶
func (l *EventLog) Subscribe(ctx context.Context, sessionID session.ID, after session.Seq) (<-chan session.Event, error)
Subscribe implements session.EventLog. It replays the events after after, then tails: each subscriber reads at its own pace, so a slow one misses nothing and never blocks Append. Retry entries are not sent. The channel closes when ctx ends, the session is closed, or the EventLog is.
type Options ¶
type Options struct {
// Schema holds the session tables. Default DefaultSchema.
Schema string
}
Options configures New. Zero fields take the defaults.
type Secrets ¶
type Secrets struct {
// contains filtered or unexported fields
}
Secrets is session.SecretStorage on Postgres for the session-scoped tokens a host passes on CreateSession, Prompt and Resume (an end user's Drive or Graph OAuth tokens, per-session object-store credentials). Each session's bag is sealed before it is written, so the database never holds a plaintext token. Service-to-service credentials do not belong here: the host passes those to the providers it builds.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is session.Store on Postgres: one row per checkpoint row, and one per session whose revision a Save compares and bumps in the transaction that writes its changes.
func (*Store) Calls ¶
func (s *Store) Calls(ctx context.Context, sessionID session.ID, callKeys []string) (map[string][]byte, error)
Calls implements session.Store in one query for every key.
func (*Store) ClearCalls ¶
ClearCalls implements session.Store.
func (*Store) Delete ¶
Delete implements session.Store: the checkpoint and every call go in one statement.
func (*Store) Load ¶
func (s *Store) Load(ctx context.Context, sessionID session.ID) ([]session.CheckpointEntry, session.Revision, error)
Load implements session.Store: the revision and the rows read in one snapshot of the database.
func (*Store) PutCall ¶
func (s *Store) PutCall(ctx context.Context, sessionID session.ID, callKey string, body []byte) error
PutCall implements session.Store: a Put of a key replaces it.
func (*Store) Save ¶
func (s *Store) Save(ctx context.Context, sessionID session.ID, changes []session.CheckpointChange, expected session.Revision) (session.Revision, error)
Save implements session.Store. It writes only when the checkpoint is at expected ("" when there is none yet), so of two saves from one revision exactly one wins and the other gets session.ErrStaleCheckpoint. The revision and the changes commit in one transaction.