agent

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: Apache-2.0 Imports: 26 Imported by: 6 modules, 6 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 agent is mcpkit's host layer: the agentic loop that turns a set of connected MCP servers and an LLM into a working agent.

The module provides four seams (see docs/AGENT_DESIGN.md for the design and the tracking epic for delivery order):

  • Provider: streaming LLM access with tool-call support
  • Runner: the multi-step tool loop, emitting wire-serializable events
  • ToolSource: tool aggregation across MCP servers and host-local functions
  • Policy hooks: context injection and event-initiated turns

It deliberately excludes sessions, chat transports, and persistence; those belong to the applications (CLIs, web hosts, native shells) that embed it.

agent/ is a separate Go module so that LLM-provider dependencies never reach mcpkit's core, server, or client packages.

Index

Constants

View Source
const (
	SpawnAgentToolName  = "spawn_agent"
	AwaitAgentToolName  = "await_agent"
	CancelAgentToolName = "cancel_agent"
	ListAgentsToolName  = "list_agents"
)

Runner-control tool names (issue 1166), kept fixed like the memory/meta-tool names.

View Source
const (
	RememberToolName = "remember"
	RecallToolName   = "recall"
	ForgetToolName   = "forget"
)

Reserved tool names MemorySource exposes so the model can manage its own working memory — a scratchpad it reads and writes across turns.

View Source
const DefaultAnthropicVersion = "2023-06-01"

DefaultAnthropicVersion is the anthropic-version header sent when AnthropicConfig.Version is empty. 2023-06-01 is the documented stable Messages API version.

View Source
const DefaultCharsPerToken = 4

DefaultCharsPerToken approximates English tokenization (~4 characters per token) for CharTokenEstimator.

View Source
const DefaultFailoverCooldown = 30 * time.Second

DefaultFailoverCooldown is how long after a primary failure the failover provider keeps routing to the backup before re-trying the primary.

View Source
const DefaultKeepRecent = 6

DefaultKeepRecent is how many trailing messages SummarizingCompactor keeps verbatim when no KeepRecent is configured — enough to preserve the active exchange while still collapsing the older head.

View Source
const DefaultListRunsLimit = 50

DefaultListRunsLimit bounds a page when ListRunsRequest.Limit is zero.

View Source
const DefaultMaxAgentDepth = 3

DefaultMaxAgentDepth bounds how deep a sub-agent call tree may nest when an AgentSource is constructed without a MaxDepth — the runaway-recursion backstop (an agent that keeps delegating to itself).

View Source
const DefaultMaxHandoffs = 8

DefaultMaxHandoffs bounds how many times a Team may transfer control before giving up — the ping-pong backstop (two agents that keep handing the conversation back and forth).

View Source
const DefaultMaxPerDrain = 16

DefaultMaxPerDrain bounds injected entries per turn when unconfigured.

View Source
const DefaultMaxSteps = 8

DefaultMaxSteps bounds a turn when RunnerConfig.MaxSteps is zero. Eight model calls is generous for real workflows; hitting it usually means the model is looping on a failing tool.

View Source
const DefaultOffloadPreview = 400

DefaultOffloadPreview is how many leading characters of the flattened result the stub carries inline, so the model can often act without a read_tool_result round-trip at all.

View Source
const DefaultOffloadThreshold = 4096

DefaultOffloadThreshold is the model-visible size (bytes of flattened result text) at or above which a successful tool result is offloaded. 4 KB keeps ordinary results inline while catching the file dumps and long stdout that actually bloat context.

View Source
const DefaultRecallTopK = 5

DefaultRecallTopK bounds a relevance recall when RecallOptions.TopK is unset.

View Source
const DefaultStubEmbedderDim = 64

DefaultStubEmbedderDim is the vector width StubEmbedder uses when its Dim is unset.

View Source
const DefaultTriggerBudget = 10

DefaultTriggerBudget caps proactive firings per policy lifetime when unconfigured.

View Source
const DefaultTriggerCooldown = 5 * time.Minute

DefaultTriggerCooldown gates re-arming when a binding does not set its own.

View Source
const MetaKeyContextHint = "io.github.panyam.mcpkit/context-hint"

MetaKeyContextHint is the vendor _meta key on an events/list EventDef that carries a ContextHint (see docs/AGENT_DESIGN.md, vendor prefix section). Advisory per the context-hints SEP draft: hosts MAY ignore it, and host configuration overrides it.

View Source
const MetaKeyTrigger = "io.github.panyam.mcpkit/trigger"

