agentrunapi

package
v0.0.74 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package agentrunapi provides in-process Classify + AutoSendOrResume for agent-run session lifecycle (run | send | resume) without shelling out to the agent-run binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoSendOrResume

func AutoSendOrResume(ctx context.Context, opts Opts) error

AutoSendOrResume validates, classifies, and dispatches run|send|resume.

func BuildFollowUpCommand

func BuildFollowUpCommand(opts FollowUpOpts) (string, error)

BuildFollowUpCommand returns a single shell-quoted command line suitable for iterm2 FollowUpCommands. Never includes --new-terminal. Open/Detach mutual exclusion: if both true, return error. Empty SessionID → error.

func IsSessionReadyFromStatus

func IsSessionReadyFromStatus(stdout string) bool

IsSessionReadyFromStatus reports banner + sendable yes (parity with agentrunbridge.IsSessionReady).

func OpenInNewTerminal

func OpenInNewTerminal(opts OpenInNewTerminalOpts) error

OpenInNewTerminal invokes OpenTerminal(dir, followUp). When OpenTerminal is nil, production uses iterm2 ModeForceNew.

func ParseTTYStatus

func ParseTTYStatus(stdout string) (screen, sendable string)

ParseTTYStatus extracts screen status and sendable token from human `agent-run tty status` stdout (parity with agentrunbridge.ParseTTYStatus).

Lines:

  • "screen status: <value>" → screen (trimmed value after colon)
  • "sendable: <token> ..." → first whitespace-separated token of the value

func WaitReady

func WaitReady(opts WaitReadyOpts) error

WaitReady polls StatusFn until IsSessionReadyFromStatus or timeout. Timeout error mentions ready and/or timeout. No agent-run binary LookPath.

Types

type FollowUpOpts

type FollowUpOpts struct {
	// Driver is the host re-exec config. Zero Binary → Resolve defaults to self
	// unless legacy DriverBinary/DriverArgsPrefix are set (compat).
	Driver agentdriver.Driver
	// DriverBinary is deprecated: use Driver.Binary.
	// Empty with empty Driver → historical default "agent-run" for bare CLI PATH.
	DriverBinary string
	// DriverArgsPrefix is deprecated: use Driver.Args.
	DriverArgsPrefix []string

	SessionID                     string
	Prompt                        string
	AgentRunner                   string
	WorkspaceDir                  string
	NoSubmit                      bool
	AllowRelocateResumeSessionDir bool
	Open                          bool
	Detach                        bool
	// Env each "KEY=VALUE" → -e KEY=VALUE before -- / prompt (optional).
	Env []string
}

FollowUpOpts builds a shell-quoted ForceNew child command (no --new-terminal).

type Mode

type Mode string

Mode is the auto-send-or-resume branch: run | send | resume.

const (
	ModeRun    Mode = "run"
	ModeSend   Mode = "send"
	ModeResume Mode = "resume"
)

func Classify

func Classify(store agentstorage.Store, sessionID string, probe ProbeFunc) (mode Mode, meta agentstorage.SessionMeta, found bool, err error)

Classify resolves session id and returns Mode using the same rules as cmd/agent-run runAutoSendOrResume. Missing session → ModeRun, found=false.

type OpenInNewTerminalOpts

type OpenInNewTerminalOpts struct {
	WorkspaceDir string
	// FollowUp if non-empty is used as-is; else built from FollowUpOpts.
	FollowUp     string
	FollowUpOpts FollowUpOpts
	// OpenTerminal replaces production iTerm ForceNew when set (unit tests).
	OpenTerminal func(dir string, followUp string) error
}

OpenInNewTerminalOpts opens a new terminal with a follow-up command.

type Opts

type Opts struct {
	SessionID                     string
	Prompt                        string
	WorkspaceDir                  string
	AgentRunner                   string
	AgentRunnerBinary             string
	RunnerConfigHome              string
	Model                         string
	Open                          bool
	Detach                        bool
	NoSubmit                      bool
	KeepTTY                       bool
	JSON                          bool
	AllowRelocateResumeSessionDir bool
	// Driver is the host re-exec config for TTY __serve_* and should match
	// ForceNew FollowUp Driver (see pkgs/agentdriver). Zero → DefaultSelf in serve.
	Driver agentdriver.Driver
	// NewTerminal: P1 unit leaves keep false. When true, ForceNew may remain CLI-owned.
	NewTerminal  bool
	Env          []string
	PrependPaths []string
	Store        agentstorage.Store
	Stdout       io.Writer
	Stderr       io.Writer
	Probe        ProbeFunc

	// Optional dispatch overrides (nil = production path).
	RunSession    func(ctx context.Context, opts Opts, meta agentstorage.SessionMeta, found bool) error
	SendLive      func(ctx context.Context, opts Opts, meta agentstorage.SessionMeta) error
	ResumeSession func(ctx context.Context, opts Opts, meta agentstorage.SessionMeta) error
}

Opts drives AutoSendOrResume. NewTerminal=false is the in-process P1 path. Dispatch hooks, when set, replace production send/run/resume for unit tests and prove no agent-run binary LookPath is required.

type ProbeFunc

type ProbeFunc func(store agentstorage.Store, meta agentstorage.SessionMeta) (ProbeReport, error)

ProbeFunc injects lifecycle probing. nil → LifecycleProbe (production TTY/registry/process probe). Use EmptyProbe in unit tests that need unknown lifecycle without TTY I/O.

type ProbeReport

type ProbeReport struct {
	// ResumeReady is true when runner is bound and exited (resume path).
	ResumeReady bool
	// RunnerExited is nil when unknown; false when live; true when exited.
	RunnerExited *bool
}

ProbeReport is the lifecycle subset Classify needs (from probeSessionStatus parity).

func EmptyProbe added in v0.0.74

func EmptyProbe(store agentstorage.Store, meta agentstorage.SessionMeta) (ProbeReport, error)

EmptyProbe always returns unknown lifecycle (ResumeReady false, RunnerExited nil). Use in unit tests that need ModeRun for a found session without TTY side effects.

func LifecycleProbe added in v0.0.74

func LifecycleProbe(store agentstorage.Store, meta agentstorage.SessionMeta) (ProbeReport, error)

LifecycleProbe is the production TTY/registry lifecycle probe for Classify. It mirrors agent-run CLI probeSessionStatus for ResumeReady and RunnerExited.

When Opts.Probe / Classify probe is nil, this function is used (not EmptyProbe).

type WaitReadyOpts

type WaitReadyOpts struct {
	SessionID    string                            // required (non-empty after trim)
	StatusFn     func() (stdout string, err error) // required
	Timeout      time.Duration                     // 0 → 60s
	PollInterval time.Duration                     // 0 → 500ms
}

WaitReadyOpts polls an injectable status source until the session is ready.

Jump to

Keyboard shortcuts

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