ciloop

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 16 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 ciloop is the CI-aware PR loop (docs/ci-loop.md): once Lectern opens a pull request for a task or session on a project that opted in, it watches the PR's checks through the target's own `gh`, and when they fail it sends the owning agent a short failure report — failing job names and a trimmed, redacted log tail — asking it to fix them. A session's agent pushes its own fix; a task's headless follow-up usually cannot run git, so Lectern commits and pushes what it leaves (commit.go). It stops on pass, merge, close or the attempt cap, and pushes a phone notification when CI goes green after fixes or when it gives up.

There are no webhooks (Lectern is tailnet-only). The loop polls, with backoff, and only while a PR still has pending or failing checks.

Index

Constants

View Source
const (
	StatePending = "pending" // checks running, or not reported yet
	StateFailing = "failing" // failed; a fix request was sent, waiting for a push
	StatePassed  = "passed"
	StateCapped  = "capped" // still failing after max_attempts fix requests
	StateMerged  = "merged"
	StateClosed  = "closed"
	StateNone    = "none"    // the PR never reported any checks
	StateError   = "error"   // gh cannot do the job (not signed in, not installed, …)
	StateStalled = "stalled" // no progress for StallAfter
)

Watch states stored in ci_watches.state. pending and failing are the active ones; everything else is terminal.

View Source
const (
	DefaultMinInterval   = 30 * time.Second
	DefaultMaxInterval   = 10 * time.Minute
	DefaultStallAfter    = 24 * time.Hour
	DefaultNoChecksAfter = 15 * time.Minute
)

Defaults for the poll schedule.

Variables

View Source
var ErrAlreadyWatched = errors.New("the CI loop is already working on this pull request")

ErrAlreadyWatched is ArmAsked's answer when the loop is already working on the PR for someone else.

View Source
var ErrChecksNotFailing = errors.New("no checks are failing on this pull request")

ErrChecksNotFailing is FixReport's answer for a PR with nothing to fix.

View Source
var ErrNotEnabled = errors.New("the CI loop is not enabled for this project")

ErrNotEnabled is Arm's answer for a project that has not opted in.

Functions

func FindPRURL

func FindPRURL(output string) string

FindPRURL pulls a PR URL out of gh output — `gh pr create` prints it on success, and names the existing one when a PR for the branch already exists.

func IsActionsLink(link string) bool

IsActionsLink reports whether a check link is a GitHub Actions job, the only kind JobLog can read.

func JobLog added in v2.6.0

func JobLog(ctx context.Context, ex executor.Executor, prURL, link string) (string, bool)

JobLog fetches one GitHub Actions job's log for a person to read (the pull request page's check drill-down): the failed steps when there are any, else the whole job. It shares failedLog's command and repository resolution; ok is false for a check that is not an Actions job.

func Label

func Label(cw *store.CIWatch) string

Label is the one-line status a card shows, e.g. "CI failing — attempt 2/3".

func Redact

func Redact(s string) string

Redact masks obvious credentials in text headed for an agent.

func TrimForAgent added in v2.6.0

func TrimForAgent(raw string) string

TrimForAgent is the loop's own per-job trimming, for a failure report built outside the loop (a GitLab job's log in "Fix checks with agent").

func ValidPRURL

func ValidPRURL(s string) bool

ValidPRURL reports whether s looks like a pull request URL.

func ViewLog added in v2.6.0

func ViewLog(raw string) string

ViewLog is trimJobLog for a person: the same cleaning and redaction with a much larger tail. Redacted because the page travels to phones and relay clients like everything else.

Types

type Notifier

type Notifier interface {
	Notify(title, body, urlPath string, extra *sinks.Extra)
}

Notifier is the phone-push side; *sinks.Notifier satisfies it.

type Owner

type Owner struct {
	TaskID    *int64
	SessionID *int64
	ProjectID *int64
	TargetID  int64
	Branch    string
}

