executor

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 20 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

Package executor abstracts "run a command / move a file on a machine". Everything above this layer is target-kind agnostic.

Index

Constants

View Source
const (
	MockPollEnd           = "ADK-POLL-END-v2"
	MockPaneDelimiter     = "\x1e---LECTERN-PANE---\x1e"
	MockDiscoverDelimiter = "\x1e---LECTERN-PS---\x1e"
	// MockAgentProbeMarker must match sessions.AgentProbeMarker.
	MockAgentProbeMarker = ": LECTERN-AGENT-PROBE;"
)

The mock's half of the interactive-session protocol.

These two literals must match `sessions.PollDelimiter` and `sessions.DiscoverDelimiter`. They are duplicated rather than imported because the sessions package depends on this one; TestMockDelimitersMatchSessions asserts they have not drifted.

View Source
const MockDiff = `` /* 205-byte string literal not displayed */

MockDiff is the patch every mock attempt "produces".

View Source
const MockNumstat = "4\t1\tapp.py\n"

MockNumstat is the matching --numstat output.

Variables

View Source
var ErrNoDial = errors.New("this kind of target cannot forward ports")

ErrNoDial is returned by targets whose loopback cannot be reached this way.

Functions

func Probe

func Probe(ctx context.Context, ex Executor) (map[string]any, error)

Probe is the capability check every target kind shares: versions plus disk.

func ShellQuote

func ShellQuote(s string) string

ShellQuote renders a value as one shell word.

func Wrap

func Wrap(vmid, cmd, cwd string) string

Wrap renders the `sudo pct exec` invocation for a command. Exported because the quoting round-trip is worth testing directly.

Types

type Dialer

type Dialer interface {
	DialTarget(ctx context.Context, addr string) (net.Conn, error)
}

Dialer is an executor that can open a TCP connection as the target sees it, so "127.0.0.1" means the target's loopback and not the control plane's. It is what lets the control plane carry a connection to a service that only listens on the target's own localhost.

type Error

type Error struct{ Msg string }

Error marks a target that could not be reached at all, as opposed to a command that ran and failed.

func Errf

func Errf(format string, args ...any) *Error

Errf builds an executor Error.

func (*Error) Error

func (e *Error) Error() string

type Executor

type Executor interface {
	// Run executes a shell command, optionally in cwd, bounded by timeout.
	Run(ctx context.Context, cmd string, opts RunOpts) (Result, error)
	// ReadFile returns bytes from offset to EOF; empty (not an error) if missing.
	ReadFile(ctx context.Context, path string, offset int64) ([]byte, error)
	// WriteFile writes data, creating parent directories.
	WriteFile(ctx context.Context, path string, data []byte) error
	// Close releases any pooled connection.
	Close() error
}

Executor is one machine lectern can drive. One instance per target.

type Local

type Local struct{}

Local runs commands on the control-plane host itself.

func NewLocal

func NewLocal() *Local

NewLocal builds a Local executor.

func (*Local) Close

func (l *Local) Close() error

Close is a no-op: nothing is pooled.

func (*Local) DialTarget

func (l *Local) DialTarget(ctx context.Context, addr string) (net.Conn, error)

DialTarget on the control-plane host is an ordinary dial.

func (*Local) ReadFile

func (l *Local) ReadFile(_ context.Context, path string, offset int64) ([]byte, error)

ReadFile reads from offset to EOF, treating a missing file as empty — the scheduler polls for an agent log that does not exist yet on every tick.

func (*Local) Run

func (l *Local) Run(ctx context.Context, cmd string, opts RunOpts) (Result, error)

Run shells out on this host.

func (*Local) WriteFile

func (l *Local) WriteFile(_ context.Context, path string, data []byte) error

WriteFile writes data, creating parent directories.

type Mock

