Documentation
¶
Overview ¶
Package delegation defines backend-neutral contracts for assigning work to another agent or execution target.
Mode distinguishes synchronous and asynchronous work. Directory owns discovery; Service owns execution and status lookup. The local service (LocalService), the deploy-bound directory (LocalDirectory), and the delegation resource factories live in this package; the model-facing delegate / delegation_status tools live in the tool subpackage.
A host can expose a Service to execution-time tools with WithService, and consumers recover it with ServiceFromHost.
Index ¶
- Constants
- Variables
- func NewServiceFactory(directory *LocalDirectory, opts ...Option) res.Factory
- func RequestNotFound(id string) error
- func TargetNotFound(id string) error
- func UnsupportedMode(mode Mode) error
- func WithService(h agent.Host, service Service) agent.Host
- type AsyncBackend
- type AsyncRequest
- type Deployment
- type Directory
- type IdempotencyRetentionProvider
- type LocalDirectory
- type LocalService
- func (s *LocalService) BindDeployment(deployment any) error
- func (s *LocalService) Close() error
- func (s *LocalService) Delegate(ctx context.Context, req Request) (Response, error)
- func (s *LocalService) Get(ctx context.Context, id string) (Response, error)
- func (s *LocalService) Run(ctx context.Context, req AsyncRequest) (Response, error)
- func (s *LocalService) Start() error
- type Mode
- type Option
- type Request
- type Response
- type Runner
- type Service
- type ServiceProvider
- type ServiceSettings
- type Status
- type Target
- type Work
- type WorkSource
Constants ¶
const ( // ServiceKind is the deployment resource kind of the local // delegation service. ServiceKind = "delegation.Service" // BackendDep is the optional async backend dependency. BackendDep = "backend" )
Variables ¶
var ( // ErrTargetNotFound identifies an unknown target. ErrTargetNotFound = errors.New("delegation: target not found") // ErrRequestNotFound identifies an unknown delegation request. ErrRequestNotFound = errors.New("delegation: request not found") // ErrUnsupportedMode identifies a valid mode unsupported by a backend. ErrUnsupportedMode = errors.New("delegation: mode not supported") )
Functions ¶
func NewServiceFactory ¶
func NewServiceFactory(directory *LocalDirectory, opts ...Option) res.Factory
NewServiceFactory returns a deployment factory for the local delegation service. directory is app-owned and may be bound to a deploy result after assembly (it cannot be a DAG dep: it bridges to the deploy result itself). Options inject app-owned behavior the document cannot express (e.g. delegation.WithWorkerHost); the declarative settings are applied after them.
func RequestNotFound ¶
RequestNotFound returns a not-found-classified request lookup error.
func TargetNotFound ¶
TargetNotFound returns a not-found-classified target lookup error.
func UnsupportedMode ¶
UnsupportedMode returns a not-available-classified backend capability error.
Types ¶
type AsyncBackend ¶
type AsyncBackend interface {
Submit(ctx context.Context, req AsyncRequest) (id string, err error)
Status(ctx context.Context, id string) (Response, error)
}
AsyncBackend stores asynchronous delegations and reports their status. Submit must safely replay the same non-empty Request.IdempotencyKey and identical AsyncRequest during the backend's declared retention window, returning the same id; reuse with different request semantics must return a conflict-classified error. The service borrows an injected backend and never closes it.
type AsyncRequest ¶
type AsyncRequest struct {
Request Request `json:"request"`
Caller string `json:"caller,omitempty"`
Depth int `json:"depth"`
}
AsyncRequest is the backend-neutral unit persisted by an AsyncBackend. Caller and Depth preserve delegation metadata across queue boundaries.
type Deployment ¶
Deployment is the minimal read-only deployment view needed by LocalDirectory. *deploy.Result implements this interface.
type Directory ¶
type Directory interface {
List(ctx context.Context) ([]Target, error)
Get(ctx context.Context, id string) (Target, error)
}
Directory discovers delegation targets independently of execution.
type IdempotencyRetentionProvider ¶
IdempotencyRetentionProvider optionally exposes an AsyncBackend's finite terminal replay and status-query window.
type LocalDirectory ¶
type LocalDirectory struct {
// contains filtered or unexported fields
}
Directory exposes the agents assembled by deploy.Build as local delegation targets. It may be constructed before Build and bound exactly once after it.
func (*LocalDirectory) Bind ¶
func (d *LocalDirectory) Bind(result Deployment) error
Bind installs Build's instances. A directory is immutable after a successful bind and borrows the instances; the result retains lifecycle ownership.
type LocalService ¶
type LocalService struct {
// contains filtered or unexported fields
}
LocalService executes local sync delegations and coordinates optional async storage/workers.
func NewService ¶
func NewService(directory *LocalDirectory, backend AsyncBackend, opts ...Option) (*LocalService, error)
NewService constructs a local service. Successful responses in every mode retain one shared idempotency key and request fingerprint. Async Accepted responses use the shorter of the service retention and a positive backend retention declaration, so replay cannot outlive backend queryability. If the backend also implements WorkSource, bounded workers start immediately. The backend remains owned by the deploy Result or caller.
func (*LocalService) BindDeployment ¶
func (s *LocalService) BindDeployment(deployment any) error
BindDeployment implements resource.DeploymentBinder: it binds the assembled deployment into the service's directory once agents are ready. The deployment view is the read-only interface exposed by *deploy.Result; binding is idempotent.
func (*LocalService) Close ¶
func (s *LocalService) Close() error
Close rejects new operations, cancels service-owned workers, and waits for every admitted operation and worker to finish. It is idempotent.
func (*LocalService) Run ¶
func (s *LocalService) Run(ctx context.Context, req AsyncRequest) (Response, error)
Run executes a backend-neutral work item through the same depth, timeout, host-propagation, and concurrency limits as a sync call.
func (*LocalService) Start ¶
func (s *LocalService) Start() error
Start begins asynchronous workers when the backend supports WorkSource. Repeated calls are safe and do not create duplicate workers.
type Option ¶
type Option func(*serviceConfig) error
Option configures a local Service.
func WithDeferredWorkers ¶
func WithDeferredWorkers() Option
WithDeferredWorkers defers asynchronous worker startup until Start. This is useful when a lifecycle owner must finish binding all dependencies before background work begins.
func WithIdempotencyRetention ¶
WithIdempotencyRetention sets how long successful responses remain safely replayable. The retention must be positive.
func WithMaxConcurrency ¶
WithMaxConcurrency bounds all local agent executions, including sync calls and work claimed by asynchronous workers.
func WithMaxDepth ¶
WithMaxDepth sets the largest allowed delegation depth. A top-level delegation executes at depth one.
func WithTimeout ¶
WithTimeout caps each local agent execution. Zero leaves the caller's deadline unchanged.
func WithWorkerHost ¶
WithWorkerHost sets the stable base Host used by asynchronous workers. Service adds its delegation capability without replacing optional capabilities such as EventBusProvider. The caller retains Host ownership.
type Request ¶
type Request struct {
Mode Mode `json:"mode"`
Target string `json:"target"`
Input string `json:"input"`
Metadata map[string]string `json:"metadata,omitempty"`
IdempotencyKey string `json:"idempotency_key,omitempty"`
}
Request is the portable input accepted by a Service.
type Response ¶
type Response struct {
ID string `json:"id"`
Status Status `json:"status"`
Output string `json:"output,omitempty"`
Error string `json:"error,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
Response is a snapshot of delegated work.
type Runner ¶
type Runner interface {
Run(ctx context.Context, req AsyncRequest) (Response, error)
}
Runner is the restricted execution seam a queue-owned worker may use.
type Service ¶
type Service interface {
Delegate(ctx context.Context, req Request) (Response, error)
Get(ctx context.Context, id string) (Response, error)
}
Service starts delegated work and retrieves its latest response. Implementations must declare and guarantee a finite idempotency retention window. Within that window, requests with the same non-empty idempotency key and business fields must safely replay the same operation, and its response must remain queryable; reusing the key for a semantically different request must return a conflict-classified error. After the declared window expires, the key may start a new operation: this contract is bounded idempotency, not permanent exactly-once execution.
type ServiceProvider ¶
type ServiceProvider interface {
DelegationService() Service
}
ServiceProvider is the optional Host capability exposing delegation.
type ServiceSettings ¶
type ServiceSettings struct {
MaxConcurrency *int `json:"max_concurrency,omitempty"`
MaxDepth *int `json:"max_depth,omitempty"`
Timeout *string `json:"timeout,omitempty"`
IdempotencyRetention *string `json:"idempotency_retention,omitempty"`
DeferWorkers bool `json:"defer_workers,omitempty"`
}
ServiceSettings is the strict settings subtree of the local service factory. Zero fields leave the service defaults.
type Status ¶
type Status string
Status describes the backend-neutral lifecycle of delegated work.
type Target ¶
type Target struct {
ID string `json:"id"`
Description string `json:"description,omitempty"`
Modes []Mode `json:"modes,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
Target describes a backend-neutral delegation destination. Modes is an explicit allowlist; an empty Modes list means unrestricted support for every defined delegation mode.
type Work ¶
type Work struct {
ID string `json:"id"`
LeaseToken string `json:"lease_token"`
Request AsyncRequest `json:"request"`
Context context.Context `json:"-"`
}
Work is one claimed asynchronous request. LeaseToken uniquely identifies this claim generation and must be supplied to WorkSource.Complete. Context optionally carries the backend-owned execution lease; canceling it must stop the claimed execution.
type WorkSource ¶
type WorkSource interface {
Claim(ctx context.Context) (Work, error)
Complete(ctx context.Context, id, leaseToken string, response Response) error
}
WorkSource is the optional worker side of an AsyncBackend. When implemented, Service starts bounded workers that claim backend-neutral work and complete it through the same backend. Claim must return a unique, non-empty lease token and unblock when ctx is canceled. Complete must only apply a terminal response when the token identifies the current lease; stale completions are ignored.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package kanban provides an in-memory core/delegation AsyncBackend and WorkSource.
|
Package kanban provides an in-memory core/delegation AsyncBackend and WorkSource. |
|
resource
Package resource adapts kanban delegation backends to deployment resources.
|
Package resource adapts kanban delegation backends to deployment resources. |
|
Package delegation exposes backend-neutral delegate, delegation_status, and delegation_targets tools.
|
Package delegation exposes backend-neutral delegate, delegation_status, and delegation_targets tools. |