Code map

Package by package, with the one function or type worth opening first in each file. Paths are relative to ci_doctor/.

ci_doctor/ The wiring, and nothing else.
File Start here What it decides
cli/ main() The command line, one module per command and one package per group (`cli/registry/ping.py` is `ci-doctor registry ping`). Parses, calls `service/`, prints, and owns the exit code: 1 for a `SetupError`, 0 for everything else, a crash included.
service/ analyze_run() What each command does, with no terminal in it: `analyze` (over a live run or a log file, in two stages: diagnose is segment → assign phases → attribute → build bundle → brief and calls no model, explain is brief → report; the MCP server runs only the first), `deliver` (artifacts, MR note), `setup` (the dry run).
lib/ pattern_set() Shared low-level helpers. `regex.py`: RE2 for every regex a user or a matcher supplies, linear time by construction, so no pattern can hang a job; `pattern_set` matches a whole list in one pass per line. `files.py`: `ENCODING` (UTF-8, how every text file is read and written) and atomic writes. `edit/`: change one entry of a JSON, Markdown, TOML or YAML file and keep the rest, comments included.
agent/ build() A coding agent calling ci-doctor. `server.py` is the MCP server, a surface like `cli/`: each tool calls `service/` and returns, and nothing here calls a model. `harness/` is `ci-doctor agent install`: one module per coding agent in `targets/`.
errors.py SetupError The one error allowed to change the exit code. Raised only before analysis starts: a bad config, an empty include, a registry that cannot be read.
config/ What is tunable, and how the layers combine.
File Start here What it decides
schemas/ Config The pydantic model, one module per top-level key (`schemas/llm.py` is `llm:`). Source of truth for the published JSON Schema and for every scalar default. `extra="forbid"` — an unknown key in a config file is an error, not a shrug.
loader.py load_config() Applies the six layers in order, and resolves a relative registry path against the directory of the config file that names it.
edit.py edit_block() Rewrites one top-level block of a config file (`matchers:`), leaving the rest of it and its comments alone.
defaults.yml — The data-heavy baseline: phase map, noise patterns, and the address of the `official` registry. No matchers. Data, not code — which is why the docs can generate the phase table from it.
core/ The domain. Pure functions, no network, no clock, no provider ever named.
File Start here What it decides
models.py Job, Section, LogLine, Phase The vocabulary everything downstream speaks. A `Section` is a named region of a log with its own lines, children and a `closed` flag; that flag is what makes "died mid-step" detectable.
ports.py CIProvider, LogSegmenter The protocols a provider must satisfy. The entire reason `core/` can stay blind to which CI system produced a log.
phases.py assign_phases() Walks the section tree and stamps a `Phase` on each node from the config map. An unrecognised section inherits the nearest known phase, falling back to `script` — an unknown block is far more often your command than infrastructure.
attribution.py attribute() The classifier: a first-match-wins ladder of rules over job metadata and log structure. Returns the phase, the reason, a confidence, the single most telling line, and the `rule_id` that fired.
denoise.py denoise() Strip ANSI, collapse `\r` progress rewrites to their final state, optionally drop the timestamp prefix, drop configured noise, then fold runs of identical lines into one with a count.
evidence.py build_bundle() The orchestrator for the evidence: picks the last section carrying the blamed phase, then denoise → extract → fit into one `EvidenceBundle`.
redact.py redact_text() The secret scrubber. Run on the prompt and again on the finished report.
engine/ Matcher files and log lines in, evidence out. Pure, and layered: spec → compile → run → select, each importing only the ones below it; outside code imports only `ci_doctor.engine`.
File Start here What it decides
plan.py Plan The one internal form every schema version compiles to: every regex once, and each output's match tree and window over them. `run` and `select` only ever see a Plan, so a new format version never changes how matching runs.
spec/ load() What a matcher file may say, per schema version: `v1/` (outputs, variants, match nodes, windows). `load` picks the model by the file's `schema`; caps are in `limits.py`, roles and ranks in `roles.py`.
compile/ compile_plan() Spec → Plan. Expands templates and `define`s into RE2, checks each regex's cost and that no match fires on an empty line, and names the place in the file on every error.
run/windows.py run() Plan × lines → windows. One RE2 pass gives each leaf its lines (`scan.py`); `all`/`any`/`not` are set operations and relations are linear sweeps (`anchors.py`, `relations.py`); each anchor becomes a window with its output's rank.
select/evidence.py select() Adds the tail window, merges what overlaps (`merge.py`), sheds the lowest-ranked windows under budget pressure (`shed.py`), and renders the gaps as explicit elision markers (`render.py`).
select/fired.py fired() The outputs that anchor in the final evidence, in log order: what the prompt describes to the model.
select/budget.py fit() The last-resort cap: trims *inside* the surviving evidence, keeping the tail, and announces how much it dropped. Token counting is a 4-chars-per-token estimate — no tokenizer is loaded.
matchers/ What a matcher, bundle and index file is, and how a matcher is tested. Nothing outside `registry/` imports `registry/`.
File Start here What it decides
models.py Index, Bundle A registry's file formats: a matcher file, a bundle, and the `index.yaml` that lists both, with the path and size rules every entry must pass.
validate.py matcher_from() Reads a registry file safely (no YAML aliases, paths contained in the registry) and applies the rules a registry adds on top of the schema. Shared by load time and the author's checks, so they cannot drift.
verify.py verify() `matchers verify`: runs each output alone over its matcher's fixture logs, framing stripped as each log's name declares, and compares with the committed `.expected` files; also checks variant coverage, runner trailers and cross-fire. `--only` narrows it to named matchers or outputs.
examine.py examine() `matchers examine`: one matcher over one log, as `verify` reads it, each output's selection with its anchors marked.
matchers/registry/ Where matchers come from: registries, read on every run and validated like untrusted input, and building one.
File Start here What it decides
resolve.py load() Resolves `matchers.include` against each registry's index, expands bundles, fetches every selected matcher, validates it again and renames it `alias/id`. Any failure is a `SetupError`.
transports/ transport_for() How a registry's files are read, picked by the location's shape: `local` (a path or `file://`) or `http` (bounded redirects and sizes).
cache.py Cache The local copy of registry files, checked against each index's sha256, so a run works through an outage and `cache pull` can fill it ahead of time.
index.py write_index() `registry index`: validates every matcher and bundle (unique names, known includes, no cycles), generates one bundle per ecosystem folder, and writes a deterministic `index.yaml`.
mirror.py mirror() `registry mirror`: copies one whole registry into a folder, for an air-gapped install.
providers/ One directory per CI system. Adding a provider means adding a directory, never editing core.
File Start here What it decides
gitlab/provider.py GitLabProvider REST client: find the failed pipeline, list its jobs, pull the trace for the failed ones.
gitlab/segmenter.py GitLabSegmenter Reads GitLab's `section_start:…:step_script` markers into the Section tree.
gitlab/reasons.py — Maps GitLab's `failure_reason` strings onto the domain `FailureReason` enum.
github/segmenter.py GitHubSegmenter The harder half of the same job: GitHub group names embed action versions ("Run actions/checkout@v4"), so they are canonicalised to stable tokens here — which is what lets the phase map in `core/` stay provider-free.
git_origin.py — Infers the project from the checkout's git remote, so the CLI usually needs no `--project`.
llm/ The one optional model call. It explains why; it never decides where.
File Start here What it decides
report.py produce_report() Builds the report. With no model configured this is the whole output — a deterministic report, not a stub. It sets the category only when the provider reason settles it; a script failure's category is the model's job.
backends.py — One `PydanticAILLMClient` over any provider Pydantic AI ships, picked by `llm.backend`; `openai` alone chooses Chat Completions or Responses. Replies are validated against the report schema.
schema.py Report The report's shape: what a model must answer with, and what `report.json` holds. A reply that does not validate is discarded, not trusted.
render/ Presentation only — no decisions live here.
File Start here What it decides
terminal.py — The rich terminal view.
markdown.py — `report.md`, and the body of the MR/PR note.
pipeline.py triage_line() The run header and the code-vs-infra triage count above the jobs.

Configuration layering

Every knob in every one of those files is set by the same six-layer merge:

schema defaults  <  defaults.yml  <  user config  <  repo .ci-doctor.yml  <  CI_DOCTOR_* env  <  CLI flags

Mappings deep-merge, lists and scalars replace: a registry you add sits beside official, and your matchers.include replaces the empty default. See Configuration for the full reference.

Adding a CI provider is a directory, not a patch. Implement the protocols in core/ports.py, add a segmenter and a reason map, and extend the shipped phase map with your section names. Nothing in core/ changes — and CI greps to make sure of it.