sim

package
v0.175.0-rc.511 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 14 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 sim models the desk's flow of an issue from its start to its release as the supervisor loop is meant to run it, simulates that model, measures the real flow from the event log and compares the two: where the real system is slower than the model, what the log names as the cause, and which single change would gain the most released issues per day.

The model is deliberately small. An issue is started by a worker when a slot is free; the worker pushes a pull request; CI runs; a reviewer is started once CI is green; the reviewer fixes its own findings or asks for changes; the merge lane merges one pull request at a time; the release rolls out. The loop drives every transition but the worker's and the reviewer's own turns, so while the loop is down nothing starts, nothing is reviewed and nothing merges.

Index

Constants

View Source
const (
	VerdictOnTrack = "on track"
	VerdictAhead   = "ahead"
	VerdictBehind  = "behind"
	VerdictStuck   = "stuck as forecast"
	VerdictGone    = "gone"
)

Verdicts of a checked item.

View Source
const (
	ProblemResolved  = "resolved"
	ProblemStanding  = "standing"
	ProblemUnchecked = "unchecked"
)

Outcomes of a checked problem.

View Source
const (
	AgentBusy   = "busy"
	AgentParked = "parked"
	AgentIdle   = "idle"
)

States of an item's agent on the roster.

View Source
const (
	SeverityStop = "stop"
	SeveritySlow = "slow"
)
View Source
const (
	StageImplementing = state.DeliveryImplementing
	StageCI           = state.DeliveryCI
	StageReviewQueued = state.DeliveryReviewQueued
	StageReviewing    = state.DeliveryReviewing
	StageFixing       = state.DeliveryFixing
	StageMerging      = state.DeliveryMerging
	StageReleasing    = state.DeliveryReleasing
	// StageDecision is an issue parked on its person's answer, which the
	// delivery steps count inside implementing.
	StageDecision = "decision"
)

Stage names are the delivery steps of state.Delivery, so a model's stage compares with the step the loop records for a real delivery.

View Source
const BehindGrace = 10 * time.Minute

BehindGrace is how late an item may run before the check calls it behind: the loop's own pass between two polls.

View Source
const DefaultSigma = 0.8

DefaultSigma is the spread of every default stage: the 90th percentile is about 2.8 times the median, as the real steps show.

View Source
const LoopGap = 15 * time.Minute

LoopGap is the longest silence of the supervisor loop that still counts as up: the loop logs a claim or a stage on nearly every 30s pass.

View Source
const Seeds = 5

Seeds is how many seeds Compare averages the fitted runs over, so that the chance of one run never ranks a change below another.

View Source
const StepBoard = "board"

StepBoard is the step of an item still on the board.

Variables

Stages lists the steps in flow order.

Functions

func Verbs

func Verbs(verb string) bool

Verbs are the event verbs Observe reads, for the caller's filter.

Types

type BoardItem

type BoardItem struct {
	Ref    string `json:"ref"`
	Status string `json:"status,omitempty"`
	Skip   string `json:"skip,omitempty"`
}

BoardItem is a board candidate in the order board next walks them; Skip is the implementation's reason not to start it, "" when free.

type Checked

type Checked struct {
	ForecastAt time.Time        `json:"forecastAt"`
	At         time.Time        `json:"at"`
	Items      []CheckedItem    `json:"items"`
	Forecast   Tally            `json:"forecast"`
	Actual     Tally            `json:"actual"`
	Problems   []CheckedProblem `json:"problems,omitempty"`
	// Counts the items by verdict (on track, ahead, behind, ...).
	Counts map[string]int `json:"counts"`
}

Checked is a forecast against the desk at a later time.

func Check

func Check(f Forecast, now time.Time, items []Item, released, started []string) Checked

Check compares f with the desk at now: items are the active deliveries, released the refs whose delivery ended done since the forecast, started the refs whose delivery started since it.

func (Checked) Summary

func (c Checked) Summary() string

Summary is the one line of the check: how many items are where the forecast said, and the rest by verdict.

type CheckedItem

