Documentation
¶
Overview ¶
Package review runs a directory of prompts as independent reviews, each on its own sidecar.
Index ¶
- Constants
- Variables
- func ClientExec(ctx context.Context, entry *sidecar.PoolEntry, script string, ...) (int, error)
- func CredentialRejected(stdout, stderr string) bool
- func DetachEnv(circleCIToken string, opts Options) map[string]string
- func DetachScript(s DetachSpec) string
- func Env(cred Credential, baseURL string) map[string]string
- func IsPromptFile(name string) bool
- func PoolSize(parallelism, prompts int) int
- func ReadScript(runDir string) string
- func WaitReady(ctx context.Context, waitSynced func(context.Context) error) error
- type Credential
- type DetachSpec
- type Execer
- type Finding
- type Options
- type Parsed
- type ProgressEvent
- type Prompt
- type PromptState
- type Result
- type RunStatus
Constants ¶
const ( ResultFile = "review.json" // the --json report LogFile = "review.err" // progress and errors ExitFile = "exit" // exit code; absent while the run is going PidFile = "pid" // the run's shell; lets a reader tell a dead run from a live one )
Files a detached run leaves in its run directory.
const ( // ExitNoReview means the chunk on the primary runs but has no review command. ExitNoReview = 64 // ExitBadBinary means the chunk on the primary cannot be executed at all, // for example a build for another OS or architecture. ExitBadBinary = 66 )
Exit codes of the detach script.
const ( RunRunning = "running" RunDone = "done" RunMissing = "missing" RunDied = "died" // no exit code, and the run's process is gone )
Run states reported by ReadScript.
const ( SeverityHigh = "high" SeverityMedium = "medium" SeverityLow = "low" SeverityInfo = "info" )
Severities a finding can carry, most serious first.
const DefaultDir = ".chunk/reviews"
DefaultDir is where prompts are read from, relative to the project root, when no directory is given.
const DefaultTimeout = 15 * time.Minute
DefaultTimeout bounds one review. Claude explores the repo before answering, so this is minutes rather than the seconds a single API call would take.
const ExitClaudeMissing = 97
ExitClaudeMissing is the exit code a script running claude on a sidecar uses for claude not being on PATH, so every caller reports it the same way. Not the shell's own 127, which claude also exits with when something it shelled out to is missing: that is one broken review, not a dead pass.
const FindingsSchema = `` /* 1096-byte string literal not displayed */
FindingsSchema is the JSON Schema a review's answer must satisfy when structured findings are wanted. It is passed to claude with --json-schema, so Claude Code validates the answer itself and retries until it conforms; the prompt is left as written.
const InstallRelease = `` /* 203-byte string literal not displayed */
InstallRelease is the shell that installs the latest released chunk into $HOME on a Linux sidecar. Used when no local binary is uploaded.
const (
// MaxFindings caps the findings kept from one review.
MaxFindings = 50
)
Limits on what is accepted from a model's structured output. They exist because the output is untrusted text: a runaway or manipulated review must not be able to make the daemon hold or post an unbounded amount.
const PoolName = "review"
PoolName is the sidecar pool name for reviews. It keys the persisted pool state in .chunk/review-pool.json, so a later run — or a later pass in the same run — picks up the same warm sidecars instead of booting new ones.
const PrimaryPoolName = "review-primary"
PrimaryPoolName names the one-sidecar pool that hosts a detached review. It differs from PoolName so the primary's own pool of reviewers, which lives in the primary's checkout, never collides with the laptop's state.
const RunsDir = ".chunk-review"
RunsDir is where detached runs keep their files on the primary, relative to the sidecar user's home.
const UploadInstall = `gunzip > "$HOME/chunk.new" && chmod +x "$HOME/chunk.new" && mv -f "$HOME/chunk.new" "$HOME/chunk"`
UploadInstall is the shell that installs a binary streamed on stdin, gzipped, as $HOME/chunk. It writes a new file and moves it into place, because writing over a chunk that an earlier detached review is still running fails with "text file busy".
Variables ¶
var EditTools = []string{
"Read", "Grep", "Glob", "Edit", "Write",
"Bash(git diff:*)", "Bash(git status:*)",
}
EditTools is the tool set of a run that fixes code rather than reviewing it: everything a review can do plus Edit and Write. It is used only for the apply pass, whose result is a diff that a person reads before anything leaves the daemon, and never for a review.
var ErrClaudeMissing = errors.New("claude is not installed on the sidecar")
ErrClaudeMissing is returned when a sidecar has no claude binary.
var ErrCredentialRejected = errors.New("anthropic rejected the credential")
ErrCredentialRejected is returned when Anthropic rejects the credential.
var ErrNoPrompts = errors.New("no prompts found")
ErrNoPrompts is returned when a prompts directory holds nothing to review.
Functions ¶
func ClientExec ¶
func ClientExec(ctx context.Context, entry *sidecar.PoolEntry, script string, env map[string]string, onOutput circleci.OutputFn, onSubmitted func(string)) (int, error)
ClientExec runs scripts through the pool entry's CircleCI client. Submit and stream are kept apart so onSubmitted sees the command ID: the caller needs it before the command ends, not after.
func CredentialRejected ¶ added in v0.7.196
CredentialRejected reports whether claude failed to authenticate. Both streams are checked: the 401 lands on stdout, while stderr can carry unrelated warnings.
func DetachEnv ¶ added in v0.7.195
DetachEnv is the environment the primary's review runs with: the credentials its own chunk needs to create the reviewer sidecars and to run Claude on them.
func DetachScript ¶ added in v0.7.195
func DetachScript(s DetachSpec) string
DetachScript builds the shell script that starts the review on the primary and returns at once. The review runs under nohup and setsid so it outlives the exec that started it; its report, log and exit code land in the run directory, which is printed as the script's last line.
func Env ¶ added in v0.7.196
func Env(cred Credential, baseURL string) map[string]string
Env is the environment claude runs with on a sidecar: only the credential, and the base URL when it points somewhere other than Anthropic.
func IsPromptFile ¶ added in v0.7.195
IsPromptFile reports whether name has an extension that LoadPrompts reads.
func PoolSize ¶
PoolSize picks how many sidecars to boot: one per prompt, capped at the requested parallelism. Booting more sidecars than prompts only bills idle machines, since each review occupies exactly one sidecar.
func ReadScript ¶ added in v0.7.195
ReadScript builds the shell script that reports a detached run: a STATUS line first, then the report once the run has finished or the tail of its log while it is still going.
func WaitReady ¶
WaitReady blocks until the pool's background clone creation and sync have finished, via waitSynced (the pool's WaitSynced), and reports any member that failed.
sidecar.NewPool returns as soon as its members exist, while reused members may still be syncing in the background. Waiting here makes a failed sync surface before any review starts, rather than as one review failing partway through a pass. It holds no members while waiting: the pool reports a failed member through Acquire only once nothing is checked out.
Types ¶
type Credential ¶ added in v0.7.194
Credential is the Claude credential a review authenticates with. EnvVar is the variable claude reads it from, so only one of the two is ever sent and a stale one cannot shadow the other.
type DetachSpec ¶ added in v0.7.195
type DetachSpec struct {
RunID string
RepoPath string // checkout on the primary
OrgID string
PromptsDir string // relative to RepoPath; empty for the default
Parallelism int
Model string
Timeout time.Duration
Image string // snapshot for the primary's reviewer sidecars; empty for its config default
DestroyPool bool // delete the primary's reviewer sidecars when the review ends
// Install is a shell fragment that puts chunk at $HOME/chunk, run before
// the review starts. Empty when the binary is already there.
Install string
}
DetachSpec describes the review the primary sidecar runs.
func (DetachSpec) RunDir ¶ added in v0.7.195
func (s DetachSpec) RunDir() string
RunDir returns the run's directory as a path relative to the sidecar home.
type Execer ¶
type Execer func(ctx context.Context, entry *sidecar.PoolEntry, script string, env map[string]string, onOutput circleci.OutputFn, onSubmitted func(commandID string)) (exitCode int, err error)
Execer runs a shell script on a sidecar and streams its output. onSubmitted, when non-nil, is called with the command ID between submission and streaming.
type Finding ¶ added in v0.7.196
type Finding struct {
// ID is unique within a run. The daemon assigns it; a review's own output
// does not carry one.
ID string `json:"id,omitempty"`
// Prompt names the review that reported it.
Prompt string `json:"prompt,omitempty"`
// File is a repository-relative path with forward slashes.
File string `json:"file"`
// Line is the 1-based line the finding is about; zero means the file as a
// whole or a line the reviewer did not give.
Line int `json:"line,omitempty"`
Severity string `json:"severity"`
Body string `json:"body"`
// Patch is an optional unified diff that would fix the finding.
Patch string `json:"patch,omitempty"`
}
Finding is one issue a review reports, in a form a program can act on: where it is, how serious it is, what is wrong, and optionally how to fix it.
func DedupeFindings ¶ added in v0.7.196
DedupeFindings drops repeats: several reviews of the same change often flag the same line for the same reason, and it need not be fixed twice. The first copy's ID is kept, at the most serious severity any copy had.
func (Finding) WorthChanging ¶ added in v0.7.196
WorthChanging reports whether a finding is serious enough to act on: severity high or medium. Lower findings are kept on the record but never trigger a fix, so a loop does not churn the code over style remarks.
type Options ¶
type Options struct {
Credential Credential
// BaseURL is forwarded to claude when it is not Anthropic's own, so a
// credential issued by a gateway is sent to that gateway.
BaseURL string
Model string // optional; claude's default when empty
Timeout time.Duration // per review; DefaultTimeout when zero
StatusFn iostream.StatusFunc
ProgressFn func(ProgressEvent) // optional; called on each prompt state change
// StructuredFindings runs each review with FindingsSchema as its
// --json-schema, parses the findings into Result.Parsed and puts the prose
// in Result.Output. A review whose answer has no structured output fails.
// Off, Result.Parsed stays empty.
StructuredFindings bool
// AllowedTools overrides the read-only tool set. Empty means read-only, which
// is what every review uses.
AllowedTools []string
// OnSubmitted is called once per prompt with the remote command ID, as soon
// as the exec is accepted and before its output is streamed. It is how a
// caller registers the run for output replay, which has to happen while the
// command is still in flight.
OnSubmitted func(entry *sidecar.PoolEntry, prompt, commandID string)
}
Options configures one review pass.
type Parsed ¶ added in v0.7.196
type Parsed struct {
// Findings are the valid findings, in the order given, at most MaxFindings.
Findings []Finding
// Dropped counts entries that were present but unusable (no file, no body, a
// path outside the repository) or over the cap.
Dropped int
// Prose is the review's prose.
Prose string
}
Parsed is the result of reading structured findings out of a review's output.
func ParseFindings ¶ added in v0.7.196
ParseFindings reads the findings out of claude's JSON result. Claude Code has already checked the shape against FindingsSchema; what is left is the content, which is still untrusted: paths are confined to the repository and sizes are capped. A result with no structured output is an error, not a prose-only review.
type ProgressEvent ¶ added in v0.7.194
type ProgressEvent struct {
Prompt string
SidecarID string
State PromptState
Duration time.Duration
Error string
}
ProgressEvent reports a state change for one prompt.
type Prompt ¶
Prompt is one review to run. Name is the file name without its extension and identifies the review in output across passes.
func LoadPrompts ¶
LoadPrompts reads every prompt file directly inside dir, sorted by name so runs are reproducible. Subdirectories are not descended into, and files that are empty after trimming whitespace are skipped rather than run as blank reviews.
type PromptState ¶ added in v0.7.194
type PromptState int
PromptState is the lifecycle state of one review in a pass.
const ( StateQueued PromptState = iota // waiting for a sidecar StateRunning // executing on a sidecar StateDone // completed successfully StateFailed // completed with error )
Prompt lifecycle states.
type Result ¶
type Result struct {
Prompt string
SidecarID string
Output string
Error string
Duration time.Duration
// Parsed holds the structured findings when Options.StructuredFindings is
// set.
Parsed Parsed
}
Result is the outcome of one prompt in one pass. Output and Error are not exclusive: a review that fails partway keeps what it produced.
func RunPass ¶
func RunPass(ctx context.Context, acquire func(context.Context) (*sidecar.PoolEntry, error), release func(*sidecar.PoolEntry), exec Execer, prompts []Prompt, opts Options) ([]Result, error)
RunPass runs every prompt once, each on a sidecar checked out with acquire and returned with release (the pool's Acquire and Release), and returns results in prompt order for every prompt that started. Per-prompt failures are recorded in Result.Error; the returned error is for failures that stop the pass itself.
A missing claude binary or a rejected credential stops the pass rather than being recorded per prompt: every sidecar in a pool shares one image and one credential, so either would fail every review the same way.
type RunStatus ¶ added in v0.7.195
type RunStatus struct {
State string // RunRunning, RunDone, RunMissing or RunDied
ExitCode int // meaningful when State is RunDone
Body string // the report when done, the log tail when running
Log string // the log tail when done
}
RunStatus is a detached run as ReadScript reported it.