durableobject

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package durableobject runs Tacklr sessions as durable objects: one object per session, each with its own ordered storage, alarm and address, as a Cloudflare Durable Object has. The platform calls the session's Object for every request and alarm; Client is the session.Client hosts call.

An object can stop at any moment and lose everything but its storage. Its session survives by journal replay: the object records the result of every orchestrator.Runtime call, and a restarted object runs orchestrator.Run again and answers from the journal until it reaches the first step that never finished. Wakes go into a mailbox in storage first, so none is lost, and an alarm restarts an object whose step stopped with it.

The package is plain Go. A platform implements Host for each object and Platform for reaching the others; package cloudflare does it on Workers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(ref Ref) http.Handler

Handler serves ref over the object protocol.

Types

type Client

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

Client implements session.Client over a Platform: each call goes to the session's object. Credentials travel to the object, which seals them; state is checked here, so a bad value fails the caller.

func NewClient

func NewClient(p Platform) *Client

NewClient returns a Client over p. It panics without p.

func (*Client) Cancel

func (c *Client) Cancel(ctx context.Context, id session.ID) error

Cancel implements session.Client.

func (*Client) Close

func (c *Client) Close(ctx context.Context, id session.ID) error

Close implements session.Client: the session's object closes its jobs and deletes what it saved. Closing a closed session is not an error.

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, req session.CreateSession) (session.ID, error)

CreateSession implements session.Client.

func (*Client) Head

func (c *Client) Head(ctx context.Context, id session.ID) (session.Seq, error)

Head implements session.Client.

func (*Client) Jobs

func (c *Client) Jobs(ctx context.Context, parent session.ID) ([]session.Status, error)

Jobs implements session.Client: the status of each job the session runs, in start order.

func (*Client) Prompt

func (c *Client) Prompt(ctx context.Context, id session.ID, msg session.Prompt) error

Prompt implements session.Client.

func (*Client) Resume

func (c *Client) Resume(ctx context.Context, id session.ID, resume session.Resume) error

Resume implements session.Client.

func (*Client) Status

func (c *Client) Status(ctx context.Context, id session.ID) (session.Status, error)

Status implements session.Client.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, id session.ID, after session.Seq) (session.Subscription, error)

Subscribe implements session.Client.

func (*Client) WireStore

func (c *Client) WireStore() WireStore

WireStore is the protocol wire store (server.ProtocolWireStore) of the sessions c reaches: each session's object keeps the session's wire state, and the session's close deletes it.

type Config

type Config struct {
	// Agent is what every session runs; a specialist session runs its
	// specialist on it.
	Agent tacklr.AgentOptions
	// Sealer seals each session's credentials before they are stored.
	Sealer session.Sealer
	// Jobs are the named jobs a session can start.
	Jobs map[string]session.JobHandler
	// MaxToolCalls bounds how many tool calls of one reply run at once
	// (default 32).
	MaxToolCalls int
	// MaxChildren bounds a session's running children (default 1000).
	MaxChildren int
	// StepAttempts is how many times a step that failed on the network
	// runs (default 3).
	StepAttempts int
	// RetryBackoff is the first wait before a step, or the delivery of a
	// message to another object, runs again; it doubles up to 30 seconds
	// (default one second).
	RetryBackoff time.Duration
	// Watchdog is how long the alarm waits before it restarts a session
	// whose step stopped with its object (default one minute).
	Watchdog time.Duration
}

Config is what every session object of a deployment runs with.

type Entry

type Entry struct {
	Key   string
	Value []byte
}

Entry is one stored key and its value.

type Host

type Host interface {
	Storage
	Platform
	// SetAlarm asks the platform to call Object.Alarm at t. The zero time
	// clears the alarm.
	SetAlarm(t time.Time) error
}

Host is what the platform gives one object: its storage, its alarm, and the other objects.

type Init

