Inner loop vs. outer loop
Two deployment archetypes: IDE Copilots serve the inner loop (a developer's edit-compile-test cycle), while Pipeline agents operate autonomously in the outer loop (CI/CD) — generating tests, repairing builds, and proposing patches. Real systems: Cognition's Devin (a planner-coder-critic architecture for multi-file tasks), Princeton's SWE-Agent resolving real GitHub issues on the SWE-bench benchmark. A key safeguard against hallucination is repository-grounded reasoning — anchoring the agent in actual repo context.
Test-Driven Generation (TDG)
TDG adapts test-driven development for agents: the test suite is written first and acts as a contract — output isn't “done” until it passes. The three-phase loop uses distinct specialized agents:
Tester agent
Turns an ambiguous requirement into a concrete, machine-readable test suite (pytest/Jest). Run now, the tests fail — proving they're active and the feature doesn't exist yet.
Developer agent
Writes the minimal code to make the tests pass, grounded in repository context. Structured test feedback shows exactly which assertions failed, driving iteration.
Refactoring agent
Removes duplication, extracts functions, applies style conventions — running the full suite after every change to guard correctness.
Orchestrated with LangGraph, whose visible flow, checkpointing, and conditional edges map onto the generate-test-refine decision points.
Governance as continuous enforcement
These embed security & policy directly into the workflow — moving governance from post-deployment audit to a continuous scan → evaluate → remediate loop that parallels TDG (policy violations trigger remediation just as test failures trigger regeneration). Three architectural layers:
- Policy engine
- Declarative rules (Rego / Open Policy Agent) return pass/fail — the “test suite” for compliance; each version tracked to a regulation.
- Static code analyzers
- SAST tools detect known vulnerability signatures (hardcoded credentials, SQL injection) and feed structured data to the policy engine.
- Language model layer
- Interprets ambiguous cases, translates policy docs into rules, and turns cryptic failures into developer-friendly remediation.
Beyond static checks it does semantic code understanding (context-dependent violations), generates remediation patches, performs data-flow analysis, and produces an audit trail. Policies evolve dynamically — including incremental learning from overrides when humans grant exceptions, reducing false positives.
A closed-loop control system
These run a broader execute → observe → learn → adapt loop — a closed-loop control system converging reinforcement learning, control theory, and observability. It monitors its own actions and modifies its reasoning without human micromanagement.
Key concept · Sensing layer & agent roles
The sensing layer gathers three feedback types: explicit (human ratings/corrections), implicit (behavioral signals — refinement iterations, abandonment), and synthetic (automated consistency checks vs. gold standards). Three roles then coordinate: the coder agent produces artifacts, the critic agent evaluates them against KPIs (Task Completion Rate, Error Recovery Ratio, Latency Distribution, User Satisfaction Index, Improvement Velocity), and the planner agent synthesizes that feedback into optimization strategies.
Chapter 9 quiz
Fifteen questions across the three software-agent classes and their feedback loops. Answer first, then expand Show answer.
Part A · Multiple choice
In Test-Driven Generation, what happens in the Red phase?
- A The developer agent writes code to pass tests
- B The tester agent writes a test suite that initially fails
- C The refactoring agent cleans up code
- D The critic agent evaluates KPIs
Show answer
B. The tester agent converts the requirement into a test suite; running it fails, proving the tests are active and the feature doesn't exist yet.
Which feedback loop characterizes Compliance-Driven agents?
- A Generate, test, refine
- B Scan, evaluate, remediate
- C Execute, observe, learn, adapt
- D Sense, model, plan, act
Show answer
B — scan, evaluate, remediate. It embeds policy enforcement into the workflow as a continuous, automated process.
In a Compliance-Driven agent, the policy engine written in Rego (Open Policy Agent) plays the role analogous to what in TDG?
- A The developer agent
- B The refactoring step
- C The test suite
- D The IDE copilot
Show answer
C — the test suite. The policy engine returns pass/fail decisions, acting as the governing signal just as tests do in TDG.
Which feedback type comes from behavioral signals like the number of refinement iterations before tests pass?
- A Explicit feedback
- B Implicit feedback
- C Synthetic feedback
- D Static feedback
Show answer
B — implicit feedback. Explicit = direct human ratings; synthetic = automated checks vs. gold standards.
Which agent role in the self-improvement loop evaluates output against KPIs like Task Completion Rate and Error Recovery Ratio?
- A Coder agent
- B Critic agent
- C Planner agent
- D Tester agent
Show answer
B — the critic agent. The planner then synthesizes the critic's feedback into optimization strategies.
Part B · True or false
In TDG, the test suite is written after the implementation code.
Show answer
False. Tests come first and act as a contract; code is written to satisfy them.
Repository-grounded reasoning helps combat hallucinations in code-generation agents.
Show answer
True. Anchoring the agent in actual repository context keeps output aligned with existing APIs and patterns.
In TDG, the test suite is re-run after every refactoring change.
Show answer
True. Running tests after each change guards correctness while code quality is improved.
IDE Copilots operate in the CI/CD “outer loop,” while Pipeline agents serve the developer's “inner loop.”
Show answer
False. It's the reverse — IDE Copilots serve the inner (edit-compile-test) loop; Pipeline agents operate autonomously in the outer CI/CD loop.
Compliance-Driven agents can learn from human overrides to reduce false positives over time.
Show answer
True. Incremental learning from accepted/rejected exceptions refines enforcement while maintaining security.
Part C · Short answer
Name the three agent classes and each one's characteristic feedback loop.
Show answer
Code-Generation → generate, test, refine. Compliance-Driven → scan, evaluate, remediate. Self-Improving → execute, observe, learn, adapt.
Name the three phases of TDG and which agent leads each.
Show answer
Red (tester agent — write failing tests), Green (developer agent — minimal code to pass), Refactor (refactoring agent — optimize, re-run tests).
Name the three architectural layers of a Compliance-Driven agent.
Show answer
Policy engine (Rego/OPA, pass-fail), static code analyzers (SAST signatures), and the language model layer (interprets ambiguity, translates policy, writes remediation).
Name the three feedback sources in a Self-Improving agent's sensing layer.
Show answer
Explicit (human ratings/corrections), implicit (behavioral indicators), and synthetic (automated consistency checks vs. gold standards).
What are the three coordinating agent roles in the self-improvement loop, and what does each do?
Show answer
Coder agent produces solutions with current strategies; critic agent evaluates them against quality KPIs; planner agent synthesizes the feedback and determines optimization strategies (e.g. add more try-catch examples to the coder's prompt).