engine

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 46 Imported by: 0 modules, 0 packages

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

Documentation

Overview

Adapted from Goose ee8500d5 and 14741e9a (Apache-2.0). See ../model/LICENSE.goose.

Package engine owns ordered turn execution and its committed effects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountedTool

func CountedTool(tools []Tool, name string) (telemetry.Tool, bool)

CountedTool is how a call of one of a turn's registered tools is counted. A name the turn did not register is not counted. A tool without a package binding was registered by the Node itself and is a built-in under its registered name, unless telemetry.UncountedBuiltins leaves it out. A package's tool is named only when its owner is a package the application ships (operations.Official); a connected server's tool and any other package's tool are counted without a name.

func ForegroundToolContext added in v0.2.7

func ForegroundToolContext(ctx, operation context.Context) context.Context

ForegroundToolContext lets a command observe steering only during foreground execution. Its preparation and durable receipts use the operation owner.

func MCPToolName

func MCPToolName(server, tool string) string

MCPToolName is the name an MCP tool is offered under when it is unique.

func Steered added in v0.2.7

func Steered(ctx context.Context) bool

Types

type Options

type Options struct {
	Python             *python.Manager
	ValidateWorktree   func(context.Context, protocol.WorktreeID) error
	Attachment         func(context.Context, protocol.TurnID, protocol.AttachmentID) (protocol.Attachment, bool, error)
	AttachmentBytes    func(context.Context, protocol.Attachment) ([]byte, error)
	Clock              clock.Clock
	Model              func(context.Context, model.Binding) (fantasy.LanguageModel, error)
	AuthorizeProvider  func(context.Context, *store.AgentInvocation) error
	Tools              []Tool
	ToolsForTurn       func(context.Context, *store.AgentInvocation) ([]Tool, error)
	HooksForTurn       func(context.Context, *store.AgentInvocation) *hooks.Binding
	PluginAvailability func(protocol.PluginReference) error
	// AfterTurn, when set, runs after a main task turn's end is stored. It
	// receives the settled run and what the execution observed.
	AfterTurn func(context.Context, *store.AgentInvocation, protocol.ConversationRun, TurnObservation)
	Telemetry *telemetry.Control
	// Working, when set, brackets every running turn.
	Working func() func()
	// StopCommands, when set, stops a session's background commands. The
	// engine calls it for a child session whose run has ended.
	StopCommands func(protocol.SessionID)
	// StopTransient, when set, stops a session's non-service background
	// commands. The engine calls it once a durable stop of the session's
	// running turn commits: a user stop, Send now or a disabled package.
	StopTransient func(protocol.SessionID)
}

Options exposes the actual model and clock boundaries to deterministic tests.

type Runtime

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

func New

func New(db *store.Handle, node protocol.NodeID, profile string, frames *feed.Hub, publish func(), options Options) *Runtime

func (*Runtime) BeginOperation

func (r *Runtime) BeginOperation(turn protocol.TurnID) (context.Context, func(), bool)

BeginOperation and drainOperations share the execution lock, sealing admission before terminal history is committed and waiting for already admitted effects.

func (*Runtime) BeginStop added in v0.2.7

func (r *Runtime) BeginStop()

BeginStop interrupts turns before the Node cancels their dependencies. Stop joins their settlement after the command lanes have released their locks.

func (*Runtime) CommandPython

func (r *Runtime) CommandPython(ctx context.Context, turn protocol.TurnID) (*python.Execution, error)

func (*Runtime) Committed

func (r *Runtime) Committed()

Committed wakes owners only after a controller transaction has committed. The durable queue remains the sole source of admitted work.

func (*Runtime) Elicit

func (r *Runtime) Elicit(ctx context.Context, turn protocol.TurnID, parent canonical.ToolRequest, request *sdk.ElicitRequest) (*sdk.ElicitResult, error)

func (*Runtime) GuidanceSignal

func (r *Runtime) GuidanceSignal(turn protocol.TurnID) <-chan struct{}

func (*Runtime) LockDecision

func (r *Runtime) LockDecision(command protocol.Command) func()

LockDecision claims the owner before a decision transaction can wait for COMMIT. Expiry uses the same owner, so it cannot overtake a claimed answer.

func (*Runtime) LockPublication

func (r *Runtime) LockPublication() func()

LockPublication orders commits, their frames and pinned controller snapshots. Acquire decision ownership first; acquire storage leases only after this lock.

func (*Runtime) OperationContext

func (r *Runtime) OperationContext(turn protocol.TurnID) (context.Context, bool)

OperationContext borrows a running turn's cancellation owner. The caller must finish its admitted effect before returning to the loop or closing the Node.

func (*Runtime) QueueCommitted added in v0.2.7

func (r *Runtime) QueueCommitted(update protocol.ConversationQueueUpdate, selection *protocol.CommandGuideQueuedTurnPayload)

QueueCommitted reconciles steering against the durable queue under publication.

func (*Runtime) RecordMediaUsage

func (r *Runtime) RecordMediaUsage(ctx context.Context, session protocol.SessionID, turn protocol.TurnID, provider protocol.ConversationProvider, name string, kind protocol.MediaKind, usage canonical.Usage) error

RecordMediaUsage records one media call under the provider and model that actually ran it, after cancellation as well.

