executor

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: AGPL-3.0 Imports: 9 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 runs a provider CLI as a child process and turns its stream into canonical events (ADR-0006). An Adapter knows only how to launch one CLI and how to translate its stream; this common Executor owns the child lifecycle (start, timeout, SIGINT forwarding), assigns ts, and observes exit code and wall-clock duration. Nothing here touches the DB, connections, or seq numbering — those live behind the Sink (SCHEMA.md).

Index

Constants

View Source
const (
	EventProviderSelected = "provider.selected"
	EventProviderOutput   = "provider.output"
	EventProviderFinished = "provider.finished"
	EventProviderError    = "provider.error"
	// EventPermissionRequired says the run stopped because a tool needed a
	// person's permission (ADR-0053 Decision 2). Measured 2026-07-27: this
	// ends the turn — there is no in-stream way to answer — so it is a
	// report, not a prompt. Whoever is driving decides what to do with it.
	//
	// View-only: it never reaches the ledger (ADR-0053 Decision 4 — 許可は
	// 配線であって経験ではない). Payload carries `tool` and a human-readable
	// `detail` of what was asked for.
	EventPermissionRequired = "permission.required"
)

Canonical event types an Adapter may emit (SCHEMA.md R4). The vocabulary lives here, not in the adapters, so the Executor can recognise the two events it treats specially: provider.finished, whose exit_code only the Executor observes (see Run), and provider.error, whose presence the Executor reports back so a caller doesn't record the same failure twice.

View Source
const PayloadDetail = "detail"

PayloadDetail is a view-only payload key (ADR-0024 Decision 6): a short, human-readable summary of a tool_use — the file it touched, the command it ran — that an Adapter may attach so a turn view can show "what, where", not just the tool name. It never reaches the ledger: the recorded event keeps the tool name only (SCHEMA.md R3), because a summary in the digest would pressure maxEventChars/maxSessionChars and force an extractor_ver bump.

View Source
const PayloadToolResult = "tool_result"

PayloadToolResult is a view-only payload key (ADR-0030): a tool's own output — a Bash colour demo, a diff, a test result — that a turn view shows so the human can judge an answer that IS terminal output, not just its assistant text. Like PayloadDetail it never reaches the ledger: SCHEMA.md R3 keeps the tool name only, and recording tool output would spend the perception digest budget (maxEventChars/maxSessionChars) on what R3 deliberately drops. It carries the provider's raw ANSI; the view sanitises it (mdlite.ToolOutput).

Variables

This section is empty.

Functions

func StripViewOnly

func StripViewOnly(payload map[string]any) map[string]any

StripViewOnly returns payload without its view-only keys (PayloadDetail), the form a Sink records. It never mutates the input and returns a copy only when there is something to strip: a chat's sink shows the event first and records it after, so the shown map must keep its detail regardless of call order, and events without a detail (the common case) allocate nothing.

func TruncateDetail

func TruncateDetail(s string, keepTail bool) string

TruncateDetail trims a candidate view-only summary and caps it at 60 runes (not bytes, so a Japanese path is measured by characters), ending the cut side with an ellipsis. A path keeps its tail — the filename answers "where", and a deep absolute path cut from the right shows only the prefix every path shares (measured on a real turn: the view showed ~/.claude-personal/projects/… with the filename gone). Everything else keeps its head. A blank value returns "" so an adapter can fall through to its next candidate. The ruler lives here beside PayloadDetail: two adapters measuring the same channel differently would be a drift bug waiting.

Types

type Adapter

type Adapter interface {
	// Name is the canonical provider name recorded as the connection target
	// (SCHEMA.md R3), e.g. "claude-code".
	Name() string
	// Command returns the executable, args, and any extra environment
	// entries ("KEY=value", appended to the parent's environment) to launch
	// for req. The launch environment is part of knowing the CLI: a provider
	// that selects a profile via an env var (e.g. CLAUDE_CONFIG_DIR) owns
	// that here, not in shell aliases around tomobit.
	Command(req Request) (name string, args []string, extraEnv []string)
	// Translate maps one stream line to zero or more canonical events. Pure:
	// no I/O and no state, so recorded fixtures can pin the mapping.
	Translate(line []byte) ([]Event, error)
}

Adapter is a provider's entire surface (ADR-0006 Decision 2): it knows the CLI and its stream format, and nothing about the DB, connections, questions, or seq/ts.

type Event

type Event struct {
	Type    string
	Payload map[string]any
}

Event is one canonical event: an R4 type plus its payload. Adapters produce these as pure data from a single stream line; the Executor assigns ts and the Sink assigns seq (store side).

type Executor

type Executor struct {
	Adapter Adapter
	Stderr  io.Writer // child stderr passthrough; nil discards
	// Warn receives translate errors — stream lines the adapter could not
	// parse at all. Distinct from Debug: a malformed line is a real defect,
	// not the forward-compatible "unknown type, ignore" path, so callers
	// that want failures visible by default wire this to os.Stderr
	// unconditionally instead of gating it behind a debug flag. Nil discards.
	Warn io.Writer
	// Debug receives lower-signal diagnostics: lines the adapter recognised
	// and intentionally dropped (0 events, no error). Nil is silent.
	Debug io.Writer
}

Executor runs an Adapter's CLI and drives its events into a Sink.

