watchd

package
v0.7.186 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 35 Imported by: 0

Documentation

Overview

Package watchd implements the chunk watch background daemon and its client.

Index

Constants

View Source
const (
	// ConflictInterval is how often the daemon re-evaluates whether each
	// project's branch still merges cleanly. Far slower than PollInterval
	// because the answer only changes when a commit lands on either side, and
	// the check costs a merge in the object database rather than a stat.
	ConflictInterval = 60 * time.Second

	// FetchInterval is how often the merge target's remote-tracking ref is
	// refreshed. The check is worthless against a ref nobody has updated in a
	// week, and expensive if refreshed every time: this is the compromise.
	FetchInterval = 3 * time.Minute

	// MaxConflictPaths caps the paths reported for one branch. A merge that
	// conflicts in hundreds of files is one fact — "this branch has diverged
	// badly" — and listing all of them buries it.
	MaxConflictPaths = 20
)
View Source
const (
	// PollInterval is how often the daemon refreshes project state from disk.
	PollInterval = 5 * time.Second
	// RecentEvents is the maximum number of events kept per project.
	RecentEvents = 300
	// RunningTimeout is how long after the last non-terminal event a sidecar is
	// considered to still be running.
	RunningTimeout = 5 * time.Minute
)
View Source
const (
	// MaxCommandBytes is the default per-command output buffer cap in bytes.
	// Override at runtime with CHUNK_OUTPUT_BUFFER_SIZE so the cap can be tuned
	// via a sandbox-provisioner deploy without rebuilding images.
	MaxCommandBytes = 10 << 20 // 10 MB

	// MaxCommands caps retained commands per project. Only finished commands are
	// evicted, so a project running more than this many at once keeps them all.
	MaxCommands = 20
)
View Source
const (
	// SampleInterval is how often the remote sampler emits a reading.
	SampleInterval = 2 * time.Second

	// StaleSamples is how many intervals a sample may age before the dashboard
	// should treat it as stale. A sampler that dies must look stalled rather than
	// look like an idle sidecar, so the last value is kept and marked, not
	// discarded.
	//
	// The budget has to cover more than SampleInterval: the reading crosses an SSH
	// connection before the daemon sees it, and the dashboard then renders that
	// same snapshot until its next 5s poll while re-evaluating the age every
	// frame. Measured against real sidecars, a healthy sampler reaches ~10s of
	// apparent age just before a poll lands, so a tighter bound flags working
	// samplers as stale for part of every cycle. Twelve seconds clears that and
	// still catches a dead sampler within two polls.
	StaleSamples = 6
)
View Source
const (
	BandLow    = "low"
	BandMedium = "medium"
	BandHigh   = "high"
)

Risk score bands. They exist so a caller can act on the score without hard-coding a number, and so the number itself stays arguable: a band is a claim about caution, not a probability of failure.

View Source
const DefaultAsyncMaxLines = 500

DefaultAsyncMaxLines is how large a change may be, in lines, and still be validated in the background.

The number is a judgement, not a measurement. What it is really choosing is how much of the inner loop runs without waiting: nearly every edit an agent makes in one turn lands under it, and the changes that do not are the ones where a developer is most likely to want the answer before doing anything else. Projects that disagree can say so — see config.AsyncValidateMaxLines.

View Source
const MaxTasksPerProject = 20

MaxTasksPerProject caps retained validation tasks per project. Only finished tasks are evicted, so a project cannot lose a run that is still going. It is also what bounds delivered results, which collect keeps rather than deletes.

View Source
const PRPollInterval = 60 * time.Second

PRPollInterval is the minimum time between PR fetches for a given branch. GitHub's API is rate-limited, so polling too frequently is wasteful. A branch change resets the clock so the new branch's PR is fetched promptly.

Variables

View Source
var ErrAsyncRefused = errors.New("async validation refused")

ErrAsyncRefused is returned by StartAsyncValidate when the daemon will not take this run asynchronously: the tree could not be fingerprinted, so a stale result would be undetectable, or the project already has its cap of runs in flight. Callers fall back to running inline, where the answer reaches whoever asked for it while it is still true. The daemon's reason is wrapped alongside the sentinel.

View Source
var ErrDaemonTimeout = errors.New("the watch daemon did not answer in time")