type Init struct {
	SessionID session.ID
	// Trace is the context of the parent turn that started a task session:
	// the child's turn is part of the parent's trace.
	Trace      telemetry.Carrier `json:",omitempty"`
	Specialist string
	// Worker names the JobHandler a job session runs.
	Worker     string
	Parent     session.ID
	Mounts     []session.MountRecipe
	MCPServers []session.MCPServer
	State      map[string]any
	// Task is a child's prompt: the session runs that one turn and ends.
	Task string
	// Auth is the credentials the session starts with; a child gets its
	// parent's. The object seals them and keeps them out of its journal.
	Auth session.AuthContext
}

Init is what a session starts from: who it is and, for a child, its task.

type MemStorage

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

MemStorage is a Storage in memory, for tests and local runs.

func NewMemStorage

func NewMemStorage() *MemStorage

NewMemStorage returns an empty MemStorage.

func (*MemStorage) Get

func (s *MemStorage) Get(key string) ([]byte, bool, error)

Get implements Storage.

func (*MemStorage) Keys

func (s *MemStorage) Keys() []string

Keys is every key the storage holds, in order.

func (*MemStorage) Scan

func (s *MemStorage) Scan(prefix, after string, limit int) ([]Entry, error)

Scan implements Storage.

func (*MemStorage) Write

func (s *MemStorage) Write(ops ...Op) error

Write implements Storage.

type Object

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

Object runs one session in one durable object. The platform makes one per object and calls it for every request and alarm. It implements Ref.

A live object holds its state in memory: the session's meta, its mailbox, its status, its outbox and its stores' heads. It reads storage once, when it starts, and writes every change to storage before it changes memory, so memory never holds what storage does not.

func NewObject

func NewObject(host Host, cfg Config) *Object

NewObject returns the object over host. It panics without cfg.Sealer.

func (*Object) Alarm

func (o *Object) Alarm(context.Context) error

Alarm is the platform's alarm: it restarts a session whose object stopped while a step ran, and delivers what the object still owes other objects.

func (*Object) Create

func (o *Object) Create(ctx context.Context, init Init) error

Create implements Ref.

func (*Object) Head

func (o *Object) Head(ctx context.Context) (session.Seq, error)

Head implements Ref.

func (*Object) Idle

func (o *Object) Idle(ctx context.Context) error

