task

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 30 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 task holds what the daemon, the bridge and `stagent task` share about tasks (タスク): the canonical path of a source checkout (元のチェ ックアウト), which paths lie in a worktree, and running git.

Index

Constants

View Source
const (
	ExitFailure  = 1
	ExitUsage    = 2
	ExitNotFound = 127
)

Exit codes of `stagent task`.

View Source
const (

	// MaxCopyBytes and MaxCopyEntries bound what one worktree gets; an
	// entry that would pass either is skipped (limit).
	MaxCopyBytes   = 2 << 30
	MaxCopyEntries = 50_000
)
View Source
const IncludeFile = ".worktreeinclude"

IncludeFile is Orca's list of paths to copy, at a checkout's root.

View Source
const OrcaFile = "orca.yaml"

OrcaFile is Orca's per-repository settings file, at a checkout's root. stagent only reads it.

Variables

This section is empty.

Functions

func Canonical

func Canonical(ctx context.Context, env []string, dir string) (string, error)

Canonical returns the canonical path (正の形) of the source checkout that dir lies in: `git rev-parse --show-toplevel`, symlinks resolved (CanonicalPath). A linked worktree is a checkout of its own: it is not mapped to its main worktree. Task.Repo and task.preview's repo are this value, as is the key of config.json's repos.

func CanonicalPath

func CanonicalPath(p string) string

CanonicalPath makes p absolute and clean, resolves its symlinks when it exists and, on Windows, upper-cases its drive letter.

func Changes

func Changes(ctx context.Context, env []string, worktree string, links []string) ([]string, error)

Changes lists what removing worktree would lose, as `git status --porcelain` lines: tracked files changed (staged or not) and untracked files git does not ignore. Ignored files (a copied .env) are not changes, nor are the shared-directory links named in links (paths relative to worktree, see ShareLinks).

func CheckBranch

func CheckBranch(ctx context.Context, env []string, repo, name string) error

CheckBranch fails when name cannot be a branch name.

func CopyFiles

func CopyFiles(ctx context.Context, j CopyJob)

CopyFiles links the shared directories and then copies what is to be copied into j.Dest, leaving every destination that exists as it is (a retry copies only what is missing), and nothing under a link. A shared directory that cannot be linked gets one warning line; what is listed under it is copied as ordinary files instead. Nothing here fails the task: errors are lines in the terminal.

func CurrentBranch

func CurrentBranch(ctx context.Context, env []string, dir string) (string, error)

CurrentBranch is the branch checked out in dir, "" when detached.

func DefaultBranch

func DefaultBranch(ctx context.Context, env []string, repo string) (string, error)

DefaultBranch is the repository's default branch as the checkout repo knows it: origin/HEAD (set by clone, or `git remote set-head origin --auto`), else origin's main or master.

func GH

func GH(ctx context.Context, env []string, dir string, args ...string) ([]byte, error)