ErrDaemonTimeout reports that a daemon was there but did not answer within conflictTimeout.

Kept apart from ErrDaemonUnreachable because the two call for different advice: one means start the daemon, the other means it is already running and busy, so asking again is what helps. Collapsing them told people with a working daemon to go start one.

View Source
var ErrDaemonUnavailable = errors.New("daemon unavailable")

ErrDaemonUnavailable is returned by RunValidate when the daemon socket is unreachable, so callers can distinguish a transient connectivity failure from a real validation error and fall back to inline execution.

View Source
var ErrDaemonUnreachable = errors.New("no watch daemon is running")

ErrDaemonUnreachable reports that no watch daemon answered.

Callers on the hook path are expected to treat this as "nothing to say" and carry on. The daemon is optional — it runs when the developer has `chunk watch` open — and a hook that complains about its absence would fire on every session end for everyone who does not.

Functions

func BuildID added in v0.7.164

func BuildID() string

BuildID identifies the binary a process was started from.

The daemon serves snapshots shaped by the code it was started from: a field added to SidecarState since then is absent rather than wrong, so a newer client renders a well-formed view of stale data with nothing to say why. A sidecar owned by a session, for instance, arrives from a pre-session daemon looking like a sidecar nobody owns. Comparing this on every ping is what makes that visible instead of silent.

The version alone will not do: every local build reports the same development version, so the executable's path, size and modification time come along to tell two of them apart. Path is included so a dev build and an installed one are never mistaken for each other.

func ConflictNotice added in v0.7.186

func ConflictNotice(rep ConflictReport) string

ConflictNotice renders the advisory an agent should be told about, or "" when there is nothing to advise.

Empty is the common case and the important one: no conflict, no answer yet, no daemon, a branch that is itself the merge target — all of them produce no text at all. An advisory that speaks when it has nothing to say trains the reader to skip it, which costs it the one time it matters.

The wording states plainly that this is not a gate. The agent reading it is the same one that treats a failed `chunk validate` as work to do before it can continue, and a notice that reads like a check would divert it into a rebase in the middle of an unrelated task.

func ConflictStatus added in v0.7.186

func ConflictStatus(rep ConflictReport) string

ConflictStatus renders the full state for someone who ran the command by hand, including every reason ConflictNotice stays silent about.

The two differ on purpose. A person typing `chunk conflicts` and getting no output cannot tell a clean merge from a daemon that is not running, and both answers change what they do next; an agent being handed the same distinction mid-task gains nothing from it.

func EnsureDir

func EnsureDir() (string, error)

EnsureDir creates ~/.chunk/watchd/ if it doesn't exist and returns the path.

func EnsureLaunched added in v0.7.164

func EnsureLaunched(subArgs []string) error

EnsureLaunched starts the daemon when nothing is answering and otherwise leaves whatever is there alone.

Unlike EnsureRunning it never replaces a daemon from another build. It is called when a poll fails mid-session, and a dashboard that has been open for a while has no business restarting a daemon another one is using: the build check is a startup decision, made once, where the cost of being wrong is one restart rather than a restart per poll for as long as two dashboards are open.

func EnsureRunning

func EnsureRunning(subArgs []string) error

EnsureRunning checks whether the watch daemon is running and serving, and launches it if not. subArgs are the CLI arguments used to invoke the daemon (e.g. ["watch", "_daemon"]).

func IsDaemonCompatible added in v0.7.175

func IsDaemonCompatible() bool

IsDaemonCompatible reports whether the watch daemon is reachable and running the same build as the current process. A daemon from a different build may not support all API endpoints (e.g. /validate), so delegation should be skipped and the operation run inline instead.

func IsDaemonRunning added in v0.7.174

func IsDaemonRunning() bool

IsDaemonRunning reports whether the watch daemon is reachable and was built from the same binary as the caller. A daemon from an older build is treated as absent: it may not serve routes added since it was compiled.

func IsRunning

func IsRunning(path string) (bool, int, error)

IsRunning reports whether the process whose PID is stored in path is alive. Returns (false, 0, nil) when the file doesn't exist.

func LogPath

func LogPath() (string, error)

LogPath returns the path to the daemon log file.

func PIDPath

func PIDPath() (string, error)