func (*Executor) Run

func (e *Executor) Run(ctx context.Context, req Request, emit Sink) (Result, error)

Run launches the adapter's command, streams stdout line by line through Translate, and emits each event via sink. It manages the child lifecycle and returns the observed exit code and duration.

A non-zero exit is NOT an error — it is reported in Result.ExitCode so the caller can decide what to record. Run errors only on operational failures: the command not starting, the timeout firing, a failed emit, or an unexpected wait error. Cancelling ctx (SIGINT) forwards SIGINT to the child and returns without error; the caller detects the cancellation via its own context.

type Permission added in v0.2.0

type Permission string

Permission is how much a Provider may do without asking (ADR-0053 Decision 1).

Three words, chosen so tomobit never holds one CLI's vocabulary. The mapping is deliberately NOT an equivalence: claude's permission modes ask a human, codex's sandbox policies build a fence. What the words name is the *intent* — "work normally, stop at the edge" — and each Adapter says that in its own CLI's terms. Translating an intent is the Adapter's job (ADR-0006 Decision 2); pretending the two mechanisms are the same would be tomobit's mistake.

const (
	// PermissionAuto lets the Provider work and stop where it needs a person.
	// The default, because the alternative is a harness that cannot act.
	PermissionAuto Permission = "auto"
	// PermissionStrict lets it look but not touch.
	PermissionStrict Permission = "strict"
	// PermissionOpen removes the asking entirely. Never a default, and never
	// reached by omission — only by someone saying so.
	PermissionOpen Permission = "open"
)

func ParsePermission added in v0.2.0

func ParsePermission(s string) (Permission, error)

ParsePermission reads the three words, rejecting anything else — including a raw CLI mode like "bypassPermissions". Letting those through is how a CLI's vocabulary leaks into tomobit one flag at a time, and the error names the three words so the fix is obvious.

func (Permission) Resolve added in v0.2.0

func (p Permission) Resolve() Permission

Resolve turns an empty value into the default. Callers translate the result; nobody has to remember that "" means auto.

type Request

type Request struct {
	Prompt string
	// ResumeID continues a provider thread instead of starting one
	// (ADR-0022 Decision 2): the provider_session_id an earlier run of the
	// same session reported. Empty starts a fresh thread. How to continue —
	// or that the CLI cannot — is the Adapter's to know.
	ResumeID string
	// PermissionMode is tomobit's own three-word vocabulary, never a CLI's
	// (ADR-0053 Decision 1): Auto / Strict / Open. Each Adapter translates it
	// into whatever its CLI calls the same intent — claude has permission
	// modes, codex has sandbox policies, and they are not the same mechanism.
	//
	// Empty means Auto. The default is deliberately the working one: a harness
	// whose Provider cannot read a file is not a harness, and the answer to
	// "may it do this?" belongs to a person at the moment it is asked, not to
	// a flag nobody set (Decision 2).
	PermissionMode Permission
	// AllowedTools are the tools a person has granted for this session
	// (ADR-0053 Decision 3). Tool names only — never a CLI's permission DSL,
	// which is the Adapter's vocabulary to know. An Adapter whose CLI has no
	// per-tool grant ignores this rather than inventing one.
	AllowedTools []string
	Timeout      time.Duration // 0 = no limit

	// WorkDir is where Tomo works — the child's cwd (ADR-0047 Decision 2).
	// Empty inherits tomobit's own, which is what a terminal `cd` already
	// expressed. The Executor applies it and no Adapter translates it: cwd is
	// an OS concept, not a CLI's vocabulary.
	WorkDir string
	// AddDirs are the places outside WorkDir that Tomo may also work with
	// (ADR-0047 Decision 3). Unlike WorkDir this one IS translated: each
	// Adapter maps it onto its own CLI's flag, and an Adapter whose CLI has
	// no such flag ignores it rather than inventing one.
	AddDirs []string
}

Request describes a single execution. The Adapter turns it into a launch command; the Executor enforces the timeout.

type Result

type Result struct {
	// Started reports whether the child process actually launched. When
	// false, ExitCode and Duration are meaningless — the caller should treat
	// this as "nothing ran", not as a failed run (e.g. skip any adoption
	// question, since there is nothing to judge).
	Started bool
	// ErrorReported reports whether the adapter's own stream already
	// produced a provider.error event (delivered to the Sink during Run).
	// A caller that also wants to record ExitCode/runErr as provider.error
	// should skip it when this is true, to avoid recording the same failure
	// twice.
	ErrorReported bool
	ExitCode      int
	Duration      time.Duration
}

Result is what the Executor observes about a run, independent of what the stream said (ADR-0006 Decision 2).

type Sink

type Sink func(ev Event, ts int64) error

Sink records one canonical event in stream order. The Executor supplies ts (wall clock at emission); the implementation assigns seq and persists.

Directories

Path Synopsis
Package claudecode adapts the Claude Code CLI (`claude -p ...
Package claudecode adapts the Claude Code CLI (`claude -p ...
Package codex adapts the Codex CLI (`codex exec --json`) to the executor's canonical events (ADR-0010 Decision 3).
Package codex adapts the Codex CLI (`codex exec --json`) to the executor's canonical events (ADR-0010 Decision 3).

Jump to

Keyboard shortcuts

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