speculation

package
v0.11.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package speculation provides idle-time predictive execution. After the agent completes a turn, a speculative fork runs with read-only tools to predict the next action. When the user accepts, the prediction is applied; when the user types, it's discarded.

Architecture ported from Claude Code's speculation.ts.

Index

Constants

This section is empty.

Variables

View Source
var ReadOnlyTools = map[string]bool{
	"read": true, "read_file": true, "file_read": true, "FileRead": true,
	"grep": true, "search": true, "file_search": true, "Grep": true,
	"glob": true, "file_glob": true, "Glob": true,
	"lsp": true, "LSP": true,
	"list": true, "ls": true,
}

ReadOnlyTools is the set of tool names safe for speculative execution.

Functions

This section is empty.

Types

type Engine

type Engine struct {

	// Callbacks set by the agent wiring.
	// OnSpeculate is called to start a speculative fork.
	// The callback receives a context that cancels on discard.
	OnSpeculate func(ctx context.Context) (*Result, error)

	// OnStateChange fires when state transitions.
	OnStateChange func(old, new State)

	// MaxTurns limits how long speculation runs.
	MaxTurns int
	// MaxDuration caps wall-clock time.
	MaxDuration time.Duration
	// contains filtered or unexported fields
}

Engine manages the speculation lifecycle.

func NewEngine

func NewEngine() *Engine

NewEngine creates a speculation engine with defaults.

func (*Engine) Accept

func (e *Engine) Accept() *Result

Accept applies the predicted result. No-op if not ready.

func (*Engine) Discard

func (e *Engine) Discard()

Discard cancels any running speculation. Called when user starts typing.

func (*Engine) Reset

func (e *Engine) Reset()

Reset transitions the engine back to Idle from any terminal state (Accepted, Discarded). Allows the engine to be reused across multiple speculation cycles without creating a new Engine instance. Cancels any running speculation goroutine. Bumps the generation counter to invalidate any in-flight speculation results (bug #39 fix).

func (*Engine) Result

func (e *Engine) Result() *Result

Result returns the completed result, if ready.

func (*Engine) Start

func (e *Engine) Start()

Start begins a speculation cycle. No-op if already running. Called after the agent completes a turn with no pending user input.

func (*Engine) State

func (e *Engine) State() State

State returns the current speculation state.

type Result

type Result struct {
	Summary     string    `json:"summary"`    // human-readable prediction
	ToolCalls   []string  `json:"tool_calls"` // predicted tool calls
	StartedAt   time.Time `json:"started_at"`
	CompletedAt time.Time `json:"completed_at"`
}

Result holds the completed speculation output.

type State

type State int

State tracks a single speculation lifecycle.

const (
	StateIdle      State = iota // no speculation running
	StateRunning                // forked agent is thinking
	StateReady                  // prediction complete, waiting for user
	StateAccepted               // user accepted, applying results
	StateDiscarded              // user typed or timed out
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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