PIDPath returns the path to the daemon PID file.

func RegisterCommand added in v0.7.173

func RegisterCommand(reg CommandReg)

RegisterCommand tells the running watch daemon to stream and buffer a command's output.

It is best-effort by design and reports no error. If the daemon is not running, the command still runs and still streams to the caller's own stdout; the only thing lost is the buffered copy. Notably this does not start the daemon: spawning a background process as a side effect of a hook firing is intrusive, and a hook that hangs waiting for a daemon launch is a far worse failure than a missing logs pane.

func RunDaemon

func RunDaemon(ctx context.Context, client *circleci.Client, authMessage string, runner ValidateRunner, ghClient *github.Client) error

RunDaemon is the watch daemon entry point, called by the hidden _daemon subcommand.

client and authMessage support the output-buffering feature; runner is called in-process to handle /validate requests. Both client and runner may be nil (the daemon still records commands without a client, and /validate returns an error without a runner). ghClient may be nil; PR monitoring is skipped when no GitHub credentials are available.

func SocketPath

func SocketPath() (string, error)

SocketPath returns the path to the daemon Unix socket.

func StartAsyncValidate added in v0.7.186

func StartAsyncValidate(projectRoot string, args []string, circleCIToken string) (string, error)

StartAsyncValidate asks the daemon to validate projectRoot in the background and returns the new task's ID without waiting for the run.

func StopForCredentialChange added in v0.7.173

func StopForCredentialChange()

StopForCredentialChange stops a running watch daemon so that the next launch picks up newly stored credentials.

The daemon resolves its CircleCI client once, at startup, so one that started before a login holds a nil client for the rest of its life and streams no output however many times the developer retries. Stopping it here is what makes `chunk auth login` take effect: a `chunk watch` already on screen relaunches it through EnsureLaunched on its next poll, and otherwise the next `chunk watch` starts a daemon that can authenticate.

Best-effort and silent, like RegisterCommand. Failing to stop the daemon must not fail a login that has otherwise succeeded, and the cost of not stopping it is the buffered output of a daemon that was not streaming anything anyway.

Types

type AsyncValidateResponse added in v0.7.186

type AsyncValidateResponse struct {
	TaskID string `json:"task_id"`
}

AsyncValidateResponse acknowledges an accepted async run.

type CollectResponse added in v0.7.186

type CollectResponse struct {
	Tasks []TaskState `json:"tasks"`
}

CollectResponse carries the finished results for a project.

type CommandReg added in v0.7.173

type CommandReg struct {
	CommandID   string    `json:"command_id"`
	SidecarID   string    `json:"sidecar_id"`
	ProjectRoot string    `json:"project_root"`
	Op          string    `json:"op"`
	Name        string    `json:"name"`
	SubmittedAt time.Time `json:"submitted_at"`
}

CommandReg is the registration a process sends after submitting a remote command, so the daemon can stream and buffer that command's output. The submitting process may exit immediately afterwards — that is the whole point, since most remote commands are run by a hook that exits as soon as the command finishes.

type CommandState added in v0.7.173

type CommandState struct {
	CommandID   string     `json:"command_id"`
	SidecarID   string     `json:"sidecar_id"`
	Op          string     `json:"op"`
	Name        string     `json:"name"`
	SubmittedAt time.Time  `json:"submitted_at"`
	EndedAt     *time.Time `json:"ended_at,omitempty"`
	ExitCode    *int       `json:"exit_code,omitempty"`
	Running     bool       `json:"running"`
	Bytes       int64      `json:"bytes"`
	Truncated   bool       `json:"truncated"`
}

CommandState describes one remote command the daemon is buffering output for.

type ConflictReport added in v0.7.186

type ConflictReport struct {
	Root string `json:"root"`
	// Conflict is nil when the daemon has no answer for this root — either it
	// does not know the project, or no check has run yet.
	Conflict *ConflictState `json:"conflict,omitempty"`
	// Known reports whether the daemon is tracking the root at all. Without it
	// "unknown project" and "checked, nothing to report" are the same response.
	Known bool `json:"known"`
}

ConflictReport is the response to a conflict query for one project root.

func FetchConflicts added in v0.7.186

func FetchConflicts(root string) (ConflictReport, error)

FetchConflicts asks the running daemon whether root's branch still merges cleanly into its merge target.

