postgres

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package postgres holds a complete Storage over Postgres (§3.2): the production storage adapter, the Go stand-in for the TypeScript package's PrismaStorage.

It owns its tables, prefixed agentenkit_ by default because a production database is usually shared with the caller's own schema. Hand it any *sql.DB opened with a Postgres driver; the package never imports one.

Index

Constants

View Source
const NotifyChannel = "agentenkit_events"

NotifyChannel is the one Postgres channel every thread's events travel on. LISTEN is per connection, not per channel-pattern, so one channel and an in-process fan-out by thread id costs one connection per process however many threads it watches.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus is an EventBus over LISTEN/NOTIFY.

At-most-once by contract, so a dropped notification is fine; the durable log is the record. What the adapter must get right is the size cap: a CHUNK carrying a tool result can exceed 8000 bytes, and NOTIFY refuses it. Such an event is sent as {threadId, seq} and the subscriber reads it back from Storage.Events; a notice (seq 0) that is too large is parked in the kv under a short TTL and referenced by key.

func NewBus

func NewBus(db *sql.DB, listener Listener, events ports.EventStore, kv *Kv, opts BusOptions) *Bus

NewBus builds the bus. Publishing goes through db (pg_notify); listening through listener. events resolves oversized durable frames and kv parks oversized notices; pass the storage's Events() and the NewKv Kv.

func (*Bus) Publish

func (b *Bus) Publish(ctx context.Context, threadID string, event ports.AgentEvent) error

func (*Bus) Subscribe

func (b *Bus) Subscribe(ctx context.Context, threadID string, handler func(ports.AgentEvent)) (func() error, error)

type BusOptions

type BusOptions struct {
	// Heartbeat is the interval of the bus-only HEARTBEAT notice each
	// subscription emits. Zero means one minute.
	Heartbeat time.Duration
}

BusOptions tunes NewBus.

type Handler

type Handler func(ctx context.Context, job ports.RunJob) error

Handler is what the queue hands a job to: the runtime's worker.

type Kv

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

Kv is a Kv over Postgres: the hot state, the run locks, the HITL handoff keys and the per-thread seq counters (§3.4), in one table beside the storage tables so a single database runs the whole platform.

Expiry is enforced on read. SET NX and Incr are single statements, so two workers can never both win a lock or both take the same seq.

func NewKv

func NewKv(ctx context.Context, db *sql.DB, opts ...Option) (*Kv, error)

NewKv creates the table if it is missing and returns the Kv. The prefix matches the storage's (default "agentenkit_").

func (*Kv) Del

func (k *Kv) Del(ctx context.Context, key string) error

func (*Kv) DeleteExpired

func (k *Kv) DeleteExpired(ctx context.Context) (int64, error)

DeleteExpired drops rows past their expiry. Expiry is already enforced on read, so this is housekeeping: call it from a periodic job.

func (*Kv) Get

func (k *Kv) Get(ctx context.Context, key string) (string, bool, error)

func (*Kv) Incr

func (k *Kv) Incr(ctx context.Context, key string) (int64, error)

func (*Kv) Set

func (k *Kv) Set(ctx context.Context, key, value string, opts ports.SetOptions) (bool, error)

type Listener

type Listener interface {
	// Listen subscribes to channel and calls handler for every notification
	// until ctx ends. It reconnects on its own; a payload lost while it was
	// down is recovered by the client's cursor replay, which is what an
	// at-most-once bus is for (§2.2).
	Listen(ctx context.Context, channel string, handler func(payload string)) error
}

Listener is what the bus needs from a driver: one long-lived LISTEN connection that hands over every payload on a channel. The package never imports a driver; pgxlisten implements this over pgx.

type Option

type Option func(*Storage)

Option tunes New.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets the table prefix. Default: "agentenkit_".

type Queue

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

Queue is a durable Queue over one Postgres table.

Enqueue is one INSERT; a delay is a future runAt. The consumer claims with SELECT ... FOR UPDATE SKIP LOCKED, so several processes can consume the same table, and each claim carries a lease that is renewed while the job runs and lapses when its worker dies. At-least-once: a job whose worker crashed is redelivered once its lease expires, and the engine's run lock makes the duplicate a no-op.

func NewQueue

func NewQueue(ctx context.Context, db *sql.DB, opts QueueOptions, storageOpts ...Option) (*Queue, error)

NewQueue creates the table if it is missing and returns the queue. Nothing is consumed until Bind.

func (*Queue) Bind

func (q *Queue) Bind(handler Handler)

Bind wires the worker and starts consuming. Call once; Close stops it.

func (*Queue) Close

func (q *Queue) Close()

Close stops the consumer and waits for running jobs to return.

func (*Queue) Enqueue

func (q *Queue) Enqueue(ctx context.Context, job ports.RunJob, opts *ports.EnqueueOptions) error

func (*Queue) Pending

func (q *Queue) Pending(ctx context.Context) (int, error)

Pending counts jobs not yet taken (ready or delayed). For tests and dashboards.

type QueueOptions

type QueueOptions struct {
	// Poll is how often an idle consumer looks for work. Zero means 500ms.
	Poll time.Duration
	// Lease is how long a claimed job stays invisible to other consumers. It
	// is renewed while the handler runs, so it only needs to outlast one
	// renewal gap plus a crash. Zero means 2 minutes.
	Lease time.Duration
	// Concurrency is how many jobs one consumer runs at once. Zero means 4.
	// The per-thread run lock keeps two jobs on one thread apart regardless.
	Concurrency int
	// MaxAttempts is how many deliveries a job gets before it is dropped as
	// dead. The engine's own §2.8 policy re-enqueues a run that fails; this
	// only guards against a handler that keeps dying. Zero means 5.
	MaxAttempts int
}

QueueOptions tunes NewQueue.

type Storage

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

Storage is a Storage over Postgres.

func New

func New(ctx context.Context, db *sql.DB, opts ...Option) (*Storage, error)

New creates the tables if they are missing, then returns the storage.

func (*Storage) Events

func (s *Storage) Events() ports.EventStore

func (*Storage) Messages

func (s *Storage) Messages() ports.MessageStore

func (*Storage) Threads

func (s *Storage) Threads() ports.ThreadStore

func (*Storage) Usage

func (s *Storage) Usage() ports.UsageStore

Directories

Path Synopsis
Package pgxlisten is the postgres bus's Listener over pgx v5: one dedicated connection that LISTENs and hands every notification over.
Package pgxlisten is the postgres bus's Listener over pgx v5: one dedicated connection that LISTENs and hands every notification over.

Jump to

Keyboard shortcuts

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