GH runs gh in dir with env (nil: this process's), found on env's PATH, and returns its standard output. A failing gh is an error with its last stderr line.

func Git

func Git(ctx context.Context, env []string, dir string, args ...string) ([]byte, error)

Git runs git in dir with env (nil: this process's environment) and returns its standard output. git is looked up in env's PATH. On Windows every call gets -c core.longpaths=true. A git that cannot be found is an error wrapping exec.ErrNotFound; one that fails is a *GitError.

func HasRef

func HasRef(ctx context.Context, env []string, repo, ref string) bool

HasRef reports whether ref names a commit in the checkout repo.

func IsDir

func IsDir(p string) bool

IsDir reports whether p is an existing directory.

func IssueBranch

func IssueBranch(number int, title string) string

IssueBranch is an issue's default branch: <number>-<slug>, the slug being its title lower-cased, every run of characters other than a-z and 0-9 one hyphen, trimmed of hyphens, at most 40 characters. When nothing of the title is left the branch is issue-<number>.

func LinkDir

func LinkDir(target, link string) error

LinkDir makes link a symbolic link to the directory target (a shared directory of a task's worktree).

func Listed

func Listed(ctx context.Context, env []string, repo, worktree string) (bool, error)

Listed reports whether worktree (canonical) is a worktree of the checkout repo. An error means git could not answer: not found (wrapping exec.ErrNotFound) or failing, e.g. because repo is gone.

func Main

func Main(args []string) int

Main is `stagent task`.

func NewID

func NewID() string

NewID returns a random 16-hex-char task id.

func PosixPath

func PosixPath(p string) string

PosixPath writes a Windows path the way Git Bash does: C:\a\b is /c/a/b.

func Prepare

func Prepare(ctx context.Context, p Prep) error

Prepare runs the stages of p's task in order — fetch, branch, worktree, copy, setup — stopping at the first that fails (*StageError). A worktree task whose directory is left empty then has it removed (after leaving it when it is the working directory). Every stage can run twice: a task that failed is prepared again with the same task (ADR 0006).

func Preview

func Preview(ctx context.Context, env []string, repo string, patterns []string) (*wire.TaskPreviewResult, error)

Preview is task.preview for the source checkout repo (canonical) with the app's patterns: the plan, at most wire.TaskPreviewRows copies and skips, and the checkout's orca.yaml scripts.setup or why that file is unusable (its sharedDirectories then ignored).

func PullRequestRefs

func PullRequestRefs(ctx context.Context, env []string, repo string, number int) (head, base string, err error)

PullRequestRefs asks gh, in the checkout repo, for a pull request's head branch (a task's branch, what `gh pr checkout` checks out) and base branch.

func Remove

func Remove(ctx context.Context, r Removal) (keptBranch string, err error)

Remove runs the stages of r's removal in order — archive (when asked), worktree (unlink the shared-directory links, then git worktree remove), branch — stopping at the first that fails (*StageError). It returns the task's branch when it is kept: `git branch -d` refused it and gh does not say its pull request was merged at the very commit the branch is on. Remote branches are never deleted.

func RunScript

func RunScript(ctx context.Context, s Script) error

RunScript runs s (PlanScript for this OS) in s.Dir and waits for it. A script exiting non-zero is an error naming its exit code.

func SameKey added in v0.14.0

func SameKey(t *wire.Task, repo, kind string, number int, name string) bool

SameKey reports whether t is the task keyed by the source checkout repo (canonical) and, for kind name, name (exactly), else number: a named task never shares its key with an issue's or PR's.

func SamePath

func SamePath(a, b string) bool

SamePath reports whether two canonical paths name the same place (case-insensitively on Windows).

func SavedPatterns

func SavedPatterns(configPath, repo string) []string

SavedPatterns reads the app's patterns for the source checkout repo (canonical) from config.json at configPath. Only the daemon writes that file (config.set); a missing or unreadable one has none.

func SavedWorktreeFolder added in v0.14.0

func SavedWorktreeFolder(configPath string) string

SavedWorktreeFolder reads the worktree folder (worktree の置き場) from config.json at configPath, as config.set stored it: absolute and clean. "": none set, or a missing or unreadable file.

func ShareLinks(repo, worktree string) []string

ShareLinks lists the shared directories (worktree.sharedDirectories of the source checkout's orca.yaml) that are links in worktree — symbolic links, or junctions on Windows — as slash-separated paths relative to it. An orca.yaml that cannot be read has none.

func Unpushed

func Unpushed(ctx context.Context, env []string, repo, branch string) (int, error)

Unpushed counts the commits of the checkout repo's local branch that no remote-tracking branch has (`git rev-list <branch> --not --remotes --count`); 0 when there is no such branch.

func ValidName added in v0.14.0

func ValidName(ctx context.Context, env []string, repo, name string) error

ValidName fails when name cannot be a named task's name: it must be 1–64 characters without whitespace, control characters or any of / \ : * ? " < > |, not start with . or -, and be a branch name git takes as it is (`git check-ref-format --branch`, run in the checkout repo, printing it back: @ and @{-N} name other branches). The name is also a directory under the worktree folder and the default branch; the app mirrors all but the git check.

func ValidPattern

func ValidPattern(p string) error

ValidPattern reports why p cannot be an app pattern (a git pathspec glob from the checkout's root): empty, a negation (!), absolute, or with a .. or .git component.

func ValidRepoKey

func ValidRepoKey(key string) error

ValidRepoKey reports why key cannot be a key of config.json's repos: it must be an absolute, clean path (no trailing separator). Whether it exists does not matter.

func Within

func Within(p, dir string) bool

Within reports whether canonical path p is dir or lies inside it.

func Worktrees

func Worktrees(ctx context.Context, env []string, repo string) ([]string, error)

Worktrees lists the worktrees `git worktree list` knows for the checkout repo (its main worktree first), as canonical paths.

Types

type CopyJob

type CopyJob struct {
	CopySpec
	Dest string
	// Out (the terminal) gets one line per link, copy and skip.
	Out io.Writer
	// Link makes link point to the directory target; nil: LinkDir.
	Link func(target, link string) error
}

CopyJob is the copy stage: spec's links and copies made in Dest, a new worktree of spec.Repo.

type CopyPlan

type CopyPlan struct {
	Copies     []wire.TaskCopy
	Links      []string // shared directories, relative with / separators
	Skipped    []wire.TaskSkip
	TotalBytes int64
}

CopyPlan is a resolved CopySpec, every shared directory taken as linked.

func PlanCopies

func PlanCopies(ctx context.Context, spec CopySpec) (*CopyPlan, error)

PlanCopies resolves spec: what a new worktree would get when every shared directory links. Only git failing is an error.

type CopySpec

type CopySpec struct {
	// Repo is the source checkout (canonical).
	Repo string
	// Env is git's environment (nil: this process's).
	Env []string
	// Patterns are the app's (config.json repos.<Repo>.worktree_include).
	Patterns []string
	// Shared is orca.yaml's worktree.sharedDirectories of Repo.
	Shared []string
}

CopySpec is what a worktree of Repo gets copied and linked.

type GitError

type GitError struct {
	Args   []string
	Stderr string
	Err    error
}

GitError is git exiting with an error: its first stderr line names why.

func (*GitError) Error

func (e *GitError) Error() string

func (*GitError) Unwrap

func (e *GitError) Unwrap() error

type Orca

type Orca struct {
	Setup             string   // scripts.setup ("": none)
	Archive           string   // scripts.archive ("": none)
	SharedDirectories []string // worktree.sharedDirectories, as written
}

Orca is what stagent uses of an orca.yaml. Which checkout's file counts depends on the key: scripts.setup comes from the task's worktree; scripts.archive and worktree.sharedDirectories from the source checkout (元のチェックアウト).

func LoadOrca

func LoadOrca(dir string) (*Orca, error)

LoadOrca reads the orca.yaml in dir. No file is (nil, nil). Like Orca, a file that is larger than 256 KiB, is not valid YAML, repeats a key or is not a mapping is refused whole (*OrcaError); a value of the wrong type, or a string over 64 KiB, is dropped alone and the rest kept. Unknown keys are ignored.

type OrcaError

type OrcaError struct {
	Path string
	Err  error
}

OrcaError is an orca.yaml that cannot be used at all.

func (*OrcaError) Error

func (e *OrcaError) Error() string

func (*OrcaError) Unwrap

func (e *OrcaError) Unwrap() error

type Prep

type Prep struct {
	Task wire.Task
	// SwitchBranch (in_place only): switch the checkout to Task.Branch.
	SwitchBranch bool
	// Setup (in_place only): run scripts.setup. A worktree task always
	// runs it.
	Setup bool
	// WorktreeInclude are the app's patterns of files to copy into a
	// worktree task's worktree (config.json repos.<repo>.worktree_include).
	WorktreeInclude []string
	// Env is the environment of git, gh and the setup script; nil: this
	// process's.
	Env []string
	// Stdin feeds the setup script (nil: none); Out, the terminal, gets
	// the stage headers, the commands run and their output.
	Stdin io.Reader
	Out   io.Writer
	// Stage is told each stage as it starts (task.progress).
	Stage func(stage string)
}

Prep is a task to prepare: what `stagent task run` does in the task's first session before it starts the agent (ADR 0006).

type Removal

type Removal struct {
	Task wire.Task
	// Archive runs scripts.archive of the source checkout's orca.yaml
	// first, in the worktree.
	Archive bool
	// Force removes a worktree with changes (git worktree remove --force).
	Force bool
	// Env is the environment of git, gh and the archive script; nil: this
	// process's.
	Env []string
	// Stdin feeds the archive script (nil: none); Out, the terminal (nil:
	// nowhere), gets the stage headers, the commands run and their output.
	Stdin io.Reader
	Out   io.Writer
	// Stage is told each stage as it starts (task.progress).
	Stage func(stage string)
}

Removal is 片付け of a worktree task once its sessions have stopped: what `stagent task remove` runs, or the bridge itself without archive (ADR 0006).

type Script

type Script struct {
	Text string
	// Dir is the task's worktree: the working directory and
	// ORCA_WORKTREE_PATH (ORCA_WORKSPACE_NAME is its base name).
	Dir string
	// Root is the source checkout (元のチェックアウト): ORCA_ROOT_PATH.
	Root string
	// Env is the environment before the ORCA_* variables; nil: this
	// process's.
	Env    []string
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
}

Script is an orca.yaml script (scripts.setup, scripts.archive) to run the way Orca runs it.

type ScriptPlan

type ScriptPlan struct {
	Program   string
	Args      []string
	Runner    string
	RunnerExt string
	Env       []string
}

ScriptPlan is how a Script runs: Program with Args and, when Runner is set, the path of a file holding Runner (suffix RunnerExt) as the last argument; Env is its environment.

func PlanScript

func PlanScript(goos string, s Script, git string) (ScriptPlan, error)

PlanScript decides how s runs on goos. git is git's path, used on Windows only, to find Git for Windows' bash.exe next to it.

  • Linux, macOS: the login shell (SHELL of s.Env when it is a POSIX shell, else /bin/sh) as `<shell> -l -c`, with `set -e` first: the first failing command fails the script.
  • Windows: cmd.exe (%ComSpec%) runs a .cmd runner, as Orca's: every line is `call`ed and the first one failing (errorlevel) ends it. A script starting with #! runs in Git for Windows' bash.exe instead, with `set -e` and the ORCA_* paths in /c/… form; without that bash it is an error.

type StageError

type StageError struct {
	Stage string
	Err   error
}

StageError is the stage that failed and why.

func (*StageError) Error

func (e *StageError) Error() string

func (*StageError) Unwrap

func (e *StageError) Unwrap() error

type Worktree

type Worktree struct {
	Path   string // canonical
	Branch string // short name; "" when detached (or bare)
}

Worktree is one entry of `git worktree list`.

func ListWorktrees

func ListWorktrees(ctx context.Context, env []string, repo string) ([]Worktree, error)

ListWorktrees lists the worktrees `git worktree list` knows for the checkout repo, its main worktree first.

Jump to

Keyboard shortcuts

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