Unlike RegisterCommand this reports its errors, because the caller decides how loudly to fail: a hook stays quiet, a person running the command by hand gets told why there is no answer.

type ConflictState added in v0.7.186

type ConflictState struct {
	// Branch is the branch that was compared, empty when there was none.
	Branch string `json:"branch,omitempty"`
	// Target is the merge target, qualified as the remote names it —
	// "origin/main".
	Target string `json:"target,omitempty"`
	// HeadSHA and TargetSHA are the two commits actually merged. They are what
	// makes a result reusable: neither side moving means the merge would
	// resolve identically.
	HeadSHA   string `json:"head_sha,omitempty"`
	TargetSHA string `json:"target_sha,omitempty"`
	// Conflicted reports that the merge does not resolve automatically. False
	// with an empty Unavailable is a real all-clear; false with Unavailable set
	// means no merge was attempted.
	Conflicted bool `json:"conflicted"`
	// Paths lists the conflicted paths, capped at MaxConflictPaths.
	Paths []string `json:"paths,omitempty"`
	// TotalPaths is how many paths conflicted in total, which exceeds len(Paths)
	// when the list was cut.
	TotalPaths int `json:"total_paths,omitempty"`
	// CheckedAt is when the answer was produced, TargetFetchedAt when the
	// target's remote-tracking ref was last refreshed. The second is the one
	// that decides how much the answer is worth.
	CheckedAt       time.Time `json:"checked_at"`
	TargetFetchedAt time.Time `json:"target_fetched_at"`
	// TargetStale reports that the last refresh of the target ref failed, so
	// the comparison ran against whatever was already on disk. The answer may
	// simply be out of date, which is worth saying rather than implying.
	TargetStale bool `json:"target_stale,omitempty"`
	// Unavailable explains why there is no answer, and is empty when there is
	// one. A detached HEAD, a repo with no recorded default branch, and a
	// branch that is itself the merge target all land here — none of them are
	// faults, and all of them would otherwise look like "no conflicts".
	Unavailable string `json:"unavailable,omitempty"`
}

ConflictState is the daemon's answer to whether one project's branch still merges cleanly into its merge target.

It describes committed history only. The preview merges HEAD against the target, so uncommitted work in the tree is invisible to it: a conflict that exists solely in unstaged edits is not reported here, and anything presenting this to a person has to say so rather than let the silence read as an all-clear.

type OutputChunk added in v0.7.173

type OutputChunk struct {
	// Data is raw command output, exactly as the remote command wrote it —
	// interleaved stdout and stderr, ANSI and carriage returns intact.
	Data []byte `json:"data"`
	// NextOffset is the offset to pass on the following read.
	NextOffset int64 `json:"next_offset"`
	Running    bool  `json:"running"`
	ExitCode   *int  `json:"exit_code,omitempty"`
	// Truncated reports that output before the returned data was evicted and is
	// gone. Saying so is the difference between showing a partial run and
	// showing a partial run that looks whole.
	Truncated bool `json:"truncated"`
	// Found is false when the daemon knows nothing about the command.
	Found bool `json:"found"`
	// Error explains why streaming stopped early, when it did. Without it a
	// failed stream is indistinguishable from a command that produced no output,
	// which sends the reader looking for a bug in their own command.
	Error string `json:"error,omitempty"`
}

OutputChunk is one response to an output read.

func FetchOutput added in v0.7.173

func FetchOutput(commandID string, offset int64) (OutputChunk, error)

FetchOutput reads buffered output for a command starting at offset.

type PRCheck added in v0.7.186

type PRCheck struct {
	Name       string `json:"name"`
	Status     string `json:"status"`     // QUEUED, IN_PROGRESS, COMPLETED
	Conclusion string `json:"conclusion"` // SUCCESS, FAILURE, NEUTRAL, CANCELLED, etc.
}

PRCheck is one CI check on a PR's latest commit.

type PRComment added in v0.7.186

type PRComment struct {
	Author    string    `json:"author"`
	Body      string    `json:"body"`
	CreatedAt time.Time `json:"created_at"`
	Resolved  bool      `json:"resolved,omitempty"`
}

PRComment is one review thread comment on a PR.

type PRState added in v0.7.186