Owner is who opened a PR, and so who gets told when its CI fails.

type SessionSender

type SessionSender interface {
	SendNotice(ctx context.Context, id int64, text string) error
}

SessionSender delivers a message into a live session's agent. It is agent-agnostic: *sessions.Manager types it into the tmux pane, which is how every CLI Lectern runs receives a message.

type View

type View struct {
	ID          int64    `json:"id"`
	TaskID      *int64   `json:"task_id,omitempty"`
	SessionID   *int64   `json:"session_id,omitempty"`
	PRURL       string   `json:"pr_url"`
	State       string   `json:"state"`
	Attempts    int      `json:"attempts"`
	MaxAttempts int      `json:"max_attempts"`
	Failing     []string `json:"failing"`
	Detail      string   `json:"detail,omitempty"`
	Label       string   `json:"label"`
	Active      bool     `json:"active"`
	UpdatedAt   float64  `json:"updated_at"`
}

View is the compact form a task or session card renders.

func ViewOf

func ViewOf(cw *store.CIWatch) *View

ViewOf builds a card view of a watch.

type Watcher

type Watcher struct {
	DB       *store.DB
	Reg      *executor.Registry
	Bus      *bus.Bus
	Notifier Notifier
	Sessions SessionSender
	Log      *slog.Logger

	MinInterval, MaxInterval, StallAfter, NoChecksAfter time.Duration
	// Now is the clock, replaceable in tests.
	Now func() float64
	// Sync polls due watches inline in Tick instead of on goroutines.
	Sync bool
	// contains filtered or unexported fields
}

Watcher owns every ci_watches row.

func New

func New(db *store.DB, reg *executor.Registry, b *bus.Bus, n Notifier, s SessionSender, log *slog.Logger) *Watcher

New builds a Watcher with the default schedule.

func (*Watcher) Arm

func (w *Watcher) Arm(o Owner, prURL string, force bool) (*store.CIWatch, error)

Arm starts watching prURL for owner. Unless force is set (a human asking for it explicitly), the owner's project must have opted in; otherwise it returns ErrNotEnabled and records nothing.

func (*Watcher) ArmAsked added in v2.6.0

func (w *Watcher) ArmAsked(o Owner, prURL, headSHA string) (*store.CIWatch, error)

ArmAsked starts watching prURL for o as though fix request 1 had already been delivered for headSHA: the loop waits for a push, then carries on. A PR the loop is already actively watching is left alone.

func (*Watcher) AttemptFinished

func (w *Watcher) AttemptFinished(att *store.Attempt)

AttemptFinished is the scheduler telling the loop that a task attempt ended. If it was a CI fix, the watch looks again now rather than after its backoff, so Lectern commits and pushes the fix straight away.

func (*Watcher) FixReport added in v2.6.0

func (w *Watcher) FixReport(ctx context.Context, ex executor.Executor, prURL, branch string, maxAttempts int) (report, headSHA string, err error)

FixReport reads the PR and its checks through the target's gh and builds fix request 1 of maxAttempts, the same report the loop itself sends.

func (*Watcher) Poll

func (w *Watcher) Poll(ctx context.Context, id int64)

Poll is one observation of one watch: read the PR and its checks, decide, act, and schedule the next look.

func (*Watcher) Pushed

func (w *Watcher) Pushed(o Owner)

Pushed is Lectern having pushed an owner's branch (the Commit → Push button): its active watches look again now, with their backoff reset.

func (*Watcher) Tick

func (w *Watcher) Tick(ctx context.Context)

Tick polls every due watch. The scheduler calls it once per tick; each poll runs on its own goroutine (unless Sync) so a slow `gh` never holds up task scheduling, and a watch is never polled twice at once.

func (*Watcher) Watching added in v2.6.0

func (w *Watcher) Watching(prURL string) *View

Watching reports the loop's active watch on prURL, if any.

Jump to

Keyboard shortcuts

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