func (*Runtime) RecordSearchUsage

func (r *Runtime) RecordSearchUsage(ctx context.Context, session protocol.SessionID, turn protocol.TurnID, provider protocol.ConversationProvider, name string, usage canonical.Usage) error

RecordSearchUsage completes the billed response append after cancellation.

func (*Runtime) Recover

func (r *Runtime) Recover(ctx context.Context) error

Recover closes interrupted owners and holds every surviving queue. It never replays a model request or an external tool operation.

func (*Runtime) Refresh

func (r *Runtime) Refresh(session protocol.SessionID)

func (*Runtime) RefreshCommitted

func (r *Runtime) RefreshCommitted(session protocol.SessionID, changedChildren ...protocol.TurnID)

RefreshCommitted publishes a pinned metadata read while its caller owns publication, so no newer interaction or queue can overtake the read.

func (*Runtime) RefreshStatistics

func (r *Runtime) RefreshStatistics(session protocol.SessionID)

RefreshStatistics coalesces committed changes under the Node lifetime. A read made dirty by a later change cannot replace that change's controller state.

func (*Runtime) RefreshStatisticsCommitted

func (r *Runtime) RefreshStatisticsCommitted(session protocol.SessionID)

RefreshStatisticsCommitted follows this commit's frames under publication.

func (*Runtime) RequestStatisticsCommitted

func (r *Runtime) RequestStatisticsCommitted(session protocol.SessionID)

RequestStatisticsCommitted fences the new reservation's snapshot. An older flight may predate writes made after the last observer closed, when metadata and policy changes need not have scheduled statistics.

func (*Runtime) Start

func (r *Runtime) Start()

func (*Runtime) Stop

func (r *Runtime) Stop()

func (*Runtime) StopCommitted added in v0.2.3

func (r *Runtime) StopCommitted(run protocol.ConversationRun)

StopCommitted delivers a durable Stop before projecting the changed history. The caller holds the publication lock, so the turn cannot finish or be replaced between this decision and cancellation of its owned work.

func (*Runtime) Wake

func (r *Runtime) Wake()

type Tool

type Tool struct {
	Definition fantasy.FunctionTool
	SchemaJSON json.RawMessage
	ReadOnly   bool
	Concurrent bool
	Deferred   bool
	Delegation bool
	Approval   bool
	// Confirm reports whether one prepared call of a tool without Approval
	// asks first: a read-only tool with an optional project write.
	Confirm func(canonical.ToolRequest) bool
	Binding *store.ApprovalBinding
	Execute func(context.Context, canonical.ToolRequest) ToolResult
	Prepare func(context.Context, canonical.ToolRequest) error
	// ReleasePrepared drops transient preparation even when the call never
	// reaches Execute. It is safe to call after Execute has consumed it.
	ReleasePrepared   func(canonical.ToolRequest)
	ObserveHistory    func([]canonical.Record)
	ConfigureApproval func(context.Context, *sql.Conn, canonical.ToolRequest, protocol.ConversationApproval) (protocol.ConversationApproval, error)
	Approved          func(canonical.ToolRequest, protocol.ApprovalID, protocol.RequestID)
	Close             func() error
	Instruction       string
	TurnInstruction   prompt.Part
	ContextParts      []string
	BeforeModel       func(context.Context, []canonical.Record, ToolResultReader) (bool, error)
	BindDelegation    func(Tool) Tool
	// Guidance makes BindDelegation receive guide_agent instead of spawn_agent.
	Guidance bool
	// Flow lists the operations a package flow may run.
	Flow []protocol.ToolOperation
	// Exposed is the name an MCP tool is offered under when no other tool
	// owns it. A package tool is offered under its declared name.
	Exposed      string
	MCPAuthority mcp.AuthorityKey
	// SearchLabels are the captured display labels and translations used only
	// for discovery. They do not enlarge the model's tool declarations.
	SearchLabels []string

	// ExecutePrepared runs a call with arguments a package prepared. The call
	// keeps the identity the model sent; the arguments are what executes.
	ExecutePrepared func(context.Context, canonical.ToolRequest, json.RawMessage) ToolResult
	// contains filtered or unexported fields
}

Tool is one captured declaration and executor. Concurrency and authority are properties of the captured registration, never arguments supplied by a model.

type ToolResult

type ToolResult struct {
	Collect         *collectedChild
	Meta            *map[string]json.RawMessage
	ReturnedFailure *protocol.ToolFailure
	ReturnedError   bool
	Value           any
	Format          canonical.ToolFormat
	Error           bool
	Failure         *protocol.Fault
	Context         *canonical.Context
	// Attachments are stored assets shown with the result and never sent to
	// the model.
	Attachments []canonical.Attachment
}

type ToolResultReader added in v0.2.4

type ToolResultReader func(*canonical.ToolResponse) (json.RawMessage, error)

ToolResultReader materializes a selected result for a package hook. The canonical history passed to the hook retains its stored asset references.

type TurnObservation

type TurnObservation struct {
	Tools   map[string]int
	Expired int
}

TurnObservation is what one execution saw that storage does not keep apart: the tool calls the model made, by the name it called, and the approvals and questions that expired unanswered.

Jump to

Keyboard shortcuts

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