type PRState struct {
	Number           int         `json:"number"`
	Title            string      `json:"title"`
	URL              string      `json:"url"`
	UpdatedAt        time.Time   `json:"updated_at,omitempty"`
	CheckState       string      `json:"check_state,omitempty"` // rollup: SUCCESS, FAILURE, PENDING, ERROR, EXPECTED
	Checks           []PRCheck   `json:"checks,omitempty"`
	Comments         []PRComment `json:"comments,omitempty"`
	ChangesRequested bool        `json:"changes_requested,omitempty"`
	FetchedAt        time.Time   `json:"fetched_at"`
}

PRState is the daemon's advisory view of the open PR for a project's current branch. It is nil when no open PR exists for the branch or when GitHub credentials are absent.

type ProjectSnapshot

type ProjectSnapshot struct {
	Root     string           `json:"root"`
	Branch   string           `json:"branch"`
	HeadRef  string           `json:"head_ref"`
	RepoName string           `json:"repo_name"`
	Sidecars []SidecarState   `json:"sidecars"`
	Events   []eventlog.Event `json:"events"`
	Commands []CommandState   `json:"commands,omitempty"`
	// Conflict is nil until the first conflict check for this project has run.
	// Nil is "not known yet", distinct from a ConflictState reporting no
	// conflict, and the two must not be collapsed by a reader.
	Conflict *ConflictState `json:"conflict,omitempty"`
	// PR is the advisory PR state for the current branch. Nil when no open PR
	// exists, or when GitHub credentials are not configured.
	PR *PRState `json:"pr,omitempty"`
}

ProjectSnapshot is the daemon's view of one project at a point in time.

type Resources added in v0.7.174

type Resources struct {
	CPUPercent     float64   `json:"cpu_percent"`
	MemUsedBytes   int64     `json:"mem_used_bytes"`
	MemLimitBytes  int64     `json:"mem_limit_bytes"`
	DiskUsedBytes  int64     `json:"disk_used_bytes"`
	DiskTotalBytes int64     `json:"disk_total_bytes"`
	SampledAt      time.Time `json:"sampled_at"`
}

Resources is one sample of a sidecar's resource usage.

Memory is reported as used-of-limit rather than a percentage so the display can show both, and because a limit of zero (unknown) has to be distinguishable from a usage of zero.

type RiskSummary added in v0.7.186

type RiskSummary struct {
	// Score is 0–100. Higher means more caution: bigger, broader, or already
	// failing. It does not decide anything on its own; see decideRisk.
	Score int `json:"score"`
	// Lines and Files are what was measured, kept apart from the score so a
	// caller can compare two changes without unpicking one.
	Lines int `json:"lines"`
	Files int `json:"files"`
	// Inert reports that every changed path was docs or text.
	Inert bool `json:"inert,omitempty"`
	// Band is Score bucketed into BandLow, BandMedium or BandHigh.
	Band string `json:"band"`
	// Parts are the facts behind the score, each with what it contributed.
	Parts []string `json:"parts,omitempty"`
	// Advice is what a developer or agent could do about it, or "" when there is
	// nothing useful to say.
	Advice string `json:"advice,omitempty"`
}

RiskSummary is what the daemon made of a change, as reported to a caller.

It is deliberately not just a number. A score on its own cannot be argued with — "risk 62" invites either trust or dismissal and supports neither — so the facts it was built from travel with it, and the advice that follows from them is spelled out rather than left to be inferred.

func (RiskSummary) String added in v0.7.186

func (r RiskSummary) String() string

String renders a summary as one line of terminal output.

type SidecarState

type SidecarState struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	// SessionID is the agent session that owns this sidecar, empty for state
	// written outside a session or before sessions existed. Sidecars are
	// isolated per session, so two entries for one project and branch are two
	// sessions working in the same tree — this is what tells them apart.
	SessionID    string    `json:"session_id,omitempty"`
	ProjectName  string    `json:"project_name"`
	RepoName     string    `json:"repo_name"`
	SnapshotName string    `json:"snapshot_name"`
	FileMtime    time.Time `json:"file_mtime"`
	// Workspace is the sidecar-side repo path, used to sample disk usage where
	// the work actually happens rather than wherever a shell starts.
	Workspace    string      `json:"workspace,omitempty"`
	LastActivity time.Time   `json:"last_activity"`
	LastOp       eventlog.Op `json:"last_op"`
	LastLevel    string      `json:"last_level"`
	Running      bool        `json:"running"`
	// Resources is the most recent resource sample, or nil when none has
	// arrived — sampling only runs while a dashboard is attached.
	Resources *Resources `json:"resources,omitempty"`
}

