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 ¶
- func Handler(ref Ref) http.Handler
- type Client
- func (c *Client) Cancel(ctx context.Context, id session.ID) error
- func (c *Client) Close(ctx context.Context, id session.ID) error
- func (c *Client) CreateSession(ctx context.Context, req session.CreateSession) (session.ID, error)
- func (c *Client) Head(ctx context.Context, id session.ID) (session.Seq, error)
- func (c *Client) Jobs(ctx context.Context, parent session.ID) ([]session.Status, error)
- func (c *Client) Prompt(ctx context.Context, id session.ID, msg session.Prompt) error
- func (c *Client) Resume(ctx context.Context, id session.ID, resume session.Resume) error
- func (c *Client) Status(ctx context.Context, id session.ID) (session.Status, error)
- func (c *Client) Subscribe(ctx context.Context, id session.ID, after session.Seq) (session.Subscription, error)
- func (c *Client) WireStore() WireStore
- type Config
- type Entry
- type Host
- type Init
- type MemStorage
- type Object
- func (o *Object) Alarm(context.Context) error
- func (o *Object) Create(ctx context.Context, init Init) error
- func (o *Object) Head(ctx context.Context) (session.Seq, error)
- func (o *Object) Idle(ctx context.Context) error
- func (o *Object) Jobs(context.Context) ([]session.ID, error)
- func (o *Object) SetWire(_ context.Context, raw []byte) error
- func (o *Object) Status(context.Context) (session.Status, error)
- func (o *Object) Stop()
- func (o *Object) Subscribe(ctx context.Context, after session.Seq) (session.Subscription, error)
- func (o *Object) Wake(ctx context.Context, w session.Wake) error
- func (o *Object) Wire(context.Context) ([]byte, error)
- type Op
- type Platform
- type Ref
- type Registry
- type Storage
- type WireStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 (*Client) Close ¶
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 ¶
CreateSession implements session.Client.
func (*Client) Jobs ¶
Jobs implements session.Client: the status of each job the session runs, in start order.
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 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 (*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.
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 (*Object) Alarm ¶
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) Idle ¶
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) Status ¶
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.
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.
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 ¶
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 ¶
Delete implements server.ProtocolWireStore. A session that is not there has no wire state to delete.
Source Files
¶
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. |