prstate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package prstate fetches a PR's diff at pinned SHAs and builds the state the decision model sees.

Index

Constants

View Source
const MaxBatches = 16

MaxBatches caps patch batches per context, bounding cost per PR.

Variables

View Source
var DefaultLimits = Limits{Question: jev.MaxQuestionTokens, Request: jev.MaxRequestTokens}

DefaultLimits are the provider's limits.

View Source
var DefaultRegistries = []string{
	"registry.npmjs.org", "registry.yarnpkg.com", "pypi.org", "files.pythonhosted.org",
	"index.crates.io", "static.crates.io", "rubygems.org", "pub.dev", "pub.dartlang.org",
}

DefaultRegistries are the hosts a lockfile may resolve packages from without holding the PR. Config adds to them.

View Source
var ErrHeadMoved = errors.New("PR head moved while listing files")

ErrHeadMoved means the PR head changed while files were being listed, so the file list may not match the scored SHA.

View Source
var ErrNoFiles = errors.New("no files to score after excludes")

ErrNoFiles means nothing is left to score after excludes.

Functions

func IsAutoRun

func IsAutoRun(p string) bool

IsAutoRun reports whether p is an auto-run file.

func IsLockfile

func IsLockfile(p string) bool

IsLockfile reports whether p is a lockfile the check reads.

func Matching

func Matching(globs []string, files ...[]File) []string

Matching returns the paths of files whose path matches any glob.

Types

type AutoRun

type AutoRun struct {
	Path, What string
}

AutoRun is a file the diff changes that sets commands to run on their own on a developer's machine: editor tasks, dev containers, git hooks, and AI-agent hooks. Attacks use these to run code without anyone starting it (docs/calibration.md#safety). Any change to one holds, because a command edit or a trigger outside the patch's context would slip past a check of the trigger line alone.

type Call

type Call struct {
	Context   rubric.Context
	Questions []rubric.Question
	State     State
	Batch     int
	Batches   int
}

Call is one decision request. Batch and Batches are 1-based; an unbatched call has Batches == 1. Values from batches of one question are min-combined.

type Fetcher

type Fetcher func(ctx context.Context, pr PR) (*Raw, error)

Fetcher reads a PR's raw diff. Replay passes one that reads its local dataset first.

type File

type File struct {
	Path         string `json:"path"`
	PreviousPath string `json:"previous_path,omitempty"`
	Status       string `json:"status"`
	Additions    int    `json:"additions"`
	Deletions    int    `json:"deletions"`
	Patch        string `json:"patch,omitempty"`
	Changes      int    `json:"-"` // GitHub's count; above 0 with no patch means GitHub omitted it
}

File is one changed file. Patch is empty for excluded and unseen files.

type Limits

type Limits struct {
	Question int
	Request  int
}

Limits are token budgets: state plus any one question, and the whole call.

type LockSource

type LockSource struct {
	Path, URL, Host string
}

LockSource is a package source a lockfile change adds that isn't one of the usual registries. Lockfiles are excluded from the model by default, so this check is the only thing that reads them.

type PR

type PR struct {
	Owner, Repo  string
	Number       int
	BaseSHA      string
	HeadSHA      string
	BaseRef      string
	HeadRef      string
	Title        string
	Body         string
	Labels       []string
	Author       string
	Fork         bool
	ChangedFiles int
}

PR identifies the scored PR. SHAs come from the event payload, never from a fresh read of the PR.

func FromPullRequest

func FromPullRequest(owner, repo string, p *github.PullRequest) PR

FromPullRequest pins a PR from an event payload or API read.

type PRState

type PRState struct {
	Title        string   `json:"title"`
	Body         string   `json:"body"`
	Base         string   `json:"base"`
	Head         string   `json:"head"`
	Labels       []string `json:"labels,omitempty"`
	LinkedIssues []string `json:"linked_issues,omitempty"`
}

PRState is the `pr` field of state.

type Plan

type Plan struct {
	Calls   []Call
	Partial []string
	Unseen  []Unseen
}

Plan is the calls for one scoring run and what they couldn't show.

type Raw

type Raw struct {
	PR           PR
	Files        []File
	LinkedIssues []string
	Partial      []string // why GitHub's file list is incomplete
}

Raw is what Fetch reads from GitHub, before exclude globs. Replay stores it so a later run can build the snapshot without GitHub.

func FetchRaw

func FetchRaw(ctx context.Context, gh *github.Client, pr PR) (*Raw, error)

FetchRaw reads pr's files and linked issue titles.

func (*Raw) Snapshot

func (r *Raw) Snapshot(exclude []string) (*Snapshot, error)

Snapshot applies exclude globs. It returns ErrNoFiles, with the snapshot, when nothing is left to score.

type Snapshot

type Snapshot struct {
	PR           PR
	LinkedIssues []string
	Files        []File // patches the model sees
	Excluded     []File // matched exclude globs; stats only
	Unseen       []Unseen
	// Partial lists reasons the model's view is incomplete. A partial view
	// never approves.
	Partial []string
	// LockSources are lockfile package sources outside DefaultRegistries,
	// read from patches before exclude globs drop them.
	LockSources []LockSource
	// AutoRuns are commands the diff sets to run on their own on a
	// developer's machine.
	AutoRuns []AutoRun
}

Snapshot is everything fetched for one scoring run.

func Fetch

func Fetch(ctx context.Context, gh *github.Client, pr PR, exclude []string) (*Snapshot, error)

Fetch reads the diff for pr and applies exclude globs.

func (*Snapshot) Plan

func (s *Snapshot) Plan(qs []rubric.Question, lim Limits) (*Plan, error)

Plan groups questions by context, one call per context when it fits. When it doesn't, diff-local questions go over patch batches and the rest over trimmed patches, and the plan is a partial view.

func (*Snapshot) State

func (s *Snapshot) State(c rubric.Context) State

State builds the full, untrimmed state for a context.

type State

type State struct {
	PR       *PRState `json:"pr,omitempty"`
	Files    []File   `json:"files"`
	Excluded []File   `json:"excluded"`
	Note     string   `json:"note,omitempty"`
}

State is the JSON object sent as the decision request's state. The diff context has no `pr` field, so author-written prose can't sway it.

type Unseen

type Unseen struct {
	Path   string
	Reason string
}

Unseen is a file the model could not see in full, and why.

Jump to

Keyboard shortcuts

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