type CheckedItem struct {
	Ref string `json:"ref"`
	// Expected is the step the forecast had the item in at the check,
	// Actual the step it is in: done once released, gone when the loop
	// no longer records it.
	Expected string `json:"expected"`
	Actual   string `json:"actual"`
	Verdict  string `json:"verdict"`
	// Behind is how long ago the forecast had the item enter the first
	// step it has not reached, with the Behind verdict.
	Behind     time.Duration `json:"behind,omitempty"`
	Agent      string        `json:"agent,omitempty"`
	AgentState string        `json:"agentState,omitempty"`
	Waits      string        `json:"waits,omitempty"`
}

CheckedItem is one forecast item against the desk now.

type CheckedProblem

type CheckedProblem struct {
	Problem
	Outcome string `json:"outcome"`
	// Moved names the refs that left the step the forecast saw them in,
	// Still those that did not.
	Moved []string `json:"moved,omitempty"`
	Still []string `json:"still,omitempty"`
}

CheckedProblem is a forecast problem against the items it named.

type Comparison

type Comparison struct {
	Model    Model    `json:"model"`
	Expected Result   `json:"expected"`
	Observed Observed `json:"observed"`
	// Fitted is the model with the observed stage times, loop uptime,
	// drop share and start rate; Baseline its run, which should come
	// close to the observed throughput when the model holds.
	Fitted   Model  `json:"fitted"`
	Baseline Result `json:"baseline"`
	Rows     []Row  `json:"rows"`
	// WhatIfs are the single changes to the fitted model, by gain.
	WhatIfs []WhatIf `json:"whatIfs"`
	// Findings are the sentences a reader acts on, ranked.
	Findings []string `json:"findings"`
}

Comparison is the model against the log: per stage, the loop, the capacity, the throughput and the changes ranked by what they gain.

func Compare

func Compare(m Model, o Observed) Comparison

Compare runs the model, fits it to the observation, runs the fit and every single change back towards the model, and ranks the changes.

type Count

type Count struct {
	Name string `json:"name"`
	N    int    `json:"n"`
}

Count is a named tally.

type FailedMerge

type FailedMerge struct {
	Ref    string    `json:"ref"`
	At     time.Time `json:"at"`
	Exit   int       `json:"exit"`
	Reason string    `json:"reason,omitempty"`
}

FailedMerge is a merge the gate recorded as failed.

type Forecast

type Forecast struct {
	At       time.Time       `json:"at"`
	Horizon  time.Duration   `json:"horizon"`
	Model    Model           `json:"model"`
	Snapshot Snapshot        `json:"snapshot"`
	Items    []ItemForecast  `json:"items"`
	Starts   []StartForecast `json:"starts,omitempty"`
	Totals   []Total         `json:"totals"`
	Problems []Problem       `json:"problems,omitempty"`
}

Forecast is the desk's expected flow over a horizon from a snapshot.

func Forecasting

func Forecasting(m Model, s Snapshot, horizon time.Duration) Forecast

Forecasting runs the snapshot forward through the model over the horizon: the items in flight from their current steps, the free board items as the slots allow, under the implementation's own verdicts (loop running, room, holds, reviews already requested).

type Item

type Item struct {
	Ref   string    `json:"ref"`
	Pulls []string  `json:"pulls,omitempty"`
	Step  string    `json:"step"`
	Since time.Time `json:"since"`
	Agent string    `json:"agent,omitempty"`
	// AgentState is busy, parked, idle or "" for no agent on the roster.
	AgentState string `json:"agentState,omitempty"`
	Waits      string `json:"waits,omitempty"`
}

Item is one active delivery as the loop records it.

type ItemForecast

type ItemForecast struct {
	Ref   string    `json:"ref"`
	Step  string    `json:"step"`
	Since time.Time `json:"since"`
	// Timeline is every stage the item is forecast to enter within the
	// horizon, done last when it is released.
	Timeline []Transition `json:"timeline,omitempty"`
	// ReleaseAt is zero while the release lies past the horizon.
	ReleaseAt time.Time `json:"releaseAt,omitzero"`
	// Stuck says why the item is forecast not to move at all.
	Stuck string `json:"stuck,omitempty"`
}

ItemForecast is one item's forecast path.

