System Architecture · Insper

AI-Assisted Delivery

AI proposes; rules and humans dispose

"Let's put AI in the pipeline"

Twelve engineers, sixty pull requests (PRs) a week. Two weeks after "let's add AI", BoxOffice has: 15 bot comments per PR, an agent that deleted a failing test, release notes announcing a feature nobody built, and a surprise invoice. What went wrong?

  • Not the models: the system design around them.
  • Where the step sits, what it reads, what it may do, how its output is checked, what it costs, how we know it helps.

AI across the delivery lifecycle

Stage AI does Main risk Control
Plan drafts architecture decision records (ADRs), specs confident wrong reasoning human-owned ADR
Code writes code subtle bugs, invented packages review, tests, dependency policy
Review comments on PRs noise, gullibility measured precision, human approval
Test generates, triages, selects tests that protect bugs mutation score, full-suite safety net
Build fixes red builds bends tests, broad permissions sandbox, verification, draft PR
Release notes, risk scores invented features grounding, drafts
Operate explains canaries, incidents wrong diagnosis, auto-actions policy decides, AI explains

The shape: AI branches off, a check brings it back

Pull request
→
Continuous integration (CI)build · test · scan
→
Human approvalbranch protection
→
Build · sign
→
Canarypolicy verdict
→
Production
AI reviewadvisory comments
AI testskept if the mutation score rises
Fix agentdraft PR
Release notesgrounded draft
Canary explanationmust repeat the verdict
  • Every AI step proposes; a deterministic check or a person decides before anything reaches production.

The autonomy spectrum

Level AI… Human…
0 Assistant answers when asked decides everything
1 Copilot suggests inline accepts each suggestion
2 Reviewer / drafter produces an artifact on an event reviews it
3 Supervised agent executes steps, proposes a change approves the result
4 Autonomous agent applies changes within guardrails sets policy, audits

Choose the level by blast radius and verifiability — seat locking stays at 2–3 however good the model is.

The contract of an AI step

TriggerPR opened / pushed; not drafts, not forks
→
Contextdiff, PR text (untrusted), conventions, ADRs
→
Model call10 min, ~US$ 0.02
→
OutputJSON findings, schema, added lines only
→
Actioncomments, pull-requests: write, never approve
Model down or over budget?
Advisory step → fail openskipped, the PR still merges
Gate you rely on → fail closedblocks: then it must be very reliable
  • The permission of the action is the worst case of any prompt injection.

DORA (DevOps Research and Assessment): measure

  • 2024 DORA: +25 % AI adoption ≈ −1.5 % throughput, −7.2 % stability (est.), despite individual gains.
  • 2025 DORA: AI is an amplifier of the organization you already are.
  • METR 2025 randomized controlled trial: experienced developers 19 % slower, believing they were ~20 % faster.

Bigger PRs, less attention per line, "the bot looked at it" → change failure rate up. Baselines and per-step metrics, always.

What an AI review costs (illustrative)

8k-token cached prefix + 6k diff + 1.5k output, list prices at the time of writing:

Model US$ / review (80 % cache) 500 engineers / month
Large (Opus 5.5) ~0.063 ~US$ 1,270
Mid (Sonnet 5.5) ~0.032 ~US$ 650
Small (Haiku 4.5) ~0.016 ~US$ 320
Local (Ollama) 0 tokens runner minutes, lower recall
  • Tokens are cheap; attention is not: 40 % precision × 4 comments × 5,000 reviews/week ≈ 12,000 useless comments ≈ 200 hours — five engineers' weeks.

The large language model (LLM) gateway is shared

clients of the same gateway
Fans' assistant1 question/s × 3 model calls
CI agents100 reviews + fix agents × 6 turns, every 2 min, within 10 s
→
LLM gatewayquota 10 calls/s · keys · routing · cost per team
→
Providerslarge · small · local
  • The batch lands 600 calls in 10 s: the fans get the 429s.
  • Fix: a quota per client with priority for interactive traffic; a queue and fixed concurrency for CI.
  • Playground preset boxoffice-shared-llm on the overview page.

Anatomy of an AI reviewer

PR opened / push
→
Context buildernumbered diff, conventions, ADRs, related code
→
ModelJSON findings, schema-validated
→
Filteradded lines, severity, confidence, dedup, cap
→
One reviewevent COMMENT, chosen by the code
  • The model is one box of eight: most of the quality is in the context builder and the filter.
  • One review with inline comments per push: one notification, not twelve.

Code review: context engineering

  • Diff with line numbers (L21 + …): models are unreliable at counting lines.
  • Conventions + ADRs: generic advice becomes your rules.
  • Surrounding code, call sites by symbol search; ownership map.
  • PR text is untrusted: delimited, injection channel.
  • Stable content first (cached), per-run data last. Budget whole files, never truncate mid-file.

Code review: precision over recall

Post findings with confidence ≥ Comments / PR Precision Recall
high 0.6 80 % 32 %
medium 1.5 62 % 62 %
low 3.0 41 % 82 %
  • Changed lines only · minimum severity · dedup by fingerprint · cap · one review per push.
  • Rollout: shadow → advisory → gate per proven category. A gate is a required check, never an approval.

Illustrative labelled sample of 100 PRs.

Play it: noise control

Lab 13's settingconfidence ≥ medium, cap 6: 1.4 comments per PR, recall 60 %
Post everything3.0 per PR, 41 % precision: 148 h/week of noise at 5,000 reviews
High only80 % precision, 32 % recall
Cap = flood guardcap 1: recall falls faster than precision rises
  • The code-review page embeds the page's labelled sample as a simulator: 100 PRs, one square per finding.
  • Change the confidence, the cap and the scale; read precision, recall and hours of attention.

