Skip to content
Yassir Kachri

Agentic Software Factory

Spec-Anchored Engineering: Writing Down Intent Before an Agent Writes Code

Coding agents build what is written and infer the rest. How a versioned, executable spec, a code-owned Spec Gate and a deterministic citation check make intent an artifact that every later control can check against.

Yassir Kachri Updated 10 min read

Every control in an agent-assisted delivery flow compares a change against something. A hook compares an edit with a list of protected paths, a critic compares a diff with expected behavior, and the person who accepts the change compares the evidence with what the business asked for. When the expected behavior exists only in a ticket thread or in one engineer’s memory, those comparisons have nothing to check against, and whatever the agent inferred from the ticket is what gets built.

This gap is intent debt: behavior that nobody wrote down or approved. A rounding rule that lives only in one engineer’s memory is an example. The agent cannot follow it and the reviewer cannot check it, so the defect surfaces in production as a rework deployment. Agents add to this debt as fast as they produce code, and nothing in their output marks which parts were inferred.

Spec-anchored engineering pays the debt down before any agent starts. Intent becomes an artifact: versioned in the repository it governs, approved by a named owner and executable as tests. Every later control then checks the agent’s work against it.

A ticket records a change; the spec records the system

A ticket describes a difference: lower this limit, add that field, round this fee another way. A difference only has meaning against a description of the current state, and in most organizations that description is spread across code, old tickets, wiki pages and people. The reference design in my book separates the two records. Durable state lives in specs/<domain>.spec.md, in the repository whose behavior it describes. Transient deltas stay in the ticket system, and a delta that changes behavior is recorded as a change to the spec.

Exhibit 1 Three properties separate a spec an agent can be held to from ordinary documentation
PropertyIn practiceWithout it
DurableLives in the repository, versioned with the code it governs and changed through the same pull requestsIntent disappears with the ticket, the sprint or the engineer
ExecutableBusiness rules written as scenarios with an identifier, inputs and an exact expected result, run as testsNo test can assert prose, so a rule without a scenario fails no build
ApprovedThe domain’s Change Owner approves each change at a Spec Gate before implementation startsThe agent settles the ambiguity itself, and nothing in the diff shows that it did

Source: Author's reference design, The Enterprise Agentic Software Factory.

A spec file has two parts. The Blueprint is a short list of positive architectural statements about the domain. The contracts are scenarios written in Gherkin, each with an identifier. A fee calculation in a settlement service shows both:

# Settlement: Cross-Currency Fees

## Blueprint
- Only src/settlement/fees/ computes fees.
- Amounts are integer minor units. No floats.
- Convert first, then compute the fee, then round once.
- Round half-even to the settlement currency's minor unit.

## Contract FEE-014: fee rounding after conversion
Feature: Cross-currency transfer fees
  Scenario: Fee rounds half-even after conversion
    Given a transfer of 1000.00 EUR settled in USD
    And an FX rate of 1.0100 USD per EUR
    And a fee rate of 0.25 percent
    When the settlement fee is computed
    Then the fee is 2.52 USD

The values are illustrative and chosen to sit on a tie. The converted amount is 1,000.00 × 1.0100 = 1,010.00 USD, and the fee at 0.25% is 2.525 USD. Half-even rounding gives 2.52 and half-up gives 2.53. A one-cent difference is easy to miss in a large diff and hard to miss in a scenario that names the expected value.

The Blueprint statements are positive on purpose. “Only src/settlement/fees/ computes fees” can be checked with a search, by a person or by a critic. “Avoid scattered fee logic” cannot be checked by anyone, so it adds words to the agent’s context without adding a control. The scenario text is also kept in one place: a make bdd target extracts each contract block from the spec into a feature file at test time, so the scenario that runs is the one the Change Owner approved.

I keep specs in the repository they govern. There they share the code’s pull requests, ownership rules and audit trail, and an agent in a CI job can read them from its checkout without holding a connector credential. The trade-off is that business analysts must work in Git or through a tool that writes to it.

The Spec Gate is a code-owned pull request