type LaneState

type LaneState struct {
	Lane  string    `json:"lane"`
	Ref   string    `json:"ref"`
	Phase string    `json:"phase"`
	Since time.Time `json:"since"`
}

LaneState is a merge in a lane.

type Model

type Model struct {
	// Stages maps a stage name to its service time. review-queued is the
	// time the loop takes to start a reviewer once CI is green, merging the
	// gate's own run (devctl pr merge with its merge-result check), and
	// releasing the release workflow with the rollout.
	Stages map[string]Stage `json:"stages"`
	// Workers is the ceiling of busy agents (capacity.ceiling): workers,
	// fixers and reviewers share it; reviewers may exceed it by
	// ReviewHeadroom (capacity.reviewHeadroom).
	Workers        int `json:"workers"`
	ReviewHeadroom int `json:"reviewHeadroom"`
	// Lanes is how many repositories merge in parallel: one merge at a
	// time per lane.
	Lanes int `json:"lanes"`
	// LoopUptime is the share of time the supervisor loop runs, 0 to 1,
	// and LoopOutage the mean length of one down period.
	LoopUptime float64       `json:"loopUptime"`
	LoopOutage time.Duration `json:"loopOutage"`
	// RedCI is the chance a CI run is red and goes back to a fixer;
	// ChangesRequested the chance a review asks for changes instead of
	// fixing on the branch; Dropped the chance a started issue ends
	// without a pull request (research, closed unmerged, overlap).
	RedCI            float64 `json:"redCI"`
	ChangesRequested float64 `json:"changesRequested"`
	Dropped          float64 `json:"dropped"`
	// UpdateBranch adds a CI run to every merge, as devctl pr merge
	// --update-branch does when the base moved.
	UpdateBranch bool `json:"updateBranch"`
	// DecisionShare is the share of issues that wait for the person's
	// answer once, for the decision stage's service time.
	DecisionShare float64 `json:"decisionShare"`
	// Backlog is how many issues wait on the board at the start, and
	// ArrivalsPerDay how many are filed per day on top. A negative
	// backlog never runs out.
	Backlog        int     `json:"backlog"`
	ArrivalsPerDay float64 `json:"arrivalsPerDay"`
	// Horizon is the simulated time, Seed the random seed.
	Horizon time.Duration `json:"horizon"`
	Seed    int64         `json:"seed"`
}

Model is the flow with its capacities, service times and chances.

func Default

func Default() Model

Default is the intended flow: the loop up, the stage times a worker, a reviewer, CI and the gate take when nothing waits, the chances the week's log shows for red CI and change requests, and no drops.

func Fit

func Fit(m Model, o Observed) Model

Fit returns m with the observed values: the stages' medians, the loop's uptime, the share of dropped work and the rate at which issues were started, so that a run of it replays the week as the log shows it.

func FitStages

func FitStages(m Model, o Observed) Model

FitStages returns m with the log's stage medians alone, where the log has at least three residences of a stage; the capacities, chances and the board stay as they were.

func (Model) With

func (m Model) With(stage string, median time.Duration) Model

With returns m with the stage's median changed.

type Observed

