Documentation
¶
Overview ¶
Package forge connects the agent factory to GitHub: the factory.Forge over the REST API, the tools an agent uses to open and follow its pull request, the repository checkout of a task's sandbox, and the loop that drives a pull request through CI, review and merge.
Index ¶
- Constants
- Variables
- func NewTools(o ToolsOptions) tools.Provider
- func Redact(s string, secrets ...string) string
- func Section(st factory.Store) agent.PromptSection
- type APIError
- type Checkout
- type GitHub
- func (g *GitHub) Checks(ctx context.Context, repo, sha string) ([]factory.Check, error)
- func (g *GitHub) Comment(ctx context.Context, repo string, number int, body string) error
- func (g *GitHub) Comments(ctx context.Context, repo string, number int) ([]factory.Comment, error)
- func (g *GitHub) DefaultBranch(ctx context.Context, repo string) (string, error)
- func (g *GitHub) Diff(ctx context.Context, repo string, number int) (string, error)
- func (g *GitHub) FailureLog(ctx context.Context, repo string, c factory.Check) (string, error)
- func (g *GitHub) FindOpenPR(ctx context.Context, repo, head, base string) (*factory.PRInfo, error)
- func (g *GitHub) GetPR(ctx context.Context, repo string, number int) (*factory.PRInfo, error)
- func (g *GitHub) Merge(ctx context.Context, repo string, number int, sha string, ...) error
- func (g *GitHub) OpenPR(ctx context.Context, in factory.OpenPR) (*factory.PRInfo, error)
- func (g *GitHub) ProtectedBranches(ctx context.Context, repo string) ([]string, error)
- func (g *GitHub) Review(ctx context.Context, repo string, number int, event, body string) error
- type GitHubOptions
- type Loop
- type LoopOptions
- type PushResult
- type ToolsOptions
Constants ¶
const ( DefaultEvery = 30 * time.Second DefaultSettle = 60 * time.Second DefaultMaxFixRounds = 5 // DefaultMaxGap is the longest a task that shows no change goes without a poll. DefaultMaxGap = 5 * time.Minute // DefaultMaxPerTick bounds the evaluations (about three GitHub calls each) of one tick. DefaultMaxPerTick = 25 // ReadyReason is the block reason of a task whose pull request is green and approved but left to a human to merge. ReadyReason = "ready to merge: awaiting a human" )
Loop defaults.
const DefaultDir = sandbox.Workspace + "/repo"
DefaultDir is where the repository is checked out in the sandbox.
const (
// FailureLogChars is how much of a failing job's log FailureLog returns.
FailureLogChars = 6000
)
Limits.
Variables ¶
var ( ErrNotFound = errors.New("github: not found") ErrForbidden = errors.New("github: forbidden") ErrUnprocessable = errors.New("github: unprocessable") // ErrConflict is a 409: for a merge, the head moved since the sha was read. ErrConflict = errors.New("github: conflict") // ErrRateLimited is a rate limit that outlasted the bounded retry. ErrRateLimited = errors.New("github: rate limited") )
Sentinels for the HTTP statuses callers react to; use errors.Is on an error from GitHub (an *APIError carries the status and message).
Functions ¶
func NewTools ¶
func NewTools(o ToolsOptions) tools.Provider
NewTools returns the provider of open_pr, pr_status, ci_log and pr_comments. Only a thread that works on a factory task with a repository gets them.
Types ¶
type APIError ¶
type APIError struct {
Method string
Path string
Status int
Message string
// RateLimited marks a 429 or a 403 caused by the primary or secondary rate limit.
RateLimited bool
}
APIError is a GitHub API response with an error status.
type Checkout ¶
type Checkout struct {
Auth factory.RepoAuth
Store factory.Store
// Dir is the checkout directory (default /workspace/repo).
Dir string
}
Checkout clones the repository of a factory task into the sandbox of its thread, on the task's branch, and pushes that branch on the host's behalf.
The agent never holds a push credential. The token is minted per command and travels only in the environment of the one Exec that clones, fetches or pushes; nothing of it is left in the sandbox afterwards.
func (*Checkout) Push ¶
func (c *Checkout) Push(ctx context.Context, threadID string, sb sandbox.Sandbox, protected ...string) (PushResult, error)
Push publishes HEAD of the thread's checkout as the task's own branch on the remote. It is how a branch leaves the sandbox: the agent has no push credential, the host pushes, only this branch, only when open_pr asks.
protected are branch names or path.Match patterns that are never pushed. Uncommitted changes are not pushed; Dirty reports them.
func (*Checkout) Section ¶
func (c *Checkout) Section() agent.PromptSection
Section is Section for the checkout's own Dir.
type GitHub ¶
type GitHub struct {
// contains filtered or unexported fields
}
GitHub implements factory.Forge over the GitHub REST API.
func NewGitHub ¶
func NewGitHub(o GitHubOptions) *GitHub
NewGitHub returns a forge for the GitHub API.
func (*GitHub) Checks ¶
Checks implements factory.Forge: the check runs and the commit statuses of sha.
func (*GitHub) Comments ¶
Comments implements factory.Forge: the issue comments, the reviews that have a verdict or a body and the inline review comments of the pull request.
func (*GitHub) DefaultBranch ¶
DefaultBranch implements factory.Forge.
func (*GitHub) FailureLog ¶
FailureLog implements factory.Forge: the end of the log of the failed Actions job behind c, ANSI codes stripped and secrets masked. "" when the check is not a failed Actions job or its log is gone or not readable.
func (*GitHub) FindOpenPR ¶
FindOpenPR returns the open pull request from head (a branch of repo itself) into base, or nil: how a caller recovers when OpenPR says one already exists.
func (*GitHub) Merge ¶
func (g *GitHub) Merge(ctx context.Context, repo string, number int, sha string, m factory.MergeMethod) error
Merge implements factory.Forge. A non-empty sha makes GitHub refuse (409) when the head moved.
func (*GitHub) ProtectedBranches ¶
ProtectedBranches implements factory.Forge: the first 100 protected branches.
type GitHubOptions ¶
type GitHubOptions struct {
// API is the REST root (default https://api.github.com; an Enterprise server has its own).
API string
// Auth mints the token of every request (installation tokens expire); nil sends no credentials.
Auth factory.RepoAuth
// HTTP is the client (default: one with a 30 s timeout).
HTTP *http.Client
// UserAgent is sent with every request (default "harness-factory").
UserAgent string
// Sleep waits for a rate limit (default: a timer that ends with ctx). For tests.
Sleep func(ctx context.Context, d time.Duration) error
// Now is the clock for rate-limit reset times (default time.Now).
Now func() time.Time
}
GitHubOptions configures a GitHub forge.
type Loop ¶
type Loop struct {
LoopOptions
// contains filtered or unexported fields
}
Loop drives the pull requests of the tasks in review: it waits for CI, feeds failures, conflicts and review findings back to the agent, asks the gate, and merges or hands the pull request to a human. All progress is kept in the task's events and fields, so a restarted process continues where the last one stopped.
func NewLoop ¶
func NewLoop(o LoopOptions) *Loop
NewLoop returns a loop; the zero options are filled with the defaults.
type LoopOptions ¶
type LoopOptions struct {
Forge factory.Forge
Store factory.Store
Controller factory.Controller
// Gate judges a pull request whose checks are green (nil: no review gate).
Gate factory.Gate
// Every is the polling interval of Run (default 30 s).
Every time.Duration
// Now is the clock (default time.Now). Task events are stamped with it, so it must be the store's clock too.
Now func() time.Time
// MergeMethod is how a pull request merges (default squash).
MergeMethod factory.MergeMethod
// RequireChecks keeps a pull request from merging while its head reports no
// check at all. A bool cannot say "default true", so the zero options
// already require checks: a repository without CI sets AllowNoChecks (and
// leaves RequireChecks false) to let a head with no check merge.
RequireChecks bool
AllowNoChecks bool
// Settle is how long the checks must stay green after the last head change
// before the pull request is judged, so a check that registers late cannot
// be skipped (default 60 s).
Settle time.Duration
// MaxFixRounds caps the automatic new attempts on one pull request (CI,
// conflicts, review findings, requested changes); then the task blocks (default 5).
MaxFixRounds int
// MaxGap caps the adaptive polling: an evaluation that found nothing new
// doubles the task's gap from Every up to this (default 5 min). A webhook
// event or any change brings it back to Every.
MaxGap time.Duration
// MaxPerTick caps the tasks one tick evaluates, the least recently
// evaluated first, so a burst of pull requests spreads over ticks instead
// of spiking the GitHub API (default 25).
MaxPerTick int
}
LoopOptions configures the pull-request loop.
type PushResult ¶
type PushResult struct {
// Branch is the branch that was pushed.
Branch string
// Ahead is the number of commits the branch has beyond its base.
Ahead int
// Dirty is the number of uncommitted changes, which are not pushed.
Dirty int
}
PushResult is what Checkout.Push published.
type ToolsOptions ¶
type ToolsOptions struct {
Forge factory.Forge
// Store tells which thread works on which task, repository and branch.
Store factory.Store
// Controller moves the task to review when the agent opens its pull request.
Controller factory.Controller
// Push publishes the thread's branch from the host (see Checkout.Push);
// protected are the branches it must refuse. open_pr calls it before it
// opens or updates the pull request. Nil leaves pushing to the agent, for
// hosts whose sandbox holds its own push credentials.
Push func(ctx context.Context, threadID string, protected []string) (PushResult, error)
}
ToolsOptions configures the agent tools of the forge.