forge

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 25 Imported by: ? 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 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

View Source
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.

View Source
const DefaultDir = sandbox.Workspace + "/repo"

DefaultDir is where the repository is checked out in the sandbox.

View Source
const (

	// FailureLogChars is how much of a failing job's log FailureLog returns.
	FailureLogChars = 6000
)

Limits.

Variables

View Source
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.

func Redact

func Redact(s string, secrets ...string) string

Redact masks secrets in s: every literal in secrets (the token that was just used) and anything shaped like a credential (GitHub tokens, API keys, private keys, authorization headers, passwords in URLs or assignments).

func Section

func Section(st factory.Store) agent.PromptSection

Section tells the agent of a repository thread how to deliver its work. The repository is assumed to be at DefaultDir; use Checkout.Section for another Dir.

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.

func (*APIError) Error

func (e *APIError) Error() string

Error implements error.

func (*APIError) Is

func (e *APIError) Is(target error) bool

Is matches the sentinels by 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.

func (*Checkout) Setup

func (c *Checkout) Setup(ctx context.Context, threadID string, sb sandbox.Sandbox) string

Setup checks the thread's repository out into sb and returns a notice for the model. It does nothing (and returns "") for a thread that is not for a repository. On the branch of a retried task the earlier attempts' commits are fetched, so the new attempt continues them.

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

func (g *GitHub) Checks(ctx context.Context, repo, sha string) ([]factory.Check, error)

Checks implements factory.Forge: the check runs and the commit statuses of sha.

func (*GitHub) Comment

func (g *GitHub) Comment(ctx context.Context, repo string, number int, body string) error

Comment implements factory.Forge.

func (*GitHub) Comments

func (g *GitHub) Comments(ctx context.Context, repo string, number int) ([]factory.Comment, error)

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

func (g *GitHub) DefaultBranch(ctx context.Context, repo string) (string, error)

DefaultBranch implements factory.Forge.

func (*GitHub) Diff

func (g *GitHub) Diff(ctx context.Context, repo string, number int) (string, error)

Diff implements factory.Forge: the unified diff, cut at 2 MiB.

func (*GitHub) FailureLog

func (g *GitHub) FailureLog(ctx context.Context, repo string, c factory.Check) (string, error)

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

func (g *GitHub) FindOpenPR(ctx context.Context, repo, head, base string) (*factory.PRInfo, error)

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) GetPR

func (g *GitHub) GetPR(ctx context.Context, repo string, number int) (*factory.PRInfo, error)

GetPR implements factory.Forge.

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) OpenPR

func (g *GitHub) OpenPR(ctx context.Context, in factory.OpenPR) (*factory.PRInfo, error)

OpenPR implements factory.Forge.

func (*GitHub) ProtectedBranches

func (g *GitHub) ProtectedBranches(ctx context.Context, repo string) ([]string, error)

ProtectedBranches implements factory.Forge: the first 100 protected branches.

func (*GitHub) Review

func (g *GitHub) Review(ctx context.Context, repo string, number int, event, body string) error

Review implements factory.Forge.

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.

func (*Loop) OnEvent

func (l *Loop) OnEvent(ctx context.Context, ev factory.Event)

OnEvent evaluates the pull request an outside event is about right away instead of at the next tick. It works synchronously: a host calls it from a goroutine, not from the request that delivered the webhook.

func (*Loop) Run

func (l *Loop) Run(ctx context.Context)

Run ticks every Every until ctx ends.

func (*Loop) Tick

func (l *Loop) Tick(ctx context.Context) error

Tick evaluates the tasks in review that are due, at most MaxPerTick of them, the least recently evaluated first, then the parked tasks whose turn has come. A failing task does not stop the others; the errors come back joined.

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.

Jump to

Keyboard shortcuts

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