type Observed struct {
	Outcomes map[string]int `json:"outcomes"`
	Since    time.Time      `json:"since"`
	Until    time.Time      `json:"until"`
	Days     float64        `json:"days"`
	// Stages are the delivery steps' residence times (delivery.step).
	Stages []ObservedStage `json:"stages"`
	// Deliveries counts the issues observed, Started those whose worker
	// started in the window, Released those that reached done in it and
	// Dropped those that ended in it without a merge; DropShare is
	// Dropped over both.
	Deliveries     int     `json:"deliveries"`
	Started        int     `json:"started"`
	Released       int     `json:"released"`
	Dropped        int     `json:"dropped"`
	DropShare      float64 `json:"dropShare"`
	StartedPerDay  float64 `json:"startedPerDay"`
	ReleasedPerDay float64 `json:"releasedPerDay"`
	// LoopUptime is the share of the window with a supervisor loop event
	// at least every LoopGap; Outages are the longest gaps.
	LoopUptime float64       `json:"loopUptime"`
	LoopDown   time.Duration `json:"loopDown"`
	Outages    []Outage      `json:"outages,omitempty"`
	// MeanBusy and MeanCeiling are the busy agents and the ceiling across
	// the subscription providers, time-weighted from the USAGE lines.
	MeanBusy    float64       `json:"meanBusy"`
	MeanCeiling float64       `json:"meanCeiling"`
	Utilization float64       `json:"utilization"`
	Providers   []ProviderUse `json:"providers,omitempty"`
	// Starts count agents.start events by kind.
	WorkerStarts int `json:"workerStarts"`
	ReviewStarts int `json:"reviewStarts"`
	// Failures are the supervisor.failure events by step and reason,
	// MergeFailures the merge.failed events by exit code, Parks the
	// agents.park events by what they wait on, Rollbacks the release
	// rollbacks.
	Failures      []Count `json:"failures,omitempty"`
	MergeFailures []Count `json:"mergeFailures,omitempty"`
	Parks         []Count `json:"parks,omitempty"`
	Rollbacks     int     `json:"rollbacks"`
	// RateLimited counts events that name an exhausted GitHub API limit.
	RateLimited int `json:"rateLimited"`
	// Lanes counts the merge lanes that saw a merge.
	Lanes int `json:"lanes"`
}

Observed is the real flow read from the event log.

func Observe

func Observe(events []state.Event, since, until time.Time) Observed

Observe measures the window since..until from the events (any order, any verbs: it keeps what it needs).

type ObservedStage

type ObservedStage struct {
	Stage     string    `json:"stage"`
	Residence Quantiles `json:"residence"`
}

ObservedStage is a delivery step's residence time over the window.

type Outage

type Outage struct {
	From time.Time `json:"from"`
	To   time.Time `json:"to"`
}

Outage is a period without any supervisor loop event.

type ParkedAgent

type ParkedAgent struct {
	Name  string `json:"name"`
	On    string `json:"on,omitempty"`
	Waits string `json:"waits"`
}

ParkedAgent is an agent kept parked and what it waits on.

type Problem

type Problem struct {
	Severity string   `json:"severity"`
	What     string   `json:"what"`
	Refs     []string `json:"refs,omitempty"`
}

Problem is what the snapshot says will not move, or moves too slowly, before any simulation: Stop stops the flow, Slow slows it.

type ProviderRoom

type ProviderRoom struct {
	Name    string `json:"name"`
	Busy    int    `json:"busy"`
	Ceiling int    `json:"ceiling"`
	Room    int    `json:"room"`
}

ProviderRoom is a provider's busy agents, ceiling and room now.

type ProviderUse

type ProviderUse struct {
	Provider    string  `json:"provider"`
	MeanBusy    float64 `json:"meanBusy"`
	MeanCeiling float64 `json:"meanCeiling"`
}

ProviderUse is a provider's mean busy agents against its mean ceiling over the window, from the watch's USAGE lines.

type Quantiles

type Quantiles struct {
	Count  int           `json:"count"`
	Median time.Duration `json:"median"`
	P90    time.Duration `json:"p90"`
	Max    time.Duration `json:"max"`
}

Quantiles summarise a set of durations.

type Result

type Result struct {
	Horizon        time.Duration `json:"horizon"`
	Started        int           `json:"started"`
	Released       int           `json:"released"`
	Dropped        int           `json:"dropped"`
	Open           int           `json:"open"`
	StartedPerDay  float64       `json:"startedPerDay"`
	ReleasedPerDay float64       `json:"releasedPerDay"`
	// Cycle is the time from a worker's start to the release.
	Cycle Quantiles `json:"cycle"`
	// BacklogWait is the time an issue waited on the board for a worker.
	BacklogWait Quantiles     `json:"backlogWait"`
	Stages      []StageResult `json:"stages"`
	// WorkerUtilization is the busy slot time over the slots (Workers plus
	// ReviewHeadroom) × Horizon, LaneUtilization the merging time over
	// Lanes × Horizon.
	WorkerUtilization float64       `json:"workerUtilization"`
	LaneUtilization   float64       `json:"laneUtilization"`
	LoopDown          time.Duration `json:"loopDown"`
}