Idle returns once the object runs nothing: no session goroutine, and no delivery to another object under way (one that waits for its retry is the alarm's). A platform keeps the object alive until then (on Workers, waitUntil).

func (*Object) Jobs

func (o *Object) Jobs(context.Context) ([]session.ID, error)

Jobs implements Ref.

func (*Object) SetWire

func (o *Object) SetWire(_ context.Context, raw []byte) error

SetWire implements Ref.

func (*Object) Status

func (o *Object) Status(context.Context) (session.Status, error)

Status implements Ref: the status of the session's last write, from memory, without running the session.

func (*Object) Stop

func (o *Object) Stop()

Stop ends what the object runs: its steps, and its deliveries. The platform calls it when it takes the object away; a new object over the same storage goes on from there.

func (*Object) Subscribe

func (o *Object) Subscribe(ctx context.Context, after session.Seq) (session.Subscription, error)

Subscribe implements Ref.

func (*Object) Wake

func (o *Object) Wake(ctx context.Context, w session.Wake) error

Wake implements Ref. Credentials on a prompt or resume are sealed into the object's secrets; the mailbox, and so the journal, gets the wake without them. A close of an ended session deletes what it left.

func (*Object) Wire

func (o *Object) Wire(context.Context) ([]byte, error)

Wire implements Ref.

type Op

type Op struct {
	Key    string
	Value  []byte
	Delete bool
}

Op is one write: a put of Value at Key, or a delete when Delete is set.

type Platform

type Platform interface {
	Object(id session.ID) Ref
}

Platform reaches session objects by id. An object starts on first use.

type Ref

type Ref interface {
	// Create starts the session. It is session.ErrExists when the object
	// already holds one.
	Create(ctx context.Context, init Init) error
	// Wake queues w for the session. It is session.ErrNotFound when the
	// object holds no session, or one that ended.
	Wake(ctx context.Context, w session.Wake) error
	Status(ctx context.Context) (session.Status, error)
	// Jobs is the session's running jobs, in start order.
	Jobs(ctx context.Context) ([]session.ID, error)
	Head(ctx context.Context) (session.Seq, error)
	Subscribe(ctx context.Context, after session.Seq) (session.Subscription, error)
	// Wire is the wire state a protocol server keeps with the session
	// (the payload of server.ProtocolWireStore). It is session.ErrNotFound
	// when the object holds no session, or no wire state.
	Wire(ctx context.Context) ([]byte, error)
	// SetWire replaces the session's wire state; nil deletes it. A close
	// of the session deletes it too.
	SetWire(ctx context.Context, raw []byte) error
}

Ref is a handle on one session's object, wherever it runs: *Object is one, and a platform's stub for a remote object is another.

func Remote

func Remote(c *http.Client) Ref

Remote is a Ref over the object protocol: c's Transport delivers each request to the one object it reaches. A platform's Platform.Object returns one per object.

type Registry

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

Registry holds the live objects of a process that hosts many (on Workers, one Go program serves every object of its isolate). It routes each request to its object by id, makes a new object when the platform starts a new instance of one (after a reset), and keeps at most a bound of objects that are idle and serve nothing, dropping the least recently used: a dropped object starts again from its storage on its next request.

func NewRegistry

func NewRegistry(limit int) *Registry

NewRegistry returns an empty Registry that keeps at most limit idle objects (zero or less: 64).

func (*Registry) Acquire

func (r *Registry) Acquire(id string, instance int, newObject func() *Object) (obj *Object, release func())

Acquire is id's object for one request of the platform's instance (a number the platform gives each instance of an object). It is made with newObject when the registry holds none for id, or holds one of an older instance, which it stops. Call release once the request, and any stream it answers with, ends.

type Storage

type Storage interface {
	// Get is the value at key; ok is false when there is none.
	Get(key string) (value []byte, ok bool, err error)
	// Scan is the entries whose keys start with prefix and sort after after
	// (an empty after is the start), in key order: at most limit of them,
	// or all when limit is zero.
	Scan(prefix, after string, limit int) ([]Entry, error)
	// Write applies ops in order, all of them or none.
	Write(ops ...Op) error
}

Storage is one object's durable key-value storage, ordered by key. The object is its only writer.

type WireStore

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

WireStore keeps each session's protocol wire state in its object.

func (WireStore) Delete

func (s WireStore) Delete(ctx context.Context, sessionID string) error

Delete implements server.ProtocolWireStore. A session that is not there has no wire state to delete.

func (WireStore) Get

func (s WireStore) Get(ctx context.Context, sessionID string) ([]byte, error)

Get implements server.ProtocolWireStore: session.ErrNotFound when the session or its wire state is not there.

func (WireStore) Put

func (s WireStore) Put(ctx context.Context, sessionID string, payload []byte) error

Put implements server.ProtocolWireStore. The session must exist.

Directories

Path Synopsis
Package cloudflare runs Tacklr sessions on Cloudflare Workers: each session is a SQLite-backed Durable Object that runs a durableobject.Object, and the Worker serves the host's own routes over a session.Client of those objects.
Package cloudflare runs Tacklr sessions on Cloudflare Workers: each session is a SQLite-backed Durable Object that runs a durableobject.Object, and the Worker serves the host's own routes over a session.Client of those objects.
Package dotest runs durable object sessions in memory, for tests: a host's tests of its agent on the durable object runtime, and the runtime's own.
Package dotest runs durable object sessions in memory, for tests: a host's tests of its agent on the durable object runtime, and the runtime's own.
internal
inproc
Package inproc serves an http.Handler in this process, without a network: the response streams through a pipe as the handler writes it.
Package inproc serves an http.Handler in this process, without a network: the response streams through a pipe as the handler writes it.

Jump to

Keyboard shortcuts

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