The Spec Gate is a pull request that changes specs/ and, where a scenario needs new test code, tests/contracts/. The domain’s Change Owner is the required approver, and no agent starts implementation until it merges. The enforcement uses ordinary Git host features. On GitHub, “Code owners are automatically requested for review when someone opens a pull request that modifies code that they own,” and the branch protection option “Require review from Code Owners” makes their approval mandatory. An excerpt of the reference CODEOWNERS file:

# Merge Gate: every change, REVIEW.md included.
*                    @acme/settlement-change-owners

# Intent: the durable spec and its executable contracts.
/specs/              @acme/settlement-change-owners
/tests/contracts/    @acme/settlement-change-owners

# Guardrails, the jobs that judge a change, the build
# targets those jobs run, and ownership itself.
/.claude/            @acme/factory-platform
/.github/            @acme/factory-platform
/Makefile            @acme/factory-platform
/.github/CODEOWNERS  @acme/settlement-change-owners

Two documented behaviors matter here. Pull requests “use the version of CODEOWNERS from the base branch of the pull request,” so a branch cannot rewrite its own reviewers. And “If any line in your CODEOWNERS file contains invalid syntax, that line will be skipped.” A typo on the catch-all line therefore removes the owner from every path it covered, and pull requests keep merging without any error. GitHub lists those errors through its REST API, and I would have the platform team read the list daily and treat any entry as a broken gate.

Three design choices define how the gate works.

  1. The spec change and its implementation go in separate pull requests. A spec change bundled with its code is approved after the work is done, which turns the gate back into ordinary code review. The cost is a second pull request for every behavior change. Changes that alter no behavior, such as a dependency update, carry a no-behavior-change label instead.
  2. New scenarios merge tagged @pending. A scenario for behavior the code does not have yet would fail the required tests. The implementation pull request must pass exactly the scenarios it names, and pending scenarios stay out of the regression run until a follow-up Spec Gate pull request removes the tag after the implementation merges. The trade-off is a window in which an approved scenario sits outside the regression suite, so removing the tag belongs to the Change Owner who accepted the implementation.
  3. An agent may draft the spec change; a person writes the test code. An agent can read the ticket, compare it with the current spec and propose Blueprint lines and scenarios for the Change Owner to review. The workbench hook refuses agent edits to tests/contracts/, because the test that judges an agent’s work should not be written by an agent working from the same instructions. That split costs engineering time on every new contract.

At the gate, the Change Owner checks business meaning: whether the change says what the business asked for, whether each scenario names concrete inputs and an exact result, and whether the ticket implies behavior that is still left to inference. A spec change for one behavior is typically a few Blueprint lines and one or two scenarios, so a person’s closest reading goes to the smaller artifact, and the Merge Gate reviews the larger implementation against evidence.

Exhibit 2 An approved spec change is the reference every later check uses, and missing intent always returns to the same gate

A ticket delta, read over MCP with per-user OAuth, becomes a Spec Gate pull request that changes the specs and contracts; the Change Owner approves it as code owner. An implementation pull request, built by the agent against the approved spec, cites it in a Spec-Gate line. Required checks follow: spec-trace confirms the citation points to a merged spec change, the named scenarios must pass, and the critic compares the diff with the spec. The Merge Gate, where the Change Owner accepts the evidence, comes last. Two return paths lead back to the Spec Gate: a SPEC-QUESTION from an interactive session or a CI job, and a SPEC-GAP verdict or a broken contract from the checks.

Source: Author's diagram of the intent plane in The Enterprise Agentic Software Factory.

A deterministic check makes the citation binding

The implementation side of the gate starts in the repository’s instruction file, which every agent session loads:

## Source of truth
- Behavior lives in specs/settlement.spec.md.
- A ticket is a delta. If ticket and spec disagree, stop and
  ask for a spec change. Never infer a business rule.
- To ask, start your reply with a first line that begins
  `SPEC-QUESTION:` and stop. Hooks and CI read that line.
- Every behavior change cites its merged Spec Gate PR in the
  PR body as `Spec-Gate: #<number> <scenario IDs>`, or carries
  the no-behavior-change label. No approved delta, no code.