SidecarState describes one active sidecar as maintained by the daemon.

type Snapshot

type Snapshot struct {
	Projects []ProjectSnapshot `json:"projects"`
	// AuthError explains why output streaming is unavailable, when it is. An
	// empty logs pane with no explanation sends people hunting the wrong fault,
	// so the daemon reports this rather than silently serving nothing.
	AuthError string `json:"auth_error,omitempty"`
}

Snapshot is a point-in-time view of all watched projects.

func FetchSnapshot

func FetchSnapshot(roots []string) (Snapshot, error)

FetchSnapshot connects to the running watch daemon and returns the current snapshot for the given project roots. If roots is empty all known projects are returned.

type TaskState added in v0.7.186

type TaskState struct {
	ID string `json:"id"`
	// ProjectRoot is the key the task is filed under — the root of the repo,
	// which may sit above the directory the run was actually asked for. See
	// taskStore.projectKey.
	ProjectRoot string    `json:"project_root"`
	StartedAt   time.Time `json:"started_at"`
	FinishedAt  time.Time `json:"finished_at,omitempty"`
	Running     bool      `json:"running"`
	// ExitCode is the validate run's exit status. Meaningful only once the task
	// has finished.
	ExitCode int `json:"exit_code"`
	// Output is what the run printed, held for whoever collects the result.
	// Capped at maxTaskOutput, keeping the tail.
	Output string `json:"output,omitempty"`
	// Stale reports that the working tree changed between the run starting and
	// its result being read, so the result describes code that is no longer on
	// disk. For a live-tree run ExitCode and Output are cleared when it is set: a
	// stale task is reported so that the discard is visible, and carrying the
	// verdict would invite it to be read as one. For a snapshot run they are
	// kept — see Snapshot, where the verdict outlives the tree moving.
	//
	// The tree most often moved because of the run itself — output written,
	// goldens regenerated, a lockfile touched. Reporting the discard is what
	// lets that be diagnosed instead of looking like no run happened.
	//
	// It is not a fix for the cause. A mid-run edit by the developer and a file
	// written by the run are the same event as far as a content digest is
	// concerned, so nothing here can tell them apart, and relaxing the
	// comparison to let artifacts through would let a real edit through with
	// them — trading a silence for a false pass, which is the one direction this
	// store must not fail in. Actually keeping the result means stopping the
	// tree from moving under the run, which is a matter of where the run
	// happens rather than how its result is judged.
	Stale bool `json:"stale"`
	// Snapshot reports that the run validated a checked-out copy of the tree
	// rather than the tree itself. Such a result is exact about the state it
	// ran against whatever happened afterwards, so it is reported even when
	// stale — qualified rather than thrown away.
	Snapshot bool `json:"snapshot,omitempty"`
	// DeliveredAt records when this result was handed to a caller. A stamped
	// task is never reported again; an unstamped one is still owed to somebody.
	//
	// It exists so that handing a result over and forgetting it are two steps
	// rather than one. See taskStore.collect.
	DeliveredAt time.Time `json:"delivered_at,omitempty"`
}

TaskState is a validation task as reported to a caller.

func CollectValidateResults added in v0.7.186

func CollectValidateResults(projectRoot string) ([]TaskState, error)

CollectValidateResults returns the finished async results for projectRoot and clears them from the daemon, so a result is reported once and not repeated.

Best-effort: with no daemon running there is nothing to collect and nothing to report, which is not an error worth surfacing on a hook path.

func (TaskState) Passed added in v0.7.186

func (t TaskState) Passed() bool

Passed reports whether a finished task validated the tree successfully.

A stale live-tree task never passes, whatever its exit code says. Its verdict is stripped when it is reported, which leaves ExitCode at zero — so without the Stale check a discarded run would read here as a clean pass, which is exactly the claim discarding it exists to avoid making.