type Mock struct {

	// Delay paces the fake agent between events.
	Delay time.Duration
	// HTTP is the client the fake agent uses for hook callbacks. Tests point it
	// at their httptest server's transport; production demo mode uses the default.
	HTTP *http.Client
	// Intercept, when set, runs synchronously at the top of every Run call.
	// It exists purely as a test seam for controlling timing — e.g. blocking
	// on a specific command to deterministically create the overlap a
	// coalescing test needs — and is never set in production code.
	Intercept func(cmd string)
	// contains filtered or unexported fields
}

Mock is a scripted fake target. It powers the whole hermetic test suite and LECTERN_MOCK=1 demo mode, and emits REAL claude stream-json shapes so the parser is exercised end to end rather than stubbed out.

Scenario markers in the prompt:

[mock:approval]        agent requests a hook approval mid-run (real HTTP)
[mock:fail]            agent exits non-zero
[mock:limit]           agent is stopped by its usage limit (Claude's real rejected rate_limit_event + limit result)
[mock:slow]            agent takes ~3x longer
[mock:subtask]         agent files a follow-up card through the task hook
[mock:note]            agent leaves a project note
[mock:approve-verdict] agent ends with VERDICT: APPROVE
[mock:reject-verdict]  agent ends with VERDICT: REQUEST_CHANGES
[mock:judge:N]         agent ends with JUDGE: attempt N (for judge-task tests)
[mock:eval-judge:match|no-match]  agent ends with EVAL_JUDGE: MATCH|NO_MATCH (replay eval judge tests)

func NewMock

func NewMock(delay time.Duration) *Mock

NewMock builds a mock executor.

func (*Mock) Close

func (m *Mock) Close() error

Close cancels every fake agent still running.

func (*Mock) CmdLog

func (m *Mock) CmdLog() []string

func (*Mock) ExitPaneAgent added in v2.4.0

func (m *Mock) ExitPaneAgent(sess string)

ExitPaneAgent makes a scripted pane's agent quit, leaving a shell prompt.

func (*Mock) Files

func (m *Mock) Files() map[string][]byte

Files returns a copy of the fake filesystem, for assertions about what was actually staged onto the target.

func (*Mock) PaneText added in v2.4.0

func (m *Mock) PaneText(sess string) string

PaneText is the current text of a scripted pane.

func (*Mock) Probe

func (m *Mock) Probe() map[string]any

Probe answers the capability check without shelling out.

func (*Mock) ReadFile

func (m *Mock) ReadFile(_ context.Context, path string, offset int64) ([]byte, error)

ReadFile reads from the fake filesystem.

func (*Mock) Run

func (m *Mock) Run(ctx context.Context, cmd string, opts RunOpts) (Result, error)

Run interprets the command against the scripted target.

func (*Mock) SetPaneLimit added in v2.4.0

func (m *Mock) SetPaneLimit(sess, message string) bool

SetPaneLimit scripts a usage limit for an interactive session: the message is printed now, and every message sent while it is set is answered with it again. An empty message lifts the limit, so the next message is worked on.

func (*Mock) WriteFile

func (m *Mock) WriteFile(_ context.Context, path string, data []byte) error

WriteFile writes to the fake filesystem.

type Pct

type Pct struct {
	VMID string
	// contains filtered or unexported fields
}

Pct drives an LXC on this Proxmox node through `pct exec` — no SSH and no per-container keys. The control-plane user needs passwordless sudo for pct. Target kind 'pct', with host holding the vmid.

func NewPct

func NewPct(vmid string) *Pct

NewPct builds a pct executor for a container id.

func (*Pct) Close

func (p *Pct) Close() error

Close is a no-op: pct exec pools nothing.

func (*Pct) ReadFile

func (p *Pct) ReadFile(ctx context.Context, filePath string, offset int64) ([]byte, error)

ReadFile tails a file inside the container from a byte offset.

func (*Pct) Run

func (p *Pct) Run(ctx context.Context, cmd string, opts RunOpts) (Result, error)

Run executes inside the container.

func (*Pct) WriteFile

func (p *Pct) WriteFile(ctx context.Context, filePath string, data []byte) error

WriteFile streams bytes into the container without shell argument limits.

type Registry

type Registry struct {

	// Mock forces every target onto the scripted MockExecutor.
	Mock      bool
	MockDelay time.Duration
	// MockHTTP is the client fake agents use for hook callbacks. Tests point it
	// at their own server.
	MockHTTP *http.Client
	// contains filtered or unexported fields
}

Registry hands out one cached executor per target id.

func NewRegistry

func NewRegistry(mock bool, mockDelay time.Duration) *Registry

NewRegistry builds an executor registry.

func (*Registry) Any

func (r *Registry) Any() Executor

Any returns one cached executor. Tests use it to inspect what reached "the target" without knowing which target id won the race.

func (*Registry) For

func (r *Registry) For(t *store.Target) (Executor, error)

For returns the executor for a target, creating and caching it on first use.

func (*Registry) Reset

func (r *Registry) Reset()

Reset drops the cache — called whenever a target's connection details change.

type Result

type Result struct {
	RC     int
	Stdout string
	Stderr string
}

Result is the outcome of one command.

func (Result) OK

func (r Result) OK() bool

OK reports a zero exit status.

type RunOpts

type RunOpts struct {
	Cwd     string
	Timeout float64 // seconds; 0 means 120
}

RunOpts carries the optional arguments of Run.

type SSH

type SSH struct {
	Host    string
	User    string
	Port    int
	KeyPath string
	// Wrapper, when set, transforms every command before it is sent. It is how a
	// host whose SSH lands somewhere other than the work becomes an ordinary
	// target. Two placeholders:
	//
	//	{cmd}  the command, POSIX shell-quoted
	//	{b64}  the command, base64-encoded
	//
	// {b64} is the one to reach for on a Windows host: its SSH server hands the
	// line to cmd.exe, which does not understand POSIX quoting, and anything
	// that survives that still gets $-expanded by the outer bash. Base64 is
	// alphanumeric, so it passes through both untouched:
	//
	//	wsl -e bash -lc "echo {b64} | base64 -d | bash"
	//
	// A wrapper with no placeholder is treated as a prefix and given the
	// shell-quoted command, which is what a POSIX host wants.
	Wrapper string
	// contains filtered or unexported fields
}

SSH is a remote target reached over SSH. Key auth only, one reused connection with bounded setup and command execution.

func NewSSH

func NewSSH(host, user string, port int, keyPath, wrapper string) *SSH

NewSSH builds an SSH executor for a target.

func (*SSH) Close

func (s *SSH) Close() error

Close drops the pooled connection.

func (*SSH) DialTarget

func (s *SSH) DialTarget(ctx context.Context, addr string) (net.Conn, error)

DialTarget rides the SSH connection the executor already holds, which is the same thing `ssh -L` does, without a second process to supervise.

A wrapped target runs its commands somewhere else again — inside a container on the SSH host, say — so the SSH host's loopback is not the loopback the session sees, and forwarding to it would reach the wrong machine's services.

func (*SSH) ReadFile

func (s *SSH) ReadFile(ctx context.Context, path string, offset int64) ([]byte, error)

ReadFile tails from a byte offset.

`tail -c +N` is 1-indexed and streams in big blocks. The obvious `dd bs=1` spelling costs one syscall per byte, which is pathological when a growing agent log is re-read on every poll.

func (*SSH) Run

func (s *SSH) Run(ctx context.Context, cmd string, opts RunOpts) (Result, error)

Run executes a command over a fresh SSH channel on the pooled connection.

func (*SSH) WriteFile

func (s *SSH) WriteFile(ctx context.Context, path string, data []byte) error

WriteFile streams file bytes on native SSH targets. Wrapped targets may consume stdin themselves (e.g. Windows -> WSL), so use bounded commands there.

Jump to

Keyboard shortcuts

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