Result is what a run of the model yields.

func Run

func Run(m Model) Result

Run simulates the model over its horizon. The same model and seed give the same result.

type Row

type Row struct {
	Stage    string    `json:"stage"`
	Expected Quantiles `json:"expected"`
	Observed Quantiles `json:"observed"`
	// Gap is the observed median over the expected one.
	Gap time.Duration `json:"gap"`
	// Causes are what the log names around this stage, most frequent first.
	Causes []string `json:"causes,omitempty"`
}

Row compares one stage: the model's residence against the log's.

type Snapshot

type Snapshot struct {
	At          time.Time `json:"at"`
	LoopRunning bool      `json:"loopRunning"`
	// Stopped names a standing stop or starts hold.
	Stopped string `json:"stopped,omitempty"`
	// Room and Blocks are capacity's verdict for a worker start.
	Room int `json:"room"`
	// StartBlocked excludes temporary occupancy of worker slots.
	StartBlocked bool              `json:"startBlocked,omitempty"`
	MergeBlocks  map[string]string `json:"mergeBlocks,omitempty"`
	Blocks       []string          `json:"blocks,omitempty"`
	Busy         int               `json:"busy"`
	Parked       int               `json:"parked"`
	Idle         int               `json:"idle"`
	Providers    []ProviderRoom    `json:"providers,omitempty"`
	// Reviewers names the busy review agents.
	Reviewers      []string              `json:"reviewers,omitempty"`
	Items          []Item                `json:"items"`
	Board          []BoardItem           `json:"board,omitempty"`
	BoardRead      bool                  `json:"boardRead"`
	Holds          []string              `json:"holds,omitempty"`
	Lanes          []LaneState           `json:"lanes,omitempty"`
	ReviewRequests []state.ReviewRequest `json:"reviewRequests,omitempty"`
	FailedMerges   []FailedMerge         `json:"failedMerges,omitempty"`
	ParkedAgents   []ParkedAgent         `json:"parkedAgents,omitempty"`
	Timers         []int                 `json:"timers,omitempty"`
	// UnmergedLimit is supervisor.unmergedLimit, for the record: board
	// next applies it and holds the items past it.
	UnmergedLimit int `json:"unmergedLimit"`
}

Snapshot is the desk now, as the forecast reads it from the state and the implementation's own verdicts (capacity, board next).

func (Snapshot) Free

func (s Snapshot) Free() []string

Free is the board items the loop would start, in the board's order: not held by the board, and not in delivery already.

type Stage

type Stage struct {
	Median time.Duration `json:"median"`
	Sigma  float64       `json:"sigma"`
}

Stage is one step's service time: a lognormal distribution around Median with the spread Sigma (0 makes every sample the median).

type StageResult

type StageResult struct {
	Stage     string    `json:"stage"`
	Wait      Quantiles `json:"wait"`
	Service   Quantiles `json:"service"`
	Residence Quantiles `json:"residence"`
}

StageResult is one stage's figures: Wait is the time an issue waited for the loop or a resource before the stage, Service the stage's own time and Residence both, which is what a delivery.step event measures.

type StartForecast

type StartForecast struct {
	Ref string    `json:"ref"`
	At  time.Time `json:"at"`
}

StartForecast is a board item forecast to start.

type Tally

type Tally struct {
	Started  int `json:"started"`
	Released int `json:"released"`
}

Tally counts the starts and releases up to the check.

type Total

type Total struct {
	After    time.Duration `json:"after"`
	Started  int           `json:"started"`
	Released int           `json:"released"`
}

Total is the forecast count after a time.

type Transition

type Transition struct {
	Step string    `json:"step"`
	At   time.Time `json:"at"`
}

Transition is a stage an item is forecast to reach at a time.

type WhatIf

type WhatIf struct {
	Change         string        `json:"change"`
	ReleasedPerDay float64       `json:"releasedPerDay"`
	Gain           float64       `json:"gain"`
	Cycle          time.Duration `json:"cycle"`
}

WhatIf is the model with the observed values and one change undone.

Jump to

Keyboard shortcuts

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