MetaKeyTrigger is the vendor _meta key on an events/list EventDef carrying a server-suggested trigger binding. Server-advertised triggers are suggestions only: the host decides whether to install them, and every firing is mediated by TriggerPolicy regardless of origin (the triggers SEP draft's stance: the server signals, the host owns the turn).

View Source
const ReadToolResultName = "read_tool_result"

ReadToolResultName is the reserved tool name OffloadingSource injects so the model can fetch an offloaded result. A wrapped source that also exposes this name is shadowed by the offloader's handler.

View Source
const SignalParentToolName = "signal_parent"

SignalParentToolName is the fixed name of the control tool a child agent calls to raise a Signal to its parent (kept fixed, like the memory/meta-tool names).

Variables

View Source
var ErrInputRequired = errors.New("agent: tool requires mid-call input and no InputHandler is configured")

ErrInputRequired is returned by ClientSource.Call when the server asks for SEP-2322 mid-call input and no InputHandler was configured. The elicitation seam (agent epic milestone 2) replaces the default handler; until then, callers can detect the condition with errors.Is.

View Source
var ErrMaxHandoffs = errors.New("agent: max handoffs exceeded")

ErrMaxHandoffs is returned by Team.Run when the handoff cap is exceeded.

View Source
var ErrMaxSteps = errors.New("agent: max steps exceeded")

ErrMaxSteps is returned (wrapped) by Run when the model keeps requesting tool calls past the step cap. Check with errors.Is.

View Source
var ErrNotAvailableNow = errors.New("agent: tool source not available now")

ErrNotAvailableNow is returned by a ToolSource.Call when the tool exists but its backing server is unreachable right now (as opposed to the tool failing). The Runner treats it as a NON-FATAL miss: it emits EventToolUnavailable and feeds the wrapped error's message back to the model, and the turn continues. Wrap it with a descriptive message (fmt.Errorf("%w: ...", ErrNotAvailableNow, ...)) so the model learns which server and can retry, route around it, or tell the user. See docs/AGENT_SERVER_STATE.md.

View Source
var ErrSignalAbort = errors.New("agent: turn aborted by child signal")

ErrSignalAbort is returned (wrapped) by a turn a SignalPolicy aborted via SignalAction.AbortTurn — a child escalated and the parent chose to stop. Check with errors.Is; the wrapped message carries the policy's Reason.

View Source
var ErrTreeBudget = errors.New("agent: tree budget exhausted")

ErrTreeBudget is returned (wrapped) by the Runner when a ctx-threaded TreeBudget is exhausted mid-turn — the aggregate step or token cap across the whole sub-agent tree was hit. For a sub-agent it surfaces as an IsError result (AgentSource softens a child error), so the parent turn continues; for the top-level Runner the turn fails, like ErrMaxSteps.

View Source
var ErrUnknownTool = errors.New("agent: unknown tool")

ErrUnknownTool is wrapped by ToolSource implementations when a Call names a tool the source does not have. Aggregators use it to distinguish a stale-index miss (worth one refresh) from a definitive dispatch failure; the Runner uses it to feed a not-found back to the model.

View Source
var ToolChoiceAuto = ToolChoice{Mode: "auto"}

ToolChoiceAuto lets the model decide (the default; same as the zero value).

View Source
var ToolChoiceNone = ToolChoice{Mode: "none"}

ToolChoiceNone forbids tool calls for this request.

View Source
var ToolChoiceRequired = ToolChoice{Mode: "required"}

ToolChoiceRequired forces the model to call some tool.

Functions

func AddAsyncFunc

func AddAsyncFunc[In any](s *FuncSource, name, description string,
	fn func(ctx context.Context, in In) (AsyncResult, error),
	onComplete func(IncomingEvent, error)) error

AddAsyncFunc registers a host-local tool that does background work without a server-side task: the handler returns an ack immediately (satisfying the tool-call contract — every call needs one response), and if it supplies an Await, that runs on a goroutine and its completion event is delivered to onComplete (a host wires onComplete to its injection policy, so the result reaches the model on a later turn — the same shape as a task.completed event, without a task runtime). For model-visible poll/cancel on local work, use a real server-side task instead.

func AddFunc

func AddFunc[In any](s *FuncSource, name, description string, fn func(ctx context.Context, in In) (string, error)) error

AddFunc registers a typed function as a tool. Arguments are decoded into In via JSON round-trip and validated only by that decoding; the returned string becomes a single text content item. A handler error becomes an IsError tool result (the model sees the failure), not a dispatch error. Registering a duplicate name returns an error.

func GrantAllPreempts

func GrantAllPreempts(Signal) bool

GrantAllPreempts is a built-in RunnerConfig.PreemptGrant that honors every child's preempt — appropriate when the parent trusts all its sub-agents equally. For a finer policy (honor a preempt only from a given Source, or carrying a given Note), pass your own predicate instead. Nil PreemptGrant (the default) honors none.

func MergeWith

func MergeWith[T any](merge func(acc, next T) T) func(acc, next IncomingEvent) IncomingEvent

MergeWith adapts a payload-typed combiner into an IncomingEvent merge for Window's merge strategy: both payloads are Bound as T, folded, and the result re-marshaled onto the newer event's envelope. If either payload does not decode, the newer event wins (never fabricate data).

func RaiseSignal

func RaiseSignal(ctx context.Context, sig Signal) bool

RaiseSignal delivers sig to the parent Runner's signal sink — the control-axis "up" primitive a child's control tool calls. It stamps sig.Source with the child's scope when unset. It returns false when ctx carries no parent sink (a top-level agent has no parent to signal), so a control tool can tell the model there is nothing to signal instead of silently dropping it.

func TypedFilter

func TypedFilter[T any](fn func(T) bool) func(IncomingEvent) bool

TypedFilter adapts a payload-typed predicate onto IncomingEvent: the payload is Bound once; events whose payload does not decode as T do not match. Use for filters that inspect fields with compile-time safety.

func TypedTransform

func TypedTransform[T any](fn func(T) (T, bool)) func(IncomingEvent) (IncomingEvent, bool)

TypedTransform adapts a payload-typed rewrite onto IncomingEvent; the returned payload is re-marshaled into Data. Undecodable payloads pass through unchanged (a transform must not silently eat events it cannot read).

func WithAgentCallBudget

func WithAgentCallBudget(ctx context.Context, n int) context.Context

WithAgentCallBudget caps the TOTAL number of sub-agent invocations allowed under ctx, shared across the whole call tree (a shape-independent cost guard, complementary to the per-source depth cap). Each AgentSource call consumes one; when the budget is exhausted, further calls are refused with an IsError result. Absent, only the depth cap applies.

func WithTreeBudget

func WithTreeBudget(ctx context.Context, b TreeBudget) context.Context

WithTreeBudget installs a shared aggregate budget on ctx, consulted by every Runner that runs under it (parent + sub-agents). Call it once at the top of a turn; child runs inherit the same live counter through ctx. A zero TreeBudget is a no-op (returns ctx unchanged). Installing again on a ctx that already carries a budget replaces it — but the Runner only installs when absent, so the top-level budget is the one the whole tree shares.

Types

type Accumulator

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

Accumulator folds a delta stream into a ProviderResponse. Zero value is ready to use. Not safe for concurrent use.

func (*Accumulator) Add

func (a *Accumulator) Add(d Delta)

Add folds one delta.

func (*Accumulator) Result

func (a *Accumulator) Result() *ProviderResponse

Result returns the folded response. Tool-call argument fragments are joined in stream order; an empty argument buffer becomes the empty JSON object so callers can always unmarshal Args.

type AgentPool

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

AgentPool runs named child agents in the background and hands the parent model a handle to each, so the model can steer sub-agents through ordinary tool calls (issue 1166, piece B of the 1036 control axis): spawn one now, await its result at a chosen point, or cancel it. It is the model-driven, handle-based counterpart to AgentSource (blocking, no handle) and AsyncAgentSource (fire-and-inject, no handle): here the model holds the handle and pulls the result via await, or drops it via cancel.

A handle outlives the spawning turn (the child runs on a DetachForBackground context), so spawn-in-turn-1 / await-in-turn-3 works. Delivery is pull-only: a spawned result is stored on the handle and returned by Await, never auto-injected — auto-injection is AsyncAgentSource's model. The pool is safe for concurrent use.

func NewAgentPool

func NewAgentPool(onEvent func(SubAgentEvent)) *AgentPool

NewAgentPool returns an empty pool. onEvent, when non-nil, receives every spawned child's event stream wrapped in a SubAgentEvent (scope = the agent name, depth threaded), so a surface renders background activity the same way it renders a blocking sub-agent's. Nil drops child events.

func (*AgentPool) Await

func (p *AgentPool) Await(ctx context.Context, id string) (*TurnResult, error)

Await blocks until the spawned agent id finishes and returns its result, or returns when ctx is cancelled (the turn was cancelled). An unknown id is an error. Await is idempotent: a second call returns the same stored result.

func (*AgentPool) Cancel

func (p *AgentPool) Cancel(id string) error

Cancel aborts the spawned agent id (its context is cancelled; the running child returns and its handle moves to "cancelled"). An unknown id is an error. Cancelling a finished agent is a no-op that still succeeds.

func (*AgentPool) Names

func (p *AgentPool) Names() []SpawnStatus

Names returns the registered spawnable agent names in registration order, with their descriptions — for a spawn tool's help text.

func (*AgentPool) Register

func (p *AgentPool) Register(name, description string, runner *Runner, maxDepth int) error

Register adds a spawnable agent under name (its child Runner + a description for the model + a depth cap; zero uses DefaultMaxAgentDepth). A duplicate name is an error.

func (*AgentPool) Spawn

func (p *AgentPool) Spawn(ctx context.Context, name, task string) (string, error)

Spawn starts the named agent on a detached, cancellable goroutine over an isolated slice seeded with task, and returns a handle id. Depth and the ctx-threaded call budget are checked before the spawn (an unknown name or an exhausted guard is an error, not a started agent). The child outlives ctx (DetachForBackground), so it keeps running after the spawning turn ends.

func (*AgentPool) Statuses

func (p *AgentPool) Statuses() []SpawnStatus

Statuses snapshots every handle (running and finished) for list_agents, in id order of creation.

type AgentSource

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

AgentSource exposes a child Runner to a parent agent as a single tool: the agent-as-tool pattern. Calling the tool runs the child over its OWN isolated conversation (a fresh slice seeded with the task) and returns the child's final text. Isolation is structural — a separate []Message — so the Runner never changes; supervision falls out for free by putting several AgentSources in a MultiSource (the existing aggregation, collision, and Selector routing all apply).

It implements ToolSource, so it drops into a RunnerConfig.Tools (directly or via MultiSource) like any other source. A6: it is model-facing (a tool the parent model calls), so it lives in agent/.

func NewAgentSource

func NewAgentSource(cfg AgentSourceConfig) (*AgentSource, error)

NewAgentSource validates cfg and builds the tool definition. Name and Runner are required.

func NewServerAgentSource

func NewServerAgentSource(cfg ServerAgentConfig) (*AgentSource, error)

NewServerAgentSource builds an AgentSource from a decoded server-advertised agent definition: a child Runner on cfg.Provider whose ToolSource advertises the agent's scoped Tools and dispatches every call back through cfg.Backing (the advertising server), seeded with the agent's Instructions. The parent then delegates to the returned source like any other AgentSource — depth, call budget, scope, and signal plumbing all apply unchanged.

Name, Provider, and Backing are required. The child is deliberately built over a scoped source (not cfg.Backing directly) so it can call only the tools the agent's definition scoped it to; a tool the server has but the definition did not scope is unreachable from the child.

func (*AgentSource) Call

func (s *AgentSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call runs the child over an isolated conversation seeded with the task and returns its final text. A child that errors or is refused by a guard surfaces as an IsError result (fed back to the parent model, which can react), not a dispatch error — only an unknown name is a dispatch error, so the parent's turn never aborts on a sub-agent problem.

func (*AgentSource) Tools

func (s *AgentSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools returns the single sub-agent tool.

type AgentSourceConfig

type AgentSourceConfig struct {
	// Name is the tool name the parent model sees and calls. Required.
	Name string

	// Description tells the parent model when to delegate to this sub-agent.
	// Required.
	Description string

	// Runner is the child agent. Required. One Runner instance serves
	// concurrent calls — Run is stateless over the history it is handed, so
	// each call gets its own isolated slice without a per-call Runner.
	Runner *Runner

	// MaxDepth caps sub-agent nesting depth (this source's calls plus any
	// deeper sub-agent calls the child makes). Zero uses DefaultMaxAgentDepth.
	MaxDepth int

	// OnEvent, when set, receives the child's event stream wrapped in a
	// SubAgentEvent envelope (scope + depth + the flat Event), so a surface
	// can render the sub-agent's turn nested under the parent's. Nil drops
	// the child's events (the sub-agent runs invisibly). Wire OnEvent on
	// every AgentSource in a tree to the same sink and the scope/depth
	// disambiguate nested runs.
	OnEvent func(SubAgentEvent)

	// InputSchema, when set, replaces the default {task} schema the tool
	// advertises, so a parent delegates a TYPED subtask. The child is seeded
	// with the raw arguments JSON as its user turn (the schema shapes what the
	// parent model must pass; the child reads it as its instruction). Nil keeps
	// the {task: string} shape. Structured OUTPUT is orthogonal: build the child
	// Runner with a ResponseSchema and Call returns its coerced JSON.
	InputSchema json.RawMessage
}

AgentSourceConfig configures an AgentSource.

type AggregateHint

type AggregateHint struct {
	WindowMs int            `json:"windowMs"`
	Strategy WindowStrategy `json:"strategy,omitempty"`
}

AggregateHint is ContextHint's coalescing sub-shape.

type AnthropicConfig

type AnthropicConfig struct {
	// BaseURL is the API root. The provider appends "/v1/messages". Defaults
	// to "https://api.anthropic.com" when empty.
	BaseURL string

	// APIKey is sent as the x-api-key header. Required against the real API;
	// may be empty for a local mock.
	APIKey string

	// Model is the model identifier sent on every request (required). The
	// caller supplies it; the provider hardcodes none.
	Model string

	// MaxTokens caps the completion length sent on every request. Defaults to
	// 4096 when not positive; a per-request ProviderRequest.MaxTokens overrides
	// it.
	MaxTokens int

	// Version is the anthropic-version header. Defaults to
	// DefaultAnthropicVersion when empty.
	Version string

	// HTTPClient overrides http.DefaultClient. Set this for proxies, custom
	// TLS, or timeouts (note: an overall client timeout also bounds streaming
	// reads; prefer per-request ctx deadlines for streams).
	HTTPClient *http.Client
}

AnthropicConfig configures the Anthropic Messages API endpoint.

type AnthropicProvider

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

AnthropicProvider implements Provider over the Anthropic Messages API wire with no SDK dependency (net/http plus servicekit's WHATWG-conformant SSE reader). Safe for concurrent use.

func NewAnthropicProvider

func NewAnthropicProvider(cfg AnthropicConfig) (*AnthropicProvider, error)

NewAnthropicProvider validates cfg and returns a provider. Model is required; BaseURL, MaxTokens, and Version fall back to defaults.

func (*AnthropicProvider) Generate

Generate implements Provider with a non-streaming request. When req.ResponseSchema is set, the request forces a synthetic tool whose input_schema is the schema and the tool_use input is returned in ProviderResponse.Text (Anthropic has no response_format).

func (*AnthropicProvider) Stream

Stream implements Provider. Anthropic SSE events map onto the Delta taxonomy: content_block_start(tool_use) → DeltaToolCallStart, text_delta → DeltaText, input_json_delta → DeltaToolCallArgs, thinking_delta → DeltaReasoning, message_delta → DeltaFinish + DeltaUsage. The stream ends with io.EOF after message_stop.

type AppendEventsRequest

type AppendEventsRequest struct {
	RunID  string
	Events []Event
}

AppendEventsRequest appends events to a run's audit/replay log. The event log is optional and independent of the message log: a store accepts events for any existing run whether or not the surface also persists messages.

type AppendEventsResponse

type AppendEventsResponse struct {
	Found bool
}

AppendEventsResponse reports whether the run existed; Found=false means nothing was written (see AppendMessagesResponse).

type AppendMessagesRequest

type AppendMessagesRequest struct {
	RunID    string
	Messages []Message
}

AppendMessagesRequest appends messages to a run's log in order. Callers pass exactly the entries a turn added (the user message plus TurnResult.Messages) so the stored log threads the same way in-process history does.

Stamping rule: implementations set Message.Timestamp to their own clock for every appended message whose Timestamp is zero, and preserve non-zero values verbatim (caller wins — a surface can stamp the user message at keypress). One boundary, one rule: code that constructs messages never needs to remember to stamp.

type AppendMessagesResponse

type AppendMessagesResponse struct {
	Found bool
}

AppendMessagesResponse reports whether the run existed. Found=false means nothing was written — the caller appended to a run it never created (or one that was pruned), which is a caller bug to surface, not a storage fault.

type ApprovalDecision

type ApprovalDecision struct {
	Allowed bool
	Reason  string
}

ApprovalDecision is a policy's verdict. Allowed true runs the call. On a refusal, Reason is surfaced to the model (as the tool result text) and to surfaces (on the tool-denied event); an empty Reason gets a default.

type ApprovalMode

type ApprovalMode int

ApprovalMode is the default disposition TieredApproval applies to a call that no per-tool rule covers.

const (
	// ModeAlwaysAsk asks for every uncovered call. The safe default.
	ModeAlwaysAsk ApprovalMode = iota
	// ModeReadOnlyAuto auto-allows calls whose tool declares readOnlyHint
	// and asks for the rest. The "read-only → auto-edit" rung of the ladder.
	ModeReadOnlyAuto
	// ModeAlwaysAllow runs every uncovered call without asking (full-auto /
	// "yolo"). Per-tool Deny rules still apply on top.
	ModeAlwaysAllow
)

type ApprovalPolicy

type ApprovalPolicy interface {
	Approve(ctx context.Context, req ApprovalRequest) (ApprovalDecision, error)
}

ApprovalPolicy gates each tool call before it runs. The Runner consults it in callTool, after argument binding and before ToolSource.Call; a nil policy on RunnerConfig means every call runs (the pre-approval behavior).

Approve may block to ask the user. It returns a decision, never a turn abort: a refusal is fed back to the model as a tool result and the loop continues, so an error from Approve is reserved for the policy itself failing (for example, the ask UI could not present the prompt).

type ApprovalRequest

type ApprovalRequest struct {
	ToolName string
	Args     core.RawJSON
	ReadOnly bool
}

ApprovalRequest describes the call a policy is deciding on. Args is the model-supplied arguments object (core.RawJSON per A5, so a policy can Bind for typed inspection without a second parse). ReadOnly reflects the tool's readOnlyHint annotation, the signal the read-only-auto tier keys on; it is false when the tool declares no such hint.

type AskFunc

type AskFunc func(ctx context.Context, req ApprovalRequest) (bool, error)

AskFunc presents a yes/no approval prompt and returns the user's choice. ElicitationCoordinator.Confirm satisfies it, which is how the "ask" outcome reuses the existing FIFO UI seam instead of introducing a second one. A nil AskFunc makes every ask resolve to a refusal (fail-closed).

type AsyncAgentSource

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

AsyncAgentSource is the Task form of a sub-agent: the spawn-and-continue counterpart to AgentSource's blocking Tool form. Call returns an ack IMMEDIATELY ("sub-agent X started") and runs the child on a detached goroutine; when the child finishes, OnComplete delivers its result, which a host injects so the parent picks it up on a later turn. The spawning turn does not wait for the subtree — right for long-running or fan-out-and-continue work (contrast AgentSource: call-and-block, answer this turn).

It is NOT an MCP task: there is no wire presence, no model-visible poll/cancel, and the goroutine is ephemeral (it dies with the process). For controllable or restart-surviving background work, use a real server-side task instead.

Depth and the ctx-threaded aggregate call budget still apply (checked at spawn time, before the goroutine starts). SubAgentEvent nesting still surfaces the child's stream while it runs in the background.

func NewAsyncAgentSource

func NewAsyncAgentSource(cfg AsyncAgentSourceConfig) (*AsyncAgentSource, error)

NewAsyncAgentSource validates cfg and builds the tool definition. Name, Runner, and OnComplete are required.

func (*AsyncAgentSource) Call

func (s *AsyncAgentSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call spawns the child on a detached goroutine and returns an ack immediately. Guards (depth, budget) are checked before the spawn and refuse as an IsError result; only an unknown name is a dispatch error.

func (*AsyncAgentSource) Tools

func (s *AsyncAgentSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools returns the single spawn tool.

type AsyncAgentSourceConfig

type AsyncAgentSourceConfig struct {
	// Name is the tool the parent model calls to spawn the sub-agent. Required.
	Name string

	// Description tells the parent when to spawn it (and that it returns later).
	Description string

	// Runner is the child agent. Required. Run is stateless over the history it
	// is handed, so one Runner serves concurrent spawns.
	Runner *Runner

	// MaxDepth caps sub-agent nesting (checked at spawn time). Zero uses
	// DefaultMaxAgentDepth.
	MaxDepth int

	// InputSchema, when set, replaces the default {task} schema so a parent
	// spawns a TYPED subtask; the child is seeded with the raw args JSON. Nil
	// keeps {task: string} (mirrors AgentSourceConfig.InputSchema).
	InputSchema json.RawMessage

	// OnEvent, when set, receives the child's event stream (scoped/depth in a
	// SubAgentEvent) WHILE it runs in the background, so a surface can still
	// render the sub-agent's activity after the spawning turn has ended. Nil
	// drops it.
	OnEvent func(SubAgentEvent)

	// OnComplete is called on the child's goroutine when it finishes, with the
	// sub-agent name, its result (nil on error), and any run error. Required —
	// it is how the result rejoins the conversation: a host wires it to its
	// injection seam (Ingest as a subagent.completed event), so the parent picks
	// the result up on a later turn. Without it the result is dropped.
	OnComplete func(name string, result *TurnResult, err error)
}

AsyncAgentSourceConfig configures an AsyncAgentSource.

type AsyncResult

type AsyncResult struct {
	// Ack is the model-facing text returned immediately (e.g. "job started").
	Ack string
	// Await, when non-nil, runs in a goroutine; its returned event is
	// handed to onComplete when it finishes. Nil means fire-and-forget.
	Await func(ctx context.Context) (IncomingEvent, error)
}

AsyncResult is what an AddAsyncFunc handler returns: an immediate acknowledgment (shown to the model as the tool result now), plus an optional completion whose event is injected when the background work finishes.

type CharTokenEstimator

type CharTokenEstimator struct {
	CharsPerToken int
}

CharTokenEstimator estimates tokens as total text length divided by CharsPerToken. It counts message text plus tool-call argument JSON, which is what actually bloats a long tool-using conversation. Zero CharsPerToken uses DefaultCharsPerToken.

func (CharTokenEstimator) Estimate

func (e CharTokenEstimator) Estimate(msgs []Message) int

Estimate implements TokenEstimator.

type ClientSource

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

ClientSource adapts one connected client.Client into a ToolSource.

Calls go through the client's MRTR-aware path so ctx cancellation reaches the wire and mid-call input requests surface deterministically: with the default configuration an input-required round fails with ErrInputRequired instead of hanging or guessing.

func NewClientSource

func NewClientSource(c *client.Client, opts ...ClientSourceOption) *ClientSource

NewClientSource wraps a connected client. The client must already be initialized; ClientSource performs no connection management.

func (*ClientSource) Call

func (s *ClientSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call dispatches tools/call with automatic MRTR rounds via the configured InputHandler. A task-creating response is waited to a terminal state: polling honors the server's interval hints, input_required pauses resolve through the SAME InputHandler (so task input reaches the elicitation seam with no extra wiring), and the terminal snapshot maps back onto the sync contract (completed carries the ToolResult, including tool-side IsError; failed and cancelled are dispatch errors).

func (*ClientSource) Tools

func (s *ClientSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools lists the server's tools in model-facing form (server-side visibility filtering applied, per ListToolsForModel).

type ClientSourceOption

type ClientSourceOption func(*ClientSource)

ClientSourceOption configures a ClientSource.

func WithInputHandler

func WithInputHandler(h client.InputHandler) ClientSourceOption

WithInputHandler installs the SEP-2322 input handler used when a tool call returns input_required mid-dispatch. The elicitation seam wires the host UI through this option; tests can use it to script input rounds.

func WithTaskCompletionHook

func WithTaskCompletionHook(fn func(*client.BackgroundTask)) ClientSourceOption

WithTaskCompletionHook fires when a DETACHED task reaches its terminal state (inline completions return through Call as usual). The agent watches the handle's Done channel and invokes this from a watcher goroutine; hosts turning completions into events or proactive turns do it here.

func WithTaskDetachHook

func WithTaskDetachHook(fn func(*client.BackgroundTask)) ClientSourceOption

WithTaskDetachHook observes each detach, delivering the client.BackgroundTask handle (registries, transcript lines, cancellation surfaces hang off it).

func WithTaskGrace

func WithTaskGrace(d time.Duration) ClientSourceOption

WithTaskGrace opts task-backed calls into background detach: a call still working when the window expires returns immediately with a model-visible "running in the background" result while the client's poll continues on a detached goroutine. The window holds while an input pause is active, so interactive tasks that park in input_required within the grace stay inline. Zero or unset keeps the synchronous wait-to-terminal contract. The detach mechanism itself lives in client.WaitForTaskOrBackground.

func WithTaskStatusHook

func WithTaskStatusHook(fn func(*core.DetailedTask)) ClientSourceOption

WithTaskStatusHook observes every polled task snapshot during task-backed tool calls: the client-level WaitOptions.OnStatus threaded through, so surfaces can render progress between tool-begin and tool-end.

type CompactionInfo

type CompactionInfo struct {
	Before int `json:"before"`
	After  int `json:"after"`
}

CompactionInfo reports what a compaction pass did: the message count before and after the head was summarized. After < Before whenever the event fires.

type Compactor

type Compactor interface {
	Compact(ctx context.Context, history []Message) ([]Message, error)
}

Compactor shrinks a turn's history before the model sees it, trading some fidelity for a smaller context. It is the lossy counterpart to tool-result offloading (which is lossless, pay-on-lookup): compaction pays unconditionally and cannot be un-done, so it is the tool of last resort for keeping a long conversation under a model's context budget.

The Runner calls Compact once at the top of a turn, on its own clone of the history, so Run stays stateless over the history it is handed. A Compactor MUST be a pure function of its input (no turn or session state) and MUST return the input unchanged when nothing needs compacting — the Runner detects a no-op by length and emits an EventCompaction only when the count actually drops.

type ContextHint

type ContextHint struct {
	// Priority orders injection under the budget: critical, high,
	// medium (default), low.
	Priority string `json:"priority,omitempty"`

	// Aggregate coalesces bursts before injection.
	Aggregate *AggregateHint `json:"aggregate,omitempty"`

	// Template renders the payload as context: {{field}} substitutes
	// top-level payload fields (deliberately no logic; hosts wanting
	// more render themselves). Empty uses the default rendering.
	Template string `json:"template,omitempty"`

	// Retention: "turn" (default; injected once) or "session" (the
	// latest occurrence re-injects every turn until superseded).
	Retention string `json:"retention,omitempty"`

	// Sensitivity: "public" (default), "personal", "restricted".
	// Restricted events are dropped unless the consent gate approves.
	Sensitivity string `json:"sensitivity,omitempty"`
}

ContextHint declares how an event's occurrences should reach the model. The shape mirrors the context-hints SEP draft one-for-one.

func HintFromMeta

func HintFromMeta(meta map[string]any) (ContextHint, bool)

HintFromMeta extracts a server-advertised ContextHint from an events/list EventDef _meta map. Second return reports presence. Malformed hints are ignored (advisory data never breaks the host).

type Control

type Control struct {
	// CallID names the in-flight tool call to cancel; empty cancels
	// every call currently in flight. An ID that is not in flight
	// (already finished, or never dispatched) is a no-op, so racing a
	// call's natural completion is safe.
	CallID string
}

Control is the turn's steering envelope: surfaces send Controls on TurnRequest.Control to steer a turn while it runs. Cancellation is the first (currently only) verb, in two modes:

  • Control{} — cancel ALL calls currently in flight, one send. The naive-Esc path: a surface needs no bookkeeping, three in-flight calls die from a single Control.
  • Control{CallID: id} — cancel exactly one call, identified by the ToolCall.ID the surface saw on that call's tool-begin event. For richer surfaces (a TUI with a row per running call).

Either way the decision stays with the sender: surfaces already hold the call inventory via tool-begin/tool-end events, and constraint A4 keeps decision callbacks out of the loop.

Future steering verbs (pause, budget bumps, mid-turn priority hints) extend this struct additively — a Kind discriminator plus verb fields, with the zero Kind meaning cancel for compatibility — rather than new channels or a handler registry. Mid-turn *content* (a "/btw" note for the model) is deliberately not a Control: anything the model should see routes through the injection path so it enters history as a message, not a side effect.

type CreateRunRequest

type CreateRunRequest struct {
	RunID string
}

CreateRunRequest starts a new empty run. RunID is optional: empty asks the store to generate a unique ID; non-empty claims a caller-chosen name (a session name, a ticket ID). Claiming an ID that already exists is not an overwrite — the store leaves the existing run intact and reports Created=false.

type CreateRunResponse

type CreateRunResponse struct {
	RunID   string
	Created bool
}

CreateRunResponse carries the run's identity. Created is false when the requested RunID already existed (the existing run is untouched); stores always report Created=true for generated IDs.

type DeleteMemoryRequest

type DeleteMemoryRequest struct {
	Key       string
	Namespace string
}

DeleteMemoryRequest identifies the item to forget by Key, within Namespace (empty = the default/global scratchpad).

type DeleteMemoryResponse

type DeleteMemoryResponse struct {
	Deleted bool
}

DeleteMemoryResponse reports whether an item was actually removed.

type Delta

type Delta struct {
	Kind DeltaKind `json:"kind"`

	// Text carries the fragment for DeltaText, DeltaReasoning, and
	// DeltaToolCallArgs.
	Text string `json:"text,omitempty"`

	// Index is the tool-call slot for DeltaToolCallStart and
	// DeltaToolCallArgs; parallel calls interleave on distinct indexes.
	Index int `json:"index,omitempty"`

	// ToolCallID and ToolName are set on DeltaToolCallStart.
	ToolCallID string `json:"toolCallId,omitempty"`
	ToolName   string `json:"toolName,omitempty"`

	// FinishReason is set on DeltaFinish ("stop", "tool_calls", ...,
	// provider vocabulary passed through).
	FinishReason string `json:"finishReason,omitempty"`

	// Usage is set on DeltaUsage.
	Usage *Usage `json:"usage,omitempty"`
}

Delta is one streamed increment of a model response. Wire-serializable by design (constraint A2): surfaces may forward deltas verbatim.

type DeltaKind

type DeltaKind string

DeltaKind discriminates Delta payloads.

const (
	DeltaText          DeltaKind = "text"
	DeltaReasoning     DeltaKind = "reasoning"
	DeltaToolCallStart DeltaKind = "tool-call-start"
	DeltaToolCallArgs  DeltaKind = "tool-call-args"
	DeltaFinish        DeltaKind = "finish"
	DeltaUsage         DeltaKind = "usage"
)

Delta kinds. Tool calls stream as one DeltaToolCallStart (carrying ID and Name) followed by any number of DeltaToolCallArgs fragments for the same Index; there is no explicit end marker, the next start or the finish delta closes the call (fold with Accumulator).

type ElicitationCoordinator

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

ElicitationCoordinator serializes elicitations from every connected server onto one ElicitationUI: exactly one request is presented at a time, waiters proceed in strict FIFO order, and a waiter whose ctx ends leaves the queue without disturbing it. Use one coordinator per user session so parallel tool calls (or multiple servers) never stack dialogs.

Wiring covers both protocol inlets with one registration, because the client routes MRTR input_required rounds through the same dispatcher as real server-initiated requests:

coord := agent.NewElicitationCoordinator(ui)
c := client.NewClient(url, info,
    client.WithElicitationHandler(coord.Handler()))
src := agent.NewClientSource(c,
    agent.WithInputHandler(client.DefaultInputHandler(c)))

With that, a legacy-wire server pushing elicitation/create and a stateless-wire (or task) server returning input_required both land on the same UI, serialized.

func NewElicitationCoordinator

func NewElicitationCoordinator(ui ElicitationUI) *ElicitationCoordinator

NewElicitationCoordinator wraps ui with FIFO serialization.

func (*ElicitationCoordinator) Confirm

func (c *ElicitationCoordinator) Confirm(ctx context.Context, message string) (bool, error)

Confirm presents a yes/no prompt through the same FIFO seam as elicitation and reports the user's choice. It is the general "ask the user to confirm" primitive; the approval ladder wires it as its AskFunc (WithAsk(coord.Confirm)) so an approval prompt inherits the one-at-a-time serialization and never stacks against a concurrent elicitation. Only an explicit accept with confirm=true returns true; decline, cancel, or a missing/false field return false. A non-nil error means the surface failed to present the prompt.

func (*ElicitationCoordinator) Handler

Handler adapts the coordinator to the client's elicitation option. Register it on every client whose servers may elicit.

type ElicitationUI

type ElicitationUI func(ctx context.Context, req core.ElicitationRequest) (core.ElicitationResult, error)

ElicitationUI renders one elicitation to the user and returns their answer. It is the single seam a surface implements: a terminal prompts inline, a web host forwards over its wire, a test scripts responses. Decline and cancel are results (ElicitationResult.Action), not errors; an error means the surface itself failed to present the request.

type Embedder

type Embedder interface {
	Embed(ctx context.Context, texts []string) ([]Embedding, error)
}

Embedder turns text into Embeddings so memory can be recalled by semantic similarity rather than substring match. It is the sibling of Provider: an independently swappable seam (a hosted API, a local model, a stub) that the rest of the agent depends on only through this interface.

Embed returns exactly one Embedding per input text, in the same order. A store must be built and queried with the same Embedder.

type Embedding

type Embedding []float32

Embedding is a dense vector produced by an Embedder. A defined type (not a bare []float32) so the comparison lives on it as a method and signatures read in domain terms; every Embedding from one Embedder shares a dimensionality, and comparing embeddings from different Embedders is meaningless.

func (Embedding) Cosine

func (a Embedding) Cosine(b Embedding) float64

Cosine returns the cosine of the angle between a and b, in [-1, 1] (1 = identical direction). It returns 0 when either vector is zero or the lengths differ — a degenerate comparison ranks last rather than erroring, so a recall query never fails on a malformed vector.

type Event

type Event struct {
	Kind EventKind `json:"kind"`

	// Step is the 1-based loop step for step-scoped kinds (deltas, tool
	// events). Zero on turn-begin, turn-end, and error.
	Step int `json:"step,omitempty"`

	// Text carries the fragment for text-delta and thinking-delta.
	Text string `json:"text,omitempty"`

	// ToolCall identifies the call for tool-begin, tool-end, and
	// tool-error.
	ToolCall *ToolCall `json:"toolCall,omitempty"`

	// ToolResult is the outcome on tool-end (including IsError results;
	// tool-error is reserved for dispatch failures).
	ToolResult *core.ToolResult `json:"toolResult,omitempty"`

	// Error is the failure description on tool-error and error. A string,
	// not an error value, so the event crosses wires unchanged.
	Error string `json:"error,omitempty"`

	// Reason is the human-readable justification on tool-denied (why the
	// approval policy refused the call) and tool-cancelled (the user
	// stopped the call). Distinct from Error because both are outcomes
	// of someone's decision, not dispatch failures; neither carries a
	// ToolResult.
	Reason string `json:"reason,omitempty"`

	// Result is the completed turn on turn-end.
	Result *TurnResult `json:"result,omitempty"`

	// Compaction carries the before/after message counts on compaction.
	Compaction *CompactionInfo `json:"compaction,omitempty"`

	// Signal is the upward signal a child raised, on EventSignal.
	Signal *Signal `json:"signal,omitempty"`
}

Event is one increment of a running turn, the payload surfaces consume (constraint A2: JSON-tagged, stable kind, no Go-only fields). Events carry no turn or session identity on purpose: in-process the emit closure is scoped to one Run call, and wire layers wrap events in their own envelope (session id, turn id, sequence) rather than the module pre-committing an ID scheme.

type EventInjectionConfig

type EventInjectionConfig struct {
	// Hints maps event name to its ContextHint. Entries here override
	// server-advertised hints (host config wins over vendor _meta).
	Hints map[string]ContextHint

	// Filters and Transforms run before buffering, in order: the
	// developer seam for dropping or rewriting events regardless of
	// hints.
	Filters    []func(IncomingEvent) bool
	Transforms []func(IncomingEvent) (IncomingEvent, bool)

	// Merge is the combiner for merge-strategy windows. Nil uses
	// ShallowMergeJSON.
	Merge func(acc, next IncomingEvent) IncomingEvent

	// MaxPerDrain caps how many rendered entries one Drain returns
	// (highest priority first). Zero means DefaultMaxPerDrain.
	MaxPerDrain int

	// Consent gates sensitive events. Nil allows public and personal
	// and drops restricted; a non-nil gate decides everything except
	// public, which always passes.
	Consent func(hint ContextHint, ev IncomingEvent) bool
	// contains filtered or unexported fields
}

EventInjectionConfig assembles an EventInjectionPolicy.

type EventInjectionPolicy

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

EventInjectionPolicy is the host half of the context-hints SEP draft: it buffers incoming events (per-event aggregation windows), renders them, and releases them in priority order under a budget when the host drains before a turn. Safe for concurrent Ingest against one Drain (the wiring's event goroutine vs the turn path).

func NewEventInjectionPolicy

func NewEventInjectionPolicy(cfg EventInjectionConfig) *EventInjectionPolicy

NewEventInjectionPolicy builds the policy.

func (*EventInjectionPolicy) Drain

Drain flushes expired windows and returns rendered entries in priority order under the budget. Turn-retention entries leave the buffer; session-retention entries re-drain every call until superseded. Entries beyond the budget stay buffered for the next drain; restricted events denied by the consent gate are counted and dropped.

func (*EventInjectionPolicy) Dropped

func (p *EventInjectionPolicy) Dropped() int

Dropped reports how many events the consent gate has refused so far.

func (*EventInjectionPolicy) Ingest

func (p *EventInjectionPolicy) Ingest(ev IncomingEvent)

Ingest feeds one event through the developer stages and into its aggregation window (or straight to pending when the hint has none).

func (*EventInjectionPolicy) SetHint

func (p *EventInjectionPolicy) SetHint(name string, h ContextHint)

SetHint installs (or overrides) the hint for an event name at runtime, e.g. from events/list discovery. Host-config entries passed at construction still win: SetHint is a no-op for names already configured.

type EventKind

type EventKind string

EventKind discriminates Event payloads. The vocabulary is the surfaces contract from docs/AGENT_DESIGN.md: every kind is emitted in-process and must project 1:1 onto a wire.

const (
	EventTurnBegin     EventKind = "turn-begin"
	EventThinkingBegin EventKind = "thinking-begin"
	EventThinkingDelta EventKind = "thinking-delta"
	EventThinkingEnd   EventKind = "thinking-end"
	EventTextDelta     EventKind = "text-delta"
	EventToolBegin     EventKind = "tool-begin"
	EventToolEnd       EventKind = "tool-end"
	EventToolError     EventKind = "tool-error"
	EventToolDenied    EventKind = "tool-denied"
	// EventToolCancelled marks a call the user cancelled mid-flight via
	// a TurnRequest Control. Distinct from tool-error (the tool did not
	// fail; the user stopped it) so surfaces can render an interrupt
	// differently from a failure. Reason carries the model-visible
	// feedback text.
	EventToolCancelled EventKind = "tool-cancelled"
	// EventToolUnavailable marks a call whose backing server was not reachable
	// (ErrNotAvailableNow): the tool exists but its server is down right now.
	// Distinct from tool-error (nothing failed on the server; it just isn't
	// there yet) so a surface can render it as a transient miss and evals keyed
	// on Error don't count it. Reason carries the model-visible feedback text;
	// the turn continues so the model can retry, route around it, or tell the
	// user. See docs/AGENT_SERVER_STATE.md.
	EventToolUnavailable EventKind = "tool-unavailable"
	// EventCompaction marks that a Compactor rewrote the turn's history
	// before the first model call (the head summarized, a recent tail kept
	// verbatim). Emitted only when compaction actually fired; Compaction
	// carries the before/after message counts. Surfaces can render a
	// "compacted context" note; evals can assert it happened.
	EventCompaction EventKind = "compaction"
	// EventSignal marks that a child agent raised an upward Signal, observed by
	// the parent Runner at the dispatch join (issue 1165). Signal carries the
	// raised signal. It is the observability projection of the control-axis "up"
	// channel — a surface renders "child escalated"; the signal's effect on the
	// turn (inject / abort) is the SignalPolicy's job, not this event's.
	EventSignal  EventKind = "signal"
	EventTurnEnd EventKind = "turn-end"
	EventError   EventKind = "error"
)

Event kinds, in the order a typical turn emits them. Thinking markers wrap contiguous reasoning deltas within one step; tool events may interleave across parallel calls of the same step, but tool-begin always precedes its call's tool-end, tool-error, tool-denied, or tool-cancelled.

type FailoverConfig

type FailoverConfig struct {
	// Primary is the preferred provider. Required.
	Primary Provider

	// Backup takes over when the primary fails cleanly. Required (a
	// failover wrapper with no backup is just the primary).
	Backup Provider

	// Cooldown is how long the primary stays benched after a failure
	// before the next call re-tries it (lazy recovery). Zero means
	// DefaultFailoverCooldown.
	Cooldown time.Duration

	// Logger receives failover transitions at Warn and recoveries at
	// Info, with structured attrs. Nil discards: the agent module never
	// writes to process-global outputs (constraint A4).
	Logger *slog.Logger
	// contains filtered or unexported fields
}

FailoverConfig assembles a FailoverProvider.

type FailoverProvider

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

FailoverProvider fronts a primary and a backup Provider behind the plain Provider interface, so the Runner never knows failover exists. Semantics:

  • A call that fails CLEANLY on the primary (the call itself errors before any delta was delivered) is transparently retried on the backup, once. A stream that already emitted deltas is never retried: the consumer observed partial output, and replaying could re-run side effects downstream.
  • After a primary failure, calls route to the backup until Cooldown elapses; the next call after that re-tries the primary (lazy recovery). StartReconciler adds an optional background probe.

Safe for concurrent use.

func NewFailoverProvider

func NewFailoverProvider(cfg FailoverConfig) (*FailoverProvider, error)

NewFailoverProvider validates cfg and returns the wrapper.

func (*FailoverProvider) Generate

Generate implements Provider with the same clean-failure retry.

func (*FailoverProvider) Health

func (f *FailoverProvider) Health() ProviderHealth

Health returns the current snapshot.

func (*FailoverProvider) StartReconciler

func (f *FailoverProvider) StartReconciler(ctx context.Context, interval time.Duration, probe func(context.Context) error) (stop func())

StartReconciler probes the primary every interval while it is benched, so recovery does not wait for user traffic. probe runs a caller-supplied cheap check (nil uses a one-token Generate against the primary). Returns a stop func; also stops when ctx ends.

func (*FailoverProvider) Stream

Stream implements Provider. See the type doc for retry semantics; the no-retry-after-deltas rule is enforced by wrapping the primary stream so a mid-stream failure surfaces to the caller instead of restarting.

type FanOutConfig

type FanOutConfig struct {
	// Name is the tool the parent calls to broadcast a task. Required.
	Name string

	// Description tells the parent model when to fan out.
	Description string

	// Members are the sub-agents the task is broadcast to. Each runs over its
	// own isolated slice (an AgentSource), so depth guard, aggregate call
	// budget, and scope threading apply per member. At least one required.
	Members []*AgentSource

	// Aggregate reduces the members' results into the single text returned to
	// the parent. Nil uses defaultFanOutAggregate (labeled sections in member
	// order). Results are always passed in member order regardless of which
	// member finished first.
	Aggregate func([]FanOutResult) string
}

FanOutConfig configures a FanOutSource.

type FanOutResult

type FanOutResult struct {
	Name    string
	Text    string
	IsError bool
}

FanOutResult is one member's outcome in a fan-out. Name identifies the member; Text is its final answer, or an error message when IsError is set.

type FanOutSource

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

FanOutSource is a leaf ToolSource whose single tool broadcasts a task to every member sub-agent CONCURRENTLY and returns their results aggregated in member order. One tool call fans to N children in parallel and returns one combined result, so the parent model delegates an ensemble in a single step instead of emitting N calls and stitching N results itself.

It reuses AgentSource wholesale: each member is an AgentSource, so the same depth guard, ctx-threaded aggregate call budget (WithAgentCallBudget), and event scope apply. A member that fails or is refused by a guard is isolated — its FanOutResult is marked IsError and folded into the aggregate, and the other members still run; the fan-out tool itself does not error. Only an unknown tool name is a dispatch error.

Concurrency note: members run in their own goroutines, so a member's AgentSource.OnEvent handler may be called concurrently with its siblings' — wire it to a serialized sink (the host's emit is mutex-guarded).

func NewFanOutSource

func NewFanOutSource(cfg FanOutConfig) (*FanOutSource, error)

NewFanOutSource validates cfg and builds the tool definition. Name and at least one member are required.

func (*FanOutSource) Call

func (s *FanOutSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call broadcasts the task to every member concurrently and returns their aggregated results. Member failures are isolated (marked in the aggregate), never a dispatch error; only an unknown name errors.

func (*FanOutSource) Tools

func (s *FanOutSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools returns the single fan-out tool.

type FileToolResultStore

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

FileToolResultStore is a filesystem-backed ToolResultStore: one JSON file per ref under a directory. It lives here in agent/ rather than a sibling module because it is dependency-free (stdlib only) — the sibling modules (agent/store/redis, agent/store/gorm) exist to isolate heavy database dependencies, which this has none of.

It is the natural store for a local or coding agent: no server to run, durable across restarts for free, and the blobs are plain files the agent can read with the file tools it already has (the "filesystem as externalized memory" pattern). Blobs are immutable one-file-per-ref, so forks share them by path with no copy, and retention is just deleting old files.

func NewFileToolResultStore

func NewFileToolResultStore(dir string) (*FileToolResultStore, error)

NewFileToolResultStore returns a store writing under dir, creating it (and parents) if absent. The directory is the store's to own; point two stores at the same dir only if you intend them to share blobs.

func (*FileToolResultStore) GetToolResult

GetToolResult implements ToolResultStore. A missing file (never stored, or deleted) is Found=false, not an error.

func (*FileToolResultStore) PutToolResult

PutToolResult implements ToolResultStore. The blob is written to a temp file and renamed into place, so a crash mid-write never leaves a partial blob a later read could trip on (rename is atomic within one filesystem).

type FilterSource

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

FilterSource wraps a ToolSource with a static allow/deny predicate: the per-profile allowlist shape. Filtered tools disappear from both listing and calling (a Call to a filtered name fails with ErrUnknownTool via the listing check), so a filter is a real capability boundary, not a presentation hint. For context-dependent narrowing use RunnerConfig. Selector instead; FilterSource is for policy that never varies by conversation.

func NewFilterSource

func NewFilterSource(src ToolSource, keep func(core.ToolDef) bool) *FilterSource

NewFilterSource wraps src, keeping only tools for which keep returns true.

func (*FilterSource) Call

func (f *FilterSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call dispatches to the underlying source only when the name survives the filter, so filtered tools cannot be invoked by guessing names.

func (*FilterSource) Tools

func (f *FilterSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools lists the underlying source minus filtered entries.

type ForkRunRequest

type ForkRunRequest struct {
	RunID    string
	NewRunID string

	// AtMessage forks from an earlier point: a positive value copies
	// only the first AtMessage messages (checkpoint/rewind semantics);
	// zero or negative copies everything, today's behavior. A value at
	// or beyond the source's length clamps to a full copy. The source's
	// length is observed when the fork starts; appends racing the fork
	// land after the cut.
	//
	// Event-log handling: the audit stream is not sliceable by message
	// index, so a partial fork copies NO events — only a full copy
	// carries the event log across. The fork's message log is complete
	// either way, which is all resume needs.
	AtMessage int
}

ForkRunRequest copies an existing run's logs into a new run so the copy can diverge. NewRunID follows CreateRunRequest.RunID semantics: empty generates a unique ID, non-empty claims a caller-chosen one.

A non-empty NewRunID is also the fork's idempotency key. Forks are all-or-nothing (see RunStore), so a retry loop that mints one deterministic ID (a session-scoped name, a ULID) and reuses it across attempts converges: retry after a failure finds nothing at the ID and forks clean; retry after an unobserved success gets Created=false against a complete fork, confirmable by loading it and checking ParentID.

type ForkRunResponse

type ForkRunResponse struct {
	RunID     string
	Found     bool
	Created   bool
	ForkPoint int
}

ForkRunResponse identifies the fork. Found is false when the source run does not exist; Created is false when NewRunID was claimed and already existed (no copy happens). On success both are true, RunID names the new run (ParentID records the lineage), and ForkPoint is the message count actually copied — the resolved fork position after clamping, also persisted on the fork's Run.

type FuncSource

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

FuncSource serves host-local Go functions as tools, so an agent can carry small utilities (dates, math, environment lookups) without running an MCP server for them. Registration is typed via AddFunc; schemas come from core.GenerateSchema on the input struct.

func NewFuncSource

func NewFuncSource() *FuncSource

NewFuncSource returns an empty source; register tools with AddFunc or AddToolFunc before handing it to a Runner.

func NewSignalSource

func NewSignalSource() *FuncSource

NewSignalSource returns a leaf ToolSource exposing the signal_parent control tool so a child agent can raise an upward Signal (issue 1165). Add it to a sub-agent's tool set; a top-level agent that has no parent gets a graceful "no parent to signal" result. Model-facing, so it lives in agent/ (A6).

func NewSpawnSource

func NewSpawnSource(pool *AgentPool) *FuncSource

NewSpawnSource returns a leaf ToolSource exposing the runner-control tools over pool: spawn_agent / await_agent / cancel_agent / list_agents (issue 1166). "Supervision = a Runner whose tools control other Runners" — this is that tool surface. Model-facing, so it lives in agent/ (A6). The spawn tool's description enumerates the pool's registered agents, so populate the pool (Register) before calling this.

func (*FuncSource) AddToolFunc

func (s *FuncSource) AddToolFunc(def core.ToolDef, fn func(ctx context.Context, args map[string]any) (*core.ToolResult, error)) error

AddToolFunc registers a tool with full control over the definition and the result shape. Use this when the tool needs structured content, a custom output schema, or non-text content items.

func (*FuncSource) Call

func (s *FuncSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call dispatches to the registered handler. Unknown names are dispatch errors; handler failures registered via AddFunc surface as IsError results.

func (*FuncSource) Tools

func (s *FuncSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools returns the registered definitions in registration order.

type GetToolResultRequest

type GetToolResultRequest struct {
	Ref string
}

GetToolResultRequest fetches by ref.

type GetToolResultResponse

type GetToolResultResponse struct {
	Result core.ToolResult
	Found  bool
}

GetToolResultResponse carries the result when Found. The returned value is the caller's own copy; in-memory implementations return the stored value (results are treated as immutable once stored, so no clone is needed — OffloadingSource never mutates a stored result).

type InMemoryMemoryStore

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

InMemoryMemoryStore is the default MemoryStore: a mutex-guarded map with stable insertion ordering, safe for concurrent use. Nothing survives process exit — a durable, session-scoped backend is a sibling-module follow-up (mirroring the ToolResultStore redis/gorm arc). An optional entry cap (WithMaxMemories) evicts the oldest item when exceeded.

func NewInMemoryMemoryStore

func NewInMemoryMemoryStore(opts ...MemoryStoreOption) *InMemoryMemoryStore

NewInMemoryMemoryStore returns an empty in-memory store.

func (*InMemoryMemoryStore) DeleteMemory

DeleteMemory implements MemoryStore.

func (*InMemoryMemoryStore) ListMemories

ListMemories implements MemoryStore: substring match on key or value (case-insensitive), oldest first, each match scored 1 (the substring store has no graded relevance). Limit caps the count.

func (*InMemoryMemoryStore) PutMemory

PutMemory implements MemoryStore.

type InMemoryRunStore

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

InMemoryRunStore is the default RunStore: a mutex-guarded map, useful for tests and single-process sessions that only need in-lifetime resume/fork. It is safe for concurrent use. Nothing survives process exit — durable deployments swap in a sibling backend behind the same interface.

func NewInMemoryRunStore

func NewInMemoryRunStore() *InMemoryRunStore

NewInMemoryRunStore returns an empty in-memory RunStore.

func (*InMemoryRunStore) AppendEvents

AppendEvents implements RunStore.

func (*InMemoryRunStore) AppendMessages

AppendMessages implements RunStore, stamping zero Timestamps per the AppendMessagesRequest rule.

func (*InMemoryRunStore) CreateRun

CreateRun implements RunStore. Generated IDs are sequential ("run-1", "run-2", ...) — deterministic on purpose, since an in-memory store never shares an ID space across processes.

func (*InMemoryRunStore) ForkRun

ForkRun implements RunStore. The fork gets cloned logs, so parent and fork diverge independently after the copy; see ForkRunRequest for the AtMessage cut and event-log semantics.

func (*InMemoryRunStore) ListRuns

ListRuns implements RunStore, newest-first. The cursor is a decimal offset into the ordered set — fine for an in-memory store whose set is stable within a process.

func (*InMemoryRunStore) LoadRun

LoadRun implements RunStore. The returned Run's slices are clones, so callers may append to or mutate them freely.

type InMemorySemanticStore

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

InMemorySemanticStore is a MemoryStore that recalls by embedding similarity instead of substring match. It composes an Embedder (text -> vector) with an in-process brute-force cosine index: PutMemory embeds the note and keeps its vector; ListMemories embeds the query and returns items ranked by cosine similarity, each carrying its Score. It implements the same MemoryStore interface as the substring default, so swapping it in makes the recall tool (and the summary) semantic with no change to the model-facing surface — the "how" of retrieval stays behind the interface.

The index is exact and O(n) per query, which is the right trade for a working-memory-sized scratchpad (tens to hundreds of notes). Approximate nearest-neighbor at scale is a durable-backend concern (a pgvector sibling MemoryStore), not something to build into the in-process default.

Concurrency: safe for concurrent use. Embedding happens outside the lock (a network call for a hosted Embedder), so Put/List never hold the mutex across I/O.

func NewInMemorySemanticStore

func NewInMemorySemanticStore(embedder Embedder, opts ...InMemorySemanticStoreOption) (*InMemorySemanticStore, error)

NewInMemorySemanticStore builds a semantic store over embedder. The embedder is required; every note and query is embedded with it, so a store must be queried with the same Embedder it was built with.

func (*InMemorySemanticStore) DeleteMemory

DeleteMemory removes an item and its vector. An unknown key is Deleted=false, not an error (same contract as the substring store).

func (*InMemorySemanticStore) ListMemories

ListMemories ranks items by cosine similarity to the query. An empty Query returns all items oldest-first with Score 0 (the "list everything" path the summary uses — there is no query to score against). Limit caps the result.

func (*InMemorySemanticStore) PutMemory

PutMemory embeds the note (key + value, so a recall query matches either) and upserts it. Embedding is synchronous — a just-remembered fact is immediately recallable; background/batch indexing of a distillation write path is a separate concern.

type InMemorySemanticStoreOption

type InMemorySemanticStoreOption func(*InMemorySemanticStore)

InMemorySemanticStoreOption configures a InMemorySemanticStore.

func WithSemanticMaxMemories

func WithSemanticMaxMemories(n int) InMemorySemanticStoreOption

WithSemanticMaxMemories caps the store at n items, evicting the oldest when a Put of a new key would exceed n. Zero or negative means unbounded.

func WithSemanticTracerProvider

func WithSemanticTracerProvider(tp core.TracerProvider) InMemorySemanticStoreOption

WithSemanticTracerProvider opts the store into an agent.memory.recall span per similarity query. Nil / NoopTracerProvider means zero overhead.

type InMemoryToolResultStore

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

InMemoryToolResultStore is the default ToolResultStore: a mutex-guarded map, safe for concurrent use. Nothing survives process exit — durable deployments swap in a sibling backend behind the same interface. An optional entry cap (WithMaxToolResults) evicts the least-recently-stored ref when exceeded; the graceful read contract makes that eviction safe. Default is unbounded.

func NewInMemoryToolResultStore

func NewInMemoryToolResultStore(opts ...ToolResultStoreOption) *InMemoryToolResultStore

NewInMemoryToolResultStore returns an empty in-memory store.

func (*InMemoryToolResultStore) GetToolResult

GetToolResult implements ToolResultStore.

func (*InMemoryToolResultStore) PutToolResult

PutToolResult implements ToolResultStore.

type IncomingEvent

type IncomingEvent struct {
	// Server identifies the source connection (the MultiSource id in
	// agentchat's wiring).
	Server string `json:"server"`

	// Name is the event name as declared in events/list.
	Name string `json:"name"`

	// ID is the delivery's event id, when the source provides one.
	ID string `json:"id,omitempty"`

	// Cursor is the replay cursor for cursored sources, empty otherwise.
	Cursor string `json:"cursor,omitempty"`

	// Time is when the host received the event (not the server's
	// timestamp; policies window on receipt time so an injected clock
	// governs tests).
	Time time.Time `json:"time"`

	// Data is the payload.
	Data core.RawJSON `json:"data"`

	// Meta is the occurrence-level _meta, opaque.
	Meta map[string]any `json:"meta,omitempty"`
}

IncomingEvent is the neutral event shape the policy engines consume, deliberately decoupled from any delivery mechanism: the events extension's stream, a webhook receiver, or a future page-bridge source all adapt into it. Wire-serializable per constraints A2/A5 (RawJSON payload) so wire surfaces can forward events verbatim.

func ShallowMergeJSON

func ShallowMergeJSON(acc, next IncomingEvent) IncomingEvent

ShallowMergeJSON is the default combiner for merge windows on IncomingEvent when no typed combiner is supplied: a shallow JSON-object merge where the newer event's fields win. Non-object payloads fall back to last-wins.

type InjectedContext

type InjectedContext struct {
	Event    IncomingEvent
	Text     string
	Priority string
}

InjectedContext is one rendered entry ready to join the model's context.

type ListMemoriesRequest

type ListMemoriesRequest struct {
	Query     string
	Limit     int
	Namespace string
}

ListMemoriesRequest filters by Query (empty means all) and caps the result at Limit (0 means no cap). Limit is the k of a top-k recall. Namespace scopes the listing to one scratchpad (empty = the default/global scratchpad); recall never crosses namespaces.

type ListMemoriesResponse

type ListMemoriesResponse struct {
	Items []ScoredMemory
}

ListMemoriesResponse carries the matching items, most-relevant first.

type ListRunsRequest

type ListRunsRequest struct {
	Cursor string
	Limit  int
}

ListRunsRequest pages the run listing. Cursor is empty for the first page and echoes a prior response's NextCursor thereafter; its content is backend-defined and opaque. Limit caps the page (zero means the backend's default).

type ListRunsResponse

type ListRunsResponse struct {
	Runs       []RunInfo
	NextCursor string
}

ListRunsResponse carries one page. NextCursor is empty on the last page. Ordering is best-effort newest-first where the backend can (in-memory, gorm); the Redis backend lists in scan order (unordered) — documented on its implementation.

type LoadRunRequest

type LoadRunRequest struct {
	RunID string
}

LoadRunRequest fetches a run by ID.

type LoadRunResponse

type LoadRunResponse struct {
	Run   Run
	Found bool
}

LoadRunResponse carries the run when Found. The returned Run is the caller's to keep: implementations return copies (or freshly decoded values), never aliases into store-internal state, so mutating Run.Messages cannot corrupt the log.

type MemoryItem

type MemoryItem struct {
	Key       string    `json:"key"`
	Value     string    `json:"value"`
	CreatedAt time.Time `json:"createdAt,omitzero"`
}

MemoryItem is one note in working memory: a short labeled value the model chose to keep. Key is the model-chosen (or auto-assigned) label used to recall or forget it; Value is the content; CreatedAt is when it was stored, used only for stable ordering in listings and the summary.

type MemorySource

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

MemorySource is working memory as a ToolSource: it exposes remember, recall, and forget tools over a MemoryStore, so the model manages its own scratchpad through ordinary tool calls. It is a leaf source (like FuncSource, not a wrapper like OffloadingSource) — a host adds it to its MultiSource alongside the server sources.

Summary renders the current scratchpad for optional pre-turn injection, keeping the model aware of what it has stored without a recall call every turn. Injection is the host's job (through its existing EventInjectionPolicy path); MemorySource only supplies the text, so the Runner never changes.

func NewMemorySource

func NewMemorySource(store MemoryStore, opts ...MemorySourceOption) (*MemorySource, error)

NewMemorySource builds a MemorySource over store and registers its three tools. The store is required.

func (*MemorySource) Call

func (m *MemorySource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call implements ToolSource by delegating to the internal FuncSource.

func (*MemorySource) RecallRelevant

func (m *MemorySource) RecallRelevant(ctx context.Context, query string, opts RecallOptions) (string, error)

RecallRelevant queries the store for notes relevant to query and renders them as a block a host can inject as a RoleSystem message before a turn. Unlike Summary (ambient, recency-budgeted, the whole scratchpad), this is targeted at the current turn: it surfaces what matters for what the user just said. The store's relevance ranking does the work (cosine for a semantic store, substring for the default), so this is backend-agnostic; it returns "" when the query is empty or nothing clears MinScore.

func (*MemorySource) Summary

func (m *MemorySource) Summary(ctx context.Context, opts SummaryOptions) (string, error)

Summary renders the current working memory as a block a host can inject as a RoleSystem message before a turn, honoring opts as a recency-priority budget. It returns "" when memory is empty (or the budget admits nothing) so the host injects nothing.

func (*MemorySource) Tools

func (m *MemorySource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools implements ToolSource by delegating to the internal FuncSource.

type MemorySourceOption

type MemorySourceOption func(*MemorySource)

MemorySourceOption configures a MemorySource.

func WithMemoryNamespaceFunc

func WithMemoryNamespaceFunc(fn func() string) MemorySourceOption

WithMemoryNamespaceFunc scopes every memory operation to the namespace fn returns at call time — pass a func that returns the current session/run id to make working memory per-session, or omit it for one shared scratchpad.

type MemoryStore

type MemoryStore interface {
	// PutMemory upserts an item by Key. Storing an existing Key overwrites
	// its Value and keeps its original listing position, so an update does
	// not reorder the scratchpad.
	PutMemory(ctx context.Context, req PutMemoryRequest) (PutMemoryResponse, error)

	// ListMemories returns items relevant to req.Query (all items when
	// Query is empty), most-relevant first, capped at req.Limit. The "how"
	// of relevance is the implementation's business — the substring default
	// filters and returns Score 1, a semantic store ranks by embedding
	// similarity, a pgvector backend does ANN — but the contract (ranked,
	// scored, top-k) is the same, which is why recall never branches on the
	// backend.
	ListMemories(ctx context.Context, req ListMemoriesRequest) (ListMemoriesResponse, error)

	// DeleteMemory removes an item by Key. Deleted reports whether a
	// matching item existed; an unknown Key is Deleted=false, not an error.
	DeleteMemory(ctx context.Context, req DeleteMemoryRequest) (DeleteMemoryResponse, error)
}

MemoryStore is the persistence seam for working memory. It pairs with the agent-only MemorySource (the model-facing tools), so it lives in agent/ and traffics in MemoryItem (A6 — same rationale as ToolResultStore and RunStore: a model-facing type keeps the seam out of root stores/).

API shape follows the gRPC-style convention in stores/STORAGE_SEAMS.md: Method(ctx, req) (resp, error); app-state travels on the response, error is reserved for storage-layer faults. In particular, forgetting an unknown key is app-state (Deleted=false), never an error — the model may forget a key it never stored, and that is a normal "nothing to do" answer, not a failure.

ListMemories carries an optional Query. The contract is loose on purpose: return the items relevant to Query, all items when Query is empty. The in-memory default interprets relevance as a substring match; a future semantic backend (issue 940) interprets the same Query as a similarity search without changing the tool surface the model sees.

type MemoryStoreOption

type MemoryStoreOption func(*InMemoryMemoryStore)

MemoryStoreOption configures an InMemoryMemoryStore.

func WithMaxMemories

func WithMaxMemories(n int) MemoryStoreOption

WithMaxMemories caps the store at n items, evicting the oldest when a Put of a new key would exceed n. Zero or negative means unbounded (the default).

type Message

type Message struct {
	Role Role `json:"role"`

	// Text is the message content. Empty is legal for assistant messages
	// that only carry tool calls.
	Text string `json:"text,omitempty"`

	// ToolCalls holds the calls an assistant message requested.
	ToolCalls []ToolCall `json:"toolCalls,omitempty"`

	// ToolCallID links a RoleTool message to the assistant ToolCall it
	// answers.
	ToolCallID string `json:"toolCallId,omitempty"`

	// Timestamp records when the message was said. Zero means unstamped;
	// RunStore implementations stamp zero-Timestamp messages with their
	// own clock on AppendMessages, and a caller-set non-zero value wins
	// (a surface can stamp the user message at keypress). Providers
	// never map this field into request bodies — it is session metadata,
	// not model input.
	Timestamp time.Time `json:"timestamp,omitzero"`
}

Message is one conversation entry in provider-neutral form. All fields are JSON-tagged so histories can cross a wire unchanged (constraint A2).

type MultiOption

type MultiOption func(*MultiSource)

MultiOption configures a MultiSource.

func WithCollisionNotify

func WithCollisionNotify(fn func(name string, sourceIDs []string)) MultiOption

WithCollisionNotify installs a hook invoked (synchronously, under the source lock) whenever Tools discovers a name claimed by multiple sources. Intended for logging/metrics, not control flow.

func WithResolver

func WithResolver(r Resolver) MultiOption

WithResolver installs the ambiguous-call resolver used for bare-name calls that collide.

type MultiSource

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

MultiSource aggregates ToolSources under stable source IDs, mirroring the collision semantics of ext/ui's ServerRegistry in ToolSource form:

  • Unique names are exposed and callable as-is.
  • Colliding names are exposed to the model ONLY in qualified form ("sourceID_name" for every claimant), so the model-facing list never contains duplicates and every tool stays reachable.
  • A bare-name Call that hits a collision consults the Resolver if one is configured, else fails with an error naming the qualified forms.

Qualification is deterministic: it depends only on the set of source IDs claiming the name, not on registration order.

func NewMultiSource

func NewMultiSource(opts ...MultiOption) *MultiSource

NewMultiSource returns an empty aggregator.

func (*MultiSource) Add

func (m *MultiSource) Add(id string, src ToolSource) error

Add registers a source under id. IDs must be unique and must not contain "/", which is reserved as the qualified-name separator; rejecting it keeps "sourceID/name" parsing unambiguous. Underscores are fine — id namespaces are commonly snake_case (e.g. "subagent:deep_researcher").

func (*MultiSource) Call

func (m *MultiSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call dispatches by bare or qualified name. Resolution order: exact unique bare name; qualified "sourceID_name"; ambiguous bare name via Resolver. A name miss against the memoized index triggers exactly one fresh gather before failing, so tools registered after the last listing stay reachable.

func (*MultiSource) Invalidate

func (m *MultiSource) Invalidate()

Invalidate drops the memoized tool index; the next Tools or Call gathers fresh lists from every source. Wire this to tools/list_changed notifications when the embedding host tracks them.

func (*MultiSource) Remove

func (m *MultiSource) Remove(id string)

Remove drops a source. Unknown ids are a no-op.

func (*MultiSource) SourceTools

func (m *MultiSource) SourceTools(ctx context.Context, id string) (defs []core.ToolDef, found bool, err error)

SourceTools returns the tools of the single source registered under id, in that source's own (unqualified) naming — the per-source view a caller uses to show "what does server X expose", distinct from the merged Tools list where colliding names are qualified. found is false for an unknown id (app state, not an error); err is only a real listing failure from that source.

func (*MultiSource) Tools

func (m *MultiSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools returns the merged, deduplicated model-facing list: unique names as-is (source order preserved), collisions replaced by qualified names for every claimant.

type OffloadConfig

type OffloadConfig struct {
	// Threshold is the offload cutoff in bytes of flattened result text.
	// Zero means DefaultOffloadThreshold. Results below it inline
	// unchanged.
	Threshold int

	// PreviewLen is the leading-character count the stub carries. Zero
	// means DefaultOffloadPreview.
	PreviewLen int

	// PerToolThreshold overrides Threshold for named tools. A present
	// entry with value <= 0 pins that tool to never offload (always
	// inline), whatever its size — the escape hatch for a tool whose
	// full output the model must always see verbatim.
	PerToolThreshold map[string]int
}

OffloadConfig tunes an OffloadingSource. The zero value is usable (default threshold and preview); only Store is required and is set by the constructor, not here.

type OffloadingSource

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

OffloadingSource wraps a ToolSource so that large successful tool results are stored out of band and replaced in the conversation by a compact stub, with a read_tool_result tool for fetching the detail on demand (the "just in time context" pattern). It composes exactly like FilterSource: put it around the aggregate MultiSource and hand the result to the Runner. No Runner change — the stub is a normal ToolResult, so the RoleTool message, the tool-end event, and the persisted log all carry the stub, keeping the log faithful to what the model actually saw.

Only successful results are offloaded: IsError results stay inline (errors are usually short, and truncating one is worse than carrying it). The stored blob keeps the full result including StructuredContent; the stub is text-only.

func NewOffloadingSource

func NewOffloadingSource(src ToolSource, store ToolResultStore, cfg OffloadConfig) *OffloadingSource

NewOffloadingSource wraps src, offloading over-threshold results into store. Store is required; a nil store panics at construction rather than silently dropping results at call time.

func (*OffloadingSource) Call

func (o *OffloadingSource) Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)

Call routes read_tool_result to the offloader's own handler and every other name to the wrapped source, offloading the result when it is a successful over-threshold payload.

func (*OffloadingSource) Tools

func (o *OffloadingSource) Tools(ctx context.Context) ([]core.ToolDef, error)

Tools lists the wrapped source's tools plus read_tool_result. The read tool is always offered while offloading is active, since a stub can appear on any call.

type OpenAIConfig

type OpenAIConfig struct {
	// BaseURL is the API root including any version prefix, e.g.
	// "http://localhost:1234/v1". The provider appends "/chat/completions".
	BaseURL string

	// APIKey, when non-empty, is sent as a Bearer token. Local servers
	// commonly need none.
	APIKey string

	// Model is the model identifier sent on every request.
	Model string

	// HTTPClient overrides http.DefaultClient. Set this for proxies,
	// custom TLS, or timeouts (note: an overall client timeout also bounds
	// streaming reads; prefer per-request ctx deadlines for streams).
	HTTPClient *http.Client
}

OpenAIConfig configures an OpenAI-compatible chat-completions endpoint (OpenAI, lmstudio, vllm, LiteLLM-style proxies, gateways).

type OpenAIEmbedder

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

OpenAIEmbedder is an Embedder over any OpenAI-compatible /embeddings endpoint (OpenAI, LM Studio, Ollama, ...), using net/http directly with no SDK dependency — the same no-SDK approach as OpenAIProvider.

func NewOpenAIEmbedder

func NewOpenAIEmbedder(cfg OpenAIEmbedderConfig) (*OpenAIEmbedder, error)

NewOpenAIEmbedder validates cfg and returns the embedder. BaseURL and Model are required.

func (*OpenAIEmbedder) Embed

func (e *OpenAIEmbedder) Embed(ctx context.Context, texts []string) ([]Embedding, error)

Embed implements Embedder. It sends all texts in one request and returns the embeddings reordered to match the input order (the API echoes an index per embedding, which need not be in request order).

type OpenAIEmbedderConfig

type OpenAIEmbedderConfig struct {
	// BaseURL is the API root including any version prefix, e.g.
	// "http://localhost:1234/v1". The embedder appends "/embeddings".
	BaseURL string

	// APIKey, when non-empty, is sent as a Bearer token. Local servers
	// commonly need none.
	APIKey string

	// Model is the embedding model identifier sent on every request.
	Model string

	// HTTPClient overrides http.DefaultClient.
	HTTPClient *http.Client

	// TracerProvider opts the embedder into an agent.embed span per call
	// (input count + model attributes). Nil or NoopTracerProvider means
	// zero overhead, the repo-wide pattern.
	TracerProvider core.TracerProvider
}

OpenAIEmbedderConfig configures an OpenAIEmbedder.

type OpenAIProvider

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

OpenAIProvider implements Provider over the OpenAI-compatible chat-completions wire with no SDK dependency (net/http plus servicekit's WHATWG-conformant SSE reader). Safe for concurrent use.

func NewOpenAIProvider

func NewOpenAIProvider(cfg OpenAIConfig) (*OpenAIProvider, error)

NewOpenAIProvider validates cfg and returns a provider. BaseURL and Model are required.

func (*OpenAIProvider) Generate

Generate implements Provider with a non-streaming request. When req.ResponseSchema is set, the request carries response_format json_schema and the structured document is returned in ProviderResponse.Text.

func (*OpenAIProvider) Stream

func (p *OpenAIProvider) Stream(ctx context.Context, req ProviderRequest) (Stream, error)

Stream implements Provider. Deltas map 1:1 from SSE chunks; tool calls arrive as DeltaToolCallStart followed by DeltaToolCallArgs fragments on the same index. The stream ends with io.EOF after the servers [DONE] marker.

type Pipeline

type Pipeline[E any] struct {
	// contains filtered or unexported fields
}

Pipeline chains stages: each event Pushed cascades through every stage in order, and Flush cascades expirations the same way.

func NewPipeline

func NewPipeline[E any](stages ...Stage[E]) *Pipeline[E]

NewPipeline builds a pipeline from stages, applied in order.

func (*Pipeline[E]) Flush

func (p *Pipeline[E]) Flush(now time.Time) []E

Flush cascades window expirations: stage i's flushed events still traverse stages i+1..n before release.

func (*Pipeline[E]) Push

func (p *Pipeline[E]) Push(now time.Time, ev E) []E

Push cascades ev through the stages.

type Provider

type Provider interface {
	// Stream runs one model call and delivers the response incrementally.
	Stream(ctx context.Context, req ProviderRequest) (Stream, error)

	// Generate runs one model call to completion. When
	// req.ResponseSchema is set, implementations request structured
	// output conforming to it and return the JSON document in Text.
	Generate(ctx context.Context, req ProviderRequest) (*ProviderResponse, error)
}

Provider is the LLM seam. Implementations must be safe for concurrent use; each Stream call is an independent model invocation.

func NewThinkingProvider

func NewThinkingProvider(p Provider, openTag, closeTag string) Provider

NewThinkingProvider wraps p so inline reasoning that a model emits in its text stream — delimited by openTag/closeTag, e.g. "<think>…</think>" — is re-emitted as DeltaReasoning instead of DeltaText. That is all the Runner needs: its consumeStream already turns DeltaReasoning into thinking-begin/delta/end events, so a surface renders the reasoning distinctly with no Runner change.

openTag empty means reasoning starts at the stream head and runs until the first closeTag (the "no open tag" models that stream reasoning first, then the answer). closeTag empty makes the hint inert: p is returned unwrapped, since without a terminator there is nothing to delimit.

The transform is delimiter-boundary safe: a tag split across two provider deltas (a "<thi" / "nk>" boundary) is buffered and matched whole. Native DeltaReasoning from a provider that already separates reasoning passes through untouched — the parser only reinterprets DeltaText.

type ProviderError

type ProviderError struct {
	StatusCode int
	Body       string
}

ProviderError reports a non-2xx response from the model endpoint. The body is included verbatim (truncated to 2 KB) because OpenAI-compatible servers put the useful diagnostics there.

func (*ProviderError) Error

func (e *ProviderError) Error() string

Error implements error.

type ProviderHealth

type ProviderHealth struct {
	// Active is "primary" or "backup".
	Active string `json:"active"`

	// ConsecutiveFailures counts primary failures since its last success.
	ConsecutiveFailures int `json:"consecutiveFailures"`

	// LastError is the most recent primary failure, empty when healthy.
	LastError string `json:"lastError,omitempty"`

	// LastFailureAt is when the primary last failed (zero when never).
	LastFailureAt time.Time `json:"lastFailureAt,omitzero"`
}

ProviderHealth is the pollable failover snapshot, the host-facing equivalent of a connection-status endpoint.

type ProviderRequest

type ProviderRequest struct {
	// Instructions is the system prompt. Providers map it to their native
	// system slot; it is not a Message so histories stay role-clean.
	Instructions string `json:"instructions,omitempty"`

	// Messages is the conversation so far, oldest first.
	Messages []Message `json:"messages"`

	// Tools lists what the model may call. Nil means no tools offered.
	Tools []core.ToolDef `json:"tools,omitempty"`

	// Temperature overrides the provider default when non-nil.
	Temperature *float64 `json:"temperature,omitempty"`

	// MaxTokens caps the completion length when positive.
	MaxTokens int `json:"maxTokens,omitempty"`

	// ToolChoice biases tool calling for this request. Empty is the
	// provider default ("auto"). Use ToolChoiceRequired to force some
	// tool call, ToolChoiceNone to forbid, or ToolChoiceFunc(name) to
	// force a specific tool. Pairs with RunnerConfig.Selector (narrow the
	// set) to steer a proactive or injected turn toward acting rather than
	// only replying. Support varies across OpenAI-compatible servers; a
	// server that ignores it degrades to "auto".
	ToolChoice ToolChoice `json:"toolChoice,omitempty"`

	// ResponseSchema, when set, asks Generate for structured output
	// conforming to this JSON Schema. Ignored by Stream.
	ResponseSchema core.RawJSON `json:"responseSchema,omitempty"`
}

ProviderRequest is one model call in provider-neutral form.

type ProviderResponse

type ProviderResponse struct {
	Text         string     `json:"text,omitempty"`
	Reasoning    string     `json:"reasoning,omitempty"`
	ToolCalls    []ToolCall `json:"toolCalls,omitempty"`
	FinishReason string     `json:"finishReason,omitempty"`
	Usage        *Usage     `json:"usage,omitempty"`
}

ProviderResponse is a completed model call: the fold of a delta stream, or the direct result of Generate.

type PutMemoryRequest

type PutMemoryRequest struct {
	Item      MemoryItem
	Namespace string
}

PutMemoryRequest carries the item to store. A zero CreatedAt is stamped with the store clock at Put time (caller-set wins, mirroring Message.Timestamp). Namespace scopes the item to one independent scratchpad (a session id, a user id, ...); empty is the default/global scratchpad. It is the per-request session-scope seam (issue 1003) — a caller that never sets it gets the single shared scratchpad, unchanged.

type PutMemoryResponse

type PutMemoryResponse struct{}

PutMemoryResponse is empty today; it exists so the method can grow app-state without a signature break, per the gRPC-style convention.

type PutToolResultRequest

type PutToolResultRequest struct {
	Ref    string
	Result core.ToolResult
}

PutToolResultRequest carries the ref and the full result to retain.

type PutToolResultResponse

type PutToolResultResponse struct{}

PutToolResultResponse is empty today; it exists so the method can grow app-state (an assigned ref, a stored-bytes count) without a signature break, per the gRPC-style convention.

type RecallOptions

type RecallOptions struct {
	// TopK caps how many of the most-relevant notes are returned. Zero uses
	// DefaultRecallTopK.
	TopK int
	// MinScore drops notes scoring below it — the poison guard: with a
	// semantic store every note gets some cosine score, so without a floor a
	// low-TopK recall would inject the least-irrelevant notes even when
	// nothing is actually relevant. Zero means no floor (keep all TopK).
	MinScore float64
}

RecallOptions bounds a pre-turn relevance recall (RecallRelevant).

type Resolver

type Resolver func(name string, candidates []ToolOwner, args map[string]any) (sourceID string, err error)

Resolver picks which source handles an ambiguous bare-name call. Returning an empty string (or an error) fails the call; the caller can always reach a specific candidate via the qualified "sourceID_name" form instead.

type Role

type Role string

Role identifies who authored a Message.

const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
	// RoleSystem carries injected context (events, trigger instructions)
	// into the conversation; providers map it to their native system
	// slot. Distinct from ProviderRequest.Instructions, which is the
	// static prompt: RoleSystem messages live in history and thread
	// across turns like any other message.
	RoleSystem Role = "system"
)

Message roles. RoleTool carries a tool result back to the model and must set ToolCallID; RoleAssistant messages may carry ToolCalls.

type Run

type Run struct {
	ID string `json:"id"`

	// ParentID is the run this one was forked from; empty for runs
	// created directly.
	ParentID string `json:"parentId,omitempty"`

	// ForkPoint is the number of ParentID's messages this run was
	// forked with — the fork position as a backend-neutral message
	// count (never a timestamp: the fork's wall-clock moment is this
	// run's CreatedAt; never a storage sequence value either). Zero for
	// runs created directly. Lineage metadata for surfaces (rewind
	// pickers, history UIs): nothing reconstructs history from it,
	// since every run owns its full copy.
	ForkPoint int `json:"forkPoint,omitempty"`

	CreatedAt time.Time `json:"createdAt"`

	Messages []Message `json:"messages"`
	Events   []Event   `json:"events,omitempty"`
}

Run is one persisted run: identity, fork lineage, and the append-only logs. Messages is the conversation history a resume feeds back into Runner.Run; Events is the optional replay/audit stream. Both element types are wire-serializable by constraint A2, so Run itself marshals cleanly through encoding/json — the property durable backends rely on.

type RunInfo

type RunInfo struct {
	ID           string    `json:"id"`
	ParentID     string    `json:"parentId,omitempty"`
	ForkPoint    int       `json:"forkPoint,omitempty"`
	CreatedAt    time.Time `json:"createdAt"`
	MessageCount int       `json:"messageCount"`
}

RunInfo is the header of a run — identity, lineage, and counts — without the message or event bodies, so a listing stays cheap. MessageCount is the current length of the message log; the fork lineage (ParentID, ForkPoint) is the session tree a picker renders.

type RunStore

type RunStore interface {
	CreateRun(ctx context.Context, req CreateRunRequest) (CreateRunResponse, error)
	AppendMessages(ctx context.Context, req AppendMessagesRequest) (AppendMessagesResponse, error)
	AppendEvents(ctx context.Context, req AppendEventsRequest) (AppendEventsResponse, error)
	LoadRun(ctx context.Context, req LoadRunRequest) (LoadRunResponse, error)
	ForkRun(ctx context.Context, req ForkRunRequest) (ForkRunResponse, error)

	// ListRuns enumerates stored runs as lightweight RunInfo (no message
	// bodies), for a session picker or a dashboard. Paged via an opaque
	// cursor; NextCursor is empty when the last page was returned. A
	// non-agent consumer (a poller, a UI) wants this too, which is why it
	// returns protocol/data objects, not model-facing ones.
	ListRuns(ctx context.Context, req ListRunsRequest) (ListRunsResponse, error)
}

RunStore is the persistence seam for runs: append-only logs of the messages (and optionally events) a session accumulates across turns. Surfaces persist TurnResult.Messages after each turn; resume is LoadRun followed by Run over the loaded messages, and fork is ForkRun followed by divergence — both cheap because the Runner is stateless over history. The Runner itself never touches a RunStore.

API shape follows the gRPC-style convention pinned in stores/STORAGE_SEAMS.md:

Method(ctx context.Context, req XRequest) (XResponse, error)

ctx threads cancellation, deadlines, and trace context. Application-level state (Found, Created) lives on the response; error is reserved for storage-layer failures (connection drops, corrupt records). In particular, an unknown RunID is app-state (Found=false), never an error.

The interface and its in-memory default live in agent/ because every method traffics in agent.Message / agent.Event: hosting them in the root stores/ package would force a root→agent dependency (constraint A6 corollary). Durable backends are sibling modules (agent/store/...) so their dependencies stay out of this module.

Atomicity contract: CreateRun with an explicit RunID and ForkRun are all-or-nothing. On error, no run observable at the requested ID came into existence; a run exists at that ID only because some create or fork fully committed. This is what makes caller-chosen IDs safe idempotency keys for retry loops (see ForkRunRequest). Implementers must uphold it — a partially-forked run that reports Created=false to a retry is a contract violation, not a quirk.

type Runner

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

Runner executes turns: the multi-step loop that streams the model, dispatches its tool calls, feeds results back, and repeats until the model answers in text. Safe for concurrent use; each Run call is an independent turn.

func NewRunner

func NewRunner(cfg RunnerConfig) (*Runner, error)

NewRunner validates cfg and returns a Runner.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, history []Message, emit func(Event)) (*TurnResult, error)

Run executes one turn against history. Events stream to emit (nil is allowed); emit is never called concurrently. Tool failures of every kind (unknown tool, transport, bad args) are fed back to the model as error-marked tool results and the loop continues; only ctx cancellation, provider failure, or the step cap abort the turn. The returned error wraps ErrMaxSteps when the cap was hit. Run is shorthand for RunTurn without mid-turn controls.

func (*Runner) RunTurn

func (r *Runner) RunTurn(ctx context.Context, req TurnRequest) (*TurnResult, error)

RunTurn executes one turn with the full request surface: Run's contract plus mid-turn Controls (per-call cancellation). See TurnRequest for the semantics of each field.

type RunnerConfig

type RunnerConfig struct {
	// Provider is the LLM. Required.
	Provider Provider

	// Tools is the tool surface offered to the model. Optional: nil means
	// the model is offered no tools and any hallucinated call fails back
	// into the conversation.
	Tools ToolSource

	// Instructions is the system prompt sent on every step.
	Instructions string

	// InstructionsFunc, when non-nil, is called once at the top of each turn to
	// compute that turn's system prompt, overriding the static Instructions. It
	// lets the prompt track dynamic state — the set of currently-connected
	// servers whose eager skills belong in the prompt, the date, injected
	// context — instead of being frozen at construction. Recomputed per turn
	// (not per step), so the prompt is stable within a turn and a provider
	// cache only breaks when the value actually changes.
	//
	// It is per-Runner: each Runner is built from its own RunnerConfig, so a
	// sub-agent's Runner has its own InstructionsFunc (or none) — different
	// runners can produce different prompts. The func captures whatever context
	// it needs (e.g. the host's set of connected servers, its own tool view) via
	// closure; the Runner is deliberately NOT passed, which would be circular
	// (the Runner holds this config). The ctx is for cancellation/deadline only.
	InstructionsFunc func(context.Context) string

	// MaxSteps caps model calls per turn. Zero means DefaultMaxSteps.
	MaxSteps int

	// TreeBudget caps aggregate steps/tokens across the whole sub-agent tree
	// (parent + sub-agents + fan-out members + handoff rounds), complementing
	// the per-Runner MaxSteps. The Runner installs it on ctx at the top of a
	// turn ONLY if the ctx does not already carry one, so a top-level Runner's
	// budget is inherited and shared by every child run (do not set it on
	// sub-agent Runner configs — they inherit it). Zero (both dimensions) is no
	// budget. Equivalent to calling WithTreeBudget on the turn ctx yourself.
	TreeBudget TreeBudget

	// TracerProvider opts the Runner into SEP 414 span emission:
	// agent.turn per Run, agent.step per model call, agent.tool per
	// dispatch, with ctx threading so client-side dispatch spans (and
	// through them server spans) stitch as children. Nil or
	// core.NoopTracerProvider means zero overhead, the repo-wide pattern.
	TracerProvider core.TracerProvider

	// MeterProvider opts the Runner into OTel metric emission (issue 1023),
	// the metrics sibling of the trace spans: a turn counter + duration
	// histogram, a steps counter, a tokens counter (by direction), and a
	// tool-call counter + duration histogram (by tool and status). Emitted
	// at the same points the spans are. Nil or core.NoopMeterProvider means
	// zero overhead, the same repo-wide pattern as TracerProvider.
	MeterProvider core.MeterProvider

	// Selector, when non-nil, narrows the tools offered to the model each
	// step. It runs on the freshly listed set with the full history, so
	// context-aware routing (keyword, embedding, scored) plugs in here.
	// Selectors must stay pure functions of (history, tools): any cache a
	// selector keeps should key on tool-list content, never on time or
	// notifications, so list-changed invalidation has exactly one source
	// (the ToolSource layer). A selector error aborts the turn: it is a
	// host configuration bug, not something the model can recover from.
	Selector ToolSelector

	// Approval, when non-nil, gates each tool call before it runs: the
	// Runner asks the policy in callTool, after argument binding and before
	// ToolSource.Call. A refusal is fed back to the model as a tool result
	// (a tool-denied event, then the turn continues), never a turn abort.
	// Nil means every call runs, the pre-approval behavior.
	Approval ApprovalPolicy

	// Compactor, when non-nil, may rewrite the turn's history before the
	// first model call — the head summarized, a recent tail kept verbatim —
	// to keep a long conversation under a context budget. The Runner calls
	// it on its own clone of the history, so Run stays stateless over the
	// history it is handed; a no-op (Compactor returns the input unchanged)
	// emits nothing, a real compaction emits EventCompaction. A Compactor
	// error aborts the turn (a misconfiguration or summarizer-provider
	// outage, not something the model can recover from), mirroring Selector.
	// Nil means history is sent verbatim. Mid-turn compaction (a single turn
	// that itself grows past the budget) is a follow-up; this fires once,
	// pre-loop.
	Compactor Compactor

	// Interruptible opts this Runner's turn into breaking the fan-out join
	// barrier when a child raises an upward Signal mid-flight (issue 1167, piece
	// C of the 1036 control axis). Default false keeps the turn a pure
	// fan-out-then-join — the property resume / fork / eval / compaction rely on;
	// only a signal-wired turn should set it. When true, the first barrier-
	// breaking signal a child raises during a dispatch (see shouldBreakOn:
	// escalate always, preempt only when PreemptGrant honors it, custom never)
	// cancels the remaining in-flight calls (they feed back "cancelled by user")
	// and the dispatch returns the partial results, so the turn's step loop
	// re-enters the model to re-plan. With no breaking signal, an interruptible
	// turn still waits for every call, identical to the default. The re-entry
	// ordering is the one bounded-nondeterminism exception to the pure turn;
	// every emitted event still projects 1:1 (A2).
	Interruptible bool

	// SignalPolicy, when non-nil, decides how this Runner (as a parent) reacts
	// to the upward Signals its children raised during a dispatch, read at the
	// join (issue 1165). It runs after the fan-out has joined, so it chooses
	// only whether to abort the turn (SignalAction.AbortTurn -> ErrSignalAbort);
	// the signals are injected into the next step as a RoleSystem note either
	// way, so the parent model sees them. Nil means inject-and-continue. See
	// AbortOnEscalate for the built-in deterministic policy.
	SignalPolicy SignalPolicy

	// PreemptGrant gates whether a child's advisory SignalPreempt breaks the
	// interruptible join barrier (cancelling the other in-flight calls). It is
	// the parent's authority over a claim the child cannot actually verify (a
	// child under A7 isolation cannot know the global goal). Nil (the default)
	// means a preempt never breaks — it is injected like any signal and the
	// parent model decides on re-plan, so a rogue or prompt-injected child
	// cannot unilaterally cancel its siblings. Non-nil is consulted per preempt
	// signal (it may inspect Source/Note to honor only trusted children);
	// returning true grants the preemption. Must be pure and goroutine-safe (it
	// is called from a child's raise). Only consulted when Interruptible is set;
	// it does not gate SignalEscalate, which breaks unconditionally.
	PreemptGrant func(sig Signal) bool

	// ResponseSchema, when set, coerces the turn's final answer into
	// structured output. After the tool loop reaches its terminal
	// no-tool-call text, the Runner makes one additional Generate call with
	// this schema (and no tools) and puts the JSON document on
	// TurnResult.Structured. Tools and a response schema are never sent in
	// the same request (many endpoints forbid it), which is why this is a
	// separate finalizing call rather than a field on the loop's requests.
	// Empty means no structured coercion.
	ResponseSchema core.RawJSON
}

RunnerConfig assembles a Runner.

type ScoredMemory

type ScoredMemory struct {
	Item  MemoryItem
	Score float64
}

ScoredMemory pairs a stored MemoryItem with its per-query relevance Score. Score is a property of the QUERY, not of the stored fact (the same item scores differently against different queries), which is why it lives here on the result and not on MemoryItem — that keeps MemoryItem the durable, query-independent fact. A substring store returns 1 for a match; a semantic store returns cosine similarity. Room to grow: a future ranking/reranker stage can add per-signal provenance (similarity vs recency vs importance) here without touching the store or the item.

type ServerAgentConfig

type ServerAgentConfig struct {
	// Name is the delegate tool name the parent model sees and calls. It is
	// what the supervisor routes on; the scoped Tools never enter the parent's
	// context. Required.
	Name string

	// Description tells the parent model when to delegate to this agent. The
	// host folds the roster fields (description, capabilities, example tasks)
	// into it. Required.
	Description string

	// Instructions is the agent's system prompt from agents/get. It seeds the
	// child Runner, so the child behaves as the server author declared.
	Instructions string

	// Tools is the agent's scoped tool set from agents/get: the schemas the
	// child Runner advertises to its own model. These are the ONLY tools the
	// child can call — the capability boundary that keeps a server-advertised
	// agent to its declared scope, not the server's full tools/list. Their
	// names must match tools the Backing source can dispatch (the same server's
	// tools, per the WG execution model: the host loops the child's tool calls
	// back to the server).
	Tools []core.ToolDef

	// Backing executes a scoped tool call. In the host it is the ClientSource
	// for the server that advertised this agent, so a call the child makes is
	// dispatched via tools/call back to that server. Only its Call is used;
	// its own Tools list is ignored (the scoped Tools above are authoritative).
	// Required.
	Backing ToolSource

	// Provider is the LLM the child Runner uses. Required. In the host it is
	// the shared main provider, mirroring the persona sub-agents.
	Provider Provider

	// MaxSteps caps the child's model calls per turn. Zero uses the Runner
	// default.
	MaxSteps int

	// MaxDepth caps sub-agent nesting depth. Zero uses DefaultMaxAgentDepth.
	MaxDepth int

	// OnEvent, when set, receives the child's event stream wrapped in a
	// SubAgentEvent envelope for nested rendering, exactly as for a persona
	// AgentSource. Nil runs the child invisibly.
	OnEvent func(SubAgentEvent)

	// TracerProvider and MeterProvider opt the child Runner into the same
	// SEP-414 spans / OTel metrics as any other Runner. Nil means zero
	// overhead. The dedicated spans for the agents extension itself are issue
	// 1145; this just threads the existing Runner instrumentation.
	TracerProvider core.TracerProvider
	MeterProvider  core.MeterProvider

	// ResponseSchema, when set, coerces the child's final answer into
	// structured output (AgentSource.Call then returns the coerced JSON).
	// Empty keeps the plain-text answer.
	ResponseSchema core.RawJSON
}

ServerAgentConfig bridges a decoded server-advertised agent definition (the experimental/ext/agents agents/get result: instructions + a scoped tool set) into the existing composition surface. NewServerAgentSource turns it into an AgentSource the parent delegates to exactly like any other sub-agent.

The deliberate non-coupling: this config takes the decoded pieces (Instructions string, Tools []core.ToolDef) plus a Backing ToolSource, NOT the experimental agents.AgentDetail wire type. That keeps agent/ free of the experimental extension module — the host layer decodes the wire object and hands the parts here (agent/CONSTRAINTS.md A6: this is agent-layer because constructing a Runner needs a model + a turn).

type Signal

type Signal struct {
	// Kind classifies the signal; a SignalPolicy switches on it.
	Kind SignalKind `json:"kind"`
	// Name names a SignalCustom signal (ignored for the built-in kinds).
	Name string `json:"name,omitempty"`
	// Note is a short reason, surfaced when the signal is injected into the
	// parent's next step.
	Note string `json:"note,omitempty"`
	// Data is an optional JSON payload for a SignalCustom signal (A5: RawJSON).
	Data core.RawJSON `json:"data,omitempty"`
	// Source is the raising child's scope path (agentScope), stamped by
	// RaiseSignal so a policy or the model knows who signalled. Callers do not
	// set it.
	Source string `json:"source,omitempty"`
}

Signal is an upward message from a child agent to its parent Runner. It is wire-serializable (constraint A2) so a remote child raises it identically to a co-located one: RaiseSignal writes it into the ctx-threaded sink the parent installs around the dispatch that spawned the child, and the parent reads the drained signals at the join.

type SignalAction

type SignalAction struct {
	// AbortTurn ends the parent turn immediately with ErrSignalAbort; Reason is
	// folded into the error. Use it for an escalation that should stop the parent.
	AbortTurn bool
	// Reason is the abort reason (and the turn error message). Ignored unless
	// AbortTurn is set.
	Reason string
}

SignalAction is a SignalPolicy's verdict over the signals drained at one dispatch join. The zero value continues the turn (inject-and-proceed).

func AbortOnEscalate

func AbortOnEscalate(signals []Signal) SignalAction

AbortOnEscalate is a built-in SignalPolicy that aborts the parent turn as soon as a child raises SignalEscalate, folding that child's note into the error. Other signal kinds inject-and-continue. It is the deterministic counterpart to letting the parent model decide via injection.

type SignalKind

type SignalKind string

SignalKind classifies an upward signal a child agent raises to its parent Runner — the control-axis complement to the downward Control (issue 936).

const (
	// SignalEscalate asks the parent to stop and handle the child's finding
	// ("decisive result" / "an error you must deal with"). With the
	// AbortOnEscalate policy the parent turn ends; otherwise the signal is
	// injected and the parent model decides.
	SignalEscalate SignalKind = "escalate"

	// SignalCustom carries an application-defined signal (named by Name, with an
	// optional Data payload) for a SignalPolicy or the parent model to interpret.
	// It is FYI-only: it is collected and injected into the parent's next step,
	// but does NOT break an interruptible dispatch's join barrier (see interrupts).
	SignalCustom SignalKind = "custom"

	// SignalPreempt is a child's *advisory* claim that its result may make the
	// parallel work unnecessary. It is deliberately NOT authoritative: a child
	// under A7 isolation cannot know the parent's global goal or what its
	// siblings are doing, so "the parallel work is moot" is its assertion, never
	// ground truth. A preempt therefore breaks the interruptible join barrier
	// (cancelling the other in-flight calls) ONLY when the parent grants it via
	// RunnerConfig.PreemptGrant; with no grant (the default) a preempt is merely
	// collected and injected like any signal, and the parent model decides on
	// re-plan — so a rogue or prompt-injected child cannot unilaterally kill its
	// siblings. It stays non-referential either way: the child names no sibling;
	// the parent, which alone holds the fan-out inventory, decides.
	SignalPreempt SignalKind = "preempt"
)

type SignalPolicy

type SignalPolicy func(signals []Signal) SignalAction

SignalPolicy decides how a parent Runner reacts to the signals its children raised during one dispatch, read at the join. It runs after the fan-out has joined (issue 1165 is non-interruptible), so it chooses only whether to abort the parent turn; the signals are injected into the next step regardless. Nil means inject-and-continue. A policy must be a pure function of the drained signals.

type SpawnStatus

type SpawnStatus struct {
	ID     string `json:"id"`
	Name   string `json:"name"`
	Status string `json:"status"`
}

SpawnStatus is a wire-serializable snapshot of a handle for list_agents (constraint A2).

type Stage

type Stage[E any] interface {
	Push(now time.Time, ev E) []E
	Flush(now time.Time) []E
}

Stage is one step of an event pipeline, generic over the event type so the machinery is promotable to any consumer (webhook receivers, page bridges) unchanged: nothing here knows about MCP or the model. Push feeds one event and returns whatever the stage releases now; Flush releases anything whose window has expired by now. Stateless stages return their result from Push and nil from Flush. Stages are not safe for concurrent use; drive each pipeline from one goroutine (the policies do).

func Filter

func Filter[E any](fn func(E) bool) Stage[E]

Filter keeps events for which fn returns true.

func Transform

func Transform[E any](fn func(E) (E, bool)) Stage[E]

Transform rewrites events; returning false drops the event.

func Window

func Window[E any](d time.Duration, strategy WindowStrategy, key func(E) string, merge func(acc, next E) E) Stage[E]

Window buffers events per key and releases per strategy when the window expires. For last-wins and merge the window opens at the FIRST event of a burst (bounded latency); for debounce every event restarts the window (quiet-gap semantics). merge folds with the supplied combiner; nil merge falls back to last-wins behavior.

type Stream

type Stream interface {
	Recv() (Delta, error)
	Close() error
}

Stream delivers Deltas for one model call. Recv returns io.EOF after the final delta. Close releases the underlying connection and is safe to call at any point, including concurrently with Recv (which then returns an error). Streams are single-consumer.

type StubEmbedder

type StubEmbedder struct {
	// Dim is the vector width. Zero uses DefaultStubEmbedderDim.
	Dim int
}

StubEmbedder is a deterministic, dependency-free Embedder for tests: it projects each text into a fixed-width bag-of-words vector (each lowercased token hashed into a bucket), so texts that share words produce similar vectors and CosineSimilarity is meaningful. No model, no network, stable across runs — the embedding counterpart of StubProvider.

func (StubEmbedder) Embed

func (e StubEmbedder) Embed(ctx context.Context, texts []string) ([]Embedding, error)

Embed implements Embedder.

type StubProvider

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

StubProvider is the deterministic Provider used by tests: it plays scripted turns in order and records every request it receives. Safe for concurrent use; turns are consumed in call order.

func NewStubProvider

func NewStubProvider(turns ...StubTurn) *StubProvider

NewStubProvider returns a provider that plays turns in order and errors when called past the script.

func (*StubProvider) Generate

Generate implements Provider by folding the scripted turn.

func (*StubProvider) Requests

func (s *StubProvider) Requests() []ProviderRequest

Requests returns a copy of every ProviderRequest received so far, in call order. Use it to assert what the Runner actually sent the model.

func (*StubProvider) Stream

func (s *StubProvider) Stream(ctx context.Context, req ProviderRequest) (Stream, error)

Stream implements Provider.

type StubTurn

type StubTurn struct {
	Deltas       []Delta
	Text         string
	ToolCalls    []ToolCall
	FinishReason string
	// Err, when set, fails the call instead of playing anything.
	Err error
}

StubTurn scripts one model call for StubProvider. Set Deltas to play a stream verbatim, or set Text/ToolCalls/FinishReason and the turn is composed into the canonical delta sequence (text, then tool-call-start+args per call, then finish).

type SubAgentEvent

type SubAgentEvent struct {
	// Scope is the slash-joined sub-agent path, e.g. "researcher" at the top
	// level or "researcher/summarizer" one level deeper.
	Scope string
	// Depth is the nesting depth: 1 for a top-level sub-agent, 2 for a
	// sub-agent it spawns, and so on.
	Depth int
	// Event is the child's event, unchanged.
	Event Event
}

SubAgentEvent is the envelope that carries a sub-agent's event to the parent surface. The scope and depth live on the envelope, NOT on Event, so Event stays flat and wire-serializable (constraint A2): a surface adds sub-agent framing without the core event vocabulary growing a nesting field.

type SummarizingCompactor

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

SummarizingCompactor collapses the head of an over-budget conversation into a single RoleSystem summary produced by a model, keeping a recent tail verbatim. It is the first, heuristic-budget compaction strategy; a token-accurate estimator and mid-turn compaction are follow-ups.

func NewSummarizingCompactor

func NewSummarizingCompactor(cfg SummarizingConfig) (*SummarizingCompactor, error)

NewSummarizingCompactor validates cfg and returns the compactor. Provider is required and MaxTokens must be positive.

func (*SummarizingCompactor) Compact

func (c *SummarizingCompactor) Compact(ctx context.Context, history []Message) ([]Message, error)

Compact implements Compactor: a no-op under budget, otherwise the head is summarized into one RoleSystem message prepended to the verbatim tail.

type SummarizingConfig

type SummarizingConfig struct {
	// Provider summarizes the head of the conversation. Required.
	Provider Provider

	// Estimator decides when history is over budget. Nil uses
	// CharTokenEstimator with the default ratio.
	Estimator TokenEstimator

	// MaxTokens is the budget: compaction fires only when the estimate
	// exceeds it. Required (must be > 0).
	MaxTokens int

	// KeepRecent is how many trailing messages stay verbatim. Zero uses
	// DefaultKeepRecent. The cut is nudged earlier if it would orphan a
	// tool-result message from its call, so the kept tail is always
	// self-contained.
	KeepRecent int

	// Instructions overrides the summarizer system prompt. Empty uses a
	// built-in prompt that asks for durable facts, decisions, and open
	// threads.
	Instructions string
}

SummarizingConfig configures a SummarizingCompactor.

type SummaryOptions

type SummaryOptions struct {
	// MaxItems caps how many notes are rendered, keeping the newest. Zero
	// means no item cap.
	MaxItems int
	// MaxChars caps the rendered length of the notes (a cheap token proxy;
	// the fixed header is not counted), dropping the oldest kept notes until
	// they fit. Zero means no length cap.
	MaxChars int
}

SummaryOptions budgets what Summary renders, so injecting the scratchpad every turn stays bounded even if the store grows. Both limits prioritize by recency (newest notes win — they are the most likely to matter). The zero value is unbounded: the whole scratchpad, which is affordable while working memory is kept small (WithMaxMemories). Relevance-based selection (inject only what matches the current turn) is the semantic-recall upgrade, a separate seam.

type Team

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

Team runs a set of named agents that transfer control to one another — handoff, the one composition mode that does NOT fit agent-as-tool (AgentSource). Where a sub-agent is called and returns an answer, a handoff transfers the whole conversation to a specialist and does not come back: the active agent runs, and if it calls a transfer_to_<name> tool, the Team swaps the active agent and continues over the SAME (carried-forward) history. The Runner is unaware it was swapped — the transfer is a tool it happened to call, intercepted above it.

A6: model-facing (agents + transfer tools), so it lives in agent/.

func NewTeam

func NewTeam(cfg TeamConfig) (*Team, error)

NewTeam validates cfg and builds each agent's Runner with its transfer tools merged in. It errors on a missing/duplicate member name, an unknown Start, or a HandoffTo naming a non-member.

func (*Team) Run

func (t *Team) Run(ctx context.Context, input string, emit func(Event)) (*TurnResult, error)

Run drives a single-shot conversation from the Start agent over one input, looping handoffs until an agent answers without transferring (the final result) or the cap is hit (ErrMaxHandoffs). It is the convenience wrapper over RunTurn for callers that do not thread history or persist the active agent.

func (*Team) RunTurn

func (t *Team) RunTurn(ctx context.Context, history []Message, active string, emit func(Event)) (*TurnResult, string, error)

RunTurn drives one user turn over the carried history: the active agent (or Start when active is "") runs, and each transfer swaps the active agent and continues over the SAME history, until an agent answers without transferring or the handoff cap is hit (ErrMaxHandoffs). All agents share the history (handoff transfers context); each brings its own instructions.

It returns the final result whose Messages hold EVERY message appended across all hops this turn (so a caller threads them into its own history as one turn), plus the agent active at the end. Persist that name and pass it back as active next turn so control stays where it was transferred, rather than re-starting from Start — the transfer-control (not re-route) semantic.

emit receives every agent's events; OnHandoff marks the transitions. An unknown active name is treated as Start (the caller's persisted value can lag a config change without erroring).

func (*Team) StartAgent

func (t *Team) StartAgent() string

StartAgent is the member that receives the first turn — the seam a host uses to initialize its persisted active agent before the first RunTurn.

func (*Team) ToolDefs

func (t *Team) ToolDefs(ctx context.Context) (map[string][]core.ToolDef, error)

ToolDefs exposes the merged tool set each member offers, for inspection (which agents can hand off where). Keyed by member name.

type TeamConfig

type TeamConfig struct {
	// Members are the agents. At least one; Start must name one of them.
	Members []TeamMember

	// Start is the agent that receives the first turn. Required.
	Start string

	// MaxHandoffs caps transfers before Run gives up with ErrMaxHandoffs.
	// Zero uses DefaultMaxHandoffs.
	MaxHandoffs int

	// OnHandoff, when set, is called on each transfer (from, to) — the seam a
	// surface renders "→ handed off to X" through.
	OnHandoff func(from, to string)

	// OnEvent, when set, receives every member's events tagged with the active
	// agent's name (SubAgentEvent{Scope: name, Depth: 1, Event}), so a surface
	// attributes team activity to the agent that produced it without inferring
	// from OnHandoff. It REPLACES the raw emit passed to Run/RunTurn for member
	// events (the tagged envelope carries the same Event); leave it nil to use
	// the untagged emit. Mirrors AgentSourceConfig.OnEvent.
	OnEvent func(SubAgentEvent)
}

TeamConfig assembles a Team.

type TeamMember

type TeamMember struct {
	// Name identifies the agent and forms its transfer tool (transfer_to_<Name>
	// on any teammate allowed to reach it). Required, unique within the Team.
	Name string

	// Config is the agent's RunnerConfig (provider, instructions, its own
	// tools). The Team merges the transfer tools in; the agent's own Tools are
	// preserved. Required.
	Config RunnerConfig

	// HandoffTo lists the names this agent may transfer to. Each becomes a
	// transfer_to_<name> tool offered only to this agent. Empty means a
	// terminal agent that must answer rather than hand off. Every name must be
	// another member.
	HandoffTo []string
}

TeamMember declares one agent in a Team and which other agents it may hand off to.

type TieredApproval

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

TieredApproval is the batteries-included ApprovalPolicy: a default mode, a map of per-tool rules that override it, an optional ask seam, and an optional session-scoped cache that remembers a tool the user approved so later calls to it skip the prompt. Safe for concurrent use by the Runner's parallel dispatch.

func NewTieredApproval

func NewTieredApproval(opts ...TieredOption) *TieredApproval

NewTieredApproval builds a TieredApproval. With no options it asks for every call and, lacking an AskFunc, refuses them all (fail-closed) — supply WithAsk to make asking meaningful.

func (*TieredApproval) Approve

Approve applies, in order: a remembered approval, a per-tool rule, then the default mode. An ask that the user accepts is remembered when the cache is on. A refusal (rule Deny, an ask returning false, or an ask with no AskFunc wired) yields Allowed:false with a Reason; the Runner feeds that back to the model and continues the turn.

func (*TieredApproval) DefaultMode

func (t *TieredApproval) DefaultMode() ApprovalMode

DefaultMode reports the current default mode.

func (*TieredApproval) SetDefaultMode

func (t *TieredApproval) SetDefaultMode(m ApprovalMode)

SetDefaultMode changes the default mode at runtime (the seam a host's "/approve <mode>" or "/yolo" command uses). Concurrency-safe against calls in flight; per-tool rules and the remember-cache are unaffected.

type TieredOption

type TieredOption func(*TieredApproval)

TieredOption configures a TieredApproval.

func WithAsk

func WithAsk(ask AskFunc) TieredOption

WithAsk supplies the seam that presents an approval prompt. Pass coord.Confirm to route asks through the shared ElicitationCoordinator.

func WithDefaultMode

func WithDefaultMode(m ApprovalMode) TieredOption

WithDefaultMode sets the disposition for calls no per-tool rule covers. Without it, the mode is ModeAlwaysAsk.

func WithRememberApprovals

func WithRememberApprovals(remember bool) TieredOption

WithRememberApprovals turns on the session cache: once the user approves a tool through an ask, subsequent calls to that same tool auto-allow for the life of this policy. A denial is never remembered.

func WithToolRule

func WithToolRule(tool string, rule ToolRule) TieredOption

WithToolRule pins a per-tool override that wins over the mode. Call it once per tool; a later rule for the same name replaces an earlier one.

type TokenEstimator

type TokenEstimator interface {
	Estimate(msgs []Message) int
}

TokenEstimator estimates the token cost of a message slice so a Compactor can decide when history exceeds a budget. Real tokenizers are provider-specific and heavy; the default is a cheap character heuristic and a real tokenizer is a drop-in behind this seam (a filed follow-up). An estimate must be monotonic: more or longer messages never estimate fewer tokens.

type ToolCall

type ToolCall struct {
	// ID is the provider-assigned call identifier, echoed on the RoleTool
	// result message.
	ID string `json:"id"`
	// Name is the tool name as listed to the model.
	Name string `json:"name"`
	// Args is the JSON arguments object. core.RawJSON so readers share
	// one parse (Bind for typed decode, Raw for display); wire shape is
	// identical to a raw message.
	Args core.RawJSON `json:"args"`
}

ToolCall is one model-requested tool invocation.

type ToolChoice

type ToolChoice struct {
	// Mode is "", "auto", "required", "none", or "function".
	Mode string `json:"mode,omitempty"`
	// Name is the forced tool for Mode == "function".
	Name string `json:"name,omitempty"`
}

ToolChoice is the request-level tool-calling bias. The zero value ("") means the provider default (auto). It marshals to the OpenAI-compatible wire form: the bare strings "auto"/"required"/"none", or the {type:function, function:{name}} object for a forced tool.

func ToolChoiceFunc

func ToolChoiceFunc(name string) ToolChoice

ToolChoiceFunc forces the model to call the named tool.

func (ToolChoice) IsZero

func (tc ToolChoice) IsZero() bool

IsZero reports whether no choice was set (provider default applies).

type ToolOwner

type ToolOwner struct {
	// SourceID is the identifier the source was added under.
	SourceID string
	// Def is that source's definition for the contested name.
	Def core.ToolDef
}

ToolOwner identifies one source's claim on a tool name during collision resolution.

type ToolResultStore

type ToolResultStore interface {
	// PutToolResult stores a result and returns nothing beyond error.
	// Refs are caller-assigned (OffloadingSource mints them) and
	// treated as opaque; storing the same ref twice overwrites, but
	// callers never reuse a ref, so that path is not relied upon.
	PutToolResult(ctx context.Context, req PutToolResultRequest) (PutToolResultResponse, error)

	// GetToolResult fetches a result by ref. Found=false means the ref
	// is unknown (never stored, or evicted) — the caller's cue to
	// degrade gracefully, not an error.
	GetToolResult(ctx context.Context, req GetToolResultRequest) (GetToolResultResponse, error)
}

ToolResultStore is the persistence seam for offloaded tool results: full tool outputs an OffloadingSource stored out of band, keyed by a ref, so the conversation carries only a compact stub and the model fetches the detail on demand (read_tool_result). It is the "just in time context" primitive — lossless, pay-on-lookup — complementary to compaction, which is lossy and pays unconditionally.

API shape follows the gRPC-style convention pinned in stores/STORAGE_SEAMS.md: Method(ctx, req) (resp, error), app-state on the response, error reserved for storage-layer faults. In particular an unknown ref is app-state (Found=false), never an error — a stub can legitimately outlive its blob once a backend evicts it, and read_tool_result turns Found=false into a graceful "no longer available" answer rather than a failure.

The interface and its in-memory default live in agent/ because they traffic in core.ToolResult and pair with the agent-only OffloadingSource (A6). Durable backends are sibling modules (agent/store/redis, agent/store/gorm), where retention is a per-backend construction concern (native TTL, a GC sweep, an LRU cap) — never part of this contract, which is exactly why the read path is built to tolerate eviction.

type ToolResultStoreOption

type ToolResultStoreOption func(*InMemoryToolResultStore)

ToolResultStoreOption configures an InMemoryToolResultStore.

func WithMaxToolResults

func WithMaxToolResults(n int) ToolResultStoreOption

WithMaxToolResults caps the store at n stored results, evicting the oldest when a Put would exceed n. Zero or negative means unbounded (the default). Eviction is safe because read_tool_result degrades an unknown ref to a "no longer available" answer rather than an error.

type ToolRule

type ToolRule int

ToolRule is the per-tool override that takes precedence over the mode.

const (
	// RuleAsk forces an ask for this tool regardless of mode.
	RuleAsk ToolRule = iota
	// RuleAllow auto-allows this tool regardless of mode.
	RuleAllow
	// RuleDeny refuses this tool outright, without asking.
	RuleDeny
)

type ToolSelector

type ToolSelector func(ctx context.Context, history []Message, tools []core.ToolDef) ([]core.ToolDef, error)

ToolSelector narrows the model-facing tool set for one step. Returning the input slice unchanged (or nil selector) offers everything; returning an empty slice offers no tools for that step. Names must be preserved verbatim: Call routing still resolves against the underlying ToolSource.

type ToolSource

type ToolSource interface {
	// Tools returns the definitions the model should see. The returned
	// slice is a snapshot; implementations decide their own caching and
	// refresh policy. Every returned name must be callable via Call, and
	// names within one source must be unique (aggregators treat a
	// repeated name from the same source as one claim, first definition
	// wins; only cross-source collisions get disambiguation).
	Tools(ctx context.Context) ([]core.ToolDef, error)

	// Call dispatches one tool invocation by name. A non-nil error means
	// the dispatch itself failed (unknown tool, transport failure,
	// unresolved ambiguity); a tool that ran and failed reports through
	// ToolResult.IsError instead, so the Runner can feed the failure back
	// to the model rather than aborting the turn.
	Call(ctx context.Context, name string, args map[string]any) (*core.ToolResult, error)
}

ToolSource is the Runner's view of callable tools, whatever their origin: a connected MCP server, a host-local function, or an aggregation of other sources. Implementations must be safe for concurrent use; the Runner may dispatch parallel tool calls against one source.

type TreeBudget

type TreeBudget struct {
	// MaxSteps caps total model calls across the tree. Zero means unbounded.
	MaxSteps int
	// MaxTokens caps total tokens (input + output, as reported by providers)
	// across the tree. Zero means unbounded. Enforced post-hoc — a turn can
	// overshoot by at most one step's output, since usage is known only after a
	// model call.
	MaxTokens int
}

TreeBudget caps the TOTAL model steps and/or tokens summed over a turn's whole agent tree — the parent Runner plus every sub-agent, fan-out member, and handoff round beneath it. It is the aggregate cost guard complementary to the per-source depth cap, the call-count budget (WithAgentCallBudget), and each Runner's own MaxSteps. A zero field means that dimension is unbounded.

type TriggerBinding

type TriggerBinding struct {
	// Server and Event select which incoming events match. Empty Server
	// matches any source.
	Server string
	Event  string

	// Filter further narrows matches. Nil matches all occurrences.
	Filter func(IncomingEvent) bool

	// Instructions seed the proactive turn (the trigger's "why you are
	// being invoked" prompt).
	Instructions string

	// Label names the binding in transcripts, logs, and slot state.
	Label string

	// Cooldown is this binding's re-arm floor. Zero means
	// DefaultTriggerCooldown.
	Cooldown time.Duration
}

TriggerBinding declares one event-to-turn binding.

type TriggerFiring

type TriggerFiring struct {
	Binding TriggerBinding
	Event   IncomingEvent
}

TriggerFiring is an approved proactive-turn request: the host runs it however it runs turns.

type TriggerPolicy

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

TriggerPolicy is the anti-nag mediation layer between events and proactive turns, the CIP playbook's Tier-1 slot machine ported: one slot per binding, OPEN to SPENT on fire, re-armed only when BOTH a user engagement arrived after the firing AND the binding's cooldown elapsed. This is a deterministic approximation of the original's LLM engagement signal (positive-engagement detection is a documented future upgrade). Safe for concurrent use.

func NewTriggerPolicy

func NewTriggerPolicy(cfg TriggerPolicyConfig) *TriggerPolicy

NewTriggerPolicy builds the policy.

func (*TriggerPolicy) Add

func (t *TriggerPolicy) Add(b TriggerBinding)

Add installs a binding at runtime — the seam a create_trigger meta-tool uses so the model can set up a standing behavior through conversation. A binding whose slotKey already exists replaces it and resets its slot (re-arming the behavior). Safe for concurrent use with OnEvent.

func (*TriggerPolicy) Bindings

func (t *TriggerPolicy) Bindings() []TriggerBinding

Bindings returns a snapshot of the installed bindings (for list_triggers surfaces).

func (*TriggerPolicy) Firings

func (t *TriggerPolicy) Firings() int

Firings reports how many proactive turns this policy has approved.

func (*TriggerPolicy) NotifyEngagement

func (t *TriggerPolicy) NotifyEngagement()

NotifyEngagement records that the user engaged (sent a message) after any pending firing: half of every spent slot's re-arm condition, the other half being its cooldown.

func (*TriggerPolicy) OnEvent

func (t *TriggerPolicy) OnEvent(ev IncomingEvent) *TriggerFiring

OnEvent evaluates ev against every binding and returns at most one approved firing (first matching OPEN binding wins), or nil when nothing fires. Suppressions (spent slot, budget, consent) are logged, not surfaced: servers get no backpressure channel by design.

func (*TriggerPolicy) Remove

func (t *TriggerPolicy) Remove(server, event, label string) bool

Remove deletes the binding with the given server/event/label (the slotKey components). Unknown bindings are a no-op. Returns whether one was removed.

type TriggerPolicyConfig

type TriggerPolicyConfig struct {
	Bindings []TriggerBinding

	// Budget caps total firings for this policy's lifetime (a session,
	// in agentchat's wiring). Zero means DefaultTriggerBudget.
	Budget int

	// Consent, when non-nil, approves each firing after the slot and
	// budget checks. The hook for "ask the user once per binding" UX.
	Consent func(TriggerBinding, IncomingEvent) bool

	// Logger records firings and suppressions (nil discards, per A4).
	Logger *slog.Logger
	// contains filtered or unexported fields
}

TriggerPolicyConfig assembles a TriggerPolicy.

type TurnRequest

type TurnRequest struct {
	// History is the conversation so far; RunTurn clones it and returns
	// only appended messages, exactly like Run.
	History []Message

	// Emit receives the turn's event stream. Nil is allowed; emit is
	// never called concurrently.
	Emit func(Event)

	// Control, when non-nil, is drained for the whole turn. A Control
	// cancels the targeted call's own context: the call fails fast
	// (ClientSource.Call threads ctx to the wire, so MCP servers see a
	// real cancellation), its result is fed back to the model as
	// "cancelled by user", and the turn continues — unlike cancelling
	// RunTurn's ctx, which aborts the whole turn. Send only while a
	// turn is running: between turns nothing drains the channel, and a
	// buffered cancel-all would hit the next turn's first dispatch.
	Control <-chan Control
}

TurnRequest consolidates RunTurn's inputs so the turn surface can grow without breaking signatures (the same C2 shape RunnerConfig uses).

type TurnResult

type TurnResult struct {
	Text         string    `json:"text,omitempty"`
	Messages     []Message `json:"messages"`
	Usage        Usage     `json:"usage"`
	Steps        int       `json:"steps"`
	FinishReason string    `json:"finishReason,omitempty"`

	// Structured is the schema-coerced final answer, present only when
	// RunnerConfig.ResponseSchema was set. It is the JSON document from the
	// finalizing Generate call; Bind it into a typed value. Its Usage is
	// already folded into Usage above. Empty when no schema was configured.
	Structured core.RawJSON `json:"structured,omitempty"`
}

TurnResult is the completed turn. Messages holds exactly the entries the turn appended (assistant messages and tool results, in order), so callers thread history as append(history, result.Messages...).

type Usage

type Usage struct {
	InputTokens  int `json:"inputTokens"`
	OutputTokens int `json:"outputTokens"`
}

Usage reports token consumption for one model call.

type WindowStrategy

type WindowStrategy string

WindowStrategy selects how a Window coalesces a burst.

const (
	WindowLastWins WindowStrategy = "last-wins"
	WindowMerge    WindowStrategy = "merge"
	WindowDebounce WindowStrategy = "debounce"
)

Window strategies, matching the context-hint vocabulary: last-wins keeps only the newest event per key, merge folds the burst through a combiner, debounce releases only after the key has been quiet for the window.

Directories

Path Synopsis
Package eval is a deterministic eval / scorer harness for agent turns.
Package eval is a deterministic eval / scorer harness for agent turns.
longmemeval
Package longmemeval provides fast, self-contained SMOKE scenarios for the Phase 2 memory work, shaped after the LongMemEval benchmark's skill categories.
Package longmemeval provides fast, self-contained SMOKE scenarios for the Phase 2 memory work, shaped after the LongMemEval benchmark's skill categories.
host module
store
gorm module
redis module
surfaces module
web module

Jump to

Keyboard shortcuts

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