An instruction file is context, and context alone does not bind an agent, so the reference workflow adds a deterministic check. A spec-trace job that calls no model fails any pull request that touches src/ unless its body carries a Spec-Gate: line pointing to a merged pull request that changed specs/ and listing every contract that pull request added, or unless the pull request carries the no-behavior-change label. The job reads the added contracts from the cited pull request’s own diff, so the list does not rest on the agent’s word. It cannot tell whether the label is honest, which is why the critic also checks for behavior the spec does not describe.

Agents read tickets through managed, per-user connectors

Agents still need ticket context, and they read it through the Model Context Protocol. The MCP authorization specification makes authorization optional, so an enterprise has to require it. When it is used, servers “MUST validate that access tokens were issued specifically for them as the intended audience,” while implementations using the local stdio transport should instead “retrieve credentials from the environment,” which in practice means a token stored on the laptop. The reference configuration therefore lists remote HTTP servers only, with per-user OAuth and read-only scopes, so each ticket read is attributable to a named user and limited to that user’s access. In Claude Code, a managed-mcp.json file sets that list centrally, and with it deployed “Users can’t add, modify, or use any other MCP servers,” apart from a few exceptions the documentation names and the platform team has to review.

Ticket content needs separate treatment from ticket access. Claude Code’s documentation warns that “Servers that fetch external content can expose you to prompt injection risk,” and a ticket can be written by anyone with access to the ticket system, including customers. For that reason the reference CI job gives implementation agents no ticket connector: they read the approved spec from their checkout. The Spec Gate, where a person turns ticket text into approved intent, limits how far injected text can travel, although a reviewer of an agent-drafted spec change can still miss an injected instruction.

When intent is missing, work returns to the Spec Gate

Missing intent is a routine event with a defined route, and every route ends at the Spec Gate.

  • A ticket and the spec disagree, or the spec is ambiguous. The agent stops with a SPEC-QUESTION: line. In an interactive session its user opens a spec pull request; in CI the marker fails the pipeline and sends the question to the Change Owner.
  • A change adds behavior the spec does not describe. The critic ends with a SPEC-GAP verdict, which returns the item to the gate.
  • The spec and the code drift apart. Contracts that are no longer pending run in every pipeline, so a change that breaks one fails the build, and the Change Owner decides whether the code or the spec is wrong.

Legacy code gains specs where it changes

Few enterprises can write specs for an existing domain before they start, and I do not recommend trying. I write the spec for the behavior a change touches, when the first change touches it, with contracts that pin the current behavior next to the cases the change alters. Untouched code stays unspecified, and the critic’s spec-gap verdict surfaces those edges. A legacy service with thin tests stays at Level 2, where a person approves each agent step, while agents draft characterization contracts and the Change Owner decides at the Spec Gate whether to approve them.

The gate needs a service level and an owner for each role

An approval step before coding is a serial stage, and a serial stage that grows can erase the gain from faster coding. I therefore measure the Spec Gate as one: the time from a spec pull request opening to its approval, reported per domain next to review wait. My recommended starting target is one business day at the 90th percentile. A domain that misses it repeatedly has a staffing problem at the gate, and the remedy is a deputy Change Owner or a smaller domain. Letting agents start before approval would shorten the wait and move its cost to rework after release.

Exhibit 3 The roles exist already; each gets a written expectation at the gate
RoleExpectation at Level 3Signal to watch
Product owner or analystWrites the ticket delta the spec change starts from, as cases with expected outcomesSpec-gap returns
Domain engineerTurns the ticket into a spec change and drives the agentsEscalations to Level 2
Change OwnerApproves the spec change, and later accepts or returns the implementation on evidenceSpec Gate wait
Deputy Change OwnerHolds the same approval right and covers absence and loadSpec Gate wait during absences

Source: Author's reference design, The Enterprise Agentic Software Factory.

Two counts, taken per domain each month, show whether the gate is keeping up with the business. Rising spec-gap returns mean the specs lag the business, and the fix is practice in writing rules as cases. A rising Spec Gate wait means the gate is understaffed. A third signal comes later: rework deployments above the service’s own baseline, which is where intent debt that passed the gate finally shows.

For a team starting this month, the first steps are concrete: pick one domain, write the spec for the next change that touches it, add the CODEOWNERS lines and the branch rule, and make spec-trace a required check on that repository.