A stale snapshot task can still pass. It ran against a copy that could not move, so its exit code remains exactly true about the state it was handed; what staleness says there is that the state has been overtaken, not that the verdict is unreliable. Callers are expected to report that qualification — see printResults.

type ValidateRequest added in v0.7.174

type ValidateRequest struct {
	// Args is os.Args[1:] from the caller, e.g. ["validate", "test", "--remote"].
	Args []string `json:"args"`
	// CircleCIToken is forwarded to the subprocess as CIRCLE_TOKEN.
	CircleCIToken string `json:"circleci_token,omitempty"`
	// Env is the caller's os.Environ(), forwarded verbatim to the subprocess so
	// session-identity variables (e.g. CLAUDE_CODE_SESSION_ID) reach it intact.
	Env []string `json:"env,omitempty"`
	// ProjectRoot is the repo the run applies to, already resolved by the client
	// (so it reflects the caller's --project, or its cwd). It decides what gets
	// validated, and it is also what a task is filed and fingerprinted under,
	// what a change is measured against, and what a verdict is remembered by —
	// so anything the daemon decides on a project's behalf needs it.
	//
	// It cannot be left to the daemon to infer. The daemon's own cwd is wherever
	// it was launched from, which is one arbitrary repo out of all the repos it
	// serves, so a run that resolved the project itself would validate that one
	// and report the answer under whichever project asked. Empty is accepted on
	// the synchronous path only, for a client too old to send it; such a run is
	// simply run, with nothing judged or recorded.
	ProjectRoot string `json:"project_root,omitempty"`
	// AllowAsync says the caller will accept being released before the answer
	// exists. It is an offer, not an instruction: the daemon weighs the change
	// and may hold the caller anyway.
	//
	// Only a caller that has somewhere to hear the answer later should set it.
	// A hook does — the next turn collects background results — while a developer
	// watching a terminal does not, and releasing them would leave the run's
	// output going nowhere they are looking.
	AllowAsync bool `json:"allow_async,omitempty"`
}

ValidateRequest is the payload sent to POST /validate and POST /validate/async.

type ValidateResponse added in v0.7.174

type ValidateResponse struct {
	ExitCode int    `json:"exit_code"`
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	// TaskID is set when the daemon took the run into the background rather than
	// running it here. Nothing has run yet: ExitCode is zero because there is no
	// exit code, and the result is collected on a later turn.
	TaskID string `json:"task_id,omitempty"`
	// Risk is what the daemon made of the change: a score, the facts behind it,
	// and any advice. Nil when the caller never offered to be released, since
	// then no change was judged.
	Risk *RiskSummary `json:"risk,omitempty"`
	// Reason says why the run was released or held, in words a caller can print
	// as one line. Empty when the caller never offered to be released, since
	// then there was no decision to explain.
	Reason string `json:"reason,omitempty"`
}

ValidateResponse is the response from POST /validate.

func RunValidate added in v0.7.174

func RunValidate(req ValidateRequest) (ValidateResponse, error)

RunValidate delegates a validate run to the daemon. req.ProjectRoot is the repo to validate, already resolved by the caller.

req.Env is filled from the caller's environment when it is empty, since the point of forwarding it is to carry this process's identity into the run. Set req.AllowAsync to offer the daemon the option of releasing this caller and reporting later; a response carrying a TaskID is that offer taken, and means nothing has run yet.

It takes the request type rather than a list of arguments because what the daemon needs to know about a run keeps growing, and every addition would otherwise be another positional parameter at two call sites.

type ValidateRunner added in v0.7.174

type ValidateRunner func(ctx context.Context, projectRoot string, args []string, env []string, stdout, stderr io.Writer) int

ValidateRunner runs a validate command in-process. projectRoot is the repo the run applies to; args is os.Args[1:] from the caller (e.g. ["validate", "test", "--remote"]); env is the caller's os.Environ(), which may differ from the daemon's own environment. stdout and stderr capture the command output. Returns the exit code.

projectRoot is a parameter rather than something the runner works out for itself because the daemon has no working directory worth trusting. One daemon serves every repo on the machine — the socket is user-global — and it was launched with whatever cwd the developer happened to be in at the time. An implementation that resolves the project from its own cwd validates that repo no matter which one the request was about.

Jump to

Keyboard shortcuts

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