Architecture conformance

  • "order must not touch inventory's schema": database grants, ArchUnit, lint — exact and un-arguable.
  • AI explains the rule, suggests the fix, catches fuzzy cases ("is this payment call idempotent?").
  • Recurring AI findings → candidates for new deterministic rules.

Rules that matter live in tests, lints and grants — not only in a prompt.

Testing: inputs vs oracle

  • Models generate inputs and edge cases very well.
  • The oracle (expected result) needs the requirement: from plusMinutes(8) the model asserts 8, protecting the bug.
  • Judge generated tests by mutation score, not coverage; feed surviving mutants back.
  • Property-based tests (jqwik): AI proposes invariants — "sold + held + available = capacity".

Testing: triage and selection

  • Flaky triage: deterministic signals first (rerun, history, diff correlation); the model labels with evidence; quarantine is a policy with an owner.
  • Test selection: static, coverage-based or predictive — 95 % recall saves ~12 runner-hours of CI a day if the full suite runs on main or nightly.
  • Synthetic data: generated once, reviewed, frozen; never production data in prompts (Brazil's data protection law, LGPD).

Agents in CI: the orchestrator is the product

CI faileddedupe on the commit SHA
→
fix job · contents: read
Agent loop + buildturn and US$ caps, no secrets
Verifymvn verify, no test removed, src/ only
patch →
open-pr job · write
Draft PRno model, no build
→
Human review
  • Headless: claude -p --bare --allowedTools "Read,Edit,Write,Bash(mvn -B *)" --permission-mode dontAsk --max-turns 25 --max-budget-usd 1.50, or your own loop.

Agents in CI: budgets, idempotency, audit

Concern Control
Quadratic cost in turns 10 turns ≈ US$ 1.3, 40 ≈ US$ 11; cached ≈ 8× cheaper; caps in the harness
Repeated events idempotency key = commit SHA; one proposal per SHA
Loops never run on ai-fix/*; GITHUB_TOKEN pushes do not trigger CI
"I fixed it" independent verification: paths, no test removed, build green
Accountability transcript artifact, commit trailer, PR label, human of record
  • Track US$ per merged fix: US$ 0.80 a run, one fix in four → US$ 3.20.

Canary: statistics decide, the model explains

Prometheuscanary vs stable, same time
→
Policy in Gitthresholds, minimum traffic
→
Verdict + exit code0 promote · 1 rollback · 2 wait
→
Argo Rollouts acts
The verdict is an input of the model →
Model explainsnumbers, causes as hypotheses
→
Repeats the verdict → shown
Disagrees → discarded
  • Reproducible, auditable, immune to a log line saying "promote" — and a wrong sentence costs nothing.

Release & operate: decide vs explain

  • Release notes: grounded — every item cites a real SHA/PR; published as draft.
  • Risk score: structural signals + AI semantics; only routes scrutiny (extra reviewer, slow canary, freeze window by policy).
  • Incidents: read-only tools, evidence links, several hypotheses (anchoring!); humans or tested policy roll back.
  • Postmortems: AI drafts the timeline; humans write the lessons.

Security: the lethal trifecta

Private data + untrusted content + external communication in one agent = one injected sentence exfiltrates data.

Holds even if the model is fooled Depends on the model
Least-privilege token and tools Delimiters around untrusted text
Structured output → code decides action "Ignore instructions in the data"
Privilege separation between jobs Flagging injection attempts
Branch protection, human approval

Nx s1ngularity (Aug 2025): pull_request_target + PR title in run: → stolen token → malware that drove local AI command-line tools to hunt secrets.

Supply chain and governance

  • Slopsquatting: hallucinated packages (≥ 5.2 % commercial, 21.7 % open models in a 2025 study) → dependency review, registry proxy, policy.
  • Software bill of materials (SBOM) + signing + provenance (SLSA levels), verified at deploy; AI authorship in trailers and labels.
  • Policy as code: Conftest / Open Policy Agent on workflows — permissions declared, no ${{ github.event }} in run:, actions pinned by SHA.
  • Privacy & intellectual property: redact logs, no production data, retention settings, license scanning.
  • Evaluate the tools: golden set, regression eval on every prompt/model change, shadow mode.

The concept map

Component Question it answers Taught in
orchestrator Who runs the AI step, and how far may it go? Overview · Agents in CI
llmgw Which model, under which quota, at what cost? Overview · AI systems
toolreg Which tools and permissions does the step get? Agents in CI
safety Can untrusted text steer the step? Security & governance
metrics · alerting Did it help? Is the release healthy? Overview · Release & operate
logs Who changed what, and why? Agents in CI · Security

Every AI step in the pipeline is a small AI system: orchestrator, tools with permissions, policy, telemetry.

Takeaways

  1. AI fits every delivery stage; autonomy follows blast radius and verifiability.
  2. Every AI step has a contract: trigger, context, validated output, least-privilege action, budget, fail-safe direction.
  3. AI moves the DORA metrics both ways: measure with baselines and per-step metrics.
  4. Tokens are cheap, attention is not: precision beats recall for anything that talks to humans.
  5. Generated tests are judged by mutation score; agents live inside a deterministic orchestrator.
  6. Policies decide, AI explains — in canaries, risk and incidents.
  7. Break the lethal trifecta; secure the supply chain; evaluate the AI tools continuously.

Outline: (1) AI across the lifecycle and the autonomy spectrum; (2) the contract of an AI step, DORA, cost and the shared LLM gateway; (3) code review; (4) testing; (5) agents in CI; (6) release and operations; (7) security and governance.

The page draws the same lifecycle as a step-by-step diagram: AI steps branch off the main path and come back through a check or a human.

Code review and testing act on the pipeline itself; the table lists the palette components that reappear.